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 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
299 fhp = &nfh.fh_generic;
300 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
303 ERROROUT(nfsm_srvsattr(&info, vap));
304 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
305 gcheck = fxdr_unsigned(int, *tl);
307 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
308 fxdr_nfsv3time(tl, &guard);
311 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
313 * Nah nah nah nah na nah
314 * There is a bug in the Sun client that puts 0xffff in the mode
315 * field of sattr when it should put in 0xffffffff. The u_short
316 * doesn't sign extend.
317 * --> check the low order 2 bytes for 0xffff
319 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
320 vap->va_mode = nfstov_mode(sp->sa_mode);
321 if (sp->sa_uid != nfs_xdrneg1)
322 vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
323 if (sp->sa_gid != nfs_xdrneg1)
324 vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
325 if (sp->sa_size != nfs_xdrneg1)
326 vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
327 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
329 fxdr_nfsv2time(&sp->sa_atime, &vap->va_atime);
331 vap->va_atime.tv_sec =
332 fxdr_unsigned(int32_t, sp->sa_atime.nfsv2_sec);
333 vap->va_atime.tv_nsec = 0;
336 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
337 fxdr_nfsv2time(&sp->sa_mtime, &vap->va_mtime);
342 * Now that we have all the fields, lets do it.
344 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam, &rdonly,
345 (nfsd->nd_flag & ND_KERBAUTH), TRUE);
347 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
348 2 * NFSX_UNSIGNED, &error));
349 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
356 * vp now an active resource, pay careful attention to cleanup
360 error = preat_ret = VOP_GETATTR(vp, &preat);
361 if (!error && gcheck &&
362 (preat.va_ctime.tv_sec != guard.tv_sec ||
363 preat.va_ctime.tv_nsec != guard.tv_nsec))
364 error = NFSERR_NOT_SYNC;
368 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
369 NFSX_WCCDATA(info.v3), &error));
370 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
378 * If the size is being changed write acces is required, otherwise
379 * just check for a read only file system.
381 if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
382 if (rdonly || (mp->mnt_flag & MNT_RDONLY)) {
387 if (vp->v_type == VDIR) {
390 } else if ((error = nfsrv_access(mp, vp, VWRITE, cred, rdonly,
395 error = VOP_SETATTR(vp, vap, cred);
396 postat_ret = VOP_GETATTR(vp, vap);
402 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
403 NFSX_WCCORFATTR(info.v3), &error));
405 nfsm_srvwcc_data(&info, nfsd, preat_ret, &preat,
410 fp = nfsm_build(&info, NFSX_V2FATTR);
411 nfsm_srvfattr(nfsd, vap, fp);
426 nfsrv_lookup(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
427 struct thread *td, struct mbuf **mrq)
429 struct sockaddr *nam = nfsd->nd_nam;
430 struct ucred *cred = &nfsd->nd_cr;
431 struct nfs_fattr *fp;
432 struct nlookupdata nd;
438 int error = 0, len, dirattr_ret = 1;
440 struct vattr va, dirattr, *vap = &va;
441 struct nfsm_info info;
443 info.mrep = nfsd->nd_mrep;
445 info.md = nfsd->nd_md;
446 info.dpos = nfsd->nd_dpos;
447 info.v3 = (nfsd->nd_flag & ND_NFSV3);
449 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
454 fhp = &nfh.fh_generic;
455 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
456 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
458 pubflag = nfs_ispublicfh(fhp);
460 error = nfs_namei(&nd, cred, 0, NULL, &vp,
461 fhp, len, slp, nam, &info.md, &info.dpos,
462 &dirp, td, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
465 * namei failure, only dirp to cleanup. Clear out garbarge from
466 * structure in case macros jump to nfsmout.
472 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
476 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
477 NFSX_POSTOPATTR(info.v3), &error));
478 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
484 * Locate index file for public filehandle
486 * error is 0 on entry and 0 on exit from this block.
490 if (vp->v_type == VDIR && nfs_pub.np_index != NULL) {
492 * Setup call to lookup() to see if we can find
493 * the index file. Arguably, this doesn't belong
494 * in a kernel.. Ugh. If an error occurs, do not
495 * try to install an index file and then clear the
498 * When we replace nd with ind and redirect ndp,
499 * maintenance of ni_startdir and ni_vp shift to
500 * ind and we have to clean them up in the old nd.
501 * However, the cnd resource continues to be maintained
502 * via the original nd. Confused? You aren't alone!
505 cache_copy(&nd.nl_nch, &nch);
507 error = nlookup_init_raw(&nd, nfs_pub.np_index,
508 UIO_SYSSPACE, 0, cred, &nch);
511 error = nlookup(&nd);
515 * Found an index file. Get rid of
516 * the old references. transfer vp and
517 * load up the new vp. Fortunately we do
518 * not have to deal with dvp, that would be
525 error = cache_vget(&nd.nl_nch, nd.nl_cred,
527 KKASSERT(error == 0);
532 * If the public filehandle was used, check that this lookup
533 * didn't result in a filehandle outside the publicly exported
534 * filesystem. We clear the poor vp here to avoid lockups due
538 if (vp->v_mount != nfs_pub.np_mount) {
547 dirattr_ret = VOP_GETATTR(dirp, &dirattr);
553 * Resources at this point:
554 * ndp->ni_vp may not be NULL
559 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
560 NFSX_POSTOPATTR(info.v3), &error));
561 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
567 * Clear out some resources prior to potentially blocking. This
568 * is not as critical as ni_dvp resources in other routines, but
574 * Get underlying attribute, then release remaining resources ( for
575 * the same potential blocking reason ) and reply.
577 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
578 error = VFS_VPTOFH(vp, &fhp->fh_fid);
580 error = VOP_GETATTR(vp, vap);
584 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
585 NFSX_SRVFH(info.v3) +
586 NFSX_POSTOPORFATTR(info.v3) +
587 NFSX_POSTOPATTR(info.v3),
590 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
594 nfsm_srvfhtom(&info, fhp);
596 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
597 nfsm_srvpostop_attr(&info, nfsd, dirattr_ret, &dirattr);
599 fp = nfsm_build(&info, NFSX_V2FATTR);
600 nfsm_srvfattr(nfsd, vap, fp);
607 nlookup_done(&nd); /* may be called twice */
614 * nfs readlink service
617 nfsrv_readlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
618 struct thread *td, struct mbuf **mrq)
620 struct sockaddr *nam = nfsd->nd_nam;
621 struct ucred *cred = &nfsd->nd_cr;
622 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
623 struct iovec *ivp = iv;
625 int error = 0, rdonly, i, tlen, len, getret;
626 struct mbuf *mp1, *mp2, *mp3;
627 struct vnode *vp = NULL;
628 struct mount *mp = NULL;
632 struct uio io, *uiop = &io;
633 struct nfsm_info info;
635 info.mrep = nfsd->nd_mrep;
637 info.md = nfsd->nd_md;
638 info.dpos = nfsd->nd_dpos;
639 info.v3 = (nfsd->nd_flag & ND_NFSV3);
641 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
646 fhp = &nfh.fh_generic;
647 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
650 while (len < NFS_MAXPATHLEN) {
651 mp1 = m_getcl(MB_WAIT, MT_DATA, 0);
652 mp1->m_len = MCLBYTES;
659 if ((len + mp1->m_len) > NFS_MAXPATHLEN) {
660 mp1->m_len = NFS_MAXPATHLEN-len;
661 len = NFS_MAXPATHLEN;
664 ivp->iov_base = mtod(mp1, caddr_t);
665 ivp->iov_len = mp1->m_len;
670 uiop->uio_iovcnt = i;
671 uiop->uio_offset = 0;
672 uiop->uio_resid = len;
673 uiop->uio_rw = UIO_READ;
674 uiop->uio_segflg = UIO_SYSSPACE;
676 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
677 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
679 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
680 2 * NFSX_UNSIGNED, &error));
681 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
685 if (vp->v_type != VLNK) {
692 error = VOP_READLINK(vp, uiop, cred);
694 getret = VOP_GETATTR(vp, &attr);
697 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
698 NFSX_POSTOPATTR(info.v3) + NFSX_UNSIGNED,
701 nfsm_srvpostop_attr(&info, nfsd, getret, &attr);
707 if (uiop->uio_resid > 0) {
708 len -= uiop->uio_resid;
709 tlen = nfsm_rndup(len);
710 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
712 tl = nfsm_build(&info, NFSX_UNSIGNED);
713 *tl = txdr_unsigned(len);
714 info.mb->m_next = mp3;
729 nfsrv_read(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
730 struct thread *td, struct mbuf **mrq)
732 struct nfsm_info info;
733 struct sockaddr *nam = nfsd->nd_nam;
734 struct ucred *cred = &nfsd->nd_cr;
738 struct nfs_fattr *fp;
742 int error = 0, rdonly, cnt, len, left, siz, tlen, getret;
744 struct vnode *vp = NULL;
745 struct mount *mp = NULL;
748 struct uio io, *uiop = &io;
749 struct vattr va, *vap = &va;
754 info.mrep = nfsd->nd_mrep;
756 info.md = nfsd->nd_md;
757 info.dpos = nfsd->nd_dpos;
758 info.v3 = (nfsd->nd_flag & ND_NFSV3);
760 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
761 fhp = &nfh.fh_generic;
762 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
764 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
765 off = fxdr_hyper(tl);
767 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
768 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
770 NEGREPLYOUT(reqlen = nfsm_srvstrsiz(&info,
771 NFS_SRVMAXDATA(nfsd), &error));
774 * Reference vp. If an error occurs, vp will be invalid, but we
775 * have to NULL it just in case. The macros might goto nfsmout
779 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
780 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
783 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
784 2 * NFSX_UNSIGNED, &error));
785 nfsm_srvpostop_attr(&info, nfsd, 1, NULL);
790 if (vp->v_type != VREG) {
794 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
797 if ((error = nfsrv_access(mp, vp, VREAD, cred, rdonly, td, 1)) != 0)
798 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 1);
800 getret = VOP_GETATTR(vp, vap);
806 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
807 NFSX_POSTOPATTR(info.v3), &error));
808 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
814 * Calculate byte count to read
817 if (off >= vap->va_size)
819 else if ((off + reqlen) > vap->va_size)
820 cnt = vap->va_size - off;
825 * Calculate seqcount for heuristic
833 * Locate best candidate
836 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
840 if (nfsheur[hi].nh_vp == vp) {
844 if (nfsheur[hi].nh_use > 0)
845 --nfsheur[hi].nh_use;
846 hi = (hi + 1) % NUM_HEURISTIC;
847 if (nfsheur[hi].nh_use < nh->nh_use)
851 if (nh->nh_vp != vp) {
854 nh->nh_use = NHUSE_INIT;
862 * Calculate heuristic
865 if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
866 if (++nh->nh_seqcount > IO_SEQMAX)
867 nh->nh_seqcount = IO_SEQMAX;
868 } else if (nh->nh_seqcount > 1) {
873 nh->nh_use += NHUSE_INC;
874 if (nh->nh_use > NHUSE_MAX)
875 nh->nh_use = NHUSE_MAX;
876 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
879 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
880 NFSX_POSTOPORFATTR(info.v3) +
881 3 * NFSX_UNSIGNED + nfsm_rndup(cnt),
884 tl = nfsm_build(&info, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
886 fp = (struct nfs_fattr *)tl;
887 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
889 tl = nfsm_build(&info, NFSX_V2FATTR + NFSX_UNSIGNED);
890 fp = (struct nfs_fattr *)tl;
891 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
893 len = left = nfsm_rndup(cnt);
896 * Generate the mbuf list with the uio_iov ref. to it.
901 siz = min(M_TRAILINGSPACE(m), left);
907 m = m_getcl(MB_WAIT, MT_DATA, 0);
913 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
915 uiop->uio_iov = iv2 = iv;
921 panic("nfsrv_read iov");
922 siz = min(M_TRAILINGSPACE(m), left);
924 iv->iov_base = mtod(m, caddr_t) + m->m_len;
933 uiop->uio_iovcnt = i;
934 uiop->uio_offset = off;
935 uiop->uio_resid = len;
936 uiop->uio_rw = UIO_READ;
937 uiop->uio_segflg = UIO_SYSSPACE;
938 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
939 off = uiop->uio_offset;
941 FREE((caddr_t)iv2, M_TEMP);
942 if (error || (getret = VOP_GETATTR(vp, vap))) {
949 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
950 NFSX_POSTOPATTR(info.v3),
952 nfsm_srvpostop_attr(&info, nfsd, getret, vap);
961 nfsm_srvfattr(nfsd, vap, fp);
962 tlen = len - uiop->uio_resid;
963 cnt = cnt < tlen ? cnt : tlen;
964 tlen = nfsm_rndup(cnt);
965 if (len != tlen || tlen != cnt)
966 nfsm_adj(info.mb, len - tlen, tlen - cnt);
968 *tl++ = txdr_unsigned(cnt);
974 *tl = txdr_unsigned(cnt);
986 nfsrv_write(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
987 struct thread *td, struct mbuf **mrq)
989 struct sockaddr *nam = nfsd->nd_nam;
990 struct ucred *cred = &nfsd->nd_cr;
994 struct nfs_fattr *fp;
996 struct vattr va, forat;
997 struct vattr *vap = &va;
999 int error = 0, rdonly, len, forat_ret = 1;
1000 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
1001 int stable = NFSV3WRITE_FILESYNC;
1002 struct vnode *vp = NULL;
1003 struct mount *mp = NULL;
1006 struct uio io, *uiop = &io;
1007 struct nfsm_info info;
1010 info.mrep = nfsd->nd_mrep;
1012 info.md = nfsd->nd_md;
1013 info.dpos = nfsd->nd_dpos;
1014 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1016 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1017 if (info.mrep == NULL) {
1021 fhp = &nfh.fh_generic;
1022 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1024 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1025 off = fxdr_hyper(tl);
1027 stable = fxdr_unsigned(int, *tl++);
1029 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1030 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1033 stable = NFSV3WRITE_UNSTABLE;
1035 retlen = len = fxdr_unsigned(int32_t, *tl);
1039 * For NFS Version 2, it is not obvious what a write of zero length
1040 * should do, but I might as well be consistent with Version 3,
1041 * which is to return ok so long as there are no permission problems.
1047 if (mp1 == info.md) {
1049 adjust = info.dpos - mtod(mp1, caddr_t);
1050 mp1->m_len -= adjust;
1051 if (mp1->m_len > 0 && adjust > 0)
1052 mp1->m_data += adjust;
1056 else if (mp1->m_len > 0) {
1059 mp1->m_len -= (i - len);
1068 if (len > NFS_MAXDATA || len < 0 || i < len) {
1070 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1071 2 * NFSX_UNSIGNED, &error));
1072 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1077 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
1078 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1081 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1082 2 * NFSX_UNSIGNED, &error));
1083 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1089 forat_ret = VOP_GETATTR(vp, &forat);
1090 if (vp->v_type != VREG) {
1094 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1097 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1102 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1103 NFSX_WCCDATA(info.v3), &error));
1104 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1111 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
1113 uiop->uio_iov = iv = ivp;
1114 uiop->uio_iovcnt = cnt;
1117 if (mp1->m_len > 0) {
1118 ivp->iov_base = mtod(mp1, caddr_t);
1119 ivp->iov_len = mp1->m_len;
1127 * The IO_METASYNC flag indicates that all metadata (and not just
1128 * enough to ensure data integrity) mus be written to stable storage
1130 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
1132 if (stable == NFSV3WRITE_UNSTABLE)
1133 ioflags = IO_NODELOCKED;
1134 else if (stable == NFSV3WRITE_DATASYNC)
1135 ioflags = (IO_SYNC | IO_NODELOCKED);
1137 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1138 uiop->uio_resid = len;
1139 uiop->uio_rw = UIO_WRITE;
1140 uiop->uio_segflg = UIO_SYSSPACE;
1141 uiop->uio_td = NULL;
1142 uiop->uio_offset = off;
1143 error = VOP_WRITE(vp, uiop, ioflags, cred);
1144 nfsstats.srvvop_writes++;
1145 FREE((caddr_t)iv, M_TEMP);
1147 aftat_ret = VOP_GETATTR(vp, vap);
1152 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1153 NFSX_PREOPATTR(info.v3) +
1154 NFSX_POSTOPORFATTR(info.v3) +
1155 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(info.v3),
1158 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1164 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1165 *tl++ = txdr_unsigned(retlen);
1167 * If nfs_async is set, then pretend the write was FILESYNC.
1169 if (stable == NFSV3WRITE_UNSTABLE && !nfs_async)
1170 *tl++ = txdr_unsigned(stable);
1172 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
1174 * Actually, there is no need to txdr these fields,
1175 * but it may make the values more human readable,
1176 * for debugging purposes.
1178 if (nfsver.tv_sec == 0)
1180 *tl++ = txdr_unsigned(nfsver.tv_sec);
1181 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1183 fp = nfsm_build(&info, NFSX_V2FATTR);
1184 nfsm_srvfattr(nfsd, vap, fp);
1194 * NFS write service with write gathering support. Called when
1195 * nfsrvw_procrastinate > 0.
1196 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
1197 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
1201 nfsrv_writegather(struct nfsrv_descript **ndp, struct nfssvc_sock *slp,
1202 struct thread *td, struct mbuf **mrq)
1205 struct nfsrv_descript *wp, *nfsd, *owp, *swp;
1206 struct nfs_fattr *fp;
1209 struct nfsrvw_delayhash *wpp;
1211 struct vattr va, forat;
1213 int error = 0, rdonly, len, forat_ret = 1;
1214 int ioflags, aftat_ret = 1, adjust, zeroing;
1216 struct vnode *vp = NULL;
1217 struct mount *mp = NULL;
1218 struct uio io, *uiop = &io;
1220 struct nfsm_info info;
1222 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1231 info.mrep = nfsd->nd_mrep;
1233 info.md = nfsd->nd_md;
1234 info.dpos = nfsd->nd_dpos;
1235 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1236 cred = &nfsd->nd_cr;
1237 LIST_INIT(&nfsd->nd_coalesce);
1238 nfsd->nd_mreq = NULL;
1239 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
1240 cur_usec = nfs_curusec();
1241 nfsd->nd_time = cur_usec +
1242 (info.v3 ? nfsrvw_procrastinate_v3 : nfsrvw_procrastinate);
1245 * Now, get the write header..
1247 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, &nfsd->nd_fh, &error));
1249 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
1250 nfsd->nd_off = fxdr_hyper(tl);
1252 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
1254 NULLOUT(tl = nfsm_dissect(&info, 4 * NFSX_UNSIGNED));
1255 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
1258 nfsd->nd_stable = NFSV3WRITE_UNSTABLE;
1260 len = fxdr_unsigned(int32_t, *tl);
1262 nfsd->nd_eoff = nfsd->nd_off + len;
1265 * Trim the header out of the mbuf list and trim off any trailing
1266 * junk so that the mbuf list has only the write data.
1272 if (mp1 == info.md) {
1274 adjust = info.dpos - mtod(mp1, caddr_t);
1275 mp1->m_len -= adjust;
1276 if (mp1->m_len > 0 && adjust > 0)
1277 mp1->m_data += adjust;
1284 mp1->m_len -= (i - len);
1290 if (len > NFS_MAXDATA || len < 0 || i < len) {
1295 nfsm_writereply(&info, nfsd, slp, error, 2 * NFSX_UNSIGNED);
1297 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1300 nfsd->nd_mreq = info.mreq;
1301 nfsd->nd_mrep = NULL;
1306 * Add this entry to the hash and time queues.
1310 wp = slp->ns_tq.lh_first;
1311 while (wp && wp->nd_time < nfsd->nd_time) {
1313 wp = wp->nd_tq.le_next;
1315 NFS_DPF(WG, ("Q%03x", nfsd->nd_retxid & 0xfff));
1317 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1319 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1321 if (nfsd->nd_mrep) {
1322 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1326 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1328 wp = wp->nd_hash.le_next;
1330 while (wp && wp->nd_off < nfsd->nd_off &&
1331 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1333 wp = wp->nd_hash.le_next;
1336 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1339 * Search the hash list for overlapping entries and
1342 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1343 wp = nfsd->nd_hash.le_next;
1344 if (NFSW_SAMECRED(owp, nfsd))
1345 nfsrvw_coalesce(owp, nfsd);
1348 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1355 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1356 * and generate the associated reply mbuf list(s).
1359 cur_usec = nfs_curusec();
1361 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1362 owp = nfsd->nd_tq.le_next;
1363 if (nfsd->nd_time > cur_usec)
1367 NFS_DPF(WG, ("P%03x", nfsd->nd_retxid & 0xfff));
1368 LIST_REMOVE(nfsd, nd_tq);
1369 LIST_REMOVE(nfsd, nd_hash);
1371 info.mrep = nfsd->nd_mrep;
1372 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1373 nfsd->nd_mrep = NULL;
1374 cred = &nfsd->nd_cr;
1375 forat_ret = aftat_ret = 1;
1376 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &mp, &vp, cred, slp,
1377 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
1380 forat_ret = VOP_GETATTR(vp, &forat);
1381 if (vp->v_type != VREG) {
1385 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1391 error = nfsrv_access(mp, vp, VWRITE, cred, rdonly, td, 1);
1394 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1395 ioflags = IO_NODELOCKED;
1396 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1397 ioflags = (IO_SYNC | IO_NODELOCKED);
1399 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1400 uiop->uio_rw = UIO_WRITE;
1401 uiop->uio_segflg = UIO_SYSSPACE;
1402 uiop->uio_td = NULL;
1403 uiop->uio_offset = nfsd->nd_off;
1404 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1405 if (uiop->uio_resid > 0) {
1413 uiop->uio_iovcnt = i;
1414 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1416 uiop->uio_iov = ivp = iov;
1419 if (mp1->m_len > 0) {
1420 ivp->iov_base = mtod(mp1, caddr_t);
1421 ivp->iov_len = mp1->m_len;
1427 error = VOP_WRITE(vp, uiop, ioflags, cred);
1428 nfsstats.srvvop_writes++;
1430 FREE((caddr_t)iov, M_TEMP);
1435 aftat_ret = VOP_GETATTR(vp, &va);
1441 * Loop around generating replies for all write rpcs that have
1442 * now been completed.
1446 NFS_DPF(WG, ("R%03x", nfsd->nd_retxid & 0xfff));
1448 nfsm_writereply(&info, nfsd, slp, error,
1449 NFSX_WCCDATA(info.v3));
1451 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1455 nfsm_writereply(&info, nfsd, slp, error,
1456 NFSX_PREOPATTR(info.v3) +
1457 NFSX_POSTOPORFATTR(info.v3) +
1459 NFSX_WRITEVERF(info.v3));
1461 nfsm_srvwcc_data(&info, nfsd, forat_ret, &forat,
1463 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
1464 *tl++ = txdr_unsigned(nfsd->nd_len);
1465 *tl++ = txdr_unsigned(swp->nd_stable);
1467 * Actually, there is no need to txdr these fields,
1468 * but it may make the values more human readable,
1469 * for debugging purposes.
1471 if (nfsver.tv_sec == 0)
1473 *tl++ = txdr_unsigned(nfsver.tv_sec);
1474 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
1476 fp = nfsm_build(&info, NFSX_V2FATTR);
1477 nfsm_srvfattr(nfsd, &va, fp);
1480 nfsd->nd_mreq = info.mreq;
1482 panic("nfsrv_write: nd_mrep not free");
1485 * Done. Put it at the head of the timer queue so that
1486 * the final phase can return the reply.
1491 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1493 nfsd = swp->nd_coalesce.lh_first;
1495 LIST_REMOVE(nfsd, nd_tq);
1501 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1508 * Search for a reply to return.
1511 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1512 if (nfsd->nd_mreq) {
1513 NFS_DPF(WG, ("X%03x", nfsd->nd_retxid & 0xfff));
1514 LIST_REMOVE(nfsd, nd_tq);
1515 *mrq = nfsd->nd_mreq;
1524 * Coalesce the write request nfsd into owp. To do this we must:
1525 * - remove nfsd from the queues
1526 * - merge nfsd->nd_mrep into owp->nd_mrep
1527 * - update the nd_eoff and nd_stable for owp
1528 * - put nfsd on owp's nd_coalesce list
1529 * NB: Must be called at splsoftclock().
1532 nfsrvw_coalesce(struct nfsrv_descript *owp, struct nfsrv_descript *nfsd)
1536 struct nfsrv_descript *p;
1538 NFS_DPF(WG, ("C%03x-%03x",
1539 nfsd->nd_retxid & 0xfff, owp->nd_retxid & 0xfff));
1540 LIST_REMOVE(nfsd, nd_hash);
1541 LIST_REMOVE(nfsd, nd_tq);
1542 if (owp->nd_eoff < nfsd->nd_eoff) {
1543 overlap = owp->nd_eoff - nfsd->nd_off;
1545 panic("nfsrv_coalesce: bad off");
1547 m_adj(nfsd->nd_mrep, overlap);
1551 mp1->m_next = nfsd->nd_mrep;
1552 owp->nd_eoff = nfsd->nd_eoff;
1554 m_freem(nfsd->nd_mrep);
1555 nfsd->nd_mrep = NULL;
1556 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1557 owp->nd_stable = NFSV3WRITE_FILESYNC;
1558 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1559 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1560 owp->nd_stable = NFSV3WRITE_DATASYNC;
1561 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1564 * If nfsd had anything else coalesced into it, transfer them
1565 * to owp, otherwise their replies will never get sent.
1567 for (p = nfsd->nd_coalesce.lh_first; p;
1568 p = nfsd->nd_coalesce.lh_first) {
1569 LIST_REMOVE(p, nd_tq);
1570 LIST_INSERT_HEAD(&owp->nd_coalesce, p, nd_tq);
1575 * nfs create service
1576 * now does a truncate to 0 length via. setattr if it already exists
1579 nfsrv_create(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1580 struct thread *td, struct mbuf **mrq)
1582 struct sockaddr *nam = nfsd->nd_nam;
1583 struct ucred *cred = &nfsd->nd_cr;
1584 struct nfs_fattr *fp;
1585 struct vattr va, dirfor, diraft;
1586 struct vattr *vap = &va;
1587 struct nfsv2_sattr *sp;
1589 struct nlookupdata nd;
1590 int error = 0, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1591 udev_t rdev = NOUDEV;
1593 int how, exclusive_flag = 0;
1601 u_char cverf[NFSX_V3CREATEVERF];
1602 struct nfsm_info info;
1604 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1610 info.mrep = nfsd->nd_mrep;
1612 info.md = nfsd->nd_md;
1613 info.dpos = nfsd->nd_dpos;
1614 info.v3 = (nfsd->nd_flag & ND_NFSV3);
1616 fhp = &nfh.fh_generic;
1617 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1618 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1621 * Call namei and do initial cleanup to get a few things
1622 * out of the way. If we get an initial error we cleanup
1623 * and return here to avoid special-casing the invalid nd
1624 * structure through the rest of the case. dirp may be
1625 * set even if an error occurs, but the nd structure will not
1626 * be valid at all if an error occurs so we have to invalidate it
1627 * prior to calling nfsm_reply ( which might goto nfsmout ).
1629 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1630 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1631 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1632 mp = vfs_getvfs(&fhp->fh_fsid);
1636 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1643 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1644 NFSX_WCCDATA(info.v3), &error));
1645 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1646 diraft_ret, &diraft);
1652 * No error. Continue. State:
1655 * vp may be valid or NULL if the target does not
1659 * The error state is set through the code and we may also do some
1660 * opportunistic releasing of vnodes to avoid holding locks through
1661 * NFS I/O. The cleanup at the end is a catch-all
1666 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1667 how = fxdr_unsigned(int, *tl);
1669 case NFSV3CREATE_GUARDED:
1675 case NFSV3CREATE_UNCHECKED:
1676 ERROROUT(nfsm_srvsattr(&info, vap));
1678 case NFSV3CREATE_EXCLUSIVE:
1679 NULLOUT(cp = nfsm_dissect(&info, NFSX_V3CREATEVERF));
1680 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1684 vap->va_type = VREG;
1686 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
1687 vap->va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1688 if (vap->va_type == VNON)
1689 vap->va_type = VREG;
1690 vap->va_mode = nfstov_mode(sp->sa_mode);
1691 switch (vap->va_type) {
1693 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1695 vap->va_size = (u_quad_t)tsize;
1700 rdev = fxdr_unsigned(long, sp->sa_size);
1708 * Iff doesn't exist, create it
1709 * otherwise just truncate to 0 length
1710 * should I set the mode too ?
1712 * The only possible error we can have at this point is EEXIST.
1713 * nd.ni_vp will also be non-NULL in that case.
1716 if (vap->va_mode == (mode_t)VNOVAL)
1718 if (vap->va_type == VREG || vap->va_type == VSOCK) {
1720 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp,
1725 if (exclusive_flag) {
1728 bcopy(cverf, (caddr_t)&vap->va_atime,
1730 error = VOP_SETATTR(vp, vap, cred);
1734 vap->va_type == VCHR ||
1735 vap->va_type == VBLK ||
1736 vap->va_type == VFIFO
1739 * Handle SysV FIFO node special cases. All other
1740 * devices require super user to access.
1742 if (vap->va_type == VCHR && rdev == 0xffffffff)
1743 vap->va_type = VFIFO;
1744 if (vap->va_type != VFIFO &&
1745 (error = priv_check_cred(cred, PRIV_ROOT, 0))) {
1748 vap->va_rmajor = umajor(rdev);
1749 vap->va_rminor = uminor(rdev);
1752 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1759 * XXX what is this junk supposed to do ?
1766 * release dvp prior to lookup
1774 * Even though LOCKPARENT was cleared, ni_dvp may
1777 nd.ni_cnd.cn_nameiop = NAMEI_LOOKUP;
1778 nd.ni_cnd.cn_flags &= ~(CNP_LOCKPARENT);
1779 nd.ni_cnd.cn_td = td;
1780 nd.ni_cnd.cn_cred = cred;
1782 error = lookup(&nd);
1786 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1788 /* fall through on certain errors */
1790 nfsrv_object_create(nd.ni_vp);
1791 if (nd.ni_cnd.cn_flags & CNP_ISSYMLINK) {
1800 if (vap->va_size != -1) {
1801 error = nfsrv_access(mp, vp, VWRITE, cred,
1802 (nd.nl_flags & NLC_NFS_RDONLY), td, 0);
1804 tempsize = vap->va_size;
1806 vap->va_size = tempsize;
1807 error = VOP_SETATTR(vp, vap, cred);
1813 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1814 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1816 error = VOP_GETATTR(vp, vap);
1819 if (exclusive_flag && !error &&
1820 bcmp(cverf, (caddr_t)&vap->va_atime, NFSX_V3CREATEVERF))
1822 diraft_ret = VOP_GETATTR(dirp, &diraft);
1826 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1827 NFSX_SRVFH(info.v3) + NFSX_FATTR(info.v3) +
1828 NFSX_WCCDATA(info.v3),
1832 nfsm_srvpostop_fh(&info, fhp);
1833 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1835 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1836 diraft_ret, &diraft);
1839 nfsm_srvfhtom(&info, fhp);
1840 fp = nfsm_build(&info, NFSX_V2FATTR);
1841 nfsm_srvfattr(nfsd, vap, fp);
1846 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
1867 * nfs v3 mknod service
1870 nfsrv_mknod(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
1871 struct thread *td, struct mbuf **mrq)
1873 struct sockaddr *nam = nfsd->nd_nam;
1874 struct ucred *cred = &nfsd->nd_cr;
1875 struct vattr va, dirfor, diraft;
1876 struct vattr *vap = &va;
1878 struct nlookupdata nd;
1879 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
1886 struct nfsm_info info;
1888 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
1894 info.mrep = nfsd->nd_mrep;
1896 info.md = nfsd->nd_md;
1897 info.dpos = nfsd->nd_dpos;
1899 fhp = &nfh.fh_generic;
1900 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
1901 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
1904 * Handle nfs_namei() call. If an error occurs, the nd structure
1905 * is not valid. However, nfsm_*() routines may still jump to
1909 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
1910 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
1911 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1913 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
1915 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1916 NFSX_WCCDATA(1), &error));
1917 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1918 diraft_ret, &diraft);
1922 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
1923 vtyp = nfsv3tov_type(*tl);
1924 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1925 error = NFSERR_BADTYPE;
1929 ERROROUT(nfsm_srvsattr(&info, vap));
1930 if (vtyp == VCHR || vtyp == VBLK) {
1931 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
1932 vap->va_rmajor = fxdr_unsigned(u_int32_t, *tl++);
1933 vap->va_rminor = fxdr_unsigned(u_int32_t, *tl);
1937 * Iff doesn't exist, create it.
1943 vap->va_type = vtyp;
1944 if (vap->va_mode == (mode_t)VNOVAL)
1946 if (vtyp == VSOCK) {
1948 error = VOP_NCREATE(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1952 if (vtyp != VFIFO && (error = priv_check_cred(cred, PRIV_ROOT, 0)))
1956 error = VOP_NMKNOD(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
1964 * send response, cleanup, return.
1976 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
1977 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1979 error = VOP_GETATTR(vp, vap);
1985 diraft_ret = VOP_GETATTR(dirp, &diraft);
1990 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
1991 NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) +
1992 NFSX_WCCDATA(1), &error));
1994 nfsm_srvpostop_fh(&info, fhp);
1995 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
1997 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
1998 diraft_ret, &diraft);
2017 * nfs remove service
2020 nfsrv_remove(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2021 struct thread *td, struct mbuf **mrq)
2023 struct sockaddr *nam = nfsd->nd_nam;
2024 struct ucred *cred = &nfsd->nd_cr;
2025 struct nlookupdata nd;
2026 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2030 struct vattr dirfor, diraft;
2033 struct nfsm_info info;
2035 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2041 info.mrep = nfsd->nd_mrep;
2043 info.md = nfsd->nd_md;
2044 info.dpos = nfsd->nd_dpos;
2045 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2047 fhp = &nfh.fh_generic;
2048 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2049 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2051 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2052 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2053 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2056 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2059 if (vp->v_type == VDIR) {
2060 error = EPERM; /* POSIX */
2064 * The root of a mounted filesystem cannot be deleted.
2066 if (vp->v_flag & VROOT) {
2078 error = VOP_NREMOVE(&nd.nl_nch, dvp, nd.nl_cred);
2083 if (dirp && info.v3)
2084 diraft_ret = VOP_GETATTR(dirp, &diraft);
2085 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2087 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2088 diraft_ret, &diraft);
2108 * nfs rename service
2111 nfsrv_rename(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2112 struct thread *td, struct mbuf **mrq)
2114 struct sockaddr *nam = nfsd->nd_nam;
2115 struct ucred *cred = &nfsd->nd_cr;
2116 int error = 0, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
2117 int tdirfor_ret = 1, tdiraft_ret = 1;
2118 struct nlookupdata fromnd, tond;
2119 struct vnode *fvp, *fdirp, *fdvp;
2120 struct vnode *tvp, *tdirp, *tdvp;
2121 struct namecache *ncp;
2122 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
2124 fhandle_t *ffhp, *tfhp;
2126 struct nfsm_info info;
2128 info.mrep = nfsd->nd_mrep;
2130 info.md = nfsd->nd_md;
2131 info.dpos = nfsd->nd_dpos;
2132 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2134 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2138 ffhp = &fnfh.fh_generic;
2139 tfhp = &tnfh.fh_generic;
2142 * Clear fields incase goto nfsmout occurs from macro.
2145 nlookup_zero(&fromnd);
2146 nlookup_zero(&tond);
2150 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, ffhp, &error));
2151 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2154 * Remember our original uid so that we can reset cr_uid before
2155 * the second nfs_namei() call, in case it is remapped.
2157 saved_uid = cred->cr_uid;
2158 error = nfs_namei(&fromnd, cred, NLC_RENAME_SRC,
2160 ffhp, len, slp, nam, &info.md, &info.dpos, &fdirp,
2161 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2164 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor);
2167 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2168 2 * NFSX_WCCDATA(info.v3), &error));
2169 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2170 fdiraft_ret, &fdiraft);
2171 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2172 tdiraft_ret, &tdiraft);
2178 * We have to unlock the from ncp before we can safely lookup
2181 KKASSERT(fromnd.nl_flags & NLC_NCPISLOCKED);
2182 cache_unlock(&fromnd.nl_nch);
2183 fromnd.nl_flags &= ~NLC_NCPISLOCKED;
2184 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, tfhp, &error));
2185 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXNAMLEN));
2186 cred->cr_uid = saved_uid;
2188 error = nfs_namei(&tond, cred, NLC_RENAME_DST, NULL, NULL,
2189 tfhp, len2, slp, nam, &info.md, &info.dpos, &tdirp,
2190 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2193 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor);
2201 if (cache_lock_nonblock(&fromnd.nl_nch) == 0) {
2202 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2203 } else if (fromnd.nl_nch.ncp > tond.nl_nch.ncp) {
2204 cache_lock(&fromnd.nl_nch);
2205 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2207 cache_unlock(&tond.nl_nch);
2208 cache_lock(&fromnd.nl_nch);
2209 cache_resolve(&fromnd.nl_nch, fromnd.nl_cred);
2210 cache_lock(&tond.nl_nch);
2211 cache_resolve(&tond.nl_nch, tond.nl_cred);
2213 fromnd.nl_flags |= NLC_NCPISLOCKED;
2215 fvp = fromnd.nl_nch.ncp->nc_vp;
2216 tvp = tond.nl_nch.ncp->nc_vp;
2219 * Set fdvp and tdvp. We haven't done all the topology checks
2220 * so these can wind up NULL (e.g. if either fvp or tvp is a mount
2221 * point). If we get through the checks these will be guarenteed
2224 * Holding the children ncp's should be sufficient to prevent
2225 * fdvp and tdvp ripouts.
2227 if (fromnd.nl_nch.ncp->nc_parent)
2228 fdvp = fromnd.nl_nch.ncp->nc_parent->nc_vp;
2231 if (tond.nl_nch.ncp->nc_parent)
2232 tdvp = tond.nl_nch.ncp->nc_parent->nc_vp;
2237 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2243 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2250 if (tvp->v_type == VDIR && (tond.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2258 if (fvp->v_type == VDIR && (fromnd.nl_nch.ncp->nc_flag & NCF_ISMOUNTPT)) {
2265 if (fromnd.nl_nch.mount != tond.nl_nch.mount) {
2272 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp->nc_parent) {
2280 * You cannot rename a source into itself or a subdirectory of itself.
2281 * We check this by travsering the target directory upwards looking
2282 * for a match against the source.
2285 for (ncp = tond.nl_nch.ncp; ncp; ncp = ncp->nc_parent) {
2286 if (fromnd.nl_nch.ncp == ncp) {
2294 * If source is the same as the destination (that is the
2295 * same vnode with the same name in the same directory),
2296 * then there is nothing to do.
2298 if (fromnd.nl_nch.ncp == tond.nl_nch.ncp)
2303 * The VOP_NRENAME function releases all vnode references &
2304 * locks prior to returning so we need to clear the pointers
2305 * to bypass cleanup code later on.
2307 error = VOP_NRENAME(&fromnd.nl_nch, &tond.nl_nch,
2308 fdvp, tdvp, tond.nl_cred);
2317 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft);
2319 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft);
2320 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2321 2 * NFSX_WCCDATA(info.v3), &error));
2323 nfsm_srvwcc_data(&info, nfsd, fdirfor_ret, &fdirfor,
2324 fdiraft_ret, &fdiraft);
2325 nfsm_srvwcc_data(&info, nfsd, tdirfor_ret, &tdirfor,
2326 tdiraft_ret, &tdiraft);
2335 nlookup_done(&tond);
2338 nlookup_done(&fromnd);
2346 nfsrv_link(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2347 struct thread *td, struct mbuf **mrq)
2349 struct sockaddr *nam = nfsd->nd_nam;
2350 struct ucred *cred = &nfsd->nd_cr;
2351 struct nlookupdata nd;
2352 int error = 0, rdonly, len, dirfor_ret = 1, diraft_ret = 1;
2360 struct vattr dirfor, diraft, at;
2362 fhandle_t *fhp, *dfhp;
2363 struct nfsm_info info;
2365 info.mrep = nfsd->nd_mrep;
2367 info.md = nfsd->nd_md;
2368 info.dpos = nfsd->nd_dpos;
2369 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2371 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2373 dirp = dvp = vp = xp = NULL;
2376 fhp = &nfh.fh_generic;
2377 dfhp = &dnfh.fh_generic;
2378 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2379 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, dfhp, &error));
2380 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2382 error = nfsrv_fhtovp(fhp, FALSE, &xmp, &xp, cred, slp, nam,
2383 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2385 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2386 NFSX_POSTOPATTR(info.v3) +
2387 NFSX_WCCDATA(info.v3),
2389 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2390 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2391 diraft_ret, &diraft);
2396 if (xp->v_type == VDIR) {
2397 error = EPERM; /* POSIX */
2401 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2402 dfhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2403 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2406 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2415 if (xp->v_mount != dvp->v_mount)
2420 error = VOP_NLINK(&nd.nl_nch, dvp, xp, nd.nl_cred);
2428 getret = VOP_GETATTR(xp, &at);
2430 diraft_ret = VOP_GETATTR(dirp, &diraft);
2431 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2432 NFSX_POSTOPATTR(info.v3) + NFSX_WCCDATA(info.v3),
2435 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2436 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2437 diraft_ret, &diraft);
2461 * nfs symbolic link service
2464 nfsrv_symlink(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2465 struct thread *td, struct mbuf **mrq)
2467 struct sockaddr *nam = nfsd->nd_nam;
2468 struct ucred *cred = &nfsd->nd_cr;
2469 struct vattr va, dirfor, diraft;
2470 struct nlookupdata nd;
2471 struct vattr *vap = &va;
2472 struct nfsv2_sattr *sp;
2473 char *pathcp = NULL;
2476 int error = 0, len, len2, dirfor_ret = 1, diraft_ret = 1;
2482 struct nfsm_info info;
2484 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2490 info.mrep = nfsd->nd_mrep;
2492 info.md = nfsd->nd_md;
2493 info.dpos = nfsd->nd_dpos;
2494 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2496 fhp = &nfh.fh_generic;
2497 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2498 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2500 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2501 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2502 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2505 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2512 ERROROUT(nfsm_srvsattr(&info, vap));
2514 NEGATIVEOUT(len2 = nfsm_strsiz(&info, NFS_MAXPATHLEN));
2515 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2516 iv.iov_base = pathcp;
2518 io.uio_resid = len2;
2522 io.uio_segflg = UIO_SYSSPACE;
2523 io.uio_rw = UIO_READ;
2525 ERROROUT(nfsm_mtouio(&info, &io, len2));
2527 NULLOUT(sp = nfsm_dissect(&info, NFSX_V2SATTR));
2528 vap->va_mode = nfstov_mode(sp->sa_mode);
2530 *(pathcp + len2) = '\0';
2536 if (vap->va_mode == (mode_t)VNOVAL)
2540 error = VOP_NSYMLINK(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap, pathcp);
2544 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2545 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2547 error = VOP_GETATTR(vp, vap);
2562 FREE(pathcp, M_TEMP);
2566 diraft_ret = VOP_GETATTR(dirp, &diraft);
2570 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2571 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2572 NFSX_WCCDATA(info.v3),
2576 nfsm_srvpostop_fh(&info, fhp);
2577 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2579 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2580 diraft_ret, &diraft);
2593 FREE(pathcp, M_TEMP);
2601 nfsrv_mkdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2602 struct thread *td, struct mbuf **mrq)
2604 struct sockaddr *nam = nfsd->nd_nam;
2605 struct ucred *cred = &nfsd->nd_cr;
2606 struct vattr va, dirfor, diraft;
2607 struct vattr *vap = &va;
2608 struct nfs_fattr *fp;
2609 struct nlookupdata nd;
2611 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2617 struct nfsm_info info;
2619 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2625 info.dpos = nfsd->nd_dpos;
2626 info.mrep = nfsd->nd_mrep;
2628 info.md = nfsd->nd_md;
2629 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2631 fhp = &nfh.fh_generic;
2632 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2633 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2635 error = nfs_namei(&nd, cred, NLC_CREATE, &dvp, &vp,
2636 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2637 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2640 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2643 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2644 NFSX_WCCDATA(info.v3), &error));
2645 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2646 diraft_ret, &diraft);
2652 ERROROUT(nfsm_srvsattr(&info, vap));
2654 NULLOUT(tl = nfsm_dissect(&info, NFSX_UNSIGNED));
2655 vap->va_mode = nfstov_mode(*tl++);
2659 * At this point nd.ni_dvp is referenced and exclusively locked and
2660 * nd.ni_vp, if it exists, is referenced but not locked.
2663 vap->va_type = VDIR;
2670 * Issue mkdir op. Since SAVESTART is not set, the pathname
2671 * component is freed by the VOP call. This will fill-in
2672 * nd.ni_vp, reference, and exclusively lock it.
2674 if (vap->va_mode == (mode_t)VNOVAL)
2677 error = VOP_NMKDIR(&nd.nl_nch, dvp, &vp, nd.nl_cred, vap);
2682 bzero(&fhp->fh_fid, sizeof(fhp->fh_fid));
2683 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2685 error = VOP_GETATTR(vp, vap);
2689 diraft_ret = VOP_GETATTR(dirp, &diraft);
2690 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2691 NFSX_SRVFH(info.v3) + NFSX_POSTOPATTR(info.v3) +
2692 NFSX_WCCDATA(info.v3),
2696 nfsm_srvpostop_fh(&info, fhp);
2697 nfsm_srvpostop_attr(&info, nfsd, 0, vap);
2699 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2700 diraft_ret, &diraft);
2702 nfsm_srvfhtom(&info, fhp);
2703 fp = nfsm_build(&info, NFSX_V2FATTR);
2704 nfsm_srvfattr(nfsd, vap, fp);
2729 nfsrv_rmdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2730 struct thread *td, struct mbuf **mrq)
2732 struct sockaddr *nam = nfsd->nd_nam;
2733 struct ucred *cred = &nfsd->nd_cr;
2734 int error = 0, len, dirfor_ret = 1, diraft_ret = 1;
2738 struct vattr dirfor, diraft;
2741 struct nlookupdata nd;
2742 struct nfsm_info info;
2744 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2750 info.mrep = nfsd->nd_mrep;
2752 info.md = nfsd->nd_md;
2753 info.dpos = nfsd->nd_dpos;
2754 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2756 fhp = &nfh.fh_generic;
2757 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2758 NEGREPLYOUT(len = nfsm_srvnamesiz(&info, &error));
2760 error = nfs_namei(&nd, cred, NLC_DELETE, &dvp, &vp,
2761 fhp, len, slp, nam, &info.md, &info.dpos, &dirp,
2762 td, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2765 dirfor_ret = VOP_GETATTR(dirp, &dirfor);
2768 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2769 NFSX_WCCDATA(info.v3), &error));
2770 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2771 diraft_ret, &diraft);
2775 if (vp->v_type != VDIR) {
2781 * The root of a mounted filesystem cannot be deleted.
2783 if (vp->v_flag & VROOT)
2787 * Issue or abort op. Since SAVESTART is not set, path name
2788 * component is freed by the VOP after either.
2795 error = VOP_NRMDIR(&nd.nl_nch, dvp, nd.nl_cred);
2802 diraft_ret = VOP_GETATTR(dirp, &diraft);
2803 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_WCCDATA(info.v3), &error));
2805 nfsm_srvwcc_data(&info, nfsd, dirfor_ret, &dirfor,
2806 diraft_ret, &diraft);
2828 * nfs readdir service
2829 * - mallocs what it thinks is enough to read
2830 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2831 * - calls VOP_READDIR()
2832 * - loops around building the reply
2833 * if the output generated exceeds count break out of loop
2834 * The nfsm_clget macro is used here so that the reply will be packed
2835 * tightly in mbuf clusters.
2836 * - it only knows that it has encountered eof when the VOP_READDIR()
2838 * - as such one readdir rpc will return eof false although you are there
2839 * and then the next will return eof
2840 * - it trims out records with d_fileno == 0
2841 * this doesn't matter for Unix clients, but they might confuse clients
2843 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2844 * than requested, but this may not apply to all filesystems. For
2845 * example, client NFS does not { although it is never remote mounted
2847 * The alternate call nfsrv_readdirplus() does lookups as well.
2848 * PS: The NFS protocol spec. does not clarify what the "count" byte
2849 * argument is a count of.. just name strings and file id's or the
2850 * entire reply rpc or ...
2851 * I tried just file name and id sizes and it confused the Sun client,
2852 * so I am using the full rpc size now. The "paranoia.." comment refers
2853 * to including the status longwords that are not a part of the dir.
2854 * "entry" structures, but are in the rpc.
2858 u_int32_t fl_postopok;
2859 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2861 u_int32_t fl_fhsize;
2862 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2866 nfsrv_readdir(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
2867 struct thread *td, struct mbuf **mrq)
2869 struct sockaddr *nam = nfsd->nd_nam;
2870 struct ucred *cred = &nfsd->nd_cr;
2875 struct mbuf *mp1, *mp2;
2876 char *cpos, *cend, *rbuf;
2877 struct vnode *vp = NULL;
2878 struct mount *mp = NULL;
2884 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2885 int siz, cnt, fullsiz, eofflag, rdonly, ncookies;
2886 u_quad_t off, toff, verf;
2887 off_t *cookies = NULL, *cookiep;
2888 struct nfsm_info info;
2890 info.mrep = nfsd->nd_mrep;
2892 info.md = nfsd->nd_md;
2893 info.dpos = nfsd->nd_dpos;
2894 info.v3 = (nfsd->nd_flag & ND_NFSV3);
2896 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
2897 fhp = &nfh.fh_generic;
2898 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
2900 NULLOUT(tl = nfsm_dissect(&info, 5 * NFSX_UNSIGNED));
2901 toff = fxdr_hyper(tl);
2903 verf = fxdr_hyper(tl);
2906 NULLOUT(tl = nfsm_dissect(&info, 2 * NFSX_UNSIGNED));
2907 toff = fxdr_unsigned(u_quad_t, *tl++);
2908 verf = 0; /* shut up gcc */
2911 cnt = fxdr_unsigned(int, *tl);
2912 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2913 xfer = NFS_SRVMAXDATA(nfsd);
2919 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
2920 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
2921 if (!error && vp->v_type != VDIR) {
2927 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
2928 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2934 * Obtain lock on vnode for this section of the code
2938 error = getret = VOP_GETATTR(vp, &at);
2941 * XXX This check may be too strict for Solaris 2.5 clients.
2943 if (!error && toff && verf && verf != at.va_filerev)
2944 error = NFSERR_BAD_COOKIE;
2948 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
2952 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2953 NFSX_POSTOPATTR(info.v3), &error));
2954 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
2961 * end section. Allocate rbuf and continue
2963 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2966 iv.iov_len = fullsiz;
2969 io.uio_offset = (off_t)off;
2970 io.uio_resid = fullsiz;
2971 io.uio_segflg = UIO_SYSSPACE;
2972 io.uio_rw = UIO_READ;
2976 kfree((caddr_t)cookies, M_TEMP);
2979 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2980 off = (off_t)io.uio_offset;
2981 if (!cookies && !error)
2982 error = NFSERR_PERM;
2984 getret = VOP_GETATTR(vp, &at);
2991 kfree((caddr_t)rbuf, M_TEMP);
2993 kfree((caddr_t)cookies, M_TEMP);
2994 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
2995 NFSX_POSTOPATTR(info.v3), &error));
2996 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3001 siz -= io.uio_resid;
3004 * If nothing read, return eof
3010 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3011 NFSX_POSTOPATTR(info.v3) +
3012 NFSX_COOKIEVERF(info.v3) +
3016 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3017 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3018 txdr_hyper(at.va_filerev, tl);
3021 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3024 FREE((caddr_t)rbuf, M_TEMP);
3025 FREE((caddr_t)cookies, M_TEMP);
3032 * Check for degenerate cases of nothing useful read.
3033 * If so go try again
3037 dp = (struct dirent *)cpos;
3040 * For some reason FreeBSD's ufs_readdir() chooses to back the
3041 * directory offset up to a block boundary, so it is necessary to
3042 * skip over the records that preceed the requested offset. This
3043 * requires the assumption that file offset cookies monotonically
3046 while (cpos < cend && ncookies > 0 &&
3047 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3048 ((u_quad_t)(*cookiep)) <= toff)) {
3049 dp = _DIRENT_NEXT(dp);
3054 if (cpos >= cend || ncookies == 0) {
3060 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
3061 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3062 NFSX_POSTOPATTR(info.v3) +
3063 NFSX_COOKIEVERF(info.v3) + siz,
3066 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3067 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3068 txdr_hyper(at.va_filerev, tl);
3070 mp1 = mp2 = info.mb;
3072 be = bp + M_TRAILINGSPACE(mp1);
3074 /* Loop through the records and build reply */
3075 while (cpos < cend && ncookies > 0) {
3076 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3077 nlen = dp->d_namlen;
3078 rem = nfsm_rndup(nlen) - nlen;
3079 len += (4 * NFSX_UNSIGNED + nlen + rem);
3081 len += 2 * NFSX_UNSIGNED;
3087 * Build the directory record xdr from
3090 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3092 bp += NFSX_UNSIGNED;
3094 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3096 bp += NFSX_UNSIGNED;
3098 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3099 *tl = txdr_unsigned(dp->d_ino);
3100 bp += NFSX_UNSIGNED;
3101 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3102 *tl = txdr_unsigned(nlen);
3103 bp += NFSX_UNSIGNED;
3105 /* And loop around copying the name */
3109 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3114 bcopy(cp, bp, tsiz);
3120 /* And null pad to a int32_t boundary */
3121 for (i = 0; i < rem; i++)
3123 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3125 /* Finish off the record */
3127 *tl = txdr_unsigned(*cookiep >> 32);
3128 bp += NFSX_UNSIGNED;
3129 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3131 *tl = txdr_unsigned(*cookiep);
3132 bp += NFSX_UNSIGNED;
3134 dp = _DIRENT_NEXT(dp);
3141 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3143 bp += NFSX_UNSIGNED;
3144 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3149 bp += NFSX_UNSIGNED;
3150 if (mp1 != info.mb) {
3152 mp1->m_len = bp - mtod(mp1, caddr_t);
3154 mp1->m_len += bp - info.bpos;
3155 FREE((caddr_t)rbuf, M_TEMP);
3156 FREE((caddr_t)cookies, M_TEMP);
3166 nfsrv_readdirplus(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3167 struct thread *td, struct mbuf **mrq)
3169 struct sockaddr *nam = nfsd->nd_nam;
3170 struct ucred *cred = &nfsd->nd_cr;
3175 struct mbuf *mp1, *mp2;
3176 char *cpos, *cend, *rbuf;
3177 struct vnode *vp = NULL, *nvp;
3178 struct mount *mp = NULL;
3181 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
3184 struct vattr va, at, *vap = &va;
3185 struct nfs_fattr *fp;
3186 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
3187 int siz, cnt, fullsiz, eofflag, rdonly, dirlen, ncookies;
3188 u_quad_t off, toff, verf;
3189 off_t *cookies = NULL, *cookiep; /* needs to be int64_t or off_t */
3190 struct nfsm_info info;
3192 info.mrep = nfsd->nd_mrep;
3194 info.md = nfsd->nd_md;
3195 info.dpos = nfsd->nd_dpos;
3197 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3198 fhp = &nfh.fh_generic;
3199 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3200 NULLOUT(tl = nfsm_dissect(&info, 6 * NFSX_UNSIGNED));
3201 toff = fxdr_hyper(tl);
3203 verf = fxdr_hyper(tl);
3205 siz = fxdr_unsigned(int, *tl++);
3206 cnt = fxdr_unsigned(int, *tl);
3208 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
3209 xfer = NFS_SRVMAXDATA(nfsd);
3215 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3216 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3217 if (!error && vp->v_type != VDIR) {
3223 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3224 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3228 error = getret = VOP_GETATTR(vp, &at);
3231 * XXX This check may be too strict for Solaris 2.5 clients.
3233 if (!error && toff && verf && verf != at.va_filerev)
3234 error = NFSERR_BAD_COOKIE;
3237 error = nfsrv_access(mp, vp, VEXEC, cred, rdonly, td, 0);
3242 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3243 NFSX_V3POSTOPATTR, &error));
3244 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3249 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
3252 iv.iov_len = fullsiz;
3255 io.uio_offset = (off_t)off;
3256 io.uio_resid = fullsiz;
3257 io.uio_segflg = UIO_SYSSPACE;
3258 io.uio_rw = UIO_READ;
3262 kfree((caddr_t)cookies, M_TEMP);
3265 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
3266 off = (u_quad_t)io.uio_offset;
3267 getret = VOP_GETATTR(vp, &at);
3268 if (!cookies && !error)
3269 error = NFSERR_PERM;
3276 kfree((caddr_t)cookies, M_TEMP);
3277 kfree((caddr_t)rbuf, M_TEMP);
3278 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3279 NFSX_V3POSTOPATTR, &error));
3280 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3285 siz -= io.uio_resid;
3288 * If nothing read, return eof
3294 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3299 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3300 tl = nfsm_build(&info, 4 * NFSX_UNSIGNED);
3301 txdr_hyper(at.va_filerev, tl);
3305 FREE((caddr_t)cookies, M_TEMP);
3306 FREE((caddr_t)rbuf, M_TEMP);
3313 * Check for degenerate cases of nothing useful read.
3314 * If so go try again
3318 dp = (struct dirent *)cpos;
3321 * For some reason FreeBSD's ufs_readdir() chooses to back the
3322 * directory offset up to a block boundary, so it is necessary to
3323 * skip over the records that preceed the requested offset. This
3324 * requires the assumption that file offset cookies monotonically
3327 while (cpos < cend && ncookies > 0 &&
3328 (dp->d_ino == 0 || dp->d_type == DT_WHT ||
3329 ((u_quad_t)(*cookiep)) <= toff)) {
3330 dp = _DIRENT_NEXT(dp);
3335 if (cpos >= cend || ncookies == 0) {
3342 * Probe one of the directory entries to see if the filesystem
3345 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp) == EOPNOTSUPP) {
3346 error = NFSERR_NOTSUPP;
3349 kfree((caddr_t)cookies, M_TEMP);
3350 kfree((caddr_t)rbuf, M_TEMP);
3351 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3352 NFSX_V3POSTOPATTR, &error));
3353 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3362 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
3364 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, cnt, &error));
3365 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3366 tl = nfsm_build(&info, 2 * NFSX_UNSIGNED);
3367 txdr_hyper(at.va_filerev, tl);
3368 mp1 = mp2 = info.mb;
3370 be = bp + M_TRAILINGSPACE(mp1);
3372 /* Loop through the records and build reply */
3373 while (cpos < cend && ncookies > 0) {
3374 if (dp->d_ino != 0 && dp->d_type != DT_WHT) {
3375 nlen = dp->d_namlen;
3376 rem = nfsm_rndup(nlen)-nlen;
3379 * For readdir_and_lookup get the vnode using
3382 if (VFS_VGET(vp->v_mount, dp->d_ino, &nvp))
3384 bzero((caddr_t)nfhp, NFSX_V3FH);
3385 nfhp->fh_fsid = fhp->fh_fsid;
3386 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3391 if (VOP_GETATTR(nvp, vap)) {
3400 * If either the dircount or maxcount will be
3401 * exceeded, get out now. Both of these lengths
3402 * are calculated conservatively, including all
3405 len += (8 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3407 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3408 if (len > cnt || dirlen > fullsiz) {
3414 * Build the directory record xdr from
3417 fp = (struct nfs_fattr *)&fl.fl_fattr;
3418 nfsm_srvfattr(nfsd, vap, fp);
3419 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3420 fl.fl_fhok = nfs_true;
3421 fl.fl_postopok = nfs_true;
3422 fl.fl_off.nfsuquad[0] = txdr_unsigned(*cookiep >> 32);
3423 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3425 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3427 bp += NFSX_UNSIGNED;
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);
3433 bp += NFSX_UNSIGNED;
3434 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3435 *tl = txdr_unsigned(nlen);
3436 bp += NFSX_UNSIGNED;
3438 /* And loop around copying the name */
3442 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3443 if ((bp + xfer) > be)
3447 bcopy(cp, bp, tsiz);
3453 /* And null pad to a int32_t boundary */
3454 for (i = 0; i < rem; i++)
3458 * Now copy the flrep structure out.
3460 xfer = sizeof (struct flrep);
3463 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3464 if ((bp + xfer) > be)
3468 bcopy(cp, bp, tsiz);
3476 dp = _DIRENT_NEXT(dp);
3483 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3485 bp += NFSX_UNSIGNED;
3486 tl = nfsm_clget(&info, mp1, mp2, bp, be);
3491 bp += NFSX_UNSIGNED;
3492 if (mp1 != info.mb) {
3494 mp1->m_len = bp - mtod(mp1, caddr_t);
3496 mp1->m_len += bp - info.bpos;
3497 FREE((caddr_t)cookies, M_TEMP);
3498 FREE((caddr_t)rbuf, M_TEMP);
3507 * nfs commit service
3510 nfsrv_commit(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3511 struct thread *td, struct mbuf **mrq)
3513 struct sockaddr *nam = nfsd->nd_nam;
3514 struct ucred *cred = &nfsd->nd_cr;
3515 struct vattr bfor, aft;
3516 struct vnode *vp = NULL;
3517 struct mount *mp = NULL;
3521 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt;
3523 struct nfsm_info info;
3525 info.mrep = nfsd->nd_mrep;
3527 info.md = nfsd->nd_md;
3528 info.dpos = nfsd->nd_dpos;
3530 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3531 fhp = &nfh.fh_generic;
3532 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3533 NULLOUT(tl = nfsm_dissect(&info, 3 * NFSX_UNSIGNED));
3536 * XXX At this time VOP_FSYNC() does not accept offset and byte
3537 * count parameters, so these arguments are useless (someday maybe).
3539 off = fxdr_hyper(tl);
3541 cnt = fxdr_unsigned(int, *tl);
3542 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3543 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3545 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3546 2 * NFSX_UNSIGNED, &error));
3547 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3552 for_ret = VOP_GETATTR(vp, &bfor);
3554 if (cnt > MAX_COMMIT_COUNT) {
3556 * Give up and do the whole thing
3559 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3560 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
3562 error = VOP_FSYNC(vp, MNT_WAIT);
3565 * Locate and synchronously write any buffers that fall
3566 * into the requested range. Note: we are assuming that
3567 * f_iosize is a power of 2.
3569 int iosize = vp->v_mount->mnt_stat.f_iosize;
3570 int iomask = iosize - 1;
3574 * Align to iosize boundry, super-align to page boundry.
3577 cnt += off & iomask;
3578 off &= ~(u_quad_t)iomask;
3580 if (off & PAGE_MASK) {
3581 cnt += off & PAGE_MASK;
3582 off &= ~(u_quad_t)PAGE_MASK;
3587 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
3588 vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
3596 * If we have a buffer and it is marked B_DELWRI we
3597 * have to lock and write it. Otherwise the prior
3598 * write is assumed to have already been committed.
3600 if ((bp = findblk(vp, loffset, FINDBLK_TEST)) != NULL) {
3601 if (bp->b_flags & B_DELWRI)
3602 bp = findblk(vp, loffset, 0);
3607 if (bp->b_flags & B_DELWRI) {
3624 aft_ret = VOP_GETATTR(vp, &aft);
3627 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3628 NFSX_V3WCCDATA + NFSX_V3WRITEVERF,
3630 nfsm_srvwcc_data(&info, nfsd, for_ret, &bfor,
3633 tl = nfsm_build(&info, NFSX_V3WRITEVERF);
3634 if (nfsver.tv_sec == 0)
3636 *tl++ = txdr_unsigned(nfsver.tv_sec);
3637 *tl = txdr_unsigned(nfsver.tv_nsec / 1000);
3649 * nfs statfs service
3652 nfsrv_statfs(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3653 struct thread *td, struct mbuf **mrq)
3655 struct sockaddr *nam = nfsd->nd_nam;
3656 struct ucred *cred = &nfsd->nd_cr;
3658 struct nfs_statfs *sfp;
3659 int error = 0, rdonly, getret = 1;
3660 struct vnode *vp = NULL;
3661 struct mount *mp = NULL;
3665 struct statfs statfs;
3667 struct nfsm_info info;
3669 info.mrep = nfsd->nd_mrep;
3671 info.md = nfsd->nd_md;
3672 info.dpos = nfsd->nd_dpos;
3673 info.v3 = (nfsd->nd_flag & ND_NFSV3);
3675 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3676 fhp = &nfh.fh_generic;
3677 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3678 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3679 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3681 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3682 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3687 error = VFS_STATFS(vp->v_mount, sf, proc0.p_ucred);
3688 getret = VOP_GETATTR(vp, &at);
3691 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3692 NFSX_POSTOPATTR(info.v3) + NFSX_STATFS(info.v3),
3695 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3700 sfp = nfsm_build(&info, NFSX_STATFS(info.v3));
3702 tval = (u_quad_t)sf->f_blocks;
3703 tval *= (u_quad_t)sf->f_bsize;
3704 txdr_hyper(tval, &sfp->sf_tbytes);
3705 tval = (u_quad_t)sf->f_bfree;
3706 tval *= (u_quad_t)sf->f_bsize;
3707 txdr_hyper(tval, &sfp->sf_fbytes);
3708 tval = (u_quad_t)sf->f_bavail;
3709 tval *= (u_quad_t)sf->f_bsize;
3710 txdr_hyper(tval, &sfp->sf_abytes);
3711 sfp->sf_tfiles.nfsuquad[0] = 0;
3712 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3713 sfp->sf_ffiles.nfsuquad[0] = 0;
3714 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3715 sfp->sf_afiles.nfsuquad[0] = 0;
3716 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3717 sfp->sf_invarsec = 0;
3719 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3720 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3721 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3722 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3723 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3733 * nfs fsinfo service
3736 nfsrv_fsinfo(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3737 struct thread *td, struct mbuf **mrq)
3739 struct sockaddr *nam = nfsd->nd_nam;
3740 struct ucred *cred = &nfsd->nd_cr;
3741 struct nfsv3_fsinfo *sip;
3742 int error = 0, rdonly, getret = 1, pref;
3743 struct vnode *vp = NULL;
3744 struct mount *mp = NULL;
3750 struct nfsm_info info;
3752 info.mrep = nfsd->nd_mrep;
3754 info.md = nfsd->nd_md;
3755 info.dpos = nfsd->nd_dpos;
3757 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3758 fhp = &nfh.fh_generic;
3759 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3760 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3761 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3763 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3764 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3769 /* XXX Try to make a guess on the max file size. */
3770 VFS_STATFS(vp->v_mount, &sb, proc0.p_ucred);
3771 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3773 getret = VOP_GETATTR(vp, &at);
3776 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3777 NFSX_V3POSTOPATTR + NFSX_V3FSINFO, &error));
3778 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3779 sip = nfsm_build(&info, NFSX_V3FSINFO);
3783 * There should be file system VFS OP(s) to get this information.
3784 * For now, assume ufs.
3786 if (slp->ns_so->so_type == SOCK_DGRAM)
3787 pref = NFS_MAXDGRAMDATA;
3790 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3791 sip->fs_rtpref = txdr_unsigned(pref);
3792 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3793 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3794 sip->fs_wtpref = txdr_unsigned(pref);
3795 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3796 sip->fs_dtpref = txdr_unsigned(pref);
3797 txdr_hyper(maxfsize, &sip->fs_maxfilesize);
3798 sip->fs_timedelta.nfsv3_sec = 0;
3799 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3800 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3801 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3802 NFSV3FSINFO_CANSETTIME);
3811 * nfs pathconf service
3814 nfsrv_pathconf(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3815 struct thread *td, struct mbuf **mrq)
3817 struct sockaddr *nam = nfsd->nd_nam;
3818 struct ucred *cred = &nfsd->nd_cr;
3819 struct nfsv3_pathconf *pc;
3820 int error = 0, rdonly, getret = 1;
3821 register_t linkmax, namemax, chownres, notrunc;
3822 struct vnode *vp = NULL;
3823 struct mount *mp = NULL;
3827 struct nfsm_info info;
3829 info.mrep = nfsd->nd_mrep;
3831 info.md = nfsd->nd_md;
3832 info.dpos = nfsd->nd_dpos;
3834 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3835 fhp = &nfh.fh_generic;
3836 NEGREPLYOUT(nfsm_srvmtofh(&info, nfsd, fhp, &error));
3837 error = nfsrv_fhtovp(fhp, 1, &mp, &vp, cred, slp, nam,
3838 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE);
3840 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, NFSX_UNSIGNED, &error));
3841 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3845 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3847 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3849 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3851 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3852 getret = VOP_GETATTR(vp, &at);
3855 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp,
3856 NFSX_V3POSTOPATTR + NFSX_V3PATHCONF,
3858 nfsm_srvpostop_attr(&info, nfsd, getret, &at);
3863 pc = nfsm_build(&info, NFSX_V3PATHCONF);
3865 pc->pc_linkmax = txdr_unsigned(linkmax);
3866 pc->pc_namemax = txdr_unsigned(namemax);
3867 pc->pc_notrunc = txdr_unsigned(notrunc);
3868 pc->pc_chownrestricted = txdr_unsigned(chownres);
3871 * These should probably be supported by VOP_PATHCONF(), but
3872 * until msdosfs is exportable (why would you want to?), the
3873 * Unix defaults should be ok.
3875 pc->pc_caseinsensitive = nfs_false;
3876 pc->pc_casepreserving = nfs_true;
3885 * Null operation, used by clients to ping server
3889 nfsrv_null(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3890 struct thread *td, struct mbuf **mrq)
3892 struct nfsm_info info;
3893 int error = NFSERR_RETVOID;
3895 info.mrep = nfsd->nd_mrep;
3898 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3899 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3906 * No operation, used for obsolete procedures
3910 nfsrv_noop(struct nfsrv_descript *nfsd, struct nfssvc_sock *slp,
3911 struct thread *td, struct mbuf **mrq)
3913 struct nfsm_info info;
3916 info.mrep = nfsd->nd_mrep;
3919 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3920 if (nfsd->nd_repstat)
3921 error = nfsd->nd_repstat;
3923 error = EPROCUNAVAIL;
3924 NEGKEEPOUT(nfsm_reply(&info, nfsd, slp, 0, &error));
3932 * Perform access checking for vnodes obtained from file handles that would
3933 * refer to files already opened by a Unix client. You cannot just use
3934 * vn_writechk() and VOP_ACCESS() for two reasons.
3935 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3936 * 2 - The owner is to be given access irrespective of mode bits for some
3937 * operations, so that processes that chmod after opening a file don't
3938 * break. I don't like this because it opens a security hole, but since
3939 * the nfs server opens a security hole the size of a barn door anyhow,
3942 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3943 * will return EPERM instead of EACCESS. EPERM is always an error.
3946 nfsrv_access(struct mount *mp, struct vnode *vp, int flags, struct ucred *cred,
3947 int rdonly, struct thread *td, int override)
3952 nfsdbprintf(("%s %d\n", __FILE__, __LINE__));
3953 if (flags & VWRITE) {
3954 /* Just vn_writechk() changed to check rdonly */
3956 * Disallow write attempts on read-only file systems;
3957 * unless the file is a socket or a block or character
3958 * device resident on the file system.
3961 ((mp->mnt_flag | vp->v_mount->mnt_flag) & MNT_RDONLY)) {
3962 switch (vp->v_type) {
3972 * If there's shared text associated with
3973 * the inode, we can't allow writing.
3975 if (vp->v_flag & VTEXT)
3978 error = VOP_GETATTR(vp, &vattr);
3981 error = VOP_ACCESS(vp, flags, cred);
3983 * Allow certain operations for the owner (reads and writes
3984 * on files that are already open).
3986 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3990 #endif /* NFS_NOSERVER */