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 nd->nd_cred->cr_gid = credanon->cr_gid; 3467 /* 3468 * 'credanon' is already a 'struct ucred' that was built 3469 * internally with calls to crsetgroups_and_egid(), so 3470 * we don't need a fallback here. 3471 */ 3472 crsetgroups(nd->nd_cred, credanon->cr_ngroups, 3473 credanon->cr_groups); 3474 } else if ((nd->nd_flag & ND_GSS) == 0) { 3475 /* 3476 * If using AUTH_SYS, call nfsrv_getgrpscred() to see 3477 * if there is a replacement credential with a group 3478 * list set up by "nfsuserd -manage-gids". 3479 * If there is no replacement, nfsrv_getgrpscred() 3480 * simply returns its argument. 3481 */ 3482 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred); 3483 } 3484 } 3485 3486 out: 3487 NFSEXITCODE2(error, nd); 3488 return (error); 3489 } 3490 3491 /* 3492 * Check exports. 3493 */ 3494 int 3495 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 3496 struct ucred **credp) 3497 { 3498 int error; 3499 3500 error = 0; 3501 *credp = NULL; 3502 MNT_ILOCK(mp); 3503 if (mp->mnt_exjail == NULL || 3504 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison) 3505 error = EACCES; 3506 MNT_IUNLOCK(mp); 3507 if (error == 0) 3508 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 3509 &exp->nes_numsecflavor, exp->nes_secflavors); 3510 if (error) { 3511 if (NFSD_VNET(nfs_rootfhset)) { 3512 exp->nes_exflag = 0; 3513 exp->nes_numsecflavor = 0; 3514 error = 0; 3515 } 3516 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor > 3517 MAXSECFLAVORS) { 3518 printf("nfsvno_checkexp: numsecflavors out of range\n"); 3519 exp->nes_numsecflavor = 0; 3520 error = EACCES; 3521 } 3522 NFSEXITCODE(error); 3523 return (error); 3524 } 3525 3526 /* 3527 * Get a vnode for a file handle and export stuff. 3528 */ 3529 int 3530 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 3531 int lktype, struct vnode **vpp, struct nfsexstuff *exp, 3532 struct ucred **credp) 3533 { 3534 int error; 3535 3536 *credp = NULL; 3537 exp->nes_numsecflavor = 0; 3538 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp); 3539 if (error != 0) 3540 /* Make sure the server replies ESTALE to the client. */ 3541 error = ESTALE; 3542 if (nam && !error) { 3543 MNT_ILOCK(mp); 3544 if (mp->mnt_exjail == NULL || 3545 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison) 3546 error = EACCES; 3547 MNT_IUNLOCK(mp); 3548 if (error == 0) 3549 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 3550 &exp->nes_numsecflavor, exp->nes_secflavors); 3551 if (error) { 3552 if (NFSD_VNET(nfs_rootfhset)) { 3553 exp->nes_exflag = 0; 3554 exp->nes_numsecflavor = 0; 3555 error = 0; 3556 } else { 3557 vput(*vpp); 3558 } 3559 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor > 3560 MAXSECFLAVORS) { 3561 printf("nfsvno_fhtovp: numsecflavors out of range\n"); 3562 exp->nes_numsecflavor = 0; 3563 error = EACCES; 3564 vput(*vpp); 3565 } 3566 } 3567 NFSEXITCODE(error); 3568 return (error); 3569 } 3570 3571 /* 3572 * nfsd_fhtovp() - convert a fh to a vnode ptr 3573 * - look up fsid in mount list (if not found ret error) 3574 * - get vp and export rights by calling nfsvno_fhtovp() 3575 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 3576 * for AUTH_SYS 3577 * - if mpp != NULL, return the mount point so that it can 3578 * be used for vn_finished_write() by the caller 3579 */ 3580 void 3581 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 3582 struct vnode **vpp, struct nfsexstuff *exp, 3583 struct mount **mpp, int startwrite, int nextop) 3584 { 3585 struct mount *mp, *mpw; 3586 struct ucred *credanon; 3587 fhandle_t *fhp; 3588 int error; 3589 3590 if (mpp != NULL) 3591 *mpp = NULL; 3592 *vpp = NULL; 3593 fhp = (fhandle_t *)nfp->nfsrvfh_data; 3594 mp = vfs_busyfs(&fhp->fh_fsid); 3595 if (mp == NULL) { 3596 nd->nd_repstat = ESTALE; 3597 goto out; 3598 } 3599 3600 if (startwrite) { 3601 mpw = mp; 3602 error = vn_start_write(NULL, &mpw, V_WAIT); 3603 if (error != 0) { 3604 mpw = NULL; 3605 vfs_unbusy(mp); 3606 nd->nd_repstat = ESTALE; 3607 goto out; 3608 } 3609 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp))) 3610 lktype = LK_EXCLUSIVE; 3611 } else 3612 mpw = NULL; 3613 3614 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 3615 &credanon); 3616 vfs_unbusy(mp); 3617 3618 if (nd->nd_repstat == 0 && 3619 nfp->nfsrvfh_len >= NFSX_MYFH + NFSX_V4NAMEDDIRFH && 3620 nfp->nfsrvfh_len <= NFSX_MYFH + NFSX_V4NAMEDATTRFH) { 3621 if (nfp->nfsrvfh_len == NFSX_MYFH + NFSX_V4NAMEDDIRFH) 3622 vn_irflag_set_cond(*vpp, VIRF_NAMEDDIR); 3623 else 3624 vn_irflag_set_cond(*vpp, VIRF_NAMEDATTR); 3625 } 3626 3627 /* 3628 * For NFSv4 without a pseudo root fs, unexported file handles 3629 * can be returned, so that Lookup works everywhere. 3630 */ 3631 if (!nd->nd_repstat && exp->nes_exflag == 0 && 3632 !(nd->nd_flag & ND_NFSV4)) { 3633 vput(*vpp); 3634 *vpp = NULL; 3635 nd->nd_repstat = EACCES; 3636 } 3637 3638 /* 3639 * Personally, I've never seen any point in requiring a 3640 * reserved port#, since only in the rare case where the 3641 * clients are all boxes with secure system privileges, 3642 * does it provide any enhanced security, but... some people 3643 * believe it to be useful and keep putting this code back in. 3644 * (There is also some "security checker" out there that 3645 * complains if the nfs server doesn't enforce this.) 3646 * However, note the following: 3647 * RFC3530 (NFSv4) specifies that a reserved port# not be 3648 * required. 3649 * RFC2623 recommends that, if a reserved port# is checked for, 3650 * that there be a way to turn that off--> ifdef'd. 3651 */ 3652 #ifdef NFS_REQRSVPORT 3653 if (!nd->nd_repstat) { 3654 struct sockaddr_in *saddr; 3655 struct sockaddr_in6 *saddr6; 3656 3657 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 3658 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 3659 if (!(nd->nd_flag & ND_NFSV4) && 3660 ((saddr->sin_family == AF_INET && 3661 ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 3662 (saddr6->sin6_family == AF_INET6 && 3663 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 3664 vput(*vpp); 3665 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 3666 } 3667 } 3668 #endif /* NFS_REQRSVPORT */ 3669 3670 /* 3671 * Check/setup credentials. 3672 */ 3673 if (!nd->nd_repstat) { 3674 nd->nd_saveduid = nd->nd_cred->cr_uid; 3675 nd->nd_repstat = nfsd_excred(nd, exp, credanon, 3676 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type)); 3677 if (nd->nd_repstat) 3678 vput(*vpp); 3679 } 3680 if (credanon != NULL) 3681 crfree(credanon); 3682 if (nd->nd_repstat) { 3683 vn_finished_write(mpw); 3684 *vpp = NULL; 3685 } else if (mpp != NULL) { 3686 *mpp = mpw; 3687 } 3688 3689 out: 3690 NFSEXITCODE2(0, nd); 3691 } 3692 3693 /* 3694 * glue for fp. 3695 */ 3696 static int 3697 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 3698 { 3699 struct filedesc *fdp; 3700 struct file *fp; 3701 int error = 0; 3702 3703 fdp = p->td_proc->p_fd; 3704 if (fd < 0 || fd >= fdp->fd_nfiles || 3705 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) { 3706 error = EBADF; 3707 goto out; 3708 } 3709 *fpp = fp; 3710 3711 out: 3712 NFSEXITCODE(error); 3713 return (error); 3714 } 3715 3716 /* 3717 * Called from nfssvc() to update the exports list. Just call 3718 * vfs_export(). This has to be done, since the v4 root fake fs isn't 3719 * in the mount list. 3720 */ 3721 int 3722 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 3723 { 3724 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 3725 int error = 0; 3726 struct nameidata nd; 3727 fhandle_t fh; 3728 3729 error = vfs_export(NFSD_VNET(nfsv4root_mnt), &nfsexargp->export, false); 3730 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 3731 NFSD_VNET(nfs_rootfhset) = 0; 3732 else if (error == 0) { 3733 if (nfsexargp->fspec == NULL) { 3734 error = EPERM; 3735 goto out; 3736 } 3737 /* 3738 * If fspec != NULL, this is the v4root path. 3739 */ 3740 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec); 3741 if ((error = namei(&nd)) != 0) 3742 goto out; 3743 error = nfsvno_getfh(nd.ni_vp, &fh, p); 3744 vrele(nd.ni_vp); 3745 if (!error) { 3746 NFSD_VNET(nfs_rootfh).nfsrvfh_len = NFSX_MYFH; 3747 NFSBCOPY((caddr_t)&fh, 3748 NFSD_VNET(nfs_rootfh).nfsrvfh_data, 3749 sizeof (fhandle_t)); 3750 NFSD_VNET(nfs_rootfhset) = 1; 3751 } 3752 } 3753 3754 out: 3755 NFSEXITCODE(error); 3756 return (error); 3757 } 3758 3759 /* 3760 * This function needs to test to see if the system is near its limit 3761 * for memory allocation via malloc() or mget() and return True iff 3762 * either of these resources are near their limit. 3763 * XXX (For now, this is just a stub.) 3764 */ 3765 int nfsrv_testmalloclimit = 0; 3766 int 3767 nfsrv_mallocmget_limit(void) 3768 { 3769 static int printmesg = 0; 3770 static int testval = 1; 3771 3772 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 3773 if ((printmesg++ % 100) == 0) 3774 printf("nfsd: malloc/mget near limit\n"); 3775 return (1); 3776 } 3777 return (0); 3778 } 3779 3780 /* 3781 * BSD specific initialization of a mount point. 3782 */ 3783 void 3784 nfsd_mntinit(void) 3785 { 3786 3787 NFSD_LOCK(); 3788 if (NFSD_VNET(nfsrv_mntinited)) { 3789 NFSD_UNLOCK(); 3790 return; 3791 } 3792 NFSD_VNET(nfsrv_mntinited) = true; 3793 nfsrvd_init(0); 3794 NFSD_UNLOCK(); 3795 3796 NFSD_VNET(nfsv4root_mnt) = malloc(sizeof(struct mount), M_TEMP, 3797 M_WAITOK | M_ZERO); 3798 NFSD_VNET(nfsv4root_mnt)->mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 3799 mtx_init(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx, "nfs4mnt", NULL, MTX_DEF); 3800 lockinit(&NFSD_VNET(nfsv4root_mnt)->mnt_explock, PVFS, "explock", 0, 0); 3801 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelist); 3802 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelist); 3803 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL; 3804 TAILQ_INIT(&NFSD_VNET(nfsv4root_opt)); 3805 TAILQ_INIT(&NFSD_VNET(nfsv4root_newopt)); 3806 NFSD_VNET(nfsv4root_mnt)->mnt_opt = &NFSD_VNET(nfsv4root_opt); 3807 NFSD_VNET(nfsv4root_mnt)->mnt_optnew = &NFSD_VNET(nfsv4root_newopt); 3808 NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelistsize = 0; 3809 NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelistsize = 0; 3810 callout_init(&NFSD_VNET(nfsd_callout), 1); 3811 3812 nfsrvd_initcache(); 3813 nfsd_init(); 3814 } 3815 3816 static void 3817 nfsd_timer(void *arg) 3818 { 3819 struct vnet *vnetp; 3820 3821 vnetp = (struct vnet *)arg; 3822 NFSD_CURVNET_SET_QUIET(vnetp); 3823 nfsrv_servertimer(vnetp); 3824 callout_reset_sbt(&NFSD_VNET(nfsd_callout), SBT_1S, SBT_1S, nfsd_timer, 3825 arg, 0); 3826 NFSD_CURVNET_RESTORE(); 3827 } 3828 3829 /* 3830 * Get a vnode for a file handle, without checking exports, etc. 3831 */ 3832 struct vnode * 3833 nfsvno_getvp(fhandle_t *fhp) 3834 { 3835 struct mount *mp; 3836 struct vnode *vp; 3837 int error; 3838 3839 mp = vfs_busyfs(&fhp->fh_fsid); 3840 if (mp == NULL) 3841 return (NULL); 3842 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 3843 vfs_unbusy(mp); 3844 if (error) 3845 return (NULL); 3846 return (vp); 3847 } 3848 3849 /* 3850 * Do a local VOP_ADVLOCK(). 3851 */ 3852 int 3853 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 3854 u_int64_t end, struct thread *td) 3855 { 3856 int error = 0; 3857 struct flock fl; 3858 u_int64_t tlen; 3859 3860 if (nfsrv_dolocallocks == 0) 3861 goto out; 3862 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked"); 3863 3864 fl.l_whence = SEEK_SET; 3865 fl.l_type = ftype; 3866 fl.l_start = (off_t)first; 3867 if (end == NFS64BITSSET) { 3868 fl.l_len = 0; 3869 } else { 3870 tlen = end - first; 3871 fl.l_len = (off_t)tlen; 3872 } 3873 /* 3874 * For FreeBSD8, the l_pid and l_sysid must be set to the same 3875 * values for all calls, so that all locks will be held by the 3876 * nfsd server. (The nfsd server handles conflicts between the 3877 * various clients.) 3878 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 3879 * bytes, so it can't be put in l_sysid. 3880 */ 3881 if (nfsv4_sysid == 0) 3882 nfsv4_sysid = nlm_acquire_next_sysid(); 3883 fl.l_pid = (pid_t)0; 3884 fl.l_sysid = (int)nfsv4_sysid; 3885 3886 if (ftype == F_UNLCK) 3887 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 3888 (F_POSIX | F_REMOTE)); 3889 else 3890 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 3891 (F_POSIX | F_REMOTE)); 3892 3893 out: 3894 NFSEXITCODE(error); 3895 return (error); 3896 } 3897 3898 /* 3899 * Check the nfsv4 root exports. 3900 */ 3901 int 3902 nfsvno_v4rootexport(struct nfsrv_descript *nd) 3903 { 3904 struct ucred *credanon; 3905 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i; 3906 uint64_t exflags; 3907 3908 error = vfs_stdcheckexp(NFSD_VNET(nfsv4root_mnt), nd->nd_nam, &exflags, 3909 &credanon, &numsecflavor, secflavors); 3910 if (error) { 3911 error = NFSERR_PROGUNAVAIL; 3912 goto out; 3913 } 3914 if (credanon != NULL) 3915 crfree(credanon); 3916 for (i = 0; i < numsecflavor; i++) { 3917 if (secflavors[i] == AUTH_SYS) 3918 nd->nd_flag |= ND_EXAUTHSYS; 3919 else if (secflavors[i] == RPCSEC_GSS_KRB5) 3920 nd->nd_flag |= ND_EXGSS; 3921 else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3922 nd->nd_flag |= ND_EXGSSINTEGRITY; 3923 else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3924 nd->nd_flag |= ND_EXGSSPRIVACY; 3925 } 3926 3927 /* And set ND_EXxx flags for TLS. */ 3928 if ((exflags & MNT_EXTLS) != 0) { 3929 nd->nd_flag |= ND_EXTLS; 3930 if ((exflags & MNT_EXTLSCERT) != 0) 3931 nd->nd_flag |= ND_EXTLSCERT; 3932 if ((exflags & MNT_EXTLSCERTUSER) != 0) 3933 nd->nd_flag |= ND_EXTLSCERTUSER; 3934 } 3935 3936 out: 3937 NFSEXITCODE(error); 3938 return (error); 3939 } 3940 3941 /* 3942 * Nfs server pseudo system call for the nfsd's 3943 */ 3944 /* 3945 * MPSAFE 3946 */ 3947 static int 3948 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3949 { 3950 struct file *fp; 3951 struct nfsd_addsock_args sockarg; 3952 struct nfsd_nfsd_args nfsdarg; 3953 struct nfsd_nfsd_oargs onfsdarg; 3954 struct nfsd_pnfsd_args pnfsdarg; 3955 struct vnode *vp, *nvp, *curdvp; 3956 struct pnfsdsfile *pf; 3957 struct nfsdevice *ds, *fds; 3958 cap_rights_t rights; 3959 int buflen, error, ret; 3960 char *buf, *cp, *cp2, *cp3; 3961 char fname[PNFS_FILENAME_LEN + 1]; 3962 3963 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td)); 3964 if (uap->flag & NFSSVC_NFSDADDSOCK) { 3965 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3966 if (error) 3967 goto out; 3968 /* 3969 * Since we don't know what rights might be required, 3970 * pretend that we need them all. It is better to be too 3971 * careful than too reckless. 3972 */ 3973 error = fget(td, sockarg.sock, 3974 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp); 3975 if (error != 0) 3976 goto out; 3977 if (fp->f_type != DTYPE_SOCKET) { 3978 fdrop(fp, td); 3979 error = EPERM; 3980 goto out; 3981 } 3982 error = nfsrvd_addsock(fp); 3983 fdrop(fp, td); 3984 } else if (uap->flag & NFSSVC_NFSDNFSD) { 3985 if (uap->argp == NULL) { 3986 error = EINVAL; 3987 goto out; 3988 } 3989 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) { 3990 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg)); 3991 if (error == 0) { 3992 nfsdarg.principal = onfsdarg.principal; 3993 nfsdarg.minthreads = onfsdarg.minthreads; 3994 nfsdarg.maxthreads = onfsdarg.maxthreads; 3995 nfsdarg.version = 1; 3996 nfsdarg.addr = NULL; 3997 nfsdarg.addrlen = 0; 3998 nfsdarg.dnshost = NULL; 3999 nfsdarg.dnshostlen = 0; 4000 nfsdarg.dspath = NULL; 4001 nfsdarg.dspathlen = 0; 4002 nfsdarg.mdspath = NULL; 4003 nfsdarg.mdspathlen = 0; 4004 nfsdarg.mirrorcnt = 1; 4005 } 4006 } else 4007 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg)); 4008 if (error) 4009 goto out; 4010 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 && 4011 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 && 4012 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 && 4013 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 && 4014 nfsdarg.mirrorcnt >= 1 && 4015 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS && 4016 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL && 4017 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) { 4018 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d" 4019 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen, 4020 nfsdarg.dspathlen, nfsdarg.dnshostlen, 4021 nfsdarg.mdspathlen, nfsdarg.mirrorcnt); 4022 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK); 4023 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen); 4024 if (error != 0) { 4025 free(cp, M_TEMP); 4026 goto out; 4027 } 4028 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */ 4029 nfsdarg.addr = cp; 4030 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK); 4031 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen); 4032 if (error != 0) { 4033 free(nfsdarg.addr, M_TEMP); 4034 free(cp, M_TEMP); 4035 goto out; 4036 } 4037 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */ 4038 nfsdarg.dnshost = cp; 4039 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK); 4040 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen); 4041 if (error != 0) { 4042 free(nfsdarg.addr, M_TEMP); 4043 free(nfsdarg.dnshost, M_TEMP); 4044 free(cp, M_TEMP); 4045 goto out; 4046 } 4047 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */ 4048 nfsdarg.dspath = cp; 4049 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK); 4050 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen); 4051 if (error != 0) { 4052 free(nfsdarg.addr, M_TEMP); 4053 free(nfsdarg.dnshost, M_TEMP); 4054 free(nfsdarg.dspath, M_TEMP); 4055 free(cp, M_TEMP); 4056 goto out; 4057 } 4058 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */ 4059 nfsdarg.mdspath = cp; 4060 } else { 4061 nfsdarg.addr = NULL; 4062 nfsdarg.addrlen = 0; 4063 nfsdarg.dnshost = NULL; 4064 nfsdarg.dnshostlen = 0; 4065 nfsdarg.dspath = NULL; 4066 nfsdarg.dspathlen = 0; 4067 nfsdarg.mdspath = NULL; 4068 nfsdarg.mdspathlen = 0; 4069 nfsdarg.mirrorcnt = 1; 4070 } 4071 nfsd_timer(NFSD_TD_TO_VNET(td)); 4072 error = nfsrvd_nfsd(td, &nfsdarg); 4073 callout_drain(&NFSD_VNET(nfsd_callout)); 4074 free(nfsdarg.addr, M_TEMP); 4075 free(nfsdarg.dnshost, M_TEMP); 4076 free(nfsdarg.dspath, M_TEMP); 4077 free(nfsdarg.mdspath, M_TEMP); 4078 } else if (uap->flag & NFSSVC_PNFSDS) { 4079 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg)); 4080 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER || 4081 pnfsdarg.op == PNFSDOP_FORCEDELDS)) { 4082 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4083 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1, 4084 NULL); 4085 if (error == 0) 4086 error = nfsrv_deldsserver(pnfsdarg.op, cp, td); 4087 free(cp, M_TEMP); 4088 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) { 4089 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4090 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS; 4091 buf = malloc(buflen, M_TEMP, M_WAITOK); 4092 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1, 4093 NULL); 4094 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error); 4095 if (error == 0 && pnfsdarg.dspath != NULL) { 4096 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4097 error = copyinstr(pnfsdarg.dspath, cp2, 4098 PATH_MAX + 1, NULL); 4099 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n", 4100 error); 4101 } else 4102 cp2 = NULL; 4103 if (error == 0 && pnfsdarg.curdspath != NULL) { 4104 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK); 4105 error = copyinstr(pnfsdarg.curdspath, cp3, 4106 PATH_MAX + 1, NULL); 4107 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n", 4108 error); 4109 } else 4110 cp3 = NULL; 4111 curdvp = NULL; 4112 fds = NULL; 4113 if (error == 0) 4114 error = nfsrv_mdscopymr(cp, cp2, cp3, buf, 4115 &buflen, fname, td, &vp, &nvp, &pf, &ds, 4116 &fds); 4117 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error); 4118 if (error == 0) { 4119 if (pf->dsf_dir >= nfsrv_dsdirsize) { 4120 printf("copymr: dsdir out of range\n"); 4121 pf->dsf_dir = 0; 4122 } 4123 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen); 4124 error = nfsrv_copymr(vp, nvp, 4125 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf, 4126 (struct pnfsdsfile *)buf, 4127 buflen / sizeof(*pf), td->td_ucred, td); 4128 vput(vp); 4129 vput(nvp); 4130 if (fds != NULL && error == 0) { 4131 curdvp = fds->nfsdev_dsdir[pf->dsf_dir]; 4132 ret = vn_lock(curdvp, LK_EXCLUSIVE); 4133 if (ret == 0) { 4134 nfsrv_dsremove(curdvp, fname, 4135 td->td_ucred, td); 4136 NFSVOPUNLOCK(curdvp); 4137 } 4138 } 4139 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error); 4140 } 4141 free(cp, M_TEMP); 4142 free(cp2, M_TEMP); 4143 free(cp3, M_TEMP); 4144 free(buf, M_TEMP); 4145 } 4146 } else { 4147 error = nfssvc_srvcall(td, uap, td->td_ucred); 4148 } 4149 4150 out: 4151 NFSD_CURVNET_RESTORE(); 4152 NFSEXITCODE(error); 4153 return (error); 4154 } 4155 4156 static int 4157 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 4158 { 4159 struct nfsex_args export; 4160 struct nfsex_oldargs oexp; 4161 struct file *fp = NULL; 4162 int stablefd, i, len; 4163 struct nfsd_clid adminrevoke; 4164 struct nfsd_dumplist dumplist; 4165 struct nfsd_dumpclients *dumpclients; 4166 struct nfsd_dumplocklist dumplocklist; 4167 struct nfsd_dumplocks *dumplocks; 4168 struct nameidata nd; 4169 vnode_t vp; 4170 int error = EINVAL, igotlock; 4171 struct proc *procp; 4172 gid_t *grps; 4173 4174 if (uap->flag & NFSSVC_PUBLICFH) { 4175 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 4176 sizeof (fhandle_t)); 4177 error = copyin(uap->argp, 4178 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 4179 if (!error) 4180 nfs_pubfhset = 1; 4181 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) == 4182 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) { 4183 error = copyin(uap->argp,(caddr_t)&export, 4184 sizeof (struct nfsex_args)); 4185 if (!error) { 4186 grps = NULL; 4187 if (export.export.ex_ngroups > NGROUPS_MAX || 4188 export.export.ex_ngroups < 0) 4189 error = EINVAL; 4190 else if (export.export.ex_ngroups > 0) { 4191 grps = malloc(export.export.ex_ngroups * 4192 sizeof(gid_t), M_TEMP, M_WAITOK); 4193 error = copyin(export.export.ex_groups, grps, 4194 export.export.ex_ngroups * sizeof(gid_t)); 4195 export.export.ex_groups = grps; 4196 } else 4197 export.export.ex_groups = NULL; 4198 if (!error) 4199 error = nfsrv_v4rootexport(&export, cred, p); 4200 free(grps, M_TEMP); 4201 } 4202 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) == 4203 NFSSVC_V4ROOTEXPORT) { 4204 error = copyin(uap->argp,(caddr_t)&oexp, 4205 sizeof (struct nfsex_oldargs)); 4206 if (!error) { 4207 memset(&export.export, 0, sizeof(export.export)); 4208 export.export.ex_flags = (uint64_t)oexp.export.ex_flags; 4209 export.export.ex_root = oexp.export.ex_root; 4210 export.export.ex_uid = oexp.export.ex_anon.cr_uid; 4211 export.export.ex_ngroups = 4212 oexp.export.ex_anon.cr_ngroups; 4213 export.export.ex_groups = NULL; 4214 if (export.export.ex_ngroups > XU_NGROUPS || 4215 export.export.ex_ngroups < 0) 4216 error = EINVAL; 4217 else if (export.export.ex_ngroups > 0) { 4218 export.export.ex_groups = malloc( 4219 export.export.ex_ngroups * sizeof(gid_t), 4220 M_TEMP, M_WAITOK); 4221 for (i = 0; i < export.export.ex_ngroups; i++) 4222 export.export.ex_groups[i] = 4223 oexp.export.ex_anon.cr_groups[i]; 4224 } 4225 export.export.ex_addr = oexp.export.ex_addr; 4226 export.export.ex_addrlen = oexp.export.ex_addrlen; 4227 export.export.ex_mask = oexp.export.ex_mask; 4228 export.export.ex_masklen = oexp.export.ex_masklen; 4229 export.export.ex_indexfile = oexp.export.ex_indexfile; 4230 export.export.ex_numsecflavors = 4231 oexp.export.ex_numsecflavors; 4232 if (export.export.ex_numsecflavors >= MAXSECFLAVORS || 4233 export.export.ex_numsecflavors < 0) 4234 error = EINVAL; 4235 else { 4236 for (i = 0; i < export.export.ex_numsecflavors; 4237 i++) 4238 export.export.ex_secflavors[i] = 4239 oexp.export.ex_secflavors[i]; 4240 } 4241 export.fspec = oexp.fspec; 4242 if (error == 0) 4243 error = nfsrv_v4rootexport(&export, cred, p); 4244 free(export.export.ex_groups, M_TEMP); 4245 } 4246 } else if (uap->flag & NFSSVC_NOPUBLICFH) { 4247 nfs_pubfhset = 0; 4248 error = 0; 4249 } else if (uap->flag & NFSSVC_STABLERESTART) { 4250 error = copyin(uap->argp, (caddr_t)&stablefd, 4251 sizeof (int)); 4252 if (!error) 4253 error = fp_getfvp(p, stablefd, &fp, &vp); 4254 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 4255 error = EBADF; 4256 if (!error && NFSD_VNET(nfsrv_numnfsd) != 0) 4257 error = ENXIO; 4258 if (!error) { 4259 NFSD_VNET(nfsrv_stablefirst).nsf_fp = fp; 4260 nfsrv_setupstable(p); 4261 } 4262 } else if (uap->flag & NFSSVC_ADMINREVOKE) { 4263 error = copyin(uap->argp, (caddr_t)&adminrevoke, 4264 sizeof (struct nfsd_clid)); 4265 if (!error) 4266 error = nfsrv_adminrevoke(&adminrevoke, p); 4267 } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 4268 error = copyin(uap->argp, (caddr_t)&dumplist, 4269 sizeof (struct nfsd_dumplist)); 4270 if (!error && (dumplist.ndl_size < 1 || 4271 dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 4272 error = EPERM; 4273 if (!error) { 4274 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 4275 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 4276 nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 4277 error = copyout(dumpclients, dumplist.ndl_list, len); 4278 free(dumpclients, M_TEMP); 4279 } 4280 } else if (uap->flag & NFSSVC_DUMPLOCKS) { 4281 error = copyin(uap->argp, (caddr_t)&dumplocklist, 4282 sizeof (struct nfsd_dumplocklist)); 4283 if (!error && (dumplocklist.ndllck_size < 1 || 4284 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 4285 error = EPERM; 4286 if (!error) 4287 error = nfsrv_lookupfilename(&nd, 4288 dumplocklist.ndllck_fname, p); 4289 if (!error) { 4290 len = sizeof (struct nfsd_dumplocks) * 4291 dumplocklist.ndllck_size; 4292 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 4293 nfsrv_dumplocks(nd.ni_vp, dumplocks, 4294 dumplocklist.ndllck_size, p); 4295 vput(nd.ni_vp); 4296 error = copyout(dumplocks, dumplocklist.ndllck_list, 4297 len); 4298 free(dumplocks, M_TEMP); 4299 } 4300 } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 4301 procp = p->td_proc; 4302 PROC_LOCK(procp); 4303 nfsd_master_pid = procp->p_pid; 4304 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 4305 nfsd_master_start = procp->p_stats->p_start; 4306 NFSD_VNET(nfsd_master_proc) = procp; 4307 PROC_UNLOCK(procp); 4308 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) { 4309 NFSLOCKV4ROOTMUTEX(); 4310 if (!NFSD_VNET(nfsrv_suspend_nfsd)) { 4311 /* Lock out all nfsd threads */ 4312 do { 4313 igotlock = nfsv4_lock( 4314 &NFSD_VNET(nfsd_suspend_lock), 1, NULL, 4315 NFSV4ROOTLOCKMUTEXPTR, NULL); 4316 } while (igotlock == 0 && 4317 !NFSD_VNET(nfsrv_suspend_nfsd)); 4318 NFSD_VNET(nfsrv_suspend_nfsd) = true; 4319 } 4320 NFSUNLOCKV4ROOTMUTEX(); 4321 error = 0; 4322 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) { 4323 NFSLOCKV4ROOTMUTEX(); 4324 if (NFSD_VNET(nfsrv_suspend_nfsd)) { 4325 nfsv4_unlock(&NFSD_VNET(nfsd_suspend_lock), 0); 4326 NFSD_VNET(nfsrv_suspend_nfsd) = false; 4327 } 4328 NFSUNLOCKV4ROOTMUTEX(); 4329 error = 0; 4330 } 4331 4332 NFSEXITCODE(error); 4333 return (error); 4334 } 4335 4336 /* 4337 * Check exports. 4338 * Returns 0 if ok, 1 otherwise. 4339 */ 4340 int 4341 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 4342 { 4343 int i; 4344 4345 if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) || 4346 (NFSVNO_EXTLSCERT(exp) && 4347 (nd->nd_flag & ND_TLSCERT) == 0) || 4348 (NFSVNO_EXTLSCERTUSER(exp) && 4349 (nd->nd_flag & ND_TLSCERTUSER) == 0)) { 4350 if ((nd->nd_flag & ND_NFSV4) != 0) 4351 return (NFSERR_WRONGSEC); 4352 #ifdef notnow 4353 /* There is currently no auth_stat for this. */ 4354 else if ((nd->nd_flag & ND_TLS) == 0) 4355 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS); 4356 else 4357 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST); 4358 #endif 4359 else 4360 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 4361 } 4362 4363 /* 4364 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use 4365 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 4366 */ 4367 if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO) 4368 return (0); 4369 4370 /* 4371 * This seems odd, but allow the case where the security flavor 4372 * list is empty. This happens when NFSv4 is traversing non-exported 4373 * file systems. Exported file systems should always have a non-empty 4374 * security flavor list. 4375 */ 4376 if (exp->nes_numsecflavor == 0) 4377 return (0); 4378 4379 for (i = 0; i < exp->nes_numsecflavor; i++) { 4380 /* 4381 * The tests for privacy and integrity must be first, 4382 * since ND_GSS is set for everything but AUTH_SYS. 4383 */ 4384 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 4385 (nd->nd_flag & ND_GSSPRIVACY)) 4386 return (0); 4387 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 4388 (nd->nd_flag & ND_GSSINTEGRITY)) 4389 return (0); 4390 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 4391 (nd->nd_flag & ND_GSS)) 4392 return (0); 4393 if (exp->nes_secflavors[i] == AUTH_SYS && 4394 (nd->nd_flag & ND_GSS) == 0) 4395 return (0); 4396 } 4397 if ((nd->nd_flag & ND_NFSV4) != 0) 4398 return (NFSERR_WRONGSEC); 4399 return (NFSERR_AUTHERR | AUTH_TOOWEAK); 4400 } 4401 4402 /* 4403 * Calculate a hash value for the fid in a file handle. 4404 */ 4405 uint32_t 4406 nfsrv_hashfh(fhandle_t *fhp) 4407 { 4408 uint32_t hashval; 4409 4410 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 4411 return (hashval); 4412 } 4413 4414 /* 4415 * Calculate a hash value for the sessionid. 4416 */ 4417 uint32_t 4418 nfsrv_hashsessionid(uint8_t *sessionid) 4419 { 4420 uint32_t hashval; 4421 4422 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0); 4423 return (hashval); 4424 } 4425 4426 /* 4427 * Signal the userland master nfsd to backup the stable restart file. 4428 */ 4429 void 4430 nfsrv_backupstable(void) 4431 { 4432 struct proc *procp; 4433 4434 if (NFSD_VNET(nfsd_master_proc) != NULL) { 4435 procp = pfind(nfsd_master_pid); 4436 /* Try to make sure it is the correct process. */ 4437 if (procp == NFSD_VNET(nfsd_master_proc) && 4438 procp->p_stats->p_start.tv_sec == 4439 nfsd_master_start.tv_sec && 4440 procp->p_stats->p_start.tv_usec == 4441 nfsd_master_start.tv_usec && 4442 strcmp(procp->p_comm, nfsd_master_comm) == 0) 4443 kern_psignal(procp, SIGUSR2); 4444 else 4445 NFSD_VNET(nfsd_master_proc) = NULL; 4446 4447 if (procp != NULL) 4448 PROC_UNLOCK(procp); 4449 } 4450 } 4451 4452 /* 4453 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror. 4454 * The arguments are in a structure, so that they can be passed through 4455 * taskqueue for a kernel process to execute this function. 4456 */ 4457 struct nfsrvdscreate { 4458 int done; 4459 int inprog; 4460 struct task tsk; 4461 struct ucred *tcred; 4462 struct vnode *dvp; 4463 NFSPROC_T *p; 4464 struct pnfsdsfile *pf; 4465 int err; 4466 fhandle_t fh; 4467 struct vattr va; 4468 struct vattr createva; 4469 }; 4470 4471 int 4472 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap, 4473 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa, 4474 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp) 4475 { 4476 struct vnode *nvp; 4477 struct nameidata named; 4478 struct vattr va; 4479 char *bufp; 4480 u_long *hashp; 4481 struct nfsnode *np; 4482 struct nfsmount *nmp; 4483 int error; 4484 4485 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE, 4486 LOCKPARENT | LOCKLEAF | NOCACHE); 4487 nfsvno_setpathbuf(&named, &bufp, &hashp); 4488 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE; 4489 named.ni_cnd.cn_nameptr = bufp; 4490 if (fnamep != NULL) { 4491 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1); 4492 named.ni_cnd.cn_namelen = strlen(bufp); 4493 } else 4494 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp); 4495 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp); 4496 4497 /* Create the date file in the DS mount. */ 4498 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE); 4499 if (error == 0) { 4500 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap); 4501 vref(dvp); 4502 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false); 4503 if (error == 0) { 4504 /* Set the ownership of the file. */ 4505 error = VOP_SETATTR(nvp, nvap, tcred); 4506 NFSD_DEBUG(4, "nfsrv_dscreate:" 4507 " setattr-uid=%d\n", error); 4508 if (error != 0) 4509 vput(nvp); 4510 } 4511 if (error != 0) 4512 printf("pNFS: pnfscreate failed=%d\n", error); 4513 } else 4514 printf("pNFS: pnfscreate vnlock=%d\n", error); 4515 if (error == 0) { 4516 np = VTONFS(nvp); 4517 nmp = VFSTONFS(nvp->v_mount); 4518 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs") 4519 != 0 || nmp->nm_nam->sa_len > sizeof( 4520 struct sockaddr_in6) || 4521 np->n_fhp->nfh_len != NFSX_MYFH) { 4522 printf("Bad DS file: fstype=%s salen=%d" 4523 " fhlen=%d\n", 4524 nvp->v_mount->mnt_vfc->vfc_name, 4525 nmp->nm_nam->sa_len, np->n_fhp->nfh_len); 4526 error = ENOENT; 4527 } 4528 4529 /* Set extattrs for the DS on the MDS file. */ 4530 if (error == 0) { 4531 if (dsa != NULL) { 4532 error = VOP_GETATTR(nvp, &va, tcred); 4533 if (error == 0) { 4534 dsa->dsa_filerev = va.va_filerev; 4535 dsa->dsa_size = va.va_size; 4536 dsa->dsa_atime = va.va_atime; 4537 dsa->dsa_mtime = va.va_mtime; 4538 dsa->dsa_bytes = va.va_bytes; 4539 } 4540 } 4541 if (error == 0) { 4542 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, 4543 NFSX_MYFH); 4544 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin, 4545 nmp->nm_nam->sa_len); 4546 NFSBCOPY(named.ni_cnd.cn_nameptr, 4547 pf->dsf_filename, 4548 sizeof(pf->dsf_filename)); 4549 } 4550 } else 4551 printf("pNFS: pnfscreate can't get DS" 4552 " attr=%d\n", error); 4553 if (nvpp != NULL && error == 0) 4554 *nvpp = nvp; 4555 else 4556 vput(nvp); 4557 } 4558 nfsvno_relpathbuf(&named); 4559 return (error); 4560 } 4561 4562 /* 4563 * Start up the thread that will execute nfsrv_dscreate(). 4564 */ 4565 static void 4566 start_dscreate(void *arg, int pending) 4567 { 4568 struct nfsrvdscreate *dsc; 4569 4570 dsc = (struct nfsrvdscreate *)arg; 4571 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh, 4572 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL); 4573 dsc->done = 1; 4574 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err); 4575 } 4576 4577 /* 4578 * Create a pNFS data file on the Data Server(s). 4579 */ 4580 static void 4581 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred, 4582 NFSPROC_T *p) 4583 { 4584 struct nfsrvdscreate *dsc, *tdsc = NULL; 4585 struct nfsdevice *ds, *tds, *fds; 4586 struct mount *mp; 4587 struct pnfsdsfile *pf, *tpf; 4588 struct pnfsdsattr dsattr; 4589 struct vattr va; 4590 struct vnode *dvp[NFSDEV_MAXMIRRORS]; 4591 struct nfsmount *nmp; 4592 fhandle_t fh; 4593 uid_t vauid; 4594 gid_t vagid; 4595 u_short vamode; 4596 struct ucred *tcred; 4597 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret; 4598 int failpos, timo; 4599 4600 /* Get a DS server directory in a round-robin order. */ 4601 mirrorcnt = 1; 4602 mp = vp->v_mount; 4603 ds = fds = NULL; 4604 NFSDDSLOCK(); 4605 /* 4606 * Search for the first entry that handles this MDS fs, but use the 4607 * first entry for all MDS fs's otherwise. 4608 */ 4609 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) { 4610 if (tds->nfsdev_nmp != NULL) { 4611 if (tds->nfsdev_mdsisset == 0 && ds == NULL) 4612 ds = tds; 4613 else if (tds->nfsdev_mdsisset != 0 && fsidcmp( 4614 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) { 4615 ds = fds = tds; 4616 break; 4617 } 4618 } 4619 } 4620 if (ds == NULL) { 4621 NFSDDSUNLOCK(); 4622 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n"); 4623 return; 4624 } 4625 i = dsdir[0] = ds->nfsdev_nextdir; 4626 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize; 4627 dvp[0] = ds->nfsdev_dsdir[i]; 4628 tds = TAILQ_NEXT(ds, nfsdev_list); 4629 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) { 4630 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) { 4631 if (tds->nfsdev_nmp != NULL && 4632 ((tds->nfsdev_mdsisset == 0 && fds == NULL) || 4633 (tds->nfsdev_mdsisset != 0 && fds != NULL && 4634 fsidcmp(&mp->mnt_stat.f_fsid, 4635 &tds->nfsdev_mdsfsid) == 0))) { 4636 dsdir[mirrorcnt] = i; 4637 dvp[mirrorcnt] = tds->nfsdev_dsdir[i]; 4638 mirrorcnt++; 4639 if (mirrorcnt >= nfsrv_maxpnfsmirror) 4640 break; 4641 } 4642 } 4643 } 4644 /* Put at end of list to implement round-robin usage. */ 4645 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list); 4646 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list); 4647 NFSDDSUNLOCK(); 4648 dsc = NULL; 4649 if (mirrorcnt > 1) 4650 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP, 4651 M_WAITOK | M_ZERO); 4652 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK | 4653 M_ZERO); 4654 4655 error = nfsvno_getfh(vp, &fh, p); 4656 if (error == 0) 4657 error = VOP_GETATTR(vp, &va, cred); 4658 if (error == 0) { 4659 /* Set the attributes for "vp" to Setattr the DS vp. */ 4660 vauid = va.va_uid; 4661 vagid = va.va_gid; 4662 vamode = va.va_mode; 4663 VATTR_NULL(&va); 4664 va.va_uid = vauid; 4665 va.va_gid = vagid; 4666 va.va_mode = vamode; 4667 va.va_size = 0; 4668 } else 4669 printf("pNFS: pnfscreate getfh+attr=%d\n", error); 4670 4671 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid, 4672 cred->cr_gid); 4673 /* Make data file name based on FH. */ 4674 tcred = newnfs_getcred(); 4675 4676 /* 4677 * Create the file on each DS mirror, using kernel process(es) for the 4678 * additional mirrors. 4679 */ 4680 failpos = -1; 4681 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) { 4682 tpf->dsf_dir = dsdir[i]; 4683 tdsc->tcred = tcred; 4684 tdsc->p = p; 4685 tdsc->pf = tpf; 4686 tdsc->createva = *vap; 4687 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh)); 4688 tdsc->va = va; 4689 tdsc->dvp = dvp[i]; 4690 tdsc->done = 0; 4691 tdsc->inprog = 0; 4692 tdsc->err = 0; 4693 ret = EIO; 4694 if (nfs_pnfsiothreads != 0) { 4695 ret = nfs_pnfsio(start_dscreate, tdsc); 4696 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret); 4697 } 4698 if (ret != 0) { 4699 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL, 4700 NULL, tcred, p, NULL); 4701 if (ret != 0) { 4702 KASSERT(error == 0, ("nfsrv_dscreate err=%d", 4703 error)); 4704 if (failpos == -1 && nfsds_failerr(ret)) 4705 failpos = i; 4706 else 4707 error = ret; 4708 } 4709 } 4710 } 4711 if (error == 0) { 4712 tpf->dsf_dir = dsdir[mirrorcnt - 1]; 4713 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf, 4714 &dsattr, NULL, tcred, p, NULL); 4715 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) { 4716 failpos = mirrorcnt - 1; 4717 error = 0; 4718 } 4719 } 4720 timo = hz / 50; /* Wait for 20msec. */ 4721 if (timo < 1) 4722 timo = 1; 4723 /* Wait for kernel task(s) to complete. */ 4724 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) { 4725 while (tdsc->inprog != 0 && tdsc->done == 0) 4726 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo); 4727 if (tdsc->err != 0) { 4728 if (failpos == -1 && nfsds_failerr(tdsc->err)) 4729 failpos = i; 4730 else if (error == 0) 4731 error = tdsc->err; 4732 } 4733 } 4734 4735 /* 4736 * If failpos has been set, that mirror has failed, so it needs 4737 * to be disabled. 4738 */ 4739 if (failpos >= 0) { 4740 nmp = VFSTONFS(dvp[failpos]->v_mount); 4741 NFSLOCKMNT(nmp); 4742 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 4743 NFSMNTP_CANCELRPCS)) == 0) { 4744 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4745 NFSUNLOCKMNT(nmp); 4746 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p); 4747 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos, 4748 ds); 4749 if (ds != NULL) 4750 nfsrv_killrpcs(nmp); 4751 NFSLOCKMNT(nmp); 4752 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4753 wakeup(nmp); 4754 } 4755 NFSUNLOCKMNT(nmp); 4756 } 4757 4758 NFSFREECRED(tcred); 4759 if (error == 0) { 4760 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp"); 4761 4762 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n", 4763 mirrorcnt, nfsrv_maxpnfsmirror); 4764 /* 4765 * For all mirrors that couldn't be created, fill in the 4766 * *pf structure, but with an IP address == 0.0.0.0. 4767 */ 4768 tpf = pf + mirrorcnt; 4769 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) { 4770 *tpf = *pf; 4771 tpf->dsf_sin.sin_family = AF_INET; 4772 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in); 4773 tpf->dsf_sin.sin_addr.s_addr = 0; 4774 tpf->dsf_sin.sin_port = 0; 4775 } 4776 4777 error = vn_extattr_set(vp, IO_NODELOCKED, 4778 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", 4779 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p); 4780 if (error == 0) 4781 error = vn_extattr_set(vp, IO_NODELOCKED, 4782 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", 4783 sizeof(dsattr), (char *)&dsattr, p); 4784 if (error != 0) 4785 printf("pNFS: pnfscreate setextattr=%d\n", 4786 error); 4787 } else 4788 printf("pNFS: pnfscreate=%d\n", error); 4789 free(pf, M_TEMP); 4790 free(dsc, M_TEMP); 4791 } 4792 4793 /* 4794 * Get the information needed to remove the pNFS Data Server file from the 4795 * Metadata file. Upon success, ddvp is set non-NULL to the locked 4796 * DS directory vnode. The caller must unlock *ddvp when done with it. 4797 */ 4798 static void 4799 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp, 4800 int *mirrorcntp, char *fname, fhandle_t *fhp) 4801 { 4802 struct vattr va; 4803 struct ucred *tcred; 4804 char *buf; 4805 int buflen, error; 4806 4807 dvpp[0] = NULL; 4808 /* If not an exported regular file or not a pNFS server, just return. */ 4809 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 || 4810 nfsrv_devidcnt == 0) 4811 return; 4812 4813 /* Check to see if this is the last hard link. */ 4814 tcred = newnfs_getcred(); 4815 error = VOP_GETATTR(vp, &va, tcred); 4816 NFSFREECRED(tcred); 4817 if (error != 0) { 4818 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error); 4819 return; 4820 } 4821 if (va.va_nlink > 1) 4822 return; 4823 4824 error = nfsvno_getfh(vp, fhp, p); 4825 if (error != 0) { 4826 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error); 4827 return; 4828 } 4829 4830 buflen = 1024; 4831 buf = malloc(buflen, M_TEMP, M_WAITOK); 4832 /* Get the directory vnode for the DS mount and the file handle. */ 4833 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp, 4834 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL); 4835 free(buf, M_TEMP); 4836 if (error != 0) 4837 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error); 4838 } 4839 4840 /* 4841 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror. 4842 * The arguments are in a structure, so that they can be passed through 4843 * taskqueue for a kernel process to execute this function. 4844 */ 4845 struct nfsrvdsremove { 4846 int done; 4847 int inprog; 4848 struct task tsk; 4849 struct ucred *tcred; 4850 struct vnode *dvp; 4851 NFSPROC_T *p; 4852 int err; 4853 char fname[PNFS_FILENAME_LEN + 1]; 4854 }; 4855 4856 static int 4857 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred, 4858 NFSPROC_T *p) 4859 { 4860 struct nameidata named; 4861 struct vnode *nvp; 4862 char *bufp; 4863 u_long *hashp; 4864 int error; 4865 4866 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE); 4867 if (error != 0) 4868 return (error); 4869 named.ni_cnd.cn_nameiop = DELETE; 4870 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY; 4871 named.ni_cnd.cn_cred = tcred; 4872 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF; 4873 nfsvno_setpathbuf(&named, &bufp, &hashp); 4874 named.ni_cnd.cn_nameptr = bufp; 4875 named.ni_cnd.cn_namelen = strlen(fname); 4876 strlcpy(bufp, fname, NAME_MAX); 4877 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp); 4878 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd); 4879 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error); 4880 if (error == 0) { 4881 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd); 4882 vput(nvp); 4883 } 4884 NFSVOPUNLOCK(dvp); 4885 nfsvno_relpathbuf(&named); 4886 if (error != 0) 4887 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error); 4888 return (error); 4889 } 4890 4891 /* 4892 * Start up the thread that will execute nfsrv_dsremove(). 4893 */ 4894 static void 4895 start_dsremove(void *arg, int pending) 4896 { 4897 struct nfsrvdsremove *dsrm; 4898 4899 dsrm = (struct nfsrvdsremove *)arg; 4900 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred, 4901 dsrm->p); 4902 dsrm->done = 1; 4903 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err); 4904 } 4905 4906 /* 4907 * Remove a pNFS data file from a Data Server. 4908 * nfsrv_pnfsremovesetup() must have been called before the MDS file was 4909 * removed to set up the dvp and fill in the FH. 4910 */ 4911 static void 4912 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp, 4913 NFSPROC_T *p) 4914 { 4915 struct ucred *tcred; 4916 struct nfsrvdsremove *dsrm, *tdsrm; 4917 struct nfsdevice *ds; 4918 struct nfsmount *nmp; 4919 int failpos, i, ret, timo; 4920 4921 tcred = newnfs_getcred(); 4922 dsrm = NULL; 4923 if (mirrorcnt > 1) 4924 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK); 4925 /* 4926 * Remove the file on each DS mirror, using kernel process(es) for the 4927 * additional mirrors. 4928 */ 4929 failpos = -1; 4930 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) { 4931 tdsrm->tcred = tcred; 4932 tdsrm->p = p; 4933 tdsrm->dvp = dvp[i]; 4934 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1); 4935 tdsrm->inprog = 0; 4936 tdsrm->done = 0; 4937 tdsrm->err = 0; 4938 ret = EIO; 4939 if (nfs_pnfsiothreads != 0) { 4940 ret = nfs_pnfsio(start_dsremove, tdsrm); 4941 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret); 4942 } 4943 if (ret != 0) { 4944 ret = nfsrv_dsremove(dvp[i], fname, tcred, p); 4945 if (failpos == -1 && nfsds_failerr(ret)) 4946 failpos = i; 4947 } 4948 } 4949 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p); 4950 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret)) 4951 failpos = mirrorcnt - 1; 4952 timo = hz / 50; /* Wait for 20msec. */ 4953 if (timo < 1) 4954 timo = 1; 4955 /* Wait for kernel task(s) to complete. */ 4956 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) { 4957 while (tdsrm->inprog != 0 && tdsrm->done == 0) 4958 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo); 4959 if (failpos == -1 && nfsds_failerr(tdsrm->err)) 4960 failpos = i; 4961 } 4962 4963 /* 4964 * If failpos has been set, that mirror has failed, so it needs 4965 * to be disabled. 4966 */ 4967 if (failpos >= 0) { 4968 nmp = VFSTONFS(dvp[failpos]->v_mount); 4969 NFSLOCKMNT(nmp); 4970 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM | 4971 NFSMNTP_CANCELRPCS)) == 0) { 4972 nmp->nm_privflag |= NFSMNTP_CANCELRPCS; 4973 NFSUNLOCKMNT(nmp); 4974 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p); 4975 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos, 4976 ds); 4977 if (ds != NULL) 4978 nfsrv_killrpcs(nmp); 4979 NFSLOCKMNT(nmp); 4980 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 4981 wakeup(nmp); 4982 } 4983 NFSUNLOCKMNT(nmp); 4984 } 4985 4986 /* Get rid all layouts for the file. */ 4987 nfsrv_freefilelayouts(fhp); 4988 4989 NFSFREECRED(tcred); 4990 free(dsrm, M_TEMP); 4991 } 4992 4993 /* 4994 * Generate a file name based on the file handle and put it in *bufp. 4995 * Return the number of bytes generated. 4996 */ 4997 static int 4998 nfsrv_putfhname(fhandle_t *fhp, char *bufp) 4999 { 5000 int i; 5001 uint8_t *cp; 5002 const uint8_t *hexdigits = "0123456789abcdef"; 5003 5004 cp = (uint8_t *)fhp; 5005 for (i = 0; i < sizeof(*fhp); i++) { 5006 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf]; 5007 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf]; 5008 } 5009 bufp[2 * i] = '\0'; 5010 return (2 * i); 5011 } 5012 5013 /* 5014 * Update the Metadata file's attributes from the DS file when a Read/Write 5015 * layout is returned. 5016 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN 5017 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file. 5018 */ 5019 int 5020 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p) 5021 { 5022 struct ucred *tcred; 5023 int error; 5024 5025 /* Do this as root so that it won't fail with EACCES. */ 5026 tcred = newnfs_getcred(); 5027 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN, 5028 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL); 5029 NFSFREECRED(tcred); 5030 return (error); 5031 } 5032 5033 /* 5034 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file. 5035 */ 5036 static int 5037 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred, 5038 NFSPROC_T *p) 5039 { 5040 int error; 5041 5042 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL, 5043 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL); 5044 return (error); 5045 } 5046 5047 static int 5048 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 5049 struct thread *p, int ioproc, struct mbuf **mpp, char *cp, 5050 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp, 5051 off_t *offp, int content, bool *eofp) 5052 { 5053 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp; 5054 fhandle_t fh[NFSDEV_MAXMIRRORS]; 5055 struct vnode *dvp[NFSDEV_MAXMIRRORS]; 5056 struct nfsdevice *ds; 5057 struct pnfsdsattr dsattr; 5058 struct opnfsdsattr odsattr; 5059 char *buf; 5060 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt; 5061 5062 NFSD_DEBUG(4, "in nfsrv_proxyds\n"); 5063 /* 5064 * If not a regular file, not exported or not a pNFS server, 5065 * just return ENOENT. 5066 */ 5067 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 || 5068 nfsrv_devidcnt == 0) 5069 return (ENOENT); 5070 5071 buflen = 1024; 5072 buf = malloc(buflen, M_TEMP, M_WAITOK); 5073 error = 0; 5074 5075 /* 5076 * For Getattr, get the Change attribute (va_filerev) and size (va_size) 5077 * from the MetaData file's extended attribute. 5078 */ 5079 if (ioproc == NFSPROC_GETATTR) { 5080 error = vn_extattr_get(vp, IO_NODELOCKED, 5081 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf, 5082 p); 5083 if (error == 0) { 5084 if (buflen == sizeof(odsattr)) { 5085 NFSBCOPY(buf, &odsattr, buflen); 5086 nap->na_filerev = odsattr.dsa_filerev; 5087 nap->na_size = odsattr.dsa_size; 5088 nap->na_atime = odsattr.dsa_atime; 5089 nap->na_mtime = odsattr.dsa_mtime; 5090 /* 5091 * Fake na_bytes by rounding up na_size. 5092 * Since we don't know the block size, just 5093 * use BLKDEV_IOSIZE. 5094 */ 5095 nap->na_bytes = (odsattr.dsa_size + 5096 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1); 5097 } else if (buflen == sizeof(dsattr)) { 5098 NFSBCOPY(buf, &dsattr, buflen); 5099 nap->na_filerev = dsattr.dsa_filerev; 5100 nap->na_size = dsattr.dsa_size; 5101 nap->na_atime = dsattr.dsa_atime; 5102 nap->na_mtime = dsattr.dsa_mtime; 5103 nap->na_bytes = dsattr.dsa_bytes; 5104 } else 5105 error = ENXIO; 5106 } 5107 if (error == 0) { 5108 /* 5109 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr() 5110 * returns 0, just return now. nfsrv_checkdsattr() 5111 * returns 0 if there is no Read/Write layout 5112 * plus either an Open/Write_access or Write 5113 * delegation issued to a client for the file. 5114 */ 5115 if (nfsrv_pnfsgetdsattr == 0 || 5116 nfsrv_checkdsattr(vp, p) == 0) { 5117 free(buf, M_TEMP); 5118 return (error); 5119 } 5120 } 5121 5122 /* 5123 * Clear ENOATTR so the code below will attempt to do a 5124 * nfsrv_getattrdsrpc() to get the attributes and (re)create 5125 * the extended attribute. 5126 */ 5127 if (error == ENOATTR) 5128 error = 0; 5129 } 5130 5131 origmircnt = -1; 5132 trycnt = 0; 5133 tryagain: 5134 if (error == 0) { 5135 buflen = 1024; 5136 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) == 5137 LK_EXCLUSIVE) 5138 printf("nfsrv_proxyds: Readds vp exclusively locked\n"); 5139 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen, 5140 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL, 5141 NULL, NULL); 5142 if (error == 0) { 5143 for (i = 0; i < mirrorcnt; i++) 5144 nmp[i] = VFSTONFS(dvp[i]->v_mount); 5145 } else 5146 printf("pNFS: proxy getextattr sockaddr=%d\n", error); 5147 } else 5148 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error); 5149 if (error == 0) { 5150 failpos = -1; 5151 if (origmircnt == -1) 5152 origmircnt = mirrorcnt; 5153 /* 5154 * If failpos is set to a mirror#, then that mirror has 5155 * failed and will be disabled. For Read, Getattr and Seek, the 5156 * function only tries one mirror, so if that mirror has 5157 * failed, it will need to be retried. As such, increment 5158 * tryitagain for these cases. 5159 * For Write, Setattr and Setacl, the function tries all 5160 * mirrors and will not return an error for the case where 5161 * one mirror has failed. For these cases, the functioning 5162 * mirror(s) will have been modified, so a retry isn't 5163 * necessary. These functions will set failpos for the 5164 * failed mirror#. 5165 */ 5166 if (ioproc == NFSPROC_READDS) { 5167 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0], 5168 mpp, mpp2); 5169 if (nfsds_failerr(error) && mirrorcnt > 1) { 5170 /* 5171 * Setting failpos will cause the mirror 5172 * to be disabled and then a retry of this 5173 * read is required. 5174 */ 5175 failpos = 0; 5176 error = 0; 5177 trycnt++; 5178 } 5179 } else if (ioproc == NFSPROC_WRITEDS) 5180 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp, 5181 &nmp[0], mirrorcnt, mpp, cp, &failpos); 5182 else if (ioproc == NFSPROC_SETATTR) 5183 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0], 5184 mirrorcnt, nap, &failpos); 5185 else if (ioproc == NFSPROC_SETACL) 5186 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0], 5187 mirrorcnt, aclp, &failpos); 5188 else if (ioproc == NFSPROC_SEEKDS) { 5189 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred, 5190 p, nmp[0]); 5191 if (nfsds_failerr(error) && mirrorcnt > 1) { 5192 /* 5193 * Setting failpos will cause the mirror 5194 * to be disabled and then a retry of this 5195 * read is required. 5196 */ 5197 failpos = 0; 5198 error = 0; 5199 trycnt++; 5200 } 5201 } else if (ioproc == NFSPROC_ALLOCATE) 5202 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp, 5203 &nmp[0], mirrorcnt, &failpos); 5204 else if (ioproc == NFSPROC_DEALLOCATE) 5205 error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p, 5206 vp, &nmp[0], mirrorcnt, &failpos); 5207 else { 5208 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p, 5209 vp, nmp[mirrorcnt - 1], nap); 5210 if (nfsds_failerr(error) && mirrorcnt > 1) { 5211 /* 5212 * Setting failpos will cause the mirror 5213 * to be disabled and then a retry of this 5214 * getattr is required. 5215 */ 5216 failpos = mirrorcnt - 1; 5217 error = 0; 5218 trycnt++; 5219 } 5220 } 5221 ds = NULL; 5222 if (failpos >= 0) { 5223 failnmp = nmp[failpos]; 5224 NFSLOCKMNT(failnmp); 5225 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM | 5226 NFSMNTP_CANCELRPCS)) == 0) { 5227 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS; 5228 NFSUNLOCKMNT(failnmp); 5229 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, 5230 failnmp, p); 5231 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n", 5232 failpos, ds); 5233 if (ds != NULL) 5234 nfsrv_killrpcs(failnmp); 5235 NFSLOCKMNT(failnmp); 5236 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS; 5237 wakeup(failnmp); 5238 } 5239 NFSUNLOCKMNT(failnmp); 5240 } 5241 for (i = 0; i < mirrorcnt; i++) 5242 NFSVOPUNLOCK(dvp[i]); 5243 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error, 5244 trycnt); 5245 /* Try the Read/Getattr again if a mirror was deleted. */ 5246 if (ds != NULL && trycnt > 0 && trycnt < origmircnt) 5247 goto tryagain; 5248 } else { 5249 /* Return ENOENT for any Extended Attribute error. */ 5250 error = ENOENT; 5251 } 5252 free(buf, M_TEMP); 5253 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error); 5254 return (error); 5255 } 5256 5257 /* 5258 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended 5259 * attribute. 5260 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs 5261 * to be checked. If it points to a NULL nmp, then it returns 5262 * a suitable destination. 5263 * curnmp - If non-NULL, it is the source mount for the copy. 5264 */ 5265 int 5266 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp, 5267 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp, 5268 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp, 5269 struct nfsmount *curnmp, int *ippos, int *dsdirp) 5270 { 5271 struct vnode *dvp, *nvp = NULL, **tdvpp; 5272 struct mount *mp; 5273 struct nfsmount *nmp, *newnmp; 5274 struct sockaddr *sad; 5275 struct sockaddr_in *sin; 5276 struct nfsdevice *ds, *tds, *fndds; 5277 struct pnfsdsfile *pf; 5278 uint32_t dsdir; 5279 int error, fhiszero, fnd, gotone, i, mirrorcnt; 5280 5281 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp"); 5282 *mirrorcntp = 1; 5283 tdvpp = dvpp; 5284 if (nvpp != NULL) 5285 *nvpp = NULL; 5286 if (dvpp != NULL) 5287 *dvpp = NULL; 5288 if (ippos != NULL) 5289 *ippos = -1; 5290 if (newnmpp != NULL) 5291 newnmp = *newnmpp; 5292 else 5293 newnmp = NULL; 5294 mp = vp->v_mount; 5295 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM, 5296 "pnfsd.dsfile", buflenp, buf, p); 5297 mirrorcnt = *buflenp / sizeof(*pf); 5298 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS || 5299 *buflenp != sizeof(*pf) * mirrorcnt)) 5300 error = ENOATTR; 5301 5302 pf = (struct pnfsdsfile *)buf; 5303 /* If curnmp != NULL, check for a match in the mirror list. */ 5304 if (curnmp != NULL && error == 0) { 5305 fnd = 0; 5306 for (i = 0; i < mirrorcnt; i++, pf++) { 5307 sad = (struct sockaddr *)&pf->dsf_sin; 5308 if (nfsaddr2_match(sad, curnmp->nm_nam)) { 5309 if (ippos != NULL) 5310 *ippos = i; 5311 fnd = 1; 5312 break; 5313 } 5314 } 5315 if (fnd == 0) 5316 error = ENXIO; 5317 } 5318 5319 gotone = 0; 5320 pf = (struct pnfsdsfile *)buf; 5321 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt, 5322 error); 5323 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) { 5324 fhiszero = 0; 5325 sad = (struct sockaddr *)&pf->dsf_sin; 5326 sin = &pf->dsf_sin; 5327 dsdir = pf->dsf_dir; 5328 if (dsdir >= nfsrv_dsdirsize) { 5329 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir); 5330 error = ENOATTR; 5331 } else if (nvpp != NULL && newnmp != NULL && 5332 nfsaddr2_match(sad, newnmp->nm_nam)) 5333 error = EEXIST; 5334 if (error == 0) { 5335 if (ippos != NULL && curnmp == NULL && 5336 sad->sa_family == AF_INET && 5337 sin->sin_addr.s_addr == 0) 5338 *ippos = i; 5339 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0) 5340 fhiszero = 1; 5341 /* Use the socket address to find the mount point. */ 5342 fndds = NULL; 5343 NFSDDSLOCK(); 5344 /* Find a match for the IP address. */ 5345 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 5346 if (ds->nfsdev_nmp != NULL) { 5347 dvp = ds->nfsdev_dvp; 5348 nmp = VFSTONFS(dvp->v_mount); 5349 if (nmp != ds->nfsdev_nmp) 5350 printf("different2 nmp %p %p\n", 5351 nmp, ds->nfsdev_nmp); 5352 if (nfsaddr2_match(sad, nmp->nm_nam)) { 5353 fndds = ds; 5354 break; 5355 } 5356 } 5357 } 5358 if (fndds != NULL && newnmpp != NULL && 5359 newnmp == NULL) { 5360 /* Search for a place to make a mirror copy. */ 5361 TAILQ_FOREACH(tds, &nfsrv_devidhead, 5362 nfsdev_list) { 5363 if (tds->nfsdev_nmp != NULL && 5364 fndds != tds && 5365 ((tds->nfsdev_mdsisset == 0 && 5366 fndds->nfsdev_mdsisset == 0) || 5367 (tds->nfsdev_mdsisset != 0 && 5368 fndds->nfsdev_mdsisset != 0 && 5369 fsidcmp(&tds->nfsdev_mdsfsid, 5370 &mp->mnt_stat.f_fsid) == 0))) { 5371 *newnmpp = tds->nfsdev_nmp; 5372 break; 5373 } 5374 } 5375 if (tds != NULL) { 5376 /* 5377 * Move this entry to the end of the 5378 * list, so it won't be selected as 5379 * easily the next time. 5380 */ 5381 TAILQ_REMOVE(&nfsrv_devidhead, tds, 5382 nfsdev_list); 5383 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds, 5384 nfsdev_list); 5385 } 5386 } 5387 NFSDDSUNLOCK(); 5388 if (fndds != NULL) { 5389 dvp = fndds->nfsdev_dsdir[dsdir]; 5390 if (lktype != 0 || fhiszero != 0 || 5391 (nvpp != NULL && *nvpp == NULL)) { 5392 if (fhiszero != 0) 5393 error = vn_lock(dvp, 5394 LK_EXCLUSIVE); 5395 else if (lktype != 0) 5396 error = vn_lock(dvp, lktype); 5397 else 5398 error = vn_lock(dvp, LK_SHARED); 5399 /* 5400 * If the file handle is all 0's, try to 5401 * do a Lookup against the DS to acquire 5402 * it. 5403 * If dvpp == NULL or the Lookup fails, 5404 * unlock dvp after the call. 5405 */ 5406 if (error == 0 && (fhiszero != 0 || 5407 (nvpp != NULL && *nvpp == NULL))) { 5408 error = nfsrv_pnfslookupds(vp, 5409 dvp, pf, &nvp, p); 5410 if (error == 0) { 5411 if (fhiszero != 0) 5412 nfsrv_pnfssetfh( 5413 vp, pf, 5414 devid, 5415 fnamep, 5416 nvp, p); 5417 if (nvpp != NULL && 5418 *nvpp == NULL) { 5419 *nvpp = nvp; 5420 *dsdirp = dsdir; 5421 } else 5422 vput(nvp); 5423 } 5424 if (error != 0 || lktype == 0) 5425 NFSVOPUNLOCK(dvp); 5426 } 5427 } 5428 if (error == 0) { 5429 gotone++; 5430 NFSD_DEBUG(4, "gotone=%d\n", gotone); 5431 if (devid != NULL) { 5432 NFSBCOPY(fndds->nfsdev_deviceid, 5433 devid, NFSX_V4DEVICEID); 5434 devid += NFSX_V4DEVICEID; 5435 } 5436 if (dvpp != NULL) 5437 *tdvpp++ = dvp; 5438 if (fhp != NULL) 5439 NFSBCOPY(&pf->dsf_fh, fhp++, 5440 NFSX_MYFH); 5441 if (fnamep != NULL && gotone == 1) 5442 strlcpy(fnamep, 5443 pf->dsf_filename, 5444 sizeof(pf->dsf_filename)); 5445 } else 5446 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt " 5447 "err=%d\n", error); 5448 } 5449 } 5450 } 5451 if (error == 0 && gotone == 0) 5452 error = ENOENT; 5453 5454 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone, 5455 error); 5456 if (error == 0) 5457 *mirrorcntp = gotone; 5458 else { 5459 if (gotone > 0 && dvpp != NULL) { 5460 /* 5461 * If the error didn't occur on the first one and 5462 * dvpp != NULL, the one(s) prior to the failure will 5463 * have locked dvp's that need to be unlocked. 5464 */ 5465 for (i = 0; i < gotone; i++) { 5466 NFSVOPUNLOCK(*dvpp); 5467 *dvpp++ = NULL; 5468 } 5469 } 5470 /* 5471 * If it found the vnode to be copied from before a failure, 5472 * it needs to be vput()'d. 5473 */ 5474 if (nvpp != NULL && *nvpp != NULL) { 5475 vput(*nvpp); 5476 *nvpp = NULL; 5477 } 5478 } 5479 return (error); 5480 } 5481 5482 /* 5483 * Set the extended attribute for the Change attribute. 5484 */ 5485 static int 5486 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p) 5487 { 5488 struct pnfsdsattr dsattr; 5489 int error; 5490 5491 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp"); 5492 dsattr.dsa_filerev = nap->na_filerev; 5493 dsattr.dsa_size = nap->na_size; 5494 dsattr.dsa_atime = nap->na_atime; 5495 dsattr.dsa_mtime = nap->na_mtime; 5496 dsattr.dsa_bytes = nap->na_bytes; 5497 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM, 5498 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p); 5499 if (error != 0) 5500 printf("pNFS: setextattr=%d\n", error); 5501 return (error); 5502 } 5503 5504 static int 5505 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred, 5506 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp) 5507 { 5508 uint32_t *tl; 5509 struct nfsrv_descript *nd; 5510 nfsv4stateid_t st; 5511 struct mbuf *m, *m2; 5512 int error = 0, retlen, tlen, trimlen; 5513 5514 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n"); 5515 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5516 *mpp = NULL; 5517 /* 5518 * Use a stateid where other is an alternating 01010 pattern and 5519 * seqid is 0xffffffff. This value is not defined as special by 5520 * the RFC and is used by the FreeBSD NFS server to indicate an 5521 * MDS->DS proxy operation. 5522 */ 5523 st.other[0] = 0x55555555; 5524 st.other[1] = 0x55555555; 5525 st.other[2] = 0x55555555; 5526 st.seqid = 0xffffffff; 5527 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp), 5528 NULL, NULL, 0, 0, cred); 5529 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5530 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3); 5531 txdr_hyper(off, tl); 5532 *(tl + 2) = txdr_unsigned(len); 5533 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 5534 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5535 if (error != 0) { 5536 free(nd, M_TEMP); 5537 return (error); 5538 } 5539 if (nd->nd_repstat == 0) { 5540 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 5541 NFSM_STRSIZ(retlen, len); 5542 if (retlen > 0) { 5543 /* Trim off the pre-data XDR from the mbuf chain. */ 5544 m = nd->nd_mrep; 5545 while (m != NULL && m != nd->nd_md) { 5546 if (m->m_next == nd->nd_md) { 5547 m->m_next = NULL; 5548 m_freem(nd->nd_mrep); 5549 nd->nd_mrep = m = nd->nd_md; 5550 } else 5551 m = m->m_next; 5552 } 5553 if (m == NULL) { 5554 printf("nfsrv_readdsrpc: busted mbuf list\n"); 5555 error = ENOENT; 5556 goto nfsmout; 5557 } 5558 5559 /* 5560 * Now, adjust first mbuf so that any XDR before the 5561 * read data is skipped over. 5562 */ 5563 trimlen = nd->nd_dpos - mtod(m, char *); 5564 if (trimlen > 0) { 5565 m->m_len -= trimlen; 5566 NFSM_DATAP(m, trimlen); 5567 } 5568 5569 /* 5570 * Truncate the mbuf chain at retlen bytes of data, 5571 * plus XDR padding that brings the length up to a 5572 * multiple of 4. 5573 */ 5574 tlen = NFSM_RNDUP(retlen); 5575 do { 5576 if (m->m_len >= tlen) { 5577 m->m_len = tlen; 5578 tlen = 0; 5579 m2 = m->m_next; 5580 m->m_next = NULL; 5581 m_freem(m2); 5582 break; 5583 } 5584 tlen -= m->m_len; 5585 m = m->m_next; 5586 } while (m != NULL); 5587 if (tlen > 0) { 5588 printf("nfsrv_readdsrpc: busted mbuf list\n"); 5589 error = ENOENT; 5590 goto nfsmout; 5591 } 5592 *mpp = nd->nd_mrep; 5593 *mpendp = m; 5594 nd->nd_mrep = NULL; 5595 } 5596 } else 5597 error = nd->nd_repstat; 5598 nfsmout: 5599 /* If nd->nd_mrep is already NULL, this is a no-op. */ 5600 m_freem(nd->nd_mrep); 5601 free(nd, M_TEMP); 5602 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error); 5603 return (error); 5604 } 5605 5606 /* 5607 * Do a write RPC on a DS data file, using this structure for the arguments, 5608 * so that this function can be executed by a separate kernel process. 5609 */ 5610 struct nfsrvwritedsdorpc { 5611 int done; 5612 int inprog; 5613 struct task tsk; 5614 fhandle_t fh; 5615 off_t off; 5616 int len; 5617 struct nfsmount *nmp; 5618 struct ucred *cred; 5619 NFSPROC_T *p; 5620 struct mbuf *m; 5621 int err; 5622 }; 5623 5624 static int 5625 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len, 5626 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p) 5627 { 5628 uint32_t *tl; 5629 struct nfsrv_descript *nd; 5630 nfsattrbit_t attrbits; 5631 nfsv4stateid_t st; 5632 int commit, error, retlen; 5633 5634 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5635 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp, 5636 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 5637 5638 /* 5639 * Use a stateid where other is an alternating 01010 pattern and 5640 * seqid is 0xffffffff. This value is not defined as special by 5641 * the RFC and is used by the FreeBSD NFS server to indicate an 5642 * MDS->DS proxy operation. 5643 */ 5644 st.other[0] = 0x55555555; 5645 st.other[1] = 0x55555555; 5646 st.other[2] = 0x55555555; 5647 st.seqid = 0xffffffff; 5648 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5649 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED); 5650 txdr_hyper(off, tl); 5651 tl += 2; 5652 /* 5653 * Do all writes FileSync, since the server doesn't hold onto dirty 5654 * buffers. Since clients should be accessing the DS servers directly 5655 * using the pNFS layouts, this just needs to work correctly as a 5656 * fallback. 5657 */ 5658 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC); 5659 *tl = txdr_unsigned(len); 5660 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len); 5661 5662 /* Put data in mbuf chain. */ 5663 nd->nd_mb->m_next = m; 5664 5665 /* Set nd_mb and nd_bpos to end of data. */ 5666 while (m->m_next != NULL) 5667 m = m->m_next; 5668 nd->nd_mb = m; 5669 nfsm_set(nd, m->m_len); 5670 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len); 5671 5672 /* Do a Getattr for the attributes that change upon writing. */ 5673 NFSZERO_ATTRBIT(&attrbits); 5674 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 5675 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 5676 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 5677 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 5678 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 5679 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 5680 *tl = txdr_unsigned(NFSV4OP_GETATTR); 5681 (void) nfsrv_putattrbit(nd, &attrbits); 5682 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 5683 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5684 if (error != 0) { 5685 free(nd, M_TEMP); 5686 return (error); 5687 } 5688 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat); 5689 /* Get rid of weak cache consistency data for now. */ 5690 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 5691 (ND_NFSV4 | ND_V4WCCATTR)) { 5692 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5693 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 5694 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error); 5695 if (error != 0) 5696 goto nfsmout; 5697 /* 5698 * Get rid of Op# and status for next op. 5699 */ 5700 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5701 if (*++tl != 0) 5702 nd->nd_flag |= ND_NOMOREDATA; 5703 } 5704 if (nd->nd_repstat == 0) { 5705 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF); 5706 retlen = fxdr_unsigned(int, *tl++); 5707 commit = fxdr_unsigned(int, *tl); 5708 if (commit != NFSWRITE_FILESYNC) 5709 error = NFSERR_IO; 5710 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n", 5711 retlen, commit, error); 5712 } else 5713 error = nd->nd_repstat; 5714 /* We have no use for the Write Verifier since we use FileSync. */ 5715 5716 /* 5717 * Get the Change, Size, Access Time and Modify Time attributes and set 5718 * on the Metadata file, so its attributes will be what the file's 5719 * would be if it had been written. 5720 */ 5721 if (error == 0) { 5722 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5723 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5724 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 5725 } 5726 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error); 5727 nfsmout: 5728 m_freem(nd->nd_mrep); 5729 free(nd, M_TEMP); 5730 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error); 5731 return (error); 5732 } 5733 5734 /* 5735 * Start up the thread that will execute nfsrv_writedsdorpc(). 5736 */ 5737 static void 5738 start_writedsdorpc(void *arg, int pending) 5739 { 5740 struct nfsrvwritedsdorpc *drpc; 5741 5742 drpc = (struct nfsrvwritedsdorpc *)arg; 5743 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 5744 drpc->len, NULL, drpc->m, drpc->cred, drpc->p); 5745 drpc->done = 1; 5746 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err); 5747 } 5748 5749 static int 5750 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred, 5751 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 5752 struct mbuf **mpp, char *cp, int *failposp) 5753 { 5754 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL; 5755 struct nfsvattr na; 5756 struct mbuf *m; 5757 int error, i, offs, ret, timo; 5758 5759 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n"); 5760 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain")); 5761 drpc = NULL; 5762 if (mirrorcnt > 1) 5763 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5764 M_WAITOK); 5765 5766 /* Calculate offset in mbuf chain that data starts. */ 5767 offs = cp - mtod(*mpp, char *); 5768 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len); 5769 5770 /* 5771 * Do the write RPC for every DS, using a separate kernel process 5772 * for every DS except the last one. 5773 */ 5774 error = 0; 5775 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5776 tdrpc->done = 0; 5777 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5778 tdrpc->off = off; 5779 tdrpc->len = len; 5780 tdrpc->nmp = *nmpp; 5781 tdrpc->cred = cred; 5782 tdrpc->p = p; 5783 tdrpc->inprog = 0; 5784 tdrpc->err = 0; 5785 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK); 5786 ret = EIO; 5787 if (nfs_pnfsiothreads != 0) { 5788 ret = nfs_pnfsio(start_writedsdorpc, tdrpc); 5789 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n", 5790 ret); 5791 } 5792 if (ret != 0) { 5793 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL, 5794 tdrpc->m, cred, p); 5795 if (nfsds_failerr(ret) && *failposp == -1) 5796 *failposp = i; 5797 else if (error == 0 && ret != 0) 5798 error = ret; 5799 } 5800 nmpp++; 5801 fhp++; 5802 } 5803 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK); 5804 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p); 5805 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5806 *failposp = mirrorcnt - 1; 5807 else if (error == 0 && ret != 0) 5808 error = ret; 5809 if (error == 0) 5810 error = nfsrv_setextattr(vp, &na, p); 5811 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error); 5812 tdrpc = drpc; 5813 timo = hz / 50; /* Wait for 20msec. */ 5814 if (timo < 1) 5815 timo = 1; 5816 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5817 /* Wait for RPCs on separate threads to complete. */ 5818 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5819 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo); 5820 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5821 *failposp = i; 5822 else if (error == 0 && tdrpc->err != 0) 5823 error = tdrpc->err; 5824 } 5825 free(drpc, M_TEMP); 5826 return (error); 5827 } 5828 5829 /* 5830 * Do a allocate RPC on a DS data file, using this structure for the arguments, 5831 * so that this function can be executed by a separate kernel process. 5832 */ 5833 struct nfsrvallocatedsdorpc { 5834 int done; 5835 int inprog; 5836 struct task tsk; 5837 fhandle_t fh; 5838 off_t off; 5839 off_t len; 5840 struct nfsmount *nmp; 5841 struct ucred *cred; 5842 NFSPROC_T *p; 5843 int err; 5844 }; 5845 5846 static int 5847 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, 5848 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p) 5849 { 5850 uint32_t *tl; 5851 struct nfsrv_descript *nd; 5852 nfsattrbit_t attrbits; 5853 nfsv4stateid_t st; 5854 int error; 5855 5856 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 5857 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp, 5858 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 5859 5860 /* 5861 * Use a stateid where other is an alternating 01010 pattern and 5862 * seqid is 0xffffffff. This value is not defined as special by 5863 * the RFC and is used by the FreeBSD NFS server to indicate an 5864 * MDS->DS proxy operation. 5865 */ 5866 st.other[0] = 0x55555555; 5867 st.other[1] = 0x55555555; 5868 st.other[2] = 0x55555555; 5869 st.seqid = 0xffffffff; 5870 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 5871 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 5872 txdr_hyper(off, tl); tl += 2; 5873 txdr_hyper(len, tl); tl += 2; 5874 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len); 5875 5876 *tl = txdr_unsigned(NFSV4OP_GETATTR); 5877 NFSGETATTR_ATTRBIT(&attrbits); 5878 nfsrv_putattrbit(nd, &attrbits); 5879 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 5880 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 5881 if (error != 0) { 5882 free(nd, M_TEMP); 5883 return (error); 5884 } 5885 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n", 5886 nd->nd_repstat); 5887 if (nd->nd_repstat == 0) { 5888 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 5889 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 5890 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 5891 } else 5892 error = nd->nd_repstat; 5893 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error); 5894 nfsmout: 5895 m_freem(nd->nd_mrep); 5896 free(nd, M_TEMP); 5897 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error); 5898 return (error); 5899 } 5900 5901 /* 5902 * Start up the thread that will execute nfsrv_allocatedsdorpc(). 5903 */ 5904 static void 5905 start_allocatedsdorpc(void *arg, int pending) 5906 { 5907 struct nfsrvallocatedsdorpc *drpc; 5908 5909 drpc = (struct nfsrvallocatedsdorpc *)arg; 5910 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 5911 drpc->len, NULL, drpc->cred, drpc->p); 5912 drpc->done = 1; 5913 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err); 5914 } 5915 5916 static int 5917 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred, 5918 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 5919 int *failposp) 5920 { 5921 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL; 5922 struct nfsvattr na; 5923 int error, i, ret, timo; 5924 5925 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n"); 5926 drpc = NULL; 5927 if (mirrorcnt > 1) 5928 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 5929 M_WAITOK); 5930 5931 /* 5932 * Do the allocate RPC for every DS, using a separate kernel process 5933 * for every DS except the last one. 5934 */ 5935 error = 0; 5936 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5937 tdrpc->done = 0; 5938 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 5939 tdrpc->off = off; 5940 tdrpc->len = len; 5941 tdrpc->nmp = *nmpp; 5942 tdrpc->cred = cred; 5943 tdrpc->p = p; 5944 tdrpc->inprog = 0; 5945 tdrpc->err = 0; 5946 ret = EIO; 5947 if (nfs_pnfsiothreads != 0) { 5948 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc); 5949 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n", 5950 ret); 5951 } 5952 if (ret != 0) { 5953 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL, 5954 cred, p); 5955 if (nfsds_failerr(ret) && *failposp == -1) 5956 *failposp = i; 5957 else if (error == 0 && ret != 0) 5958 error = ret; 5959 } 5960 nmpp++; 5961 fhp++; 5962 } 5963 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p); 5964 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 5965 *failposp = mirrorcnt - 1; 5966 else if (error == 0 && ret != 0) 5967 error = ret; 5968 if (error == 0) 5969 error = nfsrv_setextattr(vp, &na, p); 5970 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error); 5971 tdrpc = drpc; 5972 timo = hz / 50; /* Wait for 20msec. */ 5973 if (timo < 1) 5974 timo = 1; 5975 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 5976 /* Wait for RPCs on separate threads to complete. */ 5977 while (tdrpc->inprog != 0 && tdrpc->done == 0) 5978 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo); 5979 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 5980 *failposp = i; 5981 else if (error == 0 && tdrpc->err != 0) 5982 error = tdrpc->err; 5983 } 5984 free(drpc, M_TEMP); 5985 return (error); 5986 } 5987 5988 /* 5989 * Do a deallocate RPC on a DS data file, using this structure for the 5990 * arguments, so that this function can be executed by a separate kernel 5991 * process. 5992 */ 5993 struct nfsrvdeallocatedsdorpc { 5994 int done; 5995 int inprog; 5996 struct task tsk; 5997 fhandle_t fh; 5998 off_t off; 5999 off_t len; 6000 struct nfsmount *nmp; 6001 struct ucred *cred; 6002 NFSPROC_T *p; 6003 int err; 6004 }; 6005 6006 static int 6007 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, 6008 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p) 6009 { 6010 uint32_t *tl; 6011 struct nfsrv_descript *nd; 6012 nfsattrbit_t attrbits; 6013 nfsv4stateid_t st; 6014 int error; 6015 6016 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6017 nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp, 6018 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 6019 6020 /* 6021 * Use a stateid where other is an alternating 01010 pattern and 6022 * seqid is 0xffffffff. This value is not defined as special by 6023 * the RFC and is used by the FreeBSD NFS server to indicate an 6024 * MDS->DS proxy operation. 6025 */ 6026 st.other[0] = 0x55555555; 6027 st.other[1] = 0x55555555; 6028 st.other[2] = 0x55555555; 6029 st.seqid = 0xffffffff; 6030 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6031 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED); 6032 txdr_hyper(off, tl); tl += 2; 6033 txdr_hyper(len, tl); tl += 2; 6034 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len); 6035 6036 /* Do a Getattr for the attributes that change upon writing. */ 6037 NFSZERO_ATTRBIT(&attrbits); 6038 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 6039 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 6040 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 6041 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 6042 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 6043 *tl = txdr_unsigned(NFSV4OP_GETATTR); 6044 nfsrv_putattrbit(nd, &attrbits); 6045 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, 6046 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6047 if (error != 0) { 6048 free(nd, M_TEMP); 6049 return (error); 6050 } 6051 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n", 6052 nd->nd_repstat); 6053 /* Get rid of weak cache consistency data for now. */ 6054 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 6055 (ND_NFSV4 | ND_V4WCCATTR)) { 6056 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 6057 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 6058 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error); 6059 if (error != 0) 6060 goto nfsmout; 6061 /* 6062 * Get rid of Op# and status for next op. 6063 */ 6064 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6065 if (*++tl != 0) 6066 nd->nd_flag |= ND_NOMOREDATA; 6067 } 6068 if (nd->nd_repstat == 0) { 6069 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6070 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL, 6071 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 6072 } else 6073 error = nd->nd_repstat; 6074 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error); 6075 nfsmout: 6076 m_freem(nd->nd_mrep); 6077 free(nd, M_TEMP); 6078 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error); 6079 return (error); 6080 } 6081 6082 /* 6083 * Start up the thread that will execute nfsrv_deallocatedsdorpc(). 6084 */ 6085 static void 6086 start_deallocatedsdorpc(void *arg, int pending) 6087 { 6088 struct nfsrvdeallocatedsdorpc *drpc; 6089 6090 drpc = (struct nfsrvdeallocatedsdorpc *)arg; 6091 drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off, 6092 drpc->len, NULL, drpc->cred, drpc->p); 6093 drpc->done = 1; 6094 NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err); 6095 } 6096 6097 static int 6098 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred, 6099 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 6100 int *failposp) 6101 { 6102 struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL; 6103 struct nfsvattr na; 6104 int error, i, ret, timo; 6105 6106 NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n"); 6107 drpc = NULL; 6108 if (mirrorcnt > 1) 6109 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 6110 M_WAITOK); 6111 6112 /* 6113 * Do the deallocate RPC for every DS, using a separate kernel process 6114 * for every DS except the last one. 6115 */ 6116 error = 0; 6117 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6118 tdrpc->done = 0; 6119 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 6120 tdrpc->off = off; 6121 tdrpc->len = len; 6122 tdrpc->nmp = *nmpp; 6123 tdrpc->cred = cred; 6124 tdrpc->p = p; 6125 tdrpc->inprog = 0; 6126 tdrpc->err = 0; 6127 ret = EIO; 6128 if (nfs_pnfsiothreads != 0) { 6129 ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc); 6130 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n", 6131 ret); 6132 } 6133 if (ret != 0) { 6134 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, 6135 NULL, cred, p); 6136 if (nfsds_failerr(ret) && *failposp == -1) 6137 *failposp = i; 6138 else if (error == 0 && ret != 0) 6139 error = ret; 6140 } 6141 nmpp++; 6142 fhp++; 6143 } 6144 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p); 6145 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 6146 *failposp = mirrorcnt - 1; 6147 else if (error == 0 && ret != 0) 6148 error = ret; 6149 if (error == 0) 6150 error = nfsrv_setextattr(vp, &na, p); 6151 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error); 6152 tdrpc = drpc; 6153 timo = hz / 50; /* Wait for 20msec. */ 6154 if (timo < 1) 6155 timo = 1; 6156 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6157 /* Wait for RPCs on separate threads to complete. */ 6158 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6159 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo); 6160 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 6161 *failposp = i; 6162 else if (error == 0 && tdrpc->err != 0) 6163 error = tdrpc->err; 6164 } 6165 free(drpc, M_TEMP); 6166 return (error); 6167 } 6168 6169 static int 6170 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6171 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap, 6172 struct nfsvattr *dsnap) 6173 { 6174 uint32_t *tl; 6175 struct nfsrv_descript *nd; 6176 nfsv4stateid_t st; 6177 nfsattrbit_t attrbits; 6178 int error; 6179 6180 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n"); 6181 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6182 /* 6183 * Use a stateid where other is an alternating 01010 pattern and 6184 * seqid is 0xffffffff. This value is not defined as special by 6185 * the RFC and is used by the FreeBSD NFS server to indicate an 6186 * MDS->DS proxy operation. 6187 */ 6188 st.other[0] = 0x55555555; 6189 st.other[1] = 0x55555555; 6190 st.other[2] = 0x55555555; 6191 st.seqid = 0xffffffff; 6192 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp), 6193 NULL, NULL, 0, 0, cred); 6194 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6195 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0); 6196 6197 /* Do a Getattr for the attributes that change due to writing. */ 6198 NFSZERO_ATTRBIT(&attrbits); 6199 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 6200 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 6201 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 6202 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 6203 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 6204 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 6205 *tl = txdr_unsigned(NFSV4OP_GETATTR); 6206 (void) nfsrv_putattrbit(nd, &attrbits); 6207 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6208 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6209 if (error != 0) { 6210 free(nd, M_TEMP); 6211 return (error); 6212 } 6213 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n", 6214 nd->nd_repstat); 6215 /* Get rid of weak cache consistency data for now. */ 6216 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) == 6217 (ND_NFSV4 | ND_V4WCCATTR)) { 6218 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL, 6219 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); 6220 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error); 6221 if (error != 0) 6222 goto nfsmout; 6223 /* 6224 * Get rid of Op# and status for next op. 6225 */ 6226 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6227 if (*++tl != 0) 6228 nd->nd_flag |= ND_NOMOREDATA; 6229 } 6230 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 6231 if (error != 0) 6232 goto nfsmout; 6233 if (nd->nd_repstat != 0) 6234 error = nd->nd_repstat; 6235 /* 6236 * Get the Change, Size, Access Time and Modify Time attributes and set 6237 * on the Metadata file, so its attributes will be what the file's 6238 * would be if it had been written. 6239 */ 6240 if (error == 0) { 6241 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED); 6242 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL, 6243 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, 6244 NULL); 6245 } 6246 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error); 6247 nfsmout: 6248 m_freem(nd->nd_mrep); 6249 free(nd, M_TEMP); 6250 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error); 6251 return (error); 6252 } 6253 6254 struct nfsrvsetattrdsdorpc { 6255 int done; 6256 int inprog; 6257 struct task tsk; 6258 fhandle_t fh; 6259 struct nfsmount *nmp; 6260 struct vnode *vp; 6261 struct ucred *cred; 6262 NFSPROC_T *p; 6263 struct nfsvattr na; 6264 struct nfsvattr dsna; 6265 int err; 6266 }; 6267 6268 /* 6269 * Start up the thread that will execute nfsrv_setattrdsdorpc(). 6270 */ 6271 static void 6272 start_setattrdsdorpc(void *arg, int pending) 6273 { 6274 struct nfsrvsetattrdsdorpc *drpc; 6275 6276 drpc = (struct nfsrvsetattrdsdorpc *)arg; 6277 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p, 6278 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna); 6279 drpc->done = 1; 6280 } 6281 6282 static int 6283 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6284 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, 6285 struct nfsvattr *nap, int *failposp) 6286 { 6287 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL; 6288 struct nfsvattr na; 6289 int error, i, ret, timo; 6290 6291 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n"); 6292 drpc = NULL; 6293 if (mirrorcnt > 1) 6294 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 6295 M_WAITOK); 6296 6297 /* 6298 * Do the setattr RPC for every DS, using a separate kernel process 6299 * for every DS except the last one. 6300 */ 6301 error = 0; 6302 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6303 tdrpc->done = 0; 6304 tdrpc->inprog = 0; 6305 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 6306 tdrpc->nmp = *nmpp; 6307 tdrpc->vp = vp; 6308 tdrpc->cred = cred; 6309 tdrpc->p = p; 6310 tdrpc->na = *nap; 6311 tdrpc->err = 0; 6312 ret = EIO; 6313 if (nfs_pnfsiothreads != 0) { 6314 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc); 6315 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n", 6316 ret); 6317 } 6318 if (ret != 0) { 6319 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, 6320 &na); 6321 if (nfsds_failerr(ret) && *failposp == -1) 6322 *failposp = i; 6323 else if (error == 0 && ret != 0) 6324 error = ret; 6325 } 6326 nmpp++; 6327 fhp++; 6328 } 6329 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na); 6330 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 6331 *failposp = mirrorcnt - 1; 6332 else if (error == 0 && ret != 0) 6333 error = ret; 6334 if (error == 0) 6335 error = nfsrv_setextattr(vp, &na, p); 6336 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error); 6337 tdrpc = drpc; 6338 timo = hz / 50; /* Wait for 20msec. */ 6339 if (timo < 1) 6340 timo = 1; 6341 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6342 /* Wait for RPCs on separate threads to complete. */ 6343 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6344 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo); 6345 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 6346 *failposp = i; 6347 else if (error == 0 && tdrpc->err != 0) 6348 error = tdrpc->err; 6349 } 6350 free(drpc, M_TEMP); 6351 return (error); 6352 } 6353 6354 /* 6355 * Do a Setattr of an NFSv4 ACL on the DS file. 6356 */ 6357 static int 6358 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6359 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp) 6360 { 6361 struct nfsrv_descript *nd; 6362 nfsv4stateid_t st; 6363 nfsattrbit_t attrbits; 6364 int error; 6365 6366 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n"); 6367 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6368 /* 6369 * Use a stateid where other is an alternating 01010 pattern and 6370 * seqid is 0xffffffff. This value is not defined as special by 6371 * the RFC and is used by the FreeBSD NFS server to indicate an 6372 * MDS->DS proxy operation. 6373 */ 6374 st.other[0] = 0x55555555; 6375 st.other[1] = 0x55555555; 6376 st.other[2] = 0x55555555; 6377 st.seqid = 0xffffffff; 6378 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp), 6379 NULL, NULL, 0, 0, cred); 6380 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6381 NFSZERO_ATTRBIT(&attrbits); 6382 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL); 6383 /* 6384 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(), 6385 * so passing in the metadata "vp" will be ok, since it is of 6386 * the same type (VREG). 6387 */ 6388 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL, 6389 NULL, 0, 0, 0, 0, 0, NULL, false, false, false); 6390 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6391 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6392 if (error != 0) { 6393 free(nd, M_TEMP); 6394 return (error); 6395 } 6396 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n", 6397 nd->nd_repstat); 6398 error = nd->nd_repstat; 6399 m_freem(nd->nd_mrep); 6400 free(nd, M_TEMP); 6401 return (error); 6402 } 6403 6404 struct nfsrvsetacldsdorpc { 6405 int done; 6406 int inprog; 6407 struct task tsk; 6408 fhandle_t fh; 6409 struct nfsmount *nmp; 6410 struct vnode *vp; 6411 struct ucred *cred; 6412 NFSPROC_T *p; 6413 struct acl *aclp; 6414 int err; 6415 }; 6416 6417 /* 6418 * Start up the thread that will execute nfsrv_setacldsdorpc(). 6419 */ 6420 static void 6421 start_setacldsdorpc(void *arg, int pending) 6422 { 6423 struct nfsrvsetacldsdorpc *drpc; 6424 6425 drpc = (struct nfsrvsetacldsdorpc *)arg; 6426 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p, 6427 drpc->vp, drpc->nmp, drpc->aclp); 6428 drpc->done = 1; 6429 } 6430 6431 static int 6432 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6433 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp, 6434 int *failposp) 6435 { 6436 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL; 6437 int error, i, ret, timo; 6438 6439 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n"); 6440 drpc = NULL; 6441 if (mirrorcnt > 1) 6442 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP, 6443 M_WAITOK); 6444 6445 /* 6446 * Do the setattr RPC for every DS, using a separate kernel process 6447 * for every DS except the last one. 6448 */ 6449 error = 0; 6450 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6451 tdrpc->done = 0; 6452 tdrpc->inprog = 0; 6453 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp)); 6454 tdrpc->nmp = *nmpp; 6455 tdrpc->vp = vp; 6456 tdrpc->cred = cred; 6457 tdrpc->p = p; 6458 tdrpc->aclp = aclp; 6459 tdrpc->err = 0; 6460 ret = EIO; 6461 if (nfs_pnfsiothreads != 0) { 6462 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc); 6463 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n", 6464 ret); 6465 } 6466 if (ret != 0) { 6467 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, 6468 aclp); 6469 if (nfsds_failerr(ret) && *failposp == -1) 6470 *failposp = i; 6471 else if (error == 0 && ret != 0) 6472 error = ret; 6473 } 6474 nmpp++; 6475 fhp++; 6476 } 6477 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp); 6478 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1) 6479 *failposp = mirrorcnt - 1; 6480 else if (error == 0 && ret != 0) 6481 error = ret; 6482 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error); 6483 tdrpc = drpc; 6484 timo = hz / 50; /* Wait for 20msec. */ 6485 if (timo < 1) 6486 timo = 1; 6487 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) { 6488 /* Wait for RPCs on separate threads to complete. */ 6489 while (tdrpc->inprog != 0 && tdrpc->done == 0) 6490 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo); 6491 if (nfsds_failerr(tdrpc->err) && *failposp == -1) 6492 *failposp = i; 6493 else if (error == 0 && tdrpc->err != 0) 6494 error = tdrpc->err; 6495 } 6496 free(drpc, M_TEMP); 6497 return (error); 6498 } 6499 6500 /* 6501 * Getattr call to the DS for the attributes that change due to writing. 6502 */ 6503 static int 6504 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p, 6505 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap) 6506 { 6507 struct nfsrv_descript *nd; 6508 int error; 6509 nfsattrbit_t attrbits; 6510 6511 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n"); 6512 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6513 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp, 6514 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 6515 NFSZERO_ATTRBIT(&attrbits); 6516 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 6517 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE); 6518 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS); 6519 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY); 6520 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED); 6521 (void) nfsrv_putattrbit(nd, &attrbits); 6522 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6523 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6524 if (error != 0) { 6525 free(nd, M_TEMP); 6526 return (error); 6527 } 6528 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n", 6529 nd->nd_repstat); 6530 if (nd->nd_repstat == 0) { 6531 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, 6532 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, 6533 NULL, NULL, NULL); 6534 /* 6535 * We can only save the updated values in the extended 6536 * attribute if the vp is exclusively locked. 6537 * This should happen when any of the following operations 6538 * occur on the vnode: 6539 * Close, Delegreturn, LayoutCommit, LayoutReturn 6540 * As such, the updated extended attribute should get saved 6541 * before nfsrv_checkdsattr() returns 0 and allows the cached 6542 * attributes to be returned without calling this function. 6543 */ 6544 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) { 6545 error = nfsrv_setextattr(vp, nap, p); 6546 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n", 6547 error); 6548 } 6549 } else 6550 error = nd->nd_repstat; 6551 m_freem(nd->nd_mrep); 6552 free(nd, M_TEMP); 6553 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error); 6554 return (error); 6555 } 6556 6557 /* 6558 * Seek call to a DS. 6559 */ 6560 static int 6561 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp, 6562 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp) 6563 { 6564 uint32_t *tl; 6565 struct nfsrv_descript *nd; 6566 nfsv4stateid_t st; 6567 int error; 6568 6569 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n"); 6570 /* 6571 * Use a stateid where other is an alternating 01010 pattern and 6572 * seqid is 0xffffffff. This value is not defined as special by 6573 * the RFC and is used by the FreeBSD NFS server to indicate an 6574 * MDS->DS proxy operation. 6575 */ 6576 st.other[0] = 0x55555555; 6577 st.other[1] = 0x55555555; 6578 st.other[2] = 0x55555555; 6579 st.seqid = 0xffffffff; 6580 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO); 6581 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp, 6582 sizeof(fhandle_t), NULL, NULL, 0, 0, cred); 6583 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID); 6584 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED); 6585 txdr_hyper(*offp, tl); tl += 2; 6586 *tl = txdr_unsigned(content); 6587 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred, 6588 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL); 6589 if (error != 0) { 6590 free(nd, M_TEMP); 6591 return (error); 6592 } 6593 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat); 6594 if (nd->nd_repstat == 0) { 6595 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER); 6596 if (*tl++ == newnfs_true) 6597 *eofp = true; 6598 else 6599 *eofp = false; 6600 *offp = fxdr_hyper(tl); 6601 } else 6602 error = nd->nd_repstat; 6603 nfsmout: 6604 m_freem(nd->nd_mrep); 6605 free(nd, M_TEMP); 6606 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error); 6607 return (error); 6608 } 6609 6610 /* 6611 * Get the device id and file handle for a DS file. 6612 */ 6613 int 6614 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp, 6615 fhandle_t *fhp, char *devid) 6616 { 6617 int buflen, error; 6618 char *buf; 6619 6620 buflen = 1024; 6621 buf = malloc(buflen, M_TEMP, M_WAITOK); 6622 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL, 6623 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL); 6624 free(buf, M_TEMP); 6625 return (error); 6626 } 6627 6628 /* 6629 * Do a Lookup against the DS for the filename. 6630 */ 6631 static int 6632 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf, 6633 struct vnode **nvpp, NFSPROC_T *p) 6634 { 6635 struct nameidata named; 6636 struct ucred *tcred; 6637 char *bufp; 6638 u_long *hashp; 6639 struct vnode *nvp; 6640 int error; 6641 6642 tcred = newnfs_getcred(); 6643 named.ni_cnd.cn_nameiop = LOOKUP; 6644 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY; 6645 named.ni_cnd.cn_cred = tcred; 6646 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF; 6647 nfsvno_setpathbuf(&named, &bufp, &hashp); 6648 named.ni_cnd.cn_nameptr = bufp; 6649 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename); 6650 strlcpy(bufp, pf->dsf_filename, NAME_MAX); 6651 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp); 6652 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd); 6653 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error); 6654 NFSFREECRED(tcred); 6655 nfsvno_relpathbuf(&named); 6656 if (error == 0) 6657 *nvpp = nvp; 6658 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error); 6659 return (error); 6660 } 6661 6662 /* 6663 * Set the file handle to the correct one. 6664 */ 6665 static void 6666 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid, 6667 char *fnamep, struct vnode *nvp, NFSPROC_T *p) 6668 { 6669 struct nfsnode *np; 6670 int ret = 0; 6671 6672 np = VTONFS(nvp); 6673 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH); 6674 /* 6675 * We can only do a vn_set_extattr() if the vnode is exclusively 6676 * locked and vn_start_write() has been done. If devid != NULL or 6677 * fnamep != NULL or the vnode is shared locked, vn_start_write() 6678 * may not have been done. 6679 * If not done now, it will be done on a future call. 6680 */ 6681 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) == 6682 LK_EXCLUSIVE) 6683 ret = vn_extattr_set(vp, IO_NODELOCKED, 6684 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf), 6685 (char *)pf, p); 6686 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret); 6687 } 6688 6689 /* 6690 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point 6691 * when the DS has failed. 6692 */ 6693 void 6694 nfsrv_killrpcs(struct nfsmount *nmp) 6695 { 6696 6697 /* 6698 * Call newnfs_nmcancelreqs() to cause 6699 * any RPCs in progress on the mount point to 6700 * fail. 6701 * This will cause any process waiting for an 6702 * RPC to complete while holding a vnode lock 6703 * on the mounted-on vnode (such as "df" or 6704 * a non-forced "umount") to fail. 6705 * This will unlock the mounted-on vnode so 6706 * a forced dismount can succeed. 6707 * The NFSMNTP_CANCELRPCS flag should be set when this function is 6708 * called. 6709 */ 6710 newnfs_nmcancelreqs(nmp); 6711 } 6712 6713 /* 6714 * Sum up the statfs info for each of the DSs, so that the client will 6715 * receive the total for all DSs. 6716 */ 6717 static int 6718 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp) 6719 { 6720 struct statfs *tsf; 6721 struct nfsdevice *ds; 6722 struct vnode **dvpp, **tdvpp, *dvp; 6723 uint64_t tot; 6724 int cnt, error = 0, i; 6725 6726 if (nfsrv_devidcnt <= 0) 6727 return (ENXIO); 6728 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK); 6729 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK); 6730 6731 /* Get an array of the dvps for the DSs. */ 6732 tdvpp = dvpp; 6733 i = 0; 6734 NFSDDSLOCK(); 6735 /* First, search for matches for same file system. */ 6736 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 6737 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 && 6738 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) { 6739 if (++i > nfsrv_devidcnt) 6740 break; 6741 *tdvpp++ = ds->nfsdev_dvp; 6742 } 6743 } 6744 /* 6745 * If no matches for same file system, total all servers not assigned 6746 * to a file system. 6747 */ 6748 if (i == 0) { 6749 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 6750 if (ds->nfsdev_nmp != NULL && 6751 ds->nfsdev_mdsisset == 0) { 6752 if (++i > nfsrv_devidcnt) 6753 break; 6754 *tdvpp++ = ds->nfsdev_dvp; 6755 } 6756 } 6757 } 6758 NFSDDSUNLOCK(); 6759 cnt = i; 6760 6761 /* Do a VFS_STATFS() for each of the DSs and sum them up. */ 6762 tdvpp = dvpp; 6763 for (i = 0; i < cnt && error == 0; i++) { 6764 dvp = *tdvpp++; 6765 error = VFS_STATFS(dvp->v_mount, tsf); 6766 if (error == 0) { 6767 if (sf->f_bsize == 0) { 6768 if (tsf->f_bsize > 0) 6769 sf->f_bsize = tsf->f_bsize; 6770 else 6771 sf->f_bsize = 8192; 6772 } 6773 if (tsf->f_blocks > 0) { 6774 if (sf->f_bsize != tsf->f_bsize) { 6775 tot = tsf->f_blocks * tsf->f_bsize; 6776 sf->f_blocks += (tot / sf->f_bsize); 6777 } else 6778 sf->f_blocks += tsf->f_blocks; 6779 } 6780 if (tsf->f_bfree > 0) { 6781 if (sf->f_bsize != tsf->f_bsize) { 6782 tot = tsf->f_bfree * tsf->f_bsize; 6783 sf->f_bfree += (tot / sf->f_bsize); 6784 } else 6785 sf->f_bfree += tsf->f_bfree; 6786 } 6787 if (tsf->f_bavail > 0) { 6788 if (sf->f_bsize != tsf->f_bsize) { 6789 tot = tsf->f_bavail * tsf->f_bsize; 6790 sf->f_bavail += (tot / sf->f_bsize); 6791 } else 6792 sf->f_bavail += tsf->f_bavail; 6793 } 6794 } 6795 } 6796 free(tsf, M_TEMP); 6797 free(dvpp, M_TEMP); 6798 return (error); 6799 } 6800 6801 /* 6802 * Set an NFSv4 acl. 6803 */ 6804 int 6805 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p) 6806 { 6807 int error; 6808 6809 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) { 6810 error = NFSERR_ATTRNOTSUPP; 6811 goto out; 6812 } 6813 /* 6814 * With NFSv4 ACLs, chmod(2) may need to add additional entries. 6815 * Make sure it has enough room for that - splitting every entry 6816 * into two and appending "canonical six" entries at the end. 6817 * Cribbed out of kern/vfs_acl.c - Rick M. 6818 */ 6819 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) { 6820 error = NFSERR_ATTRNOTSUPP; 6821 goto out; 6822 } 6823 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p); 6824 if (error == 0) { 6825 error = nfsrv_dssetacl(vp, aclp, cred, p); 6826 if (error == ENOENT) 6827 error = 0; 6828 } 6829 6830 out: 6831 NFSEXITCODE(error); 6832 return (error); 6833 } 6834 6835 /* 6836 * Seek vnode op call (actually it is a VOP_IOCTL()). 6837 * This function is called with the vnode locked, but unlocks and vrele()s 6838 * the vp before returning. 6839 */ 6840 int 6841 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd, 6842 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p) 6843 { 6844 struct nfsvattr at; 6845 int error, ret; 6846 6847 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp"); 6848 /* 6849 * Attempt to seek on a DS file. A return of ENOENT implies 6850 * there is no DS file to seek on. 6851 */ 6852 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL, 6853 NULL, NULL, NULL, NULL, offp, content, eofp); 6854 if (error != ENOENT) { 6855 vput(vp); 6856 return (error); 6857 } 6858 6859 /* 6860 * Do the VOP_IOCTL() call. For the case where *offp == file_size, 6861 * VOP_IOCTL() will return ENXIO. However, the correct reply for 6862 * NFSv4.2 is *eofp == true and error == 0 for this case. 6863 */ 6864 NFSVOPUNLOCK(vp); 6865 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p); 6866 *eofp = false; 6867 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) { 6868 /* Handle the cases where we might be at EOF. */ 6869 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL); 6870 if (ret == 0 && *offp == at.na_size) { 6871 *eofp = true; 6872 error = 0; 6873 } 6874 if (ret != 0 && error == 0) 6875 error = ret; 6876 } 6877 vrele(vp); 6878 NFSEXITCODE(error); 6879 return (error); 6880 } 6881 6882 /* 6883 * Allocate vnode op call. 6884 */ 6885 int 6886 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred, 6887 NFSPROC_T *p) 6888 { 6889 int error; 6890 off_t olen; 6891 6892 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp"); 6893 /* 6894 * Attempt to allocate on a DS file. A return of ENOENT implies 6895 * there is no DS file to allocate on. 6896 */ 6897 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL, 6898 NULL, NULL, NULL, NULL, &len, 0, NULL); 6899 if (error != ENOENT) 6900 return (error); 6901 6902 /* 6903 * Do the actual VOP_ALLOCATE(), looping so long as 6904 * progress is being made, to achieve completion. 6905 */ 6906 do { 6907 olen = len; 6908 error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred); 6909 if (error == 0 && len > 0 && olen > len) 6910 maybe_yield(); 6911 } while (error == 0 && len > 0 && olen > len); 6912 if (error == 0 && len > 0) 6913 error = NFSERR_IO; 6914 NFSEXITCODE(error); 6915 return (error); 6916 } 6917 6918 /* 6919 * Deallocate vnode op call. 6920 */ 6921 int 6922 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred, 6923 NFSPROC_T *p) 6924 { 6925 int error; 6926 off_t olen; 6927 6928 ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp"); 6929 /* 6930 * Attempt to deallocate on a DS file. A return of ENOENT implies 6931 * there is no DS file to deallocate on. 6932 */ 6933 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL, 6934 NULL, NULL, NULL, NULL, &len, 0, NULL); 6935 if (error != ENOENT) 6936 return (error); 6937 6938 /* 6939 * Do the actual VOP_DEALLOCATE(), looping so long as 6940 * progress is being made, to achieve completion. 6941 */ 6942 do { 6943 olen = len; 6944 error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred); 6945 if (error == 0 && len > 0 && olen > len) 6946 maybe_yield(); 6947 } while (error == 0 && len > 0 && olen > len); 6948 if (error == 0 && len > 0) 6949 error = NFSERR_IO; 6950 NFSEXITCODE(error); 6951 return (error); 6952 } 6953 6954 /* 6955 * Get Extended Atribute vnode op into an mbuf list. 6956 */ 6957 int 6958 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp, 6959 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p, 6960 struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 6961 { 6962 struct iovec *iv; 6963 struct uio io, *uiop = &io; 6964 struct mbuf *m, *m2; 6965 int alen, error, len, tlen; 6966 size_t siz; 6967 6968 /* First, find out the size of the extended attribute. */ 6969 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL, 6970 &siz, cred, p); 6971 if (error != 0) 6972 return (NFSERR_NOXATTR); 6973 if (siz > maxresp - NFS_MAXXDR) 6974 return (NFSERR_XATTR2BIG); 6975 len = siz; 6976 tlen = NFSM_RNDUP(len); 6977 if (tlen > 0) { 6978 /* 6979 * If cnt > MCLBYTES and the reply will not be saved, use 6980 * ext_pgs mbufs for TLS. 6981 * For NFSv4.0, we do not know for sure if the reply will 6982 * be saved, so do not use ext_pgs mbufs for NFSv4.0. 6983 * Always use ext_pgs mbufs if ND_EXTPG is set. 6984 */ 6985 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES && 6986 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS && 6987 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)) 6988 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen, 6989 maxextsiz, &m, &m2, &iv); 6990 else 6991 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2, 6992 &iv); 6993 uiop->uio_iov = iv; 6994 } else { 6995 uiop->uio_iovcnt = 0; 6996 uiop->uio_iov = iv = NULL; 6997 m = m2 = NULL; 6998 } 6999 uiop->uio_offset = 0; 7000 uiop->uio_resid = tlen; 7001 uiop->uio_rw = UIO_READ; 7002 uiop->uio_segflg = UIO_SYSSPACE; 7003 uiop->uio_td = p; 7004 #ifdef MAC 7005 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER, 7006 name); 7007 if (error != 0) 7008 goto out; 7009 #endif 7010 7011 if (tlen > 0) 7012 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop, 7013 NULL, cred, p); 7014 if (error != 0) 7015 goto out; 7016 if (uiop->uio_resid > 0) { 7017 alen = tlen; 7018 len = tlen - uiop->uio_resid; 7019 tlen = NFSM_RNDUP(len); 7020 if (alen != tlen) 7021 printf("nfsvno_getxattr: weird size read\n"); 7022 if (tlen == 0) { 7023 m_freem(m); 7024 m = m2 = NULL; 7025 } else if (alen != tlen || tlen != len) 7026 m2 = nfsrv_adj(m, alen - tlen, tlen - len); 7027 } 7028 *lenp = len; 7029 *mpp = m; 7030 *mpendp = m2; 7031 7032 out: 7033 if (error != 0) { 7034 if (m != NULL) 7035 m_freem(m); 7036 *lenp = 0; 7037 } 7038 free(iv, M_TEMP); 7039 NFSEXITCODE(error); 7040 return (error); 7041 } 7042 7043 /* 7044 * Set Extended attribute vnode op from an mbuf list. 7045 */ 7046 int 7047 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m, 7048 char *cp, struct ucred *cred, struct thread *p) 7049 { 7050 struct iovec *iv; 7051 struct uio uio, *uiop = &uio; 7052 int cnt, error; 7053 7054 error = 0; 7055 #ifdef MAC 7056 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER, 7057 name); 7058 #endif 7059 if (error != 0) 7060 goto out; 7061 7062 uiop->uio_rw = UIO_WRITE; 7063 uiop->uio_segflg = UIO_SYSSPACE; 7064 uiop->uio_td = p; 7065 uiop->uio_offset = 0; 7066 uiop->uio_resid = len; 7067 if (len > 0) { 7068 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt); 7069 uiop->uio_iov = iv; 7070 uiop->uio_iovcnt = cnt; 7071 } else { 7072 uiop->uio_iov = iv = NULL; 7073 uiop->uio_iovcnt = 0; 7074 } 7075 if (error == 0) { 7076 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop, 7077 cred, p); 7078 if (error == 0) { 7079 if (vp->v_type == VREG && nfsrv_devidcnt != 0) 7080 nfsvno_updateds(vp, cred, p); 7081 error = VOP_FSYNC(vp, MNT_WAIT, p); 7082 } 7083 free(iv, M_TEMP); 7084 } 7085 7086 out: 7087 NFSEXITCODE(error); 7088 return (error); 7089 } 7090 7091 /* 7092 * For a pNFS server, the DS file's ctime and 7093 * va_filerev (TimeMetadata and Change) needs to 7094 * be updated. This is a hack, but works by 7095 * flipping the S_ISGID bit in va_mode and then 7096 * flipping it back. 7097 * It does result in two MDS->DS RPCs, but creating 7098 * a custom RPC just to do this seems overkill, since 7099 * Setxattr/Rmxattr will not be done that frequently. 7100 * If it fails part way through, that is not too 7101 * serious, since the DS file is never executed. 7102 */ 7103 static void 7104 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p) 7105 { 7106 struct nfsvattr nva; 7107 int ret; 7108 u_short tmode; 7109 7110 ret = VOP_GETATTR(vp, &nva.na_vattr, cred); 7111 if (ret == 0) { 7112 tmode = nva.na_mode; 7113 NFSVNO_ATTRINIT(&nva); 7114 tmode ^= S_ISGID; 7115 NFSVNO_SETATTRVAL(&nva, mode, tmode); 7116 ret = nfsrv_proxyds(vp, 0, 0, cred, p, 7117 NFSPROC_SETATTR, NULL, NULL, NULL, &nva, 7118 NULL, NULL, 0, NULL); 7119 if (ret == 0) { 7120 tmode ^= S_ISGID; 7121 NFSVNO_SETATTRVAL(&nva, mode, tmode); 7122 ret = nfsrv_proxyds(vp, 0, 0, cred, p, 7123 NFSPROC_SETATTR, NULL, NULL, NULL, 7124 &nva, NULL, NULL, 0, NULL); 7125 } 7126 } 7127 } 7128 7129 /* 7130 * Remove Extended attribute vnode op. 7131 */ 7132 int 7133 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name, 7134 struct ucred *cred, struct thread *p) 7135 { 7136 int error; 7137 7138 /* 7139 * Get rid of any delegations. I am not sure why this is required, 7140 * but RFC-8276 says so. 7141 */ 7142 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p); 7143 if (error != 0) 7144 goto out; 7145 #ifdef MAC 7146 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER, 7147 name); 7148 if (error != 0) 7149 goto out; 7150 #endif 7151 7152 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p); 7153 if (error == EOPNOTSUPP) 7154 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL, 7155 cred, p); 7156 if (error == 0) { 7157 if (vp->v_type == VREG && nfsrv_devidcnt != 0) 7158 nfsvno_updateds(vp, cred, p); 7159 error = VOP_FSYNC(vp, MNT_WAIT, p); 7160 } 7161 out: 7162 NFSEXITCODE(error); 7163 return (error); 7164 } 7165 7166 /* 7167 * List Extended Atribute vnode op into an mbuf list. 7168 */ 7169 int 7170 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred, 7171 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp) 7172 { 7173 struct iovec iv; 7174 struct uio io; 7175 int error; 7176 size_t siz; 7177 7178 *bufp = NULL; 7179 /* First, find out the size of the extended attribute. */ 7180 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred, 7181 p); 7182 if (error != 0) 7183 return (NFSERR_NOXATTR); 7184 if (siz <= cookie) { 7185 *lenp = 0; 7186 *eofp = true; 7187 goto out; 7188 } 7189 if (siz > cookie + *lenp) { 7190 siz = cookie + *lenp; 7191 *eofp = false; 7192 } else 7193 *eofp = true; 7194 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */ 7195 if (siz > 10 * 1024 * 1024) { 7196 error = NFSERR_XATTR2BIG; 7197 goto out; 7198 } 7199 *bufp = malloc(siz, M_TEMP, M_WAITOK); 7200 iv.iov_base = *bufp; 7201 iv.iov_len = siz; 7202 io.uio_iovcnt = 1; 7203 io.uio_iov = &iv; 7204 io.uio_offset = 0; 7205 io.uio_resid = siz; 7206 io.uio_rw = UIO_READ; 7207 io.uio_segflg = UIO_SYSSPACE; 7208 io.uio_td = p; 7209 #ifdef MAC 7210 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER); 7211 if (error != 0) 7212 goto out; 7213 #endif 7214 7215 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred, 7216 p); 7217 if (error != 0) 7218 goto out; 7219 if (io.uio_resid > 0) 7220 siz -= io.uio_resid; 7221 *lenp = siz; 7222 7223 out: 7224 if (error != 0) { 7225 free(*bufp, M_TEMP); 7226 *bufp = NULL; 7227 } 7228 NFSEXITCODE(error); 7229 return (error); 7230 } 7231 7232 /* 7233 * Trim trailing data off the mbuf list being built. 7234 */ 7235 void 7236 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos, 7237 int bextpg, int bextpgsiz) 7238 { 7239 vm_page_t pg; 7240 int fullpgsiz, i; 7241 7242 if (mb->m_next != NULL) { 7243 m_freem(mb->m_next); 7244 mb->m_next = NULL; 7245 } 7246 if ((mb->m_flags & M_EXTPG) != 0) { 7247 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs, 7248 ("nfsm_trimtrailing: bextpg out of range")); 7249 KASSERT(bpos == (char *)(void *) 7250 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz, 7251 ("nfsm_trimtrailing: bextpgsiz bad!")); 7252 7253 /* First, get rid of any pages after this position. */ 7254 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) { 7255 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]); 7256 vm_page_unwire_noq(pg); 7257 vm_page_free(pg); 7258 } 7259 mb->m_epg_npgs = bextpg + 1; 7260 if (bextpg == 0) 7261 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off; 7262 else 7263 fullpgsiz = PAGE_SIZE; 7264 mb->m_epg_last_len = fullpgsiz - bextpgsiz; 7265 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off); 7266 for (i = 1; i < mb->m_epg_npgs; i++) 7267 mb->m_len += m_epg_pagelen(mb, i, 0); 7268 nd->nd_bextpgsiz = bextpgsiz; 7269 nd->nd_bextpg = bextpg; 7270 } else 7271 mb->m_len = bpos - mtod(mb, char *); 7272 nd->nd_mb = mb; 7273 nd->nd_bpos = bpos; 7274 } 7275 7276 7277 /* 7278 * Check to see if a put file handle operation should test for 7279 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR. 7280 * When Open is the next operation, NFSERR_WRONGSEC cannot be 7281 * replied for the Open cases that use a component. This can 7282 * be identified by the fact that the file handle's type is VDIR. 7283 */ 7284 bool 7285 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, __enum_uint8(vtype) vtyp) 7286 { 7287 7288 if ((nd->nd_flag & ND_NFSV4) == 0) 7289 return (true); 7290 7291 if ((nd->nd_flag & ND_LASTOP) != 0) 7292 return (false); 7293 7294 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH || 7295 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH || 7296 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP || 7297 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME) 7298 return (false); 7299 if (nextop == NFSV4OP_OPEN && vtyp == VDIR) 7300 return (false); 7301 return (true); 7302 } 7303 7304 /* 7305 * Check DSs marked no space. 7306 */ 7307 void 7308 nfsrv_checknospc(void) 7309 { 7310 struct statfs *tsf; 7311 struct nfsdevice *ds; 7312 struct vnode **dvpp, **tdvpp, *dvp; 7313 char *devid, *tdevid; 7314 int cnt, error = 0, i; 7315 7316 if (nfsrv_devidcnt <= 0) 7317 return; 7318 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK); 7319 devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK); 7320 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK); 7321 7322 /* Get an array of the dvps for the DSs. */ 7323 tdvpp = dvpp; 7324 tdevid = devid; 7325 i = 0; 7326 NFSDDSLOCK(); 7327 /* First, search for matches for same file system. */ 7328 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) { 7329 if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) { 7330 if (++i > nfsrv_devidcnt) 7331 break; 7332 *tdvpp++ = ds->nfsdev_dvp; 7333 NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID); 7334 tdevid += NFSX_V4DEVICEID; 7335 } 7336 } 7337 NFSDDSUNLOCK(); 7338 7339 /* Do a VFS_STATFS() for each of the DSs and clear no space. */ 7340 cnt = i; 7341 tdvpp = dvpp; 7342 tdevid = devid; 7343 for (i = 0; i < cnt && error == 0; i++) { 7344 dvp = *tdvpp++; 7345 error = VFS_STATFS(dvp->v_mount, tsf); 7346 if (error == 0 && tsf->f_bavail > 0) { 7347 NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n"); 7348 nfsrv_marknospc(tdevid, false); 7349 } 7350 tdevid += NFSX_V4DEVICEID; 7351 } 7352 free(tsf, M_TEMP); 7353 free(dvpp, M_TEMP); 7354 free(devid, M_TEMP); 7355 } 7356 7357 /* 7358 * Initialize everything that needs to be initialized for a vnet. 7359 */ 7360 static void 7361 nfsrv_vnetinit(const void *unused __unused) 7362 { 7363 7364 nfsd_mntinit(); 7365 } 7366 VNET_SYSINIT(nfsrv_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY, 7367 nfsrv_vnetinit, NULL); 7368 7369 /* 7370 * Clean up everything that is in a vnet and needs to be 7371 * done when the jail is destroyed or the module unloaded. 7372 */ 7373 static void 7374 nfsrv_cleanup(const void *unused __unused) 7375 { 7376 int i; 7377 7378 NFSD_LOCK(); 7379 if (!NFSD_VNET(nfsrv_mntinited)) { 7380 NFSD_UNLOCK(); 7381 return; 7382 } 7383 NFSD_VNET(nfsrv_mntinited) = false; 7384 NFSD_UNLOCK(); 7385 7386 /* Clean out all NFSv4 state. */ 7387 nfsrv_throwawayallstate(curthread); 7388 7389 /* Clean the NFS server reply cache */ 7390 nfsrvd_cleancache(); 7391 7392 /* Clean out v4root exports. */ 7393 if (NFSD_VNET(nfsv4root_mnt)->mnt_export != NULL) { 7394 vfs_free_addrlist(NFSD_VNET(nfsv4root_mnt)->mnt_export); 7395 free(NFSD_VNET(nfsv4root_mnt)->mnt_export, M_MOUNT); 7396 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL; 7397 } 7398 7399 /* Free up the krpc server pool. */ 7400 if (NFSD_VNET(nfsrvd_pool) != NULL) 7401 svcpool_destroy(NFSD_VNET(nfsrvd_pool)); 7402 7403 /* and get rid of the locks */ 7404 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 7405 mtx_destroy(&NFSD_VNET(nfsrchash_table)[i].mtx); 7406 mtx_destroy(&NFSD_VNET(nfsrcahash_table)[i].mtx); 7407 } 7408 mtx_destroy(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx); 7409 for (i = 0; i < nfsrv_sessionhashsize; i++) 7410 mtx_destroy(&NFSD_VNET(nfssessionhash)[i].mtx); 7411 lockdestroy(&NFSD_VNET(nfsv4root_mnt)->mnt_explock); 7412 free(NFSD_VNET(nfsrvudphashtbl), M_NFSRVCACHE); 7413 free(NFSD_VNET(nfsrchash_table), M_NFSRVCACHE); 7414 free(NFSD_VNET(nfsrcahash_table), M_NFSRVCACHE); 7415 free(NFSD_VNET(nfsclienthash), M_NFSDCLIENT); 7416 free(NFSD_VNET(nfslockhash), M_NFSDLOCKFILE); 7417 free(NFSD_VNET(nfssessionhash), M_NFSDSESSION); 7418 free(NFSD_VNET(nfsv4root_mnt), M_TEMP); 7419 NFSD_VNET(nfsv4root_mnt) = NULL; 7420 } 7421 VNET_SYSUNINIT(nfsrv_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY, 7422 nfsrv_cleanup, NULL); 7423 7424 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 7425 7426 /* 7427 * Called once to initialize data structures... 7428 */ 7429 static int 7430 nfsd_modevent(module_t mod, int type, void *data) 7431 { 7432 int error = 0, i; 7433 static int loaded = 0; 7434 7435 switch (type) { 7436 case MOD_LOAD: 7437 if (loaded) 7438 goto out; 7439 newnfs_portinit(); 7440 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF); 7441 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF); 7442 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF); 7443 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF); 7444 #ifdef VV_DISABLEDELEG 7445 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 7446 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 7447 #endif 7448 nfsd_call_nfsd = nfssvc_nfsd; 7449 loaded = 1; 7450 break; 7451 7452 case MOD_UNLOAD: 7453 if (newnfs_numnfsd != 0) { 7454 error = EBUSY; 7455 break; 7456 } 7457 7458 #ifdef VV_DISABLEDELEG 7459 vn_deleg_ops.vndeleg_recall = NULL; 7460 vn_deleg_ops.vndeleg_disable = NULL; 7461 #endif 7462 nfsd_call_nfsd = NULL; 7463 mtx_destroy(&nfsrc_udpmtx); 7464 mtx_destroy(&nfs_v4root_mutex); 7465 mtx_destroy(&nfsrv_dontlistlock_mtx); 7466 mtx_destroy(&nfsrv_recalllock_mtx); 7467 if (nfslayouthash != NULL) { 7468 for (i = 0; i < nfsrv_layouthashsize; i++) 7469 mtx_destroy(&nfslayouthash[i].mtx); 7470 free(nfslayouthash, M_NFSDSESSION); 7471 } 7472 loaded = 0; 7473 break; 7474 default: 7475 error = EOPNOTSUPP; 7476 break; 7477 } 7478 7479 out: 7480 NFSEXITCODE(error); 7481 return (error); 7482 } 7483 static moduledata_t nfsd_mod = { 7484 "nfsd", 7485 nfsd_modevent, 7486 NULL, 7487 }; 7488 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 7489 7490 /* So that loader and kldload(2) can find us, wherever we are.. */ 7491 MODULE_VERSION(nfsd, 1); 7492 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 7493 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 7494 MODULE_DEPEND(nfsd, krpc, 1, 1, 1); 7495 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 7496