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 *);
150 int nfs_pnfsio(task_fn_t *, void *);
152 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
154 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
155 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
156 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
158 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
160 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
161 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
162 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
163 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
164 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
165 0, "Debug level for NFS server");
166 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
167 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
168 static int nfsrv_pnfsgetdsattr = 1;
169 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
170 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
173 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
175 * The dsN subdirectories for the increased values must have been created
176 * on all DS servers before this increase is done.
178 u_int nfsrv_dsdirsize = 20;
180 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
182 int error, newdsdirsize;
184 newdsdirsize = nfsrv_dsdirsize;
185 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
186 if (error != 0 || req->newptr == NULL)
188 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
191 nfsrv_dsdirsize = newdsdirsize;
194 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
195 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
196 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
198 #define MAX_REORDERED_RPC 16
199 #define NUM_HEURISTIC 1031
200 #define NHUSE_INIT 64
202 #define NHUSE_MAX 2048
204 static struct nfsheur {
205 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
206 off_t nh_nextoff; /* next offset for sequential detection */
207 int nh_use; /* use count for selection */
208 int nh_seqcount; /* heuristic */
209 } nfsheur[NUM_HEURISTIC];
212 * Heuristic to detect sequential operation.
214 static struct nfsheur *
215 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
220 /* Locate best candidate. */
222 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
225 if (nfsheur[hi].nh_vp == vp) {
229 if (nfsheur[hi].nh_use > 0)
230 --nfsheur[hi].nh_use;
231 hi = (hi + 1) % NUM_HEURISTIC;
232 if (nfsheur[hi].nh_use < nh->nh_use)
236 /* Initialize hint if this is a new file. */
237 if (nh->nh_vp != vp) {
239 nh->nh_nextoff = uio->uio_offset;
240 nh->nh_use = NHUSE_INIT;
241 if (uio->uio_offset == 0)
247 /* Calculate heuristic. */
248 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
249 uio->uio_offset == nh->nh_nextoff) {
250 /* See comments in vfs_vnops.c:sequential_heuristic(). */
251 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
252 if (nh->nh_seqcount > IO_SEQMAX)
253 nh->nh_seqcount = IO_SEQMAX;
254 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
255 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
256 /* Probably a reordered RPC, leave seqcount alone. */
257 } else if (nh->nh_seqcount > 1) {
258 nh->nh_seqcount /= 2;
262 nh->nh_use += NHUSE_INC;
263 if (nh->nh_use > NHUSE_MAX)
264 nh->nh_use = NHUSE_MAX;
269 * Get attributes into nfsvattr structure.
272 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
273 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
274 nfsattrbit_t *attrbitp)
276 int error, gotattr, lockedit = 0;
279 if (vpislocked == 0) {
281 * When vpislocked == 0, the vnode is either exclusively
282 * locked by this thread or not locked by this thread.
283 * As such, shared lock it, if not exclusively locked.
285 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
287 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
292 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
293 * attributes, as required.
294 * This needs to be done for regular files if:
295 * - non-NFSv4 RPCs or
296 * - when attrbitp == NULL or
297 * - an NFSv4 RPC with any of the above attributes in attrbitp.
298 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
299 * these attributes. nfsrv_proxyds() will return an error if the
300 * server is not a pNFS one.
303 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
304 (nd->nd_flag & ND_NFSV4) == 0 ||
305 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
306 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
307 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
308 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
309 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
310 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
311 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
317 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
322 * If we got the Change, Size and Modify Time from the DS,
326 nvap->na_atime = na.na_atime;
327 nvap->na_mtime = na.na_mtime;
328 nvap->na_filerev = na.na_filerev;
329 nvap->na_size = na.na_size;
330 nvap->na_bytes = na.na_bytes;
332 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
333 error, (uintmax_t)na.na_filerev);
340 * Get a file handle for a vnode.
343 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
347 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
348 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
349 error = VOP_VPTOFH(vp, &fhp->fh_fid);
356 * Perform access checking for vnodes obtained from file handles that would
357 * refer to files already opened by a Unix client. You cannot just use
358 * vn_writechk() and VOP_ACCESSX() for two reasons.
359 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
361 * 2 - The owner is to be given access irrespective of mode bits for some
362 * operations, so that processes that chmod after opening a file don't
366 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
367 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
368 u_int32_t *supportedtypep)
371 int error = 0, getret = 0;
373 if (vpislocked == 0) {
374 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
379 if (accmode & VWRITE) {
380 /* Just vn_writechk() changed to check rdonly */
382 * Disallow write attempts on read-only file systems;
383 * unless the file is a socket or a block or character
384 * device resident on the file system.
386 if (NFSVNO_EXRDONLY(exp) ||
387 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
388 switch (vp->v_type) {
398 * If there's shared text associated with
399 * the inode, try to free it up once. If
400 * we fail, we can't allow writing.
402 if (VOP_IS_TEXT(vp) && error == 0)
412 * Should the override still be applied when ACLs are enabled?
414 error = VOP_ACCESSX(vp, accmode, cred, p);
415 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
417 * Try again with VEXPLICIT_DENY, to see if the test for
418 * deletion is supported.
420 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
422 if (vp->v_type == VDIR) {
423 accmode &= ~(VDELETE | VDELETE_CHILD);
425 error = VOP_ACCESSX(vp, accmode, cred, p);
426 } else if (supportedtypep != NULL) {
427 *supportedtypep &= ~NFSACCESS_DELETE;
433 * Allow certain operations for the owner (reads and writes
434 * on files that are already open).
436 if (override != NFSACCCHK_NOOVERRIDE &&
437 (error == EPERM || error == EACCES)) {
438 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
440 else if (override & NFSACCCHK_ALLOWOWNER) {
441 getret = VOP_GETATTR(vp, &vattr, cred);
442 if (getret == 0 && cred->cr_uid == vattr.va_uid)
455 * Set attribute(s) vnop.
458 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
459 struct thread *p, struct nfsexstuff *exp)
461 u_quad_t savsize = 0;
466 * If this is an exported file system and a pNFS service is running,
467 * don't VOP_SETATTR() of size for the MDS file system.
471 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
472 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
473 nvap->na_vattr.va_size > 0) {
474 savsize = nvap->na_vattr.va_size;
475 nvap->na_vattr.va_size = VNOVAL;
476 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
477 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
478 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
479 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
480 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
486 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
488 nvap->na_vattr.va_size = savsize;
489 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
490 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
491 nvap->na_vattr.va_size != VNOVAL ||
492 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
493 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
494 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
495 /* Never modify birthtime on a DS file. */
496 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
497 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
498 /* For a pNFS server, set the attributes on the DS file. */
499 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
500 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
501 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
510 * Set up nameidata for a lookup() call and do it.
513 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
514 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
515 struct vnode **retdirp)
517 struct componentname *cnp = &ndp->ni_cnd;
521 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
526 cnp->cn_nameptr = cnp->cn_pnbuf;
529 * Extract and set starting directory.
531 if (dp->v_type != VDIR) {
536 nfsvno_relpathbuf(ndp);
544 if (NFSVNO_EXRDONLY(exp))
545 cnp->cn_flags |= RDONLY;
546 ndp->ni_segflg = UIO_SYSSPACE;
548 if (nd->nd_flag & ND_PUBLOOKUP) {
550 if (cnp->cn_pnbuf[0] == '/') {
553 * Check for degenerate pathnames here, since lookup()
556 for (i = 1; i < ndp->ni_pathlen; i++)
557 if (cnp->cn_pnbuf[i] != '/')
559 if (i == ndp->ni_pathlen) {
560 error = NFSERR_ACCES;
566 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
567 (nd->nd_flag & ND_NFSV4) == 0) {
569 * Only cross mount points for NFSv4 when doing a
570 * mount while traversing the file system above
571 * the mount point, unless nfsrv_enable_crossmntpt is set.
573 cnp->cn_flags |= NOCROSSMOUNT;
577 * Initialize for scan, set ni_startdir and bump ref on dp again
578 * because lookup() will dereference ni_startdir.
582 ndp->ni_startdir = dp;
583 ndp->ni_rootdir = rootvnode;
584 ndp->ni_topdir = NULL;
587 cnp->cn_flags |= LOCKLEAF;
589 cnp->cn_nameptr = cnp->cn_pnbuf;
591 * Call lookup() to do the real work. If an error occurs,
592 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
593 * we do not have to dereference anything before returning.
594 * In either case ni_startdir will be dereferenced and NULLed
602 * Check for encountering a symbolic link. Trivial
603 * termination occurs if no symlink encountered.
605 if ((cnp->cn_flags & ISSYMLINK) == 0) {
606 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
607 nfsvno_relpathbuf(ndp);
608 if (ndp->ni_vp && !lockleaf)
609 NFSVOPUNLOCK(ndp->ni_vp);
616 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
617 NFSVOPUNLOCK(ndp->ni_dvp);
618 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
623 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
627 if (ndp->ni_pathlen > 1)
628 cp = uma_zalloc(namei_zone, M_WAITOK);
632 aiov.iov_len = MAXPATHLEN;
633 auio.uio_iov = &aiov;
636 auio.uio_rw = UIO_READ;
637 auio.uio_segflg = UIO_SYSSPACE;
639 auio.uio_resid = MAXPATHLEN;
640 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
643 if (ndp->ni_pathlen > 1)
644 uma_zfree(namei_zone, cp);
650 linklen = MAXPATHLEN - auio.uio_resid;
655 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
656 error = ENAMETOOLONG;
661 * Adjust or replace path
663 if (ndp->ni_pathlen > 1) {
664 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
665 uma_zfree(namei_zone, cnp->cn_pnbuf);
668 cnp->cn_pnbuf[linklen] = '\0';
669 ndp->ni_pathlen += linklen;
672 * Cleanup refs for next loop and check if root directory
673 * should replace current directory. Normally ni_dvp
674 * becomes the new base directory and is cleaned up when
675 * we loop. Explicitly null pointers after invalidation
676 * to clarify operation.
681 if (cnp->cn_pnbuf[0] == '/') {
683 ndp->ni_dvp = ndp->ni_rootdir;
686 ndp->ni_startdir = ndp->ni_dvp;
690 cnp->cn_flags &= ~LOCKLEAF;
694 nfsvno_relpathbuf(ndp);
697 ndp->ni_startdir = NULL;
698 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
703 NFSEXITCODE2(error, nd);
708 * Set up a pathname buffer and return a pointer to it and, optionally
709 * set a hash pointer.
712 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
714 struct componentname *cnp = &ndp->ni_cnd;
716 cnp->cn_flags |= (NOMACCHECK | HASBUF);
717 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
720 *bufpp = cnp->cn_pnbuf;
724 * Release the above path buffer, if not released by nfsvno_namei().
727 nfsvno_relpathbuf(struct nameidata *ndp)
730 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
732 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
733 ndp->ni_cnd.cn_flags &= ~HASBUF;
737 * Readlink vnode op into an mbuf list.
740 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
741 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
744 struct uio io, *uiop = &io;
745 struct mbuf *mp, *mp3;
746 int len, tlen, error = 0;
748 len = NFS_MAXPATHLEN;
750 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
753 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
755 uiop->uio_offset = 0;
756 uiop->uio_resid = len;
757 uiop->uio_rw = UIO_READ;
758 uiop->uio_segflg = UIO_SYSSPACE;
760 error = VOP_READLINK(vp, uiop, cred);
767 if (uiop->uio_resid > 0) {
768 len -= uiop->uio_resid;
769 tlen = NFSM_RNDUP(len);
773 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
774 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
787 * Create an mbuf chain and an associated iovec that can be used to Read
788 * or Getextattr of data.
789 * Upon success, return pointers to the first and last mbufs in the chain
790 * plus the malloc'd iovec and its iovlen.
793 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
796 struct mbuf *m, *m2 = NULL, *m3;
803 * Generate the mbuf list with the uio_iov ref. to it.
810 siz = min(M_TRAILINGSPACE(m), left);
819 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
825 panic("nfsrv_createiovec iov");
826 siz = min(M_TRAILINGSPACE(m), left);
828 iv->iov_base = mtod(m, caddr_t) + m->m_len;
843 * Create an mbuf chain and an associated iovec that can be used to Read
844 * or Getextattr of data.
845 * Upon success, return pointers to the first and last mbufs in the chain
846 * plus the malloc'd iovec and its iovlen.
847 * Same as above, but creates ext_pgs mbuf(s).
850 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
851 struct mbuf **mpendp, struct iovec **ivp)
853 struct mbuf *m, *m2 = NULL, *m3;
855 int i, left, pgno, siz;
860 * Generate the mbuf list with the uio_iov ref. to it.
864 siz = min(left, maxextsiz);
865 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
874 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
881 panic("nfsvno_createiovec_extpgs iov");
882 siz = min(PAGE_SIZE, left);
884 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
887 if (pgno == m->m_epg_npgs - 1)
888 m->m_epg_last_len = siz;
894 if (pgno == m->m_epg_npgs && left > 0) {
897 panic("nfsvno_createiovec_extpgs iov");
907 * Read vnode op call into mbuf list.
910 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
911 int maxextsiz, struct thread *p, struct mbuf **mpp,
912 struct mbuf **mpendp)
916 int error = 0, len, tlen, ioflag = 0;
918 struct uio io, *uiop = &io;
922 * Attempt to read from a DS file. A return of ENOENT implies
923 * there is no DS file to read.
925 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
926 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
930 len = NFSM_RNDUP(cnt);
932 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
935 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
937 uiop->uio_offset = off;
938 uiop->uio_resid = len;
939 uiop->uio_rw = UIO_READ;
940 uiop->uio_segflg = UIO_SYSSPACE;
942 nh = nfsrv_sequential_heuristic(uiop, vp);
943 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
944 /* XXX KDM make this more systematic? */
945 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
946 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
953 nh->nh_nextoff = uiop->uio_offset;
954 tlen = len - uiop->uio_resid;
955 cnt = cnt < tlen ? cnt : tlen;
956 tlen = NFSM_RNDUP(cnt);
960 } else if (len != tlen || tlen != cnt)
961 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
971 * Create the iovec for the mbuf chain passed in as an argument.
972 * The "cp" argument is where the data starts within the first mbuf in
973 * the chain. It returns the iovec and the iovcnt.
976 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
984 * Loop through the mbuf chain, counting how many mbufs are a
985 * part of this write operation, so the iovec size is known.
990 i = mtod(mp, caddr_t) + mp->m_len - cp;
1004 /* Now, create the iovec. */
1006 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1009 i = mtod(mp, caddr_t) + mp->m_len - cp;
1013 panic("nfsrv_createiovecw");
1024 cp = mtod(mp, caddr_t);
1031 * Write vnode op from an mbuf list.
1034 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1035 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1038 int cnt, ioflags, error;
1039 struct uio io, *uiop = &io;
1043 * Attempt to write to a DS file. A return of ENOENT implies
1044 * there is no DS file to write.
1046 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1047 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1048 if (error != ENOENT) {
1049 *stable = NFSWRITE_FILESYNC;
1053 if (*stable == NFSWRITE_UNSTABLE)
1054 ioflags = IO_NODELOCKED;
1056 ioflags = (IO_SYNC | IO_NODELOCKED);
1057 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1061 uiop->uio_iovcnt = cnt;
1062 uiop->uio_resid = retlen;
1063 uiop->uio_rw = UIO_WRITE;
1064 uiop->uio_segflg = UIO_SYSSPACE;
1065 NFSUIOPROC(uiop, p);
1066 uiop->uio_offset = off;
1067 nh = nfsrv_sequential_heuristic(uiop, vp);
1068 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1069 /* XXX KDM make this more systematic? */
1070 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1071 error = VOP_WRITE(vp, uiop, ioflags, cred);
1073 nh->nh_nextoff = uiop->uio_offset;
1081 * Common code for creating a regular file (plus special files for V2).
1084 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1085 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1086 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1090 struct thread *p = curthread;
1092 error = nd->nd_repstat;
1093 if (!error && ndp->ni_vp == NULL) {
1094 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1095 vrele(ndp->ni_startdir);
1096 error = VOP_CREATE(ndp->ni_dvp,
1097 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1098 /* For a pNFS server, create the data file on a DS. */
1099 if (error == 0 && nvap->na_type == VREG) {
1101 * Create a data file on a DS for a pNFS server.
1102 * This function just returns if not
1103 * running a pNFS DS or the creation fails.
1105 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1109 nfsvno_relpathbuf(ndp);
1111 if (*exclusive_flagp) {
1112 *exclusive_flagp = 0;
1113 NFSVNO_ATTRINIT(nvap);
1114 nvap->na_atime.tv_sec = cverf[0];
1115 nvap->na_atime.tv_nsec = cverf[1];
1116 error = VOP_SETATTR(ndp->ni_vp,
1117 &nvap->na_vattr, nd->nd_cred);
1121 error = NFSERR_NOTSUPP;
1126 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1127 * (This implies, just get out on an error.)
1129 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1130 nvap->na_type == VFIFO) {
1131 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1132 nvap->na_type = VFIFO;
1133 if (nvap->na_type != VFIFO &&
1134 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1135 vrele(ndp->ni_startdir);
1136 nfsvno_relpathbuf(ndp);
1140 nvap->na_rdev = rdev;
1141 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1142 &ndp->ni_cnd, &nvap->na_vattr);
1144 nfsvno_relpathbuf(ndp);
1145 vrele(ndp->ni_startdir);
1149 vrele(ndp->ni_startdir);
1150 nfsvno_relpathbuf(ndp);
1158 * Handle cases where error is already set and/or
1160 * 1 - clean up the lookup
1161 * 2 - iff !error and na_size set, truncate it
1163 vrele(ndp->ni_startdir);
1164 nfsvno_relpathbuf(ndp);
1166 if (ndp->ni_dvp == *vpp)
1170 if (!error && nvap->na_size != VNOVAL) {
1171 error = nfsvno_accchk(*vpp, VWRITE,
1172 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1173 NFSACCCHK_VPISLOCKED, NULL);
1175 tempsize = nvap->na_size;
1176 NFSVNO_ATTRINIT(nvap);
1177 nvap->na_size = tempsize;
1178 error = VOP_SETATTR(*vpp,
1179 &nvap->na_vattr, nd->nd_cred);
1192 * Do a mknod vnode op.
1195 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1201 vtyp = nvap->na_type;
1203 * Iff doesn't exist, create it.
1206 vrele(ndp->ni_startdir);
1207 nfsvno_relpathbuf(ndp);
1213 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1214 vrele(ndp->ni_startdir);
1215 nfsvno_relpathbuf(ndp);
1217 error = NFSERR_BADTYPE;
1220 if (vtyp == VSOCK) {
1221 vrele(ndp->ni_startdir);
1222 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1223 &ndp->ni_cnd, &nvap->na_vattr);
1225 nfsvno_relpathbuf(ndp);
1227 if (nvap->na_type != VFIFO &&
1228 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1229 vrele(ndp->ni_startdir);
1230 nfsvno_relpathbuf(ndp);
1234 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1235 &ndp->ni_cnd, &nvap->na_vattr);
1237 nfsvno_relpathbuf(ndp);
1238 vrele(ndp->ni_startdir);
1240 * Since VOP_MKNOD returns the ni_vp, I can't
1241 * see any reason to do the lookup.
1254 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1255 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1259 if (ndp->ni_vp != NULL) {
1260 if (ndp->ni_dvp == ndp->ni_vp)
1265 nfsvno_relpathbuf(ndp);
1269 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1272 nfsvno_relpathbuf(ndp);
1283 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1284 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1285 struct nfsexstuff *exp)
1290 vrele(ndp->ni_startdir);
1291 nfsvno_relpathbuf(ndp);
1292 if (ndp->ni_dvp == ndp->ni_vp)
1301 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1302 &nvap->na_vattr, pathcp);
1304 vrele(ndp->ni_startdir);
1305 nfsvno_relpathbuf(ndp);
1307 * Although FreeBSD still had the lookup code in
1308 * it for 7/current, there doesn't seem to be any
1309 * point, since VOP_SYMLINK() returns the ni_vp.
1310 * Just vput it for v2.
1312 if (!not_v2 && !error)
1321 * Parse symbolic link arguments.
1322 * This function has an ugly side effect. It will malloc() an area for
1323 * the symlink and set iov_base to point to it, only if it succeeds.
1324 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1328 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1329 struct thread *p, char **pathcpp, int *lenp)
1332 char *pathcp = NULL;
1334 struct nfsv2_sattr *sp;
1338 if ((nd->nd_flag & ND_NFSV3) &&
1339 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1341 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1342 len = fxdr_unsigned(int, *tl);
1343 if (len > NFS_MAXPATHLEN || len <= 0) {
1347 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1348 error = nfsrv_mtostr(nd, pathcp, len);
1351 if (nd->nd_flag & ND_NFSV2) {
1352 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1353 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1357 NFSEXITCODE2(0, nd);
1361 free(pathcp, M_TEMP);
1362 NFSEXITCODE2(error, nd);
1367 * Remove a non-directory object.
1370 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1371 struct thread *p, struct nfsexstuff *exp)
1373 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1374 int error = 0, mirrorcnt;
1375 char fname[PNFS_FILENAME_LEN + 1];
1380 if (vp->v_type == VDIR)
1381 error = NFSERR_ISDIR;
1383 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1386 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1388 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1389 if (error == 0 && dsdvp[0] != NULL)
1390 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1391 if (ndp->ni_dvp == vp)
1396 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1397 nfsvno_relpathbuf(ndp);
1403 * Remove a directory.
1406 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1407 struct thread *p, struct nfsexstuff *exp)
1413 if (vp->v_type != VDIR) {
1418 * No rmdir "." please.
1420 if (ndp->ni_dvp == vp) {
1425 * The root of a mounted filesystem cannot be deleted.
1427 if (vp->v_vflag & VV_ROOT)
1431 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1432 if (ndp->ni_dvp == vp)
1437 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1438 nfsvno_relpathbuf(ndp);
1447 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1448 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1450 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1451 int error = 0, mirrorcnt;
1452 char fname[PNFS_FILENAME_LEN + 1];
1456 fvp = fromndp->ni_vp;
1458 vrele(fromndp->ni_dvp);
1463 tdvp = tondp->ni_dvp;
1466 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1467 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1469 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1470 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1473 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1474 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1479 * A rename to '.' or '..' results in a prematurely
1480 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1483 if ((tondp->ni_cnd.cn_namelen == 1 &&
1484 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1485 (tondp->ni_cnd.cn_namelen == 2 &&
1486 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1487 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1492 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1493 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1496 if (fvp->v_mount != tdvp->v_mount) {
1497 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1501 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1506 * If source and destination are the same, there is nothing to
1507 * do. Set error to -1 to indicate this.
1512 if (ndflag & ND_NFSV4) {
1513 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1514 error = nfsrv_checkremove(fvp, 0, NULL,
1515 (nfsquad_t)((u_quad_t)0), p);
1520 error = nfsrv_checkremove(tvp, 1, NULL,
1521 (nfsquad_t)((u_quad_t)0), p);
1524 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1525 * that the NFSv4 client won't be confused by the rename.
1526 * Since nfsd_recalldelegation() can only be called on an
1527 * unlocked vnode at this point and fvp is the file that will
1528 * still exist after the rename, just do fvp.
1530 nfsd_recalldelegation(fvp, p);
1532 if (error == 0 && tvp != NULL) {
1533 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1534 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1535 " dsdvp=%p\n", dsdvp[0]);
1539 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1540 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1549 vrele(fromndp->ni_dvp);
1556 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1557 * if the rename succeeded, the DS file for the tvp needs to be
1560 if (error == 0 && dsdvp[0] != NULL) {
1561 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1562 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1565 vrele(tondp->ni_startdir);
1566 nfsvno_relpathbuf(tondp);
1568 vrele(fromndp->ni_startdir);
1569 nfsvno_relpathbuf(fromndp);
1578 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1579 struct thread *p, struct nfsexstuff *exp)
1589 if (vp->v_mount != xp->v_mount)
1593 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1594 if (!VN_IS_DOOMED(vp))
1595 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1598 if (ndp->ni_dvp == vp)
1604 if (ndp->ni_dvp == ndp->ni_vp)
1611 nfsvno_relpathbuf(ndp);
1617 * Do the fsync() appropriate for the commit.
1620 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1626 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1627 * file is done. At this time VOP_FSYNC does not accept offset and
1628 * byte count parameters so call VOP_FSYNC the whole file for now.
1629 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1630 * File systems that do not use the buffer cache (as indicated
1631 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1633 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1634 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1636 * Give up and do the whole thing
1638 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1639 VM_OBJECT_WLOCK(vp->v_object);
1640 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1641 VM_OBJECT_WUNLOCK(vp->v_object);
1643 error = VOP_FSYNC(vp, MNT_WAIT, td);
1646 * Locate and synchronously write any buffers that fall
1647 * into the requested range. Note: we are assuming that
1648 * f_iosize is a power of 2.
1650 int iosize = vp->v_mount->mnt_stat.f_iosize;
1651 int iomask = iosize - 1;
1656 * Align to iosize boundary, super-align to page boundary.
1659 cnt += off & iomask;
1660 off &= ~(u_quad_t)iomask;
1662 if (off & PAGE_MASK) {
1663 cnt += off & PAGE_MASK;
1664 off &= ~(u_quad_t)PAGE_MASK;
1666 lblkno = off / iosize;
1668 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1669 VM_OBJECT_WLOCK(vp->v_object);
1670 vm_object_page_clean(vp->v_object, off, off + cnt,
1672 VM_OBJECT_WUNLOCK(vp->v_object);
1681 * If we have a buffer and it is marked B_DELWRI we
1682 * have to lock and write it. Otherwise the prior
1683 * write is assumed to have already been committed.
1685 * gbincore() can return invalid buffers now so we
1686 * have to check that bit as well (though B_DELWRI
1687 * should not be set if B_INVAL is set there could be
1688 * a race here since we haven't locked the buffer).
1690 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1691 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1692 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1694 continue; /* retry */
1696 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1699 bp->b_flags &= ~B_ASYNC;
1722 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1728 if (nfsrv_devidcnt > 0) {
1729 /* For a pNFS service, get the DS numbers. */
1730 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1731 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1737 error = VFS_STATFS(vp->v_mount, sf);
1740 sf->f_blocks = tsf->f_blocks;
1741 sf->f_bavail = tsf->f_bavail;
1742 sf->f_bfree = tsf->f_bfree;
1743 sf->f_bsize = tsf->f_bsize;
1746 * Since NFS handles these values as unsigned on the
1747 * wire, there is no way to represent negative values,
1748 * so set them to 0. Without this, they will appear
1749 * to be very large positive values for clients like
1752 if (sf->f_bavail < 0)
1754 if (sf->f_ffree < 0)
1763 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1764 * must handle nfsrv_opencheck() calls after any other access checks.
1767 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1768 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1769 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1770 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred,
1771 struct nfsexstuff *exp, struct vnode **vpp)
1773 struct vnode *vp = NULL;
1775 struct nfsexstuff nes;
1776 struct thread *p = curthread;
1778 if (ndp->ni_vp == NULL)
1779 nd->nd_repstat = nfsrv_opencheck(clientid,
1780 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1781 if (!nd->nd_repstat) {
1782 if (ndp->ni_vp == NULL) {
1783 vrele(ndp->ni_startdir);
1784 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1785 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1786 /* For a pNFS server, create the data file on a DS. */
1787 if (nd->nd_repstat == 0) {
1789 * Create a data file on a DS for a pNFS server.
1790 * This function just returns if not
1791 * running a pNFS DS or the creation fails.
1793 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1797 nfsvno_relpathbuf(ndp);
1798 if (!nd->nd_repstat) {
1799 if (*exclusive_flagp) {
1800 *exclusive_flagp = 0;
1801 NFSVNO_ATTRINIT(nvap);
1802 nvap->na_atime.tv_sec = cverf[0];
1803 nvap->na_atime.tv_nsec = cverf[1];
1804 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1805 &nvap->na_vattr, cred);
1806 if (nd->nd_repstat != 0) {
1809 nd->nd_repstat = NFSERR_NOTSUPP;
1811 NFSSETBIT_ATTRBIT(attrbitp,
1812 NFSATTRBIT_TIMEACCESS);
1814 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1815 aclp, p, attrbitp, exp);
1820 if (ndp->ni_startdir)
1821 vrele(ndp->ni_startdir);
1822 nfsvno_relpathbuf(ndp);
1824 if (create == NFSV4OPEN_CREATE) {
1825 if (ndp->ni_dvp == vp)
1830 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1831 if (ndp->ni_cnd.cn_flags & RDONLY)
1832 NFSVNO_SETEXRDONLY(&nes);
1834 NFSVNO_EXINIT(&nes);
1835 nd->nd_repstat = nfsvno_accchk(vp,
1836 VWRITE, cred, &nes, p,
1837 NFSACCCHK_NOOVERRIDE,
1838 NFSACCCHK_VPISLOCKED, NULL);
1839 nd->nd_repstat = nfsrv_opencheck(clientid,
1840 stateidp, stp, vp, nd, p, nd->nd_repstat);
1841 if (!nd->nd_repstat) {
1842 tempsize = nvap->na_size;
1843 NFSVNO_ATTRINIT(nvap);
1844 nvap->na_size = tempsize;
1845 nd->nd_repstat = VOP_SETATTR(vp,
1846 &nvap->na_vattr, cred);
1848 } else if (vp->v_type == VREG) {
1849 nd->nd_repstat = nfsrv_opencheck(clientid,
1850 stateidp, stp, vp, nd, p, nd->nd_repstat);
1854 if (ndp->ni_cnd.cn_flags & HASBUF)
1855 nfsvno_relpathbuf(ndp);
1856 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1857 vrele(ndp->ni_startdir);
1858 if (ndp->ni_dvp == ndp->ni_vp)
1868 NFSEXITCODE2(0, nd);
1872 * Updates the file rev and sets the mtime and ctime
1873 * to the current clock time, returning the va_filerev and va_Xtime
1875 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1878 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1879 struct nfsrv_descript *nd, struct thread *p)
1884 vfs_timestamp(&va.va_mtime);
1885 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1886 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1887 if (VN_IS_DOOMED(vp))
1890 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1891 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1896 * Glue routine to nfsv4_fillattr().
1899 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1900 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1901 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1902 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1908 if (nfsrv_devidcnt > 0 &&
1909 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1910 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1911 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1912 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1913 error = nfsrv_pnfsstatfs(sf, mp);
1919 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1920 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1921 mounted_on_fileno, sf);
1923 NFSEXITCODE2(0, nd);
1927 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1929 * nfs readdir service
1930 * - mallocs what it thinks is enough to read
1931 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1932 * - calls VOP_READDIR()
1933 * - loops around building the reply
1934 * if the output generated exceeds count break out of loop
1935 * The NFSM_CLGET macro is used here so that the reply will be packed
1936 * tightly in mbuf clusters.
1937 * - it trims out records with d_fileno == 0
1938 * this doesn't matter for Unix clients, but they might confuse clients
1940 * - it trims out records with d_type == DT_WHT
1941 * these cannot be seen through NFS (unless we extend the protocol)
1942 * The alternate call nfsrvd_readdirplus() does lookups as well.
1943 * PS: The NFS protocol spec. does not clarify what the "count" byte
1944 * argument is a count of.. just name strings and file id's or the
1945 * entire reply rpc or ...
1946 * I tried just file name and id sizes and it confused the Sun client,
1947 * so I am using the full rpc size now. The "paranoia.." comment refers
1948 * to including the status longwords that are not a part of the dir.
1949 * "entry" structures, but are in the rpc.
1952 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1953 struct vnode *vp, struct nfsexstuff *exp)
1958 char *cpos, *cend, *rbuf;
1960 int nlen, error = 0, getret = 1;
1961 int siz, cnt, fullsiz, eofflag, ncookies;
1962 u_int64_t off, toff, verf __unused;
1963 u_long *cookies = NULL, *cookiep;
1967 struct thread *p = curthread;
1969 if (nd->nd_repstat) {
1970 nfsrv_postopattr(nd, getret, &at);
1973 if (nd->nd_flag & ND_NFSV2) {
1974 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1975 off = fxdr_unsigned(u_quad_t, *tl++);
1977 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1978 off = fxdr_hyper(tl);
1980 verf = fxdr_hyper(tl);
1984 cnt = fxdr_unsigned(int, *tl);
1985 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1986 cnt = NFS_SRVMAXDATA(nd);
1987 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1989 if (nd->nd_flag & ND_NFSV3) {
1990 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
1994 * va_filerev is not sufficient as a cookie verifier,
1995 * since it is not supposed to change when entries are
1996 * removed/added unless that offset cookies returned to
1997 * the client are no longer valid.
1999 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2000 nd->nd_repstat = NFSERR_BAD_COOKIE;
2003 if (!nd->nd_repstat && vp->v_type != VDIR)
2004 nd->nd_repstat = NFSERR_NOTDIR;
2005 if (nd->nd_repstat == 0 && cnt == 0) {
2006 if (nd->nd_flag & ND_NFSV2)
2007 /* NFSv2 does not have NFSERR_TOOSMALL */
2008 nd->nd_repstat = EPERM;
2010 nd->nd_repstat = NFSERR_TOOSMALL;
2012 if (!nd->nd_repstat)
2013 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2014 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2015 NFSACCCHK_VPISLOCKED, NULL);
2016 if (nd->nd_repstat) {
2018 if (nd->nd_flag & ND_NFSV3)
2019 nfsrv_postopattr(nd, getret, &at);
2022 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2023 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2027 free(cookies, M_TEMP);
2035 io.uio_offset = (off_t)off;
2037 io.uio_segflg = UIO_SYSSPACE;
2038 io.uio_rw = UIO_READ;
2040 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2042 off = (u_int64_t)io.uio_offset;
2044 siz -= io.uio_resid;
2046 if (!cookies && !nd->nd_repstat)
2047 nd->nd_repstat = NFSERR_PERM;
2048 if (nd->nd_flag & ND_NFSV3) {
2049 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2050 if (!nd->nd_repstat)
2051 nd->nd_repstat = getret;
2055 * Handles the failed cases. nd->nd_repstat == 0 past here.
2057 if (nd->nd_repstat) {
2061 free(cookies, M_TEMP);
2062 if (nd->nd_flag & ND_NFSV3)
2063 nfsrv_postopattr(nd, getret, &at);
2067 * If nothing read, return eof
2072 if (nd->nd_flag & ND_NFSV2) {
2073 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2075 nfsrv_postopattr(nd, getret, &at);
2076 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2077 txdr_hyper(at.na_filerev, tl);
2080 *tl++ = newnfs_false;
2083 free(cookies, M_TEMP);
2088 * Check for degenerate cases of nothing useful read.
2089 * If so go try again
2093 dp = (struct dirent *)cpos;
2097 * For some reason FreeBSD's ufs_readdir() chooses to back the
2098 * directory offset up to a block boundary, so it is necessary to
2099 * skip over the records that precede the requested offset. This
2100 * requires the assumption that file offset cookies monotonically
2103 while (cpos < cend && ncookies > 0 &&
2104 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2105 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2106 cpos += dp->d_reclen;
2107 dp = (struct dirent *)cpos;
2111 if (cpos >= cend || ncookies == 0) {
2119 * If cnt > MCLBYTES and the reply will not be saved, use
2120 * ext_pgs mbufs for TLS.
2121 * For NFSv4.0, we do not know for sure if the reply will
2122 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2124 if (cnt > MCLBYTES && siz > MCLBYTES &&
2125 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2126 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2127 nd->nd_flag |= ND_EXTPG;
2130 * dirlen is the size of the reply, including all XDR and must
2131 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2132 * if the XDR should be included in "count", but to be safe, we do.
2133 * (Include the two booleans at the end of the reply in dirlen now.)
2135 if (nd->nd_flag & ND_NFSV3) {
2136 nfsrv_postopattr(nd, getret, &at);
2137 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2138 txdr_hyper(at.na_filerev, tl);
2139 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2141 dirlen = 2 * NFSX_UNSIGNED;
2144 /* Loop through the records and build reply */
2145 while (cpos < cend && ncookies > 0) {
2146 nlen = dp->d_namlen;
2147 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2148 nlen <= NFS_MAXNAMLEN) {
2149 if (nd->nd_flag & ND_NFSV3)
2150 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2152 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2159 * Build the directory record xdr from
2162 if (nd->nd_flag & ND_NFSV3) {
2163 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2164 *tl++ = newnfs_true;
2167 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2168 *tl++ = newnfs_true;
2170 *tl = txdr_unsigned(dp->d_fileno);
2171 (void) nfsm_strtom(nd, dp->d_name, nlen);
2172 if (nd->nd_flag & ND_NFSV3) {
2173 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2176 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2177 *tl = txdr_unsigned(*cookiep);
2179 cpos += dp->d_reclen;
2180 dp = (struct dirent *)cpos;
2186 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2187 *tl++ = newnfs_false;
2193 free(cookies, M_TEMP);
2196 NFSEXITCODE2(0, nd);
2200 NFSEXITCODE2(error, nd);
2205 * Readdirplus for V3 and Readdir for V4.
2208 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2209 struct vnode *vp, struct nfsexstuff *exp)
2214 char *cpos, *cend, *rbuf;
2217 struct nfsvattr nva, at, *nvap = &nva;
2218 struct mbuf *mb0, *mb1;
2219 struct nfsreferral *refp;
2220 int nlen, r, error = 0, getret = 1, usevget = 1;
2221 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2222 caddr_t bpos0, bpos1;
2223 u_int64_t off, toff, verf;
2224 u_long *cookies = NULL, *cookiep;
2225 nfsattrbit_t attrbits, rderrbits, savbits;
2228 struct componentname cn;
2229 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2230 struct mount *mp, *new_mp;
2231 uint64_t mounted_on_fileno;
2232 struct thread *p = curthread;
2233 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2235 if (nd->nd_repstat) {
2236 nfsrv_postopattr(nd, getret, &at);
2239 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2240 off = fxdr_hyper(tl);
2243 verf = fxdr_hyper(tl);
2245 siz = fxdr_unsigned(int, *tl++);
2246 cnt = fxdr_unsigned(int, *tl);
2249 * Use the server's maximum data transfer size as the upper bound
2252 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2253 cnt = NFS_SRVMAXDATA(nd);
2256 * siz is a "hint" of how much directory information (name, fileid,
2257 * cookie) should be in the reply. At least one client "hints" 0,
2258 * so I set it to cnt for that case. I also round it up to the
2259 * next multiple of DIRBLKSIZ.
2260 * Since the size of a Readdirplus directory entry reply will always
2261 * be greater than a directory entry returned by VOP_READDIR(), it
2262 * does not make sense to read more than NFS_SRVMAXDATA() via
2267 else if (siz > NFS_SRVMAXDATA(nd))
2268 siz = NFS_SRVMAXDATA(nd);
2269 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2271 if (nd->nd_flag & ND_NFSV4) {
2272 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2275 NFSSET_ATTRBIT(&savbits, &attrbits);
2276 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2277 NFSZERO_ATTRBIT(&rderrbits);
2278 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2280 NFSZERO_ATTRBIT(&attrbits);
2283 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2285 if (!nd->nd_repstat) {
2286 if (off && verf != at.na_filerev) {
2288 * va_filerev is not sufficient as a cookie verifier,
2289 * since it is not supposed to change when entries are
2290 * removed/added unless that offset cookies returned to
2291 * the client are no longer valid.
2293 if (nd->nd_flag & ND_NFSV4) {
2294 nd->nd_repstat = NFSERR_NOTSAME;
2296 nd->nd_repstat = NFSERR_BAD_COOKIE;
2301 if (!nd->nd_repstat && vp->v_type != VDIR)
2302 nd->nd_repstat = NFSERR_NOTDIR;
2303 if (!nd->nd_repstat && cnt == 0)
2304 nd->nd_repstat = NFSERR_TOOSMALL;
2305 if (!nd->nd_repstat)
2306 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2307 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2308 NFSACCCHK_VPISLOCKED, NULL);
2309 if (nd->nd_repstat) {
2311 if (nd->nd_flag & ND_NFSV3)
2312 nfsrv_postopattr(nd, getret, &at);
2315 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2316 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2318 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2322 free(cookies, M_TEMP);
2330 io.uio_offset = (off_t)off;
2332 io.uio_segflg = UIO_SYSSPACE;
2333 io.uio_rw = UIO_READ;
2335 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2337 off = (u_int64_t)io.uio_offset;
2339 siz -= io.uio_resid;
2341 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2343 if (!cookies && !nd->nd_repstat)
2344 nd->nd_repstat = NFSERR_PERM;
2345 if (!nd->nd_repstat)
2346 nd->nd_repstat = getret;
2347 if (nd->nd_repstat) {
2350 free(cookies, M_TEMP);
2352 if (nd->nd_flag & ND_NFSV3)
2353 nfsrv_postopattr(nd, getret, &at);
2357 * If nothing read, return eof
2362 if (nd->nd_flag & ND_NFSV3)
2363 nfsrv_postopattr(nd, getret, &at);
2364 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2365 txdr_hyper(at.na_filerev, tl);
2367 *tl++ = newnfs_false;
2369 free(cookies, M_TEMP);
2375 * Check for degenerate cases of nothing useful read.
2376 * If so go try again
2380 dp = (struct dirent *)cpos;
2384 * For some reason FreeBSD's ufs_readdir() chooses to back the
2385 * directory offset up to a block boundary, so it is necessary to
2386 * skip over the records that precede the requested offset. This
2387 * requires the assumption that file offset cookies monotonically
2390 while (cpos < cend && ncookies > 0 &&
2391 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2392 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2393 ((nd->nd_flag & ND_NFSV4) &&
2394 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2395 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2396 cpos += dp->d_reclen;
2397 dp = (struct dirent *)cpos;
2401 if (cpos >= cend || ncookies == 0) {
2408 * Busy the file system so that the mount point won't go away
2409 * and, as such, VFS_VGET() can be used safely.
2414 nd->nd_repstat = vfs_busy(mp, 0);
2416 if (nd->nd_repstat != 0) {
2418 free(cookies, M_TEMP);
2420 if (nd->nd_flag & ND_NFSV3)
2421 nfsrv_postopattr(nd, getret, &at);
2426 * Check to see if entries in this directory can be safely acquired
2427 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2428 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2429 * automount of the snapshot directory that is required will
2431 * This needs to be done here for NFSv4, since NFSv4 never does
2432 * a VFS_VGET() for "." or "..".
2435 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2436 if (r == EOPNOTSUPP) {
2438 cn.cn_nameiop = LOOKUP;
2439 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2440 cn.cn_cred = nd->nd_cred;
2447 * If the reply is likely to exceed MCLBYTES and the reply will
2448 * not be saved, use ext_pgs mbufs for TLS.
2449 * It is difficult to predict how large each entry will be and
2450 * how many entries have been read, so just assume the directory
2451 * entries grow by a factor of 4 when attributes are included.
2452 * For NFSv4.0, we do not know for sure if the reply will
2453 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2455 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2456 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2457 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2458 nd->nd_flag |= ND_EXTPG;
2461 * Save this position, in case there is an error before one entry
2465 bpos0 = nd->nd_bpos;
2466 bextpg0 = nd->nd_bextpg;
2467 bextpgsiz0 = nd->nd_bextpgsiz;
2470 * Fill in the first part of the reply.
2471 * dirlen is the reply length in bytes and cannot exceed cnt.
2472 * (Include the two booleans at the end of the reply in dirlen now,
2473 * so we recognize when we have exceeded cnt.)
2475 if (nd->nd_flag & ND_NFSV3) {
2476 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2477 nfsrv_postopattr(nd, getret, &at);
2479 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2481 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2482 txdr_hyper(at.na_filerev, tl);
2485 * Save this position, in case there is an empty reply needed.
2488 bpos1 = nd->nd_bpos;
2489 bextpg1 = nd->nd_bextpg;
2490 bextpgsiz1 = nd->nd_bextpgsiz;
2492 /* Loop through the records and build reply */
2494 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2495 nlen = dp->d_namlen;
2496 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2497 nlen <= NFS_MAXNAMLEN &&
2498 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2499 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2500 || (nlen == 1 && dp->d_name[0] != '.'))) {
2502 * Save the current position in the reply, in case
2503 * this entry exceeds cnt.
2506 bpos1 = nd->nd_bpos;
2507 bextpg1 = nd->nd_bextpg;
2508 bextpgsiz1 = nd->nd_bextpgsiz;
2511 * For readdir_and_lookup get the vnode using
2520 mounted_on_fileno = (uint64_t)dp->d_fileno;
2521 if ((nd->nd_flag & ND_NFSV3) ||
2522 NFSNONZERO_ATTRBIT(&savbits)) {
2523 if (nd->nd_flag & ND_NFSV4)
2524 refp = nfsv4root_getreferral(NULL,
2528 r = VFS_VGET(mp, dp->d_fileno,
2532 if (r == EOPNOTSUPP) {
2535 cn.cn_nameiop = LOOKUP;
2543 cn.cn_nameptr = dp->d_name;
2544 cn.cn_namelen = nlen;
2545 cn.cn_flags = ISLASTCN |
2546 NOFOLLOW | LOCKLEAF;
2548 dp->d_name[0] == '.' &&
2549 dp->d_name[1] == '.')
2552 if (NFSVOPLOCK(vp, LK_SHARED)
2554 nd->nd_repstat = EPERM;
2557 if ((vp->v_vflag & VV_ROOT) != 0
2558 && (cn.cn_flags & ISDOTDOT)
2564 r = VOP_LOOKUP(vp, &nvp,
2572 * For NFSv4, check to see if nvp is
2573 * a mount point and get the mount
2574 * point vnode, as required.
2577 nfsrv_enable_crossmntpt != 0 &&
2578 (nd->nd_flag & ND_NFSV4) != 0 &&
2579 nvp->v_type == VDIR &&
2580 nvp->v_mountedhere != NULL) {
2581 new_mp = nvp->v_mountedhere;
2582 r = vfs_busy(new_mp, 0);
2586 r = VFS_ROOT(new_mp,
2596 * If we failed to look up the entry, then it
2597 * has become invalid, most likely removed.
2604 KASSERT(refp != NULL || nvp != NULL,
2605 ("%s: undetected lookup error", __func__));
2608 ((nd->nd_flag & ND_NFSV3) ||
2609 NFSNONZERO_ATTRBIT(&attrbits))) {
2610 r = nfsvno_getfh(nvp, &nfh, p);
2612 r = nfsvno_getattr(nvp, nvap, nd, p,
2614 if (r == 0 && is_zfs == 1 &&
2615 nfsrv_enable_crossmntpt != 0 &&
2616 (nd->nd_flag & ND_NFSV4) != 0 &&
2617 nvp->v_type == VDIR &&
2618 vp->v_mount != nvp->v_mount) {
2620 * For a ZFS snapshot, there is a
2621 * pseudo mount that does not set
2622 * v_mountedhere, so it needs to
2623 * be detected via a different
2628 new_mp = nvp->v_mount;
2633 * If we failed to get attributes of the entry,
2634 * then just skip it for NFSv3 (the traditional
2635 * behavior in the old NFS server).
2636 * For NFSv4 the behavior is controlled by
2637 * RDATTRERROR: we either ignore the error or
2639 * Note that RDATTRERROR is never set for NFSv3.
2642 if (!NFSISSET_ATTRBIT(&attrbits,
2643 NFSATTRBIT_RDATTRERROR)) {
2645 if (needs_unbusy != 0)
2647 if ((nd->nd_flag & ND_NFSV3))
2656 * Build the directory record xdr
2658 if (nd->nd_flag & ND_NFSV3) {
2659 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2660 *tl++ = newnfs_true;
2662 *tl = txdr_unsigned(dp->d_fileno);
2663 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2664 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2666 *tl = txdr_unsigned(*cookiep);
2667 nfsrv_postopattr(nd, 0, nvap);
2668 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2669 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2673 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2674 *tl++ = newnfs_true;
2676 *tl = txdr_unsigned(*cookiep);
2677 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2679 supports_nfsv4acls =
2680 nfs_supportsnfsv4acls(nvp);
2683 supports_nfsv4acls = 0;
2685 dirlen += nfsrv_putreferralattr(nd,
2688 if (nd->nd_repstat) {
2691 if (needs_unbusy != 0)
2696 dirlen += nfsvno_fillattr(nd, new_mp,
2697 nvp, nvap, &nfh, r, &rderrbits,
2698 nd->nd_cred, p, isdgram, 0,
2699 supports_nfsv4acls, at_root,
2702 dirlen += nfsvno_fillattr(nd, new_mp,
2703 nvp, nvap, &nfh, r, &attrbits,
2704 nd->nd_cred, p, isdgram, 0,
2705 supports_nfsv4acls, at_root,
2710 dirlen += (3 * NFSX_UNSIGNED);
2712 if (needs_unbusy != 0)
2718 cpos += dp->d_reclen;
2719 dp = (struct dirent *)cpos;
2727 * If dirlen > cnt, we must strip off the last entry. If that
2728 * results in an empty reply, report NFSERR_TOOSMALL.
2730 if (dirlen > cnt || nd->nd_repstat) {
2731 if (!nd->nd_repstat && entrycnt == 0)
2732 nd->nd_repstat = NFSERR_TOOSMALL;
2733 if (nd->nd_repstat) {
2734 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2735 if (nd->nd_flag & ND_NFSV3)
2736 nfsrv_postopattr(nd, getret, &at);
2738 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2740 } else if (cpos < cend)
2742 if (!nd->nd_repstat) {
2743 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2744 *tl++ = newnfs_false;
2750 free(cookies, M_TEMP);
2754 NFSEXITCODE2(0, nd);
2758 NFSEXITCODE2(error, nd);
2763 * Get the settable attributes out of the mbuf list.
2764 * (Return 0 or EBADRPC)
2767 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2768 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2771 struct nfsv2_sattr *sp;
2772 int error = 0, toclient = 0;
2774 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2776 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2778 * Some old clients didn't fill in the high order 16bits.
2779 * --> check the low order 2 bytes for 0xffff
2781 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2782 nvap->na_mode = nfstov_mode(sp->sa_mode);
2783 if (sp->sa_uid != newnfs_xdrneg1)
2784 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2785 if (sp->sa_gid != newnfs_xdrneg1)
2786 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2787 if (sp->sa_size != newnfs_xdrneg1)
2788 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2789 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2791 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2793 nvap->na_atime.tv_sec =
2794 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2795 nvap->na_atime.tv_nsec = 0;
2798 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2799 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2802 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2803 if (*tl == newnfs_true) {
2804 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2805 nvap->na_mode = nfstov_mode(*tl);
2807 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2808 if (*tl == newnfs_true) {
2809 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2810 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2812 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2813 if (*tl == newnfs_true) {
2814 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2815 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2817 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2818 if (*tl == newnfs_true) {
2819 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2820 nvap->na_size = fxdr_hyper(tl);
2822 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2823 switch (fxdr_unsigned(int, *tl)) {
2824 case NFSV3SATTRTIME_TOCLIENT:
2825 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2826 fxdr_nfsv3time(tl, &nvap->na_atime);
2829 case NFSV3SATTRTIME_TOSERVER:
2830 vfs_timestamp(&nvap->na_atime);
2831 nvap->na_vaflags |= VA_UTIMES_NULL;
2834 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2835 switch (fxdr_unsigned(int, *tl)) {
2836 case NFSV3SATTRTIME_TOCLIENT:
2837 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2838 fxdr_nfsv3time(tl, &nvap->na_mtime);
2839 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2841 case NFSV3SATTRTIME_TOSERVER:
2842 vfs_timestamp(&nvap->na_mtime);
2844 nvap->na_vaflags |= VA_UTIMES_NULL;
2849 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2852 NFSEXITCODE2(error, nd);
2857 * Handle the setable attributes for V4.
2858 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2861 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2862 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2867 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2868 int moderet, toclient = 0;
2869 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2872 u_short mode, mask; /* Same type as va_mode. */
2875 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2878 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2879 attrsize = fxdr_unsigned(int, *tl);
2882 * Loop around getting the setable attributes. If an unsupported
2883 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2886 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2887 bitpos = NFSATTRBIT_MAX;
2892 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2893 if (attrsum > attrsize) {
2894 error = NFSERR_BADXDR;
2897 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2899 case NFSATTRBIT_SIZE:
2900 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2901 if (vp != NULL && vp->v_type != VREG) {
2902 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2906 nvap->na_size = fxdr_hyper(tl);
2907 attrsum += NFSX_HYPER;
2909 case NFSATTRBIT_ACL:
2910 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2914 if (aceerr && !nd->nd_repstat)
2915 nd->nd_repstat = aceerr;
2918 case NFSATTRBIT_ARCHIVE:
2919 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2920 if (!nd->nd_repstat)
2921 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2922 attrsum += NFSX_UNSIGNED;
2924 case NFSATTRBIT_HIDDEN:
2925 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2926 if (!nd->nd_repstat)
2927 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2928 attrsum += NFSX_UNSIGNED;
2930 case NFSATTRBIT_MIMETYPE:
2931 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2932 i = fxdr_unsigned(int, *tl);
2933 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2936 if (!nd->nd_repstat)
2937 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2938 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2940 case NFSATTRBIT_MODE:
2941 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
2942 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2943 nvap->na_mode = nfstov_mode(*tl);
2944 attrsum += NFSX_UNSIGNED;
2946 case NFSATTRBIT_OWNER:
2947 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2948 j = fxdr_unsigned(int, *tl);
2950 error = NFSERR_BADXDR;
2953 if (j > NFSV4_SMALLSTR)
2954 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2957 error = nfsrv_mtostr(nd, cp, j);
2959 if (j > NFSV4_SMALLSTR)
2960 free(cp, M_NFSSTRING);
2963 if (!nd->nd_repstat) {
2964 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
2966 if (!nd->nd_repstat)
2969 if (j > NFSV4_SMALLSTR)
2970 free(cp, M_NFSSTRING);
2971 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2973 case NFSATTRBIT_OWNERGROUP:
2974 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2975 j = fxdr_unsigned(int, *tl);
2977 error = NFSERR_BADXDR;
2980 if (j > NFSV4_SMALLSTR)
2981 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2984 error = nfsrv_mtostr(nd, cp, j);
2986 if (j > NFSV4_SMALLSTR)
2987 free(cp, M_NFSSTRING);
2990 if (!nd->nd_repstat) {
2991 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
2993 if (!nd->nd_repstat)
2996 if (j > NFSV4_SMALLSTR)
2997 free(cp, M_NFSSTRING);
2998 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3000 case NFSATTRBIT_SYSTEM:
3001 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3002 if (!nd->nd_repstat)
3003 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3004 attrsum += NFSX_UNSIGNED;
3006 case NFSATTRBIT_TIMEACCESSSET:
3007 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3008 attrsum += NFSX_UNSIGNED;
3009 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3010 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3011 fxdr_nfsv4time(tl, &nvap->na_atime);
3013 attrsum += NFSX_V4TIME;
3015 vfs_timestamp(&nvap->na_atime);
3016 nvap->na_vaflags |= VA_UTIMES_NULL;
3019 case NFSATTRBIT_TIMEBACKUP:
3020 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3021 if (!nd->nd_repstat)
3022 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3023 attrsum += NFSX_V4TIME;
3025 case NFSATTRBIT_TIMECREATE:
3026 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3027 fxdr_nfsv4time(tl, &nvap->na_btime);
3028 attrsum += NFSX_V4TIME;
3030 case NFSATTRBIT_TIMEMODIFYSET:
3031 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3032 attrsum += NFSX_UNSIGNED;
3033 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3034 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3035 fxdr_nfsv4time(tl, &nvap->na_mtime);
3036 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3037 attrsum += NFSX_V4TIME;
3039 vfs_timestamp(&nvap->na_mtime);
3041 nvap->na_vaflags |= VA_UTIMES_NULL;
3044 case NFSATTRBIT_MODESETMASKED:
3045 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3046 mode = fxdr_unsigned(u_short, *tl++);
3047 mask = fxdr_unsigned(u_short, *tl);
3049 * vp == NULL implies an Open/Create operation.
3050 * This attribute can only be used for Setattr and
3051 * only for NFSv4.1 or higher.
3052 * If moderet != 0, a mode attribute has also been
3053 * specified and this attribute cannot be done in the
3054 * same Setattr operation.
3056 if ((nd->nd_flag & ND_NFSV41) == 0)
3057 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3058 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3060 nd->nd_repstat = NFSERR_INVAL;
3061 else if (moderet == 0)
3062 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3064 nvap->na_mode = (mode & mask) |
3065 (va.va_mode & ~mask);
3067 nd->nd_repstat = moderet;
3068 attrsum += 2 * NFSX_UNSIGNED;
3071 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3073 * set bitpos so we drop out of the loop.
3075 bitpos = NFSATTRBIT_MAX;
3081 * some clients pad the attrlist, so we need to skip over the
3084 if (attrsum > attrsize) {
3085 error = NFSERR_BADXDR;
3087 attrsize = NFSM_RNDUP(attrsize);
3088 if (attrsum < attrsize)
3089 error = nfsm_advance(nd, attrsize - attrsum, -1);
3092 NFSEXITCODE2(error, nd);
3097 * Check/setup export credentials.
3100 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3101 struct ucred *credanon)
3106 * Check/setup credentials.
3108 if (nd->nd_flag & ND_GSS)
3109 exp->nes_exflag &= ~MNT_EXPORTANON;
3112 * Check to see if the operation is allowed for this security flavor.
3113 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
3114 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
3115 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
3117 if (nfsvno_testexp(nd, exp) &&
3118 nd->nd_procnum != NFSV4OP_SECINFO &&
3119 nd->nd_procnum != NFSPROC_FSINFO) {
3120 if (nd->nd_flag & ND_NFSV4)
3121 error = NFSERR_WRONGSEC;
3123 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3128 * Check to see if the file system is exported V4 only.
3130 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3131 error = NFSERR_PROGNOTV4;
3136 * Now, map the user credentials.
3137 * (Note that ND_AUTHNONE will only be set for an NFSv3
3138 * Fsinfo RPC. If set for anything else, this code might need
3141 if (NFSVNO_EXPORTED(exp)) {
3142 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3143 NFSVNO_EXPORTANON(exp) ||
3144 (nd->nd_flag & ND_AUTHNONE) != 0) {
3145 nd->nd_cred->cr_uid = credanon->cr_uid;
3146 nd->nd_cred->cr_gid = credanon->cr_gid;
3147 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3148 credanon->cr_groups);
3149 } else if ((nd->nd_flag & ND_GSS) == 0) {
3151 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3152 * if there is a replacement credential with a group
3153 * list set up by "nfsuserd -manage-gids".
3154 * If there is no replacement, nfsrv_getgrpscred()
3155 * simply returns its argument.
3157 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3162 NFSEXITCODE2(error, nd);
3170 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3171 struct ucred **credp)
3175 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3176 &exp->nes_numsecflavor, exp->nes_secflavors);
3178 if (nfs_rootfhset) {
3179 exp->nes_exflag = 0;
3180 exp->nes_numsecflavor = 0;
3183 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3185 printf("nfsvno_checkexp: numsecflavors out of range\n");
3186 exp->nes_numsecflavor = 0;
3194 * Get a vnode for a file handle and export stuff.
3197 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3198 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3199 struct ucred **credp)
3204 exp->nes_numsecflavor = 0;
3205 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3207 /* Make sure the server replies ESTALE to the client. */
3209 if (nam && !error) {
3210 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3211 &exp->nes_numsecflavor, exp->nes_secflavors);
3213 if (nfs_rootfhset) {
3214 exp->nes_exflag = 0;
3215 exp->nes_numsecflavor = 0;
3220 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3222 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3223 exp->nes_numsecflavor = 0;
3233 * nfsd_fhtovp() - convert a fh to a vnode ptr
3234 * - look up fsid in mount list (if not found ret error)
3235 * - get vp and export rights by calling nfsvno_fhtovp()
3236 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3238 * - if mpp != NULL, return the mount point so that it can
3239 * be used for vn_finished_write() by the caller
3242 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3243 struct vnode **vpp, struct nfsexstuff *exp,
3244 struct mount **mpp, int startwrite)
3247 struct ucred *credanon;
3250 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3252 * Check for the special case of the nfsv4root_fh.
3254 mp = vfs_busyfs(&fhp->fh_fsid);
3259 nd->nd_repstat = ESTALE;
3264 vn_start_write(NULL, mpp, V_WAIT);
3265 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3266 lktype = LK_EXCLUSIVE;
3268 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3273 * For NFSv4 without a pseudo root fs, unexported file handles
3274 * can be returned, so that Lookup works everywhere.
3276 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3277 !(nd->nd_flag & ND_NFSV4)) {
3279 nd->nd_repstat = EACCES;
3283 * If TLS is required by the export, check the flags in nd_flag.
3285 if (nd->nd_repstat == 0 && ((NFSVNO_EXTLS(exp) &&
3286 (nd->nd_flag & ND_TLS) == 0) ||
3287 (NFSVNO_EXTLSCERT(exp) &&
3288 (nd->nd_flag & ND_TLSCERT) == 0) ||
3289 (NFSVNO_EXTLSCERTUSER(exp) &&
3290 (nd->nd_flag & ND_TLSCERTUSER) == 0))) {
3292 nd->nd_repstat = NFSERR_ACCES;
3296 * Personally, I've never seen any point in requiring a
3297 * reserved port#, since only in the rare case where the
3298 * clients are all boxes with secure system privileges,
3299 * does it provide any enhanced security, but... some people
3300 * believe it to be useful and keep putting this code back in.
3301 * (There is also some "security checker" out there that
3302 * complains if the nfs server doesn't enforce this.)
3303 * However, note the following:
3304 * RFC3530 (NFSv4) specifies that a reserved port# not be
3306 * RFC2623 recommends that, if a reserved port# is checked for,
3307 * that there be a way to turn that off--> ifdef'd.
3309 #ifdef NFS_REQRSVPORT
3310 if (!nd->nd_repstat) {
3311 struct sockaddr_in *saddr;
3312 struct sockaddr_in6 *saddr6;
3314 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3315 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3316 if (!(nd->nd_flag & ND_NFSV4) &&
3317 ((saddr->sin_family == AF_INET &&
3318 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3319 (saddr6->sin6_family == AF_INET6 &&
3320 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3322 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3325 #endif /* NFS_REQRSVPORT */
3328 * Check/setup credentials.
3330 if (!nd->nd_repstat) {
3331 nd->nd_saveduid = nd->nd_cred->cr_uid;
3332 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
3336 if (credanon != NULL)
3338 if (nd->nd_repstat) {
3340 vn_finished_write(mp);
3347 NFSEXITCODE2(0, nd);
3354 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3356 struct filedesc *fdp;
3360 fdp = p->td_proc->p_fd;
3361 if (fd < 0 || fd >= fdp->fd_nfiles ||
3362 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3374 * Called from nfssvc() to update the exports list. Just call
3375 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3376 * in the mount list.
3379 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3381 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3383 struct nameidata nd;
3386 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3387 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3389 else if (error == 0) {
3390 if (nfsexargp->fspec == NULL) {
3395 * If fspec != NULL, this is the v4root path.
3397 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3398 nfsexargp->fspec, p);
3399 if ((error = namei(&nd)) != 0)
3401 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3404 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3405 NFSBCOPY((caddr_t)&fh,
3406 nfs_rootfh.nfsrvfh_data,
3407 sizeof (fhandle_t));
3418 * This function needs to test to see if the system is near its limit
3419 * for memory allocation via malloc() or mget() and return True iff
3420 * either of these resources are near their limit.
3421 * XXX (For now, this is just a stub.)
3423 int nfsrv_testmalloclimit = 0;
3425 nfsrv_mallocmget_limit(void)
3427 static int printmesg = 0;
3428 static int testval = 1;
3430 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3431 if ((printmesg++ % 100) == 0)
3432 printf("nfsd: malloc/mget near limit\n");
3439 * BSD specific initialization of a mount point.
3444 static int inited = 0;
3449 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3450 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3451 TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3452 nfsv4root_mnt.mnt_export = NULL;
3453 TAILQ_INIT(&nfsv4root_opt);
3454 TAILQ_INIT(&nfsv4root_newopt);
3455 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3456 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3457 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3458 nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3462 * Get a vnode for a file handle, without checking exports, etc.
3465 nfsvno_getvp(fhandle_t *fhp)
3471 mp = vfs_busyfs(&fhp->fh_fsid);
3474 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3482 * Do a local VOP_ADVLOCK().
3485 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3486 u_int64_t end, struct thread *td)
3492 if (nfsrv_dolocallocks == 0)
3494 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3496 fl.l_whence = SEEK_SET;
3498 fl.l_start = (off_t)first;
3499 if (end == NFS64BITSSET) {
3503 fl.l_len = (off_t)tlen;
3506 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3507 * values for all calls, so that all locks will be held by the
3508 * nfsd server. (The nfsd server handles conflicts between the
3510 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3511 * bytes, so it can't be put in l_sysid.
3513 if (nfsv4_sysid == 0)
3514 nfsv4_sysid = nlm_acquire_next_sysid();
3515 fl.l_pid = (pid_t)0;
3516 fl.l_sysid = (int)nfsv4_sysid;
3518 if (ftype == F_UNLCK)
3519 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3520 (F_POSIX | F_REMOTE));
3522 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3523 (F_POSIX | F_REMOTE));
3531 * Check the nfsv4 root exports.
3534 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3536 struct ucred *credanon;
3537 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3540 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3541 &credanon, &numsecflavor, secflavors);
3543 error = NFSERR_PROGUNAVAIL;
3546 if (credanon != NULL)
3548 for (i = 0; i < numsecflavor; i++) {
3549 if (secflavors[i] == AUTH_SYS)
3550 nd->nd_flag |= ND_EXAUTHSYS;
3551 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3552 nd->nd_flag |= ND_EXGSS;
3553 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3554 nd->nd_flag |= ND_EXGSSINTEGRITY;
3555 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3556 nd->nd_flag |= ND_EXGSSPRIVACY;
3559 /* And set ND_EXxx flags for TLS. */
3560 if ((exflags & MNT_EXTLS) != 0) {
3561 nd->nd_flag |= ND_EXTLS;
3562 if ((exflags & MNT_EXTLSCERT) != 0)
3563 nd->nd_flag |= ND_EXTLSCERT;
3564 if ((exflags & MNT_EXTLSCERTUSER) != 0)
3565 nd->nd_flag |= ND_EXTLSCERTUSER;
3574 * Nfs server pseudo system call for the nfsd's
3580 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3583 struct nfsd_addsock_args sockarg;
3584 struct nfsd_nfsd_args nfsdarg;
3585 struct nfsd_nfsd_oargs onfsdarg;
3586 struct nfsd_pnfsd_args pnfsdarg;
3587 struct vnode *vp, *nvp, *curdvp;
3588 struct pnfsdsfile *pf;
3589 struct nfsdevice *ds, *fds;
3590 cap_rights_t rights;
3591 int buflen, error, ret;
3592 char *buf, *cp, *cp2, *cp3;
3593 char fname[PNFS_FILENAME_LEN + 1];
3595 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3596 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3600 * Since we don't know what rights might be required,
3601 * pretend that we need them all. It is better to be too
3602 * careful than too reckless.
3604 error = fget(td, sockarg.sock,
3605 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3608 if (fp->f_type != DTYPE_SOCKET) {
3613 error = nfsrvd_addsock(fp);
3615 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3616 if (uap->argp == NULL) {
3620 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3621 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3623 nfsdarg.principal = onfsdarg.principal;
3624 nfsdarg.minthreads = onfsdarg.minthreads;
3625 nfsdarg.maxthreads = onfsdarg.maxthreads;
3626 nfsdarg.version = 1;
3627 nfsdarg.addr = NULL;
3628 nfsdarg.addrlen = 0;
3629 nfsdarg.dnshost = NULL;
3630 nfsdarg.dnshostlen = 0;
3631 nfsdarg.dspath = NULL;
3632 nfsdarg.dspathlen = 0;
3633 nfsdarg.mdspath = NULL;
3634 nfsdarg.mdspathlen = 0;
3635 nfsdarg.mirrorcnt = 1;
3638 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3641 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3642 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3643 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3644 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3645 nfsdarg.mirrorcnt >= 1 &&
3646 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3647 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3648 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3649 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3650 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3651 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3652 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3653 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3654 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3659 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3661 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3662 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3664 free(nfsdarg.addr, M_TEMP);
3668 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3669 nfsdarg.dnshost = cp;
3670 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3671 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3673 free(nfsdarg.addr, M_TEMP);
3674 free(nfsdarg.dnshost, M_TEMP);
3678 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3679 nfsdarg.dspath = cp;
3680 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3681 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3683 free(nfsdarg.addr, M_TEMP);
3684 free(nfsdarg.dnshost, M_TEMP);
3685 free(nfsdarg.dspath, M_TEMP);
3689 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3690 nfsdarg.mdspath = cp;
3692 nfsdarg.addr = NULL;
3693 nfsdarg.addrlen = 0;
3694 nfsdarg.dnshost = NULL;
3695 nfsdarg.dnshostlen = 0;
3696 nfsdarg.dspath = NULL;
3697 nfsdarg.dspathlen = 0;
3698 nfsdarg.mdspath = NULL;
3699 nfsdarg.mdspathlen = 0;
3700 nfsdarg.mirrorcnt = 1;
3702 error = nfsrvd_nfsd(td, &nfsdarg);
3703 free(nfsdarg.addr, M_TEMP);
3704 free(nfsdarg.dnshost, M_TEMP);
3705 free(nfsdarg.dspath, M_TEMP);
3706 free(nfsdarg.mdspath, M_TEMP);
3707 } else if (uap->flag & NFSSVC_PNFSDS) {
3708 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3709 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3710 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3711 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3712 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3715 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3717 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3718 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3719 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3720 buf = malloc(buflen, M_TEMP, M_WAITOK);
3721 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3723 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3724 if (error == 0 && pnfsdarg.dspath != NULL) {
3725 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3726 error = copyinstr(pnfsdarg.dspath, cp2,
3727 PATH_MAX + 1, NULL);
3728 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3732 if (error == 0 && pnfsdarg.curdspath != NULL) {
3733 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3734 error = copyinstr(pnfsdarg.curdspath, cp3,
3735 PATH_MAX + 1, NULL);
3736 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3743 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3744 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3746 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3748 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3749 printf("copymr: dsdir out of range\n");
3752 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3753 error = nfsrv_copymr(vp, nvp,
3754 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3755 (struct pnfsdsfile *)buf,
3756 buflen / sizeof(*pf), td->td_ucred, td);
3759 if (fds != NULL && error == 0) {
3760 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3761 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3763 nfsrv_dsremove(curdvp, fname,
3765 NFSVOPUNLOCK(curdvp);
3768 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3776 error = nfssvc_srvcall(td, uap, td->td_ucred);
3785 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3787 struct nfsex_args export;
3788 struct nfsex_oldargs oexp;
3789 struct file *fp = NULL;
3790 int stablefd, i, len;
3791 struct nfsd_clid adminrevoke;
3792 struct nfsd_dumplist dumplist;
3793 struct nfsd_dumpclients *dumpclients;
3794 struct nfsd_dumplocklist dumplocklist;
3795 struct nfsd_dumplocks *dumplocks;
3796 struct nameidata nd;
3798 int error = EINVAL, igotlock;
3801 static int suspend_nfsd = 0;
3803 if (uap->flag & NFSSVC_PUBLICFH) {
3804 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3805 sizeof (fhandle_t));
3806 error = copyin(uap->argp,
3807 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3810 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3811 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3812 error = copyin(uap->argp,(caddr_t)&export,
3813 sizeof (struct nfsex_args));
3816 if (export.export.ex_ngroups > NGROUPS_MAX ||
3817 export.export.ex_ngroups < 0)
3819 else if (export.export.ex_ngroups > 0) {
3820 grps = malloc(export.export.ex_ngroups *
3821 sizeof(gid_t), M_TEMP, M_WAITOK);
3822 error = copyin(export.export.ex_groups, grps,
3823 export.export.ex_ngroups * sizeof(gid_t));
3824 export.export.ex_groups = grps;
3826 export.export.ex_groups = NULL;
3828 error = nfsrv_v4rootexport(&export, cred, p);
3831 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3832 NFSSVC_V4ROOTEXPORT) {
3833 error = copyin(uap->argp,(caddr_t)&oexp,
3834 sizeof (struct nfsex_oldargs));
3836 memset(&export.export, 0, sizeof(export.export));
3837 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3838 export.export.ex_root = oexp.export.ex_root;
3839 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3840 export.export.ex_ngroups =
3841 oexp.export.ex_anon.cr_ngroups;
3842 export.export.ex_groups = NULL;
3843 if (export.export.ex_ngroups > XU_NGROUPS ||
3844 export.export.ex_ngroups < 0)
3846 else if (export.export.ex_ngroups > 0) {
3847 export.export.ex_groups = malloc(
3848 export.export.ex_ngroups * sizeof(gid_t),
3850 for (i = 0; i < export.export.ex_ngroups; i++)
3851 export.export.ex_groups[i] =
3852 oexp.export.ex_anon.cr_groups[i];
3854 export.export.ex_addr = oexp.export.ex_addr;
3855 export.export.ex_addrlen = oexp.export.ex_addrlen;
3856 export.export.ex_mask = oexp.export.ex_mask;
3857 export.export.ex_masklen = oexp.export.ex_masklen;
3858 export.export.ex_indexfile = oexp.export.ex_indexfile;
3859 export.export.ex_numsecflavors =
3860 oexp.export.ex_numsecflavors;
3861 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3862 export.export.ex_numsecflavors < 0)
3865 for (i = 0; i < export.export.ex_numsecflavors;
3867 export.export.ex_secflavors[i] =
3868 oexp.export.ex_secflavors[i];
3870 export.fspec = oexp.fspec;
3872 error = nfsrv_v4rootexport(&export, cred, p);
3873 free(export.export.ex_groups, M_TEMP);
3875 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3878 } else if (uap->flag & NFSSVC_STABLERESTART) {
3879 error = copyin(uap->argp, (caddr_t)&stablefd,
3882 error = fp_getfvp(p, stablefd, &fp, &vp);
3883 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3885 if (!error && newnfs_numnfsd != 0)
3888 nfsrv_stablefirst.nsf_fp = fp;
3889 nfsrv_setupstable(p);
3891 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3892 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3893 sizeof (struct nfsd_clid));
3895 error = nfsrv_adminrevoke(&adminrevoke, p);
3896 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3897 error = copyin(uap->argp, (caddr_t)&dumplist,
3898 sizeof (struct nfsd_dumplist));
3899 if (!error && (dumplist.ndl_size < 1 ||
3900 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3903 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3904 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3905 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3906 error = copyout(dumpclients, dumplist.ndl_list, len);
3907 free(dumpclients, M_TEMP);
3909 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3910 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3911 sizeof (struct nfsd_dumplocklist));
3912 if (!error && (dumplocklist.ndllck_size < 1 ||
3913 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3916 error = nfsrv_lookupfilename(&nd,
3917 dumplocklist.ndllck_fname, p);
3919 len = sizeof (struct nfsd_dumplocks) *
3920 dumplocklist.ndllck_size;
3921 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3922 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3923 dumplocklist.ndllck_size, p);
3925 error = copyout(dumplocks, dumplocklist.ndllck_list,
3927 free(dumplocks, M_TEMP);
3929 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3932 nfsd_master_pid = procp->p_pid;
3933 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3934 nfsd_master_start = procp->p_stats->p_start;
3935 nfsd_master_proc = procp;
3937 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3938 NFSLOCKV4ROOTMUTEX();
3939 if (suspend_nfsd == 0) {
3940 /* Lock out all nfsd threads */
3942 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3943 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3944 } while (igotlock == 0 && suspend_nfsd == 0);
3947 NFSUNLOCKV4ROOTMUTEX();
3949 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3950 NFSLOCKV4ROOTMUTEX();
3951 if (suspend_nfsd != 0) {
3952 nfsv4_unlock(&nfsd_suspend_lock, 0);
3955 NFSUNLOCKV4ROOTMUTEX();
3965 * Returns 0 if ok, 1 otherwise.
3968 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3973 * This seems odd, but allow the case where the security flavor
3974 * list is empty. This happens when NFSv4 is traversing non-exported
3975 * file systems. Exported file systems should always have a non-empty
3976 * security flavor list.
3978 if (exp->nes_numsecflavor == 0)
3981 for (i = 0; i < exp->nes_numsecflavor; i++) {
3983 * The tests for privacy and integrity must be first,
3984 * since ND_GSS is set for everything but AUTH_SYS.
3986 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3987 (nd->nd_flag & ND_GSSPRIVACY))
3989 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3990 (nd->nd_flag & ND_GSSINTEGRITY))
3992 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3993 (nd->nd_flag & ND_GSS))
3995 if (exp->nes_secflavors[i] == AUTH_SYS &&
3996 (nd->nd_flag & ND_GSS) == 0)
4003 * Calculate a hash value for the fid in a file handle.
4006 nfsrv_hashfh(fhandle_t *fhp)
4010 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4015 * Calculate a hash value for the sessionid.
4018 nfsrv_hashsessionid(uint8_t *sessionid)
4022 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4027 * Signal the userland master nfsd to backup the stable restart file.
4030 nfsrv_backupstable(void)
4034 if (nfsd_master_proc != NULL) {
4035 procp = pfind(nfsd_master_pid);
4036 /* Try to make sure it is the correct process. */
4037 if (procp == nfsd_master_proc &&
4038 procp->p_stats->p_start.tv_sec ==
4039 nfsd_master_start.tv_sec &&
4040 procp->p_stats->p_start.tv_usec ==
4041 nfsd_master_start.tv_usec &&
4042 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4043 kern_psignal(procp, SIGUSR2);
4045 nfsd_master_proc = NULL;
4053 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4054 * The arguments are in a structure, so that they can be passed through
4055 * taskqueue for a kernel process to execute this function.
4057 struct nfsrvdscreate {
4061 struct ucred *tcred;
4064 struct pnfsdsfile *pf;
4068 struct vattr createva;
4072 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4073 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4074 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4077 struct nameidata named;
4082 struct nfsmount *nmp;
4085 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4086 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
4087 nfsvno_setpathbuf(&named, &bufp, &hashp);
4088 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4089 named.ni_cnd.cn_thread = p;
4090 named.ni_cnd.cn_nameptr = bufp;
4091 if (fnamep != NULL) {
4092 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4093 named.ni_cnd.cn_namelen = strlen(bufp);
4095 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4096 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4098 /* Create the date file in the DS mount. */
4099 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4101 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4104 /* Set the ownership of the file. */
4105 error = VOP_SETATTR(nvp, nvap, tcred);
4106 NFSD_DEBUG(4, "nfsrv_dscreate:"
4107 " setattr-uid=%d\n", error);
4112 printf("pNFS: pnfscreate failed=%d\n", error);
4114 printf("pNFS: pnfscreate vnlock=%d\n", error);
4117 nmp = VFSTONFS(nvp->v_mount);
4118 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4119 != 0 || nmp->nm_nam->sa_len > sizeof(
4120 struct sockaddr_in6) ||
4121 np->n_fhp->nfh_len != NFSX_MYFH) {
4122 printf("Bad DS file: fstype=%s salen=%d"
4124 nvp->v_mount->mnt_vfc->vfc_name,
4125 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4129 /* Set extattrs for the DS on the MDS file. */
4132 error = VOP_GETATTR(nvp, &va, tcred);
4134 dsa->dsa_filerev = va.va_filerev;
4135 dsa->dsa_size = va.va_size;
4136 dsa->dsa_atime = va.va_atime;
4137 dsa->dsa_mtime = va.va_mtime;
4138 dsa->dsa_bytes = va.va_bytes;
4142 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4144 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4145 nmp->nm_nam->sa_len);
4146 NFSBCOPY(named.ni_cnd.cn_nameptr,
4148 sizeof(pf->dsf_filename));
4151 printf("pNFS: pnfscreate can't get DS"
4152 " attr=%d\n", error);
4153 if (nvpp != NULL && error == 0)
4158 nfsvno_relpathbuf(&named);
4163 * Start up the thread that will execute nfsrv_dscreate().
4166 start_dscreate(void *arg, int pending)
4168 struct nfsrvdscreate *dsc;
4170 dsc = (struct nfsrvdscreate *)arg;
4171 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4172 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4174 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4178 * Create a pNFS data file on the Data Server(s).
4181 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4184 struct nfsrvdscreate *dsc, *tdsc = NULL;
4185 struct nfsdevice *ds, *tds, *fds;
4187 struct pnfsdsfile *pf, *tpf;
4188 struct pnfsdsattr dsattr;
4190 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4191 struct nfsmount *nmp;
4196 struct ucred *tcred;
4197 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4200 /* Get a DS server directory in a round-robin order. */
4206 * Search for the first entry that handles this MDS fs, but use the
4207 * first entry for all MDS fs's otherwise.
4209 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4210 if (tds->nfsdev_nmp != NULL) {
4211 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4213 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4214 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4222 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4225 i = dsdir[0] = ds->nfsdev_nextdir;
4226 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4227 dvp[0] = ds->nfsdev_dsdir[i];
4228 tds = TAILQ_NEXT(ds, nfsdev_list);
4229 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4230 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4231 if (tds->nfsdev_nmp != NULL &&
4232 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4233 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4234 fsidcmp(&mp->mnt_stat.f_fsid,
4235 &tds->nfsdev_mdsfsid) == 0))) {
4236 dsdir[mirrorcnt] = i;
4237 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4239 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4244 /* Put at end of list to implement round-robin usage. */
4245 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4246 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4250 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4252 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4255 error = nfsvno_getfh(vp, &fh, p);
4257 error = VOP_GETATTR(vp, &va, cred);
4259 /* Set the attributes for "vp" to Setattr the DS vp. */
4262 vamode = va.va_mode;
4266 va.va_mode = vamode;
4269 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4271 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4273 /* Make data file name based on FH. */
4274 tcred = newnfs_getcred();
4277 * Create the file on each DS mirror, using kernel process(es) for the
4278 * additional mirrors.
4281 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4282 tpf->dsf_dir = dsdir[i];
4283 tdsc->tcred = tcred;
4286 tdsc->createva = *vap;
4287 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4294 if (nfs_pnfsiothreads != 0) {
4295 ret = nfs_pnfsio(start_dscreate, tdsc);
4296 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4299 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4300 NULL, tcred, p, NULL);
4302 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4304 if (failpos == -1 && nfsds_failerr(ret))
4312 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4313 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4314 &dsattr, NULL, tcred, p, NULL);
4315 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4316 failpos = mirrorcnt - 1;
4320 timo = hz / 50; /* Wait for 20msec. */
4323 /* Wait for kernel task(s) to complete. */
4324 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4325 while (tdsc->inprog != 0 && tdsc->done == 0)
4326 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4327 if (tdsc->err != 0) {
4328 if (failpos == -1 && nfsds_failerr(tdsc->err))
4330 else if (error == 0)
4336 * If failpos has been set, that mirror has failed, so it needs
4340 nmp = VFSTONFS(dvp[failpos]->v_mount);
4342 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4343 NFSMNTP_CANCELRPCS)) == 0) {
4344 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4346 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4347 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4350 nfsrv_killrpcs(nmp);
4352 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4360 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4362 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4363 mirrorcnt, nfsrv_maxpnfsmirror);
4365 * For all mirrors that couldn't be created, fill in the
4366 * *pf structure, but with an IP address == 0.0.0.0.
4368 tpf = pf + mirrorcnt;
4369 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4371 tpf->dsf_sin.sin_family = AF_INET;
4372 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4373 tpf->dsf_sin.sin_addr.s_addr = 0;
4374 tpf->dsf_sin.sin_port = 0;
4377 error = vn_extattr_set(vp, IO_NODELOCKED,
4378 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4379 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4381 error = vn_extattr_set(vp, IO_NODELOCKED,
4382 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4383 sizeof(dsattr), (char *)&dsattr, p);
4385 printf("pNFS: pnfscreate setextattr=%d\n",
4388 printf("pNFS: pnfscreate=%d\n", error);
4394 * Get the information needed to remove the pNFS Data Server file from the
4395 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4396 * DS directory vnode. The caller must unlock *ddvp when done with it.
4399 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4400 int *mirrorcntp, char *fname, fhandle_t *fhp)
4403 struct ucred *tcred;
4408 /* If not an exported regular file or not a pNFS server, just return. */
4409 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4410 nfsrv_devidcnt == 0)
4413 /* Check to see if this is the last hard link. */
4414 tcred = newnfs_getcred();
4415 error = VOP_GETATTR(vp, &va, tcred);
4418 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4421 if (va.va_nlink > 1)
4424 error = nfsvno_getfh(vp, fhp, p);
4426 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4431 buf = malloc(buflen, M_TEMP, M_WAITOK);
4432 /* Get the directory vnode for the DS mount and the file handle. */
4433 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4434 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4437 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4441 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4442 * The arguments are in a structure, so that they can be passed through
4443 * taskqueue for a kernel process to execute this function.
4445 struct nfsrvdsremove {
4449 struct ucred *tcred;
4453 char fname[PNFS_FILENAME_LEN + 1];
4457 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4460 struct nameidata named;
4466 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4469 named.ni_cnd.cn_nameiop = DELETE;
4470 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4471 named.ni_cnd.cn_cred = tcred;
4472 named.ni_cnd.cn_thread = p;
4473 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4474 nfsvno_setpathbuf(&named, &bufp, &hashp);
4475 named.ni_cnd.cn_nameptr = bufp;
4476 named.ni_cnd.cn_namelen = strlen(fname);
4477 strlcpy(bufp, fname, NAME_MAX);
4478 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4479 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4480 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4482 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4486 nfsvno_relpathbuf(&named);
4488 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4493 * Start up the thread that will execute nfsrv_dsremove().
4496 start_dsremove(void *arg, int pending)
4498 struct nfsrvdsremove *dsrm;
4500 dsrm = (struct nfsrvdsremove *)arg;
4501 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4504 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4508 * Remove a pNFS data file from a Data Server.
4509 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4510 * removed to set up the dvp and fill in the FH.
4513 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4516 struct ucred *tcred;
4517 struct nfsrvdsremove *dsrm, *tdsrm;
4518 struct nfsdevice *ds;
4519 struct nfsmount *nmp;
4520 int failpos, i, ret, timo;
4522 tcred = newnfs_getcred();
4525 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4527 * Remove the file on each DS mirror, using kernel process(es) for the
4528 * additional mirrors.
4531 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4532 tdsrm->tcred = tcred;
4534 tdsrm->dvp = dvp[i];
4535 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4540 if (nfs_pnfsiothreads != 0) {
4541 ret = nfs_pnfsio(start_dsremove, tdsrm);
4542 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4545 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4546 if (failpos == -1 && nfsds_failerr(ret))
4550 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4551 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4552 failpos = mirrorcnt - 1;
4553 timo = hz / 50; /* Wait for 20msec. */
4556 /* Wait for kernel task(s) to complete. */
4557 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4558 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4559 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4560 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4565 * If failpos has been set, that mirror has failed, so it needs
4569 nmp = VFSTONFS(dvp[failpos]->v_mount);
4571 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4572 NFSMNTP_CANCELRPCS)) == 0) {
4573 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4575 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4576 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4579 nfsrv_killrpcs(nmp);
4581 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4587 /* Get rid all layouts for the file. */
4588 nfsrv_freefilelayouts(fhp);
4595 * Generate a file name based on the file handle and put it in *bufp.
4596 * Return the number of bytes generated.
4599 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4603 const uint8_t *hexdigits = "0123456789abcdef";
4605 cp = (uint8_t *)fhp;
4606 for (i = 0; i < sizeof(*fhp); i++) {
4607 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4608 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4615 * Update the Metadata file's attributes from the DS file when a Read/Write
4616 * layout is returned.
4617 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4618 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4621 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4623 struct ucred *tcred;
4626 /* Do this as root so that it won't fail with EACCES. */
4627 tcred = newnfs_getcred();
4628 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4629 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4635 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4638 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4643 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4644 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4649 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4650 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4651 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4652 off_t *offp, int content, bool *eofp)
4654 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4655 fhandle_t fh[NFSDEV_MAXMIRRORS];
4656 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4657 struct nfsdevice *ds;
4658 struct pnfsdsattr dsattr;
4659 struct opnfsdsattr odsattr;
4661 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4663 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4665 * If not a regular file, not exported or not a pNFS server,
4666 * just return ENOENT.
4668 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4669 nfsrv_devidcnt == 0)
4673 buf = malloc(buflen, M_TEMP, M_WAITOK);
4677 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4678 * from the MetaData file's extended attribute.
4680 if (ioproc == NFSPROC_GETATTR) {
4681 error = vn_extattr_get(vp, IO_NODELOCKED,
4682 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4685 if (buflen == sizeof(odsattr)) {
4686 NFSBCOPY(buf, &odsattr, buflen);
4687 nap->na_filerev = odsattr.dsa_filerev;
4688 nap->na_size = odsattr.dsa_size;
4689 nap->na_atime = odsattr.dsa_atime;
4690 nap->na_mtime = odsattr.dsa_mtime;
4692 * Fake na_bytes by rounding up na_size.
4693 * Since we don't know the block size, just
4694 * use BLKDEV_IOSIZE.
4696 nap->na_bytes = (odsattr.dsa_size +
4697 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4698 } else if (buflen == sizeof(dsattr)) {
4699 NFSBCOPY(buf, &dsattr, buflen);
4700 nap->na_filerev = dsattr.dsa_filerev;
4701 nap->na_size = dsattr.dsa_size;
4702 nap->na_atime = dsattr.dsa_atime;
4703 nap->na_mtime = dsattr.dsa_mtime;
4704 nap->na_bytes = dsattr.dsa_bytes;
4710 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4711 * returns 0, just return now. nfsrv_checkdsattr()
4712 * returns 0 if there is no Read/Write layout
4713 * plus either an Open/Write_access or Write
4714 * delegation issued to a client for the file.
4716 if (nfsrv_pnfsgetdsattr == 0 ||
4717 nfsrv_checkdsattr(vp, p) == 0) {
4724 * Clear ENOATTR so the code below will attempt to do a
4725 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4726 * the extended attribute.
4728 if (error == ENOATTR)
4737 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4739 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4740 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4741 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4744 for (i = 0; i < mirrorcnt; i++)
4745 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4747 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4749 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4752 if (origmircnt == -1)
4753 origmircnt = mirrorcnt;
4755 * If failpos is set to a mirror#, then that mirror has
4756 * failed and will be disabled. For Read, Getattr and Seek, the
4757 * function only tries one mirror, so if that mirror has
4758 * failed, it will need to be retried. As such, increment
4759 * tryitagain for these cases.
4760 * For Write, Setattr and Setacl, the function tries all
4761 * mirrors and will not return an error for the case where
4762 * one mirror has failed. For these cases, the functioning
4763 * mirror(s) will have been modified, so a retry isn't
4764 * necessary. These functions will set failpos for the
4767 if (ioproc == NFSPROC_READDS) {
4768 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4770 if (nfsds_failerr(error) && mirrorcnt > 1) {
4772 * Setting failpos will cause the mirror
4773 * to be disabled and then a retry of this
4780 } else if (ioproc == NFSPROC_WRITEDS)
4781 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4782 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4783 else if (ioproc == NFSPROC_SETATTR)
4784 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4785 mirrorcnt, nap, &failpos);
4786 else if (ioproc == NFSPROC_SETACL)
4787 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4788 mirrorcnt, aclp, &failpos);
4789 else if (ioproc == NFSPROC_SEEKDS) {
4790 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4792 if (nfsds_failerr(error) && mirrorcnt > 1) {
4794 * Setting failpos will cause the mirror
4795 * to be disabled and then a retry of this
4802 } else if (ioproc == NFSPROC_ALLOCATE)
4803 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4804 &nmp[0], mirrorcnt, &failpos);
4806 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4807 vp, nmp[mirrorcnt - 1], nap);
4808 if (nfsds_failerr(error) && mirrorcnt > 1) {
4810 * Setting failpos will cause the mirror
4811 * to be disabled and then a retry of this
4812 * getattr is required.
4814 failpos = mirrorcnt - 1;
4821 failnmp = nmp[failpos];
4822 NFSLOCKMNT(failnmp);
4823 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4824 NFSMNTP_CANCELRPCS)) == 0) {
4825 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4826 NFSUNLOCKMNT(failnmp);
4827 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4829 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4832 nfsrv_killrpcs(failnmp);
4833 NFSLOCKMNT(failnmp);
4834 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4837 NFSUNLOCKMNT(failnmp);
4839 for (i = 0; i < mirrorcnt; i++)
4840 NFSVOPUNLOCK(dvp[i]);
4841 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4843 /* Try the Read/Getattr again if a mirror was deleted. */
4844 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4847 /* Return ENOENT for any Extended Attribute error. */
4851 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4856 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4858 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4859 * to be checked. If it points to a NULL nmp, then it returns
4860 * a suitable destination.
4861 * curnmp - If non-NULL, it is the source mount for the copy.
4864 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4865 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4866 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4867 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4869 struct vnode *dvp, *nvp = NULL, **tdvpp;
4871 struct nfsmount *nmp, *newnmp;
4872 struct sockaddr *sad;
4873 struct sockaddr_in *sin;
4874 struct nfsdevice *ds, *tds, *fndds;
4875 struct pnfsdsfile *pf;
4877 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4879 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4888 if (newnmpp != NULL)
4893 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4894 "pnfsd.dsfile", buflenp, buf, p);
4895 mirrorcnt = *buflenp / sizeof(*pf);
4896 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4897 *buflenp != sizeof(*pf) * mirrorcnt))
4900 pf = (struct pnfsdsfile *)buf;
4901 /* If curnmp != NULL, check for a match in the mirror list. */
4902 if (curnmp != NULL && error == 0) {
4904 for (i = 0; i < mirrorcnt; i++, pf++) {
4905 sad = (struct sockaddr *)&pf->dsf_sin;
4906 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
4918 pf = (struct pnfsdsfile *)buf;
4919 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
4921 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
4923 sad = (struct sockaddr *)&pf->dsf_sin;
4925 dsdir = pf->dsf_dir;
4926 if (dsdir >= nfsrv_dsdirsize) {
4927 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
4929 } else if (nvpp != NULL && newnmp != NULL &&
4930 nfsaddr2_match(sad, newnmp->nm_nam))
4933 if (ippos != NULL && curnmp == NULL &&
4934 sad->sa_family == AF_INET &&
4935 sin->sin_addr.s_addr == 0)
4937 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
4939 /* Use the socket address to find the mount point. */
4942 /* Find a match for the IP address. */
4943 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
4944 if (ds->nfsdev_nmp != NULL) {
4945 dvp = ds->nfsdev_dvp;
4946 nmp = VFSTONFS(dvp->v_mount);
4947 if (nmp != ds->nfsdev_nmp)
4948 printf("different2 nmp %p %p\n",
4949 nmp, ds->nfsdev_nmp);
4950 if (nfsaddr2_match(sad, nmp->nm_nam)) {
4956 if (fndds != NULL && newnmpp != NULL &&
4958 /* Search for a place to make a mirror copy. */
4959 TAILQ_FOREACH(tds, &nfsrv_devidhead,
4961 if (tds->nfsdev_nmp != NULL &&
4963 ((tds->nfsdev_mdsisset == 0 &&
4964 fndds->nfsdev_mdsisset == 0) ||
4965 (tds->nfsdev_mdsisset != 0 &&
4966 fndds->nfsdev_mdsisset != 0 &&
4967 fsidcmp(&tds->nfsdev_mdsfsid,
4968 &mp->mnt_stat.f_fsid) == 0))) {
4969 *newnmpp = tds->nfsdev_nmp;
4975 * Move this entry to the end of the
4976 * list, so it won't be selected as
4977 * easily the next time.
4979 TAILQ_REMOVE(&nfsrv_devidhead, tds,
4981 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
4986 if (fndds != NULL) {
4987 dvp = fndds->nfsdev_dsdir[dsdir];
4988 if (lktype != 0 || fhiszero != 0 ||
4989 (nvpp != NULL && *nvpp == NULL)) {
4991 error = vn_lock(dvp,
4993 else if (lktype != 0)
4994 error = vn_lock(dvp, lktype);
4996 error = vn_lock(dvp, LK_SHARED);
4998 * If the file handle is all 0's, try to
4999 * do a Lookup against the DS to acquire
5001 * If dvpp == NULL or the Lookup fails,
5002 * unlock dvp after the call.
5004 if (error == 0 && (fhiszero != 0 ||
5005 (nvpp != NULL && *nvpp == NULL))) {
5006 error = nfsrv_pnfslookupds(vp,
5022 if (error != 0 || lktype == 0)
5028 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5029 if (devid != NULL) {
5030 NFSBCOPY(fndds->nfsdev_deviceid,
5031 devid, NFSX_V4DEVICEID);
5032 devid += NFSX_V4DEVICEID;
5037 NFSBCOPY(&pf->dsf_fh, fhp++,
5039 if (fnamep != NULL && gotone == 1)
5042 sizeof(pf->dsf_filename));
5044 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5049 if (error == 0 && gotone == 0)
5052 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5055 *mirrorcntp = gotone;
5057 if (gotone > 0 && dvpp != NULL) {
5059 * If the error didn't occur on the first one and
5060 * dvpp != NULL, the one(s) prior to the failure will
5061 * have locked dvp's that need to be unlocked.
5063 for (i = 0; i < gotone; i++) {
5064 NFSVOPUNLOCK(*dvpp);
5069 * If it found the vnode to be copied from before a failure,
5070 * it needs to be vput()'d.
5072 if (nvpp != NULL && *nvpp != NULL) {
5081 * Set the extended attribute for the Change attribute.
5084 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5086 struct pnfsdsattr dsattr;
5089 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5090 dsattr.dsa_filerev = nap->na_filerev;
5091 dsattr.dsa_size = nap->na_size;
5092 dsattr.dsa_atime = nap->na_atime;
5093 dsattr.dsa_mtime = nap->na_mtime;
5094 dsattr.dsa_bytes = nap->na_bytes;
5095 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5096 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5098 printf("pNFS: setextattr=%d\n", error);
5103 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5104 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5107 struct nfsrv_descript *nd;
5109 struct mbuf *m, *m2;
5110 int error = 0, retlen, tlen, trimlen;
5112 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5113 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5116 * Use a stateid where other is an alternating 01010 pattern and
5117 * seqid is 0xffffffff. This value is not defined as special by
5118 * the RFC and is used by the FreeBSD NFS server to indicate an
5119 * MDS->DS proxy operation.
5121 st.other[0] = 0x55555555;
5122 st.other[1] = 0x55555555;
5123 st.other[2] = 0x55555555;
5124 st.seqid = 0xffffffff;
5125 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5127 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5128 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5129 txdr_hyper(off, tl);
5130 *(tl + 2) = txdr_unsigned(len);
5131 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5132 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5137 if (nd->nd_repstat == 0) {
5138 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5139 NFSM_STRSIZ(retlen, len);
5141 /* Trim off the pre-data XDR from the mbuf chain. */
5143 while (m != NULL && m != nd->nd_md) {
5144 if (m->m_next == nd->nd_md) {
5146 m_freem(nd->nd_mrep);
5147 nd->nd_mrep = m = nd->nd_md;
5152 printf("nfsrv_readdsrpc: busted mbuf list\n");
5158 * Now, adjust first mbuf so that any XDR before the
5159 * read data is skipped over.
5161 trimlen = nd->nd_dpos - mtod(m, char *);
5163 m->m_len -= trimlen;
5164 NFSM_DATAP(m, trimlen);
5168 * Truncate the mbuf chain at retlen bytes of data,
5169 * plus XDR padding that brings the length up to a
5172 tlen = NFSM_RNDUP(retlen);
5174 if (m->m_len >= tlen) {
5184 } while (m != NULL);
5186 printf("nfsrv_readdsrpc: busted mbuf list\n");
5195 error = nd->nd_repstat;
5197 /* If nd->nd_mrep is already NULL, this is a no-op. */
5198 m_freem(nd->nd_mrep);
5200 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5205 * Do a write RPC on a DS data file, using this structure for the arguments,
5206 * so that this function can be executed by a separate kernel process.
5208 struct nfsrvwritedsdorpc {
5215 struct nfsmount *nmp;
5223 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5224 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5227 struct nfsrv_descript *nd;
5228 nfsattrbit_t attrbits;
5230 int commit, error, retlen;
5232 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5233 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5234 sizeof(fhandle_t), NULL, NULL, 0, 0);
5237 * Use a stateid where other is an alternating 01010 pattern and
5238 * seqid is 0xffffffff. This value is not defined as special by
5239 * the RFC and is used by the FreeBSD NFS server to indicate an
5240 * MDS->DS proxy operation.
5242 st.other[0] = 0x55555555;
5243 st.other[1] = 0x55555555;
5244 st.other[2] = 0x55555555;
5245 st.seqid = 0xffffffff;
5246 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5247 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5248 txdr_hyper(off, tl);
5251 * Do all writes FileSync, since the server doesn't hold onto dirty
5252 * buffers. Since clients should be accessing the DS servers directly
5253 * using the pNFS layouts, this just needs to work correctly as a
5256 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5257 *tl = txdr_unsigned(len);
5258 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5260 /* Put data in mbuf chain. */
5261 nd->nd_mb->m_next = m;
5263 /* Set nd_mb and nd_bpos to end of data. */
5264 while (m->m_next != NULL)
5267 nfsm_set(nd, m->m_len);
5268 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5270 /* Do a Getattr for the attributes that change upon writing. */
5271 NFSZERO_ATTRBIT(&attrbits);
5272 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5273 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5274 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5275 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5276 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5277 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5278 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5279 (void) nfsrv_putattrbit(nd, &attrbits);
5280 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5281 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5286 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5287 /* Get rid of weak cache consistency data for now. */
5288 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5289 (ND_NFSV4 | ND_V4WCCATTR)) {
5290 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5291 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5292 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5296 * Get rid of Op# and status for next op.
5298 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5300 nd->nd_flag |= ND_NOMOREDATA;
5302 if (nd->nd_repstat == 0) {
5303 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5304 retlen = fxdr_unsigned(int, *tl++);
5305 commit = fxdr_unsigned(int, *tl);
5306 if (commit != NFSWRITE_FILESYNC)
5308 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5309 retlen, commit, error);
5311 error = nd->nd_repstat;
5312 /* We have no use for the Write Verifier since we use FileSync. */
5315 * Get the Change, Size, Access Time and Modify Time attributes and set
5316 * on the Metadata file, so its attributes will be what the file's
5317 * would be if it had been written.
5320 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5321 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5322 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5324 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5326 m_freem(nd->nd_mrep);
5328 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5333 * Start up the thread that will execute nfsrv_writedsdorpc().
5336 start_writedsdorpc(void *arg, int pending)
5338 struct nfsrvwritedsdorpc *drpc;
5340 drpc = (struct nfsrvwritedsdorpc *)arg;
5341 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5342 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5344 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5348 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5349 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5350 struct mbuf **mpp, char *cp, int *failposp)
5352 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5355 int error, i, offs, ret, timo;
5357 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5358 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5361 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5364 /* Calculate offset in mbuf chain that data starts. */
5365 offs = cp - mtod(*mpp, char *);
5366 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5369 * Do the write RPC for every DS, using a separate kernel process
5370 * for every DS except the last one.
5373 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5375 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5383 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5385 if (nfs_pnfsiothreads != 0) {
5386 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5387 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5391 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5393 if (nfsds_failerr(ret) && *failposp == -1)
5395 else if (error == 0 && ret != 0)
5401 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5402 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5403 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5404 *failposp = mirrorcnt - 1;
5405 else if (error == 0 && ret != 0)
5408 error = nfsrv_setextattr(vp, &na, p);
5409 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5411 timo = hz / 50; /* Wait for 20msec. */
5414 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5415 /* Wait for RPCs on separate threads to complete. */
5416 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5417 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5418 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5420 else if (error == 0 && tdrpc->err != 0)
5428 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5429 * so that this function can be executed by a separate kernel process.
5431 struct nfsrvallocatedsdorpc {
5438 struct nfsmount *nmp;
5445 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5446 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5449 struct nfsrv_descript *nd;
5450 nfsattrbit_t attrbits;
5454 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5455 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5456 sizeof(fhandle_t), NULL, NULL, 0, 0);
5459 * Use a stateid where other is an alternating 01010 pattern and
5460 * seqid is 0xffffffff. This value is not defined as special by
5461 * the RFC and is used by the FreeBSD NFS server to indicate an
5462 * MDS->DS proxy operation.
5464 st.other[0] = 0x55555555;
5465 st.other[1] = 0x55555555;
5466 st.other[2] = 0x55555555;
5467 st.seqid = 0xffffffff;
5468 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5469 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5470 txdr_hyper(off, tl); tl += 2;
5471 txdr_hyper(len, tl); tl += 2;
5472 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5474 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5475 NFSGETATTR_ATTRBIT(&attrbits);
5476 nfsrv_putattrbit(nd, &attrbits);
5477 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5478 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5483 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5485 if (nd->nd_repstat == 0) {
5486 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5487 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5488 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5490 error = nd->nd_repstat;
5491 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5493 m_freem(nd->nd_mrep);
5495 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5500 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5503 start_allocatedsdorpc(void *arg, int pending)
5505 struct nfsrvallocatedsdorpc *drpc;
5507 drpc = (struct nfsrvallocatedsdorpc *)arg;
5508 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5509 drpc->len, NULL, drpc->cred, drpc->p);
5511 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5515 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5516 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5519 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5521 int error, i, ret, timo;
5523 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5526 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5530 * Do the allocate RPC for every DS, using a separate kernel process
5531 * for every DS except the last one.
5534 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5536 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5545 if (nfs_pnfsiothreads != 0) {
5546 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5547 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5551 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5553 if (nfsds_failerr(ret) && *failposp == -1)
5555 else if (error == 0 && ret != 0)
5561 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5562 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5563 *failposp = mirrorcnt - 1;
5564 else if (error == 0 && ret != 0)
5567 error = nfsrv_setextattr(vp, &na, p);
5568 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5570 timo = hz / 50; /* Wait for 20msec. */
5573 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5574 /* Wait for RPCs on separate threads to complete. */
5575 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5576 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5577 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5579 else if (error == 0 && tdrpc->err != 0)
5587 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5588 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5589 struct nfsvattr *dsnap)
5592 struct nfsrv_descript *nd;
5594 nfsattrbit_t attrbits;
5597 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5598 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5600 * Use a stateid where other is an alternating 01010 pattern and
5601 * seqid is 0xffffffff. This value is not defined as special by
5602 * the RFC and is used by the FreeBSD NFS server to indicate an
5603 * MDS->DS proxy operation.
5605 st.other[0] = 0x55555555;
5606 st.other[1] = 0x55555555;
5607 st.other[2] = 0x55555555;
5608 st.seqid = 0xffffffff;
5609 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5611 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5612 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5614 /* Do a Getattr for the attributes that change due to writing. */
5615 NFSZERO_ATTRBIT(&attrbits);
5616 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5617 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5618 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5619 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5620 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5621 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5622 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5623 (void) nfsrv_putattrbit(nd, &attrbits);
5624 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5625 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5630 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5632 /* Get rid of weak cache consistency data for now. */
5633 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5634 (ND_NFSV4 | ND_V4WCCATTR)) {
5635 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5636 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5637 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5641 * Get rid of Op# and status for next op.
5643 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5645 nd->nd_flag |= ND_NOMOREDATA;
5647 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5650 if (nd->nd_repstat != 0)
5651 error = nd->nd_repstat;
5653 * Get the Change, Size, Access Time and Modify Time attributes and set
5654 * on the Metadata file, so its attributes will be what the file's
5655 * would be if it had been written.
5658 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5659 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5660 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5662 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5664 m_freem(nd->nd_mrep);
5666 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5670 struct nfsrvsetattrdsdorpc {
5675 struct nfsmount *nmp;
5680 struct nfsvattr dsna;
5685 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5688 start_setattrdsdorpc(void *arg, int pending)
5690 struct nfsrvsetattrdsdorpc *drpc;
5692 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5693 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5694 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5699 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5700 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5701 struct nfsvattr *nap, int *failposp)
5703 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5705 int error, i, ret, timo;
5707 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5710 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5714 * Do the setattr RPC for every DS, using a separate kernel process
5715 * for every DS except the last one.
5718 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5721 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5729 if (nfs_pnfsiothreads != 0) {
5730 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5731 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5735 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5737 if (nfsds_failerr(ret) && *failposp == -1)
5739 else if (error == 0 && ret != 0)
5745 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5746 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5747 *failposp = mirrorcnt - 1;
5748 else if (error == 0 && ret != 0)
5751 error = nfsrv_setextattr(vp, &na, p);
5752 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5754 timo = hz / 50; /* Wait for 20msec. */
5757 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5758 /* Wait for RPCs on separate threads to complete. */
5759 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5760 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5761 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5763 else if (error == 0 && tdrpc->err != 0)
5771 * Do a Setattr of an NFSv4 ACL on the DS file.
5774 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5775 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5777 struct nfsrv_descript *nd;
5779 nfsattrbit_t attrbits;
5782 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5783 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5785 * Use a stateid where other is an alternating 01010 pattern and
5786 * seqid is 0xffffffff. This value is not defined as special by
5787 * the RFC and is used by the FreeBSD NFS server to indicate an
5788 * MDS->DS proxy operation.
5790 st.other[0] = 0x55555555;
5791 st.other[1] = 0x55555555;
5792 st.other[2] = 0x55555555;
5793 st.seqid = 0xffffffff;
5794 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5796 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5797 NFSZERO_ATTRBIT(&attrbits);
5798 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5800 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5801 * so passing in the metadata "vp" will be ok, since it is of
5802 * the same type (VREG).
5804 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5805 NULL, 0, 0, 0, 0, 0, NULL);
5806 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5807 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5812 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5814 error = nd->nd_repstat;
5815 m_freem(nd->nd_mrep);
5820 struct nfsrvsetacldsdorpc {
5825 struct nfsmount *nmp;
5834 * Start up the thread that will execute nfsrv_setacldsdorpc().
5837 start_setacldsdorpc(void *arg, int pending)
5839 struct nfsrvsetacldsdorpc *drpc;
5841 drpc = (struct nfsrvsetacldsdorpc *)arg;
5842 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5843 drpc->vp, drpc->nmp, drpc->aclp);
5848 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5849 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5852 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
5853 int error, i, ret, timo;
5855 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5858 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5862 * Do the setattr RPC for every DS, using a separate kernel process
5863 * for every DS except the last one.
5866 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5869 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5877 if (nfs_pnfsiothreads != 0) {
5878 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5879 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5883 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5885 if (nfsds_failerr(ret) && *failposp == -1)
5887 else if (error == 0 && ret != 0)
5893 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5894 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5895 *failposp = mirrorcnt - 1;
5896 else if (error == 0 && ret != 0)
5898 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5900 timo = hz / 50; /* Wait for 20msec. */
5903 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5904 /* Wait for RPCs on separate threads to complete. */
5905 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5906 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
5907 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5909 else if (error == 0 && tdrpc->err != 0)
5917 * Getattr call to the DS for the attributes that change due to writing.
5920 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5921 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
5923 struct nfsrv_descript *nd;
5925 nfsattrbit_t attrbits;
5927 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
5928 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5929 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
5930 sizeof(fhandle_t), NULL, NULL, 0, 0);
5931 NFSZERO_ATTRBIT(&attrbits);
5932 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5933 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5934 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5935 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5936 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5937 (void) nfsrv_putattrbit(nd, &attrbits);
5938 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5939 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5944 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
5946 if (nd->nd_repstat == 0) {
5947 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5948 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5951 * We can only save the updated values in the extended
5952 * attribute if the vp is exclusively locked.
5953 * This should happen when any of the following operations
5954 * occur on the vnode:
5955 * Close, Delegreturn, LayoutCommit, LayoutReturn
5956 * As such, the updated extended attribute should get saved
5957 * before nfsrv_checkdsattr() returns 0 and allows the cached
5958 * attributes to be returned without calling this function.
5960 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
5961 error = nfsrv_setextattr(vp, nap, p);
5962 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
5966 error = nd->nd_repstat;
5967 m_freem(nd->nd_mrep);
5969 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
5974 * Seek call to a DS.
5977 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
5978 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
5981 struct nfsrv_descript *nd;
5985 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
5987 * Use a stateid where other is an alternating 01010 pattern and
5988 * seqid is 0xffffffff. This value is not defined as special by
5989 * the RFC and is used by the FreeBSD NFS server to indicate an
5990 * MDS->DS proxy operation.
5992 st.other[0] = 0x55555555;
5993 st.other[1] = 0x55555555;
5994 st.other[2] = 0x55555555;
5995 st.seqid = 0xffffffff;
5996 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5997 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
5998 sizeof(fhandle_t), NULL, NULL, 0, 0);
5999 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6000 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6001 txdr_hyper(*offp, tl); tl += 2;
6002 *tl = txdr_unsigned(content);
6003 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6004 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6009 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6010 if (nd->nd_repstat == 0) {
6011 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6012 if (*tl++ == newnfs_true)
6016 *offp = fxdr_hyper(tl);
6018 error = nd->nd_repstat;
6020 m_freem(nd->nd_mrep);
6022 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6027 * Get the device id and file handle for a DS file.
6030 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6031 fhandle_t *fhp, char *devid)
6037 buf = malloc(buflen, M_TEMP, M_WAITOK);
6038 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6039 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6045 * Do a Lookup against the DS for the filename.
6048 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6049 struct vnode **nvpp, NFSPROC_T *p)
6051 struct nameidata named;
6052 struct ucred *tcred;
6058 tcred = newnfs_getcred();
6059 named.ni_cnd.cn_nameiop = LOOKUP;
6060 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6061 named.ni_cnd.cn_cred = tcred;
6062 named.ni_cnd.cn_thread = p;
6063 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
6064 nfsvno_setpathbuf(&named, &bufp, &hashp);
6065 named.ni_cnd.cn_nameptr = bufp;
6066 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6067 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6068 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6069 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6070 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6072 nfsvno_relpathbuf(&named);
6075 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6080 * Set the file handle to the correct one.
6083 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6084 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6090 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6092 * We can only do a vn_set_extattr() if the vnode is exclusively
6093 * locked and vn_start_write() has been done. If devid != NULL or
6094 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6095 * may not have been done.
6096 * If not done now, it will be done on a future call.
6098 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6100 ret = vn_extattr_set(vp, IO_NODELOCKED,
6101 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6103 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6107 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6108 * when the DS has failed.
6111 nfsrv_killrpcs(struct nfsmount *nmp)
6115 * Call newnfs_nmcancelreqs() to cause
6116 * any RPCs in progress on the mount point to
6118 * This will cause any process waiting for an
6119 * RPC to complete while holding a vnode lock
6120 * on the mounted-on vnode (such as "df" or
6121 * a non-forced "umount") to fail.
6122 * This will unlock the mounted-on vnode so
6123 * a forced dismount can succeed.
6124 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6127 newnfs_nmcancelreqs(nmp);
6131 * Sum up the statfs info for each of the DSs, so that the client will
6132 * receive the total for all DSs.
6135 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6138 struct nfsdevice *ds;
6139 struct vnode **dvpp, **tdvpp, *dvp;
6141 int cnt, error = 0, i;
6143 if (nfsrv_devidcnt <= 0)
6145 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6146 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6148 /* Get an array of the dvps for the DSs. */
6152 /* First, search for matches for same file system. */
6153 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6154 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6155 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6156 if (++i > nfsrv_devidcnt)
6158 *tdvpp++ = ds->nfsdev_dvp;
6162 * If no matches for same file system, total all servers not assigned
6166 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6167 if (ds->nfsdev_nmp != NULL &&
6168 ds->nfsdev_mdsisset == 0) {
6169 if (++i > nfsrv_devidcnt)
6171 *tdvpp++ = ds->nfsdev_dvp;
6178 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6180 for (i = 0; i < cnt && error == 0; i++) {
6182 error = VFS_STATFS(dvp->v_mount, tsf);
6184 if (sf->f_bsize == 0) {
6185 if (tsf->f_bsize > 0)
6186 sf->f_bsize = tsf->f_bsize;
6190 if (tsf->f_blocks > 0) {
6191 if (sf->f_bsize != tsf->f_bsize) {
6192 tot = tsf->f_blocks * tsf->f_bsize;
6193 sf->f_blocks += (tot / sf->f_bsize);
6195 sf->f_blocks += tsf->f_blocks;
6197 if (tsf->f_bfree > 0) {
6198 if (sf->f_bsize != tsf->f_bsize) {
6199 tot = tsf->f_bfree * tsf->f_bsize;
6200 sf->f_bfree += (tot / sf->f_bsize);
6202 sf->f_bfree += tsf->f_bfree;
6204 if (tsf->f_bavail > 0) {
6205 if (sf->f_bsize != tsf->f_bsize) {
6206 tot = tsf->f_bavail * tsf->f_bsize;
6207 sf->f_bavail += (tot / sf->f_bsize);
6209 sf->f_bavail += tsf->f_bavail;
6222 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6226 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6227 error = NFSERR_ATTRNOTSUPP;
6231 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6232 * Make sure it has enough room for that - splitting every entry
6233 * into two and appending "canonical six" entries at the end.
6234 * Cribbed out of kern/vfs_acl.c - Rick M.
6236 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6237 error = NFSERR_ATTRNOTSUPP;
6240 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6242 error = nfsrv_dssetacl(vp, aclp, cred, p);
6243 if (error == ENOENT)
6253 * Seek vnode op call (actually it is a VOP_IOCTL()).
6254 * This function is called with the vnode locked, but unlocks and vrele()s
6255 * the vp before returning.
6258 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6259 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6264 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6266 * Attempt to seek on a DS file. A return of ENOENT implies
6267 * there is no DS file to seek on.
6269 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6270 NULL, NULL, NULL, NULL, offp, content, eofp);
6271 if (error != ENOENT) {
6277 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6278 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6279 * NFSv4.2 is *eofp == true and error == 0 for this case.
6282 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6284 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6285 /* Handle the cases where we might be at EOF. */
6286 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6287 if (ret == 0 && *offp == at.na_size) {
6291 if (ret != 0 && error == 0)
6300 * Allocate vnode op call.
6303 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6308 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6310 * Attempt to allocate on a DS file. A return of ENOENT implies
6311 * there is no DS file to allocate on.
6313 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6314 NULL, NULL, NULL, NULL, &len, 0, NULL);
6315 if (error != ENOENT)
6320 * Do the actual VOP_ALLOCATE(), looping a reasonable number of
6321 * times to achieve completion.
6324 while (error == 0 && len > 0 && trycnt++ < 20)
6325 error = VOP_ALLOCATE(vp, &off, &len);
6326 if (error == 0 && len > 0)
6333 * Get Extended Atribute vnode op into an mbuf list.
6336 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6337 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6338 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6341 struct uio io, *uiop = &io;
6342 struct mbuf *m, *m2;
6343 int alen, error, len, tlen;
6346 /* First, find out the size of the extended attribute. */
6347 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6350 return (NFSERR_NOXATTR);
6351 if (siz > maxresp - NFS_MAXXDR)
6352 return (NFSERR_XATTR2BIG);
6354 tlen = NFSM_RNDUP(len);
6357 * If cnt > MCLBYTES and the reply will not be saved, use
6358 * ext_pgs mbufs for TLS.
6359 * For NFSv4.0, we do not know for sure if the reply will
6360 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6361 * Always use ext_pgs mbufs if ND_EXTPG is set.
6363 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6364 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6365 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6366 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6367 maxextsiz, &m, &m2, &iv);
6369 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6373 uiop->uio_iovcnt = 0;
6374 uiop->uio_iov = iv = NULL;
6377 uiop->uio_offset = 0;
6378 uiop->uio_resid = tlen;
6379 uiop->uio_rw = UIO_READ;
6380 uiop->uio_segflg = UIO_SYSSPACE;
6383 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6390 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6394 if (uiop->uio_resid > 0) {
6396 len = tlen - uiop->uio_resid;
6397 tlen = NFSM_RNDUP(len);
6399 printf("nfsvno_getxattr: weird size read\n");
6403 } else if (alen != tlen || tlen != len)
6404 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6422 * Set Extended attribute vnode op from an mbuf list.
6425 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6426 char *cp, struct ucred *cred, struct thread *p)
6429 struct uio uio, *uiop = &uio;
6434 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6440 uiop->uio_rw = UIO_WRITE;
6441 uiop->uio_segflg = UIO_SYSSPACE;
6443 uiop->uio_offset = 0;
6444 uiop->uio_resid = len;
6446 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6448 uiop->uio_iovcnt = cnt;
6450 uiop->uio_iov = iv = NULL;
6451 uiop->uio_iovcnt = 0;
6454 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6465 * Remove Extended attribute vnode op.
6468 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6469 struct ucred *cred, struct thread *p)
6474 * Get rid of any delegations. I am not sure why this is required,
6475 * but RFC-8276 says so.
6477 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6481 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6487 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6488 if (error == EOPNOTSUPP)
6489 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6497 * List Extended Atribute vnode op into an mbuf list.
6500 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6501 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6509 /* First, find out the size of the extended attribute. */
6510 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6513 return (NFSERR_NOXATTR);
6514 if (siz <= cookie) {
6519 if (siz > cookie + *lenp) {
6520 siz = cookie + *lenp;
6524 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6525 if (siz > 10 * 1024 * 1024) {
6526 error = NFSERR_XATTR2BIG;
6529 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6530 iv.iov_base = *bufp;
6536 io.uio_rw = UIO_READ;
6537 io.uio_segflg = UIO_SYSSPACE;
6540 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6545 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6549 if (io.uio_resid > 0)
6550 siz -= io.uio_resid;
6555 free(*bufp, M_TEMP);
6563 * Trim trailing data off the mbuf list being built.
6566 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6567 int bextpg, int bextpgsiz)
6572 if (mb->m_next != NULL) {
6573 m_freem(mb->m_next);
6576 if ((mb->m_flags & M_EXTPG) != 0) {
6577 /* First, get rid of any pages after this position. */
6578 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
6579 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
6580 vm_page_unwire_noq(pg);
6583 mb->m_epg_npgs = bextpg + 1;
6585 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
6587 fullpgsiz = PAGE_SIZE;
6588 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
6589 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
6590 for (i = 1; i < mb->m_epg_npgs; i++)
6591 mb->m_len += m_epg_pagelen(mb, i, 0);
6592 nd->nd_bextpgsiz = bextpgsiz;
6593 nd->nd_bextpg = bextpg;
6595 mb->m_len = bpos - mtod(mb, char *);
6600 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
6603 * Called once to initialize data structures...
6606 nfsd_modevent(module_t mod, int type, void *data)
6609 static int loaded = 0;
6616 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6617 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
6619 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
6622 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
6623 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
6624 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
6625 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
6626 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
6627 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
6634 #ifdef VV_DISABLEDELEG
6635 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
6636 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
6638 nfsd_call_servertimer = nfsrv_servertimer;
6639 nfsd_call_nfsd = nfssvc_nfsd;
6644 if (newnfs_numnfsd != 0) {
6649 #ifdef VV_DISABLEDELEG
6650 vn_deleg_ops.vndeleg_recall = NULL;
6651 vn_deleg_ops.vndeleg_disable = NULL;
6653 nfsd_call_servertimer = NULL;
6654 nfsd_call_nfsd = NULL;
6656 /* Clean out all NFSv4 state. */
6657 nfsrv_throwawayallstate(curthread);
6659 /* Clean the NFS server reply cache */
6660 nfsrvd_cleancache();
6662 /* Free up the krpc server pool. */
6663 if (nfsrvd_pool != NULL)
6664 svcpool_destroy(nfsrvd_pool);
6666 /* and get rid of the locks */
6667 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6668 mtx_destroy(&nfsrchash_table[i].mtx);
6669 mtx_destroy(&nfsrcahash_table[i].mtx);
6671 mtx_destroy(&nfsrc_udpmtx);
6672 mtx_destroy(&nfs_v4root_mutex);
6673 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
6674 mtx_destroy(&nfsrv_dontlistlock_mtx);
6675 mtx_destroy(&nfsrv_recalllock_mtx);
6676 for (i = 0; i < nfsrv_sessionhashsize; i++)
6677 mtx_destroy(&nfssessionhash[i].mtx);
6678 if (nfslayouthash != NULL) {
6679 for (i = 0; i < nfsrv_layouthashsize; i++)
6680 mtx_destroy(&nfslayouthash[i].mtx);
6681 free(nfslayouthash, M_NFSDSESSION);
6683 lockdestroy(&nfsv4root_mnt.mnt_explock);
6684 free(nfsclienthash, M_NFSDCLIENT);
6685 free(nfslockhash, M_NFSDLOCKFILE);
6686 free(nfssessionhash, M_NFSDSESSION);
6698 static moduledata_t nfsd_mod = {
6703 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
6705 /* So that loader and kldload(2) can find us, wherever we are.. */
6706 MODULE_VERSION(nfsd, 1);
6707 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
6708 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
6709 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
6710 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);