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