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