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