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