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