]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/fs/nfsserver/nfs_nfsdport.c
MFC: r216691
[FreeBSD/releng/8.2.git] / sys / fs / nfsserver / nfs_nfsdport.c
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * Functions that perform the vfs operations required by the routines in
39  * nfsd_serv.c. It is hoped that this change will make the server more
40  * portable.
41  */
42
43 #include <fs/nfs/nfsport.h>
44 #include <sys/hash.h>
45 #include <sys/sysctl.h>
46 #include <nlm/nlm_prot.h>
47 #include <nlm/nlm.h>
48
49 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
50 extern int nfsv4root_set;
51 extern int nfsrv_useacl;
52 extern int newnfs_numnfsd;
53 extern struct mount nfsv4root_mnt;
54 extern struct nfsrv_stablefirst nfsrv_stablefirst;
55 extern void (*nfsd_call_servertimer)(void);
56 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
57 NFSDLOCKMUTEX;
58 struct mtx nfs_cache_mutex;
59 struct mtx nfs_v4root_mutex;
60 struct nfsrvfh nfs_rootfh, nfs_pubfh;
61 int nfs_pubfhset = 0, nfs_rootfhset = 0;
62 static uint32_t nfsv4_sysid = 0;
63
64 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
65     struct ucred *);
66
67 static int enable_crossmntpt = 1;
68 static int nfs_commit_blks;
69 static int nfs_commit_miss;
70 extern int nfsrv_issuedelegs;
71 extern int nfsrv_dolocallocks;
72
73 SYSCTL_DECL(_vfs_newnfs);
74 SYSCTL_INT(_vfs_newnfs, OID_AUTO, mirrormnt, CTLFLAG_RW, &enable_crossmntpt,
75     0, "Enable nfsd to cross mount points");
76 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
77     0, "");
78 SYSCTL_INT(_vfs_newnfs, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
79     0, "");
80 SYSCTL_INT(_vfs_newnfs, OID_AUTO, issue_delegations, CTLFLAG_RW,
81     &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
82 SYSCTL_INT(_vfs_newnfs, OID_AUTO, enable_locallocks, CTLFLAG_RW,
83     &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
84
85 #define NUM_HEURISTIC           1017
86 #define NHUSE_INIT              64
87 #define NHUSE_INC               16
88 #define NHUSE_MAX               2048
89
90 static struct nfsheur {
91         struct vnode *nh_vp;    /* vp to match (unreferenced pointer) */
92         off_t nh_nextr;         /* next offset for sequential detection */
93         int nh_use;             /* use count for selection */
94         int nh_seqcount;        /* heuristic */
95 } nfsheur[NUM_HEURISTIC];
96
97
98 /*
99  * Get attributes into nfsvattr structure.
100  */
101 int
102 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
103     struct thread *p)
104 {
105         int error, lockedit = 0;
106
107         /* Since FreeBSD insists the vnode be locked... */
108         if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
109                 lockedit = 1;
110                 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
111         }
112         error = VOP_GETATTR(vp, &nvap->na_vattr, cred);
113         if (lockedit)
114                 NFSVOPUNLOCK(vp, 0, p);
115         return (error);
116 }
117
118 /*
119  * Get a file handle for a vnode.
120  */
121 int
122 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
123 {
124         int error;
125
126         NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
127         fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
128         error = VOP_VPTOFH(vp, &fhp->fh_fid);
129         return (error);
130 }
131
132 /*
133  * Perform access checking for vnodes obtained from file handles that would
134  * refer to files already opened by a Unix client. You cannot just use
135  * vn_writechk() and VOP_ACCESSX() for two reasons.
136  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
137  *     case.
138  * 2 - The owner is to be given access irrespective of mode bits for some
139  *     operations, so that processes that chmod after opening a file don't
140  *     break.
141  */
142 int
143 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
144     struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
145     u_int32_t *supportedtypep)
146 {
147         struct vattr vattr;
148         int error = 0, getret = 0;
149
150         if (accmode & VWRITE) {
151                 /* Just vn_writechk() changed to check rdonly */
152                 /*
153                  * Disallow write attempts on read-only file systems;
154                  * unless the file is a socket or a block or character
155                  * device resident on the file system.
156                  */
157                 if (NFSVNO_EXRDONLY(exp) ||
158                     (vp->v_mount->mnt_flag & MNT_RDONLY)) {
159                         switch (vp->v_type) {
160                         case VREG:
161                         case VDIR:
162                         case VLNK:
163                                 return (EROFS);
164                         default:
165                                 break;
166                         }
167                 }
168                 /*
169                  * If there's shared text associated with
170                  * the inode, try to free it up once.  If
171                  * we fail, we can't allow writing.
172                  */
173                 if (vp->v_vflag & VV_TEXT)
174                         return (ETXTBSY);
175         }
176         if (vpislocked == 0)
177                 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
178
179         /*
180          * Should the override still be applied when ACLs are enabled?
181          */
182         error = VOP_ACCESSX(vp, accmode, cred, p);
183         if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
184                 /*
185                  * Try again with VEXPLICIT_DENY, to see if the test for
186                  * deletion is supported.
187                  */
188                 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
189                 if (error == 0) {
190                         if (vp->v_type == VDIR) {
191                                 accmode &= ~(VDELETE | VDELETE_CHILD);
192                                 accmode |= VWRITE;
193                                 error = VOP_ACCESSX(vp, accmode, cred, p);
194                         } else if (supportedtypep != NULL) {
195                                 *supportedtypep &= ~NFSACCESS_DELETE;
196                         }
197                 }
198         }
199
200         /*
201          * Allow certain operations for the owner (reads and writes
202          * on files that are already open).
203          */
204         if (override != NFSACCCHK_NOOVERRIDE &&
205             (error == EPERM || error == EACCES)) {
206                 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
207                         error = 0;
208                 else if (override & NFSACCCHK_ALLOWOWNER) {
209                         getret = VOP_GETATTR(vp, &vattr, cred);
210                         if (getret == 0 && cred->cr_uid == vattr.va_uid)
211                                 error = 0;
212                 }
213         }
214         if (vpislocked == 0)
215                 NFSVOPUNLOCK(vp, 0, p);
216         return (error);
217 }
218
219 /*
220  * Set attribute(s) vnop.
221  */
222 int
223 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
224     struct thread *p, struct nfsexstuff *exp)
225 {
226         int error;
227
228         error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
229         return (error);
230 }
231
232 /*
233  * Set up nameidata for a lookup() call and do it
234  * For the cases where we are crossing mount points
235  * (looking up the public fh path or the v4 root path when
236  *  not using a pseudo-root fs), set/release the Giant lock,
237  * as required.
238  */
239 int
240 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
241     struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
242     struct vnode **retdirp)
243 {
244         struct componentname *cnp = &ndp->ni_cnd;
245         int i;
246         struct iovec aiov;
247         struct uio auio;
248         int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
249         int error = 0, crossmnt;
250         char *cp;
251
252         *retdirp = NULL;
253         cnp->cn_nameptr = cnp->cn_pnbuf;
254         /*
255          * Extract and set starting directory.
256          */
257         if (dp->v_type != VDIR) {
258                 if (islocked)
259                         vput(dp);
260                 else
261                         vrele(dp);
262                 nfsvno_relpathbuf(ndp);
263                 return (ENOTDIR);
264         }
265         if (islocked)
266                 NFSVOPUNLOCK(dp, 0, p);
267         VREF(dp);
268         *retdirp = dp;
269         if (NFSVNO_EXRDONLY(exp))
270                 cnp->cn_flags |= RDONLY;
271         ndp->ni_segflg = UIO_SYSSPACE;
272         crossmnt = 1;
273
274         if (nd->nd_flag & ND_PUBLOOKUP) {
275                 ndp->ni_loopcnt = 0;
276                 if (cnp->cn_pnbuf[0] == '/') {
277                         vrele(dp);
278                         /*
279                          * Check for degenerate pathnames here, since lookup()
280                          * panics on them.
281                          */
282                         for (i = 1; i < ndp->ni_pathlen; i++)
283                                 if (cnp->cn_pnbuf[i] != '/')
284                                         break;
285                         if (i == ndp->ni_pathlen) {
286                                 error = NFSERR_ACCES;
287                                 goto out;
288                         }
289                         dp = rootvnode;
290                         VREF(dp);
291                 }
292         } else if ((enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
293             (nd->nd_flag & ND_NFSV4) == 0) {
294                 /*
295                  * Only cross mount points for NFSv4 when doing a
296                  * mount while traversing the file system above
297                  * the mount point, unless enable_crossmntpt is set.
298                  */
299                 cnp->cn_flags |= NOCROSSMOUNT;
300                 crossmnt = 0;
301         }
302
303         /*
304          * Initialize for scan, set ni_startdir and bump ref on dp again
305          * becuase lookup() will dereference ni_startdir.
306          */
307
308         cnp->cn_thread = p;
309         ndp->ni_startdir = dp;
310         ndp->ni_rootdir = rootvnode;
311
312         if (!lockleaf)
313                 cnp->cn_flags |= LOCKLEAF;
314         for (;;) {
315                 cnp->cn_nameptr = cnp->cn_pnbuf;
316                 /*
317                  * Call lookup() to do the real work.  If an error occurs,
318                  * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
319                  * we do not have to dereference anything before returning.
320                  * In either case ni_startdir will be dereferenced and NULLed
321                  * out.
322                  */
323                 if (exp->nes_vfslocked)
324                         ndp->ni_cnd.cn_flags |= GIANTHELD;
325                 error = lookup(ndp);
326                 /*
327                  * The Giant lock should only change when
328                  * crossing mount points.
329                  */
330                 if (crossmnt) {
331                         exp->nes_vfslocked =
332                             (ndp->ni_cnd.cn_flags & GIANTHELD) != 0;
333                         ndp->ni_cnd.cn_flags &= ~GIANTHELD;
334                 }
335                 if (error)
336                         break;
337
338                 /*
339                  * Check for encountering a symbolic link.  Trivial
340                  * termination occurs if no symlink encountered.
341                  */
342                 if ((cnp->cn_flags & ISSYMLINK) == 0) {
343                         if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
344                                 nfsvno_relpathbuf(ndp);
345                         if (ndp->ni_vp && !lockleaf)
346                                 NFSVOPUNLOCK(ndp->ni_vp, 0, p);
347                         break;
348                 }
349
350                 /*
351                  * Validate symlink
352                  */
353                 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
354                         NFSVOPUNLOCK(ndp->ni_dvp, 0, p);
355                 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
356                         error = EINVAL;
357                         goto badlink2;
358                 }
359
360                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
361                         error = ELOOP;
362                         goto badlink2;
363                 }
364                 if (ndp->ni_pathlen > 1)
365                         cp = uma_zalloc(namei_zone, M_WAITOK);
366                 else
367                         cp = cnp->cn_pnbuf;
368                 aiov.iov_base = cp;
369                 aiov.iov_len = MAXPATHLEN;
370                 auio.uio_iov = &aiov;
371                 auio.uio_iovcnt = 1;
372                 auio.uio_offset = 0;
373                 auio.uio_rw = UIO_READ;
374                 auio.uio_segflg = UIO_SYSSPACE;
375                 auio.uio_td = NULL;
376                 auio.uio_resid = MAXPATHLEN;
377                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
378                 if (error) {
379                 badlink1:
380                         if (ndp->ni_pathlen > 1)
381                                 uma_zfree(namei_zone, cp);
382                 badlink2:
383                         vrele(ndp->ni_dvp);
384                         vput(ndp->ni_vp);
385                         break;
386                 }
387                 linklen = MAXPATHLEN - auio.uio_resid;
388                 if (linklen == 0) {
389                         error = ENOENT;
390                         goto badlink1;
391                 }
392                 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
393                         error = ENAMETOOLONG;
394                         goto badlink1;
395                 }
396
397                 /*
398                  * Adjust or replace path
399                  */
400                 if (ndp->ni_pathlen > 1) {
401                         NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
402                         uma_zfree(namei_zone, cnp->cn_pnbuf);
403                         cnp->cn_pnbuf = cp;
404                 } else
405                         cnp->cn_pnbuf[linklen] = '\0';
406                 ndp->ni_pathlen += linklen;
407
408                 /*
409                  * Cleanup refs for next loop and check if root directory
410                  * should replace current directory.  Normally ni_dvp
411                  * becomes the new base directory and is cleaned up when
412                  * we loop.  Explicitly null pointers after invalidation
413                  * to clarify operation.
414                  */
415                 vput(ndp->ni_vp);
416                 ndp->ni_vp = NULL;
417
418                 if (cnp->cn_pnbuf[0] == '/') {
419                         vrele(ndp->ni_dvp);
420                         ndp->ni_dvp = ndp->ni_rootdir;
421                         VREF(ndp->ni_dvp);
422                 }
423                 ndp->ni_startdir = ndp->ni_dvp;
424                 ndp->ni_dvp = NULL;
425         }
426         if (!lockleaf)
427                 cnp->cn_flags &= ~LOCKLEAF;
428
429 out:
430         if (error) {
431                 uma_zfree(namei_zone, cnp->cn_pnbuf);
432                 ndp->ni_vp = NULL;
433                 ndp->ni_dvp = NULL;
434                 ndp->ni_startdir = NULL;
435                 cnp->cn_flags &= ~HASBUF;
436         } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
437                 ndp->ni_dvp = NULL;
438         }
439         return (error);
440 }
441
442 /*
443  * Set up a pathname buffer and return a pointer to it and, optionally
444  * set a hash pointer.
445  */
446 void
447 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
448 {
449         struct componentname *cnp = &ndp->ni_cnd;
450
451         cnp->cn_flags |= (NOMACCHECK | HASBUF);
452         cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
453         if (hashpp != NULL)
454                 *hashpp = NULL;
455         *bufpp = cnp->cn_pnbuf;
456 }
457
458 /*
459  * Release the above path buffer, if not released by nfsvno_namei().
460  */
461 void
462 nfsvno_relpathbuf(struct nameidata *ndp)
463 {
464
465         if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
466                 panic("nfsrelpath");
467         uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
468         ndp->ni_cnd.cn_flags &= ~HASBUF;
469 }
470
471 /*
472  * Readlink vnode op into an mbuf list.
473  */
474 int
475 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
476     struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
477 {
478         struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
479         struct iovec *ivp = iv;
480         struct uio io, *uiop = &io;
481         struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
482         int i, len, tlen, error;
483
484         len = 0;
485         i = 0;
486         while (len < NFS_MAXPATHLEN) {
487                 NFSMGET(mp);
488                 MCLGET(mp, M_WAIT);
489                 mp->m_len = NFSMSIZ(mp);
490                 if (len == 0) {
491                         mp3 = mp2 = mp;
492                 } else {
493                         mp2->m_next = mp;
494                         mp2 = mp;
495                 }
496                 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
497                         mp->m_len = NFS_MAXPATHLEN - len;
498                         len = NFS_MAXPATHLEN;
499                 } else {
500                         len += mp->m_len;
501                 }
502                 ivp->iov_base = mtod(mp, caddr_t);
503                 ivp->iov_len = mp->m_len;
504                 i++;
505                 ivp++;
506         }
507         uiop->uio_iov = iv;
508         uiop->uio_iovcnt = i;
509         uiop->uio_offset = 0;
510         uiop->uio_resid = len;
511         uiop->uio_rw = UIO_READ;
512         uiop->uio_segflg = UIO_SYSSPACE;
513         uiop->uio_td = NULL;
514         error = VOP_READLINK(vp, uiop, cred);
515         if (error) {
516                 m_freem(mp3);
517                 *lenp = 0;
518                 return (error);
519         }
520         if (uiop->uio_resid > 0) {
521                 len -= uiop->uio_resid;
522                 tlen = NFSM_RNDUP(len);
523                 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
524         }
525         *lenp = len;
526         *mpp = mp3;
527         *mpendp = mp;
528         return (0);
529 }
530
531 /*
532  * Read vnode op call into mbuf list.
533  */
534 int
535 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
536     struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
537 {
538         struct mbuf *m;
539         int i;
540         struct iovec *iv;
541         struct iovec *iv2;
542         int error = 0, len, left, siz, tlen, ioflag = 0, hi, try = 32;
543         struct mbuf *m2 = NULL, *m3;
544         struct uio io, *uiop = &io;
545         struct nfsheur *nh;
546
547         /*
548          * Calculate seqcount for heuristic
549          */
550         /*
551          * Locate best candidate
552          */
553
554         hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
555         nh = &nfsheur[hi];
556
557         while (try--) {
558                 if (nfsheur[hi].nh_vp == vp) {
559                         nh = &nfsheur[hi];
560                         break;
561                 }
562                 if (nfsheur[hi].nh_use > 0)
563                         --nfsheur[hi].nh_use;
564                 hi = (hi + 1) % NUM_HEURISTIC;
565                 if (nfsheur[hi].nh_use < nh->nh_use)
566                         nh = &nfsheur[hi];
567         }
568
569         if (nh->nh_vp != vp) {
570                 nh->nh_vp = vp;
571                 nh->nh_nextr = off;
572                 nh->nh_use = NHUSE_INIT;
573                 if (off == 0)
574                         nh->nh_seqcount = 4;
575                 else
576                         nh->nh_seqcount = 1;
577         }
578
579         /*
580          * Calculate heuristic
581          */
582
583         if ((off == 0 && nh->nh_seqcount > 0) || off == nh->nh_nextr) {
584                 if (++nh->nh_seqcount > IO_SEQMAX)
585                         nh->nh_seqcount = IO_SEQMAX;
586         } else if (nh->nh_seqcount > 1) {
587                 nh->nh_seqcount = 1;
588         } else {
589                 nh->nh_seqcount = 0;
590         }
591         nh->nh_use += NHUSE_INC;
592         if (nh->nh_use > NHUSE_MAX)
593                 nh->nh_use = NHUSE_MAX;
594         ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
595
596         len = left = NFSM_RNDUP(cnt);
597         m3 = NULL;
598         /*
599          * Generate the mbuf list with the uio_iov ref. to it.
600          */
601         i = 0;
602         while (left > 0) {
603                 NFSMGET(m);
604                 MCLGET(m, M_WAIT);
605                 m->m_len = 0;
606                 siz = min(M_TRAILINGSPACE(m), left);
607                 left -= siz;
608                 i++;
609                 if (m3)
610                         m2->m_next = m;
611                 else
612                         m3 = m;
613                 m2 = m;
614         }
615         MALLOC(iv, struct iovec *, i * sizeof (struct iovec),
616             M_TEMP, M_WAITOK);
617         uiop->uio_iov = iv2 = iv;
618         m = m3;
619         left = len;
620         i = 0;
621         while (left > 0) {
622                 if (m == NULL)
623                         panic("nfsvno_read iov");
624                 siz = min(M_TRAILINGSPACE(m), left);
625                 if (siz > 0) {
626                         iv->iov_base = mtod(m, caddr_t) + m->m_len;
627                         iv->iov_len = siz;
628                         m->m_len += siz;
629                         left -= siz;
630                         iv++;
631                         i++;
632                 }
633                 m = m->m_next;
634         }
635         uiop->uio_iovcnt = i;
636         uiop->uio_offset = off;
637         uiop->uio_resid = len;
638         uiop->uio_rw = UIO_READ;
639         uiop->uio_segflg = UIO_SYSSPACE;
640         error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
641         FREE((caddr_t)iv2, M_TEMP);
642         if (error) {
643                 m_freem(m3);
644                 *mpp = NULL;
645                 return (error);
646         }
647         tlen = len - uiop->uio_resid;
648         cnt = cnt < tlen ? cnt : tlen;
649         tlen = NFSM_RNDUP(cnt);
650         if (tlen == 0) {
651                 m_freem(m3);
652                 m3 = NULL;
653         } else if (len != tlen || tlen != cnt)
654                 nfsrv_adj(m3, len - tlen, tlen - cnt);
655         *mpp = m3;
656         *mpendp = m2;
657         return (0);
658 }
659
660 /*
661  * Write vnode op from an mbuf list.
662  */
663 int
664 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable,
665     struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
666 {
667         struct iovec *ivp;
668         int i, len;
669         struct iovec *iv;
670         int ioflags, error;
671         struct uio io, *uiop = &io;
672
673         MALLOC(ivp, struct iovec *, cnt * sizeof (struct iovec), M_TEMP,
674             M_WAITOK);
675         uiop->uio_iov = iv = ivp;
676         uiop->uio_iovcnt = cnt;
677         i = mtod(mp, caddr_t) + mp->m_len - cp;
678         len = retlen;
679         while (len > 0) {
680                 if (mp == NULL)
681                         panic("nfsvno_write");
682                 if (i > 0) {
683                         i = min(i, len);
684                         ivp->iov_base = cp;
685                         ivp->iov_len = i;
686                         ivp++;
687                         len -= i;
688                 }
689                 mp = mp->m_next;
690                 if (mp) {
691                         i = mp->m_len;
692                         cp = mtod(mp, caddr_t);
693                 }
694         }
695
696         if (stable == NFSWRITE_UNSTABLE)
697                 ioflags = IO_NODELOCKED;
698         else
699                 ioflags = (IO_SYNC | IO_NODELOCKED);
700         uiop->uio_resid = retlen;
701         uiop->uio_rw = UIO_WRITE;
702         uiop->uio_segflg = UIO_SYSSPACE;
703         NFSUIOPROC(uiop, p);
704         uiop->uio_offset = off;
705         error = VOP_WRITE(vp, uiop, ioflags, cred);
706         FREE((caddr_t)iv, M_TEMP);
707         return (error);
708 }
709
710 /*
711  * Common code for creating a regular file (plus special files for V2).
712  */
713 int
714 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
715     struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
716     int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
717 {
718         u_quad_t tempsize;
719         int error;
720
721         error = nd->nd_repstat;
722         if (!error && ndp->ni_vp == NULL) {
723                 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
724                         vrele(ndp->ni_startdir);
725                         error = VOP_CREATE(ndp->ni_dvp,
726                             &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
727                         vput(ndp->ni_dvp);
728                         nfsvno_relpathbuf(ndp);
729                         if (!error) {
730                                 if (*exclusive_flagp) {
731                                         *exclusive_flagp = 0;
732                                         NFSVNO_ATTRINIT(nvap);
733                                         nvap->na_atime.tv_sec = cverf[0];
734                                         nvap->na_atime.tv_nsec = cverf[1];
735                                         error = VOP_SETATTR(ndp->ni_vp,
736                                             &nvap->na_vattr, nd->nd_cred);
737                                 }
738                         }
739                 /*
740                  * NFS V2 Only. nfsrvd_mknod() does this for V3.
741                  * (This implies, just get out on an error.)
742                  */
743                 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
744                         nvap->na_type == VFIFO) {
745                         if (nvap->na_type == VCHR && rdev == 0xffffffff)
746                                 nvap->na_type = VFIFO;
747                         if (nvap->na_type != VFIFO &&
748                             (error = priv_check_cred(nd->nd_cred,
749                              PRIV_VFS_MKNOD_DEV, 0))) {
750                                 vrele(ndp->ni_startdir);
751                                 nfsvno_relpathbuf(ndp);
752                                 vput(ndp->ni_dvp);
753                                 return (error);
754                         }
755                         nvap->na_rdev = rdev;
756                         error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
757                             &ndp->ni_cnd, &nvap->na_vattr);
758                         vput(ndp->ni_dvp);
759                         nfsvno_relpathbuf(ndp);
760                         if (error) {
761                                 vrele(ndp->ni_startdir);
762                                 return (error);
763                         }
764                 } else {
765                         vrele(ndp->ni_startdir);
766                         nfsvno_relpathbuf(ndp);
767                         vput(ndp->ni_dvp);
768                         return (ENXIO);
769                 }
770                 *vpp = ndp->ni_vp;
771         } else {
772                 /*
773                  * Handle cases where error is already set and/or
774                  * the file exists.
775                  * 1 - clean up the lookup
776                  * 2 - iff !error and na_size set, truncate it
777                  */
778                 vrele(ndp->ni_startdir);
779                 nfsvno_relpathbuf(ndp);
780                 *vpp = ndp->ni_vp;
781                 if (ndp->ni_dvp == *vpp)
782                         vrele(ndp->ni_dvp);
783                 else
784                         vput(ndp->ni_dvp);
785                 if (!error && nvap->na_size != VNOVAL) {
786                         error = nfsvno_accchk(*vpp, VWRITE,
787                             nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
788                             NFSACCCHK_VPISLOCKED, NULL);
789                         if (!error) {
790                                 tempsize = nvap->na_size;
791                                 NFSVNO_ATTRINIT(nvap);
792                                 nvap->na_size = tempsize;
793                                 error = VOP_SETATTR(*vpp,
794                                     &nvap->na_vattr, nd->nd_cred);
795                         }
796                 }
797                 if (error)
798                         vput(*vpp);
799         }
800         return (error);
801 }
802
803 /*
804  * Do a mknod vnode op.
805  */
806 int
807 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
808     struct thread *p)
809 {
810         int error = 0;
811         enum vtype vtyp;
812
813         vtyp = nvap->na_type;
814         /*
815          * Iff doesn't exist, create it.
816          */
817         if (ndp->ni_vp) {
818                 vrele(ndp->ni_startdir);
819                 nfsvno_relpathbuf(ndp);
820                 vput(ndp->ni_dvp);
821                 vrele(ndp->ni_vp);
822                 return (EEXIST);
823         }
824         if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
825                 vrele(ndp->ni_startdir);
826                 nfsvno_relpathbuf(ndp);
827                 vput(ndp->ni_dvp);
828                 return (NFSERR_BADTYPE);
829         }
830         if (vtyp == VSOCK) {
831                 vrele(ndp->ni_startdir);
832                 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
833                     &ndp->ni_cnd, &nvap->na_vattr);
834                 vput(ndp->ni_dvp);
835                 nfsvno_relpathbuf(ndp);
836         } else {
837                 if (nvap->na_type != VFIFO &&
838                     (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
839                         vrele(ndp->ni_startdir);
840                         nfsvno_relpathbuf(ndp);
841                         vput(ndp->ni_dvp);
842                         return (error);
843                 }
844                 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
845                     &ndp->ni_cnd, &nvap->na_vattr);
846                 vput(ndp->ni_dvp);
847                 nfsvno_relpathbuf(ndp);
848                 if (error)
849                         vrele(ndp->ni_startdir);
850                 /*
851                  * Since VOP_MKNOD returns the ni_vp, I can't
852                  * see any reason to do the lookup.
853                  */
854         }
855         return (error);
856 }
857
858 /*
859  * Mkdir vnode op.
860  */
861 int
862 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
863     struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
864 {
865         int error = 0;
866
867         if (ndp->ni_vp != NULL) {
868                 if (ndp->ni_dvp == ndp->ni_vp)
869                         vrele(ndp->ni_dvp);
870                 else
871                         vput(ndp->ni_dvp);
872                 vrele(ndp->ni_vp);
873                 nfsvno_relpathbuf(ndp);
874                 return (EEXIST);
875         }
876         error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
877             &nvap->na_vattr);
878         vput(ndp->ni_dvp);
879         nfsvno_relpathbuf(ndp);
880         return (error);
881 }
882
883 /*
884  * symlink vnode op.
885  */
886 int
887 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
888     int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
889     struct nfsexstuff *exp)
890 {
891         int error = 0;
892
893         if (ndp->ni_vp) {
894                 vrele(ndp->ni_startdir);
895                 nfsvno_relpathbuf(ndp);
896                 if (ndp->ni_dvp == ndp->ni_vp)
897                         vrele(ndp->ni_dvp);
898                 else
899                         vput(ndp->ni_dvp);
900                 vrele(ndp->ni_vp);
901                 return (EEXIST);
902         }
903
904         error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
905             &nvap->na_vattr, pathcp);
906         vput(ndp->ni_dvp);
907         vrele(ndp->ni_startdir);
908         nfsvno_relpathbuf(ndp);
909         /*
910          * Although FreeBSD still had the lookup code in
911          * it for 7/current, there doesn't seem to be any
912          * point, since VOP_SYMLINK() returns the ni_vp.
913          * Just vput it for v2.
914          */
915         if (!not_v2 && !error)
916                 vput(ndp->ni_vp);
917         return (error);
918 }
919
920 /*
921  * Parse symbolic link arguments.
922  * This function has an ugly side effect. It will MALLOC() an area for
923  * the symlink and set iov_base to point to it, only if it succeeds.
924  * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
925  * be FREE'd later.
926  */
927 int
928 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
929     struct thread *p, char **pathcpp, int *lenp)
930 {
931         u_int32_t *tl;
932         char *pathcp = NULL;
933         int error = 0, len;
934         struct nfsv2_sattr *sp;
935
936         *pathcpp = NULL;
937         *lenp = 0;
938         if ((nd->nd_flag & ND_NFSV3) &&
939             (error = nfsrv_sattr(nd, nvap, NULL, NULL, p)))
940                 goto nfsmout;
941         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
942         len = fxdr_unsigned(int, *tl);
943         if (len > NFS_MAXPATHLEN || len <= 0) {
944                 error = EBADRPC;
945                 goto nfsmout;
946         }
947         MALLOC(pathcp, caddr_t, len + 1, M_TEMP, M_WAITOK);
948         error = nfsrv_mtostr(nd, pathcp, len);
949         if (error)
950                 goto nfsmout;
951         if (nd->nd_flag & ND_NFSV2) {
952                 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
953                 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
954         }
955         *pathcpp = pathcp;
956         *lenp = len;
957         return (0);
958 nfsmout:
959         if (pathcp)
960                 free(pathcp, M_TEMP);
961         return (error);
962 }
963
964 /*
965  * Remove a non-directory object.
966  */
967 int
968 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
969     struct thread *p, struct nfsexstuff *exp)
970 {
971         struct vnode *vp;
972         int error = 0;
973
974         vp = ndp->ni_vp;
975         if (vp->v_type == VDIR)
976                 error = NFSERR_ISDIR;
977         else if (is_v4)
978                 error = nfsrv_checkremove(vp, 1, p);
979         if (!error)
980                 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
981         if (ndp->ni_dvp == vp)
982                 vrele(ndp->ni_dvp);
983         else
984                 vput(ndp->ni_dvp);
985         vput(vp);
986         return (error);
987 }
988
989 /*
990  * Remove a directory.
991  */
992 int
993 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
994     struct thread *p, struct nfsexstuff *exp)
995 {
996         struct vnode *vp;
997         int error = 0;
998
999         vp = ndp->ni_vp;
1000         if (vp->v_type != VDIR) {
1001                 error = ENOTDIR;
1002                 goto out;
1003         }
1004         /*
1005          * No rmdir "." please.
1006          */
1007         if (ndp->ni_dvp == vp) {
1008                 error = EINVAL;
1009                 goto out;
1010         }
1011         /*
1012          * The root of a mounted filesystem cannot be deleted.
1013          */
1014         if (vp->v_vflag & VV_ROOT)
1015                 error = EBUSY;
1016 out:
1017         if (!error)
1018                 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1019         if (ndp->ni_dvp == vp)
1020                 vrele(ndp->ni_dvp);
1021         else
1022                 vput(ndp->ni_dvp);
1023         vput(vp);
1024         return (error);
1025 }
1026
1027 /*
1028  * Rename vnode op.
1029  */
1030 int
1031 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1032     u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1033 {
1034         struct vnode *fvp, *tvp, *tdvp;
1035         int error = 0;
1036
1037         fvp = fromndp->ni_vp;
1038         if (ndstat) {
1039                 vrele(fromndp->ni_dvp);
1040                 vrele(fvp);
1041                 error = ndstat;
1042                 goto out1;
1043         }
1044         tdvp = tondp->ni_dvp;
1045         tvp = tondp->ni_vp;
1046         if (tvp != NULL) {
1047                 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1048                         error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1049                         goto out;
1050                 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1051                         error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1052                         goto out;
1053                 }
1054                 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1055                         error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1056                         goto out;
1057                 }
1058
1059                 /*
1060                  * A rename to '.' or '..' results in a prematurely
1061                  * unlocked vnode on FreeBSD5, so I'm just going to fail that
1062                  * here.
1063                  */
1064                 if ((tondp->ni_cnd.cn_namelen == 1 &&
1065                      tondp->ni_cnd.cn_nameptr[0] == '.') ||
1066                     (tondp->ni_cnd.cn_namelen == 2 &&
1067                      tondp->ni_cnd.cn_nameptr[0] == '.' &&
1068                      tondp->ni_cnd.cn_nameptr[1] == '.')) {
1069                         error = EINVAL;
1070                         goto out;
1071                 }
1072         }
1073         if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1074                 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1075                 goto out;
1076         }
1077         if (fvp->v_mount != tdvp->v_mount) {
1078                 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1079                 goto out;
1080         }
1081         if (fvp == tdvp) {
1082                 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1083                 goto out;
1084         }
1085         if (fvp == tvp) {
1086                 /*
1087                  * If source and destination are the same, there is nothing to
1088                  * do. Set error to -1 to indicate this.
1089                  */
1090                 error = -1;
1091                 goto out;
1092         }
1093         if (ndflag & ND_NFSV4) {
1094                 NFSVOPLOCK(fvp, LK_EXCLUSIVE | LK_RETRY, p);
1095                 error = nfsrv_checkremove(fvp, 0, p);
1096                 NFSVOPUNLOCK(fvp, 0, p);
1097                 if (tvp && !error)
1098                         error = nfsrv_checkremove(tvp, 1, p);
1099         } else {
1100                 /*
1101                  * For NFSv2 and NFSv3, try to get rid of the delegation, so
1102                  * that the NFSv4 client won't be confused by the rename.
1103                  * Since nfsd_recalldelegation() can only be called on an
1104                  * unlocked vnode at this point and fvp is the file that will
1105                  * still exist after the rename, just do fvp.
1106                  */
1107                 nfsd_recalldelegation(fvp, p);
1108         }
1109 out:
1110         if (!error) {
1111                 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1112                     &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1113                     &tondp->ni_cnd);
1114         } else {
1115                 if (tdvp == tvp)
1116                         vrele(tdvp);
1117                 else
1118                         vput(tdvp);
1119                 if (tvp)
1120                         vput(tvp);
1121                 vrele(fromndp->ni_dvp);
1122                 vrele(fvp);
1123                 if (error == -1)
1124                         error = 0;
1125         }
1126         vrele(tondp->ni_startdir);
1127         nfsvno_relpathbuf(tondp);
1128 out1:
1129         vrele(fromndp->ni_startdir);
1130         nfsvno_relpathbuf(fromndp);
1131         return (error);
1132 }
1133
1134 /*
1135  * Link vnode op.
1136  */
1137 int
1138 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1139     struct thread *p, struct nfsexstuff *exp)
1140 {
1141         struct vnode *xp;
1142         int error = 0;
1143
1144         xp = ndp->ni_vp;
1145         if (xp != NULL) {
1146                 error = EEXIST;
1147         } else {
1148                 xp = ndp->ni_dvp;
1149                 if (vp->v_mount != xp->v_mount)
1150                         error = EXDEV;
1151         }
1152         if (!error) {
1153                 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
1154                 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1155                 if (ndp->ni_dvp == vp)
1156                         vrele(ndp->ni_dvp);
1157                 else
1158                         vput(ndp->ni_dvp);
1159                 NFSVOPUNLOCK(vp, 0, p);
1160         } else {
1161                 if (ndp->ni_dvp == ndp->ni_vp)
1162                         vrele(ndp->ni_dvp);
1163                 else
1164                         vput(ndp->ni_dvp);
1165                 if (ndp->ni_vp)
1166                         vrele(ndp->ni_vp);
1167         }
1168         nfsvno_relpathbuf(ndp);
1169         return (error);
1170 }
1171
1172 /*
1173  * Do the fsync() appropriate for the commit.
1174  */
1175 int
1176 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1177     struct thread *td)
1178 {
1179         int error = 0;
1180
1181         if (cnt > MAX_COMMIT_COUNT) {
1182                 /*
1183                  * Give up and do the whole thing
1184                  */
1185                 if (vp->v_object &&
1186                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1187                         VM_OBJECT_LOCK(vp->v_object);
1188                         vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1189                         VM_OBJECT_UNLOCK(vp->v_object);
1190                 }
1191                 error = VOP_FSYNC(vp, MNT_WAIT, td);
1192         } else {
1193                 /*
1194                  * Locate and synchronously write any buffers that fall
1195                  * into the requested range.  Note:  we are assuming that
1196                  * f_iosize is a power of 2.
1197                  */
1198                 int iosize = vp->v_mount->mnt_stat.f_iosize;
1199                 int iomask = iosize - 1;
1200                 struct bufobj *bo;
1201                 daddr_t lblkno;
1202
1203                 /*
1204                  * Align to iosize boundry, super-align to page boundry.
1205                  */
1206                 if (off & iomask) {
1207                         cnt += off & iomask;
1208                         off &= ~(u_quad_t)iomask;
1209                 }
1210                 if (off & PAGE_MASK) {
1211                         cnt += off & PAGE_MASK;
1212                         off &= ~(u_quad_t)PAGE_MASK;
1213                 }
1214                 lblkno = off / iosize;
1215
1216                 if (vp->v_object &&
1217                    (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1218                         VM_OBJECT_LOCK(vp->v_object);
1219                         vm_object_page_clean(vp->v_object, off / PAGE_SIZE, (cnt + PAGE_MASK) / PAGE_SIZE, OBJPC_SYNC);
1220                         VM_OBJECT_UNLOCK(vp->v_object);
1221                 }
1222
1223                 bo = &vp->v_bufobj;
1224                 BO_LOCK(bo);
1225                 while (cnt > 0) {
1226                         struct buf *bp;
1227
1228                         /*
1229                          * If we have a buffer and it is marked B_DELWRI we
1230                          * have to lock and write it.  Otherwise the prior
1231                          * write is assumed to have already been committed.
1232                          *
1233                          * gbincore() can return invalid buffers now so we
1234                          * have to check that bit as well (though B_DELWRI
1235                          * should not be set if B_INVAL is set there could be
1236                          * a race here since we haven't locked the buffer).
1237                          */
1238                         if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1239                                 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1240                                     LK_INTERLOCK, BO_MTX(bo)) == ENOLCK) {
1241                                         BO_LOCK(bo);
1242                                         continue; /* retry */
1243                                 }
1244                                 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1245                                     B_DELWRI) {
1246                                         bremfree(bp);
1247                                         bp->b_flags &= ~B_ASYNC;
1248                                         bwrite(bp);
1249                                         ++nfs_commit_miss;
1250                                 } else
1251                                         BUF_UNLOCK(bp);
1252                                 BO_LOCK(bo);
1253                         }
1254                         ++nfs_commit_blks;
1255                         if (cnt < iosize)
1256                                 break;
1257                         cnt -= iosize;
1258                         ++lblkno;
1259                 }
1260                 BO_UNLOCK(bo);
1261         }
1262         return (error);
1263 }
1264
1265 /*
1266  * Statfs vnode op.
1267  */
1268 int
1269 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1270 {
1271
1272         return (VFS_STATFS(vp->v_mount, sf));
1273 }
1274
1275 /*
1276  * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1277  * must handle nfsrv_opencheck() calls after any other access checks.
1278  */
1279 void
1280 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1281     nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1282     int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1283     NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1284     struct nfsexstuff *exp, struct vnode **vpp)
1285 {
1286         struct vnode *vp = NULL;
1287         u_quad_t tempsize;
1288         struct nfsexstuff nes;
1289
1290         if (ndp->ni_vp == NULL)
1291                 nd->nd_repstat = nfsrv_opencheck(clientid,
1292                     stateidp, stp, NULL, nd, p, nd->nd_repstat);
1293         if (!nd->nd_repstat) {
1294                 if (ndp->ni_vp == NULL) {
1295                         vrele(ndp->ni_startdir);
1296                         nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1297                             &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1298                         vput(ndp->ni_dvp);
1299                         nfsvno_relpathbuf(ndp);
1300                         if (!nd->nd_repstat) {
1301                                 if (*exclusive_flagp) {
1302                                         *exclusive_flagp = 0;
1303                                         NFSVNO_ATTRINIT(nvap);
1304                                         nvap->na_atime.tv_sec = cverf[0];
1305                                         nvap->na_atime.tv_nsec = cverf[1];
1306                                         nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1307                                             &nvap->na_vattr, cred);
1308                                 } else {
1309                                         nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1310                                             aclp, p, attrbitp, exp);
1311                                 }
1312                         }
1313                         vp = ndp->ni_vp;
1314                 } else {
1315                         if (ndp->ni_startdir)
1316                                 vrele(ndp->ni_startdir);
1317                         nfsvno_relpathbuf(ndp);
1318                         vp = ndp->ni_vp;
1319                         if (create == NFSV4OPEN_CREATE) {
1320                                 if (ndp->ni_dvp == vp)
1321                                         vrele(ndp->ni_dvp);
1322                                 else
1323                                         vput(ndp->ni_dvp);
1324                         }
1325                         if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1326                                 if (ndp->ni_cnd.cn_flags & RDONLY)
1327                                         NFSVNO_SETEXRDONLY(&nes);
1328                                 else
1329                                         NFSVNO_EXINIT(&nes);
1330                                 nd->nd_repstat = nfsvno_accchk(vp, 
1331                                     VWRITE, cred, &nes, p,
1332                                     NFSACCCHK_NOOVERRIDE,
1333                                     NFSACCCHK_VPISLOCKED, NULL);
1334                                 nd->nd_repstat = nfsrv_opencheck(clientid,
1335                                     stateidp, stp, vp, nd, p, nd->nd_repstat);
1336                                 if (!nd->nd_repstat) {
1337                                         tempsize = nvap->na_size;
1338                                         NFSVNO_ATTRINIT(nvap);
1339                                         nvap->na_size = tempsize;
1340                                         nd->nd_repstat = VOP_SETATTR(vp,
1341                                             &nvap->na_vattr, cred);
1342                                 }
1343                         } else if (vp->v_type == VREG) {
1344                                 nd->nd_repstat = nfsrv_opencheck(clientid,
1345                                     stateidp, stp, vp, nd, p, nd->nd_repstat);
1346                         }
1347                 }
1348         } else {
1349                 if (ndp->ni_cnd.cn_flags & HASBUF)
1350                         nfsvno_relpathbuf(ndp);
1351                 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1352                         vrele(ndp->ni_startdir);
1353                         if (ndp->ni_dvp == ndp->ni_vp)
1354                                 vrele(ndp->ni_dvp);
1355                         else
1356                                 vput(ndp->ni_dvp);
1357                         if (ndp->ni_vp)
1358                                 vput(ndp->ni_vp);
1359                 }
1360         }
1361         *vpp = vp;
1362 }
1363
1364 /*
1365  * Updates the file rev and sets the mtime and ctime
1366  * to the current clock time, returning the va_filerev and va_Xtime
1367  * values.
1368  */
1369 void
1370 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1371     struct ucred *cred, struct thread *p)
1372 {
1373         struct vattr va;
1374
1375         VATTR_NULL(&va);
1376         getnanotime(&va.va_mtime);
1377         (void) VOP_SETATTR(vp, &va, cred);
1378         (void) nfsvno_getattr(vp, nvap, cred, p);
1379 }
1380
1381 /*
1382  * Glue routine to nfsv4_fillattr().
1383  */
1384 int
1385 nfsvno_fillattr(struct nfsrv_descript *nd, struct vnode *vp,
1386     struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1387     struct ucred *cred, struct thread *p, int isdgram, int reterr)
1388 {
1389         int error;
1390
1391         error = nfsv4_fillattr(nd, vp, NULL, &nvap->na_vattr, fhp, rderror,
1392             attrbitp, cred, p, isdgram, reterr);
1393         return (error);
1394 }
1395
1396 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1397 /*
1398  * nfs readdir service
1399  * - mallocs what it thinks is enough to read
1400  *      count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1401  * - calls VOP_READDIR()
1402  * - loops around building the reply
1403  *      if the output generated exceeds count break out of loop
1404  *      The NFSM_CLGET macro is used here so that the reply will be packed
1405  *      tightly in mbuf clusters.
1406  * - it trims out records with d_fileno == 0
1407  *      this doesn't matter for Unix clients, but they might confuse clients
1408  *      for other os'.
1409  * - it trims out records with d_type == DT_WHT
1410  *      these cannot be seen through NFS (unless we extend the protocol)
1411  *     The alternate call nfsrvd_readdirplus() does lookups as well.
1412  * PS: The NFS protocol spec. does not clarify what the "count" byte
1413  *      argument is a count of.. just name strings and file id's or the
1414  *      entire reply rpc or ...
1415  *      I tried just file name and id sizes and it confused the Sun client,
1416  *      so I am using the full rpc size now. The "paranoia.." comment refers
1417  *      to including the status longwords that are not a part of the dir.
1418  *      "entry" structures, but are in the rpc.
1419  */
1420 int
1421 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1422     struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1423 {
1424         struct dirent *dp;
1425         u_int32_t *tl;
1426         int dirlen;
1427         char *cpos, *cend, *rbuf;
1428         struct nfsvattr at;
1429         int nlen, error = 0, getret = 1;
1430         int siz, cnt, fullsiz, eofflag, ncookies;
1431         u_int64_t off, toff, verf;
1432         u_long *cookies = NULL, *cookiep;
1433         struct uio io;
1434         struct iovec iv;
1435         int not_zfs;
1436
1437         if (nd->nd_repstat) {
1438                 nfsrv_postopattr(nd, getret, &at);
1439                 return (0);
1440         }
1441         if (nd->nd_flag & ND_NFSV2) {
1442                 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1443                 off = fxdr_unsigned(u_quad_t, *tl++);
1444         } else {
1445                 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1446                 off = fxdr_hyper(tl);
1447                 tl += 2;
1448                 verf = fxdr_hyper(tl);
1449                 tl += 2;
1450         }
1451         toff = off;
1452         cnt = fxdr_unsigned(int, *tl);
1453         if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1454                 cnt = NFS_SRVMAXDATA(nd);
1455         siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1456         fullsiz = siz;
1457         if (nd->nd_flag & ND_NFSV3) {
1458                 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred,
1459                     p);
1460 #if 0
1461                 /*
1462                  * va_filerev is not sufficient as a cookie verifier,
1463                  * since it is not supposed to change when entries are
1464                  * removed/added unless that offset cookies returned to
1465                  * the client are no longer valid.
1466                  */
1467                 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1468                         nd->nd_repstat = NFSERR_BAD_COOKIE;
1469 #endif
1470         }
1471         if (nd->nd_repstat == 0 && cnt == 0) {
1472                 if (nd->nd_flag & ND_NFSV2)
1473                         /* NFSv2 does not have NFSERR_TOOSMALL */
1474                         nd->nd_repstat = EPERM;
1475                 else
1476                         nd->nd_repstat = NFSERR_TOOSMALL;
1477         }
1478         if (!nd->nd_repstat)
1479                 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1480                     nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1481                     NFSACCCHK_VPISLOCKED, NULL);
1482         if (nd->nd_repstat) {
1483                 vput(vp);
1484                 if (nd->nd_flag & ND_NFSV3)
1485                         nfsrv_postopattr(nd, getret, &at);
1486                 return (0);
1487         }
1488         not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1489         NFSVOPUNLOCK(vp, 0, p);
1490         MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1491 again:
1492         eofflag = 0;
1493         if (cookies) {
1494                 free((caddr_t)cookies, M_TEMP);
1495                 cookies = NULL;
1496         }
1497
1498         iv.iov_base = rbuf;
1499         iv.iov_len = siz;
1500         io.uio_iov = &iv;
1501         io.uio_iovcnt = 1;
1502         io.uio_offset = (off_t)off;
1503         io.uio_resid = siz;
1504         io.uio_segflg = UIO_SYSSPACE;
1505         io.uio_rw = UIO_READ;
1506         io.uio_td = NULL;
1507         NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, p);
1508         nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1509             &cookies);
1510         NFSVOPUNLOCK(vp, 0, p);
1511         off = (u_int64_t)io.uio_offset;
1512         if (io.uio_resid)
1513                 siz -= io.uio_resid;
1514
1515         if (!cookies && !nd->nd_repstat)
1516                 nd->nd_repstat = NFSERR_PERM;
1517         if (nd->nd_flag & ND_NFSV3) {
1518                 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1519                 if (!nd->nd_repstat)
1520                         nd->nd_repstat = getret;
1521         }
1522
1523         /*
1524          * Handles the failed cases. nd->nd_repstat == 0 past here.
1525          */
1526         if (nd->nd_repstat) {
1527                 vrele(vp);
1528                 free((caddr_t)rbuf, M_TEMP);
1529                 if (cookies)
1530                         free((caddr_t)cookies, M_TEMP);
1531                 if (nd->nd_flag & ND_NFSV3)
1532                         nfsrv_postopattr(nd, getret, &at);
1533                 return (0);
1534         }
1535         /*
1536          * If nothing read, return eof
1537          * rpc reply
1538          */
1539         if (siz == 0) {
1540                 vrele(vp);
1541                 if (nd->nd_flag & ND_NFSV2) {
1542                         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1543                 } else {
1544                         nfsrv_postopattr(nd, getret, &at);
1545                         NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1546                         txdr_hyper(at.na_filerev, tl);
1547                         tl += 2;
1548                 }
1549                 *tl++ = newnfs_false;
1550                 *tl = newnfs_true;
1551                 FREE((caddr_t)rbuf, M_TEMP);
1552                 FREE((caddr_t)cookies, M_TEMP);
1553                 return (0);
1554         }
1555
1556         /*
1557          * Check for degenerate cases of nothing useful read.
1558          * If so go try again
1559          */
1560         cpos = rbuf;
1561         cend = rbuf + siz;
1562         dp = (struct dirent *)cpos;
1563         cookiep = cookies;
1564
1565         /*
1566          * For some reason FreeBSD's ufs_readdir() chooses to back the
1567          * directory offset up to a block boundary, so it is necessary to
1568          * skip over the records that precede the requested offset. This
1569          * requires the assumption that file offset cookies monotonically
1570          * increase.
1571          * Since the offset cookies don't monotonically increase for ZFS,
1572          * this is not done when ZFS is the file system.
1573          */
1574         while (cpos < cend && ncookies > 0 &&
1575             (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1576              (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff))) {
1577                 cpos += dp->d_reclen;
1578                 dp = (struct dirent *)cpos;
1579                 cookiep++;
1580                 ncookies--;
1581         }
1582         if (cpos >= cend || ncookies == 0) {
1583                 siz = fullsiz;
1584                 toff = off;
1585                 goto again;
1586         }
1587
1588         /*
1589          * dirlen is the size of the reply, including all XDR and must
1590          * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1591          * if the XDR should be included in "count", but to be safe, we do.
1592          * (Include the two booleans at the end of the reply in dirlen now.)
1593          */
1594         if (nd->nd_flag & ND_NFSV3) {
1595                 nfsrv_postopattr(nd, getret, &at);
1596                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1597                 txdr_hyper(at.na_filerev, tl);
1598                 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1599         } else {
1600                 dirlen = 2 * NFSX_UNSIGNED;
1601         }
1602
1603         /* Loop through the records and build reply */
1604         while (cpos < cend && ncookies > 0) {
1605                 nlen = dp->d_namlen;
1606                 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1607                         nlen <= NFS_MAXNAMLEN) {
1608                         if (nd->nd_flag & ND_NFSV3)
1609                                 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1610                         else
1611                                 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1612                         if (dirlen > cnt) {
1613                                 eofflag = 0;
1614                                 break;
1615                         }
1616
1617                         /*
1618                          * Build the directory record xdr from
1619                          * the dirent entry.
1620                          */
1621                         if (nd->nd_flag & ND_NFSV3) {
1622                                 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1623                                 *tl++ = newnfs_true;
1624                                 *tl++ = 0;
1625                         } else {
1626                                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1627                                 *tl++ = newnfs_true;
1628                         }
1629                         *tl = txdr_unsigned(dp->d_fileno);
1630                         (void) nfsm_strtom(nd, dp->d_name, nlen);
1631                         if (nd->nd_flag & ND_NFSV3) {
1632                                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1633                                 *tl++ = 0;
1634                         } else
1635                                 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
1636                         *tl = txdr_unsigned(*cookiep);
1637                 }
1638                 cpos += dp->d_reclen;
1639                 dp = (struct dirent *)cpos;
1640                 cookiep++;
1641                 ncookies--;
1642         }
1643         if (cpos < cend)
1644                 eofflag = 0;
1645         vrele(vp);
1646         NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1647         *tl++ = newnfs_false;
1648         if (eofflag)
1649                 *tl = newnfs_true;
1650         else
1651                 *tl = newnfs_false;
1652         FREE((caddr_t)rbuf, M_TEMP);
1653         FREE((caddr_t)cookies, M_TEMP);
1654         return (0);
1655 nfsmout:
1656         vput(vp);
1657         return (error);
1658 }
1659
1660 /*
1661  * Readdirplus for V3 and Readdir for V4.
1662  */
1663 int
1664 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
1665     struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1666 {
1667         struct dirent *dp;
1668         u_int32_t *tl;
1669         int dirlen;
1670         char *cpos, *cend, *rbuf;
1671         struct vnode *nvp;
1672         fhandle_t nfh;
1673         struct nfsvattr nva, at, *nvap = &nva;
1674         struct mbuf *mb0, *mb1;
1675         struct nfsreferral *refp;
1676         int nlen, r, error = 0, getret = 1, usevget = 1;
1677         int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
1678         caddr_t bpos0, bpos1;
1679         u_int64_t off, toff, verf;
1680         u_long *cookies = NULL, *cookiep;
1681         nfsattrbit_t attrbits, rderrbits, savbits;
1682         struct uio io;
1683         struct iovec iv;
1684         struct componentname cn;
1685         int not_zfs;
1686
1687         if (nd->nd_repstat) {
1688                 nfsrv_postopattr(nd, getret, &at);
1689                 return (0);
1690         }
1691         NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
1692         off = fxdr_hyper(tl);
1693         toff = off;
1694         tl += 2;
1695         verf = fxdr_hyper(tl);
1696         tl += 2;
1697         siz = fxdr_unsigned(int, *tl++);
1698         cnt = fxdr_unsigned(int, *tl);
1699
1700         /*
1701          * Use the server's maximum data transfer size as the upper bound
1702          * on reply datalen.
1703          */
1704         if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1705                 cnt = NFS_SRVMAXDATA(nd);
1706
1707         /*
1708          * siz is a "hint" of how much directory information (name, fileid,
1709          * cookie) should be in the reply. At least one client "hints" 0,
1710          * so I set it to cnt for that case. I also round it up to the
1711          * next multiple of DIRBLKSIZ.
1712          */
1713         if (siz <= 0)
1714                 siz = cnt;
1715         siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1716
1717         if (nd->nd_flag & ND_NFSV4) {
1718                 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
1719                 if (error)
1720                         goto nfsmout;
1721                 NFSSET_ATTRBIT(&savbits, &attrbits);
1722                 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
1723                 NFSZERO_ATTRBIT(&rderrbits);
1724                 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
1725         } else {
1726                 NFSZERO_ATTRBIT(&attrbits);
1727         }
1728         fullsiz = siz;
1729         nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1730         if (!nd->nd_repstat) {
1731             if (off && verf != at.na_filerev) {
1732                 /*
1733                  * va_filerev is not sufficient as a cookie verifier,
1734                  * since it is not supposed to change when entries are
1735                  * removed/added unless that offset cookies returned to
1736                  * the client are no longer valid.
1737                  */
1738 #if 0
1739                 if (nd->nd_flag & ND_NFSV4) {
1740                         nd->nd_repstat = NFSERR_NOTSAME;
1741                 } else {
1742                         nd->nd_repstat = NFSERR_BAD_COOKIE;
1743                 }
1744 #endif
1745             } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) {
1746                 nd->nd_repstat = NFSERR_BAD_COOKIE;
1747             }
1748         }
1749         if (!nd->nd_repstat && vp->v_type != VDIR)
1750                 nd->nd_repstat = NFSERR_NOTDIR;
1751         if (!nd->nd_repstat && cnt == 0)
1752                 nd->nd_repstat = NFSERR_TOOSMALL;
1753         if (!nd->nd_repstat)
1754                 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1755                     nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1756                     NFSACCCHK_VPISLOCKED, NULL);
1757         if (nd->nd_repstat) {
1758                 vput(vp);
1759                 if (nd->nd_flag & ND_NFSV3)
1760                         nfsrv_postopattr(nd, getret, &at);
1761                 return (0);
1762         }
1763         not_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs");
1764
1765         MALLOC(rbuf, caddr_t, siz, M_TEMP, M_WAITOK);
1766 again:
1767         eofflag = 0;
1768         if (cookies) {
1769                 free((caddr_t)cookies, M_TEMP);
1770                 cookies = NULL;
1771         }
1772
1773         iv.iov_base = rbuf;
1774         iv.iov_len = siz;
1775         io.uio_iov = &iv;
1776         io.uio_iovcnt = 1;
1777         io.uio_offset = (off_t)off;
1778         io.uio_resid = siz;
1779         io.uio_segflg = UIO_SYSSPACE;
1780         io.uio_rw = UIO_READ;
1781         io.uio_td = NULL;
1782         nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1783             &cookies);
1784         off = (u_int64_t)io.uio_offset;
1785         if (io.uio_resid)
1786                 siz -= io.uio_resid;
1787
1788         getret = nfsvno_getattr(vp, &at, nd->nd_cred, p);
1789
1790         if (!cookies && !nd->nd_repstat)
1791                 nd->nd_repstat = NFSERR_PERM;
1792         if (!nd->nd_repstat)
1793                 nd->nd_repstat = getret;
1794         if (nd->nd_repstat) {
1795                 vput(vp);
1796                 if (cookies)
1797                         free((caddr_t)cookies, M_TEMP);
1798                 free((caddr_t)rbuf, M_TEMP);
1799                 if (nd->nd_flag & ND_NFSV3)
1800                         nfsrv_postopattr(nd, getret, &at);
1801                 return (0);
1802         }
1803         /*
1804          * If nothing read, return eof
1805          * rpc reply
1806          */
1807         if (siz == 0) {
1808                 vput(vp);
1809                 if (nd->nd_flag & ND_NFSV3)
1810                         nfsrv_postopattr(nd, getret, &at);
1811                 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1812                 txdr_hyper(at.na_filerev, tl);
1813                 tl += 2;
1814                 *tl++ = newnfs_false;
1815                 *tl = newnfs_true;
1816                 free((caddr_t)cookies, M_TEMP);
1817                 free((caddr_t)rbuf, M_TEMP);
1818                 return (0);
1819         }
1820
1821         /*
1822          * Check for degenerate cases of nothing useful read.
1823          * If so go try again
1824          */
1825         cpos = rbuf;
1826         cend = rbuf + siz;
1827         dp = (struct dirent *)cpos;
1828         cookiep = cookies;
1829
1830         /*
1831          * For some reason FreeBSD's ufs_readdir() chooses to back the
1832          * directory offset up to a block boundary, so it is necessary to
1833          * skip over the records that precede the requested offset. This
1834          * requires the assumption that file offset cookies monotonically
1835          * increase.
1836          * Since the offset cookies don't monotonically increase for ZFS,
1837          * this is not done when ZFS is the file system.
1838          */
1839         while (cpos < cend && ncookies > 0 &&
1840           (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1841            (not_zfs != 0 && ((u_quad_t)(*cookiep)) <= toff) ||
1842            ((nd->nd_flag & ND_NFSV4) &&
1843             ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1844              (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
1845                 cpos += dp->d_reclen;
1846                 dp = (struct dirent *)cpos;
1847                 cookiep++;
1848                 ncookies--;
1849         }
1850         if (cpos >= cend || ncookies == 0) {
1851                 siz = fullsiz;
1852                 toff = off;
1853                 goto again;
1854         }
1855         NFSVOPUNLOCK(vp, 0, p);
1856
1857         /*
1858          * Save this position, in case there is an error before one entry
1859          * is created.
1860          */
1861         mb0 = nd->nd_mb;
1862         bpos0 = nd->nd_bpos;
1863
1864         /*
1865          * Fill in the first part of the reply.
1866          * dirlen is the reply length in bytes and cannot exceed cnt.
1867          * (Include the two booleans at the end of the reply in dirlen now,
1868          *  so we recognize when we have exceeded cnt.)
1869          */
1870         if (nd->nd_flag & ND_NFSV3) {
1871                 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1872                 nfsrv_postopattr(nd, getret, &at);
1873         } else {
1874                 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
1875         }
1876         NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
1877         txdr_hyper(at.na_filerev, tl);
1878
1879         /*
1880          * Save this position, in case there is an empty reply needed.
1881          */
1882         mb1 = nd->nd_mb;
1883         bpos1 = nd->nd_bpos;
1884
1885         /* Loop through the records and build reply */
1886         entrycnt = 0;
1887         while (cpos < cend && ncookies > 0 && dirlen < cnt) {
1888                 nlen = dp->d_namlen;
1889                 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1890                     nlen <= NFS_MAXNAMLEN &&
1891                     ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
1892                      (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
1893                       || (nlen == 1 && dp->d_name[0] != '.'))) {
1894                         /*
1895                          * Save the current position in the reply, in case
1896                          * this entry exceeds cnt.
1897                          */
1898                         mb1 = nd->nd_mb;
1899                         bpos1 = nd->nd_bpos;
1900         
1901                         /*
1902                          * For readdir_and_lookup get the vnode using
1903                          * the file number.
1904                          */
1905                         nvp = NULL;
1906                         refp = NULL;
1907                         r = 0;
1908                         if ((nd->nd_flag & ND_NFSV3) ||
1909                             NFSNONZERO_ATTRBIT(&savbits)) {
1910                                 if (nd->nd_flag & ND_NFSV4)
1911                                         refp = nfsv4root_getreferral(NULL,
1912                                             vp, dp->d_fileno);
1913                                 if (refp == NULL) {
1914                                         if (usevget)
1915                                                 r = VFS_VGET(vp->v_mount,
1916                                                     dp->d_fileno, LK_EXCLUSIVE,
1917                                                     &nvp);
1918                                         else
1919                                                 r = EOPNOTSUPP;
1920                                         if (r == EOPNOTSUPP) {
1921                                                 if (usevget) {
1922                                                         usevget = 0;
1923                                                         cn.cn_nameiop = LOOKUP;
1924                                                         cn.cn_lkflags =
1925                                                             LK_EXCLUSIVE |
1926                                                             LK_RETRY;
1927                                                         cn.cn_cred =
1928                                                             nd->nd_cred;
1929                                                         cn.cn_thread = p;
1930                                                 }
1931                                                 cn.cn_nameptr = dp->d_name;
1932                                                 cn.cn_namelen = nlen;
1933                                                 cn.cn_flags = ISLASTCN |
1934                                                     NOFOLLOW | LOCKLEAF |
1935                                                     MPSAFE;
1936                                                 if (nlen == 2 &&
1937                                                     dp->d_name[0] == '.' &&
1938                                                     dp->d_name[1] == '.')
1939                                                         cn.cn_flags |=
1940                                                             ISDOTDOT;
1941                                                 if (!VOP_ISLOCKED(vp))
1942                                                         vn_lock(vp,
1943                                                             LK_EXCLUSIVE |
1944                                                             LK_RETRY);
1945                                                 if ((vp->v_vflag & VV_ROOT) != 0
1946                                                     && (cn.cn_flags & ISDOTDOT)
1947                                                     != 0) {
1948                                                         vref(vp);
1949                                                         nvp = vp;
1950                                                         r = 0;
1951                                                 } else
1952                                                         r = VOP_LOOKUP(vp, &nvp,
1953                                                             &cn);
1954                                         }
1955                                 }
1956                                 if (!r) {
1957                                     if (refp == NULL &&
1958                                         ((nd->nd_flag & ND_NFSV3) ||
1959                                          NFSNONZERO_ATTRBIT(&attrbits))) {
1960                                         r = nfsvno_getfh(nvp, &nfh, p);
1961                                         if (!r)
1962                                             r = nfsvno_getattr(nvp, nvap,
1963                                                 nd->nd_cred, p);
1964                                     }
1965                                 } else {
1966                                     nvp = NULL;
1967                                 }
1968                                 if (r) {
1969                                         if (!NFSISSET_ATTRBIT(&attrbits,
1970                                             NFSATTRBIT_RDATTRERROR)) {
1971                                                 if (nvp != NULL)
1972                                                         vput(nvp);
1973                                                 nd->nd_repstat = r;
1974                                                 break;
1975                                         }
1976                                 }
1977                         }
1978
1979                         /*
1980                          * Build the directory record xdr
1981                          */
1982                         if (nd->nd_flag & ND_NFSV3) {
1983                                 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1984                                 *tl++ = newnfs_true;
1985                                 *tl++ = 0;
1986                                 *tl = txdr_unsigned(dp->d_fileno);
1987                                 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
1988                                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1989                                 *tl++ = 0;
1990                                 *tl = txdr_unsigned(*cookiep);
1991                                 nfsrv_postopattr(nd, 0, nvap);
1992                                 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
1993                                 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
1994                                 if (nvp != NULL)
1995                                         vput(nvp);
1996                         } else {
1997                                 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1998                                 *tl++ = newnfs_true;
1999                                 *tl++ = 0;
2000                                 *tl = txdr_unsigned(*cookiep);
2001                                 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2002                                 if (nvp != NULL)
2003                                         NFSVOPUNLOCK(nvp, 0, p);
2004                                 if (refp != NULL) {
2005                                         dirlen += nfsrv_putreferralattr(nd,
2006                                             &savbits, refp, 0,
2007                                             &nd->nd_repstat);
2008                                         if (nd->nd_repstat) {
2009                                                 if (nvp != NULL)
2010                                                         vrele(nvp);
2011                                                 break;
2012                                         }
2013                                 } else if (r) {
2014                                         dirlen += nfsvno_fillattr(nd, nvp, nvap,
2015                                             &nfh, r, &rderrbits, nd->nd_cred,
2016                                             p, isdgram, 0);
2017                                 } else {
2018                                         dirlen += nfsvno_fillattr(nd, nvp, nvap,
2019                                             &nfh, r, &attrbits, nd->nd_cred,
2020                                             p, isdgram, 0);
2021                                 }
2022                                 if (nvp != NULL)
2023                                         vrele(nvp);
2024                                 dirlen += (3 * NFSX_UNSIGNED);
2025                         }
2026                         if (dirlen <= cnt)
2027                                 entrycnt++;
2028                 }
2029                 cpos += dp->d_reclen;
2030                 dp = (struct dirent *)cpos;
2031                 cookiep++;
2032                 ncookies--;
2033         }
2034         if (!usevget && VOP_ISLOCKED(vp))
2035                 vput(vp);
2036         else
2037                 vrele(vp);
2038
2039         /*
2040          * If dirlen > cnt, we must strip off the last entry. If that
2041          * results in an empty reply, report NFSERR_TOOSMALL.
2042          */
2043         if (dirlen > cnt || nd->nd_repstat) {
2044                 if (!nd->nd_repstat && entrycnt == 0)
2045                         nd->nd_repstat = NFSERR_TOOSMALL;
2046                 if (nd->nd_repstat)
2047                         newnfs_trimtrailing(nd, mb0, bpos0);
2048                 else
2049                         newnfs_trimtrailing(nd, mb1, bpos1);
2050                 eofflag = 0;
2051         } else if (cpos < cend)
2052                 eofflag = 0;
2053         if (!nd->nd_repstat) {
2054                 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2055                 *tl++ = newnfs_false;
2056                 if (eofflag)
2057                         *tl = newnfs_true;
2058                 else
2059                         *tl = newnfs_false;
2060         }
2061         FREE((caddr_t)cookies, M_TEMP);
2062         FREE((caddr_t)rbuf, M_TEMP);
2063         return (0);
2064 nfsmout:
2065         vput(vp);
2066         return (error);
2067 }
2068
2069 /*
2070  * Get the settable attributes out of the mbuf list.
2071  * (Return 0 or EBADRPC)
2072  */
2073 int
2074 nfsrv_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2075     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2076 {
2077         u_int32_t *tl;
2078         struct nfsv2_sattr *sp;
2079         struct timeval curtime;
2080         int error = 0, toclient = 0;
2081
2082         switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2083         case ND_NFSV2:
2084                 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2085                 /*
2086                  * Some old clients didn't fill in the high order 16bits.
2087                  * --> check the low order 2 bytes for 0xffff
2088                  */
2089                 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2090                         nvap->na_mode = nfstov_mode(sp->sa_mode);
2091                 if (sp->sa_uid != newnfs_xdrneg1)
2092                         nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2093                 if (sp->sa_gid != newnfs_xdrneg1)
2094                         nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2095                 if (sp->sa_size != newnfs_xdrneg1)
2096                         nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2097                 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2098 #ifdef notyet
2099                         fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2100 #else
2101                         nvap->na_atime.tv_sec =
2102                                 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2103                         nvap->na_atime.tv_nsec = 0;
2104 #endif
2105                 }
2106                 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2107                         fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2108                 break;
2109         case ND_NFSV3:
2110                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2111                 if (*tl == newnfs_true) {
2112                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2113                         nvap->na_mode = nfstov_mode(*tl);
2114                 }
2115                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2116                 if (*tl == newnfs_true) {
2117                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2118                         nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2119                 }
2120                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2121                 if (*tl == newnfs_true) {
2122                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2123                         nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2124                 }
2125                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2126                 if (*tl == newnfs_true) {
2127                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2128                         nvap->na_size = fxdr_hyper(tl);
2129                 }
2130                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2131                 switch (fxdr_unsigned(int, *tl)) {
2132                 case NFSV3SATTRTIME_TOCLIENT:
2133                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2134                         fxdr_nfsv3time(tl, &nvap->na_atime);
2135                         toclient = 1;
2136                         break;
2137                 case NFSV3SATTRTIME_TOSERVER:
2138                         NFSGETTIME(&curtime);
2139                         nvap->na_atime.tv_sec = curtime.tv_sec;
2140                         nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2141                         nvap->na_vaflags |= VA_UTIMES_NULL;
2142                         break;
2143                 };
2144                 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2145                 switch (fxdr_unsigned(int, *tl)) {
2146                 case NFSV3SATTRTIME_TOCLIENT:
2147                         NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2148                         fxdr_nfsv3time(tl, &nvap->na_mtime);
2149                         nvap->na_vaflags &= ~VA_UTIMES_NULL;
2150                         break;
2151                 case NFSV3SATTRTIME_TOSERVER:
2152                         NFSGETTIME(&curtime);
2153                         nvap->na_mtime.tv_sec = curtime.tv_sec;
2154                         nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2155                         if (!toclient)
2156                                 nvap->na_vaflags |= VA_UTIMES_NULL;
2157                         break;
2158                 };
2159                 break;
2160         case ND_NFSV4:
2161                 error = nfsv4_sattr(nd, nvap, attrbitp, aclp, p);
2162         };
2163 nfsmout:
2164         return (error);
2165 }
2166
2167 /*
2168  * Handle the setable attributes for V4.
2169  * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2170  */
2171 int
2172 nfsv4_sattr(struct nfsrv_descript *nd, struct nfsvattr *nvap,
2173     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2174 {
2175         u_int32_t *tl;
2176         int attrsum = 0;
2177         int i, j;
2178         int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2179         int toclient = 0;
2180         u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2181         uid_t uid;
2182         gid_t gid;
2183         struct timeval curtime;
2184
2185         error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2186         if (error)
2187                 return (error);
2188         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2189         attrsize = fxdr_unsigned(int, *tl);
2190
2191         /*
2192          * Loop around getting the setable attributes. If an unsupported
2193          * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2194          */
2195         if (retnotsup) {
2196                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2197                 bitpos = NFSATTRBIT_MAX;
2198         } else {
2199                 bitpos = 0;
2200         }
2201         for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2202             if (attrsum > attrsize) {
2203                 error = NFSERR_BADXDR;
2204                 goto nfsmout;
2205             }
2206             if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2207                 switch (bitpos) {
2208                 case NFSATTRBIT_SIZE:
2209                         NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2210                         nvap->na_size = fxdr_hyper(tl);
2211                         attrsum += NFSX_HYPER;
2212                         break;
2213                 case NFSATTRBIT_ACL:
2214                         error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2215                             p);
2216                         if (error)
2217                                 goto nfsmout;
2218                         if (aceerr && !nd->nd_repstat)
2219                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2220                         attrsum += aclsize;
2221                         break;
2222                 case NFSATTRBIT_ARCHIVE:
2223                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2224                         if (!nd->nd_repstat)
2225                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2226                         attrsum += NFSX_UNSIGNED;
2227                         break;
2228                 case NFSATTRBIT_HIDDEN:
2229                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2230                         if (!nd->nd_repstat)
2231                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2232                         attrsum += NFSX_UNSIGNED;
2233                         break;
2234                 case NFSATTRBIT_MIMETYPE:
2235                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2236                         i = fxdr_unsigned(int, *tl);
2237                         error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2238                         if (error)
2239                                 goto nfsmout;
2240                         if (!nd->nd_repstat)
2241                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2242                         attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2243                         break;
2244                 case NFSATTRBIT_MODE:
2245                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2246                         nvap->na_mode = nfstov_mode(*tl);
2247                         attrsum += NFSX_UNSIGNED;
2248                         break;
2249                 case NFSATTRBIT_OWNER:
2250                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2251                         j = fxdr_unsigned(int, *tl);
2252                         if (j < 0)
2253                                 return (NFSERR_BADXDR);
2254                         if (j > NFSV4_SMALLSTR)
2255                                 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2256                         else
2257                                 cp = namestr;
2258                         error = nfsrv_mtostr(nd, cp, j);
2259                         if (error) {
2260                                 if (j > NFSV4_SMALLSTR)
2261                                         free(cp, M_NFSSTRING);
2262                                 return (error);
2263                         }
2264                         if (!nd->nd_repstat) {
2265                                 nd->nd_repstat = nfsv4_strtouid(cp,j,&uid,p);
2266                                 if (!nd->nd_repstat)
2267                                         nvap->na_uid = uid;
2268                         }
2269                         if (j > NFSV4_SMALLSTR)
2270                                 free(cp, M_NFSSTRING);
2271                         attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2272                         break;
2273                 case NFSATTRBIT_OWNERGROUP:
2274                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2275                         j = fxdr_unsigned(int, *tl);
2276                         if (j < 0)
2277                                 return (NFSERR_BADXDR);
2278                         if (j > NFSV4_SMALLSTR)
2279                                 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2280                         else
2281                                 cp = namestr;
2282                         error = nfsrv_mtostr(nd, cp, j);
2283                         if (error) {
2284                                 if (j > NFSV4_SMALLSTR)
2285                                         free(cp, M_NFSSTRING);
2286                                 return (error);
2287                         }
2288                         if (!nd->nd_repstat) {
2289                                 nd->nd_repstat = nfsv4_strtogid(cp,j,&gid,p);
2290                                 if (!nd->nd_repstat)
2291                                         nvap->na_gid = gid;
2292                         }
2293                         if (j > NFSV4_SMALLSTR)
2294                                 free(cp, M_NFSSTRING);
2295                         attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2296                         break;
2297                 case NFSATTRBIT_SYSTEM:
2298                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2299                         if (!nd->nd_repstat)
2300                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2301                         attrsum += NFSX_UNSIGNED;
2302                         break;
2303                 case NFSATTRBIT_TIMEACCESSSET:
2304                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2305                         attrsum += NFSX_UNSIGNED;
2306                         if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2307                             NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2308                             fxdr_nfsv4time(tl, &nvap->na_atime);
2309                             toclient = 1;
2310                             attrsum += NFSX_V4TIME;
2311                         } else {
2312                             NFSGETTIME(&curtime);
2313                             nvap->na_atime.tv_sec = curtime.tv_sec;
2314                             nvap->na_atime.tv_nsec = curtime.tv_usec * 1000;
2315                             nvap->na_vaflags |= VA_UTIMES_NULL;
2316                         }
2317                         break;
2318                 case NFSATTRBIT_TIMEBACKUP:
2319                         NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2320                         if (!nd->nd_repstat)
2321                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2322                         attrsum += NFSX_V4TIME;
2323                         break;
2324                 case NFSATTRBIT_TIMECREATE:
2325                         NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2326                         if (!nd->nd_repstat)
2327                                 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2328                         attrsum += NFSX_V4TIME;
2329                         break;
2330                 case NFSATTRBIT_TIMEMODIFYSET:
2331                         NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2332                         attrsum += NFSX_UNSIGNED;
2333                         if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2334                             NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2335                             fxdr_nfsv4time(tl, &nvap->na_mtime);
2336                             nvap->na_vaflags &= ~VA_UTIMES_NULL;
2337                             attrsum += NFSX_V4TIME;
2338                         } else {
2339                             NFSGETTIME(&curtime);
2340                             nvap->na_mtime.tv_sec = curtime.tv_sec;
2341                             nvap->na_mtime.tv_nsec = curtime.tv_usec * 1000;
2342                             if (!toclient)
2343                                 nvap->na_vaflags |= VA_UTIMES_NULL;
2344                         }
2345                         break;
2346                 default:
2347                         nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2348                         /*
2349                          * set bitpos so we drop out of the loop.
2350                          */
2351                         bitpos = NFSATTRBIT_MAX;
2352                         break;
2353                 };
2354         }
2355
2356         /*
2357          * some clients pad the attrlist, so we need to skip over the
2358          * padding.
2359          */
2360         if (attrsum > attrsize) {
2361                 error = NFSERR_BADXDR;
2362         } else {
2363                 attrsize = NFSM_RNDUP(attrsize);
2364                 if (attrsum < attrsize)
2365                         error = nfsm_advance(nd, attrsize - attrsum, -1);
2366         }
2367 nfsmout:
2368         return (error);
2369 }
2370
2371 /*
2372  * Check/setup export credentials.
2373  */
2374 int
2375 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2376     struct ucred *credanon)
2377 {
2378         int error = 0;
2379
2380         /*
2381          * Check/setup credentials.
2382          */
2383         if (nd->nd_flag & ND_GSS)
2384                 exp->nes_exflag &= ~MNT_EXPORTANON;
2385
2386         /*
2387          * Check to see if the operation is allowed for this security flavor.
2388          * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2389          * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2390          * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2391          */
2392         if (nfsvno_testexp(nd, exp) &&
2393             nd->nd_procnum != NFSV4OP_SECINFO &&
2394             nd->nd_procnum != NFSPROC_FSINFO) {
2395                 if (nd->nd_flag & ND_NFSV4)
2396                         error = NFSERR_WRONGSEC;
2397                 else
2398                         error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2399                 return (error);
2400         }
2401
2402         /*
2403          * Check to see if the file system is exported V4 only.
2404          */
2405         if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4))
2406                 return (NFSERR_PROGNOTV4);
2407
2408         /*
2409          * Now, map the user credentials.
2410          * (Note that ND_AUTHNONE will only be set for an NFSv3
2411          *  Fsinfo RPC. If set for anything else, this code might need
2412          *  to change.)
2413          */
2414         if (NFSVNO_EXPORTED(exp) &&
2415             ((!(nd->nd_flag & ND_GSS) && nd->nd_cred->cr_uid == 0) ||
2416              NFSVNO_EXPORTANON(exp) ||
2417              (nd->nd_flag & ND_AUTHNONE))) {
2418                 nd->nd_cred->cr_uid = credanon->cr_uid;
2419                 nd->nd_cred->cr_gid = credanon->cr_gid;
2420                 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2421                     credanon->cr_groups);
2422         }
2423         return (0);
2424 }
2425
2426 /*
2427  * Check exports.
2428  */
2429 int
2430 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2431     struct ucred **credp)
2432 {
2433         int i, error, *secflavors;
2434
2435         error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2436             &exp->nes_numsecflavor, &secflavors);
2437         if (error) {
2438                 if (nfs_rootfhset) {
2439                         exp->nes_exflag = 0;
2440                         exp->nes_numsecflavor = 0;
2441                         error = 0;
2442                 }
2443         } else {
2444                 /* Copy the security flavors. */
2445                 for (i = 0; i < exp->nes_numsecflavor; i++)
2446                         exp->nes_secflavors[i] = secflavors[i];
2447         }
2448         return (error);
2449 }
2450
2451 /*
2452  * Get a vnode for a file handle and export stuff.
2453  */
2454 int
2455 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2456     struct vnode **vpp, struct nfsexstuff *exp, struct ucred **credp)
2457 {
2458         int i, error, *secflavors;
2459
2460         *credp = NULL;
2461         exp->nes_numsecflavor = 0;
2462         error = VFS_FHTOVP(mp, &fhp->fh_fid, vpp);
2463         if (error != 0)
2464                 /* Make sure the server replies ESTALE to the client. */
2465                 error = ESTALE;
2466         if (nam && !error) {
2467                 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2468                     &exp->nes_numsecflavor, &secflavors);
2469                 if (error) {
2470                         if (nfs_rootfhset) {
2471                                 exp->nes_exflag = 0;
2472                                 exp->nes_numsecflavor = 0;
2473                                 error = 0;
2474                         } else {
2475                                 vput(*vpp);
2476                         }
2477                 } else {
2478                         /* Copy the security flavors. */
2479                         for (i = 0; i < exp->nes_numsecflavor; i++)
2480                                 exp->nes_secflavors[i] = secflavors[i];
2481                 }
2482         }
2483         return (error);
2484 }
2485
2486 /*
2487  * Do the pathconf vnode op.
2488  */
2489 int
2490 nfsvno_pathconf(struct vnode *vp, int flag, register_t *retf,
2491     struct ucred *cred, struct thread *p)
2492 {
2493         int error;
2494
2495         error = VOP_PATHCONF(vp, flag, retf);
2496         return (error);
2497 }
2498
2499 /*
2500  * nfsd_fhtovp() - convert a fh to a vnode ptr
2501  *      - look up fsid in mount list (if not found ret error)
2502  *      - get vp and export rights by calling nfsvno_fhtovp()
2503  *      - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2504  *        for AUTH_SYS
2505  * Also handle getting the Giant lock for the file system,
2506  * as required:
2507  * - if same mount point as *mpp
2508  *       do nothing
2509  *   else if *mpp == NULL
2510  *       if already locked
2511  *           leave it locked
2512  *       else
2513  *           call VFS_LOCK_GIANT()
2514  *   else
2515  *       if already locked
2516  *            unlock Giant
2517  *       call VFS_LOCK_GIANT()
2518  */
2519 void
2520 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp,
2521     struct vnode **vpp, struct nfsexstuff *exp,
2522     struct mount **mpp, int startwrite, struct thread *p)
2523 {
2524         struct mount *mp;
2525         struct ucred *credanon;
2526         fhandle_t *fhp;
2527
2528         fhp = (fhandle_t *)nfp->nfsrvfh_data;
2529         /*
2530          * Check for the special case of the nfsv4root_fh.
2531          */
2532         mp = vfs_getvfs(&fhp->fh_fsid);
2533         if (!mp) {
2534                 *vpp = NULL;
2535                 nd->nd_repstat = ESTALE;
2536                 if (*mpp && exp->nes_vfslocked)
2537                         VFS_UNLOCK_GIANT(*mpp);
2538                 *mpp = NULL;
2539                 exp->nes_vfslocked = 0;
2540                 return;
2541         }
2542
2543         /*
2544          * Now, handle Giant for the file system.
2545          */
2546         if (*mpp != NULL && *mpp != mp && exp->nes_vfslocked) {
2547                 VFS_UNLOCK_GIANT(*mpp);
2548                 exp->nes_vfslocked = 0;
2549         }
2550         if (!exp->nes_vfslocked && *mpp != mp)
2551                 exp->nes_vfslocked = VFS_LOCK_GIANT(mp);
2552
2553         *mpp = mp;
2554         if (startwrite)
2555                 vn_start_write(NULL, mpp, V_WAIT);
2556
2557         nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, vpp, exp,
2558             &credanon);
2559
2560         /*
2561          * For NFSv4 without a pseudo root fs, unexported file handles
2562          * can be returned, so that Lookup works everywhere.
2563          */
2564         if (!nd->nd_repstat && exp->nes_exflag == 0 &&
2565             !(nd->nd_flag & ND_NFSV4)) {
2566                 vput(*vpp);
2567                 nd->nd_repstat = EACCES;
2568         }
2569
2570         /*
2571          * Personally, I've never seen any point in requiring a
2572          * reserved port#, since only in the rare case where the
2573          * clients are all boxes with secure system priviledges,
2574          * does it provide any enhanced security, but... some people
2575          * believe it to be useful and keep putting this code back in.
2576          * (There is also some "security checker" out there that
2577          *  complains if the nfs server doesn't enforce this.)
2578          * However, note the following:
2579          * RFC3530 (NFSv4) specifies that a reserved port# not be
2580          *      required.
2581          * RFC2623 recommends that, if a reserved port# is checked for,
2582          *      that there be a way to turn that off--> ifdef'd.
2583          */
2584 #ifdef NFS_REQRSVPORT
2585         if (!nd->nd_repstat) {
2586                 struct sockaddr_in *saddr;
2587                 struct sockaddr_in6 *saddr6;
2588
2589                 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
2590                 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
2591                 if (!(nd->nd_flag & ND_NFSV4) &&
2592                     ((saddr->sin_family == AF_INET &&
2593                       ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
2594                      (saddr6->sin6_family == AF_INET6 &&
2595                       ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
2596                         vput(*vpp);
2597                         nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2598                 }
2599         }
2600 #endif  /* NFS_REQRSVPORT */
2601
2602         /*
2603          * Check/setup credentials.
2604          */
2605         if (!nd->nd_repstat) {
2606                 nd->nd_saveduid = nd->nd_cred->cr_uid;
2607                 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
2608                 if (nd->nd_repstat)
2609                         vput(*vpp);
2610         }
2611         if (credanon != NULL)
2612                 crfree(credanon);
2613         if (nd->nd_repstat) {
2614                 if (startwrite)
2615                         vn_finished_write(mp);
2616                 if (exp->nes_vfslocked) {
2617                         VFS_UNLOCK_GIANT(mp);
2618                         exp->nes_vfslocked = 0;
2619                 }
2620                 vfs_rel(mp);
2621                 *vpp = NULL;
2622                 *mpp = NULL;
2623         } else {
2624                 vfs_rel(mp);
2625         }
2626 }
2627
2628 /*
2629  * glue for fp.
2630  */
2631 int
2632 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
2633 {
2634         struct filedesc *fdp;
2635         struct file *fp;
2636
2637         fdp = p->td_proc->p_fd;
2638         if (fd >= fdp->fd_nfiles ||
2639             (fp = fdp->fd_ofiles[fd]) == NULL)
2640                 return (EBADF);
2641         *fpp = fp;
2642         return (0);
2643 }
2644
2645 /*
2646  * Called from nfssvc() to update the exports list. Just call
2647  * vfs_export(). This has to be done, since the v4 root fake fs isn't
2648  * in the mount list.
2649  */
2650 int
2651 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
2652 {
2653         struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
2654         int error;
2655         struct nameidata nd;
2656         fhandle_t fh;
2657
2658         error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
2659         if ((nfsexargp->export.ex_flags & MNT_DELEXPORT)) {
2660                 nfs_rootfhset = 0;
2661                 nfsv4root_set = 0;
2662         } else if (error == 0) {
2663                 if (nfsexargp->fspec == NULL)
2664                         return (EPERM);
2665                 /*
2666                  * If fspec != NULL, this is the v4root path.
2667                  */
2668                 NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_USERSPACE,
2669                     nfsexargp->fspec, p);
2670                 if ((error = namei(&nd)) != 0)
2671                         return (error);
2672                 error = nfsvno_getfh(nd.ni_vp, &fh, p);
2673                 vrele(nd.ni_vp);
2674                 if (!error) {
2675                         nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
2676                         NFSBCOPY((caddr_t)&fh,
2677                             nfs_rootfh.nfsrvfh_data,
2678                             sizeof (fhandle_t));
2679                         nfs_rootfhset = 1;
2680                 }
2681         }
2682         return (error);
2683 }
2684
2685 /*
2686  * Get the tcp socket sequence numbers we need.
2687  * (Maybe this should be moved to the tcp sources?)
2688  */
2689 int
2690 nfsrv_getsocksndseq(struct socket *so, tcp_seq *maxp, tcp_seq *unap)
2691 {
2692         struct inpcb *inp;
2693         struct tcpcb *tp;
2694
2695         inp = sotoinpcb(so);
2696         KASSERT(inp != NULL, ("nfsrv_getsocksndseq: inp == NULL"));
2697         INP_RLOCK(inp);
2698         if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2699                 INP_RUNLOCK(inp);
2700                 return (EPIPE);
2701         }
2702         tp = intotcpcb(inp);
2703         if (tp->t_state != TCPS_ESTABLISHED) {
2704                 INP_RUNLOCK(inp);
2705                 return (EPIPE);
2706         }
2707         *maxp = tp->snd_max;
2708         *unap = tp->snd_una;
2709         INP_RUNLOCK(inp);
2710         return (0);
2711 }
2712
2713 /*
2714  * This function needs to test to see if the system is near its limit
2715  * for memory allocation via malloc() or mget() and return True iff
2716  * either of these resources are near their limit.
2717  * XXX (For now, this is just a stub.)
2718  */
2719 int nfsrv_testmalloclimit = 0;
2720 int
2721 nfsrv_mallocmget_limit(void)
2722 {
2723         static int printmesg = 0;
2724         static int testval = 1;
2725
2726         if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
2727                 if ((printmesg++ % 100) == 0)
2728                         printf("nfsd: malloc/mget near limit\n");
2729                 return (1);
2730         }
2731         return (0);
2732 }
2733
2734 /*
2735  * BSD specific initialization of a mount point.
2736  */
2737 void
2738 nfsd_mntinit(void)
2739 {
2740         static int inited = 0;
2741
2742         if (inited)
2743                 return;
2744         inited = 1;
2745         nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
2746         TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
2747         nfsv4root_mnt.mnt_export = NULL;
2748         TAILQ_INIT(&nfsv4root_opt);
2749         TAILQ_INIT(&nfsv4root_newopt);
2750         nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
2751         nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
2752         nfsv4root_mnt.mnt_nvnodelistsize = 0;
2753 }
2754
2755 /*
2756  * Get a vnode for a file handle, without checking exports, etc.
2757  */
2758 struct vnode *
2759 nfsvno_getvp(fhandle_t *fhp)
2760 {
2761         struct mount *mp;
2762         struct vnode *vp;
2763         int error;
2764
2765         mp = vfs_getvfs(&fhp->fh_fsid);
2766         if (mp == NULL)
2767                 return (NULL);
2768         error = VFS_FHTOVP(mp, &fhp->fh_fid, &vp);
2769         if (error)
2770                 return (NULL);
2771         return (vp);
2772 }
2773
2774 /*
2775  * Check to see it a byte range lock held by a process running
2776  * locally on the server conflicts with the new lock.
2777  */
2778 int
2779 nfsvno_localconflict(struct vnode *vp, int ftype, u_int64_t first,
2780     u_int64_t end, struct nfslockconflict *cfp, struct thread *td)
2781 {
2782         int error;
2783         struct flock fl;
2784
2785         if (!nfsrv_dolocallocks)
2786                 return (0);
2787         fl.l_whence = SEEK_SET;
2788         fl.l_type = ftype;
2789         fl.l_start = (off_t)first;
2790         if (end == NFS64BITSSET)
2791                 fl.l_len = 0;
2792         else
2793                 fl.l_len = (off_t)(end - first);
2794         /*
2795          * For FreeBSD8, the l_pid and l_sysid must be set to the same
2796          * values for all calls, so that all locks will be held by the
2797          * nfsd server. (The nfsd server handles conflicts between the
2798          * various clients.)
2799          * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2800          * bytes, so it can't be put in l_sysid.
2801          */
2802         if (nfsv4_sysid == 0)
2803                 nfsv4_sysid = nlm_acquire_next_sysid();
2804         fl.l_pid = (pid_t)0;
2805         fl.l_sysid = (int)nfsv4_sysid;
2806
2807         NFSVOPUNLOCK(vp, 0, td);
2808         error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_GETLK, &fl,
2809             (F_POSIX | F_REMOTE));
2810         NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2811         if (error)
2812                 return (error);
2813         if (fl.l_type == F_UNLCK)
2814                 return (0);
2815         if (cfp != NULL) {
2816                 cfp->cl_clientid.lval[0] = cfp->cl_clientid.lval[1] = 0;
2817                 cfp->cl_first = (u_int64_t)fl.l_start;
2818                 if (fl.l_len == 0)
2819                         cfp->cl_end = NFS64BITSSET;
2820                 else
2821                         cfp->cl_end = (u_int64_t)
2822                             (fl.l_start + fl.l_len);
2823                 if (fl.l_type == F_WRLCK)
2824                         cfp->cl_flags = NFSLCK_WRITE;
2825                 else
2826                         cfp->cl_flags = NFSLCK_READ;
2827                 sprintf(cfp->cl_owner, "LOCALID%d", fl.l_pid);
2828                 cfp->cl_ownerlen = strlen(cfp->cl_owner);
2829                 return (NFSERR_DENIED);
2830         }
2831         return (NFSERR_INVAL);
2832 }
2833
2834 /*
2835  * Do a local VOP_ADVLOCK().
2836  */
2837 int
2838 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
2839     u_int64_t end, struct thread *td)
2840 {
2841         int error;
2842         struct flock fl;
2843         u_int64_t tlen;
2844
2845         if (nfsrv_dolocallocks == 0)
2846                 return (0);
2847         fl.l_whence = SEEK_SET;
2848         fl.l_type = ftype;
2849         fl.l_start = (off_t)first;
2850         if (end == NFS64BITSSET) {
2851                 fl.l_len = 0;
2852         } else {
2853                 tlen = end - first;
2854                 fl.l_len = (off_t)tlen;
2855         }
2856         /*
2857          * For FreeBSD8, the l_pid and l_sysid must be set to the same
2858          * values for all calls, so that all locks will be held by the
2859          * nfsd server. (The nfsd server handles conflicts between the
2860          * various clients.)
2861          * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
2862          * bytes, so it can't be put in l_sysid.
2863          */
2864         if (nfsv4_sysid == 0)
2865                 nfsv4_sysid = nlm_acquire_next_sysid();
2866         fl.l_pid = (pid_t)0;
2867         fl.l_sysid = (int)nfsv4_sysid;
2868
2869         NFSVOPUNLOCK(vp, 0, td);
2870         if (ftype == F_UNLCK)
2871                 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
2872                     (F_POSIX | F_REMOTE));
2873         else
2874                 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
2875                     (F_POSIX | F_REMOTE));
2876         NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY, td);
2877         return (error);
2878 }
2879
2880 /*
2881  * Unlock an underlying local file system.
2882  */
2883 void
2884 nfsvno_unlockvfs(struct mount *mp)
2885 {
2886
2887         VFS_UNLOCK_GIANT(mp);
2888 }
2889
2890 /*
2891  * Lock an underlying file system, as required, and return
2892  * whether or not it is locked.
2893  */
2894 int
2895 nfsvno_lockvfs(struct mount *mp)
2896 {
2897         int ret;
2898
2899         ret = VFS_LOCK_GIANT(mp);
2900         return (ret);
2901 }
2902
2903 /*
2904  * Check the nfsv4 root exports.
2905  */
2906 int
2907 nfsvno_v4rootexport(struct nfsrv_descript *nd)
2908 {
2909         struct ucred *credanon;
2910         int exflags, error, numsecflavor, *secflavors, i;
2911
2912         error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
2913             &credanon, &numsecflavor, &secflavors);
2914         if (error)
2915                 return (NFSERR_PROGUNAVAIL);
2916         if (credanon != NULL)
2917                 crfree(credanon);
2918         for (i = 0; i < numsecflavor; i++) {
2919                 if (secflavors[i] == AUTH_SYS)
2920                         nd->nd_flag |= ND_EXAUTHSYS;
2921                 else if (secflavors[i] == RPCSEC_GSS_KRB5)
2922                         nd->nd_flag |= ND_EXGSS;
2923                 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
2924                         nd->nd_flag |= ND_EXGSSINTEGRITY;
2925                 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
2926                         nd->nd_flag |= ND_EXGSSPRIVACY;
2927         }
2928         return (0);
2929 }
2930
2931 /*
2932  * Nfs server psuedo system call for the nfsd's
2933  */
2934 /*
2935  * MPSAFE
2936  */
2937 static int
2938 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
2939 {
2940         struct file *fp;
2941         struct nfsd_addsock_args sockarg;
2942         struct nfsd_nfsd_args nfsdarg;
2943         int error;
2944
2945         if (uap->flag & NFSSVC_NFSDADDSOCK) {
2946                 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
2947                 if (error)
2948                         return (error);
2949                 if ((error = fget(td, sockarg.sock, &fp)) != 0) {
2950                         return (error);
2951                 }
2952                 if (fp->f_type != DTYPE_SOCKET) {
2953                         fdrop(fp, td);
2954                         return (EPERM);
2955                 }
2956                 error = nfsrvd_addsock(fp);
2957                 fdrop(fp, td);
2958         } else if (uap->flag & NFSSVC_NFSDNFSD) {
2959                 if (uap->argp == NULL) 
2960                         return (EINVAL);
2961                 error = copyin(uap->argp, (caddr_t)&nfsdarg,
2962                     sizeof (nfsdarg));
2963                 if (error)
2964                         return (error);
2965                 error = nfsrvd_nfsd(td, &nfsdarg);
2966         } else {
2967                 error = nfssvc_srvcall(td, uap, td->td_ucred);
2968         }
2969         return (error);
2970 }
2971
2972 static int
2973 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
2974 {
2975         struct nfsex_args export;
2976         struct file *fp = NULL;
2977         int stablefd, len;
2978         struct nfsd_clid adminrevoke;
2979         struct nfsd_dumplist dumplist;
2980         struct nfsd_dumpclients *dumpclients;
2981         struct nfsd_dumplocklist dumplocklist;
2982         struct nfsd_dumplocks *dumplocks;
2983         struct nameidata nd;
2984         vnode_t vp;
2985         int error = EINVAL;
2986
2987         if (uap->flag & NFSSVC_PUBLICFH) {
2988                 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
2989                     sizeof (fhandle_t));
2990                 error = copyin(uap->argp,
2991                     &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
2992                 if (!error)
2993                         nfs_pubfhset = 1;
2994         } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
2995                 error = copyin(uap->argp,(caddr_t)&export,
2996                     sizeof (struct nfsex_args));
2997                 if (!error)
2998                         error = nfsrv_v4rootexport(&export, cred, p);
2999         } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3000                 nfs_pubfhset = 0;
3001                 error = 0;
3002         } else if (uap->flag & NFSSVC_STABLERESTART) {
3003                 error = copyin(uap->argp, (caddr_t)&stablefd,
3004                     sizeof (int));
3005                 if (!error)
3006                         error = fp_getfvp(p, stablefd, &fp, &vp);
3007                 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3008                         error = EBADF;
3009                 if (!error && newnfs_numnfsd != 0)
3010                         error = EPERM;
3011                 if (!error) {
3012                         nfsrv_stablefirst.nsf_fp = fp;
3013                         nfsrv_setupstable(p);
3014                 }
3015         } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3016                 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3017                     sizeof (struct nfsd_clid));
3018                 if (!error)
3019                         error = nfsrv_adminrevoke(&adminrevoke, p);
3020         } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3021                 error = copyin(uap->argp, (caddr_t)&dumplist,
3022                     sizeof (struct nfsd_dumplist));
3023                 if (!error && (dumplist.ndl_size < 1 ||
3024                         dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3025                         error = EPERM;
3026                 if (!error) {
3027                     len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3028                     dumpclients = (struct nfsd_dumpclients *)malloc(len,
3029                         M_TEMP, M_WAITOK);
3030                     nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3031                     error = copyout(dumpclients,
3032                         CAST_USER_ADDR_T(dumplist.ndl_list), len);
3033                     free((caddr_t)dumpclients, M_TEMP);
3034                 }
3035         } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3036                 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3037                     sizeof (struct nfsd_dumplocklist));
3038                 if (!error && (dumplocklist.ndllck_size < 1 ||
3039                         dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3040                         error = EPERM;
3041                 if (!error)
3042                         error = nfsrv_lookupfilename(&nd,
3043                                 dumplocklist.ndllck_fname, p);
3044                 if (!error) {
3045                         len = sizeof (struct nfsd_dumplocks) *
3046                                 dumplocklist.ndllck_size;
3047                         dumplocks = (struct nfsd_dumplocks *)malloc(len,
3048                                 M_TEMP, M_WAITOK);
3049                         nfsrv_dumplocks(nd.ni_vp, dumplocks,
3050                             dumplocklist.ndllck_size, p);
3051                         vput(nd.ni_vp);
3052                         error = copyout(dumplocks,
3053                             CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3054                         free((caddr_t)dumplocks, M_TEMP);
3055                 }
3056         }
3057         return (error);
3058 }
3059
3060 /*
3061  * Check exports.
3062  * Returns 0 if ok, 1 otherwise.
3063  */
3064 int
3065 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3066 {
3067         int i;
3068
3069         /*
3070          * This seems odd, but allow the case where the security flavor
3071          * list is empty. This happens when NFSv4 is traversing non-exported
3072          * file systems. Exported file systems should always have a non-empty
3073          * security flavor list.
3074          */
3075         if (exp->nes_numsecflavor == 0)
3076                 return (0);
3077
3078         for (i = 0; i < exp->nes_numsecflavor; i++) {
3079                 /*
3080                  * The tests for privacy and integrity must be first,
3081                  * since ND_GSS is set for everything but AUTH_SYS.
3082                  */
3083                 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3084                     (nd->nd_flag & ND_GSSPRIVACY))
3085                         return (0);
3086                 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3087                     (nd->nd_flag & ND_GSSINTEGRITY))
3088                         return (0);
3089                 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3090                     (nd->nd_flag & ND_GSS))
3091                         return (0);
3092                 if (exp->nes_secflavors[i] == AUTH_SYS &&
3093                     (nd->nd_flag & ND_GSS) == 0)
3094                         return (0);
3095         }
3096         return (1);
3097 }
3098
3099 /*
3100  * Calculate a hash value for the fid in a file handle.
3101  */
3102 uint32_t
3103 nfsrv_hashfh(fhandle_t *fhp)
3104 {
3105         uint32_t hashval;
3106
3107         hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3108         return (hashval);
3109 }
3110
3111 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
3112
3113 /*
3114  * Called once to initialize data structures...
3115  */
3116 static int
3117 nfsd_modevent(module_t mod, int type, void *data)
3118 {
3119         int error = 0;
3120         static int loaded = 0;
3121
3122         switch (type) {
3123         case MOD_LOAD:
3124                 if (loaded)
3125                         return (0);
3126                 newnfs_portinit();
3127                 mtx_init(&nfs_cache_mutex, "nfs_cache_mutex", NULL, MTX_DEF);
3128                 mtx_init(&nfs_v4root_mutex, "nfs_v4root_mutex", NULL, MTX_DEF);
3129                 mtx_init(&nfsv4root_mnt.mnt_mtx, "struct mount mtx", NULL,
3130                     MTX_DEF);
3131                 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
3132                 nfsrvd_initcache();
3133                 nfsd_init();
3134                 NFSD_LOCK();
3135                 nfsrvd_init(0);
3136                 NFSD_UNLOCK();
3137                 nfsd_mntinit();
3138 #ifdef VV_DISABLEDELEG
3139                 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
3140                 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
3141 #endif
3142                 nfsd_call_servertimer = nfsrv_servertimer;
3143                 nfsd_call_nfsd = nfssvc_nfsd;
3144                 loaded = 1;
3145                 break;
3146
3147         case MOD_UNLOAD:
3148                 if (newnfs_numnfsd != 0) {
3149                         error = EBUSY;
3150                         break;
3151                 }
3152
3153 #ifdef VV_DISABLEDELEG
3154                 vn_deleg_ops.vndeleg_recall = NULL;
3155                 vn_deleg_ops.vndeleg_disable = NULL;
3156 #endif
3157                 nfsd_call_servertimer = NULL;
3158                 nfsd_call_nfsd = NULL;
3159                 /* and get rid of the locks */
3160                 mtx_destroy(&nfs_cache_mutex);
3161                 mtx_destroy(&nfs_v4root_mutex);
3162                 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
3163                 lockdestroy(&nfsv4root_mnt.mnt_explock);
3164                 loaded = 0;
3165                 break;
3166         default:
3167                 error = EOPNOTSUPP;
3168                 break;
3169         }
3170         return error;
3171 }
3172 static moduledata_t nfsd_mod = {
3173         "nfsd",
3174         nfsd_modevent,
3175         NULL,
3176 };
3177 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
3178
3179 /* So that loader and kldload(2) can find us, wherever we are.. */
3180 MODULE_VERSION(nfsd, 1);
3181 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
3182 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
3183 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
3184 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
3185