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