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