nfs_serv.c revision 1.38 1 /* $NetBSD: nfs_serv.c,v 1.38 1997/10/10 01:53:22 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)nfs_serv.c 8.7 (Berkeley) 5/14/95
39 */
40
41 /*
42 * nfs version 2 and 3 server calls to vnode ops
43 * - these routines generally have 3 phases
44 * 1 - break down and validate rpc request in mbuf list
45 * 2 - do the vnode ops for the request
46 * (surprisingly ?? many are very similar to syscalls in vfs_syscalls.c)
47 * 3 - build the rpc reply in an mbuf list
48 * nb:
49 * - do not mix the phases, since the nfsm_?? macros can return failures
50 * on a bad rpc or similar and do not do any vrele() or vput()'s
51 *
52 * - the nfsm_reply() macro generates an nfs rpc reply with the nfs
53 * error number iff error != 0 whereas
54 * returning an error from the server function implies a fatal error
55 * such as a badly constructed rpc request that should be dropped without
56 * a reply.
57 * For Version 3, nfsm_reply() does not return for the error case, since
58 * most version 3 rpcs return more than the status for error cases.
59 */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/proc.h>
64 #include <sys/file.h>
65 #include <sys/namei.h>
66 #include <sys/vnode.h>
67 #include <sys/mount.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/mbuf.h>
71 #include <sys/dirent.h>
72 #include <sys/stat.h>
73 #include <sys/kernel.h>
74 #include <ufs/ufs/dir.h>
75
76 #include <vm/vm.h>
77
78 #include <nfs/nfsproto.h>
79 #include <nfs/rpcv2.h>
80 #include <nfs/nfs.h>
81 #include <nfs/xdr_subs.h>
82 #include <nfs/nfsm_subs.h>
83 #include <nfs/nqnfs.h>
84 #include <nfs/nfs_var.h>
85
86 /* Global vars */
87 extern u_int32_t nfs_xdrneg1;
88 extern u_int32_t nfs_false, nfs_true;
89 extern enum vtype nv3tov_type[8];
90 extern struct nfsstats nfsstats;
91 extern nfstype nfsv2_type[9];
92 extern nfstype nfsv3_type[9];
93 extern struct nfs_public nfs_pub;
94 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
95
96 /*
97 * nfs v3 access service
98 */
99 int
100 nfsrv3_access(nfsd, slp, procp, mrq)
101 struct nfsrv_descript *nfsd;
102 struct nfssvc_sock *slp;
103 struct proc *procp;
104 struct mbuf **mrq;
105 {
106 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
107 struct mbuf *nam = nfsd->nd_nam;
108 caddr_t dpos = nfsd->nd_dpos;
109 struct ucred *cred = &nfsd->nd_cr;
110 struct vnode *vp;
111 nfsfh_t nfh;
112 fhandle_t *fhp;
113 register u_int32_t *tl;
114 register int32_t t1;
115 caddr_t bpos;
116 int error = 0, rdonly, cache = 0, getret;
117 char *cp2;
118 struct mbuf *mb, *mreq, *mb2;
119 struct vattr va;
120 u_long inmode, testmode, outmode;
121 u_quad_t frev;
122
123 fhp = &nfh.fh_generic;
124 nfsm_srvmtofh(fhp);
125 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
126 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
127 (nfsd->nd_flag & ND_KERBAUTH), FALSE);
128 if (error) {
129 nfsm_reply(NFSX_UNSIGNED);
130 nfsm_srvpostop_attr(1, (struct vattr *)0);
131 return (0);
132 }
133 inmode = fxdr_unsigned(u_int32_t, *tl);
134 outmode = 0;
135 if ((inmode & NFSV3ACCESS_READ) &&
136 nfsrv_access(vp, VREAD, cred, rdonly, procp, 0) == 0)
137 outmode |= NFSV3ACCESS_READ;
138 if (vp->v_type != VDIR) {
139 testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND);
140 if (testmode &&
141 nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0) == 0)
142 outmode |= testmode;
143 if ((inmode & NFSV3ACCESS_EXECUTE) &&
144 nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0) == 0)
145 outmode |= NFSV3ACCESS_EXECUTE;
146 } else {
147 testmode = inmode & (NFSV3ACCESS_MODIFY | NFSV3ACCESS_EXTEND |
148 NFSV3ACCESS_DELETE);
149 if (testmode &&
150 nfsrv_access(vp, VWRITE, cred, rdonly, procp, 0) == 0)
151 outmode |= testmode;
152 if ((inmode & NFSV3ACCESS_LOOKUP) &&
153 nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0) == 0)
154 outmode |= NFSV3ACCESS_LOOKUP;
155 }
156 getret = VOP_GETATTR(vp, &va, cred, procp);
157 vput(vp);
158 nfsm_reply(NFSX_POSTOPATTR(1) + NFSX_UNSIGNED);
159 nfsm_srvpostop_attr(getret, &va);
160 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
161 *tl = txdr_unsigned(outmode);
162 nfsm_srvdone;
163 }
164
165 /*
166 * nfs getattr service
167 */
168 int
169 nfsrv_getattr(nfsd, slp, procp, mrq)
170 struct nfsrv_descript *nfsd;
171 struct nfssvc_sock *slp;
172 struct proc *procp;
173 struct mbuf **mrq;
174 {
175 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
176 struct mbuf *nam = nfsd->nd_nam;
177 caddr_t dpos = nfsd->nd_dpos;
178 struct ucred *cred = &nfsd->nd_cr;
179 register struct nfs_fattr *fp;
180 struct vattr va;
181 struct vnode *vp;
182 nfsfh_t nfh;
183 fhandle_t *fhp;
184 register u_int32_t *tl;
185 register int32_t t1;
186 caddr_t bpos;
187 int error = 0, rdonly, cache;
188 char *cp2;
189 struct mbuf *mb, *mb2, *mreq;
190 u_quad_t frev;
191
192 fhp = &nfh.fh_generic;
193 nfsm_srvmtofh(fhp);
194 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
195 (nfsd->nd_flag & ND_KERBAUTH), FALSE);
196 if (error) {
197 nfsm_reply(0);
198 return (0);
199 }
200 nqsrv_getl(vp, ND_READ);
201 error = VOP_GETATTR(vp, &va, cred, procp);
202 vput(vp);
203 nfsm_reply(NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
204 if (error)
205 return (0);
206 nfsm_build(fp, struct nfs_fattr *, NFSX_FATTR(nfsd->nd_flag & ND_NFSV3));
207 nfsm_srvfillattr(&va, fp);
208 nfsm_srvdone;
209 }
210
211 /*
212 * nfs setattr service
213 */
214 int
215 nfsrv_setattr(nfsd, slp, procp, mrq)
216 struct nfsrv_descript *nfsd;
217 struct nfssvc_sock *slp;
218 struct proc *procp;
219 struct mbuf **mrq;
220 {
221 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
222 struct mbuf *nam = nfsd->nd_nam;
223 caddr_t dpos = nfsd->nd_dpos;
224 struct ucred *cred = &nfsd->nd_cr;
225 struct vattr va, preat;
226 register struct nfsv2_sattr *sp;
227 register struct nfs_fattr *fp;
228 struct vnode *vp;
229 nfsfh_t nfh;
230 fhandle_t *fhp;
231 register u_int32_t *tl;
232 register int32_t t1;
233 caddr_t bpos;
234 int error = 0, rdonly, cache, preat_ret = 1, postat_ret = 1;
235 int v3 = (nfsd->nd_flag & ND_NFSV3), gcheck = 0;
236 char *cp2;
237 struct mbuf *mb, *mb2, *mreq;
238 u_quad_t frev;
239 struct timespec guard;
240
241 fhp = &nfh.fh_generic;
242 nfsm_srvmtofh(fhp);
243 VATTR_NULL(&va);
244 if (v3) {
245 nfsm_srvsattr(&va);
246 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
247 gcheck = fxdr_unsigned(int, *tl);
248 if (gcheck) {
249 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
250 fxdr_nfsv3time(tl, &guard);
251 }
252 } else {
253 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
254 /*
255 * Nah nah nah nah na nah
256 * There is a bug in the Sun client that puts 0xffff in the mode
257 * field of sattr when it should put in 0xffffffff. The u_short
258 * doesn't sign extend.
259 * --> check the low order 2 bytes for 0xffff
260 */
261 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
262 va.va_mode = nfstov_mode(sp->sa_mode);
263 if (sp->sa_uid != nfs_xdrneg1)
264 va.va_uid = fxdr_unsigned(uid_t, sp->sa_uid);
265 if (sp->sa_gid != nfs_xdrneg1)
266 va.va_gid = fxdr_unsigned(gid_t, sp->sa_gid);
267 if (sp->sa_size != nfs_xdrneg1)
268 va.va_size = fxdr_unsigned(u_quad_t, sp->sa_size);
269 if (sp->sa_atime.nfsv2_sec != nfs_xdrneg1) {
270 #ifdef notyet
271 fxdr_nfsv2time(&sp->sa_atime, &va.va_atime);
272 #else
273 va.va_atime.tv_sec =
274 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
275 va.va_atime.tv_nsec = 0;
276 #endif
277 }
278 if (sp->sa_mtime.nfsv2_sec != nfs_xdrneg1)
279 fxdr_nfsv2time(&sp->sa_mtime, &va.va_mtime);
280
281 }
282
283 /*
284 * Now that we have all the fields, lets do it.
285 */
286 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam, &rdonly,
287 (nfsd->nd_flag & ND_KERBAUTH), FALSE);
288 if (error) {
289 nfsm_reply(2 * NFSX_UNSIGNED);
290 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
291 return (0);
292 }
293 nqsrv_getl(vp, ND_WRITE);
294 if (v3) {
295 error = preat_ret = VOP_GETATTR(vp, &preat, cred, procp);
296 if (!error && gcheck &&
297 (preat.va_ctime.tv_sec != guard.tv_sec ||
298 preat.va_ctime.tv_nsec != guard.tv_nsec))
299 error = NFSERR_NOT_SYNC;
300 if (error) {
301 vput(vp);
302 nfsm_reply(NFSX_WCCDATA(v3));
303 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
304 return (0);
305 }
306 }
307
308 /*
309 * If the size is being changed write acces is required, otherwise
310 * just check for a read only file system.
311 */
312 if (va.va_size == ((u_quad_t)((quad_t) -1))) {
313 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
314 error = EROFS;
315 goto out;
316 }
317 } else {
318 if (vp->v_type == VDIR) {
319 error = EISDIR;
320 goto out;
321 } else if ((error = nfsrv_access(vp, VWRITE, cred, rdonly,
322 procp, 0)) != 0)
323 goto out;
324 }
325 error = VOP_SETATTR(vp, &va, cred, procp);
326 postat_ret = VOP_GETATTR(vp, &va, cred, procp);
327 if (!error)
328 error = postat_ret;
329 out:
330 vput(vp);
331 nfsm_reply(NFSX_WCCORFATTR(v3));
332 if (v3) {
333 nfsm_srvwcc_data(preat_ret, &preat, postat_ret, &va);
334 return (0);
335 } else {
336 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
337 nfsm_srvfillattr(&va, fp);
338 }
339 nfsm_srvdone;
340 }
341
342 /*
343 * nfs lookup rpc
344 */
345 int
346 nfsrv_lookup(nfsd, slp, procp, mrq)
347 struct nfsrv_descript *nfsd;
348 struct nfssvc_sock *slp;
349 struct proc *procp;
350 struct mbuf **mrq;
351 {
352 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
353 struct mbuf *nam = nfsd->nd_nam;
354 caddr_t dpos = nfsd->nd_dpos;
355 struct ucred *cred = &nfsd->nd_cr;
356 register struct nfs_fattr *fp;
357 struct nameidata nd, ind, *ndp = &nd;
358 struct vnode *vp, *dirp;
359 nfsfh_t nfh;
360 fhandle_t *fhp;
361 register caddr_t cp;
362 register u_int32_t *tl;
363 register int32_t t1;
364 caddr_t bpos;
365 int error = 0, cache, len, dirattr_ret = 1;
366 int v3 = (nfsd->nd_flag & ND_NFSV3), pubflag;
367 char *cp2;
368 struct mbuf *mb, *mb2, *mreq;
369 struct vattr va, dirattr;
370 u_quad_t frev;
371
372 fhp = &nfh.fh_generic;
373 nfsm_srvmtofh(fhp);
374 nfsm_srvnamesiz(len);
375
376 pubflag = nfs_ispublicfh(fhp);
377
378 nd.ni_cnd.cn_cred = cred;
379 nd.ni_cnd.cn_nameiop = LOOKUP;
380 nd.ni_cnd.cn_flags = LOCKLEAF | SAVESTART;
381 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
382 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), pubflag);
383
384 if (!error && pubflag) {
385 if (nd.ni_vp->v_type == VDIR && nfs_pub.np_index != NULL) {
386 /*
387 * Setup call to lookup() to see if we can find
388 * the index file. Arguably, this doesn't belong
389 * in a kernel.. Ugh.
390 */
391 ind = nd;
392 VOP_UNLOCK(nd.ni_vp);
393 ind.ni_pathlen = strlen(nfs_pub.np_index);
394 ind.ni_cnd.cn_nameptr = ind.ni_cnd.cn_pnbuf =
395 nfs_pub.np_index;
396 ind.ni_startdir = nd.ni_vp;
397 VREF(ind.ni_startdir);
398 error = lookup(&ind);
399 if (!error) {
400 /*
401 * Found an index file. Get rid of
402 * the old references.
403 */
404 if (dirp)
405 vrele(dirp);
406 dirp = nd.ni_vp;
407 vrele(nd.ni_startdir);
408 ndp = &ind;
409 } else
410 error = 0;
411 }
412 /*
413 * If the public filehandle was used, check that this lookup
414 * didn't result in a filehandle outside the publicly exported
415 * filesystem.
416 */
417
418 if (!error && ndp->ni_vp->v_mount != nfs_pub.np_mount) {
419 vput(nd.ni_vp);
420 error = EPERM;
421 }
422 }
423
424 if (dirp) {
425 if (v3)
426 dirattr_ret = VOP_GETATTR(dirp, &dirattr, cred,
427 procp);
428 vrele(dirp);
429 }
430
431 if (error) {
432 nfsm_reply(NFSX_POSTOPATTR(v3));
433 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
434 return (0);
435 }
436
437 nqsrv_getl(ndp->ni_startdir, ND_READ);
438 vrele(ndp->ni_startdir);
439 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
440 vp = ndp->ni_vp;
441 bzero((caddr_t)fhp, sizeof(nfh));
442 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
443 error = VFS_VPTOFH(vp, &fhp->fh_fid);
444 if (!error)
445 error = VOP_GETATTR(vp, &va, cred, procp);
446 vput(vp);
447 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPORFATTR(v3) + NFSX_POSTOPATTR(v3));
448 if (error) {
449 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
450 return (0);
451 }
452 nfsm_srvfhtom(fhp, v3);
453 if (v3) {
454 nfsm_srvpostop_attr(0, &va);
455 nfsm_srvpostop_attr(dirattr_ret, &dirattr);
456 } else {
457 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
458 nfsm_srvfillattr(&va, fp);
459 }
460 nfsm_srvdone;
461 }
462
463 /*
464 * nfs readlink service
465 */
466 int
467 nfsrv_readlink(nfsd, slp, procp, mrq)
468 struct nfsrv_descript *nfsd;
469 struct nfssvc_sock *slp;
470 struct proc *procp;
471 struct mbuf **mrq;
472 {
473 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
474 struct mbuf *nam = nfsd->nd_nam;
475 caddr_t dpos = nfsd->nd_dpos;
476 struct ucred *cred = &nfsd->nd_cr;
477 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
478 register struct iovec *ivp = iv;
479 register struct mbuf *mp;
480 register u_int32_t *tl;
481 register int32_t t1;
482 caddr_t bpos;
483 int error = 0, rdonly, cache, i, tlen, len, getret;
484 int v3 = (nfsd->nd_flag & ND_NFSV3);
485 char *cp2;
486 struct mbuf *mb, *mb2, *mp2 = NULL, *mp3 = NULL, *mreq;
487 struct vnode *vp;
488 struct vattr attr;
489 nfsfh_t nfh;
490 fhandle_t *fhp;
491 struct uio io, *uiop = &io;
492 u_quad_t frev;
493
494 fhp = &nfh.fh_generic;
495 nfsm_srvmtofh(fhp);
496 len = 0;
497 i = 0;
498 while (len < NFS_MAXPATHLEN) {
499 MGET(mp, M_WAIT, MT_DATA);
500 MCLGET(mp, M_WAIT);
501 mp->m_len = NFSMSIZ(mp);
502 if (len == 0)
503 mp3 = mp2 = mp;
504 else {
505 mp2->m_next = mp;
506 mp2 = mp;
507 }
508 if ((len+mp->m_len) > NFS_MAXPATHLEN) {
509 mp->m_len = NFS_MAXPATHLEN-len;
510 len = NFS_MAXPATHLEN;
511 } else
512 len += mp->m_len;
513 ivp->iov_base = mtod(mp, caddr_t);
514 ivp->iov_len = mp->m_len;
515 i++;
516 ivp++;
517 }
518 uiop->uio_iov = iv;
519 uiop->uio_iovcnt = i;
520 uiop->uio_offset = 0;
521 uiop->uio_resid = len;
522 uiop->uio_rw = UIO_READ;
523 uiop->uio_segflg = UIO_SYSSPACE;
524 uiop->uio_procp = (struct proc *)0;
525 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
526 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
527 if (error) {
528 m_freem(mp3);
529 nfsm_reply(2 * NFSX_UNSIGNED);
530 nfsm_srvpostop_attr(1, (struct vattr *)0);
531 return (0);
532 }
533 if (vp->v_type != VLNK) {
534 if (v3)
535 error = EINVAL;
536 else
537 error = ENXIO;
538 goto out;
539 }
540 nqsrv_getl(vp, ND_READ);
541 error = VOP_READLINK(vp, uiop, cred);
542 out:
543 getret = VOP_GETATTR(vp, &attr, cred, procp);
544 vput(vp);
545 if (error)
546 m_freem(mp3);
547 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_UNSIGNED);
548 if (v3) {
549 nfsm_srvpostop_attr(getret, &attr);
550 if (error)
551 return (0);
552 }
553 if (uiop->uio_resid > 0) {
554 len -= uiop->uio_resid;
555 tlen = nfsm_rndup(len);
556 nfsm_adj(mp3, NFS_MAXPATHLEN-tlen, tlen-len);
557 }
558 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
559 *tl = txdr_unsigned(len);
560 mb->m_next = mp3;
561 nfsm_srvdone;
562 }
563
564 /*
565 * nfs read service
566 */
567 int
568 nfsrv_read(nfsd, slp, procp, mrq)
569 struct nfsrv_descript *nfsd;
570 struct nfssvc_sock *slp;
571 struct proc *procp;
572 struct mbuf **mrq;
573 {
574 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
575 struct mbuf *nam = nfsd->nd_nam;
576 caddr_t dpos = nfsd->nd_dpos;
577 struct ucred *cred = &nfsd->nd_cr;
578 register struct iovec *iv;
579 struct iovec *iv2;
580 register struct mbuf *m;
581 register struct nfs_fattr *fp;
582 register u_int32_t *tl;
583 register int32_t t1;
584 register int i;
585 caddr_t bpos;
586 int error = 0, rdonly, cache, cnt, len, left, siz, tlen, getret;
587 int v3 = (nfsd->nd_flag & ND_NFSV3), reqlen;
588 char *cp2;
589 struct mbuf *mb, *mb2, *mreq;
590 struct mbuf *m2;
591 struct vnode *vp;
592 nfsfh_t nfh;
593 fhandle_t *fhp;
594 struct uio io, *uiop = &io;
595 struct vattr va;
596 off_t off;
597 u_quad_t frev;
598
599 fhp = &nfh.fh_generic;
600 nfsm_srvmtofh(fhp);
601 if (v3) {
602 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
603 fxdr_hyper(tl, &off);
604 } else {
605 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
606 off = (off_t)fxdr_unsigned(u_int32_t, *tl);
607 }
608 nfsm_srvstrsiz(reqlen, NFS_SRVMAXDATA(nfsd));
609 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
610 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
611 if (error) {
612 nfsm_reply(2 * NFSX_UNSIGNED);
613 nfsm_srvpostop_attr(1, (struct vattr *)0);
614 return (0);
615 }
616 if (vp->v_type != VREG) {
617 if (v3)
618 error = EINVAL;
619 else
620 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
621 }
622 if (!error) {
623 nqsrv_getl(vp, ND_READ);
624 if ((error = nfsrv_access(vp, VREAD, cred, rdonly, procp, 1)) != 0)
625 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 1);
626 }
627 getret = VOP_GETATTR(vp, &va, cred, procp);
628 if (!error)
629 error = getret;
630 if (error) {
631 vput(vp);
632 nfsm_reply(NFSX_POSTOPATTR(v3));
633 nfsm_srvpostop_attr(getret, &va);
634 return (0);
635 }
636 if (off >= va.va_size)
637 cnt = 0;
638 else if ((off + reqlen) > va.va_size)
639 cnt = nfsm_rndup(va.va_size - off);
640 else
641 cnt = reqlen;
642 nfsm_reply(NFSX_POSTOPORFATTR(v3) + 3 * NFSX_UNSIGNED+nfsm_rndup(cnt));
643 if (v3) {
644 nfsm_build(tl, u_int32_t *, NFSX_V3FATTR + 4 * NFSX_UNSIGNED);
645 *tl++ = nfs_true;
646 fp = (struct nfs_fattr *)tl;
647 tl += (NFSX_V3FATTR / sizeof (u_int32_t));
648 } else {
649 nfsm_build(tl, u_int32_t *, NFSX_V2FATTR + NFSX_UNSIGNED);
650 fp = (struct nfs_fattr *)tl;
651 tl += (NFSX_V2FATTR / sizeof (u_int32_t));
652 }
653 len = left = cnt;
654 if (cnt > 0) {
655 /*
656 * Generate the mbuf list with the uio_iov ref. to it.
657 */
658 i = 0;
659 m = m2 = mb;
660 while (left > 0) {
661 siz = min(M_TRAILINGSPACE(m), left);
662 if (siz > 0) {
663 left -= siz;
664 i++;
665 }
666 if (left > 0) {
667 MGET(m, M_WAIT, MT_DATA);
668 MCLGET(m, M_WAIT);
669 m->m_len = 0;
670 m2->m_next = m;
671 m2 = m;
672 }
673 }
674 MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
675 M_TEMP, M_WAITOK);
676 uiop->uio_iov = iv2 = iv;
677 m = mb;
678 left = cnt;
679 i = 0;
680 while (left > 0) {
681 if (m == NULL)
682 panic("nfsrv_read iov");
683 siz = min(M_TRAILINGSPACE(m), left);
684 if (siz > 0) {
685 iv->iov_base = mtod(m, caddr_t) + m->m_len;
686 iv->iov_len = siz;
687 m->m_len += siz;
688 left -= siz;
689 iv++;
690 i++;
691 }
692 m = m->m_next;
693 }
694 uiop->uio_iovcnt = i;
695 uiop->uio_offset = off;
696 uiop->uio_resid = cnt;
697 uiop->uio_rw = UIO_READ;
698 uiop->uio_segflg = UIO_SYSSPACE;
699 error = VOP_READ(vp, uiop, IO_NODELOCKED, cred);
700 off = uiop->uio_offset;
701 FREE((caddr_t)iv2, M_TEMP);
702 if (error || (getret = VOP_GETATTR(vp, &va, cred, procp)) != 0){
703 if (!error)
704 error = getret;
705 m_freem(mreq);
706 vput(vp);
707 nfsm_reply(NFSX_POSTOPATTR(v3));
708 nfsm_srvpostop_attr(getret, &va);
709 return (0);
710 }
711 } else
712 uiop->uio_resid = 0;
713 vput(vp);
714 nfsm_srvfillattr(&va, fp);
715 len -= uiop->uio_resid;
716 tlen = nfsm_rndup(len);
717 if (cnt != tlen || tlen != len)
718 nfsm_adj(mb, cnt - tlen, tlen - len);
719 if (v3) {
720 *tl++ = txdr_unsigned(len);
721 if (len < reqlen)
722 *tl++ = nfs_true;
723 else
724 *tl++ = nfs_false;
725 }
726 *tl = txdr_unsigned(len);
727 nfsm_srvdone;
728 }
729
730 /*
731 * nfs write service
732 */
733 int
734 nfsrv_write(nfsd, slp, procp, mrq)
735 struct nfsrv_descript *nfsd;
736 struct nfssvc_sock *slp;
737 struct proc *procp;
738 struct mbuf **mrq;
739 {
740 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
741 struct mbuf *nam = nfsd->nd_nam;
742 caddr_t dpos = nfsd->nd_dpos;
743 struct ucred *cred = &nfsd->nd_cr;
744 register struct iovec *ivp;
745 register int i, cnt;
746 register struct mbuf *mp;
747 register struct nfs_fattr *fp;
748 struct iovec *iv;
749 struct vattr va, forat;
750 register u_int32_t *tl;
751 register int32_t t1;
752 caddr_t bpos;
753 int error = 0, rdonly, cache, len, forat_ret = 1;
754 int ioflags, aftat_ret = 1, retlen, zeroing, adjust;
755 int stable = NFSV3WRITE_FILESYNC;
756 int v3 = (nfsd->nd_flag & ND_NFSV3);
757 char *cp2;
758 struct mbuf *mb, *mb2, *mreq;
759 struct vnode *vp;
760 nfsfh_t nfh;
761 fhandle_t *fhp;
762 struct uio io, *uiop = &io;
763 off_t off;
764 u_quad_t frev;
765
766 if (mrep == NULL) {
767 *mrq = NULL;
768 return (0);
769 }
770 fhp = &nfh.fh_generic;
771 nfsm_srvmtofh(fhp);
772 if (v3) {
773 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
774 fxdr_hyper(tl, &off);
775 tl += 3;
776 stable = fxdr_unsigned(int, *tl++);
777 } else {
778 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
779 off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
780 tl += 2;
781 }
782 retlen = len = fxdr_unsigned(int32_t, *tl);
783 cnt = i = 0;
784
785 /*
786 * For NFS Version 2, it is not obvious what a write of zero length
787 * should do, but I might as well be consistent with Version 3,
788 * which is to return ok so long as there are no permission problems.
789 */
790 if (len > 0) {
791 zeroing = 1;
792 mp = mrep;
793 while (mp) {
794 if (mp == md) {
795 zeroing = 0;
796 adjust = dpos - mtod(mp, caddr_t);
797 mp->m_len -= adjust;
798 if (mp->m_len > 0 && adjust > 0)
799 NFSMADV(mp, adjust);
800 }
801 if (zeroing)
802 mp->m_len = 0;
803 else if (mp->m_len > 0) {
804 i += mp->m_len;
805 if (i > len) {
806 mp->m_len -= (i - len);
807 zeroing = 1;
808 }
809 if (mp->m_len > 0)
810 cnt++;
811 }
812 mp = mp->m_next;
813 }
814 }
815 if (len > NFS_MAXDATA || len < 0 || i < len) {
816 error = EIO;
817 nfsm_reply(2 * NFSX_UNSIGNED);
818 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
819 return (0);
820 }
821 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
822 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
823 if (error) {
824 nfsm_reply(2 * NFSX_UNSIGNED);
825 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
826 return (0);
827 }
828 if (v3)
829 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
830 if (vp->v_type != VREG) {
831 if (v3)
832 error = EINVAL;
833 else
834 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
835 }
836 if (!error) {
837 nqsrv_getl(vp, ND_WRITE);
838 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
839 }
840 if (error) {
841 vput(vp);
842 nfsm_reply(NFSX_WCCDATA(v3));
843 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
844 return (0);
845 }
846
847 if (len > 0) {
848 MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
849 M_WAITOK);
850 uiop->uio_iov = iv = ivp;
851 uiop->uio_iovcnt = cnt;
852 mp = mrep;
853 while (mp) {
854 if (mp->m_len > 0) {
855 ivp->iov_base = mtod(mp, caddr_t);
856 ivp->iov_len = mp->m_len;
857 ivp++;
858 }
859 mp = mp->m_next;
860 }
861
862 /*
863 * XXX
864 * The IO_METASYNC flag indicates that all metadata (and not just
865 * enough to ensure data integrity) mus be written to stable storage
866 * synchronously.
867 * (IO_METASYNC is not yet implemented in 4.4BSD-Lite.)
868 */
869 if (stable == NFSV3WRITE_UNSTABLE)
870 ioflags = IO_NODELOCKED;
871 else if (stable == NFSV3WRITE_DATASYNC)
872 ioflags = (IO_SYNC | IO_NODELOCKED);
873 else
874 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
875 uiop->uio_resid = len;
876 uiop->uio_rw = UIO_WRITE;
877 uiop->uio_segflg = UIO_SYSSPACE;
878 uiop->uio_procp = (struct proc *)0;
879 uiop->uio_offset = off;
880 error = VOP_WRITE(vp, uiop, ioflags, cred);
881 nfsstats.srvvop_writes++;
882 FREE((caddr_t)iv, M_TEMP);
883 }
884 aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
885 vput(vp);
886 if (!error)
887 error = aftat_ret;
888 nfsm_reply(NFSX_PREOPATTR(v3) + NFSX_POSTOPORFATTR(v3) +
889 2 * NFSX_UNSIGNED + NFSX_WRITEVERF(v3));
890 if (v3) {
891 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
892 if (error)
893 return (0);
894 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
895 *tl++ = txdr_unsigned(retlen);
896 if (stable == NFSV3WRITE_UNSTABLE)
897 *tl++ = txdr_unsigned(stable);
898 else
899 *tl++ = txdr_unsigned(NFSV3WRITE_FILESYNC);
900 /*
901 * Actually, there is no need to txdr these fields,
902 * but it may make the values more human readable,
903 * for debugging purposes.
904 */
905 *tl++ = txdr_unsigned(boottime.tv_sec);
906 *tl = txdr_unsigned(boottime.tv_usec);
907 } else {
908 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
909 nfsm_srvfillattr(&va, fp);
910 }
911 nfsm_srvdone;
912 }
913
914 /*
915 * NFS write service with write gathering support. Called when
916 * nfsrvw_procrastinate > 0.
917 * See: Chet Juszczak, "Improving the Write Performance of an NFS Server",
918 * in Proc. of the Winter 1994 Usenix Conference, pg. 247-259, San Franscisco,
919 * Jan. 1994.
920 */
921 int
922 nfsrv_writegather(ndp, slp, procp, mrq)
923 struct nfsrv_descript **ndp;
924 struct nfssvc_sock *slp;
925 struct proc *procp;
926 struct mbuf **mrq;
927 {
928 register struct iovec *ivp;
929 register struct mbuf *mp;
930 register struct nfsrv_descript *wp, *nfsd, *owp, *swp;
931 register struct nfs_fattr *fp;
932 register int i = 0;
933 struct iovec *iov;
934 struct nfsrvw_delayhash *wpp;
935 struct ucred *cred;
936 struct vattr va, forat;
937 register u_int32_t *tl;
938 register int32_t t1;
939 caddr_t bpos, dpos;
940 int error = 0, rdonly, cache, len = 0, forat_ret = 1;
941 int ioflags, aftat_ret = 1, s, adjust, v3, zeroing;
942 char *cp2;
943 struct mbuf *mb, *mb2, *mreq, *mrep, *md;
944 struct vnode *vp;
945 struct uio io, *uiop = &io;
946 u_quad_t frev, cur_usec;
947
948 *mrq = NULL;
949 if (*ndp) {
950 nfsd = *ndp;
951 *ndp = NULL;
952 mrep = nfsd->nd_mrep;
953 md = nfsd->nd_md;
954 dpos = nfsd->nd_dpos;
955 cred = &nfsd->nd_cr;
956 v3 = (nfsd->nd_flag & ND_NFSV3);
957 LIST_INIT(&nfsd->nd_coalesce);
958 nfsd->nd_mreq = NULL;
959 nfsd->nd_stable = NFSV3WRITE_FILESYNC;
960 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
961 nfsd->nd_time = cur_usec + nfsrvw_procrastinate;
962
963 /*
964 * Now, get the write header..
965 */
966 nfsm_srvmtofh(&nfsd->nd_fh);
967 if (v3) {
968 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
969 fxdr_hyper(tl, &nfsd->nd_off);
970 tl += 3;
971 nfsd->nd_stable = fxdr_unsigned(int, *tl++);
972 } else {
973 nfsm_dissect(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
974 nfsd->nd_off = (off_t)fxdr_unsigned(u_int32_t, *++tl);
975 tl += 2;
976 }
977 len = fxdr_unsigned(int32_t, *tl);
978 nfsd->nd_len = len;
979 nfsd->nd_eoff = nfsd->nd_off + len;
980
981 /*
982 * Trim the header out of the mbuf list and trim off any trailing
983 * junk so that the mbuf list has only the write data.
984 */
985 zeroing = 1;
986 i = 0;
987 mp = mrep;
988 while (mp) {
989 if (mp == md) {
990 zeroing = 0;
991 adjust = dpos - mtod(mp, caddr_t);
992 mp->m_len -= adjust;
993 if (mp->m_len > 0 && adjust > 0)
994 NFSMADV(mp, adjust);
995 }
996 if (zeroing)
997 mp->m_len = 0;
998 else {
999 i += mp->m_len;
1000 if (i > len) {
1001 mp->m_len -= (i - len);
1002 zeroing = 1;
1003 }
1004 }
1005 mp = mp->m_next;
1006 }
1007 if (len > NFS_MAXDATA || len < 0 || i < len) {
1008 nfsmout:
1009 m_freem(mrep);
1010 error = EIO;
1011 nfsm_writereply(2 * NFSX_UNSIGNED, v3);
1012 if (v3)
1013 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1014 nfsd->nd_mreq = mreq;
1015 nfsd->nd_mrep = NULL;
1016 nfsd->nd_time = 0;
1017 }
1018
1019 /*
1020 * Add this entry to the hash and time queues.
1021 */
1022 s = splsoftclock();
1023 owp = NULL;
1024 wp = slp->ns_tq.lh_first;
1025 while (wp && wp->nd_time < nfsd->nd_time) {
1026 owp = wp;
1027 wp = wp->nd_tq.le_next;
1028 }
1029 if (owp) {
1030 LIST_INSERT_AFTER(owp, nfsd, nd_tq);
1031 } else {
1032 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1033 }
1034 if (nfsd->nd_mrep) {
1035 wpp = NWDELAYHASH(slp, nfsd->nd_fh.fh_fid.fid_data);
1036 owp = NULL;
1037 wp = wpp->lh_first;
1038 while (wp &&
1039 bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1040 owp = wp;
1041 wp = wp->nd_hash.le_next;
1042 }
1043 while (wp && wp->nd_off < nfsd->nd_off &&
1044 !bcmp((caddr_t)&nfsd->nd_fh,(caddr_t)&wp->nd_fh,NFSX_V3FH)) {
1045 owp = wp;
1046 wp = wp->nd_hash.le_next;
1047 }
1048 if (owp) {
1049 LIST_INSERT_AFTER(owp, nfsd, nd_hash);
1050
1051 /*
1052 * Search the hash list for overlapping entries and
1053 * coalesce.
1054 */
1055 for(; nfsd && NFSW_CONTIG(owp, nfsd); nfsd = wp) {
1056 wp = nfsd->nd_hash.le_next;
1057 if (NFSW_SAMECRED(owp, nfsd))
1058 nfsrvw_coalesce(owp, nfsd);
1059 }
1060 } else {
1061 LIST_INSERT_HEAD(wpp, nfsd, nd_hash);
1062 }
1063 }
1064 splx(s);
1065 }
1066
1067 /*
1068 * Now, do VOP_WRITE()s for any one(s) that need to be done now
1069 * and generate the associated reply mbuf list(s).
1070 */
1071 loop1:
1072 cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
1073 s = splsoftclock();
1074 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = owp) {
1075 owp = nfsd->nd_tq.le_next;
1076 if (nfsd->nd_time > cur_usec)
1077 break;
1078 if (nfsd->nd_mreq)
1079 continue;
1080 LIST_REMOVE(nfsd, nd_tq);
1081 LIST_REMOVE(nfsd, nd_hash);
1082 splx(s);
1083 mrep = nfsd->nd_mrep;
1084 nfsd->nd_mrep = NULL;
1085 cred = &nfsd->nd_cr;
1086 v3 = (nfsd->nd_flag & ND_NFSV3);
1087 forat_ret = aftat_ret = 1;
1088 error = nfsrv_fhtovp(&nfsd->nd_fh, 1, &vp, cred, slp,
1089 nfsd->nd_nam, &rdonly, (nfsd->nd_flag & ND_KERBAUTH),
1090 FALSE);
1091 if (!error) {
1092 if (v3)
1093 forat_ret = VOP_GETATTR(vp, &forat, cred, procp);
1094 if (vp->v_type != VREG) {
1095 if (v3)
1096 error = EINVAL;
1097 else
1098 error = (vp->v_type == VDIR) ? EISDIR : EACCES;
1099 }
1100 } else
1101 vp = NULL;
1102 if (!error) {
1103 nqsrv_getl(vp, ND_WRITE);
1104 error = nfsrv_access(vp, VWRITE, cred, rdonly, procp, 1);
1105 }
1106
1107 if (nfsd->nd_stable == NFSV3WRITE_UNSTABLE)
1108 ioflags = IO_NODELOCKED;
1109 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC)
1110 ioflags = (IO_SYNC | IO_NODELOCKED);
1111 else
1112 ioflags = (IO_METASYNC | IO_SYNC | IO_NODELOCKED);
1113 uiop->uio_rw = UIO_WRITE;
1114 uiop->uio_segflg = UIO_SYSSPACE;
1115 uiop->uio_procp = (struct proc *)0;
1116 uiop->uio_offset = nfsd->nd_off;
1117 uiop->uio_resid = nfsd->nd_eoff - nfsd->nd_off;
1118 if (uiop->uio_resid > 0) {
1119 mp = mrep;
1120 i = 0;
1121 while (mp) {
1122 if (mp->m_len > 0)
1123 i++;
1124 mp = mp->m_next;
1125 }
1126 uiop->uio_iovcnt = i;
1127 MALLOC(iov, struct iovec *, i * sizeof (struct iovec),
1128 M_TEMP, M_WAITOK);
1129 uiop->uio_iov = ivp = iov;
1130 mp = mrep;
1131 while (mp) {
1132 if (mp->m_len > 0) {
1133 ivp->iov_base = mtod(mp, caddr_t);
1134 ivp->iov_len = mp->m_len;
1135 ivp++;
1136 }
1137 mp = mp->m_next;
1138 }
1139 if (!error) {
1140 error = VOP_WRITE(vp, uiop, ioflags, cred);
1141 nfsstats.srvvop_writes++;
1142 }
1143 FREE((caddr_t)iov, M_TEMP);
1144 }
1145 m_freem(mrep);
1146 if (vp) {
1147 aftat_ret = VOP_GETATTR(vp, &va, cred, procp);
1148 vput(vp);
1149 }
1150
1151 /*
1152 * Loop around generating replies for all write rpcs that have
1153 * now been completed.
1154 */
1155 swp = nfsd;
1156 do {
1157 if (error) {
1158 nfsm_writereply(NFSX_WCCDATA(v3), v3);
1159 if (v3) {
1160 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1161 }
1162 } else {
1163 nfsm_writereply(NFSX_PREOPATTR(v3) +
1164 NFSX_POSTOPORFATTR(v3) + 2 * NFSX_UNSIGNED +
1165 NFSX_WRITEVERF(v3), v3);
1166 if (v3) {
1167 nfsm_srvwcc_data(forat_ret, &forat, aftat_ret, &va);
1168 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1169 *tl++ = txdr_unsigned(nfsd->nd_len);
1170 *tl++ = txdr_unsigned(swp->nd_stable);
1171 /*
1172 * Actually, there is no need to txdr these fields,
1173 * but it may make the values more human readable,
1174 * for debugging purposes.
1175 */
1176 *tl++ = txdr_unsigned(boottime.tv_sec);
1177 *tl = txdr_unsigned(boottime.tv_usec);
1178 } else {
1179 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1180 nfsm_srvfillattr(&va, fp);
1181 }
1182 }
1183 nfsd->nd_mreq = mreq;
1184 if (nfsd->nd_mrep)
1185 panic("nfsrv_write: nd_mrep not free");
1186
1187 /*
1188 * Done. Put it at the head of the timer queue so that
1189 * the final phase can return the reply.
1190 */
1191 s = splsoftclock();
1192 if (nfsd != swp) {
1193 nfsd->nd_time = 0;
1194 LIST_INSERT_HEAD(&slp->ns_tq, nfsd, nd_tq);
1195 }
1196 nfsd = swp->nd_coalesce.lh_first;
1197 if (nfsd) {
1198 LIST_REMOVE(nfsd, nd_tq);
1199 }
1200 splx(s);
1201 } while (nfsd);
1202 s = splsoftclock();
1203 swp->nd_time = 0;
1204 LIST_INSERT_HEAD(&slp->ns_tq, swp, nd_tq);
1205 splx(s);
1206 goto loop1;
1207 }
1208 splx(s);
1209
1210 /*
1211 * Search for a reply to return.
1212 */
1213 s = splsoftclock();
1214 for (nfsd = slp->ns_tq.lh_first; nfsd; nfsd = nfsd->nd_tq.le_next)
1215 if (nfsd->nd_mreq) {
1216 LIST_REMOVE(nfsd, nd_tq);
1217 *mrq = nfsd->nd_mreq;
1218 *ndp = nfsd;
1219 break;
1220 }
1221 splx(s);
1222 return (0);
1223 }
1224
1225 /*
1226 * Coalesce the write request nfsd into owp. To do this we must:
1227 * - remove nfsd from the queues
1228 * - merge nfsd->nd_mrep into owp->nd_mrep
1229 * - update the nd_eoff and nd_stable for owp
1230 * - put nfsd on owp's nd_coalesce list
1231 * NB: Must be called at splsoftclock().
1232 */
1233 void
1234 nfsrvw_coalesce(owp, nfsd)
1235 register struct nfsrv_descript *owp;
1236 register struct nfsrv_descript *nfsd;
1237 {
1238 register int overlap;
1239 register struct mbuf *mp;
1240
1241 LIST_REMOVE(nfsd, nd_hash);
1242 LIST_REMOVE(nfsd, nd_tq);
1243 if (owp->nd_eoff < nfsd->nd_eoff) {
1244 overlap = owp->nd_eoff - nfsd->nd_off;
1245 if (overlap < 0)
1246 panic("nfsrv_coalesce: bad off");
1247 if (overlap > 0)
1248 m_adj(nfsd->nd_mrep, overlap);
1249 mp = owp->nd_mrep;
1250 while (mp->m_next)
1251 mp = mp->m_next;
1252 mp->m_next = nfsd->nd_mrep;
1253 owp->nd_eoff = nfsd->nd_eoff;
1254 } else
1255 m_freem(nfsd->nd_mrep);
1256 nfsd->nd_mrep = NULL;
1257 if (nfsd->nd_stable == NFSV3WRITE_FILESYNC)
1258 owp->nd_stable = NFSV3WRITE_FILESYNC;
1259 else if (nfsd->nd_stable == NFSV3WRITE_DATASYNC &&
1260 owp->nd_stable == NFSV3WRITE_UNSTABLE)
1261 owp->nd_stable = NFSV3WRITE_DATASYNC;
1262 LIST_INSERT_HEAD(&owp->nd_coalesce, nfsd, nd_tq);
1263 /*
1264 * nfsd might hold coalesce elements! Move them to owp.
1265 * Otherwise, requests may be lost and clients will be stuck.
1266 */
1267 if (nfsd->nd_coalesce.lh_first)
1268 {
1269 register struct nfsrv_descript *m;
1270
1271 while ((m = nfsd->nd_coalesce.lh_first))
1272 {
1273 LIST_REMOVE(m, nd_tq);
1274 LIST_INSERT_HEAD(&owp->nd_coalesce, m, nd_tq);
1275 }
1276 }
1277 }
1278
1279 /*
1280 * nfs create service
1281 * now does a truncate to 0 length via. setattr if it already exists
1282 */
1283 int
1284 nfsrv_create(nfsd, slp, procp, mrq)
1285 struct nfsrv_descript *nfsd;
1286 struct nfssvc_sock *slp;
1287 struct proc *procp;
1288 struct mbuf **mrq;
1289 {
1290 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1291 struct mbuf *nam = nfsd->nd_nam;
1292 caddr_t dpos = nfsd->nd_dpos;
1293 struct ucred *cred = &nfsd->nd_cr;
1294 register struct nfs_fattr *fp;
1295 struct vattr va, dirfor, diraft;
1296 register struct nfsv2_sattr *sp;
1297 register u_int32_t *tl;
1298 struct nameidata nd;
1299 register caddr_t cp;
1300 register int32_t t1;
1301 caddr_t bpos;
1302 int error = 0, cache, len, tsize, dirfor_ret = 1, diraft_ret = 1;
1303 int rdev = 0;
1304 int v3 = (nfsd->nd_flag & ND_NFSV3), how, exclusive_flag = 0;
1305 char *cp2;
1306 struct mbuf *mb, *mb2, *mreq;
1307 struct vnode *vp = NULL, *dirp = NULL;
1308 nfsfh_t nfh;
1309 fhandle_t *fhp;
1310 u_quad_t frev, tempsize;
1311 u_char cverf[NFSX_V3CREATEVERF];
1312
1313 nd.ni_cnd.cn_nameiop = 0;
1314 fhp = &nfh.fh_generic;
1315 nfsm_srvmtofh(fhp);
1316 nfsm_srvnamesiz(len);
1317 nd.ni_cnd.cn_cred = cred;
1318 nd.ni_cnd.cn_nameiop = CREATE;
1319 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1320 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1321 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1322 if (dirp) {
1323 if (v3)
1324 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1325 procp);
1326 else {
1327 vrele(dirp);
1328 dirp = (struct vnode *)0;
1329 }
1330 }
1331 if (error) {
1332 nfsm_reply(NFSX_WCCDATA(v3));
1333 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1334 if (dirp)
1335 vrele(dirp);
1336 return (0);
1337 }
1338 VATTR_NULL(&va);
1339 if (v3) {
1340 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1341 how = fxdr_unsigned(int, *tl);
1342 switch (how) {
1343 case NFSV3CREATE_GUARDED:
1344 if (nd.ni_vp) {
1345 error = EEXIST;
1346 break;
1347 }
1348 case NFSV3CREATE_UNCHECKED:
1349 nfsm_srvsattr(&va);
1350 break;
1351 case NFSV3CREATE_EXCLUSIVE:
1352 nfsm_dissect(cp, caddr_t, NFSX_V3CREATEVERF);
1353 bcopy(cp, cverf, NFSX_V3CREATEVERF);
1354 exclusive_flag = 1;
1355 if (nd.ni_vp == NULL)
1356 va.va_mode = 0;
1357 break;
1358 };
1359 va.va_type = VREG;
1360 } else {
1361 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1362 va.va_type = IFTOVT(fxdr_unsigned(u_int32_t, sp->sa_mode));
1363 if (va.va_type == VNON)
1364 va.va_type = VREG;
1365 va.va_mode = nfstov_mode(sp->sa_mode);
1366 switch (va.va_type) {
1367 case VREG:
1368 tsize = fxdr_unsigned(int32_t, sp->sa_size);
1369 if (tsize != -1)
1370 va.va_size = (u_quad_t)tsize;
1371 break;
1372 case VCHR:
1373 case VBLK:
1374 case VFIFO:
1375 rdev = fxdr_unsigned(int32_t, sp->sa_size);
1376 break;
1377 default:
1378 break;
1379 };
1380 }
1381
1382 /*
1383 * Iff doesn't exist, create it
1384 * otherwise just truncate to 0 length
1385 * should I set the mode too ??
1386 */
1387 if (nd.ni_vp == NULL) {
1388 if (va.va_type == VREG || va.va_type == VSOCK) {
1389 vrele(nd.ni_startdir);
1390 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1391 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1392 if (!error) {
1393 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1394 if (exclusive_flag) {
1395 exclusive_flag = 0;
1396 VATTR_NULL(&va);
1397 bcopy(cverf, (caddr_t)&va.va_atime,
1398 NFSX_V3CREATEVERF);
1399 error = VOP_SETATTR(nd.ni_vp, &va, cred,
1400 procp);
1401 }
1402 }
1403 } else if (va.va_type == VCHR || va.va_type == VBLK ||
1404 va.va_type == VFIFO) {
1405 if (va.va_type == VCHR && rdev == 0xffffffff)
1406 va.va_type = VFIFO;
1407 error = suser(cred, (u_short *)0);
1408 if (error) {
1409 vrele(nd.ni_startdir);
1410 free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1411 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1412 vput(nd.ni_dvp);
1413 nfsm_reply(0);
1414 return (error);
1415 } else
1416 va.va_rdev = (dev_t)rdev;
1417 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1418 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd,
1419 &va);
1420 if (error) {
1421 vrele(nd.ni_startdir);
1422 nfsm_reply(0);
1423 }
1424 nd.ni_cnd.cn_nameiop = LOOKUP;
1425 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
1426 nd.ni_cnd.cn_proc = procp;
1427 nd.ni_cnd.cn_cred = cred;
1428 if ((error = lookup(&nd)) != 0) {
1429 free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1430 nfsm_reply(0);
1431 }
1432 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1433 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1434 vrele(nd.ni_dvp);
1435 vput(nd.ni_vp);
1436 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1437 error = EINVAL;
1438 nfsm_reply(0);
1439 }
1440 } else {
1441 vrele(nd.ni_startdir);
1442 free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1443 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1444 vput(nd.ni_dvp);
1445 error = ENXIO;
1446 }
1447 vp = nd.ni_vp;
1448 } else {
1449 vrele(nd.ni_startdir);
1450 free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1451 vp = nd.ni_vp;
1452 if (nd.ni_dvp == vp)
1453 vrele(nd.ni_dvp);
1454 else
1455 vput(nd.ni_dvp);
1456 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1457 if (va.va_size != -1) {
1458 error = nfsrv_access(vp, VWRITE, cred,
1459 (nd.ni_cnd.cn_flags & RDONLY), procp, 0);
1460 if (!error) {
1461 nqsrv_getl(vp, ND_WRITE);
1462 tempsize = va.va_size;
1463 VATTR_NULL(&va);
1464 va.va_size = tempsize;
1465 error = VOP_SETATTR(vp, &va, cred,
1466 procp);
1467 }
1468 if (error)
1469 vput(vp);
1470 }
1471 }
1472 if (!error) {
1473 bzero((caddr_t)fhp, sizeof(nfh));
1474 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1475 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1476 if (!error)
1477 error = VOP_GETATTR(vp, &va, cred, procp);
1478 vput(vp);
1479 }
1480 if (v3) {
1481 if (exclusive_flag && !error &&
1482 bcmp(cverf, (caddr_t)&va.va_atime, NFSX_V3CREATEVERF))
1483 error = EEXIST;
1484 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
1485 vrele(dirp);
1486 }
1487 nfsm_reply(NFSX_SRVFH(v3) + NFSX_FATTR(v3) + NFSX_WCCDATA(v3));
1488 if (v3) {
1489 if (!error) {
1490 nfsm_srvpostop_fh(fhp);
1491 nfsm_srvpostop_attr(0, &va);
1492 }
1493 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1494 } else {
1495 nfsm_srvfhtom(fhp, v3);
1496 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
1497 nfsm_srvfillattr(&va, fp);
1498 }
1499 return (0);
1500 nfsmout:
1501 if (dirp)
1502 vrele(dirp);
1503 if (nd.ni_cnd.cn_nameiop) {
1504 vrele(nd.ni_startdir);
1505 free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
1506 }
1507 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1508 if (nd.ni_dvp == nd.ni_vp)
1509 vrele(nd.ni_dvp);
1510 else
1511 vput(nd.ni_dvp);
1512 if (nd.ni_vp)
1513 vput(nd.ni_vp);
1514 return (error);
1515 }
1516
1517 /*
1518 * nfs v3 mknod service
1519 */
1520 int
1521 nfsrv_mknod(nfsd, slp, procp, mrq)
1522 struct nfsrv_descript *nfsd;
1523 struct nfssvc_sock *slp;
1524 struct proc *procp;
1525 struct mbuf **mrq;
1526 {
1527 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1528 struct mbuf *nam = nfsd->nd_nam;
1529 caddr_t dpos = nfsd->nd_dpos;
1530 struct ucred *cred = &nfsd->nd_cr;
1531 struct vattr va, dirfor, diraft;
1532 register u_int32_t *tl;
1533 struct nameidata nd;
1534 register int32_t t1;
1535 caddr_t bpos;
1536 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1537 u_int32_t major, minor;
1538 enum vtype vtyp;
1539 char *cp2;
1540 struct mbuf *mb, *mb2, *mreq;
1541 struct vnode *vp, *dirp = (struct vnode *)0;
1542 nfsfh_t nfh;
1543 fhandle_t *fhp;
1544 u_quad_t frev;
1545
1546 nd.ni_cnd.cn_nameiop = 0;
1547 fhp = &nfh.fh_generic;
1548 nfsm_srvmtofh(fhp);
1549 nfsm_srvnamesiz(len);
1550 nd.ni_cnd.cn_cred = cred;
1551 nd.ni_cnd.cn_nameiop = CREATE;
1552 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | SAVESTART;
1553 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1554 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1555 if (dirp)
1556 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred, procp);
1557 if (error) {
1558 nfsm_reply(NFSX_WCCDATA(1));
1559 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1560 if (dirp)
1561 vrele(dirp);
1562 return (0);
1563 }
1564 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1565 vtyp = nfsv3tov_type(*tl);
1566 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1567 vrele(nd.ni_startdir);
1568 free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
1569 error = NFSERR_BADTYPE;
1570 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1571 vput(nd.ni_dvp);
1572 goto out;
1573 }
1574 VATTR_NULL(&va);
1575 nfsm_srvsattr(&va);
1576 if (vtyp == VCHR || vtyp == VBLK) {
1577 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1578 major = fxdr_unsigned(u_int32_t, *tl++);
1579 minor = fxdr_unsigned(u_int32_t, *tl);
1580 va.va_rdev = makedev(major, minor);
1581 }
1582
1583 /*
1584 * Iff doesn't exist, create it.
1585 */
1586 if (nd.ni_vp) {
1587 vrele(nd.ni_startdir);
1588 free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
1589 error = EEXIST;
1590 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1591 vput(nd.ni_dvp);
1592 goto out;
1593 }
1594 va.va_type = vtyp;
1595 if (vtyp == VSOCK) {
1596 vrele(nd.ni_startdir);
1597 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1598 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1599 if (!error)
1600 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1601 } else {
1602 error = suser(cred, (u_short *)0);
1603 if (error) {
1604 vrele(nd.ni_startdir);
1605 free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
1606 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1607 vput(nd.ni_dvp);
1608 goto out;
1609 }
1610 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1611 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
1612 if (error) {
1613 vrele(nd.ni_startdir);
1614 goto out;
1615 }
1616 nd.ni_cnd.cn_nameiop = LOOKUP;
1617 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART);
1618 nd.ni_cnd.cn_proc = procp;
1619 nd.ni_cnd.cn_cred = procp->p_ucred;
1620 error = lookup(&nd);
1621 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
1622 if (error)
1623 goto out;
1624 if (nd.ni_cnd.cn_flags & ISSYMLINK) {
1625 vrele(nd.ni_dvp);
1626 vput(nd.ni_vp);
1627 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1628 error = EINVAL;
1629 }
1630 }
1631 out:
1632 vp = nd.ni_vp;
1633 if (!error) {
1634 bzero((caddr_t)fhp, sizeof(nfh));
1635 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1636 error = VFS_VPTOFH(vp, &fhp->fh_fid);
1637 if (!error)
1638 error = VOP_GETATTR(vp, &va, cred, procp);
1639 vput(vp);
1640 }
1641 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
1642 vrele(dirp);
1643 nfsm_reply(NFSX_SRVFH(1) + NFSX_POSTOPATTR(1) + NFSX_WCCDATA(1));
1644 if (!error) {
1645 nfsm_srvpostop_fh(fhp);
1646 nfsm_srvpostop_attr(0, &va);
1647 }
1648 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1649 return (0);
1650 nfsmout:
1651 if (dirp)
1652 vrele(dirp);
1653 if (nd.ni_cnd.cn_nameiop) {
1654 vrele(nd.ni_startdir);
1655 free((caddr_t)nd.ni_cnd.cn_pnbuf, M_NAMEI);
1656 }
1657 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1658 if (nd.ni_dvp == nd.ni_vp)
1659 vrele(nd.ni_dvp);
1660 else
1661 vput(nd.ni_dvp);
1662 if (nd.ni_vp)
1663 vput(nd.ni_vp);
1664 return (error);
1665 }
1666
1667 /*
1668 * nfs remove service
1669 */
1670 int
1671 nfsrv_remove(nfsd, slp, procp, mrq)
1672 struct nfsrv_descript *nfsd;
1673 struct nfssvc_sock *slp;
1674 struct proc *procp;
1675 struct mbuf **mrq;
1676 {
1677 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1678 struct mbuf *nam = nfsd->nd_nam;
1679 caddr_t dpos = nfsd->nd_dpos;
1680 struct ucred *cred = &nfsd->nd_cr;
1681 struct nameidata nd;
1682 register u_int32_t *tl;
1683 register int32_t t1;
1684 caddr_t bpos;
1685 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
1686 int v3 = (nfsd->nd_flag & ND_NFSV3);
1687 char *cp2;
1688 struct mbuf *mb, *mreq;
1689 struct vnode *vp, *dirp;
1690 struct vattr dirfor, diraft;
1691 nfsfh_t nfh;
1692 fhandle_t *fhp;
1693 u_quad_t frev;
1694
1695 #ifndef nolint
1696 vp = (struct vnode *)0;
1697 #endif
1698 fhp = &nfh.fh_generic;
1699 nfsm_srvmtofh(fhp);
1700 nfsm_srvnamesiz(len);
1701 nd.ni_cnd.cn_cred = cred;
1702 nd.ni_cnd.cn_nameiop = DELETE;
1703 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
1704 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
1705 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1706 if (dirp) {
1707 if (v3)
1708 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
1709 procp);
1710 else
1711 vrele(dirp);
1712 }
1713 if (!error) {
1714 vp = nd.ni_vp;
1715 if (vp->v_type == VDIR &&
1716 (error = suser(cred, (u_short *)0)) != 0)
1717 goto out;
1718 /*
1719 * The root of a mounted filesystem cannot be deleted.
1720 */
1721 if (vp->v_flag & VROOT) {
1722 error = EBUSY;
1723 goto out;
1724 }
1725 out:
1726 if (!error) {
1727 (void)vnode_pager_uncache(vp);
1728 nqsrv_getl(nd.ni_dvp, ND_WRITE);
1729 nqsrv_getl(vp, ND_WRITE);
1730 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1731 } else {
1732 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1733 if (nd.ni_dvp == vp)
1734 vrele(nd.ni_dvp);
1735 else
1736 vput(nd.ni_dvp);
1737 vput(vp);
1738 }
1739 }
1740 if (dirp && v3) {
1741 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
1742 vrele(dirp);
1743 }
1744 nfsm_reply(NFSX_WCCDATA(v3));
1745 if (v3) {
1746 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1747 return (0);
1748 }
1749 nfsm_srvdone;
1750 }
1751
1752 /*
1753 * nfs rename service
1754 */
1755 int
1756 nfsrv_rename(nfsd, slp, procp, mrq)
1757 struct nfsrv_descript *nfsd;
1758 struct nfssvc_sock *slp;
1759 struct proc *procp;
1760 struct mbuf **mrq;
1761 {
1762 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1763 struct mbuf *nam = nfsd->nd_nam;
1764 caddr_t dpos = nfsd->nd_dpos;
1765 struct ucred *cred = &nfsd->nd_cr;
1766 register u_int32_t *tl;
1767 register int32_t t1;
1768 caddr_t bpos;
1769 int error = 0, cache, len, len2, fdirfor_ret = 1, fdiraft_ret = 1;
1770 int tdirfor_ret = 1, tdiraft_ret = 1;
1771 int v3 = (nfsd->nd_flag & ND_NFSV3);
1772 char *cp2;
1773 struct mbuf *mb, *mreq;
1774 struct nameidata fromnd, tond;
1775 struct vnode *fvp, *tvp, *tdvp, *fdirp = (struct vnode *)0;
1776 struct vnode *tdirp = (struct vnode *)0;
1777 struct vattr fdirfor, fdiraft, tdirfor, tdiraft;
1778 nfsfh_t fnfh, tnfh;
1779 fhandle_t *ffhp, *tfhp;
1780 u_quad_t frev;
1781 uid_t saved_uid;
1782
1783 #ifndef nolint
1784 fvp = (struct vnode *)0;
1785 #endif
1786 ffhp = &fnfh.fh_generic;
1787 tfhp = &tnfh.fh_generic;
1788 fromnd.ni_cnd.cn_nameiop = 0;
1789 tond.ni_cnd.cn_nameiop = 0;
1790 nfsm_srvmtofh(ffhp);
1791 nfsm_srvnamesiz(len);
1792 /*
1793 * Remember our original uid so that we can reset cr_uid before
1794 * the second nfs_namei() call, in case it is remapped.
1795 */
1796 saved_uid = cred->cr_uid;
1797 fromnd.ni_cnd.cn_cred = cred;
1798 fromnd.ni_cnd.cn_nameiop = DELETE;
1799 fromnd.ni_cnd.cn_flags = WANTPARENT | SAVESTART;
1800 error = nfs_namei(&fromnd, ffhp, len, slp, nam, &md,
1801 &dpos, &fdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1802 if (fdirp) {
1803 if (v3)
1804 fdirfor_ret = VOP_GETATTR(fdirp, &fdirfor, cred,
1805 procp);
1806 else {
1807 vrele(fdirp);
1808 fdirp = (struct vnode *)0;
1809 }
1810 }
1811 if (error) {
1812 nfsm_reply(2 * NFSX_WCCDATA(v3));
1813 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1814 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1815 if (fdirp)
1816 vrele(fdirp);
1817 return (0);
1818 }
1819 fvp = fromnd.ni_vp;
1820 nfsm_srvmtofh(tfhp);
1821 nfsm_strsiz(len2, NFS_MAXNAMLEN);
1822 cred->cr_uid = saved_uid;
1823 tond.ni_cnd.cn_cred = cred;
1824 tond.ni_cnd.cn_nameiop = RENAME;
1825 tond.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART;
1826 error = nfs_namei(&tond, tfhp, len2, slp, nam, &md,
1827 &dpos, &tdirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1828 if (tdirp) {
1829 if (v3)
1830 tdirfor_ret = VOP_GETATTR(tdirp, &tdirfor, cred,
1831 procp);
1832 else {
1833 vrele(tdirp);
1834 tdirp = (struct vnode *)0;
1835 }
1836 }
1837 if (error) {
1838 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1839 vrele(fromnd.ni_dvp);
1840 vrele(fvp);
1841 goto out1;
1842 }
1843 tdvp = tond.ni_dvp;
1844 tvp = tond.ni_vp;
1845 if (tvp != NULL) {
1846 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1847 if (v3)
1848 error = EEXIST;
1849 else
1850 error = EISDIR;
1851 goto out;
1852 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1853 if (v3)
1854 error = EEXIST;
1855 else
1856 error = ENOTDIR;
1857 goto out;
1858 }
1859 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1860 if (v3)
1861 error = EXDEV;
1862 else
1863 error = ENOTEMPTY;
1864 goto out;
1865 }
1866 }
1867 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1868 if (v3)
1869 error = EXDEV;
1870 else
1871 error = ENOTEMPTY;
1872 goto out;
1873 }
1874 if (fvp->v_mount != tdvp->v_mount) {
1875 if (v3)
1876 error = EXDEV;
1877 else
1878 error = ENOTEMPTY;
1879 goto out;
1880 }
1881 if (fvp == tdvp)
1882 if (v3)
1883 error = EINVAL;
1884 else
1885 error = ENOTEMPTY;
1886 /*
1887 * If source is the same as the destination (that is the
1888 * same vnode with the same name in the same directory),
1889 * then there is nothing to do.
1890 */
1891 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1892 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1893 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1894 fromnd.ni_cnd.cn_namelen))
1895 error = -1;
1896 out:
1897 if (!error) {
1898 nqsrv_getl(fromnd.ni_dvp, ND_WRITE);
1899 nqsrv_getl(tdvp, ND_WRITE);
1900 if (tvp) {
1901 (void)vnode_pager_uncache(tvp);
1902 nqsrv_getl(tvp, ND_WRITE);
1903 }
1904 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1905 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1906 } else {
1907 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1908 if (tdvp == tvp)
1909 vrele(tdvp);
1910 else
1911 vput(tdvp);
1912 if (tvp)
1913 vput(tvp);
1914 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1915 vrele(fromnd.ni_dvp);
1916 vrele(fvp);
1917 if (error == -1)
1918 error = 0;
1919 }
1920 vrele(tond.ni_startdir);
1921 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1922 out1:
1923 if (fdirp) {
1924 fdiraft_ret = VOP_GETATTR(fdirp, &fdiraft, cred, procp);
1925 vrele(fdirp);
1926 }
1927 if (tdirp) {
1928 tdiraft_ret = VOP_GETATTR(tdirp, &tdiraft, cred, procp);
1929 vrele(tdirp);
1930 }
1931 vrele(fromnd.ni_startdir);
1932 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1933 nfsm_reply(2 * NFSX_WCCDATA(v3));
1934 if (v3) {
1935 nfsm_srvwcc_data(fdirfor_ret, &fdirfor, fdiraft_ret, &fdiraft);
1936 nfsm_srvwcc_data(tdirfor_ret, &tdirfor, tdiraft_ret, &tdiraft);
1937 }
1938 return (0);
1939
1940 nfsmout:
1941 if (fdirp)
1942 vrele(fdirp);
1943 if (tdirp)
1944 vrele(tdirp);
1945 if (tond.ni_cnd.cn_nameiop) {
1946 vrele(tond.ni_startdir);
1947 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1948 }
1949 if (fromnd.ni_cnd.cn_nameiop) {
1950 vrele(fromnd.ni_startdir);
1951 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1952 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1953 vrele(fromnd.ni_dvp);
1954 vrele(fvp);
1955 }
1956 return (error);
1957 }
1958
1959 /*
1960 * nfs link service
1961 */
1962 int
1963 nfsrv_link(nfsd, slp, procp, mrq)
1964 struct nfsrv_descript *nfsd;
1965 struct nfssvc_sock *slp;
1966 struct proc *procp;
1967 struct mbuf **mrq;
1968 {
1969 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
1970 struct mbuf *nam = nfsd->nd_nam;
1971 caddr_t dpos = nfsd->nd_dpos;
1972 struct ucred *cred = &nfsd->nd_cr;
1973 struct nameidata nd;
1974 register u_int32_t *tl;
1975 register int32_t t1;
1976 caddr_t bpos;
1977 int error = 0, rdonly, cache, len, dirfor_ret = 1, diraft_ret = 1;
1978 int getret = 1, v3 = (nfsd->nd_flag & ND_NFSV3);
1979 char *cp2;
1980 struct mbuf *mb, *mreq;
1981 struct vnode *vp, *xp, *dirp = (struct vnode *)0;
1982 struct vattr dirfor, diraft, at;
1983 nfsfh_t nfh, dnfh;
1984 fhandle_t *fhp, *dfhp;
1985 u_quad_t frev;
1986
1987 fhp = &nfh.fh_generic;
1988 dfhp = &dnfh.fh_generic;
1989 nfsm_srvmtofh(fhp);
1990 nfsm_srvmtofh(dfhp);
1991 nfsm_srvnamesiz(len);
1992 error = nfsrv_fhtovp(fhp, FALSE, &vp, cred, slp, nam,
1993 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
1994 if (error) {
1995 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
1996 nfsm_srvpostop_attr(getret, &at);
1997 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
1998 return (0);
1999 }
2000 if (vp->v_type == VDIR && (error = suser(cred, (u_short *)0)) != 0)
2001 goto out1;
2002 nd.ni_cnd.cn_cred = cred;
2003 nd.ni_cnd.cn_nameiop = CREATE;
2004 nd.ni_cnd.cn_flags = LOCKPARENT;
2005 error = nfs_namei(&nd, dfhp, len, slp, nam, &md, &dpos,
2006 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2007 if (dirp) {
2008 if (v3)
2009 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2010 procp);
2011 else {
2012 vrele(dirp);
2013 dirp = (struct vnode *)0;
2014 }
2015 }
2016 if (error)
2017 goto out1;
2018 xp = nd.ni_vp;
2019 if (xp != NULL) {
2020 error = EEXIST;
2021 goto out;
2022 }
2023 xp = nd.ni_dvp;
2024 if (vp->v_mount != xp->v_mount)
2025 error = EXDEV;
2026 out:
2027 if (!error) {
2028 nqsrv_getl(vp, ND_WRITE);
2029 nqsrv_getl(xp, ND_WRITE);
2030 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
2031 } else {
2032 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2033 if (nd.ni_dvp == nd.ni_vp)
2034 vrele(nd.ni_dvp);
2035 else
2036 vput(nd.ni_dvp);
2037 if (nd.ni_vp)
2038 vrele(nd.ni_vp);
2039 }
2040 out1:
2041 if (v3)
2042 getret = VOP_GETATTR(vp, &at, cred, procp);
2043 if (dirp) {
2044 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2045 vrele(dirp);
2046 }
2047 vrele(vp);
2048 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2049 if (v3) {
2050 nfsm_srvpostop_attr(getret, &at);
2051 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2052 return (0);
2053 }
2054 nfsm_srvdone;
2055 }
2056
2057 /*
2058 * nfs symbolic link service
2059 */
2060 int
2061 nfsrv_symlink(nfsd, slp, procp, mrq)
2062 struct nfsrv_descript *nfsd;
2063 struct nfssvc_sock *slp;
2064 struct proc *procp;
2065 struct mbuf **mrq;
2066 {
2067 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2068 struct mbuf *nam = nfsd->nd_nam;
2069 caddr_t dpos = nfsd->nd_dpos;
2070 struct ucred *cred = &nfsd->nd_cr;
2071 struct vattr va, dirfor, diraft;
2072 struct nameidata nd;
2073 register u_int32_t *tl;
2074 register int32_t t1;
2075 struct nfsv2_sattr *sp;
2076 char *bpos, *pathcp = NULL, *cp2;
2077 struct uio io;
2078 struct iovec iv;
2079 int error = 0, cache, len, len2, dirfor_ret = 1, diraft_ret = 1;
2080 int v3 = (nfsd->nd_flag & ND_NFSV3);
2081 struct mbuf *mb, *mreq, *mb2;
2082 struct vnode *dirp = (struct vnode *)0;
2083 nfsfh_t nfh;
2084 fhandle_t *fhp;
2085 u_quad_t frev;
2086
2087 nd.ni_cnd.cn_nameiop = 0;
2088 fhp = &nfh.fh_generic;
2089 nfsm_srvmtofh(fhp);
2090 nfsm_srvnamesiz(len);
2091 nd.ni_cnd.cn_cred = cred;
2092 nd.ni_cnd.cn_nameiop = CREATE;
2093 nd.ni_cnd.cn_flags = LOCKPARENT | SAVESTART;
2094 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2095 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2096 if (dirp) {
2097 if (v3)
2098 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2099 procp);
2100 else {
2101 vrele(dirp);
2102 dirp = (struct vnode *)0;
2103 }
2104 }
2105 if (error)
2106 goto out;
2107 VATTR_NULL(&va);
2108 if (v3)
2109 nfsm_srvsattr(&va);
2110 nfsm_strsiz(len2, NFS_MAXPATHLEN);
2111 MALLOC(pathcp, caddr_t, len2 + 1, M_TEMP, M_WAITOK);
2112 iv.iov_base = pathcp;
2113 iv.iov_len = len2;
2114 io.uio_resid = len2;
2115 io.uio_offset = 0;
2116 io.uio_iov = &iv;
2117 io.uio_iovcnt = 1;
2118 io.uio_segflg = UIO_SYSSPACE;
2119 io.uio_rw = UIO_READ;
2120 io.uio_procp = (struct proc *)0;
2121 nfsm_mtouio(&io, len2);
2122 if (!v3) {
2123 nfsm_dissect(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2124 va.va_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
2125 }
2126 *(pathcp + len2) = '\0';
2127 if (nd.ni_vp) {
2128 vrele(nd.ni_startdir);
2129 free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
2130 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2131 if (nd.ni_dvp == nd.ni_vp)
2132 vrele(nd.ni_dvp);
2133 else
2134 vput(nd.ni_dvp);
2135 vrele(nd.ni_vp);
2136 error = EEXIST;
2137 goto out;
2138 }
2139 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2140 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va, pathcp);
2141 if (error)
2142 vrele(nd.ni_startdir);
2143 else {
2144 if (v3) {
2145 nd.ni_cnd.cn_nameiop = LOOKUP;
2146 nd.ni_cnd.cn_flags &= ~(LOCKPARENT | SAVESTART | FOLLOW);
2147 nd.ni_cnd.cn_flags |= (NOFOLLOW | LOCKLEAF);
2148 nd.ni_cnd.cn_proc = procp;
2149 nd.ni_cnd.cn_cred = cred;
2150 error = lookup(&nd);
2151 if (!error) {
2152 bzero((caddr_t)fhp, sizeof(nfh));
2153 fhp->fh_fsid = nd.ni_vp->v_mount->mnt_stat.f_fsid;
2154 error = VFS_VPTOFH(nd.ni_vp, &fhp->fh_fid);
2155 if (!error)
2156 error = VOP_GETATTR(nd.ni_vp, &va, cred,
2157 procp);
2158 vput(nd.ni_vp);
2159 }
2160 } else
2161 vrele(nd.ni_startdir);
2162 FREE(nd.ni_cnd.cn_pnbuf, M_NAMEI);
2163 }
2164 out:
2165 if (pathcp)
2166 FREE(pathcp, M_TEMP);
2167 if (dirp) {
2168 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2169 vrele(dirp);
2170 }
2171 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2172 if (v3) {
2173 if (!error) {
2174 nfsm_srvpostop_fh(fhp);
2175 nfsm_srvpostop_attr(0, &va);
2176 }
2177 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2178 }
2179 return (0);
2180 nfsmout:
2181 if (nd.ni_cnd.cn_nameiop) {
2182 vrele(nd.ni_startdir);
2183 free(nd.ni_cnd.cn_pnbuf, M_NAMEI);
2184 }
2185 if (dirp)
2186 vrele(dirp);
2187 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2188 if (nd.ni_dvp == nd.ni_vp)
2189 vrele(nd.ni_dvp);
2190 else
2191 vput(nd.ni_dvp);
2192 if (nd.ni_vp)
2193 vrele(nd.ni_vp);
2194 if (pathcp)
2195 FREE(pathcp, M_TEMP);
2196 return (error);
2197 }
2198
2199 /*
2200 * nfs mkdir service
2201 */
2202 int
2203 nfsrv_mkdir(nfsd, slp, procp, mrq)
2204 struct nfsrv_descript *nfsd;
2205 struct nfssvc_sock *slp;
2206 struct proc *procp;
2207 struct mbuf **mrq;
2208 {
2209 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2210 struct mbuf *nam = nfsd->nd_nam;
2211 caddr_t dpos = nfsd->nd_dpos;
2212 struct ucred *cred = &nfsd->nd_cr;
2213 struct vattr va, dirfor, diraft;
2214 register struct nfs_fattr *fp;
2215 struct nameidata nd;
2216 register caddr_t cp;
2217 register u_int32_t *tl;
2218 register int32_t t1;
2219 caddr_t bpos;
2220 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2221 int v3 = (nfsd->nd_flag & ND_NFSV3);
2222 char *cp2;
2223 struct mbuf *mb, *mb2, *mreq;
2224 struct vnode *vp, *dirp = (struct vnode *)0;
2225 nfsfh_t nfh;
2226 fhandle_t *fhp;
2227 u_quad_t frev;
2228
2229 fhp = &nfh.fh_generic;
2230 nfsm_srvmtofh(fhp);
2231 nfsm_srvnamesiz(len);
2232 nd.ni_cnd.cn_cred = cred;
2233 nd.ni_cnd.cn_nameiop = CREATE;
2234 nd.ni_cnd.cn_flags = LOCKPARENT;
2235 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2236 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2237 if (dirp) {
2238 if (v3)
2239 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2240 procp);
2241 else {
2242 vrele(dirp);
2243 dirp = (struct vnode *)0;
2244 }
2245 }
2246 if (error) {
2247 nfsm_reply(NFSX_WCCDATA(v3));
2248 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2249 if (dirp)
2250 vrele(dirp);
2251 return (0);
2252 }
2253 VATTR_NULL(&va);
2254 if (v3) {
2255 nfsm_srvsattr(&va);
2256 } else {
2257 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2258 va.va_mode = nfstov_mode(*tl++);
2259 }
2260 va.va_type = VDIR;
2261 vp = nd.ni_vp;
2262 if (vp != NULL) {
2263 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2264 if (nd.ni_dvp == vp)
2265 vrele(nd.ni_dvp);
2266 else
2267 vput(nd.ni_dvp);
2268 vrele(vp);
2269 error = EEXIST;
2270 goto out;
2271 }
2272 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2273 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &va);
2274 if (!error) {
2275 vp = nd.ni_vp;
2276 bzero((caddr_t)fhp, sizeof(nfh));
2277 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
2278 error = VFS_VPTOFH(vp, &fhp->fh_fid);
2279 if (!error)
2280 error = VOP_GETATTR(vp, &va, cred, procp);
2281 vput(vp);
2282 }
2283 out:
2284 if (dirp) {
2285 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2286 vrele(dirp);
2287 }
2288 nfsm_reply(NFSX_SRVFH(v3) + NFSX_POSTOPATTR(v3) + NFSX_WCCDATA(v3));
2289 if (v3) {
2290 if (!error) {
2291 nfsm_srvpostop_fh(fhp);
2292 nfsm_srvpostop_attr(0, &va);
2293 }
2294 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2295 } else {
2296 nfsm_srvfhtom(fhp, v3);
2297 nfsm_build(fp, struct nfs_fattr *, NFSX_V2FATTR);
2298 nfsm_srvfillattr(&va, fp);
2299 }
2300 return (0);
2301 nfsmout:
2302 if (dirp)
2303 vrele(dirp);
2304 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2305 if (nd.ni_dvp == nd.ni_vp)
2306 vrele(nd.ni_dvp);
2307 else
2308 vput(nd.ni_dvp);
2309 if (nd.ni_vp)
2310 vrele(nd.ni_vp);
2311 return (error);
2312 }
2313
2314 /*
2315 * nfs rmdir service
2316 */
2317 int
2318 nfsrv_rmdir(nfsd, slp, procp, mrq)
2319 struct nfsrv_descript *nfsd;
2320 struct nfssvc_sock *slp;
2321 struct proc *procp;
2322 struct mbuf **mrq;
2323 {
2324 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2325 struct mbuf *nam = nfsd->nd_nam;
2326 caddr_t dpos = nfsd->nd_dpos;
2327 struct ucred *cred = &nfsd->nd_cr;
2328 register u_int32_t *tl;
2329 register int32_t t1;
2330 caddr_t bpos;
2331 int error = 0, cache, len, dirfor_ret = 1, diraft_ret = 1;
2332 int v3 = (nfsd->nd_flag & ND_NFSV3);
2333 char *cp2;
2334 struct mbuf *mb, *mreq;
2335 struct vnode *vp, *dirp = (struct vnode *)0;
2336 struct vattr dirfor, diraft;
2337 nfsfh_t nfh;
2338 fhandle_t *fhp;
2339 struct nameidata nd;
2340 u_quad_t frev;
2341
2342 fhp = &nfh.fh_generic;
2343 nfsm_srvmtofh(fhp);
2344 nfsm_srvnamesiz(len);
2345 nd.ni_cnd.cn_cred = cred;
2346 nd.ni_cnd.cn_nameiop = DELETE;
2347 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
2348 error = nfs_namei(&nd, fhp, len, slp, nam, &md, &dpos,
2349 &dirp, procp, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2350 if (dirp) {
2351 if (v3)
2352 dirfor_ret = VOP_GETATTR(dirp, &dirfor, cred,
2353 procp);
2354 else {
2355 vrele(dirp);
2356 dirp = (struct vnode *)0;
2357 }
2358 }
2359 if (error) {
2360 nfsm_reply(NFSX_WCCDATA(v3));
2361 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2362 if (dirp)
2363 vrele(dirp);
2364 return (0);
2365 }
2366 vp = nd.ni_vp;
2367 if (vp->v_type != VDIR) {
2368 error = ENOTDIR;
2369 goto out;
2370 }
2371 /*
2372 * No rmdir "." please.
2373 */
2374 if (nd.ni_dvp == vp) {
2375 error = EINVAL;
2376 goto out;
2377 }
2378 /*
2379 * The root of a mounted filesystem cannot be deleted.
2380 */
2381 if (vp->v_flag & VROOT)
2382 error = EBUSY;
2383 out:
2384 if (!error) {
2385 nqsrv_getl(nd.ni_dvp, ND_WRITE);
2386 nqsrv_getl(vp, ND_WRITE);
2387 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2388 } else {
2389 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2390 if (nd.ni_dvp == nd.ni_vp)
2391 vrele(nd.ni_dvp);
2392 else
2393 vput(nd.ni_dvp);
2394 vput(vp);
2395 }
2396 if (dirp) {
2397 diraft_ret = VOP_GETATTR(dirp, &diraft, cred, procp);
2398 vrele(dirp);
2399 }
2400 nfsm_reply(NFSX_WCCDATA(v3));
2401 if (v3) {
2402 nfsm_srvwcc_data(dirfor_ret, &dirfor, diraft_ret, &diraft);
2403 return (0);
2404 }
2405 nfsm_srvdone;
2406 }
2407
2408 /*
2409 * nfs readdir service
2410 * - mallocs what it thinks is enough to read
2411 * count rounded up to a multiple of NFS_DIRBLKSIZ <= NFS_MAXREADDIR
2412 * - calls VOP_READDIR()
2413 * - loops around building the reply
2414 * if the output generated exceeds count break out of loop
2415 * The nfsm_clget macro is used here so that the reply will be packed
2416 * tightly in mbuf clusters.
2417 * - it only knows that it has encountered eof when the VOP_READDIR()
2418 * reads nothing
2419 * - as such one readdir rpc will return eof false although you are there
2420 * and then the next will return eof
2421 * - it trims out records with d_fileno == 0
2422 * this doesn't matter for Unix clients, but they might confuse clients
2423 * for other os'.
2424 * - it trims out records with d_type == DT_WHT
2425 * these cannot be seen through NFS (unless we extend the protocol)
2426 * NB: It is tempting to set eof to true if the VOP_READDIR() reads less
2427 * than requested, but this may not apply to all filesystems. For
2428 * example, client NFS does not { although it is never remote mounted
2429 * anyhow }
2430 * The alternate call nfsrv_readdirplus() does lookups as well.
2431 * PS: The NFS protocol spec. does not clarify what the "count" byte
2432 * argument is a count of.. just name strings and file id's or the
2433 * entire reply rpc or ...
2434 * I tried just file name and id sizes and it confused the Sun client,
2435 * so I am using the full rpc size now. The "paranoia.." comment refers
2436 * to including the status longwords that are not a part of the dir.
2437 * "entry" structures, but are in the rpc.
2438 */
2439 struct flrep {
2440 nfsuint64 fl_off;
2441 u_int32_t fl_postopok;
2442 u_int32_t fl_fattr[NFSX_V3FATTR / sizeof (u_int32_t)];
2443 u_int32_t fl_fhok;
2444 u_int32_t fl_fhsize;
2445 u_int32_t fl_nfh[NFSX_V3FH / sizeof (u_int32_t)];
2446 };
2447
2448 int
2449 nfsrv_readdir(nfsd, slp, procp, mrq)
2450 struct nfsrv_descript *nfsd;
2451 struct nfssvc_sock *slp;
2452 struct proc *procp;
2453 struct mbuf **mrq;
2454 {
2455 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2456 struct mbuf *nam = nfsd->nd_nam;
2457 caddr_t dpos = nfsd->nd_dpos;
2458 struct ucred *cred = &nfsd->nd_cr;
2459 register char *bp, *be;
2460 register struct mbuf *mp;
2461 register struct dirent *dp;
2462 register caddr_t cp;
2463 register u_int32_t *tl;
2464 register int32_t t1;
2465 caddr_t bpos;
2466 struct mbuf *mb, *mb2, *mreq, *mp2;
2467 char *cpos, *cend, *cp2, *rbuf;
2468 struct vnode *vp;
2469 struct vattr at;
2470 nfsfh_t nfh;
2471 fhandle_t *fhp;
2472 struct uio io;
2473 struct iovec iv;
2474 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2475 int siz, cnt, fullsiz, eofflag, rdonly, cache, ncookies;
2476 int v3 = (nfsd->nd_flag & ND_NFSV3);
2477 u_quad_t frev, off, toff, verf;
2478 off_t *cookies = NULL, *cookiep;
2479 nfsuint64 jar;
2480
2481 fhp = &nfh.fh_generic;
2482 nfsm_srvmtofh(fhp);
2483 if (v3) {
2484 nfsm_dissect(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2485 fxdr_hyper(tl, &toff);
2486 tl += 2;
2487 fxdr_hyper(tl, &verf);
2488 tl += 2;
2489 } else {
2490 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2491 toff = fxdr_unsigned(u_quad_t, *tl++);
2492 }
2493 off = toff;
2494 cnt = fxdr_unsigned(int, *tl);
2495 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2496 xfer = NFS_SRVMAXDATA(nfsd);
2497 if (siz > xfer)
2498 siz = xfer;
2499 fullsiz = siz;
2500 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2501 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2502 if (error) {
2503 nfsm_reply(NFSX_UNSIGNED);
2504 nfsm_srvpostop_attr(getret, &at);
2505 return (0);
2506 }
2507 nqsrv_getl(vp, ND_READ);
2508 if (v3) {
2509 error = getret = VOP_GETATTR(vp, &at, cred, procp);
2510 #ifdef NFS3_STRICTVERF
2511 /*
2512 * XXX This check is too strict for Solaris 2.5 clients.
2513 */
2514 if (!error && toff && verf != at.va_filerev)
2515 error = NFSERR_BAD_COOKIE;
2516 #endif
2517 }
2518 if (!error)
2519 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
2520 if (error) {
2521 vput(vp);
2522 nfsm_reply(NFSX_POSTOPATTR(v3));
2523 nfsm_srvpostop_attr(getret, &at);
2524 return (0);
2525 }
2526 #ifdef Lite2_integrated
2527 VOP_UNLOCK(vp, 0, procp);
2528 #else
2529 VOP_UNLOCK(vp);
2530 #endif
2531 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2532 ncookies = siz / (5 * NFSX_UNSIGNED); /*7 for V3, but it's an est. so*/
2533 MALLOC(cookies, off_t *, ncookies * sizeof (off_t), M_TEMP,
2534 M_WAITOK);
2535 again:
2536 iv.iov_base = rbuf;
2537 iv.iov_len = fullsiz;
2538 io.uio_iov = &iv;
2539 io.uio_iovcnt = 1;
2540 io.uio_offset = (off_t)off;
2541 io.uio_resid = fullsiz;
2542 io.uio_segflg = UIO_SYSSPACE;
2543 io.uio_rw = UIO_READ;
2544 io.uio_procp = (struct proc *)0;
2545 eofflag = 0;
2546 #ifdef Lite2_integrated
2547 VOP_LOCK(vp, 0, procp);
2548 #else
2549 VOP_LOCK(vp);
2550 #endif
2551
2552 error = VOP_READDIR(vp, &io, cred, &eofflag, cookies, ncookies);
2553
2554 off = (off_t)io.uio_offset;
2555 if (!cookies && !error)
2556 error = NFSERR_PERM;
2557 if (v3) {
2558 getret = VOP_GETATTR(vp, &at, cred, procp);
2559 if (!error)
2560 error = getret;
2561 }
2562
2563 #ifdef Lite2_integrated
2564 VOP_UNLOCK(vp, 0, procp);
2565 #else
2566 VOP_UNLOCK(vp);
2567 #endif
2568 if (error) {
2569 vrele(vp);
2570 free((caddr_t)rbuf, M_TEMP);
2571 if (cookies)
2572 free((caddr_t)cookies, M_TEMP);
2573 nfsm_reply(NFSX_POSTOPATTR(v3));
2574 nfsm_srvpostop_attr(getret, &at);
2575 return (0);
2576 }
2577 if (io.uio_resid) {
2578 siz -= io.uio_resid;
2579
2580 /*
2581 * If nothing read, return eof
2582 * rpc reply
2583 */
2584 if (siz == 0) {
2585 vrele(vp);
2586 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2587 2 * NFSX_UNSIGNED);
2588 if (v3) {
2589 nfsm_srvpostop_attr(getret, &at);
2590 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2591 txdr_hyper(&at.va_filerev, tl);
2592 tl += 2;
2593 } else
2594 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2595 *tl++ = nfs_false;
2596 *tl = nfs_true;
2597 FREE((caddr_t)rbuf, M_TEMP);
2598 FREE((caddr_t)cookies, M_TEMP);
2599 return (0);
2600 }
2601 }
2602
2603 /*
2604 * Check for degenerate cases of nothing useful read.
2605 * If so go try again
2606 */
2607 cpos = rbuf;
2608 cend = rbuf + siz;
2609 dp = (struct dirent *)cpos;
2610 cookiep = cookies;
2611
2612 while (cpos < cend && ncookies > 0 &&
2613 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2614 cpos += dp->d_reclen;
2615 dp = (struct dirent *)cpos;
2616 cookiep++;
2617 ncookies--;
2618 }
2619 if (cpos >= cend || ncookies == 0) {
2620 toff = off;
2621 siz = fullsiz;
2622 goto again;
2623 }
2624
2625 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2626 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2627 if (v3) {
2628 nfsm_srvpostop_attr(getret, &at);
2629 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2630 txdr_hyper(&at.va_filerev, tl);
2631 }
2632 mp = mp2 = mb;
2633 bp = bpos;
2634 be = bp + M_TRAILINGSPACE(mp);
2635
2636 /* Loop through the records and build reply */
2637 while (cpos < cend && ncookies > 0) {
2638 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2639 nlen = dp->d_namlen;
2640 rem = nfsm_rndup(nlen)-nlen;
2641 len += (4 * NFSX_UNSIGNED + nlen + rem);
2642 if (v3)
2643 len += 2 * NFSX_UNSIGNED;
2644 if (len > cnt) {
2645 eofflag = 0;
2646 break;
2647 }
2648 /*
2649 * Build the directory record xdr from
2650 * the dirent entry.
2651 */
2652 nfsm_clget;
2653 *tl = nfs_true;
2654 bp += NFSX_UNSIGNED;
2655 if (v3) {
2656 nfsm_clget;
2657 *tl = 0;
2658 bp += NFSX_UNSIGNED;
2659 }
2660 nfsm_clget;
2661 *tl = txdr_unsigned(dp->d_fileno);
2662 bp += NFSX_UNSIGNED;
2663 nfsm_clget;
2664 *tl = txdr_unsigned(nlen);
2665 bp += NFSX_UNSIGNED;
2666
2667 /* And loop around copying the name */
2668 xfer = nlen;
2669 cp = dp->d_name;
2670 while (xfer > 0) {
2671 nfsm_clget;
2672 if ((bp+xfer) > be)
2673 tsiz = be-bp;
2674 else
2675 tsiz = xfer;
2676 bcopy(cp, bp, tsiz);
2677 bp += tsiz;
2678 xfer -= tsiz;
2679 if (xfer > 0)
2680 cp += tsiz;
2681 }
2682 /* And null pad to an int32_t boundary */
2683 for (i = 0; i < rem; i++)
2684 *bp++ = '\0';
2685 nfsm_clget;
2686
2687 /* Finish off the record */
2688 txdr_hyper(cookiep, &jar);
2689 if (v3) {
2690 *tl = jar.nfsuquad[0];
2691 bp += NFSX_UNSIGNED;
2692 nfsm_clget;
2693 }
2694 *tl = jar.nfsuquad[1];
2695 bp += NFSX_UNSIGNED;
2696 }
2697 cpos += dp->d_reclen;
2698 dp = (struct dirent *)cpos;
2699 cookiep++;
2700 ncookies--;
2701 }
2702 vrele(vp);
2703 nfsm_clget;
2704 *tl = nfs_false;
2705 bp += NFSX_UNSIGNED;
2706 nfsm_clget;
2707 if (eofflag)
2708 *tl = nfs_true;
2709 else
2710 *tl = nfs_false;
2711 bp += NFSX_UNSIGNED;
2712 if (mp != mb) {
2713 if (bp < be)
2714 mp->m_len = bp - mtod(mp, caddr_t);
2715 } else
2716 mp->m_len += bp - bpos;
2717 FREE((caddr_t)rbuf, M_TEMP);
2718 FREE((caddr_t)cookies, M_TEMP);
2719 nfsm_srvdone;
2720 }
2721
2722 int
2723 nfsrv_readdirplus(nfsd, slp, procp, mrq)
2724 struct nfsrv_descript *nfsd;
2725 struct nfssvc_sock *slp;
2726 struct proc *procp;
2727 struct mbuf **mrq;
2728 {
2729 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2730 struct mbuf *nam = nfsd->nd_nam;
2731 caddr_t dpos = nfsd->nd_dpos;
2732 struct ucred *cred = &nfsd->nd_cr;
2733 register char *bp, *be;
2734 register struct mbuf *mp;
2735 register struct dirent *dp;
2736 register caddr_t cp;
2737 register u_int32_t *tl;
2738 register int32_t t1;
2739 caddr_t bpos;
2740 struct mbuf *mb, *mb2, *mreq, *mp2;
2741 char *cpos, *cend, *cp2, *rbuf;
2742 struct vnode *vp, *nvp;
2743 struct flrep fl;
2744 nfsfh_t nfh;
2745 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
2746 struct uio io;
2747 struct iovec iv;
2748 struct vattr va, at, *vap = &va;
2749 struct nfs_fattr *fp;
2750 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2751 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies;
2752 u_quad_t frev, off, toff, verf;
2753 off_t *cookies = NULL, *cookiep;
2754
2755 fhp = &nfh.fh_generic;
2756 nfsm_srvmtofh(fhp);
2757 nfsm_dissect(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2758 fxdr_hyper(tl, &toff);
2759 tl += 2;
2760 fxdr_hyper(tl, &verf);
2761 tl += 2;
2762 siz = fxdr_unsigned(int, *tl++);
2763 cnt = fxdr_unsigned(int, *tl);
2764 off = toff;
2765 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2766 xfer = NFS_SRVMAXDATA(nfsd);
2767 if (siz > xfer)
2768 siz = xfer;
2769 fullsiz = siz;
2770 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2771 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
2772 if (error) {
2773 nfsm_reply(NFSX_UNSIGNED);
2774 nfsm_srvpostop_attr(getret, &at);
2775 return (0);
2776 }
2777 error = getret = VOP_GETATTR(vp, &at, cred, procp);
2778 #ifdef NFS3_STRICTVERF
2779 /*
2780 * XXX This check is too strict for Solaris 2.5 clients.
2781 */
2782 if (!error && toff && verf != at.va_filerev)
2783 error = NFSERR_BAD_COOKIE;
2784 #endif
2785 if (!error) {
2786 nqsrv_getl(vp, ND_READ);
2787 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
2788 }
2789 if (error) {
2790 vput(vp);
2791 nfsm_reply(NFSX_V3POSTOPATTR);
2792 nfsm_srvpostop_attr(getret, &at);
2793 return (0);
2794 }
2795 #ifdef Lite2_integrated
2796 VOP_UNLOCK(vp, 0, procp);
2797 #else
2798 VOP_UNLOCK(vp);
2799 #endif
2800
2801 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2802 ncookies = siz / (7 * NFSX_UNSIGNED);
2803 MALLOC(cookies, off_t *, ncookies * sizeof (off_t), M_TEMP,
2804 M_WAITOK);
2805 again:
2806 iv.iov_base = rbuf;
2807 iv.iov_len = fullsiz;
2808 io.uio_iov = &iv;
2809 io.uio_iovcnt = 1;
2810 io.uio_offset = (off_t)off;
2811 io.uio_resid = fullsiz;
2812 io.uio_segflg = UIO_SYSSPACE;
2813 io.uio_rw = UIO_READ;
2814 io.uio_procp = (struct proc *)0;
2815 eofflag = 0;
2816
2817 #ifdef Lite2_integrated
2818 VOP_LOCK(vp, 0, procp);
2819 #else
2820 VOP_LOCK(vp);
2821 #endif
2822 error = VOP_READDIR(vp, &io, cred, &eofflag, cookies, ncookies);
2823
2824 off = (u_quad_t)io.uio_offset;
2825 getret = VOP_GETATTR(vp, &at, cred, procp);
2826
2827 #ifdef Lite2_integrated
2828 VOP_UNLOCK(vp, 0, procp);
2829 #else
2830 VOP_UNLOCK(vp);
2831 #endif
2832
2833 /*
2834 * If the VGET operation doesn't work for this filesystem,
2835 * we can't support readdirplus. Returning NOTSUPP should
2836 * make clients fall back to plain readdir.
2837 * There's no need to check for VPTOFH as well, we wouldn't
2838 * even be here otherwise.
2839 */
2840 if (!getret) {
2841 if ((getret = VFS_VGET(vp->v_mount, at.va_fileid, &nvp)))
2842 getret = (getret == EOPNOTSUPP) ?
2843 NFSERR_NOTSUPP : NFSERR_IO;
2844 else
2845 vput(nvp);
2846 }
2847
2848 if (!cookies && !error)
2849 error = NFSERR_PERM;
2850 if (!error)
2851 error = getret;
2852 if (error) {
2853 vrele(vp);
2854 if (cookies)
2855 free((caddr_t)cookies, M_TEMP);
2856 free((caddr_t)rbuf, M_TEMP);
2857 nfsm_reply(NFSX_V3POSTOPATTR);
2858 nfsm_srvpostop_attr(getret, &at);
2859 return (0);
2860 }
2861 if (io.uio_resid) {
2862 siz -= io.uio_resid;
2863
2864 /*
2865 * If nothing read, return eof
2866 * rpc reply
2867 */
2868 if (siz == 0) {
2869 vrele(vp);
2870 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
2871 2 * NFSX_UNSIGNED);
2872 nfsm_srvpostop_attr(getret, &at);
2873 nfsm_build(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2874 txdr_hyper(&at.va_filerev, tl);
2875 tl += 2;
2876 *tl++ = nfs_false;
2877 *tl = nfs_true;
2878 FREE((caddr_t)cookies, M_TEMP);
2879 FREE((caddr_t)rbuf, M_TEMP);
2880 return (0);
2881 }
2882 }
2883
2884 /*
2885 * Check for degenerate cases of nothing useful read.
2886 * If so go try again
2887 */
2888 cpos = rbuf;
2889 cend = rbuf + siz;
2890 dp = (struct dirent *)cpos;
2891 cookiep = cookies;
2892
2893 while (cpos < cend && ncookies > 0 &&
2894 (dp->d_fileno == 0 || dp->d_type == DT_WHT)) {
2895 cpos += dp->d_reclen;
2896 dp = (struct dirent *)cpos;
2897 cookiep++;
2898 ncookies--;
2899 }
2900 if (cpos >= cend || ncookies == 0) {
2901 toff = off;
2902 siz = fullsiz;
2903 goto again;
2904 }
2905
2906 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
2907 nfsm_reply(cnt);
2908 nfsm_srvpostop_attr(getret, &at);
2909 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2910 txdr_hyper(&at.va_filerev, tl);
2911 mp = mp2 = mb;
2912 bp = bpos;
2913 be = bp + M_TRAILINGSPACE(mp);
2914
2915 /* Loop through the records and build reply */
2916 while (cpos < cend && ncookies > 0) {
2917 if (dp->d_fileno != 0 && dp->d_type != DT_WHT) {
2918 nlen = dp->d_namlen;
2919 rem = nfsm_rndup(nlen)-nlen;
2920
2921 /*
2922 * For readdir_and_lookup get the vnode using
2923 * the file number.
2924 */
2925 if (VFS_VGET(vp->v_mount, dp->d_fileno, &nvp))
2926 goto invalid;
2927 bzero((caddr_t)nfhp, NFSX_V3FH);
2928 nfhp->fh_fsid =
2929 nvp->v_mount->mnt_stat.f_fsid;
2930 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
2931 vput(nvp);
2932 goto invalid;
2933 }
2934 if (VOP_GETATTR(nvp, vap, cred, procp)) {
2935 vput(nvp);
2936 goto invalid;
2937 }
2938 vput(nvp);
2939
2940 /*
2941 * If either the dircount or maxcount will be
2942 * exceeded, get out now. Both of these lengths
2943 * are calculated conservatively, including all
2944 * XDR overheads.
2945 */
2946 len += (7 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
2947 NFSX_V3POSTOPATTR);
2948 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
2949 if (len > cnt || dirlen > fullsiz) {
2950 eofflag = 0;
2951 break;
2952 }
2953
2954 /*
2955 * Build the directory record xdr from
2956 * the dirent entry.
2957 */
2958 fp = (struct nfs_fattr *)&fl.fl_fattr;
2959 nfsm_srvfillattr(vap, fp);
2960 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
2961 fl.fl_fhok = nfs_true;
2962 fl.fl_postopok = nfs_true;
2963 txdr_hyper(cookiep, fl.fl_off.nfsuquad);
2964
2965 nfsm_clget;
2966 *tl = nfs_true;
2967 bp += NFSX_UNSIGNED;
2968 nfsm_clget;
2969 *tl = 0;
2970 bp += NFSX_UNSIGNED;
2971 nfsm_clget;
2972 *tl = txdr_unsigned(dp->d_fileno);
2973 bp += NFSX_UNSIGNED;
2974 nfsm_clget;
2975 *tl = txdr_unsigned(nlen);
2976 bp += NFSX_UNSIGNED;
2977
2978 /* And loop around copying the name */
2979 xfer = nlen;
2980 cp = dp->d_name;
2981 while (xfer > 0) {
2982 nfsm_clget;
2983 if ((bp + xfer) > be)
2984 tsiz = be - bp;
2985 else
2986 tsiz = xfer;
2987 bcopy(cp, bp, tsiz);
2988 bp += tsiz;
2989 xfer -= tsiz;
2990 if (xfer > 0)
2991 cp += tsiz;
2992 }
2993 /* And null pad to an int32_t boundary */
2994 for (i = 0; i < rem; i++)
2995 *bp++ = '\0';
2996
2997 /*
2998 * Now copy the flrep structure out.
2999 */
3000 xfer = sizeof (struct flrep);
3001 cp = (caddr_t)&fl;
3002 while (xfer > 0) {
3003 nfsm_clget;
3004 if ((bp + xfer) > be)
3005 tsiz = be - bp;
3006 else
3007 tsiz = xfer;
3008 bcopy(cp, bp, tsiz);
3009 bp += tsiz;
3010 xfer -= tsiz;
3011 if (xfer > 0)
3012 cp += tsiz;
3013 }
3014 }
3015 invalid:
3016 cpos += dp->d_reclen;
3017 dp = (struct dirent *)cpos;
3018 cookiep++;
3019 ncookies--;
3020 }
3021 vrele(vp);
3022 nfsm_clget;
3023 *tl = nfs_false;
3024 bp += NFSX_UNSIGNED;
3025 nfsm_clget;
3026 if (eofflag)
3027 *tl = nfs_true;
3028 else
3029 *tl = nfs_false;
3030 bp += NFSX_UNSIGNED;
3031 if (mp != mb) {
3032 if (bp < be)
3033 mp->m_len = bp - mtod(mp, caddr_t);
3034 } else
3035 mp->m_len += bp - bpos;
3036 FREE((caddr_t)cookies, M_TEMP);
3037 FREE((caddr_t)rbuf, M_TEMP);
3038 nfsm_srvdone;
3039 }
3040
3041 /*
3042 * nfs commit service
3043 */
3044 int
3045 nfsrv_commit(nfsd, slp, procp, mrq)
3046 struct nfsrv_descript *nfsd;
3047 struct nfssvc_sock *slp;
3048 struct proc *procp;
3049 struct mbuf **mrq;
3050 {
3051 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3052 struct mbuf *nam = nfsd->nd_nam;
3053 caddr_t dpos = nfsd->nd_dpos;
3054 struct ucred *cred = &nfsd->nd_cr;
3055 struct vattr bfor, aft;
3056 struct vnode *vp;
3057 nfsfh_t nfh;
3058 fhandle_t *fhp;
3059 register u_int32_t *tl;
3060 register int32_t t1;
3061 caddr_t bpos;
3062 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3063 char *cp2;
3064 struct mbuf *mb, *mb2, *mreq;
3065 u_quad_t frev, off;
3066
3067 #ifndef nolint
3068 cache = 0;
3069 #endif
3070 fhp = &nfh.fh_generic;
3071 nfsm_srvmtofh(fhp);
3072 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
3073
3074 /*
3075 * XXX At this time VOP_FSYNC() does not accept offset and byte
3076 * count parameters, so these arguments are useless (someday maybe).
3077 */
3078 fxdr_hyper(tl, &off);
3079 tl += 2;
3080 cnt = fxdr_unsigned(int, *tl);
3081 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3082 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3083 if (error) {
3084 nfsm_reply(2 * NFSX_UNSIGNED);
3085 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3086 return (0);
3087 }
3088 for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
3089 error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
3090 aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
3091 vput(vp);
3092 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3093 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3094 if (!error) {
3095 nfsm_build(tl, u_int32_t *, NFSX_V3WRITEVERF);
3096 *tl++ = txdr_unsigned(boottime.tv_sec);
3097 *tl = txdr_unsigned(boottime.tv_usec);
3098 } else
3099 return (0);
3100 nfsm_srvdone;
3101 }
3102
3103 /*
3104 * nfs statfs service
3105 */
3106 int
3107 nfsrv_statfs(nfsd, slp, procp, mrq)
3108 struct nfsrv_descript *nfsd;
3109 struct nfssvc_sock *slp;
3110 struct proc *procp;
3111 struct mbuf **mrq;
3112 {
3113 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3114 struct mbuf *nam = nfsd->nd_nam;
3115 caddr_t dpos = nfsd->nd_dpos;
3116 struct ucred *cred = &nfsd->nd_cr;
3117 register struct statfs *sf;
3118 register struct nfs_statfs *sfp;
3119 register u_int32_t *tl;
3120 register int32_t t1;
3121 caddr_t bpos;
3122 int error = 0, rdonly, cache, getret = 1;
3123 int v3 = (nfsd->nd_flag & ND_NFSV3);
3124 char *cp2;
3125 struct mbuf *mb, *mb2, *mreq;
3126 struct vnode *vp;
3127 struct vattr at;
3128 nfsfh_t nfh;
3129 fhandle_t *fhp;
3130 struct statfs statfs;
3131 u_quad_t frev, tval;
3132
3133 #ifndef nolint
3134 cache = 0;
3135 #endif
3136 fhp = &nfh.fh_generic;
3137 nfsm_srvmtofh(fhp);
3138 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3139 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3140 if (error) {
3141 nfsm_reply(NFSX_UNSIGNED);
3142 nfsm_srvpostop_attr(getret, &at);
3143 return (0);
3144 }
3145 sf = &statfs;
3146 error = VFS_STATFS(vp->v_mount, sf, procp);
3147 getret = VOP_GETATTR(vp, &at, cred, procp);
3148 vput(vp);
3149 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3150 if (v3)
3151 nfsm_srvpostop_attr(getret, &at);
3152 if (error)
3153 return (0);
3154 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3155 if (v3) {
3156 tval = (u_quad_t)sf->f_blocks;
3157 tval *= (u_quad_t)sf->f_bsize;
3158 txdr_hyper(&tval, &sfp->sf_tbytes);
3159 tval = (u_quad_t)sf->f_bfree;
3160 tval *= (u_quad_t)sf->f_bsize;
3161 txdr_hyper(&tval, &sfp->sf_fbytes);
3162 tval = (u_quad_t)sf->f_bavail;
3163 tval *= (u_quad_t)sf->f_bsize;
3164 txdr_hyper(&tval, &sfp->sf_abytes);
3165 sfp->sf_tfiles.nfsuquad[0] = 0;
3166 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3167 sfp->sf_ffiles.nfsuquad[0] = 0;
3168 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3169 sfp->sf_afiles.nfsuquad[0] = 0;
3170 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3171 sfp->sf_invarsec = 0;
3172 } else {
3173 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3174 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3175 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3176 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3177 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3178 }
3179 nfsm_srvdone;
3180 }
3181
3182 /*
3183 * nfs fsinfo service
3184 */
3185 int
3186 nfsrv_fsinfo(nfsd, slp, procp, mrq)
3187 struct nfsrv_descript *nfsd;
3188 struct nfssvc_sock *slp;
3189 struct proc *procp;
3190 struct mbuf **mrq;
3191 {
3192 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3193 struct mbuf *nam = nfsd->nd_nam;
3194 caddr_t dpos = nfsd->nd_dpos;
3195 struct ucred *cred = &nfsd->nd_cr;
3196 register u_int32_t *tl;
3197 register struct nfsv3_fsinfo *sip;
3198 register int32_t t1;
3199 caddr_t bpos;
3200 int error = 0, rdonly, cache, getret = 1, pref;
3201 char *cp2;
3202 struct mbuf *mb, *mb2, *mreq;
3203 struct vnode *vp;
3204 struct vattr at;
3205 nfsfh_t nfh;
3206 fhandle_t *fhp;
3207 u_quad_t frev, maxfsize;
3208 struct statfs sb;
3209
3210 #ifndef nolint
3211 cache = 0;
3212 #endif
3213 fhp = &nfh.fh_generic;
3214 nfsm_srvmtofh(fhp);
3215 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3216 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3217 if (error) {
3218 nfsm_reply(NFSX_UNSIGNED);
3219 nfsm_srvpostop_attr(getret, &at);
3220 return (0);
3221 }
3222
3223 /* XXX Try to make a guess on the max file size. */
3224 VFS_STATFS(vp->v_mount, &sb, (struct proc *)0);
3225 maxfsize = (u_quad_t)0x80000000 * sb.f_bsize - 1;
3226
3227 getret = VOP_GETATTR(vp, &at, cred, procp);
3228 vput(vp);
3229 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3230 nfsm_srvpostop_attr(getret, &at);
3231 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3232
3233 /*
3234 * XXX
3235 * There should be file system VFS OP(s) to get this information.
3236 * For now, assume ufs.
3237 */
3238 if (slp->ns_so->so_type == SOCK_DGRAM)
3239 pref = NFS_MAXDGRAMDATA;
3240 else
3241 pref = NFS_MAXDATA;
3242 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3243 sip->fs_rtpref = txdr_unsigned(pref);
3244 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3245 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3246 sip->fs_wtpref = txdr_unsigned(pref);
3247 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3248 sip->fs_dtpref = txdr_unsigned(pref);
3249 txdr_hyper(&maxfsize, &sip->fs_maxfilesize);
3250 sip->fs_timedelta.nfsv3_sec = 0;
3251 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3252 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3253 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3254 NFSV3FSINFO_CANSETTIME);
3255 nfsm_srvdone;
3256 }
3257
3258 /*
3259 * nfs pathconf service
3260 */
3261 int
3262 nfsrv_pathconf(nfsd, slp, procp, mrq)
3263 struct nfsrv_descript *nfsd;
3264 struct nfssvc_sock *slp;
3265 struct proc *procp;
3266 struct mbuf **mrq;
3267 {
3268 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3269 struct mbuf *nam = nfsd->nd_nam;
3270 caddr_t dpos = nfsd->nd_dpos;
3271 struct ucred *cred = &nfsd->nd_cr;
3272 register u_int32_t *tl;
3273 register struct nfsv3_pathconf *pc;
3274 register int32_t t1;
3275 caddr_t bpos;
3276 int error = 0, rdonly, cache, getret = 1;
3277 register_t linkmax, namemax, chownres, notrunc;
3278 char *cp2;
3279 struct mbuf *mb, *mb2, *mreq;
3280 struct vnode *vp;
3281 struct vattr at;
3282 nfsfh_t nfh;
3283 fhandle_t *fhp;
3284 u_quad_t frev;
3285
3286 #ifndef nolint
3287 cache = 0;
3288 #endif
3289 fhp = &nfh.fh_generic;
3290 nfsm_srvmtofh(fhp);
3291 error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3292 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), FALSE);
3293 if (error) {
3294 nfsm_reply(NFSX_UNSIGNED);
3295 nfsm_srvpostop_attr(getret, &at);
3296 return (0);
3297 }
3298 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3299 if (!error)
3300 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3301 if (!error)
3302 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3303 if (!error)
3304 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, ¬runc);
3305 getret = VOP_GETATTR(vp, &at, cred, procp);
3306 vput(vp);
3307 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3308 nfsm_srvpostop_attr(getret, &at);
3309 if (error)
3310 return (0);
3311 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3312
3313 pc->pc_linkmax = txdr_unsigned(linkmax);
3314 pc->pc_namemax = txdr_unsigned(namemax);
3315 pc->pc_notrunc = txdr_unsigned(notrunc);
3316 pc->pc_chownrestricted = txdr_unsigned(chownres);
3317
3318 /*
3319 * These should probably be supported by VOP_PATHCONF(), but
3320 * until msdosfs is exportable (why would you want to?), the
3321 * Unix defaults should be ok.
3322 */
3323 pc->pc_caseinsensitive = nfs_false;
3324 pc->pc_casepreserving = nfs_true;
3325 nfsm_srvdone;
3326 }
3327
3328 /*
3329 * Null operation, used by clients to ping server
3330 */
3331 /* ARGSUSED */
3332 int
3333 nfsrv_null(nfsd, slp, procp, mrq)
3334 struct nfsrv_descript *nfsd;
3335 struct nfssvc_sock *slp;
3336 struct proc *procp;
3337 struct mbuf **mrq;
3338 {
3339 struct mbuf *mrep = nfsd->nd_mrep;
3340 caddr_t bpos;
3341 int error = NFSERR_RETVOID, cache = 0;
3342 struct mbuf *mb, *mreq;
3343 u_quad_t frev;
3344
3345 nfsm_reply(0);
3346 return (0);
3347 }
3348
3349 /*
3350 * No operation, used for obsolete procedures
3351 */
3352 /* ARGSUSED */
3353 int
3354 nfsrv_noop(nfsd, slp, procp, mrq)
3355 struct nfsrv_descript *nfsd;
3356 struct nfssvc_sock *slp;
3357 struct proc *procp;
3358 struct mbuf **mrq;
3359 {
3360 struct mbuf *mrep = nfsd->nd_mrep;
3361 caddr_t bpos;
3362 int error, cache = 0;
3363 struct mbuf *mb, *mreq;
3364 u_quad_t frev;
3365
3366 if (nfsd->nd_repstat)
3367 error = nfsd->nd_repstat;
3368 else
3369 error = EPROCUNAVAIL;
3370 nfsm_reply(0);
3371 return (0);
3372 }
3373
3374 /*
3375 * Perform access checking for vnodes obtained from file handles that would
3376 * refer to files already opened by a Unix client. You cannot just use
3377 * vn_writechk() and VOP_ACCESS() for two reasons.
3378 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3379 * 2 - The owner is to be given access irrespective of mode bits for some
3380 * operations, so that processes that chmod after opening a file don't
3381 * break. I don't like this because it opens a security hole, but since
3382 * the nfs server opens a security hole the size of a barn door anyhow,
3383 * what the heck.
3384 *
3385 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3386 * will return EPERM instead of EACCESS. EPERM is always an error.
3387 */
3388 int
3389 nfsrv_access(vp, flags, cred, rdonly, p, override)
3390 register struct vnode *vp;
3391 int flags;
3392 register struct ucred *cred;
3393 int rdonly;
3394 struct proc *p;
3395 {
3396 struct vattr vattr;
3397 int error;
3398 if (flags & VWRITE) {
3399 /* Just vn_writechk() changed to check rdonly */
3400 /*
3401 * Disallow write attempts on read-only file systems;
3402 * unless the file is a socket or a block or character
3403 * device resident on the file system.
3404 */
3405 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3406 switch (vp->v_type) {
3407 case VREG:
3408 case VDIR:
3409 case VLNK:
3410 return (EROFS);
3411 default:
3412 break;
3413 }
3414 }
3415 /*
3416 * If there's shared text associated with
3417 * the inode, try to free it up once. If
3418 * we fail, we can't allow writing.
3419 */
3420 if ((vp->v_flag & VTEXT) && !vnode_pager_uncache(vp))
3421 return (ETXTBSY);
3422 }
3423 error = VOP_GETATTR(vp, &vattr, cred, p);
3424 if (error)
3425 return (error);
3426 error = VOP_ACCESS(vp, flags, cred, p);
3427 /*
3428 * Allow certain operations for the owner (reads and writes
3429 * on files that are already open).
3430 */
3431 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3432 error = 0;
3433 return error;
3434 }
3435