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];
214 * Heuristic to detect sequential operation.
216 static struct nfsheur *
217 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
222 /* Locate best candidate. */
224 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
227 if (nfsheur[hi].nh_vp == vp) {
231 if (nfsheur[hi].nh_use > 0)
232 --nfsheur[hi].nh_use;
233 hi = (hi + 1) % NUM_HEURISTIC;
234 if (nfsheur[hi].nh_use < nh->nh_use)
238 /* Initialize hint if this is a new file. */
239 if (nh->nh_vp != vp) {
241 nh->nh_nextoff = uio->uio_offset;
242 nh->nh_use = NHUSE_INIT;
243 if (uio->uio_offset == 0)
249 /* Calculate heuristic. */
250 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
251 uio->uio_offset == nh->nh_nextoff) {
252 /* See comments in vfs_vnops.c:sequential_heuristic(). */
253 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
254 if (nh->nh_seqcount > IO_SEQMAX)
255 nh->nh_seqcount = IO_SEQMAX;
256 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
257 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
258 /* Probably a reordered RPC, leave seqcount alone. */
259 } else if (nh->nh_seqcount > 1) {
260 nh->nh_seqcount /= 2;
264 nh->nh_use += NHUSE_INC;
265 if (nh->nh_use > NHUSE_MAX)
266 nh->nh_use = NHUSE_MAX;
271 * Get attributes into nfsvattr structure.
274 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
275 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
276 nfsattrbit_t *attrbitp)
278 int error, gotattr, lockedit = 0;
281 if (vpislocked == 0) {
283 * When vpislocked == 0, the vnode is either exclusively
284 * locked by this thread or not locked by this thread.
285 * As such, shared lock it, if not exclusively locked.
287 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
289 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
294 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
295 * attributes, as required.
296 * This needs to be done for regular files if:
297 * - non-NFSv4 RPCs or
298 * - when attrbitp == NULL or
299 * - an NFSv4 RPC with any of the above attributes in attrbitp.
300 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
301 * these attributes. nfsrv_proxyds() will return an error if the
302 * server is not a pNFS one.
305 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
306 (nd->nd_flag & ND_NFSV4) == 0 ||
307 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
308 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
309 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
310 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
311 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
312 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
313 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
319 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
324 * If we got the Change, Size and Modify Time from the DS,
328 nvap->na_atime = na.na_atime;
329 nvap->na_mtime = na.na_mtime;
330 nvap->na_filerev = na.na_filerev;
331 nvap->na_size = na.na_size;
332 nvap->na_bytes = na.na_bytes;
334 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
335 error, (uintmax_t)na.na_filerev);
342 * Get a file handle for a vnode.
345 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
349 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
350 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
351 error = VOP_VPTOFH(vp, &fhp->fh_fid);
358 * Perform access checking for vnodes obtained from file handles that would
359 * refer to files already opened by a Unix client. You cannot just use
360 * vn_writechk() and VOP_ACCESSX() for two reasons.
361 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
363 * 2 - The owner is to be given access irrespective of mode bits for some
364 * operations, so that processes that chmod after opening a file don't
368 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
369 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
370 u_int32_t *supportedtypep)
373 int error = 0, getret = 0;
375 if (vpislocked == 0) {
376 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
381 if (accmode & VWRITE) {
382 /* Just vn_writechk() changed to check rdonly */
384 * Disallow write attempts on read-only file systems;
385 * unless the file is a socket or a block or character
386 * device resident on the file system.
388 if (NFSVNO_EXRDONLY(exp) ||
389 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
390 switch (vp->v_type) {
400 * If there's shared text associated with
401 * the inode, try to free it up once. If
402 * we fail, we can't allow writing.
404 if (VOP_IS_TEXT(vp) && error == 0)
414 * Should the override still be applied when ACLs are enabled?
416 error = VOP_ACCESSX(vp, accmode, cred, p);
417 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
419 * Try again with VEXPLICIT_DENY, to see if the test for
420 * deletion is supported.
422 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
424 if (vp->v_type == VDIR) {
425 accmode &= ~(VDELETE | VDELETE_CHILD);
427 error = VOP_ACCESSX(vp, accmode, cred, p);
428 } else if (supportedtypep != NULL) {
429 *supportedtypep &= ~NFSACCESS_DELETE;
435 * Allow certain operations for the owner (reads and writes
436 * on files that are already open).
438 if (override != NFSACCCHK_NOOVERRIDE &&
439 (error == EPERM || error == EACCES)) {
440 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
442 else if (override & NFSACCCHK_ALLOWOWNER) {
443 getret = VOP_GETATTR(vp, &vattr, cred);
444 if (getret == 0 && cred->cr_uid == vattr.va_uid)
457 * Set attribute(s) vnop.
460 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
461 struct thread *p, struct nfsexstuff *exp)
463 u_quad_t savsize = 0;
468 * If this is an exported file system and a pNFS service is running,
469 * don't VOP_SETATTR() of size for the MDS file system.
473 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
474 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
475 nvap->na_vattr.va_size > 0) {
476 savsize = nvap->na_vattr.va_size;
477 nvap->na_vattr.va_size = VNOVAL;
478 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
479 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
480 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
481 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
482 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
488 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
490 nvap->na_vattr.va_size = savsize;
491 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
492 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
493 nvap->na_vattr.va_size != VNOVAL ||
494 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
495 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
496 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
497 /* Never modify birthtime on a DS file. */
498 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
499 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
500 /* For a pNFS server, set the attributes on the DS file. */
501 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
502 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
503 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
512 * Set up nameidata for a lookup() call and do it.
515 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
516 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
517 struct vnode **retdirp)
519 struct componentname *cnp = &ndp->ni_cnd;
523 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
528 cnp->cn_nameptr = cnp->cn_pnbuf;
531 * Extract and set starting directory.
533 if (dp->v_type != VDIR) {
538 nfsvno_relpathbuf(ndp);
546 if (NFSVNO_EXRDONLY(exp))
547 cnp->cn_flags |= RDONLY;
548 ndp->ni_segflg = UIO_SYSSPACE;
550 if (nd->nd_flag & ND_PUBLOOKUP) {
552 if (cnp->cn_pnbuf[0] == '/') {
555 * Check for degenerate pathnames here, since lookup()
558 for (i = 1; i < ndp->ni_pathlen; i++)
559 if (cnp->cn_pnbuf[i] != '/')
561 if (i == ndp->ni_pathlen) {
562 error = NFSERR_ACCES;
568 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
569 (nd->nd_flag & ND_NFSV4) == 0) {
571 * Only cross mount points for NFSv4 when doing a
572 * mount while traversing the file system above
573 * the mount point, unless nfsrv_enable_crossmntpt is set.
575 cnp->cn_flags |= NOCROSSMOUNT;
579 * Initialize for scan, set ni_startdir and bump ref on dp again
580 * because lookup() will dereference ni_startdir.
584 ndp->ni_startdir = dp;
585 ndp->ni_rootdir = rootvnode;
586 ndp->ni_topdir = NULL;
589 cnp->cn_flags |= LOCKLEAF;
591 cnp->cn_nameptr = cnp->cn_pnbuf;
593 * Call lookup() to do the real work. If an error occurs,
594 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
595 * we do not have to dereference anything before returning.
596 * In either case ni_startdir will be dereferenced and NULLed
604 * Check for encountering a symbolic link. Trivial
605 * termination occurs if no symlink encountered.
607 if ((cnp->cn_flags & ISSYMLINK) == 0) {
608 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
609 nfsvno_relpathbuf(ndp);
610 if (ndp->ni_vp && !lockleaf)
611 NFSVOPUNLOCK(ndp->ni_vp);
618 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
619 NFSVOPUNLOCK(ndp->ni_dvp);
620 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
625 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
629 if (ndp->ni_pathlen > 1)
630 cp = uma_zalloc(namei_zone, M_WAITOK);
634 aiov.iov_len = MAXPATHLEN;
635 auio.uio_iov = &aiov;
638 auio.uio_rw = UIO_READ;
639 auio.uio_segflg = UIO_SYSSPACE;
641 auio.uio_resid = MAXPATHLEN;
642 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
645 if (ndp->ni_pathlen > 1)
646 uma_zfree(namei_zone, cp);
652 linklen = MAXPATHLEN - auio.uio_resid;
657 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
658 error = ENAMETOOLONG;
663 * Adjust or replace path
665 if (ndp->ni_pathlen > 1) {
666 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
667 uma_zfree(namei_zone, cnp->cn_pnbuf);
670 cnp->cn_pnbuf[linklen] = '\0';
671 ndp->ni_pathlen += linklen;
674 * Cleanup refs for next loop and check if root directory
675 * should replace current directory. Normally ni_dvp
676 * becomes the new base directory and is cleaned up when
677 * we loop. Explicitly null pointers after invalidation
678 * to clarify operation.
683 if (cnp->cn_pnbuf[0] == '/') {
685 ndp->ni_dvp = ndp->ni_rootdir;
688 ndp->ni_startdir = ndp->ni_dvp;
692 cnp->cn_flags &= ~LOCKLEAF;
696 nfsvno_relpathbuf(ndp);
699 ndp->ni_startdir = NULL;
700 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
705 NFSEXITCODE2(error, nd);
710 * Set up a pathname buffer and return a pointer to it and, optionally
711 * set a hash pointer.
714 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
716 struct componentname *cnp = &ndp->ni_cnd;
718 cnp->cn_flags |= (NOMACCHECK | HASBUF);
719 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
722 *bufpp = cnp->cn_pnbuf;
726 * Release the above path buffer, if not released by nfsvno_namei().
729 nfsvno_relpathbuf(struct nameidata *ndp)
732 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
734 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
735 ndp->ni_cnd.cn_flags &= ~HASBUF;
739 * Readlink vnode op into an mbuf list.
742 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
743 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
746 struct uio io, *uiop = &io;
747 struct mbuf *mp, *mp3;
748 int len, tlen, error = 0;
750 len = NFS_MAXPATHLEN;
752 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
755 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
757 uiop->uio_offset = 0;
758 uiop->uio_resid = len;
759 uiop->uio_rw = UIO_READ;
760 uiop->uio_segflg = UIO_SYSSPACE;
762 error = VOP_READLINK(vp, uiop, cred);
769 if (uiop->uio_resid > 0) {
770 len -= uiop->uio_resid;
771 tlen = NFSM_RNDUP(len);
775 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
776 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
789 * Create an mbuf chain and an associated iovec that can be used to Read
790 * or Getextattr of data.
791 * Upon success, return pointers to the first and last mbufs in the chain
792 * plus the malloc'd iovec and its iovlen.
795 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
798 struct mbuf *m, *m2 = NULL, *m3;
805 * Generate the mbuf list with the uio_iov ref. to it.
812 siz = min(M_TRAILINGSPACE(m), left);
821 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
827 panic("nfsrv_createiovec iov");
828 siz = min(M_TRAILINGSPACE(m), left);
830 iv->iov_base = mtod(m, caddr_t) + m->m_len;
845 * Create an mbuf chain and an associated iovec that can be used to Read
846 * or Getextattr of data.
847 * Upon success, return pointers to the first and last mbufs in the chain
848 * plus the malloc'd iovec and its iovlen.
849 * Same as above, but creates ext_pgs mbuf(s).
852 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
853 struct mbuf **mpendp, struct iovec **ivp)
855 struct mbuf *m, *m2 = NULL, *m3;
857 int i, left, pgno, siz;
862 * Generate the mbuf list with the uio_iov ref. to it.
866 siz = min(left, maxextsiz);
867 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
876 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
883 panic("nfsvno_createiovec_extpgs iov");
884 siz = min(PAGE_SIZE, left);
886 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
889 if (pgno == m->m_epg_npgs - 1)
890 m->m_epg_last_len = siz;
896 if (pgno == m->m_epg_npgs && left > 0) {
899 panic("nfsvno_createiovec_extpgs iov");
909 * Read vnode op call into mbuf list.
912 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
913 int maxextsiz, struct thread *p, struct mbuf **mpp,
914 struct mbuf **mpendp)
918 int error = 0, len, tlen, ioflag = 0;
920 struct uio io, *uiop = &io;
924 * Attempt to read from a DS file. A return of ENOENT implies
925 * there is no DS file to read.
927 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
928 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
932 len = NFSM_RNDUP(cnt);
934 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
937 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
939 uiop->uio_offset = off;
940 uiop->uio_resid = len;
941 uiop->uio_rw = UIO_READ;
942 uiop->uio_segflg = UIO_SYSSPACE;
944 nh = nfsrv_sequential_heuristic(uiop, vp);
945 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
946 /* XXX KDM make this more systematic? */
947 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
948 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
955 nh->nh_nextoff = uiop->uio_offset;
956 tlen = len - uiop->uio_resid;
957 cnt = cnt < tlen ? cnt : tlen;
958 tlen = NFSM_RNDUP(cnt);
962 } else if (len != tlen || tlen != cnt)
963 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
973 * Create the iovec for the mbuf chain passed in as an argument.
974 * The "cp" argument is where the data starts within the first mbuf in
975 * the chain. It returns the iovec and the iovcnt.
978 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
986 * Loop through the mbuf chain, counting how many mbufs are a
987 * part of this write operation, so the iovec size is known.
992 i = mtod(mp, caddr_t) + mp->m_len - cp;
1006 /* Now, create the iovec. */
1008 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1011 i = mtod(mp, caddr_t) + mp->m_len - cp;
1015 panic("nfsrv_createiovecw");
1026 cp = mtod(mp, caddr_t);
1033 * Write vnode op from an mbuf list.
1036 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1037 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1040 int cnt, ioflags, error;
1041 struct uio io, *uiop = &io;
1045 * Attempt to write to a DS file. A return of ENOENT implies
1046 * there is no DS file to write.
1048 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1049 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1050 if (error != ENOENT) {
1051 *stable = NFSWRITE_FILESYNC;
1055 if (*stable == NFSWRITE_UNSTABLE)
1056 ioflags = IO_NODELOCKED;
1058 ioflags = (IO_SYNC | IO_NODELOCKED);
1059 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1063 uiop->uio_iovcnt = cnt;
1064 uiop->uio_resid = retlen;
1065 uiop->uio_rw = UIO_WRITE;
1066 uiop->uio_segflg = UIO_SYSSPACE;
1067 NFSUIOPROC(uiop, p);
1068 uiop->uio_offset = off;
1069 nh = nfsrv_sequential_heuristic(uiop, vp);
1070 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1071 /* XXX KDM make this more systematic? */
1072 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1073 error = VOP_WRITE(vp, uiop, ioflags, cred);
1075 nh->nh_nextoff = uiop->uio_offset;
1083 * Common code for creating a regular file (plus special files for V2).
1086 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1087 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1088 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1092 struct thread *p = curthread;
1094 error = nd->nd_repstat;
1095 if (!error && ndp->ni_vp == NULL) {
1096 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1097 vrele(ndp->ni_startdir);
1098 error = VOP_CREATE(ndp->ni_dvp,
1099 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1100 /* For a pNFS server, create the data file on a DS. */
1101 if (error == 0 && nvap->na_type == VREG) {
1103 * Create a data file on a DS for a pNFS server.
1104 * This function just returns if not
1105 * running a pNFS DS or the creation fails.
1107 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1111 nfsvno_relpathbuf(ndp);
1113 if (*exclusive_flagp) {
1114 *exclusive_flagp = 0;
1115 NFSVNO_ATTRINIT(nvap);
1116 nvap->na_atime.tv_sec = cverf[0];
1117 nvap->na_atime.tv_nsec = cverf[1];
1118 error = VOP_SETATTR(ndp->ni_vp,
1119 &nvap->na_vattr, nd->nd_cred);
1123 error = NFSERR_NOTSUPP;
1128 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1129 * (This implies, just get out on an error.)
1131 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1132 nvap->na_type == VFIFO) {
1133 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1134 nvap->na_type = VFIFO;
1135 if (nvap->na_type != VFIFO &&
1136 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1137 vrele(ndp->ni_startdir);
1138 nfsvno_relpathbuf(ndp);
1142 nvap->na_rdev = rdev;
1143 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1144 &ndp->ni_cnd, &nvap->na_vattr);
1146 nfsvno_relpathbuf(ndp);
1147 vrele(ndp->ni_startdir);
1151 vrele(ndp->ni_startdir);
1152 nfsvno_relpathbuf(ndp);
1160 * Handle cases where error is already set and/or
1162 * 1 - clean up the lookup
1163 * 2 - iff !error and na_size set, truncate it
1165 vrele(ndp->ni_startdir);
1166 nfsvno_relpathbuf(ndp);
1168 if (ndp->ni_dvp == *vpp)
1172 if (!error && nvap->na_size != VNOVAL) {
1173 error = nfsvno_accchk(*vpp, VWRITE,
1174 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1175 NFSACCCHK_VPISLOCKED, NULL);
1177 tempsize = nvap->na_size;
1178 NFSVNO_ATTRINIT(nvap);
1179 nvap->na_size = tempsize;
1180 error = VOP_SETATTR(*vpp,
1181 &nvap->na_vattr, nd->nd_cred);
1194 * Do a mknod vnode op.
1197 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1203 vtyp = nvap->na_type;
1205 * Iff doesn't exist, create it.
1208 vrele(ndp->ni_startdir);
1209 nfsvno_relpathbuf(ndp);
1215 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1216 vrele(ndp->ni_startdir);
1217 nfsvno_relpathbuf(ndp);
1219 error = NFSERR_BADTYPE;
1222 if (vtyp == VSOCK) {
1223 vrele(ndp->ni_startdir);
1224 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1225 &ndp->ni_cnd, &nvap->na_vattr);
1227 nfsvno_relpathbuf(ndp);
1229 if (nvap->na_type != VFIFO &&
1230 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1231 vrele(ndp->ni_startdir);
1232 nfsvno_relpathbuf(ndp);
1236 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1237 &ndp->ni_cnd, &nvap->na_vattr);
1239 nfsvno_relpathbuf(ndp);
1240 vrele(ndp->ni_startdir);
1242 * Since VOP_MKNOD returns the ni_vp, I can't
1243 * see any reason to do the lookup.
1256 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1257 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1261 if (ndp->ni_vp != NULL) {
1262 if (ndp->ni_dvp == ndp->ni_vp)
1267 nfsvno_relpathbuf(ndp);
1271 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1274 nfsvno_relpathbuf(ndp);
1285 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1286 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1287 struct nfsexstuff *exp)
1292 vrele(ndp->ni_startdir);
1293 nfsvno_relpathbuf(ndp);
1294 if (ndp->ni_dvp == ndp->ni_vp)
1303 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1304 &nvap->na_vattr, pathcp);
1306 vrele(ndp->ni_startdir);
1307 nfsvno_relpathbuf(ndp);
1309 * Although FreeBSD still had the lookup code in
1310 * it for 7/current, there doesn't seem to be any
1311 * point, since VOP_SYMLINK() returns the ni_vp.
1312 * Just vput it for v2.
1314 if (!not_v2 && !error)
1323 * Parse symbolic link arguments.
1324 * This function has an ugly side effect. It will malloc() an area for
1325 * the symlink and set iov_base to point to it, only if it succeeds.
1326 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1330 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1331 struct thread *p, char **pathcpp, int *lenp)
1334 char *pathcp = NULL;
1336 struct nfsv2_sattr *sp;
1340 if ((nd->nd_flag & ND_NFSV3) &&
1341 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1343 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1344 len = fxdr_unsigned(int, *tl);
1345 if (len > NFS_MAXPATHLEN || len <= 0) {
1349 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1350 error = nfsrv_mtostr(nd, pathcp, len);
1353 if (nd->nd_flag & ND_NFSV2) {
1354 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1355 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1359 NFSEXITCODE2(0, nd);
1363 free(pathcp, M_TEMP);
1364 NFSEXITCODE2(error, nd);
1369 * Remove a non-directory object.
1372 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1373 struct thread *p, struct nfsexstuff *exp)
1375 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1376 int error = 0, mirrorcnt;
1377 char fname[PNFS_FILENAME_LEN + 1];
1382 if (vp->v_type == VDIR)
1383 error = NFSERR_ISDIR;
1385 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1388 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1390 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1391 if (error == 0 && dsdvp[0] != NULL)
1392 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1393 if (ndp->ni_dvp == vp)
1398 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1399 nfsvno_relpathbuf(ndp);
1405 * Remove a directory.
1408 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1409 struct thread *p, struct nfsexstuff *exp)
1415 if (vp->v_type != VDIR) {
1420 * No rmdir "." please.
1422 if (ndp->ni_dvp == vp) {
1427 * The root of a mounted filesystem cannot be deleted.
1429 if (vp->v_vflag & VV_ROOT)
1433 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1434 if (ndp->ni_dvp == vp)
1439 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1440 nfsvno_relpathbuf(ndp);
1449 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1450 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1452 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1453 int error = 0, mirrorcnt;
1454 char fname[PNFS_FILENAME_LEN + 1];
1458 fvp = fromndp->ni_vp;
1460 vrele(fromndp->ni_dvp);
1465 tdvp = tondp->ni_dvp;
1468 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1469 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1471 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1472 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1475 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1476 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1481 * A rename to '.' or '..' results in a prematurely
1482 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1485 if ((tondp->ni_cnd.cn_namelen == 1 &&
1486 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1487 (tondp->ni_cnd.cn_namelen == 2 &&
1488 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1489 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1494 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1495 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1498 if (fvp->v_mount != tdvp->v_mount) {
1499 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1503 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1508 * If source and destination are the same, there is nothing to
1509 * do. Set error to -1 to indicate this.
1514 if (ndflag & ND_NFSV4) {
1515 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1516 error = nfsrv_checkremove(fvp, 0, NULL,
1517 (nfsquad_t)((u_quad_t)0), p);
1522 error = nfsrv_checkremove(tvp, 1, NULL,
1523 (nfsquad_t)((u_quad_t)0), p);
1526 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1527 * that the NFSv4 client won't be confused by the rename.
1528 * Since nfsd_recalldelegation() can only be called on an
1529 * unlocked vnode at this point and fvp is the file that will
1530 * still exist after the rename, just do fvp.
1532 nfsd_recalldelegation(fvp, p);
1534 if (error == 0 && tvp != NULL) {
1535 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1536 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1537 " dsdvp=%p\n", dsdvp[0]);
1541 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1542 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1551 vrele(fromndp->ni_dvp);
1558 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1559 * if the rename succeeded, the DS file for the tvp needs to be
1562 if (error == 0 && dsdvp[0] != NULL) {
1563 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1564 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1567 vrele(tondp->ni_startdir);
1568 nfsvno_relpathbuf(tondp);
1570 vrele(fromndp->ni_startdir);
1571 nfsvno_relpathbuf(fromndp);
1580 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1581 struct thread *p, struct nfsexstuff *exp)
1591 if (vp->v_mount != xp->v_mount)
1595 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1596 if (!VN_IS_DOOMED(vp))
1597 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1600 if (ndp->ni_dvp == vp)
1606 if (ndp->ni_dvp == ndp->ni_vp)
1613 nfsvno_relpathbuf(ndp);
1619 * Do the fsync() appropriate for the commit.
1622 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1628 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1629 * file is done. At this time VOP_FSYNC does not accept offset and
1630 * byte count parameters so call VOP_FSYNC the whole file for now.
1631 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1632 * File systems that do not use the buffer cache (as indicated
1633 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1635 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1636 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1638 * Give up and do the whole thing
1640 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1641 VM_OBJECT_WLOCK(vp->v_object);
1642 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1643 VM_OBJECT_WUNLOCK(vp->v_object);
1645 error = VOP_FSYNC(vp, MNT_WAIT, td);
1648 * Locate and synchronously write any buffers that fall
1649 * into the requested range. Note: we are assuming that
1650 * f_iosize is a power of 2.
1652 int iosize = vp->v_mount->mnt_stat.f_iosize;
1653 int iomask = iosize - 1;
1658 * Align to iosize boundary, super-align to page boundary.
1661 cnt += off & iomask;
1662 off &= ~(u_quad_t)iomask;
1664 if (off & PAGE_MASK) {
1665 cnt += off & PAGE_MASK;
1666 off &= ~(u_quad_t)PAGE_MASK;
1668 lblkno = off / iosize;
1670 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1671 VM_OBJECT_WLOCK(vp->v_object);
1672 vm_object_page_clean(vp->v_object, off, off + cnt,
1674 VM_OBJECT_WUNLOCK(vp->v_object);
1683 * If we have a buffer and it is marked B_DELWRI we
1684 * have to lock and write it. Otherwise the prior
1685 * write is assumed to have already been committed.
1687 * gbincore() can return invalid buffers now so we
1688 * have to check that bit as well (though B_DELWRI
1689 * should not be set if B_INVAL is set there could be
1690 * a race here since we haven't locked the buffer).
1692 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1693 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1694 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1696 continue; /* retry */
1698 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1701 bp->b_flags &= ~B_ASYNC;
1724 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1730 if (nfsrv_devidcnt > 0) {
1731 /* For a pNFS service, get the DS numbers. */
1732 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1733 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1739 error = VFS_STATFS(vp->v_mount, sf);
1742 sf->f_blocks = tsf->f_blocks;
1743 sf->f_bavail = tsf->f_bavail;
1744 sf->f_bfree = tsf->f_bfree;
1745 sf->f_bsize = tsf->f_bsize;
1748 * Since NFS handles these values as unsigned on the
1749 * wire, there is no way to represent negative values,
1750 * so set them to 0. Without this, they will appear
1751 * to be very large positive values for clients like
1754 if (sf->f_bavail < 0)
1756 if (sf->f_ffree < 0)
1765 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1766 * must handle nfsrv_opencheck() calls after any other access checks.
1769 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1770 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1771 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1772 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred,
1773 struct nfsexstuff *exp, struct vnode **vpp)
1775 struct vnode *vp = NULL;
1777 struct nfsexstuff nes;
1778 struct thread *p = curthread;
1780 if (ndp->ni_vp == NULL)
1781 nd->nd_repstat = nfsrv_opencheck(clientid,
1782 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1783 if (!nd->nd_repstat) {
1784 if (ndp->ni_vp == NULL) {
1785 vrele(ndp->ni_startdir);
1786 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1787 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1788 /* For a pNFS server, create the data file on a DS. */
1789 if (nd->nd_repstat == 0) {
1791 * Create a data file on a DS for a pNFS server.
1792 * This function just returns if not
1793 * running a pNFS DS or the creation fails.
1795 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1799 nfsvno_relpathbuf(ndp);
1800 if (!nd->nd_repstat) {
1801 if (*exclusive_flagp) {
1802 *exclusive_flagp = 0;
1803 NFSVNO_ATTRINIT(nvap);
1804 nvap->na_atime.tv_sec = cverf[0];
1805 nvap->na_atime.tv_nsec = cverf[1];
1806 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1807 &nvap->na_vattr, cred);
1808 if (nd->nd_repstat != 0) {
1811 nd->nd_repstat = NFSERR_NOTSUPP;
1813 NFSSETBIT_ATTRBIT(attrbitp,
1814 NFSATTRBIT_TIMEACCESS);
1816 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1817 aclp, p, attrbitp, exp);
1822 if (ndp->ni_startdir)
1823 vrele(ndp->ni_startdir);
1824 nfsvno_relpathbuf(ndp);
1826 if (create == NFSV4OPEN_CREATE) {
1827 if (ndp->ni_dvp == vp)
1832 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1833 if (ndp->ni_cnd.cn_flags & RDONLY)
1834 NFSVNO_SETEXRDONLY(&nes);
1836 NFSVNO_EXINIT(&nes);
1837 nd->nd_repstat = nfsvno_accchk(vp,
1838 VWRITE, cred, &nes, p,
1839 NFSACCCHK_NOOVERRIDE,
1840 NFSACCCHK_VPISLOCKED, NULL);
1841 nd->nd_repstat = nfsrv_opencheck(clientid,
1842 stateidp, stp, vp, nd, p, nd->nd_repstat);
1843 if (!nd->nd_repstat) {
1844 tempsize = nvap->na_size;
1845 NFSVNO_ATTRINIT(nvap);
1846 nvap->na_size = tempsize;
1847 nd->nd_repstat = VOP_SETATTR(vp,
1848 &nvap->na_vattr, cred);
1850 } else if (vp->v_type == VREG) {
1851 nd->nd_repstat = nfsrv_opencheck(clientid,
1852 stateidp, stp, vp, nd, p, nd->nd_repstat);
1856 if (ndp->ni_cnd.cn_flags & HASBUF)
1857 nfsvno_relpathbuf(ndp);
1858 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1859 vrele(ndp->ni_startdir);
1860 if (ndp->ni_dvp == ndp->ni_vp)
1870 NFSEXITCODE2(0, nd);
1874 * Updates the file rev and sets the mtime and ctime
1875 * to the current clock time, returning the va_filerev and va_Xtime
1877 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1880 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1881 struct nfsrv_descript *nd, struct thread *p)
1886 vfs_timestamp(&va.va_mtime);
1887 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1888 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1889 if (VN_IS_DOOMED(vp))
1892 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1893 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1898 * Glue routine to nfsv4_fillattr().
1901 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1902 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1903 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1904 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1910 if (nfsrv_devidcnt > 0 &&
1911 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1912 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1913 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1914 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1915 error = nfsrv_pnfsstatfs(sf, mp);
1921 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1922 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1923 mounted_on_fileno, sf);
1925 NFSEXITCODE2(0, nd);
1929 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1931 * nfs readdir service
1932 * - mallocs what it thinks is enough to read
1933 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1934 * - calls VOP_READDIR()
1935 * - loops around building the reply
1936 * if the output generated exceeds count break out of loop
1937 * The NFSM_CLGET macro is used here so that the reply will be packed
1938 * tightly in mbuf clusters.
1939 * - it trims out records with d_fileno == 0
1940 * this doesn't matter for Unix clients, but they might confuse clients
1942 * - it trims out records with d_type == DT_WHT
1943 * these cannot be seen through NFS (unless we extend the protocol)
1944 * The alternate call nfsrvd_readdirplus() does lookups as well.
1945 * PS: The NFS protocol spec. does not clarify what the "count" byte
1946 * argument is a count of.. just name strings and file id's or the
1947 * entire reply rpc or ...
1948 * I tried just file name and id sizes and it confused the Sun client,
1949 * so I am using the full rpc size now. The "paranoia.." comment refers
1950 * to including the status longwords that are not a part of the dir.
1951 * "entry" structures, but are in the rpc.
1954 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1955 struct vnode *vp, struct nfsexstuff *exp)
1960 char *cpos, *cend, *rbuf;
1962 int nlen, error = 0, getret = 1;
1963 int siz, cnt, fullsiz, eofflag, ncookies;
1964 u_int64_t off, toff, verf __unused;
1965 u_long *cookies = NULL, *cookiep;
1969 struct thread *p = curthread;
1971 if (nd->nd_repstat) {
1972 nfsrv_postopattr(nd, getret, &at);
1975 if (nd->nd_flag & ND_NFSV2) {
1976 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1977 off = fxdr_unsigned(u_quad_t, *tl++);
1979 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1980 off = fxdr_hyper(tl);
1982 verf = fxdr_hyper(tl);
1986 cnt = fxdr_unsigned(int, *tl);
1987 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1988 cnt = NFS_SRVMAXDATA(nd);
1989 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1991 if (nd->nd_flag & ND_NFSV3) {
1992 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
1996 * va_filerev is not sufficient as a cookie verifier,
1997 * since it is not supposed to change when entries are
1998 * removed/added unless that offset cookies returned to
1999 * the client are no longer valid.
2001 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2002 nd->nd_repstat = NFSERR_BAD_COOKIE;
2005 if (!nd->nd_repstat && vp->v_type != VDIR)
2006 nd->nd_repstat = NFSERR_NOTDIR;
2007 if (nd->nd_repstat == 0 && cnt == 0) {
2008 if (nd->nd_flag & ND_NFSV2)
2009 /* NFSv2 does not have NFSERR_TOOSMALL */
2010 nd->nd_repstat = EPERM;
2012 nd->nd_repstat = NFSERR_TOOSMALL;
2014 if (!nd->nd_repstat)
2015 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2016 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2017 NFSACCCHK_VPISLOCKED, NULL);
2018 if (nd->nd_repstat) {
2020 if (nd->nd_flag & ND_NFSV3)
2021 nfsrv_postopattr(nd, getret, &at);
2024 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2025 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2029 free(cookies, M_TEMP);
2037 io.uio_offset = (off_t)off;
2039 io.uio_segflg = UIO_SYSSPACE;
2040 io.uio_rw = UIO_READ;
2042 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2044 off = (u_int64_t)io.uio_offset;
2046 siz -= io.uio_resid;
2048 if (!cookies && !nd->nd_repstat)
2049 nd->nd_repstat = NFSERR_PERM;
2050 if (nd->nd_flag & ND_NFSV3) {
2051 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2052 if (!nd->nd_repstat)
2053 nd->nd_repstat = getret;
2057 * Handles the failed cases. nd->nd_repstat == 0 past here.
2059 if (nd->nd_repstat) {
2063 free(cookies, M_TEMP);
2064 if (nd->nd_flag & ND_NFSV3)
2065 nfsrv_postopattr(nd, getret, &at);
2069 * If nothing read, return eof
2074 if (nd->nd_flag & ND_NFSV2) {
2075 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2077 nfsrv_postopattr(nd, getret, &at);
2078 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2079 txdr_hyper(at.na_filerev, tl);
2082 *tl++ = newnfs_false;
2085 free(cookies, M_TEMP);
2090 * Check for degenerate cases of nothing useful read.
2091 * If so go try again
2095 dp = (struct dirent *)cpos;
2099 * For some reason FreeBSD's ufs_readdir() chooses to back the
2100 * directory offset up to a block boundary, so it is necessary to
2101 * skip over the records that precede the requested offset. This
2102 * requires the assumption that file offset cookies monotonically
2105 while (cpos < cend && ncookies > 0 &&
2106 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2107 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2108 cpos += dp->d_reclen;
2109 dp = (struct dirent *)cpos;
2113 if (cpos >= cend || ncookies == 0) {
2121 * If cnt > MCLBYTES and the reply will not be saved, use
2122 * ext_pgs mbufs for TLS.
2123 * For NFSv4.0, we do not know for sure if the reply will
2124 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2126 if (cnt > MCLBYTES && siz > MCLBYTES &&
2127 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2128 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2129 nd->nd_flag |= ND_EXTPG;
2132 * dirlen is the size of the reply, including all XDR and must
2133 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2134 * if the XDR should be included in "count", but to be safe, we do.
2135 * (Include the two booleans at the end of the reply in dirlen now.)
2137 if (nd->nd_flag & ND_NFSV3) {
2138 nfsrv_postopattr(nd, getret, &at);
2139 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2140 txdr_hyper(at.na_filerev, tl);
2141 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2143 dirlen = 2 * NFSX_UNSIGNED;
2146 /* Loop through the records and build reply */
2147 while (cpos < cend && ncookies > 0) {
2148 nlen = dp->d_namlen;
2149 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2150 nlen <= NFS_MAXNAMLEN) {
2151 if (nd->nd_flag & ND_NFSV3)
2152 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2154 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2161 * Build the directory record xdr from
2164 if (nd->nd_flag & ND_NFSV3) {
2165 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2166 *tl++ = newnfs_true;
2169 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2170 *tl++ = newnfs_true;
2172 *tl = txdr_unsigned(dp->d_fileno);
2173 (void) nfsm_strtom(nd, dp->d_name, nlen);
2174 if (nd->nd_flag & ND_NFSV3) {
2175 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2178 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2179 *tl = txdr_unsigned(*cookiep);
2181 cpos += dp->d_reclen;
2182 dp = (struct dirent *)cpos;
2188 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2189 *tl++ = newnfs_false;
2195 free(cookies, M_TEMP);
2198 NFSEXITCODE2(0, nd);
2202 NFSEXITCODE2(error, nd);
2207 * Readdirplus for V3 and Readdir for V4.
2210 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2211 struct vnode *vp, struct nfsexstuff *exp)
2216 char *cpos, *cend, *rbuf;
2219 struct nfsvattr nva, at, *nvap = &nva;
2220 struct mbuf *mb0, *mb1;
2221 struct nfsreferral *refp;
2222 int nlen, r, error = 0, getret = 1, usevget = 1;
2223 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2224 caddr_t bpos0, bpos1;
2225 u_int64_t off, toff, verf;
2226 u_long *cookies = NULL, *cookiep;
2227 nfsattrbit_t attrbits, rderrbits, savbits;
2230 struct componentname cn;
2231 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2232 struct mount *mp, *new_mp;
2233 uint64_t mounted_on_fileno;
2234 struct thread *p = curthread;
2235 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2237 if (nd->nd_repstat) {
2238 nfsrv_postopattr(nd, getret, &at);
2241 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2242 off = fxdr_hyper(tl);
2245 verf = fxdr_hyper(tl);
2247 siz = fxdr_unsigned(int, *tl++);
2248 cnt = fxdr_unsigned(int, *tl);
2251 * Use the server's maximum data transfer size as the upper bound
2254 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2255 cnt = NFS_SRVMAXDATA(nd);
2258 * siz is a "hint" of how much directory information (name, fileid,
2259 * cookie) should be in the reply. At least one client "hints" 0,
2260 * so I set it to cnt for that case. I also round it up to the
2261 * next multiple of DIRBLKSIZ.
2262 * Since the size of a Readdirplus directory entry reply will always
2263 * be greater than a directory entry returned by VOP_READDIR(), it
2264 * does not make sense to read more than NFS_SRVMAXDATA() via
2269 else if (siz > NFS_SRVMAXDATA(nd))
2270 siz = NFS_SRVMAXDATA(nd);
2271 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2273 if (nd->nd_flag & ND_NFSV4) {
2274 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2277 NFSSET_ATTRBIT(&savbits, &attrbits);
2278 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2279 NFSZERO_ATTRBIT(&rderrbits);
2280 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2282 NFSZERO_ATTRBIT(&attrbits);
2285 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2287 if (!nd->nd_repstat) {
2288 if (off && verf != at.na_filerev) {
2290 * va_filerev is not sufficient as a cookie verifier,
2291 * since it is not supposed to change when entries are
2292 * removed/added unless that offset cookies returned to
2293 * the client are no longer valid.
2295 if (nd->nd_flag & ND_NFSV4) {
2296 nd->nd_repstat = NFSERR_NOTSAME;
2298 nd->nd_repstat = NFSERR_BAD_COOKIE;
2303 if (!nd->nd_repstat && vp->v_type != VDIR)
2304 nd->nd_repstat = NFSERR_NOTDIR;
2305 if (!nd->nd_repstat && cnt == 0)
2306 nd->nd_repstat = NFSERR_TOOSMALL;
2307 if (!nd->nd_repstat)
2308 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2309 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2310 NFSACCCHK_VPISLOCKED, NULL);
2311 if (nd->nd_repstat) {
2313 if (nd->nd_flag & ND_NFSV3)
2314 nfsrv_postopattr(nd, getret, &at);
2317 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2318 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2320 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2324 free(cookies, M_TEMP);
2332 io.uio_offset = (off_t)off;
2334 io.uio_segflg = UIO_SYSSPACE;
2335 io.uio_rw = UIO_READ;
2337 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2339 off = (u_int64_t)io.uio_offset;
2341 siz -= io.uio_resid;
2343 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2345 if (!cookies && !nd->nd_repstat)
2346 nd->nd_repstat = NFSERR_PERM;
2347 if (!nd->nd_repstat)
2348 nd->nd_repstat = getret;
2349 if (nd->nd_repstat) {
2352 free(cookies, M_TEMP);
2354 if (nd->nd_flag & ND_NFSV3)
2355 nfsrv_postopattr(nd, getret, &at);
2359 * If nothing read, return eof
2364 if (nd->nd_flag & ND_NFSV3)
2365 nfsrv_postopattr(nd, getret, &at);
2366 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2367 txdr_hyper(at.na_filerev, tl);
2369 *tl++ = newnfs_false;
2371 free(cookies, M_TEMP);
2377 * Check for degenerate cases of nothing useful read.
2378 * If so go try again
2382 dp = (struct dirent *)cpos;
2386 * For some reason FreeBSD's ufs_readdir() chooses to back the
2387 * directory offset up to a block boundary, so it is necessary to
2388 * skip over the records that precede the requested offset. This
2389 * requires the assumption that file offset cookies monotonically
2392 while (cpos < cend && ncookies > 0 &&
2393 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2394 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2395 ((nd->nd_flag & ND_NFSV4) &&
2396 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2397 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2398 cpos += dp->d_reclen;
2399 dp = (struct dirent *)cpos;
2403 if (cpos >= cend || ncookies == 0) {
2410 * Busy the file system so that the mount point won't go away
2411 * and, as such, VFS_VGET() can be used safely.
2416 nd->nd_repstat = vfs_busy(mp, 0);
2418 if (nd->nd_repstat != 0) {
2420 free(cookies, M_TEMP);
2422 if (nd->nd_flag & ND_NFSV3)
2423 nfsrv_postopattr(nd, getret, &at);
2428 * Check to see if entries in this directory can be safely acquired
2429 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2430 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2431 * automount of the snapshot directory that is required will
2433 * This needs to be done here for NFSv4, since NFSv4 never does
2434 * a VFS_VGET() for "." or "..".
2437 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2438 if (r == EOPNOTSUPP) {
2440 cn.cn_nameiop = LOOKUP;
2441 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2442 cn.cn_cred = nd->nd_cred;
2449 * If the reply is likely to exceed MCLBYTES and the reply will
2450 * not be saved, use ext_pgs mbufs for TLS.
2451 * It is difficult to predict how large each entry will be and
2452 * how many entries have been read, so just assume the directory
2453 * entries grow by a factor of 4 when attributes are included.
2454 * For NFSv4.0, we do not know for sure if the reply will
2455 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2457 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2458 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2459 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2460 nd->nd_flag |= ND_EXTPG;
2463 * Save this position, in case there is an error before one entry
2467 bpos0 = nd->nd_bpos;
2468 bextpg0 = nd->nd_bextpg;
2469 bextpgsiz0 = nd->nd_bextpgsiz;
2472 * Fill in the first part of the reply.
2473 * dirlen is the reply length in bytes and cannot exceed cnt.
2474 * (Include the two booleans at the end of the reply in dirlen now,
2475 * so we recognize when we have exceeded cnt.)
2477 if (nd->nd_flag & ND_NFSV3) {
2478 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2479 nfsrv_postopattr(nd, getret, &at);
2481 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2483 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2484 txdr_hyper(at.na_filerev, tl);
2487 * Save this position, in case there is an empty reply needed.
2490 bpos1 = nd->nd_bpos;
2491 bextpg1 = nd->nd_bextpg;
2492 bextpgsiz1 = nd->nd_bextpgsiz;
2494 /* Loop through the records and build reply */
2496 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2497 nlen = dp->d_namlen;
2498 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2499 nlen <= NFS_MAXNAMLEN &&
2500 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2501 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2502 || (nlen == 1 && dp->d_name[0] != '.'))) {
2504 * Save the current position in the reply, in case
2505 * this entry exceeds cnt.
2508 bpos1 = nd->nd_bpos;
2509 bextpg1 = nd->nd_bextpg;
2510 bextpgsiz1 = nd->nd_bextpgsiz;
2513 * For readdir_and_lookup get the vnode using
2522 mounted_on_fileno = (uint64_t)dp->d_fileno;
2523 if ((nd->nd_flag & ND_NFSV3) ||
2524 NFSNONZERO_ATTRBIT(&savbits)) {
2525 if (nd->nd_flag & ND_NFSV4)
2526 refp = nfsv4root_getreferral(NULL,
2530 r = VFS_VGET(mp, dp->d_fileno,
2534 if (r == EOPNOTSUPP) {
2537 cn.cn_nameiop = LOOKUP;
2545 cn.cn_nameptr = dp->d_name;
2546 cn.cn_namelen = nlen;
2547 cn.cn_flags = ISLASTCN |
2548 NOFOLLOW | LOCKLEAF;
2550 dp->d_name[0] == '.' &&
2551 dp->d_name[1] == '.')
2554 if (NFSVOPLOCK(vp, LK_SHARED)
2556 nd->nd_repstat = EPERM;
2559 if ((vp->v_vflag & VV_ROOT) != 0
2560 && (cn.cn_flags & ISDOTDOT)
2566 r = VOP_LOOKUP(vp, &nvp,
2574 * For NFSv4, check to see if nvp is
2575 * a mount point and get the mount
2576 * point vnode, as required.
2579 nfsrv_enable_crossmntpt != 0 &&
2580 (nd->nd_flag & ND_NFSV4) != 0 &&
2581 nvp->v_type == VDIR &&
2582 nvp->v_mountedhere != NULL) {
2583 new_mp = nvp->v_mountedhere;
2584 r = vfs_busy(new_mp, 0);
2588 r = VFS_ROOT(new_mp,
2598 * If we failed to look up the entry, then it
2599 * has become invalid, most likely removed.
2606 KASSERT(refp != NULL || nvp != NULL,
2607 ("%s: undetected lookup error", __func__));
2610 ((nd->nd_flag & ND_NFSV3) ||
2611 NFSNONZERO_ATTRBIT(&attrbits))) {
2612 r = nfsvno_getfh(nvp, &nfh, p);
2614 r = nfsvno_getattr(nvp, nvap, nd, p,
2616 if (r == 0 && is_zfs == 1 &&
2617 nfsrv_enable_crossmntpt != 0 &&
2618 (nd->nd_flag & ND_NFSV4) != 0 &&
2619 nvp->v_type == VDIR &&
2620 vp->v_mount != nvp->v_mount) {
2622 * For a ZFS snapshot, there is a
2623 * pseudo mount that does not set
2624 * v_mountedhere, so it needs to
2625 * be detected via a different
2630 new_mp = nvp->v_mount;
2635 * If we failed to get attributes of the entry,
2636 * then just skip it for NFSv3 (the traditional
2637 * behavior in the old NFS server).
2638 * For NFSv4 the behavior is controlled by
2639 * RDATTRERROR: we either ignore the error or
2641 * Note that RDATTRERROR is never set for NFSv3.
2644 if (!NFSISSET_ATTRBIT(&attrbits,
2645 NFSATTRBIT_RDATTRERROR)) {
2647 if (needs_unbusy != 0)
2649 if ((nd->nd_flag & ND_NFSV3))
2658 * Build the directory record xdr
2660 if (nd->nd_flag & ND_NFSV3) {
2661 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2662 *tl++ = newnfs_true;
2664 *tl = txdr_unsigned(dp->d_fileno);
2665 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2666 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2668 *tl = txdr_unsigned(*cookiep);
2669 nfsrv_postopattr(nd, 0, nvap);
2670 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2671 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2675 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2676 *tl++ = newnfs_true;
2678 *tl = txdr_unsigned(*cookiep);
2679 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2681 supports_nfsv4acls =
2682 nfs_supportsnfsv4acls(nvp);
2685 supports_nfsv4acls = 0;
2687 dirlen += nfsrv_putreferralattr(nd,
2690 if (nd->nd_repstat) {
2693 if (needs_unbusy != 0)
2698 dirlen += nfsvno_fillattr(nd, new_mp,
2699 nvp, nvap, &nfh, r, &rderrbits,
2700 nd->nd_cred, p, isdgram, 0,
2701 supports_nfsv4acls, at_root,
2704 dirlen += nfsvno_fillattr(nd, new_mp,
2705 nvp, nvap, &nfh, r, &attrbits,
2706 nd->nd_cred, p, isdgram, 0,
2707 supports_nfsv4acls, at_root,
2712 dirlen += (3 * NFSX_UNSIGNED);
2714 if (needs_unbusy != 0)
2720 cpos += dp->d_reclen;
2721 dp = (struct dirent *)cpos;
2729 * If dirlen > cnt, we must strip off the last entry. If that
2730 * results in an empty reply, report NFSERR_TOOSMALL.
2732 if (dirlen > cnt || nd->nd_repstat) {
2733 if (!nd->nd_repstat && entrycnt == 0)
2734 nd->nd_repstat = NFSERR_TOOSMALL;
2735 if (nd->nd_repstat) {
2736 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2737 if (nd->nd_flag & ND_NFSV3)
2738 nfsrv_postopattr(nd, getret, &at);
2740 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2742 } else if (cpos < cend)
2744 if (!nd->nd_repstat) {
2745 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2746 *tl++ = newnfs_false;
2752 free(cookies, M_TEMP);
2756 NFSEXITCODE2(0, nd);
2760 NFSEXITCODE2(error, nd);
2765 * Get the settable attributes out of the mbuf list.
2766 * (Return 0 or EBADRPC)
2769 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2770 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2773 struct nfsv2_sattr *sp;
2774 int error = 0, toclient = 0;
2776 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2778 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2780 * Some old clients didn't fill in the high order 16bits.
2781 * --> check the low order 2 bytes for 0xffff
2783 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2784 nvap->na_mode = nfstov_mode(sp->sa_mode);
2785 if (sp->sa_uid != newnfs_xdrneg1)
2786 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2787 if (sp->sa_gid != newnfs_xdrneg1)
2788 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2789 if (sp->sa_size != newnfs_xdrneg1)
2790 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2791 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2793 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2795 nvap->na_atime.tv_sec =
2796 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2797 nvap->na_atime.tv_nsec = 0;
2800 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2801 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2804 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2805 if (*tl == newnfs_true) {
2806 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2807 nvap->na_mode = nfstov_mode(*tl);
2809 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2810 if (*tl == newnfs_true) {
2811 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2812 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2814 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2815 if (*tl == newnfs_true) {
2816 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2817 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2819 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2820 if (*tl == newnfs_true) {
2821 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2822 nvap->na_size = fxdr_hyper(tl);
2824 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2825 switch (fxdr_unsigned(int, *tl)) {
2826 case NFSV3SATTRTIME_TOCLIENT:
2827 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2828 fxdr_nfsv3time(tl, &nvap->na_atime);
2831 case NFSV3SATTRTIME_TOSERVER:
2832 vfs_timestamp(&nvap->na_atime);
2833 nvap->na_vaflags |= VA_UTIMES_NULL;
2836 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2837 switch (fxdr_unsigned(int, *tl)) {
2838 case NFSV3SATTRTIME_TOCLIENT:
2839 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2840 fxdr_nfsv3time(tl, &nvap->na_mtime);
2841 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2843 case NFSV3SATTRTIME_TOSERVER:
2844 vfs_timestamp(&nvap->na_mtime);
2846 nvap->na_vaflags |= VA_UTIMES_NULL;
2851 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2854 NFSEXITCODE2(error, nd);
2859 * Handle the setable attributes for V4.
2860 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2863 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2864 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2869 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2870 int moderet, toclient = 0;
2871 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2874 u_short mode, mask; /* Same type as va_mode. */
2877 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2880 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2881 attrsize = fxdr_unsigned(int, *tl);
2884 * Loop around getting the setable attributes. If an unsupported
2885 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2888 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2889 bitpos = NFSATTRBIT_MAX;
2894 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2895 if (attrsum > attrsize) {
2896 error = NFSERR_BADXDR;
2899 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2901 case NFSATTRBIT_SIZE:
2902 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2903 if (vp != NULL && vp->v_type != VREG) {
2904 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2908 nvap->na_size = fxdr_hyper(tl);
2909 attrsum += NFSX_HYPER;
2911 case NFSATTRBIT_ACL:
2912 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2916 if (aceerr && !nd->nd_repstat)
2917 nd->nd_repstat = aceerr;
2920 case NFSATTRBIT_ARCHIVE:
2921 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2922 if (!nd->nd_repstat)
2923 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2924 attrsum += NFSX_UNSIGNED;
2926 case NFSATTRBIT_HIDDEN:
2927 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2928 if (!nd->nd_repstat)
2929 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2930 attrsum += NFSX_UNSIGNED;
2932 case NFSATTRBIT_MIMETYPE:
2933 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2934 i = fxdr_unsigned(int, *tl);
2935 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2938 if (!nd->nd_repstat)
2939 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2940 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2942 case NFSATTRBIT_MODE:
2943 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
2944 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2945 nvap->na_mode = nfstov_mode(*tl);
2946 attrsum += NFSX_UNSIGNED;
2948 case NFSATTRBIT_OWNER:
2949 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2950 j = fxdr_unsigned(int, *tl);
2952 error = NFSERR_BADXDR;
2955 if (j > NFSV4_SMALLSTR)
2956 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2959 error = nfsrv_mtostr(nd, cp, j);
2961 if (j > NFSV4_SMALLSTR)
2962 free(cp, M_NFSSTRING);
2965 if (!nd->nd_repstat) {
2966 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
2968 if (!nd->nd_repstat)
2971 if (j > NFSV4_SMALLSTR)
2972 free(cp, M_NFSSTRING);
2973 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2975 case NFSATTRBIT_OWNERGROUP:
2976 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2977 j = fxdr_unsigned(int, *tl);
2979 error = NFSERR_BADXDR;
2982 if (j > NFSV4_SMALLSTR)
2983 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2986 error = nfsrv_mtostr(nd, cp, j);
2988 if (j > NFSV4_SMALLSTR)
2989 free(cp, M_NFSSTRING);
2992 if (!nd->nd_repstat) {
2993 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
2995 if (!nd->nd_repstat)
2998 if (j > NFSV4_SMALLSTR)
2999 free(cp, M_NFSSTRING);
3000 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3002 case NFSATTRBIT_SYSTEM:
3003 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3004 if (!nd->nd_repstat)
3005 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3006 attrsum += NFSX_UNSIGNED;
3008 case NFSATTRBIT_TIMEACCESSSET:
3009 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3010 attrsum += NFSX_UNSIGNED;
3011 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3012 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3013 fxdr_nfsv4time(tl, &nvap->na_atime);
3015 attrsum += NFSX_V4TIME;
3017 vfs_timestamp(&nvap->na_atime);
3018 nvap->na_vaflags |= VA_UTIMES_NULL;
3021 case NFSATTRBIT_TIMEBACKUP:
3022 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3023 if (!nd->nd_repstat)
3024 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3025 attrsum += NFSX_V4TIME;
3027 case NFSATTRBIT_TIMECREATE:
3028 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3029 fxdr_nfsv4time(tl, &nvap->na_btime);
3030 attrsum += NFSX_V4TIME;
3032 case NFSATTRBIT_TIMEMODIFYSET:
3033 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3034 attrsum += NFSX_UNSIGNED;
3035 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3036 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3037 fxdr_nfsv4time(tl, &nvap->na_mtime);
3038 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3039 attrsum += NFSX_V4TIME;
3041 vfs_timestamp(&nvap->na_mtime);
3043 nvap->na_vaflags |= VA_UTIMES_NULL;
3046 case NFSATTRBIT_MODESETMASKED:
3047 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3048 mode = fxdr_unsigned(u_short, *tl++);
3049 mask = fxdr_unsigned(u_short, *tl);
3051 * vp == NULL implies an Open/Create operation.
3052 * This attribute can only be used for Setattr and
3053 * only for NFSv4.1 or higher.
3054 * If moderet != 0, a mode attribute has also been
3055 * specified and this attribute cannot be done in the
3056 * same Setattr operation.
3058 if ((nd->nd_flag & ND_NFSV41) == 0)
3059 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3060 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3062 nd->nd_repstat = NFSERR_INVAL;
3063 else if (moderet == 0)
3064 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3066 nvap->na_mode = (mode & mask) |
3067 (va.va_mode & ~mask);
3069 nd->nd_repstat = moderet;
3070 attrsum += 2 * NFSX_UNSIGNED;
3073 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3075 * set bitpos so we drop out of the loop.
3077 bitpos = NFSATTRBIT_MAX;
3083 * some clients pad the attrlist, so we need to skip over the
3086 if (attrsum > attrsize) {
3087 error = NFSERR_BADXDR;
3089 attrsize = NFSM_RNDUP(attrsize);
3090 if (attrsum < attrsize)
3091 error = nfsm_advance(nd, attrsize - attrsum, -1);
3094 NFSEXITCODE2(error, nd);
3099 * Check/setup export credentials.
3102 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3103 struct ucred *credanon)
3108 * Check/setup credentials.
3110 if (nd->nd_flag & ND_GSS)
3111 exp->nes_exflag &= ~MNT_EXPORTANON;
3114 * Check to see if the operation is allowed for this security flavor.
3115 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
3116 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
3117 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
3119 if (nfsvno_testexp(nd, exp) &&
3120 nd->nd_procnum != NFSV4OP_SECINFO &&
3121 nd->nd_procnum != NFSPROC_FSINFO) {
3122 if (nd->nd_flag & ND_NFSV4)
3123 error = NFSERR_WRONGSEC;
3125 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3130 * Check to see if the file system is exported V4 only.
3132 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3133 error = NFSERR_PROGNOTV4;
3138 * Now, map the user credentials.
3139 * (Note that ND_AUTHNONE will only be set for an NFSv3
3140 * Fsinfo RPC. If set for anything else, this code might need
3143 if (NFSVNO_EXPORTED(exp)) {
3144 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3145 NFSVNO_EXPORTANON(exp) ||
3146 (nd->nd_flag & ND_AUTHNONE) != 0) {
3147 nd->nd_cred->cr_uid = credanon->cr_uid;
3148 nd->nd_cred->cr_gid = credanon->cr_gid;
3149 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3150 credanon->cr_groups);
3151 } else if ((nd->nd_flag & ND_GSS) == 0) {
3153 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3154 * if there is a replacement credential with a group
3155 * list set up by "nfsuserd -manage-gids".
3156 * If there is no replacement, nfsrv_getgrpscred()
3157 * simply returns its argument.
3159 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3164 NFSEXITCODE2(error, nd);
3172 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3173 struct ucred **credp)
3177 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3178 &exp->nes_numsecflavor, exp->nes_secflavors);
3180 if (nfs_rootfhset) {
3181 exp->nes_exflag = 0;
3182 exp->nes_numsecflavor = 0;
3185 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3187 printf("nfsvno_checkexp: numsecflavors out of range\n");
3188 exp->nes_numsecflavor = 0;
3196 * Get a vnode for a file handle and export stuff.
3199 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3200 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3201 struct ucred **credp)
3206 exp->nes_numsecflavor = 0;
3207 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3209 /* Make sure the server replies ESTALE to the client. */
3211 if (nam && !error) {
3212 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3213 &exp->nes_numsecflavor, exp->nes_secflavors);
3215 if (nfs_rootfhset) {
3216 exp->nes_exflag = 0;
3217 exp->nes_numsecflavor = 0;
3222 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3224 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3225 exp->nes_numsecflavor = 0;
3235 * nfsd_fhtovp() - convert a fh to a vnode ptr
3236 * - look up fsid in mount list (if not found ret error)
3237 * - get vp and export rights by calling nfsvno_fhtovp()
3238 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3240 * - if mpp != NULL, return the mount point so that it can
3241 * be used for vn_finished_write() by the caller
3244 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3245 struct vnode **vpp, struct nfsexstuff *exp,
3246 struct mount **mpp, int startwrite)
3249 struct ucred *credanon;
3252 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3254 * Check for the special case of the nfsv4root_fh.
3256 mp = vfs_busyfs(&fhp->fh_fsid);
3261 nd->nd_repstat = ESTALE;
3266 vn_start_write(NULL, mpp, V_WAIT);
3267 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3268 lktype = LK_EXCLUSIVE;
3270 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3275 * For NFSv4 without a pseudo root fs, unexported file handles
3276 * can be returned, so that Lookup works everywhere.
3278 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3279 !(nd->nd_flag & ND_NFSV4)) {
3281 nd->nd_repstat = EACCES;
3285 * If TLS is required by the export, check the flags in nd_flag.
3287 if (nd->nd_repstat == 0 && ((NFSVNO_EXTLS(exp) &&
3288 (nd->nd_flag & ND_TLS) == 0) ||
3289 (NFSVNO_EXTLSCERT(exp) &&
3290 (nd->nd_flag & ND_TLSCERT) == 0) ||
3291 (NFSVNO_EXTLSCERTUSER(exp) &&
3292 (nd->nd_flag & ND_TLSCERTUSER) == 0))) {
3294 nd->nd_repstat = NFSERR_ACCES;
3298 * Personally, I've never seen any point in requiring a
3299 * reserved port#, since only in the rare case where the
3300 * clients are all boxes with secure system privileges,
3301 * does it provide any enhanced security, but... some people
3302 * believe it to be useful and keep putting this code back in.
3303 * (There is also some "security checker" out there that
3304 * complains if the nfs server doesn't enforce this.)
3305 * However, note the following:
3306 * RFC3530 (NFSv4) specifies that a reserved port# not be
3308 * RFC2623 recommends that, if a reserved port# is checked for,
3309 * that there be a way to turn that off--> ifdef'd.
3311 #ifdef NFS_REQRSVPORT
3312 if (!nd->nd_repstat) {
3313 struct sockaddr_in *saddr;
3314 struct sockaddr_in6 *saddr6;
3316 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3317 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3318 if (!(nd->nd_flag & ND_NFSV4) &&
3319 ((saddr->sin_family == AF_INET &&
3320 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3321 (saddr6->sin6_family == AF_INET6 &&
3322 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3324 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3327 #endif /* NFS_REQRSVPORT */
3330 * Check/setup credentials.
3332 if (!nd->nd_repstat) {
3333 nd->nd_saveduid = nd->nd_cred->cr_uid;
3334 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
3338 if (credanon != NULL)
3340 if (nd->nd_repstat) {
3342 vn_finished_write(mp);
3349 NFSEXITCODE2(0, nd);
3356 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3358 struct filedesc *fdp;
3362 fdp = p->td_proc->p_fd;
3363 if (fd < 0 || fd >= fdp->fd_nfiles ||
3364 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3376 * Called from nfssvc() to update the exports list. Just call
3377 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3378 * in the mount list.
3381 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3383 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3385 struct nameidata nd;
3388 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3389 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3391 else if (error == 0) {
3392 if (nfsexargp->fspec == NULL) {
3397 * If fspec != NULL, this is the v4root path.
3399 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3400 nfsexargp->fspec, p);
3401 if ((error = namei(&nd)) != 0)
3403 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3406 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3407 NFSBCOPY((caddr_t)&fh,
3408 nfs_rootfh.nfsrvfh_data,
3409 sizeof (fhandle_t));
3420 * This function needs to test to see if the system is near its limit
3421 * for memory allocation via malloc() or mget() and return True iff
3422 * either of these resources are near their limit.
3423 * XXX (For now, this is just a stub.)
3425 int nfsrv_testmalloclimit = 0;
3427 nfsrv_mallocmget_limit(void)
3429 static int printmesg = 0;
3430 static int testval = 1;
3432 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3433 if ((printmesg++ % 100) == 0)
3434 printf("nfsd: malloc/mget near limit\n");
3441 * BSD specific initialization of a mount point.
3446 static int inited = 0;
3451 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3452 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3453 TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3454 nfsv4root_mnt.mnt_export = NULL;
3455 TAILQ_INIT(&nfsv4root_opt);
3456 TAILQ_INIT(&nfsv4root_newopt);
3457 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3458 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3459 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3460 nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3464 * Get a vnode for a file handle, without checking exports, etc.
3467 nfsvno_getvp(fhandle_t *fhp)
3473 mp = vfs_busyfs(&fhp->fh_fsid);
3476 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3484 * Do a local VOP_ADVLOCK().
3487 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3488 u_int64_t end, struct thread *td)
3494 if (nfsrv_dolocallocks == 0)
3496 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3498 fl.l_whence = SEEK_SET;
3500 fl.l_start = (off_t)first;
3501 if (end == NFS64BITSSET) {
3505 fl.l_len = (off_t)tlen;
3508 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3509 * values for all calls, so that all locks will be held by the
3510 * nfsd server. (The nfsd server handles conflicts between the
3512 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3513 * bytes, so it can't be put in l_sysid.
3515 if (nfsv4_sysid == 0)
3516 nfsv4_sysid = nlm_acquire_next_sysid();
3517 fl.l_pid = (pid_t)0;
3518 fl.l_sysid = (int)nfsv4_sysid;
3520 if (ftype == F_UNLCK)
3521 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3522 (F_POSIX | F_REMOTE));
3524 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3525 (F_POSIX | F_REMOTE));
3533 * Check the nfsv4 root exports.
3536 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3538 struct ucred *credanon;
3539 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3542 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3543 &credanon, &numsecflavor, secflavors);
3545 error = NFSERR_PROGUNAVAIL;
3548 if (credanon != NULL)
3550 for (i = 0; i < numsecflavor; i++) {
3551 if (secflavors[i] == AUTH_SYS)
3552 nd->nd_flag |= ND_EXAUTHSYS;
3553 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3554 nd->nd_flag |= ND_EXGSS;
3555 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3556 nd->nd_flag |= ND_EXGSSINTEGRITY;
3557 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3558 nd->nd_flag |= ND_EXGSSPRIVACY;
3561 /* And set ND_EXxx flags for TLS. */
3562 if ((exflags & MNT_EXTLS) != 0) {
3563 nd->nd_flag |= ND_EXTLS;
3564 if ((exflags & MNT_EXTLSCERT) != 0)
3565 nd->nd_flag |= ND_EXTLSCERT;
3566 if ((exflags & MNT_EXTLSCERTUSER) != 0)
3567 nd->nd_flag |= ND_EXTLSCERTUSER;
3576 * Nfs server pseudo system call for the nfsd's
3582 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3585 struct nfsd_addsock_args sockarg;
3586 struct nfsd_nfsd_args nfsdarg;
3587 struct nfsd_nfsd_oargs onfsdarg;
3588 struct nfsd_pnfsd_args pnfsdarg;
3589 struct vnode *vp, *nvp, *curdvp;
3590 struct pnfsdsfile *pf;
3591 struct nfsdevice *ds, *fds;
3592 cap_rights_t rights;
3593 int buflen, error, ret;
3594 char *buf, *cp, *cp2, *cp3;
3595 char fname[PNFS_FILENAME_LEN + 1];
3597 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3598 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3602 * Since we don't know what rights might be required,
3603 * pretend that we need them all. It is better to be too
3604 * careful than too reckless.
3606 error = fget(td, sockarg.sock,
3607 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3610 if (fp->f_type != DTYPE_SOCKET) {
3615 error = nfsrvd_addsock(fp);
3617 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3618 if (uap->argp == NULL) {
3622 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3623 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3625 nfsdarg.principal = onfsdarg.principal;
3626 nfsdarg.minthreads = onfsdarg.minthreads;
3627 nfsdarg.maxthreads = onfsdarg.maxthreads;
3628 nfsdarg.version = 1;
3629 nfsdarg.addr = NULL;
3630 nfsdarg.addrlen = 0;
3631 nfsdarg.dnshost = NULL;
3632 nfsdarg.dnshostlen = 0;
3633 nfsdarg.dspath = NULL;
3634 nfsdarg.dspathlen = 0;
3635 nfsdarg.mdspath = NULL;
3636 nfsdarg.mdspathlen = 0;
3637 nfsdarg.mirrorcnt = 1;
3640 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3643 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3644 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3645 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3646 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3647 nfsdarg.mirrorcnt >= 1 &&
3648 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3649 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3650 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3651 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3652 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3653 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3654 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3655 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3656 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3661 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3663 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3664 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3666 free(nfsdarg.addr, M_TEMP);
3670 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3671 nfsdarg.dnshost = cp;
3672 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3673 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3675 free(nfsdarg.addr, M_TEMP);
3676 free(nfsdarg.dnshost, M_TEMP);
3680 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3681 nfsdarg.dspath = cp;
3682 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3683 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3685 free(nfsdarg.addr, M_TEMP);
3686 free(nfsdarg.dnshost, M_TEMP);
3687 free(nfsdarg.dspath, M_TEMP);
3691 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3692 nfsdarg.mdspath = cp;
3694 nfsdarg.addr = NULL;
3695 nfsdarg.addrlen = 0;
3696 nfsdarg.dnshost = NULL;
3697 nfsdarg.dnshostlen = 0;
3698 nfsdarg.dspath = NULL;
3699 nfsdarg.dspathlen = 0;
3700 nfsdarg.mdspath = NULL;
3701 nfsdarg.mdspathlen = 0;
3702 nfsdarg.mirrorcnt = 1;
3704 error = nfsrvd_nfsd(td, &nfsdarg);
3705 free(nfsdarg.addr, M_TEMP);
3706 free(nfsdarg.dnshost, M_TEMP);
3707 free(nfsdarg.dspath, M_TEMP);
3708 free(nfsdarg.mdspath, M_TEMP);
3709 } else if (uap->flag & NFSSVC_PNFSDS) {
3710 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3711 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3712 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3713 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3714 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3717 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3719 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3720 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3721 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3722 buf = malloc(buflen, M_TEMP, M_WAITOK);
3723 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3725 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3726 if (error == 0 && pnfsdarg.dspath != NULL) {
3727 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3728 error = copyinstr(pnfsdarg.dspath, cp2,
3729 PATH_MAX + 1, NULL);
3730 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3734 if (error == 0 && pnfsdarg.curdspath != NULL) {
3735 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3736 error = copyinstr(pnfsdarg.curdspath, cp3,
3737 PATH_MAX + 1, NULL);
3738 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3745 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3746 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3748 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3750 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3751 printf("copymr: dsdir out of range\n");
3754 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3755 error = nfsrv_copymr(vp, nvp,
3756 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3757 (struct pnfsdsfile *)buf,
3758 buflen / sizeof(*pf), td->td_ucred, td);
3761 if (fds != NULL && error == 0) {
3762 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3763 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3765 nfsrv_dsremove(curdvp, fname,
3767 NFSVOPUNLOCK(curdvp);
3770 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3778 error = nfssvc_srvcall(td, uap, td->td_ucred);
3787 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3789 struct nfsex_args export;
3790 struct nfsex_oldargs oexp;
3791 struct file *fp = NULL;
3792 int stablefd, i, len;
3793 struct nfsd_clid adminrevoke;
3794 struct nfsd_dumplist dumplist;
3795 struct nfsd_dumpclients *dumpclients;
3796 struct nfsd_dumplocklist dumplocklist;
3797 struct nfsd_dumplocks *dumplocks;
3798 struct nameidata nd;
3800 int error = EINVAL, igotlock;
3803 static int suspend_nfsd = 0;
3805 if (uap->flag & NFSSVC_PUBLICFH) {
3806 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3807 sizeof (fhandle_t));
3808 error = copyin(uap->argp,
3809 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3812 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3813 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3814 error = copyin(uap->argp,(caddr_t)&export,
3815 sizeof (struct nfsex_args));
3818 if (export.export.ex_ngroups > NGROUPS_MAX ||
3819 export.export.ex_ngroups < 0)
3821 else if (export.export.ex_ngroups > 0) {
3822 grps = malloc(export.export.ex_ngroups *
3823 sizeof(gid_t), M_TEMP, M_WAITOK);
3824 error = copyin(export.export.ex_groups, grps,
3825 export.export.ex_ngroups * sizeof(gid_t));
3826 export.export.ex_groups = grps;
3828 export.export.ex_groups = NULL;
3830 error = nfsrv_v4rootexport(&export, cred, p);
3833 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3834 NFSSVC_V4ROOTEXPORT) {
3835 error = copyin(uap->argp,(caddr_t)&oexp,
3836 sizeof (struct nfsex_oldargs));
3838 memset(&export.export, 0, sizeof(export.export));
3839 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3840 export.export.ex_root = oexp.export.ex_root;
3841 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3842 export.export.ex_ngroups =
3843 oexp.export.ex_anon.cr_ngroups;
3844 export.export.ex_groups = NULL;
3845 if (export.export.ex_ngroups > XU_NGROUPS ||
3846 export.export.ex_ngroups < 0)
3848 else if (export.export.ex_ngroups > 0) {
3849 export.export.ex_groups = malloc(
3850 export.export.ex_ngroups * sizeof(gid_t),
3852 for (i = 0; i < export.export.ex_ngroups; i++)
3853 export.export.ex_groups[i] =
3854 oexp.export.ex_anon.cr_groups[i];
3856 export.export.ex_addr = oexp.export.ex_addr;
3857 export.export.ex_addrlen = oexp.export.ex_addrlen;
3858 export.export.ex_mask = oexp.export.ex_mask;
3859 export.export.ex_masklen = oexp.export.ex_masklen;
3860 export.export.ex_indexfile = oexp.export.ex_indexfile;
3861 export.export.ex_numsecflavors =
3862 oexp.export.ex_numsecflavors;
3863 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3864 export.export.ex_numsecflavors < 0)
3867 for (i = 0; i < export.export.ex_numsecflavors;
3869 export.export.ex_secflavors[i] =
3870 oexp.export.ex_secflavors[i];
3872 export.fspec = oexp.fspec;
3874 error = nfsrv_v4rootexport(&export, cred, p);
3875 free(export.export.ex_groups, M_TEMP);
3877 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3880 } else if (uap->flag & NFSSVC_STABLERESTART) {
3881 error = copyin(uap->argp, (caddr_t)&stablefd,
3884 error = fp_getfvp(p, stablefd, &fp, &vp);
3885 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3887 if (!error && newnfs_numnfsd != 0)
3890 nfsrv_stablefirst.nsf_fp = fp;
3891 nfsrv_setupstable(p);
3893 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3894 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3895 sizeof (struct nfsd_clid));
3897 error = nfsrv_adminrevoke(&adminrevoke, p);
3898 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3899 error = copyin(uap->argp, (caddr_t)&dumplist,
3900 sizeof (struct nfsd_dumplist));
3901 if (!error && (dumplist.ndl_size < 1 ||
3902 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3905 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3906 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3907 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3908 error = copyout(dumpclients, dumplist.ndl_list, len);
3909 free(dumpclients, M_TEMP);
3911 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3912 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3913 sizeof (struct nfsd_dumplocklist));
3914 if (!error && (dumplocklist.ndllck_size < 1 ||
3915 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3918 error = nfsrv_lookupfilename(&nd,
3919 dumplocklist.ndllck_fname, p);
3921 len = sizeof (struct nfsd_dumplocks) *
3922 dumplocklist.ndllck_size;
3923 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3924 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3925 dumplocklist.ndllck_size, p);
3927 error = copyout(dumplocks, dumplocklist.ndllck_list,
3929 free(dumplocks, M_TEMP);
3931 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3934 nfsd_master_pid = procp->p_pid;
3935 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3936 nfsd_master_start = procp->p_stats->p_start;
3937 nfsd_master_proc = procp;
3939 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3940 NFSLOCKV4ROOTMUTEX();
3941 if (suspend_nfsd == 0) {
3942 /* Lock out all nfsd threads */
3944 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3945 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3946 } while (igotlock == 0 && suspend_nfsd == 0);
3949 NFSUNLOCKV4ROOTMUTEX();
3951 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3952 NFSLOCKV4ROOTMUTEX();
3953 if (suspend_nfsd != 0) {
3954 nfsv4_unlock(&nfsd_suspend_lock, 0);
3957 NFSUNLOCKV4ROOTMUTEX();
3967 * Returns 0 if ok, 1 otherwise.
3970 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3975 * This seems odd, but allow the case where the security flavor
3976 * list is empty. This happens when NFSv4 is traversing non-exported
3977 * file systems. Exported file systems should always have a non-empty
3978 * security flavor list.
3980 if (exp->nes_numsecflavor == 0)
3983 for (i = 0; i < exp->nes_numsecflavor; i++) {
3985 * The tests for privacy and integrity must be first,
3986 * since ND_GSS is set for everything but AUTH_SYS.
3988 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3989 (nd->nd_flag & ND_GSSPRIVACY))
3991 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3992 (nd->nd_flag & ND_GSSINTEGRITY))
3994 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3995 (nd->nd_flag & ND_GSS))
3997 if (exp->nes_secflavors[i] == AUTH_SYS &&
3998 (nd->nd_flag & ND_GSS) == 0)
4005 * Calculate a hash value for the fid in a file handle.
4008 nfsrv_hashfh(fhandle_t *fhp)
4012 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4017 * Calculate a hash value for the sessionid.
4020 nfsrv_hashsessionid(uint8_t *sessionid)
4024 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4029 * Signal the userland master nfsd to backup the stable restart file.
4032 nfsrv_backupstable(void)
4036 if (nfsd_master_proc != NULL) {
4037 procp = pfind(nfsd_master_pid);
4038 /* Try to make sure it is the correct process. */
4039 if (procp == nfsd_master_proc &&
4040 procp->p_stats->p_start.tv_sec ==
4041 nfsd_master_start.tv_sec &&
4042 procp->p_stats->p_start.tv_usec ==
4043 nfsd_master_start.tv_usec &&
4044 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4045 kern_psignal(procp, SIGUSR2);
4047 nfsd_master_proc = NULL;
4055 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4056 * The arguments are in a structure, so that they can be passed through
4057 * taskqueue for a kernel process to execute this function.
4059 struct nfsrvdscreate {
4063 struct ucred *tcred;
4066 struct pnfsdsfile *pf;
4070 struct vattr createva;
4074 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4075 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4076 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4079 struct nameidata named;
4084 struct nfsmount *nmp;
4087 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4088 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
4089 nfsvno_setpathbuf(&named, &bufp, &hashp);
4090 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4091 named.ni_cnd.cn_thread = p;
4092 named.ni_cnd.cn_nameptr = bufp;
4093 if (fnamep != NULL) {
4094 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4095 named.ni_cnd.cn_namelen = strlen(bufp);
4097 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4098 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4100 /* Create the date file in the DS mount. */
4101 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4103 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4106 /* Set the ownership of the file. */
4107 error = VOP_SETATTR(nvp, nvap, tcred);
4108 NFSD_DEBUG(4, "nfsrv_dscreate:"
4109 " setattr-uid=%d\n", error);
4114 printf("pNFS: pnfscreate failed=%d\n", error);
4116 printf("pNFS: pnfscreate vnlock=%d\n", error);
4119 nmp = VFSTONFS(nvp->v_mount);
4120 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4121 != 0 || nmp->nm_nam->sa_len > sizeof(
4122 struct sockaddr_in6) ||
4123 np->n_fhp->nfh_len != NFSX_MYFH) {
4124 printf("Bad DS file: fstype=%s salen=%d"
4126 nvp->v_mount->mnt_vfc->vfc_name,
4127 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4131 /* Set extattrs for the DS on the MDS file. */
4134 error = VOP_GETATTR(nvp, &va, tcred);
4136 dsa->dsa_filerev = va.va_filerev;
4137 dsa->dsa_size = va.va_size;
4138 dsa->dsa_atime = va.va_atime;
4139 dsa->dsa_mtime = va.va_mtime;
4140 dsa->dsa_bytes = va.va_bytes;
4144 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4146 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4147 nmp->nm_nam->sa_len);
4148 NFSBCOPY(named.ni_cnd.cn_nameptr,
4150 sizeof(pf->dsf_filename));
4153 printf("pNFS: pnfscreate can't get DS"
4154 " attr=%d\n", error);
4155 if (nvpp != NULL && error == 0)
4160 nfsvno_relpathbuf(&named);
4165 * Start up the thread that will execute nfsrv_dscreate().
4168 start_dscreate(void *arg, int pending)
4170 struct nfsrvdscreate *dsc;
4172 dsc = (struct nfsrvdscreate *)arg;
4173 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4174 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4176 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4180 * Create a pNFS data file on the Data Server(s).
4183 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4186 struct nfsrvdscreate *dsc, *tdsc = NULL;
4187 struct nfsdevice *ds, *tds, *fds;
4189 struct pnfsdsfile *pf, *tpf;
4190 struct pnfsdsattr dsattr;
4192 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4193 struct nfsmount *nmp;
4198 struct ucred *tcred;
4199 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4202 /* Get a DS server directory in a round-robin order. */
4208 * Search for the first entry that handles this MDS fs, but use the
4209 * first entry for all MDS fs's otherwise.
4211 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4212 if (tds->nfsdev_nmp != NULL) {
4213 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4215 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4216 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4224 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4227 i = dsdir[0] = ds->nfsdev_nextdir;
4228 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4229 dvp[0] = ds->nfsdev_dsdir[i];
4230 tds = TAILQ_NEXT(ds, nfsdev_list);
4231 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4232 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4233 if (tds->nfsdev_nmp != NULL &&
4234 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4235 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4236 fsidcmp(&mp->mnt_stat.f_fsid,
4237 &tds->nfsdev_mdsfsid) == 0))) {
4238 dsdir[mirrorcnt] = i;
4239 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4241 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4246 /* Put at end of list to implement round-robin usage. */
4247 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4248 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4252 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4254 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4257 error = nfsvno_getfh(vp, &fh, p);
4259 error = VOP_GETATTR(vp, &va, cred);
4261 /* Set the attributes for "vp" to Setattr the DS vp. */
4264 vamode = va.va_mode;
4268 va.va_mode = vamode;
4271 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4273 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4275 /* Make data file name based on FH. */
4276 tcred = newnfs_getcred();
4279 * Create the file on each DS mirror, using kernel process(es) for the
4280 * additional mirrors.
4283 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4284 tpf->dsf_dir = dsdir[i];
4285 tdsc->tcred = tcred;
4288 tdsc->createva = *vap;
4289 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4296 if (nfs_pnfsiothreads != 0) {
4297 ret = nfs_pnfsio(start_dscreate, tdsc);
4298 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4301 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4302 NULL, tcred, p, NULL);
4304 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4306 if (failpos == -1 && nfsds_failerr(ret))
4314 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4315 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4316 &dsattr, NULL, tcred, p, NULL);
4317 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4318 failpos = mirrorcnt - 1;
4322 timo = hz / 50; /* Wait for 20msec. */
4325 /* Wait for kernel task(s) to complete. */
4326 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4327 while (tdsc->inprog != 0 && tdsc->done == 0)
4328 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4329 if (tdsc->err != 0) {
4330 if (failpos == -1 && nfsds_failerr(tdsc->err))
4332 else if (error == 0)
4338 * If failpos has been set, that mirror has failed, so it needs
4342 nmp = VFSTONFS(dvp[failpos]->v_mount);
4344 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4345 NFSMNTP_CANCELRPCS)) == 0) {
4346 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4348 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4349 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4352 nfsrv_killrpcs(nmp);
4354 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4362 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4364 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4365 mirrorcnt, nfsrv_maxpnfsmirror);
4367 * For all mirrors that couldn't be created, fill in the
4368 * *pf structure, but with an IP address == 0.0.0.0.
4370 tpf = pf + mirrorcnt;
4371 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4373 tpf->dsf_sin.sin_family = AF_INET;
4374 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4375 tpf->dsf_sin.sin_addr.s_addr = 0;
4376 tpf->dsf_sin.sin_port = 0;
4379 error = vn_extattr_set(vp, IO_NODELOCKED,
4380 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4381 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4383 error = vn_extattr_set(vp, IO_NODELOCKED,
4384 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4385 sizeof(dsattr), (char *)&dsattr, p);
4387 printf("pNFS: pnfscreate setextattr=%d\n",
4390 printf("pNFS: pnfscreate=%d\n", error);
4396 * Get the information needed to remove the pNFS Data Server file from the
4397 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4398 * DS directory vnode. The caller must unlock *ddvp when done with it.
4401 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4402 int *mirrorcntp, char *fname, fhandle_t *fhp)
4405 struct ucred *tcred;
4410 /* If not an exported regular file or not a pNFS server, just return. */
4411 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4412 nfsrv_devidcnt == 0)
4415 /* Check to see if this is the last hard link. */
4416 tcred = newnfs_getcred();
4417 error = VOP_GETATTR(vp, &va, tcred);
4420 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4423 if (va.va_nlink > 1)
4426 error = nfsvno_getfh(vp, fhp, p);
4428 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4433 buf = malloc(buflen, M_TEMP, M_WAITOK);
4434 /* Get the directory vnode for the DS mount and the file handle. */
4435 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4436 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4439 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4443 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4444 * The arguments are in a structure, so that they can be passed through
4445 * taskqueue for a kernel process to execute this function.
4447 struct nfsrvdsremove {
4451 struct ucred *tcred;
4455 char fname[PNFS_FILENAME_LEN + 1];
4459 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4462 struct nameidata named;
4468 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4471 named.ni_cnd.cn_nameiop = DELETE;
4472 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4473 named.ni_cnd.cn_cred = tcred;
4474 named.ni_cnd.cn_thread = p;
4475 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4476 nfsvno_setpathbuf(&named, &bufp, &hashp);
4477 named.ni_cnd.cn_nameptr = bufp;
4478 named.ni_cnd.cn_namelen = strlen(fname);
4479 strlcpy(bufp, fname, NAME_MAX);
4480 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4481 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4482 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4484 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4488 nfsvno_relpathbuf(&named);
4490 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4495 * Start up the thread that will execute nfsrv_dsremove().
4498 start_dsremove(void *arg, int pending)
4500 struct nfsrvdsremove *dsrm;
4502 dsrm = (struct nfsrvdsremove *)arg;
4503 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4506 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4510 * Remove a pNFS data file from a Data Server.
4511 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4512 * removed to set up the dvp and fill in the FH.
4515 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4518 struct ucred *tcred;
4519 struct nfsrvdsremove *dsrm, *tdsrm;
4520 struct nfsdevice *ds;
4521 struct nfsmount *nmp;
4522 int failpos, i, ret, timo;
4524 tcred = newnfs_getcred();
4527 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4529 * Remove the file on each DS mirror, using kernel process(es) for the
4530 * additional mirrors.
4533 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4534 tdsrm->tcred = tcred;
4536 tdsrm->dvp = dvp[i];
4537 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4542 if (nfs_pnfsiothreads != 0) {
4543 ret = nfs_pnfsio(start_dsremove, tdsrm);
4544 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4547 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4548 if (failpos == -1 && nfsds_failerr(ret))
4552 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4553 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4554 failpos = mirrorcnt - 1;
4555 timo = hz / 50; /* Wait for 20msec. */
4558 /* Wait for kernel task(s) to complete. */
4559 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4560 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4561 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4562 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4567 * If failpos has been set, that mirror has failed, so it needs
4571 nmp = VFSTONFS(dvp[failpos]->v_mount);
4573 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4574 NFSMNTP_CANCELRPCS)) == 0) {
4575 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4577 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4578 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4581 nfsrv_killrpcs(nmp);
4583 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4589 /* Get rid all layouts for the file. */
4590 nfsrv_freefilelayouts(fhp);
4597 * Generate a file name based on the file handle and put it in *bufp.
4598 * Return the number of bytes generated.
4601 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4605 const uint8_t *hexdigits = "0123456789abcdef";
4607 cp = (uint8_t *)fhp;
4608 for (i = 0; i < sizeof(*fhp); i++) {
4609 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4610 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4617 * Update the Metadata file's attributes from the DS file when a Read/Write
4618 * layout is returned.
4619 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4620 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4623 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4625 struct ucred *tcred;
4628 /* Do this as root so that it won't fail with EACCES. */
4629 tcred = newnfs_getcred();
4630 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4631 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4637 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4640 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4645 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4646 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4651 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4652 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4653 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4654 off_t *offp, int content, bool *eofp)
4656 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4657 fhandle_t fh[NFSDEV_MAXMIRRORS];
4658 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4659 struct nfsdevice *ds;
4660 struct pnfsdsattr dsattr;
4661 struct opnfsdsattr odsattr;
4663 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4665 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4667 * If not a regular file, not exported or not a pNFS server,
4668 * just return ENOENT.
4670 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4671 nfsrv_devidcnt == 0)
4675 buf = malloc(buflen, M_TEMP, M_WAITOK);
4679 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4680 * from the MetaData file's extended attribute.
4682 if (ioproc == NFSPROC_GETATTR) {
4683 error = vn_extattr_get(vp, IO_NODELOCKED,
4684 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4687 if (buflen == sizeof(odsattr)) {
4688 NFSBCOPY(buf, &odsattr, buflen);
4689 nap->na_filerev = odsattr.dsa_filerev;
4690 nap->na_size = odsattr.dsa_size;
4691 nap->na_atime = odsattr.dsa_atime;
4692 nap->na_mtime = odsattr.dsa_mtime;
4694 * Fake na_bytes by rounding up na_size.
4695 * Since we don't know the block size, just
4696 * use BLKDEV_IOSIZE.
4698 nap->na_bytes = (odsattr.dsa_size +
4699 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4700 } else if (buflen == sizeof(dsattr)) {
4701 NFSBCOPY(buf, &dsattr, buflen);
4702 nap->na_filerev = dsattr.dsa_filerev;
4703 nap->na_size = dsattr.dsa_size;
4704 nap->na_atime = dsattr.dsa_atime;
4705 nap->na_mtime = dsattr.dsa_mtime;
4706 nap->na_bytes = dsattr.dsa_bytes;
4712 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4713 * returns 0, just return now. nfsrv_checkdsattr()
4714 * returns 0 if there is no Read/Write layout
4715 * plus either an Open/Write_access or Write
4716 * delegation issued to a client for the file.
4718 if (nfsrv_pnfsgetdsattr == 0 ||
4719 nfsrv_checkdsattr(vp, p) == 0) {
4726 * Clear ENOATTR so the code below will attempt to do a
4727 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4728 * the extended attribute.
4730 if (error == ENOATTR)
4739 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4741 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4742 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4743 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4746 for (i = 0; i < mirrorcnt; i++)
4747 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4749 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4751 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4754 if (origmircnt == -1)
4755 origmircnt = mirrorcnt;
4757 * If failpos is set to a mirror#, then that mirror has
4758 * failed and will be disabled. For Read, Getattr and Seek, the
4759 * function only tries one mirror, so if that mirror has
4760 * failed, it will need to be retried. As such, increment
4761 * tryitagain for these cases.
4762 * For Write, Setattr and Setacl, the function tries all
4763 * mirrors and will not return an error for the case where
4764 * one mirror has failed. For these cases, the functioning
4765 * mirror(s) will have been modified, so a retry isn't
4766 * necessary. These functions will set failpos for the
4769 if (ioproc == NFSPROC_READDS) {
4770 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4772 if (nfsds_failerr(error) && mirrorcnt > 1) {
4774 * Setting failpos will cause the mirror
4775 * to be disabled and then a retry of this
4782 } else if (ioproc == NFSPROC_WRITEDS)
4783 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4784 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4785 else if (ioproc == NFSPROC_SETATTR)
4786 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4787 mirrorcnt, nap, &failpos);
4788 else if (ioproc == NFSPROC_SETACL)
4789 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4790 mirrorcnt, aclp, &failpos);
4791 else if (ioproc == NFSPROC_SEEKDS) {
4792 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4794 if (nfsds_failerr(error) && mirrorcnt > 1) {
4796 * Setting failpos will cause the mirror
4797 * to be disabled and then a retry of this
4804 } else if (ioproc == NFSPROC_ALLOCATE)
4805 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4806 &nmp[0], mirrorcnt, &failpos);
4808 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4809 vp, nmp[mirrorcnt - 1], nap);
4810 if (nfsds_failerr(error) && mirrorcnt > 1) {
4812 * Setting failpos will cause the mirror
4813 * to be disabled and then a retry of this
4814 * getattr is required.
4816 failpos = mirrorcnt - 1;
4823 failnmp = nmp[failpos];
4824 NFSLOCKMNT(failnmp);
4825 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4826 NFSMNTP_CANCELRPCS)) == 0) {
4827 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4828 NFSUNLOCKMNT(failnmp);
4829 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4831 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4834 nfsrv_killrpcs(failnmp);
4835 NFSLOCKMNT(failnmp);
4836 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4839 NFSUNLOCKMNT(failnmp);
4841 for (i = 0; i < mirrorcnt; i++)
4842 NFSVOPUNLOCK(dvp[i]);
4843 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4845 /* Try the Read/Getattr again if a mirror was deleted. */
4846 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4849 /* Return ENOENT for any Extended Attribute error. */
4853 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4858 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4860 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4861 * to be checked. If it points to a NULL nmp, then it returns
4862 * a suitable destination.
4863 * curnmp - If non-NULL, it is the source mount for the copy.
4866 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4867 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4868 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4869 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4871 struct vnode *dvp, *nvp = NULL, **tdvpp;
4873 struct nfsmount *nmp, *newnmp;
4874 struct sockaddr *sad;
4875 struct sockaddr_in *sin;
4876 struct nfsdevice *ds, *tds, *fndds;
4877 struct pnfsdsfile *pf;
4879 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4881 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4890 if (newnmpp != NULL)
4895 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4896 "pnfsd.dsfile", buflenp, buf, p);
4897 mirrorcnt = *buflenp / sizeof(*pf);
4898 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4899 *buflenp != sizeof(*pf) * mirrorcnt))
4902 pf = (struct pnfsdsfile *)buf;
4903 /* If curnmp != NULL, check for a match in the mirror list. */
4904 if (curnmp != NULL && error == 0) {
4906 for (i = 0; i < mirrorcnt; i++, pf++) {
4907 sad = (struct sockaddr *)&pf->dsf_sin;
4908 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
4920 pf = (struct pnfsdsfile *)buf;
4921 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
4923 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
4925 sad = (struct sockaddr *)&pf->dsf_sin;
4927 dsdir = pf->dsf_dir;
4928 if (dsdir >= nfsrv_dsdirsize) {
4929 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
4931 } else if (nvpp != NULL && newnmp != NULL &&
4932 nfsaddr2_match(sad, newnmp->nm_nam))
4935 if (ippos != NULL && curnmp == NULL &&
4936 sad->sa_family == AF_INET &&
4937 sin->sin_addr.s_addr == 0)
4939 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
4941 /* Use the socket address to find the mount point. */
4944 /* Find a match for the IP address. */
4945 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
4946 if (ds->nfsdev_nmp != NULL) {
4947 dvp = ds->nfsdev_dvp;
4948 nmp = VFSTONFS(dvp->v_mount);
4949 if (nmp != ds->nfsdev_nmp)
4950 printf("different2 nmp %p %p\n",
4951 nmp, ds->nfsdev_nmp);
4952 if (nfsaddr2_match(sad, nmp->nm_nam)) {
4958 if (fndds != NULL && newnmpp != NULL &&
4960 /* Search for a place to make a mirror copy. */
4961 TAILQ_FOREACH(tds, &nfsrv_devidhead,
4963 if (tds->nfsdev_nmp != NULL &&
4965 ((tds->nfsdev_mdsisset == 0 &&
4966 fndds->nfsdev_mdsisset == 0) ||
4967 (tds->nfsdev_mdsisset != 0 &&
4968 fndds->nfsdev_mdsisset != 0 &&
4969 fsidcmp(&tds->nfsdev_mdsfsid,
4970 &mp->mnt_stat.f_fsid) == 0))) {
4971 *newnmpp = tds->nfsdev_nmp;
4977 * Move this entry to the end of the
4978 * list, so it won't be selected as
4979 * easily the next time.
4981 TAILQ_REMOVE(&nfsrv_devidhead, tds,
4983 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
4988 if (fndds != NULL) {
4989 dvp = fndds->nfsdev_dsdir[dsdir];
4990 if (lktype != 0 || fhiszero != 0 ||
4991 (nvpp != NULL && *nvpp == NULL)) {
4993 error = vn_lock(dvp,
4995 else if (lktype != 0)
4996 error = vn_lock(dvp, lktype);
4998 error = vn_lock(dvp, LK_SHARED);
5000 * If the file handle is all 0's, try to
5001 * do a Lookup against the DS to acquire
5003 * If dvpp == NULL or the Lookup fails,
5004 * unlock dvp after the call.
5006 if (error == 0 && (fhiszero != 0 ||
5007 (nvpp != NULL && *nvpp == NULL))) {
5008 error = nfsrv_pnfslookupds(vp,
5024 if (error != 0 || lktype == 0)
5030 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5031 if (devid != NULL) {
5032 NFSBCOPY(fndds->nfsdev_deviceid,
5033 devid, NFSX_V4DEVICEID);
5034 devid += NFSX_V4DEVICEID;
5039 NFSBCOPY(&pf->dsf_fh, fhp++,
5041 if (fnamep != NULL && gotone == 1)
5044 sizeof(pf->dsf_filename));
5046 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5051 if (error == 0 && gotone == 0)
5054 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5057 *mirrorcntp = gotone;
5059 if (gotone > 0 && dvpp != NULL) {
5061 * If the error didn't occur on the first one and
5062 * dvpp != NULL, the one(s) prior to the failure will
5063 * have locked dvp's that need to be unlocked.
5065 for (i = 0; i < gotone; i++) {
5066 NFSVOPUNLOCK(*dvpp);
5071 * If it found the vnode to be copied from before a failure,
5072 * it needs to be vput()'d.
5074 if (nvpp != NULL && *nvpp != NULL) {
5083 * Set the extended attribute for the Change attribute.
5086 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5088 struct pnfsdsattr dsattr;
5091 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5092 dsattr.dsa_filerev = nap->na_filerev;
5093 dsattr.dsa_size = nap->na_size;
5094 dsattr.dsa_atime = nap->na_atime;
5095 dsattr.dsa_mtime = nap->na_mtime;
5096 dsattr.dsa_bytes = nap->na_bytes;
5097 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5098 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5100 printf("pNFS: setextattr=%d\n", error);
5105 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5106 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5109 struct nfsrv_descript *nd;
5111 struct mbuf *m, *m2;
5112 int error = 0, retlen, tlen, trimlen;
5114 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5115 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5118 * Use a stateid where other is an alternating 01010 pattern and
5119 * seqid is 0xffffffff. This value is not defined as special by
5120 * the RFC and is used by the FreeBSD NFS server to indicate an
5121 * MDS->DS proxy operation.
5123 st.other[0] = 0x55555555;
5124 st.other[1] = 0x55555555;
5125 st.other[2] = 0x55555555;
5126 st.seqid = 0xffffffff;
5127 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5129 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5130 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5131 txdr_hyper(off, tl);
5132 *(tl + 2) = txdr_unsigned(len);
5133 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5134 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5139 if (nd->nd_repstat == 0) {
5140 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5141 NFSM_STRSIZ(retlen, len);
5143 /* Trim off the pre-data XDR from the mbuf chain. */
5145 while (m != NULL && m != nd->nd_md) {
5146 if (m->m_next == nd->nd_md) {
5148 m_freem(nd->nd_mrep);
5149 nd->nd_mrep = m = nd->nd_md;
5154 printf("nfsrv_readdsrpc: busted mbuf list\n");
5160 * Now, adjust first mbuf so that any XDR before the
5161 * read data is skipped over.
5163 trimlen = nd->nd_dpos - mtod(m, char *);
5165 m->m_len -= trimlen;
5166 NFSM_DATAP(m, trimlen);
5170 * Truncate the mbuf chain at retlen bytes of data,
5171 * plus XDR padding that brings the length up to a
5174 tlen = NFSM_RNDUP(retlen);
5176 if (m->m_len >= tlen) {
5186 } while (m != NULL);
5188 printf("nfsrv_readdsrpc: busted mbuf list\n");
5197 error = nd->nd_repstat;
5199 /* If nd->nd_mrep is already NULL, this is a no-op. */
5200 m_freem(nd->nd_mrep);
5202 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5207 * Do a write RPC on a DS data file, using this structure for the arguments,
5208 * so that this function can be executed by a separate kernel process.
5210 struct nfsrvwritedsdorpc {
5217 struct nfsmount *nmp;
5225 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5226 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5229 struct nfsrv_descript *nd;
5230 nfsattrbit_t attrbits;
5232 int commit, error, retlen;
5234 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5235 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5236 sizeof(fhandle_t), NULL, NULL, 0, 0);
5239 * Use a stateid where other is an alternating 01010 pattern and
5240 * seqid is 0xffffffff. This value is not defined as special by
5241 * the RFC and is used by the FreeBSD NFS server to indicate an
5242 * MDS->DS proxy operation.
5244 st.other[0] = 0x55555555;
5245 st.other[1] = 0x55555555;
5246 st.other[2] = 0x55555555;
5247 st.seqid = 0xffffffff;
5248 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5249 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5250 txdr_hyper(off, tl);
5253 * Do all writes FileSync, since the server doesn't hold onto dirty
5254 * buffers. Since clients should be accessing the DS servers directly
5255 * using the pNFS layouts, this just needs to work correctly as a
5258 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5259 *tl = txdr_unsigned(len);
5260 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5262 /* Put data in mbuf chain. */
5263 nd->nd_mb->m_next = m;
5265 /* Set nd_mb and nd_bpos to end of data. */
5266 while (m->m_next != NULL)
5269 nfsm_set(nd, m->m_len);
5270 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5272 /* Do a Getattr for the attributes that change upon writing. */
5273 NFSZERO_ATTRBIT(&attrbits);
5274 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5275 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5276 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5277 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5278 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5279 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5280 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5281 (void) nfsrv_putattrbit(nd, &attrbits);
5282 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5283 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5288 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5289 /* Get rid of weak cache consistency data for now. */
5290 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5291 (ND_NFSV4 | ND_V4WCCATTR)) {
5292 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5293 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5294 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5298 * Get rid of Op# and status for next op.
5300 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5302 nd->nd_flag |= ND_NOMOREDATA;
5304 if (nd->nd_repstat == 0) {
5305 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5306 retlen = fxdr_unsigned(int, *tl++);
5307 commit = fxdr_unsigned(int, *tl);
5308 if (commit != NFSWRITE_FILESYNC)
5310 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5311 retlen, commit, error);
5313 error = nd->nd_repstat;
5314 /* We have no use for the Write Verifier since we use FileSync. */
5317 * Get the Change, Size, Access Time and Modify Time attributes and set
5318 * on the Metadata file, so its attributes will be what the file's
5319 * would be if it had been written.
5322 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5323 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5324 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5326 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5328 m_freem(nd->nd_mrep);
5330 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5335 * Start up the thread that will execute nfsrv_writedsdorpc().
5338 start_writedsdorpc(void *arg, int pending)
5340 struct nfsrvwritedsdorpc *drpc;
5342 drpc = (struct nfsrvwritedsdorpc *)arg;
5343 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5344 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5346 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5350 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5351 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5352 struct mbuf **mpp, char *cp, int *failposp)
5354 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5357 int error, i, offs, ret, timo;
5359 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5360 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5363 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5366 /* Calculate offset in mbuf chain that data starts. */
5367 offs = cp - mtod(*mpp, char *);
5368 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5371 * Do the write RPC for every DS, using a separate kernel process
5372 * for every DS except the last one.
5375 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5377 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5385 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5387 if (nfs_pnfsiothreads != 0) {
5388 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5389 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5393 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5395 if (nfsds_failerr(ret) && *failposp == -1)
5397 else if (error == 0 && ret != 0)
5403 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5404 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5405 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5406 *failposp = mirrorcnt - 1;
5407 else if (error == 0 && ret != 0)
5410 error = nfsrv_setextattr(vp, &na, p);
5411 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5413 timo = hz / 50; /* Wait for 20msec. */
5416 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5417 /* Wait for RPCs on separate threads to complete. */
5418 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5419 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5420 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5422 else if (error == 0 && tdrpc->err != 0)
5430 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5431 * so that this function can be executed by a separate kernel process.
5433 struct nfsrvallocatedsdorpc {
5440 struct nfsmount *nmp;
5447 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5448 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5451 struct nfsrv_descript *nd;
5452 nfsattrbit_t attrbits;
5456 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5457 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5458 sizeof(fhandle_t), NULL, NULL, 0, 0);
5461 * Use a stateid where other is an alternating 01010 pattern and
5462 * seqid is 0xffffffff. This value is not defined as special by
5463 * the RFC and is used by the FreeBSD NFS server to indicate an
5464 * MDS->DS proxy operation.
5466 st.other[0] = 0x55555555;
5467 st.other[1] = 0x55555555;
5468 st.other[2] = 0x55555555;
5469 st.seqid = 0xffffffff;
5470 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5471 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5472 txdr_hyper(off, tl); tl += 2;
5473 txdr_hyper(len, tl); tl += 2;
5474 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5476 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5477 NFSGETATTR_ATTRBIT(&attrbits);
5478 nfsrv_putattrbit(nd, &attrbits);
5479 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5480 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5485 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5487 if (nd->nd_repstat == 0) {
5488 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5489 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5490 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5492 error = nd->nd_repstat;
5493 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5495 m_freem(nd->nd_mrep);
5497 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5502 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5505 start_allocatedsdorpc(void *arg, int pending)
5507 struct nfsrvallocatedsdorpc *drpc;
5509 drpc = (struct nfsrvallocatedsdorpc *)arg;
5510 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5511 drpc->len, NULL, drpc->cred, drpc->p);
5513 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5517 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5518 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5521 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5523 int error, i, ret, timo;
5525 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5528 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5532 * Do the allocate RPC for every DS, using a separate kernel process
5533 * for every DS except the last one.
5536 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5538 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5547 if (nfs_pnfsiothreads != 0) {
5548 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5549 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5553 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5555 if (nfsds_failerr(ret) && *failposp == -1)
5557 else if (error == 0 && ret != 0)
5563 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5564 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5565 *failposp = mirrorcnt - 1;
5566 else if (error == 0 && ret != 0)
5569 error = nfsrv_setextattr(vp, &na, p);
5570 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5572 timo = hz / 50; /* Wait for 20msec. */
5575 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5576 /* Wait for RPCs on separate threads to complete. */
5577 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5578 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5579 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5581 else if (error == 0 && tdrpc->err != 0)
5589 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5590 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5591 struct nfsvattr *dsnap)
5594 struct nfsrv_descript *nd;
5596 nfsattrbit_t attrbits;
5599 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5600 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5602 * Use a stateid where other is an alternating 01010 pattern and
5603 * seqid is 0xffffffff. This value is not defined as special by
5604 * the RFC and is used by the FreeBSD NFS server to indicate an
5605 * MDS->DS proxy operation.
5607 st.other[0] = 0x55555555;
5608 st.other[1] = 0x55555555;
5609 st.other[2] = 0x55555555;
5610 st.seqid = 0xffffffff;
5611 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5613 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5614 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5616 /* Do a Getattr for the attributes that change due to writing. */
5617 NFSZERO_ATTRBIT(&attrbits);
5618 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5619 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5620 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5621 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5622 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5623 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5624 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5625 (void) nfsrv_putattrbit(nd, &attrbits);
5626 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5627 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5632 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5634 /* Get rid of weak cache consistency data for now. */
5635 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5636 (ND_NFSV4 | ND_V4WCCATTR)) {
5637 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5638 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5639 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5643 * Get rid of Op# and status for next op.
5645 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5647 nd->nd_flag |= ND_NOMOREDATA;
5649 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5652 if (nd->nd_repstat != 0)
5653 error = nd->nd_repstat;
5655 * Get the Change, Size, Access Time and Modify Time attributes and set
5656 * on the Metadata file, so its attributes will be what the file's
5657 * would be if it had been written.
5660 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5661 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5662 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5664 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5666 m_freem(nd->nd_mrep);
5668 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5672 struct nfsrvsetattrdsdorpc {
5677 struct nfsmount *nmp;
5682 struct nfsvattr dsna;
5687 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5690 start_setattrdsdorpc(void *arg, int pending)
5692 struct nfsrvsetattrdsdorpc *drpc;
5694 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5695 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5696 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5701 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5702 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5703 struct nfsvattr *nap, int *failposp)
5705 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5707 int error, i, ret, timo;
5709 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5712 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5716 * Do the setattr RPC for every DS, using a separate kernel process
5717 * for every DS except the last one.
5720 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5723 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5731 if (nfs_pnfsiothreads != 0) {
5732 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5733 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5737 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5739 if (nfsds_failerr(ret) && *failposp == -1)
5741 else if (error == 0 && ret != 0)
5747 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5748 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5749 *failposp = mirrorcnt - 1;
5750 else if (error == 0 && ret != 0)
5753 error = nfsrv_setextattr(vp, &na, p);
5754 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5756 timo = hz / 50; /* Wait for 20msec. */
5759 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5760 /* Wait for RPCs on separate threads to complete. */
5761 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5762 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5763 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5765 else if (error == 0 && tdrpc->err != 0)
5773 * Do a Setattr of an NFSv4 ACL on the DS file.
5776 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5777 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5779 struct nfsrv_descript *nd;
5781 nfsattrbit_t attrbits;
5784 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5785 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5787 * Use a stateid where other is an alternating 01010 pattern and
5788 * seqid is 0xffffffff. This value is not defined as special by
5789 * the RFC and is used by the FreeBSD NFS server to indicate an
5790 * MDS->DS proxy operation.
5792 st.other[0] = 0x55555555;
5793 st.other[1] = 0x55555555;
5794 st.other[2] = 0x55555555;
5795 st.seqid = 0xffffffff;
5796 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5798 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5799 NFSZERO_ATTRBIT(&attrbits);
5800 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5802 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5803 * so passing in the metadata "vp" will be ok, since it is of
5804 * the same type (VREG).
5806 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5807 NULL, 0, 0, 0, 0, 0, NULL);
5808 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5809 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5814 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5816 error = nd->nd_repstat;
5817 m_freem(nd->nd_mrep);
5822 struct nfsrvsetacldsdorpc {
5827 struct nfsmount *nmp;
5836 * Start up the thread that will execute nfsrv_setacldsdorpc().
5839 start_setacldsdorpc(void *arg, int pending)
5841 struct nfsrvsetacldsdorpc *drpc;
5843 drpc = (struct nfsrvsetacldsdorpc *)arg;
5844 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5845 drpc->vp, drpc->nmp, drpc->aclp);
5850 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5851 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5854 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
5855 int error, i, ret, timo;
5857 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5860 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5864 * Do the setattr RPC for every DS, using a separate kernel process
5865 * for every DS except the last one.
5868 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5871 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5879 if (nfs_pnfsiothreads != 0) {
5880 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5881 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5885 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5887 if (nfsds_failerr(ret) && *failposp == -1)
5889 else if (error == 0 && ret != 0)
5895 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5896 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5897 *failposp = mirrorcnt - 1;
5898 else if (error == 0 && ret != 0)
5900 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5902 timo = hz / 50; /* Wait for 20msec. */
5905 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5906 /* Wait for RPCs on separate threads to complete. */
5907 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5908 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
5909 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5911 else if (error == 0 && tdrpc->err != 0)
5919 * Getattr call to the DS for the attributes that change due to writing.
5922 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5923 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
5925 struct nfsrv_descript *nd;
5927 nfsattrbit_t attrbits;
5929 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
5930 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5931 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
5932 sizeof(fhandle_t), NULL, NULL, 0, 0);
5933 NFSZERO_ATTRBIT(&attrbits);
5934 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5935 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5936 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5937 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5938 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5939 (void) nfsrv_putattrbit(nd, &attrbits);
5940 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5941 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5946 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
5948 if (nd->nd_repstat == 0) {
5949 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5950 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5953 * We can only save the updated values in the extended
5954 * attribute if the vp is exclusively locked.
5955 * This should happen when any of the following operations
5956 * occur on the vnode:
5957 * Close, Delegreturn, LayoutCommit, LayoutReturn
5958 * As such, the updated extended attribute should get saved
5959 * before nfsrv_checkdsattr() returns 0 and allows the cached
5960 * attributes to be returned without calling this function.
5962 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
5963 error = nfsrv_setextattr(vp, nap, p);
5964 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
5968 error = nd->nd_repstat;
5969 m_freem(nd->nd_mrep);
5971 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
5976 * Seek call to a DS.
5979 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
5980 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
5983 struct nfsrv_descript *nd;
5987 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
5989 * Use a stateid where other is an alternating 01010 pattern and
5990 * seqid is 0xffffffff. This value is not defined as special by
5991 * the RFC and is used by the FreeBSD NFS server to indicate an
5992 * MDS->DS proxy operation.
5994 st.other[0] = 0x55555555;
5995 st.other[1] = 0x55555555;
5996 st.other[2] = 0x55555555;
5997 st.seqid = 0xffffffff;
5998 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5999 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6000 sizeof(fhandle_t), NULL, NULL, 0, 0);
6001 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6002 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6003 txdr_hyper(*offp, tl); tl += 2;
6004 *tl = txdr_unsigned(content);
6005 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6006 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6011 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6012 if (nd->nd_repstat == 0) {
6013 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6014 if (*tl++ == newnfs_true)
6018 *offp = fxdr_hyper(tl);
6020 error = nd->nd_repstat;
6022 m_freem(nd->nd_mrep);
6024 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6029 * Get the device id and file handle for a DS file.
6032 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6033 fhandle_t *fhp, char *devid)
6039 buf = malloc(buflen, M_TEMP, M_WAITOK);
6040 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6041 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6047 * Do a Lookup against the DS for the filename.
6050 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6051 struct vnode **nvpp, NFSPROC_T *p)
6053 struct nameidata named;
6054 struct ucred *tcred;
6060 tcred = newnfs_getcred();
6061 named.ni_cnd.cn_nameiop = LOOKUP;
6062 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6063 named.ni_cnd.cn_cred = tcred;
6064 named.ni_cnd.cn_thread = p;
6065 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
6066 nfsvno_setpathbuf(&named, &bufp, &hashp);
6067 named.ni_cnd.cn_nameptr = bufp;
6068 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6069 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6070 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6071 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6072 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6074 nfsvno_relpathbuf(&named);
6077 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6082 * Set the file handle to the correct one.
6085 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6086 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6092 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6094 * We can only do a vn_set_extattr() if the vnode is exclusively
6095 * locked and vn_start_write() has been done. If devid != NULL or
6096 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6097 * may not have been done.
6098 * If not done now, it will be done on a future call.
6100 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6102 ret = vn_extattr_set(vp, IO_NODELOCKED,
6103 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6105 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6109 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6110 * when the DS has failed.
6113 nfsrv_killrpcs(struct nfsmount *nmp)
6117 * Call newnfs_nmcancelreqs() to cause
6118 * any RPCs in progress on the mount point to
6120 * This will cause any process waiting for an
6121 * RPC to complete while holding a vnode lock
6122 * on the mounted-on vnode (such as "df" or
6123 * a non-forced "umount") to fail.
6124 * This will unlock the mounted-on vnode so
6125 * a forced dismount can succeed.
6126 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6129 newnfs_nmcancelreqs(nmp);
6133 * Sum up the statfs info for each of the DSs, so that the client will
6134 * receive the total for all DSs.
6137 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6140 struct nfsdevice *ds;
6141 struct vnode **dvpp, **tdvpp, *dvp;
6143 int cnt, error = 0, i;
6145 if (nfsrv_devidcnt <= 0)
6147 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6148 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6150 /* Get an array of the dvps for the DSs. */
6154 /* First, search for matches for same file system. */
6155 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6156 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6157 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6158 if (++i > nfsrv_devidcnt)
6160 *tdvpp++ = ds->nfsdev_dvp;
6164 * If no matches for same file system, total all servers not assigned
6168 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6169 if (ds->nfsdev_nmp != NULL &&
6170 ds->nfsdev_mdsisset == 0) {
6171 if (++i > nfsrv_devidcnt)
6173 *tdvpp++ = ds->nfsdev_dvp;
6180 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6182 for (i = 0; i < cnt && error == 0; i++) {
6184 error = VFS_STATFS(dvp->v_mount, tsf);
6186 if (sf->f_bsize == 0) {
6187 if (tsf->f_bsize > 0)
6188 sf->f_bsize = tsf->f_bsize;
6192 if (tsf->f_blocks > 0) {
6193 if (sf->f_bsize != tsf->f_bsize) {
6194 tot = tsf->f_blocks * tsf->f_bsize;
6195 sf->f_blocks += (tot / sf->f_bsize);
6197 sf->f_blocks += tsf->f_blocks;
6199 if (tsf->f_bfree > 0) {
6200 if (sf->f_bsize != tsf->f_bsize) {
6201 tot = tsf->f_bfree * tsf->f_bsize;
6202 sf->f_bfree += (tot / sf->f_bsize);
6204 sf->f_bfree += tsf->f_bfree;
6206 if (tsf->f_bavail > 0) {
6207 if (sf->f_bsize != tsf->f_bsize) {
6208 tot = tsf->f_bavail * tsf->f_bsize;
6209 sf->f_bavail += (tot / sf->f_bsize);
6211 sf->f_bavail += tsf->f_bavail;
6224 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6228 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6229 error = NFSERR_ATTRNOTSUPP;
6233 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6234 * Make sure it has enough room for that - splitting every entry
6235 * into two and appending "canonical six" entries at the end.
6236 * Cribbed out of kern/vfs_acl.c - Rick M.
6238 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6239 error = NFSERR_ATTRNOTSUPP;
6242 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6244 error = nfsrv_dssetacl(vp, aclp, cred, p);
6245 if (error == ENOENT)
6255 * Seek vnode op call (actually it is a VOP_IOCTL()).
6256 * This function is called with the vnode locked, but unlocks and vrele()s
6257 * the vp before returning.
6260 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6261 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6266 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6268 * Attempt to seek on a DS file. A return of ENOENT implies
6269 * there is no DS file to seek on.
6271 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6272 NULL, NULL, NULL, NULL, offp, content, eofp);
6273 if (error != ENOENT) {
6279 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6280 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6281 * NFSv4.2 is *eofp == true and error == 0 for this case.
6284 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6286 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6287 /* Handle the cases where we might be at EOF. */
6288 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6289 if (ret == 0 && *offp == at.na_size) {
6293 if (ret != 0 && error == 0)
6302 * Allocate vnode op call.
6305 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6310 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6312 * Attempt to allocate on a DS file. A return of ENOENT implies
6313 * there is no DS file to allocate on.
6315 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6316 NULL, NULL, NULL, NULL, &len, 0, NULL);
6317 if (error != ENOENT)
6322 * Do the actual VOP_ALLOCATE(), looping a reasonable number of
6323 * times to achieve completion.
6326 while (error == 0 && len > 0 && trycnt++ < 20)
6327 error = VOP_ALLOCATE(vp, &off, &len);
6328 if (error == 0 && len > 0)
6335 * Get Extended Atribute vnode op into an mbuf list.
6338 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6339 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6340 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6343 struct uio io, *uiop = &io;
6344 struct mbuf *m, *m2;
6345 int alen, error, len, tlen;
6348 /* First, find out the size of the extended attribute. */
6349 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6352 return (NFSERR_NOXATTR);
6353 if (siz > maxresp - NFS_MAXXDR)
6354 return (NFSERR_XATTR2BIG);
6356 tlen = NFSM_RNDUP(len);
6359 * If cnt > MCLBYTES and the reply will not be saved, use
6360 * ext_pgs mbufs for TLS.
6361 * For NFSv4.0, we do not know for sure if the reply will
6362 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6363 * Always use ext_pgs mbufs if ND_EXTPG is set.
6365 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6366 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6367 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6368 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6369 maxextsiz, &m, &m2, &iv);
6371 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6375 uiop->uio_iovcnt = 0;
6376 uiop->uio_iov = iv = NULL;
6379 uiop->uio_offset = 0;
6380 uiop->uio_resid = tlen;
6381 uiop->uio_rw = UIO_READ;
6382 uiop->uio_segflg = UIO_SYSSPACE;
6385 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6392 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6396 if (uiop->uio_resid > 0) {
6398 len = tlen - uiop->uio_resid;
6399 tlen = NFSM_RNDUP(len);
6401 printf("nfsvno_getxattr: weird size read\n");
6405 } else if (alen != tlen || tlen != len)
6406 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6424 * Set Extended attribute vnode op from an mbuf list.
6427 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6428 char *cp, struct ucred *cred, struct thread *p)
6431 struct uio uio, *uiop = &uio;
6436 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6442 uiop->uio_rw = UIO_WRITE;
6443 uiop->uio_segflg = UIO_SYSSPACE;
6445 uiop->uio_offset = 0;
6446 uiop->uio_resid = len;
6448 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6450 uiop->uio_iovcnt = cnt;
6452 uiop->uio_iov = iv = NULL;
6453 uiop->uio_iovcnt = 0;
6456 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6467 * Remove Extended attribute vnode op.
6470 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6471 struct ucred *cred, struct thread *p)
6476 * Get rid of any delegations. I am not sure why this is required,
6477 * but RFC-8276 says so.
6479 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6483 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6489 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6490 if (error == EOPNOTSUPP)
6491 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6499 * List Extended Atribute vnode op into an mbuf list.
6502 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6503 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6511 /* First, find out the size of the extended attribute. */
6512 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6515 return (NFSERR_NOXATTR);
6516 if (siz <= cookie) {
6521 if (siz > cookie + *lenp) {
6522 siz = cookie + *lenp;
6526 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6527 if (siz > 10 * 1024 * 1024) {
6528 error = NFSERR_XATTR2BIG;
6531 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6532 iv.iov_base = *bufp;
6538 io.uio_rw = UIO_READ;
6539 io.uio_segflg = UIO_SYSSPACE;
6542 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6547 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6551 if (io.uio_resid > 0)
6552 siz -= io.uio_resid;
6557 free(*bufp, M_TEMP);
6565 * Trim trailing data off the mbuf list being built.
6568 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6569 int bextpg, int bextpgsiz)
6574 if (mb->m_next != NULL) {
6575 m_freem(mb->m_next);
6578 if ((mb->m_flags & M_EXTPG) != 0) {
6579 /* First, get rid of any pages after this position. */
6580 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
6581 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
6582 vm_page_unwire_noq(pg);
6585 mb->m_epg_npgs = bextpg + 1;
6587 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
6589 fullpgsiz = PAGE_SIZE;
6590 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
6591 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
6592 for (i = 1; i < mb->m_epg_npgs; i++)
6593 mb->m_len += m_epg_pagelen(mb, i, 0);
6594 nd->nd_bextpgsiz = bextpgsiz;
6595 nd->nd_bextpg = bextpg;
6597 mb->m_len = bpos - mtod(mb, char *);
6602 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
6605 * Called once to initialize data structures...
6608 nfsd_modevent(module_t mod, int type, void *data)
6611 static int loaded = 0;
6618 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6619 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
6621 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
6624 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
6625 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
6626 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
6627 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
6628 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
6629 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
6636 #ifdef VV_DISABLEDELEG
6637 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
6638 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
6640 nfsd_call_servertimer = nfsrv_servertimer;
6641 nfsd_call_nfsd = nfssvc_nfsd;
6646 if (newnfs_numnfsd != 0) {
6651 #ifdef VV_DISABLEDELEG
6652 vn_deleg_ops.vndeleg_recall = NULL;
6653 vn_deleg_ops.vndeleg_disable = NULL;
6655 nfsd_call_servertimer = NULL;
6656 nfsd_call_nfsd = NULL;
6658 /* Clean out all NFSv4 state. */
6659 nfsrv_throwawayallstate(curthread);
6661 /* Clean the NFS server reply cache */
6662 nfsrvd_cleancache();
6664 /* Free up the krpc server pool. */
6665 if (nfsrvd_pool != NULL)
6666 svcpool_destroy(nfsrvd_pool);
6668 /* and get rid of the locks */
6669 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6670 mtx_destroy(&nfsrchash_table[i].mtx);
6671 mtx_destroy(&nfsrcahash_table[i].mtx);
6673 mtx_destroy(&nfsrc_udpmtx);
6674 mtx_destroy(&nfs_v4root_mutex);
6675 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
6676 mtx_destroy(&nfsrv_dontlistlock_mtx);
6677 mtx_destroy(&nfsrv_recalllock_mtx);
6678 for (i = 0; i < nfsrv_sessionhashsize; i++)
6679 mtx_destroy(&nfssessionhash[i].mtx);
6680 if (nfslayouthash != NULL) {
6681 for (i = 0; i < nfsrv_layouthashsize; i++)
6682 mtx_destroy(&nfslayouthash[i].mtx);
6683 free(nfslayouthash, M_NFSDSESSION);
6685 lockdestroy(&nfsv4root_mnt.mnt_explock);
6686 free(nfsclienthash, M_NFSDCLIENT);
6687 free(nfslockhash, M_NFSDLOCKFILE);
6688 free(nfssessionhash, M_NFSDSESSION);
6700 static moduledata_t nfsd_mod = {
6705 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
6707 /* So that loader and kldload(2) can find us, wherever we are.. */
6708 MODULE_VERSION(nfsd, 1);
6709 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
6710 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
6711 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
6712 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);