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