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.23 2005/05/29 10:08:36 hsu 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>
69 #include <sys/nlookup.h>
70 #include <sys/namei.h>
71 #include <sys/unistd.h>
72 #include <sys/vnode.h>
73 #include <sys/mount.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/malloc.h>
78 #include <sys/dirent.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
85 #include <vm/vm_extern.h>
86 #include <vm/vm_zone.h>
87 #include <vm/vm_object.h>
95 #include "nfsm_subs.h"
99 #define nfsdbprintf(info) printf info
101 #define nfsdbprintf(info)
104 #define MAX_COMMIT_COUNT (1024 * 1024)
106 #define NUM_HEURISTIC 1017
107 #define NHUSE_INIT 64
109 #define NHUSE_MAX 2048
111 static struct nfsheur {
112 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
113 off_t nh_nextr; /* next offset for sequential detection */
114 int nh_use; /* use count for selection */
115 int nh_seqcount; /* heuristic */
116 } nfsheur[NUM_HEURISTIC];
118 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
121 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
124 extern u_int32_t nfs_xdrneg1;
125 extern u_int32_t nfs_false, nfs_true;
126 extern enum vtype nv3tov_type[8];
127 extern struct nfsstats nfsstats;
129 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
130 int nfsrvw_procrastinate_v3 = 0;
132 static struct timespec nfsver;
134 SYSCTL_DECL(_vfs_nfs);
136 static int nfs_async;
137 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
138 static int nfs_commit_blks;
139 static int nfs_commit_miss;
140 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
141 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
143 static int nfsrv_access (struct vnode *,int,struct ucred *,int,
144 struct thread *, int);
145 static void nfsrvw_coalesce (struct nfsrv_descript *,
146 struct nfsrv_descript *);
149 * nfs v3 access service
152 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
153 struct thread *td, struct mbuf **mrq)
155 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
156 struct sockaddr *nam = nfsd->nd_nam;
157 caddr_t dpos = nfsd->nd_dpos;
158 struct ucred *cred = &nfsd->nd_cr;
159 struct vnode *vp = NULL;
165 int error = 0, rdonly, cache, getret;
167 struct mbuf *mb, *mreq, *mb2;
168 struct vattr vattr, *vap = &vattr;
169 u_long testmode, nfsmode;
172 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
176 fhp = &nfh.fh_generic;
178 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
179 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
180 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
182 nfsm_reply(NFSX_UNSIGNED);
183 nfsm_srvpostop_attr(1, (struct vattr *)0);
187 nfsmode = fxdr_unsigned(u_int32_t, *tl);
188 if ((nfsmode & NFSV3ACCESS_READ) &&
189 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
190 nfsmode &= ~NFSV3ACCESS_READ;
191 if (vp->v_type == VDIR)
192 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
195 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
196 if ((nfsmode & testmode) &&
197 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
198 nfsmode &= ~testmode;
199 if (vp->v_type == VDIR)
200 testmode = NFSV3ACCESS_LOOKUP;
202 testmode = NFSV3ACCESS_EXECUTE;
203 if ((nfsmode & testmode) &&
204 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
205 nfsmode &= ~testmode;
206 getret = VOP_GETATTR(vp, vap, td);
209 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
210 nfsm_srvpostop_attr(getret, vap);
211 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
212 *tl = txdr_unsigned(nfsmode);
220 * nfs getattr service
223 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
224 struct thread *td, struct mbuf **mrq)
226 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
227 struct sockaddr *nam = nfsd->nd_nam;
228 caddr_t dpos = nfsd->nd_dpos;
229 struct ucred *cred = &nfsd->nd_cr;
230 struct nfs_fattr *fp;
232 struct vattr *vap = &va;
233 struct vnode *vp = NULL;
239 int error = 0, rdonly, cache;
241 struct mbuf *mb, *mb2, *mreq;
244 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
245 fhp = &nfh.fh_generic;
247 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
248 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
254 nqsrv_getl(vp, ND_READ);
255 error = VOP_GETATTR(vp, vap, td);
258 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
263 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
264 nfsm_srvfillattr(vap, fp);
274 * nfs setattr service
277 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
278 struct thread *td, struct mbuf **mrq)
280 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
281 struct sockaddr *nam = nfsd->nd_nam;
282 caddr_t dpos = nfsd->nd_dpos;
283 struct ucred *cred = &nfsd->nd_cr;
284 struct vattr va, preat;
285 struct vattr *vap = &va;
286 struct nfsv2_sattr *sp;
287 struct nfs_fattr *fp;
288 struct vnode *vp = NULL;
294 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
295 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
297 struct mbuf *mb, *mb2, *mreq;
299 struct timespec guard;
301 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
302 fhp = &nfh.fh_generic;
307 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
308 gcheck = fxdr_unsigned(int, *tl);
310 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
311 fxdr_nfsv3time(tl, &guard);
314 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
316 * Nah nah nah nah na nah
317 * There is a bug in the Sun client that puts 0xffff in the mode
318 * field of sattr when it should put in 0xffffffff. The u_short
319 * doesn't sign extend.
320 * --> check the low order 2 bytes for 0xffff
322 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
323 vap->va_mode = nfstov_mode(sp->sa_mode);
324 if (sp->sa_uid != nfs_xdrneg1)
325 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
326 if (sp->sa_gid != nfs_xdrneg1)
327 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
328 if (sp->sa_size != nfs_xdrneg1)
329 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
330 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
332 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
334 vap->va_atime.tv_sec =
335 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
336 vap->va_atime.tv_nsec = 0;
339 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
340 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
345 * Now that we have all the fields, lets do it.
347 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
348 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
350 nfsm_reply(2 * NFSX_UNSIGNED);
351 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
357 * vp now an active resource, pay careful attention to cleanup
360 nqsrv_getl(vp, ND_WRITE);
362 error = preat_ret = VOP_GETATTR(vp, &preat, td);
363 if (!error && gcheck &&
364 (preat.va_ctime.tv_sec != guard.tv_sec ||
365 preat.va_ctime.tv_nsec != guard.tv_nsec))
366 error = NFSERR_NOT_SYNC;
370 nfsm_reply(NFSX_WCCDATA(v3));
371 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
378 * If the size is being changed write acces is required, otherwise
379 * just check for a read only file system.
381 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
382 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
387 if (vp->v_type == VDIR) {
390 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
395 error = VOP_SETATTR(vp, vap, cred, td);
396 postat_ret = VOP_GETATTR(vp, vap, td);
402 nfsm_reply(NFSX_WCCORFATTR(v3));
404 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
408 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
409 nfsm_srvfillattr(vap, fp);
423 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
424 struct thread *td, struct mbuf **mrq)
426 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
427 struct sockaddr *nam = nfsd->nd_nam;
428 caddr_t dpos = nfsd->nd_dpos;
429 struct ucred *cred = &nfsd->nd_cr;
430 struct nfs_fattr *fp;
431 struct nlookupdata nd;
434 struct namecache *ncp;
441 int error = 0, cache = 0, len, dirattr_ret = 1;
442 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
444 struct mbuf *mb, *mb2, *mreq;
445 struct vattr va, dirattr, *vap = &va;
448 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
453 fhp = &nfh.fh_generic;
455 nfsm_srvnamesiz(len);
457 pubflag = nfs_ispublicfh(fhp);
459 error = nfs_namei(&nd, cred, NAMEI_LOOKUP, NULL, &vp,
460 fhp, len, slp, nam, &md, &dpos,
461 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
464 * namei failure, only dirp to cleanup. Clear out garbarge from
465 * structure in case macros jump to nfsmout.
471 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
475 nfsm_reply(NFSX_POSTOPATTR(v3));
476 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
482 * Locate index file for public filehandle
484 * error is 0 on entry and 0 on exit from this block.
488 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
490 * Setup call to lookup() to see if we can find
491 * the index file. Arguably, this doesn't belong
492 * in a kernel.. Ugh. If an error occurs, do not
493 * try to install an index file and then clear the
496 * When we replace nd with ind and redirect ndp,
497 * maintenance of ni_startdir and ni_vp shift to
498 * ind and we have to clean them up in the old nd.
499 * However, the cnd resource continues to be maintained
500 * via the original nd. Confused? You aren't alone!
502 VOP_UNLOCK(vp, 0, td);
503 ncp = cache_hold(nd.nl_ncp);
505 error = nlookup_init_raw(&nd, nfs_pub.np_index,
506 UIO_SYSSPACE, 0, cred, ncp);
509 error = nlookup(&nd);
513 * Found an index file. Get rid of
514 * the old references. transfer vp and
515 * load up the new vp. Fortunately we do
516 * not have to deal with dvp, that would be
523 error = cache_vget(nd.nl_ncp, nd.nl_cred,
525 KKASSERT(error == 0);
530 * If the public filehandle was used, check that this lookup
531 * didn't result in a filehandle outside the publicly exported
532 * filesystem. We clear the poor vp here to avoid lockups due
536 if (vp->v_mount != nfs_pub.np_mount) {
545 dirattr_ret = VOP_GETATTR(dirp, &dirattr, td);
551 * Resources at this point:
552 * ndp->ni_vp may not be NULL
557 nfsm_reply(NFSX_POSTOPATTR(v3));
558 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
564 /* XXX not sure how to deal with this */
565 nqsrv_getl(ndp->ni_startdir, ND_READ);
569 * Clear out some resources prior to potentially blocking. This
570 * is not as critical as ni_dvp resources in other routines, but
576 * Get underlying attribute, then release remaining resources ( for
577 * the same potential blocking reason ) and reply.
579 bzero((caddr_t)fhp, sizeof(nfh));
580 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
581 error = VFS_VPTOFH(vp, &fhp->fh_fid);
583 error = VOP_GETATTR(vp, vap, td);
587 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
589 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
593 nfsm_srvfhtom(fhp, v3);
595 nfsm_srvpostop_attr(0, vap);
596 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
598 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
599 nfsm_srvfillattr(vap, fp);
605 nlookup_done(&nd); /* may be called twice */
612 * nfs readlink service
615 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
616 struct thread *td, struct mbuf **mrq)
618 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
619 struct sockaddr *nam = nfsd->nd_nam;
620 caddr_t dpos = nfsd->nd_dpos;
621 struct ucred *cred = &nfsd->nd_cr;
622 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
623 struct iovec *ivp = iv;
628 int error = 0, rdonly, cache, i, tlen, len, getret;
629 int v3 = (nfsd->nd_flag & ND_NFSV3);
631 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
632 struct vnode *vp = NULL;
636 struct uio io, *uiop = &io;
639 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
641 mp2 = (struct mbuf *)0;
644 fhp = &nfh.fh_generic;
648 while (len < NFS_MAXPATHLEN) {
649 mp = m_getcl(MB_WAIT, MT_DATA, 0);
650 mp->m_len = MCLBYTES;
657 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
658 mp->m_len = NFS_MAXPATHLEN-len;
659 len = NFS_MAXPATHLEN;
662 ivp->iov_base = mtod(mp, caddr_t);
663 ivp->iov_len = mp->m_len;
668 uiop->uio_iovcnt = i;
669 uiop->uio_offset = 0;
670 uiop->uio_resid = len;
671 uiop->uio_rw = UIO_READ;
672 uiop->uio_segflg = UIO_SYSSPACE;
674 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
675 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
677 nfsm_reply(2 * NFSX_UNSIGNED);
678 nfsm_srvpostop_attr(1, (struct vattr *)0);
682 if (vp->v_type != VLNK) {
689 nqsrv_getl(vp, ND_READ);
690 error = VOP_READLINK(vp, uiop, cred);
692 getret = VOP_GETATTR(vp, &attr, td);
695 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
697 nfsm_srvpostop_attr(getret, &attr);
703 if (uiop->uio_resid > 0) {
704 len -= uiop->uio_resid;
705 tlen = nfsm_rndup(len);
706 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
708 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
709 *tl = txdr_unsigned(len);
724 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
725 struct thread *td, struct mbuf **mrq)
727 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
728 struct sockaddr *nam = nfsd->nd_nam;
729 caddr_t dpos = nfsd->nd_dpos;
730 struct ucred *cred = &nfsd->nd_cr;
734 struct nfs_fattr *fp;
739 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
740 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
742 struct mbuf *mb, *mb2, *mreq;
744 struct vnode *vp = NULL;
747 struct uio io, *uiop = &io;
748 struct vattr va, *vap = &va;
754 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
755 fhp = &nfh.fh_generic;
758 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
759 off = fxdr_hyper(tl);
761 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
762 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
764 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
767 * Reference vp. If an error occurs, vp will be invalid, but we
768 * have to NULL it just in case. The macros might goto nfsmout
772 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
773 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
776 nfsm_reply(2 * NFSX_UNSIGNED);
777 nfsm_srvpostop_attr(1, (struct vattr *)0);
782 if (vp->v_type != VREG) {
786 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
789 nqsrv_getl(vp, ND_READ);
790 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
791 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
793 getret = VOP_GETATTR(vp, vap, td);
799 nfsm_reply(NFSX_POSTOPATTR(v3));
800 nfsm_srvpostop_attr(getret, vap);
806 * Calculate byte count to read
809 if (off >= vap->va_size)
811 else if ((off + reqlen) > vap->va_size)
812 cnt = vap->va_size - off;
817 * Calculate seqcount for heuristic
825 * Locate best candidate
828 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
832 if (nfsheur[hi].nh_vp == vp) {
836 if (nfsheur[hi].nh_use > 0)
837 --nfsheur[hi].nh_use;
838 hi = (hi + 1) % NUM_HEURISTIC;
839 if (nfsheur[hi].nh_use < nh->nh_use)
843 if (nh->nh_vp != vp) {
846 nh->nh_use = NHUSE_INIT;
854 * Calculate heuristic
857 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
858 if (++nh->nh_seqcount > IO_SEQMAX)
859 nh->nh_seqcount = IO_SEQMAX;
860 } else if (nh->nh_seqcount > 1) {
865 nh->nh_use += NHUSE_INC;
866 if (nh->nh_use > NHUSE_MAX)
867 nh->nh_use = NHUSE_MAX;
868 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
871 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
873 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
875 fp = (struct nfs_fattr *)tl;
876 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
878 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
879 fp = (struct nfs_fattr *)tl;
880 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
882 len = left = nfsm_rndup(cnt);
885 * Generate the mbuf list with the uio_iov ref. to it.
890 siz = min(M_TRAILINGSPACE(m), left);
896 m = m_getcl(MB_WAIT, MT_DATA, 0);
902 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
904 uiop->uio_iov = iv2 = iv;
910 panic("nfsrv_read iov");
911 siz = min(M_TRAILINGSPACE(m), left);
913 iv->iov_base = mtod(m, caddr_t) + m->m_len;
922 uiop->uio_iovcnt = i;
923 uiop->uio_offset = off;
924 uiop->uio_resid = len;
925 uiop->uio_rw = UIO_READ;
926 uiop->uio_segflg = UIO_SYSSPACE;
927 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
928 off = uiop->uio_offset;
930 FREE((caddr_t)iv2, M_TEMP);
931 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
937 nfsm_reply(NFSX_POSTOPATTR(v3));
938 nfsm_srvpostop_attr(getret, vap);
947 nfsm_srvfillattr(vap, fp);
948 tlen = len - uiop->uio_resid;
949 cnt = cnt < tlen ? cnt : tlen;
950 tlen = nfsm_rndup(cnt);
951 if (len != tlen || tlen != cnt)
952 nfsm_adj(mb, len - tlen, tlen - cnt);
954 *tl++ = txdr_unsigned(cnt);
960 *tl = txdr_unsigned(cnt);
971 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
972 struct thread *td, struct mbuf **mrq)
974 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
975 struct sockaddr *nam = nfsd->nd_nam;
976 caddr_t dpos = nfsd->nd_dpos;
977 struct ucred *cred = &nfsd->nd_cr;
981 struct nfs_fattr *fp;
983 struct vattr va, forat;
984 struct vattr *vap = &va;
988 int error = 0, rdonly, cache, len, forat_ret = 1;
989 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
990 int stable = NFSV3WRITE_FILESYNC;
991 int v3 = (nfsd->nd_flag & ND_NFSV3);
993 struct mbuf *mb, *mb2, *mreq;
994 struct vnode *vp = NULL;
997 struct uio io, *uiop = &io;
1001 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1007 fhp = &nfh.fh_generic;
1010 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1011 off = fxdr_hyper(tl);
1013 stable = fxdr_unsigned(int, *tl++);
1015 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1016 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1019 stable = NFSV3WRITE_UNSTABLE;
1021 retlen = len = fxdr_unsigned(int32_t, *tl);
1025 * For NFS Version 2, it is not obvious what a write of zero length
1026 * should do, but I might as well be consistent with Version 3,
1027 * which is to return ok so long as there are no permission problems.
1035 adjust = dpos - mtod(mp, caddr_t);
1036 mp->m_len -= adjust;
1037 if (mp->m_len > 0 && adjust > 0)
1038 NFSMADV(mp, adjust);
1042 else if (mp->m_len > 0) {
1045 mp->m_len -= (i - len);
1054 if (len > NFS_MAXDATA || len < 0 || i < len) {
1056 nfsm_reply(2 * NFSX_UNSIGNED);
1057 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1061 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1062 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1065 nfsm_reply(2 * NFSX_UNSIGNED);
1066 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1071 forat_ret = VOP_GETATTR(vp, &forat, td);
1072 if (vp->v_type != VREG) {
1076 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1079 nqsrv_getl(vp, ND_WRITE);
1080 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1085 nfsm_reply(NFSX_WCCDATA(v3));
1086 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1092 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1094 uiop->uio_iov = iv = ivp;
1095 uiop->uio_iovcnt = cnt;
1098 if (mp->m_len > 0) {
1099 ivp->iov_base = mtod(mp, caddr_t);
1100 ivp->iov_len = mp->m_len;
1108 * The IO_METASYNC flag indicates that all metadata (and not just
1109 * enough to ensure data integrity) mus be written to stable storage
1111 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1113 if (stable == NFSV3WRITE_UNSTABLE)
1114 ioflags = IO_NODELOCKED;
1115 else if (stable == NFSV3WRITE_DATASYNC)
1116 ioflags = (IO_SYNC | IO_NODELOCKED);
1118 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1119 uiop->uio_resid = len;
1120 uiop->uio_rw = UIO_WRITE;
1121 uiop->uio_segflg = UIO_SYSSPACE;
1122 uiop->uio_td = NULL;
1123 uiop->uio_offset = off;
1124 error = VOP_WRITE(vp, uiop, ioflags, cred);
1125 nfsstats.srvvop_writes++;
1126 FREE((caddr_t)iv, M_TEMP);
1128 aftat_ret = VOP_GETATTR(vp, vap, td);
1133 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1134 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1136 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1141 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1142 *tl++ = txdr_unsigned(retlen);
1144 * If nfs_async is set, then pretend the write was FILESYNC.
1146 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1147 *tl++ = txdr_unsigned(stable);
1149 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1151 * Actually, there is no need to txdr these fields,
1152 * but it may make the values more human readable,
1153 * for debugging purposes.
1155 if (nfsver.tv_sec == 0)
1157 *tl++ = txdr_unsigned(nfsver.tv_sec);
1158 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1160 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1161 nfsm_srvfillattr(vap, fp);
1170 * NFS write service with write gathering support. Called when
1171 * nfsrvw_procrastinate > 0.
1172 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1173 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1177 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1178 struct thread *td, struct mbuf **mrq)
1182 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1183 struct nfs_fattr *fp;
1186 struct nfsrvw_delayhash *wpp;
1188 struct vattr va, forat;
1192 int error = 0, rdonly, cache, len, forat_ret = 1;
1193 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1195 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1196 struct vnode *vp = NULL;
1197 struct uio io, *uiop = &io;
1198 u_quad_t frev, cur_usec;
1200 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1209 mrep = nfsd->nd_mrep;
1211 dpos = nfsd->nd_dpos;
1212 cred = &nfsd->nd_cr;
1213 v3 = (nfsd->nd_flag & ND_NFSV3);
1214 LIST_INIT(&nfsd->nd_coalesce);
1215 nfsd->nd_mreq = NULL;
1216 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1217 cur_usec = nfs_curusec();
1218 nfsd->nd_time = cur_usec +
1219 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1222 * Now, get the write header..
1224 nfsm_srvmtofh(&nfsd->nd_fh);
1226 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1227 nfsd->nd_off = fxdr_hyper(tl);
1229 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1231 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1232 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1235 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1237 len = fxdr_unsigned(int32_t, *tl);
1239 nfsd->nd_eoff = nfsd->nd_off + len;
1242 * Trim the header out of the mbuf list and trim off any trailing
1243 * junk so that the mbuf list has only the write data.
1251 adjust = dpos - mtod(mp, caddr_t);
1252 mp->m_len -= adjust;
1253 if (mp->m_len > 0 && adjust > 0)
1254 NFSMADV(mp, adjust);
1261 mp->m_len -= (i - len);
1267 if (len > NFS_MAXDATA || len < 0 || i < len) {
1271 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1273 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1274 nfsd->nd_mreq = mreq;
1275 nfsd->nd_mrep = NULL;
1280 * Add this entry to the hash and time queues.
1284 wp = slp->ns_tq.lh_first;
1285 while (wp && wp->nd_time < nfsd->nd_time) {
1287 wp = wp->nd_tq.le_next;
1289 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1291 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1293 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1295 if (nfsd->nd_mrep) {
1296 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1300 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1302 wp = wp->nd_hash.le_next;
1304 while (wp && wp->nd_off < nfsd->nd_off &&
1305 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1307 wp = wp->nd_hash.le_next;
1310 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1313 * Search the hash list for overlapping entries and
1316 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1317 wp = nfsd->nd_hash.le_next;
1318 if (NFSW_SAMECRED(owp, nfsd))
1319 nfsrvw_coalesce(owp, nfsd);
1322 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1329 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1330 * and generate the associated reply mbuf list(s).
1333 cur_usec = nfs_curusec();
1335 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1336 owp = nfsd->nd_tq.le_next;
1337 if (nfsd->nd_time > cur_usec)
1341 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1342 LIST_REMOVE(nfsd, nd_tq);
1343 LIST_REMOVE(nfsd, nd_hash);
1345 mrep = nfsd->nd_mrep;
1346 nfsd->nd_mrep = NULL;
1347 cred = &nfsd->nd_cr;
1348 v3 = (nfsd->nd_flag & ND_NFSV3);
1349 forat_ret = aftat_ret = 1;
1350 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1351 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1354 forat_ret = VOP_GETATTR(vp, &forat, td);
1355 if (vp->v_type != VREG) {
1359 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1365 nqsrv_getl(vp, ND_WRITE);
1366 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1369 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1370 ioflags = IO_NODELOCKED;
1371 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1372 ioflags = (IO_SYNC | IO_NODELOCKED);
1374 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1375 uiop->uio_rw = UIO_WRITE;
1376 uiop->uio_segflg = UIO_SYSSPACE;
1377 uiop->uio_td = NULL;
1378 uiop->uio_offset = nfsd->nd_off;
1379 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1380 if (uiop->uio_resid > 0) {
1388 uiop->uio_iovcnt = i;
1389 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1391 uiop->uio_iov = ivp = iov;
1394 if (mp->m_len > 0) {
1395 ivp->iov_base = mtod(mp, caddr_t);
1396 ivp->iov_len = mp->m_len;
1402 error = VOP_WRITE(vp, uiop, ioflags, cred);
1403 nfsstats.srvvop_writes++;
1405 FREE((caddr_t)iov, M_TEMP);
1409 aftat_ret = VOP_GETATTR(vp, &va, td);
1415 * Loop around generating replies for all write rpcs that have
1416 * now been completed.
1420 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1422 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1424 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1427 nfsm_writereply(NFSX_PREOPATTR(v3) +
1428 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1429 NFSX_WRITEVERF(v3), v3);
1431 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1432 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1433 *tl++ = txdr_unsigned(nfsd->nd_len);
1434 *tl++ = txdr_unsigned(swp->nd_stable);
1436 * Actually, there is no need to txdr these fields,
1437 * but it may make the values more human readable,
1438 * for debugging purposes.
1440 if (nfsver.tv_sec == 0)
1442 *tl++ = txdr_unsigned(nfsver.tv_sec);
1443 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1445 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1446 nfsm_srvfillattr(&va, fp);
1449 nfsd->nd_mreq = mreq;
1451 panic("nfsrv_write: nd_mrep not free");
1454 * Done. Put it at the head of the timer queue so that
1455 * the final phase can return the reply.
1460 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1462 nfsd = swp->nd_coalesce.lh_first;
1464 LIST_REMOVE(nfsd, nd_tq);
1470 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1477 * Search for a reply to return.
1480 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1481 if (nfsd->nd_mreq) {
1482 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1483 LIST_REMOVE(nfsd, nd_tq);
1484 *mrq = nfsd->nd_mreq;
1493 * Coalesce the write request nfsd into owp. To do this we must:
1494 * - remove nfsd from the queues
1495 * - merge nfsd->nd_mrep into owp->nd_mrep
1496 * - update the nd_eoff and nd_stable for owp
1497 * - put nfsd on owp's nd_coalesce list
1498 * NB: Must be called at splsoftclock().
1501 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1505 struct nfsrv_descript *p;
1507 NFS_DPF(WG, ("C%03x-%03x",
1508 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1509 LIST_REMOVE(nfsd, nd_hash);
1510 LIST_REMOVE(nfsd, nd_tq);
1511 if (owp->nd_eoff < nfsd->nd_eoff) {
1512 overlap = owp->nd_eoff - nfsd->nd_off;
1514 panic("nfsrv_coalesce: bad off");
1516 m_adj(nfsd->nd_mrep, overlap);
1520 mp->m_next = nfsd->nd_mrep;
1521 owp->nd_eoff = nfsd->nd_eoff;
1523 m_freem(nfsd->nd_mrep);
1524 nfsd->nd_mrep = NULL;
1525 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1526 owp->nd_stable = NFSV3WRITE_FILESYNC;
1527 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1528 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1529 owp->nd_stable = NFSV3WRITE_DATASYNC;
1530 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1533 * If nfsd had anything else coalesced into it, transfer them
1534 * to owp, otherwise their replies will never get sent.
1536 for (p = nfsd->nd_coalesce.lh_first; p;
1537 p = nfsd->nd_coalesce.lh_first) {
1538 LIST_REMOVE(p, nd_tq);
1539 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1544 * nfs create service
1545 * now does a truncate to 0 length via. setattr if it already exists
1548 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1549 struct thread *td, struct mbuf **mrq)
1551 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1552 struct sockaddr *nam = nfsd->nd_nam;
1553 caddr_t dpos = nfsd->nd_dpos;
1554 struct ucred *cred = &nfsd->nd_cr;
1555 struct nfs_fattr *fp;
1556 struct vattr va, dirfor, diraft;
1557 struct vattr *vap = &va;
1558 struct nfsv2_sattr *sp;
1560 struct nlookupdata nd;
1563 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1564 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1567 struct mbuf *mb, *mb2, *mreq;
1573 u_quad_t frev, tempsize;
1574 u_char cverf[NFSX_V3CREATEVERF];
1576 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1585 fhp = &nfh.fh_generic;
1587 nfsm_srvnamesiz(len);
1590 * Call namei and do initial cleanup to get a few things
1591 * out of the way. If we get an initial error we cleanup
1592 * and return here to avoid special-casing the invalid nd
1593 * structure through the rest of the case. dirp may be
1594 * set even if an error occurs, but the nd structure will not
1595 * be valid at all if an error occurs so we have to invalidate it
1596 * prior to calling nfsm_reply ( which might goto nfsmout ).
1598 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1599 fhp, len, slp, nam, &md, &dpos, &dirp,
1600 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1603 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1610 nfsm_reply(NFSX_WCCDATA(v3));
1611 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1617 * No error. Continue. State:
1620 * vp may be valid or NULL if the target does not
1624 * The error state is set through the code and we may also do some
1625 * opportunistic releasing of vnodes to avoid holding locks through
1626 * NFS I/O. The cleanup at the end is a catch-all
1631 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1632 how = fxdr_unsigned(int, *tl);
1634 case NFSV3CREATE_GUARDED:
1640 case NFSV3CREATE_UNCHECKED:
1643 case NFSV3CREATE_EXCLUSIVE:
1644 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1645 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1649 vap->va_type = VREG;
1651 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1652 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1653 if (vap->va_type == VNON)
1654 vap->va_type = VREG;
1655 vap->va_mode = nfstov_mode(sp->sa_mode);
1656 switch (vap->va_type) {
1658 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1660 vap->va_size = (u_quad_t)tsize;
1665 rdev = fxdr_unsigned(long, sp->sa_size);
1673 * Iff doesn't exist, create it
1674 * otherwise just truncate to 0 length
1675 * should I set the mode too ?
1677 * The only possible error we can have at this point is EEXIST.
1678 * nd.ni_vp will also be non-NULL in that case.
1681 if (vap->va_mode == (mode_t)VNOVAL)
1683 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1684 nqsrv_getl(dvp, ND_WRITE);
1685 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1687 nfsrv_object_create(vp);
1688 if (exclusive_flag) {
1691 bcopy(cverf, (caddr_t)&vap->va_atime,
1693 error = VOP_SETATTR(vp, vap, cred, td);
1697 vap->va_type == VCHR ||
1698 vap->va_type == VBLK ||
1699 vap->va_type == VFIFO
1702 * Handle SysV FIFO node special cases. All other
1703 * devices require super user to access.
1705 if (vap->va_type == VCHR && rdev == 0xffffffff)
1706 vap->va_type = VFIFO;
1707 if (vap->va_type != VFIFO &&
1708 (error = suser_cred(cred, 0))) {
1711 vap->va_rdev = rdev;
1712 nqsrv_getl(dvp, ND_WRITE);
1714 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1719 * XXX what is this junk supposed to do ?
1726 * release dvp prior to lookup
1734 * Even though LOCKPARENT was cleared, ni_dvp may
1737 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1738 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1739 nd.ni_cnd.cn_td = td;
1740 nd.ni_cnd.cn_cred = cred;
1742 error = lookup(&nd);
1747 /* fall through on certain errors */
1749 nfsrv_object_create(nd.ni_vp);
1750 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1759 if (vap->va_size != -1) {
1760 error = nfsrv_access(vp, VWRITE, cred,
1761 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1763 nqsrv_getl(vp, ND_WRITE);
1764 tempsize = vap->va_size;
1766 vap->va_size = tempsize;
1767 error = VOP_SETATTR(vp, vap, cred, td);
1773 bzero((caddr_t)fhp, sizeof(nfh));
1774 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1775 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1777 error = VOP_GETATTR(vp, vap, td);
1780 if (exclusive_flag && !error &&
1781 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1783 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1787 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1790 nfsm_srvpostop_fh(fhp);
1791 nfsm_srvpostop_attr(0, vap);
1793 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1796 nfsm_srvfhtom(fhp, v3);
1797 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1798 nfsm_srvfillattr(vap, fp);
1823 * nfs v3 mknod service
1826 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1827 struct thread *td, struct mbuf **mrq)
1829 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1830 struct sockaddr *nam = nfsd->nd_nam;
1831 caddr_t dpos = nfsd->nd_dpos;
1832 struct ucred *cred = &nfsd->nd_cr;
1833 struct vattr va, dirfor, diraft;
1834 struct vattr *vap = &va;
1836 struct nlookupdata nd;
1839 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1840 u_int32_t major, minor;
1843 struct mbuf *mb, *mb2, *mreq;
1851 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1857 fhp = &nfh.fh_generic;
1859 nfsm_srvnamesiz(len);
1862 * Handle nfs_namei() call. If an error occurs, the nd structure
1863 * is not valid. However, nfsm_*() routines may still jump to
1867 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1868 fhp, len, slp, nam, &md, &dpos, &dirp,
1869 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1871 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1873 nfsm_reply(NFSX_WCCDATA(1));
1874 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1878 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1879 vtyp = nfsv3tov_type(*tl);
1880 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1881 error = NFSERR_BADTYPE;
1886 if (vtyp == VCHR || vtyp == VBLK) {
1887 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1888 major = fxdr_unsigned(u_int32_t, *tl++);
1889 minor = fxdr_unsigned(u_int32_t, *tl);
1890 vap->va_rdev = makeudev(major, minor);
1894 * Iff doesn't exist, create it.
1900 vap->va_type = vtyp;
1901 if (vap->va_mode == (mode_t)VNOVAL)
1903 if (vtyp == VSOCK) {
1904 nqsrv_getl(dvp, ND_WRITE);
1905 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1907 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1909 nqsrv_getl(dvp, ND_WRITE);
1911 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1920 * Release dvp prior to lookup
1926 * XXX what is this stuff for?
1928 KKASSERT(td->td_proc);
1929 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1930 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1931 nd.ni_cnd.cn_td = td;
1932 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1934 error = lookup(&nd);
1939 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1945 * send response, cleanup, return.
1957 bzero((caddr_t)fhp, sizeof(nfh));
1958 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1959 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1961 error = VOP_GETATTR(vp, vap, td);
1967 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1972 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1974 nfsm_srvpostop_fh(fhp);
1975 nfsm_srvpostop_attr(0, vap);
1977 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1995 * nfs remove service
1998 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1999 struct thread *td, struct mbuf **mrq)
2001 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2002 struct sockaddr *nam = nfsd->nd_nam;
2003 caddr_t dpos = nfsd->nd_dpos;
2004 struct ucred *cred = &nfsd->nd_cr;
2005 struct nlookupdata nd;
2009 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2010 int v3 = (nfsd->nd_flag & ND_NFSV3);
2012 struct mbuf *mb, *mreq;
2016 struct vattr dirfor, diraft;
2021 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2027 fhp = &nfh.fh_generic;
2029 nfsm_srvnamesiz(len);
2031 error = nfs_namei(&nd, cred, NAMEI_DELETE, &dvp, &vp,
2032 fhp, len, slp, nam, &md, &dpos, &dirp,
2033 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2036 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2039 if (vp->v_type == VDIR) {
2040 error = EPERM; /* POSIX */
2044 * The root of a mounted filesystem cannot be deleted.
2046 if (vp->v_flag & VROOT) {
2052 nqsrv_getl(dvp, ND_WRITE);
2053 nqsrv_getl(vp, ND_WRITE);
2054 error = VOP_NREMOVE(nd.nl_ncp, nd.nl_cred);
2058 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2059 nfsm_reply(NFSX_WCCDATA(v3));
2061 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2080 * nfs rename service
2083 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2084 struct thread *td, struct mbuf **mrq)
2086 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2087 struct sockaddr *nam = nfsd->nd_nam;
2088 caddr_t dpos = nfsd->nd_dpos;
2089 struct ucred *cred = &nfsd->nd_cr;
2093 int error = 0, cache = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2094 int tdirfor_ret = 1, tdiraft_ret = 1;
2095 int v3 = (nfsd->nd_flag & ND_NFSV3);
2097 struct mbuf *mb, *mreq;
2098 struct nlookupdata fromnd, tond;
2099 struct vnode *fvp, *fdirp;
2100 struct vnode *tvp, *tdirp;
2101 struct namecache *ncp;
2102 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2104 fhandle_t *ffhp, *tfhp;
2108 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2110 fvp = (struct vnode *)0;
2112 ffhp = &fnfh.fh_generic;
2113 tfhp = &tnfh.fh_generic;
2116 * Clear fields incase goto nfsmout occurs from macro.
2119 nlookup_zero(&fromnd);
2120 nlookup_zero(&tond);
2124 nfsm_srvmtofh(ffhp);
2125 nfsm_srvnamesiz(len);
2127 * Remember our original uid so that we can reset cr_uid before
2128 * the second nfs_namei() call, in case it is remapped.
2130 saved_uid = cred->cr_uid;
2131 error = nfs_namei(&fromnd, cred, NAMEI_DELETE, NULL, NULL,
2132 ffhp, len, slp, nam, &md, &dpos, &fdirp,
2133 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2136 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2139 nfsm_reply(2 * NFSX_WCCDATA(v3));
2140 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2141 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2147 * We have to unlock the from ncp before we can safely lookup
2150 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2151 cache_unlock(fromnd.nl_ncp);
2152 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2153 nfsm_srvmtofh(tfhp);
2154 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2155 cred->cr_uid = saved_uid;
2157 error = nfs_namei(&tond, cred, NAMEI_RENAME, NULL, NULL,
2158 tfhp, len2, slp, nam, &md, &dpos, &tdirp,
2159 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2162 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2170 if (cache_lock_nonblock(fromnd.nl_ncp) == 0) {
2171 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2172 } else if (fromnd.nl_ncp > tond.nl_ncp) {
2173 cache_lock(fromnd.nl_ncp);
2174 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2176 cache_unlock(tond.nl_ncp);
2177 cache_lock(fromnd.nl_ncp);
2178 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2179 cache_lock(tond.nl_ncp);
2180 cache_resolve(tond.nl_ncp, tond.nl_cred);
2182 fromnd.nl_flags |= NLC_NCPISLOCKED;
2184 tvp = tond.nl_ncp->nc_vp;
2185 fvp = fromnd.nl_ncp->nc_vp;
2188 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2194 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2201 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2209 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2216 if (fromnd.nl_ncp->nc_mount != tond.nl_ncp->nc_mount) {
2223 if (fromnd.nl_ncp == tond.nl_ncp->nc_parent) {
2231 * You cannot rename a source into itself or a subdirectory of itself.
2232 * We check this by travsering the target directory upwards looking
2233 * for a match against the source.
2236 for (ncp = tond.nl_ncp; ncp; ncp = ncp->nc_parent) {
2237 if (fromnd.nl_ncp == ncp) {
2245 * If source is the same as the destination (that is the
2246 * same vnode with the same name in the same directory),
2247 * then there is nothing to do.
2249 if (fromnd.nl_ncp == tond.nl_ncp)
2254 * The VOP_NRENAME function releases all vnode references &
2255 * locks prior to returning so we need to clear the pointers
2256 * to bypass cleanup code later on.
2259 /* XXX should be handled by NRENAME */
2260 nqsrv_getl(fdvp, ND_WRITE);
2261 nqsrv_getl(tdvp, ND_WRITE);
2263 nqsrv_getl(tvp, ND_WRITE);
2266 error = VOP_NRENAME(fromnd.nl_ncp, tond.nl_ncp, tond.nl_cred);
2275 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2277 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2278 nfsm_reply(2 * NFSX_WCCDATA(v3));
2280 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2281 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2289 nlookup_done(&tond);
2292 nlookup_done(&fromnd);
2300 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2301 struct thread *td, struct mbuf **mrq)
2303 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2304 struct sockaddr *nam = nfsd->nd_nam;
2305 caddr_t dpos = nfsd->nd_dpos;
2306 struct ucred *cred = &nfsd->nd_cr;
2307 struct nlookupdata nd;
2311 int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2312 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2314 struct mbuf *mb, *mreq;
2319 struct vattr dirfor, diraft, at;
2321 fhandle_t *fhp, *dfhp;
2324 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2326 dirp = dvp = vp = xp = NULL;
2328 fhp = &nfh.fh_generic;
2329 dfhp = &dnfh.fh_generic;
2331 nfsm_srvmtofh(dfhp);
2332 nfsm_srvnamesiz(len);
2334 error = nfsrv_fhtovp(fhp, FALSE, &xp, cred, slp, nam,
2335 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2337 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2338 nfsm_srvpostop_attr(getret, &at);
2339 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2344 if (xp->v_type == VDIR) {
2345 error = EPERM; /* POSIX */
2349 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
2350 dfhp, len, slp, nam, &md, &dpos, &dirp,
2351 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2354 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2363 if (xp->v_mount != dvp->v_mount)
2368 nqsrv_getl(xp, ND_WRITE);
2369 nqsrv_getl(vp, ND_WRITE);
2371 error = VOP_NLINK(nd.nl_ncp, xp, nd.nl_cred);
2377 getret = VOP_GETATTR(xp, &at, td);
2379 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2380 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2382 nfsm_srvpostop_attr(getret, &at);
2383 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2406 * nfs symbolic link service
2409 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2410 struct thread *td, struct mbuf **mrq)
2412 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2413 struct sockaddr *nam = nfsd->nd_nam;
2414 caddr_t dpos = nfsd->nd_dpos;
2415 struct ucred *cred = &nfsd->nd_cr;
2416 struct vattr va, dirfor, diraft;
2417 struct nlookupdata nd;
2418 struct vattr *vap = &va;
2421 struct nfsv2_sattr *sp;
2422 char *bpos, *pathcp = (char *)0, *cp2;
2425 int error = 0, cache = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2426 int v3 = (nfsd->nd_flag & ND_NFSV3);
2427 struct mbuf *mb, *mreq, *mb2;
2434 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2438 fhp = &nfh.fh_generic;
2440 nfsm_srvnamesiz(len);
2442 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2443 fhp, len, slp, nam, &md, &dpos, &dirp,
2444 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2447 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2455 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2456 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2457 iv.iov_base = pathcp;
2459 io.uio_resid = len2;
2463 io.uio_segflg = UIO_SYSSPACE;
2464 io.uio_rw = UIO_READ;
2466 nfsm_mtouio(&io, len2);
2468 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2469 vap->va_mode = nfstov_mode(sp->sa_mode);
2471 *(pathcp + len2) = '\0';
2477 if (vap->va_mode == (mode_t)VNOVAL)
2480 nqsrv_getl(dvp, ND_WRITE);
2482 error = VOP_NSYMLINK(nd.nl_ncp, &vp, nd.nl_cred, vap, pathcp);
2484 bzero((caddr_t)fhp, sizeof(nfh));
2485 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2486 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2488 error = VOP_GETATTR(vp, vap, td);
2493 * We have a vp in hand from the new API call, we do not have to
2499 * Issue lookup. Leave SAVESTART set so we can easily free
2500 * the name buffer later on.
2502 * since LOCKPARENT is not set, ni_dvp will be garbage on
2503 * return whether an error occurs or not.
2505 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2506 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2507 nd.ni_cnd.cn_td = td;
2508 nd.ni_cnd.cn_cred = cred;
2510 error = lookup(&nd);
2514 bzero((caddr_t)fhp, sizeof(nfh));
2515 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2516 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2518 error = VOP_GETATTR(nd.ni_vp, vap, td);
2531 FREE(pathcp, M_TEMP);
2535 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2539 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2542 nfsm_srvpostop_fh(fhp);
2543 nfsm_srvpostop_attr(0, vap);
2545 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2557 FREE(pathcp, M_TEMP);
2565 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2566 struct thread *td, struct mbuf **mrq)
2568 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2569 struct sockaddr *nam = nfsd->nd_nam;
2570 caddr_t dpos = nfsd->nd_dpos;
2571 struct ucred *cred = &nfsd->nd_cr;
2572 struct vattr va, dirfor, diraft;
2573 struct vattr *vap = &va;
2574 struct nfs_fattr *fp;
2575 struct nlookupdata nd;
2580 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2581 int v3 = (nfsd->nd_flag & ND_NFSV3);
2583 struct mbuf *mb, *mb2, *mreq;
2590 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2595 fhp = &nfh.fh_generic;
2597 nfsm_srvnamesiz(len);
2599 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2600 fhp, len, slp, nam, &md, &dpos, &dirp,
2601 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2604 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2607 nfsm_reply(NFSX_WCCDATA(v3));
2608 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2616 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2617 vap->va_mode = nfstov_mode(*tl++);
2621 * At this point nd.ni_dvp is referenced and exclusively locked and
2622 * nd.ni_vp, if it exists, is referenced but not locked.
2625 vap->va_type = VDIR;
2632 * Issue mkdir op. Since SAVESTART is not set, the pathname
2633 * component is freed by the VOP call. This will fill-in
2634 * nd.ni_vp, reference, and exclusively lock it.
2636 if (vap->va_mode == (mode_t)VNOVAL)
2639 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2641 error = VOP_NMKDIR(nd.nl_ncp, &vp, nd.nl_cred, vap);
2644 bzero((caddr_t)fhp, sizeof(nfh));
2645 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2646 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2648 error = VOP_GETATTR(vp, vap, td);
2652 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2653 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2656 nfsm_srvpostop_fh(fhp);
2657 nfsm_srvpostop_attr(0, vap);
2659 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2661 nfsm_srvfhtom(fhp, v3);
2662 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2663 nfsm_srvfillattr(vap, fp);
2681 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2682 struct thread *td, struct mbuf **mrq)
2684 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2685 struct sockaddr *nam = nfsd->nd_nam;
2686 caddr_t dpos = nfsd->nd_dpos;
2687 struct ucred *cred = &nfsd->nd_cr;
2691 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2692 int v3 = (nfsd->nd_flag & ND_NFSV3);
2694 struct mbuf *mb, *mreq;
2697 struct vattr dirfor, diraft;
2700 struct nlookupdata nd;
2703 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2708 fhp = &nfh.fh_generic;
2710 nfsm_srvnamesiz(len);
2712 error = nfs_namei(&nd, cred, NAMEI_DELETE, NULL, &vp,
2713 fhp, len, slp, nam, &md, &dpos, &dirp,
2714 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2717 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2720 nfsm_reply(NFSX_WCCDATA(v3));
2721 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2725 if (vp->v_type != VDIR) {
2731 * The root of a mounted filesystem cannot be deleted.
2733 if (vp->v_flag & VROOT)
2737 * Issue or abort op. Since SAVESTART is not set, path name
2738 * component is freed by the VOP after either.
2742 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2743 nqsrv_getl(vp, ND_WRITE);
2747 error = VOP_NRMDIR(nd.nl_ncp, nd.nl_cred);
2752 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2753 nfsm_reply(NFSX_WCCDATA(v3));
2755 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2770 * nfs readdir service
2771 * - mallocs what it thinks is enough to read
2772 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2773 * - calls VOP_READDIR()
2774 * - loops around building the reply
2775 * if the output generated exceeds count break out of loop
2776 * The nfsm_clget macro is used here so that the reply will be packed
2777 * tightly in mbuf clusters.
2778 * - it only knows that it has encountered eof when the VOP_READDIR()
2780 * - as such one readdir rpc will return eof false although you are there
2781 * and then the next will return eof
2782 * - it trims out records with d_fileno == 0
2783 * this doesn't matter for Unix clients, but they might confuse clients
2785 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2786 * than requested, but this may not apply to all filesystems. For
2787 * example, client NFS does not { although it is never remote mounted
2789 * The alternate call nfsrv_readdirplus() does lookups as well.
2790 * PS: The NFS protocol spec. does not clarify what the "count" byte
2791 * argument is a count of.. just name strings and file id's or the
2792 * entire reply rpc or ...
2793 * I tried just file name and id sizes and it confused the Sun client,
2794 * so I am using the full rpc size now. The "paranoia.." comment refers
2795 * to including the status longwords that are not a part of the dir.
2796 * "entry" structures, but are in the rpc.
2800 u_int32_t fl_postopok;
2801 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2803 u_int32_t fl_fhsize;
2804 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2808 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2809 struct thread *td, struct mbuf **mrq)
2811 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2812 struct sockaddr *nam = nfsd->nd_nam;
2813 caddr_t dpos = nfsd->nd_dpos;
2814 struct ucred *cred = &nfsd->nd_cr;
2822 struct mbuf *mb, *mb2, *mreq, *mp2;
2823 char *cpos, *cend, *cp2, *rbuf;
2824 struct vnode *vp = NULL;
2830 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2831 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2832 int v3 = (nfsd->nd_flag & ND_NFSV3);
2833 u_quad_t frev, off, toff, verf;
2834 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2836 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2837 fhp = &nfh.fh_generic;
2840 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2841 toff = fxdr_hyper(tl);
2843 verf = fxdr_hyper(tl);
2846 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2847 toff = fxdr_unsigned(u_quad_t, *tl++);
2848 verf = 0; /* shut up gcc */
2851 cnt = fxdr_unsigned(int, *tl);
2852 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2853 xfer = NFS_SRVMAXDATA(nfsd);
2859 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2860 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2861 if (!error && vp->v_type != VDIR) {
2867 nfsm_reply(NFSX_UNSIGNED);
2868 nfsm_srvpostop_attr(getret, &at);
2874 * Obtain lock on vnode for this section of the code
2877 nqsrv_getl(vp, ND_READ);
2879 error = getret = VOP_GETATTR(vp, &at, td);
2882 * XXX This check may be too strict for Solaris 2.5 clients.
2884 if (!error && toff && verf && verf != at.va_filerev)
2885 error = NFSERR_BAD_COOKIE;
2889 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
2893 nfsm_reply(NFSX_POSTOPATTR(v3));
2894 nfsm_srvpostop_attr(getret, &at);
2898 VOP_UNLOCK(vp, 0, td);
2901 * end section. Allocate rbuf and continue
2903 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2906 iv.iov_len = fullsiz;
2909 io.uio_offset = (off_t)off;
2910 io.uio_resid = fullsiz;
2911 io.uio_segflg = UIO_SYSSPACE;
2912 io.uio_rw = UIO_READ;
2915 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2917 free((caddr_t)cookies, M_TEMP);
2920 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2921 off = (off_t)io.uio_offset;
2922 if (!cookies && !error)
2923 error = NFSERR_PERM;
2925 getret = VOP_GETATTR(vp, &at, td);
2929 VOP_UNLOCK(vp, 0, td);
2933 free((caddr_t)rbuf, M_TEMP);
2935 free((caddr_t)cookies, M_TEMP);
2936 nfsm_reply(NFSX_POSTOPATTR(v3));
2937 nfsm_srvpostop_attr(getret, &at);
2942 siz -= io.uio_resid;
2945 * If nothing read, return eof
2951 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2954 nfsm_srvpostop_attr(getret, &at);
2955 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2956 txdr_hyper(at.va_filerev, tl);
2959 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2962 FREE((caddr_t)rbuf, M_TEMP);
2963 FREE((caddr_t)cookies, M_TEMP);
2970 * Check for degenerate cases of nothing useful read.
2971 * If so go try again
2975 dp = (struct dirent *)cpos;
2978 * For some reason FreeBSD's ufs_readdir() chooses to back the
2979 * directory offset up to a block boundary, so it is necessary to
2980 * skip over the records that preceed the requested offset. This
2981 * requires the assumption that file offset cookies monotonically
2984 while (cpos < cend && ncookies > 0 &&
2985 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2986 ((u_quad_t)(*cookiep)) <= toff)) {
2987 cpos += dp->d_reclen;
2988 dp = (struct dirent *)cpos;
2992 if (cpos >= cend || ncookies == 0) {
2998 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2999 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3001 nfsm_srvpostop_attr(getret, &at);
3002 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3003 txdr_hyper(at.va_filerev, tl);
3007 be = bp + M_TRAILINGSPACE(mp);
3009 /* Loop through the records and build reply */
3010 while (cpos < cend && ncookies > 0) {
3011 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3012 nlen = dp->d_namlen;
3013 rem = nfsm_rndup(nlen) - nlen;
3014 len += (4 * NFSX_UNSIGNED + nlen + rem);
3016 len += 2 * NFSX_UNSIGNED;
3022 * Build the directory record xdr from
3027 bp += NFSX_UNSIGNED;
3031 bp += NFSX_UNSIGNED;
3034 *tl = txdr_unsigned(dp->d_fileno);
3035 bp += NFSX_UNSIGNED;
3037 *tl = txdr_unsigned(nlen);
3038 bp += NFSX_UNSIGNED;
3040 /* And loop around copying the name */
3049 bcopy(cp, bp, tsiz);
3055 /* And null pad to a int32_t boundary */
3056 for (i = 0; i < rem; i++)
3060 /* Finish off the record */
3063 bp += NFSX_UNSIGNED;
3066 *tl = txdr_unsigned(*cookiep);
3067 bp += NFSX_UNSIGNED;
3069 cpos += dp->d_reclen;
3070 dp = (struct dirent *)cpos;
3078 bp += NFSX_UNSIGNED;
3084 bp += NFSX_UNSIGNED;
3087 mp->m_len = bp - mtod(mp, caddr_t);
3089 mp->m_len += bp - bpos;
3090 FREE((caddr_t)rbuf, M_TEMP);
3091 FREE((caddr_t)cookies, M_TEMP);
3100 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3101 struct thread *td, struct mbuf **mrq)
3103 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3104 struct sockaddr *nam = nfsd->nd_nam;
3105 caddr_t dpos = nfsd->nd_dpos;
3106 struct ucred *cred = &nfsd->nd_cr;
3114 struct mbuf *mb, *mb2, *mreq, *mp2;
3115 char *cpos, *cend, *cp2, *rbuf;
3116 struct vnode *vp = NULL, *nvp;
3119 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3122 struct vattr va, at, *vap = &va;
3123 struct nfs_fattr *fp;
3124 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3125 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3126 u_quad_t frev, off, toff, verf;
3127 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3129 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3130 fhp = &nfh.fh_generic;
3132 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3133 toff = fxdr_hyper(tl);
3135 verf = fxdr_hyper(tl);
3137 siz = fxdr_unsigned(int, *tl++);
3138 cnt = fxdr_unsigned(int, *tl);
3140 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3141 xfer = NFS_SRVMAXDATA(nfsd);
3147 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3148 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3149 if (!error && vp->v_type != VDIR) {
3155 nfsm_reply(NFSX_UNSIGNED);
3156 nfsm_srvpostop_attr(getret, &at);
3160 error = getret = VOP_GETATTR(vp, &at, td);
3163 * XXX This check may be too strict for Solaris 2.5 clients.
3165 if (!error && toff && verf && verf != at.va_filerev)
3166 error = NFSERR_BAD_COOKIE;
3169 nqsrv_getl(vp, ND_READ);
3170 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3175 nfsm_reply(NFSX_V3POSTOPATTR);
3176 nfsm_srvpostop_attr(getret, &at);
3180 VOP_UNLOCK(vp, 0, td);
3181 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3184 iv.iov_len = fullsiz;
3187 io.uio_offset = (off_t)off;
3188 io.uio_resid = fullsiz;
3189 io.uio_segflg = UIO_SYSSPACE;
3190 io.uio_rw = UIO_READ;
3193 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3195 free((caddr_t)cookies, M_TEMP);
3198 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3199 off = (u_quad_t)io.uio_offset;
3200 getret = VOP_GETATTR(vp, &at, td);
3201 VOP_UNLOCK(vp, 0, td);
3202 if (!cookies && !error)
3203 error = NFSERR_PERM;
3210 free((caddr_t)cookies, M_TEMP);
3211 free((caddr_t)rbuf, M_TEMP);
3212 nfsm_reply(NFSX_V3POSTOPATTR);
3213 nfsm_srvpostop_attr(getret, &at);
3218 siz -= io.uio_resid;
3221 * If nothing read, return eof
3227 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3229 nfsm_srvpostop_attr(getret, &at);
3230 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3231 txdr_hyper(at.va_filerev, tl);
3235 FREE((caddr_t)cookies, M_TEMP);
3236 FREE((caddr_t)rbuf, M_TEMP);
3243 * Check for degenerate cases of nothing useful read.
3244 * If so go try again
3248 dp = (struct dirent *)cpos;
3251 * For some reason FreeBSD's ufs_readdir() chooses to back the
3252 * directory offset up to a block boundary, so it is necessary to
3253 * skip over the records that preceed the requested offset. This
3254 * requires the assumption that file offset cookies monotonically
3257 while (cpos < cend && ncookies > 0 &&
3258 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3259 ((u_quad_t)(*cookiep)) <= toff)) {
3260 cpos += dp->d_reclen;
3261 dp = (struct dirent *)cpos;
3265 if (cpos >= cend || ncookies == 0) {
3272 * Probe one of the directory entries to see if the filesystem
3275 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3276 error = NFSERR_NOTSUPP;
3279 free((caddr_t)cookies, M_TEMP);
3280 free((caddr_t)rbuf, M_TEMP);
3281 nfsm_reply(NFSX_V3POSTOPATTR);
3282 nfsm_srvpostop_attr(getret, &at);
3291 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3293 nfsm_srvpostop_attr(getret, &at);
3294 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3295 txdr_hyper(at.va_filerev, tl);
3298 be = bp + M_TRAILINGSPACE(mp);
3300 /* Loop through the records and build reply */
3301 while (cpos < cend && ncookies > 0) {
3302 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3303 nlen = dp->d_namlen;
3304 rem = nfsm_rndup(nlen)-nlen;
3307 * For readdir_and_lookup get the vnode using
3310 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3312 bzero((caddr_t)nfhp, NFSX_V3FH);
3314 nvp->v_mount->mnt_stat.f_fsid;
3315 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3320 if (VOP_GETATTR(nvp, vap, td)) {
3329 * If either the dircount or maxcount will be
3330 * exceeded, get out now. Both of these lengths
3331 * are calculated conservatively, including all
3334 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3336 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3337 if (len > cnt || dirlen > fullsiz) {
3343 * Build the directory record xdr from
3346 fp = (struct nfs_fattr *)&fl.fl_fattr;
3347 nfsm_srvfillattr(vap, fp);
3348 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3349 fl.fl_fhok = nfs_true;
3350 fl.fl_postopok = nfs_true;
3351 fl.fl_off.nfsuquad[0] = 0;
3352 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3356 bp += NFSX_UNSIGNED;
3359 bp += NFSX_UNSIGNED;
3361 *tl = txdr_unsigned(dp->d_fileno);
3362 bp += NFSX_UNSIGNED;
3364 *tl = txdr_unsigned(nlen);
3365 bp += NFSX_UNSIGNED;
3367 /* And loop around copying the name */
3372 if ((bp + xfer) > be)
3376 bcopy(cp, bp, tsiz);
3382 /* And null pad to a int32_t boundary */
3383 for (i = 0; i < rem; i++)
3387 * Now copy the flrep structure out.
3389 xfer = sizeof (struct flrep);
3393 if ((bp + xfer) > be)
3397 bcopy(cp, bp, tsiz);
3405 cpos += dp->d_reclen;
3406 dp = (struct dirent *)cpos;
3414 bp += NFSX_UNSIGNED;
3420 bp += NFSX_UNSIGNED;
3423 mp->m_len = bp - mtod(mp, caddr_t);
3425 mp->m_len += bp - bpos;
3426 FREE((caddr_t)cookies, M_TEMP);
3427 FREE((caddr_t)rbuf, M_TEMP);
3435 * nfs commit service
3438 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3439 struct thread *td, struct mbuf **mrq)
3441 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3442 struct sockaddr *nam = nfsd->nd_nam;
3443 caddr_t dpos = nfsd->nd_dpos;
3444 struct ucred *cred = &nfsd->nd_cr;
3445 struct vattr bfor, aft;
3446 struct vnode *vp = NULL;
3452 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3454 struct mbuf *mb, *mb2, *mreq;
3457 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3461 fhp = &nfh.fh_generic;
3463 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3466 * XXX At this time VOP_FSYNC() does not accept offset and byte
3467 * count parameters, so these arguments are useless (someday maybe).
3469 off = fxdr_hyper(tl);
3471 cnt = fxdr_unsigned(int, *tl);
3472 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3473 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3475 nfsm_reply(2 * NFSX_UNSIGNED);
3476 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3480 for_ret = VOP_GETATTR(vp, &bfor, td);
3482 if (cnt > MAX_COMMIT_COUNT) {
3484 * Give up and do the whole thing
3487 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3488 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3490 error = VOP_FSYNC(vp, MNT_WAIT, td);
3493 * Locate and synchronously write any buffers that fall
3494 * into the requested range. Note: we are assuming that
3495 * f_iosize is a power of 2.
3497 int iosize = vp->v_mount->mnt_stat.f_iosize;
3498 int iomask = iosize - 1;
3503 * Align to iosize boundry, super-align to page boundry.
3506 cnt += off & iomask;
3507 off &= ~(u_quad_t)iomask;
3509 if (off & PAGE_MASK) {
3510 cnt += off & PAGE_MASK;
3511 off &= ~(u_quad_t)PAGE_MASK;
3513 lblkno = off / iosize;
3516 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3517 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3525 * If we have a buffer and it is marked B_DELWRI we
3526 * have to lock and write it. Otherwise the prior
3527 * write is assumed to have already been committed.
3529 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3530 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3531 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3532 continue; /* retry */
3535 bp->b_flags &= ~B_ASYNC;
3536 VOP_BWRITE(bp->b_vp, bp);
3548 aft_ret = VOP_GETATTR(vp, &aft, td);
3551 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3552 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3554 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3555 if (nfsver.tv_sec == 0)
3557 *tl++ = txdr_unsigned(nfsver.tv_sec);
3558 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3569 * nfs statfs service
3572 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3573 struct thread *td, struct mbuf **mrq)
3575 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3576 struct sockaddr *nam = nfsd->nd_nam;
3577 caddr_t dpos = nfsd->nd_dpos;
3578 struct ucred *cred = &nfsd->nd_cr;
3580 struct nfs_statfs *sfp;
3584 int error = 0, rdonly, cache, getret = 1;
3585 int v3 = (nfsd->nd_flag & ND_NFSV3);
3587 struct mbuf *mb, *mb2, *mreq;
3588 struct vnode *vp = NULL;
3592 struct statfs statfs;
3593 u_quad_t frev, tval;
3595 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3599 fhp = &nfh.fh_generic;
3601 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3602 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3604 nfsm_reply(NFSX_UNSIGNED);
3605 nfsm_srvpostop_attr(getret, &at);
3610 error = VFS_STATFS(vp->v_mount, sf, td);
3611 getret = VOP_GETATTR(vp, &at, td);
3614 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3616 nfsm_srvpostop_attr(getret, &at);
3621 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3623 tval = (u_quad_t)sf->f_blocks;
3624 tval *= (u_quad_t)sf->f_bsize;
3625 txdr_hyper(tval, &sfp->sf_tbytes);
3626 tval = (u_quad_t)sf->f_bfree;
3627 tval *= (u_quad_t)sf->f_bsize;
3628 txdr_hyper(tval, &sfp->sf_fbytes);
3629 tval = (u_quad_t)sf->f_bavail;
3630 tval *= (u_quad_t)sf->f_bsize;
3631 txdr_hyper(tval, &sfp->sf_abytes);
3632 sfp->sf_tfiles.nfsuquad[0] = 0;
3633 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3634 sfp->sf_ffiles.nfsuquad[0] = 0;
3635 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3636 sfp->sf_afiles.nfsuquad[0] = 0;
3637 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3638 sfp->sf_invarsec = 0;
3640 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3641 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3642 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3643 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3644 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3653 * nfs fsinfo service
3656 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3657 struct thread *td, struct mbuf **mrq)
3659 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3660 struct sockaddr *nam = nfsd->nd_nam;
3661 caddr_t dpos = nfsd->nd_dpos;
3662 struct ucred *cred = &nfsd->nd_cr;
3664 struct nfsv3_fsinfo *sip;
3667 int error = 0, rdonly, cache, getret = 1, pref;
3669 struct mbuf *mb, *mb2, *mreq;
3670 struct vnode *vp = NULL;
3674 u_quad_t frev, maxfsize;
3677 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3681 fhp = &nfh.fh_generic;
3683 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3684 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3686 nfsm_reply(NFSX_UNSIGNED);
3687 nfsm_srvpostop_attr(getret, &at);
3692 /* XXX Try to make a guess on the max file size. */
3693 VFS_STATFS(vp->v_mount, &sb, td);
3694 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3696 getret = VOP_GETATTR(vp, &at, td);
3699 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3700 nfsm_srvpostop_attr(getret, &at);
3701 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3705 * There should be file system VFS OP(s) to get this information.
3706 * For now, assume ufs.
3708 if (slp->ns_so->so_type == SOCK_DGRAM)
3709 pref = NFS_MAXDGRAMDATA;
3712 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3713 sip->fs_rtpref = txdr_unsigned(pref);
3714 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3715 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3716 sip->fs_wtpref = txdr_unsigned(pref);
3717 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3718 sip->fs_dtpref = txdr_unsigned(pref);
3719 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3720 sip->fs_timedelta.nfsv3_sec = 0;
3721 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3722 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3723 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3724 NFSV3FSINFO_CANSETTIME);
3732 * nfs pathconf service
3735 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3736 struct thread *td, struct mbuf **mrq)
3738 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3739 struct sockaddr *nam = nfsd->nd_nam;
3740 caddr_t dpos = nfsd->nd_dpos;
3741 struct ucred *cred = &nfsd->nd_cr;
3743 struct nfsv3_pathconf *pc;
3746 int error = 0, rdonly, cache, getret = 1;
3747 register_t linkmax, namemax, chownres, notrunc;
3749 struct mbuf *mb, *mb2, *mreq;
3750 struct vnode *vp = NULL;
3756 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3760 fhp = &nfh.fh_generic;
3762 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3763 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3765 nfsm_reply(NFSX_UNSIGNED);
3766 nfsm_srvpostop_attr(getret, &at);
3770 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3772 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3774 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3776 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3777 getret = VOP_GETATTR(vp, &at, td);
3780 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3781 nfsm_srvpostop_attr(getret, &at);
3786 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3788 pc->pc_linkmax = txdr_unsigned(linkmax);
3789 pc->pc_namemax = txdr_unsigned(namemax);
3790 pc->pc_notrunc = txdr_unsigned(notrunc);
3791 pc->pc_chownrestricted = txdr_unsigned(chownres);
3794 * These should probably be supported by VOP_PATHCONF(), but
3795 * until msdosfs is exportable (why would you want to?), the
3796 * Unix defaults should be ok.
3798 pc->pc_caseinsensitive = nfs_false;
3799 pc->pc_casepreserving = nfs_true;
3807 * Null operation, used by clients to ping server
3811 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3812 struct thread *td, struct mbuf **mrq)
3814 struct mbuf *mrep = nfsd->nd_mrep;
3816 int error = NFSERR_RETVOID, cache;
3817 struct mbuf *mb, *mreq;
3820 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3829 * No operation, used for obsolete procedures
3833 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3834 struct thread *td, struct mbuf **mrq)
3836 struct mbuf *mrep = nfsd->nd_mrep;
3839 struct mbuf *mb, *mreq;
3842 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3846 if (nfsd->nd_repstat)
3847 error = nfsd->nd_repstat;
3849 error = EPROCUNAVAIL;
3856 * Perform access checking for vnodes obtained from file handles that would
3857 * refer to files already opened by a Unix client. You cannot just use
3858 * vn_writechk() and VOP_ACCESS() for two reasons.
3859 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3860 * 2 - The owner is to be given access irrespective of mode bits for some
3861 * operations, so that processes that chmod after opening a file don't
3862 * break. I don't like this because it opens a security hole, but since
3863 * the nfs server opens a security hole the size of a barn door anyhow,
3866 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3867 * will return EPERM instead of EACCESS. EPERM is always an error.
3870 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
3871 int rdonly, struct thread *td, int override)
3876 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3877 if (flags & VWRITE) {
3878 /* Just vn_writechk() changed to check rdonly */
3880 * Disallow write attempts on read-only file systems;
3881 * unless the file is a socket or a block or character
3882 * device resident on the file system.
3884 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3885 switch (vp->v_type) {
3895 * If there's shared text associated with
3896 * the inode, we can't allow writing.
3898 if (vp->v_flag & VTEXT)
3901 error = VOP_GETATTR(vp, &vattr, td);
3904 error = VOP_ACCESS(vp, flags, cred, td);
3906 * Allow certain operations for the owner (reads and writes
3907 * on files that are already open).
3909 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3913 #endif /* NFS_NOSERVER */