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>
49 #include <security/mac/mac_framework.h>
50 #include <sys/filio.h>
52 #include <sys/sysctl.h>
53 #include <nlm/nlm_prot.h>
56 FEATURE(nfsd, "NFSv4 server");
58 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
59 extern int nfsrv_useacl;
60 extern int newnfs_numnfsd;
61 extern struct mount nfsv4root_mnt;
62 extern struct nfsrv_stablefirst nfsrv_stablefirst;
63 extern void (*nfsd_call_servertimer)(void);
64 extern SVCPOOL *nfsrvd_pool;
65 extern struct nfsv4lock nfsd_suspend_lock;
66 extern struct nfsclienthashhead *nfsclienthash;
67 extern struct nfslockhashhead *nfslockhash;
68 extern struct nfssessionhash *nfssessionhash;
69 extern int nfsrv_sessionhashsize;
70 extern struct nfsstatsv1 nfsstatsv1;
71 extern struct nfslayouthash *nfslayouthash;
72 extern int nfsrv_layouthashsize;
73 extern struct mtx nfsrv_dslock_mtx;
74 extern int nfs_pnfsiothreads;
75 extern struct nfsdontlisthead nfsrv_dontlisthead;
76 extern volatile int nfsrv_dontlistlen;
77 extern volatile int nfsrv_devidcnt;
78 extern int nfsrv_maxpnfsmirror;
79 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
82 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
83 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
84 struct mtx nfsrc_udpmtx;
85 struct mtx nfs_v4root_mutex;
86 struct mtx nfsrv_dontlistlock_mtx;
87 struct mtx nfsrv_recalllock_mtx;
88 struct nfsrvfh nfs_rootfh, nfs_pubfh;
89 int nfs_pubfhset = 0, nfs_rootfhset = 0;
90 struct proc *nfsd_master_proc = NULL;
91 int nfsd_debuglevel = 0;
92 static pid_t nfsd_master_pid = (pid_t)-1;
93 static char nfsd_master_comm[MAXCOMLEN + 1];
94 static struct timeval nfsd_master_start;
95 static uint32_t nfsv4_sysid = 0;
96 static fhandle_t zerofh;
98 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
101 int nfsrv_enable_crossmntpt = 1;
102 static int nfs_commit_blks;
103 static int nfs_commit_miss;
104 extern int nfsrv_issuedelegs;
105 extern int nfsrv_dolocallocks;
106 extern int nfsd_enable_stringtouid;
107 extern struct nfsdevicehead nfsrv_devidhead;
109 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
111 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
112 struct mbuf **, struct iovec **);
113 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
115 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
117 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
118 int *, char *, fhandle_t *);
119 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
121 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
122 struct thread *, int, struct mbuf **, char *, struct mbuf **,
123 struct nfsvattr *, struct acl *, off_t *, int, bool *);
124 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
125 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
126 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
127 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
128 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
130 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
131 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
132 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
133 struct vnode *, struct nfsmount **, int, struct acl *, int *);
134 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
135 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
136 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
137 struct vnode *, struct nfsmount *, struct nfsvattr *);
138 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
139 NFSPROC_T *, struct nfsmount *);
140 static int nfsrv_putfhname(fhandle_t *, char *);
141 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
142 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
143 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
144 struct vnode *, NFSPROC_T *);
145 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
146 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
148 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
149 static void nfsm_trimtrailing(struct nfsrv_descript *, struct mbuf *,
152 int nfs_pnfsio(task_fn_t *, void *);
154 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
156 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
157 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
158 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
160 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
162 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
163 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
164 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
165 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
166 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
167 0, "Debug level for NFS server");
168 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
169 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
170 static int nfsrv_pnfsgetdsattr = 1;
171 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
172 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
175 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
177 * The dsN subdirectories for the increased values must have been created
178 * on all DS servers before this increase is done.
180 u_int nfsrv_dsdirsize = 20;
182 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
184 int error, newdsdirsize;
186 newdsdirsize = nfsrv_dsdirsize;
187 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
188 if (error != 0 || req->newptr == NULL)
190 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
193 nfsrv_dsdirsize = newdsdirsize;
196 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
197 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
198 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
200 #define MAX_REORDERED_RPC 16
201 #define NUM_HEURISTIC 1031
202 #define NHUSE_INIT 64
204 #define NHUSE_MAX 2048
206 static struct nfsheur {
207 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
208 off_t nh_nextoff; /* next offset for sequential detection */
209 int nh_use; /* use count for selection */
210 int nh_seqcount; /* heuristic */
211 } nfsheur[NUM_HEURISTIC];
215 * Heuristic to detect sequential operation.
217 static struct nfsheur *
218 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
223 /* Locate best candidate. */
225 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
228 if (nfsheur[hi].nh_vp == vp) {
232 if (nfsheur[hi].nh_use > 0)
233 --nfsheur[hi].nh_use;
234 hi = (hi + 1) % NUM_HEURISTIC;
235 if (nfsheur[hi].nh_use < nh->nh_use)
239 /* Initialize hint if this is a new file. */
240 if (nh->nh_vp != vp) {
242 nh->nh_nextoff = uio->uio_offset;
243 nh->nh_use = NHUSE_INIT;
244 if (uio->uio_offset == 0)
250 /* Calculate heuristic. */
251 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
252 uio->uio_offset == nh->nh_nextoff) {
253 /* See comments in vfs_vnops.c:sequential_heuristic(). */
254 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
255 if (nh->nh_seqcount > IO_SEQMAX)
256 nh->nh_seqcount = IO_SEQMAX;
257 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
258 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
259 /* Probably a reordered RPC, leave seqcount alone. */
260 } else if (nh->nh_seqcount > 1) {
261 nh->nh_seqcount /= 2;
265 nh->nh_use += NHUSE_INC;
266 if (nh->nh_use > NHUSE_MAX)
267 nh->nh_use = NHUSE_MAX;
272 * Get attributes into nfsvattr structure.
275 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
276 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
277 nfsattrbit_t *attrbitp)
279 int error, gotattr, lockedit = 0;
282 if (vpislocked == 0) {
284 * When vpislocked == 0, the vnode is either exclusively
285 * locked by this thread or not locked by this thread.
286 * As such, shared lock it, if not exclusively locked.
288 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
290 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
295 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
296 * attributes, as required.
297 * This needs to be done for regular files if:
298 * - non-NFSv4 RPCs or
299 * - when attrbitp == NULL or
300 * - an NFSv4 RPC with any of the above attributes in attrbitp.
301 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
302 * these attributes. nfsrv_proxyds() will return an error if the
303 * server is not a pNFS one.
306 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
307 (nd->nd_flag & ND_NFSV4) == 0 ||
308 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
309 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
310 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
311 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
312 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
313 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
314 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
320 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
325 * If we got the Change, Size and Modify Time from the DS,
329 nvap->na_atime = na.na_atime;
330 nvap->na_mtime = na.na_mtime;
331 nvap->na_filerev = na.na_filerev;
332 nvap->na_size = na.na_size;
333 nvap->na_bytes = na.na_bytes;
335 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
336 error, (uintmax_t)na.na_filerev);
343 * Get a file handle for a vnode.
346 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
350 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
351 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
352 error = VOP_VPTOFH(vp, &fhp->fh_fid);
359 * Perform access checking for vnodes obtained from file handles that would
360 * refer to files already opened by a Unix client. You cannot just use
361 * vn_writechk() and VOP_ACCESSX() for two reasons.
362 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
364 * 2 - The owner is to be given access irrespective of mode bits for some
365 * operations, so that processes that chmod after opening a file don't
369 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
370 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
371 u_int32_t *supportedtypep)
374 int error = 0, getret = 0;
376 if (vpislocked == 0) {
377 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
382 if (accmode & VWRITE) {
383 /* Just vn_writechk() changed to check rdonly */
385 * Disallow write attempts on read-only file systems;
386 * unless the file is a socket or a block or character
387 * device resident on the file system.
389 if (NFSVNO_EXRDONLY(exp) ||
390 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
391 switch (vp->v_type) {
401 * If there's shared text associated with
402 * the inode, try to free it up once. If
403 * we fail, we can't allow writing.
405 if (VOP_IS_TEXT(vp) && error == 0)
415 * Should the override still be applied when ACLs are enabled?
417 error = VOP_ACCESSX(vp, accmode, cred, p);
418 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
420 * Try again with VEXPLICIT_DENY, to see if the test for
421 * deletion is supported.
423 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
425 if (vp->v_type == VDIR) {
426 accmode &= ~(VDELETE | VDELETE_CHILD);
428 error = VOP_ACCESSX(vp, accmode, cred, p);
429 } else if (supportedtypep != NULL) {
430 *supportedtypep &= ~NFSACCESS_DELETE;
436 * Allow certain operations for the owner (reads and writes
437 * on files that are already open).
439 if (override != NFSACCCHK_NOOVERRIDE &&
440 (error == EPERM || error == EACCES)) {
441 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
443 else if (override & NFSACCCHK_ALLOWOWNER) {
444 getret = VOP_GETATTR(vp, &vattr, cred);
445 if (getret == 0 && cred->cr_uid == vattr.va_uid)
458 * Set attribute(s) vnop.
461 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
462 struct thread *p, struct nfsexstuff *exp)
464 u_quad_t savsize = 0;
469 * If this is an exported file system and a pNFS service is running,
470 * don't VOP_SETATTR() of size for the MDS file system.
474 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
475 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
476 nvap->na_vattr.va_size > 0) {
477 savsize = nvap->na_vattr.va_size;
478 nvap->na_vattr.va_size = VNOVAL;
479 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
480 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
481 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
482 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
483 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
489 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
491 nvap->na_vattr.va_size = savsize;
492 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
493 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
494 nvap->na_vattr.va_size != VNOVAL ||
495 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
496 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
497 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
498 /* Never modify birthtime on a DS file. */
499 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
500 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
501 /* For a pNFS server, set the attributes on the DS file. */
502 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
503 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
504 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
513 * Set up nameidata for a lookup() call and do it.
516 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
517 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
518 struct vnode **retdirp)
520 struct componentname *cnp = &ndp->ni_cnd;
524 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
529 cnp->cn_nameptr = cnp->cn_pnbuf;
532 * Extract and set starting directory.
534 if (dp->v_type != VDIR) {
539 nfsvno_relpathbuf(ndp);
547 if (NFSVNO_EXRDONLY(exp))
548 cnp->cn_flags |= RDONLY;
549 ndp->ni_segflg = UIO_SYSSPACE;
551 if (nd->nd_flag & ND_PUBLOOKUP) {
553 if (cnp->cn_pnbuf[0] == '/') {
556 * Check for degenerate pathnames here, since lookup()
559 for (i = 1; i < ndp->ni_pathlen; i++)
560 if (cnp->cn_pnbuf[i] != '/')
562 if (i == ndp->ni_pathlen) {
563 error = NFSERR_ACCES;
569 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
570 (nd->nd_flag & ND_NFSV4) == 0) {
572 * Only cross mount points for NFSv4 when doing a
573 * mount while traversing the file system above
574 * the mount point, unless nfsrv_enable_crossmntpt is set.
576 cnp->cn_flags |= NOCROSSMOUNT;
580 * Initialize for scan, set ni_startdir and bump ref on dp again
581 * because lookup() will dereference ni_startdir.
585 ndp->ni_startdir = dp;
586 ndp->ni_rootdir = rootvnode;
587 ndp->ni_topdir = NULL;
590 cnp->cn_flags |= LOCKLEAF;
592 cnp->cn_nameptr = cnp->cn_pnbuf;
594 * Call lookup() to do the real work. If an error occurs,
595 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
596 * we do not have to dereference anything before returning.
597 * In either case ni_startdir will be dereferenced and NULLed
605 * Check for encountering a symbolic link. Trivial
606 * termination occurs if no symlink encountered.
608 if ((cnp->cn_flags & ISSYMLINK) == 0) {
609 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
610 nfsvno_relpathbuf(ndp);
611 if (ndp->ni_vp && !lockleaf)
612 NFSVOPUNLOCK(ndp->ni_vp);
619 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
620 NFSVOPUNLOCK(ndp->ni_dvp);
621 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
626 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
630 if (ndp->ni_pathlen > 1)
631 cp = uma_zalloc(namei_zone, M_WAITOK);
635 aiov.iov_len = MAXPATHLEN;
636 auio.uio_iov = &aiov;
639 auio.uio_rw = UIO_READ;
640 auio.uio_segflg = UIO_SYSSPACE;
642 auio.uio_resid = MAXPATHLEN;
643 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
646 if (ndp->ni_pathlen > 1)
647 uma_zfree(namei_zone, cp);
653 linklen = MAXPATHLEN - auio.uio_resid;
658 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
659 error = ENAMETOOLONG;
664 * Adjust or replace path
666 if (ndp->ni_pathlen > 1) {
667 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
668 uma_zfree(namei_zone, cnp->cn_pnbuf);
671 cnp->cn_pnbuf[linklen] = '\0';
672 ndp->ni_pathlen += linklen;
675 * Cleanup refs for next loop and check if root directory
676 * should replace current directory. Normally ni_dvp
677 * becomes the new base directory and is cleaned up when
678 * we loop. Explicitly null pointers after invalidation
679 * to clarify operation.
684 if (cnp->cn_pnbuf[0] == '/') {
686 ndp->ni_dvp = ndp->ni_rootdir;
689 ndp->ni_startdir = ndp->ni_dvp;
693 cnp->cn_flags &= ~LOCKLEAF;
697 nfsvno_relpathbuf(ndp);
700 ndp->ni_startdir = NULL;
701 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
706 NFSEXITCODE2(error, nd);
711 * Set up a pathname buffer and return a pointer to it and, optionally
712 * set a hash pointer.
715 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
717 struct componentname *cnp = &ndp->ni_cnd;
719 cnp->cn_flags |= (NOMACCHECK | HASBUF);
720 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
723 *bufpp = cnp->cn_pnbuf;
727 * Release the above path buffer, if not released by nfsvno_namei().
730 nfsvno_relpathbuf(struct nameidata *ndp)
733 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
735 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
736 ndp->ni_cnd.cn_flags &= ~HASBUF;
740 * Readlink vnode op into an mbuf list.
743 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
744 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
747 struct uio io, *uiop = &io;
748 struct mbuf *mp, *mp3;
749 int len, tlen, error = 0;
751 len = NFS_MAXPATHLEN;
753 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
756 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
758 uiop->uio_offset = 0;
759 uiop->uio_resid = len;
760 uiop->uio_rw = UIO_READ;
761 uiop->uio_segflg = UIO_SYSSPACE;
763 error = VOP_READLINK(vp, uiop, cred);
770 if (uiop->uio_resid > 0) {
771 len -= uiop->uio_resid;
772 tlen = NFSM_RNDUP(len);
776 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
777 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
790 * Create an mbuf chain and an associated iovec that can be used to Read
791 * or Getextattr of data.
792 * Upon success, return pointers to the first and last mbufs in the chain
793 * plus the malloc'd iovec and its iovlen.
796 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
799 struct mbuf *m, *m2 = NULL, *m3;
806 * Generate the mbuf list with the uio_iov ref. to it.
813 siz = min(M_TRAILINGSPACE(m), left);
822 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
828 panic("nfsrv_createiovec iov");
829 siz = min(M_TRAILINGSPACE(m), left);
831 iv->iov_base = mtod(m, caddr_t) + m->m_len;
846 * Create an mbuf chain and an associated iovec that can be used to Read
847 * or Getextattr of data.
848 * Upon success, return pointers to the first and last mbufs in the chain
849 * plus the malloc'd iovec and its iovlen.
850 * Same as above, but creates ext_pgs mbuf(s).
853 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
854 struct mbuf **mpendp, struct iovec **ivp)
856 struct mbuf *m, *m2 = NULL, *m3;
858 int i, left, pgno, siz;
863 * Generate the mbuf list with the uio_iov ref. to it.
867 siz = min(left, maxextsiz);
868 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
877 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
884 panic("nfsvno_createiovec_extpgs iov");
885 siz = min(PAGE_SIZE, left);
887 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
890 if (pgno == m->m_epg_npgs - 1)
891 m->m_epg_last_len = siz;
897 if (pgno == m->m_epg_npgs && left > 0) {
900 panic("nfsvno_createiovec_extpgs iov");
910 * Read vnode op call into mbuf list.
913 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
914 int maxextsiz, struct thread *p, struct mbuf **mpp,
915 struct mbuf **mpendp)
919 int error = 0, len, tlen, ioflag = 0;
921 struct uio io, *uiop = &io;
925 * Attempt to read from a DS file. A return of ENOENT implies
926 * there is no DS file to read.
928 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
929 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
933 len = NFSM_RNDUP(cnt);
935 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
938 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
940 uiop->uio_offset = off;
941 uiop->uio_resid = len;
942 uiop->uio_rw = UIO_READ;
943 uiop->uio_segflg = UIO_SYSSPACE;
945 nh = nfsrv_sequential_heuristic(uiop, vp);
946 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
947 /* XXX KDM make this more systematic? */
948 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
949 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
956 nh->nh_nextoff = uiop->uio_offset;
957 tlen = len - uiop->uio_resid;
958 cnt = cnt < tlen ? cnt : tlen;
959 tlen = NFSM_RNDUP(cnt);
963 } else if (len != tlen || tlen != cnt)
964 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
974 * Create the iovec for the mbuf chain passed in as an argument.
975 * The "cp" argument is where the data starts within the first mbuf in
976 * the chain. It returns the iovec and the iovcnt.
979 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
987 * Loop through the mbuf chain, counting how many mbufs are a
988 * part of this write operation, so the iovec size is known.
993 i = mtod(mp, caddr_t) + mp->m_len - cp;
1007 /* Now, create the iovec. */
1009 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1012 i = mtod(mp, caddr_t) + mp->m_len - cp;
1016 panic("nfsrv_createiovecw");
1027 cp = mtod(mp, caddr_t);
1034 * Write vnode op from an mbuf list.
1037 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1038 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1041 int cnt, ioflags, error;
1042 struct uio io, *uiop = &io;
1046 * Attempt to write to a DS file. A return of ENOENT implies
1047 * there is no DS file to write.
1049 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1050 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1051 if (error != ENOENT) {
1052 *stable = NFSWRITE_FILESYNC;
1057 if (*stable == NFSWRITE_UNSTABLE)
1058 ioflags = IO_NODELOCKED;
1060 ioflags = (IO_SYNC | IO_NODELOCKED);
1061 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1065 uiop->uio_iovcnt = cnt;
1066 uiop->uio_resid = retlen;
1067 uiop->uio_rw = UIO_WRITE;
1068 uiop->uio_segflg = UIO_SYSSPACE;
1069 NFSUIOPROC(uiop, p);
1070 uiop->uio_offset = off;
1071 nh = nfsrv_sequential_heuristic(uiop, vp);
1072 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1073 /* XXX KDM make this more systematic? */
1074 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1075 error = VOP_WRITE(vp, uiop, ioflags, cred);
1077 nh->nh_nextoff = uiop->uio_offset;
1085 * Common code for creating a regular file (plus special files for V2).
1088 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1089 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1090 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1094 struct thread *p = curthread;
1096 error = nd->nd_repstat;
1097 if (!error && ndp->ni_vp == NULL) {
1098 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1099 vrele(ndp->ni_startdir);
1100 error = VOP_CREATE(ndp->ni_dvp,
1101 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1102 /* For a pNFS server, create the data file on a DS. */
1103 if (error == 0 && nvap->na_type == VREG) {
1105 * Create a data file on a DS for a pNFS server.
1106 * This function just returns if not
1107 * running a pNFS DS or the creation fails.
1109 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1113 nfsvno_relpathbuf(ndp);
1115 if (*exclusive_flagp) {
1116 *exclusive_flagp = 0;
1117 NFSVNO_ATTRINIT(nvap);
1118 nvap->na_atime.tv_sec = cverf[0];
1119 nvap->na_atime.tv_nsec = cverf[1];
1120 error = VOP_SETATTR(ndp->ni_vp,
1121 &nvap->na_vattr, nd->nd_cred);
1125 error = NFSERR_NOTSUPP;
1130 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1131 * (This implies, just get out on an error.)
1133 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1134 nvap->na_type == VFIFO) {
1135 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1136 nvap->na_type = VFIFO;
1137 if (nvap->na_type != VFIFO &&
1138 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1139 vrele(ndp->ni_startdir);
1140 nfsvno_relpathbuf(ndp);
1144 nvap->na_rdev = rdev;
1145 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1146 &ndp->ni_cnd, &nvap->na_vattr);
1148 nfsvno_relpathbuf(ndp);
1149 vrele(ndp->ni_startdir);
1153 vrele(ndp->ni_startdir);
1154 nfsvno_relpathbuf(ndp);
1162 * Handle cases where error is already set and/or
1164 * 1 - clean up the lookup
1165 * 2 - iff !error and na_size set, truncate it
1167 vrele(ndp->ni_startdir);
1168 nfsvno_relpathbuf(ndp);
1170 if (ndp->ni_dvp == *vpp)
1174 if (!error && nvap->na_size != VNOVAL) {
1175 error = nfsvno_accchk(*vpp, VWRITE,
1176 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1177 NFSACCCHK_VPISLOCKED, NULL);
1179 tempsize = nvap->na_size;
1180 NFSVNO_ATTRINIT(nvap);
1181 nvap->na_size = tempsize;
1182 error = VOP_SETATTR(*vpp,
1183 &nvap->na_vattr, nd->nd_cred);
1196 * Do a mknod vnode op.
1199 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1205 vtyp = nvap->na_type;
1207 * Iff doesn't exist, create it.
1210 vrele(ndp->ni_startdir);
1211 nfsvno_relpathbuf(ndp);
1217 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1218 vrele(ndp->ni_startdir);
1219 nfsvno_relpathbuf(ndp);
1221 error = NFSERR_BADTYPE;
1224 if (vtyp == VSOCK) {
1225 vrele(ndp->ni_startdir);
1226 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1227 &ndp->ni_cnd, &nvap->na_vattr);
1229 nfsvno_relpathbuf(ndp);
1231 if (nvap->na_type != VFIFO &&
1232 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1233 vrele(ndp->ni_startdir);
1234 nfsvno_relpathbuf(ndp);
1238 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1239 &ndp->ni_cnd, &nvap->na_vattr);
1241 nfsvno_relpathbuf(ndp);
1242 vrele(ndp->ni_startdir);
1244 * Since VOP_MKNOD returns the ni_vp, I can't
1245 * see any reason to do the lookup.
1258 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1259 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1263 if (ndp->ni_vp != NULL) {
1264 if (ndp->ni_dvp == ndp->ni_vp)
1269 nfsvno_relpathbuf(ndp);
1273 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1276 nfsvno_relpathbuf(ndp);
1287 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1288 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1289 struct nfsexstuff *exp)
1294 vrele(ndp->ni_startdir);
1295 nfsvno_relpathbuf(ndp);
1296 if (ndp->ni_dvp == ndp->ni_vp)
1305 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1306 &nvap->na_vattr, pathcp);
1308 vrele(ndp->ni_startdir);
1309 nfsvno_relpathbuf(ndp);
1311 * Although FreeBSD still had the lookup code in
1312 * it for 7/current, there doesn't seem to be any
1313 * point, since VOP_SYMLINK() returns the ni_vp.
1314 * Just vput it for v2.
1316 if (!not_v2 && !error)
1325 * Parse symbolic link arguments.
1326 * This function has an ugly side effect. It will malloc() an area for
1327 * the symlink and set iov_base to point to it, only if it succeeds.
1328 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1332 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1333 struct thread *p, char **pathcpp, int *lenp)
1336 char *pathcp = NULL;
1338 struct nfsv2_sattr *sp;
1342 if ((nd->nd_flag & ND_NFSV3) &&
1343 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1345 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1346 len = fxdr_unsigned(int, *tl);
1347 if (len > NFS_MAXPATHLEN || len <= 0) {
1351 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1352 error = nfsrv_mtostr(nd, pathcp, len);
1355 if (nd->nd_flag & ND_NFSV2) {
1356 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1357 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1361 NFSEXITCODE2(0, nd);
1365 free(pathcp, M_TEMP);
1366 NFSEXITCODE2(error, nd);
1371 * Remove a non-directory object.
1374 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1375 struct thread *p, struct nfsexstuff *exp)
1377 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1378 int error = 0, mirrorcnt;
1379 char fname[PNFS_FILENAME_LEN + 1];
1384 if (vp->v_type == VDIR)
1385 error = NFSERR_ISDIR;
1387 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1390 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1392 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1393 if (error == 0 && dsdvp[0] != NULL)
1394 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1395 if (ndp->ni_dvp == vp)
1400 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1401 nfsvno_relpathbuf(ndp);
1407 * Remove a directory.
1410 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1411 struct thread *p, struct nfsexstuff *exp)
1417 if (vp->v_type != VDIR) {
1422 * No rmdir "." please.
1424 if (ndp->ni_dvp == vp) {
1429 * The root of a mounted filesystem cannot be deleted.
1431 if (vp->v_vflag & VV_ROOT)
1435 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1436 if (ndp->ni_dvp == vp)
1441 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1442 nfsvno_relpathbuf(ndp);
1451 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1452 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1454 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1455 int error = 0, mirrorcnt;
1456 char fname[PNFS_FILENAME_LEN + 1];
1460 fvp = fromndp->ni_vp;
1462 vrele(fromndp->ni_dvp);
1467 tdvp = tondp->ni_dvp;
1470 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1471 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1473 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1474 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1477 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1478 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1483 * A rename to '.' or '..' results in a prematurely
1484 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1487 if ((tondp->ni_cnd.cn_namelen == 1 &&
1488 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1489 (tondp->ni_cnd.cn_namelen == 2 &&
1490 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1491 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1496 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1497 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1500 if (fvp->v_mount != tdvp->v_mount) {
1501 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1505 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1510 * If source and destination are the same, there is nothing to
1511 * do. Set error to -1 to indicate this.
1516 if (ndflag & ND_NFSV4) {
1517 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1518 error = nfsrv_checkremove(fvp, 0, NULL,
1519 (nfsquad_t)((u_quad_t)0), p);
1524 error = nfsrv_checkremove(tvp, 1, NULL,
1525 (nfsquad_t)((u_quad_t)0), p);
1528 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1529 * that the NFSv4 client won't be confused by the rename.
1530 * Since nfsd_recalldelegation() can only be called on an
1531 * unlocked vnode at this point and fvp is the file that will
1532 * still exist after the rename, just do fvp.
1534 nfsd_recalldelegation(fvp, p);
1536 if (error == 0 && tvp != NULL) {
1537 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1538 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1539 " dsdvp=%p\n", dsdvp[0]);
1543 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1544 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1553 vrele(fromndp->ni_dvp);
1560 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1561 * if the rename succeeded, the DS file for the tvp needs to be
1564 if (error == 0 && dsdvp[0] != NULL) {
1565 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1566 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1569 vrele(tondp->ni_startdir);
1570 nfsvno_relpathbuf(tondp);
1572 vrele(fromndp->ni_startdir);
1573 nfsvno_relpathbuf(fromndp);
1582 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1583 struct thread *p, struct nfsexstuff *exp)
1593 if (vp->v_mount != xp->v_mount)
1597 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1598 if (!VN_IS_DOOMED(vp))
1599 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1602 if (ndp->ni_dvp == vp)
1608 if (ndp->ni_dvp == ndp->ni_vp)
1615 nfsvno_relpathbuf(ndp);
1621 * Do the fsync() appropriate for the commit.
1624 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1630 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1631 * file is done. At this time VOP_FSYNC does not accept offset and
1632 * byte count parameters so call VOP_FSYNC the whole file for now.
1633 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1634 * File systems that do not use the buffer cache (as indicated
1635 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1637 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1638 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1640 * Give up and do the whole thing
1642 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1643 VM_OBJECT_WLOCK(vp->v_object);
1644 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1645 VM_OBJECT_WUNLOCK(vp->v_object);
1647 error = VOP_FSYNC(vp, MNT_WAIT, td);
1650 * Locate and synchronously write any buffers that fall
1651 * into the requested range. Note: we are assuming that
1652 * f_iosize is a power of 2.
1654 int iosize = vp->v_mount->mnt_stat.f_iosize;
1655 int iomask = iosize - 1;
1660 * Align to iosize boundary, super-align to page boundary.
1663 cnt += off & iomask;
1664 off &= ~(u_quad_t)iomask;
1666 if (off & PAGE_MASK) {
1667 cnt += off & PAGE_MASK;
1668 off &= ~(u_quad_t)PAGE_MASK;
1670 lblkno = off / iosize;
1672 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1673 VM_OBJECT_WLOCK(vp->v_object);
1674 vm_object_page_clean(vp->v_object, off, off + cnt,
1676 VM_OBJECT_WUNLOCK(vp->v_object);
1685 * If we have a buffer and it is marked B_DELWRI we
1686 * have to lock and write it. Otherwise the prior
1687 * write is assumed to have already been committed.
1689 * gbincore() can return invalid buffers now so we
1690 * have to check that bit as well (though B_DELWRI
1691 * should not be set if B_INVAL is set there could be
1692 * a race here since we haven't locked the buffer).
1694 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1695 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1696 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1698 continue; /* retry */
1700 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1703 bp->b_flags &= ~B_ASYNC;
1726 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1732 if (nfsrv_devidcnt > 0) {
1733 /* For a pNFS service, get the DS numbers. */
1734 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1735 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1741 error = VFS_STATFS(vp->v_mount, sf);
1744 sf->f_blocks = tsf->f_blocks;
1745 sf->f_bavail = tsf->f_bavail;
1746 sf->f_bfree = tsf->f_bfree;
1747 sf->f_bsize = tsf->f_bsize;
1750 * Since NFS handles these values as unsigned on the
1751 * wire, there is no way to represent negative values,
1752 * so set them to 0. Without this, they will appear
1753 * to be very large positive values for clients like
1756 if (sf->f_bavail < 0)
1758 if (sf->f_ffree < 0)
1767 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1768 * must handle nfsrv_opencheck() calls after any other access checks.
1771 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1772 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1773 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1774 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred,
1775 struct nfsexstuff *exp, struct vnode **vpp)
1777 struct vnode *vp = NULL;
1779 struct nfsexstuff nes;
1780 struct thread *p = curthread;
1782 if (ndp->ni_vp == NULL)
1783 nd->nd_repstat = nfsrv_opencheck(clientid,
1784 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1785 if (!nd->nd_repstat) {
1786 if (ndp->ni_vp == NULL) {
1787 vrele(ndp->ni_startdir);
1788 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1789 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1790 /* For a pNFS server, create the data file on a DS. */
1791 if (nd->nd_repstat == 0) {
1793 * Create a data file on a DS for a pNFS server.
1794 * This function just returns if not
1795 * running a pNFS DS or the creation fails.
1797 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1801 nfsvno_relpathbuf(ndp);
1802 if (!nd->nd_repstat) {
1803 if (*exclusive_flagp) {
1804 *exclusive_flagp = 0;
1805 NFSVNO_ATTRINIT(nvap);
1806 nvap->na_atime.tv_sec = cverf[0];
1807 nvap->na_atime.tv_nsec = cverf[1];
1808 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1809 &nvap->na_vattr, cred);
1810 if (nd->nd_repstat != 0) {
1813 nd->nd_repstat = NFSERR_NOTSUPP;
1815 NFSSETBIT_ATTRBIT(attrbitp,
1816 NFSATTRBIT_TIMEACCESS);
1818 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1819 aclp, p, attrbitp, exp);
1824 if (ndp->ni_startdir)
1825 vrele(ndp->ni_startdir);
1826 nfsvno_relpathbuf(ndp);
1828 if (create == NFSV4OPEN_CREATE) {
1829 if (ndp->ni_dvp == vp)
1834 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1835 if (ndp->ni_cnd.cn_flags & RDONLY)
1836 NFSVNO_SETEXRDONLY(&nes);
1838 NFSVNO_EXINIT(&nes);
1839 nd->nd_repstat = nfsvno_accchk(vp,
1840 VWRITE, cred, &nes, p,
1841 NFSACCCHK_NOOVERRIDE,
1842 NFSACCCHK_VPISLOCKED, NULL);
1843 nd->nd_repstat = nfsrv_opencheck(clientid,
1844 stateidp, stp, vp, nd, p, nd->nd_repstat);
1845 if (!nd->nd_repstat) {
1846 tempsize = nvap->na_size;
1847 NFSVNO_ATTRINIT(nvap);
1848 nvap->na_size = tempsize;
1849 nd->nd_repstat = VOP_SETATTR(vp,
1850 &nvap->na_vattr, cred);
1852 } else if (vp->v_type == VREG) {
1853 nd->nd_repstat = nfsrv_opencheck(clientid,
1854 stateidp, stp, vp, nd, p, nd->nd_repstat);
1858 if (ndp->ni_cnd.cn_flags & HASBUF)
1859 nfsvno_relpathbuf(ndp);
1860 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1861 vrele(ndp->ni_startdir);
1862 if (ndp->ni_dvp == ndp->ni_vp)
1872 NFSEXITCODE2(0, nd);
1876 * Updates the file rev and sets the mtime and ctime
1877 * to the current clock time, returning the va_filerev and va_Xtime
1879 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1882 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1883 struct nfsrv_descript *nd, struct thread *p)
1888 vfs_timestamp(&va.va_mtime);
1889 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1890 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1891 if (VN_IS_DOOMED(vp))
1894 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1895 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1900 * Glue routine to nfsv4_fillattr().
1903 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1904 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1905 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1906 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1912 if (nfsrv_devidcnt > 0 &&
1913 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1914 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1915 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1916 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1917 error = nfsrv_pnfsstatfs(sf, mp);
1923 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1924 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1925 mounted_on_fileno, sf);
1927 NFSEXITCODE2(0, nd);
1931 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1933 * nfs readdir service
1934 * - mallocs what it thinks is enough to read
1935 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1936 * - calls VOP_READDIR()
1937 * - loops around building the reply
1938 * if the output generated exceeds count break out of loop
1939 * The NFSM_CLGET macro is used here so that the reply will be packed
1940 * tightly in mbuf clusters.
1941 * - it trims out records with d_fileno == 0
1942 * this doesn't matter for Unix clients, but they might confuse clients
1944 * - it trims out records with d_type == DT_WHT
1945 * these cannot be seen through NFS (unless we extend the protocol)
1946 * The alternate call nfsrvd_readdirplus() does lookups as well.
1947 * PS: The NFS protocol spec. does not clarify what the "count" byte
1948 * argument is a count of.. just name strings and file id's or the
1949 * entire reply rpc or ...
1950 * I tried just file name and id sizes and it confused the Sun client,
1951 * so I am using the full rpc size now. The "paranoia.." comment refers
1952 * to including the status longwords that are not a part of the dir.
1953 * "entry" structures, but are in the rpc.
1956 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1957 struct vnode *vp, struct nfsexstuff *exp)
1962 char *cpos, *cend, *rbuf;
1964 int nlen, error = 0, getret = 1;
1965 int siz, cnt, fullsiz, eofflag, ncookies;
1966 u_int64_t off, toff, verf __unused;
1967 u_long *cookies = NULL, *cookiep;
1971 struct thread *p = curthread;
1973 if (nd->nd_repstat) {
1974 nfsrv_postopattr(nd, getret, &at);
1977 if (nd->nd_flag & ND_NFSV2) {
1978 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1979 off = fxdr_unsigned(u_quad_t, *tl++);
1981 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1982 off = fxdr_hyper(tl);
1984 verf = fxdr_hyper(tl);
1988 cnt = fxdr_unsigned(int, *tl);
1989 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1990 cnt = NFS_SRVMAXDATA(nd);
1991 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1993 if (nd->nd_flag & ND_NFSV3) {
1994 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
1998 * va_filerev is not sufficient as a cookie verifier,
1999 * since it is not supposed to change when entries are
2000 * removed/added unless that offset cookies returned to
2001 * the client are no longer valid.
2003 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2004 nd->nd_repstat = NFSERR_BAD_COOKIE;
2007 if (!nd->nd_repstat && vp->v_type != VDIR)
2008 nd->nd_repstat = NFSERR_NOTDIR;
2009 if (nd->nd_repstat == 0 && cnt == 0) {
2010 if (nd->nd_flag & ND_NFSV2)
2011 /* NFSv2 does not have NFSERR_TOOSMALL */
2012 nd->nd_repstat = EPERM;
2014 nd->nd_repstat = NFSERR_TOOSMALL;
2016 if (!nd->nd_repstat)
2017 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2018 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2019 NFSACCCHK_VPISLOCKED, NULL);
2020 if (nd->nd_repstat) {
2022 if (nd->nd_flag & ND_NFSV3)
2023 nfsrv_postopattr(nd, getret, &at);
2026 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2027 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2031 free(cookies, M_TEMP);
2039 io.uio_offset = (off_t)off;
2041 io.uio_segflg = UIO_SYSSPACE;
2042 io.uio_rw = UIO_READ;
2044 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2046 off = (u_int64_t)io.uio_offset;
2048 siz -= io.uio_resid;
2050 if (!cookies && !nd->nd_repstat)
2051 nd->nd_repstat = NFSERR_PERM;
2052 if (nd->nd_flag & ND_NFSV3) {
2053 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2054 if (!nd->nd_repstat)
2055 nd->nd_repstat = getret;
2059 * Handles the failed cases. nd->nd_repstat == 0 past here.
2061 if (nd->nd_repstat) {
2065 free(cookies, M_TEMP);
2066 if (nd->nd_flag & ND_NFSV3)
2067 nfsrv_postopattr(nd, getret, &at);
2071 * If nothing read, return eof
2076 if (nd->nd_flag & ND_NFSV2) {
2077 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2079 nfsrv_postopattr(nd, getret, &at);
2080 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2081 txdr_hyper(at.na_filerev, tl);
2084 *tl++ = newnfs_false;
2087 free(cookies, M_TEMP);
2092 * Check for degenerate cases of nothing useful read.
2093 * If so go try again
2097 dp = (struct dirent *)cpos;
2101 * For some reason FreeBSD's ufs_readdir() chooses to back the
2102 * directory offset up to a block boundary, so it is necessary to
2103 * skip over the records that precede the requested offset. This
2104 * requires the assumption that file offset cookies monotonically
2107 while (cpos < cend && ncookies > 0 &&
2108 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2109 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2110 cpos += dp->d_reclen;
2111 dp = (struct dirent *)cpos;
2115 if (cpos >= cend || ncookies == 0) {
2123 * If cnt > MCLBYTES and the reply will not be saved, use
2124 * ext_pgs mbufs for TLS.
2125 * For NFSv4.0, we do not know for sure if the reply will
2126 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2128 if (cnt > MCLBYTES && siz > MCLBYTES &&
2129 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2130 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2131 nd->nd_flag |= ND_EXTPG;
2134 * dirlen is the size of the reply, including all XDR and must
2135 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2136 * if the XDR should be included in "count", but to be safe, we do.
2137 * (Include the two booleans at the end of the reply in dirlen now.)
2139 if (nd->nd_flag & ND_NFSV3) {
2140 nfsrv_postopattr(nd, getret, &at);
2141 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2142 txdr_hyper(at.na_filerev, tl);
2143 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2145 dirlen = 2 * NFSX_UNSIGNED;
2148 /* Loop through the records and build reply */
2149 while (cpos < cend && ncookies > 0) {
2150 nlen = dp->d_namlen;
2151 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2152 nlen <= NFS_MAXNAMLEN) {
2153 if (nd->nd_flag & ND_NFSV3)
2154 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2156 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2163 * Build the directory record xdr from
2166 if (nd->nd_flag & ND_NFSV3) {
2167 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2168 *tl++ = newnfs_true;
2171 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2172 *tl++ = newnfs_true;
2174 *tl = txdr_unsigned(dp->d_fileno);
2175 (void) nfsm_strtom(nd, dp->d_name, nlen);
2176 if (nd->nd_flag & ND_NFSV3) {
2177 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2180 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2181 *tl = txdr_unsigned(*cookiep);
2183 cpos += dp->d_reclen;
2184 dp = (struct dirent *)cpos;
2190 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2191 *tl++ = newnfs_false;
2197 free(cookies, M_TEMP);
2200 NFSEXITCODE2(0, nd);
2204 NFSEXITCODE2(error, nd);
2209 * Readdirplus for V3 and Readdir for V4.
2212 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2213 struct vnode *vp, struct nfsexstuff *exp)
2218 char *cpos, *cend, *rbuf;
2221 struct nfsvattr nva, at, *nvap = &nva;
2222 struct mbuf *mb0, *mb1;
2223 struct nfsreferral *refp;
2224 int nlen, r, error = 0, getret = 1, usevget = 1;
2225 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2226 caddr_t bpos0, bpos1;
2227 u_int64_t off, toff, verf;
2228 u_long *cookies = NULL, *cookiep;
2229 nfsattrbit_t attrbits, rderrbits, savbits;
2232 struct componentname cn;
2233 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2234 struct mount *mp, *new_mp;
2235 uint64_t mounted_on_fileno;
2236 struct thread *p = curthread;
2237 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2239 if (nd->nd_repstat) {
2240 nfsrv_postopattr(nd, getret, &at);
2243 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2244 off = fxdr_hyper(tl);
2247 verf = fxdr_hyper(tl);
2249 siz = fxdr_unsigned(int, *tl++);
2250 cnt = fxdr_unsigned(int, *tl);
2253 * Use the server's maximum data transfer size as the upper bound
2256 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2257 cnt = NFS_SRVMAXDATA(nd);
2260 * siz is a "hint" of how much directory information (name, fileid,
2261 * cookie) should be in the reply. At least one client "hints" 0,
2262 * so I set it to cnt for that case. I also round it up to the
2263 * next multiple of DIRBLKSIZ.
2264 * Since the size of a Readdirplus directory entry reply will always
2265 * be greater than a directory entry returned by VOP_READDIR(), it
2266 * does not make sense to read more than NFS_SRVMAXDATA() via
2271 else if (siz > NFS_SRVMAXDATA(nd))
2272 siz = NFS_SRVMAXDATA(nd);
2273 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2275 if (nd->nd_flag & ND_NFSV4) {
2276 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2279 NFSSET_ATTRBIT(&savbits, &attrbits);
2280 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2281 NFSZERO_ATTRBIT(&rderrbits);
2282 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2284 NFSZERO_ATTRBIT(&attrbits);
2287 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2289 if (!nd->nd_repstat) {
2290 if (off && verf != at.na_filerev) {
2292 * va_filerev is not sufficient as a cookie verifier,
2293 * since it is not supposed to change when entries are
2294 * removed/added unless that offset cookies returned to
2295 * the client are no longer valid.
2297 if (nd->nd_flag & ND_NFSV4) {
2298 nd->nd_repstat = NFSERR_NOTSAME;
2300 nd->nd_repstat = NFSERR_BAD_COOKIE;
2305 if (!nd->nd_repstat && vp->v_type != VDIR)
2306 nd->nd_repstat = NFSERR_NOTDIR;
2307 if (!nd->nd_repstat && cnt == 0)
2308 nd->nd_repstat = NFSERR_TOOSMALL;
2309 if (!nd->nd_repstat)
2310 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2311 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2312 NFSACCCHK_VPISLOCKED, NULL);
2313 if (nd->nd_repstat) {
2315 if (nd->nd_flag & ND_NFSV3)
2316 nfsrv_postopattr(nd, getret, &at);
2319 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2320 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2322 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2326 free(cookies, M_TEMP);
2334 io.uio_offset = (off_t)off;
2336 io.uio_segflg = UIO_SYSSPACE;
2337 io.uio_rw = UIO_READ;
2339 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2341 off = (u_int64_t)io.uio_offset;
2343 siz -= io.uio_resid;
2345 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2347 if (!cookies && !nd->nd_repstat)
2348 nd->nd_repstat = NFSERR_PERM;
2349 if (!nd->nd_repstat)
2350 nd->nd_repstat = getret;
2351 if (nd->nd_repstat) {
2354 free(cookies, M_TEMP);
2356 if (nd->nd_flag & ND_NFSV3)
2357 nfsrv_postopattr(nd, getret, &at);
2361 * If nothing read, return eof
2366 if (nd->nd_flag & ND_NFSV3)
2367 nfsrv_postopattr(nd, getret, &at);
2368 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2369 txdr_hyper(at.na_filerev, tl);
2371 *tl++ = newnfs_false;
2373 free(cookies, M_TEMP);
2379 * Check for degenerate cases of nothing useful read.
2380 * If so go try again
2384 dp = (struct dirent *)cpos;
2388 * For some reason FreeBSD's ufs_readdir() chooses to back the
2389 * directory offset up to a block boundary, so it is necessary to
2390 * skip over the records that precede the requested offset. This
2391 * requires the assumption that file offset cookies monotonically
2394 while (cpos < cend && ncookies > 0 &&
2395 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2396 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2397 ((nd->nd_flag & ND_NFSV4) &&
2398 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2399 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2400 cpos += dp->d_reclen;
2401 dp = (struct dirent *)cpos;
2405 if (cpos >= cend || ncookies == 0) {
2412 * Busy the file system so that the mount point won't go away
2413 * and, as such, VFS_VGET() can be used safely.
2418 nd->nd_repstat = vfs_busy(mp, 0);
2420 if (nd->nd_repstat != 0) {
2422 free(cookies, M_TEMP);
2424 if (nd->nd_flag & ND_NFSV3)
2425 nfsrv_postopattr(nd, getret, &at);
2430 * Check to see if entries in this directory can be safely acquired
2431 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2432 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2433 * automount of the snapshot directory that is required will
2435 * This needs to be done here for NFSv4, since NFSv4 never does
2436 * a VFS_VGET() for "." or "..".
2439 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2440 if (r == EOPNOTSUPP) {
2442 cn.cn_nameiop = LOOKUP;
2443 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2444 cn.cn_cred = nd->nd_cred;
2451 * If the reply is likely to exceed MCLBYTES and the reply will
2452 * not be saved, use ext_pgs mbufs for TLS.
2453 * It is difficult to predict how large each entry will be and
2454 * how many entries have been read, so just assume the directory
2455 * entries grow by a factor of 4 when attributes are included.
2456 * For NFSv4.0, we do not know for sure if the reply will
2457 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2459 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2460 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2461 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2462 nd->nd_flag |= ND_EXTPG;
2465 * Save this position, in case there is an error before one entry
2469 bpos0 = nd->nd_bpos;
2470 bextpg0 = nd->nd_bextpg;
2471 bextpgsiz0 = nd->nd_bextpgsiz;
2474 * Fill in the first part of the reply.
2475 * dirlen is the reply length in bytes and cannot exceed cnt.
2476 * (Include the two booleans at the end of the reply in dirlen now,
2477 * so we recognize when we have exceeded cnt.)
2479 if (nd->nd_flag & ND_NFSV3) {
2480 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2481 nfsrv_postopattr(nd, getret, &at);
2483 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2485 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2486 txdr_hyper(at.na_filerev, tl);
2489 * Save this position, in case there is an empty reply needed.
2492 bpos1 = nd->nd_bpos;
2493 bextpg1 = nd->nd_bextpg;
2494 bextpgsiz1 = nd->nd_bextpgsiz;
2496 /* Loop through the records and build reply */
2498 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2499 nlen = dp->d_namlen;
2500 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2501 nlen <= NFS_MAXNAMLEN &&
2502 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2503 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2504 || (nlen == 1 && dp->d_name[0] != '.'))) {
2506 * Save the current position in the reply, in case
2507 * this entry exceeds cnt.
2510 bpos1 = nd->nd_bpos;
2511 bextpg1 = nd->nd_bextpg;
2512 bextpgsiz1 = nd->nd_bextpgsiz;
2515 * For readdir_and_lookup get the vnode using
2524 mounted_on_fileno = (uint64_t)dp->d_fileno;
2525 if ((nd->nd_flag & ND_NFSV3) ||
2526 NFSNONZERO_ATTRBIT(&savbits)) {
2527 if (nd->nd_flag & ND_NFSV4)
2528 refp = nfsv4root_getreferral(NULL,
2532 r = VFS_VGET(mp, dp->d_fileno,
2536 if (r == EOPNOTSUPP) {
2539 cn.cn_nameiop = LOOKUP;
2547 cn.cn_nameptr = dp->d_name;
2548 cn.cn_namelen = nlen;
2549 cn.cn_flags = ISLASTCN |
2550 NOFOLLOW | LOCKLEAF;
2552 dp->d_name[0] == '.' &&
2553 dp->d_name[1] == '.')
2556 if (NFSVOPLOCK(vp, LK_SHARED)
2558 nd->nd_repstat = EPERM;
2561 if ((vp->v_vflag & VV_ROOT) != 0
2562 && (cn.cn_flags & ISDOTDOT)
2568 r = VOP_LOOKUP(vp, &nvp,
2576 * For NFSv4, check to see if nvp is
2577 * a mount point and get the mount
2578 * point vnode, as required.
2581 nfsrv_enable_crossmntpt != 0 &&
2582 (nd->nd_flag & ND_NFSV4) != 0 &&
2583 nvp->v_type == VDIR &&
2584 nvp->v_mountedhere != NULL) {
2585 new_mp = nvp->v_mountedhere;
2586 r = vfs_busy(new_mp, 0);
2590 r = VFS_ROOT(new_mp,
2600 * If we failed to look up the entry, then it
2601 * has become invalid, most likely removed.
2608 KASSERT(refp != NULL || nvp != NULL,
2609 ("%s: undetected lookup error", __func__));
2612 ((nd->nd_flag & ND_NFSV3) ||
2613 NFSNONZERO_ATTRBIT(&attrbits))) {
2614 r = nfsvno_getfh(nvp, &nfh, p);
2616 r = nfsvno_getattr(nvp, nvap, nd, p,
2618 if (r == 0 && is_zfs == 1 &&
2619 nfsrv_enable_crossmntpt != 0 &&
2620 (nd->nd_flag & ND_NFSV4) != 0 &&
2621 nvp->v_type == VDIR &&
2622 vp->v_mount != nvp->v_mount) {
2624 * For a ZFS snapshot, there is a
2625 * pseudo mount that does not set
2626 * v_mountedhere, so it needs to
2627 * be detected via a different
2632 new_mp = nvp->v_mount;
2637 * If we failed to get attributes of the entry,
2638 * then just skip it for NFSv3 (the traditional
2639 * behavior in the old NFS server).
2640 * For NFSv4 the behavior is controlled by
2641 * RDATTRERROR: we either ignore the error or
2643 * Note that RDATTRERROR is never set for NFSv3.
2646 if (!NFSISSET_ATTRBIT(&attrbits,
2647 NFSATTRBIT_RDATTRERROR)) {
2649 if (needs_unbusy != 0)
2651 if ((nd->nd_flag & ND_NFSV3))
2660 * Build the directory record xdr
2662 if (nd->nd_flag & ND_NFSV3) {
2663 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2664 *tl++ = newnfs_true;
2666 *tl = txdr_unsigned(dp->d_fileno);
2667 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2668 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2670 *tl = txdr_unsigned(*cookiep);
2671 nfsrv_postopattr(nd, 0, nvap);
2672 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2673 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2677 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2678 *tl++ = newnfs_true;
2680 *tl = txdr_unsigned(*cookiep);
2681 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2683 supports_nfsv4acls =
2684 nfs_supportsnfsv4acls(nvp);
2687 supports_nfsv4acls = 0;
2689 dirlen += nfsrv_putreferralattr(nd,
2692 if (nd->nd_repstat) {
2695 if (needs_unbusy != 0)
2700 dirlen += nfsvno_fillattr(nd, new_mp,
2701 nvp, nvap, &nfh, r, &rderrbits,
2702 nd->nd_cred, p, isdgram, 0,
2703 supports_nfsv4acls, at_root,
2706 dirlen += nfsvno_fillattr(nd, new_mp,
2707 nvp, nvap, &nfh, r, &attrbits,
2708 nd->nd_cred, p, isdgram, 0,
2709 supports_nfsv4acls, at_root,
2714 dirlen += (3 * NFSX_UNSIGNED);
2716 if (needs_unbusy != 0)
2722 cpos += dp->d_reclen;
2723 dp = (struct dirent *)cpos;
2731 * If dirlen > cnt, we must strip off the last entry. If that
2732 * results in an empty reply, report NFSERR_TOOSMALL.
2734 if (dirlen > cnt || nd->nd_repstat) {
2735 if (!nd->nd_repstat && entrycnt == 0)
2736 nd->nd_repstat = NFSERR_TOOSMALL;
2737 if (nd->nd_repstat) {
2738 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2739 if (nd->nd_flag & ND_NFSV3)
2740 nfsrv_postopattr(nd, getret, &at);
2742 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2744 } else if (cpos < cend)
2746 if (!nd->nd_repstat) {
2747 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2748 *tl++ = newnfs_false;
2754 free(cookies, M_TEMP);
2758 NFSEXITCODE2(0, nd);
2762 NFSEXITCODE2(error, nd);
2767 * Get the settable attributes out of the mbuf list.
2768 * (Return 0 or EBADRPC)
2771 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2772 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2775 struct nfsv2_sattr *sp;
2776 int error = 0, toclient = 0;
2778 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2780 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2782 * Some old clients didn't fill in the high order 16bits.
2783 * --> check the low order 2 bytes for 0xffff
2785 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2786 nvap->na_mode = nfstov_mode(sp->sa_mode);
2787 if (sp->sa_uid != newnfs_xdrneg1)
2788 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2789 if (sp->sa_gid != newnfs_xdrneg1)
2790 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2791 if (sp->sa_size != newnfs_xdrneg1)
2792 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2793 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2795 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2797 nvap->na_atime.tv_sec =
2798 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2799 nvap->na_atime.tv_nsec = 0;
2802 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2803 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2806 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2807 if (*tl == newnfs_true) {
2808 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2809 nvap->na_mode = nfstov_mode(*tl);
2811 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2812 if (*tl == newnfs_true) {
2813 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2814 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2816 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2817 if (*tl == newnfs_true) {
2818 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2819 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2821 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2822 if (*tl == newnfs_true) {
2823 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2824 nvap->na_size = fxdr_hyper(tl);
2826 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2827 switch (fxdr_unsigned(int, *tl)) {
2828 case NFSV3SATTRTIME_TOCLIENT:
2829 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2830 fxdr_nfsv3time(tl, &nvap->na_atime);
2833 case NFSV3SATTRTIME_TOSERVER:
2834 vfs_timestamp(&nvap->na_atime);
2835 nvap->na_vaflags |= VA_UTIMES_NULL;
2838 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2839 switch (fxdr_unsigned(int, *tl)) {
2840 case NFSV3SATTRTIME_TOCLIENT:
2841 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2842 fxdr_nfsv3time(tl, &nvap->na_mtime);
2843 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2845 case NFSV3SATTRTIME_TOSERVER:
2846 vfs_timestamp(&nvap->na_mtime);
2848 nvap->na_vaflags |= VA_UTIMES_NULL;
2853 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2856 NFSEXITCODE2(error, nd);
2861 * Handle the setable attributes for V4.
2862 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2865 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2866 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2871 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2872 int moderet, toclient = 0;
2873 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2876 u_short mode, mask; /* Same type as va_mode. */
2879 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2882 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2883 attrsize = fxdr_unsigned(int, *tl);
2886 * Loop around getting the setable attributes. If an unsupported
2887 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2890 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2891 bitpos = NFSATTRBIT_MAX;
2896 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2897 if (attrsum > attrsize) {
2898 error = NFSERR_BADXDR;
2901 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2903 case NFSATTRBIT_SIZE:
2904 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2905 if (vp != NULL && vp->v_type != VREG) {
2906 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2910 nvap->na_size = fxdr_hyper(tl);
2911 attrsum += NFSX_HYPER;
2913 case NFSATTRBIT_ACL:
2914 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2918 if (aceerr && !nd->nd_repstat)
2919 nd->nd_repstat = aceerr;
2922 case NFSATTRBIT_ARCHIVE:
2923 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2924 if (!nd->nd_repstat)
2925 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2926 attrsum += NFSX_UNSIGNED;
2928 case NFSATTRBIT_HIDDEN:
2929 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2930 if (!nd->nd_repstat)
2931 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2932 attrsum += NFSX_UNSIGNED;
2934 case NFSATTRBIT_MIMETYPE:
2935 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2936 i = fxdr_unsigned(int, *tl);
2937 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2940 if (!nd->nd_repstat)
2941 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2942 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2944 case NFSATTRBIT_MODE:
2945 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
2946 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2947 nvap->na_mode = nfstov_mode(*tl);
2948 attrsum += NFSX_UNSIGNED;
2950 case NFSATTRBIT_OWNER:
2951 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2952 j = fxdr_unsigned(int, *tl);
2954 error = NFSERR_BADXDR;
2957 if (j > NFSV4_SMALLSTR)
2958 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2961 error = nfsrv_mtostr(nd, cp, j);
2963 if (j > NFSV4_SMALLSTR)
2964 free(cp, M_NFSSTRING);
2967 if (!nd->nd_repstat) {
2968 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
2970 if (!nd->nd_repstat)
2973 if (j > NFSV4_SMALLSTR)
2974 free(cp, M_NFSSTRING);
2975 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2977 case NFSATTRBIT_OWNERGROUP:
2978 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2979 j = fxdr_unsigned(int, *tl);
2981 error = NFSERR_BADXDR;
2984 if (j > NFSV4_SMALLSTR)
2985 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2988 error = nfsrv_mtostr(nd, cp, j);
2990 if (j > NFSV4_SMALLSTR)
2991 free(cp, M_NFSSTRING);
2994 if (!nd->nd_repstat) {
2995 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
2997 if (!nd->nd_repstat)
3000 if (j > NFSV4_SMALLSTR)
3001 free(cp, M_NFSSTRING);
3002 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3004 case NFSATTRBIT_SYSTEM:
3005 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3006 if (!nd->nd_repstat)
3007 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3008 attrsum += NFSX_UNSIGNED;
3010 case NFSATTRBIT_TIMEACCESSSET:
3011 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3012 attrsum += NFSX_UNSIGNED;
3013 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3014 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3015 fxdr_nfsv4time(tl, &nvap->na_atime);
3017 attrsum += NFSX_V4TIME;
3019 vfs_timestamp(&nvap->na_atime);
3020 nvap->na_vaflags |= VA_UTIMES_NULL;
3023 case NFSATTRBIT_TIMEBACKUP:
3024 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3025 if (!nd->nd_repstat)
3026 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3027 attrsum += NFSX_V4TIME;
3029 case NFSATTRBIT_TIMECREATE:
3030 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3031 fxdr_nfsv4time(tl, &nvap->na_btime);
3032 attrsum += NFSX_V4TIME;
3034 case NFSATTRBIT_TIMEMODIFYSET:
3035 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3036 attrsum += NFSX_UNSIGNED;
3037 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3038 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3039 fxdr_nfsv4time(tl, &nvap->na_mtime);
3040 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3041 attrsum += NFSX_V4TIME;
3043 vfs_timestamp(&nvap->na_mtime);
3045 nvap->na_vaflags |= VA_UTIMES_NULL;
3048 case NFSATTRBIT_MODESETMASKED:
3049 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3050 mode = fxdr_unsigned(u_short, *tl++);
3051 mask = fxdr_unsigned(u_short, *tl);
3053 * vp == NULL implies an Open/Create operation.
3054 * This attribute can only be used for Setattr and
3055 * only for NFSv4.1 or higher.
3056 * If moderet != 0, a mode attribute has also been
3057 * specified and this attribute cannot be done in the
3058 * same Setattr operation.
3060 if ((nd->nd_flag & ND_NFSV41) == 0)
3061 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3062 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3064 nd->nd_repstat = NFSERR_INVAL;
3065 else if (moderet == 0)
3066 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3068 nvap->na_mode = (mode & mask) |
3069 (va.va_mode & ~mask);
3071 nd->nd_repstat = moderet;
3072 attrsum += 2 * NFSX_UNSIGNED;
3075 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3077 * set bitpos so we drop out of the loop.
3079 bitpos = NFSATTRBIT_MAX;
3085 * some clients pad the attrlist, so we need to skip over the
3088 if (attrsum > attrsize) {
3089 error = NFSERR_BADXDR;
3091 attrsize = NFSM_RNDUP(attrsize);
3092 if (attrsum < attrsize)
3093 error = nfsm_advance(nd, attrsize - attrsum, -1);
3096 NFSEXITCODE2(error, nd);
3101 * Check/setup export credentials.
3104 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3105 struct ucred *credanon)
3110 * Check/setup credentials.
3112 if (nd->nd_flag & ND_GSS)
3113 exp->nes_exflag &= ~MNT_EXPORTANON;
3116 * Check to see if the operation is allowed for this security flavor.
3117 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
3118 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
3119 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
3121 if (nfsvno_testexp(nd, exp) &&
3122 nd->nd_procnum != NFSV4OP_SECINFO &&
3123 nd->nd_procnum != NFSPROC_FSINFO) {
3124 if (nd->nd_flag & ND_NFSV4)
3125 error = NFSERR_WRONGSEC;
3127 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3132 * Check to see if the file system is exported V4 only.
3134 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3135 error = NFSERR_PROGNOTV4;
3140 * Now, map the user credentials.
3141 * (Note that ND_AUTHNONE will only be set for an NFSv3
3142 * Fsinfo RPC. If set for anything else, this code might need
3145 if (NFSVNO_EXPORTED(exp)) {
3146 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3147 NFSVNO_EXPORTANON(exp) ||
3148 (nd->nd_flag & ND_AUTHNONE) != 0) {
3149 nd->nd_cred->cr_uid = credanon->cr_uid;
3150 nd->nd_cred->cr_gid = credanon->cr_gid;
3151 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3152 credanon->cr_groups);
3153 } else if ((nd->nd_flag & ND_GSS) == 0) {
3155 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3156 * if there is a replacement credential with a group
3157 * list set up by "nfsuserd -manage-gids".
3158 * If there is no replacement, nfsrv_getgrpscred()
3159 * simply returns its argument.
3161 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3166 NFSEXITCODE2(error, nd);
3174 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3175 struct ucred **credp)
3179 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3180 &exp->nes_numsecflavor, exp->nes_secflavors);
3182 if (nfs_rootfhset) {
3183 exp->nes_exflag = 0;
3184 exp->nes_numsecflavor = 0;
3187 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3189 printf("nfsvno_checkexp: numsecflavors out of range\n");
3190 exp->nes_numsecflavor = 0;
3198 * Get a vnode for a file handle and export stuff.
3201 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3202 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3203 struct ucred **credp)
3208 exp->nes_numsecflavor = 0;
3209 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3211 /* Make sure the server replies ESTALE to the client. */
3213 if (nam && !error) {
3214 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3215 &exp->nes_numsecflavor, exp->nes_secflavors);
3217 if (nfs_rootfhset) {
3218 exp->nes_exflag = 0;
3219 exp->nes_numsecflavor = 0;
3224 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3226 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3227 exp->nes_numsecflavor = 0;
3237 * nfsd_fhtovp() - convert a fh to a vnode ptr
3238 * - look up fsid in mount list (if not found ret error)
3239 * - get vp and export rights by calling nfsvno_fhtovp()
3240 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3242 * - if mpp != NULL, return the mount point so that it can
3243 * be used for vn_finished_write() by the caller
3246 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3247 struct vnode **vpp, struct nfsexstuff *exp,
3248 struct mount **mpp, int startwrite)
3251 struct ucred *credanon;
3254 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3256 * Check for the special case of the nfsv4root_fh.
3258 mp = vfs_busyfs(&fhp->fh_fsid);
3263 nd->nd_repstat = ESTALE;
3268 vn_start_write(NULL, mpp, V_WAIT);
3269 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3270 lktype = LK_EXCLUSIVE;
3272 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3277 * For NFSv4 without a pseudo root fs, unexported file handles
3278 * can be returned, so that Lookup works everywhere.
3280 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3281 !(nd->nd_flag & ND_NFSV4)) {
3283 nd->nd_repstat = EACCES;
3287 * Personally, I've never seen any point in requiring a
3288 * reserved port#, since only in the rare case where the
3289 * clients are all boxes with secure system privileges,
3290 * does it provide any enhanced security, but... some people
3291 * believe it to be useful and keep putting this code back in.
3292 * (There is also some "security checker" out there that
3293 * complains if the nfs server doesn't enforce this.)
3294 * However, note the following:
3295 * RFC3530 (NFSv4) specifies that a reserved port# not be
3297 * RFC2623 recommends that, if a reserved port# is checked for,
3298 * that there be a way to turn that off--> ifdef'd.
3300 #ifdef NFS_REQRSVPORT
3301 if (!nd->nd_repstat) {
3302 struct sockaddr_in *saddr;
3303 struct sockaddr_in6 *saddr6;
3305 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3306 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3307 if (!(nd->nd_flag & ND_NFSV4) &&
3308 ((saddr->sin_family == AF_INET &&
3309 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3310 (saddr6->sin6_family == AF_INET6 &&
3311 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3313 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3316 #endif /* NFS_REQRSVPORT */
3319 * Check/setup credentials.
3321 if (!nd->nd_repstat) {
3322 nd->nd_saveduid = nd->nd_cred->cr_uid;
3323 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
3327 if (credanon != NULL)
3329 if (nd->nd_repstat) {
3331 vn_finished_write(mp);
3338 NFSEXITCODE2(0, nd);
3345 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3347 struct filedesc *fdp;
3351 fdp = p->td_proc->p_fd;
3352 if (fd < 0 || fd >= fdp->fd_nfiles ||
3353 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3365 * Called from nfssvc() to update the exports list. Just call
3366 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3367 * in the mount list.
3370 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3372 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3374 struct nameidata nd;
3377 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3378 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3380 else if (error == 0) {
3381 if (nfsexargp->fspec == NULL) {
3386 * If fspec != NULL, this is the v4root path.
3388 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3389 nfsexargp->fspec, p);
3390 if ((error = namei(&nd)) != 0)
3392 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3395 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3396 NFSBCOPY((caddr_t)&fh,
3397 nfs_rootfh.nfsrvfh_data,
3398 sizeof (fhandle_t));
3409 * This function needs to test to see if the system is near its limit
3410 * for memory allocation via malloc() or mget() and return True iff
3411 * either of these resources are near their limit.
3412 * XXX (For now, this is just a stub.)
3414 int nfsrv_testmalloclimit = 0;
3416 nfsrv_mallocmget_limit(void)
3418 static int printmesg = 0;
3419 static int testval = 1;
3421 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3422 if ((printmesg++ % 100) == 0)
3423 printf("nfsd: malloc/mget near limit\n");
3430 * BSD specific initialization of a mount point.
3435 static int inited = 0;
3440 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3441 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3442 TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3443 nfsv4root_mnt.mnt_export = NULL;
3444 TAILQ_INIT(&nfsv4root_opt);
3445 TAILQ_INIT(&nfsv4root_newopt);
3446 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3447 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3448 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3449 nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3453 * Get a vnode for a file handle, without checking exports, etc.
3456 nfsvno_getvp(fhandle_t *fhp)
3462 mp = vfs_busyfs(&fhp->fh_fsid);
3465 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3473 * Do a local VOP_ADVLOCK().
3476 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3477 u_int64_t end, struct thread *td)
3483 if (nfsrv_dolocallocks == 0)
3485 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3487 fl.l_whence = SEEK_SET;
3489 fl.l_start = (off_t)first;
3490 if (end == NFS64BITSSET) {
3494 fl.l_len = (off_t)tlen;
3497 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3498 * values for all calls, so that all locks will be held by the
3499 * nfsd server. (The nfsd server handles conflicts between the
3501 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3502 * bytes, so it can't be put in l_sysid.
3504 if (nfsv4_sysid == 0)
3505 nfsv4_sysid = nlm_acquire_next_sysid();
3506 fl.l_pid = (pid_t)0;
3507 fl.l_sysid = (int)nfsv4_sysid;
3509 if (ftype == F_UNLCK)
3510 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3511 (F_POSIX | F_REMOTE));
3513 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3514 (F_POSIX | F_REMOTE));
3522 * Check the nfsv4 root exports.
3525 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3527 struct ucred *credanon;
3528 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3531 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3532 &credanon, &numsecflavor, secflavors);
3534 error = NFSERR_PROGUNAVAIL;
3537 if (credanon != NULL)
3539 for (i = 0; i < numsecflavor; i++) {
3540 if (secflavors[i] == AUTH_SYS)
3541 nd->nd_flag |= ND_EXAUTHSYS;
3542 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3543 nd->nd_flag |= ND_EXGSS;
3544 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3545 nd->nd_flag |= ND_EXGSSINTEGRITY;
3546 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3547 nd->nd_flag |= ND_EXGSSPRIVACY;
3556 * Nfs server pseudo system call for the nfsd's
3562 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3565 struct nfsd_addsock_args sockarg;
3566 struct nfsd_nfsd_args nfsdarg;
3567 struct nfsd_nfsd_oargs onfsdarg;
3568 struct nfsd_pnfsd_args pnfsdarg;
3569 struct vnode *vp, *nvp, *curdvp;
3570 struct pnfsdsfile *pf;
3571 struct nfsdevice *ds, *fds;
3572 cap_rights_t rights;
3573 int buflen, error, ret;
3574 char *buf, *cp, *cp2, *cp3;
3575 char fname[PNFS_FILENAME_LEN + 1];
3577 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3578 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3582 * Since we don't know what rights might be required,
3583 * pretend that we need them all. It is better to be too
3584 * careful than too reckless.
3586 error = fget(td, sockarg.sock,
3587 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3590 if (fp->f_type != DTYPE_SOCKET) {
3595 error = nfsrvd_addsock(fp);
3597 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3598 if (uap->argp == NULL) {
3602 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3603 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3605 nfsdarg.principal = onfsdarg.principal;
3606 nfsdarg.minthreads = onfsdarg.minthreads;
3607 nfsdarg.maxthreads = onfsdarg.maxthreads;
3608 nfsdarg.version = 1;
3609 nfsdarg.addr = NULL;
3610 nfsdarg.addrlen = 0;
3611 nfsdarg.dnshost = NULL;
3612 nfsdarg.dnshostlen = 0;
3613 nfsdarg.dspath = NULL;
3614 nfsdarg.dspathlen = 0;
3615 nfsdarg.mdspath = NULL;
3616 nfsdarg.mdspathlen = 0;
3617 nfsdarg.mirrorcnt = 1;
3620 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3623 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3624 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3625 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3626 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3627 nfsdarg.mirrorcnt >= 1 &&
3628 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3629 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3630 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3631 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3632 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3633 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3634 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3635 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3636 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3641 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3643 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3644 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3646 free(nfsdarg.addr, M_TEMP);
3650 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3651 nfsdarg.dnshost = cp;
3652 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3653 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3655 free(nfsdarg.addr, M_TEMP);
3656 free(nfsdarg.dnshost, M_TEMP);
3660 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3661 nfsdarg.dspath = cp;
3662 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3663 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3665 free(nfsdarg.addr, M_TEMP);
3666 free(nfsdarg.dnshost, M_TEMP);
3667 free(nfsdarg.dspath, M_TEMP);
3671 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3672 nfsdarg.mdspath = cp;
3674 nfsdarg.addr = NULL;
3675 nfsdarg.addrlen = 0;
3676 nfsdarg.dnshost = NULL;
3677 nfsdarg.dnshostlen = 0;
3678 nfsdarg.dspath = NULL;
3679 nfsdarg.dspathlen = 0;
3680 nfsdarg.mdspath = NULL;
3681 nfsdarg.mdspathlen = 0;
3682 nfsdarg.mirrorcnt = 1;
3684 error = nfsrvd_nfsd(td, &nfsdarg);
3685 free(nfsdarg.addr, M_TEMP);
3686 free(nfsdarg.dnshost, M_TEMP);
3687 free(nfsdarg.dspath, M_TEMP);
3688 free(nfsdarg.mdspath, M_TEMP);
3689 } else if (uap->flag & NFSSVC_PNFSDS) {
3690 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3691 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3692 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3693 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3694 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3697 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3699 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3700 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3701 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3702 buf = malloc(buflen, M_TEMP, M_WAITOK);
3703 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3705 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3706 if (error == 0 && pnfsdarg.dspath != NULL) {
3707 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3708 error = copyinstr(pnfsdarg.dspath, cp2,
3709 PATH_MAX + 1, NULL);
3710 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3714 if (error == 0 && pnfsdarg.curdspath != NULL) {
3715 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3716 error = copyinstr(pnfsdarg.curdspath, cp3,
3717 PATH_MAX + 1, NULL);
3718 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3725 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3726 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3728 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3730 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3731 printf("copymr: dsdir out of range\n");
3734 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3735 error = nfsrv_copymr(vp, nvp,
3736 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3737 (struct pnfsdsfile *)buf,
3738 buflen / sizeof(*pf), td->td_ucred, td);
3741 if (fds != NULL && error == 0) {
3742 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3743 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3745 nfsrv_dsremove(curdvp, fname,
3747 NFSVOPUNLOCK(curdvp);
3750 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3758 error = nfssvc_srvcall(td, uap, td->td_ucred);
3767 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3769 struct nfsex_args export;
3770 struct nfsex_oldargs oexp;
3771 struct file *fp = NULL;
3772 int stablefd, i, len;
3773 struct nfsd_clid adminrevoke;
3774 struct nfsd_dumplist dumplist;
3775 struct nfsd_dumpclients *dumpclients;
3776 struct nfsd_dumplocklist dumplocklist;
3777 struct nfsd_dumplocks *dumplocks;
3778 struct nameidata nd;
3780 int error = EINVAL, igotlock;
3783 static int suspend_nfsd = 0;
3785 if (uap->flag & NFSSVC_PUBLICFH) {
3786 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3787 sizeof (fhandle_t));
3788 error = copyin(uap->argp,
3789 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3792 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3793 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3794 error = copyin(uap->argp,(caddr_t)&export,
3795 sizeof (struct nfsex_args));
3798 if (export.export.ex_ngroups > NGROUPS_MAX ||
3799 export.export.ex_ngroups < 0)
3801 else if (export.export.ex_ngroups > 0) {
3802 grps = malloc(export.export.ex_ngroups *
3803 sizeof(gid_t), M_TEMP, M_WAITOK);
3804 error = copyin(export.export.ex_groups, grps,
3805 export.export.ex_ngroups * sizeof(gid_t));
3806 export.export.ex_groups = grps;
3808 export.export.ex_groups = NULL;
3810 error = nfsrv_v4rootexport(&export, cred, p);
3813 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3814 NFSSVC_V4ROOTEXPORT) {
3815 error = copyin(uap->argp,(caddr_t)&oexp,
3816 sizeof (struct nfsex_oldargs));
3818 memset(&export.export, 0, sizeof(export.export));
3819 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3820 export.export.ex_root = oexp.export.ex_root;
3821 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3822 export.export.ex_ngroups =
3823 oexp.export.ex_anon.cr_ngroups;
3824 export.export.ex_groups = NULL;
3825 if (export.export.ex_ngroups > XU_NGROUPS ||
3826 export.export.ex_ngroups < 0)
3828 else if (export.export.ex_ngroups > 0) {
3829 export.export.ex_groups = malloc(
3830 export.export.ex_ngroups * sizeof(gid_t),
3832 for (i = 0; i < export.export.ex_ngroups; i++)
3833 export.export.ex_groups[i] =
3834 oexp.export.ex_anon.cr_groups[i];
3836 export.export.ex_addr = oexp.export.ex_addr;
3837 export.export.ex_addrlen = oexp.export.ex_addrlen;
3838 export.export.ex_mask = oexp.export.ex_mask;
3839 export.export.ex_masklen = oexp.export.ex_masklen;
3840 export.export.ex_indexfile = oexp.export.ex_indexfile;
3841 export.export.ex_numsecflavors =
3842 oexp.export.ex_numsecflavors;
3843 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3844 export.export.ex_numsecflavors < 0)
3847 for (i = 0; i < export.export.ex_numsecflavors;
3849 export.export.ex_secflavors[i] =
3850 oexp.export.ex_secflavors[i];
3852 export.fspec = oexp.fspec;
3854 error = nfsrv_v4rootexport(&export, cred, p);
3855 free(export.export.ex_groups, M_TEMP);
3857 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3860 } else if (uap->flag & NFSSVC_STABLERESTART) {
3861 error = copyin(uap->argp, (caddr_t)&stablefd,
3864 error = fp_getfvp(p, stablefd, &fp, &vp);
3865 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3867 if (!error && newnfs_numnfsd != 0)
3870 nfsrv_stablefirst.nsf_fp = fp;
3871 nfsrv_setupstable(p);
3873 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3874 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3875 sizeof (struct nfsd_clid));
3877 error = nfsrv_adminrevoke(&adminrevoke, p);
3878 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3879 error = copyin(uap->argp, (caddr_t)&dumplist,
3880 sizeof (struct nfsd_dumplist));
3881 if (!error && (dumplist.ndl_size < 1 ||
3882 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3885 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3886 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3887 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3888 error = copyout(dumpclients, dumplist.ndl_list, len);
3889 free(dumpclients, M_TEMP);
3891 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3892 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3893 sizeof (struct nfsd_dumplocklist));
3894 if (!error && (dumplocklist.ndllck_size < 1 ||
3895 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3898 error = nfsrv_lookupfilename(&nd,
3899 dumplocklist.ndllck_fname, p);
3901 len = sizeof (struct nfsd_dumplocks) *
3902 dumplocklist.ndllck_size;
3903 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3904 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3905 dumplocklist.ndllck_size, p);
3907 error = copyout(dumplocks, dumplocklist.ndllck_list,
3909 free(dumplocks, M_TEMP);
3911 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3914 nfsd_master_pid = procp->p_pid;
3915 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3916 nfsd_master_start = procp->p_stats->p_start;
3917 nfsd_master_proc = procp;
3919 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3920 NFSLOCKV4ROOTMUTEX();
3921 if (suspend_nfsd == 0) {
3922 /* Lock out all nfsd threads */
3924 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3925 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3926 } while (igotlock == 0 && suspend_nfsd == 0);
3929 NFSUNLOCKV4ROOTMUTEX();
3931 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3932 NFSLOCKV4ROOTMUTEX();
3933 if (suspend_nfsd != 0) {
3934 nfsv4_unlock(&nfsd_suspend_lock, 0);
3937 NFSUNLOCKV4ROOTMUTEX();
3947 * Returns 0 if ok, 1 otherwise.
3950 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3955 * This seems odd, but allow the case where the security flavor
3956 * list is empty. This happens when NFSv4 is traversing non-exported
3957 * file systems. Exported file systems should always have a non-empty
3958 * security flavor list.
3960 if (exp->nes_numsecflavor == 0)
3963 for (i = 0; i < exp->nes_numsecflavor; i++) {
3965 * The tests for privacy and integrity must be first,
3966 * since ND_GSS is set for everything but AUTH_SYS.
3968 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3969 (nd->nd_flag & ND_GSSPRIVACY))
3971 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3972 (nd->nd_flag & ND_GSSINTEGRITY))
3974 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3975 (nd->nd_flag & ND_GSS))
3977 if (exp->nes_secflavors[i] == AUTH_SYS &&
3978 (nd->nd_flag & ND_GSS) == 0)
3985 * Calculate a hash value for the fid in a file handle.
3988 nfsrv_hashfh(fhandle_t *fhp)
3992 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3997 * Calculate a hash value for the sessionid.
4000 nfsrv_hashsessionid(uint8_t *sessionid)
4004 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4009 * Signal the userland master nfsd to backup the stable restart file.
4012 nfsrv_backupstable(void)
4016 if (nfsd_master_proc != NULL) {
4017 procp = pfind(nfsd_master_pid);
4018 /* Try to make sure it is the correct process. */
4019 if (procp == nfsd_master_proc &&
4020 procp->p_stats->p_start.tv_sec ==
4021 nfsd_master_start.tv_sec &&
4022 procp->p_stats->p_start.tv_usec ==
4023 nfsd_master_start.tv_usec &&
4024 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4025 kern_psignal(procp, SIGUSR2);
4027 nfsd_master_proc = NULL;
4035 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4036 * The arguments are in a structure, so that they can be passed through
4037 * taskqueue for a kernel process to execute this function.
4039 struct nfsrvdscreate {
4043 struct ucred *tcred;
4046 struct pnfsdsfile *pf;
4050 struct vattr createva;
4054 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4055 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4056 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4059 struct nameidata named;
4064 struct nfsmount *nmp;
4067 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4068 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
4069 nfsvno_setpathbuf(&named, &bufp, &hashp);
4070 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4071 named.ni_cnd.cn_thread = p;
4072 named.ni_cnd.cn_nameptr = bufp;
4073 if (fnamep != NULL) {
4074 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4075 named.ni_cnd.cn_namelen = strlen(bufp);
4077 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4078 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4080 /* Create the date file in the DS mount. */
4081 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4083 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4086 /* Set the ownership of the file. */
4087 error = VOP_SETATTR(nvp, nvap, tcred);
4088 NFSD_DEBUG(4, "nfsrv_dscreate:"
4089 " setattr-uid=%d\n", error);
4094 printf("pNFS: pnfscreate failed=%d\n", error);
4096 printf("pNFS: pnfscreate vnlock=%d\n", error);
4099 nmp = VFSTONFS(nvp->v_mount);
4100 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4101 != 0 || nmp->nm_nam->sa_len > sizeof(
4102 struct sockaddr_in6) ||
4103 np->n_fhp->nfh_len != NFSX_MYFH) {
4104 printf("Bad DS file: fstype=%s salen=%d"
4106 nvp->v_mount->mnt_vfc->vfc_name,
4107 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4111 /* Set extattrs for the DS on the MDS file. */
4114 error = VOP_GETATTR(nvp, &va, tcred);
4116 dsa->dsa_filerev = va.va_filerev;
4117 dsa->dsa_size = va.va_size;
4118 dsa->dsa_atime = va.va_atime;
4119 dsa->dsa_mtime = va.va_mtime;
4120 dsa->dsa_bytes = va.va_bytes;
4124 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4126 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4127 nmp->nm_nam->sa_len);
4128 NFSBCOPY(named.ni_cnd.cn_nameptr,
4130 sizeof(pf->dsf_filename));
4133 printf("pNFS: pnfscreate can't get DS"
4134 " attr=%d\n", error);
4135 if (nvpp != NULL && error == 0)
4140 nfsvno_relpathbuf(&named);
4145 * Start up the thread that will execute nfsrv_dscreate().
4148 start_dscreate(void *arg, int pending)
4150 struct nfsrvdscreate *dsc;
4152 dsc = (struct nfsrvdscreate *)arg;
4153 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4154 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4156 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4160 * Create a pNFS data file on the Data Server(s).
4163 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4166 struct nfsrvdscreate *dsc, *tdsc = NULL;
4167 struct nfsdevice *ds, *tds, *fds;
4169 struct pnfsdsfile *pf, *tpf;
4170 struct pnfsdsattr dsattr;
4172 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4173 struct nfsmount *nmp;
4178 struct ucred *tcred;
4179 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4182 /* Get a DS server directory in a round-robin order. */
4188 * Search for the first entry that handles this MDS fs, but use the
4189 * first entry for all MDS fs's otherwise.
4191 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4192 if (tds->nfsdev_nmp != NULL) {
4193 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4195 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4196 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4204 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4207 i = dsdir[0] = ds->nfsdev_nextdir;
4208 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4209 dvp[0] = ds->nfsdev_dsdir[i];
4210 tds = TAILQ_NEXT(ds, nfsdev_list);
4211 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4212 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4213 if (tds->nfsdev_nmp != NULL &&
4214 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4215 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4216 fsidcmp(&mp->mnt_stat.f_fsid,
4217 &tds->nfsdev_mdsfsid) == 0))) {
4218 dsdir[mirrorcnt] = i;
4219 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4221 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4226 /* Put at end of list to implement round-robin usage. */
4227 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4228 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4232 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4234 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4237 error = nfsvno_getfh(vp, &fh, p);
4239 error = VOP_GETATTR(vp, &va, cred);
4241 /* Set the attributes for "vp" to Setattr the DS vp. */
4244 vamode = va.va_mode;
4248 va.va_mode = vamode;
4251 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4253 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4255 /* Make data file name based on FH. */
4256 tcred = newnfs_getcred();
4259 * Create the file on each DS mirror, using kernel process(es) for the
4260 * additional mirrors.
4263 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4264 tpf->dsf_dir = dsdir[i];
4265 tdsc->tcred = tcred;
4268 tdsc->createva = *vap;
4269 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4276 if (nfs_pnfsiothreads != 0) {
4277 ret = nfs_pnfsio(start_dscreate, tdsc);
4278 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4281 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4282 NULL, tcred, p, NULL);
4284 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4286 if (failpos == -1 && nfsds_failerr(ret))
4294 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4295 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4296 &dsattr, NULL, tcred, p, NULL);
4297 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4298 failpos = mirrorcnt - 1;
4302 timo = hz / 50; /* Wait for 20msec. */
4305 /* Wait for kernel task(s) to complete. */
4306 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4307 while (tdsc->inprog != 0 && tdsc->done == 0)
4308 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4309 if (tdsc->err != 0) {
4310 if (failpos == -1 && nfsds_failerr(tdsc->err))
4312 else if (error == 0)
4318 * If failpos has been set, that mirror has failed, so it needs
4322 nmp = VFSTONFS(dvp[failpos]->v_mount);
4324 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4325 NFSMNTP_CANCELRPCS)) == 0) {
4326 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4328 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4329 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4332 nfsrv_killrpcs(nmp);
4334 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4342 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4344 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4345 mirrorcnt, nfsrv_maxpnfsmirror);
4347 * For all mirrors that couldn't be created, fill in the
4348 * *pf structure, but with an IP address == 0.0.0.0.
4350 tpf = pf + mirrorcnt;
4351 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4353 tpf->dsf_sin.sin_family = AF_INET;
4354 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4355 tpf->dsf_sin.sin_addr.s_addr = 0;
4356 tpf->dsf_sin.sin_port = 0;
4359 error = vn_extattr_set(vp, IO_NODELOCKED,
4360 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4361 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4363 error = vn_extattr_set(vp, IO_NODELOCKED,
4364 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4365 sizeof(dsattr), (char *)&dsattr, p);
4367 printf("pNFS: pnfscreate setextattr=%d\n",
4370 printf("pNFS: pnfscreate=%d\n", error);
4376 * Get the information needed to remove the pNFS Data Server file from the
4377 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4378 * DS directory vnode. The caller must unlock *ddvp when done with it.
4381 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4382 int *mirrorcntp, char *fname, fhandle_t *fhp)
4385 struct ucred *tcred;
4390 /* If not an exported regular file or not a pNFS server, just return. */
4391 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4392 nfsrv_devidcnt == 0)
4395 /* Check to see if this is the last hard link. */
4396 tcred = newnfs_getcred();
4397 error = VOP_GETATTR(vp, &va, tcred);
4400 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4403 if (va.va_nlink > 1)
4406 error = nfsvno_getfh(vp, fhp, p);
4408 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4413 buf = malloc(buflen, M_TEMP, M_WAITOK);
4414 /* Get the directory vnode for the DS mount and the file handle. */
4415 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4416 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4419 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4423 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4424 * The arguments are in a structure, so that they can be passed through
4425 * taskqueue for a kernel process to execute this function.
4427 struct nfsrvdsremove {
4431 struct ucred *tcred;
4435 char fname[PNFS_FILENAME_LEN + 1];
4439 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4442 struct nameidata named;
4448 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4451 named.ni_cnd.cn_nameiop = DELETE;
4452 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4453 named.ni_cnd.cn_cred = tcred;
4454 named.ni_cnd.cn_thread = p;
4455 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4456 nfsvno_setpathbuf(&named, &bufp, &hashp);
4457 named.ni_cnd.cn_nameptr = bufp;
4458 named.ni_cnd.cn_namelen = strlen(fname);
4459 strlcpy(bufp, fname, NAME_MAX);
4460 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4461 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4462 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4464 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4468 nfsvno_relpathbuf(&named);
4470 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4475 * Start up the thread that will execute nfsrv_dsremove().
4478 start_dsremove(void *arg, int pending)
4480 struct nfsrvdsremove *dsrm;
4482 dsrm = (struct nfsrvdsremove *)arg;
4483 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4486 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4490 * Remove a pNFS data file from a Data Server.
4491 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4492 * removed to set up the dvp and fill in the FH.
4495 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4498 struct ucred *tcred;
4499 struct nfsrvdsremove *dsrm, *tdsrm;
4500 struct nfsdevice *ds;
4501 struct nfsmount *nmp;
4502 int failpos, i, ret, timo;
4504 tcred = newnfs_getcred();
4507 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4509 * Remove the file on each DS mirror, using kernel process(es) for the
4510 * additional mirrors.
4513 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4514 tdsrm->tcred = tcred;
4516 tdsrm->dvp = dvp[i];
4517 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4522 if (nfs_pnfsiothreads != 0) {
4523 ret = nfs_pnfsio(start_dsremove, tdsrm);
4524 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4527 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4528 if (failpos == -1 && nfsds_failerr(ret))
4532 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4533 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4534 failpos = mirrorcnt - 1;
4535 timo = hz / 50; /* Wait for 20msec. */
4538 /* Wait for kernel task(s) to complete. */
4539 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4540 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4541 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4542 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4547 * If failpos has been set, that mirror has failed, so it needs
4551 nmp = VFSTONFS(dvp[failpos]->v_mount);
4553 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4554 NFSMNTP_CANCELRPCS)) == 0) {
4555 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4557 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4558 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4561 nfsrv_killrpcs(nmp);
4563 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4569 /* Get rid all layouts for the file. */
4570 nfsrv_freefilelayouts(fhp);
4577 * Generate a file name based on the file handle and put it in *bufp.
4578 * Return the number of bytes generated.
4581 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4585 const uint8_t *hexdigits = "0123456789abcdef";
4587 cp = (uint8_t *)fhp;
4588 for (i = 0; i < sizeof(*fhp); i++) {
4589 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4590 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4597 * Update the Metadata file's attributes from the DS file when a Read/Write
4598 * layout is returned.
4599 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4600 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4603 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4605 struct ucred *tcred;
4608 /* Do this as root so that it won't fail with EACCES. */
4609 tcred = newnfs_getcred();
4610 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4611 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4617 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4620 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4625 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4626 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4631 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4632 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4633 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4634 off_t *offp, int content, bool *eofp)
4636 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4637 fhandle_t fh[NFSDEV_MAXMIRRORS];
4638 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4639 struct nfsdevice *ds;
4640 struct pnfsdsattr dsattr;
4641 struct opnfsdsattr odsattr;
4643 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4645 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4647 * If not a regular file, not exported or not a pNFS server,
4648 * just return ENOENT.
4650 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4651 nfsrv_devidcnt == 0)
4655 buf = malloc(buflen, M_TEMP, M_WAITOK);
4659 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4660 * from the MetaData file's extended attribute.
4662 if (ioproc == NFSPROC_GETATTR) {
4663 error = vn_extattr_get(vp, IO_NODELOCKED,
4664 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4667 if (buflen == sizeof(odsattr)) {
4668 NFSBCOPY(buf, &odsattr, buflen);
4669 nap->na_filerev = odsattr.dsa_filerev;
4670 nap->na_size = odsattr.dsa_size;
4671 nap->na_atime = odsattr.dsa_atime;
4672 nap->na_mtime = odsattr.dsa_mtime;
4674 * Fake na_bytes by rounding up na_size.
4675 * Since we don't know the block size, just
4676 * use BLKDEV_IOSIZE.
4678 nap->na_bytes = (odsattr.dsa_size +
4679 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4680 } else if (buflen == sizeof(dsattr)) {
4681 NFSBCOPY(buf, &dsattr, buflen);
4682 nap->na_filerev = dsattr.dsa_filerev;
4683 nap->na_size = dsattr.dsa_size;
4684 nap->na_atime = dsattr.dsa_atime;
4685 nap->na_mtime = dsattr.dsa_mtime;
4686 nap->na_bytes = dsattr.dsa_bytes;
4692 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4693 * returns 0, just return now. nfsrv_checkdsattr()
4694 * returns 0 if there is no Read/Write layout
4695 * plus either an Open/Write_access or Write
4696 * delegation issued to a client for the file.
4698 if (nfsrv_pnfsgetdsattr == 0 ||
4699 nfsrv_checkdsattr(vp, p) == 0) {
4706 * Clear ENOATTR so the code below will attempt to do a
4707 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4708 * the extended attribute.
4710 if (error == ENOATTR)
4719 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4721 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4722 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4723 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4726 for (i = 0; i < mirrorcnt; i++)
4727 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4729 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4731 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4734 if (origmircnt == -1)
4735 origmircnt = mirrorcnt;
4737 * If failpos is set to a mirror#, then that mirror has
4738 * failed and will be disabled. For Read, Getattr and Seek, the
4739 * function only tries one mirror, so if that mirror has
4740 * failed, it will need to be retried. As such, increment
4741 * tryitagain for these cases.
4742 * For Write, Setattr and Setacl, the function tries all
4743 * mirrors and will not return an error for the case where
4744 * one mirror has failed. For these cases, the functioning
4745 * mirror(s) will have been modified, so a retry isn't
4746 * necessary. These functions will set failpos for the
4749 if (ioproc == NFSPROC_READDS) {
4750 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4752 if (nfsds_failerr(error) && mirrorcnt > 1) {
4754 * Setting failpos will cause the mirror
4755 * to be disabled and then a retry of this
4762 } else if (ioproc == NFSPROC_WRITEDS)
4763 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4764 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4765 else if (ioproc == NFSPROC_SETATTR)
4766 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4767 mirrorcnt, nap, &failpos);
4768 else if (ioproc == NFSPROC_SETACL)
4769 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4770 mirrorcnt, aclp, &failpos);
4771 else if (ioproc == NFSPROC_SEEKDS) {
4772 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4774 if (nfsds_failerr(error) && mirrorcnt > 1) {
4776 * Setting failpos will cause the mirror
4777 * to be disabled and then a retry of this
4784 } else if (ioproc == NFSPROC_ALLOCATE)
4785 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4786 &nmp[0], mirrorcnt, &failpos);
4788 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4789 vp, nmp[mirrorcnt - 1], nap);
4790 if (nfsds_failerr(error) && mirrorcnt > 1) {
4792 * Setting failpos will cause the mirror
4793 * to be disabled and then a retry of this
4794 * getattr is required.
4796 failpos = mirrorcnt - 1;
4803 failnmp = nmp[failpos];
4804 NFSLOCKMNT(failnmp);
4805 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4806 NFSMNTP_CANCELRPCS)) == 0) {
4807 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4808 NFSUNLOCKMNT(failnmp);
4809 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4811 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4814 nfsrv_killrpcs(failnmp);
4815 NFSLOCKMNT(failnmp);
4816 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4819 NFSUNLOCKMNT(failnmp);
4821 for (i = 0; i < mirrorcnt; i++)
4822 NFSVOPUNLOCK(dvp[i]);
4823 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4825 /* Try the Read/Getattr again if a mirror was deleted. */
4826 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4829 /* Return ENOENT for any Extended Attribute error. */
4833 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4838 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4840 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4841 * to be checked. If it points to a NULL nmp, then it returns
4842 * a suitable destination.
4843 * curnmp - If non-NULL, it is the source mount for the copy.
4846 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4847 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4848 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4849 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4851 struct vnode *dvp, *nvp = NULL, **tdvpp;
4853 struct nfsmount *nmp, *newnmp;
4854 struct sockaddr *sad;
4855 struct sockaddr_in *sin;
4856 struct nfsdevice *ds, *tds, *fndds;
4857 struct pnfsdsfile *pf;
4859 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4861 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4870 if (newnmpp != NULL)
4875 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4876 "pnfsd.dsfile", buflenp, buf, p);
4877 mirrorcnt = *buflenp / sizeof(*pf);
4878 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4879 *buflenp != sizeof(*pf) * mirrorcnt))
4882 pf = (struct pnfsdsfile *)buf;
4883 /* If curnmp != NULL, check for a match in the mirror list. */
4884 if (curnmp != NULL && error == 0) {
4886 for (i = 0; i < mirrorcnt; i++, pf++) {
4887 sad = (struct sockaddr *)&pf->dsf_sin;
4888 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
4900 pf = (struct pnfsdsfile *)buf;
4901 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
4903 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
4905 sad = (struct sockaddr *)&pf->dsf_sin;
4907 dsdir = pf->dsf_dir;
4908 if (dsdir >= nfsrv_dsdirsize) {
4909 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
4911 } else if (nvpp != NULL && newnmp != NULL &&
4912 nfsaddr2_match(sad, newnmp->nm_nam))
4915 if (ippos != NULL && curnmp == NULL &&
4916 sad->sa_family == AF_INET &&
4917 sin->sin_addr.s_addr == 0)
4919 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
4921 /* Use the socket address to find the mount point. */
4924 /* Find a match for the IP address. */
4925 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
4926 if (ds->nfsdev_nmp != NULL) {
4927 dvp = ds->nfsdev_dvp;
4928 nmp = VFSTONFS(dvp->v_mount);
4929 if (nmp != ds->nfsdev_nmp)
4930 printf("different2 nmp %p %p\n",
4931 nmp, ds->nfsdev_nmp);
4932 if (nfsaddr2_match(sad, nmp->nm_nam)) {
4938 if (fndds != NULL && newnmpp != NULL &&
4940 /* Search for a place to make a mirror copy. */
4941 TAILQ_FOREACH(tds, &nfsrv_devidhead,
4943 if (tds->nfsdev_nmp != NULL &&
4945 ((tds->nfsdev_mdsisset == 0 &&
4946 fndds->nfsdev_mdsisset == 0) ||
4947 (tds->nfsdev_mdsisset != 0 &&
4948 fndds->nfsdev_mdsisset != 0 &&
4949 fsidcmp(&tds->nfsdev_mdsfsid,
4950 &mp->mnt_stat.f_fsid) == 0))) {
4951 *newnmpp = tds->nfsdev_nmp;
4957 * Move this entry to the end of the
4958 * list, so it won't be selected as
4959 * easily the next time.
4961 TAILQ_REMOVE(&nfsrv_devidhead, tds,
4963 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
4968 if (fndds != NULL) {
4969 dvp = fndds->nfsdev_dsdir[dsdir];
4970 if (lktype != 0 || fhiszero != 0 ||
4971 (nvpp != NULL && *nvpp == NULL)) {
4973 error = vn_lock(dvp,
4975 else if (lktype != 0)
4976 error = vn_lock(dvp, lktype);
4978 error = vn_lock(dvp, LK_SHARED);
4980 * If the file handle is all 0's, try to
4981 * do a Lookup against the DS to acquire
4983 * If dvpp == NULL or the Lookup fails,
4984 * unlock dvp after the call.
4986 if (error == 0 && (fhiszero != 0 ||
4987 (nvpp != NULL && *nvpp == NULL))) {
4988 error = nfsrv_pnfslookupds(vp,
5004 if (error != 0 || lktype == 0)
5010 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5011 if (devid != NULL) {
5012 NFSBCOPY(fndds->nfsdev_deviceid,
5013 devid, NFSX_V4DEVICEID);
5014 devid += NFSX_V4DEVICEID;
5019 NFSBCOPY(&pf->dsf_fh, fhp++,
5021 if (fnamep != NULL && gotone == 1)
5024 sizeof(pf->dsf_filename));
5026 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5031 if (error == 0 && gotone == 0)
5034 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5037 *mirrorcntp = gotone;
5039 if (gotone > 0 && dvpp != NULL) {
5041 * If the error didn't occur on the first one and
5042 * dvpp != NULL, the one(s) prior to the failure will
5043 * have locked dvp's that need to be unlocked.
5045 for (i = 0; i < gotone; i++) {
5046 NFSVOPUNLOCK(*dvpp);
5051 * If it found the vnode to be copied from before a failure,
5052 * it needs to be vput()'d.
5054 if (nvpp != NULL && *nvpp != NULL) {
5063 * Set the extended attribute for the Change attribute.
5066 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5068 struct pnfsdsattr dsattr;
5071 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5072 dsattr.dsa_filerev = nap->na_filerev;
5073 dsattr.dsa_size = nap->na_size;
5074 dsattr.dsa_atime = nap->na_atime;
5075 dsattr.dsa_mtime = nap->na_mtime;
5076 dsattr.dsa_bytes = nap->na_bytes;
5077 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5078 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5080 printf("pNFS: setextattr=%d\n", error);
5085 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5086 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5089 struct nfsrv_descript *nd;
5091 struct mbuf *m, *m2;
5092 int error = 0, retlen, tlen, trimlen;
5094 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5095 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5098 * Use a stateid where other is an alternating 01010 pattern and
5099 * seqid is 0xffffffff. This value is not defined as special by
5100 * the RFC and is used by the FreeBSD NFS server to indicate an
5101 * MDS->DS proxy operation.
5103 st.other[0] = 0x55555555;
5104 st.other[1] = 0x55555555;
5105 st.other[2] = 0x55555555;
5106 st.seqid = 0xffffffff;
5107 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5109 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5110 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5111 txdr_hyper(off, tl);
5112 *(tl + 2) = txdr_unsigned(len);
5113 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5114 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5119 if (nd->nd_repstat == 0) {
5120 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5121 NFSM_STRSIZ(retlen, len);
5123 /* Trim off the pre-data XDR from the mbuf chain. */
5125 while (m != NULL && m != nd->nd_md) {
5126 if (m->m_next == nd->nd_md) {
5128 m_freem(nd->nd_mrep);
5129 nd->nd_mrep = m = nd->nd_md;
5134 printf("nfsrv_readdsrpc: busted mbuf list\n");
5140 * Now, adjust first mbuf so that any XDR before the
5141 * read data is skipped over.
5143 trimlen = nd->nd_dpos - mtod(m, char *);
5145 m->m_len -= trimlen;
5146 NFSM_DATAP(m, trimlen);
5150 * Truncate the mbuf chain at retlen bytes of data,
5151 * plus XDR padding that brings the length up to a
5154 tlen = NFSM_RNDUP(retlen);
5156 if (m->m_len >= tlen) {
5166 } while (m != NULL);
5168 printf("nfsrv_readdsrpc: busted mbuf list\n");
5177 error = nd->nd_repstat;
5179 /* If nd->nd_mrep is already NULL, this is a no-op. */
5180 m_freem(nd->nd_mrep);
5182 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5187 * Do a write RPC on a DS data file, using this structure for the arguments,
5188 * so that this function can be executed by a separate kernel process.
5190 struct nfsrvwritedsdorpc {
5197 struct nfsmount *nmp;
5205 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5206 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5209 struct nfsrv_descript *nd;
5210 nfsattrbit_t attrbits;
5212 int commit, error, retlen;
5214 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5215 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5216 sizeof(fhandle_t), NULL, NULL, 0, 0);
5219 * Use a stateid where other is an alternating 01010 pattern and
5220 * seqid is 0xffffffff. This value is not defined as special by
5221 * the RFC and is used by the FreeBSD NFS server to indicate an
5222 * MDS->DS proxy operation.
5224 st.other[0] = 0x55555555;
5225 st.other[1] = 0x55555555;
5226 st.other[2] = 0x55555555;
5227 st.seqid = 0xffffffff;
5228 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5229 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5230 txdr_hyper(off, tl);
5233 * Do all writes FileSync, since the server doesn't hold onto dirty
5234 * buffers. Since clients should be accessing the DS servers directly
5235 * using the pNFS layouts, this just needs to work correctly as a
5238 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5239 *tl = txdr_unsigned(len);
5240 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5242 /* Put data in mbuf chain. */
5243 nd->nd_mb->m_next = m;
5245 /* Set nd_mb and nd_bpos to end of data. */
5246 while (m->m_next != NULL)
5249 nfsm_set(nd, m->m_len);
5250 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5252 /* Do a Getattr for the attributes that change upon writing. */
5253 NFSZERO_ATTRBIT(&attrbits);
5254 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5255 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5256 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5257 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5258 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5259 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5260 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5261 (void) nfsrv_putattrbit(nd, &attrbits);
5262 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5263 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5268 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5269 /* Get rid of weak cache consistency data for now. */
5270 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5271 (ND_NFSV4 | ND_V4WCCATTR)) {
5272 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5273 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5274 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5278 * Get rid of Op# and status for next op.
5280 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5282 nd->nd_flag |= ND_NOMOREDATA;
5284 if (nd->nd_repstat == 0) {
5285 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5286 retlen = fxdr_unsigned(int, *tl++);
5287 commit = fxdr_unsigned(int, *tl);
5288 if (commit != NFSWRITE_FILESYNC)
5290 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5291 retlen, commit, error);
5293 error = nd->nd_repstat;
5294 /* We have no use for the Write Verifier since we use FileSync. */
5297 * Get the Change, Size, Access Time and Modify Time attributes and set
5298 * on the Metadata file, so its attributes will be what the file's
5299 * would be if it had been written.
5302 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5303 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5304 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5306 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5308 m_freem(nd->nd_mrep);
5310 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5315 * Start up the thread that will execute nfsrv_writedsdorpc().
5318 start_writedsdorpc(void *arg, int pending)
5320 struct nfsrvwritedsdorpc *drpc;
5322 drpc = (struct nfsrvwritedsdorpc *)arg;
5323 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5324 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5326 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5330 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5331 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5332 struct mbuf **mpp, char *cp, int *failposp)
5334 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5337 int error, i, offs, ret, timo;
5339 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5340 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5343 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5346 /* Calculate offset in mbuf chain that data starts. */
5347 offs = cp - mtod(*mpp, char *);
5348 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5351 * Do the write RPC for every DS, using a separate kernel process
5352 * for every DS except the last one.
5355 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5357 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5365 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5367 if (nfs_pnfsiothreads != 0) {
5368 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5369 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5373 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5375 if (nfsds_failerr(ret) && *failposp == -1)
5377 else if (error == 0 && ret != 0)
5383 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5384 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5385 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5386 *failposp = mirrorcnt - 1;
5387 else if (error == 0 && ret != 0)
5390 error = nfsrv_setextattr(vp, &na, p);
5391 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5393 timo = hz / 50; /* Wait for 20msec. */
5396 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5397 /* Wait for RPCs on separate threads to complete. */
5398 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5399 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5400 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5402 else if (error == 0 && tdrpc->err != 0)
5410 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5411 * so that this function can be executed by a separate kernel process.
5413 struct nfsrvallocatedsdorpc {
5420 struct nfsmount *nmp;
5427 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5428 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5431 struct nfsrv_descript *nd;
5432 nfsattrbit_t attrbits;
5436 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5437 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5438 sizeof(fhandle_t), NULL, NULL, 0, 0);
5441 * Use a stateid where other is an alternating 01010 pattern and
5442 * seqid is 0xffffffff. This value is not defined as special by
5443 * the RFC and is used by the FreeBSD NFS server to indicate an
5444 * MDS->DS proxy operation.
5446 st.other[0] = 0x55555555;
5447 st.other[1] = 0x55555555;
5448 st.other[2] = 0x55555555;
5449 st.seqid = 0xffffffff;
5450 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5451 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5452 txdr_hyper(off, tl); tl += 2;
5453 txdr_hyper(len, tl); tl += 2;
5454 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5456 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5457 NFSGETATTR_ATTRBIT(&attrbits);
5458 nfsrv_putattrbit(nd, &attrbits);
5459 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5460 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5465 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5467 if (nd->nd_repstat == 0) {
5468 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5469 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5470 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5472 error = nd->nd_repstat;
5473 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5475 m_freem(nd->nd_mrep);
5477 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5482 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5485 start_allocatedsdorpc(void *arg, int pending)
5487 struct nfsrvallocatedsdorpc *drpc;
5489 drpc = (struct nfsrvallocatedsdorpc *)arg;
5490 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5491 drpc->len, NULL, drpc->cred, drpc->p);
5493 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5497 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5498 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5501 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5503 int error, i, ret, timo;
5505 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5508 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5512 * Do the allocate RPC for every DS, using a separate kernel process
5513 * for every DS except the last one.
5516 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5518 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5527 if (nfs_pnfsiothreads != 0) {
5528 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5529 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5533 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5535 if (nfsds_failerr(ret) && *failposp == -1)
5537 else if (error == 0 && ret != 0)
5543 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5544 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5545 *failposp = mirrorcnt - 1;
5546 else if (error == 0 && ret != 0)
5549 error = nfsrv_setextattr(vp, &na, p);
5550 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5552 timo = hz / 50; /* Wait for 20msec. */
5555 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5556 /* Wait for RPCs on separate threads to complete. */
5557 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5558 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5559 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5561 else if (error == 0 && tdrpc->err != 0)
5569 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5570 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5571 struct nfsvattr *dsnap)
5574 struct nfsrv_descript *nd;
5576 nfsattrbit_t attrbits;
5579 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5580 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5582 * Use a stateid where other is an alternating 01010 pattern and
5583 * seqid is 0xffffffff. This value is not defined as special by
5584 * the RFC and is used by the FreeBSD NFS server to indicate an
5585 * MDS->DS proxy operation.
5587 st.other[0] = 0x55555555;
5588 st.other[1] = 0x55555555;
5589 st.other[2] = 0x55555555;
5590 st.seqid = 0xffffffff;
5591 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5593 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5594 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5596 /* Do a Getattr for the attributes that change due to writing. */
5597 NFSZERO_ATTRBIT(&attrbits);
5598 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5599 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5600 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5601 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5602 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5603 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5604 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5605 (void) nfsrv_putattrbit(nd, &attrbits);
5606 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5607 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5612 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5614 /* Get rid of weak cache consistency data for now. */
5615 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5616 (ND_NFSV4 | ND_V4WCCATTR)) {
5617 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5618 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5619 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5623 * Get rid of Op# and status for next op.
5625 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5627 nd->nd_flag |= ND_NOMOREDATA;
5629 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5632 if (nd->nd_repstat != 0)
5633 error = nd->nd_repstat;
5635 * Get the Change, Size, Access Time and Modify Time attributes and set
5636 * on the Metadata file, so its attributes will be what the file's
5637 * would be if it had been written.
5640 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5641 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5642 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5644 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5646 m_freem(nd->nd_mrep);
5648 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5652 struct nfsrvsetattrdsdorpc {
5657 struct nfsmount *nmp;
5662 struct nfsvattr dsna;
5667 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5670 start_setattrdsdorpc(void *arg, int pending)
5672 struct nfsrvsetattrdsdorpc *drpc;
5674 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5675 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5676 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5681 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5682 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5683 struct nfsvattr *nap, int *failposp)
5685 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5687 int error, i, ret, timo;
5689 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5692 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5696 * Do the setattr RPC for every DS, using a separate kernel process
5697 * for every DS except the last one.
5700 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5703 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5711 if (nfs_pnfsiothreads != 0) {
5712 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5713 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5717 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5719 if (nfsds_failerr(ret) && *failposp == -1)
5721 else if (error == 0 && ret != 0)
5727 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5728 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5729 *failposp = mirrorcnt - 1;
5730 else if (error == 0 && ret != 0)
5733 error = nfsrv_setextattr(vp, &na, p);
5734 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5736 timo = hz / 50; /* Wait for 20msec. */
5739 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5740 /* Wait for RPCs on separate threads to complete. */
5741 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5742 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5743 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5745 else if (error == 0 && tdrpc->err != 0)
5753 * Do a Setattr of an NFSv4 ACL on the DS file.
5756 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5757 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5759 struct nfsrv_descript *nd;
5761 nfsattrbit_t attrbits;
5764 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5765 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5767 * Use a stateid where other is an alternating 01010 pattern and
5768 * seqid is 0xffffffff. This value is not defined as special by
5769 * the RFC and is used by the FreeBSD NFS server to indicate an
5770 * MDS->DS proxy operation.
5772 st.other[0] = 0x55555555;
5773 st.other[1] = 0x55555555;
5774 st.other[2] = 0x55555555;
5775 st.seqid = 0xffffffff;
5776 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5778 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5779 NFSZERO_ATTRBIT(&attrbits);
5780 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5782 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5783 * so passing in the metadata "vp" will be ok, since it is of
5784 * the same type (VREG).
5786 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5787 NULL, 0, 0, 0, 0, 0, NULL);
5788 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5789 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5794 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5796 error = nd->nd_repstat;
5797 m_freem(nd->nd_mrep);
5802 struct nfsrvsetacldsdorpc {
5807 struct nfsmount *nmp;
5816 * Start up the thread that will execute nfsrv_setacldsdorpc().
5819 start_setacldsdorpc(void *arg, int pending)
5821 struct nfsrvsetacldsdorpc *drpc;
5823 drpc = (struct nfsrvsetacldsdorpc *)arg;
5824 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5825 drpc->vp, drpc->nmp, drpc->aclp);
5830 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5831 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5834 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
5835 int error, i, ret, timo;
5837 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5840 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5844 * Do the setattr RPC for every DS, using a separate kernel process
5845 * for every DS except the last one.
5848 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5851 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5859 if (nfs_pnfsiothreads != 0) {
5860 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5861 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5865 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5867 if (nfsds_failerr(ret) && *failposp == -1)
5869 else if (error == 0 && ret != 0)
5875 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5876 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5877 *failposp = mirrorcnt - 1;
5878 else if (error == 0 && ret != 0)
5880 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5882 timo = hz / 50; /* Wait for 20msec. */
5885 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5886 /* Wait for RPCs on separate threads to complete. */
5887 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5888 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
5889 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5891 else if (error == 0 && tdrpc->err != 0)
5899 * Getattr call to the DS for the attributes that change due to writing.
5902 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5903 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
5905 struct nfsrv_descript *nd;
5907 nfsattrbit_t attrbits;
5909 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
5910 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5911 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
5912 sizeof(fhandle_t), NULL, NULL, 0, 0);
5913 NFSZERO_ATTRBIT(&attrbits);
5914 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5915 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5916 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5917 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5918 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5919 (void) nfsrv_putattrbit(nd, &attrbits);
5920 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5921 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5926 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
5928 if (nd->nd_repstat == 0) {
5929 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5930 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5933 * We can only save the updated values in the extended
5934 * attribute if the vp is exclusively locked.
5935 * This should happen when any of the following operations
5936 * occur on the vnode:
5937 * Close, Delegreturn, LayoutCommit, LayoutReturn
5938 * As such, the updated extended attribute should get saved
5939 * before nfsrv_checkdsattr() returns 0 and allows the cached
5940 * attributes to be returned without calling this function.
5942 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
5943 error = nfsrv_setextattr(vp, nap, p);
5944 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
5948 error = nd->nd_repstat;
5949 m_freem(nd->nd_mrep);
5951 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
5956 * Seek call to a DS.
5959 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
5960 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
5963 struct nfsrv_descript *nd;
5967 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
5969 * Use a stateid where other is an alternating 01010 pattern and
5970 * seqid is 0xffffffff. This value is not defined as special by
5971 * the RFC and is used by the FreeBSD NFS server to indicate an
5972 * MDS->DS proxy operation.
5974 st.other[0] = 0x55555555;
5975 st.other[1] = 0x55555555;
5976 st.other[2] = 0x55555555;
5977 st.seqid = 0xffffffff;
5978 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5979 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
5980 sizeof(fhandle_t), NULL, NULL, 0, 0);
5981 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5982 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5983 txdr_hyper(*offp, tl); tl += 2;
5984 *tl = txdr_unsigned(content);
5985 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5986 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5991 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
5992 if (nd->nd_repstat == 0) {
5993 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
5994 if (*tl++ == newnfs_true)
5998 *offp = fxdr_hyper(tl);
6000 error = nd->nd_repstat;
6002 m_freem(nd->nd_mrep);
6004 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6009 * Get the device id and file handle for a DS file.
6012 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6013 fhandle_t *fhp, char *devid)
6019 buf = malloc(buflen, M_TEMP, M_WAITOK);
6020 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6021 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6027 * Do a Lookup against the DS for the filename.
6030 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6031 struct vnode **nvpp, NFSPROC_T *p)
6033 struct nameidata named;
6034 struct ucred *tcred;
6040 tcred = newnfs_getcred();
6041 named.ni_cnd.cn_nameiop = LOOKUP;
6042 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6043 named.ni_cnd.cn_cred = tcred;
6044 named.ni_cnd.cn_thread = p;
6045 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
6046 nfsvno_setpathbuf(&named, &bufp, &hashp);
6047 named.ni_cnd.cn_nameptr = bufp;
6048 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6049 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6050 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6051 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6052 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6054 nfsvno_relpathbuf(&named);
6057 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6062 * Set the file handle to the correct one.
6065 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6066 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6072 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6074 * We can only do a vn_set_extattr() if the vnode is exclusively
6075 * locked and vn_start_write() has been done. If devid != NULL or
6076 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6077 * may not have been done.
6078 * If not done now, it will be done on a future call.
6080 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6082 ret = vn_extattr_set(vp, IO_NODELOCKED,
6083 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6085 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6089 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6090 * when the DS has failed.
6093 nfsrv_killrpcs(struct nfsmount *nmp)
6097 * Call newnfs_nmcancelreqs() to cause
6098 * any RPCs in progress on the mount point to
6100 * This will cause any process waiting for an
6101 * RPC to complete while holding a vnode lock
6102 * on the mounted-on vnode (such as "df" or
6103 * a non-forced "umount") to fail.
6104 * This will unlock the mounted-on vnode so
6105 * a forced dismount can succeed.
6106 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6109 newnfs_nmcancelreqs(nmp);
6113 * Sum up the statfs info for each of the DSs, so that the client will
6114 * receive the total for all DSs.
6117 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6120 struct nfsdevice *ds;
6121 struct vnode **dvpp, **tdvpp, *dvp;
6123 int cnt, error = 0, i;
6125 if (nfsrv_devidcnt <= 0)
6127 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6128 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6130 /* Get an array of the dvps for the DSs. */
6134 /* First, search for matches for same file system. */
6135 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6136 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6137 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6138 if (++i > nfsrv_devidcnt)
6140 *tdvpp++ = ds->nfsdev_dvp;
6144 * If no matches for same file system, total all servers not assigned
6148 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6149 if (ds->nfsdev_nmp != NULL &&
6150 ds->nfsdev_mdsisset == 0) {
6151 if (++i > nfsrv_devidcnt)
6153 *tdvpp++ = ds->nfsdev_dvp;
6160 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6162 for (i = 0; i < cnt && error == 0; i++) {
6164 error = VFS_STATFS(dvp->v_mount, tsf);
6166 if (sf->f_bsize == 0) {
6167 if (tsf->f_bsize > 0)
6168 sf->f_bsize = tsf->f_bsize;
6172 if (tsf->f_blocks > 0) {
6173 if (sf->f_bsize != tsf->f_bsize) {
6174 tot = tsf->f_blocks * tsf->f_bsize;
6175 sf->f_blocks += (tot / sf->f_bsize);
6177 sf->f_blocks += tsf->f_blocks;
6179 if (tsf->f_bfree > 0) {
6180 if (sf->f_bsize != tsf->f_bsize) {
6181 tot = tsf->f_bfree * tsf->f_bsize;
6182 sf->f_bfree += (tot / sf->f_bsize);
6184 sf->f_bfree += tsf->f_bfree;
6186 if (tsf->f_bavail > 0) {
6187 if (sf->f_bsize != tsf->f_bsize) {
6188 tot = tsf->f_bavail * tsf->f_bsize;
6189 sf->f_bavail += (tot / sf->f_bsize);
6191 sf->f_bavail += tsf->f_bavail;
6204 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6208 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6209 error = NFSERR_ATTRNOTSUPP;
6213 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6214 * Make sure it has enough room for that - splitting every entry
6215 * into two and appending "canonical six" entries at the end.
6216 * Cribbed out of kern/vfs_acl.c - Rick M.
6218 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6219 error = NFSERR_ATTRNOTSUPP;
6222 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6224 error = nfsrv_dssetacl(vp, aclp, cred, p);
6225 if (error == ENOENT)
6235 * Seek vnode op call (actually it is a VOP_IOCTL()).
6236 * This function is called with the vnode locked, but unlocks and vrele()s
6237 * the vp before returning.
6240 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6241 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6246 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6248 * Attempt to seek on a DS file. A return of ENOENT implies
6249 * there is no DS file to seek on.
6251 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6252 NULL, NULL, NULL, NULL, offp, content, eofp);
6253 if (error != ENOENT) {
6259 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6260 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6261 * NFSv4.2 is *eofp == true and error == 0 for this case.
6264 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6266 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6267 /* Handle the cases where we might be at EOF. */
6268 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6269 if (ret == 0 && *offp == at.na_size) {
6273 if (ret != 0 && error == 0)
6282 * Allocate vnode op call.
6285 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6290 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6292 * Attempt to allocate on a DS file. A return of ENOENT implies
6293 * there is no DS file to allocate on.
6295 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6296 NULL, NULL, NULL, NULL, &len, 0, NULL);
6297 if (error != ENOENT)
6302 * Do the actual VOP_ALLOCATE(), looping a reasonable number of
6303 * times to achieve completion.
6306 while (error == 0 && len > 0 && trycnt++ < 20)
6307 error = VOP_ALLOCATE(vp, &off, &len);
6308 if (error == 0 && len > 0)
6315 * Get Extended Atribute vnode op into an mbuf list.
6318 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6319 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6320 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6323 struct uio io, *uiop = &io;
6324 struct mbuf *m, *m2;
6325 int alen, error, len, tlen;
6328 /* First, find out the size of the extended attribute. */
6329 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6332 return (NFSERR_NOXATTR);
6333 if (siz > maxresp - NFS_MAXXDR)
6334 return (NFSERR_XATTR2BIG);
6336 tlen = NFSM_RNDUP(len);
6339 * If cnt > MCLBYTES and the reply will not be saved, use
6340 * ext_pgs mbufs for TLS.
6341 * For NFSv4.0, we do not know for sure if the reply will
6342 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6343 * Always use ext_pgs mbufs if ND_EXTPG is set.
6345 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6346 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6347 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6348 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6349 maxextsiz, &m, &m2, &iv);
6351 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6355 uiop->uio_iovcnt = 0;
6356 uiop->uio_iov = iv = NULL;
6359 uiop->uio_offset = 0;
6360 uiop->uio_resid = tlen;
6361 uiop->uio_rw = UIO_READ;
6362 uiop->uio_segflg = UIO_SYSSPACE;
6365 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6372 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6376 if (uiop->uio_resid > 0) {
6378 len = tlen - uiop->uio_resid;
6379 tlen = NFSM_RNDUP(len);
6381 printf("nfsvno_getxattr: weird size read\n");
6385 } else if (alen != tlen || tlen != len)
6386 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6404 * Set Extended attribute vnode op from an mbuf list.
6407 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6408 char *cp, struct ucred *cred, struct thread *p)
6411 struct uio uio, *uiop = &uio;
6416 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6422 uiop->uio_rw = UIO_WRITE;
6423 uiop->uio_segflg = UIO_SYSSPACE;
6425 uiop->uio_offset = 0;
6426 uiop->uio_resid = len;
6428 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6430 uiop->uio_iovcnt = cnt;
6432 uiop->uio_iov = iv = NULL;
6433 uiop->uio_iovcnt = 0;
6436 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6447 * Remove Extended attribute vnode op.
6450 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6451 struct ucred *cred, struct thread *p)
6456 * Get rid of any delegations. I am not sure why this is required,
6457 * but RFC-8276 says so.
6459 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6463 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6469 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6470 if (error == EOPNOTSUPP)
6471 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6479 * List Extended Atribute vnode op into an mbuf list.
6482 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6483 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6491 /* First, find out the size of the extended attribute. */
6492 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6495 return (NFSERR_NOXATTR);
6496 if (siz <= cookie) {
6501 if (siz > cookie + *lenp) {
6502 siz = cookie + *lenp;
6506 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6507 if (siz > 10 * 1024 * 1024) {
6508 error = NFSERR_XATTR2BIG;
6511 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6512 iv.iov_base = *bufp;
6518 io.uio_rw = UIO_READ;
6519 io.uio_segflg = UIO_SYSSPACE;
6522 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6527 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6531 if (io.uio_resid > 0)
6532 siz -= io.uio_resid;
6537 free(*bufp, M_TEMP);
6545 * Trim trailing data off the mbuf list being built.
6548 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6549 int bextpg, int bextpgsiz)
6554 if (mb->m_next != NULL) {
6555 m_freem(mb->m_next);
6558 if ((mb->m_flags & M_EXTPG) != 0) {
6559 /* First, get rid of any pages after this position. */
6560 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
6561 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
6562 vm_page_unwire_noq(pg);
6565 mb->m_epg_npgs = bextpg + 1;
6567 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
6569 fullpgsiz = PAGE_SIZE;
6570 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
6571 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
6572 for (i = 1; i < mb->m_epg_npgs; i++)
6573 mb->m_len += m_epg_pagelen(mb, i, 0);
6574 nd->nd_bextpgsiz = bextpgsiz;
6575 nd->nd_bextpg = bextpg;
6577 mb->m_len = bpos - mtod(mb, char *);
6582 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
6585 * Called once to initialize data structures...
6588 nfsd_modevent(module_t mod, int type, void *data)
6591 static int loaded = 0;
6598 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6599 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
6601 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
6604 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
6605 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
6606 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
6607 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
6608 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
6609 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
6616 #ifdef VV_DISABLEDELEG
6617 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
6618 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
6620 nfsd_call_servertimer = nfsrv_servertimer;
6621 nfsd_call_nfsd = nfssvc_nfsd;
6626 if (newnfs_numnfsd != 0) {
6631 #ifdef VV_DISABLEDELEG
6632 vn_deleg_ops.vndeleg_recall = NULL;
6633 vn_deleg_ops.vndeleg_disable = NULL;
6635 nfsd_call_servertimer = NULL;
6636 nfsd_call_nfsd = NULL;
6638 /* Clean out all NFSv4 state. */
6639 nfsrv_throwawayallstate(curthread);
6641 /* Clean the NFS server reply cache */
6642 nfsrvd_cleancache();
6644 /* Free up the krpc server pool. */
6645 if (nfsrvd_pool != NULL)
6646 svcpool_destroy(nfsrvd_pool);
6648 /* and get rid of the locks */
6649 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6650 mtx_destroy(&nfsrchash_table[i].mtx);
6651 mtx_destroy(&nfsrcahash_table[i].mtx);
6653 mtx_destroy(&nfsrc_udpmtx);
6654 mtx_destroy(&nfs_v4root_mutex);
6655 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
6656 mtx_destroy(&nfsrv_dontlistlock_mtx);
6657 mtx_destroy(&nfsrv_recalllock_mtx);
6658 for (i = 0; i < nfsrv_sessionhashsize; i++)
6659 mtx_destroy(&nfssessionhash[i].mtx);
6660 if (nfslayouthash != NULL) {
6661 for (i = 0; i < nfsrv_layouthashsize; i++)
6662 mtx_destroy(&nfslayouthash[i].mtx);
6663 free(nfslayouthash, M_NFSDSESSION);
6665 lockdestroy(&nfsv4root_mnt.mnt_explock);
6666 free(nfsclienthash, M_NFSDCLIENT);
6667 free(nfslockhash, M_NFSDLOCKFILE);
6668 free(nfssessionhash, M_NFSDSESSION);
6680 static moduledata_t nfsd_mod = {
6685 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
6687 /* So that loader and kldload(2) can find us, wherever we are.. */
6688 MODULE_VERSION(nfsd, 1);
6689 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
6690 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
6691 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
6692 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);