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