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