1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * Rpc op calls, generally called from the vnode op calls or through the 41 * buffer cache, for NFS v2, 3 and 4. 42 * These do not normally make any changes to vnode arguments or use 43 * structures that might change between the VFS variants. The returned 44 * arguments are all at the end, after the NFSPROC_T *p one. 45 */ 46 47 #ifndef APPLEKEXT 48 #include "opt_inet6.h" 49 50 #include <fs/nfs/nfsport.h> 51 #include <fs/nfsclient/nfs.h> 52 #include <sys/extattr.h> 53 #include <sys/sysctl.h> 54 #include <sys/taskqueue.h> 55 56 SYSCTL_DECL(_vfs_nfs); 57 58 static int nfsignore_eexist = 0; 59 SYSCTL_INT(_vfs_nfs, OID_AUTO, ignore_eexist, CTLFLAG_RW, 60 &nfsignore_eexist, 0, "NFS ignore EEXIST replies for mkdir/symlink"); 61 62 static int nfscl_dssameconn = 0; 63 SYSCTL_INT(_vfs_nfs, OID_AUTO, dssameconn, CTLFLAG_RW, 64 &nfscl_dssameconn, 0, "Use same TCP connection to multiple DSs"); 65 66 /* 67 * Global variables 68 */ 69 extern int nfs_numnfscbd; 70 extern struct timeval nfsboottime; 71 extern u_int32_t newnfs_false, newnfs_true; 72 extern nfstype nfsv34_type[9]; 73 extern int nfsrv_useacl; 74 extern char nfsv4_callbackaddr[INET6_ADDRSTRLEN]; 75 extern int nfscl_debuglevel; 76 extern int nfs_pnfsiothreads; 77 extern u_long sb_max_adj; 78 extern int nfs_maxcopyrange; 79 NFSCLSTATEMUTEX; 80 int nfstest_outofseq = 0; 81 int nfscl_assumeposixlocks = 1; 82 int nfscl_enablecallb = 0; 83 short nfsv4_cbport = NFSV4_CBPORT; 84 int nfstest_openallsetattr = 0; 85 #endif /* !APPLEKEXT */ 86 87 #define DIRHDSIZ offsetof(struct dirent, d_name) 88 89 /* 90 * nfscl_getsameserver() can return one of three values: 91 * NFSDSP_USETHISSESSION - Use this session for the DS. 92 * NFSDSP_SEQTHISSESSION - Use the nfsclds_sequence field of this dsp for new 93 * session. 94 * NFSDSP_NOTFOUND - No matching server was found. 95 */ 96 enum nfsclds_state { 97 NFSDSP_USETHISSESSION = 0, 98 NFSDSP_SEQTHISSESSION = 1, 99 NFSDSP_NOTFOUND = 2, 100 }; 101 102 /* 103 * Do a write RPC on a DS data file, using this structure for the arguments, 104 * so that this function can be executed by a separate kernel process. 105 */ 106 struct nfsclwritedsdorpc { 107 int done; 108 int inprog; 109 struct task tsk; 110 struct vnode *vp; 111 int iomode; 112 int must_commit; 113 nfsv4stateid_t *stateidp; 114 struct nfsclds *dsp; 115 uint64_t off; 116 int len; 117 #ifdef notyet 118 int advise; 119 #endif 120 struct nfsfh *fhp; 121 struct mbuf *m; 122 int vers; 123 int minorvers; 124 struct ucred *cred; 125 NFSPROC_T *p; 126 int err; 127 }; 128 129 static int nfsrpc_setattrrpc(vnode_t , struct vattr *, nfsv4stateid_t *, 130 struct ucred *, NFSPROC_T *, struct nfsvattr *, int *, void *); 131 static int nfsrpc_readrpc(vnode_t , struct uio *, struct ucred *, 132 nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, void *); 133 static int nfsrpc_writerpc(vnode_t , struct uio *, int *, int *, 134 struct ucred *, nfsv4stateid_t *, NFSPROC_T *, struct nfsvattr *, int *, 135 void *); 136 static int nfsrpc_createv23(vnode_t , char *, int, struct vattr *, 137 nfsquad_t, int, struct ucred *, NFSPROC_T *, struct nfsvattr *, 138 struct nfsvattr *, struct nfsfh **, int *, int *, void *); 139 static int nfsrpc_createv4(vnode_t , char *, int, struct vattr *, 140 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, struct ucred *, 141 NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, struct nfsfh **, int *, 142 int *, void *, int *); 143 static int nfsrpc_locku(struct nfsrv_descript *, struct nfsmount *, 144 struct nfscllockowner *, u_int64_t, u_int64_t, 145 u_int32_t, struct ucred *, NFSPROC_T *, int); 146 static int nfsrpc_setaclrpc(vnode_t, struct ucred *, NFSPROC_T *, 147 struct acl *, nfsv4stateid_t *, void *); 148 static int nfsrpc_getlayout(struct nfsmount *, vnode_t, struct nfsfh *, int, 149 uint32_t *, nfsv4stateid_t *, uint64_t, struct nfscllayout **, 150 struct ucred *, NFSPROC_T *); 151 static int nfsrpc_fillsa(struct nfsmount *, struct sockaddr_in *, 152 struct sockaddr_in6 *, sa_family_t, int, int, struct nfsclds **, 153 NFSPROC_T *); 154 static void nfscl_initsessionslots(struct nfsclsession *); 155 static int nfscl_doflayoutio(vnode_t, struct uio *, int *, int *, int *, 156 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *, 157 struct nfsclflayout *, uint64_t, uint64_t, int, struct ucred *, 158 NFSPROC_T *); 159 static int nfscl_dofflayoutio(vnode_t, struct uio *, int *, int *, int *, 160 nfsv4stateid_t *, int, struct nfscldevinfo *, struct nfscllayout *, 161 struct nfsclflayout *, uint64_t, uint64_t, int, int, struct mbuf *, 162 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *); 163 static struct mbuf *nfsm_copym(struct mbuf *, int, int); 164 static int nfsrpc_readds(vnode_t, struct uio *, nfsv4stateid_t *, int *, 165 struct nfsclds *, uint64_t, int, struct nfsfh *, int, int, int, 166 struct ucred *, NFSPROC_T *); 167 static int nfsrpc_writeds(vnode_t, struct uio *, int *, int *, 168 nfsv4stateid_t *, struct nfsclds *, uint64_t, int, 169 struct nfsfh *, int, int, int, int, struct ucred *, NFSPROC_T *); 170 static int nfsio_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *, 171 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int, 172 struct nfsclwritedsdorpc *, struct ucred *, NFSPROC_T *); 173 static int nfsrpc_writedsmir(vnode_t, int *, int *, nfsv4stateid_t *, 174 struct nfsclds *, uint64_t, int, struct nfsfh *, struct mbuf *, int, int, 175 struct ucred *, NFSPROC_T *); 176 static enum nfsclds_state nfscl_getsameserver(struct nfsmount *, 177 struct nfsclds *, struct nfsclds **, uint32_t *); 178 static int nfsio_commitds(vnode_t, uint64_t, int, struct nfsclds *, 179 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *, 180 NFSPROC_T *); 181 static int nfsrpc_commitds(vnode_t, uint64_t, int, struct nfsclds *, 182 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *); 183 #ifdef notyet 184 static int nfsio_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *, 185 struct nfsfh *, int, int, struct nfsclwritedsdorpc *, struct ucred *, 186 NFSPROC_T *); 187 static int nfsrpc_adviseds(vnode_t, uint64_t, int, int, struct nfsclds *, 188 struct nfsfh *, int, int, struct ucred *, NFSPROC_T *); 189 #endif 190 static int nfsrpc_allocaterpc(vnode_t, off_t, off_t, nfsv4stateid_t *, 191 struct nfsvattr *, int *, struct ucred *, NFSPROC_T *, void *); 192 static void nfsrv_setuplayoutget(struct nfsrv_descript *, int, uint64_t, 193 uint64_t, uint64_t, nfsv4stateid_t *, int, int, int); 194 static int nfsrv_parseug(struct nfsrv_descript *, int, uid_t *, gid_t *, 195 NFSPROC_T *); 196 static int nfsrv_parselayoutget(struct nfsmount *, struct nfsrv_descript *, 197 nfsv4stateid_t *, int *, struct nfsclflayouthead *); 198 static int nfsrpc_getopenlayout(struct nfsmount *, vnode_t, u_int8_t *, 199 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int, 200 struct nfscldeleg **, struct ucred *, NFSPROC_T *); 201 static int nfsrpc_getcreatelayout(vnode_t, char *, int, struct vattr *, 202 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, 203 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, 204 struct nfsfh **, int *, int *, void *, int *); 205 static int nfsrpc_openlayoutrpc(struct nfsmount *, vnode_t, u_int8_t *, 206 int, uint8_t *, int, uint32_t, struct nfsclopen *, uint8_t *, int, 207 struct nfscldeleg **, nfsv4stateid_t *, int, int, int, int *, 208 struct nfsclflayouthead *, int *, struct ucred *, NFSPROC_T *); 209 static int nfsrpc_createlayout(vnode_t, char *, int, struct vattr *, 210 nfsquad_t, int, struct nfsclowner *, struct nfscldeleg **, 211 struct ucred *, NFSPROC_T *, struct nfsvattr *, struct nfsvattr *, 212 struct nfsfh **, int *, int *, void *, int *, nfsv4stateid_t *, 213 int, int, int, int *, struct nfsclflayouthead *, int *); 214 static int nfsrpc_layoutget(struct nfsmount *, uint8_t *, int, int, uint64_t, 215 uint64_t, uint64_t, int, int, nfsv4stateid_t *, int *, 216 struct nfsclflayouthead *, struct ucred *, NFSPROC_T *, void *); 217 static int nfsrpc_layoutgetres(struct nfsmount *, vnode_t, uint8_t *, 218 int, nfsv4stateid_t *, int, uint32_t *, struct nfscllayout **, 219 struct nfsclflayouthead *, int, int, int *, struct ucred *, NFSPROC_T *); 220 static int nfsrpc_copyrpc(vnode_t, off_t, vnode_t, off_t, size_t *, 221 nfsv4stateid_t *, nfsv4stateid_t *, struct nfsvattr *, int *, 222 struct nfsvattr *, int *, bool, int *, struct ucred *, NFSPROC_T *); 223 static int nfsrpc_seekrpc(vnode_t, off_t *, nfsv4stateid_t *, bool *, 224 int, struct nfsvattr *, int *, struct ucred *); 225 226 int nfs_pnfsio(task_fn_t *, void *); 227 228 /* 229 * nfs null call from vfs. 230 */ 231 APPLESTATIC int 232 nfsrpc_null(vnode_t vp, struct ucred *cred, NFSPROC_T *p) 233 { 234 int error; 235 struct nfsrv_descript nfsd, *nd = &nfsd; 236 237 NFSCL_REQSTART(nd, NFSPROC_NULL, vp); 238 error = nfscl_request(nd, vp, p, cred, NULL); 239 if (nd->nd_repstat && !error) 240 error = nd->nd_repstat; 241 m_freem(nd->nd_mrep); 242 return (error); 243 } 244 245 /* 246 * nfs access rpc op. 247 * For nfs version 3 and 4, use the access rpc to check accessibility. If file 248 * modes are changed on the server, accesses might still fail later. 249 */ 250 APPLESTATIC int 251 nfsrpc_access(vnode_t vp, int acmode, struct ucred *cred, 252 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp) 253 { 254 int error; 255 u_int32_t mode, rmode; 256 257 if (acmode & VREAD) 258 mode = NFSACCESS_READ; 259 else 260 mode = 0; 261 if (vnode_vtype(vp) == VDIR) { 262 if (acmode & VWRITE) 263 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND | 264 NFSACCESS_DELETE); 265 if (acmode & VEXEC) 266 mode |= NFSACCESS_LOOKUP; 267 } else { 268 if (acmode & VWRITE) 269 mode |= (NFSACCESS_MODIFY | NFSACCESS_EXTEND); 270 if (acmode & VEXEC) 271 mode |= NFSACCESS_EXECUTE; 272 } 273 274 /* 275 * Now, just call nfsrpc_accessrpc() to do the actual RPC. 276 */ 277 error = nfsrpc_accessrpc(vp, mode, cred, p, nap, attrflagp, &rmode, 278 NULL); 279 280 /* 281 * The NFS V3 spec does not clarify whether or not 282 * the returned access bits can be a superset of 283 * the ones requested, so... 284 */ 285 if (!error && (rmode & mode) != mode) 286 error = EACCES; 287 return (error); 288 } 289 290 /* 291 * The actual rpc, separated out for Darwin. 292 */ 293 APPLESTATIC int 294 nfsrpc_accessrpc(vnode_t vp, u_int32_t mode, struct ucred *cred, 295 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, u_int32_t *rmodep, 296 void *stuff) 297 { 298 u_int32_t *tl; 299 u_int32_t supported, rmode; 300 int error; 301 struct nfsrv_descript nfsd, *nd = &nfsd; 302 nfsattrbit_t attrbits; 303 304 *attrflagp = 0; 305 supported = mode; 306 NFSCL_REQSTART(nd, NFSPROC_ACCESS, vp); 307 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 308 *tl = txdr_unsigned(mode); 309 if (nd->nd_flag & ND_NFSV4) { 310 /* 311 * And do a Getattr op. 312 */ 313 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 314 *tl = txdr_unsigned(NFSV4OP_GETATTR); 315 NFSGETATTR_ATTRBIT(&attrbits); 316 (void) nfsrv_putattrbit(nd, &attrbits); 317 } 318 error = nfscl_request(nd, vp, p, cred, stuff); 319 if (error) 320 return (error); 321 if (nd->nd_flag & ND_NFSV3) { 322 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 323 if (error) 324 goto nfsmout; 325 } 326 if (!nd->nd_repstat) { 327 if (nd->nd_flag & ND_NFSV4) { 328 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 329 supported = fxdr_unsigned(u_int32_t, *tl++); 330 } else { 331 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 332 } 333 rmode = fxdr_unsigned(u_int32_t, *tl); 334 if (nd->nd_flag & ND_NFSV4) 335 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 336 337 /* 338 * It's not obvious what should be done about 339 * unsupported access modes. For now, be paranoid 340 * and clear the unsupported ones. 341 */ 342 rmode &= supported; 343 *rmodep = rmode; 344 } else 345 error = nd->nd_repstat; 346 nfsmout: 347 m_freem(nd->nd_mrep); 348 return (error); 349 } 350 351 /* 352 * nfs open rpc 353 */ 354 APPLESTATIC int 355 nfsrpc_open(vnode_t vp, int amode, struct ucred *cred, NFSPROC_T *p) 356 { 357 struct nfsclopen *op; 358 struct nfscldeleg *dp; 359 struct nfsfh *nfhp; 360 struct nfsnode *np = VTONFS(vp); 361 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 362 u_int32_t mode, clidrev; 363 int ret, newone, error, expireret = 0, retrycnt; 364 365 /* 366 * For NFSv4, Open Ops are only done on Regular Files. 367 */ 368 if (vnode_vtype(vp) != VREG) 369 return (0); 370 mode = 0; 371 if (amode & FREAD) 372 mode |= NFSV4OPEN_ACCESSREAD; 373 if (amode & FWRITE) 374 mode |= NFSV4OPEN_ACCESSWRITE; 375 nfhp = np->n_fhp; 376 377 retrycnt = 0; 378 #ifdef notdef 379 { char name[100]; int namel; 380 namel = (np->n_v4->n4_namelen < 100) ? np->n_v4->n4_namelen : 99; 381 bcopy(NFS4NODENAME(np->n_v4), name, namel); 382 name[namel] = '\0'; 383 printf("rpcopen p=0x%x name=%s",p->p_pid,name); 384 if (nfhp->nfh_len > 0) printf(" fh=0x%x\n",nfhp->nfh_fh[12]); 385 else printf(" fhl=0\n"); 386 } 387 #endif 388 do { 389 dp = NULL; 390 error = nfscl_open(vp, nfhp->nfh_fh, nfhp->nfh_len, mode, 1, 391 cred, p, NULL, &op, &newone, &ret, 1); 392 if (error) { 393 return (error); 394 } 395 if (nmp->nm_clp != NULL) 396 clidrev = nmp->nm_clp->nfsc_clientidrev; 397 else 398 clidrev = 0; 399 if (ret == NFSCLOPEN_DOOPEN) { 400 if (np->n_v4 != NULL) { 401 /* 402 * For the first attempt, try and get a layout, if 403 * pNFS is enabled for the mount. 404 */ 405 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || 406 nfs_numnfscbd == 0 || 407 (np->n_flag & NNOLAYOUT) != 0 || retrycnt > 0) 408 error = nfsrpc_openrpc(nmp, vp, 409 np->n_v4->n4_data, 410 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh, 411 np->n_fhp->nfh_len, mode, op, 412 NFS4NODENAME(np->n_v4), 413 np->n_v4->n4_namelen, 414 &dp, 0, 0x0, cred, p, 0, 0); 415 else 416 error = nfsrpc_getopenlayout(nmp, vp, 417 np->n_v4->n4_data, 418 np->n_v4->n4_fhlen, np->n_fhp->nfh_fh, 419 np->n_fhp->nfh_len, mode, op, 420 NFS4NODENAME(np->n_v4), 421 np->n_v4->n4_namelen, &dp, cred, p); 422 if (dp != NULL) { 423 #ifdef APPLE 424 OSBitAndAtomic((int32_t)~NDELEGMOD, (UInt32 *)&np->n_flag); 425 #else 426 NFSLOCKNODE(np); 427 np->n_flag &= ~NDELEGMOD; 428 /* 429 * Invalidate the attribute cache, so that 430 * attributes that pre-date the issue of a 431 * delegation are not cached, since the 432 * cached attributes will remain valid while 433 * the delegation is held. 434 */ 435 NFSINVALATTRCACHE(np); 436 NFSUNLOCKNODE(np); 437 #endif 438 (void) nfscl_deleg(nmp->nm_mountp, 439 op->nfso_own->nfsow_clp, 440 nfhp->nfh_fh, nfhp->nfh_len, cred, p, &dp); 441 } 442 } else { 443 error = EIO; 444 } 445 newnfs_copyincred(cred, &op->nfso_cred); 446 } else if (ret == NFSCLOPEN_SETCRED) 447 /* 448 * This is a new local open on a delegation. It needs 449 * to have credentials so that an open can be done 450 * against the server during recovery. 451 */ 452 newnfs_copyincred(cred, &op->nfso_cred); 453 454 /* 455 * nfso_opencnt is the count of how many VOP_OPEN()s have 456 * been done on this Open successfully and a VOP_CLOSE() 457 * is expected for each of these. 458 * If error is non-zero, don't increment it, since the Open 459 * hasn't succeeded yet. 460 */ 461 if (!error) 462 op->nfso_opencnt++; 463 nfscl_openrelease(nmp, op, error, newone); 464 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 465 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 466 error == NFSERR_BADSESSION) { 467 (void) nfs_catnap(PZERO, error, "nfs_open"); 468 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) 469 && clidrev != 0) { 470 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 471 retrycnt++; 472 } 473 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 474 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 475 error == NFSERR_BADSESSION || 476 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 477 expireret == 0 && clidrev != 0 && retrycnt < 4)); 478 if (error && retrycnt >= 4) 479 error = EIO; 480 return (error); 481 } 482 483 /* 484 * the actual open rpc 485 */ 486 APPLESTATIC int 487 nfsrpc_openrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, int fhlen, 488 u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op, 489 u_int8_t *name, int namelen, struct nfscldeleg **dpp, 490 int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p, 491 int syscred, int recursed) 492 { 493 u_int32_t *tl; 494 struct nfsrv_descript nfsd, *nd = &nfsd; 495 struct nfscldeleg *dp, *ndp = NULL; 496 struct nfsvattr nfsva; 497 u_int32_t rflags, deleg; 498 nfsattrbit_t attrbits; 499 int error, ret, acesize, limitby; 500 struct nfsclsession *tsep; 501 502 dp = *dpp; 503 *dpp = NULL; 504 nfscl_reqstart(nd, NFSPROC_OPEN, nmp, nfhp, fhlen, NULL, NULL, 0, 0); 505 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 506 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 507 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH); 508 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 509 tsep = nfsmnt_mdssession(nmp); 510 *tl++ = tsep->nfsess_clientid.lval[0]; 511 *tl = tsep->nfsess_clientid.lval[1]; 512 (void) nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN); 513 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 514 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE); 515 if (reclaim) { 516 *tl = txdr_unsigned(NFSV4OPEN_CLAIMPREVIOUS); 517 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 518 *tl = txdr_unsigned(delegtype); 519 } else { 520 if (dp != NULL) { 521 *tl = txdr_unsigned(NFSV4OPEN_CLAIMDELEGATECUR); 522 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 523 if (NFSHASNFSV4N(nmp)) 524 *tl++ = 0; 525 else 526 *tl++ = dp->nfsdl_stateid.seqid; 527 *tl++ = dp->nfsdl_stateid.other[0]; 528 *tl++ = dp->nfsdl_stateid.other[1]; 529 *tl = dp->nfsdl_stateid.other[2]; 530 } else { 531 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 532 } 533 (void) nfsm_strtom(nd, name, namelen); 534 } 535 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 536 *tl = txdr_unsigned(NFSV4OP_GETATTR); 537 NFSZERO_ATTRBIT(&attrbits); 538 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 539 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 540 (void) nfsrv_putattrbit(nd, &attrbits); 541 if (syscred) 542 nd->nd_flag |= ND_USEGSSNAME; 543 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 544 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 545 if (error) 546 return (error); 547 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 548 if (!nd->nd_repstat) { 549 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 550 6 * NFSX_UNSIGNED); 551 op->nfso_stateid.seqid = *tl++; 552 op->nfso_stateid.other[0] = *tl++; 553 op->nfso_stateid.other[1] = *tl++; 554 op->nfso_stateid.other[2] = *tl; 555 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 556 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 557 if (error) 558 goto nfsmout; 559 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 560 deleg = fxdr_unsigned(u_int32_t, *tl); 561 if (deleg == NFSV4OPEN_DELEGATEREAD || 562 deleg == NFSV4OPEN_DELEGATEWRITE) { 563 if (!(op->nfso_own->nfsow_clp->nfsc_flags & 564 NFSCLFLAGS_FIRSTDELEG)) 565 op->nfso_own->nfsow_clp->nfsc_flags |= 566 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 567 ndp = malloc( 568 sizeof (struct nfscldeleg) + newfhlen, 569 M_NFSCLDELEG, M_WAITOK); 570 LIST_INIT(&ndp->nfsdl_owner); 571 LIST_INIT(&ndp->nfsdl_lock); 572 ndp->nfsdl_clp = op->nfso_own->nfsow_clp; 573 ndp->nfsdl_fhlen = newfhlen; 574 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen); 575 newnfs_copyincred(cred, &ndp->nfsdl_cred); 576 nfscl_lockinit(&ndp->nfsdl_rwlock); 577 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 578 NFSX_UNSIGNED); 579 ndp->nfsdl_stateid.seqid = *tl++; 580 ndp->nfsdl_stateid.other[0] = *tl++; 581 ndp->nfsdl_stateid.other[1] = *tl++; 582 ndp->nfsdl_stateid.other[2] = *tl++; 583 ret = fxdr_unsigned(int, *tl); 584 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 585 ndp->nfsdl_flags = NFSCLDL_WRITE; 586 /* 587 * Indicates how much the file can grow. 588 */ 589 NFSM_DISSECT(tl, u_int32_t *, 590 3 * NFSX_UNSIGNED); 591 limitby = fxdr_unsigned(int, *tl++); 592 switch (limitby) { 593 case NFSV4OPEN_LIMITSIZE: 594 ndp->nfsdl_sizelimit = fxdr_hyper(tl); 595 break; 596 case NFSV4OPEN_LIMITBLOCKS: 597 ndp->nfsdl_sizelimit = 598 fxdr_unsigned(u_int64_t, *tl++); 599 ndp->nfsdl_sizelimit *= 600 fxdr_unsigned(u_int64_t, *tl); 601 break; 602 default: 603 error = NFSERR_BADXDR; 604 goto nfsmout; 605 } 606 } else { 607 ndp->nfsdl_flags = NFSCLDL_READ; 608 } 609 if (ret) 610 ndp->nfsdl_flags |= NFSCLDL_RECALL; 611 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret, 612 &acesize, p); 613 if (error) 614 goto nfsmout; 615 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 616 error = NFSERR_BADXDR; 617 goto nfsmout; 618 } 619 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 620 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 621 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 622 NULL, NULL, NULL, p, cred); 623 if (error) 624 goto nfsmout; 625 if (ndp != NULL) { 626 ndp->nfsdl_change = nfsva.na_filerev; 627 ndp->nfsdl_modtime = nfsva.na_mtime; 628 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET; 629 } 630 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM)) { 631 do { 632 ret = nfsrpc_openconfirm(vp, newfhp, newfhlen, op, 633 cred, p); 634 if (ret == NFSERR_DELAY) 635 (void) nfs_catnap(PZERO, ret, "nfs_open"); 636 } while (ret == NFSERR_DELAY); 637 error = ret; 638 } 639 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) || 640 nfscl_assumeposixlocks) 641 op->nfso_posixlock = 1; 642 else 643 op->nfso_posixlock = 0; 644 645 /* 646 * If the server is handing out delegations, but we didn't 647 * get one because an OpenConfirm was required, try the 648 * Open again, to get a delegation. This is a harmless no-op, 649 * from a server's point of view. 650 */ 651 if (!reclaim && (rflags & NFSV4OPEN_RESULTCONFIRM) && 652 (op->nfso_own->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) 653 && !error && dp == NULL && ndp == NULL && !recursed) { 654 do { 655 ret = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, 656 newfhlen, mode, op, name, namelen, &ndp, 0, 0x0, 657 cred, p, syscred, 1); 658 if (ret == NFSERR_DELAY) 659 (void) nfs_catnap(PZERO, ret, "nfs_open2"); 660 } while (ret == NFSERR_DELAY); 661 if (ret) { 662 if (ndp != NULL) { 663 free(ndp, M_NFSCLDELEG); 664 ndp = NULL; 665 } 666 if (ret == NFSERR_STALECLIENTID || 667 ret == NFSERR_STALEDONTRECOVER || 668 ret == NFSERR_BADSESSION) 669 error = ret; 670 } 671 } 672 } 673 if (nd->nd_repstat != 0 && error == 0) 674 error = nd->nd_repstat; 675 if (error == NFSERR_STALECLIENTID) 676 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 677 nfsmout: 678 if (!error) 679 *dpp = ndp; 680 else if (ndp != NULL) 681 free(ndp, M_NFSCLDELEG); 682 m_freem(nd->nd_mrep); 683 return (error); 684 } 685 686 /* 687 * open downgrade rpc 688 */ 689 APPLESTATIC int 690 nfsrpc_opendowngrade(vnode_t vp, u_int32_t mode, struct nfsclopen *op, 691 struct ucred *cred, NFSPROC_T *p) 692 { 693 u_int32_t *tl; 694 struct nfsrv_descript nfsd, *nd = &nfsd; 695 int error; 696 697 NFSCL_REQSTART(nd, NFSPROC_OPENDOWNGRADE, vp); 698 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 3 * NFSX_UNSIGNED); 699 if (NFSHASNFSV4N(VFSTONFS(vnode_mount(vp)))) 700 *tl++ = 0; 701 else 702 *tl++ = op->nfso_stateid.seqid; 703 *tl++ = op->nfso_stateid.other[0]; 704 *tl++ = op->nfso_stateid.other[1]; 705 *tl++ = op->nfso_stateid.other[2]; 706 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 707 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH); 708 *tl = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 709 error = nfscl_request(nd, vp, p, cred, NULL); 710 if (error) 711 return (error); 712 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 713 if (!nd->nd_repstat) { 714 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 715 op->nfso_stateid.seqid = *tl++; 716 op->nfso_stateid.other[0] = *tl++; 717 op->nfso_stateid.other[1] = *tl++; 718 op->nfso_stateid.other[2] = *tl; 719 } 720 if (nd->nd_repstat && error == 0) 721 error = nd->nd_repstat; 722 if (error == NFSERR_STALESTATEID) 723 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 724 nfsmout: 725 m_freem(nd->nd_mrep); 726 return (error); 727 } 728 729 /* 730 * V4 Close operation. 731 */ 732 APPLESTATIC int 733 nfsrpc_close(vnode_t vp, int doclose, NFSPROC_T *p) 734 { 735 struct nfsclclient *clp; 736 int error; 737 738 if (vnode_vtype(vp) != VREG) 739 return (0); 740 if (doclose) 741 error = nfscl_doclose(vp, &clp, p); 742 else 743 error = nfscl_getclose(vp, &clp); 744 if (error) 745 return (error); 746 747 nfscl_clientrelease(clp); 748 return (0); 749 } 750 751 /* 752 * Close the open. 753 */ 754 APPLESTATIC void 755 nfsrpc_doclose(struct nfsmount *nmp, struct nfsclopen *op, NFSPROC_T *p) 756 { 757 struct nfsrv_descript nfsd, *nd = &nfsd; 758 struct nfscllockowner *lp, *nlp; 759 struct nfscllock *lop, *nlop; 760 struct ucred *tcred; 761 u_int64_t off = 0, len = 0; 762 u_int32_t type = NFSV4LOCKT_READ; 763 int error, do_unlock, trycnt; 764 765 tcred = newnfs_getcred(); 766 newnfs_copycred(&op->nfso_cred, tcred); 767 /* 768 * (Theoretically this could be done in the same 769 * compound as the close, but having multiple 770 * sequenced Ops in the same compound might be 771 * too scary for some servers.) 772 */ 773 if (op->nfso_posixlock) { 774 off = 0; 775 len = NFS64BITSSET; 776 type = NFSV4LOCKT_READ; 777 } 778 779 /* 780 * Since this function is only called from VOP_INACTIVE(), no 781 * other thread will be manipulating this Open. As such, the 782 * lock lists are not being changed by other threads, so it should 783 * be safe to do this without locking. 784 */ 785 LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) { 786 do_unlock = 1; 787 LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) { 788 if (op->nfso_posixlock == 0) { 789 off = lop->nfslo_first; 790 len = lop->nfslo_end - lop->nfslo_first; 791 if (lop->nfslo_type == F_WRLCK) 792 type = NFSV4LOCKT_WRITE; 793 else 794 type = NFSV4LOCKT_READ; 795 } 796 if (do_unlock) { 797 trycnt = 0; 798 do { 799 error = nfsrpc_locku(nd, nmp, lp, off, 800 len, type, tcred, p, 0); 801 if ((nd->nd_repstat == NFSERR_GRACE || 802 nd->nd_repstat == NFSERR_DELAY) && 803 error == 0) 804 (void) nfs_catnap(PZERO, 805 (int)nd->nd_repstat, 806 "nfs_close"); 807 } while ((nd->nd_repstat == NFSERR_GRACE || 808 nd->nd_repstat == NFSERR_DELAY) && 809 error == 0 && trycnt++ < 5); 810 if (op->nfso_posixlock) 811 do_unlock = 0; 812 } 813 nfscl_freelock(lop, 0); 814 } 815 /* 816 * Do a ReleaseLockOwner. 817 * The lock owner name nfsl_owner may be used by other opens for 818 * other files but the lock_owner4 name that nfsrpc_rellockown() 819 * puts on the wire has the file handle for this file appended 820 * to it, so it can be done now. 821 */ 822 (void)nfsrpc_rellockown(nmp, lp, lp->nfsl_open->nfso_fh, 823 lp->nfsl_open->nfso_fhlen, tcred, p); 824 } 825 826 /* 827 * There could be other Opens for different files on the same 828 * OpenOwner, so locking is required. 829 */ 830 NFSLOCKCLSTATE(); 831 nfscl_lockexcl(&op->nfso_own->nfsow_rwlock, NFSCLSTATEMUTEXPTR); 832 NFSUNLOCKCLSTATE(); 833 do { 834 error = nfscl_tryclose(op, tcred, nmp, p); 835 if (error == NFSERR_GRACE) 836 (void) nfs_catnap(PZERO, error, "nfs_close"); 837 } while (error == NFSERR_GRACE); 838 NFSLOCKCLSTATE(); 839 nfscl_lockunlock(&op->nfso_own->nfsow_rwlock); 840 841 LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) 842 nfscl_freelockowner(lp, 0); 843 nfscl_freeopen(op, 0); 844 NFSUNLOCKCLSTATE(); 845 NFSFREECRED(tcred); 846 } 847 848 /* 849 * The actual Close RPC. 850 */ 851 APPLESTATIC int 852 nfsrpc_closerpc(struct nfsrv_descript *nd, struct nfsmount *nmp, 853 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p, 854 int syscred) 855 { 856 u_int32_t *tl; 857 int error; 858 859 nfscl_reqstart(nd, NFSPROC_CLOSE, nmp, op->nfso_fh, 860 op->nfso_fhlen, NULL, NULL, 0, 0); 861 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID); 862 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 863 if (NFSHASNFSV4N(nmp)) 864 *tl++ = 0; 865 else 866 *tl++ = op->nfso_stateid.seqid; 867 *tl++ = op->nfso_stateid.other[0]; 868 *tl++ = op->nfso_stateid.other[1]; 869 *tl = op->nfso_stateid.other[2]; 870 if (syscred) 871 nd->nd_flag |= ND_USEGSSNAME; 872 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 873 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 874 if (error) 875 return (error); 876 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 877 if (nd->nd_repstat == 0) 878 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 879 error = nd->nd_repstat; 880 if (error == NFSERR_STALESTATEID) 881 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 882 nfsmout: 883 m_freem(nd->nd_mrep); 884 return (error); 885 } 886 887 /* 888 * V4 Open Confirm RPC. 889 */ 890 APPLESTATIC int 891 nfsrpc_openconfirm(vnode_t vp, u_int8_t *nfhp, int fhlen, 892 struct nfsclopen *op, struct ucred *cred, NFSPROC_T *p) 893 { 894 u_int32_t *tl; 895 struct nfsrv_descript nfsd, *nd = &nfsd; 896 struct nfsmount *nmp; 897 int error; 898 899 nmp = VFSTONFS(vnode_mount(vp)); 900 if (NFSHASNFSV4N(nmp)) 901 return (0); /* No confirmation for NFSv4.1. */ 902 nfscl_reqstart(nd, NFSPROC_OPENCONFIRM, nmp, nfhp, fhlen, NULL, NULL, 903 0, 0); 904 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_STATEID); 905 *tl++ = op->nfso_stateid.seqid; 906 *tl++ = op->nfso_stateid.other[0]; 907 *tl++ = op->nfso_stateid.other[1]; 908 *tl++ = op->nfso_stateid.other[2]; 909 *tl = txdr_unsigned(op->nfso_own->nfsow_seqid); 910 error = nfscl_request(nd, vp, p, cred, NULL); 911 if (error) 912 return (error); 913 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 914 if (!nd->nd_repstat) { 915 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 916 op->nfso_stateid.seqid = *tl++; 917 op->nfso_stateid.other[0] = *tl++; 918 op->nfso_stateid.other[1] = *tl++; 919 op->nfso_stateid.other[2] = *tl; 920 } 921 error = nd->nd_repstat; 922 if (error == NFSERR_STALESTATEID) 923 nfscl_initiate_recovery(op->nfso_own->nfsow_clp); 924 nfsmout: 925 m_freem(nd->nd_mrep); 926 return (error); 927 } 928 929 /* 930 * Do the setclientid and setclientid confirm RPCs. Called from nfs_statfs() 931 * when a mount has just occurred and when the server replies NFSERR_EXPIRED. 932 */ 933 APPLESTATIC int 934 nfsrpc_setclient(struct nfsmount *nmp, struct nfsclclient *clp, int reclaim, 935 bool *retokp, struct ucred *cred, NFSPROC_T *p) 936 { 937 u_int32_t *tl; 938 struct nfsrv_descript nfsd; 939 struct nfsrv_descript *nd = &nfsd; 940 nfsattrbit_t attrbits; 941 u_int8_t *cp = NULL, *cp2, addr[INET6_ADDRSTRLEN + 9]; 942 u_short port; 943 int error, isinet6 = 0, callblen; 944 nfsquad_t confirm; 945 u_int32_t lease; 946 static u_int32_t rev = 0; 947 struct nfsclds *dsp, *odsp; 948 struct in6_addr a6; 949 struct nfsclsession *tsep; 950 951 if (nfsboottime.tv_sec == 0) 952 NFSSETBOOTTIME(nfsboottime); 953 if (NFSHASNFSV4N(nmp)) { 954 error = NFSERR_BADSESSION; 955 odsp = dsp = NULL; 956 if (retokp != NULL) { 957 NFSLOCKMNT(nmp); 958 odsp = TAILQ_FIRST(&nmp->nm_sess); 959 NFSUNLOCKMNT(nmp); 960 } 961 if (odsp != NULL) { 962 /* 963 * When a session already exists, first try a 964 * CreateSession with the extant ClientID. 965 */ 966 dsp = malloc(sizeof(struct nfsclds) + 967 odsp->nfsclds_servownlen + 1, M_NFSCLDS, 968 M_WAITOK | M_ZERO); 969 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew; 970 dsp->nfsclds_servownlen = odsp->nfsclds_servownlen; 971 dsp->nfsclds_sess.nfsess_clientid = 972 odsp->nfsclds_sess.nfsess_clientid; 973 dsp->nfsclds_sess.nfsess_sequenceid = 974 odsp->nfsclds_sess.nfsess_sequenceid; 975 dsp->nfsclds_flags = odsp->nfsclds_flags; 976 if (dsp->nfsclds_servownlen > 0) 977 memcpy(dsp->nfsclds_serverown, 978 odsp->nfsclds_serverown, 979 dsp->nfsclds_servownlen + 1); 980 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 981 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 982 NULL, MTX_DEF); 983 nfscl_initsessionslots(&dsp->nfsclds_sess); 984 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess, 985 &nmp->nm_sockreq, NULL, 986 dsp->nfsclds_sess.nfsess_sequenceid, 1, cred, p); 987 NFSCL_DEBUG(1, "create session for extant " 988 "ClientID=%d\n", error); 989 if (error != 0) { 990 nfscl_freenfsclds(dsp); 991 dsp = NULL; 992 /* 993 * If *retokp is true, return any error other 994 * than NFSERR_STALECLIENTID, 995 * NFSERR_BADSESSION or NFSERR_STALEDONTRECOVER 996 * so that nfscl_recover() will not loop. 997 */ 998 if (*retokp) 999 return (NFSERR_IO); 1000 } else 1001 *retokp = true; 1002 } else if (retokp != NULL && *retokp) 1003 return (NFSERR_IO); 1004 if (error != 0) { 1005 /* 1006 * Either there was no previous session or the 1007 * CreateSession attempt failed, so... 1008 * do an ExchangeID followed by the CreateSession. 1009 */ 1010 clp->nfsc_rev = rev++; 1011 error = nfsrpc_exchangeid(nmp, clp, &nmp->nm_sockreq, 0, 1012 NFSV4EXCH_USEPNFSMDS | NFSV4EXCH_USENONPNFS, &dsp, 1013 cred, p); 1014 NFSCL_DEBUG(1, "aft exch=%d\n", error); 1015 if (error == 0) 1016 error = nfsrpc_createsession(nmp, 1017 &dsp->nfsclds_sess, &nmp->nm_sockreq, NULL, 1018 dsp->nfsclds_sess.nfsess_sequenceid, 1, 1019 cred, p); 1020 NFSCL_DEBUG(1, "aft createsess=%d\n", error); 1021 } 1022 if (error == 0) { 1023 NFSLOCKMNT(nmp); 1024 /* 1025 * The old sessions cannot be safely free'd 1026 * here, since they may still be used by 1027 * in-progress RPCs. 1028 */ 1029 tsep = NULL; 1030 if (TAILQ_FIRST(&nmp->nm_sess) != NULL) 1031 tsep = NFSMNT_MDSSESSION(nmp); 1032 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, 1033 nfsclds_list); 1034 /* 1035 * Wake up RPCs waiting for a slot on the 1036 * old session. These will then fail with 1037 * NFSERR_BADSESSION and be retried with the 1038 * new session by nfsv4_setsequence(). 1039 * Also wakeup() processes waiting for the 1040 * new session. 1041 */ 1042 if (tsep != NULL) 1043 wakeup(&tsep->nfsess_slots); 1044 wakeup(&nmp->nm_sess); 1045 NFSUNLOCKMNT(nmp); 1046 } else if (dsp != NULL) 1047 nfscl_freenfsclds(dsp); 1048 if (error == 0 && reclaim == 0) { 1049 error = nfsrpc_reclaimcomplete(nmp, cred, p); 1050 NFSCL_DEBUG(1, "aft reclaimcomp=%d\n", error); 1051 if (error == NFSERR_COMPLETEALREADY || 1052 error == NFSERR_NOTSUPP) 1053 /* Ignore this error. */ 1054 error = 0; 1055 } 1056 return (error); 1057 } else if (retokp != NULL && *retokp) 1058 return (NFSERR_IO); 1059 clp->nfsc_rev = rev++; 1060 1061 /* 1062 * Allocate a single session structure for NFSv4.0, because some of 1063 * the fields are used by NFSv4.0 although it doesn't do a session. 1064 */ 1065 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, M_WAITOK | M_ZERO); 1066 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 1067 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", NULL, MTX_DEF); 1068 NFSLOCKMNT(nmp); 1069 TAILQ_INSERT_HEAD(&nmp->nm_sess, dsp, nfsclds_list); 1070 tsep = NFSMNT_MDSSESSION(nmp); 1071 NFSUNLOCKMNT(nmp); 1072 1073 nfscl_reqstart(nd, NFSPROC_SETCLIENTID, nmp, NULL, 0, NULL, NULL, 0, 0); 1074 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1075 *tl++ = txdr_unsigned(nfsboottime.tv_sec); 1076 *tl = txdr_unsigned(clp->nfsc_rev); 1077 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen); 1078 1079 /* 1080 * set up the callback address 1081 */ 1082 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1083 *tl = txdr_unsigned(NFS_CALLBCKPROG); 1084 callblen = strlen(nfsv4_callbackaddr); 1085 if (callblen == 0) 1086 cp = nfscl_getmyip(nmp, &a6, &isinet6); 1087 if (nfscl_enablecallb && nfs_numnfscbd > 0 && 1088 (callblen > 0 || cp != NULL)) { 1089 port = htons(nfsv4_cbport); 1090 cp2 = (u_int8_t *)&port; 1091 #ifdef INET6 1092 if ((callblen > 0 && 1093 strchr(nfsv4_callbackaddr, ':')) || isinet6) { 1094 char ip6buf[INET6_ADDRSTRLEN], *ip6add; 1095 1096 (void) nfsm_strtom(nd, "tcp6", 4); 1097 if (callblen == 0) { 1098 ip6_sprintf(ip6buf, (struct in6_addr *)cp); 1099 ip6add = ip6buf; 1100 } else { 1101 ip6add = nfsv4_callbackaddr; 1102 } 1103 snprintf(addr, INET6_ADDRSTRLEN + 9, "%s.%d.%d", 1104 ip6add, cp2[0], cp2[1]); 1105 } else 1106 #endif 1107 { 1108 (void) nfsm_strtom(nd, "tcp", 3); 1109 if (callblen == 0) 1110 snprintf(addr, INET6_ADDRSTRLEN + 9, 1111 "%d.%d.%d.%d.%d.%d", cp[0], cp[1], 1112 cp[2], cp[3], cp2[0], cp2[1]); 1113 else 1114 snprintf(addr, INET6_ADDRSTRLEN + 9, 1115 "%s.%d.%d", nfsv4_callbackaddr, 1116 cp2[0], cp2[1]); 1117 } 1118 (void) nfsm_strtom(nd, addr, strlen(addr)); 1119 } else { 1120 (void) nfsm_strtom(nd, "tcp", 3); 1121 (void) nfsm_strtom(nd, "0.0.0.0.0.0", 11); 1122 } 1123 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1124 *tl = txdr_unsigned(clp->nfsc_cbident); 1125 nd->nd_flag |= ND_USEGSSNAME; 1126 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 1127 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 1128 if (error) 1129 return (error); 1130 if (nd->nd_repstat == 0) { 1131 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1132 tsep->nfsess_clientid.lval[0] = *tl++; 1133 tsep->nfsess_clientid.lval[1] = *tl++; 1134 confirm.lval[0] = *tl++; 1135 confirm.lval[1] = *tl; 1136 m_freem(nd->nd_mrep); 1137 nd->nd_mrep = NULL; 1138 1139 /* 1140 * and confirm it. 1141 */ 1142 nfscl_reqstart(nd, NFSPROC_SETCLIENTIDCFRM, nmp, NULL, 0, NULL, 1143 NULL, 0, 0); 1144 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1145 *tl++ = tsep->nfsess_clientid.lval[0]; 1146 *tl++ = tsep->nfsess_clientid.lval[1]; 1147 *tl++ = confirm.lval[0]; 1148 *tl = confirm.lval[1]; 1149 nd->nd_flag |= ND_USEGSSNAME; 1150 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 1151 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 1152 if (error) 1153 return (error); 1154 m_freem(nd->nd_mrep); 1155 nd->nd_mrep = NULL; 1156 if (nd->nd_repstat == 0) { 1157 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, nmp->nm_fh, 1158 nmp->nm_fhsize, NULL, NULL, 0, 0); 1159 NFSZERO_ATTRBIT(&attrbits); 1160 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME); 1161 (void) nfsrv_putattrbit(nd, &attrbits); 1162 nd->nd_flag |= ND_USEGSSNAME; 1163 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 1164 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 1165 if (error) 1166 return (error); 1167 if (nd->nd_repstat == 0) { 1168 error = nfsv4_loadattr(nd, NULL, NULL, NULL, NULL, 0, NULL, 1169 NULL, NULL, NULL, NULL, 0, NULL, &lease, NULL, p, cred); 1170 if (error) 1171 goto nfsmout; 1172 clp->nfsc_renew = NFSCL_RENEW(lease); 1173 clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew; 1174 clp->nfsc_clientidrev++; 1175 if (clp->nfsc_clientidrev == 0) 1176 clp->nfsc_clientidrev++; 1177 } 1178 } 1179 } 1180 error = nd->nd_repstat; 1181 nfsmout: 1182 m_freem(nd->nd_mrep); 1183 return (error); 1184 } 1185 1186 /* 1187 * nfs getattr call. 1188 */ 1189 APPLESTATIC int 1190 nfsrpc_getattr(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 1191 struct nfsvattr *nap, void *stuff) 1192 { 1193 struct nfsrv_descript nfsd, *nd = &nfsd; 1194 int error; 1195 nfsattrbit_t attrbits; 1196 1197 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 1198 if (nd->nd_flag & ND_NFSV4) { 1199 NFSGETATTR_ATTRBIT(&attrbits); 1200 (void) nfsrv_putattrbit(nd, &attrbits); 1201 } 1202 error = nfscl_request(nd, vp, p, cred, stuff); 1203 if (error) 1204 return (error); 1205 if (!nd->nd_repstat) 1206 error = nfsm_loadattr(nd, nap); 1207 else 1208 error = nd->nd_repstat; 1209 m_freem(nd->nd_mrep); 1210 return (error); 1211 } 1212 1213 /* 1214 * nfs getattr call with non-vnode arguemnts. 1215 */ 1216 APPLESTATIC int 1217 nfsrpc_getattrnovp(struct nfsmount *nmp, u_int8_t *fhp, int fhlen, int syscred, 1218 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, u_int64_t *xidp, 1219 uint32_t *leasep) 1220 { 1221 struct nfsrv_descript nfsd, *nd = &nfsd; 1222 int error, vers = NFS_VER2; 1223 nfsattrbit_t attrbits; 1224 1225 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0); 1226 if (nd->nd_flag & ND_NFSV4) { 1227 vers = NFS_VER4; 1228 NFSGETATTR_ATTRBIT(&attrbits); 1229 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_LEASETIME); 1230 (void) nfsrv_putattrbit(nd, &attrbits); 1231 } else if (nd->nd_flag & ND_NFSV3) { 1232 vers = NFS_VER3; 1233 } 1234 if (syscred) 1235 nd->nd_flag |= ND_USEGSSNAME; 1236 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 1237 NFS_PROG, vers, NULL, 1, xidp, NULL); 1238 if (error) 1239 return (error); 1240 if (nd->nd_repstat == 0) { 1241 if ((nd->nd_flag & ND_NFSV4) != 0) 1242 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 1243 NULL, NULL, NULL, NULL, NULL, 0, NULL, leasep, NULL, 1244 NULL, NULL); 1245 else 1246 error = nfsm_loadattr(nd, nap); 1247 } else 1248 error = nd->nd_repstat; 1249 m_freem(nd->nd_mrep); 1250 return (error); 1251 } 1252 1253 /* 1254 * Do an nfs setattr operation. 1255 */ 1256 APPLESTATIC int 1257 nfsrpc_setattr(vnode_t vp, struct vattr *vap, NFSACL_T *aclp, 1258 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *rnap, int *attrflagp, 1259 void *stuff) 1260 { 1261 int error, expireret = 0, openerr, retrycnt; 1262 u_int32_t clidrev = 0, mode; 1263 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1264 struct nfsfh *nfhp; 1265 nfsv4stateid_t stateid; 1266 void *lckp; 1267 1268 if (nmp->nm_clp != NULL) 1269 clidrev = nmp->nm_clp->nfsc_clientidrev; 1270 if (vap != NULL && NFSATTRISSET(u_quad_t, vap, va_size)) 1271 mode = NFSV4OPEN_ACCESSWRITE; 1272 else 1273 mode = NFSV4OPEN_ACCESSREAD; 1274 retrycnt = 0; 1275 do { 1276 lckp = NULL; 1277 openerr = 1; 1278 if (NFSHASNFSV4(nmp)) { 1279 nfhp = VTONFS(vp)->n_fhp; 1280 error = nfscl_getstateid(vp, nfhp->nfh_fh, 1281 nfhp->nfh_len, mode, 0, cred, p, &stateid, &lckp); 1282 if (error && vnode_vtype(vp) == VREG && 1283 (mode == NFSV4OPEN_ACCESSWRITE || 1284 nfstest_openallsetattr)) { 1285 /* 1286 * No Open stateid, so try and open the file 1287 * now. 1288 */ 1289 if (mode == NFSV4OPEN_ACCESSWRITE) 1290 openerr = nfsrpc_open(vp, FWRITE, cred, 1291 p); 1292 else 1293 openerr = nfsrpc_open(vp, FREAD, cred, 1294 p); 1295 if (!openerr) 1296 (void) nfscl_getstateid(vp, 1297 nfhp->nfh_fh, nfhp->nfh_len, 1298 mode, 0, cred, p, &stateid, &lckp); 1299 } 1300 } 1301 if (vap != NULL) 1302 error = nfsrpc_setattrrpc(vp, vap, &stateid, cred, p, 1303 rnap, attrflagp, stuff); 1304 else 1305 error = nfsrpc_setaclrpc(vp, cred, p, aclp, &stateid, 1306 stuff); 1307 if (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD) { 1308 NFSLOCKMNT(nmp); 1309 nmp->nm_state |= NFSSTA_OPENMODE; 1310 NFSUNLOCKMNT(nmp); 1311 } 1312 if (error == NFSERR_STALESTATEID) 1313 nfscl_initiate_recovery(nmp->nm_clp); 1314 if (lckp != NULL) 1315 nfscl_lockderef(lckp); 1316 if (!openerr) 1317 (void) nfsrpc_close(vp, 0, p); 1318 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1319 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1320 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 1321 (void) nfs_catnap(PZERO, error, "nfs_setattr"); 1322 } else if ((error == NFSERR_EXPIRED || 1323 error == NFSERR_BADSTATEID) && clidrev != 0) { 1324 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1325 } 1326 retrycnt++; 1327 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1328 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1329 error == NFSERR_BADSESSION || 1330 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 1331 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1332 expireret == 0 && clidrev != 0 && retrycnt < 4) || 1333 (error == NFSERR_OPENMODE && mode == NFSV4OPEN_ACCESSREAD && 1334 retrycnt < 4)); 1335 if (error && retrycnt >= 4) 1336 error = EIO; 1337 return (error); 1338 } 1339 1340 static int 1341 nfsrpc_setattrrpc(vnode_t vp, struct vattr *vap, 1342 nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p, 1343 struct nfsvattr *rnap, int *attrflagp, void *stuff) 1344 { 1345 u_int32_t *tl; 1346 struct nfsrv_descript nfsd, *nd = &nfsd; 1347 int error; 1348 nfsattrbit_t attrbits; 1349 1350 *attrflagp = 0; 1351 NFSCL_REQSTART(nd, NFSPROC_SETATTR, vp); 1352 if (nd->nd_flag & ND_NFSV4) 1353 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 1354 vap->va_type = vnode_vtype(vp); 1355 nfscl_fillsattr(nd, vap, vp, NFSSATTR_FULL, 0); 1356 if (nd->nd_flag & ND_NFSV3) { 1357 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1358 *tl = newnfs_false; 1359 } else if (nd->nd_flag & ND_NFSV4) { 1360 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1361 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1362 NFSGETATTR_ATTRBIT(&attrbits); 1363 (void) nfsrv_putattrbit(nd, &attrbits); 1364 } 1365 error = nfscl_request(nd, vp, p, cred, stuff); 1366 if (error) 1367 return (error); 1368 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 1369 error = nfscl_wcc_data(nd, vp, rnap, attrflagp, NULL, stuff); 1370 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && !error) 1371 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1372 if (!(nd->nd_flag & ND_NFSV3) && !nd->nd_repstat && !error) 1373 error = nfscl_postop_attr(nd, rnap, attrflagp, stuff); 1374 m_freem(nd->nd_mrep); 1375 if (nd->nd_repstat && !error) 1376 error = nd->nd_repstat; 1377 return (error); 1378 } 1379 1380 /* 1381 * nfs lookup rpc 1382 */ 1383 APPLESTATIC int 1384 nfsrpc_lookup(vnode_t dvp, char *name, int len, struct ucred *cred, 1385 NFSPROC_T *p, struct nfsvattr *dnap, struct nfsvattr *nap, 1386 struct nfsfh **nfhpp, int *attrflagp, int *dattrflagp, void *stuff) 1387 { 1388 u_int32_t *tl; 1389 struct nfsrv_descript nfsd, *nd = &nfsd; 1390 struct nfsmount *nmp; 1391 struct nfsnode *np; 1392 struct nfsfh *nfhp; 1393 nfsattrbit_t attrbits; 1394 int error = 0, lookupp = 0; 1395 1396 *attrflagp = 0; 1397 *dattrflagp = 0; 1398 if (vnode_vtype(dvp) != VDIR) 1399 return (ENOTDIR); 1400 nmp = VFSTONFS(vnode_mount(dvp)); 1401 if (len > NFS_MAXNAMLEN) 1402 return (ENAMETOOLONG); 1403 if (NFSHASNFSV4(nmp) && len == 1 && 1404 name[0] == '.') { 1405 /* 1406 * Just return the current dir's fh. 1407 */ 1408 np = VTONFS(dvp); 1409 nfhp = malloc(sizeof (struct nfsfh) + 1410 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK); 1411 nfhp->nfh_len = np->n_fhp->nfh_len; 1412 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len); 1413 *nfhpp = nfhp; 1414 return (0); 1415 } 1416 if (NFSHASNFSV4(nmp) && len == 2 && 1417 name[0] == '.' && name[1] == '.') { 1418 lookupp = 1; 1419 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, dvp); 1420 } else { 1421 NFSCL_REQSTART(nd, NFSPROC_LOOKUP, dvp); 1422 (void) nfsm_strtom(nd, name, len); 1423 } 1424 if (nd->nd_flag & ND_NFSV4) { 1425 NFSGETATTR_ATTRBIT(&attrbits); 1426 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1427 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 1428 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1429 (void) nfsrv_putattrbit(nd, &attrbits); 1430 } 1431 error = nfscl_request(nd, dvp, p, cred, stuff); 1432 if (error) 1433 return (error); 1434 if (nd->nd_repstat) { 1435 /* 1436 * When an NFSv4 Lookupp returns ENOENT, it means that 1437 * the lookup is at the root of an fs, so return this dir. 1438 */ 1439 if (nd->nd_repstat == NFSERR_NOENT && lookupp) { 1440 np = VTONFS(dvp); 1441 nfhp = malloc(sizeof (struct nfsfh) + 1442 np->n_fhp->nfh_len, M_NFSFH, M_WAITOK); 1443 nfhp->nfh_len = np->n_fhp->nfh_len; 1444 NFSBCOPY(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len); 1445 *nfhpp = nfhp; 1446 m_freem(nd->nd_mrep); 1447 return (0); 1448 } 1449 if (nd->nd_flag & ND_NFSV3) 1450 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff); 1451 else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 1452 ND_NFSV4) { 1453 /* Load the directory attributes. */ 1454 error = nfsm_loadattr(nd, dnap); 1455 if (error == 0) 1456 *dattrflagp = 1; 1457 } 1458 goto nfsmout; 1459 } 1460 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 1461 /* Load the directory attributes. */ 1462 error = nfsm_loadattr(nd, dnap); 1463 if (error != 0) 1464 goto nfsmout; 1465 *dattrflagp = 1; 1466 /* Skip over the Lookup and GetFH operation status values. */ 1467 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1468 } 1469 error = nfsm_getfh(nd, nfhpp); 1470 if (error) 1471 goto nfsmout; 1472 1473 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1474 if ((nd->nd_flag & ND_NFSV3) && !error) 1475 error = nfscl_postop_attr(nd, dnap, dattrflagp, stuff); 1476 nfsmout: 1477 m_freem(nd->nd_mrep); 1478 if (!error && nd->nd_repstat) 1479 error = nd->nd_repstat; 1480 return (error); 1481 } 1482 1483 /* 1484 * Do a readlink rpc. 1485 */ 1486 APPLESTATIC int 1487 nfsrpc_readlink(vnode_t vp, struct uio *uiop, struct ucred *cred, 1488 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 1489 { 1490 u_int32_t *tl; 1491 struct nfsrv_descript nfsd, *nd = &nfsd; 1492 struct nfsnode *np = VTONFS(vp); 1493 nfsattrbit_t attrbits; 1494 int error, len, cangetattr = 1; 1495 1496 *attrflagp = 0; 1497 NFSCL_REQSTART(nd, NFSPROC_READLINK, vp); 1498 if (nd->nd_flag & ND_NFSV4) { 1499 /* 1500 * And do a Getattr op. 1501 */ 1502 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1503 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1504 NFSGETATTR_ATTRBIT(&attrbits); 1505 (void) nfsrv_putattrbit(nd, &attrbits); 1506 } 1507 error = nfscl_request(nd, vp, p, cred, stuff); 1508 if (error) 1509 return (error); 1510 if (nd->nd_flag & ND_NFSV3) 1511 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1512 if (!nd->nd_repstat && !error) { 1513 NFSM_STRSIZ(len, NFS_MAXPATHLEN); 1514 /* 1515 * This seems weird to me, but must have been added to 1516 * FreeBSD for some reason. The only thing I can think of 1517 * is that there was/is some server that replies with 1518 * more link data than it should? 1519 */ 1520 if (len == NFS_MAXPATHLEN) { 1521 NFSLOCKNODE(np); 1522 if (np->n_size > 0 && np->n_size < NFS_MAXPATHLEN) { 1523 len = np->n_size; 1524 cangetattr = 0; 1525 } 1526 NFSUNLOCKNODE(np); 1527 } 1528 error = nfsm_mbufuio(nd, uiop, len); 1529 if ((nd->nd_flag & ND_NFSV4) && !error && cangetattr) 1530 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1531 } 1532 if (nd->nd_repstat && !error) 1533 error = nd->nd_repstat; 1534 nfsmout: 1535 m_freem(nd->nd_mrep); 1536 return (error); 1537 } 1538 1539 /* 1540 * Read operation. 1541 */ 1542 APPLESTATIC int 1543 nfsrpc_read(vnode_t vp, struct uio *uiop, struct ucred *cred, 1544 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 1545 { 1546 int error, expireret = 0, retrycnt; 1547 u_int32_t clidrev = 0; 1548 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1549 struct nfsnode *np = VTONFS(vp); 1550 struct ucred *newcred; 1551 struct nfsfh *nfhp = NULL; 1552 nfsv4stateid_t stateid; 1553 void *lckp; 1554 1555 if (nmp->nm_clp != NULL) 1556 clidrev = nmp->nm_clp->nfsc_clientidrev; 1557 newcred = cred; 1558 if (NFSHASNFSV4(nmp)) { 1559 nfhp = np->n_fhp; 1560 newcred = NFSNEWCRED(cred); 1561 } 1562 retrycnt = 0; 1563 do { 1564 lckp = NULL; 1565 if (NFSHASNFSV4(nmp)) 1566 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 1567 NFSV4OPEN_ACCESSREAD, 0, newcred, p, &stateid, 1568 &lckp); 1569 error = nfsrpc_readrpc(vp, uiop, newcred, &stateid, p, nap, 1570 attrflagp, stuff); 1571 if (error == NFSERR_OPENMODE) { 1572 NFSLOCKMNT(nmp); 1573 nmp->nm_state |= NFSSTA_OPENMODE; 1574 NFSUNLOCKMNT(nmp); 1575 } 1576 if (error == NFSERR_STALESTATEID) 1577 nfscl_initiate_recovery(nmp->nm_clp); 1578 if (lckp != NULL) 1579 nfscl_lockderef(lckp); 1580 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1581 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1582 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 1583 (void) nfs_catnap(PZERO, error, "nfs_read"); 1584 } else if ((error == NFSERR_EXPIRED || 1585 error == NFSERR_BADSTATEID) && clidrev != 0) { 1586 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1587 } 1588 retrycnt++; 1589 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1590 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1591 error == NFSERR_BADSESSION || 1592 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 1593 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1594 expireret == 0 && clidrev != 0 && retrycnt < 4) || 1595 (error == NFSERR_OPENMODE && retrycnt < 4)); 1596 if (error && retrycnt >= 4) 1597 error = EIO; 1598 if (NFSHASNFSV4(nmp)) 1599 NFSFREECRED(newcred); 1600 return (error); 1601 } 1602 1603 /* 1604 * The actual read RPC. 1605 */ 1606 static int 1607 nfsrpc_readrpc(vnode_t vp, struct uio *uiop, struct ucred *cred, 1608 nfsv4stateid_t *stateidp, NFSPROC_T *p, struct nfsvattr *nap, 1609 int *attrflagp, void *stuff) 1610 { 1611 u_int32_t *tl; 1612 int error = 0, len, retlen, tsiz, eof = 0; 1613 struct nfsrv_descript nfsd; 1614 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1615 struct nfsrv_descript *nd = &nfsd; 1616 int rsize; 1617 off_t tmp_off; 1618 1619 *attrflagp = 0; 1620 tsiz = uiop->uio_resid; 1621 tmp_off = uiop->uio_offset + tsiz; 1622 NFSLOCKMNT(nmp); 1623 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) { 1624 NFSUNLOCKMNT(nmp); 1625 return (EFBIG); 1626 } 1627 rsize = nmp->nm_rsize; 1628 NFSUNLOCKMNT(nmp); 1629 nd->nd_mrep = NULL; 1630 while (tsiz > 0) { 1631 *attrflagp = 0; 1632 len = (tsiz > rsize) ? rsize : tsiz; 1633 NFSCL_REQSTART(nd, NFSPROC_READ, vp); 1634 if (nd->nd_flag & ND_NFSV4) 1635 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 1636 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED * 3); 1637 if (nd->nd_flag & ND_NFSV2) { 1638 *tl++ = txdr_unsigned(uiop->uio_offset); 1639 *tl++ = txdr_unsigned(len); 1640 *tl = 0; 1641 } else { 1642 txdr_hyper(uiop->uio_offset, tl); 1643 *(tl + 2) = txdr_unsigned(len); 1644 } 1645 /* 1646 * Since I can't do a Getattr for NFSv4 for Write, there 1647 * doesn't seem any point in doing one here, either. 1648 * (See the comment in nfsrpc_writerpc() for more info.) 1649 */ 1650 error = nfscl_request(nd, vp, p, cred, stuff); 1651 if (error) 1652 return (error); 1653 if (nd->nd_flag & ND_NFSV3) { 1654 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 1655 } else if (!nd->nd_repstat && (nd->nd_flag & ND_NFSV2)) { 1656 error = nfsm_loadattr(nd, nap); 1657 if (!error) 1658 *attrflagp = 1; 1659 } 1660 if (nd->nd_repstat || error) { 1661 if (!error) 1662 error = nd->nd_repstat; 1663 goto nfsmout; 1664 } 1665 if (nd->nd_flag & ND_NFSV3) { 1666 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1667 eof = fxdr_unsigned(int, *(tl + 1)); 1668 } else if (nd->nd_flag & ND_NFSV4) { 1669 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1670 eof = fxdr_unsigned(int, *tl); 1671 } 1672 NFSM_STRSIZ(retlen, len); 1673 error = nfsm_mbufuio(nd, uiop, retlen); 1674 if (error) 1675 goto nfsmout; 1676 m_freem(nd->nd_mrep); 1677 nd->nd_mrep = NULL; 1678 tsiz -= retlen; 1679 if (!(nd->nd_flag & ND_NFSV2)) { 1680 if (eof || retlen == 0) 1681 tsiz = 0; 1682 } else if (retlen < len) 1683 tsiz = 0; 1684 } 1685 return (0); 1686 nfsmout: 1687 if (nd->nd_mrep != NULL) 1688 m_freem(nd->nd_mrep); 1689 return (error); 1690 } 1691 1692 /* 1693 * nfs write operation 1694 * When called_from_strategy != 0, it should return EIO for an error that 1695 * indicates recovery is in progress, so that the buffer will be left 1696 * dirty and be written back to the server later. If it loops around, 1697 * the recovery thread could get stuck waiting for the buffer and recovery 1698 * will then deadlock. 1699 */ 1700 APPLESTATIC int 1701 nfsrpc_write(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 1702 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 1703 void *stuff, int called_from_strategy) 1704 { 1705 int error, expireret = 0, retrycnt, nostateid; 1706 u_int32_t clidrev = 0; 1707 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1708 struct nfsnode *np = VTONFS(vp); 1709 struct ucred *newcred; 1710 struct nfsfh *nfhp = NULL; 1711 nfsv4stateid_t stateid; 1712 void *lckp; 1713 1714 *must_commit = 0; 1715 if (nmp->nm_clp != NULL) 1716 clidrev = nmp->nm_clp->nfsc_clientidrev; 1717 newcred = cred; 1718 if (NFSHASNFSV4(nmp)) { 1719 newcred = NFSNEWCRED(cred); 1720 nfhp = np->n_fhp; 1721 } 1722 retrycnt = 0; 1723 do { 1724 lckp = NULL; 1725 nostateid = 0; 1726 if (NFSHASNFSV4(nmp)) { 1727 (void)nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 1728 NFSV4OPEN_ACCESSWRITE, 0, newcred, p, &stateid, 1729 &lckp); 1730 if (stateid.other[0] == 0 && stateid.other[1] == 0 && 1731 stateid.other[2] == 0) { 1732 nostateid = 1; 1733 NFSCL_DEBUG(1, "stateid0 in write\n"); 1734 } 1735 } 1736 1737 /* 1738 * If there is no stateid for NFSv4, it means this is an 1739 * extraneous write after close. Basically a poorly 1740 * implemented buffer cache. Just don't do the write. 1741 */ 1742 if (nostateid) 1743 error = 0; 1744 else 1745 error = nfsrpc_writerpc(vp, uiop, iomode, must_commit, 1746 newcred, &stateid, p, nap, attrflagp, stuff); 1747 if (error == NFSERR_STALESTATEID) 1748 nfscl_initiate_recovery(nmp->nm_clp); 1749 if (lckp != NULL) 1750 nfscl_lockderef(lckp); 1751 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 1752 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 1753 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 1754 (void) nfs_catnap(PZERO, error, "nfs_write"); 1755 } else if ((error == NFSERR_EXPIRED || 1756 error == NFSERR_BADSTATEID) && clidrev != 0) { 1757 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 1758 } 1759 retrycnt++; 1760 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 1761 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 1762 error == NFSERR_STALEDONTRECOVER) && called_from_strategy == 0) || 1763 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 1764 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 1765 expireret == 0 && clidrev != 0 && retrycnt < 4)); 1766 if (error != 0 && (retrycnt >= 4 || 1767 ((error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 1768 error == NFSERR_STALEDONTRECOVER) && called_from_strategy != 0))) 1769 error = EIO; 1770 if (NFSHASNFSV4(nmp)) 1771 NFSFREECRED(newcred); 1772 return (error); 1773 } 1774 1775 /* 1776 * The actual write RPC. 1777 */ 1778 static int 1779 nfsrpc_writerpc(vnode_t vp, struct uio *uiop, int *iomode, 1780 int *must_commit, struct ucred *cred, nfsv4stateid_t *stateidp, 1781 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 1782 { 1783 u_int32_t *tl; 1784 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 1785 struct nfsnode *np = VTONFS(vp); 1786 int error = 0, len, tsiz, rlen, commit, committed = NFSWRITE_FILESYNC; 1787 int wccflag = 0, wsize; 1788 int32_t backup; 1789 struct nfsrv_descript nfsd; 1790 struct nfsrv_descript *nd = &nfsd; 1791 nfsattrbit_t attrbits; 1792 off_t tmp_off; 1793 1794 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1")); 1795 *attrflagp = 0; 1796 tsiz = uiop->uio_resid; 1797 tmp_off = uiop->uio_offset + tsiz; 1798 NFSLOCKMNT(nmp); 1799 if (tmp_off > nmp->nm_maxfilesize || tmp_off < uiop->uio_offset) { 1800 NFSUNLOCKMNT(nmp); 1801 return (EFBIG); 1802 } 1803 wsize = nmp->nm_wsize; 1804 NFSUNLOCKMNT(nmp); 1805 nd->nd_mrep = NULL; /* NFSv2 sometimes does a write with */ 1806 nd->nd_repstat = 0; /* uio_resid == 0, so the while is not done */ 1807 while (tsiz > 0) { 1808 *attrflagp = 0; 1809 len = (tsiz > wsize) ? wsize : tsiz; 1810 NFSCL_REQSTART(nd, NFSPROC_WRITE, vp); 1811 if (nd->nd_flag & ND_NFSV4) { 1812 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 1813 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+2*NFSX_UNSIGNED); 1814 txdr_hyper(uiop->uio_offset, tl); 1815 tl += 2; 1816 *tl++ = txdr_unsigned(*iomode); 1817 *tl = txdr_unsigned(len); 1818 } else if (nd->nd_flag & ND_NFSV3) { 1819 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER+3*NFSX_UNSIGNED); 1820 txdr_hyper(uiop->uio_offset, tl); 1821 tl += 2; 1822 *tl++ = txdr_unsigned(len); 1823 *tl++ = txdr_unsigned(*iomode); 1824 *tl = txdr_unsigned(len); 1825 } else { 1826 u_int32_t x; 1827 1828 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1829 /* 1830 * Not sure why someone changed this, since the 1831 * RFC clearly states that "beginoffset" and 1832 * "totalcount" are ignored, but it wouldn't 1833 * surprise me if there's a busted server out there. 1834 */ 1835 /* Set both "begin" and "current" to non-garbage. */ 1836 x = txdr_unsigned((u_int32_t)uiop->uio_offset); 1837 *tl++ = x; /* "begin offset" */ 1838 *tl++ = x; /* "current offset" */ 1839 x = txdr_unsigned(len); 1840 *tl++ = x; /* total to this offset */ 1841 *tl = x; /* size of this write */ 1842 1843 } 1844 nfsm_uiombuf(nd, uiop, len); 1845 /* 1846 * Although it is tempting to do a normal Getattr Op in the 1847 * NFSv4 compound, the result can be a nearly hung client 1848 * system if the Getattr asks for Owner and/or OwnerGroup. 1849 * It occurs when the client can't map either the Owner or 1850 * Owner_group name in the Getattr reply to a uid/gid. When 1851 * there is a cache miss, the kernel does an upcall to the 1852 * nfsuserd. Then, it can try and read the local /etc/passwd 1853 * or /etc/group file. It can then block in getnewbuf(), 1854 * waiting for dirty writes to be pushed to the NFS server. 1855 * The only reason this doesn't result in a complete 1856 * deadlock, is that the upcall times out and allows 1857 * the write to complete. However, progress is so slow 1858 * that it might just as well be deadlocked. 1859 * As such, we get the rest of the attributes, but not 1860 * Owner or Owner_group. 1861 * nb: nfscl_loadattrcache() needs to be told that these 1862 * partial attributes from a write rpc are being 1863 * passed in, via a argument flag. 1864 */ 1865 if (nd->nd_flag & ND_NFSV4) { 1866 NFSWRITEGETATTR_ATTRBIT(&attrbits); 1867 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1868 *tl = txdr_unsigned(NFSV4OP_GETATTR); 1869 (void) nfsrv_putattrbit(nd, &attrbits); 1870 } 1871 error = nfscl_request(nd, vp, p, cred, stuff); 1872 if (error) 1873 return (error); 1874 if (nd->nd_repstat) { 1875 /* 1876 * In case the rpc gets retried, roll 1877 * the uio fileds changed by nfsm_uiombuf() 1878 * back. 1879 */ 1880 uiop->uio_offset -= len; 1881 uiop->uio_resid += len; 1882 uiop->uio_iov->iov_base = 1883 (char *)uiop->uio_iov->iov_base - len; 1884 uiop->uio_iov->iov_len += len; 1885 } 1886 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 1887 error = nfscl_wcc_data(nd, vp, nap, attrflagp, 1888 &wccflag, stuff); 1889 if (error) 1890 goto nfsmout; 1891 } 1892 if (!nd->nd_repstat) { 1893 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 1894 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED 1895 + NFSX_VERF); 1896 rlen = fxdr_unsigned(int, *tl++); 1897 if (rlen == 0) { 1898 error = NFSERR_IO; 1899 goto nfsmout; 1900 } else if (rlen < len) { 1901 backup = len - rlen; 1902 uiop->uio_iov->iov_base = 1903 (char *)uiop->uio_iov->iov_base - 1904 backup; 1905 uiop->uio_iov->iov_len += backup; 1906 uiop->uio_offset -= backup; 1907 uiop->uio_resid += backup; 1908 len = rlen; 1909 } 1910 commit = fxdr_unsigned(int, *tl++); 1911 1912 /* 1913 * Return the lowest commitment level 1914 * obtained by any of the RPCs. 1915 */ 1916 if (committed == NFSWRITE_FILESYNC) 1917 committed = commit; 1918 else if (committed == NFSWRITE_DATASYNC && 1919 commit == NFSWRITE_UNSTABLE) 1920 committed = commit; 1921 NFSLOCKMNT(nmp); 1922 if (!NFSHASWRITEVERF(nmp)) { 1923 NFSBCOPY((caddr_t)tl, 1924 (caddr_t)&nmp->nm_verf[0], 1925 NFSX_VERF); 1926 NFSSETWRITEVERF(nmp); 1927 } else if (NFSBCMP(tl, nmp->nm_verf, 1928 NFSX_VERF)) { 1929 *must_commit = 1; 1930 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 1931 } 1932 NFSUNLOCKMNT(nmp); 1933 } 1934 if (nd->nd_flag & ND_NFSV4) 1935 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1936 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) { 1937 error = nfsm_loadattr(nd, nap); 1938 if (!error) 1939 *attrflagp = NFS_LATTR_NOSHRINK; 1940 } 1941 } else { 1942 error = nd->nd_repstat; 1943 } 1944 if (error) 1945 goto nfsmout; 1946 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4)); 1947 m_freem(nd->nd_mrep); 1948 nd->nd_mrep = NULL; 1949 tsiz -= len; 1950 } 1951 nfsmout: 1952 if (nd->nd_mrep != NULL) 1953 m_freem(nd->nd_mrep); 1954 *iomode = committed; 1955 if (nd->nd_repstat && !error) 1956 error = nd->nd_repstat; 1957 return (error); 1958 } 1959 1960 /* 1961 * nfs mknod rpc 1962 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1963 * mode set to specify the file type and the size field for rdev. 1964 */ 1965 APPLESTATIC int 1966 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap, 1967 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p, 1968 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 1969 int *attrflagp, int *dattrflagp, void *dstuff) 1970 { 1971 u_int32_t *tl; 1972 int error = 0; 1973 struct nfsrv_descript nfsd, *nd = &nfsd; 1974 nfsattrbit_t attrbits; 1975 1976 *nfhpp = NULL; 1977 *attrflagp = 0; 1978 *dattrflagp = 0; 1979 if (namelen > NFS_MAXNAMLEN) 1980 return (ENAMETOOLONG); 1981 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp); 1982 if (nd->nd_flag & ND_NFSV4) { 1983 if (vtyp == VBLK || vtyp == VCHR) { 1984 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1985 *tl++ = vtonfsv34_type(vtyp); 1986 *tl++ = txdr_unsigned(NFSMAJOR(rdev)); 1987 *tl = txdr_unsigned(NFSMINOR(rdev)); 1988 } else { 1989 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1990 *tl = vtonfsv34_type(vtyp); 1991 } 1992 } 1993 (void) nfsm_strtom(nd, name, namelen); 1994 if (nd->nd_flag & ND_NFSV3) { 1995 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1996 *tl = vtonfsv34_type(vtyp); 1997 } 1998 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 1999 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2000 if ((nd->nd_flag & ND_NFSV3) && 2001 (vtyp == VCHR || vtyp == VBLK)) { 2002 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2003 *tl++ = txdr_unsigned(NFSMAJOR(rdev)); 2004 *tl = txdr_unsigned(NFSMINOR(rdev)); 2005 } 2006 if (nd->nd_flag & ND_NFSV4) { 2007 NFSGETATTR_ATTRBIT(&attrbits); 2008 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2009 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2010 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2011 (void) nfsrv_putattrbit(nd, &attrbits); 2012 } 2013 if (nd->nd_flag & ND_NFSV2) 2014 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev); 2015 error = nfscl_request(nd, dvp, p, cred, dstuff); 2016 if (error) 2017 return (error); 2018 if (nd->nd_flag & ND_NFSV4) 2019 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2020 if (!nd->nd_repstat) { 2021 if (nd->nd_flag & ND_NFSV4) { 2022 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2023 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2024 if (error) 2025 goto nfsmout; 2026 } 2027 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2028 if (error) 2029 goto nfsmout; 2030 } 2031 if (nd->nd_flag & ND_NFSV3) 2032 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2033 if (!error && nd->nd_repstat) 2034 error = nd->nd_repstat; 2035 nfsmout: 2036 m_freem(nd->nd_mrep); 2037 return (error); 2038 } 2039 2040 /* 2041 * nfs file create call 2042 * Mostly just call the approriate routine. (I separated out v4, so that 2043 * error recovery wouldn't be as difficult.) 2044 */ 2045 APPLESTATIC int 2046 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2047 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p, 2048 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 2049 int *attrflagp, int *dattrflagp, void *dstuff) 2050 { 2051 int error = 0, newone, expireret = 0, retrycnt, unlocked; 2052 struct nfsclowner *owp; 2053 struct nfscldeleg *dp; 2054 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp)); 2055 u_int32_t clidrev; 2056 2057 if (NFSHASNFSV4(nmp)) { 2058 retrycnt = 0; 2059 do { 2060 dp = NULL; 2061 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE | 2062 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone, 2063 NULL, 1); 2064 if (error) 2065 return (error); 2066 if (nmp->nm_clp != NULL) 2067 clidrev = nmp->nm_clp->nfsc_clientidrev; 2068 else 2069 clidrev = 0; 2070 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || 2071 nfs_numnfscbd == 0 || retrycnt > 0) 2072 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, 2073 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp, 2074 attrflagp, dattrflagp, dstuff, &unlocked); 2075 else 2076 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap, 2077 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp, 2078 attrflagp, dattrflagp, dstuff, &unlocked); 2079 /* 2080 * There is no need to invalidate cached attributes here, 2081 * since new post-delegation issue attributes are always 2082 * returned by nfsrpc_createv4() and these will update the 2083 * attribute cache. 2084 */ 2085 if (dp != NULL) 2086 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp, 2087 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp); 2088 nfscl_ownerrelease(nmp, owp, error, newone, unlocked); 2089 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 2090 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 2091 error == NFSERR_BADSESSION) { 2092 (void) nfs_catnap(PZERO, error, "nfs_open"); 2093 } else if ((error == NFSERR_EXPIRED || 2094 error == NFSERR_BADSTATEID) && clidrev != 0) { 2095 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 2096 retrycnt++; 2097 } 2098 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 2099 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 2100 error == NFSERR_BADSESSION || 2101 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 2102 expireret == 0 && clidrev != 0 && retrycnt < 4)); 2103 if (error && retrycnt >= 4) 2104 error = EIO; 2105 } else { 2106 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf, 2107 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 2108 dstuff); 2109 } 2110 return (error); 2111 } 2112 2113 /* 2114 * The create rpc for v2 and 3. 2115 */ 2116 static int 2117 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2118 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p, 2119 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 2120 int *attrflagp, int *dattrflagp, void *dstuff) 2121 { 2122 u_int32_t *tl; 2123 int error = 0; 2124 struct nfsrv_descript nfsd, *nd = &nfsd; 2125 2126 *nfhpp = NULL; 2127 *attrflagp = 0; 2128 *dattrflagp = 0; 2129 if (namelen > NFS_MAXNAMLEN) 2130 return (ENAMETOOLONG); 2131 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp); 2132 (void) nfsm_strtom(nd, name, namelen); 2133 if (nd->nd_flag & ND_NFSV3) { 2134 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2135 if (fmode & O_EXCL) { 2136 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE); 2137 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2138 *tl++ = cverf.lval[0]; 2139 *tl = cverf.lval[1]; 2140 } else { 2141 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 2142 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2143 } 2144 } else { 2145 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0); 2146 } 2147 error = nfscl_request(nd, dvp, p, cred, dstuff); 2148 if (error) 2149 return (error); 2150 if (nd->nd_repstat == 0) { 2151 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2152 if (error) 2153 goto nfsmout; 2154 } 2155 if (nd->nd_flag & ND_NFSV3) 2156 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2157 if (nd->nd_repstat != 0 && error == 0) 2158 error = nd->nd_repstat; 2159 nfsmout: 2160 m_freem(nd->nd_mrep); 2161 return (error); 2162 } 2163 2164 static int 2165 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2166 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 2167 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2168 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2169 int *dattrflagp, void *dstuff, int *unlockedp) 2170 { 2171 u_int32_t *tl; 2172 int error = 0, deleg, newone, ret, acesize, limitby; 2173 struct nfsrv_descript nfsd, *nd = &nfsd; 2174 struct nfsclopen *op; 2175 struct nfscldeleg *dp = NULL; 2176 struct nfsnode *np; 2177 struct nfsfh *nfhp; 2178 nfsattrbit_t attrbits; 2179 nfsv4stateid_t stateid; 2180 u_int32_t rflags; 2181 struct nfsmount *nmp; 2182 struct nfsclsession *tsep; 2183 2184 nmp = VFSTONFS(dvp->v_mount); 2185 np = VTONFS(dvp); 2186 *unlockedp = 0; 2187 *nfhpp = NULL; 2188 *dpp = NULL; 2189 *attrflagp = 0; 2190 *dattrflagp = 0; 2191 if (namelen > NFS_MAXNAMLEN) 2192 return (ENAMETOOLONG); 2193 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp); 2194 /* 2195 * For V4, this is actually an Open op. 2196 */ 2197 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2198 *tl++ = txdr_unsigned(owp->nfsow_seqid); 2199 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 2200 NFSV4OPEN_ACCESSREAD); 2201 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE); 2202 tsep = nfsmnt_mdssession(nmp); 2203 *tl++ = tsep->nfsess_clientid.lval[0]; 2204 *tl = tsep->nfsess_clientid.lval[1]; 2205 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 2206 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2207 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE); 2208 if (fmode & O_EXCL) { 2209 if (NFSHASNFSV4N(nmp)) { 2210 if (NFSHASSESSPERSIST(nmp)) { 2211 /* Use GUARDED for persistent sessions. */ 2212 *tl = txdr_unsigned(NFSCREATE_GUARDED); 2213 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2214 } else { 2215 /* Otherwise, use EXCLUSIVE4_1. */ 2216 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41); 2217 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2218 *tl++ = cverf.lval[0]; 2219 *tl = cverf.lval[1]; 2220 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2221 } 2222 } else { 2223 /* NFSv4.0 */ 2224 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE); 2225 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2226 *tl++ = cverf.lval[0]; 2227 *tl = cverf.lval[1]; 2228 } 2229 } else { 2230 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 2231 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2232 } 2233 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2234 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 2235 (void) nfsm_strtom(nd, name, namelen); 2236 /* Get the new file's handle and attributes. */ 2237 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2238 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2239 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2240 NFSGETATTR_ATTRBIT(&attrbits); 2241 (void) nfsrv_putattrbit(nd, &attrbits); 2242 /* Get the directory's post-op attributes. */ 2243 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2244 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2245 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0); 2246 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2247 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2248 (void) nfsrv_putattrbit(nd, &attrbits); 2249 error = nfscl_request(nd, dvp, p, cred, dstuff); 2250 if (error) 2251 return (error); 2252 NFSCL_INCRSEQID(owp->nfsow_seqid, nd); 2253 if (nd->nd_repstat == 0) { 2254 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 2255 6 * NFSX_UNSIGNED); 2256 stateid.seqid = *tl++; 2257 stateid.other[0] = *tl++; 2258 stateid.other[1] = *tl++; 2259 stateid.other[2] = *tl; 2260 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 2261 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2262 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2263 deleg = fxdr_unsigned(int, *tl); 2264 if (deleg == NFSV4OPEN_DELEGATEREAD || 2265 deleg == NFSV4OPEN_DELEGATEWRITE) { 2266 if (!(owp->nfsow_clp->nfsc_flags & 2267 NFSCLFLAGS_FIRSTDELEG)) 2268 owp->nfsow_clp->nfsc_flags |= 2269 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 2270 dp = malloc( 2271 sizeof (struct nfscldeleg) + NFSX_V4FHMAX, 2272 M_NFSCLDELEG, M_WAITOK); 2273 LIST_INIT(&dp->nfsdl_owner); 2274 LIST_INIT(&dp->nfsdl_lock); 2275 dp->nfsdl_clp = owp->nfsow_clp; 2276 newnfs_copyincred(cred, &dp->nfsdl_cred); 2277 nfscl_lockinit(&dp->nfsdl_rwlock); 2278 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 2279 NFSX_UNSIGNED); 2280 dp->nfsdl_stateid.seqid = *tl++; 2281 dp->nfsdl_stateid.other[0] = *tl++; 2282 dp->nfsdl_stateid.other[1] = *tl++; 2283 dp->nfsdl_stateid.other[2] = *tl++; 2284 ret = fxdr_unsigned(int, *tl); 2285 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 2286 dp->nfsdl_flags = NFSCLDL_WRITE; 2287 /* 2288 * Indicates how much the file can grow. 2289 */ 2290 NFSM_DISSECT(tl, u_int32_t *, 2291 3 * NFSX_UNSIGNED); 2292 limitby = fxdr_unsigned(int, *tl++); 2293 switch (limitby) { 2294 case NFSV4OPEN_LIMITSIZE: 2295 dp->nfsdl_sizelimit = fxdr_hyper(tl); 2296 break; 2297 case NFSV4OPEN_LIMITBLOCKS: 2298 dp->nfsdl_sizelimit = 2299 fxdr_unsigned(u_int64_t, *tl++); 2300 dp->nfsdl_sizelimit *= 2301 fxdr_unsigned(u_int64_t, *tl); 2302 break; 2303 default: 2304 error = NFSERR_BADXDR; 2305 goto nfsmout; 2306 } 2307 } else { 2308 dp->nfsdl_flags = NFSCLDL_READ; 2309 } 2310 if (ret) 2311 dp->nfsdl_flags |= NFSCLDL_RECALL; 2312 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret, 2313 &acesize, p); 2314 if (error) 2315 goto nfsmout; 2316 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 2317 error = NFSERR_BADXDR; 2318 goto nfsmout; 2319 } 2320 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2321 if (error) 2322 goto nfsmout; 2323 /* Get rid of the PutFH and Getattr status values. */ 2324 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2325 /* Load the directory attributes. */ 2326 error = nfsm_loadattr(nd, dnap); 2327 if (error) 2328 goto nfsmout; 2329 *dattrflagp = 1; 2330 if (dp != NULL && *attrflagp) { 2331 dp->nfsdl_change = nnap->na_filerev; 2332 dp->nfsdl_modtime = nnap->na_mtime; 2333 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 2334 } 2335 /* 2336 * We can now complete the Open state. 2337 */ 2338 nfhp = *nfhpp; 2339 if (dp != NULL) { 2340 dp->nfsdl_fhlen = nfhp->nfh_len; 2341 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len); 2342 } 2343 /* 2344 * Get an Open structure that will be 2345 * attached to the OpenOwner, acquired already. 2346 */ 2347 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 2348 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0, 2349 cred, p, NULL, &op, &newone, NULL, 0); 2350 if (error) 2351 goto nfsmout; 2352 op->nfso_stateid = stateid; 2353 newnfs_copyincred(cred, &op->nfso_cred); 2354 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) { 2355 do { 2356 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh, 2357 nfhp->nfh_len, op, cred, p); 2358 if (ret == NFSERR_DELAY) 2359 (void) nfs_catnap(PZERO, ret, "nfs_create"); 2360 } while (ret == NFSERR_DELAY); 2361 error = ret; 2362 } 2363 2364 /* 2365 * If the server is handing out delegations, but we didn't 2366 * get one because an OpenConfirm was required, try the 2367 * Open again, to get a delegation. This is a harmless no-op, 2368 * from a server's point of view. 2369 */ 2370 if ((rflags & NFSV4OPEN_RESULTCONFIRM) && 2371 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) && 2372 !error && dp == NULL) { 2373 do { 2374 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp, 2375 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 2376 nfhp->nfh_fh, nfhp->nfh_len, 2377 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op, 2378 name, namelen, &dp, 0, 0x0, cred, p, 0, 1); 2379 if (ret == NFSERR_DELAY) 2380 (void) nfs_catnap(PZERO, ret, "nfs_crt2"); 2381 } while (ret == NFSERR_DELAY); 2382 if (ret) { 2383 if (dp != NULL) { 2384 free(dp, M_NFSCLDELEG); 2385 dp = NULL; 2386 } 2387 if (ret == NFSERR_STALECLIENTID || 2388 ret == NFSERR_STALEDONTRECOVER || 2389 ret == NFSERR_BADSESSION) 2390 error = ret; 2391 } 2392 } 2393 nfscl_openrelease(nmp, op, error, newone); 2394 *unlockedp = 1; 2395 } 2396 if (nd->nd_repstat != 0 && error == 0) 2397 error = nd->nd_repstat; 2398 if (error == NFSERR_STALECLIENTID) 2399 nfscl_initiate_recovery(owp->nfsow_clp); 2400 nfsmout: 2401 if (!error) 2402 *dpp = dp; 2403 else if (dp != NULL) 2404 free(dp, M_NFSCLDELEG); 2405 m_freem(nd->nd_mrep); 2406 return (error); 2407 } 2408 2409 /* 2410 * Nfs remove rpc 2411 */ 2412 APPLESTATIC int 2413 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp, 2414 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, 2415 void *dstuff) 2416 { 2417 u_int32_t *tl; 2418 struct nfsrv_descript nfsd, *nd = &nfsd; 2419 struct nfsnode *np; 2420 struct nfsmount *nmp; 2421 nfsv4stateid_t dstateid; 2422 int error, ret = 0, i; 2423 2424 *dattrflagp = 0; 2425 if (namelen > NFS_MAXNAMLEN) 2426 return (ENAMETOOLONG); 2427 nmp = VFSTONFS(vnode_mount(dvp)); 2428 tryagain: 2429 if (NFSHASNFSV4(nmp) && ret == 0) { 2430 ret = nfscl_removedeleg(vp, p, &dstateid); 2431 if (ret == 1) { 2432 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp); 2433 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 2434 NFSX_UNSIGNED); 2435 if (NFSHASNFSV4N(nmp)) 2436 *tl++ = 0; 2437 else 2438 *tl++ = dstateid.seqid; 2439 *tl++ = dstateid.other[0]; 2440 *tl++ = dstateid.other[1]; 2441 *tl++ = dstateid.other[2]; 2442 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2443 np = VTONFS(dvp); 2444 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2445 np->n_fhp->nfh_len, 0); 2446 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2447 *tl = txdr_unsigned(NFSV4OP_REMOVE); 2448 } 2449 } else { 2450 ret = 0; 2451 } 2452 if (ret == 0) 2453 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp); 2454 (void) nfsm_strtom(nd, name, namelen); 2455 error = nfscl_request(nd, dvp, p, cred, dstuff); 2456 if (error) 2457 return (error); 2458 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 2459 /* For NFSv4, parse out any Delereturn replies. */ 2460 if (ret > 0 && nd->nd_repstat != 0 && 2461 (nd->nd_flag & ND_NOMOREDATA)) { 2462 /* 2463 * If the Delegreturn failed, try again without 2464 * it. The server will Recall, as required. 2465 */ 2466 m_freem(nd->nd_mrep); 2467 goto tryagain; 2468 } 2469 for (i = 0; i < (ret * 2); i++) { 2470 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 2471 ND_NFSV4) { 2472 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2473 if (*(tl + 1)) 2474 nd->nd_flag |= ND_NOMOREDATA; 2475 } 2476 } 2477 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2478 } 2479 if (nd->nd_repstat && !error) 2480 error = nd->nd_repstat; 2481 nfsmout: 2482 m_freem(nd->nd_mrep); 2483 return (error); 2484 } 2485 2486 /* 2487 * Do an nfs rename rpc. 2488 */ 2489 APPLESTATIC int 2490 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen, 2491 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred, 2492 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap, 2493 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff) 2494 { 2495 u_int32_t *tl; 2496 struct nfsrv_descript nfsd, *nd = &nfsd; 2497 struct nfsmount *nmp; 2498 struct nfsnode *np; 2499 nfsattrbit_t attrbits; 2500 nfsv4stateid_t fdstateid, tdstateid; 2501 int error = 0, ret = 0, gottd = 0, gotfd = 0, i; 2502 2503 *fattrflagp = 0; 2504 *tattrflagp = 0; 2505 nmp = VFSTONFS(vnode_mount(fdvp)); 2506 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN) 2507 return (ENAMETOOLONG); 2508 tryagain: 2509 if (NFSHASNFSV4(nmp) && ret == 0) { 2510 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp, 2511 &tdstateid, &gottd, p); 2512 if (gotfd && gottd) { 2513 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp); 2514 } else if (gotfd) { 2515 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp); 2516 } else if (gottd) { 2517 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp); 2518 } 2519 if (gotfd) { 2520 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 2521 if (NFSHASNFSV4N(nmp)) 2522 *tl++ = 0; 2523 else 2524 *tl++ = fdstateid.seqid; 2525 *tl++ = fdstateid.other[0]; 2526 *tl++ = fdstateid.other[1]; 2527 *tl = fdstateid.other[2]; 2528 if (gottd) { 2529 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2530 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2531 np = VTONFS(tvp); 2532 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2533 np->n_fhp->nfh_len, 0); 2534 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2535 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN); 2536 } 2537 } 2538 if (gottd) { 2539 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 2540 if (NFSHASNFSV4N(nmp)) 2541 *tl++ = 0; 2542 else 2543 *tl++ = tdstateid.seqid; 2544 *tl++ = tdstateid.other[0]; 2545 *tl++ = tdstateid.other[1]; 2546 *tl = tdstateid.other[2]; 2547 } 2548 if (ret > 0) { 2549 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2550 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2551 np = VTONFS(fdvp); 2552 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2553 np->n_fhp->nfh_len, 0); 2554 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2555 *tl = txdr_unsigned(NFSV4OP_SAVEFH); 2556 } 2557 } else { 2558 ret = 0; 2559 } 2560 if (ret == 0) 2561 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp); 2562 if (nd->nd_flag & ND_NFSV4) { 2563 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2564 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2565 NFSWCCATTR_ATTRBIT(&attrbits); 2566 (void) nfsrv_putattrbit(nd, &attrbits); 2567 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2568 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2569 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh, 2570 VTONFS(tdvp)->n_fhp->nfh_len, 0); 2571 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2572 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2573 (void) nfsrv_putattrbit(nd, &attrbits); 2574 nd->nd_flag |= ND_V4WCCATTR; 2575 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2576 *tl = txdr_unsigned(NFSV4OP_RENAME); 2577 } 2578 (void) nfsm_strtom(nd, fnameptr, fnamelen); 2579 if (!(nd->nd_flag & ND_NFSV4)) 2580 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh, 2581 VTONFS(tdvp)->n_fhp->nfh_len, 0); 2582 (void) nfsm_strtom(nd, tnameptr, tnamelen); 2583 error = nfscl_request(nd, fdvp, p, cred, fstuff); 2584 if (error) 2585 return (error); 2586 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 2587 /* For NFSv4, parse out any Delereturn replies. */ 2588 if (ret > 0 && nd->nd_repstat != 0 && 2589 (nd->nd_flag & ND_NOMOREDATA)) { 2590 /* 2591 * If the Delegreturn failed, try again without 2592 * it. The server will Recall, as required. 2593 */ 2594 m_freem(nd->nd_mrep); 2595 goto tryagain; 2596 } 2597 for (i = 0; i < (ret * 2); i++) { 2598 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 2599 ND_NFSV4) { 2600 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2601 if (*(tl + 1)) { 2602 if (i == 0 && ret > 1) { 2603 /* 2604 * If the Delegreturn failed, try again 2605 * without it. The server will Recall, as 2606 * required. 2607 * If ret > 1, the first iteration of this 2608 * loop is the second DelegReturn result. 2609 */ 2610 m_freem(nd->nd_mrep); 2611 goto tryagain; 2612 } else { 2613 nd->nd_flag |= ND_NOMOREDATA; 2614 } 2615 } 2616 } 2617 } 2618 /* Now, the first wcc attribute reply. */ 2619 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 2620 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2621 if (*(tl + 1)) 2622 nd->nd_flag |= ND_NOMOREDATA; 2623 } 2624 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL, 2625 fstuff); 2626 /* and the second wcc attribute reply. */ 2627 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && 2628 !error) { 2629 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2630 if (*(tl + 1)) 2631 nd->nd_flag |= ND_NOMOREDATA; 2632 } 2633 if (!error) 2634 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp, 2635 NULL, tstuff); 2636 } 2637 if (nd->nd_repstat && !error) 2638 error = nd->nd_repstat; 2639 nfsmout: 2640 m_freem(nd->nd_mrep); 2641 return (error); 2642 } 2643 2644 /* 2645 * nfs hard link create rpc 2646 */ 2647 APPLESTATIC int 2648 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen, 2649 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2650 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff) 2651 { 2652 u_int32_t *tl; 2653 struct nfsrv_descript nfsd, *nd = &nfsd; 2654 nfsattrbit_t attrbits; 2655 int error = 0; 2656 2657 *attrflagp = 0; 2658 *dattrflagp = 0; 2659 if (namelen > NFS_MAXNAMLEN) 2660 return (ENAMETOOLONG); 2661 NFSCL_REQSTART(nd, NFSPROC_LINK, vp); 2662 if (nd->nd_flag & ND_NFSV4) { 2663 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2664 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2665 } 2666 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh, 2667 VTONFS(dvp)->n_fhp->nfh_len, 0); 2668 if (nd->nd_flag & ND_NFSV4) { 2669 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2670 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2671 NFSWCCATTR_ATTRBIT(&attrbits); 2672 (void) nfsrv_putattrbit(nd, &attrbits); 2673 nd->nd_flag |= ND_V4WCCATTR; 2674 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2675 *tl = txdr_unsigned(NFSV4OP_LINK); 2676 } 2677 (void) nfsm_strtom(nd, name, namelen); 2678 error = nfscl_request(nd, vp, p, cred, dstuff); 2679 if (error) 2680 return (error); 2681 if (nd->nd_flag & ND_NFSV3) { 2682 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff); 2683 if (!error) 2684 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, 2685 NULL, dstuff); 2686 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 2687 /* 2688 * First, parse out the PutFH and Getattr result. 2689 */ 2690 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2691 if (!(*(tl + 1))) 2692 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2693 if (*(tl + 1)) 2694 nd->nd_flag |= ND_NOMOREDATA; 2695 /* 2696 * Get the pre-op attributes. 2697 */ 2698 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2699 } 2700 if (nd->nd_repstat && !error) 2701 error = nd->nd_repstat; 2702 nfsmout: 2703 m_freem(nd->nd_mrep); 2704 return (error); 2705 } 2706 2707 /* 2708 * nfs symbolic link create rpc 2709 */ 2710 APPLESTATIC int 2711 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, const char *target, 2712 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2713 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2714 int *dattrflagp, void *dstuff) 2715 { 2716 u_int32_t *tl; 2717 struct nfsrv_descript nfsd, *nd = &nfsd; 2718 struct nfsmount *nmp; 2719 int slen, error = 0; 2720 2721 *nfhpp = NULL; 2722 *attrflagp = 0; 2723 *dattrflagp = 0; 2724 nmp = VFSTONFS(vnode_mount(dvp)); 2725 slen = strlen(target); 2726 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN) 2727 return (ENAMETOOLONG); 2728 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp); 2729 if (nd->nd_flag & ND_NFSV4) { 2730 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2731 *tl = txdr_unsigned(NFLNK); 2732 (void) nfsm_strtom(nd, target, slen); 2733 } 2734 (void) nfsm_strtom(nd, name, namelen); 2735 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 2736 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2737 if (!(nd->nd_flag & ND_NFSV4)) 2738 (void) nfsm_strtom(nd, target, slen); 2739 if (nd->nd_flag & ND_NFSV2) 2740 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0); 2741 error = nfscl_request(nd, dvp, p, cred, dstuff); 2742 if (error) 2743 return (error); 2744 if (nd->nd_flag & ND_NFSV4) 2745 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2746 if ((nd->nd_flag & ND_NFSV3) && !error) { 2747 if (!nd->nd_repstat) 2748 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2749 if (!error) 2750 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, 2751 NULL, dstuff); 2752 } 2753 if (nd->nd_repstat && !error) 2754 error = nd->nd_repstat; 2755 m_freem(nd->nd_mrep); 2756 /* 2757 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 2758 * Only do this if vfs.nfs.ignore_eexist is set. 2759 * Never do this for NFSv4.1 or later minor versions, since sessions 2760 * should guarantee "exactly once" RPC semantics. 2761 */ 2762 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) || 2763 nmp->nm_minorvers == 0)) 2764 error = 0; 2765 return (error); 2766 } 2767 2768 /* 2769 * nfs make dir rpc 2770 */ 2771 APPLESTATIC int 2772 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2773 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2774 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2775 int *dattrflagp, void *dstuff) 2776 { 2777 u_int32_t *tl; 2778 struct nfsrv_descript nfsd, *nd = &nfsd; 2779 nfsattrbit_t attrbits; 2780 int error = 0; 2781 struct nfsfh *fhp; 2782 struct nfsmount *nmp; 2783 2784 *nfhpp = NULL; 2785 *attrflagp = 0; 2786 *dattrflagp = 0; 2787 nmp = VFSTONFS(vnode_mount(dvp)); 2788 fhp = VTONFS(dvp)->n_fhp; 2789 if (namelen > NFS_MAXNAMLEN) 2790 return (ENAMETOOLONG); 2791 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp); 2792 if (nd->nd_flag & ND_NFSV4) { 2793 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2794 *tl = txdr_unsigned(NFDIR); 2795 } 2796 (void) nfsm_strtom(nd, name, namelen); 2797 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0); 2798 if (nd->nd_flag & ND_NFSV4) { 2799 NFSGETATTR_ATTRBIT(&attrbits); 2800 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2801 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2802 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2803 (void) nfsrv_putattrbit(nd, &attrbits); 2804 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2805 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2806 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0); 2807 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2808 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2809 (void) nfsrv_putattrbit(nd, &attrbits); 2810 } 2811 error = nfscl_request(nd, dvp, p, cred, dstuff); 2812 if (error) 2813 return (error); 2814 if (nd->nd_flag & ND_NFSV4) 2815 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2816 if (!nd->nd_repstat && !error) { 2817 if (nd->nd_flag & ND_NFSV4) { 2818 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2819 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2820 } 2821 if (!error) 2822 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2823 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) { 2824 /* Get rid of the PutFH and Getattr status values. */ 2825 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2826 /* Load the directory attributes. */ 2827 error = nfsm_loadattr(nd, dnap); 2828 if (error == 0) 2829 *dattrflagp = 1; 2830 } 2831 } 2832 if ((nd->nd_flag & ND_NFSV3) && !error) 2833 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2834 if (nd->nd_repstat && !error) 2835 error = nd->nd_repstat; 2836 nfsmout: 2837 m_freem(nd->nd_mrep); 2838 /* 2839 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 2840 * Only do this if vfs.nfs.ignore_eexist is set. 2841 * Never do this for NFSv4.1 or later minor versions, since sessions 2842 * should guarantee "exactly once" RPC semantics. 2843 */ 2844 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) || 2845 nmp->nm_minorvers == 0)) 2846 error = 0; 2847 return (error); 2848 } 2849 2850 /* 2851 * nfs remove directory call 2852 */ 2853 APPLESTATIC int 2854 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred, 2855 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff) 2856 { 2857 struct nfsrv_descript nfsd, *nd = &nfsd; 2858 int error = 0; 2859 2860 *dattrflagp = 0; 2861 if (namelen > NFS_MAXNAMLEN) 2862 return (ENAMETOOLONG); 2863 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp); 2864 (void) nfsm_strtom(nd, name, namelen); 2865 error = nfscl_request(nd, dvp, p, cred, dstuff); 2866 if (error) 2867 return (error); 2868 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 2869 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2870 if (nd->nd_repstat && !error) 2871 error = nd->nd_repstat; 2872 m_freem(nd->nd_mrep); 2873 /* 2874 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 2875 */ 2876 if (error == ENOENT) 2877 error = 0; 2878 return (error); 2879 } 2880 2881 /* 2882 * Readdir rpc. 2883 * Always returns with either uio_resid unchanged, if you are at the 2884 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks 2885 * filled in. 2886 * I felt this would allow caching of directory blocks more easily 2887 * than returning a pertially filled block. 2888 * Directory offset cookies: 2889 * Oh my, what to do with them... 2890 * I can think of three ways to deal with them: 2891 * 1 - have the layer above these RPCs maintain a map between logical 2892 * directory byte offsets and the NFS directory offset cookies 2893 * 2 - pass the opaque directory offset cookies up into userland 2894 * and let the libc functions deal with them, via the system call 2895 * 3 - return them to userland in the "struct dirent", so future versions 2896 * of libc can use them and do whatever is necessary to make things work 2897 * above these rpc calls, in the meantime 2898 * For now, I do #3 by "hiding" the directory offset cookies after the 2899 * d_name field in struct dirent. This is space inside d_reclen that 2900 * will be ignored by anything that doesn't know about them. 2901 * The directory offset cookies are filled in as the last 8 bytes of 2902 * each directory entry, after d_name. Someday, the userland libc 2903 * functions may be able to use these. In the meantime, it satisfies 2904 * OpenBSD's requirements for cookies being returned. 2905 * If expects the directory offset cookie for the read to be in uio_offset 2906 * and returns the one for the next entry after this directory block in 2907 * there, as well. 2908 */ 2909 APPLESTATIC int 2910 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep, 2911 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 2912 int *eofp, void *stuff) 2913 { 2914 int len, left; 2915 struct dirent *dp = NULL; 2916 u_int32_t *tl; 2917 nfsquad_t cookie, ncookie; 2918 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 2919 struct nfsnode *dnp = VTONFS(vp); 2920 struct nfsvattr nfsva; 2921 struct nfsrv_descript nfsd, *nd = &nfsd; 2922 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2923 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0; 2924 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX; 2925 char *cp; 2926 nfsattrbit_t attrbits, dattrbits; 2927 u_int32_t rderr, *tl2 = NULL; 2928 size_t tresid; 2929 2930 KASSERT(uiop->uio_iovcnt == 1 && 2931 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0, 2932 ("nfs readdirrpc bad uio")); 2933 ncookie.lval[0] = ncookie.lval[1] = 0; 2934 /* 2935 * There is no point in reading a lot more than uio_resid, however 2936 * adding one additional DIRBLKSIZ makes sense. Since uio_resid 2937 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this 2938 * will never make readsize > nm_readdirsize. 2939 */ 2940 readsize = nmp->nm_readdirsize; 2941 if (readsize > uiop->uio_resid) 2942 readsize = uiop->uio_resid + DIRBLKSIZ; 2943 2944 *attrflagp = 0; 2945 if (eofp) 2946 *eofp = 0; 2947 tresid = uiop->uio_resid; 2948 cookie.lval[0] = cookiep->nfsuquad[0]; 2949 cookie.lval[1] = cookiep->nfsuquad[1]; 2950 nd->nd_mrep = NULL; 2951 2952 /* 2953 * For NFSv4, first create the "." and ".." entries. 2954 */ 2955 if (NFSHASNFSV4(nmp)) { 2956 reqsize = 6 * NFSX_UNSIGNED; 2957 NFSGETATTR_ATTRBIT(&dattrbits); 2958 NFSZERO_ATTRBIT(&attrbits); 2959 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID); 2960 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE); 2961 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr, 2962 NFSATTRBIT_MOUNTEDONFILEID)) { 2963 NFSSETBIT_ATTRBIT(&attrbits, 2964 NFSATTRBIT_MOUNTEDONFILEID); 2965 gotmnton = 1; 2966 } else { 2967 /* 2968 * Must fake it. Use the fileno, except when the 2969 * fsid is != to that of the directory. For that 2970 * case, generate a fake fileno that is not the same. 2971 */ 2972 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID); 2973 gotmnton = 0; 2974 } 2975 2976 /* 2977 * Joy, oh joy. For V4 we get to hand craft '.' and '..'. 2978 */ 2979 if (uiop->uio_offset == 0) { 2980 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp); 2981 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2982 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2983 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2984 (void) nfsrv_putattrbit(nd, &attrbits); 2985 error = nfscl_request(nd, vp, p, cred, stuff); 2986 if (error) 2987 return (error); 2988 dotfileid = 0; /* Fake out the compiler. */ 2989 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 2990 error = nfsm_loadattr(nd, &nfsva); 2991 if (error != 0) 2992 goto nfsmout; 2993 dotfileid = nfsva.na_fileid; 2994 } 2995 if (nd->nd_repstat == 0) { 2996 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2997 len = fxdr_unsigned(int, *(tl + 4)); 2998 if (len > 0 && len <= NFSX_V4FHMAX) 2999 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3000 else 3001 error = EPERM; 3002 if (!error) { 3003 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3004 nfsva.na_mntonfileno = UINT64_MAX; 3005 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 3006 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3007 NULL, NULL, NULL, p, cred); 3008 if (error) { 3009 dotdotfileid = dotfileid; 3010 } else if (gotmnton) { 3011 if (nfsva.na_mntonfileno != UINT64_MAX) 3012 dotdotfileid = nfsva.na_mntonfileno; 3013 else 3014 dotdotfileid = nfsva.na_fileid; 3015 } else if (nfsva.na_filesid[0] == 3016 dnp->n_vattr.na_filesid[0] && 3017 nfsva.na_filesid[1] == 3018 dnp->n_vattr.na_filesid[1]) { 3019 dotdotfileid = nfsva.na_fileid; 3020 } else { 3021 do { 3022 fakefileno--; 3023 } while (fakefileno == 3024 nfsva.na_fileid); 3025 dotdotfileid = fakefileno; 3026 } 3027 } 3028 } else if (nd->nd_repstat == NFSERR_NOENT) { 3029 /* 3030 * Lookupp returns NFSERR_NOENT when we are 3031 * at the root, so just use the current dir. 3032 */ 3033 nd->nd_repstat = 0; 3034 dotdotfileid = dotfileid; 3035 } else { 3036 error = nd->nd_repstat; 3037 } 3038 m_freem(nd->nd_mrep); 3039 if (error) 3040 return (error); 3041 nd->nd_mrep = NULL; 3042 dp = (struct dirent *)uiop->uio_iov->iov_base; 3043 dp->d_pad0 = dp->d_pad1 = 0; 3044 dp->d_off = 0; 3045 dp->d_type = DT_DIR; 3046 dp->d_fileno = dotfileid; 3047 dp->d_namlen = 1; 3048 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */ 3049 dp->d_name[0] = '.'; 3050 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3051 /* 3052 * Just make these offset cookie 0. 3053 */ 3054 tl = (u_int32_t *)&dp->d_name[8]; 3055 *tl++ = 0; 3056 *tl = 0; 3057 blksiz += dp->d_reclen; 3058 uiop->uio_resid -= dp->d_reclen; 3059 uiop->uio_offset += dp->d_reclen; 3060 uiop->uio_iov->iov_base = 3061 (char *)uiop->uio_iov->iov_base + dp->d_reclen; 3062 uiop->uio_iov->iov_len -= dp->d_reclen; 3063 dp = (struct dirent *)uiop->uio_iov->iov_base; 3064 dp->d_pad0 = dp->d_pad1 = 0; 3065 dp->d_off = 0; 3066 dp->d_type = DT_DIR; 3067 dp->d_fileno = dotdotfileid; 3068 dp->d_namlen = 2; 3069 *((uint64_t *)dp->d_name) = 0; 3070 dp->d_name[0] = '.'; 3071 dp->d_name[1] = '.'; 3072 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3073 /* 3074 * Just make these offset cookie 0. 3075 */ 3076 tl = (u_int32_t *)&dp->d_name[8]; 3077 *tl++ = 0; 3078 *tl = 0; 3079 blksiz += dp->d_reclen; 3080 uiop->uio_resid -= dp->d_reclen; 3081 uiop->uio_offset += dp->d_reclen; 3082 uiop->uio_iov->iov_base = 3083 (char *)uiop->uio_iov->iov_base + dp->d_reclen; 3084 uiop->uio_iov->iov_len -= dp->d_reclen; 3085 } 3086 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR); 3087 } else { 3088 reqsize = 5 * NFSX_UNSIGNED; 3089 } 3090 3091 3092 /* 3093 * Loop around doing readdir rpc's of size readsize. 3094 * The stopping criteria is EOF or buffer full. 3095 */ 3096 while (more_dirs && bigenough) { 3097 *attrflagp = 0; 3098 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp); 3099 if (nd->nd_flag & ND_NFSV2) { 3100 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3101 *tl++ = cookie.lval[1]; 3102 *tl = txdr_unsigned(readsize); 3103 } else { 3104 NFSM_BUILD(tl, u_int32_t *, reqsize); 3105 *tl++ = cookie.lval[0]; 3106 *tl++ = cookie.lval[1]; 3107 if (cookie.qval == 0) { 3108 *tl++ = 0; 3109 *tl++ = 0; 3110 } else { 3111 NFSLOCKNODE(dnp); 3112 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 3113 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 3114 NFSUNLOCKNODE(dnp); 3115 } 3116 if (nd->nd_flag & ND_NFSV4) { 3117 *tl++ = txdr_unsigned(readsize); 3118 *tl = txdr_unsigned(readsize); 3119 (void) nfsrv_putattrbit(nd, &attrbits); 3120 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3121 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3122 (void) nfsrv_putattrbit(nd, &dattrbits); 3123 } else { 3124 *tl = txdr_unsigned(readsize); 3125 } 3126 } 3127 error = nfscl_request(nd, vp, p, cred, stuff); 3128 if (error) 3129 return (error); 3130 if (!(nd->nd_flag & ND_NFSV2)) { 3131 if (nd->nd_flag & ND_NFSV3) 3132 error = nfscl_postop_attr(nd, nap, attrflagp, 3133 stuff); 3134 if (!nd->nd_repstat && !error) { 3135 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 3136 NFSLOCKNODE(dnp); 3137 dnp->n_cookieverf.nfsuquad[0] = *tl++; 3138 dnp->n_cookieverf.nfsuquad[1] = *tl; 3139 NFSUNLOCKNODE(dnp); 3140 } 3141 } 3142 if (nd->nd_repstat || error) { 3143 if (!error) 3144 error = nd->nd_repstat; 3145 goto nfsmout; 3146 } 3147 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3148 more_dirs = fxdr_unsigned(int, *tl); 3149 if (!more_dirs) 3150 tryformoredirs = 0; 3151 3152 /* loop through the dir entries, doctoring them to 4bsd form */ 3153 while (more_dirs && bigenough) { 3154 if (nd->nd_flag & ND_NFSV4) { 3155 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3156 ncookie.lval[0] = *tl++; 3157 ncookie.lval[1] = *tl++; 3158 len = fxdr_unsigned(int, *tl); 3159 } else if (nd->nd_flag & ND_NFSV3) { 3160 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3161 nfsva.na_fileid = fxdr_hyper(tl); 3162 tl += 2; 3163 len = fxdr_unsigned(int, *tl); 3164 } else { 3165 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3166 nfsva.na_fileid = fxdr_unsigned(uint64_t, 3167 *tl++); 3168 len = fxdr_unsigned(int, *tl); 3169 } 3170 if (len <= 0 || len > NFS_MAXNAMLEN) { 3171 error = EBADRPC; 3172 goto nfsmout; 3173 } 3174 tlen = roundup2(len, 8); 3175 if (tlen == len) 3176 tlen += 8; /* To ensure null termination. */ 3177 left = DIRBLKSIZ - blksiz; 3178 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) { 3179 NFSBZERO(uiop->uio_iov->iov_base, left); 3180 dp->d_reclen += left; 3181 uiop->uio_iov->iov_base = 3182 (char *)uiop->uio_iov->iov_base + left; 3183 uiop->uio_iov->iov_len -= left; 3184 uiop->uio_resid -= left; 3185 uiop->uio_offset += left; 3186 blksiz = 0; 3187 } 3188 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > 3189 uiop->uio_resid) 3190 bigenough = 0; 3191 if (bigenough) { 3192 dp = (struct dirent *)uiop->uio_iov->iov_base; 3193 dp->d_pad0 = dp->d_pad1 = 0; 3194 dp->d_off = 0; 3195 dp->d_namlen = len; 3196 dp->d_reclen = _GENERIC_DIRLEN(len) + 3197 NFSX_HYPER; 3198 dp->d_type = DT_UNKNOWN; 3199 blksiz += dp->d_reclen; 3200 if (blksiz == DIRBLKSIZ) 3201 blksiz = 0; 3202 uiop->uio_resid -= DIRHDSIZ; 3203 uiop->uio_offset += DIRHDSIZ; 3204 uiop->uio_iov->iov_base = 3205 (char *)uiop->uio_iov->iov_base + DIRHDSIZ; 3206 uiop->uio_iov->iov_len -= DIRHDSIZ; 3207 error = nfsm_mbufuio(nd, uiop, len); 3208 if (error) 3209 goto nfsmout; 3210 cp = uiop->uio_iov->iov_base; 3211 tlen -= len; 3212 NFSBZERO(cp, tlen); 3213 cp += tlen; /* points to cookie storage */ 3214 tl2 = (u_int32_t *)cp; 3215 uiop->uio_iov->iov_base = 3216 (char *)uiop->uio_iov->iov_base + tlen + 3217 NFSX_HYPER; 3218 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER; 3219 uiop->uio_resid -= tlen + NFSX_HYPER; 3220 uiop->uio_offset += (tlen + NFSX_HYPER); 3221 } else { 3222 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3223 if (error) 3224 goto nfsmout; 3225 } 3226 if (nd->nd_flag & ND_NFSV4) { 3227 rderr = 0; 3228 nfsva.na_mntonfileno = UINT64_MAX; 3229 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 3230 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3231 NULL, NULL, &rderr, p, cred); 3232 if (error) 3233 goto nfsmout; 3234 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3235 } else if (nd->nd_flag & ND_NFSV3) { 3236 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3237 ncookie.lval[0] = *tl++; 3238 ncookie.lval[1] = *tl++; 3239 } else { 3240 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3241 ncookie.lval[0] = 0; 3242 ncookie.lval[1] = *tl++; 3243 } 3244 if (bigenough) { 3245 if (nd->nd_flag & ND_NFSV4) { 3246 if (rderr) { 3247 dp->d_fileno = 0; 3248 } else { 3249 if (gotmnton) { 3250 if (nfsva.na_mntonfileno != UINT64_MAX) 3251 dp->d_fileno = nfsva.na_mntonfileno; 3252 else 3253 dp->d_fileno = nfsva.na_fileid; 3254 } else if (nfsva.na_filesid[0] == 3255 dnp->n_vattr.na_filesid[0] && 3256 nfsva.na_filesid[1] == 3257 dnp->n_vattr.na_filesid[1]) { 3258 dp->d_fileno = nfsva.na_fileid; 3259 } else { 3260 do { 3261 fakefileno--; 3262 } while (fakefileno == 3263 nfsva.na_fileid); 3264 dp->d_fileno = fakefileno; 3265 } 3266 dp->d_type = vtonfs_dtype(nfsva.na_type); 3267 } 3268 } else { 3269 dp->d_fileno = nfsva.na_fileid; 3270 } 3271 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 3272 ncookie.lval[0]; 3273 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 3274 ncookie.lval[1]; 3275 } 3276 more_dirs = fxdr_unsigned(int, *tl); 3277 } 3278 /* 3279 * If at end of rpc data, get the eof boolean 3280 */ 3281 if (!more_dirs) { 3282 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3283 eof = fxdr_unsigned(int, *tl); 3284 if (tryformoredirs) 3285 more_dirs = !eof; 3286 if (nd->nd_flag & ND_NFSV4) { 3287 error = nfscl_postop_attr(nd, nap, attrflagp, 3288 stuff); 3289 if (error) 3290 goto nfsmout; 3291 } 3292 } 3293 m_freem(nd->nd_mrep); 3294 nd->nd_mrep = NULL; 3295 } 3296 /* 3297 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 3298 * by increasing d_reclen for the last record. 3299 */ 3300 if (blksiz > 0) { 3301 left = DIRBLKSIZ - blksiz; 3302 NFSBZERO(uiop->uio_iov->iov_base, left); 3303 dp->d_reclen += left; 3304 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 3305 left; 3306 uiop->uio_iov->iov_len -= left; 3307 uiop->uio_resid -= left; 3308 uiop->uio_offset += left; 3309 } 3310 3311 /* 3312 * If returning no data, assume end of file. 3313 * If not bigenough, return not end of file, since you aren't 3314 * returning all the data 3315 * Otherwise, return the eof flag from the server. 3316 */ 3317 if (eofp) { 3318 if (tresid == ((size_t)(uiop->uio_resid))) 3319 *eofp = 1; 3320 else if (!bigenough) 3321 *eofp = 0; 3322 else 3323 *eofp = eof; 3324 } 3325 3326 /* 3327 * Add extra empty records to any remaining DIRBLKSIZ chunks. 3328 */ 3329 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) { 3330 dp = (struct dirent *)uiop->uio_iov->iov_base; 3331 NFSBZERO(dp, DIRBLKSIZ); 3332 dp->d_type = DT_UNKNOWN; 3333 tl = (u_int32_t *)&dp->d_name[4]; 3334 *tl++ = cookie.lval[0]; 3335 *tl = cookie.lval[1]; 3336 dp->d_reclen = DIRBLKSIZ; 3337 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 3338 DIRBLKSIZ; 3339 uiop->uio_iov->iov_len -= DIRBLKSIZ; 3340 uiop->uio_resid -= DIRBLKSIZ; 3341 uiop->uio_offset += DIRBLKSIZ; 3342 } 3343 3344 nfsmout: 3345 if (nd->nd_mrep != NULL) 3346 m_freem(nd->nd_mrep); 3347 return (error); 3348 } 3349 3350 #ifndef APPLE 3351 /* 3352 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir(). 3353 * (Also used for NFS V4 when mount flag set.) 3354 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.) 3355 */ 3356 APPLESTATIC int 3357 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep, 3358 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 3359 int *eofp, void *stuff) 3360 { 3361 int len, left; 3362 struct dirent *dp = NULL; 3363 u_int32_t *tl; 3364 vnode_t newvp = NULLVP; 3365 struct nfsrv_descript nfsd, *nd = &nfsd; 3366 struct nameidata nami, *ndp = &nami; 3367 struct componentname *cnp = &ndp->ni_cnd; 3368 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3369 struct nfsnode *dnp = VTONFS(vp), *np; 3370 struct nfsvattr nfsva; 3371 struct nfsfh *nfhp; 3372 nfsquad_t cookie, ncookie; 3373 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 3374 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0; 3375 int isdotdot = 0, unlocknewvp = 0; 3376 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX; 3377 u_int64_t fileno = 0; 3378 char *cp; 3379 nfsattrbit_t attrbits, dattrbits; 3380 size_t tresid; 3381 u_int32_t *tl2 = NULL, rderr; 3382 struct timespec dctime; 3383 3384 KASSERT(uiop->uio_iovcnt == 1 && 3385 (uiop->uio_resid & (DIRBLKSIZ - 1)) == 0, 3386 ("nfs readdirplusrpc bad uio")); 3387 ncookie.lval[0] = ncookie.lval[1] = 0; 3388 timespecclear(&dctime); 3389 *attrflagp = 0; 3390 if (eofp != NULL) 3391 *eofp = 0; 3392 ndp->ni_dvp = vp; 3393 nd->nd_mrep = NULL; 3394 cookie.lval[0] = cookiep->nfsuquad[0]; 3395 cookie.lval[1] = cookiep->nfsuquad[1]; 3396 tresid = uiop->uio_resid; 3397 3398 /* 3399 * For NFSv4, first create the "." and ".." entries. 3400 */ 3401 if (NFSHASNFSV4(nmp)) { 3402 NFSGETATTR_ATTRBIT(&dattrbits); 3403 NFSZERO_ATTRBIT(&attrbits); 3404 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID); 3405 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr, 3406 NFSATTRBIT_MOUNTEDONFILEID)) { 3407 NFSSETBIT_ATTRBIT(&attrbits, 3408 NFSATTRBIT_MOUNTEDONFILEID); 3409 gotmnton = 1; 3410 } else { 3411 /* 3412 * Must fake it. Use the fileno, except when the 3413 * fsid is != to that of the directory. For that 3414 * case, generate a fake fileno that is not the same. 3415 */ 3416 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID); 3417 gotmnton = 0; 3418 } 3419 3420 /* 3421 * Joy, oh joy. For V4 we get to hand craft '.' and '..'. 3422 */ 3423 if (uiop->uio_offset == 0) { 3424 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp); 3425 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3426 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 3427 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3428 (void) nfsrv_putattrbit(nd, &attrbits); 3429 error = nfscl_request(nd, vp, p, cred, stuff); 3430 if (error) 3431 return (error); 3432 dotfileid = 0; /* Fake out the compiler. */ 3433 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 3434 error = nfsm_loadattr(nd, &nfsva); 3435 if (error != 0) 3436 goto nfsmout; 3437 dctime = nfsva.na_ctime; 3438 dotfileid = nfsva.na_fileid; 3439 } 3440 if (nd->nd_repstat == 0) { 3441 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 3442 len = fxdr_unsigned(int, *(tl + 4)); 3443 if (len > 0 && len <= NFSX_V4FHMAX) 3444 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3445 else 3446 error = EPERM; 3447 if (!error) { 3448 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3449 nfsva.na_mntonfileno = UINT64_MAX; 3450 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 3451 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3452 NULL, NULL, NULL, p, cred); 3453 if (error) { 3454 dotdotfileid = dotfileid; 3455 } else if (gotmnton) { 3456 if (nfsva.na_mntonfileno != UINT64_MAX) 3457 dotdotfileid = nfsva.na_mntonfileno; 3458 else 3459 dotdotfileid = nfsva.na_fileid; 3460 } else if (nfsva.na_filesid[0] == 3461 dnp->n_vattr.na_filesid[0] && 3462 nfsva.na_filesid[1] == 3463 dnp->n_vattr.na_filesid[1]) { 3464 dotdotfileid = nfsva.na_fileid; 3465 } else { 3466 do { 3467 fakefileno--; 3468 } while (fakefileno == 3469 nfsva.na_fileid); 3470 dotdotfileid = fakefileno; 3471 } 3472 } 3473 } else if (nd->nd_repstat == NFSERR_NOENT) { 3474 /* 3475 * Lookupp returns NFSERR_NOENT when we are 3476 * at the root, so just use the current dir. 3477 */ 3478 nd->nd_repstat = 0; 3479 dotdotfileid = dotfileid; 3480 } else { 3481 error = nd->nd_repstat; 3482 } 3483 m_freem(nd->nd_mrep); 3484 if (error) 3485 return (error); 3486 nd->nd_mrep = NULL; 3487 dp = (struct dirent *)uiop->uio_iov->iov_base; 3488 dp->d_pad0 = dp->d_pad1 = 0; 3489 dp->d_off = 0; 3490 dp->d_type = DT_DIR; 3491 dp->d_fileno = dotfileid; 3492 dp->d_namlen = 1; 3493 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */ 3494 dp->d_name[0] = '.'; 3495 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3496 /* 3497 * Just make these offset cookie 0. 3498 */ 3499 tl = (u_int32_t *)&dp->d_name[8]; 3500 *tl++ = 0; 3501 *tl = 0; 3502 blksiz += dp->d_reclen; 3503 uiop->uio_resid -= dp->d_reclen; 3504 uiop->uio_offset += dp->d_reclen; 3505 uiop->uio_iov->iov_base = 3506 (char *)uiop->uio_iov->iov_base + dp->d_reclen; 3507 uiop->uio_iov->iov_len -= dp->d_reclen; 3508 dp = (struct dirent *)uiop->uio_iov->iov_base; 3509 dp->d_pad0 = dp->d_pad1 = 0; 3510 dp->d_off = 0; 3511 dp->d_type = DT_DIR; 3512 dp->d_fileno = dotdotfileid; 3513 dp->d_namlen = 2; 3514 *((uint64_t *)dp->d_name) = 0; 3515 dp->d_name[0] = '.'; 3516 dp->d_name[1] = '.'; 3517 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3518 /* 3519 * Just make these offset cookie 0. 3520 */ 3521 tl = (u_int32_t *)&dp->d_name[8]; 3522 *tl++ = 0; 3523 *tl = 0; 3524 blksiz += dp->d_reclen; 3525 uiop->uio_resid -= dp->d_reclen; 3526 uiop->uio_offset += dp->d_reclen; 3527 uiop->uio_iov->iov_base = 3528 (char *)uiop->uio_iov->iov_base + dp->d_reclen; 3529 uiop->uio_iov->iov_len -= dp->d_reclen; 3530 } 3531 NFSREADDIRPLUS_ATTRBIT(&attrbits); 3532 if (gotmnton) 3533 NFSSETBIT_ATTRBIT(&attrbits, 3534 NFSATTRBIT_MOUNTEDONFILEID); 3535 } 3536 3537 /* 3538 * Loop around doing readdir rpc's of size nm_readdirsize. 3539 * The stopping criteria is EOF or buffer full. 3540 */ 3541 while (more_dirs && bigenough) { 3542 *attrflagp = 0; 3543 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp); 3544 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 3545 *tl++ = cookie.lval[0]; 3546 *tl++ = cookie.lval[1]; 3547 if (cookie.qval == 0) { 3548 *tl++ = 0; 3549 *tl++ = 0; 3550 } else { 3551 NFSLOCKNODE(dnp); 3552 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 3553 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 3554 NFSUNLOCKNODE(dnp); 3555 } 3556 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 3557 *tl = txdr_unsigned(nmp->nm_readdirsize); 3558 if (nd->nd_flag & ND_NFSV4) { 3559 (void) nfsrv_putattrbit(nd, &attrbits); 3560 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3561 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3562 (void) nfsrv_putattrbit(nd, &dattrbits); 3563 } 3564 error = nfscl_request(nd, vp, p, cred, stuff); 3565 if (error) 3566 return (error); 3567 if (nd->nd_flag & ND_NFSV3) 3568 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3569 if (nd->nd_repstat || error) { 3570 if (!error) 3571 error = nd->nd_repstat; 3572 goto nfsmout; 3573 } 3574 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0) 3575 dctime = nap->na_ctime; 3576 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3577 NFSLOCKNODE(dnp); 3578 dnp->n_cookieverf.nfsuquad[0] = *tl++; 3579 dnp->n_cookieverf.nfsuquad[1] = *tl++; 3580 NFSUNLOCKNODE(dnp); 3581 more_dirs = fxdr_unsigned(int, *tl); 3582 if (!more_dirs) 3583 tryformoredirs = 0; 3584 3585 /* loop through the dir entries, doctoring them to 4bsd form */ 3586 while (more_dirs && bigenough) { 3587 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3588 if (nd->nd_flag & ND_NFSV4) { 3589 ncookie.lval[0] = *tl++; 3590 ncookie.lval[1] = *tl++; 3591 } else { 3592 fileno = fxdr_hyper(tl); 3593 tl += 2; 3594 } 3595 len = fxdr_unsigned(int, *tl); 3596 if (len <= 0 || len > NFS_MAXNAMLEN) { 3597 error = EBADRPC; 3598 goto nfsmout; 3599 } 3600 tlen = roundup2(len, 8); 3601 if (tlen == len) 3602 tlen += 8; /* To ensure null termination. */ 3603 left = DIRBLKSIZ - blksiz; 3604 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) { 3605 NFSBZERO(uiop->uio_iov->iov_base, left); 3606 dp->d_reclen += left; 3607 uiop->uio_iov->iov_base = 3608 (char *)uiop->uio_iov->iov_base + left; 3609 uiop->uio_iov->iov_len -= left; 3610 uiop->uio_resid -= left; 3611 uiop->uio_offset += left; 3612 blksiz = 0; 3613 } 3614 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > 3615 uiop->uio_resid) 3616 bigenough = 0; 3617 if (bigenough) { 3618 dp = (struct dirent *)uiop->uio_iov->iov_base; 3619 dp->d_pad0 = dp->d_pad1 = 0; 3620 dp->d_off = 0; 3621 dp->d_namlen = len; 3622 dp->d_reclen = _GENERIC_DIRLEN(len) + 3623 NFSX_HYPER; 3624 dp->d_type = DT_UNKNOWN; 3625 blksiz += dp->d_reclen; 3626 if (blksiz == DIRBLKSIZ) 3627 blksiz = 0; 3628 uiop->uio_resid -= DIRHDSIZ; 3629 uiop->uio_offset += DIRHDSIZ; 3630 uiop->uio_iov->iov_base = 3631 (char *)uiop->uio_iov->iov_base + DIRHDSIZ; 3632 uiop->uio_iov->iov_len -= DIRHDSIZ; 3633 cnp->cn_nameptr = uiop->uio_iov->iov_base; 3634 cnp->cn_namelen = len; 3635 NFSCNHASHZERO(cnp); 3636 error = nfsm_mbufuio(nd, uiop, len); 3637 if (error) 3638 goto nfsmout; 3639 cp = uiop->uio_iov->iov_base; 3640 tlen -= len; 3641 NFSBZERO(cp, tlen); 3642 cp += tlen; /* points to cookie storage */ 3643 tl2 = (u_int32_t *)cp; 3644 if (len == 2 && cnp->cn_nameptr[0] == '.' && 3645 cnp->cn_nameptr[1] == '.') 3646 isdotdot = 1; 3647 else 3648 isdotdot = 0; 3649 uiop->uio_iov->iov_base = 3650 (char *)uiop->uio_iov->iov_base + tlen + 3651 NFSX_HYPER; 3652 uiop->uio_iov->iov_len -= tlen + NFSX_HYPER; 3653 uiop->uio_resid -= tlen + NFSX_HYPER; 3654 uiop->uio_offset += (tlen + NFSX_HYPER); 3655 } else { 3656 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3657 if (error) 3658 goto nfsmout; 3659 } 3660 nfhp = NULL; 3661 if (nd->nd_flag & ND_NFSV3) { 3662 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3663 ncookie.lval[0] = *tl++; 3664 ncookie.lval[1] = *tl++; 3665 attrflag = fxdr_unsigned(int, *tl); 3666 if (attrflag) { 3667 error = nfsm_loadattr(nd, &nfsva); 3668 if (error) 3669 goto nfsmout; 3670 } 3671 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED); 3672 if (*tl) { 3673 error = nfsm_getfh(nd, &nfhp); 3674 if (error) 3675 goto nfsmout; 3676 } 3677 if (!attrflag && nfhp != NULL) { 3678 free(nfhp, M_NFSFH); 3679 nfhp = NULL; 3680 } 3681 } else { 3682 rderr = 0; 3683 nfsva.na_mntonfileno = 0xffffffff; 3684 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp, 3685 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3686 NULL, NULL, &rderr, p, cred); 3687 if (error) 3688 goto nfsmout; 3689 } 3690 3691 if (bigenough) { 3692 if (nd->nd_flag & ND_NFSV4) { 3693 if (rderr) { 3694 dp->d_fileno = 0; 3695 } else if (gotmnton) { 3696 if (nfsva.na_mntonfileno != 0xffffffff) 3697 dp->d_fileno = nfsva.na_mntonfileno; 3698 else 3699 dp->d_fileno = nfsva.na_fileid; 3700 } else if (nfsva.na_filesid[0] == 3701 dnp->n_vattr.na_filesid[0] && 3702 nfsva.na_filesid[1] == 3703 dnp->n_vattr.na_filesid[1]) { 3704 dp->d_fileno = nfsva.na_fileid; 3705 } else { 3706 do { 3707 fakefileno--; 3708 } while (fakefileno == 3709 nfsva.na_fileid); 3710 dp->d_fileno = fakefileno; 3711 } 3712 } else { 3713 dp->d_fileno = fileno; 3714 } 3715 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 3716 ncookie.lval[0]; 3717 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 3718 ncookie.lval[1]; 3719 3720 if (nfhp != NULL) { 3721 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len, 3722 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) { 3723 VREF(vp); 3724 newvp = vp; 3725 unlocknewvp = 0; 3726 free(nfhp, M_NFSFH); 3727 np = dnp; 3728 } else if (isdotdot != 0) { 3729 /* 3730 * Skip doing a nfscl_nget() call for "..". 3731 * There's a race between acquiring the nfs 3732 * node here and lookups that look for the 3733 * directory being read (in the parent). 3734 * It would try to get a lock on ".." here, 3735 * owning the lock on the directory being 3736 * read. Lookup will hold the lock on ".." 3737 * and try to acquire the lock on the 3738 * directory being read. 3739 * If the directory is unlocked/relocked, 3740 * then there is a LOR with the buflock 3741 * vp is relocked. 3742 */ 3743 free(nfhp, M_NFSFH); 3744 } else { 3745 error = nfscl_nget(vnode_mount(vp), vp, 3746 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE); 3747 if (!error) { 3748 newvp = NFSTOV(np); 3749 unlocknewvp = 1; 3750 } 3751 } 3752 nfhp = NULL; 3753 if (newvp != NULLVP) { 3754 error = nfscl_loadattrcache(&newvp, 3755 &nfsva, NULL, NULL, 0, 0); 3756 if (error) { 3757 if (unlocknewvp) 3758 vput(newvp); 3759 else 3760 vrele(newvp); 3761 goto nfsmout; 3762 } 3763 dp->d_type = 3764 vtonfs_dtype(np->n_vattr.na_type); 3765 ndp->ni_vp = newvp; 3766 NFSCNHASH(cnp, HASHINIT); 3767 if (cnp->cn_namelen <= NCHNAMLEN && 3768 (newvp->v_type != VDIR || 3769 dctime.tv_sec != 0)) { 3770 cache_enter_time(ndp->ni_dvp, 3771 ndp->ni_vp, cnp, 3772 &nfsva.na_ctime, 3773 newvp->v_type != VDIR ? NULL : 3774 &dctime); 3775 } 3776 if (unlocknewvp) 3777 vput(newvp); 3778 else 3779 vrele(newvp); 3780 newvp = NULLVP; 3781 } 3782 } 3783 } else if (nfhp != NULL) { 3784 free(nfhp, M_NFSFH); 3785 } 3786 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3787 more_dirs = fxdr_unsigned(int, *tl); 3788 } 3789 /* 3790 * If at end of rpc data, get the eof boolean 3791 */ 3792 if (!more_dirs) { 3793 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3794 eof = fxdr_unsigned(int, *tl); 3795 if (tryformoredirs) 3796 more_dirs = !eof; 3797 if (nd->nd_flag & ND_NFSV4) { 3798 error = nfscl_postop_attr(nd, nap, attrflagp, 3799 stuff); 3800 if (error) 3801 goto nfsmout; 3802 } 3803 } 3804 m_freem(nd->nd_mrep); 3805 nd->nd_mrep = NULL; 3806 } 3807 /* 3808 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 3809 * by increasing d_reclen for the last record. 3810 */ 3811 if (blksiz > 0) { 3812 left = DIRBLKSIZ - blksiz; 3813 NFSBZERO(uiop->uio_iov->iov_base, left); 3814 dp->d_reclen += left; 3815 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 3816 left; 3817 uiop->uio_iov->iov_len -= left; 3818 uiop->uio_resid -= left; 3819 uiop->uio_offset += left; 3820 } 3821 3822 /* 3823 * If returning no data, assume end of file. 3824 * If not bigenough, return not end of file, since you aren't 3825 * returning all the data 3826 * Otherwise, return the eof flag from the server. 3827 */ 3828 if (eofp != NULL) { 3829 if (tresid == uiop->uio_resid) 3830 *eofp = 1; 3831 else if (!bigenough) 3832 *eofp = 0; 3833 else 3834 *eofp = eof; 3835 } 3836 3837 /* 3838 * Add extra empty records to any remaining DIRBLKSIZ chunks. 3839 */ 3840 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) { 3841 dp = (struct dirent *)uiop->uio_iov->iov_base; 3842 NFSBZERO(dp, DIRBLKSIZ); 3843 dp->d_type = DT_UNKNOWN; 3844 tl = (u_int32_t *)&dp->d_name[4]; 3845 *tl++ = cookie.lval[0]; 3846 *tl = cookie.lval[1]; 3847 dp->d_reclen = DIRBLKSIZ; 3848 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 3849 DIRBLKSIZ; 3850 uiop->uio_iov->iov_len -= DIRBLKSIZ; 3851 uiop->uio_resid -= DIRBLKSIZ; 3852 uiop->uio_offset += DIRBLKSIZ; 3853 } 3854 3855 nfsmout: 3856 if (nd->nd_mrep != NULL) 3857 m_freem(nd->nd_mrep); 3858 return (error); 3859 } 3860 #endif /* !APPLE */ 3861 3862 /* 3863 * Nfs commit rpc 3864 */ 3865 APPLESTATIC int 3866 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred, 3867 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 3868 { 3869 u_int32_t *tl; 3870 struct nfsrv_descript nfsd, *nd = &nfsd; 3871 nfsattrbit_t attrbits; 3872 int error; 3873 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3874 3875 *attrflagp = 0; 3876 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp); 3877 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3878 txdr_hyper(offset, tl); 3879 tl += 2; 3880 *tl = txdr_unsigned(cnt); 3881 if (nd->nd_flag & ND_NFSV4) { 3882 /* 3883 * And do a Getattr op. 3884 */ 3885 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3886 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3887 NFSGETATTR_ATTRBIT(&attrbits); 3888 (void) nfsrv_putattrbit(nd, &attrbits); 3889 } 3890 error = nfscl_request(nd, vp, p, cred, stuff); 3891 if (error) 3892 return (error); 3893 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff); 3894 if (!error && !nd->nd_repstat) { 3895 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 3896 NFSLOCKMNT(nmp); 3897 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) { 3898 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 3899 nd->nd_repstat = NFSERR_STALEWRITEVERF; 3900 } 3901 NFSUNLOCKMNT(nmp); 3902 if (nd->nd_flag & ND_NFSV4) 3903 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3904 } 3905 nfsmout: 3906 if (!error && nd->nd_repstat) 3907 error = nd->nd_repstat; 3908 m_freem(nd->nd_mrep); 3909 return (error); 3910 } 3911 3912 /* 3913 * NFS byte range lock rpc. 3914 * (Mostly just calls one of the three lower level RPC routines.) 3915 */ 3916 APPLESTATIC int 3917 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl, 3918 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags) 3919 { 3920 struct nfscllockowner *lp; 3921 struct nfsclclient *clp; 3922 struct nfsfh *nfhp; 3923 struct nfsrv_descript nfsd, *nd = &nfsd; 3924 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3925 u_int64_t off, len; 3926 off_t start, end; 3927 u_int32_t clidrev = 0; 3928 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally; 3929 int callcnt, dorpc; 3930 3931 /* 3932 * Convert the flock structure into a start and end and do POSIX 3933 * bounds checking. 3934 */ 3935 switch (fl->l_whence) { 3936 case SEEK_SET: 3937 case SEEK_CUR: 3938 /* 3939 * Caller is responsible for adding any necessary offset 3940 * when SEEK_CUR is used. 3941 */ 3942 start = fl->l_start; 3943 off = fl->l_start; 3944 break; 3945 case SEEK_END: 3946 start = size + fl->l_start; 3947 off = size + fl->l_start; 3948 break; 3949 default: 3950 return (EINVAL); 3951 } 3952 if (start < 0) 3953 return (EINVAL); 3954 if (fl->l_len != 0) { 3955 end = start + fl->l_len - 1; 3956 if (end < start) 3957 return (EINVAL); 3958 } 3959 3960 len = fl->l_len; 3961 if (len == 0) 3962 len = NFS64BITSSET; 3963 retrycnt = 0; 3964 do { 3965 nd->nd_repstat = 0; 3966 if (op == F_GETLK) { 3967 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp); 3968 if (error) 3969 return (error); 3970 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags); 3971 if (!error) { 3972 clidrev = clp->nfsc_clientidrev; 3973 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred, 3974 p, id, flags); 3975 } else if (error == -1) { 3976 error = 0; 3977 } 3978 nfscl_clientrelease(clp); 3979 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) { 3980 /* 3981 * We must loop around for all lockowner cases. 3982 */ 3983 callcnt = 0; 3984 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp); 3985 if (error) 3986 return (error); 3987 do { 3988 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt, 3989 clp, id, flags, &lp, &dorpc); 3990 /* 3991 * If it returns a NULL lp, we're done. 3992 */ 3993 if (lp == NULL) { 3994 if (callcnt == 0) 3995 nfscl_clientrelease(clp); 3996 else 3997 nfscl_releasealllocks(clp, vp, p, id, flags); 3998 return (error); 3999 } 4000 if (nmp->nm_clp != NULL) 4001 clidrev = nmp->nm_clp->nfsc_clientidrev; 4002 else 4003 clidrev = 0; 4004 /* 4005 * If the server doesn't support Posix lock semantics, 4006 * only allow locks on the entire file, since it won't 4007 * handle overlapping byte ranges. 4008 * There might still be a problem when a lock 4009 * upgrade/downgrade (read<->write) occurs, since the 4010 * server "might" expect an unlock first? 4011 */ 4012 if (dorpc && (lp->nfsl_open->nfso_posixlock || 4013 (off == 0 && len == NFS64BITSSET))) { 4014 /* 4015 * Since the lock records will go away, we must 4016 * wait for grace and delay here. 4017 */ 4018 do { 4019 error = nfsrpc_locku(nd, nmp, lp, off, len, 4020 NFSV4LOCKT_READ, cred, p, 0); 4021 if ((nd->nd_repstat == NFSERR_GRACE || 4022 nd->nd_repstat == NFSERR_DELAY) && 4023 error == 0) 4024 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4025 "nfs_advlock"); 4026 } while ((nd->nd_repstat == NFSERR_GRACE || 4027 nd->nd_repstat == NFSERR_DELAY) && error == 0); 4028 } 4029 callcnt++; 4030 } while (error == 0 && nd->nd_repstat == 0); 4031 nfscl_releasealllocks(clp, vp, p, id, flags); 4032 } else if (op == F_SETLK) { 4033 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p, 4034 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally); 4035 if (error || donelocally) { 4036 return (error); 4037 } 4038 if (nmp->nm_clp != NULL) 4039 clidrev = nmp->nm_clp->nfsc_clientidrev; 4040 else 4041 clidrev = 0; 4042 nfhp = VTONFS(vp)->n_fhp; 4043 if (!lp->nfsl_open->nfso_posixlock && 4044 (off != 0 || len != NFS64BITSSET)) { 4045 error = EINVAL; 4046 } else { 4047 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh, 4048 nfhp->nfh_len, lp, newone, reclaim, off, 4049 len, fl->l_type, cred, p, 0); 4050 } 4051 if (!error) 4052 error = nd->nd_repstat; 4053 nfscl_lockrelease(lp, error, newone); 4054 } else { 4055 error = EINVAL; 4056 } 4057 if (!error) 4058 error = nd->nd_repstat; 4059 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 4060 error == NFSERR_STALEDONTRECOVER || 4061 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 4062 error == NFSERR_BADSESSION) { 4063 (void) nfs_catnap(PZERO, error, "nfs_advlock"); 4064 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) 4065 && clidrev != 0) { 4066 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 4067 retrycnt++; 4068 } 4069 } while (error == NFSERR_GRACE || 4070 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 4071 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID || 4072 error == NFSERR_BADSESSION || 4073 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 4074 expireret == 0 && clidrev != 0 && retrycnt < 4)); 4075 if (error && retrycnt >= 4) 4076 error = EIO; 4077 return (error); 4078 } 4079 4080 /* 4081 * The lower level routine for the LockT case. 4082 */ 4083 APPLESTATIC int 4084 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp, 4085 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl, 4086 struct ucred *cred, NFSPROC_T *p, void *id, int flags) 4087 { 4088 u_int32_t *tl; 4089 int error, type, size; 4090 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4091 struct nfsnode *np; 4092 struct nfsmount *nmp; 4093 struct nfsclsession *tsep; 4094 4095 nmp = VFSTONFS(vp->v_mount); 4096 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp); 4097 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 4098 if (fl->l_type == F_RDLCK) 4099 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 4100 else 4101 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 4102 txdr_hyper(off, tl); 4103 tl += 2; 4104 txdr_hyper(len, tl); 4105 tl += 2; 4106 tsep = nfsmnt_mdssession(nmp); 4107 *tl++ = tsep->nfsess_clientid.lval[0]; 4108 *tl = tsep->nfsess_clientid.lval[1]; 4109 nfscl_filllockowner(id, own, flags); 4110 np = VTONFS(vp); 4111 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN], 4112 np->n_fhp->nfh_len); 4113 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len); 4114 error = nfscl_request(nd, vp, p, cred, NULL); 4115 if (error) 4116 return (error); 4117 if (nd->nd_repstat == 0) { 4118 fl->l_type = F_UNLCK; 4119 } else if (nd->nd_repstat == NFSERR_DENIED) { 4120 nd->nd_repstat = 0; 4121 fl->l_whence = SEEK_SET; 4122 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 4123 fl->l_start = fxdr_hyper(tl); 4124 tl += 2; 4125 len = fxdr_hyper(tl); 4126 tl += 2; 4127 if (len == NFS64BITSSET) 4128 fl->l_len = 0; 4129 else 4130 fl->l_len = len; 4131 type = fxdr_unsigned(int, *tl++); 4132 if (type == NFSV4LOCKT_WRITE) 4133 fl->l_type = F_WRLCK; 4134 else 4135 fl->l_type = F_RDLCK; 4136 /* 4137 * XXX For now, I have no idea what to do with the 4138 * conflicting lock_owner, so I'll just set the pid == 0 4139 * and skip over the lock_owner. 4140 */ 4141 fl->l_pid = (pid_t)0; 4142 tl += 2; 4143 size = fxdr_unsigned(int, *tl); 4144 if (size < 0 || size > NFSV4_OPAQUELIMIT) 4145 error = EBADRPC; 4146 if (!error) 4147 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 4148 } else if (nd->nd_repstat == NFSERR_STALECLIENTID) 4149 nfscl_initiate_recovery(clp); 4150 nfsmout: 4151 m_freem(nd->nd_mrep); 4152 return (error); 4153 } 4154 4155 /* 4156 * Lower level function that performs the LockU RPC. 4157 */ 4158 static int 4159 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp, 4160 struct nfscllockowner *lp, u_int64_t off, u_int64_t len, 4161 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred) 4162 { 4163 u_int32_t *tl; 4164 int error; 4165 4166 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh, 4167 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0); 4168 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED); 4169 *tl++ = txdr_unsigned(type); 4170 *tl = txdr_unsigned(lp->nfsl_seqid); 4171 if (nfstest_outofseq && 4172 (arc4random() % nfstest_outofseq) == 0) 4173 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 4174 tl++; 4175 if (NFSHASNFSV4N(nmp)) 4176 *tl++ = 0; 4177 else 4178 *tl++ = lp->nfsl_stateid.seqid; 4179 *tl++ = lp->nfsl_stateid.other[0]; 4180 *tl++ = lp->nfsl_stateid.other[1]; 4181 *tl++ = lp->nfsl_stateid.other[2]; 4182 txdr_hyper(off, tl); 4183 tl += 2; 4184 txdr_hyper(len, tl); 4185 if (syscred) 4186 nd->nd_flag |= ND_USEGSSNAME; 4187 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4188 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4189 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 4190 if (error) 4191 return (error); 4192 if (nd->nd_repstat == 0) { 4193 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 4194 lp->nfsl_stateid.seqid = *tl++; 4195 lp->nfsl_stateid.other[0] = *tl++; 4196 lp->nfsl_stateid.other[1] = *tl++; 4197 lp->nfsl_stateid.other[2] = *tl; 4198 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 4199 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 4200 nfsmout: 4201 m_freem(nd->nd_mrep); 4202 return (error); 4203 } 4204 4205 /* 4206 * The actual Lock RPC. 4207 */ 4208 APPLESTATIC int 4209 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp, 4210 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone, 4211 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred, 4212 NFSPROC_T *p, int syscred) 4213 { 4214 u_int32_t *tl; 4215 int error, size; 4216 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4217 struct nfsclsession *tsep; 4218 4219 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0); 4220 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 4221 if (type == F_RDLCK) 4222 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 4223 else 4224 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 4225 *tl++ = txdr_unsigned(reclaim); 4226 txdr_hyper(off, tl); 4227 tl += 2; 4228 txdr_hyper(len, tl); 4229 tl += 2; 4230 if (newone) { 4231 *tl = newnfs_true; 4232 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 4233 2 * NFSX_UNSIGNED + NFSX_HYPER); 4234 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid); 4235 if (NFSHASNFSV4N(nmp)) 4236 *tl++ = 0; 4237 else 4238 *tl++ = lp->nfsl_open->nfso_stateid.seqid; 4239 *tl++ = lp->nfsl_open->nfso_stateid.other[0]; 4240 *tl++ = lp->nfsl_open->nfso_stateid.other[1]; 4241 *tl++ = lp->nfsl_open->nfso_stateid.other[2]; 4242 *tl++ = txdr_unsigned(lp->nfsl_seqid); 4243 tsep = nfsmnt_mdssession(nmp); 4244 *tl++ = tsep->nfsess_clientid.lval[0]; 4245 *tl = tsep->nfsess_clientid.lval[1]; 4246 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN); 4247 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen); 4248 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen); 4249 } else { 4250 *tl = newnfs_false; 4251 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED); 4252 if (NFSHASNFSV4N(nmp)) 4253 *tl++ = 0; 4254 else 4255 *tl++ = lp->nfsl_stateid.seqid; 4256 *tl++ = lp->nfsl_stateid.other[0]; 4257 *tl++ = lp->nfsl_stateid.other[1]; 4258 *tl++ = lp->nfsl_stateid.other[2]; 4259 *tl = txdr_unsigned(lp->nfsl_seqid); 4260 if (nfstest_outofseq && 4261 (arc4random() % nfstest_outofseq) == 0) 4262 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 4263 } 4264 if (syscred) 4265 nd->nd_flag |= ND_USEGSSNAME; 4266 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 4267 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4268 if (error) 4269 return (error); 4270 if (newone) 4271 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd); 4272 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 4273 if (nd->nd_repstat == 0) { 4274 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 4275 lp->nfsl_stateid.seqid = *tl++; 4276 lp->nfsl_stateid.other[0] = *tl++; 4277 lp->nfsl_stateid.other[1] = *tl++; 4278 lp->nfsl_stateid.other[2] = *tl; 4279 } else if (nd->nd_repstat == NFSERR_DENIED) { 4280 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 4281 size = fxdr_unsigned(int, *(tl + 7)); 4282 if (size < 0 || size > NFSV4_OPAQUELIMIT) 4283 error = EBADRPC; 4284 if (!error) 4285 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 4286 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 4287 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 4288 nfsmout: 4289 m_freem(nd->nd_mrep); 4290 return (error); 4291 } 4292 4293 /* 4294 * nfs statfs rpc 4295 * (always called with the vp for the mount point) 4296 */ 4297 APPLESTATIC int 4298 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp, 4299 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 4300 void *stuff) 4301 { 4302 u_int32_t *tl = NULL; 4303 struct nfsrv_descript nfsd, *nd = &nfsd; 4304 struct nfsmount *nmp; 4305 nfsattrbit_t attrbits; 4306 int error; 4307 4308 *attrflagp = 0; 4309 nmp = VFSTONFS(vnode_mount(vp)); 4310 if (NFSHASNFSV4(nmp)) { 4311 /* 4312 * For V4, you actually do a getattr. 4313 */ 4314 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 4315 NFSSTATFS_GETATTRBIT(&attrbits); 4316 (void) nfsrv_putattrbit(nd, &attrbits); 4317 nd->nd_flag |= ND_USEGSSNAME; 4318 error = nfscl_request(nd, vp, p, cred, stuff); 4319 if (error) 4320 return (error); 4321 if (nd->nd_repstat == 0) { 4322 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 4323 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p, 4324 cred); 4325 if (!error) { 4326 nmp->nm_fsid[0] = nap->na_filesid[0]; 4327 nmp->nm_fsid[1] = nap->na_filesid[1]; 4328 NFSSETHASSETFSID(nmp); 4329 *attrflagp = 1; 4330 } 4331 } else { 4332 error = nd->nd_repstat; 4333 } 4334 if (error) 4335 goto nfsmout; 4336 } else { 4337 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp); 4338 error = nfscl_request(nd, vp, p, cred, stuff); 4339 if (error) 4340 return (error); 4341 if (nd->nd_flag & ND_NFSV3) { 4342 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 4343 if (error) 4344 goto nfsmout; 4345 } 4346 if (nd->nd_repstat) { 4347 error = nd->nd_repstat; 4348 goto nfsmout; 4349 } 4350 NFSM_DISSECT(tl, u_int32_t *, 4351 NFSX_STATFS(nd->nd_flag & ND_NFSV3)); 4352 } 4353 if (NFSHASNFSV3(nmp)) { 4354 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2; 4355 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2; 4356 sbp->sf_abytes = fxdr_hyper(tl); tl += 2; 4357 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2; 4358 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2; 4359 sbp->sf_afiles = fxdr_hyper(tl); tl += 2; 4360 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl); 4361 } else if (NFSHASNFSV4(nmp) == 0) { 4362 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++); 4363 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++); 4364 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++); 4365 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++); 4366 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl); 4367 } 4368 nfsmout: 4369 m_freem(nd->nd_mrep); 4370 return (error); 4371 } 4372 4373 /* 4374 * nfs pathconf rpc 4375 */ 4376 APPLESTATIC int 4377 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc, 4378 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 4379 void *stuff) 4380 { 4381 struct nfsrv_descript nfsd, *nd = &nfsd; 4382 struct nfsmount *nmp; 4383 u_int32_t *tl; 4384 nfsattrbit_t attrbits; 4385 int error; 4386 4387 *attrflagp = 0; 4388 nmp = VFSTONFS(vnode_mount(vp)); 4389 if (NFSHASNFSV4(nmp)) { 4390 /* 4391 * For V4, you actually do a getattr. 4392 */ 4393 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 4394 NFSPATHCONF_GETATTRBIT(&attrbits); 4395 (void) nfsrv_putattrbit(nd, &attrbits); 4396 nd->nd_flag |= ND_USEGSSNAME; 4397 error = nfscl_request(nd, vp, p, cred, stuff); 4398 if (error) 4399 return (error); 4400 if (nd->nd_repstat == 0) { 4401 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 4402 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p, 4403 cred); 4404 if (!error) 4405 *attrflagp = 1; 4406 } else { 4407 error = nd->nd_repstat; 4408 } 4409 } else { 4410 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp); 4411 error = nfscl_request(nd, vp, p, cred, stuff); 4412 if (error) 4413 return (error); 4414 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 4415 if (nd->nd_repstat && !error) 4416 error = nd->nd_repstat; 4417 if (!error) { 4418 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF); 4419 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++); 4420 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++); 4421 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++); 4422 pc->pc_chownrestricted = 4423 fxdr_unsigned(u_int32_t, *tl++); 4424 pc->pc_caseinsensitive = 4425 fxdr_unsigned(u_int32_t, *tl++); 4426 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl); 4427 } 4428 } 4429 nfsmout: 4430 m_freem(nd->nd_mrep); 4431 return (error); 4432 } 4433 4434 /* 4435 * nfs version 3 fsinfo rpc call 4436 */ 4437 APPLESTATIC int 4438 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred, 4439 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 4440 { 4441 u_int32_t *tl; 4442 struct nfsrv_descript nfsd, *nd = &nfsd; 4443 int error; 4444 4445 *attrflagp = 0; 4446 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp); 4447 error = nfscl_request(nd, vp, p, cred, stuff); 4448 if (error) 4449 return (error); 4450 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 4451 if (nd->nd_repstat && !error) 4452 error = nd->nd_repstat; 4453 if (!error) { 4454 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO); 4455 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++); 4456 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++); 4457 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++); 4458 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++); 4459 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++); 4460 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++); 4461 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++); 4462 fsp->fs_maxfilesize = fxdr_hyper(tl); 4463 tl += 2; 4464 fxdr_nfsv3time(tl, &fsp->fs_timedelta); 4465 tl += 2; 4466 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl); 4467 } 4468 nfsmout: 4469 m_freem(nd->nd_mrep); 4470 return (error); 4471 } 4472 4473 /* 4474 * This function performs the Renew RPC. 4475 */ 4476 APPLESTATIC int 4477 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred, 4478 NFSPROC_T *p) 4479 { 4480 u_int32_t *tl; 4481 struct nfsrv_descript nfsd; 4482 struct nfsrv_descript *nd = &nfsd; 4483 struct nfsmount *nmp; 4484 int error; 4485 struct nfssockreq *nrp; 4486 struct nfsclsession *tsep; 4487 4488 nmp = clp->nfsc_nmp; 4489 if (nmp == NULL) 4490 return (0); 4491 if (dsp == NULL) 4492 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0, 4493 0); 4494 else 4495 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, 4496 &dsp->nfsclds_sess, 0, 0); 4497 if (!NFSHASNFSV4N(nmp)) { 4498 /* NFSv4.1 just uses a Sequence Op and not a Renew. */ 4499 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 4500 tsep = nfsmnt_mdssession(nmp); 4501 *tl++ = tsep->nfsess_clientid.lval[0]; 4502 *tl = tsep->nfsess_clientid.lval[1]; 4503 } 4504 nrp = NULL; 4505 if (dsp != NULL) 4506 nrp = dsp->nfsclds_sockp; 4507 if (nrp == NULL) 4508 /* If NULL, use the MDS socket. */ 4509 nrp = &nmp->nm_sockreq; 4510 nd->nd_flag |= ND_USEGSSNAME; 4511 if (dsp == NULL) 4512 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 4513 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4514 else { 4515 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 4516 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess); 4517 if (error == ENXIO) 4518 nfscl_cancelreqs(dsp); 4519 } 4520 if (error) 4521 return (error); 4522 error = nd->nd_repstat; 4523 m_freem(nd->nd_mrep); 4524 return (error); 4525 } 4526 4527 /* 4528 * This function performs the Releaselockowner RPC. 4529 */ 4530 APPLESTATIC int 4531 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp, 4532 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p) 4533 { 4534 struct nfsrv_descript nfsd, *nd = &nfsd; 4535 u_int32_t *tl; 4536 int error; 4537 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4538 struct nfsclsession *tsep; 4539 4540 if (NFSHASNFSV4N(nmp)) { 4541 /* For NFSv4.1, do a FreeStateID. */ 4542 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL, 4543 NULL, 0, 0); 4544 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID); 4545 } else { 4546 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL, 4547 NULL, 0, 0); 4548 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 4549 tsep = nfsmnt_mdssession(nmp); 4550 *tl++ = tsep->nfsess_clientid.lval[0]; 4551 *tl = tsep->nfsess_clientid.lval[1]; 4552 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN); 4553 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen); 4554 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen); 4555 } 4556 nd->nd_flag |= ND_USEGSSNAME; 4557 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4558 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4559 if (error) 4560 return (error); 4561 error = nd->nd_repstat; 4562 m_freem(nd->nd_mrep); 4563 return (error); 4564 } 4565 4566 /* 4567 * This function performs the Compound to get the mount pt FH. 4568 */ 4569 APPLESTATIC int 4570 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred, 4571 NFSPROC_T *p) 4572 { 4573 u_int32_t *tl; 4574 struct nfsrv_descript nfsd; 4575 struct nfsrv_descript *nd = &nfsd; 4576 u_char *cp, *cp2; 4577 int error, cnt, len, setnil; 4578 u_int32_t *opcntp; 4579 4580 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0, 4581 0); 4582 cp = dirpath; 4583 cnt = 0; 4584 do { 4585 setnil = 0; 4586 while (*cp == '/') 4587 cp++; 4588 cp2 = cp; 4589 while (*cp2 != '\0' && *cp2 != '/') 4590 cp2++; 4591 if (*cp2 == '/') { 4592 setnil = 1; 4593 *cp2 = '\0'; 4594 } 4595 if (cp2 != cp) { 4596 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4597 *tl = txdr_unsigned(NFSV4OP_LOOKUP); 4598 nfsm_strtom(nd, cp, strlen(cp)); 4599 cnt++; 4600 } 4601 if (setnil) 4602 *cp2++ = '/'; 4603 cp = cp2; 4604 } while (*cp != '\0'); 4605 if (NFSHASNFSV4N(nmp)) 4606 /* Has a Sequence Op done by nfscl_reqstart(). */ 4607 *opcntp = txdr_unsigned(3 + cnt); 4608 else 4609 *opcntp = txdr_unsigned(2 + cnt); 4610 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4611 *tl = txdr_unsigned(NFSV4OP_GETFH); 4612 nd->nd_flag |= ND_USEGSSNAME; 4613 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4614 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4615 if (error) 4616 return (error); 4617 if (nd->nd_repstat == 0) { 4618 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED); 4619 tl += (2 + 2 * cnt); 4620 if ((len = fxdr_unsigned(int, *tl)) <= 0 || 4621 len > NFSX_FHMAX) { 4622 nd->nd_repstat = NFSERR_BADXDR; 4623 } else { 4624 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len); 4625 if (nd->nd_repstat == 0) 4626 nmp->nm_fhsize = len; 4627 } 4628 } 4629 error = nd->nd_repstat; 4630 nfsmout: 4631 m_freem(nd->nd_mrep); 4632 return (error); 4633 } 4634 4635 /* 4636 * This function performs the Delegreturn RPC. 4637 */ 4638 APPLESTATIC int 4639 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred, 4640 struct nfsmount *nmp, NFSPROC_T *p, int syscred) 4641 { 4642 u_int32_t *tl; 4643 struct nfsrv_descript nfsd; 4644 struct nfsrv_descript *nd = &nfsd; 4645 int error; 4646 4647 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh, 4648 dp->nfsdl_fhlen, NULL, NULL, 0, 0); 4649 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 4650 if (NFSHASNFSV4N(nmp)) 4651 *tl++ = 0; 4652 else 4653 *tl++ = dp->nfsdl_stateid.seqid; 4654 *tl++ = dp->nfsdl_stateid.other[0]; 4655 *tl++ = dp->nfsdl_stateid.other[1]; 4656 *tl = dp->nfsdl_stateid.other[2]; 4657 if (syscred) 4658 nd->nd_flag |= ND_USEGSSNAME; 4659 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4660 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4661 if (error) 4662 return (error); 4663 error = nd->nd_repstat; 4664 m_freem(nd->nd_mrep); 4665 return (error); 4666 } 4667 4668 /* 4669 * nfs getacl call. 4670 */ 4671 APPLESTATIC int 4672 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4673 struct acl *aclp, void *stuff) 4674 { 4675 struct nfsrv_descript nfsd, *nd = &nfsd; 4676 int error; 4677 nfsattrbit_t attrbits; 4678 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4679 4680 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 4681 return (EOPNOTSUPP); 4682 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp); 4683 NFSZERO_ATTRBIT(&attrbits); 4684 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 4685 (void) nfsrv_putattrbit(nd, &attrbits); 4686 error = nfscl_request(nd, vp, p, cred, stuff); 4687 if (error) 4688 return (error); 4689 if (!nd->nd_repstat) 4690 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL, 4691 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred); 4692 else 4693 error = nd->nd_repstat; 4694 m_freem(nd->nd_mrep); 4695 return (error); 4696 } 4697 4698 /* 4699 * nfs setacl call. 4700 */ 4701 APPLESTATIC int 4702 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4703 struct acl *aclp, void *stuff) 4704 { 4705 int error; 4706 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4707 4708 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 4709 return (EOPNOTSUPP); 4710 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff); 4711 return (error); 4712 } 4713 4714 /* 4715 * nfs setacl call. 4716 */ 4717 static int 4718 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4719 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff) 4720 { 4721 struct nfsrv_descript nfsd, *nd = &nfsd; 4722 int error; 4723 nfsattrbit_t attrbits; 4724 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4725 4726 if (!NFSHASNFSV4(nmp)) 4727 return (EOPNOTSUPP); 4728 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp); 4729 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 4730 NFSZERO_ATTRBIT(&attrbits); 4731 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 4732 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0, 4733 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL); 4734 error = nfscl_request(nd, vp, p, cred, stuff); 4735 if (error) 4736 return (error); 4737 /* Don't care about the pre/postop attributes */ 4738 m_freem(nd->nd_mrep); 4739 return (nd->nd_repstat); 4740 } 4741 4742 /* 4743 * Do the NFSv4.1 Exchange ID. 4744 */ 4745 int 4746 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp, 4747 struct nfssockreq *nrp, int minorvers, uint32_t exchflags, 4748 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p) 4749 { 4750 uint32_t *tl, v41flags; 4751 struct nfsrv_descript nfsd; 4752 struct nfsrv_descript *nd = &nfsd; 4753 struct nfsclds *dsp; 4754 struct timespec verstime; 4755 int error, len; 4756 4757 *dspp = NULL; 4758 if (minorvers == 0) 4759 minorvers = nmp->nm_minorvers; 4760 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 4761 NFS_VER4, minorvers); 4762 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 4763 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */ 4764 *tl = txdr_unsigned(clp->nfsc_rev); 4765 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen); 4766 4767 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED); 4768 *tl++ = txdr_unsigned(exchflags); 4769 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); 4770 4771 /* Set the implementation id4 */ 4772 *tl = txdr_unsigned(1); 4773 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org")); 4774 (void) nfsm_strtom(nd, version, strlen(version)); 4775 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME); 4776 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */ 4777 verstime.tv_nsec = 0; 4778 txdr_nfsv4time(&verstime, tl); 4779 nd->nd_flag |= ND_USEGSSNAME; 4780 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 4781 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4782 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error, 4783 (int)nd->nd_repstat); 4784 if (error != 0) 4785 return (error); 4786 if (nd->nd_repstat == 0) { 4787 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER); 4788 len = fxdr_unsigned(int, *(tl + 7)); 4789 if (len < 0 || len > NFSV4_OPAQUELIMIT) { 4790 error = NFSERR_BADXDR; 4791 goto nfsmout; 4792 } 4793 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS, 4794 M_WAITOK | M_ZERO); 4795 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew; 4796 dsp->nfsclds_servownlen = len; 4797 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++; 4798 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++; 4799 dsp->nfsclds_sess.nfsess_sequenceid = 4800 fxdr_unsigned(uint32_t, *tl++); 4801 v41flags = fxdr_unsigned(uint32_t, *tl); 4802 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 && 4803 NFSHASPNFSOPT(nmp)) { 4804 NFSCL_DEBUG(1, "set PNFS\n"); 4805 NFSLOCKMNT(nmp); 4806 nmp->nm_state |= NFSSTA_PNFS; 4807 NFSUNLOCKMNT(nmp); 4808 dsp->nfsclds_flags |= NFSCLDS_MDS; 4809 } 4810 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0) 4811 dsp->nfsclds_flags |= NFSCLDS_DS; 4812 if (minorvers == NFSV42_MINORVERSION) 4813 dsp->nfsclds_flags |= NFSCLDS_MINORV2; 4814 if (len > 0) 4815 nd->nd_repstat = nfsrv_mtostr(nd, 4816 dsp->nfsclds_serverown, len); 4817 if (nd->nd_repstat == 0) { 4818 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 4819 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 4820 NULL, MTX_DEF); 4821 nfscl_initsessionslots(&dsp->nfsclds_sess); 4822 *dspp = dsp; 4823 } else 4824 free(dsp, M_NFSCLDS); 4825 } 4826 error = nd->nd_repstat; 4827 nfsmout: 4828 m_freem(nd->nd_mrep); 4829 return (error); 4830 } 4831 4832 /* 4833 * Do the NFSv4.1 Create Session. 4834 */ 4835 int 4836 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep, 4837 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds, 4838 struct ucred *cred, NFSPROC_T *p) 4839 { 4840 uint32_t crflags, maxval, *tl; 4841 struct nfsrv_descript nfsd; 4842 struct nfsrv_descript *nd = &nfsd; 4843 int error, irdcnt, minorvers; 4844 4845 /* Make sure nm_rsize, nm_wsize is set. */ 4846 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0) 4847 nmp->nm_rsize = NFS_MAXBSIZE; 4848 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0) 4849 nmp->nm_wsize = NFS_MAXBSIZE; 4850 if (dsp == NULL) 4851 minorvers = nmp->nm_minorvers; 4852 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0) 4853 minorvers = NFSV42_MINORVERSION; 4854 else 4855 minorvers = NFSV41_MINORVERSION; 4856 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 4857 NFS_VER4, minorvers); 4858 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 4859 *tl++ = sep->nfsess_clientid.lval[0]; 4860 *tl++ = sep->nfsess_clientid.lval[1]; 4861 *tl++ = txdr_unsigned(sequenceid); 4862 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST); 4863 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0) 4864 crflags |= NFSV4CRSESS_CONNBACKCHAN; 4865 *tl = txdr_unsigned(crflags); 4866 4867 /* Fill in fore channel attributes. */ 4868 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4869 *tl++ = 0; /* Header pad size */ 4870 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >= 4871 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) { 4872 /* 4873 * NFSv4.2 Extended Attribute operations may want to do 4874 * requests/replies that are larger than nm_rsize/nm_wsize. 4875 */ 4876 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR); 4877 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR); 4878 } else { 4879 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR); 4880 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR); 4881 } 4882 *tl++ = txdr_unsigned(4096); /* Max response size cached */ 4883 *tl++ = txdr_unsigned(20); /* Max operations */ 4884 *tl++ = txdr_unsigned(64); /* Max slots */ 4885 *tl = 0; /* No rdma ird */ 4886 4887 /* Fill in back channel attributes. */ 4888 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4889 *tl++ = 0; /* Header pad size */ 4890 *tl++ = txdr_unsigned(10000); /* Max request size */ 4891 *tl++ = txdr_unsigned(10000); /* Max response size */ 4892 *tl++ = txdr_unsigned(4096); /* Max response size cached */ 4893 *tl++ = txdr_unsigned(4); /* Max operations */ 4894 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */ 4895 *tl = 0; /* No rdma ird */ 4896 4897 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED); 4898 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */ 4899 4900 /* Allow AUTH_SYS callbacks as uid, gid == 0. */ 4901 *tl++ = txdr_unsigned(1); /* Auth_sys only */ 4902 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */ 4903 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */ 4904 *tl++ = 0; /* Null machine name */ 4905 *tl++ = 0; /* Uid == 0 */ 4906 *tl++ = 0; /* Gid == 0 */ 4907 *tl = 0; /* No additional gids */ 4908 nd->nd_flag |= ND_USEGSSNAME; 4909 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG, 4910 NFS_VER4, NULL, 1, NULL, NULL); 4911 if (error != 0) 4912 return (error); 4913 if (nd->nd_repstat == 0) { 4914 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 4915 2 * NFSX_UNSIGNED); 4916 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID); 4917 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 4918 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++); 4919 crflags = fxdr_unsigned(uint32_t, *tl); 4920 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) { 4921 NFSLOCKMNT(nmp); 4922 nmp->nm_state |= NFSSTA_SESSPERSIST; 4923 NFSUNLOCKMNT(nmp); 4924 } 4925 4926 /* Get the fore channel slot count. */ 4927 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4928 tl++; /* Skip the header pad size. */ 4929 4930 /* Make sure nm_wsize is small enough. */ 4931 maxval = fxdr_unsigned(uint32_t, *tl++); 4932 while (maxval < nmp->nm_wsize + NFS_MAXXDR) { 4933 if (nmp->nm_wsize > 8096) 4934 nmp->nm_wsize /= 2; 4935 else 4936 break; 4937 } 4938 sep->nfsess_maxreq = maxval; 4939 4940 /* Make sure nm_rsize is small enough. */ 4941 maxval = fxdr_unsigned(uint32_t, *tl++); 4942 while (maxval < nmp->nm_rsize + NFS_MAXXDR) { 4943 if (nmp->nm_rsize > 8096) 4944 nmp->nm_rsize /= 2; 4945 else 4946 break; 4947 } 4948 sep->nfsess_maxresp = maxval; 4949 4950 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++); 4951 tl++; 4952 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++); 4953 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots); 4954 irdcnt = fxdr_unsigned(int, *tl); 4955 if (irdcnt > 0) 4956 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED); 4957 4958 /* and the back channel slot count. */ 4959 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4960 tl += 5; 4961 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl); 4962 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots); 4963 } 4964 error = nd->nd_repstat; 4965 nfsmout: 4966 m_freem(nd->nd_mrep); 4967 return (error); 4968 } 4969 4970 /* 4971 * Do the NFSv4.1 Destroy Session. 4972 */ 4973 int 4974 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp, 4975 struct ucred *cred, NFSPROC_T *p) 4976 { 4977 uint32_t *tl; 4978 struct nfsrv_descript nfsd; 4979 struct nfsrv_descript *nd = &nfsd; 4980 int error; 4981 struct nfsclsession *tsep; 4982 4983 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0, 4984 0); 4985 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID); 4986 tsep = nfsmnt_mdssession(nmp); 4987 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID); 4988 nd->nd_flag |= ND_USEGSSNAME; 4989 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4990 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4991 if (error != 0) 4992 return (error); 4993 error = nd->nd_repstat; 4994 m_freem(nd->nd_mrep); 4995 return (error); 4996 } 4997 4998 /* 4999 * Do the NFSv4.1 Destroy Client. 5000 */ 5001 int 5002 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp, 5003 struct ucred *cred, NFSPROC_T *p) 5004 { 5005 uint32_t *tl; 5006 struct nfsrv_descript nfsd; 5007 struct nfsrv_descript *nd = &nfsd; 5008 int error; 5009 struct nfsclsession *tsep; 5010 5011 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0, 5012 0); 5013 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5014 tsep = nfsmnt_mdssession(nmp); 5015 *tl++ = tsep->nfsess_clientid.lval[0]; 5016 *tl = tsep->nfsess_clientid.lval[1]; 5017 nd->nd_flag |= ND_USEGSSNAME; 5018 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5019 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5020 if (error != 0) 5021 return (error); 5022 error = nd->nd_repstat; 5023 m_freem(nd->nd_mrep); 5024 return (error); 5025 } 5026 5027 /* 5028 * Do the NFSv4.1 LayoutGet. 5029 */ 5030 static int 5031 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode, 5032 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype, 5033 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep, 5034 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p, 5035 void *stuff) 5036 { 5037 struct nfsrv_descript nfsd, *nd = &nfsd; 5038 int error; 5039 5040 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0, 5041 0); 5042 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp, 5043 layouttype, layoutlen, 0); 5044 nd->nd_flag |= ND_USEGSSNAME; 5045 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5046 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5047 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat); 5048 if (error != 0) 5049 return (error); 5050 if (nd->nd_repstat == 0) 5051 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep, 5052 flhp); 5053 if (error == 0 && nd->nd_repstat != 0) 5054 error = nd->nd_repstat; 5055 m_freem(nd->nd_mrep); 5056 return (error); 5057 } 5058 5059 /* 5060 * Do the NFSv4.1 Get Device Info. 5061 */ 5062 int 5063 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype, 5064 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred, 5065 NFSPROC_T *p) 5066 { 5067 uint32_t cnt, *tl, vers, minorvers; 5068 struct nfsrv_descript nfsd; 5069 struct nfsrv_descript *nd = &nfsd; 5070 struct sockaddr_in sin, ssin; 5071 struct sockaddr_in6 sin6, ssin6; 5072 struct nfsclds *dsp = NULL, **dspp, **gotdspp; 5073 struct nfscldevinfo *ndi; 5074 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j; 5075 int stripecnt; 5076 uint8_t stripeindex; 5077 sa_family_t af, safilled; 5078 5079 ssin.sin_port = 0; /* To shut up compiler. */ 5080 ssin.sin_addr.s_addr = 0; /* ditto */ 5081 *ndip = NULL; 5082 ndi = NULL; 5083 gotdspp = NULL; 5084 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0, 5085 0); 5086 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED); 5087 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID); 5088 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 5089 *tl++ = txdr_unsigned(layouttype); 5090 *tl++ = txdr_unsigned(100000); 5091 if (notifybitsp != NULL && *notifybitsp != 0) { 5092 *tl = txdr_unsigned(1); /* One word of bits. */ 5093 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 5094 *tl = txdr_unsigned(*notifybitsp); 5095 } else 5096 *tl = txdr_unsigned(0); 5097 nd->nd_flag |= ND_USEGSSNAME; 5098 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5099 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5100 if (error != 0) 5101 return (error); 5102 if (nd->nd_repstat == 0) { 5103 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5104 if (layouttype != fxdr_unsigned(int, *tl)) 5105 printf("EEK! devinfo layout type not same!\n"); 5106 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) { 5107 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5108 stripecnt = fxdr_unsigned(int, *tl); 5109 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt); 5110 if (stripecnt < 1 || stripecnt > 4096) { 5111 printf("pNFS File layout devinfo stripecnt %d:" 5112 " out of range\n", stripecnt); 5113 error = NFSERR_BADXDR; 5114 goto nfsmout; 5115 } 5116 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * 5117 NFSX_UNSIGNED); 5118 addrcnt = fxdr_unsigned(int, *(tl + stripecnt)); 5119 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt); 5120 if (addrcnt < 1 || addrcnt > 128) { 5121 printf("NFS devinfo addrcnt %d: out of range\n", 5122 addrcnt); 5123 error = NFSERR_BADXDR; 5124 goto nfsmout; 5125 } 5126 5127 /* 5128 * Now we know how many stripe indices and addresses, so 5129 * we can allocate the structure the correct size. 5130 */ 5131 i = (stripecnt * sizeof(uint8_t)) / 5132 sizeof(struct nfsclds *) + 1; 5133 NFSCL_DEBUG(4, "stripeindices=%d\n", i); 5134 ndi = malloc(sizeof(*ndi) + (addrcnt + i) * 5135 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | 5136 M_ZERO); 5137 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, 5138 NFSX_V4DEVICEID); 5139 ndi->nfsdi_refcnt = 0; 5140 ndi->nfsdi_flags = NFSDI_FILELAYOUT; 5141 ndi->nfsdi_stripecnt = stripecnt; 5142 ndi->nfsdi_addrcnt = addrcnt; 5143 /* Fill in the stripe indices. */ 5144 for (i = 0; i < stripecnt; i++) { 5145 stripeindex = fxdr_unsigned(uint8_t, *tl++); 5146 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex); 5147 if (stripeindex >= addrcnt) { 5148 printf("pNFS File Layout devinfo" 5149 " stripeindex %d: too big\n", 5150 (int)stripeindex); 5151 error = NFSERR_BADXDR; 5152 goto nfsmout; 5153 } 5154 nfsfldi_setstripeindex(ndi, i, stripeindex); 5155 } 5156 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 5157 /* For Flex File, we only get one address list. */ 5158 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *), 5159 M_NFSDEVINFO, M_WAITOK | M_ZERO); 5160 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, 5161 NFSX_V4DEVICEID); 5162 ndi->nfsdi_refcnt = 0; 5163 ndi->nfsdi_flags = NFSDI_FLEXFILE; 5164 addrcnt = ndi->nfsdi_addrcnt = 1; 5165 } 5166 5167 /* Now, dissect the server address(es). */ 5168 safilled = AF_UNSPEC; 5169 for (i = 0; i < addrcnt; i++) { 5170 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5171 cnt = fxdr_unsigned(uint32_t, *tl); 5172 if (cnt == 0) { 5173 printf("NFS devinfo 0 len addrlist\n"); 5174 error = NFSERR_BADXDR; 5175 goto nfsmout; 5176 } 5177 dspp = nfsfldi_addr(ndi, i); 5178 safilled = AF_UNSPEC; 5179 for (j = 0; j < cnt; j++) { 5180 error = nfsv4_getipaddr(nd, &sin, &sin6, &af, 5181 &isudp); 5182 if (error != 0 && error != EPERM) { 5183 error = NFSERR_BADXDR; 5184 goto nfsmout; 5185 } 5186 if (error == 0 && isudp == 0) { 5187 /* 5188 * The priority is: 5189 * - Same address family. 5190 * Save the address and dspp, so that 5191 * the connection can be done after 5192 * parsing is complete. 5193 */ 5194 if (safilled == AF_UNSPEC || 5195 (af == nmp->nm_nam->sa_family && 5196 safilled != nmp->nm_nam->sa_family) 5197 ) { 5198 if (af == AF_INET) 5199 ssin = sin; 5200 else 5201 ssin6 = sin6; 5202 safilled = af; 5203 gotdspp = dspp; 5204 } 5205 } 5206 } 5207 } 5208 5209 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */ 5210 gotminor = NFSV41_MINORVERSION; 5211 /* For Flex File, we will take one of the versions to use. */ 5212 if (layouttype == NFSLAYOUT_FLEXFILE) { 5213 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5214 j = fxdr_unsigned(int, *tl); 5215 if (j < 1 || j > NFSDEV_MAXVERS) { 5216 printf("pNFS: too many versions\n"); 5217 error = NFSERR_BADXDR; 5218 goto nfsmout; 5219 } 5220 gotvers = 0; 5221 gotminor = 0; 5222 for (i = 0; i < j; i++) { 5223 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED); 5224 vers = fxdr_unsigned(uint32_t, *tl++); 5225 minorvers = fxdr_unsigned(uint32_t, *tl++); 5226 if (vers == NFS_VER3) 5227 minorvers = 0; 5228 if ((vers == NFS_VER4 && ((minorvers == 5229 NFSV41_MINORVERSION && gotminor == 0) || 5230 minorvers == NFSV42_MINORVERSION)) || 5231 (vers == NFS_VER3 && gotvers == 0)) { 5232 gotvers = vers; 5233 gotminor = minorvers; 5234 /* We'll take this one. */ 5235 ndi->nfsdi_versindex = i; 5236 ndi->nfsdi_vers = vers; 5237 ndi->nfsdi_minorvers = minorvers; 5238 ndi->nfsdi_rsize = fxdr_unsigned( 5239 uint32_t, *tl++); 5240 ndi->nfsdi_wsize = fxdr_unsigned( 5241 uint32_t, *tl++); 5242 if (*tl == newnfs_true) 5243 ndi->nfsdi_flags |= 5244 NFSDI_TIGHTCOUPLED; 5245 else 5246 ndi->nfsdi_flags &= 5247 ~NFSDI_TIGHTCOUPLED; 5248 } 5249 } 5250 if (gotvers == 0) { 5251 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n"); 5252 error = NFSERR_BADXDR; 5253 goto nfsmout; 5254 } 5255 } 5256 5257 /* And the notify bits. */ 5258 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5259 bitcnt = fxdr_unsigned(int, *tl); 5260 if (bitcnt > 0) { 5261 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5262 if (notifybitsp != NULL) 5263 *notifybitsp = 5264 fxdr_unsigned(uint32_t, *tl); 5265 } 5266 if (safilled != AF_UNSPEC) { 5267 KASSERT(ndi != NULL, ("ndi is NULL")); 5268 *ndip = ndi; 5269 } else 5270 error = EPERM; 5271 if (error == 0) { 5272 /* 5273 * Now we can do a TCP connection for the correct 5274 * NFS version and IP address. 5275 */ 5276 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled, 5277 gotvers, gotminor, &dsp, p); 5278 } 5279 if (error == 0) { 5280 KASSERT(gotdspp != NULL, ("gotdspp is NULL")); 5281 *gotdspp = dsp; 5282 } 5283 } 5284 if (nd->nd_repstat != 0 && error == 0) 5285 error = nd->nd_repstat; 5286 nfsmout: 5287 if (error != 0 && ndi != NULL) 5288 nfscl_freedevinfo(ndi); 5289 m_freem(nd->nd_mrep); 5290 return (error); 5291 } 5292 5293 /* 5294 * Do the NFSv4.1 LayoutCommit. 5295 */ 5296 int 5297 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim, 5298 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp, 5299 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff) 5300 { 5301 uint32_t *tl; 5302 struct nfsrv_descript nfsd, *nd = &nfsd; 5303 int error; 5304 5305 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL, 5306 0, 0); 5307 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER + 5308 NFSX_STATEID); 5309 txdr_hyper(off, tl); 5310 tl += 2; 5311 txdr_hyper(len, tl); 5312 tl += 2; 5313 if (reclaim != 0) 5314 *tl++ = newnfs_true; 5315 else 5316 *tl++ = newnfs_false; 5317 *tl++ = txdr_unsigned(stateidp->seqid); 5318 *tl++ = stateidp->other[0]; 5319 *tl++ = stateidp->other[1]; 5320 *tl++ = stateidp->other[2]; 5321 *tl++ = newnfs_true; 5322 if (lastbyte < off) 5323 lastbyte = off; 5324 else if (lastbyte >= (off + len)) 5325 lastbyte = off + len - 1; 5326 txdr_hyper(lastbyte, tl); 5327 tl += 2; 5328 *tl++ = newnfs_false; 5329 *tl++ = txdr_unsigned(layouttype); 5330 /* All supported layouts are 0 length. */ 5331 *tl = txdr_unsigned(0); 5332 nd->nd_flag |= ND_USEGSSNAME; 5333 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5334 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5335 if (error != 0) 5336 return (error); 5337 error = nd->nd_repstat; 5338 m_freem(nd->nd_mrep); 5339 return (error); 5340 } 5341 5342 /* 5343 * Do the NFSv4.1 LayoutReturn. 5344 */ 5345 int 5346 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim, 5347 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset, 5348 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p, 5349 uint32_t stat, uint32_t op, char *devid) 5350 { 5351 uint32_t *tl; 5352 struct nfsrv_descript nfsd, *nd = &nfsd; 5353 uint64_t tu64; 5354 int error; 5355 5356 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL, 5357 0, 0); 5358 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 5359 if (reclaim != 0) 5360 *tl++ = newnfs_true; 5361 else 5362 *tl++ = newnfs_false; 5363 *tl++ = txdr_unsigned(layouttype); 5364 *tl++ = txdr_unsigned(iomode); 5365 *tl = txdr_unsigned(layoutreturn); 5366 if (layoutreturn == NFSLAYOUTRETURN_FILE) { 5367 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID + 5368 NFSX_UNSIGNED); 5369 txdr_hyper(offset, tl); 5370 tl += 2; 5371 txdr_hyper(len, tl); 5372 tl += 2; 5373 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid); 5374 *tl++ = txdr_unsigned(stateidp->seqid); 5375 *tl++ = stateidp->other[0]; 5376 *tl++ = stateidp->other[1]; 5377 *tl++ = stateidp->other[2]; 5378 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) 5379 *tl = txdr_unsigned(0); 5380 else if (layouttype == NFSLAYOUT_FLEXFILE) { 5381 if (stat != 0) { 5382 *tl = txdr_unsigned(2 * NFSX_HYPER + 5383 NFSX_STATEID + NFSX_V4DEVICEID + 5 * 5384 NFSX_UNSIGNED); 5385 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 5386 NFSX_STATEID + NFSX_V4DEVICEID + 5 * 5387 NFSX_UNSIGNED); 5388 *tl++ = txdr_unsigned(1); /* One error. */ 5389 tu64 = 0; /* Offset. */ 5390 txdr_hyper(tu64, tl); tl += 2; 5391 tu64 = UINT64_MAX; /* Length. */ 5392 txdr_hyper(tu64, tl); tl += 2; 5393 NFSBCOPY(stateidp, tl, NFSX_STATEID); 5394 tl += (NFSX_STATEID / NFSX_UNSIGNED); 5395 *tl++ = txdr_unsigned(1); /* One error. */ 5396 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); 5397 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 5398 *tl++ = txdr_unsigned(stat); 5399 *tl++ = txdr_unsigned(op); 5400 } else { 5401 *tl = txdr_unsigned(2 * NFSX_UNSIGNED); 5402 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5403 /* No ioerrs. */ 5404 *tl++ = 0; 5405 } 5406 *tl = 0; /* No stats yet. */ 5407 } 5408 } 5409 nd->nd_flag |= ND_USEGSSNAME; 5410 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5411 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5412 if (error != 0) 5413 return (error); 5414 if (nd->nd_repstat == 0) { 5415 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5416 if (*tl != 0) { 5417 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID); 5418 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++); 5419 stateidp->other[0] = *tl++; 5420 stateidp->other[1] = *tl++; 5421 stateidp->other[2] = *tl; 5422 } 5423 } else 5424 error = nd->nd_repstat; 5425 nfsmout: 5426 m_freem(nd->nd_mrep); 5427 return (error); 5428 } 5429 5430 /* 5431 * Acquire a layout and devinfo, if possible. The caller must have acquired 5432 * a reference count on the nfsclclient structure before calling this. 5433 * Return the layout in lypp with a reference count on it, if successful. 5434 */ 5435 static int 5436 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp, 5437 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off, 5438 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p) 5439 { 5440 struct nfscllayout *lyp; 5441 struct nfsclflayout *flp; 5442 struct nfsclflayouthead flh; 5443 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose; 5444 nfsv4stateid_t stateid; 5445 struct nfsclsession *tsep; 5446 5447 *lypp = NULL; 5448 if (NFSHASFLEXFILE(nmp)) 5449 layouttype = NFSLAYOUT_FLEXFILE; 5450 else 5451 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5452 /* 5453 * If lyp is returned non-NULL, there will be a refcnt (shared lock) 5454 * on it, iff flp != NULL or a lock (exclusive lock) on it iff 5455 * flp == NULL. 5456 */ 5457 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len, 5458 off, &flp, &recalled); 5459 islocked = 0; 5460 if (lyp == NULL || flp == NULL) { 5461 if (recalled != 0) 5462 return (EIO); 5463 LIST_INIT(&flh); 5464 tsep = nfsmnt_mdssession(nmp); 5465 layoutlen = tsep->nfsess_maxcache - 5466 (NFSX_STATEID + 3 * NFSX_UNSIGNED); 5467 if (lyp == NULL) { 5468 stateid.seqid = 0; 5469 stateid.other[0] = stateidp->other[0]; 5470 stateid.other[1] = stateidp->other[1]; 5471 stateid.other[2] = stateidp->other[2]; 5472 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh, 5473 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX, 5474 (uint64_t)0, layouttype, layoutlen, &stateid, 5475 &retonclose, &flh, cred, p, NULL); 5476 } else { 5477 islocked = 1; 5478 stateid.seqid = lyp->nfsly_stateid.seqid; 5479 stateid.other[0] = lyp->nfsly_stateid.other[0]; 5480 stateid.other[1] = lyp->nfsly_stateid.other[1]; 5481 stateid.other[2] = lyp->nfsly_stateid.other[2]; 5482 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh, 5483 nfhp->nfh_len, iomode, off, UINT64_MAX, 5484 (uint64_t)0, layouttype, layoutlen, &stateid, 5485 &retonclose, &flh, cred, p, NULL); 5486 } 5487 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh, 5488 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp, 5489 &flh, layouttype, error, NULL, cred, p); 5490 if (error == 0) 5491 *lypp = lyp; 5492 else if (islocked != 0) 5493 nfscl_rellayout(lyp, 1); 5494 } else 5495 *lypp = lyp; 5496 return (error); 5497 } 5498 5499 /* 5500 * Do a TCP connection plus exchange id and create session. 5501 * If successful, a "struct nfsclds" is linked into the list for the 5502 * mount point and a pointer to it is returned. 5503 */ 5504 static int 5505 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin, 5506 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers, 5507 struct nfsclds **dspp, NFSPROC_T *p) 5508 { 5509 struct sockaddr_in *msad, *sad; 5510 struct sockaddr_in6 *msad6, *sad6; 5511 struct nfsclclient *clp; 5512 struct nfssockreq *nrp; 5513 struct nfsclds *dsp, *tdsp; 5514 int error, firsttry; 5515 enum nfsclds_state retv; 5516 uint32_t sequenceid = 0; 5517 5518 KASSERT(nmp->nm_sockreq.nr_cred != NULL, 5519 ("nfsrpc_fillsa: NULL nr_cred")); 5520 NFSLOCKCLSTATE(); 5521 clp = nmp->nm_clp; 5522 NFSUNLOCKCLSTATE(); 5523 if (clp == NULL) 5524 return (EPERM); 5525 if (af == AF_INET) { 5526 NFSLOCKMNT(nmp); 5527 /* 5528 * Check to see if we already have a session for this 5529 * address that is usable for a DS. 5530 * Note that the MDS's address is in a different place 5531 * than the sessions already acquired for DS's. 5532 */ 5533 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam; 5534 tdsp = TAILQ_FIRST(&nmp->nm_sess); 5535 while (tdsp != NULL) { 5536 if (msad != NULL && msad->sin_family == AF_INET && 5537 sin->sin_addr.s_addr == msad->sin_addr.s_addr && 5538 sin->sin_port == msad->sin_port && 5539 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 && 5540 tdsp->nfsclds_sess.nfsess_defunct == 0) { 5541 *dspp = tdsp; 5542 NFSUNLOCKMNT(nmp); 5543 NFSCL_DEBUG(4, "fnd same addr\n"); 5544 return (0); 5545 } 5546 tdsp = TAILQ_NEXT(tdsp, nfsclds_list); 5547 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL) 5548 msad = (struct sockaddr_in *) 5549 tdsp->nfsclds_sockp->nr_nam; 5550 else 5551 msad = NULL; 5552 } 5553 NFSUNLOCKMNT(nmp); 5554 5555 /* No IP address match, so look for new/trunked one. */ 5556 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO); 5557 sad->sin_len = sizeof(*sad); 5558 sad->sin_family = AF_INET; 5559 sad->sin_port = sin->sin_port; 5560 sad->sin_addr.s_addr = sin->sin_addr.s_addr; 5561 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO); 5562 nrp->nr_nam = (struct sockaddr *)sad; 5563 } else if (af == AF_INET6) { 5564 NFSLOCKMNT(nmp); 5565 /* 5566 * Check to see if we already have a session for this 5567 * address that is usable for a DS. 5568 * Note that the MDS's address is in a different place 5569 * than the sessions already acquired for DS's. 5570 */ 5571 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam; 5572 tdsp = TAILQ_FIRST(&nmp->nm_sess); 5573 while (tdsp != NULL) { 5574 if (msad6 != NULL && msad6->sin6_family == AF_INET6 && 5575 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 5576 &msad6->sin6_addr) && 5577 sin6->sin6_port == msad6->sin6_port && 5578 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 && 5579 tdsp->nfsclds_sess.nfsess_defunct == 0) { 5580 *dspp = tdsp; 5581 NFSUNLOCKMNT(nmp); 5582 return (0); 5583 } 5584 tdsp = TAILQ_NEXT(tdsp, nfsclds_list); 5585 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL) 5586 msad6 = (struct sockaddr_in6 *) 5587 tdsp->nfsclds_sockp->nr_nam; 5588 else 5589 msad6 = NULL; 5590 } 5591 NFSUNLOCKMNT(nmp); 5592 5593 /* No IP address match, so look for new/trunked one. */ 5594 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO); 5595 sad6->sin6_len = sizeof(*sad6); 5596 sad6->sin6_family = AF_INET6; 5597 sad6->sin6_port = sin6->sin6_port; 5598 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr, 5599 sizeof(struct in6_addr)); 5600 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO); 5601 nrp->nr_nam = (struct sockaddr *)sad6; 5602 } else 5603 return (EPERM); 5604 5605 nrp->nr_sotype = SOCK_STREAM; 5606 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF); 5607 nrp->nr_prog = NFS_PROG; 5608 nrp->nr_vers = vers; 5609 5610 /* 5611 * Use the credentials that were used for the mount, which are 5612 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc. 5613 * Ref. counting the credentials with crhold() is probably not 5614 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until 5615 * unmount, but I did it anyhow. 5616 */ 5617 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred); 5618 error = newnfs_connect(nmp, nrp, NULL, p, 0); 5619 NFSCL_DEBUG(3, "DS connect=%d\n", error); 5620 5621 dsp = NULL; 5622 /* Now, do the exchangeid and create session. */ 5623 if (error == 0) { 5624 if (vers == NFS_VER4) { 5625 firsttry = 0; 5626 do { 5627 error = nfsrpc_exchangeid(nmp, clp, nrp, 5628 minorvers, NFSV4EXCH_USEPNFSDS, &dsp, 5629 nrp->nr_cred, p); 5630 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error); 5631 if (error == NFSERR_MINORVERMISMATCH) 5632 minorvers = NFSV42_MINORVERSION; 5633 } while (error == NFSERR_MINORVERMISMATCH && 5634 firsttry++ == 0); 5635 if (error != 0) 5636 newnfs_disconnect(nrp); 5637 } else { 5638 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, 5639 M_WAITOK | M_ZERO); 5640 dsp->nfsclds_flags |= NFSCLDS_DS; 5641 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */ 5642 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 5643 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 5644 NULL, MTX_DEF); 5645 } 5646 } 5647 if (error == 0) { 5648 dsp->nfsclds_sockp = nrp; 5649 if (vers == NFS_VER4) { 5650 NFSLOCKMNT(nmp); 5651 retv = nfscl_getsameserver(nmp, dsp, &tdsp, 5652 &sequenceid); 5653 NFSCL_DEBUG(3, "getsame ret=%d\n", retv); 5654 if (retv == NFSDSP_USETHISSESSION && 5655 nfscl_dssameconn != 0) { 5656 NFSLOCKDS(tdsp); 5657 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN; 5658 NFSUNLOCKDS(tdsp); 5659 NFSUNLOCKMNT(nmp); 5660 /* 5661 * If there is already a session for this 5662 * server, use it. 5663 */ 5664 (void)newnfs_disconnect(nrp); 5665 nfscl_freenfsclds(dsp); 5666 *dspp = tdsp; 5667 return (0); 5668 } 5669 if (retv == NFSDSP_NOTFOUND) 5670 sequenceid = 5671 dsp->nfsclds_sess.nfsess_sequenceid; 5672 NFSUNLOCKMNT(nmp); 5673 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess, 5674 nrp, dsp, sequenceid, 0, nrp->nr_cred, p); 5675 NFSCL_DEBUG(3, "DS createsess=%d\n", error); 5676 } 5677 } else { 5678 NFSFREECRED(nrp->nr_cred); 5679 NFSFREEMUTEX(&nrp->nr_mtx); 5680 free(nrp->nr_nam, M_SONAME); 5681 free(nrp, M_NFSSOCKREQ); 5682 } 5683 if (error == 0) { 5684 NFSCL_DEBUG(3, "add DS session\n"); 5685 /* 5686 * Put it at the end of the list. That way the list 5687 * is ordered by when the entry was added. This matters 5688 * since the one done first is the one that should be 5689 * used for sequencid'ing any subsequent create sessions. 5690 */ 5691 NFSLOCKMNT(nmp); 5692 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list); 5693 NFSUNLOCKMNT(nmp); 5694 *dspp = dsp; 5695 } else if (dsp != NULL) { 5696 newnfs_disconnect(nrp); 5697 nfscl_freenfsclds(dsp); 5698 } 5699 return (error); 5700 } 5701 5702 /* 5703 * Do the NFSv4.1 Reclaim Complete. 5704 */ 5705 int 5706 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p) 5707 { 5708 uint32_t *tl; 5709 struct nfsrv_descript nfsd; 5710 struct nfsrv_descript *nd = &nfsd; 5711 int error; 5712 5713 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0, 5714 0); 5715 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 5716 *tl = newnfs_false; 5717 nd->nd_flag |= ND_USEGSSNAME; 5718 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5719 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5720 if (error != 0) 5721 return (error); 5722 error = nd->nd_repstat; 5723 m_freem(nd->nd_mrep); 5724 return (error); 5725 } 5726 5727 /* 5728 * Initialize the slot tables for a session. 5729 */ 5730 static void 5731 nfscl_initsessionslots(struct nfsclsession *sep) 5732 { 5733 int i; 5734 5735 for (i = 0; i < NFSV4_CBSLOTS; i++) { 5736 if (sep->nfsess_cbslots[i].nfssl_reply != NULL) 5737 m_freem(sep->nfsess_cbslots[i].nfssl_reply); 5738 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot)); 5739 } 5740 for (i = 0; i < 64; i++) 5741 sep->nfsess_slotseq[i] = 0; 5742 sep->nfsess_slots = 0; 5743 } 5744 5745 /* 5746 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS). 5747 */ 5748 int 5749 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 5750 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p) 5751 { 5752 struct nfsnode *np = VTONFS(vp); 5753 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 5754 struct nfscllayout *layp; 5755 struct nfscldevinfo *dip; 5756 struct nfsclflayout *rflp; 5757 struct mbuf *m; 5758 struct nfsclwritedsdorpc *drpc, *tdrpc; 5759 nfsv4stateid_t stateid; 5760 struct ucred *newcred; 5761 uint64_t lastbyte, len, off, oresid, xfer; 5762 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo; 5763 void *lckp; 5764 uint8_t *dev; 5765 void *iovbase = NULL; 5766 size_t iovlen = 0; 5767 off_t offs = 0; 5768 ssize_t resid = 0; 5769 5770 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 || 5771 (np->n_flag & NNOLAYOUT) != 0) 5772 return (EIO); 5773 /* Now, get a reference cnt on the clientid for this mount. */ 5774 if (nfscl_getref(nmp) == 0) 5775 return (EIO); 5776 5777 /* Find an appropriate stateid. */ 5778 newcred = NFSNEWCRED(cred); 5779 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 5780 rwaccess, 1, newcred, p, &stateid, &lckp); 5781 if (error != 0) { 5782 NFSFREECRED(newcred); 5783 nfscl_relref(nmp); 5784 return (error); 5785 } 5786 /* Search for a layout for this file. */ 5787 off = uiop->uio_offset; 5788 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh, 5789 np->n_fhp->nfh_len, off, &rflp, &recalled); 5790 if (layp == NULL || rflp == NULL) { 5791 if (recalled != 0) { 5792 NFSFREECRED(newcred); 5793 nfscl_relref(nmp); 5794 return (EIO); 5795 } 5796 if (layp != NULL) { 5797 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0); 5798 layp = NULL; 5799 } 5800 /* Try and get a Layout, if it is supported. */ 5801 if (rwaccess == NFSV4OPEN_ACCESSWRITE || 5802 (np->n_flag & NWRITEOPENED) != 0) 5803 iolaymode = NFSLAYOUTIOMODE_RW; 5804 else 5805 iolaymode = NFSLAYOUTIOMODE_READ; 5806 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode, 5807 NULL, &stateid, off, &layp, newcred, p); 5808 if (error != 0) { 5809 NFSLOCKNODE(np); 5810 np->n_flag |= NNOLAYOUT; 5811 NFSUNLOCKNODE(np); 5812 if (lckp != NULL) 5813 nfscl_lockderef(lckp); 5814 NFSFREECRED(newcred); 5815 if (layp != NULL) 5816 nfscl_rellayout(layp, 0); 5817 nfscl_relref(nmp); 5818 return (error); 5819 } 5820 } 5821 5822 /* 5823 * Loop around finding a layout that works for the first part of 5824 * this I/O operation, and then call the function that actually 5825 * does the RPC. 5826 */ 5827 eof = 0; 5828 len = (uint64_t)uiop->uio_resid; 5829 while (len > 0 && error == 0 && eof == 0) { 5830 off = uiop->uio_offset; 5831 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp); 5832 if (error == 0) { 5833 oresid = xfer = (uint64_t)uiop->uio_resid; 5834 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off)) 5835 xfer = rflp->nfsfl_end - rflp->nfsfl_off; 5836 /* 5837 * For Flex File layout with mirrored DSs, select one 5838 * of them at random for reads. For writes and commits, 5839 * do all mirrors. 5840 */ 5841 m = NULL; 5842 tdrpc = drpc = NULL; 5843 firstmirror = 0; 5844 mirrorcnt = 1; 5845 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 && 5846 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) { 5847 if (rwaccess == NFSV4OPEN_ACCESSREAD) { 5848 firstmirror = arc4random() % mirrorcnt; 5849 mirrorcnt = firstmirror + 1; 5850 } else { 5851 if (docommit == 0) { 5852 /* 5853 * Save values, so uiop can be 5854 * rolled back upon a write 5855 * error. 5856 */ 5857 offs = uiop->uio_offset; 5858 resid = uiop->uio_resid; 5859 iovbase = 5860 uiop->uio_iov->iov_base; 5861 iovlen = uiop->uio_iov->iov_len; 5862 m = nfsm_uiombuflist(uiop, len, 5863 NULL, NULL); 5864 } 5865 tdrpc = drpc = malloc(sizeof(*drpc) * 5866 (mirrorcnt - 1), M_TEMP, M_WAITOK | 5867 M_ZERO); 5868 } 5869 } 5870 for (i = firstmirror; i < mirrorcnt && error == 0; i++){ 5871 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) { 5872 dev = rflp->nfsfl_ffm[i].dev; 5873 dip = nfscl_getdevinfo(nmp->nm_clp, dev, 5874 rflp->nfsfl_ffm[i].devp); 5875 } else { 5876 dev = rflp->nfsfl_dev; 5877 dip = nfscl_getdevinfo(nmp->nm_clp, dev, 5878 rflp->nfsfl_devp); 5879 } 5880 if (dip != NULL) { 5881 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE) 5882 != 0) 5883 error = nfscl_dofflayoutio(vp, 5884 uiop, iomode, must_commit, 5885 &eof, &stateid, rwaccess, 5886 dip, layp, rflp, off, xfer, 5887 i, docommit, m, tdrpc, 5888 newcred, p); 5889 else 5890 error = nfscl_doflayoutio(vp, 5891 uiop, iomode, must_commit, 5892 &eof, &stateid, rwaccess, 5893 dip, layp, rflp, off, xfer, 5894 docommit, newcred, p); 5895 nfscl_reldevinfo(dip); 5896 } else 5897 error = EIO; 5898 tdrpc++; 5899 } 5900 if (m != NULL) 5901 m_freem(m); 5902 tdrpc = drpc; 5903 timo = hz / 50; /* Wait for 20msec. */ 5904 if (timo < 1) 5905 timo = 1; 5906 for (i = firstmirror; i < mirrorcnt - 1 && 5907 tdrpc != NULL; i++, tdrpc++) { 5908 /* 5909 * For the unused drpc entries, both inprog and 5910 * err == 0, so this loop won't break. 5911 */ 5912 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5913 tsleep(&tdrpc->tsk, PVFS, "clrpcio", 5914 timo); 5915 if (error == 0 && tdrpc->err != 0) 5916 error = tdrpc->err; 5917 } 5918 free(drpc, M_TEMP); 5919 if (error == 0) { 5920 if (mirrorcnt > 1 && rwaccess == 5921 NFSV4OPEN_ACCESSWRITE && docommit == 0) { 5922 NFSLOCKCLSTATE(); 5923 layp->nfsly_flags |= NFSLY_WRITTEN; 5924 NFSUNLOCKCLSTATE(); 5925 } 5926 lastbyte = off + xfer - 1; 5927 NFSLOCKCLSTATE(); 5928 if (lastbyte > layp->nfsly_lastbyte) 5929 layp->nfsly_lastbyte = lastbyte; 5930 NFSUNLOCKCLSTATE(); 5931 } else if (error == NFSERR_OPENMODE && 5932 rwaccess == NFSV4OPEN_ACCESSREAD) { 5933 NFSLOCKMNT(nmp); 5934 nmp->nm_state |= NFSSTA_OPENMODE; 5935 NFSUNLOCKMNT(nmp); 5936 } else 5937 error = EIO; 5938 if (error == 0) 5939 len -= (oresid - (uint64_t)uiop->uio_resid); 5940 else if (mirrorcnt > 1 && rwaccess == 5941 NFSV4OPEN_ACCESSWRITE && docommit == 0) { 5942 /* 5943 * In case the rpc gets retried, roll the 5944 * uio fields changed by nfsm_uiombuflist() 5945 * back. 5946 */ 5947 uiop->uio_offset = offs; 5948 uiop->uio_resid = resid; 5949 uiop->uio_iov->iov_base = iovbase; 5950 uiop->uio_iov->iov_len = iovlen; 5951 } 5952 } 5953 } 5954 if (lckp != NULL) 5955 nfscl_lockderef(lckp); 5956 NFSFREECRED(newcred); 5957 nfscl_rellayout(layp, 0); 5958 nfscl_relref(nmp); 5959 return (error); 5960 } 5961 5962 /* 5963 * Make a copy of the mbuf chain and add an mbuf for null padding, as required. 5964 */ 5965 static struct mbuf * 5966 nfsm_copym(struct mbuf *m, int off, int xfer) 5967 { 5968 struct mbuf *m2, *m3, *m4; 5969 uint32_t *tl; 5970 int rem; 5971 5972 m2 = m_copym(m, off, xfer, M_WAITOK); 5973 rem = NFSM_RNDUP(xfer) - xfer; 5974 if (rem > 0) { 5975 /* 5976 * The zero padding to a multiple of 4 bytes is required by 5977 * the XDR. So that the mbufs copied by reference aren't 5978 * modified, add an mbuf with the zero'd bytes to the list. 5979 * rem will be a maximum of 3, so one zero'd uint32_t is 5980 * sufficient. 5981 */ 5982 m3 = m2; 5983 while (m3->m_next != NULL) 5984 m3 = m3->m_next; 5985 NFSMGET(m4); 5986 tl = mtod(m4, uint32_t *); 5987 *tl = 0; 5988 m4->m_len = rem; 5989 m3->m_next = m4; 5990 } 5991 return (m2); 5992 } 5993 5994 /* 5995 * Find a file layout that will handle the first bytes of the requested 5996 * range and return the information from it needed to the I/O operation. 5997 */ 5998 int 5999 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess, 6000 struct nfsclflayout **retflpp) 6001 { 6002 struct nfsclflayout *flp, *nflp, *rflp; 6003 uint32_t rw; 6004 6005 rflp = NULL; 6006 rw = rwaccess; 6007 /* For reading, do the Read list first and then the Write list. */ 6008 do { 6009 if (rw == NFSV4OPEN_ACCESSREAD) 6010 flp = LIST_FIRST(&lyp->nfsly_flayread); 6011 else 6012 flp = LIST_FIRST(&lyp->nfsly_flayrw); 6013 while (flp != NULL) { 6014 nflp = LIST_NEXT(flp, nfsfl_list); 6015 if (flp->nfsfl_off > off) 6016 break; 6017 if (flp->nfsfl_end > off && 6018 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end)) 6019 rflp = flp; 6020 flp = nflp; 6021 } 6022 if (rw == NFSV4OPEN_ACCESSREAD) 6023 rw = NFSV4OPEN_ACCESSWRITE; 6024 else 6025 rw = 0; 6026 } while (rw != 0); 6027 if (rflp != NULL) { 6028 /* This one covers the most bytes starting at off. */ 6029 *retflpp = rflp; 6030 return (0); 6031 } 6032 return (EIO); 6033 } 6034 6035 /* 6036 * Do I/O using an NFSv4.1 or NFSv4.2 file layout. 6037 */ 6038 static int 6039 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6040 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp, 6041 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off, 6042 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p) 6043 { 6044 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer; 6045 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers; 6046 struct nfsnode *np; 6047 struct nfsfh *fhp; 6048 struct nfsclds **dspp; 6049 6050 np = VTONFS(vp); 6051 rel_off = off - flp->nfsfl_patoff; 6052 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff; 6053 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) % 6054 dp->nfsdi_stripecnt; 6055 transfer = stripe_unit_size - (rel_off % stripe_unit_size); 6056 error = 0; 6057 6058 /* Loop around, doing I/O for each stripe unit. */ 6059 while (len > 0 && error == 0) { 6060 stripe_index = nfsfldi_stripeindex(dp, stripe_pos); 6061 dspp = nfsfldi_addr(dp, stripe_index); 6062 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0) 6063 minorvers = NFSV42_MINORVERSION; 6064 else 6065 minorvers = NFSV41_MINORVERSION; 6066 if (len > transfer && docommit == 0) 6067 xfer = transfer; 6068 else 6069 xfer = len; 6070 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) { 6071 /* Dense layout. */ 6072 if (stripe_pos >= flp->nfsfl_fhcnt) 6073 return (EIO); 6074 fhp = flp->nfsfl_fh[stripe_pos]; 6075 io_off = (rel_off / (stripe_unit_size * 6076 dp->nfsdi_stripecnt)) * stripe_unit_size + 6077 rel_off % stripe_unit_size; 6078 } else { 6079 /* Sparse layout. */ 6080 if (flp->nfsfl_fhcnt > 1) { 6081 if (stripe_index >= flp->nfsfl_fhcnt) 6082 return (EIO); 6083 fhp = flp->nfsfl_fh[stripe_index]; 6084 } else if (flp->nfsfl_fhcnt == 1) 6085 fhp = flp->nfsfl_fh[0]; 6086 else 6087 fhp = np->n_fhp; 6088 io_off = off; 6089 } 6090 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) { 6091 commit_thru_mds = 1; 6092 if (docommit != 0) 6093 error = EIO; 6094 } else { 6095 commit_thru_mds = 0; 6096 NFSLOCKNODE(np); 6097 np->n_flag |= NDSCOMMIT; 6098 NFSUNLOCKNODE(np); 6099 } 6100 if (docommit != 0) { 6101 if (error == 0) 6102 error = nfsrpc_commitds(vp, io_off, xfer, 6103 *dspp, fhp, NFS_VER4, minorvers, cred, p); 6104 if (error == 0) { 6105 /* 6106 * Set both eof and uio_resid = 0 to end any 6107 * loops. 6108 */ 6109 *eofp = 1; 6110 uiop->uio_resid = 0; 6111 } else { 6112 NFSLOCKNODE(np); 6113 np->n_flag &= ~NDSCOMMIT; 6114 NFSUNLOCKNODE(np); 6115 } 6116 } else if (rwflag == NFSV4OPEN_ACCESSREAD) 6117 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp, 6118 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p); 6119 else { 6120 error = nfsrpc_writeds(vp, uiop, iomode, must_commit, 6121 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds, 6122 0, NFS_VER4, minorvers, cred, p); 6123 if (error == 0) { 6124 NFSLOCKCLSTATE(); 6125 lyp->nfsly_flags |= NFSLY_WRITTEN; 6126 NFSUNLOCKCLSTATE(); 6127 } 6128 } 6129 if (error == 0) { 6130 transfer = stripe_unit_size; 6131 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt; 6132 len -= xfer; 6133 off += xfer; 6134 } 6135 } 6136 return (error); 6137 } 6138 6139 /* 6140 * Do I/O using an NFSv4.1 flex file layout. 6141 */ 6142 static int 6143 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6144 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp, 6145 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off, 6146 uint64_t len, int mirror, int docommit, struct mbuf *mp, 6147 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6148 { 6149 uint64_t transfer, xfer; 6150 int error, rel_off; 6151 struct nfsnode *np; 6152 struct nfsfh *fhp; 6153 struct nfsclds **dspp; 6154 struct ucred *tcred; 6155 struct mbuf *m; 6156 6157 np = VTONFS(vp); 6158 error = 0; 6159 rel_off = 0; 6160 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off, 6161 (uintmax_t)len); 6162 /* Loop around, doing I/O for each stripe unit. */ 6163 while (len > 0 && error == 0) { 6164 dspp = nfsfldi_addr(dp, 0); 6165 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex]; 6166 stateidp = &flp->nfsfl_ffm[mirror].st; 6167 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n", 6168 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid); 6169 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) { 6170 tcred = NFSNEWCRED(cred); 6171 tcred->cr_uid = flp->nfsfl_ffm[mirror].user; 6172 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group; 6173 tcred->cr_ngroups = 1; 6174 } else 6175 tcred = cred; 6176 if (rwflag == NFSV4OPEN_ACCESSREAD) 6177 transfer = dp->nfsdi_rsize; 6178 else 6179 transfer = dp->nfsdi_wsize; 6180 NFSLOCKNODE(np); 6181 np->n_flag |= NDSCOMMIT; 6182 NFSUNLOCKNODE(np); 6183 if (len > transfer && docommit == 0) 6184 xfer = transfer; 6185 else 6186 xfer = len; 6187 if (docommit != 0) { 6188 if (error == 0) { 6189 /* 6190 * Do last mirrored DS commit with this thread. 6191 */ 6192 if (mirror < flp->nfsfl_mirrorcnt - 1) 6193 error = nfsio_commitds(vp, off, xfer, 6194 *dspp, fhp, dp->nfsdi_vers, 6195 dp->nfsdi_minorvers, drpc, tcred, 6196 p); 6197 else 6198 error = nfsrpc_commitds(vp, off, xfer, 6199 *dspp, fhp, dp->nfsdi_vers, 6200 dp->nfsdi_minorvers, tcred, p); 6201 NFSCL_DEBUG(4, "commitds=%d\n", error); 6202 if (error != 0 && error != EACCES && error != 6203 ESTALE) { 6204 NFSCL_DEBUG(4, 6205 "DS layreterr for commit\n"); 6206 nfscl_dserr(NFSV4OP_COMMIT, error, dp, 6207 lyp, *dspp); 6208 } 6209 } 6210 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error); 6211 if (error == 0) { 6212 /* 6213 * Set both eof and uio_resid = 0 to end any 6214 * loops. 6215 */ 6216 *eofp = 1; 6217 uiop->uio_resid = 0; 6218 } else { 6219 NFSLOCKNODE(np); 6220 np->n_flag &= ~NDSCOMMIT; 6221 NFSUNLOCKNODE(np); 6222 } 6223 } else if (rwflag == NFSV4OPEN_ACCESSREAD) { 6224 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp, 6225 off, xfer, fhp, 1, dp->nfsdi_vers, 6226 dp->nfsdi_minorvers, tcred, p); 6227 NFSCL_DEBUG(4, "readds=%d\n", error); 6228 if (error != 0 && error != EACCES && error != ESTALE) { 6229 NFSCL_DEBUG(4, "DS layreterr for read\n"); 6230 nfscl_dserr(NFSV4OP_READ, error, dp, lyp, 6231 *dspp); 6232 } 6233 } else { 6234 if (flp->nfsfl_mirrorcnt == 1) { 6235 error = nfsrpc_writeds(vp, uiop, iomode, 6236 must_commit, stateidp, *dspp, off, xfer, 6237 fhp, 0, 1, dp->nfsdi_vers, 6238 dp->nfsdi_minorvers, tcred, p); 6239 if (error == 0) { 6240 NFSLOCKCLSTATE(); 6241 lyp->nfsly_flags |= NFSLY_WRITTEN; 6242 NFSUNLOCKCLSTATE(); 6243 } 6244 } else { 6245 m = nfsm_copym(mp, rel_off, xfer); 6246 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n", 6247 rel_off, (uintmax_t)xfer); 6248 /* 6249 * Do last write to a mirrored DS with this 6250 * thread. 6251 */ 6252 if (mirror < flp->nfsfl_mirrorcnt - 1) 6253 error = nfsio_writedsmir(vp, iomode, 6254 must_commit, stateidp, *dspp, off, 6255 xfer, fhp, m, dp->nfsdi_vers, 6256 dp->nfsdi_minorvers, drpc, tcred, 6257 p); 6258 else 6259 error = nfsrpc_writedsmir(vp, iomode, 6260 must_commit, stateidp, *dspp, off, 6261 xfer, fhp, m, dp->nfsdi_vers, 6262 dp->nfsdi_minorvers, tcred, p); 6263 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error); 6264 if (error != 0 && error != EACCES && error != 6265 ESTALE) { 6266 NFSCL_DEBUG(4, 6267 "DS layreterr for write\n"); 6268 nfscl_dserr(NFSV4OP_WRITE, error, dp, 6269 lyp, *dspp); 6270 } 6271 } 6272 } 6273 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error); 6274 if (error == 0) { 6275 len -= xfer; 6276 off += xfer; 6277 rel_off += xfer; 6278 } 6279 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) 6280 NFSFREECRED(tcred); 6281 } 6282 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error); 6283 return (error); 6284 } 6285 6286 /* 6287 * The actual read RPC done to a DS. 6288 */ 6289 static int 6290 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp, 6291 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex, 6292 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p) 6293 { 6294 uint32_t *tl; 6295 int attrflag, error, retlen; 6296 struct nfsrv_descript nfsd; 6297 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6298 struct nfsrv_descript *nd = &nfsd; 6299 struct nfssockreq *nrp; 6300 struct nfsvattr na; 6301 6302 nd->nd_mrep = NULL; 6303 if (vers == 0 || vers == NFS_VER4) { 6304 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, 6305 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6306 vers = NFS_VER4; 6307 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers); 6308 if (flex != 0) 6309 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 6310 else 6311 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO); 6312 } else { 6313 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh, 6314 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6315 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n"); 6316 } 6317 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3); 6318 txdr_hyper(io_off, tl); 6319 *(tl + 2) = txdr_unsigned(len); 6320 nrp = dsp->nfsclds_sockp; 6321 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp); 6322 if (nrp == NULL) 6323 /* If NULL, use the MDS socket. */ 6324 nrp = &nmp->nm_sockreq; 6325 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6326 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6327 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat, 6328 error); 6329 if (error != 0) 6330 return (error); 6331 if (vers == NFS_VER3) { 6332 error = nfscl_postop_attr(nd, &na, &attrflag, NULL); 6333 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error); 6334 if (error != 0) 6335 goto nfsmout; 6336 } 6337 if (nd->nd_repstat != 0) { 6338 error = nd->nd_repstat; 6339 goto nfsmout; 6340 } 6341 if (vers == NFS_VER3) { 6342 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6343 *eofp = fxdr_unsigned(int, *(tl + 1)); 6344 } else { 6345 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 6346 *eofp = fxdr_unsigned(int, *tl); 6347 } 6348 NFSM_STRSIZ(retlen, len); 6349 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp); 6350 error = nfsm_mbufuio(nd, uiop, retlen); 6351 nfsmout: 6352 if (nd->nd_mrep != NULL) 6353 m_freem(nd->nd_mrep); 6354 return (error); 6355 } 6356 6357 /* 6358 * The actual write RPC done to a DS. 6359 */ 6360 static int 6361 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6362 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len, 6363 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers, 6364 struct ucred *cred, NFSPROC_T *p) 6365 { 6366 uint32_t *tl; 6367 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6368 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC; 6369 int32_t backup; 6370 struct nfsrv_descript nfsd; 6371 struct nfsrv_descript *nd = &nfsd; 6372 struct nfssockreq *nrp; 6373 struct nfsvattr na; 6374 6375 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1")); 6376 nd->nd_mrep = NULL; 6377 if (vers == 0 || vers == NFS_VER4) { 6378 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, 6379 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6380 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers); 6381 vers = NFS_VER4; 6382 if (flex != 0) 6383 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 6384 else 6385 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO); 6386 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 6387 } else { 6388 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh, 6389 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6390 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n"); 6391 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED); 6392 } 6393 txdr_hyper(io_off, tl); 6394 tl += 2; 6395 if (vers == NFS_VER3) 6396 *tl++ = txdr_unsigned(len); 6397 *tl++ = txdr_unsigned(*iomode); 6398 *tl = txdr_unsigned(len); 6399 nfsm_uiombuf(nd, uiop, len); 6400 nrp = dsp->nfsclds_sockp; 6401 if (nrp == NULL) 6402 /* If NULL, use the MDS socket. */ 6403 nrp = &nmp->nm_sockreq; 6404 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6405 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6406 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error, 6407 nd->nd_repstat); 6408 if (error != 0) 6409 return (error); 6410 if (nd->nd_repstat != 0) { 6411 /* 6412 * In case the rpc gets retried, roll 6413 * the uio fileds changed by nfsm_uiombuf() 6414 * back. 6415 */ 6416 uiop->uio_offset -= len; 6417 uiop->uio_resid += len; 6418 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - len; 6419 uiop->uio_iov->iov_len += len; 6420 error = nd->nd_repstat; 6421 } else { 6422 if (vers == NFS_VER3) { 6423 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 6424 NULL); 6425 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error); 6426 if (error != 0) 6427 goto nfsmout; 6428 } 6429 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 6430 rlen = fxdr_unsigned(int, *tl++); 6431 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen); 6432 if (rlen == 0) { 6433 error = NFSERR_IO; 6434 goto nfsmout; 6435 } else if (rlen < len) { 6436 backup = len - rlen; 6437 uiop->uio_iov->iov_base = 6438 (char *)uiop->uio_iov->iov_base - backup; 6439 uiop->uio_iov->iov_len += backup; 6440 uiop->uio_offset -= backup; 6441 uiop->uio_resid += backup; 6442 len = rlen; 6443 } 6444 commit = fxdr_unsigned(int, *tl++); 6445 6446 /* 6447 * Return the lowest commitment level 6448 * obtained by any of the RPCs. 6449 */ 6450 if (committed == NFSWRITE_FILESYNC) 6451 committed = commit; 6452 else if (committed == NFSWRITE_DATASYNC && 6453 commit == NFSWRITE_UNSTABLE) 6454 committed = commit; 6455 if (commit_thru_mds != 0) { 6456 NFSLOCKMNT(nmp); 6457 if (!NFSHASWRITEVERF(nmp)) { 6458 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 6459 NFSSETWRITEVERF(nmp); 6460 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) { 6461 *must_commit = 1; 6462 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 6463 } 6464 NFSUNLOCKMNT(nmp); 6465 } else { 6466 NFSLOCKDS(dsp); 6467 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) { 6468 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6469 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF; 6470 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 6471 *must_commit = 1; 6472 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6473 } 6474 NFSUNLOCKDS(dsp); 6475 } 6476 } 6477 nfsmout: 6478 if (nd->nd_mrep != NULL) 6479 m_freem(nd->nd_mrep); 6480 *iomode = committed; 6481 if (nd->nd_repstat != 0 && error == 0) 6482 error = nd->nd_repstat; 6483 return (error); 6484 } 6485 6486 /* 6487 * The actual write RPC done to a DS. 6488 * This variant is called from a separate kernel process for mirrors. 6489 * Any short write is considered an IO error. 6490 */ 6491 static int 6492 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit, 6493 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len, 6494 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers, 6495 struct ucred *cred, NFSPROC_T *p) 6496 { 6497 uint32_t *tl; 6498 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6499 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen; 6500 struct nfsrv_descript nfsd; 6501 struct nfsrv_descript *nd = &nfsd; 6502 struct nfssockreq *nrp; 6503 struct nfsvattr na; 6504 6505 nd->nd_mrep = NULL; 6506 if (vers == 0 || vers == NFS_VER4) { 6507 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, 6508 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6509 vers = NFS_VER4; 6510 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n", 6511 minorvers); 6512 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 6513 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 6514 } else { 6515 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh, 6516 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6517 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n"); 6518 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED); 6519 } 6520 txdr_hyper(io_off, tl); 6521 tl += 2; 6522 if (vers == NFS_VER3) 6523 *tl++ = txdr_unsigned(len); 6524 *tl++ = txdr_unsigned(*iomode); 6525 *tl = txdr_unsigned(len); 6526 if (len > 0) { 6527 /* Put data in mbuf chain. */ 6528 nd->nd_mb->m_next = m; 6529 /* Set nd_mb and nd_bpos to end of data. */ 6530 while (m->m_next != NULL) 6531 m = m->m_next; 6532 nd->nd_mb = m; 6533 nd->nd_bpos = mtod(m, char *) + m->m_len; 6534 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len); 6535 } 6536 nrp = dsp->nfsclds_sockp; 6537 if (nrp == NULL) 6538 /* If NULL, use the MDS socket. */ 6539 nrp = &nmp->nm_sockreq; 6540 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6541 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6542 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error, 6543 nd->nd_repstat); 6544 if (error != 0) 6545 return (error); 6546 if (nd->nd_repstat != 0) 6547 error = nd->nd_repstat; 6548 else { 6549 if (vers == NFS_VER3) { 6550 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 6551 NULL); 6552 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n", 6553 error); 6554 if (error != 0) 6555 goto nfsmout; 6556 } 6557 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 6558 rlen = fxdr_unsigned(int, *tl++); 6559 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len, 6560 rlen); 6561 if (rlen != len) { 6562 error = NFSERR_IO; 6563 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", 6564 len, rlen); 6565 goto nfsmout; 6566 } 6567 commit = fxdr_unsigned(int, *tl++); 6568 6569 /* 6570 * Return the lowest commitment level 6571 * obtained by any of the RPCs. 6572 */ 6573 if (committed == NFSWRITE_FILESYNC) 6574 committed = commit; 6575 else if (committed == NFSWRITE_DATASYNC && 6576 commit == NFSWRITE_UNSTABLE) 6577 committed = commit; 6578 NFSLOCKDS(dsp); 6579 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) { 6580 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6581 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF; 6582 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 6583 *must_commit = 1; 6584 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6585 } 6586 NFSUNLOCKDS(dsp); 6587 } 6588 nfsmout: 6589 if (nd->nd_mrep != NULL) 6590 m_freem(nd->nd_mrep); 6591 *iomode = committed; 6592 if (nd->nd_repstat != 0 && error == 0) 6593 error = nd->nd_repstat; 6594 return (error); 6595 } 6596 6597 /* 6598 * Start up the thread that will execute nfsrpc_writedsmir(). 6599 */ 6600 static void 6601 start_writedsmir(void *arg, int pending) 6602 { 6603 struct nfsclwritedsdorpc *drpc; 6604 6605 drpc = (struct nfsclwritedsdorpc *)arg; 6606 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode, 6607 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len, 6608 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred, 6609 drpc->p); 6610 drpc->done = 1; 6611 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err); 6612 } 6613 6614 /* 6615 * Set up the write DS mirror call for the pNFS I/O thread. 6616 */ 6617 static int 6618 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit, 6619 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len, 6620 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers, 6621 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6622 { 6623 int error, ret; 6624 6625 error = 0; 6626 drpc->done = 0; 6627 drpc->vp = vp; 6628 drpc->iomode = *iomode; 6629 drpc->must_commit = *must_commit; 6630 drpc->stateidp = stateidp; 6631 drpc->dsp = dsp; 6632 drpc->off = off; 6633 drpc->len = len; 6634 drpc->fhp = fhp; 6635 drpc->m = m; 6636 drpc->vers = vers; 6637 drpc->minorvers = minorvers; 6638 drpc->cred = cred; 6639 drpc->p = p; 6640 drpc->inprog = 0; 6641 ret = EIO; 6642 if (nfs_pnfsiothreads != 0) { 6643 ret = nfs_pnfsio(start_writedsmir, drpc); 6644 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret); 6645 } 6646 if (ret != 0) 6647 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp, 6648 dsp, off, len, fhp, m, vers, minorvers, cred, p); 6649 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error); 6650 return (error); 6651 } 6652 6653 /* 6654 * Free up the nfsclds structure. 6655 */ 6656 void 6657 nfscl_freenfsclds(struct nfsclds *dsp) 6658 { 6659 int i; 6660 6661 if (dsp == NULL) 6662 return; 6663 if (dsp->nfsclds_sockp != NULL) { 6664 NFSFREECRED(dsp->nfsclds_sockp->nr_cred); 6665 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx); 6666 free(dsp->nfsclds_sockp->nr_nam, M_SONAME); 6667 free(dsp->nfsclds_sockp, M_NFSSOCKREQ); 6668 } 6669 NFSFREEMUTEX(&dsp->nfsclds_mtx); 6670 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx); 6671 for (i = 0; i < NFSV4_CBSLOTS; i++) { 6672 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL) 6673 m_freem( 6674 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply); 6675 } 6676 free(dsp, M_NFSCLDS); 6677 } 6678 6679 static enum nfsclds_state 6680 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp, 6681 struct nfsclds **retdspp, uint32_t *sequencep) 6682 { 6683 struct nfsclds *dsp; 6684 int fndseq; 6685 6686 /* 6687 * Search the list of nfsclds structures for one with the same 6688 * server. 6689 */ 6690 fndseq = 0; 6691 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) { 6692 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen && 6693 dsp->nfsclds_servownlen != 0 && 6694 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown, 6695 dsp->nfsclds_servownlen) && 6696 dsp->nfsclds_sess.nfsess_defunct == 0) { 6697 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n", 6698 TAILQ_FIRST(&nmp->nm_sess), dsp, 6699 dsp->nfsclds_flags); 6700 if (fndseq == 0) { 6701 /* Get sequenceid# from first entry. */ 6702 *sequencep = 6703 dsp->nfsclds_sess.nfsess_sequenceid; 6704 fndseq = 1; 6705 } 6706 /* Server major id matches. */ 6707 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) { 6708 *retdspp = dsp; 6709 return (NFSDSP_USETHISSESSION); 6710 } 6711 6712 } 6713 } 6714 if (fndseq != 0) 6715 return (NFSDSP_SEQTHISSESSION); 6716 return (NFSDSP_NOTFOUND); 6717 } 6718 6719 /* 6720 * NFS commit rpc to a NFSv4.1 DS. 6721 */ 6722 static int 6723 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp, 6724 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred, 6725 NFSPROC_T *p) 6726 { 6727 uint32_t *tl; 6728 struct nfsrv_descript nfsd, *nd = &nfsd; 6729 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6730 struct nfssockreq *nrp; 6731 struct nfsvattr na; 6732 int attrflag, error; 6733 6734 nd->nd_mrep = NULL; 6735 if (vers == 0 || vers == NFS_VER4) { 6736 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, 6737 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6738 vers = NFS_VER4; 6739 } else 6740 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh, 6741 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6742 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers, 6743 minorvers); 6744 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 6745 txdr_hyper(offset, tl); 6746 tl += 2; 6747 *tl = txdr_unsigned(cnt); 6748 nrp = dsp->nfsclds_sockp; 6749 if (nrp == NULL) 6750 /* If NULL, use the MDS socket. */ 6751 nrp = &nmp->nm_sockreq; 6752 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6753 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6754 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error, 6755 nd->nd_repstat); 6756 if (error != 0) 6757 return (error); 6758 if (nd->nd_repstat == 0) { 6759 if (vers == NFS_VER3) { 6760 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 6761 NULL); 6762 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error); 6763 if (error != 0) 6764 goto nfsmout; 6765 } 6766 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 6767 NFSLOCKDS(dsp); 6768 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 6769 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6770 error = NFSERR_STALEWRITEVERF; 6771 } 6772 NFSUNLOCKDS(dsp); 6773 } 6774 nfsmout: 6775 if (error == 0 && nd->nd_repstat != 0) 6776 error = nd->nd_repstat; 6777 m_freem(nd->nd_mrep); 6778 return (error); 6779 } 6780 6781 /* 6782 * Start up the thread that will execute nfsrpc_commitds(). 6783 */ 6784 static void 6785 start_commitds(void *arg, int pending) 6786 { 6787 struct nfsclwritedsdorpc *drpc; 6788 6789 drpc = (struct nfsclwritedsdorpc *)arg; 6790 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len, 6791 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred, 6792 drpc->p); 6793 drpc->done = 1; 6794 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err); 6795 } 6796 6797 /* 6798 * Set up the commit DS mirror call for the pNFS I/O thread. 6799 */ 6800 static int 6801 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp, 6802 struct nfsfh *fhp, int vers, int minorvers, 6803 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6804 { 6805 int error, ret; 6806 6807 error = 0; 6808 drpc->done = 0; 6809 drpc->vp = vp; 6810 drpc->off = offset; 6811 drpc->len = cnt; 6812 drpc->dsp = dsp; 6813 drpc->fhp = fhp; 6814 drpc->vers = vers; 6815 drpc->minorvers = minorvers; 6816 drpc->cred = cred; 6817 drpc->p = p; 6818 drpc->inprog = 0; 6819 ret = EIO; 6820 if (nfs_pnfsiothreads != 0) { 6821 ret = nfs_pnfsio(start_commitds, drpc); 6822 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret); 6823 } 6824 if (ret != 0) 6825 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers, 6826 minorvers, cred, p); 6827 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error); 6828 return (error); 6829 } 6830 6831 /* 6832 * NFS Advise rpc 6833 */ 6834 APPLESTATIC int 6835 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise, 6836 struct ucred *cred, NFSPROC_T *p) 6837 { 6838 u_int32_t *tl; 6839 struct nfsrv_descript nfsd, *nd = &nfsd; 6840 nfsattrbit_t hints; 6841 int error; 6842 6843 NFSZERO_ATTRBIT(&hints); 6844 if (advise == POSIX_FADV_WILLNEED) 6845 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED); 6846 else if (advise == POSIX_FADV_DONTNEED) 6847 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED); 6848 else 6849 return (0); 6850 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp); 6851 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO); 6852 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER); 6853 txdr_hyper(offset, tl); 6854 tl += 2; 6855 txdr_hyper(cnt, tl); 6856 nfsrv_putattrbit(nd, &hints); 6857 error = nfscl_request(nd, vp, p, cred, NULL); 6858 if (error != 0) 6859 return (error); 6860 if (nd->nd_repstat != 0) 6861 error = nd->nd_repstat; 6862 m_freem(nd->nd_mrep); 6863 return (error); 6864 } 6865 6866 #ifdef notyet 6867 /* 6868 * NFS advise rpc to a NFSv4.2 DS. 6869 */ 6870 static int 6871 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise, 6872 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers, 6873 struct ucred *cred, NFSPROC_T *p) 6874 { 6875 uint32_t *tl; 6876 struct nfsrv_descript nfsd, *nd = &nfsd; 6877 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6878 struct nfssockreq *nrp; 6879 nfsattrbit_t hints; 6880 int error; 6881 6882 /* For NFS DSs prior to NFSv4.2, just return OK. */ 6883 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION) 6884 return (0); 6885 NFSZERO_ATTRBIT(&hints); 6886 if (advise == POSIX_FADV_WILLNEED) 6887 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED); 6888 else if (advise == POSIX_FADV_DONTNEED) 6889 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED); 6890 else 6891 return (0); 6892 nd->nd_mrep = NULL; 6893 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh, 6894 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6895 vers = NFS_VER4; 6896 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers, 6897 minorvers); 6898 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO); 6899 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 6900 txdr_hyper(offset, tl); 6901 tl += 2; 6902 *tl = txdr_unsigned(cnt); 6903 nfsrv_putattrbit(nd, &hints); 6904 nrp = dsp->nfsclds_sockp; 6905 if (nrp == NULL) 6906 /* If NULL, use the MDS socket. */ 6907 nrp = &nmp->nm_sockreq; 6908 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6909 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6910 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error, 6911 nd->nd_repstat); 6912 if (error != 0) 6913 return (error); 6914 if (nd->nd_repstat != 0) 6915 error = nd->nd_repstat; 6916 m_freem(nd->nd_mrep); 6917 return (error); 6918 } 6919 6920 /* 6921 * Start up the thread that will execute nfsrpc_commitds(). 6922 */ 6923 static void 6924 start_adviseds(void *arg, int pending) 6925 { 6926 struct nfsclwritedsdorpc *drpc; 6927 6928 drpc = (struct nfsclwritedsdorpc *)arg; 6929 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len, 6930 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, 6931 drpc->cred, drpc->p); 6932 drpc->done = 1; 6933 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err); 6934 } 6935 6936 /* 6937 * Set up the commit DS mirror call for the pNFS I/O thread. 6938 */ 6939 static int 6940 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise, 6941 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers, 6942 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6943 { 6944 int error, ret; 6945 6946 error = 0; 6947 drpc->done = 0; 6948 drpc->vp = vp; 6949 drpc->off = offset; 6950 drpc->len = cnt; 6951 drpc->advise = advise; 6952 drpc->dsp = dsp; 6953 drpc->fhp = fhp; 6954 drpc->vers = vers; 6955 drpc->minorvers = minorvers; 6956 drpc->cred = cred; 6957 drpc->p = p; 6958 drpc->inprog = 0; 6959 ret = EIO; 6960 if (nfs_pnfsiothreads != 0) { 6961 ret = nfs_pnfsio(start_adviseds, drpc); 6962 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret); 6963 } 6964 if (ret != 0) 6965 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers, 6966 minorvers, cred, p); 6967 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error); 6968 return (error); 6969 } 6970 #endif /* notyet */ 6971 6972 /* 6973 * Do the Allocate operation, retrying for recovery. 6974 */ 6975 APPLESTATIC int 6976 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap, 6977 int *attrflagp, struct ucred *cred, NFSPROC_T *p, void *stuff) 6978 { 6979 int error, expireret = 0, retrycnt, nostateid; 6980 uint32_t clidrev = 0; 6981 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6982 struct nfsfh *nfhp = NULL; 6983 nfsv4stateid_t stateid; 6984 off_t tmp_off; 6985 void *lckp; 6986 6987 if (len < 0) 6988 return (EINVAL); 6989 if (len == 0) 6990 return (0); 6991 tmp_off = off + len; 6992 NFSLOCKMNT(nmp); 6993 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) { 6994 NFSUNLOCKMNT(nmp); 6995 return (EFBIG); 6996 } 6997 if (nmp->nm_clp != NULL) 6998 clidrev = nmp->nm_clp->nfsc_clientidrev; 6999 NFSUNLOCKMNT(nmp); 7000 nfhp = VTONFS(vp)->n_fhp; 7001 retrycnt = 0; 7002 do { 7003 lckp = NULL; 7004 nostateid = 0; 7005 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 7006 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp); 7007 if (stateid.other[0] == 0 && stateid.other[1] == 0 && 7008 stateid.other[2] == 0) { 7009 nostateid = 1; 7010 NFSCL_DEBUG(1, "stateid0 in allocate\n"); 7011 } 7012 7013 /* 7014 * Not finding a stateid should probably never happen, 7015 * but just return an error for this case. 7016 */ 7017 if (nostateid != 0) 7018 error = EIO; 7019 else 7020 error = nfsrpc_allocaterpc(vp, off, len, &stateid, 7021 nap, attrflagp, cred, p, stuff); 7022 if (error == NFSERR_STALESTATEID) 7023 nfscl_initiate_recovery(nmp->nm_clp); 7024 if (lckp != NULL) 7025 nfscl_lockderef(lckp); 7026 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 7027 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 7028 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 7029 (void) nfs_catnap(PZERO, error, "nfs_allocate"); 7030 } else if ((error == NFSERR_EXPIRED || 7031 error == NFSERR_BADSTATEID) && clidrev != 0) { 7032 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 7033 } 7034 retrycnt++; 7035 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 7036 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 7037 error == NFSERR_STALEDONTRECOVER || 7038 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 7039 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 7040 expireret == 0 && clidrev != 0 && retrycnt < 4)); 7041 if (error != 0 && retrycnt >= 4) 7042 error = EIO; 7043 return (error); 7044 } 7045 7046 /* 7047 * The allocate RPC. 7048 */ 7049 static int 7050 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp, 7051 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p, 7052 void *stuff) 7053 { 7054 uint32_t *tl; 7055 int error; 7056 struct nfsrv_descript nfsd; 7057 struct nfsrv_descript *nd = &nfsd; 7058 nfsattrbit_t attrbits; 7059 7060 *attrflagp = 0; 7061 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp); 7062 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 7063 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 7064 txdr_hyper(off, tl); tl += 2; 7065 txdr_hyper(len, tl); tl += 2; 7066 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7067 NFSGETATTR_ATTRBIT(&attrbits); 7068 nfsrv_putattrbit(nd, &attrbits); 7069 error = nfscl_request(nd, vp, p, cred, stuff); 7070 if (error != 0) 7071 return (error); 7072 if (nd->nd_repstat == 0) { 7073 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7074 error = nfsm_loadattr(nd, nap); 7075 if (error == 0) 7076 *attrflagp = NFS_LATTR_NOSHRINK; 7077 } else 7078 error = nd->nd_repstat; 7079 nfsmout: 7080 m_freem(nd->nd_mrep); 7081 return (error); 7082 } 7083 7084 /* 7085 * Set up the XDR arguments for the LayoutGet operation. 7086 */ 7087 static void 7088 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset, 7089 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype, 7090 int layoutlen, int usecurstateid) 7091 { 7092 uint32_t *tl; 7093 7094 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER + 7095 NFSX_STATEID); 7096 *tl++ = newnfs_false; /* Don't signal availability. */ 7097 *tl++ = txdr_unsigned(layouttype); 7098 *tl++ = txdr_unsigned(iomode); 7099 txdr_hyper(offset, tl); 7100 tl += 2; 7101 txdr_hyper(len, tl); 7102 tl += 2; 7103 txdr_hyper(minlen, tl); 7104 tl += 2; 7105 if (usecurstateid != 0) { 7106 /* Special stateid for Current stateid. */ 7107 *tl++ = txdr_unsigned(1); 7108 *tl++ = 0; 7109 *tl++ = 0; 7110 *tl++ = 0; 7111 } else { 7112 *tl++ = txdr_unsigned(stateidp->seqid); 7113 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid); 7114 *tl++ = stateidp->other[0]; 7115 *tl++ = stateidp->other[1]; 7116 *tl++ = stateidp->other[2]; 7117 } 7118 *tl = txdr_unsigned(layoutlen); 7119 } 7120 7121 /* 7122 * Parse the reply for a successful LayoutGet operation. 7123 */ 7124 static int 7125 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd, 7126 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp) 7127 { 7128 uint32_t *tl; 7129 struct nfsclflayout *flp, *prevflp, *tflp; 7130 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen; 7131 int m, mirrorcnt; 7132 uint64_t retlen, off; 7133 struct nfsfh *nfhp; 7134 uint8_t *cp; 7135 uid_t user; 7136 gid_t grp; 7137 7138 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n"); 7139 error = 0; 7140 flp = NULL; 7141 gotiomode = -1; 7142 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID); 7143 if (*tl++ != 0) 7144 *retonclosep = 1; 7145 else 7146 *retonclosep = 0; 7147 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++); 7148 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep, 7149 (int)stateidp->seqid); 7150 stateidp->other[0] = *tl++; 7151 stateidp->other[1] = *tl++; 7152 stateidp->other[2] = *tl++; 7153 cnt = fxdr_unsigned(int, *tl); 7154 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt); 7155 if (cnt <= 0 || cnt > 10000) { 7156 /* Don't accept more than 10000 layouts in reply. */ 7157 error = NFSERR_BADXDR; 7158 goto nfsmout; 7159 } 7160 for (i = 0; i < cnt; i++) { 7161 /* Dissect to the layout type. */ 7162 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 7163 3 * NFSX_UNSIGNED); 7164 off = fxdr_hyper(tl); tl += 2; 7165 retlen = fxdr_hyper(tl); tl += 2; 7166 iomode = fxdr_unsigned(int, *tl++); 7167 laytype = fxdr_unsigned(int, *tl); 7168 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype, 7169 (uintmax_t)off, (uintmax_t)retlen, iomode); 7170 /* Ignore length of layout body for now. */ 7171 if (laytype == NFSLAYOUT_NFSV4_1_FILES) { 7172 /* Parse the File layout up to fhcnt. */ 7173 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + 7174 NFSX_HYPER + NFSX_V4DEVICEID); 7175 fhcnt = fxdr_unsigned(int, *(tl + 4 + 7176 NFSX_V4DEVICEID / NFSX_UNSIGNED)); 7177 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt); 7178 if (fhcnt < 0 || fhcnt > 100) { 7179 /* Don't accept more than 100 file handles. */ 7180 error = NFSERR_BADXDR; 7181 goto nfsmout; 7182 } 7183 if (fhcnt > 0) 7184 flp = malloc(sizeof(*flp) + fhcnt * 7185 sizeof(struct nfsfh *), M_NFSFLAYOUT, 7186 M_WAITOK); 7187 else 7188 flp = malloc(sizeof(*flp), M_NFSFLAYOUT, 7189 M_WAITOK); 7190 flp->nfsfl_flags = NFSFL_FILE; 7191 flp->nfsfl_fhcnt = 0; 7192 flp->nfsfl_devp = NULL; 7193 flp->nfsfl_off = off; 7194 if (flp->nfsfl_off + retlen < flp->nfsfl_off) 7195 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off; 7196 else 7197 flp->nfsfl_end = flp->nfsfl_off + retlen; 7198 flp->nfsfl_iomode = iomode; 7199 if (gotiomode == -1) 7200 gotiomode = flp->nfsfl_iomode; 7201 /* Ignore layout body length for now. */ 7202 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID); 7203 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 7204 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++); 7205 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util); 7206 mtx_lock(&nmp->nm_mtx); 7207 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util & 7208 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0) 7209 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS; 7210 mtx_unlock(&nmp->nm_mtx); 7211 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++); 7212 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2; 7213 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n", 7214 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff); 7215 for (j = 0; j < fhcnt; j++) { 7216 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7217 nfhlen = fxdr_unsigned(int, *tl); 7218 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) { 7219 error = NFSERR_BADXDR; 7220 goto nfsmout; 7221 } 7222 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1, 7223 M_NFSFH, M_WAITOK); 7224 flp->nfsfl_fh[j] = nfhp; 7225 flp->nfsfl_fhcnt++; 7226 nfhp->nfh_len = nfhlen; 7227 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen)); 7228 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen); 7229 } 7230 } else if (laytype == NFSLAYOUT_FLEXFILE) { 7231 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + 7232 NFSX_HYPER); 7233 mirrorcnt = fxdr_unsigned(int, *(tl + 2)); 7234 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt); 7235 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) { 7236 error = NFSERR_BADXDR; 7237 goto nfsmout; 7238 } 7239 flp = malloc(sizeof(*flp) + mirrorcnt * 7240 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK); 7241 flp->nfsfl_flags = NFSFL_FLEXFILE; 7242 flp->nfsfl_mirrorcnt = mirrorcnt; 7243 for (j = 0; j < mirrorcnt; j++) 7244 flp->nfsfl_ffm[j].devp = NULL; 7245 flp->nfsfl_off = off; 7246 if (flp->nfsfl_off + retlen < flp->nfsfl_off) 7247 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off; 7248 else 7249 flp->nfsfl_end = flp->nfsfl_off + retlen; 7250 flp->nfsfl_iomode = iomode; 7251 if (gotiomode == -1) 7252 gotiomode = flp->nfsfl_iomode; 7253 flp->nfsfl_stripeunit = fxdr_hyper(tl); 7254 NFSCL_DEBUG(4, "stripeunit=%ju\n", 7255 (uintmax_t)flp->nfsfl_stripeunit); 7256 for (j = 0; j < mirrorcnt; j++) { 7257 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7258 k = fxdr_unsigned(int, *tl); 7259 if (k < 1 || k > 128) { 7260 error = NFSERR_BADXDR; 7261 goto nfsmout; 7262 } 7263 NFSCL_DEBUG(4, "servercnt=%d\n", k); 7264 for (l = 0; l < k; l++) { 7265 NFSM_DISSECT(tl, uint32_t *, 7266 NFSX_V4DEVICEID + NFSX_STATEID + 7267 2 * NFSX_UNSIGNED); 7268 if (l == 0) { 7269 /* Just use the first server. */ 7270 NFSBCOPY(tl, 7271 flp->nfsfl_ffm[j].dev, 7272 NFSX_V4DEVICEID); 7273 tl += (NFSX_V4DEVICEID / 7274 NFSX_UNSIGNED); 7275 tl++; 7276 flp->nfsfl_ffm[j].st.seqid = 7277 *tl++; 7278 flp->nfsfl_ffm[j].st.other[0] = 7279 *tl++; 7280 flp->nfsfl_ffm[j].st.other[1] = 7281 *tl++; 7282 flp->nfsfl_ffm[j].st.other[2] = 7283 *tl++; 7284 NFSCL_DEBUG(4, "st.seqid=%u " 7285 "st.o0=0x%x st.o1=0x%x " 7286 "st.o2=0x%x\n", 7287 flp->nfsfl_ffm[j].st.seqid, 7288 flp->nfsfl_ffm[j].st.other[0], 7289 flp->nfsfl_ffm[j].st.other[1], 7290 flp->nfsfl_ffm[j].st.other[2]); 7291 } else 7292 tl += ((NFSX_V4DEVICEID + 7293 NFSX_STATEID + 7294 NFSX_UNSIGNED) / 7295 NFSX_UNSIGNED); 7296 fhcnt = fxdr_unsigned(int, *tl); 7297 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt); 7298 if (fhcnt < 1 || 7299 fhcnt > NFSDEV_MAXVERS) { 7300 error = NFSERR_BADXDR; 7301 goto nfsmout; 7302 } 7303 for (m = 0; m < fhcnt; m++) { 7304 NFSM_DISSECT(tl, uint32_t *, 7305 NFSX_UNSIGNED); 7306 nfhlen = fxdr_unsigned(int, 7307 *tl); 7308 NFSCL_DEBUG(4, "nfhlen=%d\n", 7309 nfhlen); 7310 if (nfhlen <= 0 || nfhlen > 7311 NFSX_V4FHMAX) { 7312 error = NFSERR_BADXDR; 7313 goto nfsmout; 7314 } 7315 NFSM_DISSECT(cp, uint8_t *, 7316 NFSM_RNDUP(nfhlen)); 7317 if (l == 0) { 7318 flp->nfsfl_ffm[j].fhcnt 7319 = fhcnt; 7320 nfhp = malloc( 7321 sizeof(*nfhp) + 7322 nfhlen - 1, M_NFSFH, 7323 M_WAITOK); 7324 flp->nfsfl_ffm[j].fh[m] 7325 = nfhp; 7326 nfhp->nfh_len = nfhlen; 7327 NFSBCOPY(cp, 7328 nfhp->nfh_fh, 7329 nfhlen); 7330 NFSCL_DEBUG(4, 7331 "got fh\n"); 7332 } 7333 } 7334 /* Now, get the ffsd_user/ffds_group. */ 7335 error = nfsrv_parseug(nd, 0, &user, 7336 &grp, curthread); 7337 NFSCL_DEBUG(4, "after parseu=%d\n", 7338 error); 7339 if (error == 0) 7340 error = nfsrv_parseug(nd, 1, 7341 &user, &grp, curthread); 7342 NFSCL_DEBUG(4, "aft parseg=%d\n", 7343 grp); 7344 if (error != 0) 7345 goto nfsmout; 7346 NFSCL_DEBUG(4, "user=%d group=%d\n", 7347 user, grp); 7348 if (l == 0) { 7349 flp->nfsfl_ffm[j].user = user; 7350 flp->nfsfl_ffm[j].group = grp; 7351 NFSCL_DEBUG(4, 7352 "usr=%d grp=%d\n", user, 7353 grp); 7354 } 7355 } 7356 } 7357 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7358 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++); 7359 #ifdef notnow 7360 /* 7361 * At this time, there is no flag. 7362 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be 7363 * added, or it may never exist? 7364 */ 7365 mtx_lock(&nmp->nm_mtx); 7366 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags & 7367 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0) 7368 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS; 7369 mtx_unlock(&nmp->nm_mtx); 7370 #endif 7371 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl); 7372 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n", 7373 flp->nfsfl_fflags, flp->nfsfl_statshint); 7374 } else { 7375 error = NFSERR_BADXDR; 7376 goto nfsmout; 7377 } 7378 if (flp->nfsfl_iomode == gotiomode) { 7379 /* Keep the list in increasing offset order. */ 7380 tflp = LIST_FIRST(flhp); 7381 prevflp = NULL; 7382 while (tflp != NULL && 7383 tflp->nfsfl_off < flp->nfsfl_off) { 7384 prevflp = tflp; 7385 tflp = LIST_NEXT(tflp, nfsfl_list); 7386 } 7387 if (prevflp == NULL) 7388 LIST_INSERT_HEAD(flhp, flp, nfsfl_list); 7389 else 7390 LIST_INSERT_AFTER(prevflp, flp, 7391 nfsfl_list); 7392 NFSCL_DEBUG(4, "flp inserted\n"); 7393 } else { 7394 printf("nfscl_layoutget(): got wrong iomode\n"); 7395 nfscl_freeflayout(flp); 7396 } 7397 flp = NULL; 7398 } 7399 nfsmout: 7400 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error); 7401 if (error != 0 && flp != NULL) 7402 nfscl_freeflayout(flp); 7403 return (error); 7404 } 7405 7406 /* 7407 * Parse a user/group digit string. 7408 */ 7409 static int 7410 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp, 7411 NFSPROC_T *p) 7412 { 7413 uint32_t *tl; 7414 char *cp, *str, str0[NFSV4_SMALLSTR + 1]; 7415 uint32_t len = 0; 7416 int error = 0; 7417 7418 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7419 len = fxdr_unsigned(uint32_t, *tl); 7420 str = NULL; 7421 if (len > NFSV4_OPAQUELIMIT) { 7422 error = NFSERR_BADXDR; 7423 goto nfsmout; 7424 } 7425 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len); 7426 if (len == 0) { 7427 if (dogrp != 0) 7428 *gidp = GID_NOGROUP; 7429 else 7430 *uidp = UID_NOBODY; 7431 return (0); 7432 } 7433 if (len > NFSV4_SMALLSTR) 7434 str = malloc(len + 1, M_TEMP, M_WAITOK); 7435 else 7436 str = str0; 7437 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len)); 7438 NFSBCOPY(cp, str, len); 7439 str[len] = '\0'; 7440 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str); 7441 if (dogrp != 0) 7442 error = nfsv4_strtogid(nd, str, len, gidp); 7443 else 7444 error = nfsv4_strtouid(nd, str, len, uidp); 7445 nfsmout: 7446 if (len > NFSV4_SMALLSTR) 7447 free(str, M_TEMP); 7448 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error); 7449 return (error); 7450 } 7451 7452 /* 7453 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(), 7454 * so that it does both an Open and a Layoutget. 7455 */ 7456 static int 7457 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, 7458 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode, 7459 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp, 7460 struct ucred *cred, NFSPROC_T *p) 7461 { 7462 struct nfscllayout *lyp; 7463 struct nfsclflayout *flp; 7464 struct nfsclflayouthead flh; 7465 int error, islocked, layoutlen, recalled, retonclose, usecurstateid; 7466 int layouttype, laystat; 7467 nfsv4stateid_t stateid; 7468 struct nfsclsession *tsep; 7469 7470 error = 0; 7471 if (NFSHASFLEXFILE(nmp)) 7472 layouttype = NFSLAYOUT_FLEXFILE; 7473 else 7474 layouttype = NFSLAYOUT_NFSV4_1_FILES; 7475 /* 7476 * If lyp is returned non-NULL, there will be a refcnt (shared lock) 7477 * on it, iff flp != NULL or a lock (exclusive lock) on it iff 7478 * flp == NULL. 7479 */ 7480 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp, 7481 &recalled); 7482 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp); 7483 if (lyp == NULL) 7484 islocked = 0; 7485 else if (flp != NULL) 7486 islocked = 1; 7487 else 7488 islocked = 2; 7489 if ((lyp == NULL || flp == NULL) && recalled == 0) { 7490 LIST_INIT(&flh); 7491 tsep = nfsmnt_mdssession(nmp); 7492 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 7493 3 * NFSX_UNSIGNED); 7494 if (lyp == NULL) 7495 usecurstateid = 1; 7496 else { 7497 usecurstateid = 0; 7498 stateid.seqid = lyp->nfsly_stateid.seqid; 7499 stateid.other[0] = lyp->nfsly_stateid.other[0]; 7500 stateid.other[1] = lyp->nfsly_stateid.other[1]; 7501 stateid.other[2] = lyp->nfsly_stateid.other[2]; 7502 } 7503 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen, 7504 newfhp, newfhlen, mode, op, name, namelen, 7505 dpp, &stateid, usecurstateid, layouttype, layoutlen, 7506 &retonclose, &flh, &laystat, cred, p); 7507 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n", 7508 laystat, error); 7509 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen, 7510 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat, 7511 &islocked, cred, p); 7512 } else 7513 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen, 7514 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0); 7515 if (islocked == 2) 7516 nfscl_rellayout(lyp, 1); 7517 else if (islocked == 1) 7518 nfscl_rellayout(lyp, 0); 7519 return (error); 7520 } 7521 7522 /* 7523 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS 7524 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are 7525 * handled by nfsrpc_openrpc(). 7526 * For the case where op == NULL, dvp is the directory. When op != NULL, it 7527 * can be NULL. 7528 */ 7529 static int 7530 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, 7531 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode, 7532 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp, 7533 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype, 7534 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp, 7535 int *laystatp, struct ucred *cred, NFSPROC_T *p) 7536 { 7537 uint32_t *tl; 7538 struct nfsrv_descript nfsd, *nd = &nfsd; 7539 struct nfscldeleg *ndp = NULL; 7540 struct nfsvattr nfsva; 7541 struct nfsclsession *tsep; 7542 uint32_t rflags, deleg; 7543 nfsattrbit_t attrbits; 7544 int error, ret, acesize, limitby, iomode; 7545 7546 *dpp = NULL; 7547 *laystatp = ENXIO; 7548 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL, 7549 0, 0); 7550 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED); 7551 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 7552 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH); 7553 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 7554 tsep = nfsmnt_mdssession(nmp); 7555 *tl++ = tsep->nfsess_clientid.lval[0]; 7556 *tl = tsep->nfsess_clientid.lval[1]; 7557 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN); 7558 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7559 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE); 7560 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 7561 nfsm_strtom(nd, name, namelen); 7562 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 7563 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7564 NFSZERO_ATTRBIT(&attrbits); 7565 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 7566 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 7567 nfsrv_putattrbit(nd, &attrbits); 7568 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 7569 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET); 7570 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0) 7571 iomode = NFSLAYOUTIOMODE_RW; 7572 else 7573 iomode = NFSLAYOUTIOMODE_READ; 7574 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp, 7575 layouttype, layoutlen, usecurstateid); 7576 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 7577 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 7578 if (error != 0) 7579 return (error); 7580 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 7581 if (nd->nd_repstat != 0) 7582 *laystatp = nd->nd_repstat; 7583 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7584 /* ND_NOMOREDATA will be set if the Open operation failed. */ 7585 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7586 6 * NFSX_UNSIGNED); 7587 op->nfso_stateid.seqid = *tl++; 7588 op->nfso_stateid.other[0] = *tl++; 7589 op->nfso_stateid.other[1] = *tl++; 7590 op->nfso_stateid.other[2] = *tl; 7591 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 7592 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 7593 if (error != 0) 7594 goto nfsmout; 7595 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 7596 deleg = fxdr_unsigned(u_int32_t, *tl); 7597 if (deleg == NFSV4OPEN_DELEGATEREAD || 7598 deleg == NFSV4OPEN_DELEGATEWRITE) { 7599 if (!(op->nfso_own->nfsow_clp->nfsc_flags & 7600 NFSCLFLAGS_FIRSTDELEG)) 7601 op->nfso_own->nfsow_clp->nfsc_flags |= 7602 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 7603 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen, 7604 M_NFSCLDELEG, M_WAITOK); 7605 LIST_INIT(&ndp->nfsdl_owner); 7606 LIST_INIT(&ndp->nfsdl_lock); 7607 ndp->nfsdl_clp = op->nfso_own->nfsow_clp; 7608 ndp->nfsdl_fhlen = newfhlen; 7609 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen); 7610 newnfs_copyincred(cred, &ndp->nfsdl_cred); 7611 nfscl_lockinit(&ndp->nfsdl_rwlock); 7612 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7613 NFSX_UNSIGNED); 7614 ndp->nfsdl_stateid.seqid = *tl++; 7615 ndp->nfsdl_stateid.other[0] = *tl++; 7616 ndp->nfsdl_stateid.other[1] = *tl++; 7617 ndp->nfsdl_stateid.other[2] = *tl++; 7618 ret = fxdr_unsigned(int, *tl); 7619 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 7620 ndp->nfsdl_flags = NFSCLDL_WRITE; 7621 /* 7622 * Indicates how much the file can grow. 7623 */ 7624 NFSM_DISSECT(tl, u_int32_t *, 7625 3 * NFSX_UNSIGNED); 7626 limitby = fxdr_unsigned(int, *tl++); 7627 switch (limitby) { 7628 case NFSV4OPEN_LIMITSIZE: 7629 ndp->nfsdl_sizelimit = fxdr_hyper(tl); 7630 break; 7631 case NFSV4OPEN_LIMITBLOCKS: 7632 ndp->nfsdl_sizelimit = 7633 fxdr_unsigned(u_int64_t, *tl++); 7634 ndp->nfsdl_sizelimit *= 7635 fxdr_unsigned(u_int64_t, *tl); 7636 break; 7637 default: 7638 error = NFSERR_BADXDR; 7639 goto nfsmout; 7640 }; 7641 } else 7642 ndp->nfsdl_flags = NFSCLDL_READ; 7643 if (ret != 0) 7644 ndp->nfsdl_flags |= NFSCLDL_RECALL; 7645 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret, 7646 &acesize, p); 7647 if (error != 0) 7648 goto nfsmout; 7649 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 7650 error = NFSERR_BADXDR; 7651 goto nfsmout; 7652 } 7653 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 || 7654 nfscl_assumeposixlocks) 7655 op->nfso_posixlock = 1; 7656 else 7657 op->nfso_posixlock = 0; 7658 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7659 /* If the 2nd element == NFS_OK, the Getattr succeeded. */ 7660 if (*++tl == 0) { 7661 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 7662 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 7663 NULL, NULL, NULL, p, cred); 7664 if (error != 0) 7665 goto nfsmout; 7666 if (ndp != NULL) { 7667 ndp->nfsdl_change = nfsva.na_filerev; 7668 ndp->nfsdl_modtime = nfsva.na_mtime; 7669 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET; 7670 *dpp = ndp; 7671 ndp = NULL; 7672 } 7673 /* 7674 * At this point, the Open has succeeded, so set 7675 * nd_repstat = NFS_OK. If the Layoutget failed, 7676 * this function just won't return a layout. 7677 */ 7678 if (nd->nd_repstat == 0) { 7679 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7680 *laystatp = fxdr_unsigned(int, *++tl); 7681 if (*laystatp == 0) { 7682 error = nfsrv_parselayoutget(nmp, nd, 7683 stateidp, retonclosep, flhp); 7684 if (error != 0) 7685 *laystatp = error; 7686 } 7687 } else 7688 nd->nd_repstat = 0; /* Return 0 for Open. */ 7689 } 7690 } 7691 if (nd->nd_repstat != 0 && error == 0) 7692 error = nd->nd_repstat; 7693 nfsmout: 7694 free(ndp, M_NFSCLDELEG); 7695 m_freem(nd->nd_mrep); 7696 return (error); 7697 } 7698 7699 /* 7700 * Similar nfsrpc_createv4(), but also does the LayoutGet operation. 7701 * Used only for mounts with pNFS enabled. 7702 */ 7703 static int 7704 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap, 7705 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 7706 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 7707 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 7708 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp, 7709 int usecurstateid, int layouttype, int layoutlen, int *retonclosep, 7710 struct nfsclflayouthead *flhp, int *laystatp) 7711 { 7712 uint32_t *tl; 7713 int error = 0, deleg, newone, ret, acesize, limitby; 7714 struct nfsrv_descript nfsd, *nd = &nfsd; 7715 struct nfsclopen *op; 7716 struct nfscldeleg *dp = NULL; 7717 struct nfsnode *np; 7718 struct nfsfh *nfhp; 7719 struct nfsclsession *tsep; 7720 nfsattrbit_t attrbits; 7721 nfsv4stateid_t stateid; 7722 struct nfsmount *nmp; 7723 7724 nmp = VFSTONFS(dvp->v_mount); 7725 np = VTONFS(dvp); 7726 *laystatp = ENXIO; 7727 *unlockedp = 0; 7728 *nfhpp = NULL; 7729 *dpp = NULL; 7730 *attrflagp = 0; 7731 *dattrflagp = 0; 7732 if (namelen > NFS_MAXNAMLEN) 7733 return (ENAMETOOLONG); 7734 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp); 7735 /* 7736 * For V4, this is actually an Open op. 7737 */ 7738 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 7739 *tl++ = txdr_unsigned(owp->nfsow_seqid); 7740 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 7741 NFSV4OPEN_ACCESSREAD); 7742 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE); 7743 tsep = nfsmnt_mdssession(nmp); 7744 *tl++ = tsep->nfsess_clientid.lval[0]; 7745 *tl = tsep->nfsess_clientid.lval[1]; 7746 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 7747 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7748 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE); 7749 if ((fmode & O_EXCL) != 0) { 7750 if (NFSHASSESSPERSIST(nmp)) { 7751 /* Use GUARDED for persistent sessions. */ 7752 *tl = txdr_unsigned(NFSCREATE_GUARDED); 7753 nfscl_fillsattr(nd, vap, dvp, 0, 0); 7754 } else { 7755 /* Otherwise, use EXCLUSIVE4_1. */ 7756 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41); 7757 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 7758 *tl++ = cverf.lval[0]; 7759 *tl = cverf.lval[1]; 7760 nfscl_fillsattr(nd, vap, dvp, 0, 0); 7761 } 7762 } else { 7763 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 7764 nfscl_fillsattr(nd, vap, dvp, 0, 0); 7765 } 7766 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 7767 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 7768 nfsm_strtom(nd, name, namelen); 7769 /* Get the new file's handle and attributes, plus save the FH. */ 7770 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 7771 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH); 7772 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 7773 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7774 NFSGETATTR_ATTRBIT(&attrbits); 7775 nfsrv_putattrbit(nd, &attrbits); 7776 /* Get the directory's post-op attributes. */ 7777 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 7778 *tl = txdr_unsigned(NFSV4OP_PUTFH); 7779 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0); 7780 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 7781 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7782 nfsrv_putattrbit(nd, &attrbits); 7783 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7784 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH); 7785 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET); 7786 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp, 7787 layouttype, layoutlen, usecurstateid); 7788 error = nfscl_request(nd, dvp, p, cred, dstuff); 7789 if (error != 0) 7790 return (error); 7791 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat, 7792 error); 7793 if (nd->nd_repstat != 0) 7794 *laystatp = nd->nd_repstat; 7795 NFSCL_INCRSEQID(owp->nfsow_seqid, nd); 7796 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7797 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n"); 7798 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7799 6 * NFSX_UNSIGNED); 7800 stateid.seqid = *tl++; 7801 stateid.other[0] = *tl++; 7802 stateid.other[1] = *tl++; 7803 stateid.other[2] = *tl; 7804 nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 7805 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 7806 deleg = fxdr_unsigned(int, *tl); 7807 if (deleg == NFSV4OPEN_DELEGATEREAD || 7808 deleg == NFSV4OPEN_DELEGATEWRITE) { 7809 if (!(owp->nfsow_clp->nfsc_flags & 7810 NFSCLFLAGS_FIRSTDELEG)) 7811 owp->nfsow_clp->nfsc_flags |= 7812 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 7813 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX, 7814 M_NFSCLDELEG, M_WAITOK); 7815 LIST_INIT(&dp->nfsdl_owner); 7816 LIST_INIT(&dp->nfsdl_lock); 7817 dp->nfsdl_clp = owp->nfsow_clp; 7818 newnfs_copyincred(cred, &dp->nfsdl_cred); 7819 nfscl_lockinit(&dp->nfsdl_rwlock); 7820 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7821 NFSX_UNSIGNED); 7822 dp->nfsdl_stateid.seqid = *tl++; 7823 dp->nfsdl_stateid.other[0] = *tl++; 7824 dp->nfsdl_stateid.other[1] = *tl++; 7825 dp->nfsdl_stateid.other[2] = *tl++; 7826 ret = fxdr_unsigned(int, *tl); 7827 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 7828 dp->nfsdl_flags = NFSCLDL_WRITE; 7829 /* 7830 * Indicates how much the file can grow. 7831 */ 7832 NFSM_DISSECT(tl, u_int32_t *, 7833 3 * NFSX_UNSIGNED); 7834 limitby = fxdr_unsigned(int, *tl++); 7835 switch (limitby) { 7836 case NFSV4OPEN_LIMITSIZE: 7837 dp->nfsdl_sizelimit = fxdr_hyper(tl); 7838 break; 7839 case NFSV4OPEN_LIMITBLOCKS: 7840 dp->nfsdl_sizelimit = 7841 fxdr_unsigned(u_int64_t, *tl++); 7842 dp->nfsdl_sizelimit *= 7843 fxdr_unsigned(u_int64_t, *tl); 7844 break; 7845 default: 7846 error = NFSERR_BADXDR; 7847 goto nfsmout; 7848 }; 7849 } else { 7850 dp->nfsdl_flags = NFSCLDL_READ; 7851 } 7852 if (ret != 0) 7853 dp->nfsdl_flags |= NFSCLDL_RECALL; 7854 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret, 7855 &acesize, p); 7856 if (error != 0) 7857 goto nfsmout; 7858 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 7859 error = NFSERR_BADXDR; 7860 goto nfsmout; 7861 } 7862 7863 /* Now, we should have the status for the SaveFH. */ 7864 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7865 if (*++tl == 0) { 7866 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n"); 7867 /* 7868 * Now, process the GetFH and Getattr for the newly 7869 * created file. nfscl_mtofh() will set 7870 * ND_NOMOREDATA if these weren't successful. 7871 */ 7872 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 7873 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error); 7874 if (error != 0) 7875 goto nfsmout; 7876 } else 7877 nd->nd_flag |= ND_NOMOREDATA; 7878 /* Now we have the PutFH and Getattr for the directory. */ 7879 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7880 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7881 if (*++tl != 0) 7882 nd->nd_flag |= ND_NOMOREDATA; 7883 else { 7884 NFSM_DISSECT(tl, uint32_t *, 2 * 7885 NFSX_UNSIGNED); 7886 if (*++tl != 0) 7887 nd->nd_flag |= ND_NOMOREDATA; 7888 } 7889 } 7890 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7891 /* Load the directory attributes. */ 7892 error = nfsm_loadattr(nd, dnap); 7893 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error); 7894 if (error != 0) 7895 goto nfsmout; 7896 *dattrflagp = 1; 7897 if (dp != NULL && *attrflagp != 0) { 7898 dp->nfsdl_change = nnap->na_filerev; 7899 dp->nfsdl_modtime = nnap->na_mtime; 7900 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 7901 } 7902 /* 7903 * We can now complete the Open state. 7904 */ 7905 nfhp = *nfhpp; 7906 if (dp != NULL) { 7907 dp->nfsdl_fhlen = nfhp->nfh_len; 7908 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, 7909 nfhp->nfh_len); 7910 } 7911 /* 7912 * Get an Open structure that will be 7913 * attached to the OpenOwner, acquired already. 7914 */ 7915 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 7916 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0, 7917 cred, p, NULL, &op, &newone, NULL, 0); 7918 if (error != 0) 7919 goto nfsmout; 7920 op->nfso_stateid = stateid; 7921 newnfs_copyincred(cred, &op->nfso_cred); 7922 7923 nfscl_openrelease(nmp, op, error, newone); 7924 *unlockedp = 1; 7925 7926 /* Now, handle the RestoreFH and LayoutGet. */ 7927 if (nd->nd_repstat == 0) { 7928 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED); 7929 *laystatp = fxdr_unsigned(int, *(tl + 3)); 7930 if (*laystatp == 0) { 7931 error = nfsrv_parselayoutget(nmp, nd, 7932 stateidp, retonclosep, flhp); 7933 if (error != 0) 7934 *laystatp = error; 7935 } 7936 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n", 7937 error); 7938 } else 7939 nd->nd_repstat = 0; 7940 } 7941 } 7942 if (nd->nd_repstat != 0 && error == 0) 7943 error = nd->nd_repstat; 7944 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION) 7945 nfscl_initiate_recovery(owp->nfsow_clp); 7946 nfsmout: 7947 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error); 7948 if (error == 0) 7949 *dpp = dp; 7950 else 7951 free(dp, M_NFSCLDELEG); 7952 m_freem(nd->nd_mrep); 7953 return (error); 7954 } 7955 7956 /* 7957 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case. 7958 */ 7959 static int 7960 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap, 7961 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 7962 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 7963 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 7964 int *dattrflagp, void *dstuff, int *unlockedp) 7965 { 7966 struct nfscllayout *lyp; 7967 struct nfsclflayouthead flh; 7968 struct nfsfh *nfhp; 7969 struct nfsclsession *tsep; 7970 struct nfsmount *nmp; 7971 nfsv4stateid_t stateid; 7972 int error, layoutlen, layouttype, retonclose, laystat; 7973 7974 error = 0; 7975 nmp = VFSTONFS(dvp->v_mount); 7976 if (NFSHASFLEXFILE(nmp)) 7977 layouttype = NFSLAYOUT_FLEXFILE; 7978 else 7979 layouttype = NFSLAYOUT_NFSV4_1_FILES; 7980 LIST_INIT(&flh); 7981 tsep = nfsmnt_mdssession(nmp); 7982 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED); 7983 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode, 7984 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 7985 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose, 7986 &flh, &laystat); 7987 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n", 7988 laystat, error); 7989 lyp = NULL; 7990 if (laystat == 0) { 7991 nfhp = *nfhpp; 7992 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh, 7993 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh, 7994 layouttype, laystat, NULL, cred, p); 7995 } else 7996 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid, 7997 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL, 7998 cred, p); 7999 if (laystat == 0) 8000 nfscl_rellayout(lyp, 0); 8001 return (error); 8002 } 8003 8004 /* 8005 * Process the results of a layoutget() operation. 8006 */ 8007 static int 8008 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp, 8009 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit, 8010 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype, 8011 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p) 8012 { 8013 struct nfsclflayout *tflp; 8014 struct nfscldevinfo *dip; 8015 uint8_t *dev; 8016 int i, mirrorcnt; 8017 8018 if (laystat == NFSERR_UNKNLAYOUTTYPE) { 8019 NFSLOCKMNT(nmp); 8020 if (!NFSHASFLEXFILE(nmp)) { 8021 /* Switch to using Flex File Layout. */ 8022 nmp->nm_state |= NFSSTA_FLEXFILE; 8023 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 8024 /* Disable pNFS. */ 8025 NFSCL_DEBUG(1, "disable PNFS\n"); 8026 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE); 8027 } 8028 NFSUNLOCKMNT(nmp); 8029 } 8030 if (laystat == 0) { 8031 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n"); 8032 LIST_FOREACH(tflp, flhp, nfsfl_list) { 8033 if (layouttype == NFSLAYOUT_FLEXFILE) 8034 mirrorcnt = tflp->nfsfl_mirrorcnt; 8035 else 8036 mirrorcnt = 1; 8037 for (i = 0; i < mirrorcnt; i++) { 8038 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp); 8039 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat); 8040 if (laystat != 0) { 8041 if (layouttype == NFSLAYOUT_FLEXFILE) 8042 dev = tflp->nfsfl_ffm[i].dev; 8043 else 8044 dev = tflp->nfsfl_dev; 8045 laystat = nfsrpc_getdeviceinfo(nmp, dev, 8046 layouttype, notifybit, &dip, cred, 8047 p); 8048 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n", 8049 laystat); 8050 if (laystat != 0) 8051 goto out; 8052 laystat = nfscl_adddevinfo(nmp, dip, i, 8053 tflp); 8054 if (laystat != 0) 8055 printf("nfsrpc_layoutgetresout" 8056 ": cannot add\n"); 8057 } 8058 } 8059 } 8060 } 8061 out: 8062 if (laystat == 0) { 8063 /* 8064 * nfscl_layout() always returns with the nfsly_lock 8065 * set to a refcnt (shared lock). 8066 * Passing in dvp is sufficient, since it is only used to 8067 * get the fsid for the file system. 8068 */ 8069 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp, 8070 layouttype, retonclose, flhp, lypp, cred, p); 8071 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n", 8072 laystat); 8073 if (laystat == 0 && islockedp != NULL) 8074 *islockedp = 1; 8075 } 8076 return (laystat); 8077 } 8078 8079 /* 8080 * nfs copy_file_range operation. 8081 */ 8082 APPLESTATIC int 8083 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp, 8084 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp, 8085 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap, 8086 struct ucred *cred, bool consecutive, bool *must_commitp) 8087 { 8088 int commit, error, expireret = 0, retrycnt; 8089 u_int32_t clidrev = 0; 8090 struct nfsmount *nmp = VFSTONFS(vnode_mount(invp)); 8091 struct nfsfh *innfhp = NULL, *outnfhp = NULL; 8092 nfsv4stateid_t instateid, outstateid; 8093 void *inlckp, *outlckp; 8094 8095 if (nmp->nm_clp != NULL) 8096 clidrev = nmp->nm_clp->nfsc_clientidrev; 8097 innfhp = VTONFS(invp)->n_fhp; 8098 outnfhp = VTONFS(outvp)->n_fhp; 8099 retrycnt = 0; 8100 do { 8101 /* Get both stateids. */ 8102 inlckp = NULL; 8103 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len, 8104 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid, 8105 &inlckp); 8106 outlckp = NULL; 8107 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len, 8108 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid, 8109 &outlckp); 8110 8111 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp, 8112 &instateid, &outstateid, innap, inattrflagp, outnap, 8113 outattrflagp, consecutive, &commit, cred, curthread); 8114 if (error == 0) { 8115 if (commit != NFSWRITE_FILESYNC) 8116 *must_commitp = true; 8117 *inoffp += *lenp; 8118 *outoffp += *lenp; 8119 } else if (error == NFSERR_STALESTATEID) 8120 nfscl_initiate_recovery(nmp->nm_clp); 8121 if (inlckp != NULL) 8122 nfscl_lockderef(inlckp); 8123 if (outlckp != NULL) 8124 nfscl_lockderef(outlckp); 8125 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8126 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8127 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 8128 (void) nfs_catnap(PZERO, error, "nfs_cfr"); 8129 } else if ((error == NFSERR_EXPIRED || 8130 error == NFSERR_BADSTATEID) && clidrev != 0) { 8131 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, 8132 curthread); 8133 } 8134 retrycnt++; 8135 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 8136 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 8137 error == NFSERR_STALEDONTRECOVER || 8138 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 8139 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 8140 expireret == 0 && clidrev != 0 && retrycnt < 4)); 8141 if (error != 0 && (retrycnt >= 4 || 8142 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 8143 error == NFSERR_STALEDONTRECOVER)) 8144 error = EIO; 8145 return (error); 8146 } 8147 8148 /* 8149 * The copy RPC. 8150 */ 8151 static int 8152 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff, 8153 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp, 8154 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap, 8155 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred, 8156 NFSPROC_T *p) 8157 { 8158 uint32_t *tl; 8159 int error; 8160 struct nfsrv_descript nfsd; 8161 struct nfsrv_descript *nd = &nfsd; 8162 struct nfsmount *nmp; 8163 nfsattrbit_t attrbits; 8164 uint64_t len; 8165 8166 nmp = VFSTONFS(outvp->v_mount); 8167 *inattrflagp = *outattrflagp = 0; 8168 *commitp = NFSWRITE_UNSTABLE; 8169 len = *lenp; 8170 *lenp = 0; 8171 if (len > nfs_maxcopyrange) 8172 len = nfs_maxcopyrange; 8173 NFSCL_REQSTART(nd, NFSPROC_COPY, invp); 8174 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8175 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8176 NFSGETATTR_ATTRBIT(&attrbits); 8177 nfsrv_putattrbit(nd, &attrbits); 8178 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8179 *tl = txdr_unsigned(NFSV4OP_PUTFH); 8180 nfsm_fhtom(nd, VTONFS(outvp)->n_fhp->nfh_fh, 8181 VTONFS(outvp)->n_fhp->nfh_len, 0); 8182 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8183 *tl = txdr_unsigned(NFSV4OP_COPY); 8184 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID); 8185 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID); 8186 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED); 8187 txdr_hyper(inoff, tl); tl += 2; 8188 txdr_hyper(outoff, tl); tl += 2; 8189 txdr_hyper(len, tl); tl += 2; 8190 if (consecutive) 8191 *tl++ = newnfs_true; 8192 else 8193 *tl++ = newnfs_false; 8194 *tl++ = newnfs_true; 8195 *tl++ = 0; 8196 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8197 NFSWRITEGETATTR_ATTRBIT(&attrbits); 8198 nfsrv_putattrbit(nd, &attrbits); 8199 error = nfscl_request(nd, invp, p, cred, NULL); 8200 if (error != 0) 8201 return (error); 8202 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8203 /* Get the input file's attributes. */ 8204 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8205 if (*(tl + 1) == 0) { 8206 error = nfsm_loadattr(nd, innap); 8207 if (error != 0) 8208 goto nfsmout; 8209 *inattrflagp = 1; 8210 } else 8211 nd->nd_flag |= ND_NOMOREDATA; 8212 } 8213 /* Skip over return stat for PutFH. */ 8214 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8215 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8216 if (*++tl != 0) 8217 nd->nd_flag |= ND_NOMOREDATA; 8218 } 8219 /* Skip over return stat for Copy. */ 8220 if ((nd->nd_flag & ND_NOMOREDATA) == 0) 8221 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8222 if (nd->nd_repstat == 0) { 8223 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8224 if (*tl != 0) { 8225 /* There should be no callback ids. */ 8226 error = NFSERR_BADXDR; 8227 goto nfsmout; 8228 } 8229 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED + 8230 NFSX_VERF); 8231 len = fxdr_hyper(tl); tl += 2; 8232 *commitp = fxdr_unsigned(int, *tl++); 8233 NFSLOCKMNT(nmp); 8234 if (!NFSHASWRITEVERF(nmp)) { 8235 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 8236 NFSSETWRITEVERF(nmp); 8237 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) { 8238 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 8239 nd->nd_repstat = NFSERR_STALEWRITEVERF; 8240 } 8241 NFSUNLOCKMNT(nmp); 8242 tl += (NFSX_VERF / NFSX_UNSIGNED); 8243 if (nd->nd_repstat == 0 && *++tl != newnfs_true) 8244 /* Must be a synchronous copy. */ 8245 nd->nd_repstat = NFSERR_NOTSUPP; 8246 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8247 error = nfsm_loadattr(nd, outnap); 8248 if (error == 0) 8249 *outattrflagp = NFS_LATTR_NOSHRINK; 8250 if (nd->nd_repstat == 0) 8251 *lenp = len; 8252 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) { 8253 /* 8254 * For the case where consecutive is not supported, but 8255 * synchronous is supported, we can try consecutive == false 8256 * by returning this error. Otherwise, return NFSERR_NOTSUPP, 8257 * since Copy cannot be done. 8258 */ 8259 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8260 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8261 if (!consecutive || *++tl == newnfs_false) 8262 nd->nd_repstat = NFSERR_NOTSUPP; 8263 } else 8264 nd->nd_repstat = NFSERR_BADXDR; 8265 } 8266 if (error == 0) 8267 error = nd->nd_repstat; 8268 nfsmout: 8269 m_freem(nd->nd_mrep); 8270 return (error); 8271 } 8272 8273 /* 8274 * Seek operation. 8275 */ 8276 APPLESTATIC int 8277 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content, 8278 struct ucred *cred, struct nfsvattr *nap, int *attrflagp) 8279 { 8280 int error, expireret = 0, retrycnt; 8281 u_int32_t clidrev = 0; 8282 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 8283 struct nfsnode *np = VTONFS(vp); 8284 struct nfsfh *nfhp = NULL; 8285 nfsv4stateid_t stateid; 8286 void *lckp; 8287 8288 if (nmp->nm_clp != NULL) 8289 clidrev = nmp->nm_clp->nfsc_clientidrev; 8290 nfhp = np->n_fhp; 8291 retrycnt = 0; 8292 do { 8293 lckp = NULL; 8294 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 8295 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp); 8296 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content, 8297 nap, attrflagp, cred); 8298 if (error == NFSERR_STALESTATEID) 8299 nfscl_initiate_recovery(nmp->nm_clp); 8300 if (lckp != NULL) 8301 nfscl_lockderef(lckp); 8302 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8303 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8304 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 8305 (void) nfs_catnap(PZERO, error, "nfs_seek"); 8306 } else if ((error == NFSERR_EXPIRED || 8307 error == NFSERR_BADSTATEID) && clidrev != 0) { 8308 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, 8309 curthread); 8310 } 8311 retrycnt++; 8312 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8313 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8314 error == NFSERR_BADSESSION || 8315 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 8316 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 8317 expireret == 0 && clidrev != 0 && retrycnt < 4) || 8318 (error == NFSERR_OPENMODE && retrycnt < 4)); 8319 if (error && retrycnt >= 4) 8320 error = EIO; 8321 return (error); 8322 } 8323 8324 /* 8325 * The seek RPC. 8326 */ 8327 static int 8328 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp, 8329 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred) 8330 { 8331 uint32_t *tl; 8332 int error; 8333 struct nfsrv_descript nfsd; 8334 struct nfsrv_descript *nd = &nfsd; 8335 nfsattrbit_t attrbits; 8336 8337 *attrflagp = 0; 8338 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp); 8339 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 8340 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 8341 txdr_hyper(*offp, tl); tl += 2; 8342 *tl++ = txdr_unsigned(content); 8343 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8344 NFSGETATTR_ATTRBIT(&attrbits); 8345 nfsrv_putattrbit(nd, &attrbits); 8346 error = nfscl_request(nd, vp, curthread, cred, NULL); 8347 if (error != 0) 8348 return (error); 8349 if (nd->nd_repstat == 0) { 8350 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER); 8351 if (*tl++ == newnfs_true) 8352 *eofp = true; 8353 else 8354 *eofp = false; 8355 *offp = fxdr_hyper(tl); 8356 /* Just skip over Getattr op status. */ 8357 error = nfsm_loadattr(nd, nap); 8358 if (error == 0) 8359 *attrflagp = 1; 8360 } 8361 error = nd->nd_repstat; 8362 nfsmout: 8363 m_freem(nd->nd_mrep); 8364 return (error); 8365 } 8366 8367 /* 8368 * The getextattr RPC. 8369 */ 8370 APPLESTATIC int 8371 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp, 8372 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 8373 { 8374 uint32_t *tl; 8375 int error; 8376 struct nfsrv_descript nfsd; 8377 struct nfsrv_descript *nd = &nfsd; 8378 nfsattrbit_t attrbits; 8379 uint32_t len, len2; 8380 8381 *attrflagp = 0; 8382 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp); 8383 nfsm_strtom(nd, name, strlen(name)); 8384 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8385 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8386 NFSGETATTR_ATTRBIT(&attrbits); 8387 nfsrv_putattrbit(nd, &attrbits); 8388 error = nfscl_request(nd, vp, p, cred, NULL); 8389 if (error != 0) 8390 return (error); 8391 if (nd->nd_repstat == 0) { 8392 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8393 len = fxdr_unsigned(uint32_t, *tl); 8394 /* Sanity check lengths. */ 8395 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX && 8396 uiop->uio_resid <= UINT32_MAX) { 8397 len2 = uiop->uio_resid; 8398 if (len2 >= len) 8399 error = nfsm_mbufuio(nd, uiop, len); 8400 else { 8401 error = nfsm_mbufuio(nd, uiop, len2); 8402 if (error == 0) { 8403 /* 8404 * nfsm_mbufuio() advances to a multiple 8405 * of 4, so round up len2 as well. Then 8406 * we need to advance over the rest of 8407 * the data, rounding up the remaining 8408 * length. 8409 */ 8410 len2 = NFSM_RNDUP(len2); 8411 len2 = NFSM_RNDUP(len - len2); 8412 if (len2 > 0) 8413 error = nfsm_advance(nd, len2, 8414 -1); 8415 } 8416 } 8417 } else if (uiop == NULL && len > 0) { 8418 /* Just wants the length and not the data. */ 8419 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 8420 } else if (len > 0) 8421 error = ENOATTR; 8422 if (error != 0) 8423 goto nfsmout; 8424 *lenp = len; 8425 /* Just skip over Getattr op status. */ 8426 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8427 error = nfsm_loadattr(nd, nap); 8428 if (error == 0) 8429 *attrflagp = 1; 8430 } 8431 if (error == 0) 8432 error = nd->nd_repstat; 8433 nfsmout: 8434 m_freem(nd->nd_mrep); 8435 return (error); 8436 } 8437 8438 /* 8439 * The setextattr RPC. 8440 */ 8441 APPLESTATIC int 8442 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop, 8443 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 8444 { 8445 uint32_t *tl; 8446 int error; 8447 struct nfsrv_descript nfsd; 8448 struct nfsrv_descript *nd = &nfsd; 8449 nfsattrbit_t attrbits; 8450 8451 *attrflagp = 0; 8452 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp); 8453 if (uiop->uio_resid > nd->nd_maxreq) { 8454 /* nd_maxreq is set by NFSCL_REQSTART(). */ 8455 m_freem(nd->nd_mreq); 8456 return (EINVAL); 8457 } 8458 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8459 *tl = txdr_unsigned(NFSV4SXATTR_EITHER); 8460 nfsm_strtom(nd, name, strlen(name)); 8461 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8462 *tl = txdr_unsigned(uiop->uio_resid); 8463 nfsm_uiombuf(nd, uiop, uiop->uio_resid); 8464 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8465 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8466 NFSGETATTR_ATTRBIT(&attrbits); 8467 nfsrv_putattrbit(nd, &attrbits); 8468 error = nfscl_request(nd, vp, p, cred, NULL); 8469 if (error != 0) 8470 return (error); 8471 if (nd->nd_repstat == 0) { 8472 /* Just skip over the reply and Getattr op status. */ 8473 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 * 8474 NFSX_UNSIGNED); 8475 error = nfsm_loadattr(nd, nap); 8476 if (error == 0) 8477 *attrflagp = 1; 8478 } 8479 if (error == 0) 8480 error = nd->nd_repstat; 8481 nfsmout: 8482 m_freem(nd->nd_mrep); 8483 return (error); 8484 } 8485 8486 /* 8487 * The removeextattr RPC. 8488 */ 8489 APPLESTATIC int 8490 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap, 8491 int *attrflagp, struct ucred *cred, NFSPROC_T *p) 8492 { 8493 uint32_t *tl; 8494 int error; 8495 struct nfsrv_descript nfsd; 8496 struct nfsrv_descript *nd = &nfsd; 8497 nfsattrbit_t attrbits; 8498 8499 *attrflagp = 0; 8500 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp); 8501 nfsm_strtom(nd, name, strlen(name)); 8502 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8503 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8504 NFSGETATTR_ATTRBIT(&attrbits); 8505 nfsrv_putattrbit(nd, &attrbits); 8506 error = nfscl_request(nd, vp, p, cred, NULL); 8507 if (error != 0) 8508 return (error); 8509 if (nd->nd_repstat == 0) { 8510 /* Just skip over the reply and Getattr op status. */ 8511 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 * 8512 NFSX_UNSIGNED); 8513 error = nfsm_loadattr(nd, nap); 8514 if (error == 0) 8515 *attrflagp = 1; 8516 } 8517 if (error == 0) 8518 error = nd->nd_repstat; 8519 nfsmout: 8520 m_freem(nd->nd_mrep); 8521 return (error); 8522 } 8523 8524 /* 8525 * The listextattr RPC. 8526 */ 8527 APPLESTATIC int 8528 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop, 8529 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp, 8530 struct ucred *cred, NFSPROC_T *p) 8531 { 8532 uint32_t *tl; 8533 int cnt, error, i, len; 8534 struct nfsrv_descript nfsd; 8535 struct nfsrv_descript *nd = &nfsd; 8536 nfsattrbit_t attrbits; 8537 u_char c; 8538 8539 *attrflagp = 0; 8540 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp); 8541 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 8542 txdr_hyper(*cookiep, tl); tl += 2; 8543 *tl++ = txdr_unsigned(*lenp); 8544 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8545 NFSGETATTR_ATTRBIT(&attrbits); 8546 nfsrv_putattrbit(nd, &attrbits); 8547 error = nfscl_request(nd, vp, p, cred, NULL); 8548 if (error != 0) 8549 return (error); 8550 *eofp = true; 8551 *lenp = 0; 8552 if (nd->nd_repstat == 0) { 8553 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 8554 *cookiep = fxdr_hyper(tl); tl += 2; 8555 cnt = fxdr_unsigned(int, *tl); 8556 if (cnt < 0) { 8557 error = EBADRPC; 8558 goto nfsmout; 8559 } 8560 for (i = 0; i < cnt; i++) { 8561 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8562 len = fxdr_unsigned(int, *tl); 8563 if (len <= 0 || len > EXTATTR_MAXNAMELEN) { 8564 error = EBADRPC; 8565 goto nfsmout; 8566 } 8567 if (uiop == NULL) 8568 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 8569 else if (uiop->uio_resid >= len + 1) { 8570 c = len; 8571 error = uiomove(&c, sizeof(c), uiop); 8572 if (error == 0) 8573 error = nfsm_mbufuio(nd, uiop, len); 8574 } else { 8575 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 8576 *eofp = false; 8577 } 8578 if (error != 0) 8579 goto nfsmout; 8580 *lenp += (len + 1); 8581 } 8582 /* Get the eof and skip over the Getattr op status. */ 8583 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED); 8584 /* 8585 * *eofp is set false above, because it wasn't able to copy 8586 * all of the reply. 8587 */ 8588 if (*eofp && *tl == 0) 8589 *eofp = false; 8590 error = nfsm_loadattr(nd, nap); 8591 if (error == 0) 8592 *attrflagp = 1; 8593 } 8594 if (error == 0) 8595 error = nd->nd_repstat; 8596 nfsmout: 8597 m_freem(nd->nd_mrep); 8598 return (error); 8599 } 8600 8601