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