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