]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_serv.c
xnu-344.23.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_serv.c
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
112 nfstype nfsv3_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
113 NFFIFO, NFNON };
114 #ifndef NFS_NOSERVER
115 nfstype nfsv2_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFNON,
116 NFCHR, NFNON };
117 /* Global vars */
118 extern u_long nfs_xdrneg1;
119 extern u_long nfs_false, nfs_true;
120 extern enum vtype nv3tov_type[8];
121 extern struct nfsstats nfsstats;
122
123 int nfsrvw_procrastinate = NFS_GATHERDELAY * 1000;
124 int nfsrvw_procrastinate_v3 = 0;
125
126 int nfs_async = 0;
127 #ifdef notyet
128 /* XXX CSM 11/25/97 Upgrade sysctl.h someday */
129 SYSCTL_INT(_vfs_nfs, OID_AUTO, async, CTLFLAG_RW, &nfs_async, 0, "");
130 #endif
131
132 static int nfsrv_access __P((struct vnode *,int,struct ucred *,int,
133 struct proc *, int));
134 static void nfsrvw_coalesce __P((struct nfsrv_descript *,
135 struct nfsrv_descript *));
136
137 /*
138 * nfs v3 access service
139 */
140 int
141 nfsrv3_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 */
207 int
208 nfsrv_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 */
253 int
254 nfsrv_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;
368 out:
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 */
384 int
385 nfsrv_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 */
511 int
512 nfsrv_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);
589 out:
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 */
614 int
615 nfsrv_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 */
784 int
785 nfsrv_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 */
977 int
978 nfsrv_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) {
1071 nfsmout:
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 */
1135 loop1:
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 */
1299 static void
1300 nfsrvw_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 */
1349 void
1350 nfsrvw_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 */
1370 void
1371 nfsrv_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 */
1389 int
1390 nfsrv_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);
1615 nfsmout:
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 */
1636 int
1637 nfsrv_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 }
1750 out:
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);
1769 nfsmout:
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 */
1790 int
1791 nfsrv_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 }
1846 out:
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 */
1877 int
1878 nfsrv_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;
2015 out:
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);
2039 out1:
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
2057 nfsmout:
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 */
2080 int
2081 nfsrv_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;
2145 out:
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 }
2159 out1:
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 */
2179 int
2180 nfsrv_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 }
2284 out:
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);
2300 nfsmout:
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 */
2322 int
2323 nfsrv_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 }
2404 out:
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);
2422 nfsmout:
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 */
2438 int
2439 nfsrv_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;
2504 out:
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 */
2558 struct 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
2567 int
2568 nfsrv_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);
2640 again:
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
2652 if (cookies) {
2653 _FREE((caddr_t)cookies, M_TEMP);
2654 cookies = NULL;
2655 }
2656 if (error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp)) {
2657 FREE((caddr_t)rbuf, M_TEMP);
2658 nfsm_reply(NFSX_POSTOPATTR(v3));
2659 nfsm_srvpostop_attr(getret, &at);
2660 return (0);
2661 }
2662 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2663 off = (off_t)io.uio_offset;
2664 /*
2665 * We cannot set the error in the case where there are no cookies
2666 * and no error, only, as FreeBSD. In the scenario the client is
2667 * calling us back being told there were "more" entries on last readdir
2668 * return, and we have no more entries, our VOP_READDIR can give
2669 * cookies = NULL and no error. This is due to a zero size to MALLOC
2670 * returning NULL unlike FreeBSD which returns a pointer.
2671 * With FreeBSD it makes sense if the MALLOC failed and you get in that
2672 * bind. For us, we need something more. Thus, we should make sure we
2673 * had some cookies to return, but no pointer and no error for EPERM case.
2674 * Otherwise, go thru normal processing of sending back the eofflag. This check
2675 * is also legit on first call to the routine by client since . and ..
2676 * should be returned. Make same change to nfsrv_readdirplus.
2677 */
2678 if ((ncookies != 0) && !cookies && !error)
2679 error = NFSERR_PERM;
2680
2681 if (v3) {
2682 getret = VOP_GETATTR(vp, &at, cred, procp);
2683 if (!error)
2684 error = getret;
2685 }
2686 VOP_UNLOCK(vp, 0, procp);
2687 if (error) {
2688 vrele(vp);
2689 _FREE((caddr_t)rbuf, M_TEMP);
2690 if (cookies)
2691 _FREE((caddr_t)cookies, M_TEMP);
2692 nfsm_reply(NFSX_POSTOPATTR(v3));
2693 nfsm_srvpostop_attr(getret, &at);
2694 return (0);
2695 }
2696 if (io.uio_resid) {
2697 siz -= io.uio_resid;
2698
2699 /*
2700 * If nothing read, return eof
2701 * rpc reply
2702 */
2703 if (siz == 0) {
2704 vrele(vp);
2705 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) +
2706 2 * NFSX_UNSIGNED);
2707 if (v3) {
2708 nfsm_srvpostop_attr(getret, &at);
2709 nfsm_build(tl, u_long *, 4 * NFSX_UNSIGNED);
2710 txdr_hyper(&at.va_filerev, tl);
2711 tl += 2;
2712 } else
2713 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
2714 *tl++ = nfs_false;
2715 *tl = nfs_true;
2716 FREE((caddr_t)rbuf, M_TEMP);
2717 FREE((caddr_t)cookies, M_TEMP);
2718 return (0);
2719 }
2720 }
2721
2722 /*
2723 * Check for degenerate cases of nothing useful read.
2724 * If so go try again
2725 */
2726 cpos = rbuf;
2727 cend = rbuf + siz;
2728 dp = (struct dirent *)cpos;
2729 cookiep = cookies;
2730 #ifdef __FreeBSD__
2731 /*
2732 * For some reason FreeBSD's ufs_readdir() chooses to back the
2733 * directory offset up to a block boundary, so it is necessary to
2734 * skip over the records that preceed the requested offset. This
2735 * requires the assumption that file offset cookies monotonically
2736 * increase.
2737 */
2738 while (cpos < cend && ncookies > 0 &&
2739 (dp->d_fileno == 0 || ((u_quad_t)(*cookiep)) <= toff)) {
2740 #else
2741 while (dp->d_fileno == 0 && cpos < cend && ncookies > 0) {
2742 #endif
2743 cpos += dp->d_reclen;
2744 dp = (struct dirent *)cpos;
2745 cookiep++;
2746 ncookies--;
2747 }
2748 if (cpos >= cend || ncookies == 0) {
2749 toff = off;
2750 siz = fullsiz;
2751 goto again;
2752 }
2753
2754 len = 3 * NFSX_UNSIGNED; /* paranoia, probably can be 0 */
2755 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_COOKIEVERF(v3) + siz);
2756 if (v3) {
2757 nfsm_srvpostop_attr(getret, &at);
2758 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
2759 txdr_hyper(&at.va_filerev, tl);
2760 }
2761 mp = mp2 = mb;
2762 bp = bpos;
2763 be = bp + M_TRAILINGSPACE(mp);
2764
2765 /* Loop through the records and build reply */
2766 while (cpos < cend && ncookies > 0) {
2767 if (dp->d_fileno != 0) {
2768 nlen = dp->d_namlen;
2769 rem = nfsm_rndup(nlen)-nlen;
2770 len += (4 * NFSX_UNSIGNED + nlen + rem);
2771 if (v3)
2772 len += 2 * NFSX_UNSIGNED;
2773 if (len > cnt) {
2774 eofflag = 0;
2775 break;
2776 }
2777 /*
2778 * Build the directory record xdr from
2779 * the dirent entry.
2780 */
2781 nfsm_clget;
2782 *tl = nfs_true;
2783 bp += NFSX_UNSIGNED;
2784 if (v3) {
2785 nfsm_clget;
2786 *tl = 0;
2787 bp += NFSX_UNSIGNED;
2788 }
2789 nfsm_clget;
2790 *tl = txdr_unsigned(dp->d_fileno);
2791 bp += NFSX_UNSIGNED;
2792 nfsm_clget;
2793 *tl = txdr_unsigned(nlen);
2794 bp += NFSX_UNSIGNED;
2795
2796 /* And loop around copying the name */
2797 xfer = nlen;
2798 cp = dp->d_name;
2799 while (xfer > 0) {
2800 nfsm_clget;
2801 if ((bp+xfer) > be)
2802 tsiz = be-bp;
2803 else
2804 tsiz = xfer;
2805 bcopy(cp, bp, tsiz);
2806 bp += tsiz;
2807 xfer -= tsiz;
2808 if (xfer > 0)
2809 cp += tsiz;
2810 }
2811 /* And null pad to a long boundary */
2812 for (i = 0; i < rem; i++)
2813 *bp++ = '\0';
2814 nfsm_clget;
2815
2816 /* Finish off the record */
2817 if (v3) {
2818 *tl = 0;
2819 bp += NFSX_UNSIGNED;
2820 nfsm_clget;
2821 }
2822 *tl = txdr_unsigned(*cookiep);
2823 bp += NFSX_UNSIGNED;
2824 }
2825 cpos += dp->d_reclen;
2826 dp = (struct dirent *)cpos;
2827 cookiep++;
2828 ncookies--;
2829 }
2830 vrele(vp);
2831 nfsm_clget;
2832 *tl = nfs_false;
2833 bp += NFSX_UNSIGNED;
2834 nfsm_clget;
2835 if (eofflag)
2836 *tl = nfs_true;
2837 else
2838 *tl = nfs_false;
2839 bp += NFSX_UNSIGNED;
2840 if (mp != mb) {
2841 if (bp < be)
2842 mp->m_len = bp - mtod(mp, caddr_t);
2843 } else
2844 mp->m_len += bp - bpos;
2845 FREE((caddr_t)rbuf, M_TEMP);
2846 FREE((caddr_t)cookies, M_TEMP);
2847 nfsm_srvdone;
2848 }
2849
2850 int
2851 nfsrv_readdirplus(nfsd, slp, procp, mrq)
2852 struct nfsrv_descript *nfsd;
2853 struct nfssvc_sock *slp;
2854 struct proc *procp;
2855 struct mbuf **mrq;
2856 {
2857 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
2858 struct mbuf *nam = nfsd->nd_nam;
2859 caddr_t dpos = nfsd->nd_dpos;
2860 struct ucred *cred = &nfsd->nd_cr;
2861 register char *bp, *be;
2862 register struct mbuf *mp;
2863 register struct dirent *dp;
2864 register caddr_t cp;
2865 register u_long *tl;
2866 register long t1;
2867 caddr_t bpos;
2868 struct mbuf *mb, *mb2, *mreq, *mp2;
2869 char *cpos, *cend, *cp2, *rbuf;
2870 struct vnode *vp, *nvp;
2871 struct flrep fl;
2872 nfsfh_t nfh;
2873 fhandle_t *fhp, *nfhp = (fhandle_t *)fl.fl_nfh;
2874 struct uio io;
2875 struct iovec iv;
2876 struct vattr va, at, *vap = &va;
2877 struct nfs_fattr *fp;
2878 int len, nlen, rem, xfer, tsiz, i, error = 0, getret = 1;
2879 int siz, cnt, fullsiz, eofflag, rdonly, cache, dirlen, ncookies = 0;
2880 u_quad_t frev, off, toff, verf;
2881 u_long *cookies = NULL, *cookiep;
2882 void *file;
2883
2884 fhp = &nfh.fh_generic;
2885 nfsm_srvmtofh(fhp);
2886 nfsm_dissect(tl, u_long *, 6 * NFSX_UNSIGNED);
2887 fxdr_hyper(tl, &toff);
2888 tl += 2;
2889 fxdr_hyper(tl, &verf);
2890 tl += 2;
2891 siz = fxdr_unsigned(int, *tl++);
2892 cnt = fxdr_unsigned(int, *tl);
2893 off = toff;
2894 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2895 xfer = NFS_SRVMAXDATA(nfsd);
2896 if (siz > xfer)
2897 siz = xfer;
2898 fullsiz = siz;
2899 if ((error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
2900 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE))) {
2901 nfsm_reply(NFSX_UNSIGNED);
2902 nfsm_srvpostop_attr(getret, &at);
2903 return (0);
2904 }
2905 error = getret = VOP_GETATTR(vp, &at, cred, procp);
2906 if (!error && toff && verf && verf != at.va_filerev)
2907 error = NFSERR_BAD_COOKIE;
2908 if (!error) {
2909 nqsrv_getl(vp, ND_READ);
2910 error = nfsrv_access(vp, VEXEC, cred, rdonly, procp, 0);
2911 }
2912 if (error) {
2913 vput(vp);
2914 nfsm_reply(NFSX_V3POSTOPATTR);
2915 nfsm_srvpostop_attr(getret, &at);
2916 return (0);
2917 }
2918 VOP_UNLOCK(vp, 0, procp);
2919 MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
2920 again:
2921 iv.iov_base = rbuf;
2922 iv.iov_len = fullsiz;
2923 io.uio_iov = &iv;
2924 io.uio_iovcnt = 1;
2925 io.uio_offset = (off_t)off;
2926 io.uio_resid = fullsiz;
2927 io.uio_segflg = UIO_SYSSPACE;
2928 io.uio_rw = UIO_READ;
2929 io.uio_procp = (struct proc *)0;
2930 eofflag = 0;
2931 if (cookies) {
2932 _FREE((caddr_t)cookies, M_TEMP);
2933 cookies = NULL;
2934 }
2935 if (error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, procp)) {
2936 FREE((caddr_t)rbuf, M_TEMP);
2937 nfsm_reply(NFSX_V3POSTOPATTR);
2938 nfsm_srvpostop_attr(getret, &at);
2939 return (0);
2940 }
2941 error = VOP_READDIR(vp, &io, cred, &eofflag, &ncookies, &cookies);
2942 off = (u_quad_t)io.uio_offset;
2943 getret = VOP_GETATTR(vp, &at, cred, procp);
2944 VOP_UNLOCK(vp, 0, procp);
2945 /*
2946 * See nfsrv_readdir comment above on this
2947 */
2948 if ((ncookies != 0) && !cookies && !error)
2949 error = NFSERR_PERM;
2950
2951 if (!error)
2952 error = getret;
2953 if (error) {
2954 vrele(vp);
2955 if (cookies)
2956 _FREE((caddr_t)cookies, M_TEMP);
2957 _FREE((caddr_t)rbuf, M_TEMP);
2958 nfsm_reply(NFSX_V3POSTOPATTR);
2959 nfsm_srvpostop_attr(getret, &at);
2960 return (0);
2961 }
2962 if (io.uio_resid) {
2963 siz -= io.uio_resid;
2964
2965 /*
2966 * If nothing read, return eof
2967 * rpc reply
2968 */
2969 if (siz == 0) {
2970 vrele(vp);
2971 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF +
2972 2 * NFSX_UNSIGNED);
2973 nfsm_srvpostop_attr(getret, &at);
2974 nfsm_build(tl, u_long *, 4 * NFSX_UNSIGNED);
2975 txdr_hyper(&at.va_filerev, tl);
2976 tl += 2;
2977 *tl++ = nfs_false;
2978 *tl = nfs_true;
2979 FREE((caddr_t)cookies, M_TEMP);
2980 FREE((caddr_t)rbuf, M_TEMP);
2981 return (0);
2982 }
2983 }
2984
2985 /*
2986 * Check for degenerate cases of nothing useful read.
2987 * If so go try again
2988 */
2989 cpos = rbuf;
2990 cend = rbuf + siz;
2991 dp = (struct dirent *)cpos;
2992 cookiep = cookies;
2993 #ifdef __FreeBSD__
2994 /*
2995 * For some reason FreeBSD's ufs_readdir() chooses to back the
2996 * directory offset up to a block boundary, so it is necessary to
2997 * skip over the records that preceed the requested offset. This
2998 * requires the assumption that file offset cookies monotonically
2999 * increase.
3000 */
3001 while (cpos < cend && ncookies > 0 &&
3002 (dp->d_fileno == 0 || ((u_quad_t)(*cookiep)) <= toff)) {
3003 #else
3004 while (dp->d_fileno == 0 && cpos < cend && ncookies > 0) {
3005 #endif
3006 cpos += dp->d_reclen;
3007 dp = (struct dirent *)cpos;
3008 cookiep++;
3009 ncookies--;
3010 }
3011 if (cpos >= cend || ncookies == 0) {
3012 toff = off;
3013 siz = fullsiz;
3014 goto again;
3015 }
3016
3017 /*
3018 * Probe one of the directory entries to see if the filesystem
3019 * supports VGET. See later comment for VFS_VGET changes.
3020 */
3021 if (vp->v_tag == VT_UFS)
3022 file = (void *) dp->d_fileno;
3023 else {
3024 file = &dp->d_fileno;
3025 }
3026
3027 if (error = VFS_VGET(vp->v_mount, file, &nvp)) {
3028 if (error == EOPNOTSUPP) /* let others get passed back */
3029 error = NFSERR_NOTSUPP;
3030 vrele(vp);
3031 _FREE((caddr_t)cookies, M_TEMP);
3032 _FREE((caddr_t)rbuf, M_TEMP);
3033 nfsm_reply(NFSX_V3POSTOPATTR);
3034 nfsm_srvpostop_attr(getret, &at);
3035 return (0);
3036 }
3037 vput(nvp);
3038
3039 dirlen = len = NFSX_V3POSTOPATTR + NFSX_V3COOKIEVERF + 2 * NFSX_UNSIGNED;
3040 nfsm_reply(cnt);
3041 nfsm_srvpostop_attr(getret, &at);
3042 nfsm_build(tl, u_long *, 2 * NFSX_UNSIGNED);
3043 txdr_hyper(&at.va_filerev, tl);
3044 mp = mp2 = mb;
3045 bp = bpos;
3046 be = bp + M_TRAILINGSPACE(mp);
3047
3048 /* Loop through the records and build reply */
3049 while (cpos < cend && ncookies > 0) {
3050 if (dp->d_fileno != 0) {
3051 nlen = dp->d_namlen;
3052 rem = nfsm_rndup(nlen)-nlen;
3053
3054 /*
3055 * Got to get the vnode for lookup per entry.
3056 * HFS+/volfs and others use address of file identifier to VGET
3057 * UFS, nullfs, umapfs use inode (u_int32_t)
3058 * until they are consistent, we must differentiate now.
3059 * UFS is the only one of the latter class that is exported.
3060 * Note this will be pulled out as we resolve the VGET issue
3061 * of which it should use u_in32_t or addresses.
3062 */
3063
3064 if (vp->v_tag == VT_UFS)
3065 file = (void *) dp->d_fileno;
3066 else
3067 file = &dp->d_fileno;
3068
3069 if (VFS_VGET(vp->v_mount, file, &nvp))
3070 goto invalid;
3071 bzero((caddr_t)nfhp, NFSX_V3FH);
3072 nfhp->fh_fsid =
3073 nvp->v_mount->mnt_stat.f_fsid;
3074 if (VFS_VPTOFH(nvp, &nfhp->fh_fid)) {
3075 vput(nvp);
3076 goto invalid;
3077 }
3078 if (VOP_GETATTR(nvp, vap, cred, procp)) {
3079 vput(nvp);
3080 goto invalid;
3081 }
3082 vput(nvp);
3083
3084 /*
3085 * If either the dircount or maxcount will be
3086 * exceeded, get out now. Both of these lengths
3087 * are calculated conservatively, including all
3088 * XDR overheads.
3089 */
3090 len += (7 * NFSX_UNSIGNED + nlen + rem + NFSX_V3FH +
3091 NFSX_V3POSTOPATTR);
3092 dirlen += (6 * NFSX_UNSIGNED + nlen + rem);
3093 if (len > cnt || dirlen > fullsiz) {
3094 eofflag = 0;
3095 break;
3096 }
3097
3098 /*
3099 * Build the directory record xdr from
3100 * the dirent entry.
3101 */
3102 fp = (struct nfs_fattr *)&fl.fl_fattr;
3103 nfsm_srvfillattr(vap, fp);
3104 fl.fl_fhsize = txdr_unsigned(NFSX_V3FH);
3105 fl.fl_fhok = nfs_true;
3106 fl.fl_postopok = nfs_true;
3107 fl.fl_off.nfsuquad[0] = 0;
3108 fl.fl_off.nfsuquad[1] = txdr_unsigned(*cookiep);
3109
3110 nfsm_clget;
3111 *tl = nfs_true;
3112 bp += NFSX_UNSIGNED;
3113 nfsm_clget;
3114 *tl = 0;
3115 bp += NFSX_UNSIGNED;
3116 nfsm_clget;
3117 *tl = txdr_unsigned(dp->d_fileno);
3118 bp += NFSX_UNSIGNED;
3119 nfsm_clget;
3120 *tl = txdr_unsigned(nlen);
3121 bp += NFSX_UNSIGNED;
3122
3123 /* And loop around copying the name */
3124 xfer = nlen;
3125 cp = dp->d_name;
3126 while (xfer > 0) {
3127 nfsm_clget;
3128 if ((bp + xfer) > be)
3129 tsiz = be - bp;
3130 else
3131 tsiz = xfer;
3132 bcopy(cp, bp, tsiz);
3133 bp += tsiz;
3134 xfer -= tsiz;
3135 if (xfer > 0)
3136 cp += tsiz;
3137 }
3138 /* And null pad to a long boundary */
3139 for (i = 0; i < rem; i++)
3140 *bp++ = '\0';
3141
3142 /*
3143 * Now copy the flrep structure out.
3144 */
3145 xfer = sizeof (struct flrep);
3146 cp = (caddr_t)&fl;
3147 while (xfer > 0) {
3148 nfsm_clget;
3149 if ((bp + xfer) > be)
3150 tsiz = be - bp;
3151 else
3152 tsiz = xfer;
3153 bcopy(cp, bp, tsiz);
3154 bp += tsiz;
3155 xfer -= tsiz;
3156 if (xfer > 0)
3157 cp += tsiz;
3158 }
3159 }
3160 invalid:
3161 cpos += dp->d_reclen;
3162 dp = (struct dirent *)cpos;
3163 cookiep++;
3164 ncookies--;
3165 }
3166 vrele(vp);
3167 nfsm_clget;
3168 *tl = nfs_false;
3169 bp += NFSX_UNSIGNED;
3170 nfsm_clget;
3171 if (eofflag)
3172 *tl = nfs_true;
3173 else
3174 *tl = nfs_false;
3175 bp += NFSX_UNSIGNED;
3176 if (mp != mb) {
3177 if (bp < be)
3178 mp->m_len = bp - mtod(mp, caddr_t);
3179 } else
3180 mp->m_len += bp - bpos;
3181 FREE((caddr_t)cookies, M_TEMP);
3182 FREE((caddr_t)rbuf, M_TEMP);
3183 nfsm_srvdone;
3184 }
3185
3186 /*
3187 * nfs commit service
3188 */
3189 int
3190 nfsrv_commit(nfsd, slp, procp, mrq)
3191 struct nfsrv_descript *nfsd;
3192 struct nfssvc_sock *slp;
3193 struct proc *procp;
3194 struct mbuf **mrq;
3195 {
3196 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3197 struct mbuf *nam = nfsd->nd_nam;
3198 caddr_t dpos = nfsd->nd_dpos;
3199 struct ucred *cred = &nfsd->nd_cr;
3200 struct vattr bfor, aft;
3201 struct vnode *vp;
3202 nfsfh_t nfh;
3203 fhandle_t *fhp;
3204 register u_long *tl;
3205 register long t1;
3206 caddr_t bpos;
3207 int error = 0, rdonly, for_ret = 1, aft_ret = 1, cnt, cache;
3208 char *cp2;
3209 struct mbuf *mb, *mb2, *mreq;
3210 u_quad_t frev, off;
3211
3212 #ifndef nolint
3213 cache = 0;
3214 #endif
3215 fhp = &nfh.fh_generic;
3216 nfsm_srvmtofh(fhp);
3217 nfsm_dissect(tl, u_long *, 3 * NFSX_UNSIGNED);
3218
3219 /*
3220 * XXX At this time VOP_FSYNC() does not accept offset and byte
3221 * count parameters, so these arguments are useless (someday maybe).
3222 */
3223 fxdr_hyper(tl, &off);
3224 tl += 2;
3225 cnt = fxdr_unsigned(int, *tl);
3226 if ((error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3227 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE))) {
3228 nfsm_reply(2 * NFSX_UNSIGNED);
3229 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3230 return (0);
3231 }
3232 for_ret = VOP_GETATTR(vp, &bfor, cred, procp);
3233 error = VOP_FSYNC(vp, cred, MNT_WAIT, procp);
3234 aft_ret = VOP_GETATTR(vp, &aft, cred, procp);
3235 vput(vp);
3236 nfsm_reply(NFSX_V3WCCDATA + NFSX_V3WRITEVERF);
3237 nfsm_srvwcc_data(for_ret, &bfor, aft_ret, &aft);
3238 if (!error) {
3239 nfsm_build(tl, u_long *, NFSX_V3WRITEVERF);
3240 *tl++ = txdr_unsigned(boottime.tv_sec);
3241 *tl = txdr_unsigned(boottime.tv_usec);
3242 } else
3243 return (0);
3244 nfsm_srvdone;
3245 }
3246
3247 /*
3248 * nfs statfs service
3249 */
3250 int
3251 nfsrv_statfs(nfsd, slp, procp, mrq)
3252 struct nfsrv_descript *nfsd;
3253 struct nfssvc_sock *slp;
3254 struct proc *procp;
3255 struct mbuf **mrq;
3256 {
3257 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3258 struct mbuf *nam = nfsd->nd_nam;
3259 caddr_t dpos = nfsd->nd_dpos;
3260 struct ucred *cred = &nfsd->nd_cr;
3261 register struct statfs *sf;
3262 register struct nfs_statfs *sfp;
3263 register u_long *tl;
3264 register long t1;
3265 caddr_t bpos;
3266 int error = 0, rdonly, cache, getret = 1;
3267 int v3 = (nfsd->nd_flag & ND_NFSV3);
3268 char *cp2;
3269 struct mbuf *mb, *mb2, *mreq;
3270 struct vnode *vp;
3271 struct vattr at;
3272 nfsfh_t nfh;
3273 fhandle_t *fhp;
3274 struct statfs statfs;
3275 u_quad_t frev, tval;
3276
3277 #ifndef nolint
3278 cache = 0;
3279 #endif
3280 fhp = &nfh.fh_generic;
3281 nfsm_srvmtofh(fhp);
3282 if ((error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3283 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE))) {
3284 nfsm_reply(NFSX_UNSIGNED);
3285 nfsm_srvpostop_attr(getret, &at);
3286 return (0);
3287 }
3288 sf = &statfs;
3289 error = VFS_STATFS(vp->v_mount, sf, procp);
3290 getret = VOP_GETATTR(vp, &at, cred, procp);
3291 vput(vp);
3292 nfsm_reply(NFSX_POSTOPATTR(v3) + NFSX_STATFS(v3));
3293 if (v3)
3294 nfsm_srvpostop_attr(getret, &at);
3295 if (error)
3296 return (0);
3297 nfsm_build(sfp, struct nfs_statfs *, NFSX_STATFS(v3));
3298 if (v3) {
3299 tval = (u_quad_t)sf->f_blocks;
3300 tval *= (u_quad_t)sf->f_bsize;
3301 txdr_hyper(&tval, &sfp->sf_tbytes);
3302 tval = (u_quad_t)sf->f_bfree;
3303 tval *= (u_quad_t)sf->f_bsize;
3304 txdr_hyper(&tval, &sfp->sf_fbytes);
3305 tval = (u_quad_t)sf->f_bavail;
3306 tval *= (u_quad_t)sf->f_bsize;
3307 txdr_hyper(&tval, &sfp->sf_abytes);
3308 sfp->sf_tfiles.nfsuquad[0] = 0;
3309 sfp->sf_tfiles.nfsuquad[1] = txdr_unsigned(sf->f_files);
3310 sfp->sf_ffiles.nfsuquad[0] = 0;
3311 sfp->sf_ffiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3312 sfp->sf_afiles.nfsuquad[0] = 0;
3313 sfp->sf_afiles.nfsuquad[1] = txdr_unsigned(sf->f_ffree);
3314 sfp->sf_invarsec = 0;
3315 } else {
3316 sfp->sf_tsize = txdr_unsigned(NFS_MAXDGRAMDATA);
3317 sfp->sf_bsize = txdr_unsigned(sf->f_bsize);
3318 sfp->sf_blocks = txdr_unsigned(sf->f_blocks);
3319 sfp->sf_bfree = txdr_unsigned(sf->f_bfree);
3320 sfp->sf_bavail = txdr_unsigned(sf->f_bavail);
3321 }
3322 nfsm_srvdone;
3323 }
3324
3325 /*
3326 * nfs fsinfo service
3327 */
3328 int
3329 nfsrv_fsinfo(nfsd, slp, procp, mrq)
3330 struct nfsrv_descript *nfsd;
3331 struct nfssvc_sock *slp;
3332 struct proc *procp;
3333 struct mbuf **mrq;
3334 {
3335 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3336 struct mbuf *nam = nfsd->nd_nam;
3337 caddr_t dpos = nfsd->nd_dpos;
3338 struct ucred *cred = &nfsd->nd_cr;
3339 register u_long *tl;
3340 register struct nfsv3_fsinfo *sip;
3341 register long t1;
3342 caddr_t bpos;
3343 int error = 0, rdonly, cache, getret = 1, pref;
3344 char *cp2;
3345 struct mbuf *mb, *mb2, *mreq;
3346 struct vnode *vp;
3347 struct vattr at;
3348 nfsfh_t nfh;
3349 fhandle_t *fhp;
3350 u_quad_t frev;
3351
3352 #ifndef nolint
3353 cache = 0;
3354 #endif
3355 fhp = &nfh.fh_generic;
3356 nfsm_srvmtofh(fhp);
3357 if ((error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3358 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE))) {
3359 nfsm_reply(NFSX_UNSIGNED);
3360 nfsm_srvpostop_attr(getret, &at);
3361 return (0);
3362 }
3363 getret = VOP_GETATTR(vp, &at, cred, procp);
3364 vput(vp);
3365 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3FSINFO);
3366 nfsm_srvpostop_attr(getret, &at);
3367 nfsm_build(sip, struct nfsv3_fsinfo *, NFSX_V3FSINFO);
3368
3369 /*
3370 * XXX
3371 * There should be file system VFS OP(s) to get this information.
3372 * For now, assume ufs.
3373 */
3374 if (slp->ns_so->so_type == SOCK_DGRAM)
3375 pref = NFS_MAXDGRAMDATA;
3376 else
3377 pref = NFS_MAXDATA;
3378 sip->fs_rtmax = txdr_unsigned(NFS_MAXDATA);
3379 sip->fs_rtpref = txdr_unsigned(pref);
3380 sip->fs_rtmult = txdr_unsigned(NFS_FABLKSIZE);
3381 sip->fs_wtmax = txdr_unsigned(NFS_MAXDATA);
3382 sip->fs_wtpref = txdr_unsigned(pref);
3383 sip->fs_wtmult = txdr_unsigned(NFS_FABLKSIZE);
3384 sip->fs_dtpref = txdr_unsigned(pref);
3385 sip->fs_maxfilesize.nfsuquad[0] = 0xffffffff;
3386 sip->fs_maxfilesize.nfsuquad[1] = 0xffffffff;
3387 sip->fs_timedelta.nfsv3_sec = 0;
3388 sip->fs_timedelta.nfsv3_nsec = txdr_unsigned(1);
3389 sip->fs_properties = txdr_unsigned(NFSV3FSINFO_LINK |
3390 NFSV3FSINFO_SYMLINK | NFSV3FSINFO_HOMOGENEOUS |
3391 NFSV3FSINFO_CANSETTIME);
3392 nfsm_srvdone;
3393 }
3394
3395 /*
3396 * nfs pathconf service
3397 */
3398 int
3399 nfsrv_pathconf(nfsd, slp, procp, mrq)
3400 struct nfsrv_descript *nfsd;
3401 struct nfssvc_sock *slp;
3402 struct proc *procp;
3403 struct mbuf **mrq;
3404 {
3405 struct mbuf *mrep = nfsd->nd_mrep, *md = nfsd->nd_md;
3406 struct mbuf *nam = nfsd->nd_nam;
3407 caddr_t dpos = nfsd->nd_dpos;
3408 struct ucred *cred = &nfsd->nd_cr;
3409 register u_long *tl;
3410 register struct nfsv3_pathconf *pc;
3411 register long t1;
3412 caddr_t bpos;
3413 int error = 0, rdonly, cache, getret = 1, linkmax, namemax;
3414 int chownres, notrunc;
3415 char *cp2;
3416 struct mbuf *mb, *mb2, *mreq;
3417 struct vnode *vp;
3418 struct vattr at;
3419 nfsfh_t nfh;
3420 fhandle_t *fhp;
3421 u_quad_t frev;
3422
3423 #ifndef nolint
3424 cache = 0;
3425 #endif
3426 fhp = &nfh.fh_generic;
3427 nfsm_srvmtofh(fhp);
3428 if ((error = nfsrv_fhtovp(fhp, 1, &vp, cred, slp, nam,
3429 &rdonly, (nfsd->nd_flag & ND_KERBAUTH), TRUE))) {
3430 nfsm_reply(NFSX_UNSIGNED);
3431 nfsm_srvpostop_attr(getret, &at);
3432 return (0);
3433 }
3434 error = VOP_PATHCONF(vp, _PC_LINK_MAX, &linkmax);
3435 if (!error)
3436 error = VOP_PATHCONF(vp, _PC_NAME_MAX, &namemax);
3437 if (!error)
3438 error = VOP_PATHCONF(vp, _PC_CHOWN_RESTRICTED, &chownres);
3439 if (!error)
3440 error = VOP_PATHCONF(vp, _PC_NO_TRUNC, &notrunc);
3441 getret = VOP_GETATTR(vp, &at, cred, procp);
3442 vput(vp);
3443 nfsm_reply(NFSX_V3POSTOPATTR + NFSX_V3PATHCONF);
3444 nfsm_srvpostop_attr(getret, &at);
3445 if (error)
3446 return (0);
3447 nfsm_build(pc, struct nfsv3_pathconf *, NFSX_V3PATHCONF);
3448
3449 pc->pc_linkmax = txdr_unsigned(linkmax);
3450 pc->pc_namemax = txdr_unsigned(namemax);
3451 pc->pc_notrunc = txdr_unsigned(notrunc);
3452 pc->pc_chownrestricted = txdr_unsigned(chownres);
3453
3454 /*
3455 * These should probably be supported by VOP_PATHCONF(), but
3456 * until msdosfs is exportable (why would you want to?), the
3457 * Unix defaults should be ok.
3458 */
3459 pc->pc_caseinsensitive = nfs_false;
3460 pc->pc_casepreserving = nfs_true;
3461 nfsm_srvdone;
3462 }
3463
3464 /*
3465 * Null operation, used by clients to ping server
3466 */
3467 /* ARGSUSED */
3468 int
3469 nfsrv_null(nfsd, slp, procp, mrq)
3470 struct nfsrv_descript *nfsd;
3471 struct nfssvc_sock *slp;
3472 struct proc *procp;
3473 struct mbuf **mrq;
3474 {
3475 struct mbuf *mrep = nfsd->nd_mrep;
3476 caddr_t bpos;
3477 int error = NFSERR_RETVOID, cache;
3478 struct mbuf *mb, *mreq;
3479 u_quad_t frev;
3480
3481 #ifndef nolint
3482 cache = 0;
3483 #endif
3484 nfsm_reply(0);
3485 return (0);
3486 }
3487
3488 /*
3489 * No operation, used for obsolete procedures
3490 */
3491 /* ARGSUSED */
3492 int
3493 nfsrv_noop(nfsd, slp, procp, mrq)
3494 struct nfsrv_descript *nfsd;
3495 struct nfssvc_sock *slp;
3496 struct proc *procp;
3497 struct mbuf **mrq;
3498 {
3499 struct mbuf *mrep = nfsd->nd_mrep;
3500 caddr_t bpos;
3501 int error, cache;
3502 struct mbuf *mb, *mreq;
3503 u_quad_t frev;
3504
3505 #ifndef nolint
3506 cache = 0;
3507 #endif
3508 if (nfsd->nd_repstat)
3509 error = nfsd->nd_repstat;
3510 else
3511 error = EPROCUNAVAIL;
3512 nfsm_reply(0);
3513 return (0);
3514 }
3515
3516 /*
3517 * Perform access checking for vnodes obtained from file handles that would
3518 * refer to files already opened by a Unix client. You cannot just use
3519 * vn_writechk() and VOP_ACCESS() for two reasons.
3520 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write case
3521 * 2 - The owner is to be given access irrespective of mode bits so that
3522 * processes that chmod after opening a file don't break. I don't like
3523 * this because it opens a security hole, but since the nfs server opens
3524 * a security hole the size of a barn door anyhow, what the heck.
3525
3526 * The exception to rule 2 is EPERM. If a file is IMMUTABLE, VOP_ACCESS()
3527 * will return EPERM instead of EACCESS. EPERM is always an error.
3528 */
3529
3530 static int
3531 nfsrv_access(vp, flags, cred, rdonly, p, override)
3532 register struct vnode *vp;
3533 int flags;
3534 register struct ucred *cred;
3535 int rdonly;
3536 struct proc *p;
3537 int override;
3538 {
3539 struct vattr vattr;
3540 int error;
3541 if (flags & VWRITE) {
3542 /* Just vn_writechk() changed to check rdonly */
3543 /*
3544 * Disallow write attempts on read-only file systems;
3545 * unless the file is a socket or a block or character
3546 * device resident on the file system.
3547 */
3548 if (rdonly || (vp->v_mount->mnt_flag & MNT_RDONLY)) {
3549 switch (vp->v_type) {
3550 case VREG: case VDIR: case VLNK: case VCPLX:
3551 return (EROFS);
3552 }
3553 }
3554 /*
3555 * If there's shared text associated with
3556 * the inode, we can't allow writing.
3557 */
3558 if (vp->v_flag & VTEXT)
3559 return (ETXTBSY);
3560 }
3561 if ((error = VOP_GETATTR(vp, &vattr, cred, p)))
3562 return (error);
3563 error = VOP_ACCESS(vp, flags, cred, p);
3564 /*
3565 * Allow certain operations for the owner (reads and writes
3566 * on files that are already open). Picking up from FreeBSD.
3567 */
3568 if (override && error == EACCES && cred->cr_uid == vattr.va_uid)
3569 error = 0;
3570 return error;
3571 }
3572 #endif /* NFS_NOSERVER */
3573