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__));
1236 info.mrep = nfsd->nd_mrep;
1238 info.md = nfsd->nd_md;
1239 info.dpos = nfsd->nd_dpos;
1240 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1241 cred = &nfsd->nd_cr;
1242 LIST_INIT(&nfsd->nd_coalesce);
1243 nfsd->nd_mreq = NULL;
1244 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1245 cur_usec = nfs_curusec();
1246 nfsd->nd_time = cur_usec +
1247 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1250 * Now, get the write header..
1252 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1254 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1255 nfsd->nd_off = fxdr_hyper(tl);
1257 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1259 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1260 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1263 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1265 len = fxdr_unsigned(int32_t, *tl);
1267 nfsd->nd_eoff = nfsd->nd_off + len;
1270 * Trim the header out of the mbuf list and trim off any trailing
1271 * junk so that the mbuf list has only the write data.
1277 if (mp1 == info.md) {
1279 adjust = info.dpos - mtod(mp1, caddr_t);
1280 mp1->m_len -= adjust;
1281 if (mp1->m_len > 0 && adjust > 0)
1282 mp1->m_data += adjust;
1289 mp1->m_len -= (i - len);
1295 if (len > NFS_MAXDATA || len < 0 || i < len) {
1300 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1302 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1305 nfsd->nd_mreq = info.mreq;
1306 nfsd->nd_mrep = NULL;
1311 * Add this entry to the hash and time queues.
1315 wp = slp->ns_tq.lh_first;
1316 while (wp && wp->nd_time < nfsd->nd_time) {
1318 wp = wp->nd_tq.le_next;
1320 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1322 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1324 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1326 if (nfsd->nd_mrep) {
1327 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1331 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1333 wp = wp->nd_hash.le_next;
1335 while (wp && wp->nd_off < nfsd->nd_off &&
1336 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1338 wp = wp->nd_hash.le_next;
1341 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1344 * Search the hash list for overlapping entries and
1347 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1348 wp = nfsd->nd_hash.le_next;
1349 if (NFSW_SAMECRED(owp, nfsd))
1350 nfsrvw_coalesce(owp, nfsd);
1353 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1360 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1361 * and generate the associated reply mbuf list(s).
1364 cur_usec = nfs_curusec();
1366 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1367 owp = nfsd->nd_tq.le_next;
1368 if (nfsd->nd_time > cur_usec)
1372 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1373 LIST_REMOVE(nfsd, nd_tq);
1374 LIST_REMOVE(nfsd, nd_hash);
1376 info.mrep = nfsd->nd_mrep;
1377 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1378 nfsd->nd_mrep = NULL;
1379 cred = &nfsd->nd_cr;
1380 forat_ret = aftat_ret = 1;
1381 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1382 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1385 forat_ret = VOP_GETATTR(vp, &forat);
1386 if (vp->v_type != VREG) {
1390 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1396 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1399 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1400 ioflags = IO_NODELOCKED;
1401 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1402 ioflags = (IO_SYNC | IO_NODELOCKED);
1404 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1405 uiop->uio_rw = UIO_WRITE;
1406 uiop->uio_segflg = UIO_SYSSPACE;
1407 uiop->uio_td = NULL;
1408 uiop->uio_offset = nfsd->nd_off;
1409 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1410 if (uiop->uio_resid > 0) {
1418 uiop->uio_iovcnt = i;
1419 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1421 uiop->uio_iov = ivp = iov;
1424 if (mp1->m_len > 0) {
1425 ivp->iov_base = mtod(mp1, caddr_t);
1426 ivp->iov_len = mp1->m_len;
1432 error = VOP_WRITE(vp, uiop, ioflags, cred);
1433 nfsstats.srvvop_writes++;
1435 FREE((caddr_t)iov, M_TEMP);
1440 aftat_ret = VOP_GETATTR(vp, &va);
1446 * Loop around generating replies for all write rpcs that have
1447 * now been completed.
1451 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1453 nfsm_writereply(&info, nfsd, slp, error,
1454 NFSX_WCCDATA(info.v3));
1456 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1460 nfsm_writereply(&info, nfsd, slp, error,
1461 NFSX_PREOPATTR(info.v3) +
1462 NFSX_POSTOPORFATTR(info.v3) +
1464 NFSX_WRITEVERF(info.v3));
1466 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1468 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1469 *tl++ = txdr_unsigned(nfsd->nd_len);
1470 *tl++ = txdr_unsigned(swp->nd_stable);
1472 * Actually, there is no need to txdr these fields,
1473 * but it may make the values more human readable,
1474 * for debugging purposes.
1476 if (nfsver.tv_sec == 0)
1478 *tl++ = txdr_unsigned(nfsver.tv_sec);
1479 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1481 fp = nfsm_build(&info, NFSX_V2FATTR);
1482 nfsm_srvfattr(nfsd, &va, fp);
1485 nfsd->nd_mreq = info.mreq;
1487 panic("nfsrv_write: nd_mrep not free");
1490 * Done. Put it at the head of the timer queue so that
1491 * the final phase can return the reply.
1496 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1498 nfsd = swp->nd_coalesce.lh_first;
1500 LIST_REMOVE(nfsd, nd_tq);
1506 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1513 * Search for a reply to return.
1516 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1517 if (nfsd->nd_mreq) {
1518 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1519 LIST_REMOVE(nfsd, nd_tq);
1520 *mrq = nfsd->nd_mreq;
1530 * Coalesce the write request nfsd into owp. To do this we must:
1531 * - remove nfsd from the queues
1532 * - merge nfsd->nd_mrep into owp->nd_mrep
1533 * - update the nd_eoff and nd_stable for owp
1534 * - put nfsd on owp's nd_coalesce list
1535 * NB: Must be called at splsoftclock().
1538 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1542 struct nfsrv_descript *p;
1544 NFS_DPF(WG, ("C%03x-%03x",
1545 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1546 LIST_REMOVE(nfsd, nd_hash);
1547 LIST_REMOVE(nfsd, nd_tq);
1548 if (owp->nd_eoff < nfsd->nd_eoff) {
1549 overlap = owp->nd_eoff - nfsd->nd_off;
1551 panic("nfsrv_coalesce: bad off");
1553 m_adj(nfsd->nd_mrep, overlap);
1557 mp1->m_next = nfsd->nd_mrep;
1558 owp->nd_eoff = nfsd->nd_eoff;
1560 m_freem(nfsd->nd_mrep);
1561 nfsd->nd_mrep = NULL;
1562 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1563 owp->nd_stable = NFSV3WRITE_FILESYNC;
1564 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1565 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1566 owp->nd_stable = NFSV3WRITE_DATASYNC;
1567 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1570 * If nfsd had anything else coalesced into it, transfer them
1571 * to owp, otherwise their replies will never get sent.
1573 for (p = nfsd->nd_coalesce.lh_first; p;
1574 p = nfsd->nd_coalesce.lh_first) {
1575 LIST_REMOVE(p, nd_tq);
1576 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1581 * nfs create service
1582 * now does a truncate to 0 length via. setattr if it already exists
1585 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1586 struct thread *td, struct mbuf **mrq)
1588 struct sockaddr *nam = nfsd->nd_nam;
1589 struct ucred *cred = &nfsd->nd_cr;
1590 struct nfs_fattr *fp;
1591 struct vattr va, dirfor, diraft;
1592 struct vattr *vap = &va;
1593 struct nfsv2_sattr *sp;
1595 struct nlookupdata nd;
1596 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1597 udev_t rdev = NOUDEV;
1599 int how, exclusive_flag = 0;
1607 u_char cverf[NFSX_V3CREATEVERF];
1608 struct nfsm_info info;
1610 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1616 info.mrep = nfsd->nd_mrep;
1618 info.md = nfsd->nd_md;
1619 info.dpos = nfsd->nd_dpos;
1620 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1622 fhp = &nfh.fh_generic;
1623 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1624 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1627 * Call namei and do initial cleanup to get a few things
1628 * out of the way. If we get an initial error we cleanup
1629 * and return here to avoid special-casing the invalid nd
1630 * structure through the rest of the case. dirp may be
1631 * set even if an error occurs, but the nd structure will not
1632 * be valid at all if an error occurs so we have to invalidate it
1633 * prior to calling nfsm_reply ( which might goto nfsmout ).
1635 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1636 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1637 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1638 mp = vfs_getvfs(&fhp->fh_fsid);
1642 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1649 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1650 NFSX_WCCDATA(info.v3), &error));
1651 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1652 diraft_ret, &diraft);
1658 * No error. Continue. State:
1661 * vp may be valid or NULL if the target does not
1665 * The error state is set through the code and we may also do some
1666 * opportunistic releasing of vnodes to avoid holding locks through
1667 * NFS I/O. The cleanup at the end is a catch-all
1672 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1673 how = fxdr_unsigned(int, *tl);
1675 case NFSV3CREATE_GUARDED:
1681 case NFSV3CREATE_UNCHECKED:
1682 ERROROUT(nfsm_srvsattr(&info, vap));
1684 case NFSV3CREATE_EXCLUSIVE:
1685 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1686 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1690 vap->va_type = VREG;
1692 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1693 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1694 if (vap->va_type == VNON)
1695 vap->va_type = VREG;
1696 vap->va_mode = nfstov_mode(sp->sa_mode);
1697 switch (vap->va_type) {
1699 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1701 vap->va_size = (u_quad_t)tsize;
1706 rdev = fxdr_unsigned(long, sp->sa_size);
1714 * Iff doesn't exist, create it
1715 * otherwise just truncate to 0 length
1716 * should I set the mode too ?
1718 * The only possible error we can have at this point is EEXIST.
1719 * nd.ni_vp will also be non-NULL in that case.
1722 if (vap->va_mode == (mode_t)VNOVAL)
1724 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1726 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1731 if (exclusive_flag) {
1734 bcopy(cverf, (caddr_t)&vap->va_atime,
1736 error = VOP_SETATTR(vp, vap, cred);
1740 vap->va_type == VCHR ||
1741 vap->va_type == VBLK ||
1742 vap->va_type == VFIFO
1745 * Handle SysV FIFO node special cases. All other
1746 * devices require super user to access.
1748 if (vap->va_type == VCHR && rdev == 0xffffffff)
1749 vap->va_type = VFIFO;
1750 if (vap->va_type != VFIFO &&
1751 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1754 vap->va_rmajor = umajor(rdev);
1755 vap->va_rminor = uminor(rdev);
1758 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1765 * XXX what is this junk supposed to do ?
1772 * release dvp prior to lookup
1780 * Even though LOCKPARENT was cleared, ni_dvp may
1783 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1784 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1785 nd.ni_cnd.cn_td = td;
1786 nd.ni_cnd.cn_cred = cred;
1788 error = lookup(&nd);
1792 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1794 /* fall through on certain errors */
1796 nfsrv_object_create(nd.ni_vp);
1797 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1806 if (vap->va_size != -1) {
1807 error = nfsrv_access(mp, vp, VWRITE, cred,
1808 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1810 tempsize = vap->va_size;
1812 vap->va_size = tempsize;
1813 error = VOP_SETATTR(vp, vap, cred);
1819 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1820 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1822 error = VOP_GETATTR(vp, vap);
1825 if (exclusive_flag && !error &&
1826 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1828 diraft_ret = VOP_GETATTR(dirp, &diraft);
1832 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1833 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1834 NFSX_WCCDATA(info.v3),
1838 nfsm_srvpostop_fh(&info, fhp);
1839 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1841 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1842 diraft_ret, &diraft);
1845 nfsm_srvfhtom(&info, fhp);
1846 fp = nfsm_build(&info, NFSX_V2FATTR);
1847 nfsm_srvfattr(nfsd, vap, fp);
1852 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1873 * nfs v3 mknod service
1876 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1877 struct thread *td, struct mbuf **mrq)
1879 struct sockaddr *nam = nfsd->nd_nam;
1880 struct ucred *cred = &nfsd->nd_cr;
1881 struct vattr va, dirfor, diraft;
1882 struct vattr *vap = &va;
1884 struct nlookupdata nd;
1885 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1892 struct nfsm_info info;
1894 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1900 info.mrep = nfsd->nd_mrep;
1902 info.md = nfsd->nd_md;
1903 info.dpos = nfsd->nd_dpos;
1905 fhp = &nfh.fh_generic;
1906 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1907 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1910 * Handle nfs_namei() call. If an error occurs, the nd structure
1911 * is not valid. However, nfsm_*() routines may still jump to
1915 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1916 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1917 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1919 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1921 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1922 NFSX_WCCDATA(1), &error));
1923 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1924 diraft_ret, &diraft);
1928 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1929 vtyp = nfsv3tov_type(*tl);
1930 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1931 error = NFSERR_BADTYPE;
1935 ERROROUT(nfsm_srvsattr(&info, vap));
1936 if (vtyp == VCHR || vtyp == VBLK) {
1937 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1938 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1939 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1943 * Iff doesn't exist, create it.
1949 vap->va_type = vtyp;
1950 if (vap->va_mode == (mode_t)VNOVAL)
1952 if (vtyp == VSOCK) {
1954 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1958 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1962 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1970 * send response, cleanup, return.
1982 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1983 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1985 error = VOP_GETATTR(vp, vap);
1991 diraft_ret = VOP_GETATTR(dirp, &diraft);
1996 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1997 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
1998 NFSX_WCCDATA(1), &error));
2000 nfsm_srvpostop_fh(&info, fhp);
2001 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2003 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2004 diraft_ret, &diraft);
2024 * nfs remove service
2027 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2028 struct thread *td, struct mbuf **mrq)
2030 struct sockaddr *nam = nfsd->nd_nam;
2031 struct ucred *cred = &nfsd->nd_cr;
2032 struct nlookupdata nd;
2033 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2037 struct vattr dirfor, diraft;
2040 struct nfsm_info info;
2042 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2048 info.mrep = nfsd->nd_mrep;
2050 info.md = nfsd->nd_md;
2051 info.dpos = nfsd->nd_dpos;
2052 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2054 fhp = &nfh.fh_generic;
2055 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2056 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2058 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2059 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2060 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2063 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2066 if (vp->v_type == VDIR) {
2067 error = EPERM; /* POSIX */
2071 * The root of a mounted filesystem cannot be deleted.
2073 if (vp->v_flag & VROOT) {
2085 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2090 if (dirp && info.v3)
2091 diraft_ret = VOP_GETATTR(dirp, &diraft);
2092 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2094 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2095 diraft_ret, &diraft);
2115 * nfs rename service
2118 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2119 struct thread *td, struct mbuf **mrq)
2121 struct sockaddr *nam = nfsd->nd_nam;
2122 struct ucred *cred = &nfsd->nd_cr;
2123 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2124 int tdirfor_ret = 1, tdiraft_ret = 1;
2125 struct nlookupdata fromnd, tond;
2126 struct vnode *fvp, *fdirp, *fdvp;
2127 struct vnode *tvp, *tdirp, *tdvp;
2128 struct namecache *ncp;
2129 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2131 fhandle_t *ffhp, *tfhp;
2133 struct nfsm_info info;
2135 info.mrep = nfsd->nd_mrep;
2137 info.md = nfsd->nd_md;
2138 info.dpos = nfsd->nd_dpos;
2139 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2141 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2145 ffhp = &fnfh.fh_generic;
2146 tfhp = &tnfh.fh_generic;
2149 * Clear fields incase goto nfsmout occurs from macro.
2152 nlookup_zero(&fromnd);
2153 nlookup_zero(&tond);
2157 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2158 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2161 * Remember our original uid so that we can reset cr_uid before
2162 * the second nfs_namei() call, in case it is remapped.
2164 saved_uid = cred->cr_uid;
2165 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2167 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2168 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2171 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2174 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2175 2 * NFSX_WCCDATA(info.v3), &error));
2176 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2177 fdiraft_ret, &fdiraft);
2178 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2179 tdiraft_ret, &tdiraft);
2185 * We have to unlock the from ncp before we can safely lookup
2188 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2189 cache_unlock(&fromnd.nl_nch);
2190 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2191 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2192 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2193 cred->cr_uid = saved_uid;
2195 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2196 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2197 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2200 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2208 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2209 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2210 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2211 cache_lock(&fromnd.nl_nch);
2212 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2214 cache_unlock(&tond.nl_nch);
2215 cache_lock(&fromnd.nl_nch);
2216 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2217 cache_lock(&tond.nl_nch);
2218 cache_resolve(&tond.nl_nch, tond.nl_cred);
2220 fromnd.nl_flags |= NLC_NCPISLOCKED;
2222 fvp = fromnd.nl_nch.ncp->nc_vp;
2223 tvp = tond.nl_nch.ncp->nc_vp;
2226 * Set fdvp and tdvp. We haven't done all the topology checks
2227 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2228 * point). If we get through the checks these will be guarenteed
2231 * Holding the children ncp's should be sufficient to prevent
2232 * fdvp and tdvp ripouts.
2234 if (fromnd.nl_nch.ncp->nc_parent)
2235 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2238 if (tond.nl_nch.ncp->nc_parent)
2239 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2244 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2250 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2257 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2265 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2272 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2279 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2287 * You cannot rename a source into itself or a subdirectory of itself.
2288 * We check this by travsering the target directory upwards looking
2289 * for a match against the source.
2292 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2293 if (fromnd.nl_nch.ncp == ncp) {
2301 * If source is the same as the destination (that is the
2302 * same vnode with the same name in the same directory),
2303 * then there is nothing to do.
2305 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2310 * The VOP_NRENAME function releases all vnode references &
2311 * locks prior to returning so we need to clear the pointers
2312 * to bypass cleanup code later on.
2314 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2315 fdvp, tdvp, tond.nl_cred);
2324 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2326 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2327 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2328 2 * NFSX_WCCDATA(info.v3), &error));
2330 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2331 fdiraft_ret, &fdiraft);
2332 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2333 tdiraft_ret, &tdiraft);
2342 nlookup_done(&tond);
2345 nlookup_done(&fromnd);
2353 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2354 struct thread *td, struct mbuf **mrq)
2356 struct sockaddr *nam = nfsd->nd_nam;
2357 struct ucred *cred = &nfsd->nd_cr;
2358 struct nlookupdata nd;
2359 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2367 struct vattr dirfor, diraft, at;
2369 fhandle_t *fhp, *dfhp;
2370 struct nfsm_info info;
2372 info.mrep = nfsd->nd_mrep;
2374 info.md = nfsd->nd_md;
2375 info.dpos = nfsd->nd_dpos;
2376 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2378 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2380 dirp = dvp = vp = xp = NULL;
2383 fhp = &nfh.fh_generic;
2384 dfhp = &dnfh.fh_generic;
2385 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2386 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2387 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2389 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2390 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2392 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2393 NFSX_POSTOPATTR(info.v3) +
2394 NFSX_WCCDATA(info.v3),
2396 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2397 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2398 diraft_ret, &diraft);
2403 if (xp->v_type == VDIR) {
2404 error = EPERM; /* POSIX */
2408 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2409 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2410 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2413 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2422 if (xp->v_mount != dvp->v_mount)
2427 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2435 getret = VOP_GETATTR(xp, &at);
2437 diraft_ret = VOP_GETATTR(dirp, &diraft);
2438 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2439 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2442 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2443 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2444 diraft_ret, &diraft);
2468 * nfs symbolic link service
2471 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2472 struct thread *td, struct mbuf **mrq)
2474 struct sockaddr *nam = nfsd->nd_nam;
2475 struct ucred *cred = &nfsd->nd_cr;
2476 struct vattr va, dirfor, diraft;
2477 struct nlookupdata nd;
2478 struct vattr *vap = &va;
2479 struct nfsv2_sattr *sp;
2480 char *pathcp = NULL;
2483 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2489 struct nfsm_info info;
2491 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2497 info.mrep = nfsd->nd_mrep;
2499 info.md = nfsd->nd_md;
2500 info.dpos = nfsd->nd_dpos;
2501 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2503 fhp = &nfh.fh_generic;
2504 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2505 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2507 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2508 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2509 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2512 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2519 ERROROUT(nfsm_srvsattr(&info, vap));
2521 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2522 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2523 iv.iov_base = pathcp;
2525 io.uio_resid = len2;
2529 io.uio_segflg = UIO_SYSSPACE;
2530 io.uio_rw = UIO_READ;
2532 ERROROUT(nfsm_mtouio(&info, &io, len2));
2534 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2535 vap->va_mode = nfstov_mode(sp->sa_mode);
2537 *(pathcp + len2) = '\0';
2543 if (vap->va_mode == (mode_t)VNOVAL)
2547 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2551 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2552 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2554 error = VOP_GETATTR(vp, vap);
2569 FREE(pathcp, M_TEMP);
2573 diraft_ret = VOP_GETATTR(dirp, &diraft);
2577 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2578 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2579 NFSX_WCCDATA(info.v3),
2583 nfsm_srvpostop_fh(&info, fhp);
2584 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2586 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2587 diraft_ret, &diraft);
2600 FREE(pathcp, M_TEMP);
2608 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2609 struct thread *td, struct mbuf **mrq)
2611 struct sockaddr *nam = nfsd->nd_nam;
2612 struct ucred *cred = &nfsd->nd_cr;
2613 struct vattr va, dirfor, diraft;
2614 struct vattr *vap = &va;
2615 struct nfs_fattr *fp;
2616 struct nlookupdata nd;
2618 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2624 struct nfsm_info info;
2626 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2632 info.dpos = nfsd->nd_dpos;
2633 info.mrep = nfsd->nd_mrep;
2635 info.md = nfsd->nd_md;
2636 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2638 fhp = &nfh.fh_generic;
2639 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2640 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2642 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2643 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2644 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2647 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2650 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2651 NFSX_WCCDATA(info.v3), &error));
2652 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2653 diraft_ret, &diraft);
2659 ERROROUT(nfsm_srvsattr(&info, vap));
2661 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2662 vap->va_mode = nfstov_mode(*tl++);
2666 * At this point nd.ni_dvp is referenced and exclusively locked and
2667 * nd.ni_vp, if it exists, is referenced but not locked.
2670 vap->va_type = VDIR;
2677 * Issue mkdir op. Since SAVESTART is not set, the pathname
2678 * component is freed by the VOP call. This will fill-in
2679 * nd.ni_vp, reference, and exclusively lock it.
2681 if (vap->va_mode == (mode_t)VNOVAL)
2684 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2689 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2690 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2692 error = VOP_GETATTR(vp, vap);
2696 diraft_ret = VOP_GETATTR(dirp, &diraft);
2697 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2698 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2699 NFSX_WCCDATA(info.v3),
2703 nfsm_srvpostop_fh(&info, fhp);
2704 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2706 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2707 diraft_ret, &diraft);
2709 nfsm_srvfhtom(&info, fhp);
2710 fp = nfsm_build(&info, NFSX_V2FATTR);
2711 nfsm_srvfattr(nfsd, vap, fp);
2736 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2737 struct thread *td, struct mbuf **mrq)
2739 struct sockaddr *nam = nfsd->nd_nam;
2740 struct ucred *cred = &nfsd->nd_cr;
2741 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2745 struct vattr dirfor, diraft;
2748 struct nlookupdata nd;
2749 struct nfsm_info info;
2751 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2757 info.mrep = nfsd->nd_mrep;
2759 info.md = nfsd->nd_md;
2760 info.dpos = nfsd->nd_dpos;
2761 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2763 fhp = &nfh.fh_generic;
2764 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2765 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2767 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2768 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2769 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2772 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2775 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2776 NFSX_WCCDATA(info.v3), &error));
2777 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2778 diraft_ret, &diraft);
2782 if (vp->v_type != VDIR) {
2788 * The root of a mounted filesystem cannot be deleted.
2790 if (vp->v_flag & VROOT)
2794 * Issue or abort op. Since SAVESTART is not set, path name
2795 * component is freed by the VOP after either.
2802 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2809 diraft_ret = VOP_GETATTR(dirp, &diraft);
2810 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2812 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2813 diraft_ret, &diraft);
2835 * nfs readdir service
2836 * - mallocs what it thinks is enough to read
2837 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2838 * - calls VOP_READDIR()
2839 * - loops around building the reply
2840 * if the output generated exceeds count break out of loop
2841 * The nfsm_clget macro is used here so that the reply will be packed
2842 * tightly in mbuf clusters.
2843 * - it only knows that it has encountered eof when the VOP_READDIR()
2845 * - as such one readdir rpc will return eof false although you are there
2846 * and then the next will return eof
2847 * - it trims out records with d_fileno == 0
2848 * this doesn't matter for Unix clients, but they might confuse clients
2850 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2851 * than requested, but this may not apply to all filesystems. For
2852 * example, client NFS does not { although it is never remote mounted
2854 * The alternate call nfsrv_readdirplus() does lookups as well.
2855 * PS: The NFS protocol spec. does not clarify what the "count" byte
2856 * argument is a count of.. just name strings and file id's or the
2857 * entire reply rpc or ...
2858 * I tried just file name and id sizes and it confused the Sun client,
2859 * so I am using the full rpc size now. The "paranoia.." comment refers
2860 * to including the status longwords that are not a part of the dir.
2861 * "entry" structures, but are in the rpc.
2865 u_int32_t fl_postopok;
2866 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2868 u_int32_t fl_fhsize;
2869 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2873 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2874 struct thread *td, struct mbuf **mrq)
2876 struct sockaddr *nam = nfsd->nd_nam;
2877 struct ucred *cred = &nfsd->nd_cr;
2882 struct mbuf *mp1, *mp2;
2883 char *cpos, *cend, *rbuf;
2884 struct vnode *vp = NULL;
2885 struct mount *mp = NULL;
2891 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2892 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2893 u_quad_t off, toff, verf;
2894 off_t *cookies = NULL, *cookiep;
2895 struct nfsm_info info;
2897 info.mrep = nfsd->nd_mrep;
2899 info.md = nfsd->nd_md;
2900 info.dpos = nfsd->nd_dpos;
2901 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2903 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2904 fhp = &nfh.fh_generic;
2905 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2907 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2908 toff = fxdr_hyper(tl);
2910 verf = fxdr_hyper(tl);
2913 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2914 toff = fxdr_unsigned(u_quad_t, *tl++);
2915 verf = 0; /* shut up gcc */
2918 cnt = fxdr_unsigned(int, *tl);
2919 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2920 xfer = NFS_SRVMAXDATA(nfsd);
2921 if ((unsigned)cnt > xfer)
2923 if ((unsigned)siz > xfer)
2926 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2927 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2928 if (!error && vp->v_type != VDIR) {
2934 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2935 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2941 * Obtain lock on vnode for this section of the code
2945 error = getret = VOP_GETATTR(vp, &at);
2948 * XXX This check may be too strict for Solaris 2.5 clients.
2950 if (!error && toff && verf && verf != at.va_filerev)
2951 error = NFSERR_BAD_COOKIE;
2955 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2959 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2960 NFSX_POSTOPATTR(info.v3), &error));
2961 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2968 * end section. Allocate rbuf and continue
2970 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2973 iv.iov_len = fullsiz;
2976 io.uio_offset = (off_t)off;
2977 io.uio_resid = fullsiz;
2978 io.uio_segflg = UIO_SYSSPACE;
2979 io.uio_rw = UIO_READ;
2983 kfree((caddr_t)cookies, M_TEMP);
2986 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2987 off = (off_t)io.uio_offset;
2988 if (!cookies && !error)
2989 error = NFSERR_PERM;
2991 getret = VOP_GETATTR(vp, &at);
2998 kfree((caddr_t)rbuf, M_TEMP);
3000 kfree((caddr_t)cookies, M_TEMP);
3001 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3002 NFSX_POSTOPATTR(info.v3), &error));
3003 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3008 siz -= io.uio_resid;
3011 * If nothing read, return eof
3017 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3018 NFSX_POSTOPATTR(info.v3) +
3019 NFSX_COOKIEVERF(info.v3) +
3023 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3024 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3025 txdr_hyper(at.va_filerev, tl);
3028 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3031 FREE((caddr_t)rbuf, M_TEMP);
3032 FREE((caddr_t)cookies, M_TEMP);
3039 * Check for degenerate cases of nothing useful read.
3040 * If so go try again
3044 dp = (struct dirent *)cpos;
3047 * For some reason FreeBSD's ufs_readdir() chooses to back the
3048 * directory offset up to a block boundary, so it is necessary to
3049 * skip over the records that preceed the requested offset. This
3050 * requires the assumption that file offset cookies monotonically
3053 while (cpos < cend && ncookies > 0 &&
3054 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3055 ((u_quad_t)(*cookiep)) <= toff)) {
3056 dp = _DIRENT_NEXT(dp);
3061 if (cpos >= cend || ncookies == 0) {
3067 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3068 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3069 NFSX_POSTOPATTR(info.v3) +
3070 NFSX_COOKIEVERF(info.v3) + siz,
3073 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3074 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3075 txdr_hyper(at.va_filerev, tl);
3077 mp1 = mp2 = info.mb;
3079 be = bp + M_TRAILINGSPACE(mp1);
3081 /* Loop through the records and build reply */
3082 while (cpos < cend && ncookies > 0) {
3083 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3084 nlen = dp->d_namlen;
3085 rem = nfsm_rndup(nlen) - nlen;
3086 len += (4 * NFSX_UNSIGNED + nlen + rem);
3088 len += 2 * NFSX_UNSIGNED;
3094 * Build the directory record xdr from
3097 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3099 bp += NFSX_UNSIGNED;
3101 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3102 *tl = txdr_unsigned(dp->d_ino >> 32);
3103 bp += NFSX_UNSIGNED;
3105 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3106 *tl = txdr_unsigned(dp->d_ino);
3107 bp += NFSX_UNSIGNED;
3108 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3109 *tl = txdr_unsigned(nlen);
3110 bp += NFSX_UNSIGNED;
3112 /* And loop around copying the name */
3116 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3121 bcopy(cp, bp, tsiz);
3127 /* And null pad to a int32_t boundary */
3128 for (i = 0; i < rem; i++)
3130 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3132 /* Finish off the record */
3134 *tl = txdr_unsigned(*cookiep >> 32);
3135 bp += NFSX_UNSIGNED;
3136 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3138 *tl = txdr_unsigned(*cookiep);
3139 bp += NFSX_UNSIGNED;
3141 dp = _DIRENT_NEXT(dp);
3148 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3150 bp += NFSX_UNSIGNED;
3151 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3156 bp += NFSX_UNSIGNED;
3157 if (mp1 != info.mb) {
3159 mp1->m_len = bp - mtod(mp1, caddr_t);
3161 mp1->m_len += bp - info.bpos;
3162 FREE((caddr_t)rbuf, M_TEMP);
3163 FREE((caddr_t)cookies, M_TEMP);
3173 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3174 struct thread *td, struct mbuf **mrq)
3176 struct sockaddr *nam = nfsd->nd_nam;
3177 struct ucred *cred = &nfsd->nd_cr;
3182 struct mbuf *mp1, *mp2;
3183 char *cpos, *cend, *rbuf;
3184 struct vnode *vp = NULL, *nvp;
3185 struct mount *mp = NULL;
3188 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3191 struct vattr va, at, *vap = &va;
3192 struct nfs_fattr *fp;
3193 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3194 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3195 u_quad_t off, toff, verf;
3196 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3197 struct nfsm_info info;
3199 info.mrep = nfsd->nd_mrep;
3201 info.md = nfsd->nd_md;
3202 info.dpos = nfsd->nd_dpos;
3203 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3205 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3206 fhp = &nfh.fh_generic;
3207 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3208 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3209 toff = fxdr_hyper(tl);
3211 verf = fxdr_hyper(tl);
3213 siz = fxdr_unsigned(int, *tl++);
3214 cnt = fxdr_unsigned(int, *tl);
3216 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3217 xfer = NFS_SRVMAXDATA(nfsd);
3218 if ((unsigned)cnt > xfer)
3220 if ((unsigned)siz > xfer)
3223 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3224 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3225 if (!error && vp->v_type != VDIR) {
3231 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3232 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3236 error = getret = VOP_GETATTR(vp, &at);
3239 * XXX This check may be too strict for Solaris 2.5 clients.
3241 if (!error && toff && verf && verf != at.va_filerev)
3242 error = NFSERR_BAD_COOKIE;
3245 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3250 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3251 NFSX_V3POSTOPATTR, &error));
3252 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3257 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3260 iv.iov_len = fullsiz;
3263 io.uio_offset = (off_t)off;
3264 io.uio_resid = fullsiz;
3265 io.uio_segflg = UIO_SYSSPACE;
3266 io.uio_rw = UIO_READ;
3270 kfree((caddr_t)cookies, M_TEMP);
3273 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3274 off = (u_quad_t)io.uio_offset;
3275 getret = VOP_GETATTR(vp, &at);
3276 if (!cookies && !error)
3277 error = NFSERR_PERM;
3284 kfree((caddr_t)cookies, M_TEMP);
3285 kfree((caddr_t)rbuf, M_TEMP);
3286 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3287 NFSX_V3POSTOPATTR, &error));
3288 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3293 siz -= io.uio_resid;
3296 * If nothing read, return eof
3302 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3307 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3308 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3309 txdr_hyper(at.va_filerev, tl);
3313 FREE((caddr_t)cookies, M_TEMP);
3314 FREE((caddr_t)rbuf, M_TEMP);
3321 * Check for degenerate cases of nothing useful read.
3322 * If so go try again
3326 dp = (struct dirent *)cpos;
3329 * For some reason FreeBSD's ufs_readdir() chooses to back the
3330 * directory offset up to a block boundary, so it is necessary to
3331 * skip over the records that preceed the requested offset. This
3332 * requires the assumption that file offset cookies monotonically
3335 while (cpos < cend && ncookies > 0 &&
3336 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3337 ((u_quad_t)(*cookiep)) <= toff)) {
3338 dp = _DIRENT_NEXT(dp);
3343 if (cpos >= cend || ncookies == 0) {
3350 * Probe one of the directory entries to see if the filesystem
3353 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp) == EOPNOTSUPP) {
3354 error = NFSERR_NOTSUPP;
3357 kfree((caddr_t)cookies, M_TEMP);
3358 kfree((caddr_t)rbuf, M_TEMP);
3359 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3360 NFSX_V3POSTOPATTR, &error));
3361 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3370 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3372 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3373 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3374 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3375 txdr_hyper(at.va_filerev, tl);
3376 mp1 = mp2 = info.mb;
3378 be = bp + M_TRAILINGSPACE(mp1);
3380 /* Loop through the records and build reply */
3381 while (cpos < cend && ncookies > 0) {
3382 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3383 nlen = dp->d_namlen;
3384 rem = nfsm_rndup(nlen) - nlen;
3387 * For readdir_and_lookup get the vnode using
3390 if (VFS_VGET(vp->v_mount, vp, dp->d_ino, &nvp))
3392 bzero((caddr_t)nfhp, NFSX_V3FH);
3393 nfhp->fh_fsid = fhp->fh_fsid;
3394 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3399 if (VOP_GETATTR(nvp, vap)) {
3408 * If either the dircount or maxcount will be
3409 * exceeded, get out now. Both of these lengths
3410 * are calculated conservatively, including all
3413 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3415 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3416 if (len > cnt || dirlen > fullsiz) {
3422 * Build the directory record xdr from
3425 fp = (struct nfs_fattr *)&fl.fl_fattr;
3426 nfsm_srvfattr(nfsd, vap, fp);
3427 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3428 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3429 fl.fl_postopok = nfs_true;
3430 fl.fl_fhok = nfs_true;
3431 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3433 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3435 bp += NFSX_UNSIGNED;
3436 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3437 *tl = txdr_unsigned(dp->d_ino >> 32);
3438 bp += NFSX_UNSIGNED;
3439 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3440 *tl = txdr_unsigned(dp->d_ino);
3441 bp += NFSX_UNSIGNED;
3442 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3443 *tl = txdr_unsigned(nlen);
3444 bp += NFSX_UNSIGNED;
3446 /* And loop around copying the name */
3450 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3451 if ((bp + xfer) > be)
3455 bcopy(cp, bp, tsiz);
3460 /* And null pad to a int32_t boundary */
3461 for (i = 0; i < rem; i++)
3465 * Now copy the flrep structure out.
3467 xfer = sizeof (struct flrep);
3470 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3471 if ((bp + xfer) > be)
3475 bcopy(cp, bp, tsiz);
3482 dp = _DIRENT_NEXT(dp);
3489 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3491 bp += NFSX_UNSIGNED;
3492 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3497 bp += NFSX_UNSIGNED;
3498 if (mp1 != info.mb) {
3500 mp1->m_len = bp - mtod(mp1, caddr_t);
3502 mp1->m_len += bp - info.bpos;
3503 FREE((caddr_t)cookies, M_TEMP);
3504 FREE((caddr_t)rbuf, M_TEMP);
3513 * nfs commit service
3516 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3517 struct thread *td, struct mbuf **mrq)
3519 struct sockaddr *nam = nfsd->nd_nam;
3520 struct ucred *cred = &nfsd->nd_cr;
3521 struct vattr bfor, aft;
3522 struct vnode *vp = NULL;
3523 struct mount *mp = NULL;
3527 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3529 struct nfsm_info info;
3531 info.mrep = nfsd->nd_mrep;
3533 info.md = nfsd->nd_md;
3534 info.dpos = nfsd->nd_dpos;
3536 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3537 fhp = &nfh.fh_generic;
3538 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3539 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3542 * XXX At this time VOP_FSYNC() does not accept offset and byte
3543 * count parameters, so these arguments are useless (someday maybe).
3545 off = fxdr_hyper(tl);
3547 cnt = fxdr_unsigned(int, *tl);
3548 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3549 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3551 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3552 2 * NFSX_UNSIGNED, &error));
3553 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3558 for_ret = VOP_GETATTR(vp, &bfor);
3560 if (cnt > MAX_COMMIT_COUNT) {
3562 * Give up and do the whole thing
3565 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3566 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3568 error = VOP_FSYNC(vp, MNT_WAIT, 0);
3571 * Locate and synchronously write any buffers that fall
3572 * into the requested range. Note: we are assuming that
3573 * f_iosize is a power of 2.
3575 int iosize = vp->v_mount->mnt_stat.f_iosize;
3576 int iomask = iosize - 1;
3580 * Align to iosize boundry, super-align to page boundry.
3583 cnt += off & iomask;
3584 off &= ~(u_quad_t)iomask;
3586 if (off & PAGE_MASK) {
3587 cnt += off & PAGE_MASK;
3588 off &= ~(u_quad_t)PAGE_MASK;
3593 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3594 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3602 * If we have a buffer and it is marked B_DELWRI we
3603 * have to lock and write it. Otherwise the prior
3604 * write is assumed to have already been committed.
3606 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3607 if (bp->b_flags & B_DELWRI)
3608 bp = findblk(vp, loffset, 0);
3613 if (bp->b_flags & B_DELWRI) {
3630 aft_ret = VOP_GETATTR(vp, &aft);
3633 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3634 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3636 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3639 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3640 if (nfsver.tv_sec == 0)
3642 *tl++ = txdr_unsigned(nfsver.tv_sec);
3643 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3655 * nfs statfs service
3658 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3659 struct thread *td, struct mbuf **mrq)
3661 struct sockaddr *nam = nfsd->nd_nam;
3662 struct ucred *cred = &nfsd->nd_cr;
3664 struct nfs_statfs *sfp;
3665 int error = 0, rdonly, getret = 1;
3666 struct vnode *vp = NULL;
3667 struct mount *mp = NULL;
3671 struct statfs statfs;
3673 struct nfsm_info info;
3675 info.mrep = nfsd->nd_mrep;
3677 info.md = nfsd->nd_md;
3678 info.dpos = nfsd->nd_dpos;
3679 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3681 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3682 fhp = &nfh.fh_generic;
3683 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3684 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3685 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3687 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3688 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3693 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3694 getret = VOP_GETATTR(vp, &at);
3697 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3698 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3701 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3706 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3708 tval = (u_quad_t)sf->f_blocks;
3709 tval *= (u_quad_t)sf->f_bsize;
3710 txdr_hyper(tval, &sfp->sf_tbytes);
3711 tval = (u_quad_t)sf->f_bfree;
3712 tval *= (u_quad_t)sf->f_bsize;
3713 txdr_hyper(tval, &sfp->sf_fbytes);
3714 tval = (u_quad_t)sf->f_bavail;
3715 tval *= (u_quad_t)sf->f_bsize;
3716 txdr_hyper(tval, &sfp->sf_abytes);
3717 sfp->sf_tfiles.nfsuquad[0] = 0;
3718 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3719 sfp->sf_ffiles.nfsuquad[0] = 0;
3720 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3721 sfp->sf_afiles.nfsuquad[0] = 0;
3722 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3723 sfp->sf_invarsec = 0;
3725 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3726 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3727 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3728 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3729 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3739 * nfs fsinfo service
3742 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3743 struct thread *td, struct mbuf **mrq)
3745 struct sockaddr *nam = nfsd->nd_nam;
3746 struct ucred *cred = &nfsd->nd_cr;
3747 struct nfsv3_fsinfo *sip;
3748 int error = 0, rdonly, getret = 1, pref;
3749 struct vnode *vp = NULL;
3750 struct mount *mp = NULL;
3756 struct nfsm_info info;
3758 info.mrep = nfsd->nd_mrep;
3760 info.md = nfsd->nd_md;
3761 info.dpos = nfsd->nd_dpos;
3763 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3764 fhp = &nfh.fh_generic;
3765 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3766 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3767 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3769 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3770 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3775 /* XXX Try to make a guess on the max file size. */
3776 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3777 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3779 getret = VOP_GETATTR(vp, &at);
3782 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3783 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3784 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3785 sip = nfsm_build(&info, NFSX_V3FSINFO);
3789 * There should be file system VFS OP(s) to get this information.
3790 * For now, assume ufs.
3792 if (slp->ns_so->so_type == SOCK_DGRAM)
3793 pref = NFS_MAXDGRAMDATA;
3796 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3797 sip->fs_rtpref = txdr_unsigned(pref);
3798 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3799 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3800 sip->fs_wtpref = txdr_unsigned(pref);
3801 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3802 sip->fs_dtpref = txdr_unsigned(pref);
3803 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3804 sip->fs_timedelta.nfsv3_sec = 0;
3805 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3806 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3807 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3808 NFSV3FSINFO_CANSETTIME);
3817 * nfs pathconf service
3820 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3821 struct thread *td, struct mbuf **mrq)
3823 struct sockaddr *nam = nfsd->nd_nam;
3824 struct ucred *cred = &nfsd->nd_cr;
3825 struct nfsv3_pathconf *pc;
3826 int error = 0, rdonly, getret = 1;
3827 register_t linkmax, namemax, chownres, notrunc;
3828 struct vnode *vp = NULL;
3829 struct mount *mp = NULL;
3833 struct nfsm_info info;
3835 info.mrep = nfsd->nd_mrep;
3837 info.md = nfsd->nd_md;
3838 info.dpos = nfsd->nd_dpos;
3840 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3841 fhp = &nfh.fh_generic;
3842 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3843 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3844 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3846 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3847 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3851 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3853 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3855 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3857 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3858 getret = VOP_GETATTR(vp, &at);
3861 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3862 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3864 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3869 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3871 pc->pc_linkmax = txdr_unsigned(linkmax);
3872 pc->pc_namemax = txdr_unsigned(namemax);
3873 pc->pc_notrunc = txdr_unsigned(notrunc);
3874 pc->pc_chownrestricted = txdr_unsigned(chownres);
3877 * These should probably be supported by VOP_PATHCONF(), but
3878 * until msdosfs is exportable (why would you want to?), the
3879 * Unix defaults should be ok.
3881 pc->pc_caseinsensitive = nfs_false;
3882 pc->pc_casepreserving = nfs_true;
3891 * Null operation, used by clients to ping server
3895 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3896 struct thread *td, struct mbuf **mrq)
3898 struct nfsm_info info;
3899 int error = NFSERR_RETVOID;
3901 info.mrep = nfsd->nd_mrep;
3904 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3905 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3912 * No operation, used for obsolete procedures
3916 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3917 struct thread *td, struct mbuf **mrq)
3919 struct nfsm_info info;
3922 info.mrep = nfsd->nd_mrep;
3925 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3926 if (nfsd->nd_repstat)
3927 error = nfsd->nd_repstat;
3929 error = EPROCUNAVAIL;
3930 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3938 * Perform access checking for vnodes obtained from file handles that would
3939 * refer to files already opened by a Unix client. You cannot just use
3940 * vn_writechk() and VOP_ACCESS() for two reasons.
3941 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3942 * 2 - The owner is to be given access irrespective of mode bits for some
3943 * operations, so that processes that chmod after opening a file don't
3944 * break. I don't like this because it opens a security hole, but since
3945 * the nfs server opens a security hole the size of a barn door anyhow,
3948 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3949 * will return EPERM instead of EACCESS. EPERM is always an error.
3952 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3953 int rdonly, struct thread *td, int override)
3958 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3959 if (flags & VWRITE) {
3960 /* Just vn_writechk() changed to check rdonly */
3962 * Disallow write attempts on read-only file systems;
3963 * unless the file is a socket or a block or character
3964 * device resident on the file system.
3967 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3968 switch (vp->v_type) {
3978 * If there's shared text associated with
3979 * the inode, we can't allow writing.
3981 if (vp->v_flag & VTEXT)
3984 error = VOP_GETATTR(vp, &vattr);
3987 error = VOP_ACCESS(vp, flags, cred); /* XXX ruid/rgid vs uid/gid */
3989 * Allow certain operations for the owner (reads and writes
3990 * on files that are already open).
3992 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3996 #endif /* NFS_NOSERVER */