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