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 /* 38 * Rpc op calls, generally called from the vnode op calls or through the 39 * buffer cache, for NFS v2, 3 and 4. 40 * These do not normally make any changes to vnode arguments or use 41 * structures that might change between the VFS variants. The returned 42 * arguments are all at the end, after the NFSPROC_T *p one. 43 */ 44 45 #ifndef APPLEKEXT 46 #include <fs/nfs/nfsport.h> 47 48 /* 49 * Global variables 50 */ 51 extern int nfs_numnfscbd; 52 extern struct timeval nfsboottime; 53 extern u_int32_t newnfs_false, newnfs_true; 54 extern nfstype nfsv34_type[9]; 55 extern int nfsrv_useacl; 56 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN]; 57 NFSCLSTATEMUTEX; 58 int nfstest_outofseq = 0; 59 int nfscl_assumeposixlocks = 1; 60 int nfscl_enablecallb = 0; 61 short nfsv4_cbport = NFSV4_CBPORT; 62 int nfstest_openallsetattr = 0; 63 #endif /* !APPLEKEXT */ 64 65 #define DIRHDSIZ (sizeof (struct dirent) - (MAXNAMLEN + 1)) 66 67 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *, 68 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *); 69 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *, 70 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *); 71 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, u_char *, 72 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, 73 void *); 74 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *, 75 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *, 76 struct nfsvattr *, struct nfsfh **, int *, int *, void *); 77 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *, 78 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *, 79 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *, 80 int *, void *, int *); 81 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *, 82 struct nfscllockowner *, u_int64_t, u_int64_t, 83 u_int32_t, struct ucred *, NFSPROC_T *, int); 84 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *, 85 struct acl *, nfsv4stateid_t *, void *); 86 87 /* 88 * nfs null call from vfs. 89 */ 90 APPLESTATIC int 91 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p) 92 { 93 int error; 94 struct nfsrv_descript nfsd, *nd = &nfsd; 95 96 NFSCL_REQSTART(nd, NFSPROC_NULL, vp); 97 error = nfscl_request(nd, vp, p, cred, NULL); 98 if (nd->nd_repstat && !error) 99 error = nd->nd_repstat; 100 mbuf_freem(nd->nd_mrep); 101 return (error); 102 } 103 104 /* 105 * nfs access rpc op. 106 * For nfs version 3 and 4, use the access rpc to check accessibility. If file 107 * modes are changed on the server, accesses might still fail later. 108 */ 109 APPLESTATIC int 110 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred, 111 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp) 112 { 113 int error; 114 u_int32_t mode, rmode; 115 116 if (acmode & VREAD) 117 mode = NFSACCESS_READ; 118 else 119 mode = 0; 120 if (vnode_vtype(vp) == VDIR) { 121 if (acmode & VWRITE) 122 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND | 123 NFSACCESS_DELETE); 124 if (acmode & VEXEC) 125 mode |= NFSACCESS_LOOKUP; 126 } else { 127 if (acmode & VWRITE) 128 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND); 129 if (acmode & VEXEC) 130 mode |= NFSACCESS_EXECUTE; 131 } 132 133 /* 134 * Now, just call nfsrpc_accessrpc() to do the actual RPC. 135 */ 136 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode, 137 NULL); 138 139 /* 140 * The NFS V3 spec does not clarify whether or not 141 * the returned access bits can be a superset of 142 * the ones requested, so... 143 */ 144 if (!error && (rmode & mode) != mode) 145 error = EACCES; 146 return (error); 147 } 148 149 /* 150 * The actual rpc, separated out for Darwin. 151 */ 152 APPLESTATIC int 153 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred, 154 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep, 155 void *stuff) 156 { 157 u_int32_t *tl; 158 u_int32_t supported, rmode; 159 int error; 160 struct nfsrv_descript nfsd, *nd = &nfsd; 161 nfsattrbit_t attrbits; 162 163 *attrflagp = 0; 164 supported = mode; 165 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp); 166 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 167 *tl = txdr_unsigned(mode); 168 if (nd->nd_flag & ND_NFSV4) { 169 /* 170 * And do a Getattr op. 171 */ 172 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 173 *tl = txdr_unsigned(NFSV4OP_GETATTR); 174 NFSGETATTR_ATTRBIT(&attrbits); 175 (void) nfsrv_putattrbit(nd, &attrbits); 176 } 177 error = nfscl_request(nd, vp, p, cred, stuff); 178 if (error) 179 return (error); 180 if (nd->nd_flag & ND_NFSV3) { 181 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 182 if (error) 183 goto nfsmout; 184 } 185 if (!nd->nd_repstat) { 186 if (nd->nd_flag & ND_NFSV4) { 187 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 188 supported = fxdr_unsigned(u_int32_t, *tl++); 189 } else { 190 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 191 } 192 rmode = fxdr_unsigned(u_int32_t, *tl); 193 if (nd->nd_flag & ND_NFSV4) 194 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 195 196 /* 197 * It's not obvious what should be done about 198 * unsupported access modes. For now, be paranoid 199 * and clear the unsupported ones. 200 */ 201 rmode &= supported; 202 *rmodep = rmode; 203 } else 204 error = nd->nd_repstat; 205 nfsmout: 206 mbuf_freem(nd->nd_mrep); 207 return (error); 208 } 209 210 /* 211 * nfs open rpc 212 */ 213 APPLESTATIC int 214 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p) 215 { 216 struct nfsclopen *op; 217 struct nfscldeleg *dp; 218 struct nfsfh *nfhp; 219 struct nfsnode *np = VTONFS(vp); 220 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 221 u_int32_t mode, clidrev; 222 int ret, newone, error, expireret = 0, retrycnt; 223 224 /* 225 * For NFSv4, Open Ops are only done on Regular Files. 226 */ 227 if (vnode_vtype(vp) != VREG) 228 return (0); 229 mode = 0; 230 if (amode & FREAD) 231 mode |= NFSV4OPEN_ACCESSREAD; 232 if (amode & FWRITE) 233 mode |= NFSV4OPEN_ACCESSWRITE; 234 nfhp = np->n_fhp; 235 236 retrycnt = 0; 237 #ifdef notdef 238 { char name[100]; int namel; 239 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99; 240 bcopy(NFS4NODENAME(np->n_v4), name, namel); 241 name[namel] = '\0'; 242 printf("rpcopen p=0x%x name=%s",p->p_pid,name); 243 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]); 244 else printf(" fhl=0\n"); 245 } 246 #endif 247 do { 248 dp = NULL; 249 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1, 250 cred, p, NULL, &op, &newone, &ret, 1); 251 if (error) { 252 return (error); 253 } 254 if (nmp->nm_clp != NULL) 255 clidrev = nmp->nm_clp->nfsc_clientidrev; 256 else 257 clidrev = 0; 258 if (ret == NFSCLOPEN_DOOPEN) { 259 if (np->n_v4 != NULL) { 260 error = nfsrpc_openrpc(nmp, vp, np->n_v4->n4_data, 261 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh, 262 np->n_fhp->nfh_len, mode, op, 263 NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &dp, 264 0, 0x0, cred, p, 0, 0); 265 if (dp != NULL) { 266 #ifdef APPLE 267 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag); 268 #else 269 NFSLOCKNODE(np); 270 np->n_flag &= ~NDELEGMOD; 271 NFSUNLOCKNODE(np); 272 #endif 273 (void) nfscl_deleg(nmp->nm_mountp, 274 op->nfso_own->nfsow_clp, 275 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp); 276 } 277 } else { 278 error = EIO; 279 } 280 newnfs_copyincred(cred, &op->nfso_cred); 281 } 282 283 /* 284 * nfso_opencnt is the count of how many VOP_OPEN()s have 285 * been done on this Open successfully and a VOP_CLOSE() 286 * is expected for each of these. 287 * If error is non-zero, don't increment it, since the Open 288 * hasn't succeeded yet. 289 */ 290 if (!error) 291 op->nfso_opencnt++; 292 nfscl_openrelease(op, error, newone); 293 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 294 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) { 295 (void) nfs_catnap(PZERO, "nfs_open"); 296 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) 297 && clidrev != 0) { 298 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 299 retrycnt++; 300 } 301 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 302 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 303 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 304 expireret == 0 && clidrev != 0 && retrycnt < 4)); 305 if (error && retrycnt >= 4) 306 error = EIO; 307 return (error); 308 } 309 310 /* 311 * the actual open rpc 312 */ 313 APPLESTATIC int 314 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen, 315 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op, 316 u_int8_t *name, int namelen, struct nfscldeleg **dpp, 317 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p, 318 int syscred, int recursed) 319 { 320 u_int32_t *tl; 321 struct nfsrv_descript nfsd, *nd = &nfsd; 322 struct nfscldeleg *dp, *ndp = NULL; 323 struct nfsvattr nfsva; 324 u_int32_t rflags, deleg; 325 nfsattrbit_t attrbits; 326 int error, ret, acesize, limitby; 327 328 dp = *dpp; 329 *dpp = NULL; 330 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL); 331 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 332 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 333 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH); 334 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 335 *tl++ = op->nfso_own->nfsow_clp->nfsc_clientid.lval[0]; 336 *tl = op->nfso_own->nfsow_clp->nfsc_clientid.lval[1]; 337 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN); 338 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 339 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE); 340 if (reclaim) { 341 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS); 342 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 343 *tl = txdr_unsigned(delegtype); 344 } else { 345 if (dp != NULL) { 346 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR); 347 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 348 *tl++ = dp->nfsdl_stateid.seqid; 349 *tl++ = dp->nfsdl_stateid.other[0]; 350 *tl++ = dp->nfsdl_stateid.other[1]; 351 *tl = dp->nfsdl_stateid.other[2]; 352 } else { 353 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 354 } 355 (void) nfsm_strtom(nd, name, namelen); 356 } 357 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 358 *tl = txdr_unsigned(NFSV4OP_GETATTR); 359 NFSZERO_ATTRBIT(&attrbits); 360 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 361 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 362 (void) nfsrv_putattrbit(nd, &attrbits); 363 if (syscred) 364 nd->nd_flag |= ND_USEGSSNAME; 365 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 366 NFS_PROG, NFS_VER4, NULL, 1, NULL); 367 if (error) 368 return (error); 369 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 370 if (!nd->nd_repstat) { 371 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 372 6 * NFSX_UNSIGNED); 373 op->nfso_stateid.seqid = *tl++; 374 op->nfso_stateid.other[0] = *tl++; 375 op->nfso_stateid.other[1] = *tl++; 376 op->nfso_stateid.other[2] = *tl; 377 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 378 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 379 if (error) 380 goto nfsmout; 381 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 382 deleg = fxdr_unsigned(u_int32_t, *tl); 383 if (deleg == NFSV4OPEN_DELEGATEREAD || 384 deleg == NFSV4OPEN_DELEGATEWRITE) { 385 if (!(op->nfso_own->nfsow_clp->nfsc_flags & 386 NFSCLFLAGS_FIRSTDELEG)) 387 op->nfso_own->nfsow_clp->nfsc_flags |= 388 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 389 MALLOC(ndp, struct nfscldeleg *, 390 sizeof (struct nfscldeleg) + newfhlen, 391 M_NFSCLDELEG, M_WAITOK); 392 LIST_INIT(&ndp->nfsdl_owner); 393 LIST_INIT(&ndp->nfsdl_lock); 394 ndp->nfsdl_clp = op->nfso_own->nfsow_clp; 395 ndp->nfsdl_fhlen = newfhlen; 396 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen); 397 newnfs_copyincred(cred, &ndp->nfsdl_cred); 398 nfscl_lockinit(&ndp->nfsdl_rwlock); 399 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 400 NFSX_UNSIGNED); 401 ndp->nfsdl_stateid.seqid = *tl++; 402 ndp->nfsdl_stateid.other[0] = *tl++; 403 ndp->nfsdl_stateid.other[1] = *tl++; 404 ndp->nfsdl_stateid.other[2] = *tl++; 405 ret = fxdr_unsigned(int, *tl); 406 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 407 ndp->nfsdl_flags = NFSCLDL_WRITE; 408 /* 409 * Indicates how much the file can grow. 410 */ 411 NFSM_DISSECT(tl, u_int32_t *, 412 3 * NFSX_UNSIGNED); 413 limitby = fxdr_unsigned(int, *tl++); 414 switch (limitby) { 415 case NFSV4OPEN_LIMITSIZE: 416 ndp->nfsdl_sizelimit = fxdr_hyper(tl); 417 break; 418 case NFSV4OPEN_LIMITBLOCKS: 419 ndp->nfsdl_sizelimit = 420 fxdr_unsigned(u_int64_t, *tl++); 421 ndp->nfsdl_sizelimit *= 422 fxdr_unsigned(u_int64_t, *tl); 423 break; 424 default: 425 error = NFSERR_BADXDR; 426 goto nfsmout; 427 }; 428 } else { 429 ndp->nfsdl_flags = NFSCLDL_READ; 430 } 431 if (ret) 432 ndp->nfsdl_flags |= NFSCLDL_RECALL; 433 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret, 434 &acesize, p); 435 if (error) 436 goto nfsmout; 437 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 438 error = NFSERR_BADXDR; 439 goto nfsmout; 440 } 441 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 442 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 443 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 444 NULL, NULL, NULL, p, cred); 445 if (error) 446 goto nfsmout; 447 if (ndp != NULL) { 448 ndp->nfsdl_change = nfsva.na_filerev; 449 ndp->nfsdl_modtime = nfsva.na_mtime; 450 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET; 451 } 452 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) { 453 do { 454 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op, 455 cred, p); 456 if (ret == NFSERR_DELAY) 457 (void) nfs_catnap(PZERO, "nfs_open"); 458 } while (ret == NFSERR_DELAY); 459 error = ret; 460 } 461 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) || 462 nfscl_assumeposixlocks) 463 op->nfso_posixlock = 1; 464 else 465 op->nfso_posixlock = 0; 466 467 /* 468 * If the server is handing out delegations, but we didn't 469 * get one because an OpenConfirm was required, try the 470 * Open again, to get a delegation. This is a harmless no-op, 471 * from a server's point of view. 472 */ 473 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) && 474 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) 475 && !error && dp == NULL && ndp == NULL && !recursed) { 476 do { 477 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, 478 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0, 479 cred, p, syscred, 1); 480 if (ret == NFSERR_DELAY) 481 (void) nfs_catnap(PZERO, "nfs_open2"); 482 } while (ret == NFSERR_DELAY); 483 if (ret) { 484 if (ndp != NULL) 485 FREE((caddr_t)ndp, M_NFSCLDELEG); 486 if (ret == NFSERR_STALECLIENTID || 487 ret == NFSERR_STALEDONTRECOVER) 488 error = ret; 489 } 490 } 491 } 492 if (nd->nd_repstat != 0 && error == 0) 493 error = nd->nd_repstat; 494 if (error == NFSERR_STALECLIENTID) 495 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 496 nfsmout: 497 if (!error) 498 *dpp = ndp; 499 else if (ndp != NULL) 500 FREE((caddr_t)ndp, M_NFSCLDELEG); 501 mbuf_freem(nd->nd_mrep); 502 return (error); 503 } 504 505 /* 506 * open downgrade rpc 507 */ 508 APPLESTATIC int 509 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op, 510 struct ucred *cred, NFSPROC_T *p) 511 { 512 u_int32_t *tl; 513 struct nfsrv_descript nfsd, *nd = &nfsd; 514 int error; 515 516 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp); 517 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED); 518 *tl++ = op->nfso_stateid.seqid; 519 *tl++ = op->nfso_stateid.other[0]; 520 *tl++ = op->nfso_stateid.other[1]; 521 *tl++ = op->nfso_stateid.other[2]; 522 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 523 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH); 524 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 525 error = nfscl_request(nd, vp, p, cred, NULL); 526 if (error) 527 return (error); 528 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 529 if (!nd->nd_repstat) { 530 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 531 op->nfso_stateid.seqid = *tl++; 532 op->nfso_stateid.other[0] = *tl++; 533 op->nfso_stateid.other[1] = *tl++; 534 op->nfso_stateid.other[2] = *tl; 535 } 536 if (nd->nd_repstat && error == 0) 537 error = nd->nd_repstat; 538 if (error == NFSERR_STALESTATEID) 539 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 540 nfsmout: 541 mbuf_freem(nd->nd_mrep); 542 return (error); 543 } 544 545 /* 546 * V4 Close operation. 547 */ 548 APPLESTATIC int 549 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p) 550 { 551 struct nfsclclient *clp; 552 int error; 553 554 if (vnode_vtype(vp) != VREG) 555 return (0); 556 if (doclose) 557 error = nfscl_doclose(vp, &clp, p); 558 else 559 error = nfscl_getclose(vp, &clp); 560 if (error) 561 return (error); 562 563 nfscl_clientrelease(clp); 564 return (0); 565 } 566 567 /* 568 * Close the open. 569 */ 570 APPLESTATIC void 571 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p) 572 { 573 struct nfsrv_descript nfsd, *nd = &nfsd; 574 struct nfscllockowner *lp; 575 struct nfscllock *lop, *nlop; 576 struct ucred *tcred; 577 u_int64_t off = 0, len = 0; 578 u_int32_t type = NFSV4LOCKT_READ; 579 int error, do_unlock, trycnt; 580 581 tcred = newnfs_getcred(); 582 newnfs_copycred(&op->nfso_cred, tcred); 583 /* 584 * (Theoretically this could be done in the same 585 * compound as the close, but having multiple 586 * sequenced Ops in the same compound might be 587 * too scary for some servers.) 588 */ 589 if (op->nfso_posixlock) { 590 off = 0; 591 len = NFS64BITSSET; 592 type = NFSV4LOCKT_READ; 593 } 594 595 /* 596 * Since this function is only called from VOP_INACTIVE(), no 597 * other thread will be manipulating this Open. As such, the 598 * lock lists are not being changed by other threads, so it should 599 * be safe to do this without locking. 600 */ 601 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 602 do_unlock = 1; 603 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) { 604 if (op->nfso_posixlock == 0) { 605 off = lop->nfslo_first; 606 len = lop->nfslo_end - lop->nfslo_first; 607 if (lop->nfslo_type == F_WRLCK) 608 type = NFSV4LOCKT_WRITE; 609 else 610 type = NFSV4LOCKT_READ; 611 } 612 if (do_unlock) { 613 trycnt = 0; 614 do { 615 error = nfsrpc_locku(nd, nmp, lp, off, 616 len, type, tcred, p, 0); 617 if ((nd->nd_repstat == NFSERR_GRACE || 618 nd->nd_repstat == NFSERR_DELAY) && 619 error == 0) 620 (void) nfs_catnap(PZERO, 621 "nfs_close"); 622 } while ((nd->nd_repstat == NFSERR_GRACE || 623 nd->nd_repstat == NFSERR_DELAY) && 624 error == 0 && trycnt++ < 5); 625 if (op->nfso_posixlock) 626 do_unlock = 0; 627 } 628 nfscl_freelock(lop, 0); 629 } 630 } 631 632 /* 633 * There could be other Opens for different files on the same 634 * OpenOwner, so locking is required. 635 */ 636 NFSLOCKCLSTATE(); 637 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR); 638 NFSUNLOCKCLSTATE(); 639 do { 640 error = nfscl_tryclose(op, tcred, nmp, p); 641 if (error == NFSERR_GRACE) 642 (void) nfs_catnap(PZERO, "nfs_close"); 643 } while (error == NFSERR_GRACE); 644 NFSLOCKCLSTATE(); 645 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock); 646 647 /* 648 * Move the lockowner to nfsc_defunctlockowner, 649 * so the Renew thread will do the ReleaseLockOwner 650 * Op on it later. There might still be other 651 * opens using the same lockowner name. 652 */ 653 lp = LIST_FIRST(&op->nfso_lock); 654 if (lp != NULL) { 655 while (LIST_NEXT(lp, nfsl_list) != NULL) 656 lp = LIST_NEXT(lp, nfsl_list); 657 LIST_PREPEND(&nmp->nm_clp->nfsc_defunctlockowner, 658 &op->nfso_lock, lp, nfsl_list); 659 LIST_INIT(&op->nfso_lock); 660 } 661 nfscl_freeopen(op, 0); 662 NFSUNLOCKCLSTATE(); 663 NFSFREECRED(tcred); 664 } 665 666 /* 667 * The actual Close RPC. 668 */ 669 APPLESTATIC int 670 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp, 671 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p, 672 int syscred) 673 { 674 u_int32_t *tl; 675 int error; 676 677 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh, 678 op->nfso_fhlen, NULL); 679 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID); 680 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 681 *tl++ = op->nfso_stateid.seqid; 682 *tl++ = op->nfso_stateid.other[0]; 683 *tl++ = op->nfso_stateid.other[1]; 684 *tl = op->nfso_stateid.other[2]; 685 if (syscred) 686 nd->nd_flag |= ND_USEGSSNAME; 687 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 688 NFS_PROG, NFS_VER4, NULL, 1, NULL); 689 if (error) 690 return (error); 691 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 692 if (nd->nd_repstat == 0) 693 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 694 error = nd->nd_repstat; 695 if (error == NFSERR_STALESTATEID) 696 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 697 nfsmout: 698 mbuf_freem(nd->nd_mrep); 699 return (error); 700 } 701 702 /* 703 * V4 Open Confirm RPC. 704 */ 705 APPLESTATIC int 706 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen, 707 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p) 708 { 709 u_int32_t *tl; 710 struct nfsrv_descript nfsd, *nd = &nfsd; 711 int error; 712 713 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, VFSTONFS(vnode_mount(vp)), 714 nfhp, fhlen, NULL); 715 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID); 716 *tl++ = op->nfso_stateid.seqid; 717 *tl++ = op->nfso_stateid.other[0]; 718 *tl++ = op->nfso_stateid.other[1]; 719 *tl++ = op->nfso_stateid.other[2]; 720 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid); 721 error = nfscl_request(nd, vp, p, cred, NULL); 722 if (error) 723 return (error); 724 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 725 if (!nd->nd_repstat) { 726 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 727 op->nfso_stateid.seqid = *tl++; 728 op->nfso_stateid.other[0] = *tl++; 729 op->nfso_stateid.other[1] = *tl++; 730 op->nfso_stateid.other[2] = *tl; 731 } 732 error = nd->nd_repstat; 733 if (error == NFSERR_STALESTATEID) 734 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 735 nfsmout: 736 mbuf_freem(nd->nd_mrep); 737 return (error); 738 } 739 740 /* 741 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs() 742 * when a mount has just occurred and when the server replies NFSERR_EXPIRED. 743 */ 744 APPLESTATIC int 745 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, 746 struct ucred *cred, NFSPROC_T *p) 747 { 748 u_int32_t *tl; 749 struct nfsrv_descript nfsd; 750 struct nfsrv_descript *nd = &nfsd; 751 nfsattrbit_t attrbits; 752 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9]; 753 u_short port; 754 int error, isinet6 = 0, callblen; 755 nfsquad_t confirm; 756 u_int32_t lease; 757 static u_int32_t rev = 0; 758 759 if (nfsboottime.tv_sec == 0) 760 NFSSETBOOTTIME(nfsboottime); 761 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL); 762 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 763 *tl++ = txdr_unsigned(nfsboottime.tv_sec); 764 *tl = txdr_unsigned(rev++); 765 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen); 766 767 /* 768 * set up the callback address 769 */ 770 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 771 *tl = txdr_unsigned(NFS_CALLBCKPROG); 772 callblen = strlen(nfsv4_callbackaddr); 773 if (callblen == 0) 774 cp = nfscl_getmyip(nmp, &isinet6); 775 if (nfscl_enablecallb && nfs_numnfscbd > 0 && 776 (callblen > 0 || cp != NULL)) { 777 port = htons(nfsv4_cbport); 778 cp2 = (u_int8_t *)&port; 779 #ifdef INET6 780 if ((callblen > 0 && 781 strchr(nfsv4_callbackaddr, ':')) || isinet6) { 782 char ip6buf[INET6_ADDRSTRLEN], *ip6add; 783 784 (void) nfsm_strtom(nd, "tcp6", 4); 785 if (callblen == 0) { 786 ip6_sprintf(ip6buf, (struct in6_addr *)cp); 787 ip6add = ip6buf; 788 } else { 789 ip6add = nfsv4_callbackaddr; 790 } 791 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d", 792 ip6add, cp2[0], cp2[1]); 793 } else 794 #endif 795 { 796 (void) nfsm_strtom(nd, "tcp", 3); 797 if (callblen == 0) 798 snprintf(addr, INET6_ADDRSTRLEN + 9, 799 "%d.%d.%d.%d.%d.%d", cp[0], cp[1], 800 cp[2], cp[3], cp2[0], cp2[1]); 801 else 802 snprintf(addr, INET6_ADDRSTRLEN + 9, 803 "%s.%d.%d", nfsv4_callbackaddr, 804 cp2[0], cp2[1]); 805 } 806 (void) nfsm_strtom(nd, addr, strlen(addr)); 807 } else { 808 (void) nfsm_strtom(nd, "tcp", 3); 809 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11); 810 } 811 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 812 *tl = txdr_unsigned(clp->nfsc_cbident); 813 nd->nd_flag |= ND_USEGSSNAME; 814 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 815 NFS_PROG, NFS_VER4, NULL, 1, NULL); 816 if (error) 817 return (error); 818 if (nd->nd_repstat == 0) { 819 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 820 clp->nfsc_clientid.lval[0] = *tl++; 821 clp->nfsc_clientid.lval[1] = *tl++; 822 confirm.lval[0] = *tl++; 823 confirm.lval[1] = *tl; 824 mbuf_freem(nd->nd_mrep); 825 nd->nd_mrep = NULL; 826 827 /* 828 * and confirm it. 829 */ 830 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL); 831 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 832 *tl++ = clp->nfsc_clientid.lval[0]; 833 *tl++ = clp->nfsc_clientid.lval[1]; 834 *tl++ = confirm.lval[0]; 835 *tl = confirm.lval[1]; 836 nd->nd_flag |= ND_USEGSSNAME; 837 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 838 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL); 839 if (error) 840 return (error); 841 mbuf_freem(nd->nd_mrep); 842 nd->nd_mrep = NULL; 843 if (nd->nd_repstat == 0) { 844 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh, 845 nmp->nm_fhsize, NULL); 846 NFSZERO_ATTRBIT(&attrbits); 847 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME); 848 (void) nfsrv_putattrbit(nd, &attrbits); 849 nd->nd_flag |= ND_USEGSSNAME; 850 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 851 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL); 852 if (error) 853 return (error); 854 if (nd->nd_repstat == 0) { 855 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL, 856 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred); 857 if (error) 858 goto nfsmout; 859 clp->nfsc_renew = NFSCL_RENEW(lease); 860 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew; 861 clp->nfsc_clientidrev++; 862 if (clp->nfsc_clientidrev == 0) 863 clp->nfsc_clientidrev++; 864 } 865 } 866 } 867 error = nd->nd_repstat; 868 nfsmout: 869 mbuf_freem(nd->nd_mrep); 870 return (error); 871 } 872 873 /* 874 * nfs getattr call. 875 */ 876 APPLESTATIC int 877 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 878 struct nfsvattr *nap, void *stuff) 879 { 880 struct nfsrv_descript nfsd, *nd = &nfsd; 881 int error; 882 nfsattrbit_t attrbits; 883 884 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 885 if (nd->nd_flag & ND_NFSV4) { 886 NFSGETATTR_ATTRBIT(&attrbits); 887 (void) nfsrv_putattrbit(nd, &attrbits); 888 } 889 error = nfscl_request(nd, vp, p, cred, stuff); 890 if (error) 891 return (error); 892 if (!nd->nd_repstat) 893 error = nfsm_loadattr(nd, nap); 894 else 895 error = nd->nd_repstat; 896 mbuf_freem(nd->nd_mrep); 897 return (error); 898 } 899 900 /* 901 * nfs getattr call with non-vnode arguemnts. 902 */ 903 APPLESTATIC int 904 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred, 905 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp) 906 { 907 struct nfsrv_descript nfsd, *nd = &nfsd; 908 int error, vers = NFS_VER2; 909 nfsattrbit_t attrbits; 910 911 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL); 912 if (nd->nd_flag & ND_NFSV4) { 913 vers = NFS_VER4; 914 NFSGETATTR_ATTRBIT(&attrbits); 915 (void) nfsrv_putattrbit(nd, &attrbits); 916 } else if (nd->nd_flag & ND_NFSV3) { 917 vers = NFS_VER3; 918 } 919 if (syscred) 920 nd->nd_flag |= ND_USEGSSNAME; 921 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 922 NFS_PROG, vers, NULL, 1, xidp); 923 if (error) 924 return (error); 925 if (!nd->nd_repstat) 926 error = nfsm_loadattr(nd, nap); 927 else 928 error = nd->nd_repstat; 929 mbuf_freem(nd->nd_mrep); 930 return (error); 931 } 932 933 /* 934 * Do an nfs setattr operation. 935 */ 936 APPLESTATIC int 937 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp, 938 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp, 939 void *stuff) 940 { 941 int error, expireret = 0, openerr, retrycnt; 942 u_int32_t clidrev = 0, mode; 943 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 944 struct nfsfh *nfhp; 945 nfsv4stateid_t stateid; 946 void *lckp; 947 948 if (nmp->nm_clp != NULL) 949 clidrev = nmp->nm_clp->nfsc_clientidrev; 950 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size)) 951 mode = NFSV4OPEN_ACCESSWRITE; 952 else 953 mode = NFSV4OPEN_ACCESSREAD; 954 retrycnt = 0; 955 do { 956 lckp = NULL; 957 openerr = 1; 958 if (NFSHASNFSV4(nmp)) { 959 nfhp = VTONFS(vp)->n_fhp; 960 error = nfscl_getstateid(vp, nfhp->nfh_fh, 961 nfhp->nfh_len, mode, cred, p, &stateid, &lckp); 962 if (error && vnode_vtype(vp) == VREG && 963 (mode == NFSV4OPEN_ACCESSWRITE || 964 nfstest_openallsetattr)) { 965 /* 966 * No Open stateid, so try and open the file 967 * now. 968 */ 969 if (mode == NFSV4OPEN_ACCESSWRITE) 970 openerr = nfsrpc_open(vp, FWRITE, cred, 971 p); 972 else 973 openerr = nfsrpc_open(vp, FREAD, cred, 974 p); 975 if (!openerr) 976 (void) nfscl_getstateid(vp, 977 nfhp->nfh_fh, nfhp->nfh_len, 978 mode, cred, p, &stateid, &lckp); 979 } 980 } 981 if (vap != NULL) 982 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p, 983 rnap, attrflagp, stuff); 984 else 985 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid, 986 stuff); 987 if (error == NFSERR_STALESTATEID) 988 nfscl_initiate_recovery(nmp->nm_clp); 989 if (lckp != NULL) 990 nfscl_lockderef(lckp); 991 if (!openerr) 992 (void) nfsrpc_close(vp, 0, p); 993 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 994 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 995 error == NFSERR_OLDSTATEID) { 996 (void) nfs_catnap(PZERO, "nfs_setattr"); 997 } else if ((error == NFSERR_EXPIRED || 998 error == NFSERR_BADSTATEID) && clidrev != 0) { 999 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1000 } 1001 retrycnt++; 1002 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1003 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1004 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 1005 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1006 expireret == 0 && clidrev != 0 && retrycnt < 4)); 1007 if (error && retrycnt >= 4) 1008 error = EIO; 1009 return (error); 1010 } 1011 1012 static int 1013 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap, 1014 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p, 1015 struct nfsvattr *rnap, int *attrflagp, void *stuff) 1016 { 1017 u_int32_t *tl; 1018 struct nfsrv_descript nfsd, *nd = &nfsd; 1019 int error; 1020 nfsattrbit_t attrbits; 1021 1022 *attrflagp = 0; 1023 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp); 1024 if (nd->nd_flag & ND_NFSV4) 1025 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 1026 vap->va_type = vnode_vtype(vp); 1027 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0); 1028 if (nd->nd_flag & ND_NFSV3) { 1029 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1030 *tl = newnfs_false; 1031 } else if (nd->nd_flag & ND_NFSV4) { 1032 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1033 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1034 NFSGETATTR_ATTRBIT(&attrbits); 1035 (void) nfsrv_putattrbit(nd, &attrbits); 1036 } 1037 error = nfscl_request(nd, vp, p, cred, stuff); 1038 if (error) 1039 return (error); 1040 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 1041 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff); 1042 if ((nd->nd_flag & ND_NFSV4) && !error) 1043 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1044 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error) 1045 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff); 1046 mbuf_freem(nd->nd_mrep); 1047 if (nd->nd_repstat && !error) 1048 error = nd->nd_repstat; 1049 return (error); 1050 } 1051 1052 /* 1053 * nfs lookup rpc 1054 */ 1055 APPLESTATIC int 1056 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred, 1057 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap, 1058 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff) 1059 { 1060 u_int32_t *tl; 1061 struct nfsrv_descript nfsd, *nd = &nfsd; 1062 struct nfsmount *nmp; 1063 struct nfsnode *np; 1064 struct nfsfh *nfhp; 1065 nfsattrbit_t attrbits; 1066 int error = 0, lookupp = 0; 1067 1068 *attrflagp = 0; 1069 *dattrflagp = 0; 1070 if (vnode_vtype(dvp) != VDIR) 1071 return (ENOTDIR); 1072 nmp = VFSTONFS(vnode_mount(dvp)); 1073 if (len > NFS_MAXNAMLEN) 1074 return (ENAMETOOLONG); 1075 if (NFSHASNFSV4(nmp) && len == 1 && 1076 name[0] == '.') { 1077 /* 1078 * Just return the current dir's fh. 1079 */ 1080 np = VTONFS(dvp); 1081 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + 1082 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK); 1083 nfhp->nfh_len = np->n_fhp->nfh_len; 1084 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len); 1085 *nfhpp = nfhp; 1086 return (0); 1087 } 1088 if (NFSHASNFSV4(nmp) && len == 2 && 1089 name[0] == '.' && name[1] == '.') { 1090 lookupp = 1; 1091 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp); 1092 } else { 1093 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp); 1094 (void) nfsm_strtom(nd, name, len); 1095 } 1096 if (nd->nd_flag & ND_NFSV4) { 1097 NFSGETATTR_ATTRBIT(&attrbits); 1098 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1099 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 1100 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1101 (void) nfsrv_putattrbit(nd, &attrbits); 1102 } 1103 error = nfscl_request(nd, dvp, p, cred, stuff); 1104 if (error) 1105 return (error); 1106 if (nd->nd_repstat) { 1107 /* 1108 * When an NFSv4 Lookupp returns ENOENT, it means that 1109 * the lookup is at the root of an fs, so return this dir. 1110 */ 1111 if (nd->nd_repstat == NFSERR_NOENT && lookupp) { 1112 np = VTONFS(dvp); 1113 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + 1114 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK); 1115 nfhp->nfh_len = np->n_fhp->nfh_len; 1116 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len); 1117 *nfhpp = nfhp; 1118 mbuf_freem(nd->nd_mrep); 1119 return (0); 1120 } 1121 if (nd->nd_flag & ND_NFSV3) 1122 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff); 1123 goto nfsmout; 1124 } 1125 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 1126 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1127 if (*(tl + 1)) { 1128 nd->nd_flag |= ND_NOMOREDATA; 1129 goto nfsmout; 1130 } 1131 } 1132 error = nfsm_getfh(nd, nfhpp); 1133 if (error) 1134 goto nfsmout; 1135 1136 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1137 if ((nd->nd_flag & ND_NFSV3) && !error) 1138 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff); 1139 nfsmout: 1140 mbuf_freem(nd->nd_mrep); 1141 if (!error && nd->nd_repstat) 1142 error = nd->nd_repstat; 1143 return (error); 1144 } 1145 1146 /* 1147 * Do a readlink rpc. 1148 */ 1149 APPLESTATIC int 1150 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred, 1151 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 1152 { 1153 u_int32_t *tl; 1154 struct nfsrv_descript nfsd, *nd = &nfsd; 1155 struct nfsnode *np = VTONFS(vp); 1156 nfsattrbit_t attrbits; 1157 int error, len, cangetattr = 1; 1158 1159 *attrflagp = 0; 1160 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp); 1161 if (nd->nd_flag & ND_NFSV4) { 1162 /* 1163 * And do a Getattr op. 1164 */ 1165 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1166 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1167 NFSGETATTR_ATTRBIT(&attrbits); 1168 (void) nfsrv_putattrbit(nd, &attrbits); 1169 } 1170 error = nfscl_request(nd, vp, p, cred, stuff); 1171 if (error) 1172 return (error); 1173 if (nd->nd_flag & ND_NFSV3) 1174 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1175 if (!nd->nd_repstat && !error) { 1176 NFSM_STRSIZ(len, NFS_MAXPATHLEN); 1177 /* 1178 * This seems weird to me, but must have been added to 1179 * FreeBSD for some reason. The only thing I can think of 1180 * is that there was/is some server that replies with 1181 * more link data than it should? 1182 */ 1183 if (len == NFS_MAXPATHLEN) { 1184 NFSLOCKNODE(np); 1185 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) { 1186 len = np->n_size; 1187 cangetattr = 0; 1188 } 1189 NFSUNLOCKNODE(np); 1190 } 1191 error = nfsm_mbufuio(nd, uiop, len); 1192 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr) 1193 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1194 } 1195 if (nd->nd_repstat && !error) 1196 error = nd->nd_repstat; 1197 nfsmout: 1198 mbuf_freem(nd->nd_mrep); 1199 return (error); 1200 } 1201 1202 /* 1203 * Read operation. 1204 */ 1205 APPLESTATIC int 1206 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred, 1207 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 1208 { 1209 int error, expireret = 0, retrycnt; 1210 u_int32_t clidrev = 0; 1211 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1212 struct nfsnode *np = VTONFS(vp); 1213 struct ucred *newcred; 1214 struct nfsfh *nfhp = NULL; 1215 nfsv4stateid_t stateid; 1216 void *lckp; 1217 1218 if (nmp->nm_clp != NULL) 1219 clidrev = nmp->nm_clp->nfsc_clientidrev; 1220 newcred = cred; 1221 if (NFSHASNFSV4(nmp)) { 1222 nfhp = np->n_fhp; 1223 if (p == NULL) 1224 newcred = NFSNEWCRED(cred); 1225 } 1226 retrycnt = 0; 1227 do { 1228 lckp = NULL; 1229 if (NFSHASNFSV4(nmp)) 1230 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 1231 NFSV4OPEN_ACCESSREAD, newcred, p, &stateid, &lckp); 1232 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap, 1233 attrflagp, stuff); 1234 if (error == NFSERR_STALESTATEID) 1235 nfscl_initiate_recovery(nmp->nm_clp); 1236 if (lckp != NULL) 1237 nfscl_lockderef(lckp); 1238 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1239 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1240 error == NFSERR_OLDSTATEID) { 1241 (void) nfs_catnap(PZERO, "nfs_read"); 1242 } else if ((error == NFSERR_EXPIRED || 1243 error == NFSERR_BADSTATEID) && clidrev != 0) { 1244 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1245 } 1246 retrycnt++; 1247 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1248 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1249 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 1250 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1251 expireret == 0 && clidrev != 0 && retrycnt < 4)); 1252 if (error && retrycnt >= 4) 1253 error = EIO; 1254 if (NFSHASNFSV4(nmp) && p == NULL) 1255 NFSFREECRED(newcred); 1256 return (error); 1257 } 1258 1259 /* 1260 * The actual read RPC. 1261 */ 1262 static int 1263 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred, 1264 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap, 1265 int *attrflagp, void *stuff) 1266 { 1267 u_int32_t *tl; 1268 int error = 0, len, retlen, tsiz, eof = 0; 1269 struct nfsrv_descript nfsd; 1270 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1271 struct nfsrv_descript *nd = &nfsd; 1272 1273 *attrflagp = 0; 1274 tsiz = uio_uio_resid(uiop); 1275 if (uiop->uio_offset + tsiz > 0xffffffff && 1276 !NFSHASNFSV3OR4(nmp)) 1277 return (EFBIG); 1278 nd->nd_mrep = NULL; 1279 while (tsiz > 0) { 1280 *attrflagp = 0; 1281 len = (tsiz > nmp->nm_rsize) ? nmp->nm_rsize : tsiz; 1282 NFSCL_REQSTART(nd, NFSPROC_READ, vp); 1283 if (nd->nd_flag & ND_NFSV4) 1284 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 1285 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3); 1286 if (nd->nd_flag & ND_NFSV2) { 1287 *tl++ = txdr_unsigned(uiop->uio_offset); 1288 *tl++ = txdr_unsigned(len); 1289 *tl = 0; 1290 } else { 1291 txdr_hyper(uiop->uio_offset, tl); 1292 *(tl + 2) = txdr_unsigned(len); 1293 } 1294 /* 1295 * Since I can't do a Getattr for NFSv4 for Write, there 1296 * doesn't seem any point in doing one here, either. 1297 * (See the comment in nfsrpc_writerpc() for more info.) 1298 */ 1299 error = nfscl_request(nd, vp, p, cred, stuff); 1300 if (error) 1301 return (error); 1302 if (nd->nd_flag & ND_NFSV3) { 1303 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1304 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) { 1305 error = nfsm_loadattr(nd, nap); 1306 if (!error) 1307 *attrflagp = 1; 1308 } 1309 if (nd->nd_repstat || error) { 1310 if (!error) 1311 error = nd->nd_repstat; 1312 goto nfsmout; 1313 } 1314 if (nd->nd_flag & ND_NFSV3) { 1315 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1316 eof = fxdr_unsigned(int, *(tl + 1)); 1317 } else if (nd->nd_flag & ND_NFSV4) { 1318 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1319 eof = fxdr_unsigned(int, *tl); 1320 } 1321 NFSM_STRSIZ(retlen, nmp->nm_rsize); 1322 error = nfsm_mbufuio(nd, uiop, retlen); 1323 if (error) 1324 goto nfsmout; 1325 mbuf_freem(nd->nd_mrep); 1326 nd->nd_mrep = NULL; 1327 tsiz -= retlen; 1328 if (!(nd->nd_flag & ND_NFSV2)) { 1329 if (eof || retlen == 0) 1330 tsiz = 0; 1331 } else if (retlen < len) 1332 tsiz = 0; 1333 } 1334 return (0); 1335 nfsmout: 1336 if (nd->nd_mrep != NULL) 1337 mbuf_freem(nd->nd_mrep); 1338 return (error); 1339 } 1340 1341 /* 1342 * nfs write operation 1343 */ 1344 APPLESTATIC int 1345 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, u_char *verfp, 1346 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 1347 void *stuff) 1348 { 1349 int error, expireret = 0, retrycnt, nostateid; 1350 u_int32_t clidrev = 0; 1351 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1352 struct nfsnode *np = VTONFS(vp); 1353 struct ucred *newcred; 1354 struct nfsfh *nfhp = NULL; 1355 nfsv4stateid_t stateid; 1356 void *lckp; 1357 1358 if (nmp->nm_clp != NULL) 1359 clidrev = nmp->nm_clp->nfsc_clientidrev; 1360 newcred = cred; 1361 if (NFSHASNFSV4(nmp)) { 1362 if (p == NULL) 1363 newcred = NFSNEWCRED(cred); 1364 nfhp = np->n_fhp; 1365 } 1366 retrycnt = 0; 1367 do { 1368 lckp = NULL; 1369 nostateid = 0; 1370 if (NFSHASNFSV4(nmp)) { 1371 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 1372 NFSV4OPEN_ACCESSWRITE, newcred, p, &stateid, &lckp); 1373 if (stateid.other[0] == 0 && stateid.other[1] == 0 && 1374 stateid.other[2] == 0) { 1375 nostateid = 1; 1376 printf("stateid0 in write\n"); 1377 } 1378 } 1379 1380 /* 1381 * If there is no stateid for NFSv4, it means this is an 1382 * extraneous write after close. Basically a poorly 1383 * implemented buffer cache. Just don't do the write. 1384 */ 1385 if (nostateid) 1386 error = 0; 1387 else 1388 error = nfsrpc_writerpc(vp, uiop, iomode, verfp, 1389 newcred, &stateid, p, nap, attrflagp, stuff); 1390 if (error == NFSERR_BADSTATEID) { 1391 printf("st=0x%x 0x%x 0x%x\n",stateid.other[0],stateid.other[1],stateid.other[2]); 1392 nfscl_dumpstate(nmp, 1, 1, 0, 0); 1393 } 1394 if (error == NFSERR_STALESTATEID) 1395 nfscl_initiate_recovery(nmp->nm_clp); 1396 if (lckp != NULL) 1397 nfscl_lockderef(lckp); 1398 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1399 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1400 error == NFSERR_OLDSTATEID) { 1401 (void) nfs_catnap(PZERO, "nfs_write"); 1402 } else if ((error == NFSERR_EXPIRED || 1403 error == NFSERR_BADSTATEID) && clidrev != 0) { 1404 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1405 } 1406 retrycnt++; 1407 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1408 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1409 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 1410 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1411 expireret == 0 && clidrev != 0 && retrycnt < 4)); 1412 if (error && retrycnt >= 4) 1413 error = EIO; 1414 if (NFSHASNFSV4(nmp) && p == NULL) 1415 NFSFREECRED(newcred); 1416 return (error); 1417 } 1418 1419 /* 1420 * The actual write RPC. 1421 */ 1422 static int 1423 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode, 1424 u_char *verfp, struct ucred *cred, nfsv4stateid_t *stateidp, 1425 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 1426 { 1427 u_int32_t *tl; 1428 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1429 struct nfsnode *np = VTONFS(vp); 1430 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC; 1431 int wccflag = 0, wsize; 1432 int32_t backup; 1433 struct nfsrv_descript nfsd; 1434 struct nfsrv_descript *nd = &nfsd; 1435 nfsattrbit_t attrbits; 1436 1437 #ifdef DIAGNOSTIC 1438 if (uiop->uio_iovcnt != 1) 1439 panic("nfs: writerpc iovcnt > 1"); 1440 #endif 1441 *attrflagp = 0; 1442 tsiz = uio_uio_resid(uiop); 1443 NFSLOCKMNT(nmp); 1444 if (uiop->uio_offset + tsiz > 0xffffffff && 1445 !NFSHASNFSV3OR4(nmp)) { 1446 NFSUNLOCKMNT(nmp); 1447 return (EFBIG); 1448 } 1449 wsize = nmp->nm_wsize; 1450 NFSUNLOCKMNT(nmp); 1451 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */ 1452 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */ 1453 while (tsiz > 0) { 1454 nmp = VFSTONFS(vnode_mount(vp)); 1455 if (nmp == NULL) { 1456 error = ENXIO; 1457 goto nfsmout; 1458 } 1459 *attrflagp = 0; 1460 len = (tsiz > wsize) ? wsize : tsiz; 1461 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp); 1462 if (nd->nd_flag & ND_NFSV4) { 1463 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 1464 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED); 1465 txdr_hyper(uiop->uio_offset, tl); 1466 tl += 2; 1467 *tl++ = txdr_unsigned(*iomode); 1468 *tl = txdr_unsigned(len); 1469 } else if (nd->nd_flag & ND_NFSV3) { 1470 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED); 1471 txdr_hyper(uiop->uio_offset, tl); 1472 tl += 2; 1473 *tl++ = txdr_unsigned(len); 1474 *tl++ = txdr_unsigned(*iomode); 1475 *tl = txdr_unsigned(len); 1476 } else { 1477 u_int32_t x; 1478 1479 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1480 /* 1481 * Not sure why someone changed this, since the 1482 * RFC clearly states that "beginoffset" and 1483 * "totalcount" are ignored, but it wouldn't 1484 * surprise me if there's a busted server out there. 1485 */ 1486 /* Set both "begin" and "current" to non-garbage. */ 1487 x = txdr_unsigned((u_int32_t)uiop->uio_offset); 1488 *tl++ = x; /* "begin offset" */ 1489 *tl++ = x; /* "current offset" */ 1490 x = txdr_unsigned(len); 1491 *tl++ = x; /* total to this offset */ 1492 *tl = x; /* size of this write */ 1493 1494 } 1495 nfsm_uiombuf(nd, uiop, len); 1496 /* 1497 * Although it is tempting to do a normal Getattr Op in the 1498 * NFSv4 compound, the result can be a nearly hung client 1499 * system if the Getattr asks for Owner and/or OwnerGroup. 1500 * It occurs when the client can't map either the Owner or 1501 * Owner_group name in the Getattr reply to a uid/gid. When 1502 * there is a cache miss, the kernel does an upcall to the 1503 * nfsuserd. Then, it can try and read the local /etc/passwd 1504 * or /etc/group file. It can then block in getnewbuf(), 1505 * waiting for dirty writes to be pushed to the NFS server. 1506 * The only reason this doesn't result in a complete 1507 * deadlock, is that the upcall times out and allows 1508 * the write to complete. However, progress is so slow 1509 * that it might just as well be deadlocked. 1510 * So, we just get the attributes that change with each 1511 * write Op. 1512 * nb: nfscl_loadattrcache() needs to be told that these 1513 * partial attributes from a write rpc are being 1514 * passed in, via a argument flag. 1515 */ 1516 if (nd->nd_flag & ND_NFSV4) { 1517 NFSWRITEGETATTR_ATTRBIT(&attrbits); 1518 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1519 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1520 (void) nfsrv_putattrbit(nd, &attrbits); 1521 } 1522 error = nfscl_request(nd, vp, p, cred, stuff); 1523 if (error) 1524 return (error); 1525 if (nd->nd_repstat) { 1526 /* 1527 * In case the rpc gets retried, roll 1528 * the uio fileds changed by nfsm_uiombuf() 1529 * back. 1530 */ 1531 uiop->uio_offset -= len; 1532 uio_uio_resid_add(uiop, len); 1533 uio_iov_base_add(uiop, -len); 1534 uio_iov_len_add(uiop, len); 1535 } 1536 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 1537 error = nfscl_wcc_data(nd, vp, nap, attrflagp, 1538 &wccflag, stuff); 1539 if (error) 1540 goto nfsmout; 1541 } 1542 if (!nd->nd_repstat) { 1543 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 1544 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED 1545 + NFSX_VERF); 1546 rlen = fxdr_unsigned(int, *tl++); 1547 if (rlen == 0) { 1548 error = NFSERR_IO; 1549 goto nfsmout; 1550 } else if (rlen < len) { 1551 backup = len - rlen; 1552 uio_iov_base_add(uiop, -(backup)); 1553 uio_iov_len_add(uiop, backup); 1554 uiop->uio_offset -= backup; 1555 uio_uio_resid_add(uiop, backup); 1556 len = rlen; 1557 } 1558 commit = fxdr_unsigned(int, *tl++); 1559 1560 /* 1561 * Return the lowest committment level 1562 * obtained by any of the RPCs. 1563 */ 1564 if (committed == NFSWRITE_FILESYNC) 1565 committed = commit; 1566 else if (committed == NFSWRITE_DATASYNC && 1567 commit == NFSWRITE_UNSTABLE) 1568 committed = commit; 1569 if (verfp != NULL) 1570 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF); 1571 NFSLOCKMNT(nmp); 1572 if (!NFSHASWRITEVERF(nmp)) { 1573 NFSBCOPY((caddr_t)tl, 1574 (caddr_t)&nmp->nm_verf[0], 1575 NFSX_VERF); 1576 NFSSETWRITEVERF(nmp); 1577 } 1578 NFSUNLOCKMNT(nmp); 1579 } 1580 if (nd->nd_flag & ND_NFSV4) 1581 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1582 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) { 1583 error = nfsm_loadattr(nd, nap); 1584 if (!error) 1585 *attrflagp = NFS_LATTR_NOSHRINK; 1586 } 1587 } else { 1588 error = nd->nd_repstat; 1589 } 1590 if (error) 1591 goto nfsmout; 1592 NFSWRITERPC_SETTIME(wccflag, np, (nd->nd_flag & ND_NFSV4)); 1593 mbuf_freem(nd->nd_mrep); 1594 nd->nd_mrep = NULL; 1595 tsiz -= len; 1596 } 1597 nfsmout: 1598 if (nd->nd_mrep != NULL) 1599 mbuf_freem(nd->nd_mrep); 1600 *iomode = committed; 1601 if (nd->nd_repstat && !error) 1602 error = nd->nd_repstat; 1603 return (error); 1604 } 1605 1606 /* 1607 * nfs mknod rpc 1608 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1609 * mode set to specify the file type and the size field for rdev. 1610 */ 1611 APPLESTATIC int 1612 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap, 1613 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p, 1614 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 1615 int *attrflagp, int *dattrflagp, void *dstuff) 1616 { 1617 u_int32_t *tl; 1618 int error = 0; 1619 struct nfsrv_descript nfsd, *nd = &nfsd; 1620 nfsattrbit_t attrbits; 1621 1622 *nfhpp = NULL; 1623 *attrflagp = 0; 1624 *dattrflagp = 0; 1625 if (namelen > NFS_MAXNAMLEN) 1626 return (ENAMETOOLONG); 1627 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp); 1628 if (nd->nd_flag & ND_NFSV4) { 1629 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1630 *tl++ = vtonfsv34_type(vtyp); 1631 *tl++ = txdr_unsigned(NFSMAJOR(rdev)); 1632 *tl = txdr_unsigned(NFSMINOR(rdev)); 1633 } 1634 (void) nfsm_strtom(nd, name, namelen); 1635 if (nd->nd_flag & ND_NFSV3) { 1636 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1637 *tl = vtonfsv34_type(vtyp); 1638 } 1639 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 1640 nfscl_fillsattr(nd, vap, dvp, 0, 0); 1641 if ((nd->nd_flag & ND_NFSV3) && 1642 (vtyp == VCHR || vtyp == VBLK)) { 1643 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1644 *tl++ = txdr_unsigned(NFSMAJOR(rdev)); 1645 *tl = txdr_unsigned(NFSMINOR(rdev)); 1646 } 1647 if (nd->nd_flag & ND_NFSV4) { 1648 NFSGETATTR_ATTRBIT(&attrbits); 1649 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1650 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 1651 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1652 (void) nfsrv_putattrbit(nd, &attrbits); 1653 } 1654 if (nd->nd_flag & ND_NFSV2) 1655 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev); 1656 error = nfscl_request(nd, dvp, p, cred, dstuff); 1657 if (error) 1658 return (error); 1659 if (nd->nd_flag & ND_NFSV4) 1660 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 1661 if (!nd->nd_repstat) { 1662 if (nd->nd_flag & ND_NFSV4) { 1663 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1664 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1665 if (error) 1666 goto nfsmout; 1667 } 1668 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 1669 if (error) 1670 goto nfsmout; 1671 } 1672 if (nd->nd_flag & ND_NFSV3) 1673 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 1674 if (!error && nd->nd_repstat) 1675 error = nd->nd_repstat; 1676 nfsmout: 1677 mbuf_freem(nd->nd_mrep); 1678 return (error); 1679 } 1680 1681 /* 1682 * nfs file create call 1683 * Mostly just call the approriate routine. (I separated out v4, so that 1684 * error recovery wouldn't be as difficult.) 1685 */ 1686 APPLESTATIC int 1687 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap, 1688 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p, 1689 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 1690 int *attrflagp, int *dattrflagp, void *dstuff) 1691 { 1692 int error = 0, newone, expireret = 0, retrycnt, unlocked; 1693 struct nfsclowner *owp; 1694 struct nfscldeleg *dp; 1695 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp)); 1696 u_int32_t clidrev; 1697 1698 if (NFSHASNFSV4(nmp)) { 1699 retrycnt = 0; 1700 do { 1701 dp = NULL; 1702 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE | 1703 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone, 1704 NULL, 1); 1705 if (error) 1706 return (error); 1707 if (nmp->nm_clp != NULL) 1708 clidrev = nmp->nm_clp->nfsc_clientidrev; 1709 else 1710 clidrev = 0; 1711 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, fmode, 1712 owp, &dp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 1713 dstuff, &unlocked); 1714 if (dp != NULL) 1715 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp, 1716 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp); 1717 nfscl_ownerrelease(owp, error, newone, unlocked); 1718 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 1719 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY) { 1720 (void) nfs_catnap(PZERO, "nfs_open"); 1721 } else if ((error == NFSERR_EXPIRED || 1722 error == NFSERR_BADSTATEID) && clidrev != 0) { 1723 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1724 retrycnt++; 1725 } 1726 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 1727 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1728 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1729 expireret == 0 && clidrev != 0 && retrycnt < 4)); 1730 if (error && retrycnt >= 4) 1731 error = EIO; 1732 } else { 1733 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf, 1734 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 1735 dstuff); 1736 } 1737 return (error); 1738 } 1739 1740 /* 1741 * The create rpc for v2 and 3. 1742 */ 1743 static int 1744 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap, 1745 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p, 1746 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 1747 int *attrflagp, int *dattrflagp, void *dstuff) 1748 { 1749 u_int32_t *tl; 1750 int error = 0; 1751 struct nfsrv_descript nfsd, *nd = &nfsd; 1752 1753 *nfhpp = NULL; 1754 *attrflagp = 0; 1755 *dattrflagp = 0; 1756 if (namelen > NFS_MAXNAMLEN) 1757 return (ENAMETOOLONG); 1758 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp); 1759 (void) nfsm_strtom(nd, name, namelen); 1760 if (nd->nd_flag & ND_NFSV3) { 1761 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1762 if (fmode & O_EXCL) { 1763 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE); 1764 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 1765 *tl++ = cverf.lval[0]; 1766 *tl = cverf.lval[1]; 1767 } else { 1768 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 1769 nfscl_fillsattr(nd, vap, dvp, 0, 0); 1770 } 1771 } else { 1772 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0); 1773 } 1774 error = nfscl_request(nd, dvp, p, cred, dstuff); 1775 if (error) 1776 return (error); 1777 if (nd->nd_repstat == 0) { 1778 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 1779 if (error) 1780 goto nfsmout; 1781 } 1782 if (nd->nd_flag & ND_NFSV3) 1783 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 1784 if (nd->nd_repstat != 0 && error == 0) 1785 error = nd->nd_repstat; 1786 nfsmout: 1787 mbuf_freem(nd->nd_mrep); 1788 return (error); 1789 } 1790 1791 static int 1792 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap, 1793 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 1794 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 1795 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 1796 int *dattrflagp, void *dstuff, int *unlockedp) 1797 { 1798 u_int32_t *tl; 1799 int error = 0, deleg, newone, ret, acesize, limitby; 1800 struct nfsrv_descript nfsd, *nd = &nfsd; 1801 struct nfsclopen *op; 1802 struct nfscldeleg *dp = NULL; 1803 struct nfsnode *np; 1804 struct nfsfh *nfhp; 1805 nfsattrbit_t attrbits; 1806 nfsv4stateid_t stateid; 1807 u_int32_t rflags; 1808 1809 *unlockedp = 0; 1810 *nfhpp = NULL; 1811 *dpp = NULL; 1812 *attrflagp = 0; 1813 *dattrflagp = 0; 1814 if (namelen > NFS_MAXNAMLEN) 1815 return (ENAMETOOLONG); 1816 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp); 1817 /* 1818 * For V4, this is actually an Open op. 1819 */ 1820 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1821 *tl++ = txdr_unsigned(owp->nfsow_seqid); 1822 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 1823 NFSV4OPEN_ACCESSREAD); 1824 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE); 1825 *tl++ = owp->nfsow_clp->nfsc_clientid.lval[0]; 1826 *tl = owp->nfsow_clp->nfsc_clientid.lval[1]; 1827 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 1828 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1829 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE); 1830 if (fmode & O_EXCL) { 1831 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE); 1832 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 1833 *tl++ = cverf.lval[0]; 1834 *tl = cverf.lval[1]; 1835 } else { 1836 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 1837 nfscl_fillsattr(nd, vap, dvp, 0, 0); 1838 } 1839 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1840 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 1841 (void) nfsm_strtom(nd, name, namelen); 1842 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1843 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 1844 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1845 NFSGETATTR_ATTRBIT(&attrbits); 1846 (void) nfsrv_putattrbit(nd, &attrbits); 1847 error = nfscl_request(nd, dvp, p, cred, dstuff); 1848 if (error) 1849 return (error); 1850 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 1851 if (error) 1852 goto nfsmout; 1853 NFSCL_INCRSEQID(owp->nfsow_seqid, nd); 1854 if (nd->nd_repstat == 0) { 1855 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 1856 6 * NFSX_UNSIGNED); 1857 stateid.seqid = *tl++; 1858 stateid.other[0] = *tl++; 1859 stateid.other[1] = *tl++; 1860 stateid.other[2] = *tl; 1861 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 1862 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1863 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1864 deleg = fxdr_unsigned(int, *tl); 1865 if (deleg == NFSV4OPEN_DELEGATEREAD || 1866 deleg == NFSV4OPEN_DELEGATEWRITE) { 1867 if (!(owp->nfsow_clp->nfsc_flags & 1868 NFSCLFLAGS_FIRSTDELEG)) 1869 owp->nfsow_clp->nfsc_flags |= 1870 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 1871 MALLOC(dp, struct nfscldeleg *, 1872 sizeof (struct nfscldeleg) + NFSX_V4FHMAX, 1873 M_NFSCLDELEG, M_WAITOK); 1874 LIST_INIT(&dp->nfsdl_owner); 1875 LIST_INIT(&dp->nfsdl_lock); 1876 dp->nfsdl_clp = owp->nfsow_clp; 1877 newnfs_copyincred(cred, &dp->nfsdl_cred); 1878 nfscl_lockinit(&dp->nfsdl_rwlock); 1879 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 1880 NFSX_UNSIGNED); 1881 dp->nfsdl_stateid.seqid = *tl++; 1882 dp->nfsdl_stateid.other[0] = *tl++; 1883 dp->nfsdl_stateid.other[1] = *tl++; 1884 dp->nfsdl_stateid.other[2] = *tl++; 1885 ret = fxdr_unsigned(int, *tl); 1886 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 1887 dp->nfsdl_flags = NFSCLDL_WRITE; 1888 /* 1889 * Indicates how much the file can grow. 1890 */ 1891 NFSM_DISSECT(tl, u_int32_t *, 1892 3 * NFSX_UNSIGNED); 1893 limitby = fxdr_unsigned(int, *tl++); 1894 switch (limitby) { 1895 case NFSV4OPEN_LIMITSIZE: 1896 dp->nfsdl_sizelimit = fxdr_hyper(tl); 1897 break; 1898 case NFSV4OPEN_LIMITBLOCKS: 1899 dp->nfsdl_sizelimit = 1900 fxdr_unsigned(u_int64_t, *tl++); 1901 dp->nfsdl_sizelimit *= 1902 fxdr_unsigned(u_int64_t, *tl); 1903 break; 1904 default: 1905 error = NFSERR_BADXDR; 1906 goto nfsmout; 1907 }; 1908 } else { 1909 dp->nfsdl_flags = NFSCLDL_READ; 1910 } 1911 if (ret) 1912 dp->nfsdl_flags |= NFSCLDL_RECALL; 1913 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret, 1914 &acesize, p); 1915 if (error) 1916 goto nfsmout; 1917 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 1918 error = NFSERR_BADXDR; 1919 goto nfsmout; 1920 } 1921 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 1922 if (error) 1923 goto nfsmout; 1924 if (dp != NULL && *attrflagp) { 1925 dp->nfsdl_change = nnap->na_filerev; 1926 dp->nfsdl_modtime = nnap->na_mtime; 1927 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 1928 } 1929 /* 1930 * We can now complete the Open state. 1931 */ 1932 nfhp = *nfhpp; 1933 if (dp != NULL) { 1934 dp->nfsdl_fhlen = nfhp->nfh_len; 1935 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len); 1936 } 1937 /* 1938 * Get an Open structure that will be 1939 * attached to the OpenOwner, acquired already. 1940 */ 1941 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 1942 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0, 1943 cred, p, NULL, &op, &newone, NULL, 0); 1944 if (error) 1945 goto nfsmout; 1946 op->nfso_stateid = stateid; 1947 newnfs_copyincred(cred, &op->nfso_cred); 1948 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) { 1949 do { 1950 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh, 1951 nfhp->nfh_len, op, cred, p); 1952 if (ret == NFSERR_DELAY) 1953 (void) nfs_catnap(PZERO, "nfs_create"); 1954 } while (ret == NFSERR_DELAY); 1955 error = ret; 1956 } 1957 1958 /* 1959 * If the server is handing out delegations, but we didn't 1960 * get one because an OpenConfirm was required, try the 1961 * Open again, to get a delegation. This is a harmless no-op, 1962 * from a server's point of view. 1963 */ 1964 if ((rflags & NFSV4OPEN_RESULTCONFIRM) && 1965 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) && 1966 !error && dp == NULL) { 1967 np = VTONFS(dvp); 1968 do { 1969 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp, 1970 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 1971 nfhp->nfh_fh, nfhp->nfh_len, 1972 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op, 1973 name, namelen, &dp, 0, 0x0, cred, p, 0, 1); 1974 if (ret == NFSERR_DELAY) 1975 (void) nfs_catnap(PZERO, "nfs_crt2"); 1976 } while (ret == NFSERR_DELAY); 1977 if (ret) { 1978 if (dp != NULL) 1979 FREE((caddr_t)dp, M_NFSCLDELEG); 1980 if (ret == NFSERR_STALECLIENTID || 1981 ret == NFSERR_STALEDONTRECOVER) 1982 error = ret; 1983 } 1984 } 1985 nfscl_openrelease(op, error, newone); 1986 *unlockedp = 1; 1987 } 1988 if (nd->nd_repstat != 0 && error == 0) 1989 error = nd->nd_repstat; 1990 if (error == NFSERR_STALECLIENTID) 1991 nfscl_initiate_recovery(owp->nfsow_clp); 1992 nfsmout: 1993 if (!error) 1994 *dpp = dp; 1995 else if (dp != NULL) 1996 FREE((caddr_t)dp, M_NFSCLDELEG); 1997 mbuf_freem(nd->nd_mrep); 1998 return (error); 1999 } 2000 2001 /* 2002 * Nfs remove rpc 2003 */ 2004 APPLESTATIC int 2005 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp, 2006 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, 2007 void *dstuff) 2008 { 2009 u_int32_t *tl; 2010 struct nfsrv_descript nfsd, *nd = &nfsd; 2011 struct nfsnode *np; 2012 struct nfsmount *nmp; 2013 nfsv4stateid_t dstateid; 2014 int error, ret = 0, i; 2015 2016 *dattrflagp = 0; 2017 if (namelen > NFS_MAXNAMLEN) 2018 return (ENAMETOOLONG); 2019 nmp = VFSTONFS(vnode_mount(dvp)); 2020 tryagain: 2021 if (NFSHASNFSV4(nmp) && ret == 0) { 2022 ret = nfscl_removedeleg(vp, p, &dstateid); 2023 if (ret == 1) { 2024 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp); 2025 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 2026 NFSX_UNSIGNED); 2027 *tl++ = dstateid.seqid; 2028 *tl++ = dstateid.other[0]; 2029 *tl++ = dstateid.other[1]; 2030 *tl++ = dstateid.other[2]; 2031 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2032 np = VTONFS(dvp); 2033 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2034 np->n_fhp->nfh_len, 0); 2035 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2036 *tl = txdr_unsigned(NFSV4OP_REMOVE); 2037 } 2038 } else { 2039 ret = 0; 2040 } 2041 if (ret == 0) 2042 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp); 2043 (void) nfsm_strtom(nd, name, namelen); 2044 error = nfscl_request(nd, dvp, p, cred, dstuff); 2045 if (error) 2046 return (error); 2047 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 2048 /* For NFSv4, parse out any Delereturn replies. */ 2049 if (ret > 0 && nd->nd_repstat != 0 && 2050 (nd->nd_flag & ND_NOMOREDATA)) { 2051 /* 2052 * If the Delegreturn failed, try again without 2053 * it. The server will Recall, as required. 2054 */ 2055 mbuf_freem(nd->nd_mrep); 2056 goto tryagain; 2057 } 2058 for (i = 0; i < (ret * 2); i++) { 2059 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 2060 ND_NFSV4) { 2061 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2062 if (*(tl + 1)) 2063 nd->nd_flag |= ND_NOMOREDATA; 2064 } 2065 } 2066 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2067 } 2068 if (nd->nd_repstat && !error) 2069 error = nd->nd_repstat; 2070 nfsmout: 2071 mbuf_freem(nd->nd_mrep); 2072 return (error); 2073 } 2074 2075 /* 2076 * Do an nfs rename rpc. 2077 */ 2078 APPLESTATIC int 2079 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen, 2080 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred, 2081 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap, 2082 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff) 2083 { 2084 u_int32_t *tl; 2085 struct nfsrv_descript nfsd, *nd = &nfsd; 2086 struct nfsmount *nmp; 2087 struct nfsnode *np; 2088 nfsattrbit_t attrbits; 2089 nfsv4stateid_t fdstateid, tdstateid; 2090 int error = 0, ret = 0, gottd = 0, gotfd = 0, i; 2091 2092 *fattrflagp = 0; 2093 *tattrflagp = 0; 2094 nmp = VFSTONFS(vnode_mount(fdvp)); 2095 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN) 2096 return (ENAMETOOLONG); 2097 tryagain: 2098 if (NFSHASNFSV4(nmp) && ret == 0) { 2099 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp, 2100 &tdstateid, &gottd, p); 2101 if (gotfd && gottd) { 2102 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp); 2103 } else if (gotfd) { 2104 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp); 2105 } else if (gottd) { 2106 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp); 2107 } 2108 if (gotfd) { 2109 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 2110 *tl++ = fdstateid.seqid; 2111 *tl++ = fdstateid.other[0]; 2112 *tl++ = fdstateid.other[1]; 2113 *tl = fdstateid.other[2]; 2114 if (gottd) { 2115 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2116 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2117 np = VTONFS(tvp); 2118 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2119 np->n_fhp->nfh_len, 0); 2120 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2121 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN); 2122 } 2123 } 2124 if (gottd) { 2125 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 2126 *tl++ = tdstateid.seqid; 2127 *tl++ = tdstateid.other[0]; 2128 *tl++ = tdstateid.other[1]; 2129 *tl = tdstateid.other[2]; 2130 } 2131 if (ret > 0) { 2132 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2133 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2134 np = VTONFS(fdvp); 2135 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2136 np->n_fhp->nfh_len, 0); 2137 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2138 *tl = txdr_unsigned(NFSV4OP_SAVEFH); 2139 } 2140 } else { 2141 ret = 0; 2142 } 2143 if (ret == 0) 2144 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp); 2145 if (nd->nd_flag & ND_NFSV4) { 2146 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2147 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2148 NFSWCCATTR_ATTRBIT(&attrbits); 2149 (void) nfsrv_putattrbit(nd, &attrbits); 2150 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2151 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2152 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh, 2153 VTONFS(tdvp)->n_fhp->nfh_len, 0); 2154 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2155 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2156 (void) nfsrv_putattrbit(nd, &attrbits); 2157 nd->nd_flag |= ND_V4WCCATTR; 2158 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2159 *tl = txdr_unsigned(NFSV4OP_RENAME); 2160 } 2161 (void) nfsm_strtom(nd, fnameptr, fnamelen); 2162 if (!(nd->nd_flag & ND_NFSV4)) 2163 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh, 2164 VTONFS(tdvp)->n_fhp->nfh_len, 0); 2165 (void) nfsm_strtom(nd, tnameptr, tnamelen); 2166 error = nfscl_request(nd, fdvp, p, cred, fstuff); 2167 if (error) 2168 return (error); 2169 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 2170 /* For NFSv4, parse out any Delereturn replies. */ 2171 if (ret > 0 && nd->nd_repstat != 0 && 2172 (nd->nd_flag & ND_NOMOREDATA)) { 2173 /* 2174 * If the Delegreturn failed, try again without 2175 * it. The server will Recall, as required. 2176 */ 2177 mbuf_freem(nd->nd_mrep); 2178 goto tryagain; 2179 } 2180 for (i = 0; i < (ret * 2); i++) { 2181 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 2182 ND_NFSV4) { 2183 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2184 if (*(tl + 1)) { 2185 if (i == 0 && ret > 1) { 2186 /* 2187 * If the Delegreturn failed, try again 2188 * without it. The server will Recall, as 2189 * required. 2190 * If ret > 1, the first iteration of this 2191 * loop is the second DelegReturn result. 2192 */ 2193 mbuf_freem(nd->nd_mrep); 2194 goto tryagain; 2195 } else { 2196 nd->nd_flag |= ND_NOMOREDATA; 2197 } 2198 } 2199 } 2200 } 2201 /* Now, the first wcc attribute reply. */ 2202 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 2203 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2204 if (*(tl + 1)) 2205 nd->nd_flag |= ND_NOMOREDATA; 2206 } 2207 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL, 2208 fstuff); 2209 /* and the second wcc attribute reply. */ 2210 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && 2211 !error) { 2212 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2213 if (*(tl + 1)) 2214 nd->nd_flag |= ND_NOMOREDATA; 2215 } 2216 if (!error) 2217 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp, 2218 NULL, tstuff); 2219 } 2220 if (nd->nd_repstat && !error) 2221 error = nd->nd_repstat; 2222 nfsmout: 2223 mbuf_freem(nd->nd_mrep); 2224 return (error); 2225 } 2226 2227 /* 2228 * nfs hard link create rpc 2229 */ 2230 APPLESTATIC int 2231 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen, 2232 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2233 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff) 2234 { 2235 u_int32_t *tl; 2236 struct nfsrv_descript nfsd, *nd = &nfsd; 2237 nfsattrbit_t attrbits; 2238 int error = 0; 2239 2240 *attrflagp = 0; 2241 *dattrflagp = 0; 2242 if (namelen > NFS_MAXNAMLEN) 2243 return (ENAMETOOLONG); 2244 NFSCL_REQSTART(nd, NFSPROC_LINK, vp); 2245 if (nd->nd_flag & ND_NFSV4) { 2246 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2247 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2248 } 2249 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh, 2250 VTONFS(dvp)->n_fhp->nfh_len, 0); 2251 if (nd->nd_flag & ND_NFSV4) { 2252 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2253 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2254 NFSWCCATTR_ATTRBIT(&attrbits); 2255 (void) nfsrv_putattrbit(nd, &attrbits); 2256 nd->nd_flag |= ND_V4WCCATTR; 2257 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2258 *tl = txdr_unsigned(NFSV4OP_LINK); 2259 } 2260 (void) nfsm_strtom(nd, name, namelen); 2261 error = nfscl_request(nd, vp, p, cred, dstuff); 2262 if (error) 2263 return (error); 2264 if (nd->nd_flag & ND_NFSV3) { 2265 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff); 2266 if (!error) 2267 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, 2268 NULL, dstuff); 2269 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 2270 /* 2271 * First, parse out the PutFH and Getattr result. 2272 */ 2273 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2274 if (!(*(tl + 1))) 2275 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2276 if (*(tl + 1)) 2277 nd->nd_flag |= ND_NOMOREDATA; 2278 /* 2279 * Get the pre-op attributes. 2280 */ 2281 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2282 } 2283 if (nd->nd_repstat && !error) 2284 error = nd->nd_repstat; 2285 nfsmout: 2286 mbuf_freem(nd->nd_mrep); 2287 return (error); 2288 } 2289 2290 /* 2291 * nfs symbolic link create rpc 2292 */ 2293 APPLESTATIC int 2294 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, char *target, 2295 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2296 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2297 int *dattrflagp, void *dstuff) 2298 { 2299 u_int32_t *tl; 2300 struct nfsrv_descript nfsd, *nd = &nfsd; 2301 struct nfsmount *nmp; 2302 int slen, error = 0; 2303 2304 *nfhpp = NULL; 2305 *attrflagp = 0; 2306 *dattrflagp = 0; 2307 nmp = VFSTONFS(vnode_mount(dvp)); 2308 slen = strlen(target); 2309 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN) 2310 return (ENAMETOOLONG); 2311 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp); 2312 if (nd->nd_flag & ND_NFSV4) { 2313 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2314 *tl = txdr_unsigned(NFLNK); 2315 (void) nfsm_strtom(nd, target, slen); 2316 } 2317 (void) nfsm_strtom(nd, name, namelen); 2318 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 2319 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2320 if (!(nd->nd_flag & ND_NFSV4)) 2321 (void) nfsm_strtom(nd, target, slen); 2322 if (nd->nd_flag & ND_NFSV2) 2323 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0); 2324 error = nfscl_request(nd, dvp, p, cred, dstuff); 2325 if (error) 2326 return (error); 2327 if (nd->nd_flag & ND_NFSV4) 2328 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2329 if ((nd->nd_flag & ND_NFSV3) && !error) { 2330 if (!nd->nd_repstat) 2331 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2332 if (!error) 2333 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, 2334 NULL, dstuff); 2335 } 2336 if (nd->nd_repstat && !error) 2337 error = nd->nd_repstat; 2338 mbuf_freem(nd->nd_mrep); 2339 /* 2340 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 2341 */ 2342 if (error == EEXIST) 2343 error = 0; 2344 return (error); 2345 } 2346 2347 /* 2348 * nfs make dir rpc 2349 */ 2350 APPLESTATIC int 2351 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2352 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2353 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2354 int *dattrflagp, void *dstuff) 2355 { 2356 u_int32_t *tl; 2357 struct nfsrv_descript nfsd, *nd = &nfsd; 2358 nfsattrbit_t attrbits; 2359 int error = 0; 2360 2361 *nfhpp = NULL; 2362 *attrflagp = 0; 2363 *dattrflagp = 0; 2364 if (namelen > NFS_MAXNAMLEN) 2365 return (ENAMETOOLONG); 2366 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp); 2367 if (nd->nd_flag & ND_NFSV4) { 2368 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2369 *tl = txdr_unsigned(NFDIR); 2370 } 2371 (void) nfsm_strtom(nd, name, namelen); 2372 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0); 2373 if (nd->nd_flag & ND_NFSV4) { 2374 NFSGETATTR_ATTRBIT(&attrbits); 2375 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2376 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2377 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2378 (void) nfsrv_putattrbit(nd, &attrbits); 2379 } 2380 error = nfscl_request(nd, dvp, p, cred, dstuff); 2381 if (error) 2382 return (error); 2383 if (nd->nd_flag & ND_NFSV4) 2384 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2385 if (!nd->nd_repstat && !error) { 2386 if (nd->nd_flag & ND_NFSV4) { 2387 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2388 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2389 } 2390 if (!error) 2391 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2392 } 2393 if ((nd->nd_flag & ND_NFSV3) && !error) 2394 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2395 if (nd->nd_repstat && !error) 2396 error = nd->nd_repstat; 2397 nfsmout: 2398 mbuf_freem(nd->nd_mrep); 2399 /* 2400 * Kludge: Map EEXIST => 0 assuming that you have a reply to a retry. 2401 */ 2402 if (error == EEXIST) 2403 error = 0; 2404 return (error); 2405 } 2406 2407 /* 2408 * nfs remove directory call 2409 */ 2410 APPLESTATIC int 2411 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred, 2412 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff) 2413 { 2414 struct nfsrv_descript nfsd, *nd = &nfsd; 2415 int error = 0; 2416 2417 *dattrflagp = 0; 2418 if (namelen > NFS_MAXNAMLEN) 2419 return (ENAMETOOLONG); 2420 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp); 2421 (void) nfsm_strtom(nd, name, namelen); 2422 error = nfscl_request(nd, dvp, p, cred, dstuff); 2423 if (error) 2424 return (error); 2425 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 2426 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2427 if (nd->nd_repstat && !error) 2428 error = nd->nd_repstat; 2429 mbuf_freem(nd->nd_mrep); 2430 /* 2431 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 2432 */ 2433 if (error == ENOENT) 2434 error = 0; 2435 return (error); 2436 } 2437 2438 /* 2439 * Readdir rpc. 2440 * Always returns with either uio_resid unchanged, if you are at the 2441 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks 2442 * filled in. 2443 * I felt this would allow caching of directory blocks more easily 2444 * than returning a pertially filled block. 2445 * Directory offset cookies: 2446 * Oh my, what to do with them... 2447 * I can think of three ways to deal with them: 2448 * 1 - have the layer above these RPCs maintain a map between logical 2449 * directory byte offsets and the NFS directory offset cookies 2450 * 2 - pass the opaque directory offset cookies up into userland 2451 * and let the libc functions deal with them, via the system call 2452 * 3 - return them to userland in the "struct dirent", so future versions 2453 * of libc can use them and do whatever is necessary to amke things work 2454 * above these rpc calls, in the meantime 2455 * For now, I do #3 by "hiding" the directory offset cookies after the 2456 * d_name field in struct dirent. This is space inside d_reclen that 2457 * will be ignored by anything that doesn't know about them. 2458 * The directory offset cookies are filled in as the last 8 bytes of 2459 * each directory entry, after d_name. Someday, the userland libc 2460 * functions may be able to use these. In the meantime, it satisfies 2461 * OpenBSD's requirements for cookies being returned. 2462 * If expects the directory offset cookie for the read to be in uio_offset 2463 * and returns the one for the next entry after this directory block in 2464 * there, as well. 2465 */ 2466 APPLESTATIC int 2467 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep, 2468 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 2469 int *eofp, void *stuff) 2470 { 2471 int len, left; 2472 struct dirent *dp = NULL; 2473 u_int32_t *tl; 2474 nfsquad_t cookie, ncookie; 2475 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 2476 struct nfsnode *dnp = VTONFS(vp); 2477 struct nfsvattr nfsva; 2478 struct nfsrv_descript nfsd, *nd = &nfsd; 2479 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2480 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0; 2481 long dotfileid, dotdotfileid = 0; 2482 u_int32_t fakefileno = 0xffffffff, rderr; 2483 char *cp; 2484 nfsattrbit_t attrbits, dattrbits; 2485 u_int32_t *tl2 = NULL; 2486 size_t tresid; 2487 2488 #ifdef DIAGNOSTIC 2489 if (uiop->uio_iovcnt != 1 || (uio_uio_resid(uiop) & (DIRBLKSIZ - 1))) 2490 panic("nfs readdirrpc bad uio"); 2491 #endif 2492 2493 /* 2494 * There is no point in reading a lot more than uio_resid, however 2495 * adding one additional DIRBLKSIZ makes sense. Since uio_resid 2496 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this 2497 * will never make readsize > nm_readdirsize. 2498 */ 2499 readsize = nmp->nm_readdirsize; 2500 if (readsize > uio_uio_resid(uiop)) 2501 readsize = uio_uio_resid(uiop) + DIRBLKSIZ; 2502 2503 *attrflagp = 0; 2504 if (eofp) 2505 *eofp = 0; 2506 tresid = uio_uio_resid(uiop); 2507 cookie.lval[0] = cookiep->nfsuquad[0]; 2508 cookie.lval[1] = cookiep->nfsuquad[1]; 2509 nd->nd_mrep = NULL; 2510 2511 /* 2512 * For NFSv4, first create the "." and ".." entries. 2513 */ 2514 if (NFSHASNFSV4(nmp)) { 2515 reqsize = 6 * NFSX_UNSIGNED; 2516 NFSGETATTR_ATTRBIT(&dattrbits); 2517 NFSZERO_ATTRBIT(&attrbits); 2518 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID); 2519 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE); 2520 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr, 2521 NFSATTRBIT_MOUNTEDONFILEID)) { 2522 NFSSETBIT_ATTRBIT(&attrbits, 2523 NFSATTRBIT_MOUNTEDONFILEID); 2524 gotmnton = 1; 2525 } else { 2526 /* 2527 * Must fake it. Use the fileno, except when the 2528 * fsid is != to that of the directory. For that 2529 * case, generate a fake fileno that is not the same. 2530 */ 2531 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID); 2532 gotmnton = 0; 2533 } 2534 2535 /* 2536 * Joy, oh joy. For V4 we get to hand craft '.' and '..'. 2537 */ 2538 if (uiop->uio_offset == 0) { 2539 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000 2540 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred); 2541 #else 2542 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p); 2543 #endif 2544 if (error) 2545 return (error); 2546 dotfileid = nfsva.na_fileid; 2547 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp); 2548 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2549 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2550 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2551 (void) nfsrv_putattrbit(nd, &attrbits); 2552 error = nfscl_request(nd, vp, p, cred, stuff); 2553 if (error) 2554 return (error); 2555 if (nd->nd_repstat == 0) { 2556 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 2557 len = fxdr_unsigned(int, *(tl + 2)); 2558 if (len > 0 && len <= NFSX_V4FHMAX) 2559 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 2560 else 2561 error = EPERM; 2562 if (!error) { 2563 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 2564 nfsva.na_mntonfileno = 0xffffffff; 2565 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 2566 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 2567 NULL, NULL, NULL, p, cred); 2568 if (error) { 2569 dotdotfileid = dotfileid; 2570 } else if (gotmnton) { 2571 if (nfsva.na_mntonfileno != 0xffffffff) 2572 dotdotfileid = nfsva.na_mntonfileno; 2573 else 2574 dotdotfileid = nfsva.na_fileid; 2575 } else if (nfsva.na_filesid[0] == 2576 dnp->n_vattr.na_filesid[0] && 2577 nfsva.na_filesid[1] == 2578 dnp->n_vattr.na_filesid[1]) { 2579 dotdotfileid = nfsva.na_fileid; 2580 } else { 2581 do { 2582 fakefileno--; 2583 } while (fakefileno == 2584 nfsva.na_fileid); 2585 dotdotfileid = fakefileno; 2586 } 2587 } 2588 } else if (nd->nd_repstat == NFSERR_NOENT) { 2589 /* 2590 * Lookupp returns NFSERR_NOENT when we are 2591 * at the root, so just use the current dir. 2592 */ 2593 nd->nd_repstat = 0; 2594 dotdotfileid = dotfileid; 2595 } else { 2596 error = nd->nd_repstat; 2597 } 2598 mbuf_freem(nd->nd_mrep); 2599 if (error) 2600 return (error); 2601 nd->nd_mrep = NULL; 2602 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop)); 2603 dp->d_type = DT_DIR; 2604 dp->d_fileno = dotfileid; 2605 dp->d_namlen = 1; 2606 dp->d_name[0] = '.'; 2607 dp->d_name[1] = '\0'; 2608 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER; 2609 /* 2610 * Just make these offset cookie 0. 2611 */ 2612 tl = (u_int32_t *)&dp->d_name[4]; 2613 *tl++ = 0; 2614 *tl = 0; 2615 blksiz += dp->d_reclen; 2616 uio_uio_resid_add(uiop, -(dp->d_reclen)); 2617 uiop->uio_offset += dp->d_reclen; 2618 uio_iov_base_add(uiop, dp->d_reclen); 2619 uio_iov_len_add(uiop, -(dp->d_reclen)); 2620 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop)); 2621 dp->d_type = DT_DIR; 2622 dp->d_fileno = dotdotfileid; 2623 dp->d_namlen = 2; 2624 dp->d_name[0] = '.'; 2625 dp->d_name[1] = '.'; 2626 dp->d_name[2] = '\0'; 2627 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER; 2628 /* 2629 * Just make these offset cookie 0. 2630 */ 2631 tl = (u_int32_t *)&dp->d_name[4]; 2632 *tl++ = 0; 2633 *tl = 0; 2634 blksiz += dp->d_reclen; 2635 uio_uio_resid_add(uiop, -(dp->d_reclen)); 2636 uiop->uio_offset += dp->d_reclen; 2637 uio_iov_base_add(uiop, dp->d_reclen); 2638 uio_iov_len_add(uiop, -(dp->d_reclen)); 2639 } 2640 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR); 2641 } else { 2642 reqsize = 5 * NFSX_UNSIGNED; 2643 } 2644 2645 2646 /* 2647 * Loop around doing readdir rpc's of size readsize. 2648 * The stopping criteria is EOF or buffer full. 2649 */ 2650 while (more_dirs && bigenough) { 2651 *attrflagp = 0; 2652 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp); 2653 if (nd->nd_flag & ND_NFSV2) { 2654 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2655 *tl++ = cookie.lval[1]; 2656 *tl = txdr_unsigned(readsize); 2657 } else { 2658 NFSM_BUILD(tl, u_int32_t *, reqsize); 2659 *tl++ = cookie.lval[0]; 2660 *tl++ = cookie.lval[1]; 2661 if (cookie.qval == 0) { 2662 *tl++ = 0; 2663 *tl++ = 0; 2664 } else { 2665 NFSLOCKNODE(dnp); 2666 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 2667 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 2668 NFSUNLOCKNODE(dnp); 2669 } 2670 if (nd->nd_flag & ND_NFSV4) { 2671 *tl++ = txdr_unsigned(readsize); 2672 *tl = txdr_unsigned(readsize); 2673 (void) nfsrv_putattrbit(nd, &attrbits); 2674 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2675 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2676 (void) nfsrv_putattrbit(nd, &dattrbits); 2677 } else { 2678 *tl = txdr_unsigned(readsize); 2679 } 2680 } 2681 error = nfscl_request(nd, vp, p, cred, stuff); 2682 if (error) 2683 return (error); 2684 if (!(nd->nd_flag & ND_NFSV2)) { 2685 if (nd->nd_flag & ND_NFSV3) 2686 error = nfscl_postop_attr(nd, nap, attrflagp, 2687 stuff); 2688 if (!nd->nd_repstat && !error) { 2689 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2690 NFSLOCKNODE(dnp); 2691 dnp->n_cookieverf.nfsuquad[0] = *tl++; 2692 dnp->n_cookieverf.nfsuquad[1] = *tl; 2693 NFSUNLOCKNODE(dnp); 2694 } 2695 } 2696 if (nd->nd_repstat || error) { 2697 if (!error) 2698 error = nd->nd_repstat; 2699 goto nfsmout; 2700 } 2701 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2702 more_dirs = fxdr_unsigned(int, *tl); 2703 if (!more_dirs) 2704 tryformoredirs = 0; 2705 2706 /* loop thru the dir entries, doctoring them to 4bsd form */ 2707 while (more_dirs && bigenough) { 2708 if (nd->nd_flag & ND_NFSV4) { 2709 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 2710 ncookie.lval[0] = *tl++; 2711 ncookie.lval[1] = *tl++; 2712 len = fxdr_unsigned(int, *tl); 2713 } else if (nd->nd_flag & ND_NFSV3) { 2714 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 2715 nfsva.na_fileid = 2716 fxdr_unsigned(long, *++tl); 2717 len = fxdr_unsigned(int, *++tl); 2718 } else { 2719 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 2720 nfsva.na_fileid = 2721 fxdr_unsigned(long, *tl++); 2722 len = fxdr_unsigned(int, *tl); 2723 } 2724 if (len <= 0 || len > NFS_MAXNAMLEN) { 2725 error = EBADRPC; 2726 goto nfsmout; 2727 } 2728 tlen = NFSM_RNDUP(len); 2729 if (tlen == len) 2730 tlen += 4; /* To ensure null termination */ 2731 left = DIRBLKSIZ - blksiz; 2732 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > left) { 2733 dp->d_reclen += left; 2734 uio_iov_base_add(uiop, left); 2735 uio_iov_len_add(uiop, -(left)); 2736 uio_uio_resid_add(uiop, -(left)); 2737 uiop->uio_offset += left; 2738 blksiz = 0; 2739 } 2740 if ((int)(tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop)) 2741 bigenough = 0; 2742 if (bigenough) { 2743 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop)); 2744 dp->d_namlen = len; 2745 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER; 2746 dp->d_type = DT_UNKNOWN; 2747 blksiz += dp->d_reclen; 2748 if (blksiz == DIRBLKSIZ) 2749 blksiz = 0; 2750 uio_uio_resid_add(uiop, -(DIRHDSIZ)); 2751 uiop->uio_offset += DIRHDSIZ; 2752 uio_iov_base_add(uiop, DIRHDSIZ); 2753 uio_iov_len_add(uiop, -(DIRHDSIZ)); 2754 error = nfsm_mbufuio(nd, uiop, len); 2755 if (error) 2756 goto nfsmout; 2757 cp = CAST_DOWN(caddr_t, uio_iov_base(uiop)); 2758 tlen -= len; 2759 *cp = '\0'; /* null terminate */ 2760 cp += tlen; /* points to cookie storage */ 2761 tl2 = (u_int32_t *)cp; 2762 uio_iov_base_add(uiop, (tlen + NFSX_HYPER)); 2763 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER)); 2764 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER)); 2765 uiop->uio_offset += (tlen + NFSX_HYPER); 2766 } else { 2767 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 2768 if (error) 2769 goto nfsmout; 2770 } 2771 if (nd->nd_flag & ND_NFSV4) { 2772 rderr = 0; 2773 nfsva.na_mntonfileno = 0xffffffff; 2774 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 2775 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 2776 NULL, NULL, &rderr, p, cred); 2777 if (error) 2778 goto nfsmout; 2779 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2780 } else if (nd->nd_flag & ND_NFSV3) { 2781 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 2782 ncookie.lval[0] = *tl++; 2783 ncookie.lval[1] = *tl++; 2784 } else { 2785 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 2786 ncookie.lval[0] = 0; 2787 ncookie.lval[1] = *tl++; 2788 } 2789 if (bigenough) { 2790 if (nd->nd_flag & ND_NFSV4) { 2791 if (rderr) { 2792 dp->d_fileno = 0; 2793 } else { 2794 if (gotmnton) { 2795 if (nfsva.na_mntonfileno != 0xffffffff) 2796 dp->d_fileno = nfsva.na_mntonfileno; 2797 else 2798 dp->d_fileno = nfsva.na_fileid; 2799 } else if (nfsva.na_filesid[0] == 2800 dnp->n_vattr.na_filesid[0] && 2801 nfsva.na_filesid[1] == 2802 dnp->n_vattr.na_filesid[1]) { 2803 dp->d_fileno = nfsva.na_fileid; 2804 } else { 2805 do { 2806 fakefileno--; 2807 } while (fakefileno == 2808 nfsva.na_fileid); 2809 dp->d_fileno = fakefileno; 2810 } 2811 dp->d_type = vtonfs_dtype(nfsva.na_type); 2812 } 2813 } else { 2814 dp->d_fileno = nfsva.na_fileid; 2815 } 2816 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 2817 ncookie.lval[0]; 2818 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 2819 ncookie.lval[1]; 2820 } 2821 more_dirs = fxdr_unsigned(int, *tl); 2822 } 2823 /* 2824 * If at end of rpc data, get the eof boolean 2825 */ 2826 if (!more_dirs) { 2827 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2828 eof = fxdr_unsigned(int, *tl); 2829 if (tryformoredirs) 2830 more_dirs = !eof; 2831 if (nd->nd_flag & ND_NFSV4) { 2832 error = nfscl_postop_attr(nd, nap, attrflagp, 2833 stuff); 2834 if (error) 2835 goto nfsmout; 2836 } 2837 } 2838 mbuf_freem(nd->nd_mrep); 2839 nd->nd_mrep = NULL; 2840 } 2841 /* 2842 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 2843 * by increasing d_reclen for the last record. 2844 */ 2845 if (blksiz > 0) { 2846 left = DIRBLKSIZ - blksiz; 2847 dp->d_reclen += left; 2848 uio_iov_base_add(uiop, left); 2849 uio_iov_len_add(uiop, -(left)); 2850 uio_uio_resid_add(uiop, -(left)); 2851 uiop->uio_offset += left; 2852 } 2853 2854 /* 2855 * If returning no data, assume end of file. 2856 * If not bigenough, return not end of file, since you aren't 2857 * returning all the data 2858 * Otherwise, return the eof flag from the server. 2859 */ 2860 if (eofp) { 2861 if (tresid == ((size_t)(uio_uio_resid(uiop)))) 2862 *eofp = 1; 2863 else if (!bigenough) 2864 *eofp = 0; 2865 else 2866 *eofp = eof; 2867 } 2868 2869 /* 2870 * Add extra empty records to any remaining DIRBLKSIZ chunks. 2871 */ 2872 while (uio_uio_resid(uiop) > 0 && ((size_t)(uio_uio_resid(uiop))) != tresid) { 2873 dp = (struct dirent *) CAST_DOWN(caddr_t, uio_iov_base(uiop)); 2874 dp->d_type = DT_UNKNOWN; 2875 dp->d_fileno = 0; 2876 dp->d_namlen = 0; 2877 dp->d_name[0] = '\0'; 2878 tl = (u_int32_t *)&dp->d_name[4]; 2879 *tl++ = cookie.lval[0]; 2880 *tl = cookie.lval[1]; 2881 dp->d_reclen = DIRBLKSIZ; 2882 uio_iov_base_add(uiop, DIRBLKSIZ); 2883 uio_iov_len_add(uiop, -(DIRBLKSIZ)); 2884 uio_uio_resid_add(uiop, -(DIRBLKSIZ)); 2885 uiop->uio_offset += DIRBLKSIZ; 2886 } 2887 2888 nfsmout: 2889 if (nd->nd_mrep != NULL) 2890 mbuf_freem(nd->nd_mrep); 2891 return (error); 2892 } 2893 2894 #ifndef APPLE 2895 /* 2896 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir(). 2897 * (Also used for NFS V4 when mount flag set.) 2898 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.) 2899 */ 2900 APPLESTATIC int 2901 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep, 2902 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 2903 int *eofp, void *stuff) 2904 { 2905 int len, left; 2906 struct dirent *dp = NULL; 2907 u_int32_t *tl; 2908 vnode_t newvp = NULLVP; 2909 struct nfsrv_descript nfsd, *nd = &nfsd; 2910 struct nameidata nami, *ndp = &nami; 2911 struct componentname *cnp = &ndp->ni_cnd; 2912 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 2913 struct nfsnode *dnp = VTONFS(vp), *np; 2914 struct nfsvattr nfsva; 2915 struct nfsfh *nfhp; 2916 nfsquad_t cookie, ncookie; 2917 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2918 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0; 2919 int unlocknewvp = 0; 2920 long dotfileid, dotdotfileid = 0, fileno = 0; 2921 char *cp; 2922 nfsattrbit_t attrbits, dattrbits; 2923 size_t tresid; 2924 u_int32_t *tl2 = NULL, fakefileno = 0xffffffff, rderr; 2925 2926 #ifdef DIAGNOSTIC 2927 if (uiop->uio_iovcnt != 1 || (uio_uio_resid(uiop) & (DIRBLKSIZ - 1))) 2928 panic("nfs readdirplusrpc bad uio"); 2929 #endif 2930 *attrflagp = 0; 2931 if (eofp != NULL) 2932 *eofp = 0; 2933 ndp->ni_dvp = vp; 2934 nd->nd_mrep = NULL; 2935 cookie.lval[0] = cookiep->nfsuquad[0]; 2936 cookie.lval[1] = cookiep->nfsuquad[1]; 2937 tresid = uio_uio_resid(uiop); 2938 2939 /* 2940 * For NFSv4, first create the "." and ".." entries. 2941 */ 2942 if (NFSHASNFSV4(nmp)) { 2943 NFSGETATTR_ATTRBIT(&dattrbits); 2944 NFSZERO_ATTRBIT(&attrbits); 2945 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID); 2946 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr, 2947 NFSATTRBIT_MOUNTEDONFILEID)) { 2948 NFSSETBIT_ATTRBIT(&attrbits, 2949 NFSATTRBIT_MOUNTEDONFILEID); 2950 gotmnton = 1; 2951 } else { 2952 /* 2953 * Must fake it. Use the fileno, except when the 2954 * fsid is != to that of the directory. For that 2955 * case, generate a fake fileno that is not the same. 2956 */ 2957 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID); 2958 gotmnton = 0; 2959 } 2960 2961 /* 2962 * Joy, oh joy. For V4 we get to hand craft '.' and '..'. 2963 */ 2964 if (uiop->uio_offset == 0) { 2965 #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000 2966 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred); 2967 #else 2968 error = VOP_GETATTR(vp, &nfsva.na_vattr, cred, p); 2969 #endif 2970 if (error) 2971 return (error); 2972 dotfileid = nfsva.na_fileid; 2973 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp); 2974 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2975 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2976 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2977 (void) nfsrv_putattrbit(nd, &attrbits); 2978 error = nfscl_request(nd, vp, p, cred, stuff); 2979 if (error) 2980 return (error); 2981 if (nd->nd_repstat == 0) { 2982 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 2983 len = fxdr_unsigned(int, *(tl + 2)); 2984 if (len > 0 && len <= NFSX_V4FHMAX) 2985 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 2986 else 2987 error = EPERM; 2988 if (!error) { 2989 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 2990 nfsva.na_mntonfileno = 0xffffffff; 2991 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 2992 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 2993 NULL, NULL, NULL, p, cred); 2994 if (error) { 2995 dotdotfileid = dotfileid; 2996 } else if (gotmnton) { 2997 if (nfsva.na_mntonfileno != 0xffffffff) 2998 dotdotfileid = nfsva.na_mntonfileno; 2999 else 3000 dotdotfileid = nfsva.na_fileid; 3001 } else if (nfsva.na_filesid[0] == 3002 dnp->n_vattr.na_filesid[0] && 3003 nfsva.na_filesid[1] == 3004 dnp->n_vattr.na_filesid[1]) { 3005 dotdotfileid = nfsva.na_fileid; 3006 } else { 3007 do { 3008 fakefileno--; 3009 } while (fakefileno == 3010 nfsva.na_fileid); 3011 dotdotfileid = fakefileno; 3012 } 3013 } 3014 } else if (nd->nd_repstat == NFSERR_NOENT) { 3015 /* 3016 * Lookupp returns NFSERR_NOENT when we are 3017 * at the root, so just use the current dir. 3018 */ 3019 nd->nd_repstat = 0; 3020 dotdotfileid = dotfileid; 3021 } else { 3022 error = nd->nd_repstat; 3023 } 3024 mbuf_freem(nd->nd_mrep); 3025 if (error) 3026 return (error); 3027 nd->nd_mrep = NULL; 3028 dp = (struct dirent *)uio_iov_base(uiop); 3029 dp->d_type = DT_DIR; 3030 dp->d_fileno = dotfileid; 3031 dp->d_namlen = 1; 3032 dp->d_name[0] = '.'; 3033 dp->d_name[1] = '\0'; 3034 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER; 3035 /* 3036 * Just make these offset cookie 0. 3037 */ 3038 tl = (u_int32_t *)&dp->d_name[4]; 3039 *tl++ = 0; 3040 *tl = 0; 3041 blksiz += dp->d_reclen; 3042 uio_uio_resid_add(uiop, -(dp->d_reclen)); 3043 uiop->uio_offset += dp->d_reclen; 3044 uio_iov_base_add(uiop, dp->d_reclen); 3045 uio_iov_len_add(uiop, -(dp->d_reclen)); 3046 dp = (struct dirent *)uio_iov_base(uiop); 3047 dp->d_type = DT_DIR; 3048 dp->d_fileno = dotdotfileid; 3049 dp->d_namlen = 2; 3050 dp->d_name[0] = '.'; 3051 dp->d_name[1] = '.'; 3052 dp->d_name[2] = '\0'; 3053 dp->d_reclen = DIRENT_SIZE(dp) + NFSX_HYPER; 3054 /* 3055 * Just make these offset cookie 0. 3056 */ 3057 tl = (u_int32_t *)&dp->d_name[4]; 3058 *tl++ = 0; 3059 *tl = 0; 3060 blksiz += dp->d_reclen; 3061 uio_uio_resid_add(uiop, -(dp->d_reclen)); 3062 uiop->uio_offset += dp->d_reclen; 3063 uio_iov_base_add(uiop, dp->d_reclen); 3064 uio_iov_len_add(uiop, -(dp->d_reclen)); 3065 } 3066 NFSREADDIRPLUS_ATTRBIT(&attrbits); 3067 if (gotmnton) 3068 NFSSETBIT_ATTRBIT(&attrbits, 3069 NFSATTRBIT_MOUNTEDONFILEID); 3070 } 3071 3072 /* 3073 * Loop around doing readdir rpc's of size nm_readdirsize. 3074 * The stopping criteria is EOF or buffer full. 3075 */ 3076 while (more_dirs && bigenough) { 3077 *attrflagp = 0; 3078 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp); 3079 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 3080 *tl++ = cookie.lval[0]; 3081 *tl++ = cookie.lval[1]; 3082 if (cookie.qval == 0) { 3083 *tl++ = 0; 3084 *tl++ = 0; 3085 } else { 3086 NFSLOCKNODE(dnp); 3087 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 3088 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 3089 NFSUNLOCKNODE(dnp); 3090 } 3091 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 3092 *tl = txdr_unsigned(nmp->nm_readdirsize); 3093 if (nd->nd_flag & ND_NFSV4) { 3094 (void) nfsrv_putattrbit(nd, &attrbits); 3095 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3096 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3097 (void) nfsrv_putattrbit(nd, &dattrbits); 3098 } 3099 error = nfscl_request(nd, vp, p, cred, stuff); 3100 if (error) 3101 return (error); 3102 if (nd->nd_flag & ND_NFSV3) 3103 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3104 if (nd->nd_repstat || error) { 3105 if (!error) 3106 error = nd->nd_repstat; 3107 goto nfsmout; 3108 } 3109 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3110 NFSLOCKNODE(dnp); 3111 dnp->n_cookieverf.nfsuquad[0] = *tl++; 3112 dnp->n_cookieverf.nfsuquad[1] = *tl++; 3113 NFSUNLOCKNODE(dnp); 3114 more_dirs = fxdr_unsigned(int, *tl); 3115 if (!more_dirs) 3116 tryformoredirs = 0; 3117 3118 /* loop thru the dir entries, doctoring them to 4bsd form */ 3119 while (more_dirs && bigenough) { 3120 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3121 if (nd->nd_flag & ND_NFSV4) { 3122 ncookie.lval[0] = *tl++; 3123 ncookie.lval[1] = *tl++; 3124 } else { 3125 fileno = fxdr_unsigned(long, *++tl); 3126 tl++; 3127 } 3128 len = fxdr_unsigned(int, *tl); 3129 if (len <= 0 || len > NFS_MAXNAMLEN) { 3130 error = EBADRPC; 3131 goto nfsmout; 3132 } 3133 tlen = NFSM_RNDUP(len); 3134 if (tlen == len) 3135 tlen += 4; /* To ensure null termination */ 3136 left = DIRBLKSIZ - blksiz; 3137 if ((tlen + DIRHDSIZ + NFSX_HYPER) > left) { 3138 dp->d_reclen += left; 3139 uio_iov_base_add(uiop, left); 3140 uio_iov_len_add(uiop, -(left)); 3141 uio_uio_resid_add(uiop, -(left)); 3142 uiop->uio_offset += left; 3143 blksiz = 0; 3144 } 3145 if ((tlen + DIRHDSIZ + NFSX_HYPER) > uio_uio_resid(uiop)) 3146 bigenough = 0; 3147 if (bigenough) { 3148 dp = (struct dirent *)uio_iov_base(uiop); 3149 dp->d_namlen = len; 3150 dp->d_reclen = tlen + DIRHDSIZ + NFSX_HYPER; 3151 dp->d_type = DT_UNKNOWN; 3152 blksiz += dp->d_reclen; 3153 if (blksiz == DIRBLKSIZ) 3154 blksiz = 0; 3155 uio_uio_resid_add(uiop, -(DIRHDSIZ)); 3156 uiop->uio_offset += DIRHDSIZ; 3157 uio_iov_base_add(uiop, DIRHDSIZ); 3158 uio_iov_len_add(uiop, -(DIRHDSIZ)); 3159 cnp->cn_nameptr = uio_iov_base(uiop); 3160 cnp->cn_namelen = len; 3161 NFSCNHASHZERO(cnp); 3162 error = nfsm_mbufuio(nd, uiop, len); 3163 if (error) 3164 goto nfsmout; 3165 cp = uio_iov_base(uiop); 3166 tlen -= len; 3167 *cp = '\0'; 3168 cp += tlen; /* points to cookie storage */ 3169 tl2 = (u_int32_t *)cp; 3170 uio_iov_base_add(uiop, (tlen + NFSX_HYPER)); 3171 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER)); 3172 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER)); 3173 uiop->uio_offset += (tlen + NFSX_HYPER); 3174 } else { 3175 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3176 if (error) 3177 goto nfsmout; 3178 } 3179 nfhp = NULL; 3180 if (nd->nd_flag & ND_NFSV3) { 3181 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3182 ncookie.lval[0] = *tl++; 3183 ncookie.lval[1] = *tl++; 3184 attrflag = fxdr_unsigned(int, *tl); 3185 if (attrflag) { 3186 error = nfsm_loadattr(nd, &nfsva); 3187 if (error) 3188 goto nfsmout; 3189 } 3190 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED); 3191 if (*tl) { 3192 error = nfsm_getfh(nd, &nfhp); 3193 if (error) 3194 goto nfsmout; 3195 } 3196 if (!attrflag && nfhp != NULL) { 3197 FREE((caddr_t)nfhp, M_NFSFH); 3198 nfhp = NULL; 3199 } 3200 } else { 3201 rderr = 0; 3202 nfsva.na_mntonfileno = 0xffffffff; 3203 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp, 3204 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3205 NULL, NULL, &rderr, p, cred); 3206 if (error) 3207 goto nfsmout; 3208 } 3209 3210 if (bigenough) { 3211 if (nd->nd_flag & ND_NFSV4) { 3212 if (rderr) { 3213 dp->d_fileno = 0; 3214 } else if (gotmnton) { 3215 if (nfsva.na_mntonfileno != 0xffffffff) 3216 dp->d_fileno = nfsva.na_mntonfileno; 3217 else 3218 dp->d_fileno = nfsva.na_fileid; 3219 } else if (nfsva.na_filesid[0] == 3220 dnp->n_vattr.na_filesid[0] && 3221 nfsva.na_filesid[1] == 3222 dnp->n_vattr.na_filesid[1]) { 3223 dp->d_fileno = nfsva.na_fileid; 3224 } else { 3225 do { 3226 fakefileno--; 3227 } while (fakefileno == 3228 nfsva.na_fileid); 3229 dp->d_fileno = fakefileno; 3230 } 3231 } else { 3232 dp->d_fileno = fileno; 3233 } 3234 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 3235 ncookie.lval[0]; 3236 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 3237 ncookie.lval[1]; 3238 3239 if (nfhp != NULL) { 3240 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len, 3241 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) { 3242 VREF(vp); 3243 newvp = vp; 3244 unlocknewvp = 0; 3245 FREE((caddr_t)nfhp, M_NFSFH); 3246 np = dnp; 3247 } else { 3248 error = nfscl_nget(vnode_mount(vp), vp, 3249 nfhp, cnp, p, &np, NULL); 3250 if (!error) { 3251 newvp = NFSTOV(np); 3252 unlocknewvp = 1; 3253 } 3254 } 3255 nfhp = NULL; 3256 if (newvp != NULLVP) { 3257 error = nfscl_loadattrcache(&newvp, 3258 &nfsva, NULL, NULL, 0, 0); 3259 if (error) { 3260 if (unlocknewvp) 3261 vput(newvp); 3262 else 3263 vrele(newvp); 3264 goto nfsmout; 3265 } 3266 dp->d_type = 3267 vtonfs_dtype(np->n_vattr.na_type); 3268 ndp->ni_vp = newvp; 3269 NFSCNHASH(cnp, HASHINIT); 3270 if (cnp->cn_namelen <= NCHNAMLEN) { 3271 np->n_ctime = 3272 np->n_vattr.na_ctime.tv_sec; 3273 cache_enter(ndp->ni_dvp,ndp->ni_vp,cnp); 3274 } 3275 if (unlocknewvp) 3276 vput(newvp); 3277 else 3278 vrele(newvp); 3279 newvp = NULLVP; 3280 } 3281 } 3282 } else if (nfhp != NULL) { 3283 FREE((caddr_t)nfhp, M_NFSFH); 3284 } 3285 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3286 more_dirs = fxdr_unsigned(int, *tl); 3287 } 3288 /* 3289 * If at end of rpc data, get the eof boolean 3290 */ 3291 if (!more_dirs) { 3292 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3293 eof = fxdr_unsigned(int, *tl); 3294 if (tryformoredirs) 3295 more_dirs = !eof; 3296 if (nd->nd_flag & ND_NFSV4) { 3297 error = nfscl_postop_attr(nd, nap, attrflagp, 3298 stuff); 3299 if (error) 3300 goto nfsmout; 3301 } 3302 } 3303 mbuf_freem(nd->nd_mrep); 3304 nd->nd_mrep = NULL; 3305 } 3306 /* 3307 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 3308 * by increasing d_reclen for the last record. 3309 */ 3310 if (blksiz > 0) { 3311 left = DIRBLKSIZ - blksiz; 3312 dp->d_reclen += left; 3313 uio_iov_base_add(uiop, left); 3314 uio_iov_len_add(uiop, -(left)); 3315 uio_uio_resid_add(uiop, -(left)); 3316 uiop->uio_offset += left; 3317 } 3318 3319 /* 3320 * If returning no data, assume end of file. 3321 * If not bigenough, return not end of file, since you aren't 3322 * returning all the data 3323 * Otherwise, return the eof flag from the server. 3324 */ 3325 if (eofp != NULL) { 3326 if (tresid == uio_uio_resid(uiop)) 3327 *eofp = 1; 3328 else if (!bigenough) 3329 *eofp = 0; 3330 else 3331 *eofp = eof; 3332 } 3333 3334 /* 3335 * Add extra empty records to any remaining DIRBLKSIZ chunks. 3336 */ 3337 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) { 3338 dp = (struct dirent *)uio_iov_base(uiop); 3339 dp->d_type = DT_UNKNOWN; 3340 dp->d_fileno = 0; 3341 dp->d_namlen = 0; 3342 dp->d_name[0] = '\0'; 3343 tl = (u_int32_t *)&dp->d_name[4]; 3344 *tl++ = cookie.lval[0]; 3345 *tl = cookie.lval[1]; 3346 dp->d_reclen = DIRBLKSIZ; 3347 uio_iov_base_add(uiop, DIRBLKSIZ); 3348 uio_iov_len_add(uiop, -(DIRBLKSIZ)); 3349 uio_uio_resid_add(uiop, -(DIRBLKSIZ)); 3350 uiop->uio_offset += DIRBLKSIZ; 3351 } 3352 3353 nfsmout: 3354 if (nd->nd_mrep != NULL) 3355 mbuf_freem(nd->nd_mrep); 3356 return (error); 3357 } 3358 #endif /* !APPLE */ 3359 3360 /* 3361 * Nfs commit rpc 3362 */ 3363 APPLESTATIC int 3364 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred, 3365 NFSPROC_T *p, u_char *verfp, struct nfsvattr *nap, int *attrflagp, 3366 void *stuff) 3367 { 3368 u_int32_t *tl; 3369 struct nfsrv_descript nfsd, *nd = &nfsd; 3370 nfsattrbit_t attrbits; 3371 int error; 3372 3373 *attrflagp = 0; 3374 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp); 3375 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3376 txdr_hyper(offset, tl); 3377 tl += 2; 3378 *tl = txdr_unsigned(cnt); 3379 if (nd->nd_flag & ND_NFSV4) { 3380 /* 3381 * And do a Getattr op. 3382 */ 3383 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3384 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3385 NFSGETATTR_ATTRBIT(&attrbits); 3386 (void) nfsrv_putattrbit(nd, &attrbits); 3387 } 3388 error = nfscl_request(nd, vp, p, cred, stuff); 3389 if (error) 3390 return (error); 3391 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff); 3392 if (!error && !nd->nd_repstat) { 3393 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 3394 NFSBCOPY((caddr_t)tl, verfp, NFSX_VERF); 3395 if (nd->nd_flag & ND_NFSV4) 3396 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3397 } 3398 nfsmout: 3399 if (!error && nd->nd_repstat) 3400 error = nd->nd_repstat; 3401 mbuf_freem(nd->nd_mrep); 3402 return (error); 3403 } 3404 3405 /* 3406 * NFS byte range lock rpc. 3407 * (Mostly just calls one of the three lower level RPC routines.) 3408 */ 3409 APPLESTATIC int 3410 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl, 3411 int reclaim, struct ucred *cred, NFSPROC_T *p) 3412 { 3413 struct nfscllockowner *lp; 3414 struct nfsclclient *clp; 3415 struct nfsfh *nfhp; 3416 struct nfsrv_descript nfsd, *nd = &nfsd; 3417 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3418 u_int64_t off, len; 3419 off_t start, end; 3420 u_int32_t clidrev = 0; 3421 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally; 3422 int callcnt, dorpc; 3423 3424 /* 3425 * Convert the flock structure into a start and end and do POSIX 3426 * bounds checking. 3427 */ 3428 switch (fl->l_whence) { 3429 case SEEK_SET: 3430 case SEEK_CUR: 3431 /* 3432 * Caller is responsible for adding any necessary offset 3433 * when SEEK_CUR is used. 3434 */ 3435 start = fl->l_start; 3436 off = fl->l_start; 3437 break; 3438 case SEEK_END: 3439 start = size + fl->l_start; 3440 off = size + fl->l_start; 3441 break; 3442 default: 3443 return (EINVAL); 3444 }; 3445 if (start < 0) 3446 return (EINVAL); 3447 if (fl->l_len != 0) { 3448 end = start + fl->l_len - 1; 3449 if (end < start) 3450 return (EINVAL); 3451 } 3452 3453 len = fl->l_len; 3454 if (len == 0) 3455 len = NFS64BITSSET; 3456 retrycnt = 0; 3457 do { 3458 nd->nd_repstat = 0; 3459 if (op == F_GETLK) { 3460 error = nfscl_getcl(vp, cred, p, &clp); 3461 if (error) 3462 return (error); 3463 error = nfscl_lockt(vp, clp, off, len, fl, p); 3464 if (!error) { 3465 clidrev = clp->nfsc_clientidrev; 3466 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred, 3467 p); 3468 } else if (error == -1) { 3469 error = 0; 3470 } 3471 nfscl_clientrelease(clp); 3472 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) { 3473 /* 3474 * We must loop around for all lockowner cases. 3475 */ 3476 callcnt = 0; 3477 error = nfscl_getcl(vp, cred, p, &clp); 3478 if (error) 3479 return (error); 3480 do { 3481 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt, 3482 clp, &lp, &dorpc); 3483 /* 3484 * If it returns a NULL lp, we're done. 3485 */ 3486 if (lp == NULL) { 3487 if (callcnt == 0) 3488 nfscl_clientrelease(clp); 3489 else 3490 nfscl_releasealllocks(clp, vp, p); 3491 return (error); 3492 } 3493 if (nmp->nm_clp != NULL) 3494 clidrev = nmp->nm_clp->nfsc_clientidrev; 3495 else 3496 clidrev = 0; 3497 /* 3498 * If the server doesn't support Posix lock semantics, 3499 * only allow locks on the entire file, since it won't 3500 * handle overlapping byte ranges. 3501 * There might still be a problem when a lock 3502 * upgrade/downgrade (read<->write) occurs, since the 3503 * server "might" expect an unlock first? 3504 */ 3505 if (dorpc && (lp->nfsl_open->nfso_posixlock || 3506 (off == 0 && len == NFS64BITSSET))) { 3507 /* 3508 * Since the lock records will go away, we must 3509 * wait for grace and delay here. 3510 */ 3511 do { 3512 error = nfsrpc_locku(nd, nmp, lp, off, len, 3513 NFSV4LOCKT_READ, cred, p, 0); 3514 if ((nd->nd_repstat == NFSERR_GRACE || 3515 nd->nd_repstat == NFSERR_DELAY) && 3516 error == 0) 3517 (void) nfs_catnap(PZERO, "nfs_advlock"); 3518 } while ((nd->nd_repstat == NFSERR_GRACE || 3519 nd->nd_repstat == NFSERR_DELAY) && error == 0); 3520 } 3521 callcnt++; 3522 } while (error == 0 && nd->nd_repstat == 0); 3523 nfscl_releasealllocks(clp, vp, p); 3524 } else if (op == F_SETLK) { 3525 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p, 3526 NULL, 0, NULL, NULL, &lp, &newone, &donelocally); 3527 if (error || donelocally) { 3528 return (error); 3529 } 3530 if (nmp->nm_clp != NULL) 3531 clidrev = nmp->nm_clp->nfsc_clientidrev; 3532 else 3533 clidrev = 0; 3534 nfhp = VTONFS(vp)->n_fhp; 3535 if (!lp->nfsl_open->nfso_posixlock && 3536 (off != 0 || len != NFS64BITSSET)) { 3537 error = EINVAL; 3538 } else { 3539 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh, 3540 nfhp->nfh_len, lp, newone, reclaim, off, 3541 len, fl->l_type, cred, p, 0); 3542 } 3543 if (!error) 3544 error = nd->nd_repstat; 3545 nfscl_lockrelease(lp, error, newone); 3546 } else { 3547 error = EINVAL; 3548 } 3549 if (!error) 3550 error = nd->nd_repstat; 3551 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 3552 error == NFSERR_STALEDONTRECOVER || 3553 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY) { 3554 (void) nfs_catnap(PZERO, "nfs_advlock"); 3555 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) 3556 && clidrev != 0) { 3557 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 3558 retrycnt++; 3559 } 3560 } while (error == NFSERR_GRACE || 3561 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 3562 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID || 3563 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 3564 expireret == 0 && clidrev != 0 && retrycnt < 4)); 3565 if (error && retrycnt >= 4) 3566 error = EIO; 3567 return (error); 3568 } 3569 3570 /* 3571 * The lower level routine for the LockT case. 3572 */ 3573 APPLESTATIC int 3574 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp, 3575 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl, 3576 struct ucred *cred, NFSPROC_T *p) 3577 { 3578 u_int32_t *tl; 3579 int error, type, size; 3580 u_int8_t own[NFSV4CL_LOCKNAMELEN]; 3581 3582 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp); 3583 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 3584 if (fl->l_type == F_RDLCK) 3585 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 3586 else 3587 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 3588 txdr_hyper(off, tl); 3589 tl += 2; 3590 txdr_hyper(len, tl); 3591 tl += 2; 3592 *tl++ = clp->nfsc_clientid.lval[0]; 3593 *tl = clp->nfsc_clientid.lval[1]; 3594 nfscl_filllockowner(p, own); 3595 (void) nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN); 3596 error = nfscl_request(nd, vp, p, cred, NULL); 3597 if (error) 3598 return (error); 3599 if (nd->nd_repstat == 0) { 3600 fl->l_type = F_UNLCK; 3601 } else if (nd->nd_repstat == NFSERR_DENIED) { 3602 nd->nd_repstat = 0; 3603 fl->l_whence = SEEK_SET; 3604 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 3605 fl->l_start = fxdr_hyper(tl); 3606 tl += 2; 3607 len = fxdr_hyper(tl); 3608 tl += 2; 3609 if (len == NFS64BITSSET) 3610 fl->l_len = 0; 3611 else 3612 fl->l_len = len; 3613 type = fxdr_unsigned(int, *tl++); 3614 if (type == NFSV4LOCKT_WRITE) 3615 fl->l_type = F_WRLCK; 3616 else 3617 fl->l_type = F_RDLCK; 3618 /* 3619 * XXX For now, I have no idea what to do with the 3620 * conflicting lock_owner, so I'll just set the pid == 0 3621 * and skip over the lock_owner. 3622 */ 3623 fl->l_pid = (pid_t)0; 3624 tl += 2; 3625 size = fxdr_unsigned(int, *tl); 3626 if (size < 0 || size > NFSV4_OPAQUELIMIT) 3627 error = EBADRPC; 3628 if (!error) 3629 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 3630 } else if (nd->nd_repstat == NFSERR_STALECLIENTID) 3631 nfscl_initiate_recovery(clp); 3632 nfsmout: 3633 mbuf_freem(nd->nd_mrep); 3634 return (error); 3635 } 3636 3637 /* 3638 * Lower level function that performs the LockU RPC. 3639 */ 3640 static int 3641 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp, 3642 struct nfscllockowner *lp, u_int64_t off, u_int64_t len, 3643 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred) 3644 { 3645 u_int32_t *tl; 3646 int error; 3647 3648 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh, 3649 lp->nfsl_open->nfso_fhlen, NULL); 3650 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED); 3651 *tl++ = txdr_unsigned(type); 3652 *tl = txdr_unsigned(lp->nfsl_seqid); 3653 if (nfstest_outofseq && 3654 (arc4random() % nfstest_outofseq) == 0) 3655 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 3656 tl++; 3657 *tl++ = lp->nfsl_stateid.seqid; 3658 *tl++ = lp->nfsl_stateid.other[0]; 3659 *tl++ = lp->nfsl_stateid.other[1]; 3660 *tl++ = lp->nfsl_stateid.other[2]; 3661 txdr_hyper(off, tl); 3662 tl += 2; 3663 txdr_hyper(len, tl); 3664 if (syscred) 3665 nd->nd_flag |= ND_USEGSSNAME; 3666 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 3667 NFS_PROG, NFS_VER4, NULL, 1, NULL); 3668 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 3669 if (error) 3670 return (error); 3671 if (nd->nd_repstat == 0) { 3672 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 3673 lp->nfsl_stateid.seqid = *tl++; 3674 lp->nfsl_stateid.other[0] = *tl++; 3675 lp->nfsl_stateid.other[1] = *tl++; 3676 lp->nfsl_stateid.other[2] = *tl; 3677 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 3678 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 3679 nfsmout: 3680 mbuf_freem(nd->nd_mrep); 3681 return (error); 3682 } 3683 3684 /* 3685 * The actual Lock RPC. 3686 */ 3687 APPLESTATIC int 3688 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp, 3689 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone, 3690 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred, 3691 NFSPROC_T *p, int syscred) 3692 { 3693 u_int32_t *tl; 3694 int error, size; 3695 3696 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL); 3697 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 3698 if (type == F_RDLCK) 3699 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 3700 else 3701 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 3702 *tl++ = txdr_unsigned(reclaim); 3703 txdr_hyper(off, tl); 3704 tl += 2; 3705 txdr_hyper(len, tl); 3706 tl += 2; 3707 if (newone) { 3708 *tl = newnfs_true; 3709 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3710 2 * NFSX_UNSIGNED + NFSX_HYPER); 3711 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid); 3712 *tl++ = lp->nfsl_open->nfso_stateid.seqid; 3713 *tl++ = lp->nfsl_open->nfso_stateid.other[0]; 3714 *tl++ = lp->nfsl_open->nfso_stateid.other[1]; 3715 *tl++ = lp->nfsl_open->nfso_stateid.other[2]; 3716 *tl++ = txdr_unsigned(lp->nfsl_seqid); 3717 *tl++ = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[0]; 3718 *tl = lp->nfsl_open->nfso_own->nfsow_clp->nfsc_clientid.lval[1]; 3719 (void) nfsm_strtom(nd, lp->nfsl_owner, NFSV4CL_LOCKNAMELEN); 3720 } else { 3721 *tl = newnfs_false; 3722 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED); 3723 *tl++ = lp->nfsl_stateid.seqid; 3724 *tl++ = lp->nfsl_stateid.other[0]; 3725 *tl++ = lp->nfsl_stateid.other[1]; 3726 *tl++ = lp->nfsl_stateid.other[2]; 3727 *tl = txdr_unsigned(lp->nfsl_seqid); 3728 if (nfstest_outofseq && 3729 (arc4random() % nfstest_outofseq) == 0) 3730 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 3731 } 3732 if (syscred) 3733 nd->nd_flag |= ND_USEGSSNAME; 3734 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 3735 NFS_PROG, NFS_VER4, NULL, 1, NULL); 3736 if (error) 3737 return (error); 3738 if (newone) 3739 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd); 3740 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 3741 if (nd->nd_repstat == 0) { 3742 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 3743 lp->nfsl_stateid.seqid = *tl++; 3744 lp->nfsl_stateid.other[0] = *tl++; 3745 lp->nfsl_stateid.other[1] = *tl++; 3746 lp->nfsl_stateid.other[2] = *tl; 3747 } else if (nd->nd_repstat == NFSERR_DENIED) { 3748 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 3749 size = fxdr_unsigned(int, *(tl + 7)); 3750 if (size < 0 || size > NFSV4_OPAQUELIMIT) 3751 error = EBADRPC; 3752 if (!error) 3753 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 3754 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 3755 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 3756 nfsmout: 3757 mbuf_freem(nd->nd_mrep); 3758 return (error); 3759 } 3760 3761 /* 3762 * nfs statfs rpc 3763 * (always called with the vp for the mount point) 3764 */ 3765 APPLESTATIC int 3766 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp, 3767 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 3768 void *stuff) 3769 { 3770 u_int32_t *tl = NULL; 3771 struct nfsrv_descript nfsd, *nd = &nfsd; 3772 struct nfsmount *nmp; 3773 nfsattrbit_t attrbits; 3774 int error; 3775 3776 *attrflagp = 0; 3777 nmp = VFSTONFS(vnode_mount(vp)); 3778 if (NFSHASNFSV4(nmp)) { 3779 /* 3780 * For V4, you actually do a getattr. 3781 */ 3782 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 3783 NFSSTATFS_GETATTRBIT(&attrbits); 3784 (void) nfsrv_putattrbit(nd, &attrbits); 3785 nd->nd_flag |= ND_USEGSSNAME; 3786 error = nfscl_request(nd, vp, p, cred, stuff); 3787 if (error) 3788 return (error); 3789 if (nd->nd_repstat == 0) { 3790 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 3791 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p, 3792 cred); 3793 if (!error) { 3794 nmp->nm_fsid[0] = nap->na_filesid[0]; 3795 nmp->nm_fsid[1] = nap->na_filesid[1]; 3796 NFSSETHASSETFSID(nmp); 3797 *attrflagp = 1; 3798 } 3799 } else { 3800 error = nd->nd_repstat; 3801 } 3802 if (error) 3803 goto nfsmout; 3804 } else { 3805 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp); 3806 error = nfscl_request(nd, vp, p, cred, stuff); 3807 if (error) 3808 return (error); 3809 if (nd->nd_flag & ND_NFSV3) { 3810 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3811 if (error) 3812 goto nfsmout; 3813 } 3814 if (nd->nd_repstat) { 3815 error = nd->nd_repstat; 3816 goto nfsmout; 3817 } 3818 NFSM_DISSECT(tl, u_int32_t *, 3819 NFSX_STATFS(nd->nd_flag & ND_NFSV3)); 3820 } 3821 if (NFSHASNFSV3(nmp)) { 3822 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2; 3823 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2; 3824 sbp->sf_abytes = fxdr_hyper(tl); tl += 2; 3825 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2; 3826 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2; 3827 sbp->sf_afiles = fxdr_hyper(tl); tl += 2; 3828 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl); 3829 } else if (NFSHASNFSV4(nmp) == 0) { 3830 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++); 3831 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++); 3832 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++); 3833 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++); 3834 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl); 3835 } 3836 nfsmout: 3837 mbuf_freem(nd->nd_mrep); 3838 return (error); 3839 } 3840 3841 /* 3842 * nfs pathconf rpc 3843 */ 3844 APPLESTATIC int 3845 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc, 3846 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 3847 void *stuff) 3848 { 3849 struct nfsrv_descript nfsd, *nd = &nfsd; 3850 struct nfsmount *nmp; 3851 u_int32_t *tl; 3852 nfsattrbit_t attrbits; 3853 int error; 3854 3855 *attrflagp = 0; 3856 nmp = VFSTONFS(vnode_mount(vp)); 3857 if (NFSHASNFSV4(nmp)) { 3858 /* 3859 * For V4, you actually do a getattr. 3860 */ 3861 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 3862 NFSPATHCONF_GETATTRBIT(&attrbits); 3863 (void) nfsrv_putattrbit(nd, &attrbits); 3864 nd->nd_flag |= ND_USEGSSNAME; 3865 error = nfscl_request(nd, vp, p, cred, stuff); 3866 if (error) 3867 return (error); 3868 if (nd->nd_repstat == 0) { 3869 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 3870 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p, 3871 cred); 3872 if (!error) 3873 *attrflagp = 1; 3874 } else { 3875 error = nd->nd_repstat; 3876 } 3877 } else { 3878 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp); 3879 error = nfscl_request(nd, vp, p, cred, stuff); 3880 if (error) 3881 return (error); 3882 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3883 if (nd->nd_repstat && !error) 3884 error = nd->nd_repstat; 3885 if (!error) { 3886 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF); 3887 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++); 3888 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++); 3889 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++); 3890 pc->pc_chownrestricted = 3891 fxdr_unsigned(u_int32_t, *tl++); 3892 pc->pc_caseinsensitive = 3893 fxdr_unsigned(u_int32_t, *tl++); 3894 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl); 3895 } 3896 } 3897 nfsmout: 3898 mbuf_freem(nd->nd_mrep); 3899 return (error); 3900 } 3901 3902 /* 3903 * nfs version 3 fsinfo rpc call 3904 */ 3905 APPLESTATIC int 3906 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred, 3907 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 3908 { 3909 u_int32_t *tl; 3910 struct nfsrv_descript nfsd, *nd = &nfsd; 3911 int error; 3912 3913 *attrflagp = 0; 3914 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp); 3915 error = nfscl_request(nd, vp, p, cred, stuff); 3916 if (error) 3917 return (error); 3918 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3919 if (nd->nd_repstat && !error) 3920 error = nd->nd_repstat; 3921 if (!error) { 3922 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO); 3923 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++); 3924 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++); 3925 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++); 3926 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++); 3927 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++); 3928 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++); 3929 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++); 3930 fsp->fs_maxfilesize = fxdr_hyper(tl); 3931 tl += 2; 3932 fxdr_nfsv3time(tl, &fsp->fs_timedelta); 3933 tl += 2; 3934 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl); 3935 } 3936 nfsmout: 3937 mbuf_freem(nd->nd_mrep); 3938 return (error); 3939 } 3940 3941 /* 3942 * This function performs the Renew RPC. 3943 */ 3944 APPLESTATIC int 3945 nfsrpc_renew(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p) 3946 { 3947 u_int32_t *tl; 3948 struct nfsrv_descript nfsd; 3949 struct nfsrv_descript *nd = &nfsd; 3950 struct nfsmount *nmp; 3951 int error; 3952 3953 nmp = clp->nfsc_nmp; 3954 if (nmp == NULL) 3955 return (0); 3956 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL); 3957 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3958 *tl++ = clp->nfsc_clientid.lval[0]; 3959 *tl = clp->nfsc_clientid.lval[1]; 3960 nd->nd_flag |= ND_USEGSSNAME; 3961 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 3962 NFS_PROG, NFS_VER4, NULL, 1, NULL); 3963 if (error) 3964 return (error); 3965 error = nd->nd_repstat; 3966 mbuf_freem(nd->nd_mrep); 3967 return (error); 3968 } 3969 3970 /* 3971 * This function performs the Releaselockowner RPC. 3972 */ 3973 APPLESTATIC int 3974 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp, 3975 struct ucred *cred, NFSPROC_T *p) 3976 { 3977 struct nfsrv_descript nfsd, *nd = &nfsd; 3978 u_int32_t *tl; 3979 int error; 3980 3981 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL); 3982 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3983 *tl++ = nmp->nm_clp->nfsc_clientid.lval[0]; 3984 *tl = nmp->nm_clp->nfsc_clientid.lval[1]; 3985 (void) nfsm_strtom(nd, lp->nfsl_owner, NFSV4CL_LOCKNAMELEN); 3986 nd->nd_flag |= ND_USEGSSNAME; 3987 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 3988 NFS_PROG, NFS_VER4, NULL, 1, NULL); 3989 if (error) 3990 return (error); 3991 error = nd->nd_repstat; 3992 mbuf_freem(nd->nd_mrep); 3993 return (error); 3994 } 3995 3996 /* 3997 * This function performs the Compound to get the mount pt FH. 3998 */ 3999 APPLESTATIC int 4000 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred, 4001 NFSPROC_T *p) 4002 { 4003 u_int32_t *tl; 4004 struct nfsrv_descript nfsd; 4005 struct nfsrv_descript *nd = &nfsd; 4006 u_char *cp, *cp2; 4007 int error, cnt, len, setnil; 4008 u_int32_t *opcntp; 4009 4010 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp); 4011 cp = dirpath; 4012 cnt = 0; 4013 do { 4014 setnil = 0; 4015 while (*cp == '/') 4016 cp++; 4017 cp2 = cp; 4018 while (*cp2 != '\0' && *cp2 != '/') 4019 cp2++; 4020 if (*cp2 == '/') { 4021 setnil = 1; 4022 *cp2 = '\0'; 4023 } 4024 if (cp2 != cp) { 4025 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4026 *tl = txdr_unsigned(NFSV4OP_LOOKUP); 4027 nfsm_strtom(nd, cp, strlen(cp)); 4028 cnt++; 4029 } 4030 if (setnil) 4031 *cp2++ = '/'; 4032 cp = cp2; 4033 } while (*cp != '\0'); 4034 *opcntp = txdr_unsigned(2 + cnt); 4035 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4036 *tl = txdr_unsigned(NFSV4OP_GETFH); 4037 nd->nd_flag |= ND_USEGSSNAME; 4038 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4039 NFS_PROG, NFS_VER4, NULL, 1, NULL); 4040 if (error) 4041 return (error); 4042 if (nd->nd_repstat == 0) { 4043 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED); 4044 tl += (2 + 2 * cnt); 4045 if ((len = fxdr_unsigned(int, *tl)) <= 0 || 4046 len > NFSX_FHMAX) { 4047 nd->nd_repstat = NFSERR_BADXDR; 4048 } else { 4049 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len); 4050 if (nd->nd_repstat == 0) 4051 nmp->nm_fhsize = len; 4052 } 4053 } 4054 error = nd->nd_repstat; 4055 nfsmout: 4056 mbuf_freem(nd->nd_mrep); 4057 return (error); 4058 } 4059 4060 /* 4061 * This function performs the Delegreturn RPC. 4062 */ 4063 APPLESTATIC int 4064 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred, 4065 struct nfsmount *nmp, NFSPROC_T *p, int syscred) 4066 { 4067 u_int32_t *tl; 4068 struct nfsrv_descript nfsd; 4069 struct nfsrv_descript *nd = &nfsd; 4070 int error; 4071 4072 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh, 4073 dp->nfsdl_fhlen, NULL); 4074 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 4075 *tl++ = dp->nfsdl_stateid.seqid; 4076 *tl++ = dp->nfsdl_stateid.other[0]; 4077 *tl++ = dp->nfsdl_stateid.other[1]; 4078 *tl = dp->nfsdl_stateid.other[2]; 4079 if (syscred) 4080 nd->nd_flag |= ND_USEGSSNAME; 4081 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4082 NFS_PROG, NFS_VER4, NULL, 1, NULL); 4083 if (error) 4084 return (error); 4085 error = nd->nd_repstat; 4086 mbuf_freem(nd->nd_mrep); 4087 return (error); 4088 } 4089 4090 /* 4091 * nfs getacl call. 4092 */ 4093 APPLESTATIC int 4094 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4095 struct acl *aclp, void *stuff) 4096 { 4097 struct nfsrv_descript nfsd, *nd = &nfsd; 4098 int error; 4099 nfsattrbit_t attrbits; 4100 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4101 4102 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 4103 return (EOPNOTSUPP); 4104 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp); 4105 NFSZERO_ATTRBIT(&attrbits); 4106 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 4107 (void) nfsrv_putattrbit(nd, &attrbits); 4108 error = nfscl_request(nd, vp, p, cred, stuff); 4109 if (error) 4110 return (error); 4111 if (!nd->nd_repstat) 4112 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL, 4113 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred); 4114 else 4115 error = nd->nd_repstat; 4116 mbuf_freem(nd->nd_mrep); 4117 return (error); 4118 } 4119 4120 /* 4121 * nfs setacl call. 4122 */ 4123 APPLESTATIC int 4124 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4125 struct acl *aclp, void *stuff) 4126 { 4127 int error; 4128 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4129 4130 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 4131 return (EOPNOTSUPP); 4132 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff); 4133 return (error); 4134 } 4135 4136 /* 4137 * nfs setacl call. 4138 */ 4139 static int 4140 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4141 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff) 4142 { 4143 struct nfsrv_descript nfsd, *nd = &nfsd; 4144 int error; 4145 nfsattrbit_t attrbits; 4146 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4147 4148 if (!NFSHASNFSV4(nmp)) 4149 return (EOPNOTSUPP); 4150 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp); 4151 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 4152 NFSZERO_ATTRBIT(&attrbits); 4153 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 4154 (void) nfsv4_fillattr(nd, vp, aclp, NULL, NULL, 0, &attrbits, 4155 NULL, NULL, 0, 0); 4156 error = nfscl_request(nd, vp, p, cred, stuff); 4157 if (error) 4158 return (error); 4159 /* Don't care about the pre/postop attributes */ 4160 mbuf_freem(nd->nd_mrep); 4161 return (nd->nd_repstat); 4162 } 4163