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