2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
37 * $FreeBSD: src/sys/nfs/nfs_serv.c,v 1.93.2.6 2002/12/29 18:19:53 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_serv.c,v 1.48 2008/09/17 21:44:24 dillon Exp $
42 * nfs version 2 and 3 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request
46 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
47 * 3 - build the rpc reply in an mbuf list
49 * - do not mix the phases, since the nfsm_?? macros can return failures
50 * on a bad rpc or similar and do not do any vrele() or vput()'s
52 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
53 * error number iff error != 0 whereas
54 * returning an error from the server function implies a fatal error
55 * such as a badly constructed rpc request that should be dropped without
57 * For Version 3, nfsm_reply() does not return for the error case, since
58 * most version 3 rpcs return more than the status for error cases.
61 * Warning: always pay careful attention to resource cleanup on return
62 * and note that nfsm_*() macros can terminate a procedure on certain
66 #include <sys/param.h>
67 #include <sys/systm.h>
70 #include <sys/nlookup.h>
71 #include <sys/namei.h>
72 #include <sys/unistd.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/malloc.h>
79 #include <sys/dirent.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_object.h>
91 #include <sys/thread2.h>
97 #include "nfsm_subs.h"
100 #define nfsdbprintf(info) kprintf info
102 #define nfsdbprintf(info)
105 #define MAX_COMMIT_COUNT (1024 * 1024)
107 #define NUM_HEURISTIC 1017
108 #define NHUSE_INIT 64
110 #define NHUSE_MAX 2048
112 static struct nfsheur {
113 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
114 off_t nh_nextr; /* next offset for sequential detection */
115 int nh_use; /* use count for selection */
116 int nh_seqcount; /* heuristic */
117 } nfsheur[NUM_HEURISTIC];
119 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
122 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
125 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
126 int nfsrvw_procrastinate_v3 = 0;
128 static struct timespec nfsver;
130 SYSCTL_DECL(_vfs_nfs);
133 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
134 "Enable unstable and fast writes");
135 static int nfs_commit_blks;
136 static int nfs_commit_miss;
137 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
138 "Number of committed blocks");
139 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0,
140 "Number of nfs blocks committed from dirty buffers");
142 static int nfsrv_access (struct mount *, struct vnode *, int,
143 struct ucred *, int, struct thread *, int);
144 static void nfsrvw_coalesce (struct nfsrv_descript *,
145 struct nfsrv_descript *);
148 * nfs v3 access service
151 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
152 struct thread *td, struct mbuf **mrq)
154 struct sockaddr *nam = nfsd->nd_nam;
155 struct ucred *cred = &nfsd->nd_cr;
156 struct vnode *vp = NULL;
157 struct mount *mp = NULL;
160 int error = 0, rdonly, getret;
161 struct vattr vattr, *vap = &vattr;
162 u_long testmode, nfsmode;
163 struct nfsm_info info;
166 info.dpos = nfsd->nd_dpos;
167 info.md = nfsd->nd_md;
168 info.mrep = nfsd->nd_mrep;
171 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
172 fhp = &nfh.fh_generic;
173 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
174 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
175 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
176 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
178 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
179 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
183 nfsmode = fxdr_unsigned(u_int32_t, *tl);
184 if ((nfsmode & NFSV3ACCESS_READ) &&
185 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
186 nfsmode &= ~NFSV3ACCESS_READ;
187 if (vp->v_type == VDIR)
188 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
191 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
192 if ((nfsmode & testmode) &&
193 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
194 nfsmode &= ~testmode;
195 if (vp->v_type == VDIR)
196 testmode = NFSV3ACCESS_LOOKUP;
198 testmode = NFSV3ACCESS_EXECUTE;
199 if ((nfsmode & testmode) &&
200 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
201 nfsmode &= ~testmode;
202 getret = VOP_GETATTR(vp, vap);
205 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
206 NFSX_POSTOPATTR(1) + NFSX_UNSIGNED, &error));
207 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
208 tl = nfsm_build(&info, NFSX_UNSIGNED);
209 *tl = txdr_unsigned(nfsmode);
218 * nfs getattr service
221 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
222 struct thread *td, struct mbuf **mrq)
224 struct sockaddr *nam = nfsd->nd_nam;
225 struct ucred *cred = &nfsd->nd_cr;
226 struct nfs_fattr *fp;
228 struct vattr *vap = &va;
229 struct vnode *vp = NULL;
230 struct mount *mp = NULL;
233 int error = 0, rdonly;
234 struct nfsm_info info;
236 info.mrep = nfsd->nd_mrep;
237 info.md = nfsd->nd_md;
238 info.dpos = nfsd->nd_dpos;
241 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
242 fhp = &nfh.fh_generic;
243 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
244 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
245 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
247 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
251 error = VOP_GETATTR(vp, vap);
254 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
255 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3), &error));
260 fp = nfsm_build(&info, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
261 nfsm_srvfattr(nfsd, vap, fp);
272 * nfs setattr service
275 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
276 struct thread *td, struct mbuf **mrq)
278 struct sockaddr *nam = nfsd->nd_nam;
279 struct ucred *cred = &nfsd->nd_cr;
280 struct vattr va, preat;
281 struct vattr *vap = &va;
282 struct nfsv2_sattr *sp;
283 struct nfs_fattr *fp;
284 struct vnode *vp = NULL;
285 struct mount *mp = NULL;
289 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
291 struct timespec guard;
292 struct nfsm_info info;
294 info.mrep = nfsd->nd_mrep;
296 info.md = nfsd->nd_md;
297 info.dpos = nfsd->nd_dpos;
298 info.v3 = (nfsd->nd_flag & ND_NFSV3);
300 guard.tv_sec = 0; /* fix compiler warning */
303 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
304 fhp = &nfh.fh_generic;
305 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
308 ERROROUT(nfsm_srvsattr(&info, vap));
309 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
310 gcheck = fxdr_unsigned(int, *tl);
312 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
313 fxdr_nfsv3time(tl, &guard);
316 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
318 * Nah nah nah nah na nah
319 * There is a bug in the Sun client that puts 0xffff in the mode
320 * field of sattr when it should put in 0xffffffff. The u_short
321 * doesn't sign extend.
322 * --> check the low order 2 bytes for 0xffff
324 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
325 vap->va_mode = nfstov_mode(sp->sa_mode);
326 if (sp->sa_uid != nfs_xdrneg1)
327 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
328 if (sp->sa_gid != nfs_xdrneg1)
329 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
330 if (sp->sa_size != nfs_xdrneg1)
331 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
332 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
334 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
336 vap->va_atime.tv_sec =
337 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
338 vap->va_atime.tv_nsec = 0;
341 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
342 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
347 * Now that we have all the fields, lets do it.
349 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
350 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
352 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
353 2 * NFSX_UNSIGNED, &error));
354 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
361 * vp now an active resource, pay careful attention to cleanup
365 error = preat_ret = VOP_GETATTR(vp, &preat);
366 if (!error && gcheck &&
367 (preat.va_ctime.tv_sec != guard.tv_sec ||
368 preat.va_ctime.tv_nsec != guard.tv_nsec))
369 error = NFSERR_NOT_SYNC;
373 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
374 NFSX_WCCDATA(info.v3), &error));
375 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
383 * If the size is being changed write acces is required, otherwise
384 * just check for a read only file system.
386 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
387 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
392 if (vp->v_type == VDIR) {
395 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
400 error = VOP_SETATTR(vp, vap, cred);
401 postat_ret = VOP_GETATTR(vp, vap);
407 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
408 NFSX_WCCORFATTR(info.v3), &error));
410 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
415 fp = nfsm_build(&info, NFSX_V2FATTR);
416 nfsm_srvfattr(nfsd, vap, fp);
431 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
432 struct thread *td, struct mbuf **mrq)
434 struct sockaddr *nam = nfsd->nd_nam;
435 struct ucred *cred = &nfsd->nd_cr;
436 struct nfs_fattr *fp;
437 struct nlookupdata nd;
443 int error = 0, len, dirattr_ret = 1;
445 struct vattr va, dirattr, *vap = &va;
446 struct nfsm_info info;
448 info.mrep = nfsd->nd_mrep;
450 info.md = nfsd->nd_md;
451 info.dpos = nfsd->nd_dpos;
452 info.v3 = (nfsd->nd_flag & ND_NFSV3);
454 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
459 fhp = &nfh.fh_generic;
460 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
461 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
463 pubflag = nfs_ispublicfh(fhp);
465 error = nfs_namei(&nd, cred, 0, NULL, &vp,
466 fhp, len, slp, nam, &info.md, &info.dpos,
467 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
470 * namei failure, only dirp to cleanup. Clear out garbarge from
471 * structure in case macros jump to nfsmout.
477 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
481 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
482 NFSX_POSTOPATTR(info.v3), &error));
483 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
489 * Locate index file for public filehandle
491 * error is 0 on entry and 0 on exit from this block.
495 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
497 * Setup call to lookup() to see if we can find
498 * the index file. Arguably, this doesn't belong
499 * in a kernel.. Ugh. If an error occurs, do not
500 * try to install an index file and then clear the
503 * When we replace nd with ind and redirect ndp,
504 * maintenance of ni_startdir and ni_vp shift to
505 * ind and we have to clean them up in the old nd.
506 * However, the cnd resource continues to be maintained
507 * via the original nd. Confused? You aren't alone!
510 cache_copy(&nd.nl_nch, &nch);
512 error = nlookup_init_raw(&nd, nfs_pub.np_index,
513 UIO_SYSSPACE, 0, cred, &nch);
516 error = nlookup(&nd);
520 * Found an index file. Get rid of
521 * the old references. transfer vp and
522 * load up the new vp. Fortunately we do
523 * not have to deal with dvp, that would be
530 error = cache_vget(&nd.nl_nch, nd.nl_cred,
532 KKASSERT(error == 0);
537 * If the public filehandle was used, check that this lookup
538 * didn't result in a filehandle outside the publicly exported
539 * filesystem. We clear the poor vp here to avoid lockups due
543 if (vp->v_mount != nfs_pub.np_mount) {
552 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
558 * Resources at this point:
559 * ndp->ni_vp may not be NULL
564 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
565 NFSX_POSTOPATTR(info.v3), &error));
566 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
572 * Clear out some resources prior to potentially blocking. This
573 * is not as critical as ni_dvp resources in other routines, but
579 * Get underlying attribute, then release remaining resources ( for
580 * the same potential blocking reason ) and reply.
582 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
583 error = VFS_VPTOFH(vp, &fhp->fh_fid);
585 error = VOP_GETATTR(vp, vap);
589 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
590 NFSX_SRVFH(info.v3) +
591 NFSX_POSTOPORFATTR(info.v3) +
592 NFSX_POSTOPATTR(info.v3),
595 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
599 nfsm_srvfhtom(&info, fhp);
601 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
602 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
604 fp = nfsm_build(&info, NFSX_V2FATTR);
605 nfsm_srvfattr(nfsd, vap, fp);
612 nlookup_done(&nd); /* may be called twice */
619 * nfs readlink service
622 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
623 struct thread *td, struct mbuf **mrq)
625 struct sockaddr *nam = nfsd->nd_nam;
626 struct ucred *cred = &nfsd->nd_cr;
627 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
628 struct iovec *ivp = iv;
630 int error = 0, rdonly, i, tlen, len, getret;
631 struct mbuf *mp1, *mp2, *mp3;
632 struct vnode *vp = NULL;
633 struct mount *mp = NULL;
637 struct uio io, *uiop = &io;
638 struct nfsm_info info;
640 info.mrep = nfsd->nd_mrep;
642 info.md = nfsd->nd_md;
643 info.dpos = nfsd->nd_dpos;
644 info.v3 = (nfsd->nd_flag & ND_NFSV3);
646 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
651 fhp = &nfh.fh_generic;
652 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
655 while (len < NFS_MAXPATHLEN) {
656 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
657 mp1->m_len = MCLBYTES;
664 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
665 mp1->m_len = NFS_MAXPATHLEN-len;
666 len = NFS_MAXPATHLEN;
669 ivp->iov_base = mtod(mp1, caddr_t);
670 ivp->iov_len = mp1->m_len;
675 uiop->uio_iovcnt = i;
676 uiop->uio_offset = 0;
677 uiop->uio_resid = len;
678 uiop->uio_rw = UIO_READ;
679 uiop->uio_segflg = UIO_SYSSPACE;
681 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
682 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
684 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
685 2 * NFSX_UNSIGNED, &error));
686 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
690 if (vp->v_type != VLNK) {
697 error = VOP_READLINK(vp, uiop, cred);
699 getret = VOP_GETATTR(vp, &attr);
702 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
703 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
706 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
712 if (uiop->uio_resid > 0) {
713 len -= uiop->uio_resid;
714 tlen = nfsm_rndup(len);
715 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
717 tl = nfsm_build(&info, NFSX_UNSIGNED);
718 *tl = txdr_unsigned(len);
719 info.mb->m_next = mp3;
734 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
735 struct thread *td, struct mbuf **mrq)
737 struct nfsm_info info;
738 struct sockaddr *nam = nfsd->nd_nam;
739 struct ucred *cred = &nfsd->nd_cr;
743 struct nfs_fattr *fp;
747 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
749 struct vnode *vp = NULL;
750 struct mount *mp = NULL;
753 struct uio io, *uiop = &io;
754 struct vattr va, *vap = &va;
759 info.mrep = nfsd->nd_mrep;
761 info.md = nfsd->nd_md;
762 info.dpos = nfsd->nd_dpos;
763 info.v3 = (nfsd->nd_flag & ND_NFSV3);
765 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
766 fhp = &nfh.fh_generic;
767 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
769 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
770 off = fxdr_hyper(tl);
772 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
773 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
775 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
776 NFS_SRVMAXDATA(nfsd), &error));
779 * Reference vp. If an error occurs, vp will be invalid, but we
780 * have to NULL it just in case. The macros might goto nfsmout
784 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
785 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
788 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
789 2 * NFSX_UNSIGNED, &error));
790 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
795 if (vp->v_type != VREG) {
799 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
802 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
803 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
805 getret = VOP_GETATTR(vp, vap);
811 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
812 NFSX_POSTOPATTR(info.v3), &error));
813 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
819 * Calculate byte count to read
822 if (off >= vap->va_size)
824 else if ((off + reqlen) > vap->va_size)
825 cnt = vap->va_size - off;
830 * Calculate seqcount for heuristic
838 * Locate best candidate
841 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
845 if (nfsheur[hi].nh_vp == vp) {
849 if (nfsheur[hi].nh_use > 0)
850 --nfsheur[hi].nh_use;
851 hi = (hi + 1) % NUM_HEURISTIC;
852 if (nfsheur[hi].nh_use < nh->nh_use)
856 if (nh->nh_vp != vp) {
859 nh->nh_use = NHUSE_INIT;
867 * Calculate heuristic
870 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
871 if (++nh->nh_seqcount > IO_SEQMAX)
872 nh->nh_seqcount = IO_SEQMAX;
873 } else if (nh->nh_seqcount > 1) {
878 nh->nh_use += NHUSE_INC;
879 if (nh->nh_use > NHUSE_MAX)
880 nh->nh_use = NHUSE_MAX;
881 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
884 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
885 NFSX_POSTOPORFATTR(info.v3) +
886 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
889 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
891 fp = (struct nfs_fattr *)tl;
892 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
894 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
895 fp = (struct nfs_fattr *)tl;
896 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
898 len = left = nfsm_rndup(cnt);
901 * Generate the mbuf list with the uio_iov ref. to it.
906 siz = min(M_TRAILINGSPACE(m), left);
912 m = m_getcl(MB_WAIT, MT_DATA, 0);
918 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
920 uiop->uio_iov = iv2 = iv;
926 panic("nfsrv_read iov");
927 siz = min(M_TRAILINGSPACE(m), left);
929 iv->iov_base = mtod(m, caddr_t) + m->m_len;
938 uiop->uio_iovcnt = i;
939 uiop->uio_offset = off;
940 uiop->uio_resid = len;
941 uiop->uio_rw = UIO_READ;
942 uiop->uio_segflg = UIO_SYSSPACE;
943 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
944 off = uiop->uio_offset;
946 FREE((caddr_t)iv2, M_TEMP);
947 if (error || (getret = VOP_GETATTR(vp, vap))) {
954 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
955 NFSX_POSTOPATTR(info.v3),
957 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
966 nfsm_srvfattr(nfsd, vap, fp);
967 tlen = len - uiop->uio_resid;
968 cnt = cnt < tlen ? cnt : tlen;
969 tlen = nfsm_rndup(cnt);
970 if (len != tlen || tlen != cnt)
971 nfsm_adj(info.mb, len - tlen, tlen - cnt);
973 *tl++ = txdr_unsigned(cnt);
979 *tl = txdr_unsigned(cnt);
991 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
992 struct thread *td, struct mbuf **mrq)
994 struct sockaddr *nam = nfsd->nd_nam;
995 struct ucred *cred = &nfsd->nd_cr;
999 struct nfs_fattr *fp;
1001 struct vattr va, forat;
1002 struct vattr *vap = &va;
1004 int error = 0, rdonly, len, forat_ret = 1;
1005 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1006 int stable = NFSV3WRITE_FILESYNC;
1007 struct vnode *vp = NULL;
1008 struct mount *mp = NULL;
1011 struct uio io, *uiop = &io;
1012 struct nfsm_info info;
1015 info.mrep = nfsd->nd_mrep;
1017 info.md = nfsd->nd_md;
1018 info.dpos = nfsd->nd_dpos;
1019 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1021 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1022 if (info.mrep == NULL) {
1026 fhp = &nfh.fh_generic;
1027 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1029 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1030 off = fxdr_hyper(tl);
1032 stable = fxdr_unsigned(int, *tl++);
1034 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1035 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1038 stable = NFSV3WRITE_UNSTABLE;
1040 retlen = len = fxdr_unsigned(int32_t, *tl);
1044 * For NFS Version 2, it is not obvious what a write of zero length
1045 * should do, but I might as well be consistent with Version 3,
1046 * which is to return ok so long as there are no permission problems.
1052 if (mp1 == info.md) {
1054 adjust = info.dpos - mtod(mp1, caddr_t);
1055 mp1->m_len -= adjust;
1056 if (mp1->m_len > 0 && adjust > 0)
1057 mp1->m_data += adjust;
1061 else if (mp1->m_len > 0) {
1064 mp1->m_len -= (i - len);
1073 if (len > NFS_MAXDATA || len < 0 || i < len) {
1075 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1076 2 * NFSX_UNSIGNED, &error));
1077 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1082 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1083 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1086 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1087 2 * NFSX_UNSIGNED, &error));
1088 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1094 forat_ret = VOP_GETATTR(vp, &forat);
1095 if (vp->v_type != VREG) {
1099 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1102 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1107 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1108 NFSX_WCCDATA(info.v3), &error));
1109 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1116 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1118 uiop->uio_iov = iv = ivp;
1119 uiop->uio_iovcnt = cnt;
1122 if (mp1->m_len > 0) {
1123 ivp->iov_base = mtod(mp1, caddr_t);
1124 ivp->iov_len = mp1->m_len;
1132 * The IO_METASYNC flag indicates that all metadata (and not just
1133 * enough to ensure data integrity) mus be written to stable storage
1135 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1137 if (stable == NFSV3WRITE_UNSTABLE)
1138 ioflags = IO_NODELOCKED;
1139 else if (stable == NFSV3WRITE_DATASYNC)
1140 ioflags = (IO_SYNC | IO_NODELOCKED);
1142 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1143 uiop->uio_resid = len;
1144 uiop->uio_rw = UIO_WRITE;
1145 uiop->uio_segflg = UIO_SYSSPACE;
1146 uiop->uio_td = NULL;
1147 uiop->uio_offset = off;
1148 error = VOP_WRITE(vp, uiop, ioflags, cred);
1149 nfsstats.srvvop_writes++;
1150 FREE((caddr_t)iv, M_TEMP);
1152 aftat_ret = VOP_GETATTR(vp, vap);
1157 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1158 NFSX_PREOPATTR(info.v3) +
1159 NFSX_POSTOPORFATTR(info.v3) +
1160 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1163 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1169 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1170 *tl++ = txdr_unsigned(retlen);
1172 * If nfs_async is set, then pretend the write was FILESYNC.
1174 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1175 *tl++ = txdr_unsigned(stable);
1177 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1179 * Actually, there is no need to txdr these fields,
1180 * but it may make the values more human readable,
1181 * for debugging purposes.
1183 if (nfsver.tv_sec == 0)
1185 *tl++ = txdr_unsigned(nfsver.tv_sec);
1186 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1188 fp = nfsm_build(&info, NFSX_V2FATTR);
1189 nfsm_srvfattr(nfsd, vap, fp);
1199 * NFS write service with write gathering support. Called when
1200 * nfsrvw_procrastinate > 0.
1201 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1202 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1206 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1207 struct thread *td, struct mbuf **mrq)
1210 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1211 struct nfs_fattr *fp;
1214 struct nfsrvw_delayhash *wpp;
1216 struct vattr va, forat;
1218 int error = 0, rdonly, len, forat_ret = 1;
1219 int ioflags, aftat_ret = 1, adjust, zeroing;
1221 struct vnode *vp = NULL;
1222 struct mount *mp = NULL;
1223 struct uio io, *uiop = &io;
1225 struct nfsm_info info;
1229 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1237 info.mrep = nfsd->nd_mrep;
1239 info.md = nfsd->nd_md;
1240 info.dpos = nfsd->nd_dpos;
1241 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1242 cred = &nfsd->nd_cr;
1243 LIST_INIT(&nfsd->nd_coalesce);
1244 nfsd->nd_mreq = NULL;
1245 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1246 cur_usec = nfs_curusec();
1247 nfsd->nd_time = cur_usec +
1248 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1251 * Now, get the write header..
1253 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1255 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1256 nfsd->nd_off = fxdr_hyper(tl);
1258 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1260 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1261 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1264 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1266 len = fxdr_unsigned(int32_t, *tl);
1268 nfsd->nd_eoff = nfsd->nd_off + len;
1271 * Trim the header out of the mbuf list and trim off any trailing
1272 * junk so that the mbuf list has only the write data.
1278 if (mp1 == info.md) {
1280 adjust = info.dpos - mtod(mp1, caddr_t);
1281 mp1->m_len -= adjust;
1282 if (mp1->m_len > 0 && adjust > 0)
1283 mp1->m_data += adjust;
1290 mp1->m_len -= (i - len);
1296 if (len > NFS_MAXDATA || len < 0 || i < len) {
1301 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1303 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1306 nfsd->nd_mreq = info.mreq;
1307 nfsd->nd_mrep = NULL;
1312 * Add this entry to the hash and time queues.
1315 wp = slp->ns_tq.lh_first;
1316 while (wp && wp->nd_time < nfsd->nd_time) {
1318 wp = wp->nd_tq.le_next;
1320 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1322 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1324 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1326 if (nfsd->nd_mrep) {
1327 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1331 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1333 wp = wp->nd_hash.le_next;
1335 while (wp && wp->nd_off < nfsd->nd_off &&
1336 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1338 wp = wp->nd_hash.le_next;
1341 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1344 * Search the hash list for overlapping entries and
1347 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1348 wp = nfsd->nd_hash.le_next;
1349 if (NFSW_SAMECRED(owp, nfsd))
1350 nfsrvw_coalesce(owp, nfsd);
1353 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1359 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1360 * and generate the associated reply mbuf list(s).
1363 cur_usec = nfs_curusec();
1364 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1365 owp = nfsd->nd_tq.le_next;
1366 if (nfsd->nd_time > cur_usec)
1370 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1371 LIST_REMOVE(nfsd, nd_tq);
1372 LIST_REMOVE(nfsd, nd_hash);
1373 info.mrep = nfsd->nd_mrep;
1375 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1376 nfsd->nd_mrep = NULL;
1377 cred = &nfsd->nd_cr;
1378 forat_ret = aftat_ret = 1;
1379 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1380 nfsd->nd_nam, &rdonly,
1381 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1384 forat_ret = VOP_GETATTR(vp, &forat);
1385 if (vp->v_type != VREG) {
1389 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1395 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1398 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1399 ioflags = IO_NODELOCKED;
1400 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1401 ioflags = (IO_SYNC | IO_NODELOCKED);
1403 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1404 uiop->uio_rw = UIO_WRITE;
1405 uiop->uio_segflg = UIO_SYSSPACE;
1406 uiop->uio_td = NULL;
1407 uiop->uio_offset = nfsd->nd_off;
1408 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1409 if (uiop->uio_resid > 0) {
1417 uiop->uio_iovcnt = i;
1418 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1420 uiop->uio_iov = ivp = iov;
1423 if (mp1->m_len > 0) {
1424 ivp->iov_base = mtod(mp1, caddr_t);
1425 ivp->iov_len = mp1->m_len;
1431 error = VOP_WRITE(vp, uiop, ioflags, cred);
1432 nfsstats.srvvop_writes++;
1434 FREE((caddr_t)iov, M_TEMP);
1439 aftat_ret = VOP_GETATTR(vp, &va);
1445 * Loop around generating replies for all write rpcs that have
1446 * now been completed.
1450 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1452 nfsm_writereply(&info, nfsd, slp, error,
1453 NFSX_WCCDATA(info.v3));
1455 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1459 nfsm_writereply(&info, nfsd, slp, error,
1460 NFSX_PREOPATTR(info.v3) +
1461 NFSX_POSTOPORFATTR(info.v3) +
1463 NFSX_WRITEVERF(info.v3));
1465 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1467 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1468 *tl++ = txdr_unsigned(nfsd->nd_len);
1469 *tl++ = txdr_unsigned(swp->nd_stable);
1471 * Actually, there is no need to txdr these fields,
1472 * but it may make the values more human readable,
1473 * for debugging purposes.
1475 if (nfsver.tv_sec == 0)
1477 *tl++ = txdr_unsigned(nfsver.tv_sec);
1478 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1480 fp = nfsm_build(&info, NFSX_V2FATTR);
1481 nfsm_srvfattr(nfsd, &va, fp);
1484 nfsd->nd_mreq = info.mreq;
1486 panic("nfsrv_write: nd_mrep not free");
1489 * Done. Put it at the head of the timer queue so that
1490 * the final phase can return the reply.
1494 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1496 nfsd = swp->nd_coalesce.lh_first;
1498 LIST_REMOVE(nfsd, nd_tq);
1502 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1507 * Search for a reply to return.
1509 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next) {
1510 if (nfsd->nd_mreq) {
1511 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1512 LIST_REMOVE(nfsd, nd_tq);
1518 *mrq = nfsd->nd_mreq;
1527 * Coalesce the write request nfsd into owp. To do this we must:
1528 * - remove nfsd from the queues
1529 * - merge nfsd->nd_mrep into owp->nd_mrep
1530 * - update the nd_eoff and nd_stable for owp
1531 * - put nfsd on owp's nd_coalesce list
1532 * NB: Must be called at splsoftclock().
1535 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1539 struct nfsrv_descript *p;
1541 NFS_DPF(WG, ("C%03x-%03x",
1542 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1543 LIST_REMOVE(nfsd, nd_hash);
1544 LIST_REMOVE(nfsd, nd_tq);
1545 if (owp->nd_eoff < nfsd->nd_eoff) {
1546 overlap = owp->nd_eoff - nfsd->nd_off;
1548 panic("nfsrv_coalesce: bad off");
1550 m_adj(nfsd->nd_mrep, overlap);
1554 mp1->m_next = nfsd->nd_mrep;
1555 owp->nd_eoff = nfsd->nd_eoff;
1557 m_freem(nfsd->nd_mrep);
1558 nfsd->nd_mrep = NULL;
1559 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1560 owp->nd_stable = NFSV3WRITE_FILESYNC;
1561 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1562 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1563 owp->nd_stable = NFSV3WRITE_DATASYNC;
1564 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1567 * If nfsd had anything else coalesced into it, transfer them
1568 * to owp, otherwise their replies will never get sent.
1570 for (p = nfsd->nd_coalesce.lh_first; p;
1571 p = nfsd->nd_coalesce.lh_first) {
1572 LIST_REMOVE(p, nd_tq);
1573 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1578 * nfs create service
1579 * now does a truncate to 0 length via. setattr if it already exists
1582 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1583 struct thread *td, struct mbuf **mrq)
1585 struct sockaddr *nam = nfsd->nd_nam;
1586 struct ucred *cred = &nfsd->nd_cr;
1587 struct nfs_fattr *fp;
1588 struct vattr va, dirfor, diraft;
1589 struct vattr *vap = &va;
1590 struct nfsv2_sattr *sp;
1592 struct nlookupdata nd;
1593 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1594 udev_t rdev = NOUDEV;
1596 int how, exclusive_flag = 0;
1604 u_char cverf[NFSX_V3CREATEVERF];
1605 struct nfsm_info info;
1607 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1613 info.mrep = nfsd->nd_mrep;
1615 info.md = nfsd->nd_md;
1616 info.dpos = nfsd->nd_dpos;
1617 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1619 fhp = &nfh.fh_generic;
1620 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1621 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1624 * Call namei and do initial cleanup to get a few things
1625 * out of the way. If we get an initial error we cleanup
1626 * and return here to avoid special-casing the invalid nd
1627 * structure through the rest of the case. dirp may be
1628 * set even if an error occurs, but the nd structure will not
1629 * be valid at all if an error occurs so we have to invalidate it
1630 * prior to calling nfsm_reply ( which might goto nfsmout ).
1632 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1633 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1634 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1635 mp = vfs_getvfs(&fhp->fh_fsid);
1639 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1646 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1647 NFSX_WCCDATA(info.v3), &error));
1648 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1649 diraft_ret, &diraft);
1655 * No error. Continue. State:
1658 * vp may be valid or NULL if the target does not
1662 * The error state is set through the code and we may also do some
1663 * opportunistic releasing of vnodes to avoid holding locks through
1664 * NFS I/O. The cleanup at the end is a catch-all
1669 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1670 how = fxdr_unsigned(int, *tl);
1672 case NFSV3CREATE_GUARDED:
1678 case NFSV3CREATE_UNCHECKED:
1679 ERROROUT(nfsm_srvsattr(&info, vap));
1681 case NFSV3CREATE_EXCLUSIVE:
1682 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1683 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1687 vap->va_type = VREG;
1689 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1690 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1691 if (vap->va_type == VNON)
1692 vap->va_type = VREG;
1693 vap->va_mode = nfstov_mode(sp->sa_mode);
1694 switch (vap->va_type) {
1696 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1698 vap->va_size = (u_quad_t)tsize;
1703 rdev = fxdr_unsigned(long, sp->sa_size);
1711 * Iff doesn't exist, create it
1712 * otherwise just truncate to 0 length
1713 * should I set the mode too ?
1715 * The only possible error we can have at this point is EEXIST.
1716 * nd.ni_vp will also be non-NULL in that case.
1719 if (vap->va_mode == (mode_t)VNOVAL)
1721 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1723 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1728 if (exclusive_flag) {
1731 bcopy(cverf, (caddr_t)&vap->va_atime,
1733 error = VOP_SETATTR(vp, vap, cred);
1737 vap->va_type == VCHR ||
1738 vap->va_type == VBLK ||
1739 vap->va_type == VFIFO
1742 * Handle SysV FIFO node special cases. All other
1743 * devices require super user to access.
1745 if (vap->va_type == VCHR && rdev == 0xffffffff)
1746 vap->va_type = VFIFO;
1747 if (vap->va_type != VFIFO &&
1748 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1751 vap->va_rmajor = umajor(rdev);
1752 vap->va_rminor = uminor(rdev);
1755 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1762 * XXX what is this junk supposed to do ?
1769 * release dvp prior to lookup
1777 * Even though LOCKPARENT was cleared, ni_dvp may
1780 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1781 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1782 nd.ni_cnd.cn_td = td;
1783 nd.ni_cnd.cn_cred = cred;
1785 error = lookup(&nd);
1789 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1791 /* fall through on certain errors */
1793 nfsrv_object_create(nd.ni_vp);
1794 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1803 if (vap->va_size != -1) {
1804 error = nfsrv_access(mp, vp, VWRITE, cred,
1805 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1807 tempsize = vap->va_size;
1809 vap->va_size = tempsize;
1810 error = VOP_SETATTR(vp, vap, cred);
1816 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1817 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1819 error = VOP_GETATTR(vp, vap);
1822 if (exclusive_flag && !error &&
1823 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1825 diraft_ret = VOP_GETATTR(dirp, &diraft);
1829 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1830 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1831 NFSX_WCCDATA(info.v3),
1835 nfsm_srvpostop_fh(&info, fhp);
1836 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1838 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1839 diraft_ret, &diraft);
1842 nfsm_srvfhtom(&info, fhp);
1843 fp = nfsm_build(&info, NFSX_V2FATTR);
1844 nfsm_srvfattr(nfsd, vap, fp);
1849 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1870 * nfs v3 mknod service
1873 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1874 struct thread *td, struct mbuf **mrq)
1876 struct sockaddr *nam = nfsd->nd_nam;
1877 struct ucred *cred = &nfsd->nd_cr;
1878 struct vattr va, dirfor, diraft;
1879 struct vattr *vap = &va;
1881 struct nlookupdata nd;
1882 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1889 struct nfsm_info info;
1891 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1897 info.mrep = nfsd->nd_mrep;
1899 info.md = nfsd->nd_md;
1900 info.dpos = nfsd->nd_dpos;
1902 fhp = &nfh.fh_generic;
1903 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1904 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1907 * Handle nfs_namei() call. If an error occurs, the nd structure
1908 * is not valid. However, nfsm_*() routines may still jump to
1912 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1913 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1914 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1916 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1918 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1919 NFSX_WCCDATA(1), &error));
1920 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1921 diraft_ret, &diraft);
1925 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1926 vtyp = nfsv3tov_type(*tl);
1927 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1928 error = NFSERR_BADTYPE;
1932 ERROROUT(nfsm_srvsattr(&info, vap));
1933 if (vtyp == VCHR || vtyp == VBLK) {
1934 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1935 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1936 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1940 * Iff doesn't exist, create it.
1946 vap->va_type = vtyp;
1947 if (vap->va_mode == (mode_t)VNOVAL)
1949 if (vtyp == VSOCK) {
1951 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1955 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1959 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1967 * send response, cleanup, return.
1979 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1980 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1982 error = VOP_GETATTR(vp, vap);
1988 diraft_ret = VOP_GETATTR(dirp, &diraft);
1993 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1994 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
1995 NFSX_WCCDATA(1), &error));
1997 nfsm_srvpostop_fh(&info, fhp);
1998 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2000 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2001 diraft_ret, &diraft);
2021 * nfs remove service
2024 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2025 struct thread *td, struct mbuf **mrq)
2027 struct sockaddr *nam = nfsd->nd_nam;
2028 struct ucred *cred = &nfsd->nd_cr;
2029 struct nlookupdata nd;
2030 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2034 struct vattr dirfor, diraft;
2037 struct nfsm_info info;
2039 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2045 info.mrep = nfsd->nd_mrep;
2047 info.md = nfsd->nd_md;
2048 info.dpos = nfsd->nd_dpos;
2049 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2051 fhp = &nfh.fh_generic;
2052 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2053 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2055 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2056 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2057 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2060 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2063 if (vp->v_type == VDIR) {
2064 error = EPERM; /* POSIX */
2068 * The root of a mounted filesystem cannot be deleted.
2070 if (vp->v_flag & VROOT) {
2082 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2087 if (dirp && info.v3)
2088 diraft_ret = VOP_GETATTR(dirp, &diraft);
2089 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2091 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2092 diraft_ret, &diraft);
2112 * nfs rename service
2115 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2116 struct thread *td, struct mbuf **mrq)
2118 struct sockaddr *nam = nfsd->nd_nam;
2119 struct ucred *cred = &nfsd->nd_cr;
2120 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2121 int tdirfor_ret = 1, tdiraft_ret = 1;
2122 struct nlookupdata fromnd, tond;
2123 struct vnode *fvp, *fdirp, *fdvp;
2124 struct vnode *tvp, *tdirp, *tdvp;
2125 struct namecache *ncp;
2126 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2128 fhandle_t *ffhp, *tfhp;
2130 struct nfsm_info info;
2132 info.mrep = nfsd->nd_mrep;
2134 info.md = nfsd->nd_md;
2135 info.dpos = nfsd->nd_dpos;
2136 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2138 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2142 ffhp = &fnfh.fh_generic;
2143 tfhp = &tnfh.fh_generic;
2146 * Clear fields incase goto nfsmout occurs from macro.
2149 nlookup_zero(&fromnd);
2150 nlookup_zero(&tond);
2154 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2155 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2158 * Remember our original uid so that we can reset cr_uid before
2159 * the second nfs_namei() call, in case it is remapped.
2161 saved_uid = cred->cr_uid;
2162 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2164 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2165 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2168 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2171 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2172 2 * NFSX_WCCDATA(info.v3), &error));
2173 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2174 fdiraft_ret, &fdiraft);
2175 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2176 tdiraft_ret, &tdiraft);
2182 * We have to unlock the from ncp before we can safely lookup
2185 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2186 cache_unlock(&fromnd.nl_nch);
2187 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2188 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2189 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2190 cred->cr_uid = saved_uid;
2192 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2193 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2194 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2197 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2205 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2206 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2207 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2208 cache_lock(&fromnd.nl_nch);
2209 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2211 cache_unlock(&tond.nl_nch);
2212 cache_lock(&fromnd.nl_nch);
2213 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2214 cache_lock(&tond.nl_nch);
2215 cache_resolve(&tond.nl_nch, tond.nl_cred);
2217 fromnd.nl_flags |= NLC_NCPISLOCKED;
2219 fvp = fromnd.nl_nch.ncp->nc_vp;
2220 tvp = tond.nl_nch.ncp->nc_vp;
2223 * Set fdvp and tdvp. We haven't done all the topology checks
2224 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2225 * point). If we get through the checks these will be guarenteed
2228 * Holding the children ncp's should be sufficient to prevent
2229 * fdvp and tdvp ripouts.
2231 if (fromnd.nl_nch.ncp->nc_parent)
2232 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2235 if (tond.nl_nch.ncp->nc_parent)
2236 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2241 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2247 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2254 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2262 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2269 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2276 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2284 * You cannot rename a source into itself or a subdirectory of itself.
2285 * We check this by travsering the target directory upwards looking
2286 * for a match against the source.
2289 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2290 if (fromnd.nl_nch.ncp == ncp) {
2298 * If source is the same as the destination (that is the
2299 * same vnode with the same name in the same directory),
2300 * then there is nothing to do.
2302 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2307 * The VOP_NRENAME function releases all vnode references &
2308 * locks prior to returning so we need to clear the pointers
2309 * to bypass cleanup code later on.
2311 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2312 fdvp, tdvp, tond.nl_cred);
2321 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2323 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2324 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2325 2 * NFSX_WCCDATA(info.v3), &error));
2327 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2328 fdiraft_ret, &fdiraft);
2329 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2330 tdiraft_ret, &tdiraft);
2339 nlookup_done(&tond);
2342 nlookup_done(&fromnd);
2350 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2351 struct thread *td, struct mbuf **mrq)
2353 struct sockaddr *nam = nfsd->nd_nam;
2354 struct ucred *cred = &nfsd->nd_cr;
2355 struct nlookupdata nd;
2356 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2364 struct vattr dirfor, diraft, at;
2366 fhandle_t *fhp, *dfhp;
2367 struct nfsm_info info;
2369 info.mrep = nfsd->nd_mrep;
2371 info.md = nfsd->nd_md;
2372 info.dpos = nfsd->nd_dpos;
2373 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2375 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2377 dirp = dvp = vp = xp = NULL;
2380 fhp = &nfh.fh_generic;
2381 dfhp = &dnfh.fh_generic;
2382 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2383 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2384 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2386 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2387 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2389 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2390 NFSX_POSTOPATTR(info.v3) +
2391 NFSX_WCCDATA(info.v3),
2393 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2394 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2395 diraft_ret, &diraft);
2400 if (xp->v_type == VDIR) {
2401 error = EPERM; /* POSIX */
2405 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2406 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2407 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2410 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2419 if (xp->v_mount != dvp->v_mount)
2424 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2432 getret = VOP_GETATTR(xp, &at);
2434 diraft_ret = VOP_GETATTR(dirp, &diraft);
2435 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2436 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2439 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2440 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2441 diraft_ret, &diraft);
2465 * nfs symbolic link service
2468 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2469 struct thread *td, struct mbuf **mrq)
2471 struct sockaddr *nam = nfsd->nd_nam;
2472 struct ucred *cred = &nfsd->nd_cr;
2473 struct vattr va, dirfor, diraft;
2474 struct nlookupdata nd;
2475 struct vattr *vap = &va;
2476 struct nfsv2_sattr *sp;
2477 char *pathcp = NULL;
2480 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2486 struct nfsm_info info;
2488 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2494 info.mrep = nfsd->nd_mrep;
2496 info.md = nfsd->nd_md;
2497 info.dpos = nfsd->nd_dpos;
2498 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2500 fhp = &nfh.fh_generic;
2501 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2502 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2504 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2505 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2506 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2509 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2516 ERROROUT(nfsm_srvsattr(&info, vap));
2518 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2519 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2520 iv.iov_base = pathcp;
2522 io.uio_resid = len2;
2526 io.uio_segflg = UIO_SYSSPACE;
2527 io.uio_rw = UIO_READ;
2529 ERROROUT(nfsm_mtouio(&info, &io, len2));
2531 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2532 vap->va_mode = nfstov_mode(sp->sa_mode);
2534 *(pathcp + len2) = '\0';
2540 if (vap->va_mode == (mode_t)VNOVAL)
2544 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2548 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2549 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2551 error = VOP_GETATTR(vp, vap);
2566 FREE(pathcp, M_TEMP);
2570 diraft_ret = VOP_GETATTR(dirp, &diraft);
2574 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2575 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2576 NFSX_WCCDATA(info.v3),
2580 nfsm_srvpostop_fh(&info, fhp);
2581 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2583 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2584 diraft_ret, &diraft);
2597 FREE(pathcp, M_TEMP);
2605 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2606 struct thread *td, struct mbuf **mrq)
2608 struct sockaddr *nam = nfsd->nd_nam;
2609 struct ucred *cred = &nfsd->nd_cr;
2610 struct vattr va, dirfor, diraft;
2611 struct vattr *vap = &va;
2612 struct nfs_fattr *fp;
2613 struct nlookupdata nd;
2615 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2621 struct nfsm_info info;
2623 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2629 info.dpos = nfsd->nd_dpos;
2630 info.mrep = nfsd->nd_mrep;
2632 info.md = nfsd->nd_md;
2633 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2635 fhp = &nfh.fh_generic;
2636 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2637 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2639 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2640 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2641 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2644 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2647 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2648 NFSX_WCCDATA(info.v3), &error));
2649 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2650 diraft_ret, &diraft);
2656 ERROROUT(nfsm_srvsattr(&info, vap));
2658 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2659 vap->va_mode = nfstov_mode(*tl++);
2663 * At this point nd.ni_dvp is referenced and exclusively locked and
2664 * nd.ni_vp, if it exists, is referenced but not locked.
2667 vap->va_type = VDIR;
2674 * Issue mkdir op. Since SAVESTART is not set, the pathname
2675 * component is freed by the VOP call. This will fill-in
2676 * nd.ni_vp, reference, and exclusively lock it.
2678 if (vap->va_mode == (mode_t)VNOVAL)
2681 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2686 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2687 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2689 error = VOP_GETATTR(vp, vap);
2693 diraft_ret = VOP_GETATTR(dirp, &diraft);
2694 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2695 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2696 NFSX_WCCDATA(info.v3),
2700 nfsm_srvpostop_fh(&info, fhp);
2701 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2703 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2704 diraft_ret, &diraft);
2706 nfsm_srvfhtom(&info, fhp);
2707 fp = nfsm_build(&info, NFSX_V2FATTR);
2708 nfsm_srvfattr(nfsd, vap, fp);
2733 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2734 struct thread *td, struct mbuf **mrq)
2736 struct sockaddr *nam = nfsd->nd_nam;
2737 struct ucred *cred = &nfsd->nd_cr;
2738 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2742 struct vattr dirfor, diraft;
2745 struct nlookupdata nd;
2746 struct nfsm_info info;
2748 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2754 info.mrep = nfsd->nd_mrep;
2756 info.md = nfsd->nd_md;
2757 info.dpos = nfsd->nd_dpos;
2758 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2760 fhp = &nfh.fh_generic;
2761 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2762 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2764 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2765 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2766 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2769 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2772 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2773 NFSX_WCCDATA(info.v3), &error));
2774 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2775 diraft_ret, &diraft);
2779 if (vp->v_type != VDIR) {
2785 * The root of a mounted filesystem cannot be deleted.
2787 if (vp->v_flag & VROOT)
2791 * Issue or abort op. Since SAVESTART is not set, path name
2792 * component is freed by the VOP after either.
2799 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2806 diraft_ret = VOP_GETATTR(dirp, &diraft);
2807 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2809 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2810 diraft_ret, &diraft);
2832 * nfs readdir service
2833 * - mallocs what it thinks is enough to read
2834 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2835 * - calls VOP_READDIR()
2836 * - loops around building the reply
2837 * if the output generated exceeds count break out of loop
2838 * The nfsm_clget macro is used here so that the reply will be packed
2839 * tightly in mbuf clusters.
2840 * - it only knows that it has encountered eof when the VOP_READDIR()
2842 * - as such one readdir rpc will return eof false although you are there
2843 * and then the next will return eof
2844 * - it trims out records with d_fileno == 0
2845 * this doesn't matter for Unix clients, but they might confuse clients
2847 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2848 * than requested, but this may not apply to all filesystems. For
2849 * example, client NFS does not { although it is never remote mounted
2851 * The alternate call nfsrv_readdirplus() does lookups as well.
2852 * PS: The NFS protocol spec. does not clarify what the "count" byte
2853 * argument is a count of.. just name strings and file id's or the
2854 * entire reply rpc or ...
2855 * I tried just file name and id sizes and it confused the Sun client,
2856 * so I am using the full rpc size now. The "paranoia.." comment refers
2857 * to including the status longwords that are not a part of the dir.
2858 * "entry" structures, but are in the rpc.
2862 u_int32_t fl_postopok;
2863 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2865 u_int32_t fl_fhsize;
2866 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2870 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2871 struct thread *td, struct mbuf **mrq)
2873 struct sockaddr *nam = nfsd->nd_nam;
2874 struct ucred *cred = &nfsd->nd_cr;
2879 struct mbuf *mp1, *mp2;
2880 char *cpos, *cend, *rbuf;
2881 struct vnode *vp = NULL;
2882 struct mount *mp = NULL;
2888 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2889 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2890 u_quad_t off, toff, verf;
2891 off_t *cookies = NULL, *cookiep;
2892 struct nfsm_info info;
2894 info.mrep = nfsd->nd_mrep;
2896 info.md = nfsd->nd_md;
2897 info.dpos = nfsd->nd_dpos;
2898 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2900 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2901 fhp = &nfh.fh_generic;
2902 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2904 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2905 toff = fxdr_hyper(tl);
2907 verf = fxdr_hyper(tl);
2910 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2911 toff = fxdr_unsigned(u_quad_t, *tl++);
2912 verf = 0; /* shut up gcc */
2915 cnt = fxdr_unsigned(int, *tl);
2916 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2917 xfer = NFS_SRVMAXDATA(nfsd);
2918 if ((unsigned)cnt > xfer)
2920 if ((unsigned)siz > xfer)
2923 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2924 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2925 if (!error && vp->v_type != VDIR) {
2931 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2932 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2938 * Obtain lock on vnode for this section of the code
2942 error = getret = VOP_GETATTR(vp, &at);
2945 * XXX This check may be too strict for Solaris 2.5 clients.
2947 if (!error && toff && verf && verf != at.va_filerev)
2948 error = NFSERR_BAD_COOKIE;
2952 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2956 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2957 NFSX_POSTOPATTR(info.v3), &error));
2958 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2965 * end section. Allocate rbuf and continue
2967 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2970 iv.iov_len = fullsiz;
2973 io.uio_offset = (off_t)off;
2974 io.uio_resid = fullsiz;
2975 io.uio_segflg = UIO_SYSSPACE;
2976 io.uio_rw = UIO_READ;
2980 kfree((caddr_t)cookies, M_TEMP);
2983 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2984 off = (off_t)io.uio_offset;
2985 if (!cookies && !error)
2986 error = NFSERR_PERM;
2988 getret = VOP_GETATTR(vp, &at);
2995 kfree((caddr_t)rbuf, M_TEMP);
2997 kfree((caddr_t)cookies, M_TEMP);
2998 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2999 NFSX_POSTOPATTR(info.v3), &error));
3000 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3005 siz -= io.uio_resid;
3008 * If nothing read, return eof
3014 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3015 NFSX_POSTOPATTR(info.v3) +
3016 NFSX_COOKIEVERF(info.v3) +
3020 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3021 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3022 txdr_hyper(at.va_filerev, tl);
3025 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3028 FREE((caddr_t)rbuf, M_TEMP);
3029 FREE((caddr_t)cookies, M_TEMP);
3036 * Check for degenerate cases of nothing useful read.
3037 * If so go try again
3041 dp = (struct dirent *)cpos;
3044 * For some reason FreeBSD's ufs_readdir() chooses to back the
3045 * directory offset up to a block boundary, so it is necessary to
3046 * skip over the records that preceed the requested offset. This
3047 * requires the assumption that file offset cookies monotonically
3050 while (cpos < cend && ncookies > 0 &&
3051 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3052 ((u_quad_t)(*cookiep)) <= toff)) {
3053 dp = _DIRENT_NEXT(dp);
3058 if (cpos >= cend || ncookies == 0) {
3064 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3065 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3066 NFSX_POSTOPATTR(info.v3) +
3067 NFSX_COOKIEVERF(info.v3) + siz,
3070 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3071 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3072 txdr_hyper(at.va_filerev, tl);
3074 mp1 = mp2 = info.mb;
3076 be = bp + M_TRAILINGSPACE(mp1);
3078 /* Loop through the records and build reply */
3079 while (cpos < cend && ncookies > 0) {
3080 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3081 nlen = dp->d_namlen;
3082 rem = nfsm_rndup(nlen) - nlen;
3083 len += (4 * NFSX_UNSIGNED + nlen + rem);
3085 len += 2 * NFSX_UNSIGNED;
3091 * Build the directory record xdr from
3094 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3096 bp += NFSX_UNSIGNED;
3098 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3099 *tl = txdr_unsigned(dp->d_ino >> 32);
3100 bp += NFSX_UNSIGNED;
3102 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3103 *tl = txdr_unsigned(dp->d_ino);
3104 bp += NFSX_UNSIGNED;
3105 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3106 *tl = txdr_unsigned(nlen);
3107 bp += NFSX_UNSIGNED;
3109 /* And loop around copying the name */
3113 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3118 bcopy(cp, bp, tsiz);
3124 /* And null pad to a int32_t boundary */
3125 for (i = 0; i < rem; i++)
3127 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3129 /* Finish off the record */
3131 *tl = txdr_unsigned(*cookiep >> 32);
3132 bp += NFSX_UNSIGNED;
3133 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3135 *tl = txdr_unsigned(*cookiep);
3136 bp += NFSX_UNSIGNED;
3138 dp = _DIRENT_NEXT(dp);
3145 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3147 bp += NFSX_UNSIGNED;
3148 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3153 bp += NFSX_UNSIGNED;
3154 if (mp1 != info.mb) {
3156 mp1->m_len = bp - mtod(mp1, caddr_t);
3158 mp1->m_len += bp - info.bpos;
3159 FREE((caddr_t)rbuf, M_TEMP);
3160 FREE((caddr_t)cookies, M_TEMP);
3170 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3171 struct thread *td, struct mbuf **mrq)
3173 struct sockaddr *nam = nfsd->nd_nam;
3174 struct ucred *cred = &nfsd->nd_cr;
3179 struct mbuf *mp1, *mp2;
3180 char *cpos, *cend, *rbuf;
3181 struct vnode *vp = NULL, *nvp;
3182 struct mount *mp = NULL;
3185 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3188 struct vattr va, at, *vap = &va;
3189 struct nfs_fattr *fp;
3190 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3191 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3192 u_quad_t off, toff, verf;
3193 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3194 struct nfsm_info info;
3196 info.mrep = nfsd->nd_mrep;
3198 info.md = nfsd->nd_md;
3199 info.dpos = nfsd->nd_dpos;
3200 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3202 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3203 fhp = &nfh.fh_generic;
3204 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3205 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3206 toff = fxdr_hyper(tl);
3208 verf = fxdr_hyper(tl);
3210 siz = fxdr_unsigned(int, *tl++);
3211 cnt = fxdr_unsigned(int, *tl);
3213 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3214 xfer = NFS_SRVMAXDATA(nfsd);
3215 if ((unsigned)cnt > xfer)
3217 if ((unsigned)siz > xfer)
3220 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3221 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3222 if (!error && vp->v_type != VDIR) {
3228 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3229 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3233 error = getret = VOP_GETATTR(vp, &at);
3236 * XXX This check may be too strict for Solaris 2.5 clients.
3238 if (!error && toff && verf && verf != at.va_filerev)
3239 error = NFSERR_BAD_COOKIE;
3242 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3247 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3248 NFSX_V3POSTOPATTR, &error));
3249 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3254 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3257 iv.iov_len = fullsiz;
3260 io.uio_offset = (off_t)off;
3261 io.uio_resid = fullsiz;
3262 io.uio_segflg = UIO_SYSSPACE;
3263 io.uio_rw = UIO_READ;
3267 kfree((caddr_t)cookies, M_TEMP);
3270 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3271 off = (u_quad_t)io.uio_offset;
3272 getret = VOP_GETATTR(vp, &at);
3273 if (!cookies && !error)
3274 error = NFSERR_PERM;
3281 kfree((caddr_t)cookies, M_TEMP);
3282 kfree((caddr_t)rbuf, M_TEMP);
3283 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3284 NFSX_V3POSTOPATTR, &error));
3285 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3290 siz -= io.uio_resid;
3293 * If nothing read, return eof
3299 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3304 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3305 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3306 txdr_hyper(at.va_filerev, tl);
3310 FREE((caddr_t)cookies, M_TEMP);
3311 FREE((caddr_t)rbuf, M_TEMP);
3318 * Check for degenerate cases of nothing useful read.
3319 * If so go try again
3323 dp = (struct dirent *)cpos;
3326 * For some reason FreeBSD's ufs_readdir() chooses to back the
3327 * directory offset up to a block boundary, so it is necessary to
3328 * skip over the records that preceed the requested offset. This
3329 * requires the assumption that file offset cookies monotonically
3332 while (cpos < cend && ncookies > 0 &&
3333 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3334 ((u_quad_t)(*cookiep)) <= toff)) {
3335 dp = _DIRENT_NEXT(dp);
3340 if (cpos >= cend || ncookies == 0) {
3347 * Probe one of the directory entries to see if the filesystem
3350 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3351 error = NFSERR_NOTSUPP;
3354 kfree((caddr_t)cookies, M_TEMP);
3355 kfree((caddr_t)rbuf, M_TEMP);
3356 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3357 NFSX_V3POSTOPATTR, &error));
3358 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3367 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3369 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3370 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3371 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3372 txdr_hyper(at.va_filerev, tl);
3373 mp1 = mp2 = info.mb;
3375 be = bp + M_TRAILINGSPACE(mp1);
3377 /* Loop through the records and build reply */
3378 while (cpos < cend && ncookies > 0) {
3379 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3380 nlen = dp->d_namlen;
3381 rem = nfsm_rndup(nlen) - nlen;
3384 * For readdir_and_lookup get the vnode using
3387 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3389 bzero((caddr_t)nfhp, NFSX_V3FH);
3390 nfhp->fh_fsid = fhp->fh_fsid;
3391 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3396 if (VOP_GETATTR(nvp, vap)) {
3405 * If either the dircount or maxcount will be
3406 * exceeded, get out now. Both of these lengths
3407 * are calculated conservatively, including all
3410 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3412 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3413 if (len > cnt || dirlen > fullsiz) {
3419 * Build the directory record xdr from
3422 fp = (struct nfs_fattr *)&fl.fl_fattr;
3423 nfsm_srvfattr(nfsd, vap, fp);
3424 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3425 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3426 fl.fl_postopok = nfs_true;
3427 fl.fl_fhok = nfs_true;
3428 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3430 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3432 bp += NFSX_UNSIGNED;
3433 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3434 *tl = txdr_unsigned(dp->d_ino >> 32);
3435 bp += NFSX_UNSIGNED;
3436 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3437 *tl = txdr_unsigned(dp->d_ino);
3438 bp += NFSX_UNSIGNED;
3439 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3440 *tl = txdr_unsigned(nlen);
3441 bp += NFSX_UNSIGNED;
3443 /* And loop around copying the name */
3447 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3448 if ((bp + xfer) > be)
3452 bcopy(cp, bp, tsiz);
3457 /* And null pad to a int32_t boundary */
3458 for (i = 0; i < rem; i++)
3462 * Now copy the flrep structure out.
3464 xfer = sizeof (struct flrep);
3467 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3468 if ((bp + xfer) > be)
3472 bcopy(cp, bp, tsiz);
3479 dp = _DIRENT_NEXT(dp);
3486 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3488 bp += NFSX_UNSIGNED;
3489 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3494 bp += NFSX_UNSIGNED;
3495 if (mp1 != info.mb) {
3497 mp1->m_len = bp - mtod(mp1, caddr_t);
3499 mp1->m_len += bp - info.bpos;
3500 FREE((caddr_t)cookies, M_TEMP);
3501 FREE((caddr_t)rbuf, M_TEMP);
3510 * nfs commit service
3513 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3514 struct thread *td, struct mbuf **mrq)
3516 struct sockaddr *nam = nfsd->nd_nam;
3517 struct ucred *cred = &nfsd->nd_cr;
3518 struct vattr bfor, aft;
3519 struct vnode *vp = NULL;
3520 struct mount *mp = NULL;
3524 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3526 struct nfsm_info info;
3528 info.mrep = nfsd->nd_mrep;
3530 info.md = nfsd->nd_md;
3531 info.dpos = nfsd->nd_dpos;
3533 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3534 fhp = &nfh.fh_generic;
3535 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3536 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3539 * XXX At this time VOP_FSYNC() does not accept offset and byte
3540 * count parameters, so these arguments are useless (someday maybe).
3542 off = fxdr_hyper(tl);
3544 cnt = fxdr_unsigned(int, *tl);
3545 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3546 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3548 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3549 2 * NFSX_UNSIGNED, &error));
3550 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3555 for_ret = VOP_GETATTR(vp, &bfor);
3557 if (cnt > MAX_COMMIT_COUNT) {
3559 * Give up and do the whole thing
3562 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3563 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3565 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3568 * Locate and synchronously write any buffers that fall
3569 * into the requested range. Note: we are assuming that
3570 * f_iosize is a power of 2.
3572 int iosize = vp->v_mount->mnt_stat.f_iosize;
3573 int iomask = iosize - 1;
3577 * Align to iosize boundry, super-align to page boundry.
3580 cnt += off & iomask;
3581 off &= ~(u_quad_t)iomask;
3583 if (off & PAGE_MASK) {
3584 cnt += off & PAGE_MASK;
3585 off &= ~(u_quad_t)PAGE_MASK;
3590 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3591 vm_object_page_clean(vp->v_object, off / PAGE_SIZE,
3592 (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3600 * If we have a buffer and it is marked B_DELWRI we
3601 * have to lock and write it. Otherwise the prior
3602 * write is assumed to have already been committed.
3604 * WARNING: FINDBLK_TEST buffers represent stable
3605 * storage but not necessarily stable
3606 * content. It is ok in this case.
3608 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3609 if (bp->b_flags & B_DELWRI)
3610 bp = findblk(vp, loffset, 0);
3615 if (bp->b_flags & B_DELWRI) {
3632 aft_ret = VOP_GETATTR(vp, &aft);
3635 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3636 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3638 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3641 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3642 if (nfsver.tv_sec == 0)
3644 *tl++ = txdr_unsigned(nfsver.tv_sec);
3645 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3657 * nfs statfs service
3660 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3661 struct thread *td, struct mbuf **mrq)
3663 struct sockaddr *nam = nfsd->nd_nam;
3664 struct ucred *cred = &nfsd->nd_cr;
3666 struct nfs_statfs *sfp;
3667 int error = 0, rdonly, getret = 1;
3668 struct vnode *vp = NULL;
3669 struct mount *mp = NULL;
3673 struct statfs statfs;
3675 struct nfsm_info info;
3677 info.mrep = nfsd->nd_mrep;
3679 info.md = nfsd->nd_md;
3680 info.dpos = nfsd->nd_dpos;
3681 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3683 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3684 fhp = &nfh.fh_generic;
3685 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3686 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3687 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3689 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3690 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3695 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3696 getret = VOP_GETATTR(vp, &at);
3699 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3700 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3703 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3708 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3710 tval = (u_quad_t)sf->f_blocks;
3711 tval *= (u_quad_t)sf->f_bsize;
3712 txdr_hyper(tval, &sfp->sf_tbytes);
3713 tval = (u_quad_t)sf->f_bfree;
3714 tval *= (u_quad_t)sf->f_bsize;
3715 txdr_hyper(tval, &sfp->sf_fbytes);
3716 tval = (u_quad_t)sf->f_bavail;
3717 tval *= (u_quad_t)sf->f_bsize;
3718 txdr_hyper(tval, &sfp->sf_abytes);
3719 sfp->sf_tfiles.nfsuquad[0] = 0;
3720 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3721 sfp->sf_ffiles.nfsuquad[0] = 0;
3722 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3723 sfp->sf_afiles.nfsuquad[0] = 0;
3724 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3725 sfp->sf_invarsec = 0;
3727 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3728 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3729 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3730 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3731 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3741 * nfs fsinfo service
3744 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3745 struct thread *td, struct mbuf **mrq)
3747 struct sockaddr *nam = nfsd->nd_nam;
3748 struct ucred *cred = &nfsd->nd_cr;
3749 struct nfsv3_fsinfo *sip;
3750 int error = 0, rdonly, getret = 1, pref;
3751 struct vnode *vp = NULL;
3752 struct mount *mp = NULL;
3758 struct nfsm_info info;
3760 info.mrep = nfsd->nd_mrep;
3762 info.md = nfsd->nd_md;
3763 info.dpos = nfsd->nd_dpos;
3765 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3766 fhp = &nfh.fh_generic;
3767 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3768 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3769 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3771 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3772 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3777 /* XXX Try to make a guess on the max file size. */
3778 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3779 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3781 getret = VOP_GETATTR(vp, &at);
3784 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3785 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3786 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3787 sip = nfsm_build(&info, NFSX_V3FSINFO);
3791 * There should be file system VFS OP(s) to get this information.
3792 * For now, assume ufs.
3794 if (slp->ns_so->so_type == SOCK_DGRAM)
3795 pref = NFS_MAXDGRAMDATA;
3798 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3799 sip->fs_rtpref = txdr_unsigned(pref);
3800 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3801 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3802 sip->fs_wtpref = txdr_unsigned(pref);
3803 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3804 sip->fs_dtpref = txdr_unsigned(pref);
3805 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3806 sip->fs_timedelta.nfsv3_sec = 0;
3807 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3808 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3809 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3810 NFSV3FSINFO_CANSETTIME);
3819 * nfs pathconf service
3822 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3823 struct thread *td, struct mbuf **mrq)
3825 struct sockaddr *nam = nfsd->nd_nam;
3826 struct ucred *cred = &nfsd->nd_cr;
3827 struct nfsv3_pathconf *pc;
3828 int error = 0, rdonly, getret = 1;
3829 register_t linkmax, namemax, chownres, notrunc;
3830 struct vnode *vp = NULL;
3831 struct mount *mp = NULL;
3835 struct nfsm_info info;
3837 info.mrep = nfsd->nd_mrep;
3839 info.md = nfsd->nd_md;
3840 info.dpos = nfsd->nd_dpos;
3842 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3843 fhp = &nfh.fh_generic;
3844 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3845 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3846 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3848 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3849 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3853 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3855 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3857 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3859 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3860 getret = VOP_GETATTR(vp, &at);
3863 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3864 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3866 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3871 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3873 pc->pc_linkmax = txdr_unsigned(linkmax);
3874 pc->pc_namemax = txdr_unsigned(namemax);
3875 pc->pc_notrunc = txdr_unsigned(notrunc);
3876 pc->pc_chownrestricted = txdr_unsigned(chownres);
3879 * These should probably be supported by VOP_PATHCONF(), but
3880 * until msdosfs is exportable (why would you want to?), the
3881 * Unix defaults should be ok.
3883 pc->pc_caseinsensitive = nfs_false;
3884 pc->pc_casepreserving = nfs_true;
3893 * Null operation, used by clients to ping server
3897 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3898 struct thread *td, struct mbuf **mrq)
3900 struct nfsm_info info;
3901 int error = NFSERR_RETVOID;
3903 info.mrep = nfsd->nd_mrep;
3906 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3907 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3914 * No operation, used for obsolete procedures
3918 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3919 struct thread *td, struct mbuf **mrq)
3921 struct nfsm_info info;
3924 info.mrep = nfsd->nd_mrep;
3927 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3928 if (nfsd->nd_repstat)
3929 error = nfsd->nd_repstat;
3931 error = EPROCUNAVAIL;
3932 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3940 * Perform access checking for vnodes obtained from file handles that would
3941 * refer to files already opened by a Unix client. You cannot just use
3942 * vn_writechk() and VOP_ACCESS() for two reasons.
3943 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3944 * 2 - The owner is to be given access irrespective of mode bits for some
3945 * operations, so that processes that chmod after opening a file don't
3946 * break. I don't like this because it opens a security hole, but since
3947 * the nfs server opens a security hole the size of a barn door anyhow,
3950 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3951 * will return EPERM instead of EACCESS. EPERM is always an error.
3954 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3955 int rdonly, struct thread *td, int override)
3960 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3961 if (flags & VWRITE) {
3962 /* Just vn_writechk() changed to check rdonly */
3964 * Disallow write attempts on read-only file systems;
3965 * unless the file is a socket or a block or character
3966 * device resident on the file system.
3969 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3970 switch (vp->v_type) {
3980 * If there's shared text associated with
3981 * the inode, we can't allow writing.
3983 if (vp->v_flag & VTEXT)
3986 error = VOP_GETATTR(vp, &vattr);
3989 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
3991 * Allow certain operations for the owner (reads and writes
3992 * on files that are already open).
3994 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3998 #endif /* NFS_NOSERVER */