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