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 { 2118 struct statfs *sf; 2119 int error; 2120 2121 sf = NULL; 2122 if (nfsrv_devidcnt > 0 && 2123 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) || 2124 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) || 2125 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) { 2126 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO); 2127 error = nfsrv_pnfsstatfs(sf, mp); 2128 if (error != 0) { 2129 free(sf, M_TEMP); 2130 sf = NULL; 2131 } 2132 } 2133 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 2134 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 2135 mounted_on_fileno, sf, xattrsupp, has_hiddensystem, has_namedattr); 2136 free(sf, M_TEMP); 2137 NFSEXITCODE2(0, nd); 2138 return (error); 2139 } 2140 2141 /* 2142 * Convert a dirent d_type to a vnode type. 2143 */ 2144 static void nfs_dtypetovtype(struct nfsvattr *nvap, struct vnode *vp, 2145 uint8_t dtype) 2146 { 2147 2148 if ((vn_irflag_read(vp) & VIRF_NAMEDDIR) != 0) { 2149 nvap->na_type = VREG; 2150 nvap->na_bsdflags |= SFBSD_NAMEDATTR; 2151 } else if (dtype <= DT_WHT) { 2152 nvap->na_type = dtype_to_vnode[dtype]; 2153 } else { 2154 nvap->na_type = VNON; 2155 } 2156 } 2157 2158 /* Since the Readdir vnode ops vary, put the entire functions in here. */ 2159 /* 2160 * nfs readdir service 2161 * - mallocs what it thinks is enough to read 2162 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 2163 * - calls VOP_READDIR() 2164 * - loops around building the reply 2165 * if the output generated exceeds count break out of loop 2166 * The NFSM_CLGET macro is used here so that the reply will be packed 2167 * tightly in mbuf clusters. 2168 * - it trims out records with d_fileno == 0 2169 * this doesn't matter for Unix clients, but they might confuse clients 2170 * for other os'. 2171 * - it trims out records with d_type == DT_WHT 2172 * these cannot be seen through NFS (unless we extend the protocol) 2173 * The alternate call nfsrvd_readdirplus() does lookups as well. 2174 * PS: The NFS protocol spec. does not clarify what the "count" byte 2175 * argument is a count of.. just name strings and file id's or the 2176 * entire reply rpc or ... 2177 * I tried just file name and id sizes and it confused the Sun client, 2178 * so I am using the full rpc size now. The "paranoia.." comment refers 2179 * to including the status longwords that are not a part of the dir. 2180 * "entry" structures, but are in the rpc. 2181 */ 2182 int 2183 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 2184 struct vnode *vp, struct nfsexstuff *exp) 2185 { 2186 struct dirent *dp; 2187 u_int32_t *tl; 2188 int dirlen; 2189 char *cpos, *cend, *rbuf; 2190 struct nfsvattr at; 2191 int nlen, error = 0, getret = 1; 2192 int siz, cnt, fullsiz, eofflag, ncookies; 2193 u_int64_t off, toff, verf __unused; 2194 uint64_t *cookies = NULL, *cookiep; 2195 struct uio io; 2196 struct iovec iv; 2197 int is_ufs; 2198 struct thread *p = curthread; 2199 2200 if (nd->nd_repstat) { 2201 nfsrv_postopattr(nd, getret, &at); 2202 goto out; 2203 } 2204 if (nd->nd_flag & ND_NFSV2) { 2205 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2206 off = fxdr_unsigned(u_quad_t, *tl++); 2207 } else { 2208 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2209 off = fxdr_hyper(tl); 2210 tl += 2; 2211 verf = fxdr_hyper(tl); 2212 tl += 2; 2213 } 2214 toff = off; 2215 cnt = fxdr_unsigned(int, *tl); 2216 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 2217 cnt = NFS_SRVMAXDATA(nd); 2218 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 2219 fullsiz = siz; 2220 if (nd->nd_flag & ND_NFSV3) { 2221 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, 2222 NULL); 2223 #if 0 2224 /* 2225 * va_filerev is not sufficient as a cookie verifier, 2226 * since it is not supposed to change when entries are 2227 * removed/added unless that offset cookies returned to 2228 * the client are no longer valid. 2229 */ 2230 if (!nd->nd_repstat && toff && verf != at.na_filerev) 2231 nd->nd_repstat = NFSERR_BAD_COOKIE; 2232 #endif 2233 } 2234 if (!nd->nd_repstat && vp->v_type != VDIR) 2235 nd->nd_repstat = NFSERR_NOTDIR; 2236 if (nd->nd_repstat == 0 && cnt == 0) { 2237 if (nd->nd_flag & ND_NFSV2) 2238 /* NFSv2 does not have NFSERR_TOOSMALL */ 2239 nd->nd_repstat = EPERM; 2240 else 2241 nd->nd_repstat = NFSERR_TOOSMALL; 2242 } 2243 if (!nd->nd_repstat) 2244 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 2245 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 2246 NFSACCCHK_VPISLOCKED, NULL); 2247 if (nd->nd_repstat) { 2248 vput(vp); 2249 if (nd->nd_flag & ND_NFSV3) 2250 nfsrv_postopattr(nd, getret, &at); 2251 goto out; 2252 } 2253 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 2254 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2255 again: 2256 eofflag = 0; 2257 if (cookies) { 2258 free(cookies, M_TEMP); 2259 cookies = NULL; 2260 } 2261 2262 iv.iov_base = rbuf; 2263 iv.iov_len = siz; 2264 io.uio_iov = &iv; 2265 io.uio_iovcnt = 1; 2266 io.uio_offset = (off_t)off; 2267 io.uio_resid = siz; 2268 io.uio_segflg = UIO_SYSSPACE; 2269 io.uio_rw = UIO_READ; 2270 io.uio_td = NULL; 2271 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 2272 &cookies); 2273 off = (u_int64_t)io.uio_offset; 2274 if (io.uio_resid) 2275 siz -= io.uio_resid; 2276 2277 if (!cookies && !nd->nd_repstat) 2278 nd->nd_repstat = NFSERR_PERM; 2279 if (nd->nd_flag & ND_NFSV3) { 2280 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL); 2281 if (!nd->nd_repstat) 2282 nd->nd_repstat = getret; 2283 } 2284 2285 /* 2286 * Handles the failed cases. nd->nd_repstat == 0 past here. 2287 */ 2288 if (nd->nd_repstat) { 2289 vput(vp); 2290 free(rbuf, M_TEMP); 2291 if (cookies) 2292 free(cookies, M_TEMP); 2293 if (nd->nd_flag & ND_NFSV3) 2294 nfsrv_postopattr(nd, getret, &at); 2295 goto out; 2296 } 2297 /* 2298 * If nothing read, return eof 2299 * rpc reply 2300 */ 2301 if (siz == 0) { 2302 vput(vp); 2303 if (nd->nd_flag & ND_NFSV2) { 2304 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2305 } else { 2306 nfsrv_postopattr(nd, getret, &at); 2307 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2308 txdr_hyper(at.na_filerev, tl); 2309 tl += 2; 2310 } 2311 *tl++ = newnfs_false; 2312 *tl = newnfs_true; 2313 free(rbuf, M_TEMP); 2314 free(cookies, M_TEMP); 2315 goto out; 2316 } 2317 2318 /* 2319 * Check for degenerate cases of nothing useful read. 2320 * If so go try again 2321 */ 2322 cpos = rbuf; 2323 cend = rbuf + siz; 2324 dp = (struct dirent *)cpos; 2325 cookiep = cookies; 2326 2327 /* 2328 * For some reason FreeBSD's ufs_readdir() chooses to back the 2329 * directory offset up to a block boundary, so it is necessary to 2330 * skip over the records that precede the requested offset. This 2331 * requires the assumption that file offset cookies monotonically 2332 * increase. 2333 */ 2334 while (cpos < cend && ncookies > 0 && 2335 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 2336 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) { 2337 cpos += dp->d_reclen; 2338 dp = (struct dirent *)cpos; 2339 cookiep++; 2340 ncookies--; 2341 } 2342 if (cpos >= cend || ncookies == 0) { 2343 siz = fullsiz; 2344 toff = off; 2345 goto again; 2346 } 2347 vput(vp); 2348 2349 /* 2350 * If cnt > MCLBYTES and the reply will not be saved, use 2351 * ext_pgs mbufs for TLS. 2352 * For NFSv4.0, we do not know for sure if the reply will 2353 * be saved, so do not use ext_pgs mbufs for NFSv4.0. 2354 */ 2355 if (cnt > MCLBYTES && siz > MCLBYTES && 2356 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS && 2357 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4) 2358 nd->nd_flag |= ND_EXTPG; 2359 2360 /* 2361 * dirlen is the size of the reply, including all XDR and must 2362 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 2363 * if the XDR should be included in "count", but to be safe, we do. 2364 * (Include the two booleans at the end of the reply in dirlen now.) 2365 */ 2366 if (nd->nd_flag & ND_NFSV3) { 2367 nfsrv_postopattr(nd, getret, &at); 2368 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2369 txdr_hyper(at.na_filerev, tl); 2370 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 2371 } else { 2372 dirlen = 2 * NFSX_UNSIGNED; 2373 } 2374 2375 /* Loop through the records and build reply */ 2376 while (cpos < cend && ncookies > 0) { 2377 nlen = dp->d_namlen; 2378 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2379 nlen <= NFS_MAXNAMLEN) { 2380 if (nd->nd_flag & ND_NFSV3) 2381 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 2382 else 2383 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 2384 if (dirlen > cnt) { 2385 eofflag = 0; 2386 break; 2387 } 2388 2389 /* 2390 * Build the directory record xdr from 2391 * the dirent entry. 2392 */ 2393 if (nd->nd_flag & ND_NFSV3) { 2394 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2395 *tl++ = newnfs_true; 2396 txdr_hyper(dp->d_fileno, tl); 2397 } else { 2398 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2399 *tl++ = newnfs_true; 2400 *tl = txdr_unsigned(dp->d_fileno); 2401 } 2402 (void) nfsm_strtom(nd, dp->d_name, nlen); 2403 if (nd->nd_flag & ND_NFSV3) { 2404 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2405 txdr_hyper(*cookiep, tl); 2406 } else { 2407 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 2408 *tl = txdr_unsigned(*cookiep); 2409 } 2410 } 2411 cpos += dp->d_reclen; 2412 dp = (struct dirent *)cpos; 2413 cookiep++; 2414 ncookies--; 2415 } 2416 if (cpos < cend) 2417 eofflag = 0; 2418 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2419 *tl++ = newnfs_false; 2420 if (eofflag) 2421 *tl = newnfs_true; 2422 else 2423 *tl = newnfs_false; 2424 free(rbuf, M_TEMP); 2425 free(cookies, M_TEMP); 2426 2427 out: 2428 NFSEXITCODE2(0, nd); 2429 return (0); 2430 nfsmout: 2431 vput(vp); 2432 NFSEXITCODE2(error, nd); 2433 return (error); 2434 } 2435 2436 /* 2437 * Readdirplus for V3 and Readdir for V4. 2438 */ 2439 int 2440 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 2441 struct vnode *vp, struct nfsexstuff *exp) 2442 { 2443 struct dirent *dp; 2444 u_int32_t *tl; 2445 int dirlen; 2446 char *cpos, *cend, *rbuf; 2447 struct vnode *nvp; 2448 fhandle_t nfh; 2449 struct nfsvattr nva, at, *nvap = &nva; 2450 struct mbuf *mb0, *mb1; 2451 struct nfsreferral *refp; 2452 int nlen, r, error = 0, getret = 1, ret, usevget = 1; 2453 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 2454 caddr_t bpos0, bpos1; 2455 u_int64_t off, toff, verf __unused; 2456 uint64_t *cookies = NULL, *cookiep; 2457 nfsattrbit_t attrbits, rderrbits, savbits, refbits; 2458 struct uio io; 2459 struct iovec iv; 2460 struct componentname cn; 2461 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls; 2462 struct mount *mp, *new_mp; 2463 uint64_t mounted_on_fileno; 2464 struct thread *p = curthread; 2465 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1; 2466 size_t atsiz; 2467 long pathval; 2468 bool has_hiddensystem, has_namedattr, xattrsupp; 2469 2470 if (nd->nd_repstat) { 2471 nfsrv_postopattr(nd, getret, &at); 2472 goto out; 2473 } 2474 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 2475 off = fxdr_hyper(tl); 2476 toff = off; 2477 tl += 2; 2478 verf = fxdr_hyper(tl); 2479 tl += 2; 2480 siz = fxdr_unsigned(int, *tl++); 2481 cnt = fxdr_unsigned(int, *tl); 2482 2483 /* 2484 * Use the server's maximum data transfer size as the upper bound 2485 * on reply datalen. 2486 */ 2487 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 2488 cnt = NFS_SRVMAXDATA(nd); 2489 2490 /* 2491 * siz is a "hint" of how much directory information (name, fileid, 2492 * cookie) should be in the reply. At least one client "hints" 0, 2493 * so I set it to cnt for that case. I also round it up to the 2494 * next multiple of DIRBLKSIZ. 2495 * Since the size of a Readdirplus directory entry reply will always 2496 * be greater than a directory entry returned by VOP_READDIR(), it 2497 * does not make sense to read more than NFS_SRVMAXDATA() via 2498 * VOP_READDIR(). 2499 */ 2500 if (siz <= 0) 2501 siz = cnt; 2502 else if (siz > NFS_SRVMAXDATA(nd)) 2503 siz = NFS_SRVMAXDATA(nd); 2504 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 2505 2506 if (nd->nd_flag & ND_NFSV4) { 2507 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 2508 if (error) 2509 goto nfsmout; 2510 NFSSET_ATTRBIT(&savbits, &attrbits); 2511 NFSSET_ATTRBIT(&refbits, &attrbits); 2512 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd); 2513 NFSZERO_ATTRBIT(&rderrbits); 2514 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 2515 /* 2516 * If these 4 bits are the only attributes requested by the 2517 * client, they can be satisfied without acquiring the vnode 2518 * for the file object unless it is a directory. 2519 * This will be indicated by savbits being all 0s. 2520 */ 2521 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_TYPE); 2522 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_FILEID); 2523 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_MOUNTEDONFILEID); 2524 NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_RDATTRERROR); 2525 } else { 2526 NFSZERO_ATTRBIT(&attrbits); 2527 } 2528 fullsiz = siz; 2529 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL); 2530 #if 0 2531 if (!nd->nd_repstat) { 2532 if (off && verf != at.na_filerev) { 2533 /* 2534 * va_filerev is not sufficient as a cookie verifier, 2535 * since it is not supposed to change when entries are 2536 * removed/added unless that offset cookies returned to 2537 * the client are no longer valid. 2538 */ 2539 if (nd->nd_flag & ND_NFSV4) { 2540 nd->nd_repstat = NFSERR_NOTSAME; 2541 } else { 2542 nd->nd_repstat = NFSERR_BAD_COOKIE; 2543 } 2544 } 2545 } 2546 #endif 2547 if (!nd->nd_repstat && vp->v_type != VDIR) 2548 nd->nd_repstat = NFSERR_NOTDIR; 2549 if (!nd->nd_repstat && cnt == 0) 2550 nd->nd_repstat = NFSERR_TOOSMALL; 2551 if (!nd->nd_repstat) 2552 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 2553 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 2554 NFSACCCHK_VPISLOCKED, NULL); 2555 if (nd->nd_repstat) { 2556 vput(vp); 2557 if (nd->nd_flag & ND_NFSV3) 2558 nfsrv_postopattr(nd, getret, &at); 2559 goto out; 2560 } 2561 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 2562 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0; 2563 2564 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2565 again: 2566 eofflag = 0; 2567 if (cookies) { 2568 free(cookies, M_TEMP); 2569 cookies = NULL; 2570 } 2571 2572 iv.iov_base = rbuf; 2573 iv.iov_len = siz; 2574 io.uio_iov = &iv; 2575 io.uio_iovcnt = 1; 2576 io.uio_offset = (off_t)off; 2577 io.uio_resid = siz; 2578 io.uio_segflg = UIO_SYSSPACE; 2579 io.uio_rw = UIO_READ; 2580 io.uio_td = NULL; 2581 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 2582 &cookies); 2583 off = (u_int64_t)io.uio_offset; 2584 if (io.uio_resid) 2585 siz -= io.uio_resid; 2586 2587 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL); 2588 2589 if (!cookies && !nd->nd_repstat) 2590 nd->nd_repstat = NFSERR_PERM; 2591 if (!nd->nd_repstat) 2592 nd->nd_repstat = getret; 2593 if (nd->nd_repstat) { 2594 vput(vp); 2595 if (cookies) 2596 free(cookies, M_TEMP); 2597 free(rbuf, M_TEMP); 2598 if (nd->nd_flag & ND_NFSV3) 2599 nfsrv_postopattr(nd, getret, &at); 2600 goto out; 2601 } 2602 /* 2603 * If nothing read, return eof 2604 * rpc reply 2605 */ 2606 if (siz == 0) { 2607 vput(vp); 2608 if (nd->nd_flag & ND_NFSV3) 2609 nfsrv_postopattr(nd, getret, &at); 2610 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2611 txdr_hyper(at.na_filerev, tl); 2612 tl += 2; 2613 *tl++ = newnfs_false; 2614 *tl = newnfs_true; 2615 free(cookies, M_TEMP); 2616 free(rbuf, M_TEMP); 2617 goto out; 2618 } 2619 2620 /* 2621 * Check for degenerate cases of nothing useful read. 2622 * If so go try again 2623 */ 2624 cpos = rbuf; 2625 cend = rbuf + siz; 2626 dp = (struct dirent *)cpos; 2627 cookiep = cookies; 2628 2629 /* 2630 * For some reason FreeBSD's ufs_readdir() chooses to back the 2631 * directory offset up to a block boundary, so it is necessary to 2632 * skip over the records that precede the requested offset. This 2633 * requires the assumption that file offset cookies monotonically 2634 * increase. 2635 */ 2636 while (cpos < cend && ncookies > 0 && 2637 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 2638 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) || 2639 ((nd->nd_flag & ND_NFSV4) && 2640 ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 2641 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 2642 cpos += dp->d_reclen; 2643 dp = (struct dirent *)cpos; 2644 cookiep++; 2645 ncookies--; 2646 } 2647 if (cpos >= cend || ncookies == 0) { 2648 siz = fullsiz; 2649 toff = off; 2650 goto again; 2651 } 2652 2653 /* 2654 * Busy the file system so that the mount point won't go away 2655 * and, as such, VFS_VGET() can be used safely. 2656 */ 2657 mp = vp->v_mount; 2658 vfs_ref(mp); 2659 NFSVOPUNLOCK(vp); 2660 nd->nd_repstat = vfs_busy(mp, 0); 2661 vfs_rel(mp); 2662 if (nd->nd_repstat != 0) { 2663 vrele(vp); 2664 free(cookies, M_TEMP); 2665 free(rbuf, M_TEMP); 2666 if (nd->nd_flag & ND_NFSV3) 2667 nfsrv_postopattr(nd, getret, &at); 2668 goto out; 2669 } 2670 2671 /* 2672 * Check to see if entries in this directory can be safely acquired 2673 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required. 2674 * ZFS snapshot directories need VOP_LOOKUP(), so that any 2675 * automount of the snapshot directory that is required will 2676 * be done. 2677 * This needs to be done here for NFSv4, since NFSv4 never does 2678 * a VFS_VGET() for "." or "..". 2679 */ 2680 if (is_zfs == 1) { 2681 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp); 2682 if (r == EOPNOTSUPP) { 2683 usevget = 0; 2684 cn.cn_nameiop = LOOKUP; 2685 cn.cn_lkflags = LK_SHARED | LK_RETRY; 2686 cn.cn_cred = nd->nd_cred; 2687 } else if (r == 0) 2688 vput(nvp); 2689 } 2690 2691 /* 2692 * If the reply is likely to exceed MCLBYTES and the reply will 2693 * not be saved, use ext_pgs mbufs for TLS. 2694 * It is difficult to predict how large each entry will be and 2695 * how many entries have been read, so just assume the directory 2696 * entries grow by a factor of 4 when attributes are included. 2697 * For NFSv4.0, we do not know for sure if the reply will 2698 * be saved, so do not use ext_pgs mbufs for NFSv4.0. 2699 */ 2700 if (cnt > MCLBYTES && siz > MCLBYTES / 4 && 2701 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS && 2702 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4) 2703 nd->nd_flag |= ND_EXTPG; 2704 2705 /* 2706 * Save this position, in case there is an error before one entry 2707 * is created. 2708 */ 2709 mb0 = nd->nd_mb; 2710 bpos0 = nd->nd_bpos; 2711 bextpg0 = nd->nd_bextpg; 2712 bextpgsiz0 = nd->nd_bextpgsiz; 2713 2714 /* 2715 * Fill in the first part of the reply. 2716 * dirlen is the reply length in bytes and cannot exceed cnt. 2717 * (Include the two booleans at the end of the reply in dirlen now, 2718 * so we recognize when we have exceeded cnt.) 2719 */ 2720 if (nd->nd_flag & ND_NFSV3) { 2721 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 2722 nfsrv_postopattr(nd, getret, &at); 2723 } else { 2724 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 2725 } 2726 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2727 txdr_hyper(at.na_filerev, tl); 2728 2729 /* 2730 * Save this position, in case there is an empty reply needed. 2731 */ 2732 mb1 = nd->nd_mb; 2733 bpos1 = nd->nd_bpos; 2734 bextpg1 = nd->nd_bextpg; 2735 bextpgsiz1 = nd->nd_bextpgsiz; 2736 2737 /* Loop through the records and build reply */ 2738 entrycnt = 0; 2739 while (cpos < cend && ncookies > 0 && dirlen < cnt) { 2740 nlen = dp->d_namlen; 2741 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2742 nlen <= NFS_MAXNAMLEN && 2743 ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 2744 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 2745 || (nlen == 1 && dp->d_name[0] != '.'))) { 2746 /* 2747 * Save the current position in the reply, in case 2748 * this entry exceeds cnt. 2749 */ 2750 mb1 = nd->nd_mb; 2751 bpos1 = nd->nd_bpos; 2752 bextpg1 = nd->nd_bextpg; 2753 bextpgsiz1 = nd->nd_bextpgsiz; 2754 2755 /* 2756 * For readdir_and_lookup get the vnode using 2757 * the file number. 2758 */ 2759 nvp = NULL; 2760 refp = NULL; 2761 r = 0; 2762 at_root = 0; 2763 needs_unbusy = 0; 2764 new_mp = mp; 2765 mounted_on_fileno = (uint64_t)dp->d_fileno; 2766 if ((nd->nd_flag & ND_NFSV3) || 2767 NFSNONZERO_ATTRBIT(&savbits) || 2768 dp->d_type == DT_UNKNOWN || 2769 (dp->d_type == DT_DIR && 2770 nfsrv_enable_crossmntpt != 0)) { 2771 if (nd->nd_flag & ND_NFSV4) 2772 refp = nfsv4root_getreferral(NULL, 2773 vp, dp->d_fileno); 2774 if (refp == NULL) { 2775 if (usevget) 2776 r = VFS_VGET(mp, dp->d_fileno, 2777 LK_SHARED, &nvp); 2778 else 2779 r = EOPNOTSUPP; 2780 if (r == 0 && (vn_irflag_read(vp) & 2781 VIRF_NAMEDDIR) != 0) 2782 vn_irflag_set_cond(nvp, 2783 VIRF_NAMEDATTR); 2784 if (r == EOPNOTSUPP) { 2785 if (usevget) { 2786 usevget = 0; 2787 cn.cn_nameiop = LOOKUP; 2788 cn.cn_lkflags = 2789 LK_SHARED | 2790 LK_RETRY; 2791 cn.cn_cred = 2792 nd->nd_cred; 2793 } 2794 cn.cn_nameptr = dp->d_name; 2795 cn.cn_namelen = nlen; 2796 cn.cn_flags = ISLASTCN | 2797 NOFOLLOW | LOCKLEAF; 2798 if ((vn_irflag_read(vp) & 2799 VIRF_NAMEDDIR) != 0) 2800 cn.cn_flags |= 2801 OPENNAMED; 2802 if (nlen == 2 && 2803 dp->d_name[0] == '.' && 2804 dp->d_name[1] == '.') 2805 cn.cn_flags |= 2806 ISDOTDOT; 2807 if (NFSVOPLOCK(vp, LK_SHARED) 2808 != 0) { 2809 nd->nd_repstat = EPERM; 2810 break; 2811 } 2812 if ((vp->v_vflag & VV_ROOT) != 0 2813 && (cn.cn_flags & ISDOTDOT) 2814 != 0) { 2815 vref(vp); 2816 nvp = vp; 2817 r = 0; 2818 } else { 2819 r = VOP_LOOKUP(vp, &nvp, 2820 &cn); 2821 if (vp != nvp) 2822 NFSVOPUNLOCK(vp); 2823 } 2824 } 2825 2826 /* 2827 * For NFSv4, check to see if nvp is 2828 * a mount point and get the mount 2829 * point vnode, as required. 2830 */ 2831 if (r == 0 && 2832 nfsrv_enable_crossmntpt != 0 && 2833 (nd->nd_flag & ND_NFSV4) != 0 && 2834 nvp->v_type == VDIR && 2835 nvp->v_mountedhere != NULL) { 2836 new_mp = nvp->v_mountedhere; 2837 r = vfs_busy(new_mp, 0); 2838 vput(nvp); 2839 nvp = NULL; 2840 if (r == 0) { 2841 r = VFS_ROOT(new_mp, 2842 LK_SHARED, &nvp); 2843 needs_unbusy = 1; 2844 if (r == 0) 2845 at_root = 1; 2846 } 2847 } 2848 } 2849 2850 /* 2851 * If we failed to look up the entry, then it 2852 * has become invalid, most likely removed. 2853 */ 2854 if (r != 0) { 2855 if (needs_unbusy) 2856 vfs_unbusy(new_mp); 2857 goto invalid; 2858 } 2859 KASSERT(refp != NULL || nvp != NULL, 2860 ("%s: undetected lookup error", __func__)); 2861 2862 if (refp == NULL && 2863 ((nd->nd_flag & ND_NFSV3) || 2864 NFSNONZERO_ATTRBIT(&attrbits))) { 2865 r = nfsvno_getfh(nvp, &nfh, p); 2866 if (!r) 2867 r = nfsvno_getattr(nvp, nvap, nd, p, 2868 1, &attrbits); 2869 if (r == 0 && is_zfs == 1 && 2870 nfsrv_enable_crossmntpt != 0 && 2871 (nd->nd_flag & ND_NFSV4) != 0 && 2872 nvp->v_type == VDIR && 2873 vp->v_mount != nvp->v_mount) { 2874 /* 2875 * For a ZFS snapshot, there is a 2876 * pseudo mount that does not set 2877 * v_mountedhere, so it needs to 2878 * be detected via a different 2879 * mount structure. 2880 */ 2881 at_root = 1; 2882 if (new_mp == mp) 2883 new_mp = nvp->v_mount; 2884 } 2885 } 2886 2887 /* 2888 * If we failed to get attributes of the entry, 2889 * then just skip it for NFSv3 (the traditional 2890 * behavior in the old NFS server). 2891 * For NFSv4 the behavior is controlled by 2892 * RDATTRERROR: we either ignore the error or 2893 * fail the request. 2894 * The exception is EOPNOTSUPP, which can be 2895 * returned by nfsvno_getfh() for certain 2896 * file systems, such as devfs. This indicates 2897 * that the file system cannot be exported, 2898 * so just skip over the entry. 2899 * Note that RDATTRERROR is never set for NFSv3. 2900 */ 2901 if (r != 0) { 2902 if (!NFSISSET_ATTRBIT(&attrbits, 2903 NFSATTRBIT_RDATTRERROR) || 2904 r == EOPNOTSUPP) { 2905 vput(nvp); 2906 if (needs_unbusy != 0) 2907 vfs_unbusy(new_mp); 2908 if ((nd->nd_flag & ND_NFSV3) || 2909 r == EOPNOTSUPP) 2910 goto invalid; 2911 nd->nd_repstat = r; 2912 break; 2913 } 2914 } 2915 } else if (NFSNONZERO_ATTRBIT(&attrbits)) { 2916 /* Only need Type and/or Fileid. */ 2917 VATTR_NULL(&nvap->na_vattr); 2918 nvap->na_fileid = dp->d_fileno; 2919 nfs_dtypetovtype(nvap, vp, dp->d_type); 2920 } 2921 2922 /* 2923 * Build the directory record xdr 2924 */ 2925 if (nd->nd_flag & ND_NFSV3) { 2926 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2927 *tl++ = newnfs_true; 2928 txdr_hyper(dp->d_fileno, tl); 2929 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2930 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2931 txdr_hyper(*cookiep, tl); 2932 nfsrv_postopattr(nd, 0, nvap); 2933 dirlen += nfsm_fhtom(NULL, nd, (u_int8_t *)&nfh, 2934 0, 1); 2935 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 2936 if (nvp != NULL) 2937 vput(nvp); 2938 } else { 2939 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2940 *tl++ = newnfs_true; 2941 txdr_hyper(*cookiep, tl); 2942 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2943 xattrsupp = false; 2944 has_hiddensystem = false; 2945 has_namedattr = false; 2946 if (nvp != NULL) { 2947 supports_nfsv4acls = 2948 nfs_supportsnfsv4acls(nvp); 2949 if (NFSISSET_ATTRBIT(&attrbits, 2950 NFSATTRBIT_XATTRSUPPORT)) { 2951 ret = VOP_GETEXTATTR(nvp, 2952 EXTATTR_NAMESPACE_USER, 2953 "xxx", NULL, &atsiz, 2954 nd->nd_cred, p); 2955 xattrsupp = ret != EOPNOTSUPP; 2956 } 2957 if (VOP_PATHCONF(nvp, 2958 _PC_HAS_HIDDENSYSTEM, &pathval) != 2959 0) 2960 pathval = 0; 2961 has_hiddensystem = pathval > 0; 2962 pathval = 0; 2963 if (NFSISSET_ATTRBIT(&attrbits, 2964 NFSATTRBIT_NAMEDATTR) && 2965 VOP_PATHCONF(nvp, _PC_HAS_NAMEDATTR, 2966 &pathval) != 0) 2967 pathval = 0; 2968 has_namedattr = pathval > 0; 2969 NFSVOPUNLOCK(nvp); 2970 } else 2971 supports_nfsv4acls = 0; 2972 if (refp != NULL) { 2973 dirlen += nfsrv_putreferralattr(nd, 2974 &refbits, refp, 0, 2975 &nd->nd_repstat); 2976 if (nd->nd_repstat) { 2977 if (nvp != NULL) 2978 vrele(nvp); 2979 if (needs_unbusy != 0) 2980 vfs_unbusy(new_mp); 2981 break; 2982 } 2983 } else if (r) { 2984 dirlen += nfsvno_fillattr(nd, new_mp, 2985 nvp, nvap, &nfh, r, &rderrbits, 2986 nd->nd_cred, p, isdgram, 0, 2987 supports_nfsv4acls, at_root, 2988 mounted_on_fileno, xattrsupp, 2989 has_hiddensystem, has_namedattr); 2990 } else { 2991 dirlen += nfsvno_fillattr(nd, new_mp, 2992 nvp, nvap, &nfh, r, &attrbits, 2993 nd->nd_cred, p, isdgram, 0, 2994 supports_nfsv4acls, at_root, 2995 mounted_on_fileno, xattrsupp, 2996 has_hiddensystem, has_namedattr); 2997 } 2998 if (nvp != NULL) 2999 vrele(nvp); 3000 dirlen += (3 * NFSX_UNSIGNED); 3001 } 3002 if (needs_unbusy != 0) 3003 vfs_unbusy(new_mp); 3004 if (dirlen <= cnt) 3005 entrycnt++; 3006 } 3007 invalid: 3008 cpos += dp->d_reclen; 3009 dp = (struct dirent *)cpos; 3010 cookiep++; 3011 ncookies--; 3012 } 3013 vrele(vp); 3014 vfs_unbusy(mp); 3015 3016 /* 3017 * If dirlen > cnt, we must strip off the last entry. If that 3018 * results in an empty reply, report NFSERR_TOOSMALL. 3019 */ 3020 if (dirlen > cnt || nd->nd_repstat) { 3021 if (!nd->nd_repstat && entrycnt == 0) 3022 nd->nd_repstat = NFSERR_TOOSMALL; 3023 if (nd->nd_repstat) { 3024 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0); 3025 if (nd->nd_flag & ND_NFSV3) 3026 nfsrv_postopattr(nd, getret, &at); 3027 } else 3028 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1); 3029 eofflag = 0; 3030 } else if (cpos < cend) 3031 eofflag = 0; 3032 if (!nd->nd_repstat) { 3033 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3034 *tl++ = newnfs_false; 3035 if (eofflag) 3036 *tl = newnfs_true; 3037 else 3038 *tl = newnfs_false; 3039 } 3040 free(cookies, M_TEMP); 3041 free(rbuf, M_TEMP); 3042 3043 out: 3044 NFSEXITCODE2(0, nd); 3045 return (0); 3046 nfsmout: 3047 vput(vp); 3048 NFSEXITCODE2(error, nd); 3049 return (error); 3050 } 3051 3052 /* 3053 * Get the settable attributes out of the mbuf list. 3054 * (Return 0 or EBADRPC) 3055 */ 3056 int 3057 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap, 3058 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 3059 { 3060 u_int32_t *tl; 3061 struct nfsv2_sattr *sp; 3062 int error = 0, toclient = 0; 3063 3064 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 3065 case ND_NFSV2: 3066 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 3067 /* 3068 * Some old clients didn't fill in the high order 16bits. 3069 * --> check the low order 2 bytes for 0xffff 3070 */ 3071 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 3072 nvap->na_mode = nfstov_mode(sp->sa_mode); 3073 if (sp->sa_uid != newnfs_xdrneg1) 3074 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 3075 if (sp->sa_gid != newnfs_xdrneg1) 3076 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 3077 if (sp->sa_size != newnfs_xdrneg1) 3078 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 3079 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 3080 #ifdef notyet 3081 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 3082 #else 3083 nvap->na_atime.tv_sec = 3084 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 3085 nvap->na_atime.tv_nsec = 0; 3086 #endif 3087 } 3088 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 3089 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 3090 break; 3091 case ND_NFSV3: 3092 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3093 if (*tl == newnfs_true) { 3094 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3095 nvap->na_mode = nfstov_mode(*tl); 3096 } 3097 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3098 if (*tl == newnfs_true) { 3099 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3100 nvap->na_uid = fxdr_unsigned(uid_t, *tl); 3101 } 3102 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3103 if (*tl == newnfs_true) { 3104 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3105 nvap->na_gid = fxdr_unsigned(gid_t, *tl); 3106 } 3107 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3108 if (*tl == newnfs_true) { 3109 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3110 nvap->na_size = fxdr_hyper(tl); 3111 } 3112 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3113 switch (fxdr_unsigned(int, *tl)) { 3114 case NFSV3SATTRTIME_TOCLIENT: 3115 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3116 fxdr_nfsv3time(tl, &nvap->na_atime); 3117 toclient = 1; 3118 break; 3119 case NFSV3SATTRTIME_TOSERVER: 3120 vfs_timestamp(&nvap->na_atime); 3121 nvap->na_vaflags |= VA_UTIMES_NULL; 3122 break; 3123 } 3124 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3125 switch (fxdr_unsigned(int, *tl)) { 3126 case NFSV3SATTRTIME_TOCLIENT: 3127 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 3128 fxdr_nfsv3time(tl, &nvap->na_mtime); 3129 nvap->na_vaflags &= ~VA_UTIMES_NULL; 3130 break; 3131 case NFSV3SATTRTIME_TOSERVER: 3132 vfs_timestamp(&nvap->na_mtime); 3133 if (!toclient) 3134 nvap->na_vaflags |= VA_UTIMES_NULL; 3135 break; 3136 } 3137 break; 3138 case ND_NFSV4: 3139 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p); 3140 } 3141 nfsmout: 3142 NFSEXITCODE2(error, nd); 3143 return (error); 3144 } 3145 3146 /* 3147 * Handle the setable attributes for V4. 3148 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 3149 */ 3150 int 3151 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap, 3152 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 3153 { 3154 u_int32_t *tl; 3155 int attrsum = 0; 3156 int i, j; 3157 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 3158 int moderet, toclient = 0; 3159 u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 3160 uid_t uid; 3161 gid_t gid; 3162 u_short mode, mask; /* Same type as va_mode. */ 3163 struct vattr va; 3164 3165 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 3166 if (error) 3167 goto nfsmout; 3168 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3169 attrsize = fxdr_unsigned(int, *tl); 3170 3171 /* 3172 * Loop around getting the setable attributes. If an unsupported 3173 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 3174 * Once nd_repstat != 0, do not set the attribute value, but keep 3175 * parsing the attribute(s). 3176 */ 3177 if (retnotsup) { 3178 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3179 bitpos = NFSATTRBIT_MAX; 3180 } else { 3181 bitpos = 0; 3182 if (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_HIDDEN) || 3183 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SYSTEM)) 3184 nvap->na_flags = 0; 3185 } 3186 moderet = 0; 3187 for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 3188 if (attrsum > attrsize) { 3189 error = NFSERR_BADXDR; 3190 goto nfsmout; 3191 } 3192 if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 3193 switch (bitpos) { 3194 case NFSATTRBIT_SIZE: 3195 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 3196 if (!nd->nd_repstat) { 3197 if (vp != NULL && vp->v_type != VREG) 3198 nd->nd_repstat = (vp->v_type == VDIR) ? 3199 NFSERR_ISDIR : NFSERR_INVAL; 3200 else 3201 nvap->na_size = fxdr_hyper(tl); 3202 } 3203 attrsum += NFSX_HYPER; 3204 break; 3205 case NFSATTRBIT_ACL: 3206 error = nfsrv_dissectacl(nd, aclp, true, &aceerr, 3207 &aclsize, p); 3208 if (error) 3209 goto nfsmout; 3210 if (aceerr && !nd->nd_repstat) 3211 nd->nd_repstat = aceerr; 3212 attrsum += aclsize; 3213 break; 3214 case NFSATTRBIT_ARCHIVE: 3215 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3216 if (!nd->nd_repstat) 3217 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3218 attrsum += NFSX_UNSIGNED; 3219 break; 3220 case NFSATTRBIT_HIDDEN: 3221 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 3222 if (nd->nd_repstat == 0) { 3223 if (*tl == newnfs_true) 3224 nvap->na_flags |= UF_HIDDEN; 3225 } 3226 attrsum += NFSX_UNSIGNED; 3227 break; 3228 case NFSATTRBIT_MIMETYPE: 3229 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3230 i = fxdr_unsigned(int, *tl); 3231 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 3232 if (error) 3233 goto nfsmout; 3234 if (!nd->nd_repstat) 3235 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3236 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 3237 break; 3238 case NFSATTRBIT_MODE: 3239 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */ 3240 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3241 if (!nd->nd_repstat) 3242 nvap->na_mode = nfstov_mode(*tl); 3243 attrsum += NFSX_UNSIGNED; 3244 break; 3245 case NFSATTRBIT_OWNER: 3246 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3247 j = fxdr_unsigned(int, *tl); 3248 if (j < 0) { 3249 error = NFSERR_BADXDR; 3250 goto nfsmout; 3251 } 3252 if (j > NFSV4_SMALLSTR) 3253 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 3254 else 3255 cp = namestr; 3256 error = nfsrv_mtostr(nd, cp, j); 3257 if (error) { 3258 if (j > NFSV4_SMALLSTR) 3259 free(cp, M_NFSSTRING); 3260 goto nfsmout; 3261 } 3262 if (!nd->nd_repstat) { 3263 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, 3264 &uid); 3265 if (!nd->nd_repstat) 3266 nvap->na_uid = uid; 3267 } 3268 if (j > NFSV4_SMALLSTR) 3269 free(cp, M_NFSSTRING); 3270 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 3271 break; 3272 case NFSATTRBIT_OWNERGROUP: 3273 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3274 j = fxdr_unsigned(int, *tl); 3275 if (j < 0) { 3276 error = NFSERR_BADXDR; 3277 goto nfsmout; 3278 } 3279 if (j > NFSV4_SMALLSTR) 3280 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 3281 else 3282 cp = namestr; 3283 error = nfsrv_mtostr(nd, cp, j); 3284 if (error) { 3285 if (j > NFSV4_SMALLSTR) 3286 free(cp, M_NFSSTRING); 3287 goto nfsmout; 3288 } 3289 if (!nd->nd_repstat) { 3290 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, 3291 &gid); 3292 if (!nd->nd_repstat) 3293 nvap->na_gid = gid; 3294 } 3295 if (j > NFSV4_SMALLSTR) 3296 free(cp, M_NFSSTRING); 3297 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 3298 break; 3299 case NFSATTRBIT_SYSTEM: 3300 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED); 3301 if (nd->nd_repstat == 0) { 3302 if (*tl == newnfs_true) 3303 nvap->na_flags |= UF_SYSTEM; 3304 } 3305 attrsum += NFSX_UNSIGNED; 3306 break; 3307 case NFSATTRBIT_TIMEACCESSSET: 3308 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3309 attrsum += NFSX_UNSIGNED; 3310 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 3311 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 3312 if (!nd->nd_repstat) 3313 fxdr_nfsv4time(tl, &nvap->na_atime); 3314 toclient = 1; 3315 attrsum += NFSX_V4TIME; 3316 } else if (!nd->nd_repstat) { 3317 vfs_timestamp(&nvap->na_atime); 3318 nvap->na_vaflags |= VA_UTIMES_NULL; 3319 } 3320 break; 3321 case NFSATTRBIT_TIMEBACKUP: 3322 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 3323 if (!nd->nd_repstat) 3324 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3325 attrsum += NFSX_V4TIME; 3326 break; 3327 case NFSATTRBIT_TIMECREATE: 3328 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 3329 if (!nd->nd_repstat) 3330 fxdr_nfsv4time(tl, &nvap->na_btime); 3331 attrsum += NFSX_V4TIME; 3332 break; 3333 case NFSATTRBIT_TIMEMODIFYSET: 3334 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 3335 attrsum += NFSX_UNSIGNED; 3336 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 3337 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 3338 if (!nd->nd_repstat) 3339 fxdr_nfsv4time(tl, &nvap->na_mtime); 3340 nvap->na_vaflags &= ~VA_UTIMES_NULL; 3341 attrsum += NFSX_V4TIME; 3342 } else if (!nd->nd_repstat) { 3343 vfs_timestamp(&nvap->na_mtime); 3344 if (!toclient) 3345 nvap->na_vaflags |= VA_UTIMES_NULL; 3346 } 3347 break; 3348 case NFSATTRBIT_MODESETMASKED: 3349 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 3350 mode = fxdr_unsigned(u_short, *tl++); 3351 mask = fxdr_unsigned(u_short, *tl); 3352 /* 3353 * vp == NULL implies an Open/Create operation. 3354 * This attribute can only be used for Setattr and 3355 * only for NFSv4.1 or higher. 3356 * If moderet != 0, a mode attribute has also been 3357 * specified and this attribute cannot be done in the 3358 * same Setattr operation. 3359 */ 3360 if (!nd->nd_repstat) { 3361 if ((nd->nd_flag & ND_NFSV41) == 0) 3362 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3363 else if ((mode & ~07777) != 0 || 3364 (mask & ~07777) != 0 || vp == NULL) 3365 nd->nd_repstat = NFSERR_INVAL; 3366 else if (moderet == 0) 3367 moderet = VOP_GETATTR(vp, &va, 3368 nd->nd_cred); 3369 if (moderet == 0) 3370 nvap->na_mode = (mode & mask) | 3371 (va.va_mode & ~mask); 3372 else 3373 nd->nd_repstat = moderet; 3374 } 3375 attrsum += 2 * NFSX_UNSIGNED; 3376 break; 3377 case NFSATTRBIT_MODEUMASK: 3378 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 3379 mode = fxdr_unsigned(u_short, *tl++); 3380 mask = fxdr_unsigned(u_short, *tl); 3381 /* 3382 * If moderet != 0, mode has already been done. 3383 * If vp != NULL, this is not a file object creation. 3384 */ 3385 if (!nd->nd_repstat) { 3386 if ((nd->nd_flag & ND_NFSV42) == 0) 3387 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3388 else if ((mask & ~0777) != 0 || vp != NULL || 3389 moderet != 0) 3390 nd->nd_repstat = NFSERR_INVAL; 3391 else 3392 nvap->na_mode = (mode & ~mask); 3393 } 3394 attrsum += 2 * NFSX_UNSIGNED; 3395 break; 3396 default: 3397 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 3398 /* 3399 * set bitpos so we drop out of the loop. 3400 */ 3401 bitpos = NFSATTRBIT_MAX; 3402 break; 3403 } 3404 } 3405 3406 /* 3407 * some clients pad the attrlist, so we need to skip over the 3408 * padding. This also skips over unparsed non-supported attributes. 3409 */ 3410 if (attrsum > attrsize) { 3411 error = NFSERR_BADXDR; 3412 } else { 3413 attrsize = NFSM_RNDUP(attrsize); 3414 if (attrsum < attrsize) 3415 error = nfsm_advance(nd, attrsize - attrsum, -1); 3416 } 3417 nfsmout: 3418 NFSEXITCODE2(error, nd); 3419 return (error); 3420 } 3421 3422 /* 3423 * Check/setup export credentials. 3424 */ 3425 int 3426 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 3427 struct ucred *credanon, bool testsec) 3428 { 3429 int error; 3430 3431 /* 3432 * Check/setup credentials. 3433 */ 3434 if (nd->nd_flag & ND_GSS) 3435 exp->nes_exflag &= ~MNT_EXPORTANON; 3436 3437 /* 3438 * Check to see if the operation is allowed for this security flavor. 3439 */ 3440 error = 0; 3441 if (testsec) { 3442 error = nfsvno_testexp(nd, exp); 3443 if (error != 0) 3444 goto out; 3445 } 3446 3447 /* 3448 * Check to see if the file system is exported V4 only. 3449 */ 3450 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) { 3451 error = NFSERR_PROGNOTV4; 3452 goto out; 3453 } 3454 3455 /* 3456 * Now, map the user credentials. 3457 * (Note that ND_AUTHNONE will only be set for an NFSv3 3458 * Fsinfo RPC. If set for anything else, this code might need 3459 * to change.) 3460 */ 3461 if (NFSVNO_EXPORTED(exp)) { 3462 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) || 3463 NFSVNO_EXPORTANON(exp) || 3464 (nd->nd_flag & ND_AUTHNONE) != 0) { 3465 nd->nd_cred->cr_uid = credanon->cr_uid; 3466 /* 3467 * 'credanon' is already a 'struct ucred' that was built 3468 * internally with calls to crsetgroups_fallback(), so 3469 * we don't need a fallback here. 3470 */ 3471 crsetgroups(nd->nd_cred, credanon->cr_ngroups, 3472 credanon->cr_groups); 3473 } else if ((nd->nd_flag & ND_GSS) == 0) { 3474 /* 3475 * If using AUTH_SYS, call nfsrv_getgrpscred() to see 3476 * if there is a replacement credential with a group 3477 * list set up by "nfsuserd -manage-gids". 3478 * If there is no replacement, nfsrv_getgrpscred() 3479 * simply returns its argument. 3480 */ 3481 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred); 3482 } 3483 } 3484 3485 out: 3486 NFSEXITCODE2(error, nd); 3487 return (error); 3488 } 3489 3490 /* 3491 * Check exports. 3492 */ 3493 int 3494 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 3495 struct ucred **credp) 3496 { 3497 int error; 3498 3499 error = 0; 3500 *credp = NULL; 3501 MNT_ILOCK(mp); 3502 if (mp->mnt_exjail == NULL || 3503 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison) 3504 error = EACCES; 3505 MNT_IUNLOCK(mp); 3506 if (error == 0) 3507 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 3508 &exp->nes_numsecflavor, exp->nes_secflavors); 3509 if (error) { 3510 if (NFSD_VNET(nfs_rootfhset)) { 3511 exp->nes_exflag = 0; 3512 exp->nes_numsecflavor = 0; 3513 error = 0; 3514 } 3515 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor > 3516 MAXSECFLAVORS) { 3517 printf("nfsvno_checkexp: numsecflavors out of range\n"); 3518 exp->nes_numsecflavor = 0; 3519 error = EACCES; 3520 } 3521 NFSEXITCODE(error); 3522 return (error); 3523 } 3524 3525 /* 3526 * Get a vnode for a file handle and export stuff. 3527 */ 3528 int 3529 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 3530 int lktype, struct vnode **vpp, struct nfsexstuff *exp, 3531 struct ucred **credp) 3532 { 3533 int error; 3534 3535 *credp = NULL; 3536 exp->nes_numsecflavor = 0; 3537 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp); 3538 if (error != 0) 3539 /* Make sure the server replies ESTALE to the client. */ 3540 error = ESTALE; 3541 if (nam && !error) { 3542 MNT_ILOCK(mp); 3543 if (mp->mnt_exjail == NULL || 3544 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison) 3545 error = EACCES; 3546 MNT_IUNLOCK(mp); 3547 if (error == 0) 3548 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 3549 &exp->nes_numsecflavor, exp->nes_secflavors); 3550 if (error) { 3551 if (NFSD_VNET(nfs_rootfhset)) { 3552 exp->nes_exflag = 0; 3553 exp->nes_numsecflavor = 0; 3554 error = 0; 3555 } else { 3556 vput(*vpp); 3557 } 3558 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor > 3559 MAXSECFLAVORS) { 3560 printf("nfsvno_fhtovp: numsecflavors out of range\n"); 3561 exp->nes_numsecflavor = 0; 3562 error = EACCES; 3563 vput(*vpp); 3564 } 3565 } 3566 NFSEXITCODE(error); 3567 return (error); 3568 } 3569 3570 /* 3571 * nfsd_fhtovp() - convert a fh to a vnode ptr 3572 * - look up fsid in mount list (if not found ret error) 3573 * - get vp and export rights by calling nfsvno_fhtovp() 3574 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 3575 * for AUTH_SYS 3576 * - if mpp != NULL, return the mount point so that it can 3577 * be used for vn_finished_write() by the caller 3578 */ 3579 void 3580 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 3581 struct vnode **vpp, struct nfsexstuff *exp, 3582 struct mount **mpp, int startwrite, int nextop) 3583 { 3584 struct mount *mp, *mpw; 3585 struct ucred *credanon; 3586 fhandle_t *fhp; 3587 int error; 3588 3589 if (mpp != NULL) 3590 *mpp = NULL; 3591 *vpp = NULL; 3592 fhp = (fhandle_t *)nfp->nfsrvfh_data; 3593 mp = vfs_busyfs(&fhp->fh_fsid); 3594 if (mp == NULL) { 3595 nd->nd_repstat = ESTALE; 3596 goto out; 3597 } 3598 3599 if (startwrite) { 3600 mpw = mp; 3601 error = vn_start_write(NULL, &mpw, V_WAIT); 3602 if (error != 0) { 3603 mpw = NULL; 3604 vfs_unbusy(mp); 3605 nd->nd_repstat = ESTALE; 3606 goto out; 3607 } 3608 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp))) 3609 lktype = LK_EXCLUSIVE; 3610 } else 3611 mpw = NULL; 3612 3613 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 3614 &credanon); 3615 vfs_unbusy(mp); 3616 3617 if (nd->nd_repstat == 0 && 3618 nfp->nfsrvfh_len >= NFSX_MYFH + NFSX_V4NAMEDDIRFH && 3619 nfp->nfsrvfh_len <= NFSX_MYFH + NFSX_V4NAMEDATTRFH) { 3620 if (nfp->nfsrvfh_len == NFSX_MYFH + NFSX_V4NAMEDDIRFH) 3621 vn_irflag_set_cond(*vpp, VIRF_NAMEDDIR); 3622 else 3623 vn_irflag_set_cond(*vpp, VIRF_NAMEDATTR); 3624 } 3625 3626 /* 3627 * For NFSv4 without a pseudo root fs, unexported file handles 3628 * can be returned, so that Lookup works everywhere. 3629 */ 3630 if (!nd->nd_repstat && exp->nes_exflag == 0 && 3631 !(nd->nd_flag & ND_NFSV4)) { 3632 vput(*vpp); 3633 *vpp = NULL; 3634 nd->nd_repstat = EACCES; 3635 } 3636 3637 /* 3638 * Personally, I've never seen any point in requiring a 3639 * reserved port#, since only in the rare case where the 3640 * clients are all boxes with secure system privileges, 3641 * does it provide any enhanced security, but... some people 3642 * believe it to be useful and keep putting this code back in. 3643 * (There is also some "security checker" out there that 3644 * complains if the nfs server doesn't enforce this.) 3645 * However, note the following: 3646 * RFC3530 (NFSv4) specifies that a reserved port# not be 3647 * required. 3648 * RFC2623 recommends that, if a reserved port# is checked for, 3649 * that there be a way to turn that off--> ifdef'd. 3650 */ 3651 #ifdef NFS_REQRSVPORT 3652 if (!nd->nd_repstat) { 3653 struct sockaddr_in *saddr; 3654 struct sockaddr_in6 *saddr6; 3655 3656 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 3657 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 3658 if (!(nd->nd_flag & ND_NFSV4) && 3659 ((saddr->sin_family == AF_INET && 3660 ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 3661 (saddr6->sin6_family == AF_INET6 && 3662 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 3663 vput(*vpp); 3664 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 3665 } 3666 } 3667 #endif /* NFS_REQRSVPORT */ 3668 3669 /* 3670 * Check/setup credentials. 3671 */ 3672 if (!nd->nd_repstat) { 3673 nd->nd_saveduid = nd->nd_cred->cr_uid; 3674 nd->nd_repstat = nfsd_excred(nd, exp, credanon, 3675 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type)); 3676 if (nd->nd_repstat) 3677 vput(*vpp); 3678 } 3679 if (credanon != NULL) 3680 crfree(credanon); 3681 if (nd->nd_repstat) { 3682 vn_finished_write(mpw); 3683 *vpp = NULL; 3684 } else if (mpp != NULL) { 3685 *mpp = mpw; 3686 } 3687 3688 out: 3689 NFSEXITCODE2(0, nd); 3690 } 3691 3692 /* 3693 * glue for fp. 3694 */ 3695 static int 3696 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 3697 { 3698 struct filedesc *fdp; 3699 struct file *fp; 3700 int error = 0; 3701 3702 fdp = p->td_proc->p_fd; 3703 if (fd < 0 || fd >= fdp->fd_nfiles || 3704 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) { 3705 error = EBADF; 3706 goto out; 3707 } 3708 *fpp = fp; 3709 3710 out: 3711 NFSEXITCODE(error); 3712 return (error); 3713 } 3714 3715 /* 3716 * Called from nfssvc() to update the exports list. Just call 3717 * vfs_export(). This has to be done, since the v4 root fake fs isn't 3718 * in the mount list. 3719 */ 3720 int 3721 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 3722 { 3723 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 3724 int error = 0; 3725 struct nameidata nd; 3726 fhandle_t fh; 3727 3728 error = vfs_export(NFSD_VNET(nfsv4root_mnt), &nfsexargp->export, false); 3729 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 3730 NFSD_VNET(nfs_rootfhset) = 0; 3731 else if (error == 0) { 3732 if (nfsexargp->fspec == NULL) { 3733 error = EPERM; 3734 goto out; 3735 } 3736 /* 3737 * If fspec != NULL, this is the v4root path. 3738 */ 3739 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec); 3740 if ((error = namei(&nd)) != 0) 3741 goto out; 3742 error = nfsvno_getfh(nd.ni_vp, &fh, p); 3743 vrele(nd.ni_vp); 3744 if (!error) { 3745 NFSD_VNET(nfs_rootfh).nfsrvfh_len = NFSX_MYFH; 3746 NFSBCOPY((caddr_t)&fh, 3747 NFSD_VNET(nfs_rootfh).nfsrvfh_data, 3748 sizeof (fhandle_t)); 3749 NFSD_VNET(nfs_rootfhset) = 1; 3750 } 3751 } 3752 3753 out: 3754 NFSEXITCODE(error); 3755 return (error); 3756 } 3757 3758 /* 3759 * This function needs to test to see if the system is near its limit 3760 * for memory allocation via malloc() or mget() and return True iff 3761 * either of these resources are near their limit. 3762 * XXX (For now, this is just a stub.) 3763 */ 3764 int nfsrv_testmalloclimit = 0; 3765 int 3766 nfsrv_mallocmget_limit(void) 3767 { 3768 static int printmesg = 0; 3769 static int testval = 1; 3770 3771 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 3772 if ((printmesg++ % 100) == 0) 3773 printf("nfsd: malloc/mget near limit\n"); 3774 return (1); 3775 } 3776 return (0); 3777 } 3778 3779 /* 3780 * BSD specific initialization of a mount point. 3781 */ 3782 void 3783 nfsd_mntinit(void) 3784 { 3785 3786 NFSD_LOCK(); 3787 if (NFSD_VNET(nfsrv_mntinited)) { 3788 NFSD_UNLOCK(); 3789 return; 3790 } 3791 NFSD_VNET(nfsrv_mntinited) = true; 3792 nfsrvd_init(0); 3793 NFSD_UNLOCK(); 3794 3795 NFSD_VNET(nfsv4root_mnt) = malloc(sizeof(struct mount), M_TEMP, 3796 M_WAITOK | M_ZERO); 3797 NFSD_VNET(nfsv4root_mnt)->mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 3798 mtx_init(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx, "nfs4mnt", NULL, MTX_DEF); 3799 lockinit(&NFSD_VNET(nfsv4root_mnt)->mnt_explock, PVFS, "explock", 0, 0); 3800 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelist); 3801 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelist); 3802 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL; 3803 TAILQ_INIT(&NFSD_VNET(nfsv4root_opt)); 3804 TAILQ_INIT(&NFSD_VNET(nfsv4root_newopt)); 3805 NFSD_VNET(nfsv4root_mnt)->mnt_opt = &NFSD_VNET(nfsv4root_opt); 3806 NFSD_VNET(nfsv4root_mnt)->mnt_optnew = &NFSD_VNET(nfsv4root_newopt); 3807 NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelistsize = 0; 3808 NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelistsize = 0; 3809 callout_init(&NFSD_VNET(nfsd_callout), 1); 3810 3811 nfsrvd_initcache(); 3812 nfsd_init(); 3813 } 3814 3815 static void 3816 nfsd_timer(void *arg) 3817 { 3818 struct vnet *vnetp; 3819 3820 vnetp = (struct vnet *)arg; 3821 NFSD_CURVNET_SET_QUIET(vnetp); 3822 nfsrv_servertimer(vnetp); 3823 callout_reset_sbt(&NFSD_VNET(nfsd_callout), SBT_1S, SBT_1S, nfsd_timer, 3824 arg, 0); 3825 NFSD_CURVNET_RESTORE(); 3826 } 3827 3828 /* 3829 * Get a vnode for a file handle, without checking exports, etc. 3830 */ 3831 struct vnode * 3832 nfsvno_getvp(fhandle_t *fhp) 3833 { 3834 struct mount *mp; 3835 struct vnode *vp; 3836 int error; 3837 3838 mp = vfs_busyfs(&fhp->fh_fsid); 3839 if (mp == NULL) 3840 return (NULL); 3841 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 3842 vfs_unbusy(mp); 3843 if (error) 3844 return (NULL); 3845 return (vp); 3846 } 3847 3848 /* 3849 * Do a local VOP_ADVLOCK(). 3850 */ 3851 int 3852 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 3853 u_int64_t end, struct thread *td) 3854 { 3855 int error = 0; 3856 struct flock fl; 3857 u_int64_t tlen; 3858 3859 if (nfsrv_dolocallocks == 0) 3860 goto out; 3861 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked"); 3862 3863 fl.l_whence = SEEK_SET; 3864 fl.l_type = ftype; 3865 fl.l_start = (off_t)first; 3866 if (end == NFS64BITSSET) { 3867 fl.l_len = 0; 3868 } else { 3869 tlen = end - first; 3870 fl.l_len = (off_t)tlen; 3871 } 3872 /* 3873 * For FreeBSD8, the l_pid and l_sysid must be set to the same 3874 * values for all calls, so that all locks will be held by the 3875 * nfsd server. (The nfsd server handles conflicts between the 3876 * various clients.) 3877 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 3878 * bytes, so it can't be put in l_sysid. 3879 */ 3880 if (nfsv4_sysid == 0) 3881 nfsv4_sysid = nlm_acquire_next_sysid(); 3882 fl.l_pid = (pid_t)0; 3883 fl.l_sysid = (int)nfsv4_sysid; 3884 3885 if (ftype == F_UNLCK) 3886 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 3887 (F_POSIX | F_REMOTE)); 3888 else 3889 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 3890 (F_POSIX | F_REMOTE)); 3891 3892 out: 3893 NFSEXITCODE(error); 3894 return (error); 3895 } 3896 3897 /* 3898 * Check the nfsv4 root exports. 3899 */ 3900 int 3901 nfsvno_v4rootexport(struct nfsrv_descript *nd) 3902 { 3903 struct ucred *credanon; 3904 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i; 3905 uint64_t exflags; 3906 3907 error = vfs_stdcheckexp(NFSD_VNET(nfsv4root_mnt), nd->nd_nam, &exflags, 3908 &credanon, &numsecflavor, secflavors); 3909 if (error) { 3910 error = NFSERR_PROGUNAVAIL; 3911 goto out; 3912 } 3913 if (credanon != NULL) 3914 crfree(credanon); 3915 for (i = 0; i < numsecflavor; i++) { 3916 if (secflavors[i] == AUTH_SYS) 3917 nd->nd_flag |= ND_EXAUTHSYS; 3918 else if (secflavors[i] == RPCSEC_GSS_KRB5) 3919 nd->nd_flag |= ND_EXGSS; 3920 else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3921 nd->nd_flag |= ND_EXGSSINTEGRITY; 3922 else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3923 nd->nd_flag |= ND_EXGSSPRIVACY; 3924 } 3925 3926 /* And set ND_EXxx flags for TLS. */ 3927 if ((exflags & MNT_EXTLS) != 0) { 3928 nd->nd_flag |= ND_EXTLS; 3929 if ((exflags & MNT_EXTLSCERT) != 0) 3930 nd->nd_flag |= ND_EXTLSCERT; 3931 if ((exflags & MNT_EXTLSCERTUSER) != 0) 3932 nd->nd_flag |= ND_EXTLSCERTUSER; 3933 } 3934 3935 out: 3936 NFSEXITCODE(error); 3937 return (error); 3938 } 3939 3940 /* 3941 * Nfs server pseudo system call for the nfsd's 3942 */ 3943 /* 3944 * MPSAFE 3945 */ 3946 static int 3947 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3948 { 3949 struct file *fp; 3950 struct nfsd_addsock_args sockarg; 3951 struct nfsd_nfsd_args nfsdarg; 3952 struct nfsd_nfsd_oargs onfsdarg; 3953 struct nfsd_pnfsd_args pnfsdarg; 3954 struct vnode *vp, *nvp, *curdvp; 3955 struct pnfsdsfile *pf; 3956 struct nfsdevice *ds, *fds; 3957 cap_rights_t rights; 3958 int buflen, error, ret; 3959 char *buf, *cp, *cp2, *cp3; 3960 char fname[PNFS_FILENAME_LEN + 1]; 3961 3962 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td)); 3963 if (uap->flag & NFSSVC_NFSDADDSOCK) { 3964 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3965 if (error) 3966 goto out; 3967 /* 3968 * Since we don't know what rights might be required, 3969 * pretend that we need them all. It is better to be too 3970 * careful than too reckless. 3971 */ 3972 error = fget(td, sockarg.sock, 3973 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp); 3974 if (error != 0) 3975 goto out; 3976 if (fp->f_type != DTYPE_SOCKET) { 3977 fdrop(fp, td); 3978 error = EPERM; 3979 goto out; 3980 } 3981 error = nfsrvd_addsock(fp); 3982 fdrop(fp, td); 3983 } else if (uap->flag & NFSSVC_NFSDNFSD) { 3984 if (uap->argp == NULL) { 3985 error = EINVAL; 3986 goto out; 3987 } 3988 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) { 3989 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg)); 3990 if (error == 0) { 3991 nfsdarg.principal = onfsdarg.principal; 3992 nfsdarg.minthreads = onfsdarg.minthreads; 3993 nfsdarg.maxthreads = onfsdarg.maxthreads; 3994 nfsdarg.version = 1; 3995 nfsdarg.addr = NULL; 3996 nfsdarg.addrlen = 0; 3997 nfsdarg.dnshost = NULL; 3998 nfsdarg.dnshostlen = 0; 3999 nfsdarg.dspath = NULL; 4000 nfsdarg.dspathlen = 0; 4001 nfsdarg.mdspath = NULL; 4002 nfsdarg.mdspathlen = 0; 4003 nfsdarg.mirrorcnt = 1; 4004 } 4005 } else 4006 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg)); 4007 if (error) 4008 goto out; 4009 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 && 4010 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 && 4011 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 && 4012 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 && 4013 nfsdarg.mirrorcnt >= 1 && 4014 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS && 4015 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL && 4016 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) { 4017 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d" 4018 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen, 4019 nfsdarg.dspathlen, nfsdarg.dnshostlen, 4020 nfsdarg.mdspathlen, nfsdarg.mirrorcnt); 4021 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK); 4022 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen); 4023 if (error != 0) { 4024 free(cp, M_TEMP); 4025 goto out; 4026 } 4027 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */ 4028 nfsdarg.addr = cp; 4029 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK); 4030 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen); 4031 if (error != 0) { 4032 free(nfsdarg.addr, M_TEMP); 4033 free(cp, M_TEMP); 4034 goto out; 4035 } 4036 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */ 4037 nfsdarg.dnshost = cp; 4038 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK); 4039 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen); 4040 if (error != 0) { 4041 free(nfsdarg.addr, M_TEMP); 4042 free(nfsdarg.dnshost, M_TEMP); 4043 free(cp, M_TEMP); 4044 goto out; 4045 } 4046 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */ 4047 nfsdarg.dspath = cp; 4048 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK); 4049 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen); 4050 if (error != 0) { 4051 free(nfsdarg.addr, M_TEMP); 4052 free(nfsdarg.dnshost, M_TEMP); 4053 free(nfsdarg.dspath, M_TEMP); 4054 free(cp, M_TEMP); 4055 goto out; 4056 } 4057 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */ 4058 nfsdarg.mdspath = cp; 4059 } else { 4060 nfsdarg.addr = NULL; 4061 nfsdarg.addrlen = 0; 4062 nfsdarg.dnshost = NULL; 4063 nfsdarg.dnshostlen = 0; 4064 nfsdarg.dspath = NULL; 4065 nfsdarg.dspathlen = 0; 4066 nfsdarg.mdspath = NULL; 4067 nfsdarg.mdspathlen = 0; 4068 nfsdarg.mirrorcnt = 1; 4069 } 4070 nfsd_timer(NFSD_TD_TO_VNET(td)); 4071 error = nfsrvd_nfsd(td, &nfsdarg); 4072 callout_drain(&NFSD_VNET(nfsd_callout)); 4073 free(nfsdarg.addr, M_TEMP); 4074 free(nfsdarg.dnshost, M_TEMP); 4075 free(nfsdarg.dspath, M_TEMP); 4076 free(nfsdarg.mdspath, M_TEMP); 4077 } else if (uap->flag & NFSSVC_PNFSDS) { 4078 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg)); 4079 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER || 4080 pnfsdarg.op == PNFSDOP_FORCEDELDS)) { 4081 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4082 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1, 4083 NULL); 4084 if (error == 0) 4085 error = nfsrv_deldsserver(pnfsdarg.op, cp, td); 4086 free(cp, M_TEMP); 4087 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) { 4088 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4089 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS; 4090 buf = malloc(buflen, M_TEMP, M_WAITOK); 4091 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1, 4092 NULL); 4093 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error); 4094 if (error == 0 && pnfsdarg.dspath != NULL) { 4095 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4096 error = copyinstr(pnfsdarg.dspath, cp2, 4097 PATH_MAX + 1, NULL); 4098 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n", 4099 error); 4100 } else 4101 cp2 = NULL; 4102 if (error == 0 && pnfsdarg.curdspath != NULL) { 4103 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4104 error = copyinstr(pnfsdarg.curdspath, cp3, 4105 PATH_MAX + 1, NULL); 4106 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n", 4107 error); 4108 } else 4109 cp3 = NULL; 4110 curdvp = NULL; 4111 fds = NULL; 4112 if (error == 0) 4113 error = nfsrv_mdscopymr(cp, cp2, cp3, buf, 4114 &buflen, fname, td, &vp, &nvp, &pf, &ds, 4115 &fds); 4116 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error); 4117 if (error == 0) { 4118 if (pf->dsf_dir >= nfsrv_dsdirsize) { 4119 printf("copymr: dsdir out of range\n"); 4120 pf->dsf_dir = 0; 4121 } 4122 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen); 4123 error = nfsrv_copymr(vp, nvp, 4124 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf, 4125 (struct pnfsdsfile *)buf, 4126 buflen / sizeof(*pf), td->td_ucred, td); 4127 vput(vp); 4128 vput(nvp); 4129 if (fds != NULL && error == 0) { 4130 curdvp = fds->nfsdev_dsdir[pf->dsf_dir]; 4131 ret = vn_lock(curdvp, LK_EXCLUSIVE); 4132 if (ret == 0) { 4133 nfsrv_dsremove(curdvp, fname, 4134 td->td_ucred, td); 4135 NFSVOPUNLOCK(curdvp); 4136 } 4137 } 4138 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error); 4139 } 4140 free(cp, M_TEMP); 4141 free(cp2, M_TEMP); 4142 free(cp3, M_TEMP); 4143 free(buf, M_TEMP); 4144 } 4145 } else { 4146 error = nfssvc_srvcall(td, uap, td->td_ucred); 4147 } 4148 4149 out: 4150 NFSD_CURVNET_RESTORE(); 4151 NFSEXITCODE(error); 4152 return (error); 4153 } 4154 4155 static int 4156 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 4157 { 4158 struct nfsex_args export; 4159 struct nfsex_oldargs oexp; 4160 struct file *fp = NULL; 4161 int stablefd, i, len; 4162 struct nfsd_clid adminrevoke; 4163 struct nfsd_dumplist dumplist; 4164 struct nfsd_dumpclients *dumpclients; 4165 struct nfsd_dumplocklist dumplocklist; 4166 struct nfsd_dumplocks *dumplocks; 4167 struct nameidata nd; 4168 vnode_t vp; 4169 int error = EINVAL, igotlock; 4170 struct proc *procp; 4171 gid_t *grps; 4172 4173 if (uap->flag & NFSSVC_PUBLICFH) { 4174 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 4175 sizeof (fhandle_t)); 4176 error = copyin(uap->argp, 4177 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 4178 if (!error) 4179 nfs_pubfhset = 1; 4180 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) == 4181 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) { 4182 error = copyin(uap->argp,(caddr_t)&export, 4183 sizeof (struct nfsex_args)); 4184 if (!error) { 4185 grps = NULL; 4186 if (export.export.ex_ngroups > NGROUPS_MAX || 4187 export.export.ex_ngroups < 0) 4188 error = EINVAL; 4189 else if (export.export.ex_ngroups > 0) { 4190 grps = malloc(export.export.ex_ngroups * 4191 sizeof(gid_t), M_TEMP, M_WAITOK); 4192 error = copyin(export.export.ex_groups, grps, 4193 export.export.ex_ngroups * sizeof(gid_t)); 4194 export.export.ex_groups = grps; 4195 } else 4196 export.export.ex_groups = NULL; 4197 if (!error) 4198 error = nfsrv_v4rootexport(&export, cred, p); 4199 free(grps, M_TEMP); 4200 } 4201 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) == 4202 NFSSVC_V4ROOTEXPORT) { 4203 error = copyin(uap->argp,(caddr_t)&oexp, 4204 sizeof (struct nfsex_oldargs)); 4205 if (!error) { 4206 memset(&export.export, 0, sizeof(export.export)); 4207 export.export.ex_flags = (uint64_t)oexp.export.ex_flags; 4208 export.export.ex_root = oexp.export.ex_root; 4209 export.export.ex_uid = oexp.export.ex_anon.cr_uid; 4210 export.export.ex_ngroups = 4211 oexp.export.ex_anon.cr_ngroups; 4212 export.export.ex_groups = NULL; 4213 if (export.export.ex_ngroups > XU_NGROUPS || 4214 export.export.ex_ngroups < 0) 4215 error = EINVAL; 4216 else if (export.export.ex_ngroups > 0) { 4217 export.export.ex_groups = malloc( 4218 export.export.ex_ngroups * sizeof(gid_t), 4219 M_TEMP, M_WAITOK); 4220 for (i = 0; i < export.export.ex_ngroups; i++) 4221 export.export.ex_groups[i] = 4222 oexp.export.ex_anon.cr_groups[i]; 4223 } 4224 export.export.ex_addr = oexp.export.ex_addr; 4225 export.export.ex_addrlen = oexp.export.ex_addrlen; 4226 export.export.ex_mask = oexp.export.ex_mask; 4227 export.export.ex_masklen = oexp.export.ex_masklen; 4228 export.export.ex_indexfile = oexp.export.ex_indexfile; 4229 export.export.ex_numsecflavors = 4230 oexp.export.ex_numsecflavors; 4231 if (export.export.ex_numsecflavors >= MAXSECFLAVORS || 4232 export.export.ex_numsecflavors < 0) 4233 error = EINVAL; 4234 else { 4235 for (i = 0; i < export.export.ex_numsecflavors; 4236 i++) 4237 export.export.ex_secflavors[i] = 4238 oexp.export.ex_secflavors[i]; 4239 } 4240 export.fspec = oexp.fspec; 4241 if (error == 0) 4242 error = nfsrv_v4rootexport(&export, cred, p); 4243 free(export.export.ex_groups, M_TEMP); 4244 } 4245 } else if (uap->flag & NFSSVC_NOPUBLICFH) { 4246 nfs_pubfhset = 0; 4247 error = 0; 4248 } else if (uap->flag & NFSSVC_STABLERESTART) { 4249 error = copyin(uap->argp, (caddr_t)&stablefd, 4250 sizeof (int)); 4251 if (!error) 4252 error = fp_getfvp(p, stablefd, &fp, &vp); 4253 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 4254 error = EBADF; 4255 if (!error && NFSD_VNET(nfsrv_numnfsd) != 0) 4256 error = ENXIO; 4257 if (!error) { 4258 NFSD_VNET(nfsrv_stablefirst).nsf_fp = fp; 4259 nfsrv_setupstable(p); 4260 } 4261 } else if (uap->flag & NFSSVC_ADMINREVOKE) { 4262 error = copyin(uap->argp, (caddr_t)&adminrevoke, 4263 sizeof (struct nfsd_clid)); 4264 if (!error) 4265 error = nfsrv_adminrevoke(&adminrevoke, p); 4266 } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 4267 error = copyin(uap->argp, (caddr_t)&dumplist, 4268 sizeof (struct nfsd_dumplist)); 4269 if (!error && (dumplist.ndl_size < 1 || 4270 dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 4271 error = EPERM; 4272 if (!error) { 4273 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 4274 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 4275 nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 4276 error = copyout(dumpclients, dumplist.ndl_list, len); 4277 free(dumpclients, M_TEMP); 4278 } 4279 } else if (uap->flag & NFSSVC_DUMPLOCKS) { 4280 error = copyin(uap->argp, (caddr_t)&dumplocklist, 4281 sizeof (struct nfsd_dumplocklist)); 4282 if (!error && (dumplocklist.ndllck_size < 1 || 4283 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 4284 error = EPERM; 4285 if (!error) 4286 error = nfsrv_lookupfilename(&nd, 4287 dumplocklist.ndllck_fname, p); 4288 if (!error) { 4289 len = sizeof (struct nfsd_dumplocks) * 4290 dumplocklist.ndllck_size; 4291 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 4292 nfsrv_dumplocks(nd.ni_vp, dumplocks, 4293 dumplocklist.ndllck_size, p); 4294 vput(nd.ni_vp); 4295 error = copyout(dumplocks, dumplocklist.ndllck_list, 4296 len); 4297 free(dumplocks, M_TEMP); 4298 } 4299 } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 4300 procp = p->td_proc; 4301 PROC_LOCK(procp); 4302 nfsd_master_pid = procp->p_pid; 4303 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 4304 nfsd_master_start = procp->p_stats->p_start; 4305 NFSD_VNET(nfsd_master_proc) = procp; 4306 PROC_UNLOCK(procp); 4307 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) { 4308 NFSLOCKV4ROOTMUTEX(); 4309 if (!NFSD_VNET(nfsrv_suspend_nfsd)) { 4310 /* Lock out all nfsd threads */ 4311 do { 4312 igotlock = nfsv4_lock( 4313 &NFSD_VNET(nfsd_suspend_lock), 1, NULL, 4314 NFSV4ROOTLOCKMUTEXPTR, NULL); 4315 } while (igotlock == 0 && 4316 !NFSD_VNET(nfsrv_suspend_nfsd)); 4317 NFSD_VNET(nfsrv_suspend_nfsd) = true; 4318 } 4319 NFSUNLOCKV4ROOTMUTEX(); 4320 error = 0; 4321 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) { 4322 NFSLOCKV4ROOTMUTEX(); 4323 if (NFSD_VNET(nfsrv_suspend_nfsd)) { 4324 nfsv4_unlock(&NFSD_VNET(nfsd_suspend_lock), 0); 4325 NFSD_VNET(nfsrv_suspend_nfsd) = false; 4326 } 4327 NFSUNLOCKV4ROOTMUTEX(); 4328 error = 0; 4329 } 4330 4331 NFSEXITCODE(error); 4332 return (error); 4333 } 4334 4335 /* 4336 * Check exports. 4337 * Returns 0 if ok, 1 otherwise. 4338 */ 4339 int 4340 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 4341 { 4342 int i; 4343 4344 if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) || 4345 (NFSVNO_EXTLSCERT(exp) && 4346 (nd->nd_flag & ND_TLSCERT) == 0) || 4347 (NFSVNO_EXTLSCERTUSER(exp) && 4348 (nd->nd_flag & ND_TLSCERTUSER) == 0)) { 4349 if ((nd->nd_flag & ND_NFSV4) != 0) 4350 return (NFSERR_WRONGSEC); 4351 #ifdef notnow 4352 /* There is currently no auth_stat for this. */ 4353 else if ((nd->nd_flag & ND_TLS) == 0) 4354 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS); 4355 else 4356 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST); 4357 #endif 4358 else 4359 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 4360 } 4361 4362 /* 4363 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use 4364 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 4365 */ 4366 if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO) 4367 return (0); 4368 4369 /* 4370 * This seems odd, but allow the case where the security flavor 4371 * list is empty. This happens when NFSv4 is traversing non-exported 4372 * file systems. Exported file systems should always have a non-empty 4373 * security flavor list. 4374 */ 4375 if (exp->nes_numsecflavor == 0) 4376 return (0); 4377 4378 for (i = 0; i < exp->nes_numsecflavor; i++) { 4379 /* 4380 * The tests for privacy and integrity must be first, 4381 * since ND_GSS is set for everything but AUTH_SYS. 4382 */ 4383 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 4384 (nd->nd_flag & ND_GSSPRIVACY)) 4385 return (0); 4386 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 4387 (nd->nd_flag & ND_GSSINTEGRITY)) 4388 return (0); 4389 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 4390 (nd->nd_flag & ND_GSS)) 4391 return (0); 4392 if (exp->nes_secflavors[i] == AUTH_SYS && 4393 (nd->nd_flag & ND_GSS) == 0) 4394 return (0); 4395 } 4396 if ((nd->nd_flag & ND_NFSV4) != 0) 4397 return (NFSERR_WRONGSEC); 4398 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 4399 } 4400 4401 /* 4402 * Calculate a hash value for the fid in a file handle. 4403 */ 4404 uint32_t 4405 nfsrv_hashfh(fhandle_t *fhp) 4406 { 4407 uint32_t hashval; 4408 4409 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 4410 return (hashval); 4411 } 4412 4413 /* 4414 * Calculate a hash value for the sessionid. 4415 */ 4416 uint32_t 4417 nfsrv_hashsessionid(uint8_t *sessionid) 4418 { 4419 uint32_t hashval; 4420 4421 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0); 4422 return (hashval); 4423 } 4424 4425 /* 4426 * Signal the userland master nfsd to backup the stable restart file. 4427 */ 4428 void 4429 nfsrv_backupstable(void) 4430 { 4431 struct proc *procp; 4432 4433 if (NFSD_VNET(nfsd_master_proc) != NULL) { 4434 procp = pfind(nfsd_master_pid); 4435 /* Try to make sure it is the correct process. */ 4436 if (procp == NFSD_VNET(nfsd_master_proc) && 4437 procp->p_stats->p_start.tv_sec == 4438 nfsd_master_start.tv_sec && 4439 procp->p_stats->p_start.tv_usec == 4440 nfsd_master_start.tv_usec && 4441 strcmp(procp->p_comm, nfsd_master_comm) == 0) 4442 kern_psignal(procp, SIGUSR2); 4443 else 4444 NFSD_VNET(nfsd_master_proc) = NULL; 4445 4446 if (procp != NULL) 4447 PROC_UNLOCK(procp); 4448 } 4449 } 4450 4451 /* 4452 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror. 4453 * The arguments are in a structure, so that they can be passed through 4454 * taskqueue for a kernel process to execute this function. 4455 */ 4456 struct nfsrvdscreate { 4457 int done; 4458 int inprog; 4459 struct task tsk; 4460 struct ucred *tcred; 4461 struct vnode *dvp; 4462 NFSPROC_T *p; 4463 struct pnfsdsfile *pf; 4464 int err; 4465 fhandle_t fh; 4466 struct vattr va; 4467 struct vattr createva; 4468 }; 4469 4470 int 4471 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap, 4472 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa, 4473 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp) 4474 { 4475 struct vnode *nvp; 4476 struct nameidata named; 4477 struct vattr va; 4478 char *bufp; 4479 u_long *hashp; 4480 struct nfsnode *np; 4481 struct nfsmount *nmp; 4482 int error; 4483 4484 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE, 4485 LOCKPARENT | LOCKLEAF | NOCACHE); 4486 nfsvno_setpathbuf(&named, &bufp, &hashp); 4487 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE; 4488 named.ni_cnd.cn_nameptr = bufp; 4489 if (fnamep != NULL) { 4490 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1); 4491 named.ni_cnd.cn_namelen = strlen(bufp); 4492 } else 4493 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp); 4494 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp); 4495 4496 /* Create the date file in the DS mount. */ 4497 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE); 4498 if (error == 0) { 4499 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap); 4500 vref(dvp); 4501 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false); 4502 if (error == 0) { 4503 /* Set the ownership of the file. */ 4504 error = VOP_SETATTR(nvp, nvap, tcred); 4505 NFSD_DEBUG(4, "nfsrv_dscreate:" 4506 " setattr-uid=%d\n", error); 4507 if (error != 0) 4508 vput(nvp); 4509 } 4510 if (error != 0) 4511 printf("pNFS: pnfscreate failed=%d\n", error); 4512 } else 4513 printf("pNFS: pnfscreate vnlock=%d\n", error); 4514 if (error == 0) { 4515 np = VTONFS(nvp); 4516 nmp = VFSTONFS(nvp->v_mount); 4517 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs") 4518 != 0 || nmp->nm_nam->sa_len > sizeof( 4519 struct sockaddr_in6) || 4520 np->n_fhp->nfh_len != NFSX_MYFH) { 4521 printf("Bad DS file: fstype=%s salen=%d" 4522 " fhlen=%d\n", 4523 nvp->v_mount->mnt_vfc->vfc_name, 4524 nmp->nm_nam->sa_len, np->n_fhp->nfh_len); 4525 error = ENOENT; 4526 } 4527 4528 /* Set extattrs for the DS on the MDS file. */ 4529 if (error == 0) { 4530 if (dsa != NULL) { 4531 error = VOP_GETATTR(nvp, &va, tcred); 4532 if (error == 0) { 4533 dsa->dsa_filerev = va.va_filerev; 4534 dsa->dsa_size = va.va_size; 4535 dsa->dsa_atime = va.va_atime; 4536 dsa->dsa_mtime = va.va_mtime; 4537 dsa->dsa_bytes = va.va_bytes; 4538 } 4539 } 4540 if (error == 0) { 4541 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, 4542 NFSX_MYFH); 4543 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin, 4544 nmp->nm_nam->sa_len); 4545 NFSBCOPY(named.ni_cnd.cn_nameptr, 4546 pf->dsf_filename, 4547 sizeof(pf->dsf_filename)); 4548 } 4549 } else 4550 printf("pNFS: pnfscreate can't get DS" 4551 " attr=%d\n", error); 4552 if (nvpp != NULL && error == 0) 4553 *nvpp = nvp; 4554 else 4555 vput(nvp); 4556 } 4557 nfsvno_relpathbuf(&named); 4558 return (error); 4559 } 4560 4561 /* 4562 * Start up the thread that will execute nfsrv_dscreate(). 4563 */ 4564 static void 4565 start_dscreate(void *arg, int pending) 4566 { 4567 struct nfsrvdscreate *dsc; 4568 4569 dsc = (struct nfsrvdscreate *)arg; 4570 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh, 4571 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL); 4572 dsc->done = 1; 4573 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err); 4574 } 4575 4576 /* 4577 * Create a pNFS data file on the Data Server(s). 4578 */ 4579 static void 4580 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred, 4581 NFSPROC_T *p) 4582 { 4583 struct nfsrvdscreate *dsc, *tdsc = NULL; 4584 struct nfsdevice *ds, *tds, *fds; 4585 struct mount *mp; 4586 struct pnfsdsfile *pf, *tpf; 4587 struct pnfsdsattr dsattr; 4588 struct vattr va; 4589 struct vnode *dvp[NFSDEV_MAXMIRRORS]; 4590 struct nfsmount *nmp; 4591 fhandle_t fh; 4592 uid_t vauid; 4593 gid_t vagid; 4594 u_short vamode; 4595 struct ucred *tcred; 4596 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret; 4597 int failpos, timo; 4598 4599 /* Get a DS server directory in a round-robin order. */ 4600 mirrorcnt = 1; 4601 mp = vp->v_mount; 4602 ds = fds = NULL; 4603 NFSDDSLOCK(); 4604 /* 4605 * Search for the first entry that handles this MDS fs, but use the 4606 * first entry for all MDS fs's otherwise. 4607 */ 4608 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) { 4609 if (tds->nfsdev_nmp != NULL) { 4610 if (tds->nfsdev_mdsisset == 0 && ds == NULL) 4611 ds = tds; 4612 else if (tds->nfsdev_mdsisset != 0 && fsidcmp( 4613 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) { 4614 ds = fds = tds; 4615 break; 4616 } 4617 } 4618 } 4619 if (ds == NULL) { 4620 NFSDDSUNLOCK(); 4621 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n"); 4622 return; 4623 } 4624 i = dsdir[0] = ds->nfsdev_nextdir; 4625 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize; 4626 dvp[0] = ds->nfsdev_dsdir[i]; 4627 tds = TAILQ_NEXT(ds, nfsdev_list); 4628 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) { 4629 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) { 4630 if (tds->nfsdev_nmp != NULL && 4631 ((tds->nfsdev_mdsisset == 0 && fds == NULL) || 4632 (tds->nfsdev_mdsisset != 0 && fds != NULL && 4633 fsidcmp(&mp->mnt_stat.f_fsid, 4634 &tds->nfsdev_mdsfsid) == 0))) { 4635 dsdir[mirrorcnt] = i; 4636 dvp[mirrorcnt] = tds->nfsdev_dsdir[i]; 4637 mirrorcnt++; 4638 if (mirrorcnt >= nfsrv_maxpnfsmirror) 4639 break; 4640 } 4641 } 4642 } 4643 /* Put at end of list to implement round-robin usage. */ 4644 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list); 4645 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list); 4646 NFSDDSUNLOCK(); 4647 dsc = NULL; 4648 if (mirrorcnt > 1) 4649 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP, 4650 M_WAITOK | M_ZERO); 4651 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK | 4652 M_ZERO); 4653 4654 error = nfsvno_getfh(vp, &fh, p); 4655 if (error == 0) 4656 error = VOP_GETATTR(vp, &va, cred); 4657 if (error == 0) { 4658 /* Set the attributes for "vp" to Setattr the DS vp. */ 4659 vauid = va.va_uid; 4660 vagid = va.va_gid; 4661 vamode = va.va_mode; 4662 VATTR_NULL(&va); 4663 va.va_uid = vauid; 4664 va.va_gid = vagid; 4665 va.va_mode = vamode; 4666 va.va_size = 0; 4667 } else 4668 printf("pNFS: pnfscreate getfh+attr=%d\n", error); 4669 4670 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid, 4671 cred->cr_gid); 4672 /* Make data file name based on FH. */ 4673 tcred = newnfs_getcred(); 4674 4675 /* 4676 * Create the file on each DS mirror, using kernel process(es) for the 4677 * additional mirrors. 4678 */ 4679 failpos = -1; 4680 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) { 4681 tpf->dsf_dir = dsdir[i]; 4682 tdsc->tcred = tcred; 4683 tdsc->p = p; 4684 tdsc->pf = tpf; 4685 tdsc->createva = *vap; 4686 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh)); 4687 tdsc->va = va; 4688 tdsc->dvp = dvp[i]; 4689 tdsc->done = 0; 4690 tdsc->inprog = 0; 4691 tdsc->err = 0; 4692 ret = EIO; 4693 if (nfs_pnfsiothreads != 0) { 4694 ret = nfs_pnfsio(start_dscreate, tdsc); 4695 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret); 4696 } 4697 if (ret != 0) { 4698 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL, 4699 NULL, tcred, p, NULL); 4700 if (ret != 0) { 4701 KASSERT(error == 0, ("nfsrv_dscreate err=%d", 4702 error)); 4703 if (failpos == -1 && nfsds_failerr(ret)) 4704 failpos = i; 4705 else 4706 error = ret; 4707 } 4708 } 4709 } 4710 if (error == 0) { 4711 tpf->dsf_dir = dsdir[mirrorcnt - 1]; 4712 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf, 4713 &dsattr, NULL, tcred, p, NULL); 4714 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) { 4715 failpos = mirrorcnt - 1; 4716 error = 0; 4717 } 4718 } 4719 timo = hz / 50; /* Wait for 20msec. */ 4720 if (timo < 1) 4721 timo = 1; 4722 /* Wait for kernel task(s) to complete. */ 4723 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) { 4724 while (tdsc->inprog != 0 && tdsc->done == 0) 4725 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo); 4726 if (tdsc->err != 0) { 4727 if (failpos == -1 && nfsds_failerr(tdsc->err)) 4728 failpos = i; 4729 else if (error == 0) 4730 error = tdsc->err; 4731 } 4732 } 4733 4734 /* 4735 * If failpos has been set, that mirror has failed, so it needs 4736 * to be disabled. 4737 */ 4738 if (failpos >= 0) { 4739 nmp = VFSTONFS(dvp[failpos]->v_mount); 4740 NFSLOCKMNT(nmp); 4741 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 4742 NFSMNTP_CANCELRPCS)) == 0) { 4743 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4744 NFSUNLOCKMNT(nmp); 4745 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p); 4746 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos, 4747 ds); 4748 if (ds != NULL) 4749 nfsrv_killrpcs(nmp); 4750 NFSLOCKMNT(nmp); 4751 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4752 wakeup(nmp); 4753 } 4754 NFSUNLOCKMNT(nmp); 4755 } 4756 4757 NFSFREECRED(tcred); 4758 if (error == 0) { 4759 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp"); 4760 4761 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n", 4762 mirrorcnt, nfsrv_maxpnfsmirror); 4763 /* 4764 * For all mirrors that couldn't be created, fill in the 4765 * *pf structure, but with an IP address == 0.0.0.0. 4766 */ 4767 tpf = pf + mirrorcnt; 4768 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) { 4769 *tpf = *pf; 4770 tpf->dsf_sin.sin_family = AF_INET; 4771 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in); 4772 tpf->dsf_sin.sin_addr.s_addr = 0; 4773 tpf->dsf_sin.sin_port = 0; 4774 } 4775 4776 error = vn_extattr_set(vp, IO_NODELOCKED, 4777 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", 4778 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p); 4779 if (error == 0) 4780 error = vn_extattr_set(vp, IO_NODELOCKED, 4781 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", 4782 sizeof(dsattr), (char *)&dsattr, p); 4783 if (error != 0) 4784 printf("pNFS: pnfscreate setextattr=%d\n", 4785 error); 4786 } else 4787 printf("pNFS: pnfscreate=%d\n", error); 4788 free(pf, M_TEMP); 4789 free(dsc, M_TEMP); 4790 } 4791 4792 /* 4793 * Get the information needed to remove the pNFS Data Server file from the 4794 * Metadata file. Upon success, ddvp is set non-NULL to the locked 4795 * DS directory vnode. The caller must unlock *ddvp when done with it. 4796 */ 4797 static void 4798 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp, 4799 int *mirrorcntp, char *fname, fhandle_t *fhp) 4800 { 4801 struct vattr va; 4802 struct ucred *tcred; 4803 char *buf; 4804 int buflen, error; 4805 4806 dvpp[0] = NULL; 4807 /* If not an exported regular file or not a pNFS server, just return. */ 4808 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 || 4809 nfsrv_devidcnt == 0) 4810 return; 4811 4812 /* Check to see if this is the last hard link. */ 4813 tcred = newnfs_getcred(); 4814 error = VOP_GETATTR(vp, &va, tcred); 4815 NFSFREECRED(tcred); 4816 if (error != 0) { 4817 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error); 4818 return; 4819 } 4820 if (va.va_nlink > 1) 4821 return; 4822 4823 error = nfsvno_getfh(vp, fhp, p); 4824 if (error != 0) { 4825 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error); 4826 return; 4827 } 4828 4829 buflen = 1024; 4830 buf = malloc(buflen, M_TEMP, M_WAITOK); 4831 /* Get the directory vnode for the DS mount and the file handle. */ 4832 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp, 4833 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL); 4834 free(buf, M_TEMP); 4835 if (error != 0) 4836 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error); 4837 } 4838 4839 /* 4840 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror. 4841 * The arguments are in a structure, so that they can be passed through 4842 * taskqueue for a kernel process to execute this function. 4843 */ 4844 struct nfsrvdsremove { 4845 int done; 4846 int inprog; 4847 struct task tsk; 4848 struct ucred *tcred; 4849 struct vnode *dvp; 4850 NFSPROC_T *p; 4851 int err; 4852 char fname[PNFS_FILENAME_LEN + 1]; 4853 }; 4854 4855 static int 4856 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred, 4857 NFSPROC_T *p) 4858 { 4859 struct nameidata named; 4860 struct vnode *nvp; 4861 char *bufp; 4862 u_long *hashp; 4863 int error; 4864 4865 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE); 4866 if (error != 0) 4867 return (error); 4868 named.ni_cnd.cn_nameiop = DELETE; 4869 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY; 4870 named.ni_cnd.cn_cred = tcred; 4871 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF; 4872 nfsvno_setpathbuf(&named, &bufp, &hashp); 4873 named.ni_cnd.cn_nameptr = bufp; 4874 named.ni_cnd.cn_namelen = strlen(fname); 4875 strlcpy(bufp, fname, NAME_MAX); 4876 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp); 4877 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd); 4878 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error); 4879 if (error == 0) { 4880 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd); 4881 vput(nvp); 4882 } 4883 NFSVOPUNLOCK(dvp); 4884 nfsvno_relpathbuf(&named); 4885 if (error != 0) 4886 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error); 4887 return (error); 4888 } 4889 4890 /* 4891 * Start up the thread that will execute nfsrv_dsremove(). 4892 */ 4893 static void 4894 start_dsremove(void *arg, int pending) 4895 { 4896 struct nfsrvdsremove *dsrm; 4897 4898 dsrm = (struct nfsrvdsremove *)arg; 4899 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred, 4900 dsrm->p); 4901 dsrm->done = 1; 4902 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err); 4903 } 4904 4905 /* 4906 * Remove a pNFS data file from a Data Server. 4907 * nfsrv_pnfsremovesetup() must have been called before the MDS file was 4908 * removed to set up the dvp and fill in the FH. 4909 */ 4910 static void 4911 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp, 4912 NFSPROC_T *p) 4913 { 4914 struct ucred *tcred; 4915 struct nfsrvdsremove *dsrm, *tdsrm; 4916 struct nfsdevice *ds; 4917 struct nfsmount *nmp; 4918 int failpos, i, ret, timo; 4919 4920 tcred = newnfs_getcred(); 4921 dsrm = NULL; 4922 if (mirrorcnt > 1) 4923 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK); 4924 /* 4925 * Remove the file on each DS mirror, using kernel process(es) for the 4926 * additional mirrors. 4927 */ 4928 failpos = -1; 4929 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) { 4930 tdsrm->tcred = tcred; 4931 tdsrm->p = p; 4932 tdsrm->dvp = dvp[i]; 4933 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1); 4934 tdsrm->inprog = 0; 4935 tdsrm->done = 0; 4936 tdsrm->err = 0; 4937 ret = EIO; 4938 if (nfs_pnfsiothreads != 0) { 4939 ret = nfs_pnfsio(start_dsremove, tdsrm); 4940 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret); 4941 } 4942 if (ret != 0) { 4943 ret = nfsrv_dsremove(dvp[i], fname, tcred, p); 4944 if (failpos == -1 && nfsds_failerr(ret)) 4945 failpos = i; 4946 } 4947 } 4948 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p); 4949 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret)) 4950 failpos = mirrorcnt - 1; 4951 timo = hz / 50; /* Wait for 20msec. */ 4952 if (timo < 1) 4953 timo = 1; 4954 /* Wait for kernel task(s) to complete. */ 4955 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) { 4956 while (tdsrm->inprog != 0 && tdsrm->done == 0) 4957 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo); 4958 if (failpos == -1 && nfsds_failerr(tdsrm->err)) 4959 failpos = i; 4960 } 4961 4962 /* 4963 * If failpos has been set, that mirror has failed, so it needs 4964 * to be disabled. 4965 */ 4966 if (failpos >= 0) { 4967 nmp = VFSTONFS(dvp[failpos]->v_mount); 4968 NFSLOCKMNT(nmp); 4969 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 4970 NFSMNTP_CANCELRPCS)) == 0) { 4971 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4972 NFSUNLOCKMNT(nmp); 4973 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p); 4974 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos, 4975 ds); 4976 if (ds != NULL) 4977 nfsrv_killrpcs(nmp); 4978 NFSLOCKMNT(nmp); 4979 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4980 wakeup(nmp); 4981 } 4982 NFSUNLOCKMNT(nmp); 4983 } 4984 4985 /* Get rid all layouts for the file. */ 4986 nfsrv_freefilelayouts(fhp); 4987 4988 NFSFREECRED(tcred); 4989 free(dsrm, M_TEMP); 4990 } 4991 4992 /* 4993 * Generate a file name based on the file handle and put it in *bufp. 4994 * Return the number of bytes generated. 4995 */ 4996 static int 4997 nfsrv_putfhname(fhandle_t *fhp, char *bufp) 4998 { 4999 int i; 5000 uint8_t *cp; 5001 const uint8_t *hexdigits = "0123456789abcdef"; 5002 5003 cp = (uint8_t *)fhp; 5004 for (i = 0; i < sizeof(*fhp); i++) { 5005 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf]; 5006 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf]; 5007 } 5008 bufp[2 * i] = '\0'; 5009 return (2 * i); 5010 } 5011 5012 /* 5013 * Update the Metadata file's attributes from the DS file when a Read/Write 5014 * layout is returned. 5015 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN 5016 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file. 5017 */ 5018 int 5019 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p) 5020 { 5021 struct ucred *tcred; 5022 int error; 5023 5024 /* Do this as root so that it won't fail with EACCES. */ 5025 tcred = newnfs_getcred(); 5026 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN, 5027 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL); 5028 NFSFREECRED(tcred); 5029 return (error); 5030 } 5031 5032 /* 5033 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file. 5034 */ 5035 static int 5036 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred, 5037 NFSPROC_T *p) 5038 { 5039 int error; 5040 5041 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL, 5042 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL); 5043 return (error); 5044 } 5045 5046 static int 5047 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 5048 struct thread *p, int ioproc, struct mbuf **mpp, char *cp, 5049 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp, 5050 off_t *offp, int content, bool *eofp) 5051 { 5052 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp; 5053 fhandle_t fh[NFSDEV_MAXMIRRORS]; 5054 struct vnode *dvp[NFSDEV_MAXMIRRORS]; 5055 struct nfsdevice *ds; 5056 struct pnfsdsattr dsattr; 5057 struct opnfsdsattr odsattr; 5058 char *buf; 5059 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt; 5060 5061 NFSD_DEBUG(4, "in nfsrv_proxyds\n"); 5062 /* 5063 * If not a regular file, not exported or not a pNFS server, 5064 * just return ENOENT. 5065 */ 5066 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 || 5067 nfsrv_devidcnt == 0) 5068 return (ENOENT); 5069 5070 buflen = 1024; 5071 buf = malloc(buflen, M_TEMP, M_WAITOK); 5072 error = 0; 5073 5074 /* 5075 * For Getattr, get the Change attribute (va_filerev) and size (va_size) 5076 * from the MetaData file's extended attribute. 5077 */ 5078 if (ioproc == NFSPROC_GETATTR) { 5079 error = vn_extattr_get(vp, IO_NODELOCKED, 5080 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf, 5081 p); 5082 if (error == 0) { 5083 if (buflen == sizeof(odsattr)) { 5084 NFSBCOPY(buf, &odsattr, buflen); 5085 nap->na_filerev = odsattr.dsa_filerev; 5086 nap->na_size = odsattr.dsa_size; 5087 nap->na_atime = odsattr.dsa_atime; 5088 nap->na_mtime = odsattr.dsa_mtime; 5089 /* 5090 * Fake na_bytes by rounding up na_size. 5091 * Since we don't know the block size, just 5092 * use BLKDEV_IOSIZE. 5093 */ 5094 nap->na_bytes = (odsattr.dsa_size + 5095 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1); 5096 } else if (buflen == sizeof(dsattr)) { 5097 NFSBCOPY(buf, &dsattr, buflen); 5098 nap->na_filerev = dsattr.dsa_filerev; 5099 nap->na_size = dsattr.dsa_size; 5100 nap->na_atime = dsattr.dsa_atime; 5101 nap->na_mtime = dsattr.dsa_mtime; 5102 nap->na_bytes = dsattr.dsa_bytes; 5103 } else 5104 error = ENXIO; 5105 } 5106 if (error == 0) { 5107 /* 5108 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr() 5109 * returns 0, just return now. nfsrv_checkdsattr() 5110 * returns 0 if there is no Read/Write layout 5111 * plus either an Open/Write_access or Write 5112 * delegation issued to a client for the file. 5113 */ 5114 if (nfsrv_pnfsgetdsattr == 0 || 5115 nfsrv_checkdsattr(vp, p) == 0) { 5116 free(buf, M_TEMP); 5117 return (error); 5118 } 5119 } 5120 5121 /* 5122 * Clear ENOATTR so the code below will attempt to do a 5123 * nfsrv_getattrdsrpc() to get the attributes and (re)create 5124 * the extended attribute. 5125 */ 5126 if (error == ENOATTR) 5127 error = 0; 5128 } 5129 5130 origmircnt = -1; 5131 trycnt = 0; 5132 tryagain: 5133 if (error == 0) { 5134 buflen = 1024; 5135 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) == 5136 LK_EXCLUSIVE) 5137 printf("nfsrv_proxyds: Readds vp exclusively locked\n"); 5138 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen, 5139 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL, 5140 NULL, NULL); 5141 if (error == 0) { 5142 for (i = 0; i < mirrorcnt; i++) 5143 nmp[i] = VFSTONFS(dvp[i]->v_mount); 5144 } else 5145 printf("pNFS: proxy getextattr sockaddr=%d\n", error); 5146 } else 5147 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error); 5148 if (error == 0) { 5149 failpos = -1; 5150 if (origmircnt == -1) 5151 origmircnt = mirrorcnt; 5152 /* 5153 * If failpos is set to a mirror#, then that mirror has 5154 * failed and will be disabled. For Read, Getattr and Seek, the 5155 * function only tries one mirror, so if that mirror has 5156 * failed, it will need to be retried. As such, increment 5157 * tryitagain for these cases. 5158 * For Write, Setattr and Setacl, the function tries all 5159 * mirrors and will not return an error for the case where 5160 * one mirror has failed. For these cases, the functioning 5161 * mirror(s) will have been modified, so a retry isn't 5162 * necessary. These functions will set failpos for the 5163 * failed mirror#. 5164 */ 5165 if (ioproc == NFSPROC_READDS) { 5166 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0], 5167 mpp, mpp2); 5168 if (nfsds_failerr(error) && mirrorcnt > 1) { 5169 /* 5170 * Setting failpos will cause the mirror 5171 * to be disabled and then a retry of this 5172 * read is required. 5173 */ 5174 failpos = 0; 5175 error = 0; 5176 trycnt++; 5177 } 5178 } else if (ioproc == NFSPROC_WRITEDS) 5179 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp, 5180 &nmp[0], mirrorcnt, mpp, cp, &failpos); 5181 else if (ioproc == NFSPROC_SETATTR) 5182 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0], 5183 mirrorcnt, nap, &failpos); 5184 else if (ioproc == NFSPROC_SETACL) 5185 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0], 5186 mirrorcnt, aclp, &failpos); 5187 else if (ioproc == NFSPROC_SEEKDS) { 5188 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred, 5189 p, nmp[0]); 5190 if (nfsds_failerr(error) && mirrorcnt > 1) { 5191 /* 5192 * Setting failpos will cause the mirror 5193 * to be disabled and then a retry of this 5194 * read is required. 5195 */ 5196 failpos = 0; 5197 error = 0; 5198 trycnt++; 5199 } 5200 } else if (ioproc == NFSPROC_ALLOCATE) 5201 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp, 5202 &nmp[0], mirrorcnt, &failpos); 5203 else if (ioproc == NFSPROC_DEALLOCATE) 5204 error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p, 5205 vp, &nmp[0], mirrorcnt, &failpos); 5206 else { 5207 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p, 5208 vp, nmp[mirrorcnt - 1], nap); 5209 if (nfsds_failerr(error) && mirrorcnt > 1) { 5210 /* 5211 * Setting failpos will cause the mirror 5212 * to be disabled and then a retry of this 5213 * getattr is required. 5214 */ 5215 failpos = mirrorcnt - 1; 5216 error = 0; 5217 trycnt++; 5218 } 5219 } 5220 ds = NULL; 5221 if (failpos >= 0) { 5222 failnmp = nmp[failpos]; 5223 NFSLOCKMNT(failnmp); 5224 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM | 5225 NFSMNTP_CANCELRPCS)) == 0) { 5226 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS; 5227 NFSUNLOCKMNT(failnmp); 5228 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, 5229 failnmp, p); 5230 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n", 5231 failpos, ds); 5232 if (ds != NULL) 5233 nfsrv_killrpcs(failnmp); 5234 NFSLOCKMNT(failnmp); 5235 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 5236 wakeup(failnmp); 5237 } 5238 NFSUNLOCKMNT(failnmp); 5239 } 5240 for (i = 0; i < mirrorcnt; i++) 5241 NFSVOPUNLOCK(dvp[i]); 5242 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error, 5243 trycnt); 5244 /* Try the Read/Getattr again if a mirror was deleted. */ 5245 if (ds != NULL && trycnt > 0 && trycnt < origmircnt) 5246 goto tryagain; 5247 } else { 5248 /* Return ENOENT for any Extended Attribute error. */ 5249 error = ENOENT; 5250 } 5251 free(buf, M_TEMP); 5252 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error); 5253 return (error); 5254 } 5255 5256 /* 5257 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended 5258 * attribute. 5259 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs 5260 * to be checked. If it points to a NULL nmp, then it returns 5261 * a suitable destination. 5262 * curnmp - If non-NULL, it is the source mount for the copy. 5263 */ 5264 int 5265 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp, 5266 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp, 5267 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp, 5268 struct nfsmount *curnmp, int *ippos, int *dsdirp) 5269 { 5270 struct vnode *dvp, *nvp = NULL, **tdvpp; 5271 struct mount *mp; 5272 struct nfsmount *nmp, *newnmp; 5273 struct sockaddr *sad; 5274 struct sockaddr_in *sin; 5275 struct nfsdevice *ds, *tds, *fndds; 5276 struct pnfsdsfile *pf; 5277 uint32_t dsdir; 5278 int error, fhiszero, fnd, gotone, i, mirrorcnt; 5279 5280 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp"); 5281 *mirrorcntp = 1; 5282 tdvpp = dvpp; 5283 if (nvpp != NULL) 5284 *nvpp = NULL; 5285 if (dvpp != NULL) 5286 *dvpp = NULL; 5287 if (ippos != NULL) 5288 *ippos = -1; 5289 if (newnmpp != NULL) 5290 newnmp = *newnmpp; 5291 else 5292 newnmp = NULL; 5293 mp = vp->v_mount; 5294 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM, 5295 "pnfsd.dsfile", buflenp, buf, p); 5296 mirrorcnt = *buflenp / sizeof(*pf); 5297 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS || 5298 *buflenp != sizeof(*pf) * mirrorcnt)) 5299 error = ENOATTR; 5300 5301 pf = (struct pnfsdsfile *)buf; 5302 /* If curnmp != NULL, check for a match in the mirror list. */ 5303 if (curnmp != NULL && error == 0) { 5304 fnd = 0; 5305 for (i = 0; i < mirrorcnt; i++, pf++) { 5306 sad = (struct sockaddr *)&pf->dsf_sin; 5307 if (nfsaddr2_match(sad, curnmp->nm_nam)) { 5308 if (ippos != NULL) 5309 *ippos = i; 5310 fnd = 1; 5311 break; 5312 } 5313 } 5314 if (fnd == 0) 5315 error = ENXIO; 5316 } 5317 5318 gotone = 0; 5319 pf = (struct pnfsdsfile *)buf; 5320 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt, 5321 error); 5322 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) { 5323 fhiszero = 0; 5324 sad = (struct sockaddr *)&pf->dsf_sin; 5325 sin = &pf->dsf_sin; 5326 dsdir = pf->dsf_dir; 5327 if (dsdir >= nfsrv_dsdirsize) { 5328 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir); 5329 error = ENOATTR; 5330 } else if (nvpp != NULL && newnmp != NULL && 5331 nfsaddr2_match(sad, newnmp->nm_nam)) 5332 error = EEXIST; 5333 if (error == 0) { 5334 if (ippos != NULL && curnmp == NULL && 5335 sad->sa_family == AF_INET && 5336 sin->sin_addr.s_addr == 0) 5337 *ippos = i; 5338 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0) 5339 fhiszero = 1; 5340 /* Use the socket address to find the mount point. */ 5341 fndds = NULL; 5342 NFSDDSLOCK(); 5343 /* Find a match for the IP address. */ 5344 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 5345 if (ds->nfsdev_nmp != NULL) { 5346 dvp = ds->nfsdev_dvp; 5347 nmp = VFSTONFS(dvp->v_mount); 5348 if (nmp != ds->nfsdev_nmp) 5349 printf("different2 nmp %p %p\n", 5350 nmp, ds->nfsdev_nmp); 5351 if (nfsaddr2_match(sad, nmp->nm_nam)) { 5352 fndds = ds; 5353 break; 5354 } 5355 } 5356 } 5357 if (fndds != NULL && newnmpp != NULL && 5358 newnmp == NULL) { 5359 /* Search for a place to make a mirror copy. */ 5360 TAILQ_FOREACH(tds, &nfsrv_devidhead, 5361 nfsdev_list) { 5362 if (tds->nfsdev_nmp != NULL && 5363 fndds != tds && 5364 ((tds->nfsdev_mdsisset == 0 && 5365 fndds->nfsdev_mdsisset == 0) || 5366 (tds->nfsdev_mdsisset != 0 && 5367 fndds->nfsdev_mdsisset != 0 && 5368 fsidcmp(&tds->nfsdev_mdsfsid, 5369 &mp->mnt_stat.f_fsid) == 0))) { 5370 *newnmpp = tds->nfsdev_nmp; 5371 break; 5372 } 5373 } 5374 if (tds != NULL) { 5375 /* 5376 * Move this entry to the end of the 5377 * list, so it won't be selected as 5378 * easily the next time. 5379 */ 5380 TAILQ_REMOVE(&nfsrv_devidhead, tds, 5381 nfsdev_list); 5382 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds, 5383 nfsdev_list); 5384 } 5385 } 5386 NFSDDSUNLOCK(); 5387 if (fndds != NULL) { 5388 dvp = fndds->nfsdev_dsdir[dsdir]; 5389 if (lktype != 0 || fhiszero != 0 || 5390 (nvpp != NULL && *nvpp == NULL)) { 5391 if (fhiszero != 0) 5392 error = vn_lock(dvp, 5393 LK_EXCLUSIVE); 5394 else if (lktype != 0) 5395 error = vn_lock(dvp, lktype); 5396 else 5397 error = vn_lock(dvp, LK_SHARED); 5398 /* 5399 * If the file handle is all 0's, try to 5400 * do a Lookup against the DS to acquire 5401 * it. 5402 * If dvpp == NULL or the Lookup fails, 5403 * unlock dvp after the call. 5404 */ 5405 if (error == 0 && (fhiszero != 0 || 5406 (nvpp != NULL && *nvpp == NULL))) { 5407 error = nfsrv_pnfslookupds(vp, 5408 dvp, pf, &nvp, p); 5409 if (error == 0) { 5410 if (fhiszero != 0) 5411 nfsrv_pnfssetfh( 5412 vp, pf, 5413 devid, 5414 fnamep, 5415 nvp, p); 5416 if (nvpp != NULL && 5417 *nvpp == NULL) { 5418 *nvpp = nvp; 5419 *dsdirp = dsdir; 5420 } else 5421 vput(nvp); 5422 } 5423 if (error != 0 || lktype == 0) 5424 NFSVOPUNLOCK(dvp); 5425 } 5426 } 5427 if (error == 0) { 5428 gotone++; 5429 NFSD_DEBUG(4, "gotone=%d\n", gotone); 5430 if (devid != NULL) { 5431 NFSBCOPY(fndds->nfsdev_deviceid, 5432 devid, NFSX_V4DEVICEID); 5433 devid += NFSX_V4DEVICEID; 5434 } 5435 if (dvpp != NULL) 5436 *tdvpp++ = dvp; 5437 if (fhp != NULL) 5438 NFSBCOPY(&pf->dsf_fh, fhp++, 5439 NFSX_MYFH); 5440 if (fnamep != NULL && gotone == 1) 5441 strlcpy(fnamep, 5442 pf->dsf_filename, 5443 sizeof(pf->dsf_filename)); 5444 } else 5445 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt " 5446 "err=%d\n", error); 5447 } 5448 } 5449 } 5450 if (error == 0 && gotone == 0) 5451 error = ENOENT; 5452 5453 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone, 5454 error); 5455 if (error == 0) 5456 *mirrorcntp = gotone; 5457 else { 5458 if (gotone > 0 && dvpp != NULL) { 5459 /* 5460 * If the error didn't occur on the first one and 5461 * dvpp != NULL, the one(s) prior to the failure will 5462 * have locked dvp's that need to be unlocked. 5463 */ 5464 for (i = 0; i < gotone; i++) { 5465 NFSVOPUNLOCK(*dvpp); 5466 *dvpp++ = NULL; 5467 } 5468 } 5469 /* 5470 * If it found the vnode to be copied from before a failure, 5471 * it needs to be vput()'d. 5472 */ 5473 if (nvpp != NULL && *nvpp != NULL) { 5474 vput(*nvpp); 5475 *nvpp = NULL; 5476 } 5477 } 5478 return (error); 5479 } 5480 5481 /* 5482 * Set the extended attribute for the Change attribute. 5483 */ 5484 static int 5485 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p) 5486 { 5487 struct pnfsdsattr dsattr; 5488 int error; 5489 5490 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp"); 5491 dsattr.dsa_filerev = nap->na_filerev; 5492 dsattr.dsa_size = nap->na_size; 5493 dsattr.dsa_atime = nap->na_atime; 5494 dsattr.dsa_mtime = nap->na_mtime; 5495 dsattr.dsa_bytes = nap->na_bytes; 5496 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM, 5497 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p); 5498 if (error != 0) 5499 printf("pNFS: setextattr=%d\n", error); 5500 return (error); 5501 } 5502 5503 static int 5504 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred, 5505 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp) 5506 { 5507 uint32_t *tl; 5508 struct nfsrv_descript *nd; 5509 nfsv4stateid_t st; 5510 struct mbuf *m, *m2; 5511 int error = 0, retlen, tlen, trimlen; 5512 5513 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n"); 5514 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5515 *mpp = NULL; 5516 /* 5517 * Use a stateid where other is an alternating 01010 pattern and 5518 * seqid is 0xffffffff. This value is not defined as special by 5519 * the RFC and is used by the FreeBSD NFS server to indicate an 5520 * MDS->DS proxy operation. 5521 */ 5522 st.other[0] = 0x55555555; 5523 st.other[1] = 0x55555555; 5524 st.other[2] = 0x55555555; 5525 st.seqid = 0xffffffff; 5526 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp), 5527 NULL, NULL, 0, 0, cred); 5528 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5529 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3); 5530 txdr_hyper(off, tl); 5531 *(tl + 2) = txdr_unsigned(len); 5532 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5533 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5534 if (error != 0) { 5535 free(nd, M_TEMP); 5536 return (error); 5537 } 5538 if (nd->nd_repstat == 0) { 5539 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 5540 NFSM_STRSIZ(retlen, len); 5541 if (retlen > 0) { 5542 /* Trim off the pre-data XDR from the mbuf chain. */ 5543 m = nd->nd_mrep; 5544 while (m != NULL && m != nd->nd_md) { 5545 if (m->m_next == nd->nd_md) { 5546 m->m_next = NULL; 5547 m_freem(nd->nd_mrep); 5548 nd->nd_mrep = m = nd->nd_md; 5549 } else 5550 m = m->m_next; 5551 } 5552 if (m == NULL) { 5553 printf("nfsrv_readdsrpc: busted mbuf list\n"); 5554 error = ENOENT; 5555 goto nfsmout; 5556 } 5557 5558 /* 5559 * Now, adjust first mbuf so that any XDR before the 5560 * read data is skipped over. 5561 */ 5562 trimlen = nd->nd_dpos - mtod(m, char *); 5563 if (trimlen > 0) { 5564 m->m_len -= trimlen; 5565 NFSM_DATAP(m, trimlen); 5566 } 5567 5568 /* 5569 * Truncate the mbuf chain at retlen bytes of data, 5570 * plus XDR padding that brings the length up to a 5571 * multiple of 4. 5572 */ 5573 tlen = NFSM_RNDUP(retlen); 5574 do { 5575 if (m->m_len >= tlen) { 5576 m->m_len = tlen; 5577 tlen = 0; 5578 m2 = m->m_next; 5579 m->m_next = NULL; 5580 m_freem(m2); 5581 break; 5582 } 5583 tlen -= m->m_len; 5584 m = m->m_next; 5585 } while (m != NULL); 5586 if (tlen > 0) { 5587 printf("nfsrv_readdsrpc: busted mbuf list\n"); 5588 error = ENOENT; 5589 goto nfsmout; 5590 } 5591 *mpp = nd->nd_mrep; 5592 *mpendp = m; 5593 nd->nd_mrep = NULL; 5594 } 5595 } else 5596 error = nd->nd_repstat; 5597 nfsmout: 5598 /* If nd->nd_mrep is already NULL, this is a no-op. */ 5599 m_freem(nd->nd_mrep); 5600 free(nd, M_TEMP); 5601 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error); 5602 return (error); 5603 } 5604 5605 /* 5606 * Do a write RPC on a DS data file, using this structure for the arguments, 5607 * so that this function can be executed by a separate kernel process. 5608 */ 5609 struct nfsrvwritedsdorpc { 5610 int done; 5611 int inprog; 5612 struct task tsk; 5613 fhandle_t fh; 5614 off_t off; 5615 int len; 5616 struct nfsmount *nmp; 5617 struct ucred *cred; 5618 NFSPROC_T *p; 5619 struct mbuf *m; 5620 int err; 5621 }; 5622 5623 static int 5624 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len, 5625 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p) 5626 { 5627 uint32_t *tl; 5628 struct nfsrv_descript *nd; 5629 nfsattrbit_t attrbits; 5630 nfsv4stateid_t st; 5631 int commit, error, retlen; 5632 5633 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5634 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp, 5635 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 5636 5637 /* 5638 * Use a stateid where other is an alternating 01010 pattern and 5639 * seqid is 0xffffffff. This value is not defined as special by 5640 * the RFC and is used by the FreeBSD NFS server to indicate an 5641 * MDS->DS proxy operation. 5642 */ 5643 st.other[0] = 0x55555555; 5644 st.other[1] = 0x55555555; 5645 st.other[2] = 0x55555555; 5646 st.seqid = 0xffffffff; 5647 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5648 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 5649 txdr_hyper(off, tl); 5650 tl += 2; 5651 /* 5652 * Do all writes FileSync, since the server doesn't hold onto dirty 5653 * buffers. Since clients should be accessing the DS servers directly 5654 * using the pNFS layouts, this just needs to work correctly as a 5655 * fallback. 5656 */ 5657 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC); 5658 *tl = txdr_unsigned(len); 5659 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len); 5660 5661 /* Put data in mbuf chain. */ 5662 nd->nd_mb->m_next = m; 5663 5664 /* Set nd_mb and nd_bpos to end of data. */ 5665 while (m->m_next != NULL) 5666 m = m->m_next; 5667 nd->nd_mb = m; 5668 nfsm_set(nd, m->m_len); 5669 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len); 5670 5671 /* Do a Getattr for the attributes that change upon writing. */ 5672 NFSZERO_ATTRBIT(&attrbits); 5673 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 5674 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 5675 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 5676 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 5677 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 5678 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 5679 *tl = txdr_unsigned(NFSV4OP_GETATTR); 5680 (void) nfsrv_putattrbit(nd, &attrbits); 5681 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 5682 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5683 if (error != 0) { 5684 free(nd, M_TEMP); 5685 return (error); 5686 } 5687 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat); 5688 /* Get rid of weak cache consistency data for now. */ 5689 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 5690 (ND_NFSV4 | ND_V4WCCATTR)) { 5691 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5692 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 5693 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error); 5694 if (error != 0) 5695 goto nfsmout; 5696 /* 5697 * Get rid of Op# and status for next op. 5698 */ 5699 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5700 if (*++tl != 0) 5701 nd->nd_flag |= ND_NOMOREDATA; 5702 } 5703 if (nd->nd_repstat == 0) { 5704 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 5705 retlen = fxdr_unsigned(int, *tl++); 5706 commit = fxdr_unsigned(int, *tl); 5707 if (commit != NFSWRITE_FILESYNC) 5708 error = NFSERR_IO; 5709 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n", 5710 retlen, commit, error); 5711 } else 5712 error = nd->nd_repstat; 5713 /* We have no use for the Write Verifier since we use FileSync. */ 5714 5715 /* 5716 * Get the Change, Size, Access Time and Modify Time attributes and set 5717 * on the Metadata file, so its attributes will be what the file's 5718 * would be if it had been written. 5719 */ 5720 if (error == 0) { 5721 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5722 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5723 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 5724 } 5725 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error); 5726 nfsmout: 5727 m_freem(nd->nd_mrep); 5728 free(nd, M_TEMP); 5729 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error); 5730 return (error); 5731 } 5732 5733 /* 5734 * Start up the thread that will execute nfsrv_writedsdorpc(). 5735 */ 5736 static void 5737 start_writedsdorpc(void *arg, int pending) 5738 { 5739 struct nfsrvwritedsdorpc *drpc; 5740 5741 drpc = (struct nfsrvwritedsdorpc *)arg; 5742 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 5743 drpc->len, NULL, drpc->m, drpc->cred, drpc->p); 5744 drpc->done = 1; 5745 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err); 5746 } 5747 5748 static int 5749 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred, 5750 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 5751 struct mbuf **mpp, char *cp, int *failposp) 5752 { 5753 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL; 5754 struct nfsvattr na; 5755 struct mbuf *m; 5756 int error, i, offs, ret, timo; 5757 5758 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n"); 5759 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain")); 5760 drpc = NULL; 5761 if (mirrorcnt > 1) 5762 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5763 M_WAITOK); 5764 5765 /* Calculate offset in mbuf chain that data starts. */ 5766 offs = cp - mtod(*mpp, char *); 5767 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len); 5768 5769 /* 5770 * Do the write RPC for every DS, using a separate kernel process 5771 * for every DS except the last one. 5772 */ 5773 error = 0; 5774 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5775 tdrpc->done = 0; 5776 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5777 tdrpc->off = off; 5778 tdrpc->len = len; 5779 tdrpc->nmp = *nmpp; 5780 tdrpc->cred = cred; 5781 tdrpc->p = p; 5782 tdrpc->inprog = 0; 5783 tdrpc->err = 0; 5784 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK); 5785 ret = EIO; 5786 if (nfs_pnfsiothreads != 0) { 5787 ret = nfs_pnfsio(start_writedsdorpc, tdrpc); 5788 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n", 5789 ret); 5790 } 5791 if (ret != 0) { 5792 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL, 5793 tdrpc->m, cred, p); 5794 if (nfsds_failerr(ret) && *failposp == -1) 5795 *failposp = i; 5796 else if (error == 0 && ret != 0) 5797 error = ret; 5798 } 5799 nmpp++; 5800 fhp++; 5801 } 5802 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK); 5803 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p); 5804 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5805 *failposp = mirrorcnt - 1; 5806 else if (error == 0 && ret != 0) 5807 error = ret; 5808 if (error == 0) 5809 error = nfsrv_setextattr(vp, &na, p); 5810 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error); 5811 tdrpc = drpc; 5812 timo = hz / 50; /* Wait for 20msec. */ 5813 if (timo < 1) 5814 timo = 1; 5815 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5816 /* Wait for RPCs on separate threads to complete. */ 5817 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5818 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo); 5819 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5820 *failposp = i; 5821 else if (error == 0 && tdrpc->err != 0) 5822 error = tdrpc->err; 5823 } 5824 free(drpc, M_TEMP); 5825 return (error); 5826 } 5827 5828 /* 5829 * Do a allocate RPC on a DS data file, using this structure for the arguments, 5830 * so that this function can be executed by a separate kernel process. 5831 */ 5832 struct nfsrvallocatedsdorpc { 5833 int done; 5834 int inprog; 5835 struct task tsk; 5836 fhandle_t fh; 5837 off_t off; 5838 off_t len; 5839 struct nfsmount *nmp; 5840 struct ucred *cred; 5841 NFSPROC_T *p; 5842 int err; 5843 }; 5844 5845 static int 5846 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, 5847 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p) 5848 { 5849 uint32_t *tl; 5850 struct nfsrv_descript *nd; 5851 nfsattrbit_t attrbits; 5852 nfsv4stateid_t st; 5853 int error; 5854 5855 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5856 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp, 5857 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 5858 5859 /* 5860 * Use a stateid where other is an alternating 01010 pattern and 5861 * seqid is 0xffffffff. This value is not defined as special by 5862 * the RFC and is used by the FreeBSD NFS server to indicate an 5863 * MDS->DS proxy operation. 5864 */ 5865 st.other[0] = 0x55555555; 5866 st.other[1] = 0x55555555; 5867 st.other[2] = 0x55555555; 5868 st.seqid = 0xffffffff; 5869 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5870 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 5871 txdr_hyper(off, tl); tl += 2; 5872 txdr_hyper(len, tl); tl += 2; 5873 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len); 5874 5875 *tl = txdr_unsigned(NFSV4OP_GETATTR); 5876 NFSGETATTR_ATTRBIT(&attrbits); 5877 nfsrv_putattrbit(nd, &attrbits); 5878 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 5879 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5880 if (error != 0) { 5881 free(nd, M_TEMP); 5882 return (error); 5883 } 5884 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n", 5885 nd->nd_repstat); 5886 if (nd->nd_repstat == 0) { 5887 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5888 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5889 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 5890 } else 5891 error = nd->nd_repstat; 5892 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error); 5893 nfsmout: 5894 m_freem(nd->nd_mrep); 5895 free(nd, M_TEMP); 5896 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error); 5897 return (error); 5898 } 5899 5900 /* 5901 * Start up the thread that will execute nfsrv_allocatedsdorpc(). 5902 */ 5903 static void 5904 start_allocatedsdorpc(void *arg, int pending) 5905 { 5906 struct nfsrvallocatedsdorpc *drpc; 5907 5908 drpc = (struct nfsrvallocatedsdorpc *)arg; 5909 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 5910 drpc->len, NULL, drpc->cred, drpc->p); 5911 drpc->done = 1; 5912 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err); 5913 } 5914 5915 static int 5916 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred, 5917 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 5918 int *failposp) 5919 { 5920 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL; 5921 struct nfsvattr na; 5922 int error, i, ret, timo; 5923 5924 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n"); 5925 drpc = NULL; 5926 if (mirrorcnt > 1) 5927 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5928 M_WAITOK); 5929 5930 /* 5931 * Do the allocate RPC for every DS, using a separate kernel process 5932 * for every DS except the last one. 5933 */ 5934 error = 0; 5935 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5936 tdrpc->done = 0; 5937 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5938 tdrpc->off = off; 5939 tdrpc->len = len; 5940 tdrpc->nmp = *nmpp; 5941 tdrpc->cred = cred; 5942 tdrpc->p = p; 5943 tdrpc->inprog = 0; 5944 tdrpc->err = 0; 5945 ret = EIO; 5946 if (nfs_pnfsiothreads != 0) { 5947 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc); 5948 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n", 5949 ret); 5950 } 5951 if (ret != 0) { 5952 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL, 5953 cred, p); 5954 if (nfsds_failerr(ret) && *failposp == -1) 5955 *failposp = i; 5956 else if (error == 0 && ret != 0) 5957 error = ret; 5958 } 5959 nmpp++; 5960 fhp++; 5961 } 5962 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p); 5963 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5964 *failposp = mirrorcnt - 1; 5965 else if (error == 0 && ret != 0) 5966 error = ret; 5967 if (error == 0) 5968 error = nfsrv_setextattr(vp, &na, p); 5969 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error); 5970 tdrpc = drpc; 5971 timo = hz / 50; /* Wait for 20msec. */ 5972 if (timo < 1) 5973 timo = 1; 5974 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5975 /* Wait for RPCs on separate threads to complete. */ 5976 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5977 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo); 5978 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5979 *failposp = i; 5980 else if (error == 0 && tdrpc->err != 0) 5981 error = tdrpc->err; 5982 } 5983 free(drpc, M_TEMP); 5984 return (error); 5985 } 5986 5987 /* 5988 * Do a deallocate RPC on a DS data file, using this structure for the 5989 * arguments, so that this function can be executed by a separate kernel 5990 * process. 5991 */ 5992 struct nfsrvdeallocatedsdorpc { 5993 int done; 5994 int inprog; 5995 struct task tsk; 5996 fhandle_t fh; 5997 off_t off; 5998 off_t len; 5999 struct nfsmount *nmp; 6000 struct ucred *cred; 6001 NFSPROC_T *p; 6002 int err; 6003 }; 6004 6005 static int 6006 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, 6007 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p) 6008 { 6009 uint32_t *tl; 6010 struct nfsrv_descript *nd; 6011 nfsattrbit_t attrbits; 6012 nfsv4stateid_t st; 6013 int error; 6014 6015 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6016 nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp, 6017 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 6018 6019 /* 6020 * Use a stateid where other is an alternating 01010 pattern and 6021 * seqid is 0xffffffff. This value is not defined as special by 6022 * the RFC and is used by the FreeBSD NFS server to indicate an 6023 * MDS->DS proxy operation. 6024 */ 6025 st.other[0] = 0x55555555; 6026 st.other[1] = 0x55555555; 6027 st.other[2] = 0x55555555; 6028 st.seqid = 0xffffffff; 6029 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6030 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 6031 txdr_hyper(off, tl); tl += 2; 6032 txdr_hyper(len, tl); tl += 2; 6033 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len); 6034 6035 /* Do a Getattr for the attributes that change upon writing. */ 6036 NFSZERO_ATTRBIT(&attrbits); 6037 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 6038 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 6039 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 6040 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 6041 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 6042 *tl = txdr_unsigned(NFSV4OP_GETATTR); 6043 nfsrv_putattrbit(nd, &attrbits); 6044 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 6045 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6046 if (error != 0) { 6047 free(nd, M_TEMP); 6048 return (error); 6049 } 6050 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n", 6051 nd->nd_repstat); 6052 /* Get rid of weak cache consistency data for now. */ 6053 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 6054 (ND_NFSV4 | ND_V4WCCATTR)) { 6055 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 6056 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 6057 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error); 6058 if (error != 0) 6059 goto nfsmout; 6060 /* 6061 * Get rid of Op# and status for next op. 6062 */ 6063 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6064 if (*++tl != 0) 6065 nd->nd_flag |= ND_NOMOREDATA; 6066 } 6067 if (nd->nd_repstat == 0) { 6068 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6069 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 6070 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 6071 } else 6072 error = nd->nd_repstat; 6073 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error); 6074 nfsmout: 6075 m_freem(nd->nd_mrep); 6076 free(nd, M_TEMP); 6077 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error); 6078 return (error); 6079 } 6080 6081 /* 6082 * Start up the thread that will execute nfsrv_deallocatedsdorpc(). 6083 */ 6084 static void 6085 start_deallocatedsdorpc(void *arg, int pending) 6086 { 6087 struct nfsrvdeallocatedsdorpc *drpc; 6088 6089 drpc = (struct nfsrvdeallocatedsdorpc *)arg; 6090 drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 6091 drpc->len, NULL, drpc->cred, drpc->p); 6092 drpc->done = 1; 6093 NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err); 6094 } 6095 6096 static int 6097 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred, 6098 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 6099 int *failposp) 6100 { 6101 struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL; 6102 struct nfsvattr na; 6103 int error, i, ret, timo; 6104 6105 NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n"); 6106 drpc = NULL; 6107 if (mirrorcnt > 1) 6108 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 6109 M_WAITOK); 6110 6111 /* 6112 * Do the deallocate RPC for every DS, using a separate kernel process 6113 * for every DS except the last one. 6114 */ 6115 error = 0; 6116 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6117 tdrpc->done = 0; 6118 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 6119 tdrpc->off = off; 6120 tdrpc->len = len; 6121 tdrpc->nmp = *nmpp; 6122 tdrpc->cred = cred; 6123 tdrpc->p = p; 6124 tdrpc->inprog = 0; 6125 tdrpc->err = 0; 6126 ret = EIO; 6127 if (nfs_pnfsiothreads != 0) { 6128 ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc); 6129 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n", 6130 ret); 6131 } 6132 if (ret != 0) { 6133 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, 6134 NULL, cred, p); 6135 if (nfsds_failerr(ret) && *failposp == -1) 6136 *failposp = i; 6137 else if (error == 0 && ret != 0) 6138 error = ret; 6139 } 6140 nmpp++; 6141 fhp++; 6142 } 6143 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p); 6144 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 6145 *failposp = mirrorcnt - 1; 6146 else if (error == 0 && ret != 0) 6147 error = ret; 6148 if (error == 0) 6149 error = nfsrv_setextattr(vp, &na, p); 6150 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error); 6151 tdrpc = drpc; 6152 timo = hz / 50; /* Wait for 20msec. */ 6153 if (timo < 1) 6154 timo = 1; 6155 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6156 /* Wait for RPCs on separate threads to complete. */ 6157 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6158 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo); 6159 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 6160 *failposp = i; 6161 else if (error == 0 && tdrpc->err != 0) 6162 error = tdrpc->err; 6163 } 6164 free(drpc, M_TEMP); 6165 return (error); 6166 } 6167 6168 static int 6169 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6170 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap, 6171 struct nfsvattr *dsnap) 6172 { 6173 uint32_t *tl; 6174 struct nfsrv_descript *nd; 6175 nfsv4stateid_t st; 6176 nfsattrbit_t attrbits; 6177 int error; 6178 6179 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n"); 6180 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6181 /* 6182 * Use a stateid where other is an alternating 01010 pattern and 6183 * seqid is 0xffffffff. This value is not defined as special by 6184 * the RFC and is used by the FreeBSD NFS server to indicate an 6185 * MDS->DS proxy operation. 6186 */ 6187 st.other[0] = 0x55555555; 6188 st.other[1] = 0x55555555; 6189 st.other[2] = 0x55555555; 6190 st.seqid = 0xffffffff; 6191 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp), 6192 NULL, NULL, 0, 0, cred); 6193 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6194 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0); 6195 6196 /* Do a Getattr for the attributes that change due to writing. */ 6197 NFSZERO_ATTRBIT(&attrbits); 6198 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 6199 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 6200 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 6201 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 6202 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 6203 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 6204 *tl = txdr_unsigned(NFSV4OP_GETATTR); 6205 (void) nfsrv_putattrbit(nd, &attrbits); 6206 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6207 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6208 if (error != 0) { 6209 free(nd, M_TEMP); 6210 return (error); 6211 } 6212 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n", 6213 nd->nd_repstat); 6214 /* Get rid of weak cache consistency data for now. */ 6215 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 6216 (ND_NFSV4 | ND_V4WCCATTR)) { 6217 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL, 6218 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 6219 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error); 6220 if (error != 0) 6221 goto nfsmout; 6222 /* 6223 * Get rid of Op# and status for next op. 6224 */ 6225 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6226 if (*++tl != 0) 6227 nd->nd_flag |= ND_NOMOREDATA; 6228 } 6229 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 6230 if (error != 0) 6231 goto nfsmout; 6232 if (nd->nd_repstat != 0) 6233 error = nd->nd_repstat; 6234 /* 6235 * Get the Change, Size, Access Time and Modify Time attributes and set 6236 * on the Metadata file, so its attributes will be what the file's 6237 * would be if it had been written. 6238 */ 6239 if (error == 0) { 6240 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6241 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL, 6242 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, 6243 NULL); 6244 } 6245 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error); 6246 nfsmout: 6247 m_freem(nd->nd_mrep); 6248 free(nd, M_TEMP); 6249 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error); 6250 return (error); 6251 } 6252 6253 struct nfsrvsetattrdsdorpc { 6254 int done; 6255 int inprog; 6256 struct task tsk; 6257 fhandle_t fh; 6258 struct nfsmount *nmp; 6259 struct vnode *vp; 6260 struct ucred *cred; 6261 NFSPROC_T *p; 6262 struct nfsvattr na; 6263 struct nfsvattr dsna; 6264 int err; 6265 }; 6266 6267 /* 6268 * Start up the thread that will execute nfsrv_setattrdsdorpc(). 6269 */ 6270 static void 6271 start_setattrdsdorpc(void *arg, int pending) 6272 { 6273 struct nfsrvsetattrdsdorpc *drpc; 6274 6275 drpc = (struct nfsrvsetattrdsdorpc *)arg; 6276 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p, 6277 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna); 6278 drpc->done = 1; 6279 } 6280 6281 static int 6282 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6283 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 6284 struct nfsvattr *nap, int *failposp) 6285 { 6286 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL; 6287 struct nfsvattr na; 6288 int error, i, ret, timo; 6289 6290 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n"); 6291 drpc = NULL; 6292 if (mirrorcnt > 1) 6293 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 6294 M_WAITOK); 6295 6296 /* 6297 * Do the setattr RPC for every DS, using a separate kernel process 6298 * for every DS except the last one. 6299 */ 6300 error = 0; 6301 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6302 tdrpc->done = 0; 6303 tdrpc->inprog = 0; 6304 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 6305 tdrpc->nmp = *nmpp; 6306 tdrpc->vp = vp; 6307 tdrpc->cred = cred; 6308 tdrpc->p = p; 6309 tdrpc->na = *nap; 6310 tdrpc->err = 0; 6311 ret = EIO; 6312 if (nfs_pnfsiothreads != 0) { 6313 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc); 6314 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n", 6315 ret); 6316 } 6317 if (ret != 0) { 6318 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, 6319 &na); 6320 if (nfsds_failerr(ret) && *failposp == -1) 6321 *failposp = i; 6322 else if (error == 0 && ret != 0) 6323 error = ret; 6324 } 6325 nmpp++; 6326 fhp++; 6327 } 6328 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na); 6329 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 6330 *failposp = mirrorcnt - 1; 6331 else if (error == 0 && ret != 0) 6332 error = ret; 6333 if (error == 0) 6334 error = nfsrv_setextattr(vp, &na, p); 6335 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error); 6336 tdrpc = drpc; 6337 timo = hz / 50; /* Wait for 20msec. */ 6338 if (timo < 1) 6339 timo = 1; 6340 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6341 /* Wait for RPCs on separate threads to complete. */ 6342 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6343 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo); 6344 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 6345 *failposp = i; 6346 else if (error == 0 && tdrpc->err != 0) 6347 error = tdrpc->err; 6348 } 6349 free(drpc, M_TEMP); 6350 return (error); 6351 } 6352 6353 /* 6354 * Do a Setattr of an NFSv4 ACL on the DS file. 6355 */ 6356 static int 6357 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6358 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp) 6359 { 6360 struct nfsrv_descript *nd; 6361 nfsv4stateid_t st; 6362 nfsattrbit_t attrbits; 6363 int error; 6364 6365 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n"); 6366 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6367 /* 6368 * Use a stateid where other is an alternating 01010 pattern and 6369 * seqid is 0xffffffff. This value is not defined as special by 6370 * the RFC and is used by the FreeBSD NFS server to indicate an 6371 * MDS->DS proxy operation. 6372 */ 6373 st.other[0] = 0x55555555; 6374 st.other[1] = 0x55555555; 6375 st.other[2] = 0x55555555; 6376 st.seqid = 0xffffffff; 6377 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp), 6378 NULL, NULL, 0, 0, cred); 6379 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6380 NFSZERO_ATTRBIT(&attrbits); 6381 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 6382 /* 6383 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(), 6384 * so passing in the metadata "vp" will be ok, since it is of 6385 * the same type (VREG). 6386 */ 6387 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL, 6388 NULL, 0, 0, 0, 0, 0, NULL, false, false, false); 6389 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6390 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6391 if (error != 0) { 6392 free(nd, M_TEMP); 6393 return (error); 6394 } 6395 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n", 6396 nd->nd_repstat); 6397 error = nd->nd_repstat; 6398 m_freem(nd->nd_mrep); 6399 free(nd, M_TEMP); 6400 return (error); 6401 } 6402 6403 struct nfsrvsetacldsdorpc { 6404 int done; 6405 int inprog; 6406 struct task tsk; 6407 fhandle_t fh; 6408 struct nfsmount *nmp; 6409 struct vnode *vp; 6410 struct ucred *cred; 6411 NFSPROC_T *p; 6412 struct acl *aclp; 6413 int err; 6414 }; 6415 6416 /* 6417 * Start up the thread that will execute nfsrv_setacldsdorpc(). 6418 */ 6419 static void 6420 start_setacldsdorpc(void *arg, int pending) 6421 { 6422 struct nfsrvsetacldsdorpc *drpc; 6423 6424 drpc = (struct nfsrvsetacldsdorpc *)arg; 6425 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p, 6426 drpc->vp, drpc->nmp, drpc->aclp); 6427 drpc->done = 1; 6428 } 6429 6430 static int 6431 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6432 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp, 6433 int *failposp) 6434 { 6435 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL; 6436 int error, i, ret, timo; 6437 6438 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n"); 6439 drpc = NULL; 6440 if (mirrorcnt > 1) 6441 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 6442 M_WAITOK); 6443 6444 /* 6445 * Do the setattr RPC for every DS, using a separate kernel process 6446 * for every DS except the last one. 6447 */ 6448 error = 0; 6449 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6450 tdrpc->done = 0; 6451 tdrpc->inprog = 0; 6452 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 6453 tdrpc->nmp = *nmpp; 6454 tdrpc->vp = vp; 6455 tdrpc->cred = cred; 6456 tdrpc->p = p; 6457 tdrpc->aclp = aclp; 6458 tdrpc->err = 0; 6459 ret = EIO; 6460 if (nfs_pnfsiothreads != 0) { 6461 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc); 6462 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n", 6463 ret); 6464 } 6465 if (ret != 0) { 6466 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, 6467 aclp); 6468 if (nfsds_failerr(ret) && *failposp == -1) 6469 *failposp = i; 6470 else if (error == 0 && ret != 0) 6471 error = ret; 6472 } 6473 nmpp++; 6474 fhp++; 6475 } 6476 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp); 6477 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 6478 *failposp = mirrorcnt - 1; 6479 else if (error == 0 && ret != 0) 6480 error = ret; 6481 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error); 6482 tdrpc = drpc; 6483 timo = hz / 50; /* Wait for 20msec. */ 6484 if (timo < 1) 6485 timo = 1; 6486 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6487 /* Wait for RPCs on separate threads to complete. */ 6488 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6489 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo); 6490 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 6491 *failposp = i; 6492 else if (error == 0 && tdrpc->err != 0) 6493 error = tdrpc->err; 6494 } 6495 free(drpc, M_TEMP); 6496 return (error); 6497 } 6498 6499 /* 6500 * Getattr call to the DS for the attributes that change due to writing. 6501 */ 6502 static int 6503 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6504 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap) 6505 { 6506 struct nfsrv_descript *nd; 6507 int error; 6508 nfsattrbit_t attrbits; 6509 6510 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n"); 6511 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6512 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp, 6513 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 6514 NFSZERO_ATTRBIT(&attrbits); 6515 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 6516 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 6517 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 6518 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 6519 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 6520 (void) nfsrv_putattrbit(nd, &attrbits); 6521 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6522 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6523 if (error != 0) { 6524 free(nd, M_TEMP); 6525 return (error); 6526 } 6527 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n", 6528 nd->nd_repstat); 6529 if (nd->nd_repstat == 0) { 6530 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 6531 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, 6532 NULL, NULL, NULL); 6533 /* 6534 * We can only save the updated values in the extended 6535 * attribute if the vp is exclusively locked. 6536 * This should happen when any of the following operations 6537 * occur on the vnode: 6538 * Close, Delegreturn, LayoutCommit, LayoutReturn 6539 * As such, the updated extended attribute should get saved 6540 * before nfsrv_checkdsattr() returns 0 and allows the cached 6541 * attributes to be returned without calling this function. 6542 */ 6543 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) { 6544 error = nfsrv_setextattr(vp, nap, p); 6545 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n", 6546 error); 6547 } 6548 } else 6549 error = nd->nd_repstat; 6550 m_freem(nd->nd_mrep); 6551 free(nd, M_TEMP); 6552 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error); 6553 return (error); 6554 } 6555 6556 /* 6557 * Seek call to a DS. 6558 */ 6559 static int 6560 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp, 6561 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp) 6562 { 6563 uint32_t *tl; 6564 struct nfsrv_descript *nd; 6565 nfsv4stateid_t st; 6566 int error; 6567 6568 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n"); 6569 /* 6570 * Use a stateid where other is an alternating 01010 pattern and 6571 * seqid is 0xffffffff. This value is not defined as special by 6572 * the RFC and is used by the FreeBSD NFS server to indicate an 6573 * MDS->DS proxy operation. 6574 */ 6575 st.other[0] = 0x55555555; 6576 st.other[1] = 0x55555555; 6577 st.other[2] = 0x55555555; 6578 st.seqid = 0xffffffff; 6579 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6580 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp, 6581 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 6582 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6583 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 6584 txdr_hyper(*offp, tl); tl += 2; 6585 *tl = txdr_unsigned(content); 6586 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6587 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6588 if (error != 0) { 6589 free(nd, M_TEMP); 6590 return (error); 6591 } 6592 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat); 6593 if (nd->nd_repstat == 0) { 6594 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER); 6595 if (*tl++ == newnfs_true) 6596 *eofp = true; 6597 else 6598 *eofp = false; 6599 *offp = fxdr_hyper(tl); 6600 } else 6601 error = nd->nd_repstat; 6602 nfsmout: 6603 m_freem(nd->nd_mrep); 6604 free(nd, M_TEMP); 6605 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error); 6606 return (error); 6607 } 6608 6609 /* 6610 * Get the device id and file handle for a DS file. 6611 */ 6612 int 6613 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp, 6614 fhandle_t *fhp, char *devid) 6615 { 6616 int buflen, error; 6617 char *buf; 6618 6619 buflen = 1024; 6620 buf = malloc(buflen, M_TEMP, M_WAITOK); 6621 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL, 6622 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL); 6623 free(buf, M_TEMP); 6624 return (error); 6625 } 6626 6627 /* 6628 * Do a Lookup against the DS for the filename. 6629 */ 6630 static int 6631 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf, 6632 struct vnode **nvpp, NFSPROC_T *p) 6633 { 6634 struct nameidata named; 6635 struct ucred *tcred; 6636 char *bufp; 6637 u_long *hashp; 6638 struct vnode *nvp; 6639 int error; 6640 6641 tcred = newnfs_getcred(); 6642 named.ni_cnd.cn_nameiop = LOOKUP; 6643 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY; 6644 named.ni_cnd.cn_cred = tcred; 6645 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF; 6646 nfsvno_setpathbuf(&named, &bufp, &hashp); 6647 named.ni_cnd.cn_nameptr = bufp; 6648 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename); 6649 strlcpy(bufp, pf->dsf_filename, NAME_MAX); 6650 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp); 6651 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd); 6652 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error); 6653 NFSFREECRED(tcred); 6654 nfsvno_relpathbuf(&named); 6655 if (error == 0) 6656 *nvpp = nvp; 6657 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error); 6658 return (error); 6659 } 6660 6661 /* 6662 * Set the file handle to the correct one. 6663 */ 6664 static void 6665 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid, 6666 char *fnamep, struct vnode *nvp, NFSPROC_T *p) 6667 { 6668 struct nfsnode *np; 6669 int ret = 0; 6670 6671 np = VTONFS(nvp); 6672 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH); 6673 /* 6674 * We can only do a vn_set_extattr() if the vnode is exclusively 6675 * locked and vn_start_write() has been done. If devid != NULL or 6676 * fnamep != NULL or the vnode is shared locked, vn_start_write() 6677 * may not have been done. 6678 * If not done now, it will be done on a future call. 6679 */ 6680 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) == 6681 LK_EXCLUSIVE) 6682 ret = vn_extattr_set(vp, IO_NODELOCKED, 6683 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf), 6684 (char *)pf, p); 6685 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret); 6686 } 6687 6688 /* 6689 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point 6690 * when the DS has failed. 6691 */ 6692 void 6693 nfsrv_killrpcs(struct nfsmount *nmp) 6694 { 6695 6696 /* 6697 * Call newnfs_nmcancelreqs() to cause 6698 * any RPCs in progress on the mount point to 6699 * fail. 6700 * This will cause any process waiting for an 6701 * RPC to complete while holding a vnode lock 6702 * on the mounted-on vnode (such as "df" or 6703 * a non-forced "umount") to fail. 6704 * This will unlock the mounted-on vnode so 6705 * a forced dismount can succeed. 6706 * The NFSMNTP_CANCELRPCS flag should be set when this function is 6707 * called. 6708 */ 6709 newnfs_nmcancelreqs(nmp); 6710 } 6711 6712 /* 6713 * Sum up the statfs info for each of the DSs, so that the client will 6714 * receive the total for all DSs. 6715 */ 6716 static int 6717 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp) 6718 { 6719 struct statfs *tsf; 6720 struct nfsdevice *ds; 6721 struct vnode **dvpp, **tdvpp, *dvp; 6722 uint64_t tot; 6723 int cnt, error = 0, i; 6724 6725 if (nfsrv_devidcnt <= 0) 6726 return (ENXIO); 6727 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK); 6728 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK); 6729 6730 /* Get an array of the dvps for the DSs. */ 6731 tdvpp = dvpp; 6732 i = 0; 6733 NFSDDSLOCK(); 6734 /* First, search for matches for same file system. */ 6735 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 6736 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 && 6737 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) { 6738 if (++i > nfsrv_devidcnt) 6739 break; 6740 *tdvpp++ = ds->nfsdev_dvp; 6741 } 6742 } 6743 /* 6744 * If no matches for same file system, total all servers not assigned 6745 * to a file system. 6746 */ 6747 if (i == 0) { 6748 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 6749 if (ds->nfsdev_nmp != NULL && 6750 ds->nfsdev_mdsisset == 0) { 6751 if (++i > nfsrv_devidcnt) 6752 break; 6753 *tdvpp++ = ds->nfsdev_dvp; 6754 } 6755 } 6756 } 6757 NFSDDSUNLOCK(); 6758 cnt = i; 6759 6760 /* Do a VFS_STATFS() for each of the DSs and sum them up. */ 6761 tdvpp = dvpp; 6762 for (i = 0; i < cnt && error == 0; i++) { 6763 dvp = *tdvpp++; 6764 error = VFS_STATFS(dvp->v_mount, tsf); 6765 if (error == 0) { 6766 if (sf->f_bsize == 0) { 6767 if (tsf->f_bsize > 0) 6768 sf->f_bsize = tsf->f_bsize; 6769 else 6770 sf->f_bsize = 8192; 6771 } 6772 if (tsf->f_blocks > 0) { 6773 if (sf->f_bsize != tsf->f_bsize) { 6774 tot = tsf->f_blocks * tsf->f_bsize; 6775 sf->f_blocks += (tot / sf->f_bsize); 6776 } else 6777 sf->f_blocks += tsf->f_blocks; 6778 } 6779 if (tsf->f_bfree > 0) { 6780 if (sf->f_bsize != tsf->f_bsize) { 6781 tot = tsf->f_bfree * tsf->f_bsize; 6782 sf->f_bfree += (tot / sf->f_bsize); 6783 } else 6784 sf->f_bfree += tsf->f_bfree; 6785 } 6786 if (tsf->f_bavail > 0) { 6787 if (sf->f_bsize != tsf->f_bsize) { 6788 tot = tsf->f_bavail * tsf->f_bsize; 6789 sf->f_bavail += (tot / sf->f_bsize); 6790 } else 6791 sf->f_bavail += tsf->f_bavail; 6792 } 6793 } 6794 } 6795 free(tsf, M_TEMP); 6796 free(dvpp, M_TEMP); 6797 return (error); 6798 } 6799 6800 /* 6801 * Set an NFSv4 acl. 6802 */ 6803 int 6804 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p) 6805 { 6806 int error; 6807 6808 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) { 6809 error = NFSERR_ATTRNOTSUPP; 6810 goto out; 6811 } 6812 /* 6813 * With NFSv4 ACLs, chmod(2) may need to add additional entries. 6814 * Make sure it has enough room for that - splitting every entry 6815 * into two and appending "canonical six" entries at the end. 6816 * Cribbed out of kern/vfs_acl.c - Rick M. 6817 */ 6818 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) { 6819 error = NFSERR_ATTRNOTSUPP; 6820 goto out; 6821 } 6822 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p); 6823 if (error == 0) { 6824 error = nfsrv_dssetacl(vp, aclp, cred, p); 6825 if (error == ENOENT) 6826 error = 0; 6827 } 6828 6829 out: 6830 NFSEXITCODE(error); 6831 return (error); 6832 } 6833 6834 /* 6835 * Seek vnode op call (actually it is a VOP_IOCTL()). 6836 * This function is called with the vnode locked, but unlocks and vrele()s 6837 * the vp before returning. 6838 */ 6839 int 6840 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd, 6841 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p) 6842 { 6843 struct nfsvattr at; 6844 int error, ret; 6845 6846 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp"); 6847 /* 6848 * Attempt to seek on a DS file. A return of ENOENT implies 6849 * there is no DS file to seek on. 6850 */ 6851 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL, 6852 NULL, NULL, NULL, NULL, offp, content, eofp); 6853 if (error != ENOENT) { 6854 vput(vp); 6855 return (error); 6856 } 6857 6858 /* 6859 * Do the VOP_IOCTL() call. For the case where *offp == file_size, 6860 * VOP_IOCTL() will return ENXIO. However, the correct reply for 6861 * NFSv4.2 is *eofp == true and error == 0 for this case. 6862 */ 6863 NFSVOPUNLOCK(vp); 6864 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p); 6865 *eofp = false; 6866 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) { 6867 /* Handle the cases where we might be at EOF. */ 6868 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL); 6869 if (ret == 0 && *offp == at.na_size) { 6870 *eofp = true; 6871 error = 0; 6872 } 6873 if (ret != 0 && error == 0) 6874 error = ret; 6875 } 6876 vrele(vp); 6877 NFSEXITCODE(error); 6878 return (error); 6879 } 6880 6881 /* 6882 * Allocate vnode op call. 6883 */ 6884 int 6885 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred, 6886 NFSPROC_T *p) 6887 { 6888 int error; 6889 off_t olen; 6890 6891 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp"); 6892 /* 6893 * Attempt to allocate on a DS file. A return of ENOENT implies 6894 * there is no DS file to allocate on. 6895 */ 6896 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL, 6897 NULL, NULL, NULL, NULL, &len, 0, NULL); 6898 if (error != ENOENT) 6899 return (error); 6900 6901 /* 6902 * Do the actual VOP_ALLOCATE(), looping so long as 6903 * progress is being made, to achieve completion. 6904 */ 6905 do { 6906 olen = len; 6907 error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred); 6908 if (error == 0 && len > 0 && olen > len) 6909 maybe_yield(); 6910 } while (error == 0 && len > 0 && olen > len); 6911 if (error == 0 && len > 0) 6912 error = NFSERR_IO; 6913 NFSEXITCODE(error); 6914 return (error); 6915 } 6916 6917 /* 6918 * Deallocate vnode op call. 6919 */ 6920 int 6921 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred, 6922 NFSPROC_T *p) 6923 { 6924 int error; 6925 off_t olen; 6926 6927 ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp"); 6928 /* 6929 * Attempt to deallocate on a DS file. A return of ENOENT implies 6930 * there is no DS file to deallocate on. 6931 */ 6932 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL, 6933 NULL, NULL, NULL, NULL, &len, 0, NULL); 6934 if (error != ENOENT) 6935 return (error); 6936 6937 /* 6938 * Do the actual VOP_DEALLOCATE(), looping so long as 6939 * progress is being made, to achieve completion. 6940 */ 6941 do { 6942 olen = len; 6943 error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred); 6944 if (error == 0 && len > 0 && olen > len) 6945 maybe_yield(); 6946 } while (error == 0 && len > 0 && olen > len); 6947 if (error == 0 && len > 0) 6948 error = NFSERR_IO; 6949 NFSEXITCODE(error); 6950 return (error); 6951 } 6952 6953 /* 6954 * Get Extended Atribute vnode op into an mbuf list. 6955 */ 6956 int 6957 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp, 6958 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p, 6959 struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 6960 { 6961 struct iovec *iv; 6962 struct uio io, *uiop = &io; 6963 struct mbuf *m, *m2; 6964 int alen, error, len, tlen; 6965 size_t siz; 6966 6967 /* First, find out the size of the extended attribute. */ 6968 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL, 6969 &siz, cred, p); 6970 if (error != 0) 6971 return (NFSERR_NOXATTR); 6972 if (siz > maxresp - NFS_MAXXDR) 6973 return (NFSERR_XATTR2BIG); 6974 len = siz; 6975 tlen = NFSM_RNDUP(len); 6976 if (tlen > 0) { 6977 /* 6978 * If cnt > MCLBYTES and the reply will not be saved, use 6979 * ext_pgs mbufs for TLS. 6980 * For NFSv4.0, we do not know for sure if the reply will 6981 * be saved, so do not use ext_pgs mbufs for NFSv4.0. 6982 * Always use ext_pgs mbufs if ND_EXTPG is set. 6983 */ 6984 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES && 6985 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS && 6986 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)) 6987 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen, 6988 maxextsiz, &m, &m2, &iv); 6989 else 6990 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2, 6991 &iv); 6992 uiop->uio_iov = iv; 6993 } else { 6994 uiop->uio_iovcnt = 0; 6995 uiop->uio_iov = iv = NULL; 6996 m = m2 = NULL; 6997 } 6998 uiop->uio_offset = 0; 6999 uiop->uio_resid = tlen; 7000 uiop->uio_rw = UIO_READ; 7001 uiop->uio_segflg = UIO_SYSSPACE; 7002 uiop->uio_td = p; 7003 #ifdef MAC 7004 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER, 7005 name); 7006 if (error != 0) 7007 goto out; 7008 #endif 7009 7010 if (tlen > 0) 7011 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop, 7012 NULL, cred, p); 7013 if (error != 0) 7014 goto out; 7015 if (uiop->uio_resid > 0) { 7016 alen = tlen; 7017 len = tlen - uiop->uio_resid; 7018 tlen = NFSM_RNDUP(len); 7019 if (alen != tlen) 7020 printf("nfsvno_getxattr: weird size read\n"); 7021 if (tlen == 0) { 7022 m_freem(m); 7023 m = m2 = NULL; 7024 } else if (alen != tlen || tlen != len) 7025 m2 = nfsrv_adj(m, alen - tlen, tlen - len); 7026 } 7027 *lenp = len; 7028 *mpp = m; 7029 *mpendp = m2; 7030 7031 out: 7032 if (error != 0) { 7033 if (m != NULL) 7034 m_freem(m); 7035 *lenp = 0; 7036 } 7037 free(iv, M_TEMP); 7038 NFSEXITCODE(error); 7039 return (error); 7040 } 7041 7042 /* 7043 * Set Extended attribute vnode op from an mbuf list. 7044 */ 7045 int 7046 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m, 7047 char *cp, struct ucred *cred, struct thread *p) 7048 { 7049 struct iovec *iv; 7050 struct uio uio, *uiop = &uio; 7051 int cnt, error; 7052 7053 error = 0; 7054 #ifdef MAC 7055 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER, 7056 name); 7057 #endif 7058 if (error != 0) 7059 goto out; 7060 7061 uiop->uio_rw = UIO_WRITE; 7062 uiop->uio_segflg = UIO_SYSSPACE; 7063 uiop->uio_td = p; 7064 uiop->uio_offset = 0; 7065 uiop->uio_resid = len; 7066 if (len > 0) { 7067 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt); 7068 uiop->uio_iov = iv; 7069 uiop->uio_iovcnt = cnt; 7070 } else { 7071 uiop->uio_iov = iv = NULL; 7072 uiop->uio_iovcnt = 0; 7073 } 7074 if (error == 0) { 7075 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop, 7076 cred, p); 7077 if (error == 0) { 7078 if (vp->v_type == VREG && nfsrv_devidcnt != 0) 7079 nfsvno_updateds(vp, cred, p); 7080 error = VOP_FSYNC(vp, MNT_WAIT, p); 7081 } 7082 free(iv, M_TEMP); 7083 } 7084 7085 out: 7086 NFSEXITCODE(error); 7087 return (error); 7088 } 7089 7090 /* 7091 * For a pNFS server, the DS file's ctime and 7092 * va_filerev (TimeMetadata and Change) needs to 7093 * be updated. This is a hack, but works by 7094 * flipping the S_ISGID bit in va_mode and then 7095 * flipping it back. 7096 * It does result in two MDS->DS RPCs, but creating 7097 * a custom RPC just to do this seems overkill, since 7098 * Setxattr/Rmxattr will not be done that frequently. 7099 * If it fails part way through, that is not too 7100 * serious, since the DS file is never executed. 7101 */ 7102 static void 7103 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p) 7104 { 7105 struct nfsvattr nva; 7106 int ret; 7107 u_short tmode; 7108 7109 ret = VOP_GETATTR(vp, &nva.na_vattr, cred); 7110 if (ret == 0) { 7111 tmode = nva.na_mode; 7112 NFSVNO_ATTRINIT(&nva); 7113 tmode ^= S_ISGID; 7114 NFSVNO_SETATTRVAL(&nva, mode, tmode); 7115 ret = nfsrv_proxyds(vp, 0, 0, cred, p, 7116 NFSPROC_SETATTR, NULL, NULL, NULL, &nva, 7117 NULL, NULL, 0, NULL); 7118 if (ret == 0) { 7119 tmode ^= S_ISGID; 7120 NFSVNO_SETATTRVAL(&nva, mode, tmode); 7121 ret = nfsrv_proxyds(vp, 0, 0, cred, p, 7122 NFSPROC_SETATTR, NULL, NULL, NULL, 7123 &nva, NULL, NULL, 0, NULL); 7124 } 7125 } 7126 } 7127 7128 /* 7129 * Remove Extended attribute vnode op. 7130 */ 7131 int 7132 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name, 7133 struct ucred *cred, struct thread *p) 7134 { 7135 int error; 7136 7137 /* 7138 * Get rid of any delegations. I am not sure why this is required, 7139 * but RFC-8276 says so. 7140 */ 7141 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p); 7142 if (error != 0) 7143 goto out; 7144 #ifdef MAC 7145 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER, 7146 name); 7147 if (error != 0) 7148 goto out; 7149 #endif 7150 7151 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p); 7152 if (error == EOPNOTSUPP) 7153 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL, 7154 cred, p); 7155 if (error == 0) { 7156 if (vp->v_type == VREG && nfsrv_devidcnt != 0) 7157 nfsvno_updateds(vp, cred, p); 7158 error = VOP_FSYNC(vp, MNT_WAIT, p); 7159 } 7160 out: 7161 NFSEXITCODE(error); 7162 return (error); 7163 } 7164 7165 /* 7166 * List Extended Atribute vnode op into an mbuf list. 7167 */ 7168 int 7169 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred, 7170 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp) 7171 { 7172 struct iovec iv; 7173 struct uio io; 7174 int error; 7175 size_t siz; 7176 7177 *bufp = NULL; 7178 /* First, find out the size of the extended attribute. */ 7179 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred, 7180 p); 7181 if (error != 0) 7182 return (NFSERR_NOXATTR); 7183 if (siz <= cookie) { 7184 *lenp = 0; 7185 *eofp = true; 7186 goto out; 7187 } 7188 if (siz > cookie + *lenp) { 7189 siz = cookie + *lenp; 7190 *eofp = false; 7191 } else 7192 *eofp = true; 7193 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */ 7194 if (siz > 10 * 1024 * 1024) { 7195 error = NFSERR_XATTR2BIG; 7196 goto out; 7197 } 7198 *bufp = malloc(siz, M_TEMP, M_WAITOK); 7199 iv.iov_base = *bufp; 7200 iv.iov_len = siz; 7201 io.uio_iovcnt = 1; 7202 io.uio_iov = &iv; 7203 io.uio_offset = 0; 7204 io.uio_resid = siz; 7205 io.uio_rw = UIO_READ; 7206 io.uio_segflg = UIO_SYSSPACE; 7207 io.uio_td = p; 7208 #ifdef MAC 7209 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER); 7210 if (error != 0) 7211 goto out; 7212 #endif 7213 7214 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred, 7215 p); 7216 if (error != 0) 7217 goto out; 7218 if (io.uio_resid > 0) 7219 siz -= io.uio_resid; 7220 *lenp = siz; 7221 7222 out: 7223 if (error != 0) { 7224 free(*bufp, M_TEMP); 7225 *bufp = NULL; 7226 } 7227 NFSEXITCODE(error); 7228 return (error); 7229 } 7230 7231 /* 7232 * Trim trailing data off the mbuf list being built. 7233 */ 7234 void 7235 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos, 7236 int bextpg, int bextpgsiz) 7237 { 7238 vm_page_t pg; 7239 int fullpgsiz, i; 7240 7241 if (mb->m_next != NULL) { 7242 m_freem(mb->m_next); 7243 mb->m_next = NULL; 7244 } 7245 if ((mb->m_flags & M_EXTPG) != 0) { 7246 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs, 7247 ("nfsm_trimtrailing: bextpg out of range")); 7248 KASSERT(bpos == (char *)(void *) 7249 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz, 7250 ("nfsm_trimtrailing: bextpgsiz bad!")); 7251 7252 /* First, get rid of any pages after this position. */ 7253 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) { 7254 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]); 7255 vm_page_unwire_noq(pg); 7256 vm_page_free(pg); 7257 } 7258 mb->m_epg_npgs = bextpg + 1; 7259 if (bextpg == 0) 7260 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off; 7261 else 7262 fullpgsiz = PAGE_SIZE; 7263 mb->m_epg_last_len = fullpgsiz - bextpgsiz; 7264 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off); 7265 for (i = 1; i < mb->m_epg_npgs; i++) 7266 mb->m_len += m_epg_pagelen(mb, i, 0); 7267 nd->nd_bextpgsiz = bextpgsiz; 7268 nd->nd_bextpg = bextpg; 7269 } else 7270 mb->m_len = bpos - mtod(mb, char *); 7271 nd->nd_mb = mb; 7272 nd->nd_bpos = bpos; 7273 } 7274 7275 7276 /* 7277 * Check to see if a put file handle operation should test for 7278 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR. 7279 * When Open is the next operation, NFSERR_WRONGSEC cannot be 7280 * replied for the Open cases that use a component. This can 7281 * be identified by the fact that the file handle's type is VDIR. 7282 */ 7283 bool 7284 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, __enum_uint8(vtype) vtyp) 7285 { 7286 7287 if ((nd->nd_flag & ND_NFSV4) == 0) 7288 return (true); 7289 7290 if ((nd->nd_flag & ND_LASTOP) != 0) 7291 return (false); 7292 7293 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH || 7294 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH || 7295 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP || 7296 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME) 7297 return (false); 7298 if (nextop == NFSV4OP_OPEN && vtyp == VDIR) 7299 return (false); 7300 return (true); 7301 } 7302 7303 /* 7304 * Check DSs marked no space. 7305 */ 7306 void 7307 nfsrv_checknospc(void) 7308 { 7309 struct statfs *tsf; 7310 struct nfsdevice *ds; 7311 struct vnode **dvpp, **tdvpp, *dvp; 7312 char *devid, *tdevid; 7313 int cnt, error = 0, i; 7314 7315 if (nfsrv_devidcnt <= 0) 7316 return; 7317 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK); 7318 devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK); 7319 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK); 7320 7321 /* Get an array of the dvps for the DSs. */ 7322 tdvpp = dvpp; 7323 tdevid = devid; 7324 i = 0; 7325 NFSDDSLOCK(); 7326 /* First, search for matches for same file system. */ 7327 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 7328 if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) { 7329 if (++i > nfsrv_devidcnt) 7330 break; 7331 *tdvpp++ = ds->nfsdev_dvp; 7332 NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID); 7333 tdevid += NFSX_V4DEVICEID; 7334 } 7335 } 7336 NFSDDSUNLOCK(); 7337 7338 /* Do a VFS_STATFS() for each of the DSs and clear no space. */ 7339 cnt = i; 7340 tdvpp = dvpp; 7341 tdevid = devid; 7342 for (i = 0; i < cnt && error == 0; i++) { 7343 dvp = *tdvpp++; 7344 error = VFS_STATFS(dvp->v_mount, tsf); 7345 if (error == 0 && tsf->f_bavail > 0) { 7346 NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n"); 7347 nfsrv_marknospc(tdevid, false); 7348 } 7349 tdevid += NFSX_V4DEVICEID; 7350 } 7351 free(tsf, M_TEMP); 7352 free(dvpp, M_TEMP); 7353 free(devid, M_TEMP); 7354 } 7355 7356 /* 7357 * Initialize everything that needs to be initialized for a vnet. 7358 */ 7359 static void 7360 nfsrv_vnetinit(const void *unused __unused) 7361 { 7362 7363 nfsd_mntinit(); 7364 } 7365 VNET_SYSINIT(nfsrv_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY, 7366 nfsrv_vnetinit, NULL); 7367 7368 /* 7369 * Clean up everything that is in a vnet and needs to be 7370 * done when the jail is destroyed or the module unloaded. 7371 */ 7372 static void 7373 nfsrv_cleanup(const void *unused __unused) 7374 { 7375 int i; 7376 7377 NFSD_LOCK(); 7378 if (!NFSD_VNET(nfsrv_mntinited)) { 7379 NFSD_UNLOCK(); 7380 return; 7381 } 7382 NFSD_VNET(nfsrv_mntinited) = false; 7383 NFSD_UNLOCK(); 7384 7385 /* Clean out all NFSv4 state. */ 7386 nfsrv_throwawayallstate(curthread); 7387 7388 /* Clean the NFS server reply cache */ 7389 nfsrvd_cleancache(); 7390 7391 /* Clean out v4root exports. */ 7392 if (NFSD_VNET(nfsv4root_mnt)->mnt_export != NULL) { 7393 vfs_free_addrlist(NFSD_VNET(nfsv4root_mnt)->mnt_export); 7394 free(NFSD_VNET(nfsv4root_mnt)->mnt_export, M_MOUNT); 7395 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL; 7396 } 7397 7398 /* Free up the krpc server pool. */ 7399 if (NFSD_VNET(nfsrvd_pool) != NULL) 7400 svcpool_destroy(NFSD_VNET(nfsrvd_pool)); 7401 7402 /* and get rid of the locks */ 7403 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 7404 mtx_destroy(&NFSD_VNET(nfsrchash_table)[i].mtx); 7405 mtx_destroy(&NFSD_VNET(nfsrcahash_table)[i].mtx); 7406 } 7407 mtx_destroy(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx); 7408 for (i = 0; i < nfsrv_sessionhashsize; i++) 7409 mtx_destroy(&NFSD_VNET(nfssessionhash)[i].mtx); 7410 lockdestroy(&NFSD_VNET(nfsv4root_mnt)->mnt_explock); 7411 free(NFSD_VNET(nfsrvudphashtbl), M_NFSRVCACHE); 7412 free(NFSD_VNET(nfsrchash_table), M_NFSRVCACHE); 7413 free(NFSD_VNET(nfsrcahash_table), M_NFSRVCACHE); 7414 free(NFSD_VNET(nfsclienthash), M_NFSDCLIENT); 7415 free(NFSD_VNET(nfslockhash), M_NFSDLOCKFILE); 7416 free(NFSD_VNET(nfssessionhash), M_NFSDSESSION); 7417 free(NFSD_VNET(nfsv4root_mnt), M_TEMP); 7418 NFSD_VNET(nfsv4root_mnt) = NULL; 7419 } 7420 VNET_SYSUNINIT(nfsrv_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY, 7421 nfsrv_cleanup, NULL); 7422 7423 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 7424 7425 /* 7426 * Called once to initialize data structures... 7427 */ 7428 static int 7429 nfsd_modevent(module_t mod, int type, void *data) 7430 { 7431 int error = 0, i; 7432 static int loaded = 0; 7433 7434 switch (type) { 7435 case MOD_LOAD: 7436 if (loaded) 7437 goto out; 7438 newnfs_portinit(); 7439 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF); 7440 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF); 7441 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF); 7442 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF); 7443 #ifdef VV_DISABLEDELEG 7444 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 7445 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 7446 #endif 7447 nfsd_call_nfsd = nfssvc_nfsd; 7448 loaded = 1; 7449 break; 7450 7451 case MOD_UNLOAD: 7452 if (newnfs_numnfsd != 0) { 7453 error = EBUSY; 7454 break; 7455 } 7456 7457 #ifdef VV_DISABLEDELEG 7458 vn_deleg_ops.vndeleg_recall = NULL; 7459 vn_deleg_ops.vndeleg_disable = NULL; 7460 #endif 7461 nfsd_call_nfsd = NULL; 7462 mtx_destroy(&nfsrc_udpmtx); 7463 mtx_destroy(&nfs_v4root_mutex); 7464 mtx_destroy(&nfsrv_dontlistlock_mtx); 7465 mtx_destroy(&nfsrv_recalllock_mtx); 7466 if (nfslayouthash != NULL) { 7467 for (i = 0; i < nfsrv_layouthashsize; i++) 7468 mtx_destroy(&nfslayouthash[i].mtx); 7469 free(nfslayouthash, M_NFSDSESSION); 7470 } 7471 loaded = 0; 7472 break; 7473 default: 7474 error = EOPNOTSUPP; 7475 break; 7476 } 7477 7478 out: 7479 NFSEXITCODE(error); 7480 return (error); 7481 } 7482 static moduledata_t nfsd_mod = { 7483 "nfsd", 7484 nfsd_modevent, 7485 NULL, 7486 }; 7487 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 7488 7489 /* So that loader and kldload(2) can find us, wherever we are.. */ 7490 MODULE_VERSION(nfsd, 1); 7491 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 7492 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 7493 MODULE_DEPEND(nfsd, krpc, 1, 1, 1); 7494 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 7495