nfs_serv.c revision 1.15 1 1.1 cgd /*
2 1.15 mycroft * Copyright (c) 1989, 1993
3 1.15 mycroft * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Rick Macklem at The University of Guelph.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.15 mycroft * from: @(#)nfs_serv.c 8.3 (Berkeley) 1/12/94
37 1.15 mycroft * $Id: nfs_serv.c,v 1.15 1994/06/08 11:36:55 mycroft Exp $
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * nfs version 2 server calls to vnode ops
42 1.1 cgd * - these routines generally have 3 phases
43 1.1 cgd * 1 - break down and validate rpc request in mbuf list
44 1.1 cgd * 2 - do the vnode ops for the request
45 1.1 cgd * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
46 1.1 cgd * 3 - build the rpc reply in an mbuf list
47 1.1 cgd * nb:
48 1.1 cgd * - do not mix the phases, since the nfsm_?? macros can return failures
49 1.1 cgd * on a bad rpc or similar and do not do any vrele() or vput()'s
50 1.1 cgd *
51 1.1 cgd * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
52 1.1 cgd * error number iff error != 0 whereas
53 1.1 cgd * returning an error from the server function implies a fatal error
54 1.1 cgd * such as a badly constructed rpc request that should be dropped without
55 1.1 cgd * a reply.
56 1.1 cgd */
57 1.1 cgd
58 1.10 mycroft #include <sys/param.h>
59 1.10 mycroft #include <sys/systm.h>
60 1.10 mycroft #include <sys/proc.h>
61 1.10 mycroft #include <sys/file.h>
62 1.10 mycroft #include <sys/namei.h>
63 1.10 mycroft #include <sys/vnode.h>
64 1.10 mycroft #include <sys/mount.h>
65 1.10 mycroft #include <sys/mbuf.h>
66 1.15 mycroft #include <sys/dirent.h>
67 1.15 mycroft #include <sys/stat.h>
68 1.1 cgd
69 1.15 mycroft #include <vm/vm.h>
70 1.1 cgd
71 1.10 mycroft #include <nfs/nfsv2.h>
72 1.15 mycroft #include <nfs/rpcv2.h>
73 1.10 mycroft #include <nfs/nfs.h>
74 1.10 mycroft #include <nfs/xdr_subs.h>
75 1.10 mycroft #include <nfs/nfsm_subs.h>
76 1.15 mycroft #include <nfs/nqnfs.h>
77 1.1 cgd
78 1.1 cgd /* Defs */
79 1.1 cgd #define TRUE 1
80 1.1 cgd #define FALSE 0
81 1.1 cgd
82 1.1 cgd /* Global vars */
83 1.1 cgd extern u_long nfs_procids[NFS_NPROCS];
84 1.1 cgd extern u_long nfs_xdrneg1;
85 1.1 cgd extern u_long nfs_false, nfs_true;
86 1.15 mycroft nfstype nfs_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
87 1.1 cgd NFCHR, NFNON };
88 1.1 cgd
89 1.15 mycroft /*
90 1.15 mycroft * nqnfs access service
91 1.15 mycroft */
92 1.15 mycroft nqnfsrv_access(nfsd, mrep, md, dpos, cred, nam, mrq)
93 1.15 mycroft struct nfsd *nfsd;
94 1.15 mycroft struct mbuf *mrep, *md;
95 1.15 mycroft caddr_t dpos;
96 1.15 mycroft struct ucred *cred;
97 1.15 mycroft struct mbuf *nam, **mrq;
98 1.15 mycroft {
99 1.15 mycroft struct vnode *vp;
100 1.15 mycroft nfsv2fh_t nfh;
101 1.15 mycroft fhandle_t *fhp;
102 1.15 mycroft register u_long *tl;
103 1.15 mycroft register long t1;
104 1.15 mycroft caddr_t bpos;
105 1.15 mycroft int error = 0, rdonly, cache, mode = 0;
106 1.15 mycroft char *cp2;
107 1.15 mycroft struct mbuf *mb, *mreq;
108 1.15 mycroft u_quad_t frev;
109 1.15 mycroft
110 1.15 mycroft fhp = &nfh.fh_generic;
111 1.15 mycroft nfsm_srvmtofh(fhp);
112 1.15 mycroft nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
113 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
114 1.15 mycroft nfsm_reply(0);
115 1.15 mycroft if (*tl++ == nfs_true)
116 1.15 mycroft mode |= VREAD;
117 1.15 mycroft if (*tl++ == nfs_true)
118 1.15 mycroft mode |= VWRITE;
119 1.15 mycroft if (*tl == nfs_true)
120 1.15 mycroft mode |= VEXEC;
121 1.15 mycroft error = nfsrv_access(vp, mode, cred, rdonly, nfsd->nd_procp);
122 1.15 mycroft vput(vp);
123 1.15 mycroft nfsm_reply(0);
124 1.15 mycroft nfsm_srvdone;
125 1.15 mycroft }
126 1.15 mycroft
127 1.1 cgd /*
128 1.1 cgd * nfs getattr service
129 1.1 cgd */
130 1.15 mycroft nfsrv_getattr(nfsd, mrep, md, dpos, cred, nam, mrq)
131 1.15 mycroft struct nfsd *nfsd;
132 1.1 cgd struct mbuf *mrep, *md;
133 1.1 cgd caddr_t dpos;
134 1.1 cgd struct ucred *cred;
135 1.15 mycroft struct mbuf *nam, **mrq;
136 1.1 cgd {
137 1.1 cgd register struct nfsv2_fattr *fp;
138 1.1 cgd struct vattr va;
139 1.1 cgd register struct vattr *vap = &va;
140 1.1 cgd struct vnode *vp;
141 1.1 cgd nfsv2fh_t nfh;
142 1.1 cgd fhandle_t *fhp;
143 1.1 cgd register u_long *tl;
144 1.1 cgd register long t1;
145 1.1 cgd caddr_t bpos;
146 1.15 mycroft int error = 0, rdonly, cache;
147 1.1 cgd char *cp2;
148 1.1 cgd struct mbuf *mb, *mb2, *mreq;
149 1.15 mycroft u_quad_t frev;
150 1.1 cgd
151 1.1 cgd fhp = &nfh.fh_generic;
152 1.1 cgd nfsm_srvmtofh(fhp);
153 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
154 1.1 cgd nfsm_reply(0);
155 1.15 mycroft nqsrv_getl(vp, NQL_READ);
156 1.15 mycroft error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
157 1.1 cgd vput(vp);
158 1.15 mycroft nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
159 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
160 1.1 cgd nfsm_srvfillattr;
161 1.1 cgd nfsm_srvdone;
162 1.1 cgd }
163 1.1 cgd
164 1.1 cgd /*
165 1.1 cgd * nfs setattr service
166 1.1 cgd */
167 1.15 mycroft nfsrv_setattr(nfsd, mrep, md, dpos, cred, nam, mrq)
168 1.15 mycroft struct nfsd *nfsd;
169 1.1 cgd struct mbuf *mrep, *md;
170 1.1 cgd caddr_t dpos;
171 1.1 cgd struct ucred *cred;
172 1.15 mycroft struct mbuf *nam, **mrq;
173 1.1 cgd {
174 1.1 cgd struct vattr va;
175 1.1 cgd register struct vattr *vap = &va;
176 1.1 cgd register struct nfsv2_sattr *sp;
177 1.1 cgd register struct nfsv2_fattr *fp;
178 1.1 cgd struct vnode *vp;
179 1.1 cgd nfsv2fh_t nfh;
180 1.1 cgd fhandle_t *fhp;
181 1.1 cgd register u_long *tl;
182 1.1 cgd register long t1;
183 1.1 cgd caddr_t bpos;
184 1.15 mycroft int error = 0, rdonly, cache;
185 1.1 cgd char *cp2;
186 1.1 cgd struct mbuf *mb, *mb2, *mreq;
187 1.15 mycroft u_quad_t frev, frev2;
188 1.1 cgd
189 1.1 cgd fhp = &nfh.fh_generic;
190 1.1 cgd nfsm_srvmtofh(fhp);
191 1.15 mycroft nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
192 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
193 1.1 cgd nfsm_reply(0);
194 1.15 mycroft nqsrv_getl(vp, NQL_WRITE);
195 1.1 cgd VATTR_NULL(vap);
196 1.1 cgd /*
197 1.1 cgd * Nah nah nah nah na nah
198 1.1 cgd * There is a bug in the Sun client that puts 0xffff in the mode
199 1.1 cgd * field of sattr when it should put in 0xffffffff. The u_short
200 1.1 cgd * doesn't sign extend.
201 1.1 cgd * --> check the low order 2 bytes for 0xffff
202 1.1 cgd */
203 1.1 cgd if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
204 1.1 cgd vap->va_mode = nfstov_mode(sp->sa_mode);
205 1.1 cgd if (sp->sa_uid != nfs_xdrneg1)
206 1.1 cgd vap->va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
207 1.1 cgd if (sp->sa_gid != nfs_xdrneg1)
208 1.1 cgd vap->va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
209 1.15 mycroft if (nfsd->nd_nqlflag == NQL_NOVAL) {
210 1.15 mycroft if (sp->sa_nfssize != nfs_xdrneg1)
211 1.15 mycroft vap->va_size = fxdr_unsigned(u_quad_t, sp->sa_nfssize);
212 1.15 mycroft if (sp->sa_nfsatime.nfs_sec != nfs_xdrneg1) {
213 1.15 mycroft #ifdef notyet
214 1.15 mycroft fxdr_nfstime(&sp->sa_nfsatime, &vap->va_atime);
215 1.15 mycroft #else
216 1.15 mycroft vap->va_atime.ts_sec =
217 1.15 mycroft fxdr_unsigned(long, sp->sa_nfsatime.nfs_sec);
218 1.15 mycroft vap->va_atime.ts_nsec = 0;
219 1.15 mycroft #endif
220 1.15 mycroft }
221 1.15 mycroft if (sp->sa_nfsmtime.nfs_sec != nfs_xdrneg1)
222 1.15 mycroft fxdr_nfstime(&sp->sa_nfsmtime, &vap->va_mtime);
223 1.15 mycroft } else {
224 1.15 mycroft fxdr_hyper(&sp->sa_nqsize, &vap->va_size);
225 1.15 mycroft fxdr_nqtime(&sp->sa_nqatime, &vap->va_atime);
226 1.15 mycroft fxdr_nqtime(&sp->sa_nqmtime, &vap->va_mtime);
227 1.15 mycroft vap->va_flags = fxdr_unsigned(u_long, sp->sa_nqflags);
228 1.15 mycroft }
229 1.15 mycroft
230 1.1 cgd /*
231 1.15 mycroft * If the size is being changed write acces is required, otherwise
232 1.15 mycroft * just check for a read only file system.
233 1.1 cgd */
234 1.15 mycroft if (vap->va_size == ((u_quad_t)((quad_t) -1))) {
235 1.15 mycroft if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
236 1.15 mycroft error = EROFS;
237 1.15 mycroft goto out;
238 1.15 mycroft }
239 1.15 mycroft } else {
240 1.15 mycroft if (vp->v_type == VDIR) {
241 1.15 mycroft error = EISDIR;
242 1.15 mycroft goto out;
243 1.15 mycroft } else if (error = nfsrv_access(vp, VWRITE, cred, rdonly,
244 1.15 mycroft nfsd->nd_procp))
245 1.15 mycroft goto out;
246 1.1 cgd }
247 1.15 mycroft if (error = VOP_SETATTR(vp, vap, cred, nfsd->nd_procp)) {
248 1.1 cgd vput(vp);
249 1.1 cgd nfsm_reply(0);
250 1.1 cgd }
251 1.15 mycroft error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
252 1.1 cgd out:
253 1.1 cgd vput(vp);
254 1.15 mycroft nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL) + 2*NFSX_UNSIGNED);
255 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
256 1.1 cgd nfsm_srvfillattr;
257 1.15 mycroft if (nfsd->nd_nqlflag != NQL_NOVAL) {
258 1.15 mycroft nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
259 1.15 mycroft txdr_hyper(&frev2, tl);
260 1.15 mycroft }
261 1.1 cgd nfsm_srvdone;
262 1.1 cgd }
263 1.1 cgd
264 1.1 cgd /*
265 1.1 cgd * nfs lookup rpc
266 1.1 cgd */
267 1.15 mycroft nfsrv_lookup(nfsd, mrep, md, dpos, cred, nam, mrq)
268 1.15 mycroft struct nfsd *nfsd;
269 1.1 cgd struct mbuf *mrep, *md;
270 1.1 cgd caddr_t dpos;
271 1.1 cgd struct ucred *cred;
272 1.15 mycroft struct mbuf *nam, **mrq;
273 1.1 cgd {
274 1.1 cgd register struct nfsv2_fattr *fp;
275 1.1 cgd struct nameidata nd;
276 1.1 cgd struct vnode *vp;
277 1.1 cgd nfsv2fh_t nfh;
278 1.1 cgd fhandle_t *fhp;
279 1.1 cgd register caddr_t cp;
280 1.1 cgd register u_long *tl;
281 1.1 cgd register long t1;
282 1.1 cgd caddr_t bpos;
283 1.15 mycroft int error = 0, cache, duration2, cache2, len;
284 1.1 cgd char *cp2;
285 1.1 cgd struct mbuf *mb, *mb2, *mreq;
286 1.1 cgd struct vattr va, *vap = &va;
287 1.15 mycroft u_quad_t frev, frev2;
288 1.1 cgd
289 1.1 cgd fhp = &nfh.fh_generic;
290 1.15 mycroft duration2 = 0;
291 1.15 mycroft if (nfsd->nd_nqlflag != NQL_NOVAL) {
292 1.15 mycroft nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
293 1.15 mycroft duration2 = fxdr_unsigned(int, *tl);
294 1.15 mycroft }
295 1.1 cgd nfsm_srvmtofh(fhp);
296 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
297 1.15 mycroft nd.ni_cnd.cn_cred = cred;
298 1.15 mycroft nd.ni_cnd.cn_nameiop = LOOKUP;
299 1.15 mycroft nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
300 1.15 mycroft if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
301 1.15 mycroft nfsd->nd_procp))
302 1.1 cgd nfsm_reply(0);
303 1.15 mycroft nqsrv_getl(nd.ni_startdir, NQL_READ);
304 1.15 mycroft vrele(nd.ni_startdir);
305 1.15 mycroft FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
306 1.1 cgd vp = nd.ni_vp;
307 1.1 cgd bzero((caddr_t)fhp, sizeof(nfh));
308 1.1 cgd fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
309 1.1 cgd if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
310 1.1 cgd vput(vp);
311 1.1 cgd nfsm_reply(0);
312 1.1 cgd }
313 1.15 mycroft if (duration2)
314 1.15 mycroft (void) nqsrv_getlease(vp, &duration2, NQL_READ, nfsd,
315 1.15 mycroft nam, &cache2, &frev2, cred);
316 1.15 mycroft error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
317 1.1 cgd vput(vp);
318 1.15 mycroft nfsm_reply(NFSX_FH + NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL) + 5*NFSX_UNSIGNED);
319 1.15 mycroft if (nfsd->nd_nqlflag != NQL_NOVAL) {
320 1.15 mycroft if (duration2) {
321 1.15 mycroft nfsm_build(tl, u_long *, 5*NFSX_UNSIGNED);
322 1.15 mycroft *tl++ = txdr_unsigned(NQL_READ);
323 1.15 mycroft *tl++ = txdr_unsigned(cache2);
324 1.15 mycroft *tl++ = txdr_unsigned(duration2);
325 1.15 mycroft txdr_hyper(&frev2, tl);
326 1.15 mycroft } else {
327 1.15 mycroft nfsm_build(tl, u_long *, NFSX_UNSIGNED);
328 1.15 mycroft *tl = 0;
329 1.15 mycroft }
330 1.15 mycroft }
331 1.1 cgd nfsm_srvfhtom(fhp);
332 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
333 1.1 cgd nfsm_srvfillattr;
334 1.1 cgd nfsm_srvdone;
335 1.1 cgd }
336 1.1 cgd
337 1.1 cgd /*
338 1.1 cgd * nfs readlink service
339 1.1 cgd */
340 1.15 mycroft nfsrv_readlink(nfsd, mrep, md, dpos, cred, nam, mrq)
341 1.15 mycroft struct nfsd *nfsd;
342 1.1 cgd struct mbuf *mrep, *md;
343 1.1 cgd caddr_t dpos;
344 1.1 cgd struct ucred *cred;
345 1.15 mycroft struct mbuf *nam, **mrq;
346 1.1 cgd {
347 1.1 cgd struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
348 1.1 cgd register struct iovec *ivp = iv;
349 1.1 cgd register struct mbuf *mp;
350 1.1 cgd register u_long *tl;
351 1.1 cgd register long t1;
352 1.1 cgd caddr_t bpos;
353 1.15 mycroft int error = 0, rdonly, cache, i, tlen, len;
354 1.1 cgd char *cp2;
355 1.1 cgd struct mbuf *mb, *mb2, *mp2, *mp3, *mreq;
356 1.1 cgd struct vnode *vp;
357 1.1 cgd nfsv2fh_t nfh;
358 1.1 cgd fhandle_t *fhp;
359 1.1 cgd struct uio io, *uiop = &io;
360 1.15 mycroft u_quad_t frev;
361 1.1 cgd
362 1.1 cgd fhp = &nfh.fh_generic;
363 1.1 cgd nfsm_srvmtofh(fhp);
364 1.1 cgd len = 0;
365 1.1 cgd i = 0;
366 1.1 cgd while (len < NFS_MAXPATHLEN) {
367 1.1 cgd MGET(mp, M_WAIT, MT_DATA);
368 1.1 cgd MCLGET(mp, M_WAIT);
369 1.1 cgd mp->m_len = NFSMSIZ(mp);
370 1.1 cgd if (len == 0)
371 1.1 cgd mp3 = mp2 = mp;
372 1.1 cgd else {
373 1.1 cgd mp2->m_next = mp;
374 1.1 cgd mp2 = mp;
375 1.1 cgd }
376 1.1 cgd if ((len+mp->m_len) > NFS_MAXPATHLEN) {
377 1.1 cgd mp->m_len = NFS_MAXPATHLEN-len;
378 1.1 cgd len = NFS_MAXPATHLEN;
379 1.1 cgd } else
380 1.1 cgd len += mp->m_len;
381 1.1 cgd ivp->iov_base = mtod(mp, caddr_t);
382 1.1 cgd ivp->iov_len = mp->m_len;
383 1.1 cgd i++;
384 1.1 cgd ivp++;
385 1.1 cgd }
386 1.1 cgd uiop->uio_iov = iv;
387 1.1 cgd uiop->uio_iovcnt = i;
388 1.1 cgd uiop->uio_offset = 0;
389 1.1 cgd uiop->uio_resid = len;
390 1.1 cgd uiop->uio_rw = UIO_READ;
391 1.1 cgd uiop->uio_segflg = UIO_SYSSPACE;
392 1.1 cgd uiop->uio_procp = (struct proc *)0;
393 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly)) {
394 1.1 cgd m_freem(mp3);
395 1.1 cgd nfsm_reply(0);
396 1.1 cgd }
397 1.1 cgd if (vp->v_type != VLNK) {
398 1.1 cgd error = EINVAL;
399 1.1 cgd goto out;
400 1.1 cgd }
401 1.15 mycroft nqsrv_getl(vp, NQL_READ);
402 1.1 cgd error = VOP_READLINK(vp, uiop, cred);
403 1.1 cgd out:
404 1.1 cgd vput(vp);
405 1.1 cgd if (error)
406 1.1 cgd m_freem(mp3);
407 1.1 cgd nfsm_reply(NFSX_UNSIGNED);
408 1.1 cgd if (uiop->uio_resid > 0) {
409 1.1 cgd len -= uiop->uio_resid;
410 1.1 cgd tlen = nfsm_rndup(len);
411 1.1 cgd nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
412 1.1 cgd }
413 1.1 cgd nfsm_build(tl, u_long *, NFSX_UNSIGNED);
414 1.1 cgd *tl = txdr_unsigned(len);
415 1.1 cgd mb->m_next = mp3;
416 1.1 cgd nfsm_srvdone;
417 1.1 cgd }
418 1.1 cgd
419 1.1 cgd /*
420 1.1 cgd * nfs read service
421 1.1 cgd */
422 1.15 mycroft nfsrv_read(nfsd, mrep, md, dpos, cred, nam, mrq)
423 1.15 mycroft struct nfsd *nfsd;
424 1.1 cgd struct mbuf *mrep, *md;
425 1.1 cgd caddr_t dpos;
426 1.1 cgd struct ucred *cred;
427 1.15 mycroft struct mbuf *nam, **mrq;
428 1.1 cgd {
429 1.1 cgd register struct iovec *iv;
430 1.1 cgd struct iovec *iv2;
431 1.1 cgd register struct mbuf *m;
432 1.1 cgd register struct nfsv2_fattr *fp;
433 1.1 cgd register u_long *tl;
434 1.1 cgd register long t1;
435 1.1 cgd caddr_t bpos;
436 1.15 mycroft int error = 0, rdonly, cache, i, cnt, len, left, siz, tlen;
437 1.1 cgd char *cp2;
438 1.1 cgd struct mbuf *mb, *mb2, *mreq;
439 1.15 mycroft struct mbuf *m2;
440 1.1 cgd struct vnode *vp;
441 1.1 cgd nfsv2fh_t nfh;
442 1.1 cgd fhandle_t *fhp;
443 1.1 cgd struct uio io, *uiop = &io;
444 1.1 cgd struct vattr va, *vap = &va;
445 1.1 cgd off_t off;
446 1.15 mycroft u_quad_t frev;
447 1.1 cgd
448 1.1 cgd fhp = &nfh.fh_generic;
449 1.1 cgd nfsm_srvmtofh(fhp);
450 1.15 mycroft if (nfsd->nd_nqlflag == NQL_NOVAL) {
451 1.15 mycroft nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
452 1.15 mycroft off = (off_t)fxdr_unsigned(u_long, *tl);
453 1.15 mycroft } else {
454 1.15 mycroft nfsm_dissect(tl, u_long *, 2 * NFSX_UNSIGNED);
455 1.15 mycroft fxdr_hyper(tl, &off);
456 1.15 mycroft }
457 1.1 cgd nfsm_srvstrsiz(cnt, NFS_MAXDATA);
458 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
459 1.1 cgd nfsm_reply(0);
460 1.15 mycroft if (vp->v_type != VREG) {
461 1.15 mycroft error = (vp->v_type == VDIR) ? EISDIR : EACCES;
462 1.1 cgd vput(vp);
463 1.1 cgd nfsm_reply(0);
464 1.1 cgd }
465 1.15 mycroft nqsrv_getl(vp, NQL_READ);
466 1.15 mycroft if ((error = nfsrv_access(vp, VREAD, cred, rdonly, nfsd->nd_procp)) &&
467 1.15 mycroft (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp))) {
468 1.15 mycroft vput(vp);
469 1.15 mycroft nfsm_reply(0);
470 1.1 cgd }
471 1.15 mycroft if (error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp)) {
472 1.1 cgd vput(vp);
473 1.1 cgd nfsm_reply(0);
474 1.1 cgd }
475 1.15 mycroft if (off >= vap->va_size)
476 1.15 mycroft cnt = 0;
477 1.15 mycroft else if ((off + cnt) > vap->va_size)
478 1.15 mycroft cnt = nfsm_rndup(vap->va_size - off);
479 1.15 mycroft nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL)+NFSX_UNSIGNED+nfsm_rndup(cnt));
480 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
481 1.15 mycroft nfsm_build(tl, u_long *, NFSX_UNSIGNED);
482 1.15 mycroft len = left = cnt;
483 1.15 mycroft if (cnt > 0) {
484 1.15 mycroft /*
485 1.15 mycroft * Generate the mbuf list with the uio_iov ref. to it.
486 1.15 mycroft */
487 1.15 mycroft i = 0;
488 1.15 mycroft m = m2 = mb;
489 1.15 mycroft MALLOC(iv, struct iovec *,
490 1.15 mycroft ((NFS_MAXDATA+MLEN-1)/MLEN) * sizeof (struct iovec),
491 1.15 mycroft M_TEMP, M_WAITOK);
492 1.15 mycroft iv2 = iv;
493 1.15 mycroft while (left > 0) {
494 1.15 mycroft siz = min(M_TRAILINGSPACE(m), left);
495 1.15 mycroft if (siz > 0) {
496 1.15 mycroft m->m_len += siz;
497 1.15 mycroft iv->iov_base = bpos;
498 1.15 mycroft iv->iov_len = siz;
499 1.15 mycroft iv++;
500 1.15 mycroft i++;
501 1.15 mycroft left -= siz;
502 1.15 mycroft }
503 1.15 mycroft if (left > 0) {
504 1.15 mycroft MGET(m, M_WAIT, MT_DATA);
505 1.15 mycroft MCLGET(m, M_WAIT);
506 1.15 mycroft m->m_len = 0;
507 1.15 mycroft m2->m_next = m;
508 1.15 mycroft m2 = m;
509 1.15 mycroft bpos = mtod(m, caddr_t);
510 1.15 mycroft }
511 1.15 mycroft }
512 1.15 mycroft uiop->uio_iov = iv2;
513 1.15 mycroft uiop->uio_iovcnt = i;
514 1.15 mycroft uiop->uio_offset = off;
515 1.15 mycroft uiop->uio_resid = cnt;
516 1.15 mycroft uiop->uio_rw = UIO_READ;
517 1.15 mycroft uiop->uio_segflg = UIO_SYSSPACE;
518 1.15 mycroft error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
519 1.15 mycroft off = uiop->uio_offset;
520 1.15 mycroft FREE((caddr_t)iv2, M_TEMP);
521 1.15 mycroft if (error || (error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp))) {
522 1.15 mycroft m_freem(mreq);
523 1.15 mycroft vput(vp);
524 1.15 mycroft nfsm_reply(0);
525 1.15 mycroft }
526 1.15 mycroft } else
527 1.15 mycroft uiop->uio_resid = 0;
528 1.1 cgd vput(vp);
529 1.1 cgd nfsm_srvfillattr;
530 1.1 cgd len -= uiop->uio_resid;
531 1.15 mycroft tlen = nfsm_rndup(len);
532 1.15 mycroft if (cnt != tlen || tlen != len)
533 1.15 mycroft nfsm_adj(mb, cnt-tlen, tlen-len);
534 1.1 cgd *tl = txdr_unsigned(len);
535 1.1 cgd nfsm_srvdone;
536 1.1 cgd }
537 1.1 cgd
538 1.1 cgd /*
539 1.1 cgd * nfs write service
540 1.1 cgd */
541 1.15 mycroft nfsrv_write(nfsd, mrep, md, dpos, cred, nam, mrq)
542 1.15 mycroft struct nfsd *nfsd;
543 1.15 mycroft struct mbuf *mrep, *md;
544 1.1 cgd caddr_t dpos;
545 1.1 cgd struct ucred *cred;
546 1.15 mycroft struct mbuf *nam, **mrq;
547 1.1 cgd {
548 1.1 cgd register struct iovec *ivp;
549 1.1 cgd register struct mbuf *mp;
550 1.1 cgd register struct nfsv2_fattr *fp;
551 1.1 cgd struct iovec iv[NFS_MAXIOVEC];
552 1.1 cgd struct vattr va;
553 1.1 cgd register struct vattr *vap = &va;
554 1.1 cgd register u_long *tl;
555 1.1 cgd register long t1;
556 1.1 cgd caddr_t bpos;
557 1.15 mycroft int error = 0, rdonly, cache, siz, len, xfer;
558 1.15 mycroft int ioflags = IO_SYNC | IO_NODELOCKED;
559 1.1 cgd char *cp2;
560 1.1 cgd struct mbuf *mb, *mb2, *mreq;
561 1.1 cgd struct vnode *vp;
562 1.1 cgd nfsv2fh_t nfh;
563 1.1 cgd fhandle_t *fhp;
564 1.1 cgd struct uio io, *uiop = &io;
565 1.1 cgd off_t off;
566 1.15 mycroft u_quad_t frev;
567 1.1 cgd
568 1.1 cgd fhp = &nfh.fh_generic;
569 1.1 cgd nfsm_srvmtofh(fhp);
570 1.15 mycroft nfsm_dissect(tl, u_long *, 4 * NFSX_UNSIGNED);
571 1.15 mycroft if (nfsd->nd_nqlflag == NQL_NOVAL) {
572 1.15 mycroft off = (off_t)fxdr_unsigned(u_long, *++tl);
573 1.15 mycroft tl += 2;
574 1.15 mycroft } else {
575 1.15 mycroft fxdr_hyper(tl, &off);
576 1.15 mycroft tl += 2;
577 1.15 mycroft if (fxdr_unsigned(u_long, *tl++))
578 1.15 mycroft ioflags |= IO_APPEND;
579 1.15 mycroft }
580 1.1 cgd len = fxdr_unsigned(long, *tl);
581 1.1 cgd if (len > NFS_MAXDATA || len <= 0) {
582 1.1 cgd error = EBADRPC;
583 1.1 cgd nfsm_reply(0);
584 1.1 cgd }
585 1.1 cgd if (dpos == (mtod(md, caddr_t)+md->m_len)) {
586 1.1 cgd mp = md->m_next;
587 1.1 cgd if (mp == NULL) {
588 1.1 cgd error = EBADRPC;
589 1.1 cgd nfsm_reply(0);
590 1.1 cgd }
591 1.1 cgd } else {
592 1.1 cgd mp = md;
593 1.1 cgd siz = dpos-mtod(mp, caddr_t);
594 1.1 cgd mp->m_len -= siz;
595 1.1 cgd NFSMADV(mp, siz);
596 1.1 cgd }
597 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
598 1.1 cgd nfsm_reply(0);
599 1.15 mycroft if (vp->v_type != VREG) {
600 1.15 mycroft error = (vp->v_type == VDIR) ? EISDIR : EACCES;
601 1.15 mycroft vput(vp);
602 1.15 mycroft nfsm_reply(0);
603 1.15 mycroft }
604 1.15 mycroft nqsrv_getl(vp, NQL_WRITE);
605 1.15 mycroft if (error = nfsrv_access(vp, VWRITE, cred, rdonly, nfsd->nd_procp)) {
606 1.1 cgd vput(vp);
607 1.1 cgd nfsm_reply(0);
608 1.1 cgd }
609 1.1 cgd uiop->uio_resid = 0;
610 1.1 cgd uiop->uio_rw = UIO_WRITE;
611 1.1 cgd uiop->uio_segflg = UIO_SYSSPACE;
612 1.1 cgd uiop->uio_procp = (struct proc *)0;
613 1.1 cgd /*
614 1.1 cgd * Do up to NFS_MAXIOVEC mbufs of write each iteration of the
615 1.1 cgd * loop until done.
616 1.1 cgd */
617 1.1 cgd while (len > 0 && uiop->uio_resid == 0) {
618 1.1 cgd ivp = iv;
619 1.1 cgd siz = 0;
620 1.1 cgd uiop->uio_iov = ivp;
621 1.1 cgd uiop->uio_iovcnt = 0;
622 1.1 cgd uiop->uio_offset = off;
623 1.1 cgd while (len > 0 && uiop->uio_iovcnt < NFS_MAXIOVEC && mp != NULL) {
624 1.1 cgd ivp->iov_base = mtod(mp, caddr_t);
625 1.1 cgd if (len < mp->m_len)
626 1.1 cgd ivp->iov_len = xfer = len;
627 1.1 cgd else
628 1.1 cgd ivp->iov_len = xfer = mp->m_len;
629 1.1 cgd #ifdef notdef
630 1.1 cgd /* Not Yet .. */
631 1.1 cgd if (M_HASCL(mp) && (((u_long)ivp->iov_base) & CLOFSET) == 0)
632 1.1 cgd ivp->iov_op = NULL; /* what should it be ?? */
633 1.1 cgd else
634 1.1 cgd ivp->iov_op = NULL;
635 1.1 cgd #endif
636 1.1 cgd uiop->uio_iovcnt++;
637 1.1 cgd ivp++;
638 1.1 cgd len -= xfer;
639 1.1 cgd siz += xfer;
640 1.1 cgd mp = mp->m_next;
641 1.1 cgd }
642 1.1 cgd if (len > 0 && mp == NULL) {
643 1.1 cgd error = EBADRPC;
644 1.1 cgd vput(vp);
645 1.1 cgd nfsm_reply(0);
646 1.1 cgd }
647 1.1 cgd uiop->uio_resid = siz;
648 1.15 mycroft if (error = VOP_WRITE(vp, uiop, ioflags, cred)) {
649 1.1 cgd vput(vp);
650 1.1 cgd nfsm_reply(0);
651 1.1 cgd }
652 1.1 cgd off = uiop->uio_offset;
653 1.1 cgd }
654 1.15 mycroft error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
655 1.1 cgd vput(vp);
656 1.15 mycroft nfsm_reply(NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
657 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
658 1.1 cgd nfsm_srvfillattr;
659 1.15 mycroft if (nfsd->nd_nqlflag != NQL_NOVAL) {
660 1.15 mycroft nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
661 1.15 mycroft txdr_hyper(&vap->va_filerev, tl);
662 1.15 mycroft }
663 1.1 cgd nfsm_srvdone;
664 1.1 cgd }
665 1.1 cgd
666 1.1 cgd /*
667 1.1 cgd * nfs create service
668 1.15 mycroft * now does a truncate to 0 length via. setattr if it already exists
669 1.1 cgd */
670 1.15 mycroft nfsrv_create(nfsd, mrep, md, dpos, cred, nam, mrq)
671 1.15 mycroft struct nfsd *nfsd;
672 1.15 mycroft struct mbuf *mrep, *md;
673 1.1 cgd caddr_t dpos;
674 1.1 cgd struct ucred *cred;
675 1.15 mycroft struct mbuf *nam, **mrq;
676 1.1 cgd {
677 1.1 cgd register struct nfsv2_fattr *fp;
678 1.1 cgd struct vattr va;
679 1.1 cgd register struct vattr *vap = &va;
680 1.15 mycroft register struct nfsv2_sattr *sp;
681 1.15 mycroft register u_long *tl;
682 1.1 cgd struct nameidata nd;
683 1.1 cgd register caddr_t cp;
684 1.1 cgd register long t1;
685 1.1 cgd caddr_t bpos;
686 1.15 mycroft int error = 0, rdev, cache, len, tsize;
687 1.1 cgd char *cp2;
688 1.1 cgd struct mbuf *mb, *mb2, *mreq;
689 1.1 cgd struct vnode *vp;
690 1.1 cgd nfsv2fh_t nfh;
691 1.1 cgd fhandle_t *fhp;
692 1.15 mycroft u_quad_t frev;
693 1.1 cgd
694 1.15 mycroft nd.ni_cnd.cn_nameiop = 0;
695 1.1 cgd fhp = &nfh.fh_generic;
696 1.1 cgd nfsm_srvmtofh(fhp);
697 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
698 1.15 mycroft nd.ni_cnd.cn_cred = cred;
699 1.15 mycroft nd.ni_cnd.cn_nameiop = CREATE;
700 1.15 mycroft nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
701 1.15 mycroft if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
702 1.15 mycroft nfsd->nd_procp))
703 1.1 cgd nfsm_reply(0);
704 1.1 cgd VATTR_NULL(vap);
705 1.15 mycroft nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
706 1.1 cgd /*
707 1.15 mycroft * Iff doesn't exist, create it
708 1.15 mycroft * otherwise just truncate to 0 length
709 1.1 cgd * should I set the mode too ??
710 1.1 cgd */
711 1.1 cgd if (nd.ni_vp == NULL) {
712 1.15 mycroft vap->va_type = IFTOVT(fxdr_unsigned(u_long, sp->sa_mode));
713 1.1 cgd if (vap->va_type == VNON)
714 1.1 cgd vap->va_type = VREG;
715 1.15 mycroft vap->va_mode = nfstov_mode(sp->sa_mode);
716 1.15 mycroft if (nfsd->nd_nqlflag == NQL_NOVAL)
717 1.15 mycroft rdev = fxdr_unsigned(long, sp->sa_nfssize);
718 1.15 mycroft else
719 1.15 mycroft rdev = fxdr_unsigned(long, sp->sa_nqrdev);
720 1.1 cgd if (vap->va_type == VREG || vap->va_type == VSOCK) {
721 1.1 cgd vrele(nd.ni_startdir);
722 1.15 mycroft nqsrv_getl(nd.ni_dvp, NQL_WRITE);
723 1.15 mycroft if (error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap))
724 1.1 cgd nfsm_reply(0);
725 1.15 mycroft FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
726 1.1 cgd } else if (vap->va_type == VCHR || vap->va_type == VBLK ||
727 1.1 cgd vap->va_type == VFIFO) {
728 1.1 cgd if (vap->va_type == VCHR && rdev == 0xffffffff)
729 1.1 cgd vap->va_type = VFIFO;
730 1.1 cgd if (vap->va_type == VFIFO) {
731 1.1 cgd #ifndef FIFO
732 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
733 1.1 cgd vput(nd.ni_dvp);
734 1.1 cgd error = ENXIO;
735 1.1 cgd goto out;
736 1.1 cgd #endif /* FIFO */
737 1.6 cgd } else if (error = suser(cred, (u_short *)0)) {
738 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
739 1.1 cgd vput(nd.ni_dvp);
740 1.1 cgd goto out;
741 1.1 cgd } else
742 1.1 cgd vap->va_rdev = (dev_t)rdev;
743 1.15 mycroft nqsrv_getl(nd.ni_dvp, NQL_WRITE);
744 1.15 mycroft if (error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap)) {
745 1.1 cgd vrele(nd.ni_startdir);
746 1.1 cgd nfsm_reply(0);
747 1.1 cgd }
748 1.15 mycroft nd.ni_cnd.cn_nameiop = LOOKUP;
749 1.15 mycroft nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
750 1.15 mycroft nd.ni_cnd.cn_proc = nfsd->nd_procp;
751 1.15 mycroft nd.ni_cnd.cn_cred = nfsd->nd_procp->p_ucred;
752 1.15 mycroft if (error = lookup(&nd)) {
753 1.15 mycroft free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
754 1.1 cgd nfsm_reply(0);
755 1.1 cgd }
756 1.15 mycroft FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
757 1.15 mycroft if (nd.ni_cnd.cn_flags & ISSYMLINK) {
758 1.1 cgd vrele(nd.ni_dvp);
759 1.1 cgd vput(nd.ni_vp);
760 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
761 1.1 cgd error = EINVAL;
762 1.1 cgd nfsm_reply(0);
763 1.1 cgd }
764 1.1 cgd } else {
765 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
766 1.1 cgd vput(nd.ni_dvp);
767 1.1 cgd error = ENXIO;
768 1.1 cgd goto out;
769 1.1 cgd }
770 1.1 cgd vp = nd.ni_vp;
771 1.1 cgd } else {
772 1.1 cgd vrele(nd.ni_startdir);
773 1.15 mycroft free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
774 1.1 cgd vp = nd.ni_vp;
775 1.1 cgd if (nd.ni_dvp == vp)
776 1.1 cgd vrele(nd.ni_dvp);
777 1.1 cgd else
778 1.1 cgd vput(nd.ni_dvp);
779 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
780 1.15 mycroft if (nfsd->nd_nqlflag == NQL_NOVAL) {
781 1.15 mycroft tsize = fxdr_unsigned(long, sp->sa_nfssize);
782 1.15 mycroft if (tsize != -1)
783 1.15 mycroft vap->va_size = (u_quad_t)tsize;
784 1.15 mycroft else
785 1.15 mycroft vap->va_size = -1;
786 1.15 mycroft } else
787 1.15 mycroft fxdr_hyper(&sp->sa_nqsize, &vap->va_size);
788 1.15 mycroft if (vap->va_size != -1) {
789 1.15 mycroft if (error = nfsrv_access(vp, VWRITE, cred,
790 1.15 mycroft (nd.ni_cnd.cn_flags & RDONLY), nfsd->nd_procp)) {
791 1.15 mycroft vput(vp);
792 1.15 mycroft nfsm_reply(0);
793 1.15 mycroft }
794 1.15 mycroft nqsrv_getl(vp, NQL_WRITE);
795 1.15 mycroft if (error = VOP_SETATTR(vp, vap, cred, nfsd->nd_procp)) {
796 1.15 mycroft vput(vp);
797 1.15 mycroft nfsm_reply(0);
798 1.15 mycroft }
799 1.1 cgd }
800 1.1 cgd }
801 1.1 cgd bzero((caddr_t)fhp, sizeof(nfh));
802 1.1 cgd fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
803 1.1 cgd if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
804 1.1 cgd vput(vp);
805 1.1 cgd nfsm_reply(0);
806 1.1 cgd }
807 1.15 mycroft error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
808 1.1 cgd vput(vp);
809 1.15 mycroft nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
810 1.1 cgd nfsm_srvfhtom(fhp);
811 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
812 1.1 cgd nfsm_srvfillattr;
813 1.1 cgd return (error);
814 1.1 cgd nfsmout:
815 1.15 mycroft if (nd.ni_cnd.cn_nameiop || nd.ni_cnd.cn_flags)
816 1.1 cgd vrele(nd.ni_startdir);
817 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
818 1.1 cgd if (nd.ni_dvp == nd.ni_vp)
819 1.1 cgd vrele(nd.ni_dvp);
820 1.1 cgd else
821 1.1 cgd vput(nd.ni_dvp);
822 1.1 cgd if (nd.ni_vp)
823 1.1 cgd vput(nd.ni_vp);
824 1.1 cgd return (error);
825 1.1 cgd
826 1.1 cgd out:
827 1.1 cgd vrele(nd.ni_startdir);
828 1.15 mycroft free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
829 1.1 cgd nfsm_reply(0);
830 1.1 cgd }
831 1.1 cgd
832 1.1 cgd /*
833 1.1 cgd * nfs remove service
834 1.1 cgd */
835 1.15 mycroft nfsrv_remove(nfsd, mrep, md, dpos, cred, nam, mrq)
836 1.15 mycroft struct nfsd *nfsd;
837 1.15 mycroft struct mbuf *mrep, *md;
838 1.1 cgd caddr_t dpos;
839 1.1 cgd struct ucred *cred;
840 1.15 mycroft struct mbuf *nam, **mrq;
841 1.1 cgd {
842 1.1 cgd struct nameidata nd;
843 1.1 cgd register u_long *tl;
844 1.1 cgd register long t1;
845 1.1 cgd caddr_t bpos;
846 1.15 mycroft int error = 0, cache, len;
847 1.1 cgd char *cp2;
848 1.1 cgd struct mbuf *mb, *mreq;
849 1.1 cgd struct vnode *vp;
850 1.1 cgd nfsv2fh_t nfh;
851 1.1 cgd fhandle_t *fhp;
852 1.15 mycroft u_quad_t frev;
853 1.1 cgd
854 1.1 cgd fhp = &nfh.fh_generic;
855 1.1 cgd nfsm_srvmtofh(fhp);
856 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
857 1.15 mycroft nd.ni_cnd.cn_cred = cred;
858 1.15 mycroft nd.ni_cnd.cn_nameiop = DELETE;
859 1.15 mycroft nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
860 1.15 mycroft if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
861 1.15 mycroft nfsd->nd_procp))
862 1.1 cgd nfsm_reply(0);
863 1.1 cgd vp = nd.ni_vp;
864 1.1 cgd if (vp->v_type == VDIR &&
865 1.6 cgd (error = suser(cred, (u_short *)0)))
866 1.1 cgd goto out;
867 1.1 cgd /*
868 1.1 cgd * The root of a mounted filesystem cannot be deleted.
869 1.1 cgd */
870 1.1 cgd if (vp->v_flag & VROOT) {
871 1.1 cgd error = EBUSY;
872 1.1 cgd goto out;
873 1.1 cgd }
874 1.1 cgd if (vp->v_flag & VTEXT)
875 1.1 cgd (void) vnode_pager_uncache(vp);
876 1.1 cgd out:
877 1.1 cgd if (!error) {
878 1.15 mycroft nqsrv_getl(nd.ni_dvp, NQL_WRITE);
879 1.15 mycroft nqsrv_getl(vp, NQL_WRITE);
880 1.15 mycroft error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
881 1.1 cgd } else {
882 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
883 1.1 cgd if (nd.ni_dvp == vp)
884 1.1 cgd vrele(nd.ni_dvp);
885 1.1 cgd else
886 1.1 cgd vput(nd.ni_dvp);
887 1.1 cgd vput(vp);
888 1.1 cgd }
889 1.1 cgd nfsm_reply(0);
890 1.1 cgd nfsm_srvdone;
891 1.1 cgd }
892 1.1 cgd
893 1.1 cgd /*
894 1.1 cgd * nfs rename service
895 1.1 cgd */
896 1.15 mycroft nfsrv_rename(nfsd, mrep, md, dpos, cred, nam, mrq)
897 1.15 mycroft struct nfsd *nfsd;
898 1.15 mycroft struct mbuf *mrep, *md;
899 1.1 cgd caddr_t dpos;
900 1.1 cgd struct ucred *cred;
901 1.15 mycroft struct mbuf *nam, **mrq;
902 1.1 cgd {
903 1.1 cgd register u_long *tl;
904 1.1 cgd register long t1;
905 1.1 cgd caddr_t bpos;
906 1.15 mycroft int error = 0, cache, len, len2;
907 1.1 cgd char *cp2;
908 1.1 cgd struct mbuf *mb, *mreq;
909 1.1 cgd struct nameidata fromnd, tond;
910 1.1 cgd struct vnode *fvp, *tvp, *tdvp;
911 1.1 cgd nfsv2fh_t fnfh, tnfh;
912 1.1 cgd fhandle_t *ffhp, *tfhp;
913 1.15 mycroft u_quad_t frev;
914 1.15 mycroft uid_t saved_uid;
915 1.1 cgd
916 1.1 cgd ffhp = &fnfh.fh_generic;
917 1.1 cgd tfhp = &tnfh.fh_generic;
918 1.15 mycroft fromnd.ni_cnd.cn_nameiop = 0;
919 1.15 mycroft tond.ni_cnd.cn_nameiop = 0;
920 1.1 cgd nfsm_srvmtofh(ffhp);
921 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
922 1.1 cgd /*
923 1.15 mycroft * Remember our original uid so that we can reset cr_uid before
924 1.15 mycroft * the second nfs_namei() call, in case it is remapped.
925 1.1 cgd */
926 1.15 mycroft saved_uid = cred->cr_uid;
927 1.15 mycroft fromnd.ni_cnd.cn_cred = cred;
928 1.15 mycroft fromnd.ni_cnd.cn_nameiop = DELETE;
929 1.15 mycroft fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
930 1.15 mycroft if (error = nfs_namei(&fromnd, ffhp, len, nfsd->nd_slp, nam, &md,
931 1.15 mycroft &dpos, nfsd->nd_procp))
932 1.1 cgd nfsm_reply(0);
933 1.1 cgd fvp = fromnd.ni_vp;
934 1.1 cgd nfsm_srvmtofh(tfhp);
935 1.1 cgd nfsm_strsiz(len2, NFS_MAXNAMLEN);
936 1.15 mycroft cred->cr_uid = saved_uid;
937 1.15 mycroft tond.ni_cnd.cn_cred = cred;
938 1.15 mycroft tond.ni_cnd.cn_nameiop = RENAME;
939 1.15 mycroft tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
940 1.15 mycroft if (error = nfs_namei(&tond, tfhp, len2, nfsd->nd_slp, nam, &md,
941 1.15 mycroft &dpos, nfsd->nd_procp)) {
942 1.15 mycroft VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
943 1.1 cgd vrele(fromnd.ni_dvp);
944 1.1 cgd vrele(fvp);
945 1.1 cgd goto out1;
946 1.1 cgd }
947 1.1 cgd tdvp = tond.ni_dvp;
948 1.1 cgd tvp = tond.ni_vp;
949 1.1 cgd if (tvp != NULL) {
950 1.1 cgd if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
951 1.1 cgd error = EISDIR;
952 1.1 cgd goto out;
953 1.1 cgd } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
954 1.1 cgd error = ENOTDIR;
955 1.1 cgd goto out;
956 1.1 cgd }
957 1.15 mycroft if (tvp->v_type == VDIR && tvp->v_mountedhere) {
958 1.15 mycroft error = EXDEV;
959 1.15 mycroft goto out;
960 1.15 mycroft }
961 1.15 mycroft }
962 1.15 mycroft if (fvp->v_type == VDIR && fvp->v_mountedhere) {
963 1.15 mycroft error = EBUSY;
964 1.15 mycroft goto out;
965 1.1 cgd }
966 1.1 cgd if (fvp->v_mount != tdvp->v_mount) {
967 1.1 cgd error = EXDEV;
968 1.1 cgd goto out;
969 1.1 cgd }
970 1.1 cgd if (fvp == tdvp)
971 1.1 cgd error = EINVAL;
972 1.1 cgd /*
973 1.1 cgd * If source is the same as the destination (that is the
974 1.1 cgd * same vnode with the same name in the same directory),
975 1.1 cgd * then there is nothing to do.
976 1.1 cgd */
977 1.1 cgd if (fvp == tvp && fromnd.ni_dvp == tdvp &&
978 1.15 mycroft fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
979 1.15 mycroft !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
980 1.15 mycroft fromnd.ni_cnd.cn_namelen))
981 1.1 cgd error = -1;
982 1.1 cgd out:
983 1.1 cgd if (!error) {
984 1.15 mycroft nqsrv_getl(fromnd.ni_dvp, NQL_WRITE);
985 1.15 mycroft nqsrv_getl(tdvp, NQL_WRITE);
986 1.15 mycroft if (tvp)
987 1.15 mycroft nqsrv_getl(tvp, NQL_WRITE);
988 1.15 mycroft error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
989 1.15 mycroft tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
990 1.1 cgd } else {
991 1.15 mycroft VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
992 1.1 cgd if (tdvp == tvp)
993 1.1 cgd vrele(tdvp);
994 1.1 cgd else
995 1.1 cgd vput(tdvp);
996 1.1 cgd if (tvp)
997 1.1 cgd vput(tvp);
998 1.15 mycroft VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
999 1.1 cgd vrele(fromnd.ni_dvp);
1000 1.1 cgd vrele(fvp);
1001 1.1 cgd }
1002 1.1 cgd vrele(tond.ni_startdir);
1003 1.15 mycroft FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1004 1.1 cgd out1:
1005 1.1 cgd vrele(fromnd.ni_startdir);
1006 1.15 mycroft FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1007 1.1 cgd nfsm_reply(0);
1008 1.1 cgd return (error);
1009 1.1 cgd
1010 1.1 cgd nfsmout:
1011 1.15 mycroft if (tond.ni_cnd.cn_nameiop || tond.ni_cnd.cn_flags) {
1012 1.1 cgd vrele(tond.ni_startdir);
1013 1.15 mycroft FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1014 1.1 cgd }
1015 1.15 mycroft if (fromnd.ni_cnd.cn_nameiop || fromnd.ni_cnd.cn_flags) {
1016 1.1 cgd vrele(fromnd.ni_startdir);
1017 1.15 mycroft FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1018 1.15 mycroft VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1019 1.1 cgd vrele(fromnd.ni_dvp);
1020 1.1 cgd vrele(fvp);
1021 1.1 cgd }
1022 1.1 cgd return (error);
1023 1.1 cgd }
1024 1.1 cgd
1025 1.1 cgd /*
1026 1.1 cgd * nfs link service
1027 1.1 cgd */
1028 1.15 mycroft nfsrv_link(nfsd, mrep, md, dpos, cred, nam, mrq)
1029 1.15 mycroft struct nfsd *nfsd;
1030 1.15 mycroft struct mbuf *mrep, *md;
1031 1.1 cgd caddr_t dpos;
1032 1.1 cgd struct ucred *cred;
1033 1.15 mycroft struct mbuf *nam, **mrq;
1034 1.1 cgd {
1035 1.1 cgd struct nameidata nd;
1036 1.1 cgd register u_long *tl;
1037 1.1 cgd register long t1;
1038 1.1 cgd caddr_t bpos;
1039 1.15 mycroft int error = 0, rdonly, cache, len;
1040 1.1 cgd char *cp2;
1041 1.1 cgd struct mbuf *mb, *mreq;
1042 1.1 cgd struct vnode *vp, *xp;
1043 1.1 cgd nfsv2fh_t nfh, dnfh;
1044 1.1 cgd fhandle_t *fhp, *dfhp;
1045 1.15 mycroft u_quad_t frev;
1046 1.1 cgd
1047 1.1 cgd fhp = &nfh.fh_generic;
1048 1.1 cgd dfhp = &dnfh.fh_generic;
1049 1.1 cgd nfsm_srvmtofh(fhp);
1050 1.1 cgd nfsm_srvmtofh(dfhp);
1051 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
1052 1.15 mycroft if (error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
1053 1.1 cgd nfsm_reply(0);
1054 1.15 mycroft if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)))
1055 1.1 cgd goto out1;
1056 1.15 mycroft nd.ni_cnd.cn_cred = cred;
1057 1.15 mycroft nd.ni_cnd.cn_nameiop = CREATE;
1058 1.15 mycroft nd.ni_cnd.cn_flags = LOCKPARENT;
1059 1.15 mycroft if (error = nfs_namei(&nd, dfhp, len, nfsd->nd_slp, nam, &md, &dpos,
1060 1.15 mycroft nfsd->nd_procp))
1061 1.1 cgd goto out1;
1062 1.1 cgd xp = nd.ni_vp;
1063 1.1 cgd if (xp != NULL) {
1064 1.1 cgd error = EEXIST;
1065 1.1 cgd goto out;
1066 1.1 cgd }
1067 1.1 cgd xp = nd.ni_dvp;
1068 1.1 cgd if (vp->v_mount != xp->v_mount)
1069 1.1 cgd error = EXDEV;
1070 1.1 cgd out:
1071 1.1 cgd if (!error) {
1072 1.15 mycroft nqsrv_getl(vp, NQL_WRITE);
1073 1.15 mycroft nqsrv_getl(xp, NQL_WRITE);
1074 1.15 mycroft error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1075 1.1 cgd } else {
1076 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1077 1.1 cgd if (nd.ni_dvp == nd.ni_vp)
1078 1.1 cgd vrele(nd.ni_dvp);
1079 1.1 cgd else
1080 1.1 cgd vput(nd.ni_dvp);
1081 1.1 cgd if (nd.ni_vp)
1082 1.1 cgd vrele(nd.ni_vp);
1083 1.1 cgd }
1084 1.1 cgd out1:
1085 1.1 cgd vrele(vp);
1086 1.1 cgd nfsm_reply(0);
1087 1.1 cgd nfsm_srvdone;
1088 1.1 cgd }
1089 1.1 cgd
1090 1.1 cgd /*
1091 1.1 cgd * nfs symbolic link service
1092 1.1 cgd */
1093 1.15 mycroft nfsrv_symlink(nfsd, mrep, md, dpos, cred, nam, mrq)
1094 1.15 mycroft struct nfsd *nfsd;
1095 1.15 mycroft struct mbuf *mrep, *md;
1096 1.1 cgd caddr_t dpos;
1097 1.1 cgd struct ucred *cred;
1098 1.15 mycroft struct mbuf *nam, **mrq;
1099 1.1 cgd {
1100 1.1 cgd struct vattr va;
1101 1.1 cgd struct nameidata nd;
1102 1.1 cgd register struct vattr *vap = &va;
1103 1.1 cgd register u_long *tl;
1104 1.1 cgd register long t1;
1105 1.1 cgd struct nfsv2_sattr *sp;
1106 1.1 cgd caddr_t bpos;
1107 1.1 cgd struct uio io;
1108 1.1 cgd struct iovec iv;
1109 1.15 mycroft int error = 0, cache, len, len2;
1110 1.1 cgd char *pathcp, *cp2;
1111 1.1 cgd struct mbuf *mb, *mreq;
1112 1.1 cgd nfsv2fh_t nfh;
1113 1.1 cgd fhandle_t *fhp;
1114 1.15 mycroft u_quad_t frev;
1115 1.1 cgd
1116 1.1 cgd pathcp = (char *)0;
1117 1.1 cgd fhp = &nfh.fh_generic;
1118 1.1 cgd nfsm_srvmtofh(fhp);
1119 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
1120 1.15 mycroft nd.ni_cnd.cn_cred = cred;
1121 1.15 mycroft nd.ni_cnd.cn_nameiop = CREATE;
1122 1.15 mycroft nd.ni_cnd.cn_flags = LOCKPARENT;
1123 1.15 mycroft if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
1124 1.15 mycroft nfsd->nd_procp))
1125 1.1 cgd goto out;
1126 1.1 cgd nfsm_strsiz(len2, NFS_MAXPATHLEN);
1127 1.1 cgd MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
1128 1.1 cgd iv.iov_base = pathcp;
1129 1.1 cgd iv.iov_len = len2;
1130 1.1 cgd io.uio_resid = len2;
1131 1.1 cgd io.uio_offset = 0;
1132 1.1 cgd io.uio_iov = &iv;
1133 1.1 cgd io.uio_iovcnt = 1;
1134 1.1 cgd io.uio_segflg = UIO_SYSSPACE;
1135 1.1 cgd io.uio_rw = UIO_READ;
1136 1.1 cgd io.uio_procp = (struct proc *)0;
1137 1.1 cgd nfsm_mtouio(&io, len2);
1138 1.15 mycroft nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_SATTR(nfsd->nd_nqlflag != NQL_NOVAL));
1139 1.1 cgd *(pathcp + len2) = '\0';
1140 1.1 cgd if (nd.ni_vp) {
1141 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1142 1.1 cgd if (nd.ni_dvp == nd.ni_vp)
1143 1.1 cgd vrele(nd.ni_dvp);
1144 1.1 cgd else
1145 1.1 cgd vput(nd.ni_dvp);
1146 1.1 cgd vrele(nd.ni_vp);
1147 1.1 cgd error = EEXIST;
1148 1.1 cgd goto out;
1149 1.1 cgd }
1150 1.1 cgd VATTR_NULL(vap);
1151 1.1 cgd vap->va_mode = fxdr_unsigned(u_short, sp->sa_mode);
1152 1.15 mycroft nqsrv_getl(nd.ni_dvp, NQL_WRITE);
1153 1.15 mycroft error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap, pathcp);
1154 1.1 cgd out:
1155 1.1 cgd if (pathcp)
1156 1.1 cgd FREE(pathcp, M_TEMP);
1157 1.1 cgd nfsm_reply(0);
1158 1.1 cgd return (error);
1159 1.1 cgd nfsmout:
1160 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1161 1.1 cgd if (nd.ni_dvp == nd.ni_vp)
1162 1.1 cgd vrele(nd.ni_dvp);
1163 1.1 cgd else
1164 1.1 cgd vput(nd.ni_dvp);
1165 1.1 cgd if (nd.ni_vp)
1166 1.1 cgd vrele(nd.ni_vp);
1167 1.1 cgd if (pathcp)
1168 1.1 cgd FREE(pathcp, M_TEMP);
1169 1.1 cgd return (error);
1170 1.1 cgd }
1171 1.1 cgd
1172 1.1 cgd /*
1173 1.1 cgd * nfs mkdir service
1174 1.1 cgd */
1175 1.15 mycroft nfsrv_mkdir(nfsd, mrep, md, dpos, cred, nam, mrq)
1176 1.15 mycroft struct nfsd *nfsd;
1177 1.15 mycroft struct mbuf *mrep, *md;
1178 1.1 cgd caddr_t dpos;
1179 1.1 cgd struct ucred *cred;
1180 1.15 mycroft struct mbuf *nam, **mrq;
1181 1.1 cgd {
1182 1.1 cgd struct vattr va;
1183 1.1 cgd register struct vattr *vap = &va;
1184 1.1 cgd register struct nfsv2_fattr *fp;
1185 1.1 cgd struct nameidata nd;
1186 1.1 cgd register caddr_t cp;
1187 1.1 cgd register u_long *tl;
1188 1.1 cgd register long t1;
1189 1.1 cgd caddr_t bpos;
1190 1.15 mycroft int error = 0, cache, len;
1191 1.1 cgd char *cp2;
1192 1.1 cgd struct mbuf *mb, *mb2, *mreq;
1193 1.1 cgd struct vnode *vp;
1194 1.1 cgd nfsv2fh_t nfh;
1195 1.1 cgd fhandle_t *fhp;
1196 1.15 mycroft u_quad_t frev;
1197 1.1 cgd
1198 1.1 cgd fhp = &nfh.fh_generic;
1199 1.1 cgd nfsm_srvmtofh(fhp);
1200 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
1201 1.15 mycroft nd.ni_cnd.cn_cred = cred;
1202 1.15 mycroft nd.ni_cnd.cn_nameiop = CREATE;
1203 1.15 mycroft nd.ni_cnd.cn_flags = LOCKPARENT;
1204 1.15 mycroft if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
1205 1.15 mycroft nfsd->nd_procp))
1206 1.1 cgd nfsm_reply(0);
1207 1.15 mycroft nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1208 1.1 cgd VATTR_NULL(vap);
1209 1.1 cgd vap->va_type = VDIR;
1210 1.1 cgd vap->va_mode = nfstov_mode(*tl++);
1211 1.1 cgd vp = nd.ni_vp;
1212 1.1 cgd if (vp != NULL) {
1213 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1214 1.1 cgd if (nd.ni_dvp == vp)
1215 1.1 cgd vrele(nd.ni_dvp);
1216 1.1 cgd else
1217 1.1 cgd vput(nd.ni_dvp);
1218 1.1 cgd vrele(vp);
1219 1.1 cgd error = EEXIST;
1220 1.1 cgd nfsm_reply(0);
1221 1.1 cgd }
1222 1.15 mycroft nqsrv_getl(nd.ni_dvp, NQL_WRITE);
1223 1.15 mycroft if (error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, vap))
1224 1.1 cgd nfsm_reply(0);
1225 1.1 cgd vp = nd.ni_vp;
1226 1.1 cgd bzero((caddr_t)fhp, sizeof(nfh));
1227 1.1 cgd fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1228 1.1 cgd if (error = VFS_VPTOFH(vp, &fhp->fh_fid)) {
1229 1.1 cgd vput(vp);
1230 1.1 cgd nfsm_reply(0);
1231 1.1 cgd }
1232 1.15 mycroft error = VOP_GETATTR(vp, vap, cred, nfsd->nd_procp);
1233 1.1 cgd vput(vp);
1234 1.15 mycroft nfsm_reply(NFSX_FH+NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
1235 1.1 cgd nfsm_srvfhtom(fhp);
1236 1.15 mycroft nfsm_build(fp, struct nfsv2_fattr *, NFSX_FATTR(nfsd->nd_nqlflag != NQL_NOVAL));
1237 1.1 cgd nfsm_srvfillattr;
1238 1.1 cgd return (error);
1239 1.1 cgd nfsmout:
1240 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1241 1.1 cgd if (nd.ni_dvp == nd.ni_vp)
1242 1.1 cgd vrele(nd.ni_dvp);
1243 1.1 cgd else
1244 1.1 cgd vput(nd.ni_dvp);
1245 1.1 cgd if (nd.ni_vp)
1246 1.1 cgd vrele(nd.ni_vp);
1247 1.1 cgd return (error);
1248 1.1 cgd }
1249 1.1 cgd
1250 1.1 cgd /*
1251 1.1 cgd * nfs rmdir service
1252 1.1 cgd */
1253 1.15 mycroft nfsrv_rmdir(nfsd, mrep, md, dpos, cred, nam, mrq)
1254 1.15 mycroft struct nfsd *nfsd;
1255 1.15 mycroft struct mbuf *mrep, *md;
1256 1.1 cgd caddr_t dpos;
1257 1.1 cgd struct ucred *cred;
1258 1.15 mycroft struct mbuf *nam, **mrq;
1259 1.1 cgd {
1260 1.1 cgd register u_long *tl;
1261 1.1 cgd register long t1;
1262 1.1 cgd caddr_t bpos;
1263 1.15 mycroft int error = 0, cache, len;
1264 1.1 cgd char *cp2;
1265 1.1 cgd struct mbuf *mb, *mreq;
1266 1.1 cgd struct vnode *vp;
1267 1.1 cgd nfsv2fh_t nfh;
1268 1.1 cgd fhandle_t *fhp;
1269 1.1 cgd struct nameidata nd;
1270 1.15 mycroft u_quad_t frev;
1271 1.1 cgd
1272 1.1 cgd fhp = &nfh.fh_generic;
1273 1.1 cgd nfsm_srvmtofh(fhp);
1274 1.1 cgd nfsm_srvstrsiz(len, NFS_MAXNAMLEN);
1275 1.15 mycroft nd.ni_cnd.cn_cred = cred;
1276 1.15 mycroft nd.ni_cnd.cn_nameiop = DELETE;
1277 1.15 mycroft nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1278 1.15 mycroft if (error = nfs_namei(&nd, fhp, len, nfsd->nd_slp, nam, &md, &dpos,
1279 1.15 mycroft nfsd->nd_procp))
1280 1.1 cgd nfsm_reply(0);
1281 1.1 cgd vp = nd.ni_vp;
1282 1.1 cgd if (vp->v_type != VDIR) {
1283 1.1 cgd error = ENOTDIR;
1284 1.1 cgd goto out;
1285 1.1 cgd }
1286 1.1 cgd /*
1287 1.1 cgd * No rmdir "." please.
1288 1.1 cgd */
1289 1.1 cgd if (nd.ni_dvp == vp) {
1290 1.1 cgd error = EINVAL;
1291 1.1 cgd goto out;
1292 1.1 cgd }
1293 1.1 cgd /*
1294 1.1 cgd * The root of a mounted filesystem cannot be deleted.
1295 1.1 cgd */
1296 1.1 cgd if (vp->v_flag & VROOT)
1297 1.1 cgd error = EBUSY;
1298 1.1 cgd out:
1299 1.1 cgd if (!error) {
1300 1.15 mycroft nqsrv_getl(nd.ni_dvp, NQL_WRITE);
1301 1.15 mycroft nqsrv_getl(vp, NQL_WRITE);
1302 1.15 mycroft error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1303 1.1 cgd } else {
1304 1.15 mycroft VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1305 1.1 cgd if (nd.ni_dvp == nd.ni_vp)
1306 1.1 cgd vrele(nd.ni_dvp);
1307 1.1 cgd else
1308 1.1 cgd vput(nd.ni_dvp);
1309 1.1 cgd vput(vp);
1310 1.1 cgd }
1311 1.1 cgd nfsm_reply(0);
1312 1.1 cgd nfsm_srvdone;
1313 1.1 cgd }
1314 1.1 cgd
1315 1.1 cgd /*
1316 1.1 cgd * nfs readdir service
1317 1.1 cgd * - mallocs what it thinks is enough to read
1318 1.1 cgd * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
1319 1.1 cgd * - calls VOP_READDIR()
1320 1.1 cgd * - loops around building the reply
1321 1.1 cgd * if the output generated exceeds count break out of loop
1322 1.1 cgd * The nfsm_clget macro is used here so that the reply will be packed
1323 1.1 cgd * tightly in mbuf clusters.
1324 1.1 cgd * - it only knows that it has encountered eof when the VOP_READDIR()
1325 1.1 cgd * reads nothing
1326 1.1 cgd * - as such one readdir rpc will return eof false although you are there
1327 1.1 cgd * and then the next will return eof
1328 1.11 ws * - it trims out records with d_fileno == 0
1329 1.1 cgd * this doesn't matter for Unix clients, but they might confuse clients
1330 1.1 cgd * for other os'.
1331 1.1 cgd * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
1332 1.1 cgd * than requested, but this may not apply to all filesystems. For
1333 1.1 cgd * example, client NFS does not { although it is never remote mounted
1334 1.1 cgd * anyhow }
1335 1.15 mycroft * The alternate call nqnfsrv_readdirlook() does lookups as well.
1336 1.1 cgd * PS: The NFS protocol spec. does not clarify what the "count" byte
1337 1.1 cgd * argument is a count of.. just name strings and file id's or the
1338 1.1 cgd * entire reply rpc or ...
1339 1.1 cgd * I tried just file name and id sizes and it confused the Sun client,
1340 1.1 cgd * so I am using the full rpc size now. The "paranoia.." comment refers
1341 1.1 cgd * to including the status longwords that are not a part of the dir.
1342 1.1 cgd * "entry" structures, but are in the rpc.
1343 1.1 cgd */
1344 1.15 mycroft struct flrep {
1345 1.15 mycroft u_long fl_cachable;
1346 1.15 mycroft u_long fl_duration;
1347 1.15 mycroft u_long fl_frev[2];
1348 1.15 mycroft nfsv2fh_t fl_nfh;
1349 1.15 mycroft u_long fl_fattr[NFSX_NQFATTR / sizeof (u_long)];
1350 1.15 mycroft };
1351 1.15 mycroft
1352 1.15 mycroft nfsrv_readdir(nfsd, mrep, md, dpos, cred, nam, mrq)
1353 1.15 mycroft struct nfsd *nfsd;
1354 1.1 cgd struct mbuf *mrep, *md;
1355 1.1 cgd caddr_t dpos;
1356 1.1 cgd struct ucred *cred;
1357 1.15 mycroft struct mbuf *nam, **mrq;
1358 1.1 cgd {
1359 1.1 cgd register char *bp, *be;
1360 1.1 cgd register struct mbuf *mp;
1361 1.11 ws register struct dirent *dp;
1362 1.1 cgd register caddr_t cp;
1363 1.1 cgd register u_long *tl;
1364 1.1 cgd register long t1;
1365 1.1 cgd caddr_t bpos;
1366 1.15 mycroft struct mbuf *mb, *mb2, *mreq, *mp2;
1367 1.15 mycroft char *cpos, *cend, *cp2, *rbuf;
1368 1.1 cgd struct vnode *vp;
1369 1.1 cgd nfsv2fh_t nfh;
1370 1.1 cgd fhandle_t *fhp;
1371 1.1 cgd struct uio io;
1372 1.1 cgd struct iovec iv;
1373 1.15 mycroft int len, nlen, rem, xfer, tsiz, i, error = 0;
1374 1.15 mycroft int siz, cnt, fullsiz, eofflag, rdonly, cache;
1375 1.15 mycroft u_quad_t frev;
1376 1.15 mycroft u_long off, *cookiebuf, *cookie;
1377 1.8 ws int ncookies;
1378 1.15 mycroft
1379 1.1 cgd fhp = &nfh.fh_generic;
1380 1.1 cgd nfsm_srvmtofh(fhp);
1381 1.15 mycroft nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1382 1.15 mycroft off = fxdr_unsigned(u_long, *tl++);
1383 1.1 cgd cnt = fxdr_unsigned(int, *tl);
1384 1.1 cgd siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
1385 1.1 cgd if (cnt > NFS_MAXREADDIR)
1386 1.1 cgd siz = NFS_MAXREADDIR;
1387 1.1 cgd fullsiz = siz;
1388 1.15 mycroft ncookies = siz / 16; /* Guess at the number of cookies needed. */
1389 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
1390 1.1 cgd nfsm_reply(0);
1391 1.15 mycroft nqsrv_getl(vp, NQL_READ);
1392 1.15 mycroft if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
1393 1.1 cgd vput(vp);
1394 1.1 cgd nfsm_reply(0);
1395 1.1 cgd }
1396 1.1 cgd VOP_UNLOCK(vp);
1397 1.1 cgd MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1398 1.15 mycroft MALLOC(cookiebuf, u_long *, ncookies * sizeof(*cookiebuf), M_TEMP,
1399 1.15 mycroft M_WAITOK);
1400 1.1 cgd again:
1401 1.1 cgd iv.iov_base = rbuf;
1402 1.1 cgd iv.iov_len = fullsiz;
1403 1.1 cgd io.uio_iov = &iv;
1404 1.1 cgd io.uio_iovcnt = 1;
1405 1.15 mycroft io.uio_offset = (off_t)off;
1406 1.1 cgd io.uio_resid = fullsiz;
1407 1.1 cgd io.uio_segflg = UIO_SYSSPACE;
1408 1.1 cgd io.uio_rw = UIO_READ;
1409 1.1 cgd io.uio_procp = (struct proc *)0;
1410 1.8 ws error = VOP_READDIR(vp, &io, cred, &eofflag, cookiebuf, ncookies);
1411 1.8 ws cookie = cookiebuf;
1412 1.15 mycroft off = (off_t)io.uio_offset;
1413 1.1 cgd if (error) {
1414 1.1 cgd vrele(vp);
1415 1.15 mycroft free((caddr_t)cookiebuf, M_TEMP);
1416 1.1 cgd free((caddr_t)rbuf, M_TEMP);
1417 1.1 cgd nfsm_reply(0);
1418 1.1 cgd }
1419 1.1 cgd if (io.uio_resid) {
1420 1.1 cgd siz -= io.uio_resid;
1421 1.1 cgd
1422 1.1 cgd /*
1423 1.1 cgd * If nothing read, return eof
1424 1.1 cgd * rpc reply
1425 1.1 cgd */
1426 1.1 cgd if (siz == 0) {
1427 1.1 cgd vrele(vp);
1428 1.1 cgd nfsm_reply(2*NFSX_UNSIGNED);
1429 1.1 cgd nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
1430 1.1 cgd *tl++ = nfs_false;
1431 1.1 cgd *tl = nfs_true;
1432 1.8 ws FREE((caddr_t)cookiebuf, M_TEMP);
1433 1.1 cgd FREE((caddr_t)rbuf, M_TEMP);
1434 1.1 cgd return (0);
1435 1.1 cgd }
1436 1.1 cgd }
1437 1.1 cgd
1438 1.1 cgd /*
1439 1.1 cgd * Check for degenerate cases of nothing useful read.
1440 1.1 cgd * If so go try again
1441 1.1 cgd */
1442 1.8 ws cpos = rbuf;
1443 1.1 cgd cend = rbuf + siz;
1444 1.8 ws while (cpos < cend) {
1445 1.11 ws dp = (struct dirent *)cpos;
1446 1.15 mycroft if (dp->d_fileno == 0) {
1447 1.8 ws cpos += dp->d_reclen;
1448 1.8 ws cookie++;
1449 1.8 ws } else
1450 1.8 ws break;
1451 1.1 cgd }
1452 1.1 cgd if (cpos >= cend) {
1453 1.1 cgd siz = fullsiz;
1454 1.1 cgd goto again;
1455 1.1 cgd }
1456 1.1 cgd
1457 1.15 mycroft len = 3*NFSX_UNSIGNED; /* paranoia, probably can be 0 */
1458 1.15 mycroft nfsm_reply(siz);
1459 1.15 mycroft mp = mp2 = mb;
1460 1.15 mycroft bp = bpos;
1461 1.15 mycroft be = bp + M_TRAILINGSPACE(mp);
1462 1.15 mycroft
1463 1.15 mycroft /* Loop through the records and build reply */
1464 1.15 mycroft while (cpos < cend) {
1465 1.15 mycroft if (dp->d_fileno != 0) {
1466 1.15 mycroft nlen = dp->d_namlen;
1467 1.15 mycroft rem = nfsm_rndup(nlen)-nlen;
1468 1.15 mycroft len += (4*NFSX_UNSIGNED + nlen + rem);
1469 1.15 mycroft if (len > cnt) {
1470 1.15 mycroft eofflag = 0;
1471 1.15 mycroft break;
1472 1.15 mycroft }
1473 1.15 mycroft /*
1474 1.15 mycroft * Build the directory record xdr from
1475 1.15 mycroft * the dirent entry.
1476 1.15 mycroft */
1477 1.15 mycroft nfsm_clget;
1478 1.15 mycroft *tl = nfs_true;
1479 1.15 mycroft bp += NFSX_UNSIGNED;
1480 1.15 mycroft nfsm_clget;
1481 1.15 mycroft *tl = txdr_unsigned(dp->d_fileno);
1482 1.15 mycroft bp += NFSX_UNSIGNED;
1483 1.15 mycroft nfsm_clget;
1484 1.15 mycroft *tl = txdr_unsigned(nlen);
1485 1.15 mycroft bp += NFSX_UNSIGNED;
1486 1.15 mycroft
1487 1.15 mycroft /* And loop around copying the name */
1488 1.15 mycroft xfer = nlen;
1489 1.15 mycroft cp = dp->d_name;
1490 1.15 mycroft while (xfer > 0) {
1491 1.15 mycroft nfsm_clget;
1492 1.15 mycroft if ((bp+xfer) > be)
1493 1.15 mycroft tsiz = be-bp;
1494 1.15 mycroft else
1495 1.15 mycroft tsiz = xfer;
1496 1.15 mycroft bcopy(cp, bp, tsiz);
1497 1.15 mycroft bp += tsiz;
1498 1.15 mycroft xfer -= tsiz;
1499 1.15 mycroft if (xfer > 0)
1500 1.15 mycroft cp += tsiz;
1501 1.15 mycroft }
1502 1.15 mycroft /* And null pad to a long boundary */
1503 1.15 mycroft for (i = 0; i < rem; i++)
1504 1.15 mycroft *bp++ = '\0';
1505 1.15 mycroft nfsm_clget;
1506 1.15 mycroft
1507 1.15 mycroft /* Finish off the record */
1508 1.15 mycroft *tl = txdr_unsigned(*cookie);
1509 1.15 mycroft bp += NFSX_UNSIGNED;
1510 1.15 mycroft }
1511 1.15 mycroft cpos += dp->d_reclen;
1512 1.15 mycroft dp = (struct dirent *)cpos;
1513 1.15 mycroft cookie++;
1514 1.15 mycroft }
1515 1.1 cgd vrele(vp);
1516 1.15 mycroft nfsm_clget;
1517 1.15 mycroft *tl = nfs_false;
1518 1.15 mycroft bp += NFSX_UNSIGNED;
1519 1.15 mycroft nfsm_clget;
1520 1.15 mycroft if (eofflag)
1521 1.15 mycroft *tl = nfs_true;
1522 1.15 mycroft else
1523 1.15 mycroft *tl = nfs_false;
1524 1.15 mycroft bp += NFSX_UNSIGNED;
1525 1.15 mycroft if (mp != mb) {
1526 1.15 mycroft if (bp < be)
1527 1.15 mycroft mp->m_len = bp - mtod(mp, caddr_t);
1528 1.15 mycroft } else
1529 1.15 mycroft mp->m_len += bp - bpos;
1530 1.15 mycroft FREE(cookiebuf, M_TEMP);
1531 1.15 mycroft FREE(rbuf, M_TEMP);
1532 1.15 mycroft nfsm_srvdone;
1533 1.15 mycroft }
1534 1.15 mycroft
1535 1.15 mycroft nqnfsrv_readdirlook(nfsd, mrep, md, dpos, cred, nam, mrq)
1536 1.15 mycroft struct nfsd *nfsd;
1537 1.15 mycroft struct mbuf *mrep, *md;
1538 1.15 mycroft caddr_t dpos;
1539 1.15 mycroft struct ucred *cred;
1540 1.15 mycroft struct mbuf *nam, **mrq;
1541 1.15 mycroft {
1542 1.15 mycroft register char *bp, *be;
1543 1.15 mycroft register struct mbuf *mp;
1544 1.15 mycroft register struct dirent *dp;
1545 1.15 mycroft register caddr_t cp;
1546 1.15 mycroft register u_long *tl;
1547 1.15 mycroft register long t1;
1548 1.15 mycroft caddr_t bpos;
1549 1.15 mycroft struct mbuf *mb, *mb2, *mreq, *mp2;
1550 1.15 mycroft char *cpos, *cend, *cp2, *rbuf;
1551 1.15 mycroft struct vnode *vp, *nvp;
1552 1.15 mycroft struct flrep fl;
1553 1.15 mycroft nfsv2fh_t nfh;
1554 1.15 mycroft fhandle_t *fhp;
1555 1.15 mycroft struct uio io;
1556 1.15 mycroft struct iovec iv;
1557 1.15 mycroft struct vattr va, *vap = &va;
1558 1.15 mycroft struct nfsv2_fattr *fp;
1559 1.15 mycroft int len, nlen, rem, xfer, tsiz, i, error = 0, duration2, cache2;
1560 1.15 mycroft int siz, cnt, fullsiz, eofflag, rdonly, cache;
1561 1.15 mycroft u_quad_t frev, frev2;
1562 1.15 mycroft u_long off, *cookiebuf, *cookie;
1563 1.15 mycroft int ncookies;
1564 1.15 mycroft
1565 1.15 mycroft fhp = &nfh.fh_generic;
1566 1.15 mycroft nfsm_srvmtofh(fhp);
1567 1.15 mycroft nfsm_dissect(tl, u_long *, 3*NFSX_UNSIGNED);
1568 1.15 mycroft off = fxdr_unsigned(u_long, *tl++);
1569 1.15 mycroft cnt = fxdr_unsigned(int, *tl++);
1570 1.15 mycroft duration2 = fxdr_unsigned(int, *tl);
1571 1.15 mycroft siz = ((cnt+NFS_DIRBLKSIZ-1) & ~(NFS_DIRBLKSIZ-1));
1572 1.15 mycroft if (cnt > NFS_MAXREADDIR)
1573 1.15 mycroft siz = NFS_MAXREADDIR;
1574 1.15 mycroft fullsiz = siz;
1575 1.15 mycroft ncookies = siz / 16; /* Guess at the number of cookies needed. */
1576 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
1577 1.15 mycroft nfsm_reply(0);
1578 1.15 mycroft nqsrv_getl(vp, NQL_READ);
1579 1.15 mycroft if (error = nfsrv_access(vp, VEXEC, cred, rdonly, nfsd->nd_procp)) {
1580 1.15 mycroft vput(vp);
1581 1.15 mycroft nfsm_reply(0);
1582 1.15 mycroft }
1583 1.15 mycroft VOP_UNLOCK(vp);
1584 1.15 mycroft MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1585 1.15 mycroft MALLOC(cookiebuf, u_long *, ncookies * sizeof(*cookiebuf), M_TEMP,
1586 1.15 mycroft M_WAITOK);
1587 1.15 mycroft again:
1588 1.15 mycroft iv.iov_base = rbuf;
1589 1.15 mycroft iv.iov_len = fullsiz;
1590 1.15 mycroft io.uio_iov = &iv;
1591 1.15 mycroft io.uio_iovcnt = 1;
1592 1.15 mycroft io.uio_offset = (off_t)off;
1593 1.15 mycroft io.uio_resid = fullsiz;
1594 1.15 mycroft io.uio_segflg = UIO_SYSSPACE;
1595 1.15 mycroft io.uio_rw = UIO_READ;
1596 1.15 mycroft io.uio_procp = (struct proc *)0;
1597 1.15 mycroft error = VOP_READDIR(vp, &io, cred, &eofflag, cookiebuf, ncookies);
1598 1.15 mycroft cookie = cookiebuf;
1599 1.15 mycroft off = (u_long)io.uio_offset;
1600 1.15 mycroft if (error) {
1601 1.15 mycroft vrele(vp);
1602 1.15 mycroft free((caddr_t)cookiebuf, M_TEMP);
1603 1.15 mycroft free((caddr_t)rbuf, M_TEMP);
1604 1.15 mycroft nfsm_reply(0);
1605 1.15 mycroft }
1606 1.15 mycroft if (io.uio_resid) {
1607 1.15 mycroft siz -= io.uio_resid;
1608 1.15 mycroft
1609 1.15 mycroft /*
1610 1.15 mycroft * If nothing read, return eof
1611 1.15 mycroft * rpc reply
1612 1.15 mycroft */
1613 1.15 mycroft if (siz == 0) {
1614 1.15 mycroft vrele(vp);
1615 1.15 mycroft nfsm_reply(2 * NFSX_UNSIGNED);
1616 1.15 mycroft nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
1617 1.15 mycroft *tl++ = nfs_false;
1618 1.15 mycroft *tl = nfs_true;
1619 1.15 mycroft FREE((caddr_t)cookiebuf, M_TEMP);
1620 1.15 mycroft FREE((caddr_t)rbuf, M_TEMP);
1621 1.15 mycroft return (0);
1622 1.15 mycroft }
1623 1.15 mycroft }
1624 1.15 mycroft
1625 1.15 mycroft /*
1626 1.15 mycroft * Check for degenerate cases of nothing useful read.
1627 1.15 mycroft * If so go try again
1628 1.15 mycroft */
1629 1.15 mycroft cpos = rbuf;
1630 1.15 mycroft cend = rbuf + siz;
1631 1.15 mycroft while (cpos < cend) {
1632 1.15 mycroft dp = (struct dirent *)cpos;
1633 1.15 mycroft if (dp->d_fileno == 0) {
1634 1.15 mycroft cpos += dp->d_reclen;
1635 1.15 mycroft cookie++;
1636 1.15 mycroft } else
1637 1.15 mycroft break;
1638 1.15 mycroft }
1639 1.15 mycroft if (cpos >= cend) {
1640 1.15 mycroft siz = fullsiz;
1641 1.15 mycroft goto again;
1642 1.15 mycroft }
1643 1.15 mycroft
1644 1.15 mycroft len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
1645 1.1 cgd nfsm_reply(siz);
1646 1.15 mycroft mp = mp2 = mb;
1647 1.15 mycroft bp = bpos;
1648 1.15 mycroft be = bp + M_TRAILINGSPACE(mp);
1649 1.1 cgd
1650 1.1 cgd /* Loop through the records and build reply */
1651 1.1 cgd while (cpos < cend) {
1652 1.11 ws if (dp->d_fileno != 0) {
1653 1.1 cgd nlen = dp->d_namlen;
1654 1.1 cgd rem = nfsm_rndup(nlen)-nlen;
1655 1.1 cgd
1656 1.1 cgd /*
1657 1.15 mycroft * For readdir_and_lookup get the vnode using
1658 1.15 mycroft * the file number.
1659 1.1 cgd */
1660 1.15 mycroft if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
1661 1.15 mycroft goto invalid;
1662 1.15 mycroft bzero((caddr_t)&fl.fl_nfh, sizeof (nfsv2fh_t));
1663 1.15 mycroft fl.fl_nfh.fh_generic.fh_fsid =
1664 1.15 mycroft nvp->v_mount->mnt_stat.f_fsid;
1665 1.15 mycroft if (VFS_VPTOFH(nvp, &fl.fl_nfh.fh_generic.fh_fid)) {
1666 1.15 mycroft vput(nvp);
1667 1.15 mycroft goto invalid;
1668 1.15 mycroft }
1669 1.15 mycroft if (duration2) {
1670 1.15 mycroft (void) nqsrv_getlease(nvp, &duration2, NQL_READ,
1671 1.15 mycroft nfsd, nam, &cache2, &frev2, cred);
1672 1.15 mycroft fl.fl_duration = txdr_unsigned(duration2);
1673 1.15 mycroft fl.fl_cachable = txdr_unsigned(cache2);
1674 1.15 mycroft txdr_hyper(&frev2, fl.fl_frev);
1675 1.15 mycroft } else
1676 1.15 mycroft fl.fl_duration = 0;
1677 1.15 mycroft if (VOP_GETATTR(nvp, vap, cred, nfsd->nd_procp)) {
1678 1.15 mycroft vput(nvp);
1679 1.15 mycroft goto invalid;
1680 1.15 mycroft }
1681 1.15 mycroft vput(nvp);
1682 1.15 mycroft fp = (struct nfsv2_fattr *)&fl.fl_fattr;
1683 1.15 mycroft nfsm_srvfillattr;
1684 1.15 mycroft len += (4*NFSX_UNSIGNED + nlen + rem + NFSX_FH
1685 1.15 mycroft + NFSX_NQFATTR);
1686 1.1 cgd if (len > cnt) {
1687 1.1 cgd eofflag = 0;
1688 1.1 cgd break;
1689 1.1 cgd }
1690 1.15 mycroft /*
1691 1.15 mycroft * Build the directory record xdr from
1692 1.15 mycroft * the dirent entry.
1693 1.15 mycroft */
1694 1.1 cgd nfsm_clget;
1695 1.1 cgd *tl = nfs_true;
1696 1.1 cgd bp += NFSX_UNSIGNED;
1697 1.15 mycroft
1698 1.15 mycroft /*
1699 1.15 mycroft * For readdir_and_lookup copy the stuff out.
1700 1.15 mycroft */
1701 1.15 mycroft xfer = sizeof (struct flrep);
1702 1.15 mycroft cp = (caddr_t)&fl;
1703 1.15 mycroft while (xfer > 0) {
1704 1.15 mycroft nfsm_clget;
1705 1.15 mycroft if ((bp+xfer) > be)
1706 1.15 mycroft tsiz = be-bp;
1707 1.15 mycroft else
1708 1.15 mycroft tsiz = xfer;
1709 1.15 mycroft bcopy(cp, bp, tsiz);
1710 1.15 mycroft bp += tsiz;
1711 1.15 mycroft xfer -= tsiz;
1712 1.15 mycroft if (xfer > 0)
1713 1.15 mycroft cp += tsiz;
1714 1.15 mycroft }
1715 1.1 cgd nfsm_clget;
1716 1.11 ws *tl = txdr_unsigned(dp->d_fileno);
1717 1.1 cgd bp += NFSX_UNSIGNED;
1718 1.1 cgd nfsm_clget;
1719 1.1 cgd *tl = txdr_unsigned(nlen);
1720 1.1 cgd bp += NFSX_UNSIGNED;
1721 1.1 cgd
1722 1.15 mycroft /* And loop around copying the name */
1723 1.1 cgd xfer = nlen;
1724 1.1 cgd cp = dp->d_name;
1725 1.1 cgd while (xfer > 0) {
1726 1.1 cgd nfsm_clget;
1727 1.1 cgd if ((bp+xfer) > be)
1728 1.1 cgd tsiz = be-bp;
1729 1.1 cgd else
1730 1.1 cgd tsiz = xfer;
1731 1.1 cgd bcopy(cp, bp, tsiz);
1732 1.1 cgd bp += tsiz;
1733 1.1 cgd xfer -= tsiz;
1734 1.1 cgd if (xfer > 0)
1735 1.1 cgd cp += tsiz;
1736 1.1 cgd }
1737 1.1 cgd /* And null pad to a long boundary */
1738 1.1 cgd for (i = 0; i < rem; i++)
1739 1.1 cgd *bp++ = '\0';
1740 1.1 cgd nfsm_clget;
1741 1.1 cgd
1742 1.1 cgd /* Finish off the record */
1743 1.8 ws *tl = txdr_unsigned(*cookie);
1744 1.1 cgd bp += NFSX_UNSIGNED;
1745 1.8 ws }
1746 1.15 mycroft invalid:
1747 1.1 cgd cpos += dp->d_reclen;
1748 1.11 ws dp = (struct dirent *)cpos;
1749 1.8 ws cookie++;
1750 1.1 cgd }
1751 1.15 mycroft vrele(vp);
1752 1.1 cgd nfsm_clget;
1753 1.1 cgd *tl = nfs_false;
1754 1.1 cgd bp += NFSX_UNSIGNED;
1755 1.1 cgd nfsm_clget;
1756 1.1 cgd if (eofflag)
1757 1.1 cgd *tl = nfs_true;
1758 1.1 cgd else
1759 1.1 cgd *tl = nfs_false;
1760 1.1 cgd bp += NFSX_UNSIGNED;
1761 1.15 mycroft if (mp != mb) {
1762 1.15 mycroft if (bp < be)
1763 1.15 mycroft mp->m_len = bp - mtod(mp, caddr_t);
1764 1.15 mycroft } else
1765 1.15 mycroft mp->m_len += bp - bpos;
1766 1.8 ws FREE(cookiebuf, M_TEMP);
1767 1.1 cgd FREE(rbuf, M_TEMP);
1768 1.1 cgd nfsm_srvdone;
1769 1.1 cgd }
1770 1.1 cgd
1771 1.1 cgd /*
1772 1.1 cgd * nfs statfs service
1773 1.1 cgd */
1774 1.15 mycroft nfsrv_statfs(nfsd, mrep, md, dpos, cred, nam, mrq)
1775 1.15 mycroft struct nfsd *nfsd;
1776 1.1 cgd struct mbuf *mrep, *md;
1777 1.1 cgd caddr_t dpos;
1778 1.1 cgd struct ucred *cred;
1779 1.15 mycroft struct mbuf *nam, **mrq;
1780 1.1 cgd {
1781 1.1 cgd register struct statfs *sf;
1782 1.1 cgd register struct nfsv2_statfs *sfp;
1783 1.1 cgd register u_long *tl;
1784 1.1 cgd register long t1;
1785 1.1 cgd caddr_t bpos;
1786 1.15 mycroft int error = 0, rdonly, cache, isnq;
1787 1.1 cgd char *cp2;
1788 1.1 cgd struct mbuf *mb, *mb2, *mreq;
1789 1.1 cgd struct vnode *vp;
1790 1.1 cgd nfsv2fh_t nfh;
1791 1.1 cgd fhandle_t *fhp;
1792 1.1 cgd struct statfs statfs;
1793 1.15 mycroft u_quad_t frev;
1794 1.1 cgd
1795 1.1 cgd fhp = &nfh.fh_generic;
1796 1.15 mycroft isnq = (nfsd->nd_nqlflag != NQL_NOVAL);
1797 1.1 cgd nfsm_srvmtofh(fhp);
1798 1.15 mycroft if (error = nfsrv_fhtovp(fhp, TRUE, &vp, cred, nfsd->nd_slp, nam, &rdonly))
1799 1.1 cgd nfsm_reply(0);
1800 1.1 cgd sf = &statfs;
1801 1.15 mycroft error = VFS_STATFS(vp->v_mount, sf, nfsd->nd_procp);
1802 1.1 cgd vput(vp);
1803 1.15 mycroft nfsm_reply(NFSX_STATFS(isnq));
1804 1.15 mycroft nfsm_build(sfp, struct nfsv2_statfs *, NFSX_STATFS(isnq));
1805 1.1 cgd sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
1806 1.14 cgd sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
1807 1.1 cgd sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
1808 1.1 cgd sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
1809 1.1 cgd sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
1810 1.15 mycroft if (isnq) {
1811 1.15 mycroft sfp->sf_files = txdr_unsigned(sf->f_files);
1812 1.15 mycroft sfp->sf_ffree = txdr_unsigned(sf->f_ffree);
1813 1.15 mycroft }
1814 1.1 cgd nfsm_srvdone;
1815 1.1 cgd }
1816 1.1 cgd
1817 1.1 cgd /*
1818 1.1 cgd * Null operation, used by clients to ping server
1819 1.1 cgd */
1820 1.1 cgd /* ARGSUSED */
1821 1.15 mycroft nfsrv_null(nfsd, mrep, md, dpos, cred, nam, mrq)
1822 1.15 mycroft struct nfsd *nfsd;
1823 1.1 cgd struct mbuf *mrep, *md;
1824 1.1 cgd caddr_t dpos;
1825 1.1 cgd struct ucred *cred;
1826 1.15 mycroft struct mbuf *nam, **mrq;
1827 1.1 cgd {
1828 1.1 cgd caddr_t bpos;
1829 1.15 mycroft int error = VNOVAL, cache;
1830 1.1 cgd struct mbuf *mb, *mreq;
1831 1.15 mycroft u_quad_t frev;
1832 1.1 cgd
1833 1.1 cgd nfsm_reply(0);
1834 1.1 cgd return (error);
1835 1.1 cgd }
1836 1.1 cgd
1837 1.1 cgd /*
1838 1.1 cgd * No operation, used for obsolete procedures
1839 1.1 cgd */
1840 1.1 cgd /* ARGSUSED */
1841 1.15 mycroft nfsrv_noop(nfsd, mrep, md, dpos, cred, nam, mrq)
1842 1.15 mycroft struct nfsd *nfsd;
1843 1.1 cgd struct mbuf *mrep, *md;
1844 1.1 cgd caddr_t dpos;
1845 1.1 cgd struct ucred *cred;
1846 1.15 mycroft struct mbuf *nam, **mrq;
1847 1.1 cgd {
1848 1.1 cgd caddr_t bpos;
1849 1.15 mycroft int error, cache;
1850 1.1 cgd struct mbuf *mb, *mreq;
1851 1.15 mycroft u_quad_t frev;
1852 1.1 cgd
1853 1.15 mycroft if (nfsd->nd_repstat)
1854 1.15 mycroft error = nfsd->nd_repstat;
1855 1.2 cgd else
1856 1.2 cgd error = EPROCUNAVAIL;
1857 1.1 cgd nfsm_reply(0);
1858 1.1 cgd return (error);
1859 1.1 cgd }
1860 1.1 cgd
1861 1.1 cgd /*
1862 1.1 cgd * Perform access checking for vnodes obtained from file handles that would
1863 1.1 cgd * refer to files already opened by a Unix client. You cannot just use
1864 1.1 cgd * vn_writechk() and VOP_ACCESS() for two reasons.
1865 1.15 mycroft * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
1866 1.1 cgd * 2 - The owner is to be given access irrespective of mode bits so that
1867 1.1 cgd * processes that chmod after opening a file don't break. I don't like
1868 1.1 cgd * this because it opens a security hole, but since the nfs server opens
1869 1.1 cgd * a security hole the size of a barn door anyhow, what the heck.
1870 1.1 cgd */
1871 1.15 mycroft nfsrv_access(vp, flags, cred, rdonly, p)
1872 1.1 cgd register struct vnode *vp;
1873 1.1 cgd int flags;
1874 1.1 cgd register struct ucred *cred;
1875 1.15 mycroft int rdonly;
1876 1.1 cgd struct proc *p;
1877 1.1 cgd {
1878 1.1 cgd struct vattr vattr;
1879 1.1 cgd int error;
1880 1.1 cgd if (flags & VWRITE) {
1881 1.15 mycroft /* Just vn_writechk() changed to check rdonly */
1882 1.1 cgd /*
1883 1.1 cgd * Disallow write attempts on read-only file systems;
1884 1.1 cgd * unless the file is a socket or a block or character
1885 1.1 cgd * device resident on the file system.
1886 1.1 cgd */
1887 1.15 mycroft if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
1888 1.1 cgd switch (vp->v_type) {
1889 1.1 cgd case VREG: case VDIR: case VLNK:
1890 1.1 cgd return (EROFS);
1891 1.1 cgd }
1892 1.1 cgd }
1893 1.1 cgd /*
1894 1.1 cgd * If there's shared text associated with
1895 1.1 cgd * the inode, try to free it up once. If
1896 1.1 cgd * we fail, we can't allow writing.
1897 1.1 cgd */
1898 1.1 cgd if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
1899 1.1 cgd return (ETXTBSY);
1900 1.1 cgd }
1901 1.1 cgd if (error = VOP_GETATTR(vp, &vattr, cred, p))
1902 1.1 cgd return (error);
1903 1.1 cgd if ((error = VOP_ACCESS(vp, flags, cred, p)) &&
1904 1.1 cgd cred->cr_uid != vattr.va_uid)
1905 1.1 cgd return (error);
1906 1.1 cgd return (0);
1907 1.1 cgd }
1908