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