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