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