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