2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_serv.c 8.8 (Berkeley) 7/31/95
37 * $FreeBSD: src/sys/nfs/nfs_serv.c,v 1.93.2.6 2002/12/29 18:19:53 dillon Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_serv.c,v 1.48 2008/09/17 21:44:24 dillon Exp $
42 * nfs version 2 and 3 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request
46 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
47 * 3 - build the rpc reply in an mbuf list
49 * - do not mix the phases, since the nfsm_?? macros can return failures
50 * on a bad rpc or similar and do not do any vrele() or vput()'s
52 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
53 * error number iff error != 0 whereas
54 * returning an error from the server function implies a fatal error
55 * such as a badly constructed rpc request that should be dropped without
57 * For Version 3, nfsm_reply() does not return for the error case, since
58 * most version 3 rpcs return more than the status for error cases.
61 * Warning: always pay careful attention to resource cleanup on return
62 * and note that nfsm_*() macros can terminate a procedure on certain
66 #include <sys/param.h>
67 #include <sys/systm.h>
70 #include <sys/nlookup.h>
71 #include <sys/namei.h>
72 #include <sys/unistd.h>
73 #include <sys/vnode.h>
74 #include <sys/mount.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/malloc.h>
79 #include <sys/dirent.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
86 #include <vm/vm_extern.h>
87 #include <vm/vm_zone.h>
88 #include <vm/vm_object.h>
92 #include <sys/thread2.h>
98 #include "nfsm_subs.h"
101 #define nfsdbprintf(info) kprintf info
103 #define nfsdbprintf(info)
106 #define MAX_COMMIT_COUNT (1024 * 1024)
108 #define NUM_HEURISTIC 1017
109 #define NHUSE_INIT 64
111 #define NHUSE_MAX 2048
113 static struct nfsheur {
114 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
115 off_t nh_nextr; /* next offset for sequential detection */
116 int nh_use; /* use count for selection */
117 int nh_seqcount; /* heuristic */
118 } nfsheur[NUM_HEURISTIC];
120 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
123 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
126 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
127 int nfsrvw_procrastinate_v3 = 0;
129 static struct timespec nfsver;
131 SYSCTL_DECL(_vfs_nfs);
134 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
135 static int nfs_commit_blks;
136 static int nfs_commit_miss;
137 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 0, "");
138 SYSCTL_INT(_vfs_nfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 0, "");
140 static int nfsrv_access (struct mount *, struct vnode *, int,
141 struct ucred *, int, struct thread *, int);
142 static void nfsrvw_coalesce (struct nfsrv_descript *,
143 struct nfsrv_descript *);
146 * nfs v3 access service
149 nfsrv3_access(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
150 struct thread *td, struct mbuf **mrq)
152 struct sockaddr *nam = nfsd->nd_nam;
153 struct ucred *cred = &nfsd->nd_cr;
154 struct vnode *vp = NULL;
155 struct mount *mp = NULL;
158 int error = 0, rdonly, getret;
159 struct vattr vattr, *vap = &vattr;
160 u_long testmode, nfsmode;
161 struct nfsm_info info;
164 info.dpos = nfsd->nd_dpos;
165 info.md = nfsd->nd_md;
166 info.mrep = nfsd->nd_mrep;
169 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
170 fhp = &nfh.fh_generic;
171 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
172 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
173 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
174 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
176 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
177 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
181 nfsmode = fxdr_unsigned(u_int32_t, *tl);
182 if ((nfsmode & NFSV3ACCESS_READ) &&
183 nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 0))
184 nfsmode &= ~NFSV3ACCESS_READ;
185 if (vp->v_type == VDIR)
186 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
189 testmode = (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
190 if ((nfsmode & testmode) &&
191 nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 0))
192 nfsmode &= ~testmode;
193 if (vp->v_type == VDIR)
194 testmode = NFSV3ACCESS_LOOKUP;
196 testmode = NFSV3ACCESS_EXECUTE;
197 if ((nfsmode & testmode) &&
198 nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0))
199 nfsmode &= ~testmode;
200 getret = VOP_GETATTR(vp, vap);
203 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
204 NFSX_POSTOPATTR(1) + NFSX_UNSIGNED, &error));
205 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
206 tl = nfsm_build(&info, NFSX_UNSIGNED);
207 *tl = txdr_unsigned(nfsmode);
216 * nfs getattr service
219 nfsrv_getattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
220 struct thread *td, struct mbuf **mrq)
222 struct sockaddr *nam = nfsd->nd_nam;
223 struct ucred *cred = &nfsd->nd_cr;
224 struct nfs_fattr *fp;
226 struct vattr *vap = &va;
227 struct vnode *vp = NULL;
228 struct mount *mp = NULL;
231 int error = 0, rdonly;
232 struct nfsm_info info;
234 info.mrep = nfsd->nd_mrep;
235 info.md = nfsd->nd_md;
236 info.dpos = nfsd->nd_dpos;
239 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
240 fhp = &nfh.fh_generic;
241 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
242 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
243 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
245 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
249 error = VOP_GETATTR(vp, vap);
252 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
253 NFSX_FATTR(nfsd->nd_flag & ND_NFSV3), &error));
258 fp = nfsm_build(&info, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
259 nfsm_srvfattr(nfsd, vap, fp);
270 * nfs setattr service
273 nfsrv_setattr(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
274 struct thread *td, struct mbuf **mrq)
276 struct sockaddr *nam = nfsd->nd_nam;
277 struct ucred *cred = &nfsd->nd_cr;
278 struct vattr va, preat;
279 struct vattr *vap = &va;
280 struct nfsv2_sattr *sp;
281 struct nfs_fattr *fp;
282 struct vnode *vp = NULL;
283 struct mount *mp = NULL;
287 int error = 0, rdonly, preat_ret = 1, postat_ret = 1;
289 struct timespec guard;
290 struct nfsm_info info;
292 info.mrep = nfsd->nd_mrep;
294 info.md = nfsd->nd_md;
295 info.dpos = nfsd->nd_dpos;
296 info.v3 = (nfsd->nd_flag & ND_NFSV3);
298 guard.tv_sec = 0; /* fix compiler warning */
301 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
302 fhp = &nfh.fh_generic;
303 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
306 ERROROUT(nfsm_srvsattr(&info, vap));
307 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
308 gcheck = fxdr_unsigned(int, *tl);
310 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
311 fxdr_nfsv3time(tl, &guard);
314 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
316 * Nah nah nah nah na nah
317 * There is a bug in the Sun client that puts 0xffff in the mode
318 * field of sattr when it should put in 0xffffffff. The u_short
319 * doesn't sign extend.
320 * --> check the low order 2 bytes for 0xffff
322 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
323 vap->va_mode = nfstov_mode(sp->sa_mode);
324 if (sp->sa_uid != nfs_xdrneg1)
325 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
326 if (sp->sa_gid != nfs_xdrneg1)
327 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
328 if (sp->sa_size != nfs_xdrneg1)
329 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
330 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
332 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
334 vap->va_atime.tv_sec =
335 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
336 vap->va_atime.tv_nsec = 0;
339 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
340 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
345 * Now that we have all the fields, lets do it.
347 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
348 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
350 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
351 2 * NFSX_UNSIGNED, &error));
352 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
359 * vp now an active resource, pay careful attention to cleanup
363 error = preat_ret = VOP_GETATTR(vp, &preat);
364 if (!error && gcheck &&
365 (preat.va_ctime.tv_sec != guard.tv_sec ||
366 preat.va_ctime.tv_nsec != guard.tv_nsec))
367 error = NFSERR_NOT_SYNC;
371 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
372 NFSX_WCCDATA(info.v3), &error));
373 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
381 * If the size is being changed write acces is required, otherwise
382 * just check for a read only file system.
384 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
385 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
390 if (vp->v_type == VDIR) {
393 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
398 error = VOP_SETATTR(vp, vap, cred);
399 postat_ret = VOP_GETATTR(vp, vap);
405 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
406 NFSX_WCCORFATTR(info.v3), &error));
408 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
413 fp = nfsm_build(&info, NFSX_V2FATTR);
414 nfsm_srvfattr(nfsd, vap, fp);
429 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
430 struct thread *td, struct mbuf **mrq)
432 struct sockaddr *nam = nfsd->nd_nam;
433 struct ucred *cred = &nfsd->nd_cr;
434 struct nfs_fattr *fp;
435 struct nlookupdata nd;
441 int error = 0, len, dirattr_ret = 1;
443 struct vattr va, dirattr, *vap = &va;
444 struct nfsm_info info;
446 info.mrep = nfsd->nd_mrep;
448 info.md = nfsd->nd_md;
449 info.dpos = nfsd->nd_dpos;
450 info.v3 = (nfsd->nd_flag & ND_NFSV3);
452 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
457 fhp = &nfh.fh_generic;
458 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
459 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
461 pubflag = nfs_ispublicfh(fhp);
463 error = nfs_namei(&nd, cred, 0, NULL, &vp,
464 fhp, len, slp, nam, &info.md, &info.dpos,
465 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
468 * namei failure, only dirp to cleanup. Clear out garbarge from
469 * structure in case macros jump to nfsmout.
475 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
479 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
480 NFSX_POSTOPATTR(info.v3), &error));
481 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
487 * Locate index file for public filehandle
489 * error is 0 on entry and 0 on exit from this block.
493 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
495 * Setup call to lookup() to see if we can find
496 * the index file. Arguably, this doesn't belong
497 * in a kernel.. Ugh. If an error occurs, do not
498 * try to install an index file and then clear the
501 * When we replace nd with ind and redirect ndp,
502 * maintenance of ni_startdir and ni_vp shift to
503 * ind and we have to clean them up in the old nd.
504 * However, the cnd resource continues to be maintained
505 * via the original nd. Confused? You aren't alone!
508 cache_copy(&nd.nl_nch, &nch);
510 error = nlookup_init_raw(&nd, nfs_pub.np_index,
511 UIO_SYSSPACE, 0, cred, &nch);
514 error = nlookup(&nd);
518 * Found an index file. Get rid of
519 * the old references. transfer vp and
520 * load up the new vp. Fortunately we do
521 * not have to deal with dvp, that would be
528 error = cache_vget(&nd.nl_nch, nd.nl_cred,
530 KKASSERT(error == 0);
535 * If the public filehandle was used, check that this lookup
536 * didn't result in a filehandle outside the publicly exported
537 * filesystem. We clear the poor vp here to avoid lockups due
541 if (vp->v_mount != nfs_pub.np_mount) {
550 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
556 * Resources at this point:
557 * ndp->ni_vp may not be NULL
562 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
563 NFSX_POSTOPATTR(info.v3), &error));
564 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
570 * Clear out some resources prior to potentially blocking. This
571 * is not as critical as ni_dvp resources in other routines, but
577 * Get underlying attribute, then release remaining resources ( for
578 * the same potential blocking reason ) and reply.
580 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
581 error = VFS_VPTOFH(vp, &fhp->fh_fid);
583 error = VOP_GETATTR(vp, vap);
587 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
588 NFSX_SRVFH(info.v3) +
589 NFSX_POSTOPORFATTR(info.v3) +
590 NFSX_POSTOPATTR(info.v3),
593 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
597 nfsm_srvfhtom(&info, fhp);
599 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
600 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
602 fp = nfsm_build(&info, NFSX_V2FATTR);
603 nfsm_srvfattr(nfsd, vap, fp);
610 nlookup_done(&nd); /* may be called twice */
617 * nfs readlink service
620 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
621 struct thread *td, struct mbuf **mrq)
623 struct sockaddr *nam = nfsd->nd_nam;
624 struct ucred *cred = &nfsd->nd_cr;
625 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
626 struct iovec *ivp = iv;
628 int error = 0, rdonly, i, tlen, len, getret;
629 struct mbuf *mp1, *mp2, *mp3;
630 struct vnode *vp = NULL;
631 struct mount *mp = NULL;
635 struct uio io, *uiop = &io;
636 struct nfsm_info info;
638 info.mrep = nfsd->nd_mrep;
640 info.md = nfsd->nd_md;
641 info.dpos = nfsd->nd_dpos;
642 info.v3 = (nfsd->nd_flag & ND_NFSV3);
644 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
649 fhp = &nfh.fh_generic;
650 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
653 while (len < NFS_MAXPATHLEN) {
654 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
655 mp1->m_len = MCLBYTES;
662 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
663 mp1->m_len = NFS_MAXPATHLEN-len;
664 len = NFS_MAXPATHLEN;
667 ivp->iov_base = mtod(mp1, caddr_t);
668 ivp->iov_len = mp1->m_len;
673 uiop->uio_iovcnt = i;
674 uiop->uio_offset = 0;
675 uiop->uio_resid = len;
676 uiop->uio_rw = UIO_READ;
677 uiop->uio_segflg = UIO_SYSSPACE;
679 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
680 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
682 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
683 2 * NFSX_UNSIGNED, &error));
684 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
688 if (vp->v_type != VLNK) {
695 error = VOP_READLINK(vp, uiop, cred);
697 getret = VOP_GETATTR(vp, &attr);
700 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
701 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
704 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
710 if (uiop->uio_resid > 0) {
711 len -= uiop->uio_resid;
712 tlen = nfsm_rndup(len);
713 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
715 tl = nfsm_build(&info, NFSX_UNSIGNED);
716 *tl = txdr_unsigned(len);
717 info.mb->m_next = mp3;
732 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
733 struct thread *td, struct mbuf **mrq)
735 struct nfsm_info info;
736 struct sockaddr *nam = nfsd->nd_nam;
737 struct ucred *cred = &nfsd->nd_cr;
741 struct nfs_fattr *fp;
745 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
747 struct vnode *vp = NULL;
748 struct mount *mp = NULL;
751 struct uio io, *uiop = &io;
752 struct vattr va, *vap = &va;
757 info.mrep = nfsd->nd_mrep;
759 info.md = nfsd->nd_md;
760 info.dpos = nfsd->nd_dpos;
761 info.v3 = (nfsd->nd_flag & ND_NFSV3);
763 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
764 fhp = &nfh.fh_generic;
765 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
767 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
768 off = fxdr_hyper(tl);
770 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
771 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
773 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
774 NFS_SRVMAXDATA(nfsd), &error));
777 * Reference vp. If an error occurs, vp will be invalid, but we
778 * have to NULL it just in case. The macros might goto nfsmout
782 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
783 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
786 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
787 2 * NFSX_UNSIGNED, &error));
788 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
793 if (vp->v_type != VREG) {
797 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
800 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
801 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
803 getret = VOP_GETATTR(vp, vap);
809 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
810 NFSX_POSTOPATTR(info.v3), &error));
811 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
817 * Calculate byte count to read
820 if (off >= vap->va_size)
822 else if ((off + reqlen) > vap->va_size)
823 cnt = vap->va_size - off;
828 * Calculate seqcount for heuristic
836 * Locate best candidate
839 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
843 if (nfsheur[hi].nh_vp == vp) {
847 if (nfsheur[hi].nh_use > 0)
848 --nfsheur[hi].nh_use;
849 hi = (hi + 1) % NUM_HEURISTIC;
850 if (nfsheur[hi].nh_use < nh->nh_use)
854 if (nh->nh_vp != vp) {
857 nh->nh_use = NHUSE_INIT;
865 * Calculate heuristic
868 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
869 if (++nh->nh_seqcount > IO_SEQMAX)
870 nh->nh_seqcount = IO_SEQMAX;
871 } else if (nh->nh_seqcount > 1) {
876 nh->nh_use += NHUSE_INC;
877 if (nh->nh_use > NHUSE_MAX)
878 nh->nh_use = NHUSE_MAX;
879 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
882 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
883 NFSX_POSTOPORFATTR(info.v3) +
884 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
887 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
889 fp = (struct nfs_fattr *)tl;
890 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
892 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
893 fp = (struct nfs_fattr *)tl;
894 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
896 len = left = nfsm_rndup(cnt);
899 * Generate the mbuf list with the uio_iov ref. to it.
904 siz = min(M_TRAILINGSPACE(m), left);
910 m = m_getcl(MB_WAIT, MT_DATA, 0);
916 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
918 uiop->uio_iov = iv2 = iv;
924 panic("nfsrv_read iov");
925 siz = min(M_TRAILINGSPACE(m), left);
927 iv->iov_base = mtod(m, caddr_t) + m->m_len;
936 uiop->uio_iovcnt = i;
937 uiop->uio_offset = off;
938 uiop->uio_resid = len;
939 uiop->uio_rw = UIO_READ;
940 uiop->uio_segflg = UIO_SYSSPACE;
941 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
942 off = uiop->uio_offset;
944 FREE((caddr_t)iv2, M_TEMP);
945 if (error || (getret = VOP_GETATTR(vp, vap))) {
952 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
953 NFSX_POSTOPATTR(info.v3),
955 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
964 nfsm_srvfattr(nfsd, vap, fp);
965 tlen = len - uiop->uio_resid;
966 cnt = cnt < tlen ? cnt : tlen;
967 tlen = nfsm_rndup(cnt);
968 if (len != tlen || tlen != cnt)
969 nfsm_adj(info.mb, len - tlen, tlen - cnt);
971 *tl++ = txdr_unsigned(cnt);
977 *tl = txdr_unsigned(cnt);
989 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
990 struct thread *td, struct mbuf **mrq)
992 struct sockaddr *nam = nfsd->nd_nam;
993 struct ucred *cred = &nfsd->nd_cr;
997 struct nfs_fattr *fp;
999 struct vattr va, forat;
1000 struct vattr *vap = &va;
1002 int error = 0, rdonly, len, forat_ret = 1;
1003 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1004 int stable = NFSV3WRITE_FILESYNC;
1005 struct vnode *vp = NULL;
1006 struct mount *mp = NULL;
1009 struct uio io, *uiop = &io;
1010 struct nfsm_info info;
1013 info.mrep = nfsd->nd_mrep;
1015 info.md = nfsd->nd_md;
1016 info.dpos = nfsd->nd_dpos;
1017 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1019 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1020 if (info.mrep == NULL) {
1024 fhp = &nfh.fh_generic;
1025 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1027 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1028 off = fxdr_hyper(tl);
1030 stable = fxdr_unsigned(int, *tl++);
1032 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1033 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1036 stable = NFSV3WRITE_UNSTABLE;
1038 retlen = len = fxdr_unsigned(int32_t, *tl);
1042 * For NFS Version 2, it is not obvious what a write of zero length
1043 * should do, but I might as well be consistent with Version 3,
1044 * which is to return ok so long as there are no permission problems.
1050 if (mp1 == info.md) {
1052 adjust = info.dpos - mtod(mp1, caddr_t);
1053 mp1->m_len -= adjust;
1054 if (mp1->m_len > 0 && adjust > 0)
1055 mp1->m_data += adjust;
1059 else if (mp1->m_len > 0) {
1062 mp1->m_len -= (i - len);
1071 if (len > NFS_MAXDATA || len < 0 || i < len) {
1073 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1074 2 * NFSX_UNSIGNED, &error));
1075 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1080 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1081 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1084 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1085 2 * NFSX_UNSIGNED, &error));
1086 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1092 forat_ret = VOP_GETATTR(vp, &forat);
1093 if (vp->v_type != VREG) {
1097 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1100 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1105 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1106 NFSX_WCCDATA(info.v3), &error));
1107 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1114 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1116 uiop->uio_iov = iv = ivp;
1117 uiop->uio_iovcnt = cnt;
1120 if (mp1->m_len > 0) {
1121 ivp->iov_base = mtod(mp1, caddr_t);
1122 ivp->iov_len = mp1->m_len;
1130 * The IO_METASYNC flag indicates that all metadata (and not just
1131 * enough to ensure data integrity) mus be written to stable storage
1133 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1135 if (stable == NFSV3WRITE_UNSTABLE)
1136 ioflags = IO_NODELOCKED;
1137 else if (stable == NFSV3WRITE_DATASYNC)
1138 ioflags = (IO_SYNC | IO_NODELOCKED);
1140 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1141 uiop->uio_resid = len;
1142 uiop->uio_rw = UIO_WRITE;
1143 uiop->uio_segflg = UIO_SYSSPACE;
1144 uiop->uio_td = NULL;
1145 uiop->uio_offset = off;
1146 error = VOP_WRITE(vp, uiop, ioflags, cred);
1147 nfsstats.srvvop_writes++;
1148 FREE((caddr_t)iv, M_TEMP);
1150 aftat_ret = VOP_GETATTR(vp, vap);
1155 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1156 NFSX_PREOPATTR(info.v3) +
1157 NFSX_POSTOPORFATTR(info.v3) +
1158 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1161 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1167 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1168 *tl++ = txdr_unsigned(retlen);
1170 * If nfs_async is set, then pretend the write was FILESYNC.
1172 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1173 *tl++ = txdr_unsigned(stable);
1175 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1177 * Actually, there is no need to txdr these fields,
1178 * but it may make the values more human readable,
1179 * for debugging purposes.
1181 if (nfsver.tv_sec == 0)
1183 *tl++ = txdr_unsigned(nfsver.tv_sec);
1184 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1186 fp = nfsm_build(&info, NFSX_V2FATTR);
1187 nfsm_srvfattr(nfsd, vap, fp);
1197 * NFS write service with write gathering support. Called when
1198 * nfsrvw_procrastinate > 0.
1199 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1200 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1204 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1205 struct thread *td, struct mbuf **mrq)
1208 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1209 struct nfs_fattr *fp;
1212 struct nfsrvw_delayhash *wpp;
1214 struct vattr va, forat;
1216 int error = 0, rdonly, len, forat_ret = 1;
1217 int ioflags, aftat_ret = 1, adjust, zeroing;
1219 struct vnode *vp = NULL;
1220 struct mount *mp = NULL;
1221 struct uio io, *uiop = &io;
1223 struct nfsm_info info;
1227 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1235 info.mrep = nfsd->nd_mrep;
1237 info.md = nfsd->nd_md;
1238 info.dpos = nfsd->nd_dpos;
1239 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1240 cred = &nfsd->nd_cr;
1241 LIST_INIT(&nfsd->nd_coalesce);
1242 nfsd->nd_mreq = NULL;
1243 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1244 cur_usec = nfs_curusec();
1245 nfsd->nd_time = cur_usec +
1246 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1249 * Now, get the write header..
1251 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1253 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1254 nfsd->nd_off = fxdr_hyper(tl);
1256 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1258 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1259 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1262 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1264 len = fxdr_unsigned(int32_t, *tl);
1266 nfsd->nd_eoff = nfsd->nd_off + len;
1269 * Trim the header out of the mbuf list and trim off any trailing
1270 * junk so that the mbuf list has only the write data.
1276 if (mp1 == info.md) {
1278 adjust = info.dpos - mtod(mp1, caddr_t);
1279 mp1->m_len -= adjust;
1280 if (mp1->m_len > 0 && adjust > 0)
1281 mp1->m_data += adjust;
1288 mp1->m_len -= (i - len);
1294 if (len > NFS_MAXDATA || len < 0 || i < len) {
1299 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1301 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1304 nfsd->nd_mreq = info.mreq;
1305 nfsd->nd_mrep = NULL;
1310 * Add this entry to the hash and time queues.
1313 wp = slp->ns_tq.lh_first;
1314 while (wp && wp->nd_time < nfsd->nd_time) {
1316 wp = wp->nd_tq.le_next;
1318 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1320 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1322 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1324 if (nfsd->nd_mrep) {
1325 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1329 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1331 wp = wp->nd_hash.le_next;
1333 while (wp && wp->nd_off < nfsd->nd_off &&
1334 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1336 wp = wp->nd_hash.le_next;
1339 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1342 * Search the hash list for overlapping entries and
1345 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1346 wp = nfsd->nd_hash.le_next;
1347 if (NFSW_SAMECRED(owp, nfsd))
1348 nfsrvw_coalesce(owp, nfsd);
1351 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1357 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1358 * and generate the associated reply mbuf list(s).
1361 cur_usec = nfs_curusec();
1362 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1363 owp = nfsd->nd_tq.le_next;
1364 if (nfsd->nd_time > cur_usec)
1368 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1369 LIST_REMOVE(nfsd, nd_tq);
1370 LIST_REMOVE(nfsd, nd_hash);
1371 info.mrep = nfsd->nd_mrep;
1373 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1374 nfsd->nd_mrep = NULL;
1375 cred = &nfsd->nd_cr;
1376 forat_ret = aftat_ret = 1;
1377 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1378 nfsd->nd_nam, &rdonly,
1379 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1382 forat_ret = VOP_GETATTR(vp, &forat);
1383 if (vp->v_type != VREG) {
1387 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1393 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1396 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1397 ioflags = IO_NODELOCKED;
1398 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1399 ioflags = (IO_SYNC | IO_NODELOCKED);
1401 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1402 uiop->uio_rw = UIO_WRITE;
1403 uiop->uio_segflg = UIO_SYSSPACE;
1404 uiop->uio_td = NULL;
1405 uiop->uio_offset = nfsd->nd_off;
1406 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1407 if (uiop->uio_resid > 0) {
1415 uiop->uio_iovcnt = i;
1416 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1418 uiop->uio_iov = ivp = iov;
1421 if (mp1->m_len > 0) {
1422 ivp->iov_base = mtod(mp1, caddr_t);
1423 ivp->iov_len = mp1->m_len;
1429 error = VOP_WRITE(vp, uiop, ioflags, cred);
1430 nfsstats.srvvop_writes++;
1432 FREE((caddr_t)iov, M_TEMP);
1437 aftat_ret = VOP_GETATTR(vp, &va);
1443 * Loop around generating replies for all write rpcs that have
1444 * now been completed.
1448 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1450 nfsm_writereply(&info, nfsd, slp, error,
1451 NFSX_WCCDATA(info.v3));
1453 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1457 nfsm_writereply(&info, nfsd, slp, error,
1458 NFSX_PREOPATTR(info.v3) +
1459 NFSX_POSTOPORFATTR(info.v3) +
1461 NFSX_WRITEVERF(info.v3));
1463 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1465 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1466 *tl++ = txdr_unsigned(nfsd->nd_len);
1467 *tl++ = txdr_unsigned(swp->nd_stable);
1469 * Actually, there is no need to txdr these fields,
1470 * but it may make the values more human readable,
1471 * for debugging purposes.
1473 if (nfsver.tv_sec == 0)
1475 *tl++ = txdr_unsigned(nfsver.tv_sec);
1476 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1478 fp = nfsm_build(&info, NFSX_V2FATTR);
1479 nfsm_srvfattr(nfsd, &va, fp);
1482 nfsd->nd_mreq = info.mreq;
1484 panic("nfsrv_write: nd_mrep not free");
1487 * Done. Put it at the head of the timer queue so that
1488 * the final phase can return the reply.
1492 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1494 nfsd = swp->nd_coalesce.lh_first;
1496 LIST_REMOVE(nfsd, nd_tq);
1500 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1505 * Search for a reply to return.
1507 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next) {
1508 if (nfsd->nd_mreq) {
1509 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1510 LIST_REMOVE(nfsd, nd_tq);
1516 *mrq = nfsd->nd_mreq;
1525 * Coalesce the write request nfsd into owp. To do this we must:
1526 * - remove nfsd from the queues
1527 * - merge nfsd->nd_mrep into owp->nd_mrep
1528 * - update the nd_eoff and nd_stable for owp
1529 * - put nfsd on owp's nd_coalesce list
1530 * NB: Must be called at splsoftclock().
1533 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1537 struct nfsrv_descript *p;
1539 NFS_DPF(WG, ("C%03x-%03x",
1540 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1541 LIST_REMOVE(nfsd, nd_hash);
1542 LIST_REMOVE(nfsd, nd_tq);
1543 if (owp->nd_eoff < nfsd->nd_eoff) {
1544 overlap = owp->nd_eoff - nfsd->nd_off;
1546 panic("nfsrv_coalesce: bad off");
1548 m_adj(nfsd->nd_mrep, overlap);
1552 mp1->m_next = nfsd->nd_mrep;
1553 owp->nd_eoff = nfsd->nd_eoff;
1555 m_freem(nfsd->nd_mrep);
1556 nfsd->nd_mrep = NULL;
1557 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1558 owp->nd_stable = NFSV3WRITE_FILESYNC;
1559 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1560 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1561 owp->nd_stable = NFSV3WRITE_DATASYNC;
1562 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1565 * If nfsd had anything else coalesced into it, transfer them
1566 * to owp, otherwise their replies will never get sent.
1568 for (p = nfsd->nd_coalesce.lh_first; p;
1569 p = nfsd->nd_coalesce.lh_first) {
1570 LIST_REMOVE(p, nd_tq);
1571 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1576 * nfs create service
1577 * now does a truncate to 0 length via. setattr if it already exists
1580 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1581 struct thread *td, struct mbuf **mrq)
1583 struct sockaddr *nam = nfsd->nd_nam;
1584 struct ucred *cred = &nfsd->nd_cr;
1585 struct nfs_fattr *fp;
1586 struct vattr va, dirfor, diraft;
1587 struct vattr *vap = &va;
1588 struct nfsv2_sattr *sp;
1590 struct nlookupdata nd;
1591 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1592 udev_t rdev = NOUDEV;
1594 int how, exclusive_flag = 0;
1602 u_char cverf[NFSX_V3CREATEVERF];
1603 struct nfsm_info info;
1605 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1611 info.mrep = nfsd->nd_mrep;
1613 info.md = nfsd->nd_md;
1614 info.dpos = nfsd->nd_dpos;
1615 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1617 fhp = &nfh.fh_generic;
1618 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1619 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1622 * Call namei and do initial cleanup to get a few things
1623 * out of the way. If we get an initial error we cleanup
1624 * and return here to avoid special-casing the invalid nd
1625 * structure through the rest of the case. dirp may be
1626 * set even if an error occurs, but the nd structure will not
1627 * be valid at all if an error occurs so we have to invalidate it
1628 * prior to calling nfsm_reply ( which might goto nfsmout ).
1630 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1631 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1632 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1633 mp = vfs_getvfs(&fhp->fh_fsid);
1637 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1644 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1645 NFSX_WCCDATA(info.v3), &error));
1646 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1647 diraft_ret, &diraft);
1653 * No error. Continue. State:
1656 * vp may be valid or NULL if the target does not
1660 * The error state is set through the code and we may also do some
1661 * opportunistic releasing of vnodes to avoid holding locks through
1662 * NFS I/O. The cleanup at the end is a catch-all
1667 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1668 how = fxdr_unsigned(int, *tl);
1670 case NFSV3CREATE_GUARDED:
1676 case NFSV3CREATE_UNCHECKED:
1677 ERROROUT(nfsm_srvsattr(&info, vap));
1679 case NFSV3CREATE_EXCLUSIVE:
1680 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1681 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1685 vap->va_type = VREG;
1687 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1688 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1689 if (vap->va_type == VNON)
1690 vap->va_type = VREG;
1691 vap->va_mode = nfstov_mode(sp->sa_mode);
1692 switch (vap->va_type) {
1694 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1696 vap->va_size = (u_quad_t)tsize;
1701 rdev = fxdr_unsigned(long, sp->sa_size);
1709 * Iff doesn't exist, create it
1710 * otherwise just truncate to 0 length
1711 * should I set the mode too ?
1713 * The only possible error we can have at this point is EEXIST.
1714 * nd.ni_vp will also be non-NULL in that case.
1717 if (vap->va_mode == (mode_t)VNOVAL)
1719 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1721 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1726 if (exclusive_flag) {
1729 bcopy(cverf, (caddr_t)&vap->va_atime,
1731 error = VOP_SETATTR(vp, vap, cred);
1735 vap->va_type == VCHR ||
1736 vap->va_type == VBLK ||
1737 vap->va_type == VFIFO
1740 * Handle SysV FIFO node special cases. All other
1741 * devices require super user to access.
1743 if (vap->va_type == VCHR && rdev == 0xffffffff)
1744 vap->va_type = VFIFO;
1745 if (vap->va_type != VFIFO &&
1746 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1749 vap->va_rmajor = umajor(rdev);
1750 vap->va_rminor = uminor(rdev);
1753 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1760 * XXX what is this junk supposed to do ?
1767 * release dvp prior to lookup
1775 * Even though LOCKPARENT was cleared, ni_dvp may
1778 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1779 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1780 nd.ni_cnd.cn_td = td;
1781 nd.ni_cnd.cn_cred = cred;
1783 error = lookup(&nd);
1787 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1789 /* fall through on certain errors */
1791 nfsrv_object_create(nd.ni_vp);
1792 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1801 if (vap->va_size != -1) {
1802 error = nfsrv_access(mp, vp, VWRITE, cred,
1803 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1805 tempsize = vap->va_size;
1807 vap->va_size = tempsize;
1808 error = VOP_SETATTR(vp, vap, cred);
1814 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1815 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1817 error = VOP_GETATTR(vp, vap);
1820 if (exclusive_flag && !error &&
1821 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1823 diraft_ret = VOP_GETATTR(dirp, &diraft);
1827 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1828 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1829 NFSX_WCCDATA(info.v3),
1833 nfsm_srvpostop_fh(&info, fhp);
1834 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1836 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1837 diraft_ret, &diraft);
1840 nfsm_srvfhtom(&info, fhp);
1841 fp = nfsm_build(&info, NFSX_V2FATTR);
1842 nfsm_srvfattr(nfsd, vap, fp);
1847 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1868 * nfs v3 mknod service
1871 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1872 struct thread *td, struct mbuf **mrq)
1874 struct sockaddr *nam = nfsd->nd_nam;
1875 struct ucred *cred = &nfsd->nd_cr;
1876 struct vattr va, dirfor, diraft;
1877 struct vattr *vap = &va;
1879 struct nlookupdata nd;
1880 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1887 struct nfsm_info info;
1889 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1895 info.mrep = nfsd->nd_mrep;
1897 info.md = nfsd->nd_md;
1898 info.dpos = nfsd->nd_dpos;
1900 fhp = &nfh.fh_generic;
1901 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1902 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1905 * Handle nfs_namei() call. If an error occurs, the nd structure
1906 * is not valid. However, nfsm_*() routines may still jump to
1910 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1911 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1912 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1914 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1916 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1917 NFSX_WCCDATA(1), &error));
1918 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1919 diraft_ret, &diraft);
1923 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1924 vtyp = nfsv3tov_type(*tl);
1925 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1926 error = NFSERR_BADTYPE;
1930 ERROROUT(nfsm_srvsattr(&info, vap));
1931 if (vtyp == VCHR || vtyp == VBLK) {
1932 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1933 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1934 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1938 * Iff doesn't exist, create it.
1944 vap->va_type = vtyp;
1945 if (vap->va_mode == (mode_t)VNOVAL)
1947 if (vtyp == VSOCK) {
1949 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1953 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1957 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1965 * send response, cleanup, return.
1977 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1978 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1980 error = VOP_GETATTR(vp, vap);
1986 diraft_ret = VOP_GETATTR(dirp, &diraft);
1991 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1992 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
1993 NFSX_WCCDATA(1), &error));
1995 nfsm_srvpostop_fh(&info, fhp);
1996 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1998 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1999 diraft_ret, &diraft);
2019 * nfs remove service
2022 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2023 struct thread *td, struct mbuf **mrq)
2025 struct sockaddr *nam = nfsd->nd_nam;
2026 struct ucred *cred = &nfsd->nd_cr;
2027 struct nlookupdata nd;
2028 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2032 struct vattr dirfor, diraft;
2035 struct nfsm_info info;
2037 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2043 info.mrep = nfsd->nd_mrep;
2045 info.md = nfsd->nd_md;
2046 info.dpos = nfsd->nd_dpos;
2047 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2049 fhp = &nfh.fh_generic;
2050 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2051 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2053 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2054 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2055 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2058 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2061 if (vp->v_type == VDIR) {
2062 error = EPERM; /* POSIX */
2066 * The root of a mounted filesystem cannot be deleted.
2068 if (vp->v_flag & VROOT) {
2080 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2085 if (dirp && info.v3)
2086 diraft_ret = VOP_GETATTR(dirp, &diraft);
2087 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2089 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2090 diraft_ret, &diraft);
2110 * nfs rename service
2113 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2114 struct thread *td, struct mbuf **mrq)
2116 struct sockaddr *nam = nfsd->nd_nam;
2117 struct ucred *cred = &nfsd->nd_cr;
2118 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2119 int tdirfor_ret = 1, tdiraft_ret = 1;
2120 struct nlookupdata fromnd, tond;
2121 struct vnode *fvp, *fdirp, *fdvp;
2122 struct vnode *tvp, *tdirp, *tdvp;
2123 struct namecache *ncp;
2124 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2126 fhandle_t *ffhp, *tfhp;
2128 struct nfsm_info info;
2130 info.mrep = nfsd->nd_mrep;
2132 info.md = nfsd->nd_md;
2133 info.dpos = nfsd->nd_dpos;
2134 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2136 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2140 ffhp = &fnfh.fh_generic;
2141 tfhp = &tnfh.fh_generic;
2144 * Clear fields incase goto nfsmout occurs from macro.
2147 nlookup_zero(&fromnd);
2148 nlookup_zero(&tond);
2152 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2153 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2156 * Remember our original uid so that we can reset cr_uid before
2157 * the second nfs_namei() call, in case it is remapped.
2159 saved_uid = cred->cr_uid;
2160 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2162 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2163 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2166 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2169 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2170 2 * NFSX_WCCDATA(info.v3), &error));
2171 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2172 fdiraft_ret, &fdiraft);
2173 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2174 tdiraft_ret, &tdiraft);
2180 * We have to unlock the from ncp before we can safely lookup
2183 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2184 cache_unlock(&fromnd.nl_nch);
2185 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2186 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2187 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2188 cred->cr_uid = saved_uid;
2190 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2191 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2192 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2195 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2203 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2204 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2205 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2206 cache_lock(&fromnd.nl_nch);
2207 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2209 cache_unlock(&tond.nl_nch);
2210 cache_lock(&fromnd.nl_nch);
2211 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2212 cache_lock(&tond.nl_nch);
2213 cache_resolve(&tond.nl_nch, tond.nl_cred);
2215 fromnd.nl_flags |= NLC_NCPISLOCKED;
2217 fvp = fromnd.nl_nch.ncp->nc_vp;
2218 tvp = tond.nl_nch.ncp->nc_vp;
2221 * Set fdvp and tdvp. We haven't done all the topology checks
2222 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2223 * point). If we get through the checks these will be guarenteed
2226 * Holding the children ncp's should be sufficient to prevent
2227 * fdvp and tdvp ripouts.
2229 if (fromnd.nl_nch.ncp->nc_parent)
2230 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2233 if (tond.nl_nch.ncp->nc_parent)
2234 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2239 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2245 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2252 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2260 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2267 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2274 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2282 * You cannot rename a source into itself or a subdirectory of itself.
2283 * We check this by travsering the target directory upwards looking
2284 * for a match against the source.
2287 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2288 if (fromnd.nl_nch.ncp == ncp) {
2296 * If source is the same as the destination (that is the
2297 * same vnode with the same name in the same directory),
2298 * then there is nothing to do.
2300 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2305 * The VOP_NRENAME function releases all vnode references &
2306 * locks prior to returning so we need to clear the pointers
2307 * to bypass cleanup code later on.
2309 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2310 fdvp, tdvp, tond.nl_cred);
2319 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2321 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2322 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2323 2 * NFSX_WCCDATA(info.v3), &error));
2325 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2326 fdiraft_ret, &fdiraft);
2327 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2328 tdiraft_ret, &tdiraft);
2337 nlookup_done(&tond);
2340 nlookup_done(&fromnd);
2348 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2349 struct thread *td, struct mbuf **mrq)
2351 struct sockaddr *nam = nfsd->nd_nam;
2352 struct ucred *cred = &nfsd->nd_cr;
2353 struct nlookupdata nd;
2354 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2362 struct vattr dirfor, diraft, at;
2364 fhandle_t *fhp, *dfhp;
2365 struct nfsm_info info;
2367 info.mrep = nfsd->nd_mrep;
2369 info.md = nfsd->nd_md;
2370 info.dpos = nfsd->nd_dpos;
2371 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2373 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2375 dirp = dvp = vp = xp = NULL;
2378 fhp = &nfh.fh_generic;
2379 dfhp = &dnfh.fh_generic;
2380 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2381 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2382 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2384 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2385 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2387 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2388 NFSX_POSTOPATTR(info.v3) +
2389 NFSX_WCCDATA(info.v3),
2391 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2392 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2393 diraft_ret, &diraft);
2398 if (xp->v_type == VDIR) {
2399 error = EPERM; /* POSIX */
2403 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2404 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2405 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2408 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2417 if (xp->v_mount != dvp->v_mount)
2422 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2430 getret = VOP_GETATTR(xp, &at);
2432 diraft_ret = VOP_GETATTR(dirp, &diraft);
2433 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2434 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2437 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2438 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2439 diraft_ret, &diraft);
2463 * nfs symbolic link service
2466 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2467 struct thread *td, struct mbuf **mrq)
2469 struct sockaddr *nam = nfsd->nd_nam;
2470 struct ucred *cred = &nfsd->nd_cr;
2471 struct vattr va, dirfor, diraft;
2472 struct nlookupdata nd;
2473 struct vattr *vap = &va;
2474 struct nfsv2_sattr *sp;
2475 char *pathcp = NULL;
2478 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2484 struct nfsm_info info;
2486 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2492 info.mrep = nfsd->nd_mrep;
2494 info.md = nfsd->nd_md;
2495 info.dpos = nfsd->nd_dpos;
2496 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2498 fhp = &nfh.fh_generic;
2499 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2500 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2502 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2503 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2504 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2507 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2514 ERROROUT(nfsm_srvsattr(&info, vap));
2516 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2517 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2518 iv.iov_base = pathcp;
2520 io.uio_resid = len2;
2524 io.uio_segflg = UIO_SYSSPACE;
2525 io.uio_rw = UIO_READ;
2527 ERROROUT(nfsm_mtouio(&info, &io, len2));
2529 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2530 vap->va_mode = nfstov_mode(sp->sa_mode);
2532 *(pathcp + len2) = '\0';
2538 if (vap->va_mode == (mode_t)VNOVAL)
2542 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2546 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2547 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2549 error = VOP_GETATTR(vp, vap);
2564 FREE(pathcp, M_TEMP);
2568 diraft_ret = VOP_GETATTR(dirp, &diraft);
2572 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2573 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2574 NFSX_WCCDATA(info.v3),
2578 nfsm_srvpostop_fh(&info, fhp);
2579 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2581 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2582 diraft_ret, &diraft);
2595 FREE(pathcp, M_TEMP);
2603 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2604 struct thread *td, struct mbuf **mrq)
2606 struct sockaddr *nam = nfsd->nd_nam;
2607 struct ucred *cred = &nfsd->nd_cr;
2608 struct vattr va, dirfor, diraft;
2609 struct vattr *vap = &va;
2610 struct nfs_fattr *fp;
2611 struct nlookupdata nd;
2613 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2619 struct nfsm_info info;
2621 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2627 info.dpos = nfsd->nd_dpos;
2628 info.mrep = nfsd->nd_mrep;
2630 info.md = nfsd->nd_md;
2631 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2633 fhp = &nfh.fh_generic;
2634 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2635 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2637 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2638 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2639 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2642 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2645 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2646 NFSX_WCCDATA(info.v3), &error));
2647 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2648 diraft_ret, &diraft);
2654 ERROROUT(nfsm_srvsattr(&info, vap));
2656 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2657 vap->va_mode = nfstov_mode(*tl++);
2661 * At this point nd.ni_dvp is referenced and exclusively locked and
2662 * nd.ni_vp, if it exists, is referenced but not locked.
2665 vap->va_type = VDIR;
2672 * Issue mkdir op. Since SAVESTART is not set, the pathname
2673 * component is freed by the VOP call. This will fill-in
2674 * nd.ni_vp, reference, and exclusively lock it.
2676 if (vap->va_mode == (mode_t)VNOVAL)
2679 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2684 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2685 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2687 error = VOP_GETATTR(vp, vap);
2691 diraft_ret = VOP_GETATTR(dirp, &diraft);
2692 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2693 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2694 NFSX_WCCDATA(info.v3),
2698 nfsm_srvpostop_fh(&info, fhp);
2699 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2701 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2702 diraft_ret, &diraft);
2704 nfsm_srvfhtom(&info, fhp);
2705 fp = nfsm_build(&info, NFSX_V2FATTR);
2706 nfsm_srvfattr(nfsd, vap, fp);
2731 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2732 struct thread *td, struct mbuf **mrq)
2734 struct sockaddr *nam = nfsd->nd_nam;
2735 struct ucred *cred = &nfsd->nd_cr;
2736 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2740 struct vattr dirfor, diraft;
2743 struct nlookupdata nd;
2744 struct nfsm_info info;
2746 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2752 info.mrep = nfsd->nd_mrep;
2754 info.md = nfsd->nd_md;
2755 info.dpos = nfsd->nd_dpos;
2756 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2758 fhp = &nfh.fh_generic;
2759 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2760 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2762 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2763 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2764 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2767 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2770 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2771 NFSX_WCCDATA(info.v3), &error));
2772 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2773 diraft_ret, &diraft);
2777 if (vp->v_type != VDIR) {
2783 * The root of a mounted filesystem cannot be deleted.
2785 if (vp->v_flag & VROOT)
2789 * Issue or abort op. Since SAVESTART is not set, path name
2790 * component is freed by the VOP after either.
2797 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2804 diraft_ret = VOP_GETATTR(dirp, &diraft);
2805 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2807 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2808 diraft_ret, &diraft);
2830 * nfs readdir service
2831 * - mallocs what it thinks is enough to read
2832 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2833 * - calls VOP_READDIR()
2834 * - loops around building the reply
2835 * if the output generated exceeds count break out of loop
2836 * The nfsm_clget macro is used here so that the reply will be packed
2837 * tightly in mbuf clusters.
2838 * - it only knows that it has encountered eof when the VOP_READDIR()
2840 * - as such one readdir rpc will return eof false although you are there
2841 * and then the next will return eof
2842 * - it trims out records with d_fileno == 0
2843 * this doesn't matter for Unix clients, but they might confuse clients
2845 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2846 * than requested, but this may not apply to all filesystems. For
2847 * example, client NFS does not { although it is never remote mounted
2849 * The alternate call nfsrv_readdirplus() does lookups as well.
2850 * PS: The NFS protocol spec. does not clarify what the "count" byte
2851 * argument is a count of.. just name strings and file id's or the
2852 * entire reply rpc or ...
2853 * I tried just file name and id sizes and it confused the Sun client,
2854 * so I am using the full rpc size now. The "paranoia.." comment refers
2855 * to including the status longwords that are not a part of the dir.
2856 * "entry" structures, but are in the rpc.
2860 u_int32_t fl_postopok;
2861 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2863 u_int32_t fl_fhsize;
2864 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2868 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2869 struct thread *td, struct mbuf **mrq)
2871 struct sockaddr *nam = nfsd->nd_nam;
2872 struct ucred *cred = &nfsd->nd_cr;
2877 struct mbuf *mp1, *mp2;
2878 char *cpos, *cend, *rbuf;
2879 struct vnode *vp = NULL;
2880 struct mount *mp = NULL;
2886 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2887 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2888 u_quad_t off, toff, verf;
2889 off_t *cookies = NULL, *cookiep;
2890 struct nfsm_info info;
2892 info.mrep = nfsd->nd_mrep;
2894 info.md = nfsd->nd_md;
2895 info.dpos = nfsd->nd_dpos;
2896 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2898 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2899 fhp = &nfh.fh_generic;
2900 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2902 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2903 toff = fxdr_hyper(tl);
2905 verf = fxdr_hyper(tl);
2908 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2909 toff = fxdr_unsigned(u_quad_t, *tl++);
2910 verf = 0; /* shut up gcc */
2913 cnt = fxdr_unsigned(int, *tl);
2914 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2915 xfer = NFS_SRVMAXDATA(nfsd);
2916 if ((unsigned)cnt > xfer)
2918 if ((unsigned)siz > xfer)
2921 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2922 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2923 if (!error && vp->v_type != VDIR) {
2929 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2930 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2936 * Obtain lock on vnode for this section of the code
2940 error = getret = VOP_GETATTR(vp, &at);
2943 * XXX This check may be too strict for Solaris 2.5 clients.
2945 if (!error && toff && verf && verf != at.va_filerev)
2946 error = NFSERR_BAD_COOKIE;
2950 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2954 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2955 NFSX_POSTOPATTR(info.v3), &error));
2956 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2963 * end section. Allocate rbuf and continue
2965 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2968 iv.iov_len = fullsiz;
2971 io.uio_offset = (off_t)off;
2972 io.uio_resid = fullsiz;
2973 io.uio_segflg = UIO_SYSSPACE;
2974 io.uio_rw = UIO_READ;
2978 kfree((caddr_t)cookies, M_TEMP);
2981 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2982 off = (off_t)io.uio_offset;
2983 if (!cookies && !error)
2984 error = NFSERR_PERM;
2986 getret = VOP_GETATTR(vp, &at);
2993 kfree((caddr_t)rbuf, M_TEMP);
2995 kfree((caddr_t)cookies, M_TEMP);
2996 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2997 NFSX_POSTOPATTR(info.v3), &error));
2998 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3003 siz -= io.uio_resid;
3006 * If nothing read, return eof
3012 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3013 NFSX_POSTOPATTR(info.v3) +
3014 NFSX_COOKIEVERF(info.v3) +
3018 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3019 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3020 txdr_hyper(at.va_filerev, tl);
3023 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3026 FREE((caddr_t)rbuf, M_TEMP);
3027 FREE((caddr_t)cookies, M_TEMP);
3034 * Check for degenerate cases of nothing useful read.
3035 * If so go try again
3039 dp = (struct dirent *)cpos;
3042 * For some reason FreeBSD's ufs_readdir() chooses to back the
3043 * directory offset up to a block boundary, so it is necessary to
3044 * skip over the records that preceed the requested offset. This
3045 * requires the assumption that file offset cookies monotonically
3048 while (cpos < cend && ncookies > 0 &&
3049 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3050 ((u_quad_t)(*cookiep)) <= toff)) {
3051 dp = _DIRENT_NEXT(dp);
3056 if (cpos >= cend || ncookies == 0) {
3062 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3063 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3064 NFSX_POSTOPATTR(info.v3) +
3065 NFSX_COOKIEVERF(info.v3) + siz,
3068 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3069 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3070 txdr_hyper(at.va_filerev, tl);
3072 mp1 = mp2 = info.mb;
3074 be = bp + M_TRAILINGSPACE(mp1);
3076 /* Loop through the records and build reply */
3077 while (cpos < cend && ncookies > 0) {
3078 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3079 nlen = dp->d_namlen;
3080 rem = nfsm_rndup(nlen) - nlen;
3081 len += (4 * NFSX_UNSIGNED + nlen + rem);
3083 len += 2 * NFSX_UNSIGNED;
3089 * Build the directory record xdr from
3092 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3094 bp += NFSX_UNSIGNED;
3096 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3097 *tl = txdr_unsigned(dp->d_ino >> 32);
3098 bp += NFSX_UNSIGNED;
3100 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3101 *tl = txdr_unsigned(dp->d_ino);
3102 bp += NFSX_UNSIGNED;
3103 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3104 *tl = txdr_unsigned(nlen);
3105 bp += NFSX_UNSIGNED;
3107 /* And loop around copying the name */
3111 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3116 bcopy(cp, bp, tsiz);
3122 /* And null pad to a int32_t boundary */
3123 for (i = 0; i < rem; i++)
3125 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3127 /* Finish off the record */
3129 *tl = txdr_unsigned(*cookiep >> 32);
3130 bp += NFSX_UNSIGNED;
3131 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3133 *tl = txdr_unsigned(*cookiep);
3134 bp += NFSX_UNSIGNED;
3136 dp = _DIRENT_NEXT(dp);
3143 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3145 bp += NFSX_UNSIGNED;
3146 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3151 bp += NFSX_UNSIGNED;
3152 if (mp1 != info.mb) {
3154 mp1->m_len = bp - mtod(mp1, caddr_t);
3156 mp1->m_len += bp - info.bpos;
3157 FREE((caddr_t)rbuf, M_TEMP);
3158 FREE((caddr_t)cookies, M_TEMP);
3168 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3169 struct thread *td, struct mbuf **mrq)
3171 struct sockaddr *nam = nfsd->nd_nam;
3172 struct ucred *cred = &nfsd->nd_cr;
3177 struct mbuf *mp1, *mp2;
3178 char *cpos, *cend, *rbuf;
3179 struct vnode *vp = NULL, *nvp;
3180 struct mount *mp = NULL;
3183 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3186 struct vattr va, at, *vap = &va;
3187 struct nfs_fattr *fp;
3188 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3189 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3190 u_quad_t off, toff, verf;
3191 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3192 struct nfsm_info info;
3194 info.mrep = nfsd->nd_mrep;
3196 info.md = nfsd->nd_md;
3197 info.dpos = nfsd->nd_dpos;
3198 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3200 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3201 fhp = &nfh.fh_generic;
3202 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3203 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3204 toff = fxdr_hyper(tl);
3206 verf = fxdr_hyper(tl);
3208 siz = fxdr_unsigned(int, *tl++);
3209 cnt = fxdr_unsigned(int, *tl);
3211 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3212 xfer = NFS_SRVMAXDATA(nfsd);
3213 if ((unsigned)cnt > xfer)
3215 if ((unsigned)siz > xfer)
3218 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3219 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3220 if (!error && vp->v_type != VDIR) {
3226 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3227 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3231 error = getret = VOP_GETATTR(vp, &at);
3234 * XXX This check may be too strict for Solaris 2.5 clients.
3236 if (!error && toff && verf && verf != at.va_filerev)
3237 error = NFSERR_BAD_COOKIE;
3240 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3245 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3246 NFSX_V3POSTOPATTR, &error));
3247 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3252 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3255 iv.iov_len = fullsiz;
3258 io.uio_offset = (off_t)off;
3259 io.uio_resid = fullsiz;
3260 io.uio_segflg = UIO_SYSSPACE;
3261 io.uio_rw = UIO_READ;
3265 kfree((caddr_t)cookies, M_TEMP);
3268 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3269 off = (u_quad_t)io.uio_offset;
3270 getret = VOP_GETATTR(vp, &at);
3271 if (!cookies && !error)
3272 error = NFSERR_PERM;
3279 kfree((caddr_t)cookies, M_TEMP);
3280 kfree((caddr_t)rbuf, M_TEMP);
3281 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3282 NFSX_V3POSTOPATTR, &error));
3283 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3288 siz -= io.uio_resid;
3291 * If nothing read, return eof
3297 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3302 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3303 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3304 txdr_hyper(at.va_filerev, tl);
3308 FREE((caddr_t)cookies, M_TEMP);
3309 FREE((caddr_t)rbuf, M_TEMP);
3316 * Check for degenerate cases of nothing useful read.
3317 * If so go try again
3321 dp = (struct dirent *)cpos;
3324 * For some reason FreeBSD's ufs_readdir() chooses to back the
3325 * directory offset up to a block boundary, so it is necessary to
3326 * skip over the records that preceed the requested offset. This
3327 * requires the assumption that file offset cookies monotonically
3330 while (cpos < cend && ncookies > 0 &&
3331 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3332 ((u_quad_t)(*cookiep)) <= toff)) {
3333 dp = _DIRENT_NEXT(dp);
3338 if (cpos >= cend || ncookies == 0) {
3345 * Probe one of the directory entries to see if the filesystem
3348 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3349 error = NFSERR_NOTSUPP;
3352 kfree((caddr_t)cookies, M_TEMP);
3353 kfree((caddr_t)rbuf, M_TEMP);
3354 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3355 NFSX_V3POSTOPATTR, &error));
3356 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3365 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3367 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3368 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3369 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3370 txdr_hyper(at.va_filerev, tl);
3371 mp1 = mp2 = info.mb;
3373 be = bp + M_TRAILINGSPACE(mp1);
3375 /* Loop through the records and build reply */
3376 while (cpos < cend && ncookies > 0) {
3377 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3378 nlen = dp->d_namlen;
3379 rem = nfsm_rndup(nlen) - nlen;
3382 * For readdir_and_lookup get the vnode using
3385 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3387 bzero((caddr_t)nfhp, NFSX_V3FH);
3388 nfhp->fh_fsid = fhp->fh_fsid;
3389 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3394 if (VOP_GETATTR(nvp, vap)) {
3403 * If either the dircount or maxcount will be
3404 * exceeded, get out now. Both of these lengths
3405 * are calculated conservatively, including all
3408 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3410 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3411 if (len > cnt || dirlen > fullsiz) {
3417 * Build the directory record xdr from
3420 fp = (struct nfs_fattr *)&fl.fl_fattr;
3421 nfsm_srvfattr(nfsd, vap, fp);
3422 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3423 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3424 fl.fl_postopok = nfs_true;
3425 fl.fl_fhok = nfs_true;
3426 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3428 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3430 bp += NFSX_UNSIGNED;
3431 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3432 *tl = txdr_unsigned(dp->d_ino >> 32);
3433 bp += NFSX_UNSIGNED;
3434 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3435 *tl = txdr_unsigned(dp->d_ino);
3436 bp += NFSX_UNSIGNED;
3437 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3438 *tl = txdr_unsigned(nlen);
3439 bp += NFSX_UNSIGNED;
3441 /* And loop around copying the name */
3445 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3446 if ((bp + xfer) > be)
3450 bcopy(cp, bp, tsiz);
3455 /* And null pad to a int32_t boundary */
3456 for (i = 0; i < rem; i++)
3460 * Now copy the flrep structure out.
3462 xfer = sizeof (struct flrep);
3465 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3466 if ((bp + xfer) > be)
3470 bcopy(cp, bp, tsiz);
3477 dp = _DIRENT_NEXT(dp);
3484 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3486 bp += NFSX_UNSIGNED;
3487 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3492 bp += NFSX_UNSIGNED;
3493 if (mp1 != info.mb) {
3495 mp1->m_len = bp - mtod(mp1, caddr_t);
3497 mp1->m_len += bp - info.bpos;
3498 FREE((caddr_t)cookies, M_TEMP);
3499 FREE((caddr_t)rbuf, M_TEMP);
3508 * nfs commit service
3511 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3512 struct thread *td, struct mbuf **mrq)
3514 struct sockaddr *nam = nfsd->nd_nam;
3515 struct ucred *cred = &nfsd->nd_cr;
3516 struct vattr bfor, aft;
3517 struct vnode *vp = NULL;
3518 struct mount *mp = NULL;
3522 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3524 struct nfsm_info info;
3526 info.mrep = nfsd->nd_mrep;
3528 info.md = nfsd->nd_md;
3529 info.dpos = nfsd->nd_dpos;
3531 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3532 fhp = &nfh.fh_generic;
3533 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3534 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3537 * XXX At this time VOP_FSYNC() does not accept offset and byte
3538 * count parameters, so these arguments are useless (someday maybe).
3540 off = fxdr_hyper(tl);
3542 cnt = fxdr_unsigned(int, *tl);
3543 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3544 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3546 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3547 2 * NFSX_UNSIGNED, &error));
3548 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3553 for_ret = VOP_GETATTR(vp, &bfor);
3555 if (cnt > MAX_COMMIT_COUNT) {
3557 * Give up and do the whole thing
3560 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3561 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3563 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3566 * Locate and synchronously write any buffers that fall
3567 * into the requested range. Note: we are assuming that
3568 * f_iosize is a power of 2.
3570 int iosize = vp->v_mount->mnt_stat.f_iosize;
3571 int iomask = iosize - 1;
3575 * Align to iosize boundry, super-align to page boundry.
3578 cnt += off & iomask;
3579 off &= ~(u_quad_t)iomask;
3581 if (off & PAGE_MASK) {
3582 cnt += off & PAGE_MASK;
3583 off &= ~(u_quad_t)PAGE_MASK;
3588 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3589 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3597 * If we have a buffer and it is marked B_DELWRI we
3598 * have to lock and write it. Otherwise the prior
3599 * write is assumed to have already been committed.
3601 * WARNING: FINDBLK_TEST buffers represent stable
3602 * storage but not necessarily stable
3603 * content. It is ok in this case.
3605 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3606 if (bp->b_flags & B_DELWRI)
3607 bp = findblk(vp, loffset, 0);
3612 if (bp->b_flags & B_DELWRI) {
3629 aft_ret = VOP_GETATTR(vp, &aft);
3632 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3633 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3635 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3638 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3639 if (nfsver.tv_sec == 0)
3641 *tl++ = txdr_unsigned(nfsver.tv_sec);
3642 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3654 * nfs statfs service
3657 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3658 struct thread *td, struct mbuf **mrq)
3660 struct sockaddr *nam = nfsd->nd_nam;
3661 struct ucred *cred = &nfsd->nd_cr;
3663 struct nfs_statfs *sfp;
3664 int error = 0, rdonly, getret = 1;
3665 struct vnode *vp = NULL;
3666 struct mount *mp = NULL;
3670 struct statfs statfs;
3672 struct nfsm_info info;
3674 info.mrep = nfsd->nd_mrep;
3676 info.md = nfsd->nd_md;
3677 info.dpos = nfsd->nd_dpos;
3678 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3680 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3681 fhp = &nfh.fh_generic;
3682 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3683 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3684 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3686 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3687 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3692 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3693 getret = VOP_GETATTR(vp, &at);
3696 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3697 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3700 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3705 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3707 tval = (u_quad_t)sf->f_blocks;
3708 tval *= (u_quad_t)sf->f_bsize;
3709 txdr_hyper(tval, &sfp->sf_tbytes);
3710 tval = (u_quad_t)sf->f_bfree;
3711 tval *= (u_quad_t)sf->f_bsize;
3712 txdr_hyper(tval, &sfp->sf_fbytes);
3713 tval = (u_quad_t)sf->f_bavail;
3714 tval *= (u_quad_t)sf->f_bsize;
3715 txdr_hyper(tval, &sfp->sf_abytes);
3716 sfp->sf_tfiles.nfsuquad[0] = 0;
3717 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3718 sfp->sf_ffiles.nfsuquad[0] = 0;
3719 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3720 sfp->sf_afiles.nfsuquad[0] = 0;
3721 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3722 sfp->sf_invarsec = 0;
3724 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3725 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3726 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3727 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3728 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3738 * nfs fsinfo service
3741 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3742 struct thread *td, struct mbuf **mrq)
3744 struct sockaddr *nam = nfsd->nd_nam;
3745 struct ucred *cred = &nfsd->nd_cr;
3746 struct nfsv3_fsinfo *sip;
3747 int error = 0, rdonly, getret = 1, pref;
3748 struct vnode *vp = NULL;
3749 struct mount *mp = NULL;
3755 struct nfsm_info info;
3757 info.mrep = nfsd->nd_mrep;
3759 info.md = nfsd->nd_md;
3760 info.dpos = nfsd->nd_dpos;
3762 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3763 fhp = &nfh.fh_generic;
3764 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3765 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3766 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3768 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3769 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3774 /* XXX Try to make a guess on the max file size. */
3775 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3776 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3778 getret = VOP_GETATTR(vp, &at);
3781 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3782 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3783 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3784 sip = nfsm_build(&info, NFSX_V3FSINFO);
3788 * There should be file system VFS OP(s) to get this information.
3789 * For now, assume ufs.
3791 if (slp->ns_so->so_type == SOCK_DGRAM)
3792 pref = NFS_MAXDGRAMDATA;
3795 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3796 sip->fs_rtpref = txdr_unsigned(pref);
3797 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3798 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3799 sip->fs_wtpref = txdr_unsigned(pref);
3800 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3801 sip->fs_dtpref = txdr_unsigned(pref);
3802 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3803 sip->fs_timedelta.nfsv3_sec = 0;
3804 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3805 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3806 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3807 NFSV3FSINFO_CANSETTIME);
3816 * nfs pathconf service
3819 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3820 struct thread *td, struct mbuf **mrq)
3822 struct sockaddr *nam = nfsd->nd_nam;
3823 struct ucred *cred = &nfsd->nd_cr;
3824 struct nfsv3_pathconf *pc;
3825 int error = 0, rdonly, getret = 1;
3826 register_t linkmax, namemax, chownres, notrunc;
3827 struct vnode *vp = NULL;
3828 struct mount *mp = NULL;
3832 struct nfsm_info info;
3834 info.mrep = nfsd->nd_mrep;
3836 info.md = nfsd->nd_md;
3837 info.dpos = nfsd->nd_dpos;
3839 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3840 fhp = &nfh.fh_generic;
3841 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3842 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3843 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3845 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3846 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3850 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3852 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3854 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3856 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3857 getret = VOP_GETATTR(vp, &at);
3860 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3861 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3863 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3868 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3870 pc->pc_linkmax = txdr_unsigned(linkmax);
3871 pc->pc_namemax = txdr_unsigned(namemax);
3872 pc->pc_notrunc = txdr_unsigned(notrunc);
3873 pc->pc_chownrestricted = txdr_unsigned(chownres);
3876 * These should probably be supported by VOP_PATHCONF(), but
3877 * until msdosfs is exportable (why would you want to?), the
3878 * Unix defaults should be ok.
3880 pc->pc_caseinsensitive = nfs_false;
3881 pc->pc_casepreserving = nfs_true;
3890 * Null operation, used by clients to ping server
3894 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3895 struct thread *td, struct mbuf **mrq)
3897 struct nfsm_info info;
3898 int error = NFSERR_RETVOID;
3900 info.mrep = nfsd->nd_mrep;
3903 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3904 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3911 * No operation, used for obsolete procedures
3915 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3916 struct thread *td, struct mbuf **mrq)
3918 struct nfsm_info info;
3921 info.mrep = nfsd->nd_mrep;
3924 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3925 if (nfsd->nd_repstat)
3926 error = nfsd->nd_repstat;
3928 error = EPROCUNAVAIL;
3929 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3937 * Perform access checking for vnodes obtained from file handles that would
3938 * refer to files already opened by a Unix client. You cannot just use
3939 * vn_writechk() and VOP_ACCESS() for two reasons.
3940 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3941 * 2 - The owner is to be given access irrespective of mode bits for some
3942 * operations, so that processes that chmod after opening a file don't
3943 * break. I don't like this because it opens a security hole, but since
3944 * the nfs server opens a security hole the size of a barn door anyhow,
3947 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3948 * will return EPERM instead of EACCESS. EPERM is always an error.
3951 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3952 int rdonly, struct thread *td, int override)
3957 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3958 if (flags & VWRITE) {
3959 /* Just vn_writechk() changed to check rdonly */
3961 * Disallow write attempts on read-only file systems;
3962 * unless the file is a socket or a block or character
3963 * device resident on the file system.
3966 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3967 switch (vp->v_type) {
3977 * If there's shared text associated with
3978 * the inode, we can't allow writing.
3980 if (vp->v_flag & VTEXT)
3983 error = VOP_GETATTR(vp, &vattr);
3986 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
3988 * Allow certain operations for the owner (reads and writes
3989 * on files that are already open).
3991 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3995 #endif /* NFS_NOSERVER */