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