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