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