1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/capsicum.h> 40 #include <sys/extattr.h> 41 42 /* 43 * Functions that perform the vfs operations required by the routines in 44 * nfsd_serv.c. It is hoped that this change will make the server more 45 * portable. 46 */ 47 48 #include <fs/nfs/nfsport.h> 49 #include <sys/hash.h> 50 #include <sys/sysctl.h> 51 #include <nlm/nlm_prot.h> 52 #include <nlm/nlm.h> 53 54 FEATURE(nfsd, "NFSv4 server"); 55 56 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1; 57 extern int nfsrv_useacl; 58 extern int newnfs_numnfsd; 59 extern struct mount nfsv4root_mnt; 60 extern struct nfsrv_stablefirst nfsrv_stablefirst; 61 extern void (*nfsd_call_servertimer)(void); 62 extern SVCPOOL *nfsrvd_pool; 63 extern struct nfsv4lock nfsd_suspend_lock; 64 extern struct nfsclienthashhead *nfsclienthash; 65 extern struct nfslockhashhead *nfslockhash; 66 extern struct nfssessionhash *nfssessionhash; 67 extern int nfsrv_sessionhashsize; 68 extern struct nfsstatsv1 nfsstatsv1; 69 extern struct nfslayouthash *nfslayouthash; 70 extern int nfsrv_layouthashsize; 71 extern struct mtx nfsrv_dslock_mtx; 72 extern int nfs_pnfsiothreads; 73 extern struct nfsdontlisthead nfsrv_dontlisthead; 74 extern volatile int nfsrv_dontlistlen; 75 extern volatile int nfsrv_devidcnt; 76 extern int nfsrv_maxpnfsmirror; 77 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt; 78 NFSDLOCKMUTEX; 79 NFSSTATESPINLOCK; 80 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE]; 81 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE]; 82 struct mtx nfsrc_udpmtx; 83 struct mtx nfs_v4root_mutex; 84 struct mtx nfsrv_dontlistlock_mtx; 85 struct mtx nfsrv_recalllock_mtx; 86 struct nfsrvfh nfs_rootfh, nfs_pubfh; 87 int nfs_pubfhset = 0, nfs_rootfhset = 0; 88 struct proc *nfsd_master_proc = NULL; 89 int nfsd_debuglevel = 0; 90 static pid_t nfsd_master_pid = (pid_t)-1; 91 static char nfsd_master_comm[MAXCOMLEN + 1]; 92 static struct timeval nfsd_master_start; 93 static uint32_t nfsv4_sysid = 0; 94 static fhandle_t zerofh; 95 96 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *, 97 struct ucred *); 98 99 int nfsrv_enable_crossmntpt = 1; 100 static int nfs_commit_blks; 101 static int nfs_commit_miss; 102 extern int nfsrv_issuedelegs; 103 extern int nfsrv_dolocallocks; 104 extern int nfsd_enable_stringtouid; 105 extern struct nfsdevicehead nfsrv_devidhead; 106 107 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *, 108 NFSPROC_T *); 109 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **, 110 int *, char *, fhandle_t *); 111 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *, 112 NFSPROC_T *); 113 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *, 114 struct thread *, int, struct mbuf **, char *, struct mbuf **, 115 struct nfsvattr *, struct acl *); 116 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *); 117 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *, 118 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **); 119 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *, 120 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **, 121 char *, int *); 122 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *, 123 struct vnode *, struct nfsmount **, int, struct acl *, int *); 124 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *, 125 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *); 126 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *, 127 struct vnode *, struct nfsmount *, struct nfsvattr *); 128 static int nfsrv_putfhname(fhandle_t *, char *); 129 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *, 130 struct pnfsdsfile *, struct vnode **, NFSPROC_T *); 131 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *, 132 struct vnode *, NFSPROC_T *); 133 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *); 134 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *, 135 NFSPROC_T *); 136 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *); 137 138 int nfs_pnfsio(task_fn_t *, void *); 139 140 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server"); 141 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW, 142 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points"); 143 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 144 0, ""); 145 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 146 0, ""); 147 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW, 148 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 149 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW, 150 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 151 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel, 152 0, "Debug level for NFS server"); 153 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW, 154 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names"); 155 static int nfsrv_pnfsgetdsattr = 1; 156 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW, 157 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC"); 158 159 /* 160 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are 161 * not running. 162 * The dsN subdirectories for the increased values must have been created 163 * on all DS servers before this increase is done. 164 */ 165 u_int nfsrv_dsdirsize = 20; 166 static int 167 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS) 168 { 169 int error, newdsdirsize; 170 171 newdsdirsize = nfsrv_dsdirsize; 172 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req); 173 if (error != 0 || req->newptr == NULL) 174 return (error); 175 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 || 176 newnfs_numnfsd != 0) 177 return (EINVAL); 178 nfsrv_dsdirsize = newdsdirsize; 179 return (0); 180 } 181 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize, CTLTYPE_UINT | CTLFLAG_RW, 0, 182 sizeof(nfsrv_dsdirsize), sysctl_dsdirsize, "IU", 183 "Number of dsN subdirs on the DS servers"); 184 185 #define MAX_REORDERED_RPC 16 186 #define NUM_HEURISTIC 1031 187 #define NHUSE_INIT 64 188 #define NHUSE_INC 16 189 #define NHUSE_MAX 2048 190 191 static struct nfsheur { 192 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 193 off_t nh_nextoff; /* next offset for sequential detection */ 194 int nh_use; /* use count for selection */ 195 int nh_seqcount; /* heuristic */ 196 } nfsheur[NUM_HEURISTIC]; 197 198 199 /* 200 * Heuristic to detect sequential operation. 201 */ 202 static struct nfsheur * 203 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp) 204 { 205 struct nfsheur *nh; 206 int hi, try; 207 208 /* Locate best candidate. */ 209 try = 32; 210 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 211 nh = &nfsheur[hi]; 212 while (try--) { 213 if (nfsheur[hi].nh_vp == vp) { 214 nh = &nfsheur[hi]; 215 break; 216 } 217 if (nfsheur[hi].nh_use > 0) 218 --nfsheur[hi].nh_use; 219 hi = (hi + 1) % NUM_HEURISTIC; 220 if (nfsheur[hi].nh_use < nh->nh_use) 221 nh = &nfsheur[hi]; 222 } 223 224 /* Initialize hint if this is a new file. */ 225 if (nh->nh_vp != vp) { 226 nh->nh_vp = vp; 227 nh->nh_nextoff = uio->uio_offset; 228 nh->nh_use = NHUSE_INIT; 229 if (uio->uio_offset == 0) 230 nh->nh_seqcount = 4; 231 else 232 nh->nh_seqcount = 1; 233 } 234 235 /* Calculate heuristic. */ 236 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) || 237 uio->uio_offset == nh->nh_nextoff) { 238 /* See comments in vfs_vnops.c:sequential_heuristic(). */ 239 nh->nh_seqcount += howmany(uio->uio_resid, 16384); 240 if (nh->nh_seqcount > IO_SEQMAX) 241 nh->nh_seqcount = IO_SEQMAX; 242 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC * 243 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) { 244 /* Probably a reordered RPC, leave seqcount alone. */ 245 } else if (nh->nh_seqcount > 1) { 246 nh->nh_seqcount /= 2; 247 } else { 248 nh->nh_seqcount = 0; 249 } 250 nh->nh_use += NHUSE_INC; 251 if (nh->nh_use > NHUSE_MAX) 252 nh->nh_use = NHUSE_MAX; 253 return (nh); 254 } 255 256 /* 257 * Get attributes into nfsvattr structure. 258 */ 259 int 260 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, 261 struct nfsrv_descript *nd, struct thread *p, int vpislocked, 262 nfsattrbit_t *attrbitp) 263 { 264 int error, gotattr, lockedit = 0; 265 struct nfsvattr na; 266 267 if (vpislocked == 0) { 268 /* 269 * When vpislocked == 0, the vnode is either exclusively 270 * locked by this thread or not locked by this thread. 271 * As such, shared lock it, if not exclusively locked. 272 */ 273 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 274 lockedit = 1; 275 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY); 276 } 277 } 278 279 /* 280 * Acquire the Change, Size and TimeModify attributes, as required. 281 * This needs to be done for regular files if: 282 * - non-NFSv4 RPCs or 283 * - when attrbitp == NULL or 284 * - an NFSv4 RPC with any of the above attributes in attrbitp. 285 * A return of 0 for nfsrv_proxyds() indicates that it has acquired 286 * these attributes. nfsrv_proxyds() will return an error if the 287 * server is not a pNFS one. 288 */ 289 gotattr = 0; 290 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL || 291 (nd->nd_flag & ND_NFSV4) == 0 || 292 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) || 293 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) || 294 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) || 295 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY))) { 296 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p, 297 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL); 298 if (error == 0) 299 gotattr = 1; 300 } 301 302 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred); 303 if (lockedit != 0) 304 NFSVOPUNLOCK(vp, 0); 305 306 /* 307 * If we got the Change, Size and Modify Time from the DS, 308 * replace them. 309 */ 310 if (gotattr != 0) { 311 nvap->na_atime = na.na_atime; 312 nvap->na_mtime = na.na_mtime; 313 nvap->na_filerev = na.na_filerev; 314 nvap->na_size = na.na_size; 315 } 316 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr, 317 error, (uintmax_t)na.na_filerev); 318 319 NFSEXITCODE(error); 320 return (error); 321 } 322 323 /* 324 * Get a file handle for a vnode. 325 */ 326 int 327 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 328 { 329 int error; 330 331 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 332 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 333 error = VOP_VPTOFH(vp, &fhp->fh_fid); 334 335 NFSEXITCODE(error); 336 return (error); 337 } 338 339 /* 340 * Perform access checking for vnodes obtained from file handles that would 341 * refer to files already opened by a Unix client. You cannot just use 342 * vn_writechk() and VOP_ACCESSX() for two reasons. 343 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 344 * case. 345 * 2 - The owner is to be given access irrespective of mode bits for some 346 * operations, so that processes that chmod after opening a file don't 347 * break. 348 */ 349 int 350 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 351 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 352 u_int32_t *supportedtypep) 353 { 354 struct vattr vattr; 355 int error = 0, getret = 0; 356 357 if (vpislocked == 0) { 358 if (NFSVOPLOCK(vp, LK_SHARED) != 0) { 359 error = EPERM; 360 goto out; 361 } 362 } 363 if (accmode & VWRITE) { 364 /* Just vn_writechk() changed to check rdonly */ 365 /* 366 * Disallow write attempts on read-only file systems; 367 * unless the file is a socket or a block or character 368 * device resident on the file system. 369 */ 370 if (NFSVNO_EXRDONLY(exp) || 371 (vp->v_mount->mnt_flag & MNT_RDONLY)) { 372 switch (vp->v_type) { 373 case VREG: 374 case VDIR: 375 case VLNK: 376 error = EROFS; 377 default: 378 break; 379 } 380 } 381 /* 382 * If there's shared text associated with 383 * the inode, try to free it up once. If 384 * we fail, we can't allow writing. 385 */ 386 if (VOP_IS_TEXT(vp) && error == 0) 387 error = ETXTBSY; 388 } 389 if (error != 0) { 390 if (vpislocked == 0) 391 NFSVOPUNLOCK(vp, 0); 392 goto out; 393 } 394 395 /* 396 * Should the override still be applied when ACLs are enabled? 397 */ 398 error = VOP_ACCESSX(vp, accmode, cred, p); 399 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 400 /* 401 * Try again with VEXPLICIT_DENY, to see if the test for 402 * deletion is supported. 403 */ 404 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 405 if (error == 0) { 406 if (vp->v_type == VDIR) { 407 accmode &= ~(VDELETE | VDELETE_CHILD); 408 accmode |= VWRITE; 409 error = VOP_ACCESSX(vp, accmode, cred, p); 410 } else if (supportedtypep != NULL) { 411 *supportedtypep &= ~NFSACCESS_DELETE; 412 } 413 } 414 } 415 416 /* 417 * Allow certain operations for the owner (reads and writes 418 * on files that are already open). 419 */ 420 if (override != NFSACCCHK_NOOVERRIDE && 421 (error == EPERM || error == EACCES)) { 422 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 423 error = 0; 424 else if (override & NFSACCCHK_ALLOWOWNER) { 425 getret = VOP_GETATTR(vp, &vattr, cred); 426 if (getret == 0 && cred->cr_uid == vattr.va_uid) 427 error = 0; 428 } 429 } 430 if (vpislocked == 0) 431 NFSVOPUNLOCK(vp, 0); 432 433 out: 434 NFSEXITCODE(error); 435 return (error); 436 } 437 438 /* 439 * Set attribute(s) vnop. 440 */ 441 int 442 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 443 struct thread *p, struct nfsexstuff *exp) 444 { 445 u_quad_t savsize = 0; 446 int error, savedit; 447 448 /* 449 * If this is an exported file system and a pNFS service is running, 450 * don't VOP_SETATTR() of size for the MDS file system. 451 */ 452 savedit = 0; 453 error = 0; 454 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 && 455 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL && 456 nvap->na_vattr.va_size > 0) { 457 savsize = nvap->na_vattr.va_size; 458 nvap->na_vattr.va_size = VNOVAL; 459 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL || 460 nvap->na_vattr.va_gid != (gid_t)VNOVAL || 461 nvap->na_vattr.va_mode != (mode_t)VNOVAL || 462 nvap->na_vattr.va_atime.tv_sec != VNOVAL || 463 nvap->na_vattr.va_mtime.tv_sec != VNOVAL) 464 savedit = 1; 465 else 466 savedit = 2; 467 } 468 if (savedit != 2) 469 error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 470 if (savedit != 0) 471 nvap->na_vattr.va_size = savsize; 472 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL || 473 nvap->na_vattr.va_gid != (gid_t)VNOVAL || 474 nvap->na_vattr.va_size != VNOVAL || 475 nvap->na_vattr.va_mode != (mode_t)VNOVAL || 476 nvap->na_vattr.va_atime.tv_sec != VNOVAL || 477 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) { 478 /* For a pNFS server, set the attributes on the DS file. */ 479 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR, 480 NULL, NULL, NULL, nvap, NULL); 481 if (error == ENOENT) 482 error = 0; 483 } 484 NFSEXITCODE(error); 485 return (error); 486 } 487 488 /* 489 * Set up nameidata for a lookup() call and do it. 490 */ 491 int 492 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 493 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 494 struct vnode **retdirp) 495 { 496 struct componentname *cnp = &ndp->ni_cnd; 497 int i; 498 struct iovec aiov; 499 struct uio auio; 500 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 501 int error = 0; 502 char *cp; 503 504 *retdirp = NULL; 505 cnp->cn_nameptr = cnp->cn_pnbuf; 506 ndp->ni_lcf = 0; 507 /* 508 * Extract and set starting directory. 509 */ 510 if (dp->v_type != VDIR) { 511 if (islocked) 512 vput(dp); 513 else 514 vrele(dp); 515 nfsvno_relpathbuf(ndp); 516 error = ENOTDIR; 517 goto out1; 518 } 519 if (islocked) 520 NFSVOPUNLOCK(dp, 0); 521 VREF(dp); 522 *retdirp = dp; 523 if (NFSVNO_EXRDONLY(exp)) 524 cnp->cn_flags |= RDONLY; 525 ndp->ni_segflg = UIO_SYSSPACE; 526 527 if (nd->nd_flag & ND_PUBLOOKUP) { 528 ndp->ni_loopcnt = 0; 529 if (cnp->cn_pnbuf[0] == '/') { 530 vrele(dp); 531 /* 532 * Check for degenerate pathnames here, since lookup() 533 * panics on them. 534 */ 535 for (i = 1; i < ndp->ni_pathlen; i++) 536 if (cnp->cn_pnbuf[i] != '/') 537 break; 538 if (i == ndp->ni_pathlen) { 539 error = NFSERR_ACCES; 540 goto out; 541 } 542 dp = rootvnode; 543 VREF(dp); 544 } 545 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 546 (nd->nd_flag & ND_NFSV4) == 0) { 547 /* 548 * Only cross mount points for NFSv4 when doing a 549 * mount while traversing the file system above 550 * the mount point, unless nfsrv_enable_crossmntpt is set. 551 */ 552 cnp->cn_flags |= NOCROSSMOUNT; 553 } 554 555 /* 556 * Initialize for scan, set ni_startdir and bump ref on dp again 557 * because lookup() will dereference ni_startdir. 558 */ 559 560 cnp->cn_thread = p; 561 ndp->ni_startdir = dp; 562 ndp->ni_rootdir = rootvnode; 563 ndp->ni_topdir = NULL; 564 565 if (!lockleaf) 566 cnp->cn_flags |= LOCKLEAF; 567 for (;;) { 568 cnp->cn_nameptr = cnp->cn_pnbuf; 569 /* 570 * Call lookup() to do the real work. If an error occurs, 571 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 572 * we do not have to dereference anything before returning. 573 * In either case ni_startdir will be dereferenced and NULLed 574 * out. 575 */ 576 error = lookup(ndp); 577 if (error) 578 break; 579 580 /* 581 * Check for encountering a symbolic link. Trivial 582 * termination occurs if no symlink encountered. 583 */ 584 if ((cnp->cn_flags & ISSYMLINK) == 0) { 585 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 586 nfsvno_relpathbuf(ndp); 587 if (ndp->ni_vp && !lockleaf) 588 NFSVOPUNLOCK(ndp->ni_vp, 0); 589 break; 590 } 591 592 /* 593 * Validate symlink 594 */ 595 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 596 NFSVOPUNLOCK(ndp->ni_dvp, 0); 597 if (!(nd->nd_flag & ND_PUBLOOKUP)) { 598 error = EINVAL; 599 goto badlink2; 600 } 601 602 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 603 error = ELOOP; 604 goto badlink2; 605 } 606 if (ndp->ni_pathlen > 1) 607 cp = uma_zalloc(namei_zone, M_WAITOK); 608 else 609 cp = cnp->cn_pnbuf; 610 aiov.iov_base = cp; 611 aiov.iov_len = MAXPATHLEN; 612 auio.uio_iov = &aiov; 613 auio.uio_iovcnt = 1; 614 auio.uio_offset = 0; 615 auio.uio_rw = UIO_READ; 616 auio.uio_segflg = UIO_SYSSPACE; 617 auio.uio_td = NULL; 618 auio.uio_resid = MAXPATHLEN; 619 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 620 if (error) { 621 badlink1: 622 if (ndp->ni_pathlen > 1) 623 uma_zfree(namei_zone, cp); 624 badlink2: 625 vrele(ndp->ni_dvp); 626 vput(ndp->ni_vp); 627 break; 628 } 629 linklen = MAXPATHLEN - auio.uio_resid; 630 if (linklen == 0) { 631 error = ENOENT; 632 goto badlink1; 633 } 634 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 635 error = ENAMETOOLONG; 636 goto badlink1; 637 } 638 639 /* 640 * Adjust or replace path 641 */ 642 if (ndp->ni_pathlen > 1) { 643 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 644 uma_zfree(namei_zone, cnp->cn_pnbuf); 645 cnp->cn_pnbuf = cp; 646 } else 647 cnp->cn_pnbuf[linklen] = '\0'; 648 ndp->ni_pathlen += linklen; 649 650 /* 651 * Cleanup refs for next loop and check if root directory 652 * should replace current directory. Normally ni_dvp 653 * becomes the new base directory and is cleaned up when 654 * we loop. Explicitly null pointers after invalidation 655 * to clarify operation. 656 */ 657 vput(ndp->ni_vp); 658 ndp->ni_vp = NULL; 659 660 if (cnp->cn_pnbuf[0] == '/') { 661 vrele(ndp->ni_dvp); 662 ndp->ni_dvp = ndp->ni_rootdir; 663 VREF(ndp->ni_dvp); 664 } 665 ndp->ni_startdir = ndp->ni_dvp; 666 ndp->ni_dvp = NULL; 667 } 668 if (!lockleaf) 669 cnp->cn_flags &= ~LOCKLEAF; 670 671 out: 672 if (error) { 673 nfsvno_relpathbuf(ndp); 674 ndp->ni_vp = NULL; 675 ndp->ni_dvp = NULL; 676 ndp->ni_startdir = NULL; 677 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 678 ndp->ni_dvp = NULL; 679 } 680 681 out1: 682 NFSEXITCODE2(error, nd); 683 return (error); 684 } 685 686 /* 687 * Set up a pathname buffer and return a pointer to it and, optionally 688 * set a hash pointer. 689 */ 690 void 691 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 692 { 693 struct componentname *cnp = &ndp->ni_cnd; 694 695 cnp->cn_flags |= (NOMACCHECK | HASBUF); 696 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 697 if (hashpp != NULL) 698 *hashpp = NULL; 699 *bufpp = cnp->cn_pnbuf; 700 } 701 702 /* 703 * Release the above path buffer, if not released by nfsvno_namei(). 704 */ 705 void 706 nfsvno_relpathbuf(struct nameidata *ndp) 707 { 708 709 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 710 panic("nfsrelpath"); 711 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 712 ndp->ni_cnd.cn_flags &= ~HASBUF; 713 } 714 715 /* 716 * Readlink vnode op into an mbuf list. 717 */ 718 int 719 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 720 struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 721 { 722 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 723 struct iovec *ivp = iv; 724 struct uio io, *uiop = &io; 725 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 726 int i, len, tlen, error = 0; 727 728 len = 0; 729 i = 0; 730 while (len < NFS_MAXPATHLEN) { 731 NFSMGET(mp); 732 MCLGET(mp, M_WAITOK); 733 mp->m_len = M_SIZE(mp); 734 if (len == 0) { 735 mp3 = mp2 = mp; 736 } else { 737 mp2->m_next = mp; 738 mp2 = mp; 739 } 740 if ((len + mp->m_len) > NFS_MAXPATHLEN) { 741 mp->m_len = NFS_MAXPATHLEN - len; 742 len = NFS_MAXPATHLEN; 743 } else { 744 len += mp->m_len; 745 } 746 ivp->iov_base = mtod(mp, caddr_t); 747 ivp->iov_len = mp->m_len; 748 i++; 749 ivp++; 750 } 751 uiop->uio_iov = iv; 752 uiop->uio_iovcnt = i; 753 uiop->uio_offset = 0; 754 uiop->uio_resid = len; 755 uiop->uio_rw = UIO_READ; 756 uiop->uio_segflg = UIO_SYSSPACE; 757 uiop->uio_td = NULL; 758 error = VOP_READLINK(vp, uiop, cred); 759 if (error) { 760 m_freem(mp3); 761 *lenp = 0; 762 goto out; 763 } 764 if (uiop->uio_resid > 0) { 765 len -= uiop->uio_resid; 766 tlen = NFSM_RNDUP(len); 767 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 768 } 769 *lenp = len; 770 *mpp = mp3; 771 *mpendp = mp; 772 773 out: 774 NFSEXITCODE(error); 775 return (error); 776 } 777 778 /* 779 * Read vnode op call into mbuf list. 780 */ 781 int 782 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 783 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 784 { 785 struct mbuf *m; 786 int i; 787 struct iovec *iv; 788 struct iovec *iv2; 789 int error = 0, len, left, siz, tlen, ioflag = 0; 790 struct mbuf *m2 = NULL, *m3; 791 struct uio io, *uiop = &io; 792 struct nfsheur *nh; 793 794 /* 795 * Attempt to read from a DS file. A return of ENOENT implies 796 * there is no DS file to read. 797 */ 798 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp, 799 NULL, mpendp, NULL, NULL); 800 if (error != ENOENT) 801 return (error); 802 803 len = left = NFSM_RNDUP(cnt); 804 m3 = NULL; 805 /* 806 * Generate the mbuf list with the uio_iov ref. to it. 807 */ 808 i = 0; 809 while (left > 0) { 810 NFSMGET(m); 811 MCLGET(m, M_WAITOK); 812 m->m_len = 0; 813 siz = min(M_TRAILINGSPACE(m), left); 814 left -= siz; 815 i++; 816 if (m3) 817 m2->m_next = m; 818 else 819 m3 = m; 820 m2 = m; 821 } 822 iv = malloc(i * sizeof (struct iovec), 823 M_TEMP, M_WAITOK); 824 uiop->uio_iov = iv2 = iv; 825 m = m3; 826 left = len; 827 i = 0; 828 while (left > 0) { 829 if (m == NULL) 830 panic("nfsvno_read iov"); 831 siz = min(M_TRAILINGSPACE(m), left); 832 if (siz > 0) { 833 iv->iov_base = mtod(m, caddr_t) + m->m_len; 834 iv->iov_len = siz; 835 m->m_len += siz; 836 left -= siz; 837 iv++; 838 i++; 839 } 840 m = m->m_next; 841 } 842 uiop->uio_iovcnt = i; 843 uiop->uio_offset = off; 844 uiop->uio_resid = len; 845 uiop->uio_rw = UIO_READ; 846 uiop->uio_segflg = UIO_SYSSPACE; 847 uiop->uio_td = NULL; 848 nh = nfsrv_sequential_heuristic(uiop, vp); 849 ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 850 /* XXX KDM make this more systematic? */ 851 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid; 852 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 853 free(iv2, M_TEMP); 854 if (error) { 855 m_freem(m3); 856 *mpp = NULL; 857 goto out; 858 } 859 nh->nh_nextoff = uiop->uio_offset; 860 tlen = len - uiop->uio_resid; 861 cnt = cnt < tlen ? cnt : tlen; 862 tlen = NFSM_RNDUP(cnt); 863 if (tlen == 0) { 864 m_freem(m3); 865 m3 = NULL; 866 } else if (len != tlen || tlen != cnt) 867 nfsrv_adj(m3, len - tlen, tlen - cnt); 868 *mpp = m3; 869 *mpendp = m2; 870 871 out: 872 NFSEXITCODE(error); 873 return (error); 874 } 875 876 /* 877 * Write vnode op from an mbuf list. 878 */ 879 int 880 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int *stable, 881 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 882 { 883 struct iovec *ivp; 884 int i, len; 885 struct iovec *iv; 886 int ioflags, error; 887 struct uio io, *uiop = &io; 888 struct nfsheur *nh; 889 890 /* 891 * Attempt to write to a DS file. A return of ENOENT implies 892 * there is no DS file to write. 893 */ 894 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS, 895 &mp, cp, NULL, NULL, NULL); 896 if (error != ENOENT) { 897 *stable = NFSWRITE_FILESYNC; 898 return (error); 899 } 900 901 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP, 902 M_WAITOK); 903 uiop->uio_iov = iv = ivp; 904 uiop->uio_iovcnt = cnt; 905 i = mtod(mp, caddr_t) + mp->m_len - cp; 906 len = retlen; 907 while (len > 0) { 908 if (mp == NULL) 909 panic("nfsvno_write"); 910 if (i > 0) { 911 i = min(i, len); 912 ivp->iov_base = cp; 913 ivp->iov_len = i; 914 ivp++; 915 len -= i; 916 } 917 mp = mp->m_next; 918 if (mp) { 919 i = mp->m_len; 920 cp = mtod(mp, caddr_t); 921 } 922 } 923 924 if (*stable == NFSWRITE_UNSTABLE) 925 ioflags = IO_NODELOCKED; 926 else 927 ioflags = (IO_SYNC | IO_NODELOCKED); 928 uiop->uio_resid = retlen; 929 uiop->uio_rw = UIO_WRITE; 930 uiop->uio_segflg = UIO_SYSSPACE; 931 NFSUIOPROC(uiop, p); 932 uiop->uio_offset = off; 933 nh = nfsrv_sequential_heuristic(uiop, vp); 934 ioflags |= nh->nh_seqcount << IO_SEQSHIFT; 935 /* XXX KDM make this more systematic? */ 936 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid; 937 error = VOP_WRITE(vp, uiop, ioflags, cred); 938 if (error == 0) 939 nh->nh_nextoff = uiop->uio_offset; 940 free(iv, M_TEMP); 941 942 NFSEXITCODE(error); 943 return (error); 944 } 945 946 /* 947 * Common code for creating a regular file (plus special files for V2). 948 */ 949 int 950 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 951 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 952 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp) 953 { 954 u_quad_t tempsize; 955 int error; 956 struct thread *p = curthread; 957 958 error = nd->nd_repstat; 959 if (!error && ndp->ni_vp == NULL) { 960 if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 961 vrele(ndp->ni_startdir); 962 error = VOP_CREATE(ndp->ni_dvp, 963 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 964 /* For a pNFS server, create the data file on a DS. */ 965 if (error == 0 && nvap->na_type == VREG) { 966 /* 967 * Create a data file on a DS for a pNFS server. 968 * This function just returns if not 969 * running a pNFS DS or the creation fails. 970 */ 971 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr, 972 nd->nd_cred, p); 973 } 974 vput(ndp->ni_dvp); 975 nfsvno_relpathbuf(ndp); 976 if (!error) { 977 if (*exclusive_flagp) { 978 *exclusive_flagp = 0; 979 NFSVNO_ATTRINIT(nvap); 980 nvap->na_atime.tv_sec = cverf[0]; 981 nvap->na_atime.tv_nsec = cverf[1]; 982 error = VOP_SETATTR(ndp->ni_vp, 983 &nvap->na_vattr, nd->nd_cred); 984 if (error != 0) { 985 vput(ndp->ni_vp); 986 ndp->ni_vp = NULL; 987 error = NFSERR_NOTSUPP; 988 } 989 } 990 } 991 /* 992 * NFS V2 Only. nfsrvd_mknod() does this for V3. 993 * (This implies, just get out on an error.) 994 */ 995 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 996 nvap->na_type == VFIFO) { 997 if (nvap->na_type == VCHR && rdev == 0xffffffff) 998 nvap->na_type = VFIFO; 999 if (nvap->na_type != VFIFO && 1000 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) { 1001 vrele(ndp->ni_startdir); 1002 nfsvno_relpathbuf(ndp); 1003 vput(ndp->ni_dvp); 1004 goto out; 1005 } 1006 nvap->na_rdev = rdev; 1007 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 1008 &ndp->ni_cnd, &nvap->na_vattr); 1009 vput(ndp->ni_dvp); 1010 nfsvno_relpathbuf(ndp); 1011 vrele(ndp->ni_startdir); 1012 if (error) 1013 goto out; 1014 } else { 1015 vrele(ndp->ni_startdir); 1016 nfsvno_relpathbuf(ndp); 1017 vput(ndp->ni_dvp); 1018 error = ENXIO; 1019 goto out; 1020 } 1021 *vpp = ndp->ni_vp; 1022 } else { 1023 /* 1024 * Handle cases where error is already set and/or 1025 * the file exists. 1026 * 1 - clean up the lookup 1027 * 2 - iff !error and na_size set, truncate it 1028 */ 1029 vrele(ndp->ni_startdir); 1030 nfsvno_relpathbuf(ndp); 1031 *vpp = ndp->ni_vp; 1032 if (ndp->ni_dvp == *vpp) 1033 vrele(ndp->ni_dvp); 1034 else 1035 vput(ndp->ni_dvp); 1036 if (!error && nvap->na_size != VNOVAL) { 1037 error = nfsvno_accchk(*vpp, VWRITE, 1038 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1039 NFSACCCHK_VPISLOCKED, NULL); 1040 if (!error) { 1041 tempsize = nvap->na_size; 1042 NFSVNO_ATTRINIT(nvap); 1043 nvap->na_size = tempsize; 1044 error = VOP_SETATTR(*vpp, 1045 &nvap->na_vattr, nd->nd_cred); 1046 } 1047 } 1048 if (error) 1049 vput(*vpp); 1050 } 1051 1052 out: 1053 NFSEXITCODE(error); 1054 return (error); 1055 } 1056 1057 /* 1058 * Do a mknod vnode op. 1059 */ 1060 int 1061 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 1062 struct thread *p) 1063 { 1064 int error = 0; 1065 enum vtype vtyp; 1066 1067 vtyp = nvap->na_type; 1068 /* 1069 * Iff doesn't exist, create it. 1070 */ 1071 if (ndp->ni_vp) { 1072 vrele(ndp->ni_startdir); 1073 nfsvno_relpathbuf(ndp); 1074 vput(ndp->ni_dvp); 1075 vrele(ndp->ni_vp); 1076 error = EEXIST; 1077 goto out; 1078 } 1079 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 1080 vrele(ndp->ni_startdir); 1081 nfsvno_relpathbuf(ndp); 1082 vput(ndp->ni_dvp); 1083 error = NFSERR_BADTYPE; 1084 goto out; 1085 } 1086 if (vtyp == VSOCK) { 1087 vrele(ndp->ni_startdir); 1088 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 1089 &ndp->ni_cnd, &nvap->na_vattr); 1090 vput(ndp->ni_dvp); 1091 nfsvno_relpathbuf(ndp); 1092 } else { 1093 if (nvap->na_type != VFIFO && 1094 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) { 1095 vrele(ndp->ni_startdir); 1096 nfsvno_relpathbuf(ndp); 1097 vput(ndp->ni_dvp); 1098 goto out; 1099 } 1100 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 1101 &ndp->ni_cnd, &nvap->na_vattr); 1102 vput(ndp->ni_dvp); 1103 nfsvno_relpathbuf(ndp); 1104 vrele(ndp->ni_startdir); 1105 /* 1106 * Since VOP_MKNOD returns the ni_vp, I can't 1107 * see any reason to do the lookup. 1108 */ 1109 } 1110 1111 out: 1112 NFSEXITCODE(error); 1113 return (error); 1114 } 1115 1116 /* 1117 * Mkdir vnode op. 1118 */ 1119 int 1120 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 1121 struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 1122 { 1123 int error = 0; 1124 1125 if (ndp->ni_vp != NULL) { 1126 if (ndp->ni_dvp == ndp->ni_vp) 1127 vrele(ndp->ni_dvp); 1128 else 1129 vput(ndp->ni_dvp); 1130 vrele(ndp->ni_vp); 1131 nfsvno_relpathbuf(ndp); 1132 error = EEXIST; 1133 goto out; 1134 } 1135 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 1136 &nvap->na_vattr); 1137 vput(ndp->ni_dvp); 1138 nfsvno_relpathbuf(ndp); 1139 1140 out: 1141 NFSEXITCODE(error); 1142 return (error); 1143 } 1144 1145 /* 1146 * symlink vnode op. 1147 */ 1148 int 1149 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 1150 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 1151 struct nfsexstuff *exp) 1152 { 1153 int error = 0; 1154 1155 if (ndp->ni_vp) { 1156 vrele(ndp->ni_startdir); 1157 nfsvno_relpathbuf(ndp); 1158 if (ndp->ni_dvp == ndp->ni_vp) 1159 vrele(ndp->ni_dvp); 1160 else 1161 vput(ndp->ni_dvp); 1162 vrele(ndp->ni_vp); 1163 error = EEXIST; 1164 goto out; 1165 } 1166 1167 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 1168 &nvap->na_vattr, pathcp); 1169 vput(ndp->ni_dvp); 1170 vrele(ndp->ni_startdir); 1171 nfsvno_relpathbuf(ndp); 1172 /* 1173 * Although FreeBSD still had the lookup code in 1174 * it for 7/current, there doesn't seem to be any 1175 * point, since VOP_SYMLINK() returns the ni_vp. 1176 * Just vput it for v2. 1177 */ 1178 if (!not_v2 && !error) 1179 vput(ndp->ni_vp); 1180 1181 out: 1182 NFSEXITCODE(error); 1183 return (error); 1184 } 1185 1186 /* 1187 * Parse symbolic link arguments. 1188 * This function has an ugly side effect. It will malloc() an area for 1189 * the symlink and set iov_base to point to it, only if it succeeds. 1190 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 1191 * be FREE'd later. 1192 */ 1193 int 1194 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 1195 struct thread *p, char **pathcpp, int *lenp) 1196 { 1197 u_int32_t *tl; 1198 char *pathcp = NULL; 1199 int error = 0, len; 1200 struct nfsv2_sattr *sp; 1201 1202 *pathcpp = NULL; 1203 *lenp = 0; 1204 if ((nd->nd_flag & ND_NFSV3) && 1205 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p))) 1206 goto nfsmout; 1207 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1208 len = fxdr_unsigned(int, *tl); 1209 if (len > NFS_MAXPATHLEN || len <= 0) { 1210 error = EBADRPC; 1211 goto nfsmout; 1212 } 1213 pathcp = malloc(len + 1, M_TEMP, M_WAITOK); 1214 error = nfsrv_mtostr(nd, pathcp, len); 1215 if (error) 1216 goto nfsmout; 1217 if (nd->nd_flag & ND_NFSV2) { 1218 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1219 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 1220 } 1221 *pathcpp = pathcp; 1222 *lenp = len; 1223 NFSEXITCODE2(0, nd); 1224 return (0); 1225 nfsmout: 1226 if (pathcp) 1227 free(pathcp, M_TEMP); 1228 NFSEXITCODE2(error, nd); 1229 return (error); 1230 } 1231 1232 /* 1233 * Remove a non-directory object. 1234 */ 1235 int 1236 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1237 struct thread *p, struct nfsexstuff *exp) 1238 { 1239 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS]; 1240 int error = 0, mirrorcnt; 1241 char fname[PNFS_FILENAME_LEN + 1]; 1242 fhandle_t fh; 1243 1244 vp = ndp->ni_vp; 1245 dsdvp[0] = NULL; 1246 if (vp->v_type == VDIR) 1247 error = NFSERR_ISDIR; 1248 else if (is_v4) 1249 error = nfsrv_checkremove(vp, 1, p); 1250 if (error == 0) 1251 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh); 1252 if (!error) 1253 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 1254 if (error == 0 && dsdvp[0] != NULL) 1255 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p); 1256 if (ndp->ni_dvp == vp) 1257 vrele(ndp->ni_dvp); 1258 else 1259 vput(ndp->ni_dvp); 1260 vput(vp); 1261 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1262 nfsvno_relpathbuf(ndp); 1263 NFSEXITCODE(error); 1264 return (error); 1265 } 1266 1267 /* 1268 * Remove a directory. 1269 */ 1270 int 1271 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1272 struct thread *p, struct nfsexstuff *exp) 1273 { 1274 struct vnode *vp; 1275 int error = 0; 1276 1277 vp = ndp->ni_vp; 1278 if (vp->v_type != VDIR) { 1279 error = ENOTDIR; 1280 goto out; 1281 } 1282 /* 1283 * No rmdir "." please. 1284 */ 1285 if (ndp->ni_dvp == vp) { 1286 error = EINVAL; 1287 goto out; 1288 } 1289 /* 1290 * The root of a mounted filesystem cannot be deleted. 1291 */ 1292 if (vp->v_vflag & VV_ROOT) 1293 error = EBUSY; 1294 out: 1295 if (!error) 1296 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1297 if (ndp->ni_dvp == vp) 1298 vrele(ndp->ni_dvp); 1299 else 1300 vput(ndp->ni_dvp); 1301 vput(vp); 1302 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1303 nfsvno_relpathbuf(ndp); 1304 NFSEXITCODE(error); 1305 return (error); 1306 } 1307 1308 /* 1309 * Rename vnode op. 1310 */ 1311 int 1312 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1313 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1314 { 1315 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS]; 1316 int error = 0, mirrorcnt; 1317 char fname[PNFS_FILENAME_LEN + 1]; 1318 fhandle_t fh; 1319 1320 dsdvp[0] = NULL; 1321 fvp = fromndp->ni_vp; 1322 if (ndstat) { 1323 vrele(fromndp->ni_dvp); 1324 vrele(fvp); 1325 error = ndstat; 1326 goto out1; 1327 } 1328 tdvp = tondp->ni_dvp; 1329 tvp = tondp->ni_vp; 1330 if (tvp != NULL) { 1331 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1332 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1333 goto out; 1334 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1335 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1336 goto out; 1337 } 1338 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1339 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1340 goto out; 1341 } 1342 1343 /* 1344 * A rename to '.' or '..' results in a prematurely 1345 * unlocked vnode on FreeBSD5, so I'm just going to fail that 1346 * here. 1347 */ 1348 if ((tondp->ni_cnd.cn_namelen == 1 && 1349 tondp->ni_cnd.cn_nameptr[0] == '.') || 1350 (tondp->ni_cnd.cn_namelen == 2 && 1351 tondp->ni_cnd.cn_nameptr[0] == '.' && 1352 tondp->ni_cnd.cn_nameptr[1] == '.')) { 1353 error = EINVAL; 1354 goto out; 1355 } 1356 } 1357 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1358 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1359 goto out; 1360 } 1361 if (fvp->v_mount != tdvp->v_mount) { 1362 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1363 goto out; 1364 } 1365 if (fvp == tdvp) { 1366 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1367 goto out; 1368 } 1369 if (fvp == tvp) { 1370 /* 1371 * If source and destination are the same, there is nothing to 1372 * do. Set error to -1 to indicate this. 1373 */ 1374 error = -1; 1375 goto out; 1376 } 1377 if (ndflag & ND_NFSV4) { 1378 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) { 1379 error = nfsrv_checkremove(fvp, 0, p); 1380 NFSVOPUNLOCK(fvp, 0); 1381 } else 1382 error = EPERM; 1383 if (tvp && !error) 1384 error = nfsrv_checkremove(tvp, 1, p); 1385 } else { 1386 /* 1387 * For NFSv2 and NFSv3, try to get rid of the delegation, so 1388 * that the NFSv4 client won't be confused by the rename. 1389 * Since nfsd_recalldelegation() can only be called on an 1390 * unlocked vnode at this point and fvp is the file that will 1391 * still exist after the rename, just do fvp. 1392 */ 1393 nfsd_recalldelegation(fvp, p); 1394 } 1395 if (error == 0 && tvp != NULL) { 1396 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh); 1397 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup" 1398 " dsdvp=%p\n", dsdvp[0]); 1399 } 1400 out: 1401 if (!error) { 1402 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1403 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1404 &tondp->ni_cnd); 1405 } else { 1406 if (tdvp == tvp) 1407 vrele(tdvp); 1408 else 1409 vput(tdvp); 1410 if (tvp) 1411 vput(tvp); 1412 vrele(fromndp->ni_dvp); 1413 vrele(fvp); 1414 if (error == -1) 1415 error = 0; 1416 } 1417 1418 /* 1419 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and 1420 * if the rename succeeded, the DS file for the tvp needs to be 1421 * removed. 1422 */ 1423 if (error == 0 && dsdvp[0] != NULL) { 1424 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p); 1425 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n"); 1426 } 1427 1428 vrele(tondp->ni_startdir); 1429 nfsvno_relpathbuf(tondp); 1430 out1: 1431 vrele(fromndp->ni_startdir); 1432 nfsvno_relpathbuf(fromndp); 1433 NFSEXITCODE(error); 1434 return (error); 1435 } 1436 1437 /* 1438 * Link vnode op. 1439 */ 1440 int 1441 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1442 struct thread *p, struct nfsexstuff *exp) 1443 { 1444 struct vnode *xp; 1445 int error = 0; 1446 1447 xp = ndp->ni_vp; 1448 if (xp != NULL) { 1449 error = EEXIST; 1450 } else { 1451 xp = ndp->ni_dvp; 1452 if (vp->v_mount != xp->v_mount) 1453 error = EXDEV; 1454 } 1455 if (!error) { 1456 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 1457 if ((vp->v_iflag & VI_DOOMED) == 0) 1458 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1459 else 1460 error = EPERM; 1461 if (ndp->ni_dvp == vp) 1462 vrele(ndp->ni_dvp); 1463 else 1464 vput(ndp->ni_dvp); 1465 NFSVOPUNLOCK(vp, 0); 1466 } else { 1467 if (ndp->ni_dvp == ndp->ni_vp) 1468 vrele(ndp->ni_dvp); 1469 else 1470 vput(ndp->ni_dvp); 1471 if (ndp->ni_vp) 1472 vrele(ndp->ni_vp); 1473 } 1474 nfsvno_relpathbuf(ndp); 1475 NFSEXITCODE(error); 1476 return (error); 1477 } 1478 1479 /* 1480 * Do the fsync() appropriate for the commit. 1481 */ 1482 int 1483 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1484 struct thread *td) 1485 { 1486 int error = 0; 1487 1488 /* 1489 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of 1490 * file is done. At this time VOP_FSYNC does not accept offset and 1491 * byte count parameters so call VOP_FSYNC the whole file for now. 1492 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3. 1493 * File systems that do not use the buffer cache (as indicated 1494 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC(). 1495 */ 1496 if (cnt == 0 || cnt > MAX_COMMIT_COUNT || 1497 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) { 1498 /* 1499 * Give up and do the whole thing 1500 */ 1501 if (vp->v_object && 1502 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1503 VM_OBJECT_WLOCK(vp->v_object); 1504 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1505 VM_OBJECT_WUNLOCK(vp->v_object); 1506 } 1507 error = VOP_FSYNC(vp, MNT_WAIT, td); 1508 } else { 1509 /* 1510 * Locate and synchronously write any buffers that fall 1511 * into the requested range. Note: we are assuming that 1512 * f_iosize is a power of 2. 1513 */ 1514 int iosize = vp->v_mount->mnt_stat.f_iosize; 1515 int iomask = iosize - 1; 1516 struct bufobj *bo; 1517 daddr_t lblkno; 1518 1519 /* 1520 * Align to iosize boundary, super-align to page boundary. 1521 */ 1522 if (off & iomask) { 1523 cnt += off & iomask; 1524 off &= ~(u_quad_t)iomask; 1525 } 1526 if (off & PAGE_MASK) { 1527 cnt += off & PAGE_MASK; 1528 off &= ~(u_quad_t)PAGE_MASK; 1529 } 1530 lblkno = off / iosize; 1531 1532 if (vp->v_object && 1533 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1534 VM_OBJECT_WLOCK(vp->v_object); 1535 vm_object_page_clean(vp->v_object, off, off + cnt, 1536 OBJPC_SYNC); 1537 VM_OBJECT_WUNLOCK(vp->v_object); 1538 } 1539 1540 bo = &vp->v_bufobj; 1541 BO_LOCK(bo); 1542 while (cnt > 0) { 1543 struct buf *bp; 1544 1545 /* 1546 * If we have a buffer and it is marked B_DELWRI we 1547 * have to lock and write it. Otherwise the prior 1548 * write is assumed to have already been committed. 1549 * 1550 * gbincore() can return invalid buffers now so we 1551 * have to check that bit as well (though B_DELWRI 1552 * should not be set if B_INVAL is set there could be 1553 * a race here since we haven't locked the buffer). 1554 */ 1555 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1556 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1557 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) { 1558 BO_LOCK(bo); 1559 continue; /* retry */ 1560 } 1561 if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1562 B_DELWRI) { 1563 bremfree(bp); 1564 bp->b_flags &= ~B_ASYNC; 1565 bwrite(bp); 1566 ++nfs_commit_miss; 1567 } else 1568 BUF_UNLOCK(bp); 1569 BO_LOCK(bo); 1570 } 1571 ++nfs_commit_blks; 1572 if (cnt < iosize) 1573 break; 1574 cnt -= iosize; 1575 ++lblkno; 1576 } 1577 BO_UNLOCK(bo); 1578 } 1579 NFSEXITCODE(error); 1580 return (error); 1581 } 1582 1583 /* 1584 * Statfs vnode op. 1585 */ 1586 int 1587 nfsvno_statfs(struct vnode *vp, struct statfs *sf) 1588 { 1589 struct statfs *tsf; 1590 int error; 1591 1592 tsf = NULL; 1593 if (nfsrv_devidcnt > 0) { 1594 /* For a pNFS service, get the DS numbers. */ 1595 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO); 1596 error = nfsrv_pnfsstatfs(tsf, vp->v_mount); 1597 if (error != 0) { 1598 free(tsf, M_TEMP); 1599 tsf = NULL; 1600 } 1601 } 1602 error = VFS_STATFS(vp->v_mount, sf); 1603 if (error == 0) { 1604 if (tsf != NULL) { 1605 sf->f_blocks = tsf->f_blocks; 1606 sf->f_bavail = tsf->f_bavail; 1607 sf->f_bfree = tsf->f_bfree; 1608 sf->f_bsize = tsf->f_bsize; 1609 } 1610 /* 1611 * Since NFS handles these values as unsigned on the 1612 * wire, there is no way to represent negative values, 1613 * so set them to 0. Without this, they will appear 1614 * to be very large positive values for clients like 1615 * Solaris10. 1616 */ 1617 if (sf->f_bavail < 0) 1618 sf->f_bavail = 0; 1619 if (sf->f_ffree < 0) 1620 sf->f_ffree = 0; 1621 } 1622 free(tsf, M_TEMP); 1623 NFSEXITCODE(error); 1624 return (error); 1625 } 1626 1627 /* 1628 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1629 * must handle nfsrv_opencheck() calls after any other access checks. 1630 */ 1631 void 1632 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1633 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1634 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1635 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, 1636 struct nfsexstuff *exp, struct vnode **vpp) 1637 { 1638 struct vnode *vp = NULL; 1639 u_quad_t tempsize; 1640 struct nfsexstuff nes; 1641 struct thread *p = curthread; 1642 1643 if (ndp->ni_vp == NULL) 1644 nd->nd_repstat = nfsrv_opencheck(clientid, 1645 stateidp, stp, NULL, nd, p, nd->nd_repstat); 1646 if (!nd->nd_repstat) { 1647 if (ndp->ni_vp == NULL) { 1648 vrele(ndp->ni_startdir); 1649 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1650 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1651 /* For a pNFS server, create the data file on a DS. */ 1652 if (nd->nd_repstat == 0) { 1653 /* 1654 * Create a data file on a DS for a pNFS server. 1655 * This function just returns if not 1656 * running a pNFS DS or the creation fails. 1657 */ 1658 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr, 1659 cred, p); 1660 } 1661 vput(ndp->ni_dvp); 1662 nfsvno_relpathbuf(ndp); 1663 if (!nd->nd_repstat) { 1664 if (*exclusive_flagp) { 1665 *exclusive_flagp = 0; 1666 NFSVNO_ATTRINIT(nvap); 1667 nvap->na_atime.tv_sec = cverf[0]; 1668 nvap->na_atime.tv_nsec = cverf[1]; 1669 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1670 &nvap->na_vattr, cred); 1671 if (nd->nd_repstat != 0) { 1672 vput(ndp->ni_vp); 1673 ndp->ni_vp = NULL; 1674 nd->nd_repstat = NFSERR_NOTSUPP; 1675 } else 1676 NFSSETBIT_ATTRBIT(attrbitp, 1677 NFSATTRBIT_TIMEACCESS); 1678 } else { 1679 nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1680 aclp, p, attrbitp, exp); 1681 } 1682 } 1683 vp = ndp->ni_vp; 1684 } else { 1685 if (ndp->ni_startdir) 1686 vrele(ndp->ni_startdir); 1687 nfsvno_relpathbuf(ndp); 1688 vp = ndp->ni_vp; 1689 if (create == NFSV4OPEN_CREATE) { 1690 if (ndp->ni_dvp == vp) 1691 vrele(ndp->ni_dvp); 1692 else 1693 vput(ndp->ni_dvp); 1694 } 1695 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1696 if (ndp->ni_cnd.cn_flags & RDONLY) 1697 NFSVNO_SETEXRDONLY(&nes); 1698 else 1699 NFSVNO_EXINIT(&nes); 1700 nd->nd_repstat = nfsvno_accchk(vp, 1701 VWRITE, cred, &nes, p, 1702 NFSACCCHK_NOOVERRIDE, 1703 NFSACCCHK_VPISLOCKED, NULL); 1704 nd->nd_repstat = nfsrv_opencheck(clientid, 1705 stateidp, stp, vp, nd, p, nd->nd_repstat); 1706 if (!nd->nd_repstat) { 1707 tempsize = nvap->na_size; 1708 NFSVNO_ATTRINIT(nvap); 1709 nvap->na_size = tempsize; 1710 nd->nd_repstat = VOP_SETATTR(vp, 1711 &nvap->na_vattr, cred); 1712 } 1713 } else if (vp->v_type == VREG) { 1714 nd->nd_repstat = nfsrv_opencheck(clientid, 1715 stateidp, stp, vp, nd, p, nd->nd_repstat); 1716 } 1717 } 1718 } else { 1719 if (ndp->ni_cnd.cn_flags & HASBUF) 1720 nfsvno_relpathbuf(ndp); 1721 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1722 vrele(ndp->ni_startdir); 1723 if (ndp->ni_dvp == ndp->ni_vp) 1724 vrele(ndp->ni_dvp); 1725 else 1726 vput(ndp->ni_dvp); 1727 if (ndp->ni_vp) 1728 vput(ndp->ni_vp); 1729 } 1730 } 1731 *vpp = vp; 1732 1733 NFSEXITCODE2(0, nd); 1734 } 1735 1736 /* 1737 * Updates the file rev and sets the mtime and ctime 1738 * to the current clock time, returning the va_filerev and va_Xtime 1739 * values. 1740 * Return ESTALE to indicate the vnode is VI_DOOMED. 1741 */ 1742 int 1743 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1744 struct nfsrv_descript *nd, struct thread *p) 1745 { 1746 struct vattr va; 1747 1748 VATTR_NULL(&va); 1749 vfs_timestamp(&va.va_mtime); 1750 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 1751 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY); 1752 if ((vp->v_iflag & VI_DOOMED) != 0) 1753 return (ESTALE); 1754 } 1755 (void) VOP_SETATTR(vp, &va, nd->nd_cred); 1756 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL); 1757 return (0); 1758 } 1759 1760 /* 1761 * Glue routine to nfsv4_fillattr(). 1762 */ 1763 int 1764 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp, 1765 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1766 struct ucred *cred, struct thread *p, int isdgram, int reterr, 1767 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno) 1768 { 1769 struct statfs *sf; 1770 int error; 1771 1772 sf = NULL; 1773 if (nfsrv_devidcnt > 0 && 1774 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) || 1775 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) || 1776 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) { 1777 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO); 1778 error = nfsrv_pnfsstatfs(sf, mp); 1779 if (error != 0) { 1780 free(sf, M_TEMP); 1781 sf = NULL; 1782 } 1783 } 1784 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 1785 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 1786 mounted_on_fileno, sf); 1787 free(sf, M_TEMP); 1788 NFSEXITCODE2(0, nd); 1789 return (error); 1790 } 1791 1792 /* Since the Readdir vnode ops vary, put the entire functions in here. */ 1793 /* 1794 * nfs readdir service 1795 * - mallocs what it thinks is enough to read 1796 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1797 * - calls VOP_READDIR() 1798 * - loops around building the reply 1799 * if the output generated exceeds count break out of loop 1800 * The NFSM_CLGET macro is used here so that the reply will be packed 1801 * tightly in mbuf clusters. 1802 * - it trims out records with d_fileno == 0 1803 * this doesn't matter for Unix clients, but they might confuse clients 1804 * for other os'. 1805 * - it trims out records with d_type == DT_WHT 1806 * these cannot be seen through NFS (unless we extend the protocol) 1807 * The alternate call nfsrvd_readdirplus() does lookups as well. 1808 * PS: The NFS protocol spec. does not clarify what the "count" byte 1809 * argument is a count of.. just name strings and file id's or the 1810 * entire reply rpc or ... 1811 * I tried just file name and id sizes and it confused the Sun client, 1812 * so I am using the full rpc size now. The "paranoia.." comment refers 1813 * to including the status longwords that are not a part of the dir. 1814 * "entry" structures, but are in the rpc. 1815 */ 1816 int 1817 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1818 struct vnode *vp, struct nfsexstuff *exp) 1819 { 1820 struct dirent *dp; 1821 u_int32_t *tl; 1822 int dirlen; 1823 char *cpos, *cend, *rbuf; 1824 struct nfsvattr at; 1825 int nlen, error = 0, getret = 1; 1826 int siz, cnt, fullsiz, eofflag, ncookies; 1827 u_int64_t off, toff, verf __unused; 1828 u_long *cookies = NULL, *cookiep; 1829 struct uio io; 1830 struct iovec iv; 1831 int is_ufs; 1832 struct thread *p = curthread; 1833 1834 if (nd->nd_repstat) { 1835 nfsrv_postopattr(nd, getret, &at); 1836 goto out; 1837 } 1838 if (nd->nd_flag & ND_NFSV2) { 1839 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1840 off = fxdr_unsigned(u_quad_t, *tl++); 1841 } else { 1842 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1843 off = fxdr_hyper(tl); 1844 tl += 2; 1845 verf = fxdr_hyper(tl); 1846 tl += 2; 1847 } 1848 toff = off; 1849 cnt = fxdr_unsigned(int, *tl); 1850 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1851 cnt = NFS_SRVMAXDATA(nd); 1852 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1853 fullsiz = siz; 1854 if (nd->nd_flag & ND_NFSV3) { 1855 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, 1856 NULL); 1857 #if 0 1858 /* 1859 * va_filerev is not sufficient as a cookie verifier, 1860 * since it is not supposed to change when entries are 1861 * removed/added unless that offset cookies returned to 1862 * the client are no longer valid. 1863 */ 1864 if (!nd->nd_repstat && toff && verf != at.na_filerev) 1865 nd->nd_repstat = NFSERR_BAD_COOKIE; 1866 #endif 1867 } 1868 if (!nd->nd_repstat && vp->v_type != VDIR) 1869 nd->nd_repstat = NFSERR_NOTDIR; 1870 if (nd->nd_repstat == 0 && cnt == 0) { 1871 if (nd->nd_flag & ND_NFSV2) 1872 /* NFSv2 does not have NFSERR_TOOSMALL */ 1873 nd->nd_repstat = EPERM; 1874 else 1875 nd->nd_repstat = NFSERR_TOOSMALL; 1876 } 1877 if (!nd->nd_repstat) 1878 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1879 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1880 NFSACCCHK_VPISLOCKED, NULL); 1881 if (nd->nd_repstat) { 1882 vput(vp); 1883 if (nd->nd_flag & ND_NFSV3) 1884 nfsrv_postopattr(nd, getret, &at); 1885 goto out; 1886 } 1887 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 1888 rbuf = malloc(siz, M_TEMP, M_WAITOK); 1889 again: 1890 eofflag = 0; 1891 if (cookies) { 1892 free(cookies, M_TEMP); 1893 cookies = NULL; 1894 } 1895 1896 iv.iov_base = rbuf; 1897 iv.iov_len = siz; 1898 io.uio_iov = &iv; 1899 io.uio_iovcnt = 1; 1900 io.uio_offset = (off_t)off; 1901 io.uio_resid = siz; 1902 io.uio_segflg = UIO_SYSSPACE; 1903 io.uio_rw = UIO_READ; 1904 io.uio_td = NULL; 1905 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1906 &cookies); 1907 off = (u_int64_t)io.uio_offset; 1908 if (io.uio_resid) 1909 siz -= io.uio_resid; 1910 1911 if (!cookies && !nd->nd_repstat) 1912 nd->nd_repstat = NFSERR_PERM; 1913 if (nd->nd_flag & ND_NFSV3) { 1914 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL); 1915 if (!nd->nd_repstat) 1916 nd->nd_repstat = getret; 1917 } 1918 1919 /* 1920 * Handles the failed cases. nd->nd_repstat == 0 past here. 1921 */ 1922 if (nd->nd_repstat) { 1923 vput(vp); 1924 free(rbuf, M_TEMP); 1925 if (cookies) 1926 free(cookies, M_TEMP); 1927 if (nd->nd_flag & ND_NFSV3) 1928 nfsrv_postopattr(nd, getret, &at); 1929 goto out; 1930 } 1931 /* 1932 * If nothing read, return eof 1933 * rpc reply 1934 */ 1935 if (siz == 0) { 1936 vput(vp); 1937 if (nd->nd_flag & ND_NFSV2) { 1938 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1939 } else { 1940 nfsrv_postopattr(nd, getret, &at); 1941 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1942 txdr_hyper(at.na_filerev, tl); 1943 tl += 2; 1944 } 1945 *tl++ = newnfs_false; 1946 *tl = newnfs_true; 1947 free(rbuf, M_TEMP); 1948 free(cookies, M_TEMP); 1949 goto out; 1950 } 1951 1952 /* 1953 * Check for degenerate cases of nothing useful read. 1954 * If so go try again 1955 */ 1956 cpos = rbuf; 1957 cend = rbuf + siz; 1958 dp = (struct dirent *)cpos; 1959 cookiep = cookies; 1960 1961 /* 1962 * For some reason FreeBSD's ufs_readdir() chooses to back the 1963 * directory offset up to a block boundary, so it is necessary to 1964 * skip over the records that precede the requested offset. This 1965 * requires the assumption that file offset cookies monotonically 1966 * increase. 1967 */ 1968 while (cpos < cend && ncookies > 0 && 1969 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1970 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) { 1971 cpos += dp->d_reclen; 1972 dp = (struct dirent *)cpos; 1973 cookiep++; 1974 ncookies--; 1975 } 1976 if (cpos >= cend || ncookies == 0) { 1977 siz = fullsiz; 1978 toff = off; 1979 goto again; 1980 } 1981 vput(vp); 1982 1983 /* 1984 * dirlen is the size of the reply, including all XDR and must 1985 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1986 * if the XDR should be included in "count", but to be safe, we do. 1987 * (Include the two booleans at the end of the reply in dirlen now.) 1988 */ 1989 if (nd->nd_flag & ND_NFSV3) { 1990 nfsrv_postopattr(nd, getret, &at); 1991 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1992 txdr_hyper(at.na_filerev, tl); 1993 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1994 } else { 1995 dirlen = 2 * NFSX_UNSIGNED; 1996 } 1997 1998 /* Loop through the records and build reply */ 1999 while (cpos < cend && ncookies > 0) { 2000 nlen = dp->d_namlen; 2001 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2002 nlen <= NFS_MAXNAMLEN) { 2003 if (nd->nd_flag & ND_NFSV3) 2004 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 2005 else 2006 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 2007 if (dirlen > cnt) { 2008 eofflag = 0; 2009 break; 2010 } 2011 2012 /* 2013 * Build the directory record xdr from 2014 * the dirent entry. 2015 */ 2016 if (nd->nd_flag & ND_NFSV3) { 2017 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2018 *tl++ = newnfs_true; 2019 *tl++ = 0; 2020 } else { 2021 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2022 *tl++ = newnfs_true; 2023 } 2024 *tl = txdr_unsigned(dp->d_fileno); 2025 (void) nfsm_strtom(nd, dp->d_name, nlen); 2026 if (nd->nd_flag & ND_NFSV3) { 2027 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2028 *tl++ = 0; 2029 } else 2030 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2031 *tl = txdr_unsigned(*cookiep); 2032 } 2033 cpos += dp->d_reclen; 2034 dp = (struct dirent *)cpos; 2035 cookiep++; 2036 ncookies--; 2037 } 2038 if (cpos < cend) 2039 eofflag = 0; 2040 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2041 *tl++ = newnfs_false; 2042 if (eofflag) 2043 *tl = newnfs_true; 2044 else 2045 *tl = newnfs_false; 2046 free(rbuf, M_TEMP); 2047 free(cookies, M_TEMP); 2048 2049 out: 2050 NFSEXITCODE2(0, nd); 2051 return (0); 2052 nfsmout: 2053 vput(vp); 2054 NFSEXITCODE2(error, nd); 2055 return (error); 2056 } 2057 2058 /* 2059 * Readdirplus for V3 and Readdir for V4. 2060 */ 2061 int 2062 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 2063 struct vnode *vp, struct nfsexstuff *exp) 2064 { 2065 struct dirent *dp; 2066 u_int32_t *tl; 2067 int dirlen; 2068 char *cpos, *cend, *rbuf; 2069 struct vnode *nvp; 2070 fhandle_t nfh; 2071 struct nfsvattr nva, at, *nvap = &nva; 2072 struct mbuf *mb0, *mb1; 2073 struct nfsreferral *refp; 2074 int nlen, r, error = 0, getret = 1, usevget = 1; 2075 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 2076 caddr_t bpos0, bpos1; 2077 u_int64_t off, toff, verf; 2078 u_long *cookies = NULL, *cookiep; 2079 nfsattrbit_t attrbits, rderrbits, savbits; 2080 struct uio io; 2081 struct iovec iv; 2082 struct componentname cn; 2083 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls; 2084 struct mount *mp, *new_mp; 2085 uint64_t mounted_on_fileno; 2086 struct thread *p = curthread; 2087 2088 if (nd->nd_repstat) { 2089 nfsrv_postopattr(nd, getret, &at); 2090 goto out; 2091 } 2092 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 2093 off = fxdr_hyper(tl); 2094 toff = off; 2095 tl += 2; 2096 verf = fxdr_hyper(tl); 2097 tl += 2; 2098 siz = fxdr_unsigned(int, *tl++); 2099 cnt = fxdr_unsigned(int, *tl); 2100 2101 /* 2102 * Use the server's maximum data transfer size as the upper bound 2103 * on reply datalen. 2104 */ 2105 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 2106 cnt = NFS_SRVMAXDATA(nd); 2107 2108 /* 2109 * siz is a "hint" of how much directory information (name, fileid, 2110 * cookie) should be in the reply. At least one client "hints" 0, 2111 * so I set it to cnt for that case. I also round it up to the 2112 * next multiple of DIRBLKSIZ. 2113 * Since the size of a Readdirplus directory entry reply will always 2114 * be greater than a directory entry returned by VOP_READDIR(), it 2115 * does not make sense to read more than NFS_SRVMAXDATA() via 2116 * VOP_READDIR(). 2117 */ 2118 if (siz <= 0) 2119 siz = cnt; 2120 else if (siz > NFS_SRVMAXDATA(nd)) 2121 siz = NFS_SRVMAXDATA(nd); 2122 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 2123 2124 if (nd->nd_flag & ND_NFSV4) { 2125 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2126 if (error) 2127 goto nfsmout; 2128 NFSSET_ATTRBIT(&savbits, &attrbits); 2129 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd); 2130 NFSZERO_ATTRBIT(&rderrbits); 2131 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 2132 } else { 2133 NFSZERO_ATTRBIT(&attrbits); 2134 } 2135 fullsiz = siz; 2136 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL); 2137 #if 0 2138 if (!nd->nd_repstat) { 2139 if (off && verf != at.na_filerev) { 2140 /* 2141 * va_filerev is not sufficient as a cookie verifier, 2142 * since it is not supposed to change when entries are 2143 * removed/added unless that offset cookies returned to 2144 * the client are no longer valid. 2145 */ 2146 if (nd->nd_flag & ND_NFSV4) { 2147 nd->nd_repstat = NFSERR_NOTSAME; 2148 } else { 2149 nd->nd_repstat = NFSERR_BAD_COOKIE; 2150 } 2151 } 2152 } 2153 #endif 2154 if (!nd->nd_repstat && vp->v_type != VDIR) 2155 nd->nd_repstat = NFSERR_NOTDIR; 2156 if (!nd->nd_repstat && cnt == 0) 2157 nd->nd_repstat = NFSERR_TOOSMALL; 2158 if (!nd->nd_repstat) 2159 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 2160 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 2161 NFSACCCHK_VPISLOCKED, NULL); 2162 if (nd->nd_repstat) { 2163 vput(vp); 2164 if (nd->nd_flag & ND_NFSV3) 2165 nfsrv_postopattr(nd, getret, &at); 2166 goto out; 2167 } 2168 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 2169 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0; 2170 2171 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2172 again: 2173 eofflag = 0; 2174 if (cookies) { 2175 free(cookies, M_TEMP); 2176 cookies = NULL; 2177 } 2178 2179 iv.iov_base = rbuf; 2180 iv.iov_len = siz; 2181 io.uio_iov = &iv; 2182 io.uio_iovcnt = 1; 2183 io.uio_offset = (off_t)off; 2184 io.uio_resid = siz; 2185 io.uio_segflg = UIO_SYSSPACE; 2186 io.uio_rw = UIO_READ; 2187 io.uio_td = NULL; 2188 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 2189 &cookies); 2190 off = (u_int64_t)io.uio_offset; 2191 if (io.uio_resid) 2192 siz -= io.uio_resid; 2193 2194 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL); 2195 2196 if (!cookies && !nd->nd_repstat) 2197 nd->nd_repstat = NFSERR_PERM; 2198 if (!nd->nd_repstat) 2199 nd->nd_repstat = getret; 2200 if (nd->nd_repstat) { 2201 vput(vp); 2202 if (cookies) 2203 free(cookies, M_TEMP); 2204 free(rbuf, M_TEMP); 2205 if (nd->nd_flag & ND_NFSV3) 2206 nfsrv_postopattr(nd, getret, &at); 2207 goto out; 2208 } 2209 /* 2210 * If nothing read, return eof 2211 * rpc reply 2212 */ 2213 if (siz == 0) { 2214 vput(vp); 2215 if (nd->nd_flag & ND_NFSV3) 2216 nfsrv_postopattr(nd, getret, &at); 2217 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2218 txdr_hyper(at.na_filerev, tl); 2219 tl += 2; 2220 *tl++ = newnfs_false; 2221 *tl = newnfs_true; 2222 free(cookies, M_TEMP); 2223 free(rbuf, M_TEMP); 2224 goto out; 2225 } 2226 2227 /* 2228 * Check for degenerate cases of nothing useful read. 2229 * If so go try again 2230 */ 2231 cpos = rbuf; 2232 cend = rbuf + siz; 2233 dp = (struct dirent *)cpos; 2234 cookiep = cookies; 2235 2236 /* 2237 * For some reason FreeBSD's ufs_readdir() chooses to back the 2238 * directory offset up to a block boundary, so it is necessary to 2239 * skip over the records that precede the requested offset. This 2240 * requires the assumption that file offset cookies monotonically 2241 * increase. 2242 */ 2243 while (cpos < cend && ncookies > 0 && 2244 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 2245 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) || 2246 ((nd->nd_flag & ND_NFSV4) && 2247 ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 2248 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 2249 cpos += dp->d_reclen; 2250 dp = (struct dirent *)cpos; 2251 cookiep++; 2252 ncookies--; 2253 } 2254 if (cpos >= cend || ncookies == 0) { 2255 siz = fullsiz; 2256 toff = off; 2257 goto again; 2258 } 2259 2260 /* 2261 * Busy the file system so that the mount point won't go away 2262 * and, as such, VFS_VGET() can be used safely. 2263 */ 2264 mp = vp->v_mount; 2265 vfs_ref(mp); 2266 NFSVOPUNLOCK(vp, 0); 2267 nd->nd_repstat = vfs_busy(mp, 0); 2268 vfs_rel(mp); 2269 if (nd->nd_repstat != 0) { 2270 vrele(vp); 2271 free(cookies, M_TEMP); 2272 free(rbuf, M_TEMP); 2273 if (nd->nd_flag & ND_NFSV3) 2274 nfsrv_postopattr(nd, getret, &at); 2275 goto out; 2276 } 2277 2278 /* 2279 * Check to see if entries in this directory can be safely acquired 2280 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required. 2281 * ZFS snapshot directories need VOP_LOOKUP(), so that any 2282 * automount of the snapshot directory that is required will 2283 * be done. 2284 * This needs to be done here for NFSv4, since NFSv4 never does 2285 * a VFS_VGET() for "." or "..". 2286 */ 2287 if (is_zfs == 1) { 2288 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp); 2289 if (r == EOPNOTSUPP) { 2290 usevget = 0; 2291 cn.cn_nameiop = LOOKUP; 2292 cn.cn_lkflags = LK_SHARED | LK_RETRY; 2293 cn.cn_cred = nd->nd_cred; 2294 cn.cn_thread = p; 2295 } else if (r == 0) 2296 vput(nvp); 2297 } 2298 2299 /* 2300 * Save this position, in case there is an error before one entry 2301 * is created. 2302 */ 2303 mb0 = nd->nd_mb; 2304 bpos0 = nd->nd_bpos; 2305 2306 /* 2307 * Fill in the first part of the reply. 2308 * dirlen is the reply length in bytes and cannot exceed cnt. 2309 * (Include the two booleans at the end of the reply in dirlen now, 2310 * so we recognize when we have exceeded cnt.) 2311 */ 2312 if (nd->nd_flag & ND_NFSV3) { 2313 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 2314 nfsrv_postopattr(nd, getret, &at); 2315 } else { 2316 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 2317 } 2318 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2319 txdr_hyper(at.na_filerev, tl); 2320 2321 /* 2322 * Save this position, in case there is an empty reply needed. 2323 */ 2324 mb1 = nd->nd_mb; 2325 bpos1 = nd->nd_bpos; 2326 2327 /* Loop through the records and build reply */ 2328 entrycnt = 0; 2329 while (cpos < cend && ncookies > 0 && dirlen < cnt) { 2330 nlen = dp->d_namlen; 2331 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2332 nlen <= NFS_MAXNAMLEN && 2333 ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 2334 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 2335 || (nlen == 1 && dp->d_name[0] != '.'))) { 2336 /* 2337 * Save the current position in the reply, in case 2338 * this entry exceeds cnt. 2339 */ 2340 mb1 = nd->nd_mb; 2341 bpos1 = nd->nd_bpos; 2342 2343 /* 2344 * For readdir_and_lookup get the vnode using 2345 * the file number. 2346 */ 2347 nvp = NULL; 2348 refp = NULL; 2349 r = 0; 2350 at_root = 0; 2351 needs_unbusy = 0; 2352 new_mp = mp; 2353 mounted_on_fileno = (uint64_t)dp->d_fileno; 2354 if ((nd->nd_flag & ND_NFSV3) || 2355 NFSNONZERO_ATTRBIT(&savbits)) { 2356 if (nd->nd_flag & ND_NFSV4) 2357 refp = nfsv4root_getreferral(NULL, 2358 vp, dp->d_fileno); 2359 if (refp == NULL) { 2360 if (usevget) 2361 r = VFS_VGET(mp, dp->d_fileno, 2362 LK_SHARED, &nvp); 2363 else 2364 r = EOPNOTSUPP; 2365 if (r == EOPNOTSUPP) { 2366 if (usevget) { 2367 usevget = 0; 2368 cn.cn_nameiop = LOOKUP; 2369 cn.cn_lkflags = 2370 LK_SHARED | 2371 LK_RETRY; 2372 cn.cn_cred = 2373 nd->nd_cred; 2374 cn.cn_thread = p; 2375 } 2376 cn.cn_nameptr = dp->d_name; 2377 cn.cn_namelen = nlen; 2378 cn.cn_flags = ISLASTCN | 2379 NOFOLLOW | LOCKLEAF; 2380 if (nlen == 2 && 2381 dp->d_name[0] == '.' && 2382 dp->d_name[1] == '.') 2383 cn.cn_flags |= 2384 ISDOTDOT; 2385 if (NFSVOPLOCK(vp, LK_SHARED) 2386 != 0) { 2387 nd->nd_repstat = EPERM; 2388 break; 2389 } 2390 if ((vp->v_vflag & VV_ROOT) != 0 2391 && (cn.cn_flags & ISDOTDOT) 2392 != 0) { 2393 vref(vp); 2394 nvp = vp; 2395 r = 0; 2396 } else { 2397 r = VOP_LOOKUP(vp, &nvp, 2398 &cn); 2399 if (vp != nvp) 2400 NFSVOPUNLOCK(vp, 2401 0); 2402 } 2403 } 2404 2405 /* 2406 * For NFSv4, check to see if nvp is 2407 * a mount point and get the mount 2408 * point vnode, as required. 2409 */ 2410 if (r == 0 && 2411 nfsrv_enable_crossmntpt != 0 && 2412 (nd->nd_flag & ND_NFSV4) != 0 && 2413 nvp->v_type == VDIR && 2414 nvp->v_mountedhere != NULL) { 2415 new_mp = nvp->v_mountedhere; 2416 r = vfs_busy(new_mp, 0); 2417 vput(nvp); 2418 nvp = NULL; 2419 if (r == 0) { 2420 r = VFS_ROOT(new_mp, 2421 LK_SHARED, &nvp); 2422 needs_unbusy = 1; 2423 if (r == 0) 2424 at_root = 1; 2425 } 2426 } 2427 } 2428 2429 /* 2430 * If we failed to look up the entry, then it 2431 * has become invalid, most likely removed. 2432 */ 2433 if (r != 0) { 2434 if (needs_unbusy) 2435 vfs_unbusy(new_mp); 2436 goto invalid; 2437 } 2438 KASSERT(refp != NULL || nvp != NULL, 2439 ("%s: undetected lookup error", __func__)); 2440 2441 if (refp == NULL && 2442 ((nd->nd_flag & ND_NFSV3) || 2443 NFSNONZERO_ATTRBIT(&attrbits))) { 2444 r = nfsvno_getfh(nvp, &nfh, p); 2445 if (!r) 2446 r = nfsvno_getattr(nvp, nvap, nd, p, 2447 1, &attrbits); 2448 if (r == 0 && is_zfs == 1 && 2449 nfsrv_enable_crossmntpt != 0 && 2450 (nd->nd_flag & ND_NFSV4) != 0 && 2451 nvp->v_type == VDIR && 2452 vp->v_mount != nvp->v_mount) { 2453 /* 2454 * For a ZFS snapshot, there is a 2455 * pseudo mount that does not set 2456 * v_mountedhere, so it needs to 2457 * be detected via a different 2458 * mount structure. 2459 */ 2460 at_root = 1; 2461 if (new_mp == mp) 2462 new_mp = nvp->v_mount; 2463 } 2464 } 2465 2466 /* 2467 * If we failed to get attributes of the entry, 2468 * then just skip it for NFSv3 (the traditional 2469 * behavior in the old NFS server). 2470 * For NFSv4 the behavior is controlled by 2471 * RDATTRERROR: we either ignore the error or 2472 * fail the request. 2473 * Note that RDATTRERROR is never set for NFSv3. 2474 */ 2475 if (r != 0) { 2476 if (!NFSISSET_ATTRBIT(&attrbits, 2477 NFSATTRBIT_RDATTRERROR)) { 2478 vput(nvp); 2479 if (needs_unbusy != 0) 2480 vfs_unbusy(new_mp); 2481 if ((nd->nd_flag & ND_NFSV3)) 2482 goto invalid; 2483 nd->nd_repstat = r; 2484 break; 2485 } 2486 } 2487 } 2488 2489 /* 2490 * Build the directory record xdr 2491 */ 2492 if (nd->nd_flag & ND_NFSV3) { 2493 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2494 *tl++ = newnfs_true; 2495 *tl++ = 0; 2496 *tl = txdr_unsigned(dp->d_fileno); 2497 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2498 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2499 *tl++ = 0; 2500 *tl = txdr_unsigned(*cookiep); 2501 nfsrv_postopattr(nd, 0, nvap); 2502 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1); 2503 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 2504 if (nvp != NULL) 2505 vput(nvp); 2506 } else { 2507 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2508 *tl++ = newnfs_true; 2509 *tl++ = 0; 2510 *tl = txdr_unsigned(*cookiep); 2511 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2512 if (nvp != NULL) { 2513 supports_nfsv4acls = 2514 nfs_supportsnfsv4acls(nvp); 2515 NFSVOPUNLOCK(nvp, 0); 2516 } else 2517 supports_nfsv4acls = 0; 2518 if (refp != NULL) { 2519 dirlen += nfsrv_putreferralattr(nd, 2520 &savbits, refp, 0, 2521 &nd->nd_repstat); 2522 if (nd->nd_repstat) { 2523 if (nvp != NULL) 2524 vrele(nvp); 2525 if (needs_unbusy != 0) 2526 vfs_unbusy(new_mp); 2527 break; 2528 } 2529 } else if (r) { 2530 dirlen += nfsvno_fillattr(nd, new_mp, 2531 nvp, nvap, &nfh, r, &rderrbits, 2532 nd->nd_cred, p, isdgram, 0, 2533 supports_nfsv4acls, at_root, 2534 mounted_on_fileno); 2535 } else { 2536 dirlen += nfsvno_fillattr(nd, new_mp, 2537 nvp, nvap, &nfh, r, &attrbits, 2538 nd->nd_cred, p, isdgram, 0, 2539 supports_nfsv4acls, at_root, 2540 mounted_on_fileno); 2541 } 2542 if (nvp != NULL) 2543 vrele(nvp); 2544 dirlen += (3 * NFSX_UNSIGNED); 2545 } 2546 if (needs_unbusy != 0) 2547 vfs_unbusy(new_mp); 2548 if (dirlen <= cnt) 2549 entrycnt++; 2550 } 2551 invalid: 2552 cpos += dp->d_reclen; 2553 dp = (struct dirent *)cpos; 2554 cookiep++; 2555 ncookies--; 2556 } 2557 vrele(vp); 2558 vfs_unbusy(mp); 2559 2560 /* 2561 * If dirlen > cnt, we must strip off the last entry. If that 2562 * results in an empty reply, report NFSERR_TOOSMALL. 2563 */ 2564 if (dirlen > cnt || nd->nd_repstat) { 2565 if (!nd->nd_repstat && entrycnt == 0) 2566 nd->nd_repstat = NFSERR_TOOSMALL; 2567 if (nd->nd_repstat) { 2568 newnfs_trimtrailing(nd, mb0, bpos0); 2569 if (nd->nd_flag & ND_NFSV3) 2570 nfsrv_postopattr(nd, getret, &at); 2571 } else 2572 newnfs_trimtrailing(nd, mb1, bpos1); 2573 eofflag = 0; 2574 } else if (cpos < cend) 2575 eofflag = 0; 2576 if (!nd->nd_repstat) { 2577 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2578 *tl++ = newnfs_false; 2579 if (eofflag) 2580 *tl = newnfs_true; 2581 else 2582 *tl = newnfs_false; 2583 } 2584 free(cookies, M_TEMP); 2585 free(rbuf, M_TEMP); 2586 2587 out: 2588 NFSEXITCODE2(0, nd); 2589 return (0); 2590 nfsmout: 2591 vput(vp); 2592 NFSEXITCODE2(error, nd); 2593 return (error); 2594 } 2595 2596 /* 2597 * Get the settable attributes out of the mbuf list. 2598 * (Return 0 or EBADRPC) 2599 */ 2600 int 2601 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap, 2602 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2603 { 2604 u_int32_t *tl; 2605 struct nfsv2_sattr *sp; 2606 int error = 0, toclient = 0; 2607 2608 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 2609 case ND_NFSV2: 2610 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2611 /* 2612 * Some old clients didn't fill in the high order 16bits. 2613 * --> check the low order 2 bytes for 0xffff 2614 */ 2615 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 2616 nvap->na_mode = nfstov_mode(sp->sa_mode); 2617 if (sp->sa_uid != newnfs_xdrneg1) 2618 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 2619 if (sp->sa_gid != newnfs_xdrneg1) 2620 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 2621 if (sp->sa_size != newnfs_xdrneg1) 2622 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 2623 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 2624 #ifdef notyet 2625 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 2626 #else 2627 nvap->na_atime.tv_sec = 2628 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 2629 nvap->na_atime.tv_nsec = 0; 2630 #endif 2631 } 2632 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 2633 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 2634 break; 2635 case ND_NFSV3: 2636 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2637 if (*tl == newnfs_true) { 2638 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2639 nvap->na_mode = nfstov_mode(*tl); 2640 } 2641 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2642 if (*tl == newnfs_true) { 2643 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2644 nvap->na_uid = fxdr_unsigned(uid_t, *tl); 2645 } 2646 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2647 if (*tl == newnfs_true) { 2648 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2649 nvap->na_gid = fxdr_unsigned(gid_t, *tl); 2650 } 2651 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2652 if (*tl == newnfs_true) { 2653 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2654 nvap->na_size = fxdr_hyper(tl); 2655 } 2656 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2657 switch (fxdr_unsigned(int, *tl)) { 2658 case NFSV3SATTRTIME_TOCLIENT: 2659 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2660 fxdr_nfsv3time(tl, &nvap->na_atime); 2661 toclient = 1; 2662 break; 2663 case NFSV3SATTRTIME_TOSERVER: 2664 vfs_timestamp(&nvap->na_atime); 2665 nvap->na_vaflags |= VA_UTIMES_NULL; 2666 break; 2667 } 2668 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2669 switch (fxdr_unsigned(int, *tl)) { 2670 case NFSV3SATTRTIME_TOCLIENT: 2671 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2672 fxdr_nfsv3time(tl, &nvap->na_mtime); 2673 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2674 break; 2675 case NFSV3SATTRTIME_TOSERVER: 2676 vfs_timestamp(&nvap->na_mtime); 2677 if (!toclient) 2678 nvap->na_vaflags |= VA_UTIMES_NULL; 2679 break; 2680 } 2681 break; 2682 case ND_NFSV4: 2683 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p); 2684 } 2685 nfsmout: 2686 NFSEXITCODE2(error, nd); 2687 return (error); 2688 } 2689 2690 /* 2691 * Handle the setable attributes for V4. 2692 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 2693 */ 2694 int 2695 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap, 2696 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2697 { 2698 u_int32_t *tl; 2699 int attrsum = 0; 2700 int i, j; 2701 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 2702 int moderet, toclient = 0; 2703 u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 2704 uid_t uid; 2705 gid_t gid; 2706 u_short mode, mask; /* Same type as va_mode. */ 2707 struct vattr va; 2708 2709 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 2710 if (error) 2711 goto nfsmout; 2712 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2713 attrsize = fxdr_unsigned(int, *tl); 2714 2715 /* 2716 * Loop around getting the setable attributes. If an unsupported 2717 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 2718 */ 2719 if (retnotsup) { 2720 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2721 bitpos = NFSATTRBIT_MAX; 2722 } else { 2723 bitpos = 0; 2724 } 2725 moderet = 0; 2726 for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 2727 if (attrsum > attrsize) { 2728 error = NFSERR_BADXDR; 2729 goto nfsmout; 2730 } 2731 if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 2732 switch (bitpos) { 2733 case NFSATTRBIT_SIZE: 2734 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2735 if (vp != NULL && vp->v_type != VREG) { 2736 error = (vp->v_type == VDIR) ? NFSERR_ISDIR : 2737 NFSERR_INVAL; 2738 goto nfsmout; 2739 } 2740 nvap->na_size = fxdr_hyper(tl); 2741 attrsum += NFSX_HYPER; 2742 break; 2743 case NFSATTRBIT_ACL: 2744 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize, 2745 p); 2746 if (error) 2747 goto nfsmout; 2748 if (aceerr && !nd->nd_repstat) 2749 nd->nd_repstat = aceerr; 2750 attrsum += aclsize; 2751 break; 2752 case NFSATTRBIT_ARCHIVE: 2753 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2754 if (!nd->nd_repstat) 2755 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2756 attrsum += NFSX_UNSIGNED; 2757 break; 2758 case NFSATTRBIT_HIDDEN: 2759 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2760 if (!nd->nd_repstat) 2761 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2762 attrsum += NFSX_UNSIGNED; 2763 break; 2764 case NFSATTRBIT_MIMETYPE: 2765 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2766 i = fxdr_unsigned(int, *tl); 2767 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 2768 if (error) 2769 goto nfsmout; 2770 if (!nd->nd_repstat) 2771 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2772 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 2773 break; 2774 case NFSATTRBIT_MODE: 2775 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */ 2776 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2777 nvap->na_mode = nfstov_mode(*tl); 2778 attrsum += NFSX_UNSIGNED; 2779 break; 2780 case NFSATTRBIT_OWNER: 2781 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2782 j = fxdr_unsigned(int, *tl); 2783 if (j < 0) { 2784 error = NFSERR_BADXDR; 2785 goto nfsmout; 2786 } 2787 if (j > NFSV4_SMALLSTR) 2788 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2789 else 2790 cp = namestr; 2791 error = nfsrv_mtostr(nd, cp, j); 2792 if (error) { 2793 if (j > NFSV4_SMALLSTR) 2794 free(cp, M_NFSSTRING); 2795 goto nfsmout; 2796 } 2797 if (!nd->nd_repstat) { 2798 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, 2799 &uid); 2800 if (!nd->nd_repstat) 2801 nvap->na_uid = uid; 2802 } 2803 if (j > NFSV4_SMALLSTR) 2804 free(cp, M_NFSSTRING); 2805 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2806 break; 2807 case NFSATTRBIT_OWNERGROUP: 2808 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2809 j = fxdr_unsigned(int, *tl); 2810 if (j < 0) { 2811 error = NFSERR_BADXDR; 2812 goto nfsmout; 2813 } 2814 if (j > NFSV4_SMALLSTR) 2815 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2816 else 2817 cp = namestr; 2818 error = nfsrv_mtostr(nd, cp, j); 2819 if (error) { 2820 if (j > NFSV4_SMALLSTR) 2821 free(cp, M_NFSSTRING); 2822 goto nfsmout; 2823 } 2824 if (!nd->nd_repstat) { 2825 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, 2826 &gid); 2827 if (!nd->nd_repstat) 2828 nvap->na_gid = gid; 2829 } 2830 if (j > NFSV4_SMALLSTR) 2831 free(cp, M_NFSSTRING); 2832 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2833 break; 2834 case NFSATTRBIT_SYSTEM: 2835 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2836 if (!nd->nd_repstat) 2837 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2838 attrsum += NFSX_UNSIGNED; 2839 break; 2840 case NFSATTRBIT_TIMEACCESSSET: 2841 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2842 attrsum += NFSX_UNSIGNED; 2843 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2844 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2845 fxdr_nfsv4time(tl, &nvap->na_atime); 2846 toclient = 1; 2847 attrsum += NFSX_V4TIME; 2848 } else { 2849 vfs_timestamp(&nvap->na_atime); 2850 nvap->na_vaflags |= VA_UTIMES_NULL; 2851 } 2852 break; 2853 case NFSATTRBIT_TIMEBACKUP: 2854 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2855 if (!nd->nd_repstat) 2856 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2857 attrsum += NFSX_V4TIME; 2858 break; 2859 case NFSATTRBIT_TIMECREATE: 2860 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2861 if (!nd->nd_repstat) 2862 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2863 attrsum += NFSX_V4TIME; 2864 break; 2865 case NFSATTRBIT_TIMEMODIFYSET: 2866 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2867 attrsum += NFSX_UNSIGNED; 2868 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2869 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2870 fxdr_nfsv4time(tl, &nvap->na_mtime); 2871 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2872 attrsum += NFSX_V4TIME; 2873 } else { 2874 vfs_timestamp(&nvap->na_mtime); 2875 if (!toclient) 2876 nvap->na_vaflags |= VA_UTIMES_NULL; 2877 } 2878 break; 2879 case NFSATTRBIT_MODESETMASKED: 2880 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 2881 mode = fxdr_unsigned(u_short, *tl++); 2882 mask = fxdr_unsigned(u_short, *tl); 2883 /* 2884 * vp == NULL implies an Open/Create operation. 2885 * This attribute can only be used for Setattr and 2886 * only for NFSv4.1 or higher. 2887 * If moderet != 0, a mode attribute has also been 2888 * specified and this attribute cannot be done in the 2889 * same Setattr operation. 2890 */ 2891 if ((nd->nd_flag & ND_NFSV41) == 0) 2892 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2893 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 || 2894 vp == NULL) 2895 nd->nd_repstat = NFSERR_INVAL; 2896 else if (moderet == 0) 2897 moderet = VOP_GETATTR(vp, &va, nd->nd_cred); 2898 if (moderet == 0) 2899 nvap->na_mode = (mode & mask) | 2900 (va.va_mode & ~mask); 2901 else 2902 nd->nd_repstat = moderet; 2903 attrsum += 2 * NFSX_UNSIGNED; 2904 break; 2905 default: 2906 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2907 /* 2908 * set bitpos so we drop out of the loop. 2909 */ 2910 bitpos = NFSATTRBIT_MAX; 2911 break; 2912 } 2913 } 2914 2915 /* 2916 * some clients pad the attrlist, so we need to skip over the 2917 * padding. 2918 */ 2919 if (attrsum > attrsize) { 2920 error = NFSERR_BADXDR; 2921 } else { 2922 attrsize = NFSM_RNDUP(attrsize); 2923 if (attrsum < attrsize) 2924 error = nfsm_advance(nd, attrsize - attrsum, -1); 2925 } 2926 nfsmout: 2927 NFSEXITCODE2(error, nd); 2928 return (error); 2929 } 2930 2931 /* 2932 * Check/setup export credentials. 2933 */ 2934 int 2935 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 2936 struct ucred *credanon) 2937 { 2938 int error = 0; 2939 2940 /* 2941 * Check/setup credentials. 2942 */ 2943 if (nd->nd_flag & ND_GSS) 2944 exp->nes_exflag &= ~MNT_EXPORTANON; 2945 2946 /* 2947 * Check to see if the operation is allowed for this security flavor. 2948 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to 2949 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 2950 * Also, allow Secinfo, so that it can acquire the correct flavor(s). 2951 */ 2952 if (nfsvno_testexp(nd, exp) && 2953 nd->nd_procnum != NFSV4OP_SECINFO && 2954 nd->nd_procnum != NFSPROC_FSINFO) { 2955 if (nd->nd_flag & ND_NFSV4) 2956 error = NFSERR_WRONGSEC; 2957 else 2958 error = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2959 goto out; 2960 } 2961 2962 /* 2963 * Check to see if the file system is exported V4 only. 2964 */ 2965 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) { 2966 error = NFSERR_PROGNOTV4; 2967 goto out; 2968 } 2969 2970 /* 2971 * Now, map the user credentials. 2972 * (Note that ND_AUTHNONE will only be set for an NFSv3 2973 * Fsinfo RPC. If set for anything else, this code might need 2974 * to change.) 2975 */ 2976 if (NFSVNO_EXPORTED(exp)) { 2977 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) || 2978 NFSVNO_EXPORTANON(exp) || 2979 (nd->nd_flag & ND_AUTHNONE) != 0) { 2980 nd->nd_cred->cr_uid = credanon->cr_uid; 2981 nd->nd_cred->cr_gid = credanon->cr_gid; 2982 crsetgroups(nd->nd_cred, credanon->cr_ngroups, 2983 credanon->cr_groups); 2984 } else if ((nd->nd_flag & ND_GSS) == 0) { 2985 /* 2986 * If using AUTH_SYS, call nfsrv_getgrpscred() to see 2987 * if there is a replacement credential with a group 2988 * list set up by "nfsuserd -manage-gids". 2989 * If there is no replacement, nfsrv_getgrpscred() 2990 * simply returns its argument. 2991 */ 2992 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred); 2993 } 2994 } 2995 2996 out: 2997 NFSEXITCODE2(error, nd); 2998 return (error); 2999 } 3000 3001 /* 3002 * Check exports. 3003 */ 3004 int 3005 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 3006 struct ucred **credp) 3007 { 3008 int i, error, *secflavors; 3009 3010 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 3011 &exp->nes_numsecflavor, &secflavors); 3012 if (error) { 3013 if (nfs_rootfhset) { 3014 exp->nes_exflag = 0; 3015 exp->nes_numsecflavor = 0; 3016 error = 0; 3017 } 3018 } else { 3019 /* Copy the security flavors. */ 3020 for (i = 0; i < exp->nes_numsecflavor; i++) 3021 exp->nes_secflavors[i] = secflavors[i]; 3022 } 3023 NFSEXITCODE(error); 3024 return (error); 3025 } 3026 3027 /* 3028 * Get a vnode for a file handle and export stuff. 3029 */ 3030 int 3031 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 3032 int lktype, struct vnode **vpp, struct nfsexstuff *exp, 3033 struct ucred **credp) 3034 { 3035 int i, error, *secflavors; 3036 3037 *credp = NULL; 3038 exp->nes_numsecflavor = 0; 3039 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp); 3040 if (error != 0) 3041 /* Make sure the server replies ESTALE to the client. */ 3042 error = ESTALE; 3043 if (nam && !error) { 3044 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 3045 &exp->nes_numsecflavor, &secflavors); 3046 if (error) { 3047 if (nfs_rootfhset) { 3048 exp->nes_exflag = 0; 3049 exp->nes_numsecflavor = 0; 3050 error = 0; 3051 } else { 3052 vput(*vpp); 3053 } 3054 } else { 3055 /* Copy the security flavors. */ 3056 for (i = 0; i < exp->nes_numsecflavor; i++) 3057 exp->nes_secflavors[i] = secflavors[i]; 3058 } 3059 } 3060 NFSEXITCODE(error); 3061 return (error); 3062 } 3063 3064 /* 3065 * nfsd_fhtovp() - convert a fh to a vnode ptr 3066 * - look up fsid in mount list (if not found ret error) 3067 * - get vp and export rights by calling nfsvno_fhtovp() 3068 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 3069 * for AUTH_SYS 3070 * - if mpp != NULL, return the mount point so that it can 3071 * be used for vn_finished_write() by the caller 3072 */ 3073 void 3074 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 3075 struct vnode **vpp, struct nfsexstuff *exp, 3076 struct mount **mpp, int startwrite) 3077 { 3078 struct mount *mp; 3079 struct ucred *credanon; 3080 fhandle_t *fhp; 3081 3082 fhp = (fhandle_t *)nfp->nfsrvfh_data; 3083 /* 3084 * Check for the special case of the nfsv4root_fh. 3085 */ 3086 mp = vfs_busyfs(&fhp->fh_fsid); 3087 if (mpp != NULL) 3088 *mpp = mp; 3089 if (mp == NULL) { 3090 *vpp = NULL; 3091 nd->nd_repstat = ESTALE; 3092 goto out; 3093 } 3094 3095 if (startwrite) { 3096 vn_start_write(NULL, mpp, V_WAIT); 3097 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp))) 3098 lktype = LK_EXCLUSIVE; 3099 } 3100 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 3101 &credanon); 3102 vfs_unbusy(mp); 3103 3104 /* 3105 * For NFSv4 without a pseudo root fs, unexported file handles 3106 * can be returned, so that Lookup works everywhere. 3107 */ 3108 if (!nd->nd_repstat && exp->nes_exflag == 0 && 3109 !(nd->nd_flag & ND_NFSV4)) { 3110 vput(*vpp); 3111 nd->nd_repstat = EACCES; 3112 } 3113 3114 /* 3115 * Personally, I've never seen any point in requiring a 3116 * reserved port#, since only in the rare case where the 3117 * clients are all boxes with secure system privileges, 3118 * does it provide any enhanced security, but... some people 3119 * believe it to be useful and keep putting this code back in. 3120 * (There is also some "security checker" out there that 3121 * complains if the nfs server doesn't enforce this.) 3122 * However, note the following: 3123 * RFC3530 (NFSv4) specifies that a reserved port# not be 3124 * required. 3125 * RFC2623 recommends that, if a reserved port# is checked for, 3126 * that there be a way to turn that off--> ifdef'd. 3127 */ 3128 #ifdef NFS_REQRSVPORT 3129 if (!nd->nd_repstat) { 3130 struct sockaddr_in *saddr; 3131 struct sockaddr_in6 *saddr6; 3132 3133 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 3134 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 3135 if (!(nd->nd_flag & ND_NFSV4) && 3136 ((saddr->sin_family == AF_INET && 3137 ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 3138 (saddr6->sin6_family == AF_INET6 && 3139 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 3140 vput(*vpp); 3141 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 3142 } 3143 } 3144 #endif /* NFS_REQRSVPORT */ 3145 3146 /* 3147 * Check/setup credentials. 3148 */ 3149 if (!nd->nd_repstat) { 3150 nd->nd_saveduid = nd->nd_cred->cr_uid; 3151 nd->nd_repstat = nfsd_excred(nd, exp, credanon); 3152 if (nd->nd_repstat) 3153 vput(*vpp); 3154 } 3155 if (credanon != NULL) 3156 crfree(credanon); 3157 if (nd->nd_repstat) { 3158 if (startwrite) 3159 vn_finished_write(mp); 3160 *vpp = NULL; 3161 if (mpp != NULL) 3162 *mpp = NULL; 3163 } 3164 3165 out: 3166 NFSEXITCODE2(0, nd); 3167 } 3168 3169 /* 3170 * glue for fp. 3171 */ 3172 static int 3173 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 3174 { 3175 struct filedesc *fdp; 3176 struct file *fp; 3177 int error = 0; 3178 3179 fdp = p->td_proc->p_fd; 3180 if (fd < 0 || fd >= fdp->fd_nfiles || 3181 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) { 3182 error = EBADF; 3183 goto out; 3184 } 3185 *fpp = fp; 3186 3187 out: 3188 NFSEXITCODE(error); 3189 return (error); 3190 } 3191 3192 /* 3193 * Called from nfssvc() to update the exports list. Just call 3194 * vfs_export(). This has to be done, since the v4 root fake fs isn't 3195 * in the mount list. 3196 */ 3197 int 3198 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 3199 { 3200 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 3201 int error = 0; 3202 struct nameidata nd; 3203 fhandle_t fh; 3204 3205 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export); 3206 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 3207 nfs_rootfhset = 0; 3208 else if (error == 0) { 3209 if (nfsexargp->fspec == NULL) { 3210 error = EPERM; 3211 goto out; 3212 } 3213 /* 3214 * If fspec != NULL, this is the v4root path. 3215 */ 3216 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, 3217 nfsexargp->fspec, p); 3218 if ((error = namei(&nd)) != 0) 3219 goto out; 3220 error = nfsvno_getfh(nd.ni_vp, &fh, p); 3221 vrele(nd.ni_vp); 3222 if (!error) { 3223 nfs_rootfh.nfsrvfh_len = NFSX_MYFH; 3224 NFSBCOPY((caddr_t)&fh, 3225 nfs_rootfh.nfsrvfh_data, 3226 sizeof (fhandle_t)); 3227 nfs_rootfhset = 1; 3228 } 3229 } 3230 3231 out: 3232 NFSEXITCODE(error); 3233 return (error); 3234 } 3235 3236 /* 3237 * This function needs to test to see if the system is near its limit 3238 * for memory allocation via malloc() or mget() and return True iff 3239 * either of these resources are near their limit. 3240 * XXX (For now, this is just a stub.) 3241 */ 3242 int nfsrv_testmalloclimit = 0; 3243 int 3244 nfsrv_mallocmget_limit(void) 3245 { 3246 static int printmesg = 0; 3247 static int testval = 1; 3248 3249 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 3250 if ((printmesg++ % 100) == 0) 3251 printf("nfsd: malloc/mget near limit\n"); 3252 return (1); 3253 } 3254 return (0); 3255 } 3256 3257 /* 3258 * BSD specific initialization of a mount point. 3259 */ 3260 void 3261 nfsd_mntinit(void) 3262 { 3263 static int inited = 0; 3264 3265 if (inited) 3266 return; 3267 inited = 1; 3268 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 3269 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist); 3270 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist); 3271 nfsv4root_mnt.mnt_export = NULL; 3272 TAILQ_INIT(&nfsv4root_opt); 3273 TAILQ_INIT(&nfsv4root_newopt); 3274 nfsv4root_mnt.mnt_opt = &nfsv4root_opt; 3275 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt; 3276 nfsv4root_mnt.mnt_nvnodelistsize = 0; 3277 nfsv4root_mnt.mnt_activevnodelistsize = 0; 3278 } 3279 3280 /* 3281 * Get a vnode for a file handle, without checking exports, etc. 3282 */ 3283 struct vnode * 3284 nfsvno_getvp(fhandle_t *fhp) 3285 { 3286 struct mount *mp; 3287 struct vnode *vp; 3288 int error; 3289 3290 mp = vfs_busyfs(&fhp->fh_fsid); 3291 if (mp == NULL) 3292 return (NULL); 3293 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 3294 vfs_unbusy(mp); 3295 if (error) 3296 return (NULL); 3297 return (vp); 3298 } 3299 3300 /* 3301 * Do a local VOP_ADVLOCK(). 3302 */ 3303 int 3304 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 3305 u_int64_t end, struct thread *td) 3306 { 3307 int error = 0; 3308 struct flock fl; 3309 u_int64_t tlen; 3310 3311 if (nfsrv_dolocallocks == 0) 3312 goto out; 3313 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked"); 3314 3315 fl.l_whence = SEEK_SET; 3316 fl.l_type = ftype; 3317 fl.l_start = (off_t)first; 3318 if (end == NFS64BITSSET) { 3319 fl.l_len = 0; 3320 } else { 3321 tlen = end - first; 3322 fl.l_len = (off_t)tlen; 3323 } 3324 /* 3325 * For FreeBSD8, the l_pid and l_sysid must be set to the same 3326 * values for all calls, so that all locks will be held by the 3327 * nfsd server. (The nfsd server handles conflicts between the 3328 * various clients.) 3329 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 3330 * bytes, so it can't be put in l_sysid. 3331 */ 3332 if (nfsv4_sysid == 0) 3333 nfsv4_sysid = nlm_acquire_next_sysid(); 3334 fl.l_pid = (pid_t)0; 3335 fl.l_sysid = (int)nfsv4_sysid; 3336 3337 if (ftype == F_UNLCK) 3338 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 3339 (F_POSIX | F_REMOTE)); 3340 else 3341 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 3342 (F_POSIX | F_REMOTE)); 3343 3344 out: 3345 NFSEXITCODE(error); 3346 return (error); 3347 } 3348 3349 /* 3350 * Check the nfsv4 root exports. 3351 */ 3352 int 3353 nfsvno_v4rootexport(struct nfsrv_descript *nd) 3354 { 3355 struct ucred *credanon; 3356 int exflags, error = 0, numsecflavor, *secflavors, i; 3357 3358 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags, 3359 &credanon, &numsecflavor, &secflavors); 3360 if (error) { 3361 error = NFSERR_PROGUNAVAIL; 3362 goto out; 3363 } 3364 if (credanon != NULL) 3365 crfree(credanon); 3366 for (i = 0; i < numsecflavor; i++) { 3367 if (secflavors[i] == AUTH_SYS) 3368 nd->nd_flag |= ND_EXAUTHSYS; 3369 else if (secflavors[i] == RPCSEC_GSS_KRB5) 3370 nd->nd_flag |= ND_EXGSS; 3371 else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3372 nd->nd_flag |= ND_EXGSSINTEGRITY; 3373 else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3374 nd->nd_flag |= ND_EXGSSPRIVACY; 3375 } 3376 3377 out: 3378 NFSEXITCODE(error); 3379 return (error); 3380 } 3381 3382 /* 3383 * Nfs server pseudo system call for the nfsd's 3384 */ 3385 /* 3386 * MPSAFE 3387 */ 3388 static int 3389 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3390 { 3391 struct file *fp; 3392 struct nfsd_addsock_args sockarg; 3393 struct nfsd_nfsd_args nfsdarg; 3394 struct nfsd_nfsd_oargs onfsdarg; 3395 struct nfsd_pnfsd_args pnfsdarg; 3396 struct vnode *vp, *nvp, *curdvp; 3397 struct pnfsdsfile *pf; 3398 struct nfsdevice *ds, *fds; 3399 cap_rights_t rights; 3400 int buflen, error, ret; 3401 char *buf, *cp, *cp2, *cp3; 3402 char fname[PNFS_FILENAME_LEN + 1]; 3403 3404 if (uap->flag & NFSSVC_NFSDADDSOCK) { 3405 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3406 if (error) 3407 goto out; 3408 /* 3409 * Since we don't know what rights might be required, 3410 * pretend that we need them all. It is better to be too 3411 * careful than too reckless. 3412 */ 3413 error = fget(td, sockarg.sock, 3414 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp); 3415 if (error != 0) 3416 goto out; 3417 if (fp->f_type != DTYPE_SOCKET) { 3418 fdrop(fp, td); 3419 error = EPERM; 3420 goto out; 3421 } 3422 error = nfsrvd_addsock(fp); 3423 fdrop(fp, td); 3424 } else if (uap->flag & NFSSVC_NFSDNFSD) { 3425 if (uap->argp == NULL) { 3426 error = EINVAL; 3427 goto out; 3428 } 3429 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) { 3430 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg)); 3431 if (error == 0) { 3432 nfsdarg.principal = onfsdarg.principal; 3433 nfsdarg.minthreads = onfsdarg.minthreads; 3434 nfsdarg.maxthreads = onfsdarg.maxthreads; 3435 nfsdarg.version = 1; 3436 nfsdarg.addr = NULL; 3437 nfsdarg.addrlen = 0; 3438 nfsdarg.dnshost = NULL; 3439 nfsdarg.dnshostlen = 0; 3440 nfsdarg.dspath = NULL; 3441 nfsdarg.dspathlen = 0; 3442 nfsdarg.mdspath = NULL; 3443 nfsdarg.mdspathlen = 0; 3444 nfsdarg.mirrorcnt = 1; 3445 } 3446 } else 3447 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg)); 3448 if (error) 3449 goto out; 3450 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 && 3451 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 && 3452 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 && 3453 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 && 3454 nfsdarg.mirrorcnt >= 1 && 3455 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS && 3456 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL && 3457 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) { 3458 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d" 3459 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen, 3460 nfsdarg.dspathlen, nfsdarg.dnshostlen, 3461 nfsdarg.mdspathlen, nfsdarg.mirrorcnt); 3462 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK); 3463 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen); 3464 if (error != 0) { 3465 free(cp, M_TEMP); 3466 goto out; 3467 } 3468 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */ 3469 nfsdarg.addr = cp; 3470 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK); 3471 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen); 3472 if (error != 0) { 3473 free(nfsdarg.addr, M_TEMP); 3474 free(cp, M_TEMP); 3475 goto out; 3476 } 3477 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */ 3478 nfsdarg.dnshost = cp; 3479 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK); 3480 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen); 3481 if (error != 0) { 3482 free(nfsdarg.addr, M_TEMP); 3483 free(nfsdarg.dnshost, M_TEMP); 3484 free(cp, M_TEMP); 3485 goto out; 3486 } 3487 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */ 3488 nfsdarg.dspath = cp; 3489 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK); 3490 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen); 3491 if (error != 0) { 3492 free(nfsdarg.addr, M_TEMP); 3493 free(nfsdarg.dnshost, M_TEMP); 3494 free(nfsdarg.dspath, M_TEMP); 3495 free(cp, M_TEMP); 3496 goto out; 3497 } 3498 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */ 3499 nfsdarg.mdspath = cp; 3500 } else { 3501 nfsdarg.addr = NULL; 3502 nfsdarg.addrlen = 0; 3503 nfsdarg.dnshost = NULL; 3504 nfsdarg.dnshostlen = 0; 3505 nfsdarg.dspath = NULL; 3506 nfsdarg.dspathlen = 0; 3507 nfsdarg.mdspath = NULL; 3508 nfsdarg.mdspathlen = 0; 3509 nfsdarg.mirrorcnt = 1; 3510 } 3511 error = nfsrvd_nfsd(td, &nfsdarg); 3512 free(nfsdarg.addr, M_TEMP); 3513 free(nfsdarg.dnshost, M_TEMP); 3514 free(nfsdarg.dspath, M_TEMP); 3515 free(nfsdarg.mdspath, M_TEMP); 3516 } else if (uap->flag & NFSSVC_PNFSDS) { 3517 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg)); 3518 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER || 3519 pnfsdarg.op == PNFSDOP_FORCEDELDS)) { 3520 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 3521 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1, 3522 NULL); 3523 if (error == 0) 3524 error = nfsrv_deldsserver(pnfsdarg.op, cp, td); 3525 free(cp, M_TEMP); 3526 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) { 3527 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 3528 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS; 3529 buf = malloc(buflen, M_TEMP, M_WAITOK); 3530 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1, 3531 NULL); 3532 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error); 3533 if (error == 0 && pnfsdarg.dspath != NULL) { 3534 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 3535 error = copyinstr(pnfsdarg.dspath, cp2, 3536 PATH_MAX + 1, NULL); 3537 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n", 3538 error); 3539 } else 3540 cp2 = NULL; 3541 if (error == 0 && pnfsdarg.curdspath != NULL) { 3542 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 3543 error = copyinstr(pnfsdarg.curdspath, cp3, 3544 PATH_MAX + 1, NULL); 3545 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n", 3546 error); 3547 } else 3548 cp3 = NULL; 3549 curdvp = NULL; 3550 fds = NULL; 3551 if (error == 0) 3552 error = nfsrv_mdscopymr(cp, cp2, cp3, buf, 3553 &buflen, fname, td, &vp, &nvp, &pf, &ds, 3554 &fds); 3555 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error); 3556 if (error == 0) { 3557 if (pf->dsf_dir >= nfsrv_dsdirsize) { 3558 printf("copymr: dsdir out of range\n"); 3559 pf->dsf_dir = 0; 3560 } 3561 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen); 3562 error = nfsrv_copymr(vp, nvp, 3563 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf, 3564 (struct pnfsdsfile *)buf, 3565 buflen / sizeof(*pf), td->td_ucred, td); 3566 vput(vp); 3567 vput(nvp); 3568 if (fds != NULL && error == 0) { 3569 curdvp = fds->nfsdev_dsdir[pf->dsf_dir]; 3570 ret = vn_lock(curdvp, LK_EXCLUSIVE); 3571 if (ret == 0) { 3572 nfsrv_dsremove(curdvp, fname, 3573 td->td_ucred, td); 3574 NFSVOPUNLOCK(curdvp, 0); 3575 } 3576 } 3577 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error); 3578 } 3579 free(cp, M_TEMP); 3580 free(cp2, M_TEMP); 3581 free(cp3, M_TEMP); 3582 free(buf, M_TEMP); 3583 } 3584 } else { 3585 error = nfssvc_srvcall(td, uap, td->td_ucred); 3586 } 3587 3588 out: 3589 NFSEXITCODE(error); 3590 return (error); 3591 } 3592 3593 static int 3594 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 3595 { 3596 struct nfsex_args export; 3597 struct file *fp = NULL; 3598 int stablefd, len; 3599 struct nfsd_clid adminrevoke; 3600 struct nfsd_dumplist dumplist; 3601 struct nfsd_dumpclients *dumpclients; 3602 struct nfsd_dumplocklist dumplocklist; 3603 struct nfsd_dumplocks *dumplocks; 3604 struct nameidata nd; 3605 vnode_t vp; 3606 int error = EINVAL, igotlock; 3607 struct proc *procp; 3608 static int suspend_nfsd = 0; 3609 3610 if (uap->flag & NFSSVC_PUBLICFH) { 3611 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 3612 sizeof (fhandle_t)); 3613 error = copyin(uap->argp, 3614 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 3615 if (!error) 3616 nfs_pubfhset = 1; 3617 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) { 3618 error = copyin(uap->argp,(caddr_t)&export, 3619 sizeof (struct nfsex_args)); 3620 if (!error) 3621 error = nfsrv_v4rootexport(&export, cred, p); 3622 } else if (uap->flag & NFSSVC_NOPUBLICFH) { 3623 nfs_pubfhset = 0; 3624 error = 0; 3625 } else if (uap->flag & NFSSVC_STABLERESTART) { 3626 error = copyin(uap->argp, (caddr_t)&stablefd, 3627 sizeof (int)); 3628 if (!error) 3629 error = fp_getfvp(p, stablefd, &fp, &vp); 3630 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 3631 error = EBADF; 3632 if (!error && newnfs_numnfsd != 0) 3633 error = EPERM; 3634 if (!error) { 3635 nfsrv_stablefirst.nsf_fp = fp; 3636 nfsrv_setupstable(p); 3637 } 3638 } else if (uap->flag & NFSSVC_ADMINREVOKE) { 3639 error = copyin(uap->argp, (caddr_t)&adminrevoke, 3640 sizeof (struct nfsd_clid)); 3641 if (!error) 3642 error = nfsrv_adminrevoke(&adminrevoke, p); 3643 } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 3644 error = copyin(uap->argp, (caddr_t)&dumplist, 3645 sizeof (struct nfsd_dumplist)); 3646 if (!error && (dumplist.ndl_size < 1 || 3647 dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 3648 error = EPERM; 3649 if (!error) { 3650 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 3651 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 3652 nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 3653 error = copyout(dumpclients, 3654 CAST_USER_ADDR_T(dumplist.ndl_list), len); 3655 free(dumpclients, M_TEMP); 3656 } 3657 } else if (uap->flag & NFSSVC_DUMPLOCKS) { 3658 error = copyin(uap->argp, (caddr_t)&dumplocklist, 3659 sizeof (struct nfsd_dumplocklist)); 3660 if (!error && (dumplocklist.ndllck_size < 1 || 3661 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 3662 error = EPERM; 3663 if (!error) 3664 error = nfsrv_lookupfilename(&nd, 3665 dumplocklist.ndllck_fname, p); 3666 if (!error) { 3667 len = sizeof (struct nfsd_dumplocks) * 3668 dumplocklist.ndllck_size; 3669 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 3670 nfsrv_dumplocks(nd.ni_vp, dumplocks, 3671 dumplocklist.ndllck_size, p); 3672 vput(nd.ni_vp); 3673 error = copyout(dumplocks, 3674 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len); 3675 free(dumplocks, M_TEMP); 3676 } 3677 } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 3678 procp = p->td_proc; 3679 PROC_LOCK(procp); 3680 nfsd_master_pid = procp->p_pid; 3681 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 3682 nfsd_master_start = procp->p_stats->p_start; 3683 nfsd_master_proc = procp; 3684 PROC_UNLOCK(procp); 3685 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) { 3686 NFSLOCKV4ROOTMUTEX(); 3687 if (suspend_nfsd == 0) { 3688 /* Lock out all nfsd threads */ 3689 do { 3690 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1, 3691 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 3692 } while (igotlock == 0 && suspend_nfsd == 0); 3693 suspend_nfsd = 1; 3694 } 3695 NFSUNLOCKV4ROOTMUTEX(); 3696 error = 0; 3697 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) { 3698 NFSLOCKV4ROOTMUTEX(); 3699 if (suspend_nfsd != 0) { 3700 nfsv4_unlock(&nfsd_suspend_lock, 0); 3701 suspend_nfsd = 0; 3702 } 3703 NFSUNLOCKV4ROOTMUTEX(); 3704 error = 0; 3705 } 3706 3707 NFSEXITCODE(error); 3708 return (error); 3709 } 3710 3711 /* 3712 * Check exports. 3713 * Returns 0 if ok, 1 otherwise. 3714 */ 3715 int 3716 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 3717 { 3718 int i; 3719 3720 /* 3721 * This seems odd, but allow the case where the security flavor 3722 * list is empty. This happens when NFSv4 is traversing non-exported 3723 * file systems. Exported file systems should always have a non-empty 3724 * security flavor list. 3725 */ 3726 if (exp->nes_numsecflavor == 0) 3727 return (0); 3728 3729 for (i = 0; i < exp->nes_numsecflavor; i++) { 3730 /* 3731 * The tests for privacy and integrity must be first, 3732 * since ND_GSS is set for everything but AUTH_SYS. 3733 */ 3734 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 3735 (nd->nd_flag & ND_GSSPRIVACY)) 3736 return (0); 3737 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 3738 (nd->nd_flag & ND_GSSINTEGRITY)) 3739 return (0); 3740 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 3741 (nd->nd_flag & ND_GSS)) 3742 return (0); 3743 if (exp->nes_secflavors[i] == AUTH_SYS && 3744 (nd->nd_flag & ND_GSS) == 0) 3745 return (0); 3746 } 3747 return (1); 3748 } 3749 3750 /* 3751 * Calculate a hash value for the fid in a file handle. 3752 */ 3753 uint32_t 3754 nfsrv_hashfh(fhandle_t *fhp) 3755 { 3756 uint32_t hashval; 3757 3758 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 3759 return (hashval); 3760 } 3761 3762 /* 3763 * Calculate a hash value for the sessionid. 3764 */ 3765 uint32_t 3766 nfsrv_hashsessionid(uint8_t *sessionid) 3767 { 3768 uint32_t hashval; 3769 3770 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0); 3771 return (hashval); 3772 } 3773 3774 /* 3775 * Signal the userland master nfsd to backup the stable restart file. 3776 */ 3777 void 3778 nfsrv_backupstable(void) 3779 { 3780 struct proc *procp; 3781 3782 if (nfsd_master_proc != NULL) { 3783 procp = pfind(nfsd_master_pid); 3784 /* Try to make sure it is the correct process. */ 3785 if (procp == nfsd_master_proc && 3786 procp->p_stats->p_start.tv_sec == 3787 nfsd_master_start.tv_sec && 3788 procp->p_stats->p_start.tv_usec == 3789 nfsd_master_start.tv_usec && 3790 strcmp(procp->p_comm, nfsd_master_comm) == 0) 3791 kern_psignal(procp, SIGUSR2); 3792 else 3793 nfsd_master_proc = NULL; 3794 3795 if (procp != NULL) 3796 PROC_UNLOCK(procp); 3797 } 3798 } 3799 3800 /* 3801 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror. 3802 * The arguments are in a structure, so that they can be passed through 3803 * taskqueue for a kernel process to execute this function. 3804 */ 3805 struct nfsrvdscreate { 3806 int done; 3807 int inprog; 3808 struct task tsk; 3809 struct ucred *tcred; 3810 struct vnode *dvp; 3811 NFSPROC_T *p; 3812 struct pnfsdsfile *pf; 3813 int err; 3814 fhandle_t fh; 3815 struct vattr va; 3816 struct vattr createva; 3817 }; 3818 3819 int 3820 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap, 3821 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa, 3822 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp) 3823 { 3824 struct vnode *nvp; 3825 struct nameidata named; 3826 struct vattr va; 3827 char *bufp; 3828 u_long *hashp; 3829 struct nfsnode *np; 3830 struct nfsmount *nmp; 3831 int error; 3832 3833 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE, 3834 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE); 3835 nfsvno_setpathbuf(&named, &bufp, &hashp); 3836 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE; 3837 named.ni_cnd.cn_thread = p; 3838 named.ni_cnd.cn_nameptr = bufp; 3839 if (fnamep != NULL) { 3840 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1); 3841 named.ni_cnd.cn_namelen = strlen(bufp); 3842 } else 3843 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp); 3844 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp); 3845 3846 /* Create the date file in the DS mount. */ 3847 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE); 3848 if (error == 0) { 3849 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap); 3850 NFSVOPUNLOCK(dvp, 0); 3851 if (error == 0) { 3852 /* Set the ownership of the file. */ 3853 error = VOP_SETATTR(nvp, nvap, tcred); 3854 NFSD_DEBUG(4, "nfsrv_dscreate:" 3855 " setattr-uid=%d\n", error); 3856 if (error != 0) 3857 vput(nvp); 3858 } 3859 if (error != 0) 3860 printf("pNFS: pnfscreate failed=%d\n", error); 3861 } else 3862 printf("pNFS: pnfscreate vnlock=%d\n", error); 3863 if (error == 0) { 3864 np = VTONFS(nvp); 3865 nmp = VFSTONFS(nvp->v_mount); 3866 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs") 3867 != 0 || nmp->nm_nam->sa_len > sizeof( 3868 struct sockaddr_in6) || 3869 np->n_fhp->nfh_len != NFSX_MYFH) { 3870 printf("Bad DS file: fstype=%s salen=%d" 3871 " fhlen=%d\n", 3872 nvp->v_mount->mnt_vfc->vfc_name, 3873 nmp->nm_nam->sa_len, np->n_fhp->nfh_len); 3874 error = ENOENT; 3875 } 3876 3877 /* Set extattrs for the DS on the MDS file. */ 3878 if (error == 0) { 3879 if (dsa != NULL) { 3880 error = VOP_GETATTR(nvp, &va, tcred); 3881 if (error == 0) { 3882 dsa->dsa_filerev = va.va_filerev; 3883 dsa->dsa_size = va.va_size; 3884 dsa->dsa_atime = va.va_atime; 3885 dsa->dsa_mtime = va.va_mtime; 3886 } 3887 } 3888 if (error == 0) { 3889 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, 3890 NFSX_MYFH); 3891 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin, 3892 nmp->nm_nam->sa_len); 3893 NFSBCOPY(named.ni_cnd.cn_nameptr, 3894 pf->dsf_filename, 3895 sizeof(pf->dsf_filename)); 3896 } 3897 } else 3898 printf("pNFS: pnfscreate can't get DS" 3899 " attr=%d\n", error); 3900 if (nvpp != NULL && error == 0) 3901 *nvpp = nvp; 3902 else 3903 vput(nvp); 3904 } 3905 nfsvno_relpathbuf(&named); 3906 return (error); 3907 } 3908 3909 /* 3910 * Start up the thread that will execute nfsrv_dscreate(). 3911 */ 3912 static void 3913 start_dscreate(void *arg, int pending) 3914 { 3915 struct nfsrvdscreate *dsc; 3916 3917 dsc = (struct nfsrvdscreate *)arg; 3918 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh, 3919 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL); 3920 dsc->done = 1; 3921 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err); 3922 } 3923 3924 /* 3925 * Create a pNFS data file on the Data Server(s). 3926 */ 3927 static void 3928 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred, 3929 NFSPROC_T *p) 3930 { 3931 struct nfsrvdscreate *dsc, *tdsc; 3932 struct nfsdevice *ds, *tds, *fds; 3933 struct mount *mp; 3934 struct pnfsdsfile *pf, *tpf; 3935 struct pnfsdsattr dsattr; 3936 struct vattr va; 3937 struct vnode *dvp[NFSDEV_MAXMIRRORS]; 3938 struct nfsmount *nmp; 3939 fhandle_t fh; 3940 uid_t vauid; 3941 gid_t vagid; 3942 u_short vamode; 3943 struct ucred *tcred; 3944 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret; 3945 int failpos, timo; 3946 3947 /* Get a DS server directory in a round-robin order. */ 3948 mirrorcnt = 1; 3949 mp = vp->v_mount; 3950 ds = fds = NULL; 3951 NFSDDSLOCK(); 3952 /* 3953 * Search for the first entry that handles this MDS fs, but use the 3954 * first entry for all MDS fs's otherwise. 3955 */ 3956 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) { 3957 if (tds->nfsdev_nmp != NULL) { 3958 if (tds->nfsdev_mdsisset == 0 && ds == NULL) 3959 ds = tds; 3960 else if (tds->nfsdev_mdsisset != 0 && 3961 mp->mnt_stat.f_fsid.val[0] == 3962 tds->nfsdev_mdsfsid.val[0] && 3963 mp->mnt_stat.f_fsid.val[1] == 3964 tds->nfsdev_mdsfsid.val[1]) { 3965 ds = fds = tds; 3966 break; 3967 } 3968 } 3969 } 3970 if (ds == NULL) { 3971 NFSDDSUNLOCK(); 3972 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n"); 3973 return; 3974 } 3975 i = dsdir[0] = ds->nfsdev_nextdir; 3976 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize; 3977 dvp[0] = ds->nfsdev_dsdir[i]; 3978 tds = TAILQ_NEXT(ds, nfsdev_list); 3979 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) { 3980 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) { 3981 if (tds->nfsdev_nmp != NULL && 3982 ((tds->nfsdev_mdsisset == 0 && fds == NULL) || 3983 (tds->nfsdev_mdsisset != 0 && fds != NULL && 3984 mp->mnt_stat.f_fsid.val[0] == 3985 tds->nfsdev_mdsfsid.val[0] && 3986 mp->mnt_stat.f_fsid.val[1] == 3987 tds->nfsdev_mdsfsid.val[1]))) { 3988 dsdir[mirrorcnt] = i; 3989 dvp[mirrorcnt] = tds->nfsdev_dsdir[i]; 3990 mirrorcnt++; 3991 if (mirrorcnt >= nfsrv_maxpnfsmirror) 3992 break; 3993 } 3994 } 3995 } 3996 /* Put at end of list to implement round-robin usage. */ 3997 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list); 3998 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list); 3999 NFSDDSUNLOCK(); 4000 dsc = NULL; 4001 if (mirrorcnt > 1) 4002 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP, 4003 M_WAITOK | M_ZERO); 4004 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK | 4005 M_ZERO); 4006 4007 error = nfsvno_getfh(vp, &fh, p); 4008 if (error == 0) 4009 error = VOP_GETATTR(vp, &va, cred); 4010 if (error == 0) { 4011 /* Set the attributes for "vp" to Setattr the DS vp. */ 4012 vauid = va.va_uid; 4013 vagid = va.va_gid; 4014 vamode = va.va_mode; 4015 VATTR_NULL(&va); 4016 va.va_uid = vauid; 4017 va.va_gid = vagid; 4018 va.va_mode = vamode; 4019 va.va_size = 0; 4020 } else 4021 printf("pNFS: pnfscreate getfh+attr=%d\n", error); 4022 4023 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid, 4024 cred->cr_gid); 4025 /* Make data file name based on FH. */ 4026 tcred = newnfs_getcred(); 4027 4028 /* 4029 * Create the file on each DS mirror, using kernel process(es) for the 4030 * additional mirrors. 4031 */ 4032 failpos = -1; 4033 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) { 4034 tpf->dsf_dir = dsdir[i]; 4035 tdsc->tcred = tcred; 4036 tdsc->p = p; 4037 tdsc->pf = tpf; 4038 tdsc->createva = *vap; 4039 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh)); 4040 tdsc->va = va; 4041 tdsc->dvp = dvp[i]; 4042 tdsc->done = 0; 4043 tdsc->inprog = 0; 4044 tdsc->err = 0; 4045 ret = EIO; 4046 if (nfs_pnfsiothreads != 0) { 4047 ret = nfs_pnfsio(start_dscreate, tdsc); 4048 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret); 4049 } 4050 if (ret != 0) { 4051 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL, 4052 NULL, tcred, p, NULL); 4053 if (ret != 0) { 4054 KASSERT(error == 0, ("nfsrv_dscreate err=%d", 4055 error)); 4056 if (failpos == -1 && nfsds_failerr(ret)) 4057 failpos = i; 4058 else 4059 error = ret; 4060 } 4061 } 4062 } 4063 if (error == 0) { 4064 tpf->dsf_dir = dsdir[mirrorcnt - 1]; 4065 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf, 4066 &dsattr, NULL, tcred, p, NULL); 4067 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) { 4068 failpos = mirrorcnt - 1; 4069 error = 0; 4070 } 4071 } 4072 timo = hz / 50; /* Wait for 20msec. */ 4073 if (timo < 1) 4074 timo = 1; 4075 /* Wait for kernel task(s) to complete. */ 4076 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) { 4077 while (tdsc->inprog != 0 && tdsc->done == 0) 4078 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo); 4079 if (tdsc->err != 0) { 4080 if (failpos == -1 && nfsds_failerr(tdsc->err)) 4081 failpos = i; 4082 else if (error == 0) 4083 error = tdsc->err; 4084 } 4085 } 4086 4087 /* 4088 * If failpos has been set, that mirror has failed, so it needs 4089 * to be disabled. 4090 */ 4091 if (failpos >= 0) { 4092 nmp = VFSTONFS(dvp[failpos]->v_mount); 4093 NFSLOCKMNT(nmp); 4094 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 4095 NFSMNTP_CANCELRPCS)) == 0) { 4096 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4097 NFSUNLOCKMNT(nmp); 4098 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p); 4099 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos, 4100 ds); 4101 if (ds != NULL) 4102 nfsrv_killrpcs(nmp); 4103 NFSLOCKMNT(nmp); 4104 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4105 wakeup(nmp); 4106 } 4107 NFSUNLOCKMNT(nmp); 4108 } 4109 4110 NFSFREECRED(tcred); 4111 if (error == 0) { 4112 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp"); 4113 4114 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n", 4115 mirrorcnt, nfsrv_maxpnfsmirror); 4116 /* 4117 * For all mirrors that couldn't be created, fill in the 4118 * *pf structure, but with an IP address == 0.0.0.0. 4119 */ 4120 tpf = pf + mirrorcnt; 4121 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) { 4122 *tpf = *pf; 4123 tpf->dsf_sin.sin_family = AF_INET; 4124 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in); 4125 tpf->dsf_sin.sin_addr.s_addr = 0; 4126 tpf->dsf_sin.sin_port = 0; 4127 } 4128 4129 error = vn_extattr_set(vp, IO_NODELOCKED, 4130 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", 4131 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p); 4132 if (error == 0) 4133 error = vn_extattr_set(vp, IO_NODELOCKED, 4134 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", 4135 sizeof(dsattr), (char *)&dsattr, p); 4136 if (error != 0) 4137 printf("pNFS: pnfscreate setextattr=%d\n", 4138 error); 4139 } else 4140 printf("pNFS: pnfscreate=%d\n", error); 4141 free(pf, M_TEMP); 4142 free(dsc, M_TEMP); 4143 } 4144 4145 /* 4146 * Get the information needed to remove the pNFS Data Server file from the 4147 * Metadata file. Upon success, ddvp is set non-NULL to the locked 4148 * DS directory vnode. The caller must unlock *ddvp when done with it. 4149 */ 4150 static void 4151 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp, 4152 int *mirrorcntp, char *fname, fhandle_t *fhp) 4153 { 4154 struct vattr va; 4155 struct ucred *tcred; 4156 char *buf; 4157 int buflen, error; 4158 4159 dvpp[0] = NULL; 4160 /* If not an exported regular file or not a pNFS server, just return. */ 4161 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 || 4162 nfsrv_devidcnt == 0) 4163 return; 4164 4165 /* Check to see if this is the last hard link. */ 4166 tcred = newnfs_getcred(); 4167 error = VOP_GETATTR(vp, &va, tcred); 4168 NFSFREECRED(tcred); 4169 if (error != 0) { 4170 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error); 4171 return; 4172 } 4173 if (va.va_nlink > 1) 4174 return; 4175 4176 error = nfsvno_getfh(vp, fhp, p); 4177 if (error != 0) { 4178 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error); 4179 return; 4180 } 4181 4182 buflen = 1024; 4183 buf = malloc(buflen, M_TEMP, M_WAITOK); 4184 /* Get the directory vnode for the DS mount and the file handle. */ 4185 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp, 4186 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL); 4187 free(buf, M_TEMP); 4188 if (error != 0) 4189 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error); 4190 } 4191 4192 /* 4193 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror. 4194 * The arguments are in a structure, so that they can be passed through 4195 * taskqueue for a kernel process to execute this function. 4196 */ 4197 struct nfsrvdsremove { 4198 int done; 4199 int inprog; 4200 struct task tsk; 4201 struct ucred *tcred; 4202 struct vnode *dvp; 4203 NFSPROC_T *p; 4204 int err; 4205 char fname[PNFS_FILENAME_LEN + 1]; 4206 }; 4207 4208 static int 4209 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred, 4210 NFSPROC_T *p) 4211 { 4212 struct nameidata named; 4213 struct vnode *nvp; 4214 char *bufp; 4215 u_long *hashp; 4216 int error; 4217 4218 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE); 4219 if (error != 0) 4220 return (error); 4221 named.ni_cnd.cn_nameiop = DELETE; 4222 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY; 4223 named.ni_cnd.cn_cred = tcred; 4224 named.ni_cnd.cn_thread = p; 4225 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME; 4226 nfsvno_setpathbuf(&named, &bufp, &hashp); 4227 named.ni_cnd.cn_nameptr = bufp; 4228 named.ni_cnd.cn_namelen = strlen(fname); 4229 strlcpy(bufp, fname, NAME_MAX); 4230 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp); 4231 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd); 4232 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error); 4233 if (error == 0) { 4234 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd); 4235 vput(nvp); 4236 } 4237 NFSVOPUNLOCK(dvp, 0); 4238 nfsvno_relpathbuf(&named); 4239 if (error != 0) 4240 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error); 4241 return (error); 4242 } 4243 4244 /* 4245 * Start up the thread that will execute nfsrv_dsremove(). 4246 */ 4247 static void 4248 start_dsremove(void *arg, int pending) 4249 { 4250 struct nfsrvdsremove *dsrm; 4251 4252 dsrm = (struct nfsrvdsremove *)arg; 4253 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred, 4254 dsrm->p); 4255 dsrm->done = 1; 4256 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err); 4257 } 4258 4259 /* 4260 * Remove a pNFS data file from a Data Server. 4261 * nfsrv_pnfsremovesetup() must have been called before the MDS file was 4262 * removed to set up the dvp and fill in the FH. 4263 */ 4264 static void 4265 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp, 4266 NFSPROC_T *p) 4267 { 4268 struct ucred *tcred; 4269 struct nfsrvdsremove *dsrm, *tdsrm; 4270 struct nfsdevice *ds; 4271 struct nfsmount *nmp; 4272 int failpos, i, ret, timo; 4273 4274 tcred = newnfs_getcred(); 4275 dsrm = NULL; 4276 if (mirrorcnt > 1) 4277 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK); 4278 /* 4279 * Remove the file on each DS mirror, using kernel process(es) for the 4280 * additional mirrors. 4281 */ 4282 failpos = -1; 4283 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) { 4284 tdsrm->tcred = tcred; 4285 tdsrm->p = p; 4286 tdsrm->dvp = dvp[i]; 4287 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1); 4288 tdsrm->inprog = 0; 4289 tdsrm->done = 0; 4290 tdsrm->err = 0; 4291 ret = EIO; 4292 if (nfs_pnfsiothreads != 0) { 4293 ret = nfs_pnfsio(start_dsremove, tdsrm); 4294 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret); 4295 } 4296 if (ret != 0) { 4297 ret = nfsrv_dsremove(dvp[i], fname, tcred, p); 4298 if (failpos == -1 && nfsds_failerr(ret)) 4299 failpos = i; 4300 } 4301 } 4302 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p); 4303 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret)) 4304 failpos = mirrorcnt - 1; 4305 timo = hz / 50; /* Wait for 20msec. */ 4306 if (timo < 1) 4307 timo = 1; 4308 /* Wait for kernel task(s) to complete. */ 4309 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) { 4310 while (tdsrm->inprog != 0 && tdsrm->done == 0) 4311 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo); 4312 if (failpos == -1 && nfsds_failerr(tdsrm->err)) 4313 failpos = i; 4314 } 4315 4316 /* 4317 * If failpos has been set, that mirror has failed, so it needs 4318 * to be disabled. 4319 */ 4320 if (failpos >= 0) { 4321 nmp = VFSTONFS(dvp[failpos]->v_mount); 4322 NFSLOCKMNT(nmp); 4323 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 4324 NFSMNTP_CANCELRPCS)) == 0) { 4325 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4326 NFSUNLOCKMNT(nmp); 4327 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p); 4328 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos, 4329 ds); 4330 if (ds != NULL) 4331 nfsrv_killrpcs(nmp); 4332 NFSLOCKMNT(nmp); 4333 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4334 wakeup(nmp); 4335 } 4336 NFSUNLOCKMNT(nmp); 4337 } 4338 4339 /* Get rid all layouts for the file. */ 4340 nfsrv_freefilelayouts(fhp); 4341 4342 NFSFREECRED(tcred); 4343 free(dsrm, M_TEMP); 4344 } 4345 4346 /* 4347 * Generate a file name based on the file handle and put it in *bufp. 4348 * Return the number of bytes generated. 4349 */ 4350 static int 4351 nfsrv_putfhname(fhandle_t *fhp, char *bufp) 4352 { 4353 int i; 4354 uint8_t *cp; 4355 const uint8_t *hexdigits = "0123456789abcdef"; 4356 4357 cp = (uint8_t *)fhp; 4358 for (i = 0; i < sizeof(*fhp); i++) { 4359 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf]; 4360 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf]; 4361 } 4362 bufp[2 * i] = '\0'; 4363 return (2 * i); 4364 } 4365 4366 /* 4367 * Update the Metadata file's attributes from the DS file when a Read/Write 4368 * layout is returned. 4369 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN 4370 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file. 4371 */ 4372 int 4373 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p) 4374 { 4375 struct ucred *tcred; 4376 int error; 4377 4378 /* Do this as root so that it won't fail with EACCES. */ 4379 tcred = newnfs_getcred(); 4380 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN, 4381 NULL, NULL, NULL, nap, NULL); 4382 NFSFREECRED(tcred); 4383 return (error); 4384 } 4385 4386 /* 4387 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file. 4388 */ 4389 static int 4390 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred, 4391 NFSPROC_T *p) 4392 { 4393 int error; 4394 4395 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL, 4396 NULL, NULL, NULL, NULL, aclp); 4397 return (error); 4398 } 4399 4400 static int 4401 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 4402 struct thread *p, int ioproc, struct mbuf **mpp, char *cp, 4403 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp) 4404 { 4405 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp; 4406 fhandle_t fh[NFSDEV_MAXMIRRORS]; 4407 struct vnode *dvp[NFSDEV_MAXMIRRORS]; 4408 struct nfsdevice *ds; 4409 struct pnfsdsattr dsattr; 4410 char *buf; 4411 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt; 4412 4413 NFSD_DEBUG(4, "in nfsrv_proxyds\n"); 4414 /* 4415 * If not a regular file, not exported or not a pNFS server, 4416 * just return ENOENT. 4417 */ 4418 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 || 4419 nfsrv_devidcnt == 0) 4420 return (ENOENT); 4421 4422 buflen = 1024; 4423 buf = malloc(buflen, M_TEMP, M_WAITOK); 4424 error = 0; 4425 4426 /* 4427 * For Getattr, get the Change attribute (va_filerev) and size (va_size) 4428 * from the MetaData file's extended attribute. 4429 */ 4430 if (ioproc == NFSPROC_GETATTR) { 4431 error = vn_extattr_get(vp, IO_NODELOCKED, 4432 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf, 4433 p); 4434 if (error == 0 && buflen != sizeof(dsattr)) 4435 error = ENXIO; 4436 if (error == 0) { 4437 NFSBCOPY(buf, &dsattr, buflen); 4438 nap->na_filerev = dsattr.dsa_filerev; 4439 nap->na_size = dsattr.dsa_size; 4440 nap->na_atime = dsattr.dsa_atime; 4441 nap->na_mtime = dsattr.dsa_mtime; 4442 4443 /* 4444 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr() 4445 * returns 0, just return now. nfsrv_checkdsattr() 4446 * returns 0 if there is no Read/Write layout 4447 * plus either an Open/Write_access or Write 4448 * delegation issued to a client for the file. 4449 */ 4450 if (nfsrv_pnfsgetdsattr == 0 || 4451 nfsrv_checkdsattr(vp, p) == 0) { 4452 free(buf, M_TEMP); 4453 return (error); 4454 } 4455 } 4456 4457 /* 4458 * Clear ENOATTR so the code below will attempt to do a 4459 * nfsrv_getattrdsrpc() to get the attributes and (re)create 4460 * the extended attribute. 4461 */ 4462 if (error == ENOATTR) 4463 error = 0; 4464 } 4465 4466 origmircnt = -1; 4467 trycnt = 0; 4468 tryagain: 4469 if (error == 0) { 4470 buflen = 1024; 4471 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) == 4472 LK_EXCLUSIVE) 4473 printf("nfsrv_proxyds: Readds vp exclusively locked\n"); 4474 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen, 4475 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL, 4476 NULL, NULL); 4477 if (error == 0) { 4478 for (i = 0; i < mirrorcnt; i++) 4479 nmp[i] = VFSTONFS(dvp[i]->v_mount); 4480 } else 4481 printf("pNFS: proxy getextattr sockaddr=%d\n", error); 4482 } else 4483 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error); 4484 if (error == 0) { 4485 failpos = -1; 4486 if (origmircnt == -1) 4487 origmircnt = mirrorcnt; 4488 /* 4489 * If failpos is set to a mirror#, then that mirror has 4490 * failed and will be disabled. For Read and Getattr, the 4491 * function only tries one mirror, so if that mirror has 4492 * failed, it will need to be retried. As such, increment 4493 * tryitagain for these cases. 4494 * For Write, Setattr and Setacl, the function tries all 4495 * mirrors and will not return an error for the case where 4496 * one mirror has failed. For these cases, the functioning 4497 * mirror(s) will have been modified, so a retry isn't 4498 * necessary. These functions will set failpos for the 4499 * failed mirror#. 4500 */ 4501 if (ioproc == NFSPROC_READDS) { 4502 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0], 4503 mpp, mpp2); 4504 if (nfsds_failerr(error) && mirrorcnt > 1) { 4505 /* 4506 * Setting failpos will cause the mirror 4507 * to be disabled and then a retry of this 4508 * read is required. 4509 */ 4510 failpos = 0; 4511 error = 0; 4512 trycnt++; 4513 } 4514 } else if (ioproc == NFSPROC_WRITEDS) 4515 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp, 4516 &nmp[0], mirrorcnt, mpp, cp, &failpos); 4517 else if (ioproc == NFSPROC_SETATTR) 4518 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0], 4519 mirrorcnt, nap, &failpos); 4520 else if (ioproc == NFSPROC_SETACL) 4521 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0], 4522 mirrorcnt, aclp, &failpos); 4523 else { 4524 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p, 4525 vp, nmp[mirrorcnt - 1], nap); 4526 if (nfsds_failerr(error) && mirrorcnt > 1) { 4527 /* 4528 * Setting failpos will cause the mirror 4529 * to be disabled and then a retry of this 4530 * getattr is required. 4531 */ 4532 failpos = mirrorcnt - 1; 4533 error = 0; 4534 trycnt++; 4535 } 4536 } 4537 ds = NULL; 4538 if (failpos >= 0) { 4539 failnmp = nmp[failpos]; 4540 NFSLOCKMNT(failnmp); 4541 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM | 4542 NFSMNTP_CANCELRPCS)) == 0) { 4543 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4544 NFSUNLOCKMNT(failnmp); 4545 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, 4546 failnmp, p); 4547 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n", 4548 failpos, ds); 4549 if (ds != NULL) 4550 nfsrv_killrpcs(failnmp); 4551 NFSLOCKMNT(failnmp); 4552 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4553 wakeup(failnmp); 4554 } 4555 NFSUNLOCKMNT(failnmp); 4556 } 4557 for (i = 0; i < mirrorcnt; i++) 4558 NFSVOPUNLOCK(dvp[i], 0); 4559 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error, 4560 trycnt); 4561 /* Try the Read/Getattr again if a mirror was deleted. */ 4562 if (ds != NULL && trycnt > 0 && trycnt < origmircnt) 4563 goto tryagain; 4564 } else { 4565 /* Return ENOENT for any Extended Attribute error. */ 4566 error = ENOENT; 4567 } 4568 free(buf, M_TEMP); 4569 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error); 4570 return (error); 4571 } 4572 4573 /* 4574 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended 4575 * attribute. 4576 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs 4577 * to be checked. If it points to a NULL nmp, then it returns 4578 * a suitable destination. 4579 * curnmp - If non-NULL, it is the source mount for the copy. 4580 */ 4581 int 4582 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp, 4583 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp, 4584 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp, 4585 struct nfsmount *curnmp, int *ippos, int *dsdirp) 4586 { 4587 struct vnode *dvp, *nvp, **tdvpp; 4588 struct mount *mp; 4589 struct nfsmount *nmp, *newnmp; 4590 struct sockaddr *sad; 4591 struct sockaddr_in *sin; 4592 struct nfsdevice *ds, *tds, *fndds; 4593 struct pnfsdsfile *pf; 4594 uint32_t dsdir; 4595 int error, fhiszero, fnd, gotone, i, mirrorcnt; 4596 4597 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp"); 4598 *mirrorcntp = 1; 4599 tdvpp = dvpp; 4600 if (nvpp != NULL) 4601 *nvpp = NULL; 4602 if (dvpp != NULL) 4603 *dvpp = NULL; 4604 if (ippos != NULL) 4605 *ippos = -1; 4606 if (newnmpp != NULL) 4607 newnmp = *newnmpp; 4608 else 4609 newnmp = NULL; 4610 mp = vp->v_mount; 4611 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM, 4612 "pnfsd.dsfile", buflenp, buf, p); 4613 mirrorcnt = *buflenp / sizeof(*pf); 4614 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS || 4615 *buflenp != sizeof(*pf) * mirrorcnt)) 4616 error = ENOATTR; 4617 4618 pf = (struct pnfsdsfile *)buf; 4619 /* If curnmp != NULL, check for a match in the mirror list. */ 4620 if (curnmp != NULL && error == 0) { 4621 fnd = 0; 4622 for (i = 0; i < mirrorcnt; i++, pf++) { 4623 sad = (struct sockaddr *)&pf->dsf_sin; 4624 if (nfsaddr2_match(sad, curnmp->nm_nam)) { 4625 if (ippos != NULL) 4626 *ippos = i; 4627 fnd = 1; 4628 break; 4629 } 4630 } 4631 if (fnd == 0) 4632 error = ENXIO; 4633 } 4634 4635 gotone = 0; 4636 pf = (struct pnfsdsfile *)buf; 4637 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt, 4638 error); 4639 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) { 4640 fhiszero = 0; 4641 sad = (struct sockaddr *)&pf->dsf_sin; 4642 sin = &pf->dsf_sin; 4643 dsdir = pf->dsf_dir; 4644 if (dsdir >= nfsrv_dsdirsize) { 4645 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir); 4646 error = ENOATTR; 4647 } else if (nvpp != NULL && newnmp != NULL && 4648 nfsaddr2_match(sad, newnmp->nm_nam)) 4649 error = EEXIST; 4650 if (error == 0) { 4651 if (ippos != NULL && curnmp == NULL && 4652 sad->sa_family == AF_INET && 4653 sin->sin_addr.s_addr == 0) 4654 *ippos = i; 4655 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0) 4656 fhiszero = 1; 4657 /* Use the socket address to find the mount point. */ 4658 fndds = NULL; 4659 NFSDDSLOCK(); 4660 /* Find a match for the IP address. */ 4661 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 4662 if (ds->nfsdev_nmp != NULL) { 4663 dvp = ds->nfsdev_dvp; 4664 nmp = VFSTONFS(dvp->v_mount); 4665 if (nmp != ds->nfsdev_nmp) 4666 printf("different2 nmp %p %p\n", 4667 nmp, ds->nfsdev_nmp); 4668 if (nfsaddr2_match(sad, nmp->nm_nam)) { 4669 fndds = ds; 4670 break; 4671 } 4672 } 4673 } 4674 if (fndds != NULL && newnmpp != NULL && 4675 newnmp == NULL) { 4676 /* Search for a place to make a mirror copy. */ 4677 TAILQ_FOREACH(tds, &nfsrv_devidhead, 4678 nfsdev_list) { 4679 if (tds->nfsdev_nmp != NULL && 4680 fndds != tds && 4681 ((tds->nfsdev_mdsisset == 0 && 4682 fndds->nfsdev_mdsisset == 0) || 4683 (tds->nfsdev_mdsisset != 0 && 4684 fndds->nfsdev_mdsisset != 0 && 4685 tds->nfsdev_mdsfsid.val[0] == 4686 mp->mnt_stat.f_fsid.val[0] && 4687 tds->nfsdev_mdsfsid.val[1] == 4688 mp->mnt_stat.f_fsid.val[1]))) { 4689 *newnmpp = tds->nfsdev_nmp; 4690 break; 4691 } 4692 } 4693 if (tds != NULL) { 4694 /* 4695 * Move this entry to the end of the 4696 * list, so it won't be selected as 4697 * easily the next time. 4698 */ 4699 TAILQ_REMOVE(&nfsrv_devidhead, tds, 4700 nfsdev_list); 4701 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds, 4702 nfsdev_list); 4703 } 4704 } 4705 NFSDDSUNLOCK(); 4706 if (fndds != NULL) { 4707 dvp = fndds->nfsdev_dsdir[dsdir]; 4708 if (lktype != 0 || fhiszero != 0 || 4709 (nvpp != NULL && *nvpp == NULL)) { 4710 if (fhiszero != 0) 4711 error = vn_lock(dvp, 4712 LK_EXCLUSIVE); 4713 else if (lktype != 0) 4714 error = vn_lock(dvp, lktype); 4715 else 4716 error = vn_lock(dvp, LK_SHARED); 4717 /* 4718 * If the file handle is all 0's, try to 4719 * do a Lookup against the DS to acquire 4720 * it. 4721 * If dvpp == NULL or the Lookup fails, 4722 * unlock dvp after the call. 4723 */ 4724 if (error == 0 && (fhiszero != 0 || 4725 (nvpp != NULL && *nvpp == NULL))) { 4726 error = nfsrv_pnfslookupds(vp, 4727 dvp, pf, &nvp, p); 4728 if (error == 0) { 4729 if (fhiszero != 0) 4730 nfsrv_pnfssetfh( 4731 vp, pf, 4732 devid, 4733 fnamep, 4734 nvp, p); 4735 if (nvpp != NULL && 4736 *nvpp == NULL) { 4737 *nvpp = nvp; 4738 *dsdirp = dsdir; 4739 } else 4740 vput(nvp); 4741 } 4742 if (error != 0 || lktype == 0) 4743 NFSVOPUNLOCK(dvp, 0); 4744 } 4745 } 4746 if (error == 0) { 4747 gotone++; 4748 NFSD_DEBUG(4, "gotone=%d\n", gotone); 4749 if (devid != NULL) { 4750 NFSBCOPY(fndds->nfsdev_deviceid, 4751 devid, NFSX_V4DEVICEID); 4752 devid += NFSX_V4DEVICEID; 4753 } 4754 if (dvpp != NULL) 4755 *tdvpp++ = dvp; 4756 if (fhp != NULL) 4757 NFSBCOPY(&pf->dsf_fh, fhp++, 4758 NFSX_MYFH); 4759 if (fnamep != NULL && gotone == 1) 4760 strlcpy(fnamep, 4761 pf->dsf_filename, 4762 sizeof(pf->dsf_filename)); 4763 } else 4764 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt " 4765 "err=%d\n", error); 4766 } 4767 } 4768 } 4769 if (error == 0 && gotone == 0) 4770 error = ENOENT; 4771 4772 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone, 4773 error); 4774 if (error == 0) 4775 *mirrorcntp = gotone; 4776 else { 4777 if (gotone > 0 && dvpp != NULL) { 4778 /* 4779 * If the error didn't occur on the first one and 4780 * dvpp != NULL, the one(s) prior to the failure will 4781 * have locked dvp's that need to be unlocked. 4782 */ 4783 for (i = 0; i < gotone; i++) { 4784 NFSVOPUNLOCK(*dvpp, 0); 4785 *dvpp++ = NULL; 4786 } 4787 } 4788 /* 4789 * If it found the vnode to be copied from before a failure, 4790 * it needs to be vput()'d. 4791 */ 4792 if (nvpp != NULL && *nvpp != NULL) { 4793 vput(*nvpp); 4794 *nvpp = NULL; 4795 } 4796 } 4797 return (error); 4798 } 4799 4800 /* 4801 * Set the extended attribute for the Change attribute. 4802 */ 4803 static int 4804 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p) 4805 { 4806 struct pnfsdsattr dsattr; 4807 int error; 4808 4809 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp"); 4810 dsattr.dsa_filerev = nap->na_filerev; 4811 dsattr.dsa_size = nap->na_size; 4812 dsattr.dsa_atime = nap->na_atime; 4813 dsattr.dsa_mtime = nap->na_mtime; 4814 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM, 4815 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p); 4816 if (error != 0) 4817 printf("pNFS: setextattr=%d\n", error); 4818 return (error); 4819 } 4820 4821 static int 4822 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred, 4823 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp) 4824 { 4825 uint32_t *tl; 4826 struct nfsrv_descript *nd; 4827 nfsv4stateid_t st; 4828 struct mbuf *m, *m2; 4829 int error = 0, retlen, tlen, trimlen; 4830 4831 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n"); 4832 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 4833 *mpp = NULL; 4834 /* 4835 * Use a stateid where other is an alternating 01010 pattern and 4836 * seqid is 0xffffffff. This value is not defined as special by 4837 * the RFC and is used by the FreeBSD NFS server to indicate an 4838 * MDS->DS proxy operation. 4839 */ 4840 st.other[0] = 0x55555555; 4841 st.other[1] = 0x55555555; 4842 st.other[2] = 0x55555555; 4843 st.seqid = 0xffffffff; 4844 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp), 4845 NULL, NULL, 0, 0); 4846 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 4847 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3); 4848 txdr_hyper(off, tl); 4849 *(tl + 2) = txdr_unsigned(len); 4850 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 4851 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 4852 if (error != 0) { 4853 free(nd, M_TEMP); 4854 return (error); 4855 } 4856 if (nd->nd_repstat == 0) { 4857 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 4858 NFSM_STRSIZ(retlen, len); 4859 if (retlen > 0) { 4860 /* Trim off the pre-data XDR from the mbuf chain. */ 4861 m = nd->nd_mrep; 4862 while (m != NULL && m != nd->nd_md) { 4863 if (m->m_next == nd->nd_md) { 4864 m->m_next = NULL; 4865 m_freem(nd->nd_mrep); 4866 nd->nd_mrep = m = nd->nd_md; 4867 } else 4868 m = m->m_next; 4869 } 4870 if (m == NULL) { 4871 printf("nfsrv_readdsrpc: busted mbuf list\n"); 4872 error = ENOENT; 4873 goto nfsmout; 4874 } 4875 4876 /* 4877 * Now, adjust first mbuf so that any XDR before the 4878 * read data is skipped over. 4879 */ 4880 trimlen = nd->nd_dpos - mtod(m, char *); 4881 if (trimlen > 0) { 4882 m->m_len -= trimlen; 4883 NFSM_DATAP(m, trimlen); 4884 } 4885 4886 /* 4887 * Truncate the mbuf chain at retlen bytes of data, 4888 * plus XDR padding that brings the length up to a 4889 * multiple of 4. 4890 */ 4891 tlen = NFSM_RNDUP(retlen); 4892 do { 4893 if (m->m_len >= tlen) { 4894 m->m_len = tlen; 4895 tlen = 0; 4896 m2 = m->m_next; 4897 m->m_next = NULL; 4898 m_freem(m2); 4899 break; 4900 } 4901 tlen -= m->m_len; 4902 m = m->m_next; 4903 } while (m != NULL); 4904 if (tlen > 0) { 4905 printf("nfsrv_readdsrpc: busted mbuf list\n"); 4906 error = ENOENT; 4907 goto nfsmout; 4908 } 4909 *mpp = nd->nd_mrep; 4910 *mpendp = m; 4911 nd->nd_mrep = NULL; 4912 } 4913 } else 4914 error = nd->nd_repstat; 4915 nfsmout: 4916 /* If nd->nd_mrep is already NULL, this is a no-op. */ 4917 m_freem(nd->nd_mrep); 4918 free(nd, M_TEMP); 4919 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error); 4920 return (error); 4921 } 4922 4923 /* 4924 * Do a write RPC on a DS data file, using this structure for the arguments, 4925 * so that this function can be executed by a separate kernel process. 4926 */ 4927 struct nfsrvwritedsdorpc { 4928 int done; 4929 int inprog; 4930 struct task tsk; 4931 fhandle_t fh; 4932 off_t off; 4933 int len; 4934 struct nfsmount *nmp; 4935 struct ucred *cred; 4936 NFSPROC_T *p; 4937 struct mbuf *m; 4938 int err; 4939 }; 4940 4941 static int 4942 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len, 4943 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p) 4944 { 4945 uint32_t *tl; 4946 struct nfsrv_descript *nd; 4947 nfsattrbit_t attrbits; 4948 nfsv4stateid_t st; 4949 int commit, error, retlen; 4950 4951 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 4952 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp, 4953 sizeof(fhandle_t), NULL, NULL, 0, 0); 4954 4955 /* 4956 * Use a stateid where other is an alternating 01010 pattern and 4957 * seqid is 0xffffffff. This value is not defined as special by 4958 * the RFC and is used by the FreeBSD NFS server to indicate an 4959 * MDS->DS proxy operation. 4960 */ 4961 st.other[0] = 0x55555555; 4962 st.other[1] = 0x55555555; 4963 st.other[2] = 0x55555555; 4964 st.seqid = 0xffffffff; 4965 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 4966 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 4967 txdr_hyper(off, tl); 4968 tl += 2; 4969 /* 4970 * Do all writes FileSync, since the server doesn't hold onto dirty 4971 * buffers. Since clients should be accessing the DS servers directly 4972 * using the pNFS layouts, this just needs to work correctly as a 4973 * fallback. 4974 */ 4975 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC); 4976 *tl = txdr_unsigned(len); 4977 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len); 4978 4979 /* Put data in mbuf chain. */ 4980 nd->nd_mb->m_next = m; 4981 4982 /* Set nd_mb and nd_bpos to end of data. */ 4983 while (m->m_next != NULL) 4984 m = m->m_next; 4985 nd->nd_mb = m; 4986 nd->nd_bpos = mtod(m, char *) + m->m_len; 4987 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len); 4988 4989 /* Do a Getattr for Size, Change and Modify Time. */ 4990 NFSZERO_ATTRBIT(&attrbits); 4991 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 4992 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 4993 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 4994 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 4995 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 4996 *tl = txdr_unsigned(NFSV4OP_GETATTR); 4997 (void) nfsrv_putattrbit(nd, &attrbits); 4998 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 4999 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5000 if (error != 0) { 5001 free(nd, M_TEMP); 5002 return (error); 5003 } 5004 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat); 5005 /* Get rid of weak cache consistency data for now. */ 5006 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 5007 (ND_NFSV4 | ND_V4WCCATTR)) { 5008 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5009 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 5010 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error); 5011 if (error != 0) 5012 goto nfsmout; 5013 /* 5014 * Get rid of Op# and status for next op. 5015 */ 5016 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5017 if (*++tl != 0) 5018 nd->nd_flag |= ND_NOMOREDATA; 5019 } 5020 if (nd->nd_repstat == 0) { 5021 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 5022 retlen = fxdr_unsigned(int, *tl++); 5023 commit = fxdr_unsigned(int, *tl); 5024 if (commit != NFSWRITE_FILESYNC) 5025 error = NFSERR_IO; 5026 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n", 5027 retlen, commit, error); 5028 } else 5029 error = nd->nd_repstat; 5030 /* We have no use for the Write Verifier since we use FileSync. */ 5031 5032 /* 5033 * Get the Change, Size, Access Time and Modify Time attributes and set 5034 * on the Metadata file, so its attributes will be what the file's 5035 * would be if it had been written. 5036 */ 5037 if (error == 0) { 5038 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5039 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5040 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 5041 } 5042 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error); 5043 nfsmout: 5044 m_freem(nd->nd_mrep); 5045 free(nd, M_TEMP); 5046 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error); 5047 return (error); 5048 } 5049 5050 /* 5051 * Start up the thread that will execute nfsrv_writedsdorpc(). 5052 */ 5053 static void 5054 start_writedsdorpc(void *arg, int pending) 5055 { 5056 struct nfsrvwritedsdorpc *drpc; 5057 5058 drpc = (struct nfsrvwritedsdorpc *)arg; 5059 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 5060 drpc->len, NULL, drpc->m, drpc->cred, drpc->p); 5061 drpc->done = 1; 5062 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err); 5063 } 5064 5065 static int 5066 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred, 5067 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 5068 struct mbuf **mpp, char *cp, int *failposp) 5069 { 5070 struct nfsrvwritedsdorpc *drpc, *tdrpc; 5071 struct nfsvattr na; 5072 struct mbuf *m; 5073 int error, i, offs, ret, timo; 5074 5075 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n"); 5076 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain")); 5077 drpc = NULL; 5078 if (mirrorcnt > 1) 5079 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5080 M_WAITOK); 5081 5082 /* Calculate offset in mbuf chain that data starts. */ 5083 offs = cp - mtod(*mpp, char *); 5084 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len); 5085 5086 /* 5087 * Do the write RPC for every DS, using a separate kernel process 5088 * for every DS except the last one. 5089 */ 5090 error = 0; 5091 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5092 tdrpc->done = 0; 5093 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5094 tdrpc->off = off; 5095 tdrpc->len = len; 5096 tdrpc->nmp = *nmpp; 5097 tdrpc->cred = cred; 5098 tdrpc->p = p; 5099 tdrpc->inprog = 0; 5100 tdrpc->err = 0; 5101 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK); 5102 ret = EIO; 5103 if (nfs_pnfsiothreads != 0) { 5104 ret = nfs_pnfsio(start_writedsdorpc, tdrpc); 5105 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n", 5106 ret); 5107 } 5108 if (ret != 0) { 5109 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL, 5110 tdrpc->m, cred, p); 5111 if (nfsds_failerr(ret) && *failposp == -1) 5112 *failposp = i; 5113 else if (error == 0 && ret != 0) 5114 error = ret; 5115 } 5116 nmpp++; 5117 fhp++; 5118 } 5119 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK); 5120 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p); 5121 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5122 *failposp = mirrorcnt - 1; 5123 else if (error == 0 && ret != 0) 5124 error = ret; 5125 if (error == 0) 5126 error = nfsrv_setextattr(vp, &na, p); 5127 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error); 5128 tdrpc = drpc; 5129 timo = hz / 50; /* Wait for 20msec. */ 5130 if (timo < 1) 5131 timo = 1; 5132 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5133 /* Wait for RPCs on separate threads to complete. */ 5134 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5135 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo); 5136 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5137 *failposp = i; 5138 else if (error == 0 && tdrpc->err != 0) 5139 error = tdrpc->err; 5140 } 5141 free(drpc, M_TEMP); 5142 return (error); 5143 } 5144 5145 static int 5146 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 5147 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap, 5148 struct nfsvattr *dsnap) 5149 { 5150 uint32_t *tl; 5151 struct nfsrv_descript *nd; 5152 nfsv4stateid_t st; 5153 nfsattrbit_t attrbits; 5154 int error; 5155 5156 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n"); 5157 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5158 /* 5159 * Use a stateid where other is an alternating 01010 pattern and 5160 * seqid is 0xffffffff. This value is not defined as special by 5161 * the RFC and is used by the FreeBSD NFS server to indicate an 5162 * MDS->DS proxy operation. 5163 */ 5164 st.other[0] = 0x55555555; 5165 st.other[1] = 0x55555555; 5166 st.other[2] = 0x55555555; 5167 st.seqid = 0xffffffff; 5168 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp), 5169 NULL, NULL, 0, 0); 5170 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5171 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0); 5172 5173 /* Do a Getattr for Size, Change, Access Time and Modify Time. */ 5174 NFSZERO_ATTRBIT(&attrbits); 5175 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 5176 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 5177 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 5178 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 5179 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 5180 *tl = txdr_unsigned(NFSV4OP_GETATTR); 5181 (void) nfsrv_putattrbit(nd, &attrbits); 5182 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5183 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5184 if (error != 0) { 5185 free(nd, M_TEMP); 5186 return (error); 5187 } 5188 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n", 5189 nd->nd_repstat); 5190 /* Get rid of weak cache consistency data for now. */ 5191 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 5192 (ND_NFSV4 | ND_V4WCCATTR)) { 5193 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL, 5194 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 5195 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error); 5196 if (error != 0) 5197 goto nfsmout; 5198 /* 5199 * Get rid of Op# and status for next op. 5200 */ 5201 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5202 if (*++tl != 0) 5203 nd->nd_flag |= ND_NOMOREDATA; 5204 } 5205 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 5206 if (error != 0) 5207 goto nfsmout; 5208 if (nd->nd_repstat != 0) 5209 error = nd->nd_repstat; 5210 /* 5211 * Get the Change, Size, Access Time and Modify Time attributes and set 5212 * on the Metadata file, so its attributes will be what the file's 5213 * would be if it had been written. 5214 */ 5215 if (error == 0) { 5216 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5217 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL, 5218 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL); 5219 } 5220 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error); 5221 nfsmout: 5222 m_freem(nd->nd_mrep); 5223 free(nd, M_TEMP); 5224 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error); 5225 return (error); 5226 } 5227 5228 struct nfsrvsetattrdsdorpc { 5229 int done; 5230 int inprog; 5231 struct task tsk; 5232 fhandle_t fh; 5233 struct nfsmount *nmp; 5234 struct vnode *vp; 5235 struct ucred *cred; 5236 NFSPROC_T *p; 5237 struct nfsvattr na; 5238 struct nfsvattr dsna; 5239 int err; 5240 }; 5241 5242 /* 5243 * Start up the thread that will execute nfsrv_setattrdsdorpc(). 5244 */ 5245 static void 5246 start_setattrdsdorpc(void *arg, int pending) 5247 { 5248 struct nfsrvsetattrdsdorpc *drpc; 5249 5250 drpc = (struct nfsrvsetattrdsdorpc *)arg; 5251 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p, 5252 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna); 5253 drpc->done = 1; 5254 } 5255 5256 static int 5257 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 5258 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 5259 struct nfsvattr *nap, int *failposp) 5260 { 5261 struct nfsrvsetattrdsdorpc *drpc, *tdrpc; 5262 struct nfsvattr na; 5263 int error, i, ret, timo; 5264 5265 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n"); 5266 drpc = NULL; 5267 if (mirrorcnt > 1) 5268 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5269 M_WAITOK); 5270 5271 /* 5272 * Do the setattr RPC for every DS, using a separate kernel process 5273 * for every DS except the last one. 5274 */ 5275 error = 0; 5276 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5277 tdrpc->done = 0; 5278 tdrpc->inprog = 0; 5279 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5280 tdrpc->nmp = *nmpp; 5281 tdrpc->vp = vp; 5282 tdrpc->cred = cred; 5283 tdrpc->p = p; 5284 tdrpc->na = *nap; 5285 tdrpc->err = 0; 5286 ret = EIO; 5287 if (nfs_pnfsiothreads != 0) { 5288 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc); 5289 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n", 5290 ret); 5291 } 5292 if (ret != 0) { 5293 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, 5294 &na); 5295 if (nfsds_failerr(ret) && *failposp == -1) 5296 *failposp = i; 5297 else if (error == 0 && ret != 0) 5298 error = ret; 5299 } 5300 nmpp++; 5301 fhp++; 5302 } 5303 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na); 5304 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5305 *failposp = mirrorcnt - 1; 5306 else if (error == 0 && ret != 0) 5307 error = ret; 5308 if (error == 0) 5309 error = nfsrv_setextattr(vp, &na, p); 5310 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error); 5311 tdrpc = drpc; 5312 timo = hz / 50; /* Wait for 20msec. */ 5313 if (timo < 1) 5314 timo = 1; 5315 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5316 /* Wait for RPCs on separate threads to complete. */ 5317 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5318 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo); 5319 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5320 *failposp = i; 5321 else if (error == 0 && tdrpc->err != 0) 5322 error = tdrpc->err; 5323 } 5324 free(drpc, M_TEMP); 5325 return (error); 5326 } 5327 5328 /* 5329 * Do a Setattr of an NFSv4 ACL on the DS file. 5330 */ 5331 static int 5332 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 5333 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp) 5334 { 5335 struct nfsrv_descript *nd; 5336 nfsv4stateid_t st; 5337 nfsattrbit_t attrbits; 5338 int error; 5339 5340 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n"); 5341 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5342 /* 5343 * Use a stateid where other is an alternating 01010 pattern and 5344 * seqid is 0xffffffff. This value is not defined as special by 5345 * the RFC and is used by the FreeBSD NFS server to indicate an 5346 * MDS->DS proxy operation. 5347 */ 5348 st.other[0] = 0x55555555; 5349 st.other[1] = 0x55555555; 5350 st.other[2] = 0x55555555; 5351 st.seqid = 0xffffffff; 5352 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp), 5353 NULL, NULL, 0, 0); 5354 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5355 NFSZERO_ATTRBIT(&attrbits); 5356 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 5357 /* 5358 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(), 5359 * so passing in the metadata "vp" will be ok, since it is of 5360 * the same type (VREG). 5361 */ 5362 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL, 5363 NULL, 0, 0, 0, 0, 0, NULL); 5364 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5365 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5366 if (error != 0) { 5367 free(nd, M_TEMP); 5368 return (error); 5369 } 5370 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n", 5371 nd->nd_repstat); 5372 error = nd->nd_repstat; 5373 m_freem(nd->nd_mrep); 5374 free(nd, M_TEMP); 5375 return (error); 5376 } 5377 5378 struct nfsrvsetacldsdorpc { 5379 int done; 5380 int inprog; 5381 struct task tsk; 5382 fhandle_t fh; 5383 struct nfsmount *nmp; 5384 struct vnode *vp; 5385 struct ucred *cred; 5386 NFSPROC_T *p; 5387 struct acl *aclp; 5388 int err; 5389 }; 5390 5391 /* 5392 * Start up the thread that will execute nfsrv_setacldsdorpc(). 5393 */ 5394 static void 5395 start_setacldsdorpc(void *arg, int pending) 5396 { 5397 struct nfsrvsetacldsdorpc *drpc; 5398 5399 drpc = (struct nfsrvsetacldsdorpc *)arg; 5400 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p, 5401 drpc->vp, drpc->nmp, drpc->aclp); 5402 drpc->done = 1; 5403 } 5404 5405 static int 5406 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 5407 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp, 5408 int *failposp) 5409 { 5410 struct nfsrvsetacldsdorpc *drpc, *tdrpc; 5411 int error, i, ret, timo; 5412 5413 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n"); 5414 drpc = NULL; 5415 if (mirrorcnt > 1) 5416 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5417 M_WAITOK); 5418 5419 /* 5420 * Do the setattr RPC for every DS, using a separate kernel process 5421 * for every DS except the last one. 5422 */ 5423 error = 0; 5424 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5425 tdrpc->done = 0; 5426 tdrpc->inprog = 0; 5427 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5428 tdrpc->nmp = *nmpp; 5429 tdrpc->vp = vp; 5430 tdrpc->cred = cred; 5431 tdrpc->p = p; 5432 tdrpc->aclp = aclp; 5433 tdrpc->err = 0; 5434 ret = EIO; 5435 if (nfs_pnfsiothreads != 0) { 5436 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc); 5437 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n", 5438 ret); 5439 } 5440 if (ret != 0) { 5441 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, 5442 aclp); 5443 if (nfsds_failerr(ret) && *failposp == -1) 5444 *failposp = i; 5445 else if (error == 0 && ret != 0) 5446 error = ret; 5447 } 5448 nmpp++; 5449 fhp++; 5450 } 5451 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp); 5452 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5453 *failposp = mirrorcnt - 1; 5454 else if (error == 0 && ret != 0) 5455 error = ret; 5456 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error); 5457 tdrpc = drpc; 5458 timo = hz / 50; /* Wait for 20msec. */ 5459 if (timo < 1) 5460 timo = 1; 5461 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5462 /* Wait for RPCs on separate threads to complete. */ 5463 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5464 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo); 5465 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5466 *failposp = i; 5467 else if (error == 0 && tdrpc->err != 0) 5468 error = tdrpc->err; 5469 } 5470 free(drpc, M_TEMP); 5471 return (error); 5472 } 5473 5474 /* 5475 * Getattr call to the DS for the Modify, Size and Change attributes. 5476 */ 5477 static int 5478 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 5479 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap) 5480 { 5481 struct nfsrv_descript *nd; 5482 int error; 5483 nfsattrbit_t attrbits; 5484 5485 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n"); 5486 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5487 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp, 5488 sizeof(fhandle_t), NULL, NULL, 0, 0); 5489 NFSZERO_ATTRBIT(&attrbits); 5490 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 5491 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 5492 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 5493 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 5494 (void) nfsrv_putattrbit(nd, &attrbits); 5495 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5496 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5497 if (error != 0) { 5498 free(nd, M_TEMP); 5499 return (error); 5500 } 5501 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n", 5502 nd->nd_repstat); 5503 if (nd->nd_repstat == 0) { 5504 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 5505 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, 5506 NULL, NULL); 5507 /* 5508 * We can only save the updated values in the extended 5509 * attribute if the vp is exclusively locked. 5510 * This should happen when any of the following operations 5511 * occur on the vnode: 5512 * Close, Delegreturn, LayoutCommit, LayoutReturn 5513 * As such, the updated extended attribute should get saved 5514 * before nfsrv_checkdsattr() returns 0 and allows the cached 5515 * attributes to be returned without calling this function. 5516 */ 5517 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) { 5518 error = nfsrv_setextattr(vp, nap, p); 5519 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n", 5520 error); 5521 } 5522 } else 5523 error = nd->nd_repstat; 5524 m_freem(nd->nd_mrep); 5525 free(nd, M_TEMP); 5526 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error); 5527 return (error); 5528 } 5529 5530 /* 5531 * Get the device id and file handle for a DS file. 5532 */ 5533 int 5534 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp, 5535 fhandle_t *fhp, char *devid) 5536 { 5537 int buflen, error; 5538 char *buf; 5539 5540 buflen = 1024; 5541 buf = malloc(buflen, M_TEMP, M_WAITOK); 5542 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL, 5543 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL); 5544 free(buf, M_TEMP); 5545 return (error); 5546 } 5547 5548 /* 5549 * Do a Lookup against the DS for the filename. 5550 */ 5551 static int 5552 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf, 5553 struct vnode **nvpp, NFSPROC_T *p) 5554 { 5555 struct nameidata named; 5556 struct ucred *tcred; 5557 char *bufp; 5558 u_long *hashp; 5559 struct vnode *nvp; 5560 int error; 5561 5562 tcred = newnfs_getcred(); 5563 named.ni_cnd.cn_nameiop = LOOKUP; 5564 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY; 5565 named.ni_cnd.cn_cred = tcred; 5566 named.ni_cnd.cn_thread = p; 5567 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME; 5568 nfsvno_setpathbuf(&named, &bufp, &hashp); 5569 named.ni_cnd.cn_nameptr = bufp; 5570 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename); 5571 strlcpy(bufp, pf->dsf_filename, NAME_MAX); 5572 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp); 5573 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd); 5574 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error); 5575 NFSFREECRED(tcred); 5576 nfsvno_relpathbuf(&named); 5577 if (error == 0) 5578 *nvpp = nvp; 5579 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error); 5580 return (error); 5581 } 5582 5583 /* 5584 * Set the file handle to the correct one. 5585 */ 5586 static void 5587 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid, 5588 char *fnamep, struct vnode *nvp, NFSPROC_T *p) 5589 { 5590 struct nfsnode *np; 5591 int ret; 5592 5593 np = VTONFS(nvp); 5594 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH); 5595 /* 5596 * We can only do a vn_set_extattr() if the vnode is exclusively 5597 * locked and vn_start_write() has been done. If devid != NULL or 5598 * fnamep != NULL or the vnode is shared locked, vn_start_write() 5599 * may not have been done. 5600 * If not done now, it will be done on a future call. 5601 */ 5602 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) == 5603 LK_EXCLUSIVE) 5604 ret = vn_extattr_set(vp, IO_NODELOCKED, 5605 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf), 5606 (char *)pf, p); 5607 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret); 5608 } 5609 5610 /* 5611 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point 5612 * when the DS has failed. 5613 */ 5614 void 5615 nfsrv_killrpcs(struct nfsmount *nmp) 5616 { 5617 5618 /* 5619 * Call newnfs_nmcancelreqs() to cause 5620 * any RPCs in progress on the mount point to 5621 * fail. 5622 * This will cause any process waiting for an 5623 * RPC to complete while holding a vnode lock 5624 * on the mounted-on vnode (such as "df" or 5625 * a non-forced "umount") to fail. 5626 * This will unlock the mounted-on vnode so 5627 * a forced dismount can succeed. 5628 * The NFSMNTP_CANCELRPCS flag should be set when this function is 5629 * called. 5630 */ 5631 newnfs_nmcancelreqs(nmp); 5632 } 5633 5634 /* 5635 * Sum up the statfs info for each of the DSs, so that the client will 5636 * receive the total for all DSs. 5637 */ 5638 static int 5639 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp) 5640 { 5641 struct statfs *tsf; 5642 struct nfsdevice *ds; 5643 struct vnode **dvpp, **tdvpp, *dvp; 5644 uint64_t tot; 5645 int cnt, error = 0, i; 5646 5647 if (nfsrv_devidcnt <= 0) 5648 return (ENXIO); 5649 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK); 5650 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK); 5651 5652 /* Get an array of the dvps for the DSs. */ 5653 tdvpp = dvpp; 5654 i = 0; 5655 NFSDDSLOCK(); 5656 /* First, search for matches for same file system. */ 5657 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 5658 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 && 5659 ds->nfsdev_mdsfsid.val[0] == mp->mnt_stat.f_fsid.val[0] && 5660 ds->nfsdev_mdsfsid.val[1] == mp->mnt_stat.f_fsid.val[1]) { 5661 if (++i > nfsrv_devidcnt) 5662 break; 5663 *tdvpp++ = ds->nfsdev_dvp; 5664 } 5665 } 5666 /* 5667 * If no matches for same file system, total all servers not assigned 5668 * to a file system. 5669 */ 5670 if (i == 0) { 5671 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 5672 if (ds->nfsdev_nmp != NULL && 5673 ds->nfsdev_mdsisset == 0) { 5674 if (++i > nfsrv_devidcnt) 5675 break; 5676 *tdvpp++ = ds->nfsdev_dvp; 5677 } 5678 } 5679 } 5680 NFSDDSUNLOCK(); 5681 cnt = i; 5682 5683 /* Do a VFS_STATFS() for each of the DSs and sum them up. */ 5684 tdvpp = dvpp; 5685 for (i = 0; i < cnt && error == 0; i++) { 5686 dvp = *tdvpp++; 5687 error = VFS_STATFS(dvp->v_mount, tsf); 5688 if (error == 0) { 5689 if (sf->f_bsize == 0) { 5690 if (tsf->f_bsize > 0) 5691 sf->f_bsize = tsf->f_bsize; 5692 else 5693 sf->f_bsize = 8192; 5694 } 5695 if (tsf->f_blocks > 0) { 5696 if (sf->f_bsize != tsf->f_bsize) { 5697 tot = tsf->f_blocks * tsf->f_bsize; 5698 sf->f_blocks += (tot / sf->f_bsize); 5699 } else 5700 sf->f_blocks += tsf->f_blocks; 5701 } 5702 if (tsf->f_bfree > 0) { 5703 if (sf->f_bsize != tsf->f_bsize) { 5704 tot = tsf->f_bfree * tsf->f_bsize; 5705 sf->f_bfree += (tot / sf->f_bsize); 5706 } else 5707 sf->f_bfree += tsf->f_bfree; 5708 } 5709 if (tsf->f_bavail > 0) { 5710 if (sf->f_bsize != tsf->f_bsize) { 5711 tot = tsf->f_bavail * tsf->f_bsize; 5712 sf->f_bavail += (tot / sf->f_bsize); 5713 } else 5714 sf->f_bavail += tsf->f_bavail; 5715 } 5716 } 5717 } 5718 free(tsf, M_TEMP); 5719 free(dvpp, M_TEMP); 5720 return (error); 5721 } 5722 5723 /* 5724 * Set an NFSv4 acl. 5725 */ 5726 int 5727 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p) 5728 { 5729 int error; 5730 5731 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) { 5732 error = NFSERR_ATTRNOTSUPP; 5733 goto out; 5734 } 5735 /* 5736 * With NFSv4 ACLs, chmod(2) may need to add additional entries. 5737 * Make sure it has enough room for that - splitting every entry 5738 * into two and appending "canonical six" entries at the end. 5739 * Cribbed out of kern/vfs_acl.c - Rick M. 5740 */ 5741 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) { 5742 error = NFSERR_ATTRNOTSUPP; 5743 goto out; 5744 } 5745 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p); 5746 if (error == 0) { 5747 error = nfsrv_dssetacl(vp, aclp, cred, p); 5748 if (error == ENOENT) 5749 error = 0; 5750 } 5751 5752 out: 5753 NFSEXITCODE(error); 5754 return (error); 5755 } 5756 5757 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 5758 5759 /* 5760 * Called once to initialize data structures... 5761 */ 5762 static int 5763 nfsd_modevent(module_t mod, int type, void *data) 5764 { 5765 int error = 0, i; 5766 static int loaded = 0; 5767 5768 switch (type) { 5769 case MOD_LOAD: 5770 if (loaded) 5771 goto out; 5772 newnfs_portinit(); 5773 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 5774 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL, 5775 MTX_DEF); 5776 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL, 5777 MTX_DEF); 5778 } 5779 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF); 5780 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF); 5781 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF); 5782 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF); 5783 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF); 5784 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0); 5785 nfsrvd_initcache(); 5786 nfsd_init(); 5787 NFSD_LOCK(); 5788 nfsrvd_init(0); 5789 NFSD_UNLOCK(); 5790 nfsd_mntinit(); 5791 #ifdef VV_DISABLEDELEG 5792 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 5793 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 5794 #endif 5795 nfsd_call_servertimer = nfsrv_servertimer; 5796 nfsd_call_nfsd = nfssvc_nfsd; 5797 loaded = 1; 5798 break; 5799 5800 case MOD_UNLOAD: 5801 if (newnfs_numnfsd != 0) { 5802 error = EBUSY; 5803 break; 5804 } 5805 5806 #ifdef VV_DISABLEDELEG 5807 vn_deleg_ops.vndeleg_recall = NULL; 5808 vn_deleg_ops.vndeleg_disable = NULL; 5809 #endif 5810 nfsd_call_servertimer = NULL; 5811 nfsd_call_nfsd = NULL; 5812 5813 /* Clean out all NFSv4 state. */ 5814 nfsrv_throwawayallstate(curthread); 5815 5816 /* Clean the NFS server reply cache */ 5817 nfsrvd_cleancache(); 5818 5819 /* Free up the krpc server pool. */ 5820 if (nfsrvd_pool != NULL) 5821 svcpool_destroy(nfsrvd_pool); 5822 5823 /* and get rid of the locks */ 5824 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 5825 mtx_destroy(&nfsrchash_table[i].mtx); 5826 mtx_destroy(&nfsrcahash_table[i].mtx); 5827 } 5828 mtx_destroy(&nfsrc_udpmtx); 5829 mtx_destroy(&nfs_v4root_mutex); 5830 mtx_destroy(&nfsv4root_mnt.mnt_mtx); 5831 mtx_destroy(&nfsrv_dontlistlock_mtx); 5832 mtx_destroy(&nfsrv_recalllock_mtx); 5833 for (i = 0; i < nfsrv_sessionhashsize; i++) 5834 mtx_destroy(&nfssessionhash[i].mtx); 5835 if (nfslayouthash != NULL) { 5836 for (i = 0; i < nfsrv_layouthashsize; i++) 5837 mtx_destroy(&nfslayouthash[i].mtx); 5838 free(nfslayouthash, M_NFSDSESSION); 5839 } 5840 lockdestroy(&nfsv4root_mnt.mnt_explock); 5841 free(nfsclienthash, M_NFSDCLIENT); 5842 free(nfslockhash, M_NFSDLOCKFILE); 5843 free(nfssessionhash, M_NFSDSESSION); 5844 loaded = 0; 5845 break; 5846 default: 5847 error = EOPNOTSUPP; 5848 break; 5849 } 5850 5851 out: 5852 NFSEXITCODE(error); 5853 return (error); 5854 } 5855 static moduledata_t nfsd_mod = { 5856 "nfsd", 5857 nfsd_modevent, 5858 NULL, 5859 }; 5860 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 5861 5862 /* So that loader and kldload(2) can find us, wherever we are.. */ 5863 MODULE_VERSION(nfsd, 1); 5864 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 5865 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1); 5866 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 5867 MODULE_DEPEND(nfsd, krpc, 1, 1, 1); 5868 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 5869 5870