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.5 2003/06/25 03:56:07 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
65 * lookup() and namei()
66 * may return garbage in various structural fields/return elements
67 * if an error is returned, and may garbage up nd.ni_dvp even if no
68 * error is returned and you did not request LOCKPARENT or WANTPARENT.
70 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
71 * buffer has been freed or not.
74 #include <sys/param.h>
75 #include <sys/systm.h>
77 #include <sys/namei.h>
78 #include <sys/unistd.h>
79 #include <sys/vnode.h>
80 #include <sys/mount.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/malloc.h>
85 #include <sys/dirent.h>
87 #include <sys/kernel.h>
88 #include <sys/sysctl.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_zone.h>
94 #include <vm/vm_object.h>
98 #include <nfs/nfsproto.h>
99 #include <nfs/rpcv2.h>
101 #include <nfs/xdr_subs.h>
102 #include <nfs/nfsm_subs.h>
103 #include <nfs/nqnfs.h>
106 #define nfsdbprintf(info) printf info
108 #define nfsdbprintf(info)
111 #define MAX_COMMIT_COUNT (1024 * 1024)
113 #define NUM_HEURISTIC 64
114 #define NHUSE_INIT 64
116 #define NHUSE_MAX 2048
118 static struct nfsheur {
119 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
120 off_t nh_nextr; /* next offset for sequential detection */
121 int nh_use; /* use count for selection */
122 int nh_seqcount; /* heuristic */
123 } nfsheur[NUM_HEURISTIC];
125 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
128 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
131 extern u_int32_t nfs_xdrneg1;
132 extern u_int32_t nfs_false, nfs_true;
133 extern enum vtype nv3tov_type[8];
134 extern struct nfsstats nfsstats;
136 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
137 int nfsrvw_procrastinate_v3 = 0;
139 static struct timeval nfsver = { 0 };
141 SYSCTL_DECL(_vfs_nfs);
143 static int nfs_async;
144 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
145 static int nfs_commit_blks;
146 static int nfs_commit_miss;
147 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
148 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
150 static int nfsrv_access __P((struct vnode *,int,struct ucred *,int,
151 struct thread *, int));
152 static void nfsrvw_coalesce __P((struct nfsrv_descript *,
153 struct nfsrv_descript *));
156 * Clear nameidata fields that are tested in nsfmout cleanup code prior
157 * to using first nfsm macro (that might jump to the cleanup code).
162 ndclear(struct nameidata *nd)
164 nd->ni_cnd.cn_flags = 0;
167 nd->ni_startdir = NULL;
171 * nfs v3 access service
174 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
175 struct thread *td, struct mbuf **mrq)
177 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
178 struct sockaddr *nam = nfsd->nd_nam;
179 caddr_t dpos = nfsd->nd_dpos;
180 struct ucred *cred = &nfsd->nd_cr;
181 struct vnode *vp = NULL;
187 int error = 0, rdonly, cache, getret;
189 struct mbuf *mb, *mreq, *mb2;
190 struct vattr vattr, *vap = &vattr;
191 u_long testmode, nfsmode;
194 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
198 fhp = &nfh.fh_generic;
200 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
201 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
202 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
204 nfsm_reply(NFSX_UNSIGNED);
205 nfsm_srvpostop_attr(1, (struct vattr *)0);
209 nfsmode = fxdr_unsigned(u_int32_t, *tl);
210 if ((nfsmode & NFSV3ACCESS_READ) &&
211 nfsrv_access(vp, VREAD, cred, rdonly, td, 0))
212 nfsmode &= ~NFSV3ACCESS_READ;
213 if (vp->v_type == VDIR)
214 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
217 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
218 if ((nfsmode & testmode) &&
219 nfsrv_access(vp, VWRITE, cred, rdonly, td, 0))
220 nfsmode &= ~testmode;
221 if (vp->v_type == VDIR)
222 testmode = NFSV3ACCESS_LOOKUP;
224 testmode = NFSV3ACCESS_EXECUTE;
225 if ((nfsmode & testmode) &&
226 nfsrv_access(vp, VEXEC, cred, rdonly, td, 0))
227 nfsmode &= ~testmode;
228 getret = VOP_GETATTR(vp, vap, cred, td);
231 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
232 nfsm_srvpostop_attr(getret, vap);
233 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
234 *tl = txdr_unsigned(nfsmode);
242 * nfs getattr service
245 nfsrv_getattr(nfsd, slp, td, mrq)
246 struct nfsrv_descript *nfsd;
247 struct nfssvc_sock *slp;
251 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
252 struct sockaddr *nam = nfsd->nd_nam;
253 caddr_t dpos = nfsd->nd_dpos;
254 struct ucred *cred = &nfsd->nd_cr;
255 struct nfs_fattr *fp;
257 struct vattr *vap = &va;
258 struct vnode *vp = NULL;
264 int error = 0, rdonly, cache;
266 struct mbuf *mb, *mb2, *mreq;
269 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
270 fhp = &nfh.fh_generic;
272 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
273 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
279 nqsrv_getl(vp, ND_READ);
280 error = VOP_GETATTR(vp, vap, cred, td);
283 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
288 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
289 nfsm_srvfillattr(vap, fp);
299 * nfs setattr service
302 nfsrv_setattr(nfsd, slp, td, mrq)
303 struct nfsrv_descript *nfsd;
304 struct nfssvc_sock *slp;
308 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
309 struct sockaddr *nam = nfsd->nd_nam;
310 caddr_t dpos = nfsd->nd_dpos;
311 struct ucred *cred = &nfsd->nd_cr;
312 struct vattr va, preat;
313 struct vattr *vap = &va;
314 struct nfsv2_sattr *sp;
315 struct nfs_fattr *fp;
316 struct vnode *vp = NULL;
322 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
323 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
325 struct mbuf *mb, *mb2, *mreq;
327 struct timespec guard;
329 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
330 fhp = &nfh.fh_generic;
335 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
336 gcheck = fxdr_unsigned(int, *tl);
338 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
339 fxdr_nfsv3time(tl, &guard);
342 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
344 * Nah nah nah nah na nah
345 * There is a bug in the Sun client that puts 0xffff in the mode
346 * field of sattr when it should put in 0xffffffff. The u_short
347 * doesn't sign extend.
348 * --> check the low order 2 bytes for 0xffff
350 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
351 vap->va_mode = nfstov_mode(sp->sa_mode);
352 if (sp->sa_uid != nfs_xdrneg1)
353 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
354 if (sp->sa_gid != nfs_xdrneg1)
355 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
356 if (sp->sa_size != nfs_xdrneg1)
357 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
358 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
360 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
362 vap->va_atime.tv_sec =
363 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
364 vap->va_atime.tv_nsec = 0;
367 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
368 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
373 * Now that we have all the fields, lets do it.
375 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
376 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
378 nfsm_reply(2 * NFSX_UNSIGNED);
379 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
385 * vp now an active resource, pay careful attention to cleanup
388 nqsrv_getl(vp, ND_WRITE);
390 error = preat_ret = VOP_GETATTR(vp, &preat, cred, td);
391 if (!error && gcheck &&
392 (preat.va_ctime.tv_sec != guard.tv_sec ||
393 preat.va_ctime.tv_nsec != guard.tv_nsec))
394 error = NFSERR_NOT_SYNC;
398 nfsm_reply(NFSX_WCCDATA(v3));
399 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
406 * If the size is being changed write acces is required, otherwise
407 * just check for a read only file system.
409 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
410 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
415 if (vp->v_type == VDIR) {
418 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
423 error = VOP_SETATTR(vp, vap, cred, td);
424 postat_ret = VOP_GETATTR(vp, vap, cred, td);
430 nfsm_reply(NFSX_WCCORFATTR(v3));
432 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
436 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
437 nfsm_srvfillattr(vap, fp);
451 nfsrv_lookup(nfsd, slp, td, mrq)
452 struct nfsrv_descript *nfsd;
453 struct nfssvc_sock *slp;
457 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
458 struct sockaddr *nam = nfsd->nd_nam;
459 caddr_t dpos = nfsd->nd_dpos;
460 struct ucred *cred = &nfsd->nd_cr;
461 struct nfs_fattr *fp;
462 struct nameidata nd, ind, *ndp = &nd;
463 struct vnode *vp, *dirp = NULL;
470 int error = 0, cache, len, dirattr_ret = 1;
471 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
473 struct mbuf *mb, *mb2, *mreq;
474 struct vattr va, dirattr, *vap = &va;
477 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
480 fhp = &nfh.fh_generic;
482 nfsm_srvnamesiz(len);
484 pubflag = nfs_ispublicfh(fhp);
486 nd.ni_cnd.cn_cred = cred;
487 nd.ni_cnd.cn_nameiop = LOOKUP;
488 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
489 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
490 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
493 * namei failure, only dirp to cleanup. Clear out garbarge from
494 * structure in case macros jump to nfsmout.
500 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
505 nfsm_reply(NFSX_POSTOPATTR(v3));
506 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
512 * Locate index file for public filehandle
514 * error is 0 on entry and 0 on exit from this block.
518 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
520 * Setup call to lookup() to see if we can find
521 * the index file. Arguably, this doesn't belong
522 * in a kernel.. Ugh. If an error occurs, do not
523 * try to install an index file and then clear the
526 * When we replace nd with ind and redirect ndp,
527 * maintenance of ni_startdir and ni_vp shift to
528 * ind and we have to clean them up in the old nd.
529 * However, the cnd resource continues to be maintained
530 * via the original nd. Confused? You aren't alone!
533 VOP_UNLOCK(nd.ni_vp, 0, td);
534 ind.ni_pathlen = strlen(nfs_pub.np_index);
535 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
537 ind.ni_startdir = nd.ni_vp;
538 VREF(ind.ni_startdir);
540 error = lookup(&ind);
545 * Found an index file. Get rid of
546 * the old references. transfer nd.ni_vp'
552 vrele(nd.ni_startdir);
553 nd.ni_startdir = NULL;
559 * If the public filehandle was used, check that this lookup
560 * didn't result in a filehandle outside the publicly exported
561 * filesystem. We clear the poor vp here to avoid lockups due
565 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
574 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
581 * Resources at this point:
582 * ndp->ni_vp may not be NULL
587 nfsm_reply(NFSX_POSTOPATTR(v3));
588 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
593 nqsrv_getl(ndp->ni_startdir, ND_READ);
596 * Clear out some resources prior to potentially blocking. This
597 * is not as critical as ni_dvp resources in other routines, but
600 vrele(ndp->ni_startdir);
601 ndp->ni_startdir = NULL;
602 NDFREE(&nd, NDF_ONLY_PNBUF);
605 * Get underlying attribute, then release remaining resources ( for
606 * the same potential blocking reason ) and reply.
609 bzero((caddr_t)fhp, sizeof(nfh));
610 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
611 error = VFS_VPTOFH(vp, &fhp->fh_fid);
613 error = VOP_GETATTR(vp, vap, cred, td);
617 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
619 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
623 nfsm_srvfhtom(fhp, v3);
625 nfsm_srvpostop_attr(0, vap);
626 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
628 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
629 nfsm_srvfillattr(vap, fp);
635 NDFREE(&nd, NDF_ONLY_PNBUF);
636 if (ndp->ni_startdir)
637 vrele(ndp->ni_startdir);
644 * nfs readlink service
647 nfsrv_readlink(nfsd, slp, td, mrq)
648 struct nfsrv_descript *nfsd;
649 struct nfssvc_sock *slp;
653 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
654 struct sockaddr *nam = nfsd->nd_nam;
655 caddr_t dpos = nfsd->nd_dpos;
656 struct ucred *cred = &nfsd->nd_cr;
657 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
658 struct iovec *ivp = iv;
663 int error = 0, rdonly, cache, i, tlen, len, getret;
664 int v3 = (nfsd->nd_flag & ND_NFSV3);
666 struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
667 struct vnode *vp = NULL;
671 struct uio io, *uiop = &io;
674 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
676 mp2 = (struct mbuf *)0;
679 fhp = &nfh.fh_generic;
683 while (len < NFS_MAXPATHLEN) {
684 MGET(mp, M_WAIT, MT_DATA);
686 mp->m_len = NFSMSIZ(mp);
693 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
694 mp->m_len = NFS_MAXPATHLEN-len;
695 len = NFS_MAXPATHLEN;
698 ivp->iov_base = mtod(mp, caddr_t);
699 ivp->iov_len = mp->m_len;
704 uiop->uio_iovcnt = i;
705 uiop->uio_offset = 0;
706 uiop->uio_resid = len;
707 uiop->uio_rw = UIO_READ;
708 uiop->uio_segflg = UIO_SYSSPACE;
710 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
711 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
713 nfsm_reply(2 * NFSX_UNSIGNED);
714 nfsm_srvpostop_attr(1, (struct vattr *)0);
718 if (vp->v_type != VLNK) {
725 nqsrv_getl(vp, ND_READ);
726 error = VOP_READLINK(vp, uiop, cred);
728 getret = VOP_GETATTR(vp, &attr, cred, td);
731 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
733 nfsm_srvpostop_attr(getret, &attr);
739 if (uiop->uio_resid > 0) {
740 len -= uiop->uio_resid;
741 tlen = nfsm_rndup(len);
742 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
744 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
745 *tl = txdr_unsigned(len);
760 nfsrv_read(nfsd, slp, td, mrq)
761 struct nfsrv_descript *nfsd;
762 struct nfssvc_sock *slp;
766 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
767 struct sockaddr *nam = nfsd->nd_nam;
768 caddr_t dpos = nfsd->nd_dpos;
769 struct ucred *cred = &nfsd->nd_cr;
773 struct nfs_fattr *fp;
778 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
779 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
781 struct mbuf *mb, *mb2, *mreq;
783 struct vnode *vp = NULL;
786 struct uio io, *uiop = &io;
787 struct vattr va, *vap = &va;
793 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
794 fhp = &nfh.fh_generic;
797 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
798 off = fxdr_hyper(tl);
800 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
801 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
803 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
806 * Reference vp. If an error occurs, vp will be invalid, but we
807 * have to NULL it just in case. The macros might goto nfsmout
811 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
812 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
815 nfsm_reply(2 * NFSX_UNSIGNED);
816 nfsm_srvpostop_attr(1, (struct vattr *)0);
821 if (vp->v_type != VREG) {
825 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
828 nqsrv_getl(vp, ND_READ);
829 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, td, 1)) != 0)
830 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 1);
832 getret = VOP_GETATTR(vp, vap, cred, td);
838 nfsm_reply(NFSX_POSTOPATTR(v3));
839 nfsm_srvpostop_attr(getret, vap);
845 * Calculate byte count to read
848 if (off >= vap->va_size)
850 else if ((off + reqlen) > vap->va_size)
851 cnt = vap->va_size - off;
856 * Calculate seqcount for heuristic
864 * Locate best candidate
867 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) & (NUM_HEURISTIC - 1);
871 if (nfsheur[hi].nh_vp == vp) {
875 if (nfsheur[hi].nh_use > 0)
876 --nfsheur[hi].nh_use;
877 hi = (hi + 1) & (NUM_HEURISTIC - 1);
878 if (nfsheur[hi].nh_use < nh->nh_use)
882 if (nh->nh_vp != vp) {
885 nh->nh_use = NHUSE_INIT;
893 * Calculate heuristic
896 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
897 if (++nh->nh_seqcount > IO_SEQMAX)
898 nh->nh_seqcount = IO_SEQMAX;
899 } else if (nh->nh_seqcount > 1) {
904 nh->nh_use += NHUSE_INC;
905 if (nh->nh_use > NHUSE_MAX)
906 nh->nh_use = NHUSE_MAX;
907 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
910 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
912 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
914 fp = (struct nfs_fattr *)tl;
915 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
917 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
918 fp = (struct nfs_fattr *)tl;
919 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
921 len = left = nfsm_rndup(cnt);
924 * Generate the mbuf list with the uio_iov ref. to it.
929 siz = min(M_TRAILINGSPACE(m), left);
935 MGET(m, M_WAIT, MT_DATA);
942 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
944 uiop->uio_iov = iv2 = iv;
950 panic("nfsrv_read iov");
951 siz = min(M_TRAILINGSPACE(m), left);
953 iv->iov_base = mtod(m, caddr_t) + m->m_len;
962 uiop->uio_iovcnt = i;
963 uiop->uio_offset = off;
964 uiop->uio_resid = len;
965 uiop->uio_rw = UIO_READ;
966 uiop->uio_segflg = UIO_SYSSPACE;
967 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
968 off = uiop->uio_offset;
970 FREE((caddr_t)iv2, M_TEMP);
971 if (error || (getret = VOP_GETATTR(vp, vap, cred, td))) {
977 nfsm_reply(NFSX_POSTOPATTR(v3));
978 nfsm_srvpostop_attr(getret, vap);
987 nfsm_srvfillattr(vap, fp);
988 tlen = len - uiop->uio_resid;
989 cnt = cnt < tlen ? cnt : tlen;
990 tlen = nfsm_rndup(cnt);
991 if (len != tlen || tlen != cnt)
992 nfsm_adj(mb, len - tlen, tlen - cnt);
994 *tl++ = txdr_unsigned(cnt);
1000 *tl = txdr_unsigned(cnt);
1011 nfsrv_write(nfsd, slp, td, mrq)
1012 struct nfsrv_descript *nfsd;
1013 struct nfssvc_sock *slp;
1017 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1018 struct sockaddr *nam = nfsd->nd_nam;
1019 caddr_t dpos = nfsd->nd_dpos;
1020 struct ucred *cred = &nfsd->nd_cr;
1024 struct nfs_fattr *fp;
1026 struct vattr va, forat;
1027 struct vattr *vap = &va;
1031 int error = 0, rdonly, cache, len, forat_ret = 1;
1032 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1033 int stable = NFSV3WRITE_FILESYNC;
1034 int v3 = (nfsd->nd_flag & ND_NFSV3);
1036 struct mbuf *mb, *mb2, *mreq;
1037 struct vnode *vp = NULL;
1040 struct uio io, *uiop = &io;
1044 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1050 fhp = &nfh.fh_generic;
1053 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1054 off = fxdr_hyper(tl);
1056 stable = fxdr_unsigned(int, *tl++);
1058 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1059 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1062 stable = NFSV3WRITE_UNSTABLE;
1064 retlen = len = fxdr_unsigned(int32_t, *tl);
1068 * For NFS Version 2, it is not obvious what a write of zero length
1069 * should do, but I might as well be consistent with Version 3,
1070 * which is to return ok so long as there are no permission problems.
1078 adjust = dpos - mtod(mp, caddr_t);
1079 mp->m_len -= adjust;
1080 if (mp->m_len > 0 && adjust > 0)
1081 NFSMADV(mp, adjust);
1085 else if (mp->m_len > 0) {
1088 mp->m_len -= (i - len);
1097 if (len > NFS_MAXDATA || len < 0 || i < len) {
1099 nfsm_reply(2 * NFSX_UNSIGNED);
1100 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1104 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
1105 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1108 nfsm_reply(2 * NFSX_UNSIGNED);
1109 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1114 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1115 if (vp->v_type != VREG) {
1119 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1122 nqsrv_getl(vp, ND_WRITE);
1123 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1128 nfsm_reply(NFSX_WCCDATA(v3));
1129 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1135 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1137 uiop->uio_iov = iv = ivp;
1138 uiop->uio_iovcnt = cnt;
1141 if (mp->m_len > 0) {
1142 ivp->iov_base = mtod(mp, caddr_t);
1143 ivp->iov_len = mp->m_len;
1151 * The IO_METASYNC flag indicates that all metadata (and not just
1152 * enough to ensure data integrity) mus be written to stable storage
1154 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1156 if (stable == NFSV3WRITE_UNSTABLE)
1157 ioflags = IO_NODELOCKED;
1158 else if (stable == NFSV3WRITE_DATASYNC)
1159 ioflags = (IO_SYNC | IO_NODELOCKED);
1161 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1162 uiop->uio_resid = len;
1163 uiop->uio_rw = UIO_WRITE;
1164 uiop->uio_segflg = UIO_SYSSPACE;
1165 uiop->uio_td = NULL;
1166 uiop->uio_offset = off;
1167 error = VOP_WRITE(vp, uiop, ioflags, cred);
1168 nfsstats.srvvop_writes++;
1169 FREE((caddr_t)iv, M_TEMP);
1171 aftat_ret = VOP_GETATTR(vp, vap, cred, td);
1176 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1177 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1179 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1184 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1185 *tl++ = txdr_unsigned(retlen);
1187 * If nfs_async is set, then pretend the write was FILESYNC.
1189 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1190 *tl++ = txdr_unsigned(stable);
1192 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1194 * Actually, there is no need to txdr these fields,
1195 * but it may make the values more human readable,
1196 * for debugging purposes.
1198 if (nfsver.tv_sec == 0)
1200 *tl++ = txdr_unsigned(nfsver.tv_sec);
1201 *tl = txdr_unsigned(nfsver.tv_usec);
1203 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1204 nfsm_srvfillattr(vap, fp);
1213 * NFS write service with write gathering support. Called when
1214 * nfsrvw_procrastinate > 0.
1215 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1216 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1220 nfsrv_writegather(ndp, slp, td, mrq)
1221 struct nfsrv_descript **ndp;
1222 struct nfssvc_sock *slp;
1228 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1229 struct nfs_fattr *fp;
1232 struct nfsrvw_delayhash *wpp;
1234 struct vattr va, forat;
1238 int error = 0, rdonly, cache, len, forat_ret = 1;
1239 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
1241 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
1242 struct vnode *vp = NULL;
1243 struct uio io, *uiop = &io;
1244 u_quad_t frev, cur_usec;
1246 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1255 mrep = nfsd->nd_mrep;
1257 dpos = nfsd->nd_dpos;
1258 cred = &nfsd->nd_cr;
1259 v3 = (nfsd->nd_flag & ND_NFSV3);
1260 LIST_INIT(&nfsd->nd_coalesce);
1261 nfsd->nd_mreq = NULL;
1262 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1263 cur_usec = nfs_curusec();
1264 nfsd->nd_time = cur_usec +
1265 (v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1268 * Now, get the write header..
1270 nfsm_srvmtofh(&nfsd->nd_fh);
1272 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1273 nfsd->nd_off = fxdr_hyper(tl);
1275 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1277 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1278 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1281 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1283 len = fxdr_unsigned(int32_t, *tl);
1285 nfsd->nd_eoff = nfsd->nd_off + len;
1288 * Trim the header out of the mbuf list and trim off any trailing
1289 * junk so that the mbuf list has only the write data.
1297 adjust = dpos - mtod(mp, caddr_t);
1298 mp->m_len -= adjust;
1299 if (mp->m_len > 0 && adjust > 0)
1300 NFSMADV(mp, adjust);
1307 mp->m_len -= (i - len);
1313 if (len > NFS_MAXDATA || len < 0 || i < len) {
1317 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1319 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1320 nfsd->nd_mreq = mreq;
1321 nfsd->nd_mrep = NULL;
1326 * Add this entry to the hash and time queues.
1330 wp = slp->ns_tq.lh_first;
1331 while (wp && wp->nd_time < nfsd->nd_time) {
1333 wp = wp->nd_tq.le_next;
1335 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1337 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1339 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1341 if (nfsd->nd_mrep) {
1342 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1346 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1348 wp = wp->nd_hash.le_next;
1350 while (wp && wp->nd_off < nfsd->nd_off &&
1351 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1353 wp = wp->nd_hash.le_next;
1356 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1359 * Search the hash list for overlapping entries and
1362 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1363 wp = nfsd->nd_hash.le_next;
1364 if (NFSW_SAMECRED(owp, nfsd))
1365 nfsrvw_coalesce(owp, nfsd);
1368 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1375 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1376 * and generate the associated reply mbuf list(s).
1379 cur_usec = nfs_curusec();
1381 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1382 owp = nfsd->nd_tq.le_next;
1383 if (nfsd->nd_time > cur_usec)
1387 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1388 LIST_REMOVE(nfsd, nd_tq);
1389 LIST_REMOVE(nfsd, nd_hash);
1391 mrep = nfsd->nd_mrep;
1392 nfsd->nd_mrep = NULL;
1393 cred = &nfsd->nd_cr;
1394 v3 = (nfsd->nd_flag & ND_NFSV3);
1395 forat_ret = aftat_ret = 1;
1396 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1397 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1400 forat_ret = VOP_GETATTR(vp, &forat, cred, td);
1401 if (vp->v_type != VREG) {
1405 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1411 nqsrv_getl(vp, ND_WRITE);
1412 error = nfsrv_access(vp, VWRITE, cred, rdonly, td, 1);
1415 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1416 ioflags = IO_NODELOCKED;
1417 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1418 ioflags = (IO_SYNC | IO_NODELOCKED);
1420 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1421 uiop->uio_rw = UIO_WRITE;
1422 uiop->uio_segflg = UIO_SYSSPACE;
1423 uiop->uio_td = NULL;
1424 uiop->uio_offset = nfsd->nd_off;
1425 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1426 if (uiop->uio_resid > 0) {
1434 uiop->uio_iovcnt = i;
1435 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1437 uiop->uio_iov = ivp = iov;
1440 if (mp->m_len > 0) {
1441 ivp->iov_base = mtod(mp, caddr_t);
1442 ivp->iov_len = mp->m_len;
1448 error = VOP_WRITE(vp, uiop, ioflags, cred);
1449 nfsstats.srvvop_writes++;
1451 FREE((caddr_t)iov, M_TEMP);
1455 aftat_ret = VOP_GETATTR(vp, &va, cred, td);
1461 * Loop around generating replies for all write rpcs that have
1462 * now been completed.
1466 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1468 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1470 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1473 nfsm_writereply(NFSX_PREOPATTR(v3) +
1474 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1475 NFSX_WRITEVERF(v3), v3);
1477 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1478 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1479 *tl++ = txdr_unsigned(nfsd->nd_len);
1480 *tl++ = txdr_unsigned(swp->nd_stable);
1482 * Actually, there is no need to txdr these fields,
1483 * but it may make the values more human readable,
1484 * for debugging purposes.
1486 if (nfsver.tv_sec == 0)
1488 *tl++ = txdr_unsigned(nfsver.tv_sec);
1489 *tl = txdr_unsigned(nfsver.tv_usec);
1491 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1492 nfsm_srvfillattr(&va, fp);
1495 nfsd->nd_mreq = mreq;
1497 panic("nfsrv_write: nd_mrep not free");
1500 * Done. Put it at the head of the timer queue so that
1501 * the final phase can return the reply.
1506 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1508 nfsd = swp->nd_coalesce.lh_first;
1510 LIST_REMOVE(nfsd, nd_tq);
1516 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1523 * Search for a reply to return.
1526 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1527 if (nfsd->nd_mreq) {
1528 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1529 LIST_REMOVE(nfsd, nd_tq);
1530 *mrq = nfsd->nd_mreq;
1539 * Coalesce the write request nfsd into owp. To do this we must:
1540 * - remove nfsd from the queues
1541 * - merge nfsd->nd_mrep into owp->nd_mrep
1542 * - update the nd_eoff and nd_stable for owp
1543 * - put nfsd on owp's nd_coalesce list
1544 * NB: Must be called at splsoftclock().
1547 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1551 struct nfsrv_descript *p;
1553 NFS_DPF(WG, ("C%03x-%03x",
1554 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1555 LIST_REMOVE(nfsd, nd_hash);
1556 LIST_REMOVE(nfsd, nd_tq);
1557 if (owp->nd_eoff < nfsd->nd_eoff) {
1558 overlap = owp->nd_eoff - nfsd->nd_off;
1560 panic("nfsrv_coalesce: bad off");
1562 m_adj(nfsd->nd_mrep, overlap);
1566 mp->m_next = nfsd->nd_mrep;
1567 owp->nd_eoff = nfsd->nd_eoff;
1569 m_freem(nfsd->nd_mrep);
1570 nfsd->nd_mrep = NULL;
1571 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1572 owp->nd_stable = NFSV3WRITE_FILESYNC;
1573 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1574 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1575 owp->nd_stable = NFSV3WRITE_DATASYNC;
1576 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1579 * If nfsd had anything else coalesced into it, transfer them
1580 * to owp, otherwise their replies will never get sent.
1582 for (p = nfsd->nd_coalesce.lh_first; p;
1583 p = nfsd->nd_coalesce.lh_first) {
1584 LIST_REMOVE(p, nd_tq);
1585 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1590 * nfs create service
1591 * now does a truncate to 0 length via. setattr if it already exists
1594 nfsrv_create(nfsd, slp, td, mrq)
1595 struct nfsrv_descript *nfsd;
1596 struct nfssvc_sock *slp;
1600 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1601 struct sockaddr *nam = nfsd->nd_nam;
1602 caddr_t dpos = nfsd->nd_dpos;
1603 struct ucred *cred = &nfsd->nd_cr;
1604 struct nfs_fattr *fp;
1605 struct vattr va, dirfor, diraft;
1606 struct vattr *vap = &va;
1607 struct nfsv2_sattr *sp;
1609 struct nameidata nd;
1612 int error = 0, rdev, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1613 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1616 struct mbuf *mb, *mb2, *mreq;
1617 struct vnode *dirp = (struct vnode *)0;
1620 u_quad_t frev, tempsize;
1621 u_char cverf[NFSX_V3CREATEVERF];
1623 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1629 fhp = &nfh.fh_generic;
1631 nfsm_srvnamesiz(len);
1633 nd.ni_cnd.cn_cred = cred;
1634 nd.ni_cnd.cn_nameiop = CREATE;
1635 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1638 * Call namei and do initial cleanup to get a few things
1639 * out of the way. If we get an initial error we cleanup
1640 * and return here to avoid special-casing the invalid nd
1641 * structure through the rest of the case. dirp may be
1642 * set even if an error occurs, but the nd structure will not
1643 * be valid at all if an error occurs so we have to invalidate it
1644 * prior to calling nfsm_reply ( which might goto nfsmout ).
1646 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1647 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1650 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1658 nfsm_reply(NFSX_WCCDATA(v3));
1659 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1665 * No error. Continue. State:
1667 * startdir is valid ( we release this immediately )
1669 * nd.ni_vp may be valid
1670 * nd.ni_dvp is valid
1672 * The error state is set through the code and we may also do some
1673 * opportunistic releasing of vnodes to avoid holding locks through
1674 * NFS I/O. The cleanup at the end is a catch-all
1679 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1680 how = fxdr_unsigned(int, *tl);
1682 case NFSV3CREATE_GUARDED:
1688 case NFSV3CREATE_UNCHECKED:
1691 case NFSV3CREATE_EXCLUSIVE:
1692 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1693 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1697 vap->va_type = VREG;
1699 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1700 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1701 if (vap->va_type == VNON)
1702 vap->va_type = VREG;
1703 vap->va_mode = nfstov_mode(sp->sa_mode);
1704 switch (vap->va_type) {
1706 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1708 vap->va_size = (u_quad_t)tsize;
1713 rdev = fxdr_unsigned(long, sp->sa_size);
1721 * Iff doesn't exist, create it
1722 * otherwise just truncate to 0 length
1723 * should I set the mode too ?
1725 * The only possible error we can have at this point is EEXIST.
1726 * nd.ni_vp will also be non-NULL in that case.
1728 if (nd.ni_vp == NULL) {
1729 if (vap->va_mode == (mode_t)VNOVAL)
1731 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1732 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1733 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1735 NDFREE(&nd, NDF_ONLY_PNBUF);
1737 nfsrv_object_create(nd.ni_vp);
1738 if (exclusive_flag) {
1741 bcopy(cverf, (caddr_t)&vap->va_atime,
1743 error = VOP_SETATTR(nd.ni_vp, vap, cred,
1748 vap->va_type == VCHR ||
1749 vap->va_type == VBLK ||
1750 vap->va_type == VFIFO
1753 * Handle SysV FIFO node special cases. All other
1754 * devices require super user to access.
1756 if (vap->va_type == VCHR && rdev == 0xffffffff)
1757 vap->va_type = VFIFO;
1758 if (vap->va_type != VFIFO &&
1759 (error = suser_cred(cred, 0))) {
1762 vap->va_rdev = rdev;
1763 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1765 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1767 NDFREE(&nd, NDF_ONLY_PNBUF);
1774 * release dvp prior to lookup
1782 * Even though LOCKPARENT was cleared, ni_dvp may
1785 nd.ni_cnd.cn_nameiop = LOOKUP;
1786 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1787 nd.ni_cnd.cn_td = td;
1788 nd.ni_cnd.cn_cred = cred;
1790 error = lookup(&nd);
1795 /* fall through on certain errors */
1797 nfsrv_object_create(nd.ni_vp);
1798 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1806 if (vap->va_size != -1) {
1807 error = nfsrv_access(nd.ni_vp, VWRITE, cred,
1808 (nd.ni_cnd.cn_flags & RDONLY), td, 0);
1810 nqsrv_getl(nd.ni_vp, ND_WRITE);
1811 tempsize = vap->va_size;
1813 vap->va_size = tempsize;
1814 error = VOP_SETATTR(nd.ni_vp, vap, cred, td);
1820 bzero((caddr_t)fhp, sizeof(nfh));
1821 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1822 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1824 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
1827 if (exclusive_flag && !error &&
1828 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1830 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
1834 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1837 nfsm_srvpostop_fh(fhp);
1838 nfsm_srvpostop_attr(0, vap);
1840 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1843 nfsm_srvfhtom(fhp, v3);
1844 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1845 nfsm_srvfillattr(vap, fp);
1855 if (nd.ni_startdir) {
1856 vrele(nd.ni_startdir);
1857 nd.ni_startdir = NULL;
1861 NDFREE(&nd, NDF_ONLY_PNBUF);
1863 if (nd.ni_dvp == nd.ni_vp)
1874 * nfs v3 mknod service
1877 nfsrv_mknod(nfsd, slp, td, mrq)
1878 struct nfsrv_descript *nfsd;
1879 struct nfssvc_sock *slp;
1883 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1884 struct sockaddr *nam = nfsd->nd_nam;
1885 caddr_t dpos = nfsd->nd_dpos;
1886 struct ucred *cred = &nfsd->nd_cr;
1887 struct vattr va, dirfor, diraft;
1888 struct vattr *vap = &va;
1890 struct nameidata nd;
1893 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1894 u_int32_t major, minor;
1897 struct mbuf *mb, *mb2, *mreq;
1898 struct vnode *vp, *dirp = (struct vnode *)0;
1903 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1906 fhp = &nfh.fh_generic;
1908 nfsm_srvnamesiz(len);
1910 nd.ni_cnd.cn_cred = cred;
1911 nd.ni_cnd.cn_nameiop = CREATE;
1912 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1915 * Handle nfs_namei() call. If an error occurs, the nd structure
1916 * is not valid. However, nfsm_*() routines may still jump to
1920 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1921 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1923 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
1925 nfsm_reply(NFSX_WCCDATA(1));
1926 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1930 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1931 vtyp = nfsv3tov_type(*tl);
1932 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1933 error = NFSERR_BADTYPE;
1938 if (vtyp == VCHR || vtyp == VBLK) {
1939 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1940 major = fxdr_unsigned(u_int32_t, *tl++);
1941 minor = fxdr_unsigned(u_int32_t, *tl);
1942 vap->va_rdev = makeudev(major, minor);
1946 * Iff doesn't exist, create it.
1952 vap->va_type = vtyp;
1953 if (vap->va_mode == (mode_t)VNOVAL)
1955 if (vtyp == VSOCK) {
1956 vrele(nd.ni_startdir);
1957 nd.ni_startdir = NULL;
1958 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1959 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1961 NDFREE(&nd, NDF_ONLY_PNBUF);
1963 if (vtyp != VFIFO && (error = suser_cred(cred, 0)))
1965 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1967 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1969 NDFREE(&nd, NDF_ONLY_PNBUF);
1976 * Release dvp prior to lookup
1981 KKASSERT(td->td_proc);
1982 nd.ni_cnd.cn_nameiop = LOOKUP;
1983 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1984 nd.ni_cnd.cn_td = td;
1985 nd.ni_cnd.cn_cred = td->td_proc->p_ucred;
1987 error = lookup(&nd);
1992 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1997 * send response, cleanup, return.
2000 if (nd.ni_startdir) {
2001 vrele(nd.ni_startdir);
2002 nd.ni_startdir = NULL;
2004 NDFREE(&nd, NDF_ONLY_PNBUF);
2006 if (nd.ni_dvp == nd.ni_vp)
2014 bzero((caddr_t)fhp, sizeof(nfh));
2015 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2016 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2018 error = VOP_GETATTR(vp, vap, cred, td);
2025 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2030 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
2032 nfsm_srvpostop_fh(fhp);
2033 nfsm_srvpostop_attr(0, vap);
2035 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2041 vrele(nd.ni_startdir);
2042 NDFREE(&nd, NDF_ONLY_PNBUF);
2044 if (nd.ni_dvp == nd.ni_vp)
2055 * nfs remove service
2058 nfsrv_remove(nfsd, slp, td, mrq)
2059 struct nfsrv_descript *nfsd;
2060 struct nfssvc_sock *slp;
2064 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2065 struct sockaddr *nam = nfsd->nd_nam;
2066 caddr_t dpos = nfsd->nd_dpos;
2067 struct ucred *cred = &nfsd->nd_cr;
2068 struct nameidata nd;
2072 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2073 int v3 = (nfsd->nd_flag & ND_NFSV3);
2075 struct mbuf *mb, *mreq;
2077 struct vattr dirfor, diraft;
2082 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2085 fhp = &nfh.fh_generic;
2087 nfsm_srvnamesiz(len);
2089 nd.ni_cnd.cn_cred = cred;
2090 nd.ni_cnd.cn_nameiop = DELETE;
2091 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2092 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2093 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2096 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
2103 if (nd.ni_vp->v_type == VDIR) {
2104 error = EPERM; /* POSIX */
2108 * The root of a mounted filesystem cannot be deleted.
2110 if (nd.ni_vp->v_flag & VROOT) {
2116 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2117 nqsrv_getl(nd.ni_vp, ND_WRITE);
2118 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2119 NDFREE(&nd, NDF_ONLY_PNBUF);
2123 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2127 nfsm_reply(NFSX_WCCDATA(v3));
2129 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2133 NDFREE(&nd, NDF_ONLY_PNBUF);
2135 if (nd.ni_dvp == nd.ni_vp)
2146 * nfs rename service
2149 nfsrv_rename(nfsd, slp, td, mrq)
2150 struct nfsrv_descript *nfsd;
2151 struct nfssvc_sock *slp;
2155 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2156 struct sockaddr *nam = nfsd->nd_nam;
2157 caddr_t dpos = nfsd->nd_dpos;
2158 struct ucred *cred = &nfsd->nd_cr;
2162 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2163 int tdirfor_ret = 1, tdiraft_ret = 1;
2164 int v3 = (nfsd->nd_flag & ND_NFSV3);
2166 struct mbuf *mb, *mreq;
2167 struct nameidata fromnd, tond;
2168 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
2169 struct vnode *tdirp = (struct vnode *)0;
2170 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2172 fhandle_t *ffhp, *tfhp;
2176 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2178 fvp = (struct vnode *)0;
2180 ffhp = &fnfh.fh_generic;
2181 tfhp = &tnfh.fh_generic;
2184 * Clear fields incase goto nfsmout occurs from macro.
2190 nfsm_srvmtofh(ffhp);
2191 nfsm_srvnamesiz(len);
2193 * Remember our original uid so that we can reset cr_uid before
2194 * the second nfs_namei() call, in case it is remapped.
2196 saved_uid = cred->cr_uid;
2197 fromnd.ni_cnd.cn_cred = cred;
2198 fromnd.ni_cnd.cn_nameiop = DELETE;
2199 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
2200 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
2201 &dpos, &fdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2204 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred, td);
2211 nfsm_reply(2 * NFSX_WCCDATA(v3));
2212 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2213 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2218 nfsm_srvmtofh(tfhp);
2219 nfsm_strsiz(len2, NFS_MAXNAMLEN);
2220 cred->cr_uid = saved_uid;
2221 tond.ni_cnd.cn_cred = cred;
2222 tond.ni_cnd.cn_nameiop = RENAME;
2223 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
2224 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
2225 &dpos, &tdirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2228 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred, td);
2240 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2246 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2253 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
2261 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
2268 if (fvp->v_mount != tdvp->v_mount) {
2282 * If source is the same as the destination (that is the
2283 * same vnode with the same name in the same directory),
2284 * then there is nothing to do.
2286 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
2287 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2288 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
2289 fromnd.ni_cnd.cn_namelen))
2294 * The VOP_RENAME function releases all vnode references &
2295 * locks prior to returning so we need to clear the pointers
2296 * to bypass cleanup code later on.
2298 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
2299 nqsrv_getl(tdvp, ND_WRITE);
2301 nqsrv_getl(tvp, ND_WRITE);
2303 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2304 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2305 fromnd.ni_dvp = NULL;
2306 fromnd.ni_vp = NULL;
2310 fromnd.ni_cnd.cn_flags &= ~HASBUF;
2311 tond.ni_cnd.cn_flags &= ~HASBUF;
2321 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, td);
2323 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, td);
2324 nfsm_reply(2 * NFSX_WCCDATA(v3));
2326 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
2327 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
2334 * Clear out tond related fields
2338 if (tond.ni_startdir)
2339 vrele(tond.ni_startdir);
2340 NDFREE(&tond, NDF_ONLY_PNBUF);
2342 if (tond.ni_dvp == tond.ni_vp)
2351 * Clear out fromnd related fields
2355 if (fromnd.ni_startdir)
2356 vrele(fromnd.ni_startdir);
2357 NDFREE(&fromnd, NDF_ONLY_PNBUF);
2359 vrele(fromnd.ni_dvp);
2361 vrele(fromnd.ni_vp);
2370 nfsrv_link(nfsd, slp, td, mrq)
2371 struct nfsrv_descript *nfsd;
2372 struct nfssvc_sock *slp;
2376 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2377 struct sockaddr *nam = nfsd->nd_nam;
2378 caddr_t dpos = nfsd->nd_dpos;
2379 struct ucred *cred = &nfsd->nd_cr;
2380 struct nameidata nd;
2384 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
2385 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
2387 struct mbuf *mb, *mreq;
2388 struct vnode *vp = NULL, *xp, *dirp = (struct vnode *)0;
2389 struct vattr dirfor, diraft, at;
2391 fhandle_t *fhp, *dfhp;
2394 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2397 fhp = &nfh.fh_generic;
2398 dfhp = &dnfh.fh_generic;
2400 nfsm_srvmtofh(dfhp);
2401 nfsm_srvnamesiz(len);
2403 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
2404 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2406 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2407 nfsm_srvpostop_attr(getret, &at);
2408 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2413 if (vp->v_type == VDIR) {
2414 error = EPERM; /* POSIX */
2417 nd.ni_cnd.cn_cred = cred;
2418 nd.ni_cnd.cn_nameiop = CREATE;
2419 nd.ni_cnd.cn_flags = LOCKPARENT;
2420 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2421 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2424 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
2439 if (vp->v_mount != xp->v_mount)
2443 nqsrv_getl(vp, ND_WRITE);
2444 nqsrv_getl(xp, ND_WRITE);
2445 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2446 NDFREE(&nd, NDF_ONLY_PNBUF);
2452 getret = VOP_GETATTR(vp, &at, cred, td);
2454 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2455 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2457 nfsm_srvpostop_attr(getret, &at);
2458 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2464 NDFREE(&nd, NDF_ONLY_PNBUF);
2470 if (nd.ni_dvp == nd.ni_vp)
2481 * nfs symbolic link service
2484 nfsrv_symlink(nfsd, slp, td, mrq)
2485 struct nfsrv_descript *nfsd;
2486 struct nfssvc_sock *slp;
2490 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2491 struct sockaddr *nam = nfsd->nd_nam;
2492 caddr_t dpos = nfsd->nd_dpos;
2493 struct ucred *cred = &nfsd->nd_cr;
2494 struct vattr va, dirfor, diraft;
2495 struct nameidata nd;
2496 struct vattr *vap = &va;
2499 struct nfsv2_sattr *sp;
2500 char *bpos, *pathcp = (char *)0, *cp2;
2503 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2504 int v3 = (nfsd->nd_flag & ND_NFSV3);
2505 struct mbuf *mb, *mreq, *mb2;
2506 struct vnode *dirp = (struct vnode *)0;
2511 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2514 fhp = &nfh.fh_generic;
2516 nfsm_srvnamesiz(len);
2517 nd.ni_cnd.cn_cred = cred;
2518 nd.ni_cnd.cn_nameiop = CREATE;
2519 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2520 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2521 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2524 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
2536 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2537 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2538 iv.iov_base = pathcp;
2540 io.uio_resid = len2;
2544 io.uio_segflg = UIO_SYSSPACE;
2545 io.uio_rw = UIO_READ;
2547 nfsm_mtouio(&io, len2);
2549 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2550 vap->va_mode = nfstov_mode(sp->sa_mode);
2552 *(pathcp + len2) = '\0';
2559 * issue symlink op. SAVESTART is set so the underlying path component
2560 * is only freed by the VOP if an error occurs.
2562 if (vap->va_mode == (mode_t)VNOVAL)
2564 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2565 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2567 NDFREE(&nd, NDF_ONLY_PNBUF);
2572 * releases directory prior to potential lookup op.
2580 * Issue lookup. Leave SAVESTART set so we can easily free
2581 * the name buffer later on.
2583 * since LOCKPARENT is not set, ni_dvp will be garbage on
2584 * return whether an error occurs or not.
2586 nd.ni_cnd.cn_nameiop = LOOKUP;
2587 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2588 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2589 nd.ni_cnd.cn_td = td;
2590 nd.ni_cnd.cn_cred = cred;
2592 error = lookup(&nd);
2596 bzero((caddr_t)fhp, sizeof(nfh));
2597 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2598 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2600 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2608 * These releases aren't strictly required, does even doing them
2609 * make any sense? XXX can nfsm_reply() block?
2612 FREE(pathcp, M_TEMP);
2616 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2620 if (nd.ni_startdir) {
2621 vrele(nd.ni_startdir);
2622 nd.ni_startdir = NULL;
2624 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2627 nfsm_srvpostop_fh(fhp);
2628 nfsm_srvpostop_attr(0, vap);
2630 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2636 NDFREE(&nd, NDF_ONLY_PNBUF);
2638 if (nd.ni_dvp == nd.ni_vp)
2646 vrele(nd.ni_startdir);
2650 FREE(pathcp, M_TEMP);
2659 nfsrv_mkdir(nfsd, slp, td, mrq)
2660 struct nfsrv_descript *nfsd;
2661 struct nfssvc_sock *slp;
2665 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2666 struct sockaddr *nam = nfsd->nd_nam;
2667 caddr_t dpos = nfsd->nd_dpos;
2668 struct ucred *cred = &nfsd->nd_cr;
2669 struct vattr va, dirfor, diraft;
2670 struct vattr *vap = &va;
2671 struct nfs_fattr *fp;
2672 struct nameidata nd;
2677 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2678 int v3 = (nfsd->nd_flag & ND_NFSV3);
2680 struct mbuf *mb, *mb2, *mreq;
2681 struct vnode *dirp = NULL;
2687 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2690 fhp = &nfh.fh_generic;
2692 nfsm_srvnamesiz(len);
2693 nd.ni_cnd.cn_cred = cred;
2694 nd.ni_cnd.cn_nameiop = CREATE;
2695 nd.ni_cnd.cn_flags = LOCKPARENT;
2697 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2698 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2701 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
2708 nfsm_reply(NFSX_WCCDATA(v3));
2709 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2717 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2718 vap->va_mode = nfstov_mode(*tl++);
2722 * At this point nd.ni_dvp is referenced and exclusively locked and
2723 * nd.ni_vp, if it exists, is referenced but not locked.
2726 vap->va_type = VDIR;
2727 if (nd.ni_vp != NULL) {
2728 NDFREE(&nd, NDF_ONLY_PNBUF);
2734 * Issue mkdir op. Since SAVESTART is not set, the pathname
2735 * component is freed by the VOP call. This will fill-in
2736 * nd.ni_vp, reference, and exclusively lock it.
2738 if (vap->va_mode == (mode_t)VNOVAL)
2740 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2741 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2742 NDFREE(&nd, NDF_ONLY_PNBUF);
2749 bzero((caddr_t)fhp, sizeof(nfh));
2750 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2751 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2753 error = VOP_GETATTR(nd.ni_vp, vap, cred, td);
2757 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2758 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2761 nfsm_srvpostop_fh(fhp);
2762 nfsm_srvpostop_attr(0, vap);
2764 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2766 nfsm_srvfhtom(fhp, v3);
2767 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2768 nfsm_srvfillattr(vap, fp);
2777 NDFREE(&nd, NDF_ONLY_PNBUF);
2778 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2796 nfsrv_rmdir(nfsd, slp, td, mrq)
2797 struct nfsrv_descript *nfsd;
2798 struct nfssvc_sock *slp;
2802 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2803 struct sockaddr *nam = nfsd->nd_nam;
2804 caddr_t dpos = nfsd->nd_dpos;
2805 struct ucred *cred = &nfsd->nd_cr;
2809 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2810 int v3 = (nfsd->nd_flag & ND_NFSV3);
2812 struct mbuf *mb, *mreq;
2813 struct vnode *vp, *dirp = (struct vnode *)0;
2814 struct vattr dirfor, diraft;
2817 struct nameidata nd;
2820 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2823 fhp = &nfh.fh_generic;
2825 nfsm_srvnamesiz(len);
2826 nd.ni_cnd.cn_cred = cred;
2827 nd.ni_cnd.cn_nameiop = DELETE;
2828 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2829 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2830 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2833 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, td);
2840 nfsm_reply(NFSX_WCCDATA(v3));
2841 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2846 if (vp->v_type != VDIR) {
2851 * No rmdir "." please.
2853 if (nd.ni_dvp == vp) {
2858 * The root of a mounted filesystem cannot be deleted.
2860 if (vp->v_flag & VROOT)
2864 * Issue or abort op. Since SAVESTART is not set, path name
2865 * component is freed by the VOP after either.
2868 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2869 nqsrv_getl(vp, ND_WRITE);
2870 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2872 NDFREE(&nd, NDF_ONLY_PNBUF);
2875 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, td);
2876 nfsm_reply(NFSX_WCCDATA(v3));
2878 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2884 NDFREE(&nd, NDF_ONLY_PNBUF);
2888 if (nd.ni_dvp == nd.ni_vp)
2900 * nfs readdir service
2901 * - mallocs what it thinks is enough to read
2902 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2903 * - calls VOP_READDIR()
2904 * - loops around building the reply
2905 * if the output generated exceeds count break out of loop
2906 * The nfsm_clget macro is used here so that the reply will be packed
2907 * tightly in mbuf clusters.
2908 * - it only knows that it has encountered eof when the VOP_READDIR()
2910 * - as such one readdir rpc will return eof false although you are there
2911 * and then the next will return eof
2912 * - it trims out records with d_fileno == 0
2913 * this doesn't matter for Unix clients, but they might confuse clients
2915 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2916 * than requested, but this may not apply to all filesystems. For
2917 * example, client NFS does not { although it is never remote mounted
2919 * The alternate call nfsrv_readdirplus() does lookups as well.
2920 * PS: The NFS protocol spec. does not clarify what the "count" byte
2921 * argument is a count of.. just name strings and file id's or the
2922 * entire reply rpc or ...
2923 * I tried just file name and id sizes and it confused the Sun client,
2924 * so I am using the full rpc size now. The "paranoia.." comment refers
2925 * to including the status longwords that are not a part of the dir.
2926 * "entry" structures, but are in the rpc.
2930 u_int32_t fl_postopok;
2931 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2933 u_int32_t fl_fhsize;
2934 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2938 nfsrv_readdir(nfsd, slp, td, mrq)
2939 struct nfsrv_descript *nfsd;
2940 struct nfssvc_sock *slp;
2944 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2945 struct sockaddr *nam = nfsd->nd_nam;
2946 caddr_t dpos = nfsd->nd_dpos;
2947 struct ucred *cred = &nfsd->nd_cr;
2955 struct mbuf *mb, *mb2, *mreq, *mp2;
2956 char *cpos, *cend, *cp2, *rbuf;
2957 struct vnode *vp = NULL;
2963 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2964 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2965 int v3 = (nfsd->nd_flag & ND_NFSV3);
2966 u_quad_t frev, off, toff, verf;
2967 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2969 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2970 fhp = &nfh.fh_generic;
2973 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2974 toff = fxdr_hyper(tl);
2976 verf = fxdr_hyper(tl);
2979 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2980 toff = fxdr_unsigned(u_quad_t, *tl++);
2981 verf = 0; /* shut up gcc */
2984 cnt = fxdr_unsigned(int, *tl);
2985 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2986 xfer = NFS_SRVMAXDATA(nfsd);
2992 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2993 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2994 if (!error && vp->v_type != VDIR) {
3000 nfsm_reply(NFSX_UNSIGNED);
3001 nfsm_srvpostop_attr(getret, &at);
3007 * Obtain lock on vnode for this section of the code
3010 nqsrv_getl(vp, ND_READ);
3012 error = getret = VOP_GETATTR(vp, &at, cred, td);
3015 * XXX This check may be too strict for Solaris 2.5 clients.
3017 if (!error && toff && verf && verf != at.va_filerev)
3018 error = NFSERR_BAD_COOKIE;
3022 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3026 nfsm_reply(NFSX_POSTOPATTR(v3));
3027 nfsm_srvpostop_attr(getret, &at);
3031 VOP_UNLOCK(vp, 0, td);
3034 * end section. Allocate rbuf and continue
3036 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3039 iv.iov_len = fullsiz;
3042 io.uio_offset = (off_t)off;
3043 io.uio_resid = fullsiz;
3044 io.uio_segflg = UIO_SYSSPACE;
3045 io.uio_rw = UIO_READ;
3048 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3050 free((caddr_t)cookies, M_TEMP);
3053 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3054 off = (off_t)io.uio_offset;
3055 if (!cookies && !error)
3056 error = NFSERR_PERM;
3058 getret = VOP_GETATTR(vp, &at, cred, td);
3062 VOP_UNLOCK(vp, 0, td);
3066 free((caddr_t)rbuf, M_TEMP);
3068 free((caddr_t)cookies, M_TEMP);
3069 nfsm_reply(NFSX_POSTOPATTR(v3));
3070 nfsm_srvpostop_attr(getret, &at);
3075 siz -= io.uio_resid;
3078 * If nothing read, return eof
3084 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
3087 nfsm_srvpostop_attr(getret, &at);
3088 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3089 txdr_hyper(at.va_filerev, tl);
3092 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3095 FREE((caddr_t)rbuf, M_TEMP);
3096 FREE((caddr_t)cookies, M_TEMP);
3103 * Check for degenerate cases of nothing useful read.
3104 * If so go try again
3108 dp = (struct dirent *)cpos;
3111 * For some reason FreeBSD's ufs_readdir() chooses to back the
3112 * directory offset up to a block boundary, so it is necessary to
3113 * skip over the records that preceed the requested offset. This
3114 * requires the assumption that file offset cookies monotonically
3117 while (cpos < cend && ncookies > 0 &&
3118 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3119 ((u_quad_t)(*cookiep)) <= toff)) {
3120 cpos += dp->d_reclen;
3121 dp = (struct dirent *)cpos;
3125 if (cpos >= cend || ncookies == 0) {
3131 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3132 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
3134 nfsm_srvpostop_attr(getret, &at);
3135 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3136 txdr_hyper(at.va_filerev, tl);
3140 be = bp + M_TRAILINGSPACE(mp);
3142 /* Loop through the records and build reply */
3143 while (cpos < cend && ncookies > 0) {
3144 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3145 nlen = dp->d_namlen;
3146 rem = nfsm_rndup(nlen) - nlen;
3147 len += (4 * NFSX_UNSIGNED + nlen + rem);
3149 len += 2 * NFSX_UNSIGNED;
3155 * Build the directory record xdr from
3160 bp += NFSX_UNSIGNED;
3164 bp += NFSX_UNSIGNED;
3167 *tl = txdr_unsigned(dp->d_fileno);
3168 bp += NFSX_UNSIGNED;
3170 *tl = txdr_unsigned(nlen);
3171 bp += NFSX_UNSIGNED;
3173 /* And loop around copying the name */
3182 bcopy(cp, bp, tsiz);
3188 /* And null pad to a int32_t boundary */
3189 for (i = 0; i < rem; i++)
3193 /* Finish off the record */
3196 bp += NFSX_UNSIGNED;
3199 *tl = txdr_unsigned(*cookiep);
3200 bp += NFSX_UNSIGNED;
3202 cpos += dp->d_reclen;
3203 dp = (struct dirent *)cpos;
3211 bp += NFSX_UNSIGNED;
3217 bp += NFSX_UNSIGNED;
3220 mp->m_len = bp - mtod(mp, caddr_t);
3222 mp->m_len += bp - bpos;
3223 FREE((caddr_t)rbuf, M_TEMP);
3224 FREE((caddr_t)cookies, M_TEMP);
3233 nfsrv_readdirplus(nfsd, slp, td, mrq)
3234 struct nfsrv_descript *nfsd;
3235 struct nfssvc_sock *slp;
3239 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3240 struct sockaddr *nam = nfsd->nd_nam;
3241 caddr_t dpos = nfsd->nd_dpos;
3242 struct ucred *cred = &nfsd->nd_cr;
3250 struct mbuf *mb, *mb2, *mreq, *mp2;
3251 char *cpos, *cend, *cp2, *rbuf;
3252 struct vnode *vp = NULL, *nvp;
3255 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3258 struct vattr va, at, *vap = &va;
3259 struct nfs_fattr *fp;
3260 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3261 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
3262 u_quad_t frev, off, toff, verf;
3263 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3265 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3266 fhp = &nfh.fh_generic;
3268 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
3269 toff = fxdr_hyper(tl);
3271 verf = fxdr_hyper(tl);
3273 siz = fxdr_unsigned(int, *tl++);
3274 cnt = fxdr_unsigned(int, *tl);
3276 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3277 xfer = NFS_SRVMAXDATA(nfsd);
3283 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3284 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3285 if (!error && vp->v_type != VDIR) {
3291 nfsm_reply(NFSX_UNSIGNED);
3292 nfsm_srvpostop_attr(getret, &at);
3296 error = getret = VOP_GETATTR(vp, &at, cred, td);
3299 * XXX This check may be too strict for Solaris 2.5 clients.
3301 if (!error && toff && verf && verf != at.va_filerev)
3302 error = NFSERR_BAD_COOKIE;
3305 nqsrv_getl(vp, ND_READ);
3306 error = nfsrv_access(vp, VEXEC, cred, rdonly, td, 0);
3311 nfsm_reply(NFSX_V3POSTOPATTR);
3312 nfsm_srvpostop_attr(getret, &at);
3316 VOP_UNLOCK(vp, 0, td);
3317 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3320 iv.iov_len = fullsiz;
3323 io.uio_offset = (off_t)off;
3324 io.uio_resid = fullsiz;
3325 io.uio_segflg = UIO_SYSSPACE;
3326 io.uio_rw = UIO_READ;
3329 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
3331 free((caddr_t)cookies, M_TEMP);
3334 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3335 off = (u_quad_t)io.uio_offset;
3336 getret = VOP_GETATTR(vp, &at, cred, td);
3337 VOP_UNLOCK(vp, 0, td);
3338 if (!cookies && !error)
3339 error = NFSERR_PERM;
3346 free((caddr_t)cookies, M_TEMP);
3347 free((caddr_t)rbuf, M_TEMP);
3348 nfsm_reply(NFSX_V3POSTOPATTR);
3349 nfsm_srvpostop_attr(getret, &at);
3354 siz -= io.uio_resid;
3357 * If nothing read, return eof
3363 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3365 nfsm_srvpostop_attr(getret, &at);
3366 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
3367 txdr_hyper(at.va_filerev, tl);
3371 FREE((caddr_t)cookies, M_TEMP);
3372 FREE((caddr_t)rbuf, M_TEMP);
3379 * Check for degenerate cases of nothing useful read.
3380 * If so go try again
3384 dp = (struct dirent *)cpos;
3387 * For some reason FreeBSD's ufs_readdir() chooses to back the
3388 * directory offset up to a block boundary, so it is necessary to
3389 * skip over the records that preceed the requested offset. This
3390 * requires the assumption that file offset cookies monotonically
3393 while (cpos < cend && ncookies > 0 &&
3394 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3395 ((u_quad_t)(*cookiep)) <= toff)) {
3396 cpos += dp->d_reclen;
3397 dp = (struct dirent *)cpos;
3401 if (cpos >= cend || ncookies == 0) {
3408 * Probe one of the directory entries to see if the filesystem
3411 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp) == EOPNOTSUPP) {
3412 error = NFSERR_NOTSUPP;
3415 free((caddr_t)cookies, M_TEMP);
3416 free((caddr_t)rbuf, M_TEMP);
3417 nfsm_reply(NFSX_V3POSTOPATTR);
3418 nfsm_srvpostop_attr(getret, &at);
3425 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3427 nfsm_srvpostop_attr(getret, &at);
3428 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
3429 txdr_hyper(at.va_filerev, tl);
3432 be = bp + M_TRAILINGSPACE(mp);
3434 /* Loop through the records and build reply */
3435 while (cpos < cend && ncookies > 0) {
3436 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3437 nlen = dp->d_namlen;
3438 rem = nfsm_rndup(nlen)-nlen;
3441 * For readdir_and_lookup get the vnode using
3444 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
3446 bzero((caddr_t)nfhp, NFSX_V3FH);
3448 nvp->v_mount->mnt_stat.f_fsid;
3449 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3454 if (VOP_GETATTR(nvp, vap, cred, td)) {
3463 * If either the dircount or maxcount will be
3464 * exceeded, get out now. Both of these lengths
3465 * are calculated conservatively, including all
3468 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3470 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3471 if (len > cnt || dirlen > fullsiz) {
3477 * Build the directory record xdr from
3480 fp = (struct nfs_fattr *)&fl.fl_fattr;
3481 nfsm_srvfillattr(vap, fp);
3482 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3483 fl.fl_fhok = nfs_true;
3484 fl.fl_postopok = nfs_true;
3485 fl.fl_off.nfsuquad[0] = 0;
3486 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3490 bp += NFSX_UNSIGNED;
3493 bp += NFSX_UNSIGNED;
3495 *tl = txdr_unsigned(dp->d_fileno);
3496 bp += NFSX_UNSIGNED;
3498 *tl = txdr_unsigned(nlen);
3499 bp += NFSX_UNSIGNED;
3501 /* And loop around copying the name */
3506 if ((bp + xfer) > be)
3510 bcopy(cp, bp, tsiz);
3516 /* And null pad to a int32_t boundary */
3517 for (i = 0; i < rem; i++)
3521 * Now copy the flrep structure out.
3523 xfer = sizeof (struct flrep);
3527 if ((bp + xfer) > be)
3531 bcopy(cp, bp, tsiz);
3539 cpos += dp->d_reclen;
3540 dp = (struct dirent *)cpos;
3548 bp += NFSX_UNSIGNED;
3554 bp += NFSX_UNSIGNED;
3557 mp->m_len = bp - mtod(mp, caddr_t);
3559 mp->m_len += bp - bpos;
3560 FREE((caddr_t)cookies, M_TEMP);
3561 FREE((caddr_t)rbuf, M_TEMP);
3569 * nfs commit service
3572 nfsrv_commit(nfsd, slp, td, mrq)
3573 struct nfsrv_descript *nfsd;
3574 struct nfssvc_sock *slp;
3578 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3579 struct sockaddr *nam = nfsd->nd_nam;
3580 caddr_t dpos = nfsd->nd_dpos;
3581 struct ucred *cred = &nfsd->nd_cr;
3582 struct vattr bfor, aft;
3583 struct vnode *vp = NULL;
3589 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3591 struct mbuf *mb, *mb2, *mreq;
3594 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3598 fhp = &nfh.fh_generic;
3600 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3603 * XXX At this time VOP_FSYNC() does not accept offset and byte
3604 * count parameters, so these arguments are useless (someday maybe).
3606 off = fxdr_hyper(tl);
3608 cnt = fxdr_unsigned(int, *tl);
3609 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3610 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3612 nfsm_reply(2 * NFSX_UNSIGNED);
3613 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3617 for_ret = VOP_GETATTR(vp, &bfor, cred, td);
3619 if (cnt > MAX_COMMIT_COUNT) {
3621 * Give up and do the whole thing
3624 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3625 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3627 error = VOP_FSYNC(vp, cred, MNT_WAIT, td);
3630 * Locate and synchronously write any buffers that fall
3631 * into the requested range. Note: we are assuming that
3632 * f_iosize is a power of 2.
3634 int iosize = vp->v_mount->mnt_stat.f_iosize;
3635 int iomask = iosize - 1;
3640 * Align to iosize boundry, super-align to page boundry.
3643 cnt += off & iomask;
3644 off &= ~(u_quad_t)iomask;
3646 if (off & PAGE_MASK) {
3647 cnt += off & PAGE_MASK;
3648 off &= ~(u_quad_t)PAGE_MASK;
3650 lblkno = off / iosize;
3653 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3654 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3662 * If we have a buffer and it is marked B_DELWRI we
3663 * have to lock and write it. Otherwise the prior
3664 * write is assumed to have already been committed.
3666 if ((bp = gbincore(vp, lblkno)) != NULL && (bp->b_flags & B_DELWRI)) {
3667 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
3668 BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL);
3669 continue; /* retry */
3672 bp->b_flags &= ~B_ASYNC;
3673 VOP_BWRITE(bp->b_vp, bp);
3685 aft_ret = VOP_GETATTR(vp, &aft, cred, td);
3688 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3689 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3691 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3692 if (nfsver.tv_sec == 0)
3694 *tl++ = txdr_unsigned(nfsver.tv_sec);
3695 *tl = txdr_unsigned(nfsver.tv_usec);
3706 * nfs statfs service
3709 nfsrv_statfs(nfsd, slp, td, mrq)
3710 struct nfsrv_descript *nfsd;
3711 struct nfssvc_sock *slp;
3715 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3716 struct sockaddr *nam = nfsd->nd_nam;
3717 caddr_t dpos = nfsd->nd_dpos;
3718 struct ucred *cred = &nfsd->nd_cr;
3720 struct nfs_statfs *sfp;
3724 int error = 0, rdonly, cache, getret = 1;
3725 int v3 = (nfsd->nd_flag & ND_NFSV3);
3727 struct mbuf *mb, *mb2, *mreq;
3728 struct vnode *vp = NULL;
3732 struct statfs statfs;
3733 u_quad_t frev, tval;
3735 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3739 fhp = &nfh.fh_generic;
3741 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3742 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3744 nfsm_reply(NFSX_UNSIGNED);
3745 nfsm_srvpostop_attr(getret, &at);
3750 error = VFS_STATFS(vp->v_mount, sf, td);
3751 getret = VOP_GETATTR(vp, &at, cred, td);
3754 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3756 nfsm_srvpostop_attr(getret, &at);
3761 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3763 tval = (u_quad_t)sf->f_blocks;
3764 tval *= (u_quad_t)sf->f_bsize;
3765 txdr_hyper(tval, &sfp->sf_tbytes);
3766 tval = (u_quad_t)sf->f_bfree;
3767 tval *= (u_quad_t)sf->f_bsize;
3768 txdr_hyper(tval, &sfp->sf_fbytes);
3769 tval = (u_quad_t)sf->f_bavail;
3770 tval *= (u_quad_t)sf->f_bsize;
3771 txdr_hyper(tval, &sfp->sf_abytes);
3772 sfp->sf_tfiles.nfsuquad[0] = 0;
3773 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3774 sfp->sf_ffiles.nfsuquad[0] = 0;
3775 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3776 sfp->sf_afiles.nfsuquad[0] = 0;
3777 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3778 sfp->sf_invarsec = 0;
3780 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3781 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3782 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3783 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3784 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3793 * nfs fsinfo service
3796 nfsrv_fsinfo(nfsd, slp, td, mrq)
3797 struct nfsrv_descript *nfsd;
3798 struct nfssvc_sock *slp;
3802 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3803 struct sockaddr *nam = nfsd->nd_nam;
3804 caddr_t dpos = nfsd->nd_dpos;
3805 struct ucred *cred = &nfsd->nd_cr;
3807 struct nfsv3_fsinfo *sip;
3810 int error = 0, rdonly, cache, getret = 1, pref;
3812 struct mbuf *mb, *mb2, *mreq;
3813 struct vnode *vp = NULL;
3817 u_quad_t frev, maxfsize;
3820 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3824 fhp = &nfh.fh_generic;
3826 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3827 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3829 nfsm_reply(NFSX_UNSIGNED);
3830 nfsm_srvpostop_attr(getret, &at);
3835 /* XXX Try to make a guess on the max file size. */
3836 VFS_STATFS(vp->v_mount, &sb, td);
3837 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3839 getret = VOP_GETATTR(vp, &at, cred, td);
3842 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3843 nfsm_srvpostop_attr(getret, &at);
3844 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3848 * There should be file system VFS OP(s) to get this information.
3849 * For now, assume ufs.
3851 if (slp->ns_so->so_type == SOCK_DGRAM)
3852 pref = NFS_MAXDGRAMDATA;
3855 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3856 sip->fs_rtpref = txdr_unsigned(pref);
3857 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3858 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3859 sip->fs_wtpref = txdr_unsigned(pref);
3860 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3861 sip->fs_dtpref = txdr_unsigned(pref);
3862 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3863 sip->fs_timedelta.nfsv3_sec = 0;
3864 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3865 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3866 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3867 NFSV3FSINFO_CANSETTIME);
3875 * nfs pathconf service
3878 nfsrv_pathconf(nfsd, slp, td, mrq)
3879 struct nfsrv_descript *nfsd;
3880 struct nfssvc_sock *slp;
3884 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3885 struct sockaddr *nam = nfsd->nd_nam;
3886 caddr_t dpos = nfsd->nd_dpos;
3887 struct ucred *cred = &nfsd->nd_cr;
3889 struct nfsv3_pathconf *pc;
3892 int error = 0, rdonly, cache, getret = 1;
3893 register_t linkmax, namemax, chownres, notrunc;
3895 struct mbuf *mb, *mb2, *mreq;
3896 struct vnode *vp = NULL;
3902 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3906 fhp = &nfh.fh_generic;
3908 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3909 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3911 nfsm_reply(NFSX_UNSIGNED);
3912 nfsm_srvpostop_attr(getret, &at);
3916 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3918 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3920 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3922 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3923 getret = VOP_GETATTR(vp, &at, cred, td);
3926 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3927 nfsm_srvpostop_attr(getret, &at);
3932 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3934 pc->pc_linkmax = txdr_unsigned(linkmax);
3935 pc->pc_namemax = txdr_unsigned(namemax);
3936 pc->pc_notrunc = txdr_unsigned(notrunc);
3937 pc->pc_chownrestricted = txdr_unsigned(chownres);
3940 * These should probably be supported by VOP_PATHCONF(), but
3941 * until msdosfs is exportable (why would you want to?), the
3942 * Unix defaults should be ok.
3944 pc->pc_caseinsensitive = nfs_false;
3945 pc->pc_casepreserving = nfs_true;
3953 * Null operation, used by clients to ping server
3957 nfsrv_null(nfsd, slp, td, mrq)
3958 struct nfsrv_descript *nfsd;
3959 struct nfssvc_sock *slp;
3963 struct mbuf *mrep = nfsd->nd_mrep;
3965 int error = NFSERR_RETVOID, cache;
3966 struct mbuf *mb, *mreq;
3969 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3978 * No operation, used for obsolete procedures
3982 nfsrv_noop(nfsd, slp, td, mrq)
3983 struct nfsrv_descript *nfsd;
3984 struct nfssvc_sock *slp;
3988 struct mbuf *mrep = nfsd->nd_mrep;
3991 struct mbuf *mb, *mreq;
3994 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3998 if (nfsd->nd_repstat)
3999 error = nfsd->nd_repstat;
4001 error = EPROCUNAVAIL;
4008 * Perform access checking for vnodes obtained from file handles that would
4009 * refer to files already opened by a Unix client. You cannot just use
4010 * vn_writechk() and VOP_ACCESS() for two reasons.
4011 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
4012 * 2 - The owner is to be given access irrespective of mode bits for some
4013 * operations, so that processes that chmod after opening a file don't
4014 * break. I don't like this because it opens a security hole, but since
4015 * the nfs server opens a security hole the size of a barn door anyhow,
4018 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
4019 * will return EPERM instead of EACCESS. EPERM is always an error.
4022 nfsrv_access(struct vnode *vp, int flags, struct ucred *cred,
4023 int rdonly, struct thread *td, int override)
4028 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
4029 if (flags & VWRITE) {
4030 /* Just vn_writechk() changed to check rdonly */
4032 * Disallow write attempts on read-only file systems;
4033 * unless the file is a socket or a block or character
4034 * device resident on the file system.
4036 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
4037 switch (vp->v_type) {
4047 * If there's shared text associated with
4048 * the inode, we can't allow writing.
4050 if (vp->v_flag & VTEXT)
4053 error = VOP_GETATTR(vp, &vattr, cred, td);
4056 error = VOP_ACCESS(vp, flags, cred, td);
4058 * Allow certain operations for the owner (reads and writes
4059 * on files that are already open).
4061 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4065 #endif /* NFS_NOSERVER */