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 *, char *, char *,
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 *, struct mount *);
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)
445 u_quad_t savsize = 0;
449 * If this is an exported file system and a pNFS service is running,
450 * don't VOP_SETATTR() of size for the MDS file system.
454 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
455 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
456 nvap->na_vattr.va_size > 0) {
457 savsize = nvap->na_vattr.va_size;
458 nvap->na_vattr.va_size = VNOVAL;
459 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
460 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
461 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
462 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
463 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
469 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
471 nvap->na_vattr.va_size = savsize;
472 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
473 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
474 nvap->na_vattr.va_size != VNOVAL ||
475 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
476 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
477 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
478 /* For a pNFS server, set the attributes on the DS file. */
479 error = nfsrv_proxyds(NULL, vp, 0, 0, cred, p, NFSPROC_SETATTR,
480 NULL, NULL, NULL, nvap, NULL);
489 * Set up nameidata for a lookup() call and do it.
492 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
493 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
494 struct vnode **retdirp)
496 struct componentname *cnp = &ndp->ni_cnd;
500 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
505 cnp->cn_nameptr = cnp->cn_pnbuf;
508 * Extract and set starting directory.
510 if (dp->v_type != VDIR) {
515 nfsvno_relpathbuf(ndp);
523 if (NFSVNO_EXRDONLY(exp))
524 cnp->cn_flags |= RDONLY;
525 ndp->ni_segflg = UIO_SYSSPACE;
527 if (nd->nd_flag & ND_PUBLOOKUP) {
529 if (cnp->cn_pnbuf[0] == '/') {
532 * Check for degenerate pathnames here, since lookup()
535 for (i = 1; i < ndp->ni_pathlen; i++)
536 if (cnp->cn_pnbuf[i] != '/')
538 if (i == ndp->ni_pathlen) {
539 error = NFSERR_ACCES;
545 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
546 (nd->nd_flag & ND_NFSV4) == 0) {
548 * Only cross mount points for NFSv4 when doing a
549 * mount while traversing the file system above
550 * the mount point, unless nfsrv_enable_crossmntpt is set.
552 cnp->cn_flags |= NOCROSSMOUNT;
556 * Initialize for scan, set ni_startdir and bump ref on dp again
557 * because lookup() will dereference ni_startdir.
561 ndp->ni_startdir = dp;
562 ndp->ni_rootdir = rootvnode;
563 ndp->ni_topdir = NULL;
566 cnp->cn_flags |= LOCKLEAF;
568 cnp->cn_nameptr = cnp->cn_pnbuf;
570 * Call lookup() to do the real work. If an error occurs,
571 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
572 * we do not have to dereference anything before returning.
573 * In either case ni_startdir will be dereferenced and NULLed
581 * Check for encountering a symbolic link. Trivial
582 * termination occurs if no symlink encountered.
584 if ((cnp->cn_flags & ISSYMLINK) == 0) {
585 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
586 nfsvno_relpathbuf(ndp);
587 if (ndp->ni_vp && !lockleaf)
588 NFSVOPUNLOCK(ndp->ni_vp, 0);
595 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
596 NFSVOPUNLOCK(ndp->ni_dvp, 0);
597 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
602 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
606 if (ndp->ni_pathlen > 1)
607 cp = uma_zalloc(namei_zone, M_WAITOK);
611 aiov.iov_len = MAXPATHLEN;
612 auio.uio_iov = &aiov;
615 auio.uio_rw = UIO_READ;
616 auio.uio_segflg = UIO_SYSSPACE;
618 auio.uio_resid = MAXPATHLEN;
619 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
622 if (ndp->ni_pathlen > 1)
623 uma_zfree(namei_zone, cp);
629 linklen = MAXPATHLEN - auio.uio_resid;
634 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
635 error = ENAMETOOLONG;
640 * Adjust or replace path
642 if (ndp->ni_pathlen > 1) {
643 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
644 uma_zfree(namei_zone, cnp->cn_pnbuf);
647 cnp->cn_pnbuf[linklen] = '\0';
648 ndp->ni_pathlen += linklen;
651 * Cleanup refs for next loop and check if root directory
652 * should replace current directory. Normally ni_dvp
653 * becomes the new base directory and is cleaned up when
654 * we loop. Explicitly null pointers after invalidation
655 * to clarify operation.
660 if (cnp->cn_pnbuf[0] == '/') {
662 ndp->ni_dvp = ndp->ni_rootdir;
665 ndp->ni_startdir = ndp->ni_dvp;
669 cnp->cn_flags &= ~LOCKLEAF;
673 nfsvno_relpathbuf(ndp);
676 ndp->ni_startdir = NULL;
677 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
682 NFSEXITCODE2(error, nd);
687 * Set up a pathname buffer and return a pointer to it and, optionally
688 * set a hash pointer.
691 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
693 struct componentname *cnp = &ndp->ni_cnd;
695 cnp->cn_flags |= (NOMACCHECK | HASBUF);
696 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
699 *bufpp = cnp->cn_pnbuf;
703 * Release the above path buffer, if not released by nfsvno_namei().
706 nfsvno_relpathbuf(struct nameidata *ndp)
709 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
711 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
712 ndp->ni_cnd.cn_flags &= ~HASBUF;
716 * Readlink vnode op into an mbuf list.
719 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
720 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
722 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN];
723 struct iovec *ivp = iv;
724 struct uio io, *uiop = &io;
725 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL;
726 int i, len, tlen, error = 0;
730 while (len < NFS_MAXPATHLEN) {
732 MCLGET(mp, M_WAITOK);
733 mp->m_len = M_SIZE(mp);
740 if ((len + mp->m_len) > NFS_MAXPATHLEN) {
741 mp->m_len = NFS_MAXPATHLEN - len;
742 len = NFS_MAXPATHLEN;
746 ivp->iov_base = mtod(mp, caddr_t);
747 ivp->iov_len = mp->m_len;
752 uiop->uio_iovcnt = i;
753 uiop->uio_offset = 0;
754 uiop->uio_resid = len;
755 uiop->uio_rw = UIO_READ;
756 uiop->uio_segflg = UIO_SYSSPACE;
758 error = VOP_READLINK(vp, uiop, cred);
764 if (uiop->uio_resid > 0) {
765 len -= uiop->uio_resid;
766 tlen = NFSM_RNDUP(len);
767 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
779 * Read vnode op call into mbuf list.
782 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
783 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
789 int error = 0, len, left, siz, tlen, ioflag = 0;
790 struct mbuf *m2 = NULL, *m3;
791 struct uio io, *uiop = &io;
795 * Attempt to read from a DS file. A return of ENOENT implies
796 * there is no DS file to read.
798 error = nfsrv_proxyds(NULL, vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
799 NULL, mpendp, NULL, NULL);
803 len = left = NFSM_RNDUP(cnt);
806 * Generate the mbuf list with the uio_iov ref. to it.
813 siz = min(M_TRAILINGSPACE(m), left);
822 iv = malloc(i * sizeof (struct iovec),
824 uiop->uio_iov = iv2 = iv;
830 panic("nfsvno_read iov");
831 siz = min(M_TRAILINGSPACE(m), left);
833 iv->iov_base = mtod(m, caddr_t) + m->m_len;
842 uiop->uio_iovcnt = i;
843 uiop->uio_offset = off;
844 uiop->uio_resid = len;
845 uiop->uio_rw = UIO_READ;
846 uiop->uio_segflg = UIO_SYSSPACE;
848 nh = nfsrv_sequential_heuristic(uiop, vp);
849 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
850 /* XXX KDM make this more systematic? */
851 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
852 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
859 nh->nh_nextoff = uiop->uio_offset;
860 tlen = len - uiop->uio_resid;
861 cnt = cnt < tlen ? cnt : tlen;
862 tlen = NFSM_RNDUP(cnt);
866 } else if (len != tlen || tlen != cnt)
867 nfsrv_adj(m3, len - tlen, tlen - cnt);
877 * Write vnode op from an mbuf list.
880 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int *stable,
881 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
887 struct uio io, *uiop = &io;
891 * Attempt to write to a DS file. A return of ENOENT implies
892 * there is no DS file to write.
894 error = nfsrv_proxyds(NULL, vp, off, retlen, cred, p, NFSPROC_WRITEDS,
895 &mp, cp, NULL, NULL, NULL);
896 if (error != ENOENT) {
897 *stable = NFSWRITE_FILESYNC;
901 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
903 uiop->uio_iov = iv = ivp;
904 uiop->uio_iovcnt = cnt;
905 i = mtod(mp, caddr_t) + mp->m_len - cp;
909 panic("nfsvno_write");
920 cp = mtod(mp, caddr_t);
924 if (*stable == NFSWRITE_UNSTABLE)
925 ioflags = IO_NODELOCKED;
927 ioflags = (IO_SYNC | IO_NODELOCKED);
928 uiop->uio_resid = retlen;
929 uiop->uio_rw = UIO_WRITE;
930 uiop->uio_segflg = UIO_SYSSPACE;
932 uiop->uio_offset = off;
933 nh = nfsrv_sequential_heuristic(uiop, vp);
934 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
935 /* XXX KDM make this more systematic? */
936 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
937 error = VOP_WRITE(vp, uiop, ioflags, cred);
939 nh->nh_nextoff = uiop->uio_offset;
947 * Common code for creating a regular file (plus special files for V2).
950 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
951 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
952 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp)
957 error = nd->nd_repstat;
958 if (!error && ndp->ni_vp == NULL) {
959 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
960 vrele(ndp->ni_startdir);
961 error = VOP_CREATE(ndp->ni_dvp,
962 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
963 /* For a pNFS server, create the data file on a DS. */
964 if (error == 0 && nvap->na_type == VREG) {
966 * Create a data file on a DS for a pNFS server.
967 * This function just returns if not
968 * running a pNFS DS or the creation fails.
970 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
974 nfsvno_relpathbuf(ndp);
976 if (*exclusive_flagp) {
977 *exclusive_flagp = 0;
978 NFSVNO_ATTRINIT(nvap);
979 nvap->na_atime.tv_sec = cverf[0];
980 nvap->na_atime.tv_nsec = cverf[1];
981 error = VOP_SETATTR(ndp->ni_vp,
982 &nvap->na_vattr, nd->nd_cred);
986 error = NFSERR_NOTSUPP;
991 * NFS V2 Only. nfsrvd_mknod() does this for V3.
992 * (This implies, just get out on an error.)
994 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
995 nvap->na_type == VFIFO) {
996 if (nvap->na_type == VCHR && rdev == 0xffffffff)
997 nvap->na_type = VFIFO;
998 if (nvap->na_type != VFIFO &&
999 (error = priv_check_cred(nd->nd_cred,
1000 PRIV_VFS_MKNOD_DEV, 0))) {
1001 vrele(ndp->ni_startdir);
1002 nfsvno_relpathbuf(ndp);
1006 nvap->na_rdev = rdev;
1007 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1008 &ndp->ni_cnd, &nvap->na_vattr);
1010 nfsvno_relpathbuf(ndp);
1011 vrele(ndp->ni_startdir);
1015 vrele(ndp->ni_startdir);
1016 nfsvno_relpathbuf(ndp);
1024 * Handle cases where error is already set and/or
1026 * 1 - clean up the lookup
1027 * 2 - iff !error and na_size set, truncate it
1029 vrele(ndp->ni_startdir);
1030 nfsvno_relpathbuf(ndp);
1032 if (ndp->ni_dvp == *vpp)
1036 if (!error && nvap->na_size != VNOVAL) {
1037 error = nfsvno_accchk(*vpp, VWRITE,
1038 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1039 NFSACCCHK_VPISLOCKED, NULL);
1041 tempsize = nvap->na_size;
1042 NFSVNO_ATTRINIT(nvap);
1043 nvap->na_size = tempsize;
1044 error = VOP_SETATTR(*vpp,
1045 &nvap->na_vattr, nd->nd_cred);
1058 * Do a mknod vnode op.
1061 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1067 vtyp = nvap->na_type;
1069 * Iff doesn't exist, create it.
1072 vrele(ndp->ni_startdir);
1073 nfsvno_relpathbuf(ndp);
1079 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1080 vrele(ndp->ni_startdir);
1081 nfsvno_relpathbuf(ndp);
1083 error = NFSERR_BADTYPE;
1086 if (vtyp == VSOCK) {
1087 vrele(ndp->ni_startdir);
1088 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1089 &ndp->ni_cnd, &nvap->na_vattr);
1091 nfsvno_relpathbuf(ndp);
1093 if (nvap->na_type != VFIFO &&
1094 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) {
1095 vrele(ndp->ni_startdir);
1096 nfsvno_relpathbuf(ndp);
1100 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1101 &ndp->ni_cnd, &nvap->na_vattr);
1103 nfsvno_relpathbuf(ndp);
1104 vrele(ndp->ni_startdir);
1106 * Since VOP_MKNOD returns the ni_vp, I can't
1107 * see any reason to do the lookup.
1120 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1121 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1125 if (ndp->ni_vp != NULL) {
1126 if (ndp->ni_dvp == ndp->ni_vp)
1131 nfsvno_relpathbuf(ndp);
1135 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1138 nfsvno_relpathbuf(ndp);
1149 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1150 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1151 struct nfsexstuff *exp)
1156 vrele(ndp->ni_startdir);
1157 nfsvno_relpathbuf(ndp);
1158 if (ndp->ni_dvp == ndp->ni_vp)
1167 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1168 &nvap->na_vattr, pathcp);
1170 vrele(ndp->ni_startdir);
1171 nfsvno_relpathbuf(ndp);
1173 * Although FreeBSD still had the lookup code in
1174 * it for 7/current, there doesn't seem to be any
1175 * point, since VOP_SYMLINK() returns the ni_vp.
1176 * Just vput it for v2.
1178 if (!not_v2 && !error)
1187 * Parse symbolic link arguments.
1188 * This function has an ugly side effect. It will malloc() an area for
1189 * the symlink and set iov_base to point to it, only if it succeeds.
1190 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1194 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1195 struct thread *p, char **pathcpp, int *lenp)
1198 char *pathcp = NULL;
1200 struct nfsv2_sattr *sp;
1204 if ((nd->nd_flag & ND_NFSV3) &&
1205 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1207 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1208 len = fxdr_unsigned(int, *tl);
1209 if (len > NFS_MAXPATHLEN || len <= 0) {
1213 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1214 error = nfsrv_mtostr(nd, pathcp, len);
1217 if (nd->nd_flag & ND_NFSV2) {
1218 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1219 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1223 NFSEXITCODE2(0, nd);
1227 free(pathcp, M_TEMP);
1228 NFSEXITCODE2(error, nd);
1233 * Remove a non-directory object.
1236 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1237 struct thread *p, struct nfsexstuff *exp)
1239 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1240 int error = 0, mirrorcnt;
1241 char fname[PNFS_FILENAME_LEN + 1];
1246 if (vp->v_type == VDIR)
1247 error = NFSERR_ISDIR;
1249 error = nfsrv_checkremove(vp, 1, p);
1251 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1253 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1254 if (error == 0 && dsdvp[0] != NULL)
1255 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1256 if (ndp->ni_dvp == vp)
1261 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1262 nfsvno_relpathbuf(ndp);
1268 * Remove a directory.
1271 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1272 struct thread *p, struct nfsexstuff *exp)
1278 if (vp->v_type != VDIR) {
1283 * No rmdir "." please.
1285 if (ndp->ni_dvp == vp) {
1290 * The root of a mounted filesystem cannot be deleted.
1292 if (vp->v_vflag & VV_ROOT)
1296 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1297 if (ndp->ni_dvp == vp)
1302 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1303 nfsvno_relpathbuf(ndp);
1312 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1313 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1315 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1316 int error = 0, mirrorcnt;
1317 char fname[PNFS_FILENAME_LEN + 1];
1321 fvp = fromndp->ni_vp;
1323 vrele(fromndp->ni_dvp);
1328 tdvp = tondp->ni_dvp;
1331 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1332 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1334 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1335 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1338 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1339 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1344 * A rename to '.' or '..' results in a prematurely
1345 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1348 if ((tondp->ni_cnd.cn_namelen == 1 &&
1349 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1350 (tondp->ni_cnd.cn_namelen == 2 &&
1351 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1352 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1357 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1358 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1361 if (fvp->v_mount != tdvp->v_mount) {
1362 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1366 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1371 * If source and destination are the same, there is nothing to
1372 * do. Set error to -1 to indicate this.
1377 if (ndflag & ND_NFSV4) {
1378 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1379 error = nfsrv_checkremove(fvp, 0, p);
1380 NFSVOPUNLOCK(fvp, 0);
1384 error = nfsrv_checkremove(tvp, 1, p);
1387 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1388 * that the NFSv4 client won't be confused by the rename.
1389 * Since nfsd_recalldelegation() can only be called on an
1390 * unlocked vnode at this point and fvp is the file that will
1391 * still exist after the rename, just do fvp.
1393 nfsd_recalldelegation(fvp, p);
1395 if (error == 0 && tvp != NULL) {
1396 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1397 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1398 " dsdvp=%p\n", dsdvp[0]);
1402 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1403 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1412 vrele(fromndp->ni_dvp);
1419 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1420 * if the rename succeeded, the DS file for the tvp needs to be
1423 if (error == 0 && dsdvp[0] != NULL) {
1424 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1425 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1428 vrele(tondp->ni_startdir);
1429 nfsvno_relpathbuf(tondp);
1431 vrele(fromndp->ni_startdir);
1432 nfsvno_relpathbuf(fromndp);
1441 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1442 struct thread *p, struct nfsexstuff *exp)
1452 if (vp->v_mount != xp->v_mount)
1456 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1457 if ((vp->v_iflag & VI_DOOMED) == 0)
1458 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1461 if (ndp->ni_dvp == vp)
1465 NFSVOPUNLOCK(vp, 0);
1467 if (ndp->ni_dvp == ndp->ni_vp)
1474 nfsvno_relpathbuf(ndp);
1480 * Do the fsync() appropriate for the commit.
1483 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1489 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1490 * file is done. At this time VOP_FSYNC does not accept offset and
1491 * byte count parameters so call VOP_FSYNC the whole file for now.
1492 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1493 * File systems that do not use the buffer cache (as indicated
1494 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1496 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1497 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1499 * Give up and do the whole thing
1502 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1503 VM_OBJECT_WLOCK(vp->v_object);
1504 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1505 VM_OBJECT_WUNLOCK(vp->v_object);
1507 error = VOP_FSYNC(vp, MNT_WAIT, td);
1510 * Locate and synchronously write any buffers that fall
1511 * into the requested range. Note: we are assuming that
1512 * f_iosize is a power of 2.
1514 int iosize = vp->v_mount->mnt_stat.f_iosize;
1515 int iomask = iosize - 1;
1520 * Align to iosize boundary, super-align to page boundary.
1523 cnt += off & iomask;
1524 off &= ~(u_quad_t)iomask;
1526 if (off & PAGE_MASK) {
1527 cnt += off & PAGE_MASK;
1528 off &= ~(u_quad_t)PAGE_MASK;
1530 lblkno = off / iosize;
1533 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
1534 VM_OBJECT_WLOCK(vp->v_object);
1535 vm_object_page_clean(vp->v_object, off, off + cnt,
1537 VM_OBJECT_WUNLOCK(vp->v_object);
1546 * If we have a buffer and it is marked B_DELWRI we
1547 * have to lock and write it. Otherwise the prior
1548 * write is assumed to have already been committed.
1550 * gbincore() can return invalid buffers now so we
1551 * have to check that bit as well (though B_DELWRI
1552 * should not be set if B_INVAL is set there could be
1553 * a race here since we haven't locked the buffer).
1555 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1556 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1557 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1559 continue; /* retry */
1561 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1564 bp->b_flags &= ~B_ASYNC;
1587 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1593 if (nfsrv_devidcnt > 0) {
1594 /* For a pNFS service, get the DS numbers. */
1595 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1596 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1602 error = VFS_STATFS(vp->v_mount, sf);
1605 sf->f_blocks = tsf->f_blocks;
1606 sf->f_bavail = tsf->f_bavail;
1607 sf->f_bfree = tsf->f_bfree;
1608 sf->f_bsize = tsf->f_bsize;
1611 * Since NFS handles these values as unsigned on the
1612 * wire, there is no way to represent negative values,
1613 * so set them to 0. Without this, they will appear
1614 * to be very large positive values for clients like
1617 if (sf->f_bavail < 0)
1619 if (sf->f_ffree < 0)
1628 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1629 * must handle nfsrv_opencheck() calls after any other access checks.
1632 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1633 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1634 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1635 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p,
1636 struct nfsexstuff *exp, struct vnode **vpp)
1638 struct vnode *vp = NULL;
1640 struct nfsexstuff nes;
1642 if (ndp->ni_vp == NULL)
1643 nd->nd_repstat = nfsrv_opencheck(clientid,
1644 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1645 if (!nd->nd_repstat) {
1646 if (ndp->ni_vp == NULL) {
1647 vrele(ndp->ni_startdir);
1648 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1649 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1650 /* For a pNFS server, create the data file on a DS. */
1651 if (nd->nd_repstat == 0) {
1653 * Create a data file on a DS for a pNFS server.
1654 * This function just returns if not
1655 * running a pNFS DS or the creation fails.
1657 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1661 nfsvno_relpathbuf(ndp);
1662 if (!nd->nd_repstat) {
1663 if (*exclusive_flagp) {
1664 *exclusive_flagp = 0;
1665 NFSVNO_ATTRINIT(nvap);
1666 nvap->na_atime.tv_sec = cverf[0];
1667 nvap->na_atime.tv_nsec = cverf[1];
1668 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1669 &nvap->na_vattr, cred);
1670 if (nd->nd_repstat != 0) {
1673 nd->nd_repstat = NFSERR_NOTSUPP;
1675 NFSSETBIT_ATTRBIT(attrbitp,
1676 NFSATTRBIT_TIMEACCESS);
1678 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1679 aclp, p, attrbitp, exp);
1684 if (ndp->ni_startdir)
1685 vrele(ndp->ni_startdir);
1686 nfsvno_relpathbuf(ndp);
1688 if (create == NFSV4OPEN_CREATE) {
1689 if (ndp->ni_dvp == vp)
1694 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1695 if (ndp->ni_cnd.cn_flags & RDONLY)
1696 NFSVNO_SETEXRDONLY(&nes);
1698 NFSVNO_EXINIT(&nes);
1699 nd->nd_repstat = nfsvno_accchk(vp,
1700 VWRITE, cred, &nes, p,
1701 NFSACCCHK_NOOVERRIDE,
1702 NFSACCCHK_VPISLOCKED, NULL);
1703 nd->nd_repstat = nfsrv_opencheck(clientid,
1704 stateidp, stp, vp, nd, p, nd->nd_repstat);
1705 if (!nd->nd_repstat) {
1706 tempsize = nvap->na_size;
1707 NFSVNO_ATTRINIT(nvap);
1708 nvap->na_size = tempsize;
1709 nd->nd_repstat = VOP_SETATTR(vp,
1710 &nvap->na_vattr, cred);
1712 } else if (vp->v_type == VREG) {
1713 nd->nd_repstat = nfsrv_opencheck(clientid,
1714 stateidp, stp, vp, nd, p, nd->nd_repstat);
1718 if (ndp->ni_cnd.cn_flags & HASBUF)
1719 nfsvno_relpathbuf(ndp);
1720 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1721 vrele(ndp->ni_startdir);
1722 if (ndp->ni_dvp == ndp->ni_vp)
1732 NFSEXITCODE2(0, nd);
1736 * Updates the file rev and sets the mtime and ctime
1737 * to the current clock time, returning the va_filerev and va_Xtime
1739 * Return ESTALE to indicate the vnode is VI_DOOMED.
1742 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1743 struct nfsrv_descript *nd, struct thread *p)
1748 vfs_timestamp(&va.va_mtime);
1749 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1750 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1751 if ((vp->v_iflag & VI_DOOMED) != 0)
1754 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1755 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1760 * Glue routine to nfsv4_fillattr().
1763 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1764 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1765 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1766 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1772 if (nfsrv_devidcnt > 0 &&
1773 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1774 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1775 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1776 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1777 error = nfsrv_pnfsstatfs(sf, mp);
1783 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1784 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1785 mounted_on_fileno, sf);
1787 NFSEXITCODE2(0, nd);
1791 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1793 * nfs readdir service
1794 * - mallocs what it thinks is enough to read
1795 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1796 * - calls VOP_READDIR()
1797 * - loops around building the reply
1798 * if the output generated exceeds count break out of loop
1799 * The NFSM_CLGET macro is used here so that the reply will be packed
1800 * tightly in mbuf clusters.
1801 * - it trims out records with d_fileno == 0
1802 * this doesn't matter for Unix clients, but they might confuse clients
1804 * - it trims out records with d_type == DT_WHT
1805 * these cannot be seen through NFS (unless we extend the protocol)
1806 * The alternate call nfsrvd_readdirplus() does lookups as well.
1807 * PS: The NFS protocol spec. does not clarify what the "count" byte
1808 * argument is a count of.. just name strings and file id's or the
1809 * entire reply rpc or ...
1810 * I tried just file name and id sizes and it confused the Sun client,
1811 * so I am using the full rpc size now. The "paranoia.." comment refers
1812 * to including the status longwords that are not a part of the dir.
1813 * "entry" structures, but are in the rpc.
1816 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1817 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
1822 char *cpos, *cend, *rbuf;
1824 int nlen, error = 0, getret = 1;
1825 int siz, cnt, fullsiz, eofflag, ncookies;
1826 u_int64_t off, toff, verf __unused;
1827 u_long *cookies = NULL, *cookiep;
1832 if (nd->nd_repstat) {
1833 nfsrv_postopattr(nd, getret, &at);
1836 if (nd->nd_flag & ND_NFSV2) {
1837 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1838 off = fxdr_unsigned(u_quad_t, *tl++);
1840 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1841 off = fxdr_hyper(tl);
1843 verf = fxdr_hyper(tl);
1847 cnt = fxdr_unsigned(int, *tl);
1848 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1849 cnt = NFS_SRVMAXDATA(nd);
1850 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1852 if (nd->nd_flag & ND_NFSV3) {
1853 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
1857 * va_filerev is not sufficient as a cookie verifier,
1858 * since it is not supposed to change when entries are
1859 * removed/added unless that offset cookies returned to
1860 * the client are no longer valid.
1862 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1863 nd->nd_repstat = NFSERR_BAD_COOKIE;
1866 if (!nd->nd_repstat && vp->v_type != VDIR)
1867 nd->nd_repstat = NFSERR_NOTDIR;
1868 if (nd->nd_repstat == 0 && cnt == 0) {
1869 if (nd->nd_flag & ND_NFSV2)
1870 /* NFSv2 does not have NFSERR_TOOSMALL */
1871 nd->nd_repstat = EPERM;
1873 nd->nd_repstat = NFSERR_TOOSMALL;
1875 if (!nd->nd_repstat)
1876 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1877 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1878 NFSACCCHK_VPISLOCKED, NULL);
1879 if (nd->nd_repstat) {
1881 if (nd->nd_flag & ND_NFSV3)
1882 nfsrv_postopattr(nd, getret, &at);
1885 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1886 rbuf = malloc(siz, M_TEMP, M_WAITOK);
1890 free(cookies, M_TEMP);
1898 io.uio_offset = (off_t)off;
1900 io.uio_segflg = UIO_SYSSPACE;
1901 io.uio_rw = UIO_READ;
1903 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1905 off = (u_int64_t)io.uio_offset;
1907 siz -= io.uio_resid;
1909 if (!cookies && !nd->nd_repstat)
1910 nd->nd_repstat = NFSERR_PERM;
1911 if (nd->nd_flag & ND_NFSV3) {
1912 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
1913 if (!nd->nd_repstat)
1914 nd->nd_repstat = getret;
1918 * Handles the failed cases. nd->nd_repstat == 0 past here.
1920 if (nd->nd_repstat) {
1924 free(cookies, M_TEMP);
1925 if (nd->nd_flag & ND_NFSV3)
1926 nfsrv_postopattr(nd, getret, &at);
1930 * If nothing read, return eof
1935 if (nd->nd_flag & ND_NFSV2) {
1936 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1938 nfsrv_postopattr(nd, getret, &at);
1939 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1940 txdr_hyper(at.na_filerev, tl);
1943 *tl++ = newnfs_false;
1946 free(cookies, M_TEMP);
1951 * Check for degenerate cases of nothing useful read.
1952 * If so go try again
1956 dp = (struct dirent *)cpos;
1960 * For some reason FreeBSD's ufs_readdir() chooses to back the
1961 * directory offset up to a block boundary, so it is necessary to
1962 * skip over the records that precede the requested offset. This
1963 * requires the assumption that file offset cookies monotonically
1966 while (cpos < cend && ncookies > 0 &&
1967 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
1968 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
1969 cpos += dp->d_reclen;
1970 dp = (struct dirent *)cpos;
1974 if (cpos >= cend || ncookies == 0) {
1982 * dirlen is the size of the reply, including all XDR and must
1983 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
1984 * if the XDR should be included in "count", but to be safe, we do.
1985 * (Include the two booleans at the end of the reply in dirlen now.)
1987 if (nd->nd_flag & ND_NFSV3) {
1988 nfsrv_postopattr(nd, getret, &at);
1989 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1990 txdr_hyper(at.na_filerev, tl);
1991 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
1993 dirlen = 2 * NFSX_UNSIGNED;
1996 /* Loop through the records and build reply */
1997 while (cpos < cend && ncookies > 0) {
1998 nlen = dp->d_namlen;
1999 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2000 nlen <= NFS_MAXNAMLEN) {
2001 if (nd->nd_flag & ND_NFSV3)
2002 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2004 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2011 * Build the directory record xdr from
2014 if (nd->nd_flag & ND_NFSV3) {
2015 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2016 *tl++ = newnfs_true;
2019 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2020 *tl++ = newnfs_true;
2022 *tl = txdr_unsigned(dp->d_fileno);
2023 (void) nfsm_strtom(nd, dp->d_name, nlen);
2024 if (nd->nd_flag & ND_NFSV3) {
2025 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2028 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2029 *tl = txdr_unsigned(*cookiep);
2031 cpos += dp->d_reclen;
2032 dp = (struct dirent *)cpos;
2038 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2039 *tl++ = newnfs_false;
2045 free(cookies, M_TEMP);
2048 NFSEXITCODE2(0, nd);
2052 NFSEXITCODE2(error, nd);
2057 * Readdirplus for V3 and Readdir for V4.
2060 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2061 struct vnode *vp, struct thread *p, struct nfsexstuff *exp)
2066 char *cpos, *cend, *rbuf;
2069 struct nfsvattr nva, at, *nvap = &nva;
2070 struct mbuf *mb0, *mb1;
2071 struct nfsreferral *refp;
2072 int nlen, r, error = 0, getret = 1, usevget = 1;
2073 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2074 caddr_t bpos0, bpos1;
2075 u_int64_t off, toff, verf;
2076 u_long *cookies = NULL, *cookiep;
2077 nfsattrbit_t attrbits, rderrbits, savbits;
2080 struct componentname cn;
2081 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2082 struct mount *mp, *new_mp;
2083 uint64_t mounted_on_fileno;
2085 if (nd->nd_repstat) {
2086 nfsrv_postopattr(nd, getret, &at);
2089 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2090 off = fxdr_hyper(tl);
2093 verf = fxdr_hyper(tl);
2095 siz = fxdr_unsigned(int, *tl++);
2096 cnt = fxdr_unsigned(int, *tl);
2099 * Use the server's maximum data transfer size as the upper bound
2102 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2103 cnt = NFS_SRVMAXDATA(nd);
2106 * siz is a "hint" of how much directory information (name, fileid,
2107 * cookie) should be in the reply. At least one client "hints" 0,
2108 * so I set it to cnt for that case. I also round it up to the
2109 * next multiple of DIRBLKSIZ.
2110 * Since the size of a Readdirplus directory entry reply will always
2111 * be greater than a directory entry returned by VOP_READDIR(), it
2112 * does not make sense to read more than NFS_SRVMAXDATA() via
2117 else if (siz > NFS_SRVMAXDATA(nd))
2118 siz = NFS_SRVMAXDATA(nd);
2119 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2121 if (nd->nd_flag & ND_NFSV4) {
2122 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2125 NFSSET_ATTRBIT(&savbits, &attrbits);
2126 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2127 NFSZERO_ATTRBIT(&rderrbits);
2128 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2130 NFSZERO_ATTRBIT(&attrbits);
2133 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2135 if (!nd->nd_repstat) {
2136 if (off && verf != at.na_filerev) {
2138 * va_filerev is not sufficient as a cookie verifier,
2139 * since it is not supposed to change when entries are
2140 * removed/added unless that offset cookies returned to
2141 * the client are no longer valid.
2143 if (nd->nd_flag & ND_NFSV4) {
2144 nd->nd_repstat = NFSERR_NOTSAME;
2146 nd->nd_repstat = NFSERR_BAD_COOKIE;
2151 if (!nd->nd_repstat && vp->v_type != VDIR)
2152 nd->nd_repstat = NFSERR_NOTDIR;
2153 if (!nd->nd_repstat && cnt == 0)
2154 nd->nd_repstat = NFSERR_TOOSMALL;
2155 if (!nd->nd_repstat)
2156 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2157 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2158 NFSACCCHK_VPISLOCKED, NULL);
2159 if (nd->nd_repstat) {
2161 if (nd->nd_flag & ND_NFSV3)
2162 nfsrv_postopattr(nd, getret, &at);
2165 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2166 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2168 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2172 free(cookies, M_TEMP);
2180 io.uio_offset = (off_t)off;
2182 io.uio_segflg = UIO_SYSSPACE;
2183 io.uio_rw = UIO_READ;
2185 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2187 off = (u_int64_t)io.uio_offset;
2189 siz -= io.uio_resid;
2191 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2193 if (!cookies && !nd->nd_repstat)
2194 nd->nd_repstat = NFSERR_PERM;
2195 if (!nd->nd_repstat)
2196 nd->nd_repstat = getret;
2197 if (nd->nd_repstat) {
2200 free(cookies, M_TEMP);
2202 if (nd->nd_flag & ND_NFSV3)
2203 nfsrv_postopattr(nd, getret, &at);
2207 * If nothing read, return eof
2212 if (nd->nd_flag & ND_NFSV3)
2213 nfsrv_postopattr(nd, getret, &at);
2214 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2215 txdr_hyper(at.na_filerev, tl);
2217 *tl++ = newnfs_false;
2219 free(cookies, M_TEMP);
2225 * Check for degenerate cases of nothing useful read.
2226 * If so go try again
2230 dp = (struct dirent *)cpos;
2234 * For some reason FreeBSD's ufs_readdir() chooses to back the
2235 * directory offset up to a block boundary, so it is necessary to
2236 * skip over the records that precede the requested offset. This
2237 * requires the assumption that file offset cookies monotonically
2240 while (cpos < cend && ncookies > 0 &&
2241 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2242 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2243 ((nd->nd_flag & ND_NFSV4) &&
2244 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2245 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2246 cpos += dp->d_reclen;
2247 dp = (struct dirent *)cpos;
2251 if (cpos >= cend || ncookies == 0) {
2258 * Busy the file system so that the mount point won't go away
2259 * and, as such, VFS_VGET() can be used safely.
2263 NFSVOPUNLOCK(vp, 0);
2264 nd->nd_repstat = vfs_busy(mp, 0);
2266 if (nd->nd_repstat != 0) {
2268 free(cookies, M_TEMP);
2270 if (nd->nd_flag & ND_NFSV3)
2271 nfsrv_postopattr(nd, getret, &at);
2276 * Check to see if entries in this directory can be safely acquired
2277 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2278 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2279 * automount of the snapshot directory that is required will
2281 * This needs to be done here for NFSv4, since NFSv4 never does
2282 * a VFS_VGET() for "." or "..".
2285 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2286 if (r == EOPNOTSUPP) {
2288 cn.cn_nameiop = LOOKUP;
2289 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2290 cn.cn_cred = nd->nd_cred;
2297 * Save this position, in case there is an error before one entry
2301 bpos0 = nd->nd_bpos;
2304 * Fill in the first part of the reply.
2305 * dirlen is the reply length in bytes and cannot exceed cnt.
2306 * (Include the two booleans at the end of the reply in dirlen now,
2307 * so we recognize when we have exceeded cnt.)
2309 if (nd->nd_flag & ND_NFSV3) {
2310 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2311 nfsrv_postopattr(nd, getret, &at);
2313 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2315 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2316 txdr_hyper(at.na_filerev, tl);
2319 * Save this position, in case there is an empty reply needed.
2322 bpos1 = nd->nd_bpos;
2324 /* Loop through the records and build reply */
2326 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2327 nlen = dp->d_namlen;
2328 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2329 nlen <= NFS_MAXNAMLEN &&
2330 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2331 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2332 || (nlen == 1 && dp->d_name[0] != '.'))) {
2334 * Save the current position in the reply, in case
2335 * this entry exceeds cnt.
2338 bpos1 = nd->nd_bpos;
2341 * For readdir_and_lookup get the vnode using
2350 mounted_on_fileno = (uint64_t)dp->d_fileno;
2351 if ((nd->nd_flag & ND_NFSV3) ||
2352 NFSNONZERO_ATTRBIT(&savbits)) {
2353 if (nd->nd_flag & ND_NFSV4)
2354 refp = nfsv4root_getreferral(NULL,
2358 r = VFS_VGET(mp, dp->d_fileno,
2362 if (r == EOPNOTSUPP) {
2365 cn.cn_nameiop = LOOKUP;
2373 cn.cn_nameptr = dp->d_name;
2374 cn.cn_namelen = nlen;
2375 cn.cn_flags = ISLASTCN |
2376 NOFOLLOW | LOCKLEAF;
2378 dp->d_name[0] == '.' &&
2379 dp->d_name[1] == '.')
2382 if (NFSVOPLOCK(vp, LK_SHARED)
2384 nd->nd_repstat = EPERM;
2387 if ((vp->v_vflag & VV_ROOT) != 0
2388 && (cn.cn_flags & ISDOTDOT)
2394 r = VOP_LOOKUP(vp, &nvp,
2403 * For NFSv4, check to see if nvp is
2404 * a mount point and get the mount
2405 * point vnode, as required.
2408 nfsrv_enable_crossmntpt != 0 &&
2409 (nd->nd_flag & ND_NFSV4) != 0 &&
2410 nvp->v_type == VDIR &&
2411 nvp->v_mountedhere != NULL) {
2412 new_mp = nvp->v_mountedhere;
2413 r = vfs_busy(new_mp, 0);
2417 r = VFS_ROOT(new_mp,
2427 * If we failed to look up the entry, then it
2428 * has become invalid, most likely removed.
2435 KASSERT(refp != NULL || nvp != NULL,
2436 ("%s: undetected lookup error", __func__));
2439 ((nd->nd_flag & ND_NFSV3) ||
2440 NFSNONZERO_ATTRBIT(&attrbits))) {
2441 r = nfsvno_getfh(nvp, &nfh, p);
2443 r = nfsvno_getattr(nvp, nvap, nd, p,
2445 if (r == 0 && is_zfs == 1 &&
2446 nfsrv_enable_crossmntpt != 0 &&
2447 (nd->nd_flag & ND_NFSV4) != 0 &&
2448 nvp->v_type == VDIR &&
2449 vp->v_mount != nvp->v_mount) {
2451 * For a ZFS snapshot, there is a
2452 * pseudo mount that does not set
2453 * v_mountedhere, so it needs to
2454 * be detected via a different
2459 new_mp = nvp->v_mount;
2464 * If we failed to get attributes of the entry,
2465 * then just skip it for NFSv3 (the traditional
2466 * behavior in the old NFS server).
2467 * For NFSv4 the behavior is controlled by
2468 * RDATTRERROR: we either ignore the error or
2470 * Note that RDATTRERROR is never set for NFSv3.
2473 if (!NFSISSET_ATTRBIT(&attrbits,
2474 NFSATTRBIT_RDATTRERROR)) {
2476 if (needs_unbusy != 0)
2478 if ((nd->nd_flag & ND_NFSV3))
2487 * Build the directory record xdr
2489 if (nd->nd_flag & ND_NFSV3) {
2490 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2491 *tl++ = newnfs_true;
2493 *tl = txdr_unsigned(dp->d_fileno);
2494 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2495 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2497 *tl = txdr_unsigned(*cookiep);
2498 nfsrv_postopattr(nd, 0, nvap);
2499 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2500 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2504 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2505 *tl++ = newnfs_true;
2507 *tl = txdr_unsigned(*cookiep);
2508 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2510 supports_nfsv4acls =
2511 nfs_supportsnfsv4acls(nvp);
2512 NFSVOPUNLOCK(nvp, 0);
2514 supports_nfsv4acls = 0;
2516 dirlen += nfsrv_putreferralattr(nd,
2519 if (nd->nd_repstat) {
2522 if (needs_unbusy != 0)
2527 dirlen += nfsvno_fillattr(nd, new_mp,
2528 nvp, nvap, &nfh, r, &rderrbits,
2529 nd->nd_cred, p, isdgram, 0,
2530 supports_nfsv4acls, at_root,
2533 dirlen += nfsvno_fillattr(nd, new_mp,
2534 nvp, nvap, &nfh, r, &attrbits,
2535 nd->nd_cred, p, isdgram, 0,
2536 supports_nfsv4acls, at_root,
2541 dirlen += (3 * NFSX_UNSIGNED);
2543 if (needs_unbusy != 0)
2549 cpos += dp->d_reclen;
2550 dp = (struct dirent *)cpos;
2558 * If dirlen > cnt, we must strip off the last entry. If that
2559 * results in an empty reply, report NFSERR_TOOSMALL.
2561 if (dirlen > cnt || nd->nd_repstat) {
2562 if (!nd->nd_repstat && entrycnt == 0)
2563 nd->nd_repstat = NFSERR_TOOSMALL;
2564 if (nd->nd_repstat) {
2565 newnfs_trimtrailing(nd, mb0, bpos0);
2566 if (nd->nd_flag & ND_NFSV3)
2567 nfsrv_postopattr(nd, getret, &at);
2569 newnfs_trimtrailing(nd, mb1, bpos1);
2571 } else if (cpos < cend)
2573 if (!nd->nd_repstat) {
2574 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2575 *tl++ = newnfs_false;
2581 free(cookies, M_TEMP);
2585 NFSEXITCODE2(0, nd);
2589 NFSEXITCODE2(error, nd);
2594 * Get the settable attributes out of the mbuf list.
2595 * (Return 0 or EBADRPC)
2598 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2599 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2602 struct nfsv2_sattr *sp;
2603 int error = 0, toclient = 0;
2605 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2607 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2609 * Some old clients didn't fill in the high order 16bits.
2610 * --> check the low order 2 bytes for 0xffff
2612 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2613 nvap->na_mode = nfstov_mode(sp->sa_mode);
2614 if (sp->sa_uid != newnfs_xdrneg1)
2615 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2616 if (sp->sa_gid != newnfs_xdrneg1)
2617 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2618 if (sp->sa_size != newnfs_xdrneg1)
2619 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2620 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2622 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2624 nvap->na_atime.tv_sec =
2625 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2626 nvap->na_atime.tv_nsec = 0;
2629 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2630 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2633 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2634 if (*tl == newnfs_true) {
2635 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2636 nvap->na_mode = nfstov_mode(*tl);
2638 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2639 if (*tl == newnfs_true) {
2640 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2641 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2643 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2644 if (*tl == newnfs_true) {
2645 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2646 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2648 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2649 if (*tl == newnfs_true) {
2650 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2651 nvap->na_size = fxdr_hyper(tl);
2653 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2654 switch (fxdr_unsigned(int, *tl)) {
2655 case NFSV3SATTRTIME_TOCLIENT:
2656 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2657 fxdr_nfsv3time(tl, &nvap->na_atime);
2660 case NFSV3SATTRTIME_TOSERVER:
2661 vfs_timestamp(&nvap->na_atime);
2662 nvap->na_vaflags |= VA_UTIMES_NULL;
2665 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2666 switch (fxdr_unsigned(int, *tl)) {
2667 case NFSV3SATTRTIME_TOCLIENT:
2668 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2669 fxdr_nfsv3time(tl, &nvap->na_mtime);
2670 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2672 case NFSV3SATTRTIME_TOSERVER:
2673 vfs_timestamp(&nvap->na_mtime);
2675 nvap->na_vaflags |= VA_UTIMES_NULL;
2680 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2683 NFSEXITCODE2(error, nd);
2688 * Handle the setable attributes for V4.
2689 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2692 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2693 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2698 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2699 int moderet, toclient = 0;
2700 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2703 u_short mode, mask; /* Same type as va_mode. */
2706 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2709 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2710 attrsize = fxdr_unsigned(int, *tl);
2713 * Loop around getting the setable attributes. If an unsupported
2714 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2717 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2718 bitpos = NFSATTRBIT_MAX;
2723 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2724 if (attrsum > attrsize) {
2725 error = NFSERR_BADXDR;
2728 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2730 case NFSATTRBIT_SIZE:
2731 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2732 if (vp != NULL && vp->v_type != VREG) {
2733 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2737 nvap->na_size = fxdr_hyper(tl);
2738 attrsum += NFSX_HYPER;
2740 case NFSATTRBIT_ACL:
2741 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2745 if (aceerr && !nd->nd_repstat)
2746 nd->nd_repstat = aceerr;
2749 case NFSATTRBIT_ARCHIVE:
2750 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2751 if (!nd->nd_repstat)
2752 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2753 attrsum += NFSX_UNSIGNED;
2755 case NFSATTRBIT_HIDDEN:
2756 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2757 if (!nd->nd_repstat)
2758 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2759 attrsum += NFSX_UNSIGNED;
2761 case NFSATTRBIT_MIMETYPE:
2762 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2763 i = fxdr_unsigned(int, *tl);
2764 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2767 if (!nd->nd_repstat)
2768 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2769 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2771 case NFSATTRBIT_MODE:
2772 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
2773 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2774 nvap->na_mode = nfstov_mode(*tl);
2775 attrsum += NFSX_UNSIGNED;
2777 case NFSATTRBIT_OWNER:
2778 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2779 j = fxdr_unsigned(int, *tl);
2781 error = NFSERR_BADXDR;
2784 if (j > NFSV4_SMALLSTR)
2785 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2788 error = nfsrv_mtostr(nd, cp, j);
2790 if (j > NFSV4_SMALLSTR)
2791 free(cp, M_NFSSTRING);
2794 if (!nd->nd_repstat) {
2795 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid,
2797 if (!nd->nd_repstat)
2800 if (j > NFSV4_SMALLSTR)
2801 free(cp, M_NFSSTRING);
2802 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2804 case NFSATTRBIT_OWNERGROUP:
2805 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2806 j = fxdr_unsigned(int, *tl);
2808 error = NFSERR_BADXDR;
2811 if (j > NFSV4_SMALLSTR)
2812 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2815 error = nfsrv_mtostr(nd, cp, j);
2817 if (j > NFSV4_SMALLSTR)
2818 free(cp, M_NFSSTRING);
2821 if (!nd->nd_repstat) {
2822 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid,
2824 if (!nd->nd_repstat)
2827 if (j > NFSV4_SMALLSTR)
2828 free(cp, M_NFSSTRING);
2829 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2831 case NFSATTRBIT_SYSTEM:
2832 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2833 if (!nd->nd_repstat)
2834 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2835 attrsum += NFSX_UNSIGNED;
2837 case NFSATTRBIT_TIMEACCESSSET:
2838 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2839 attrsum += NFSX_UNSIGNED;
2840 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2841 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2842 fxdr_nfsv4time(tl, &nvap->na_atime);
2844 attrsum += NFSX_V4TIME;
2846 vfs_timestamp(&nvap->na_atime);
2847 nvap->na_vaflags |= VA_UTIMES_NULL;
2850 case NFSATTRBIT_TIMEBACKUP:
2851 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2852 if (!nd->nd_repstat)
2853 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2854 attrsum += NFSX_V4TIME;
2856 case NFSATTRBIT_TIMECREATE:
2857 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2858 if (!nd->nd_repstat)
2859 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2860 attrsum += NFSX_V4TIME;
2862 case NFSATTRBIT_TIMEMODIFYSET:
2863 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2864 attrsum += NFSX_UNSIGNED;
2865 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2866 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2867 fxdr_nfsv4time(tl, &nvap->na_mtime);
2868 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2869 attrsum += NFSX_V4TIME;
2871 vfs_timestamp(&nvap->na_mtime);
2873 nvap->na_vaflags |= VA_UTIMES_NULL;
2876 case NFSATTRBIT_MODESETMASKED:
2877 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2878 mode = fxdr_unsigned(u_short, *tl++);
2879 mask = fxdr_unsigned(u_short, *tl);
2881 * vp == NULL implies an Open/Create operation.
2882 * This attribute can only be used for Setattr and
2883 * only for NFSv4.1 or higher.
2884 * If moderet != 0, a mode attribute has also been
2885 * specified and this attribute cannot be done in the
2886 * same Setattr operation.
2888 if ((nd->nd_flag & ND_NFSV41) == 0)
2889 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2890 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
2892 nd->nd_repstat = NFSERR_INVAL;
2893 else if (moderet == 0)
2894 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
2896 nvap->na_mode = (mode & mask) |
2897 (va.va_mode & ~mask);
2899 nd->nd_repstat = moderet;
2900 attrsum += 2 * NFSX_UNSIGNED;
2903 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2905 * set bitpos so we drop out of the loop.
2907 bitpos = NFSATTRBIT_MAX;
2913 * some clients pad the attrlist, so we need to skip over the
2916 if (attrsum > attrsize) {
2917 error = NFSERR_BADXDR;
2919 attrsize = NFSM_RNDUP(attrsize);
2920 if (attrsum < attrsize)
2921 error = nfsm_advance(nd, attrsize - attrsum, -1);
2924 NFSEXITCODE2(error, nd);
2929 * Check/setup export credentials.
2932 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2933 struct ucred *credanon)
2938 * Check/setup credentials.
2940 if (nd->nd_flag & ND_GSS)
2941 exp->nes_exflag &= ~MNT_EXPORTANON;
2944 * Check to see if the operation is allowed for this security flavor.
2945 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
2946 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
2947 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
2949 if (nfsvno_testexp(nd, exp) &&
2950 nd->nd_procnum != NFSV4OP_SECINFO &&
2951 nd->nd_procnum != NFSPROC_FSINFO) {
2952 if (nd->nd_flag & ND_NFSV4)
2953 error = NFSERR_WRONGSEC;
2955 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
2960 * Check to see if the file system is exported V4 only.
2962 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
2963 error = NFSERR_PROGNOTV4;
2968 * Now, map the user credentials.
2969 * (Note that ND_AUTHNONE will only be set for an NFSv3
2970 * Fsinfo RPC. If set for anything else, this code might need
2973 if (NFSVNO_EXPORTED(exp)) {
2974 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
2975 NFSVNO_EXPORTANON(exp) ||
2976 (nd->nd_flag & ND_AUTHNONE) != 0) {
2977 nd->nd_cred->cr_uid = credanon->cr_uid;
2978 nd->nd_cred->cr_gid = credanon->cr_gid;
2979 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
2980 credanon->cr_groups);
2981 } else if ((nd->nd_flag & ND_GSS) == 0) {
2983 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
2984 * if there is a replacement credential with a group
2985 * list set up by "nfsuserd -manage-gids".
2986 * If there is no replacement, nfsrv_getgrpscred()
2987 * simply returns its argument.
2989 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
2994 NFSEXITCODE2(error, nd);
3002 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3003 struct ucred **credp)
3005 int i, error, *secflavors;
3007 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3008 &exp->nes_numsecflavor, &secflavors);
3010 if (nfs_rootfhset) {
3011 exp->nes_exflag = 0;
3012 exp->nes_numsecflavor = 0;
3016 /* Copy the security flavors. */
3017 for (i = 0; i < exp->nes_numsecflavor; i++)
3018 exp->nes_secflavors[i] = secflavors[i];
3025 * Get a vnode for a file handle and export stuff.
3028 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3029 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3030 struct ucred **credp)
3032 int i, error, *secflavors;
3035 exp->nes_numsecflavor = 0;
3036 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3038 /* Make sure the server replies ESTALE to the client. */
3040 if (nam && !error) {
3041 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3042 &exp->nes_numsecflavor, &secflavors);
3044 if (nfs_rootfhset) {
3045 exp->nes_exflag = 0;
3046 exp->nes_numsecflavor = 0;
3052 /* Copy the security flavors. */
3053 for (i = 0; i < exp->nes_numsecflavor; i++)
3054 exp->nes_secflavors[i] = secflavors[i];
3062 * nfsd_fhtovp() - convert a fh to a vnode ptr
3063 * - look up fsid in mount list (if not found ret error)
3064 * - get vp and export rights by calling nfsvno_fhtovp()
3065 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3067 * - if mpp != NULL, return the mount point so that it can
3068 * be used for vn_finished_write() by the caller
3071 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3072 struct vnode **vpp, struct nfsexstuff *exp,
3073 struct mount **mpp, int startwrite, struct thread *p)
3076 struct ucred *credanon;
3079 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3081 * Check for the special case of the nfsv4root_fh.
3083 mp = vfs_busyfs(&fhp->fh_fsid);
3088 nd->nd_repstat = ESTALE;
3093 vn_start_write(NULL, mpp, V_WAIT);
3094 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3095 lktype = LK_EXCLUSIVE;
3097 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3102 * For NFSv4 without a pseudo root fs, unexported file handles
3103 * can be returned, so that Lookup works everywhere.
3105 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3106 !(nd->nd_flag & ND_NFSV4)) {
3108 nd->nd_repstat = EACCES;
3112 * Personally, I've never seen any point in requiring a
3113 * reserved port#, since only in the rare case where the
3114 * clients are all boxes with secure system privileges,
3115 * does it provide any enhanced security, but... some people
3116 * believe it to be useful and keep putting this code back in.
3117 * (There is also some "security checker" out there that
3118 * complains if the nfs server doesn't enforce this.)
3119 * However, note the following:
3120 * RFC3530 (NFSv4) specifies that a reserved port# not be
3122 * RFC2623 recommends that, if a reserved port# is checked for,
3123 * that there be a way to turn that off--> ifdef'd.
3125 #ifdef NFS_REQRSVPORT
3126 if (!nd->nd_repstat) {
3127 struct sockaddr_in *saddr;
3128 struct sockaddr_in6 *saddr6;
3130 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3131 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3132 if (!(nd->nd_flag & ND_NFSV4) &&
3133 ((saddr->sin_family == AF_INET &&
3134 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3135 (saddr6->sin6_family == AF_INET6 &&
3136 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3138 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3141 #endif /* NFS_REQRSVPORT */
3144 * Check/setup credentials.
3146 if (!nd->nd_repstat) {
3147 nd->nd_saveduid = nd->nd_cred->cr_uid;
3148 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
3152 if (credanon != NULL)
3154 if (nd->nd_repstat) {
3156 vn_finished_write(mp);
3163 NFSEXITCODE2(0, nd);
3170 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3172 struct filedesc *fdp;
3176 fdp = p->td_proc->p_fd;
3177 if (fd < 0 || fd >= fdp->fd_nfiles ||
3178 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3190 * Called from nfssvc() to update the exports list. Just call
3191 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3192 * in the mount list.
3195 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3197 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3199 struct nameidata nd;
3202 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3203 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3205 else if (error == 0) {
3206 if (nfsexargp->fspec == NULL) {
3211 * If fspec != NULL, this is the v4root path.
3213 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3214 nfsexargp->fspec, p);
3215 if ((error = namei(&nd)) != 0)
3217 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3220 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3221 NFSBCOPY((caddr_t)&fh,
3222 nfs_rootfh.nfsrvfh_data,
3223 sizeof (fhandle_t));
3234 * This function needs to test to see if the system is near its limit
3235 * for memory allocation via malloc() or mget() and return True iff
3236 * either of these resources are near their limit.
3237 * XXX (For now, this is just a stub.)
3239 int nfsrv_testmalloclimit = 0;
3241 nfsrv_mallocmget_limit(void)
3243 static int printmesg = 0;
3244 static int testval = 1;
3246 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3247 if ((printmesg++ % 100) == 0)
3248 printf("nfsd: malloc/mget near limit\n");
3255 * BSD specific initialization of a mount point.
3260 static int inited = 0;
3265 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3266 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3267 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist);
3268 nfsv4root_mnt.mnt_export = NULL;
3269 TAILQ_INIT(&nfsv4root_opt);
3270 TAILQ_INIT(&nfsv4root_newopt);
3271 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3272 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3273 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3274 nfsv4root_mnt.mnt_activevnodelistsize = 0;
3278 * Get a vnode for a file handle, without checking exports, etc.
3281 nfsvno_getvp(fhandle_t *fhp)
3287 mp = vfs_busyfs(&fhp->fh_fsid);
3290 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3298 * Do a local VOP_ADVLOCK().
3301 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3302 u_int64_t end, struct thread *td)
3308 if (nfsrv_dolocallocks == 0)
3310 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3312 fl.l_whence = SEEK_SET;
3314 fl.l_start = (off_t)first;
3315 if (end == NFS64BITSSET) {
3319 fl.l_len = (off_t)tlen;
3322 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3323 * values for all calls, so that all locks will be held by the
3324 * nfsd server. (The nfsd server handles conflicts between the
3326 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3327 * bytes, so it can't be put in l_sysid.
3329 if (nfsv4_sysid == 0)
3330 nfsv4_sysid = nlm_acquire_next_sysid();
3331 fl.l_pid = (pid_t)0;
3332 fl.l_sysid = (int)nfsv4_sysid;
3334 if (ftype == F_UNLCK)
3335 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3336 (F_POSIX | F_REMOTE));
3338 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3339 (F_POSIX | F_REMOTE));
3347 * Check the nfsv4 root exports.
3350 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3352 struct ucred *credanon;
3353 int exflags, error = 0, numsecflavor, *secflavors, i;
3355 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3356 &credanon, &numsecflavor, &secflavors);
3358 error = NFSERR_PROGUNAVAIL;
3361 if (credanon != NULL)
3363 for (i = 0; i < numsecflavor; i++) {
3364 if (secflavors[i] == AUTH_SYS)
3365 nd->nd_flag |= ND_EXAUTHSYS;
3366 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3367 nd->nd_flag |= ND_EXGSS;
3368 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3369 nd->nd_flag |= ND_EXGSSINTEGRITY;
3370 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3371 nd->nd_flag |= ND_EXGSSPRIVACY;
3380 * Nfs server pseudo system call for the nfsd's
3386 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3389 struct nfsd_addsock_args sockarg;
3390 struct nfsd_nfsd_args nfsdarg;
3391 struct nfsd_nfsd_oargs onfsdarg;
3392 struct nfsd_pnfsd_args pnfsdarg;
3393 struct vnode *vp, *nvp, *curdvp;
3394 struct pnfsdsfile *pf;
3395 struct nfsdevice *ds, *fds;
3396 cap_rights_t rights;
3397 int buflen, error, ret;
3398 char *buf, *cp, *cp2, *cp3;
3399 char fname[PNFS_FILENAME_LEN + 1];
3401 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3402 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3406 * Since we don't know what rights might be required,
3407 * pretend that we need them all. It is better to be too
3408 * careful than too reckless.
3410 error = fget(td, sockarg.sock,
3411 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3414 if (fp->f_type != DTYPE_SOCKET) {
3419 error = nfsrvd_addsock(fp);
3421 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3422 if (uap->argp == NULL) {
3426 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3427 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3429 nfsdarg.principal = onfsdarg.principal;
3430 nfsdarg.minthreads = onfsdarg.minthreads;
3431 nfsdarg.maxthreads = onfsdarg.maxthreads;
3432 nfsdarg.version = 1;
3433 nfsdarg.addr = NULL;
3434 nfsdarg.addrlen = 0;
3435 nfsdarg.dnshost = NULL;
3436 nfsdarg.dnshostlen = 0;
3437 nfsdarg.dspath = NULL;
3438 nfsdarg.dspathlen = 0;
3439 nfsdarg.mdspath = NULL;
3440 nfsdarg.mdspathlen = 0;
3441 nfsdarg.mirrorcnt = 1;
3444 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3447 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3448 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3449 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3450 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3451 nfsdarg.mirrorcnt >= 1 &&
3452 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3453 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3454 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3455 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3456 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3457 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3458 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3459 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3460 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3465 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3467 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3468 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3470 free(nfsdarg.addr, M_TEMP);
3474 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3475 nfsdarg.dnshost = cp;
3476 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3477 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3479 free(nfsdarg.addr, M_TEMP);
3480 free(nfsdarg.dnshost, M_TEMP);
3484 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3485 nfsdarg.dspath = cp;
3486 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3487 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3489 free(nfsdarg.addr, M_TEMP);
3490 free(nfsdarg.dnshost, M_TEMP);
3491 free(nfsdarg.dspath, M_TEMP);
3495 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3496 nfsdarg.mdspath = cp;
3498 nfsdarg.addr = NULL;
3499 nfsdarg.addrlen = 0;
3500 nfsdarg.dnshost = NULL;
3501 nfsdarg.dnshostlen = 0;
3502 nfsdarg.dspath = NULL;
3503 nfsdarg.dspathlen = 0;
3504 nfsdarg.mdspath = NULL;
3505 nfsdarg.mdspathlen = 0;
3506 nfsdarg.mirrorcnt = 1;
3508 error = nfsrvd_nfsd(td, &nfsdarg);
3509 free(nfsdarg.addr, M_TEMP);
3510 free(nfsdarg.dnshost, M_TEMP);
3511 free(nfsdarg.dspath, M_TEMP);
3512 free(nfsdarg.mdspath, M_TEMP);
3513 } else if (uap->flag & NFSSVC_PNFSDS) {
3514 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3515 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3516 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3517 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3518 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3521 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3523 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3524 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3525 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3526 buf = malloc(buflen, M_TEMP, M_WAITOK);
3527 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3529 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3530 if (error == 0 && pnfsdarg.dspath != NULL) {
3531 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3532 error = copyinstr(pnfsdarg.dspath, cp2,
3533 PATH_MAX + 1, NULL);
3534 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3538 if (error == 0 && pnfsdarg.curdspath != NULL) {
3539 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3540 error = copyinstr(pnfsdarg.curdspath, cp3,
3541 PATH_MAX + 1, NULL);
3542 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3549 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3550 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3552 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3554 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3555 printf("copymr: dsdir out of range\n");
3558 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3559 error = nfsrv_copymr(vp, nvp,
3560 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3561 (struct pnfsdsfile *)buf,
3562 buflen / sizeof(*pf), td->td_ucred, td);
3565 if (fds != NULL && error == 0) {
3566 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3567 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3569 nfsrv_dsremove(curdvp, fname,
3571 NFSVOPUNLOCK(curdvp, 0);
3574 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3582 error = nfssvc_srvcall(td, uap, td->td_ucred);
3591 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3593 struct nfsex_args export;
3594 struct file *fp = NULL;
3596 struct nfsd_clid adminrevoke;
3597 struct nfsd_dumplist dumplist;
3598 struct nfsd_dumpclients *dumpclients;
3599 struct nfsd_dumplocklist dumplocklist;
3600 struct nfsd_dumplocks *dumplocks;
3601 struct nameidata nd;
3603 int error = EINVAL, igotlock;
3605 static int suspend_nfsd = 0;
3607 if (uap->flag & NFSSVC_PUBLICFH) {
3608 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3609 sizeof (fhandle_t));
3610 error = copyin(uap->argp,
3611 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3614 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3615 error = copyin(uap->argp,(caddr_t)&export,
3616 sizeof (struct nfsex_args));
3618 error = nfsrv_v4rootexport(&export, cred, p);
3619 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3622 } else if (uap->flag & NFSSVC_STABLERESTART) {
3623 error = copyin(uap->argp, (caddr_t)&stablefd,
3626 error = fp_getfvp(p, stablefd, &fp, &vp);
3627 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3629 if (!error && newnfs_numnfsd != 0)
3632 nfsrv_stablefirst.nsf_fp = fp;
3633 nfsrv_setupstable(p);
3635 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3636 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3637 sizeof (struct nfsd_clid));
3639 error = nfsrv_adminrevoke(&adminrevoke, p);
3640 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3641 error = copyin(uap->argp, (caddr_t)&dumplist,
3642 sizeof (struct nfsd_dumplist));
3643 if (!error && (dumplist.ndl_size < 1 ||
3644 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3647 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3648 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3649 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3650 error = copyout(dumpclients,
3651 CAST_USER_ADDR_T(dumplist.ndl_list), len);
3652 free(dumpclients, M_TEMP);
3654 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3655 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3656 sizeof (struct nfsd_dumplocklist));
3657 if (!error && (dumplocklist.ndllck_size < 1 ||
3658 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3661 error = nfsrv_lookupfilename(&nd,
3662 dumplocklist.ndllck_fname, p);
3664 len = sizeof (struct nfsd_dumplocks) *
3665 dumplocklist.ndllck_size;
3666 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3667 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3668 dumplocklist.ndllck_size, p);
3670 error = copyout(dumplocks,
3671 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len);
3672 free(dumplocks, M_TEMP);
3674 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3677 nfsd_master_pid = procp->p_pid;
3678 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3679 nfsd_master_start = procp->p_stats->p_start;
3680 nfsd_master_proc = procp;
3682 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3683 NFSLOCKV4ROOTMUTEX();
3684 if (suspend_nfsd == 0) {
3685 /* Lock out all nfsd threads */
3687 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3688 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3689 } while (igotlock == 0 && suspend_nfsd == 0);
3692 NFSUNLOCKV4ROOTMUTEX();
3694 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3695 NFSLOCKV4ROOTMUTEX();
3696 if (suspend_nfsd != 0) {
3697 nfsv4_unlock(&nfsd_suspend_lock, 0);
3700 NFSUNLOCKV4ROOTMUTEX();
3710 * Returns 0 if ok, 1 otherwise.
3713 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3718 * This seems odd, but allow the case where the security flavor
3719 * list is empty. This happens when NFSv4 is traversing non-exported
3720 * file systems. Exported file systems should always have a non-empty
3721 * security flavor list.
3723 if (exp->nes_numsecflavor == 0)
3726 for (i = 0; i < exp->nes_numsecflavor; i++) {
3728 * The tests for privacy and integrity must be first,
3729 * since ND_GSS is set for everything but AUTH_SYS.
3731 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3732 (nd->nd_flag & ND_GSSPRIVACY))
3734 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3735 (nd->nd_flag & ND_GSSINTEGRITY))
3737 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3738 (nd->nd_flag & ND_GSS))
3740 if (exp->nes_secflavors[i] == AUTH_SYS &&
3741 (nd->nd_flag & ND_GSS) == 0)
3748 * Calculate a hash value for the fid in a file handle.
3751 nfsrv_hashfh(fhandle_t *fhp)
3755 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3760 * Calculate a hash value for the sessionid.
3763 nfsrv_hashsessionid(uint8_t *sessionid)
3767 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3772 * Signal the userland master nfsd to backup the stable restart file.
3775 nfsrv_backupstable(void)
3779 if (nfsd_master_proc != NULL) {
3780 procp = pfind(nfsd_master_pid);
3781 /* Try to make sure it is the correct process. */
3782 if (procp == nfsd_master_proc &&
3783 procp->p_stats->p_start.tv_sec ==
3784 nfsd_master_start.tv_sec &&
3785 procp->p_stats->p_start.tv_usec ==
3786 nfsd_master_start.tv_usec &&
3787 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3788 kern_psignal(procp, SIGUSR2);
3790 nfsd_master_proc = NULL;
3798 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
3799 * The arguments are in a structure, so that they can be passed through
3800 * taskqueue for a kernel process to execute this function.
3802 struct nfsrvdscreate {
3806 struct ucred *tcred;
3809 struct pnfsdsfile *pf;
3813 struct vattr createva;
3817 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
3818 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
3819 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
3822 struct nameidata named;
3827 struct nfsmount *nmp;
3830 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
3831 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
3832 nfsvno_setpathbuf(&named, &bufp, &hashp);
3833 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
3834 named.ni_cnd.cn_thread = p;
3835 named.ni_cnd.cn_nameptr = bufp;
3836 if (fnamep != NULL) {
3837 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
3838 named.ni_cnd.cn_namelen = strlen(bufp);
3840 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
3841 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
3843 /* Create the date file in the DS mount. */
3844 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
3846 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
3847 NFSVOPUNLOCK(dvp, 0);
3849 /* Set the ownership of the file. */
3850 error = VOP_SETATTR(nvp, nvap, tcred);
3851 NFSD_DEBUG(4, "nfsrv_dscreate:"
3852 " setattr-uid=%d\n", error);
3857 printf("pNFS: pnfscreate failed=%d\n", error);
3859 printf("pNFS: pnfscreate vnlock=%d\n", error);
3862 nmp = VFSTONFS(nvp->v_mount);
3863 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
3864 != 0 || nmp->nm_nam->sa_len > sizeof(
3865 struct sockaddr_in6) ||
3866 np->n_fhp->nfh_len != NFSX_MYFH) {
3867 printf("Bad DS file: fstype=%s salen=%d"
3869 nvp->v_mount->mnt_vfc->vfc_name,
3870 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
3874 /* Set extattrs for the DS on the MDS file. */
3877 error = VOP_GETATTR(nvp, &va, tcred);
3879 dsa->dsa_filerev = va.va_filerev;
3880 dsa->dsa_size = va.va_size;
3881 dsa->dsa_atime = va.va_atime;
3882 dsa->dsa_mtime = va.va_mtime;
3886 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
3888 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
3889 nmp->nm_nam->sa_len);
3890 NFSBCOPY(named.ni_cnd.cn_nameptr,
3892 sizeof(pf->dsf_filename));
3895 printf("pNFS: pnfscreate can't get DS"
3896 " attr=%d\n", error);
3897 if (nvpp != NULL && error == 0)
3902 nfsvno_relpathbuf(&named);
3907 * Start up the thread that will execute nfsrv_dscreate().
3910 start_dscreate(void *arg, int pending)
3912 struct nfsrvdscreate *dsc;
3914 dsc = (struct nfsrvdscreate *)arg;
3915 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
3916 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
3918 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
3922 * Create a pNFS data file on the Data Server(s).
3925 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
3928 struct nfsrvdscreate *dsc, *tdsc;
3929 struct nfsdevice *ds, *tds, *fds;
3931 struct pnfsdsfile *pf, *tpf;
3932 struct pnfsdsattr dsattr;
3934 struct vnode *dvp[NFSDEV_MAXMIRRORS];
3935 struct nfsmount *nmp;
3940 struct ucred *tcred;
3941 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
3944 /* Get a DS server directory in a round-robin order. */
3950 * Search for the first entry that handles this MDS fs, but use the
3951 * first entry for all MDS fs's otherwise.
3953 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
3954 if (tds->nfsdev_nmp != NULL) {
3955 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
3957 else if (tds->nfsdev_mdsisset != 0 &&
3958 mp->mnt_stat.f_fsid.val[0] ==
3959 tds->nfsdev_mdsfsid.val[0] &&
3960 mp->mnt_stat.f_fsid.val[1] ==
3961 tds->nfsdev_mdsfsid.val[1]) {
3969 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
3972 i = dsdir[0] = ds->nfsdev_nextdir;
3973 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
3974 dvp[0] = ds->nfsdev_dsdir[i];
3975 tds = TAILQ_NEXT(ds, nfsdev_list);
3976 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
3977 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
3978 if (tds->nfsdev_nmp != NULL &&
3979 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
3980 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
3981 mp->mnt_stat.f_fsid.val[0] ==
3982 tds->nfsdev_mdsfsid.val[0] &&
3983 mp->mnt_stat.f_fsid.val[1] ==
3984 tds->nfsdev_mdsfsid.val[1]))) {
3985 dsdir[mirrorcnt] = i;
3986 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
3988 if (mirrorcnt >= nfsrv_maxpnfsmirror)
3993 /* Put at end of list to implement round-robin usage. */
3994 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
3995 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
3999 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4001 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4004 error = nfsvno_getfh(vp, &fh, p);
4006 error = VOP_GETATTR(vp, &va, cred);
4008 /* Set the attributes for "vp" to Setattr the DS vp. */
4011 vamode = va.va_mode;
4015 va.va_mode = vamode;
4018 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4020 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4022 /* Make data file name based on FH. */
4023 tcred = newnfs_getcred();
4026 * Create the file on each DS mirror, using kernel process(es) for the
4027 * additional mirrors.
4030 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4031 tpf->dsf_dir = dsdir[i];
4032 tdsc->tcred = tcred;
4035 tdsc->createva = *vap;
4036 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4043 if (nfs_pnfsiothreads != 0) {
4044 ret = nfs_pnfsio(start_dscreate, tdsc);
4045 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4048 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4049 NULL, tcred, p, NULL);
4051 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4053 if (failpos == -1 && nfsds_failerr(ret))
4061 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4062 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4063 &dsattr, NULL, tcred, p, NULL);
4064 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4065 failpos = mirrorcnt - 1;
4069 timo = hz / 50; /* Wait for 20msec. */
4072 /* Wait for kernel task(s) to complete. */
4073 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4074 while (tdsc->inprog != 0 && tdsc->done == 0)
4075 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4076 if (tdsc->err != 0) {
4077 if (failpos == -1 && nfsds_failerr(tdsc->err))
4079 else if (error == 0)
4085 * If failpos has been set, that mirror has failed, so it needs
4089 nmp = VFSTONFS(dvp[failpos]->v_mount);
4091 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4092 NFSMNTP_CANCELRPCS)) == 0) {
4093 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4095 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4096 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4099 nfsrv_killrpcs(nmp);
4101 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4109 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4111 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4112 mirrorcnt, nfsrv_maxpnfsmirror);
4114 * For all mirrors that couldn't be created, fill in the
4115 * *pf structure, but with an IP address == 0.0.0.0.
4117 tpf = pf + mirrorcnt;
4118 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4120 tpf->dsf_sin.sin_family = AF_INET;
4121 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4122 tpf->dsf_sin.sin_addr.s_addr = 0;
4123 tpf->dsf_sin.sin_port = 0;
4126 error = vn_extattr_set(vp, IO_NODELOCKED,
4127 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4128 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4130 error = vn_extattr_set(vp, IO_NODELOCKED,
4131 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4132 sizeof(dsattr), (char *)&dsattr, p);
4134 printf("pNFS: pnfscreate setextattr=%d\n",
4137 printf("pNFS: pnfscreate=%d\n", error);
4143 * Get the information needed to remove the pNFS Data Server file from the
4144 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4145 * DS directory vnode. The caller must unlock *ddvp when done with it.
4148 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4149 int *mirrorcntp, char *fname, fhandle_t *fhp)
4152 struct ucred *tcred;
4157 /* If not an exported regular file or not a pNFS server, just return. */
4158 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4159 nfsrv_devidcnt == 0)
4162 /* Check to see if this is the last hard link. */
4163 tcred = newnfs_getcred();
4164 error = VOP_GETATTR(vp, &va, tcred);
4167 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4170 if (va.va_nlink > 1)
4173 error = nfsvno_getfh(vp, fhp, p);
4175 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4180 buf = malloc(buflen, M_TEMP, M_WAITOK);
4181 /* Get the directory vnode for the DS mount and the file handle. */
4182 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4183 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4186 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4190 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4191 * The arguments are in a structure, so that they can be passed through
4192 * taskqueue for a kernel process to execute this function.
4194 struct nfsrvdsremove {
4198 struct ucred *tcred;
4202 char fname[PNFS_FILENAME_LEN + 1];
4206 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4209 struct nameidata named;
4215 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4218 named.ni_cnd.cn_nameiop = DELETE;
4219 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4220 named.ni_cnd.cn_cred = tcred;
4221 named.ni_cnd.cn_thread = p;
4222 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4223 nfsvno_setpathbuf(&named, &bufp, &hashp);
4224 named.ni_cnd.cn_nameptr = bufp;
4225 named.ni_cnd.cn_namelen = strlen(fname);
4226 strlcpy(bufp, fname, NAME_MAX);
4227 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4228 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4229 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4231 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4234 NFSVOPUNLOCK(dvp, 0);
4235 nfsvno_relpathbuf(&named);
4237 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4242 * Start up the thread that will execute nfsrv_dsremove().
4245 start_dsremove(void *arg, int pending)
4247 struct nfsrvdsremove *dsrm;
4249 dsrm = (struct nfsrvdsremove *)arg;
4250 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4253 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4257 * Remove a pNFS data file from a Data Server.
4258 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4259 * removed to set up the dvp and fill in the FH.
4262 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4265 struct ucred *tcred;
4266 struct nfsrvdsremove *dsrm, *tdsrm;
4267 struct nfsdevice *ds;
4268 struct nfsmount *nmp;
4269 int failpos, i, ret, timo;
4271 tcred = newnfs_getcred();
4274 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4276 * Remove the file on each DS mirror, using kernel process(es) for the
4277 * additional mirrors.
4280 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4281 tdsrm->tcred = tcred;
4283 tdsrm->dvp = dvp[i];
4284 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4289 if (nfs_pnfsiothreads != 0) {
4290 ret = nfs_pnfsio(start_dsremove, tdsrm);
4291 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4294 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4295 if (failpos == -1 && nfsds_failerr(ret))
4299 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4300 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4301 failpos = mirrorcnt - 1;
4302 timo = hz / 50; /* Wait for 20msec. */
4305 /* Wait for kernel task(s) to complete. */
4306 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4307 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4308 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4309 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4314 * If failpos has been set, that mirror has failed, so it needs
4318 nmp = VFSTONFS(dvp[failpos]->v_mount);
4320 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4321 NFSMNTP_CANCELRPCS)) == 0) {
4322 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4324 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4325 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4328 nfsrv_killrpcs(nmp);
4330 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4336 /* Get rid all layouts for the file. */
4337 nfsrv_freefilelayouts(fhp);
4344 * Generate a file name based on the file handle and put it in *bufp.
4345 * Return the number of bytes generated.
4348 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4352 const uint8_t *hexdigits = "0123456789abcdef";
4354 cp = (uint8_t *)fhp;
4355 for (i = 0; i < sizeof(*fhp); i++) {
4356 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4357 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4364 * Update the Metadata file's attributes from the DS file when a Read/Write
4365 * layout is returned.
4366 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4367 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4370 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4372 struct ucred *tcred;
4375 /* Do this as root so that it won't fail with EACCES. */
4376 tcred = newnfs_getcred();
4377 error = nfsrv_proxyds(NULL, vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4378 NULL, NULL, NULL, nap, NULL);
4384 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4387 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4392 error = nfsrv_proxyds(NULL, vp, 0, 0, cred, p, NFSPROC_SETACL,
4393 NULL, NULL, NULL, NULL, aclp);
4398 nfsrv_proxyds(struct nfsrv_descript *nd, struct vnode *vp, off_t off, int cnt,
4399 struct ucred *cred, struct thread *p, int ioproc, struct mbuf **mpp,
4400 char *cp, struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp)
4402 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4403 fhandle_t fh[NFSDEV_MAXMIRRORS];
4404 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4405 struct nfsdevice *ds;
4406 struct pnfsdsattr dsattr;
4408 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4410 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4412 * If not a regular file, not exported or not a pNFS server,
4413 * just return ENOENT.
4415 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4416 nfsrv_devidcnt == 0)
4420 buf = malloc(buflen, M_TEMP, M_WAITOK);
4424 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4425 * from the MetaData file's extended attribute.
4427 if (ioproc == NFSPROC_GETATTR) {
4428 error = vn_extattr_get(vp, IO_NODELOCKED,
4429 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4431 if (error == 0 && buflen != sizeof(dsattr))
4434 NFSBCOPY(buf, &dsattr, buflen);
4435 nap->na_filerev = dsattr.dsa_filerev;
4436 nap->na_size = dsattr.dsa_size;
4437 nap->na_atime = dsattr.dsa_atime;
4438 nap->na_mtime = dsattr.dsa_mtime;
4441 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4442 * returns 0, just return now. nfsrv_checkdsattr()
4443 * returns 0 if there is no Read/Write layout
4444 * plus either an Open/Write_access or Write
4445 * delegation issued to a client for the file.
4447 if (nfsrv_pnfsgetdsattr == 0 ||
4448 nfsrv_checkdsattr(nd, vp, p) == 0) {
4455 * Clear ENOATTR so the code below will attempt to do a
4456 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4457 * the extended attribute.
4459 if (error == ENOATTR)
4468 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4470 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4471 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4472 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4475 for (i = 0; i < mirrorcnt; i++)
4476 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4478 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4480 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4483 if (origmircnt == -1)
4484 origmircnt = mirrorcnt;
4486 * If failpos is set to a mirror#, then that mirror has
4487 * failed and will be disabled. For Read and Getattr, the
4488 * function only tries one mirror, so if that mirror has
4489 * failed, it will need to be retried. As such, increment
4490 * tryitagain for these cases.
4491 * For Write, Setattr and Setacl, the function tries all
4492 * mirrors and will not return an error for the case where
4493 * one mirror has failed. For these cases, the functioning
4494 * mirror(s) will have been modified, so a retry isn't
4495 * necessary. These functions will set failpos for the
4498 if (ioproc == NFSPROC_READDS) {
4499 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4501 if (nfsds_failerr(error) && mirrorcnt > 1) {
4503 * Setting failpos will cause the mirror
4504 * to be disabled and then a retry of this
4511 } else if (ioproc == NFSPROC_WRITEDS)
4512 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4513 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4514 else if (ioproc == NFSPROC_SETATTR)
4515 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4516 mirrorcnt, nap, &failpos);
4517 else if (ioproc == NFSPROC_SETACL)
4518 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4519 mirrorcnt, aclp, &failpos);
4521 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4522 vp, nmp[mirrorcnt - 1], nap);
4523 if (nfsds_failerr(error) && mirrorcnt > 1) {
4525 * Setting failpos will cause the mirror
4526 * to be disabled and then a retry of this
4527 * getattr is required.
4529 failpos = mirrorcnt - 1;
4536 failnmp = nmp[failpos];
4537 NFSLOCKMNT(failnmp);
4538 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4539 NFSMNTP_CANCELRPCS)) == 0) {
4540 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4541 NFSUNLOCKMNT(failnmp);
4542 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4544 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4547 nfsrv_killrpcs(failnmp);
4548 NFSLOCKMNT(failnmp);
4549 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4552 NFSUNLOCKMNT(failnmp);
4554 for (i = 0; i < mirrorcnt; i++)
4555 NFSVOPUNLOCK(dvp[i], 0);
4556 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4558 /* Try the Read/Getattr again if a mirror was deleted. */
4559 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4562 /* Return ENOENT for any Extended Attribute error. */
4566 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4571 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4573 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4574 * to be checked. If it points to a NULL nmp, then it returns
4575 * a suitable destination.
4576 * curnmp - If non-NULL, it is the source mount for the copy.
4579 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4580 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4581 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4582 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4584 struct vnode *dvp, *nvp, **tdvpp;
4586 struct nfsmount *nmp, *newnmp;
4587 struct sockaddr *sad;
4588 struct sockaddr_in *sin;
4589 struct nfsdevice *ds, *tds, *fndds;
4590 struct pnfsdsfile *pf;
4592 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4594 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4603 if (newnmpp != NULL)
4608 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4609 "pnfsd.dsfile", buflenp, buf, p);
4610 mirrorcnt = *buflenp / sizeof(*pf);
4611 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4612 *buflenp != sizeof(*pf) * mirrorcnt))
4615 pf = (struct pnfsdsfile *)buf;
4616 /* If curnmp != NULL, check for a match in the mirror list. */
4617 if (curnmp != NULL && error == 0) {
4619 for (i = 0; i < mirrorcnt; i++, pf++) {
4620 sad = (struct sockaddr *)&pf->dsf_sin;
4621 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
4633 pf = (struct pnfsdsfile *)buf;
4634 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
4636 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
4638 sad = (struct sockaddr *)&pf->dsf_sin;
4640 dsdir = pf->dsf_dir;
4641 if (dsdir >= nfsrv_dsdirsize) {
4642 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
4644 } else if (nvpp != NULL && newnmp != NULL &&
4645 nfsaddr2_match(sad, newnmp->nm_nam))
4648 if (ippos != NULL && curnmp == NULL &&
4649 sad->sa_family == AF_INET &&
4650 sin->sin_addr.s_addr == 0)
4652 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
4654 /* Use the socket address to find the mount point. */
4657 /* Find a match for the IP address. */
4658 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
4659 if (ds->nfsdev_nmp != NULL) {
4660 dvp = ds->nfsdev_dvp;
4661 nmp = VFSTONFS(dvp->v_mount);
4662 if (nmp != ds->nfsdev_nmp)
4663 printf("different2 nmp %p %p\n",
4664 nmp, ds->nfsdev_nmp);
4665 if (nfsaddr2_match(sad, nmp->nm_nam)) {
4671 if (fndds != NULL && newnmpp != NULL &&
4673 /* Search for a place to make a mirror copy. */
4674 TAILQ_FOREACH(tds, &nfsrv_devidhead,
4676 if (tds->nfsdev_nmp != NULL &&
4678 ((tds->nfsdev_mdsisset == 0 &&
4679 fndds->nfsdev_mdsisset == 0) ||
4680 (tds->nfsdev_mdsisset != 0 &&
4681 fndds->nfsdev_mdsisset != 0 &&
4682 tds->nfsdev_mdsfsid.val[0] ==
4683 mp->mnt_stat.f_fsid.val[0] &&
4684 tds->nfsdev_mdsfsid.val[1] ==
4685 mp->mnt_stat.f_fsid.val[1]))) {
4686 *newnmpp = tds->nfsdev_nmp;
4692 * Move this entry to the end of the
4693 * list, so it won't be selected as
4694 * easily the next time.
4696 TAILQ_REMOVE(&nfsrv_devidhead, tds,
4698 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
4703 if (fndds != NULL) {
4704 dvp = fndds->nfsdev_dsdir[dsdir];
4705 if (lktype != 0 || fhiszero != 0 ||
4706 (nvpp != NULL && *nvpp == NULL)) {
4708 error = vn_lock(dvp,
4710 else if (lktype != 0)
4711 error = vn_lock(dvp, lktype);
4713 error = vn_lock(dvp, LK_SHARED);
4715 * If the file handle is all 0's, try to
4716 * do a Lookup against the DS to acquire
4718 * If dvpp == NULL or the Lookup fails,
4719 * unlock dvp after the call.
4721 if (error == 0 && (fhiszero != 0 ||
4722 (nvpp != NULL && *nvpp == NULL))) {
4723 error = nfsrv_pnfslookupds(vp,
4739 if (error != 0 || lktype == 0)
4740 NFSVOPUNLOCK(dvp, 0);
4745 NFSD_DEBUG(4, "gotone=%d\n", gotone);
4746 if (devid != NULL) {
4747 NFSBCOPY(fndds->nfsdev_deviceid,
4748 devid, NFSX_V4DEVICEID);
4749 devid += NFSX_V4DEVICEID;
4754 NFSBCOPY(&pf->dsf_fh, fhp++,
4756 if (fnamep != NULL && gotone == 1)
4759 sizeof(pf->dsf_filename));
4761 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
4766 if (error == 0 && gotone == 0)
4769 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
4772 *mirrorcntp = gotone;
4774 if (gotone > 0 && dvpp != NULL) {
4776 * If the error didn't occur on the first one and
4777 * dvpp != NULL, the one(s) prior to the failure will
4778 * have locked dvp's that need to be unlocked.
4780 for (i = 0; i < gotone; i++) {
4781 NFSVOPUNLOCK(*dvpp, 0);
4786 * If it found the vnode to be copied from before a failure,
4787 * it needs to be vput()'d.
4789 if (nvpp != NULL && *nvpp != NULL) {
4798 * Set the extended attribute for the Change attribute.
4801 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4803 struct pnfsdsattr dsattr;
4806 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
4807 dsattr.dsa_filerev = nap->na_filerev;
4808 dsattr.dsa_size = nap->na_size;
4809 dsattr.dsa_atime = nap->na_atime;
4810 dsattr.dsa_mtime = nap->na_mtime;
4811 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4812 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
4814 printf("pNFS: setextattr=%d\n", error);
4819 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
4820 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
4823 struct nfsrv_descript *nd;
4825 struct mbuf *m, *m2;
4826 int error = 0, retlen, tlen, trimlen;
4828 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
4829 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4832 * Use a stateid where other is an alternating 01010 pattern and
4833 * seqid is 0xffffffff. This value is not defined as special by
4834 * the RFC and is used by the FreeBSD NFS server to indicate an
4835 * MDS->DS proxy operation.
4837 st.other[0] = 0x55555555;
4838 st.other[1] = 0x55555555;
4839 st.other[2] = 0x55555555;
4840 st.seqid = 0xffffffff;
4841 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
4843 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
4844 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
4845 txdr_hyper(off, tl);
4846 *(tl + 2) = txdr_unsigned(len);
4847 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4848 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4853 if (nd->nd_repstat == 0) {
4854 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4855 NFSM_STRSIZ(retlen, len);
4857 /* Trim off the pre-data XDR from the mbuf chain. */
4859 while (m != NULL && m != nd->nd_md) {
4860 if (m->m_next == nd->nd_md) {
4862 m_freem(nd->nd_mrep);
4863 nd->nd_mrep = m = nd->nd_md;
4868 printf("nfsrv_readdsrpc: busted mbuf list\n");
4874 * Now, adjust first mbuf so that any XDR before the
4875 * read data is skipped over.
4877 trimlen = nd->nd_dpos - mtod(m, char *);
4879 m->m_len -= trimlen;
4880 NFSM_DATAP(m, trimlen);
4884 * Truncate the mbuf chain at retlen bytes of data,
4885 * plus XDR padding that brings the length up to a
4888 tlen = NFSM_RNDUP(retlen);
4890 if (m->m_len >= tlen) {
4900 } while (m != NULL);
4902 printf("nfsrv_readdsrpc: busted mbuf list\n");
4911 error = nd->nd_repstat;
4913 /* If nd->nd_mrep is already NULL, this is a no-op. */
4914 m_freem(nd->nd_mrep);
4916 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
4921 * Do a write RPC on a DS data file, using this structure for the arguments,
4922 * so that this function can be executed by a separate kernel process.
4924 struct nfsrvwritedsdorpc {
4931 struct nfsmount *nmp;
4939 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
4940 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
4943 struct nfsrv_descript *nd;
4944 nfsattrbit_t attrbits;
4946 int commit, error, retlen;
4948 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4949 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
4950 sizeof(fhandle_t), NULL, NULL, 0, 0);
4953 * Use a stateid where other is an alternating 01010 pattern and
4954 * seqid is 0xffffffff. This value is not defined as special by
4955 * the RFC and is used by the FreeBSD NFS server to indicate an
4956 * MDS->DS proxy operation.
4958 st.other[0] = 0x55555555;
4959 st.other[1] = 0x55555555;
4960 st.other[2] = 0x55555555;
4961 st.seqid = 0xffffffff;
4962 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
4963 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
4964 txdr_hyper(off, tl);
4967 * Do all writes FileSync, since the server doesn't hold onto dirty
4968 * buffers. Since clients should be accessing the DS servers directly
4969 * using the pNFS layouts, this just needs to work correctly as a
4972 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
4973 *tl = txdr_unsigned(len);
4974 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
4976 /* Put data in mbuf chain. */
4977 nd->nd_mb->m_next = m;
4979 /* Set nd_mb and nd_bpos to end of data. */
4980 while (m->m_next != NULL)
4983 nd->nd_bpos = mtod(m, char *) + m->m_len;
4984 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
4986 /* Do a Getattr for Size, Change and Modify Time. */
4987 NFSZERO_ATTRBIT(&attrbits);
4988 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
4989 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
4990 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
4991 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
4992 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
4993 *tl = txdr_unsigned(NFSV4OP_GETATTR);
4994 (void) nfsrv_putattrbit(nd, &attrbits);
4995 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
4996 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5001 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5002 /* Get rid of weak cache consistency data for now. */
5003 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5004 (ND_NFSV4 | ND_V4WCCATTR)) {
5005 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5006 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5007 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5011 * Get rid of Op# and status for next op.
5013 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5015 nd->nd_flag |= ND_NOMOREDATA;
5017 if (nd->nd_repstat == 0) {
5018 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5019 retlen = fxdr_unsigned(int, *tl++);
5020 commit = fxdr_unsigned(int, *tl);
5021 if (commit != NFSWRITE_FILESYNC)
5023 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5024 retlen, commit, error);
5026 error = nd->nd_repstat;
5027 /* We have no use for the Write Verifier since we use FileSync. */
5030 * Get the Change, Size, Access Time and Modify Time attributes and set
5031 * on the Metadata file, so its attributes will be what the file's
5032 * would be if it had been written.
5035 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5036 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5037 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5039 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5041 m_freem(nd->nd_mrep);
5043 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5048 * Start up the thread that will execute nfsrv_writedsdorpc().
5051 start_writedsdorpc(void *arg, int pending)
5053 struct nfsrvwritedsdorpc *drpc;
5055 drpc = (struct nfsrvwritedsdorpc *)arg;
5056 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5057 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5059 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5063 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5064 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5065 struct mbuf **mpp, char *cp, int *failposp)
5067 struct nfsrvwritedsdorpc *drpc, *tdrpc;
5070 int error, i, offs, ret, timo;
5072 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5073 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5076 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5079 /* Calculate offset in mbuf chain that data starts. */
5080 offs = cp - mtod(*mpp, char *);
5081 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5084 * Do the write RPC for every DS, using a separate kernel process
5085 * for every DS except the last one.
5088 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5090 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5098 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5100 if (nfs_pnfsiothreads != 0) {
5101 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5102 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5106 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5108 if (nfsds_failerr(ret) && *failposp == -1)
5110 else if (error == 0 && ret != 0)
5116 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5117 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5118 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5119 *failposp = mirrorcnt - 1;
5120 else if (error == 0 && ret != 0)
5123 error = nfsrv_setextattr(vp, &na, p);
5124 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5126 timo = hz / 50; /* Wait for 20msec. */
5129 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5130 /* Wait for RPCs on separate threads to complete. */
5131 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5132 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5133 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5135 else if (error == 0 && tdrpc->err != 0)
5143 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5144 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5145 struct nfsvattr *dsnap)
5148 struct nfsrv_descript *nd;
5150 nfsattrbit_t attrbits;
5153 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5154 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5156 * Use a stateid where other is an alternating 01010 pattern and
5157 * seqid is 0xffffffff. This value is not defined as special by
5158 * the RFC and is used by the FreeBSD NFS server to indicate an
5159 * MDS->DS proxy operation.
5161 st.other[0] = 0x55555555;
5162 st.other[1] = 0x55555555;
5163 st.other[2] = 0x55555555;
5164 st.seqid = 0xffffffff;
5165 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5167 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5168 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5170 /* Do a Getattr for Size, Change, Access Time and Modify Time. */
5171 NFSZERO_ATTRBIT(&attrbits);
5172 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5173 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5174 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5175 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5176 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5177 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5178 (void) nfsrv_putattrbit(nd, &attrbits);
5179 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5180 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5185 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5187 /* Get rid of weak cache consistency data for now. */
5188 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5189 (ND_NFSV4 | ND_V4WCCATTR)) {
5190 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5191 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5192 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5196 * Get rid of Op# and status for next op.
5198 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5200 nd->nd_flag |= ND_NOMOREDATA;
5202 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5205 if (nd->nd_repstat != 0)
5206 error = nd->nd_repstat;
5208 * Get the Change, Size, Access Time and Modify Time attributes and set
5209 * on the Metadata file, so its attributes will be what the file's
5210 * would be if it had been written.
5213 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5214 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5215 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5217 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5219 m_freem(nd->nd_mrep);
5221 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5225 struct nfsrvsetattrdsdorpc {
5230 struct nfsmount *nmp;
5235 struct nfsvattr dsna;
5240 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5243 start_setattrdsdorpc(void *arg, int pending)
5245 struct nfsrvsetattrdsdorpc *drpc;
5247 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5248 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5249 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5254 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5255 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5256 struct nfsvattr *nap, int *failposp)
5258 struct nfsrvsetattrdsdorpc *drpc, *tdrpc;
5260 int error, i, ret, timo;
5262 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5265 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5269 * Do the setattr RPC for every DS, using a separate kernel process
5270 * for every DS except the last one.
5273 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5276 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5284 if (nfs_pnfsiothreads != 0) {
5285 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5286 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5290 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5292 if (nfsds_failerr(ret) && *failposp == -1)
5294 else if (error == 0 && ret != 0)
5300 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5301 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5302 *failposp = mirrorcnt - 1;
5303 else if (error == 0 && ret != 0)
5306 error = nfsrv_setextattr(vp, &na, p);
5307 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5309 timo = hz / 50; /* Wait for 20msec. */
5312 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5313 /* Wait for RPCs on separate threads to complete. */
5314 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5315 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5316 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5318 else if (error == 0 && tdrpc->err != 0)
5326 * Do a Setattr of an NFSv4 ACL on the DS file.
5329 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5330 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5332 struct nfsrv_descript *nd;
5334 nfsattrbit_t attrbits;
5337 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5338 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5340 * Use a stateid where other is an alternating 01010 pattern and
5341 * seqid is 0xffffffff. This value is not defined as special by
5342 * the RFC and is used by the FreeBSD NFS server to indicate an
5343 * MDS->DS proxy operation.
5345 st.other[0] = 0x55555555;
5346 st.other[1] = 0x55555555;
5347 st.other[2] = 0x55555555;
5348 st.seqid = 0xffffffff;
5349 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5351 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5352 NFSZERO_ATTRBIT(&attrbits);
5353 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5355 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5356 * so passing in the metadata "vp" will be ok, since it is of
5357 * the same type (VREG).
5359 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5360 NULL, 0, 0, 0, 0, 0, NULL);
5361 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5362 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5367 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5369 error = nd->nd_repstat;
5370 m_freem(nd->nd_mrep);
5375 struct nfsrvsetacldsdorpc {
5380 struct nfsmount *nmp;
5389 * Start up the thread that will execute nfsrv_setacldsdorpc().
5392 start_setacldsdorpc(void *arg, int pending)
5394 struct nfsrvsetacldsdorpc *drpc;
5396 drpc = (struct nfsrvsetacldsdorpc *)arg;
5397 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5398 drpc->vp, drpc->nmp, drpc->aclp);
5403 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5404 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5407 struct nfsrvsetacldsdorpc *drpc, *tdrpc;
5408 int error, i, ret, timo;
5410 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5413 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5417 * Do the setattr RPC for every DS, using a separate kernel process
5418 * for every DS except the last one.
5421 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5424 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5432 if (nfs_pnfsiothreads != 0) {
5433 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5434 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5438 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5440 if (nfsds_failerr(ret) && *failposp == -1)
5442 else if (error == 0 && ret != 0)
5448 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5449 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5450 *failposp = mirrorcnt - 1;
5451 else if (error == 0 && ret != 0)
5453 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5455 timo = hz / 50; /* Wait for 20msec. */
5458 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5459 /* Wait for RPCs on separate threads to complete. */
5460 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5461 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
5462 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5464 else if (error == 0 && tdrpc->err != 0)
5472 * Getattr call to the DS for the Modify, Size and Change attributes.
5475 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5476 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
5478 struct nfsrv_descript *nd;
5480 nfsattrbit_t attrbits;
5482 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
5483 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5484 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
5485 sizeof(fhandle_t), NULL, NULL, 0, 0);
5486 NFSZERO_ATTRBIT(&attrbits);
5487 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5488 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5489 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5490 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5491 (void) nfsrv_putattrbit(nd, &attrbits);
5492 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5493 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5498 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
5500 if (nd->nd_repstat == 0) {
5501 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5502 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5505 * We can only save the updated values in the extended
5506 * attribute if the vp is exclusively locked.
5507 * This should happen when any of the following operations
5508 * occur on the vnode:
5509 * Close, Delegreturn, LayoutCommit, LayoutReturn
5510 * As such, the updated extended attribute should get saved
5511 * before nfsrv_checkdsattr() returns 0 and allows the cached
5512 * attributes to be returned without calling this function.
5514 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
5515 error = nfsrv_setextattr(vp, nap, p);
5516 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
5520 error = nd->nd_repstat;
5521 m_freem(nd->nd_mrep);
5523 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
5528 * Get the device id and file handle for a DS file.
5531 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
5532 fhandle_t *fhp, char *devid)
5538 buf = malloc(buflen, M_TEMP, M_WAITOK);
5539 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
5540 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
5546 * Do a Lookup against the DS for the filename.
5549 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
5550 struct vnode **nvpp, NFSPROC_T *p)
5552 struct nameidata named;
5553 struct ucred *tcred;
5559 tcred = newnfs_getcred();
5560 named.ni_cnd.cn_nameiop = LOOKUP;
5561 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
5562 named.ni_cnd.cn_cred = tcred;
5563 named.ni_cnd.cn_thread = p;
5564 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
5565 nfsvno_setpathbuf(&named, &bufp, &hashp);
5566 named.ni_cnd.cn_nameptr = bufp;
5567 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
5568 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
5569 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
5570 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
5571 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
5573 nfsvno_relpathbuf(&named);
5576 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
5581 * Set the file handle to the correct one.
5584 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
5585 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
5591 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
5593 * We can only do a vn_set_extattr() if the vnode is exclusively
5594 * locked and vn_start_write() has been done. If devid != NULL or
5595 * fnamep != NULL or the vnode is shared locked, vn_start_write()
5596 * may not have been done.
5597 * If not done now, it will be done on a future call.
5599 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
5601 ret = vn_extattr_set(vp, IO_NODELOCKED,
5602 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
5604 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
5608 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
5609 * when the DS has failed.
5612 nfsrv_killrpcs(struct nfsmount *nmp)
5616 * Call newnfs_nmcancelreqs() to cause
5617 * any RPCs in progress on the mount point to
5619 * This will cause any process waiting for an
5620 * RPC to complete while holding a vnode lock
5621 * on the mounted-on vnode (such as "df" or
5622 * a non-forced "umount") to fail.
5623 * This will unlock the mounted-on vnode so
5624 * a forced dismount can succeed.
5625 * The NFSMNTP_CANCELRPCS flag should be set when this function is
5628 newnfs_nmcancelreqs(nmp);
5632 * Sum up the statfs info for each of the DSs, so that the client will
5633 * receive the total for all DSs.
5636 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
5639 struct nfsdevice *ds;
5640 struct vnode **dvpp, **tdvpp, *dvp;
5642 int cnt, error = 0, i;
5644 if (nfsrv_devidcnt <= 0)
5646 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
5647 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
5649 /* Get an array of the dvps for the DSs. */
5653 /* First, search for matches for same file system. */
5654 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5655 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
5656 ds->nfsdev_mdsfsid.val[0] == mp->mnt_stat.f_fsid.val[0] &&
5657 ds->nfsdev_mdsfsid.val[1] == mp->mnt_stat.f_fsid.val[1]) {
5658 if (++i > nfsrv_devidcnt)
5660 *tdvpp++ = ds->nfsdev_dvp;
5664 * If no matches for same file system, total all servers not assigned
5668 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5669 if (ds->nfsdev_nmp != NULL &&
5670 ds->nfsdev_mdsisset == 0) {
5671 if (++i > nfsrv_devidcnt)
5673 *tdvpp++ = ds->nfsdev_dvp;
5680 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
5682 for (i = 0; i < cnt && error == 0; i++) {
5684 error = VFS_STATFS(dvp->v_mount, tsf);
5686 if (sf->f_bsize == 0) {
5687 if (tsf->f_bsize > 0)
5688 sf->f_bsize = tsf->f_bsize;
5692 if (tsf->f_blocks > 0) {
5693 if (sf->f_bsize != tsf->f_bsize) {
5694 tot = tsf->f_blocks * tsf->f_bsize;
5695 sf->f_blocks += (tot / sf->f_bsize);
5697 sf->f_blocks += tsf->f_blocks;
5699 if (tsf->f_bfree > 0) {
5700 if (sf->f_bsize != tsf->f_bsize) {
5701 tot = tsf->f_bfree * tsf->f_bsize;
5702 sf->f_bfree += (tot / sf->f_bsize);
5704 sf->f_bfree += tsf->f_bfree;
5706 if (tsf->f_bavail > 0) {
5707 if (sf->f_bsize != tsf->f_bsize) {
5708 tot = tsf->f_bavail * tsf->f_bsize;
5709 sf->f_bavail += (tot / sf->f_bsize);
5711 sf->f_bavail += tsf->f_bavail;
5724 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
5728 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
5729 error = NFSERR_ATTRNOTSUPP;
5733 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
5734 * Make sure it has enough room for that - splitting every entry
5735 * into two and appending "canonical six" entries at the end.
5736 * Cribbed out of kern/vfs_acl.c - Rick M.
5738 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
5739 error = NFSERR_ATTRNOTSUPP;
5742 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
5744 error = nfsrv_dssetacl(vp, aclp, cred, p);
5745 if (error == ENOENT)
5754 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
5757 * Called once to initialize data structures...
5760 nfsd_modevent(module_t mod, int type, void *data)
5763 static int loaded = 0;
5770 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
5771 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
5773 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
5776 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
5777 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
5778 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
5779 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
5780 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
5781 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
5788 #ifdef VV_DISABLEDELEG
5789 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
5790 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
5792 nfsd_call_servertimer = nfsrv_servertimer;
5793 nfsd_call_nfsd = nfssvc_nfsd;
5798 if (newnfs_numnfsd != 0) {
5803 #ifdef VV_DISABLEDELEG
5804 vn_deleg_ops.vndeleg_recall = NULL;
5805 vn_deleg_ops.vndeleg_disable = NULL;
5807 nfsd_call_servertimer = NULL;
5808 nfsd_call_nfsd = NULL;
5810 /* Clean out all NFSv4 state. */
5811 nfsrv_throwawayallstate(curthread);
5813 /* Clean the NFS server reply cache */
5814 nfsrvd_cleancache();
5816 /* Free up the krpc server pool. */
5817 if (nfsrvd_pool != NULL)
5818 svcpool_destroy(nfsrvd_pool);
5820 /* and get rid of the locks */
5821 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
5822 mtx_destroy(&nfsrchash_table[i].mtx);
5823 mtx_destroy(&nfsrcahash_table[i].mtx);
5825 mtx_destroy(&nfsrc_udpmtx);
5826 mtx_destroy(&nfs_v4root_mutex);
5827 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
5828 mtx_destroy(&nfsrv_dontlistlock_mtx);
5829 mtx_destroy(&nfsrv_recalllock_mtx);
5830 for (i = 0; i < nfsrv_sessionhashsize; i++)
5831 mtx_destroy(&nfssessionhash[i].mtx);
5832 if (nfslayouthash != NULL) {
5833 for (i = 0; i < nfsrv_layouthashsize; i++)
5834 mtx_destroy(&nfslayouthash[i].mtx);
5835 free(nfslayouthash, M_NFSDSESSION);
5837 lockdestroy(&nfsv4root_mnt.mnt_explock);
5838 free(nfsclienthash, M_NFSDCLIENT);
5839 free(nfslockhash, M_NFSDLOCKFILE);
5840 free(nfssessionhash, M_NFSDSESSION);
5852 static moduledata_t nfsd_mod = {
5857 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
5859 /* So that loader and kldload(2) can find us, wherever we are.. */
5860 MODULE_VERSION(nfsd, 1);
5861 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
5862 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1);
5863 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
5864 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
5865 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);