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