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.21 2004/12/20 18:37:59 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>
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 MGET(mp, MB_WAIT, MT_DATA);
651 mp->m_len = NFSMSIZ(mp);
658 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
659 mp->m_len = NFS_MAXPATHLEN-len;
660 len = NFS_MAXPATHLEN;
663 ivp->iov_base = mtod(mp, caddr_t);
664 ivp->iov_len = mp->m_len;
669 uiop->uio_iovcnt = i;
670 uiop->uio_offset = 0;
671 uiop->uio_resid = len;
672 uiop->uio_rw = UIO_READ;
673 uiop->uio_segflg = UIO_SYSSPACE;
675 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
676 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
678 nfsm_reply(2 * NFSX_UNSIGNED);
679 nfsm_srvpostop_attr(1, (struct vattr *)0);
683 if (vp->v_type != VLNK) {
690 nqsrv_getl(vp, ND_READ);
691 error = VOP_READLINK(vp, uiop, cred);
693 getret = VOP_GETATTR(vp, &attr, td);
696 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
698 nfsm_srvpostop_attr(getret, &attr);
704 if (uiop->uio_resid > 0) {
705 len -= uiop->uio_resid;
706 tlen = nfsm_rndup(len);
707 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
709 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
710 *tl = txdr_unsigned(len);
725 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
726 struct thread *td, struct mbuf **mrq)
728 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
729 struct sockaddr *nam = nfsd->nd_nam;
730 caddr_t dpos = nfsd->nd_dpos;
731 struct ucred *cred = &nfsd->nd_cr;
735 struct nfs_fattr *fp;
740 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
741 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
743 struct mbuf *mb, *mb2, *mreq;
745 struct vnode *vp = NULL;
748 struct uio io, *uiop = &io;
749 struct vattr va, *vap = &va;
755 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
756 fhp = &nfh.fh_generic;
759 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
760 off = fxdr_hyper(tl);
762 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
763 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
765 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
768 * Reference vp. If an error occurs, vp will be invalid, but we
769 * have to NULL it just in case. The macros might goto nfsmout
773 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
774 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
777 nfsm_reply(2 * NFSX_UNSIGNED);
778 nfsm_srvpostop_attr(1, (struct vattr *)0);
783 if (vp->v_type != VREG) {
787 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
790 nqsrv_getl(vp, ND_READ);
791 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
792 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
794 getret = VOP_GETATTR(vp, vap, td);
800 nfsm_reply(NFSX_POSTOPATTR(v3));
801 nfsm_srvpostop_attr(getret, vap);
807 * Calculate byte count to read
810 if (off >= vap->va_size)
812 else if ((off + reqlen) > vap->va_size)
813 cnt = vap->va_size - off;
818 * Calculate seqcount for heuristic
826 * Locate best candidate
829 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
833 if (nfsheur[hi].nh_vp == vp) {
837 if (nfsheur[hi].nh_use > 0)
838 --nfsheur[hi].nh_use;
839 hi = (hi + 1) % NUM_HEURISTIC;
840 if (nfsheur[hi].nh_use < nh->nh_use)
844 if (nh->nh_vp != vp) {
847 nh->nh_use = NHUSE_INIT;
855 * Calculate heuristic
858 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
859 if (++nh->nh_seqcount > IO_SEQMAX)
860 nh->nh_seqcount = IO_SEQMAX;
861 } else if (nh->nh_seqcount > 1) {
866 nh->nh_use += NHUSE_INC;
867 if (nh->nh_use > NHUSE_MAX)
868 nh->nh_use = NHUSE_MAX;
869 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
872 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
874 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
876 fp = (struct nfs_fattr *)tl;
877 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
879 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
880 fp = (struct nfs_fattr *)tl;
881 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
883 len = left = nfsm_rndup(cnt);
886 * Generate the mbuf list with the uio_iov ref. to it.
891 siz = min(M_TRAILINGSPACE(m), left);
897 MGET(m, MB_WAIT, MT_DATA);
904 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
906 uiop->uio_iov = iv2 = iv;
912 panic("nfsrv_read iov");
913 siz = min(M_TRAILINGSPACE(m), left);
915 iv->iov_base = mtod(m, caddr_t) + m->m_len;
924 uiop->uio_iovcnt = i;
925 uiop->uio_offset = off;
926 uiop->uio_resid = len;
927 uiop->uio_rw = UIO_READ;
928 uiop->uio_segflg = UIO_SYSSPACE;
929 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
930 off = uiop->uio_offset;
932 FREE((caddr_t)iv2, M_TEMP);
933 if (error || (getret = VOP_GETATTR(vp, vap, td))) {
939 nfsm_reply(NFSX_POSTOPATTR(v3));
940 nfsm_srvpostop_attr(getret, vap);
949 nfsm_srvfillattr(vap, fp);
950 tlen = len - uiop->uio_resid;
951 cnt = cnt < tlen ? cnt : tlen;
952 tlen = nfsm_rndup(cnt);
953 if (len != tlen || tlen != cnt)
954 nfsm_adj(mb, len - tlen, tlen - cnt);
956 *tl++ = txdr_unsigned(cnt);
962 *tl = txdr_unsigned(cnt);
973 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
974 struct thread *td, struct mbuf **mrq)
976 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
977 struct sockaddr *nam = nfsd->nd_nam;
978 caddr_t dpos = nfsd->nd_dpos;
979 struct ucred *cred = &nfsd->nd_cr;
983 struct nfs_fattr *fp;
985 struct vattr va, forat;
986 struct vattr *vap = &va;
990 int error = 0, rdonly, cache, len, forat_ret = 1;
991 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
992 int stable = NFSV3WRITE_FILESYNC;
993 int v3 = (nfsd->nd_flag & ND_NFSV3);
995 struct mbuf *mb, *mb2, *mreq;
996 struct vnode *vp = NULL;
999 struct uio io, *uiop = &io;
1003 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1009 fhp = &nfh.fh_generic;
1012 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1013 off = fxdr_hyper(tl);
1015 stable = fxdr_unsigned(int, *tl++);
1017 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1018 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1021 stable = NFSV3WRITE_UNSTABLE;
1023 retlen = len = fxdr_unsigned(int32_t, *tl);
1027 * For NFS Version 2, it is not obvious what a write of zero length
1028 * should do, but I might as well be consistent with Version 3,
1029 * which is to return ok so long as there are no permission problems.
1037 adjust = dpos - mtod(mp, caddr_t);
1038 mp->m_len -= adjust;
1039 if (mp->m_len > 0 && adjust > 0)
1040 NFSMADV(mp, adjust);
1044 else if (mp->m_len > 0) {
1047 mp->m_len -= (i - len);
1056 if (len > NFS_MAXDATA || len < 0 || i < len) {
1058 nfsm_reply(2 * NFSX_UNSIGNED);
1059 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1063 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1064 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1067 nfsm_reply(2 * NFSX_UNSIGNED);
1068 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1073 forat_ret = VOP_GETATTR(vp, &forat, td);
1074 if (vp->v_type != VREG) {
1078 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1081 nqsrv_getl(vp, ND_WRITE);
1082 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1087 nfsm_reply(NFSX_WCCDATA(v3));
1088 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1094 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1096 uiop->uio_iov = iv = ivp;
1097 uiop->uio_iovcnt = cnt;
1100 if (mp->m_len > 0) {
1101 ivp->iov_base = mtod(mp, caddr_t);
1102 ivp->iov_len = mp->m_len;
1110 * The IO_METASYNC flag indicates that all metadata (and not just
1111 * enough to ensure data integrity) mus be written to stable storage
1113 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1115 if (stable == NFSV3WRITE_UNSTABLE)
1116 ioflags = IO_NODELOCKED;
1117 else if (stable == NFSV3WRITE_DATASYNC)
1118 ioflags = (IO_SYNC | IO_NODELOCKED);
1120 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1121 uiop->uio_resid = len;
1122 uiop->uio_rw = UIO_WRITE;
1123 uiop->uio_segflg = UIO_SYSSPACE;
1124 uiop->uio_td = NULL;
1125 uiop->uio_offset = off;
1126 error = VOP_WRITE(vp, uiop, ioflags, cred);
1127 nfsstats.srvvop_writes++;
1128 FREE((caddr_t)iv, M_TEMP);
1130 aftat_ret = VOP_GETATTR(vp, vap, td);
1135 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1136 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1138 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1143 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1144 *tl++ = txdr_unsigned(retlen);
1146 * If nfs_async is set, then pretend the write was FILESYNC.
1148 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1149 *tl++ = txdr_unsigned(stable);
1151 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1153 * Actually, there is no need to txdr these fields,
1154 * but it may make the values more human readable,
1155 * for debugging purposes.
1157 if (nfsver.tv_sec == 0)
1159 *tl++ = txdr_unsigned(nfsver.tv_sec);
1160 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1162 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1163 nfsm_srvfillattr(vap, fp);
1172 * NFS write service with write gathering support. Called when
1173 * nfsrvw_procrastinate > 0.
1174 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1175 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1179 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1180 struct thread *td, struct mbuf **mrq)
1184 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1185 struct nfs_fattr *fp;
1188 struct nfsrvw_delayhash *wpp;
1190 struct vattr va, forat;
1194 int error = 0, rdonly, cache, len, forat_ret = 1;
1195 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1197 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1198 struct vnode *vp = NULL;
1199 struct uio io, *uiop = &io;
1200 u_quad_t frev, cur_usec;
1202 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1211 mrep = nfsd->nd_mrep;
1213 dpos = nfsd->nd_dpos;
1214 cred = &nfsd->nd_cr;
1215 v3 = (nfsd->nd_flag & ND_NFSV3);
1216 LIST_INIT(&nfsd->nd_coalesce);
1217 nfsd->nd_mreq = NULL;
1218 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1219 cur_usec = nfs_curusec();
1220 nfsd->nd_time = cur_usec +
1221 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1224 * Now, get the write header..
1226 nfsm_srvmtofh(&nfsd->nd_fh);
1228 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1229 nfsd->nd_off = fxdr_hyper(tl);
1231 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1233 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1234 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1237 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1239 len = fxdr_unsigned(int32_t, *tl);
1241 nfsd->nd_eoff = nfsd->nd_off + len;
1244 * Trim the header out of the mbuf list and trim off any trailing
1245 * junk so that the mbuf list has only the write data.
1253 adjust = dpos - mtod(mp, caddr_t);
1254 mp->m_len -= adjust;
1255 if (mp->m_len > 0 && adjust > 0)
1256 NFSMADV(mp, adjust);
1263 mp->m_len -= (i - len);
1269 if (len > NFS_MAXDATA || len < 0 || i < len) {
1273 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1275 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1276 nfsd->nd_mreq = mreq;
1277 nfsd->nd_mrep = NULL;
1282 * Add this entry to the hash and time queues.
1286 wp = slp->ns_tq.lh_first;
1287 while (wp && wp->nd_time < nfsd->nd_time) {
1289 wp = wp->nd_tq.le_next;
1291 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1293 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1295 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1297 if (nfsd->nd_mrep) {
1298 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1302 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1304 wp = wp->nd_hash.le_next;
1306 while (wp && wp->nd_off < nfsd->nd_off &&
1307 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1309 wp = wp->nd_hash.le_next;
1312 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1315 * Search the hash list for overlapping entries and
1318 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1319 wp = nfsd->nd_hash.le_next;
1320 if (NFSW_SAMECRED(owp, nfsd))
1321 nfsrvw_coalesce(owp, nfsd);
1324 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1331 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1332 * and generate the associated reply mbuf list(s).
1335 cur_usec = nfs_curusec();
1337 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1338 owp = nfsd->nd_tq.le_next;
1339 if (nfsd->nd_time > cur_usec)
1343 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1344 LIST_REMOVE(nfsd, nd_tq);
1345 LIST_REMOVE(nfsd, nd_hash);
1347 mrep = nfsd->nd_mrep;
1348 nfsd->nd_mrep = NULL;
1349 cred = &nfsd->nd_cr;
1350 v3 = (nfsd->nd_flag & ND_NFSV3);
1351 forat_ret = aftat_ret = 1;
1352 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1353 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1356 forat_ret = VOP_GETATTR(vp, &forat, td);
1357 if (vp->v_type != VREG) {
1361 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1367 nqsrv_getl(vp, ND_WRITE);
1368 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1371 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1372 ioflags = IO_NODELOCKED;
1373 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1374 ioflags = (IO_SYNC | IO_NODELOCKED);
1376 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1377 uiop->uio_rw = UIO_WRITE;
1378 uiop->uio_segflg = UIO_SYSSPACE;
1379 uiop->uio_td = NULL;
1380 uiop->uio_offset = nfsd->nd_off;
1381 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1382 if (uiop->uio_resid > 0) {
1390 uiop->uio_iovcnt = i;
1391 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1393 uiop->uio_iov = ivp = iov;
1396 if (mp->m_len > 0) {
1397 ivp->iov_base = mtod(mp, caddr_t);
1398 ivp->iov_len = mp->m_len;
1404 error = VOP_WRITE(vp, uiop, ioflags, cred);
1405 nfsstats.srvvop_writes++;
1407 FREE((caddr_t)iov, M_TEMP);
1411 aftat_ret = VOP_GETATTR(vp, &va, td);
1417 * Loop around generating replies for all write rpcs that have
1418 * now been completed.
1422 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1424 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1426 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1429 nfsm_writereply(NFSX_PREOPATTR(v3) +
1430 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1431 NFSX_WRITEVERF(v3), v3);
1433 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1434 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1435 *tl++ = txdr_unsigned(nfsd->nd_len);
1436 *tl++ = txdr_unsigned(swp->nd_stable);
1438 * Actually, there is no need to txdr these fields,
1439 * but it may make the values more human readable,
1440 * for debugging purposes.
1442 if (nfsver.tv_sec == 0)
1444 *tl++ = txdr_unsigned(nfsver.tv_sec);
1445 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1447 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1448 nfsm_srvfillattr(&va, fp);
1451 nfsd->nd_mreq = mreq;
1453 panic("nfsrv_write: nd_mrep not free");
1456 * Done. Put it at the head of the timer queue so that
1457 * the final phase can return the reply.
1462 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1464 nfsd = swp->nd_coalesce.lh_first;
1466 LIST_REMOVE(nfsd, nd_tq);
1472 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1479 * Search for a reply to return.
1482 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1483 if (nfsd->nd_mreq) {
1484 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1485 LIST_REMOVE(nfsd, nd_tq);
1486 *mrq = nfsd->nd_mreq;
1495 * Coalesce the write request nfsd into owp. To do this we must:
1496 * - remove nfsd from the queues
1497 * - merge nfsd->nd_mrep into owp->nd_mrep
1498 * - update the nd_eoff and nd_stable for owp
1499 * - put nfsd on owp's nd_coalesce list
1500 * NB: Must be called at splsoftclock().
1503 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1507 struct nfsrv_descript *p;
1509 NFS_DPF(WG, ("C%03x-%03x",
1510 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1511 LIST_REMOVE(nfsd, nd_hash);
1512 LIST_REMOVE(nfsd, nd_tq);
1513 if (owp->nd_eoff < nfsd->nd_eoff) {
1514 overlap = owp->nd_eoff - nfsd->nd_off;
1516 panic("nfsrv_coalesce: bad off");
1518 m_adj(nfsd->nd_mrep, overlap);
1522 mp->m_next = nfsd->nd_mrep;
1523 owp->nd_eoff = nfsd->nd_eoff;
1525 m_freem(nfsd->nd_mrep);
1526 nfsd->nd_mrep = NULL;
1527 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1528 owp->nd_stable = NFSV3WRITE_FILESYNC;
1529 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1530 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1531 owp->nd_stable = NFSV3WRITE_DATASYNC;
1532 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1535 * If nfsd had anything else coalesced into it, transfer them
1536 * to owp, otherwise their replies will never get sent.
1538 for (p = nfsd->nd_coalesce.lh_first; p;
1539 p = nfsd->nd_coalesce.lh_first) {
1540 LIST_REMOVE(p, nd_tq);
1541 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1546 * nfs create service
1547 * now does a truncate to 0 length via. setattr if it already exists
1550 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1551 struct thread *td, struct mbuf **mrq)
1553 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1554 struct sockaddr *nam = nfsd->nd_nam;
1555 caddr_t dpos = nfsd->nd_dpos;
1556 struct ucred *cred = &nfsd->nd_cr;
1557 struct nfs_fattr *fp;
1558 struct vattr va, dirfor, diraft;
1559 struct vattr *vap = &va;
1560 struct nfsv2_sattr *sp;
1562 struct nlookupdata nd;
1565 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1566 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1569 struct mbuf *mb, *mb2, *mreq;
1575 u_quad_t frev, tempsize;
1576 u_char cverf[NFSX_V3CREATEVERF];
1578 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1587 fhp = &nfh.fh_generic;
1589 nfsm_srvnamesiz(len);
1592 * Call namei and do initial cleanup to get a few things
1593 * out of the way. If we get an initial error we cleanup
1594 * and return here to avoid special-casing the invalid nd
1595 * structure through the rest of the case. dirp may be
1596 * set even if an error occurs, but the nd structure will not
1597 * be valid at all if an error occurs so we have to invalidate it
1598 * prior to calling nfsm_reply ( which might goto nfsmout ).
1600 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1601 fhp, len, slp, nam, &md, &dpos, &dirp,
1602 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1605 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1612 nfsm_reply(NFSX_WCCDATA(v3));
1613 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1619 * No error. Continue. State:
1622 * vp may be valid or NULL if the target does not
1626 * The error state is set through the code and we may also do some
1627 * opportunistic releasing of vnodes to avoid holding locks through
1628 * NFS I/O. The cleanup at the end is a catch-all
1633 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1634 how = fxdr_unsigned(int, *tl);
1636 case NFSV3CREATE_GUARDED:
1642 case NFSV3CREATE_UNCHECKED:
1645 case NFSV3CREATE_EXCLUSIVE:
1646 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1647 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1651 vap->va_type = VREG;
1653 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1654 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1655 if (vap->va_type == VNON)
1656 vap->va_type = VREG;
1657 vap->va_mode = nfstov_mode(sp->sa_mode);
1658 switch (vap->va_type) {
1660 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1662 vap->va_size = (u_quad_t)tsize;
1667 rdev = fxdr_unsigned(long, sp->sa_size);
1675 * Iff doesn't exist, create it
1676 * otherwise just truncate to 0 length
1677 * should I set the mode too ?
1679 * The only possible error we can have at this point is EEXIST.
1680 * nd.ni_vp will also be non-NULL in that case.
1683 if (vap->va_mode == (mode_t)VNOVAL)
1685 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1686 nqsrv_getl(dvp, ND_WRITE);
1687 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1689 nfsrv_object_create(vp);
1690 if (exclusive_flag) {
1693 bcopy(cverf, (caddr_t)&vap->va_atime,
1695 error = VOP_SETATTR(vp, vap, cred, td);
1699 vap->va_type == VCHR ||
1700 vap->va_type == VBLK ||
1701 vap->va_type == VFIFO
1704 * Handle SysV FIFO node special cases. All other
1705 * devices require super user to access.
1707 if (vap->va_type == VCHR && rdev == 0xffffffff)
1708 vap->va_type = VFIFO;
1709 if (vap->va_type != VFIFO &&
1710 (error = suser_cred(cred, 0))) {
1713 vap->va_rdev = rdev;
1714 nqsrv_getl(dvp, ND_WRITE);
1716 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1721 * XXX what is this junk supposed to do ?
1728 * release dvp prior to lookup
1736 * Even though LOCKPARENT was cleared, ni_dvp may
1739 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1740 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1741 nd.ni_cnd.cn_td = td;
1742 nd.ni_cnd.cn_cred = cred;
1744 error = lookup(&nd);
1749 /* fall through on certain errors */
1751 nfsrv_object_create(nd.ni_vp);
1752 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1761 if (vap->va_size != -1) {
1762 error = nfsrv_access(vp, VWRITE, cred,
1763 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1765 nqsrv_getl(vp, ND_WRITE);
1766 tempsize = vap->va_size;
1768 vap->va_size = tempsize;
1769 error = VOP_SETATTR(vp, vap, cred, td);
1775 bzero((caddr_t)fhp, sizeof(nfh));
1776 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1777 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1779 error = VOP_GETATTR(vp, vap, td);
1782 if (exclusive_flag && !error &&
1783 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1785 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1789 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1792 nfsm_srvpostop_fh(fhp);
1793 nfsm_srvpostop_attr(0, vap);
1795 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1798 nfsm_srvfhtom(fhp, v3);
1799 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1800 nfsm_srvfillattr(vap, fp);
1825 * nfs v3 mknod service
1828 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1829 struct thread *td, struct mbuf **mrq)
1831 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1832 struct sockaddr *nam = nfsd->nd_nam;
1833 caddr_t dpos = nfsd->nd_dpos;
1834 struct ucred *cred = &nfsd->nd_cr;
1835 struct vattr va, dirfor, diraft;
1836 struct vattr *vap = &va;
1838 struct nlookupdata nd;
1841 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1842 u_int32_t major, minor;
1845 struct mbuf *mb, *mb2, *mreq;
1853 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1859 fhp = &nfh.fh_generic;
1861 nfsm_srvnamesiz(len);
1864 * Handle nfs_namei() call. If an error occurs, the nd structure
1865 * is not valid. However, nfsm_*() routines may still jump to
1869 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
1870 fhp, len, slp, nam, &md, &dpos, &dirp,
1871 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1873 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
1875 nfsm_reply(NFSX_WCCDATA(1));
1876 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1880 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1881 vtyp = nfsv3tov_type(*tl);
1882 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1883 error = NFSERR_BADTYPE;
1888 if (vtyp == VCHR || vtyp == VBLK) {
1889 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1890 major = fxdr_unsigned(u_int32_t, *tl++);
1891 minor = fxdr_unsigned(u_int32_t, *tl);
1892 vap->va_rdev = makeudev(major, minor);
1896 * Iff doesn't exist, create it.
1902 vap->va_type = vtyp;
1903 if (vap->va_mode == (mode_t)VNOVAL)
1905 if (vtyp == VSOCK) {
1906 nqsrv_getl(dvp, ND_WRITE);
1907 error = VOP_NCREATE(nd.nl_ncp, &vp, nd.nl_cred, vap);
1909 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1911 nqsrv_getl(dvp, ND_WRITE);
1913 error = VOP_NMKNOD(nd.nl_ncp, &vp, nd.nl_cred, vap);
1922 * Release dvp prior to lookup
1928 * XXX what is this stuff for?
1930 KKASSERT(td->td_proc);
1931 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1932 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1933 nd.ni_cnd.cn_td = td;
1934 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1936 error = lookup(&nd);
1941 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK)
1947 * send response, cleanup, return.
1959 bzero((caddr_t)fhp, sizeof(nfh));
1960 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1961 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1963 error = VOP_GETATTR(vp, vap, td);
1969 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
1974 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1976 nfsm_srvpostop_fh(fhp);
1977 nfsm_srvpostop_attr(0, vap);
1979 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1997 * nfs remove service
2000 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2001 struct thread *td, struct mbuf **mrq)
2003 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2004 struct sockaddr *nam = nfsd->nd_nam;
2005 caddr_t dpos = nfsd->nd_dpos;
2006 struct ucred *cred = &nfsd->nd_cr;
2007 struct nlookupdata nd;
2011 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2012 int v3 = (nfsd->nd_flag & ND_NFSV3);
2014 struct mbuf *mb, *mreq;
2018 struct vattr dirfor, diraft;
2023 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2029 fhp = &nfh.fh_generic;
2031 nfsm_srvnamesiz(len);
2033 error = nfs_namei(&nd, cred, NAMEI_DELETE, &dvp, &vp,
2034 fhp, len, slp, nam, &md, &dpos, &dirp,
2035 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2038 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2041 if (vp->v_type == VDIR) {
2042 error = EPERM; /* POSIX */
2046 * The root of a mounted filesystem cannot be deleted.
2048 if (vp->v_flag & VROOT) {
2054 nqsrv_getl(dvp, ND_WRITE);
2055 nqsrv_getl(vp, ND_WRITE);
2056 error = VOP_NREMOVE(nd.nl_ncp, nd.nl_cred);
2060 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2061 nfsm_reply(NFSX_WCCDATA(v3));
2063 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2082 * nfs rename service
2085 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2086 struct thread *td, struct mbuf **mrq)
2088 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2089 struct sockaddr *nam = nfsd->nd_nam;
2090 caddr_t dpos = nfsd->nd_dpos;
2091 struct ucred *cred = &nfsd->nd_cr;
2095 int error = 0, cache = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2096 int tdirfor_ret = 1, tdiraft_ret = 1;
2097 int v3 = (nfsd->nd_flag & ND_NFSV3);
2099 struct mbuf *mb, *mreq;
2100 struct nlookupdata fromnd, tond;
2101 struct vnode *fvp, *fdirp;
2102 struct vnode *tvp, *tdirp;
2103 struct namecache *ncp;
2104 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2106 fhandle_t *ffhp, *tfhp;
2110 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2112 fvp = (struct vnode *)0;
2114 ffhp = &fnfh.fh_generic;
2115 tfhp = &tnfh.fh_generic;
2118 * Clear fields incase goto nfsmout occurs from macro.
2121 nlookup_zero(&fromnd);
2122 nlookup_zero(&tond);
2126 nfsm_srvmtofh(ffhp);
2127 nfsm_srvnamesiz(len);
2129 * Remember our original uid so that we can reset cr_uid before
2130 * the second nfs_namei() call, in case it is remapped.
2132 saved_uid = cred->cr_uid;
2133 error = nfs_namei(&fromnd, cred, NAMEI_DELETE, NULL, NULL,
2134 ffhp, len, slp, nam, &md, &dpos, &fdirp,
2135 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2138 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, td);
2141 nfsm_reply(2 * NFSX_WCCDATA(v3));
2142 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2143 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2149 * We have to unlock the from ncp before we can safely lookup
2152 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2153 cache_unlock(fromnd.nl_ncp);
2154 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2155 nfsm_srvmtofh(tfhp);
2156 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2157 cred->cr_uid = saved_uid;
2159 error = nfs_namei(&tond, cred, NAMEI_RENAME, NULL, NULL,
2160 tfhp, len2, slp, nam, &md, &dpos, &tdirp,
2161 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2164 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, td);
2172 if (cache_lock_nonblock(fromnd.nl_ncp) == 0) {
2173 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2174 } else if (fromnd.nl_ncp > tond.nl_ncp) {
2175 cache_lock(fromnd.nl_ncp);
2176 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2178 cache_unlock(tond.nl_ncp);
2179 cache_lock(fromnd.nl_ncp);
2180 cache_resolve(fromnd.nl_ncp, fromnd.nl_cred);
2181 cache_lock(tond.nl_ncp);
2182 cache_resolve(tond.nl_ncp, tond.nl_cred);
2184 fromnd.nl_flags |= NLC_NCPISLOCKED;
2186 tvp = tond.nl_ncp->nc_vp;
2187 fvp = fromnd.nl_ncp->nc_vp;
2190 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2196 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2203 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2211 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2218 if (fromnd.nl_ncp->nc_mount != tond.nl_ncp->nc_mount) {
2225 if (fromnd.nl_ncp == tond.nl_ncp->nc_parent) {
2233 * You cannot rename a source into itself or a subdirectory of itself.
2234 * We check this by travsering the target directory upwards looking
2235 * for a match against the source.
2238 for (ncp = tond.nl_ncp; ncp; ncp = ncp->nc_parent) {
2239 if (fromnd.nl_ncp == ncp) {
2247 * If source is the same as the destination (that is the
2248 * same vnode with the same name in the same directory),
2249 * then there is nothing to do.
2251 if (fromnd.nl_ncp == tond.nl_ncp)
2256 * The VOP_NRENAME function releases all vnode references &
2257 * locks prior to returning so we need to clear the pointers
2258 * to bypass cleanup code later on.
2261 /* XXX should be handled by NRENAME */
2262 nqsrv_getl(fdvp, ND_WRITE);
2263 nqsrv_getl(tdvp, ND_WRITE);
2265 nqsrv_getl(tvp, ND_WRITE);
2268 error = VOP_NRENAME(fromnd.nl_ncp, tond.nl_ncp, tond.nl_cred);
2277 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, td);
2279 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, td);
2280 nfsm_reply(2 * NFSX_WCCDATA(v3));
2282 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2283 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2291 nlookup_done(&tond);
2294 nlookup_done(&fromnd);
2302 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2303 struct thread *td, struct mbuf **mrq)
2305 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2306 struct sockaddr *nam = nfsd->nd_nam;
2307 caddr_t dpos = nfsd->nd_dpos;
2308 struct ucred *cred = &nfsd->nd_cr;
2309 struct nlookupdata nd;
2313 int error = 0, rdonly, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2314 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2316 struct mbuf *mb, *mreq;
2321 struct vattr dirfor, diraft, at;
2323 fhandle_t *fhp, *dfhp;
2326 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2328 dirp = dvp = vp = xp = NULL;
2330 fhp = &nfh.fh_generic;
2331 dfhp = &dnfh.fh_generic;
2333 nfsm_srvmtofh(dfhp);
2334 nfsm_srvnamesiz(len);
2336 error = nfsrv_fhtovp(fhp, FALSE, &xp, cred, slp, nam,
2337 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2339 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2340 nfsm_srvpostop_attr(getret, &at);
2341 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2346 if (xp->v_type == VDIR) {
2347 error = EPERM; /* POSIX */
2351 error = nfs_namei(&nd, cred, NAMEI_CREATE, &dvp, &vp,
2352 dfhp, len, slp, nam, &md, &dpos, &dirp,
2353 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2356 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2365 if (xp->v_mount != dvp->v_mount)
2370 nqsrv_getl(xp, ND_WRITE);
2371 nqsrv_getl(vp, ND_WRITE);
2373 error = VOP_NLINK(nd.nl_ncp, xp, nd.nl_cred);
2379 getret = VOP_GETATTR(xp, &at, td);
2381 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2382 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2384 nfsm_srvpostop_attr(getret, &at);
2385 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2408 * nfs symbolic link service
2411 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2412 struct thread *td, struct mbuf **mrq)
2414 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2415 struct sockaddr *nam = nfsd->nd_nam;
2416 caddr_t dpos = nfsd->nd_dpos;
2417 struct ucred *cred = &nfsd->nd_cr;
2418 struct vattr va, dirfor, diraft;
2419 struct nlookupdata nd;
2420 struct vattr *vap = &va;
2423 struct nfsv2_sattr *sp;
2424 char *bpos, *pathcp = (char *)0, *cp2;
2427 int error = 0, cache = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2428 int v3 = (nfsd->nd_flag & ND_NFSV3);
2429 struct mbuf *mb, *mreq, *mb2;
2436 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2440 fhp = &nfh.fh_generic;
2442 nfsm_srvnamesiz(len);
2444 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2445 fhp, len, slp, nam, &md, &dpos, &dirp,
2446 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2449 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2457 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2458 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2459 iv.iov_base = pathcp;
2461 io.uio_resid = len2;
2465 io.uio_segflg = UIO_SYSSPACE;
2466 io.uio_rw = UIO_READ;
2468 nfsm_mtouio(&io, len2);
2470 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2471 vap->va_mode = nfstov_mode(sp->sa_mode);
2473 *(pathcp + len2) = '\0';
2479 if (vap->va_mode == (mode_t)VNOVAL)
2482 nqsrv_getl(dvp, ND_WRITE);
2484 error = VOP_NSYMLINK(nd.nl_ncp, &vp, nd.nl_cred, vap, pathcp);
2486 bzero((caddr_t)fhp, sizeof(nfh));
2487 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2488 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2490 error = VOP_GETATTR(vp, vap, td);
2495 * We have a vp in hand from the new API call, we do not have to
2501 * Issue lookup. Leave SAVESTART set so we can easily free
2502 * the name buffer later on.
2504 * since LOCKPARENT is not set, ni_dvp will be garbage on
2505 * return whether an error occurs or not.
2507 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
2508 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT | CNP_FOLLOW);
2509 nd.ni_cnd.cn_td = td;
2510 nd.ni_cnd.cn_cred = cred;
2512 error = lookup(&nd);
2516 bzero((caddr_t)fhp, sizeof(nfh));
2517 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2518 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2520 error = VOP_GETATTR(nd.ni_vp, vap, td);
2533 FREE(pathcp, M_TEMP);
2537 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2541 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2544 nfsm_srvpostop_fh(fhp);
2545 nfsm_srvpostop_attr(0, vap);
2547 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2559 FREE(pathcp, M_TEMP);
2567 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2568 struct thread *td, struct mbuf **mrq)
2570 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2571 struct sockaddr *nam = nfsd->nd_nam;
2572 caddr_t dpos = nfsd->nd_dpos;
2573 struct ucred *cred = &nfsd->nd_cr;
2574 struct vattr va, dirfor, diraft;
2575 struct vattr *vap = &va;
2576 struct nfs_fattr *fp;
2577 struct nlookupdata nd;
2582 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2583 int v3 = (nfsd->nd_flag & ND_NFSV3);
2585 struct mbuf *mb, *mb2, *mreq;
2592 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2597 fhp = &nfh.fh_generic;
2599 nfsm_srvnamesiz(len);
2601 error = nfs_namei(&nd, cred, NAMEI_CREATE, NULL, &vp,
2602 fhp, len, slp, nam, &md, &dpos, &dirp,
2603 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2606 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2609 nfsm_reply(NFSX_WCCDATA(v3));
2610 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2618 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2619 vap->va_mode = nfstov_mode(*tl++);
2623 * At this point nd.ni_dvp is referenced and exclusively locked and
2624 * nd.ni_vp, if it exists, is referenced but not locked.
2627 vap->va_type = VDIR;
2634 * Issue mkdir op. Since SAVESTART is not set, the pathname
2635 * component is freed by the VOP call. This will fill-in
2636 * nd.ni_vp, reference, and exclusively lock it.
2638 if (vap->va_mode == (mode_t)VNOVAL)
2641 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2643 error = VOP_NMKDIR(nd.nl_ncp, &vp, nd.nl_cred, vap);
2646 bzero((caddr_t)fhp, sizeof(nfh));
2647 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2648 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2650 error = VOP_GETATTR(vp, vap, td);
2654 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2655 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2658 nfsm_srvpostop_fh(fhp);
2659 nfsm_srvpostop_attr(0, vap);
2661 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2663 nfsm_srvfhtom(fhp, v3);
2664 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2665 nfsm_srvfillattr(vap, fp);
2683 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2684 struct thread *td, struct mbuf **mrq)
2686 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2687 struct sockaddr *nam = nfsd->nd_nam;
2688 caddr_t dpos = nfsd->nd_dpos;
2689 struct ucred *cred = &nfsd->nd_cr;
2693 int error = 0, cache = 0, len, dirfor_ret = 1, diraft_ret = 1;
2694 int v3 = (nfsd->nd_flag & ND_NFSV3);
2696 struct mbuf *mb, *mreq;
2699 struct vattr dirfor, diraft;
2702 struct nlookupdata nd;
2705 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2710 fhp = &nfh.fh_generic;
2712 nfsm_srvnamesiz(len);
2714 error = nfs_namei(&nd, cred, NAMEI_DELETE, NULL, &vp,
2715 fhp, len, slp, nam, &md, &dpos, &dirp,
2716 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2719 dirfor_ret = VOP_GETATTR(dirp, &dirfor, td);
2722 nfsm_reply(NFSX_WCCDATA(v3));
2723 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2727 if (vp->v_type != VDIR) {
2733 * The root of a mounted filesystem cannot be deleted.
2735 if (vp->v_flag & VROOT)
2739 * Issue or abort op. Since SAVESTART is not set, path name
2740 * component is freed by the VOP after either.
2744 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2745 nqsrv_getl(vp, ND_WRITE);
2749 error = VOP_NRMDIR(nd.nl_ncp, nd.nl_cred);
2754 diraft_ret = VOP_GETATTR(dirp, &diraft, td);
2755 nfsm_reply(NFSX_WCCDATA(v3));
2757 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2772 * nfs readdir service
2773 * - mallocs what it thinks is enough to read
2774 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2775 * - calls VOP_READDIR()
2776 * - loops around building the reply
2777 * if the output generated exceeds count break out of loop
2778 * The nfsm_clget macro is used here so that the reply will be packed
2779 * tightly in mbuf clusters.
2780 * - it only knows that it has encountered eof when the VOP_READDIR()
2782 * - as such one readdir rpc will return eof false although you are there
2783 * and then the next will return eof
2784 * - it trims out records with d_fileno == 0
2785 * this doesn't matter for Unix clients, but they might confuse clients
2787 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2788 * than requested, but this may not apply to all filesystems. For
2789 * example, client NFS does not { although it is never remote mounted
2791 * The alternate call nfsrv_readdirplus() does lookups as well.
2792 * PS: The NFS protocol spec. does not clarify what the "count" byte
2793 * argument is a count of.. just name strings and file id's or the
2794 * entire reply rpc or ...
2795 * I tried just file name and id sizes and it confused the Sun client,
2796 * so I am using the full rpc size now. The "paranoia.." comment refers
2797 * to including the status longwords that are not a part of the dir.
2798 * "entry" structures, but are in the rpc.
2802 u_int32_t fl_postopok;
2803 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2805 u_int32_t fl_fhsize;
2806 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2810 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2811 struct thread *td, struct mbuf **mrq)
2813 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2814 struct sockaddr *nam = nfsd->nd_nam;
2815 caddr_t dpos = nfsd->nd_dpos;
2816 struct ucred *cred = &nfsd->nd_cr;
2824 struct mbuf *mb, *mb2, *mreq, *mp2;
2825 char *cpos, *cend, *cp2, *rbuf;
2826 struct vnode *vp = NULL;
2832 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2833 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2834 int v3 = (nfsd->nd_flag & ND_NFSV3);
2835 u_quad_t frev, off, toff, verf;
2836 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2838 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2839 fhp = &nfh.fh_generic;
2842 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2843 toff = fxdr_hyper(tl);
2845 verf = fxdr_hyper(tl);
2848 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2849 toff = fxdr_unsigned(u_quad_t, *tl++);
2850 verf = 0; /* shut up gcc */
2853 cnt = fxdr_unsigned(int, *tl);
2854 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2855 xfer = NFS_SRVMAXDATA(nfsd);
2861 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2862 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2863 if (!error && vp->v_type != VDIR) {
2869 nfsm_reply(NFSX_UNSIGNED);
2870 nfsm_srvpostop_attr(getret, &at);
2876 * Obtain lock on vnode for this section of the code
2879 nqsrv_getl(vp, ND_READ);
2881 error = getret = VOP_GETATTR(vp, &at, td);
2884 * XXX This check may be too strict for Solaris 2.5 clients.
2886 if (!error && toff && verf && verf != at.va_filerev)
2887 error = NFSERR_BAD_COOKIE;
2891 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
2895 nfsm_reply(NFSX_POSTOPATTR(v3));
2896 nfsm_srvpostop_attr(getret, &at);
2900 VOP_UNLOCK(vp, 0, td);
2903 * end section. Allocate rbuf and continue
2905 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2908 iv.iov_len = fullsiz;
2911 io.uio_offset = (off_t)off;
2912 io.uio_resid = fullsiz;
2913 io.uio_segflg = UIO_SYSSPACE;
2914 io.uio_rw = UIO_READ;
2917 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2919 free((caddr_t)cookies, M_TEMP);
2922 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2923 off = (off_t)io.uio_offset;
2924 if (!cookies && !error)
2925 error = NFSERR_PERM;
2927 getret = VOP_GETATTR(vp, &at, td);
2931 VOP_UNLOCK(vp, 0, td);
2935 free((caddr_t)rbuf, M_TEMP);
2937 free((caddr_t)cookies, M_TEMP);
2938 nfsm_reply(NFSX_POSTOPATTR(v3));
2939 nfsm_srvpostop_attr(getret, &at);
2944 siz -= io.uio_resid;
2947 * If nothing read, return eof
2953 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2956 nfsm_srvpostop_attr(getret, &at);
2957 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2958 txdr_hyper(at.va_filerev, tl);
2961 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2964 FREE((caddr_t)rbuf, M_TEMP);
2965 FREE((caddr_t)cookies, M_TEMP);
2972 * Check for degenerate cases of nothing useful read.
2973 * If so go try again
2977 dp = (struct dirent *)cpos;
2980 * For some reason FreeBSD's ufs_readdir() chooses to back the
2981 * directory offset up to a block boundary, so it is necessary to
2982 * skip over the records that preceed the requested offset. This
2983 * requires the assumption that file offset cookies monotonically
2986 while (cpos < cend && ncookies > 0 &&
2987 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2988 ((u_quad_t)(*cookiep)) <= toff)) {
2989 cpos += dp->d_reclen;
2990 dp = (struct dirent *)cpos;
2994 if (cpos >= cend || ncookies == 0) {
3000 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3001 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3003 nfsm_srvpostop_attr(getret, &at);
3004 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3005 txdr_hyper(at.va_filerev, tl);
3009 be = bp + M_TRAILINGSPACE(mp);
3011 /* Loop through the records and build reply */
3012 while (cpos < cend && ncookies > 0) {
3013 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3014 nlen = dp->d_namlen;
3015 rem = nfsm_rndup(nlen) - nlen;
3016 len += (4 * NFSX_UNSIGNED + nlen + rem);
3018 len += 2 * NFSX_UNSIGNED;
3024 * Build the directory record xdr from
3029 bp += NFSX_UNSIGNED;
3033 bp += NFSX_UNSIGNED;
3036 *tl = txdr_unsigned(dp->d_fileno);
3037 bp += NFSX_UNSIGNED;
3039 *tl = txdr_unsigned(nlen);
3040 bp += NFSX_UNSIGNED;
3042 /* And loop around copying the name */
3051 bcopy(cp, bp, tsiz);
3057 /* And null pad to a int32_t boundary */
3058 for (i = 0; i < rem; i++)
3062 /* Finish off the record */
3065 bp += NFSX_UNSIGNED;
3068 *tl = txdr_unsigned(*cookiep);
3069 bp += NFSX_UNSIGNED;
3071 cpos += dp->d_reclen;
3072 dp = (struct dirent *)cpos;
3080 bp += NFSX_UNSIGNED;
3086 bp += NFSX_UNSIGNED;
3089 mp->m_len = bp - mtod(mp, caddr_t);
3091 mp->m_len += bp - bpos;
3092 FREE((caddr_t)rbuf, M_TEMP);
3093 FREE((caddr_t)cookies, M_TEMP);
3102 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3103 struct thread *td, struct mbuf **mrq)
3105 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3106 struct sockaddr *nam = nfsd->nd_nam;
3107 caddr_t dpos = nfsd->nd_dpos;
3108 struct ucred *cred = &nfsd->nd_cr;
3116 struct mbuf *mb, *mb2, *mreq, *mp2;
3117 char *cpos, *cend, *cp2, *rbuf;
3118 struct vnode *vp = NULL, *nvp;
3121 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3124 struct vattr va, at, *vap = &va;
3125 struct nfs_fattr *fp;
3126 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3127 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3128 u_quad_t frev, off, toff, verf;
3129 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3131 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3132 fhp = &nfh.fh_generic;
3134 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3135 toff = fxdr_hyper(tl);
3137 verf = fxdr_hyper(tl);
3139 siz = fxdr_unsigned(int, *tl++);
3140 cnt = fxdr_unsigned(int, *tl);
3142 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3143 xfer = NFS_SRVMAXDATA(nfsd);
3149 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3150 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3151 if (!error && vp->v_type != VDIR) {
3157 nfsm_reply(NFSX_UNSIGNED);
3158 nfsm_srvpostop_attr(getret, &at);
3162 error = getret = VOP_GETATTR(vp, &at, td);
3165 * XXX This check may be too strict for Solaris 2.5 clients.
3167 if (!error && toff && verf && verf != at.va_filerev)
3168 error = NFSERR_BAD_COOKIE;
3171 nqsrv_getl(vp, ND_READ);
3172 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3177 nfsm_reply(NFSX_V3POSTOPATTR);
3178 nfsm_srvpostop_attr(getret, &at);
3182 VOP_UNLOCK(vp, 0, td);
3183 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3186 iv.iov_len = fullsiz;
3189 io.uio_offset = (off_t)off;
3190 io.uio_resid = fullsiz;
3191 io.uio_segflg = UIO_SYSSPACE;
3192 io.uio_rw = UIO_READ;
3195 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3197 free((caddr_t)cookies, M_TEMP);
3200 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3201 off = (u_quad_t)io.uio_offset;
3202 getret = VOP_GETATTR(vp, &at, td);
3203 VOP_UNLOCK(vp, 0, td);
3204 if (!cookies && !error)
3205 error = NFSERR_PERM;
3212 free((caddr_t)cookies, M_TEMP);
3213 free((caddr_t)rbuf, M_TEMP);
3214 nfsm_reply(NFSX_V3POSTOPATTR);
3215 nfsm_srvpostop_attr(getret, &at);
3220 siz -= io.uio_resid;
3223 * If nothing read, return eof
3229 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3231 nfsm_srvpostop_attr(getret, &at);
3232 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3233 txdr_hyper(at.va_filerev, tl);
3237 FREE((caddr_t)cookies, M_TEMP);
3238 FREE((caddr_t)rbuf, M_TEMP);
3245 * Check for degenerate cases of nothing useful read.
3246 * If so go try again
3250 dp = (struct dirent *)cpos;
3253 * For some reason FreeBSD's ufs_readdir() chooses to back the
3254 * directory offset up to a block boundary, so it is necessary to
3255 * skip over the records that preceed the requested offset. This
3256 * requires the assumption that file offset cookies monotonically
3259 while (cpos < cend && ncookies > 0 &&
3260 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3261 ((u_quad_t)(*cookiep)) <= toff)) {
3262 cpos += dp->d_reclen;
3263 dp = (struct dirent *)cpos;
3267 if (cpos >= cend || ncookies == 0) {
3274 * Probe one of the directory entries to see if the filesystem
3277 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3278 error = NFSERR_NOTSUPP;
3281 free((caddr_t)cookies, M_TEMP);
3282 free((caddr_t)rbuf, M_TEMP);
3283 nfsm_reply(NFSX_V3POSTOPATTR);
3284 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 */