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 = uio_uio_resid(uiop); 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 = uio_uio_resid(uiop); 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 uio_uio_resid_add(uiop, len); 1882 uio_iov_base_add(uiop, -len); 1883 uio_iov_len_add(uiop, len); 1884 } 1885 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 1886 error = nfscl_wcc_data(nd, vp, nap, attrflagp, 1887 &wccflag, stuff); 1888 if (error) 1889 goto nfsmout; 1890 } 1891 if (!nd->nd_repstat) { 1892 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 1893 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED 1894 + NFSX_VERF); 1895 rlen = fxdr_unsigned(int, *tl++); 1896 if (rlen == 0) { 1897 error = NFSERR_IO; 1898 goto nfsmout; 1899 } else if (rlen < len) { 1900 backup = len - rlen; 1901 uio_iov_base_add(uiop, -(backup)); 1902 uio_iov_len_add(uiop, backup); 1903 uiop->uio_offset -= backup; 1904 uio_uio_resid_add(uiop, backup); 1905 len = rlen; 1906 } 1907 commit = fxdr_unsigned(int, *tl++); 1908 1909 /* 1910 * Return the lowest commitment level 1911 * obtained by any of the RPCs. 1912 */ 1913 if (committed == NFSWRITE_FILESYNC) 1914 committed = commit; 1915 else if (committed == NFSWRITE_DATASYNC && 1916 commit == NFSWRITE_UNSTABLE) 1917 committed = commit; 1918 NFSLOCKMNT(nmp); 1919 if (!NFSHASWRITEVERF(nmp)) { 1920 NFSBCOPY((caddr_t)tl, 1921 (caddr_t)&nmp->nm_verf[0], 1922 NFSX_VERF); 1923 NFSSETWRITEVERF(nmp); 1924 } else if (NFSBCMP(tl, nmp->nm_verf, 1925 NFSX_VERF)) { 1926 *must_commit = 1; 1927 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 1928 } 1929 NFSUNLOCKMNT(nmp); 1930 } 1931 if (nd->nd_flag & ND_NFSV4) 1932 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1933 if (nd->nd_flag & (ND_NFSV2 | ND_NFSV4)) { 1934 error = nfsm_loadattr(nd, nap); 1935 if (!error) 1936 *attrflagp = NFS_LATTR_NOSHRINK; 1937 } 1938 } else { 1939 error = nd->nd_repstat; 1940 } 1941 if (error) 1942 goto nfsmout; 1943 NFSWRITERPC_SETTIME(wccflag, np, nap, (nd->nd_flag & ND_NFSV4)); 1944 m_freem(nd->nd_mrep); 1945 nd->nd_mrep = NULL; 1946 tsiz -= len; 1947 } 1948 nfsmout: 1949 if (nd->nd_mrep != NULL) 1950 m_freem(nd->nd_mrep); 1951 *iomode = committed; 1952 if (nd->nd_repstat && !error) 1953 error = nd->nd_repstat; 1954 return (error); 1955 } 1956 1957 /* 1958 * nfs mknod rpc 1959 * For NFS v2 this is a kludge. Use a create rpc but with the IFMT bits of the 1960 * mode set to specify the file type and the size field for rdev. 1961 */ 1962 APPLESTATIC int 1963 nfsrpc_mknod(vnode_t dvp, char *name, int namelen, struct vattr *vap, 1964 u_int32_t rdev, enum vtype vtyp, struct ucred *cred, NFSPROC_T *p, 1965 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 1966 int *attrflagp, int *dattrflagp, void *dstuff) 1967 { 1968 u_int32_t *tl; 1969 int error = 0; 1970 struct nfsrv_descript nfsd, *nd = &nfsd; 1971 nfsattrbit_t attrbits; 1972 1973 *nfhpp = NULL; 1974 *attrflagp = 0; 1975 *dattrflagp = 0; 1976 if (namelen > NFS_MAXNAMLEN) 1977 return (ENAMETOOLONG); 1978 NFSCL_REQSTART(nd, NFSPROC_MKNOD, dvp); 1979 if (nd->nd_flag & ND_NFSV4) { 1980 if (vtyp == VBLK || vtyp == VCHR) { 1981 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1982 *tl++ = vtonfsv34_type(vtyp); 1983 *tl++ = txdr_unsigned(NFSMAJOR(rdev)); 1984 *tl = txdr_unsigned(NFSMINOR(rdev)); 1985 } else { 1986 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1987 *tl = vtonfsv34_type(vtyp); 1988 } 1989 } 1990 (void) nfsm_strtom(nd, name, namelen); 1991 if (nd->nd_flag & ND_NFSV3) { 1992 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1993 *tl = vtonfsv34_type(vtyp); 1994 } 1995 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 1996 nfscl_fillsattr(nd, vap, dvp, 0, 0); 1997 if ((nd->nd_flag & ND_NFSV3) && 1998 (vtyp == VCHR || vtyp == VBLK)) { 1999 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2000 *tl++ = txdr_unsigned(NFSMAJOR(rdev)); 2001 *tl = txdr_unsigned(NFSMINOR(rdev)); 2002 } 2003 if (nd->nd_flag & ND_NFSV4) { 2004 NFSGETATTR_ATTRBIT(&attrbits); 2005 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2006 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2007 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2008 (void) nfsrv_putattrbit(nd, &attrbits); 2009 } 2010 if (nd->nd_flag & ND_NFSV2) 2011 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZERDEV, rdev); 2012 error = nfscl_request(nd, dvp, p, cred, dstuff); 2013 if (error) 2014 return (error); 2015 if (nd->nd_flag & ND_NFSV4) 2016 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2017 if (!nd->nd_repstat) { 2018 if (nd->nd_flag & ND_NFSV4) { 2019 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2020 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2021 if (error) 2022 goto nfsmout; 2023 } 2024 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2025 if (error) 2026 goto nfsmout; 2027 } 2028 if (nd->nd_flag & ND_NFSV3) 2029 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2030 if (!error && nd->nd_repstat) 2031 error = nd->nd_repstat; 2032 nfsmout: 2033 m_freem(nd->nd_mrep); 2034 return (error); 2035 } 2036 2037 /* 2038 * nfs file create call 2039 * Mostly just call the approriate routine. (I separated out v4, so that 2040 * error recovery wouldn't be as difficult.) 2041 */ 2042 APPLESTATIC int 2043 nfsrpc_create(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2044 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p, 2045 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 2046 int *attrflagp, int *dattrflagp, void *dstuff) 2047 { 2048 int error = 0, newone, expireret = 0, retrycnt, unlocked; 2049 struct nfsclowner *owp; 2050 struct nfscldeleg *dp; 2051 struct nfsmount *nmp = VFSTONFS(vnode_mount(dvp)); 2052 u_int32_t clidrev; 2053 2054 if (NFSHASNFSV4(nmp)) { 2055 retrycnt = 0; 2056 do { 2057 dp = NULL; 2058 error = nfscl_open(dvp, NULL, 0, (NFSV4OPEN_ACCESSWRITE | 2059 NFSV4OPEN_ACCESSREAD), 0, cred, p, &owp, NULL, &newone, 2060 NULL, 1); 2061 if (error) 2062 return (error); 2063 if (nmp->nm_clp != NULL) 2064 clidrev = nmp->nm_clp->nfsc_clientidrev; 2065 else 2066 clidrev = 0; 2067 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || 2068 nfs_numnfscbd == 0 || retrycnt > 0) 2069 error = nfsrpc_createv4(dvp, name, namelen, vap, cverf, 2070 fmode, owp, &dp, cred, p, dnap, nnap, nfhpp, 2071 attrflagp, dattrflagp, dstuff, &unlocked); 2072 else 2073 error = nfsrpc_getcreatelayout(dvp, name, namelen, vap, 2074 cverf, fmode, owp, &dp, cred, p, dnap, nnap, nfhpp, 2075 attrflagp, dattrflagp, dstuff, &unlocked); 2076 /* 2077 * There is no need to invalidate cached attributes here, 2078 * since new post-delegation issue attributes are always 2079 * returned by nfsrpc_createv4() and these will update the 2080 * attribute cache. 2081 */ 2082 if (dp != NULL) 2083 (void) nfscl_deleg(nmp->nm_mountp, owp->nfsow_clp, 2084 (*nfhpp)->nfh_fh, (*nfhpp)->nfh_len, cred, p, &dp); 2085 nfscl_ownerrelease(nmp, owp, error, newone, unlocked); 2086 if (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 2087 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 2088 error == NFSERR_BADSESSION) { 2089 (void) nfs_catnap(PZERO, error, "nfs_open"); 2090 } else if ((error == NFSERR_EXPIRED || 2091 error == NFSERR_BADSTATEID) && clidrev != 0) { 2092 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 2093 retrycnt++; 2094 } 2095 } while (error == NFSERR_GRACE || error == NFSERR_STALECLIENTID || 2096 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 2097 error == NFSERR_BADSESSION || 2098 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 2099 expireret == 0 && clidrev != 0 && retrycnt < 4)); 2100 if (error && retrycnt >= 4) 2101 error = EIO; 2102 } else { 2103 error = nfsrpc_createv23(dvp, name, namelen, vap, cverf, 2104 fmode, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 2105 dstuff); 2106 } 2107 return (error); 2108 } 2109 2110 /* 2111 * The create rpc for v2 and 3. 2112 */ 2113 static int 2114 nfsrpc_createv23(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2115 nfsquad_t cverf, int fmode, struct ucred *cred, NFSPROC_T *p, 2116 struct nfsvattr *dnap, struct nfsvattr *nnap, struct nfsfh **nfhpp, 2117 int *attrflagp, int *dattrflagp, void *dstuff) 2118 { 2119 u_int32_t *tl; 2120 int error = 0; 2121 struct nfsrv_descript nfsd, *nd = &nfsd; 2122 2123 *nfhpp = NULL; 2124 *attrflagp = 0; 2125 *dattrflagp = 0; 2126 if (namelen > NFS_MAXNAMLEN) 2127 return (ENAMETOOLONG); 2128 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp); 2129 (void) nfsm_strtom(nd, name, namelen); 2130 if (nd->nd_flag & ND_NFSV3) { 2131 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2132 if (fmode & O_EXCL) { 2133 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE); 2134 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2135 *tl++ = cverf.lval[0]; 2136 *tl = cverf.lval[1]; 2137 } else { 2138 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 2139 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2140 } 2141 } else { 2142 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZE0, 0); 2143 } 2144 error = nfscl_request(nd, dvp, p, cred, dstuff); 2145 if (error) 2146 return (error); 2147 if (nd->nd_repstat == 0) { 2148 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2149 if (error) 2150 goto nfsmout; 2151 } 2152 if (nd->nd_flag & ND_NFSV3) 2153 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2154 if (nd->nd_repstat != 0 && error == 0) 2155 error = nd->nd_repstat; 2156 nfsmout: 2157 m_freem(nd->nd_mrep); 2158 return (error); 2159 } 2160 2161 static int 2162 nfsrpc_createv4(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2163 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 2164 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2165 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2166 int *dattrflagp, void *dstuff, int *unlockedp) 2167 { 2168 u_int32_t *tl; 2169 int error = 0, deleg, newone, ret, acesize, limitby; 2170 struct nfsrv_descript nfsd, *nd = &nfsd; 2171 struct nfsclopen *op; 2172 struct nfscldeleg *dp = NULL; 2173 struct nfsnode *np; 2174 struct nfsfh *nfhp; 2175 nfsattrbit_t attrbits; 2176 nfsv4stateid_t stateid; 2177 u_int32_t rflags; 2178 struct nfsmount *nmp; 2179 struct nfsclsession *tsep; 2180 2181 nmp = VFSTONFS(dvp->v_mount); 2182 np = VTONFS(dvp); 2183 *unlockedp = 0; 2184 *nfhpp = NULL; 2185 *dpp = NULL; 2186 *attrflagp = 0; 2187 *dattrflagp = 0; 2188 if (namelen > NFS_MAXNAMLEN) 2189 return (ENAMETOOLONG); 2190 NFSCL_REQSTART(nd, NFSPROC_CREATE, dvp); 2191 /* 2192 * For V4, this is actually an Open op. 2193 */ 2194 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2195 *tl++ = txdr_unsigned(owp->nfsow_seqid); 2196 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 2197 NFSV4OPEN_ACCESSREAD); 2198 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE); 2199 tsep = nfsmnt_mdssession(nmp); 2200 *tl++ = tsep->nfsess_clientid.lval[0]; 2201 *tl = tsep->nfsess_clientid.lval[1]; 2202 (void) nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 2203 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2204 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE); 2205 if (fmode & O_EXCL) { 2206 if (NFSHASNFSV4N(nmp)) { 2207 if (NFSHASSESSPERSIST(nmp)) { 2208 /* Use GUARDED for persistent sessions. */ 2209 *tl = txdr_unsigned(NFSCREATE_GUARDED); 2210 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2211 } else { 2212 /* Otherwise, use EXCLUSIVE4_1. */ 2213 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41); 2214 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2215 *tl++ = cverf.lval[0]; 2216 *tl = cverf.lval[1]; 2217 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2218 } 2219 } else { 2220 /* NFSv4.0 */ 2221 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE); 2222 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2223 *tl++ = cverf.lval[0]; 2224 *tl = cverf.lval[1]; 2225 } 2226 } else { 2227 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 2228 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2229 } 2230 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2231 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 2232 (void) nfsm_strtom(nd, name, namelen); 2233 /* Get the new file's handle and attributes. */ 2234 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2235 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2236 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2237 NFSGETATTR_ATTRBIT(&attrbits); 2238 (void) nfsrv_putattrbit(nd, &attrbits); 2239 /* Get the directory's post-op attributes. */ 2240 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2241 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2242 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0); 2243 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2244 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2245 (void) nfsrv_putattrbit(nd, &attrbits); 2246 error = nfscl_request(nd, dvp, p, cred, dstuff); 2247 if (error) 2248 return (error); 2249 NFSCL_INCRSEQID(owp->nfsow_seqid, nd); 2250 if (nd->nd_repstat == 0) { 2251 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 2252 6 * NFSX_UNSIGNED); 2253 stateid.seqid = *tl++; 2254 stateid.other[0] = *tl++; 2255 stateid.other[1] = *tl++; 2256 stateid.other[2] = *tl; 2257 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 2258 (void) nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2259 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2260 deleg = fxdr_unsigned(int, *tl); 2261 if (deleg == NFSV4OPEN_DELEGATEREAD || 2262 deleg == NFSV4OPEN_DELEGATEWRITE) { 2263 if (!(owp->nfsow_clp->nfsc_flags & 2264 NFSCLFLAGS_FIRSTDELEG)) 2265 owp->nfsow_clp->nfsc_flags |= 2266 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 2267 dp = malloc( 2268 sizeof (struct nfscldeleg) + NFSX_V4FHMAX, 2269 M_NFSCLDELEG, M_WAITOK); 2270 LIST_INIT(&dp->nfsdl_owner); 2271 LIST_INIT(&dp->nfsdl_lock); 2272 dp->nfsdl_clp = owp->nfsow_clp; 2273 newnfs_copyincred(cred, &dp->nfsdl_cred); 2274 nfscl_lockinit(&dp->nfsdl_rwlock); 2275 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 2276 NFSX_UNSIGNED); 2277 dp->nfsdl_stateid.seqid = *tl++; 2278 dp->nfsdl_stateid.other[0] = *tl++; 2279 dp->nfsdl_stateid.other[1] = *tl++; 2280 dp->nfsdl_stateid.other[2] = *tl++; 2281 ret = fxdr_unsigned(int, *tl); 2282 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 2283 dp->nfsdl_flags = NFSCLDL_WRITE; 2284 /* 2285 * Indicates how much the file can grow. 2286 */ 2287 NFSM_DISSECT(tl, u_int32_t *, 2288 3 * NFSX_UNSIGNED); 2289 limitby = fxdr_unsigned(int, *tl++); 2290 switch (limitby) { 2291 case NFSV4OPEN_LIMITSIZE: 2292 dp->nfsdl_sizelimit = fxdr_hyper(tl); 2293 break; 2294 case NFSV4OPEN_LIMITBLOCKS: 2295 dp->nfsdl_sizelimit = 2296 fxdr_unsigned(u_int64_t, *tl++); 2297 dp->nfsdl_sizelimit *= 2298 fxdr_unsigned(u_int64_t, *tl); 2299 break; 2300 default: 2301 error = NFSERR_BADXDR; 2302 goto nfsmout; 2303 } 2304 } else { 2305 dp->nfsdl_flags = NFSCLDL_READ; 2306 } 2307 if (ret) 2308 dp->nfsdl_flags |= NFSCLDL_RECALL; 2309 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret, 2310 &acesize, p); 2311 if (error) 2312 goto nfsmout; 2313 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 2314 error = NFSERR_BADXDR; 2315 goto nfsmout; 2316 } 2317 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2318 if (error) 2319 goto nfsmout; 2320 /* Get rid of the PutFH and Getattr status values. */ 2321 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2322 /* Load the directory attributes. */ 2323 error = nfsm_loadattr(nd, dnap); 2324 if (error) 2325 goto nfsmout; 2326 *dattrflagp = 1; 2327 if (dp != NULL && *attrflagp) { 2328 dp->nfsdl_change = nnap->na_filerev; 2329 dp->nfsdl_modtime = nnap->na_mtime; 2330 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 2331 } 2332 /* 2333 * We can now complete the Open state. 2334 */ 2335 nfhp = *nfhpp; 2336 if (dp != NULL) { 2337 dp->nfsdl_fhlen = nfhp->nfh_len; 2338 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, nfhp->nfh_len); 2339 } 2340 /* 2341 * Get an Open structure that will be 2342 * attached to the OpenOwner, acquired already. 2343 */ 2344 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 2345 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0, 2346 cred, p, NULL, &op, &newone, NULL, 0); 2347 if (error) 2348 goto nfsmout; 2349 op->nfso_stateid = stateid; 2350 newnfs_copyincred(cred, &op->nfso_cred); 2351 if ((rflags & NFSV4OPEN_RESULTCONFIRM)) { 2352 do { 2353 ret = nfsrpc_openconfirm(dvp, nfhp->nfh_fh, 2354 nfhp->nfh_len, op, cred, p); 2355 if (ret == NFSERR_DELAY) 2356 (void) nfs_catnap(PZERO, ret, "nfs_create"); 2357 } while (ret == NFSERR_DELAY); 2358 error = ret; 2359 } 2360 2361 /* 2362 * If the server is handing out delegations, but we didn't 2363 * get one because an OpenConfirm was required, try the 2364 * Open again, to get a delegation. This is a harmless no-op, 2365 * from a server's point of view. 2366 */ 2367 if ((rflags & NFSV4OPEN_RESULTCONFIRM) && 2368 (owp->nfsow_clp->nfsc_flags & NFSCLFLAGS_GOTDELEG) && 2369 !error && dp == NULL) { 2370 do { 2371 ret = nfsrpc_openrpc(VFSTONFS(vnode_mount(dvp)), dvp, 2372 np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 2373 nfhp->nfh_fh, nfhp->nfh_len, 2374 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), op, 2375 name, namelen, &dp, 0, 0x0, cred, p, 0, 1); 2376 if (ret == NFSERR_DELAY) 2377 (void) nfs_catnap(PZERO, ret, "nfs_crt2"); 2378 } while (ret == NFSERR_DELAY); 2379 if (ret) { 2380 if (dp != NULL) { 2381 free(dp, M_NFSCLDELEG); 2382 dp = NULL; 2383 } 2384 if (ret == NFSERR_STALECLIENTID || 2385 ret == NFSERR_STALEDONTRECOVER || 2386 ret == NFSERR_BADSESSION) 2387 error = ret; 2388 } 2389 } 2390 nfscl_openrelease(nmp, op, error, newone); 2391 *unlockedp = 1; 2392 } 2393 if (nd->nd_repstat != 0 && error == 0) 2394 error = nd->nd_repstat; 2395 if (error == NFSERR_STALECLIENTID) 2396 nfscl_initiate_recovery(owp->nfsow_clp); 2397 nfsmout: 2398 if (!error) 2399 *dpp = dp; 2400 else if (dp != NULL) 2401 free(dp, M_NFSCLDELEG); 2402 m_freem(nd->nd_mrep); 2403 return (error); 2404 } 2405 2406 /* 2407 * Nfs remove rpc 2408 */ 2409 APPLESTATIC int 2410 nfsrpc_remove(vnode_t dvp, char *name, int namelen, vnode_t vp, 2411 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, 2412 void *dstuff) 2413 { 2414 u_int32_t *tl; 2415 struct nfsrv_descript nfsd, *nd = &nfsd; 2416 struct nfsnode *np; 2417 struct nfsmount *nmp; 2418 nfsv4stateid_t dstateid; 2419 int error, ret = 0, i; 2420 2421 *dattrflagp = 0; 2422 if (namelen > NFS_MAXNAMLEN) 2423 return (ENAMETOOLONG); 2424 nmp = VFSTONFS(vnode_mount(dvp)); 2425 tryagain: 2426 if (NFSHASNFSV4(nmp) && ret == 0) { 2427 ret = nfscl_removedeleg(vp, p, &dstateid); 2428 if (ret == 1) { 2429 NFSCL_REQSTART(nd, NFSPROC_RETDELEGREMOVE, vp); 2430 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 2431 NFSX_UNSIGNED); 2432 if (NFSHASNFSV4N(nmp)) 2433 *tl++ = 0; 2434 else 2435 *tl++ = dstateid.seqid; 2436 *tl++ = dstateid.other[0]; 2437 *tl++ = dstateid.other[1]; 2438 *tl++ = dstateid.other[2]; 2439 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2440 np = VTONFS(dvp); 2441 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2442 np->n_fhp->nfh_len, 0); 2443 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2444 *tl = txdr_unsigned(NFSV4OP_REMOVE); 2445 } 2446 } else { 2447 ret = 0; 2448 } 2449 if (ret == 0) 2450 NFSCL_REQSTART(nd, NFSPROC_REMOVE, dvp); 2451 (void) nfsm_strtom(nd, name, namelen); 2452 error = nfscl_request(nd, dvp, p, cred, dstuff); 2453 if (error) 2454 return (error); 2455 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 2456 /* For NFSv4, parse out any Delereturn replies. */ 2457 if (ret > 0 && nd->nd_repstat != 0 && 2458 (nd->nd_flag & ND_NOMOREDATA)) { 2459 /* 2460 * If the Delegreturn failed, try again without 2461 * it. The server will Recall, as required. 2462 */ 2463 m_freem(nd->nd_mrep); 2464 goto tryagain; 2465 } 2466 for (i = 0; i < (ret * 2); i++) { 2467 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 2468 ND_NFSV4) { 2469 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2470 if (*(tl + 1)) 2471 nd->nd_flag |= ND_NOMOREDATA; 2472 } 2473 } 2474 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2475 } 2476 if (nd->nd_repstat && !error) 2477 error = nd->nd_repstat; 2478 nfsmout: 2479 m_freem(nd->nd_mrep); 2480 return (error); 2481 } 2482 2483 /* 2484 * Do an nfs rename rpc. 2485 */ 2486 APPLESTATIC int 2487 nfsrpc_rename(vnode_t fdvp, vnode_t fvp, char *fnameptr, int fnamelen, 2488 vnode_t tdvp, vnode_t tvp, char *tnameptr, int tnamelen, struct ucred *cred, 2489 NFSPROC_T *p, struct nfsvattr *fnap, struct nfsvattr *tnap, 2490 int *fattrflagp, int *tattrflagp, void *fstuff, void *tstuff) 2491 { 2492 u_int32_t *tl; 2493 struct nfsrv_descript nfsd, *nd = &nfsd; 2494 struct nfsmount *nmp; 2495 struct nfsnode *np; 2496 nfsattrbit_t attrbits; 2497 nfsv4stateid_t fdstateid, tdstateid; 2498 int error = 0, ret = 0, gottd = 0, gotfd = 0, i; 2499 2500 *fattrflagp = 0; 2501 *tattrflagp = 0; 2502 nmp = VFSTONFS(vnode_mount(fdvp)); 2503 if (fnamelen > NFS_MAXNAMLEN || tnamelen > NFS_MAXNAMLEN) 2504 return (ENAMETOOLONG); 2505 tryagain: 2506 if (NFSHASNFSV4(nmp) && ret == 0) { 2507 ret = nfscl_renamedeleg(fvp, &fdstateid, &gotfd, tvp, 2508 &tdstateid, &gottd, p); 2509 if (gotfd && gottd) { 2510 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME2, fvp); 2511 } else if (gotfd) { 2512 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, fvp); 2513 } else if (gottd) { 2514 NFSCL_REQSTART(nd, NFSPROC_RETDELEGRENAME1, tvp); 2515 } 2516 if (gotfd) { 2517 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 2518 if (NFSHASNFSV4N(nmp)) 2519 *tl++ = 0; 2520 else 2521 *tl++ = fdstateid.seqid; 2522 *tl++ = fdstateid.other[0]; 2523 *tl++ = fdstateid.other[1]; 2524 *tl = fdstateid.other[2]; 2525 if (gottd) { 2526 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2527 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2528 np = VTONFS(tvp); 2529 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2530 np->n_fhp->nfh_len, 0); 2531 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2532 *tl = txdr_unsigned(NFSV4OP_DELEGRETURN); 2533 } 2534 } 2535 if (gottd) { 2536 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 2537 if (NFSHASNFSV4N(nmp)) 2538 *tl++ = 0; 2539 else 2540 *tl++ = tdstateid.seqid; 2541 *tl++ = tdstateid.other[0]; 2542 *tl++ = tdstateid.other[1]; 2543 *tl = tdstateid.other[2]; 2544 } 2545 if (ret > 0) { 2546 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2547 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2548 np = VTONFS(fdvp); 2549 (void) nfsm_fhtom(nd, np->n_fhp->nfh_fh, 2550 np->n_fhp->nfh_len, 0); 2551 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2552 *tl = txdr_unsigned(NFSV4OP_SAVEFH); 2553 } 2554 } else { 2555 ret = 0; 2556 } 2557 if (ret == 0) 2558 NFSCL_REQSTART(nd, NFSPROC_RENAME, fdvp); 2559 if (nd->nd_flag & ND_NFSV4) { 2560 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2561 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2562 NFSWCCATTR_ATTRBIT(&attrbits); 2563 (void) nfsrv_putattrbit(nd, &attrbits); 2564 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2565 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2566 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh, 2567 VTONFS(tdvp)->n_fhp->nfh_len, 0); 2568 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2569 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2570 (void) nfsrv_putattrbit(nd, &attrbits); 2571 nd->nd_flag |= ND_V4WCCATTR; 2572 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2573 *tl = txdr_unsigned(NFSV4OP_RENAME); 2574 } 2575 (void) nfsm_strtom(nd, fnameptr, fnamelen); 2576 if (!(nd->nd_flag & ND_NFSV4)) 2577 (void) nfsm_fhtom(nd, VTONFS(tdvp)->n_fhp->nfh_fh, 2578 VTONFS(tdvp)->n_fhp->nfh_len, 0); 2579 (void) nfsm_strtom(nd, tnameptr, tnamelen); 2580 error = nfscl_request(nd, fdvp, p, cred, fstuff); 2581 if (error) 2582 return (error); 2583 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) { 2584 /* For NFSv4, parse out any Delereturn replies. */ 2585 if (ret > 0 && nd->nd_repstat != 0 && 2586 (nd->nd_flag & ND_NOMOREDATA)) { 2587 /* 2588 * If the Delegreturn failed, try again without 2589 * it. The server will Recall, as required. 2590 */ 2591 m_freem(nd->nd_mrep); 2592 goto tryagain; 2593 } 2594 for (i = 0; i < (ret * 2); i++) { 2595 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == 2596 ND_NFSV4) { 2597 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2598 if (*(tl + 1)) { 2599 if (i == 0 && ret > 1) { 2600 /* 2601 * If the Delegreturn failed, try again 2602 * without it. The server will Recall, as 2603 * required. 2604 * If ret > 1, the first iteration of this 2605 * loop is the second DelegReturn result. 2606 */ 2607 m_freem(nd->nd_mrep); 2608 goto tryagain; 2609 } else { 2610 nd->nd_flag |= ND_NOMOREDATA; 2611 } 2612 } 2613 } 2614 } 2615 /* Now, the first wcc attribute reply. */ 2616 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 2617 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2618 if (*(tl + 1)) 2619 nd->nd_flag |= ND_NOMOREDATA; 2620 } 2621 error = nfscl_wcc_data(nd, fdvp, fnap, fattrflagp, NULL, 2622 fstuff); 2623 /* and the second wcc attribute reply. */ 2624 if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4 && 2625 !error) { 2626 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2627 if (*(tl + 1)) 2628 nd->nd_flag |= ND_NOMOREDATA; 2629 } 2630 if (!error) 2631 error = nfscl_wcc_data(nd, tdvp, tnap, tattrflagp, 2632 NULL, tstuff); 2633 } 2634 if (nd->nd_repstat && !error) 2635 error = nd->nd_repstat; 2636 nfsmout: 2637 m_freem(nd->nd_mrep); 2638 return (error); 2639 } 2640 2641 /* 2642 * nfs hard link create rpc 2643 */ 2644 APPLESTATIC int 2645 nfsrpc_link(vnode_t dvp, vnode_t vp, char *name, int namelen, 2646 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2647 struct nfsvattr *nap, int *attrflagp, int *dattrflagp, void *dstuff) 2648 { 2649 u_int32_t *tl; 2650 struct nfsrv_descript nfsd, *nd = &nfsd; 2651 nfsattrbit_t attrbits; 2652 int error = 0; 2653 2654 *attrflagp = 0; 2655 *dattrflagp = 0; 2656 if (namelen > NFS_MAXNAMLEN) 2657 return (ENAMETOOLONG); 2658 NFSCL_REQSTART(nd, NFSPROC_LINK, vp); 2659 if (nd->nd_flag & ND_NFSV4) { 2660 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2661 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2662 } 2663 (void) nfsm_fhtom(nd, VTONFS(dvp)->n_fhp->nfh_fh, 2664 VTONFS(dvp)->n_fhp->nfh_len, 0); 2665 if (nd->nd_flag & ND_NFSV4) { 2666 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2667 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2668 NFSWCCATTR_ATTRBIT(&attrbits); 2669 (void) nfsrv_putattrbit(nd, &attrbits); 2670 nd->nd_flag |= ND_V4WCCATTR; 2671 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2672 *tl = txdr_unsigned(NFSV4OP_LINK); 2673 } 2674 (void) nfsm_strtom(nd, name, namelen); 2675 error = nfscl_request(nd, vp, p, cred, dstuff); 2676 if (error) 2677 return (error); 2678 if (nd->nd_flag & ND_NFSV3) { 2679 error = nfscl_postop_attr(nd, nap, attrflagp, dstuff); 2680 if (!error) 2681 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, 2682 NULL, dstuff); 2683 } else if ((nd->nd_flag & (ND_NFSV4 | ND_NOMOREDATA)) == ND_NFSV4) { 2684 /* 2685 * First, parse out the PutFH and Getattr result. 2686 */ 2687 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2688 if (!(*(tl + 1))) 2689 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2690 if (*(tl + 1)) 2691 nd->nd_flag |= ND_NOMOREDATA; 2692 /* 2693 * Get the pre-op attributes. 2694 */ 2695 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2696 } 2697 if (nd->nd_repstat && !error) 2698 error = nd->nd_repstat; 2699 nfsmout: 2700 m_freem(nd->nd_mrep); 2701 return (error); 2702 } 2703 2704 /* 2705 * nfs symbolic link create rpc 2706 */ 2707 APPLESTATIC int 2708 nfsrpc_symlink(vnode_t dvp, char *name, int namelen, const char *target, 2709 struct vattr *vap, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2710 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2711 int *dattrflagp, void *dstuff) 2712 { 2713 u_int32_t *tl; 2714 struct nfsrv_descript nfsd, *nd = &nfsd; 2715 struct nfsmount *nmp; 2716 int slen, error = 0; 2717 2718 *nfhpp = NULL; 2719 *attrflagp = 0; 2720 *dattrflagp = 0; 2721 nmp = VFSTONFS(vnode_mount(dvp)); 2722 slen = strlen(target); 2723 if (slen > NFS_MAXPATHLEN || namelen > NFS_MAXNAMLEN) 2724 return (ENAMETOOLONG); 2725 NFSCL_REQSTART(nd, NFSPROC_SYMLINK, dvp); 2726 if (nd->nd_flag & ND_NFSV4) { 2727 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2728 *tl = txdr_unsigned(NFLNK); 2729 (void) nfsm_strtom(nd, target, slen); 2730 } 2731 (void) nfsm_strtom(nd, name, namelen); 2732 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 2733 nfscl_fillsattr(nd, vap, dvp, 0, 0); 2734 if (!(nd->nd_flag & ND_NFSV4)) 2735 (void) nfsm_strtom(nd, target, slen); 2736 if (nd->nd_flag & ND_NFSV2) 2737 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0); 2738 error = nfscl_request(nd, dvp, p, cred, dstuff); 2739 if (error) 2740 return (error); 2741 if (nd->nd_flag & ND_NFSV4) 2742 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2743 if ((nd->nd_flag & ND_NFSV3) && !error) { 2744 if (!nd->nd_repstat) 2745 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2746 if (!error) 2747 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, 2748 NULL, dstuff); 2749 } 2750 if (nd->nd_repstat && !error) 2751 error = nd->nd_repstat; 2752 m_freem(nd->nd_mrep); 2753 /* 2754 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 2755 * Only do this if vfs.nfs.ignore_eexist is set. 2756 * Never do this for NFSv4.1 or later minor versions, since sessions 2757 * should guarantee "exactly once" RPC semantics. 2758 */ 2759 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) || 2760 nmp->nm_minorvers == 0)) 2761 error = 0; 2762 return (error); 2763 } 2764 2765 /* 2766 * nfs make dir rpc 2767 */ 2768 APPLESTATIC int 2769 nfsrpc_mkdir(vnode_t dvp, char *name, int namelen, struct vattr *vap, 2770 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 2771 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 2772 int *dattrflagp, void *dstuff) 2773 { 2774 u_int32_t *tl; 2775 struct nfsrv_descript nfsd, *nd = &nfsd; 2776 nfsattrbit_t attrbits; 2777 int error = 0; 2778 struct nfsfh *fhp; 2779 struct nfsmount *nmp; 2780 2781 *nfhpp = NULL; 2782 *attrflagp = 0; 2783 *dattrflagp = 0; 2784 nmp = VFSTONFS(vnode_mount(dvp)); 2785 fhp = VTONFS(dvp)->n_fhp; 2786 if (namelen > NFS_MAXNAMLEN) 2787 return (ENAMETOOLONG); 2788 NFSCL_REQSTART(nd, NFSPROC_MKDIR, dvp); 2789 if (nd->nd_flag & ND_NFSV4) { 2790 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2791 *tl = txdr_unsigned(NFDIR); 2792 } 2793 (void) nfsm_strtom(nd, name, namelen); 2794 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_SIZENEG1, 0); 2795 if (nd->nd_flag & ND_NFSV4) { 2796 NFSGETATTR_ATTRBIT(&attrbits); 2797 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2798 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2799 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2800 (void) nfsrv_putattrbit(nd, &attrbits); 2801 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2802 *tl = txdr_unsigned(NFSV4OP_PUTFH); 2803 (void) nfsm_fhtom(nd, fhp->nfh_fh, fhp->nfh_len, 0); 2804 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2805 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2806 (void) nfsrv_putattrbit(nd, &attrbits); 2807 } 2808 error = nfscl_request(nd, dvp, p, cred, dstuff); 2809 if (error) 2810 return (error); 2811 if (nd->nd_flag & ND_NFSV4) 2812 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2813 if (!nd->nd_repstat && !error) { 2814 if (nd->nd_flag & ND_NFSV4) { 2815 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2816 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2817 } 2818 if (!error) 2819 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 2820 if (error == 0 && (nd->nd_flag & ND_NFSV4) != 0) { 2821 /* Get rid of the PutFH and Getattr status values. */ 2822 NFSM_DISSECT(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2823 /* Load the directory attributes. */ 2824 error = nfsm_loadattr(nd, dnap); 2825 if (error == 0) 2826 *dattrflagp = 1; 2827 } 2828 } 2829 if ((nd->nd_flag & ND_NFSV3) && !error) 2830 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2831 if (nd->nd_repstat && !error) 2832 error = nd->nd_repstat; 2833 nfsmout: 2834 m_freem(nd->nd_mrep); 2835 /* 2836 * Kludge: Map EEXIST => 0 assuming that it is a reply to a retry. 2837 * Only do this if vfs.nfs.ignore_eexist is set. 2838 * Never do this for NFSv4.1 or later minor versions, since sessions 2839 * should guarantee "exactly once" RPC semantics. 2840 */ 2841 if (error == EEXIST && nfsignore_eexist != 0 && (!NFSHASNFSV4(nmp) || 2842 nmp->nm_minorvers == 0)) 2843 error = 0; 2844 return (error); 2845 } 2846 2847 /* 2848 * nfs remove directory call 2849 */ 2850 APPLESTATIC int 2851 nfsrpc_rmdir(vnode_t dvp, char *name, int namelen, struct ucred *cred, 2852 NFSPROC_T *p, struct nfsvattr *dnap, int *dattrflagp, void *dstuff) 2853 { 2854 struct nfsrv_descript nfsd, *nd = &nfsd; 2855 int error = 0; 2856 2857 *dattrflagp = 0; 2858 if (namelen > NFS_MAXNAMLEN) 2859 return (ENAMETOOLONG); 2860 NFSCL_REQSTART(nd, NFSPROC_RMDIR, dvp); 2861 (void) nfsm_strtom(nd, name, namelen); 2862 error = nfscl_request(nd, dvp, p, cred, dstuff); 2863 if (error) 2864 return (error); 2865 if (nd->nd_flag & (ND_NFSV3 | ND_NFSV4)) 2866 error = nfscl_wcc_data(nd, dvp, dnap, dattrflagp, NULL, dstuff); 2867 if (nd->nd_repstat && !error) 2868 error = nd->nd_repstat; 2869 m_freem(nd->nd_mrep); 2870 /* 2871 * Kludge: Map ENOENT => 0 assuming that you have a reply to a retry. 2872 */ 2873 if (error == ENOENT) 2874 error = 0; 2875 return (error); 2876 } 2877 2878 /* 2879 * Readdir rpc. 2880 * Always returns with either uio_resid unchanged, if you are at the 2881 * end of the directory, or uio_resid == 0, with all DIRBLKSIZ chunks 2882 * filled in. 2883 * I felt this would allow caching of directory blocks more easily 2884 * than returning a pertially filled block. 2885 * Directory offset cookies: 2886 * Oh my, what to do with them... 2887 * I can think of three ways to deal with them: 2888 * 1 - have the layer above these RPCs maintain a map between logical 2889 * directory byte offsets and the NFS directory offset cookies 2890 * 2 - pass the opaque directory offset cookies up into userland 2891 * and let the libc functions deal with them, via the system call 2892 * 3 - return them to userland in the "struct dirent", so future versions 2893 * of libc can use them and do whatever is necessary to make things work 2894 * above these rpc calls, in the meantime 2895 * For now, I do #3 by "hiding" the directory offset cookies after the 2896 * d_name field in struct dirent. This is space inside d_reclen that 2897 * will be ignored by anything that doesn't know about them. 2898 * The directory offset cookies are filled in as the last 8 bytes of 2899 * each directory entry, after d_name. Someday, the userland libc 2900 * functions may be able to use these. In the meantime, it satisfies 2901 * OpenBSD's requirements for cookies being returned. 2902 * If expects the directory offset cookie for the read to be in uio_offset 2903 * and returns the one for the next entry after this directory block in 2904 * there, as well. 2905 */ 2906 APPLESTATIC int 2907 nfsrpc_readdir(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep, 2908 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 2909 int *eofp, void *stuff) 2910 { 2911 int len, left; 2912 struct dirent *dp = NULL; 2913 u_int32_t *tl; 2914 nfsquad_t cookie, ncookie; 2915 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 2916 struct nfsnode *dnp = VTONFS(vp); 2917 struct nfsvattr nfsva; 2918 struct nfsrv_descript nfsd, *nd = &nfsd; 2919 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 2920 int reqsize, tryformoredirs = 1, readsize, eof = 0, gotmnton = 0; 2921 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX; 2922 char *cp; 2923 nfsattrbit_t attrbits, dattrbits; 2924 u_int32_t rderr, *tl2 = NULL; 2925 size_t tresid; 2926 2927 KASSERT(uiop->uio_iovcnt == 1 && 2928 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0, 2929 ("nfs readdirrpc bad uio")); 2930 ncookie.lval[0] = ncookie.lval[1] = 0; 2931 /* 2932 * There is no point in reading a lot more than uio_resid, however 2933 * adding one additional DIRBLKSIZ makes sense. Since uio_resid 2934 * and nm_readdirsize are both exact multiples of DIRBLKSIZ, this 2935 * will never make readsize > nm_readdirsize. 2936 */ 2937 readsize = nmp->nm_readdirsize; 2938 if (readsize > uio_uio_resid(uiop)) 2939 readsize = uio_uio_resid(uiop) + DIRBLKSIZ; 2940 2941 *attrflagp = 0; 2942 if (eofp) 2943 *eofp = 0; 2944 tresid = uio_uio_resid(uiop); 2945 cookie.lval[0] = cookiep->nfsuquad[0]; 2946 cookie.lval[1] = cookiep->nfsuquad[1]; 2947 nd->nd_mrep = NULL; 2948 2949 /* 2950 * For NFSv4, first create the "." and ".." entries. 2951 */ 2952 if (NFSHASNFSV4(nmp)) { 2953 reqsize = 6 * NFSX_UNSIGNED; 2954 NFSGETATTR_ATTRBIT(&dattrbits); 2955 NFSZERO_ATTRBIT(&attrbits); 2956 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID); 2957 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TYPE); 2958 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr, 2959 NFSATTRBIT_MOUNTEDONFILEID)) { 2960 NFSSETBIT_ATTRBIT(&attrbits, 2961 NFSATTRBIT_MOUNTEDONFILEID); 2962 gotmnton = 1; 2963 } else { 2964 /* 2965 * Must fake it. Use the fileno, except when the 2966 * fsid is != to that of the directory. For that 2967 * case, generate a fake fileno that is not the same. 2968 */ 2969 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID); 2970 gotmnton = 0; 2971 } 2972 2973 /* 2974 * Joy, oh joy. For V4 we get to hand craft '.' and '..'. 2975 */ 2976 if (uiop->uio_offset == 0) { 2977 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp); 2978 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2979 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 2980 *tl = txdr_unsigned(NFSV4OP_GETATTR); 2981 (void) nfsrv_putattrbit(nd, &attrbits); 2982 error = nfscl_request(nd, vp, p, cred, stuff); 2983 if (error) 2984 return (error); 2985 dotfileid = 0; /* Fake out the compiler. */ 2986 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 2987 error = nfsm_loadattr(nd, &nfsva); 2988 if (error != 0) 2989 goto nfsmout; 2990 dotfileid = nfsva.na_fileid; 2991 } 2992 if (nd->nd_repstat == 0) { 2993 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2994 len = fxdr_unsigned(int, *(tl + 4)); 2995 if (len > 0 && len <= NFSX_V4FHMAX) 2996 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 2997 else 2998 error = EPERM; 2999 if (!error) { 3000 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3001 nfsva.na_mntonfileno = UINT64_MAX; 3002 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 3003 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3004 NULL, NULL, NULL, p, cred); 3005 if (error) { 3006 dotdotfileid = dotfileid; 3007 } else if (gotmnton) { 3008 if (nfsva.na_mntonfileno != UINT64_MAX) 3009 dotdotfileid = nfsva.na_mntonfileno; 3010 else 3011 dotdotfileid = nfsva.na_fileid; 3012 } else if (nfsva.na_filesid[0] == 3013 dnp->n_vattr.na_filesid[0] && 3014 nfsva.na_filesid[1] == 3015 dnp->n_vattr.na_filesid[1]) { 3016 dotdotfileid = nfsva.na_fileid; 3017 } else { 3018 do { 3019 fakefileno--; 3020 } while (fakefileno == 3021 nfsva.na_fileid); 3022 dotdotfileid = fakefileno; 3023 } 3024 } 3025 } else if (nd->nd_repstat == NFSERR_NOENT) { 3026 /* 3027 * Lookupp returns NFSERR_NOENT when we are 3028 * at the root, so just use the current dir. 3029 */ 3030 nd->nd_repstat = 0; 3031 dotdotfileid = dotfileid; 3032 } else { 3033 error = nd->nd_repstat; 3034 } 3035 m_freem(nd->nd_mrep); 3036 if (error) 3037 return (error); 3038 nd->nd_mrep = NULL; 3039 dp = (struct dirent *)uio_iov_base(uiop); 3040 dp->d_pad0 = dp->d_pad1 = 0; 3041 dp->d_off = 0; 3042 dp->d_type = DT_DIR; 3043 dp->d_fileno = dotfileid; 3044 dp->d_namlen = 1; 3045 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */ 3046 dp->d_name[0] = '.'; 3047 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3048 /* 3049 * Just make these offset cookie 0. 3050 */ 3051 tl = (u_int32_t *)&dp->d_name[8]; 3052 *tl++ = 0; 3053 *tl = 0; 3054 blksiz += dp->d_reclen; 3055 uio_uio_resid_add(uiop, -(dp->d_reclen)); 3056 uiop->uio_offset += dp->d_reclen; 3057 uio_iov_base_add(uiop, dp->d_reclen); 3058 uio_iov_len_add(uiop, -(dp->d_reclen)); 3059 dp = (struct dirent *)uio_iov_base(uiop); 3060 dp->d_pad0 = dp->d_pad1 = 0; 3061 dp->d_off = 0; 3062 dp->d_type = DT_DIR; 3063 dp->d_fileno = dotdotfileid; 3064 dp->d_namlen = 2; 3065 *((uint64_t *)dp->d_name) = 0; 3066 dp->d_name[0] = '.'; 3067 dp->d_name[1] = '.'; 3068 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3069 /* 3070 * Just make these offset cookie 0. 3071 */ 3072 tl = (u_int32_t *)&dp->d_name[8]; 3073 *tl++ = 0; 3074 *tl = 0; 3075 blksiz += dp->d_reclen; 3076 uio_uio_resid_add(uiop, -(dp->d_reclen)); 3077 uiop->uio_offset += dp->d_reclen; 3078 uio_iov_base_add(uiop, dp->d_reclen); 3079 uio_iov_len_add(uiop, -(dp->d_reclen)); 3080 } 3081 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_RDATTRERROR); 3082 } else { 3083 reqsize = 5 * NFSX_UNSIGNED; 3084 } 3085 3086 3087 /* 3088 * Loop around doing readdir rpc's of size readsize. 3089 * The stopping criteria is EOF or buffer full. 3090 */ 3091 while (more_dirs && bigenough) { 3092 *attrflagp = 0; 3093 NFSCL_REQSTART(nd, NFSPROC_READDIR, vp); 3094 if (nd->nd_flag & ND_NFSV2) { 3095 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3096 *tl++ = cookie.lval[1]; 3097 *tl = txdr_unsigned(readsize); 3098 } else { 3099 NFSM_BUILD(tl, u_int32_t *, reqsize); 3100 *tl++ = cookie.lval[0]; 3101 *tl++ = cookie.lval[1]; 3102 if (cookie.qval == 0) { 3103 *tl++ = 0; 3104 *tl++ = 0; 3105 } else { 3106 NFSLOCKNODE(dnp); 3107 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 3108 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 3109 NFSUNLOCKNODE(dnp); 3110 } 3111 if (nd->nd_flag & ND_NFSV4) { 3112 *tl++ = txdr_unsigned(readsize); 3113 *tl = txdr_unsigned(readsize); 3114 (void) nfsrv_putattrbit(nd, &attrbits); 3115 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3116 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3117 (void) nfsrv_putattrbit(nd, &dattrbits); 3118 } else { 3119 *tl = txdr_unsigned(readsize); 3120 } 3121 } 3122 error = nfscl_request(nd, vp, p, cred, stuff); 3123 if (error) 3124 return (error); 3125 if (!(nd->nd_flag & ND_NFSV2)) { 3126 if (nd->nd_flag & ND_NFSV3) 3127 error = nfscl_postop_attr(nd, nap, attrflagp, 3128 stuff); 3129 if (!nd->nd_repstat && !error) { 3130 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 3131 NFSLOCKNODE(dnp); 3132 dnp->n_cookieverf.nfsuquad[0] = *tl++; 3133 dnp->n_cookieverf.nfsuquad[1] = *tl; 3134 NFSUNLOCKNODE(dnp); 3135 } 3136 } 3137 if (nd->nd_repstat || error) { 3138 if (!error) 3139 error = nd->nd_repstat; 3140 goto nfsmout; 3141 } 3142 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3143 more_dirs = fxdr_unsigned(int, *tl); 3144 if (!more_dirs) 3145 tryformoredirs = 0; 3146 3147 /* loop through the dir entries, doctoring them to 4bsd form */ 3148 while (more_dirs && bigenough) { 3149 if (nd->nd_flag & ND_NFSV4) { 3150 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3151 ncookie.lval[0] = *tl++; 3152 ncookie.lval[1] = *tl++; 3153 len = fxdr_unsigned(int, *tl); 3154 } else if (nd->nd_flag & ND_NFSV3) { 3155 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3156 nfsva.na_fileid = fxdr_hyper(tl); 3157 tl += 2; 3158 len = fxdr_unsigned(int, *tl); 3159 } else { 3160 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3161 nfsva.na_fileid = fxdr_unsigned(uint64_t, 3162 *tl++); 3163 len = fxdr_unsigned(int, *tl); 3164 } 3165 if (len <= 0 || len > NFS_MAXNAMLEN) { 3166 error = EBADRPC; 3167 goto nfsmout; 3168 } 3169 tlen = roundup2(len, 8); 3170 if (tlen == len) 3171 tlen += 8; /* To ensure null termination. */ 3172 left = DIRBLKSIZ - blksiz; 3173 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) { 3174 NFSBZERO(uio_iov_base(uiop), left); 3175 dp->d_reclen += left; 3176 uio_iov_base_add(uiop, left); 3177 uio_iov_len_add(uiop, -(left)); 3178 uio_uio_resid_add(uiop, -(left)); 3179 uiop->uio_offset += left; 3180 blksiz = 0; 3181 } 3182 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > 3183 uio_uio_resid(uiop)) 3184 bigenough = 0; 3185 if (bigenough) { 3186 dp = (struct dirent *)uio_iov_base(uiop); 3187 dp->d_pad0 = dp->d_pad1 = 0; 3188 dp->d_off = 0; 3189 dp->d_namlen = len; 3190 dp->d_reclen = _GENERIC_DIRLEN(len) + 3191 NFSX_HYPER; 3192 dp->d_type = DT_UNKNOWN; 3193 blksiz += dp->d_reclen; 3194 if (blksiz == DIRBLKSIZ) 3195 blksiz = 0; 3196 uio_uio_resid_add(uiop, -(DIRHDSIZ)); 3197 uiop->uio_offset += DIRHDSIZ; 3198 uio_iov_base_add(uiop, DIRHDSIZ); 3199 uio_iov_len_add(uiop, -(DIRHDSIZ)); 3200 error = nfsm_mbufuio(nd, uiop, len); 3201 if (error) 3202 goto nfsmout; 3203 cp = uio_iov_base(uiop); 3204 tlen -= len; 3205 NFSBZERO(cp, tlen); 3206 cp += tlen; /* points to cookie storage */ 3207 tl2 = (u_int32_t *)cp; 3208 uio_iov_base_add(uiop, (tlen + NFSX_HYPER)); 3209 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER)); 3210 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER)); 3211 uiop->uio_offset += (tlen + NFSX_HYPER); 3212 } else { 3213 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3214 if (error) 3215 goto nfsmout; 3216 } 3217 if (nd->nd_flag & ND_NFSV4) { 3218 rderr = 0; 3219 nfsva.na_mntonfileno = UINT64_MAX; 3220 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 3221 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3222 NULL, NULL, &rderr, p, cred); 3223 if (error) 3224 goto nfsmout; 3225 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3226 } else if (nd->nd_flag & ND_NFSV3) { 3227 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3228 ncookie.lval[0] = *tl++; 3229 ncookie.lval[1] = *tl++; 3230 } else { 3231 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3232 ncookie.lval[0] = 0; 3233 ncookie.lval[1] = *tl++; 3234 } 3235 if (bigenough) { 3236 if (nd->nd_flag & ND_NFSV4) { 3237 if (rderr) { 3238 dp->d_fileno = 0; 3239 } else { 3240 if (gotmnton) { 3241 if (nfsva.na_mntonfileno != UINT64_MAX) 3242 dp->d_fileno = nfsva.na_mntonfileno; 3243 else 3244 dp->d_fileno = nfsva.na_fileid; 3245 } else if (nfsva.na_filesid[0] == 3246 dnp->n_vattr.na_filesid[0] && 3247 nfsva.na_filesid[1] == 3248 dnp->n_vattr.na_filesid[1]) { 3249 dp->d_fileno = nfsva.na_fileid; 3250 } else { 3251 do { 3252 fakefileno--; 3253 } while (fakefileno == 3254 nfsva.na_fileid); 3255 dp->d_fileno = fakefileno; 3256 } 3257 dp->d_type = vtonfs_dtype(nfsva.na_type); 3258 } 3259 } else { 3260 dp->d_fileno = nfsva.na_fileid; 3261 } 3262 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 3263 ncookie.lval[0]; 3264 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 3265 ncookie.lval[1]; 3266 } 3267 more_dirs = fxdr_unsigned(int, *tl); 3268 } 3269 /* 3270 * If at end of rpc data, get the eof boolean 3271 */ 3272 if (!more_dirs) { 3273 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3274 eof = fxdr_unsigned(int, *tl); 3275 if (tryformoredirs) 3276 more_dirs = !eof; 3277 if (nd->nd_flag & ND_NFSV4) { 3278 error = nfscl_postop_attr(nd, nap, attrflagp, 3279 stuff); 3280 if (error) 3281 goto nfsmout; 3282 } 3283 } 3284 m_freem(nd->nd_mrep); 3285 nd->nd_mrep = NULL; 3286 } 3287 /* 3288 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 3289 * by increasing d_reclen for the last record. 3290 */ 3291 if (blksiz > 0) { 3292 left = DIRBLKSIZ - blksiz; 3293 NFSBZERO(uio_iov_base(uiop), left); 3294 dp->d_reclen += left; 3295 uio_iov_base_add(uiop, left); 3296 uio_iov_len_add(uiop, -(left)); 3297 uio_uio_resid_add(uiop, -(left)); 3298 uiop->uio_offset += left; 3299 } 3300 3301 /* 3302 * If returning no data, assume end of file. 3303 * If not bigenough, return not end of file, since you aren't 3304 * returning all the data 3305 * Otherwise, return the eof flag from the server. 3306 */ 3307 if (eofp) { 3308 if (tresid == ((size_t)(uio_uio_resid(uiop)))) 3309 *eofp = 1; 3310 else if (!bigenough) 3311 *eofp = 0; 3312 else 3313 *eofp = eof; 3314 } 3315 3316 /* 3317 * Add extra empty records to any remaining DIRBLKSIZ chunks. 3318 */ 3319 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) { 3320 dp = (struct dirent *)uio_iov_base(uiop); 3321 NFSBZERO(dp, DIRBLKSIZ); 3322 dp->d_type = DT_UNKNOWN; 3323 tl = (u_int32_t *)&dp->d_name[4]; 3324 *tl++ = cookie.lval[0]; 3325 *tl = cookie.lval[1]; 3326 dp->d_reclen = DIRBLKSIZ; 3327 uio_iov_base_add(uiop, DIRBLKSIZ); 3328 uio_iov_len_add(uiop, -(DIRBLKSIZ)); 3329 uio_uio_resid_add(uiop, -(DIRBLKSIZ)); 3330 uiop->uio_offset += DIRBLKSIZ; 3331 } 3332 3333 nfsmout: 3334 if (nd->nd_mrep != NULL) 3335 m_freem(nd->nd_mrep); 3336 return (error); 3337 } 3338 3339 #ifndef APPLE 3340 /* 3341 * NFS V3 readdir plus RPC. Used in place of nfsrpc_readdir(). 3342 * (Also used for NFS V4 when mount flag set.) 3343 * (ditto above w.r.t. multiple of DIRBLKSIZ, etc.) 3344 */ 3345 APPLESTATIC int 3346 nfsrpc_readdirplus(vnode_t vp, struct uio *uiop, nfsuint64 *cookiep, 3347 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 3348 int *eofp, void *stuff) 3349 { 3350 int len, left; 3351 struct dirent *dp = NULL; 3352 u_int32_t *tl; 3353 vnode_t newvp = NULLVP; 3354 struct nfsrv_descript nfsd, *nd = &nfsd; 3355 struct nameidata nami, *ndp = &nami; 3356 struct componentname *cnp = &ndp->ni_cnd; 3357 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3358 struct nfsnode *dnp = VTONFS(vp), *np; 3359 struct nfsvattr nfsva; 3360 struct nfsfh *nfhp; 3361 nfsquad_t cookie, ncookie; 3362 int error = 0, tlen, more_dirs = 1, blksiz = 0, bigenough = 1; 3363 int attrflag, tryformoredirs = 1, eof = 0, gotmnton = 0; 3364 int isdotdot = 0, unlocknewvp = 0; 3365 u_int64_t dotfileid, dotdotfileid = 0, fakefileno = UINT64_MAX; 3366 u_int64_t fileno = 0; 3367 char *cp; 3368 nfsattrbit_t attrbits, dattrbits; 3369 size_t tresid; 3370 u_int32_t *tl2 = NULL, rderr; 3371 struct timespec dctime; 3372 3373 KASSERT(uiop->uio_iovcnt == 1 && 3374 (uio_uio_resid(uiop) & (DIRBLKSIZ - 1)) == 0, 3375 ("nfs readdirplusrpc bad uio")); 3376 ncookie.lval[0] = ncookie.lval[1] = 0; 3377 timespecclear(&dctime); 3378 *attrflagp = 0; 3379 if (eofp != NULL) 3380 *eofp = 0; 3381 ndp->ni_dvp = vp; 3382 nd->nd_mrep = NULL; 3383 cookie.lval[0] = cookiep->nfsuquad[0]; 3384 cookie.lval[1] = cookiep->nfsuquad[1]; 3385 tresid = uio_uio_resid(uiop); 3386 3387 /* 3388 * For NFSv4, first create the "." and ".." entries. 3389 */ 3390 if (NFSHASNFSV4(nmp)) { 3391 NFSGETATTR_ATTRBIT(&dattrbits); 3392 NFSZERO_ATTRBIT(&attrbits); 3393 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FILEID); 3394 if (NFSISSET_ATTRBIT(&dnp->n_vattr.na_suppattr, 3395 NFSATTRBIT_MOUNTEDONFILEID)) { 3396 NFSSETBIT_ATTRBIT(&attrbits, 3397 NFSATTRBIT_MOUNTEDONFILEID); 3398 gotmnton = 1; 3399 } else { 3400 /* 3401 * Must fake it. Use the fileno, except when the 3402 * fsid is != to that of the directory. For that 3403 * case, generate a fake fileno that is not the same. 3404 */ 3405 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_FSID); 3406 gotmnton = 0; 3407 } 3408 3409 /* 3410 * Joy, oh joy. For V4 we get to hand craft '.' and '..'. 3411 */ 3412 if (uiop->uio_offset == 0) { 3413 NFSCL_REQSTART(nd, NFSPROC_LOOKUPP, vp); 3414 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3415 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 3416 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3417 (void) nfsrv_putattrbit(nd, &attrbits); 3418 error = nfscl_request(nd, vp, p, cred, stuff); 3419 if (error) 3420 return (error); 3421 dotfileid = 0; /* Fake out the compiler. */ 3422 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 3423 error = nfsm_loadattr(nd, &nfsva); 3424 if (error != 0) 3425 goto nfsmout; 3426 dctime = nfsva.na_ctime; 3427 dotfileid = nfsva.na_fileid; 3428 } 3429 if (nd->nd_repstat == 0) { 3430 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 3431 len = fxdr_unsigned(int, *(tl + 4)); 3432 if (len > 0 && len <= NFSX_V4FHMAX) 3433 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3434 else 3435 error = EPERM; 3436 if (!error) { 3437 NFSM_DISSECT(tl, u_int32_t *, 2*NFSX_UNSIGNED); 3438 nfsva.na_mntonfileno = UINT64_MAX; 3439 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 3440 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3441 NULL, NULL, NULL, p, cred); 3442 if (error) { 3443 dotdotfileid = dotfileid; 3444 } else if (gotmnton) { 3445 if (nfsva.na_mntonfileno != UINT64_MAX) 3446 dotdotfileid = nfsva.na_mntonfileno; 3447 else 3448 dotdotfileid = nfsva.na_fileid; 3449 } else if (nfsva.na_filesid[0] == 3450 dnp->n_vattr.na_filesid[0] && 3451 nfsva.na_filesid[1] == 3452 dnp->n_vattr.na_filesid[1]) { 3453 dotdotfileid = nfsva.na_fileid; 3454 } else { 3455 do { 3456 fakefileno--; 3457 } while (fakefileno == 3458 nfsva.na_fileid); 3459 dotdotfileid = fakefileno; 3460 } 3461 } 3462 } else if (nd->nd_repstat == NFSERR_NOENT) { 3463 /* 3464 * Lookupp returns NFSERR_NOENT when we are 3465 * at the root, so just use the current dir. 3466 */ 3467 nd->nd_repstat = 0; 3468 dotdotfileid = dotfileid; 3469 } else { 3470 error = nd->nd_repstat; 3471 } 3472 m_freem(nd->nd_mrep); 3473 if (error) 3474 return (error); 3475 nd->nd_mrep = NULL; 3476 dp = (struct dirent *)uio_iov_base(uiop); 3477 dp->d_pad0 = dp->d_pad1 = 0; 3478 dp->d_off = 0; 3479 dp->d_type = DT_DIR; 3480 dp->d_fileno = dotfileid; 3481 dp->d_namlen = 1; 3482 *((uint64_t *)dp->d_name) = 0; /* Zero pad it. */ 3483 dp->d_name[0] = '.'; 3484 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3485 /* 3486 * Just make these offset cookie 0. 3487 */ 3488 tl = (u_int32_t *)&dp->d_name[8]; 3489 *tl++ = 0; 3490 *tl = 0; 3491 blksiz += dp->d_reclen; 3492 uio_uio_resid_add(uiop, -(dp->d_reclen)); 3493 uiop->uio_offset += dp->d_reclen; 3494 uio_iov_base_add(uiop, dp->d_reclen); 3495 uio_iov_len_add(uiop, -(dp->d_reclen)); 3496 dp = (struct dirent *)uio_iov_base(uiop); 3497 dp->d_pad0 = dp->d_pad1 = 0; 3498 dp->d_off = 0; 3499 dp->d_type = DT_DIR; 3500 dp->d_fileno = dotdotfileid; 3501 dp->d_namlen = 2; 3502 *((uint64_t *)dp->d_name) = 0; 3503 dp->d_name[0] = '.'; 3504 dp->d_name[1] = '.'; 3505 dp->d_reclen = _GENERIC_DIRSIZ(dp) + NFSX_HYPER; 3506 /* 3507 * Just make these offset cookie 0. 3508 */ 3509 tl = (u_int32_t *)&dp->d_name[8]; 3510 *tl++ = 0; 3511 *tl = 0; 3512 blksiz += dp->d_reclen; 3513 uio_uio_resid_add(uiop, -(dp->d_reclen)); 3514 uiop->uio_offset += dp->d_reclen; 3515 uio_iov_base_add(uiop, dp->d_reclen); 3516 uio_iov_len_add(uiop, -(dp->d_reclen)); 3517 } 3518 NFSREADDIRPLUS_ATTRBIT(&attrbits); 3519 if (gotmnton) 3520 NFSSETBIT_ATTRBIT(&attrbits, 3521 NFSATTRBIT_MOUNTEDONFILEID); 3522 } 3523 3524 /* 3525 * Loop around doing readdir rpc's of size nm_readdirsize. 3526 * The stopping criteria is EOF or buffer full. 3527 */ 3528 while (more_dirs && bigenough) { 3529 *attrflagp = 0; 3530 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp); 3531 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 3532 *tl++ = cookie.lval[0]; 3533 *tl++ = cookie.lval[1]; 3534 if (cookie.qval == 0) { 3535 *tl++ = 0; 3536 *tl++ = 0; 3537 } else { 3538 NFSLOCKNODE(dnp); 3539 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 3540 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 3541 NFSUNLOCKNODE(dnp); 3542 } 3543 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 3544 *tl = txdr_unsigned(nmp->nm_readdirsize); 3545 if (nd->nd_flag & ND_NFSV4) { 3546 (void) nfsrv_putattrbit(nd, &attrbits); 3547 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3548 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3549 (void) nfsrv_putattrbit(nd, &dattrbits); 3550 } 3551 error = nfscl_request(nd, vp, p, cred, stuff); 3552 if (error) 3553 return (error); 3554 if (nd->nd_flag & ND_NFSV3) 3555 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3556 if (nd->nd_repstat || error) { 3557 if (!error) 3558 error = nd->nd_repstat; 3559 goto nfsmout; 3560 } 3561 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0) 3562 dctime = nap->na_ctime; 3563 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3564 NFSLOCKNODE(dnp); 3565 dnp->n_cookieverf.nfsuquad[0] = *tl++; 3566 dnp->n_cookieverf.nfsuquad[1] = *tl++; 3567 NFSUNLOCKNODE(dnp); 3568 more_dirs = fxdr_unsigned(int, *tl); 3569 if (!more_dirs) 3570 tryformoredirs = 0; 3571 3572 /* loop through the dir entries, doctoring them to 4bsd form */ 3573 while (more_dirs && bigenough) { 3574 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3575 if (nd->nd_flag & ND_NFSV4) { 3576 ncookie.lval[0] = *tl++; 3577 ncookie.lval[1] = *tl++; 3578 } else { 3579 fileno = fxdr_hyper(tl); 3580 tl += 2; 3581 } 3582 len = fxdr_unsigned(int, *tl); 3583 if (len <= 0 || len > NFS_MAXNAMLEN) { 3584 error = EBADRPC; 3585 goto nfsmout; 3586 } 3587 tlen = roundup2(len, 8); 3588 if (tlen == len) 3589 tlen += 8; /* To ensure null termination. */ 3590 left = DIRBLKSIZ - blksiz; 3591 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) { 3592 NFSBZERO(uio_iov_base(uiop), left); 3593 dp->d_reclen += left; 3594 uio_iov_base_add(uiop, left); 3595 uio_iov_len_add(uiop, -(left)); 3596 uio_uio_resid_add(uiop, -(left)); 3597 uiop->uio_offset += left; 3598 blksiz = 0; 3599 } 3600 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > 3601 uio_uio_resid(uiop)) 3602 bigenough = 0; 3603 if (bigenough) { 3604 dp = (struct dirent *)uio_iov_base(uiop); 3605 dp->d_pad0 = dp->d_pad1 = 0; 3606 dp->d_off = 0; 3607 dp->d_namlen = len; 3608 dp->d_reclen = _GENERIC_DIRLEN(len) + 3609 NFSX_HYPER; 3610 dp->d_type = DT_UNKNOWN; 3611 blksiz += dp->d_reclen; 3612 if (blksiz == DIRBLKSIZ) 3613 blksiz = 0; 3614 uio_uio_resid_add(uiop, -(DIRHDSIZ)); 3615 uiop->uio_offset += DIRHDSIZ; 3616 uio_iov_base_add(uiop, DIRHDSIZ); 3617 uio_iov_len_add(uiop, -(DIRHDSIZ)); 3618 cnp->cn_nameptr = uio_iov_base(uiop); 3619 cnp->cn_namelen = len; 3620 NFSCNHASHZERO(cnp); 3621 error = nfsm_mbufuio(nd, uiop, len); 3622 if (error) 3623 goto nfsmout; 3624 cp = uio_iov_base(uiop); 3625 tlen -= len; 3626 NFSBZERO(cp, tlen); 3627 cp += tlen; /* points to cookie storage */ 3628 tl2 = (u_int32_t *)cp; 3629 if (len == 2 && cnp->cn_nameptr[0] == '.' && 3630 cnp->cn_nameptr[1] == '.') 3631 isdotdot = 1; 3632 else 3633 isdotdot = 0; 3634 uio_iov_base_add(uiop, (tlen + NFSX_HYPER)); 3635 uio_iov_len_add(uiop, -(tlen + NFSX_HYPER)); 3636 uio_uio_resid_add(uiop, -(tlen + NFSX_HYPER)); 3637 uiop->uio_offset += (tlen + NFSX_HYPER); 3638 } else { 3639 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 3640 if (error) 3641 goto nfsmout; 3642 } 3643 nfhp = NULL; 3644 if (nd->nd_flag & ND_NFSV3) { 3645 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 3646 ncookie.lval[0] = *tl++; 3647 ncookie.lval[1] = *tl++; 3648 attrflag = fxdr_unsigned(int, *tl); 3649 if (attrflag) { 3650 error = nfsm_loadattr(nd, &nfsva); 3651 if (error) 3652 goto nfsmout; 3653 } 3654 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED); 3655 if (*tl) { 3656 error = nfsm_getfh(nd, &nfhp); 3657 if (error) 3658 goto nfsmout; 3659 } 3660 if (!attrflag && nfhp != NULL) { 3661 free(nfhp, M_NFSFH); 3662 nfhp = NULL; 3663 } 3664 } else { 3665 rderr = 0; 3666 nfsva.na_mntonfileno = 0xffffffff; 3667 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp, 3668 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 3669 NULL, NULL, &rderr, p, cred); 3670 if (error) 3671 goto nfsmout; 3672 } 3673 3674 if (bigenough) { 3675 if (nd->nd_flag & ND_NFSV4) { 3676 if (rderr) { 3677 dp->d_fileno = 0; 3678 } else if (gotmnton) { 3679 if (nfsva.na_mntonfileno != 0xffffffff) 3680 dp->d_fileno = nfsva.na_mntonfileno; 3681 else 3682 dp->d_fileno = nfsva.na_fileid; 3683 } else if (nfsva.na_filesid[0] == 3684 dnp->n_vattr.na_filesid[0] && 3685 nfsva.na_filesid[1] == 3686 dnp->n_vattr.na_filesid[1]) { 3687 dp->d_fileno = nfsva.na_fileid; 3688 } else { 3689 do { 3690 fakefileno--; 3691 } while (fakefileno == 3692 nfsva.na_fileid); 3693 dp->d_fileno = fakefileno; 3694 } 3695 } else { 3696 dp->d_fileno = fileno; 3697 } 3698 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 3699 ncookie.lval[0]; 3700 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 3701 ncookie.lval[1]; 3702 3703 if (nfhp != NULL) { 3704 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len, 3705 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) { 3706 VREF(vp); 3707 newvp = vp; 3708 unlocknewvp = 0; 3709 free(nfhp, M_NFSFH); 3710 np = dnp; 3711 } else if (isdotdot != 0) { 3712 /* 3713 * Skip doing a nfscl_nget() call for "..". 3714 * There's a race between acquiring the nfs 3715 * node here and lookups that look for the 3716 * directory being read (in the parent). 3717 * It would try to get a lock on ".." here, 3718 * owning the lock on the directory being 3719 * read. Lookup will hold the lock on ".." 3720 * and try to acquire the lock on the 3721 * directory being read. 3722 * If the directory is unlocked/relocked, 3723 * then there is a LOR with the buflock 3724 * vp is relocked. 3725 */ 3726 free(nfhp, M_NFSFH); 3727 } else { 3728 error = nfscl_nget(vnode_mount(vp), vp, 3729 nfhp, cnp, p, &np, NULL, LK_EXCLUSIVE); 3730 if (!error) { 3731 newvp = NFSTOV(np); 3732 unlocknewvp = 1; 3733 } 3734 } 3735 nfhp = NULL; 3736 if (newvp != NULLVP) { 3737 error = nfscl_loadattrcache(&newvp, 3738 &nfsva, NULL, NULL, 0, 0); 3739 if (error) { 3740 if (unlocknewvp) 3741 vput(newvp); 3742 else 3743 vrele(newvp); 3744 goto nfsmout; 3745 } 3746 dp->d_type = 3747 vtonfs_dtype(np->n_vattr.na_type); 3748 ndp->ni_vp = newvp; 3749 NFSCNHASH(cnp, HASHINIT); 3750 if (cnp->cn_namelen <= NCHNAMLEN && 3751 (newvp->v_type != VDIR || 3752 dctime.tv_sec != 0)) { 3753 cache_enter_time(ndp->ni_dvp, 3754 ndp->ni_vp, cnp, 3755 &nfsva.na_ctime, 3756 newvp->v_type != VDIR ? NULL : 3757 &dctime); 3758 } 3759 if (unlocknewvp) 3760 vput(newvp); 3761 else 3762 vrele(newvp); 3763 newvp = NULLVP; 3764 } 3765 } 3766 } else if (nfhp != NULL) { 3767 free(nfhp, M_NFSFH); 3768 } 3769 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3770 more_dirs = fxdr_unsigned(int, *tl); 3771 } 3772 /* 3773 * If at end of rpc data, get the eof boolean 3774 */ 3775 if (!more_dirs) { 3776 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3777 eof = fxdr_unsigned(int, *tl); 3778 if (tryformoredirs) 3779 more_dirs = !eof; 3780 if (nd->nd_flag & ND_NFSV4) { 3781 error = nfscl_postop_attr(nd, nap, attrflagp, 3782 stuff); 3783 if (error) 3784 goto nfsmout; 3785 } 3786 } 3787 m_freem(nd->nd_mrep); 3788 nd->nd_mrep = NULL; 3789 } 3790 /* 3791 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 3792 * by increasing d_reclen for the last record. 3793 */ 3794 if (blksiz > 0) { 3795 left = DIRBLKSIZ - blksiz; 3796 NFSBZERO(uio_iov_base(uiop), left); 3797 dp->d_reclen += left; 3798 uio_iov_base_add(uiop, left); 3799 uio_iov_len_add(uiop, -(left)); 3800 uio_uio_resid_add(uiop, -(left)); 3801 uiop->uio_offset += left; 3802 } 3803 3804 /* 3805 * If returning no data, assume end of file. 3806 * If not bigenough, return not end of file, since you aren't 3807 * returning all the data 3808 * Otherwise, return the eof flag from the server. 3809 */ 3810 if (eofp != NULL) { 3811 if (tresid == uio_uio_resid(uiop)) 3812 *eofp = 1; 3813 else if (!bigenough) 3814 *eofp = 0; 3815 else 3816 *eofp = eof; 3817 } 3818 3819 /* 3820 * Add extra empty records to any remaining DIRBLKSIZ chunks. 3821 */ 3822 while (uio_uio_resid(uiop) > 0 && uio_uio_resid(uiop) != tresid) { 3823 dp = (struct dirent *)uio_iov_base(uiop); 3824 NFSBZERO(dp, DIRBLKSIZ); 3825 dp->d_type = DT_UNKNOWN; 3826 tl = (u_int32_t *)&dp->d_name[4]; 3827 *tl++ = cookie.lval[0]; 3828 *tl = cookie.lval[1]; 3829 dp->d_reclen = DIRBLKSIZ; 3830 uio_iov_base_add(uiop, DIRBLKSIZ); 3831 uio_iov_len_add(uiop, -(DIRBLKSIZ)); 3832 uio_uio_resid_add(uiop, -(DIRBLKSIZ)); 3833 uiop->uio_offset += DIRBLKSIZ; 3834 } 3835 3836 nfsmout: 3837 if (nd->nd_mrep != NULL) 3838 m_freem(nd->nd_mrep); 3839 return (error); 3840 } 3841 #endif /* !APPLE */ 3842 3843 /* 3844 * Nfs commit rpc 3845 */ 3846 APPLESTATIC int 3847 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred, 3848 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 3849 { 3850 u_int32_t *tl; 3851 struct nfsrv_descript nfsd, *nd = &nfsd; 3852 nfsattrbit_t attrbits; 3853 int error; 3854 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3855 3856 *attrflagp = 0; 3857 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp); 3858 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3859 txdr_hyper(offset, tl); 3860 tl += 2; 3861 *tl = txdr_unsigned(cnt); 3862 if (nd->nd_flag & ND_NFSV4) { 3863 /* 3864 * And do a Getattr op. 3865 */ 3866 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 3867 *tl = txdr_unsigned(NFSV4OP_GETATTR); 3868 NFSGETATTR_ATTRBIT(&attrbits); 3869 (void) nfsrv_putattrbit(nd, &attrbits); 3870 } 3871 error = nfscl_request(nd, vp, p, cred, stuff); 3872 if (error) 3873 return (error); 3874 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, stuff); 3875 if (!error && !nd->nd_repstat) { 3876 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 3877 NFSLOCKMNT(nmp); 3878 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) { 3879 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 3880 nd->nd_repstat = NFSERR_STALEWRITEVERF; 3881 } 3882 NFSUNLOCKMNT(nmp); 3883 if (nd->nd_flag & ND_NFSV4) 3884 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 3885 } 3886 nfsmout: 3887 if (!error && nd->nd_repstat) 3888 error = nd->nd_repstat; 3889 m_freem(nd->nd_mrep); 3890 return (error); 3891 } 3892 3893 /* 3894 * NFS byte range lock rpc. 3895 * (Mostly just calls one of the three lower level RPC routines.) 3896 */ 3897 APPLESTATIC int 3898 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl, 3899 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags) 3900 { 3901 struct nfscllockowner *lp; 3902 struct nfsclclient *clp; 3903 struct nfsfh *nfhp; 3904 struct nfsrv_descript nfsd, *nd = &nfsd; 3905 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 3906 u_int64_t off, len; 3907 off_t start, end; 3908 u_int32_t clidrev = 0; 3909 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally; 3910 int callcnt, dorpc; 3911 3912 /* 3913 * Convert the flock structure into a start and end and do POSIX 3914 * bounds checking. 3915 */ 3916 switch (fl->l_whence) { 3917 case SEEK_SET: 3918 case SEEK_CUR: 3919 /* 3920 * Caller is responsible for adding any necessary offset 3921 * when SEEK_CUR is used. 3922 */ 3923 start = fl->l_start; 3924 off = fl->l_start; 3925 break; 3926 case SEEK_END: 3927 start = size + fl->l_start; 3928 off = size + fl->l_start; 3929 break; 3930 default: 3931 return (EINVAL); 3932 } 3933 if (start < 0) 3934 return (EINVAL); 3935 if (fl->l_len != 0) { 3936 end = start + fl->l_len - 1; 3937 if (end < start) 3938 return (EINVAL); 3939 } 3940 3941 len = fl->l_len; 3942 if (len == 0) 3943 len = NFS64BITSSET; 3944 retrycnt = 0; 3945 do { 3946 nd->nd_repstat = 0; 3947 if (op == F_GETLK) { 3948 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp); 3949 if (error) 3950 return (error); 3951 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags); 3952 if (!error) { 3953 clidrev = clp->nfsc_clientidrev; 3954 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred, 3955 p, id, flags); 3956 } else if (error == -1) { 3957 error = 0; 3958 } 3959 nfscl_clientrelease(clp); 3960 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) { 3961 /* 3962 * We must loop around for all lockowner cases. 3963 */ 3964 callcnt = 0; 3965 error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp); 3966 if (error) 3967 return (error); 3968 do { 3969 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt, 3970 clp, id, flags, &lp, &dorpc); 3971 /* 3972 * If it returns a NULL lp, we're done. 3973 */ 3974 if (lp == NULL) { 3975 if (callcnt == 0) 3976 nfscl_clientrelease(clp); 3977 else 3978 nfscl_releasealllocks(clp, vp, p, id, flags); 3979 return (error); 3980 } 3981 if (nmp->nm_clp != NULL) 3982 clidrev = nmp->nm_clp->nfsc_clientidrev; 3983 else 3984 clidrev = 0; 3985 /* 3986 * If the server doesn't support Posix lock semantics, 3987 * only allow locks on the entire file, since it won't 3988 * handle overlapping byte ranges. 3989 * There might still be a problem when a lock 3990 * upgrade/downgrade (read<->write) occurs, since the 3991 * server "might" expect an unlock first? 3992 */ 3993 if (dorpc && (lp->nfsl_open->nfso_posixlock || 3994 (off == 0 && len == NFS64BITSSET))) { 3995 /* 3996 * Since the lock records will go away, we must 3997 * wait for grace and delay here. 3998 */ 3999 do { 4000 error = nfsrpc_locku(nd, nmp, lp, off, len, 4001 NFSV4LOCKT_READ, cred, p, 0); 4002 if ((nd->nd_repstat == NFSERR_GRACE || 4003 nd->nd_repstat == NFSERR_DELAY) && 4004 error == 0) 4005 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4006 "nfs_advlock"); 4007 } while ((nd->nd_repstat == NFSERR_GRACE || 4008 nd->nd_repstat == NFSERR_DELAY) && error == 0); 4009 } 4010 callcnt++; 4011 } while (error == 0 && nd->nd_repstat == 0); 4012 nfscl_releasealllocks(clp, vp, p, id, flags); 4013 } else if (op == F_SETLK) { 4014 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p, 4015 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally); 4016 if (error || donelocally) { 4017 return (error); 4018 } 4019 if (nmp->nm_clp != NULL) 4020 clidrev = nmp->nm_clp->nfsc_clientidrev; 4021 else 4022 clidrev = 0; 4023 nfhp = VTONFS(vp)->n_fhp; 4024 if (!lp->nfsl_open->nfso_posixlock && 4025 (off != 0 || len != NFS64BITSSET)) { 4026 error = EINVAL; 4027 } else { 4028 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh, 4029 nfhp->nfh_len, lp, newone, reclaim, off, 4030 len, fl->l_type, cred, p, 0); 4031 } 4032 if (!error) 4033 error = nd->nd_repstat; 4034 nfscl_lockrelease(lp, error, newone); 4035 } else { 4036 error = EINVAL; 4037 } 4038 if (!error) 4039 error = nd->nd_repstat; 4040 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 4041 error == NFSERR_STALEDONTRECOVER || 4042 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 4043 error == NFSERR_BADSESSION) { 4044 (void) nfs_catnap(PZERO, error, "nfs_advlock"); 4045 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) 4046 && clidrev != 0) { 4047 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 4048 retrycnt++; 4049 } 4050 } while (error == NFSERR_GRACE || 4051 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 4052 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID || 4053 error == NFSERR_BADSESSION || 4054 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 4055 expireret == 0 && clidrev != 0 && retrycnt < 4)); 4056 if (error && retrycnt >= 4) 4057 error = EIO; 4058 return (error); 4059 } 4060 4061 /* 4062 * The lower level routine for the LockT case. 4063 */ 4064 APPLESTATIC int 4065 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp, 4066 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl, 4067 struct ucred *cred, NFSPROC_T *p, void *id, int flags) 4068 { 4069 u_int32_t *tl; 4070 int error, type, size; 4071 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4072 struct nfsnode *np; 4073 struct nfsmount *nmp; 4074 struct nfsclsession *tsep; 4075 4076 nmp = VFSTONFS(vp->v_mount); 4077 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp); 4078 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 4079 if (fl->l_type == F_RDLCK) 4080 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 4081 else 4082 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 4083 txdr_hyper(off, tl); 4084 tl += 2; 4085 txdr_hyper(len, tl); 4086 tl += 2; 4087 tsep = nfsmnt_mdssession(nmp); 4088 *tl++ = tsep->nfsess_clientid.lval[0]; 4089 *tl = tsep->nfsess_clientid.lval[1]; 4090 nfscl_filllockowner(id, own, flags); 4091 np = VTONFS(vp); 4092 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN], 4093 np->n_fhp->nfh_len); 4094 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len); 4095 error = nfscl_request(nd, vp, p, cred, NULL); 4096 if (error) 4097 return (error); 4098 if (nd->nd_repstat == 0) { 4099 fl->l_type = F_UNLCK; 4100 } else if (nd->nd_repstat == NFSERR_DENIED) { 4101 nd->nd_repstat = 0; 4102 fl->l_whence = SEEK_SET; 4103 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 4104 fl->l_start = fxdr_hyper(tl); 4105 tl += 2; 4106 len = fxdr_hyper(tl); 4107 tl += 2; 4108 if (len == NFS64BITSSET) 4109 fl->l_len = 0; 4110 else 4111 fl->l_len = len; 4112 type = fxdr_unsigned(int, *tl++); 4113 if (type == NFSV4LOCKT_WRITE) 4114 fl->l_type = F_WRLCK; 4115 else 4116 fl->l_type = F_RDLCK; 4117 /* 4118 * XXX For now, I have no idea what to do with the 4119 * conflicting lock_owner, so I'll just set the pid == 0 4120 * and skip over the lock_owner. 4121 */ 4122 fl->l_pid = (pid_t)0; 4123 tl += 2; 4124 size = fxdr_unsigned(int, *tl); 4125 if (size < 0 || size > NFSV4_OPAQUELIMIT) 4126 error = EBADRPC; 4127 if (!error) 4128 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 4129 } else if (nd->nd_repstat == NFSERR_STALECLIENTID) 4130 nfscl_initiate_recovery(clp); 4131 nfsmout: 4132 m_freem(nd->nd_mrep); 4133 return (error); 4134 } 4135 4136 /* 4137 * Lower level function that performs the LockU RPC. 4138 */ 4139 static int 4140 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp, 4141 struct nfscllockowner *lp, u_int64_t off, u_int64_t len, 4142 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred) 4143 { 4144 u_int32_t *tl; 4145 int error; 4146 4147 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh, 4148 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0); 4149 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED); 4150 *tl++ = txdr_unsigned(type); 4151 *tl = txdr_unsigned(lp->nfsl_seqid); 4152 if (nfstest_outofseq && 4153 (arc4random() % nfstest_outofseq) == 0) 4154 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 4155 tl++; 4156 if (NFSHASNFSV4N(nmp)) 4157 *tl++ = 0; 4158 else 4159 *tl++ = lp->nfsl_stateid.seqid; 4160 *tl++ = lp->nfsl_stateid.other[0]; 4161 *tl++ = lp->nfsl_stateid.other[1]; 4162 *tl++ = lp->nfsl_stateid.other[2]; 4163 txdr_hyper(off, tl); 4164 tl += 2; 4165 txdr_hyper(len, tl); 4166 if (syscred) 4167 nd->nd_flag |= ND_USEGSSNAME; 4168 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4169 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4170 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 4171 if (error) 4172 return (error); 4173 if (nd->nd_repstat == 0) { 4174 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 4175 lp->nfsl_stateid.seqid = *tl++; 4176 lp->nfsl_stateid.other[0] = *tl++; 4177 lp->nfsl_stateid.other[1] = *tl++; 4178 lp->nfsl_stateid.other[2] = *tl; 4179 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 4180 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 4181 nfsmout: 4182 m_freem(nd->nd_mrep); 4183 return (error); 4184 } 4185 4186 /* 4187 * The actual Lock RPC. 4188 */ 4189 APPLESTATIC int 4190 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp, 4191 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone, 4192 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred, 4193 NFSPROC_T *p, int syscred) 4194 { 4195 u_int32_t *tl; 4196 int error, size; 4197 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4198 struct nfsclsession *tsep; 4199 4200 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0); 4201 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 4202 if (type == F_RDLCK) 4203 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 4204 else 4205 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 4206 *tl++ = txdr_unsigned(reclaim); 4207 txdr_hyper(off, tl); 4208 tl += 2; 4209 txdr_hyper(len, tl); 4210 tl += 2; 4211 if (newone) { 4212 *tl = newnfs_true; 4213 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 4214 2 * NFSX_UNSIGNED + NFSX_HYPER); 4215 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid); 4216 if (NFSHASNFSV4N(nmp)) 4217 *tl++ = 0; 4218 else 4219 *tl++ = lp->nfsl_open->nfso_stateid.seqid; 4220 *tl++ = lp->nfsl_open->nfso_stateid.other[0]; 4221 *tl++ = lp->nfsl_open->nfso_stateid.other[1]; 4222 *tl++ = lp->nfsl_open->nfso_stateid.other[2]; 4223 *tl++ = txdr_unsigned(lp->nfsl_seqid); 4224 tsep = nfsmnt_mdssession(nmp); 4225 *tl++ = tsep->nfsess_clientid.lval[0]; 4226 *tl = tsep->nfsess_clientid.lval[1]; 4227 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN); 4228 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen); 4229 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen); 4230 } else { 4231 *tl = newnfs_false; 4232 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED); 4233 if (NFSHASNFSV4N(nmp)) 4234 *tl++ = 0; 4235 else 4236 *tl++ = lp->nfsl_stateid.seqid; 4237 *tl++ = lp->nfsl_stateid.other[0]; 4238 *tl++ = lp->nfsl_stateid.other[1]; 4239 *tl++ = lp->nfsl_stateid.other[2]; 4240 *tl = txdr_unsigned(lp->nfsl_seqid); 4241 if (nfstest_outofseq && 4242 (arc4random() % nfstest_outofseq) == 0) 4243 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 4244 } 4245 if (syscred) 4246 nd->nd_flag |= ND_USEGSSNAME; 4247 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 4248 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4249 if (error) 4250 return (error); 4251 if (newone) 4252 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd); 4253 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 4254 if (nd->nd_repstat == 0) { 4255 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 4256 lp->nfsl_stateid.seqid = *tl++; 4257 lp->nfsl_stateid.other[0] = *tl++; 4258 lp->nfsl_stateid.other[1] = *tl++; 4259 lp->nfsl_stateid.other[2] = *tl; 4260 } else if (nd->nd_repstat == NFSERR_DENIED) { 4261 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 4262 size = fxdr_unsigned(int, *(tl + 7)); 4263 if (size < 0 || size > NFSV4_OPAQUELIMIT) 4264 error = EBADRPC; 4265 if (!error) 4266 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 4267 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 4268 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 4269 nfsmout: 4270 m_freem(nd->nd_mrep); 4271 return (error); 4272 } 4273 4274 /* 4275 * nfs statfs rpc 4276 * (always called with the vp for the mount point) 4277 */ 4278 APPLESTATIC int 4279 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp, 4280 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 4281 void *stuff) 4282 { 4283 u_int32_t *tl = NULL; 4284 struct nfsrv_descript nfsd, *nd = &nfsd; 4285 struct nfsmount *nmp; 4286 nfsattrbit_t attrbits; 4287 int error; 4288 4289 *attrflagp = 0; 4290 nmp = VFSTONFS(vnode_mount(vp)); 4291 if (NFSHASNFSV4(nmp)) { 4292 /* 4293 * For V4, you actually do a getattr. 4294 */ 4295 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 4296 NFSSTATFS_GETATTRBIT(&attrbits); 4297 (void) nfsrv_putattrbit(nd, &attrbits); 4298 nd->nd_flag |= ND_USEGSSNAME; 4299 error = nfscl_request(nd, vp, p, cred, stuff); 4300 if (error) 4301 return (error); 4302 if (nd->nd_repstat == 0) { 4303 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 4304 NULL, NULL, sbp, fsp, NULL, 0, NULL, NULL, NULL, p, 4305 cred); 4306 if (!error) { 4307 nmp->nm_fsid[0] = nap->na_filesid[0]; 4308 nmp->nm_fsid[1] = nap->na_filesid[1]; 4309 NFSSETHASSETFSID(nmp); 4310 *attrflagp = 1; 4311 } 4312 } else { 4313 error = nd->nd_repstat; 4314 } 4315 if (error) 4316 goto nfsmout; 4317 } else { 4318 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp); 4319 error = nfscl_request(nd, vp, p, cred, stuff); 4320 if (error) 4321 return (error); 4322 if (nd->nd_flag & ND_NFSV3) { 4323 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 4324 if (error) 4325 goto nfsmout; 4326 } 4327 if (nd->nd_repstat) { 4328 error = nd->nd_repstat; 4329 goto nfsmout; 4330 } 4331 NFSM_DISSECT(tl, u_int32_t *, 4332 NFSX_STATFS(nd->nd_flag & ND_NFSV3)); 4333 } 4334 if (NFSHASNFSV3(nmp)) { 4335 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2; 4336 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2; 4337 sbp->sf_abytes = fxdr_hyper(tl); tl += 2; 4338 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2; 4339 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2; 4340 sbp->sf_afiles = fxdr_hyper(tl); tl += 2; 4341 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl); 4342 } else if (NFSHASNFSV4(nmp) == 0) { 4343 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++); 4344 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++); 4345 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++); 4346 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++); 4347 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl); 4348 } 4349 nfsmout: 4350 m_freem(nd->nd_mrep); 4351 return (error); 4352 } 4353 4354 /* 4355 * nfs pathconf rpc 4356 */ 4357 APPLESTATIC int 4358 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc, 4359 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, 4360 void *stuff) 4361 { 4362 struct nfsrv_descript nfsd, *nd = &nfsd; 4363 struct nfsmount *nmp; 4364 u_int32_t *tl; 4365 nfsattrbit_t attrbits; 4366 int error; 4367 4368 *attrflagp = 0; 4369 nmp = VFSTONFS(vnode_mount(vp)); 4370 if (NFSHASNFSV4(nmp)) { 4371 /* 4372 * For V4, you actually do a getattr. 4373 */ 4374 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp); 4375 NFSPATHCONF_GETATTRBIT(&attrbits); 4376 (void) nfsrv_putattrbit(nd, &attrbits); 4377 nd->nd_flag |= ND_USEGSSNAME; 4378 error = nfscl_request(nd, vp, p, cred, stuff); 4379 if (error) 4380 return (error); 4381 if (nd->nd_repstat == 0) { 4382 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 4383 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, p, 4384 cred); 4385 if (!error) 4386 *attrflagp = 1; 4387 } else { 4388 error = nd->nd_repstat; 4389 } 4390 } else { 4391 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp); 4392 error = nfscl_request(nd, vp, p, cred, stuff); 4393 if (error) 4394 return (error); 4395 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 4396 if (nd->nd_repstat && !error) 4397 error = nd->nd_repstat; 4398 if (!error) { 4399 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF); 4400 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++); 4401 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++); 4402 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++); 4403 pc->pc_chownrestricted = 4404 fxdr_unsigned(u_int32_t, *tl++); 4405 pc->pc_caseinsensitive = 4406 fxdr_unsigned(u_int32_t, *tl++); 4407 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl); 4408 } 4409 } 4410 nfsmout: 4411 m_freem(nd->nd_mrep); 4412 return (error); 4413 } 4414 4415 /* 4416 * nfs version 3 fsinfo rpc call 4417 */ 4418 APPLESTATIC int 4419 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred, 4420 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp, void *stuff) 4421 { 4422 u_int32_t *tl; 4423 struct nfsrv_descript nfsd, *nd = &nfsd; 4424 int error; 4425 4426 *attrflagp = 0; 4427 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp); 4428 error = nfscl_request(nd, vp, p, cred, stuff); 4429 if (error) 4430 return (error); 4431 error = nfscl_postop_attr(nd, nap, attrflagp, stuff); 4432 if (nd->nd_repstat && !error) 4433 error = nd->nd_repstat; 4434 if (!error) { 4435 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO); 4436 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++); 4437 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++); 4438 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++); 4439 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++); 4440 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++); 4441 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++); 4442 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++); 4443 fsp->fs_maxfilesize = fxdr_hyper(tl); 4444 tl += 2; 4445 fxdr_nfsv3time(tl, &fsp->fs_timedelta); 4446 tl += 2; 4447 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl); 4448 } 4449 nfsmout: 4450 m_freem(nd->nd_mrep); 4451 return (error); 4452 } 4453 4454 /* 4455 * This function performs the Renew RPC. 4456 */ 4457 APPLESTATIC int 4458 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred, 4459 NFSPROC_T *p) 4460 { 4461 u_int32_t *tl; 4462 struct nfsrv_descript nfsd; 4463 struct nfsrv_descript *nd = &nfsd; 4464 struct nfsmount *nmp; 4465 int error; 4466 struct nfssockreq *nrp; 4467 struct nfsclsession *tsep; 4468 4469 nmp = clp->nfsc_nmp; 4470 if (nmp == NULL) 4471 return (0); 4472 if (dsp == NULL) 4473 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0, 4474 0); 4475 else 4476 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, 4477 &dsp->nfsclds_sess, 0, 0); 4478 if (!NFSHASNFSV4N(nmp)) { 4479 /* NFSv4.1 just uses a Sequence Op and not a Renew. */ 4480 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 4481 tsep = nfsmnt_mdssession(nmp); 4482 *tl++ = tsep->nfsess_clientid.lval[0]; 4483 *tl = tsep->nfsess_clientid.lval[1]; 4484 } 4485 nrp = NULL; 4486 if (dsp != NULL) 4487 nrp = dsp->nfsclds_sockp; 4488 if (nrp == NULL) 4489 /* If NULL, use the MDS socket. */ 4490 nrp = &nmp->nm_sockreq; 4491 nd->nd_flag |= ND_USEGSSNAME; 4492 if (dsp == NULL) 4493 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 4494 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4495 else { 4496 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 4497 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess); 4498 if (error == ENXIO) 4499 nfscl_cancelreqs(dsp); 4500 } 4501 if (error) 4502 return (error); 4503 error = nd->nd_repstat; 4504 m_freem(nd->nd_mrep); 4505 return (error); 4506 } 4507 4508 /* 4509 * This function performs the Releaselockowner RPC. 4510 */ 4511 APPLESTATIC int 4512 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp, 4513 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p) 4514 { 4515 struct nfsrv_descript nfsd, *nd = &nfsd; 4516 u_int32_t *tl; 4517 int error; 4518 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4519 struct nfsclsession *tsep; 4520 4521 if (NFSHASNFSV4N(nmp)) { 4522 /* For NFSv4.1, do a FreeStateID. */ 4523 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL, 4524 NULL, 0, 0); 4525 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID); 4526 } else { 4527 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL, 4528 NULL, 0, 0); 4529 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 4530 tsep = nfsmnt_mdssession(nmp); 4531 *tl++ = tsep->nfsess_clientid.lval[0]; 4532 *tl = tsep->nfsess_clientid.lval[1]; 4533 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN); 4534 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen); 4535 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen); 4536 } 4537 nd->nd_flag |= ND_USEGSSNAME; 4538 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4539 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4540 if (error) 4541 return (error); 4542 error = nd->nd_repstat; 4543 m_freem(nd->nd_mrep); 4544 return (error); 4545 } 4546 4547 /* 4548 * This function performs the Compound to get the mount pt FH. 4549 */ 4550 APPLESTATIC int 4551 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred, 4552 NFSPROC_T *p) 4553 { 4554 u_int32_t *tl; 4555 struct nfsrv_descript nfsd; 4556 struct nfsrv_descript *nd = &nfsd; 4557 u_char *cp, *cp2; 4558 int error, cnt, len, setnil; 4559 u_int32_t *opcntp; 4560 4561 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0, 4562 0); 4563 cp = dirpath; 4564 cnt = 0; 4565 do { 4566 setnil = 0; 4567 while (*cp == '/') 4568 cp++; 4569 cp2 = cp; 4570 while (*cp2 != '\0' && *cp2 != '/') 4571 cp2++; 4572 if (*cp2 == '/') { 4573 setnil = 1; 4574 *cp2 = '\0'; 4575 } 4576 if (cp2 != cp) { 4577 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4578 *tl = txdr_unsigned(NFSV4OP_LOOKUP); 4579 nfsm_strtom(nd, cp, strlen(cp)); 4580 cnt++; 4581 } 4582 if (setnil) 4583 *cp2++ = '/'; 4584 cp = cp2; 4585 } while (*cp != '\0'); 4586 if (NFSHASNFSV4N(nmp)) 4587 /* Has a Sequence Op done by nfscl_reqstart(). */ 4588 *opcntp = txdr_unsigned(3 + cnt); 4589 else 4590 *opcntp = txdr_unsigned(2 + cnt); 4591 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4592 *tl = txdr_unsigned(NFSV4OP_GETFH); 4593 nd->nd_flag |= ND_USEGSSNAME; 4594 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4595 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4596 if (error) 4597 return (error); 4598 if (nd->nd_repstat == 0) { 4599 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED); 4600 tl += (2 + 2 * cnt); 4601 if ((len = fxdr_unsigned(int, *tl)) <= 0 || 4602 len > NFSX_FHMAX) { 4603 nd->nd_repstat = NFSERR_BADXDR; 4604 } else { 4605 nd->nd_repstat = nfsrv_mtostr(nd, nmp->nm_fh, len); 4606 if (nd->nd_repstat == 0) 4607 nmp->nm_fhsize = len; 4608 } 4609 } 4610 error = nd->nd_repstat; 4611 nfsmout: 4612 m_freem(nd->nd_mrep); 4613 return (error); 4614 } 4615 4616 /* 4617 * This function performs the Delegreturn RPC. 4618 */ 4619 APPLESTATIC int 4620 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred, 4621 struct nfsmount *nmp, NFSPROC_T *p, int syscred) 4622 { 4623 u_int32_t *tl; 4624 struct nfsrv_descript nfsd; 4625 struct nfsrv_descript *nd = &nfsd; 4626 int error; 4627 4628 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh, 4629 dp->nfsdl_fhlen, NULL, NULL, 0, 0); 4630 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 4631 if (NFSHASNFSV4N(nmp)) 4632 *tl++ = 0; 4633 else 4634 *tl++ = dp->nfsdl_stateid.seqid; 4635 *tl++ = dp->nfsdl_stateid.other[0]; 4636 *tl++ = dp->nfsdl_stateid.other[1]; 4637 *tl = dp->nfsdl_stateid.other[2]; 4638 if (syscred) 4639 nd->nd_flag |= ND_USEGSSNAME; 4640 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4641 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4642 if (error) 4643 return (error); 4644 error = nd->nd_repstat; 4645 m_freem(nd->nd_mrep); 4646 return (error); 4647 } 4648 4649 /* 4650 * nfs getacl call. 4651 */ 4652 APPLESTATIC int 4653 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4654 struct acl *aclp, void *stuff) 4655 { 4656 struct nfsrv_descript nfsd, *nd = &nfsd; 4657 int error; 4658 nfsattrbit_t attrbits; 4659 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4660 4661 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 4662 return (EOPNOTSUPP); 4663 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp); 4664 NFSZERO_ATTRBIT(&attrbits); 4665 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 4666 (void) nfsrv_putattrbit(nd, &attrbits); 4667 error = nfscl_request(nd, vp, p, cred, stuff); 4668 if (error) 4669 return (error); 4670 if (!nd->nd_repstat) 4671 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL, 4672 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, p, cred); 4673 else 4674 error = nd->nd_repstat; 4675 m_freem(nd->nd_mrep); 4676 return (error); 4677 } 4678 4679 /* 4680 * nfs setacl call. 4681 */ 4682 APPLESTATIC int 4683 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4684 struct acl *aclp, void *stuff) 4685 { 4686 int error; 4687 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4688 4689 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 4690 return (EOPNOTSUPP); 4691 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL, stuff); 4692 return (error); 4693 } 4694 4695 /* 4696 * nfs setacl call. 4697 */ 4698 static int 4699 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 4700 struct acl *aclp, nfsv4stateid_t *stateidp, void *stuff) 4701 { 4702 struct nfsrv_descript nfsd, *nd = &nfsd; 4703 int error; 4704 nfsattrbit_t attrbits; 4705 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 4706 4707 if (!NFSHASNFSV4(nmp)) 4708 return (EOPNOTSUPP); 4709 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp); 4710 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 4711 NFSZERO_ATTRBIT(&attrbits); 4712 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 4713 (void) nfsv4_fillattr(nd, vnode_mount(vp), vp, aclp, NULL, NULL, 0, 4714 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL); 4715 error = nfscl_request(nd, vp, p, cred, stuff); 4716 if (error) 4717 return (error); 4718 /* Don't care about the pre/postop attributes */ 4719 m_freem(nd->nd_mrep); 4720 return (nd->nd_repstat); 4721 } 4722 4723 /* 4724 * Do the NFSv4.1 Exchange ID. 4725 */ 4726 int 4727 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp, 4728 struct nfssockreq *nrp, int minorvers, uint32_t exchflags, 4729 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p) 4730 { 4731 uint32_t *tl, v41flags; 4732 struct nfsrv_descript nfsd; 4733 struct nfsrv_descript *nd = &nfsd; 4734 struct nfsclds *dsp; 4735 struct timespec verstime; 4736 int error, len; 4737 4738 *dspp = NULL; 4739 if (minorvers == 0) 4740 minorvers = nmp->nm_minorvers; 4741 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 4742 NFS_VER4, minorvers); 4743 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 4744 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */ 4745 *tl = txdr_unsigned(clp->nfsc_rev); 4746 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen); 4747 4748 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED); 4749 *tl++ = txdr_unsigned(exchflags); 4750 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); 4751 4752 /* Set the implementation id4 */ 4753 *tl = txdr_unsigned(1); 4754 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org")); 4755 (void) nfsm_strtom(nd, version, strlen(version)); 4756 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME); 4757 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */ 4758 verstime.tv_nsec = 0; 4759 txdr_nfsv4time(&verstime, tl); 4760 nd->nd_flag |= ND_USEGSSNAME; 4761 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 4762 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4763 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error, 4764 (int)nd->nd_repstat); 4765 if (error != 0) 4766 return (error); 4767 if (nd->nd_repstat == 0) { 4768 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER); 4769 len = fxdr_unsigned(int, *(tl + 7)); 4770 if (len < 0 || len > NFSV4_OPAQUELIMIT) { 4771 error = NFSERR_BADXDR; 4772 goto nfsmout; 4773 } 4774 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS, 4775 M_WAITOK | M_ZERO); 4776 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew; 4777 dsp->nfsclds_servownlen = len; 4778 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++; 4779 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++; 4780 dsp->nfsclds_sess.nfsess_sequenceid = 4781 fxdr_unsigned(uint32_t, *tl++); 4782 v41flags = fxdr_unsigned(uint32_t, *tl); 4783 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 && 4784 NFSHASPNFSOPT(nmp)) { 4785 NFSCL_DEBUG(1, "set PNFS\n"); 4786 NFSLOCKMNT(nmp); 4787 nmp->nm_state |= NFSSTA_PNFS; 4788 NFSUNLOCKMNT(nmp); 4789 dsp->nfsclds_flags |= NFSCLDS_MDS; 4790 } 4791 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0) 4792 dsp->nfsclds_flags |= NFSCLDS_DS; 4793 if (minorvers == NFSV42_MINORVERSION) 4794 dsp->nfsclds_flags |= NFSCLDS_MINORV2; 4795 if (len > 0) 4796 nd->nd_repstat = nfsrv_mtostr(nd, 4797 dsp->nfsclds_serverown, len); 4798 if (nd->nd_repstat == 0) { 4799 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 4800 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 4801 NULL, MTX_DEF); 4802 nfscl_initsessionslots(&dsp->nfsclds_sess); 4803 *dspp = dsp; 4804 } else 4805 free(dsp, M_NFSCLDS); 4806 } 4807 error = nd->nd_repstat; 4808 nfsmout: 4809 m_freem(nd->nd_mrep); 4810 return (error); 4811 } 4812 4813 /* 4814 * Do the NFSv4.1 Create Session. 4815 */ 4816 int 4817 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep, 4818 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds, 4819 struct ucred *cred, NFSPROC_T *p) 4820 { 4821 uint32_t crflags, maxval, *tl; 4822 struct nfsrv_descript nfsd; 4823 struct nfsrv_descript *nd = &nfsd; 4824 int error, irdcnt, minorvers; 4825 4826 /* Make sure nm_rsize, nm_wsize is set. */ 4827 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0) 4828 nmp->nm_rsize = NFS_MAXBSIZE; 4829 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0) 4830 nmp->nm_wsize = NFS_MAXBSIZE; 4831 if (dsp == NULL) 4832 minorvers = nmp->nm_minorvers; 4833 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0) 4834 minorvers = NFSV42_MINORVERSION; 4835 else 4836 minorvers = NFSV41_MINORVERSION; 4837 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 4838 NFS_VER4, minorvers); 4839 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 4840 *tl++ = sep->nfsess_clientid.lval[0]; 4841 *tl++ = sep->nfsess_clientid.lval[1]; 4842 *tl++ = txdr_unsigned(sequenceid); 4843 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST); 4844 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0) 4845 crflags |= NFSV4CRSESS_CONNBACKCHAN; 4846 *tl = txdr_unsigned(crflags); 4847 4848 /* Fill in fore channel attributes. */ 4849 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4850 *tl++ = 0; /* Header pad size */ 4851 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >= 4852 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) { 4853 /* 4854 * NFSv4.2 Extended Attribute operations may want to do 4855 * requests/replies that are larger than nm_rsize/nm_wsize. 4856 */ 4857 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR); 4858 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR); 4859 } else { 4860 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR); 4861 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR); 4862 } 4863 *tl++ = txdr_unsigned(4096); /* Max response size cached */ 4864 *tl++ = txdr_unsigned(20); /* Max operations */ 4865 *tl++ = txdr_unsigned(64); /* Max slots */ 4866 *tl = 0; /* No rdma ird */ 4867 4868 /* Fill in back channel attributes. */ 4869 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4870 *tl++ = 0; /* Header pad size */ 4871 *tl++ = txdr_unsigned(10000); /* Max request size */ 4872 *tl++ = txdr_unsigned(10000); /* Max response size */ 4873 *tl++ = txdr_unsigned(4096); /* Max response size cached */ 4874 *tl++ = txdr_unsigned(4); /* Max operations */ 4875 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */ 4876 *tl = 0; /* No rdma ird */ 4877 4878 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED); 4879 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */ 4880 4881 /* Allow AUTH_SYS callbacks as uid, gid == 0. */ 4882 *tl++ = txdr_unsigned(1); /* Auth_sys only */ 4883 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */ 4884 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */ 4885 *tl++ = 0; /* Null machine name */ 4886 *tl++ = 0; /* Uid == 0 */ 4887 *tl++ = 0; /* Gid == 0 */ 4888 *tl = 0; /* No additional gids */ 4889 nd->nd_flag |= ND_USEGSSNAME; 4890 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG, 4891 NFS_VER4, NULL, 1, NULL, NULL); 4892 if (error != 0) 4893 return (error); 4894 if (nd->nd_repstat == 0) { 4895 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 4896 2 * NFSX_UNSIGNED); 4897 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID); 4898 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 4899 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++); 4900 crflags = fxdr_unsigned(uint32_t, *tl); 4901 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) { 4902 NFSLOCKMNT(nmp); 4903 nmp->nm_state |= NFSSTA_SESSPERSIST; 4904 NFSUNLOCKMNT(nmp); 4905 } 4906 4907 /* Get the fore channel slot count. */ 4908 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4909 tl++; /* Skip the header pad size. */ 4910 4911 /* Make sure nm_wsize is small enough. */ 4912 maxval = fxdr_unsigned(uint32_t, *tl++); 4913 while (maxval < nmp->nm_wsize + NFS_MAXXDR) { 4914 if (nmp->nm_wsize > 8096) 4915 nmp->nm_wsize /= 2; 4916 else 4917 break; 4918 } 4919 sep->nfsess_maxreq = maxval; 4920 4921 /* Make sure nm_rsize is small enough. */ 4922 maxval = fxdr_unsigned(uint32_t, *tl++); 4923 while (maxval < nmp->nm_rsize + NFS_MAXXDR) { 4924 if (nmp->nm_rsize > 8096) 4925 nmp->nm_rsize /= 2; 4926 else 4927 break; 4928 } 4929 sep->nfsess_maxresp = maxval; 4930 4931 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++); 4932 tl++; 4933 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++); 4934 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots); 4935 irdcnt = fxdr_unsigned(int, *tl); 4936 if (irdcnt > 0) 4937 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED); 4938 4939 /* and the back channel slot count. */ 4940 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED); 4941 tl += 5; 4942 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl); 4943 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots); 4944 } 4945 error = nd->nd_repstat; 4946 nfsmout: 4947 m_freem(nd->nd_mrep); 4948 return (error); 4949 } 4950 4951 /* 4952 * Do the NFSv4.1 Destroy Session. 4953 */ 4954 int 4955 nfsrpc_destroysession(struct nfsmount *nmp, struct nfsclclient *clp, 4956 struct ucred *cred, NFSPROC_T *p) 4957 { 4958 uint32_t *tl; 4959 struct nfsrv_descript nfsd; 4960 struct nfsrv_descript *nd = &nfsd; 4961 int error; 4962 struct nfsclsession *tsep; 4963 4964 nfscl_reqstart(nd, NFSPROC_DESTROYSESSION, nmp, NULL, 0, NULL, NULL, 0, 4965 0); 4966 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID); 4967 tsep = nfsmnt_mdssession(nmp); 4968 bcopy(tsep->nfsess_sessionid, tl, NFSX_V4SESSIONID); 4969 nd->nd_flag |= ND_USEGSSNAME; 4970 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4971 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4972 if (error != 0) 4973 return (error); 4974 error = nd->nd_repstat; 4975 m_freem(nd->nd_mrep); 4976 return (error); 4977 } 4978 4979 /* 4980 * Do the NFSv4.1 Destroy Client. 4981 */ 4982 int 4983 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp, 4984 struct ucred *cred, NFSPROC_T *p) 4985 { 4986 uint32_t *tl; 4987 struct nfsrv_descript nfsd; 4988 struct nfsrv_descript *nd = &nfsd; 4989 int error; 4990 struct nfsclsession *tsep; 4991 4992 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0, 4993 0); 4994 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 4995 tsep = nfsmnt_mdssession(nmp); 4996 *tl++ = tsep->nfsess_clientid.lval[0]; 4997 *tl = tsep->nfsess_clientid.lval[1]; 4998 nd->nd_flag |= ND_USEGSSNAME; 4999 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5000 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5001 if (error != 0) 5002 return (error); 5003 error = nd->nd_repstat; 5004 m_freem(nd->nd_mrep); 5005 return (error); 5006 } 5007 5008 /* 5009 * Do the NFSv4.1 LayoutGet. 5010 */ 5011 static int 5012 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode, 5013 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype, 5014 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep, 5015 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p, 5016 void *stuff) 5017 { 5018 struct nfsrv_descript nfsd, *nd = &nfsd; 5019 int error; 5020 5021 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0, 5022 0); 5023 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp, 5024 layouttype, layoutlen, 0); 5025 nd->nd_flag |= ND_USEGSSNAME; 5026 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5027 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5028 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat); 5029 if (error != 0) 5030 return (error); 5031 if (nd->nd_repstat == 0) 5032 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep, 5033 flhp); 5034 if (error == 0 && nd->nd_repstat != 0) 5035 error = nd->nd_repstat; 5036 m_freem(nd->nd_mrep); 5037 return (error); 5038 } 5039 5040 /* 5041 * Do the NFSv4.1 Get Device Info. 5042 */ 5043 int 5044 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype, 5045 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred, 5046 NFSPROC_T *p) 5047 { 5048 uint32_t cnt, *tl, vers, minorvers; 5049 struct nfsrv_descript nfsd; 5050 struct nfsrv_descript *nd = &nfsd; 5051 struct sockaddr_in sin, ssin; 5052 struct sockaddr_in6 sin6, ssin6; 5053 struct nfsclds *dsp = NULL, **dspp, **gotdspp; 5054 struct nfscldevinfo *ndi; 5055 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j; 5056 int stripecnt; 5057 uint8_t stripeindex; 5058 sa_family_t af, safilled; 5059 5060 ssin.sin_port = 0; /* To shut up compiler. */ 5061 ssin.sin_addr.s_addr = 0; /* ditto */ 5062 *ndip = NULL; 5063 ndi = NULL; 5064 gotdspp = NULL; 5065 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0, 5066 0); 5067 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED); 5068 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID); 5069 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 5070 *tl++ = txdr_unsigned(layouttype); 5071 *tl++ = txdr_unsigned(100000); 5072 if (notifybitsp != NULL && *notifybitsp != 0) { 5073 *tl = txdr_unsigned(1); /* One word of bits. */ 5074 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 5075 *tl = txdr_unsigned(*notifybitsp); 5076 } else 5077 *tl = txdr_unsigned(0); 5078 nd->nd_flag |= ND_USEGSSNAME; 5079 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5080 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5081 if (error != 0) 5082 return (error); 5083 if (nd->nd_repstat == 0) { 5084 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5085 if (layouttype != fxdr_unsigned(int, *tl)) 5086 printf("EEK! devinfo layout type not same!\n"); 5087 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) { 5088 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5089 stripecnt = fxdr_unsigned(int, *tl); 5090 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt); 5091 if (stripecnt < 1 || stripecnt > 4096) { 5092 printf("pNFS File layout devinfo stripecnt %d:" 5093 " out of range\n", stripecnt); 5094 error = NFSERR_BADXDR; 5095 goto nfsmout; 5096 } 5097 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * 5098 NFSX_UNSIGNED); 5099 addrcnt = fxdr_unsigned(int, *(tl + stripecnt)); 5100 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt); 5101 if (addrcnt < 1 || addrcnt > 128) { 5102 printf("NFS devinfo addrcnt %d: out of range\n", 5103 addrcnt); 5104 error = NFSERR_BADXDR; 5105 goto nfsmout; 5106 } 5107 5108 /* 5109 * Now we know how many stripe indices and addresses, so 5110 * we can allocate the structure the correct size. 5111 */ 5112 i = (stripecnt * sizeof(uint8_t)) / 5113 sizeof(struct nfsclds *) + 1; 5114 NFSCL_DEBUG(4, "stripeindices=%d\n", i); 5115 ndi = malloc(sizeof(*ndi) + (addrcnt + i) * 5116 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | 5117 M_ZERO); 5118 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, 5119 NFSX_V4DEVICEID); 5120 ndi->nfsdi_refcnt = 0; 5121 ndi->nfsdi_flags = NFSDI_FILELAYOUT; 5122 ndi->nfsdi_stripecnt = stripecnt; 5123 ndi->nfsdi_addrcnt = addrcnt; 5124 /* Fill in the stripe indices. */ 5125 for (i = 0; i < stripecnt; i++) { 5126 stripeindex = fxdr_unsigned(uint8_t, *tl++); 5127 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex); 5128 if (stripeindex >= addrcnt) { 5129 printf("pNFS File Layout devinfo" 5130 " stripeindex %d: too big\n", 5131 (int)stripeindex); 5132 error = NFSERR_BADXDR; 5133 goto nfsmout; 5134 } 5135 nfsfldi_setstripeindex(ndi, i, stripeindex); 5136 } 5137 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 5138 /* For Flex File, we only get one address list. */ 5139 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *), 5140 M_NFSDEVINFO, M_WAITOK | M_ZERO); 5141 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, 5142 NFSX_V4DEVICEID); 5143 ndi->nfsdi_refcnt = 0; 5144 ndi->nfsdi_flags = NFSDI_FLEXFILE; 5145 addrcnt = ndi->nfsdi_addrcnt = 1; 5146 } 5147 5148 /* Now, dissect the server address(es). */ 5149 safilled = AF_UNSPEC; 5150 for (i = 0; i < addrcnt; i++) { 5151 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5152 cnt = fxdr_unsigned(uint32_t, *tl); 5153 if (cnt == 0) { 5154 printf("NFS devinfo 0 len addrlist\n"); 5155 error = NFSERR_BADXDR; 5156 goto nfsmout; 5157 } 5158 dspp = nfsfldi_addr(ndi, i); 5159 safilled = AF_UNSPEC; 5160 for (j = 0; j < cnt; j++) { 5161 error = nfsv4_getipaddr(nd, &sin, &sin6, &af, 5162 &isudp); 5163 if (error != 0 && error != EPERM) { 5164 error = NFSERR_BADXDR; 5165 goto nfsmout; 5166 } 5167 if (error == 0 && isudp == 0) { 5168 /* 5169 * The priority is: 5170 * - Same address family. 5171 * Save the address and dspp, so that 5172 * the connection can be done after 5173 * parsing is complete. 5174 */ 5175 if (safilled == AF_UNSPEC || 5176 (af == nmp->nm_nam->sa_family && 5177 safilled != nmp->nm_nam->sa_family) 5178 ) { 5179 if (af == AF_INET) 5180 ssin = sin; 5181 else 5182 ssin6 = sin6; 5183 safilled = af; 5184 gotdspp = dspp; 5185 } 5186 } 5187 } 5188 } 5189 5190 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */ 5191 gotminor = NFSV41_MINORVERSION; 5192 /* For Flex File, we will take one of the versions to use. */ 5193 if (layouttype == NFSLAYOUT_FLEXFILE) { 5194 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5195 j = fxdr_unsigned(int, *tl); 5196 if (j < 1 || j > NFSDEV_MAXVERS) { 5197 printf("pNFS: too many versions\n"); 5198 error = NFSERR_BADXDR; 5199 goto nfsmout; 5200 } 5201 gotvers = 0; 5202 gotminor = 0; 5203 for (i = 0; i < j; i++) { 5204 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED); 5205 vers = fxdr_unsigned(uint32_t, *tl++); 5206 minorvers = fxdr_unsigned(uint32_t, *tl++); 5207 if (vers == NFS_VER3) 5208 minorvers = 0; 5209 if ((vers == NFS_VER4 && ((minorvers == 5210 NFSV41_MINORVERSION && gotminor == 0) || 5211 minorvers == NFSV42_MINORVERSION)) || 5212 (vers == NFS_VER3 && gotvers == 0)) { 5213 gotvers = vers; 5214 gotminor = minorvers; 5215 /* We'll take this one. */ 5216 ndi->nfsdi_versindex = i; 5217 ndi->nfsdi_vers = vers; 5218 ndi->nfsdi_minorvers = minorvers; 5219 ndi->nfsdi_rsize = fxdr_unsigned( 5220 uint32_t, *tl++); 5221 ndi->nfsdi_wsize = fxdr_unsigned( 5222 uint32_t, *tl++); 5223 if (*tl == newnfs_true) 5224 ndi->nfsdi_flags |= 5225 NFSDI_TIGHTCOUPLED; 5226 else 5227 ndi->nfsdi_flags &= 5228 ~NFSDI_TIGHTCOUPLED; 5229 } 5230 } 5231 if (gotvers == 0) { 5232 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n"); 5233 error = NFSERR_BADXDR; 5234 goto nfsmout; 5235 } 5236 } 5237 5238 /* And the notify bits. */ 5239 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5240 bitcnt = fxdr_unsigned(int, *tl); 5241 if (bitcnt > 0) { 5242 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5243 if (notifybitsp != NULL) 5244 *notifybitsp = 5245 fxdr_unsigned(uint32_t, *tl); 5246 } 5247 if (safilled != AF_UNSPEC) { 5248 KASSERT(ndi != NULL, ("ndi is NULL")); 5249 *ndip = ndi; 5250 } else 5251 error = EPERM; 5252 if (error == 0) { 5253 /* 5254 * Now we can do a TCP connection for the correct 5255 * NFS version and IP address. 5256 */ 5257 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled, 5258 gotvers, gotminor, &dsp, p); 5259 } 5260 if (error == 0) { 5261 KASSERT(gotdspp != NULL, ("gotdspp is NULL")); 5262 *gotdspp = dsp; 5263 } 5264 } 5265 if (nd->nd_repstat != 0 && error == 0) 5266 error = nd->nd_repstat; 5267 nfsmout: 5268 if (error != 0 && ndi != NULL) 5269 nfscl_freedevinfo(ndi); 5270 m_freem(nd->nd_mrep); 5271 return (error); 5272 } 5273 5274 /* 5275 * Do the NFSv4.1 LayoutCommit. 5276 */ 5277 int 5278 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim, 5279 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp, 5280 int layouttype, struct ucred *cred, NFSPROC_T *p, void *stuff) 5281 { 5282 uint32_t *tl; 5283 struct nfsrv_descript nfsd, *nd = &nfsd; 5284 int error; 5285 5286 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL, 5287 0, 0); 5288 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER + 5289 NFSX_STATEID); 5290 txdr_hyper(off, tl); 5291 tl += 2; 5292 txdr_hyper(len, tl); 5293 tl += 2; 5294 if (reclaim != 0) 5295 *tl++ = newnfs_true; 5296 else 5297 *tl++ = newnfs_false; 5298 *tl++ = txdr_unsigned(stateidp->seqid); 5299 *tl++ = stateidp->other[0]; 5300 *tl++ = stateidp->other[1]; 5301 *tl++ = stateidp->other[2]; 5302 *tl++ = newnfs_true; 5303 if (lastbyte < off) 5304 lastbyte = off; 5305 else if (lastbyte >= (off + len)) 5306 lastbyte = off + len - 1; 5307 txdr_hyper(lastbyte, tl); 5308 tl += 2; 5309 *tl++ = newnfs_false; 5310 *tl++ = txdr_unsigned(layouttype); 5311 /* All supported layouts are 0 length. */ 5312 *tl = txdr_unsigned(0); 5313 nd->nd_flag |= ND_USEGSSNAME; 5314 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5315 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5316 if (error != 0) 5317 return (error); 5318 error = nd->nd_repstat; 5319 m_freem(nd->nd_mrep); 5320 return (error); 5321 } 5322 5323 /* 5324 * Do the NFSv4.1 LayoutReturn. 5325 */ 5326 int 5327 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim, 5328 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset, 5329 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p, 5330 uint32_t stat, uint32_t op, char *devid) 5331 { 5332 uint32_t *tl; 5333 struct nfsrv_descript nfsd, *nd = &nfsd; 5334 uint64_t tu64; 5335 int error; 5336 5337 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL, 5338 0, 0); 5339 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 5340 if (reclaim != 0) 5341 *tl++ = newnfs_true; 5342 else 5343 *tl++ = newnfs_false; 5344 *tl++ = txdr_unsigned(layouttype); 5345 *tl++ = txdr_unsigned(iomode); 5346 *tl = txdr_unsigned(layoutreturn); 5347 if (layoutreturn == NFSLAYOUTRETURN_FILE) { 5348 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID + 5349 NFSX_UNSIGNED); 5350 txdr_hyper(offset, tl); 5351 tl += 2; 5352 txdr_hyper(len, tl); 5353 tl += 2; 5354 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid); 5355 *tl++ = txdr_unsigned(stateidp->seqid); 5356 *tl++ = stateidp->other[0]; 5357 *tl++ = stateidp->other[1]; 5358 *tl++ = stateidp->other[2]; 5359 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) 5360 *tl = txdr_unsigned(0); 5361 else if (layouttype == NFSLAYOUT_FLEXFILE) { 5362 if (stat != 0) { 5363 *tl = txdr_unsigned(2 * NFSX_HYPER + 5364 NFSX_STATEID + NFSX_V4DEVICEID + 5 * 5365 NFSX_UNSIGNED); 5366 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 5367 NFSX_STATEID + NFSX_V4DEVICEID + 5 * 5368 NFSX_UNSIGNED); 5369 *tl++ = txdr_unsigned(1); /* One error. */ 5370 tu64 = 0; /* Offset. */ 5371 txdr_hyper(tu64, tl); tl += 2; 5372 tu64 = UINT64_MAX; /* Length. */ 5373 txdr_hyper(tu64, tl); tl += 2; 5374 NFSBCOPY(stateidp, tl, NFSX_STATEID); 5375 tl += (NFSX_STATEID / NFSX_UNSIGNED); 5376 *tl++ = txdr_unsigned(1); /* One error. */ 5377 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); 5378 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 5379 *tl++ = txdr_unsigned(stat); 5380 *tl++ = txdr_unsigned(op); 5381 } else { 5382 *tl = txdr_unsigned(2 * NFSX_UNSIGNED); 5383 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5384 /* No ioerrs. */ 5385 *tl++ = 0; 5386 } 5387 *tl = 0; /* No stats yet. */ 5388 } 5389 } 5390 nd->nd_flag |= ND_USEGSSNAME; 5391 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5392 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5393 if (error != 0) 5394 return (error); 5395 if (nd->nd_repstat == 0) { 5396 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5397 if (*tl != 0) { 5398 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID); 5399 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++); 5400 stateidp->other[0] = *tl++; 5401 stateidp->other[1] = *tl++; 5402 stateidp->other[2] = *tl; 5403 } 5404 } else 5405 error = nd->nd_repstat; 5406 nfsmout: 5407 m_freem(nd->nd_mrep); 5408 return (error); 5409 } 5410 5411 /* 5412 * Acquire a layout and devinfo, if possible. The caller must have acquired 5413 * a reference count on the nfsclclient structure before calling this. 5414 * Return the layout in lypp with a reference count on it, if successful. 5415 */ 5416 static int 5417 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp, 5418 int iomode, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, uint64_t off, 5419 struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p) 5420 { 5421 struct nfscllayout *lyp; 5422 struct nfsclflayout *flp; 5423 struct nfsclflayouthead flh; 5424 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose; 5425 nfsv4stateid_t stateid; 5426 struct nfsclsession *tsep; 5427 5428 *lypp = NULL; 5429 if (NFSHASFLEXFILE(nmp)) 5430 layouttype = NFSLAYOUT_FLEXFILE; 5431 else 5432 layouttype = NFSLAYOUT_NFSV4_1_FILES; 5433 /* 5434 * If lyp is returned non-NULL, there will be a refcnt (shared lock) 5435 * on it, iff flp != NULL or a lock (exclusive lock) on it iff 5436 * flp == NULL. 5437 */ 5438 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len, 5439 off, &flp, &recalled); 5440 islocked = 0; 5441 if (lyp == NULL || flp == NULL) { 5442 if (recalled != 0) 5443 return (EIO); 5444 LIST_INIT(&flh); 5445 tsep = nfsmnt_mdssession(nmp); 5446 layoutlen = tsep->nfsess_maxcache - 5447 (NFSX_STATEID + 3 * NFSX_UNSIGNED); 5448 if (lyp == NULL) { 5449 stateid.seqid = 0; 5450 stateid.other[0] = stateidp->other[0]; 5451 stateid.other[1] = stateidp->other[1]; 5452 stateid.other[2] = stateidp->other[2]; 5453 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh, 5454 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX, 5455 (uint64_t)0, layouttype, layoutlen, &stateid, 5456 &retonclose, &flh, cred, p, NULL); 5457 } else { 5458 islocked = 1; 5459 stateid.seqid = lyp->nfsly_stateid.seqid; 5460 stateid.other[0] = lyp->nfsly_stateid.other[0]; 5461 stateid.other[1] = lyp->nfsly_stateid.other[1]; 5462 stateid.other[2] = lyp->nfsly_stateid.other[2]; 5463 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh, 5464 nfhp->nfh_len, iomode, off, UINT64_MAX, 5465 (uint64_t)0, layouttype, layoutlen, &stateid, 5466 &retonclose, &flh, cred, p, NULL); 5467 } 5468 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh, 5469 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp, 5470 &flh, layouttype, error, NULL, cred, p); 5471 if (error == 0) 5472 *lypp = lyp; 5473 else if (islocked != 0) 5474 nfscl_rellayout(lyp, 1); 5475 } else 5476 *lypp = lyp; 5477 return (error); 5478 } 5479 5480 /* 5481 * Do a TCP connection plus exchange id and create session. 5482 * If successful, a "struct nfsclds" is linked into the list for the 5483 * mount point and a pointer to it is returned. 5484 */ 5485 static int 5486 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin, 5487 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers, 5488 struct nfsclds **dspp, NFSPROC_T *p) 5489 { 5490 struct sockaddr_in *msad, *sad; 5491 struct sockaddr_in6 *msad6, *sad6; 5492 struct nfsclclient *clp; 5493 struct nfssockreq *nrp; 5494 struct nfsclds *dsp, *tdsp; 5495 int error, firsttry; 5496 enum nfsclds_state retv; 5497 uint32_t sequenceid = 0; 5498 5499 KASSERT(nmp->nm_sockreq.nr_cred != NULL, 5500 ("nfsrpc_fillsa: NULL nr_cred")); 5501 NFSLOCKCLSTATE(); 5502 clp = nmp->nm_clp; 5503 NFSUNLOCKCLSTATE(); 5504 if (clp == NULL) 5505 return (EPERM); 5506 if (af == AF_INET) { 5507 NFSLOCKMNT(nmp); 5508 /* 5509 * Check to see if we already have a session for this 5510 * address that is usable for a DS. 5511 * Note that the MDS's address is in a different place 5512 * than the sessions already acquired for DS's. 5513 */ 5514 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam; 5515 tdsp = TAILQ_FIRST(&nmp->nm_sess); 5516 while (tdsp != NULL) { 5517 if (msad != NULL && msad->sin_family == AF_INET && 5518 sin->sin_addr.s_addr == msad->sin_addr.s_addr && 5519 sin->sin_port == msad->sin_port && 5520 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 && 5521 tdsp->nfsclds_sess.nfsess_defunct == 0) { 5522 *dspp = tdsp; 5523 NFSUNLOCKMNT(nmp); 5524 NFSCL_DEBUG(4, "fnd same addr\n"); 5525 return (0); 5526 } 5527 tdsp = TAILQ_NEXT(tdsp, nfsclds_list); 5528 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL) 5529 msad = (struct sockaddr_in *) 5530 tdsp->nfsclds_sockp->nr_nam; 5531 else 5532 msad = NULL; 5533 } 5534 NFSUNLOCKMNT(nmp); 5535 5536 /* No IP address match, so look for new/trunked one. */ 5537 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO); 5538 sad->sin_len = sizeof(*sad); 5539 sad->sin_family = AF_INET; 5540 sad->sin_port = sin->sin_port; 5541 sad->sin_addr.s_addr = sin->sin_addr.s_addr; 5542 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO); 5543 nrp->nr_nam = (struct sockaddr *)sad; 5544 } else if (af == AF_INET6) { 5545 NFSLOCKMNT(nmp); 5546 /* 5547 * Check to see if we already have a session for this 5548 * address that is usable for a DS. 5549 * Note that the MDS's address is in a different place 5550 * than the sessions already acquired for DS's. 5551 */ 5552 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam; 5553 tdsp = TAILQ_FIRST(&nmp->nm_sess); 5554 while (tdsp != NULL) { 5555 if (msad6 != NULL && msad6->sin6_family == AF_INET6 && 5556 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 5557 &msad6->sin6_addr) && 5558 sin6->sin6_port == msad6->sin6_port && 5559 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 && 5560 tdsp->nfsclds_sess.nfsess_defunct == 0) { 5561 *dspp = tdsp; 5562 NFSUNLOCKMNT(nmp); 5563 return (0); 5564 } 5565 tdsp = TAILQ_NEXT(tdsp, nfsclds_list); 5566 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL) 5567 msad6 = (struct sockaddr_in6 *) 5568 tdsp->nfsclds_sockp->nr_nam; 5569 else 5570 msad6 = NULL; 5571 } 5572 NFSUNLOCKMNT(nmp); 5573 5574 /* No IP address match, so look for new/trunked one. */ 5575 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO); 5576 sad6->sin6_len = sizeof(*sad6); 5577 sad6->sin6_family = AF_INET6; 5578 sad6->sin6_port = sin6->sin6_port; 5579 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr, 5580 sizeof(struct in6_addr)); 5581 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, M_WAITOK | M_ZERO); 5582 nrp->nr_nam = (struct sockaddr *)sad6; 5583 } else 5584 return (EPERM); 5585 5586 nrp->nr_sotype = SOCK_STREAM; 5587 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF); 5588 nrp->nr_prog = NFS_PROG; 5589 nrp->nr_vers = vers; 5590 5591 /* 5592 * Use the credentials that were used for the mount, which are 5593 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc. 5594 * Ref. counting the credentials with crhold() is probably not 5595 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until 5596 * unmount, but I did it anyhow. 5597 */ 5598 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred); 5599 error = newnfs_connect(nmp, nrp, NULL, p, 0); 5600 NFSCL_DEBUG(3, "DS connect=%d\n", error); 5601 5602 dsp = NULL; 5603 /* Now, do the exchangeid and create session. */ 5604 if (error == 0) { 5605 if (vers == NFS_VER4) { 5606 firsttry = 0; 5607 do { 5608 error = nfsrpc_exchangeid(nmp, clp, nrp, 5609 minorvers, NFSV4EXCH_USEPNFSDS, &dsp, 5610 nrp->nr_cred, p); 5611 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error); 5612 if (error == NFSERR_MINORVERMISMATCH) 5613 minorvers = NFSV42_MINORVERSION; 5614 } while (error == NFSERR_MINORVERMISMATCH && 5615 firsttry++ == 0); 5616 if (error != 0) 5617 newnfs_disconnect(nrp); 5618 } else { 5619 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, 5620 M_WAITOK | M_ZERO); 5621 dsp->nfsclds_flags |= NFSCLDS_DS; 5622 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */ 5623 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 5624 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 5625 NULL, MTX_DEF); 5626 } 5627 } 5628 if (error == 0) { 5629 dsp->nfsclds_sockp = nrp; 5630 if (vers == NFS_VER4) { 5631 NFSLOCKMNT(nmp); 5632 retv = nfscl_getsameserver(nmp, dsp, &tdsp, 5633 &sequenceid); 5634 NFSCL_DEBUG(3, "getsame ret=%d\n", retv); 5635 if (retv == NFSDSP_USETHISSESSION && 5636 nfscl_dssameconn != 0) { 5637 NFSLOCKDS(tdsp); 5638 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN; 5639 NFSUNLOCKDS(tdsp); 5640 NFSUNLOCKMNT(nmp); 5641 /* 5642 * If there is already a session for this 5643 * server, use it. 5644 */ 5645 (void)newnfs_disconnect(nrp); 5646 nfscl_freenfsclds(dsp); 5647 *dspp = tdsp; 5648 return (0); 5649 } 5650 if (retv == NFSDSP_NOTFOUND) 5651 sequenceid = 5652 dsp->nfsclds_sess.nfsess_sequenceid; 5653 NFSUNLOCKMNT(nmp); 5654 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess, 5655 nrp, dsp, sequenceid, 0, nrp->nr_cred, p); 5656 NFSCL_DEBUG(3, "DS createsess=%d\n", error); 5657 } 5658 } else { 5659 NFSFREECRED(nrp->nr_cred); 5660 NFSFREEMUTEX(&nrp->nr_mtx); 5661 free(nrp->nr_nam, M_SONAME); 5662 free(nrp, M_NFSSOCKREQ); 5663 } 5664 if (error == 0) { 5665 NFSCL_DEBUG(3, "add DS session\n"); 5666 /* 5667 * Put it at the end of the list. That way the list 5668 * is ordered by when the entry was added. This matters 5669 * since the one done first is the one that should be 5670 * used for sequencid'ing any subsequent create sessions. 5671 */ 5672 NFSLOCKMNT(nmp); 5673 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list); 5674 NFSUNLOCKMNT(nmp); 5675 *dspp = dsp; 5676 } else if (dsp != NULL) { 5677 newnfs_disconnect(nrp); 5678 nfscl_freenfsclds(dsp); 5679 } 5680 return (error); 5681 } 5682 5683 /* 5684 * Do the NFSv4.1 Reclaim Complete. 5685 */ 5686 int 5687 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p) 5688 { 5689 uint32_t *tl; 5690 struct nfsrv_descript nfsd; 5691 struct nfsrv_descript *nd = &nfsd; 5692 int error; 5693 5694 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0, 5695 0); 5696 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 5697 *tl = newnfs_false; 5698 nd->nd_flag |= ND_USEGSSNAME; 5699 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5700 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5701 if (error != 0) 5702 return (error); 5703 error = nd->nd_repstat; 5704 m_freem(nd->nd_mrep); 5705 return (error); 5706 } 5707 5708 /* 5709 * Initialize the slot tables for a session. 5710 */ 5711 static void 5712 nfscl_initsessionslots(struct nfsclsession *sep) 5713 { 5714 int i; 5715 5716 for (i = 0; i < NFSV4_CBSLOTS; i++) { 5717 if (sep->nfsess_cbslots[i].nfssl_reply != NULL) 5718 m_freem(sep->nfsess_cbslots[i].nfssl_reply); 5719 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot)); 5720 } 5721 for (i = 0; i < 64; i++) 5722 sep->nfsess_slotseq[i] = 0; 5723 sep->nfsess_slots = 0; 5724 } 5725 5726 /* 5727 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS). 5728 */ 5729 int 5730 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 5731 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p) 5732 { 5733 struct nfsnode *np = VTONFS(vp); 5734 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 5735 struct nfscllayout *layp; 5736 struct nfscldevinfo *dip; 5737 struct nfsclflayout *rflp; 5738 struct mbuf *m; 5739 struct nfsclwritedsdorpc *drpc, *tdrpc; 5740 nfsv4stateid_t stateid; 5741 struct ucred *newcred; 5742 uint64_t lastbyte, len, off, oresid, xfer; 5743 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo; 5744 void *lckp; 5745 uint8_t *dev; 5746 void *iovbase = NULL; 5747 size_t iovlen = 0; 5748 off_t offs = 0; 5749 ssize_t resid = 0; 5750 5751 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 || 5752 (np->n_flag & NNOLAYOUT) != 0) 5753 return (EIO); 5754 /* Now, get a reference cnt on the clientid for this mount. */ 5755 if (nfscl_getref(nmp) == 0) 5756 return (EIO); 5757 5758 /* Find an appropriate stateid. */ 5759 newcred = NFSNEWCRED(cred); 5760 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 5761 rwaccess, 1, newcred, p, &stateid, &lckp); 5762 if (error != 0) { 5763 NFSFREECRED(newcred); 5764 nfscl_relref(nmp); 5765 return (error); 5766 } 5767 /* Search for a layout for this file. */ 5768 off = uiop->uio_offset; 5769 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh, 5770 np->n_fhp->nfh_len, off, &rflp, &recalled); 5771 if (layp == NULL || rflp == NULL) { 5772 if (recalled != 0) { 5773 NFSFREECRED(newcred); 5774 nfscl_relref(nmp); 5775 return (EIO); 5776 } 5777 if (layp != NULL) { 5778 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0); 5779 layp = NULL; 5780 } 5781 /* Try and get a Layout, if it is supported. */ 5782 if (rwaccess == NFSV4OPEN_ACCESSWRITE || 5783 (np->n_flag & NWRITEOPENED) != 0) 5784 iolaymode = NFSLAYOUTIOMODE_RW; 5785 else 5786 iolaymode = NFSLAYOUTIOMODE_READ; 5787 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode, 5788 NULL, &stateid, off, &layp, newcred, p); 5789 if (error != 0) { 5790 NFSLOCKNODE(np); 5791 np->n_flag |= NNOLAYOUT; 5792 NFSUNLOCKNODE(np); 5793 if (lckp != NULL) 5794 nfscl_lockderef(lckp); 5795 NFSFREECRED(newcred); 5796 if (layp != NULL) 5797 nfscl_rellayout(layp, 0); 5798 nfscl_relref(nmp); 5799 return (error); 5800 } 5801 } 5802 5803 /* 5804 * Loop around finding a layout that works for the first part of 5805 * this I/O operation, and then call the function that actually 5806 * does the RPC. 5807 */ 5808 eof = 0; 5809 len = (uint64_t)uiop->uio_resid; 5810 while (len > 0 && error == 0 && eof == 0) { 5811 off = uiop->uio_offset; 5812 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp); 5813 if (error == 0) { 5814 oresid = xfer = (uint64_t)uiop->uio_resid; 5815 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off)) 5816 xfer = rflp->nfsfl_end - rflp->nfsfl_off; 5817 /* 5818 * For Flex File layout with mirrored DSs, select one 5819 * of them at random for reads. For writes and commits, 5820 * do all mirrors. 5821 */ 5822 m = NULL; 5823 tdrpc = drpc = NULL; 5824 firstmirror = 0; 5825 mirrorcnt = 1; 5826 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 && 5827 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) { 5828 if (rwaccess == NFSV4OPEN_ACCESSREAD) { 5829 firstmirror = arc4random() % mirrorcnt; 5830 mirrorcnt = firstmirror + 1; 5831 } else { 5832 if (docommit == 0) { 5833 /* 5834 * Save values, so uiop can be 5835 * rolled back upon a write 5836 * error. 5837 */ 5838 offs = uiop->uio_offset; 5839 resid = uiop->uio_resid; 5840 iovbase = 5841 uiop->uio_iov->iov_base; 5842 iovlen = uiop->uio_iov->iov_len; 5843 m = nfsm_uiombuflist(uiop, len, 5844 NULL, NULL); 5845 } 5846 tdrpc = drpc = malloc(sizeof(*drpc) * 5847 (mirrorcnt - 1), M_TEMP, M_WAITOK | 5848 M_ZERO); 5849 } 5850 } 5851 for (i = firstmirror; i < mirrorcnt && error == 0; i++){ 5852 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) { 5853 dev = rflp->nfsfl_ffm[i].dev; 5854 dip = nfscl_getdevinfo(nmp->nm_clp, dev, 5855 rflp->nfsfl_ffm[i].devp); 5856 } else { 5857 dev = rflp->nfsfl_dev; 5858 dip = nfscl_getdevinfo(nmp->nm_clp, dev, 5859 rflp->nfsfl_devp); 5860 } 5861 if (dip != NULL) { 5862 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE) 5863 != 0) 5864 error = nfscl_dofflayoutio(vp, 5865 uiop, iomode, must_commit, 5866 &eof, &stateid, rwaccess, 5867 dip, layp, rflp, off, xfer, 5868 i, docommit, m, tdrpc, 5869 newcred, p); 5870 else 5871 error = nfscl_doflayoutio(vp, 5872 uiop, iomode, must_commit, 5873 &eof, &stateid, rwaccess, 5874 dip, layp, rflp, off, xfer, 5875 docommit, newcred, p); 5876 nfscl_reldevinfo(dip); 5877 } else 5878 error = EIO; 5879 tdrpc++; 5880 } 5881 if (m != NULL) 5882 m_freem(m); 5883 tdrpc = drpc; 5884 timo = hz / 50; /* Wait for 20msec. */ 5885 if (timo < 1) 5886 timo = 1; 5887 for (i = firstmirror; i < mirrorcnt - 1 && 5888 tdrpc != NULL; i++, tdrpc++) { 5889 /* 5890 * For the unused drpc entries, both inprog and 5891 * err == 0, so this loop won't break. 5892 */ 5893 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5894 tsleep(&tdrpc->tsk, PVFS, "clrpcio", 5895 timo); 5896 if (error == 0 && tdrpc->err != 0) 5897 error = tdrpc->err; 5898 } 5899 free(drpc, M_TEMP); 5900 if (error == 0) { 5901 if (mirrorcnt > 1 && rwaccess == 5902 NFSV4OPEN_ACCESSWRITE && docommit == 0) { 5903 NFSLOCKCLSTATE(); 5904 layp->nfsly_flags |= NFSLY_WRITTEN; 5905 NFSUNLOCKCLSTATE(); 5906 } 5907 lastbyte = off + xfer - 1; 5908 NFSLOCKCLSTATE(); 5909 if (lastbyte > layp->nfsly_lastbyte) 5910 layp->nfsly_lastbyte = lastbyte; 5911 NFSUNLOCKCLSTATE(); 5912 } else if (error == NFSERR_OPENMODE && 5913 rwaccess == NFSV4OPEN_ACCESSREAD) { 5914 NFSLOCKMNT(nmp); 5915 nmp->nm_state |= NFSSTA_OPENMODE; 5916 NFSUNLOCKMNT(nmp); 5917 } else 5918 error = EIO; 5919 if (error == 0) 5920 len -= (oresid - (uint64_t)uiop->uio_resid); 5921 else if (mirrorcnt > 1 && rwaccess == 5922 NFSV4OPEN_ACCESSWRITE && docommit == 0) { 5923 /* 5924 * In case the rpc gets retried, roll the 5925 * uio fields changed by nfsm_uiombuflist() 5926 * back. 5927 */ 5928 uiop->uio_offset = offs; 5929 uiop->uio_resid = resid; 5930 uiop->uio_iov->iov_base = iovbase; 5931 uiop->uio_iov->iov_len = iovlen; 5932 } 5933 } 5934 } 5935 if (lckp != NULL) 5936 nfscl_lockderef(lckp); 5937 NFSFREECRED(newcred); 5938 nfscl_rellayout(layp, 0); 5939 nfscl_relref(nmp); 5940 return (error); 5941 } 5942 5943 /* 5944 * Make a copy of the mbuf chain and add an mbuf for null padding, as required. 5945 */ 5946 static struct mbuf * 5947 nfsm_copym(struct mbuf *m, int off, int xfer) 5948 { 5949 struct mbuf *m2, *m3, *m4; 5950 uint32_t *tl; 5951 int rem; 5952 5953 m2 = m_copym(m, off, xfer, M_WAITOK); 5954 rem = NFSM_RNDUP(xfer) - xfer; 5955 if (rem > 0) { 5956 /* 5957 * The zero padding to a multiple of 4 bytes is required by 5958 * the XDR. So that the mbufs copied by reference aren't 5959 * modified, add an mbuf with the zero'd bytes to the list. 5960 * rem will be a maximum of 3, so one zero'd uint32_t is 5961 * sufficient. 5962 */ 5963 m3 = m2; 5964 while (m3->m_next != NULL) 5965 m3 = m3->m_next; 5966 NFSMGET(m4); 5967 tl = mtod(m4, uint32_t *); 5968 *tl = 0; 5969 m4->m_len = rem; 5970 m3->m_next = m4; 5971 } 5972 return (m2); 5973 } 5974 5975 /* 5976 * Find a file layout that will handle the first bytes of the requested 5977 * range and return the information from it needed to the I/O operation. 5978 */ 5979 int 5980 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess, 5981 struct nfsclflayout **retflpp) 5982 { 5983 struct nfsclflayout *flp, *nflp, *rflp; 5984 uint32_t rw; 5985 5986 rflp = NULL; 5987 rw = rwaccess; 5988 /* For reading, do the Read list first and then the Write list. */ 5989 do { 5990 if (rw == NFSV4OPEN_ACCESSREAD) 5991 flp = LIST_FIRST(&lyp->nfsly_flayread); 5992 else 5993 flp = LIST_FIRST(&lyp->nfsly_flayrw); 5994 while (flp != NULL) { 5995 nflp = LIST_NEXT(flp, nfsfl_list); 5996 if (flp->nfsfl_off > off) 5997 break; 5998 if (flp->nfsfl_end > off && 5999 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end)) 6000 rflp = flp; 6001 flp = nflp; 6002 } 6003 if (rw == NFSV4OPEN_ACCESSREAD) 6004 rw = NFSV4OPEN_ACCESSWRITE; 6005 else 6006 rw = 0; 6007 } while (rw != 0); 6008 if (rflp != NULL) { 6009 /* This one covers the most bytes starting at off. */ 6010 *retflpp = rflp; 6011 return (0); 6012 } 6013 return (EIO); 6014 } 6015 6016 /* 6017 * Do I/O using an NFSv4.1 or NFSv4.2 file layout. 6018 */ 6019 static int 6020 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6021 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp, 6022 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off, 6023 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p) 6024 { 6025 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer; 6026 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers; 6027 struct nfsnode *np; 6028 struct nfsfh *fhp; 6029 struct nfsclds **dspp; 6030 6031 np = VTONFS(vp); 6032 rel_off = off - flp->nfsfl_patoff; 6033 stripe_unit_size = (flp->nfsfl_util >> 6) & 0x3ffffff; 6034 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) % 6035 dp->nfsdi_stripecnt; 6036 transfer = stripe_unit_size - (rel_off % stripe_unit_size); 6037 error = 0; 6038 6039 /* Loop around, doing I/O for each stripe unit. */ 6040 while (len > 0 && error == 0) { 6041 stripe_index = nfsfldi_stripeindex(dp, stripe_pos); 6042 dspp = nfsfldi_addr(dp, stripe_index); 6043 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0) 6044 minorvers = NFSV42_MINORVERSION; 6045 else 6046 minorvers = NFSV41_MINORVERSION; 6047 if (len > transfer && docommit == 0) 6048 xfer = transfer; 6049 else 6050 xfer = len; 6051 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) { 6052 /* Dense layout. */ 6053 if (stripe_pos >= flp->nfsfl_fhcnt) 6054 return (EIO); 6055 fhp = flp->nfsfl_fh[stripe_pos]; 6056 io_off = (rel_off / (stripe_unit_size * 6057 dp->nfsdi_stripecnt)) * stripe_unit_size + 6058 rel_off % stripe_unit_size; 6059 } else { 6060 /* Sparse layout. */ 6061 if (flp->nfsfl_fhcnt > 1) { 6062 if (stripe_index >= flp->nfsfl_fhcnt) 6063 return (EIO); 6064 fhp = flp->nfsfl_fh[stripe_index]; 6065 } else if (flp->nfsfl_fhcnt == 1) 6066 fhp = flp->nfsfl_fh[0]; 6067 else 6068 fhp = np->n_fhp; 6069 io_off = off; 6070 } 6071 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) { 6072 commit_thru_mds = 1; 6073 if (docommit != 0) 6074 error = EIO; 6075 } else { 6076 commit_thru_mds = 0; 6077 NFSLOCKNODE(np); 6078 np->n_flag |= NDSCOMMIT; 6079 NFSUNLOCKNODE(np); 6080 } 6081 if (docommit != 0) { 6082 if (error == 0) 6083 error = nfsrpc_commitds(vp, io_off, xfer, 6084 *dspp, fhp, NFS_VER4, minorvers, cred, p); 6085 if (error == 0) { 6086 /* 6087 * Set both eof and uio_resid = 0 to end any 6088 * loops. 6089 */ 6090 *eofp = 1; 6091 uiop->uio_resid = 0; 6092 } else { 6093 NFSLOCKNODE(np); 6094 np->n_flag &= ~NDSCOMMIT; 6095 NFSUNLOCKNODE(np); 6096 } 6097 } else if (rwflag == NFSV4OPEN_ACCESSREAD) 6098 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp, 6099 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p); 6100 else { 6101 error = nfsrpc_writeds(vp, uiop, iomode, must_commit, 6102 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds, 6103 0, NFS_VER4, minorvers, cred, p); 6104 if (error == 0) { 6105 NFSLOCKCLSTATE(); 6106 lyp->nfsly_flags |= NFSLY_WRITTEN; 6107 NFSUNLOCKCLSTATE(); 6108 } 6109 } 6110 if (error == 0) { 6111 transfer = stripe_unit_size; 6112 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt; 6113 len -= xfer; 6114 off += xfer; 6115 } 6116 } 6117 return (error); 6118 } 6119 6120 /* 6121 * Do I/O using an NFSv4.1 flex file layout. 6122 */ 6123 static int 6124 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6125 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp, 6126 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off, 6127 uint64_t len, int mirror, int docommit, struct mbuf *mp, 6128 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6129 { 6130 uint64_t transfer, xfer; 6131 int error, rel_off; 6132 struct nfsnode *np; 6133 struct nfsfh *fhp; 6134 struct nfsclds **dspp; 6135 struct ucred *tcred; 6136 struct mbuf *m; 6137 6138 np = VTONFS(vp); 6139 error = 0; 6140 rel_off = 0; 6141 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off, 6142 (uintmax_t)len); 6143 /* Loop around, doing I/O for each stripe unit. */ 6144 while (len > 0 && error == 0) { 6145 dspp = nfsfldi_addr(dp, 0); 6146 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex]; 6147 stateidp = &flp->nfsfl_ffm[mirror].st; 6148 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n", 6149 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid); 6150 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) { 6151 tcred = NFSNEWCRED(cred); 6152 tcred->cr_uid = flp->nfsfl_ffm[mirror].user; 6153 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group; 6154 tcred->cr_ngroups = 1; 6155 } else 6156 tcred = cred; 6157 if (rwflag == NFSV4OPEN_ACCESSREAD) 6158 transfer = dp->nfsdi_rsize; 6159 else 6160 transfer = dp->nfsdi_wsize; 6161 NFSLOCKNODE(np); 6162 np->n_flag |= NDSCOMMIT; 6163 NFSUNLOCKNODE(np); 6164 if (len > transfer && docommit == 0) 6165 xfer = transfer; 6166 else 6167 xfer = len; 6168 if (docommit != 0) { 6169 if (error == 0) { 6170 /* 6171 * Do last mirrored DS commit with this thread. 6172 */ 6173 if (mirror < flp->nfsfl_mirrorcnt - 1) 6174 error = nfsio_commitds(vp, off, xfer, 6175 *dspp, fhp, dp->nfsdi_vers, 6176 dp->nfsdi_minorvers, drpc, tcred, 6177 p); 6178 else 6179 error = nfsrpc_commitds(vp, off, xfer, 6180 *dspp, fhp, dp->nfsdi_vers, 6181 dp->nfsdi_minorvers, tcred, p); 6182 NFSCL_DEBUG(4, "commitds=%d\n", error); 6183 if (error != 0 && error != EACCES && error != 6184 ESTALE) { 6185 NFSCL_DEBUG(4, 6186 "DS layreterr for commit\n"); 6187 nfscl_dserr(NFSV4OP_COMMIT, error, dp, 6188 lyp, *dspp); 6189 } 6190 } 6191 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error); 6192 if (error == 0) { 6193 /* 6194 * Set both eof and uio_resid = 0 to end any 6195 * loops. 6196 */ 6197 *eofp = 1; 6198 uiop->uio_resid = 0; 6199 } else { 6200 NFSLOCKNODE(np); 6201 np->n_flag &= ~NDSCOMMIT; 6202 NFSUNLOCKNODE(np); 6203 } 6204 } else if (rwflag == NFSV4OPEN_ACCESSREAD) { 6205 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp, 6206 off, xfer, fhp, 1, dp->nfsdi_vers, 6207 dp->nfsdi_minorvers, tcred, p); 6208 NFSCL_DEBUG(4, "readds=%d\n", error); 6209 if (error != 0 && error != EACCES && error != ESTALE) { 6210 NFSCL_DEBUG(4, "DS layreterr for read\n"); 6211 nfscl_dserr(NFSV4OP_READ, error, dp, lyp, 6212 *dspp); 6213 } 6214 } else { 6215 if (flp->nfsfl_mirrorcnt == 1) { 6216 error = nfsrpc_writeds(vp, uiop, iomode, 6217 must_commit, stateidp, *dspp, off, xfer, 6218 fhp, 0, 1, dp->nfsdi_vers, 6219 dp->nfsdi_minorvers, tcred, p); 6220 if (error == 0) { 6221 NFSLOCKCLSTATE(); 6222 lyp->nfsly_flags |= NFSLY_WRITTEN; 6223 NFSUNLOCKCLSTATE(); 6224 } 6225 } else { 6226 m = nfsm_copym(mp, rel_off, xfer); 6227 NFSCL_DEBUG(4, "mcopy reloff=%d xfer=%jd\n", 6228 rel_off, (uintmax_t)xfer); 6229 /* 6230 * Do last write to a mirrored DS with this 6231 * thread. 6232 */ 6233 if (mirror < flp->nfsfl_mirrorcnt - 1) 6234 error = nfsio_writedsmir(vp, iomode, 6235 must_commit, stateidp, *dspp, off, 6236 xfer, fhp, m, dp->nfsdi_vers, 6237 dp->nfsdi_minorvers, drpc, tcred, 6238 p); 6239 else 6240 error = nfsrpc_writedsmir(vp, iomode, 6241 must_commit, stateidp, *dspp, off, 6242 xfer, fhp, m, dp->nfsdi_vers, 6243 dp->nfsdi_minorvers, tcred, p); 6244 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error); 6245 if (error != 0 && error != EACCES && error != 6246 ESTALE) { 6247 NFSCL_DEBUG(4, 6248 "DS layreterr for write\n"); 6249 nfscl_dserr(NFSV4OP_WRITE, error, dp, 6250 lyp, *dspp); 6251 } 6252 } 6253 } 6254 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error); 6255 if (error == 0) { 6256 len -= xfer; 6257 off += xfer; 6258 rel_off += xfer; 6259 } 6260 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) 6261 NFSFREECRED(tcred); 6262 } 6263 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error); 6264 return (error); 6265 } 6266 6267 /* 6268 * The actual read RPC done to a DS. 6269 */ 6270 static int 6271 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp, 6272 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex, 6273 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p) 6274 { 6275 uint32_t *tl; 6276 int attrflag, error, retlen; 6277 struct nfsrv_descript nfsd; 6278 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6279 struct nfsrv_descript *nd = &nfsd; 6280 struct nfssockreq *nrp; 6281 struct nfsvattr na; 6282 6283 nd->nd_mrep = NULL; 6284 if (vers == 0 || vers == NFS_VER4) { 6285 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, 6286 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6287 vers = NFS_VER4; 6288 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers); 6289 if (flex != 0) 6290 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 6291 else 6292 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO); 6293 } else { 6294 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh, 6295 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6296 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n"); 6297 } 6298 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3); 6299 txdr_hyper(io_off, tl); 6300 *(tl + 2) = txdr_unsigned(len); 6301 nrp = dsp->nfsclds_sockp; 6302 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp); 6303 if (nrp == NULL) 6304 /* If NULL, use the MDS socket. */ 6305 nrp = &nmp->nm_sockreq; 6306 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6307 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6308 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat, 6309 error); 6310 if (error != 0) 6311 return (error); 6312 if (vers == NFS_VER3) { 6313 error = nfscl_postop_attr(nd, &na, &attrflag, NULL); 6314 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error); 6315 if (error != 0) 6316 goto nfsmout; 6317 } 6318 if (nd->nd_repstat != 0) { 6319 error = nd->nd_repstat; 6320 goto nfsmout; 6321 } 6322 if (vers == NFS_VER3) { 6323 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6324 *eofp = fxdr_unsigned(int, *(tl + 1)); 6325 } else { 6326 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 6327 *eofp = fxdr_unsigned(int, *tl); 6328 } 6329 NFSM_STRSIZ(retlen, len); 6330 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp); 6331 error = nfsm_mbufuio(nd, uiop, retlen); 6332 nfsmout: 6333 if (nd->nd_mrep != NULL) 6334 m_freem(nd->nd_mrep); 6335 return (error); 6336 } 6337 6338 /* 6339 * The actual write RPC done to a DS. 6340 */ 6341 static int 6342 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6343 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len, 6344 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers, 6345 struct ucred *cred, NFSPROC_T *p) 6346 { 6347 uint32_t *tl; 6348 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6349 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC; 6350 int32_t backup; 6351 struct nfsrv_descript nfsd; 6352 struct nfsrv_descript *nd = &nfsd; 6353 struct nfssockreq *nrp; 6354 struct nfsvattr na; 6355 6356 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1")); 6357 nd->nd_mrep = NULL; 6358 if (vers == 0 || vers == NFS_VER4) { 6359 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, 6360 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6361 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers); 6362 vers = NFS_VER4; 6363 if (flex != 0) 6364 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 6365 else 6366 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO); 6367 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 6368 } else { 6369 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh, 6370 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6371 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n"); 6372 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED); 6373 } 6374 txdr_hyper(io_off, tl); 6375 tl += 2; 6376 if (vers == NFS_VER3) 6377 *tl++ = txdr_unsigned(len); 6378 *tl++ = txdr_unsigned(*iomode); 6379 *tl = txdr_unsigned(len); 6380 nfsm_uiombuf(nd, uiop, len); 6381 nrp = dsp->nfsclds_sockp; 6382 if (nrp == NULL) 6383 /* If NULL, use the MDS socket. */ 6384 nrp = &nmp->nm_sockreq; 6385 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6386 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6387 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error, 6388 nd->nd_repstat); 6389 if (error != 0) 6390 return (error); 6391 if (nd->nd_repstat != 0) { 6392 /* 6393 * In case the rpc gets retried, roll 6394 * the uio fileds changed by nfsm_uiombuf() 6395 * back. 6396 */ 6397 uiop->uio_offset -= len; 6398 uio_uio_resid_add(uiop, len); 6399 uio_iov_base_add(uiop, -len); 6400 uio_iov_len_add(uiop, len); 6401 error = nd->nd_repstat; 6402 } else { 6403 if (vers == NFS_VER3) { 6404 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 6405 NULL); 6406 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error); 6407 if (error != 0) 6408 goto nfsmout; 6409 } 6410 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 6411 rlen = fxdr_unsigned(int, *tl++); 6412 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen); 6413 if (rlen == 0) { 6414 error = NFSERR_IO; 6415 goto nfsmout; 6416 } else if (rlen < len) { 6417 backup = len - rlen; 6418 uio_iov_base_add(uiop, -(backup)); 6419 uio_iov_len_add(uiop, backup); 6420 uiop->uio_offset -= backup; 6421 uio_uio_resid_add(uiop, backup); 6422 len = rlen; 6423 } 6424 commit = fxdr_unsigned(int, *tl++); 6425 6426 /* 6427 * Return the lowest commitment level 6428 * obtained by any of the RPCs. 6429 */ 6430 if (committed == NFSWRITE_FILESYNC) 6431 committed = commit; 6432 else if (committed == NFSWRITE_DATASYNC && 6433 commit == NFSWRITE_UNSTABLE) 6434 committed = commit; 6435 if (commit_thru_mds != 0) { 6436 NFSLOCKMNT(nmp); 6437 if (!NFSHASWRITEVERF(nmp)) { 6438 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 6439 NFSSETWRITEVERF(nmp); 6440 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) { 6441 *must_commit = 1; 6442 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 6443 } 6444 NFSUNLOCKMNT(nmp); 6445 } else { 6446 NFSLOCKDS(dsp); 6447 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) { 6448 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6449 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF; 6450 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 6451 *must_commit = 1; 6452 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6453 } 6454 NFSUNLOCKDS(dsp); 6455 } 6456 } 6457 nfsmout: 6458 if (nd->nd_mrep != NULL) 6459 m_freem(nd->nd_mrep); 6460 *iomode = committed; 6461 if (nd->nd_repstat != 0 && error == 0) 6462 error = nd->nd_repstat; 6463 return (error); 6464 } 6465 6466 /* 6467 * The actual write RPC done to a DS. 6468 * This variant is called from a separate kernel process for mirrors. 6469 * Any short write is considered an IO error. 6470 */ 6471 static int 6472 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit, 6473 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len, 6474 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers, 6475 struct ucred *cred, NFSPROC_T *p) 6476 { 6477 uint32_t *tl; 6478 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6479 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen; 6480 struct nfsrv_descript nfsd; 6481 struct nfsrv_descript *nd = &nfsd; 6482 struct nfssockreq *nrp; 6483 struct nfsvattr na; 6484 6485 nd->nd_mrep = NULL; 6486 if (vers == 0 || vers == NFS_VER4) { 6487 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, 6488 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6489 vers = NFS_VER4; 6490 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n", 6491 minorvers); 6492 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 6493 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 6494 } else { 6495 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh, 6496 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6497 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n"); 6498 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED); 6499 } 6500 txdr_hyper(io_off, tl); 6501 tl += 2; 6502 if (vers == NFS_VER3) 6503 *tl++ = txdr_unsigned(len); 6504 *tl++ = txdr_unsigned(*iomode); 6505 *tl = txdr_unsigned(len); 6506 if (len > 0) { 6507 /* Put data in mbuf chain. */ 6508 nd->nd_mb->m_next = m; 6509 /* Set nd_mb and nd_bpos to end of data. */ 6510 while (m->m_next != NULL) 6511 m = m->m_next; 6512 nd->nd_mb = m; 6513 nd->nd_bpos = mtod(m, char *) + m->m_len; 6514 NFSCL_DEBUG(4, "nfsrpc_writedsmir: lastmb len=%d\n", m->m_len); 6515 } 6516 nrp = dsp->nfsclds_sockp; 6517 if (nrp == NULL) 6518 /* If NULL, use the MDS socket. */ 6519 nrp = &nmp->nm_sockreq; 6520 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6521 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6522 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error, 6523 nd->nd_repstat); 6524 if (error != 0) 6525 return (error); 6526 if (nd->nd_repstat != 0) 6527 error = nd->nd_repstat; 6528 else { 6529 if (vers == NFS_VER3) { 6530 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 6531 NULL); 6532 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n", 6533 error); 6534 if (error != 0) 6535 goto nfsmout; 6536 } 6537 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 6538 rlen = fxdr_unsigned(int, *tl++); 6539 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len, 6540 rlen); 6541 if (rlen != len) { 6542 error = NFSERR_IO; 6543 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", 6544 len, rlen); 6545 goto nfsmout; 6546 } 6547 commit = fxdr_unsigned(int, *tl++); 6548 6549 /* 6550 * Return the lowest commitment level 6551 * obtained by any of the RPCs. 6552 */ 6553 if (committed == NFSWRITE_FILESYNC) 6554 committed = commit; 6555 else if (committed == NFSWRITE_DATASYNC && 6556 commit == NFSWRITE_UNSTABLE) 6557 committed = commit; 6558 NFSLOCKDS(dsp); 6559 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) { 6560 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6561 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF; 6562 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 6563 *must_commit = 1; 6564 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6565 } 6566 NFSUNLOCKDS(dsp); 6567 } 6568 nfsmout: 6569 if (nd->nd_mrep != NULL) 6570 m_freem(nd->nd_mrep); 6571 *iomode = committed; 6572 if (nd->nd_repstat != 0 && error == 0) 6573 error = nd->nd_repstat; 6574 return (error); 6575 } 6576 6577 /* 6578 * Start up the thread that will execute nfsrpc_writedsmir(). 6579 */ 6580 static void 6581 start_writedsmir(void *arg, int pending) 6582 { 6583 struct nfsclwritedsdorpc *drpc; 6584 6585 drpc = (struct nfsclwritedsdorpc *)arg; 6586 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode, 6587 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len, 6588 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred, 6589 drpc->p); 6590 drpc->done = 1; 6591 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err); 6592 } 6593 6594 /* 6595 * Set up the write DS mirror call for the pNFS I/O thread. 6596 */ 6597 static int 6598 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit, 6599 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len, 6600 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers, 6601 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6602 { 6603 int error, ret; 6604 6605 error = 0; 6606 drpc->done = 0; 6607 drpc->vp = vp; 6608 drpc->iomode = *iomode; 6609 drpc->must_commit = *must_commit; 6610 drpc->stateidp = stateidp; 6611 drpc->dsp = dsp; 6612 drpc->off = off; 6613 drpc->len = len; 6614 drpc->fhp = fhp; 6615 drpc->m = m; 6616 drpc->vers = vers; 6617 drpc->minorvers = minorvers; 6618 drpc->cred = cred; 6619 drpc->p = p; 6620 drpc->inprog = 0; 6621 ret = EIO; 6622 if (nfs_pnfsiothreads != 0) { 6623 ret = nfs_pnfsio(start_writedsmir, drpc); 6624 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret); 6625 } 6626 if (ret != 0) 6627 error = nfsrpc_writedsmir(vp, iomode, must_commit, stateidp, 6628 dsp, off, len, fhp, m, vers, minorvers, cred, p); 6629 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error); 6630 return (error); 6631 } 6632 6633 /* 6634 * Free up the nfsclds structure. 6635 */ 6636 void 6637 nfscl_freenfsclds(struct nfsclds *dsp) 6638 { 6639 int i; 6640 6641 if (dsp == NULL) 6642 return; 6643 if (dsp->nfsclds_sockp != NULL) { 6644 NFSFREECRED(dsp->nfsclds_sockp->nr_cred); 6645 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx); 6646 free(dsp->nfsclds_sockp->nr_nam, M_SONAME); 6647 free(dsp->nfsclds_sockp, M_NFSSOCKREQ); 6648 } 6649 NFSFREEMUTEX(&dsp->nfsclds_mtx); 6650 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx); 6651 for (i = 0; i < NFSV4_CBSLOTS; i++) { 6652 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL) 6653 m_freem( 6654 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply); 6655 } 6656 free(dsp, M_NFSCLDS); 6657 } 6658 6659 static enum nfsclds_state 6660 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp, 6661 struct nfsclds **retdspp, uint32_t *sequencep) 6662 { 6663 struct nfsclds *dsp; 6664 int fndseq; 6665 6666 /* 6667 * Search the list of nfsclds structures for one with the same 6668 * server. 6669 */ 6670 fndseq = 0; 6671 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) { 6672 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen && 6673 dsp->nfsclds_servownlen != 0 && 6674 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown, 6675 dsp->nfsclds_servownlen) && 6676 dsp->nfsclds_sess.nfsess_defunct == 0) { 6677 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n", 6678 TAILQ_FIRST(&nmp->nm_sess), dsp, 6679 dsp->nfsclds_flags); 6680 if (fndseq == 0) { 6681 /* Get sequenceid# from first entry. */ 6682 *sequencep = 6683 dsp->nfsclds_sess.nfsess_sequenceid; 6684 fndseq = 1; 6685 } 6686 /* Server major id matches. */ 6687 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) { 6688 *retdspp = dsp; 6689 return (NFSDSP_USETHISSESSION); 6690 } 6691 6692 } 6693 } 6694 if (fndseq != 0) 6695 return (NFSDSP_SEQTHISSESSION); 6696 return (NFSDSP_NOTFOUND); 6697 } 6698 6699 /* 6700 * NFS commit rpc to a NFSv4.1 DS. 6701 */ 6702 static int 6703 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp, 6704 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred, 6705 NFSPROC_T *p) 6706 { 6707 uint32_t *tl; 6708 struct nfsrv_descript nfsd, *nd = &nfsd; 6709 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6710 struct nfssockreq *nrp; 6711 struct nfsvattr na; 6712 int attrflag, error; 6713 6714 nd->nd_mrep = NULL; 6715 if (vers == 0 || vers == NFS_VER4) { 6716 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, 6717 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6718 vers = NFS_VER4; 6719 } else 6720 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh, 6721 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6722 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers, 6723 minorvers); 6724 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 6725 txdr_hyper(offset, tl); 6726 tl += 2; 6727 *tl = txdr_unsigned(cnt); 6728 nrp = dsp->nfsclds_sockp; 6729 if (nrp == NULL) 6730 /* If NULL, use the MDS socket. */ 6731 nrp = &nmp->nm_sockreq; 6732 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6733 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6734 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error, 6735 nd->nd_repstat); 6736 if (error != 0) 6737 return (error); 6738 if (nd->nd_repstat == 0) { 6739 if (vers == NFS_VER3) { 6740 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 6741 NULL); 6742 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error); 6743 if (error != 0) 6744 goto nfsmout; 6745 } 6746 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 6747 NFSLOCKDS(dsp); 6748 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 6749 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 6750 error = NFSERR_STALEWRITEVERF; 6751 } 6752 NFSUNLOCKDS(dsp); 6753 } 6754 nfsmout: 6755 if (error == 0 && nd->nd_repstat != 0) 6756 error = nd->nd_repstat; 6757 m_freem(nd->nd_mrep); 6758 return (error); 6759 } 6760 6761 /* 6762 * Start up the thread that will execute nfsrpc_commitds(). 6763 */ 6764 static void 6765 start_commitds(void *arg, int pending) 6766 { 6767 struct nfsclwritedsdorpc *drpc; 6768 6769 drpc = (struct nfsclwritedsdorpc *)arg; 6770 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len, 6771 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred, 6772 drpc->p); 6773 drpc->done = 1; 6774 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err); 6775 } 6776 6777 /* 6778 * Set up the commit DS mirror call for the pNFS I/O thread. 6779 */ 6780 static int 6781 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp, 6782 struct nfsfh *fhp, int vers, int minorvers, 6783 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6784 { 6785 int error, ret; 6786 6787 error = 0; 6788 drpc->done = 0; 6789 drpc->vp = vp; 6790 drpc->off = offset; 6791 drpc->len = cnt; 6792 drpc->dsp = dsp; 6793 drpc->fhp = fhp; 6794 drpc->vers = vers; 6795 drpc->minorvers = minorvers; 6796 drpc->cred = cred; 6797 drpc->p = p; 6798 drpc->inprog = 0; 6799 ret = EIO; 6800 if (nfs_pnfsiothreads != 0) { 6801 ret = nfs_pnfsio(start_commitds, drpc); 6802 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret); 6803 } 6804 if (ret != 0) 6805 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers, 6806 minorvers, cred, p); 6807 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error); 6808 return (error); 6809 } 6810 6811 /* 6812 * NFS Advise rpc 6813 */ 6814 APPLESTATIC int 6815 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise, 6816 struct ucred *cred, NFSPROC_T *p) 6817 { 6818 u_int32_t *tl; 6819 struct nfsrv_descript nfsd, *nd = &nfsd; 6820 nfsattrbit_t hints; 6821 int error; 6822 6823 NFSZERO_ATTRBIT(&hints); 6824 if (advise == POSIX_FADV_WILLNEED) 6825 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED); 6826 else if (advise == POSIX_FADV_DONTNEED) 6827 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED); 6828 else 6829 return (0); 6830 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp); 6831 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO); 6832 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER); 6833 txdr_hyper(offset, tl); 6834 tl += 2; 6835 txdr_hyper(cnt, tl); 6836 nfsrv_putattrbit(nd, &hints); 6837 error = nfscl_request(nd, vp, p, cred, NULL); 6838 if (error != 0) 6839 return (error); 6840 if (nd->nd_repstat != 0) 6841 error = nd->nd_repstat; 6842 m_freem(nd->nd_mrep); 6843 return (error); 6844 } 6845 6846 #ifdef notyet 6847 /* 6848 * NFS advise rpc to a NFSv4.2 DS. 6849 */ 6850 static int 6851 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise, 6852 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers, 6853 struct ucred *cred, NFSPROC_T *p) 6854 { 6855 uint32_t *tl; 6856 struct nfsrv_descript nfsd, *nd = &nfsd; 6857 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6858 struct nfssockreq *nrp; 6859 nfsattrbit_t hints; 6860 int error; 6861 6862 /* For NFS DSs prior to NFSv4.2, just return OK. */ 6863 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION) 6864 return (0); 6865 NFSZERO_ATTRBIT(&hints); 6866 if (advise == POSIX_FADV_WILLNEED) 6867 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED); 6868 else if (advise == POSIX_FADV_DONTNEED) 6869 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED); 6870 else 6871 return (0); 6872 nd->nd_mrep = NULL; 6873 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh, 6874 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers); 6875 vers = NFS_VER4; 6876 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers, 6877 minorvers); 6878 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO); 6879 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 6880 txdr_hyper(offset, tl); 6881 tl += 2; 6882 *tl = txdr_unsigned(cnt); 6883 nfsrv_putattrbit(nd, &hints); 6884 nrp = dsp->nfsclds_sockp; 6885 if (nrp == NULL) 6886 /* If NULL, use the MDS socket. */ 6887 nrp = &nmp->nm_sockreq; 6888 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 6889 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 6890 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error, 6891 nd->nd_repstat); 6892 if (error != 0) 6893 return (error); 6894 if (nd->nd_repstat != 0) 6895 error = nd->nd_repstat; 6896 m_freem(nd->nd_mrep); 6897 return (error); 6898 } 6899 6900 /* 6901 * Start up the thread that will execute nfsrpc_commitds(). 6902 */ 6903 static void 6904 start_adviseds(void *arg, int pending) 6905 { 6906 struct nfsclwritedsdorpc *drpc; 6907 6908 drpc = (struct nfsclwritedsdorpc *)arg; 6909 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len, 6910 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, 6911 drpc->cred, drpc->p); 6912 drpc->done = 1; 6913 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err); 6914 } 6915 6916 /* 6917 * Set up the commit DS mirror call for the pNFS I/O thread. 6918 */ 6919 static int 6920 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise, 6921 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers, 6922 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6923 { 6924 int error, ret; 6925 6926 error = 0; 6927 drpc->done = 0; 6928 drpc->vp = vp; 6929 drpc->off = offset; 6930 drpc->len = cnt; 6931 drpc->advise = advise; 6932 drpc->dsp = dsp; 6933 drpc->fhp = fhp; 6934 drpc->vers = vers; 6935 drpc->minorvers = minorvers; 6936 drpc->cred = cred; 6937 drpc->p = p; 6938 drpc->inprog = 0; 6939 ret = EIO; 6940 if (nfs_pnfsiothreads != 0) { 6941 ret = nfs_pnfsio(start_adviseds, drpc); 6942 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret); 6943 } 6944 if (ret != 0) 6945 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers, 6946 minorvers, cred, p); 6947 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error); 6948 return (error); 6949 } 6950 #endif /* notyet */ 6951 6952 /* 6953 * Do the Allocate operation, retrying for recovery. 6954 */ 6955 APPLESTATIC int 6956 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap, 6957 int *attrflagp, struct ucred *cred, NFSPROC_T *p, void *stuff) 6958 { 6959 int error, expireret = 0, retrycnt, nostateid; 6960 uint32_t clidrev = 0; 6961 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 6962 struct nfsfh *nfhp = NULL; 6963 nfsv4stateid_t stateid; 6964 off_t tmp_off; 6965 void *lckp; 6966 6967 if (len < 0) 6968 return (EINVAL); 6969 if (len == 0) 6970 return (0); 6971 tmp_off = off + len; 6972 NFSLOCKMNT(nmp); 6973 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) { 6974 NFSUNLOCKMNT(nmp); 6975 return (EFBIG); 6976 } 6977 if (nmp->nm_clp != NULL) 6978 clidrev = nmp->nm_clp->nfsc_clientidrev; 6979 NFSUNLOCKMNT(nmp); 6980 nfhp = VTONFS(vp)->n_fhp; 6981 retrycnt = 0; 6982 do { 6983 lckp = NULL; 6984 nostateid = 0; 6985 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 6986 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp); 6987 if (stateid.other[0] == 0 && stateid.other[1] == 0 && 6988 stateid.other[2] == 0) { 6989 nostateid = 1; 6990 NFSCL_DEBUG(1, "stateid0 in allocate\n"); 6991 } 6992 6993 /* 6994 * Not finding a stateid should probably never happen, 6995 * but just return an error for this case. 6996 */ 6997 if (nostateid != 0) 6998 error = EIO; 6999 else 7000 error = nfsrpc_allocaterpc(vp, off, len, &stateid, 7001 nap, attrflagp, cred, p, stuff); 7002 if (error == NFSERR_STALESTATEID) 7003 nfscl_initiate_recovery(nmp->nm_clp); 7004 if (lckp != NULL) 7005 nfscl_lockderef(lckp); 7006 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 7007 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 7008 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 7009 (void) nfs_catnap(PZERO, error, "nfs_allocate"); 7010 } else if ((error == NFSERR_EXPIRED || 7011 error == NFSERR_BADSTATEID) && clidrev != 0) { 7012 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 7013 } 7014 retrycnt++; 7015 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 7016 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 7017 error == NFSERR_STALEDONTRECOVER || 7018 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 7019 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 7020 expireret == 0 && clidrev != 0 && retrycnt < 4)); 7021 if (error != 0 && retrycnt >= 4) 7022 error = EIO; 7023 return (error); 7024 } 7025 7026 /* 7027 * The allocate RPC. 7028 */ 7029 static int 7030 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp, 7031 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p, 7032 void *stuff) 7033 { 7034 uint32_t *tl; 7035 int error; 7036 struct nfsrv_descript nfsd; 7037 struct nfsrv_descript *nd = &nfsd; 7038 nfsattrbit_t attrbits; 7039 7040 *attrflagp = 0; 7041 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp); 7042 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 7043 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 7044 txdr_hyper(off, tl); tl += 2; 7045 txdr_hyper(len, tl); tl += 2; 7046 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7047 NFSGETATTR_ATTRBIT(&attrbits); 7048 nfsrv_putattrbit(nd, &attrbits); 7049 error = nfscl_request(nd, vp, p, cred, stuff); 7050 if (error != 0) 7051 return (error); 7052 if (nd->nd_repstat == 0) { 7053 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7054 error = nfsm_loadattr(nd, nap); 7055 if (error == 0) 7056 *attrflagp = NFS_LATTR_NOSHRINK; 7057 } else 7058 error = nd->nd_repstat; 7059 nfsmout: 7060 m_freem(nd->nd_mrep); 7061 return (error); 7062 } 7063 7064 /* 7065 * Set up the XDR arguments for the LayoutGet operation. 7066 */ 7067 static void 7068 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset, 7069 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype, 7070 int layoutlen, int usecurstateid) 7071 { 7072 uint32_t *tl; 7073 7074 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER + 7075 NFSX_STATEID); 7076 *tl++ = newnfs_false; /* Don't signal availability. */ 7077 *tl++ = txdr_unsigned(layouttype); 7078 *tl++ = txdr_unsigned(iomode); 7079 txdr_hyper(offset, tl); 7080 tl += 2; 7081 txdr_hyper(len, tl); 7082 tl += 2; 7083 txdr_hyper(minlen, tl); 7084 tl += 2; 7085 if (usecurstateid != 0) { 7086 /* Special stateid for Current stateid. */ 7087 *tl++ = txdr_unsigned(1); 7088 *tl++ = 0; 7089 *tl++ = 0; 7090 *tl++ = 0; 7091 } else { 7092 *tl++ = txdr_unsigned(stateidp->seqid); 7093 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid); 7094 *tl++ = stateidp->other[0]; 7095 *tl++ = stateidp->other[1]; 7096 *tl++ = stateidp->other[2]; 7097 } 7098 *tl = txdr_unsigned(layoutlen); 7099 } 7100 7101 /* 7102 * Parse the reply for a successful LayoutGet operation. 7103 */ 7104 static int 7105 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd, 7106 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp) 7107 { 7108 uint32_t *tl; 7109 struct nfsclflayout *flp, *prevflp, *tflp; 7110 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen; 7111 int m, mirrorcnt; 7112 uint64_t retlen, off; 7113 struct nfsfh *nfhp; 7114 uint8_t *cp; 7115 uid_t user; 7116 gid_t grp; 7117 7118 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n"); 7119 error = 0; 7120 flp = NULL; 7121 gotiomode = -1; 7122 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID); 7123 if (*tl++ != 0) 7124 *retonclosep = 1; 7125 else 7126 *retonclosep = 0; 7127 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++); 7128 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep, 7129 (int)stateidp->seqid); 7130 stateidp->other[0] = *tl++; 7131 stateidp->other[1] = *tl++; 7132 stateidp->other[2] = *tl++; 7133 cnt = fxdr_unsigned(int, *tl); 7134 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt); 7135 if (cnt <= 0 || cnt > 10000) { 7136 /* Don't accept more than 10000 layouts in reply. */ 7137 error = NFSERR_BADXDR; 7138 goto nfsmout; 7139 } 7140 for (i = 0; i < cnt; i++) { 7141 /* Dissect to the layout type. */ 7142 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 7143 3 * NFSX_UNSIGNED); 7144 off = fxdr_hyper(tl); tl += 2; 7145 retlen = fxdr_hyper(tl); tl += 2; 7146 iomode = fxdr_unsigned(int, *tl++); 7147 laytype = fxdr_unsigned(int, *tl); 7148 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype, 7149 (uintmax_t)off, (uintmax_t)retlen, iomode); 7150 /* Ignore length of layout body for now. */ 7151 if (laytype == NFSLAYOUT_NFSV4_1_FILES) { 7152 /* Parse the File layout up to fhcnt. */ 7153 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + 7154 NFSX_HYPER + NFSX_V4DEVICEID); 7155 fhcnt = fxdr_unsigned(int, *(tl + 4 + 7156 NFSX_V4DEVICEID / NFSX_UNSIGNED)); 7157 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt); 7158 if (fhcnt < 0 || fhcnt > 100) { 7159 /* Don't accept more than 100 file handles. */ 7160 error = NFSERR_BADXDR; 7161 goto nfsmout; 7162 } 7163 if (fhcnt > 0) 7164 flp = malloc(sizeof(*flp) + fhcnt * 7165 sizeof(struct nfsfh *), M_NFSFLAYOUT, 7166 M_WAITOK); 7167 else 7168 flp = malloc(sizeof(*flp), M_NFSFLAYOUT, 7169 M_WAITOK); 7170 flp->nfsfl_flags = NFSFL_FILE; 7171 flp->nfsfl_fhcnt = 0; 7172 flp->nfsfl_devp = NULL; 7173 flp->nfsfl_off = off; 7174 if (flp->nfsfl_off + retlen < flp->nfsfl_off) 7175 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off; 7176 else 7177 flp->nfsfl_end = flp->nfsfl_off + retlen; 7178 flp->nfsfl_iomode = iomode; 7179 if (gotiomode == -1) 7180 gotiomode = flp->nfsfl_iomode; 7181 /* Ignore layout body length for now. */ 7182 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID); 7183 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 7184 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++); 7185 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util); 7186 mtx_lock(&nmp->nm_mtx); 7187 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util & 7188 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0) 7189 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS; 7190 mtx_unlock(&nmp->nm_mtx); 7191 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++); 7192 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2; 7193 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n", 7194 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff); 7195 for (j = 0; j < fhcnt; j++) { 7196 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7197 nfhlen = fxdr_unsigned(int, *tl); 7198 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) { 7199 error = NFSERR_BADXDR; 7200 goto nfsmout; 7201 } 7202 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1, 7203 M_NFSFH, M_WAITOK); 7204 flp->nfsfl_fh[j] = nfhp; 7205 flp->nfsfl_fhcnt++; 7206 nfhp->nfh_len = nfhlen; 7207 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen)); 7208 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen); 7209 } 7210 } else if (laytype == NFSLAYOUT_FLEXFILE) { 7211 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + 7212 NFSX_HYPER); 7213 mirrorcnt = fxdr_unsigned(int, *(tl + 2)); 7214 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt); 7215 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) { 7216 error = NFSERR_BADXDR; 7217 goto nfsmout; 7218 } 7219 flp = malloc(sizeof(*flp) + mirrorcnt * 7220 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK); 7221 flp->nfsfl_flags = NFSFL_FLEXFILE; 7222 flp->nfsfl_mirrorcnt = mirrorcnt; 7223 for (j = 0; j < mirrorcnt; j++) 7224 flp->nfsfl_ffm[j].devp = NULL; 7225 flp->nfsfl_off = off; 7226 if (flp->nfsfl_off + retlen < flp->nfsfl_off) 7227 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off; 7228 else 7229 flp->nfsfl_end = flp->nfsfl_off + retlen; 7230 flp->nfsfl_iomode = iomode; 7231 if (gotiomode == -1) 7232 gotiomode = flp->nfsfl_iomode; 7233 flp->nfsfl_stripeunit = fxdr_hyper(tl); 7234 NFSCL_DEBUG(4, "stripeunit=%ju\n", 7235 (uintmax_t)flp->nfsfl_stripeunit); 7236 for (j = 0; j < mirrorcnt; j++) { 7237 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7238 k = fxdr_unsigned(int, *tl); 7239 if (k < 1 || k > 128) { 7240 error = NFSERR_BADXDR; 7241 goto nfsmout; 7242 } 7243 NFSCL_DEBUG(4, "servercnt=%d\n", k); 7244 for (l = 0; l < k; l++) { 7245 NFSM_DISSECT(tl, uint32_t *, 7246 NFSX_V4DEVICEID + NFSX_STATEID + 7247 2 * NFSX_UNSIGNED); 7248 if (l == 0) { 7249 /* Just use the first server. */ 7250 NFSBCOPY(tl, 7251 flp->nfsfl_ffm[j].dev, 7252 NFSX_V4DEVICEID); 7253 tl += (NFSX_V4DEVICEID / 7254 NFSX_UNSIGNED); 7255 tl++; 7256 flp->nfsfl_ffm[j].st.seqid = 7257 *tl++; 7258 flp->nfsfl_ffm[j].st.other[0] = 7259 *tl++; 7260 flp->nfsfl_ffm[j].st.other[1] = 7261 *tl++; 7262 flp->nfsfl_ffm[j].st.other[2] = 7263 *tl++; 7264 NFSCL_DEBUG(4, "st.seqid=%u " 7265 "st.o0=0x%x st.o1=0x%x " 7266 "st.o2=0x%x\n", 7267 flp->nfsfl_ffm[j].st.seqid, 7268 flp->nfsfl_ffm[j].st.other[0], 7269 flp->nfsfl_ffm[j].st.other[1], 7270 flp->nfsfl_ffm[j].st.other[2]); 7271 } else 7272 tl += ((NFSX_V4DEVICEID + 7273 NFSX_STATEID + 7274 NFSX_UNSIGNED) / 7275 NFSX_UNSIGNED); 7276 fhcnt = fxdr_unsigned(int, *tl); 7277 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt); 7278 if (fhcnt < 1 || 7279 fhcnt > NFSDEV_MAXVERS) { 7280 error = NFSERR_BADXDR; 7281 goto nfsmout; 7282 } 7283 for (m = 0; m < fhcnt; m++) { 7284 NFSM_DISSECT(tl, uint32_t *, 7285 NFSX_UNSIGNED); 7286 nfhlen = fxdr_unsigned(int, 7287 *tl); 7288 NFSCL_DEBUG(4, "nfhlen=%d\n", 7289 nfhlen); 7290 if (nfhlen <= 0 || nfhlen > 7291 NFSX_V4FHMAX) { 7292 error = NFSERR_BADXDR; 7293 goto nfsmout; 7294 } 7295 NFSM_DISSECT(cp, uint8_t *, 7296 NFSM_RNDUP(nfhlen)); 7297 if (l == 0) { 7298 flp->nfsfl_ffm[j].fhcnt 7299 = fhcnt; 7300 nfhp = malloc( 7301 sizeof(*nfhp) + 7302 nfhlen - 1, M_NFSFH, 7303 M_WAITOK); 7304 flp->nfsfl_ffm[j].fh[m] 7305 = nfhp; 7306 nfhp->nfh_len = nfhlen; 7307 NFSBCOPY(cp, 7308 nfhp->nfh_fh, 7309 nfhlen); 7310 NFSCL_DEBUG(4, 7311 "got fh\n"); 7312 } 7313 } 7314 /* Now, get the ffsd_user/ffds_group. */ 7315 error = nfsrv_parseug(nd, 0, &user, 7316 &grp, curthread); 7317 NFSCL_DEBUG(4, "after parseu=%d\n", 7318 error); 7319 if (error == 0) 7320 error = nfsrv_parseug(nd, 1, 7321 &user, &grp, curthread); 7322 NFSCL_DEBUG(4, "aft parseg=%d\n", 7323 grp); 7324 if (error != 0) 7325 goto nfsmout; 7326 NFSCL_DEBUG(4, "user=%d group=%d\n", 7327 user, grp); 7328 if (l == 0) { 7329 flp->nfsfl_ffm[j].user = user; 7330 flp->nfsfl_ffm[j].group = grp; 7331 NFSCL_DEBUG(4, 7332 "usr=%d grp=%d\n", user, 7333 grp); 7334 } 7335 } 7336 } 7337 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7338 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++); 7339 #ifdef notnow 7340 /* 7341 * At this time, there is no flag. 7342 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be 7343 * added, or it may never exist? 7344 */ 7345 mtx_lock(&nmp->nm_mtx); 7346 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags & 7347 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0) 7348 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS; 7349 mtx_unlock(&nmp->nm_mtx); 7350 #endif 7351 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl); 7352 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n", 7353 flp->nfsfl_fflags, flp->nfsfl_statshint); 7354 } else { 7355 error = NFSERR_BADXDR; 7356 goto nfsmout; 7357 } 7358 if (flp->nfsfl_iomode == gotiomode) { 7359 /* Keep the list in increasing offset order. */ 7360 tflp = LIST_FIRST(flhp); 7361 prevflp = NULL; 7362 while (tflp != NULL && 7363 tflp->nfsfl_off < flp->nfsfl_off) { 7364 prevflp = tflp; 7365 tflp = LIST_NEXT(tflp, nfsfl_list); 7366 } 7367 if (prevflp == NULL) 7368 LIST_INSERT_HEAD(flhp, flp, nfsfl_list); 7369 else 7370 LIST_INSERT_AFTER(prevflp, flp, 7371 nfsfl_list); 7372 NFSCL_DEBUG(4, "flp inserted\n"); 7373 } else { 7374 printf("nfscl_layoutget(): got wrong iomode\n"); 7375 nfscl_freeflayout(flp); 7376 } 7377 flp = NULL; 7378 } 7379 nfsmout: 7380 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error); 7381 if (error != 0 && flp != NULL) 7382 nfscl_freeflayout(flp); 7383 return (error); 7384 } 7385 7386 /* 7387 * Parse a user/group digit string. 7388 */ 7389 static int 7390 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp, 7391 NFSPROC_T *p) 7392 { 7393 uint32_t *tl; 7394 char *cp, *str, str0[NFSV4_SMALLSTR + 1]; 7395 uint32_t len = 0; 7396 int error = 0; 7397 7398 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7399 len = fxdr_unsigned(uint32_t, *tl); 7400 str = NULL; 7401 if (len > NFSV4_OPAQUELIMIT) { 7402 error = NFSERR_BADXDR; 7403 goto nfsmout; 7404 } 7405 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len); 7406 if (len == 0) { 7407 if (dogrp != 0) 7408 *gidp = GID_NOGROUP; 7409 else 7410 *uidp = UID_NOBODY; 7411 return (0); 7412 } 7413 if (len > NFSV4_SMALLSTR) 7414 str = malloc(len + 1, M_TEMP, M_WAITOK); 7415 else 7416 str = str0; 7417 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len)); 7418 NFSBCOPY(cp, str, len); 7419 str[len] = '\0'; 7420 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str); 7421 if (dogrp != 0) 7422 error = nfsv4_strtogid(nd, str, len, gidp); 7423 else 7424 error = nfsv4_strtouid(nd, str, len, uidp); 7425 nfsmout: 7426 if (len > NFSV4_SMALLSTR) 7427 free(str, M_TEMP); 7428 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error); 7429 return (error); 7430 } 7431 7432 /* 7433 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(), 7434 * so that it does both an Open and a Layoutget. 7435 */ 7436 static int 7437 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, 7438 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode, 7439 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp, 7440 struct ucred *cred, NFSPROC_T *p) 7441 { 7442 struct nfscllayout *lyp; 7443 struct nfsclflayout *flp; 7444 struct nfsclflayouthead flh; 7445 int error, islocked, layoutlen, recalled, retonclose, usecurstateid; 7446 int layouttype, laystat; 7447 nfsv4stateid_t stateid; 7448 struct nfsclsession *tsep; 7449 7450 error = 0; 7451 if (NFSHASFLEXFILE(nmp)) 7452 layouttype = NFSLAYOUT_FLEXFILE; 7453 else 7454 layouttype = NFSLAYOUT_NFSV4_1_FILES; 7455 /* 7456 * If lyp is returned non-NULL, there will be a refcnt (shared lock) 7457 * on it, iff flp != NULL or a lock (exclusive lock) on it iff 7458 * flp == NULL. 7459 */ 7460 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, &flp, 7461 &recalled); 7462 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp); 7463 if (lyp == NULL) 7464 islocked = 0; 7465 else if (flp != NULL) 7466 islocked = 1; 7467 else 7468 islocked = 2; 7469 if ((lyp == NULL || flp == NULL) && recalled == 0) { 7470 LIST_INIT(&flh); 7471 tsep = nfsmnt_mdssession(nmp); 7472 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 7473 3 * NFSX_UNSIGNED); 7474 if (lyp == NULL) 7475 usecurstateid = 1; 7476 else { 7477 usecurstateid = 0; 7478 stateid.seqid = lyp->nfsly_stateid.seqid; 7479 stateid.other[0] = lyp->nfsly_stateid.other[0]; 7480 stateid.other[1] = lyp->nfsly_stateid.other[1]; 7481 stateid.other[2] = lyp->nfsly_stateid.other[2]; 7482 } 7483 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen, 7484 newfhp, newfhlen, mode, op, name, namelen, 7485 dpp, &stateid, usecurstateid, layouttype, layoutlen, 7486 &retonclose, &flh, &laystat, cred, p); 7487 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n", 7488 laystat, error); 7489 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen, 7490 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat, 7491 &islocked, cred, p); 7492 } else 7493 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen, 7494 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0); 7495 if (islocked == 2) 7496 nfscl_rellayout(lyp, 1); 7497 else if (islocked == 1) 7498 nfscl_rellayout(lyp, 0); 7499 return (error); 7500 } 7501 7502 /* 7503 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS 7504 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are 7505 * handled by nfsrpc_openrpc(). 7506 * For the case where op == NULL, dvp is the directory. When op != NULL, it 7507 * can be NULL. 7508 */ 7509 static int 7510 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, 7511 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode, 7512 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp, 7513 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype, 7514 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp, 7515 int *laystatp, struct ucred *cred, NFSPROC_T *p) 7516 { 7517 uint32_t *tl; 7518 struct nfsrv_descript nfsd, *nd = &nfsd; 7519 struct nfscldeleg *ndp = NULL; 7520 struct nfsvattr nfsva; 7521 struct nfsclsession *tsep; 7522 uint32_t rflags, deleg; 7523 nfsattrbit_t attrbits; 7524 int error, ret, acesize, limitby, iomode; 7525 7526 *dpp = NULL; 7527 *laystatp = ENXIO; 7528 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL, 7529 0, 0); 7530 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED); 7531 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 7532 *tl++ = txdr_unsigned(mode & NFSV4OPEN_ACCESSBOTH); 7533 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 7534 tsep = nfsmnt_mdssession(nmp); 7535 *tl++ = tsep->nfsess_clientid.lval[0]; 7536 *tl = tsep->nfsess_clientid.lval[1]; 7537 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN); 7538 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7539 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE); 7540 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 7541 nfsm_strtom(nd, name, namelen); 7542 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 7543 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7544 NFSZERO_ATTRBIT(&attrbits); 7545 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 7546 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 7547 nfsrv_putattrbit(nd, &attrbits); 7548 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 7549 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET); 7550 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0) 7551 iomode = NFSLAYOUTIOMODE_RW; 7552 else 7553 iomode = NFSLAYOUTIOMODE_READ; 7554 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp, 7555 layouttype, layoutlen, usecurstateid); 7556 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 7557 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 7558 if (error != 0) 7559 return (error); 7560 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 7561 if (nd->nd_repstat != 0) 7562 *laystatp = nd->nd_repstat; 7563 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7564 /* ND_NOMOREDATA will be set if the Open operation failed. */ 7565 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7566 6 * NFSX_UNSIGNED); 7567 op->nfso_stateid.seqid = *tl++; 7568 op->nfso_stateid.other[0] = *tl++; 7569 op->nfso_stateid.other[1] = *tl++; 7570 op->nfso_stateid.other[2] = *tl; 7571 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 7572 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 7573 if (error != 0) 7574 goto nfsmout; 7575 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 7576 deleg = fxdr_unsigned(u_int32_t, *tl); 7577 if (deleg == NFSV4OPEN_DELEGATEREAD || 7578 deleg == NFSV4OPEN_DELEGATEWRITE) { 7579 if (!(op->nfso_own->nfsow_clp->nfsc_flags & 7580 NFSCLFLAGS_FIRSTDELEG)) 7581 op->nfso_own->nfsow_clp->nfsc_flags |= 7582 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 7583 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen, 7584 M_NFSCLDELEG, M_WAITOK); 7585 LIST_INIT(&ndp->nfsdl_owner); 7586 LIST_INIT(&ndp->nfsdl_lock); 7587 ndp->nfsdl_clp = op->nfso_own->nfsow_clp; 7588 ndp->nfsdl_fhlen = newfhlen; 7589 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen); 7590 newnfs_copyincred(cred, &ndp->nfsdl_cred); 7591 nfscl_lockinit(&ndp->nfsdl_rwlock); 7592 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7593 NFSX_UNSIGNED); 7594 ndp->nfsdl_stateid.seqid = *tl++; 7595 ndp->nfsdl_stateid.other[0] = *tl++; 7596 ndp->nfsdl_stateid.other[1] = *tl++; 7597 ndp->nfsdl_stateid.other[2] = *tl++; 7598 ret = fxdr_unsigned(int, *tl); 7599 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 7600 ndp->nfsdl_flags = NFSCLDL_WRITE; 7601 /* 7602 * Indicates how much the file can grow. 7603 */ 7604 NFSM_DISSECT(tl, u_int32_t *, 7605 3 * NFSX_UNSIGNED); 7606 limitby = fxdr_unsigned(int, *tl++); 7607 switch (limitby) { 7608 case NFSV4OPEN_LIMITSIZE: 7609 ndp->nfsdl_sizelimit = fxdr_hyper(tl); 7610 break; 7611 case NFSV4OPEN_LIMITBLOCKS: 7612 ndp->nfsdl_sizelimit = 7613 fxdr_unsigned(u_int64_t, *tl++); 7614 ndp->nfsdl_sizelimit *= 7615 fxdr_unsigned(u_int64_t, *tl); 7616 break; 7617 default: 7618 error = NFSERR_BADXDR; 7619 goto nfsmout; 7620 }; 7621 } else 7622 ndp->nfsdl_flags = NFSCLDL_READ; 7623 if (ret != 0) 7624 ndp->nfsdl_flags |= NFSCLDL_RECALL; 7625 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, &ret, 7626 &acesize, p); 7627 if (error != 0) 7628 goto nfsmout; 7629 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 7630 error = NFSERR_BADXDR; 7631 goto nfsmout; 7632 } 7633 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 || 7634 nfscl_assumeposixlocks) 7635 op->nfso_posixlock = 1; 7636 else 7637 op->nfso_posixlock = 0; 7638 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7639 /* If the 2nd element == NFS_OK, the Getattr succeeded. */ 7640 if (*++tl == 0) { 7641 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 7642 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 7643 NULL, NULL, NULL, p, cred); 7644 if (error != 0) 7645 goto nfsmout; 7646 if (ndp != NULL) { 7647 ndp->nfsdl_change = nfsva.na_filerev; 7648 ndp->nfsdl_modtime = nfsva.na_mtime; 7649 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET; 7650 *dpp = ndp; 7651 ndp = NULL; 7652 } 7653 /* 7654 * At this point, the Open has succeeded, so set 7655 * nd_repstat = NFS_OK. If the Layoutget failed, 7656 * this function just won't return a layout. 7657 */ 7658 if (nd->nd_repstat == 0) { 7659 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7660 *laystatp = fxdr_unsigned(int, *++tl); 7661 if (*laystatp == 0) { 7662 error = nfsrv_parselayoutget(nmp, nd, 7663 stateidp, retonclosep, flhp); 7664 if (error != 0) 7665 *laystatp = error; 7666 } 7667 } else 7668 nd->nd_repstat = 0; /* Return 0 for Open. */ 7669 } 7670 } 7671 if (nd->nd_repstat != 0 && error == 0) 7672 error = nd->nd_repstat; 7673 nfsmout: 7674 free(ndp, M_NFSCLDELEG); 7675 m_freem(nd->nd_mrep); 7676 return (error); 7677 } 7678 7679 /* 7680 * Similar nfsrpc_createv4(), but also does the LayoutGet operation. 7681 * Used only for mounts with pNFS enabled. 7682 */ 7683 static int 7684 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap, 7685 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 7686 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 7687 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 7688 int *dattrflagp, void *dstuff, int *unlockedp, nfsv4stateid_t *stateidp, 7689 int usecurstateid, int layouttype, int layoutlen, int *retonclosep, 7690 struct nfsclflayouthead *flhp, int *laystatp) 7691 { 7692 uint32_t *tl; 7693 int error = 0, deleg, newone, ret, acesize, limitby; 7694 struct nfsrv_descript nfsd, *nd = &nfsd; 7695 struct nfsclopen *op; 7696 struct nfscldeleg *dp = NULL; 7697 struct nfsnode *np; 7698 struct nfsfh *nfhp; 7699 struct nfsclsession *tsep; 7700 nfsattrbit_t attrbits; 7701 nfsv4stateid_t stateid; 7702 struct nfsmount *nmp; 7703 7704 nmp = VFSTONFS(dvp->v_mount); 7705 np = VTONFS(dvp); 7706 *laystatp = ENXIO; 7707 *unlockedp = 0; 7708 *nfhpp = NULL; 7709 *dpp = NULL; 7710 *attrflagp = 0; 7711 *dattrflagp = 0; 7712 if (namelen > NFS_MAXNAMLEN) 7713 return (ENAMETOOLONG); 7714 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp); 7715 /* 7716 * For V4, this is actually an Open op. 7717 */ 7718 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 7719 *tl++ = txdr_unsigned(owp->nfsow_seqid); 7720 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 7721 NFSV4OPEN_ACCESSREAD); 7722 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE); 7723 tsep = nfsmnt_mdssession(nmp); 7724 *tl++ = tsep->nfsess_clientid.lval[0]; 7725 *tl = tsep->nfsess_clientid.lval[1]; 7726 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 7727 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7728 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE); 7729 if ((fmode & O_EXCL) != 0) { 7730 if (NFSHASSESSPERSIST(nmp)) { 7731 /* Use GUARDED for persistent sessions. */ 7732 *tl = txdr_unsigned(NFSCREATE_GUARDED); 7733 nfscl_fillsattr(nd, vap, dvp, 0, 0); 7734 } else { 7735 /* Otherwise, use EXCLUSIVE4_1. */ 7736 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41); 7737 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 7738 *tl++ = cverf.lval[0]; 7739 *tl = cverf.lval[1]; 7740 nfscl_fillsattr(nd, vap, dvp, 0, 0); 7741 } 7742 } else { 7743 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 7744 nfscl_fillsattr(nd, vap, dvp, 0, 0); 7745 } 7746 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 7747 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 7748 nfsm_strtom(nd, name, namelen); 7749 /* Get the new file's handle and attributes, plus save the FH. */ 7750 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 7751 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH); 7752 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 7753 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7754 NFSGETATTR_ATTRBIT(&attrbits); 7755 nfsrv_putattrbit(nd, &attrbits); 7756 /* Get the directory's post-op attributes. */ 7757 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 7758 *tl = txdr_unsigned(NFSV4OP_PUTFH); 7759 nfsm_fhtom(nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0); 7760 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 7761 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7762 nfsrv_putattrbit(nd, &attrbits); 7763 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7764 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH); 7765 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET); 7766 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp, 7767 layouttype, layoutlen, usecurstateid); 7768 error = nfscl_request(nd, dvp, p, cred, dstuff); 7769 if (error != 0) 7770 return (error); 7771 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat, 7772 error); 7773 if (nd->nd_repstat != 0) 7774 *laystatp = nd->nd_repstat; 7775 NFSCL_INCRSEQID(owp->nfsow_seqid, nd); 7776 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7777 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n"); 7778 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7779 6 * NFSX_UNSIGNED); 7780 stateid.seqid = *tl++; 7781 stateid.other[0] = *tl++; 7782 stateid.other[1] = *tl++; 7783 stateid.other[2] = *tl; 7784 nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 7785 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 7786 deleg = fxdr_unsigned(int, *tl); 7787 if (deleg == NFSV4OPEN_DELEGATEREAD || 7788 deleg == NFSV4OPEN_DELEGATEWRITE) { 7789 if (!(owp->nfsow_clp->nfsc_flags & 7790 NFSCLFLAGS_FIRSTDELEG)) 7791 owp->nfsow_clp->nfsc_flags |= 7792 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 7793 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX, 7794 M_NFSCLDELEG, M_WAITOK); 7795 LIST_INIT(&dp->nfsdl_owner); 7796 LIST_INIT(&dp->nfsdl_lock); 7797 dp->nfsdl_clp = owp->nfsow_clp; 7798 newnfs_copyincred(cred, &dp->nfsdl_cred); 7799 nfscl_lockinit(&dp->nfsdl_rwlock); 7800 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 7801 NFSX_UNSIGNED); 7802 dp->nfsdl_stateid.seqid = *tl++; 7803 dp->nfsdl_stateid.other[0] = *tl++; 7804 dp->nfsdl_stateid.other[1] = *tl++; 7805 dp->nfsdl_stateid.other[2] = *tl++; 7806 ret = fxdr_unsigned(int, *tl); 7807 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 7808 dp->nfsdl_flags = NFSCLDL_WRITE; 7809 /* 7810 * Indicates how much the file can grow. 7811 */ 7812 NFSM_DISSECT(tl, u_int32_t *, 7813 3 * NFSX_UNSIGNED); 7814 limitby = fxdr_unsigned(int, *tl++); 7815 switch (limitby) { 7816 case NFSV4OPEN_LIMITSIZE: 7817 dp->nfsdl_sizelimit = fxdr_hyper(tl); 7818 break; 7819 case NFSV4OPEN_LIMITBLOCKS: 7820 dp->nfsdl_sizelimit = 7821 fxdr_unsigned(u_int64_t, *tl++); 7822 dp->nfsdl_sizelimit *= 7823 fxdr_unsigned(u_int64_t, *tl); 7824 break; 7825 default: 7826 error = NFSERR_BADXDR; 7827 goto nfsmout; 7828 }; 7829 } else { 7830 dp->nfsdl_flags = NFSCLDL_READ; 7831 } 7832 if (ret != 0) 7833 dp->nfsdl_flags |= NFSCLDL_RECALL; 7834 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, &ret, 7835 &acesize, p); 7836 if (error != 0) 7837 goto nfsmout; 7838 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 7839 error = NFSERR_BADXDR; 7840 goto nfsmout; 7841 } 7842 7843 /* Now, we should have the status for the SaveFH. */ 7844 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7845 if (*++tl == 0) { 7846 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n"); 7847 /* 7848 * Now, process the GetFH and Getattr for the newly 7849 * created file. nfscl_mtofh() will set 7850 * ND_NOMOREDATA if these weren't successful. 7851 */ 7852 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 7853 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error); 7854 if (error != 0) 7855 goto nfsmout; 7856 } else 7857 nd->nd_flag |= ND_NOMOREDATA; 7858 /* Now we have the PutFH and Getattr for the directory. */ 7859 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7860 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7861 if (*++tl != 0) 7862 nd->nd_flag |= ND_NOMOREDATA; 7863 else { 7864 NFSM_DISSECT(tl, uint32_t *, 2 * 7865 NFSX_UNSIGNED); 7866 if (*++tl != 0) 7867 nd->nd_flag |= ND_NOMOREDATA; 7868 } 7869 } 7870 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 7871 /* Load the directory attributes. */ 7872 error = nfsm_loadattr(nd, dnap); 7873 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error); 7874 if (error != 0) 7875 goto nfsmout; 7876 *dattrflagp = 1; 7877 if (dp != NULL && *attrflagp != 0) { 7878 dp->nfsdl_change = nnap->na_filerev; 7879 dp->nfsdl_modtime = nnap->na_mtime; 7880 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 7881 } 7882 /* 7883 * We can now complete the Open state. 7884 */ 7885 nfhp = *nfhpp; 7886 if (dp != NULL) { 7887 dp->nfsdl_fhlen = nfhp->nfh_len; 7888 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, 7889 nfhp->nfh_len); 7890 } 7891 /* 7892 * Get an Open structure that will be 7893 * attached to the OpenOwner, acquired already. 7894 */ 7895 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 7896 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0, 7897 cred, p, NULL, &op, &newone, NULL, 0); 7898 if (error != 0) 7899 goto nfsmout; 7900 op->nfso_stateid = stateid; 7901 newnfs_copyincred(cred, &op->nfso_cred); 7902 7903 nfscl_openrelease(nmp, op, error, newone); 7904 *unlockedp = 1; 7905 7906 /* Now, handle the RestoreFH and LayoutGet. */ 7907 if (nd->nd_repstat == 0) { 7908 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED); 7909 *laystatp = fxdr_unsigned(int, *(tl + 3)); 7910 if (*laystatp == 0) { 7911 error = nfsrv_parselayoutget(nmp, nd, 7912 stateidp, retonclosep, flhp); 7913 if (error != 0) 7914 *laystatp = error; 7915 } 7916 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n", 7917 error); 7918 } else 7919 nd->nd_repstat = 0; 7920 } 7921 } 7922 if (nd->nd_repstat != 0 && error == 0) 7923 error = nd->nd_repstat; 7924 if (error == NFSERR_STALECLIENTID || error == NFSERR_BADSESSION) 7925 nfscl_initiate_recovery(owp->nfsow_clp); 7926 nfsmout: 7927 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error); 7928 if (error == 0) 7929 *dpp = dp; 7930 else 7931 free(dp, M_NFSCLDELEG); 7932 m_freem(nd->nd_mrep); 7933 return (error); 7934 } 7935 7936 /* 7937 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case. 7938 */ 7939 static int 7940 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap, 7941 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 7942 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 7943 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 7944 int *dattrflagp, void *dstuff, int *unlockedp) 7945 { 7946 struct nfscllayout *lyp; 7947 struct nfsclflayouthead flh; 7948 struct nfsfh *nfhp; 7949 struct nfsclsession *tsep; 7950 struct nfsmount *nmp; 7951 nfsv4stateid_t stateid; 7952 int error, layoutlen, layouttype, retonclose, laystat; 7953 7954 error = 0; 7955 nmp = VFSTONFS(dvp->v_mount); 7956 if (NFSHASFLEXFILE(nmp)) 7957 layouttype = NFSLAYOUT_FLEXFILE; 7958 else 7959 layouttype = NFSLAYOUT_NFSV4_1_FILES; 7960 LIST_INIT(&flh); 7961 tsep = nfsmnt_mdssession(nmp); 7962 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED); 7963 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode, 7964 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 7965 dstuff, unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose, 7966 &flh, &laystat); 7967 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n", 7968 laystat, error); 7969 lyp = NULL; 7970 if (laystat == 0) { 7971 nfhp = *nfhpp; 7972 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh, 7973 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh, 7974 layouttype, laystat, NULL, cred, p); 7975 } else 7976 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid, 7977 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL, 7978 cred, p); 7979 if (laystat == 0) 7980 nfscl_rellayout(lyp, 0); 7981 return (error); 7982 } 7983 7984 /* 7985 * Process the results of a layoutget() operation. 7986 */ 7987 static int 7988 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp, 7989 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit, 7990 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype, 7991 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p) 7992 { 7993 struct nfsclflayout *tflp; 7994 struct nfscldevinfo *dip; 7995 uint8_t *dev; 7996 int i, mirrorcnt; 7997 7998 if (laystat == NFSERR_UNKNLAYOUTTYPE) { 7999 NFSLOCKMNT(nmp); 8000 if (!NFSHASFLEXFILE(nmp)) { 8001 /* Switch to using Flex File Layout. */ 8002 nmp->nm_state |= NFSSTA_FLEXFILE; 8003 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 8004 /* Disable pNFS. */ 8005 NFSCL_DEBUG(1, "disable PNFS\n"); 8006 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE); 8007 } 8008 NFSUNLOCKMNT(nmp); 8009 } 8010 if (laystat == 0) { 8011 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n"); 8012 LIST_FOREACH(tflp, flhp, nfsfl_list) { 8013 if (layouttype == NFSLAYOUT_FLEXFILE) 8014 mirrorcnt = tflp->nfsfl_mirrorcnt; 8015 else 8016 mirrorcnt = 1; 8017 for (i = 0; i < mirrorcnt; i++) { 8018 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp); 8019 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat); 8020 if (laystat != 0) { 8021 if (layouttype == NFSLAYOUT_FLEXFILE) 8022 dev = tflp->nfsfl_ffm[i].dev; 8023 else 8024 dev = tflp->nfsfl_dev; 8025 laystat = nfsrpc_getdeviceinfo(nmp, dev, 8026 layouttype, notifybit, &dip, cred, 8027 p); 8028 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n", 8029 laystat); 8030 if (laystat != 0) 8031 goto out; 8032 laystat = nfscl_adddevinfo(nmp, dip, i, 8033 tflp); 8034 if (laystat != 0) 8035 printf("nfsrpc_layoutgetresout" 8036 ": cannot add\n"); 8037 } 8038 } 8039 } 8040 } 8041 out: 8042 if (laystat == 0) { 8043 /* 8044 * nfscl_layout() always returns with the nfsly_lock 8045 * set to a refcnt (shared lock). 8046 * Passing in dvp is sufficient, since it is only used to 8047 * get the fsid for the file system. 8048 */ 8049 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp, 8050 layouttype, retonclose, flhp, lypp, cred, p); 8051 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n", 8052 laystat); 8053 if (laystat == 0 && islockedp != NULL) 8054 *islockedp = 1; 8055 } 8056 return (laystat); 8057 } 8058 8059 /* 8060 * nfs copy_file_range operation. 8061 */ 8062 APPLESTATIC int 8063 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp, 8064 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp, 8065 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap, 8066 struct ucred *cred, bool consecutive, bool *must_commitp) 8067 { 8068 int commit, error, expireret = 0, retrycnt; 8069 u_int32_t clidrev = 0; 8070 struct nfsmount *nmp = VFSTONFS(vnode_mount(invp)); 8071 struct nfsfh *innfhp = NULL, *outnfhp = NULL; 8072 nfsv4stateid_t instateid, outstateid; 8073 void *inlckp, *outlckp; 8074 8075 if (nmp->nm_clp != NULL) 8076 clidrev = nmp->nm_clp->nfsc_clientidrev; 8077 innfhp = VTONFS(invp)->n_fhp; 8078 outnfhp = VTONFS(outvp)->n_fhp; 8079 retrycnt = 0; 8080 do { 8081 /* Get both stateids. */ 8082 inlckp = NULL; 8083 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len, 8084 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid, 8085 &inlckp); 8086 outlckp = NULL; 8087 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len, 8088 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid, 8089 &outlckp); 8090 8091 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp, 8092 &instateid, &outstateid, innap, inattrflagp, outnap, 8093 outattrflagp, consecutive, &commit, cred, curthread); 8094 if (error == 0) { 8095 if (commit != NFSWRITE_FILESYNC) 8096 *must_commitp = true; 8097 *inoffp += *lenp; 8098 *outoffp += *lenp; 8099 } else if (error == NFSERR_STALESTATEID) 8100 nfscl_initiate_recovery(nmp->nm_clp); 8101 if (inlckp != NULL) 8102 nfscl_lockderef(inlckp); 8103 if (outlckp != NULL) 8104 nfscl_lockderef(outlckp); 8105 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8106 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8107 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 8108 (void) nfs_catnap(PZERO, error, "nfs_cfr"); 8109 } else if ((error == NFSERR_EXPIRED || 8110 error == NFSERR_BADSTATEID) && clidrev != 0) { 8111 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, 8112 curthread); 8113 } 8114 retrycnt++; 8115 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 8116 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 8117 error == NFSERR_STALEDONTRECOVER || 8118 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 8119 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 8120 expireret == 0 && clidrev != 0 && retrycnt < 4)); 8121 if (error != 0 && (retrycnt >= 4 || 8122 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 8123 error == NFSERR_STALEDONTRECOVER)) 8124 error = EIO; 8125 return (error); 8126 } 8127 8128 /* 8129 * The copy RPC. 8130 */ 8131 static int 8132 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff, 8133 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp, 8134 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap, 8135 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred, 8136 NFSPROC_T *p) 8137 { 8138 uint32_t *tl; 8139 int error; 8140 struct nfsrv_descript nfsd; 8141 struct nfsrv_descript *nd = &nfsd; 8142 struct nfsmount *nmp; 8143 nfsattrbit_t attrbits; 8144 uint64_t len; 8145 8146 nmp = VFSTONFS(outvp->v_mount); 8147 *inattrflagp = *outattrflagp = 0; 8148 *commitp = NFSWRITE_UNSTABLE; 8149 len = *lenp; 8150 *lenp = 0; 8151 if (len > nfs_maxcopyrange) 8152 len = nfs_maxcopyrange; 8153 NFSCL_REQSTART(nd, NFSPROC_COPY, invp); 8154 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8155 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8156 NFSGETATTR_ATTRBIT(&attrbits); 8157 nfsrv_putattrbit(nd, &attrbits); 8158 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8159 *tl = txdr_unsigned(NFSV4OP_PUTFH); 8160 nfsm_fhtom(nd, VTONFS(outvp)->n_fhp->nfh_fh, 8161 VTONFS(outvp)->n_fhp->nfh_len, 0); 8162 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8163 *tl = txdr_unsigned(NFSV4OP_COPY); 8164 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID); 8165 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID); 8166 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED); 8167 txdr_hyper(inoff, tl); tl += 2; 8168 txdr_hyper(outoff, tl); tl += 2; 8169 txdr_hyper(len, tl); tl += 2; 8170 if (consecutive) 8171 *tl++ = newnfs_true; 8172 else 8173 *tl++ = newnfs_false; 8174 *tl++ = newnfs_true; 8175 *tl++ = 0; 8176 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8177 NFSWRITEGETATTR_ATTRBIT(&attrbits); 8178 nfsrv_putattrbit(nd, &attrbits); 8179 error = nfscl_request(nd, invp, p, cred, NULL); 8180 if (error != 0) 8181 return (error); 8182 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8183 /* Get the input file's attributes. */ 8184 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8185 if (*(tl + 1) == 0) { 8186 error = nfsm_loadattr(nd, innap); 8187 if (error != 0) 8188 goto nfsmout; 8189 *inattrflagp = 1; 8190 } else 8191 nd->nd_flag |= ND_NOMOREDATA; 8192 } 8193 /* Skip over return stat for PutFH. */ 8194 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8195 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8196 if (*++tl != 0) 8197 nd->nd_flag |= ND_NOMOREDATA; 8198 } 8199 /* Skip over return stat for Copy. */ 8200 if ((nd->nd_flag & ND_NOMOREDATA) == 0) 8201 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8202 if (nd->nd_repstat == 0) { 8203 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8204 if (*tl != 0) { 8205 /* There should be no callback ids. */ 8206 error = NFSERR_BADXDR; 8207 goto nfsmout; 8208 } 8209 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED + 8210 NFSX_VERF); 8211 len = fxdr_hyper(tl); tl += 2; 8212 *commitp = fxdr_unsigned(int, *tl++); 8213 NFSLOCKMNT(nmp); 8214 if (!NFSHASWRITEVERF(nmp)) { 8215 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 8216 NFSSETWRITEVERF(nmp); 8217 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) { 8218 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 8219 nd->nd_repstat = NFSERR_STALEWRITEVERF; 8220 } 8221 NFSUNLOCKMNT(nmp); 8222 tl += (NFSX_VERF / NFSX_UNSIGNED); 8223 if (nd->nd_repstat == 0 && *++tl != newnfs_true) 8224 /* Must be a synchronous copy. */ 8225 nd->nd_repstat = NFSERR_NOTSUPP; 8226 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8227 error = nfsm_loadattr(nd, outnap); 8228 if (error == 0) 8229 *outattrflagp = NFS_LATTR_NOSHRINK; 8230 if (nd->nd_repstat == 0) 8231 *lenp = len; 8232 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) { 8233 /* 8234 * For the case where consecutive is not supported, but 8235 * synchronous is supported, we can try consecutive == false 8236 * by returning this error. Otherwise, return NFSERR_NOTSUPP, 8237 * since Copy cannot be done. 8238 */ 8239 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8240 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8241 if (!consecutive || *++tl == newnfs_false) 8242 nd->nd_repstat = NFSERR_NOTSUPP; 8243 } else 8244 nd->nd_repstat = NFSERR_BADXDR; 8245 } 8246 if (error == 0) 8247 error = nd->nd_repstat; 8248 nfsmout: 8249 m_freem(nd->nd_mrep); 8250 return (error); 8251 } 8252 8253 /* 8254 * Seek operation. 8255 */ 8256 APPLESTATIC int 8257 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content, 8258 struct ucred *cred, struct nfsvattr *nap, int *attrflagp) 8259 { 8260 int error, expireret = 0, retrycnt; 8261 u_int32_t clidrev = 0; 8262 struct nfsmount *nmp = VFSTONFS(vnode_mount(vp)); 8263 struct nfsnode *np = VTONFS(vp); 8264 struct nfsfh *nfhp = NULL; 8265 nfsv4stateid_t stateid; 8266 void *lckp; 8267 8268 if (nmp->nm_clp != NULL) 8269 clidrev = nmp->nm_clp->nfsc_clientidrev; 8270 nfhp = np->n_fhp; 8271 retrycnt = 0; 8272 do { 8273 lckp = NULL; 8274 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 8275 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp); 8276 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content, 8277 nap, attrflagp, cred); 8278 if (error == NFSERR_STALESTATEID) 8279 nfscl_initiate_recovery(nmp->nm_clp); 8280 if (lckp != NULL) 8281 nfscl_lockderef(lckp); 8282 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8283 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8284 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 8285 (void) nfs_catnap(PZERO, error, "nfs_seek"); 8286 } else if ((error == NFSERR_EXPIRED || 8287 error == NFSERR_BADSTATEID) && clidrev != 0) { 8288 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, 8289 curthread); 8290 } 8291 retrycnt++; 8292 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8293 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8294 error == NFSERR_BADSESSION || 8295 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 8296 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 8297 expireret == 0 && clidrev != 0 && retrycnt < 4) || 8298 (error == NFSERR_OPENMODE && retrycnt < 4)); 8299 if (error && retrycnt >= 4) 8300 error = EIO; 8301 return (error); 8302 } 8303 8304 /* 8305 * The seek RPC. 8306 */ 8307 static int 8308 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp, 8309 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred) 8310 { 8311 uint32_t *tl; 8312 int error; 8313 struct nfsrv_descript nfsd; 8314 struct nfsrv_descript *nd = &nfsd; 8315 nfsattrbit_t attrbits; 8316 8317 *attrflagp = 0; 8318 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp); 8319 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 8320 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 8321 txdr_hyper(*offp, tl); tl += 2; 8322 *tl++ = txdr_unsigned(content); 8323 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8324 NFSGETATTR_ATTRBIT(&attrbits); 8325 nfsrv_putattrbit(nd, &attrbits); 8326 error = nfscl_request(nd, vp, curthread, cred, NULL); 8327 if (error != 0) 8328 return (error); 8329 if (nd->nd_repstat == 0) { 8330 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER); 8331 if (*tl++ == newnfs_true) 8332 *eofp = true; 8333 else 8334 *eofp = false; 8335 *offp = fxdr_hyper(tl); 8336 /* Just skip over Getattr op status. */ 8337 error = nfsm_loadattr(nd, nap); 8338 if (error == 0) 8339 *attrflagp = 1; 8340 } 8341 error = nd->nd_repstat; 8342 nfsmout: 8343 m_freem(nd->nd_mrep); 8344 return (error); 8345 } 8346 8347 /* 8348 * The getextattr RPC. 8349 */ 8350 APPLESTATIC int 8351 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp, 8352 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 8353 { 8354 uint32_t *tl; 8355 int error; 8356 struct nfsrv_descript nfsd; 8357 struct nfsrv_descript *nd = &nfsd; 8358 nfsattrbit_t attrbits; 8359 uint32_t len, len2; 8360 8361 *attrflagp = 0; 8362 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp); 8363 nfsm_strtom(nd, name, strlen(name)); 8364 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8365 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8366 NFSGETATTR_ATTRBIT(&attrbits); 8367 nfsrv_putattrbit(nd, &attrbits); 8368 error = nfscl_request(nd, vp, p, cred, NULL); 8369 if (error != 0) 8370 return (error); 8371 if (nd->nd_repstat == 0) { 8372 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8373 len = fxdr_unsigned(uint32_t, *tl); 8374 /* Sanity check lengths. */ 8375 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX && 8376 uiop->uio_resid <= UINT32_MAX) { 8377 len2 = uiop->uio_resid; 8378 if (len2 >= len) 8379 error = nfsm_mbufuio(nd, uiop, len); 8380 else { 8381 error = nfsm_mbufuio(nd, uiop, len2); 8382 if (error == 0) { 8383 /* 8384 * nfsm_mbufuio() advances to a multiple 8385 * of 4, so round up len2 as well. Then 8386 * we need to advance over the rest of 8387 * the data, rounding up the remaining 8388 * length. 8389 */ 8390 len2 = NFSM_RNDUP(len2); 8391 len2 = NFSM_RNDUP(len - len2); 8392 if (len2 > 0) 8393 error = nfsm_advance(nd, len2, 8394 -1); 8395 } 8396 } 8397 } else if (uiop == NULL && len > 0) { 8398 /* Just wants the length and not the data. */ 8399 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 8400 } else if (len > 0) 8401 error = ENOATTR; 8402 if (error != 0) 8403 goto nfsmout; 8404 *lenp = len; 8405 /* Just skip over Getattr op status. */ 8406 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8407 error = nfsm_loadattr(nd, nap); 8408 if (error == 0) 8409 *attrflagp = 1; 8410 } 8411 if (error == 0) 8412 error = nd->nd_repstat; 8413 nfsmout: 8414 m_freem(nd->nd_mrep); 8415 return (error); 8416 } 8417 8418 /* 8419 * The setextattr RPC. 8420 */ 8421 APPLESTATIC int 8422 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop, 8423 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 8424 { 8425 uint32_t *tl; 8426 int error; 8427 struct nfsrv_descript nfsd; 8428 struct nfsrv_descript *nd = &nfsd; 8429 nfsattrbit_t attrbits; 8430 8431 *attrflagp = 0; 8432 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp); 8433 if (uiop->uio_resid > nd->nd_maxreq) { 8434 /* nd_maxreq is set by NFSCL_REQSTART(). */ 8435 m_freem(nd->nd_mreq); 8436 return (EINVAL); 8437 } 8438 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8439 *tl = txdr_unsigned(NFSV4SXATTR_EITHER); 8440 nfsm_strtom(nd, name, strlen(name)); 8441 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8442 *tl = txdr_unsigned(uiop->uio_resid); 8443 nfsm_uiombuf(nd, uiop, uiop->uio_resid); 8444 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8445 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8446 NFSGETATTR_ATTRBIT(&attrbits); 8447 nfsrv_putattrbit(nd, &attrbits); 8448 error = nfscl_request(nd, vp, p, cred, NULL); 8449 if (error != 0) 8450 return (error); 8451 if (nd->nd_repstat == 0) { 8452 /* Just skip over the reply and Getattr op status. */ 8453 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 * 8454 NFSX_UNSIGNED); 8455 error = nfsm_loadattr(nd, nap); 8456 if (error == 0) 8457 *attrflagp = 1; 8458 } 8459 if (error == 0) 8460 error = nd->nd_repstat; 8461 nfsmout: 8462 m_freem(nd->nd_mrep); 8463 return (error); 8464 } 8465 8466 /* 8467 * The removeextattr RPC. 8468 */ 8469 APPLESTATIC int 8470 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap, 8471 int *attrflagp, struct ucred *cred, NFSPROC_T *p) 8472 { 8473 uint32_t *tl; 8474 int error; 8475 struct nfsrv_descript nfsd; 8476 struct nfsrv_descript *nd = &nfsd; 8477 nfsattrbit_t attrbits; 8478 8479 *attrflagp = 0; 8480 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp); 8481 nfsm_strtom(nd, name, strlen(name)); 8482 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8483 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8484 NFSGETATTR_ATTRBIT(&attrbits); 8485 nfsrv_putattrbit(nd, &attrbits); 8486 error = nfscl_request(nd, vp, p, cred, NULL); 8487 if (error != 0) 8488 return (error); 8489 if (nd->nd_repstat == 0) { 8490 /* Just skip over the reply and Getattr op status. */ 8491 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 * 8492 NFSX_UNSIGNED); 8493 error = nfsm_loadattr(nd, nap); 8494 if (error == 0) 8495 *attrflagp = 1; 8496 } 8497 if (error == 0) 8498 error = nd->nd_repstat; 8499 nfsmout: 8500 m_freem(nd->nd_mrep); 8501 return (error); 8502 } 8503 8504 /* 8505 * The listextattr RPC. 8506 */ 8507 APPLESTATIC int 8508 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop, 8509 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp, 8510 struct ucred *cred, NFSPROC_T *p) 8511 { 8512 uint32_t *tl; 8513 int cnt, error, i, len; 8514 struct nfsrv_descript nfsd; 8515 struct nfsrv_descript *nd = &nfsd; 8516 nfsattrbit_t attrbits; 8517 u_char c; 8518 8519 *attrflagp = 0; 8520 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp); 8521 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 8522 txdr_hyper(*cookiep, tl); tl += 2; 8523 *tl++ = txdr_unsigned(*lenp); 8524 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8525 NFSGETATTR_ATTRBIT(&attrbits); 8526 nfsrv_putattrbit(nd, &attrbits); 8527 error = nfscl_request(nd, vp, p, cred, NULL); 8528 if (error != 0) 8529 return (error); 8530 *eofp = true; 8531 *lenp = 0; 8532 if (nd->nd_repstat == 0) { 8533 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 8534 *cookiep = fxdr_hyper(tl); tl += 2; 8535 cnt = fxdr_unsigned(int, *tl); 8536 if (cnt < 0) { 8537 error = EBADRPC; 8538 goto nfsmout; 8539 } 8540 for (i = 0; i < cnt; i++) { 8541 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8542 len = fxdr_unsigned(int, *tl); 8543 if (len <= 0 || len > EXTATTR_MAXNAMELEN) { 8544 error = EBADRPC; 8545 goto nfsmout; 8546 } 8547 if (uiop == NULL) 8548 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 8549 else if (uiop->uio_resid >= len + 1) { 8550 c = len; 8551 error = uiomove(&c, sizeof(c), uiop); 8552 if (error == 0) 8553 error = nfsm_mbufuio(nd, uiop, len); 8554 } else { 8555 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 8556 *eofp = false; 8557 } 8558 if (error != 0) 8559 goto nfsmout; 8560 *lenp += (len + 1); 8561 } 8562 /* Get the eof and skip over the Getattr op status. */ 8563 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED); 8564 /* 8565 * *eofp is set false above, because it wasn't able to copy 8566 * all of the reply. 8567 */ 8568 if (*eofp && *tl == 0) 8569 *eofp = false; 8570 error = nfsm_loadattr(nd, nap); 8571 if (error == 0) 8572 *attrflagp = 1; 8573 } 8574 if (error == 0) 8575 error = nd->nd_repstat; 8576 nfsmout: 8577 m_freem(nd->nd_mrep); 8578 return (error); 8579 } 8580 8581