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