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 extern uint32_t nfs_srvmaxio;
80 extern int nfs_bufpackets;
81 extern u_long sb_max_adj;
82 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
85 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
86 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
87 struct mtx nfsrc_udpmtx;
88 struct mtx nfs_v4root_mutex;
89 struct mtx nfsrv_dontlistlock_mtx;
90 struct mtx nfsrv_recalllock_mtx;
91 struct nfsrvfh nfs_rootfh, nfs_pubfh;
92 int nfs_pubfhset = 0, nfs_rootfhset = 0;
93 struct proc *nfsd_master_proc = NULL;
94 int nfsd_debuglevel = 0;
95 static pid_t nfsd_master_pid = (pid_t)-1;
96 static char nfsd_master_comm[MAXCOMLEN + 1];
97 static struct timeval nfsd_master_start;
98 static uint32_t nfsv4_sysid = 0;
99 static fhandle_t zerofh;
101 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
104 int nfsrv_enable_crossmntpt = 1;
105 static int nfs_commit_blks;
106 static int nfs_commit_miss;
107 extern int nfsrv_issuedelegs;
108 extern int nfsrv_dolocallocks;
109 extern int nfsd_enable_stringtouid;
110 extern struct nfsdevicehead nfsrv_devidhead;
112 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
114 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
115 struct mbuf **, struct iovec **);
116 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
118 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
120 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
121 int *, char *, fhandle_t *);
122 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
124 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
125 struct thread *, int, struct mbuf **, char *, struct mbuf **,
126 struct nfsvattr *, struct acl *, off_t *, int, bool *);
127 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
128 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
129 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
130 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
131 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
133 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
134 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
135 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
136 struct vnode *, struct nfsmount **, int, struct acl *, int *);
137 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
138 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
139 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
140 struct vnode *, struct nfsmount *, struct nfsvattr *);
141 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
142 NFSPROC_T *, struct nfsmount *);
143 static int nfsrv_putfhname(fhandle_t *, char *);
144 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
145 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
146 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
147 struct vnode *, NFSPROC_T *);
148 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
149 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
151 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
153 int nfs_pnfsio(task_fn_t *, void *);
155 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
157 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
158 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
159 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
161 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
163 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
164 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
165 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
166 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
167 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
168 0, "Debug level for NFS server");
169 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
170 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
171 static int nfsrv_pnfsgetdsattr = 1;
172 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
173 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
176 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
178 * The dsN subdirectories for the increased values must have been created
179 * on all DS servers before this increase is done.
181 u_int nfsrv_dsdirsize = 20;
183 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
185 int error, newdsdirsize;
187 newdsdirsize = nfsrv_dsdirsize;
188 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
189 if (error != 0 || req->newptr == NULL)
191 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
194 nfsrv_dsdirsize = newdsdirsize;
197 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
198 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
199 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
202 * nfs_srvmaxio can only be increased and only when the nfsd threads are
203 * not running. The setting must be a power of 2, with the current limit of
207 sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)
213 newsrvmaxio = nfs_srvmaxio;
214 error = sysctl_handle_int(oidp, &newsrvmaxio, 0, req);
215 if (error != 0 || req->newptr == NULL)
217 if (newsrvmaxio == nfs_srvmaxio)
219 if (newsrvmaxio < nfs_srvmaxio) {
220 printf("nfsd: vfs.nfsd.srvmaxio can only be increased\n");
223 if (newsrvmaxio > 1048576) {
224 printf("nfsd: vfs.nfsd.srvmaxio cannot be > 1Mbyte\n");
227 if ((newsrvmaxio & (newsrvmaxio - 1)) != 0) {
228 printf("nfsd: vfs.nfsd.srvmaxio must be a power of 2\n");
233 * Check that kern.ipc.maxsockbuf is large enough for
234 * newsrviomax, given the setting of vfs.nfs.bufpackets.
236 if ((newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets >
239 * Suggest vfs.nfs.bufpackets * maximum RPC message for
242 tval = (newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets;
245 * Convert suggested sb_max_adj value to a suggested
246 * sb_max value, which is what is set via kern.ipc.maxsockbuf.
247 * Perform the inverse calculation of (from uipc_sockbuf.c):
248 * sb_max_adj = (u_quad_t)sb_max * MCLBYTES /
249 * (MSIZE + MCLBYTES);
250 * XXX If the calculation of sb_max_adj from sb_max changes,
251 * this calculation must be changed as well.
253 tval *= (MSIZE + MCLBYTES); /* Brackets for readability. */
254 tval += MCLBYTES - 1; /* Round up divide. */
256 printf("nfsd: set kern.ipc.maxsockbuf to a minimum of "
257 "%ju to support %ubyte NFS I/O\n", (uintmax_t)tval,
263 if (newnfs_numnfsd != 0) {
265 printf("nfsd: cannot set vfs.nfsd.srvmaxio when nfsd "
266 "threads are running\n");
271 nfs_srvmaxio = newsrvmaxio;
275 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, srvmaxio,
276 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
277 sysctl_srvmaxio, "IU", "Maximum I/O size in bytes");
279 #define MAX_REORDERED_RPC 16
280 #define NUM_HEURISTIC 1031
281 #define NHUSE_INIT 64
283 #define NHUSE_MAX 2048
285 static struct nfsheur {
286 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
287 off_t nh_nextoff; /* next offset for sequential detection */
288 int nh_use; /* use count for selection */
289 int nh_seqcount; /* heuristic */
290 } nfsheur[NUM_HEURISTIC];
293 * Heuristic to detect sequential operation.
295 static struct nfsheur *
296 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
301 /* Locate best candidate. */
303 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
306 if (nfsheur[hi].nh_vp == vp) {
310 if (nfsheur[hi].nh_use > 0)
311 --nfsheur[hi].nh_use;
312 hi = (hi + 1) % NUM_HEURISTIC;
313 if (nfsheur[hi].nh_use < nh->nh_use)
317 /* Initialize hint if this is a new file. */
318 if (nh->nh_vp != vp) {
320 nh->nh_nextoff = uio->uio_offset;
321 nh->nh_use = NHUSE_INIT;
322 if (uio->uio_offset == 0)
328 /* Calculate heuristic. */
329 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
330 uio->uio_offset == nh->nh_nextoff) {
331 /* See comments in vfs_vnops.c:sequential_heuristic(). */
332 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
333 if (nh->nh_seqcount > IO_SEQMAX)
334 nh->nh_seqcount = IO_SEQMAX;
335 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
336 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
337 /* Probably a reordered RPC, leave seqcount alone. */
338 } else if (nh->nh_seqcount > 1) {
339 nh->nh_seqcount /= 2;
343 nh->nh_use += NHUSE_INC;
344 if (nh->nh_use > NHUSE_MAX)
345 nh->nh_use = NHUSE_MAX;
350 * Get attributes into nfsvattr structure.
353 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
354 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
355 nfsattrbit_t *attrbitp)
357 int error, gotattr, lockedit = 0;
360 if (vpislocked == 0) {
362 * When vpislocked == 0, the vnode is either exclusively
363 * locked by this thread or not locked by this thread.
364 * As such, shared lock it, if not exclusively locked.
366 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
368 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
373 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
374 * attributes, as required.
375 * This needs to be done for regular files if:
376 * - non-NFSv4 RPCs or
377 * - when attrbitp == NULL or
378 * - an NFSv4 RPC with any of the above attributes in attrbitp.
379 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
380 * these attributes. nfsrv_proxyds() will return an error if the
381 * server is not a pNFS one.
384 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
385 (nd->nd_flag & ND_NFSV4) == 0 ||
386 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
387 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
388 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
389 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
390 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
391 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
392 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
398 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
403 * If we got the Change, Size and Modify Time from the DS,
407 nvap->na_atime = na.na_atime;
408 nvap->na_mtime = na.na_mtime;
409 nvap->na_filerev = na.na_filerev;
410 nvap->na_size = na.na_size;
411 nvap->na_bytes = na.na_bytes;
413 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
414 error, (uintmax_t)na.na_filerev);
421 * Get a file handle for a vnode.
424 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
428 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
429 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
430 error = VOP_VPTOFH(vp, &fhp->fh_fid);
437 * Perform access checking for vnodes obtained from file handles that would
438 * refer to files already opened by a Unix client. You cannot just use
439 * vn_writechk() and VOP_ACCESSX() for two reasons.
440 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
442 * 2 - The owner is to be given access irrespective of mode bits for some
443 * operations, so that processes that chmod after opening a file don't
447 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
448 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
449 u_int32_t *supportedtypep)
452 int error = 0, getret = 0;
454 if (vpislocked == 0) {
455 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
460 if (accmode & VWRITE) {
461 /* Just vn_writechk() changed to check rdonly */
463 * Disallow write attempts on read-only file systems;
464 * unless the file is a socket or a block or character
465 * device resident on the file system.
467 if (NFSVNO_EXRDONLY(exp) ||
468 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
469 switch (vp->v_type) {
479 * If there's shared text associated with
480 * the inode, try to free it up once. If
481 * we fail, we can't allow writing.
483 if (VOP_IS_TEXT(vp) && error == 0)
493 * Should the override still be applied when ACLs are enabled?
495 error = VOP_ACCESSX(vp, accmode, cred, p);
496 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
498 * Try again with VEXPLICIT_DENY, to see if the test for
499 * deletion is supported.
501 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
503 if (vp->v_type == VDIR) {
504 accmode &= ~(VDELETE | VDELETE_CHILD);
506 error = VOP_ACCESSX(vp, accmode, cred, p);
507 } else if (supportedtypep != NULL) {
508 *supportedtypep &= ~NFSACCESS_DELETE;
514 * Allow certain operations for the owner (reads and writes
515 * on files that are already open).
517 if (override != NFSACCCHK_NOOVERRIDE &&
518 (error == EPERM || error == EACCES)) {
519 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
521 else if (override & NFSACCCHK_ALLOWOWNER) {
522 getret = VOP_GETATTR(vp, &vattr, cred);
523 if (getret == 0 && cred->cr_uid == vattr.va_uid)
536 * Set attribute(s) vnop.
539 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
540 struct thread *p, struct nfsexstuff *exp)
542 u_quad_t savsize = 0;
547 * If this is an exported file system and a pNFS service is running,
548 * don't VOP_SETATTR() of size for the MDS file system.
552 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
553 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
554 nvap->na_vattr.va_size > 0) {
555 savsize = nvap->na_vattr.va_size;
556 nvap->na_vattr.va_size = VNOVAL;
557 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
558 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
559 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
560 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
561 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
567 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
569 nvap->na_vattr.va_size = savsize;
570 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
571 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
572 nvap->na_vattr.va_size != VNOVAL ||
573 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
574 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
575 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
576 /* Never modify birthtime on a DS file. */
577 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
578 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
579 /* For a pNFS server, set the attributes on the DS file. */
580 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
581 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
582 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
591 * Set up nameidata for a lookup() call and do it.
594 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
595 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
596 struct vnode **retdirp)
598 struct componentname *cnp = &ndp->ni_cnd;
602 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
607 cnp->cn_nameptr = cnp->cn_pnbuf;
610 * Extract and set starting directory.
612 if (dp->v_type != VDIR) {
617 nfsvno_relpathbuf(ndp);
625 if (NFSVNO_EXRDONLY(exp))
626 cnp->cn_flags |= RDONLY;
627 ndp->ni_segflg = UIO_SYSSPACE;
629 if (nd->nd_flag & ND_PUBLOOKUP) {
631 if (cnp->cn_pnbuf[0] == '/') {
634 * Check for degenerate pathnames here, since lookup()
637 for (i = 1; i < ndp->ni_pathlen; i++)
638 if (cnp->cn_pnbuf[i] != '/')
640 if (i == ndp->ni_pathlen) {
641 error = NFSERR_ACCES;
647 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
648 (nd->nd_flag & ND_NFSV4) == 0) {
650 * Only cross mount points for NFSv4 when doing a
651 * mount while traversing the file system above
652 * the mount point, unless nfsrv_enable_crossmntpt is set.
654 cnp->cn_flags |= NOCROSSMOUNT;
658 * Initialize for scan, set ni_startdir and bump ref on dp again
659 * because lookup() will dereference ni_startdir.
663 ndp->ni_startdir = dp;
664 ndp->ni_rootdir = rootvnode;
665 ndp->ni_topdir = NULL;
668 cnp->cn_flags |= LOCKLEAF;
670 cnp->cn_nameptr = cnp->cn_pnbuf;
672 * Call lookup() to do the real work. If an error occurs,
673 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
674 * we do not have to dereference anything before returning.
675 * In either case ni_startdir will be dereferenced and NULLed
683 * Check for encountering a symbolic link. Trivial
684 * termination occurs if no symlink encountered.
686 if ((cnp->cn_flags & ISSYMLINK) == 0) {
687 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
688 nfsvno_relpathbuf(ndp);
689 if (ndp->ni_vp && !lockleaf)
690 NFSVOPUNLOCK(ndp->ni_vp);
697 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
698 NFSVOPUNLOCK(ndp->ni_dvp);
699 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
704 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
708 if (ndp->ni_pathlen > 1)
709 cp = uma_zalloc(namei_zone, M_WAITOK);
713 aiov.iov_len = MAXPATHLEN;
714 auio.uio_iov = &aiov;
717 auio.uio_rw = UIO_READ;
718 auio.uio_segflg = UIO_SYSSPACE;
720 auio.uio_resid = MAXPATHLEN;
721 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
724 if (ndp->ni_pathlen > 1)
725 uma_zfree(namei_zone, cp);
731 linklen = MAXPATHLEN - auio.uio_resid;
736 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
737 error = ENAMETOOLONG;
742 * Adjust or replace path
744 if (ndp->ni_pathlen > 1) {
745 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
746 uma_zfree(namei_zone, cnp->cn_pnbuf);
749 cnp->cn_pnbuf[linklen] = '\0';
750 ndp->ni_pathlen += linklen;
753 * Cleanup refs for next loop and check if root directory
754 * should replace current directory. Normally ni_dvp
755 * becomes the new base directory and is cleaned up when
756 * we loop. Explicitly null pointers after invalidation
757 * to clarify operation.
762 if (cnp->cn_pnbuf[0] == '/') {
764 ndp->ni_dvp = ndp->ni_rootdir;
767 ndp->ni_startdir = ndp->ni_dvp;
771 cnp->cn_flags &= ~LOCKLEAF;
775 nfsvno_relpathbuf(ndp);
778 ndp->ni_startdir = NULL;
779 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
784 NFSEXITCODE2(error, nd);
789 * Set up a pathname buffer and return a pointer to it and, optionally
790 * set a hash pointer.
793 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
795 struct componentname *cnp = &ndp->ni_cnd;
797 cnp->cn_flags |= (NOMACCHECK | HASBUF);
798 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
801 *bufpp = cnp->cn_pnbuf;
805 * Release the above path buffer, if not released by nfsvno_namei().
808 nfsvno_relpathbuf(struct nameidata *ndp)
811 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
813 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
814 ndp->ni_cnd.cn_flags &= ~HASBUF;
818 * Readlink vnode op into an mbuf list.
821 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
822 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
825 struct uio io, *uiop = &io;
826 struct mbuf *mp, *mp3;
827 int len, tlen, error = 0;
829 len = NFS_MAXPATHLEN;
831 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
834 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
836 uiop->uio_offset = 0;
837 uiop->uio_resid = len;
838 uiop->uio_rw = UIO_READ;
839 uiop->uio_segflg = UIO_SYSSPACE;
841 error = VOP_READLINK(vp, uiop, cred);
848 if (uiop->uio_resid > 0) {
849 len -= uiop->uio_resid;
850 tlen = NFSM_RNDUP(len);
854 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
855 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
868 * Create an mbuf chain and an associated iovec that can be used to Read
869 * or Getextattr of data.
870 * Upon success, return pointers to the first and last mbufs in the chain
871 * plus the malloc'd iovec and its iovlen.
874 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
877 struct mbuf *m, *m2 = NULL, *m3;
884 * Generate the mbuf list with the uio_iov ref. to it.
891 siz = min(M_TRAILINGSPACE(m), left);
900 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
906 panic("nfsrv_createiovec iov");
907 siz = min(M_TRAILINGSPACE(m), left);
909 iv->iov_base = mtod(m, caddr_t) + m->m_len;
924 * Create an mbuf chain and an associated iovec that can be used to Read
925 * or Getextattr of data.
926 * Upon success, return pointers to the first and last mbufs in the chain
927 * plus the malloc'd iovec and its iovlen.
928 * Same as above, but creates ext_pgs mbuf(s).
931 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
932 struct mbuf **mpendp, struct iovec **ivp)
934 struct mbuf *m, *m2 = NULL, *m3;
936 int i, left, pgno, siz;
941 * Generate the mbuf list with the uio_iov ref. to it.
945 siz = min(left, maxextsiz);
946 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
955 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
962 panic("nfsvno_createiovec_extpgs iov");
963 siz = min(PAGE_SIZE, left);
965 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
968 if (pgno == m->m_epg_npgs - 1)
969 m->m_epg_last_len = siz;
975 if (pgno == m->m_epg_npgs && left > 0) {
978 panic("nfsvno_createiovec_extpgs iov");
988 * Read vnode op call into mbuf list.
991 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
992 int maxextsiz, struct thread *p, struct mbuf **mpp,
993 struct mbuf **mpendp)
997 int error = 0, len, tlen, ioflag = 0;
999 struct uio io, *uiop = &io;
1003 * Attempt to read from a DS file. A return of ENOENT implies
1004 * there is no DS file to read.
1006 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
1007 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
1008 if (error != ENOENT)
1011 len = NFSM_RNDUP(cnt);
1013 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
1016 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
1018 uiop->uio_offset = off;
1019 uiop->uio_resid = len;
1020 uiop->uio_rw = UIO_READ;
1021 uiop->uio_segflg = UIO_SYSSPACE;
1022 uiop->uio_td = NULL;
1023 nh = nfsrv_sequential_heuristic(uiop, vp);
1024 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
1025 /* XXX KDM make this more systematic? */
1026 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
1027 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
1034 nh->nh_nextoff = uiop->uio_offset;
1035 tlen = len - uiop->uio_resid;
1036 cnt = cnt < tlen ? cnt : tlen;
1037 tlen = NFSM_RNDUP(cnt);
1041 } else if (len != tlen || tlen != cnt)
1042 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
1052 * Create the iovec for the mbuf chain passed in as an argument.
1053 * The "cp" argument is where the data starts within the first mbuf in
1054 * the chain. It returns the iovec and the iovcnt.
1057 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
1065 * Loop through the mbuf chain, counting how many mbufs are a
1066 * part of this write operation, so the iovec size is known.
1071 i = mtod(mp, caddr_t) + mp->m_len - cp;
1085 /* Now, create the iovec. */
1087 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1090 i = mtod(mp, caddr_t) + mp->m_len - cp;
1094 panic("nfsrv_createiovecw");
1105 cp = mtod(mp, caddr_t);
1112 * Write vnode op from an mbuf list.
1115 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1116 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1119 int cnt, ioflags, error;
1120 struct uio io, *uiop = &io;
1124 * Attempt to write to a DS file. A return of ENOENT implies
1125 * there is no DS file to write.
1127 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1128 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1129 if (error != ENOENT) {
1130 *stable = NFSWRITE_FILESYNC;
1134 if (*stable == NFSWRITE_UNSTABLE)
1135 ioflags = IO_NODELOCKED;
1137 ioflags = (IO_SYNC | IO_NODELOCKED);
1138 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1142 uiop->uio_iovcnt = cnt;
1143 uiop->uio_resid = retlen;
1144 uiop->uio_rw = UIO_WRITE;
1145 uiop->uio_segflg = UIO_SYSSPACE;
1146 NFSUIOPROC(uiop, p);
1147 uiop->uio_offset = off;
1148 nh = nfsrv_sequential_heuristic(uiop, vp);
1149 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1150 /* XXX KDM make this more systematic? */
1151 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1152 error = VOP_WRITE(vp, uiop, ioflags, cred);
1154 nh->nh_nextoff = uiop->uio_offset;
1162 * Common code for creating a regular file (plus special files for V2).
1165 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1166 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1167 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1171 struct thread *p = curthread;
1173 error = nd->nd_repstat;
1174 if (!error && ndp->ni_vp == NULL) {
1175 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1176 vrele(ndp->ni_startdir);
1177 error = VOP_CREATE(ndp->ni_dvp,
1178 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1179 /* For a pNFS server, create the data file on a DS. */
1180 if (error == 0 && nvap->na_type == VREG) {
1182 * Create a data file on a DS for a pNFS server.
1183 * This function just returns if not
1184 * running a pNFS DS or the creation fails.
1186 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1189 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1191 nfsvno_relpathbuf(ndp);
1193 if (*exclusive_flagp) {
1194 *exclusive_flagp = 0;
1195 NFSVNO_ATTRINIT(nvap);
1196 nvap->na_atime.tv_sec = cverf[0];
1197 nvap->na_atime.tv_nsec = cverf[1];
1198 error = VOP_SETATTR(ndp->ni_vp,
1199 &nvap->na_vattr, nd->nd_cred);
1203 error = NFSERR_NOTSUPP;
1208 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1209 * (This implies, just get out on an error.)
1211 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1212 nvap->na_type == VFIFO) {
1213 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1214 nvap->na_type = VFIFO;
1215 if (nvap->na_type != VFIFO &&
1216 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1217 vrele(ndp->ni_startdir);
1218 nfsvno_relpathbuf(ndp);
1222 nvap->na_rdev = rdev;
1223 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1224 &ndp->ni_cnd, &nvap->na_vattr);
1225 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1227 nfsvno_relpathbuf(ndp);
1228 vrele(ndp->ni_startdir);
1232 vrele(ndp->ni_startdir);
1233 nfsvno_relpathbuf(ndp);
1241 * Handle cases where error is already set and/or
1243 * 1 - clean up the lookup
1244 * 2 - iff !error and na_size set, truncate it
1246 vrele(ndp->ni_startdir);
1247 nfsvno_relpathbuf(ndp);
1249 if (ndp->ni_dvp == *vpp)
1253 if (!error && nvap->na_size != VNOVAL) {
1254 error = nfsvno_accchk(*vpp, VWRITE,
1255 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1256 NFSACCCHK_VPISLOCKED, NULL);
1258 tempsize = nvap->na_size;
1259 NFSVNO_ATTRINIT(nvap);
1260 nvap->na_size = tempsize;
1261 error = VOP_SETATTR(*vpp,
1262 &nvap->na_vattr, nd->nd_cred);
1275 * Do a mknod vnode op.
1278 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1284 vtyp = nvap->na_type;
1286 * Iff doesn't exist, create it.
1289 vrele(ndp->ni_startdir);
1290 nfsvno_relpathbuf(ndp);
1296 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1297 vrele(ndp->ni_startdir);
1298 nfsvno_relpathbuf(ndp);
1300 error = NFSERR_BADTYPE;
1303 if (vtyp == VSOCK) {
1304 vrele(ndp->ni_startdir);
1305 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1306 &ndp->ni_cnd, &nvap->na_vattr);
1307 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1309 nfsvno_relpathbuf(ndp);
1311 if (nvap->na_type != VFIFO &&
1312 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1313 vrele(ndp->ni_startdir);
1314 nfsvno_relpathbuf(ndp);
1318 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1319 &ndp->ni_cnd, &nvap->na_vattr);
1320 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1322 nfsvno_relpathbuf(ndp);
1323 vrele(ndp->ni_startdir);
1325 * Since VOP_MKNOD returns the ni_vp, I can't
1326 * see any reason to do the lookup.
1339 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1340 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1344 if (ndp->ni_vp != NULL) {
1345 if (ndp->ni_dvp == ndp->ni_vp)
1350 nfsvno_relpathbuf(ndp);
1354 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1356 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL, false);
1357 nfsvno_relpathbuf(ndp);
1368 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1369 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1370 struct nfsexstuff *exp)
1375 vrele(ndp->ni_startdir);
1376 nfsvno_relpathbuf(ndp);
1377 if (ndp->ni_dvp == ndp->ni_vp)
1386 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1387 &nvap->na_vattr, pathcp);
1389 * Although FreeBSD still had the lookup code in
1390 * it for 7/current, there doesn't seem to be any
1391 * point, since VOP_SYMLINK() returns the ni_vp.
1392 * Just vput it for v2.
1394 VOP_VPUT_PAIR(ndp->ni_dvp, &ndp->ni_vp, !not_v2 && error == 0);
1395 vrele(ndp->ni_startdir);
1396 nfsvno_relpathbuf(ndp);
1404 * Parse symbolic link arguments.
1405 * This function has an ugly side effect. It will malloc() an area for
1406 * the symlink and set iov_base to point to it, only if it succeeds.
1407 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1411 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1412 struct thread *p, char **pathcpp, int *lenp)
1415 char *pathcp = NULL;
1417 struct nfsv2_sattr *sp;
1421 if ((nd->nd_flag & ND_NFSV3) &&
1422 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1424 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1425 len = fxdr_unsigned(int, *tl);
1426 if (len > NFS_MAXPATHLEN || len <= 0) {
1430 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1431 error = nfsrv_mtostr(nd, pathcp, len);
1434 if (nd->nd_flag & ND_NFSV2) {
1435 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1436 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1440 NFSEXITCODE2(0, nd);
1444 free(pathcp, M_TEMP);
1445 NFSEXITCODE2(error, nd);
1450 * Remove a non-directory object.
1453 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1454 struct thread *p, struct nfsexstuff *exp)
1456 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1457 int error = 0, mirrorcnt;
1458 char fname[PNFS_FILENAME_LEN + 1];
1463 if (vp->v_type == VDIR)
1464 error = NFSERR_ISDIR;
1466 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1469 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1471 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1472 if (error == 0 && dsdvp[0] != NULL)
1473 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1474 if (ndp->ni_dvp == vp)
1479 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1480 nfsvno_relpathbuf(ndp);
1486 * Remove a directory.
1489 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1490 struct thread *p, struct nfsexstuff *exp)
1496 if (vp->v_type != VDIR) {
1501 * No rmdir "." please.
1503 if (ndp->ni_dvp == vp) {
1508 * The root of a mounted filesystem cannot be deleted.
1510 if (vp->v_vflag & VV_ROOT)
1514 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1515 if (ndp->ni_dvp == vp)
1520 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1521 nfsvno_relpathbuf(ndp);
1530 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1531 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1533 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1534 int error = 0, mirrorcnt;
1535 char fname[PNFS_FILENAME_LEN + 1];
1539 fvp = fromndp->ni_vp;
1541 vrele(fromndp->ni_dvp);
1546 tdvp = tondp->ni_dvp;
1549 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1550 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1552 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1553 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1556 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1557 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1562 * A rename to '.' or '..' results in a prematurely
1563 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1566 if ((tondp->ni_cnd.cn_namelen == 1 &&
1567 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1568 (tondp->ni_cnd.cn_namelen == 2 &&
1569 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1570 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1575 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1576 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1579 if (fvp->v_mount != tdvp->v_mount) {
1580 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1584 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1589 * If source and destination are the same, there is nothing to
1590 * do. Set error to -1 to indicate this.
1595 if (ndflag & ND_NFSV4) {
1596 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1597 error = nfsrv_checkremove(fvp, 0, NULL,
1598 (nfsquad_t)((u_quad_t)0), p);
1603 error = nfsrv_checkremove(tvp, 1, NULL,
1604 (nfsquad_t)((u_quad_t)0), p);
1607 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1608 * that the NFSv4 client won't be confused by the rename.
1609 * Since nfsd_recalldelegation() can only be called on an
1610 * unlocked vnode at this point and fvp is the file that will
1611 * still exist after the rename, just do fvp.
1613 nfsd_recalldelegation(fvp, p);
1615 if (error == 0 && tvp != NULL) {
1616 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1617 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1618 " dsdvp=%p\n", dsdvp[0]);
1622 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1623 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1632 vrele(fromndp->ni_dvp);
1639 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1640 * if the rename succeeded, the DS file for the tvp needs to be
1643 if (error == 0 && dsdvp[0] != NULL) {
1644 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1645 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1648 vrele(tondp->ni_startdir);
1649 nfsvno_relpathbuf(tondp);
1651 vrele(fromndp->ni_startdir);
1652 nfsvno_relpathbuf(fromndp);
1661 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1662 struct thread *p, struct nfsexstuff *exp)
1672 if (vp->v_mount != xp->v_mount)
1676 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1677 if (!VN_IS_DOOMED(vp))
1678 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1681 if (ndp->ni_dvp == vp) {
1686 VOP_VPUT_PAIR(ndp->ni_dvp, &vp, true);
1689 if (ndp->ni_dvp == ndp->ni_vp)
1696 nfsvno_relpathbuf(ndp);
1702 * Do the fsync() appropriate for the commit.
1705 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1711 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1712 * file is done. At this time VOP_FSYNC does not accept offset and
1713 * byte count parameters so call VOP_FSYNC the whole file for now.
1714 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1715 * File systems that do not use the buffer cache (as indicated
1716 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1718 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1719 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1721 * Give up and do the whole thing
1723 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1724 VM_OBJECT_WLOCK(vp->v_object);
1725 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1726 VM_OBJECT_WUNLOCK(vp->v_object);
1728 error = VOP_FSYNC(vp, MNT_WAIT, td);
1731 * Locate and synchronously write any buffers that fall
1732 * into the requested range. Note: we are assuming that
1733 * f_iosize is a power of 2.
1735 int iosize = vp->v_mount->mnt_stat.f_iosize;
1736 int iomask = iosize - 1;
1741 * Align to iosize boundary, super-align to page boundary.
1744 cnt += off & iomask;
1745 off &= ~(u_quad_t)iomask;
1747 if (off & PAGE_MASK) {
1748 cnt += off & PAGE_MASK;
1749 off &= ~(u_quad_t)PAGE_MASK;
1751 lblkno = off / iosize;
1753 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1754 VM_OBJECT_WLOCK(vp->v_object);
1755 vm_object_page_clean(vp->v_object, off, off + cnt,
1757 VM_OBJECT_WUNLOCK(vp->v_object);
1766 * If we have a buffer and it is marked B_DELWRI we
1767 * have to lock and write it. Otherwise the prior
1768 * write is assumed to have already been committed.
1770 * gbincore() can return invalid buffers now so we
1771 * have to check that bit as well (though B_DELWRI
1772 * should not be set if B_INVAL is set there could be
1773 * a race here since we haven't locked the buffer).
1775 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1776 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1777 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1779 continue; /* retry */
1781 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1784 bp->b_flags &= ~B_ASYNC;
1807 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1813 if (nfsrv_devidcnt > 0) {
1814 /* For a pNFS service, get the DS numbers. */
1815 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1816 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1822 error = VFS_STATFS(vp->v_mount, sf);
1825 sf->f_blocks = tsf->f_blocks;
1826 sf->f_bavail = tsf->f_bavail;
1827 sf->f_bfree = tsf->f_bfree;
1828 sf->f_bsize = tsf->f_bsize;
1831 * Since NFS handles these values as unsigned on the
1832 * wire, there is no way to represent negative values,
1833 * so set them to 0. Without this, they will appear
1834 * to be very large positive values for clients like
1837 if (sf->f_bavail < 0)
1839 if (sf->f_ffree < 0)
1848 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1849 * must handle nfsrv_opencheck() calls after any other access checks.
1852 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1853 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1854 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1855 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred,
1856 struct nfsexstuff *exp, struct vnode **vpp)
1858 struct vnode *vp = NULL;
1860 struct nfsexstuff nes;
1861 struct thread *p = curthread;
1863 if (ndp->ni_vp == NULL)
1864 nd->nd_repstat = nfsrv_opencheck(clientid,
1865 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1866 if (!nd->nd_repstat) {
1867 if (ndp->ni_vp == NULL) {
1868 vrele(ndp->ni_startdir);
1869 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1870 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1871 /* For a pNFS server, create the data file on a DS. */
1872 if (nd->nd_repstat == 0) {
1874 * Create a data file on a DS for a pNFS server.
1875 * This function just returns if not
1876 * running a pNFS DS or the creation fails.
1878 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1881 VOP_VPUT_PAIR(ndp->ni_dvp, nd->nd_repstat == 0 ?
1882 &ndp->ni_vp : NULL, false);
1883 nfsvno_relpathbuf(ndp);
1884 if (!nd->nd_repstat) {
1885 if (*exclusive_flagp) {
1886 *exclusive_flagp = 0;
1887 NFSVNO_ATTRINIT(nvap);
1888 nvap->na_atime.tv_sec = cverf[0];
1889 nvap->na_atime.tv_nsec = cverf[1];
1890 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1891 &nvap->na_vattr, cred);
1892 if (nd->nd_repstat != 0) {
1895 nd->nd_repstat = NFSERR_NOTSUPP;
1897 NFSSETBIT_ATTRBIT(attrbitp,
1898 NFSATTRBIT_TIMEACCESS);
1900 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1901 aclp, p, attrbitp, exp);
1906 if (ndp->ni_startdir)
1907 vrele(ndp->ni_startdir);
1908 nfsvno_relpathbuf(ndp);
1910 if (create == NFSV4OPEN_CREATE) {
1911 if (ndp->ni_dvp == vp)
1916 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1917 if (ndp->ni_cnd.cn_flags & RDONLY)
1918 NFSVNO_SETEXRDONLY(&nes);
1920 NFSVNO_EXINIT(&nes);
1921 nd->nd_repstat = nfsvno_accchk(vp,
1922 VWRITE, cred, &nes, p,
1923 NFSACCCHK_NOOVERRIDE,
1924 NFSACCCHK_VPISLOCKED, NULL);
1925 nd->nd_repstat = nfsrv_opencheck(clientid,
1926 stateidp, stp, vp, nd, p, nd->nd_repstat);
1927 if (!nd->nd_repstat) {
1928 tempsize = nvap->na_size;
1929 NFSVNO_ATTRINIT(nvap);
1930 nvap->na_size = tempsize;
1931 nd->nd_repstat = VOP_SETATTR(vp,
1932 &nvap->na_vattr, cred);
1934 } else if (vp->v_type == VREG) {
1935 nd->nd_repstat = nfsrv_opencheck(clientid,
1936 stateidp, stp, vp, nd, p, nd->nd_repstat);
1940 if (ndp->ni_cnd.cn_flags & HASBUF)
1941 nfsvno_relpathbuf(ndp);
1942 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1943 vrele(ndp->ni_startdir);
1944 if (ndp->ni_dvp == ndp->ni_vp)
1954 NFSEXITCODE2(0, nd);
1958 * Updates the file rev and sets the mtime and ctime
1959 * to the current clock time, returning the va_filerev and va_Xtime
1961 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1964 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1965 struct nfsrv_descript *nd, struct thread *p)
1970 vfs_timestamp(&va.va_mtime);
1971 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1972 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1973 if (VN_IS_DOOMED(vp))
1976 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1977 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1982 * Glue routine to nfsv4_fillattr().
1985 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1986 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1987 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1988 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1994 if (nfsrv_devidcnt > 0 &&
1995 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1996 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1997 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1998 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1999 error = nfsrv_pnfsstatfs(sf, mp);
2005 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
2006 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
2007 mounted_on_fileno, sf);
2009 NFSEXITCODE2(0, nd);
2013 /* Since the Readdir vnode ops vary, put the entire functions in here. */
2015 * nfs readdir service
2016 * - mallocs what it thinks is enough to read
2017 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
2018 * - calls VOP_READDIR()
2019 * - loops around building the reply
2020 * if the output generated exceeds count break out of loop
2021 * The NFSM_CLGET macro is used here so that the reply will be packed
2022 * tightly in mbuf clusters.
2023 * - it trims out records with d_fileno == 0
2024 * this doesn't matter for Unix clients, but they might confuse clients
2026 * - it trims out records with d_type == DT_WHT
2027 * these cannot be seen through NFS (unless we extend the protocol)
2028 * The alternate call nfsrvd_readdirplus() does lookups as well.
2029 * PS: The NFS protocol spec. does not clarify what the "count" byte
2030 * argument is a count of.. just name strings and file id's or the
2031 * entire reply rpc or ...
2032 * I tried just file name and id sizes and it confused the Sun client,
2033 * so I am using the full rpc size now. The "paranoia.." comment refers
2034 * to including the status longwords that are not a part of the dir.
2035 * "entry" structures, but are in the rpc.
2038 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2039 struct vnode *vp, struct nfsexstuff *exp)
2044 char *cpos, *cend, *rbuf;
2046 int nlen, error = 0, getret = 1;
2047 int siz, cnt, fullsiz, eofflag, ncookies;
2048 u_int64_t off, toff, verf __unused;
2049 u_long *cookies = NULL, *cookiep;
2053 struct thread *p = curthread;
2055 if (nd->nd_repstat) {
2056 nfsrv_postopattr(nd, getret, &at);
2059 if (nd->nd_flag & ND_NFSV2) {
2060 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2061 off = fxdr_unsigned(u_quad_t, *tl++);
2063 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2064 off = fxdr_hyper(tl);
2066 verf = fxdr_hyper(tl);
2070 cnt = fxdr_unsigned(int, *tl);
2071 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2072 cnt = NFS_SRVMAXDATA(nd);
2073 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2075 if (nd->nd_flag & ND_NFSV3) {
2076 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2080 * va_filerev is not sufficient as a cookie verifier,
2081 * since it is not supposed to change when entries are
2082 * removed/added unless that offset cookies returned to
2083 * the client are no longer valid.
2085 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2086 nd->nd_repstat = NFSERR_BAD_COOKIE;
2089 if (!nd->nd_repstat && vp->v_type != VDIR)
2090 nd->nd_repstat = NFSERR_NOTDIR;
2091 if (nd->nd_repstat == 0 && cnt == 0) {
2092 if (nd->nd_flag & ND_NFSV2)
2093 /* NFSv2 does not have NFSERR_TOOSMALL */
2094 nd->nd_repstat = EPERM;
2096 nd->nd_repstat = NFSERR_TOOSMALL;
2098 if (!nd->nd_repstat)
2099 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2100 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2101 NFSACCCHK_VPISLOCKED, NULL);
2102 if (nd->nd_repstat) {
2104 if (nd->nd_flag & ND_NFSV3)
2105 nfsrv_postopattr(nd, getret, &at);
2108 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2109 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2113 free(cookies, M_TEMP);
2121 io.uio_offset = (off_t)off;
2123 io.uio_segflg = UIO_SYSSPACE;
2124 io.uio_rw = UIO_READ;
2126 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2128 off = (u_int64_t)io.uio_offset;
2130 siz -= io.uio_resid;
2132 if (!cookies && !nd->nd_repstat)
2133 nd->nd_repstat = NFSERR_PERM;
2134 if (nd->nd_flag & ND_NFSV3) {
2135 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2136 if (!nd->nd_repstat)
2137 nd->nd_repstat = getret;
2141 * Handles the failed cases. nd->nd_repstat == 0 past here.
2143 if (nd->nd_repstat) {
2147 free(cookies, M_TEMP);
2148 if (nd->nd_flag & ND_NFSV3)
2149 nfsrv_postopattr(nd, getret, &at);
2153 * If nothing read, return eof
2158 if (nd->nd_flag & ND_NFSV2) {
2159 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2161 nfsrv_postopattr(nd, getret, &at);
2162 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2163 txdr_hyper(at.na_filerev, tl);
2166 *tl++ = newnfs_false;
2169 free(cookies, M_TEMP);
2174 * Check for degenerate cases of nothing useful read.
2175 * If so go try again
2179 dp = (struct dirent *)cpos;
2183 * For some reason FreeBSD's ufs_readdir() chooses to back the
2184 * directory offset up to a block boundary, so it is necessary to
2185 * skip over the records that precede the requested offset. This
2186 * requires the assumption that file offset cookies monotonically
2189 while (cpos < cend && ncookies > 0 &&
2190 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2191 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2192 cpos += dp->d_reclen;
2193 dp = (struct dirent *)cpos;
2197 if (cpos >= cend || ncookies == 0) {
2205 * If cnt > MCLBYTES and the reply will not be saved, use
2206 * ext_pgs mbufs for TLS.
2207 * For NFSv4.0, we do not know for sure if the reply will
2208 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2210 if (cnt > MCLBYTES && siz > MCLBYTES &&
2211 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2212 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2213 nd->nd_flag |= ND_EXTPG;
2216 * dirlen is the size of the reply, including all XDR and must
2217 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2218 * if the XDR should be included in "count", but to be safe, we do.
2219 * (Include the two booleans at the end of the reply in dirlen now.)
2221 if (nd->nd_flag & ND_NFSV3) {
2222 nfsrv_postopattr(nd, getret, &at);
2223 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2224 txdr_hyper(at.na_filerev, tl);
2225 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2227 dirlen = 2 * NFSX_UNSIGNED;
2230 /* Loop through the records and build reply */
2231 while (cpos < cend && ncookies > 0) {
2232 nlen = dp->d_namlen;
2233 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2234 nlen <= NFS_MAXNAMLEN) {
2235 if (nd->nd_flag & ND_NFSV3)
2236 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2238 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2245 * Build the directory record xdr from
2248 if (nd->nd_flag & ND_NFSV3) {
2249 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2250 *tl++ = newnfs_true;
2253 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2254 *tl++ = newnfs_true;
2256 *tl = txdr_unsigned(dp->d_fileno);
2257 (void) nfsm_strtom(nd, dp->d_name, nlen);
2258 if (nd->nd_flag & ND_NFSV3) {
2259 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2262 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2263 *tl = txdr_unsigned(*cookiep);
2265 cpos += dp->d_reclen;
2266 dp = (struct dirent *)cpos;
2272 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2273 *tl++ = newnfs_false;
2279 free(cookies, M_TEMP);
2282 NFSEXITCODE2(0, nd);
2286 NFSEXITCODE2(error, nd);
2291 * Readdirplus for V3 and Readdir for V4.
2294 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2295 struct vnode *vp, struct nfsexstuff *exp)
2300 char *cpos, *cend, *rbuf;
2303 struct nfsvattr nva, at, *nvap = &nva;
2304 struct mbuf *mb0, *mb1;
2305 struct nfsreferral *refp;
2306 int nlen, r, error = 0, getret = 1, usevget = 1;
2307 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2308 caddr_t bpos0, bpos1;
2309 u_int64_t off, toff, verf;
2310 u_long *cookies = NULL, *cookiep;
2311 nfsattrbit_t attrbits, rderrbits, savbits;
2314 struct componentname cn;
2315 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2316 struct mount *mp, *new_mp;
2317 uint64_t mounted_on_fileno;
2318 struct thread *p = curthread;
2319 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2321 if (nd->nd_repstat) {
2322 nfsrv_postopattr(nd, getret, &at);
2325 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2326 off = fxdr_hyper(tl);
2329 verf = fxdr_hyper(tl);
2331 siz = fxdr_unsigned(int, *tl++);
2332 cnt = fxdr_unsigned(int, *tl);
2335 * Use the server's maximum data transfer size as the upper bound
2338 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2339 cnt = NFS_SRVMAXDATA(nd);
2342 * siz is a "hint" of how much directory information (name, fileid,
2343 * cookie) should be in the reply. At least one client "hints" 0,
2344 * so I set it to cnt for that case. I also round it up to the
2345 * next multiple of DIRBLKSIZ.
2346 * Since the size of a Readdirplus directory entry reply will always
2347 * be greater than a directory entry returned by VOP_READDIR(), it
2348 * does not make sense to read more than NFS_SRVMAXDATA() via
2353 else if (siz > NFS_SRVMAXDATA(nd))
2354 siz = NFS_SRVMAXDATA(nd);
2355 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2357 if (nd->nd_flag & ND_NFSV4) {
2358 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2361 NFSSET_ATTRBIT(&savbits, &attrbits);
2362 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2363 NFSZERO_ATTRBIT(&rderrbits);
2364 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2366 NFSZERO_ATTRBIT(&attrbits);
2369 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2371 if (!nd->nd_repstat) {
2372 if (off && verf != at.na_filerev) {
2374 * va_filerev is not sufficient as a cookie verifier,
2375 * since it is not supposed to change when entries are
2376 * removed/added unless that offset cookies returned to
2377 * the client are no longer valid.
2379 if (nd->nd_flag & ND_NFSV4) {
2380 nd->nd_repstat = NFSERR_NOTSAME;
2382 nd->nd_repstat = NFSERR_BAD_COOKIE;
2387 if (!nd->nd_repstat && vp->v_type != VDIR)
2388 nd->nd_repstat = NFSERR_NOTDIR;
2389 if (!nd->nd_repstat && cnt == 0)
2390 nd->nd_repstat = NFSERR_TOOSMALL;
2391 if (!nd->nd_repstat)
2392 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2393 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2394 NFSACCCHK_VPISLOCKED, NULL);
2395 if (nd->nd_repstat) {
2397 if (nd->nd_flag & ND_NFSV3)
2398 nfsrv_postopattr(nd, getret, &at);
2401 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2402 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2404 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2408 free(cookies, M_TEMP);
2416 io.uio_offset = (off_t)off;
2418 io.uio_segflg = UIO_SYSSPACE;
2419 io.uio_rw = UIO_READ;
2421 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2423 off = (u_int64_t)io.uio_offset;
2425 siz -= io.uio_resid;
2427 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2429 if (!cookies && !nd->nd_repstat)
2430 nd->nd_repstat = NFSERR_PERM;
2431 if (!nd->nd_repstat)
2432 nd->nd_repstat = getret;
2433 if (nd->nd_repstat) {
2436 free(cookies, M_TEMP);
2438 if (nd->nd_flag & ND_NFSV3)
2439 nfsrv_postopattr(nd, getret, &at);
2443 * If nothing read, return eof
2448 if (nd->nd_flag & ND_NFSV3)
2449 nfsrv_postopattr(nd, getret, &at);
2450 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2451 txdr_hyper(at.na_filerev, tl);
2453 *tl++ = newnfs_false;
2455 free(cookies, M_TEMP);
2461 * Check for degenerate cases of nothing useful read.
2462 * If so go try again
2466 dp = (struct dirent *)cpos;
2470 * For some reason FreeBSD's ufs_readdir() chooses to back the
2471 * directory offset up to a block boundary, so it is necessary to
2472 * skip over the records that precede the requested offset. This
2473 * requires the assumption that file offset cookies monotonically
2476 while (cpos < cend && ncookies > 0 &&
2477 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2478 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2479 ((nd->nd_flag & ND_NFSV4) &&
2480 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2481 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2482 cpos += dp->d_reclen;
2483 dp = (struct dirent *)cpos;
2487 if (cpos >= cend || ncookies == 0) {
2494 * Busy the file system so that the mount point won't go away
2495 * and, as such, VFS_VGET() can be used safely.
2500 nd->nd_repstat = vfs_busy(mp, 0);
2502 if (nd->nd_repstat != 0) {
2504 free(cookies, M_TEMP);
2506 if (nd->nd_flag & ND_NFSV3)
2507 nfsrv_postopattr(nd, getret, &at);
2512 * Check to see if entries in this directory can be safely acquired
2513 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2514 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2515 * automount of the snapshot directory that is required will
2517 * This needs to be done here for NFSv4, since NFSv4 never does
2518 * a VFS_VGET() for "." or "..".
2521 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2522 if (r == EOPNOTSUPP) {
2524 cn.cn_nameiop = LOOKUP;
2525 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2526 cn.cn_cred = nd->nd_cred;
2533 * If the reply is likely to exceed MCLBYTES and the reply will
2534 * not be saved, use ext_pgs mbufs for TLS.
2535 * It is difficult to predict how large each entry will be and
2536 * how many entries have been read, so just assume the directory
2537 * entries grow by a factor of 4 when attributes are included.
2538 * For NFSv4.0, we do not know for sure if the reply will
2539 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2541 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2542 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2543 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2544 nd->nd_flag |= ND_EXTPG;
2547 * Save this position, in case there is an error before one entry
2551 bpos0 = nd->nd_bpos;
2552 bextpg0 = nd->nd_bextpg;
2553 bextpgsiz0 = nd->nd_bextpgsiz;
2556 * Fill in the first part of the reply.
2557 * dirlen is the reply length in bytes and cannot exceed cnt.
2558 * (Include the two booleans at the end of the reply in dirlen now,
2559 * so we recognize when we have exceeded cnt.)
2561 if (nd->nd_flag & ND_NFSV3) {
2562 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2563 nfsrv_postopattr(nd, getret, &at);
2565 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2567 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2568 txdr_hyper(at.na_filerev, tl);
2571 * Save this position, in case there is an empty reply needed.
2574 bpos1 = nd->nd_bpos;
2575 bextpg1 = nd->nd_bextpg;
2576 bextpgsiz1 = nd->nd_bextpgsiz;
2578 /* Loop through the records and build reply */
2580 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2581 nlen = dp->d_namlen;
2582 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2583 nlen <= NFS_MAXNAMLEN &&
2584 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2585 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2586 || (nlen == 1 && dp->d_name[0] != '.'))) {
2588 * Save the current position in the reply, in case
2589 * this entry exceeds cnt.
2592 bpos1 = nd->nd_bpos;
2593 bextpg1 = nd->nd_bextpg;
2594 bextpgsiz1 = nd->nd_bextpgsiz;
2597 * For readdir_and_lookup get the vnode using
2606 mounted_on_fileno = (uint64_t)dp->d_fileno;
2607 if ((nd->nd_flag & ND_NFSV3) ||
2608 NFSNONZERO_ATTRBIT(&savbits)) {
2609 if (nd->nd_flag & ND_NFSV4)
2610 refp = nfsv4root_getreferral(NULL,
2614 r = VFS_VGET(mp, dp->d_fileno,
2618 if (r == EOPNOTSUPP) {
2621 cn.cn_nameiop = LOOKUP;
2629 cn.cn_nameptr = dp->d_name;
2630 cn.cn_namelen = nlen;
2631 cn.cn_flags = ISLASTCN |
2632 NOFOLLOW | LOCKLEAF;
2634 dp->d_name[0] == '.' &&
2635 dp->d_name[1] == '.')
2638 if (NFSVOPLOCK(vp, LK_SHARED)
2640 nd->nd_repstat = EPERM;
2643 if ((vp->v_vflag & VV_ROOT) != 0
2644 && (cn.cn_flags & ISDOTDOT)
2650 r = VOP_LOOKUP(vp, &nvp,
2658 * For NFSv4, check to see if nvp is
2659 * a mount point and get the mount
2660 * point vnode, as required.
2663 nfsrv_enable_crossmntpt != 0 &&
2664 (nd->nd_flag & ND_NFSV4) != 0 &&
2665 nvp->v_type == VDIR &&
2666 nvp->v_mountedhere != NULL) {
2667 new_mp = nvp->v_mountedhere;
2668 r = vfs_busy(new_mp, 0);
2672 r = VFS_ROOT(new_mp,
2682 * If we failed to look up the entry, then it
2683 * has become invalid, most likely removed.
2690 KASSERT(refp != NULL || nvp != NULL,
2691 ("%s: undetected lookup error", __func__));
2694 ((nd->nd_flag & ND_NFSV3) ||
2695 NFSNONZERO_ATTRBIT(&attrbits))) {
2696 r = nfsvno_getfh(nvp, &nfh, p);
2698 r = nfsvno_getattr(nvp, nvap, nd, p,
2700 if (r == 0 && is_zfs == 1 &&
2701 nfsrv_enable_crossmntpt != 0 &&
2702 (nd->nd_flag & ND_NFSV4) != 0 &&
2703 nvp->v_type == VDIR &&
2704 vp->v_mount != nvp->v_mount) {
2706 * For a ZFS snapshot, there is a
2707 * pseudo mount that does not set
2708 * v_mountedhere, so it needs to
2709 * be detected via a different
2714 new_mp = nvp->v_mount;
2719 * If we failed to get attributes of the entry,
2720 * then just skip it for NFSv3 (the traditional
2721 * behavior in the old NFS server).
2722 * For NFSv4 the behavior is controlled by
2723 * RDATTRERROR: we either ignore the error or
2725 * Note that RDATTRERROR is never set for NFSv3.
2728 if (!NFSISSET_ATTRBIT(&attrbits,
2729 NFSATTRBIT_RDATTRERROR)) {
2731 if (needs_unbusy != 0)
2733 if ((nd->nd_flag & ND_NFSV3))
2742 * Build the directory record xdr
2744 if (nd->nd_flag & ND_NFSV3) {
2745 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2746 *tl++ = newnfs_true;
2748 *tl = txdr_unsigned(dp->d_fileno);
2749 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2750 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2752 *tl = txdr_unsigned(*cookiep);
2753 nfsrv_postopattr(nd, 0, nvap);
2754 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2755 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2759 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2760 *tl++ = newnfs_true;
2762 *tl = txdr_unsigned(*cookiep);
2763 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2765 supports_nfsv4acls =
2766 nfs_supportsnfsv4acls(nvp);
2769 supports_nfsv4acls = 0;
2771 dirlen += nfsrv_putreferralattr(nd,
2774 if (nd->nd_repstat) {
2777 if (needs_unbusy != 0)
2782 dirlen += nfsvno_fillattr(nd, new_mp,
2783 nvp, nvap, &nfh, r, &rderrbits,
2784 nd->nd_cred, p, isdgram, 0,
2785 supports_nfsv4acls, at_root,
2788 dirlen += nfsvno_fillattr(nd, new_mp,
2789 nvp, nvap, &nfh, r, &attrbits,
2790 nd->nd_cred, p, isdgram, 0,
2791 supports_nfsv4acls, at_root,
2796 dirlen += (3 * NFSX_UNSIGNED);
2798 if (needs_unbusy != 0)
2804 cpos += dp->d_reclen;
2805 dp = (struct dirent *)cpos;
2813 * If dirlen > cnt, we must strip off the last entry. If that
2814 * results in an empty reply, report NFSERR_TOOSMALL.
2816 if (dirlen > cnt || nd->nd_repstat) {
2817 if (!nd->nd_repstat && entrycnt == 0)
2818 nd->nd_repstat = NFSERR_TOOSMALL;
2819 if (nd->nd_repstat) {
2820 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2821 if (nd->nd_flag & ND_NFSV3)
2822 nfsrv_postopattr(nd, getret, &at);
2824 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2826 } else if (cpos < cend)
2828 if (!nd->nd_repstat) {
2829 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2830 *tl++ = newnfs_false;
2836 free(cookies, M_TEMP);
2840 NFSEXITCODE2(0, nd);
2844 NFSEXITCODE2(error, nd);
2849 * Get the settable attributes out of the mbuf list.
2850 * (Return 0 or EBADRPC)
2853 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2854 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2857 struct nfsv2_sattr *sp;
2858 int error = 0, toclient = 0;
2860 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2862 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2864 * Some old clients didn't fill in the high order 16bits.
2865 * --> check the low order 2 bytes for 0xffff
2867 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2868 nvap->na_mode = nfstov_mode(sp->sa_mode);
2869 if (sp->sa_uid != newnfs_xdrneg1)
2870 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2871 if (sp->sa_gid != newnfs_xdrneg1)
2872 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2873 if (sp->sa_size != newnfs_xdrneg1)
2874 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2875 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2877 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2879 nvap->na_atime.tv_sec =
2880 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2881 nvap->na_atime.tv_nsec = 0;
2884 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2885 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2888 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2889 if (*tl == newnfs_true) {
2890 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2891 nvap->na_mode = nfstov_mode(*tl);
2893 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2894 if (*tl == newnfs_true) {
2895 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2896 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2898 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2899 if (*tl == newnfs_true) {
2900 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2901 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2903 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2904 if (*tl == newnfs_true) {
2905 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2906 nvap->na_size = fxdr_hyper(tl);
2908 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2909 switch (fxdr_unsigned(int, *tl)) {
2910 case NFSV3SATTRTIME_TOCLIENT:
2911 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2912 fxdr_nfsv3time(tl, &nvap->na_atime);
2915 case NFSV3SATTRTIME_TOSERVER:
2916 vfs_timestamp(&nvap->na_atime);
2917 nvap->na_vaflags |= VA_UTIMES_NULL;
2920 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2921 switch (fxdr_unsigned(int, *tl)) {
2922 case NFSV3SATTRTIME_TOCLIENT:
2923 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2924 fxdr_nfsv3time(tl, &nvap->na_mtime);
2925 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2927 case NFSV3SATTRTIME_TOSERVER:
2928 vfs_timestamp(&nvap->na_mtime);
2930 nvap->na_vaflags |= VA_UTIMES_NULL;
2935 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2938 NFSEXITCODE2(error, nd);
2943 * Handle the setable attributes for V4.
2944 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2947 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2948 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2953 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2954 int moderet, toclient = 0;
2955 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2958 u_short mode, mask; /* Same type as va_mode. */
2961 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2964 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2965 attrsize = fxdr_unsigned(int, *tl);
2968 * Loop around getting the setable attributes. If an unsupported
2969 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2972 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2973 bitpos = NFSATTRBIT_MAX;
2978 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2979 if (attrsum > attrsize) {
2980 error = NFSERR_BADXDR;
2983 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2985 case NFSATTRBIT_SIZE:
2986 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2987 if (vp != NULL && vp->v_type != VREG) {
2988 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2992 nvap->na_size = fxdr_hyper(tl);
2993 attrsum += NFSX_HYPER;
2995 case NFSATTRBIT_ACL:
2996 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
3000 if (aceerr && !nd->nd_repstat)
3001 nd->nd_repstat = aceerr;
3004 case NFSATTRBIT_ARCHIVE:
3005 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3006 if (!nd->nd_repstat)
3007 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3008 attrsum += NFSX_UNSIGNED;
3010 case NFSATTRBIT_HIDDEN:
3011 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3012 if (!nd->nd_repstat)
3013 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3014 attrsum += NFSX_UNSIGNED;
3016 case NFSATTRBIT_MIMETYPE:
3017 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3018 i = fxdr_unsigned(int, *tl);
3019 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3022 if (!nd->nd_repstat)
3023 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3024 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3026 case NFSATTRBIT_MODE:
3027 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
3028 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3029 nvap->na_mode = nfstov_mode(*tl);
3030 attrsum += NFSX_UNSIGNED;
3032 case NFSATTRBIT_OWNER:
3033 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3034 j = fxdr_unsigned(int, *tl);
3036 error = NFSERR_BADXDR;
3039 if (j > NFSV4_SMALLSTR)
3040 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3043 error = nfsrv_mtostr(nd, cp, j);
3045 if (j > NFSV4_SMALLSTR)
3046 free(cp, M_NFSSTRING);
3049 if (!nd->nd_repstat) {
3050 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3052 if (!nd->nd_repstat)
3055 if (j > NFSV4_SMALLSTR)
3056 free(cp, M_NFSSTRING);
3057 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3059 case NFSATTRBIT_OWNERGROUP:
3060 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3061 j = fxdr_unsigned(int, *tl);
3063 error = NFSERR_BADXDR;
3066 if (j > NFSV4_SMALLSTR)
3067 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3070 error = nfsrv_mtostr(nd, cp, j);
3072 if (j > NFSV4_SMALLSTR)
3073 free(cp, M_NFSSTRING);
3076 if (!nd->nd_repstat) {
3077 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3079 if (!nd->nd_repstat)
3082 if (j > NFSV4_SMALLSTR)
3083 free(cp, M_NFSSTRING);
3084 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3086 case NFSATTRBIT_SYSTEM:
3087 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3088 if (!nd->nd_repstat)
3089 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3090 attrsum += NFSX_UNSIGNED;
3092 case NFSATTRBIT_TIMEACCESSSET:
3093 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3094 attrsum += NFSX_UNSIGNED;
3095 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3096 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3097 fxdr_nfsv4time(tl, &nvap->na_atime);
3099 attrsum += NFSX_V4TIME;
3101 vfs_timestamp(&nvap->na_atime);
3102 nvap->na_vaflags |= VA_UTIMES_NULL;
3105 case NFSATTRBIT_TIMEBACKUP:
3106 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3107 if (!nd->nd_repstat)
3108 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3109 attrsum += NFSX_V4TIME;
3111 case NFSATTRBIT_TIMECREATE:
3112 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3113 fxdr_nfsv4time(tl, &nvap->na_btime);
3114 attrsum += NFSX_V4TIME;
3116 case NFSATTRBIT_TIMEMODIFYSET:
3117 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3118 attrsum += NFSX_UNSIGNED;
3119 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3120 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3121 fxdr_nfsv4time(tl, &nvap->na_mtime);
3122 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3123 attrsum += NFSX_V4TIME;
3125 vfs_timestamp(&nvap->na_mtime);
3127 nvap->na_vaflags |= VA_UTIMES_NULL;
3130 case NFSATTRBIT_MODESETMASKED:
3131 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3132 mode = fxdr_unsigned(u_short, *tl++);
3133 mask = fxdr_unsigned(u_short, *tl);
3135 * vp == NULL implies an Open/Create operation.
3136 * This attribute can only be used for Setattr and
3137 * only for NFSv4.1 or higher.
3138 * If moderet != 0, a mode attribute has also been
3139 * specified and this attribute cannot be done in the
3140 * same Setattr operation.
3142 if ((nd->nd_flag & ND_NFSV41) == 0)
3143 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3144 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3146 nd->nd_repstat = NFSERR_INVAL;
3147 else if (moderet == 0)
3148 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3150 nvap->na_mode = (mode & mask) |
3151 (va.va_mode & ~mask);
3153 nd->nd_repstat = moderet;
3154 attrsum += 2 * NFSX_UNSIGNED;
3157 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3159 * set bitpos so we drop out of the loop.
3161 bitpos = NFSATTRBIT_MAX;
3167 * some clients pad the attrlist, so we need to skip over the
3170 if (attrsum > attrsize) {
3171 error = NFSERR_BADXDR;
3173 attrsize = NFSM_RNDUP(attrsize);
3174 if (attrsum < attrsize)
3175 error = nfsm_advance(nd, attrsize - attrsum, -1);
3178 NFSEXITCODE2(error, nd);
3183 * Check/setup export credentials.
3186 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3187 struct ucred *credanon, bool testsec)
3192 * Check/setup credentials.
3194 if (nd->nd_flag & ND_GSS)
3195 exp->nes_exflag &= ~MNT_EXPORTANON;
3198 * Check to see if the operation is allowed for this security flavor.
3202 error = nfsvno_testexp(nd, exp);
3208 * Check to see if the file system is exported V4 only.
3210 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3211 error = NFSERR_PROGNOTV4;
3216 * Now, map the user credentials.
3217 * (Note that ND_AUTHNONE will only be set for an NFSv3
3218 * Fsinfo RPC. If set for anything else, this code might need
3221 if (NFSVNO_EXPORTED(exp)) {
3222 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3223 NFSVNO_EXPORTANON(exp) ||
3224 (nd->nd_flag & ND_AUTHNONE) != 0) {
3225 nd->nd_cred->cr_uid = credanon->cr_uid;
3226 nd->nd_cred->cr_gid = credanon->cr_gid;
3227 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3228 credanon->cr_groups);
3229 } else if ((nd->nd_flag & ND_GSS) == 0) {
3231 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3232 * if there is a replacement credential with a group
3233 * list set up by "nfsuserd -manage-gids".
3234 * If there is no replacement, nfsrv_getgrpscred()
3235 * simply returns its argument.
3237 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3242 NFSEXITCODE2(error, nd);
3250 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3251 struct ucred **credp)
3255 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3256 &exp->nes_numsecflavor, exp->nes_secflavors);
3258 if (nfs_rootfhset) {
3259 exp->nes_exflag = 0;
3260 exp->nes_numsecflavor = 0;
3263 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3265 printf("nfsvno_checkexp: numsecflavors out of range\n");
3266 exp->nes_numsecflavor = 0;
3274 * Get a vnode for a file handle and export stuff.
3277 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3278 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3279 struct ucred **credp)
3284 exp->nes_numsecflavor = 0;
3285 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3287 /* Make sure the server replies ESTALE to the client. */
3289 if (nam && !error) {
3290 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3291 &exp->nes_numsecflavor, exp->nes_secflavors);
3293 if (nfs_rootfhset) {
3294 exp->nes_exflag = 0;
3295 exp->nes_numsecflavor = 0;
3300 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3302 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3303 exp->nes_numsecflavor = 0;
3313 * nfsd_fhtovp() - convert a fh to a vnode ptr
3314 * - look up fsid in mount list (if not found ret error)
3315 * - get vp and export rights by calling nfsvno_fhtovp()
3316 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3318 * - if mpp != NULL, return the mount point so that it can
3319 * be used for vn_finished_write() by the caller
3322 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3323 struct vnode **vpp, struct nfsexstuff *exp,
3324 struct mount **mpp, int startwrite, int nextop)
3326 struct mount *mp, *mpw;
3327 struct ucred *credanon;
3334 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3335 mp = vfs_busyfs(&fhp->fh_fsid);
3337 nd->nd_repstat = ESTALE;
3343 error = vn_start_write(NULL, &mpw, V_WAIT);
3347 nd->nd_repstat = ESTALE;
3350 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3351 lktype = LK_EXCLUSIVE;
3355 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3360 * For NFSv4 without a pseudo root fs, unexported file handles
3361 * can be returned, so that Lookup works everywhere.
3363 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3364 !(nd->nd_flag & ND_NFSV4)) {
3367 nd->nd_repstat = EACCES;
3371 * Personally, I've never seen any point in requiring a
3372 * reserved port#, since only in the rare case where the
3373 * clients are all boxes with secure system privileges,
3374 * does it provide any enhanced security, but... some people
3375 * believe it to be useful and keep putting this code back in.
3376 * (There is also some "security checker" out there that
3377 * complains if the nfs server doesn't enforce this.)
3378 * However, note the following:
3379 * RFC3530 (NFSv4) specifies that a reserved port# not be
3381 * RFC2623 recommends that, if a reserved port# is checked for,
3382 * that there be a way to turn that off--> ifdef'd.
3384 #ifdef NFS_REQRSVPORT
3385 if (!nd->nd_repstat) {
3386 struct sockaddr_in *saddr;
3387 struct sockaddr_in6 *saddr6;
3389 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3390 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3391 if (!(nd->nd_flag & ND_NFSV4) &&
3392 ((saddr->sin_family == AF_INET &&
3393 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3394 (saddr6->sin6_family == AF_INET6 &&
3395 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3397 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3400 #endif /* NFS_REQRSVPORT */
3403 * Check/setup credentials.
3405 if (!nd->nd_repstat) {
3406 nd->nd_saveduid = nd->nd_cred->cr_uid;
3407 nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3408 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3412 if (credanon != NULL)
3414 if (nd->nd_repstat) {
3415 vn_finished_write(mpw);
3417 } else if (mpp != NULL) {
3422 NFSEXITCODE2(0, nd);
3429 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3431 struct filedesc *fdp;
3435 fdp = p->td_proc->p_fd;
3436 if (fd < 0 || fd >= fdp->fd_nfiles ||
3437 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3449 * Called from nfssvc() to update the exports list. Just call
3450 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3451 * in the mount list.
3454 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3456 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3458 struct nameidata nd;
3461 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3462 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3464 else if (error == 0) {
3465 if (nfsexargp->fspec == NULL) {
3470 * If fspec != NULL, this is the v4root path.
3472 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3473 nfsexargp->fspec, p);
3474 if ((error = namei(&nd)) != 0)
3476 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3479 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3480 NFSBCOPY((caddr_t)&fh,
3481 nfs_rootfh.nfsrvfh_data,
3482 sizeof (fhandle_t));
3493 * This function needs to test to see if the system is near its limit
3494 * for memory allocation via malloc() or mget() and return True iff
3495 * either of these resources are near their limit.
3496 * XXX (For now, this is just a stub.)
3498 int nfsrv_testmalloclimit = 0;
3500 nfsrv_mallocmget_limit(void)
3502 static int printmesg = 0;
3503 static int testval = 1;
3505 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3506 if ((printmesg++ % 100) == 0)
3507 printf("nfsd: malloc/mget near limit\n");
3514 * BSD specific initialization of a mount point.
3519 static int inited = 0;
3524 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3525 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3526 TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3527 nfsv4root_mnt.mnt_export = NULL;
3528 TAILQ_INIT(&nfsv4root_opt);
3529 TAILQ_INIT(&nfsv4root_newopt);
3530 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3531 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3532 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3533 nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3537 * Get a vnode for a file handle, without checking exports, etc.
3540 nfsvno_getvp(fhandle_t *fhp)
3546 mp = vfs_busyfs(&fhp->fh_fsid);
3549 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3557 * Do a local VOP_ADVLOCK().
3560 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3561 u_int64_t end, struct thread *td)
3567 if (nfsrv_dolocallocks == 0)
3569 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3571 fl.l_whence = SEEK_SET;
3573 fl.l_start = (off_t)first;
3574 if (end == NFS64BITSSET) {
3578 fl.l_len = (off_t)tlen;
3581 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3582 * values for all calls, so that all locks will be held by the
3583 * nfsd server. (The nfsd server handles conflicts between the
3585 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3586 * bytes, so it can't be put in l_sysid.
3588 if (nfsv4_sysid == 0)
3589 nfsv4_sysid = nlm_acquire_next_sysid();
3590 fl.l_pid = (pid_t)0;
3591 fl.l_sysid = (int)nfsv4_sysid;
3593 if (ftype == F_UNLCK)
3594 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3595 (F_POSIX | F_REMOTE));
3597 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3598 (F_POSIX | F_REMOTE));
3606 * Check the nfsv4 root exports.
3609 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3611 struct ucred *credanon;
3612 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3615 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3616 &credanon, &numsecflavor, secflavors);
3618 error = NFSERR_PROGUNAVAIL;
3621 if (credanon != NULL)
3623 for (i = 0; i < numsecflavor; i++) {
3624 if (secflavors[i] == AUTH_SYS)
3625 nd->nd_flag |= ND_EXAUTHSYS;
3626 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3627 nd->nd_flag |= ND_EXGSS;
3628 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3629 nd->nd_flag |= ND_EXGSSINTEGRITY;
3630 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3631 nd->nd_flag |= ND_EXGSSPRIVACY;
3634 /* And set ND_EXxx flags for TLS. */
3635 if ((exflags & MNT_EXTLS) != 0) {
3636 nd->nd_flag |= ND_EXTLS;
3637 if ((exflags & MNT_EXTLSCERT) != 0)
3638 nd->nd_flag |= ND_EXTLSCERT;
3639 if ((exflags & MNT_EXTLSCERTUSER) != 0)
3640 nd->nd_flag |= ND_EXTLSCERTUSER;
3649 * Nfs server pseudo system call for the nfsd's
3655 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3658 struct nfsd_addsock_args sockarg;
3659 struct nfsd_nfsd_args nfsdarg;
3660 struct nfsd_nfsd_oargs onfsdarg;
3661 struct nfsd_pnfsd_args pnfsdarg;
3662 struct vnode *vp, *nvp, *curdvp;
3663 struct pnfsdsfile *pf;
3664 struct nfsdevice *ds, *fds;
3665 cap_rights_t rights;
3666 int buflen, error, ret;
3667 char *buf, *cp, *cp2, *cp3;
3668 char fname[PNFS_FILENAME_LEN + 1];
3670 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3671 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3675 * Since we don't know what rights might be required,
3676 * pretend that we need them all. It is better to be too
3677 * careful than too reckless.
3679 error = fget(td, sockarg.sock,
3680 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
3683 if (fp->f_type != DTYPE_SOCKET) {
3688 error = nfsrvd_addsock(fp);
3690 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3691 if (uap->argp == NULL) {
3695 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3696 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3698 nfsdarg.principal = onfsdarg.principal;
3699 nfsdarg.minthreads = onfsdarg.minthreads;
3700 nfsdarg.maxthreads = onfsdarg.maxthreads;
3701 nfsdarg.version = 1;
3702 nfsdarg.addr = NULL;
3703 nfsdarg.addrlen = 0;
3704 nfsdarg.dnshost = NULL;
3705 nfsdarg.dnshostlen = 0;
3706 nfsdarg.dspath = NULL;
3707 nfsdarg.dspathlen = 0;
3708 nfsdarg.mdspath = NULL;
3709 nfsdarg.mdspathlen = 0;
3710 nfsdarg.mirrorcnt = 1;
3713 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3716 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3717 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3718 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3719 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3720 nfsdarg.mirrorcnt >= 1 &&
3721 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3722 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3723 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3724 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3725 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3726 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3727 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3728 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3729 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3734 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3736 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3737 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3739 free(nfsdarg.addr, M_TEMP);
3743 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3744 nfsdarg.dnshost = cp;
3745 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3746 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3748 free(nfsdarg.addr, M_TEMP);
3749 free(nfsdarg.dnshost, M_TEMP);
3753 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3754 nfsdarg.dspath = cp;
3755 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3756 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3758 free(nfsdarg.addr, M_TEMP);
3759 free(nfsdarg.dnshost, M_TEMP);
3760 free(nfsdarg.dspath, M_TEMP);
3764 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3765 nfsdarg.mdspath = cp;
3767 nfsdarg.addr = NULL;
3768 nfsdarg.addrlen = 0;
3769 nfsdarg.dnshost = NULL;
3770 nfsdarg.dnshostlen = 0;
3771 nfsdarg.dspath = NULL;
3772 nfsdarg.dspathlen = 0;
3773 nfsdarg.mdspath = NULL;
3774 nfsdarg.mdspathlen = 0;
3775 nfsdarg.mirrorcnt = 1;
3777 error = nfsrvd_nfsd(td, &nfsdarg);
3778 free(nfsdarg.addr, M_TEMP);
3779 free(nfsdarg.dnshost, M_TEMP);
3780 free(nfsdarg.dspath, M_TEMP);
3781 free(nfsdarg.mdspath, M_TEMP);
3782 } else if (uap->flag & NFSSVC_PNFSDS) {
3783 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3784 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3785 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3786 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3787 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3790 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3792 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3793 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3794 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3795 buf = malloc(buflen, M_TEMP, M_WAITOK);
3796 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3798 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3799 if (error == 0 && pnfsdarg.dspath != NULL) {
3800 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3801 error = copyinstr(pnfsdarg.dspath, cp2,
3802 PATH_MAX + 1, NULL);
3803 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3807 if (error == 0 && pnfsdarg.curdspath != NULL) {
3808 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3809 error = copyinstr(pnfsdarg.curdspath, cp3,
3810 PATH_MAX + 1, NULL);
3811 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3818 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3819 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3821 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3823 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3824 printf("copymr: dsdir out of range\n");
3827 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3828 error = nfsrv_copymr(vp, nvp,
3829 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3830 (struct pnfsdsfile *)buf,
3831 buflen / sizeof(*pf), td->td_ucred, td);
3834 if (fds != NULL && error == 0) {
3835 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3836 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3838 nfsrv_dsremove(curdvp, fname,
3840 NFSVOPUNLOCK(curdvp);
3843 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3851 error = nfssvc_srvcall(td, uap, td->td_ucred);
3860 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3862 struct nfsex_args export;
3863 struct nfsex_oldargs oexp;
3864 struct file *fp = NULL;
3865 int stablefd, i, len;
3866 struct nfsd_clid adminrevoke;
3867 struct nfsd_dumplist dumplist;
3868 struct nfsd_dumpclients *dumpclients;
3869 struct nfsd_dumplocklist dumplocklist;
3870 struct nfsd_dumplocks *dumplocks;
3871 struct nameidata nd;
3873 int error = EINVAL, igotlock;
3876 static int suspend_nfsd = 0;
3878 if (uap->flag & NFSSVC_PUBLICFH) {
3879 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3880 sizeof (fhandle_t));
3881 error = copyin(uap->argp,
3882 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3885 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3886 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3887 error = copyin(uap->argp,(caddr_t)&export,
3888 sizeof (struct nfsex_args));
3891 if (export.export.ex_ngroups > NGROUPS_MAX ||
3892 export.export.ex_ngroups < 0)
3894 else if (export.export.ex_ngroups > 0) {
3895 grps = malloc(export.export.ex_ngroups *
3896 sizeof(gid_t), M_TEMP, M_WAITOK);
3897 error = copyin(export.export.ex_groups, grps,
3898 export.export.ex_ngroups * sizeof(gid_t));
3899 export.export.ex_groups = grps;
3901 export.export.ex_groups = NULL;
3903 error = nfsrv_v4rootexport(&export, cred, p);
3906 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3907 NFSSVC_V4ROOTEXPORT) {
3908 error = copyin(uap->argp,(caddr_t)&oexp,
3909 sizeof (struct nfsex_oldargs));
3911 memset(&export.export, 0, sizeof(export.export));
3912 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3913 export.export.ex_root = oexp.export.ex_root;
3914 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3915 export.export.ex_ngroups =
3916 oexp.export.ex_anon.cr_ngroups;
3917 export.export.ex_groups = NULL;
3918 if (export.export.ex_ngroups > XU_NGROUPS ||
3919 export.export.ex_ngroups < 0)
3921 else if (export.export.ex_ngroups > 0) {
3922 export.export.ex_groups = malloc(
3923 export.export.ex_ngroups * sizeof(gid_t),
3925 for (i = 0; i < export.export.ex_ngroups; i++)
3926 export.export.ex_groups[i] =
3927 oexp.export.ex_anon.cr_groups[i];
3929 export.export.ex_addr = oexp.export.ex_addr;
3930 export.export.ex_addrlen = oexp.export.ex_addrlen;
3931 export.export.ex_mask = oexp.export.ex_mask;
3932 export.export.ex_masklen = oexp.export.ex_masklen;
3933 export.export.ex_indexfile = oexp.export.ex_indexfile;
3934 export.export.ex_numsecflavors =
3935 oexp.export.ex_numsecflavors;
3936 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3937 export.export.ex_numsecflavors < 0)
3940 for (i = 0; i < export.export.ex_numsecflavors;
3942 export.export.ex_secflavors[i] =
3943 oexp.export.ex_secflavors[i];
3945 export.fspec = oexp.fspec;
3947 error = nfsrv_v4rootexport(&export, cred, p);
3948 free(export.export.ex_groups, M_TEMP);
3950 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3953 } else if (uap->flag & NFSSVC_STABLERESTART) {
3954 error = copyin(uap->argp, (caddr_t)&stablefd,
3957 error = fp_getfvp(p, stablefd, &fp, &vp);
3958 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3960 if (!error && newnfs_numnfsd != 0)
3963 nfsrv_stablefirst.nsf_fp = fp;
3964 nfsrv_setupstable(p);
3966 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3967 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3968 sizeof (struct nfsd_clid));
3970 error = nfsrv_adminrevoke(&adminrevoke, p);
3971 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3972 error = copyin(uap->argp, (caddr_t)&dumplist,
3973 sizeof (struct nfsd_dumplist));
3974 if (!error && (dumplist.ndl_size < 1 ||
3975 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3978 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3979 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3980 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3981 error = copyout(dumpclients, dumplist.ndl_list, len);
3982 free(dumpclients, M_TEMP);
3984 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3985 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3986 sizeof (struct nfsd_dumplocklist));
3987 if (!error && (dumplocklist.ndllck_size < 1 ||
3988 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3991 error = nfsrv_lookupfilename(&nd,
3992 dumplocklist.ndllck_fname, p);
3994 len = sizeof (struct nfsd_dumplocks) *
3995 dumplocklist.ndllck_size;
3996 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3997 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3998 dumplocklist.ndllck_size, p);
4000 error = copyout(dumplocks, dumplocklist.ndllck_list,
4002 free(dumplocks, M_TEMP);
4004 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
4007 nfsd_master_pid = procp->p_pid;
4008 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
4009 nfsd_master_start = procp->p_stats->p_start;
4010 nfsd_master_proc = procp;
4012 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4013 NFSLOCKV4ROOTMUTEX();
4014 if (suspend_nfsd == 0) {
4015 /* Lock out all nfsd threads */
4017 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
4018 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
4019 } while (igotlock == 0 && suspend_nfsd == 0);
4022 NFSUNLOCKV4ROOTMUTEX();
4024 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4025 NFSLOCKV4ROOTMUTEX();
4026 if (suspend_nfsd != 0) {
4027 nfsv4_unlock(&nfsd_suspend_lock, 0);
4030 NFSUNLOCKV4ROOTMUTEX();
4040 * Returns 0 if ok, 1 otherwise.
4043 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4048 * Allow NFSv3 Fsinfo per RFC2623.
4050 if (((nd->nd_flag & ND_NFSV4) != 0 ||
4051 nd->nd_procnum != NFSPROC_FSINFO) &&
4052 ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4053 (NFSVNO_EXTLSCERT(exp) &&
4054 (nd->nd_flag & ND_TLSCERT) == 0) ||
4055 (NFSVNO_EXTLSCERTUSER(exp) &&
4056 (nd->nd_flag & ND_TLSCERTUSER) == 0))) {
4057 if ((nd->nd_flag & ND_NFSV4) != 0)
4058 return (NFSERR_WRONGSEC);
4059 else if ((nd->nd_flag & ND_TLS) == 0)
4060 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4062 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4066 * This seems odd, but allow the case where the security flavor
4067 * list is empty. This happens when NFSv4 is traversing non-exported
4068 * file systems. Exported file systems should always have a non-empty
4069 * security flavor list.
4071 if (exp->nes_numsecflavor == 0)
4074 for (i = 0; i < exp->nes_numsecflavor; i++) {
4076 * The tests for privacy and integrity must be first,
4077 * since ND_GSS is set for everything but AUTH_SYS.
4079 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4080 (nd->nd_flag & ND_GSSPRIVACY))
4082 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4083 (nd->nd_flag & ND_GSSINTEGRITY))
4085 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4086 (nd->nd_flag & ND_GSS))
4088 if (exp->nes_secflavors[i] == AUTH_SYS &&
4089 (nd->nd_flag & ND_GSS) == 0)
4092 if ((nd->nd_flag & ND_NFSV4) != 0)
4093 return (NFSERR_WRONGSEC);
4094 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4098 * Calculate a hash value for the fid in a file handle.
4101 nfsrv_hashfh(fhandle_t *fhp)
4105 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4110 * Calculate a hash value for the sessionid.
4113 nfsrv_hashsessionid(uint8_t *sessionid)
4117 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4122 * Signal the userland master nfsd to backup the stable restart file.
4125 nfsrv_backupstable(void)
4129 if (nfsd_master_proc != NULL) {
4130 procp = pfind(nfsd_master_pid);
4131 /* Try to make sure it is the correct process. */
4132 if (procp == nfsd_master_proc &&
4133 procp->p_stats->p_start.tv_sec ==
4134 nfsd_master_start.tv_sec &&
4135 procp->p_stats->p_start.tv_usec ==
4136 nfsd_master_start.tv_usec &&
4137 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4138 kern_psignal(procp, SIGUSR2);
4140 nfsd_master_proc = NULL;
4148 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4149 * The arguments are in a structure, so that they can be passed through
4150 * taskqueue for a kernel process to execute this function.
4152 struct nfsrvdscreate {
4156 struct ucred *tcred;
4159 struct pnfsdsfile *pf;
4163 struct vattr createva;
4167 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4168 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4169 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4172 struct nameidata named;
4177 struct nfsmount *nmp;
4180 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4181 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
4182 nfsvno_setpathbuf(&named, &bufp, &hashp);
4183 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4184 named.ni_cnd.cn_thread = p;
4185 named.ni_cnd.cn_nameptr = bufp;
4186 if (fnamep != NULL) {
4187 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4188 named.ni_cnd.cn_namelen = strlen(bufp);
4190 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4191 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4193 /* Create the date file in the DS mount. */
4194 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4196 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4198 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4200 /* Set the ownership of the file. */
4201 error = VOP_SETATTR(nvp, nvap, tcred);
4202 NFSD_DEBUG(4, "nfsrv_dscreate:"
4203 " setattr-uid=%d\n", error);
4208 printf("pNFS: pnfscreate failed=%d\n", error);
4210 printf("pNFS: pnfscreate vnlock=%d\n", error);
4213 nmp = VFSTONFS(nvp->v_mount);
4214 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4215 != 0 || nmp->nm_nam->sa_len > sizeof(
4216 struct sockaddr_in6) ||
4217 np->n_fhp->nfh_len != NFSX_MYFH) {
4218 printf("Bad DS file: fstype=%s salen=%d"
4220 nvp->v_mount->mnt_vfc->vfc_name,
4221 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4225 /* Set extattrs for the DS on the MDS file. */
4228 error = VOP_GETATTR(nvp, &va, tcred);
4230 dsa->dsa_filerev = va.va_filerev;
4231 dsa->dsa_size = va.va_size;
4232 dsa->dsa_atime = va.va_atime;
4233 dsa->dsa_mtime = va.va_mtime;
4234 dsa->dsa_bytes = va.va_bytes;
4238 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4240 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4241 nmp->nm_nam->sa_len);
4242 NFSBCOPY(named.ni_cnd.cn_nameptr,
4244 sizeof(pf->dsf_filename));
4247 printf("pNFS: pnfscreate can't get DS"
4248 " attr=%d\n", error);
4249 if (nvpp != NULL && error == 0)
4254 nfsvno_relpathbuf(&named);
4259 * Start up the thread that will execute nfsrv_dscreate().
4262 start_dscreate(void *arg, int pending)
4264 struct nfsrvdscreate *dsc;
4266 dsc = (struct nfsrvdscreate *)arg;
4267 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4268 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4270 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4274 * Create a pNFS data file on the Data Server(s).
4277 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4280 struct nfsrvdscreate *dsc, *tdsc = NULL;
4281 struct nfsdevice *ds, *tds, *fds;
4283 struct pnfsdsfile *pf, *tpf;
4284 struct pnfsdsattr dsattr;
4286 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4287 struct nfsmount *nmp;
4292 struct ucred *tcred;
4293 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4296 /* Get a DS server directory in a round-robin order. */
4302 * Search for the first entry that handles this MDS fs, but use the
4303 * first entry for all MDS fs's otherwise.
4305 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4306 if (tds->nfsdev_nmp != NULL) {
4307 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4309 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4310 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4318 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4321 i = dsdir[0] = ds->nfsdev_nextdir;
4322 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4323 dvp[0] = ds->nfsdev_dsdir[i];
4324 tds = TAILQ_NEXT(ds, nfsdev_list);
4325 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4326 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4327 if (tds->nfsdev_nmp != NULL &&
4328 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4329 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4330 fsidcmp(&mp->mnt_stat.f_fsid,
4331 &tds->nfsdev_mdsfsid) == 0))) {
4332 dsdir[mirrorcnt] = i;
4333 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4335 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4340 /* Put at end of list to implement round-robin usage. */
4341 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4342 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4346 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4348 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4351 error = nfsvno_getfh(vp, &fh, p);
4353 error = VOP_GETATTR(vp, &va, cred);
4355 /* Set the attributes for "vp" to Setattr the DS vp. */
4358 vamode = va.va_mode;
4362 va.va_mode = vamode;
4365 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4367 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4369 /* Make data file name based on FH. */
4370 tcred = newnfs_getcred();
4373 * Create the file on each DS mirror, using kernel process(es) for the
4374 * additional mirrors.
4377 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4378 tpf->dsf_dir = dsdir[i];
4379 tdsc->tcred = tcred;
4382 tdsc->createva = *vap;
4383 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4390 if (nfs_pnfsiothreads != 0) {
4391 ret = nfs_pnfsio(start_dscreate, tdsc);
4392 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4395 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4396 NULL, tcred, p, NULL);
4398 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4400 if (failpos == -1 && nfsds_failerr(ret))
4408 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4409 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4410 &dsattr, NULL, tcred, p, NULL);
4411 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4412 failpos = mirrorcnt - 1;
4416 timo = hz / 50; /* Wait for 20msec. */
4419 /* Wait for kernel task(s) to complete. */
4420 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4421 while (tdsc->inprog != 0 && tdsc->done == 0)
4422 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4423 if (tdsc->err != 0) {
4424 if (failpos == -1 && nfsds_failerr(tdsc->err))
4426 else if (error == 0)
4432 * If failpos has been set, that mirror has failed, so it needs
4436 nmp = VFSTONFS(dvp[failpos]->v_mount);
4438 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4439 NFSMNTP_CANCELRPCS)) == 0) {
4440 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4442 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4443 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4446 nfsrv_killrpcs(nmp);
4448 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4456 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4458 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4459 mirrorcnt, nfsrv_maxpnfsmirror);
4461 * For all mirrors that couldn't be created, fill in the
4462 * *pf structure, but with an IP address == 0.0.0.0.
4464 tpf = pf + mirrorcnt;
4465 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4467 tpf->dsf_sin.sin_family = AF_INET;
4468 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4469 tpf->dsf_sin.sin_addr.s_addr = 0;
4470 tpf->dsf_sin.sin_port = 0;
4473 error = vn_extattr_set(vp, IO_NODELOCKED,
4474 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4475 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4477 error = vn_extattr_set(vp, IO_NODELOCKED,
4478 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4479 sizeof(dsattr), (char *)&dsattr, p);
4481 printf("pNFS: pnfscreate setextattr=%d\n",
4484 printf("pNFS: pnfscreate=%d\n", error);
4490 * Get the information needed to remove the pNFS Data Server file from the
4491 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4492 * DS directory vnode. The caller must unlock *ddvp when done with it.
4495 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4496 int *mirrorcntp, char *fname, fhandle_t *fhp)
4499 struct ucred *tcred;
4504 /* If not an exported regular file or not a pNFS server, just return. */
4505 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4506 nfsrv_devidcnt == 0)
4509 /* Check to see if this is the last hard link. */
4510 tcred = newnfs_getcred();
4511 error = VOP_GETATTR(vp, &va, tcred);
4514 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4517 if (va.va_nlink > 1)
4520 error = nfsvno_getfh(vp, fhp, p);
4522 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4527 buf = malloc(buflen, M_TEMP, M_WAITOK);
4528 /* Get the directory vnode for the DS mount and the file handle. */
4529 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4530 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4533 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4537 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4538 * The arguments are in a structure, so that they can be passed through
4539 * taskqueue for a kernel process to execute this function.
4541 struct nfsrvdsremove {
4545 struct ucred *tcred;
4549 char fname[PNFS_FILENAME_LEN + 1];
4553 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4556 struct nameidata named;
4562 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4565 named.ni_cnd.cn_nameiop = DELETE;
4566 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4567 named.ni_cnd.cn_cred = tcred;
4568 named.ni_cnd.cn_thread = p;
4569 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4570 nfsvno_setpathbuf(&named, &bufp, &hashp);
4571 named.ni_cnd.cn_nameptr = bufp;
4572 named.ni_cnd.cn_namelen = strlen(fname);
4573 strlcpy(bufp, fname, NAME_MAX);
4574 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4575 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4576 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4578 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4582 nfsvno_relpathbuf(&named);
4584 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4589 * Start up the thread that will execute nfsrv_dsremove().
4592 start_dsremove(void *arg, int pending)
4594 struct nfsrvdsremove *dsrm;
4596 dsrm = (struct nfsrvdsremove *)arg;
4597 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4600 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4604 * Remove a pNFS data file from a Data Server.
4605 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4606 * removed to set up the dvp and fill in the FH.
4609 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4612 struct ucred *tcred;
4613 struct nfsrvdsremove *dsrm, *tdsrm;
4614 struct nfsdevice *ds;
4615 struct nfsmount *nmp;
4616 int failpos, i, ret, timo;
4618 tcred = newnfs_getcred();
4621 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4623 * Remove the file on each DS mirror, using kernel process(es) for the
4624 * additional mirrors.
4627 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4628 tdsrm->tcred = tcred;
4630 tdsrm->dvp = dvp[i];
4631 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4636 if (nfs_pnfsiothreads != 0) {
4637 ret = nfs_pnfsio(start_dsremove, tdsrm);
4638 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4641 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4642 if (failpos == -1 && nfsds_failerr(ret))
4646 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4647 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4648 failpos = mirrorcnt - 1;
4649 timo = hz / 50; /* Wait for 20msec. */
4652 /* Wait for kernel task(s) to complete. */
4653 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4654 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4655 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4656 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4661 * If failpos has been set, that mirror has failed, so it needs
4665 nmp = VFSTONFS(dvp[failpos]->v_mount);
4667 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4668 NFSMNTP_CANCELRPCS)) == 0) {
4669 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4671 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4672 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4675 nfsrv_killrpcs(nmp);
4677 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4683 /* Get rid all layouts for the file. */
4684 nfsrv_freefilelayouts(fhp);
4691 * Generate a file name based on the file handle and put it in *bufp.
4692 * Return the number of bytes generated.
4695 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4699 const uint8_t *hexdigits = "0123456789abcdef";
4701 cp = (uint8_t *)fhp;
4702 for (i = 0; i < sizeof(*fhp); i++) {
4703 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4704 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4711 * Update the Metadata file's attributes from the DS file when a Read/Write
4712 * layout is returned.
4713 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4714 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4717 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4719 struct ucred *tcred;
4722 /* Do this as root so that it won't fail with EACCES. */
4723 tcred = newnfs_getcred();
4724 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4725 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4731 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4734 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4739 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4740 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4745 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4746 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4747 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4748 off_t *offp, int content, bool *eofp)
4750 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4751 fhandle_t fh[NFSDEV_MAXMIRRORS];
4752 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4753 struct nfsdevice *ds;
4754 struct pnfsdsattr dsattr;
4755 struct opnfsdsattr odsattr;
4757 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4759 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4761 * If not a regular file, not exported or not a pNFS server,
4762 * just return ENOENT.
4764 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4765 nfsrv_devidcnt == 0)
4769 buf = malloc(buflen, M_TEMP, M_WAITOK);
4773 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4774 * from the MetaData file's extended attribute.
4776 if (ioproc == NFSPROC_GETATTR) {
4777 error = vn_extattr_get(vp, IO_NODELOCKED,
4778 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4781 if (buflen == sizeof(odsattr)) {
4782 NFSBCOPY(buf, &odsattr, buflen);
4783 nap->na_filerev = odsattr.dsa_filerev;
4784 nap->na_size = odsattr.dsa_size;
4785 nap->na_atime = odsattr.dsa_atime;
4786 nap->na_mtime = odsattr.dsa_mtime;
4788 * Fake na_bytes by rounding up na_size.
4789 * Since we don't know the block size, just
4790 * use BLKDEV_IOSIZE.
4792 nap->na_bytes = (odsattr.dsa_size +
4793 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4794 } else if (buflen == sizeof(dsattr)) {
4795 NFSBCOPY(buf, &dsattr, buflen);
4796 nap->na_filerev = dsattr.dsa_filerev;
4797 nap->na_size = dsattr.dsa_size;
4798 nap->na_atime = dsattr.dsa_atime;
4799 nap->na_mtime = dsattr.dsa_mtime;
4800 nap->na_bytes = dsattr.dsa_bytes;
4806 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4807 * returns 0, just return now. nfsrv_checkdsattr()
4808 * returns 0 if there is no Read/Write layout
4809 * plus either an Open/Write_access or Write
4810 * delegation issued to a client for the file.
4812 if (nfsrv_pnfsgetdsattr == 0 ||
4813 nfsrv_checkdsattr(vp, p) == 0) {
4820 * Clear ENOATTR so the code below will attempt to do a
4821 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4822 * the extended attribute.
4824 if (error == ENOATTR)
4833 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4835 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4836 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4837 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4840 for (i = 0; i < mirrorcnt; i++)
4841 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4843 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4845 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4848 if (origmircnt == -1)
4849 origmircnt = mirrorcnt;
4851 * If failpos is set to a mirror#, then that mirror has
4852 * failed and will be disabled. For Read, Getattr and Seek, the
4853 * function only tries one mirror, so if that mirror has
4854 * failed, it will need to be retried. As such, increment
4855 * tryitagain for these cases.
4856 * For Write, Setattr and Setacl, the function tries all
4857 * mirrors and will not return an error for the case where
4858 * one mirror has failed. For these cases, the functioning
4859 * mirror(s) will have been modified, so a retry isn't
4860 * necessary. These functions will set failpos for the
4863 if (ioproc == NFSPROC_READDS) {
4864 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4866 if (nfsds_failerr(error) && mirrorcnt > 1) {
4868 * Setting failpos will cause the mirror
4869 * to be disabled and then a retry of this
4876 } else if (ioproc == NFSPROC_WRITEDS)
4877 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4878 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4879 else if (ioproc == NFSPROC_SETATTR)
4880 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4881 mirrorcnt, nap, &failpos);
4882 else if (ioproc == NFSPROC_SETACL)
4883 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4884 mirrorcnt, aclp, &failpos);
4885 else if (ioproc == NFSPROC_SEEKDS) {
4886 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4888 if (nfsds_failerr(error) && mirrorcnt > 1) {
4890 * Setting failpos will cause the mirror
4891 * to be disabled and then a retry of this
4898 } else if (ioproc == NFSPROC_ALLOCATE)
4899 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4900 &nmp[0], mirrorcnt, &failpos);
4902 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4903 vp, nmp[mirrorcnt - 1], nap);
4904 if (nfsds_failerr(error) && mirrorcnt > 1) {
4906 * Setting failpos will cause the mirror
4907 * to be disabled and then a retry of this
4908 * getattr is required.
4910 failpos = mirrorcnt - 1;
4917 failnmp = nmp[failpos];
4918 NFSLOCKMNT(failnmp);
4919 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4920 NFSMNTP_CANCELRPCS)) == 0) {
4921 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4922 NFSUNLOCKMNT(failnmp);
4923 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4925 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4928 nfsrv_killrpcs(failnmp);
4929 NFSLOCKMNT(failnmp);
4930 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4933 NFSUNLOCKMNT(failnmp);
4935 for (i = 0; i < mirrorcnt; i++)
4936 NFSVOPUNLOCK(dvp[i]);
4937 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4939 /* Try the Read/Getattr again if a mirror was deleted. */
4940 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4943 /* Return ENOENT for any Extended Attribute error. */
4947 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4952 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4954 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4955 * to be checked. If it points to a NULL nmp, then it returns
4956 * a suitable destination.
4957 * curnmp - If non-NULL, it is the source mount for the copy.
4960 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4961 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4962 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4963 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4965 struct vnode *dvp, *nvp = NULL, **tdvpp;
4967 struct nfsmount *nmp, *newnmp;
4968 struct sockaddr *sad;
4969 struct sockaddr_in *sin;
4970 struct nfsdevice *ds, *tds, *fndds;
4971 struct pnfsdsfile *pf;
4973 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4975 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4984 if (newnmpp != NULL)
4989 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4990 "pnfsd.dsfile", buflenp, buf, p);
4991 mirrorcnt = *buflenp / sizeof(*pf);
4992 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4993 *buflenp != sizeof(*pf) * mirrorcnt))
4996 pf = (struct pnfsdsfile *)buf;
4997 /* If curnmp != NULL, check for a match in the mirror list. */
4998 if (curnmp != NULL && error == 0) {
5000 for (i = 0; i < mirrorcnt; i++, pf++) {
5001 sad = (struct sockaddr *)&pf->dsf_sin;
5002 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5014 pf = (struct pnfsdsfile *)buf;
5015 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5017 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5019 sad = (struct sockaddr *)&pf->dsf_sin;
5021 dsdir = pf->dsf_dir;
5022 if (dsdir >= nfsrv_dsdirsize) {
5023 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5025 } else if (nvpp != NULL && newnmp != NULL &&
5026 nfsaddr2_match(sad, newnmp->nm_nam))
5029 if (ippos != NULL && curnmp == NULL &&
5030 sad->sa_family == AF_INET &&
5031 sin->sin_addr.s_addr == 0)
5033 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5035 /* Use the socket address to find the mount point. */
5038 /* Find a match for the IP address. */
5039 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5040 if (ds->nfsdev_nmp != NULL) {
5041 dvp = ds->nfsdev_dvp;
5042 nmp = VFSTONFS(dvp->v_mount);
5043 if (nmp != ds->nfsdev_nmp)
5044 printf("different2 nmp %p %p\n",
5045 nmp, ds->nfsdev_nmp);
5046 if (nfsaddr2_match(sad, nmp->nm_nam)) {
5052 if (fndds != NULL && newnmpp != NULL &&
5054 /* Search for a place to make a mirror copy. */
5055 TAILQ_FOREACH(tds, &nfsrv_devidhead,
5057 if (tds->nfsdev_nmp != NULL &&
5059 ((tds->nfsdev_mdsisset == 0 &&
5060 fndds->nfsdev_mdsisset == 0) ||
5061 (tds->nfsdev_mdsisset != 0 &&
5062 fndds->nfsdev_mdsisset != 0 &&
5063 fsidcmp(&tds->nfsdev_mdsfsid,
5064 &mp->mnt_stat.f_fsid) == 0))) {
5065 *newnmpp = tds->nfsdev_nmp;
5071 * Move this entry to the end of the
5072 * list, so it won't be selected as
5073 * easily the next time.
5075 TAILQ_REMOVE(&nfsrv_devidhead, tds,
5077 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5082 if (fndds != NULL) {
5083 dvp = fndds->nfsdev_dsdir[dsdir];
5084 if (lktype != 0 || fhiszero != 0 ||
5085 (nvpp != NULL && *nvpp == NULL)) {
5087 error = vn_lock(dvp,
5089 else if (lktype != 0)
5090 error = vn_lock(dvp, lktype);
5092 error = vn_lock(dvp, LK_SHARED);
5094 * If the file handle is all 0's, try to
5095 * do a Lookup against the DS to acquire
5097 * If dvpp == NULL or the Lookup fails,
5098 * unlock dvp after the call.
5100 if (error == 0 && (fhiszero != 0 ||
5101 (nvpp != NULL && *nvpp == NULL))) {
5102 error = nfsrv_pnfslookupds(vp,
5118 if (error != 0 || lktype == 0)
5124 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5125 if (devid != NULL) {
5126 NFSBCOPY(fndds->nfsdev_deviceid,
5127 devid, NFSX_V4DEVICEID);
5128 devid += NFSX_V4DEVICEID;
5133 NFSBCOPY(&pf->dsf_fh, fhp++,
5135 if (fnamep != NULL && gotone == 1)
5138 sizeof(pf->dsf_filename));
5140 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5145 if (error == 0 && gotone == 0)
5148 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5151 *mirrorcntp = gotone;
5153 if (gotone > 0 && dvpp != NULL) {
5155 * If the error didn't occur on the first one and
5156 * dvpp != NULL, the one(s) prior to the failure will
5157 * have locked dvp's that need to be unlocked.
5159 for (i = 0; i < gotone; i++) {
5160 NFSVOPUNLOCK(*dvpp);
5165 * If it found the vnode to be copied from before a failure,
5166 * it needs to be vput()'d.
5168 if (nvpp != NULL && *nvpp != NULL) {
5177 * Set the extended attribute for the Change attribute.
5180 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5182 struct pnfsdsattr dsattr;
5185 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5186 dsattr.dsa_filerev = nap->na_filerev;
5187 dsattr.dsa_size = nap->na_size;
5188 dsattr.dsa_atime = nap->na_atime;
5189 dsattr.dsa_mtime = nap->na_mtime;
5190 dsattr.dsa_bytes = nap->na_bytes;
5191 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5192 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5194 printf("pNFS: setextattr=%d\n", error);
5199 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5200 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5203 struct nfsrv_descript *nd;
5205 struct mbuf *m, *m2;
5206 int error = 0, retlen, tlen, trimlen;
5208 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5209 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5212 * Use a stateid where other is an alternating 01010 pattern and
5213 * seqid is 0xffffffff. This value is not defined as special by
5214 * the RFC and is used by the FreeBSD NFS server to indicate an
5215 * MDS->DS proxy operation.
5217 st.other[0] = 0x55555555;
5218 st.other[1] = 0x55555555;
5219 st.other[2] = 0x55555555;
5220 st.seqid = 0xffffffff;
5221 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5223 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5224 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5225 txdr_hyper(off, tl);
5226 *(tl + 2) = txdr_unsigned(len);
5227 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5228 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5233 if (nd->nd_repstat == 0) {
5234 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5235 NFSM_STRSIZ(retlen, len);
5237 /* Trim off the pre-data XDR from the mbuf chain. */
5239 while (m != NULL && m != nd->nd_md) {
5240 if (m->m_next == nd->nd_md) {
5242 m_freem(nd->nd_mrep);
5243 nd->nd_mrep = m = nd->nd_md;
5248 printf("nfsrv_readdsrpc: busted mbuf list\n");
5254 * Now, adjust first mbuf so that any XDR before the
5255 * read data is skipped over.
5257 trimlen = nd->nd_dpos - mtod(m, char *);
5259 m->m_len -= trimlen;
5260 NFSM_DATAP(m, trimlen);
5264 * Truncate the mbuf chain at retlen bytes of data,
5265 * plus XDR padding that brings the length up to a
5268 tlen = NFSM_RNDUP(retlen);
5270 if (m->m_len >= tlen) {
5280 } while (m != NULL);
5282 printf("nfsrv_readdsrpc: busted mbuf list\n");
5291 error = nd->nd_repstat;
5293 /* If nd->nd_mrep is already NULL, this is a no-op. */
5294 m_freem(nd->nd_mrep);
5296 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5301 * Do a write RPC on a DS data file, using this structure for the arguments,
5302 * so that this function can be executed by a separate kernel process.
5304 struct nfsrvwritedsdorpc {
5311 struct nfsmount *nmp;
5319 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5320 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5323 struct nfsrv_descript *nd;
5324 nfsattrbit_t attrbits;
5326 int commit, error, retlen;
5328 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5329 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5330 sizeof(fhandle_t), NULL, NULL, 0, 0);
5333 * Use a stateid where other is an alternating 01010 pattern and
5334 * seqid is 0xffffffff. This value is not defined as special by
5335 * the RFC and is used by the FreeBSD NFS server to indicate an
5336 * MDS->DS proxy operation.
5338 st.other[0] = 0x55555555;
5339 st.other[1] = 0x55555555;
5340 st.other[2] = 0x55555555;
5341 st.seqid = 0xffffffff;
5342 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5343 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5344 txdr_hyper(off, tl);
5347 * Do all writes FileSync, since the server doesn't hold onto dirty
5348 * buffers. Since clients should be accessing the DS servers directly
5349 * using the pNFS layouts, this just needs to work correctly as a
5352 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5353 *tl = txdr_unsigned(len);
5354 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5356 /* Put data in mbuf chain. */
5357 nd->nd_mb->m_next = m;
5359 /* Set nd_mb and nd_bpos to end of data. */
5360 while (m->m_next != NULL)
5363 nfsm_set(nd, m->m_len);
5364 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5366 /* Do a Getattr for the attributes that change upon writing. */
5367 NFSZERO_ATTRBIT(&attrbits);
5368 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5369 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5370 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5371 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5372 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5373 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5374 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5375 (void) nfsrv_putattrbit(nd, &attrbits);
5376 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5377 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5382 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5383 /* Get rid of weak cache consistency data for now. */
5384 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5385 (ND_NFSV4 | ND_V4WCCATTR)) {
5386 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5387 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5388 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5392 * Get rid of Op# and status for next op.
5394 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5396 nd->nd_flag |= ND_NOMOREDATA;
5398 if (nd->nd_repstat == 0) {
5399 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5400 retlen = fxdr_unsigned(int, *tl++);
5401 commit = fxdr_unsigned(int, *tl);
5402 if (commit != NFSWRITE_FILESYNC)
5404 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5405 retlen, commit, error);
5407 error = nd->nd_repstat;
5408 /* We have no use for the Write Verifier since we use FileSync. */
5411 * Get the Change, Size, Access Time and Modify Time attributes and set
5412 * on the Metadata file, so its attributes will be what the file's
5413 * would be if it had been written.
5416 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5417 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5418 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5420 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5422 m_freem(nd->nd_mrep);
5424 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5429 * Start up the thread that will execute nfsrv_writedsdorpc().
5432 start_writedsdorpc(void *arg, int pending)
5434 struct nfsrvwritedsdorpc *drpc;
5436 drpc = (struct nfsrvwritedsdorpc *)arg;
5437 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5438 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5440 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5444 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5445 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5446 struct mbuf **mpp, char *cp, int *failposp)
5448 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5451 int error, i, offs, ret, timo;
5453 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5454 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5457 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5460 /* Calculate offset in mbuf chain that data starts. */
5461 offs = cp - mtod(*mpp, char *);
5462 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5465 * Do the write RPC for every DS, using a separate kernel process
5466 * for every DS except the last one.
5469 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5471 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5479 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5481 if (nfs_pnfsiothreads != 0) {
5482 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5483 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5487 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5489 if (nfsds_failerr(ret) && *failposp == -1)
5491 else if (error == 0 && ret != 0)
5497 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5498 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5499 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5500 *failposp = mirrorcnt - 1;
5501 else if (error == 0 && ret != 0)
5504 error = nfsrv_setextattr(vp, &na, p);
5505 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5507 timo = hz / 50; /* Wait for 20msec. */
5510 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5511 /* Wait for RPCs on separate threads to complete. */
5512 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5513 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5514 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5516 else if (error == 0 && tdrpc->err != 0)
5524 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5525 * so that this function can be executed by a separate kernel process.
5527 struct nfsrvallocatedsdorpc {
5534 struct nfsmount *nmp;
5541 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5542 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5545 struct nfsrv_descript *nd;
5546 nfsattrbit_t attrbits;
5550 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5551 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5552 sizeof(fhandle_t), NULL, NULL, 0, 0);
5555 * Use a stateid where other is an alternating 01010 pattern and
5556 * seqid is 0xffffffff. This value is not defined as special by
5557 * the RFC and is used by the FreeBSD NFS server to indicate an
5558 * MDS->DS proxy operation.
5560 st.other[0] = 0x55555555;
5561 st.other[1] = 0x55555555;
5562 st.other[2] = 0x55555555;
5563 st.seqid = 0xffffffff;
5564 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5565 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5566 txdr_hyper(off, tl); tl += 2;
5567 txdr_hyper(len, tl); tl += 2;
5568 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5570 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5571 NFSGETATTR_ATTRBIT(&attrbits);
5572 nfsrv_putattrbit(nd, &attrbits);
5573 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5574 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5579 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5581 if (nd->nd_repstat == 0) {
5582 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5583 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5584 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5586 error = nd->nd_repstat;
5587 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5589 m_freem(nd->nd_mrep);
5591 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5596 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5599 start_allocatedsdorpc(void *arg, int pending)
5601 struct nfsrvallocatedsdorpc *drpc;
5603 drpc = (struct nfsrvallocatedsdorpc *)arg;
5604 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5605 drpc->len, NULL, drpc->cred, drpc->p);
5607 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5611 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5612 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5615 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5617 int error, i, ret, timo;
5619 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5622 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5626 * Do the allocate RPC for every DS, using a separate kernel process
5627 * for every DS except the last one.
5630 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5632 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5641 if (nfs_pnfsiothreads != 0) {
5642 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5643 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5647 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5649 if (nfsds_failerr(ret) && *failposp == -1)
5651 else if (error == 0 && ret != 0)
5657 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5658 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5659 *failposp = mirrorcnt - 1;
5660 else if (error == 0 && ret != 0)
5663 error = nfsrv_setextattr(vp, &na, p);
5664 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5666 timo = hz / 50; /* Wait for 20msec. */
5669 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5670 /* Wait for RPCs on separate threads to complete. */
5671 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5672 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5673 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5675 else if (error == 0 && tdrpc->err != 0)
5683 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5684 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5685 struct nfsvattr *dsnap)
5688 struct nfsrv_descript *nd;
5690 nfsattrbit_t attrbits;
5693 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5694 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5696 * Use a stateid where other is an alternating 01010 pattern and
5697 * seqid is 0xffffffff. This value is not defined as special by
5698 * the RFC and is used by the FreeBSD NFS server to indicate an
5699 * MDS->DS proxy operation.
5701 st.other[0] = 0x55555555;
5702 st.other[1] = 0x55555555;
5703 st.other[2] = 0x55555555;
5704 st.seqid = 0xffffffff;
5705 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5707 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5708 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5710 /* Do a Getattr for the attributes that change due to writing. */
5711 NFSZERO_ATTRBIT(&attrbits);
5712 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5713 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5714 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5715 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5716 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5717 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5718 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5719 (void) nfsrv_putattrbit(nd, &attrbits);
5720 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5721 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5726 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5728 /* Get rid of weak cache consistency data for now. */
5729 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5730 (ND_NFSV4 | ND_V4WCCATTR)) {
5731 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5732 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5733 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5737 * Get rid of Op# and status for next op.
5739 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5741 nd->nd_flag |= ND_NOMOREDATA;
5743 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5746 if (nd->nd_repstat != 0)
5747 error = nd->nd_repstat;
5749 * Get the Change, Size, Access Time and Modify Time attributes and set
5750 * on the Metadata file, so its attributes will be what the file's
5751 * would be if it had been written.
5754 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5755 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5756 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5758 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5760 m_freem(nd->nd_mrep);
5762 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5766 struct nfsrvsetattrdsdorpc {
5771 struct nfsmount *nmp;
5776 struct nfsvattr dsna;
5781 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5784 start_setattrdsdorpc(void *arg, int pending)
5786 struct nfsrvsetattrdsdorpc *drpc;
5788 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5789 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5790 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5795 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5796 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5797 struct nfsvattr *nap, int *failposp)
5799 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5801 int error, i, ret, timo;
5803 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5806 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5810 * Do the setattr RPC for every DS, using a separate kernel process
5811 * for every DS except the last one.
5814 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5817 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5825 if (nfs_pnfsiothreads != 0) {
5826 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5827 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5831 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5833 if (nfsds_failerr(ret) && *failposp == -1)
5835 else if (error == 0 && ret != 0)
5841 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5842 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5843 *failposp = mirrorcnt - 1;
5844 else if (error == 0 && ret != 0)
5847 error = nfsrv_setextattr(vp, &na, p);
5848 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5850 timo = hz / 50; /* Wait for 20msec. */
5853 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5854 /* Wait for RPCs on separate threads to complete. */
5855 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5856 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5857 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5859 else if (error == 0 && tdrpc->err != 0)
5867 * Do a Setattr of an NFSv4 ACL on the DS file.
5870 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5871 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5873 struct nfsrv_descript *nd;
5875 nfsattrbit_t attrbits;
5878 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5879 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5881 * Use a stateid where other is an alternating 01010 pattern and
5882 * seqid is 0xffffffff. This value is not defined as special by
5883 * the RFC and is used by the FreeBSD NFS server to indicate an
5884 * MDS->DS proxy operation.
5886 st.other[0] = 0x55555555;
5887 st.other[1] = 0x55555555;
5888 st.other[2] = 0x55555555;
5889 st.seqid = 0xffffffff;
5890 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5892 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5893 NFSZERO_ATTRBIT(&attrbits);
5894 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5896 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5897 * so passing in the metadata "vp" will be ok, since it is of
5898 * the same type (VREG).
5900 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5901 NULL, 0, 0, 0, 0, 0, NULL);
5902 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5903 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5908 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5910 error = nd->nd_repstat;
5911 m_freem(nd->nd_mrep);
5916 struct nfsrvsetacldsdorpc {
5921 struct nfsmount *nmp;
5930 * Start up the thread that will execute nfsrv_setacldsdorpc().
5933 start_setacldsdorpc(void *arg, int pending)
5935 struct nfsrvsetacldsdorpc *drpc;
5937 drpc = (struct nfsrvsetacldsdorpc *)arg;
5938 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5939 drpc->vp, drpc->nmp, drpc->aclp);
5944 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5945 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5948 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
5949 int error, i, ret, timo;
5951 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5954 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5958 * Do the setattr RPC for every DS, using a separate kernel process
5959 * for every DS except the last one.
5962 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5965 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5973 if (nfs_pnfsiothreads != 0) {
5974 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5975 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5979 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5981 if (nfsds_failerr(ret) && *failposp == -1)
5983 else if (error == 0 && ret != 0)
5989 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5990 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5991 *failposp = mirrorcnt - 1;
5992 else if (error == 0 && ret != 0)
5994 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5996 timo = hz / 50; /* Wait for 20msec. */
5999 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6000 /* Wait for RPCs on separate threads to complete. */
6001 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6002 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6003 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6005 else if (error == 0 && tdrpc->err != 0)
6013 * Getattr call to the DS for the attributes that change due to writing.
6016 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6017 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6019 struct nfsrv_descript *nd;
6021 nfsattrbit_t attrbits;
6023 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6024 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6025 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6026 sizeof(fhandle_t), NULL, NULL, 0, 0);
6027 NFSZERO_ATTRBIT(&attrbits);
6028 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6029 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6030 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6031 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6032 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6033 (void) nfsrv_putattrbit(nd, &attrbits);
6034 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6035 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6040 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6042 if (nd->nd_repstat == 0) {
6043 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6044 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6047 * We can only save the updated values in the extended
6048 * attribute if the vp is exclusively locked.
6049 * This should happen when any of the following operations
6050 * occur on the vnode:
6051 * Close, Delegreturn, LayoutCommit, LayoutReturn
6052 * As such, the updated extended attribute should get saved
6053 * before nfsrv_checkdsattr() returns 0 and allows the cached
6054 * attributes to be returned without calling this function.
6056 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6057 error = nfsrv_setextattr(vp, nap, p);
6058 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6062 error = nd->nd_repstat;
6063 m_freem(nd->nd_mrep);
6065 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6070 * Seek call to a DS.
6073 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6074 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6077 struct nfsrv_descript *nd;
6081 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6083 * Use a stateid where other is an alternating 01010 pattern and
6084 * seqid is 0xffffffff. This value is not defined as special by
6085 * the RFC and is used by the FreeBSD NFS server to indicate an
6086 * MDS->DS proxy operation.
6088 st.other[0] = 0x55555555;
6089 st.other[1] = 0x55555555;
6090 st.other[2] = 0x55555555;
6091 st.seqid = 0xffffffff;
6092 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6093 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6094 sizeof(fhandle_t), NULL, NULL, 0, 0);
6095 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6096 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6097 txdr_hyper(*offp, tl); tl += 2;
6098 *tl = txdr_unsigned(content);
6099 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6100 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6105 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6106 if (nd->nd_repstat == 0) {
6107 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6108 if (*tl++ == newnfs_true)
6112 *offp = fxdr_hyper(tl);
6114 error = nd->nd_repstat;
6116 m_freem(nd->nd_mrep);
6118 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6123 * Get the device id and file handle for a DS file.
6126 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6127 fhandle_t *fhp, char *devid)
6133 buf = malloc(buflen, M_TEMP, M_WAITOK);
6134 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6135 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6141 * Do a Lookup against the DS for the filename.
6144 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6145 struct vnode **nvpp, NFSPROC_T *p)
6147 struct nameidata named;
6148 struct ucred *tcred;
6154 tcred = newnfs_getcred();
6155 named.ni_cnd.cn_nameiop = LOOKUP;
6156 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6157 named.ni_cnd.cn_cred = tcred;
6158 named.ni_cnd.cn_thread = p;
6159 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
6160 nfsvno_setpathbuf(&named, &bufp, &hashp);
6161 named.ni_cnd.cn_nameptr = bufp;
6162 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6163 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6164 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6165 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6166 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6168 nfsvno_relpathbuf(&named);
6171 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6176 * Set the file handle to the correct one.
6179 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6180 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6186 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6188 * We can only do a vn_set_extattr() if the vnode is exclusively
6189 * locked and vn_start_write() has been done. If devid != NULL or
6190 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6191 * may not have been done.
6192 * If not done now, it will be done on a future call.
6194 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6196 ret = vn_extattr_set(vp, IO_NODELOCKED,
6197 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6199 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6203 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6204 * when the DS has failed.
6207 nfsrv_killrpcs(struct nfsmount *nmp)
6211 * Call newnfs_nmcancelreqs() to cause
6212 * any RPCs in progress on the mount point to
6214 * This will cause any process waiting for an
6215 * RPC to complete while holding a vnode lock
6216 * on the mounted-on vnode (such as "df" or
6217 * a non-forced "umount") to fail.
6218 * This will unlock the mounted-on vnode so
6219 * a forced dismount can succeed.
6220 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6223 newnfs_nmcancelreqs(nmp);
6227 * Sum up the statfs info for each of the DSs, so that the client will
6228 * receive the total for all DSs.
6231 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6234 struct nfsdevice *ds;
6235 struct vnode **dvpp, **tdvpp, *dvp;
6237 int cnt, error = 0, i;
6239 if (nfsrv_devidcnt <= 0)
6241 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6242 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6244 /* Get an array of the dvps for the DSs. */
6248 /* First, search for matches for same file system. */
6249 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6250 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6251 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6252 if (++i > nfsrv_devidcnt)
6254 *tdvpp++ = ds->nfsdev_dvp;
6258 * If no matches for same file system, total all servers not assigned
6262 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6263 if (ds->nfsdev_nmp != NULL &&
6264 ds->nfsdev_mdsisset == 0) {
6265 if (++i > nfsrv_devidcnt)
6267 *tdvpp++ = ds->nfsdev_dvp;
6274 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6276 for (i = 0; i < cnt && error == 0; i++) {
6278 error = VFS_STATFS(dvp->v_mount, tsf);
6280 if (sf->f_bsize == 0) {
6281 if (tsf->f_bsize > 0)
6282 sf->f_bsize = tsf->f_bsize;
6286 if (tsf->f_blocks > 0) {
6287 if (sf->f_bsize != tsf->f_bsize) {
6288 tot = tsf->f_blocks * tsf->f_bsize;
6289 sf->f_blocks += (tot / sf->f_bsize);
6291 sf->f_blocks += tsf->f_blocks;
6293 if (tsf->f_bfree > 0) {
6294 if (sf->f_bsize != tsf->f_bsize) {
6295 tot = tsf->f_bfree * tsf->f_bsize;
6296 sf->f_bfree += (tot / sf->f_bsize);
6298 sf->f_bfree += tsf->f_bfree;
6300 if (tsf->f_bavail > 0) {
6301 if (sf->f_bsize != tsf->f_bsize) {
6302 tot = tsf->f_bavail * tsf->f_bsize;
6303 sf->f_bavail += (tot / sf->f_bsize);
6305 sf->f_bavail += tsf->f_bavail;
6318 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6322 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6323 error = NFSERR_ATTRNOTSUPP;
6327 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6328 * Make sure it has enough room for that - splitting every entry
6329 * into two and appending "canonical six" entries at the end.
6330 * Cribbed out of kern/vfs_acl.c - Rick M.
6332 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6333 error = NFSERR_ATTRNOTSUPP;
6336 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6338 error = nfsrv_dssetacl(vp, aclp, cred, p);
6339 if (error == ENOENT)
6349 * Seek vnode op call (actually it is a VOP_IOCTL()).
6350 * This function is called with the vnode locked, but unlocks and vrele()s
6351 * the vp before returning.
6354 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6355 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6360 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6362 * Attempt to seek on a DS file. A return of ENOENT implies
6363 * there is no DS file to seek on.
6365 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6366 NULL, NULL, NULL, NULL, offp, content, eofp);
6367 if (error != ENOENT) {
6373 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6374 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6375 * NFSv4.2 is *eofp == true and error == 0 for this case.
6378 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6380 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6381 /* Handle the cases where we might be at EOF. */
6382 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6383 if (ret == 0 && *offp == at.na_size) {
6387 if (ret != 0 && error == 0)
6396 * Allocate vnode op call.
6399 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6405 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6407 * Attempt to allocate on a DS file. A return of ENOENT implies
6408 * there is no DS file to allocate on.
6410 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6411 NULL, NULL, NULL, NULL, &len, 0, NULL);
6412 if (error != ENOENT)
6416 * Do the actual VOP_ALLOCATE(), looping so long as
6417 * progress is being made, to achieve completion.
6421 error = VOP_ALLOCATE(vp, &off, &len);
6422 if (error == 0 && len > 0 && olen > len)
6424 } while (error == 0 && len > 0 && olen > len);
6425 if (error == 0 && len > 0)
6432 * Get Extended Atribute vnode op into an mbuf list.
6435 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6436 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6437 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6440 struct uio io, *uiop = &io;
6441 struct mbuf *m, *m2;
6442 int alen, error, len, tlen;
6445 /* First, find out the size of the extended attribute. */
6446 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6449 return (NFSERR_NOXATTR);
6450 if (siz > maxresp - NFS_MAXXDR)
6451 return (NFSERR_XATTR2BIG);
6453 tlen = NFSM_RNDUP(len);
6456 * If cnt > MCLBYTES and the reply will not be saved, use
6457 * ext_pgs mbufs for TLS.
6458 * For NFSv4.0, we do not know for sure if the reply will
6459 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6460 * Always use ext_pgs mbufs if ND_EXTPG is set.
6462 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6463 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6464 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6465 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6466 maxextsiz, &m, &m2, &iv);
6468 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6472 uiop->uio_iovcnt = 0;
6473 uiop->uio_iov = iv = NULL;
6476 uiop->uio_offset = 0;
6477 uiop->uio_resid = tlen;
6478 uiop->uio_rw = UIO_READ;
6479 uiop->uio_segflg = UIO_SYSSPACE;
6482 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6489 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6493 if (uiop->uio_resid > 0) {
6495 len = tlen - uiop->uio_resid;
6496 tlen = NFSM_RNDUP(len);
6498 printf("nfsvno_getxattr: weird size read\n");
6502 } else if (alen != tlen || tlen != len)
6503 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6521 * Set Extended attribute vnode op from an mbuf list.
6524 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6525 char *cp, struct ucred *cred, struct thread *p)
6528 struct uio uio, *uiop = &uio;
6533 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6539 uiop->uio_rw = UIO_WRITE;
6540 uiop->uio_segflg = UIO_SYSSPACE;
6542 uiop->uio_offset = 0;
6543 uiop->uio_resid = len;
6545 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6547 uiop->uio_iovcnt = cnt;
6549 uiop->uio_iov = iv = NULL;
6550 uiop->uio_iovcnt = 0;
6553 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6564 * Remove Extended attribute vnode op.
6567 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6568 struct ucred *cred, struct thread *p)
6573 * Get rid of any delegations. I am not sure why this is required,
6574 * but RFC-8276 says so.
6576 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6580 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6586 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6587 if (error == EOPNOTSUPP)
6588 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6596 * List Extended Atribute vnode op into an mbuf list.
6599 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6600 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6608 /* First, find out the size of the extended attribute. */
6609 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6612 return (NFSERR_NOXATTR);
6613 if (siz <= cookie) {
6618 if (siz > cookie + *lenp) {
6619 siz = cookie + *lenp;
6623 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6624 if (siz > 10 * 1024 * 1024) {
6625 error = NFSERR_XATTR2BIG;
6628 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6629 iv.iov_base = *bufp;
6635 io.uio_rw = UIO_READ;
6636 io.uio_segflg = UIO_SYSSPACE;
6639 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6644 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6648 if (io.uio_resid > 0)
6649 siz -= io.uio_resid;
6654 free(*bufp, M_TEMP);
6662 * Trim trailing data off the mbuf list being built.
6665 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6666 int bextpg, int bextpgsiz)
6671 if (mb->m_next != NULL) {
6672 m_freem(mb->m_next);
6675 if ((mb->m_flags & M_EXTPG) != 0) {
6676 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
6677 ("nfsm_trimtrailing: bextpg out of range"));
6678 KASSERT(bpos == (char *)(void *)
6679 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
6680 ("nfsm_trimtrailing: bextpgsiz bad!"));
6682 /* First, get rid of any pages after this position. */
6683 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
6684 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
6685 vm_page_unwire_noq(pg);
6688 mb->m_epg_npgs = bextpg + 1;
6690 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
6692 fullpgsiz = PAGE_SIZE;
6693 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
6694 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
6695 for (i = 1; i < mb->m_epg_npgs; i++)
6696 mb->m_len += m_epg_pagelen(mb, i, 0);
6697 nd->nd_bextpgsiz = bextpgsiz;
6698 nd->nd_bextpg = bextpg;
6700 mb->m_len = bpos - mtod(mb, char *);
6707 * Check to see if a put file handle operation should test for
6708 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
6709 * When Open is the next operation, NFSERR_WRONGSEC cannot be
6710 * replied for the Open cases that use a component. Thia can
6711 * be identified by the fact that the file handle's type is VDIR.
6714 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, enum vtype vtyp)
6717 if ((nd->nd_flag & ND_NFSV4) == 0) {
6718 if (nd->nd_procnum == NFSPROC_FSINFO)
6723 if ((nd->nd_flag & ND_LASTOP) != 0)
6726 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
6727 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
6728 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
6729 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
6731 if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
6736 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
6739 * Called once to initialize data structures...
6742 nfsd_modevent(module_t mod, int type, void *data)
6745 static int loaded = 0;
6752 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6753 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
6755 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
6758 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
6759 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
6760 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
6761 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
6762 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
6763 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
6770 #ifdef VV_DISABLEDELEG
6771 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
6772 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
6774 nfsd_call_servertimer = nfsrv_servertimer;
6775 nfsd_call_nfsd = nfssvc_nfsd;
6780 if (newnfs_numnfsd != 0) {
6785 #ifdef VV_DISABLEDELEG
6786 vn_deleg_ops.vndeleg_recall = NULL;
6787 vn_deleg_ops.vndeleg_disable = NULL;
6789 nfsd_call_servertimer = NULL;
6790 nfsd_call_nfsd = NULL;
6792 /* Clean out all NFSv4 state. */
6793 nfsrv_throwawayallstate(curthread);
6795 /* Clean the NFS server reply cache */
6796 nfsrvd_cleancache();
6798 /* Free up the krpc server pool. */
6799 if (nfsrvd_pool != NULL)
6800 svcpool_destroy(nfsrvd_pool);
6802 /* and get rid of the locks */
6803 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6804 mtx_destroy(&nfsrchash_table[i].mtx);
6805 mtx_destroy(&nfsrcahash_table[i].mtx);
6807 mtx_destroy(&nfsrc_udpmtx);
6808 mtx_destroy(&nfs_v4root_mutex);
6809 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
6810 mtx_destroy(&nfsrv_dontlistlock_mtx);
6811 mtx_destroy(&nfsrv_recalllock_mtx);
6812 for (i = 0; i < nfsrv_sessionhashsize; i++)
6813 mtx_destroy(&nfssessionhash[i].mtx);
6814 if (nfslayouthash != NULL) {
6815 for (i = 0; i < nfsrv_layouthashsize; i++)
6816 mtx_destroy(&nfslayouthash[i].mtx);
6817 free(nfslayouthash, M_NFSDSESSION);
6819 lockdestroy(&nfsv4root_mnt.mnt_explock);
6820 free(nfsclienthash, M_NFSDCLIENT);
6821 free(nfslockhash, M_NFSDLOCKFILE);
6822 free(nfssessionhash, M_NFSDSESSION);
6834 static moduledata_t nfsd_mod = {
6839 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
6841 /* So that loader and kldload(2) can find us, wherever we are.. */
6842 MODULE_VERSION(nfsd, 1);
6843 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
6844 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
6845 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
6846 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);