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