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