1 /* $NetBSD: nfs_serv.c,v 1.60 2001/07/24 15:39:33 assar 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 | SAVESTART; 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 vrele(nd.ni_startdir); 1386 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1387 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 1388 if (!error) { 1389 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1390 if (exclusive_flag) { 1391 exclusive_flag = 0; 1392 VATTR_NULL(&va); 1393 memcpy((caddr_t)&va.va_atime, cverf, 1394 NFSX_V3CREATEVERF); 1395 error = VOP_SETATTR(nd.ni_vp, &va, cred, 1396 procp); 1397 } 1398 } 1399 } else if (va.va_type == VCHR || va.va_type == VBLK || 1400 va.va_type == VFIFO) { 1401 if (va.va_type == VCHR && rdev == 0xffffffff) 1402 va.va_type = VFIFO; 1403 if (va.va_type != VFIFO && 1404 (error = suser(cred, (u_short *)0))) { 1405 vrele(nd.ni_startdir); 1406 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1407 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1408 vput(nd.ni_dvp); 1409 nfsm_reply(0); 1410 return (error); 1411 } else 1412 va.va_rdev = (dev_t)rdev; 1413 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1414 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, 1415 &va); 1416 if (error) { 1417 vrele(nd.ni_startdir); 1418 nfsm_reply(0); 1419 } 1420 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1421 if (nd.ni_cnd.cn_flags & ISSYMLINK) { 1422 vrele(nd.ni_dvp); 1423 vput(nd.ni_vp); 1424 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1425 error = EINVAL; 1426 nfsm_reply(0); 1427 } 1428 } else { 1429 vrele(nd.ni_startdir); 1430 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1431 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1432 vput(nd.ni_dvp); 1433 error = ENXIO; 1434 } 1435 vp = nd.ni_vp; 1436 } else { 1437 vrele(nd.ni_startdir); 1438 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1439 vp = nd.ni_vp; 1440 if (nd.ni_dvp == vp) 1441 vrele(nd.ni_dvp); 1442 else 1443 vput(nd.ni_dvp); 1444 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1445 if (!error && va.va_size != -1) { 1446 error = nfsrv_access(vp, VWRITE, cred, 1447 (nd.ni_cnd.cn_flags & RDONLY), procp, 0); 1448 if (!error) { 1449 nqsrv_getl(vp, ND_WRITE); 1450 tempsize = va.va_size; 1451 VATTR_NULL(&va); 1452 va.va_size = tempsize; 1453 error = VOP_SETATTR(vp, &va, cred, 1454 procp); 1455 } 1456 } 1457 if (error) 1458 vput(vp); 1459 } 1460 if (!error) { 1461 memset((caddr_t)fhp, 0, sizeof(nfh)); 1462 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 1463 error = VFS_VPTOFH(vp, &fhp->fh_fid); 1464 if (!error) 1465 error = VOP_GETATTR(vp, &va, cred, procp); 1466 vput(vp); 1467 } 1468 if (v3) { 1469 if (exclusive_flag && !error && 1470 memcmp(cverf, (caddr_t)&va.va_atime, NFSX_V3CREATEVERF)) 1471 error = EEXIST; 1472 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 1473 vrele(dirp); 1474 } 1475 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3)); 1476 if (v3) { 1477 if (!error) { 1478 nfsm_srvpostop_fh(fhp); 1479 nfsm_srvpostop_attr(0, &va); 1480 } 1481 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1482 } else { 1483 nfsm_srvfhtom(fhp, v3); 1484 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 1485 nfsm_srvfillattr(&va, fp); 1486 } 1487 return (0); 1488 nfsmout: 1489 if (dirp) 1490 vrele(dirp); 1491 if (nd.ni_cnd.cn_nameiop) { 1492 vrele(nd.ni_startdir); 1493 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1494 } 1495 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1496 if (nd.ni_dvp == nd.ni_vp) 1497 vrele(nd.ni_dvp); 1498 else 1499 vput(nd.ni_dvp); 1500 if (nd.ni_vp) 1501 vput(nd.ni_vp); 1502 return (error); 1503 } 1504 1505 /* 1506 * nfs v3 mknod service 1507 */ 1508 int 1509 nfsrv_mknod(nfsd, slp, procp, mrq) 1510 struct nfsrv_descript *nfsd; 1511 struct nfssvc_sock *slp; 1512 struct proc *procp; 1513 struct mbuf **mrq; 1514 { 1515 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1516 struct mbuf *nam = nfsd->nd_nam; 1517 caddr_t dpos = nfsd->nd_dpos; 1518 struct ucred *cred = &nfsd->nd_cr; 1519 struct vattr va, dirfor, diraft; 1520 u_int32_t *tl; 1521 struct nameidata nd; 1522 int32_t t1; 1523 caddr_t bpos; 1524 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1; 1525 u_int32_t major, minor; 1526 enum vtype vtyp; 1527 char *cp2; 1528 struct mbuf *mb, *mb2, *mreq; 1529 struct vnode *vp, *dirp = (struct vnode *)0; 1530 nfsfh_t nfh; 1531 fhandle_t *fhp; 1532 u_quad_t frev; 1533 1534 nd.ni_cnd.cn_nameiop = 0; 1535 fhp = &nfh.fh_generic; 1536 nfsm_srvmtofh(fhp); 1537 nfsm_srvnamesiz(len); 1538 nd.ni_cnd.cn_cred = cred; 1539 nd.ni_cnd.cn_nameiop = CREATE; 1540 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART; 1541 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos, 1542 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 1543 if (dirp) 1544 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp); 1545 if (error) { 1546 nfsm_reply(NFSX_WCCDATA(1)); 1547 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1548 if (dirp) 1549 vrele(dirp); 1550 return (0); 1551 } 1552 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 1553 vtyp = nfsv3tov_type(*tl); 1554 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 1555 vrele(nd.ni_startdir); 1556 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1557 error = NFSERR_BADTYPE; 1558 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1559 if (nd.ni_dvp == nd.ni_vp) 1560 vrele(nd.ni_dvp); 1561 else 1562 vput(nd.ni_dvp); 1563 if (nd.ni_vp) 1564 vput(nd.ni_vp); 1565 goto out; 1566 } 1567 VATTR_NULL(&va); 1568 nfsm_srvsattr(&va); 1569 if (vtyp == VCHR || vtyp == VBLK) { 1570 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1571 major = fxdr_unsigned(u_int32_t, *tl++); 1572 minor = fxdr_unsigned(u_int32_t, *tl); 1573 va.va_rdev = makedev(major, minor); 1574 } 1575 1576 /* 1577 * Iff doesn't exist, create it. 1578 */ 1579 if (nd.ni_vp) { 1580 vrele(nd.ni_startdir); 1581 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1582 error = EEXIST; 1583 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1584 if (nd.ni_dvp == nd.ni_vp) 1585 vrele(nd.ni_dvp); 1586 else 1587 vput(nd.ni_dvp); 1588 vput(nd.ni_vp); 1589 goto out; 1590 } 1591 va.va_type = vtyp; 1592 if (vtyp == VSOCK) { 1593 vrele(nd.ni_startdir); 1594 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1595 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 1596 if (!error) 1597 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1598 } else { 1599 if (va.va_type != VFIFO && 1600 (error = suser(cred, (u_short *)0))) { 1601 vrele(nd.ni_startdir); 1602 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1603 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1604 vput(nd.ni_dvp); 1605 goto out; 1606 } 1607 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1608 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 1609 if (error) { 1610 vrele(nd.ni_startdir); 1611 goto out; 1612 } 1613 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1614 if (error) 1615 goto out; 1616 if (nd.ni_cnd.cn_flags & ISSYMLINK) { 1617 vrele(nd.ni_dvp); 1618 vput(nd.ni_vp); 1619 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1620 error = EINVAL; 1621 } 1622 } 1623 out: 1624 vp = nd.ni_vp; 1625 if (!error) { 1626 memset((caddr_t)fhp, 0, sizeof(nfh)); 1627 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 1628 error = VFS_VPTOFH(vp, &fhp->fh_fid); 1629 if (!error) 1630 error = VOP_GETATTR(vp, &va, cred, procp); 1631 vput(vp); 1632 } 1633 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 1634 vrele(dirp); 1635 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1)); 1636 if (!error) { 1637 nfsm_srvpostop_fh(fhp); 1638 nfsm_srvpostop_attr(0, &va); 1639 } 1640 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1641 return (0); 1642 nfsmout: 1643 if (dirp) 1644 vrele(dirp); 1645 if (nd.ni_cnd.cn_nameiop) { 1646 vrele(nd.ni_startdir); 1647 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 1648 } 1649 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1650 if (nd.ni_dvp == nd.ni_vp) 1651 vrele(nd.ni_dvp); 1652 else 1653 vput(nd.ni_dvp); 1654 if (nd.ni_vp) 1655 vput(nd.ni_vp); 1656 return (error); 1657 } 1658 1659 /* 1660 * nfs remove service 1661 */ 1662 int 1663 nfsrv_remove(nfsd, slp, procp, mrq) 1664 struct nfsrv_descript *nfsd; 1665 struct nfssvc_sock *slp; 1666 struct proc *procp; 1667 struct mbuf **mrq; 1668 { 1669 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1670 struct mbuf *nam = nfsd->nd_nam; 1671 caddr_t dpos = nfsd->nd_dpos; 1672 struct ucred *cred = &nfsd->nd_cr; 1673 struct nameidata nd; 1674 u_int32_t *tl; 1675 int32_t t1; 1676 caddr_t bpos; 1677 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1; 1678 int v3 = (nfsd->nd_flag & ND_NFSV3); 1679 char *cp2; 1680 struct mbuf *mb, *mreq; 1681 struct vnode *vp, *dirp; 1682 struct vattr dirfor, diraft; 1683 nfsfh_t nfh; 1684 fhandle_t *fhp; 1685 u_quad_t frev; 1686 1687 #ifndef nolint 1688 vp = (struct vnode *)0; 1689 #endif 1690 fhp = &nfh.fh_generic; 1691 nfsm_srvmtofh(fhp); 1692 nfsm_srvnamesiz(len); 1693 nd.ni_cnd.cn_cred = cred; 1694 nd.ni_cnd.cn_nameiop = DELETE; 1695 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 1696 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos, 1697 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 1698 if (dirp) { 1699 if (v3) 1700 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, 1701 procp); 1702 else 1703 vrele(dirp); 1704 } 1705 if (!error) { 1706 vp = nd.ni_vp; 1707 if (vp->v_type == VDIR && 1708 (error = suser(cred, (u_short *)0)) != 0) 1709 goto out; 1710 /* 1711 * The root of a mounted filesystem cannot be deleted. 1712 */ 1713 if (vp->v_flag & VROOT) { 1714 error = EBUSY; 1715 goto out; 1716 } 1717 out: 1718 if (!error) { 1719 nqsrv_getl(nd.ni_dvp, ND_WRITE); 1720 nqsrv_getl(vp, ND_WRITE); 1721 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 1722 } else { 1723 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 1724 if (nd.ni_dvp == vp) 1725 vrele(nd.ni_dvp); 1726 else 1727 vput(nd.ni_dvp); 1728 vput(vp); 1729 } 1730 } 1731 if (dirp && v3) { 1732 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 1733 vrele(dirp); 1734 } 1735 nfsm_reply(NFSX_WCCDATA(v3)); 1736 if (v3) { 1737 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1738 return (0); 1739 } 1740 nfsm_srvdone; 1741 } 1742 1743 /* 1744 * nfs rename service 1745 */ 1746 int 1747 nfsrv_rename(nfsd, slp, procp, mrq) 1748 struct nfsrv_descript *nfsd; 1749 struct nfssvc_sock *slp; 1750 struct proc *procp; 1751 struct mbuf **mrq; 1752 { 1753 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1754 struct mbuf *nam = nfsd->nd_nam; 1755 caddr_t dpos = nfsd->nd_dpos; 1756 struct ucred *cred = &nfsd->nd_cr; 1757 u_int32_t *tl; 1758 int32_t t1; 1759 caddr_t bpos; 1760 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1; 1761 int tdirfor_ret = 1, tdiraft_ret = 1; 1762 int v3 = (nfsd->nd_flag & ND_NFSV3); 1763 char *cp2; 1764 struct mbuf *mb, *mreq; 1765 struct nameidata fromnd, tond; 1766 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0; 1767 struct vnode *tdirp = (struct vnode *)0; 1768 struct vattr fdirfor, fdiraft, tdirfor, tdiraft; 1769 nfsfh_t fnfh, tnfh; 1770 fhandle_t *ffhp, *tfhp; 1771 u_quad_t frev; 1772 uid_t saved_uid; 1773 1774 #ifndef nolint 1775 fvp = (struct vnode *)0; 1776 #endif 1777 ffhp = &fnfh.fh_generic; 1778 tfhp = &tnfh.fh_generic; 1779 fromnd.ni_cnd.cn_nameiop = 0; 1780 tond.ni_cnd.cn_nameiop = 0; 1781 nfsm_srvmtofh(ffhp); 1782 nfsm_srvnamesiz(len); 1783 /* 1784 * Remember our original uid so that we can reset cr_uid before 1785 * the second nfs_namei() call, in case it is remapped. 1786 */ 1787 saved_uid = cred->cr_uid; 1788 fromnd.ni_cnd.cn_cred = cred; 1789 fromnd.ni_cnd.cn_nameiop = DELETE; 1790 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART; 1791 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md, 1792 &dpos, &fdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 1793 if (fdirp) { 1794 if (v3) 1795 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred, 1796 procp); 1797 else { 1798 vrele(fdirp); 1799 fdirp = (struct vnode *)0; 1800 } 1801 } 1802 if (error) { 1803 nfsm_reply(2 * NFSX_WCCDATA(v3)); 1804 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft); 1805 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft); 1806 if (fdirp) 1807 vrele(fdirp); 1808 return (0); 1809 } 1810 fvp = fromnd.ni_vp; 1811 nfsm_srvmtofh(tfhp); 1812 nfsm_strsiz(len2, NFS_MAXNAMLEN); 1813 cred->cr_uid = saved_uid; 1814 tond.ni_cnd.cn_cred = cred; 1815 tond.ni_cnd.cn_nameiop = RENAME; 1816 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART; 1817 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md, 1818 &dpos, &tdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 1819 if (tdirp) { 1820 if (v3) 1821 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred, 1822 procp); 1823 else { 1824 vrele(tdirp); 1825 tdirp = (struct vnode *)0; 1826 } 1827 } 1828 if (error) { 1829 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1830 vrele(fromnd.ni_dvp); 1831 vrele(fvp); 1832 goto out1; 1833 } 1834 tdvp = tond.ni_dvp; 1835 tvp = tond.ni_vp; 1836 if (tvp != NULL) { 1837 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1838 if (v3) 1839 error = EEXIST; 1840 else 1841 error = EISDIR; 1842 goto out; 1843 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1844 if (v3) 1845 error = EEXIST; 1846 else 1847 error = ENOTDIR; 1848 goto out; 1849 } 1850 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1851 if (v3) 1852 error = EXDEV; 1853 else 1854 error = ENOTEMPTY; 1855 goto out; 1856 } 1857 } 1858 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1859 if (v3) 1860 error = EXDEV; 1861 else 1862 error = ENOTEMPTY; 1863 goto out; 1864 } 1865 if (fvp->v_mount != tdvp->v_mount) { 1866 if (v3) 1867 error = EXDEV; 1868 else 1869 error = ENOTEMPTY; 1870 goto out; 1871 } 1872 if (fvp == tdvp) { 1873 if (v3) 1874 error = EINVAL; 1875 else 1876 error = ENOTEMPTY; 1877 } 1878 /* 1879 * If source is the same as the destination (that is the 1880 * same vnode with the same name in the same directory), 1881 * then there is nothing to do. 1882 */ 1883 if (fvp == tvp && fromnd.ni_dvp == tdvp && 1884 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen && 1885 !memcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr, 1886 fromnd.ni_cnd.cn_namelen)) 1887 error = -1; 1888 out: 1889 if (!error) { 1890 nqsrv_getl(fromnd.ni_dvp, ND_WRITE); 1891 nqsrv_getl(tdvp, ND_WRITE); 1892 if (tvp) { 1893 nqsrv_getl(tvp, ND_WRITE); 1894 } 1895 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd, 1896 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd); 1897 } else { 1898 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd); 1899 if (tdvp == tvp) 1900 vrele(tdvp); 1901 else 1902 vput(tdvp); 1903 if (tvp) 1904 vput(tvp); 1905 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1906 vrele(fromnd.ni_dvp); 1907 vrele(fvp); 1908 if (error == -1) 1909 error = 0; 1910 } 1911 vrele(tond.ni_startdir); 1912 PNBUF_PUT(tond.ni_cnd.cn_pnbuf); 1913 out1: 1914 if (fdirp) { 1915 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp); 1916 vrele(fdirp); 1917 } 1918 if (tdirp) { 1919 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp); 1920 vrele(tdirp); 1921 } 1922 vrele(fromnd.ni_startdir); 1923 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 1924 nfsm_reply(2 * NFSX_WCCDATA(v3)); 1925 if (v3) { 1926 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft); 1927 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft); 1928 } 1929 return (0); 1930 1931 nfsmout: 1932 if (fdirp) 1933 vrele(fdirp); 1934 if (tdirp) 1935 vrele(tdirp); 1936 if (tond.ni_cnd.cn_nameiop) { 1937 vrele(tond.ni_startdir); 1938 PNBUF_PUT(tond.ni_cnd.cn_pnbuf); 1939 } 1940 if (fromnd.ni_cnd.cn_nameiop) { 1941 vrele(fromnd.ni_startdir); 1942 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf); 1943 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd); 1944 vrele(fromnd.ni_dvp); 1945 vrele(fvp); 1946 } 1947 return (error); 1948 } 1949 1950 /* 1951 * nfs link service 1952 */ 1953 int 1954 nfsrv_link(nfsd, slp, procp, mrq) 1955 struct nfsrv_descript *nfsd; 1956 struct nfssvc_sock *slp; 1957 struct proc *procp; 1958 struct mbuf **mrq; 1959 { 1960 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 1961 struct mbuf *nam = nfsd->nd_nam; 1962 caddr_t dpos = nfsd->nd_dpos; 1963 struct ucred *cred = &nfsd->nd_cr; 1964 struct nameidata nd; 1965 u_int32_t *tl; 1966 int32_t t1; 1967 caddr_t bpos; 1968 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1; 1969 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3); 1970 char *cp2; 1971 struct mbuf *mb, *mreq; 1972 struct vnode *vp, *xp, *dirp = (struct vnode *)0; 1973 struct vattr dirfor, diraft, at; 1974 nfsfh_t nfh, dnfh; 1975 fhandle_t *fhp, *dfhp; 1976 u_quad_t frev; 1977 1978 fhp = &nfh.fh_generic; 1979 dfhp = &dnfh.fh_generic; 1980 nfsm_srvmtofh(fhp); 1981 nfsm_srvmtofh(dfhp); 1982 nfsm_srvnamesiz(len); 1983 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam, 1984 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 1985 if (error) { 1986 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3)); 1987 nfsm_srvpostop_attr(getret, &at); 1988 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 1989 return (0); 1990 } 1991 if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)) != 0) 1992 goto out1; 1993 nd.ni_cnd.cn_cred = cred; 1994 nd.ni_cnd.cn_nameiop = CREATE; 1995 nd.ni_cnd.cn_flags = LOCKPARENT; 1996 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos, 1997 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 1998 if (dirp) { 1999 if (v3) 2000 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, 2001 procp); 2002 else { 2003 vrele(dirp); 2004 dirp = (struct vnode *)0; 2005 } 2006 } 2007 if (error) 2008 goto out1; 2009 xp = nd.ni_vp; 2010 if (xp != NULL) { 2011 error = EEXIST; 2012 goto out; 2013 } 2014 xp = nd.ni_dvp; 2015 if (vp->v_mount != xp->v_mount) 2016 error = EXDEV; 2017 out: 2018 if (!error) { 2019 nqsrv_getl(vp, ND_WRITE); 2020 nqsrv_getl(xp, ND_WRITE); 2021 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); 2022 } else { 2023 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2024 if (nd.ni_dvp == nd.ni_vp) 2025 vrele(nd.ni_dvp); 2026 else 2027 vput(nd.ni_dvp); 2028 if (nd.ni_vp) 2029 vrele(nd.ni_vp); 2030 } 2031 out1: 2032 if (v3) 2033 getret = VOP_GETATTR(vp, &at, cred, procp); 2034 if (dirp) { 2035 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 2036 vrele(dirp); 2037 } 2038 vrele(vp); 2039 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3)); 2040 if (v3) { 2041 nfsm_srvpostop_attr(getret, &at); 2042 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2043 return (0); 2044 } 2045 nfsm_srvdone; 2046 } 2047 2048 /* 2049 * nfs symbolic link service 2050 */ 2051 int 2052 nfsrv_symlink(nfsd, slp, procp, mrq) 2053 struct nfsrv_descript *nfsd; 2054 struct nfssvc_sock *slp; 2055 struct proc *procp; 2056 struct mbuf **mrq; 2057 { 2058 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2059 struct mbuf *nam = nfsd->nd_nam; 2060 caddr_t dpos = nfsd->nd_dpos; 2061 struct ucred *cred = &nfsd->nd_cr; 2062 struct vattr va, dirfor, diraft; 2063 struct nameidata nd; 2064 u_int32_t *tl; 2065 int32_t t1; 2066 struct nfsv2_sattr *sp; 2067 char *bpos, *pathcp = NULL, *cp2; 2068 struct uio io; 2069 struct iovec iv; 2070 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1; 2071 int v3 = (nfsd->nd_flag & ND_NFSV3); 2072 struct mbuf *mb, *mreq, *mb2; 2073 struct vnode *dirp = (struct vnode *)0; 2074 nfsfh_t nfh; 2075 fhandle_t *fhp; 2076 u_quad_t frev; 2077 2078 nd.ni_cnd.cn_nameiop = 0; 2079 fhp = &nfh.fh_generic; 2080 nfsm_srvmtofh(fhp); 2081 nfsm_srvnamesiz(len); 2082 nd.ni_cnd.cn_cred = cred; 2083 nd.ni_cnd.cn_nameiop = CREATE; 2084 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART; 2085 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos, 2086 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 2087 if (dirp) { 2088 if (v3) 2089 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, 2090 procp); 2091 else { 2092 vrele(dirp); 2093 dirp = (struct vnode *)0; 2094 } 2095 } 2096 if (error) 2097 goto out; 2098 VATTR_NULL(&va); 2099 if (v3) 2100 nfsm_srvsattr(&va); 2101 nfsm_strsiz(len2, NFS_MAXPATHLEN); 2102 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK); 2103 iv.iov_base = pathcp; 2104 iv.iov_len = len2; 2105 io.uio_resid = len2; 2106 io.uio_offset = 0; 2107 io.uio_iov = &iv; 2108 io.uio_iovcnt = 1; 2109 io.uio_segflg = UIO_SYSSPACE; 2110 io.uio_rw = UIO_READ; 2111 io.uio_procp = (struct proc *)0; 2112 nfsm_mtouio(&io, len2); 2113 if (!v3) { 2114 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2115 va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 2116 } 2117 *(pathcp + len2) = '\0'; 2118 if (nd.ni_vp) { 2119 vrele(nd.ni_startdir); 2120 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 2121 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2122 if (nd.ni_dvp == nd.ni_vp) 2123 vrele(nd.ni_dvp); 2124 else 2125 vput(nd.ni_dvp); 2126 vrele(nd.ni_vp); 2127 error = EEXIST; 2128 goto out; 2129 } 2130 nqsrv_getl(nd.ni_dvp, ND_WRITE); 2131 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp); 2132 if (error) 2133 vrele(nd.ni_startdir); 2134 else { 2135 if (v3) { 2136 memset((caddr_t)fhp, 0, sizeof(nfh)); 2137 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid; 2138 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid); 2139 if (!error) 2140 error = VOP_GETATTR(nd.ni_vp, &va, cred, 2141 procp); 2142 vput(nd.ni_vp); 2143 } else { 2144 vrele(nd.ni_startdir); 2145 vput(nd.ni_vp); 2146 } 2147 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 2148 } 2149 out: 2150 if (pathcp) 2151 free(pathcp, M_TEMP); 2152 if (dirp) { 2153 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 2154 vrele(dirp); 2155 } 2156 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3)); 2157 if (v3) { 2158 if (!error) { 2159 nfsm_srvpostop_fh(fhp); 2160 nfsm_srvpostop_attr(0, &va); 2161 } 2162 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2163 } 2164 return (0); 2165 nfsmout: 2166 if (nd.ni_cnd.cn_nameiop) { 2167 vrele(nd.ni_startdir); 2168 PNBUF_PUT(nd.ni_cnd.cn_pnbuf); 2169 } 2170 if (dirp) 2171 vrele(dirp); 2172 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2173 if (nd.ni_dvp == nd.ni_vp) 2174 vrele(nd.ni_dvp); 2175 else 2176 vput(nd.ni_dvp); 2177 if (nd.ni_vp) 2178 vrele(nd.ni_vp); 2179 if (pathcp) 2180 free(pathcp, M_TEMP); 2181 return (error); 2182 } 2183 2184 /* 2185 * nfs mkdir service 2186 */ 2187 int 2188 nfsrv_mkdir(nfsd, slp, procp, mrq) 2189 struct nfsrv_descript *nfsd; 2190 struct nfssvc_sock *slp; 2191 struct proc *procp; 2192 struct mbuf **mrq; 2193 { 2194 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2195 struct mbuf *nam = nfsd->nd_nam; 2196 caddr_t dpos = nfsd->nd_dpos; 2197 struct ucred *cred = &nfsd->nd_cr; 2198 struct vattr va, dirfor, diraft; 2199 struct nfs_fattr *fp; 2200 struct nameidata nd; 2201 caddr_t cp; 2202 u_int32_t *tl; 2203 int32_t t1; 2204 caddr_t bpos; 2205 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1; 2206 int v3 = (nfsd->nd_flag & ND_NFSV3); 2207 char *cp2; 2208 struct mbuf *mb, *mb2, *mreq; 2209 struct vnode *vp, *dirp = (struct vnode *)0; 2210 nfsfh_t nfh; 2211 fhandle_t *fhp; 2212 u_quad_t frev; 2213 2214 fhp = &nfh.fh_generic; 2215 nfsm_srvmtofh(fhp); 2216 nfsm_srvnamesiz(len); 2217 nd.ni_cnd.cn_cred = cred; 2218 nd.ni_cnd.cn_nameiop = CREATE; 2219 nd.ni_cnd.cn_flags = LOCKPARENT; 2220 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos, 2221 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 2222 if (dirp) { 2223 if (v3) 2224 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, 2225 procp); 2226 else { 2227 vrele(dirp); 2228 dirp = (struct vnode *)0; 2229 } 2230 } 2231 if (error) { 2232 nfsm_reply(NFSX_WCCDATA(v3)); 2233 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2234 if (dirp) 2235 vrele(dirp); 2236 return (0); 2237 } 2238 VATTR_NULL(&va); 2239 if (v3) { 2240 nfsm_srvsattr(&va); 2241 } else { 2242 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED); 2243 va.va_mode = nfstov_mode(*tl++); 2244 } 2245 va.va_type = VDIR; 2246 vp = nd.ni_vp; 2247 if (vp != NULL) { 2248 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2249 if (nd.ni_dvp == vp) 2250 vrele(nd.ni_dvp); 2251 else 2252 vput(nd.ni_dvp); 2253 vrele(vp); 2254 error = EEXIST; 2255 goto out; 2256 } 2257 nqsrv_getl(nd.ni_dvp, ND_WRITE); 2258 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va); 2259 if (!error) { 2260 vp = nd.ni_vp; 2261 memset((caddr_t)fhp, 0, sizeof(nfh)); 2262 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 2263 error = VFS_VPTOFH(vp, &fhp->fh_fid); 2264 if (!error) 2265 error = VOP_GETATTR(vp, &va, cred, procp); 2266 vput(vp); 2267 } 2268 out: 2269 if (dirp) { 2270 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 2271 vrele(dirp); 2272 } 2273 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3)); 2274 if (v3) { 2275 if (!error) { 2276 nfsm_srvpostop_fh(fhp); 2277 nfsm_srvpostop_attr(0, &va); 2278 } 2279 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2280 } else { 2281 nfsm_srvfhtom(fhp, v3); 2282 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR); 2283 nfsm_srvfillattr(&va, fp); 2284 } 2285 return (0); 2286 nfsmout: 2287 if (dirp) 2288 vrele(dirp); 2289 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2290 if (nd.ni_dvp == nd.ni_vp) 2291 vrele(nd.ni_dvp); 2292 else 2293 vput(nd.ni_dvp); 2294 if (nd.ni_vp) 2295 vrele(nd.ni_vp); 2296 return (error); 2297 } 2298 2299 /* 2300 * nfs rmdir service 2301 */ 2302 int 2303 nfsrv_rmdir(nfsd, slp, procp, mrq) 2304 struct nfsrv_descript *nfsd; 2305 struct nfssvc_sock *slp; 2306 struct proc *procp; 2307 struct mbuf **mrq; 2308 { 2309 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2310 struct mbuf *nam = nfsd->nd_nam; 2311 caddr_t dpos = nfsd->nd_dpos; 2312 struct ucred *cred = &nfsd->nd_cr; 2313 u_int32_t *tl; 2314 int32_t t1; 2315 caddr_t bpos; 2316 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1; 2317 int v3 = (nfsd->nd_flag & ND_NFSV3); 2318 char *cp2; 2319 struct mbuf *mb, *mreq; 2320 struct vnode *vp, *dirp = (struct vnode *)0; 2321 struct vattr dirfor, diraft; 2322 nfsfh_t nfh; 2323 fhandle_t *fhp; 2324 struct nameidata nd; 2325 u_quad_t frev; 2326 2327 fhp = &nfh.fh_generic; 2328 nfsm_srvmtofh(fhp); 2329 nfsm_srvnamesiz(len); 2330 nd.ni_cnd.cn_cred = cred; 2331 nd.ni_cnd.cn_nameiop = DELETE; 2332 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF; 2333 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos, 2334 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 2335 if (dirp) { 2336 if (v3) 2337 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, 2338 procp); 2339 else { 2340 vrele(dirp); 2341 dirp = (struct vnode *)0; 2342 } 2343 } 2344 if (error) { 2345 nfsm_reply(NFSX_WCCDATA(v3)); 2346 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2347 if (dirp) 2348 vrele(dirp); 2349 return (0); 2350 } 2351 vp = nd.ni_vp; 2352 if (vp->v_type != VDIR) { 2353 error = ENOTDIR; 2354 goto out; 2355 } 2356 /* 2357 * No rmdir "." please. 2358 */ 2359 if (nd.ni_dvp == vp) { 2360 error = EINVAL; 2361 goto out; 2362 } 2363 /* 2364 * The root of a mounted filesystem cannot be deleted. 2365 */ 2366 if (vp->v_flag & VROOT) 2367 error = EBUSY; 2368 out: 2369 if (!error) { 2370 nqsrv_getl(nd.ni_dvp, ND_WRITE); 2371 nqsrv_getl(vp, ND_WRITE); 2372 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 2373 } else { 2374 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 2375 if (nd.ni_dvp == nd.ni_vp) 2376 vrele(nd.ni_dvp); 2377 else 2378 vput(nd.ni_dvp); 2379 vput(vp); 2380 } 2381 if (dirp) { 2382 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp); 2383 vrele(dirp); 2384 } 2385 nfsm_reply(NFSX_WCCDATA(v3)); 2386 if (v3) { 2387 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft); 2388 return (0); 2389 } 2390 nfsm_srvdone; 2391 } 2392 2393 /* 2394 * nfs readdir service 2395 * - mallocs what it thinks is enough to read 2396 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR 2397 * - calls VOP_READDIR() 2398 * - loops around building the reply 2399 * if the output generated exceeds count break out of loop 2400 * The nfsm_clget macro is used here so that the reply will be packed 2401 * tightly in mbuf clusters. 2402 * - it only knows that it has encountered eof when the VOP_READDIR() 2403 * reads nothing 2404 * - as such one readdir rpc will return eof false although you are there 2405 * and then the next will return eof 2406 * - it trims out records with d_fileno == 0 2407 * this doesn't matter for Unix clients, but they might confuse clients 2408 * for other os'. 2409 * - it trims out records with d_type == DT_WHT 2410 * these cannot be seen through NFS (unless we extend the protocol) 2411 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less 2412 * than requested, but this may not apply to all filesystems. For 2413 * example, client NFS does not { although it is never remote mounted 2414 * anyhow } 2415 * The alternate call nfsrv_readdirplus() does lookups as well. 2416 * PS: The NFS protocol spec. does not clarify what the "count" byte 2417 * argument is a count of.. just name strings and file id's or the 2418 * entire reply rpc or ... 2419 * I tried just file name and id sizes and it confused the Sun client, 2420 * so I am using the full rpc size now. The "paranoia.." comment refers 2421 * to including the status longwords that are not a part of the dir. 2422 * "entry" structures, but are in the rpc. 2423 */ 2424 struct flrep { 2425 nfsuint64 fl_off; 2426 u_int32_t fl_postopok; 2427 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)]; 2428 u_int32_t fl_fhok; 2429 u_int32_t fl_fhsize; 2430 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)]; 2431 }; 2432 2433 int 2434 nfsrv_readdir(nfsd, slp, procp, mrq) 2435 struct nfsrv_descript *nfsd; 2436 struct nfssvc_sock *slp; 2437 struct proc *procp; 2438 struct mbuf **mrq; 2439 { 2440 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2441 struct mbuf *nam = nfsd->nd_nam; 2442 caddr_t dpos = nfsd->nd_dpos; 2443 struct ucred *cred = &nfsd->nd_cr; 2444 char *bp, *be; 2445 struct mbuf *mp; 2446 struct dirent *dp; 2447 caddr_t cp; 2448 u_int32_t *tl; 2449 int32_t t1; 2450 caddr_t bpos; 2451 struct mbuf *mb, *mb2, *mreq, *mp2; 2452 char *cpos, *cend, *cp2, *rbuf; 2453 struct vnode *vp; 2454 struct vattr at; 2455 nfsfh_t nfh; 2456 fhandle_t *fhp; 2457 struct uio io; 2458 struct iovec iv; 2459 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1; 2460 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies; 2461 int v3 = (nfsd->nd_flag & ND_NFSV3); 2462 u_quad_t frev, off, toff, verf; 2463 off_t *cookies = NULL, *cookiep; 2464 nfsuint64 jar; 2465 2466 fhp = &nfh.fh_generic; 2467 nfsm_srvmtofh(fhp); 2468 if (v3) { 2469 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 2470 toff = fxdr_hyper(tl); 2471 tl += 2; 2472 verf = fxdr_hyper(tl); 2473 tl += 2; 2474 } else { 2475 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2476 toff = fxdr_unsigned(u_quad_t, *tl++); 2477 } 2478 off = toff; 2479 cnt = fxdr_unsigned(int, *tl); 2480 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 2481 xfer = NFS_SRVMAXDATA(nfsd); 2482 if (siz > xfer) 2483 siz = xfer; 2484 fullsiz = siz; 2485 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, 2486 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 2487 if (!error && vp->v_type != VDIR) { 2488 error = ENOTDIR; 2489 vput(vp); 2490 } 2491 if (error) { 2492 nfsm_reply(NFSX_UNSIGNED); 2493 nfsm_srvpostop_attr(getret, &at); 2494 return (0); 2495 } 2496 nqsrv_getl(vp, ND_READ); 2497 if (v3) { 2498 error = getret = VOP_GETATTR(vp, &at, cred, procp); 2499 #ifdef NFS3_STRICTVERF 2500 /* 2501 * XXX This check is too strict for Solaris 2.5 clients. 2502 */ 2503 if (!error && toff && verf != at.va_filerev) 2504 error = NFSERR_BAD_COOKIE; 2505 #endif 2506 } 2507 if (!error) 2508 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0); 2509 if (error) { 2510 vput(vp); 2511 nfsm_reply(NFSX_POSTOPATTR(v3)); 2512 nfsm_srvpostop_attr(getret, &at); 2513 return (0); 2514 } 2515 VOP_UNLOCK(vp, 0); 2516 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2517 again: 2518 iv.iov_base = rbuf; 2519 iv.iov_len = fullsiz; 2520 io.uio_iov = &iv; 2521 io.uio_iovcnt = 1; 2522 io.uio_offset = (off_t)off; 2523 io.uio_resid = fullsiz; 2524 io.uio_segflg = UIO_SYSSPACE; 2525 io.uio_rw = UIO_READ; 2526 io.uio_procp = (struct proc *)0; 2527 eofflag = 0; 2528 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2529 2530 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies); 2531 2532 off = (off_t)io.uio_offset; 2533 if (!cookies && !error) 2534 error = NFSERR_PERM; 2535 if (v3) { 2536 getret = VOP_GETATTR(vp, &at, cred, procp); 2537 if (!error) 2538 error = getret; 2539 } 2540 2541 VOP_UNLOCK(vp, 0); 2542 if (error) { 2543 vrele(vp); 2544 free((caddr_t)rbuf, M_TEMP); 2545 if (cookies) 2546 free((caddr_t)cookies, M_TEMP); 2547 nfsm_reply(NFSX_POSTOPATTR(v3)); 2548 nfsm_srvpostop_attr(getret, &at); 2549 return (0); 2550 } 2551 if (io.uio_resid) { 2552 siz -= io.uio_resid; 2553 2554 /* 2555 * If nothing read, return eof 2556 * rpc reply 2557 */ 2558 if (siz == 0) { 2559 vrele(vp); 2560 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + 2561 2 * NFSX_UNSIGNED); 2562 if (v3) { 2563 nfsm_srvpostop_attr(getret, &at); 2564 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2565 txdr_hyper(at.va_filerev, tl); 2566 tl += 2; 2567 } else 2568 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2569 *tl++ = nfs_false; 2570 *tl = nfs_true; 2571 free((caddr_t)rbuf, M_TEMP); 2572 free((caddr_t)cookies, M_TEMP); 2573 return (0); 2574 } 2575 } 2576 2577 /* 2578 * Check for degenerate cases of nothing useful read. 2579 * If so go try again 2580 */ 2581 cpos = rbuf; 2582 cend = rbuf + siz; 2583 dp = (struct dirent *)cpos; 2584 cookiep = cookies; 2585 2586 while (cpos < cend && ncookies > 0 && 2587 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) { 2588 cpos += dp->d_reclen; 2589 dp = (struct dirent *)cpos; 2590 cookiep++; 2591 ncookies--; 2592 } 2593 if (cpos >= cend || ncookies == 0) { 2594 toff = off; 2595 siz = fullsiz; 2596 goto again; 2597 } 2598 2599 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */ 2600 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz); 2601 if (v3) { 2602 nfsm_srvpostop_attr(getret, &at); 2603 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2604 txdr_hyper(at.va_filerev, tl); 2605 } 2606 mp = mp2 = mb; 2607 bp = bpos; 2608 be = bp + M_TRAILINGSPACE(mp); 2609 2610 /* Loop through the records and build reply */ 2611 while (cpos < cend && ncookies > 0) { 2612 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) { 2613 nlen = dp->d_namlen; 2614 rem = nfsm_rndup(nlen)-nlen; 2615 len += (4 * NFSX_UNSIGNED + nlen + rem); 2616 if (v3) 2617 len += 2 * NFSX_UNSIGNED; 2618 if (len > cnt) { 2619 eofflag = 0; 2620 break; 2621 } 2622 /* 2623 * Build the directory record xdr from 2624 * the dirent entry. 2625 */ 2626 nfsm_clget; 2627 *tl = nfs_true; 2628 bp += NFSX_UNSIGNED; 2629 if (v3) { 2630 nfsm_clget; 2631 *tl = 0; 2632 bp += NFSX_UNSIGNED; 2633 } 2634 nfsm_clget; 2635 *tl = txdr_unsigned(dp->d_fileno); 2636 bp += NFSX_UNSIGNED; 2637 nfsm_clget; 2638 *tl = txdr_unsigned(nlen); 2639 bp += NFSX_UNSIGNED; 2640 2641 /* And loop around copying the name */ 2642 xfer = nlen; 2643 cp = dp->d_name; 2644 while (xfer > 0) { 2645 nfsm_clget; 2646 if ((bp+xfer) > be) 2647 tsiz = be-bp; 2648 else 2649 tsiz = xfer; 2650 memcpy(bp, cp, tsiz); 2651 bp += tsiz; 2652 xfer -= tsiz; 2653 if (xfer > 0) 2654 cp += tsiz; 2655 } 2656 /* And null pad to an int32_t boundary */ 2657 for (i = 0; i < rem; i++) 2658 *bp++ = '\0'; 2659 nfsm_clget; 2660 2661 /* Finish off the record */ 2662 txdr_hyper(*cookiep, &jar); 2663 if (v3) { 2664 *tl = jar.nfsuquad[0]; 2665 bp += NFSX_UNSIGNED; 2666 nfsm_clget; 2667 } 2668 *tl = jar.nfsuquad[1]; 2669 bp += NFSX_UNSIGNED; 2670 } 2671 cpos += dp->d_reclen; 2672 dp = (struct dirent *)cpos; 2673 cookiep++; 2674 ncookies--; 2675 } 2676 vrele(vp); 2677 nfsm_clget; 2678 *tl = nfs_false; 2679 bp += NFSX_UNSIGNED; 2680 nfsm_clget; 2681 if (eofflag) 2682 *tl = nfs_true; 2683 else 2684 *tl = nfs_false; 2685 bp += NFSX_UNSIGNED; 2686 if (mp != mb) { 2687 if (bp < be) 2688 mp->m_len = bp - mtod(mp, caddr_t); 2689 } else 2690 mp->m_len += bp - bpos; 2691 free((caddr_t)rbuf, M_TEMP); 2692 free((caddr_t)cookies, M_TEMP); 2693 nfsm_srvdone; 2694 } 2695 2696 int 2697 nfsrv_readdirplus(nfsd, slp, procp, mrq) 2698 struct nfsrv_descript *nfsd; 2699 struct nfssvc_sock *slp; 2700 struct proc *procp; 2701 struct mbuf **mrq; 2702 { 2703 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 2704 struct mbuf *nam = nfsd->nd_nam; 2705 caddr_t dpos = nfsd->nd_dpos; 2706 struct ucred *cred = &nfsd->nd_cr; 2707 char *bp, *be; 2708 struct mbuf *mp; 2709 struct dirent *dp; 2710 caddr_t cp; 2711 u_int32_t *tl; 2712 int32_t t1; 2713 caddr_t bpos; 2714 struct mbuf *mb, *mb2, *mreq, *mp2; 2715 char *cpos, *cend, *cp2, *rbuf; 2716 struct vnode *vp, *nvp; 2717 struct flrep fl; 2718 nfsfh_t nfh; 2719 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh; 2720 struct uio io; 2721 struct iovec iv; 2722 struct vattr va, at, *vap = &va; 2723 struct nfs_fattr *fp; 2724 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1; 2725 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies; 2726 u_quad_t frev, off, toff, verf; 2727 off_t *cookies = NULL, *cookiep; 2728 2729 fhp = &nfh.fh_generic; 2730 nfsm_srvmtofh(fhp); 2731 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 2732 toff = fxdr_hyper(tl); 2733 tl += 2; 2734 verf = fxdr_hyper(tl); 2735 tl += 2; 2736 siz = fxdr_unsigned(int, *tl++); 2737 cnt = fxdr_unsigned(int, *tl); 2738 off = toff; 2739 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 2740 xfer = NFS_SRVMAXDATA(nfsd); 2741 if (siz > xfer) 2742 siz = xfer; 2743 fullsiz = siz; 2744 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, 2745 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 2746 if (!error && vp->v_type != VDIR) { 2747 error = ENOTDIR; 2748 vput(vp); 2749 } 2750 if (error) { 2751 nfsm_reply(NFSX_UNSIGNED); 2752 nfsm_srvpostop_attr(getret, &at); 2753 return (0); 2754 } 2755 error = getret = VOP_GETATTR(vp, &at, cred, procp); 2756 #ifdef NFS3_STRICTVERF 2757 /* 2758 * XXX This check is too strict for Solaris 2.5 clients. 2759 */ 2760 if (!error && toff && verf != at.va_filerev) 2761 error = NFSERR_BAD_COOKIE; 2762 #endif 2763 if (!error) { 2764 nqsrv_getl(vp, ND_READ); 2765 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0); 2766 } 2767 if (error) { 2768 vput(vp); 2769 nfsm_reply(NFSX_V3POSTOPATTR); 2770 nfsm_srvpostop_attr(getret, &at); 2771 return (0); 2772 } 2773 VOP_UNLOCK(vp, 0); 2774 2775 rbuf = malloc(siz, M_TEMP, M_WAITOK); 2776 again: 2777 iv.iov_base = rbuf; 2778 iv.iov_len = fullsiz; 2779 io.uio_iov = &iv; 2780 io.uio_iovcnt = 1; 2781 io.uio_offset = (off_t)off; 2782 io.uio_resid = fullsiz; 2783 io.uio_segflg = UIO_SYSSPACE; 2784 io.uio_rw = UIO_READ; 2785 io.uio_procp = (struct proc *)0; 2786 eofflag = 0; 2787 2788 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2789 2790 error = VOP_READDIR(vp, &io, cred, &eofflag, &cookies, &ncookies); 2791 2792 off = (u_quad_t)io.uio_offset; 2793 getret = VOP_GETATTR(vp, &at, cred, procp); 2794 2795 VOP_UNLOCK(vp, 0); 2796 2797 /* 2798 * If the VGET operation doesn't work for this filesystem, 2799 * we can't support readdirplus. Returning NOTSUPP should 2800 * make clients fall back to plain readdir. 2801 * There's no need to check for VPTOFH as well, we wouldn't 2802 * even be here otherwise. 2803 */ 2804 if (!getret) { 2805 if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp))) 2806 getret = (getret == EOPNOTSUPP) ? 2807 NFSERR_NOTSUPP : NFSERR_IO; 2808 else 2809 vput(nvp); 2810 } 2811 2812 if (!cookies && !error) 2813 error = NFSERR_PERM; 2814 if (!error) 2815 error = getret; 2816 if (error) { 2817 vrele(vp); 2818 if (cookies) 2819 free((caddr_t)cookies, M_TEMP); 2820 free((caddr_t)rbuf, M_TEMP); 2821 nfsm_reply(NFSX_V3POSTOPATTR); 2822 nfsm_srvpostop_attr(getret, &at); 2823 return (0); 2824 } 2825 if (io.uio_resid) { 2826 siz -= io.uio_resid; 2827 2828 /* 2829 * If nothing read, return eof 2830 * rpc reply 2831 */ 2832 if (siz == 0) { 2833 vrele(vp); 2834 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2835 2 * NFSX_UNSIGNED); 2836 nfsm_srvpostop_attr(getret, &at); 2837 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 2838 txdr_hyper(at.va_filerev, tl); 2839 tl += 2; 2840 *tl++ = nfs_false; 2841 *tl = nfs_true; 2842 free((caddr_t)cookies, M_TEMP); 2843 free((caddr_t)rbuf, M_TEMP); 2844 return (0); 2845 } 2846 } 2847 2848 /* 2849 * Check for degenerate cases of nothing useful read. 2850 * If so go try again 2851 */ 2852 cpos = rbuf; 2853 cend = rbuf + siz; 2854 dp = (struct dirent *)cpos; 2855 cookiep = cookies; 2856 2857 while (cpos < cend && ncookies > 0 && 2858 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) { 2859 cpos += dp->d_reclen; 2860 dp = (struct dirent *)cpos; 2861 cookiep++; 2862 ncookies--; 2863 } 2864 if (cpos >= cend || ncookies == 0) { 2865 toff = off; 2866 siz = fullsiz; 2867 goto again; 2868 } 2869 2870 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED; 2871 nfsm_reply(cnt); 2872 nfsm_srvpostop_attr(getret, &at); 2873 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2874 txdr_hyper(at.va_filerev, tl); 2875 mp = mp2 = mb; 2876 bp = bpos; 2877 be = bp + M_TRAILINGSPACE(mp); 2878 2879 /* Loop through the records and build reply */ 2880 while (cpos < cend && ncookies > 0) { 2881 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) { 2882 nlen = dp->d_namlen; 2883 rem = nfsm_rndup(nlen)-nlen; 2884 2885 /* 2886 * For readdir_and_lookup get the vnode using 2887 * the file number. 2888 */ 2889 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp)) 2890 goto invalid; 2891 memset((caddr_t)nfhp, 0, NFSX_V3FH); 2892 nfhp->fh_fsid = 2893 nvp->v_mount->mnt_stat.f_fsid; 2894 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) { 2895 vput(nvp); 2896 goto invalid; 2897 } 2898 if (VOP_GETATTR(nvp, vap, cred, procp)) { 2899 vput(nvp); 2900 goto invalid; 2901 } 2902 vput(nvp); 2903 2904 /* 2905 * If either the dircount or maxcount will be 2906 * exceeded, get out now. Both of these lengths 2907 * are calculated conservatively, including all 2908 * XDR overheads. 2909 */ 2910 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH + 2911 NFSX_V3POSTOPATTR); 2912 dirlen += (6 * NFSX_UNSIGNED + nlen + rem); 2913 if (len > cnt || dirlen > fullsiz) { 2914 eofflag = 0; 2915 break; 2916 } 2917 2918 /* 2919 * Build the directory record xdr from 2920 * the dirent entry. 2921 */ 2922 fp = (struct nfs_fattr *)&fl.fl_fattr; 2923 nfsm_srvfillattr(vap, fp); 2924 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH); 2925 fl.fl_fhok = nfs_true; 2926 fl.fl_postopok = nfs_true; 2927 txdr_hyper(*cookiep, fl.fl_off.nfsuquad); 2928 2929 nfsm_clget; 2930 *tl = nfs_true; 2931 bp += NFSX_UNSIGNED; 2932 nfsm_clget; 2933 *tl = 0; 2934 bp += NFSX_UNSIGNED; 2935 nfsm_clget; 2936 *tl = txdr_unsigned(dp->d_fileno); 2937 bp += NFSX_UNSIGNED; 2938 nfsm_clget; 2939 *tl = txdr_unsigned(nlen); 2940 bp += NFSX_UNSIGNED; 2941 2942 /* And loop around copying the name */ 2943 xfer = nlen; 2944 cp = dp->d_name; 2945 while (xfer > 0) { 2946 nfsm_clget; 2947 if ((bp + xfer) > be) 2948 tsiz = be - bp; 2949 else 2950 tsiz = xfer; 2951 memcpy(bp, cp, tsiz); 2952 bp += tsiz; 2953 xfer -= tsiz; 2954 if (xfer > 0) 2955 cp += tsiz; 2956 } 2957 /* And null pad to an int32_t boundary */ 2958 for (i = 0; i < rem; i++) 2959 *bp++ = '\0'; 2960 2961 /* 2962 * Now copy the flrep structure out. 2963 */ 2964 xfer = sizeof (struct flrep); 2965 cp = (caddr_t)&fl; 2966 while (xfer > 0) { 2967 nfsm_clget; 2968 if ((bp + xfer) > be) 2969 tsiz = be - bp; 2970 else 2971 tsiz = xfer; 2972 memcpy(bp, cp, tsiz); 2973 bp += tsiz; 2974 xfer -= tsiz; 2975 if (xfer > 0) 2976 cp += tsiz; 2977 } 2978 } 2979 invalid: 2980 cpos += dp->d_reclen; 2981 dp = (struct dirent *)cpos; 2982 cookiep++; 2983 ncookies--; 2984 } 2985 vrele(vp); 2986 nfsm_clget; 2987 *tl = nfs_false; 2988 bp += NFSX_UNSIGNED; 2989 nfsm_clget; 2990 if (eofflag) 2991 *tl = nfs_true; 2992 else 2993 *tl = nfs_false; 2994 bp += NFSX_UNSIGNED; 2995 if (mp != mb) { 2996 if (bp < be) 2997 mp->m_len = bp - mtod(mp, caddr_t); 2998 } else 2999 mp->m_len += bp - bpos; 3000 free((caddr_t)cookies, M_TEMP); 3001 free((caddr_t)rbuf, M_TEMP); 3002 nfsm_srvdone; 3003 } 3004 3005 /* 3006 * nfs commit service 3007 */ 3008 int 3009 nfsrv_commit(nfsd, slp, procp, mrq) 3010 struct nfsrv_descript *nfsd; 3011 struct nfssvc_sock *slp; 3012 struct proc *procp; 3013 struct mbuf **mrq; 3014 { 3015 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3016 struct mbuf *nam = nfsd->nd_nam; 3017 caddr_t dpos = nfsd->nd_dpos; 3018 struct ucred *cred = &nfsd->nd_cr; 3019 struct vattr bfor, aft; 3020 struct vnode *vp; 3021 nfsfh_t nfh; 3022 fhandle_t *fhp; 3023 u_int32_t *tl; 3024 int32_t t1; 3025 caddr_t bpos; 3026 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache; 3027 char *cp2; 3028 struct mbuf *mb, *mb2, *mreq; 3029 u_quad_t frev, off; 3030 3031 #ifndef nolint 3032 cache = 0; 3033 #endif 3034 fhp = &nfh.fh_generic; 3035 nfsm_srvmtofh(fhp); 3036 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 3037 3038 off = fxdr_hyper(tl); 3039 tl += 2; 3040 cnt = fxdr_unsigned(int, *tl); 3041 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, 3042 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 3043 if (error) { 3044 nfsm_reply(2 * NFSX_UNSIGNED); 3045 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft); 3046 return (0); 3047 } 3048 for_ret = VOP_GETATTR(vp, &bfor, cred, procp); 3049 error = VOP_FSYNC(vp, cred, FSYNC_WAIT, off, off + cnt, procp); 3050 aft_ret = VOP_GETATTR(vp, &aft, cred, procp); 3051 vput(vp); 3052 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF); 3053 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft); 3054 if (!error) { 3055 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF); 3056 *tl++ = txdr_unsigned(boottime.tv_sec); 3057 *tl = txdr_unsigned(boottime.tv_usec); 3058 } else 3059 return (0); 3060 nfsm_srvdone; 3061 } 3062 3063 /* 3064 * nfs statfs service 3065 */ 3066 int 3067 nfsrv_statfs(nfsd, slp, procp, mrq) 3068 struct nfsrv_descript *nfsd; 3069 struct nfssvc_sock *slp; 3070 struct proc *procp; 3071 struct mbuf **mrq; 3072 { 3073 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3074 struct mbuf *nam = nfsd->nd_nam; 3075 caddr_t dpos = nfsd->nd_dpos; 3076 struct ucred *cred = &nfsd->nd_cr; 3077 struct statfs *sf; 3078 struct nfs_statfs *sfp; 3079 u_int32_t *tl; 3080 int32_t t1; 3081 caddr_t bpos; 3082 int error = 0, rdonly, cache, getret = 1; 3083 int v3 = (nfsd->nd_flag & ND_NFSV3); 3084 char *cp2; 3085 struct mbuf *mb, *mb2, *mreq; 3086 struct vnode *vp; 3087 struct vattr at; 3088 nfsfh_t nfh; 3089 fhandle_t *fhp; 3090 struct statfs statfs; 3091 u_quad_t frev, tval; 3092 3093 #ifndef nolint 3094 cache = 0; 3095 #endif 3096 fhp = &nfh.fh_generic; 3097 nfsm_srvmtofh(fhp); 3098 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, 3099 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 3100 if (error) { 3101 nfsm_reply(NFSX_UNSIGNED); 3102 nfsm_srvpostop_attr(getret, &at); 3103 return (0); 3104 } 3105 sf = &statfs; 3106 error = VFS_STATFS(vp->v_mount, sf, procp); 3107 getret = VOP_GETATTR(vp, &at, cred, procp); 3108 vput(vp); 3109 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3)); 3110 if (v3) 3111 nfsm_srvpostop_attr(getret, &at); 3112 if (error) 3113 return (0); 3114 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3)); 3115 if (v3) { 3116 tval = (u_quad_t)((quad_t)sf->f_blocks * (quad_t)sf->f_bsize); 3117 txdr_hyper(tval, &sfp->sf_tbytes); 3118 tval = (u_quad_t)((quad_t)sf->f_bfree * (quad_t)sf->f_bsize); 3119 txdr_hyper(tval, &sfp->sf_fbytes); 3120 tval = (u_quad_t)((quad_t)sf->f_bavail * (quad_t)sf->f_bsize); 3121 txdr_hyper(tval, &sfp->sf_abytes); 3122 tval = (u_quad_t)sf->f_files; 3123 txdr_hyper(tval, &sfp->sf_tfiles); 3124 tval = (u_quad_t)sf->f_ffree; 3125 txdr_hyper(tval, &sfp->sf_ffiles); 3126 txdr_hyper(tval, &sfp->sf_afiles); 3127 sfp->sf_invarsec = 0; 3128 } else { 3129 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA); 3130 sfp->sf_bsize = txdr_unsigned(sf->f_bsize); 3131 sfp->sf_blocks = txdr_unsigned(sf->f_blocks); 3132 sfp->sf_bfree = txdr_unsigned(sf->f_bfree); 3133 sfp->sf_bavail = txdr_unsigned(sf->f_bavail); 3134 } 3135 nfsm_srvdone; 3136 } 3137 3138 /* 3139 * nfs fsinfo service 3140 */ 3141 int 3142 nfsrv_fsinfo(nfsd, slp, procp, mrq) 3143 struct nfsrv_descript *nfsd; 3144 struct nfssvc_sock *slp; 3145 struct proc *procp; 3146 struct mbuf **mrq; 3147 { 3148 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3149 struct mbuf *nam = nfsd->nd_nam; 3150 caddr_t dpos = nfsd->nd_dpos; 3151 struct ucred *cred = &nfsd->nd_cr; 3152 u_int32_t *tl; 3153 struct nfsv3_fsinfo *sip; 3154 int32_t t1; 3155 caddr_t bpos; 3156 int error = 0, rdonly, cache, getret = 1, pref; 3157 char *cp2; 3158 struct mbuf *mb, *mb2, *mreq; 3159 struct vnode *vp; 3160 struct vattr at; 3161 nfsfh_t nfh; 3162 fhandle_t *fhp; 3163 u_quad_t frev, maxfsize; 3164 struct statfs sb; 3165 3166 #ifndef nolint 3167 cache = 0; 3168 #endif 3169 fhp = &nfh.fh_generic; 3170 nfsm_srvmtofh(fhp); 3171 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, 3172 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 3173 if (error) { 3174 nfsm_reply(NFSX_UNSIGNED); 3175 nfsm_srvpostop_attr(getret, &at); 3176 return (0); 3177 } 3178 3179 /* XXX Try to make a guess on the max file size. */ 3180 VFS_STATFS(vp->v_mount, &sb, (struct proc *)0); 3181 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1; 3182 3183 getret = VOP_GETATTR(vp, &at, cred, procp); 3184 vput(vp); 3185 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO); 3186 nfsm_srvpostop_attr(getret, &at); 3187 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO); 3188 3189 /* 3190 * XXX 3191 * There should be file system VFS OP(s) to get this information. 3192 * For now, assume ufs. 3193 */ 3194 if (slp->ns_so->so_type == SOCK_DGRAM) 3195 pref = NFS_MAXDGRAMDATA; 3196 else 3197 pref = NFS_MAXDATA; 3198 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA); 3199 sip->fs_rtpref = txdr_unsigned(pref); 3200 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE); 3201 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA); 3202 sip->fs_wtpref = txdr_unsigned(pref); 3203 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE); 3204 sip->fs_dtpref = txdr_unsigned(pref); 3205 txdr_hyper(maxfsize, &sip->fs_maxfilesize); 3206 sip->fs_timedelta.nfsv3_sec = 0; 3207 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1); 3208 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK | 3209 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS | 3210 NFSV3FSINFO_CANSETTIME); 3211 nfsm_srvdone; 3212 } 3213 3214 /* 3215 * nfs pathconf service 3216 */ 3217 int 3218 nfsrv_pathconf(nfsd, slp, procp, mrq) 3219 struct nfsrv_descript *nfsd; 3220 struct nfssvc_sock *slp; 3221 struct proc *procp; 3222 struct mbuf **mrq; 3223 { 3224 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md; 3225 struct mbuf *nam = nfsd->nd_nam; 3226 caddr_t dpos = nfsd->nd_dpos; 3227 struct ucred *cred = &nfsd->nd_cr; 3228 u_int32_t *tl; 3229 struct nfsv3_pathconf *pc; 3230 int32_t t1; 3231 caddr_t bpos; 3232 int error = 0, rdonly, cache, getret = 1; 3233 register_t linkmax, namemax, chownres, notrunc; 3234 char *cp2; 3235 struct mbuf *mb, *mb2, *mreq; 3236 struct vnode *vp; 3237 struct vattr at; 3238 nfsfh_t nfh; 3239 fhandle_t *fhp; 3240 u_quad_t frev; 3241 3242 #ifndef nolint 3243 cache = 0; 3244 #endif 3245 fhp = &nfh.fh_generic; 3246 nfsm_srvmtofh(fhp); 3247 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, 3248 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE); 3249 if (error) { 3250 nfsm_reply(NFSX_UNSIGNED); 3251 nfsm_srvpostop_attr(getret, &at); 3252 return (0); 3253 } 3254 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax); 3255 if (!error) 3256 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax); 3257 if (!error) 3258 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres); 3259 if (!error) 3260 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc); 3261 getret = VOP_GETATTR(vp, &at, cred, procp); 3262 vput(vp); 3263 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF); 3264 nfsm_srvpostop_attr(getret, &at); 3265 if (error) 3266 return (0); 3267 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF); 3268 3269 pc->pc_linkmax = txdr_unsigned(linkmax); 3270 pc->pc_namemax = txdr_unsigned(namemax); 3271 pc->pc_notrunc = txdr_unsigned(notrunc); 3272 pc->pc_chownrestricted = txdr_unsigned(chownres); 3273 3274 /* 3275 * These should probably be supported by VOP_PATHCONF(), but 3276 * until msdosfs is exportable (why would you want to?), the 3277 * Unix defaults should be ok. 3278 */ 3279 pc->pc_caseinsensitive = nfs_false; 3280 pc->pc_casepreserving = nfs_true; 3281 nfsm_srvdone; 3282 } 3283 3284 /* 3285 * Null operation, used by clients to ping server 3286 */ 3287 /* ARGSUSED */ 3288 int 3289 nfsrv_null(nfsd, slp, procp, mrq) 3290 struct nfsrv_descript *nfsd; 3291 struct nfssvc_sock *slp; 3292 struct proc *procp; 3293 struct mbuf **mrq; 3294 { 3295 struct mbuf *mrep = nfsd->nd_mrep; 3296 caddr_t bpos; 3297 int error = NFSERR_RETVOID, cache = 0; 3298 struct mbuf *mb, *mreq; 3299 u_quad_t frev; 3300 3301 nfsm_reply(0); 3302 return (0); 3303 } 3304 3305 /* 3306 * No operation, used for obsolete procedures 3307 */ 3308 /* ARGSUSED */ 3309 int 3310 nfsrv_noop(nfsd, slp, procp, mrq) 3311 struct nfsrv_descript *nfsd; 3312 struct nfssvc_sock *slp; 3313 struct proc *procp; 3314 struct mbuf **mrq; 3315 { 3316 struct mbuf *mrep = nfsd->nd_mrep; 3317 caddr_t bpos; 3318 int error, cache = 0; 3319 struct mbuf *mb, *mreq; 3320 u_quad_t frev; 3321 3322 if (nfsd->nd_repstat) 3323 error = nfsd->nd_repstat; 3324 else 3325 error = EPROCUNAVAIL; 3326 nfsm_reply(0); 3327 return (0); 3328 } 3329 3330 /* 3331 * Perform access checking for vnodes obtained from file handles that would 3332 * refer to files already opened by a Unix client. You cannot just use 3333 * vn_writechk() and VOP_ACCESS() for two reasons. 3334 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case 3335 * 2 - The owner is to be given access irrespective of mode bits for some 3336 * operations, so that processes that chmod after opening a file don't 3337 * break. I don't like this because it opens a security hole, but since 3338 * the nfs server opens a security hole the size of a barn door anyhow, 3339 * what the heck. 3340 * 3341 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS() 3342 * will return EPERM instead of EACCESS. EPERM is always an error. 3343 */ 3344 int 3345 nfsrv_access(vp, flags, cred, rdonly, p, override) 3346 struct vnode *vp; 3347 int flags; 3348 struct ucred *cred; 3349 int rdonly; 3350 struct proc *p; 3351 { 3352 struct vattr vattr; 3353 int error; 3354 if (flags & VWRITE) { 3355 /* Just vn_writechk() changed to check rdonly */ 3356 /* 3357 * Disallow write attempts on read-only file systems; 3358 * unless the file is a socket or a block or character 3359 * device resident on the file system. 3360 */ 3361 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) { 3362 switch (vp->v_type) { 3363 case VREG: 3364 case VDIR: 3365 case VLNK: 3366 return (EROFS); 3367 default: 3368 break; 3369 } 3370 } 3371 3372 /* 3373 * If the vnode is in use as a process's text, 3374 * we can't allow writing. 3375 */ 3376 if (vp->v_flag & VTEXT) 3377 return (ETXTBSY); 3378 } 3379 error = VOP_GETATTR(vp, &vattr, cred, p); 3380 if (error) 3381 return (error); 3382 error = VOP_ACCESS(vp, flags, cred, p); 3383 /* 3384 * Allow certain operations for the owner (reads and writes 3385 * on files that are already open). 3386 */ 3387 if (override && error == EACCES && cred->cr_uid == vattr.va_uid) 3388 error = 0; 3389 return error; 3390 } 3391