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