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