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 } 4162 4163 /* 4164 * Loop around doing readdir rpc's of size nm_readdirsize. 4165 * The stopping criteria is EOF or buffer full. 4166 */ 4167 while (more_dirs && bigenough) { 4168 *attrflagp = 0; 4169 NFSCL_REQSTART(nd, NFSPROC_READDIRPLUS, vp, cred); 4170 NFSM_BUILD(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 4171 *tl++ = cookie.lval[0]; 4172 *tl++ = cookie.lval[1]; 4173 if (cookie.qval == 0) { 4174 *tl++ = 0; 4175 *tl++ = 0; 4176 } else { 4177 NFSLOCKNODE(dnp); 4178 *tl++ = dnp->n_cookieverf.nfsuquad[0]; 4179 *tl++ = dnp->n_cookieverf.nfsuquad[1]; 4180 NFSUNLOCKNODE(dnp); 4181 } 4182 *tl++ = txdr_unsigned(nmp->nm_readdirsize); 4183 *tl = txdr_unsigned(nmp->nm_readdirsize); 4184 if (nd->nd_flag & ND_NFSV4) { 4185 (void) nfsrv_putattrbit(nd, &attrbits); 4186 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4187 *tl = txdr_unsigned(NFSV4OP_GETATTR); 4188 (void) nfsrv_putattrbit(nd, &dattrbits); 4189 } 4190 nanouptime(&ts); 4191 error = nfscl_request(nd, vp, p, cred); 4192 if (error) 4193 return (error); 4194 if (nd->nd_flag & ND_NFSV3) 4195 error = nfscl_postop_attr(nd, nap, attrflagp); 4196 if (nd->nd_repstat || error) { 4197 if (!error) 4198 error = nd->nd_repstat; 4199 goto nfsmout; 4200 } 4201 if ((nd->nd_flag & ND_NFSV3) != 0 && *attrflagp != 0) 4202 dctime = nap->na_ctime; 4203 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 4204 NFSLOCKNODE(dnp); 4205 dnp->n_cookieverf.nfsuquad[0] = *tl++; 4206 dnp->n_cookieverf.nfsuquad[1] = *tl++; 4207 NFSUNLOCKNODE(dnp); 4208 more_dirs = fxdr_unsigned(int, *tl); 4209 if (!more_dirs) 4210 tryformoredirs = 0; 4211 4212 /* loop through the dir entries, doctoring them to 4bsd form */ 4213 while (more_dirs && bigenough) { 4214 validentry = true; 4215 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 4216 if (nd->nd_flag & ND_NFSV4) { 4217 ncookie.lval[0] = *tl++; 4218 ncookie.lval[1] = *tl++; 4219 } else { 4220 fileno = fxdr_hyper(tl); 4221 tl += 2; 4222 } 4223 len = fxdr_unsigned(int, *tl); 4224 if (len <= 0 || len > NFS_MAXNAMLEN) { 4225 error = EBADRPC; 4226 goto nfsmout; 4227 } 4228 tlen = roundup2(len, 8); 4229 if (tlen == len) 4230 tlen += 8; /* To ensure null termination. */ 4231 left = DIRBLKSIZ - blksiz; 4232 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > left) { 4233 NFSBZERO(uiop->uio_iov->iov_base, left); 4234 dp->d_reclen += left; 4235 uiop->uio_iov->iov_base = 4236 (char *)uiop->uio_iov->iov_base + left; 4237 uiop->uio_iov->iov_len -= left; 4238 uiop->uio_resid -= left; 4239 uiop->uio_offset += left; 4240 blksiz = 0; 4241 } 4242 if (_GENERIC_DIRLEN(len) + NFSX_HYPER > 4243 uiop->uio_resid) 4244 bigenough = 0; 4245 if (bigenough) { 4246 struct iovec saviov; 4247 off_t savoff; 4248 ssize_t savresid; 4249 int savblksiz; 4250 4251 saviov.iov_base = uiop->uio_iov->iov_base; 4252 saviov.iov_len = uiop->uio_iov->iov_len; 4253 savoff = uiop->uio_offset; 4254 savresid = uiop->uio_resid; 4255 savblksiz = blksiz; 4256 4257 dp = (struct dirent *)uiop->uio_iov->iov_base; 4258 dp->d_pad0 = dp->d_pad1 = 0; 4259 dp->d_off = 0; 4260 dp->d_namlen = len; 4261 dp->d_reclen = _GENERIC_DIRLEN(len) + 4262 NFSX_HYPER; 4263 dp->d_type = DT_UNKNOWN; 4264 blksiz += dp->d_reclen; 4265 if (blksiz == DIRBLKSIZ) 4266 blksiz = 0; 4267 uiop->uio_resid -= DIRHDSIZ; 4268 uiop->uio_offset += DIRHDSIZ; 4269 uiop->uio_iov->iov_base = 4270 (char *)uiop->uio_iov->iov_base + DIRHDSIZ; 4271 uiop->uio_iov->iov_len -= DIRHDSIZ; 4272 cnp->cn_nameptr = uiop->uio_iov->iov_base; 4273 cnp->cn_namelen = len; 4274 NFSCNHASHZERO(cnp); 4275 cp = uiop->uio_iov->iov_base; 4276 error = nfsm_mbufuio(nd, uiop, len); 4277 if (error) 4278 goto nfsmout; 4279 /* Check for an invalid file name. */ 4280 if (nfscl_invalidfname( 4281 (nd->nd_flag & ND_NFSV4) != 0, cp, len)) { 4282 /* Skip over this entry. */ 4283 uiop->uio_iov->iov_base = 4284 saviov.iov_base; 4285 uiop->uio_iov->iov_len = 4286 saviov.iov_len; 4287 uiop->uio_offset = savoff; 4288 uiop->uio_resid = savresid; 4289 blksiz = savblksiz; 4290 validentry = false; 4291 } else { 4292 cp = uiop->uio_iov->iov_base; 4293 tlen -= len; 4294 NFSBZERO(cp, tlen); 4295 cp += tlen; /* points to cookie store */ 4296 tl2 = (u_int32_t *)cp; 4297 if (len == 2 && 4298 cnp->cn_nameptr[0] == '.' && 4299 cnp->cn_nameptr[1] == '.') 4300 isdotdot = 1; 4301 else 4302 isdotdot = 0; 4303 uiop->uio_iov->iov_base = 4304 (char *)uiop->uio_iov->iov_base + 4305 tlen + NFSX_HYPER; 4306 uiop->uio_iov->iov_len -= tlen + 4307 NFSX_HYPER; 4308 uiop->uio_resid -= tlen + NFSX_HYPER; 4309 uiop->uio_offset += (tlen + NFSX_HYPER); 4310 } 4311 } else { 4312 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 4313 if (error) 4314 goto nfsmout; 4315 } 4316 nfhp = NULL; 4317 if (nd->nd_flag & ND_NFSV3) { 4318 NFSM_DISSECT(tl, u_int32_t *, 3*NFSX_UNSIGNED); 4319 ncookie.lval[0] = *tl++; 4320 ncookie.lval[1] = *tl++; 4321 attrflag = fxdr_unsigned(int, *tl); 4322 if (attrflag) { 4323 error = nfsm_loadattr(nd, &nfsva); 4324 if (error) 4325 goto nfsmout; 4326 } 4327 NFSM_DISSECT(tl,u_int32_t *,NFSX_UNSIGNED); 4328 if (*tl) { 4329 error = nfsm_getfh(nd, &nfhp); 4330 if (error) 4331 goto nfsmout; 4332 } 4333 if (!attrflag && nfhp != NULL) { 4334 free(nfhp, M_NFSFH); 4335 nfhp = NULL; 4336 } 4337 } else { 4338 rderr = 0; 4339 nfsva.na_mntonfileno = 0xffffffff; 4340 error = nfsv4_loadattr(nd, NULL, &nfsva, &nfhp, 4341 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 4342 NULL, NULL, &rderr, NULL, p, cred); 4343 if (error) 4344 goto nfsmout; 4345 } 4346 4347 if (bigenough && validentry) { 4348 if (nd->nd_flag & ND_NFSV4) { 4349 if (rderr) { 4350 dp->d_fileno = 0; 4351 } else if (gotmnton) { 4352 if (nfsva.na_mntonfileno != 0xffffffff) 4353 dp->d_fileno = nfsva.na_mntonfileno; 4354 else 4355 dp->d_fileno = nfsva.na_fileid; 4356 } else if (nfsva.na_filesid[0] == 4357 dnp->n_vattr.na_filesid[0] && 4358 nfsva.na_filesid[1] == 4359 dnp->n_vattr.na_filesid[1]) { 4360 dp->d_fileno = nfsva.na_fileid; 4361 } else { 4362 do { 4363 fakefileno--; 4364 } while (fakefileno == 4365 nfsva.na_fileid); 4366 dp->d_fileno = fakefileno; 4367 } 4368 } else { 4369 dp->d_fileno = fileno; 4370 } 4371 *tl2++ = cookiep->nfsuquad[0] = cookie.lval[0] = 4372 ncookie.lval[0]; 4373 *tl2 = cookiep->nfsuquad[1] = cookie.lval[1] = 4374 ncookie.lval[1]; 4375 4376 if (nfhp != NULL) { 4377 attr_ok = true; 4378 if (NFSRV_CMPFH(nfhp->nfh_fh, nfhp->nfh_len, 4379 dnp->n_fhp->nfh_fh, dnp->n_fhp->nfh_len)) { 4380 VREF(vp); 4381 newvp = vp; 4382 unlocknewvp = 0; 4383 free(nfhp, M_NFSFH); 4384 np = dnp; 4385 } else if (isdotdot != 0) { 4386 /* 4387 * Skip doing a nfscl_nget() call for "..". 4388 * There's a race between acquiring the nfs 4389 * node here and lookups that look for the 4390 * directory being read (in the parent). 4391 * It would try to get a lock on ".." here, 4392 * owning the lock on the directory being 4393 * read. Lookup will hold the lock on ".." 4394 * and try to acquire the lock on the 4395 * directory being read. 4396 * If the directory is unlocked/relocked, 4397 * then there is a LOR with the buflock 4398 * vp is relocked. 4399 */ 4400 free(nfhp, M_NFSFH); 4401 } else { 4402 error = nfscl_nget(vp->v_mount, vp, 4403 nfhp, cnp, p, &np, LK_EXCLUSIVE); 4404 if (!error) { 4405 newvp = NFSTOV(np); 4406 unlocknewvp = 1; 4407 /* 4408 * If n_localmodtime >= time before RPC, 4409 * then a file modification operation, 4410 * such as VOP_SETATTR() of size, has 4411 * occurred while the Lookup RPC and 4412 * acquisition of the vnode happened. As 4413 * such, the attributes might be stale, 4414 * with possibly an incorrect size. 4415 */ 4416 NFSLOCKNODE(np); 4417 if (timespecisset( 4418 &np->n_localmodtime) && 4419 timespeccmp(&np->n_localmodtime, 4420 &ts, >=)) { 4421 NFSCL_DEBUG(4, "nfsrpc_readdirplus:" 4422 " localmod stale attributes\n"); 4423 attr_ok = false; 4424 } 4425 NFSUNLOCKNODE(np); 4426 } 4427 } 4428 nfhp = NULL; 4429 if (newvp != NULLVP) { 4430 if (attr_ok) 4431 error = nfscl_loadattrcache(&newvp, 4432 &nfsva, NULL, 0, 0); 4433 if (error) { 4434 if (unlocknewvp) 4435 vput(newvp); 4436 else 4437 vrele(newvp); 4438 goto nfsmout; 4439 } 4440 dp->d_type = 4441 vtonfs_dtype(np->n_vattr.na_type); 4442 ndp->ni_vp = newvp; 4443 NFSCNHASH(cnp, HASHINIT); 4444 if (cnp->cn_namelen <= NCHNAMLEN && 4445 ndp->ni_dvp != ndp->ni_vp && 4446 (newvp->v_type != VDIR || 4447 dctime.tv_sec != 0) && 4448 !named_dir) { 4449 cache_enter_time_flags(ndp->ni_dvp, 4450 ndp->ni_vp, cnp, 4451 &nfsva.na_ctime, 4452 newvp->v_type != VDIR ? NULL : 4453 &dctime, VFS_CACHE_DROPOLD); 4454 } 4455 if (unlocknewvp) 4456 vput(newvp); 4457 else 4458 vrele(newvp); 4459 newvp = NULLVP; 4460 } 4461 } 4462 } else if (nfhp != NULL) { 4463 free(nfhp, M_NFSFH); 4464 } 4465 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 4466 more_dirs = fxdr_unsigned(int, *tl); 4467 } 4468 /* 4469 * If at end of rpc data, get the eof boolean 4470 */ 4471 if (!more_dirs) { 4472 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 4473 eof = fxdr_unsigned(int, *tl); 4474 if (tryformoredirs) 4475 more_dirs = !eof; 4476 if (nd->nd_flag & ND_NFSV4) { 4477 error = nfscl_postop_attr(nd, nap, attrflagp); 4478 if (error) 4479 goto nfsmout; 4480 } 4481 } 4482 m_freem(nd->nd_mrep); 4483 nd->nd_mrep = NULL; 4484 } 4485 /* 4486 * Fill last record, iff any, out to a multiple of DIRBLKSIZ 4487 * by increasing d_reclen for the last record. 4488 */ 4489 if (blksiz > 0) { 4490 left = DIRBLKSIZ - blksiz; 4491 NFSBZERO(uiop->uio_iov->iov_base, left); 4492 dp->d_reclen += left; 4493 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 4494 left; 4495 uiop->uio_iov->iov_len -= left; 4496 uiop->uio_resid -= left; 4497 uiop->uio_offset += left; 4498 } 4499 4500 /* 4501 * If returning no data, assume end of file. 4502 * If not bigenough, return not end of file, since you aren't 4503 * returning all the data 4504 * Otherwise, return the eof flag from the server. 4505 */ 4506 if (eofp != NULL) { 4507 if (tresid == uiop->uio_resid) 4508 *eofp = 1; 4509 else if (!bigenough) 4510 *eofp = 0; 4511 else 4512 *eofp = eof; 4513 } 4514 4515 /* 4516 * Add extra empty records to any remaining DIRBLKSIZ chunks. 4517 */ 4518 while (uiop->uio_resid > 0 && uiop->uio_resid != tresid) { 4519 dp = (struct dirent *)uiop->uio_iov->iov_base; 4520 NFSBZERO(dp, DIRBLKSIZ); 4521 dp->d_type = DT_UNKNOWN; 4522 tl = (u_int32_t *)&dp->d_name[4]; 4523 *tl++ = cookie.lval[0]; 4524 *tl = cookie.lval[1]; 4525 dp->d_reclen = DIRBLKSIZ; 4526 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base + 4527 DIRBLKSIZ; 4528 uiop->uio_iov->iov_len -= DIRBLKSIZ; 4529 uiop->uio_resid -= DIRBLKSIZ; 4530 uiop->uio_offset += DIRBLKSIZ; 4531 } 4532 4533 nfsmout: 4534 if (nd->nd_mrep != NULL) 4535 m_freem(nd->nd_mrep); 4536 return (error); 4537 } 4538 4539 /* 4540 * Nfs commit rpc 4541 */ 4542 int 4543 nfsrpc_commit(vnode_t vp, u_quad_t offset, int cnt, struct ucred *cred, 4544 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp) 4545 { 4546 u_int32_t *tl; 4547 struct nfsrv_descript nfsd, *nd = &nfsd; 4548 nfsattrbit_t attrbits; 4549 int error; 4550 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 4551 4552 *attrflagp = 0; 4553 NFSCL_REQSTART(nd, NFSPROC_COMMIT, vp, cred); 4554 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 4555 txdr_hyper(offset, tl); 4556 tl += 2; 4557 *tl = txdr_unsigned(cnt); 4558 if (nd->nd_flag & ND_NFSV4) { 4559 /* 4560 * And do a Getattr op. 4561 */ 4562 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4563 *tl = txdr_unsigned(NFSV4OP_GETATTR); 4564 NFSGETATTR_ATTRBIT(&attrbits); 4565 (void) nfsrv_putattrbit(nd, &attrbits); 4566 } 4567 error = nfscl_request(nd, vp, p, cred); 4568 if (error) 4569 return (error); 4570 error = nfscl_wcc_data(nd, vp, nap, attrflagp, NULL, NULL); 4571 if (!error && !nd->nd_repstat) { 4572 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 4573 NFSLOCKMNT(nmp); 4574 if (NFSBCMP(nmp->nm_verf, tl, NFSX_VERF)) { 4575 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 4576 nd->nd_repstat = NFSERR_STALEWRITEVERF; 4577 } 4578 NFSUNLOCKMNT(nmp); 4579 if (nd->nd_flag & ND_NFSV4) 4580 error = nfscl_postop_attr(nd, nap, attrflagp); 4581 } 4582 nfsmout: 4583 if (!error && nd->nd_repstat) 4584 error = nd->nd_repstat; 4585 m_freem(nd->nd_mrep); 4586 return (error); 4587 } 4588 4589 /* 4590 * NFS byte range lock rpc. 4591 * (Mostly just calls one of the three lower level RPC routines.) 4592 */ 4593 int 4594 nfsrpc_advlock(vnode_t vp, off_t size, int op, struct flock *fl, 4595 int reclaim, struct ucred *cred, NFSPROC_T *p, void *id, int flags) 4596 { 4597 struct nfscllockowner *lp; 4598 struct nfsclclient *clp; 4599 struct nfsfh *nfhp; 4600 struct nfsrv_descript nfsd, *nd = &nfsd; 4601 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 4602 u_int64_t off, len; 4603 off_t start, end; 4604 u_int32_t clidrev = 0; 4605 int error = 0, newone = 0, expireret = 0, retrycnt, donelocally; 4606 int callcnt, dorpc; 4607 4608 /* 4609 * Convert the flock structure into a start and end and do POSIX 4610 * bounds checking. 4611 */ 4612 switch (fl->l_whence) { 4613 case SEEK_SET: 4614 case SEEK_CUR: 4615 /* 4616 * Caller is responsible for adding any necessary offset 4617 * when SEEK_CUR is used. 4618 */ 4619 start = fl->l_start; 4620 off = fl->l_start; 4621 break; 4622 case SEEK_END: 4623 start = size + fl->l_start; 4624 off = size + fl->l_start; 4625 break; 4626 default: 4627 return (EINVAL); 4628 } 4629 if (start < 0) 4630 return (EINVAL); 4631 if (fl->l_len != 0) { 4632 end = start + fl->l_len - 1; 4633 if (end < start) 4634 return (EINVAL); 4635 } 4636 4637 len = fl->l_len; 4638 if (len == 0) 4639 len = NFS64BITSSET; 4640 retrycnt = 0; 4641 do { 4642 nd->nd_repstat = 0; 4643 if (op == F_GETLK) { 4644 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp); 4645 if (error) 4646 return (error); 4647 error = nfscl_lockt(vp, clp, off, len, fl, p, id, flags); 4648 if (!error) { 4649 clidrev = clp->nfsc_clientidrev; 4650 error = nfsrpc_lockt(nd, vp, clp, off, len, fl, cred, 4651 p, id, flags); 4652 } else if (error == -1) { 4653 error = 0; 4654 } 4655 nfscl_clientrelease(clp); 4656 } else if (op == F_UNLCK && fl->l_type == F_UNLCK) { 4657 /* 4658 * We must loop around for all lockowner cases. 4659 */ 4660 callcnt = 0; 4661 error = nfscl_getcl(vp->v_mount, cred, p, false, true, &clp); 4662 if (error) 4663 return (error); 4664 do { 4665 error = nfscl_relbytelock(vp, off, len, cred, p, callcnt, 4666 clp, id, flags, &lp, &dorpc); 4667 /* 4668 * If it returns a NULL lp, we're done. 4669 */ 4670 if (lp == NULL) { 4671 if (callcnt == 0) 4672 nfscl_clientrelease(clp); 4673 else 4674 nfscl_releasealllocks(clp, vp, p, id, flags); 4675 return (error); 4676 } 4677 if (nmp->nm_clp != NULL) 4678 clidrev = nmp->nm_clp->nfsc_clientidrev; 4679 else 4680 clidrev = 0; 4681 /* 4682 * If the server doesn't support Posix lock semantics, 4683 * only allow locks on the entire file, since it won't 4684 * handle overlapping byte ranges. 4685 * There might still be a problem when a lock 4686 * upgrade/downgrade (read<->write) occurs, since the 4687 * server "might" expect an unlock first? 4688 */ 4689 if (dorpc && (lp->nfsl_open->nfso_posixlock || 4690 (off == 0 && len == NFS64BITSSET))) { 4691 /* 4692 * Since the lock records will go away, we must 4693 * wait for grace and delay here. 4694 */ 4695 do { 4696 error = nfsrpc_locku(nd, nmp, lp, off, len, 4697 NFSV4LOCKT_READ, cred, p, 0); 4698 if ((nd->nd_repstat == NFSERR_GRACE || 4699 nd->nd_repstat == NFSERR_DELAY) && 4700 error == 0) 4701 (void) nfs_catnap(PZERO, (int)nd->nd_repstat, 4702 "nfs_advlock"); 4703 } while ((nd->nd_repstat == NFSERR_GRACE || 4704 nd->nd_repstat == NFSERR_DELAY) && error == 0); 4705 } 4706 callcnt++; 4707 } while (error == 0 && nd->nd_repstat == 0); 4708 nfscl_releasealllocks(clp, vp, p, id, flags); 4709 } else if (op == F_SETLK) { 4710 error = nfscl_getbytelock(vp, off, len, fl->l_type, cred, p, 4711 NULL, 0, id, flags, NULL, NULL, &lp, &newone, &donelocally); 4712 if (error || donelocally) { 4713 return (error); 4714 } 4715 if (nmp->nm_clp != NULL) 4716 clidrev = nmp->nm_clp->nfsc_clientidrev; 4717 else 4718 clidrev = 0; 4719 nfhp = VTONFS(vp)->n_fhp; 4720 if (!lp->nfsl_open->nfso_posixlock && 4721 (off != 0 || len != NFS64BITSSET)) { 4722 error = EINVAL; 4723 } else { 4724 error = nfsrpc_lock(nd, nmp, vp, nfhp->nfh_fh, 4725 nfhp->nfh_len, lp, newone, reclaim, off, 4726 len, fl->l_type, cred, p, 0); 4727 } 4728 if (!error) 4729 error = nd->nd_repstat; 4730 nfscl_lockrelease(lp, error, newone); 4731 } else { 4732 error = EINVAL; 4733 } 4734 if (!error) 4735 error = nd->nd_repstat; 4736 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 4737 error == NFSERR_STALEDONTRECOVER || 4738 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 4739 error == NFSERR_BADSESSION) { 4740 (void) nfs_catnap(PZERO, error, "nfs_advlock"); 4741 } else if ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) 4742 && clidrev != 0) { 4743 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 4744 retrycnt++; 4745 } 4746 } while (error == NFSERR_GRACE || 4747 error == NFSERR_STALECLIENTID || error == NFSERR_DELAY || 4748 error == NFSERR_STALEDONTRECOVER || error == NFSERR_STALESTATEID || 4749 error == NFSERR_BADSESSION || 4750 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 4751 expireret == 0 && clidrev != 0 && retrycnt < 4)); 4752 if (error && retrycnt >= 4) 4753 error = EIO; 4754 return (error); 4755 } 4756 4757 /* 4758 * The lower level routine for the LockT case. 4759 */ 4760 int 4761 nfsrpc_lockt(struct nfsrv_descript *nd, vnode_t vp, 4762 struct nfsclclient *clp, u_int64_t off, u_int64_t len, struct flock *fl, 4763 struct ucred *cred, NFSPROC_T *p, void *id, int flags) 4764 { 4765 u_int32_t *tl; 4766 int error, type, size; 4767 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4768 struct nfsnode *np; 4769 struct nfsmount *nmp; 4770 struct nfsclsession *tsep; 4771 4772 nmp = VFSTONFS(vp->v_mount); 4773 NFSCL_REQSTART(nd, NFSPROC_LOCKT, vp, cred); 4774 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 4775 if (fl->l_type == F_RDLCK) 4776 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 4777 else 4778 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 4779 txdr_hyper(off, tl); 4780 tl += 2; 4781 txdr_hyper(len, tl); 4782 tl += 2; 4783 tsep = nfsmnt_mdssession(nmp); 4784 *tl++ = tsep->nfsess_clientid.lval[0]; 4785 *tl = tsep->nfsess_clientid.lval[1]; 4786 nfscl_filllockowner(id, own, flags); 4787 np = VTONFS(vp); 4788 NFSBCOPY(np->n_fhp->nfh_fh, &own[NFSV4CL_LOCKNAMELEN], 4789 np->n_fhp->nfh_len); 4790 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + np->n_fhp->nfh_len); 4791 error = nfscl_request(nd, vp, p, cred); 4792 if (error) 4793 return (error); 4794 if (nd->nd_repstat == 0) { 4795 fl->l_type = F_UNLCK; 4796 } else if (nd->nd_repstat == NFSERR_DENIED) { 4797 nd->nd_repstat = 0; 4798 fl->l_whence = SEEK_SET; 4799 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 4800 fl->l_start = fxdr_hyper(tl); 4801 tl += 2; 4802 len = fxdr_hyper(tl); 4803 tl += 2; 4804 if (len == NFS64BITSSET) 4805 fl->l_len = 0; 4806 else 4807 fl->l_len = len; 4808 type = fxdr_unsigned(int, *tl++); 4809 if (type == NFSV4LOCKT_WRITE) 4810 fl->l_type = F_WRLCK; 4811 else 4812 fl->l_type = F_RDLCK; 4813 /* 4814 * XXX For now, I have no idea what to do with the 4815 * conflicting lock_owner, so I'll just set the pid == 0 4816 * and skip over the lock_owner. 4817 */ 4818 fl->l_pid = (pid_t)0; 4819 tl += 2; 4820 size = fxdr_unsigned(int, *tl); 4821 if (size < 0 || size > NFSV4_OPAQUELIMIT) 4822 error = EBADRPC; 4823 if (!error) 4824 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 4825 } else if (nd->nd_repstat == NFSERR_STALECLIENTID) 4826 nfscl_initiate_recovery(clp); 4827 nfsmout: 4828 m_freem(nd->nd_mrep); 4829 return (error); 4830 } 4831 4832 /* 4833 * Lower level function that performs the LockU RPC. 4834 */ 4835 static int 4836 nfsrpc_locku(struct nfsrv_descript *nd, struct nfsmount *nmp, 4837 struct nfscllockowner *lp, u_int64_t off, u_int64_t len, 4838 u_int32_t type, struct ucred *cred, NFSPROC_T *p, int syscred) 4839 { 4840 u_int32_t *tl; 4841 int error; 4842 4843 nfscl_reqstart(nd, NFSPROC_LOCKU, nmp, lp->nfsl_open->nfso_fh, 4844 lp->nfsl_open->nfso_fhlen, NULL, NULL, 0, 0, cred); 4845 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 6 * NFSX_UNSIGNED); 4846 *tl++ = txdr_unsigned(type); 4847 *tl = txdr_unsigned(lp->nfsl_seqid); 4848 if (nfstest_outofseq && 4849 (arc4random() % nfstest_outofseq) == 0) 4850 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 4851 tl++; 4852 if (NFSHASNFSV4N(nmp)) 4853 *tl++ = 0; 4854 else 4855 *tl++ = lp->nfsl_stateid.seqid; 4856 *tl++ = lp->nfsl_stateid.other[0]; 4857 *tl++ = lp->nfsl_stateid.other[1]; 4858 *tl++ = lp->nfsl_stateid.other[2]; 4859 txdr_hyper(off, tl); 4860 tl += 2; 4861 txdr_hyper(len, tl); 4862 if (syscred) 4863 nd->nd_flag |= ND_USEGSSNAME; 4864 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4865 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4866 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 4867 if (error) 4868 return (error); 4869 if (nd->nd_repstat == 0) { 4870 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 4871 lp->nfsl_stateid.seqid = *tl++; 4872 lp->nfsl_stateid.other[0] = *tl++; 4873 lp->nfsl_stateid.other[1] = *tl++; 4874 lp->nfsl_stateid.other[2] = *tl; 4875 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 4876 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 4877 nfsmout: 4878 m_freem(nd->nd_mrep); 4879 return (error); 4880 } 4881 4882 /* 4883 * The actual Lock RPC. 4884 */ 4885 int 4886 nfsrpc_lock(struct nfsrv_descript *nd, struct nfsmount *nmp, vnode_t vp, 4887 u_int8_t *nfhp, int fhlen, struct nfscllockowner *lp, int newone, 4888 int reclaim, u_int64_t off, u_int64_t len, short type, struct ucred *cred, 4889 NFSPROC_T *p, int syscred) 4890 { 4891 u_int32_t *tl; 4892 int error, size; 4893 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 4894 struct nfsclsession *tsep; 4895 4896 nfscl_reqstart(nd, NFSPROC_LOCK, nmp, nfhp, fhlen, NULL, NULL, 0, 0, 4897 cred); 4898 NFSM_BUILD(tl, u_int32_t *, 7 * NFSX_UNSIGNED); 4899 if (type == F_RDLCK) 4900 *tl++ = txdr_unsigned(NFSV4LOCKT_READ); 4901 else 4902 *tl++ = txdr_unsigned(NFSV4LOCKT_WRITE); 4903 *tl++ = txdr_unsigned(reclaim); 4904 txdr_hyper(off, tl); 4905 tl += 2; 4906 txdr_hyper(len, tl); 4907 tl += 2; 4908 if (newone) { 4909 *tl = newnfs_true; 4910 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + 4911 2 * NFSX_UNSIGNED + NFSX_HYPER); 4912 *tl++ = txdr_unsigned(lp->nfsl_open->nfso_own->nfsow_seqid); 4913 if (NFSHASNFSV4N(nmp)) 4914 *tl++ = 0; 4915 else 4916 *tl++ = lp->nfsl_open->nfso_stateid.seqid; 4917 *tl++ = lp->nfsl_open->nfso_stateid.other[0]; 4918 *tl++ = lp->nfsl_open->nfso_stateid.other[1]; 4919 *tl++ = lp->nfsl_open->nfso_stateid.other[2]; 4920 *tl++ = txdr_unsigned(lp->nfsl_seqid); 4921 tsep = nfsmnt_mdssession(nmp); 4922 *tl++ = tsep->nfsess_clientid.lval[0]; 4923 *tl = tsep->nfsess_clientid.lval[1]; 4924 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN); 4925 NFSBCOPY(nfhp, &own[NFSV4CL_LOCKNAMELEN], fhlen); 4926 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen); 4927 } else { 4928 *tl = newnfs_false; 4929 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID + NFSX_UNSIGNED); 4930 if (NFSHASNFSV4N(nmp)) 4931 *tl++ = 0; 4932 else 4933 *tl++ = lp->nfsl_stateid.seqid; 4934 *tl++ = lp->nfsl_stateid.other[0]; 4935 *tl++ = lp->nfsl_stateid.other[1]; 4936 *tl++ = lp->nfsl_stateid.other[2]; 4937 *tl = txdr_unsigned(lp->nfsl_seqid); 4938 if (nfstest_outofseq && 4939 (arc4random() % nfstest_outofseq) == 0) 4940 *tl = txdr_unsigned(lp->nfsl_seqid + 1); 4941 } 4942 if (syscred) 4943 nd->nd_flag |= ND_USEGSSNAME; 4944 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 4945 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4946 if (error) 4947 return (error); 4948 if (newone) 4949 NFSCL_INCRSEQID(lp->nfsl_open->nfso_own->nfsow_seqid, nd); 4950 NFSCL_INCRSEQID(lp->nfsl_seqid, nd); 4951 if (nd->nd_repstat == 0) { 4952 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID); 4953 lp->nfsl_stateid.seqid = *tl++; 4954 lp->nfsl_stateid.other[0] = *tl++; 4955 lp->nfsl_stateid.other[1] = *tl++; 4956 lp->nfsl_stateid.other[2] = *tl; 4957 } else if (nd->nd_repstat == NFSERR_DENIED) { 4958 NFSM_DISSECT(tl, u_int32_t *, 8 * NFSX_UNSIGNED); 4959 size = fxdr_unsigned(int, *(tl + 7)); 4960 if (size < 0 || size > NFSV4_OPAQUELIMIT) 4961 error = EBADRPC; 4962 if (!error) 4963 error = nfsm_advance(nd, NFSM_RNDUP(size), -1); 4964 } else if (nd->nd_repstat == NFSERR_STALESTATEID) 4965 nfscl_initiate_recovery(lp->nfsl_open->nfso_own->nfsow_clp); 4966 nfsmout: 4967 m_freem(nd->nd_mrep); 4968 return (error); 4969 } 4970 4971 /* 4972 * nfs statfs rpc 4973 * (always called with the vp for the mount point) 4974 */ 4975 int 4976 nfsrpc_statfs(vnode_t vp, struct nfsstatfs *sbp, struct nfsfsinfo *fsp, 4977 uint32_t *leasep, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, 4978 int *attrflagp) 4979 { 4980 u_int32_t *tl = NULL; 4981 struct nfsrv_descript nfsd, *nd = &nfsd; 4982 struct nfsmount *nmp; 4983 nfsattrbit_t attrbits; 4984 int error; 4985 4986 *attrflagp = 0; 4987 nmp = VFSTONFS(vp->v_mount); 4988 if (NFSHASNFSV4(nmp)) { 4989 /* 4990 * For V4, you actually do a getattr. 4991 */ 4992 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred); 4993 if (leasep != NULL) 4994 NFSROOTFS_GETATTRBIT(&attrbits); 4995 else 4996 NFSSTATFS_GETATTRBIT(&attrbits); 4997 (void) nfsrv_putattrbit(nd, &attrbits); 4998 nd->nd_flag |= ND_USEGSSNAME; 4999 error = nfscl_request(nd, vp, p, cred); 5000 if (error) 5001 return (error); 5002 if (nd->nd_repstat == 0) { 5003 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 5004 NULL, NULL, sbp, fsp, NULL, 0, NULL, leasep, NULL, 5005 NULL, p, cred); 5006 if (!error) { 5007 nmp->nm_fsid[0] = nap->na_filesid[0]; 5008 nmp->nm_fsid[1] = nap->na_filesid[1]; 5009 NFSSETHASSETFSID(nmp); 5010 *attrflagp = 1; 5011 } 5012 } else { 5013 error = nd->nd_repstat; 5014 } 5015 if (error) 5016 goto nfsmout; 5017 } else { 5018 NFSCL_REQSTART(nd, NFSPROC_FSSTAT, vp, NULL); 5019 error = nfscl_request(nd, vp, p, cred); 5020 if (error) 5021 return (error); 5022 if (nd->nd_flag & ND_NFSV3) { 5023 error = nfscl_postop_attr(nd, nap, attrflagp); 5024 if (error) 5025 goto nfsmout; 5026 } 5027 if (nd->nd_repstat) { 5028 error = nd->nd_repstat; 5029 goto nfsmout; 5030 } 5031 NFSM_DISSECT(tl, u_int32_t *, 5032 NFSX_STATFS(nd->nd_flag & ND_NFSV3)); 5033 } 5034 if (NFSHASNFSV3(nmp)) { 5035 sbp->sf_tbytes = fxdr_hyper(tl); tl += 2; 5036 sbp->sf_fbytes = fxdr_hyper(tl); tl += 2; 5037 sbp->sf_abytes = fxdr_hyper(tl); tl += 2; 5038 sbp->sf_tfiles = fxdr_hyper(tl); tl += 2; 5039 sbp->sf_ffiles = fxdr_hyper(tl); tl += 2; 5040 sbp->sf_afiles = fxdr_hyper(tl); tl += 2; 5041 sbp->sf_invarsec = fxdr_unsigned(u_int32_t, *tl); 5042 } else if (NFSHASNFSV4(nmp) == 0) { 5043 sbp->sf_tsize = fxdr_unsigned(u_int32_t, *tl++); 5044 sbp->sf_bsize = fxdr_unsigned(u_int32_t, *tl++); 5045 sbp->sf_blocks = fxdr_unsigned(u_int32_t, *tl++); 5046 sbp->sf_bfree = fxdr_unsigned(u_int32_t, *tl++); 5047 sbp->sf_bavail = fxdr_unsigned(u_int32_t, *tl); 5048 } 5049 nfsmout: 5050 m_freem(nd->nd_mrep); 5051 return (error); 5052 } 5053 5054 /* 5055 * nfs pathconf rpc 5056 */ 5057 int 5058 nfsrpc_pathconf(vnode_t vp, struct nfsv3_pathconf *pc, bool *has_namedattrp, 5059 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp) 5060 { 5061 struct nfsrv_descript nfsd, *nd = &nfsd; 5062 struct nfsmount *nmp; 5063 u_int32_t *tl; 5064 nfsattrbit_t attrbits; 5065 int error; 5066 struct nfsnode *np; 5067 5068 *has_namedattrp = false; 5069 *attrflagp = 0; 5070 nmp = VFSTONFS(vp->v_mount); 5071 if (NFSHASNFSV4(nmp)) { 5072 np = VTONFS(vp); 5073 if ((nmp->nm_privflag & NFSMNTP_FAKEROOTFH) != 0 && 5074 nmp->nm_fhsize == 0) { 5075 /* Attempt to get the actual root file handle. */ 5076 error = nfsrpc_getdirpath(nmp, NFSMNT_DIRPATH(nmp), 5077 cred, p); 5078 if (error != 0) 5079 return (EACCES); 5080 if (np->n_fhp->nfh_len == NFSX_FHMAX + 1) 5081 nfscl_statfs(vp, cred, p); 5082 } 5083 /* 5084 * For V4, you actually do a getattr. 5085 */ 5086 NFSCL_REQSTART(nd, NFSPROC_GETATTR, vp, cred); 5087 NFSPATHCONF_GETATTRBIT(&attrbits); 5088 (void) nfsrv_putattrbit(nd, &attrbits); 5089 nd->nd_flag |= ND_USEGSSNAME; 5090 error = nfscl_request(nd, vp, p, cred); 5091 if (error) 5092 return (error); 5093 if (nd->nd_repstat == 0) { 5094 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 5095 pc, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, 5096 has_namedattrp, p, cred); 5097 if (!error) 5098 *attrflagp = 1; 5099 } else { 5100 error = nd->nd_repstat; 5101 } 5102 } else { 5103 NFSCL_REQSTART(nd, NFSPROC_PATHCONF, vp, NULL); 5104 error = nfscl_request(nd, vp, p, cred); 5105 if (error) 5106 return (error); 5107 error = nfscl_postop_attr(nd, nap, attrflagp); 5108 if (nd->nd_repstat && !error) 5109 error = nd->nd_repstat; 5110 if (!error) { 5111 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3PATHCONF); 5112 pc->pc_linkmax = fxdr_unsigned(u_int32_t, *tl++); 5113 pc->pc_namemax = fxdr_unsigned(u_int32_t, *tl++); 5114 pc->pc_notrunc = fxdr_unsigned(u_int32_t, *tl++); 5115 pc->pc_chownrestricted = 5116 fxdr_unsigned(u_int32_t, *tl++); 5117 pc->pc_caseinsensitive = 5118 fxdr_unsigned(u_int32_t, *tl++); 5119 pc->pc_casepreserving = fxdr_unsigned(u_int32_t, *tl); 5120 } 5121 } 5122 nfsmout: 5123 m_freem(nd->nd_mrep); 5124 return (error); 5125 } 5126 5127 /* 5128 * nfs version 3 fsinfo rpc call 5129 */ 5130 int 5131 nfsrpc_fsinfo(vnode_t vp, struct nfsfsinfo *fsp, struct ucred *cred, 5132 NFSPROC_T *p, struct nfsvattr *nap, int *attrflagp) 5133 { 5134 u_int32_t *tl; 5135 struct nfsrv_descript nfsd, *nd = &nfsd; 5136 int error; 5137 5138 *attrflagp = 0; 5139 NFSCL_REQSTART(nd, NFSPROC_FSINFO, vp, NULL); 5140 error = nfscl_request(nd, vp, p, cred); 5141 if (error) 5142 return (error); 5143 error = nfscl_postop_attr(nd, nap, attrflagp); 5144 if (nd->nd_repstat && !error) 5145 error = nd->nd_repstat; 5146 if (!error) { 5147 NFSM_DISSECT(tl, u_int32_t *, NFSX_V3FSINFO); 5148 fsp->fs_rtmax = fxdr_unsigned(u_int32_t, *tl++); 5149 fsp->fs_rtpref = fxdr_unsigned(u_int32_t, *tl++); 5150 fsp->fs_rtmult = fxdr_unsigned(u_int32_t, *tl++); 5151 fsp->fs_wtmax = fxdr_unsigned(u_int32_t, *tl++); 5152 fsp->fs_wtpref = fxdr_unsigned(u_int32_t, *tl++); 5153 fsp->fs_wtmult = fxdr_unsigned(u_int32_t, *tl++); 5154 fsp->fs_dtpref = fxdr_unsigned(u_int32_t, *tl++); 5155 fsp->fs_maxfilesize = fxdr_hyper(tl); 5156 tl += 2; 5157 fxdr_nfsv3time(tl, &fsp->fs_timedelta); 5158 tl += 2; 5159 fsp->fs_properties = fxdr_unsigned(u_int32_t, *tl); 5160 } 5161 nfsmout: 5162 m_freem(nd->nd_mrep); 5163 return (error); 5164 } 5165 5166 /* 5167 * This function performs the Renew RPC. 5168 */ 5169 int 5170 nfsrpc_renew(struct nfsclclient *clp, struct nfsclds *dsp, struct ucred *cred, 5171 NFSPROC_T *p) 5172 { 5173 u_int32_t *tl; 5174 struct nfsrv_descript nfsd; 5175 struct nfsrv_descript *nd = &nfsd; 5176 struct nfsmount *nmp; 5177 int error; 5178 struct nfssockreq *nrp; 5179 struct nfsclsession *tsep; 5180 5181 nmp = clp->nfsc_nmp; 5182 if (nmp == NULL) 5183 return (0); 5184 if (dsp == NULL) 5185 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, NULL, 0, 5186 0, cred); 5187 else 5188 nfscl_reqstart(nd, NFSPROC_RENEW, nmp, NULL, 0, NULL, 5189 &dsp->nfsclds_sess, 0, 0, NULL); 5190 if (!NFSHASNFSV4N(nmp)) { 5191 /* NFSv4.1 just uses a Sequence Op and not a Renew. */ 5192 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 5193 tsep = nfsmnt_mdssession(nmp); 5194 *tl++ = tsep->nfsess_clientid.lval[0]; 5195 *tl = tsep->nfsess_clientid.lval[1]; 5196 } 5197 nrp = NULL; 5198 if (dsp != NULL) 5199 nrp = dsp->nfsclds_sockp; 5200 if (nrp == NULL) 5201 /* If NULL, use the MDS socket. */ 5202 nrp = &nmp->nm_sockreq; 5203 nd->nd_flag |= ND_USEGSSNAME; 5204 if (dsp == NULL) 5205 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 5206 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5207 else { 5208 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 5209 NFS_PROG, NFS_VER4, NULL, 1, NULL, &dsp->nfsclds_sess); 5210 if (error == ENXIO) 5211 nfscl_cancelreqs(dsp); 5212 } 5213 if (error) 5214 return (error); 5215 error = nd->nd_repstat; 5216 m_freem(nd->nd_mrep); 5217 return (error); 5218 } 5219 5220 /* 5221 * This function performs the Releaselockowner RPC. 5222 */ 5223 int 5224 nfsrpc_rellockown(struct nfsmount *nmp, struct nfscllockowner *lp, 5225 uint8_t *fh, int fhlen, struct ucred *cred, NFSPROC_T *p) 5226 { 5227 struct nfsrv_descript nfsd, *nd = &nfsd; 5228 u_int32_t *tl; 5229 int error; 5230 uint8_t own[NFSV4CL_LOCKNAMELEN + NFSX_V4FHMAX]; 5231 struct nfsclsession *tsep; 5232 5233 if (NFSHASNFSV4N(nmp)) { 5234 /* For NFSv4.1, do a FreeStateID. */ 5235 nfscl_reqstart(nd, NFSPROC_FREESTATEID, nmp, NULL, 0, NULL, 5236 NULL, 0, 0, cred); 5237 nfsm_stateidtom(nd, &lp->nfsl_stateid, NFSSTATEID_PUTSTATEID); 5238 } else { 5239 nfscl_reqstart(nd, NFSPROC_RELEASELCKOWN, nmp, NULL, 0, NULL, 5240 NULL, 0, 0, NULL); 5241 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 5242 tsep = nfsmnt_mdssession(nmp); 5243 *tl++ = tsep->nfsess_clientid.lval[0]; 5244 *tl = tsep->nfsess_clientid.lval[1]; 5245 NFSBCOPY(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN); 5246 NFSBCOPY(fh, &own[NFSV4CL_LOCKNAMELEN], fhlen); 5247 (void)nfsm_strtom(nd, own, NFSV4CL_LOCKNAMELEN + fhlen); 5248 } 5249 nd->nd_flag |= ND_USEGSSNAME; 5250 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5251 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5252 if (error) 5253 return (error); 5254 error = nd->nd_repstat; 5255 m_freem(nd->nd_mrep); 5256 return (error); 5257 } 5258 5259 /* 5260 * This function performs the Compound to get the mount pt FH. 5261 */ 5262 int 5263 nfsrpc_getdirpath(struct nfsmount *nmp, u_char *dirpath, struct ucred *cred, 5264 NFSPROC_T *p) 5265 { 5266 u_int32_t *tl; 5267 struct nfsrv_descript nfsd; 5268 struct nfsrv_descript *nd = &nfsd; 5269 u_char *cp, *cp2, *fhp; 5270 int error, cnt, len, setnil; 5271 u_int32_t *opcntp; 5272 5273 nfscl_reqstart(nd, NFSPROC_PUTROOTFH, nmp, NULL, 0, &opcntp, NULL, 0, 5274 0, NULL); 5275 cp = dirpath; 5276 cnt = 0; 5277 do { 5278 setnil = 0; 5279 while (*cp == '/') 5280 cp++; 5281 cp2 = cp; 5282 while (*cp2 != '\0' && *cp2 != '/') 5283 cp2++; 5284 if (*cp2 == '/') { 5285 setnil = 1; 5286 *cp2 = '\0'; 5287 } 5288 if (cp2 != cp) { 5289 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 5290 *tl = txdr_unsigned(NFSV4OP_LOOKUP); 5291 nfsm_strtom(nd, cp, strlen(cp)); 5292 cnt++; 5293 } 5294 if (setnil) 5295 *cp2++ = '/'; 5296 cp = cp2; 5297 } while (*cp != '\0'); 5298 if (NFSHASNFSV4N(nmp)) 5299 /* Has a Sequence Op done by nfscl_reqstart(). */ 5300 *opcntp = txdr_unsigned(3 + cnt); 5301 else 5302 *opcntp = txdr_unsigned(2 + cnt); 5303 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 5304 *tl = txdr_unsigned(NFSV4OP_GETFH); 5305 nd->nd_flag |= ND_USEGSSNAME; 5306 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5307 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5308 if (error) 5309 return (error); 5310 if (nd->nd_repstat == 0) { 5311 NFSM_DISSECT(tl, u_int32_t *, (3 + 2 * cnt) * NFSX_UNSIGNED); 5312 tl += (2 + 2 * cnt); 5313 if ((len = fxdr_unsigned(int, *tl)) <= 0 || 5314 len > NFSX_FHMAX) { 5315 nd->nd_repstat = NFSERR_BADXDR; 5316 } else { 5317 fhp = malloc(len + 1, M_TEMP, M_WAITOK); 5318 nd->nd_repstat = nfsrv_mtostr(nd, fhp, len); 5319 if (nd->nd_repstat == 0) { 5320 NFSLOCKMNT(nmp); 5321 if (nmp->nm_fhsize == 0) { 5322 NFSBCOPY(fhp, nmp->nm_fh, len); 5323 nmp->nm_fhsize = len; 5324 } 5325 NFSUNLOCKMNT(nmp); 5326 } 5327 free(fhp, M_TEMP); 5328 } 5329 } 5330 error = nd->nd_repstat; 5331 nfsmout: 5332 m_freem(nd->nd_mrep); 5333 return (error); 5334 } 5335 5336 /* 5337 * This function performs the Delegreturn RPC. 5338 */ 5339 int 5340 nfsrpc_delegreturn(struct nfscldeleg *dp, struct ucred *cred, 5341 struct nfsmount *nmp, NFSPROC_T *p, int syscred) 5342 { 5343 u_int32_t *tl; 5344 struct nfsrv_descript nfsd; 5345 struct nfsrv_descript *nd = &nfsd; 5346 int error; 5347 5348 nfscl_reqstart(nd, NFSPROC_DELEGRETURN, nmp, dp->nfsdl_fh, 5349 dp->nfsdl_fhlen, NULL, NULL, 0, 0, cred); 5350 NFSM_BUILD(tl, u_int32_t *, NFSX_STATEID); 5351 if (NFSHASNFSV4N(nmp)) 5352 *tl++ = 0; 5353 else 5354 *tl++ = dp->nfsdl_stateid.seqid; 5355 *tl++ = dp->nfsdl_stateid.other[0]; 5356 *tl++ = dp->nfsdl_stateid.other[1]; 5357 *tl = dp->nfsdl_stateid.other[2]; 5358 if (syscred) 5359 nd->nd_flag |= ND_USEGSSNAME; 5360 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5361 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5362 if (error) 5363 return (error); 5364 error = nd->nd_repstat; 5365 m_freem(nd->nd_mrep); 5366 return (error); 5367 } 5368 5369 /* 5370 * nfs getacl call. 5371 */ 5372 int 5373 nfsrpc_getacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, struct acl *aclp) 5374 { 5375 struct nfsrv_descript nfsd, *nd = &nfsd; 5376 int error; 5377 nfsattrbit_t attrbits; 5378 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 5379 5380 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 5381 return (EOPNOTSUPP); 5382 NFSCL_REQSTART(nd, NFSPROC_GETACL, vp, cred); 5383 NFSZERO_ATTRBIT(&attrbits); 5384 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 5385 (void) nfsrv_putattrbit(nd, &attrbits); 5386 error = nfscl_request(nd, vp, p, cred); 5387 if (error) 5388 return (error); 5389 if (!nd->nd_repstat) 5390 error = nfsv4_loadattr(nd, vp, NULL, NULL, NULL, 0, NULL, 5391 NULL, NULL, NULL, aclp, 0, NULL, NULL, NULL, NULL, p, cred); 5392 else 5393 error = nd->nd_repstat; 5394 m_freem(nd->nd_mrep); 5395 return (error); 5396 } 5397 5398 /* 5399 * nfs setacl call. 5400 */ 5401 int 5402 nfsrpc_setacl(vnode_t vp, struct ucred *cred, NFSPROC_T *p, struct acl *aclp) 5403 { 5404 int error; 5405 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 5406 5407 if (nfsrv_useacl == 0 || !NFSHASNFSV4(nmp)) 5408 return (EOPNOTSUPP); 5409 error = nfsrpc_setattr(vp, NULL, aclp, cred, p, NULL, NULL); 5410 return (error); 5411 } 5412 5413 /* 5414 * nfs setacl call. 5415 */ 5416 static int 5417 nfsrpc_setaclrpc(vnode_t vp, struct ucred *cred, NFSPROC_T *p, 5418 struct acl *aclp, nfsv4stateid_t *stateidp) 5419 { 5420 struct nfsrv_descript nfsd, *nd = &nfsd; 5421 int error; 5422 nfsattrbit_t attrbits; 5423 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 5424 5425 if (!NFSHASNFSV4(nmp)) 5426 return (EOPNOTSUPP); 5427 NFSCL_REQSTART(nd, NFSPROC_SETACL, vp, cred); 5428 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 5429 NFSZERO_ATTRBIT(&attrbits); 5430 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 5431 (void) nfsv4_fillattr(nd, vp->v_mount, vp, aclp, NULL, NULL, 0, 5432 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL); 5433 error = nfscl_request(nd, vp, p, cred); 5434 if (error) 5435 return (error); 5436 /* Don't care about the pre/postop attributes */ 5437 m_freem(nd->nd_mrep); 5438 return (nd->nd_repstat); 5439 } 5440 5441 /* 5442 * Do the NFSv4.1 Exchange ID. 5443 */ 5444 int 5445 nfsrpc_exchangeid(struct nfsmount *nmp, struct nfsclclient *clp, 5446 struct nfssockreq *nrp, int minorvers, uint32_t exchflags, 5447 struct nfsclds **dspp, struct ucred *cred, NFSPROC_T *p) 5448 { 5449 uint32_t *tl, v41flags; 5450 struct nfsrv_descript nfsd; 5451 struct nfsrv_descript *nd = &nfsd; 5452 struct nfsclds *dsp; 5453 struct timespec verstime; 5454 int error, len; 5455 5456 *dspp = NULL; 5457 if (minorvers == 0) 5458 minorvers = nmp->nm_minorvers; 5459 nfscl_reqstart(nd, NFSPROC_EXCHANGEID, nmp, NULL, 0, NULL, NULL, 5460 NFS_VER4, minorvers, NULL); 5461 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5462 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* Client owner */ 5463 *tl = txdr_unsigned(clp->nfsc_rev); 5464 (void) nfsm_strtom(nd, clp->nfsc_id, clp->nfsc_idlen); 5465 5466 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_UNSIGNED); 5467 *tl++ = txdr_unsigned(exchflags); 5468 *tl++ = txdr_unsigned(NFSV4EXCH_SP4NONE); 5469 5470 /* Set the implementation id4 */ 5471 *tl = txdr_unsigned(1); 5472 (void) nfsm_strtom(nd, "freebsd.org", strlen("freebsd.org")); 5473 (void) nfsm_strtom(nd, version, strlen(version)); 5474 NFSM_BUILD(tl, uint32_t *, NFSX_V4TIME); 5475 verstime.tv_sec = 1293840000; /* Jan 1, 2011 */ 5476 verstime.tv_nsec = 0; 5477 txdr_nfsv4time(&verstime, tl); 5478 nd->nd_flag |= ND_USEGSSNAME; 5479 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, 5480 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5481 NFSCL_DEBUG(1, "exchangeid err=%d reps=%d\n", error, 5482 (int)nd->nd_repstat); 5483 if (error != 0) 5484 return (error); 5485 if (nd->nd_repstat == 0) { 5486 NFSM_DISSECT(tl, uint32_t *, 6 * NFSX_UNSIGNED + NFSX_HYPER); 5487 len = fxdr_unsigned(int, *(tl + 7)); 5488 if (len < 0 || len > NFSV4_OPAQUELIMIT) { 5489 error = NFSERR_BADXDR; 5490 goto nfsmout; 5491 } 5492 dsp = malloc(sizeof(struct nfsclds) + len + 1, M_NFSCLDS, 5493 M_WAITOK | M_ZERO); 5494 dsp->nfsclds_expire = NFSD_MONOSEC + clp->nfsc_renew; 5495 dsp->nfsclds_servownlen = len; 5496 dsp->nfsclds_sess.nfsess_clientid.lval[0] = *tl++; 5497 dsp->nfsclds_sess.nfsess_clientid.lval[1] = *tl++; 5498 dsp->nfsclds_sess.nfsess_sequenceid = 5499 fxdr_unsigned(uint32_t, *tl++); 5500 v41flags = fxdr_unsigned(uint32_t, *tl); 5501 if ((v41flags & NFSV4EXCH_USEPNFSMDS) != 0 && 5502 NFSHASPNFSOPT(nmp)) { 5503 NFSCL_DEBUG(1, "set PNFS\n"); 5504 NFSLOCKMNT(nmp); 5505 nmp->nm_state |= NFSSTA_PNFS; 5506 NFSUNLOCKMNT(nmp); 5507 dsp->nfsclds_flags |= NFSCLDS_MDS; 5508 } 5509 if ((v41flags & NFSV4EXCH_USEPNFSDS) != 0) 5510 dsp->nfsclds_flags |= NFSCLDS_DS; 5511 if (minorvers == NFSV42_MINORVERSION) 5512 dsp->nfsclds_flags |= NFSCLDS_MINORV2; 5513 if (len > 0) 5514 nd->nd_repstat = nfsrv_mtostr(nd, 5515 dsp->nfsclds_serverown, len); 5516 if (nd->nd_repstat == 0) { 5517 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 5518 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 5519 NULL, MTX_DEF); 5520 nfscl_initsessionslots(&dsp->nfsclds_sess); 5521 *dspp = dsp; 5522 } else 5523 free(dsp, M_NFSCLDS); 5524 } 5525 error = nd->nd_repstat; 5526 nfsmout: 5527 m_freem(nd->nd_mrep); 5528 return (error); 5529 } 5530 5531 /* 5532 * Do the NFSv4.1 Create Session. 5533 */ 5534 int 5535 nfsrpc_createsession(struct nfsmount *nmp, struct nfsclsession *sep, 5536 struct nfssockreq *nrp, struct nfsclds *dsp, uint32_t sequenceid, int mds, 5537 struct ucred *cred, NFSPROC_T *p) 5538 { 5539 uint32_t crflags, maxval, *tl; 5540 struct nfsrv_descript nfsd; 5541 struct nfsrv_descript *nd = &nfsd; 5542 int error, irdcnt, minorvers; 5543 5544 /* Make sure nm_rsize, nm_wsize is set. */ 5545 if (nmp->nm_rsize > NFS_MAXBSIZE || nmp->nm_rsize == 0) 5546 nmp->nm_rsize = NFS_MAXBSIZE; 5547 if (nmp->nm_wsize > NFS_MAXBSIZE || nmp->nm_wsize == 0) 5548 nmp->nm_wsize = NFS_MAXBSIZE; 5549 if (dsp == NULL) 5550 minorvers = nmp->nm_minorvers; 5551 else if ((dsp->nfsclds_flags & NFSCLDS_MINORV2) != 0) 5552 minorvers = NFSV42_MINORVERSION; 5553 else 5554 minorvers = NFSV41_MINORVERSION; 5555 nfscl_reqstart(nd, NFSPROC_CREATESESSION, nmp, NULL, 0, NULL, NULL, 5556 NFS_VER4, minorvers, NULL); 5557 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 5558 *tl++ = sep->nfsess_clientid.lval[0]; 5559 *tl++ = sep->nfsess_clientid.lval[1]; 5560 *tl++ = txdr_unsigned(sequenceid); 5561 crflags = (NFSMNT_RDONLY(nmp->nm_mountp) ? 0 : NFSV4CRSESS_PERSIST); 5562 if (nfscl_enablecallb != 0 && nfs_numnfscbd > 0 && mds != 0) 5563 crflags |= NFSV4CRSESS_CONNBACKCHAN; 5564 *tl = txdr_unsigned(crflags); 5565 5566 /* Fill in fore channel attributes. */ 5567 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED); 5568 *tl++ = 0; /* Header pad size */ 5569 if ((nd->nd_flag & ND_NFSV42) != 0 && mds != 0 && sb_max_adj >= 5570 nmp->nm_wsize && sb_max_adj >= nmp->nm_rsize) { 5571 /* 5572 * NFSv4.2 Extended Attribute operations may want to do 5573 * requests/replies that are larger than nm_rsize/nm_wsize. 5574 */ 5575 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR); 5576 *tl++ = txdr_unsigned(sb_max_adj - NFS_MAXXDR); 5577 } else { 5578 *tl++ = txdr_unsigned(nmp->nm_wsize + NFS_MAXXDR); 5579 *tl++ = txdr_unsigned(nmp->nm_rsize + NFS_MAXXDR); 5580 } 5581 *tl++ = txdr_unsigned(4096); /* Max response size cached */ 5582 *tl++ = txdr_unsigned(20); /* Max operations */ 5583 *tl++ = txdr_unsigned(64); /* Max slots */ 5584 *tl = 0; /* No rdma ird */ 5585 5586 /* Fill in back channel attributes. */ 5587 NFSM_BUILD(tl, uint32_t *, 7 * NFSX_UNSIGNED); 5588 *tl++ = 0; /* Header pad size */ 5589 *tl++ = txdr_unsigned(10000); /* Max request size */ 5590 *tl++ = txdr_unsigned(10000); /* Max response size */ 5591 *tl++ = txdr_unsigned(4096); /* Max response size cached */ 5592 *tl++ = txdr_unsigned(4); /* Max operations */ 5593 *tl++ = txdr_unsigned(NFSV4_CBSLOTS); /* Max slots */ 5594 *tl = 0; /* No rdma ird */ 5595 5596 NFSM_BUILD(tl, uint32_t *, 8 * NFSX_UNSIGNED); 5597 *tl++ = txdr_unsigned(NFS_CALLBCKPROG); /* Call back prog # */ 5598 5599 /* Allow AUTH_SYS callbacks as uid, gid == 0. */ 5600 *tl++ = txdr_unsigned(1); /* Auth_sys only */ 5601 *tl++ = txdr_unsigned(AUTH_SYS); /* AUTH_SYS type */ 5602 *tl++ = txdr_unsigned(nfsboottime.tv_sec); /* time stamp */ 5603 *tl++ = 0; /* Null machine name */ 5604 *tl++ = 0; /* Uid == 0 */ 5605 *tl++ = 0; /* Gid == 0 */ 5606 *tl = 0; /* No additional gids */ 5607 nd->nd_flag |= ND_USEGSSNAME; 5608 error = newnfs_request(nd, nmp, NULL, nrp, NULL, p, cred, NFS_PROG, 5609 NFS_VER4, NULL, 1, NULL, NULL); 5610 if (error != 0) 5611 return (error); 5612 if (nd->nd_repstat == 0) { 5613 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 5614 2 * NFSX_UNSIGNED); 5615 bcopy(tl, sep->nfsess_sessionid, NFSX_V4SESSIONID); 5616 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 5617 sep->nfsess_sequenceid = fxdr_unsigned(uint32_t, *tl++); 5618 crflags = fxdr_unsigned(uint32_t, *tl); 5619 if ((crflags & NFSV4CRSESS_PERSIST) != 0 && mds != 0) { 5620 NFSLOCKMNT(nmp); 5621 nmp->nm_state |= NFSSTA_SESSPERSIST; 5622 NFSUNLOCKMNT(nmp); 5623 } 5624 5625 /* Get the fore channel slot count. */ 5626 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED); 5627 tl++; /* Skip the header pad size. */ 5628 5629 /* Make sure nm_wsize is small enough. */ 5630 maxval = fxdr_unsigned(uint32_t, *tl++); 5631 while (maxval < nmp->nm_wsize + NFS_MAXXDR) { 5632 if (nmp->nm_wsize > 8096) 5633 nmp->nm_wsize /= 2; 5634 else 5635 break; 5636 } 5637 sep->nfsess_maxreq = maxval; 5638 5639 /* Make sure nm_rsize is small enough. */ 5640 maxval = fxdr_unsigned(uint32_t, *tl++); 5641 while (maxval < nmp->nm_rsize + NFS_MAXXDR) { 5642 if (nmp->nm_rsize > 8096) 5643 nmp->nm_rsize /= 2; 5644 else 5645 break; 5646 } 5647 sep->nfsess_maxresp = maxval; 5648 5649 sep->nfsess_maxcache = fxdr_unsigned(int, *tl++); 5650 tl++; 5651 sep->nfsess_foreslots = fxdr_unsigned(uint16_t, *tl++); 5652 NFSCL_DEBUG(4, "fore slots=%d\n", (int)sep->nfsess_foreslots); 5653 irdcnt = fxdr_unsigned(int, *tl); 5654 if (irdcnt < 0 || irdcnt > 1) { 5655 error = NFSERR_BADXDR; 5656 goto nfsmout; 5657 } 5658 if (irdcnt > 0) 5659 NFSM_DISSECT(tl, uint32_t *, irdcnt * NFSX_UNSIGNED); 5660 5661 /* and the back channel slot count. */ 5662 NFSM_DISSECT(tl, uint32_t *, 7 * NFSX_UNSIGNED); 5663 tl += 5; 5664 sep->nfsess_backslots = fxdr_unsigned(uint16_t, *tl); 5665 NFSCL_DEBUG(4, "back slots=%d\n", (int)sep->nfsess_backslots); 5666 } 5667 error = nd->nd_repstat; 5668 nfsmout: 5669 m_freem(nd->nd_mrep); 5670 return (error); 5671 } 5672 5673 /* 5674 * Do the NFSv4.1 Destroy Client. 5675 */ 5676 int 5677 nfsrpc_destroyclient(struct nfsmount *nmp, struct nfsclclient *clp, 5678 struct ucred *cred, NFSPROC_T *p) 5679 { 5680 uint32_t *tl; 5681 struct nfsrv_descript nfsd; 5682 struct nfsrv_descript *nd = &nfsd; 5683 int error; 5684 struct nfsclsession *tsep; 5685 5686 nfscl_reqstart(nd, NFSPROC_DESTROYCLIENT, nmp, NULL, 0, NULL, NULL, 0, 5687 0, NULL); 5688 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5689 tsep = nfsmnt_mdssession(nmp); 5690 *tl++ = tsep->nfsess_clientid.lval[0]; 5691 *tl = tsep->nfsess_clientid.lval[1]; 5692 nd->nd_flag |= ND_USEGSSNAME; 5693 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5694 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5695 if (error != 0) 5696 return (error); 5697 error = nd->nd_repstat; 5698 m_freem(nd->nd_mrep); 5699 return (error); 5700 } 5701 5702 /* 5703 * Do the NFSv4.1 LayoutGet. 5704 */ 5705 static int 5706 nfsrpc_layoutget(struct nfsmount *nmp, uint8_t *fhp, int fhlen, int iomode, 5707 uint64_t offset, uint64_t len, uint64_t minlen, int layouttype, 5708 int layoutlen, nfsv4stateid_t *stateidp, int *retonclosep, 5709 struct nfsclflayouthead *flhp, struct ucred *cred, NFSPROC_T *p) 5710 { 5711 struct nfsrv_descript nfsd, *nd = &nfsd; 5712 int error; 5713 5714 nfscl_reqstart(nd, NFSPROC_LAYOUTGET, nmp, fhp, fhlen, NULL, NULL, 0, 5715 0, cred); 5716 nfsrv_setuplayoutget(nd, iomode, offset, len, minlen, stateidp, 5717 layouttype, layoutlen, 0); 5718 nd->nd_flag |= ND_USEGSSNAME; 5719 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5720 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5721 NFSCL_DEBUG(4, "layget err=%d st=%d\n", error, nd->nd_repstat); 5722 if (error != 0) 5723 return (error); 5724 if (nd->nd_repstat == 0) 5725 error = nfsrv_parselayoutget(nmp, nd, stateidp, retonclosep, 5726 flhp); 5727 if (error == 0 && nd->nd_repstat != 0) 5728 error = nd->nd_repstat; 5729 m_freem(nd->nd_mrep); 5730 return (error); 5731 } 5732 5733 /* 5734 * Do the NFSv4.1 Get Device Info. 5735 */ 5736 int 5737 nfsrpc_getdeviceinfo(struct nfsmount *nmp, uint8_t *deviceid, int layouttype, 5738 uint32_t *notifybitsp, struct nfscldevinfo **ndip, struct ucred *cred, 5739 NFSPROC_T *p) 5740 { 5741 uint32_t cnt, *tl, vers, minorvers; 5742 struct nfsrv_descript nfsd; 5743 struct nfsrv_descript *nd = &nfsd; 5744 struct sockaddr_in sin, ssin; 5745 struct sockaddr_in6 sin6, ssin6; 5746 struct nfsclds *dsp = NULL, **dspp, **gotdspp; 5747 struct nfscldevinfo *ndi; 5748 int addrcnt = 0, bitcnt, error, gotminor, gotvers, i, isudp, j; 5749 int stripecnt; 5750 uint8_t stripeindex; 5751 sa_family_t af, safilled; 5752 5753 ssin.sin_port = 0; /* To shut up compiler. */ 5754 ssin.sin_addr.s_addr = 0; /* ditto */ 5755 *ndip = NULL; 5756 ndi = NULL; 5757 gotdspp = NULL; 5758 nfscl_reqstart(nd, NFSPROC_GETDEVICEINFO, nmp, NULL, 0, NULL, NULL, 0, 5759 0, cred); 5760 NFSM_BUILD(tl, uint32_t *, NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED); 5761 NFSBCOPY(deviceid, tl, NFSX_V4DEVICEID); 5762 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 5763 *tl++ = txdr_unsigned(layouttype); 5764 *tl++ = txdr_unsigned(100000); 5765 if (notifybitsp != NULL && *notifybitsp != 0) { 5766 *tl = txdr_unsigned(1); /* One word of bits. */ 5767 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 5768 *tl = txdr_unsigned(*notifybitsp); 5769 } else 5770 *tl = txdr_unsigned(0); 5771 nd->nd_flag |= ND_USEGSSNAME; 5772 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5773 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5774 if (error != 0) 5775 return (error); 5776 if (nd->nd_repstat == 0) { 5777 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5778 if (layouttype != fxdr_unsigned(int, *tl)) 5779 printf("EEK! devinfo layout type not same!\n"); 5780 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) { 5781 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5782 stripecnt = fxdr_unsigned(int, *tl); 5783 NFSCL_DEBUG(4, "stripecnt=%d\n", stripecnt); 5784 if (stripecnt < 1 || stripecnt > 4096) { 5785 printf("pNFS File layout devinfo stripecnt %d:" 5786 " out of range\n", stripecnt); 5787 error = NFSERR_BADXDR; 5788 goto nfsmout; 5789 } 5790 NFSM_DISSECT(tl, uint32_t *, (stripecnt + 1) * 5791 NFSX_UNSIGNED); 5792 addrcnt = fxdr_unsigned(int, *(tl + stripecnt)); 5793 NFSCL_DEBUG(4, "addrcnt=%d\n", addrcnt); 5794 if (addrcnt < 1 || addrcnt > 128) { 5795 printf("NFS devinfo addrcnt %d: out of range\n", 5796 addrcnt); 5797 error = NFSERR_BADXDR; 5798 goto nfsmout; 5799 } 5800 5801 /* 5802 * Now we know how many stripe indices and addresses, so 5803 * we can allocate the structure the correct size. 5804 */ 5805 i = (stripecnt * sizeof(uint8_t)) / 5806 sizeof(struct nfsclds *) + 1; 5807 NFSCL_DEBUG(4, "stripeindices=%d\n", i); 5808 ndi = malloc(sizeof(*ndi) + (addrcnt + i) * 5809 sizeof(struct nfsclds *), M_NFSDEVINFO, M_WAITOK | 5810 M_ZERO); 5811 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, 5812 NFSX_V4DEVICEID); 5813 ndi->nfsdi_refcnt = 0; 5814 ndi->nfsdi_flags = NFSDI_FILELAYOUT; 5815 ndi->nfsdi_stripecnt = stripecnt; 5816 ndi->nfsdi_addrcnt = addrcnt; 5817 /* Fill in the stripe indices. */ 5818 for (i = 0; i < stripecnt; i++) { 5819 stripeindex = fxdr_unsigned(uint8_t, *tl++); 5820 NFSCL_DEBUG(4, "stripeind=%d\n", stripeindex); 5821 if (stripeindex >= addrcnt) { 5822 printf("pNFS File Layout devinfo" 5823 " stripeindex %d: too big\n", 5824 (int)stripeindex); 5825 error = NFSERR_BADXDR; 5826 goto nfsmout; 5827 } 5828 nfsfldi_setstripeindex(ndi, i, stripeindex); 5829 } 5830 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 5831 /* For Flex File, we only get one address list. */ 5832 ndi = malloc(sizeof(*ndi) + sizeof(struct nfsclds *), 5833 M_NFSDEVINFO, M_WAITOK | M_ZERO); 5834 NFSBCOPY(deviceid, ndi->nfsdi_deviceid, 5835 NFSX_V4DEVICEID); 5836 ndi->nfsdi_refcnt = 0; 5837 ndi->nfsdi_flags = NFSDI_FLEXFILE; 5838 addrcnt = ndi->nfsdi_addrcnt = 1; 5839 } 5840 5841 /* Now, dissect the server address(es). */ 5842 safilled = AF_UNSPEC; 5843 for (i = 0; i < addrcnt; i++) { 5844 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5845 cnt = fxdr_unsigned(uint32_t, *tl); 5846 if (cnt == 0) { 5847 printf("NFS devinfo 0 len addrlist\n"); 5848 error = NFSERR_BADXDR; 5849 goto nfsmout; 5850 } 5851 dspp = nfsfldi_addr(ndi, i); 5852 safilled = AF_UNSPEC; 5853 for (j = 0; j < cnt; j++) { 5854 error = nfsv4_getipaddr(nd, &sin, &sin6, &af, 5855 &isudp); 5856 if (error != 0 && error != EPERM) { 5857 error = NFSERR_BADXDR; 5858 goto nfsmout; 5859 } 5860 if (error == 0 && isudp == 0) { 5861 /* 5862 * The priority is: 5863 * - Same address family. 5864 * Save the address and dspp, so that 5865 * the connection can be done after 5866 * parsing is complete. 5867 */ 5868 if (safilled == AF_UNSPEC || 5869 (af == nmp->nm_nam->sa_family && 5870 safilled != nmp->nm_nam->sa_family) 5871 ) { 5872 if (af == AF_INET) 5873 ssin = sin; 5874 else 5875 ssin6 = sin6; 5876 safilled = af; 5877 gotdspp = dspp; 5878 } 5879 } 5880 } 5881 } 5882 5883 gotvers = NFS_VER4; /* Default NFSv4.1 for File Layout. */ 5884 gotminor = NFSV41_MINORVERSION; 5885 /* For Flex File, we will take one of the versions to use. */ 5886 if (layouttype == NFSLAYOUT_FLEXFILE) { 5887 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5888 j = fxdr_unsigned(int, *tl); 5889 if (j < 1 || j > NFSDEV_MAXVERS) { 5890 printf("pNFS: too many versions\n"); 5891 error = NFSERR_BADXDR; 5892 goto nfsmout; 5893 } 5894 gotvers = 0; 5895 gotminor = 0; 5896 for (i = 0; i < j; i++) { 5897 NFSM_DISSECT(tl, uint32_t *, 5 * NFSX_UNSIGNED); 5898 vers = fxdr_unsigned(uint32_t, *tl++); 5899 minorvers = fxdr_unsigned(uint32_t, *tl++); 5900 if (vers == NFS_VER3) 5901 minorvers = 0; 5902 if ((vers == NFS_VER4 && ((minorvers == 5903 NFSV41_MINORVERSION && gotminor == 0) || 5904 minorvers == NFSV42_MINORVERSION)) || 5905 (vers == NFS_VER3 && gotvers == 0)) { 5906 gotvers = vers; 5907 gotminor = minorvers; 5908 /* We'll take this one. */ 5909 ndi->nfsdi_versindex = i; 5910 ndi->nfsdi_vers = vers; 5911 ndi->nfsdi_minorvers = minorvers; 5912 ndi->nfsdi_rsize = fxdr_unsigned( 5913 uint32_t, *tl++); 5914 ndi->nfsdi_wsize = fxdr_unsigned( 5915 uint32_t, *tl++); 5916 if (*tl == newnfs_true) 5917 ndi->nfsdi_flags |= 5918 NFSDI_TIGHTCOUPLED; 5919 else 5920 ndi->nfsdi_flags &= 5921 ~NFSDI_TIGHTCOUPLED; 5922 } 5923 } 5924 if (gotvers == 0) { 5925 printf("pNFS: no NFSv3, NFSv4.1 or NFSv4.2\n"); 5926 error = NFSERR_BADXDR; 5927 goto nfsmout; 5928 } 5929 } 5930 5931 /* And the notify bits. */ 5932 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5933 bitcnt = fxdr_unsigned(int, *tl); 5934 if (bitcnt > 0) { 5935 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 5936 if (notifybitsp != NULL) 5937 *notifybitsp = 5938 fxdr_unsigned(uint32_t, *tl); 5939 } 5940 if (safilled != AF_UNSPEC) { 5941 KASSERT(ndi != NULL, ("ndi is NULL")); 5942 *ndip = ndi; 5943 } else 5944 error = EPERM; 5945 if (error == 0) { 5946 /* 5947 * Now we can do a TCP connection for the correct 5948 * NFS version and IP address. 5949 */ 5950 error = nfsrpc_fillsa(nmp, &ssin, &ssin6, safilled, 5951 gotvers, gotminor, &dsp, p); 5952 } 5953 if (error == 0) { 5954 KASSERT(gotdspp != NULL, ("gotdspp is NULL")); 5955 *gotdspp = dsp; 5956 } 5957 } 5958 if (nd->nd_repstat != 0 && error == 0) 5959 error = nd->nd_repstat; 5960 nfsmout: 5961 if (error != 0 && ndi != NULL) 5962 nfscl_freedevinfo(ndi); 5963 m_freem(nd->nd_mrep); 5964 return (error); 5965 } 5966 5967 /* 5968 * Do the NFSv4.1 LayoutCommit. 5969 */ 5970 int 5971 nfsrpc_layoutcommit(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim, 5972 uint64_t off, uint64_t len, uint64_t lastbyte, nfsv4stateid_t *stateidp, 5973 int layouttype, struct ucred *cred, NFSPROC_T *p) 5974 { 5975 uint32_t *tl; 5976 struct nfsrv_descript nfsd, *nd = &nfsd; 5977 int error; 5978 5979 nfscl_reqstart(nd, NFSPROC_LAYOUTCOMMIT, nmp, fh, fhlen, NULL, NULL, 5980 0, 0, cred); 5981 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED + 3 * NFSX_HYPER + 5982 NFSX_STATEID); 5983 txdr_hyper(off, tl); 5984 tl += 2; 5985 txdr_hyper(len, tl); 5986 tl += 2; 5987 if (reclaim != 0) 5988 *tl++ = newnfs_true; 5989 else 5990 *tl++ = newnfs_false; 5991 *tl++ = txdr_unsigned(stateidp->seqid); 5992 *tl++ = stateidp->other[0]; 5993 *tl++ = stateidp->other[1]; 5994 *tl++ = stateidp->other[2]; 5995 *tl++ = newnfs_true; 5996 if (lastbyte < off) 5997 lastbyte = off; 5998 else if (lastbyte >= (off + len)) 5999 lastbyte = off + len - 1; 6000 txdr_hyper(lastbyte, tl); 6001 tl += 2; 6002 *tl++ = newnfs_false; 6003 *tl++ = txdr_unsigned(layouttype); 6004 /* All supported layouts are 0 length. */ 6005 *tl = txdr_unsigned(0); 6006 nd->nd_flag |= ND_USEGSSNAME; 6007 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6008 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6009 if (error != 0) 6010 return (error); 6011 error = nd->nd_repstat; 6012 m_freem(nd->nd_mrep); 6013 return (error); 6014 } 6015 6016 /* 6017 * Do the NFSv4.1 LayoutReturn. 6018 */ 6019 int 6020 nfsrpc_layoutreturn(struct nfsmount *nmp, uint8_t *fh, int fhlen, int reclaim, 6021 int layouttype, uint32_t iomode, int layoutreturn, uint64_t offset, 6022 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p, 6023 uint32_t stat, uint32_t op, char *devid) 6024 { 6025 uint32_t *tl; 6026 struct nfsrv_descript nfsd, *nd = &nfsd; 6027 uint64_t tu64; 6028 int error; 6029 6030 nfscl_reqstart(nd, NFSPROC_LAYOUTRETURN, nmp, fh, fhlen, NULL, NULL, 6031 0, 0, cred); 6032 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED); 6033 if (reclaim != 0) 6034 *tl++ = newnfs_true; 6035 else 6036 *tl++ = newnfs_false; 6037 *tl++ = txdr_unsigned(layouttype); 6038 *tl++ = txdr_unsigned(iomode); 6039 *tl = txdr_unsigned(layoutreturn); 6040 if (layoutreturn == NFSLAYOUTRETURN_FILE) { 6041 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID + 6042 NFSX_UNSIGNED); 6043 txdr_hyper(offset, tl); 6044 tl += 2; 6045 txdr_hyper(len, tl); 6046 tl += 2; 6047 NFSCL_DEBUG(4, "layoutret stseq=%d\n", (int)stateidp->seqid); 6048 *tl++ = txdr_unsigned(stateidp->seqid); 6049 *tl++ = stateidp->other[0]; 6050 *tl++ = stateidp->other[1]; 6051 *tl++ = stateidp->other[2]; 6052 if (layouttype == NFSLAYOUT_NFSV4_1_FILES) 6053 *tl = txdr_unsigned(0); 6054 else if (layouttype == NFSLAYOUT_FLEXFILE) { 6055 if (stat != 0) { 6056 *tl = txdr_unsigned(2 * NFSX_HYPER + 6057 NFSX_STATEID + NFSX_V4DEVICEID + 5 * 6058 NFSX_UNSIGNED); 6059 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + 6060 NFSX_STATEID + NFSX_V4DEVICEID + 5 * 6061 NFSX_UNSIGNED); 6062 *tl++ = txdr_unsigned(1); /* One error. */ 6063 tu64 = 0; /* Offset. */ 6064 txdr_hyper(tu64, tl); tl += 2; 6065 tu64 = UINT64_MAX; /* Length. */ 6066 txdr_hyper(tu64, tl); tl += 2; 6067 NFSBCOPY(stateidp, tl, NFSX_STATEID); 6068 tl += (NFSX_STATEID / NFSX_UNSIGNED); 6069 *tl++ = txdr_unsigned(1); /* One error. */ 6070 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); 6071 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 6072 *tl++ = txdr_unsigned(stat); 6073 *tl++ = txdr_unsigned(op); 6074 } else { 6075 *tl = txdr_unsigned(2 * NFSX_UNSIGNED); 6076 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6077 /* No ioerrs. */ 6078 *tl++ = 0; 6079 } 6080 *tl = 0; /* No stats yet. */ 6081 } 6082 } 6083 nd->nd_flag |= ND_USEGSSNAME; 6084 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6085 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6086 if (error != 0) 6087 return (error); 6088 if (nd->nd_repstat == 0) { 6089 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 6090 if (*tl != 0) { 6091 NFSM_DISSECT(tl, uint32_t *, NFSX_STATEID); 6092 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++); 6093 stateidp->other[0] = *tl++; 6094 stateidp->other[1] = *tl++; 6095 stateidp->other[2] = *tl; 6096 } 6097 } else 6098 error = nd->nd_repstat; 6099 nfsmout: 6100 m_freem(nd->nd_mrep); 6101 return (error); 6102 } 6103 6104 /* 6105 * Do the NFSv4.2 LayoutError. 6106 */ 6107 static int 6108 nfsrpc_layouterror(struct nfsmount *nmp, uint8_t *fh, int fhlen, uint64_t offset, 6109 uint64_t len, nfsv4stateid_t *stateidp, struct ucred *cred, NFSPROC_T *p, 6110 uint32_t stat, uint32_t op, char *devid) 6111 { 6112 uint32_t *tl; 6113 struct nfsrv_descript nfsd, *nd = &nfsd; 6114 int error; 6115 6116 nfscl_reqstart(nd, NFSPROC_LAYOUTERROR, nmp, fh, fhlen, NULL, NULL, 6117 0, 0, cred); 6118 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_STATEID + 6119 NFSX_V4DEVICEID + 3 * NFSX_UNSIGNED); 6120 txdr_hyper(offset, tl); tl += 2; 6121 txdr_hyper(len, tl); tl += 2; 6122 *tl++ = txdr_unsigned(stateidp->seqid); 6123 *tl++ = stateidp->other[0]; 6124 *tl++ = stateidp->other[1]; 6125 *tl++ = stateidp->other[2]; 6126 *tl++ = txdr_unsigned(1); 6127 NFSBCOPY(devid, tl, NFSX_V4DEVICEID); 6128 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 6129 *tl++ = txdr_unsigned(stat); 6130 *tl = txdr_unsigned(op); 6131 nd->nd_flag |= ND_USEGSSNAME; 6132 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6133 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6134 if (error != 0) 6135 return (error); 6136 if (nd->nd_repstat != 0) 6137 error = nd->nd_repstat; 6138 m_freem(nd->nd_mrep); 6139 return (error); 6140 } 6141 6142 /* 6143 * Acquire a layout and devinfo, if possible. The caller must have acquired 6144 * a reference count on the nfsclclient structure before calling this. 6145 * Return the layout in lypp with a reference count on it, if successful. 6146 */ 6147 static int 6148 nfsrpc_getlayout(struct nfsmount *nmp, vnode_t vp, struct nfsfh *nfhp, 6149 int iomode, uint32_t rw, uint32_t *notifybitsp, nfsv4stateid_t *stateidp, 6150 uint64_t off, struct nfscllayout **lypp, struct ucred *cred, NFSPROC_T *p) 6151 { 6152 struct nfscllayout *lyp; 6153 struct nfsclflayout *flp; 6154 struct nfsclflayouthead flh; 6155 int error = 0, islocked, layoutlen, layouttype, recalled, retonclose; 6156 nfsv4stateid_t stateid; 6157 struct nfsclsession *tsep; 6158 6159 *lypp = NULL; 6160 if (NFSHASFLEXFILE(nmp)) 6161 layouttype = NFSLAYOUT_FLEXFILE; 6162 else 6163 layouttype = NFSLAYOUT_NFSV4_1_FILES; 6164 /* 6165 * If lyp is returned non-NULL, there will be a refcnt (shared lock) 6166 * on it, iff flp != NULL or a lock (exclusive lock) on it iff 6167 * flp == NULL. 6168 */ 6169 lyp = nfscl_getlayout(nmp->nm_clp, nfhp->nfh_fh, nfhp->nfh_len, 6170 off, rw, &flp, &recalled); 6171 islocked = 0; 6172 if (lyp == NULL || flp == NULL) { 6173 if (recalled != 0) 6174 return (EIO); 6175 LIST_INIT(&flh); 6176 tsep = nfsmnt_mdssession(nmp); 6177 layoutlen = tsep->nfsess_maxcache - 6178 (NFSX_STATEID + 3 * NFSX_UNSIGNED); 6179 if (lyp == NULL) { 6180 stateid.seqid = 0; 6181 stateid.other[0] = stateidp->other[0]; 6182 stateid.other[1] = stateidp->other[1]; 6183 stateid.other[2] = stateidp->other[2]; 6184 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh, 6185 nfhp->nfh_len, iomode, (uint64_t)0, UINT64_MAX, 6186 (uint64_t)0, layouttype, layoutlen, &stateid, 6187 &retonclose, &flh, cred, p); 6188 } else { 6189 islocked = 1; 6190 stateid.seqid = lyp->nfsly_stateid.seqid; 6191 stateid.other[0] = lyp->nfsly_stateid.other[0]; 6192 stateid.other[1] = lyp->nfsly_stateid.other[1]; 6193 stateid.other[2] = lyp->nfsly_stateid.other[2]; 6194 error = nfsrpc_layoutget(nmp, nfhp->nfh_fh, 6195 nfhp->nfh_len, iomode, off, UINT64_MAX, 6196 (uint64_t)0, layouttype, layoutlen, &stateid, 6197 &retonclose, &flh, cred, p); 6198 } 6199 error = nfsrpc_layoutgetres(nmp, vp, nfhp->nfh_fh, 6200 nfhp->nfh_len, &stateid, retonclose, notifybitsp, &lyp, 6201 &flh, layouttype, error, NULL, cred, p); 6202 if (error == 0) 6203 *lypp = lyp; 6204 else if (islocked != 0) 6205 nfscl_rellayout(lyp, 1); 6206 } else 6207 *lypp = lyp; 6208 return (error); 6209 } 6210 6211 /* 6212 * Do a TCP connection plus exchange id and create session. 6213 * If successful, a "struct nfsclds" is linked into the list for the 6214 * mount point and a pointer to it is returned. 6215 */ 6216 static int 6217 nfsrpc_fillsa(struct nfsmount *nmp, struct sockaddr_in *sin, 6218 struct sockaddr_in6 *sin6, sa_family_t af, int vers, int minorvers, 6219 struct nfsclds **dspp, NFSPROC_T *p) 6220 { 6221 struct sockaddr_in *msad, *sad; 6222 struct sockaddr_in6 *msad6, *sad6; 6223 struct nfsclclient *clp; 6224 struct nfssockreq *nrp; 6225 struct nfsclds *dsp, *tdsp; 6226 int error, firsttry; 6227 enum nfsclds_state retv; 6228 uint32_t sequenceid = 0; 6229 6230 KASSERT(nmp->nm_sockreq.nr_cred != NULL, 6231 ("nfsrpc_fillsa: NULL nr_cred")); 6232 NFSLOCKCLSTATE(); 6233 clp = nmp->nm_clp; 6234 NFSUNLOCKCLSTATE(); 6235 if (clp == NULL) 6236 return (EPERM); 6237 if (af == AF_INET) { 6238 NFSLOCKMNT(nmp); 6239 /* 6240 * Check to see if we already have a session for this 6241 * address that is usable for a DS. 6242 * Note that the MDS's address is in a different place 6243 * than the sessions already acquired for DS's. 6244 */ 6245 msad = (struct sockaddr_in *)nmp->nm_sockreq.nr_nam; 6246 tdsp = TAILQ_FIRST(&nmp->nm_sess); 6247 while (tdsp != NULL) { 6248 if (msad != NULL && msad->sin_family == AF_INET && 6249 sin->sin_addr.s_addr == msad->sin_addr.s_addr && 6250 sin->sin_port == msad->sin_port && 6251 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 && 6252 tdsp->nfsclds_sess.nfsess_defunct == 0) { 6253 *dspp = tdsp; 6254 NFSUNLOCKMNT(nmp); 6255 NFSCL_DEBUG(4, "fnd same addr\n"); 6256 return (0); 6257 } 6258 tdsp = TAILQ_NEXT(tdsp, nfsclds_list); 6259 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL) 6260 msad = (struct sockaddr_in *) 6261 tdsp->nfsclds_sockp->nr_nam; 6262 else 6263 msad = NULL; 6264 } 6265 NFSUNLOCKMNT(nmp); 6266 6267 /* No IP address match, so look for new/trunked one. */ 6268 sad = malloc(sizeof(*sad), M_SONAME, M_WAITOK | M_ZERO); 6269 sad->sin_len = sizeof(*sad); 6270 sad->sin_family = AF_INET; 6271 sad->sin_port = sin->sin_port; 6272 sad->sin_addr.s_addr = sin->sin_addr.s_addr; 6273 if (NFSHASPNFS(nmp) && NFSHASKERB(nmp)) { 6274 /* For pNFS, a separate server principal is needed. */ 6275 nrp = malloc(sizeof(*nrp) + NI_MAXSERV + NI_MAXHOST, 6276 M_NFSSOCKREQ, M_WAITOK | M_ZERO); 6277 /* 6278 * Use the latter part of nr_srvprinc as a temporary 6279 * buffer for the IP address. 6280 */ 6281 inet_ntoa_r(sad->sin_addr, 6282 &nrp->nr_srvprinc[NI_MAXSERV]); 6283 NFSCL_DEBUG(1, "nfsrpc_fillsa: DS IP=%s\n", 6284 &nrp->nr_srvprinc[NI_MAXSERV]); 6285 if (!rpc_gss_ip_to_srv_principal_call( 6286 &nrp->nr_srvprinc[NI_MAXSERV], "nfs", 6287 nrp->nr_srvprinc)) 6288 nrp->nr_srvprinc[0] = '\0'; 6289 NFSCL_DEBUG(1, "nfsrpc_fillsa: srv principal=%s\n", 6290 nrp->nr_srvprinc); 6291 } else 6292 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, 6293 M_WAITOK | M_ZERO); 6294 nrp->nr_nam = (struct sockaddr *)sad; 6295 } else if (af == AF_INET6) { 6296 NFSLOCKMNT(nmp); 6297 /* 6298 * Check to see if we already have a session for this 6299 * address that is usable for a DS. 6300 * Note that the MDS's address is in a different place 6301 * than the sessions already acquired for DS's. 6302 */ 6303 msad6 = (struct sockaddr_in6 *)nmp->nm_sockreq.nr_nam; 6304 tdsp = TAILQ_FIRST(&nmp->nm_sess); 6305 while (tdsp != NULL) { 6306 if (msad6 != NULL && msad6->sin6_family == AF_INET6 && 6307 IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, 6308 &msad6->sin6_addr) && 6309 sin6->sin6_port == msad6->sin6_port && 6310 (tdsp->nfsclds_flags & NFSCLDS_DS) != 0 && 6311 tdsp->nfsclds_sess.nfsess_defunct == 0) { 6312 *dspp = tdsp; 6313 NFSUNLOCKMNT(nmp); 6314 return (0); 6315 } 6316 tdsp = TAILQ_NEXT(tdsp, nfsclds_list); 6317 if (tdsp != NULL && tdsp->nfsclds_sockp != NULL) 6318 msad6 = (struct sockaddr_in6 *) 6319 tdsp->nfsclds_sockp->nr_nam; 6320 else 6321 msad6 = NULL; 6322 } 6323 NFSUNLOCKMNT(nmp); 6324 6325 /* No IP address match, so look for new/trunked one. */ 6326 sad6 = malloc(sizeof(*sad6), M_SONAME, M_WAITOK | M_ZERO); 6327 sad6->sin6_len = sizeof(*sad6); 6328 sad6->sin6_family = AF_INET6; 6329 sad6->sin6_port = sin6->sin6_port; 6330 NFSBCOPY(&sin6->sin6_addr, &sad6->sin6_addr, 6331 sizeof(struct in6_addr)); 6332 if (NFSHASPNFS(nmp) && NFSHASKERB(nmp)) { 6333 /* For pNFS, a separate server principal is needed. */ 6334 nrp = malloc(sizeof(*nrp) + NI_MAXSERV + NI_MAXHOST, 6335 M_NFSSOCKREQ, M_WAITOK | M_ZERO); 6336 /* 6337 * Use the latter part of nr_srvprinc as a temporary 6338 * buffer for the IP address. 6339 */ 6340 inet_ntop(AF_INET6, &sad6->sin6_addr, 6341 &nrp->nr_srvprinc[NI_MAXSERV], NI_MAXHOST); 6342 NFSCL_DEBUG(1, "nfsrpc_fillsa: DS IP=%s\n", 6343 &nrp->nr_srvprinc[NI_MAXSERV]); 6344 if (!rpc_gss_ip_to_srv_principal_call( 6345 &nrp->nr_srvprinc[NI_MAXSERV], "nfs", 6346 nrp->nr_srvprinc)) 6347 nrp->nr_srvprinc[0] = '\0'; 6348 NFSCL_DEBUG(1, "nfsrpc_fillsa: srv principal=%s\n", 6349 nrp->nr_srvprinc); 6350 } else 6351 nrp = malloc(sizeof(*nrp), M_NFSSOCKREQ, 6352 M_WAITOK | M_ZERO); 6353 nrp->nr_nam = (struct sockaddr *)sad6; 6354 } else 6355 return (EPERM); 6356 6357 nrp->nr_sotype = SOCK_STREAM; 6358 mtx_init(&nrp->nr_mtx, "nfssock", NULL, MTX_DEF); 6359 nrp->nr_prog = NFS_PROG; 6360 nrp->nr_vers = vers; 6361 6362 /* 6363 * Use the credentials that were used for the mount, which are 6364 * in nmp->nm_sockreq.nr_cred for newnfs_connect() etc. 6365 * Ref. counting the credentials with crhold() is probably not 6366 * necessary, since nm_sockreq.nr_cred won't be crfree()'d until 6367 * unmount, but I did it anyhow. 6368 */ 6369 nrp->nr_cred = crhold(nmp->nm_sockreq.nr_cred); 6370 error = newnfs_connect(nmp, nrp, NULL, p, 0, false, &nrp->nr_client); 6371 NFSCL_DEBUG(3, "DS connect=%d\n", error); 6372 6373 dsp = NULL; 6374 /* Now, do the exchangeid and create session. */ 6375 if (error == 0) { 6376 if (vers == NFS_VER4) { 6377 firsttry = 0; 6378 do { 6379 error = nfsrpc_exchangeid(nmp, clp, nrp, 6380 minorvers, NFSV4EXCH_USEPNFSDS, &dsp, 6381 nrp->nr_cred, p); 6382 NFSCL_DEBUG(3, "DS exchangeid=%d\n", error); 6383 if (error == NFSERR_MINORVERMISMATCH) 6384 minorvers = NFSV42_MINORVERSION; 6385 } while (error == NFSERR_MINORVERMISMATCH && 6386 firsttry++ == 0); 6387 if (error != 0) 6388 newnfs_disconnect(NULL, nrp); 6389 } else { 6390 dsp = malloc(sizeof(struct nfsclds), M_NFSCLDS, 6391 M_WAITOK | M_ZERO); 6392 dsp->nfsclds_flags |= NFSCLDS_DS; 6393 dsp->nfsclds_expire = INT32_MAX; /* No renews needed. */ 6394 mtx_init(&dsp->nfsclds_mtx, "nfsds", NULL, MTX_DEF); 6395 mtx_init(&dsp->nfsclds_sess.nfsess_mtx, "nfssession", 6396 NULL, MTX_DEF); 6397 } 6398 } 6399 if (error == 0) { 6400 dsp->nfsclds_sockp = nrp; 6401 if (vers == NFS_VER4) { 6402 NFSLOCKMNT(nmp); 6403 retv = nfscl_getsameserver(nmp, dsp, &tdsp, 6404 &sequenceid); 6405 NFSCL_DEBUG(3, "getsame ret=%d\n", retv); 6406 if (retv == NFSDSP_USETHISSESSION && 6407 nfscl_dssameconn != 0) { 6408 NFSLOCKDS(tdsp); 6409 tdsp->nfsclds_flags |= NFSCLDS_SAMECONN; 6410 NFSUNLOCKDS(tdsp); 6411 NFSUNLOCKMNT(nmp); 6412 /* 6413 * If there is already a session for this 6414 * server, use it. 6415 */ 6416 newnfs_disconnect(NULL, nrp); 6417 nfscl_freenfsclds(dsp); 6418 *dspp = tdsp; 6419 return (0); 6420 } 6421 if (retv == NFSDSP_NOTFOUND) 6422 sequenceid = 6423 dsp->nfsclds_sess.nfsess_sequenceid; 6424 NFSUNLOCKMNT(nmp); 6425 error = nfsrpc_createsession(nmp, &dsp->nfsclds_sess, 6426 nrp, dsp, sequenceid, 0, nrp->nr_cred, p); 6427 NFSCL_DEBUG(3, "DS createsess=%d\n", error); 6428 } 6429 } else { 6430 NFSFREECRED(nrp->nr_cred); 6431 NFSFREEMUTEX(&nrp->nr_mtx); 6432 free(nrp->nr_nam, M_SONAME); 6433 free(nrp, M_NFSSOCKREQ); 6434 } 6435 if (error == 0) { 6436 NFSCL_DEBUG(3, "add DS session\n"); 6437 /* 6438 * Put it at the end of the list. That way the list 6439 * is ordered by when the entry was added. This matters 6440 * since the one done first is the one that should be 6441 * used for sequencid'ing any subsequent create sessions. 6442 */ 6443 NFSLOCKMNT(nmp); 6444 TAILQ_INSERT_TAIL(&nmp->nm_sess, dsp, nfsclds_list); 6445 NFSUNLOCKMNT(nmp); 6446 *dspp = dsp; 6447 } else if (dsp != NULL) { 6448 newnfs_disconnect(NULL, nrp); 6449 nfscl_freenfsclds(dsp); 6450 } 6451 return (error); 6452 } 6453 6454 /* 6455 * Do the NFSv4.1 Reclaim Complete. 6456 */ 6457 int 6458 nfsrpc_reclaimcomplete(struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p) 6459 { 6460 uint32_t *tl; 6461 struct nfsrv_descript nfsd; 6462 struct nfsrv_descript *nd = &nfsd; 6463 int error; 6464 6465 nfscl_reqstart(nd, NFSPROC_RECLAIMCOMPL, nmp, NULL, 0, NULL, NULL, 0, 6466 0, cred); 6467 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 6468 *tl = newnfs_false; 6469 nd->nd_flag |= ND_USEGSSNAME; 6470 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6471 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6472 if (error != 0) 6473 return (error); 6474 error = nd->nd_repstat; 6475 m_freem(nd->nd_mrep); 6476 return (error); 6477 } 6478 6479 /* 6480 * Initialize the slot tables for a session. 6481 */ 6482 static void 6483 nfscl_initsessionslots(struct nfsclsession *sep) 6484 { 6485 int i; 6486 6487 for (i = 0; i < NFSV4_CBSLOTS; i++) { 6488 if (sep->nfsess_cbslots[i].nfssl_reply != NULL) 6489 m_freem(sep->nfsess_cbslots[i].nfssl_reply); 6490 NFSBZERO(&sep->nfsess_cbslots[i], sizeof(struct nfsslot)); 6491 } 6492 for (i = 0; i < 64; i++) 6493 sep->nfsess_slotseq[i] = 0; 6494 sep->nfsess_slots = 0; 6495 sep->nfsess_badslots = 0; 6496 } 6497 6498 /* 6499 * Called to try and do an I/O operation via an NFSv4.1 Data Server (DS). 6500 */ 6501 int 6502 nfscl_doiods(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6503 uint32_t rwaccess, int docommit, struct ucred *cred, NFSPROC_T *p) 6504 { 6505 struct nfsnode *np = VTONFS(vp); 6506 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 6507 struct nfscllayout *layp; 6508 struct nfscldevinfo *dip; 6509 struct nfsclflayout *rflp; 6510 struct mbuf *m, *m2; 6511 struct nfsclwritedsdorpc *drpc, *tdrpc; 6512 nfsv4stateid_t stateid; 6513 struct ucred *newcred; 6514 uint64_t lastbyte, len, off, oresid, xfer; 6515 int eof, error, firstmirror, i, iolaymode, mirrorcnt, recalled, timo; 6516 void *lckp; 6517 uint8_t *dev; 6518 void *iovbase = NULL; 6519 size_t iovlen = 0; 6520 off_t offs = 0; 6521 ssize_t resid = 0; 6522 uint32_t op; 6523 6524 if (!NFSHASPNFS(nmp) || nfscl_enablecallb == 0 || nfs_numnfscbd == 0 || 6525 (np->n_flag & NNOLAYOUT) != 0) 6526 return (EIO); 6527 /* Now, get a reference cnt on the clientid for this mount. */ 6528 if (nfscl_getref(nmp) == 0) 6529 return (EIO); 6530 6531 /* Find an appropriate stateid. */ 6532 newcred = NFSNEWCRED(cred); 6533 error = nfscl_getstateid(vp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 6534 rwaccess, 1, newcred, p, &stateid, &lckp); 6535 if (error != 0) { 6536 NFSFREECRED(newcred); 6537 nfscl_relref(nmp); 6538 return (error); 6539 } 6540 /* Search for a layout for this file. */ 6541 off = uiop->uio_offset; 6542 layp = nfscl_getlayout(nmp->nm_clp, np->n_fhp->nfh_fh, 6543 np->n_fhp->nfh_len, off, rwaccess, &rflp, &recalled); 6544 if (layp == NULL || rflp == NULL) { 6545 if (recalled != 0) { 6546 NFSFREECRED(newcred); 6547 if (lckp != NULL) 6548 nfscl_lockderef(lckp); 6549 nfscl_relref(nmp); 6550 return (EIO); 6551 } 6552 if (layp != NULL) { 6553 nfscl_rellayout(layp, (rflp == NULL) ? 1 : 0); 6554 layp = NULL; 6555 } 6556 /* Try and get a Layout, if it is supported. */ 6557 if (rwaccess == NFSV4OPEN_ACCESSWRITE || 6558 (np->n_flag & NWRITEOPENED) != 0) 6559 iolaymode = NFSLAYOUTIOMODE_RW; 6560 else 6561 iolaymode = NFSLAYOUTIOMODE_READ; 6562 error = nfsrpc_getlayout(nmp, vp, np->n_fhp, iolaymode, 6563 rwaccess, NULL, &stateid, off, &layp, newcred, p); 6564 if (error != 0) { 6565 NFSLOCKNODE(np); 6566 np->n_flag |= NNOLAYOUT; 6567 NFSUNLOCKNODE(np); 6568 if (lckp != NULL) 6569 nfscl_lockderef(lckp); 6570 NFSFREECRED(newcred); 6571 if (layp != NULL) 6572 nfscl_rellayout(layp, 0); 6573 nfscl_relref(nmp); 6574 return (error); 6575 } 6576 } 6577 6578 /* 6579 * Loop around finding a layout that works for the first part of 6580 * this I/O operation, and then call the function that actually 6581 * does the RPC. 6582 */ 6583 eof = 0; 6584 len = (uint64_t)uiop->uio_resid; 6585 while (len > 0 && error == 0 && eof == 0) { 6586 off = uiop->uio_offset; 6587 error = nfscl_findlayoutforio(layp, off, rwaccess, &rflp); 6588 if (error == 0) { 6589 oresid = xfer = (uint64_t)uiop->uio_resid; 6590 if (xfer > (rflp->nfsfl_end - rflp->nfsfl_off)) 6591 xfer = rflp->nfsfl_end - rflp->nfsfl_off; 6592 /* 6593 * For Flex File layout with mirrored DSs, select one 6594 * of them at random for reads. For writes and commits, 6595 * do all mirrors. 6596 */ 6597 m = NULL; 6598 tdrpc = drpc = NULL; 6599 firstmirror = 0; 6600 mirrorcnt = 1; 6601 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0 && 6602 (mirrorcnt = rflp->nfsfl_mirrorcnt) > 1) { 6603 if (rwaccess == NFSV4OPEN_ACCESSREAD) { 6604 firstmirror = arc4random() % mirrorcnt; 6605 mirrorcnt = firstmirror + 1; 6606 } else { 6607 if (docommit == 0) { 6608 /* 6609 * Save values, so uiop can be 6610 * rolled back upon a write 6611 * error. 6612 */ 6613 offs = uiop->uio_offset; 6614 resid = uiop->uio_resid; 6615 iovbase = 6616 uiop->uio_iov->iov_base; 6617 iovlen = uiop->uio_iov->iov_len; 6618 m = nfsm_uiombuflist(uiop, len, 6619 0); 6620 if (m == NULL) { 6621 error = EFAULT; 6622 break; 6623 } 6624 } 6625 tdrpc = drpc = malloc(sizeof(*drpc) * 6626 (mirrorcnt - 1), M_TEMP, M_WAITOK | 6627 M_ZERO); 6628 } 6629 } 6630 for (i = firstmirror; i < mirrorcnt && error == 0; i++){ 6631 m2 = NULL; 6632 if (m != NULL && i < mirrorcnt - 1) 6633 m2 = m_copym(m, 0, M_COPYALL, M_WAITOK); 6634 else { 6635 m2 = m; 6636 m = NULL; 6637 } 6638 if ((layp->nfsly_flags & NFSLY_FLEXFILE) != 0) { 6639 dev = rflp->nfsfl_ffm[i].dev; 6640 dip = nfscl_getdevinfo(nmp->nm_clp, dev, 6641 rflp->nfsfl_ffm[i].devp); 6642 } else { 6643 dev = rflp->nfsfl_dev; 6644 dip = nfscl_getdevinfo(nmp->nm_clp, dev, 6645 rflp->nfsfl_devp); 6646 } 6647 if (dip != NULL) { 6648 if ((rflp->nfsfl_flags & NFSFL_FLEXFILE) 6649 != 0) 6650 error = nfscl_dofflayoutio(vp, 6651 uiop, iomode, must_commit, 6652 &eof, &stateid, rwaccess, 6653 dip, layp, rflp, off, xfer, 6654 i, docommit, m2, tdrpc, 6655 newcred, p); 6656 else 6657 error = nfscl_doflayoutio(vp, 6658 uiop, iomode, must_commit, 6659 &eof, &stateid, rwaccess, 6660 dip, layp, rflp, off, xfer, 6661 docommit, newcred, p); 6662 nfscl_reldevinfo(dip); 6663 } else { 6664 if (m2 != NULL) 6665 m_freem(m2); 6666 error = EIO; 6667 } 6668 tdrpc++; 6669 } 6670 if (m != NULL) 6671 m_freem(m); 6672 tdrpc = drpc; 6673 timo = hz / 50; /* Wait for 20msec. */ 6674 if (timo < 1) 6675 timo = 1; 6676 for (i = firstmirror; i < mirrorcnt - 1 && 6677 tdrpc != NULL; i++, tdrpc++) { 6678 /* 6679 * For the unused drpc entries, both inprog and 6680 * err == 0, so this loop won't break. 6681 */ 6682 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6683 tsleep(&tdrpc->tsk, PVFS, "clrpcio", 6684 timo); 6685 if (error == 0 && tdrpc->err != 0) 6686 error = tdrpc->err; 6687 if (rwaccess != NFSV4OPEN_ACCESSREAD && 6688 docommit == 0 && *must_commit == 0 && 6689 tdrpc->must_commit == 1) 6690 *must_commit = 1; 6691 } 6692 free(drpc, M_TEMP); 6693 if (error == 0) { 6694 if (mirrorcnt > 1 && rwaccess == 6695 NFSV4OPEN_ACCESSWRITE && docommit == 0) { 6696 NFSLOCKCLSTATE(); 6697 layp->nfsly_flags |= NFSLY_WRITTEN; 6698 NFSUNLOCKCLSTATE(); 6699 } 6700 lastbyte = off + xfer - 1; 6701 NFSLOCKCLSTATE(); 6702 if (lastbyte > layp->nfsly_lastbyte) 6703 layp->nfsly_lastbyte = lastbyte; 6704 NFSUNLOCKCLSTATE(); 6705 } else if (error == NFSERR_OPENMODE && 6706 rwaccess == NFSV4OPEN_ACCESSREAD) { 6707 NFSLOCKMNT(nmp); 6708 nmp->nm_state |= NFSSTA_OPENMODE; 6709 NFSUNLOCKMNT(nmp); 6710 } else if ((error == NFSERR_NOSPC || 6711 error == NFSERR_IO || error == NFSERR_NXIO) && 6712 nmp->nm_minorvers == NFSV42_MINORVERSION) { 6713 if (docommit != 0) 6714 op = NFSV4OP_COMMIT; 6715 else if (rwaccess == NFSV4OPEN_ACCESSREAD) 6716 op = NFSV4OP_READ; 6717 else 6718 op = NFSV4OP_WRITE; 6719 nfsrpc_layouterror(nmp, np->n_fhp->nfh_fh, 6720 np->n_fhp->nfh_len, off, xfer, 6721 &layp->nfsly_stateid, newcred, p, error, op, 6722 dip->nfsdi_deviceid); 6723 error = EIO; 6724 } else 6725 error = EIO; 6726 if (error == 0) 6727 len -= (oresid - (uint64_t)uiop->uio_resid); 6728 else if (mirrorcnt > 1 && rwaccess == 6729 NFSV4OPEN_ACCESSWRITE && docommit == 0) { 6730 /* 6731 * In case the rpc gets retried, roll the 6732 * uio fields changed by nfsm_uiombuflist() 6733 * back. 6734 */ 6735 uiop->uio_offset = offs; 6736 uiop->uio_resid = resid; 6737 uiop->uio_iov->iov_base = iovbase; 6738 uiop->uio_iov->iov_len = iovlen; 6739 } 6740 } 6741 } 6742 if (lckp != NULL) 6743 nfscl_lockderef(lckp); 6744 NFSFREECRED(newcred); 6745 nfscl_rellayout(layp, 0); 6746 nfscl_relref(nmp); 6747 return (error); 6748 } 6749 6750 /* 6751 * Find a file layout that will handle the first bytes of the requested 6752 * range and return the information from it needed to the I/O operation. 6753 */ 6754 int 6755 nfscl_findlayoutforio(struct nfscllayout *lyp, uint64_t off, uint32_t rwaccess, 6756 struct nfsclflayout **retflpp) 6757 { 6758 struct nfsclflayout *flp, *nflp, *rflp; 6759 uint32_t rw; 6760 6761 rflp = NULL; 6762 rw = rwaccess; 6763 /* For reading, do the Read list first and then the Write list. */ 6764 do { 6765 if (rw == NFSV4OPEN_ACCESSREAD) 6766 flp = LIST_FIRST(&lyp->nfsly_flayread); 6767 else 6768 flp = LIST_FIRST(&lyp->nfsly_flayrw); 6769 while (flp != NULL) { 6770 nflp = LIST_NEXT(flp, nfsfl_list); 6771 if (flp->nfsfl_off > off) 6772 break; 6773 if (flp->nfsfl_end > off && 6774 (rflp == NULL || rflp->nfsfl_end < flp->nfsfl_end)) 6775 rflp = flp; 6776 flp = nflp; 6777 } 6778 if (rw == NFSV4OPEN_ACCESSREAD) 6779 rw = NFSV4OPEN_ACCESSWRITE; 6780 else 6781 rw = 0; 6782 } while (rw != 0); 6783 if (rflp != NULL) { 6784 /* This one covers the most bytes starting at off. */ 6785 *retflpp = rflp; 6786 return (0); 6787 } 6788 return (EIO); 6789 } 6790 6791 /* 6792 * Do I/O using an NFSv4.1 or NFSv4.2 file layout. 6793 */ 6794 static int 6795 nfscl_doflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6796 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp, 6797 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off, 6798 uint64_t len, int docommit, struct ucred *cred, NFSPROC_T *p) 6799 { 6800 uint64_t io_off, rel_off, stripe_unit_size, transfer, xfer; 6801 int commit_thru_mds, error, stripe_index, stripe_pos, minorvers; 6802 struct nfsnode *np; 6803 struct nfsfh *fhp; 6804 struct nfsclds **dspp; 6805 6806 np = VTONFS(vp); 6807 rel_off = off - flp->nfsfl_patoff; 6808 stripe_unit_size = flp->nfsfl_util & NFSFLAYUTIL_STRIPE_MASK; 6809 stripe_pos = (rel_off / stripe_unit_size + flp->nfsfl_stripe1) % 6810 dp->nfsdi_stripecnt; 6811 transfer = stripe_unit_size - (rel_off % stripe_unit_size); 6812 error = 0; 6813 6814 /* Loop around, doing I/O for each stripe unit. */ 6815 while (len > 0 && error == 0) { 6816 stripe_index = nfsfldi_stripeindex(dp, stripe_pos); 6817 dspp = nfsfldi_addr(dp, stripe_index); 6818 if (((*dspp)->nfsclds_flags & NFSCLDS_MINORV2) != 0) 6819 minorvers = NFSV42_MINORVERSION; 6820 else 6821 minorvers = NFSV41_MINORVERSION; 6822 if (len > transfer && docommit == 0) 6823 xfer = transfer; 6824 else 6825 xfer = len; 6826 if ((flp->nfsfl_util & NFSFLAYUTIL_DENSE) != 0) { 6827 /* Dense layout. */ 6828 if (stripe_pos >= flp->nfsfl_fhcnt) 6829 return (EIO); 6830 fhp = flp->nfsfl_fh[stripe_pos]; 6831 io_off = (rel_off / (stripe_unit_size * 6832 dp->nfsdi_stripecnt)) * stripe_unit_size + 6833 rel_off % stripe_unit_size; 6834 } else { 6835 /* Sparse layout. */ 6836 if (flp->nfsfl_fhcnt > 1) { 6837 if (stripe_index >= flp->nfsfl_fhcnt) 6838 return (EIO); 6839 fhp = flp->nfsfl_fh[stripe_index]; 6840 } else if (flp->nfsfl_fhcnt == 1) 6841 fhp = flp->nfsfl_fh[0]; 6842 else 6843 fhp = np->n_fhp; 6844 io_off = off; 6845 } 6846 if ((flp->nfsfl_util & NFSFLAYUTIL_COMMIT_THRU_MDS) != 0) { 6847 commit_thru_mds = 1; 6848 if (docommit != 0) 6849 error = EIO; 6850 } else { 6851 commit_thru_mds = 0; 6852 NFSLOCKNODE(np); 6853 np->n_flag |= NDSCOMMIT; 6854 NFSUNLOCKNODE(np); 6855 } 6856 if (docommit != 0) { 6857 if (error == 0) 6858 error = nfsrpc_commitds(vp, io_off, xfer, 6859 *dspp, fhp, NFS_VER4, minorvers, cred, p); 6860 if (error == 0) { 6861 /* 6862 * Set both eof and uio_resid = 0 to end any 6863 * loops. 6864 */ 6865 *eofp = 1; 6866 uiop->uio_resid = 0; 6867 } else { 6868 NFSLOCKNODE(np); 6869 np->n_flag &= ~NDSCOMMIT; 6870 NFSUNLOCKNODE(np); 6871 } 6872 } else if (rwflag == NFSV4OPEN_ACCESSREAD) 6873 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp, 6874 io_off, xfer, fhp, 0, NFS_VER4, minorvers, cred, p); 6875 else { 6876 error = nfsrpc_writeds(vp, uiop, iomode, must_commit, 6877 stateidp, *dspp, io_off, xfer, fhp, commit_thru_mds, 6878 0, NFS_VER4, minorvers, cred, p); 6879 if (error == 0) { 6880 NFSLOCKCLSTATE(); 6881 lyp->nfsly_flags |= NFSLY_WRITTEN; 6882 NFSUNLOCKCLSTATE(); 6883 } 6884 } 6885 if (error == 0) { 6886 transfer = stripe_unit_size; 6887 stripe_pos = (stripe_pos + 1) % dp->nfsdi_stripecnt; 6888 len -= xfer; 6889 off += xfer; 6890 } 6891 } 6892 return (error); 6893 } 6894 6895 /* 6896 * Do I/O using an NFSv4.1 flex file layout. 6897 */ 6898 static int 6899 nfscl_dofflayoutio(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 6900 int *eofp, nfsv4stateid_t *stateidp, int rwflag, struct nfscldevinfo *dp, 6901 struct nfscllayout *lyp, struct nfsclflayout *flp, uint64_t off, 6902 uint64_t len, int mirror, int docommit, struct mbuf *mp, 6903 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 6904 { 6905 uint64_t xfer; 6906 int error; 6907 struct nfsnode *np; 6908 struct nfsfh *fhp; 6909 struct nfsclds **dspp; 6910 struct ucred *tcred; 6911 struct mbuf *m, *m2; 6912 uint32_t copylen; 6913 6914 np = VTONFS(vp); 6915 error = 0; 6916 NFSCL_DEBUG(4, "nfscl_dofflayoutio: off=%ju len=%ju\n", (uintmax_t)off, 6917 (uintmax_t)len); 6918 /* Loop around, doing I/O for each stripe unit. */ 6919 while (len > 0 && error == 0) { 6920 dspp = nfsfldi_addr(dp, 0); 6921 fhp = flp->nfsfl_ffm[mirror].fh[dp->nfsdi_versindex]; 6922 stateidp = &flp->nfsfl_ffm[mirror].st; 6923 NFSCL_DEBUG(4, "mirror=%d vind=%d fhlen=%d st.seqid=0x%x\n", 6924 mirror, dp->nfsdi_versindex, fhp->nfh_len, stateidp->seqid); 6925 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) { 6926 tcred = NFSNEWCRED(cred); 6927 tcred->cr_uid = flp->nfsfl_ffm[mirror].user; 6928 tcred->cr_groups[0] = flp->nfsfl_ffm[mirror].group; 6929 tcred->cr_ngroups = 1; 6930 } else 6931 tcred = cred; 6932 if (rwflag == NFSV4OPEN_ACCESSREAD) 6933 copylen = dp->nfsdi_rsize; 6934 else { 6935 copylen = dp->nfsdi_wsize; 6936 if (len > copylen && mp != NULL) { 6937 /* 6938 * When a mirrored configuration needs to do 6939 * multiple writes to each mirror, all writes 6940 * except the last one must be a multiple of 6941 * 4 bytes. This is required so that the XDR 6942 * does not need padding. 6943 * If possible, clip the size to an exact 6944 * multiple of the mbuf length, so that the 6945 * split will be on an mbuf boundary. 6946 */ 6947 copylen &= 0xfffffffc; 6948 if (copylen > mp->m_len) 6949 copylen = copylen / mp->m_len * 6950 mp->m_len; 6951 } 6952 } 6953 NFSLOCKNODE(np); 6954 np->n_flag |= NDSCOMMIT; 6955 NFSUNLOCKNODE(np); 6956 if (len > copylen && docommit == 0) 6957 xfer = copylen; 6958 else 6959 xfer = len; 6960 if (docommit != 0) { 6961 if (error == 0) { 6962 /* 6963 * Do last mirrored DS commit with this thread. 6964 */ 6965 if (mirror < flp->nfsfl_mirrorcnt - 1) 6966 error = nfsio_commitds(vp, off, xfer, 6967 *dspp, fhp, dp->nfsdi_vers, 6968 dp->nfsdi_minorvers, drpc, tcred, 6969 p); 6970 else 6971 error = nfsrpc_commitds(vp, off, xfer, 6972 *dspp, fhp, dp->nfsdi_vers, 6973 dp->nfsdi_minorvers, tcred, p); 6974 NFSCL_DEBUG(4, "commitds=%d\n", error); 6975 if (error != 0 && error != EACCES && error != 6976 ESTALE) { 6977 NFSCL_DEBUG(4, 6978 "DS layreterr for commit\n"); 6979 nfscl_dserr(NFSV4OP_COMMIT, error, dp, 6980 lyp, *dspp); 6981 } 6982 } 6983 NFSCL_DEBUG(4, "aft nfsio_commitds=%d\n", error); 6984 if (error == 0) { 6985 /* 6986 * Set both eof and uio_resid = 0 to end any 6987 * loops. 6988 */ 6989 *eofp = 1; 6990 uiop->uio_resid = 0; 6991 } else { 6992 NFSLOCKNODE(np); 6993 np->n_flag &= ~NDSCOMMIT; 6994 NFSUNLOCKNODE(np); 6995 } 6996 } else if (rwflag == NFSV4OPEN_ACCESSREAD) { 6997 error = nfsrpc_readds(vp, uiop, stateidp, eofp, *dspp, 6998 off, xfer, fhp, 1, dp->nfsdi_vers, 6999 dp->nfsdi_minorvers, tcred, p); 7000 NFSCL_DEBUG(4, "readds=%d\n", error); 7001 if (error != 0 && error != EACCES && error != ESTALE) { 7002 NFSCL_DEBUG(4, "DS layreterr for read\n"); 7003 nfscl_dserr(NFSV4OP_READ, error, dp, lyp, 7004 *dspp); 7005 } 7006 } else { 7007 if (flp->nfsfl_mirrorcnt == 1) { 7008 error = nfsrpc_writeds(vp, uiop, iomode, 7009 must_commit, stateidp, *dspp, off, xfer, 7010 fhp, 0, 1, dp->nfsdi_vers, 7011 dp->nfsdi_minorvers, tcred, p); 7012 if (error == 0) { 7013 NFSLOCKCLSTATE(); 7014 lyp->nfsly_flags |= NFSLY_WRITTEN; 7015 NFSUNLOCKCLSTATE(); 7016 } 7017 } else { 7018 m = mp; 7019 if (xfer < len) { 7020 /* The mbuf list must be split. */ 7021 m2 = nfsm_split(mp, xfer); 7022 if (m2 != NULL) 7023 mp = m2; 7024 else { 7025 m_freem(mp); 7026 error = EIO; 7027 } 7028 } 7029 NFSCL_DEBUG(4, "mcopy len=%jd xfer=%jd\n", 7030 (uintmax_t)len, (uintmax_t)xfer); 7031 /* 7032 * Do last write to a mirrored DS with this 7033 * thread. 7034 */ 7035 if (error == 0) { 7036 if (mirror < flp->nfsfl_mirrorcnt - 1) 7037 error = nfsio_writedsmir(vp, 7038 iomode, must_commit, 7039 stateidp, *dspp, off, 7040 xfer, fhp, m, 7041 dp->nfsdi_vers, 7042 dp->nfsdi_minorvers, drpc, 7043 tcred, p); 7044 else 7045 error = nfsrpc_writedsmir(vp, 7046 iomode, must_commit, 7047 stateidp, *dspp, off, 7048 xfer, fhp, m, 7049 dp->nfsdi_vers, 7050 dp->nfsdi_minorvers, tcred, 7051 p); 7052 } 7053 NFSCL_DEBUG(4, "nfsio_writedsmir=%d\n", error); 7054 if (error != 0 && error != EACCES && error != 7055 ESTALE) { 7056 NFSCL_DEBUG(4, 7057 "DS layreterr for write\n"); 7058 nfscl_dserr(NFSV4OP_WRITE, error, dp, 7059 lyp, *dspp); 7060 } 7061 } 7062 } 7063 NFSCL_DEBUG(4, "aft read/writeds=%d\n", error); 7064 if (error == 0) { 7065 len -= xfer; 7066 off += xfer; 7067 } 7068 if ((dp->nfsdi_flags & NFSDI_TIGHTCOUPLED) == 0) 7069 NFSFREECRED(tcred); 7070 } 7071 NFSCL_DEBUG(4, "eo nfscl_dofflayoutio=%d\n", error); 7072 return (error); 7073 } 7074 7075 /* 7076 * The actual read RPC done to a DS. 7077 */ 7078 static int 7079 nfsrpc_readds(vnode_t vp, struct uio *uiop, nfsv4stateid_t *stateidp, int *eofp, 7080 struct nfsclds *dsp, uint64_t io_off, int len, struct nfsfh *fhp, int flex, 7081 int vers, int minorvers, struct ucred *cred, NFSPROC_T *p) 7082 { 7083 uint32_t *tl; 7084 int attrflag, error, retlen; 7085 struct nfsrv_descript nfsd; 7086 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 7087 struct nfsrv_descript *nd = &nfsd; 7088 struct nfssockreq *nrp; 7089 struct nfsvattr na; 7090 7091 nd->nd_mrep = NULL; 7092 if (vers == 0 || vers == NFS_VER4) { 7093 nfscl_reqstart(nd, NFSPROC_READDS, nmp, fhp->nfh_fh, 7094 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7095 NULL); 7096 vers = NFS_VER4; 7097 NFSCL_DEBUG(4, "nfsrpc_readds: vers4 minvers=%d\n", minorvers); 7098 if (flex != 0) 7099 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 7100 else 7101 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO); 7102 } else { 7103 nfscl_reqstart(nd, NFSPROC_READ, nmp, fhp->nfh_fh, 7104 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7105 NULL); 7106 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READ]); 7107 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_READDS]); 7108 NFSCL_DEBUG(4, "nfsrpc_readds: vers3\n"); 7109 } 7110 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3); 7111 txdr_hyper(io_off, tl); 7112 *(tl + 2) = txdr_unsigned(len); 7113 nrp = dsp->nfsclds_sockp; 7114 NFSCL_DEBUG(4, "nfsrpc_readds: nrp=%p\n", nrp); 7115 if (nrp == NULL) 7116 /* If NULL, use the MDS socket. */ 7117 nrp = &nmp->nm_sockreq; 7118 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 7119 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 7120 NFSCL_DEBUG(4, "nfsrpc_readds: stat=%d err=%d\n", nd->nd_repstat, 7121 error); 7122 if (error != 0) 7123 return (error); 7124 if (vers == NFS_VER3) { 7125 error = nfscl_postop_attr(nd, &na, &attrflag); 7126 NFSCL_DEBUG(4, "nfsrpc_readds: postop=%d\n", error); 7127 if (error != 0) 7128 goto nfsmout; 7129 } 7130 if (nd->nd_repstat != 0) { 7131 error = nd->nd_repstat; 7132 goto nfsmout; 7133 } 7134 if (vers == NFS_VER3) { 7135 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 7136 *eofp = fxdr_unsigned(int, *(tl + 1)); 7137 } else { 7138 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 7139 *eofp = fxdr_unsigned(int, *tl); 7140 } 7141 NFSM_STRSIZ(retlen, len); 7142 NFSCL_DEBUG(4, "nfsrpc_readds: retlen=%d eof=%d\n", retlen, *eofp); 7143 error = nfsm_mbufuio(nd, uiop, retlen); 7144 nfsmout: 7145 if (nd->nd_mrep != NULL) 7146 m_freem(nd->nd_mrep); 7147 return (error); 7148 } 7149 7150 /* 7151 * The actual write RPC done to a DS. 7152 */ 7153 static int 7154 nfsrpc_writeds(vnode_t vp, struct uio *uiop, int *iomode, int *must_commit, 7155 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len, 7156 struct nfsfh *fhp, int commit_thru_mds, int flex, int vers, int minorvers, 7157 struct ucred *cred, NFSPROC_T *p) 7158 { 7159 uint32_t *tl; 7160 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 7161 int attrflag, error, rlen, commit, committed = NFSWRITE_FILESYNC; 7162 int32_t backup; 7163 struct nfsrv_descript nfsd; 7164 struct nfsrv_descript *nd = &nfsd; 7165 struct nfssockreq *nrp; 7166 struct nfsvattr na; 7167 7168 KASSERT(uiop->uio_iovcnt == 1, ("nfs: writerpc iovcnt > 1")); 7169 nd->nd_mrep = NULL; 7170 if (vers == 0 || vers == NFS_VER4) { 7171 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, 7172 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7173 NULL); 7174 NFSCL_DEBUG(4, "nfsrpc_writeds: vers4 minvers=%d\n", minorvers); 7175 vers = NFS_VER4; 7176 if (flex != 0) 7177 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 7178 else 7179 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSEQIDZERO); 7180 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 7181 } else { 7182 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh, 7183 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7184 NULL); 7185 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]); 7186 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]); 7187 NFSCL_DEBUG(4, "nfsrpc_writeds: vers3\n"); 7188 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED); 7189 } 7190 txdr_hyper(io_off, tl); 7191 tl += 2; 7192 if (vers == NFS_VER3) 7193 *tl++ = txdr_unsigned(len); 7194 *tl++ = txdr_unsigned(*iomode); 7195 *tl = txdr_unsigned(len); 7196 error = nfsm_uiombuf(nd, uiop, len); 7197 if (error != 0) { 7198 m_freem(nd->nd_mreq); 7199 return (error); 7200 } 7201 nrp = dsp->nfsclds_sockp; 7202 if (nrp == NULL) 7203 /* If NULL, use the MDS socket. */ 7204 nrp = &nmp->nm_sockreq; 7205 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 7206 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 7207 NFSCL_DEBUG(4, "nfsrpc_writeds: err=%d stat=%d\n", error, 7208 nd->nd_repstat); 7209 if (error != 0) 7210 return (error); 7211 if (nd->nd_repstat != 0) { 7212 /* 7213 * In case the rpc gets retried, roll 7214 * the uio fields changed by nfsm_uiombuf() 7215 * back. 7216 */ 7217 uiop->uio_offset -= len; 7218 uiop->uio_resid += len; 7219 uiop->uio_iov->iov_base = (char *)uiop->uio_iov->iov_base - len; 7220 uiop->uio_iov->iov_len += len; 7221 error = nd->nd_repstat; 7222 } else { 7223 if (vers == NFS_VER3) { 7224 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 7225 NULL); 7226 NFSCL_DEBUG(4, "nfsrpc_writeds: wcc_data=%d\n", error); 7227 if (error != 0) 7228 goto nfsmout; 7229 } 7230 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 7231 rlen = fxdr_unsigned(int, *tl++); 7232 NFSCL_DEBUG(4, "nfsrpc_writeds: len=%d rlen=%d\n", len, rlen); 7233 if (rlen == 0) { 7234 error = NFSERR_IO; 7235 goto nfsmout; 7236 } else if (rlen < len) { 7237 backup = len - rlen; 7238 uiop->uio_iov->iov_base = 7239 (char *)uiop->uio_iov->iov_base - backup; 7240 uiop->uio_iov->iov_len += backup; 7241 uiop->uio_offset -= backup; 7242 uiop->uio_resid += backup; 7243 len = rlen; 7244 } 7245 commit = fxdr_unsigned(int, *tl++); 7246 7247 /* 7248 * Return the lowest commitment level 7249 * obtained by any of the RPCs. 7250 */ 7251 if (committed == NFSWRITE_FILESYNC) 7252 committed = commit; 7253 else if (committed == NFSWRITE_DATASYNC && 7254 commit == NFSWRITE_UNSTABLE) 7255 committed = commit; 7256 if (commit_thru_mds != 0) { 7257 NFSLOCKMNT(nmp); 7258 if (!NFSHASWRITEVERF(nmp)) { 7259 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 7260 NFSSETWRITEVERF(nmp); 7261 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF) && 7262 *must_commit != 2) { 7263 *must_commit = 1; 7264 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 7265 } 7266 NFSUNLOCKMNT(nmp); 7267 } else { 7268 NFSLOCKDS(dsp); 7269 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) { 7270 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 7271 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF; 7272 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF) && 7273 *must_commit != 2) { 7274 *must_commit = 1; 7275 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 7276 } 7277 NFSUNLOCKDS(dsp); 7278 } 7279 } 7280 nfsmout: 7281 if (nd->nd_mrep != NULL) 7282 m_freem(nd->nd_mrep); 7283 *iomode = committed; 7284 if (nd->nd_repstat != 0 && error == 0) 7285 error = nd->nd_repstat; 7286 return (error); 7287 } 7288 7289 /* 7290 * The actual write RPC done to a DS. 7291 * This variant is called from a separate kernel process for mirrors. 7292 * Any short write is considered an IO error. 7293 */ 7294 static int 7295 nfsrpc_writedsmir(vnode_t vp, int *iomode, int *must_commit, 7296 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t io_off, int len, 7297 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers, 7298 struct ucred *cred, NFSPROC_T *p) 7299 { 7300 uint32_t *tl; 7301 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 7302 int attrflag, error, commit, committed = NFSWRITE_FILESYNC, rlen; 7303 struct nfsrv_descript nfsd; 7304 struct nfsrv_descript *nd = &nfsd; 7305 struct nfssockreq *nrp; 7306 struct nfsvattr na; 7307 7308 nd->nd_mrep = NULL; 7309 if (vers == 0 || vers == NFS_VER4) { 7310 nfscl_reqstart(nd, NFSPROC_WRITEDS, nmp, fhp->nfh_fh, 7311 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7312 NULL); 7313 vers = NFS_VER4; 7314 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers4 minvers=%d\n", 7315 minorvers); 7316 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 7317 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 7318 } else { 7319 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, fhp->nfh_fh, 7320 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7321 NULL); 7322 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITE]); 7323 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_WRITEDS]); 7324 NFSCL_DEBUG(4, "nfsrpc_writedsmir: vers3\n"); 7325 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED); 7326 } 7327 txdr_hyper(io_off, tl); 7328 tl += 2; 7329 if (vers == NFS_VER3) 7330 *tl++ = txdr_unsigned(len); 7331 *tl++ = txdr_unsigned(*iomode); 7332 *tl = txdr_unsigned(len); 7333 if (len > 0) { 7334 /* Put data in mbuf chain. */ 7335 nd->nd_mb->m_next = m; 7336 } 7337 nrp = dsp->nfsclds_sockp; 7338 if (nrp == NULL) 7339 /* If NULL, use the MDS socket. */ 7340 nrp = &nmp->nm_sockreq; 7341 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 7342 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 7343 NFSCL_DEBUG(4, "nfsrpc_writedsmir: err=%d stat=%d\n", error, 7344 nd->nd_repstat); 7345 if (error != 0) 7346 return (error); 7347 if (nd->nd_repstat != 0) 7348 error = nd->nd_repstat; 7349 else { 7350 if (vers == NFS_VER3) { 7351 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 7352 NULL); 7353 NFSCL_DEBUG(4, "nfsrpc_writedsmir: wcc_data=%d\n", 7354 error); 7355 if (error != 0) 7356 goto nfsmout; 7357 } 7358 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 7359 rlen = fxdr_unsigned(int, *tl++); 7360 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", len, 7361 rlen); 7362 if (rlen != len) { 7363 error = NFSERR_IO; 7364 NFSCL_DEBUG(4, "nfsrpc_writedsmir: len=%d rlen=%d\n", 7365 len, rlen); 7366 goto nfsmout; 7367 } 7368 commit = fxdr_unsigned(int, *tl++); 7369 7370 /* 7371 * Return the lowest commitment level 7372 * obtained by any of the RPCs. 7373 */ 7374 if (committed == NFSWRITE_FILESYNC) 7375 committed = commit; 7376 else if (committed == NFSWRITE_DATASYNC && 7377 commit == NFSWRITE_UNSTABLE) 7378 committed = commit; 7379 NFSLOCKDS(dsp); 7380 if ((dsp->nfsclds_flags & NFSCLDS_HASWRITEVERF) == 0) { 7381 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 7382 dsp->nfsclds_flags |= NFSCLDS_HASWRITEVERF; 7383 } else if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF) && 7384 *must_commit != 2) { 7385 *must_commit = 1; 7386 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 7387 } 7388 NFSUNLOCKDS(dsp); 7389 } 7390 nfsmout: 7391 if (nd->nd_mrep != NULL) 7392 m_freem(nd->nd_mrep); 7393 *iomode = committed; 7394 if (nd->nd_repstat != 0 && error == 0) 7395 error = nd->nd_repstat; 7396 return (error); 7397 } 7398 7399 /* 7400 * Start up the thread that will execute nfsrpc_writedsmir(). 7401 */ 7402 static void 7403 start_writedsmir(void *arg, int pending) 7404 { 7405 struct nfsclwritedsdorpc *drpc; 7406 7407 drpc = (struct nfsclwritedsdorpc *)arg; 7408 drpc->err = nfsrpc_writedsmir(drpc->vp, &drpc->iomode, 7409 &drpc->must_commit, drpc->stateidp, drpc->dsp, drpc->off, drpc->len, 7410 drpc->fhp, drpc->m, drpc->vers, drpc->minorvers, drpc->cred, 7411 drpc->p); 7412 drpc->done = 1; 7413 crfree(drpc->cred); 7414 NFSCL_DEBUG(4, "start_writedsmir: err=%d\n", drpc->err); 7415 } 7416 7417 /* 7418 * Set up the write DS mirror call for the pNFS I/O thread. 7419 */ 7420 static int 7421 nfsio_writedsmir(vnode_t vp, int *iomode, int *must_commit, 7422 nfsv4stateid_t *stateidp, struct nfsclds *dsp, uint64_t off, int len, 7423 struct nfsfh *fhp, struct mbuf *m, int vers, int minorvers, 7424 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 7425 { 7426 int error, ret; 7427 7428 error = 0; 7429 drpc->done = 0; 7430 drpc->vp = vp; 7431 drpc->iomode = *iomode; 7432 drpc->must_commit = *must_commit; 7433 drpc->stateidp = stateidp; 7434 drpc->dsp = dsp; 7435 drpc->off = off; 7436 drpc->len = len; 7437 drpc->fhp = fhp; 7438 drpc->m = m; 7439 drpc->vers = vers; 7440 drpc->minorvers = minorvers; 7441 drpc->cred = crhold(cred); 7442 drpc->p = p; 7443 drpc->inprog = 0; 7444 ret = EIO; 7445 if (nfs_pnfsiothreads != 0) { 7446 ret = nfs_pnfsio(start_writedsmir, drpc); 7447 NFSCL_DEBUG(4, "nfsio_writedsmir: nfs_pnfsio=%d\n", ret); 7448 } 7449 if (ret != 0) { 7450 error = nfsrpc_writedsmir(vp, iomode, &drpc->must_commit, 7451 stateidp, dsp, off, len, fhp, m, vers, minorvers, cred, p); 7452 crfree(drpc->cred); 7453 } 7454 NFSCL_DEBUG(4, "nfsio_writedsmir: error=%d\n", error); 7455 return (error); 7456 } 7457 7458 /* 7459 * Free up the nfsclds structure. 7460 */ 7461 void 7462 nfscl_freenfsclds(struct nfsclds *dsp) 7463 { 7464 int i; 7465 7466 if (dsp == NULL) 7467 return; 7468 if (dsp->nfsclds_sockp != NULL) { 7469 NFSFREECRED(dsp->nfsclds_sockp->nr_cred); 7470 NFSFREEMUTEX(&dsp->nfsclds_sockp->nr_mtx); 7471 free(dsp->nfsclds_sockp->nr_nam, M_SONAME); 7472 free(dsp->nfsclds_sockp, M_NFSSOCKREQ); 7473 } 7474 NFSFREEMUTEX(&dsp->nfsclds_mtx); 7475 NFSFREEMUTEX(&dsp->nfsclds_sess.nfsess_mtx); 7476 for (i = 0; i < NFSV4_CBSLOTS; i++) { 7477 if (dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply != NULL) 7478 m_freem( 7479 dsp->nfsclds_sess.nfsess_cbslots[i].nfssl_reply); 7480 } 7481 free(dsp, M_NFSCLDS); 7482 } 7483 7484 static enum nfsclds_state 7485 nfscl_getsameserver(struct nfsmount *nmp, struct nfsclds *newdsp, 7486 struct nfsclds **retdspp, uint32_t *sequencep) 7487 { 7488 struct nfsclds *dsp; 7489 int fndseq; 7490 7491 /* 7492 * Search the list of nfsclds structures for one with the same 7493 * server. 7494 */ 7495 fndseq = 0; 7496 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) { 7497 if (dsp->nfsclds_servownlen == newdsp->nfsclds_servownlen && 7498 dsp->nfsclds_servownlen != 0 && 7499 !NFSBCMP(dsp->nfsclds_serverown, newdsp->nfsclds_serverown, 7500 dsp->nfsclds_servownlen) && 7501 dsp->nfsclds_sess.nfsess_defunct == 0) { 7502 NFSCL_DEBUG(4, "fnd same fdsp=%p dsp=%p flg=0x%x\n", 7503 TAILQ_FIRST(&nmp->nm_sess), dsp, 7504 dsp->nfsclds_flags); 7505 if (fndseq == 0) { 7506 /* Get sequenceid# from first entry. */ 7507 *sequencep = 7508 dsp->nfsclds_sess.nfsess_sequenceid; 7509 fndseq = 1; 7510 } 7511 /* Server major id matches. */ 7512 if ((dsp->nfsclds_flags & NFSCLDS_DS) != 0) { 7513 *retdspp = dsp; 7514 return (NFSDSP_USETHISSESSION); 7515 } 7516 } 7517 } 7518 if (fndseq != 0) 7519 return (NFSDSP_SEQTHISSESSION); 7520 return (NFSDSP_NOTFOUND); 7521 } 7522 7523 /* 7524 * NFS commit rpc to a NFSv4.1 DS. 7525 */ 7526 static int 7527 nfsrpc_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp, 7528 struct nfsfh *fhp, int vers, int minorvers, struct ucred *cred, 7529 NFSPROC_T *p) 7530 { 7531 uint32_t *tl; 7532 struct nfsrv_descript nfsd, *nd = &nfsd; 7533 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 7534 struct nfssockreq *nrp; 7535 struct nfsvattr na; 7536 int attrflag, error; 7537 7538 nd->nd_mrep = NULL; 7539 if (vers == 0 || vers == NFS_VER4) { 7540 nfscl_reqstart(nd, NFSPROC_COMMITDS, nmp, fhp->nfh_fh, 7541 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7542 NULL); 7543 vers = NFS_VER4; 7544 } else { 7545 nfscl_reqstart(nd, NFSPROC_COMMIT, nmp, fhp->nfh_fh, 7546 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, 7547 NULL); 7548 NFSDECRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMIT]); 7549 NFSINCRGLOBAL(nfsstatsv1.rpccnt[NFSPROC_COMMITDS]); 7550 } 7551 NFSCL_DEBUG(4, "nfsrpc_commitds: vers=%d minvers=%d\n", vers, 7552 minorvers); 7553 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 7554 txdr_hyper(offset, tl); 7555 tl += 2; 7556 *tl = txdr_unsigned(cnt); 7557 nrp = dsp->nfsclds_sockp; 7558 if (nrp == NULL) 7559 /* If NULL, use the MDS socket. */ 7560 nrp = &nmp->nm_sockreq; 7561 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 7562 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 7563 NFSCL_DEBUG(4, "nfsrpc_commitds: err=%d stat=%d\n", error, 7564 nd->nd_repstat); 7565 if (error != 0) 7566 return (error); 7567 if (nd->nd_repstat == 0) { 7568 if (vers == NFS_VER3) { 7569 error = nfscl_wcc_data(nd, vp, &na, &attrflag, NULL, 7570 NULL); 7571 NFSCL_DEBUG(4, "nfsrpc_commitds: wccdata=%d\n", error); 7572 if (error != 0) 7573 goto nfsmout; 7574 } 7575 NFSM_DISSECT(tl, u_int32_t *, NFSX_VERF); 7576 NFSLOCKDS(dsp); 7577 if (NFSBCMP(tl, dsp->nfsclds_verf, NFSX_VERF)) { 7578 NFSBCOPY(tl, dsp->nfsclds_verf, NFSX_VERF); 7579 error = NFSERR_STALEWRITEVERF; 7580 } 7581 NFSUNLOCKDS(dsp); 7582 } 7583 nfsmout: 7584 if (error == 0 && nd->nd_repstat != 0) 7585 error = nd->nd_repstat; 7586 m_freem(nd->nd_mrep); 7587 return (error); 7588 } 7589 7590 /* 7591 * Start up the thread that will execute nfsrpc_commitds(). 7592 */ 7593 static void 7594 start_commitds(void *arg, int pending) 7595 { 7596 struct nfsclwritedsdorpc *drpc; 7597 7598 drpc = (struct nfsclwritedsdorpc *)arg; 7599 drpc->err = nfsrpc_commitds(drpc->vp, drpc->off, drpc->len, 7600 drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, drpc->cred, 7601 drpc->p); 7602 drpc->done = 1; 7603 crfree(drpc->cred); 7604 NFSCL_DEBUG(4, "start_commitds: err=%d\n", drpc->err); 7605 } 7606 7607 /* 7608 * Set up the commit DS mirror call for the pNFS I/O thread. 7609 */ 7610 static int 7611 nfsio_commitds(vnode_t vp, uint64_t offset, int cnt, struct nfsclds *dsp, 7612 struct nfsfh *fhp, int vers, int minorvers, 7613 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 7614 { 7615 int error, ret; 7616 7617 error = 0; 7618 drpc->done = 0; 7619 drpc->vp = vp; 7620 drpc->off = offset; 7621 drpc->len = cnt; 7622 drpc->dsp = dsp; 7623 drpc->fhp = fhp; 7624 drpc->vers = vers; 7625 drpc->minorvers = minorvers; 7626 drpc->cred = crhold(cred); 7627 drpc->p = p; 7628 drpc->inprog = 0; 7629 ret = EIO; 7630 if (nfs_pnfsiothreads != 0) { 7631 ret = nfs_pnfsio(start_commitds, drpc); 7632 NFSCL_DEBUG(4, "nfsio_commitds: nfs_pnfsio=%d\n", ret); 7633 } 7634 if (ret != 0) { 7635 error = nfsrpc_commitds(vp, offset, cnt, dsp, fhp, vers, 7636 minorvers, cred, p); 7637 crfree(drpc->cred); 7638 } 7639 NFSCL_DEBUG(4, "nfsio_commitds: error=%d\n", error); 7640 return (error); 7641 } 7642 7643 /* 7644 * NFS Advise rpc 7645 */ 7646 int 7647 nfsrpc_advise(vnode_t vp, off_t offset, uint64_t cnt, int advise, 7648 struct ucred *cred, NFSPROC_T *p) 7649 { 7650 u_int32_t *tl; 7651 struct nfsrv_descript nfsd, *nd = &nfsd; 7652 nfsattrbit_t hints; 7653 int error; 7654 7655 NFSZERO_ATTRBIT(&hints); 7656 if (advise == POSIX_FADV_WILLNEED) 7657 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED); 7658 else if (advise == POSIX_FADV_DONTNEED) 7659 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED); 7660 else 7661 return (0); 7662 NFSCL_REQSTART(nd, NFSPROC_IOADVISE, vp, cred); 7663 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO); 7664 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER); 7665 txdr_hyper(offset, tl); 7666 tl += 2; 7667 txdr_hyper(cnt, tl); 7668 nfsrv_putattrbit(nd, &hints); 7669 error = nfscl_request(nd, vp, p, cred); 7670 if (error != 0) 7671 return (error); 7672 if (nd->nd_repstat != 0) 7673 error = nd->nd_repstat; 7674 m_freem(nd->nd_mrep); 7675 return (error); 7676 } 7677 7678 #ifdef notyet 7679 /* 7680 * NFS advise rpc to a NFSv4.2 DS. 7681 */ 7682 static int 7683 nfsrpc_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise, 7684 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers, 7685 struct ucred *cred, NFSPROC_T *p) 7686 { 7687 uint32_t *tl; 7688 struct nfsrv_descript nfsd, *nd = &nfsd; 7689 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 7690 struct nfssockreq *nrp; 7691 nfsattrbit_t hints; 7692 int error; 7693 7694 /* For NFS DSs prior to NFSv4.2, just return OK. */ 7695 if (vers == NFS_VER3 || minorversion < NFSV42_MINORVERSION) 7696 return (0); 7697 NFSZERO_ATTRBIT(&hints); 7698 if (advise == POSIX_FADV_WILLNEED) 7699 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_WILLNEED); 7700 else if (advise == POSIX_FADV_DONTNEED) 7701 NFSSETBIT_ATTRBIT(&hints, NFSV4IOHINT_DONTNEED); 7702 else 7703 return (0); 7704 nd->nd_mrep = NULL; 7705 nfscl_reqstart(nd, NFSPROC_IOADVISEDS, nmp, fhp->nfh_fh, 7706 fhp->nfh_len, NULL, &dsp->nfsclds_sess, vers, minorvers, NULL); 7707 vers = NFS_VER4; 7708 NFSCL_DEBUG(4, "nfsrpc_adviseds: vers=%d minvers=%d\n", vers, 7709 minorvers); 7710 nfsm_stateidtom(nd, NULL, NFSSTATEID_PUTALLZERO); 7711 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 7712 txdr_hyper(offset, tl); 7713 tl += 2; 7714 *tl = txdr_unsigned(cnt); 7715 nfsrv_putattrbit(nd, &hints); 7716 nrp = dsp->nfsclds_sockp; 7717 if (nrp == NULL) 7718 /* If NULL, use the MDS socket. */ 7719 nrp = &nmp->nm_sockreq; 7720 error = newnfs_request(nd, nmp, NULL, nrp, vp, p, cred, 7721 NFS_PROG, vers, NULL, 1, NULL, &dsp->nfsclds_sess); 7722 NFSCL_DEBUG(4, "nfsrpc_adviseds: err=%d stat=%d\n", error, 7723 nd->nd_repstat); 7724 if (error != 0) 7725 return (error); 7726 if (nd->nd_repstat != 0) 7727 error = nd->nd_repstat; 7728 m_freem(nd->nd_mrep); 7729 return (error); 7730 } 7731 7732 /* 7733 * Start up the thread that will execute nfsrpc_commitds(). 7734 */ 7735 static void 7736 start_adviseds(void *arg, int pending) 7737 { 7738 struct nfsclwritedsdorpc *drpc; 7739 7740 drpc = (struct nfsclwritedsdorpc *)arg; 7741 drpc->err = nfsrpc_adviseds(drpc->vp, drpc->off, drpc->len, 7742 drpc->advise, drpc->dsp, drpc->fhp, drpc->vers, drpc->minorvers, 7743 drpc->cred, drpc->p); 7744 drpc->done = 1; 7745 crfree(drpc->cred); 7746 NFSCL_DEBUG(4, "start_adviseds: err=%d\n", drpc->err); 7747 } 7748 7749 /* 7750 * Set up the advise DS mirror call for the pNFS I/O thread. 7751 */ 7752 static int 7753 nfsio_adviseds(vnode_t vp, uint64_t offset, int cnt, int advise, 7754 struct nfsclds *dsp, struct nfsfh *fhp, int vers, int minorvers, 7755 struct nfsclwritedsdorpc *drpc, struct ucred *cred, NFSPROC_T *p) 7756 { 7757 int error, ret; 7758 7759 error = 0; 7760 drpc->done = 0; 7761 drpc->vp = vp; 7762 drpc->off = offset; 7763 drpc->len = cnt; 7764 drpc->advise = advise; 7765 drpc->dsp = dsp; 7766 drpc->fhp = fhp; 7767 drpc->vers = vers; 7768 drpc->minorvers = minorvers; 7769 drpc->cred = crhold(cred); 7770 drpc->p = p; 7771 drpc->inprog = 0; 7772 ret = EIO; 7773 if (nfs_pnfsiothreads != 0) { 7774 ret = nfs_pnfsio(start_adviseds, drpc); 7775 NFSCL_DEBUG(4, "nfsio_adviseds: nfs_pnfsio=%d\n", ret); 7776 } 7777 if (ret != 0) { 7778 error = nfsrpc_adviseds(vp, offset, cnt, advise, dsp, fhp, vers, 7779 minorvers, cred, p); 7780 crfree(drpc->cred); 7781 } 7782 NFSCL_DEBUG(4, "nfsio_adviseds: error=%d\n", error); 7783 return (error); 7784 } 7785 #endif /* notyet */ 7786 7787 /* 7788 * Do the Allocate operation, retrying for recovery. 7789 */ 7790 int 7791 nfsrpc_allocate(vnode_t vp, off_t off, off_t len, struct nfsvattr *nap, 7792 int *attrflagp, struct ucred *cred, NFSPROC_T *p) 7793 { 7794 int error, expireret = 0, retrycnt, nostateid; 7795 uint32_t clidrev = 0; 7796 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 7797 struct nfsfh *nfhp = NULL; 7798 nfsv4stateid_t stateid; 7799 off_t tmp_off; 7800 void *lckp; 7801 7802 if (len < 0) 7803 return (EINVAL); 7804 if (len == 0) 7805 return (0); 7806 tmp_off = off + len; 7807 NFSLOCKMNT(nmp); 7808 if (tmp_off > nmp->nm_maxfilesize || tmp_off < off) { 7809 NFSUNLOCKMNT(nmp); 7810 return (EFBIG); 7811 } 7812 if (nmp->nm_clp != NULL) 7813 clidrev = nmp->nm_clp->nfsc_clientidrev; 7814 NFSUNLOCKMNT(nmp); 7815 nfhp = VTONFS(vp)->n_fhp; 7816 retrycnt = 0; 7817 do { 7818 lckp = NULL; 7819 nostateid = 0; 7820 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 7821 NFSV4OPEN_ACCESSWRITE, 0, cred, p, &stateid, &lckp); 7822 if (stateid.other[0] == 0 && stateid.other[1] == 0 && 7823 stateid.other[2] == 0) { 7824 nostateid = 1; 7825 NFSCL_DEBUG(1, "stateid0 in allocate\n"); 7826 } 7827 7828 /* 7829 * Not finding a stateid should probably never happen, 7830 * but just return an error for this case. 7831 */ 7832 if (nostateid != 0) 7833 error = EIO; 7834 else 7835 error = nfsrpc_allocaterpc(vp, off, len, &stateid, 7836 nap, attrflagp, cred, p); 7837 if (error == NFSERR_STALESTATEID) 7838 nfscl_initiate_recovery(nmp->nm_clp); 7839 if (lckp != NULL) 7840 nfscl_lockderef(lckp); 7841 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 7842 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 7843 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 7844 (void) nfs_catnap(PZERO, error, "nfs_allocate"); 7845 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) && 7846 error == NFSERR_BADSTATEID)) && clidrev != 0) { 7847 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, p); 7848 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) { 7849 error = EIO; 7850 } 7851 retrycnt++; 7852 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 7853 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 7854 error == NFSERR_STALEDONTRECOVER || 7855 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 7856 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 7857 expireret == 0 && clidrev != 0 && retrycnt < 4)); 7858 if (error != 0 && retrycnt >= 4) 7859 error = EIO; 7860 return (error); 7861 } 7862 7863 /* 7864 * The allocate RPC. 7865 */ 7866 static int 7867 nfsrpc_allocaterpc(vnode_t vp, off_t off, off_t len, nfsv4stateid_t *stateidp, 7868 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 7869 { 7870 uint32_t *tl; 7871 int error; 7872 struct nfsrv_descript nfsd; 7873 struct nfsrv_descript *nd = &nfsd; 7874 nfsattrbit_t attrbits; 7875 7876 *attrflagp = 0; 7877 NFSCL_REQSTART(nd, NFSPROC_ALLOCATE, vp, cred); 7878 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 7879 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 7880 txdr_hyper(off, tl); tl += 2; 7881 txdr_hyper(len, tl); tl += 2; 7882 *tl = txdr_unsigned(NFSV4OP_GETATTR); 7883 NFSGETATTR_ATTRBIT(&attrbits); 7884 nfsrv_putattrbit(nd, &attrbits); 7885 error = nfscl_request(nd, vp, p, cred); 7886 if (error != 0) 7887 return (error); 7888 if (nd->nd_repstat == 0) { 7889 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 7890 error = nfsm_loadattr(nd, nap); 7891 if (error == 0) 7892 *attrflagp = NFS_LATTR_NOSHRINK; 7893 } else 7894 error = nd->nd_repstat; 7895 nfsmout: 7896 m_freem(nd->nd_mrep); 7897 return (error); 7898 } 7899 7900 /* 7901 * Set up the XDR arguments for the LayoutGet operation. 7902 */ 7903 static void 7904 nfsrv_setuplayoutget(struct nfsrv_descript *nd, int iomode, uint64_t offset, 7905 uint64_t len, uint64_t minlen, nfsv4stateid_t *stateidp, int layouttype, 7906 int layoutlen, int usecurstateid) 7907 { 7908 uint32_t *tl; 7909 7910 NFSM_BUILD(tl, uint32_t *, 4 * NFSX_UNSIGNED + 3 * NFSX_HYPER + 7911 NFSX_STATEID); 7912 *tl++ = newnfs_false; /* Don't signal availability. */ 7913 *tl++ = txdr_unsigned(layouttype); 7914 *tl++ = txdr_unsigned(iomode); 7915 txdr_hyper(offset, tl); 7916 tl += 2; 7917 txdr_hyper(len, tl); 7918 tl += 2; 7919 txdr_hyper(minlen, tl); 7920 tl += 2; 7921 if (usecurstateid != 0) { 7922 /* Special stateid for Current stateid. */ 7923 *tl++ = txdr_unsigned(1); 7924 *tl++ = 0; 7925 *tl++ = 0; 7926 *tl++ = 0; 7927 } else { 7928 *tl++ = txdr_unsigned(stateidp->seqid); 7929 NFSCL_DEBUG(4, "layget seq=%d\n", (int)stateidp->seqid); 7930 *tl++ = stateidp->other[0]; 7931 *tl++ = stateidp->other[1]; 7932 *tl++ = stateidp->other[2]; 7933 } 7934 *tl = txdr_unsigned(layoutlen); 7935 } 7936 7937 /* 7938 * Parse the reply for a successful LayoutGet operation. 7939 */ 7940 static int 7941 nfsrv_parselayoutget(struct nfsmount *nmp, struct nfsrv_descript *nd, 7942 nfsv4stateid_t *stateidp, int *retonclosep, struct nfsclflayouthead *flhp) 7943 { 7944 uint32_t *tl; 7945 struct nfsclflayout *flp, *prevflp, *tflp; 7946 int cnt, error, fhcnt, gotiomode, i, iomode, j, k, l, laytype, nfhlen; 7947 int m, mirrorcnt; 7948 uint64_t retlen, off; 7949 struct nfsfh *nfhp; 7950 uint8_t *cp; 7951 uid_t user; 7952 gid_t grp; 7953 7954 NFSCL_DEBUG(4, "in nfsrv_parselayoutget\n"); 7955 error = 0; 7956 flp = NULL; 7957 gotiomode = -1; 7958 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_STATEID); 7959 if (*tl++ != 0) 7960 *retonclosep = 1; 7961 else 7962 *retonclosep = 0; 7963 stateidp->seqid = fxdr_unsigned(uint32_t, *tl++); 7964 NFSCL_DEBUG(4, "retoncls=%d stseq=%d\n", *retonclosep, 7965 (int)stateidp->seqid); 7966 stateidp->other[0] = *tl++; 7967 stateidp->other[1] = *tl++; 7968 stateidp->other[2] = *tl++; 7969 cnt = fxdr_unsigned(int, *tl); 7970 NFSCL_DEBUG(4, "layg cnt=%d\n", cnt); 7971 if (cnt <= 0 || cnt > 10000) { 7972 /* Don't accept more than 10000 layouts in reply. */ 7973 error = NFSERR_BADXDR; 7974 goto nfsmout; 7975 } 7976 for (i = 0; i < cnt; i++) { 7977 /* Dissect to the layout type. */ 7978 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 7979 3 * NFSX_UNSIGNED); 7980 off = fxdr_hyper(tl); tl += 2; 7981 retlen = fxdr_hyper(tl); tl += 2; 7982 iomode = fxdr_unsigned(int, *tl++); 7983 laytype = fxdr_unsigned(int, *tl); 7984 NFSCL_DEBUG(4, "layt=%d off=%ju len=%ju iom=%d\n", laytype, 7985 (uintmax_t)off, (uintmax_t)retlen, iomode); 7986 /* Ignore length of layout body for now. */ 7987 if (laytype == NFSLAYOUT_NFSV4_1_FILES) { 7988 /* Parse the File layout up to fhcnt. */ 7989 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + 7990 NFSX_HYPER + NFSX_V4DEVICEID); 7991 fhcnt = fxdr_unsigned(int, *(tl + 4 + 7992 NFSX_V4DEVICEID / NFSX_UNSIGNED)); 7993 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt); 7994 if (fhcnt < 0 || fhcnt > 100) { 7995 /* Don't accept more than 100 file handles. */ 7996 error = NFSERR_BADXDR; 7997 goto nfsmout; 7998 } 7999 if (fhcnt > 0) 8000 flp = malloc(sizeof(*flp) + fhcnt * 8001 sizeof(struct nfsfh *), M_NFSFLAYOUT, 8002 M_WAITOK); 8003 else 8004 flp = malloc(sizeof(*flp), M_NFSFLAYOUT, 8005 M_WAITOK); 8006 flp->nfsfl_flags = NFSFL_FILE; 8007 flp->nfsfl_fhcnt = 0; 8008 flp->nfsfl_devp = NULL; 8009 flp->nfsfl_off = off; 8010 if (flp->nfsfl_off + retlen < flp->nfsfl_off) 8011 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off; 8012 else 8013 flp->nfsfl_end = flp->nfsfl_off + retlen; 8014 flp->nfsfl_iomode = iomode; 8015 if (gotiomode == -1) 8016 gotiomode = flp->nfsfl_iomode; 8017 /* Ignore layout body length for now. */ 8018 NFSBCOPY(tl, flp->nfsfl_dev, NFSX_V4DEVICEID); 8019 tl += (NFSX_V4DEVICEID / NFSX_UNSIGNED); 8020 flp->nfsfl_util = fxdr_unsigned(uint32_t, *tl++); 8021 NFSCL_DEBUG(4, "flutil=0x%x\n", flp->nfsfl_util); 8022 mtx_lock(&nmp->nm_mtx); 8023 if (nmp->nm_minorvers > 1 && (flp->nfsfl_util & 8024 NFSFLAYUTIL_IOADVISE_THRU_MDS) != 0) 8025 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS; 8026 mtx_unlock(&nmp->nm_mtx); 8027 flp->nfsfl_stripe1 = fxdr_unsigned(uint32_t, *tl++); 8028 flp->nfsfl_patoff = fxdr_hyper(tl); tl += 2; 8029 NFSCL_DEBUG(4, "stripe1=%u poff=%ju\n", 8030 flp->nfsfl_stripe1, (uintmax_t)flp->nfsfl_patoff); 8031 for (j = 0; j < fhcnt; j++) { 8032 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8033 nfhlen = fxdr_unsigned(int, *tl); 8034 if (nfhlen <= 0 || nfhlen > NFSX_V4FHMAX) { 8035 error = NFSERR_BADXDR; 8036 goto nfsmout; 8037 } 8038 nfhp = malloc(sizeof(*nfhp) + nfhlen - 1, 8039 M_NFSFH, M_WAITOK); 8040 flp->nfsfl_fh[j] = nfhp; 8041 flp->nfsfl_fhcnt++; 8042 nfhp->nfh_len = nfhlen; 8043 NFSM_DISSECT(cp, uint8_t *, NFSM_RNDUP(nfhlen)); 8044 NFSBCOPY(cp, nfhp->nfh_fh, nfhlen); 8045 } 8046 } else if (laytype == NFSLAYOUT_FLEXFILE) { 8047 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + 8048 NFSX_HYPER); 8049 mirrorcnt = fxdr_unsigned(int, *(tl + 2)); 8050 NFSCL_DEBUG(4, "mirrorcnt=%d\n", mirrorcnt); 8051 if (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS) { 8052 error = NFSERR_BADXDR; 8053 goto nfsmout; 8054 } 8055 flp = malloc(sizeof(*flp) + mirrorcnt * 8056 sizeof(struct nfsffm), M_NFSFLAYOUT, M_WAITOK); 8057 flp->nfsfl_flags = NFSFL_FLEXFILE; 8058 flp->nfsfl_mirrorcnt = mirrorcnt; 8059 for (j = 0; j < mirrorcnt; j++) 8060 flp->nfsfl_ffm[j].devp = NULL; 8061 flp->nfsfl_off = off; 8062 if (flp->nfsfl_off + retlen < flp->nfsfl_off) 8063 flp->nfsfl_end = UINT64_MAX - flp->nfsfl_off; 8064 else 8065 flp->nfsfl_end = flp->nfsfl_off + retlen; 8066 flp->nfsfl_iomode = iomode; 8067 if (gotiomode == -1) 8068 gotiomode = flp->nfsfl_iomode; 8069 flp->nfsfl_stripeunit = fxdr_hyper(tl); 8070 NFSCL_DEBUG(4, "stripeunit=%ju\n", 8071 (uintmax_t)flp->nfsfl_stripeunit); 8072 for (j = 0; j < mirrorcnt; j++) { 8073 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8074 k = fxdr_unsigned(int, *tl); 8075 if (k < 1 || k > 128) { 8076 error = NFSERR_BADXDR; 8077 goto nfsmout; 8078 } 8079 NFSCL_DEBUG(4, "servercnt=%d\n", k); 8080 for (l = 0; l < k; l++) { 8081 NFSM_DISSECT(tl, uint32_t *, 8082 NFSX_V4DEVICEID + NFSX_STATEID + 8083 2 * NFSX_UNSIGNED); 8084 if (l == 0) { 8085 /* Just use the first server. */ 8086 NFSBCOPY(tl, 8087 flp->nfsfl_ffm[j].dev, 8088 NFSX_V4DEVICEID); 8089 tl += (NFSX_V4DEVICEID / 8090 NFSX_UNSIGNED); 8091 tl++; 8092 flp->nfsfl_ffm[j].st.seqid = 8093 *tl++; 8094 flp->nfsfl_ffm[j].st.other[0] = 8095 *tl++; 8096 flp->nfsfl_ffm[j].st.other[1] = 8097 *tl++; 8098 flp->nfsfl_ffm[j].st.other[2] = 8099 *tl++; 8100 NFSCL_DEBUG(4, "st.seqid=%u " 8101 "st.o0=0x%x st.o1=0x%x " 8102 "st.o2=0x%x\n", 8103 flp->nfsfl_ffm[j].st.seqid, 8104 flp->nfsfl_ffm[j].st.other[0], 8105 flp->nfsfl_ffm[j].st.other[1], 8106 flp->nfsfl_ffm[j].st.other[2]); 8107 } else 8108 tl += ((NFSX_V4DEVICEID + 8109 NFSX_STATEID + 8110 NFSX_UNSIGNED) / 8111 NFSX_UNSIGNED); 8112 fhcnt = fxdr_unsigned(int, *tl); 8113 NFSCL_DEBUG(4, "fhcnt=%d\n", fhcnt); 8114 if (fhcnt < 1 || 8115 fhcnt > NFSDEV_MAXVERS) { 8116 error = NFSERR_BADXDR; 8117 goto nfsmout; 8118 } 8119 for (m = 0; m < fhcnt; m++) { 8120 NFSM_DISSECT(tl, uint32_t *, 8121 NFSX_UNSIGNED); 8122 nfhlen = fxdr_unsigned(int, 8123 *tl); 8124 NFSCL_DEBUG(4, "nfhlen=%d\n", 8125 nfhlen); 8126 if (nfhlen <= 0 || nfhlen > 8127 NFSX_V4FHMAX) { 8128 error = NFSERR_BADXDR; 8129 goto nfsmout; 8130 } 8131 NFSM_DISSECT(cp, uint8_t *, 8132 NFSM_RNDUP(nfhlen)); 8133 if (l == 0) { 8134 flp->nfsfl_ffm[j].fhcnt 8135 = fhcnt; 8136 nfhp = malloc( 8137 sizeof(*nfhp) + 8138 nfhlen - 1, M_NFSFH, 8139 M_WAITOK); 8140 flp->nfsfl_ffm[j].fh[m] 8141 = nfhp; 8142 nfhp->nfh_len = nfhlen; 8143 NFSBCOPY(cp, 8144 nfhp->nfh_fh, 8145 nfhlen); 8146 NFSCL_DEBUG(4, 8147 "got fh\n"); 8148 } 8149 } 8150 /* Now, get the ffsd_user/ffds_group. */ 8151 error = nfsrv_parseug(nd, 0, &user, 8152 &grp, curthread); 8153 NFSCL_DEBUG(4, "after parseu=%d\n", 8154 error); 8155 if (error == 0) 8156 error = nfsrv_parseug(nd, 1, 8157 &user, &grp, curthread); 8158 NFSCL_DEBUG(4, "aft parseg=%d\n", 8159 grp); 8160 if (error != 0) 8161 goto nfsmout; 8162 NFSCL_DEBUG(4, "user=%d group=%d\n", 8163 user, grp); 8164 if (l == 0) { 8165 flp->nfsfl_ffm[j].user = user; 8166 flp->nfsfl_ffm[j].group = grp; 8167 NFSCL_DEBUG(4, 8168 "usr=%d grp=%d\n", user, 8169 grp); 8170 } 8171 } 8172 } 8173 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8174 flp->nfsfl_fflags = fxdr_unsigned(uint32_t, *tl++); 8175 #ifdef notnow 8176 /* 8177 * At this time, there is no flag. 8178 * NFSFLEXFLAG_IOADVISE_THRU_MDS might need to be 8179 * added, or it may never exist? 8180 */ 8181 mtx_lock(&nmp->nm_mtx); 8182 if (nmp->nm_minorvers > 1 && (flp->nfsfl_fflags & 8183 NFSFLEXFLAG_IOADVISE_THRU_MDS) != 0) 8184 nmp->nm_privflag |= NFSMNTP_IOADVISETHRUMDS; 8185 mtx_unlock(&nmp->nm_mtx); 8186 #endif 8187 flp->nfsfl_statshint = fxdr_unsigned(uint32_t, *tl); 8188 NFSCL_DEBUG(4, "fflags=0x%x statshint=%d\n", 8189 flp->nfsfl_fflags, flp->nfsfl_statshint); 8190 } else { 8191 error = NFSERR_BADXDR; 8192 goto nfsmout; 8193 } 8194 if (flp->nfsfl_iomode == gotiomode) { 8195 /* Keep the list in increasing offset order. */ 8196 tflp = LIST_FIRST(flhp); 8197 prevflp = NULL; 8198 while (tflp != NULL && 8199 tflp->nfsfl_off < flp->nfsfl_off) { 8200 prevflp = tflp; 8201 tflp = LIST_NEXT(tflp, nfsfl_list); 8202 } 8203 if (prevflp == NULL) 8204 LIST_INSERT_HEAD(flhp, flp, nfsfl_list); 8205 else 8206 LIST_INSERT_AFTER(prevflp, flp, 8207 nfsfl_list); 8208 NFSCL_DEBUG(4, "flp inserted\n"); 8209 } else { 8210 printf("nfscl_layoutget(): got wrong iomode\n"); 8211 nfscl_freeflayout(flp); 8212 } 8213 flp = NULL; 8214 } 8215 nfsmout: 8216 NFSCL_DEBUG(4, "eo nfsrv_parselayoutget=%d\n", error); 8217 if (error != 0 && flp != NULL) 8218 nfscl_freeflayout(flp); 8219 return (error); 8220 } 8221 8222 /* 8223 * Parse a user/group digit string. 8224 */ 8225 static int 8226 nfsrv_parseug(struct nfsrv_descript *nd, int dogrp, uid_t *uidp, gid_t *gidp, 8227 NFSPROC_T *p) 8228 { 8229 uint32_t *tl; 8230 char *cp, *str, str0[NFSV4_SMALLSTR + 1]; 8231 uint32_t len = 0; 8232 int error = 0; 8233 8234 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8235 len = fxdr_unsigned(uint32_t, *tl); 8236 str = NULL; 8237 if (len > NFSV4_OPAQUELIMIT) { 8238 error = NFSERR_BADXDR; 8239 goto nfsmout; 8240 } 8241 NFSCL_DEBUG(4, "nfsrv_parseug: len=%d\n", len); 8242 if (len == 0) { 8243 if (dogrp != 0) 8244 *gidp = GID_NOGROUP; 8245 else 8246 *uidp = UID_NOBODY; 8247 return (0); 8248 } 8249 if (len > NFSV4_SMALLSTR) 8250 str = malloc(len + 1, M_TEMP, M_WAITOK); 8251 else 8252 str = str0; 8253 NFSM_DISSECT(cp, char *, NFSM_RNDUP(len)); 8254 NFSBCOPY(cp, str, len); 8255 str[len] = '\0'; 8256 NFSCL_DEBUG(4, "nfsrv_parseug: str=%s\n", str); 8257 if (dogrp != 0) 8258 error = nfsv4_strtogid(nd, str, len, gidp); 8259 else 8260 error = nfsv4_strtouid(nd, str, len, uidp); 8261 nfsmout: 8262 if (len > NFSV4_SMALLSTR) 8263 free(str, M_TEMP); 8264 NFSCL_DEBUG(4, "eo nfsrv_parseug=%d\n", error); 8265 return (error); 8266 } 8267 8268 /* 8269 * Similar to nfsrpc_getlayout(), except that it uses nfsrpc_openlayget(), 8270 * so that it does both an Open and a Layoutget. 8271 */ 8272 static int 8273 nfsrpc_getopenlayout(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, 8274 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode, 8275 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp, 8276 struct ucred *cred, NFSPROC_T *p) 8277 { 8278 struct nfscllayout *lyp; 8279 struct nfsclflayout *flp; 8280 struct nfsclflayouthead flh; 8281 int error, islocked, layoutlen, recalled, retonclose, usecurstateid; 8282 int layouttype, laystat; 8283 nfsv4stateid_t stateid; 8284 struct nfsclsession *tsep; 8285 8286 error = 0; 8287 if (NFSHASFLEXFILE(nmp)) 8288 layouttype = NFSLAYOUT_FLEXFILE; 8289 else 8290 layouttype = NFSLAYOUT_NFSV4_1_FILES; 8291 /* 8292 * If lyp is returned non-NULL, there will be a refcnt (shared lock) 8293 * on it, iff flp != NULL or a lock (exclusive lock) on it iff 8294 * flp == NULL. 8295 */ 8296 lyp = nfscl_getlayout(nmp->nm_clp, newfhp, newfhlen, 0, mode, &flp, 8297 &recalled); 8298 NFSCL_DEBUG(4, "nfsrpc_getopenlayout nfscl_getlayout lyp=%p\n", lyp); 8299 if (lyp == NULL) 8300 islocked = 0; 8301 else if (flp != NULL) 8302 islocked = 1; 8303 else 8304 islocked = 2; 8305 if ((lyp == NULL || flp == NULL) && recalled == 0) { 8306 LIST_INIT(&flh); 8307 tsep = nfsmnt_mdssession(nmp); 8308 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 8309 3 * NFSX_UNSIGNED); 8310 if (lyp == NULL) 8311 usecurstateid = 1; 8312 else { 8313 usecurstateid = 0; 8314 stateid.seqid = lyp->nfsly_stateid.seqid; 8315 stateid.other[0] = lyp->nfsly_stateid.other[0]; 8316 stateid.other[1] = lyp->nfsly_stateid.other[1]; 8317 stateid.other[2] = lyp->nfsly_stateid.other[2]; 8318 } 8319 error = nfsrpc_openlayoutrpc(nmp, vp, nfhp, fhlen, 8320 newfhp, newfhlen, mode, op, name, namelen, 8321 dpp, &stateid, usecurstateid, layouttype, layoutlen, 8322 &retonclose, &flh, &laystat, cred, p); 8323 NFSCL_DEBUG(4, "aft nfsrpc_openlayoutrpc laystat=%d err=%d\n", 8324 laystat, error); 8325 laystat = nfsrpc_layoutgetres(nmp, vp, newfhp, newfhlen, 8326 &stateid, retonclose, NULL, &lyp, &flh, layouttype, laystat, 8327 &islocked, cred, p); 8328 } else 8329 error = nfsrpc_openrpc(nmp, vp, nfhp, fhlen, newfhp, newfhlen, 8330 mode, op, name, namelen, dpp, 0, 0, cred, p, 0, 0); 8331 if (islocked == 2) 8332 nfscl_rellayout(lyp, 1); 8333 else if (islocked == 1) 8334 nfscl_rellayout(lyp, 0); 8335 return (error); 8336 } 8337 8338 /* 8339 * This function does an Open+LayoutGet for an NFSv4.1 mount with pNFS 8340 * enabled, only for the CLAIM_NULL case. All other NFSv4 Opens are 8341 * handled by nfsrpc_openrpc(). 8342 * For the case where op == NULL, dvp is the directory. When op != NULL, it 8343 * can be NULL. 8344 */ 8345 static int 8346 nfsrpc_openlayoutrpc(struct nfsmount *nmp, vnode_t vp, u_int8_t *nfhp, 8347 int fhlen, uint8_t *newfhp, int newfhlen, uint32_t mode, 8348 struct nfsclopen *op, uint8_t *name, int namelen, struct nfscldeleg **dpp, 8349 nfsv4stateid_t *stateidp, int usecurstateid, int layouttype, 8350 int layoutlen, int *retonclosep, struct nfsclflayouthead *flhp, 8351 int *laystatp, struct ucred *cred, NFSPROC_T *p) 8352 { 8353 uint32_t *tl; 8354 struct nfsrv_descript nfsd, *nd = &nfsd; 8355 struct nfscldeleg *ndp = NULL; 8356 struct nfsvattr nfsva; 8357 struct nfsclsession *tsep; 8358 uint32_t rflags, deleg; 8359 nfsattrbit_t attrbits; 8360 int error, ret, acesize, limitby, iomode; 8361 8362 *dpp = NULL; 8363 *laystatp = ENXIO; 8364 nfscl_reqstart(nd, NFSPROC_OPENLAYGET, nmp, nfhp, fhlen, NULL, NULL, 8365 0, 0, cred); 8366 NFSM_BUILD(tl, uint32_t *, 5 * NFSX_UNSIGNED); 8367 *tl++ = txdr_unsigned(op->nfso_own->nfsow_seqid); 8368 *tl++ = txdr_unsigned(mode & (NFSV4OPEN_ACCESSBOTH | 8369 NFSV4OPEN_WANTDELEGMASK)); 8370 *tl++ = txdr_unsigned((mode >> NFSLCK_SHIFT) & NFSV4OPEN_DENYBOTH); 8371 tsep = nfsmnt_mdssession(nmp); 8372 *tl++ = tsep->nfsess_clientid.lval[0]; 8373 *tl = tsep->nfsess_clientid.lval[1]; 8374 nfsm_strtom(nd, op->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN); 8375 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8376 *tl++ = txdr_unsigned(NFSV4OPEN_NOCREATE); 8377 if (NFSHASNFSV4N(nmp)) { 8378 *tl = txdr_unsigned(NFSV4OPEN_CLAIMFH); 8379 } else { 8380 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 8381 nfsm_strtom(nd, name, namelen); 8382 } 8383 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8384 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8385 NFSZERO_ATTRBIT(&attrbits); 8386 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 8387 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 8388 nfsrv_putattrbit(nd, &attrbits); 8389 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 8390 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET); 8391 if ((mode & NFSV4OPEN_ACCESSWRITE) != 0) 8392 iomode = NFSLAYOUTIOMODE_RW; 8393 else 8394 iomode = NFSLAYOUTIOMODE_READ; 8395 nfsrv_setuplayoutget(nd, iomode, 0, UINT64_MAX, 0, stateidp, 8396 layouttype, layoutlen, usecurstateid); 8397 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 8398 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 8399 if (error != 0) 8400 return (error); 8401 NFSCL_INCRSEQID(op->nfso_own->nfsow_seqid, nd); 8402 if (nd->nd_repstat != 0) 8403 *laystatp = nd->nd_repstat; 8404 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8405 /* ND_NOMOREDATA will be set if the Open operation failed. */ 8406 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 8407 6 * NFSX_UNSIGNED); 8408 op->nfso_stateid.seqid = *tl++; 8409 op->nfso_stateid.other[0] = *tl++; 8410 op->nfso_stateid.other[1] = *tl++; 8411 op->nfso_stateid.other[2] = *tl; 8412 rflags = fxdr_unsigned(u_int32_t, *(tl + 6)); 8413 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 8414 if (error != 0) 8415 goto nfsmout; 8416 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 8417 deleg = fxdr_unsigned(u_int32_t, *tl); 8418 if (deleg == NFSV4OPEN_DELEGATEREAD || 8419 deleg == NFSV4OPEN_DELEGATEWRITE) { 8420 if (!(op->nfso_own->nfsow_clp->nfsc_flags & 8421 NFSCLFLAGS_FIRSTDELEG)) 8422 op->nfso_own->nfsow_clp->nfsc_flags |= 8423 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 8424 ndp = malloc(sizeof(struct nfscldeleg) + newfhlen, 8425 M_NFSCLDELEG, M_WAITOK); 8426 LIST_INIT(&ndp->nfsdl_owner); 8427 LIST_INIT(&ndp->nfsdl_lock); 8428 ndp->nfsdl_clp = op->nfso_own->nfsow_clp; 8429 ndp->nfsdl_fhlen = newfhlen; 8430 NFSBCOPY(newfhp, ndp->nfsdl_fh, newfhlen); 8431 newnfs_copyincred(cred, &ndp->nfsdl_cred); 8432 nfscl_lockinit(&ndp->nfsdl_rwlock); 8433 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 8434 NFSX_UNSIGNED); 8435 ndp->nfsdl_stateid.seqid = *tl++; 8436 ndp->nfsdl_stateid.other[0] = *tl++; 8437 ndp->nfsdl_stateid.other[1] = *tl++; 8438 ndp->nfsdl_stateid.other[2] = *tl++; 8439 ret = fxdr_unsigned(int, *tl); 8440 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 8441 ndp->nfsdl_flags = NFSCLDL_WRITE; 8442 /* 8443 * Indicates how much the file can grow. 8444 */ 8445 NFSM_DISSECT(tl, u_int32_t *, 8446 3 * NFSX_UNSIGNED); 8447 limitby = fxdr_unsigned(int, *tl++); 8448 switch (limitby) { 8449 case NFSV4OPEN_LIMITSIZE: 8450 ndp->nfsdl_sizelimit = fxdr_hyper(tl); 8451 break; 8452 case NFSV4OPEN_LIMITBLOCKS: 8453 ndp->nfsdl_sizelimit = 8454 fxdr_unsigned(u_int64_t, *tl++); 8455 ndp->nfsdl_sizelimit *= 8456 fxdr_unsigned(u_int64_t, *tl); 8457 break; 8458 default: 8459 error = NFSERR_BADXDR; 8460 goto nfsmout; 8461 }; 8462 } else 8463 ndp->nfsdl_flags = NFSCLDL_READ; 8464 if (ret != 0) 8465 ndp->nfsdl_flags |= NFSCLDL_RECALL; 8466 error = nfsrv_dissectace(nd, &ndp->nfsdl_ace, false, 8467 &ret, &acesize, p); 8468 if (error != 0) 8469 goto nfsmout; 8470 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT && 8471 NFSHASNFSV4N(nmp)) { 8472 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8473 deleg = fxdr_unsigned(uint32_t, *tl); 8474 if (deleg == NFSV4OPEN_CONTENTION || 8475 deleg == NFSV4OPEN_RESOURCE) 8476 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8477 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 8478 error = NFSERR_BADXDR; 8479 goto nfsmout; 8480 } 8481 if ((rflags & NFSV4OPEN_LOCKTYPEPOSIX) != 0 || 8482 nfscl_assumeposixlocks) 8483 op->nfso_posixlock = 1; 8484 else 8485 op->nfso_posixlock = 0; 8486 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 8487 /* If the 2nd element == NFS_OK, the Getattr succeeded. */ 8488 if (*++tl == 0) { 8489 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 8490 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 8491 NULL, NULL, NULL, NULL, p, cred); 8492 if (error != 0) 8493 goto nfsmout; 8494 if (ndp != NULL) { 8495 ndp->nfsdl_change = nfsva.na_filerev; 8496 ndp->nfsdl_modtime = nfsva.na_mtime; 8497 ndp->nfsdl_flags |= NFSCLDL_MODTIMESET; 8498 *dpp = ndp; 8499 ndp = NULL; 8500 } 8501 /* 8502 * At this point, the Open has succeeded, so set 8503 * nd_repstat = NFS_OK. If the Layoutget failed, 8504 * this function just won't return a layout. 8505 */ 8506 if (nd->nd_repstat == 0) { 8507 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8508 *laystatp = fxdr_unsigned(int, *++tl); 8509 if (*laystatp == 0) { 8510 error = nfsrv_parselayoutget(nmp, nd, 8511 stateidp, retonclosep, flhp); 8512 if (error != 0) 8513 *laystatp = error; 8514 } 8515 } else 8516 nd->nd_repstat = 0; /* Return 0 for Open. */ 8517 } 8518 } 8519 if (nd->nd_repstat != 0 && error == 0) 8520 error = nd->nd_repstat; 8521 nfsmout: 8522 free(ndp, M_NFSCLDELEG); 8523 m_freem(nd->nd_mrep); 8524 return (error); 8525 } 8526 8527 /* 8528 * Similar nfsrpc_createv4(), but also does the LayoutGet operation. 8529 * Used only for mounts with pNFS enabled. 8530 */ 8531 static int 8532 nfsrpc_createlayout(vnode_t dvp, char *name, int namelen, struct vattr *vap, 8533 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 8534 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 8535 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 8536 int *dattrflagp, int *unlockedp, nfsv4stateid_t *stateidp, 8537 int usecurstateid, int layouttype, int layoutlen, int *retonclosep, 8538 struct nfsclflayouthead *flhp, int *laystatp) 8539 { 8540 uint32_t *tl; 8541 int error = 0, deleg, newone, ret, acesize, limitby; 8542 struct nfsrv_descript nfsd, *nd = &nfsd; 8543 struct nfsclopen *op; 8544 struct nfscldeleg *dp = NULL; 8545 struct nfsnode *np; 8546 struct nfsfh *nfhp; 8547 struct nfsclsession *tsep; 8548 nfsattrbit_t attrbits; 8549 nfsv4stateid_t stateid; 8550 struct nfsmount *nmp; 8551 8552 nmp = VFSTONFS(dvp->v_mount); 8553 np = VTONFS(dvp); 8554 *laystatp = ENXIO; 8555 *unlockedp = 0; 8556 *nfhpp = NULL; 8557 *dpp = NULL; 8558 *attrflagp = 0; 8559 *dattrflagp = 0; 8560 if (namelen > NFS_MAXNAMLEN) 8561 return (ENAMETOOLONG); 8562 NFSCL_REQSTART(nd, NFSPROC_CREATELAYGET, dvp, cred); 8563 /* 8564 * For V4, this is actually an Open op. 8565 */ 8566 NFSM_BUILD(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 8567 *tl++ = txdr_unsigned(owp->nfsow_seqid); 8568 if (NFSHASNFSV4N(nmp)) { 8569 if (!NFSHASPNFS(nmp) && nfscl_enablecallb != 0 && 8570 nfs_numnfscbd > 0) 8571 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 8572 NFSV4OPEN_ACCESSREAD | NFSV4OPEN_WANTWRITEDELEG); 8573 else 8574 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 8575 NFSV4OPEN_ACCESSREAD | NFSV4OPEN_WANTNODELEG); 8576 } else 8577 *tl++ = txdr_unsigned(NFSV4OPEN_ACCESSWRITE | 8578 NFSV4OPEN_ACCESSREAD); 8579 *tl++ = txdr_unsigned(NFSV4OPEN_DENYNONE); 8580 tsep = nfsmnt_mdssession(nmp); 8581 *tl++ = tsep->nfsess_clientid.lval[0]; 8582 *tl = tsep->nfsess_clientid.lval[1]; 8583 nfsm_strtom(nd, owp->nfsow_owner, NFSV4CL_LOCKNAMELEN); 8584 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 8585 *tl++ = txdr_unsigned(NFSV4OPEN_CREATE); 8586 if ((fmode & O_EXCL) != 0) { 8587 if (NFSHASSESSPERSIST(nmp)) { 8588 /* Use GUARDED for persistent sessions. */ 8589 *tl = txdr_unsigned(NFSCREATE_GUARDED); 8590 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0); 8591 } else { 8592 /* Otherwise, use EXCLUSIVE4_1. */ 8593 *tl = txdr_unsigned(NFSCREATE_EXCLUSIVE41); 8594 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 8595 *tl++ = cverf.lval[0]; 8596 *tl = cverf.lval[1]; 8597 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0); 8598 } 8599 } else { 8600 *tl = txdr_unsigned(NFSCREATE_UNCHECKED); 8601 nfscl_fillsattr(nd, vap, dvp, NFSSATTR_NEWFILE, 0); 8602 } 8603 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 8604 *tl = txdr_unsigned(NFSV4OPEN_CLAIMNULL); 8605 nfsm_strtom(nd, name, namelen); 8606 /* Get the new file's handle and attributes, plus save the FH. */ 8607 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 8608 *tl++ = txdr_unsigned(NFSV4OP_SAVEFH); 8609 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 8610 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8611 NFSGETATTR_ATTRBIT(&attrbits); 8612 nfsrv_putattrbit(nd, &attrbits); 8613 /* Get the directory's post-op attributes. */ 8614 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 8615 *tl = txdr_unsigned(NFSV4OP_PUTFH); 8616 (void)nfsm_fhtom(nmp, nd, np->n_fhp->nfh_fh, np->n_fhp->nfh_len, 0); 8617 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 8618 *tl = txdr_unsigned(NFSV4OP_GETATTR); 8619 nfsrv_putattrbit(nd, &attrbits); 8620 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 8621 *tl++ = txdr_unsigned(NFSV4OP_RESTOREFH); 8622 *tl = txdr_unsigned(NFSV4OP_LAYOUTGET); 8623 nfsrv_setuplayoutget(nd, NFSLAYOUTIOMODE_RW, 0, UINT64_MAX, 0, stateidp, 8624 layouttype, layoutlen, usecurstateid); 8625 error = nfscl_request(nd, dvp, p, cred); 8626 if (error != 0) 8627 return (error); 8628 NFSCL_DEBUG(4, "nfsrpc_createlayout stat=%d err=%d\n", nd->nd_repstat, 8629 error); 8630 if (nd->nd_repstat != 0) 8631 *laystatp = nd->nd_repstat; 8632 NFSCL_INCRSEQID(owp->nfsow_seqid, nd); 8633 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8634 NFSCL_DEBUG(4, "nfsrpc_createlayout open succeeded\n"); 8635 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 8636 6 * NFSX_UNSIGNED); 8637 stateid.seqid = *tl++; 8638 stateid.other[0] = *tl++; 8639 stateid.other[1] = *tl++; 8640 stateid.other[2] = *tl; 8641 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 8642 if (error != 0) 8643 goto nfsmout; 8644 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 8645 deleg = fxdr_unsigned(int, *tl); 8646 if (deleg == NFSV4OPEN_DELEGATEREAD || 8647 deleg == NFSV4OPEN_DELEGATEWRITE) { 8648 if (!(owp->nfsow_clp->nfsc_flags & 8649 NFSCLFLAGS_FIRSTDELEG)) 8650 owp->nfsow_clp->nfsc_flags |= 8651 (NFSCLFLAGS_FIRSTDELEG | NFSCLFLAGS_GOTDELEG); 8652 dp = malloc(sizeof(struct nfscldeleg) + NFSX_V4FHMAX, 8653 M_NFSCLDELEG, M_WAITOK); 8654 LIST_INIT(&dp->nfsdl_owner); 8655 LIST_INIT(&dp->nfsdl_lock); 8656 dp->nfsdl_clp = owp->nfsow_clp; 8657 newnfs_copyincred(cred, &dp->nfsdl_cred); 8658 nfscl_lockinit(&dp->nfsdl_rwlock); 8659 NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID + 8660 NFSX_UNSIGNED); 8661 dp->nfsdl_stateid.seqid = *tl++; 8662 dp->nfsdl_stateid.other[0] = *tl++; 8663 dp->nfsdl_stateid.other[1] = *tl++; 8664 dp->nfsdl_stateid.other[2] = *tl++; 8665 ret = fxdr_unsigned(int, *tl); 8666 if (deleg == NFSV4OPEN_DELEGATEWRITE) { 8667 dp->nfsdl_flags = NFSCLDL_WRITE; 8668 /* 8669 * Indicates how much the file can grow. 8670 */ 8671 NFSM_DISSECT(tl, u_int32_t *, 8672 3 * NFSX_UNSIGNED); 8673 limitby = fxdr_unsigned(int, *tl++); 8674 switch (limitby) { 8675 case NFSV4OPEN_LIMITSIZE: 8676 dp->nfsdl_sizelimit = fxdr_hyper(tl); 8677 break; 8678 case NFSV4OPEN_LIMITBLOCKS: 8679 dp->nfsdl_sizelimit = 8680 fxdr_unsigned(u_int64_t, *tl++); 8681 dp->nfsdl_sizelimit *= 8682 fxdr_unsigned(u_int64_t, *tl); 8683 break; 8684 default: 8685 error = NFSERR_BADXDR; 8686 goto nfsmout; 8687 }; 8688 } else { 8689 dp->nfsdl_flags = NFSCLDL_READ; 8690 } 8691 if (ret != 0) 8692 dp->nfsdl_flags |= NFSCLDL_RECALL; 8693 error = nfsrv_dissectace(nd, &dp->nfsdl_ace, false, 8694 &ret, &acesize, p); 8695 if (error != 0) 8696 goto nfsmout; 8697 } else if (deleg == NFSV4OPEN_DELEGATENONEEXT && 8698 NFSHASNFSV4N(nmp)) { 8699 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8700 deleg = fxdr_unsigned(uint32_t, *tl); 8701 if (deleg == NFSV4OPEN_CONTENTION || 8702 deleg == NFSV4OPEN_RESOURCE) 8703 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 8704 } else if (deleg != NFSV4OPEN_DELEGATENONE) { 8705 error = NFSERR_BADXDR; 8706 goto nfsmout; 8707 } 8708 8709 /* Now, we should have the status for the SaveFH. */ 8710 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8711 if (*++tl == 0) { 8712 NFSCL_DEBUG(4, "nfsrpc_createlayout SaveFH ok\n"); 8713 /* 8714 * Now, process the GetFH and Getattr for the newly 8715 * created file. nfscl_mtofh() will set 8716 * ND_NOMOREDATA if these weren't successful. 8717 */ 8718 error = nfscl_mtofh(nd, nfhpp, nnap, attrflagp); 8719 NFSCL_DEBUG(4, "aft nfscl_mtofh err=%d\n", error); 8720 if (error != 0) 8721 goto nfsmout; 8722 } else 8723 nd->nd_flag |= ND_NOMOREDATA; 8724 /* Now we have the PutFH and Getattr for the directory. */ 8725 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8726 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 8727 if (*++tl != 0) 8728 nd->nd_flag |= ND_NOMOREDATA; 8729 else { 8730 NFSM_DISSECT(tl, uint32_t *, 2 * 8731 NFSX_UNSIGNED); 8732 if (*++tl != 0) 8733 nd->nd_flag |= ND_NOMOREDATA; 8734 } 8735 } 8736 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 8737 /* Load the directory attributes. */ 8738 error = nfsm_loadattr(nd, dnap); 8739 NFSCL_DEBUG(4, "aft nfsm_loadattr err=%d\n", error); 8740 if (error != 0) 8741 goto nfsmout; 8742 *dattrflagp = 1; 8743 if (dp != NULL && *attrflagp != 0) { 8744 dp->nfsdl_change = nnap->na_filerev; 8745 dp->nfsdl_modtime = nnap->na_mtime; 8746 dp->nfsdl_flags |= NFSCLDL_MODTIMESET; 8747 } 8748 /* 8749 * We can now complete the Open state. 8750 */ 8751 nfhp = *nfhpp; 8752 if (dp != NULL) { 8753 dp->nfsdl_fhlen = nfhp->nfh_len; 8754 NFSBCOPY(nfhp->nfh_fh, dp->nfsdl_fh, 8755 nfhp->nfh_len); 8756 } 8757 /* 8758 * Get an Open structure that will be 8759 * attached to the OpenOwner, acquired already. 8760 */ 8761 error = nfscl_open(dvp, nfhp->nfh_fh, nfhp->nfh_len, 8762 (NFSV4OPEN_ACCESSWRITE | NFSV4OPEN_ACCESSREAD), 0, 8763 cred, p, NULL, &op, &newone, NULL, 0, false); 8764 if (error != 0) 8765 goto nfsmout; 8766 op->nfso_stateid = stateid; 8767 newnfs_copyincred(cred, &op->nfso_cred); 8768 8769 nfscl_openrelease(nmp, op, error, newone); 8770 *unlockedp = 1; 8771 8772 /* Now, handle the RestoreFH and LayoutGet. */ 8773 if (nd->nd_repstat == 0) { 8774 NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED); 8775 *laystatp = fxdr_unsigned(int, *(tl + 3)); 8776 if (*laystatp == 0) { 8777 error = nfsrv_parselayoutget(nmp, nd, 8778 stateidp, retonclosep, flhp); 8779 if (error != 0) 8780 *laystatp = error; 8781 } 8782 NFSCL_DEBUG(4, "aft nfsrv_parselayout err=%d\n", 8783 error); 8784 } else 8785 nd->nd_repstat = 0; 8786 } 8787 } 8788 if (nd->nd_repstat != 0 && error == 0) 8789 error = nd->nd_repstat; 8790 if (error == NFSERR_STALECLIENTID) 8791 nfscl_initiate_recovery(owp->nfsow_clp); 8792 nfsmout: 8793 NFSCL_DEBUG(4, "eo nfsrpc_createlayout err=%d\n", error); 8794 if (error == 0) 8795 *dpp = dp; 8796 else 8797 free(dp, M_NFSCLDELEG); 8798 m_freem(nd->nd_mrep); 8799 return (error); 8800 } 8801 8802 /* 8803 * Similar to nfsrpc_getopenlayout(), except that it used for the Create case. 8804 */ 8805 static int 8806 nfsrpc_getcreatelayout(vnode_t dvp, char *name, int namelen, struct vattr *vap, 8807 nfsquad_t cverf, int fmode, struct nfsclowner *owp, struct nfscldeleg **dpp, 8808 struct ucred *cred, NFSPROC_T *p, struct nfsvattr *dnap, 8809 struct nfsvattr *nnap, struct nfsfh **nfhpp, int *attrflagp, 8810 int *dattrflagp, int *unlockedp) 8811 { 8812 struct nfscllayout *lyp; 8813 struct nfsclflayouthead flh; 8814 struct nfsfh *nfhp; 8815 struct nfsclsession *tsep; 8816 struct nfsmount *nmp; 8817 nfsv4stateid_t stateid; 8818 int error, layoutlen, layouttype, retonclose, laystat; 8819 8820 error = 0; 8821 nmp = VFSTONFS(dvp->v_mount); 8822 if (NFSHASFLEXFILE(nmp)) 8823 layouttype = NFSLAYOUT_FLEXFILE; 8824 else 8825 layouttype = NFSLAYOUT_NFSV4_1_FILES; 8826 LIST_INIT(&flh); 8827 tsep = nfsmnt_mdssession(nmp); 8828 layoutlen = tsep->nfsess_maxcache - (NFSX_STATEID + 3 * NFSX_UNSIGNED); 8829 error = nfsrpc_createlayout(dvp, name, namelen, vap, cverf, fmode, 8830 owp, dpp, cred, p, dnap, nnap, nfhpp, attrflagp, dattrflagp, 8831 unlockedp, &stateid, 1, layouttype, layoutlen, &retonclose, 8832 &flh, &laystat); 8833 NFSCL_DEBUG(4, "aft nfsrpc_createlayoutrpc laystat=%d err=%d\n", 8834 laystat, error); 8835 lyp = NULL; 8836 if (laystat == 0) { 8837 nfhp = *nfhpp; 8838 laystat = nfsrpc_layoutgetres(nmp, dvp, nfhp->nfh_fh, 8839 nfhp->nfh_len, &stateid, retonclose, NULL, &lyp, &flh, 8840 layouttype, laystat, NULL, cred, p); 8841 } else 8842 laystat = nfsrpc_layoutgetres(nmp, dvp, NULL, 0, &stateid, 8843 retonclose, NULL, &lyp, &flh, layouttype, laystat, NULL, 8844 cred, p); 8845 if (laystat == 0) 8846 nfscl_rellayout(lyp, 0); 8847 return (error); 8848 } 8849 8850 /* 8851 * Process the results of a layoutget() operation. 8852 */ 8853 static int 8854 nfsrpc_layoutgetres(struct nfsmount *nmp, vnode_t vp, uint8_t *newfhp, 8855 int newfhlen, nfsv4stateid_t *stateidp, int retonclose, uint32_t *notifybit, 8856 struct nfscllayout **lypp, struct nfsclflayouthead *flhp, int layouttype, 8857 int laystat, int *islockedp, struct ucred *cred, NFSPROC_T *p) 8858 { 8859 struct nfsclflayout *tflp; 8860 struct nfscldevinfo *dip; 8861 uint8_t *dev; 8862 int i, mirrorcnt; 8863 8864 if (laystat == NFSERR_UNKNLAYOUTTYPE) { 8865 NFSLOCKMNT(nmp); 8866 if (!NFSHASFLEXFILE(nmp)) { 8867 /* Switch to using Flex File Layout. */ 8868 nmp->nm_state |= NFSSTA_FLEXFILE; 8869 } else if (layouttype == NFSLAYOUT_FLEXFILE) { 8870 /* Disable pNFS. */ 8871 NFSCL_DEBUG(1, "disable PNFS\n"); 8872 nmp->nm_state &= ~(NFSSTA_PNFS | NFSSTA_FLEXFILE); 8873 } 8874 NFSUNLOCKMNT(nmp); 8875 } 8876 if (laystat == 0) { 8877 NFSCL_DEBUG(4, "nfsrpc_layoutgetres at FOREACH\n"); 8878 LIST_FOREACH(tflp, flhp, nfsfl_list) { 8879 if (layouttype == NFSLAYOUT_FLEXFILE) 8880 mirrorcnt = tflp->nfsfl_mirrorcnt; 8881 else 8882 mirrorcnt = 1; 8883 for (i = 0; i < mirrorcnt; i++) { 8884 laystat = nfscl_adddevinfo(nmp, NULL, i, tflp); 8885 NFSCL_DEBUG(4, "aft adddev=%d\n", laystat); 8886 if (laystat != 0) { 8887 if (layouttype == NFSLAYOUT_FLEXFILE) 8888 dev = tflp->nfsfl_ffm[i].dev; 8889 else 8890 dev = tflp->nfsfl_dev; 8891 laystat = nfsrpc_getdeviceinfo(nmp, dev, 8892 layouttype, notifybit, &dip, cred, 8893 p); 8894 NFSCL_DEBUG(4, "aft nfsrpc_gdi=%d\n", 8895 laystat); 8896 if (laystat != 0) 8897 goto out; 8898 laystat = nfscl_adddevinfo(nmp, dip, i, 8899 tflp); 8900 if (laystat != 0) 8901 printf("nfsrpc_layoutgetresout" 8902 ": cannot add\n"); 8903 } 8904 } 8905 } 8906 } 8907 out: 8908 if (laystat == 0) { 8909 /* 8910 * nfscl_layout() always returns with the nfsly_lock 8911 * set to a refcnt (shared lock). 8912 * Passing in dvp is sufficient, since it is only used to 8913 * get the fsid for the file system. 8914 */ 8915 laystat = nfscl_layout(nmp, vp, newfhp, newfhlen, stateidp, 8916 layouttype, retonclose, flhp, lypp, cred, p); 8917 NFSCL_DEBUG(4, "nfsrpc_layoutgetres: aft nfscl_layout=%d\n", 8918 laystat); 8919 if (laystat == 0 && islockedp != NULL) 8920 *islockedp = 1; 8921 } 8922 return (laystat); 8923 } 8924 8925 /* 8926 * nfs copy_file_range operation. 8927 */ 8928 int 8929 nfsrpc_copy_file_range(vnode_t invp, off_t *inoffp, vnode_t outvp, 8930 off_t *outoffp, size_t *lenp, unsigned int flags, int *inattrflagp, 8931 struct nfsvattr *innap, int *outattrflagp, struct nfsvattr *outnap, 8932 struct ucred *cred, bool consecutive, bool *must_commitp) 8933 { 8934 int commit, error, expireret = 0, retrycnt; 8935 u_int32_t clidrev = 0; 8936 struct nfsmount *nmp = VFSTONFS(invp->v_mount); 8937 struct nfsfh *innfhp = NULL, *outnfhp = NULL; 8938 nfsv4stateid_t instateid, outstateid; 8939 void *inlckp, *outlckp; 8940 8941 if (nmp->nm_clp != NULL) 8942 clidrev = nmp->nm_clp->nfsc_clientidrev; 8943 innfhp = VTONFS(invp)->n_fhp; 8944 outnfhp = VTONFS(outvp)->n_fhp; 8945 retrycnt = 0; 8946 do { 8947 /* Get both stateids. */ 8948 inlckp = NULL; 8949 nfscl_getstateid(invp, innfhp->nfh_fh, innfhp->nfh_len, 8950 NFSV4OPEN_ACCESSREAD, 0, NULL, curthread, &instateid, 8951 &inlckp); 8952 outlckp = NULL; 8953 nfscl_getstateid(outvp, outnfhp->nfh_fh, outnfhp->nfh_len, 8954 NFSV4OPEN_ACCESSWRITE, 0, NULL, curthread, &outstateid, 8955 &outlckp); 8956 8957 error = nfsrpc_copyrpc(invp, *inoffp, outvp, *outoffp, lenp, 8958 &instateid, &outstateid, innap, inattrflagp, outnap, 8959 outattrflagp, consecutive, &commit, cred, curthread); 8960 if (error == 0) { 8961 if (commit != NFSWRITE_FILESYNC) 8962 *must_commitp = true; 8963 *inoffp += *lenp; 8964 *outoffp += *lenp; 8965 } else if (error == NFSERR_STALESTATEID) 8966 nfscl_initiate_recovery(nmp->nm_clp); 8967 if (inlckp != NULL) 8968 nfscl_lockderef(inlckp); 8969 if (outlckp != NULL) 8970 nfscl_lockderef(outlckp); 8971 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 8972 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 8973 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 8974 (void) nfs_catnap(PZERO, error, "nfs_cfr"); 8975 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) && 8976 error == NFSERR_BADSTATEID)) && clidrev != 0) { 8977 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, 8978 curthread); 8979 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) { 8980 error = EIO; 8981 } 8982 retrycnt++; 8983 } while (error == NFSERR_GRACE || error == NFSERR_DELAY || 8984 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 8985 error == NFSERR_STALEDONTRECOVER || 8986 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 8987 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 8988 expireret == 0 && clidrev != 0 && retrycnt < 4)); 8989 if (error != 0 && (retrycnt >= 4 || 8990 error == NFSERR_STALESTATEID || error == NFSERR_BADSESSION || 8991 error == NFSERR_STALEDONTRECOVER)) 8992 error = EIO; 8993 return (error); 8994 } 8995 8996 /* 8997 * The copy RPC. 8998 */ 8999 static int 9000 nfsrpc_copyrpc(vnode_t invp, off_t inoff, vnode_t outvp, off_t outoff, 9001 size_t *lenp, nfsv4stateid_t *instateidp, nfsv4stateid_t *outstateidp, 9002 struct nfsvattr *innap, int *inattrflagp, struct nfsvattr *outnap, 9003 int *outattrflagp, bool consecutive, int *commitp, struct ucred *cred, 9004 NFSPROC_T *p) 9005 { 9006 uint32_t *tl, *opcntp; 9007 int error; 9008 struct nfsrv_descript nfsd; 9009 struct nfsrv_descript *nd = &nfsd; 9010 struct nfsmount *nmp; 9011 nfsattrbit_t attrbits; 9012 struct vattr va; 9013 uint64_t len; 9014 9015 nmp = VFSTONFS(invp->v_mount); 9016 *inattrflagp = *outattrflagp = 0; 9017 *commitp = NFSWRITE_UNSTABLE; 9018 len = *lenp; 9019 *lenp = 0; 9020 if (len > nfs_maxcopyrange) 9021 len = nfs_maxcopyrange; 9022 nfscl_reqstart(nd, NFSPROC_COPY, nmp, VTONFS(invp)->n_fhp->nfh_fh, 9023 VTONFS(invp)->n_fhp->nfh_len, &opcntp, NULL, 0, 0, cred); 9024 /* 9025 * First do a Setattr of atime to the server's clock 9026 * time. The FreeBSD "collective" was of the opinion 9027 * that setting atime was necessary for this syscall. 9028 * Do the Setattr before the Copy, so that it can be 9029 * handled well if the server replies NFSERR_DELAY to 9030 * the Setattr operation. 9031 */ 9032 if ((nmp->nm_mountp->mnt_flag & MNT_NOATIME) == 0) { 9033 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9034 *tl = txdr_unsigned(NFSV4OP_SETATTR); 9035 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID); 9036 VATTR_NULL(&va); 9037 va.va_atime.tv_sec = va.va_atime.tv_nsec = 0; 9038 va.va_vaflags = VA_UTIMES_NULL; 9039 nfscl_fillsattr(nd, &va, invp, 0, 0); 9040 /* Bump opcnt from 7 to 8. */ 9041 *opcntp = txdr_unsigned(8); 9042 } 9043 9044 /* Now Getattr the invp attributes. */ 9045 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9046 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9047 NFSGETATTR_ATTRBIT(&attrbits); 9048 nfsrv_putattrbit(nd, &attrbits); 9049 9050 /* Set outvp. */ 9051 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9052 *tl = txdr_unsigned(NFSV4OP_PUTFH); 9053 (void)nfsm_fhtom(nmp, nd, VTONFS(outvp)->n_fhp->nfh_fh, 9054 VTONFS(outvp)->n_fhp->nfh_len, 0); 9055 9056 /* Do the Copy. */ 9057 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9058 *tl = txdr_unsigned(NFSV4OP_COPY); 9059 nfsm_stateidtom(nd, instateidp, NFSSTATEID_PUTSTATEID); 9060 nfsm_stateidtom(nd, outstateidp, NFSSTATEID_PUTSTATEID); 9061 NFSM_BUILD(tl, uint32_t *, 3 * NFSX_HYPER + 4 * NFSX_UNSIGNED); 9062 txdr_hyper(inoff, tl); tl += 2; 9063 txdr_hyper(outoff, tl); tl += 2; 9064 txdr_hyper(len, tl); tl += 2; 9065 if (consecutive) 9066 *tl++ = newnfs_true; 9067 else 9068 *tl++ = newnfs_false; 9069 *tl++ = newnfs_true; 9070 *tl++ = 0; 9071 9072 /* Get the outvp attributes. */ 9073 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9074 NFSWRITEGETATTR_ATTRBIT(&attrbits); 9075 nfsrv_putattrbit(nd, &attrbits); 9076 9077 error = nfscl_request(nd, invp, p, cred); 9078 if (error != 0) 9079 return (error); 9080 /* Skip over the Setattr reply. */ 9081 if ((nd->nd_flag & ND_NOMOREDATA) == 0 && 9082 (nmp->nm_mountp->mnt_flag & MNT_NOATIME) == 0) { 9083 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9084 if (*(tl + 1) == 0) { 9085 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 9086 if (error != 0) 9087 goto nfsmout; 9088 } else 9089 nd->nd_flag |= ND_NOMOREDATA; 9090 } 9091 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 9092 /* Get the input file's attributes. */ 9093 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9094 if (*(tl + 1) == 0) { 9095 error = nfsm_loadattr(nd, innap); 9096 if (error != 0) 9097 goto nfsmout; 9098 *inattrflagp = 1; 9099 } else 9100 nd->nd_flag |= ND_NOMOREDATA; 9101 } 9102 /* Skip over return stat for PutFH. */ 9103 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 9104 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9105 if (*++tl != 0) 9106 nd->nd_flag |= ND_NOMOREDATA; 9107 } 9108 /* Skip over return stat for Copy. */ 9109 if ((nd->nd_flag & ND_NOMOREDATA) == 0) 9110 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9111 if (nd->nd_repstat == 0) { 9112 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 9113 if (*tl != 0) { 9114 /* There should be no callback ids. */ 9115 error = NFSERR_BADXDR; 9116 goto nfsmout; 9117 } 9118 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + 3 * NFSX_UNSIGNED + 9119 NFSX_VERF); 9120 len = fxdr_hyper(tl); tl += 2; 9121 *commitp = fxdr_unsigned(int, *tl++); 9122 NFSLOCKMNT(nmp); 9123 if (!NFSHASWRITEVERF(nmp)) { 9124 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 9125 NFSSETWRITEVERF(nmp); 9126 } else if (NFSBCMP(tl, nmp->nm_verf, NFSX_VERF)) { 9127 NFSBCOPY(tl, nmp->nm_verf, NFSX_VERF); 9128 nd->nd_repstat = NFSERR_STALEWRITEVERF; 9129 } 9130 NFSUNLOCKMNT(nmp); 9131 tl += (NFSX_VERF / NFSX_UNSIGNED); 9132 if (nd->nd_repstat == 0 && *++tl != newnfs_true) 9133 /* Must be a synchronous copy. */ 9134 nd->nd_repstat = NFSERR_NOTSUPP; 9135 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9136 error = nfsm_loadattr(nd, outnap); 9137 if (error == 0) 9138 *outattrflagp = NFS_LATTR_NOSHRINK; 9139 if (nd->nd_repstat == 0) 9140 *lenp = len; 9141 } else if (nd->nd_repstat == NFSERR_OFFLOADNOREQS) { 9142 /* 9143 * For the case where consecutive is not supported, but 9144 * synchronous is supported, we can try consecutive == false 9145 * by returning this error. Otherwise, return NFSERR_NOTSUPP, 9146 * since Copy cannot be done. 9147 */ 9148 if ((nd->nd_flag & ND_NOMOREDATA) == 0) { 9149 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9150 if (!consecutive || *++tl == newnfs_false) 9151 nd->nd_repstat = NFSERR_NOTSUPP; 9152 } else 9153 nd->nd_repstat = NFSERR_BADXDR; 9154 } 9155 if (error == 0) 9156 error = nd->nd_repstat; 9157 nfsmout: 9158 m_freem(nd->nd_mrep); 9159 return (error); 9160 } 9161 9162 /* 9163 * Seek operation. 9164 */ 9165 int 9166 nfsrpc_seek(vnode_t vp, off_t *offp, bool *eofp, int content, 9167 struct ucred *cred, struct nfsvattr *nap, int *attrflagp) 9168 { 9169 int error, expireret = 0, retrycnt; 9170 u_int32_t clidrev = 0; 9171 struct nfsmount *nmp = VFSTONFS(vp->v_mount); 9172 struct nfsnode *np = VTONFS(vp); 9173 struct nfsfh *nfhp = NULL; 9174 nfsv4stateid_t stateid; 9175 void *lckp; 9176 9177 if (nmp->nm_clp != NULL) 9178 clidrev = nmp->nm_clp->nfsc_clientidrev; 9179 nfhp = np->n_fhp; 9180 retrycnt = 0; 9181 do { 9182 lckp = NULL; 9183 nfscl_getstateid(vp, nfhp->nfh_fh, nfhp->nfh_len, 9184 NFSV4OPEN_ACCESSREAD, 0, cred, curthread, &stateid, &lckp); 9185 error = nfsrpc_seekrpc(vp, offp, &stateid, eofp, content, 9186 nap, attrflagp, cred); 9187 if (error == NFSERR_STALESTATEID) 9188 nfscl_initiate_recovery(nmp->nm_clp); 9189 if (lckp != NULL) 9190 nfscl_lockderef(lckp); 9191 if (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 9192 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 9193 error == NFSERR_OLDSTATEID || error == NFSERR_BADSESSION) { 9194 (void) nfs_catnap(PZERO, error, "nfs_seek"); 9195 } else if ((error == NFSERR_EXPIRED || (!NFSHASINT(nmp) && 9196 error == NFSERR_BADSTATEID)) && clidrev != 0) { 9197 expireret = nfscl_hasexpired(nmp->nm_clp, clidrev, 9198 curthread); 9199 } else if (error == NFSERR_BADSTATEID && NFSHASINT(nmp)) { 9200 error = EIO; 9201 } 9202 retrycnt++; 9203 } while (error == NFSERR_GRACE || error == NFSERR_STALESTATEID || 9204 error == NFSERR_STALEDONTRECOVER || error == NFSERR_DELAY || 9205 error == NFSERR_BADSESSION || 9206 (error == NFSERR_OLDSTATEID && retrycnt < 20) || 9207 ((error == NFSERR_EXPIRED || error == NFSERR_BADSTATEID) && 9208 expireret == 0 && clidrev != 0 && retrycnt < 4) || 9209 (error == NFSERR_OPENMODE && retrycnt < 4)); 9210 if (error && retrycnt >= 4) 9211 error = EIO; 9212 return (error); 9213 } 9214 9215 /* 9216 * The seek RPC. 9217 */ 9218 static int 9219 nfsrpc_seekrpc(vnode_t vp, off_t *offp, nfsv4stateid_t *stateidp, bool *eofp, 9220 int content, struct nfsvattr *nap, int *attrflagp, struct ucred *cred) 9221 { 9222 uint32_t *tl; 9223 int error; 9224 struct nfsrv_descript nfsd; 9225 struct nfsrv_descript *nd = &nfsd; 9226 nfsattrbit_t attrbits; 9227 9228 *attrflagp = 0; 9229 NFSCL_REQSTART(nd, NFSPROC_SEEK, vp, cred); 9230 nfsm_stateidtom(nd, stateidp, NFSSTATEID_PUTSTATEID); 9231 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 9232 txdr_hyper(*offp, tl); tl += 2; 9233 *tl++ = txdr_unsigned(content); 9234 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9235 NFSGETATTR_ATTRBIT(&attrbits); 9236 nfsrv_putattrbit(nd, &attrbits); 9237 error = nfscl_request(nd, vp, curthread, cred); 9238 if (error != 0) 9239 return (error); 9240 if (nd->nd_repstat == 0) { 9241 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED + NFSX_HYPER); 9242 if (*tl++ == newnfs_true) 9243 *eofp = true; 9244 else 9245 *eofp = false; 9246 *offp = fxdr_hyper(tl); 9247 /* Just skip over Getattr op status. */ 9248 error = nfsm_loadattr(nd, nap); 9249 if (error == 0) 9250 *attrflagp = 1; 9251 } 9252 error = nd->nd_repstat; 9253 nfsmout: 9254 m_freem(nd->nd_mrep); 9255 return (error); 9256 } 9257 9258 /* 9259 * The getextattr RPC. 9260 */ 9261 int 9262 nfsrpc_getextattr(vnode_t vp, const char *name, struct uio *uiop, ssize_t *lenp, 9263 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 9264 { 9265 uint32_t *tl; 9266 int error; 9267 struct nfsrv_descript nfsd; 9268 struct nfsrv_descript *nd = &nfsd; 9269 nfsattrbit_t attrbits; 9270 uint32_t len, len2; 9271 9272 *attrflagp = 0; 9273 NFSCL_REQSTART(nd, NFSPROC_GETEXTATTR, vp, cred); 9274 nfsm_strtom(nd, name, strlen(name)); 9275 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9276 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9277 NFSGETATTR_ATTRBIT(&attrbits); 9278 nfsrv_putattrbit(nd, &attrbits); 9279 error = nfscl_request(nd, vp, p, cred); 9280 if (error != 0) 9281 return (error); 9282 if (nd->nd_repstat == 0) { 9283 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 9284 len = fxdr_unsigned(uint32_t, *tl); 9285 /* Sanity check lengths. */ 9286 if (uiop != NULL && len > 0 && len <= IOSIZE_MAX && 9287 uiop->uio_resid <= UINT32_MAX) { 9288 len2 = uiop->uio_resid; 9289 if (len2 >= len) 9290 error = nfsm_mbufuio(nd, uiop, len); 9291 else { 9292 error = nfsm_mbufuio(nd, uiop, len2); 9293 if (error == 0) { 9294 /* 9295 * nfsm_mbufuio() advances to a multiple 9296 * of 4, so round up len2 as well. Then 9297 * we need to advance over the rest of 9298 * the data, rounding up the remaining 9299 * length. 9300 */ 9301 len2 = NFSM_RNDUP(len2); 9302 len2 = NFSM_RNDUP(len - len2); 9303 if (len2 > 0) 9304 error = nfsm_advance(nd, len2, 9305 -1); 9306 } 9307 } 9308 } else if (uiop == NULL && len > 0) { 9309 /* Just wants the length and not the data. */ 9310 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 9311 } else if (len > 0) 9312 error = ENOATTR; 9313 if (error != 0) 9314 goto nfsmout; 9315 *lenp = len; 9316 /* Just skip over Getattr op status. */ 9317 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9318 error = nfsm_loadattr(nd, nap); 9319 if (error == 0) 9320 *attrflagp = 1; 9321 } 9322 if (error == 0) 9323 error = nd->nd_repstat; 9324 nfsmout: 9325 m_freem(nd->nd_mrep); 9326 return (error); 9327 } 9328 9329 /* 9330 * The setextattr RPC. 9331 */ 9332 int 9333 nfsrpc_setextattr(vnode_t vp, const char *name, struct uio *uiop, 9334 struct nfsvattr *nap, int *attrflagp, struct ucred *cred, NFSPROC_T *p) 9335 { 9336 uint32_t *tl; 9337 int error; 9338 struct nfsrv_descript nfsd; 9339 struct nfsrv_descript *nd = &nfsd; 9340 nfsattrbit_t attrbits; 9341 9342 *attrflagp = 0; 9343 NFSCL_REQSTART(nd, NFSPROC_SETEXTATTR, vp, cred); 9344 if (uiop->uio_resid > nd->nd_maxreq) { 9345 /* nd_maxreq is set by NFSCL_REQSTART(). */ 9346 m_freem(nd->nd_mreq); 9347 return (EINVAL); 9348 } 9349 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9350 *tl = txdr_unsigned(NFSV4SXATTR_EITHER); 9351 nfsm_strtom(nd, name, strlen(name)); 9352 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9353 *tl = txdr_unsigned(uiop->uio_resid); 9354 error = nfsm_uiombuf(nd, uiop, uiop->uio_resid); 9355 if (error != 0) { 9356 m_freem(nd->nd_mreq); 9357 return (error); 9358 } 9359 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9360 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9361 NFSGETATTR_ATTRBIT(&attrbits); 9362 nfsrv_putattrbit(nd, &attrbits); 9363 error = nfscl_request(nd, vp, p, cred); 9364 if (error != 0) 9365 return (error); 9366 if (nd->nd_repstat == 0) { 9367 /* Just skip over the reply and Getattr op status. */ 9368 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 * 9369 NFSX_UNSIGNED); 9370 error = nfsm_loadattr(nd, nap); 9371 if (error == 0) 9372 *attrflagp = 1; 9373 } 9374 if (error == 0) 9375 error = nd->nd_repstat; 9376 nfsmout: 9377 m_freem(nd->nd_mrep); 9378 return (error); 9379 } 9380 9381 /* 9382 * The removeextattr RPC. 9383 */ 9384 int 9385 nfsrpc_rmextattr(vnode_t vp, const char *name, struct nfsvattr *nap, 9386 int *attrflagp, struct ucred *cred, NFSPROC_T *p) 9387 { 9388 uint32_t *tl; 9389 int error; 9390 struct nfsrv_descript nfsd; 9391 struct nfsrv_descript *nd = &nfsd; 9392 nfsattrbit_t attrbits; 9393 9394 *attrflagp = 0; 9395 NFSCL_REQSTART(nd, NFSPROC_RMEXTATTR, vp, cred); 9396 nfsm_strtom(nd, name, strlen(name)); 9397 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9398 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9399 NFSGETATTR_ATTRBIT(&attrbits); 9400 nfsrv_putattrbit(nd, &attrbits); 9401 error = nfscl_request(nd, vp, p, cred); 9402 if (error != 0) 9403 return (error); 9404 if (nd->nd_repstat == 0) { 9405 /* Just skip over the reply and Getattr op status. */ 9406 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER + 3 * 9407 NFSX_UNSIGNED); 9408 error = nfsm_loadattr(nd, nap); 9409 if (error == 0) 9410 *attrflagp = 1; 9411 } 9412 if (error == 0) 9413 error = nd->nd_repstat; 9414 nfsmout: 9415 m_freem(nd->nd_mrep); 9416 return (error); 9417 } 9418 9419 /* 9420 * The listextattr RPC. 9421 */ 9422 int 9423 nfsrpc_listextattr(vnode_t vp, uint64_t *cookiep, struct uio *uiop, 9424 size_t *lenp, bool *eofp, struct nfsvattr *nap, int *attrflagp, 9425 struct ucred *cred, NFSPROC_T *p) 9426 { 9427 uint32_t *tl; 9428 int cnt, error, i, len; 9429 struct nfsrv_descript nfsd; 9430 struct nfsrv_descript *nd = &nfsd; 9431 nfsattrbit_t attrbits; 9432 u_char c; 9433 9434 *attrflagp = 0; 9435 NFSCL_REQSTART(nd, NFSPROC_LISTEXTATTR, vp, cred); 9436 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 9437 txdr_hyper(*cookiep, tl); tl += 2; 9438 *tl++ = txdr_unsigned(*lenp); 9439 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9440 NFSGETATTR_ATTRBIT(&attrbits); 9441 nfsrv_putattrbit(nd, &attrbits); 9442 error = nfscl_request(nd, vp, p, cred); 9443 if (error != 0) 9444 return (error); 9445 *eofp = true; 9446 *lenp = 0; 9447 if (nd->nd_repstat == 0) { 9448 NFSM_DISSECT(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 9449 *cookiep = fxdr_hyper(tl); tl += 2; 9450 cnt = fxdr_unsigned(int, *tl); 9451 if (cnt < 0) { 9452 error = EBADRPC; 9453 goto nfsmout; 9454 } 9455 for (i = 0; i < cnt; i++) { 9456 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 9457 len = fxdr_unsigned(int, *tl); 9458 if (len <= 0 || len > EXTATTR_MAXNAMELEN) { 9459 error = EBADRPC; 9460 goto nfsmout; 9461 } 9462 if (uiop == NULL) 9463 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 9464 else if (uiop->uio_resid >= len + 1) { 9465 c = len; 9466 error = uiomove(&c, sizeof(c), uiop); 9467 if (error == 0) 9468 error = nfsm_mbufuio(nd, uiop, len); 9469 } else { 9470 error = nfsm_advance(nd, NFSM_RNDUP(len), -1); 9471 *eofp = false; 9472 } 9473 if (error != 0) 9474 goto nfsmout; 9475 *lenp += (len + 1); 9476 } 9477 /* Get the eof and skip over the Getattr op status. */ 9478 NFSM_DISSECT(tl, uint32_t *, 3 * NFSX_UNSIGNED); 9479 /* 9480 * *eofp is set false above, because it wasn't able to copy 9481 * all of the reply. 9482 */ 9483 if (*eofp && *tl == 0) 9484 *eofp = false; 9485 error = nfsm_loadattr(nd, nap); 9486 if (error == 0) 9487 *attrflagp = 1; 9488 } 9489 if (error == 0) 9490 error = nd->nd_repstat; 9491 nfsmout: 9492 m_freem(nd->nd_mrep); 9493 return (error); 9494 } 9495 9496 /* 9497 * Split an mbuf list. For non-M_EXTPG mbufs, just use m_split(). 9498 */ 9499 static struct mbuf * 9500 nfsm_split(struct mbuf *mp, uint64_t xfer) 9501 { 9502 struct mbuf *m, *m2; 9503 vm_page_t pg; 9504 int i, j, left, pgno, plen, trim; 9505 char *cp, *cp2; 9506 9507 if ((mp->m_flags & M_EXTPG) == 0) { 9508 m = m_split(mp, xfer, M_WAITOK); 9509 return (m); 9510 } 9511 9512 /* Find the correct mbuf to split at. */ 9513 for (m = mp; m != NULL && xfer > m->m_len; m = m->m_next) 9514 xfer -= m->m_len; 9515 if (m == NULL) 9516 return (NULL); 9517 9518 /* If xfer == m->m_len, we can just split the mbuf list. */ 9519 if (xfer == m->m_len) { 9520 m2 = m->m_next; 9521 m->m_next = NULL; 9522 return (m2); 9523 } 9524 9525 /* Find the page to split at. */ 9526 pgno = 0; 9527 left = xfer; 9528 do { 9529 if (pgno == 0) 9530 plen = m_epg_pagelen(m, 0, m->m_epg_1st_off); 9531 else 9532 plen = m_epg_pagelen(m, pgno, 0); 9533 if (left <= plen) 9534 break; 9535 left -= plen; 9536 pgno++; 9537 } while (pgno < m->m_epg_npgs); 9538 if (pgno == m->m_epg_npgs) 9539 panic("nfsm_split: eroneous ext_pgs mbuf"); 9540 9541 m2 = mb_alloc_ext_pgs(M_WAITOK, mb_free_mext_pgs, 0); 9542 m2->m_epg_flags |= EPG_FLAG_ANON; 9543 9544 /* 9545 * If left < plen, allocate a new page for the new mbuf 9546 * and copy the data after left in the page to this new 9547 * page. 9548 */ 9549 if (left < plen) { 9550 pg = vm_page_alloc_noobj(VM_ALLOC_WAITOK | VM_ALLOC_NODUMP | 9551 VM_ALLOC_WIRED); 9552 m2->m_epg_pa[0] = VM_PAGE_TO_PHYS(pg); 9553 m2->m_epg_npgs = 1; 9554 9555 /* Copy the data after left to the new page. */ 9556 trim = plen - left; 9557 cp = (char *)(void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]); 9558 if (pgno == 0) 9559 cp += m->m_epg_1st_off; 9560 cp += left; 9561 cp2 = (char *)(void *)PHYS_TO_DMAP(m2->m_epg_pa[0]); 9562 if (pgno == m->m_epg_npgs - 1) 9563 m2->m_epg_last_len = trim; 9564 else { 9565 cp2 += PAGE_SIZE - trim; 9566 m2->m_epg_1st_off = PAGE_SIZE - trim; 9567 m2->m_epg_last_len = m->m_epg_last_len; 9568 } 9569 memcpy(cp2, cp, trim); 9570 m2->m_len = trim; 9571 } else { 9572 m2->m_len = 0; 9573 m2->m_epg_last_len = m->m_epg_last_len; 9574 } 9575 9576 /* Move the pages beyond pgno to the new mbuf. */ 9577 for (i = pgno + 1, j = m2->m_epg_npgs; i < m->m_epg_npgs; i++, j++) { 9578 m2->m_epg_pa[j] = m->m_epg_pa[i]; 9579 /* Never moves page 0. */ 9580 m2->m_len += m_epg_pagelen(m, i, 0); 9581 } 9582 m2->m_epg_npgs = j; 9583 m->m_epg_npgs = pgno + 1; 9584 m->m_epg_last_len = left; 9585 m->m_len = xfer; 9586 9587 m2->m_next = m->m_next; 9588 m->m_next = NULL; 9589 return (m2); 9590 } 9591 9592 /* 9593 * Do the NFSv4.1 Bind Connection to Session. 9594 * Called from the reconnect layer of the krpc (sys/rpc/clnt_rc.c). 9595 */ 9596 void 9597 nfsrpc_bindconnsess(CLIENT *cl, void *arg, struct ucred *cr) 9598 { 9599 struct nfscl_reconarg *rcp = (struct nfscl_reconarg *)arg; 9600 uint32_t res, *tl; 9601 struct nfsrv_descript nfsd; 9602 struct nfsrv_descript *nd = &nfsd; 9603 struct rpc_callextra ext; 9604 struct timeval utimeout; 9605 enum clnt_stat stat; 9606 int error; 9607 9608 nfscl_reqstart(nd, NFSPROC_BINDCONNTOSESS, NULL, NULL, 0, NULL, NULL, 9609 NFS_VER4, rcp->minorvers, NULL); 9610 NFSM_BUILD(tl, uint32_t *, NFSX_V4SESSIONID + 2 * NFSX_UNSIGNED); 9611 memcpy(tl, rcp->sessionid, NFSX_V4SESSIONID); 9612 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 9613 *tl++ = txdr_unsigned(NFSCDFC4_FORE_OR_BOTH); 9614 *tl = newnfs_false; 9615 9616 memset(&ext, 0, sizeof(ext)); 9617 utimeout.tv_sec = 30; 9618 utimeout.tv_usec = 0; 9619 ext.rc_auth = authunix_create(cr); 9620 nd->nd_mrep = NULL; 9621 stat = CLNT_CALL_MBUF(cl, &ext, NFSV4PROC_COMPOUND, nd->nd_mreq, 9622 &nd->nd_mrep, utimeout); 9623 AUTH_DESTROY(ext.rc_auth); 9624 if (stat != RPC_SUCCESS) { 9625 printf("nfsrpc_bindconnsess: call failed stat=%d\n", stat); 9626 return; 9627 } 9628 if (nd->nd_mrep == NULL) { 9629 printf("nfsrpc_bindconnsess: no reply args\n"); 9630 return; 9631 } 9632 error = 0; 9633 newnfs_realign(&nd->nd_mrep, M_WAITOK); 9634 nd->nd_md = nd->nd_mrep; 9635 nd->nd_dpos = mtod(nd->nd_md, char *); 9636 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9637 nd->nd_repstat = fxdr_unsigned(uint32_t, *tl++); 9638 if (nd->nd_repstat == NFSERR_OK) { 9639 res = fxdr_unsigned(uint32_t, *tl); 9640 if (res > 0 && (error = nfsm_advance(nd, NFSM_RNDUP(res), 9641 -1)) != 0) 9642 goto nfsmout; 9643 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID + 9644 4 * NFSX_UNSIGNED); 9645 tl += 3; 9646 if (!NFSBCMP(tl, rcp->sessionid, NFSX_V4SESSIONID)) { 9647 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED; 9648 res = fxdr_unsigned(uint32_t, *tl); 9649 if (res != NFSCDFS4_BOTH) 9650 printf("nfsrpc_bindconnsess: did not " 9651 "return FS4_BOTH\n"); 9652 } else 9653 printf("nfsrpc_bindconnsess: not same " 9654 "sessionid\n"); 9655 } else if (nd->nd_repstat != NFSERR_BADSESSION) 9656 printf("nfsrpc_bindconnsess: returned %d\n", nd->nd_repstat); 9657 nfsmout: 9658 if (error != 0) 9659 printf("nfsrpc_bindconnsess: reply bad xdr\n"); 9660 m_freem(nd->nd_mrep); 9661 } 9662 9663 /* 9664 * nfs opeattr rpc 9665 */ 9666 int 9667 nfsrpc_openattr(struct nfsmount *nmp, struct vnode *vp, uint8_t *fhp, int fhlen, 9668 bool createit, struct ucred *cred, NFSPROC_T *p, struct nfsvattr *nap, 9669 struct nfsfh **nfhpp, int *attrflagp) 9670 { 9671 uint32_t *tl; 9672 struct nfsrv_descript nfsd, *nd = &nfsd; 9673 nfsattrbit_t attrbits; 9674 int error = 0; 9675 9676 *attrflagp = 0; 9677 nfscl_reqstart(nd, NFSPROC_OPENATTR, nmp, fhp, fhlen, NULL, NULL, 0, 0, 9678 cred); 9679 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED); 9680 if (createit) 9681 *tl = newnfs_true; 9682 else 9683 *tl = newnfs_false; 9684 NFSGETATTR_ATTRBIT(&attrbits); 9685 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9686 *tl++ = txdr_unsigned(NFSV4OP_GETFH); 9687 *tl = txdr_unsigned(NFSV4OP_GETATTR); 9688 (void)nfsrv_putattrbit(nd, &attrbits); 9689 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 9690 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 9691 if (error != 0) 9692 return (error); 9693 if (nd->nd_repstat == 0) { 9694 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 9695 error = nfsm_getfh(nd, nfhpp); 9696 if (error != 0) 9697 goto nfsmout; 9698 error = nfscl_postop_attr(nd, nap, attrflagp); 9699 } 9700 nfsmout: 9701 m_freem(nd->nd_mrep); 9702 if (error == 0 && nd->nd_repstat != 0) 9703 error = nd->nd_repstat; 9704 return (error); 9705 } 9706 9707 /* 9708 * Do roughly what nfs_statfs() does for NFSv4, but when called with a shared 9709 * locked vnode. 9710 */ 9711 static void 9712 nfscl_statfs(struct vnode *vp, struct ucred *cred, NFSPROC_T *td) 9713 { 9714 struct nfsvattr nfsva; 9715 struct nfsfsinfo fs; 9716 struct nfsstatfs sb; 9717 struct mount *mp; 9718 struct nfsmount *nmp; 9719 uint32_t lease; 9720 int attrflag, error; 9721 9722 mp = vp->v_mount; 9723 nmp = VFSTONFS(mp); 9724 error = nfsrpc_statfs(vp, &sb, &fs, &lease, cred, td, &nfsva, 9725 &attrflag); 9726 if (attrflag != 0) 9727 (void) nfscl_loadattrcache(&vp, &nfsva, NULL, 0, 1); 9728 if (error == 0) { 9729 NFSLOCKCLSTATE(); 9730 if (nmp->nm_clp != NULL) 9731 nmp->nm_clp->nfsc_renew = NFSCL_RENEW(lease); 9732 NFSUNLOCKCLSTATE(); 9733 mtx_lock(&nmp->nm_mtx); 9734 nfscl_loadfsinfo(nmp, &fs); 9735 nfscl_loadsbinfo(nmp, &sb, &mp->mnt_stat); 9736 mp->mnt_stat.f_iosize = newnfs_iosize(nmp); 9737 mtx_unlock(&nmp->nm_mtx); 9738 } 9739 } 9740