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. 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
33 * $FreeBSD: src/sys/nfs/nfs_serv.c,v 1.93.2.6 2002/12/29 18:19:53 dillon Exp $
37 * nfs version 2 and 3 server calls to vnode ops
38 * - these routines generally have 3 phases
39 * 1 - break down and validate rpc request in mbuf list
40 * 2 - do the vnode ops for the request
41 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
42 * 3 - build the rpc reply in an mbuf list
44 * - do not mix the phases, since the nfsm_?? macros can return failures
45 * on a bad rpc or similar and do not do any vrele() or vput()'s
47 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
48 * error number iff error != 0 whereas
49 * returning an error from the server function implies a fatal error
50 * such as a badly constructed rpc request that should be dropped without
52 * For Version 3, nfsm_reply() does not return for the error case, since
53 * most version 3 rpcs return more than the status for error cases.
56 * Warning: always pay careful attention to resource cleanup on return
57 * and note that nfsm_*() macros can terminate a procedure on certain
61 #include <sys/param.h>
62 #include <sys/systm.h>
65 #include <sys/nlookup.h>
66 #include <sys/namei.h>
67 #include <sys/unistd.h>
68 #include <sys/vnode.h>
69 #include <sys/mount.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
72 #include <sys/malloc.h>
74 #include <sys/dirent.h>
76 #include <sys/kernel.h>
77 #include <sys/sysctl.h>
81 #include <vm/vm_extern.h>
82 #include <vm/vm_object.h>
86 #include <sys/thread2.h>
92 #include "nfsm_subs.h"
95 #define nfsdbprintf(info) kprintf info
97 #define nfsdbprintf(info)
100 #define MAX_REORDERED_RPC (16)
101 #define MAX_COMMIT_COUNT (1024 * 1024)
103 #define NUM_HEURISTIC 1031
104 #define NHUSE_INIT 64
106 #define NHUSE_MAX 2048
108 static struct nfsheur {
109 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
110 off_t nh_nextoff; /* next offset for sequential detection */
111 int nh_use; /* use count for selection */
112 int nh_seqcount; /* heuristic */
113 } nfsheur[NUM_HEURISTIC];
115 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
118 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
121 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
122 int nfsrvw_procrastinate_v3 = 0;
124 static struct timespec nfsver;
126 SYSCTL_DECL(_vfs_nfs);
129 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0,
130 "Enable unstable and fast writes");
131 static int nfs_commit_blks;
132 static int nfs_commit_miss;
133 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0,
134 "Number of committed blocks");
135 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0,
136 "Number of nfs blocks committed from dirty buffers");
138 static int nfsrv_access (struct mount *, struct vnode *, int,
139 struct ucred *, int, struct thread *, int);
140 static void nfsrvw_coalesce (struct nfsrv_descript *,
141 struct nfsrv_descript *);
144 * Heuristic to detect sequential operation.
146 static struct nfsheur *
147 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp, int writeop)
152 /* Locate best candidate */
154 hi = ((int)(vm_offset_t) vp / sizeof(struct vnode)) % NUM_HEURISTIC;
158 if (nfsheur[hi].nh_vp == vp) {
162 if (nfsheur[hi].nh_use > 0)
163 --nfsheur[hi].nh_use;
164 hi = (hi + 1) % NUM_HEURISTIC;
165 if (nfsheur[hi].nh_use < nh->nh_use)
169 /* Initialize hint if this is a new file */
170 if (nh->nh_vp != vp) {
172 nh->nh_nextoff = uio->uio_offset;
173 nh->nh_use = NHUSE_INIT;
174 if (uio->uio_offset == 0)
181 * Calculate heuristic
183 * See vfs_vnops.c:sequential_heuristic().
185 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
186 uio->uio_offset == nh->nh_nextoff) {
187 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
188 if (nh->nh_seqcount > IO_SEQMAX)
189 nh->nh_seqcount = IO_SEQMAX;
190 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
191 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
192 /* Probably a reordered RPC, leave seqcount alone. */
193 } else if (nh->nh_seqcount > 1) {
194 nh->nh_seqcount /= 2;
198 nh->nh_use += NHUSE_INC;
199 if (nh->nh_use > NHUSE_MAX)
200 nh->nh_use = NHUSE_MAX;
205 * nfs v3 access service
208 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
209 struct thread *td, struct mbuf **mrq)
211 struct sockaddr *nam = nfsd->nd_nam;
212 struct ucred *cred = &nfsd->nd_cr;
213 struct vnode *vp = NULL;
214 struct mount *mp = NULL;
217 int error = 0, rdonly, getret;
218 struct vattr vattr, *vap = &vattr;
219 u_long testmode, nfsmode;
220 struct nfsm_info info;
223 info.dpos = nfsd->nd_dpos;
224 info.md = nfsd->nd_md;
225 info.mrep = nfsd->nd_mrep;
228 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
229 fhp = &nfh.fh_generic;
230 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
231 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
232 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
233 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
235 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
236 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
240 nfsmode = fxdr_unsigned(u_int32_t, *tl);
241 if ((nfsmode & NFSV3ACCESS_READ) &&
242 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
243 nfsmode &= ~NFSV3ACCESS_READ;
244 if (vp->v_type == VDIR)
245 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
248 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
249 if ((nfsmode & testmode) &&
250 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
251 nfsmode &= ~testmode;
252 if (vp->v_type == VDIR)
253 testmode = NFSV3ACCESS_LOOKUP;
255 testmode = NFSV3ACCESS_EXECUTE;
256 if ((nfsmode & testmode) &&
257 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
258 nfsmode &= ~testmode;
259 getret = VOP_GETATTR(vp, vap);
262 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
263 NFSX_POSTOPATTR(1) + NFSX_UNSIGNED, &error));
264 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
265 tl = nfsm_build(&info, NFSX_UNSIGNED);
266 *tl = txdr_unsigned(nfsmode);
275 * nfs getattr service
278 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
279 struct thread *td, struct mbuf **mrq)
281 struct sockaddr *nam = nfsd->nd_nam;
282 struct ucred *cred = &nfsd->nd_cr;
283 struct nfs_fattr *fp;
285 struct vattr *vap = &va;
286 struct vnode *vp = NULL;
287 struct mount *mp = NULL;
290 int error = 0, rdonly;
291 struct nfsm_info info;
293 info.mrep = nfsd->nd_mrep;
294 info.md = nfsd->nd_md;
295 info.dpos = nfsd->nd_dpos;
298 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
299 fhp = &nfh.fh_generic;
300 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
301 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
302 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
304 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
308 error = VOP_GETATTR(vp, vap);
311 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
312 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3), &error));
317 fp = nfsm_build(&info, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
318 nfsm_srvfattr(nfsd, vap, fp);
329 * nfs setattr service
332 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
333 struct thread *td, struct mbuf **mrq)
335 struct sockaddr *nam = nfsd->nd_nam;
336 struct ucred *cred = &nfsd->nd_cr;
337 struct vattr va, preat;
338 struct vattr *vap = &va;
339 struct nfsv2_sattr *sp;
340 struct nfs_fattr *fp;
341 struct vnode *vp = NULL;
342 struct mount *mp = NULL;
346 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
348 struct timespec guard;
349 struct nfsm_info info;
351 info.mrep = nfsd->nd_mrep;
353 info.md = nfsd->nd_md;
354 info.dpos = nfsd->nd_dpos;
355 info.v3 = (nfsd->nd_flag & ND_NFSV3);
357 guard.tv_sec = 0; /* fix compiler warning */
360 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
361 fhp = &nfh.fh_generic;
362 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
365 ERROROUT(nfsm_srvsattr(&info, vap));
366 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
367 gcheck = fxdr_unsigned(int, *tl);
369 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
370 fxdr_nfsv3time(tl, &guard);
373 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
375 * Nah nah nah nah na nah
376 * There is a bug in the Sun client that puts 0xffff in the mode
377 * field of sattr when it should put in 0xffffffff. The u_short
378 * doesn't sign extend.
379 * --> check the low order 2 bytes for 0xffff
381 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
382 vap->va_mode = nfstov_mode(sp->sa_mode);
383 if (sp->sa_uid != nfs_xdrneg1)
384 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
385 if (sp->sa_gid != nfs_xdrneg1)
386 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
387 if (sp->sa_size != nfs_xdrneg1)
388 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
389 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
391 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
393 vap->va_atime.tv_sec =
394 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
395 vap->va_atime.tv_nsec = 0;
398 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
399 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
404 * Now that we have all the fields, lets do it.
406 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
407 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
409 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
410 2 * NFSX_UNSIGNED, &error));
411 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
418 * vp now an active resource, pay careful attention to cleanup
422 error = preat_ret = VOP_GETATTR(vp, &preat);
423 if (!error && gcheck &&
424 (preat.va_ctime.tv_sec != guard.tv_sec ||
425 preat.va_ctime.tv_nsec != guard.tv_nsec))
426 error = NFSERR_NOT_SYNC;
430 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
431 NFSX_WCCDATA(info.v3), &error));
432 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
440 * If the size is being changed write acces is required, otherwise
441 * just check for a read only file system.
443 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
444 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
449 if (vp->v_type == VDIR) {
452 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
457 error = VOP_SETATTR(vp, vap, cred);
458 postat_ret = VOP_GETATTR(vp, vap);
464 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
465 NFSX_WCCORFATTR(info.v3), &error));
467 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
472 fp = nfsm_build(&info, NFSX_V2FATTR);
473 nfsm_srvfattr(nfsd, vap, fp);
488 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
489 struct thread *td, struct mbuf **mrq)
491 struct sockaddr *nam = nfsd->nd_nam;
492 struct ucred *cred = &nfsd->nd_cr;
493 struct nfs_fattr *fp;
494 struct nlookupdata nd;
500 int error = 0, len, dirattr_ret = 1;
502 struct vattr va, dirattr, *vap = &va;
503 struct nfsm_info info;
505 info.mrep = nfsd->nd_mrep;
507 info.md = nfsd->nd_md;
508 info.dpos = nfsd->nd_dpos;
509 info.v3 = (nfsd->nd_flag & ND_NFSV3);
511 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
516 fhp = &nfh.fh_generic;
517 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
518 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
520 pubflag = nfs_ispublicfh(fhp);
522 error = nfs_namei(&nd, cred, 0, NULL, &vp,
523 fhp, len, slp, nam, &info.md, &info.dpos,
524 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
527 * namei failure, only dirp to cleanup. Clear out garbarge from
528 * structure in case macros jump to nfsmout.
534 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
538 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
539 NFSX_POSTOPATTR(info.v3), &error));
540 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
546 * Locate index file for public filehandle
548 * error is 0 on entry and 0 on exit from this block.
552 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
554 * Setup call to lookup() to see if we can find
555 * the index file. Arguably, this doesn't belong
556 * in a kernel.. Ugh. If an error occurs, do not
557 * try to install an index file and then clear the
560 * When we replace nd with ind and redirect ndp,
561 * maintenance of ni_startdir and ni_vp shift to
562 * ind and we have to clean them up in the old nd.
563 * However, the cnd resource continues to be maintained
564 * via the original nd. Confused? You aren't alone!
567 cache_copy(&nd.nl_nch, &nch);
569 error = nlookup_init_raw(&nd, nfs_pub.np_index,
570 UIO_SYSSPACE, 0, cred, &nch);
573 error = nlookup(&nd);
577 * Found an index file. Get rid of
578 * the old references. transfer vp and
579 * load up the new vp. Fortunately we do
580 * not have to deal with dvp, that would be
587 error = cache_vget(&nd.nl_nch, nd.nl_cred,
589 KKASSERT(error == 0);
594 * If the public filehandle was used, check that this lookup
595 * didn't result in a filehandle outside the publicly exported
596 * filesystem. We clear the poor vp here to avoid lockups due
600 if (vp->v_mount != nfs_pub.np_mount) {
609 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
615 * Resources at this point:
616 * ndp->ni_vp may not be NULL
621 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
622 NFSX_POSTOPATTR(info.v3), &error));
623 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
629 * Clear out some resources prior to potentially blocking. This
630 * is not as critical as ni_dvp resources in other routines, but
636 * Get underlying attribute, then release remaining resources ( for
637 * the same potential blocking reason ) and reply.
639 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
640 error = VFS_VPTOFH(vp, &fhp->fh_fid);
642 error = VOP_GETATTR(vp, vap);
646 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
647 NFSX_SRVFH(info.v3) +
648 NFSX_POSTOPORFATTR(info.v3) +
649 NFSX_POSTOPATTR(info.v3),
652 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
656 nfsm_srvfhtom(&info, fhp);
658 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
659 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
661 fp = nfsm_build(&info, NFSX_V2FATTR);
662 nfsm_srvfattr(nfsd, vap, fp);
669 nlookup_done(&nd); /* may be called twice */
676 * nfs readlink service
679 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
680 struct thread *td, struct mbuf **mrq)
682 struct sockaddr *nam = nfsd->nd_nam;
683 struct ucred *cred = &nfsd->nd_cr;
684 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
685 struct iovec *ivp = iv;
687 int error = 0, rdonly, i, tlen, len, getret;
688 struct mbuf *mp1, *mp2, *mp3;
689 struct vnode *vp = NULL;
690 struct mount *mp = NULL;
694 struct uio io, *uiop = &io;
695 struct nfsm_info info;
697 info.mrep = nfsd->nd_mrep;
699 info.md = nfsd->nd_md;
700 info.dpos = nfsd->nd_dpos;
701 info.v3 = (nfsd->nd_flag & ND_NFSV3);
703 bzero(&io, sizeof(struct uio));
705 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
710 fhp = &nfh.fh_generic;
711 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
714 while (len < NFS_MAXPATHLEN) {
715 mp1 = m_getcl(M_WAITOK, MT_DATA, 0);
716 mp1->m_len = MCLBYTES;
723 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
724 mp1->m_len = NFS_MAXPATHLEN-len;
725 len = NFS_MAXPATHLEN;
728 ivp->iov_base = mtod(mp1, caddr_t);
729 ivp->iov_len = mp1->m_len;
734 uiop->uio_iovcnt = i;
735 uiop->uio_offset = 0;
736 uiop->uio_resid = len;
737 uiop->uio_rw = UIO_READ;
738 uiop->uio_segflg = UIO_SYSSPACE;
740 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
741 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
743 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
744 2 * NFSX_UNSIGNED, &error));
745 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
749 if (vp->v_type != VLNK) {
756 error = VOP_READLINK(vp, uiop, cred);
758 getret = VOP_GETATTR(vp, &attr);
761 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
762 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
765 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
771 if (uiop->uio_resid > 0) {
772 len -= uiop->uio_resid;
773 tlen = nfsm_rndup(len);
774 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
776 tl = nfsm_build(&info, NFSX_UNSIGNED);
777 *tl = txdr_unsigned(len);
778 info.mb->m_next = mp3;
793 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
794 struct thread *td, struct mbuf **mrq)
796 struct nfsm_info info;
797 struct sockaddr *nam = nfsd->nd_nam;
798 struct ucred *cred = &nfsd->nd_cr;
802 struct nfs_fattr *fp;
806 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
808 struct vnode *vp = NULL;
809 struct mount *mp = NULL;
812 struct uio io, *uiop = &io;
813 struct vattr va, *vap = &va;
818 info.mrep = nfsd->nd_mrep;
820 info.md = nfsd->nd_md;
821 info.dpos = nfsd->nd_dpos;
822 info.v3 = (nfsd->nd_flag & ND_NFSV3);
824 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
825 fhp = &nfh.fh_generic;
826 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
828 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
829 off = fxdr_hyper(tl);
831 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
832 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
834 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
835 NFS_SRVMAXDATA(nfsd), &error));
838 * Reference vp. If an error occurs, vp will be invalid, but we
839 * have to NULL it just in case. The macros might goto nfsmout
843 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
844 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
847 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
848 2 * NFSX_UNSIGNED, &error));
849 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
854 if (vp->v_type != VREG) {
858 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
861 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
862 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
864 getret = VOP_GETATTR(vp, vap);
870 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
871 NFSX_POSTOPATTR(info.v3), &error));
872 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
878 * Calculate byte count to read
881 if (off >= vap->va_size)
883 else if ((off + reqlen) > vap->va_size)
884 cnt = vap->va_size - off;
888 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
889 NFSX_POSTOPORFATTR(info.v3) +
890 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
893 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
895 fp = (struct nfs_fattr *)tl;
896 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
898 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
899 fp = (struct nfs_fattr *)tl;
900 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
902 len = left = nfsm_rndup(cnt);
905 * Generate the mbuf list with the uio_iov ref. to it.
910 siz = min(M_TRAILINGSPACE(m), left);
916 m = m_getcl(M_WAITOK, MT_DATA, 0);
922 iv = kmalloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
923 uiop->uio_iov = iv2 = iv;
929 panic("nfsrv_read iov");
930 siz = min(M_TRAILINGSPACE(m), left);
932 iv->iov_base = mtod(m, caddr_t) + m->m_len;
941 uiop->uio_iovcnt = i;
942 uiop->uio_offset = off;
943 uiop->uio_resid = len;
944 uiop->uio_rw = UIO_READ;
945 uiop->uio_segflg = UIO_SYSSPACE;
946 nh = nfsrv_sequential_heuristic(uiop, vp, 0);
947 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
948 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
950 off = uiop->uio_offset;
951 nh->nh_nextoff = off;
953 kfree((caddr_t)iv2, M_TEMP);
954 if (error || (getret = VOP_GETATTR(vp, vap))) {
961 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
962 NFSX_POSTOPATTR(info.v3),
964 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
973 nfsm_srvfattr(nfsd, vap, fp);
974 tlen = len - uiop->uio_resid;
975 cnt = cnt < tlen ? cnt : tlen;
976 tlen = nfsm_rndup(cnt);
977 if (len != tlen || tlen != cnt)
978 nfsm_adj(info.mb, len - tlen, tlen - cnt);
980 *tl++ = txdr_unsigned(cnt);
986 *tl = txdr_unsigned(cnt);
998 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
999 struct thread *td, struct mbuf **mrq)
1001 struct sockaddr *nam = nfsd->nd_nam;
1002 struct ucred *cred = &nfsd->nd_cr;
1006 struct nfs_fattr *fp;
1008 struct vattr va, forat;
1009 struct vattr *vap = &va;
1011 int error = 0, rdonly, len, forat_ret = 1;
1012 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1013 int stable = NFSV3WRITE_FILESYNC;
1014 struct vnode *vp = NULL;
1015 struct mount *mp = NULL;
1019 struct uio io, *uiop = &io;
1020 struct nfsm_info info;
1023 info.mrep = nfsd->nd_mrep;
1025 info.md = nfsd->nd_md;
1026 info.dpos = nfsd->nd_dpos;
1027 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1029 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1030 if (info.mrep == NULL) {
1034 fhp = &nfh.fh_generic;
1035 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1037 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1038 off = fxdr_hyper(tl);
1040 stable = fxdr_unsigned(int, *tl++);
1042 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1043 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1046 stable = NFSV3WRITE_UNSTABLE;
1048 retlen = len = fxdr_unsigned(int32_t, *tl);
1052 * For NFS Version 2, it is not obvious what a write of zero length
1053 * should do, but I might as well be consistent with Version 3,
1054 * which is to return ok so long as there are no permission problems.
1060 if (mp1 == info.md) {
1062 adjust = info.dpos - mtod(mp1, caddr_t);
1063 mp1->m_len -= adjust;
1064 if (mp1->m_len > 0 && adjust > 0)
1065 mp1->m_data += adjust;
1069 else if (mp1->m_len > 0) {
1072 mp1->m_len -= (i - len);
1081 if (len > NFS_MAXDATA || len < 0 || i < len) {
1083 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1084 2 * NFSX_UNSIGNED, &error));
1085 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1090 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1091 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1094 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1095 2 * NFSX_UNSIGNED, &error));
1096 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1102 forat_ret = VOP_GETATTR(vp, &forat);
1103 if (vp->v_type != VREG) {
1107 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1110 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1115 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1116 NFSX_WCCDATA(info.v3), &error));
1117 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1124 ivp = kmalloc(cnt * sizeof(struct iovec), M_TEMP, M_WAITOK);
1125 uiop->uio_iov = iv = ivp;
1126 uiop->uio_iovcnt = cnt;
1129 if (mp1->m_len > 0) {
1130 ivp->iov_base = mtod(mp1, caddr_t);
1131 ivp->iov_len = mp1->m_len;
1139 * The IO_METASYNC flag indicates that all metadata (and not just
1140 * enough to ensure data integrity) mus be written to stable storage
1142 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1144 if (stable == NFSV3WRITE_UNSTABLE)
1145 ioflags = IO_NODELOCKED;
1146 else if (stable == NFSV3WRITE_DATASYNC)
1147 ioflags = (IO_SYNC | IO_NODELOCKED);
1149 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1150 uiop->uio_resid = len;
1151 uiop->uio_rw = UIO_WRITE;
1152 uiop->uio_segflg = UIO_SYSSPACE;
1153 uiop->uio_td = NULL;
1154 uiop->uio_offset = off;
1155 nh = nfsrv_sequential_heuristic(uiop, vp, 1);
1156 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1157 error = VOP_WRITE(vp, uiop, ioflags, cred);
1159 nh->nh_nextoff = uiop->uio_offset;
1160 nfsstats.srvvop_writes++;
1161 kfree((caddr_t)iv, M_TEMP);
1163 aftat_ret = VOP_GETATTR(vp, vap);
1168 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1169 NFSX_PREOPATTR(info.v3) +
1170 NFSX_POSTOPORFATTR(info.v3) +
1171 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1174 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1180 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1181 *tl++ = txdr_unsigned(retlen);
1183 * If nfs_async is set, then pretend the write was FILESYNC.
1185 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1186 *tl++ = txdr_unsigned(stable);
1188 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1190 * Actually, there is no need to txdr these fields,
1191 * but it may make the values more human readable,
1192 * for debugging purposes.
1194 if (nfsver.tv_sec == 0)
1196 *tl++ = txdr_unsigned(nfsver.tv_sec);
1197 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1199 fp = nfsm_build(&info, NFSX_V2FATTR);
1200 nfsm_srvfattr(nfsd, vap, fp);
1210 * NFS write service with write gathering support. Called when
1211 * nfsrvw_procrastinate > 0.
1212 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1213 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1217 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1218 struct thread *td, struct mbuf **mrq)
1221 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1222 struct nfs_fattr *fp;
1225 struct nfsrvw_delayhash *wpp;
1227 struct vattr va, forat;
1229 int error = 0, rdonly, len, forat_ret = 1;
1230 int ioflags, aftat_ret = 1, adjust, zeroing;
1232 struct vnode *vp = NULL;
1233 struct mount *mp = NULL;
1234 struct uio io, *uiop = &io;
1236 struct nfsm_info info;
1240 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1248 info.mrep = nfsd->nd_mrep;
1250 info.md = nfsd->nd_md;
1251 info.dpos = nfsd->nd_dpos;
1252 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1253 cred = &nfsd->nd_cr;
1254 LIST_INIT(&nfsd->nd_coalesce);
1255 nfsd->nd_mreq = NULL;
1256 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1257 cur_usec = nfs_curusec();
1258 nfsd->nd_time = cur_usec +
1259 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1262 * Now, get the write header..
1264 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1266 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1267 nfsd->nd_off = fxdr_hyper(tl);
1269 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1271 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1272 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1275 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1277 len = fxdr_unsigned(int32_t, *tl);
1279 nfsd->nd_eoff = nfsd->nd_off + len;
1282 * Trim the header out of the mbuf list and trim off any trailing
1283 * junk so that the mbuf list has only the write data.
1289 if (mp1 == info.md) {
1291 adjust = info.dpos - mtod(mp1, caddr_t);
1292 mp1->m_len -= adjust;
1293 if (mp1->m_len > 0 && adjust > 0)
1294 mp1->m_data += adjust;
1301 mp1->m_len -= (i - len);
1307 if (len > NFS_MAXDATA || len < 0 || i < len) {
1312 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1314 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1317 nfsd->nd_mreq = info.mreq;
1318 nfsd->nd_mrep = NULL;
1323 * Add this entry to the hash and time queues.
1326 wp = slp->ns_tq.lh_first;
1327 while (wp && wp->nd_time < nfsd->nd_time) {
1329 wp = wp->nd_tq.le_next;
1331 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1333 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1335 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1337 if (nfsd->nd_mrep) {
1338 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1342 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1344 wp = wp->nd_hash.le_next;
1346 while (wp && wp->nd_off < nfsd->nd_off &&
1347 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1349 wp = wp->nd_hash.le_next;
1352 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1355 * Search the hash list for overlapping entries and
1358 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1359 wp = nfsd->nd_hash.le_next;
1360 if (NFSW_SAMECRED(owp, nfsd))
1361 nfsrvw_coalesce(owp, nfsd);
1364 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1370 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1371 * and generate the associated reply mbuf list(s).
1374 cur_usec = nfs_curusec();
1375 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1376 owp = nfsd->nd_tq.le_next;
1377 if (nfsd->nd_time > cur_usec)
1381 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1382 LIST_REMOVE(nfsd, nd_tq);
1383 LIST_REMOVE(nfsd, nd_hash);
1384 info.mrep = nfsd->nd_mrep;
1386 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1387 nfsd->nd_mrep = NULL;
1388 cred = &nfsd->nd_cr;
1389 forat_ret = aftat_ret = 1;
1390 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1391 nfsd->nd_nam, &rdonly,
1392 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1395 forat_ret = VOP_GETATTR(vp, &forat);
1396 if (vp->v_type != VREG) {
1400 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1406 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1409 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1410 ioflags = IO_NODELOCKED;
1411 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1412 ioflags = (IO_SYNC | IO_NODELOCKED);
1414 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1415 uiop->uio_rw = UIO_WRITE;
1416 uiop->uio_segflg = UIO_SYSSPACE;
1417 uiop->uio_td = NULL;
1418 uiop->uio_offset = nfsd->nd_off;
1419 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1420 if (uiop->uio_resid > 0) {
1428 uiop->uio_iovcnt = i;
1429 iov = kmalloc(i * sizeof(struct iovec), M_TEMP, M_WAITOK);
1430 uiop->uio_iov = ivp = iov;
1433 if (mp1->m_len > 0) {
1434 ivp->iov_base = mtod(mp1, caddr_t);
1435 ivp->iov_len = mp1->m_len;
1441 error = VOP_WRITE(vp, uiop, ioflags, cred);
1442 nfsstats.srvvop_writes++;
1444 kfree((caddr_t)iov, M_TEMP);
1449 aftat_ret = VOP_GETATTR(vp, &va);
1455 * Loop around generating replies for all write rpcs that have
1456 * now been completed.
1460 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1462 nfsm_writereply(&info, nfsd, slp, error,
1463 NFSX_WCCDATA(info.v3));
1465 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1469 nfsm_writereply(&info, nfsd, slp, error,
1470 NFSX_PREOPATTR(info.v3) +
1471 NFSX_POSTOPORFATTR(info.v3) +
1473 NFSX_WRITEVERF(info.v3));
1475 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1477 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1478 *tl++ = txdr_unsigned(nfsd->nd_len);
1479 *tl++ = txdr_unsigned(swp->nd_stable);
1481 * Actually, there is no need to txdr these fields,
1482 * but it may make the values more human readable,
1483 * for debugging purposes.
1485 if (nfsver.tv_sec == 0)
1487 *tl++ = txdr_unsigned(nfsver.tv_sec);
1488 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1490 fp = nfsm_build(&info, NFSX_V2FATTR);
1491 nfsm_srvfattr(nfsd, &va, fp);
1494 nfsd->nd_mreq = info.mreq;
1496 panic("nfsrv_write: nd_mrep not free");
1499 * Done. Put it at the head of the timer queue so that
1500 * the final phase can return the reply.
1504 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1506 nfsd = swp->nd_coalesce.lh_first;
1508 LIST_REMOVE(nfsd, nd_tq);
1512 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1517 * Search for a reply to return.
1519 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next) {
1520 if (nfsd->nd_mreq) {
1521 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1522 LIST_REMOVE(nfsd, nd_tq);
1528 *mrq = nfsd->nd_mreq;
1537 * Coalesce the write request nfsd into owp. To do this we must:
1538 * - remove nfsd from the queues
1539 * - merge nfsd->nd_mrep into owp->nd_mrep
1540 * - update the nd_eoff and nd_stable for owp
1541 * - put nfsd on owp's nd_coalesce list
1542 * NB: Must be called at splsoftclock().
1545 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1549 struct nfsrv_descript *p;
1551 NFS_DPF(WG, ("C%03x-%03x",
1552 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1553 LIST_REMOVE(nfsd, nd_hash);
1554 LIST_REMOVE(nfsd, nd_tq);
1555 if (owp->nd_eoff < nfsd->nd_eoff) {
1556 overlap = owp->nd_eoff - nfsd->nd_off;
1558 panic("nfsrv_coalesce: bad off");
1560 m_adj(nfsd->nd_mrep, overlap);
1564 mp1->m_next = nfsd->nd_mrep;
1565 owp->nd_eoff = nfsd->nd_eoff;
1567 m_freem(nfsd->nd_mrep);
1568 nfsd->nd_mrep = NULL;
1569 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1570 owp->nd_stable = NFSV3WRITE_FILESYNC;
1571 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1572 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1573 owp->nd_stable = NFSV3WRITE_DATASYNC;
1574 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1577 * If nfsd had anything else coalesced into it, transfer them
1578 * to owp, otherwise their replies will never get sent.
1580 for (p = nfsd->nd_coalesce.lh_first; p;
1581 p = nfsd->nd_coalesce.lh_first) {
1582 LIST_REMOVE(p, nd_tq);
1583 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1588 * nfs create service
1589 * now does a truncate to 0 length via. setattr if it already exists
1592 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1593 struct thread *td, struct mbuf **mrq)
1595 struct sockaddr *nam = nfsd->nd_nam;
1596 struct ucred *cred = &nfsd->nd_cr;
1597 struct nfs_fattr *fp;
1598 struct vattr va, dirfor, diraft;
1599 struct vattr *vap = &va;
1600 struct nfsv2_sattr *sp;
1602 struct nlookupdata nd;
1603 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1604 udev_t rdev = NOUDEV;
1606 int how, exclusive_flag = 0;
1614 u_char cverf[NFSX_V3CREATEVERF];
1615 struct nfsm_info info;
1617 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1623 info.mrep = nfsd->nd_mrep;
1625 info.md = nfsd->nd_md;
1626 info.dpos = nfsd->nd_dpos;
1627 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1629 fhp = &nfh.fh_generic;
1630 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1631 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1634 * Call namei and do initial cleanup to get a few things
1635 * out of the way. If we get an initial error we cleanup
1636 * and return here to avoid special-casing the invalid nd
1637 * structure through the rest of the case. dirp may be
1638 * set even if an error occurs, but the nd structure will not
1639 * be valid at all if an error occurs so we have to invalidate it
1640 * prior to calling nfsm_reply ( which might goto nfsmout ).
1642 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1643 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1644 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1645 mp = vfs_getvfs(&fhp->fh_fsid);
1649 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1656 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1657 NFSX_WCCDATA(info.v3), &error));
1658 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1659 diraft_ret, &diraft);
1665 * No error. Continue. State:
1668 * vp may be valid or NULL if the target does not
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 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1680 how = fxdr_unsigned(int, *tl);
1682 case NFSV3CREATE_GUARDED:
1688 case NFSV3CREATE_UNCHECKED:
1689 ERROROUT(nfsm_srvsattr(&info, vap));
1691 case NFSV3CREATE_EXCLUSIVE:
1692 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1693 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1697 vap->va_type = VREG;
1699 NULLOUT(sp = nfsm_dissect(&info, 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.
1729 if (vap->va_mode == (mode_t)VNOVAL)
1731 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1733 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1738 if (exclusive_flag) {
1741 bcopy(cverf, (caddr_t)&vap->va_atime,
1743 error = VOP_SETATTR(vp, vap, cred);
1747 vap->va_type == VCHR ||
1748 vap->va_type == VBLK ||
1749 vap->va_type == VFIFO
1752 * Handle SysV FIFO node special cases. All other
1753 * devices require super user to access.
1755 if (vap->va_type == VCHR && rdev == 0xffffffff)
1756 vap->va_type = VFIFO;
1757 if (vap->va_type != VFIFO &&
1758 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1761 vap->va_rmajor = umajor(rdev);
1762 vap->va_rminor = uminor(rdev);
1765 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1772 * XXX what is this junk supposed to do ?
1779 * release dvp prior to lookup
1787 * Even though LOCKPARENT was cleared, ni_dvp may
1790 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1791 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1792 nd.ni_cnd.cn_td = td;
1793 nd.ni_cnd.cn_cred = cred;
1795 error = lookup(&nd);
1799 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1801 /* fall through on certain errors */
1803 nfsrv_object_create(nd.ni_vp);
1804 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1813 if (vap->va_size != -1) {
1814 error = nfsrv_access(mp, vp, VWRITE, cred,
1815 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1817 tempsize = vap->va_size;
1819 vap->va_size = tempsize;
1820 error = VOP_SETATTR(vp, vap, cred);
1826 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1827 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1829 error = VOP_GETATTR(vp, vap);
1832 if (exclusive_flag && !error &&
1833 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1835 diraft_ret = VOP_GETATTR(dirp, &diraft);
1839 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1840 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1841 NFSX_WCCDATA(info.v3),
1845 nfsm_srvpostop_fh(&info, fhp);
1846 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1848 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1849 diraft_ret, &diraft);
1852 nfsm_srvfhtom(&info, fhp);
1853 fp = nfsm_build(&info, NFSX_V2FATTR);
1854 nfsm_srvfattr(nfsd, vap, fp);
1859 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1880 * nfs v3 mknod service
1883 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1884 struct thread *td, struct mbuf **mrq)
1886 struct sockaddr *nam = nfsd->nd_nam;
1887 struct ucred *cred = &nfsd->nd_cr;
1888 struct vattr va, dirfor, diraft;
1889 struct vattr *vap = &va;
1891 struct nlookupdata nd;
1892 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1899 struct nfsm_info info;
1901 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1907 info.mrep = nfsd->nd_mrep;
1909 info.md = nfsd->nd_md;
1910 info.dpos = nfsd->nd_dpos;
1912 fhp = &nfh.fh_generic;
1913 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1914 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1917 * Handle nfs_namei() call. If an error occurs, the nd structure
1918 * is not valid. However, nfsm_*() routines may still jump to
1922 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1923 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1924 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1926 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1928 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1929 NFSX_WCCDATA(1), &error));
1930 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1931 diraft_ret, &diraft);
1935 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1936 vtyp = nfsv3tov_type(*tl);
1937 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1938 error = NFSERR_BADTYPE;
1942 ERROROUT(nfsm_srvsattr(&info, vap));
1943 if (vtyp == VCHR || vtyp == VBLK) {
1944 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1945 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1946 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1950 * Iff doesn't exist, create it.
1956 vap->va_type = vtyp;
1957 if (vap->va_mode == (mode_t)VNOVAL)
1959 if (vtyp == VSOCK) {
1961 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1965 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1969 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1977 * send response, cleanup, return.
1989 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1990 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1992 error = VOP_GETATTR(vp, vap);
1998 diraft_ret = VOP_GETATTR(dirp, &diraft);
2003 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2004 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
2005 NFSX_WCCDATA(1), &error));
2007 nfsm_srvpostop_fh(&info, fhp);
2008 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2010 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2011 diraft_ret, &diraft);
2031 * nfs remove service
2034 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2035 struct thread *td, struct mbuf **mrq)
2037 struct sockaddr *nam = nfsd->nd_nam;
2038 struct ucred *cred = &nfsd->nd_cr;
2039 struct nlookupdata nd;
2040 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2044 struct vattr dirfor, diraft;
2047 struct nfsm_info info;
2049 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2055 info.mrep = nfsd->nd_mrep;
2057 info.md = nfsd->nd_md;
2058 info.dpos = nfsd->nd_dpos;
2059 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2061 fhp = &nfh.fh_generic;
2062 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2063 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2065 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2066 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2067 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2070 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2073 if (vp->v_type == VDIR) {
2074 error = EPERM; /* POSIX */
2078 * The root of a mounted filesystem cannot be deleted.
2080 if (vp->v_flag & VROOT) {
2092 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2097 if (dirp && info.v3)
2098 diraft_ret = VOP_GETATTR(dirp, &diraft);
2099 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2101 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2102 diraft_ret, &diraft);
2122 * nfs rename service
2125 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2126 struct thread *td, struct mbuf **mrq)
2128 struct sockaddr *nam = nfsd->nd_nam;
2129 struct ucred *cred = &nfsd->nd_cr;
2130 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2131 int tdirfor_ret = 1, tdiraft_ret = 1;
2132 struct nlookupdata fromnd, tond;
2133 struct vnode *fvp, *fdirp, *fdvp;
2134 struct vnode *tvp, *tdirp, *tdvp;
2135 struct namecache *ncp;
2136 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2138 fhandle_t *ffhp, *tfhp;
2140 struct nfsm_info info;
2142 info.mrep = nfsd->nd_mrep;
2144 info.md = nfsd->nd_md;
2145 info.dpos = nfsd->nd_dpos;
2146 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2148 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2152 ffhp = &fnfh.fh_generic;
2153 tfhp = &tnfh.fh_generic;
2156 * Clear fields incase goto nfsmout occurs from macro.
2159 nlookup_zero(&fromnd);
2160 nlookup_zero(&tond);
2164 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2165 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2168 * Remember our original uid so that we can reset cr_uid before
2169 * the second nfs_namei() call, in case it is remapped.
2171 saved_uid = cred->cr_uid;
2172 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2174 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2175 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2178 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2181 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2182 2 * NFSX_WCCDATA(info.v3), &error));
2183 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2184 fdiraft_ret, &fdiraft);
2185 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2186 tdiraft_ret, &tdiraft);
2192 * We have to unlock the from ncp before we can safely lookup
2195 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2196 cache_unlock(&fromnd.nl_nch);
2197 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2198 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2199 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2200 cred->cr_uid = saved_uid;
2202 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2203 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2204 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2207 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2215 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2216 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2217 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2218 cache_lock(&fromnd.nl_nch);
2219 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2221 cache_unlock(&tond.nl_nch);
2222 cache_lock(&fromnd.nl_nch);
2223 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2224 cache_lock(&tond.nl_nch);
2225 cache_resolve(&tond.nl_nch, tond.nl_cred);
2227 fromnd.nl_flags |= NLC_NCPISLOCKED;
2229 fvp = fromnd.nl_nch.ncp->nc_vp;
2230 tvp = tond.nl_nch.ncp->nc_vp;
2233 * Set fdvp and tdvp. We haven't done all the topology checks
2234 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2235 * point). If we get through the checks these will be guarenteed
2238 * Holding the children ncp's should be sufficient to prevent
2239 * fdvp and tdvp ripouts.
2241 if (fromnd.nl_nch.ncp->nc_parent)
2242 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2245 if (tond.nl_nch.ncp->nc_parent)
2246 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2251 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2257 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2264 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2272 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2279 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2286 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2294 * You cannot rename a source into itself or a subdirectory of itself.
2295 * We check this by travsering the target directory upwards looking
2296 * for a match against the source.
2299 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2300 if (fromnd.nl_nch.ncp == ncp) {
2308 * If source is the same as the destination (that is the
2309 * same vnode with the same name in the same directory),
2310 * then there is nothing to do.
2312 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2317 * The VOP_NRENAME function releases all vnode references &
2318 * locks prior to returning so we need to clear the pointers
2319 * to bypass cleanup code later on.
2321 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2322 fdvp, tdvp, tond.nl_cred);
2331 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2333 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2334 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2335 2 * NFSX_WCCDATA(info.v3), &error));
2337 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2338 fdiraft_ret, &fdiraft);
2339 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2340 tdiraft_ret, &tdiraft);
2349 nlookup_done(&tond);
2352 nlookup_done(&fromnd);
2360 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2361 struct thread *td, struct mbuf **mrq)
2363 struct sockaddr *nam = nfsd->nd_nam;
2364 struct ucred *cred = &nfsd->nd_cr;
2365 struct nlookupdata nd;
2366 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2373 struct vattr dirfor, diraft, at;
2375 fhandle_t *fhp, *dfhp;
2376 struct nfsm_info info;
2378 info.mrep = nfsd->nd_mrep;
2380 info.md = nfsd->nd_md;
2381 info.dpos = nfsd->nd_dpos;
2382 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2384 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2386 dirp = dvp = vp = xp = NULL;
2389 fhp = &nfh.fh_generic;
2390 dfhp = &dnfh.fh_generic;
2391 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2392 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2393 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2395 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2396 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2398 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2399 NFSX_POSTOPATTR(info.v3) +
2400 NFSX_WCCDATA(info.v3),
2402 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2403 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2404 diraft_ret, &diraft);
2409 if (xp->v_type == VDIR) {
2410 error = EPERM; /* POSIX */
2414 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2415 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2416 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2419 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2428 if (xp->v_mount != dvp->v_mount)
2433 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2441 getret = VOP_GETATTR(xp, &at);
2443 diraft_ret = VOP_GETATTR(dirp, &diraft);
2444 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2445 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2448 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2449 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2450 diraft_ret, &diraft);
2474 * nfs symbolic link service
2477 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2478 struct thread *td, struct mbuf **mrq)
2480 struct sockaddr *nam = nfsd->nd_nam;
2481 struct ucred *cred = &nfsd->nd_cr;
2482 struct vattr va, dirfor, diraft;
2483 struct nlookupdata nd;
2484 struct vattr *vap = &va;
2485 struct nfsv2_sattr *sp;
2486 char *pathcp = NULL;
2489 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2495 struct nfsm_info info;
2497 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2503 info.mrep = nfsd->nd_mrep;
2505 info.md = nfsd->nd_md;
2506 info.dpos = nfsd->nd_dpos;
2507 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2509 fhp = &nfh.fh_generic;
2510 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2511 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2513 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2514 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2515 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2518 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2525 ERROROUT(nfsm_srvsattr(&info, vap));
2527 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2528 pathcp = kmalloc(len2 + 1, M_TEMP, M_WAITOK);
2529 iv.iov_base = pathcp;
2531 io.uio_resid = len2;
2535 io.uio_segflg = UIO_SYSSPACE;
2536 io.uio_rw = UIO_READ;
2538 ERROROUT(nfsm_mtouio(&info, &io, len2));
2540 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2541 vap->va_mode = nfstov_mode(sp->sa_mode);
2543 *(pathcp + len2) = '\0';
2549 if (vap->va_mode == (mode_t)VNOVAL)
2553 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2557 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2558 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2560 error = VOP_GETATTR(vp, vap);
2575 kfree(pathcp, M_TEMP);
2579 diraft_ret = VOP_GETATTR(dirp, &diraft);
2583 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2584 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2585 NFSX_WCCDATA(info.v3),
2589 nfsm_srvpostop_fh(&info, fhp);
2590 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2592 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2593 diraft_ret, &diraft);
2606 kfree(pathcp, M_TEMP);
2614 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2615 struct thread *td, struct mbuf **mrq)
2617 struct sockaddr *nam = nfsd->nd_nam;
2618 struct ucred *cred = &nfsd->nd_cr;
2619 struct vattr va, dirfor, diraft;
2620 struct vattr *vap = &va;
2621 struct nfs_fattr *fp;
2622 struct nlookupdata nd;
2624 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2630 struct nfsm_info info;
2632 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2638 info.dpos = nfsd->nd_dpos;
2639 info.mrep = nfsd->nd_mrep;
2641 info.md = nfsd->nd_md;
2642 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2644 fhp = &nfh.fh_generic;
2645 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2646 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2648 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2649 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2650 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2653 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2656 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2657 NFSX_WCCDATA(info.v3), &error));
2658 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2659 diraft_ret, &diraft);
2665 ERROROUT(nfsm_srvsattr(&info, vap));
2667 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2668 vap->va_mode = nfstov_mode(*tl++);
2672 * At this point nd.ni_dvp is referenced and exclusively locked and
2673 * nd.ni_vp, if it exists, is referenced but not locked.
2676 vap->va_type = VDIR;
2683 * Issue mkdir op. Since SAVESTART is not set, the pathname
2684 * component is freed by the VOP call. This will fill-in
2685 * nd.ni_vp, reference, and exclusively lock it.
2687 if (vap->va_mode == (mode_t)VNOVAL)
2690 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2695 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2696 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2698 error = VOP_GETATTR(vp, vap);
2702 diraft_ret = VOP_GETATTR(dirp, &diraft);
2703 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2704 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2705 NFSX_WCCDATA(info.v3),
2709 nfsm_srvpostop_fh(&info, fhp);
2710 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2712 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2713 diraft_ret, &diraft);
2715 nfsm_srvfhtom(&info, fhp);
2716 fp = nfsm_build(&info, NFSX_V2FATTR);
2717 nfsm_srvfattr(nfsd, vap, fp);
2742 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2743 struct thread *td, struct mbuf **mrq)
2745 struct sockaddr *nam = nfsd->nd_nam;
2746 struct ucred *cred = &nfsd->nd_cr;
2747 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2751 struct vattr dirfor, diraft;
2754 struct nlookupdata nd;
2755 struct nfsm_info info;
2757 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2763 info.mrep = nfsd->nd_mrep;
2765 info.md = nfsd->nd_md;
2766 info.dpos = nfsd->nd_dpos;
2767 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2769 fhp = &nfh.fh_generic;
2770 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2771 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2773 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2774 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2775 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2778 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2781 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2782 NFSX_WCCDATA(info.v3), &error));
2783 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2784 diraft_ret, &diraft);
2788 if (vp->v_type != VDIR) {
2794 * The root of a mounted filesystem cannot be deleted.
2796 if (vp->v_flag & VROOT)
2800 * Issue or abort op. Since SAVESTART is not set, path name
2801 * component is freed by the VOP after either.
2808 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2815 diraft_ret = VOP_GETATTR(dirp, &diraft);
2816 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2818 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2819 diraft_ret, &diraft);
2841 * nfs readdir service
2842 * - mallocs what it thinks is enough to read
2843 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2844 * - calls VOP_READDIR()
2845 * - loops around building the reply
2846 * if the output generated exceeds count break out of loop
2847 * The nfsm_clget macro is used here so that the reply will be packed
2848 * tightly in mbuf clusters.
2849 * - it only knows that it has encountered eof when the VOP_READDIR()
2851 * - as such one readdir rpc will return eof false although you are there
2852 * and then the next will return eof
2853 * - it trims out records with d_fileno == 0
2854 * this doesn't matter for Unix clients, but they might confuse clients
2856 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2857 * than requested, but this may not apply to all filesystems. For
2858 * example, client NFS does not { although it is never remote mounted
2860 * The alternate call nfsrv_readdirplus() does lookups as well.
2861 * PS: The NFS protocol spec. does not clarify what the "count" byte
2862 * argument is a count of.. just name strings and file id's or the
2863 * entire reply rpc or ...
2864 * I tried just file name and id sizes and it confused the Sun client,
2865 * so I am using the full rpc size now. The "paranoia.." comment refers
2866 * to including the status longwords that are not a part of the dir.
2867 * "entry" structures, but are in the rpc.
2871 u_int32_t fl_postopok;
2872 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2874 u_int32_t fl_fhsize;
2875 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2879 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2880 struct thread *td, struct mbuf **mrq)
2882 struct sockaddr *nam = nfsd->nd_nam;
2883 struct ucred *cred = &nfsd->nd_cr;
2888 struct mbuf *mp1, *mp2;
2889 char *cpos, *cend, *rbuf;
2890 struct vnode *vp = NULL;
2891 struct mount *mp = NULL;
2897 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2898 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2903 off_t *cookies = NULL, *cookiep;
2904 struct nfsm_info info;
2906 info.mrep = nfsd->nd_mrep;
2908 info.md = nfsd->nd_md;
2909 info.dpos = nfsd->nd_dpos;
2910 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2912 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2913 fhp = &nfh.fh_generic;
2914 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2916 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2917 toff = fxdr_hyper(tl);
2920 verf = fxdr_hyper(tl);
2924 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2925 toff = fxdr_unsigned(u_quad_t, *tl++);
2927 verf = 0; /* shut up gcc */
2931 cnt = fxdr_unsigned(int, *tl);
2932 siz = roundup2(cnt, DIRBLKSIZ);
2933 xfer = NFS_SRVMAXDATA(nfsd);
2934 if ((unsigned)cnt > xfer)
2936 if ((unsigned)siz > xfer)
2939 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2940 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2941 if (!error && vp->v_type != VDIR) {
2947 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2948 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2954 * Obtain lock on vnode for this section of the code
2958 error = getret = VOP_GETATTR(vp, &at);
2961 * XXX This check may be too strict for Solaris 2.5 clients.
2963 if (!error && toff && verf && verf != at.va_filerev)
2964 error = NFSERR_BAD_COOKIE;
2968 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2972 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2973 NFSX_POSTOPATTR(info.v3), &error));
2974 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2981 * end section. Allocate rbuf and continue
2983 rbuf = kmalloc(siz, M_TEMP, M_WAITOK);
2986 iv.iov_len = fullsiz;
2989 io.uio_offset = (off_t)off;
2990 io.uio_resid = fullsiz;
2991 io.uio_segflg = UIO_SYSSPACE;
2992 io.uio_rw = UIO_READ;
2996 kfree((caddr_t)cookies, M_TEMP);
2999 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3000 off = (off_t)io.uio_offset;
3001 if (!cookies && !error)
3002 error = NFSERR_PERM;
3004 getret = VOP_GETATTR(vp, &at);
3011 kfree((caddr_t)rbuf, M_TEMP);
3013 kfree((caddr_t)cookies, M_TEMP);
3014 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3015 NFSX_POSTOPATTR(info.v3), &error));
3016 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3021 siz -= io.uio_resid;
3024 * If nothing read, return eof
3030 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3031 NFSX_POSTOPATTR(info.v3) +
3032 NFSX_COOKIEVERF(info.v3) +
3036 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3037 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3038 txdr_hyper(at.va_filerev, tl);
3041 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3044 kfree((caddr_t)rbuf, M_TEMP);
3045 kfree((caddr_t)cookies, M_TEMP);
3052 * Check for degenerate cases of nothing useful read.
3053 * If so go try again
3057 dp = (struct dirent *)cpos;
3060 * For some reason FreeBSD's ufs_readdir() chooses to back the
3061 * directory offset up to a block boundary, so it is necessary to
3062 * skip over the records that preceed the requested offset. This
3063 * requires the assumption that file offset cookies monotonically
3066 while (cpos < cend && ncookies > 0 &&
3067 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3068 ((u_quad_t)(*cookiep)) <= toff)) {
3069 dp = _DIRENT_NEXT(dp);
3074 if (cpos >= cend || ncookies == 0) {
3080 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3081 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3082 NFSX_POSTOPATTR(info.v3) +
3083 NFSX_COOKIEVERF(info.v3) + siz,
3086 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3087 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3088 txdr_hyper(at.va_filerev, tl);
3090 mp1 = mp2 = info.mb;
3092 be = bp + M_TRAILINGSPACE(mp1);
3094 /* Loop through the records and build reply */
3095 while (cpos < cend && ncookies > 0) {
3096 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3097 nlen = dp->d_namlen;
3098 rem = nfsm_rndup(nlen) - nlen;
3099 len += (4 * NFSX_UNSIGNED + nlen + rem);
3101 len += 2 * NFSX_UNSIGNED;
3107 * Build the directory record xdr from
3110 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3112 bp += NFSX_UNSIGNED;
3114 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3115 *tl = txdr_unsigned(dp->d_ino >> 32);
3116 bp += NFSX_UNSIGNED;
3118 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3119 *tl = txdr_unsigned(dp->d_ino);
3120 bp += NFSX_UNSIGNED;
3121 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3122 *tl = txdr_unsigned(nlen);
3123 bp += NFSX_UNSIGNED;
3125 /* And loop around copying the name */
3129 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3134 bcopy(cp, bp, tsiz);
3140 /* And null pad to a int32_t boundary */
3141 for (i = 0; i < rem; i++)
3143 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3145 /* Finish off the record */
3147 *tl = txdr_unsigned(*cookiep >> 32);
3148 bp += NFSX_UNSIGNED;
3149 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3151 *tl = txdr_unsigned(*cookiep);
3152 bp += NFSX_UNSIGNED;
3154 dp = _DIRENT_NEXT(dp);
3161 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3163 bp += NFSX_UNSIGNED;
3164 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3169 bp += NFSX_UNSIGNED;
3170 if (mp1 != info.mb) {
3172 mp1->m_len = bp - mtod(mp1, caddr_t);
3174 mp1->m_len += bp - info.bpos;
3175 kfree((caddr_t)rbuf, M_TEMP);
3176 kfree((caddr_t)cookies, M_TEMP);
3186 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3187 struct thread *td, struct mbuf **mrq)
3189 struct sockaddr *nam = nfsd->nd_nam;
3190 struct ucred *cred = &nfsd->nd_cr;
3195 struct mbuf *mp1, *mp2;
3196 char *cpos, *cend, *rbuf;
3197 struct vnode *vp = NULL, *nvp;
3198 struct mount *mp = NULL;
3201 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3204 struct vattr va, at, *vap = &va;
3205 struct nfs_fattr *fp;
3206 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3207 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3212 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3213 struct nfsm_info info;
3215 info.mrep = nfsd->nd_mrep;
3217 info.md = nfsd->nd_md;
3218 info.dpos = nfsd->nd_dpos;
3219 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3221 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3222 fhp = &nfh.fh_generic;
3223 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3224 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3225 toff = fxdr_hyper(tl);
3228 verf = fxdr_hyper(tl);
3231 siz = fxdr_unsigned(int, *tl++);
3232 cnt = fxdr_unsigned(int, *tl);
3234 siz = roundup2(siz, DIRBLKSIZ);
3235 xfer = NFS_SRVMAXDATA(nfsd);
3236 if ((unsigned)cnt > xfer)
3238 if ((unsigned)siz > xfer)
3241 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3242 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3243 if (!error && vp->v_type != VDIR) {
3249 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3250 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3254 error = getret = VOP_GETATTR(vp, &at);
3257 * XXX This check may be too strict for Solaris 2.5 clients.
3259 if (!error && toff && verf && verf != at.va_filerev)
3260 error = NFSERR_BAD_COOKIE;
3263 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3268 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3269 NFSX_V3POSTOPATTR, &error));
3270 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3275 rbuf = kmalloc(siz, M_TEMP, M_WAITOK);
3278 iv.iov_len = fullsiz;
3281 io.uio_offset = (off_t)off;
3282 io.uio_resid = fullsiz;
3283 io.uio_segflg = UIO_SYSSPACE;
3284 io.uio_rw = UIO_READ;
3288 kfree((caddr_t)cookies, M_TEMP);
3291 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3292 off = (u_quad_t)io.uio_offset;
3293 getret = VOP_GETATTR(vp, &at);
3294 if (!cookies && !error)
3295 error = NFSERR_PERM;
3302 kfree((caddr_t)cookies, M_TEMP);
3303 kfree((caddr_t)rbuf, M_TEMP);
3304 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3305 NFSX_V3POSTOPATTR, &error));
3306 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3311 siz -= io.uio_resid;
3314 * If nothing read, return eof
3320 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3325 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3326 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3327 txdr_hyper(at.va_filerev, tl);
3331 kfree((caddr_t)cookies, M_TEMP);
3332 kfree((caddr_t)rbuf, M_TEMP);
3339 * Check for degenerate cases of nothing useful read.
3340 * If so go try again
3344 dp = (struct dirent *)cpos;
3347 * For some reason FreeBSD's ufs_readdir() chooses to back the
3348 * directory offset up to a block boundary, so it is necessary to
3349 * skip over the records that preceed the requested offset. This
3350 * requires the assumption that file offset cookies monotonically
3353 while (cpos < cend && ncookies > 0 &&
3354 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3355 ((u_quad_t)(*cookiep)) <= toff)) {
3356 dp = _DIRENT_NEXT(dp);
3361 if (cpos >= cend || ncookies == 0) {
3368 * Probe one of the directory entries to see if the filesystem
3371 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3372 error = NFSERR_NOTSUPP;
3375 kfree((caddr_t)cookies, M_TEMP);
3376 kfree((caddr_t)rbuf, M_TEMP);
3377 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3378 NFSX_V3POSTOPATTR, &error));
3379 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3388 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3390 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3391 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3392 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3393 txdr_hyper(at.va_filerev, tl);
3394 mp1 = mp2 = info.mb;
3396 be = bp + M_TRAILINGSPACE(mp1);
3398 /* Loop through the records and build reply */
3399 while (cpos < cend && ncookies > 0) {
3400 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3401 nlen = dp->d_namlen;
3402 rem = nfsm_rndup(nlen) - nlen;
3405 * For readdir_and_lookup get the vnode using
3408 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3410 bzero((caddr_t)nfhp, NFSX_V3FH);
3411 nfhp->fh_fsid = fhp->fh_fsid;
3412 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3417 if (VOP_GETATTR(nvp, vap)) {
3426 * If either the dircount or maxcount will be
3427 * exceeded, get out now. Both of these lengths
3428 * are calculated conservatively, including all
3431 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3433 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3434 if (len > cnt || dirlen > fullsiz) {
3440 * Build the directory record xdr from
3443 fp = (struct nfs_fattr *)&fl.fl_fattr;
3444 nfsm_srvfattr(nfsd, vap, fp);
3445 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3446 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3447 fl.fl_postopok = nfs_true;
3448 fl.fl_fhok = nfs_true;
3449 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3451 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3453 bp += NFSX_UNSIGNED;
3454 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3455 *tl = txdr_unsigned(dp->d_ino >> 32);
3456 bp += NFSX_UNSIGNED;
3457 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3458 *tl = txdr_unsigned(dp->d_ino);
3459 bp += NFSX_UNSIGNED;
3460 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3461 *tl = txdr_unsigned(nlen);
3462 bp += NFSX_UNSIGNED;
3464 /* And loop around copying the name */
3468 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3469 if ((bp + xfer) > be)
3473 bcopy(cp, bp, tsiz);
3478 /* And null pad to a int32_t boundary */
3479 for (i = 0; i < rem; i++)
3483 * Now copy the flrep structure out.
3485 xfer = sizeof (struct flrep);
3488 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3489 if ((bp + xfer) > be)
3493 bcopy(cp, bp, tsiz);
3500 dp = _DIRENT_NEXT(dp);
3507 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3509 bp += NFSX_UNSIGNED;
3510 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3515 bp += NFSX_UNSIGNED;
3516 if (mp1 != info.mb) {
3518 mp1->m_len = bp - mtod(mp1, caddr_t);
3520 mp1->m_len += bp - info.bpos;
3521 kfree((caddr_t)cookies, M_TEMP);
3522 kfree((caddr_t)rbuf, M_TEMP);
3531 * nfs commit service
3534 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3535 struct thread *td, struct mbuf **mrq)
3537 struct sockaddr *nam = nfsd->nd_nam;
3538 struct ucred *cred = &nfsd->nd_cr;
3539 struct vattr bfor, aft;
3540 struct vnode *vp = NULL;
3541 struct mount *mp = NULL;
3545 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3547 struct nfsm_info info;
3549 info.mrep = nfsd->nd_mrep;
3551 info.md = nfsd->nd_md;
3552 info.dpos = nfsd->nd_dpos;
3554 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3555 fhp = &nfh.fh_generic;
3556 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3557 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3560 * XXX At this time VOP_FSYNC() does not accept offset and byte
3561 * count parameters, so these arguments are useless (someday maybe).
3563 off = fxdr_hyper(tl);
3565 cnt = fxdr_unsigned(int, *tl);
3566 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3567 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3569 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3570 2 * NFSX_UNSIGNED, &error));
3571 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3576 for_ret = VOP_GETATTR(vp, &bfor);
3579 * RFC 1813 3.3.21: If count is 0, a flush from offset to the end of
3580 * file is done. At this time VOP_FSYNC does not accept offset and
3581 * byte count parameters, so call VOP_FSYNC the whole file for now.
3583 if (cnt == 0 || cnt > MAX_COMMIT_COUNT) {
3585 * Give up and do the whole thing
3588 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3589 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3591 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3594 * Locate and synchronously write any buffers that fall
3595 * into the requested range. Note: we are assuming that
3596 * f_iosize is a power of 2.
3598 int iosize = vp->v_mount->mnt_stat.f_iosize;
3599 int iomask = iosize - 1;
3603 * Align to iosize boundry, super-align to page boundry.
3606 cnt += off & iomask;
3607 off &= ~(u_quad_t)iomask;
3609 if (off & PAGE_MASK) {
3610 cnt += off & PAGE_MASK;
3611 off &= ~(u_quad_t)PAGE_MASK;
3616 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3617 vm_object_page_clean(vp->v_object, off / PAGE_SIZE,
3618 (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3622 while (error == 0 || cnt > 0) {
3626 * If we have a buffer and it is marked B_DELWRI we
3627 * have to lock and write it. Otherwise the prior
3628 * write is assumed to have already been committed.
3630 * WARNING: FINDBLK_TEST buffers represent stable
3631 * storage but not necessarily stable
3632 * content. It is ok in this case.
3634 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3635 if (bp->b_flags & B_DELWRI)
3636 bp = findblk(vp, loffset, 0);
3641 if (bp->b_flags & B_DELWRI) {
3658 aft_ret = VOP_GETATTR(vp, &aft);
3661 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3662 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3664 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3667 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3668 if (nfsver.tv_sec == 0)
3670 *tl++ = txdr_unsigned(nfsver.tv_sec);
3671 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3683 * nfs statfs service
3686 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3687 struct thread *td, struct mbuf **mrq)
3689 struct sockaddr *nam = nfsd->nd_nam;
3690 struct ucred *cred = &nfsd->nd_cr;
3692 struct nfs_statfs *sfp;
3693 int error = 0, rdonly, getret = 1;
3694 struct vnode *vp = NULL;
3695 struct mount *mp = NULL;
3699 struct statfs statfs;
3701 struct nfsm_info info;
3703 info.mrep = nfsd->nd_mrep;
3705 info.md = nfsd->nd_md;
3706 info.dpos = nfsd->nd_dpos;
3707 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3709 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3710 fhp = &nfh.fh_generic;
3711 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3712 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3713 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3715 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3716 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3721 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3722 getret = VOP_GETATTR(vp, &at);
3725 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3726 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3729 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3734 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3736 tval = (u_quad_t)sf->f_blocks;
3737 tval *= (u_quad_t)sf->f_bsize;
3738 txdr_hyper(tval, &sfp->sf_tbytes);
3739 tval = (u_quad_t)sf->f_bfree;
3740 tval *= (u_quad_t)sf->f_bsize;
3741 txdr_hyper(tval, &sfp->sf_fbytes);
3742 tval = (u_quad_t)sf->f_bavail;
3743 tval *= (u_quad_t)sf->f_bsize;
3744 txdr_hyper(tval, &sfp->sf_abytes);
3745 sfp->sf_tfiles.nfsuquad[0] = 0;
3746 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3747 sfp->sf_ffiles.nfsuquad[0] = 0;
3748 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3749 sfp->sf_afiles.nfsuquad[0] = 0;
3750 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3751 sfp->sf_invarsec = 0;
3753 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3754 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3755 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3756 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3757 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3767 * nfs fsinfo service
3770 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3771 struct thread *td, struct mbuf **mrq)
3773 struct sockaddr *nam = nfsd->nd_nam;
3774 struct ucred *cred = &nfsd->nd_cr;
3775 struct nfsv3_fsinfo *sip;
3776 int error = 0, rdonly, getret = 1, pref;
3777 struct vnode *vp = NULL;
3778 struct mount *mp = NULL;
3784 struct nfsm_info info;
3786 info.mrep = nfsd->nd_mrep;
3788 info.md = nfsd->nd_md;
3789 info.dpos = nfsd->nd_dpos;
3791 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3792 fhp = &nfh.fh_generic;
3793 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3794 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3795 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3797 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3798 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3803 /* XXX Try to make a guess on the max file size. */
3804 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3805 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3807 getret = VOP_GETATTR(vp, &at);
3810 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3811 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3812 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3813 sip = nfsm_build(&info, NFSX_V3FSINFO);
3817 * There should be file system VFS OP(s) to get this information.
3818 * For now, assume ufs.
3820 if (slp->ns_so->so_type == SOCK_DGRAM)
3821 pref = NFS_MAXDGRAMDATA;
3824 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3825 sip->fs_rtpref = txdr_unsigned(pref);
3826 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3827 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3828 sip->fs_wtpref = txdr_unsigned(pref);
3829 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3830 sip->fs_dtpref = txdr_unsigned(pref);
3831 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3832 sip->fs_timedelta.nfsv3_sec = 0;
3833 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3834 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3835 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3836 NFSV3FSINFO_CANSETTIME);
3845 * nfs pathconf service
3848 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3849 struct thread *td, struct mbuf **mrq)
3851 struct sockaddr *nam = nfsd->nd_nam;
3852 struct ucred *cred = &nfsd->nd_cr;
3853 struct nfsv3_pathconf *pc;
3854 int error = 0, rdonly, getret = 1;
3855 register_t linkmax, namemax, chownres, notrunc;
3856 struct vnode *vp = NULL;
3857 struct mount *mp = NULL;
3861 struct nfsm_info info;
3863 info.mrep = nfsd->nd_mrep;
3865 info.md = nfsd->nd_md;
3866 info.dpos = nfsd->nd_dpos;
3868 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3869 fhp = &nfh.fh_generic;
3870 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3871 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3872 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3874 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3875 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3879 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3881 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3883 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3885 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3886 getret = VOP_GETATTR(vp, &at);
3889 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3890 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3892 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3897 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3899 pc->pc_linkmax = txdr_unsigned(linkmax);
3900 pc->pc_namemax = txdr_unsigned(namemax);
3901 pc->pc_notrunc = txdr_unsigned(notrunc);
3902 pc->pc_chownrestricted = txdr_unsigned(chownres);
3905 * These should probably be supported by VOP_PATHCONF(), but
3906 * until msdosfs is exportable (why would you want to?), the
3907 * Unix defaults should be ok.
3909 pc->pc_caseinsensitive = nfs_false;
3910 pc->pc_casepreserving = nfs_true;
3919 * Null operation, used by clients to ping server
3923 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3924 struct thread *td, struct mbuf **mrq)
3926 struct nfsm_info info;
3927 int error = NFSERR_RETVOID;
3929 info.mrep = nfsd->nd_mrep;
3932 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3933 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3940 * No operation, used for obsolete procedures
3944 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3945 struct thread *td, struct mbuf **mrq)
3947 struct nfsm_info info;
3950 info.mrep = nfsd->nd_mrep;
3953 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3954 if (nfsd->nd_repstat)
3955 error = nfsd->nd_repstat;
3957 error = EPROCUNAVAIL;
3958 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3966 * Perform access checking for vnodes obtained from file handles that would
3967 * refer to files already opened by a Unix client. You cannot just use
3968 * vn_writechk() and VOP_ACCESS() for two reasons.
3969 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3970 * 2 - The owner is to be given access irrespective of mode bits for some
3971 * operations, so that processes that chmod after opening a file don't
3972 * break. I don't like this because it opens a security hole, but since
3973 * the nfs server opens a security hole the size of a barn door anyhow,
3976 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3977 * will return EPERM instead of EACCESS. EPERM is always an error.
3980 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3981 int rdonly, struct thread *td, int override)
3986 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3987 if (flags & VWRITE) {
3988 /* Just vn_writechk() changed to check rdonly */
3990 * Disallow write attempts on read-only file systems;
3991 * unless the file is a socket or a block or character
3992 * device resident on the file system.
3995 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3996 switch (vp->v_type) {
4006 * If there's shared text associated with
4007 * the inode, we can't allow writing.
4009 if (vp->v_flag & VTEXT)
4012 error = VOP_GETATTR(vp, &vattr);
4015 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
4017 * Allow certain operations for the owner (reads and writes
4018 * on files that are already open).
4020 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
4024 #endif /* NFS_NOSERVER */