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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * nfs version 2 and 3 server calls to vnode ops
40 * - these routines generally have 3 phases
41 * 1 - break down and validate rpc request in mbuf list
42 * 2 - do the vnode ops for the request
43 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
44 * 3 - build the rpc reply in an mbuf list
46 * - do not mix the phases, since the nfsm_?? macros can return failures
47 * on a bad rpc or similar and do not do any vrele() or vput()'s
49 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
50 * error number iff error != 0 whereas
51 * returning an error from the server function implies a fatal error
52 * such as a badly constructed rpc request that should be dropped without
54 * For nfsm_reply(), the case where error == EBADRPC is treated
55 * specially; after constructing a reply, it does an immediate
56 * `goto nfsmout' to avoid getting any V3 post-op status appended.
59 * Warning: always pay careful attention to resource cleanup on return
60 * and note that nfsm_*() macros can terminate a procedure on certain
63 * lookup() and namei()
64 * may return garbage in various structural fields/return elements
65 * if an error is returned, and may garbage up nd.ni_dvp even if no
66 * error is returned and you did not request LOCKPARENT or WANTPARENT.
68 * We use the ni_cnd.cn_flags 'HASBUF' flag to track whether the name
69 * buffer has been freed or not.
72 #include <sys/param.h>
73 #include <sys/systm.h>
75 #include <sys/namei.h>
76 #include <sys/unistd.h>
77 #include <sys/vnode.h>
78 #include <sys/mount.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/malloc.h>
84 #include <sys/dirent.h>
86 #include <sys/kernel.h>
87 #include <sys/sysctl.h>
90 #include <sys/rwlock.h>
93 #include <vm/vm_extern.h>
94 #include <vm/vm_object.h>
96 #include <nfs/nfsproto.h>
97 #include <nfsserver/nfs.h>
98 #include <nfs/xdr_subs.h>
99 #include <nfsserver/nfsm_subs.h>
101 FEATURE(nfsserver, "NFS server");
104 #define nfsdbprintf(info) printf info
106 #define nfsdbprintf(info)
109 #define MAX_COMMIT_COUNT (1024 * 1024)
111 #define MAX_REORDERED_RPC 16
112 #define NUM_HEURISTIC 1031
113 #define NHUSE_INIT 64
115 #define NHUSE_MAX 2048
117 static struct nfsheur {
118 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
119 off_t nh_nextoff; /* next offset for sequential detection */
120 int nh_use; /* use count for selection */
121 int nh_seqcount; /* heuristic */
122 } nfsheur[NUM_HEURISTIC];
126 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
127 int nfsrvw_procrastinate_v3 = 0;
129 static struct timeval nfsver = { 0 };
131 SYSCTL_NODE(_vfs, OID_AUTO, nfsrv, CTLFLAG_RW, 0, "NFS server");
133 static int nfs_async;
134 static int nfs_commit_blks;
135 static int nfs_commit_miss;
136 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
137 "Tell client that writes were synced even though they were not");
138 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
139 "Number of completed commits");
140 SYSCTL_INT(_vfs_nfsrv, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
142 struct nfsrvstats nfsrvstats;
143 SYSCTL_STRUCT(_vfs_nfsrv, NFS_NFSRVSTATS, nfsrvstats, CTLFLAG_RW,
144 &nfsrvstats, nfsrvstats, "S,nfsrvstats");
146 static int nfsrv_access(struct vnode *, accmode_t, struct ucred *,
150 * Clear nameidata fields that are tested in nsfmout cleanup code prior
151 * to using first nfsm macro (that might jump to the cleanup code).
155 ndclear(struct nameidata *nd)
158 nd->ni_cnd.cn_flags = 0;
161 nd->ni_startdir = NULL;
162 nd->ni_strictrelative = 0;
166 * Heuristic to detect sequential operation.
168 static struct nfsheur *
169 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
174 /* Locate best candidate. */
176 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
179 if (nfsheur[hi].nh_vp == vp) {
183 if (nfsheur[hi].nh_use > 0)
184 --nfsheur[hi].nh_use;
185 hi = (hi + 1) % NUM_HEURISTIC;
186 if (nfsheur[hi].nh_use < nh->nh_use)
190 /* Initialize hint if this is a new file. */
191 if (nh->nh_vp != vp) {
193 nh->nh_nextoff = uio->uio_offset;
194 nh->nh_use = NHUSE_INIT;
195 if (uio->uio_offset == 0)
201 /* Calculate heuristic. */
202 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
203 uio->uio_offset == nh->nh_nextoff) {
204 /* See comments in vfs_vnops.c:sequential_heuristic(). */
205 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
206 if (nh->nh_seqcount > IO_SEQMAX)
207 nh->nh_seqcount = IO_SEQMAX;
208 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
209 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
210 /* Probably a reordered RPC, leave seqcount alone. */
211 } else if (nh->nh_seqcount > 1) {
212 nh->nh_seqcount /= 2;
216 nh->nh_use += NHUSE_INC;
217 if (nh->nh_use > NHUSE_MAX)
218 nh->nh_use = NHUSE_MAX;
223 * nfs v3 access service
226 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
229 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
230 struct sockaddr *nam = nfsd->nd_nam;
231 caddr_t dpos = nfsd->nd_dpos;
232 struct ucred *cred = nfsd->nd_cr;
233 struct vnode *vp = NULL;
238 int error = 0, rdonly, getret;
239 struct mbuf *mb, *mreq;
240 struct vattr vattr, *vap = &vattr;
241 u_long testmode, nfsmode;
242 int v3 = (nfsd->nd_flag & ND_NFSV3);
244 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
246 panic("nfsrv3_access: v3 proc called on a v2 connection");
247 fhp = &nfh.fh_generic;
249 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
250 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
252 nfsm_reply(NFSX_UNSIGNED);
253 nfsm_srvpostop_attr(1, NULL);
257 nfsmode = fxdr_unsigned(u_int32_t, *tl);
258 if ((nfsmode & NFSV3ACCESS_READ) &&
259 nfsrv_access(vp, VREAD, cred, rdonly, 0))
260 nfsmode &= ~NFSV3ACCESS_READ;
261 if (vp->v_type == VDIR)
262 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
265 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
266 if ((nfsmode & testmode) &&
267 nfsrv_access(vp, VWRITE, cred, rdonly, 0))
268 nfsmode &= ~testmode;
269 if (vp->v_type == VDIR)
270 testmode = NFSV3ACCESS_LOOKUP;
272 testmode = NFSV3ACCESS_EXECUTE;
273 if ((nfsmode & testmode) &&
274 nfsrv_access(vp, VEXEC, cred, rdonly, 0))
275 nfsmode &= ~testmode;
276 getret = VOP_GETATTR(vp, vap, cred);
279 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
280 nfsm_srvpostop_attr(getret, vap);
281 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
282 *tl = txdr_unsigned(nfsmode);
290 * nfs getattr service
293 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
296 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
297 struct sockaddr *nam = nfsd->nd_nam;
298 caddr_t dpos = nfsd->nd_dpos;
299 struct ucred *cred = nfsd->nd_cr;
300 struct nfs_fattr *fp;
302 struct vattr *vap = &va;
303 struct vnode *vp = NULL;
307 int error = 0, rdonly;
308 struct mbuf *mb, *mreq;
310 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
311 fhp = &nfh.fh_generic;
313 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
319 error = VOP_GETATTR(vp, vap, cred);
322 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
327 fp = nfsm_build(struct nfs_fattr *,
328 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
329 nfsm_srvfillattr(vap, fp);
339 * nfs setattr service
342 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
345 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
346 struct sockaddr *nam = nfsd->nd_nam;
347 caddr_t dpos = nfsd->nd_dpos;
348 struct ucred *cred = nfsd->nd_cr;
349 struct vattr va, preat;
350 struct vattr *vap = &va;
351 struct nfsv2_sattr *sp;
352 struct nfs_fattr *fp;
353 struct vnode *vp = NULL;
358 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
359 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
360 struct mbuf *mb, *mreq;
361 struct timespec guard = { 0, 0 };
362 struct mount *mp = NULL;
364 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
365 fhp = &nfh.fh_generic;
367 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
371 (void) vn_start_write(NULL, &mp, V_WAIT);
372 vfs_rel(mp); /* The write holds a ref. */
376 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
377 gcheck = fxdr_unsigned(int, *tl);
379 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
380 fxdr_nfsv3time(tl, &guard);
383 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
385 * Nah nah nah nah na nah
386 * There is a bug in the Sun client that puts 0xffff in the mode
387 * field of sattr when it should put in 0xffffffff. The u_short
388 * doesn't sign extend.
389 * --> check the low order 2 bytes for 0xffff
391 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
392 vap->va_mode = nfstov_mode(sp->sa_mode);
393 if (sp->sa_uid != nfsrv_nfs_xdrneg1)
394 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
395 if (sp->sa_gid != nfsrv_nfs_xdrneg1)
396 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
397 if (sp->sa_size != nfsrv_nfs_xdrneg1)
398 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
399 if (sp->sa_atime.nfsv2_sec != nfsrv_nfs_xdrneg1) {
401 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
403 vap->va_atime.tv_sec =
404 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
405 vap->va_atime.tv_nsec = 0;
408 if (sp->sa_mtime.nfsv2_sec != nfsrv_nfs_xdrneg1)
409 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
414 * Now that we have all the fields, lets do it.
416 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
418 nfsm_reply(2 * NFSX_UNSIGNED);
420 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
426 * vp now an active resource, pay careful attention to cleanup
429 error = preat_ret = VOP_GETATTR(vp, &preat, cred);
430 if (!error && gcheck &&
431 (preat.va_ctime.tv_sec != guard.tv_sec ||
432 preat.va_ctime.tv_nsec != guard.tv_nsec))
433 error = NFSERR_NOT_SYNC;
437 nfsm_reply(NFSX_WCCDATA(v3));
439 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
446 * If the size is being changed write acces is required, otherwise
447 * just check for a read only filesystem.
449 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
450 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
455 if (vp->v_type == VDIR) {
458 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
462 error = VOP_SETATTR(vp, vap, cred);
463 postat_ret = VOP_GETATTR(vp, vap, cred);
471 nfsm_reply(NFSX_WCCORFATTR(v3));
473 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, vap);
475 /* v2 non-error case. */
476 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
477 nfsm_srvfillattr(vap, fp);
485 vn_finished_write(mp);
493 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
496 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
497 struct sockaddr *nam = nfsd->nd_nam;
498 caddr_t dpos = nfsd->nd_dpos;
499 struct ucred *cred = nfsd->nd_cr;
500 struct nfs_fattr *fp;
501 struct nameidata nd, ind, *ndp = &nd;
502 struct vnode *vp, *dirp = NULL;
506 int error = 0, len, dirattr_ret = 1;
507 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
508 struct mbuf *mb, *mreq;
509 struct vattr va, dirattr, *vap = &va;
511 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
514 fhp = &nfh.fh_generic;
516 nfsm_srvnamesiz(len);
518 pubflag = nfs_ispublicfh(fhp);
520 nd.ni_cnd.cn_cred = cred;
521 nd.ni_cnd.cn_nameiop = LOOKUP;
522 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
523 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
524 &dirp, v3, &dirattr, &dirattr_ret, pubflag);
527 * namei failure, only dirp to cleanup. Clear out garbarge from
528 * structure in case macros jump to nfsmout.
536 nfsm_reply(NFSX_POSTOPATTR(v3));
538 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
544 * Locate index file for public filehandle
546 * error is 0 on entry and 0 on exit from this block.
550 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
552 * Setup call to lookup() to see if we can find
553 * the index file. Arguably, this doesn't belong
554 * in a kernel.. Ugh. If an error occurs, do not
555 * try to install an index file and then clear the
558 * When we replace nd with ind and redirect ndp,
559 * maintenance of ni_startdir and ni_vp shift to
560 * ind and we have to clean them up in the old nd.
561 * However, the cnd resource continues to be maintained
562 * via the original nd. Confused? You aren't alone!
565 VOP_UNLOCK(nd.ni_vp, 0);
566 ind.ni_pathlen = strlen(nfs_pub.np_index);
567 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
569 ind.ni_startdir = nd.ni_vp;
570 VREF(ind.ni_startdir);
571 error = lookup(&ind);
576 * Found an index file. Get rid of
577 * the old references. transfer nd.ni_vp'
583 vrele(nd.ni_startdir);
584 nd.ni_startdir = NULL;
590 * If the public filehandle was used, check that this lookup
591 * didn't result in a filehandle outside the publicly exported
592 * filesystem. We clear the poor vp here to avoid lockups due
596 if (ndp->ni_vp->v_mount != nfs_pub.np_mount) {
604 * Resources at this point:
605 * ndp->ni_vp may not be NULL
609 nfsm_reply(NFSX_POSTOPATTR(v3));
611 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
617 * Get underlying attribute, then release remaining resources ( for
618 * the same potential blocking reason ) and reply.
621 bzero((caddr_t)fhp, sizeof(nfh));
622 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
623 error = VOP_VPTOFH(vp, &fhp->fh_fid);
625 error = VOP_GETATTR(vp, vap, cred);
628 vrele(ndp->ni_startdir);
631 ndp->ni_startdir = NULL;
633 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
636 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
640 nfsm_srvfhtom(fhp, v3);
642 nfsm_srvpostop_attr(0, vap);
643 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
645 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
646 nfsm_srvfillattr(vap, fp);
650 if (ndp->ni_vp || dirp || ndp->ni_startdir) {
655 if (ndp->ni_startdir)
656 vrele(ndp->ni_startdir);
658 NDFREE(&nd, NDF_ONLY_PNBUF);
663 * nfs readlink service
666 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
669 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
670 struct sockaddr *nam = nfsd->nd_nam;
671 caddr_t dpos = nfsd->nd_dpos;
672 struct ucred *cred = nfsd->nd_cr;
673 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
674 struct iovec *ivp = iv;
678 int error = 0, rdonly, i, tlen, len, getret;
679 int v3 = (nfsd->nd_flag & ND_NFSV3);
680 struct mbuf *mb, *mp3, *nmp, *mreq;
681 struct vnode *vp = NULL;
685 struct uio io, *uiop = &io;
687 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
692 fhp = &nfh.fh_generic;
696 while (len < NFS_MAXPATHLEN) {
697 MGET(nmp, M_WAITOK, MT_DATA);
698 MCLGET(nmp, M_WAITOK);
699 nmp->m_len = NFSMSIZ(nmp);
706 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
707 mp->m_len = NFS_MAXPATHLEN - len;
708 len = NFS_MAXPATHLEN;
711 ivp->iov_base = mtod(mp, caddr_t);
712 ivp->iov_len = mp->m_len;
717 uiop->uio_iovcnt = i;
718 uiop->uio_offset = 0;
719 uiop->uio_resid = len;
720 uiop->uio_rw = UIO_READ;
721 uiop->uio_segflg = UIO_SYSSPACE;
723 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
725 nfsm_reply(2 * NFSX_UNSIGNED);
727 nfsm_srvpostop_attr(1, NULL);
731 if (vp->v_type != VLNK) {
737 error = VOP_READLINK(vp, uiop, cred);
738 getret = VOP_GETATTR(vp, &attr, cred);
741 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
743 nfsm_srvpostop_attr(getret, &attr);
748 if (uiop->uio_resid > 0) {
749 len -= uiop->uio_resid;
750 tlen = nfsm_rndup(len);
751 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
753 tl = nfsm_build(u_int32_t *, NFSX_UNSIGNED);
754 *tl = txdr_unsigned(len);
769 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
772 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
773 struct sockaddr *nam = nfsd->nd_nam;
774 caddr_t dpos = nfsd->nd_dpos;
775 struct ucred *cred = nfsd->nd_cr;
779 struct nfs_fattr *fp;
783 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
784 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
785 struct mbuf *mb, *mreq;
787 struct vnode *vp = NULL;
790 struct uio io, *uiop = &io;
791 struct vattr va, *vap = &va;
796 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
797 fhp = &nfh.fh_generic;
800 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
801 off = fxdr_hyper(tl);
803 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
804 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
806 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
809 * Reference vp. If an error occurs, vp will be invalid, but we
810 * have to NULL it just in case. The macros might goto nfsmout
814 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
817 nfsm_reply(2 * NFSX_UNSIGNED);
819 nfsm_srvpostop_attr(1, NULL);
824 if (vp->v_type != VREG) {
828 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
831 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, 1)) != 0)
832 error = nfsrv_access(vp, VEXEC, cred, rdonly, 1);
834 getret = VOP_GETATTR(vp, vap, cred);
840 nfsm_reply(NFSX_POSTOPATTR(v3));
842 nfsm_srvpostop_attr(getret, vap);
848 * Calculate byte count to read
850 if (off >= vap->va_size)
852 else if ((off + reqlen) > vap->va_size)
853 cnt = vap->va_size - off;
857 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
859 tl = nfsm_build(u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
860 *tl++ = nfsrv_nfs_true;
861 fp = (struct nfs_fattr *)tl;
862 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
864 tl = nfsm_build(u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
865 fp = (struct nfs_fattr *)tl;
866 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
868 len = left = nfsm_rndup(cnt);
871 * Generate the mbuf list with the uio_iov ref. to it.
876 siz = min(M_TRAILINGSPACE(m), left);
882 MGET(m, M_WAITOK, MT_DATA);
889 iv = malloc(i * sizeof (struct iovec),
891 uiop->uio_iov = iv2 = iv;
897 panic("nfsrv_read iov");
898 siz = min(M_TRAILINGSPACE(m), left);
900 iv->iov_base = mtod(m, caddr_t) + m->m_len;
909 uiop->uio_iovcnt = i;
910 uiop->uio_offset = off;
911 uiop->uio_resid = len;
912 uiop->uio_rw = UIO_READ;
913 uiop->uio_segflg = UIO_SYSSPACE;
915 nh = nfsrv_sequential_heuristic(uiop, vp);
916 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
917 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
919 nh->nh_nextoff = uiop->uio_offset;
920 free((caddr_t)iv2, M_TEMP);
921 if (error || (getret = VOP_GETATTR(vp, vap, cred))) {
927 nfsm_reply(NFSX_POSTOPATTR(v3));
929 nfsm_srvpostop_attr(getret, vap);
937 nfsm_srvfillattr(vap, fp);
938 tlen = len - uiop->uio_resid;
939 cnt = cnt < tlen ? cnt : tlen;
940 tlen = nfsm_rndup(cnt);
941 if (len != tlen || tlen != cnt)
942 nfsm_adj(mb, len - tlen, tlen - cnt);
944 *tl++ = txdr_unsigned(cnt);
946 *tl++ = nfsrv_nfs_true;
948 *tl++ = nfsrv_nfs_false;
950 *tl = txdr_unsigned(cnt);
961 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
964 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
965 struct sockaddr *nam = nfsd->nd_nam;
966 caddr_t dpos = nfsd->nd_dpos;
967 struct ucred *cred = nfsd->nd_cr;
971 struct nfs_fattr *fp;
973 struct vattr va, forat;
974 struct vattr *vap = &va;
977 int error = 0, rdonly, len, forat_ret = 1;
978 int ioflags, aftat_ret = 1, retlen = 0, zeroing, adjust;
979 int stable = NFSV3WRITE_FILESYNC;
980 int v3 = (nfsd->nd_flag & ND_NFSV3);
981 struct mbuf *mb, *mreq;
982 struct vnode *vp = NULL;
986 struct uio io, *uiop = &io;
988 struct mount *mntp = NULL;
990 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
996 fhp = &nfh.fh_generic;
998 if ((mntp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1002 (void) vn_start_write(NULL, &mntp, V_WAIT);
1003 vfs_rel(mntp); /* The write holds a ref. */
1005 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
1006 off = fxdr_hyper(tl);
1008 stable = fxdr_unsigned(int, *tl++);
1010 tl = nfsm_dissect_nonblock(u_int32_t *, 4 * NFSX_UNSIGNED);
1011 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1014 stable = NFSV3WRITE_UNSTABLE;
1016 retlen = len = fxdr_unsigned(int32_t, *tl);
1020 * For NFS Version 2, it is not obvious what a write of zero length
1021 * should do, but I might as well be consistent with Version 3,
1022 * which is to return ok so long as there are no permission problems.
1030 adjust = dpos - mtod(mp, caddr_t);
1031 mp->m_len -= adjust;
1032 if (mp->m_len > 0 && adjust > 0)
1033 mp->m_data += adjust;
1037 else if (mp->m_len > 0) {
1040 mp->m_len -= (i - len);
1049 if (len > NFS_MAXDATA || len < 0 || i < len) {
1051 nfsm_reply(2 * NFSX_UNSIGNED);
1053 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1057 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
1060 nfsm_reply(2 * NFSX_UNSIGNED);
1062 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1067 forat_ret = VOP_GETATTR(vp, &forat, cred);
1068 if (vp->v_type != VREG) {
1072 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1075 error = nfsrv_access(vp, VWRITE, cred, rdonly, 1);
1079 nfsm_reply(NFSX_WCCDATA(v3));
1081 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1087 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1089 uiop->uio_iov = iv = ivp;
1090 uiop->uio_iovcnt = cnt;
1093 if (mp->m_len > 0) {
1094 ivp->iov_base = mtod(mp, caddr_t);
1095 ivp->iov_len = mp->m_len;
1103 * The IO_METASYNC flag indicates that all metadata (and not just
1104 * enough to ensure data integrity) mus be written to stable storage
1106 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1108 if (stable == NFSV3WRITE_UNSTABLE)
1109 ioflags = IO_NODELOCKED;
1110 else if (stable == NFSV3WRITE_DATASYNC)
1111 ioflags = (IO_SYNC | IO_NODELOCKED);
1113 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1114 uiop->uio_resid = len;
1115 uiop->uio_rw = UIO_WRITE;
1116 uiop->uio_segflg = UIO_SYSSPACE;
1117 uiop->uio_td = NULL;
1118 uiop->uio_offset = off;
1119 nh = nfsrv_sequential_heuristic(uiop, vp);
1120 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1121 error = VOP_WRITE(vp, uiop, ioflags, cred);
1123 nh->nh_nextoff = uiop->uio_offset;
1124 /* Unlocked write. */
1125 nfsrvstats.srvvop_writes++;
1126 free((caddr_t)iv, M_TEMP);
1128 aftat_ret = VOP_GETATTR(vp, vap, cred);
1134 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
1135 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
1137 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, vap);
1142 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
1143 *tl++ = txdr_unsigned(retlen);
1145 * If nfs_async is set, then pretend the write was FILESYNC.
1147 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1148 *tl++ = txdr_unsigned(stable);
1150 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1152 * Actually, there is no need to txdr these fields,
1153 * but it may make the values more human readable,
1154 * for debugging purposes.
1156 if (nfsver.tv_sec == 0)
1158 *tl++ = txdr_unsigned(nfsver.tv_sec);
1159 *tl = txdr_unsigned(nfsver.tv_usec);
1160 } else if (!error) {
1161 /* v2 non-error case. */
1162 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1163 nfsm_srvfillattr(vap, fp);
1169 vn_finished_write(mntp);
1174 * nfs create service
1175 * now does a truncate to 0 length via. setattr if it already exists
1178 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1181 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1182 struct sockaddr *nam = nfsd->nd_nam;
1183 caddr_t dpos = nfsd->nd_dpos;
1184 struct ucred *cred = nfsd->nd_cr;
1185 struct nfs_fattr *fp;
1186 struct vattr va, dirfor, diraft;
1187 struct vattr *vap = &va;
1188 struct nfsv2_sattr *sp;
1190 struct nameidata nd;
1192 int error = 0, rdev, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1193 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1194 struct mbuf *mb, *mreq;
1195 struct vnode *dirp = NULL;
1199 struct timespec cverf;
1200 struct mount *mp = NULL;
1202 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1208 fhp = &nfh.fh_generic;
1210 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1214 (void) vn_start_write(NULL, &mp, V_WAIT);
1215 vfs_rel(mp); /* The write holds a ref. */
1216 nfsm_srvnamesiz(len);
1218 nd.ni_cnd.cn_cred = cred;
1219 nd.ni_cnd.cn_nameiop = CREATE;
1220 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE;
1223 * Call namei and do initial cleanup to get a few things
1224 * out of the way. If we get an initial error we cleanup
1225 * and return here to avoid special-casing the invalid nd
1226 * structure through the rest of the case. dirp may be
1227 * set even if an error occurs, but the nd structure will not
1228 * be valid at all if an error occurs so we have to invalidate it
1229 * prior to calling nfsm_reply ( which might goto nfsmout ).
1231 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
1232 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1238 nfsm_reply(NFSX_WCCDATA(v3));
1240 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1246 * No error. Continue. State:
1248 * startdir is valid ( we release this immediately )
1250 * nd.ni_vp may be valid
1251 * nd.ni_dvp is valid
1253 * The error state is set through the code and we may also do some
1254 * opportunistic releasing of vnodes to avoid holding locks through
1255 * NFS I/O. The cleanup at the end is a catch-all
1260 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1261 how = fxdr_unsigned(int, *tl);
1263 case NFSV3CREATE_GUARDED:
1269 case NFSV3CREATE_UNCHECKED:
1272 case NFSV3CREATE_EXCLUSIVE:
1273 tl = nfsm_dissect_nonblock(u_int32_t *,
1275 /* Unique bytes, endianness is not important. */
1276 cverf.tv_sec = (int32_t)tl[0];
1277 cverf.tv_nsec = tl[1];
1281 vap->va_type = VREG;
1283 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
1284 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1285 if (vap->va_type == VNON)
1286 vap->va_type = VREG;
1287 vap->va_mode = nfstov_mode(sp->sa_mode);
1288 switch (vap->va_type) {
1290 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1292 vap->va_size = (u_quad_t)tsize;
1297 rdev = fxdr_unsigned(long, sp->sa_size);
1305 * Iff doesn't exist, create it
1306 * otherwise just truncate to 0 length
1307 * should I set the mode too ?
1309 * The only possible error we can have at this point is EEXIST.
1310 * nd.ni_vp will also be non-NULL in that case.
1312 if (nd.ni_vp == NULL) {
1313 if (vap->va_mode == (mode_t)VNOVAL)
1315 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1316 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1318 NDFREE(&nd, NDF_ONLY_PNBUF);
1320 if (exclusive_flag) {
1323 vap->va_atime = cverf;
1324 error = VOP_SETATTR(nd.ni_vp, vap,
1328 } else if (vap->va_type == VCHR || vap->va_type == VBLK ||
1329 vap->va_type == VFIFO) {
1331 * NFSv2-specific code for creating device nodes
1334 * Handle SysV FIFO node special cases. All other
1335 * devices require super user to access.
1337 if (vap->va_type == VCHR && rdev == 0xffffffff)
1338 vap->va_type = VFIFO;
1339 if (vap->va_type != VFIFO &&
1340 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV,
1344 vap->va_rdev = rdev;
1345 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1347 NDFREE(&nd, NDF_ONLY_PNBUF);
1354 * release dvp prior to lookup
1361 * Even though LOCKPARENT was cleared, ni_dvp may
1364 nd.ni_cnd.cn_nameiop = LOOKUP;
1365 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1366 nd.ni_cnd.cn_thread = curthread;
1367 nd.ni_cnd.cn_cred = cred;
1368 error = lookup(&nd);
1373 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1381 if (vap->va_size != -1) {
1382 error = nfsrv_access(nd.ni_vp, VWRITE,
1383 cred, (nd.ni_cnd.cn_flags & RDONLY), 0);
1385 tempsize = vap->va_size;
1387 vap->va_size = tempsize;
1388 error = VOP_SETATTR(nd.ni_vp, vap, cred);
1394 bzero((caddr_t)fhp, sizeof(nfh));
1395 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
1396 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
1398 error = VOP_GETATTR(nd.ni_vp, vap, cred);
1401 if (exclusive_flag && !error &&
1402 bcmp(&cverf, &vap->va_atime, sizeof (cverf)))
1404 if (dirp == nd.ni_dvp)
1405 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1407 /* Drop the other locks to avoid deadlock. */
1409 if (nd.ni_dvp == nd.ni_vp)
1419 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1420 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1421 VOP_UNLOCK(dirp, 0);
1425 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1428 nfsm_srvpostop_fh(fhp);
1429 nfsm_srvpostop_attr(0, vap);
1431 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1432 } else if (!error) {
1433 /* v2 non-error case. */
1434 nfsm_srvfhtom(fhp, v3);
1435 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
1436 nfsm_srvfillattr(vap, fp);
1442 if (nd.ni_dvp == nd.ni_vp)
1449 if (nd.ni_startdir) {
1450 vrele(nd.ni_startdir);
1451 nd.ni_startdir = NULL;
1455 NDFREE(&nd, NDF_ONLY_PNBUF);
1456 vn_finished_write(mp);
1461 * nfs v3 mknod service
1464 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1467 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1468 struct sockaddr *nam = nfsd->nd_nam;
1469 caddr_t dpos = nfsd->nd_dpos;
1470 struct ucred *cred = nfsd->nd_cr;
1471 struct vattr va, dirfor, diraft;
1472 struct vattr *vap = &va;
1473 struct thread *td = curthread;
1475 struct nameidata nd;
1477 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1478 u_int32_t major, minor;
1480 struct mbuf *mb, *mreq;
1481 struct vnode *vp, *dirp = NULL;
1484 struct mount *mp = NULL;
1485 int v3 = (nfsd->nd_flag & ND_NFSV3);
1487 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1489 panic("nfsrv_mknod: v3 proc called on a v2 connection");
1492 fhp = &nfh.fh_generic;
1494 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1498 (void) vn_start_write(NULL, &mp, V_WAIT);
1499 vfs_rel(mp); /* The write holds a ref. */
1500 nfsm_srvnamesiz(len);
1502 nd.ni_cnd.cn_cred = cred;
1503 nd.ni_cnd.cn_nameiop = CREATE;
1504 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE;
1507 * Handle nfs_namei() call. If an error occurs, the nd structure
1508 * is not valid. However, nfsm_*() routines may still jump to
1512 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
1513 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1515 nfsm_reply(NFSX_WCCDATA(1));
1516 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1520 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
1521 vtyp = nfsv3tov_type(*tl);
1522 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1523 error = NFSERR_BADTYPE;
1528 if (vtyp == VCHR || vtyp == VBLK) {
1529 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
1530 major = fxdr_unsigned(u_int32_t, *tl++);
1531 minor = fxdr_unsigned(u_int32_t, *tl);
1532 vap->va_rdev = makedev(major, minor);
1536 * Iff doesn't exist, create it.
1542 vap->va_type = vtyp;
1543 if (vap->va_mode == (mode_t)VNOVAL)
1545 if (vtyp == VSOCK) {
1546 vrele(nd.ni_startdir);
1547 nd.ni_startdir = NULL;
1548 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1550 NDFREE(&nd, NDF_ONLY_PNBUF);
1552 if (vtyp != VFIFO && (error = priv_check_cred(cred,
1553 PRIV_VFS_MKNOD_DEV, 0)))
1555 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
1557 NDFREE(&nd, NDF_ONLY_PNBUF);
1564 * Release dvp prior to lookup
1569 nd.ni_cnd.cn_nameiop = LOOKUP;
1570 nd.ni_cnd.cn_flags &= ~(LOCKPARENT);
1571 nd.ni_cnd.cn_thread = td;
1572 nd.ni_cnd.cn_cred = td->td_ucred;
1573 error = lookup(&nd);
1578 if (nd.ni_cnd.cn_flags & ISSYMLINK)
1583 * send response, cleanup, return.
1588 bzero((caddr_t)fhp, sizeof(nfh));
1589 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1590 error = VOP_VPTOFH(vp, &fhp->fh_fid);
1592 error = VOP_GETATTR(vp, vap, cred);
1595 if (nd.ni_dvp == nd.ni_vp)
1606 if (nd.ni_startdir) {
1607 vrele(nd.ni_startdir);
1608 nd.ni_startdir = NULL;
1610 NDFREE(&nd, NDF_ONLY_PNBUF);
1612 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1613 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1617 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1620 nfsm_srvpostop_fh(fhp);
1621 nfsm_srvpostop_attr(0, vap);
1623 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1625 vn_finished_write(mp);
1629 if (nd.ni_dvp == nd.ni_vp)
1639 vrele(nd.ni_startdir);
1640 NDFREE(&nd, NDF_ONLY_PNBUF);
1641 vn_finished_write(mp);
1646 * nfs remove service
1649 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1652 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1653 struct sockaddr *nam = nfsd->nd_nam;
1654 caddr_t dpos = nfsd->nd_dpos;
1655 struct ucred *cred = nfsd->nd_cr;
1656 struct nameidata nd;
1658 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1659 int v3 = (nfsd->nd_flag & ND_NFSV3);
1660 struct mbuf *mb, *mreq;
1662 struct vattr dirfor, diraft;
1665 struct mount *mp = NULL;
1667 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1670 fhp = &nfh.fh_generic;
1672 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
1676 (void) vn_start_write(NULL, &mp, V_WAIT);
1677 vfs_rel(mp); /* The write holds a ref. */
1678 nfsm_srvnamesiz(len);
1680 nd.ni_cnd.cn_cred = cred;
1681 nd.ni_cnd.cn_nameiop = DELETE;
1682 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1683 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
1684 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
1690 if (nd.ni_vp->v_type == VDIR) {
1691 error = EPERM; /* POSIX */
1695 * The root of a mounted filesystem cannot be deleted.
1697 if (nd.ni_vp->v_vflag & VV_ROOT) {
1703 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1704 NDFREE(&nd, NDF_ONLY_PNBUF);
1708 if (dirp == nd.ni_dvp)
1709 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1711 /* Drop the other locks to avoid deadlock. */
1713 if (nd.ni_dvp == nd.ni_vp)
1723 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
1724 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
1725 VOP_UNLOCK(dirp, 0);
1731 nfsm_reply(NFSX_WCCDATA(v3));
1733 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1736 NDFREE(&nd, NDF_ONLY_PNBUF);
1738 if (nd.ni_dvp == nd.ni_vp)
1745 vn_finished_write(mp);
1750 * nfs rename service
1753 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1756 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1757 struct sockaddr *nam = nfsd->nd_nam;
1758 caddr_t dpos = nfsd->nd_dpos;
1759 struct ucred *cred = nfsd->nd_cr;
1761 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
1762 int tdirfor_ret = 1, tdiraft_ret = 1;
1763 int v3 = (nfsd->nd_flag & ND_NFSV3);
1764 struct mbuf *mb, *mreq;
1765 struct nameidata fromnd, tond;
1766 struct vnode *fvp, *tvp, *tdvp, *fdirp = NULL;
1767 struct vnode *tdirp = NULL;
1768 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1770 fhandle_t *ffhp, *tfhp;
1772 struct mount *mp = NULL;
1774 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1778 ffhp = &fnfh.fh_generic;
1779 tfhp = &tnfh.fh_generic;
1782 * Clear fields incase goto nfsmout occurs from macro.
1788 nfsm_srvmtofh(ffhp);
1789 if ((mp = vfs_getvfs(&ffhp->fh_fsid)) == NULL) {
1793 (void) vn_start_write(NULL, &mp, V_WAIT);
1794 vfs_rel(mp); /* The write holds a ref. */
1795 nfsm_srvnamesiz(len);
1797 * Remember our original uid so that we can reset cr_uid before
1798 * the second nfs_namei() call, in case it is remapped.
1800 saved_uid = cred->cr_uid;
1801 fromnd.ni_cnd.cn_cred = cred;
1802 fromnd.ni_cnd.cn_nameiop = DELETE;
1803 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
1804 error = nfs_namei(&fromnd, nfsd, ffhp, len, slp, nam, &md,
1805 &dpos, &fdirp, v3, &fdirfor, &fdirfor_ret, FALSE);
1811 nfsm_reply(2 * NFSX_WCCDATA(v3));
1813 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1814 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1820 nfsm_srvmtofh(tfhp);
1821 nfsm_srvnamesiz(len2);
1822 cred->cr_uid = saved_uid;
1823 tond.ni_cnd.cn_cred = cred;
1824 tond.ni_cnd.cn_nameiop = RENAME;
1825 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
1826 error = nfs_namei(&tond, nfsd, tfhp, len2, slp, nam, &md,
1827 &dpos, &tdirp, v3, &tdirfor, &tdirfor_ret, FALSE);
1838 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1844 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1851 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1859 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1866 if (fvp->v_mount != tdvp->v_mount) {
1880 * If source is the same as the destination (that is the
1881 * same vnode with the same name in the same directory),
1882 * then there is nothing to do.
1884 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1885 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1886 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1887 fromnd.ni_cnd.cn_namelen))
1892 * The VOP_RENAME function releases all vnode references &
1893 * locks prior to returning so we need to clear the pointers
1894 * to bypass cleanup code later on.
1896 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1897 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1898 fromnd.ni_dvp = NULL;
1899 fromnd.ni_vp = NULL;
1903 NDFREE(&fromnd, NDF_ONLY_PNBUF);
1904 NDFREE(&tond, NDF_ONLY_PNBUF);
1912 nfsm_reply(2 * NFSX_WCCDATA(v3));
1914 /* Release existing locks to prevent deadlock. */
1916 if (tond.ni_dvp == tond.ni_vp)
1927 vn_lock(fdirp, LK_EXCLUSIVE | LK_RETRY);
1928 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred);
1929 VOP_UNLOCK(fdirp, 0);
1932 vn_lock(tdirp, LK_EXCLUSIVE | LK_RETRY);
1933 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred);
1934 VOP_UNLOCK(tdirp, 0);
1936 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1937 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1944 * Clear out tond related fields
1947 if (tond.ni_dvp == tond.ni_vp)
1956 if (tond.ni_startdir)
1957 vrele(tond.ni_startdir);
1958 NDFREE(&tond, NDF_ONLY_PNBUF);
1960 * Clear out fromnd related fields
1964 if (fromnd.ni_startdir)
1965 vrele(fromnd.ni_startdir);
1966 NDFREE(&fromnd, NDF_ONLY_PNBUF);
1968 vrele(fromnd.ni_dvp);
1970 vrele(fromnd.ni_vp);
1972 vn_finished_write(mp);
1980 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1983 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1984 struct sockaddr *nam = nfsd->nd_nam;
1985 caddr_t dpos = nfsd->nd_dpos;
1986 struct ucred *cred = nfsd->nd_cr;
1987 struct nameidata nd;
1989 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
1990 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
1991 struct mbuf *mb, *mreq;
1992 struct vnode *vp = NULL, *xp, *dirp = NULL;
1993 struct vattr dirfor, diraft, at;
1995 fhandle_t *fhp, *dfhp;
1996 struct mount *mp = NULL;
1998 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2001 fhp = &nfh.fh_generic;
2002 dfhp = &dnfh.fh_generic;
2004 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2008 (void) vn_start_write(NULL, &mp, V_WAIT);
2009 vfs_rel(mp); /* The write holds a ref. */
2010 nfsm_srvmtofh(dfhp);
2011 nfsm_srvnamesiz(len);
2013 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
2015 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2017 nfsm_srvpostop_attr(getret, &at);
2018 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2025 getret = VOP_GETATTR(vp, &at, cred);
2026 if (vp->v_type == VDIR) {
2027 error = EPERM; /* POSIX */
2031 nd.ni_cnd.cn_cred = cred;
2032 nd.ni_cnd.cn_nameiop = CREATE;
2033 nd.ni_cnd.cn_flags = LOCKPARENT | NOCACHE;
2034 error = nfs_namei(&nd, nfsd, dfhp, len, slp, nam, &md, &dpos,
2035 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2053 if (vp->v_mount != xp->v_mount) {
2059 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2060 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2061 NDFREE(&nd, NDF_ONLY_PNBUF);
2066 getret = VOP_GETATTR(vp, &at, cred);
2069 if (dirp == nd.ni_dvp)
2070 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2072 /* Release existing locks to prevent deadlock. */
2074 if (nd.ni_dvp == nd.ni_vp)
2084 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2085 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2086 VOP_UNLOCK(dirp, 0);
2090 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2092 nfsm_srvpostop_attr(getret, &at);
2093 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2099 NDFREE(&nd, NDF_ONLY_PNBUF);
2103 if (nd.ni_dvp == nd.ni_vp)
2112 vn_finished_write(mp);
2117 * nfs symbolic link service
2120 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2123 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2124 struct sockaddr *nam = nfsd->nd_nam;
2125 caddr_t dpos = nfsd->nd_dpos;
2126 struct ucred *cred = nfsd->nd_cr;
2127 struct vattr va, dirfor, diraft;
2128 struct nameidata nd;
2129 struct vattr *vap = &va;
2130 struct nfsv2_sattr *sp;
2131 char *bpos, *pathcp = NULL;
2134 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2135 int v3 = (nfsd->nd_flag & ND_NFSV3);
2136 struct mbuf *mb, *mreq;
2137 struct vnode *dirp = NULL;
2140 struct mount *mp = NULL;
2142 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2145 fhp = &nfh.fh_generic;
2147 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2151 (void) vn_start_write(NULL, &mp, V_WAIT);
2152 vfs_rel(mp); /* The write holds a ref. */
2153 nfsm_srvnamesiz(len);
2154 nd.ni_cnd.cn_cred = cred;
2155 nd.ni_cnd.cn_nameiop = CREATE;
2156 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART | NOCACHE;
2157 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2158 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2163 nfsm_srvpathsiz(len2);
2171 pathcp = malloc(len2 + 1, M_TEMP, M_WAITOK);
2172 iv.iov_base = pathcp;
2174 io.uio_resid = len2;
2178 io.uio_segflg = UIO_SYSSPACE;
2179 io.uio_rw = UIO_READ;
2181 nfsm_mtouio(&io, len2);
2183 sp = nfsm_dissect_nonblock(struct nfsv2_sattr *, NFSX_V2SATTR);
2184 vap->va_mode = nfstov_mode(sp->sa_mode);
2186 *(pathcp + len2) = '\0';
2193 * issue symlink op. SAVESTART is set so the underlying path component
2194 * is only freed by the VOP if an error occurs.
2196 if (vap->va_mode == (mode_t)VNOVAL)
2198 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
2200 NDFREE(&nd, NDF_ONLY_PNBUF);
2205 * releases directory prior to potential lookup op.
2213 * Issue lookup. Leave SAVESTART set so we can easily free
2214 * the name buffer later on.
2216 * since LOCKPARENT is not set, ni_dvp will be garbage on
2217 * return whether an error occurs or not.
2219 nd.ni_cnd.cn_nameiop = LOOKUP;
2220 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | FOLLOW);
2221 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2222 nd.ni_cnd.cn_thread = curthread;
2223 nd.ni_cnd.cn_cred = cred;
2224 error = lookup(&nd);
2228 bzero((caddr_t)fhp, sizeof(nfh));
2229 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2230 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2232 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2240 * These releases aren't strictly required, does even doing them
2241 * make any sense? XXX can nfsm_reply() block?
2244 free(pathcp, M_TEMP);
2248 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2249 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2250 VOP_UNLOCK(dirp, 0);
2252 if (nd.ni_startdir) {
2253 vrele(nd.ni_startdir);
2254 nd.ni_startdir = NULL;
2256 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2259 nfsm_srvpostop_fh(fhp);
2260 nfsm_srvpostop_attr(0, vap);
2262 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2268 NDFREE(&nd, NDF_ONLY_PNBUF);
2270 if (nd.ni_dvp == nd.ni_vp)
2278 vrele(nd.ni_startdir);
2282 free(pathcp, M_TEMP);
2284 vn_finished_write(mp);
2292 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2295 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2296 struct sockaddr *nam = nfsd->nd_nam;
2297 caddr_t dpos = nfsd->nd_dpos;
2298 struct ucred *cred = nfsd->nd_cr;
2299 struct vattr va, dirfor, diraft;
2300 struct vattr *vap = &va;
2301 struct nfs_fattr *fp;
2302 struct nameidata nd;
2305 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2306 int v3 = (nfsd->nd_flag & ND_NFSV3);
2307 struct mbuf *mb, *mreq;
2308 struct vnode *dirp = NULL;
2312 struct mount *mp = NULL;
2314 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2317 fhp = &nfh.fh_generic;
2319 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2323 (void) vn_start_write(NULL, &mp, V_WAIT);
2324 vfs_rel(mp); /* The write holds a ref. */
2325 nfsm_srvnamesiz(len);
2326 nd.ni_cnd.cn_cred = cred;
2327 nd.ni_cnd.cn_nameiop = CREATE;
2328 nd.ni_cnd.cn_flags = LOCKPARENT | NOCACHE;
2330 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2331 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2337 nfsm_reply(NFSX_WCCDATA(v3));
2339 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2347 tl = nfsm_dissect_nonblock(u_int32_t *, NFSX_UNSIGNED);
2348 vap->va_mode = nfstov_mode(*tl++);
2352 * At this point nd.ni_dvp is referenced and exclusively locked and
2353 * nd.ni_vp, if it exists, is referenced but not locked.
2356 vap->va_type = VDIR;
2357 if (nd.ni_vp != NULL) {
2358 NDFREE(&nd, NDF_ONLY_PNBUF);
2364 * Issue mkdir op. Since SAVESTART is not set, the pathname
2365 * component is freed by the VOP call. This will fill-in
2366 * nd.ni_vp, reference, and exclusively lock it.
2368 if (vap->va_mode == (mode_t)VNOVAL)
2370 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap);
2371 NDFREE(&nd, NDF_ONLY_PNBUF);
2378 bzero((caddr_t)fhp, sizeof(nfh));
2379 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2380 error = VOP_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2382 error = VOP_GETATTR(nd.ni_vp, vap, cred);
2386 if (dirp == nd.ni_dvp) {
2387 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2389 /* Release existing locks to prevent deadlock. */
2391 NDFREE(&nd, NDF_ONLY_PNBUF);
2392 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2405 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2406 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2407 VOP_UNLOCK(dirp, 0);
2410 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2413 nfsm_srvpostop_fh(fhp);
2414 nfsm_srvpostop_attr(0, vap);
2416 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2417 } else if (!error) {
2418 /* v2 non-error case. */
2419 nfsm_srvfhtom(fhp, v3);
2420 fp = nfsm_build(struct nfs_fattr *, NFSX_V2FATTR);
2421 nfsm_srvfillattr(vap, fp);
2428 NDFREE(&nd, NDF_ONLY_PNBUF);
2429 if (nd.ni_dvp == nd.ni_vp && vpexcl)
2442 vn_finished_write(mp);
2450 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2453 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2454 struct sockaddr *nam = nfsd->nd_nam;
2455 caddr_t dpos = nfsd->nd_dpos;
2456 struct ucred *cred = nfsd->nd_cr;
2458 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2459 int v3 = (nfsd->nd_flag & ND_NFSV3);
2460 struct mbuf *mb, *mreq;
2461 struct vnode *vp, *dirp = NULL;
2462 struct vattr dirfor, diraft;
2465 struct nameidata nd;
2466 struct mount *mp = NULL;
2468 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2471 fhp = &nfh.fh_generic;
2473 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
2477 (void) vn_start_write(NULL, &mp, V_WAIT);
2478 vfs_rel(mp); /* The write holds a ref. */
2479 nfsm_srvnamesiz(len);
2480 nd.ni_cnd.cn_cred = cred;
2481 nd.ni_cnd.cn_nameiop = DELETE;
2482 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2483 error = nfs_namei(&nd, nfsd, fhp, len, slp, nam, &md, &dpos,
2484 &dirp, v3, &dirfor, &dirfor_ret, FALSE);
2490 nfsm_reply(NFSX_WCCDATA(v3));
2492 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2497 if (vp->v_type != VDIR) {
2502 * No rmdir "." please.
2504 if (nd.ni_dvp == vp) {
2509 * The root of a mounted filesystem cannot be deleted.
2511 if (vp->v_vflag & VV_ROOT)
2515 * Issue or abort op. Since SAVESTART is not set, path name
2516 * component is freed by the VOP after either.
2519 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2520 NDFREE(&nd, NDF_ONLY_PNBUF);
2523 if (dirp == nd.ni_dvp)
2524 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2526 /* Release existing locks to prevent deadlock. */
2528 if (nd.ni_dvp == nd.ni_vp)
2537 vn_lock(dirp, LK_EXCLUSIVE | LK_RETRY);
2538 diraft_ret = VOP_GETATTR(dirp, &diraft, cred);
2539 VOP_UNLOCK(dirp, 0);
2542 nfsm_reply(NFSX_WCCDATA(v3));
2545 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2549 NDFREE(&nd, NDF_ONLY_PNBUF);
2551 if (nd.ni_dvp == nd.ni_vp)
2561 vn_finished_write(mp);
2566 * nfs readdir service
2567 * - mallocs what it thinks is enough to read
2568 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2569 * - calls VOP_READDIR()
2570 * - loops around building the reply
2571 * if the output generated exceeds count break out of loop
2572 * The nfsm_clget macro is used here so that the reply will be packed
2573 * tightly in mbuf clusters.
2574 * - it only knows that it has encountered eof when the VOP_READDIR()
2576 * - as such one readdir rpc will return eof false although you are there
2577 * and then the next will return eof
2578 * - it trims out records with d_fileno == 0
2579 * this doesn't matter for Unix clients, but they might confuse clients
2581 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2582 * than requested, but this may not apply to all filesystems. For
2583 * example, client NFS does not { although it is never remote mounted
2585 * The alternate call nfsrv_readdirplus() does lookups as well.
2586 * PS: The NFS protocol spec. does not clarify what the "count" byte
2587 * argument is a count of.. just name strings and file id's or the
2588 * entire reply rpc or ...
2589 * I tried just file name and id sizes and it confused the Sun client,
2590 * so I am using the full rpc size now. The "paranoia.." comment refers
2591 * to including the status longwords that are not a part of the dir.
2592 * "entry" structures, but are in the rpc.
2596 u_int32_t fl_postopok;
2597 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2599 u_int32_t fl_fhsize;
2600 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2604 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2607 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2608 struct sockaddr *nam = nfsd->nd_nam;
2609 caddr_t dpos = nfsd->nd_dpos;
2610 struct ucred *cred = nfsd->nd_cr;
2617 struct mbuf *mb, *mreq;
2618 char *cpos, *cend, *rbuf;
2619 struct vnode *vp = NULL;
2625 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2626 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2627 int v3 = (nfsd->nd_flag & ND_NFSV3);
2628 u_quad_t off, toff, verf;
2629 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2632 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2633 fhp = &nfh.fh_generic;
2636 tl = nfsm_dissect_nonblock(u_int32_t *, 5 * NFSX_UNSIGNED);
2637 toff = fxdr_hyper(tl);
2639 verf = fxdr_hyper(tl);
2642 tl = nfsm_dissect_nonblock(u_int32_t *, 2 * NFSX_UNSIGNED);
2643 toff = fxdr_unsigned(u_quad_t, *tl++);
2644 verf = 0; /* shut up gcc */
2647 cnt = fxdr_unsigned(int, *tl);
2648 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2649 xfer = NFS_SRVMAXDATA(nfsd);
2655 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
2656 if (!error && vp->v_type != VDIR) {
2662 nfsm_reply(NFSX_UNSIGNED);
2664 nfsm_srvpostop_attr(getret, &at);
2670 * Obtain lock on vnode for this section of the code
2673 error = getret = VOP_GETATTR(vp, &at, cred);
2676 * XXX This check may be too strict for Solaris 2.5 clients.
2678 if (!error && toff && verf && verf != at.va_filerev)
2679 error = NFSERR_BAD_COOKIE;
2683 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
2687 nfsm_reply(NFSX_POSTOPATTR(v3));
2689 nfsm_srvpostop_attr(getret, &at);
2693 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2697 * end section. Allocate rbuf and continue
2699 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2702 iv.iov_len = fullsiz;
2705 io.uio_offset = (off_t)off;
2706 io.uio_resid = fullsiz;
2707 io.uio_segflg = UIO_SYSSPACE;
2708 io.uio_rw = UIO_READ;
2712 free((caddr_t)cookies, M_TEMP);
2715 vn_lock(vp, LK_SHARED | LK_RETRY);
2716 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2717 off = (off_t)io.uio_offset;
2718 if (!cookies && !error)
2719 error = NFSERR_PERM;
2721 getret = VOP_GETATTR(vp, &at, cred);
2729 free((caddr_t)rbuf, M_TEMP);
2731 free((caddr_t)cookies, M_TEMP);
2732 nfsm_reply(NFSX_POSTOPATTR(v3));
2734 nfsm_srvpostop_attr(getret, &at);
2739 siz -= io.uio_resid;
2742 * If nothing read, return eof
2748 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2751 nfsm_srvpostop_attr(getret, &at);
2752 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
2753 txdr_hyper(at.va_filerev, tl);
2756 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2757 *tl++ = nfsrv_nfs_false;
2758 *tl = nfsrv_nfs_true;
2759 free((caddr_t)rbuf, M_TEMP);
2760 free((caddr_t)cookies, M_TEMP);
2767 * Check for degenerate cases of nothing useful read.
2768 * If so go try again
2772 dp = (struct dirent *)cpos;
2775 * For some reason FreeBSD's ufs_readdir() chooses to back the
2776 * directory offset up to a block boundary, so it is necessary to
2777 * skip over the records that precede the requested offset. This
2778 * requires the assumption that file offset cookies monotonically
2781 while (cpos < cend && ncookies > 0 &&
2782 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2783 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2784 cpos += dp->d_reclen;
2785 dp = (struct dirent *)cpos;
2789 if (cpos >= cend || ncookies == 0) {
2795 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2796 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2798 nfsm_srvpostop_attr(getret, &at);
2799 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
2800 txdr_hyper(at.va_filerev, tl);
2804 be = bp + M_TRAILINGSPACE(mp);
2806 /* Loop through the records and build reply */
2807 while (cpos < cend && ncookies > 0) {
2808 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2809 nlen = dp->d_namlen;
2810 rem = nfsm_rndup(nlen) - nlen;
2811 len += (4 * NFSX_UNSIGNED + nlen + rem);
2813 len += 2 * NFSX_UNSIGNED;
2819 * Build the directory record xdr from
2823 *tl = nfsrv_nfs_true;
2824 bp += NFSX_UNSIGNED;
2828 bp += NFSX_UNSIGNED;
2831 *tl = txdr_unsigned(dp->d_fileno);
2832 bp += NFSX_UNSIGNED;
2834 *tl = txdr_unsigned(nlen);
2835 bp += NFSX_UNSIGNED;
2837 /* And loop around copying the name */
2846 bcopy(cp, bp, tsiz);
2852 /* And null pad to an int32_t boundary. */
2853 for (i = 0; i < rem; i++)
2857 /* Finish off the record */
2860 bp += NFSX_UNSIGNED;
2863 *tl = txdr_unsigned(*cookiep);
2864 bp += NFSX_UNSIGNED;
2866 cpos += dp->d_reclen;
2867 dp = (struct dirent *)cpos;
2874 *tl = nfsrv_nfs_false;
2875 bp += NFSX_UNSIGNED;
2878 *tl = nfsrv_nfs_true;
2880 *tl = nfsrv_nfs_false;
2881 bp += NFSX_UNSIGNED;
2884 mp->m_len = bp - mtod(mp, caddr_t);
2886 mp->m_len += bp - bpos;
2887 free((caddr_t)rbuf, M_TEMP);
2888 free((caddr_t)cookies, M_TEMP);
2897 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2900 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2901 struct sockaddr *nam = nfsd->nd_nam;
2902 caddr_t dpos = nfsd->nd_dpos;
2903 struct ucred *cred = nfsd->nd_cr;
2910 struct mbuf *mb, *mreq;
2911 char *cpos, *cend, *rbuf;
2912 struct vnode *vp = NULL, *nvp;
2915 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
2918 struct vattr va, at, *vap = &va;
2919 struct nfs_fattr *fp;
2920 int len, nlen, rem, xfer, tsiz, i, error = 0, error1, getret = 1;
2922 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
2923 u_quad_t off, toff, verf;
2924 u_long *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
2925 int v3 = (nfsd->nd_flag & ND_NFSV3);
2927 struct componentname cn;
2928 struct mount *mntp = NULL;
2931 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2934 panic("nfsrv_readdirplus: v3 proc called on a v2 connection");
2935 fhp = &nfh.fh_generic;
2937 tl = nfsm_dissect_nonblock(u_int32_t *, 6 * NFSX_UNSIGNED);
2938 toff = fxdr_hyper(tl);
2940 verf = fxdr_hyper(tl);
2942 siz = fxdr_unsigned(int, *tl++);
2943 cnt = fxdr_unsigned(int, *tl);
2945 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2946 xfer = NFS_SRVMAXDATA(nfsd);
2952 error = nfsrv_fhtovp(fhp, NFSRV_FLAG_BUSY, &vp, nfsd, slp,
2957 if (vp->v_type != VDIR) {
2965 nfsm_reply(NFSX_UNSIGNED);
2966 nfsm_srvpostop_attr(getret, &at);
2970 error = getret = VOP_GETATTR(vp, &at, cred);
2973 * XXX This check may be too strict for Solaris 2.5 clients.
2975 if (!error && toff && verf && verf != at.va_filerev)
2976 error = NFSERR_BAD_COOKIE;
2979 error = nfsrv_access(vp, VEXEC, cred, rdonly, 0);
2984 nfsm_reply(NFSX_V3POSTOPATTR);
2985 nfsm_srvpostop_attr(getret, &at);
2989 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2992 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2995 iv.iov_len = fullsiz;
2998 io.uio_offset = (off_t)off;
2999 io.uio_resid = fullsiz;
3000 io.uio_segflg = UIO_SYSSPACE;
3001 io.uio_rw = UIO_READ;
3006 free((caddr_t)cookies, M_TEMP);
3009 vn_lock(vp, LK_SHARED | LK_RETRY);
3010 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3011 off = (u_quad_t)io.uio_offset;
3012 getret = VOP_GETATTR(vp, &at, cred);
3015 if (!cookies && !error)
3016 error = NFSERR_PERM;
3023 free((caddr_t)cookies, M_TEMP);
3024 free((caddr_t)rbuf, M_TEMP);
3025 nfsm_reply(NFSX_V3POSTOPATTR);
3026 nfsm_srvpostop_attr(getret, &at);
3031 siz -= io.uio_resid;
3034 * If nothing read, return eof
3040 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3042 nfsm_srvpostop_attr(getret, &at);
3043 tl = nfsm_build(u_int32_t *, 4 * NFSX_UNSIGNED);
3044 txdr_hyper(at.va_filerev, tl);
3046 *tl++ = nfsrv_nfs_false;
3047 *tl = nfsrv_nfs_true;
3048 free((caddr_t)cookies, M_TEMP);
3049 free((caddr_t)rbuf, M_TEMP);
3056 * Check for degenerate cases of nothing useful read.
3057 * If so go try again
3061 dp = (struct dirent *)cpos;
3064 * For some reason FreeBSD's ufs_readdir() chooses to back the
3065 * directory offset up to a block boundary, so it is necessary to
3066 * skip over the records that precede the requested offset. This
3067 * requires the assumption that file offset cookies monotonically
3070 while (cpos < cend && ncookies > 0 &&
3071 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
3072 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
3073 cpos += dp->d_reclen;
3074 dp = (struct dirent *)cpos;
3078 if (cpos >= cend || ncookies == 0) {
3084 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3087 nfsm_srvpostop_attr(getret, &at);
3088 tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
3089 txdr_hyper(at.va_filerev, tl);
3092 be = bp + M_TRAILINGSPACE(mp);
3094 /* Loop through the records and build reply */
3095 while (cpos < cend && ncookies > 0) {
3096 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
3097 nlen = dp->d_namlen;
3098 rem = nfsm_rndup(nlen)-nlen;
3102 * For readdir_and_lookup get the vnode using
3105 error = VFS_VGET(mntp, dp->d_fileno, LK_SHARED,
3107 if (error != 0 && error != EOPNOTSUPP) {
3110 } else if (error == EOPNOTSUPP) {
3112 * VFS_VGET() not supported?
3113 * Let's switch to VOP_LOOKUP().
3117 cn.cn_nameiop = LOOKUP;
3118 cn.cn_flags = ISLASTCN | NOFOLLOW | \
3119 LOCKSHARED | LOCKLEAF;
3120 cn.cn_lkflags = LK_SHARED | LK_RETRY;
3122 cn.cn_thread = curthread;
3126 cn.cn_nameptr = dp->d_name;
3127 cn.cn_namelen = dp->d_namlen;
3128 if (dp->d_namlen == 2 &&
3129 dp->d_name[0] == '.' &&
3130 dp->d_name[1] == '.') {
3131 cn.cn_flags |= ISDOTDOT;
3133 cn.cn_flags &= ~ISDOTDOT;
3136 vn_lock(vp, LK_SHARED | LK_RETRY);
3139 if ((vp->v_vflag & VV_ROOT) != 0 &&
3140 (cn.cn_flags & ISDOTDOT) != 0) {
3143 } else if (VOP_LOOKUP(vp, &nvp, &cn) != 0)
3147 bzero((caddr_t)nfhp, NFSX_V3FH);
3148 nfhp->fh_fsid = nvp->v_mount->mnt_stat.f_fsid;
3149 if ((error1 = VOP_VPTOFH(nvp, &nfhp->fh_fid)) == 0)
3150 error1 = VOP_GETATTR(nvp, vap, cred);
3151 if (!usevget && vp == nvp)
3160 * If either the dircount or maxcount will be
3161 * exceeded, get out now. Both of these lengths
3162 * are calculated conservatively, including all
3165 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3167 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3168 if (len > cnt || dirlen > fullsiz) {
3174 * Build the directory record xdr from
3177 fp = (struct nfs_fattr *)&fl.fl_fattr;
3178 nfsm_srvfillattr(vap, fp);
3179 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3180 fl.fl_fhok = nfsrv_nfs_true;
3181 fl.fl_postopok = nfsrv_nfs_true;
3182 fl.fl_off.nfsuquad[0] = 0;
3183 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3186 *tl = nfsrv_nfs_true;
3187 bp += NFSX_UNSIGNED;
3190 bp += NFSX_UNSIGNED;
3192 *tl = txdr_unsigned(dp->d_fileno);
3193 bp += NFSX_UNSIGNED;
3195 *tl = txdr_unsigned(nlen);
3196 bp += NFSX_UNSIGNED;
3198 /* And loop around copying the name */
3203 if ((bp + xfer) > be)
3207 bcopy(cp, bp, tsiz);
3213 /* And null pad to an int32_t boundary. */
3214 for (i = 0; i < rem; i++)
3218 * Now copy the flrep structure out.
3220 xfer = sizeof (struct flrep);
3224 if ((bp + xfer) > be)
3228 bcopy(cp, bp, tsiz);
3236 cpos += dp->d_reclen;
3237 dp = (struct dirent *)cpos;
3241 if (!usevget && vp_locked)
3247 *tl = nfsrv_nfs_false;
3248 bp += NFSX_UNSIGNED;
3251 *tl = nfsrv_nfs_true;
3253 *tl = nfsrv_nfs_false;
3254 bp += NFSX_UNSIGNED;
3257 mp->m_len = bp - mtod(mp, caddr_t);
3259 mp->m_len += bp - bpos;
3260 free((caddr_t)cookies, M_TEMP);
3261 free((caddr_t)rbuf, M_TEMP);
3271 * nfs commit service
3274 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3277 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3278 struct sockaddr *nam = nfsd->nd_nam;
3279 caddr_t dpos = nfsd->nd_dpos;
3280 struct ucred *cred = nfsd->nd_cr;
3281 struct vattr bfor, aft;
3282 struct vnode *vp = NULL;
3287 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3288 struct mbuf *mb, *mreq;
3290 struct mount *mp = NULL;
3291 int v3 = (nfsd->nd_flag & ND_NFSV3);
3293 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3295 panic("nfsrv_commit: v3 proc called on a v2 connection");
3296 fhp = &nfh.fh_generic;
3298 if ((mp = vfs_getvfs(&fhp->fh_fsid)) == NULL) {
3302 (void) vn_start_write(NULL, &mp, V_WAIT);
3303 vfs_rel(mp); /* The write holds a ref. */
3304 tl = nfsm_dissect_nonblock(u_int32_t *, 3 * NFSX_UNSIGNED);
3307 * XXX At this time VOP_FSYNC() does not accept offset and byte
3308 * count parameters, so these arguments are useless (someday maybe).
3310 off = fxdr_hyper(tl);
3312 cnt = fxdr_unsigned(int, *tl);
3313 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
3315 nfsm_reply(2 * NFSX_UNSIGNED);
3316 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3320 for_ret = VOP_GETATTR(vp, &bfor, cred);
3323 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of file
3324 * is done. At this time VOP_FSYNC does not accept offset and byte count
3325 * parameters so call VOP_FSYNC the whole file for now.
3327 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
3329 * Give up and do the whole thing
3332 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3333 VM_OBJECT_WLOCK(vp->v_object);
3334 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3335 VM_OBJECT_WUNLOCK(vp->v_object);
3337 error = VOP_FSYNC(vp, MNT_WAIT, curthread);
3340 * Locate and synchronously write any buffers that fall
3341 * into the requested range. Note: we are assuming that
3342 * f_iosize is a power of 2.
3344 int iosize = vp->v_mount->mnt_stat.f_iosize;
3345 int iomask = iosize - 1;
3350 * Align to iosize boundry, super-align to page boundry.
3353 cnt += off & iomask;
3354 off &= ~(u_quad_t)iomask;
3356 if (off & PAGE_MASK) {
3357 cnt += off & PAGE_MASK;
3358 off &= ~(u_quad_t)PAGE_MASK;
3360 lblkno = off / iosize;
3363 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3364 VM_OBJECT_WLOCK(vp->v_object);
3365 vm_object_page_clean(vp->v_object, off, off + cnt,
3367 VM_OBJECT_WUNLOCK(vp->v_object);
3376 * If we have a buffer and it is marked B_DELWRI we
3377 * have to lock and write it. Otherwise the prior
3378 * write is assumed to have already been committed.
3380 * gbincore() can return invalid buffers now so we
3381 * have to check that bit as well (though B_DELWRI
3382 * should not be set if B_INVAL is set there could be
3383 * a race here since we haven't locked the buffer).
3385 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
3386 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
3387 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
3389 continue; /* retry */
3391 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
3394 bp->b_flags &= ~B_ASYNC;
3410 aft_ret = VOP_GETATTR(vp, &aft, cred);
3414 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3415 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3417 tl = nfsm_build(u_int32_t *, NFSX_V3WRITEVERF);
3418 if (nfsver.tv_sec == 0)
3420 *tl++ = txdr_unsigned(nfsver.tv_sec);
3421 *tl = txdr_unsigned(nfsver.tv_usec);
3428 vn_finished_write(mp);
3433 * nfs statfs service
3436 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3439 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3440 struct sockaddr *nam = nfsd->nd_nam;
3441 caddr_t dpos = nfsd->nd_dpos;
3442 struct ucred *cred = nfsd->nd_cr;
3444 struct nfs_statfs *sfp;
3446 int error = 0, rdonly, getret = 1;
3447 int v3 = (nfsd->nd_flag & ND_NFSV3);
3448 struct mbuf *mb, *mreq;
3449 struct vnode *vp = NULL;
3453 struct statfs statfs;
3456 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3457 fhp = &nfh.fh_generic;
3459 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
3461 nfsm_reply(NFSX_UNSIGNED);
3463 nfsm_srvpostop_attr(getret, &at);
3468 error = VFS_STATFS(vp->v_mount, sf);
3469 getret = VOP_GETATTR(vp, &at, cred);
3472 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3474 nfsm_srvpostop_attr(getret, &at);
3479 sfp = nfsm_build(struct nfs_statfs *, NFSX_STATFS(v3));
3481 tval = (u_quad_t)sf->f_blocks;
3482 tval *= (u_quad_t)sf->f_bsize;
3483 txdr_hyper(tval, &sfp->sf_tbytes);
3484 tval = (u_quad_t)sf->f_bfree;
3485 tval *= (u_quad_t)sf->f_bsize;
3486 txdr_hyper(tval, &sfp->sf_fbytes);
3488 * Don't send negative values for available space,
3489 * since this field is unsigned in the NFS protocol.
3490 * Otherwise, the client would see absurdly high
3491 * numbers for free space.
3493 if (sf->f_bavail < 0)
3496 tval = (u_quad_t)sf->f_bavail;
3497 tval *= (u_quad_t)sf->f_bsize;
3498 txdr_hyper(tval, &sfp->sf_abytes);
3499 sfp->sf_tfiles.nfsuquad[0] = 0;
3500 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3501 sfp->sf_ffiles.nfsuquad[0] = 0;
3502 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3503 sfp->sf_afiles.nfsuquad[0] = 0;
3504 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3505 sfp->sf_invarsec = 0;
3507 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3508 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3509 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3510 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3511 if (sf->f_bavail < 0)
3514 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3523 * nfs fsinfo service
3526 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3529 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3530 struct sockaddr *nam = nfsd->nd_nam;
3531 caddr_t dpos = nfsd->nd_dpos;
3532 struct ucred *cred = nfsd->nd_cr;
3533 struct nfsv3_fsinfo *sip;
3535 int error = 0, rdonly, getret = 1, pref;
3536 struct mbuf *mb, *mreq;
3537 struct vnode *vp = NULL;
3543 int v3 = (nfsd->nd_flag & ND_NFSV3);
3545 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3547 panic("nfsrv_fsinfo: v3 proc called on a v2 connection");
3548 fhp = &nfh.fh_generic;
3550 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
3552 nfsm_reply(NFSX_UNSIGNED);
3553 nfsm_srvpostop_attr(getret, &at);
3558 /* XXX Try to make a guess on the max file size. */
3559 VFS_STATFS(vp->v_mount, &sb);
3560 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3562 getret = VOP_GETATTR(vp, &at, cred);
3565 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3566 nfsm_srvpostop_attr(getret, &at);
3567 sip = nfsm_build(struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3571 * There should be filesystem VFS OP(s) to get this information.
3572 * For now, assume ufs.
3574 pref = NFS_SRVMAXDATA(nfsd);
3575 sip->fs_rtmax = txdr_unsigned(pref);
3576 sip->fs_rtpref = txdr_unsigned(pref);
3577 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3578 sip->fs_wtmax = txdr_unsigned(pref);
3579 sip->fs_wtpref = txdr_unsigned(pref);
3580 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3581 sip->fs_dtpref = txdr_unsigned(pref);
3582 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3583 sip->fs_timedelta.nfsv3_sec = 0;
3584 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3585 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3586 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3587 NFSV3FSINFO_CANSETTIME);
3595 * nfs pathconf service
3598 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3601 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3602 struct sockaddr *nam = nfsd->nd_nam;
3603 caddr_t dpos = nfsd->nd_dpos;
3604 struct ucred *cred = nfsd->nd_cr;
3605 struct nfsv3_pathconf *pc;
3607 int error = 0, rdonly, getret = 1;
3608 register_t linkmax, namemax, chownres, notrunc;
3609 struct mbuf *mb, *mreq;
3610 struct vnode *vp = NULL;
3614 int v3 = (nfsd->nd_flag & ND_NFSV3);
3616 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3618 panic("nfsrv_pathconf: v3 proc called on a v2 connection");
3619 fhp = &nfh.fh_generic;
3621 error = nfsrv_fhtovp(fhp, 0, &vp, nfsd, slp, nam, &rdonly);
3623 nfsm_reply(NFSX_UNSIGNED);
3624 nfsm_srvpostop_attr(getret, &at);
3628 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3630 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3632 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3634 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3635 getret = VOP_GETATTR(vp, &at, cred);
3638 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3639 nfsm_srvpostop_attr(getret, &at);
3644 pc = nfsm_build(struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3646 pc->pc_linkmax = txdr_unsigned(linkmax);
3647 pc->pc_namemax = txdr_unsigned(namemax);
3648 pc->pc_notrunc = txdr_unsigned(notrunc);
3649 pc->pc_chownrestricted = txdr_unsigned(chownres);
3652 * These should probably be supported by VOP_PATHCONF(), but
3653 * until msdosfs is exportable (why would you want to?), the
3654 * Unix defaults should be ok.
3656 pc->pc_caseinsensitive = nfsrv_nfs_false;
3657 pc->pc_casepreserving = nfsrv_nfs_true;
3665 * Null operation, used by clients to ping server
3669 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3672 struct mbuf *mrep = nfsd->nd_mrep;
3674 int error = NFSERR_RETVOID;
3675 struct mbuf *mb, *mreq;
3677 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3684 * No operation, used for obsolete procedures
3688 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3691 struct mbuf *mrep = nfsd->nd_mrep;
3694 struct mbuf *mb, *mreq;
3696 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3697 if (nfsd->nd_repstat)
3698 error = nfsd->nd_repstat;
3700 error = EPROCUNAVAIL;
3708 * Perform access checking for vnodes obtained from file handles that would
3709 * refer to files already opened by a Unix client. You cannot just use
3710 * vn_writechk() and VOP_ACCESS() for two reasons.
3711 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
3713 * 2 - The owner is to be given access irrespective of mode bits for some
3714 * operations, so that processes that chmod after opening a file don't
3715 * break. I don't like this because it opens a security hole, but since
3716 * the nfs server opens a security hole the size of a barn door anyhow,
3719 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3720 * will return EPERM instead of EACCES. EPERM is always an error.
3723 nfsrv_access(struct vnode *vp, accmode_t accmode, struct ucred *cred,
3724 int rdonly, int override)
3729 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3731 if (accmode & VWRITE) {
3732 /* Just vn_writechk() changed to check rdonly */
3734 * Disallow write attempts on read-only filesystems;
3735 * unless the file is a socket or a block or character
3736 * device resident on the filesystem.
3738 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3739 switch (vp->v_type) {
3749 * If there's shared text associated with
3750 * the inode, we can't allow writing.
3752 if (VOP_IS_TEXT(vp))
3756 error = VOP_GETATTR(vp, &vattr, cred);
3759 error = VOP_ACCESS(vp, accmode, cred, curthread);
3761 * Allow certain operations for the owner (reads and writes
3762 * on files that are already open).
3764 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)