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