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