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