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