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