2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/capsicum.h>
40 #include <sys/extattr.h>
43 * Functions that perform the vfs operations required by the routines in
44 * nfsd_serv.c. It is hoped that this change will make the server more
48 #include <fs/nfs/nfsport.h>
50 #include <sys/sysctl.h>
51 #include <nlm/nlm_prot.h>
54 FEATURE(nfsd, "NFSv4 server");
56 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
57 extern int nfsrv_useacl;
58 extern int newnfs_numnfsd;
59 extern struct mount nfsv4root_mnt;
60 extern struct nfsrv_stablefirst nfsrv_stablefirst;
61 extern void (*nfsd_call_servertimer)(void);
62 extern SVCPOOL *nfsrvd_pool;
63 extern struct nfsv4lock nfsd_suspend_lock;
64 extern struct nfsclienthashhead *nfsclienthash;
65 extern struct nfslockhashhead *nfslockhash;
66 extern struct nfssessionhash *nfssessionhash;
67 extern int nfsrv_sessionhashsize;
68 extern struct nfsstatsv1 nfsstatsv1;
69 extern struct nfslayouthash *nfslayouthash;
70 extern int nfsrv_layouthashsize;
71 extern struct mtx nfsrv_dslock_mtx;
72 extern int nfs_pnfsiothreads;
73 extern struct nfsdontlisthead nfsrv_dontlisthead;
74 extern volatile int nfsrv_dontlistlen;
75 extern volatile int nfsrv_devidcnt;
76 extern int nfsrv_maxpnfsmirror;
77 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
80 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
81 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
82 struct mtx nfsrc_udpmtx;
83 struct mtx nfs_v4root_mutex;
84 struct mtx nfsrv_dontlistlock_mtx;
85 struct mtx nfsrv_recalllock_mtx;
86 struct nfsrvfh nfs_rootfh, nfs_pubfh;
87 int nfs_pubfhset = 0, nfs_rootfhset = 0;
88 struct proc *nfsd_master_proc = NULL;
89 int nfsd_debuglevel = 0;
90 static pid_t nfsd_master_pid = (pid_t)-1;
91 static char nfsd_master_comm[MAXCOMLEN + 1];
92 static struct timeval nfsd_master_start;
93 static uint32_t nfsv4_sysid = 0;
94 static fhandle_t zerofh;
96 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
99 int nfsrv_enable_crossmntpt = 1;
100 static int nfs_commit_blks;
101 static int nfs_commit_miss;
102 extern int nfsrv_issuedelegs;
103 extern int nfsrv_dolocallocks;
104 extern int nfsd_enable_stringtouid;
105 extern struct nfsdevicehead nfsrv_devidhead;
107 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
109 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
110 int *, char *, fhandle_t *);
111 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
113 static int nfsrv_proxyds(struct nfsrv_descript *, struct vnode *, off_t, int,
114 struct ucred *, struct thread *, int, struct mbuf **, char *,
115 struct mbuf **, struct nfsvattr *, struct acl *);
116 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
117 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
118 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
119 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
120 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
122 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
123 struct vnode *, struct nfsmount **, int, struct acl *, int *);
124 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
125 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
126 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
127 struct vnode *, struct nfsmount *, struct nfsvattr *);
128 static int nfsrv_putfhname(fhandle_t *, char *);
129 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
130 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
131 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *,
132 struct vnode *, NFSPROC_T *);
133 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
134 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
136 static int nfsrv_pnfsstatfs(struct statfs *);
138 int nfs_pnfsio(task_fn_t *, void *);
140 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server");
141 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
142 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
143 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
145 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
147 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
148 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
149 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
150 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
151 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
152 0, "Debug level for NFS server");
153 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
154 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
155 static int nfsrv_pnfsgetdsattr = 1;
156 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
157 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
160 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
162 * The dsN subdirectories for the increased values must have been created
163 * on all DS servers before this increase is done.
165 u_int nfsrv_dsdirsize = 20;
167 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
169 int error, newdsdirsize;
171 newdsdirsize = nfsrv_dsdirsize;
172 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
173 if (error != 0 || req->newptr == NULL)
175 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
178 nfsrv_dsdirsize = newdsdirsize;
181 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize, CTLTYPE_UINT | CTLFLAG_RW, 0,
182 sizeof(nfsrv_dsdirsize), sysctl_dsdirsize, "IU",
183 "Number of dsN subdirs on the DS servers");
185 #define MAX_REORDERED_RPC 16
186 #define NUM_HEURISTIC 1031
187 #define NHUSE_INIT 64
189 #define NHUSE_MAX 2048
191 static struct nfsheur {
192 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
193 off_t nh_nextoff; /* next offset for sequential detection */
194 int nh_use; /* use count for selection */
195 int nh_seqcount; /* heuristic */
196 } nfsheur[NUM_HEURISTIC];
200 * Heuristic to detect sequential operation.
202 static struct nfsheur *
203 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
208 /* Locate best candidate. */
210 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
213 if (nfsheur[hi].nh_vp == vp) {
217 if (nfsheur[hi].nh_use > 0)
218 --nfsheur[hi].nh_use;
219 hi = (hi + 1) % NUM_HEURISTIC;
220 if (nfsheur[hi].nh_use < nh->nh_use)
224 /* Initialize hint if this is a new file. */
225 if (nh->nh_vp != vp) {
227 nh->nh_nextoff = uio->uio_offset;
228 nh->nh_use = NHUSE_INIT;
229 if (uio->uio_offset == 0)
235 /* Calculate heuristic. */
236 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
237 uio->uio_offset == nh->nh_nextoff) {
238 /* See comments in vfs_vnops.c:sequential_heuristic(). */
239 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
240 if (nh->nh_seqcount > IO_SEQMAX)
241 nh->nh_seqcount = IO_SEQMAX;
242 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
243 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
244 /* Probably a reordered RPC, leave seqcount alone. */
245 } else if (nh->nh_seqcount > 1) {
246 nh->nh_seqcount /= 2;
250 nh->nh_use += NHUSE_INC;
251 if (nh->nh_use > NHUSE_MAX)
252 nh->nh_use = NHUSE_MAX;
257 * Get attributes into nfsvattr structure.
260 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
261 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
262 nfsattrbit_t *attrbitp)
264 int error, gotattr, lockedit = 0;
267 if (vpislocked == 0) {
269 * When vpislocked == 0, the vnode is either exclusively
270 * locked by this thread or not locked by this thread.
271 * As such, shared lock it, if not exclusively locked.
273 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
275 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
280 * Acquire the Change, Size and TimeModify attributes, as required.
281 * This needs to be done for regular files if:
282 * - non-NFSv4 RPCs or
283 * - when attrbitp == NULL or
284 * - an NFSv4 RPC with any of the above attributes in attrbitp.
285 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
286 * these attributes. nfsrv_proxyds() will return an error if the
287 * server is not a pNFS one.
290 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
291 (nd->nd_flag & ND_NFSV4) == 0 ||
292 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
293 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
294 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
295 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY))) {
296 error = nfsrv_proxyds(nd, vp, 0, 0, nd->nd_cred, p,
297 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL);
302 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
307 * If we got the Change, Size and Modify Time from the DS,
311 nvap->na_atime = na.na_atime;
312 nvap->na_mtime = na.na_mtime;
313 nvap->na_filerev = na.na_filerev;
314 nvap->na_size = na.na_size;
316 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
317 error, (uintmax_t)na.na_filerev);
324 * Get a file handle for a vnode.
327 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
331 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
332 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
333 error = VOP_VPTOFH(vp, &fhp->fh_fid);
340 * Perform access checking for vnodes obtained from file handles that would
341 * refer to files already opened by a Unix client. You cannot just use
342 * vn_writechk() and VOP_ACCESSX() for two reasons.
343 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
345 * 2 - The owner is to be given access irrespective of mode bits for some
346 * operations, so that processes that chmod after opening a file don't
350 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
351 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
352 u_int32_t *supportedtypep)
355 int error = 0, getret = 0;
357 if (vpislocked == 0) {
358 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
363 if (accmode & VWRITE) {
364 /* Just vn_writechk() changed to check rdonly */
366 * Disallow write attempts on read-only file systems;
367 * unless the file is a socket or a block or character
368 * device resident on the file system.
370 if (NFSVNO_EXRDONLY(exp) ||
371 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
372 switch (vp->v_type) {
382 * If there's shared text associated with
383 * the inode, try to free it up once. If
384 * we fail, we can't allow writing.
386 if (VOP_IS_TEXT(vp) && error == 0)
396 * Should the override still be applied when ACLs are enabled?
398 error = VOP_ACCESSX(vp, accmode, cred, p);
399 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
401 * Try again with VEXPLICIT_DENY, to see if the test for
402 * deletion is supported.
404 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
406 if (vp->v_type == VDIR) {
407 accmode &= ~(VDELETE | VDELETE_CHILD);
409 error = VOP_ACCESSX(vp, accmode, cred, p);
410 } else if (supportedtypep != NULL) {
411 *supportedtypep &= ~NFSACCESS_DELETE;
417 * Allow certain operations for the owner (reads and writes
418 * on files that are already open).
420 if (override != NFSACCCHK_NOOVERRIDE &&
421 (error == EPERM || error == EACCES)) {
422 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
424 else if (override & NFSACCCHK_ALLOWOWNER) {
425 getret = VOP_GETATTR(vp, &vattr, cred);
426 if (getret == 0 && cred->cr_uid == vattr.va_uid)
439 * Set attribute(s) vnop.
442 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
443 struct thread *p, struct nfsexstuff *exp)
447 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
448 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
449 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
450 nvap->na_vattr.va_size != VNOVAL ||
451 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
452 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
453 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
454 /* For a pNFS server, set the attributes on the DS file. */
455 error = nfsrv_proxyds(NULL, vp, 0, 0, cred, p, NFSPROC_SETATTR,
456 NULL, NULL, NULL, nvap, NULL);
465 * Set up nameidata for a lookup() call and do it.
468 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
469 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
470 struct vnode **retdirp)
472 struct componentname *cnp = &ndp->ni_cnd;
476 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
481 cnp->cn_nameptr = cnp->cn_pnbuf;
484 * Extract and set starting directory.
486 if (dp->v_type != VDIR) {
491 nfsvno_relpathbuf(ndp);
499 if (NFSVNO_EXRDONLY(exp))
500 cnp->cn_flags |= RDONLY;
501 ndp->ni_segflg = UIO_SYSSPACE;
503 if (nd->nd_flag & ND_PUBLOOKUP) {
505 if (cnp->cn_pnbuf[0] == '/') {
508 * Check for degenerate pathnames here, since lookup()
511 for (i = 1; i < ndp->ni_pathlen; i++)
512 if (cnp->cn_pnbuf[i] != '/')
514 if (i == ndp->ni_pathlen) {
515 error = NFSERR_ACCES;
521 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
522 (nd->nd_flag & ND_NFSV4) == 0) {
524 * Only cross mount points for NFSv4 when doing a
525 * mount while traversing the file system above
526 * the mount point, unless nfsrv_enable_crossmntpt is set.
528 cnp->cn_flags |= NOCROSSMOUNT;
532 * Initialize for scan, set ni_startdir and bump ref on dp again
533 * because lookup() will dereference ni_startdir.
537 ndp->ni_startdir = dp;
538 ndp->ni_rootdir = rootvnode;
539 ndp->ni_topdir = NULL;
542 cnp->cn_flags |= LOCKLEAF;
544 cnp->cn_nameptr = cnp->cn_pnbuf;
546 * Call lookup() to do the real work. If an error occurs,
547 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
548 * we do not have to dereference anything before returning.
549 * In either case ni_startdir will be dereferenced and NULLed
557 * Check for encountering a symbolic link. Trivial
558 * termination occurs if no symlink encountered.
560 if ((cnp->cn_flags & ISSYMLINK) == 0) {
561 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
562 nfsvno_relpathbuf(ndp);
563 if (ndp->ni_vp && !lockleaf)
564 NFSVOPUNLOCK(ndp->ni_vp, 0);
571 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
572 NFSVOPUNLOCK(ndp->ni_dvp, 0);
573 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
578 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
582 if (ndp->ni_pathlen > 1)
583 cp = uma_zalloc(namei_zone, M_WAITOK);
587 aiov.iov_len = MAXPATHLEN;
588 auio.uio_iov = &aiov;
591 auio.uio_rw = UIO_READ;
592 auio.uio_segflg = UIO_SYSSPACE;
594 auio.uio_resid = MAXPATHLEN;
595 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
598 if (ndp->ni_pathlen > 1)
599 uma_zfree(namei_zone, cp);
605 linklen = MAXPATHLEN - auio.uio_resid;
610 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
611 error = ENAMETOOLONG;
616 * Adjust or replace path
618 if (ndp->ni_pathlen > 1) {
619 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
620 uma_zfree(namei_zone, cnp->cn_pnbuf);
623 cnp->cn_pnbuf[linklen] = '\0';
624 ndp->ni_pathlen += linklen;
627 * Cleanup refs for next loop and check if root directory
628 * should replace current directory. Normally ni_dvp
629 * becomes the new base directory and is cleaned up when
630 * we loop. Explicitly null pointers after invalidation
631 * to clarify operation.
636 if (cnp->cn_pnbuf[0] == '/') {
638 ndp->ni_dvp = ndp->ni_rootdir;
641 ndp->ni_startdir = ndp->ni_dvp;
645 cnp->cn_flags &= ~LOCKLEAF;
649 nfsvno_relpathbuf(ndp);
652 ndp->ni_startdir = NULL;
653 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
658 NFSEXITCODE2(error, nd);
663 * Set up a pathname buffer and return a pointer to it and, optionally
664 * set a hash pointer.
667 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
669 struct componentname *cnp = &ndp->ni_cnd;
671 cnp->cn_flags |= (NOMACCHECK | HASBUF);
672 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
675 *bufpp = cnp->cn_pnbuf;
679 * Release the above path buffer, if not released by nfsvno_namei().
682 nfsvno_relpathbuf(struct nameidata *ndp)
685 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
687 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
688 ndp->ni_cnd.cn_flags &= ~HASBUF;
692 * Readlink vnode op into an mbuf list.
695 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
696 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
698 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
699 struct iovec *ivp = iv;
700 struct uio io, *uiop = &io;
701 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
702 int i, len, tlen, error = 0;
706 while (len < NFS_MAXPATHLEN) {
708 MCLGET(mp, M_WAITOK);
709 mp->m_len = M_SIZE(mp);
716 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
717 mp->m_len = NFS_MAXPATHLEN - len;
718 len = NFS_MAXPATHLEN;
722 ivp->iov_base = mtod(mp, caddr_t);
723 ivp->iov_len = mp->m_len;
728 uiop->uio_iovcnt = i;
729 uiop->uio_offset = 0;
730 uiop->uio_resid = len;
731 uiop->uio_rw = UIO_READ;
732 uiop->uio_segflg = UIO_SYSSPACE;
734 error = VOP_READLINK(vp, uiop, cred);
740 if (uiop->uio_resid > 0) {
741 len -= uiop->uio_resid;
742 tlen = NFSM_RNDUP(len);
743 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
755 * Read vnode op call into mbuf list.
758 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
759 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
765 int error = 0, len, left, siz, tlen, ioflag = 0;
766 struct mbuf *m2 = NULL, *m3;
767 struct uio io, *uiop = &io;
771 * Attempt to read from a DS file. A return of ENOENT implies
772 * there is no DS file to read.
774 error = nfsrv_proxyds(NULL, vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
775 NULL, mpendp, NULL, NULL);
779 len = left = NFSM_RNDUP(cnt);
782 * Generate the mbuf list with the uio_iov ref. to it.
789 siz = min(M_TRAILINGSPACE(m), left);
798 iv = malloc(i * sizeof (struct iovec),
800 uiop->uio_iov = iv2 = iv;
806 panic("nfsvno_read iov");
807 siz = min(M_TRAILINGSPACE(m), left);
809 iv->iov_base = mtod(m, caddr_t) + m->m_len;
818 uiop->uio_iovcnt = i;
819 uiop->uio_offset = off;
820 uiop->uio_resid = len;
821 uiop->uio_rw = UIO_READ;
822 uiop->uio_segflg = UIO_SYSSPACE;
824 nh = nfsrv_sequential_heuristic(uiop, vp);
825 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
826 /* XXX KDM make this more systematic? */
827 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
828 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
835 nh->nh_nextoff = uiop->uio_offset;
836 tlen = len - uiop->uio_resid;
837 cnt = cnt < tlen ? cnt : tlen;
838 tlen = NFSM_RNDUP(cnt);
842 } else if (len != tlen || tlen != cnt)
843 nfsrv_adj(m3, len - tlen, tlen - cnt);
853 * Write vnode op from an mbuf list.
856 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int *stable,
857 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
863 struct uio io, *uiop = &io;
867 * Attempt to write to a DS file. A return of ENOENT implies
868 * there is no DS file to write.
870 error = nfsrv_proxyds(NULL, vp, off, retlen, cred, p, NFSPROC_WRITEDS,
871 &mp, cp, NULL, NULL, NULL);
872 if (error != ENOENT) {
873 *stable = NFSWRITE_FILESYNC;
877 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
879 uiop->uio_iov = iv = ivp;
880 uiop->uio_iovcnt = cnt;
881 i = mtod(mp, caddr_t) + mp->m_len - cp;
885 panic("nfsvno_write");
896 cp = mtod(mp, caddr_t);
900 if (*stable == NFSWRITE_UNSTABLE)
901 ioflags = IO_NODELOCKED;
903 ioflags = (IO_SYNC | IO_NODELOCKED);
904 uiop->uio_resid = retlen;
905 uiop->uio_rw = UIO_WRITE;
906 uiop->uio_segflg = UIO_SYSSPACE;
908 uiop->uio_offset = off;
909 nh = nfsrv_sequential_heuristic(uiop, vp);
910 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
911 /* XXX KDM make this more systematic? */
912 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
913 error = VOP_WRITE(vp, uiop, ioflags, cred);
915 nh->nh_nextoff = uiop->uio_offset;
923 * Common code for creating a regular file (plus special files for V2).
926 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
927 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
928 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
933 error = nd->nd_repstat;
934 if (!error && ndp->ni_vp == NULL) {
935 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
936 vrele(ndp->ni_startdir);
937 error = VOP_CREATE(ndp->ni_dvp,
938 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
939 /* For a pNFS server, create the data file on a DS. */
940 if (error == 0 && nvap->na_type == VREG) {
942 * Create a data file on a DS for a pNFS server.
943 * This function just returns if not
944 * running a pNFS DS or the creation fails.
946 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
950 nfsvno_relpathbuf(ndp);
952 if (*exclusive_flagp) {
953 *exclusive_flagp = 0;
954 NFSVNO_ATTRINIT(nvap);
955 nvap->na_atime.tv_sec = cverf[0];
956 nvap->na_atime.tv_nsec = cverf[1];
957 error = VOP_SETATTR(ndp->ni_vp,
958 &nvap->na_vattr, nd->nd_cred);
962 error = NFSERR_NOTSUPP;
967 * NFS V2 Only. nfsrvd_mknod() does this for V3.
968 * (This implies, just get out on an error.)
970 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
971 nvap->na_type == VFIFO) {
972 if (nvap->na_type == VCHR && rdev == 0xffffffff)
973 nvap->na_type = VFIFO;
974 if (nvap->na_type != VFIFO &&
975 (error = priv_check_cred(nd->nd_cred,
976 PRIV_VFS_MKNOD_DEV, 0))) {
977 vrele(ndp->ni_startdir);
978 nfsvno_relpathbuf(ndp);
982 nvap->na_rdev = rdev;
983 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
984 &ndp->ni_cnd, &nvap->na_vattr);
986 nfsvno_relpathbuf(ndp);
987 vrele(ndp->ni_startdir);
991 vrele(ndp->ni_startdir);
992 nfsvno_relpathbuf(ndp);
1000 * Handle cases where error is already set and/or
1002 * 1 - clean up the lookup
1003 * 2 - iff !error and na_size set, truncate it
1005 vrele(ndp->ni_startdir);
1006 nfsvno_relpathbuf(ndp);
1008 if (ndp->ni_dvp == *vpp)
1012 if (!error && nvap->na_size != VNOVAL) {
1013 error = nfsvno_accchk(*vpp, VWRITE,
1014 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1015 NFSACCCHK_VPISLOCKED, NULL);
1017 tempsize = nvap->na_size;
1018 NFSVNO_ATTRINIT(nvap);
1019 nvap->na_size = tempsize;
1020 error = VOP_SETATTR(*vpp,
1021 &nvap->na_vattr, nd->nd_cred);
1034 * Do a mknod vnode op.
1037 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1043 vtyp = nvap->na_type;
1045 * Iff doesn't exist, create it.
1048 vrele(ndp->ni_startdir);
1049 nfsvno_relpathbuf(ndp);
1055 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1056 vrele(ndp->ni_startdir);
1057 nfsvno_relpathbuf(ndp);
1059 error = NFSERR_BADTYPE;
1062 if (vtyp == VSOCK) {
1063 vrele(ndp->ni_startdir);
1064 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1065 &ndp->ni_cnd, &nvap->na_vattr);
1067 nfsvno_relpathbuf(ndp);
1069 if (nvap->na_type != VFIFO &&
1070 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
1071 vrele(ndp->ni_startdir);
1072 nfsvno_relpathbuf(ndp);
1076 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1077 &ndp->ni_cnd, &nvap->na_vattr);
1079 nfsvno_relpathbuf(ndp);
1080 vrele(ndp->ni_startdir);
1082 * Since VOP_MKNOD returns the ni_vp, I can't
1083 * see any reason to do the lookup.
1096 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1097 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1101 if (ndp->ni_vp != NULL) {
1102 if (ndp->ni_dvp == ndp->ni_vp)
1107 nfsvno_relpathbuf(ndp);
1111 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1114 nfsvno_relpathbuf(ndp);
1125 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1126 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1127 struct nfsexstuff *exp)
1132 vrele(ndp->ni_startdir);
1133 nfsvno_relpathbuf(ndp);
1134 if (ndp->ni_dvp == ndp->ni_vp)
1143 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1144 &nvap->na_vattr, pathcp);
1146 vrele(ndp->ni_startdir);
1147 nfsvno_relpathbuf(ndp);
1149 * Although FreeBSD still had the lookup code in
1150 * it for 7/current, there doesn't seem to be any
1151 * point, since VOP_SYMLINK() returns the ni_vp.
1152 * Just vput it for v2.
1154 if (!not_v2 && !error)
1163 * Parse symbolic link arguments.
1164 * This function has an ugly side effect. It will malloc() an area for
1165 * the symlink and set iov_base to point to it, only if it succeeds.
1166 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1170 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1171 struct thread *p, char **pathcpp, int *lenp)
1174 char *pathcp = NULL;
1176 struct nfsv2_sattr *sp;
1180 if ((nd->nd_flag & ND_NFSV3) &&
1181 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1183 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1184 len = fxdr_unsigned(int, *tl);
1185 if (len > NFS_MAXPATHLEN || len <= 0) {
1189 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1190 error = nfsrv_mtostr(nd, pathcp, len);
1193 if (nd->nd_flag & ND_NFSV2) {
1194 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1195 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1199 NFSEXITCODE2(0, nd);
1203 free(pathcp, M_TEMP);
1204 NFSEXITCODE2(error, nd);
1209 * Remove a non-directory object.
1212 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1213 struct thread *p, struct nfsexstuff *exp)
1215 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1216 int error = 0, mirrorcnt;
1217 char fname[PNFS_FILENAME_LEN + 1];
1222 if (vp->v_type == VDIR)
1223 error = NFSERR_ISDIR;
1225 error = nfsrv_checkremove(vp, 1, p);
1227 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1229 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1230 if (error == 0 && dsdvp[0] != NULL)
1231 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1232 if (ndp->ni_dvp == vp)
1237 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1238 nfsvno_relpathbuf(ndp);
1244 * Remove a directory.
1247 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1248 struct thread *p, struct nfsexstuff *exp)
1254 if (vp->v_type != VDIR) {
1259 * No rmdir "." please.
1261 if (ndp->ni_dvp == vp) {
1266 * The root of a mounted filesystem cannot be deleted.
1268 if (vp->v_vflag & VV_ROOT)
1272 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1273 if (ndp->ni_dvp == vp)
1278 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1279 nfsvno_relpathbuf(ndp);
1288 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1289 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1291 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1292 int error = 0, mirrorcnt;
1293 char fname[PNFS_FILENAME_LEN + 1];
1297 fvp = fromndp->ni_vp;
1299 vrele(fromndp->ni_dvp);
1304 tdvp = tondp->ni_dvp;
1307 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1308 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1310 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1311 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1314 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1315 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1320 * A rename to '.' or '..' results in a prematurely
1321 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1324 if ((tondp->ni_cnd.cn_namelen == 1 &&
1325 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1326 (tondp->ni_cnd.cn_namelen == 2 &&
1327 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1328 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1333 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1334 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1337 if (fvp->v_mount != tdvp->v_mount) {
1338 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1342 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1347 * If source and destination are the same, there is nothing to
1348 * do. Set error to -1 to indicate this.
1353 if (ndflag & ND_NFSV4) {
1354 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1355 error = nfsrv_checkremove(fvp, 0, p);
1356 NFSVOPUNLOCK(fvp, 0);
1360 error = nfsrv_checkremove(tvp, 1, p);
1363 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1364 * that the NFSv4 client won't be confused by the rename.
1365 * Since nfsd_recalldelegation() can only be called on an
1366 * unlocked vnode at this point and fvp is the file that will
1367 * still exist after the rename, just do fvp.
1369 nfsd_recalldelegation(fvp, p);
1371 if (error == 0 && tvp != NULL) {
1372 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1373 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1374 " dsdvp=%p\n", dsdvp[0]);
1378 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1379 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1388 vrele(fromndp->ni_dvp);
1395 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1396 * if the rename succeeded, the DS file for the tvp needs to be
1399 if (error == 0 && dsdvp[0] != NULL) {
1400 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1401 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1404 vrele(tondp->ni_startdir);
1405 nfsvno_relpathbuf(tondp);
1407 vrele(fromndp->ni_startdir);
1408 nfsvno_relpathbuf(fromndp);
1417 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1418 struct thread *p, struct nfsexstuff *exp)
1428 if (vp->v_mount != xp->v_mount)
1432 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1433 if ((vp->v_iflag & VI_DOOMED) == 0)
1434 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1437 if (ndp->ni_dvp == vp)
1441 NFSVOPUNLOCK(vp, 0);
1443 if (ndp->ni_dvp == ndp->ni_vp)
1450 nfsvno_relpathbuf(ndp);
1456 * Do the fsync() appropriate for the commit.
1459 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1465 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1466 * file is done. At this time VOP_FSYNC does not accept offset and
1467 * byte count parameters so call VOP_FSYNC the whole file for now.
1468 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1469 * File systems that do not use the buffer cache (as indicated
1470 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1472 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1473 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1475 * Give up and do the whole thing
1478 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1479 VM_OBJECT_WLOCK(vp->v_object);
1480 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1481 VM_OBJECT_WUNLOCK(vp->v_object);
1483 error = VOP_FSYNC(vp, MNT_WAIT, td);
1486 * Locate and synchronously write any buffers that fall
1487 * into the requested range. Note: we are assuming that
1488 * f_iosize is a power of 2.
1490 int iosize = vp->v_mount->mnt_stat.f_iosize;
1491 int iomask = iosize - 1;
1496 * Align to iosize boundary, super-align to page boundary.
1499 cnt += off & iomask;
1500 off &= ~(u_quad_t)iomask;
1502 if (off & PAGE_MASK) {
1503 cnt += off & PAGE_MASK;
1504 off &= ~(u_quad_t)PAGE_MASK;
1506 lblkno = off / iosize;
1509 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1510 VM_OBJECT_WLOCK(vp->v_object);
1511 vm_object_page_clean(vp->v_object, off, off + cnt,
1513 VM_OBJECT_WUNLOCK(vp->v_object);
1522 * If we have a buffer and it is marked B_DELWRI we
1523 * have to lock and write it. Otherwise the prior
1524 * write is assumed to have already been committed.
1526 * gbincore() can return invalid buffers now so we
1527 * have to check that bit as well (though B_DELWRI
1528 * should not be set if B_INVAL is set there could be
1529 * a race here since we haven't locked the buffer).
1531 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1532 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1533 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1535 continue; /* retry */
1537 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1540 bp->b_flags &= ~B_ASYNC;
1563 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1569 if (nfsrv_devidcnt > 0) {
1570 /* For a pNFS service, get the DS numbers. */
1571 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1572 error = nfsrv_pnfsstatfs(tsf);
1578 error = VFS_STATFS(vp->v_mount, sf);
1581 sf->f_blocks = tsf->f_blocks;
1582 sf->f_bavail = tsf->f_bavail;
1583 sf->f_bfree = tsf->f_bfree;
1584 sf->f_bsize = tsf->f_bsize;
1587 * Since NFS handles these values as unsigned on the
1588 * wire, there is no way to represent negative values,
1589 * so set them to 0. Without this, they will appear
1590 * to be very large positive values for clients like
1593 if (sf->f_bavail < 0)
1595 if (sf->f_ffree < 0)
1604 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1605 * must handle nfsrv_opencheck() calls after any other access checks.
1608 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1609 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1610 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1611 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1612 struct nfsexstuff *exp, struct vnode **vpp)
1614 struct vnode *vp = NULL;
1616 struct nfsexstuff nes;
1618 if (ndp->ni_vp == NULL)
1619 nd->nd_repstat = nfsrv_opencheck(clientid,
1620 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1621 if (!nd->nd_repstat) {
1622 if (ndp->ni_vp == NULL) {
1623 vrele(ndp->ni_startdir);
1624 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1625 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1626 /* For a pNFS server, create the data file on a DS. */
1627 if (nd->nd_repstat == 0) {
1629 * Create a data file on a DS for a pNFS server.
1630 * This function just returns if not
1631 * running a pNFS DS or the creation fails.
1633 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1637 nfsvno_relpathbuf(ndp);
1638 if (!nd->nd_repstat) {
1639 if (*exclusive_flagp) {
1640 *exclusive_flagp = 0;
1641 NFSVNO_ATTRINIT(nvap);
1642 nvap->na_atime.tv_sec = cverf[0];
1643 nvap->na_atime.tv_nsec = cverf[1];
1644 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1645 &nvap->na_vattr, cred);
1646 if (nd->nd_repstat != 0) {
1649 nd->nd_repstat = NFSERR_NOTSUPP;
1651 NFSSETBIT_ATTRBIT(attrbitp,
1652 NFSATTRBIT_TIMEACCESS);
1654 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1655 aclp, p, attrbitp, exp);
1660 if (ndp->ni_startdir)
1661 vrele(ndp->ni_startdir);
1662 nfsvno_relpathbuf(ndp);
1664 if (create == NFSV4OPEN_CREATE) {
1665 if (ndp->ni_dvp == vp)
1670 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1671 if (ndp->ni_cnd.cn_flags & RDONLY)
1672 NFSVNO_SETEXRDONLY(&nes);
1674 NFSVNO_EXINIT(&nes);
1675 nd->nd_repstat = nfsvno_accchk(vp,
1676 VWRITE, cred, &nes, p,
1677 NFSACCCHK_NOOVERRIDE,
1678 NFSACCCHK_VPISLOCKED, NULL);
1679 nd->nd_repstat = nfsrv_opencheck(clientid,
1680 stateidp, stp, vp, nd, p, nd->nd_repstat);
1681 if (!nd->nd_repstat) {
1682 tempsize = nvap->na_size;
1683 NFSVNO_ATTRINIT(nvap);
1684 nvap->na_size = tempsize;
1685 nd->nd_repstat = VOP_SETATTR(vp,
1686 &nvap->na_vattr, cred);
1688 } else if (vp->v_type == VREG) {
1689 nd->nd_repstat = nfsrv_opencheck(clientid,
1690 stateidp, stp, vp, nd, p, nd->nd_repstat);
1694 if (ndp->ni_cnd.cn_flags & HASBUF)
1695 nfsvno_relpathbuf(ndp);
1696 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1697 vrele(ndp->ni_startdir);
1698 if (ndp->ni_dvp == ndp->ni_vp)
1708 NFSEXITCODE2(0, nd);
1712 * Updates the file rev and sets the mtime and ctime
1713 * to the current clock time, returning the va_filerev and va_Xtime
1715 * Return ESTALE to indicate the vnode is VI_DOOMED.
1718 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1719 struct nfsrv_descript *nd, struct thread *p)
1724 vfs_timestamp(&va.va_mtime);
1725 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1726 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1727 if ((vp->v_iflag & VI_DOOMED) != 0)
1730 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1731 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1736 * Glue routine to nfsv4_fillattr().
1739 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1740 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1741 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1742 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1748 if (nfsrv_devidcnt > 0 &&
1749 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1750 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1751 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1752 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1753 error = nfsrv_pnfsstatfs(sf);
1759 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1760 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1761 mounted_on_fileno, sf);
1763 NFSEXITCODE2(0, nd);
1767 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1769 * nfs readdir service
1770 * - mallocs what it thinks is enough to read
1771 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1772 * - calls VOP_READDIR()
1773 * - loops around building the reply
1774 * if the output generated exceeds count break out of loop
1775 * The NFSM_CLGET macro is used here so that the reply will be packed
1776 * tightly in mbuf clusters.
1777 * - it trims out records with d_fileno == 0
1778 * this doesn't matter for Unix clients, but they might confuse clients
1780 * - it trims out records with d_type == DT_WHT
1781 * these cannot be seen through NFS (unless we extend the protocol)
1782 * The alternate call nfsrvd_readdirplus() does lookups as well.
1783 * PS: The NFS protocol spec. does not clarify what the "count" byte
1784 * argument is a count of.. just name strings and file id's or the
1785 * entire reply rpc or ...
1786 * I tried just file name and id sizes and it confused the Sun client,
1787 * so I am using the full rpc size now. The "paranoia.." comment refers
1788 * to including the status longwords that are not a part of the dir.
1789 * "entry" structures, but are in the rpc.
1792 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1793 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1798 char *cpos, *cend, *rbuf;
1800 int nlen, error = 0, getret = 1;
1801 int siz, cnt, fullsiz, eofflag, ncookies;
1802 u_int64_t off, toff, verf __unused;
1803 u_long *cookies = NULL, *cookiep;
1808 if (nd->nd_repstat) {
1809 nfsrv_postopattr(nd, getret, &at);
1812 if (nd->nd_flag & ND_NFSV2) {
1813 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1814 off = fxdr_unsigned(u_quad_t, *tl++);
1816 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1817 off = fxdr_hyper(tl);
1819 verf = fxdr_hyper(tl);
1823 cnt = fxdr_unsigned(int, *tl);
1824 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1825 cnt = NFS_SRVMAXDATA(nd);
1826 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1828 if (nd->nd_flag & ND_NFSV3) {
1829 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
1833 * va_filerev is not sufficient as a cookie verifier,
1834 * since it is not supposed to change when entries are
1835 * removed/added unless that offset cookies returned to
1836 * the client are no longer valid.
1838 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1839 nd->nd_repstat = NFSERR_BAD_COOKIE;
1842 if (!nd->nd_repstat && vp->v_type != VDIR)
1843 nd->nd_repstat = NFSERR_NOTDIR;
1844 if (nd->nd_repstat == 0 && cnt == 0) {
1845 if (nd->nd_flag & ND_NFSV2)
1846 /* NFSv2 does not have NFSERR_TOOSMALL */
1847 nd->nd_repstat = EPERM;
1849 nd->nd_repstat = NFSERR_TOOSMALL;
1851 if (!nd->nd_repstat)
1852 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1853 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1854 NFSACCCHK_VPISLOCKED, NULL);
1855 if (nd->nd_repstat) {
1857 if (nd->nd_flag & ND_NFSV3)
1858 nfsrv_postopattr(nd, getret, &at);
1861 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1862 rbuf = malloc(siz, M_TEMP, M_WAITOK);
1866 free(cookies, M_TEMP);
1874 io.uio_offset = (off_t)off;
1876 io.uio_segflg = UIO_SYSSPACE;
1877 io.uio_rw = UIO_READ;
1879 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1881 off = (u_int64_t)io.uio_offset;
1883 siz -= io.uio_resid;
1885 if (!cookies && !nd->nd_repstat)
1886 nd->nd_repstat = NFSERR_PERM;
1887 if (nd->nd_flag & ND_NFSV3) {
1888 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
1889 if (!nd->nd_repstat)
1890 nd->nd_repstat = getret;
1894 * Handles the failed cases. nd->nd_repstat == 0 past here.
1896 if (nd->nd_repstat) {
1900 free(cookies, M_TEMP);
1901 if (nd->nd_flag & ND_NFSV3)
1902 nfsrv_postopattr(nd, getret, &at);
1906 * If nothing read, return eof
1911 if (nd->nd_flag & ND_NFSV2) {
1912 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1914 nfsrv_postopattr(nd, getret, &at);
1915 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1916 txdr_hyper(at.na_filerev, tl);
1919 *tl++ = newnfs_false;
1922 free(cookies, M_TEMP);
1927 * Check for degenerate cases of nothing useful read.
1928 * If so go try again
1932 dp = (struct dirent *)cpos;
1936 * For some reason FreeBSD's ufs_readdir() chooses to back the
1937 * directory offset up to a block boundary, so it is necessary to
1938 * skip over the records that precede the requested offset. This
1939 * requires the assumption that file offset cookies monotonically
1942 while (cpos < cend && ncookies > 0 &&
1943 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1944 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1945 cpos += dp->d_reclen;
1946 dp = (struct dirent *)cpos;
1950 if (cpos >= cend || ncookies == 0) {
1958 * dirlen is the size of the reply, including all XDR and must
1959 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1960 * if the XDR should be included in "count", but to be safe, we do.
1961 * (Include the two booleans at the end of the reply in dirlen now.)
1963 if (nd->nd_flag & ND_NFSV3) {
1964 nfsrv_postopattr(nd, getret, &at);
1965 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1966 txdr_hyper(at.na_filerev, tl);
1967 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1969 dirlen = 2 * NFSX_UNSIGNED;
1972 /* Loop through the records and build reply */
1973 while (cpos < cend && ncookies > 0) {
1974 nlen = dp->d_namlen;
1975 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
1976 nlen <= NFS_MAXNAMLEN) {
1977 if (nd->nd_flag & ND_NFSV3)
1978 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1980 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
1987 * Build the directory record xdr from
1990 if (nd->nd_flag & ND_NFSV3) {
1991 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1992 *tl++ = newnfs_true;
1995 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1996 *tl++ = newnfs_true;
1998 *tl = txdr_unsigned(dp->d_fileno);
1999 (void) nfsm_strtom(nd, dp->d_name, nlen);
2000 if (nd->nd_flag & ND_NFSV3) {
2001 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2004 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2005 *tl = txdr_unsigned(*cookiep);
2007 cpos += dp->d_reclen;
2008 dp = (struct dirent *)cpos;
2014 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2015 *tl++ = newnfs_false;
2021 free(cookies, M_TEMP);
2024 NFSEXITCODE2(0, nd);
2028 NFSEXITCODE2(error, nd);
2033 * Readdirplus for V3 and Readdir for V4.
2036 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2037 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
2042 char *cpos, *cend, *rbuf;
2045 struct nfsvattr nva, at, *nvap = &nva;
2046 struct mbuf *mb0, *mb1;
2047 struct nfsreferral *refp;
2048 int nlen, r, error = 0, getret = 1, usevget = 1;
2049 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2050 caddr_t bpos0, bpos1;
2051 u_int64_t off, toff, verf;
2052 u_long *cookies = NULL, *cookiep;
2053 nfsattrbit_t attrbits, rderrbits, savbits;
2056 struct componentname cn;
2057 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2058 struct mount *mp, *new_mp;
2059 uint64_t mounted_on_fileno;
2061 if (nd->nd_repstat) {
2062 nfsrv_postopattr(nd, getret, &at);
2065 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2066 off = fxdr_hyper(tl);
2069 verf = fxdr_hyper(tl);
2071 siz = fxdr_unsigned(int, *tl++);
2072 cnt = fxdr_unsigned(int, *tl);
2075 * Use the server's maximum data transfer size as the upper bound
2078 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2079 cnt = NFS_SRVMAXDATA(nd);
2082 * siz is a "hint" of how much directory information (name, fileid,
2083 * cookie) should be in the reply. At least one client "hints" 0,
2084 * so I set it to cnt for that case. I also round it up to the
2085 * next multiple of DIRBLKSIZ.
2089 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2091 if (nd->nd_flag & ND_NFSV4) {
2092 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2095 NFSSET_ATTRBIT(&savbits, &attrbits);
2096 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits);
2097 NFSZERO_ATTRBIT(&rderrbits);
2098 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2100 NFSZERO_ATTRBIT(&attrbits);
2103 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2105 if (!nd->nd_repstat) {
2106 if (off && verf != at.na_filerev) {
2108 * va_filerev is not sufficient as a cookie verifier,
2109 * since it is not supposed to change when entries are
2110 * removed/added unless that offset cookies returned to
2111 * the client are no longer valid.
2113 if (nd->nd_flag & ND_NFSV4) {
2114 nd->nd_repstat = NFSERR_NOTSAME;
2116 nd->nd_repstat = NFSERR_BAD_COOKIE;
2121 if (!nd->nd_repstat && vp->v_type != VDIR)
2122 nd->nd_repstat = NFSERR_NOTDIR;
2123 if (!nd->nd_repstat && cnt == 0)
2124 nd->nd_repstat = NFSERR_TOOSMALL;
2125 if (!nd->nd_repstat)
2126 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2127 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2128 NFSACCCHK_VPISLOCKED, NULL);
2129 if (nd->nd_repstat) {
2131 if (nd->nd_flag & ND_NFSV3)
2132 nfsrv_postopattr(nd, getret, &at);
2135 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2136 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2138 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2142 free(cookies, M_TEMP);
2150 io.uio_offset = (off_t)off;
2152 io.uio_segflg = UIO_SYSSPACE;
2153 io.uio_rw = UIO_READ;
2155 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2157 off = (u_int64_t)io.uio_offset;
2159 siz -= io.uio_resid;
2161 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2163 if (!cookies && !nd->nd_repstat)
2164 nd->nd_repstat = NFSERR_PERM;
2165 if (!nd->nd_repstat)
2166 nd->nd_repstat = getret;
2167 if (nd->nd_repstat) {
2170 free(cookies, M_TEMP);
2172 if (nd->nd_flag & ND_NFSV3)
2173 nfsrv_postopattr(nd, getret, &at);
2177 * If nothing read, return eof
2182 if (nd->nd_flag & ND_NFSV3)
2183 nfsrv_postopattr(nd, getret, &at);
2184 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2185 txdr_hyper(at.na_filerev, tl);
2187 *tl++ = newnfs_false;
2189 free(cookies, M_TEMP);
2195 * Check for degenerate cases of nothing useful read.
2196 * If so go try again
2200 dp = (struct dirent *)cpos;
2204 * For some reason FreeBSD's ufs_readdir() chooses to back the
2205 * directory offset up to a block boundary, so it is necessary to
2206 * skip over the records that precede the requested offset. This
2207 * requires the assumption that file offset cookies monotonically
2210 while (cpos < cend && ncookies > 0 &&
2211 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2212 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2213 ((nd->nd_flag & ND_NFSV4) &&
2214 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2215 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2216 cpos += dp->d_reclen;
2217 dp = (struct dirent *)cpos;
2221 if (cpos >= cend || ncookies == 0) {
2228 * Busy the file system so that the mount point won't go away
2229 * and, as such, VFS_VGET() can be used safely.
2233 NFSVOPUNLOCK(vp, 0);
2234 nd->nd_repstat = vfs_busy(mp, 0);
2236 if (nd->nd_repstat != 0) {
2238 free(cookies, M_TEMP);
2240 if (nd->nd_flag & ND_NFSV3)
2241 nfsrv_postopattr(nd, getret, &at);
2246 * Check to see if entries in this directory can be safely acquired
2247 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2248 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2249 * automount of the snapshot directory that is required will
2251 * This needs to be done here for NFSv4, since NFSv4 never does
2252 * a VFS_VGET() for "." or "..".
2255 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2256 if (r == EOPNOTSUPP) {
2258 cn.cn_nameiop = LOOKUP;
2259 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2260 cn.cn_cred = nd->nd_cred;
2267 * Save this position, in case there is an error before one entry
2271 bpos0 = nd->nd_bpos;
2274 * Fill in the first part of the reply.
2275 * dirlen is the reply length in bytes and cannot exceed cnt.
2276 * (Include the two booleans at the end of the reply in dirlen now,
2277 * so we recognize when we have exceeded cnt.)
2279 if (nd->nd_flag & ND_NFSV3) {
2280 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2281 nfsrv_postopattr(nd, getret, &at);
2283 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2285 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2286 txdr_hyper(at.na_filerev, tl);
2289 * Save this position, in case there is an empty reply needed.
2292 bpos1 = nd->nd_bpos;
2294 /* Loop through the records and build reply */
2296 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2297 nlen = dp->d_namlen;
2298 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2299 nlen <= NFS_MAXNAMLEN &&
2300 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2301 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2302 || (nlen == 1 && dp->d_name[0] != '.'))) {
2304 * Save the current position in the reply, in case
2305 * this entry exceeds cnt.
2308 bpos1 = nd->nd_bpos;
2311 * For readdir_and_lookup get the vnode using
2320 mounted_on_fileno = (uint64_t)dp->d_fileno;
2321 if ((nd->nd_flag & ND_NFSV3) ||
2322 NFSNONZERO_ATTRBIT(&savbits)) {
2323 if (nd->nd_flag & ND_NFSV4)
2324 refp = nfsv4root_getreferral(NULL,
2328 r = VFS_VGET(mp, dp->d_fileno,
2332 if (r == EOPNOTSUPP) {
2335 cn.cn_nameiop = LOOKUP;
2343 cn.cn_nameptr = dp->d_name;
2344 cn.cn_namelen = nlen;
2345 cn.cn_flags = ISLASTCN |
2346 NOFOLLOW | LOCKLEAF;
2348 dp->d_name[0] == '.' &&
2349 dp->d_name[1] == '.')
2352 if (NFSVOPLOCK(vp, LK_SHARED)
2354 nd->nd_repstat = EPERM;
2357 if ((vp->v_vflag & VV_ROOT) != 0
2358 && (cn.cn_flags & ISDOTDOT)
2364 r = VOP_LOOKUP(vp, &nvp,
2373 * For NFSv4, check to see if nvp is
2374 * a mount point and get the mount
2375 * point vnode, as required.
2378 nfsrv_enable_crossmntpt != 0 &&
2379 (nd->nd_flag & ND_NFSV4) != 0 &&
2380 nvp->v_type == VDIR &&
2381 nvp->v_mountedhere != NULL) {
2382 new_mp = nvp->v_mountedhere;
2383 r = vfs_busy(new_mp, 0);
2387 r = VFS_ROOT(new_mp,
2397 ((nd->nd_flag & ND_NFSV3) ||
2398 NFSNONZERO_ATTRBIT(&attrbits))) {
2399 r = nfsvno_getfh(nvp, &nfh, p);
2401 r = nfsvno_getattr(nvp, nvap, nd, p,
2403 if (r == 0 && is_zfs == 1 &&
2404 nfsrv_enable_crossmntpt != 0 &&
2405 (nd->nd_flag & ND_NFSV4) != 0 &&
2406 nvp->v_type == VDIR &&
2407 vp->v_mount != nvp->v_mount) {
2409 * For a ZFS snapshot, there is a
2410 * pseudo mount that does not set
2411 * v_mountedhere, so it needs to
2412 * be detected via a different
2417 new_mp = nvp->v_mount;
2424 if (!NFSISSET_ATTRBIT(&attrbits,
2425 NFSATTRBIT_RDATTRERROR)) {
2428 if (needs_unbusy != 0)
2437 * Build the directory record xdr
2439 if (nd->nd_flag & ND_NFSV3) {
2440 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2441 *tl++ = newnfs_true;
2443 *tl = txdr_unsigned(dp->d_fileno);
2444 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2445 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2447 *tl = txdr_unsigned(*cookiep);
2448 nfsrv_postopattr(nd, 0, nvap);
2449 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2450 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2454 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2455 *tl++ = newnfs_true;
2457 *tl = txdr_unsigned(*cookiep);
2458 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2460 supports_nfsv4acls =
2461 nfs_supportsnfsv4acls(nvp);
2462 NFSVOPUNLOCK(nvp, 0);
2464 supports_nfsv4acls = 0;
2466 dirlen += nfsrv_putreferralattr(nd,
2469 if (nd->nd_repstat) {
2472 if (needs_unbusy != 0)
2477 dirlen += nfsvno_fillattr(nd, new_mp,
2478 nvp, nvap, &nfh, r, &rderrbits,
2479 nd->nd_cred, p, isdgram, 0,
2480 supports_nfsv4acls, at_root,
2483 dirlen += nfsvno_fillattr(nd, new_mp,
2484 nvp, nvap, &nfh, r, &attrbits,
2485 nd->nd_cred, p, isdgram, 0,
2486 supports_nfsv4acls, at_root,
2491 dirlen += (3 * NFSX_UNSIGNED);
2493 if (needs_unbusy != 0)
2498 cpos += dp->d_reclen;
2499 dp = (struct dirent *)cpos;
2507 * If dirlen > cnt, we must strip off the last entry. If that
2508 * results in an empty reply, report NFSERR_TOOSMALL.
2510 if (dirlen > cnt || nd->nd_repstat) {
2511 if (!nd->nd_repstat && entrycnt == 0)
2512 nd->nd_repstat = NFSERR_TOOSMALL;
2513 if (nd->nd_repstat) {
2514 newnfs_trimtrailing(nd, mb0, bpos0);
2515 if (nd->nd_flag & ND_NFSV3)
2516 nfsrv_postopattr(nd, getret, &at);
2518 newnfs_trimtrailing(nd, mb1, bpos1);
2520 } else if (cpos < cend)
2522 if (!nd->nd_repstat) {
2523 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2524 *tl++ = newnfs_false;
2530 free(cookies, M_TEMP);
2534 NFSEXITCODE2(0, nd);
2538 NFSEXITCODE2(error, nd);
2543 * Get the settable attributes out of the mbuf list.
2544 * (Return 0 or EBADRPC)
2547 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2548 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2551 struct nfsv2_sattr *sp;
2552 int error = 0, toclient = 0;
2554 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2556 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2558 * Some old clients didn't fill in the high order 16bits.
2559 * --> check the low order 2 bytes for 0xffff
2561 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2562 nvap->na_mode = nfstov_mode(sp->sa_mode);
2563 if (sp->sa_uid != newnfs_xdrneg1)
2564 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2565 if (sp->sa_gid != newnfs_xdrneg1)
2566 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2567 if (sp->sa_size != newnfs_xdrneg1)
2568 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2569 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2571 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2573 nvap->na_atime.tv_sec =
2574 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2575 nvap->na_atime.tv_nsec = 0;
2578 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2579 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2582 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2583 if (*tl == newnfs_true) {
2584 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2585 nvap->na_mode = nfstov_mode(*tl);
2587 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2588 if (*tl == newnfs_true) {
2589 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2590 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2592 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2593 if (*tl == newnfs_true) {
2594 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2595 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2597 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2598 if (*tl == newnfs_true) {
2599 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2600 nvap->na_size = fxdr_hyper(tl);
2602 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2603 switch (fxdr_unsigned(int, *tl)) {
2604 case NFSV3SATTRTIME_TOCLIENT:
2605 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2606 fxdr_nfsv3time(tl, &nvap->na_atime);
2609 case NFSV3SATTRTIME_TOSERVER:
2610 vfs_timestamp(&nvap->na_atime);
2611 nvap->na_vaflags |= VA_UTIMES_NULL;
2614 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2615 switch (fxdr_unsigned(int, *tl)) {
2616 case NFSV3SATTRTIME_TOCLIENT:
2617 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2618 fxdr_nfsv3time(tl, &nvap->na_mtime);
2619 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2621 case NFSV3SATTRTIME_TOSERVER:
2622 vfs_timestamp(&nvap->na_mtime);
2624 nvap->na_vaflags |= VA_UTIMES_NULL;
2629 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2632 NFSEXITCODE2(error, nd);
2637 * Handle the setable attributes for V4.
2638 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2641 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2642 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2647 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2649 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2653 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2656 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2657 attrsize = fxdr_unsigned(int, *tl);
2660 * Loop around getting the setable attributes. If an unsupported
2661 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2664 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2665 bitpos = NFSATTRBIT_MAX;
2669 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2670 if (attrsum > attrsize) {
2671 error = NFSERR_BADXDR;
2674 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2676 case NFSATTRBIT_SIZE:
2677 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2678 if (vp != NULL && vp->v_type != VREG) {
2679 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2683 nvap->na_size = fxdr_hyper(tl);
2684 attrsum += NFSX_HYPER;
2686 case NFSATTRBIT_ACL:
2687 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2691 if (aceerr && !nd->nd_repstat)
2692 nd->nd_repstat = aceerr;
2695 case NFSATTRBIT_ARCHIVE:
2696 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2697 if (!nd->nd_repstat)
2698 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2699 attrsum += NFSX_UNSIGNED;
2701 case NFSATTRBIT_HIDDEN:
2702 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2703 if (!nd->nd_repstat)
2704 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2705 attrsum += NFSX_UNSIGNED;
2707 case NFSATTRBIT_MIMETYPE:
2708 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2709 i = fxdr_unsigned(int, *tl);
2710 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2713 if (!nd->nd_repstat)
2714 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2715 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2717 case NFSATTRBIT_MODE:
2718 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2719 nvap->na_mode = nfstov_mode(*tl);
2720 attrsum += NFSX_UNSIGNED;
2722 case NFSATTRBIT_OWNER:
2723 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2724 j = fxdr_unsigned(int, *tl);
2726 error = NFSERR_BADXDR;
2729 if (j > NFSV4_SMALLSTR)
2730 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2733 error = nfsrv_mtostr(nd, cp, j);
2735 if (j > NFSV4_SMALLSTR)
2736 free(cp, M_NFSSTRING);
2739 if (!nd->nd_repstat) {
2740 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2742 if (!nd->nd_repstat)
2745 if (j > NFSV4_SMALLSTR)
2746 free(cp, M_NFSSTRING);
2747 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2749 case NFSATTRBIT_OWNERGROUP:
2750 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2751 j = fxdr_unsigned(int, *tl);
2753 error = NFSERR_BADXDR;
2756 if (j > NFSV4_SMALLSTR)
2757 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2760 error = nfsrv_mtostr(nd, cp, j);
2762 if (j > NFSV4_SMALLSTR)
2763 free(cp, M_NFSSTRING);
2766 if (!nd->nd_repstat) {
2767 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2769 if (!nd->nd_repstat)
2772 if (j > NFSV4_SMALLSTR)
2773 free(cp, M_NFSSTRING);
2774 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2776 case NFSATTRBIT_SYSTEM:
2777 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2778 if (!nd->nd_repstat)
2779 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2780 attrsum += NFSX_UNSIGNED;
2782 case NFSATTRBIT_TIMEACCESSSET:
2783 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2784 attrsum += NFSX_UNSIGNED;
2785 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2786 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2787 fxdr_nfsv4time(tl, &nvap->na_atime);
2789 attrsum += NFSX_V4TIME;
2791 vfs_timestamp(&nvap->na_atime);
2792 nvap->na_vaflags |= VA_UTIMES_NULL;
2795 case NFSATTRBIT_TIMEBACKUP:
2796 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2797 if (!nd->nd_repstat)
2798 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2799 attrsum += NFSX_V4TIME;
2801 case NFSATTRBIT_TIMECREATE:
2802 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2803 if (!nd->nd_repstat)
2804 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2805 attrsum += NFSX_V4TIME;
2807 case NFSATTRBIT_TIMEMODIFYSET:
2808 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2809 attrsum += NFSX_UNSIGNED;
2810 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2811 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2812 fxdr_nfsv4time(tl, &nvap->na_mtime);
2813 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2814 attrsum += NFSX_V4TIME;
2816 vfs_timestamp(&nvap->na_mtime);
2818 nvap->na_vaflags |= VA_UTIMES_NULL;
2822 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2824 * set bitpos so we drop out of the loop.
2826 bitpos = NFSATTRBIT_MAX;
2832 * some clients pad the attrlist, so we need to skip over the
2835 if (attrsum > attrsize) {
2836 error = NFSERR_BADXDR;
2838 attrsize = NFSM_RNDUP(attrsize);
2839 if (attrsum < attrsize)
2840 error = nfsm_advance(nd, attrsize - attrsum, -1);
2843 NFSEXITCODE2(error, nd);
2848 * Check/setup export credentials.
2851 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2852 struct ucred *credanon)
2857 * Check/setup credentials.
2859 if (nd->nd_flag & ND_GSS)
2860 exp->nes_exflag &= ~MNT_EXPORTANON;
2863 * Check to see if the operation is allowed for this security flavor.
2864 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2865 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2866 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2868 if (nfsvno_testexp(nd, exp) &&
2869 nd->nd_procnum != NFSV4OP_SECINFO &&
2870 nd->nd_procnum != NFSPROC_FSINFO) {
2871 if (nd->nd_flag & ND_NFSV4)
2872 error = NFSERR_WRONGSEC;
2874 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2879 * Check to see if the file system is exported V4 only.
2881 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2882 error = NFSERR_PROGNOTV4;
2887 * Now, map the user credentials.
2888 * (Note that ND_AUTHNONE will only be set for an NFSv3
2889 * Fsinfo RPC. If set for anything else, this code might need
2892 if (NFSVNO_EXPORTED(exp)) {
2893 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2894 NFSVNO_EXPORTANON(exp) ||
2895 (nd->nd_flag & ND_AUTHNONE) != 0) {
2896 nd->nd_cred->cr_uid = credanon->cr_uid;
2897 nd->nd_cred->cr_gid = credanon->cr_gid;
2898 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2899 credanon->cr_groups);
2900 } else if ((nd->nd_flag & ND_GSS) == 0) {
2902 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2903 * if there is a replacement credential with a group
2904 * list set up by "nfsuserd -manage-gids".
2905 * If there is no replacement, nfsrv_getgrpscred()
2906 * simply returns its argument.
2908 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2913 NFSEXITCODE2(error, nd);
2921 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
2922 struct ucred **credp)
2924 int i, error, *secflavors;
2926 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2927 &exp->nes_numsecflavor, &secflavors);
2929 if (nfs_rootfhset) {
2930 exp->nes_exflag = 0;
2931 exp->nes_numsecflavor = 0;
2935 /* Copy the security flavors. */
2936 for (i = 0; i < exp->nes_numsecflavor; i++)
2937 exp->nes_secflavors[i] = secflavors[i];
2944 * Get a vnode for a file handle and export stuff.
2947 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
2948 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
2949 struct ucred **credp)
2951 int i, error, *secflavors;
2954 exp->nes_numsecflavor = 0;
2955 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
2957 /* Make sure the server replies ESTALE to the client. */
2959 if (nam && !error) {
2960 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
2961 &exp->nes_numsecflavor, &secflavors);
2963 if (nfs_rootfhset) {
2964 exp->nes_exflag = 0;
2965 exp->nes_numsecflavor = 0;
2971 /* Copy the security flavors. */
2972 for (i = 0; i < exp->nes_numsecflavor; i++)
2973 exp->nes_secflavors[i] = secflavors[i];
2981 * nfsd_fhtovp() - convert a fh to a vnode ptr
2982 * - look up fsid in mount list (if not found ret error)
2983 * - get vp and export rights by calling nfsvno_fhtovp()
2984 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
2986 * - if mpp != NULL, return the mount point so that it can
2987 * be used for vn_finished_write() by the caller
2990 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
2991 struct vnode **vpp, struct nfsexstuff *exp,
2992 struct mount **mpp, int startwrite, struct thread *p)
2995 struct ucred *credanon;
2998 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3000 * Check for the special case of the nfsv4root_fh.
3002 mp = vfs_busyfs(&fhp->fh_fsid);
3007 nd->nd_repstat = ESTALE;
3012 vn_start_write(NULL, mpp, V_WAIT);
3013 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3014 lktype = LK_EXCLUSIVE;
3016 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3021 * For NFSv4 without a pseudo root fs, unexported file handles
3022 * can be returned, so that Lookup works everywhere.
3024 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3025 !(nd->nd_flag & ND_NFSV4)) {
3027 nd->nd_repstat = EACCES;
3031 * Personally, I've never seen any point in requiring a
3032 * reserved port#, since only in the rare case where the
3033 * clients are all boxes with secure system privileges,
3034 * does it provide any enhanced security, but... some people
3035 * believe it to be useful and keep putting this code back in.
3036 * (There is also some "security checker" out there that
3037 * complains if the nfs server doesn't enforce this.)
3038 * However, note the following:
3039 * RFC3530 (NFSv4) specifies that a reserved port# not be
3041 * RFC2623 recommends that, if a reserved port# is checked for,
3042 * that there be a way to turn that off--> ifdef'd.
3044 #ifdef NFS_REQRSVPORT
3045 if (!nd->nd_repstat) {
3046 struct sockaddr_in *saddr;
3047 struct sockaddr_in6 *saddr6;
3049 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3050 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3051 if (!(nd->nd_flag & ND_NFSV4) &&
3052 ((saddr->sin_family == AF_INET &&
3053 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3054 (saddr6->sin6_family == AF_INET6 &&
3055 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3057 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3060 #endif /* NFS_REQRSVPORT */
3063 * Check/setup credentials.
3065 if (!nd->nd_repstat) {
3066 nd->nd_saveduid = nd->nd_cred->cr_uid;
3067 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
3071 if (credanon != NULL)
3073 if (nd->nd_repstat) {
3075 vn_finished_write(mp);
3082 NFSEXITCODE2(0, nd);
3089 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3091 struct filedesc *fdp;
3095 fdp = p->td_proc->p_fd;
3096 if (fd < 0 || fd >= fdp->fd_nfiles ||
3097 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3109 * Called from nfssvc() to update the exports list. Just call
3110 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3111 * in the mount list.
3114 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3116 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3118 struct nameidata nd;
3121 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3122 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3124 else if (error == 0) {
3125 if (nfsexargp->fspec == NULL) {
3130 * If fspec != NULL, this is the v4root path.
3132 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3133 nfsexargp->fspec, p);
3134 if ((error = namei(&nd)) != 0)
3136 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3139 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3140 NFSBCOPY((caddr_t)&fh,
3141 nfs_rootfh.nfsrvfh_data,
3142 sizeof (fhandle_t));
3153 * This function needs to test to see if the system is near its limit
3154 * for memory allocation via malloc() or mget() and return True iff
3155 * either of these resources are near their limit.
3156 * XXX (For now, this is just a stub.)
3158 int nfsrv_testmalloclimit = 0;
3160 nfsrv_mallocmget_limit(void)
3162 static int printmesg = 0;
3163 static int testval = 1;
3165 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3166 if ((printmesg++ % 100) == 0)
3167 printf("nfsd: malloc/mget near limit\n");
3174 * BSD specific initialization of a mount point.
3179 static int inited = 0;
3184 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3185 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3186 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
3187 nfsv4root_mnt.mnt_export = NULL;
3188 TAILQ_INIT(&nfsv4root_opt);
3189 TAILQ_INIT(&nfsv4root_newopt);
3190 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3191 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3192 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3193 nfsv4root_mnt.mnt_activevnodelistsize = 0;
3197 * Get a vnode for a file handle, without checking exports, etc.
3200 nfsvno_getvp(fhandle_t *fhp)
3206 mp = vfs_busyfs(&fhp->fh_fsid);
3209 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3217 * Do a local VOP_ADVLOCK().
3220 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3221 u_int64_t end, struct thread *td)
3227 if (nfsrv_dolocallocks == 0)
3229 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3231 fl.l_whence = SEEK_SET;
3233 fl.l_start = (off_t)first;
3234 if (end == NFS64BITSSET) {
3238 fl.l_len = (off_t)tlen;
3241 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3242 * values for all calls, so that all locks will be held by the
3243 * nfsd server. (The nfsd server handles conflicts between the
3245 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3246 * bytes, so it can't be put in l_sysid.
3248 if (nfsv4_sysid == 0)
3249 nfsv4_sysid = nlm_acquire_next_sysid();
3250 fl.l_pid = (pid_t)0;
3251 fl.l_sysid = (int)nfsv4_sysid;
3253 if (ftype == F_UNLCK)
3254 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3255 (F_POSIX | F_REMOTE));
3257 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3258 (F_POSIX | F_REMOTE));
3266 * Check the nfsv4 root exports.
3269 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3271 struct ucred *credanon;
3272 int exflags, error = 0, numsecflavor, *secflavors, i;
3274 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3275 &credanon, &numsecflavor, &secflavors);
3277 error = NFSERR_PROGUNAVAIL;
3280 if (credanon != NULL)
3282 for (i = 0; i < numsecflavor; i++) {
3283 if (secflavors[i] == AUTH_SYS)
3284 nd->nd_flag |= ND_EXAUTHSYS;
3285 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3286 nd->nd_flag |= ND_EXGSS;
3287 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3288 nd->nd_flag |= ND_EXGSSINTEGRITY;
3289 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3290 nd->nd_flag |= ND_EXGSSPRIVACY;
3299 * Nfs server pseudo system call for the nfsd's
3305 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3308 struct nfsd_addsock_args sockarg;
3309 struct nfsd_nfsd_args nfsdarg;
3310 struct nfsd_nfsd_oargs onfsdarg;
3311 struct nfsd_pnfsd_args pnfsdarg;
3312 struct vnode *vp, *nvp, *curdvp;
3313 struct pnfsdsfile *pf;
3314 struct nfsdevice *ds, *fds;
3315 cap_rights_t rights;
3316 int buflen, error, ret;
3317 char *buf, *cp, *cp2, *cp3;
3318 char fname[PNFS_FILENAME_LEN + 1];
3320 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3321 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3325 * Since we don't know what rights might be required,
3326 * pretend that we need them all. It is better to be too
3327 * careful than too reckless.
3329 error = fget(td, sockarg.sock,
3330 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3333 if (fp->f_type != DTYPE_SOCKET) {
3338 error = nfsrvd_addsock(fp);
3340 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3341 if (uap->argp == NULL) {
3345 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3346 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3348 nfsdarg.principal = onfsdarg.principal;
3349 nfsdarg.minthreads = onfsdarg.minthreads;
3350 nfsdarg.maxthreads = onfsdarg.maxthreads;
3351 nfsdarg.version = 1;
3352 nfsdarg.addr = NULL;
3353 nfsdarg.addrlen = 0;
3354 nfsdarg.dnshost = NULL;
3355 nfsdarg.dnshostlen = 0;
3356 nfsdarg.dspath = NULL;
3357 nfsdarg.dspathlen = 0;
3358 nfsdarg.mdspath = NULL;
3359 nfsdarg.mdspathlen = 0;
3360 nfsdarg.mirrorcnt = 1;
3363 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3366 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3367 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3368 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3369 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3370 nfsdarg.mirrorcnt >= 1 &&
3371 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3372 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3373 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3374 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3375 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3376 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3377 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3378 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3379 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3384 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3386 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3387 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3389 free(nfsdarg.addr, M_TEMP);
3393 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3394 nfsdarg.dnshost = cp;
3395 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3396 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3398 free(nfsdarg.addr, M_TEMP);
3399 free(nfsdarg.dnshost, M_TEMP);
3403 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3404 nfsdarg.dspath = cp;
3405 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3406 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3408 free(nfsdarg.addr, M_TEMP);
3409 free(nfsdarg.dnshost, M_TEMP);
3410 free(nfsdarg.dspath, M_TEMP);
3414 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3415 nfsdarg.mdspath = cp;
3417 nfsdarg.addr = NULL;
3418 nfsdarg.addrlen = 0;
3419 nfsdarg.dnshost = NULL;
3420 nfsdarg.dnshostlen = 0;
3421 nfsdarg.dspath = NULL;
3422 nfsdarg.dspathlen = 0;
3423 nfsdarg.mdspath = NULL;
3424 nfsdarg.mdspathlen = 0;
3425 nfsdarg.mirrorcnt = 1;
3427 error = nfsrvd_nfsd(td, &nfsdarg);
3428 free(nfsdarg.addr, M_TEMP);
3429 free(nfsdarg.dnshost, M_TEMP);
3430 free(nfsdarg.dspath, M_TEMP);
3431 free(nfsdarg.mdspath, M_TEMP);
3432 } else if (uap->flag & NFSSVC_PNFSDS) {
3433 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3434 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3435 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3436 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3437 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3440 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3442 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3443 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3444 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3445 buf = malloc(buflen, M_TEMP, M_WAITOK);
3446 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3448 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3449 if (error == 0 && pnfsdarg.dspath != NULL) {
3450 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3451 error = copyinstr(pnfsdarg.dspath, cp2,
3452 PATH_MAX + 1, NULL);
3453 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3457 if (error == 0 && pnfsdarg.curdspath != NULL) {
3458 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3459 error = copyinstr(pnfsdarg.curdspath, cp3,
3460 PATH_MAX + 1, NULL);
3461 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3468 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3469 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3471 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3473 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3474 printf("copymr: dsdir out of range\n");
3477 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3478 error = nfsrv_copymr(vp, nvp,
3479 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3480 (struct pnfsdsfile *)buf,
3481 buflen / sizeof(*pf), td->td_ucred, td);
3484 if (fds != NULL && error == 0) {
3485 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3486 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3488 nfsrv_dsremove(curdvp, fname,
3490 NFSVOPUNLOCK(curdvp, 0);
3493 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3501 error = nfssvc_srvcall(td, uap, td->td_ucred);
3510 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3512 struct nfsex_args export;
3513 struct file *fp = NULL;
3515 struct nfsd_clid adminrevoke;
3516 struct nfsd_dumplist dumplist;
3517 struct nfsd_dumpclients *dumpclients;
3518 struct nfsd_dumplocklist dumplocklist;
3519 struct nfsd_dumplocks *dumplocks;
3520 struct nameidata nd;
3522 int error = EINVAL, igotlock;
3524 static int suspend_nfsd = 0;
3526 if (uap->flag & NFSSVC_PUBLICFH) {
3527 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3528 sizeof (fhandle_t));
3529 error = copyin(uap->argp,
3530 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3533 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3534 error = copyin(uap->argp,(caddr_t)&export,
3535 sizeof (struct nfsex_args));
3537 error = nfsrv_v4rootexport(&export, cred, p);
3538 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3541 } else if (uap->flag & NFSSVC_STABLERESTART) {
3542 error = copyin(uap->argp, (caddr_t)&stablefd,
3545 error = fp_getfvp(p, stablefd, &fp, &vp);
3546 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3548 if (!error && newnfs_numnfsd != 0)
3551 nfsrv_stablefirst.nsf_fp = fp;
3552 nfsrv_setupstable(p);
3554 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3555 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3556 sizeof (struct nfsd_clid));
3558 error = nfsrv_adminrevoke(&adminrevoke, p);
3559 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3560 error = copyin(uap->argp, (caddr_t)&dumplist,
3561 sizeof (struct nfsd_dumplist));
3562 if (!error && (dumplist.ndl_size < 1 ||
3563 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3566 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3567 dumpclients = (struct nfsd_dumpclients *)malloc(len,
3569 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3570 error = copyout(dumpclients,
3571 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3572 free(dumpclients, M_TEMP);
3574 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3575 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3576 sizeof (struct nfsd_dumplocklist));
3577 if (!error && (dumplocklist.ndllck_size < 1 ||
3578 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3581 error = nfsrv_lookupfilename(&nd,
3582 dumplocklist.ndllck_fname, p);
3584 len = sizeof (struct nfsd_dumplocks) *
3585 dumplocklist.ndllck_size;
3586 dumplocks = (struct nfsd_dumplocks *)malloc(len,
3588 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3589 dumplocklist.ndllck_size, p);
3591 error = copyout(dumplocks,
3592 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3593 free(dumplocks, M_TEMP);
3595 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3598 nfsd_master_pid = procp->p_pid;
3599 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3600 nfsd_master_start = procp->p_stats->p_start;
3601 nfsd_master_proc = procp;
3603 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3604 NFSLOCKV4ROOTMUTEX();
3605 if (suspend_nfsd == 0) {
3606 /* Lock out all nfsd threads */
3608 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3609 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3610 } while (igotlock == 0 && suspend_nfsd == 0);
3613 NFSUNLOCKV4ROOTMUTEX();
3615 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3616 NFSLOCKV4ROOTMUTEX();
3617 if (suspend_nfsd != 0) {
3618 nfsv4_unlock(&nfsd_suspend_lock, 0);
3621 NFSUNLOCKV4ROOTMUTEX();
3631 * Returns 0 if ok, 1 otherwise.
3634 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3639 * This seems odd, but allow the case where the security flavor
3640 * list is empty. This happens when NFSv4 is traversing non-exported
3641 * file systems. Exported file systems should always have a non-empty
3642 * security flavor list.
3644 if (exp->nes_numsecflavor == 0)
3647 for (i = 0; i < exp->nes_numsecflavor; i++) {
3649 * The tests for privacy and integrity must be first,
3650 * since ND_GSS is set for everything but AUTH_SYS.
3652 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3653 (nd->nd_flag & ND_GSSPRIVACY))
3655 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3656 (nd->nd_flag & ND_GSSINTEGRITY))
3658 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3659 (nd->nd_flag & ND_GSS))
3661 if (exp->nes_secflavors[i] == AUTH_SYS &&
3662 (nd->nd_flag & ND_GSS) == 0)
3669 * Calculate a hash value for the fid in a file handle.
3672 nfsrv_hashfh(fhandle_t *fhp)
3676 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3681 * Calculate a hash value for the sessionid.
3684 nfsrv_hashsessionid(uint8_t *sessionid)
3688 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3693 * Signal the userland master nfsd to backup the stable restart file.
3696 nfsrv_backupstable(void)
3700 if (nfsd_master_proc != NULL) {
3701 procp = pfind(nfsd_master_pid);
3702 /* Try to make sure it is the correct process. */
3703 if (procp == nfsd_master_proc &&
3704 procp->p_stats->p_start.tv_sec ==
3705 nfsd_master_start.tv_sec &&
3706 procp->p_stats->p_start.tv_usec ==
3707 nfsd_master_start.tv_usec &&
3708 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3709 kern_psignal(procp, SIGUSR2);
3711 nfsd_master_proc = NULL;
3719 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
3720 * The arguments are in a structure, so that they can be passed through
3721 * taskqueue for a kernel process to execute this function.
3723 struct nfsrvdscreate {
3727 struct ucred *tcred;
3730 struct pnfsdsfile *pf;
3734 struct vattr createva;
3738 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
3739 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
3740 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
3743 struct nameidata named;
3748 struct nfsmount *nmp;
3751 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
3752 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
3753 nfsvno_setpathbuf(&named, &bufp, &hashp);
3754 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
3755 named.ni_cnd.cn_thread = p;
3756 named.ni_cnd.cn_nameptr = bufp;
3757 if (fnamep != NULL) {
3758 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
3759 named.ni_cnd.cn_namelen = strlen(bufp);
3761 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
3762 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
3764 /* Create the date file in the DS mount. */
3765 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
3767 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
3768 NFSVOPUNLOCK(dvp, 0);
3770 /* Set the ownership of the file. */
3771 error = VOP_SETATTR(nvp, nvap, tcred);
3772 NFSD_DEBUG(4, "nfsrv_dscreate:"
3773 " setattr-uid=%d\n", error);
3778 printf("pNFS: pnfscreate failed=%d\n", error);
3780 printf("pNFS: pnfscreate vnlock=%d\n", error);
3783 nmp = VFSTONFS(nvp->v_mount);
3784 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
3785 != 0 || nmp->nm_nam->sa_len > sizeof(
3786 struct sockaddr_in6) ||
3787 np->n_fhp->nfh_len != NFSX_MYFH) {
3788 printf("Bad DS file: fstype=%s salen=%d"
3790 nvp->v_mount->mnt_vfc->vfc_name,
3791 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
3795 /* Set extattrs for the DS on the MDS file. */
3798 error = VOP_GETATTR(nvp, &va, tcred);
3800 dsa->dsa_filerev = va.va_filerev;
3801 dsa->dsa_size = va.va_size;
3802 dsa->dsa_atime = va.va_atime;
3803 dsa->dsa_mtime = va.va_mtime;
3807 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
3809 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
3810 nmp->nm_nam->sa_len);
3811 NFSBCOPY(named.ni_cnd.cn_nameptr,
3813 sizeof(pf->dsf_filename));
3816 printf("pNFS: pnfscreate can't get DS"
3817 " attr=%d\n", error);
3818 if (nvpp != NULL && error == 0)
3823 nfsvno_relpathbuf(&named);
3828 * Start up the thread that will execute nfsrv_dscreate().
3831 start_dscreate(void *arg, int pending)
3833 struct nfsrvdscreate *dsc;
3835 dsc = (struct nfsrvdscreate *)arg;
3836 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
3837 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
3839 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
3843 * Create a pNFS data file on the Data Server(s).
3846 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
3849 struct nfsrvdscreate *dsc, *tdsc;
3850 struct nfsdevice *ds, *tds, *fds;
3852 struct pnfsdsfile *pf, *tpf;
3853 struct pnfsdsattr dsattr;
3855 struct vnode *dvp[NFSDEV_MAXMIRRORS];
3856 struct nfsmount *nmp;
3861 struct ucred *tcred;
3862 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
3865 /* Get a DS server directory in a round-robin order. */
3871 * Search for the first entry that handles this MDS fs, but use the
3872 * first entry for all MDS fs's otherwise.
3874 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
3875 if (tds->nfsdev_nmp != NULL) {
3876 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
3878 else if (tds->nfsdev_mdsisset != 0 &&
3879 mp->mnt_stat.f_fsid.val[0] ==
3880 tds->nfsdev_mdsfsid.val[0] &&
3881 mp->mnt_stat.f_fsid.val[1] ==
3882 tds->nfsdev_mdsfsid.val[1]) {
3890 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
3893 i = dsdir[0] = ds->nfsdev_nextdir;
3894 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
3895 dvp[0] = ds->nfsdev_dsdir[i];
3896 tds = TAILQ_NEXT(ds, nfsdev_list);
3897 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
3898 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
3899 if (tds->nfsdev_nmp != NULL &&
3900 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
3901 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
3902 mp->mnt_stat.f_fsid.val[0] ==
3903 tds->nfsdev_mdsfsid.val[0] &&
3904 mp->mnt_stat.f_fsid.val[1] ==
3905 tds->nfsdev_mdsfsid.val[1]))) {
3906 dsdir[mirrorcnt] = i;
3907 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
3909 if (mirrorcnt >= nfsrv_maxpnfsmirror)
3914 /* Put at end of list to implement round-robin usage. */
3915 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
3916 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
3920 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
3922 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
3925 error = nfsvno_getfh(vp, &fh, p);
3927 error = VOP_GETATTR(vp, &va, cred);
3929 /* Set the attributes for "vp" to Setattr the DS vp. */
3932 vamode = va.va_mode;
3936 va.va_mode = vamode;
3939 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
3941 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
3943 /* Make data file name based on FH. */
3944 tcred = newnfs_getcred();
3947 * Create the file on each DS mirror, using kernel process(es) for the
3948 * additional mirrors.
3951 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
3952 tpf->dsf_dir = dsdir[i];
3953 tdsc->tcred = tcred;
3956 tdsc->createva = *vap;
3957 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
3964 if (nfs_pnfsiothreads != 0) {
3965 ret = nfs_pnfsio(start_dscreate, tdsc);
3966 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
3969 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
3970 NULL, tcred, p, NULL);
3972 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
3974 if (failpos == -1 && nfsds_failerr(ret))
3982 tpf->dsf_dir = dsdir[mirrorcnt - 1];
3983 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
3984 &dsattr, NULL, tcred, p, NULL);
3985 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
3986 failpos = mirrorcnt - 1;
3990 timo = hz / 50; /* Wait for 20msec. */
3993 /* Wait for kernel task(s) to complete. */
3994 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
3995 while (tdsc->inprog != 0 && tdsc->done == 0)
3996 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
3997 if (tdsc->err != 0) {
3998 if (failpos == -1 && nfsds_failerr(tdsc->err))
4000 else if (error == 0)
4006 * If failpos has been set, that mirror has failed, so it needs
4010 nmp = VFSTONFS(dvp[failpos]->v_mount);
4012 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4013 NFSMNTP_CANCELRPCS)) == 0) {
4014 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4016 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4017 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4020 nfsrv_killrpcs(nmp);
4022 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4030 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4032 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4033 mirrorcnt, nfsrv_maxpnfsmirror);
4035 * For all mirrors that couldn't be created, fill in the
4036 * *pf structure, but with an IP address == 0.0.0.0.
4038 tpf = pf + mirrorcnt;
4039 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4041 tpf->dsf_sin.sin_family = AF_INET;
4042 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4043 tpf->dsf_sin.sin_addr.s_addr = 0;
4044 tpf->dsf_sin.sin_port = 0;
4047 error = vn_start_write(vp, &mp, V_WAIT);
4049 error = vn_extattr_set(vp, IO_NODELOCKED,
4050 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4051 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4053 error = vn_extattr_set(vp, IO_NODELOCKED,
4054 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4055 sizeof(dsattr), (char *)&dsattr, p);
4056 vn_finished_write(mp);
4058 printf("pNFS: pnfscreate setextattr=%d\n",
4061 printf("pNFS: pnfscreate startwrite=%d\n", error);
4063 printf("pNFS: pnfscreate=%d\n", error);
4069 * Get the information needed to remove the pNFS Data Server file from the
4070 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4071 * DS directory vnode. The caller must unlock *ddvp when done with it.
4074 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4075 int *mirrorcntp, char *fname, fhandle_t *fhp)
4078 struct ucred *tcred;
4083 /* If not an exported regular file or not a pNFS server, just return. */
4084 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4085 nfsrv_devidcnt == 0)
4088 /* Check to see if this is the last hard link. */
4089 tcred = newnfs_getcred();
4090 error = VOP_GETATTR(vp, &va, tcred);
4093 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4096 if (va.va_nlink > 1)
4099 error = nfsvno_getfh(vp, fhp, p);
4101 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4106 buf = malloc(buflen, M_TEMP, M_WAITOK);
4107 /* Get the directory vnode for the DS mount and the file handle. */
4108 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4109 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4112 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4116 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4117 * The arguments are in a structure, so that they can be passed through
4118 * taskqueue for a kernel process to execute this function.
4120 struct nfsrvdsremove {
4124 struct ucred *tcred;
4128 char fname[PNFS_FILENAME_LEN + 1];
4132 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4135 struct nameidata named;
4141 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4144 named.ni_cnd.cn_nameiop = DELETE;
4145 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4146 named.ni_cnd.cn_cred = tcred;
4147 named.ni_cnd.cn_thread = p;
4148 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4149 nfsvno_setpathbuf(&named, &bufp, &hashp);
4150 named.ni_cnd.cn_nameptr = bufp;
4151 named.ni_cnd.cn_namelen = strlen(fname);
4152 strlcpy(bufp, fname, NAME_MAX);
4153 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4154 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4155 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4157 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4160 NFSVOPUNLOCK(dvp, 0);
4161 nfsvno_relpathbuf(&named);
4163 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4168 * Start up the thread that will execute nfsrv_dsremove().
4171 start_dsremove(void *arg, int pending)
4173 struct nfsrvdsremove *dsrm;
4175 dsrm = (struct nfsrvdsremove *)arg;
4176 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4179 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4183 * Remove a pNFS data file from a Data Server.
4184 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4185 * removed to set up the dvp and fill in the FH.
4188 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4191 struct ucred *tcred;
4192 struct nfsrvdsremove *dsrm, *tdsrm;
4193 struct nfsdevice *ds;
4194 struct nfsmount *nmp;
4195 int failpos, i, ret, timo;
4197 tcred = newnfs_getcred();
4200 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4202 * Remove the file on each DS mirror, using kernel process(es) for the
4203 * additional mirrors.
4206 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4207 tdsrm->tcred = tcred;
4209 tdsrm->dvp = dvp[i];
4210 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4215 if (nfs_pnfsiothreads != 0) {
4216 ret = nfs_pnfsio(start_dsremove, tdsrm);
4217 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4220 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4221 if (failpos == -1 && nfsds_failerr(ret))
4225 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4226 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4227 failpos = mirrorcnt - 1;
4228 timo = hz / 50; /* Wait for 20msec. */
4231 /* Wait for kernel task(s) to complete. */
4232 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4233 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4234 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4235 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4240 * If failpos has been set, that mirror has failed, so it needs
4244 nmp = VFSTONFS(dvp[failpos]->v_mount);
4246 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4247 NFSMNTP_CANCELRPCS)) == 0) {
4248 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4250 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4251 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4254 nfsrv_killrpcs(nmp);
4256 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4262 /* Get rid all layouts for the file. */
4263 nfsrv_freefilelayouts(fhp);
4270 * Generate a file name based on the file handle and put it in *bufp.
4271 * Return the number of bytes generated.
4274 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4278 const uint8_t *hexdigits = "0123456789abcdef";
4280 cp = (uint8_t *)fhp;
4281 for (i = 0; i < sizeof(*fhp); i++) {
4282 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4283 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4290 * Update the Metadata file's attributes from the DS file when a Read/Write
4291 * layout is returned.
4292 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4293 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4296 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4298 struct ucred *tcred;
4301 /* Do this as root so that it won't fail with EACCES. */
4302 tcred = newnfs_getcred();
4303 error = nfsrv_proxyds(NULL, vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4304 NULL, NULL, NULL, nap, NULL);
4310 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4313 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4318 error = nfsrv_proxyds(NULL, vp, 0, 0, cred, p, NFSPROC_SETACL,
4319 NULL, NULL, NULL, NULL, aclp);
4324 nfsrv_proxyds(struct nfsrv_descript *nd, struct vnode *vp, off_t off, int cnt,
4325 struct ucred *cred, struct thread *p, int ioproc, struct mbuf **mpp,
4326 char *cp, struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp)
4328 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4329 fhandle_t fh[NFSDEV_MAXMIRRORS];
4330 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4331 struct nfsdevice *ds;
4332 struct pnfsdsattr dsattr;
4334 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4336 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4338 * If not a regular file, not exported or not a pNFS server,
4339 * just return ENOENT.
4341 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4342 nfsrv_devidcnt == 0)
4346 buf = malloc(buflen, M_TEMP, M_WAITOK);
4350 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4351 * from the MetaData file's extended attribute.
4353 if (ioproc == NFSPROC_GETATTR) {
4354 error = vn_extattr_get(vp, IO_NODELOCKED,
4355 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4357 if (error == 0 && buflen != sizeof(dsattr))
4360 NFSBCOPY(buf, &dsattr, buflen);
4361 nap->na_filerev = dsattr.dsa_filerev;
4362 nap->na_size = dsattr.dsa_size;
4363 nap->na_atime = dsattr.dsa_atime;
4364 nap->na_mtime = dsattr.dsa_mtime;
4367 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4368 * returns 0, just return now. nfsrv_checkdsattr()
4369 * returns 0 if there is no Read/Write layout
4370 * plus either an Open/Write_access or Write
4371 * delegation issued to a client for the file.
4373 if (nfsrv_pnfsgetdsattr == 0 ||
4374 nfsrv_checkdsattr(nd, vp, p) == 0) {
4381 * Clear ENOATTR so the code below will attempt to do a
4382 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4383 * the extended attribute.
4385 if (error == ENOATTR)
4394 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4395 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4398 for (i = 0; i < mirrorcnt; i++)
4399 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4401 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4403 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4406 if (origmircnt == -1)
4407 origmircnt = mirrorcnt;
4409 * If failpos is set to a mirror#, then that mirror has
4410 * failed and will be disabled. For Read and Getattr, the
4411 * function only tries one mirror, so if that mirror has
4412 * failed, it will need to be retried. As such, increment
4413 * tryitagain for these cases.
4414 * For Write, Setattr and Setacl, the function tries all
4415 * mirrors and will not return an error for the case where
4416 * one mirror has failed. For these cases, the functioning
4417 * mirror(s) will have been modified, so a retry isn't
4418 * necessary. These functions will set failpos for the
4421 if (ioproc == NFSPROC_READDS) {
4422 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4424 if (nfsds_failerr(error) && mirrorcnt > 1) {
4426 * Setting failpos will cause the mirror
4427 * to be disabled and then a retry of this
4434 } else if (ioproc == NFSPROC_WRITEDS)
4435 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4436 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4437 else if (ioproc == NFSPROC_SETATTR)
4438 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4439 mirrorcnt, nap, &failpos);
4440 else if (ioproc == NFSPROC_SETACL)
4441 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4442 mirrorcnt, aclp, &failpos);
4444 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4445 vp, nmp[mirrorcnt - 1], nap);
4446 if (nfsds_failerr(error) && mirrorcnt > 1) {
4448 * Setting failpos will cause the mirror
4449 * to be disabled and then a retry of this
4450 * getattr is required.
4452 failpos = mirrorcnt - 1;
4459 failnmp = nmp[failpos];
4460 NFSLOCKMNT(failnmp);
4461 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4462 NFSMNTP_CANCELRPCS)) == 0) {
4463 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4464 NFSUNLOCKMNT(failnmp);
4465 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4467 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4470 nfsrv_killrpcs(failnmp);
4471 NFSLOCKMNT(failnmp);
4472 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4475 NFSUNLOCKMNT(failnmp);
4477 for (i = 0; i < mirrorcnt; i++)
4478 NFSVOPUNLOCK(dvp[i], 0);
4479 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4481 /* Try the Read/Getattr again if a mirror was deleted. */
4482 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4485 /* Return ENOENT for any Extended Attribute error. */
4489 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4494 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4496 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4497 * to be checked. If it points to a NULL nmp, then it returns
4498 * a suitable destination.
4499 * curnmp - If non-NULL, it is the source mount for the copy.
4502 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4503 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4504 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4505 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4507 struct vnode *dvp, *nvp, **tdvpp;
4509 struct nfsmount *nmp, *newnmp;
4510 struct sockaddr *sad;
4511 struct sockaddr_in *sin;
4512 struct nfsdevice *ds, *tds, *fndds;
4513 struct pnfsdsfile *pf;
4515 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4517 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4526 if (newnmpp != NULL)
4531 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4532 "pnfsd.dsfile", buflenp, buf, p);
4533 mirrorcnt = *buflenp / sizeof(*pf);
4534 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4535 *buflenp != sizeof(*pf) * mirrorcnt))
4538 pf = (struct pnfsdsfile *)buf;
4539 /* If curnmp != NULL, check for a match in the mirror list. */
4540 if (curnmp != NULL && error == 0) {
4542 for (i = 0; i < mirrorcnt; i++, pf++) {
4543 sad = (struct sockaddr *)&pf->dsf_sin;
4544 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
4556 pf = (struct pnfsdsfile *)buf;
4557 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
4559 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
4561 sad = (struct sockaddr *)&pf->dsf_sin;
4563 dsdir = pf->dsf_dir;
4564 if (dsdir >= nfsrv_dsdirsize) {
4565 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
4567 } else if (nvpp != NULL && newnmp != NULL &&
4568 nfsaddr2_match(sad, newnmp->nm_nam))
4571 if (ippos != NULL && curnmp == NULL &&
4572 sad->sa_family == AF_INET &&
4573 sin->sin_addr.s_addr == 0)
4575 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
4577 /* Use the socket address to find the mount point. */
4580 /* Find a match for the IP address. */
4581 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
4582 if (ds->nfsdev_nmp != NULL) {
4583 dvp = ds->nfsdev_dvp;
4584 nmp = VFSTONFS(dvp->v_mount);
4585 if (nmp != ds->nfsdev_nmp)
4586 printf("different2 nmp %p %p\n",
4587 nmp, ds->nfsdev_nmp);
4588 if (nfsaddr2_match(sad, nmp->nm_nam)) {
4594 if (fndds != NULL && newnmpp != NULL &&
4596 /* Search for a place to make a mirror copy. */
4597 TAILQ_FOREACH(tds, &nfsrv_devidhead,
4599 if (tds->nfsdev_nmp != NULL &&
4601 ((tds->nfsdev_mdsisset == 0 &&
4602 fndds->nfsdev_mdsisset == 0) ||
4603 (tds->nfsdev_mdsisset != 0 &&
4604 fndds->nfsdev_mdsisset != 0 &&
4605 tds->nfsdev_mdsfsid.val[0] ==
4606 mp->mnt_stat.f_fsid.val[0] &&
4607 tds->nfsdev_mdsfsid.val[1] ==
4608 mp->mnt_stat.f_fsid.val[1]))) {
4609 *newnmpp = tds->nfsdev_nmp;
4615 * Move this entry to the end of the
4616 * list, so it won't be selected as
4617 * easily the next time.
4619 TAILQ_REMOVE(&nfsrv_devidhead, tds,
4621 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
4626 if (fndds != NULL) {
4627 dvp = fndds->nfsdev_dsdir[dsdir];
4628 if (lktype != 0 || fhiszero != 0 ||
4629 (nvpp != NULL && *nvpp == NULL)) {
4631 error = vn_lock(dvp,
4633 else if (lktype != 0)
4634 error = vn_lock(dvp, lktype);
4636 error = vn_lock(dvp, LK_SHARED);
4638 * If the file handle is all 0's, try to
4639 * do a Lookup against the DS to acquire
4641 * If dvpp == NULL or the Lookup fails,
4642 * unlock dvp after the call.
4644 if (error == 0 && (fhiszero != 0 ||
4645 (nvpp != NULL && *nvpp == NULL))) {
4646 error = nfsrv_pnfslookupds(vp,
4660 if (error != 0 || lktype == 0)
4661 NFSVOPUNLOCK(dvp, 0);
4666 NFSD_DEBUG(4, "gotone=%d\n", gotone);
4667 if (devid != NULL) {
4668 NFSBCOPY(fndds->nfsdev_deviceid,
4669 devid, NFSX_V4DEVICEID);
4670 devid += NFSX_V4DEVICEID;
4675 NFSBCOPY(&pf->dsf_fh, fhp++,
4677 if (fnamep != NULL && gotone == 1)
4680 sizeof(pf->dsf_filename));
4682 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
4687 if (error == 0 && gotone == 0)
4690 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
4693 *mirrorcntp = gotone;
4695 if (gotone > 0 && dvpp != NULL) {
4697 * If the error didn't occur on the first one and
4698 * dvpp != NULL, the one(s) prior to the failure will
4699 * have locked dvp's that need to be unlocked.
4701 for (i = 0; i < gotone; i++) {
4702 NFSVOPUNLOCK(*dvpp, 0);
4707 * If it found the vnode to be copied from before a failure,
4708 * it needs to be vput()'d.
4710 if (nvpp != NULL && *nvpp != NULL) {
4719 * Set the extended attribute for the Change attribute.
4722 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4724 struct pnfsdsattr dsattr;
4728 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
4729 error = vn_start_write(vp, &mp, V_WAIT);
4731 dsattr.dsa_filerev = nap->na_filerev;
4732 dsattr.dsa_size = nap->na_size;
4733 dsattr.dsa_atime = nap->na_atime;
4734 dsattr.dsa_mtime = nap->na_mtime;
4735 error = vn_extattr_set(vp, IO_NODELOCKED,
4736 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4737 sizeof(dsattr), (char *)&dsattr, p);
4738 vn_finished_write(mp);
4741 printf("pNFS: setextattr=%d\n", error);
4746 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
4747 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
4750 struct nfsrv_descript *nd;
4752 struct mbuf *m, *m2;
4753 int error = 0, retlen, tlen, trimlen;
4755 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
4756 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4759 * Use a stateid where other is an alternating 01010 pattern and
4760 * seqid is 0xffffffff. This value is not defined as special by
4761 * the RFC and is used by the FreeBSD NFS server to indicate an
4762 * MDS->DS proxy operation.
4764 st.other[0] = 0x55555555;
4765 st.other[1] = 0x55555555;
4766 st.other[2] = 0x55555555;
4767 st.seqid = 0xffffffff;
4768 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
4770 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
4771 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
4772 txdr_hyper(off, tl);
4773 *(tl + 2) = txdr_unsigned(len);
4774 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4775 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4780 if (nd->nd_repstat == 0) {
4781 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4782 NFSM_STRSIZ(retlen, len);
4784 /* Trim off the pre-data XDR from the mbuf chain. */
4786 while (m != NULL && m != nd->nd_md) {
4787 if (m->m_next == nd->nd_md) {
4789 m_freem(nd->nd_mrep);
4790 nd->nd_mrep = m = nd->nd_md;
4795 printf("nfsrv_readdsrpc: busted mbuf list\n");
4801 * Now, adjust first mbuf so that any XDR before the
4802 * read data is skipped over.
4804 trimlen = nd->nd_dpos - mtod(m, char *);
4806 m->m_len -= trimlen;
4807 NFSM_DATAP(m, trimlen);
4811 * Truncate the mbuf chain at retlen bytes of data,
4812 * plus XDR padding that brings the length up to a
4815 tlen = NFSM_RNDUP(retlen);
4817 if (m->m_len >= tlen) {
4827 } while (m != NULL);
4829 printf("nfsrv_readdsrpc: busted mbuf list\n");
4838 error = nd->nd_repstat;
4840 /* If nd->nd_mrep is already NULL, this is a no-op. */
4841 m_freem(nd->nd_mrep);
4843 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
4848 * Do a write RPC on a DS data file, using this structure for the arguments,
4849 * so that this function can be executed by a separate kernel process.
4851 struct nfsrvwritedsdorpc {
4858 struct nfsmount *nmp;
4866 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
4867 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
4870 struct nfsrv_descript *nd;
4871 nfsattrbit_t attrbits;
4873 int commit, error, retlen;
4875 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4876 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
4877 sizeof(fhandle_t), NULL, NULL, 0, 0);
4880 * Use a stateid where other is an alternating 01010 pattern and
4881 * seqid is 0xffffffff. This value is not defined as special by
4882 * the RFC and is used by the FreeBSD NFS server to indicate an
4883 * MDS->DS proxy operation.
4885 st.other[0] = 0x55555555;
4886 st.other[1] = 0x55555555;
4887 st.other[2] = 0x55555555;
4888 st.seqid = 0xffffffff;
4889 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
4890 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
4891 txdr_hyper(off, tl);
4894 * Do all writes FileSync, since the server doesn't hold onto dirty
4895 * buffers. Since clients should be accessing the DS servers directly
4896 * using the pNFS layouts, this just needs to work correctly as a
4899 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
4900 *tl = txdr_unsigned(len);
4901 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
4903 /* Put data in mbuf chain. */
4904 nd->nd_mb->m_next = m;
4906 /* Set nd_mb and nd_bpos to end of data. */
4907 while (m->m_next != NULL)
4910 nd->nd_bpos = mtod(m, char *) + m->m_len;
4911 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
4913 /* Do a Getattr for Size, Change and Modify Time. */
4914 NFSZERO_ATTRBIT(&attrbits);
4915 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
4916 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
4917 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
4918 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
4919 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4920 *tl = txdr_unsigned(NFSV4OP_GETATTR);
4921 (void) nfsrv_putattrbit(nd, &attrbits);
4922 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
4923 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4928 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
4929 /* Get rid of weak cache consistency data for now. */
4930 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
4931 (ND_NFSV4 | ND_V4WCCATTR)) {
4932 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
4933 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
4934 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
4938 * Get rid of Op# and status for next op.
4940 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4942 nd->nd_flag |= ND_NOMOREDATA;
4944 if (nd->nd_repstat == 0) {
4945 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
4946 retlen = fxdr_unsigned(int, *tl++);
4947 commit = fxdr_unsigned(int, *tl);
4948 if (commit != NFSWRITE_FILESYNC)
4950 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
4951 retlen, commit, error);
4953 error = nd->nd_repstat;
4954 /* We have no use for the Write Verifier since we use FileSync. */
4957 * Get the Change, Size, Access Time and Modify Time attributes and set
4958 * on the Metadata file, so its attributes will be what the file's
4959 * would be if it had been written.
4962 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
4963 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
4964 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
4966 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
4968 m_freem(nd->nd_mrep);
4970 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
4975 * Start up the thread that will execute nfsrv_writedsdorpc().
4978 start_writedsdorpc(void *arg, int pending)
4980 struct nfsrvwritedsdorpc *drpc;
4982 drpc = (struct nfsrvwritedsdorpc *)arg;
4983 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
4984 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
4986 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
4990 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
4991 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
4992 struct mbuf **mpp, char *cp, int *failposp)
4994 struct nfsrvwritedsdorpc *drpc, *tdrpc;
4997 int error, i, offs, ret, timo;
4999 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5000 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5003 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5006 /* Calculate offset in mbuf chain that data starts. */
5007 offs = cp - mtod(*mpp, char *);
5008 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5011 * Do the write RPC for every DS, using a separate kernel process
5012 * for every DS except the last one.
5015 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5017 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5025 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5027 if (nfs_pnfsiothreads != 0) {
5028 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5029 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5033 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5035 if (nfsds_failerr(ret) && *failposp == -1)
5037 else if (error == 0 && ret != 0)
5043 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5044 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5045 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5046 *failposp = mirrorcnt - 1;
5047 else if (error == 0 && ret != 0)
5050 error = nfsrv_setextattr(vp, &na, p);
5051 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5053 timo = hz / 50; /* Wait for 20msec. */
5056 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5057 /* Wait for RPCs on separate threads to complete. */
5058 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5059 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5060 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5062 else if (error == 0 && tdrpc->err != 0)
5070 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5071 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5072 struct nfsvattr *dsnap)
5075 struct nfsrv_descript *nd;
5077 nfsattrbit_t attrbits;
5080 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5081 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5083 * Use a stateid where other is an alternating 01010 pattern and
5084 * seqid is 0xffffffff. This value is not defined as special by
5085 * the RFC and is used by the FreeBSD NFS server to indicate an
5086 * MDS->DS proxy operation.
5088 st.other[0] = 0x55555555;
5089 st.other[1] = 0x55555555;
5090 st.other[2] = 0x55555555;
5091 st.seqid = 0xffffffff;
5092 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5094 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5095 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5097 /* Do a Getattr for Size, Change, Access Time and Modify Time. */
5098 NFSZERO_ATTRBIT(&attrbits);
5099 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5100 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5101 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5102 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5103 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5104 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5105 (void) nfsrv_putattrbit(nd, &attrbits);
5106 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5107 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5112 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5114 /* Get rid of weak cache consistency data for now. */
5115 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5116 (ND_NFSV4 | ND_V4WCCATTR)) {
5117 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5118 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5119 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5123 * Get rid of Op# and status for next op.
5125 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5127 nd->nd_flag |= ND_NOMOREDATA;
5129 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5132 if (nd->nd_repstat != 0)
5133 error = nd->nd_repstat;
5135 * Get the Change, Size, Access Time and Modify Time attributes and set
5136 * on the Metadata file, so its attributes will be what the file's
5137 * would be if it had been written.
5140 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5141 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5142 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5144 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5146 m_freem(nd->nd_mrep);
5148 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5152 struct nfsrvsetattrdsdorpc {
5157 struct nfsmount *nmp;
5162 struct nfsvattr dsna;
5167 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5170 start_setattrdsdorpc(void *arg, int pending)
5172 struct nfsrvsetattrdsdorpc *drpc;
5174 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5175 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5176 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5181 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5182 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5183 struct nfsvattr *nap, int *failposp)
5185 struct nfsrvsetattrdsdorpc *drpc, *tdrpc;
5187 int error, i, ret, timo;
5189 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5192 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5196 * Do the setattr RPC for every DS, using a separate kernel process
5197 * for every DS except the last one.
5200 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5203 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5211 if (nfs_pnfsiothreads != 0) {
5212 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5213 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5217 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5219 if (nfsds_failerr(ret) && *failposp == -1)
5221 else if (error == 0 && ret != 0)
5227 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5228 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5229 *failposp = mirrorcnt - 1;
5230 else if (error == 0 && ret != 0)
5233 error = nfsrv_setextattr(vp, &na, p);
5234 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5236 timo = hz / 50; /* Wait for 20msec. */
5239 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5240 /* Wait for RPCs on separate threads to complete. */
5241 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5242 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5243 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5245 else if (error == 0 && tdrpc->err != 0)
5253 * Do a Setattr of an NFSv4 ACL on the DS file.
5256 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5257 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5259 struct nfsrv_descript *nd;
5261 nfsattrbit_t attrbits;
5264 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5265 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5267 * Use a stateid where other is an alternating 01010 pattern and
5268 * seqid is 0xffffffff. This value is not defined as special by
5269 * the RFC and is used by the FreeBSD NFS server to indicate an
5270 * MDS->DS proxy operation.
5272 st.other[0] = 0x55555555;
5273 st.other[1] = 0x55555555;
5274 st.other[2] = 0x55555555;
5275 st.seqid = 0xffffffff;
5276 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5278 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5279 NFSZERO_ATTRBIT(&attrbits);
5280 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5282 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5283 * so passing in the metadata "vp" will be ok, since it is of
5284 * the same type (VREG).
5286 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5287 NULL, 0, 0, 0, 0, 0, NULL);
5288 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5289 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5294 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5296 error = nd->nd_repstat;
5297 m_freem(nd->nd_mrep);
5302 struct nfsrvsetacldsdorpc {
5307 struct nfsmount *nmp;
5316 * Start up the thread that will execute nfsrv_setacldsdorpc().
5319 start_setacldsdorpc(void *arg, int pending)
5321 struct nfsrvsetacldsdorpc *drpc;
5323 drpc = (struct nfsrvsetacldsdorpc *)arg;
5324 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5325 drpc->vp, drpc->nmp, drpc->aclp);
5330 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5331 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5334 struct nfsrvsetacldsdorpc *drpc, *tdrpc;
5335 int error, i, ret, timo;
5337 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5340 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5344 * Do the setattr RPC for every DS, using a separate kernel process
5345 * for every DS except the last one.
5348 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5351 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5359 if (nfs_pnfsiothreads != 0) {
5360 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5361 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5365 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5367 if (nfsds_failerr(ret) && *failposp == -1)
5369 else if (error == 0 && ret != 0)
5375 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5376 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5377 *failposp = mirrorcnt - 1;
5378 else if (error == 0 && ret != 0)
5380 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5382 timo = hz / 50; /* Wait for 20msec. */
5385 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5386 /* Wait for RPCs on separate threads to complete. */
5387 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5388 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
5389 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5391 else if (error == 0 && tdrpc->err != 0)
5399 * Getattr call to the DS for the Modify, Size and Change attributes.
5402 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5403 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
5405 struct nfsrv_descript *nd;
5407 nfsattrbit_t attrbits;
5409 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
5410 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5411 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
5412 sizeof(fhandle_t), NULL, NULL, 0, 0);
5413 NFSZERO_ATTRBIT(&attrbits);
5414 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5415 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5416 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5417 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5418 (void) nfsrv_putattrbit(nd, &attrbits);
5419 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5420 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5425 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
5427 if (nd->nd_repstat == 0) {
5428 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5429 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5432 * We can only save the updated values in the extended
5433 * attribute if the vp is exclusively locked.
5434 * This should happen when any of the following operations
5435 * occur on the vnode:
5436 * Close, Delegreturn, LayoutCommit, LayoutReturn
5437 * As such, the updated extended attribute should get saved
5438 * before nfsrv_checkdsattr() returns 0 and allows the cached
5439 * attributes to be returned without calling this function.
5441 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
5442 error = nfsrv_setextattr(vp, nap, p);
5443 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
5447 error = nd->nd_repstat;
5448 m_freem(nd->nd_mrep);
5450 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
5455 * Get the device id and file handle for a DS file.
5458 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
5459 fhandle_t *fhp, char *devid)
5465 buf = malloc(buflen, M_TEMP, M_WAITOK);
5466 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
5467 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
5473 * Do a Lookup against the DS for the filename.
5476 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
5477 struct vnode **nvpp, NFSPROC_T *p)
5479 struct nameidata named;
5480 struct ucred *tcred;
5486 tcred = newnfs_getcred();
5487 named.ni_cnd.cn_nameiop = LOOKUP;
5488 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
5489 named.ni_cnd.cn_cred = tcred;
5490 named.ni_cnd.cn_thread = p;
5491 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
5492 nfsvno_setpathbuf(&named, &bufp, &hashp);
5493 named.ni_cnd.cn_nameptr = bufp;
5494 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
5495 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
5496 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
5497 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
5498 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
5500 nfsvno_relpathbuf(&named);
5503 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
5508 * Set the file handle to the correct one.
5511 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, struct vnode *nvp,
5519 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
5521 * We can only do a setextattr for an exclusively
5522 * locked vp. Instead of trying to upgrade a shared
5523 * lock, just leave dsf_fh zeroed out and it will
5524 * keep doing this lookup until it is done with an
5525 * exclusively locked vp.
5527 if (NFSVOPISLOCKED(vp) == LK_EXCLUSIVE) {
5528 ret = vn_start_write(vp, &mp, V_WAIT);
5529 NFSD_DEBUG(4, "nfsrv_pnfssetfh: vn_start_write=%d\n",
5532 ret = vn_extattr_set(vp, IO_NODELOCKED,
5533 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
5534 sizeof(*pf), (char *)pf, p);
5535 vn_finished_write(mp);
5536 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft "
5537 "vn_extattr_set=%d\n", ret);
5540 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
5544 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
5545 * when the DS has failed.
5548 nfsrv_killrpcs(struct nfsmount *nmp)
5552 * Call newnfs_nmcancelreqs() to cause
5553 * any RPCs in progress on the mount point to
5555 * This will cause any process waiting for an
5556 * RPC to complete while holding a vnode lock
5557 * on the mounted-on vnode (such as "df" or
5558 * a non-forced "umount") to fail.
5559 * This will unlock the mounted-on vnode so
5560 * a forced dismount can succeed.
5561 * The NFSMNTP_CANCELRPCS flag should be set when this function is
5564 newnfs_nmcancelreqs(nmp);
5568 * Sum up the statfs info for each of the DSs, so that the client will
5569 * receive the total for all DSs.
5572 nfsrv_pnfsstatfs(struct statfs *sf)
5575 struct nfsdevice *ds;
5576 struct vnode **dvpp, **tdvpp, *dvp;
5578 int cnt, error = 0, i;
5580 if (nfsrv_devidcnt <= 0)
5582 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
5583 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
5585 /* Get an array of the dvps for the DSs. */
5589 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5590 if (ds->nfsdev_nmp != NULL) {
5591 if (++i > nfsrv_devidcnt)
5593 *tdvpp++ = ds->nfsdev_dvp;
5599 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
5601 for (i = 0; i < cnt && error == 0; i++) {
5603 error = VFS_STATFS(dvp->v_mount, tsf);
5605 if (sf->f_bsize == 0) {
5606 if (tsf->f_bsize > 0)
5607 sf->f_bsize = tsf->f_bsize;
5611 if (tsf->f_blocks > 0) {
5612 if (sf->f_bsize != tsf->f_bsize) {
5613 tot = tsf->f_blocks * tsf->f_bsize;
5614 sf->f_blocks += (tot / sf->f_bsize);
5616 sf->f_blocks += tsf->f_blocks;
5618 if (tsf->f_bfree > 0) {
5619 if (sf->f_bsize != tsf->f_bsize) {
5620 tot = tsf->f_bfree * tsf->f_bsize;
5621 sf->f_bfree += (tot / sf->f_bsize);
5623 sf->f_bfree += tsf->f_bfree;
5625 if (tsf->f_bavail > 0) {
5626 if (sf->f_bsize != tsf->f_bsize) {
5627 tot = tsf->f_bavail * tsf->f_bsize;
5628 sf->f_bavail += (tot / sf->f_bsize);
5630 sf->f_bavail += tsf->f_bavail;
5643 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
5647 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
5648 error = NFSERR_ATTRNOTSUPP;
5652 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
5653 * Make sure it has enough room for that - splitting every entry
5654 * into two and appending "canonical six" entries at the end.
5655 * Cribbed out of kern/vfs_acl.c - Rick M.
5657 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
5658 error = NFSERR_ATTRNOTSUPP;
5661 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
5663 error = nfsrv_dssetacl(vp, aclp, cred, p);
5664 if (error == ENOENT)
5673 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
5676 * Called once to initialize data structures...
5679 nfsd_modevent(module_t mod, int type, void *data)
5682 static int loaded = 0;
5689 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
5690 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
5692 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
5695 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
5696 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
5697 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
5698 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
5699 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
5700 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
5707 #ifdef VV_DISABLEDELEG
5708 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
5709 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
5711 nfsd_call_servertimer = nfsrv_servertimer;
5712 nfsd_call_nfsd = nfssvc_nfsd;
5717 if (newnfs_numnfsd != 0) {
5722 #ifdef VV_DISABLEDELEG
5723 vn_deleg_ops.vndeleg_recall = NULL;
5724 vn_deleg_ops.vndeleg_disable = NULL;
5726 nfsd_call_servertimer = NULL;
5727 nfsd_call_nfsd = NULL;
5729 /* Clean out all NFSv4 state. */
5730 nfsrv_throwawayallstate(curthread);
5732 /* Clean the NFS server reply cache */
5733 nfsrvd_cleancache();
5735 /* Free up the krpc server pool. */
5736 if (nfsrvd_pool != NULL)
5737 svcpool_destroy(nfsrvd_pool);
5739 /* and get rid of the locks */
5740 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
5741 mtx_destroy(&nfsrchash_table[i].mtx);
5742 mtx_destroy(&nfsrcahash_table[i].mtx);
5744 mtx_destroy(&nfsrc_udpmtx);
5745 mtx_destroy(&nfs_v4root_mutex);
5746 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
5747 mtx_destroy(&nfsrv_dontlistlock_mtx);
5748 mtx_destroy(&nfsrv_recalllock_mtx);
5749 for (i = 0; i < nfsrv_sessionhashsize; i++)
5750 mtx_destroy(&nfssessionhash[i].mtx);
5751 if (nfslayouthash != NULL) {
5752 for (i = 0; i < nfsrv_layouthashsize; i++)
5753 mtx_destroy(&nfslayouthash[i].mtx);
5754 free(nfslayouthash, M_NFSDSESSION);
5756 lockdestroy(&nfsv4root_mnt.mnt_explock);
5757 free(nfsclienthash, M_NFSDCLIENT);
5758 free(nfslockhash, M_NFSDLOCKFILE);
5759 free(nfssessionhash, M_NFSDSESSION);
5771 static moduledata_t nfsd_mod = {
5776 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
5778 /* So that loader and kldload(2) can find us, wherever we are.. */
5779 MODULE_VERSION(nfsd, 1);
5780 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
5781 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
5782 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
5783 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
5784 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);