1 /* $NetBSD: nfs_serv.c,v 1.146 2009/05/23 15:31:21 ad Exp $ */ 2 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 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95 35 */ 36 37 /* 38 * nfs version 2 and 3 server calls to vnode ops 39 * - these routines generally have 3 phases 40 * 1 - break down and validate rpc request in mbuf list 41 * 2 - do the vnode ops for the request 42 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c) 43 * 3 - build the rpc reply in an mbuf list 44 * nb: 45 * - do not mix the phases, since the nfsm_?? macros can return failures 46 * on a bad rpc or similar and do not do any vrele() or vput()'s 47 * 48 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs 49 * error number iff error != 0 whereas 50 * returning an error from the server function implies a fatal error 51 * such as a badly constructed rpc request that should be dropped without 52 * a reply. 53 * For Version 3, nfsm_reply() does not return for the error case, since 54 * most version 3 rpcs return more than the status for error cases. 55 */ 56 57 #include <sys/cdefs.h> 58 __KERNEL_RCSID(0, "$NetBSD: nfs_serv.c,v 1.146 2009/05/23 15:31:21 ad Exp $"); 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/proc.h> 63 #include <sys/file.h> 64 #include <sys/namei.h> 65 #include <sys/vnode.h> 66 #include <sys/mount.h> 67 #include <sys/socket.h> 68 #include <sys/socketvar.h> 69 #include <sys/mbuf.h> 70 #include <sys/dirent.h> 71 #include <sys/stat.h> 72 #include <sys/kernel.h> 73 #include <sys/hash.h> 74 #include <sys/kauth.h> 75 #include <sys/module.h> 76 #include <sys/syscall.h> 77 #include <sys/syscallargs.h> 78 #include <sys/syscallvar.h> 79 80 #include <uvm/uvm.h> 81 82 #include <nfs/nfsproto.h> 83 #include <nfs/rpcv2.h> 84 #include <nfs/nfs.h> 85 #include <nfs/xdr_subs.h> 86 #include <nfs/nfsm_subs.h> 87 #include <nfs/nfs_var.h> 88 89 MODULE(MODULE_CLASS_MISC, nfsserver, "nfs"); 90 91 /* Global vars */ 92 extern u_int32_t nfs_xdrneg1; 93 extern u_int32_t nfs_false, nfs_true; 94 extern const enum vtype nv3tov_type[8]; 95 extern struct nfsstats nfsstats; 96 extern const nfstype nfsv2_type[9]; 97 extern const nfstype nfsv3_type[9]; 98 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000; 99 bool nfsd_use_loan = true; /* use page-loan for READ OP */ 100 101 #define nqsrv_getl(vp, rw) /* nothing */ 102 103 static const struct syscall_package nfsserver_syscalls[] = { 104 { SYS_nfssvc, 0, (sy_call_t *)sys_nfssvc }, 105 { 0, 0, NULL }, 106 }; 107 108 static int 109 nfsserver_modcmd(modcmd_t cmd, void *arg) 110 { 111 extern struct vfs_hooks nfs_export_hooks; /* XXX */ 112 int error; 113 114 switch (cmd) { 115 case MODULE_CMD_INIT: 116 error = syscall_establish(NULL, nfsserver_syscalls); 117 if (error != 0) { 118 return error; 119 } 120 nfs_init(); /* XXX for monolithic kernel */ 121 netexport_init(); 122 nfsrv_initcache(); /* Init the server request cache */ 123 nfsrv_init(0); /* Init server data structures */ 124 vfs_hooks_attach(&nfs_export_hooks); 125 nfs_timer_srvinit(nfsrv_timer); 126 return 0; 127 case MODULE_CMD_FINI: 128 error = syscall_disestablish(NULL, nfsserver_syscalls); 129 if (error != 0) { 130 return error; 131 } 132 /* 133 * Kill export list before detaching VFS hooks, so we 134 * we don't leak state due to a concurrent umount(). 135 */ 136 netexport_fini(); 137 vfs_hooks_detach(&nfs_export_hooks); 138 139 /* Kill timer before server goes away. */ 140 nfs_timer_srvfini(); 141 nfsrv_fini(); 142 143 /* Server uses server cache, so kill cache last. */ 144 nfsrv_finicache(); 145 return 0; 146 default: 147 return ENOTTY; 148 } 149 } 150 151 /* 152 * nfs v3 access service 153 */ 154 int 155 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 156 { 157 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 158 struct mbuf *nam = nfsd->nd_nam; 159 char *dpos = nfsd->nd_dpos; 160 kauth_cred_t cred = nfsd->nd_cr; 161 struct vnode *vp; 162 nfsrvfh_t nsfh; 163 u_int32_t *tl; 164 int32_t t1; 165 char *bpos; 166 int error = 0, rdonly, cache = 0, getret; 167 char *cp2; 168 struct mbuf *mb, *mreq; 169 struct vattr va; 170 u_long inmode, testmode, outmode; 171 u_quad_t frev; 172 173 nfsm_srvmtofh(&nsfh); 174 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 175 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly, 176 (nfsd->nd_flag & ND_KERBAUTH), false); 177 if (error) { 178 nfsm_reply(NFSX_UNSIGNED); 179 nfsm_srvpostop_attr(1, (struct vattr *)0); 180 return (0); 181 } 182 inmode = fxdr_unsigned(u_int32_t, *tl); 183 outmode = 0; 184 if ((inmode & NFSV3ACCESS_READ) && 185 nfsrv_access(vp, VREAD, cred, rdonly, lwp, 0) == 0) 186 outmode |= NFSV3ACCESS_READ; 187 if (vp->v_type != VDIR) { 188 testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND); 189 if (testmode && 190 nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0) 191 outmode |= testmode; 192 if ((inmode & NFSV3ACCESS_EXECUTE) && 193 nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0) 194 outmode |= NFSV3ACCESS_EXECUTE; 195 } else { 196 testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND | 197 NFSV3ACCESS_DELETE); 198 if (testmode && 199 nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 0) == 0) 200 outmode |= testmode; 201 if ((inmode & NFSV3ACCESS_LOOKUP) && 202 nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0) == 0) 203 outmode |= NFSV3ACCESS_LOOKUP; 204 } 205 getret = VOP_GETATTR(vp, &va, cred); 206 vput(vp); 207 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED); 208 nfsm_srvpostop_attr(getret, &va); 209 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 210 *tl = txdr_unsigned(outmode); 211 nfsm_srvdone; 212 } 213 214 /* 215 * nfs getattr service 216 */ 217 int 218 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 219 { 220 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 221 struct mbuf *nam = nfsd->nd_nam; 222 char *dpos = nfsd->nd_dpos; 223 kauth_cred_t cred = nfsd->nd_cr; 224 struct nfs_fattr *fp; 225 struct vattr va; 226 struct vnode *vp; 227 nfsrvfh_t nsfh; 228 u_int32_t *tl; 229 int32_t t1; 230 char *bpos; 231 int error = 0, rdonly, cache = 0; 232 char *cp2; 233 struct mbuf *mb, *mreq; 234 u_quad_t frev; 235 236 nfsm_srvmtofh(&nsfh); 237 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly, 238 (nfsd->nd_flag & ND_KERBAUTH), false); 239 if (error) { 240 nfsm_reply(0); 241 return (0); 242 } 243 nqsrv_getl(vp, ND_READ); 244 error = VOP_GETATTR(vp, &va, cred); 245 vput(vp); 246 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3)); 247 if (error) 248 return (0); 249 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3)); 250 nfsm_srvfillattr(&va, fp); 251 nfsm_srvdone; 252 } 253 254 /* 255 * nfs setattr service 256 */ 257 int 258 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 259 { 260 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 261 struct mbuf *nam = nfsd->nd_nam; 262 char *dpos = nfsd->nd_dpos; 263 kauth_cred_t cred = nfsd->nd_cr; 264 struct vattr va, preat; 265 struct nfsv2_sattr *sp; 266 struct nfs_fattr *fp; 267 struct vnode *vp; 268 nfsrvfh_t nsfh; 269 u_int32_t *tl; 270 int32_t t1; 271 char *bpos; 272 int error = 0, rdonly, cache = 0, preat_ret = 1, postat_ret = 1; 273 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0; 274 char *cp2; 275 struct mbuf *mb, *mreq; 276 u_quad_t frev; 277 struct timespec guard; 278 279 memset(&guard, 0, sizeof guard); /* XXX gcc */ 280 281 nfsm_srvmtofh(&nsfh); 282 VATTR_NULL(&va); 283 if (v3) { 284 nfsm_srvsattr(&va); 285 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 286 gcheck = fxdr_unsigned(int, *tl); 287 if (gcheck) { 288 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 289 fxdr_nfsv3time(tl, &guard); 290 } 291 } else { 292 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 293 /* 294 * Nah nah nah nah na nah 295 * There is a bug in the Sun client that puts 0xffff in the mode 296 * field of sattr when it should put in 0xffffffff. The u_short 297 * doesn't sign extend. 298 * --> check the low order 2 bytes for 0xffff 299 */ 300 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 301 va.va_mode = nfstov_mode(sp->sa_mode); 302 if (sp->sa_uid != nfs_xdrneg1) 303 va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid); 304 if (sp->sa_gid != nfs_xdrneg1) 305 va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid); 306 if (sp->sa_size != nfs_xdrneg1) 307 va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size); 308 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) { 309 #ifdef notyet 310 fxdr_nfsv2time(&sp->sa_atime, &va.va_atime); 311 #else 312 va.va_atime.tv_sec = 313 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 314 va.va_atime.tv_nsec = 0; 315 #endif 316 } 317 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1) 318 fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime); 319 320 } 321 322 /* 323 * Now that we have all the fields, lets do it. 324 */ 325 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, &rdonly, 326 (nfsd->nd_flag & ND_KERBAUTH), false); 327 if (error) { 328 nfsm_reply(2 * NFSX_UNSIGNED); 329 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va); 330 return (0); 331 } 332 nqsrv_getl(vp, ND_WRITE); 333 if (v3) { 334 error = preat_ret = VOP_GETATTR(vp, &preat, cred); 335 if (!error && gcheck && 336 (preat.va_ctime.tv_sec != guard.tv_sec || 337 preat.va_ctime.tv_nsec != guard.tv_nsec)) 338 error = NFSERR_NOT_SYNC; 339 if (error) { 340 vput(vp); 341 nfsm_reply(NFSX_WCCDATA(v3)); 342 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va); 343 return (0); 344 } 345 } 346 347 /* 348 * If the size is being changed write acces is required, otherwise 349 * just check for a read only file system. 350 */ 351 if (va.va_size == ((u_quad_t)((quad_t) -1))) { 352 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) { 353 error = EROFS; 354 goto out; 355 } 356 } else { 357 if (vp->v_type == VDIR) { 358 error = EISDIR; 359 goto out; 360 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly, 361 lwp, 0)) != 0) 362 goto out; 363 } 364 error = VOP_SETATTR(vp, &va, cred); 365 postat_ret = VOP_GETATTR(vp, &va, cred); 366 if (!error) 367 error = postat_ret; 368 out: 369 vput(vp); 370 nfsm_reply(NFSX_WCCORFATTR(v3)); 371 if (v3) { 372 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va); 373 return (0); 374 } else { 375 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 376 nfsm_srvfillattr(&va, fp); 377 } 378 nfsm_srvdone; 379 } 380 381 /* 382 * nfs lookup rpc 383 */ 384 int 385 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 386 { 387 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 388 struct mbuf *nam = nfsd->nd_nam; 389 char *dpos = nfsd->nd_dpos; 390 kauth_cred_t cred = nfsd->nd_cr; 391 struct nfs_fattr *fp; 392 struct nameidata nd, ind, *ndp = &nd; 393 struct vnode *vp, *dirp; 394 nfsrvfh_t nsfh; 395 char *cp; 396 u_int32_t *tl; 397 int32_t t1; 398 char *bpos; 399 int error = 0, cache = 0, dirattr_ret = 1; 400 uint32_t len; 401 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag; 402 char *cp2; 403 struct mbuf *mb, *mreq; 404 struct vattr va, dirattr; 405 u_quad_t frev; 406 407 nfsm_srvmtofh(&nsfh); 408 nfsm_srvnamesiz(len); 409 410 pubflag = nfs_ispublicfh(&nsfh); 411 412 nd.ni_cnd.cn_cred = cred; 413 nd.ni_cnd.cn_nameiop = LOOKUP; 414 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART; 415 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 416 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), pubflag); 417 418 if (!error && pubflag) { 419 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) { 420 /* 421 * Setup call to lookup() to see if we can find 422 * the index file. Arguably, this doesn't belong 423 * in a kernel.. Ugh. 424 */ 425 ind = nd; 426 VOP_UNLOCK(nd.ni_vp, 0); 427 ind.ni_pathlen = strlen(nfs_pub.np_index); 428 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf = 429 nfs_pub.np_index; 430 ind.ni_startdir = nd.ni_vp; 431 VREF(ind.ni_startdir); 432 error = lookup(&ind); 433 if (!error) { 434 /* 435 * Found an index file. Get rid of 436 * the old references. 437 */ 438 if (dirp) 439 vrele(dirp); 440 dirp = nd.ni_vp; 441 vrele(nd.ni_startdir); 442 ndp = &ind; 443 } else 444 error = 0; 445 } 446 /* 447 * If the public filehandle was used, check that this lookup 448 * didn't result in a filehandle outside the publicly exported 449 * filesystem. 450 */ 451 452 if (!error && ndp->ni_vp->v_mount != nfs_pub.np_mount) { 453 vput(nd.ni_vp); 454 error = EPERM; 455 } 456 } 457 458 if (dirp) { 459 if (v3) 460 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred); 461 vrele(dirp); 462 } 463 464 if (error) { 465 nfsm_reply(NFSX_POSTOPATTR(v3)); 466 nfsm_srvpostop_attr(dirattr_ret, &dirattr); 467 return (0); 468 } 469 470 nqsrv_getl(ndp->ni_startdir, ND_READ); 471 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 472 vp = ndp->ni_vp; 473 error = nfsrv_composefh(vp, &nsfh, v3); 474 if (!error) 475 error = VOP_GETATTR(vp, &va, cred); 476 vput(vp); 477 vrele(ndp->ni_startdir); 478 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPORFATTR(v3) + 479 NFSX_POSTOPATTR(v3)); 480 if (error) { 481 nfsm_srvpostop_attr(dirattr_ret, &dirattr); 482 return (0); 483 } 484 nfsm_srvfhtom(&nsfh, v3); 485 if (v3) { 486 nfsm_srvpostop_attr(0, &va); 487 nfsm_srvpostop_attr(dirattr_ret, &dirattr); 488 } else { 489 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 490 nfsm_srvfillattr(&va, fp); 491 } 492 nfsm_srvdone; 493 } 494 495 /* 496 * nfs readlink service 497 */ 498 int 499 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 500 { 501 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 502 struct mbuf *nam = nfsd->nd_nam; 503 char *dpos = nfsd->nd_dpos; 504 kauth_cred_t cred = nfsd->nd_cr; 505 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 506 struct iovec *ivp = iv; 507 struct mbuf *mp; 508 u_int32_t *tl; 509 int32_t t1; 510 char *bpos; 511 int error = 0, rdonly, cache = 0, i, padlen, getret; 512 uint32_t len; 513 int v3 = (nfsd->nd_flag & ND_NFSV3); 514 char *cp2; 515 struct mbuf *mb, *mp2 = NULL, *mp3 = NULL, *mreq; 516 struct vnode *vp; 517 struct vattr attr; 518 nfsrvfh_t nsfh; 519 struct uio io, *uiop = &io; 520 u_quad_t frev; 521 522 nfsm_srvmtofh(&nsfh); 523 len = 0; 524 i = 0; 525 while (len < NFS_MAXPATHLEN) { 526 mp = m_get(M_WAIT, MT_DATA); 527 MCLAIM(mp, &nfs_mowner); 528 m_clget(mp, M_WAIT); 529 mp->m_len = NFSMSIZ(mp); 530 if (len == 0) 531 mp3 = mp2 = mp; 532 else { 533 mp2->m_next = mp; 534 mp2 = mp; 535 } 536 if ((len+mp->m_len) > NFS_MAXPATHLEN) { 537 mp->m_len = NFS_MAXPATHLEN-len; 538 len = NFS_MAXPATHLEN; 539 } else 540 len += mp->m_len; 541 ivp->iov_base = mtod(mp, void *); 542 ivp->iov_len = mp->m_len; 543 i++; 544 ivp++; 545 } 546 uiop->uio_iov = iv; 547 uiop->uio_iovcnt = i; 548 uiop->uio_offset = 0; 549 uiop->uio_resid = len; 550 uiop->uio_rw = UIO_READ; 551 UIO_SETUP_SYSSPACE(uiop); 552 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 553 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 554 if (error) { 555 m_freem(mp3); 556 nfsm_reply(2 * NFSX_UNSIGNED); 557 nfsm_srvpostop_attr(1, (struct vattr *)0); 558 return (0); 559 } 560 if (vp->v_type != VLNK) { 561 if (v3) 562 error = EINVAL; 563 else 564 error = ENXIO; 565 goto out; 566 } 567 nqsrv_getl(vp, ND_READ); 568 error = VOP_READLINK(vp, uiop, cred); 569 out: 570 getret = VOP_GETATTR(vp, &attr, cred); 571 vput(vp); 572 if (error) 573 m_freem(mp3); 574 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED); 575 if (v3) { 576 nfsm_srvpostop_attr(getret, &attr); 577 if (error) 578 return (0); 579 } 580 len -= uiop->uio_resid; 581 padlen = nfsm_padlen(len); 582 if (uiop->uio_resid || padlen) 583 nfs_zeropad(mp3, uiop->uio_resid, padlen); 584 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED); 585 *tl = txdr_unsigned(len); 586 mb->m_next = mp3; 587 nfsm_srvdone; 588 } 589 590 /* 591 * nfs read service 592 */ 593 int 594 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 595 { 596 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 597 struct mbuf *nam = nfsd->nd_nam; 598 char *dpos = nfsd->nd_dpos; 599 kauth_cred_t cred = nfsd->nd_cr; 600 struct mbuf *m; 601 struct nfs_fattr *fp; 602 u_int32_t *tl; 603 int32_t t1; 604 int i; 605 char *bpos; 606 int error = 0, rdonly, cache = 0, getret; 607 int v3 = (nfsd->nd_flag & ND_NFSV3); 608 uint32_t reqlen, len, cnt, left; 609 int padlen; 610 char *cp2; 611 struct mbuf *mb, *mreq; 612 struct vnode *vp; 613 nfsrvfh_t nsfh; 614 struct uio io, *uiop = &io; 615 struct vattr va; 616 off_t off; 617 u_quad_t frev; 618 619 nfsm_srvmtofh(&nsfh); 620 if (v3) { 621 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 622 off = fxdr_hyper(tl); 623 } else { 624 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 625 off = (off_t)fxdr_unsigned(u_int32_t, *tl); 626 } 627 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED); 628 reqlen = fxdr_unsigned(uint32_t, *tl); 629 reqlen = MIN(reqlen, NFS_SRVMAXDATA(nfsd)); 630 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 631 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 632 if (error) { 633 nfsm_reply(2 * NFSX_UNSIGNED); 634 nfsm_srvpostop_attr(1, (struct vattr *)0); 635 return (0); 636 } 637 if (vp->v_type != VREG) { 638 if (v3) 639 error = EINVAL; 640 else 641 error = (vp->v_type == VDIR) ? EISDIR : EACCES; 642 } 643 if (!error) { 644 nqsrv_getl(vp, ND_READ); 645 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, lwp, 1)) != 0) 646 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 1); 647 } 648 getret = VOP_GETATTR(vp, &va, cred); 649 if (!error) 650 error = getret; 651 if (error) { 652 vput(vp); 653 nfsm_reply(NFSX_POSTOPATTR(v3)); 654 nfsm_srvpostop_attr(getret, &va); 655 return (0); 656 } 657 if (off >= va.va_size) 658 cnt = 0; 659 else if ((off + reqlen) > va.va_size) 660 cnt = va.va_size - off; 661 else 662 cnt = reqlen; 663 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt)); 664 if (v3) { 665 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED); 666 *tl++ = nfs_true; 667 fp = (struct nfs_fattr *)tl; 668 tl += (NFSX_V3FATTR / sizeof (u_int32_t)); 669 } else { 670 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED); 671 fp = (struct nfs_fattr *)tl; 672 tl += (NFSX_V2FATTR / sizeof (u_int32_t)); 673 } 674 len = left = cnt; 675 if (cnt > 0) { 676 if (nfsd_use_loan) { 677 struct vm_page **pgpp; 678 voff_t pgoff = trunc_page(off); 679 int npages; 680 vaddr_t lva; 681 682 npages = (round_page(off + cnt) - pgoff) >> PAGE_SHIFT; 683 KASSERT(npages <= M_EXT_MAXPAGES); /* XXX */ 684 685 /* allocate kva for mbuf data */ 686 lva = sokvaalloc(npages << PAGE_SHIFT, slp->ns_so); 687 if (lva == 0) { 688 /* fall back to VOP_READ */ 689 goto loan_fail; 690 } 691 692 /* allocate mbuf */ 693 m = m_get(M_WAIT, MT_DATA); 694 MCLAIM(m, &nfs_mowner); 695 pgpp = m->m_ext.ext_pgs; 696 697 /* loan pages */ 698 error = uvm_loanuobjpages(&vp->v_uobj, pgoff, npages, 699 pgpp); 700 if (error) { 701 sokvafree(lva, npages << PAGE_SHIFT); 702 m_free(m); 703 if (error == EBUSY) 704 goto loan_fail; 705 goto read_error; 706 } 707 708 /* associate kva to mbuf */ 709 MEXTADD(m, (void *)(lva + ((vaddr_t)off & PAGE_MASK)), 710 cnt, M_MBUF, soloanfree, slp->ns_so); 711 m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP; 712 m->m_len = cnt; 713 714 /* map pages */ 715 for (i = 0; i < npages; i++) { 716 pmap_kenter_pa(lva, VM_PAGE_TO_PHYS(pgpp[i]), 717 VM_PROT_READ); 718 lva += PAGE_SIZE; 719 } 720 721 pmap_update(pmap_kernel()); 722 723 mb->m_next = m; 724 mb = m; 725 error = 0; 726 uiop->uio_resid = 0; 727 } else { 728 struct iovec *iv; 729 struct iovec *iv2; 730 struct mbuf *m2; 731 int siz; 732 loan_fail: 733 /* 734 * Generate the mbuf list with the uio_iov ref. to it. 735 */ 736 i = 0; 737 m = m2 = mb; 738 while (left > 0) { 739 siz = min(M_TRAILINGSPACE(m), left); 740 if (siz > 0) { 741 left -= siz; 742 i++; 743 } 744 if (left > 0) { 745 m = m_get(M_WAIT, MT_DATA); 746 MCLAIM(m, &nfs_mowner); 747 m_clget(m, M_WAIT); 748 m->m_len = 0; 749 m2->m_next = m; 750 m2 = m; 751 } 752 } 753 iv = malloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK); 754 uiop->uio_iov = iv2 = iv; 755 m = mb; 756 left = cnt; 757 i = 0; 758 while (left > 0) { 759 if (m == NULL) 760 panic("nfsrv_read iov"); 761 siz = min(M_TRAILINGSPACE(m), left); 762 if (siz > 0) { 763 iv->iov_base = mtod(m, char *) + 764 m->m_len; 765 iv->iov_len = siz; 766 m->m_len += siz; 767 left -= siz; 768 iv++; 769 i++; 770 } 771 m = m->m_next; 772 } 773 uiop->uio_iovcnt = i; 774 uiop->uio_offset = off; 775 uiop->uio_resid = cnt; 776 uiop->uio_rw = UIO_READ; 777 UIO_SETUP_SYSSPACE(uiop); 778 error = VOP_READ(vp, uiop, IO_NODELOCKED, cred); 779 free((void *)iv2, M_TEMP); 780 } 781 read_error: 782 if (error || (getret = VOP_GETATTR(vp, &va, cred)) != 0){ 783 if (!error) 784 error = getret; 785 m_freem(mreq); 786 vput(vp); 787 nfsm_reply(NFSX_POSTOPATTR(v3)); 788 nfsm_srvpostop_attr(getret, &va); 789 return (0); 790 } 791 } else { 792 uiop->uio_resid = 0; 793 } 794 vput(vp); 795 nfsm_srvfillattr(&va, fp); 796 len -= uiop->uio_resid; 797 padlen = nfsm_padlen(len); 798 if (uiop->uio_resid || padlen) 799 nfs_zeropad(mb, uiop->uio_resid, padlen); 800 if (v3) { 801 /* count */ 802 *tl++ = txdr_unsigned(len); 803 /* eof */ 804 if (off + len >= va.va_size) 805 *tl++ = nfs_true; 806 else 807 *tl++ = nfs_false; 808 } 809 *tl = txdr_unsigned(len); 810 nfsm_srvdone; 811 } 812 813 /* 814 * nfs write service 815 */ 816 int 817 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 818 { 819 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 820 struct mbuf *nam = nfsd->nd_nam; 821 char *dpos = nfsd->nd_dpos; 822 kauth_cred_t cred = nfsd->nd_cr; 823 struct iovec *ivp; 824 int i, cnt; 825 struct mbuf *mp; 826 struct nfs_fattr *fp; 827 struct iovec *iv; 828 struct vattr va, forat; 829 u_int32_t *tl; 830 int32_t t1; 831 char *bpos; 832 int error = 0, rdonly, cache = 0, len, forat_ret = 1; 833 int ioflags, aftat_ret = 1, retlen, zeroing, adjust; 834 int stable = NFSV3WRITE_FILESYNC; 835 int v3 = (nfsd->nd_flag & ND_NFSV3); 836 char *cp2; 837 struct mbuf *mb, *mreq; 838 struct vnode *vp; 839 nfsrvfh_t nsfh; 840 struct uio io, *uiop = &io; 841 off_t off; 842 u_quad_t frev; 843 844 if (mrep == NULL) { 845 *mrq = NULL; 846 return (0); 847 } 848 nfsm_srvmtofh(&nsfh); 849 if (v3) { 850 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 851 off = fxdr_hyper(tl); 852 tl += 3; 853 stable = fxdr_unsigned(int, *tl++); 854 } else { 855 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 856 off = (off_t)fxdr_unsigned(u_int32_t, *++tl); 857 tl += 2; 858 } 859 retlen = len = fxdr_unsigned(int32_t, *tl); 860 cnt = i = 0; 861 862 /* 863 * For NFS Version 2, it is not obvious what a write of zero length 864 * should do, but I might as well be consistent with Version 3, 865 * which is to return ok so long as there are no permission problems. 866 */ 867 if (len > 0) { 868 zeroing = 1; 869 mp = mrep; 870 while (mp) { 871 if (mp == md) { 872 zeroing = 0; 873 adjust = dpos - mtod(mp, char *); 874 mp->m_len -= adjust; 875 if (mp->m_len > 0 && adjust > 0) 876 NFSMADV(mp, adjust); 877 } 878 if (zeroing) 879 mp->m_len = 0; 880 else if (mp->m_len > 0) { 881 i += mp->m_len; 882 if (i > len) { 883 mp->m_len -= (i - len); 884 zeroing = 1; 885 } 886 if (mp->m_len > 0) 887 cnt++; 888 } 889 mp = mp->m_next; 890 } 891 } 892 if (len > NFS_MAXDATA || len < 0 || i < len) { 893 error = EIO; 894 nfsm_reply(2 * NFSX_UNSIGNED); 895 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 896 return (0); 897 } 898 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 899 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 900 if (error) { 901 nfsm_reply(2 * NFSX_UNSIGNED); 902 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 903 return (0); 904 } 905 if (v3) 906 forat_ret = VOP_GETATTR(vp, &forat, cred); 907 if (vp->v_type != VREG) { 908 if (v3) 909 error = EINVAL; 910 else 911 error = (vp->v_type == VDIR) ? EISDIR : EACCES; 912 } 913 if (!error) { 914 nqsrv_getl(vp, ND_WRITE); 915 error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1); 916 } 917 if (error) { 918 vput(vp); 919 nfsm_reply(NFSX_WCCDATA(v3)); 920 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 921 return (0); 922 } 923 924 if (len > 0) { 925 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP, M_WAITOK); 926 uiop->uio_iov = iv = ivp; 927 uiop->uio_iovcnt = cnt; 928 mp = mrep; 929 while (mp) { 930 if (mp->m_len > 0) { 931 ivp->iov_base = mtod(mp, void *); 932 ivp->iov_len = mp->m_len; 933 ivp++; 934 } 935 mp = mp->m_next; 936 } 937 938 /* 939 * XXX 940 * The IO_METASYNC flag indicates that all metadata (and not 941 * just enough to ensure data integrity) must be written to 942 * stable storage synchronously. 943 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.) 944 */ 945 if (stable == NFSV3WRITE_UNSTABLE) 946 ioflags = IO_NODELOCKED; 947 else if (stable == NFSV3WRITE_DATASYNC) 948 ioflags = (IO_SYNC | IO_NODELOCKED); 949 else 950 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED); 951 uiop->uio_resid = len; 952 uiop->uio_rw = UIO_WRITE; 953 uiop->uio_offset = off; 954 UIO_SETUP_SYSSPACE(uiop); 955 error = VOP_WRITE(vp, uiop, ioflags, cred); 956 nfsstats.srvvop_writes++; 957 free(iv, M_TEMP); 958 } 959 aftat_ret = VOP_GETATTR(vp, &va, cred); 960 vput(vp); 961 if (!error) 962 error = aftat_ret; 963 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) + 964 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3)); 965 if (v3) { 966 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 967 if (error) 968 return (0); 969 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 970 *tl++ = txdr_unsigned(retlen); 971 if (stable == NFSV3WRITE_UNSTABLE) 972 *tl++ = txdr_unsigned(stable); 973 else 974 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC); 975 /* 976 * Actually, there is no need to txdr these fields, 977 * but it may make the values more human readable, 978 * for debugging purposes. 979 */ 980 *tl++ = txdr_unsigned(boottime.tv_sec); 981 *tl = txdr_unsigned(boottime.tv_nsec / 1000); 982 } else { 983 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 984 nfsm_srvfillattr(&va, fp); 985 } 986 nfsm_srvdone; 987 } 988 989 /* 990 * XXX elad: the original NFSW_SAMECRED() macro also made sure the 991 * two nd_flag fields of the descriptors contained 992 * ND_KERBAUTH. 993 */ 994 static int 995 nfsrv_samecred(kauth_cred_t cred1, kauth_cred_t cred2) 996 { 997 int i, do_ngroups; 998 999 if (kauth_cred_geteuid(cred1) != kauth_cred_geteuid(cred2)) 1000 return (0); 1001 if (kauth_cred_ngroups(cred1) != kauth_cred_ngroups(cred2)) 1002 return (0); 1003 do_ngroups = kauth_cred_ngroups(cred1); 1004 for (i = 0; i < do_ngroups; i++) 1005 if (kauth_cred_group(cred1, i) != 1006 kauth_cred_group(cred2, i)) 1007 return (0); 1008 1009 return (1); 1010 } 1011 1012 static struct nfsrvw_delayhash * 1013 nfsrv_nwdelayhash(struct nfssvc_sock *slp, const nfsrvfh_t *nsfh) 1014 { 1015 uint32_t hash; 1016 1017 hash = hash32_buf(NFSRVFH_DATA(nsfh), NFSRVFH_SIZE(nsfh), 1018 HASH32_BUF_INIT); 1019 return &slp->ns_wdelayhashtbl[hash % NFS_WDELAYHASHSIZ]; 1020 } 1021 1022 /* 1023 * NFS write service with write gathering support. Called when 1024 * nfsrvw_procrastinate > 0. 1025 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server", 1026 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco, 1027 * Jan. 1994. 1028 */ 1029 int 1030 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 1031 { 1032 struct timeval now; 1033 struct iovec *ivp; 1034 struct mbuf *mp; 1035 struct nfsrv_descript *wp, *nfsd, *owp, *swp; 1036 struct nfs_fattr *fp; 1037 int i = 0; 1038 struct iovec *iov; 1039 struct nfsrvw_delayhash *wpp; 1040 kauth_cred_t cred; 1041 struct vattr va, forat; 1042 u_int32_t *tl; 1043 int32_t t1; 1044 char *bpos, *dpos; 1045 int error = 0, rdonly, cache = 0, len = 0, forat_ret = 1; 1046 int ioflags, aftat_ret = 1, adjust, v3, zeroing; 1047 char *cp2; 1048 struct mbuf *mb, *mreq, *mrep, *md; 1049 struct vnode *vp; 1050 struct uio io, *uiop = &io; 1051 u_quad_t frev, cur_usec; 1052 1053 *mrq = NULL; 1054 if (*ndp) { 1055 nfsd = *ndp; 1056 *ndp = NULL; 1057 mrep = nfsd->nd_mrep; 1058 md = nfsd->nd_md; 1059 dpos = nfsd->nd_dpos; 1060 cred = nfsd->nd_cr; 1061 v3 = (nfsd->nd_flag & ND_NFSV3); 1062 LIST_INIT(&nfsd->nd_coalesce); 1063 nfsd->nd_mreq = NULL; 1064 nfsd->nd_stable = NFSV3WRITE_FILESYNC; 1065 getmicrotime(&now); 1066 cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec; 1067 nfsd->nd_time = cur_usec + nfsrvw_procrastinate; 1068 1069 /* 1070 * Now, get the write header.. 1071 */ 1072 nfsm_srvmtofh(&nfsd->nd_fh); 1073 if (v3) { 1074 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1075 nfsd->nd_off = fxdr_hyper(tl); 1076 tl += 3; 1077 nfsd->nd_stable = fxdr_unsigned(int, *tl++); 1078 } else { 1079 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1080 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl); 1081 tl += 2; 1082 } 1083 len = fxdr_unsigned(int32_t, *tl); 1084 nfsd->nd_len = len; 1085 nfsd->nd_eoff = nfsd->nd_off + len; 1086 1087 /* 1088 * Trim the header out of the mbuf list and trim off any trailing 1089 * junk so that the mbuf list has only the write data. 1090 */ 1091 zeroing = 1; 1092 i = 0; 1093 mp = mrep; 1094 while (mp) { 1095 if (mp == md) { 1096 zeroing = 0; 1097 adjust = dpos - mtod(mp, char *); 1098 mp->m_len -= adjust; 1099 if (mp->m_len > 0 && adjust > 0) 1100 NFSMADV(mp, adjust); 1101 } 1102 if (zeroing) 1103 mp->m_len = 0; 1104 else { 1105 i += mp->m_len; 1106 if (i > len) { 1107 mp->m_len -= (i - len); 1108 zeroing = 1; 1109 } 1110 } 1111 mp = mp->m_next; 1112 } 1113 if (len > NFS_MAXDATA || len < 0 || i < len) { 1114 nfsmout: 1115 m_freem(mrep); 1116 error = EIO; 1117 nfsm_writereply(2 * NFSX_UNSIGNED, v3); 1118 if (v3) 1119 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 1120 nfsd->nd_mreq = mreq; 1121 nfsd->nd_mrep = NULL; 1122 nfsd->nd_time = 0; 1123 } 1124 1125 /* 1126 * Add this entry to the hash and time queues. 1127 */ 1128 owp = NULL; 1129 mutex_enter(&nfsd_lock); 1130 wp = LIST_FIRST(&slp->ns_tq); 1131 while (wp && wp->nd_time < nfsd->nd_time) { 1132 owp = wp; 1133 wp = LIST_NEXT(wp, nd_tq); 1134 } 1135 if (owp) { 1136 LIST_INSERT_AFTER(owp, nfsd, nd_tq); 1137 } else { 1138 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq); 1139 } 1140 if (nfsd->nd_mrep) { 1141 wpp = nfsrv_nwdelayhash(slp, &nfsd->nd_fh); 1142 owp = NULL; 1143 wp = LIST_FIRST(wpp); 1144 while (wp && nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) { 1145 owp = wp; 1146 wp = LIST_NEXT(wp, nd_hash); 1147 } 1148 while (wp && wp->nd_off < nfsd->nd_off && 1149 !nfsrv_comparefh(&nfsd->nd_fh, &wp->nd_fh)) { 1150 owp = wp; 1151 wp = LIST_NEXT(wp, nd_hash); 1152 } 1153 if (owp) { 1154 LIST_INSERT_AFTER(owp, nfsd, nd_hash); 1155 1156 /* 1157 * Search the hash list for overlapping entries and 1158 * coalesce. 1159 */ 1160 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) { 1161 wp = LIST_NEXT(nfsd, nd_hash); 1162 if (nfsrv_samecred(owp->nd_cr, nfsd->nd_cr)) 1163 nfsrvw_coalesce(owp, nfsd); 1164 } 1165 } else { 1166 LIST_INSERT_HEAD(wpp, nfsd, nd_hash); 1167 } 1168 } 1169 mutex_exit(&nfsd_lock); 1170 } 1171 1172 /* 1173 * Now, do VOP_WRITE()s for any one(s) that need to be done now 1174 * and generate the associated reply mbuf list(s). 1175 */ 1176 loop1: 1177 getmicrotime(&now); 1178 cur_usec = (u_quad_t)now.tv_sec * 1000000 + (u_quad_t)now.tv_usec; 1179 mutex_enter(&nfsd_lock); 1180 for (nfsd = LIST_FIRST(&slp->ns_tq); nfsd; nfsd = owp) { 1181 owp = LIST_NEXT(nfsd, nd_tq); 1182 if (nfsd->nd_time > cur_usec) 1183 break; 1184 if (nfsd->nd_mreq) 1185 continue; 1186 LIST_REMOVE(nfsd, nd_tq); 1187 LIST_REMOVE(nfsd, nd_hash); 1188 mutex_exit(&nfsd_lock); 1189 1190 mrep = nfsd->nd_mrep; 1191 nfsd->nd_mrep = NULL; 1192 cred = nfsd->nd_cr; 1193 v3 = (nfsd->nd_flag & ND_NFSV3); 1194 forat_ret = aftat_ret = 1; 1195 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp, 1196 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), 1197 false); 1198 if (!error) { 1199 if (v3) 1200 forat_ret = VOP_GETATTR(vp, &forat, cred); 1201 if (vp->v_type != VREG) { 1202 if (v3) 1203 error = EINVAL; 1204 else 1205 error = (vp->v_type == VDIR) ? EISDIR : EACCES; 1206 } 1207 } else 1208 vp = NULL; 1209 if (!error) { 1210 nqsrv_getl(vp, ND_WRITE); 1211 error = nfsrv_access(vp, VWRITE, cred, rdonly, lwp, 1); 1212 } 1213 1214 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE) 1215 ioflags = IO_NODELOCKED; 1216 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC) 1217 ioflags = (IO_SYNC | IO_NODELOCKED); 1218 else 1219 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED); 1220 uiop->uio_rw = UIO_WRITE; 1221 uiop->uio_offset = nfsd->nd_off; 1222 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off; 1223 UIO_SETUP_SYSSPACE(uiop); 1224 if (uiop->uio_resid > 0) { 1225 mp = mrep; 1226 i = 0; 1227 while (mp) { 1228 if (mp->m_len > 0) 1229 i++; 1230 mp = mp->m_next; 1231 } 1232 uiop->uio_iovcnt = i; 1233 iov = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK); 1234 uiop->uio_iov = ivp = iov; 1235 mp = mrep; 1236 while (mp) { 1237 if (mp->m_len > 0) { 1238 ivp->iov_base = mtod(mp, void *); 1239 ivp->iov_len = mp->m_len; 1240 ivp++; 1241 } 1242 mp = mp->m_next; 1243 } 1244 if (!error) { 1245 error = VOP_WRITE(vp, uiop, ioflags, cred); 1246 nfsstats.srvvop_writes++; 1247 } 1248 free((void *)iov, M_TEMP); 1249 } 1250 m_freem(mrep); 1251 if (vp) { 1252 aftat_ret = VOP_GETATTR(vp, &va, cred); 1253 vput(vp); 1254 } 1255 1256 /* 1257 * Loop around generating replies for all write rpcs that have 1258 * now been completed. 1259 */ 1260 swp = nfsd; 1261 do { 1262 if (error) { 1263 nfsm_writereply(NFSX_WCCDATA(v3), v3); 1264 if (v3) { 1265 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 1266 } 1267 } else { 1268 nfsm_writereply(NFSX_PREOPATTR(v3) + 1269 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED + 1270 NFSX_WRITEVERF(v3), v3); 1271 if (v3) { 1272 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va); 1273 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1274 *tl++ = txdr_unsigned(nfsd->nd_len); 1275 *tl++ = txdr_unsigned(swp->nd_stable); 1276 /* 1277 * Actually, there is no need to txdr these fields, 1278 * but it may make the values more human readable, 1279 * for debugging purposes. 1280 */ 1281 *tl++ = txdr_unsigned(boottime.tv_sec); 1282 *tl = txdr_unsigned(boottime.tv_nsec / 1000); 1283 } else { 1284 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 1285 nfsm_srvfillattr(&va, fp); 1286 } 1287 } 1288 nfsd->nd_mreq = mreq; 1289 if (nfsd->nd_mrep) 1290 panic("nfsrv_write: nd_mrep not free"); 1291 1292 /* 1293 * Done. Put it at the head of the timer queue so that 1294 * the final phase can return the reply. 1295 */ 1296 mutex_enter(&nfsd_lock); 1297 if (nfsd != swp) { 1298 nfsd->nd_time = 0; 1299 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq); 1300 } 1301 nfsd = LIST_FIRST(&swp->nd_coalesce); 1302 if (nfsd) { 1303 LIST_REMOVE(nfsd, nd_tq); 1304 } 1305 mutex_exit(&nfsd_lock); 1306 } while (nfsd); 1307 swp->nd_time = 0; 1308 1309 mutex_enter(&nfsd_lock); 1310 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq); 1311 mutex_exit(&nfsd_lock); 1312 goto loop1; 1313 } 1314 mutex_exit(&nfsd_lock); 1315 nfs_timer_start(); 1316 1317 /* 1318 * Search for a reply to return. 1319 */ 1320 mutex_enter(&nfsd_lock); 1321 LIST_FOREACH(nfsd, &slp->ns_tq, nd_tq) { 1322 if (nfsd->nd_mreq) { 1323 LIST_REMOVE(nfsd, nd_tq); 1324 *mrq = nfsd->nd_mreq; 1325 *ndp = nfsd; 1326 break; 1327 } 1328 } 1329 mutex_exit(&nfsd_lock); 1330 return (0); 1331 } 1332 1333 /* 1334 * Coalesce the write request nfsd into owp. To do this we must: 1335 * - remove nfsd from the queues 1336 * - merge nfsd->nd_mrep into owp->nd_mrep 1337 * - update the nd_eoff and nd_stable for owp 1338 * - put nfsd on owp's nd_coalesce list 1339 * NB: Must be called at splsoftclock(). 1340 */ 1341 void 1342 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd) 1343 { 1344 int overlap; 1345 struct mbuf *mp; 1346 struct nfsrv_descript *m; 1347 1348 KASSERT(mutex_owned(&nfsd_lock)); 1349 1350 LIST_REMOVE(nfsd, nd_hash); 1351 LIST_REMOVE(nfsd, nd_tq); 1352 if (owp->nd_eoff < nfsd->nd_eoff) { 1353 overlap = owp->nd_eoff - nfsd->nd_off; 1354 if (overlap < 0) 1355 panic("nfsrv_coalesce: bad off"); 1356 if (overlap > 0) 1357 m_adj(nfsd->nd_mrep, overlap); 1358 mp = owp->nd_mrep; 1359 while (mp->m_next) 1360 mp = mp->m_next; 1361 mp->m_next = nfsd->nd_mrep; 1362 owp->nd_eoff = nfsd->nd_eoff; 1363 } else 1364 m_freem(nfsd->nd_mrep); 1365 nfsd->nd_mrep = NULL; 1366 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC) 1367 owp->nd_stable = NFSV3WRITE_FILESYNC; 1368 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC && 1369 owp->nd_stable == NFSV3WRITE_UNSTABLE) 1370 owp->nd_stable = NFSV3WRITE_DATASYNC; 1371 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq); 1372 /* 1373 * nfsd might hold coalesce elements! Move them to owp. 1374 * Otherwise, requests may be lost and clients will be stuck. 1375 */ 1376 while ((m = LIST_FIRST(&nfsd->nd_coalesce)) != NULL) { 1377 LIST_REMOVE(m, nd_tq); 1378 LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq); 1379 } 1380 } 1381 1382 /* 1383 * nfs create service 1384 * now does a truncate to 0 length via. setattr if it already exists 1385 */ 1386 int 1387 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 1388 { 1389 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1390 struct mbuf *nam = nfsd->nd_nam; 1391 char *dpos = nfsd->nd_dpos; 1392 kauth_cred_t cred = nfsd->nd_cr; 1393 struct nfs_fattr *fp; 1394 struct vattr va, dirfor, diraft; 1395 struct nfsv2_sattr *sp; 1396 u_int32_t *tl; 1397 struct nameidata nd; 1398 char *cp; 1399 int32_t t1; 1400 char *bpos; 1401 int error = 0, cache = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1; 1402 int rdev = 0, abort = 0; 1403 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0; 1404 char *cp2; 1405 struct mbuf *mb, *mreq; 1406 struct vnode *vp = NULL, *dirp = NULL; 1407 nfsrvfh_t nsfh; 1408 u_quad_t frev, tempsize; 1409 u_char cverf[NFSX_V3CREATEVERF]; 1410 1411 nd.ni_cnd.cn_nameiop = 0; 1412 nfsm_srvmtofh(&nsfh); 1413 nfsm_srvnamesiz(len); 1414 nd.ni_cnd.cn_cred = cred; 1415 nd.ni_cnd.cn_nameiop = CREATE; 1416 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 1417 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 1418 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 1419 if (dirp && v3) { 1420 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 1421 } 1422 if (error) { 1423 nfsm_reply(NFSX_WCCDATA(v3)); 1424 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1425 if (dirp) 1426 vrele(dirp); 1427 return (0); 1428 } 1429 abort = 1; 1430 VATTR_NULL(&va); 1431 if (v3) { 1432 va.va_mode = 0; 1433 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1434 how = fxdr_unsigned(int, *tl); 1435 switch (how) { 1436 case NFSV3CREATE_GUARDED: 1437 if (nd.ni_vp) { 1438 error = EEXIST; 1439 break; 1440 } 1441 case NFSV3CREATE_UNCHECKED: 1442 nfsm_srvsattr(&va); 1443 break; 1444 case NFSV3CREATE_EXCLUSIVE: 1445 nfsm_dissect(cp, void *, NFSX_V3CREATEVERF); 1446 memcpy(cverf, cp, NFSX_V3CREATEVERF); 1447 exclusive_flag = 1; 1448 break; 1449 }; 1450 va.va_type = VREG; 1451 } else { 1452 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1453 va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode)); 1454 if (va.va_type == VNON) 1455 va.va_type = VREG; 1456 va.va_mode = nfstov_mode(sp->sa_mode); 1457 switch (va.va_type) { 1458 case VREG: 1459 tsize = fxdr_unsigned(int32_t, sp->sa_size); 1460 if (tsize != -1) 1461 va.va_size = (u_quad_t)tsize; 1462 break; 1463 case VCHR: 1464 case VBLK: 1465 case VFIFO: 1466 rdev = fxdr_unsigned(int32_t, sp->sa_size); 1467 break; 1468 default: 1469 break; 1470 }; 1471 } 1472 1473 /* 1474 * Iff doesn't exist, create it 1475 * otherwise just truncate to 0 length 1476 * should I set the mode too ?? 1477 */ 1478 if (nd.ni_vp == NULL) { 1479 if (va.va_type == VREG || va.va_type == VSOCK) { 1480 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1481 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 1482 if (!error) { 1483 if (exclusive_flag) { 1484 exclusive_flag = 0; 1485 VATTR_NULL(&va); 1486 /* 1487 * XXX 1488 * assuming NFSX_V3CREATEVERF 1489 * == sizeof(nfstime3) 1490 */ 1491 fxdr_nfsv3time(cverf, &va.va_atime); 1492 error = VOP_SETATTR(nd.ni_vp, &va, 1493 cred); 1494 } 1495 } 1496 } else if (va.va_type == VCHR || va.va_type == VBLK || 1497 va.va_type == VFIFO) { 1498 if (va.va_type == VCHR && rdev == 0xffffffff) 1499 va.va_type = VFIFO; 1500 if (va.va_type != VFIFO && 1501 (error = kauth_authorize_system(cred, 1502 KAUTH_SYSTEM_MKNOD, 0, NULL, NULL, NULL))) { 1503 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1504 vput(nd.ni_dvp); 1505 abort = 0; 1506 nfsm_reply(0); 1507 return (error); 1508 } else 1509 va.va_rdev = (dev_t)rdev; 1510 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1511 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, 1512 &va); 1513 if (error) { 1514 nfsm_reply(0); 1515 } 1516 if (nd.ni_cnd.cn_flags & ISSYMLINK) { 1517 vput(nd.ni_vp); 1518 vrele(nd.ni_dvp); 1519 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1520 error = EINVAL; 1521 abort = 0; 1522 nfsm_reply(0); 1523 } 1524 } else { 1525 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1526 vput(nd.ni_dvp); 1527 error = ENXIO; 1528 abort = 0; 1529 } 1530 vp = nd.ni_vp; 1531 } else { 1532 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1533 vp = nd.ni_vp; 1534 if (nd.ni_dvp == vp) 1535 vrele(nd.ni_dvp); 1536 else 1537 vput(nd.ni_dvp); 1538 abort = 0; 1539 if (!error && va.va_size != -1) { 1540 error = nfsrv_access(vp, VWRITE, cred, 1541 (nd.ni_cnd.cn_flags & RDONLY), lwp, 0); 1542 if (!error) { 1543 nqsrv_getl(vp, ND_WRITE); 1544 tempsize = va.va_size; 1545 VATTR_NULL(&va); 1546 va.va_size = tempsize; 1547 error = VOP_SETATTR(vp, &va, cred); 1548 } 1549 } 1550 if (error) 1551 vput(vp); 1552 } 1553 if (!error) { 1554 error = nfsrv_composefh(vp, &nsfh, v3); 1555 if (!error) 1556 error = VOP_GETATTR(vp, &va, cred); 1557 vput(vp); 1558 } 1559 if (v3) { 1560 if (exclusive_flag && !error) { 1561 /* 1562 * XXX assuming NFSX_V3CREATEVERF == sizeof(nfstime3) 1563 */ 1564 char oldverf[NFSX_V3CREATEVERF]; 1565 1566 txdr_nfsv3time(&va.va_atime, oldverf); 1567 if (memcmp(cverf, oldverf, NFSX_V3CREATEVERF)) 1568 error = EEXIST; 1569 } 1570 if (dirp) { 1571 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 1572 } 1573 } 1574 if (dirp) { 1575 vrele(dirp); 1576 dirp = NULL; 1577 } 1578 abort = 0; 1579 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3)); 1580 if (v3) { 1581 if (!error) { 1582 nfsm_srvpostop_fh(&nsfh); 1583 nfsm_srvpostop_attr(0, &va); 1584 } 1585 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1586 } else { 1587 nfsm_srvfhtom(&nsfh, v3); 1588 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 1589 nfsm_srvfillattr(&va, fp); 1590 } 1591 return (0); 1592 nfsmout: 1593 if (dirp) 1594 vrele(dirp); 1595 if (abort) { 1596 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1597 if (nd.ni_dvp == nd.ni_vp) 1598 vrele(nd.ni_dvp); 1599 else 1600 vput(nd.ni_dvp); 1601 if (nd.ni_vp) 1602 vput(nd.ni_vp); 1603 } 1604 return (error); 1605 } 1606 1607 /* 1608 * nfs v3 mknod service 1609 */ 1610 int 1611 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 1612 { 1613 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1614 struct mbuf *nam = nfsd->nd_nam; 1615 char *dpos = nfsd->nd_dpos; 1616 kauth_cred_t cred = nfsd->nd_cr; 1617 struct vattr va, dirfor, diraft; 1618 u_int32_t *tl; 1619 struct nameidata nd; 1620 int32_t t1; 1621 char *bpos; 1622 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1; 1623 int abort = 0; 1624 u_int32_t major, minor; 1625 enum vtype vtyp; 1626 char *cp2; 1627 struct mbuf *mb, *mreq; 1628 struct vnode *vp, *dirp = (struct vnode *)0; 1629 nfsrvfh_t nsfh; 1630 u_quad_t frev; 1631 1632 nd.ni_cnd.cn_nameiop = 0; 1633 nfsm_srvmtofh(&nsfh); 1634 nfsm_srvnamesiz(len); 1635 nd.ni_cnd.cn_cred = cred; 1636 nd.ni_cnd.cn_nameiop = CREATE; 1637 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 1638 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 1639 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 1640 if (dirp) 1641 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 1642 if (error) { 1643 nfsm_reply(NFSX_WCCDATA(1)); 1644 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1645 if (dirp) 1646 vrele(dirp); 1647 return (0); 1648 } 1649 abort = 1; 1650 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1651 vtyp = nfsv3tov_type(*tl); 1652 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 1653 error = NFSERR_BADTYPE; 1654 goto abort; 1655 } 1656 VATTR_NULL(&va); 1657 va.va_mode = 0; 1658 nfsm_srvsattr(&va); 1659 if (vtyp == VCHR || vtyp == VBLK) { 1660 dev_t rdev; 1661 1662 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1663 major = fxdr_unsigned(u_int32_t, *tl++); 1664 minor = fxdr_unsigned(u_int32_t, *tl); 1665 rdev = makedev(major, minor); 1666 if (major(rdev) != major || minor(rdev) != minor) { 1667 error = EINVAL; 1668 goto abort; 1669 } 1670 va.va_rdev = rdev; 1671 } 1672 1673 /* 1674 * Iff doesn't exist, create it. 1675 */ 1676 if (nd.ni_vp) { 1677 error = EEXIST; 1678 abort: 1679 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1680 if (nd.ni_dvp == nd.ni_vp) 1681 vrele(nd.ni_dvp); 1682 else 1683 vput(nd.ni_dvp); 1684 if (nd.ni_vp) 1685 vput(nd.ni_vp); 1686 goto out; 1687 } 1688 va.va_type = vtyp; 1689 if (vtyp == VSOCK) { 1690 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1691 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 1692 } else { 1693 if (va.va_type != VFIFO && 1694 (error = kauth_authorize_system(cred, 1695 KAUTH_SYSTEM_MKNOD, 0, NULL, NULL, NULL))) { 1696 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1697 vput(nd.ni_dvp); 1698 goto out; 1699 } 1700 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1701 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 1702 if (error) 1703 goto out; 1704 if (nd.ni_cnd.cn_flags & ISSYMLINK) { 1705 vput(nd.ni_vp); 1706 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1707 error = EINVAL; 1708 } 1709 } 1710 out: 1711 vp = nd.ni_vp; 1712 if (!error) { 1713 error = nfsrv_composefh(vp, &nsfh, true); 1714 if (!error) 1715 error = VOP_GETATTR(vp, &va, cred); 1716 vput(vp); 1717 } 1718 if (dirp) { 1719 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 1720 vrele(dirp); 1721 dirp = NULL; 1722 } 1723 abort = 0; 1724 nfsm_reply(NFSX_SRVFH(&nsfh, true) + NFSX_POSTOPATTR(1) + 1725 NFSX_WCCDATA(1)); 1726 if (!error) { 1727 nfsm_srvpostop_fh(&nsfh); 1728 nfsm_srvpostop_attr(0, &va); 1729 } 1730 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1731 return (0); 1732 nfsmout: 1733 if (abort) { 1734 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1735 if (nd.ni_dvp == nd.ni_vp) 1736 vrele(nd.ni_dvp); 1737 else 1738 vput(nd.ni_dvp); 1739 if (nd.ni_vp) 1740 vput(nd.ni_vp); 1741 } 1742 if (dirp) 1743 vrele(dirp); 1744 return (error); 1745 } 1746 1747 /* 1748 * nfs remove service 1749 */ 1750 int 1751 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 1752 { 1753 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1754 struct mbuf *nam = nfsd->nd_nam; 1755 char *dpos = nfsd->nd_dpos; 1756 kauth_cred_t cred = nfsd->nd_cr; 1757 struct nameidata nd; 1758 u_int32_t *tl; 1759 int32_t t1; 1760 char *bpos; 1761 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1; 1762 int v3 = (nfsd->nd_flag & ND_NFSV3); 1763 char *cp2; 1764 struct mbuf *mb, *mreq; 1765 struct vnode *vp, *dirp; 1766 struct vattr dirfor, diraft; 1767 nfsrvfh_t nsfh; 1768 u_quad_t frev; 1769 1770 #ifndef nolint 1771 vp = (struct vnode *)0; 1772 #endif 1773 nfsm_srvmtofh(&nsfh); 1774 nfsm_srvnamesiz(len); 1775 nd.ni_cnd.cn_cred = cred; 1776 nd.ni_cnd.cn_nameiop = DELETE; 1777 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 1778 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 1779 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 1780 if (dirp && v3) { 1781 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 1782 } 1783 if (!error) { 1784 vp = nd.ni_vp; 1785 if (vp->v_type == VDIR) { 1786 error = EPERM; 1787 goto out; 1788 } 1789 /* 1790 * The root of a mounted filesystem cannot be deleted. 1791 */ 1792 if (vp->v_vflag & VV_ROOT) { 1793 error = EBUSY; 1794 } 1795 out: 1796 if (!error) { 1797 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1798 nqsrv_getl(vp, ND_WRITE); 1799 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 1800 } else { 1801 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1802 if (nd.ni_dvp == vp) 1803 vrele(nd.ni_dvp); 1804 else 1805 vput(nd.ni_dvp); 1806 vput(vp); 1807 } 1808 } 1809 if (dirp) { 1810 if (v3) { 1811 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 1812 } 1813 vrele(dirp); 1814 } 1815 nfsm_reply(NFSX_WCCDATA(v3)); 1816 if (v3) { 1817 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1818 return (0); 1819 } 1820 nfsm_srvdone; 1821 } 1822 1823 /* 1824 * nfs rename service 1825 */ 1826 int 1827 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 1828 { 1829 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1830 struct mbuf *nam = nfsd->nd_nam; 1831 char *dpos = nfsd->nd_dpos; 1832 kauth_cred_t cred = nfsd->nd_cr; 1833 u_int32_t *tl; 1834 int32_t t1; 1835 char *bpos; 1836 int error = 0, cache = 0, fdirfor_ret = 1, fdiraft_ret = 1; 1837 uint32_t len, len2; 1838 int tdirfor_ret = 1, tdiraft_ret = 1; 1839 int v3 = (nfsd->nd_flag & ND_NFSV3); 1840 char *cp2; 1841 struct mbuf *mb, *mreq; 1842 struct nameidata fromnd, tond; 1843 struct vnode *fvp, *tvp, *tdvp; 1844 struct vnode *fdirp = NULL, *tdirp = NULL; 1845 struct mount *localfs = NULL; 1846 struct vattr fdirfor, fdiraft, tdirfor, tdiraft; 1847 nfsrvfh_t fnsfh, tnsfh; 1848 u_quad_t frev; 1849 uid_t saved_uid; 1850 uint32_t saveflag; 1851 1852 #ifndef nolint 1853 fvp = (struct vnode *)0; 1854 #endif 1855 fromnd.ni_cnd.cn_nameiop = 0; 1856 tond.ni_cnd.cn_nameiop = 0; 1857 nfsm_srvmtofh(&fnsfh); 1858 nfsm_srvnamesiz(len); 1859 /* 1860 * Remember our original uid so that we can reset cr_uid before 1861 * the second nfs_namei() call, in case it is remapped. 1862 */ 1863 saved_uid = kauth_cred_geteuid(cred); 1864 fromnd.ni_cnd.cn_cred = cred; 1865 fromnd.ni_cnd.cn_nameiop = DELETE; 1866 fromnd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART; 1867 error = nfs_namei(&fromnd, &fnsfh, len, slp, nam, &md, 1868 &dpos, &fdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 1869 if (fdirp && v3) { 1870 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred); 1871 } 1872 if (error) { 1873 nfsm_reply(2 * NFSX_WCCDATA(v3)); 1874 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft); 1875 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft); 1876 if (fdirp) 1877 vrele(fdirp); 1878 return (0); 1879 } 1880 if (fromnd.ni_dvp != fromnd.ni_vp) { 1881 VOP_UNLOCK(fromnd.ni_dvp, 0); 1882 } 1883 fvp = fromnd.ni_vp; 1884 1885 localfs = fvp->v_mount; 1886 error = VFS_RENAMELOCK_ENTER(localfs); 1887 if (error) { 1888 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1889 vrele(fromnd.ni_dvp); 1890 vrele(fvp); 1891 goto out1; 1892 } 1893 1894 /* Copied, regrettably, from vfs_syscalls.c (q.v.) */ 1895 vrele(fvp); 1896 if ((fromnd.ni_cnd.cn_namelen == 1 && 1897 fromnd.ni_cnd.cn_nameptr[0] == '.') || 1898 (fromnd.ni_cnd.cn_namelen == 2 && 1899 fromnd.ni_cnd.cn_nameptr[0] == '.' && 1900 fromnd.ni_cnd.cn_nameptr[1] == '.')) { 1901 error = EINVAL; 1902 VFS_RENAMELOCK_EXIT(localfs); 1903 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1904 vrele(fromnd.ni_dvp); 1905 goto out1; 1906 } 1907 saveflag = fromnd.ni_cnd.cn_flags & SAVESTART; 1908 fromnd.ni_cnd.cn_flags &= ~SAVESTART; 1909 vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY); 1910 error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd); 1911 fromnd.ni_cnd.cn_flags |= saveflag; 1912 if (error) { 1913 VOP_UNLOCK(fromnd.ni_dvp, 0); 1914 VFS_RENAMELOCK_EXIT(localfs); 1915 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1916 vrele(fromnd.ni_dvp); 1917 goto out1; 1918 } 1919 VOP_UNLOCK(fromnd.ni_vp, 0); 1920 if (fromnd.ni_dvp != fromnd.ni_vp) 1921 VOP_UNLOCK(fromnd.ni_dvp, 0); 1922 fvp = fromnd.ni_vp; 1923 1924 nfsm_srvmtofh(&tnsfh); 1925 if (v3) { 1926 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED); 1927 len2 = fxdr_unsigned(uint32_t, *tl); 1928 /* len2 will be checked by nfs_namei */ 1929 } 1930 else { 1931 /* NFSv2 */ 1932 nfsm_strsiz(len2, NFS_MAXNAMLEN); 1933 } 1934 kauth_cred_seteuid(cred, saved_uid); 1935 tond.ni_cnd.cn_cred = cred; 1936 tond.ni_cnd.cn_nameiop = RENAME; 1937 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART; 1938 error = nfs_namei(&tond, &tnsfh, len2, slp, nam, &md, 1939 &dpos, &tdirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 1940 if (tdirp && v3) { 1941 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred); 1942 } 1943 if (error) { 1944 VFS_RENAMELOCK_EXIT(localfs); 1945 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1946 vrele(fromnd.ni_dvp); 1947 vrele(fvp); 1948 goto out1; 1949 } 1950 tdvp = tond.ni_dvp; 1951 tvp = tond.ni_vp; 1952 if (tvp != NULL) { 1953 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1954 if (v3) 1955 error = EEXIST; 1956 else 1957 error = EISDIR; 1958 goto out; 1959 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1960 if (v3) 1961 error = EEXIST; 1962 else 1963 error = ENOTDIR; 1964 goto out; 1965 } 1966 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1967 if (v3) 1968 error = EXDEV; 1969 else 1970 error = ENOTEMPTY; 1971 goto out; 1972 } 1973 } 1974 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1975 if (v3) 1976 error = EXDEV; 1977 else 1978 error = ENOTEMPTY; 1979 goto out; 1980 } 1981 if (fvp->v_mount != tdvp->v_mount) { 1982 if (v3) 1983 error = EXDEV; 1984 else 1985 error = ENOTEMPTY; 1986 goto out; 1987 } 1988 if (fvp == tdvp) { 1989 if (v3) 1990 error = EINVAL; 1991 else 1992 error = ENOTEMPTY; 1993 } 1994 /* 1995 * If source is the same as the destination (that is the 1996 * same vnode with the same name in the same directory), 1997 * then there is nothing to do. 1998 */ 1999 if (fvp == tvp && fromnd.ni_dvp == tdvp && 2000 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen && 2001 !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr, 2002 fromnd.ni_cnd.cn_namelen)) 2003 error = -1; 2004 out: 2005 if (!error) { 2006 nqsrv_getl(fromnd.ni_dvp, ND_WRITE); 2007 nqsrv_getl(tdvp, ND_WRITE); 2008 if (tvp) { 2009 nqsrv_getl(tvp, ND_WRITE); 2010 } 2011 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd, 2012 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd); 2013 VFS_RENAMELOCK_EXIT(localfs); 2014 } else { 2015 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd); 2016 if (tdvp == tvp) 2017 vrele(tdvp); 2018 else 2019 vput(tdvp); 2020 if (tvp) 2021 vput(tvp); 2022 VFS_RENAMELOCK_EXIT(localfs); 2023 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 2024 vrele(fromnd.ni_dvp); 2025 vrele(fvp); 2026 if (error == -1) 2027 error = 0; 2028 } 2029 vrele(tond.ni_startdir); 2030 PNBUF_PUT(tond.ni_cnd.cn_pnbuf); 2031 tond.ni_cnd.cn_nameiop = 0; 2032 out1: 2033 if (fdirp) { 2034 if (v3) { 2035 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred); 2036 } 2037 vrele(fdirp); 2038 fdirp = NULL; 2039 } 2040 if (tdirp) { 2041 if (v3) { 2042 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred); 2043 } 2044 vrele(tdirp); 2045 tdirp = NULL; 2046 } 2047 vrele(fromnd.ni_startdir); 2048 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 2049 fromnd.ni_cnd.cn_nameiop = 0; 2050 localfs = NULL; 2051 nfsm_reply(2 * NFSX_WCCDATA(v3)); 2052 if (v3) { 2053 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft); 2054 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft); 2055 } 2056 return (0); 2057 2058 nfsmout: 2059 if (fdirp) 2060 vrele(fdirp); 2061 #ifdef notdef 2062 if (tdirp) 2063 vrele(tdirp); 2064 #endif 2065 if (tond.ni_cnd.cn_nameiop) { 2066 vrele(tond.ni_startdir); 2067 PNBUF_PUT(tond.ni_cnd.cn_pnbuf); 2068 } 2069 if (localfs) { 2070 VFS_RENAMELOCK_EXIT(localfs); 2071 } 2072 if (fromnd.ni_cnd.cn_nameiop) { 2073 vrele(fromnd.ni_startdir); 2074 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 2075 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 2076 vrele(fromnd.ni_dvp); 2077 vrele(fvp); 2078 } 2079 return (error); 2080 } 2081 2082 /* 2083 * nfs link service 2084 */ 2085 int 2086 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 2087 { 2088 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2089 struct mbuf *nam = nfsd->nd_nam; 2090 char *dpos = nfsd->nd_dpos; 2091 kauth_cred_t cred = nfsd->nd_cr; 2092 struct nameidata nd; 2093 u_int32_t *tl; 2094 int32_t t1; 2095 char *bpos; 2096 int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1; 2097 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3); 2098 char *cp2; 2099 struct mbuf *mb, *mreq; 2100 struct vnode *vp, *xp, *dirp = (struct vnode *)0; 2101 struct vattr dirfor, diraft, at; 2102 nfsrvfh_t nsfh, dnsfh; 2103 u_quad_t frev; 2104 2105 nfsm_srvmtofh(&nsfh); 2106 nfsm_srvmtofh(&dnsfh); 2107 nfsm_srvnamesiz(len); 2108 error = nfsrv_fhtovp(&nsfh, false, &vp, cred, slp, nam, 2109 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 2110 if (error) { 2111 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3)); 2112 nfsm_srvpostop_attr(getret, &at); 2113 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2114 return (0); 2115 } 2116 if (vp->v_type == VDIR) { 2117 error = EPERM; 2118 goto out1; 2119 } 2120 nd.ni_cnd.cn_cred = cred; 2121 nd.ni_cnd.cn_nameiop = CREATE; 2122 nd.ni_cnd.cn_flags = LOCKPARENT; 2123 error = nfs_namei(&nd, &dnsfh, len, slp, nam, &md, &dpos, 2124 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 2125 if (dirp && v3) { 2126 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 2127 } 2128 if (error) 2129 goto out1; 2130 xp = nd.ni_vp; 2131 if (xp != NULL) { 2132 error = EEXIST; 2133 goto out; 2134 } 2135 xp = nd.ni_dvp; 2136 if (vp->v_mount != xp->v_mount) 2137 error = EXDEV; 2138 out: 2139 if (!error) { 2140 nqsrv_getl(vp, ND_WRITE); 2141 nqsrv_getl(xp, ND_WRITE); 2142 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); 2143 } else { 2144 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2145 if (nd.ni_dvp == nd.ni_vp) 2146 vrele(nd.ni_dvp); 2147 else 2148 vput(nd.ni_dvp); 2149 if (nd.ni_vp) 2150 vrele(nd.ni_vp); 2151 } 2152 out1: 2153 if (v3) 2154 getret = VOP_GETATTR(vp, &at, cred); 2155 if (dirp) { 2156 if (v3) { 2157 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 2158 } 2159 vrele(dirp); 2160 } 2161 vrele(vp); 2162 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3)); 2163 if (v3) { 2164 nfsm_srvpostop_attr(getret, &at); 2165 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2166 return (0); 2167 } 2168 nfsm_srvdone; 2169 } 2170 2171 /* 2172 * nfs symbolic link service 2173 */ 2174 int 2175 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 2176 { 2177 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2178 struct mbuf *nam = nfsd->nd_nam; 2179 char *dpos = nfsd->nd_dpos; 2180 kauth_cred_t cred = nfsd->nd_cr; 2181 struct vattr va, dirfor, diraft; 2182 struct nameidata nd; 2183 u_int32_t *tl; 2184 int32_t t1; 2185 struct nfsv2_sattr *sp; 2186 char *bpos, *pathcp = NULL, *cp2; 2187 struct uio io; 2188 struct iovec iv; 2189 int error = 0, cache = 0, dirfor_ret = 1, diraft_ret = 1, abort = 0; 2190 uint32_t len, len2; 2191 int v3 = (nfsd->nd_flag & ND_NFSV3); 2192 struct mbuf *mb, *mreq; 2193 struct vnode *dirp = (struct vnode *)0; 2194 nfsrvfh_t nsfh; 2195 u_quad_t frev; 2196 2197 nd.ni_cnd.cn_nameiop = 0; 2198 nfsm_srvmtofh(&nsfh); 2199 nfsm_srvnamesiz(len); 2200 nd.ni_cnd.cn_cred = cred; 2201 nd.ni_cnd.cn_nameiop = CREATE; 2202 nd.ni_cnd.cn_flags = LOCKPARENT; 2203 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 2204 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 2205 if (dirp && v3) { 2206 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 2207 } 2208 if (error) 2209 goto out; 2210 abort = 1; 2211 VATTR_NULL(&va); 2212 va.va_type = VLNK; 2213 if (v3) { 2214 va.va_mode = 0; 2215 nfsm_srvsattr(&va); 2216 nfsm_dissect(tl, uint32_t *, NFSX_UNSIGNED); 2217 len2 = fxdr_unsigned(uint32_t, *tl); 2218 if (len2 > PATH_MAX) { 2219 /* XXX should check _PC_NO_TRUNC */ 2220 error = ENAMETOOLONG; 2221 goto abortop; 2222 } 2223 } 2224 else { 2225 /* NFSv2 */ 2226 nfsm_strsiz(len2, NFS_MAXPATHLEN); 2227 } 2228 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK); 2229 iv.iov_base = pathcp; 2230 iv.iov_len = len2; 2231 io.uio_resid = len2; 2232 io.uio_offset = 0; 2233 io.uio_iov = &iv; 2234 io.uio_iovcnt = 1; 2235 io.uio_rw = UIO_READ; 2236 UIO_SETUP_SYSSPACE(&io); 2237 nfsm_mtouio(&io, len2); 2238 if (!v3) { 2239 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2240 va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 2241 } 2242 *(pathcp + len2) = '\0'; 2243 if (nd.ni_vp) { 2244 error = EEXIST; 2245 abortop: 2246 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2247 if (nd.ni_dvp == nd.ni_vp) 2248 vrele(nd.ni_dvp); 2249 else 2250 vput(nd.ni_dvp); 2251 if (nd.ni_vp) 2252 vrele(nd.ni_vp); 2253 goto out; 2254 } 2255 nqsrv_getl(nd.ni_dvp, ND_WRITE); 2256 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp); 2257 if (!error) { 2258 if (v3) { 2259 error = nfsrv_composefh(nd.ni_vp, &nsfh, v3); 2260 if (!error) 2261 error = VOP_GETATTR(nd.ni_vp, &va, cred); 2262 vput(nd.ni_vp); 2263 } else { 2264 vput(nd.ni_vp); 2265 } 2266 } 2267 out: 2268 if (pathcp) 2269 free(pathcp, M_TEMP); 2270 if (dirp) { 2271 if (v3) { 2272 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 2273 } 2274 vrele(dirp); 2275 dirp = NULL; 2276 } 2277 abort = 0; 2278 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) + 2279 NFSX_WCCDATA(v3)); 2280 if (v3) { 2281 if (!error) { 2282 nfsm_srvpostop_fh(&nsfh); 2283 nfsm_srvpostop_attr(0, &va); 2284 } 2285 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2286 } 2287 return (0); 2288 nfsmout: 2289 if (abort) { 2290 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2291 if (nd.ni_dvp == nd.ni_vp) 2292 vrele(nd.ni_dvp); 2293 else 2294 vput(nd.ni_dvp); 2295 if (nd.ni_vp) 2296 vrele(nd.ni_vp); 2297 } 2298 if (dirp) 2299 vrele(dirp); 2300 if (pathcp) 2301 free(pathcp, M_TEMP); 2302 return (error); 2303 } 2304 2305 /* 2306 * nfs mkdir service 2307 */ 2308 int 2309 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 2310 { 2311 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2312 struct mbuf *nam = nfsd->nd_nam; 2313 char *dpos = nfsd->nd_dpos; 2314 kauth_cred_t cred = nfsd->nd_cr; 2315 struct vattr va, dirfor, diraft; 2316 struct nfs_fattr *fp; 2317 struct nameidata nd; 2318 char *cp; 2319 u_int32_t *tl; 2320 int32_t t1; 2321 char *bpos; 2322 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1; 2323 int abort = 0; 2324 int v3 = (nfsd->nd_flag & ND_NFSV3); 2325 char *cp2; 2326 struct mbuf *mb, *mreq; 2327 struct vnode *vp, *dirp = (struct vnode *)0; 2328 nfsrvfh_t nsfh; 2329 u_quad_t frev; 2330 2331 nfsm_srvmtofh(&nsfh); 2332 nfsm_srvnamesiz(len); 2333 nd.ni_cnd.cn_cred = cred; 2334 nd.ni_cnd.cn_nameiop = CREATE; 2335 nd.ni_cnd.cn_flags = LOCKPARENT; 2336 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 2337 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 2338 if (dirp && v3) { 2339 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 2340 } 2341 if (error) { 2342 nfsm_reply(NFSX_WCCDATA(v3)); 2343 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2344 if (dirp) 2345 vrele(dirp); 2346 return (0); 2347 } 2348 abort = 1; 2349 VATTR_NULL(&va); 2350 if (v3) { 2351 va.va_mode = 0; 2352 nfsm_srvsattr(&va); 2353 } else { 2354 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2355 va.va_mode = nfstov_mode(*tl++); 2356 } 2357 va.va_type = VDIR; 2358 vp = nd.ni_vp; 2359 if (vp != NULL) { 2360 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2361 if (nd.ni_dvp == vp) 2362 vrele(nd.ni_dvp); 2363 else 2364 vput(nd.ni_dvp); 2365 vrele(vp); 2366 error = EEXIST; 2367 goto out; 2368 } 2369 nqsrv_getl(nd.ni_dvp, ND_WRITE); 2370 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 2371 if (!error) { 2372 vp = nd.ni_vp; 2373 error = nfsrv_composefh(vp, &nsfh, v3); 2374 if (!error) 2375 error = VOP_GETATTR(vp, &va, cred); 2376 vput(vp); 2377 } 2378 out: 2379 if (dirp) { 2380 if (v3) { 2381 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 2382 } 2383 vrele(dirp); 2384 dirp = NULL; 2385 } 2386 abort = 0; 2387 nfsm_reply(NFSX_SRVFH(&nsfh, v3) + NFSX_POSTOPATTR(v3) + 2388 NFSX_WCCDATA(v3)); 2389 if (v3) { 2390 if (!error) { 2391 nfsm_srvpostop_fh(&nsfh); 2392 nfsm_srvpostop_attr(0, &va); 2393 } 2394 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2395 } else { 2396 nfsm_srvfhtom(&nsfh, v3); 2397 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 2398 nfsm_srvfillattr(&va, fp); 2399 } 2400 return (0); 2401 nfsmout: 2402 if (abort) { 2403 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2404 if (nd.ni_dvp == nd.ni_vp) 2405 vrele(nd.ni_dvp); 2406 else 2407 vput(nd.ni_dvp); 2408 if (nd.ni_vp) 2409 vrele(nd.ni_vp); 2410 } 2411 if (dirp) 2412 vrele(dirp); 2413 return (error); 2414 } 2415 2416 /* 2417 * nfs rmdir service 2418 */ 2419 int 2420 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 2421 { 2422 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2423 struct mbuf *nam = nfsd->nd_nam; 2424 char *dpos = nfsd->nd_dpos; 2425 kauth_cred_t cred = nfsd->nd_cr; 2426 u_int32_t *tl; 2427 int32_t t1; 2428 char *bpos; 2429 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1; 2430 int v3 = (nfsd->nd_flag & ND_NFSV3); 2431 char *cp2; 2432 struct mbuf *mb, *mreq; 2433 struct vnode *vp, *dirp = (struct vnode *)0; 2434 struct vattr dirfor, diraft; 2435 nfsrvfh_t nsfh; 2436 struct nameidata nd; 2437 u_quad_t frev; 2438 2439 nfsm_srvmtofh(&nsfh); 2440 nfsm_srvnamesiz(len); 2441 nd.ni_cnd.cn_cred = cred; 2442 nd.ni_cnd.cn_nameiop = DELETE; 2443 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 2444 error = nfs_namei(&nd, &nsfh, len, slp, nam, &md, &dpos, 2445 &dirp, lwp, (nfsd->nd_flag & ND_KERBAUTH), false); 2446 if (dirp && v3) { 2447 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred); 2448 } 2449 if (error) { 2450 nfsm_reply(NFSX_WCCDATA(v3)); 2451 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2452 if (dirp) 2453 vrele(dirp); 2454 return (0); 2455 } 2456 vp = nd.ni_vp; 2457 if (vp->v_type != VDIR) { 2458 error = ENOTDIR; 2459 goto out; 2460 } 2461 /* 2462 * No rmdir "." please. 2463 */ 2464 if (nd.ni_dvp == vp) { 2465 error = EINVAL; 2466 goto out; 2467 } 2468 /* 2469 * The root of a mounted filesystem cannot be deleted. 2470 */ 2471 if (vp->v_vflag & VV_ROOT) 2472 error = EBUSY; 2473 out: 2474 if (!error) { 2475 nqsrv_getl(nd.ni_dvp, ND_WRITE); 2476 nqsrv_getl(vp, ND_WRITE); 2477 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 2478 } else { 2479 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2480 if (nd.ni_dvp == nd.ni_vp) 2481 vrele(nd.ni_dvp); 2482 else 2483 vput(nd.ni_dvp); 2484 vput(vp); 2485 } 2486 if (dirp) { 2487 if (v3) { 2488 diraft_ret = VOP_GETATTR(dirp, &diraft, cred); 2489 } 2490 vrele(dirp); 2491 } 2492 nfsm_reply(NFSX_WCCDATA(v3)); 2493 if (v3) { 2494 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2495 return (0); 2496 } 2497 nfsm_srvdone; 2498 } 2499 2500 /* 2501 * nfs readdir service 2502 * - mallocs what it thinks is enough to read 2503 * count rounded up to a multiple of NFS_SRVDIRBLKSIZ <= NFS_MAXREADDIR 2504 * - calls VOP_READDIR() 2505 * - loops around building the reply 2506 * if the output generated exceeds count break out of loop 2507 * The nfsm_clget macro is used here so that the reply will be packed 2508 * tightly in mbuf clusters. 2509 * - it only knows that it has encountered eof when the VOP_READDIR() 2510 * reads nothing 2511 * - as such one readdir rpc will return eof false although you are there 2512 * and then the next will return eof 2513 * - it trims out records with d_fileno == 0 2514 * this doesn't matter for Unix clients, but they might confuse clients 2515 * for other os'. 2516 * - it trims out records with d_type == DT_WHT 2517 * these cannot be seen through NFS (unless we extend the protocol) 2518 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less 2519 * than requested, but this may not apply to all filesystems. For 2520 * example, client NFS does not { although it is never remote mounted 2521 * anyhow } 2522 * The alternate call nfsrv_readdirplus() does lookups as well. 2523 * PS: The NFS protocol spec. does not clarify what the "count" byte 2524 * argument is a count of.. just name strings and file id's or the 2525 * entire reply rpc or ... 2526 * I tried just file name and id sizes and it confused the Sun client, 2527 * so I am using the full rpc size now. The "paranoia.." comment refers 2528 * to including the status longwords that are not a part of the dir. 2529 * "entry" structures, but are in the rpc. 2530 */ 2531 2532 #define NFS_SRVDIRBLKSIZ 1024 2533 2534 struct flrep { 2535 nfsuint64 fl_off; 2536 u_int32_t fl_postopok; 2537 struct nfs_fattr fl_fattr; /* XXX: must be of fattr3 size */ 2538 u_int32_t fl_fhok; 2539 u_int32_t fl_fhsize; 2540 /* handle comes here, filled in dynamically */ 2541 }; 2542 2543 int 2544 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 2545 { 2546 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2547 struct mbuf *nam = nfsd->nd_nam; 2548 char *dpos = nfsd->nd_dpos; 2549 kauth_cred_t cred = nfsd->nd_cr; 2550 char *bp, *be; 2551 struct mbuf *mp; 2552 struct dirent *dp; 2553 char *cp; 2554 u_int32_t *tl; 2555 int32_t t1; 2556 char *bpos; 2557 struct mbuf *mb, *mreq, *mp2; 2558 char *cpos, *cend, *cp2, *rbuf; 2559 struct vnode *vp; 2560 struct vattr at; 2561 nfsrvfh_t nsfh; 2562 struct uio io; 2563 struct iovec iv; 2564 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1; 2565 int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, ncookies; 2566 int v3 = (nfsd->nd_flag & ND_NFSV3); 2567 u_quad_t frev, off, toff, verf; 2568 off_t *cookies = NULL, *cookiep; 2569 nfsuint64 jar; 2570 2571 nfsm_srvmtofh(&nsfh); 2572 if (v3) { 2573 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2574 toff = fxdr_hyper(tl); 2575 tl += 2; 2576 verf = fxdr_hyper(tl); 2577 tl += 2; 2578 } else { 2579 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2580 toff = fxdr_unsigned(u_quad_t, *tl++); 2581 } 2582 off = toff; 2583 cnt = fxdr_unsigned(int, *tl); 2584 siz = ((cnt + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1)); 2585 xfer = NFS_SRVMAXDATA(nfsd); 2586 if (siz > xfer) 2587 siz = xfer; 2588 fullsiz = siz; 2589 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 2590 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 2591 if (!error && vp->v_type != VDIR) { 2592 error = ENOTDIR; 2593 vput(vp); 2594 } 2595 if (error) { 2596 nfsm_reply(NFSX_UNSIGNED); 2597 nfsm_srvpostop_attr(getret, &at); 2598 return (0); 2599 } 2600 nqsrv_getl(vp, ND_READ); 2601 if (v3) { 2602 error = getret = VOP_GETATTR(vp, &at, cred); 2603 #ifdef NFS3_STRICTVERF 2604 /* 2605 * XXX This check is too strict for Solaris 2.5 clients. 2606 */ 2607 if (!error && toff && verf != at.va_filerev) 2608 error = NFSERR_BAD_COOKIE; 2609 #endif 2610 } 2611 if (!error) 2612 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0); 2613 if (error) { 2614 vput(vp); 2615 nfsm_reply(NFSX_POSTOPATTR(v3)); 2616 nfsm_srvpostop_attr(getret, &at); 2617 return (0); 2618 } 2619 VOP_UNLOCK(vp, 0); 2620 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2621 again: 2622 iv.iov_base = rbuf; 2623 iv.iov_len = fullsiz; 2624 io.uio_iov = &iv; 2625 io.uio_iovcnt = 1; 2626 io.uio_offset = (off_t)off; 2627 io.uio_resid = fullsiz; 2628 io.uio_rw = UIO_READ; 2629 UIO_SETUP_SYSSPACE(&io); 2630 eofflag = 0; 2631 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2632 2633 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies); 2634 2635 off = (off_t)io.uio_offset; 2636 if (!cookies && !error) 2637 error = NFSERR_PERM; 2638 if (v3) { 2639 getret = VOP_GETATTR(vp, &at, cred); 2640 if (!error) 2641 error = getret; 2642 } 2643 2644 VOP_UNLOCK(vp, 0); 2645 if (error) { 2646 vrele(vp); 2647 free((void *)rbuf, M_TEMP); 2648 if (cookies) 2649 free((void *)cookies, M_TEMP); 2650 nfsm_reply(NFSX_POSTOPATTR(v3)); 2651 nfsm_srvpostop_attr(getret, &at); 2652 return (0); 2653 } 2654 if (io.uio_resid) { 2655 siz -= io.uio_resid; 2656 2657 /* 2658 * If nothing read, return eof 2659 * rpc reply 2660 */ 2661 if (siz == 0) { 2662 vrele(vp); 2663 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + 2664 2 * NFSX_UNSIGNED); 2665 if (v3) { 2666 nfsm_srvpostop_attr(getret, &at); 2667 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2668 txdr_hyper(at.va_filerev, tl); 2669 tl += 2; 2670 } else 2671 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2672 *tl++ = nfs_false; 2673 *tl = nfs_true; 2674 free((void *)rbuf, M_TEMP); 2675 free((void *)cookies, M_TEMP); 2676 return (0); 2677 } 2678 } 2679 2680 /* 2681 * Check for degenerate cases of nothing useful read. 2682 * If so go try again 2683 */ 2684 cpos = rbuf; 2685 cend = rbuf + siz; 2686 dp = (struct dirent *)cpos; 2687 cookiep = cookies; 2688 2689 while (cpos < cend && ncookies > 0 && 2690 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) { 2691 cpos += dp->d_reclen; 2692 dp = (struct dirent *)cpos; 2693 cookiep++; 2694 ncookies--; 2695 } 2696 if (cpos >= cend || ncookies == 0) { 2697 toff = off; 2698 siz = fullsiz; 2699 free(cookies, M_TEMP); 2700 cookies = NULL; 2701 goto again; 2702 } 2703 2704 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */ 2705 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz); 2706 if (v3) { 2707 nfsm_srvpostop_attr(getret, &at); 2708 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2709 txdr_hyper(at.va_filerev, tl); 2710 } 2711 mp = mp2 = mb; 2712 bp = bpos; 2713 be = bp + M_TRAILINGSPACE(mp); 2714 2715 /* Loop through the records and build reply */ 2716 while (cpos < cend && ncookies > 0) { 2717 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) { 2718 nlen = dp->d_namlen; 2719 rem = nfsm_rndup(nlen)-nlen; 2720 len += (4 * NFSX_UNSIGNED + nlen + rem); 2721 if (v3) 2722 len += 2 * NFSX_UNSIGNED; 2723 if (len > cnt) { 2724 eofflag = 0; 2725 break; 2726 } 2727 /* 2728 * Build the directory record xdr from 2729 * the dirent entry. 2730 */ 2731 nfsm_clget; 2732 *tl = nfs_true; 2733 bp += NFSX_UNSIGNED; 2734 if (v3) { 2735 nfsm_clget; 2736 *tl = txdr_unsigned(dp->d_fileno >> 32); 2737 bp += NFSX_UNSIGNED; 2738 } 2739 nfsm_clget; 2740 *tl = txdr_unsigned(dp->d_fileno); 2741 bp += NFSX_UNSIGNED; 2742 nfsm_clget; 2743 *tl = txdr_unsigned(nlen); 2744 bp += NFSX_UNSIGNED; 2745 2746 /* And loop around copying the name */ 2747 xfer = nlen; 2748 cp = dp->d_name; 2749 while (xfer > 0) { 2750 nfsm_clget; 2751 if ((bp+xfer) > be) 2752 tsiz = be-bp; 2753 else 2754 tsiz = xfer; 2755 memcpy(bp, cp, tsiz); 2756 bp += tsiz; 2757 xfer -= tsiz; 2758 if (xfer > 0) 2759 cp += tsiz; 2760 } 2761 /* And null pad to an int32_t boundary */ 2762 for (i = 0; i < rem; i++) 2763 *bp++ = '\0'; 2764 nfsm_clget; 2765 2766 /* Finish off the record */ 2767 txdr_hyper(*cookiep, &jar); 2768 if (v3) { 2769 *tl = jar.nfsuquad[0]; 2770 bp += NFSX_UNSIGNED; 2771 nfsm_clget; 2772 } 2773 *tl = jar.nfsuquad[1]; 2774 bp += NFSX_UNSIGNED; 2775 } 2776 cpos += dp->d_reclen; 2777 dp = (struct dirent *)cpos; 2778 cookiep++; 2779 ncookies--; 2780 } 2781 vrele(vp); 2782 nfsm_clget; 2783 *tl = nfs_false; 2784 bp += NFSX_UNSIGNED; 2785 nfsm_clget; 2786 if (eofflag) 2787 *tl = nfs_true; 2788 else 2789 *tl = nfs_false; 2790 bp += NFSX_UNSIGNED; 2791 if (mp != mb) { 2792 if (bp < be) 2793 mp->m_len = bp - mtod(mp, char *); 2794 } else 2795 mp->m_len += bp - bpos; 2796 free((void *)rbuf, M_TEMP); 2797 free((void *)cookies, M_TEMP); 2798 nfsm_srvdone; 2799 } 2800 2801 int 2802 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 2803 { 2804 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2805 struct mbuf *nam = nfsd->nd_nam; 2806 char *dpos = nfsd->nd_dpos; 2807 kauth_cred_t cred = nfsd->nd_cr; 2808 char *bp, *be; 2809 struct mbuf *mp; 2810 struct dirent *dp; 2811 char *cp; 2812 u_int32_t *tl; 2813 int32_t t1; 2814 char *bpos; 2815 struct mbuf *mb, *mreq, *mp2; 2816 char *cpos, *cend, *cp2, *rbuf; 2817 struct vnode *vp, *nvp; 2818 struct flrep fl; 2819 nfsrvfh_t nsfh; 2820 struct uio io; 2821 struct iovec iv; 2822 struct vattr va, at, *vap = &va; 2823 struct nfs_fattr *fp; 2824 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1; 2825 int siz, cnt, fullsiz, eofflag, rdonly, cache = 0, dirlen, ncookies; 2826 u_quad_t frev, off, toff, verf; 2827 off_t *cookies = NULL, *cookiep; 2828 2829 nfsm_srvmtofh(&nsfh); 2830 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 2831 toff = fxdr_hyper(tl); 2832 tl += 2; 2833 verf = fxdr_hyper(tl); 2834 tl += 2; 2835 siz = fxdr_unsigned(int, *tl++); 2836 cnt = fxdr_unsigned(int, *tl); 2837 off = toff; 2838 siz = ((siz + NFS_SRVDIRBLKSIZ - 1) & ~(NFS_SRVDIRBLKSIZ - 1)); 2839 xfer = NFS_SRVMAXDATA(nfsd); 2840 if (siz > xfer) 2841 siz = xfer; 2842 fullsiz = siz; 2843 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 2844 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 2845 if (!error && vp->v_type != VDIR) { 2846 error = ENOTDIR; 2847 vput(vp); 2848 } 2849 if (error) { 2850 nfsm_reply(NFSX_UNSIGNED); 2851 nfsm_srvpostop_attr(getret, &at); 2852 return (0); 2853 } 2854 error = getret = VOP_GETATTR(vp, &at, cred); 2855 #ifdef NFS3_STRICTVERF 2856 /* 2857 * XXX This check is too strict for Solaris 2.5 clients. 2858 */ 2859 if (!error && toff && verf != at.va_filerev) 2860 error = NFSERR_BAD_COOKIE; 2861 #endif 2862 if (!error) { 2863 nqsrv_getl(vp, ND_READ); 2864 error = nfsrv_access(vp, VEXEC, cred, rdonly, lwp, 0); 2865 } 2866 if (error) { 2867 vput(vp); 2868 nfsm_reply(NFSX_V3POSTOPATTR); 2869 nfsm_srvpostop_attr(getret, &at); 2870 return (0); 2871 } 2872 VOP_UNLOCK(vp, 0); 2873 2874 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2875 again: 2876 iv.iov_base = rbuf; 2877 iv.iov_len = fullsiz; 2878 io.uio_iov = &iv; 2879 io.uio_iovcnt = 1; 2880 io.uio_offset = (off_t)off; 2881 io.uio_resid = fullsiz; 2882 io.uio_rw = UIO_READ; 2883 UIO_SETUP_SYSSPACE(&io); 2884 eofflag = 0; 2885 2886 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2887 2888 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies); 2889 2890 off = (u_quad_t)io.uio_offset; 2891 getret = VOP_GETATTR(vp, &at, cred); 2892 2893 VOP_UNLOCK(vp, 0); 2894 2895 /* 2896 * If the VGET operation doesn't work for this filesystem, 2897 * we can't support readdirplus. Returning NOTSUPP should 2898 * make clients fall back to plain readdir. 2899 * There's no need to check for VPTOFH as well, we wouldn't 2900 * even be here otherwise. 2901 */ 2902 if (!getret) { 2903 if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp))) 2904 getret = (getret == EOPNOTSUPP) ? 2905 NFSERR_NOTSUPP : NFSERR_IO; 2906 else 2907 vput(nvp); 2908 } 2909 2910 if (!cookies && !error) 2911 error = NFSERR_PERM; 2912 if (!error) 2913 error = getret; 2914 if (error) { 2915 vrele(vp); 2916 if (cookies) 2917 free((void *)cookies, M_TEMP); 2918 free((void *)rbuf, M_TEMP); 2919 nfsm_reply(NFSX_V3POSTOPATTR); 2920 nfsm_srvpostop_attr(getret, &at); 2921 return (0); 2922 } 2923 if (io.uio_resid) { 2924 siz -= io.uio_resid; 2925 2926 /* 2927 * If nothing read, return eof 2928 * rpc reply 2929 */ 2930 if (siz == 0) { 2931 vrele(vp); 2932 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2933 2 * NFSX_UNSIGNED); 2934 nfsm_srvpostop_attr(getret, &at); 2935 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2936 txdr_hyper(at.va_filerev, tl); 2937 tl += 2; 2938 *tl++ = nfs_false; 2939 *tl = nfs_true; 2940 free((void *)cookies, M_TEMP); 2941 free((void *)rbuf, M_TEMP); 2942 return (0); 2943 } 2944 } 2945 2946 /* 2947 * Check for degenerate cases of nothing useful read. 2948 * If so go try again 2949 */ 2950 cpos = rbuf; 2951 cend = rbuf + siz; 2952 dp = (struct dirent *)cpos; 2953 cookiep = cookies; 2954 2955 while (cpos < cend && ncookies > 0 && 2956 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) { 2957 cpos += dp->d_reclen; 2958 dp = (struct dirent *)cpos; 2959 cookiep++; 2960 ncookies--; 2961 } 2962 if (cpos >= cend || ncookies == 0) { 2963 toff = off; 2964 siz = fullsiz; 2965 free(cookies, M_TEMP); 2966 cookies = NULL; 2967 goto again; 2968 } 2969 2970 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED; 2971 nfsm_reply(cnt); 2972 nfsm_srvpostop_attr(getret, &at); 2973 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2974 txdr_hyper(at.va_filerev, tl); 2975 mp = mp2 = mb; 2976 bp = bpos; 2977 be = bp + M_TRAILINGSPACE(mp); 2978 2979 /* Loop through the records and build reply */ 2980 while (cpos < cend && ncookies > 0) { 2981 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) { 2982 nfsrvfh_t nnsfh; 2983 2984 nlen = dp->d_namlen; 2985 rem = nfsm_rndup(nlen)-nlen; 2986 2987 /* 2988 * For readdir_and_lookup get the vnode using 2989 * the file number. 2990 */ 2991 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp)) 2992 goto invalid; 2993 if (nfsrv_composefh(nvp, &nnsfh, true)) { 2994 vput(nvp); 2995 goto invalid; 2996 } 2997 if (VOP_GETATTR(nvp, vap, cred)) { 2998 vput(nvp); 2999 goto invalid; 3000 } 3001 vput(nvp); 3002 3003 /* 3004 * If either the dircount or maxcount will be 3005 * exceeded, get out now. Both of these lengths 3006 * are calculated conservatively, including all 3007 * XDR overheads. 3008 */ 3009 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH + 3010 NFSX_V3POSTOPATTR); 3011 dirlen += (6 * NFSX_UNSIGNED + nlen + rem); 3012 if (len > cnt || dirlen > fullsiz) { 3013 eofflag = 0; 3014 break; 3015 } 3016 3017 /* 3018 * Build the directory record xdr from 3019 * the dirent entry. 3020 */ 3021 fp = (struct nfs_fattr *)&fl.fl_fattr; 3022 nfsm_srvfillattr(vap, fp); 3023 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH); 3024 fl.fl_fhok = nfs_true; 3025 fl.fl_postopok = nfs_true; 3026 txdr_hyper(*cookiep, fl.fl_off.nfsuquad); 3027 3028 nfsm_clget; 3029 *tl = nfs_true; 3030 bp += NFSX_UNSIGNED; 3031 nfsm_clget; 3032 *tl = txdr_unsigned(dp->d_fileno >> 32); 3033 bp += NFSX_UNSIGNED; 3034 nfsm_clget; 3035 *tl = txdr_unsigned(dp->d_fileno); 3036 bp += NFSX_UNSIGNED; 3037 nfsm_clget; 3038 *tl = txdr_unsigned(nlen); 3039 bp += NFSX_UNSIGNED; 3040 3041 /* And loop around copying the name */ 3042 xfer = nlen; 3043 cp = dp->d_name; 3044 while (xfer > 0) { 3045 nfsm_clget; 3046 if ((bp + xfer) > be) 3047 tsiz = be - bp; 3048 else 3049 tsiz = xfer; 3050 memcpy(bp, cp, tsiz); 3051 bp += tsiz; 3052 xfer -= tsiz; 3053 if (xfer > 0) 3054 cp += tsiz; 3055 } 3056 /* And null pad to an int32_t boundary */ 3057 for (i = 0; i < rem; i++) 3058 *bp++ = '\0'; 3059 3060 /* 3061 * Now copy the flrep structure out. 3062 */ 3063 xfer = sizeof(struct flrep); 3064 cp = (void *)&fl; 3065 while (xfer > 0) { 3066 nfsm_clget; 3067 if ((bp + xfer) > be) 3068 tsiz = be - bp; 3069 else 3070 tsiz = xfer; 3071 memcpy(bp, cp, tsiz); 3072 bp += tsiz; 3073 xfer -= tsiz; 3074 if (xfer > 0) 3075 cp += tsiz; 3076 } 3077 3078 /* 3079 * ... and filehandle. 3080 */ 3081 xfer = NFSRVFH_SIZE(&nnsfh); 3082 cp = NFSRVFH_DATA(&nnsfh); 3083 while (xfer > 0) { 3084 nfsm_clget; 3085 if ((bp + xfer) > be) 3086 tsiz = be - bp; 3087 else 3088 tsiz = xfer; 3089 memcpy(bp, cp, tsiz); 3090 bp += tsiz; 3091 xfer -= tsiz; 3092 if (xfer > 0) 3093 cp += tsiz; 3094 } 3095 } 3096 invalid: 3097 cpos += dp->d_reclen; 3098 dp = (struct dirent *)cpos; 3099 cookiep++; 3100 ncookies--; 3101 } 3102 vrele(vp); 3103 nfsm_clget; 3104 *tl = nfs_false; 3105 bp += NFSX_UNSIGNED; 3106 nfsm_clget; 3107 if (eofflag) 3108 *tl = nfs_true; 3109 else 3110 *tl = nfs_false; 3111 bp += NFSX_UNSIGNED; 3112 if (mp != mb) { 3113 if (bp < be) 3114 mp->m_len = bp - mtod(mp, char *); 3115 } else 3116 mp->m_len += bp - bpos; 3117 free((void *)cookies, M_TEMP); 3118 free((void *)rbuf, M_TEMP); 3119 nfsm_srvdone; 3120 } 3121 3122 /* 3123 * nfs commit service 3124 */ 3125 int 3126 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 3127 { 3128 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3129 struct mbuf *nam = nfsd->nd_nam; 3130 char *dpos = nfsd->nd_dpos; 3131 kauth_cred_t cred = nfsd->nd_cr; 3132 struct vattr bfor, aft; 3133 struct vnode *vp; 3134 nfsrvfh_t nsfh; 3135 u_int32_t *tl; 3136 int32_t t1; 3137 char *bpos; 3138 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cache = 0; 3139 uint32_t cnt; 3140 char *cp2; 3141 struct mbuf *mb, *mreq; 3142 u_quad_t frev, off, end; 3143 3144 nfsm_srvmtofh(&nsfh); 3145 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3146 3147 off = fxdr_hyper(tl); 3148 tl += 2; 3149 cnt = fxdr_unsigned(uint32_t, *tl); 3150 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 3151 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 3152 if (error) { 3153 nfsm_reply(2 * NFSX_UNSIGNED); 3154 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft); 3155 return (0); 3156 } 3157 for_ret = VOP_GETATTR(vp, &bfor, cred); 3158 end = (cnt > 0) ? off + cnt : vp->v_size; 3159 if (end < off || end > vp->v_size) 3160 end = vp->v_size; 3161 if (off < vp->v_size) 3162 error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, end); 3163 /* else error == 0, from nfsrv_fhtovp() */ 3164 aft_ret = VOP_GETATTR(vp, &aft, cred); 3165 vput(vp); 3166 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF); 3167 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft); 3168 if (!error) { 3169 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF); 3170 *tl++ = txdr_unsigned(boottime.tv_sec); 3171 *tl = txdr_unsigned(boottime.tv_nsec / 1000); 3172 } else { 3173 return (0); 3174 } 3175 nfsm_srvdone; 3176 } 3177 3178 /* 3179 * nfs statfs service 3180 */ 3181 int 3182 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 3183 { 3184 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3185 struct mbuf *nam = nfsd->nd_nam; 3186 char *dpos = nfsd->nd_dpos; 3187 kauth_cred_t cred = nfsd->nd_cr; 3188 struct statvfs *sf = NULL; 3189 struct nfs_statfs *sfp; 3190 u_int32_t *tl; 3191 int32_t t1; 3192 char *bpos; 3193 int error = 0, rdonly, cache = 0, getret = 1; 3194 int v3 = (nfsd->nd_flag & ND_NFSV3); 3195 char *cp2; 3196 struct mbuf *mb, *mreq; 3197 struct vnode *vp; 3198 struct vattr at; 3199 nfsrvfh_t nsfh; 3200 u_quad_t frev, tval; 3201 3202 nfsm_srvmtofh(&nsfh); 3203 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 3204 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 3205 if (error) { 3206 nfsm_reply(NFSX_UNSIGNED); 3207 nfsm_srvpostop_attr(getret, &at); 3208 return (0); 3209 } 3210 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK); 3211 error = VFS_STATVFS(vp->v_mount, sf); 3212 getret = VOP_GETATTR(vp, &at, cred); 3213 vput(vp); 3214 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3)); 3215 if (v3) 3216 nfsm_srvpostop_attr(getret, &at); 3217 if (error) { 3218 free(sf, M_TEMP); 3219 return (0); 3220 } 3221 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3)); 3222 if (v3) { 3223 tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_frsize); 3224 txdr_hyper(tval, &sfp->sf_tbytes); 3225 tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_frsize); 3226 txdr_hyper(tval, &sfp->sf_fbytes); 3227 tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_frsize); 3228 txdr_hyper(tval, &sfp->sf_abytes); 3229 tval = (u_quad_t)sf->f_files; 3230 txdr_hyper(tval, &sfp->sf_tfiles); 3231 tval = (u_quad_t)sf->f_ffree; 3232 txdr_hyper(tval, &sfp->sf_ffiles); 3233 txdr_hyper(tval, &sfp->sf_afiles); 3234 sfp->sf_invarsec = 0; 3235 } else { 3236 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA); 3237 sfp->sf_bsize = txdr_unsigned(sf->f_frsize); 3238 sfp->sf_blocks = txdr_unsigned(sf->f_blocks); 3239 sfp->sf_bfree = txdr_unsigned(sf->f_bfree); 3240 sfp->sf_bavail = txdr_unsigned(sf->f_bavail); 3241 } 3242 nfsmout: 3243 if (sf) 3244 free(sf, M_TEMP); 3245 return error; 3246 } 3247 3248 /* 3249 * nfs fsinfo service 3250 */ 3251 int 3252 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 3253 { 3254 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3255 struct mbuf *nam = nfsd->nd_nam; 3256 char *dpos = nfsd->nd_dpos; 3257 kauth_cred_t cred = nfsd->nd_cr; 3258 u_int32_t *tl; 3259 struct nfsv3_fsinfo *sip; 3260 int32_t t1; 3261 char *bpos; 3262 int error = 0, rdonly, cache = 0, getret = 1; 3263 uint32_t maxdata; 3264 char *cp2; 3265 struct mbuf *mb, *mreq; 3266 struct vnode *vp; 3267 struct vattr at; 3268 nfsrvfh_t nsfh; 3269 u_quad_t frev, maxfsize; 3270 struct statvfs *sb; 3271 3272 nfsm_srvmtofh(&nsfh); 3273 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 3274 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 3275 if (error) { 3276 nfsm_reply(NFSX_UNSIGNED); 3277 nfsm_srvpostop_attr(getret, &at); 3278 return (0); 3279 } 3280 3281 /* XXX Try to make a guess on the max file size. */ 3282 sb = malloc(sizeof(*sb), M_TEMP, M_WAITOK); 3283 VFS_STATVFS(vp->v_mount, sb); 3284 maxfsize = (u_quad_t)0x80000000 * sb->f_frsize - 1; 3285 free(sb, M_TEMP); 3286 3287 getret = VOP_GETATTR(vp, &at, cred); 3288 vput(vp); 3289 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO); 3290 nfsm_srvpostop_attr(getret, &at); 3291 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 3292 3293 /* 3294 * XXX 3295 * There should be file system VFS OP(s) to get this information. 3296 * For now, assume ufs. 3297 */ 3298 if (slp->ns_so->so_type == SOCK_DGRAM) 3299 maxdata = NFS_MAXDGRAMDATA; 3300 else 3301 maxdata = NFS_MAXDATA; 3302 sip->fs_rtmax = txdr_unsigned(maxdata); 3303 sip->fs_rtpref = txdr_unsigned(maxdata); 3304 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE); 3305 sip->fs_wtmax = txdr_unsigned(maxdata); 3306 sip->fs_wtpref = txdr_unsigned(maxdata); 3307 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE); 3308 sip->fs_dtpref = txdr_unsigned(maxdata); 3309 txdr_hyper(maxfsize, &sip->fs_maxfilesize); 3310 sip->fs_timedelta.nfsv3_sec = 0; 3311 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1); 3312 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK | 3313 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS | 3314 NFSV3FSINFO_CANSETTIME); 3315 nfsm_srvdone; 3316 } 3317 3318 /* 3319 * nfs pathconf service 3320 */ 3321 int 3322 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, struct lwp *lwp, struct mbuf **mrq) 3323 { 3324 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3325 struct mbuf *nam = nfsd->nd_nam; 3326 char *dpos = nfsd->nd_dpos; 3327 kauth_cred_t cred = nfsd->nd_cr; 3328 u_int32_t *tl; 3329 struct nfsv3_pathconf *pc; 3330 int32_t t1; 3331 char *bpos; 3332 int error = 0, rdonly, cache = 0, getret = 1; 3333 register_t linkmax, namemax, chownres, notrunc; 3334 char *cp2; 3335 struct mbuf *mb, *mreq; 3336 struct vnode *vp; 3337 struct vattr at; 3338 nfsrvfh_t nsfh; 3339 u_quad_t frev; 3340 3341 nfsm_srvmtofh(&nsfh); 3342 error = nfsrv_fhtovp(&nsfh, 1, &vp, cred, slp, nam, 3343 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), false); 3344 if (error) { 3345 nfsm_reply(NFSX_UNSIGNED); 3346 nfsm_srvpostop_attr(getret, &at); 3347 return (0); 3348 } 3349 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax); 3350 if (!error) 3351 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax); 3352 if (!error) 3353 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres); 3354 if (!error) 3355 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc); 3356 getret = VOP_GETATTR(vp, &at, cred); 3357 vput(vp); 3358 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF); 3359 nfsm_srvpostop_attr(getret, &at); 3360 if (error) 3361 return (0); 3362 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF); 3363 3364 pc->pc_linkmax = txdr_unsigned(linkmax); 3365 pc->pc_namemax = txdr_unsigned(namemax); 3366 pc->pc_notrunc = txdr_unsigned(notrunc); 3367 pc->pc_chownrestricted = txdr_unsigned(chownres); 3368 3369 /* 3370 * These should probably be supported by VOP_PATHCONF(), but 3371 * until msdosfs is exportable (why would you want to?), the 3372 * Unix defaults should be ok. 3373 */ 3374 pc->pc_caseinsensitive = nfs_false; 3375 pc->pc_casepreserving = nfs_true; 3376 nfsm_srvdone; 3377 } 3378 3379 /* 3380 * Null operation, used by clients to ping server 3381 */ 3382 /* ARGSUSED */ 3383 int 3384 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 3385 struct lwp *lwp, struct mbuf **mrq) 3386 { 3387 struct mbuf *mrep = nfsd->nd_mrep; 3388 char *bpos; 3389 int error = NFSERR_RETVOID, cache = 0; 3390 struct mbuf *mb, *mreq; 3391 u_quad_t frev; 3392 3393 nfsm_reply(0); 3394 nfsmout: 3395 return (0); 3396 } 3397 3398 /* 3399 * No operation, used for obsolete procedures 3400 */ 3401 /* ARGSUSED */ 3402 int 3403 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp, 3404 struct lwp *lwp, struct mbuf **mrq) 3405 { 3406 struct mbuf *mrep = nfsd->nd_mrep; 3407 char *bpos; 3408 int error, cache = 0; 3409 struct mbuf *mb, *mreq; 3410 u_quad_t frev; 3411 3412 if (nfsd->nd_repstat) 3413 error = nfsd->nd_repstat; 3414 else 3415 error = EPROCUNAVAIL; 3416 nfsm_reply(0); 3417 nfsmout: 3418 return (0); 3419 } 3420 3421 /* 3422 * Perform access checking for vnodes obtained from file handles that would 3423 * refer to files already opened by a Unix client. You cannot just use 3424 * vn_writechk() and VOP_ACCESS() for two reasons. 3425 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case 3426 * 2 - The owner is to be given access irrespective of mode bits for some 3427 * operations, so that processes that chmod after opening a file don't 3428 * break. I don't like this because it opens a security hole, but since 3429 * the nfs server opens a security hole the size of a barn door anyhow, 3430 * what the heck. 3431 * 3432 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS() 3433 * will return EPERM instead of EACCESS. EPERM is always an error. 3434 */ 3435 int 3436 nfsrv_access(struct vnode *vp, int flags, kauth_cred_t cred, int rdonly, struct lwp *lwp, int override) 3437 { 3438 struct vattr vattr; 3439 int error; 3440 if (flags & VWRITE) { 3441 /* Just vn_writechk() changed to check rdonly */ 3442 /* 3443 * Disallow write attempts on read-only file systems; 3444 * unless the file is a socket or a block or character 3445 * device resident on the file system. 3446 */ 3447 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) { 3448 switch (vp->v_type) { 3449 case VREG: 3450 case VDIR: 3451 case VLNK: 3452 return (EROFS); 3453 default: 3454 break; 3455 } 3456 } 3457 3458 /* 3459 * If the vnode is in use as a process's text, 3460 * we can't allow writing. 3461 */ 3462 if (vp->v_iflag & VI_TEXT) 3463 return (ETXTBSY); 3464 } 3465 error = VOP_GETATTR(vp, &vattr, cred); 3466 if (error) 3467 return (error); 3468 error = VOP_ACCESS(vp, flags, cred); 3469 /* 3470 * Allow certain operations for the owner (reads and writes 3471 * on files that are already open). 3472 */ 3473 if (override && error == EACCES && kauth_cred_geteuid(cred) == vattr.va_uid) 3474 error = 0; 3475 return error; 3476 } 3477