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