2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/capsicum.h>
40 #include <sys/extattr.h>
43 * Functions that perform the vfs operations required by the routines in
44 * nfsd_serv.c. It is hoped that this change will make the server more
48 #include <fs/nfs/nfsport.h>
49 #include <security/mac/mac_framework.h>
50 #include <sys/filio.h>
52 #include <sys/sysctl.h>
53 #include <nlm/nlm_prot.h>
56 FEATURE(nfsd, "NFSv4 server");
58 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
59 extern int nfsrv_useacl;
60 extern int newnfs_numnfsd;
61 extern struct mount nfsv4root_mnt;
62 extern struct nfsrv_stablefirst nfsrv_stablefirst;
63 extern void (*nfsd_call_servertimer)(void);
64 extern SVCPOOL *nfsrvd_pool;
65 extern struct nfsv4lock nfsd_suspend_lock;
66 extern struct nfsclienthashhead *nfsclienthash;
67 extern struct nfslockhashhead *nfslockhash;
68 extern struct nfssessionhash *nfssessionhash;
69 extern int nfsrv_sessionhashsize;
70 extern struct nfsstatsv1 nfsstatsv1;
71 extern struct nfslayouthash *nfslayouthash;
72 extern int nfsrv_layouthashsize;
73 extern struct mtx nfsrv_dslock_mtx;
74 extern int nfs_pnfsiothreads;
75 extern struct nfsdontlisthead nfsrv_dontlisthead;
76 extern volatile int nfsrv_dontlistlen;
77 extern volatile int nfsrv_devidcnt;
78 extern int nfsrv_maxpnfsmirror;
79 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
82 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
83 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
84 struct mtx nfsrc_udpmtx;
85 struct mtx nfs_v4root_mutex;
86 struct mtx nfsrv_dontlistlock_mtx;
87 struct mtx nfsrv_recalllock_mtx;
88 struct nfsrvfh nfs_rootfh, nfs_pubfh;
89 int nfs_pubfhset = 0, nfs_rootfhset = 0;
90 struct proc *nfsd_master_proc = NULL;
91 int nfsd_debuglevel = 0;
92 static pid_t nfsd_master_pid = (pid_t)-1;
93 static char nfsd_master_comm[MAXCOMLEN + 1];
94 static struct timeval nfsd_master_start;
95 static uint32_t nfsv4_sysid = 0;
96 static fhandle_t zerofh;
98 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
101 int nfsrv_enable_crossmntpt = 1;
102 static int nfs_commit_blks;
103 static int nfs_commit_miss;
104 extern int nfsrv_issuedelegs;
105 extern int nfsrv_dolocallocks;
106 extern int nfsd_enable_stringtouid;
107 extern struct nfsdevicehead nfsrv_devidhead;
109 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
111 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
113 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
115 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
116 int *, char *, fhandle_t *);
117 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
119 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
120 struct thread *, int, struct mbuf **, char *, struct mbuf **,
121 struct nfsvattr *, struct acl *, off_t *, int, bool *);
122 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
123 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
124 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
125 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
126 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
128 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
129 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
130 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
131 struct vnode *, struct nfsmount **, int, struct acl *, int *);
132 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
133 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
134 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
135 struct vnode *, struct nfsmount *, struct nfsvattr *);
136 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
137 NFSPROC_T *, struct nfsmount *);
138 static int nfsrv_putfhname(fhandle_t *, char *);
139 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
140 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
141 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
142 struct vnode *, NFSPROC_T *);
143 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
144 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
146 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
148 int nfs_pnfsio(task_fn_t *, void *);
150 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
152 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
153 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
154 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
156 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
158 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
159 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
160 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
161 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
162 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
163 0, "Debug level for NFS server");
164 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
165 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
166 static int nfsrv_pnfsgetdsattr = 1;
167 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
168 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
171 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
173 * The dsN subdirectories for the increased values must have been created
174 * on all DS servers before this increase is done.
176 u_int nfsrv_dsdirsize = 20;
178 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
180 int error, newdsdirsize;
182 newdsdirsize = nfsrv_dsdirsize;
183 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
184 if (error != 0 || req->newptr == NULL)
186 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
189 nfsrv_dsdirsize = newdsdirsize;
192 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
193 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
194 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
196 #define MAX_REORDERED_RPC 16
197 #define NUM_HEURISTIC 1031
198 #define NHUSE_INIT 64
200 #define NHUSE_MAX 2048
202 static struct nfsheur {
203 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
204 off_t nh_nextoff; /* next offset for sequential detection */
205 int nh_use; /* use count for selection */
206 int nh_seqcount; /* heuristic */
207 } nfsheur[NUM_HEURISTIC];
211 * Heuristic to detect sequential operation.
213 static struct nfsheur *
214 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
219 /* Locate best candidate. */
221 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
224 if (nfsheur[hi].nh_vp == vp) {
228 if (nfsheur[hi].nh_use > 0)
229 --nfsheur[hi].nh_use;
230 hi = (hi + 1) % NUM_HEURISTIC;
231 if (nfsheur[hi].nh_use < nh->nh_use)
235 /* Initialize hint if this is a new file. */
236 if (nh->nh_vp != vp) {
238 nh->nh_nextoff = uio->uio_offset;
239 nh->nh_use = NHUSE_INIT;
240 if (uio->uio_offset == 0)
246 /* Calculate heuristic. */
247 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
248 uio->uio_offset == nh->nh_nextoff) {
249 /* See comments in vfs_vnops.c:sequential_heuristic(). */
250 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
251 if (nh->nh_seqcount > IO_SEQMAX)
252 nh->nh_seqcount = IO_SEQMAX;
253 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
254 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
255 /* Probably a reordered RPC, leave seqcount alone. */
256 } else if (nh->nh_seqcount > 1) {
257 nh->nh_seqcount /= 2;
261 nh->nh_use += NHUSE_INC;
262 if (nh->nh_use > NHUSE_MAX)
263 nh->nh_use = NHUSE_MAX;
268 * Get attributes into nfsvattr structure.
271 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
272 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
273 nfsattrbit_t *attrbitp)
275 int error, gotattr, lockedit = 0;
278 if (vpislocked == 0) {
280 * When vpislocked == 0, the vnode is either exclusively
281 * locked by this thread or not locked by this thread.
282 * As such, shared lock it, if not exclusively locked.
284 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
286 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
291 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
292 * attributes, as required.
293 * This needs to be done for regular files if:
294 * - non-NFSv4 RPCs or
295 * - when attrbitp == NULL or
296 * - an NFSv4 RPC with any of the above attributes in attrbitp.
297 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
298 * these attributes. nfsrv_proxyds() will return an error if the
299 * server is not a pNFS one.
302 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
303 (nd->nd_flag & ND_NFSV4) == 0 ||
304 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
305 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
306 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
307 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
308 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
309 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
310 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
316 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
321 * If we got the Change, Size and Modify Time from the DS,
325 nvap->na_atime = na.na_atime;
326 nvap->na_mtime = na.na_mtime;
327 nvap->na_filerev = na.na_filerev;
328 nvap->na_size = na.na_size;
329 nvap->na_bytes = na.na_bytes;
331 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
332 error, (uintmax_t)na.na_filerev);
339 * Get a file handle for a vnode.
342 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
346 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
347 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
348 error = VOP_VPTOFH(vp, &fhp->fh_fid);
355 * Perform access checking for vnodes obtained from file handles that would
356 * refer to files already opened by a Unix client. You cannot just use
357 * vn_writechk() and VOP_ACCESSX() for two reasons.
358 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
360 * 2 - The owner is to be given access irrespective of mode bits for some
361 * operations, so that processes that chmod after opening a file don't
365 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
366 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
367 u_int32_t *supportedtypep)
370 int error = 0, getret = 0;
372 if (vpislocked == 0) {
373 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
378 if (accmode & VWRITE) {
379 /* Just vn_writechk() changed to check rdonly */
381 * Disallow write attempts on read-only file systems;
382 * unless the file is a socket or a block or character
383 * device resident on the file system.
385 if (NFSVNO_EXRDONLY(exp) ||
386 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
387 switch (vp->v_type) {
397 * If there's shared text associated with
398 * the inode, try to free it up once. If
399 * we fail, we can't allow writing.
401 if (VOP_IS_TEXT(vp) && error == 0)
411 * Should the override still be applied when ACLs are enabled?
413 error = VOP_ACCESSX(vp, accmode, cred, p);
414 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
416 * Try again with VEXPLICIT_DENY, to see if the test for
417 * deletion is supported.
419 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
421 if (vp->v_type == VDIR) {
422 accmode &= ~(VDELETE | VDELETE_CHILD);
424 error = VOP_ACCESSX(vp, accmode, cred, p);
425 } else if (supportedtypep != NULL) {
426 *supportedtypep &= ~NFSACCESS_DELETE;
432 * Allow certain operations for the owner (reads and writes
433 * on files that are already open).
435 if (override != NFSACCCHK_NOOVERRIDE &&
436 (error == EPERM || error == EACCES)) {
437 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
439 else if (override & NFSACCCHK_ALLOWOWNER) {
440 getret = VOP_GETATTR(vp, &vattr, cred);
441 if (getret == 0 && cred->cr_uid == vattr.va_uid)
454 * Set attribute(s) vnop.
457 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
458 struct thread *p, struct nfsexstuff *exp)
460 u_quad_t savsize = 0;
464 * If this is an exported file system and a pNFS service is running,
465 * don't VOP_SETATTR() of size for the MDS file system.
469 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
470 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
471 nvap->na_vattr.va_size > 0) {
472 savsize = nvap->na_vattr.va_size;
473 nvap->na_vattr.va_size = VNOVAL;
474 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
475 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
476 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
477 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
478 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
484 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
486 nvap->na_vattr.va_size = savsize;
487 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
488 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
489 nvap->na_vattr.va_size != VNOVAL ||
490 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
491 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
492 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
493 /* For a pNFS server, set the attributes on the DS file. */
494 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
495 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
504 * Set up nameidata for a lookup() call and do it.
507 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
508 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p,
509 struct vnode **retdirp)
511 struct componentname *cnp = &ndp->ni_cnd;
515 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
520 cnp->cn_nameptr = cnp->cn_pnbuf;
523 * Extract and set starting directory.
525 if (dp->v_type != VDIR) {
530 nfsvno_relpathbuf(ndp);
538 if (NFSVNO_EXRDONLY(exp))
539 cnp->cn_flags |= RDONLY;
540 ndp->ni_segflg = UIO_SYSSPACE;
542 if (nd->nd_flag & ND_PUBLOOKUP) {
544 if (cnp->cn_pnbuf[0] == '/') {
547 * Check for degenerate pathnames here, since lookup()
550 for (i = 1; i < ndp->ni_pathlen; i++)
551 if (cnp->cn_pnbuf[i] != '/')
553 if (i == ndp->ni_pathlen) {
554 error = NFSERR_ACCES;
560 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
561 (nd->nd_flag & ND_NFSV4) == 0) {
563 * Only cross mount points for NFSv4 when doing a
564 * mount while traversing the file system above
565 * the mount point, unless nfsrv_enable_crossmntpt is set.
567 cnp->cn_flags |= NOCROSSMOUNT;
571 * Initialize for scan, set ni_startdir and bump ref on dp again
572 * because lookup() will dereference ni_startdir.
576 ndp->ni_startdir = dp;
577 ndp->ni_rootdir = rootvnode;
578 ndp->ni_topdir = NULL;
581 cnp->cn_flags |= LOCKLEAF;
583 cnp->cn_nameptr = cnp->cn_pnbuf;
585 * Call lookup() to do the real work. If an error occurs,
586 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
587 * we do not have to dereference anything before returning.
588 * In either case ni_startdir will be dereferenced and NULLed
596 * Check for encountering a symbolic link. Trivial
597 * termination occurs if no symlink encountered.
599 if ((cnp->cn_flags & ISSYMLINK) == 0) {
600 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
601 nfsvno_relpathbuf(ndp);
602 if (ndp->ni_vp && !lockleaf)
603 NFSVOPUNLOCK(ndp->ni_vp);
610 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
611 NFSVOPUNLOCK(ndp->ni_dvp);
612 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
617 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
621 if (ndp->ni_pathlen > 1)
622 cp = uma_zalloc(namei_zone, M_WAITOK);
626 aiov.iov_len = MAXPATHLEN;
627 auio.uio_iov = &aiov;
630 auio.uio_rw = UIO_READ;
631 auio.uio_segflg = UIO_SYSSPACE;
633 auio.uio_resid = MAXPATHLEN;
634 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
637 if (ndp->ni_pathlen > 1)
638 uma_zfree(namei_zone, cp);
644 linklen = MAXPATHLEN - auio.uio_resid;
649 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
650 error = ENAMETOOLONG;
655 * Adjust or replace path
657 if (ndp->ni_pathlen > 1) {
658 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
659 uma_zfree(namei_zone, cnp->cn_pnbuf);
662 cnp->cn_pnbuf[linklen] = '\0';
663 ndp->ni_pathlen += linklen;
666 * Cleanup refs for next loop and check if root directory
667 * should replace current directory. Normally ni_dvp
668 * becomes the new base directory and is cleaned up when
669 * we loop. Explicitly null pointers after invalidation
670 * to clarify operation.
675 if (cnp->cn_pnbuf[0] == '/') {
677 ndp->ni_dvp = ndp->ni_rootdir;
680 ndp->ni_startdir = ndp->ni_dvp;
684 cnp->cn_flags &= ~LOCKLEAF;
688 nfsvno_relpathbuf(ndp);
691 ndp->ni_startdir = NULL;
692 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
697 NFSEXITCODE2(error, nd);
702 * Set up a pathname buffer and return a pointer to it and, optionally
703 * set a hash pointer.
706 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
708 struct componentname *cnp = &ndp->ni_cnd;
710 cnp->cn_flags |= (NOMACCHECK | HASBUF);
711 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
714 *bufpp = cnp->cn_pnbuf;
718 * Release the above path buffer, if not released by nfsvno_namei().
721 nfsvno_relpathbuf(struct nameidata *ndp)
724 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0)
726 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
727 ndp->ni_cnd.cn_flags &= ~HASBUF;
731 * Readlink vnode op into an mbuf list.
734 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p,
735 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
738 struct uio io, *uiop = &io;
739 struct mbuf *mp, *mp3;
740 int len, tlen, error = 0;
742 len = NFS_MAXPATHLEN;
743 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
745 uiop->uio_offset = 0;
746 uiop->uio_resid = len;
747 uiop->uio_rw = UIO_READ;
748 uiop->uio_segflg = UIO_SYSSPACE;
750 error = VOP_READLINK(vp, uiop, cred);
757 if (uiop->uio_resid > 0) {
758 len -= uiop->uio_resid;
759 tlen = NFSM_RNDUP(len);
760 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len);
772 * Create an mbuf chain and an associated iovec that can be used to Read
773 * or Getextattr of data.
774 * Upon success, return pointers to the first and last mbufs in the chain
775 * plus the malloc'd iovec and its iovlen.
778 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
781 struct mbuf *m, *m2 = NULL, *m3;
788 * Generate the mbuf list with the uio_iov ref. to it.
795 siz = min(M_TRAILINGSPACE(m), left);
804 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
810 panic("nfsvno_read iov");
811 siz = min(M_TRAILINGSPACE(m), left);
813 iv->iov_base = mtod(m, caddr_t) + m->m_len;
828 * Read vnode op call into mbuf list.
831 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
832 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp)
836 int error = 0, len, tlen, ioflag = 0;
838 struct uio io, *uiop = &io;
842 * Attempt to read from a DS file. A return of ENOENT implies
843 * there is no DS file to read.
845 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
846 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
850 len = NFSM_RNDUP(cnt);
851 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
853 uiop->uio_offset = off;
854 uiop->uio_resid = len;
855 uiop->uio_rw = UIO_READ;
856 uiop->uio_segflg = UIO_SYSSPACE;
858 nh = nfsrv_sequential_heuristic(uiop, vp);
859 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
860 /* XXX KDM make this more systematic? */
861 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
862 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
869 nh->nh_nextoff = uiop->uio_offset;
870 tlen = len - uiop->uio_resid;
871 cnt = cnt < tlen ? cnt : tlen;
872 tlen = NFSM_RNDUP(cnt);
876 } else if (len != tlen || tlen != cnt)
877 nfsrv_adj(m3, len - tlen, tlen - cnt);
887 * Create the iovec for the mbuf chain passed in as an argument.
888 * The "cp" argument is where the data starts within the first mbuf in
889 * the chain. It returns the iovec and the iovcnt.
892 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
900 * Loop through the mbuf chain, counting how many mbufs are a
901 * part of this write operation, so the iovec size is known.
906 i = mtod(mp, caddr_t) + mp->m_len - cp;
920 /* Now, create the iovec. */
922 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
925 i = mtod(mp, caddr_t) + mp->m_len - cp;
929 panic("nfsvno_write");
940 cp = mtod(mp, caddr_t);
947 * Write vnode op from an mbuf list.
950 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
951 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
954 int cnt, ioflags, error;
955 struct uio io, *uiop = &io;
959 * Attempt to write to a DS file. A return of ENOENT implies
960 * there is no DS file to write.
962 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
963 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
964 if (error != ENOENT) {
965 *stable = NFSWRITE_FILESYNC;
970 if (*stable == NFSWRITE_UNSTABLE)
971 ioflags = IO_NODELOCKED;
973 ioflags = (IO_SYNC | IO_NODELOCKED);
974 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
978 uiop->uio_iovcnt = cnt;
979 uiop->uio_resid = retlen;
980 uiop->uio_rw = UIO_WRITE;
981 uiop->uio_segflg = UIO_SYSSPACE;
983 uiop->uio_offset = off;
984 nh = nfsrv_sequential_heuristic(uiop, vp);
985 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
986 /* XXX KDM make this more systematic? */
987 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
988 error = VOP_WRITE(vp, uiop, ioflags, cred);
990 nh->nh_nextoff = uiop->uio_offset;
998 * Common code for creating a regular file (plus special files for V2).
1001 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1002 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1003 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1007 struct thread *p = curthread;
1009 error = nd->nd_repstat;
1010 if (!error && ndp->ni_vp == NULL) {
1011 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1012 vrele(ndp->ni_startdir);
1013 error = VOP_CREATE(ndp->ni_dvp,
1014 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1015 /* For a pNFS server, create the data file on a DS. */
1016 if (error == 0 && nvap->na_type == VREG) {
1018 * Create a data file on a DS for a pNFS server.
1019 * This function just returns if not
1020 * running a pNFS DS or the creation fails.
1022 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1026 nfsvno_relpathbuf(ndp);
1028 if (*exclusive_flagp) {
1029 *exclusive_flagp = 0;
1030 NFSVNO_ATTRINIT(nvap);
1031 nvap->na_atime.tv_sec = cverf[0];
1032 nvap->na_atime.tv_nsec = cverf[1];
1033 error = VOP_SETATTR(ndp->ni_vp,
1034 &nvap->na_vattr, nd->nd_cred);
1038 error = NFSERR_NOTSUPP;
1043 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1044 * (This implies, just get out on an error.)
1046 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1047 nvap->na_type == VFIFO) {
1048 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1049 nvap->na_type = VFIFO;
1050 if (nvap->na_type != VFIFO &&
1051 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1052 vrele(ndp->ni_startdir);
1053 nfsvno_relpathbuf(ndp);
1057 nvap->na_rdev = rdev;
1058 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1059 &ndp->ni_cnd, &nvap->na_vattr);
1061 nfsvno_relpathbuf(ndp);
1062 vrele(ndp->ni_startdir);
1066 vrele(ndp->ni_startdir);
1067 nfsvno_relpathbuf(ndp);
1075 * Handle cases where error is already set and/or
1077 * 1 - clean up the lookup
1078 * 2 - iff !error and na_size set, truncate it
1080 vrele(ndp->ni_startdir);
1081 nfsvno_relpathbuf(ndp);
1083 if (ndp->ni_dvp == *vpp)
1087 if (!error && nvap->na_size != VNOVAL) {
1088 error = nfsvno_accchk(*vpp, VWRITE,
1089 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1090 NFSACCCHK_VPISLOCKED, NULL);
1092 tempsize = nvap->na_size;
1093 NFSVNO_ATTRINIT(nvap);
1094 nvap->na_size = tempsize;
1095 error = VOP_SETATTR(*vpp,
1096 &nvap->na_vattr, nd->nd_cred);
1109 * Do a mknod vnode op.
1112 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1118 vtyp = nvap->na_type;
1120 * Iff doesn't exist, create it.
1123 vrele(ndp->ni_startdir);
1124 nfsvno_relpathbuf(ndp);
1130 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1131 vrele(ndp->ni_startdir);
1132 nfsvno_relpathbuf(ndp);
1134 error = NFSERR_BADTYPE;
1137 if (vtyp == VSOCK) {
1138 vrele(ndp->ni_startdir);
1139 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1140 &ndp->ni_cnd, &nvap->na_vattr);
1142 nfsvno_relpathbuf(ndp);
1144 if (nvap->na_type != VFIFO &&
1145 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1146 vrele(ndp->ni_startdir);
1147 nfsvno_relpathbuf(ndp);
1151 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1152 &ndp->ni_cnd, &nvap->na_vattr);
1154 nfsvno_relpathbuf(ndp);
1155 vrele(ndp->ni_startdir);
1157 * Since VOP_MKNOD returns the ni_vp, I can't
1158 * see any reason to do the lookup.
1171 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1172 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1176 if (ndp->ni_vp != NULL) {
1177 if (ndp->ni_dvp == ndp->ni_vp)
1182 nfsvno_relpathbuf(ndp);
1186 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1189 nfsvno_relpathbuf(ndp);
1200 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1201 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1202 struct nfsexstuff *exp)
1207 vrele(ndp->ni_startdir);
1208 nfsvno_relpathbuf(ndp);
1209 if (ndp->ni_dvp == ndp->ni_vp)
1218 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1219 &nvap->na_vattr, pathcp);
1221 vrele(ndp->ni_startdir);
1222 nfsvno_relpathbuf(ndp);
1224 * Although FreeBSD still had the lookup code in
1225 * it for 7/current, there doesn't seem to be any
1226 * point, since VOP_SYMLINK() returns the ni_vp.
1227 * Just vput it for v2.
1229 if (!not_v2 && !error)
1238 * Parse symbolic link arguments.
1239 * This function has an ugly side effect. It will malloc() an area for
1240 * the symlink and set iov_base to point to it, only if it succeeds.
1241 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1245 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1246 struct thread *p, char **pathcpp, int *lenp)
1249 char *pathcp = NULL;
1251 struct nfsv2_sattr *sp;
1255 if ((nd->nd_flag & ND_NFSV3) &&
1256 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1258 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1259 len = fxdr_unsigned(int, *tl);
1260 if (len > NFS_MAXPATHLEN || len <= 0) {
1264 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1265 error = nfsrv_mtostr(nd, pathcp, len);
1268 if (nd->nd_flag & ND_NFSV2) {
1269 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1270 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1274 NFSEXITCODE2(0, nd);
1278 free(pathcp, M_TEMP);
1279 NFSEXITCODE2(error, nd);
1284 * Remove a non-directory object.
1287 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1288 struct thread *p, struct nfsexstuff *exp)
1290 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1291 int error = 0, mirrorcnt;
1292 char fname[PNFS_FILENAME_LEN + 1];
1297 if (vp->v_type == VDIR)
1298 error = NFSERR_ISDIR;
1300 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1303 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1305 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1306 if (error == 0 && dsdvp[0] != NULL)
1307 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1308 if (ndp->ni_dvp == vp)
1313 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1314 nfsvno_relpathbuf(ndp);
1320 * Remove a directory.
1323 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1324 struct thread *p, struct nfsexstuff *exp)
1330 if (vp->v_type != VDIR) {
1335 * No rmdir "." please.
1337 if (ndp->ni_dvp == vp) {
1342 * The root of a mounted filesystem cannot be deleted.
1344 if (vp->v_vflag & VV_ROOT)
1348 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1349 if (ndp->ni_dvp == vp)
1354 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0)
1355 nfsvno_relpathbuf(ndp);
1364 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1365 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1367 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1368 int error = 0, mirrorcnt;
1369 char fname[PNFS_FILENAME_LEN + 1];
1373 fvp = fromndp->ni_vp;
1375 vrele(fromndp->ni_dvp);
1380 tdvp = tondp->ni_dvp;
1383 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1384 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1386 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1387 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1390 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1391 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1396 * A rename to '.' or '..' results in a prematurely
1397 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1400 if ((tondp->ni_cnd.cn_namelen == 1 &&
1401 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1402 (tondp->ni_cnd.cn_namelen == 2 &&
1403 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1404 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1409 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1410 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1413 if (fvp->v_mount != tdvp->v_mount) {
1414 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1418 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1423 * If source and destination are the same, there is nothing to
1424 * do. Set error to -1 to indicate this.
1429 if (ndflag & ND_NFSV4) {
1430 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1431 error = nfsrv_checkremove(fvp, 0, NULL,
1432 (nfsquad_t)((u_quad_t)0), p);
1437 error = nfsrv_checkremove(tvp, 1, NULL,
1438 (nfsquad_t)((u_quad_t)0), p);
1441 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1442 * that the NFSv4 client won't be confused by the rename.
1443 * Since nfsd_recalldelegation() can only be called on an
1444 * unlocked vnode at this point and fvp is the file that will
1445 * still exist after the rename, just do fvp.
1447 nfsd_recalldelegation(fvp, p);
1449 if (error == 0 && tvp != NULL) {
1450 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1451 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1452 " dsdvp=%p\n", dsdvp[0]);
1456 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1457 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1466 vrele(fromndp->ni_dvp);
1473 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1474 * if the rename succeeded, the DS file for the tvp needs to be
1477 if (error == 0 && dsdvp[0] != NULL) {
1478 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1479 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1482 vrele(tondp->ni_startdir);
1483 nfsvno_relpathbuf(tondp);
1485 vrele(fromndp->ni_startdir);
1486 nfsvno_relpathbuf(fromndp);
1495 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1496 struct thread *p, struct nfsexstuff *exp)
1506 if (vp->v_mount != xp->v_mount)
1510 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1511 if (!VN_IS_DOOMED(vp))
1512 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1515 if (ndp->ni_dvp == vp)
1521 if (ndp->ni_dvp == ndp->ni_vp)
1528 nfsvno_relpathbuf(ndp);
1534 * Do the fsync() appropriate for the commit.
1537 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1543 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1544 * file is done. At this time VOP_FSYNC does not accept offset and
1545 * byte count parameters so call VOP_FSYNC the whole file for now.
1546 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1547 * File systems that do not use the buffer cache (as indicated
1548 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1550 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1551 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1553 * Give up and do the whole thing
1555 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1556 VM_OBJECT_WLOCK(vp->v_object);
1557 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1558 VM_OBJECT_WUNLOCK(vp->v_object);
1560 error = VOP_FSYNC(vp, MNT_WAIT, td);
1563 * Locate and synchronously write any buffers that fall
1564 * into the requested range. Note: we are assuming that
1565 * f_iosize is a power of 2.
1567 int iosize = vp->v_mount->mnt_stat.f_iosize;
1568 int iomask = iosize - 1;
1573 * Align to iosize boundary, super-align to page boundary.
1576 cnt += off & iomask;
1577 off &= ~(u_quad_t)iomask;
1579 if (off & PAGE_MASK) {
1580 cnt += off & PAGE_MASK;
1581 off &= ~(u_quad_t)PAGE_MASK;
1583 lblkno = off / iosize;
1585 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1586 VM_OBJECT_WLOCK(vp->v_object);
1587 vm_object_page_clean(vp->v_object, off, off + cnt,
1589 VM_OBJECT_WUNLOCK(vp->v_object);
1598 * If we have a buffer and it is marked B_DELWRI we
1599 * have to lock and write it. Otherwise the prior
1600 * write is assumed to have already been committed.
1602 * gbincore() can return invalid buffers now so we
1603 * have to check that bit as well (though B_DELWRI
1604 * should not be set if B_INVAL is set there could be
1605 * a race here since we haven't locked the buffer).
1607 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1608 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1609 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1611 continue; /* retry */
1613 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1616 bp->b_flags &= ~B_ASYNC;
1639 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1645 if (nfsrv_devidcnt > 0) {
1646 /* For a pNFS service, get the DS numbers. */
1647 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1648 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1654 error = VFS_STATFS(vp->v_mount, sf);
1657 sf->f_blocks = tsf->f_blocks;
1658 sf->f_bavail = tsf->f_bavail;
1659 sf->f_bfree = tsf->f_bfree;
1660 sf->f_bsize = tsf->f_bsize;
1663 * Since NFS handles these values as unsigned on the
1664 * wire, there is no way to represent negative values,
1665 * so set them to 0. Without this, they will appear
1666 * to be very large positive values for clients like
1669 if (sf->f_bavail < 0)
1671 if (sf->f_ffree < 0)
1680 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1681 * must handle nfsrv_opencheck() calls after any other access checks.
1684 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1685 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1686 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1687 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred,
1688 struct nfsexstuff *exp, struct vnode **vpp)
1690 struct vnode *vp = NULL;
1692 struct nfsexstuff nes;
1693 struct thread *p = curthread;
1695 if (ndp->ni_vp == NULL)
1696 nd->nd_repstat = nfsrv_opencheck(clientid,
1697 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1698 if (!nd->nd_repstat) {
1699 if (ndp->ni_vp == NULL) {
1700 vrele(ndp->ni_startdir);
1701 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1702 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1703 /* For a pNFS server, create the data file on a DS. */
1704 if (nd->nd_repstat == 0) {
1706 * Create a data file on a DS for a pNFS server.
1707 * This function just returns if not
1708 * running a pNFS DS or the creation fails.
1710 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1714 nfsvno_relpathbuf(ndp);
1715 if (!nd->nd_repstat) {
1716 if (*exclusive_flagp) {
1717 *exclusive_flagp = 0;
1718 NFSVNO_ATTRINIT(nvap);
1719 nvap->na_atime.tv_sec = cverf[0];
1720 nvap->na_atime.tv_nsec = cverf[1];
1721 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1722 &nvap->na_vattr, cred);
1723 if (nd->nd_repstat != 0) {
1726 nd->nd_repstat = NFSERR_NOTSUPP;
1728 NFSSETBIT_ATTRBIT(attrbitp,
1729 NFSATTRBIT_TIMEACCESS);
1731 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1732 aclp, p, attrbitp, exp);
1737 if (ndp->ni_startdir)
1738 vrele(ndp->ni_startdir);
1739 nfsvno_relpathbuf(ndp);
1741 if (create == NFSV4OPEN_CREATE) {
1742 if (ndp->ni_dvp == vp)
1747 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1748 if (ndp->ni_cnd.cn_flags & RDONLY)
1749 NFSVNO_SETEXRDONLY(&nes);
1751 NFSVNO_EXINIT(&nes);
1752 nd->nd_repstat = nfsvno_accchk(vp,
1753 VWRITE, cred, &nes, p,
1754 NFSACCCHK_NOOVERRIDE,
1755 NFSACCCHK_VPISLOCKED, NULL);
1756 nd->nd_repstat = nfsrv_opencheck(clientid,
1757 stateidp, stp, vp, nd, p, nd->nd_repstat);
1758 if (!nd->nd_repstat) {
1759 tempsize = nvap->na_size;
1760 NFSVNO_ATTRINIT(nvap);
1761 nvap->na_size = tempsize;
1762 nd->nd_repstat = VOP_SETATTR(vp,
1763 &nvap->na_vattr, cred);
1765 } else if (vp->v_type == VREG) {
1766 nd->nd_repstat = nfsrv_opencheck(clientid,
1767 stateidp, stp, vp, nd, p, nd->nd_repstat);
1771 if (ndp->ni_cnd.cn_flags & HASBUF)
1772 nfsvno_relpathbuf(ndp);
1773 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) {
1774 vrele(ndp->ni_startdir);
1775 if (ndp->ni_dvp == ndp->ni_vp)
1785 NFSEXITCODE2(0, nd);
1789 * Updates the file rev and sets the mtime and ctime
1790 * to the current clock time, returning the va_filerev and va_Xtime
1792 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1795 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1796 struct nfsrv_descript *nd, struct thread *p)
1801 vfs_timestamp(&va.va_mtime);
1802 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1803 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1804 if (VN_IS_DOOMED(vp))
1807 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1808 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1813 * Glue routine to nfsv4_fillattr().
1816 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1817 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1818 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1819 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1825 if (nfsrv_devidcnt > 0 &&
1826 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1827 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1828 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1829 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1830 error = nfsrv_pnfsstatfs(sf, mp);
1836 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1837 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1838 mounted_on_fileno, sf);
1840 NFSEXITCODE2(0, nd);
1844 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1846 * nfs readdir service
1847 * - mallocs what it thinks is enough to read
1848 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1849 * - calls VOP_READDIR()
1850 * - loops around building the reply
1851 * if the output generated exceeds count break out of loop
1852 * The NFSM_CLGET macro is used here so that the reply will be packed
1853 * tightly in mbuf clusters.
1854 * - it trims out records with d_fileno == 0
1855 * this doesn't matter for Unix clients, but they might confuse clients
1857 * - it trims out records with d_type == DT_WHT
1858 * these cannot be seen through NFS (unless we extend the protocol)
1859 * The alternate call nfsrvd_readdirplus() does lookups as well.
1860 * PS: The NFS protocol spec. does not clarify what the "count" byte
1861 * argument is a count of.. just name strings and file id's or the
1862 * entire reply rpc or ...
1863 * I tried just file name and id sizes and it confused the Sun client,
1864 * so I am using the full rpc size now. The "paranoia.." comment refers
1865 * to including the status longwords that are not a part of the dir.
1866 * "entry" structures, but are in the rpc.
1869 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
1870 struct vnode *vp, struct nfsexstuff *exp)
1875 char *cpos, *cend, *rbuf;
1877 int nlen, error = 0, getret = 1;
1878 int siz, cnt, fullsiz, eofflag, ncookies;
1879 u_int64_t off, toff, verf __unused;
1880 u_long *cookies = NULL, *cookiep;
1884 struct thread *p = curthread;
1886 if (nd->nd_repstat) {
1887 nfsrv_postopattr(nd, getret, &at);
1890 if (nd->nd_flag & ND_NFSV2) {
1891 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1892 off = fxdr_unsigned(u_quad_t, *tl++);
1894 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1895 off = fxdr_hyper(tl);
1897 verf = fxdr_hyper(tl);
1901 cnt = fxdr_unsigned(int, *tl);
1902 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
1903 cnt = NFS_SRVMAXDATA(nd);
1904 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
1906 if (nd->nd_flag & ND_NFSV3) {
1907 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
1911 * va_filerev is not sufficient as a cookie verifier,
1912 * since it is not supposed to change when entries are
1913 * removed/added unless that offset cookies returned to
1914 * the client are no longer valid.
1916 if (!nd->nd_repstat && toff && verf != at.na_filerev)
1917 nd->nd_repstat = NFSERR_BAD_COOKIE;
1920 if (!nd->nd_repstat && vp->v_type != VDIR)
1921 nd->nd_repstat = NFSERR_NOTDIR;
1922 if (nd->nd_repstat == 0 && cnt == 0) {
1923 if (nd->nd_flag & ND_NFSV2)
1924 /* NFSv2 does not have NFSERR_TOOSMALL */
1925 nd->nd_repstat = EPERM;
1927 nd->nd_repstat = NFSERR_TOOSMALL;
1929 if (!nd->nd_repstat)
1930 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
1931 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1932 NFSACCCHK_VPISLOCKED, NULL);
1933 if (nd->nd_repstat) {
1935 if (nd->nd_flag & ND_NFSV3)
1936 nfsrv_postopattr(nd, getret, &at);
1939 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
1940 rbuf = malloc(siz, M_TEMP, M_WAITOK);
1944 free(cookies, M_TEMP);
1952 io.uio_offset = (off_t)off;
1954 io.uio_segflg = UIO_SYSSPACE;
1955 io.uio_rw = UIO_READ;
1957 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
1959 off = (u_int64_t)io.uio_offset;
1961 siz -= io.uio_resid;
1963 if (!cookies && !nd->nd_repstat)
1964 nd->nd_repstat = NFSERR_PERM;
1965 if (nd->nd_flag & ND_NFSV3) {
1966 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
1967 if (!nd->nd_repstat)
1968 nd->nd_repstat = getret;
1972 * Handles the failed cases. nd->nd_repstat == 0 past here.
1974 if (nd->nd_repstat) {
1978 free(cookies, M_TEMP);
1979 if (nd->nd_flag & ND_NFSV3)
1980 nfsrv_postopattr(nd, getret, &at);
1984 * If nothing read, return eof
1989 if (nd->nd_flag & ND_NFSV2) {
1990 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1992 nfsrv_postopattr(nd, getret, &at);
1993 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
1994 txdr_hyper(at.na_filerev, tl);
1997 *tl++ = newnfs_false;
2000 free(cookies, M_TEMP);
2005 * Check for degenerate cases of nothing useful read.
2006 * If so go try again
2010 dp = (struct dirent *)cpos;
2014 * For some reason FreeBSD's ufs_readdir() chooses to back the
2015 * directory offset up to a block boundary, so it is necessary to
2016 * skip over the records that precede the requested offset. This
2017 * requires the assumption that file offset cookies monotonically
2020 while (cpos < cend && ncookies > 0 &&
2021 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2022 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2023 cpos += dp->d_reclen;
2024 dp = (struct dirent *)cpos;
2028 if (cpos >= cend || ncookies == 0) {
2036 * dirlen is the size of the reply, including all XDR and must
2037 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2038 * if the XDR should be included in "count", but to be safe, we do.
2039 * (Include the two booleans at the end of the reply in dirlen now.)
2041 if (nd->nd_flag & ND_NFSV3) {
2042 nfsrv_postopattr(nd, getret, &at);
2043 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2044 txdr_hyper(at.na_filerev, tl);
2045 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2047 dirlen = 2 * NFSX_UNSIGNED;
2050 /* Loop through the records and build reply */
2051 while (cpos < cend && ncookies > 0) {
2052 nlen = dp->d_namlen;
2053 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2054 nlen <= NFS_MAXNAMLEN) {
2055 if (nd->nd_flag & ND_NFSV3)
2056 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2058 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2065 * Build the directory record xdr from
2068 if (nd->nd_flag & ND_NFSV3) {
2069 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2070 *tl++ = newnfs_true;
2073 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2074 *tl++ = newnfs_true;
2076 *tl = txdr_unsigned(dp->d_fileno);
2077 (void) nfsm_strtom(nd, dp->d_name, nlen);
2078 if (nd->nd_flag & ND_NFSV3) {
2079 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2082 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2083 *tl = txdr_unsigned(*cookiep);
2085 cpos += dp->d_reclen;
2086 dp = (struct dirent *)cpos;
2092 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2093 *tl++ = newnfs_false;
2099 free(cookies, M_TEMP);
2102 NFSEXITCODE2(0, nd);
2106 NFSEXITCODE2(error, nd);
2111 * Readdirplus for V3 and Readdir for V4.
2114 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2115 struct vnode *vp, struct nfsexstuff *exp)
2120 char *cpos, *cend, *rbuf;
2123 struct nfsvattr nva, at, *nvap = &nva;
2124 struct mbuf *mb0, *mb1;
2125 struct nfsreferral *refp;
2126 int nlen, r, error = 0, getret = 1, usevget = 1;
2127 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2128 caddr_t bpos0, bpos1;
2129 u_int64_t off, toff, verf;
2130 u_long *cookies = NULL, *cookiep;
2131 nfsattrbit_t attrbits, rderrbits, savbits;
2134 struct componentname cn;
2135 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2136 struct mount *mp, *new_mp;
2137 uint64_t mounted_on_fileno;
2138 struct thread *p = curthread;
2140 if (nd->nd_repstat) {
2141 nfsrv_postopattr(nd, getret, &at);
2144 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2145 off = fxdr_hyper(tl);
2148 verf = fxdr_hyper(tl);
2150 siz = fxdr_unsigned(int, *tl++);
2151 cnt = fxdr_unsigned(int, *tl);
2154 * Use the server's maximum data transfer size as the upper bound
2157 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2158 cnt = NFS_SRVMAXDATA(nd);
2161 * siz is a "hint" of how much directory information (name, fileid,
2162 * cookie) should be in the reply. At least one client "hints" 0,
2163 * so I set it to cnt for that case. I also round it up to the
2164 * next multiple of DIRBLKSIZ.
2165 * Since the size of a Readdirplus directory entry reply will always
2166 * be greater than a directory entry returned by VOP_READDIR(), it
2167 * does not make sense to read more than NFS_SRVMAXDATA() via
2172 else if (siz > NFS_SRVMAXDATA(nd))
2173 siz = NFS_SRVMAXDATA(nd);
2174 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2176 if (nd->nd_flag & ND_NFSV4) {
2177 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2180 NFSSET_ATTRBIT(&savbits, &attrbits);
2181 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2182 NFSZERO_ATTRBIT(&rderrbits);
2183 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2185 NFSZERO_ATTRBIT(&attrbits);
2188 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2190 if (!nd->nd_repstat) {
2191 if (off && verf != at.na_filerev) {
2193 * va_filerev is not sufficient as a cookie verifier,
2194 * since it is not supposed to change when entries are
2195 * removed/added unless that offset cookies returned to
2196 * the client are no longer valid.
2198 if (nd->nd_flag & ND_NFSV4) {
2199 nd->nd_repstat = NFSERR_NOTSAME;
2201 nd->nd_repstat = NFSERR_BAD_COOKIE;
2206 if (!nd->nd_repstat && vp->v_type != VDIR)
2207 nd->nd_repstat = NFSERR_NOTDIR;
2208 if (!nd->nd_repstat && cnt == 0)
2209 nd->nd_repstat = NFSERR_TOOSMALL;
2210 if (!nd->nd_repstat)
2211 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2212 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2213 NFSACCCHK_VPISLOCKED, NULL);
2214 if (nd->nd_repstat) {
2216 if (nd->nd_flag & ND_NFSV3)
2217 nfsrv_postopattr(nd, getret, &at);
2220 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2221 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2223 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2227 free(cookies, M_TEMP);
2235 io.uio_offset = (off_t)off;
2237 io.uio_segflg = UIO_SYSSPACE;
2238 io.uio_rw = UIO_READ;
2240 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2242 off = (u_int64_t)io.uio_offset;
2244 siz -= io.uio_resid;
2246 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2248 if (!cookies && !nd->nd_repstat)
2249 nd->nd_repstat = NFSERR_PERM;
2250 if (!nd->nd_repstat)
2251 nd->nd_repstat = getret;
2252 if (nd->nd_repstat) {
2255 free(cookies, M_TEMP);
2257 if (nd->nd_flag & ND_NFSV3)
2258 nfsrv_postopattr(nd, getret, &at);
2262 * If nothing read, return eof
2267 if (nd->nd_flag & ND_NFSV3)
2268 nfsrv_postopattr(nd, getret, &at);
2269 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2270 txdr_hyper(at.na_filerev, tl);
2272 *tl++ = newnfs_false;
2274 free(cookies, M_TEMP);
2280 * Check for degenerate cases of nothing useful read.
2281 * If so go try again
2285 dp = (struct dirent *)cpos;
2289 * For some reason FreeBSD's ufs_readdir() chooses to back the
2290 * directory offset up to a block boundary, so it is necessary to
2291 * skip over the records that precede the requested offset. This
2292 * requires the assumption that file offset cookies monotonically
2295 while (cpos < cend && ncookies > 0 &&
2296 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2297 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2298 ((nd->nd_flag & ND_NFSV4) &&
2299 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2300 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2301 cpos += dp->d_reclen;
2302 dp = (struct dirent *)cpos;
2306 if (cpos >= cend || ncookies == 0) {
2313 * Busy the file system so that the mount point won't go away
2314 * and, as such, VFS_VGET() can be used safely.
2319 nd->nd_repstat = vfs_busy(mp, 0);
2321 if (nd->nd_repstat != 0) {
2323 free(cookies, M_TEMP);
2325 if (nd->nd_flag & ND_NFSV3)
2326 nfsrv_postopattr(nd, getret, &at);
2331 * Check to see if entries in this directory can be safely acquired
2332 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2333 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2334 * automount of the snapshot directory that is required will
2336 * This needs to be done here for NFSv4, since NFSv4 never does
2337 * a VFS_VGET() for "." or "..".
2340 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2341 if (r == EOPNOTSUPP) {
2343 cn.cn_nameiop = LOOKUP;
2344 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2345 cn.cn_cred = nd->nd_cred;
2352 * Save this position, in case there is an error before one entry
2356 bpos0 = nd->nd_bpos;
2359 * Fill in the first part of the reply.
2360 * dirlen is the reply length in bytes and cannot exceed cnt.
2361 * (Include the two booleans at the end of the reply in dirlen now,
2362 * so we recognize when we have exceeded cnt.)
2364 if (nd->nd_flag & ND_NFSV3) {
2365 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2366 nfsrv_postopattr(nd, getret, &at);
2368 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2370 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2371 txdr_hyper(at.na_filerev, tl);
2374 * Save this position, in case there is an empty reply needed.
2377 bpos1 = nd->nd_bpos;
2379 /* Loop through the records and build reply */
2381 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2382 nlen = dp->d_namlen;
2383 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2384 nlen <= NFS_MAXNAMLEN &&
2385 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2386 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2387 || (nlen == 1 && dp->d_name[0] != '.'))) {
2389 * Save the current position in the reply, in case
2390 * this entry exceeds cnt.
2393 bpos1 = nd->nd_bpos;
2396 * For readdir_and_lookup get the vnode using
2405 mounted_on_fileno = (uint64_t)dp->d_fileno;
2406 if ((nd->nd_flag & ND_NFSV3) ||
2407 NFSNONZERO_ATTRBIT(&savbits)) {
2408 if (nd->nd_flag & ND_NFSV4)
2409 refp = nfsv4root_getreferral(NULL,
2413 r = VFS_VGET(mp, dp->d_fileno,
2417 if (r == EOPNOTSUPP) {
2420 cn.cn_nameiop = LOOKUP;
2428 cn.cn_nameptr = dp->d_name;
2429 cn.cn_namelen = nlen;
2430 cn.cn_flags = ISLASTCN |
2431 NOFOLLOW | LOCKLEAF;
2433 dp->d_name[0] == '.' &&
2434 dp->d_name[1] == '.')
2437 if (NFSVOPLOCK(vp, LK_SHARED)
2439 nd->nd_repstat = EPERM;
2442 if ((vp->v_vflag & VV_ROOT) != 0
2443 && (cn.cn_flags & ISDOTDOT)
2449 r = VOP_LOOKUP(vp, &nvp,
2457 * For NFSv4, check to see if nvp is
2458 * a mount point and get the mount
2459 * point vnode, as required.
2462 nfsrv_enable_crossmntpt != 0 &&
2463 (nd->nd_flag & ND_NFSV4) != 0 &&
2464 nvp->v_type == VDIR &&
2465 nvp->v_mountedhere != NULL) {
2466 new_mp = nvp->v_mountedhere;
2467 r = vfs_busy(new_mp, 0);
2471 r = VFS_ROOT(new_mp,
2481 * If we failed to look up the entry, then it
2482 * has become invalid, most likely removed.
2489 KASSERT(refp != NULL || nvp != NULL,
2490 ("%s: undetected lookup error", __func__));
2493 ((nd->nd_flag & ND_NFSV3) ||
2494 NFSNONZERO_ATTRBIT(&attrbits))) {
2495 r = nfsvno_getfh(nvp, &nfh, p);
2497 r = nfsvno_getattr(nvp, nvap, nd, p,
2499 if (r == 0 && is_zfs == 1 &&
2500 nfsrv_enable_crossmntpt != 0 &&
2501 (nd->nd_flag & ND_NFSV4) != 0 &&
2502 nvp->v_type == VDIR &&
2503 vp->v_mount != nvp->v_mount) {
2505 * For a ZFS snapshot, there is a
2506 * pseudo mount that does not set
2507 * v_mountedhere, so it needs to
2508 * be detected via a different
2513 new_mp = nvp->v_mount;
2518 * If we failed to get attributes of the entry,
2519 * then just skip it for NFSv3 (the traditional
2520 * behavior in the old NFS server).
2521 * For NFSv4 the behavior is controlled by
2522 * RDATTRERROR: we either ignore the error or
2524 * Note that RDATTRERROR is never set for NFSv3.
2527 if (!NFSISSET_ATTRBIT(&attrbits,
2528 NFSATTRBIT_RDATTRERROR)) {
2530 if (needs_unbusy != 0)
2532 if ((nd->nd_flag & ND_NFSV3))
2541 * Build the directory record xdr
2543 if (nd->nd_flag & ND_NFSV3) {
2544 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2545 *tl++ = newnfs_true;
2547 *tl = txdr_unsigned(dp->d_fileno);
2548 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2549 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2551 *tl = txdr_unsigned(*cookiep);
2552 nfsrv_postopattr(nd, 0, nvap);
2553 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2554 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2558 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2559 *tl++ = newnfs_true;
2561 *tl = txdr_unsigned(*cookiep);
2562 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2564 supports_nfsv4acls =
2565 nfs_supportsnfsv4acls(nvp);
2568 supports_nfsv4acls = 0;
2570 dirlen += nfsrv_putreferralattr(nd,
2573 if (nd->nd_repstat) {
2576 if (needs_unbusy != 0)
2581 dirlen += nfsvno_fillattr(nd, new_mp,
2582 nvp, nvap, &nfh, r, &rderrbits,
2583 nd->nd_cred, p, isdgram, 0,
2584 supports_nfsv4acls, at_root,
2587 dirlen += nfsvno_fillattr(nd, new_mp,
2588 nvp, nvap, &nfh, r, &attrbits,
2589 nd->nd_cred, p, isdgram, 0,
2590 supports_nfsv4acls, at_root,
2595 dirlen += (3 * NFSX_UNSIGNED);
2597 if (needs_unbusy != 0)
2603 cpos += dp->d_reclen;
2604 dp = (struct dirent *)cpos;
2612 * If dirlen > cnt, we must strip off the last entry. If that
2613 * results in an empty reply, report NFSERR_TOOSMALL.
2615 if (dirlen > cnt || nd->nd_repstat) {
2616 if (!nd->nd_repstat && entrycnt == 0)
2617 nd->nd_repstat = NFSERR_TOOSMALL;
2618 if (nd->nd_repstat) {
2619 newnfs_trimtrailing(nd, mb0, bpos0);
2620 if (nd->nd_flag & ND_NFSV3)
2621 nfsrv_postopattr(nd, getret, &at);
2623 newnfs_trimtrailing(nd, mb1, bpos1);
2625 } else if (cpos < cend)
2627 if (!nd->nd_repstat) {
2628 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2629 *tl++ = newnfs_false;
2635 free(cookies, M_TEMP);
2639 NFSEXITCODE2(0, nd);
2643 NFSEXITCODE2(error, nd);
2648 * Get the settable attributes out of the mbuf list.
2649 * (Return 0 or EBADRPC)
2652 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2653 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2656 struct nfsv2_sattr *sp;
2657 int error = 0, toclient = 0;
2659 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2661 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2663 * Some old clients didn't fill in the high order 16bits.
2664 * --> check the low order 2 bytes for 0xffff
2666 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2667 nvap->na_mode = nfstov_mode(sp->sa_mode);
2668 if (sp->sa_uid != newnfs_xdrneg1)
2669 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2670 if (sp->sa_gid != newnfs_xdrneg1)
2671 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2672 if (sp->sa_size != newnfs_xdrneg1)
2673 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2674 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2676 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2678 nvap->na_atime.tv_sec =
2679 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2680 nvap->na_atime.tv_nsec = 0;
2683 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2684 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2687 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2688 if (*tl == newnfs_true) {
2689 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2690 nvap->na_mode = nfstov_mode(*tl);
2692 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2693 if (*tl == newnfs_true) {
2694 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2695 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2697 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2698 if (*tl == newnfs_true) {
2699 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2700 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2702 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2703 if (*tl == newnfs_true) {
2704 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2705 nvap->na_size = fxdr_hyper(tl);
2707 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2708 switch (fxdr_unsigned(int, *tl)) {
2709 case NFSV3SATTRTIME_TOCLIENT:
2710 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2711 fxdr_nfsv3time(tl, &nvap->na_atime);
2714 case NFSV3SATTRTIME_TOSERVER:
2715 vfs_timestamp(&nvap->na_atime);
2716 nvap->na_vaflags |= VA_UTIMES_NULL;
2719 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2720 switch (fxdr_unsigned(int, *tl)) {
2721 case NFSV3SATTRTIME_TOCLIENT:
2722 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2723 fxdr_nfsv3time(tl, &nvap->na_mtime);
2724 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2726 case NFSV3SATTRTIME_TOSERVER:
2727 vfs_timestamp(&nvap->na_mtime);
2729 nvap->na_vaflags |= VA_UTIMES_NULL;
2734 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2737 NFSEXITCODE2(error, nd);
2742 * Handle the setable attributes for V4.
2743 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2746 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2747 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2752 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2753 int moderet, toclient = 0;
2754 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2757 u_short mode, mask; /* Same type as va_mode. */
2760 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2763 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2764 attrsize = fxdr_unsigned(int, *tl);
2767 * Loop around getting the setable attributes. If an unsupported
2768 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2771 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2772 bitpos = NFSATTRBIT_MAX;
2777 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2778 if (attrsum > attrsize) {
2779 error = NFSERR_BADXDR;
2782 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2784 case NFSATTRBIT_SIZE:
2785 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2786 if (vp != NULL && vp->v_type != VREG) {
2787 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2791 nvap->na_size = fxdr_hyper(tl);
2792 attrsum += NFSX_HYPER;
2794 case NFSATTRBIT_ACL:
2795 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize,
2799 if (aceerr && !nd->nd_repstat)
2800 nd->nd_repstat = aceerr;
2803 case NFSATTRBIT_ARCHIVE:
2804 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2805 if (!nd->nd_repstat)
2806 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2807 attrsum += NFSX_UNSIGNED;
2809 case NFSATTRBIT_HIDDEN:
2810 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2811 if (!nd->nd_repstat)
2812 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2813 attrsum += NFSX_UNSIGNED;
2815 case NFSATTRBIT_MIMETYPE:
2816 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2817 i = fxdr_unsigned(int, *tl);
2818 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
2821 if (!nd->nd_repstat)
2822 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2823 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
2825 case NFSATTRBIT_MODE:
2826 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
2827 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2828 nvap->na_mode = nfstov_mode(*tl);
2829 attrsum += NFSX_UNSIGNED;
2831 case NFSATTRBIT_OWNER:
2832 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2833 j = fxdr_unsigned(int, *tl);
2835 error = NFSERR_BADXDR;
2838 if (j > NFSV4_SMALLSTR)
2839 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2842 error = nfsrv_mtostr(nd, cp, j);
2844 if (j > NFSV4_SMALLSTR)
2845 free(cp, M_NFSSTRING);
2848 if (!nd->nd_repstat) {
2849 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
2851 if (!nd->nd_repstat)
2854 if (j > NFSV4_SMALLSTR)
2855 free(cp, M_NFSSTRING);
2856 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2858 case NFSATTRBIT_OWNERGROUP:
2859 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2860 j = fxdr_unsigned(int, *tl);
2862 error = NFSERR_BADXDR;
2865 if (j > NFSV4_SMALLSTR)
2866 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
2869 error = nfsrv_mtostr(nd, cp, j);
2871 if (j > NFSV4_SMALLSTR)
2872 free(cp, M_NFSSTRING);
2875 if (!nd->nd_repstat) {
2876 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
2878 if (!nd->nd_repstat)
2881 if (j > NFSV4_SMALLSTR)
2882 free(cp, M_NFSSTRING);
2883 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
2885 case NFSATTRBIT_SYSTEM:
2886 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2887 if (!nd->nd_repstat)
2888 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2889 attrsum += NFSX_UNSIGNED;
2891 case NFSATTRBIT_TIMEACCESSSET:
2892 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2893 attrsum += NFSX_UNSIGNED;
2894 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2895 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2896 fxdr_nfsv4time(tl, &nvap->na_atime);
2898 attrsum += NFSX_V4TIME;
2900 vfs_timestamp(&nvap->na_atime);
2901 nvap->na_vaflags |= VA_UTIMES_NULL;
2904 case NFSATTRBIT_TIMEBACKUP:
2905 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2906 if (!nd->nd_repstat)
2907 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2908 attrsum += NFSX_V4TIME;
2910 case NFSATTRBIT_TIMECREATE:
2911 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2912 if (!nd->nd_repstat)
2913 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2914 attrsum += NFSX_V4TIME;
2916 case NFSATTRBIT_TIMEMODIFYSET:
2917 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2918 attrsum += NFSX_UNSIGNED;
2919 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
2920 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
2921 fxdr_nfsv4time(tl, &nvap->na_mtime);
2922 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2923 attrsum += NFSX_V4TIME;
2925 vfs_timestamp(&nvap->na_mtime);
2927 nvap->na_vaflags |= VA_UTIMES_NULL;
2930 case NFSATTRBIT_MODESETMASKED:
2931 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
2932 mode = fxdr_unsigned(u_short, *tl++);
2933 mask = fxdr_unsigned(u_short, *tl);
2935 * vp == NULL implies an Open/Create operation.
2936 * This attribute can only be used for Setattr and
2937 * only for NFSv4.1 or higher.
2938 * If moderet != 0, a mode attribute has also been
2939 * specified and this attribute cannot be done in the
2940 * same Setattr operation.
2942 if ((nd->nd_flag & ND_NFSV41) == 0)
2943 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2944 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
2946 nd->nd_repstat = NFSERR_INVAL;
2947 else if (moderet == 0)
2948 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
2950 nvap->na_mode = (mode & mask) |
2951 (va.va_mode & ~mask);
2953 nd->nd_repstat = moderet;
2954 attrsum += 2 * NFSX_UNSIGNED;
2957 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2959 * set bitpos so we drop out of the loop.
2961 bitpos = NFSATTRBIT_MAX;
2967 * some clients pad the attrlist, so we need to skip over the
2970 if (attrsum > attrsize) {
2971 error = NFSERR_BADXDR;
2973 attrsize = NFSM_RNDUP(attrsize);
2974 if (attrsum < attrsize)
2975 error = nfsm_advance(nd, attrsize - attrsum, -1);
2978 NFSEXITCODE2(error, nd);
2983 * Check/setup export credentials.
2986 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
2987 struct ucred *credanon)
2992 * Check/setup credentials.
2994 if (nd->nd_flag & ND_GSS)
2995 exp->nes_exflag &= ~MNT_EXPORTANON;
2998 * Check to see if the operation is allowed for this security flavor.
2999 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to
3000 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
3001 * Also, allow Secinfo, so that it can acquire the correct flavor(s).
3003 if (nfsvno_testexp(nd, exp) &&
3004 nd->nd_procnum != NFSV4OP_SECINFO &&
3005 nd->nd_procnum != NFSPROC_FSINFO) {
3006 if (nd->nd_flag & ND_NFSV4)
3007 error = NFSERR_WRONGSEC;
3009 error = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3014 * Check to see if the file system is exported V4 only.
3016 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3017 error = NFSERR_PROGNOTV4;
3022 * Now, map the user credentials.
3023 * (Note that ND_AUTHNONE will only be set for an NFSv3
3024 * Fsinfo RPC. If set for anything else, this code might need
3027 if (NFSVNO_EXPORTED(exp)) {
3028 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3029 NFSVNO_EXPORTANON(exp) ||
3030 (nd->nd_flag & ND_AUTHNONE) != 0) {
3031 nd->nd_cred->cr_uid = credanon->cr_uid;
3032 nd->nd_cred->cr_gid = credanon->cr_gid;
3033 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3034 credanon->cr_groups);
3035 } else if ((nd->nd_flag & ND_GSS) == 0) {
3037 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3038 * if there is a replacement credential with a group
3039 * list set up by "nfsuserd -manage-gids".
3040 * If there is no replacement, nfsrv_getgrpscred()
3041 * simply returns its argument.
3043 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3048 NFSEXITCODE2(error, nd);
3056 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3057 struct ucred **credp)
3059 int i, error, *secflavors;
3061 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3062 &exp->nes_numsecflavor, &secflavors);
3064 if (nfs_rootfhset) {
3065 exp->nes_exflag = 0;
3066 exp->nes_numsecflavor = 0;
3069 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3071 printf("nfsvno_checkexp: numsecflavors out of range\n");
3072 exp->nes_numsecflavor = 0;
3075 /* Copy the security flavors. */
3076 for (i = 0; i < exp->nes_numsecflavor; i++)
3077 exp->nes_secflavors[i] = secflavors[i];
3084 * Get a vnode for a file handle and export stuff.
3087 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3088 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3089 struct ucred **credp)
3091 int i, error, *secflavors;
3094 exp->nes_numsecflavor = 0;
3095 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3097 /* Make sure the server replies ESTALE to the client. */
3099 if (nam && !error) {
3100 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3101 &exp->nes_numsecflavor, &secflavors);
3103 if (nfs_rootfhset) {
3104 exp->nes_exflag = 0;
3105 exp->nes_numsecflavor = 0;
3110 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3112 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3113 exp->nes_numsecflavor = 0;
3117 /* Copy the security flavors. */
3118 for (i = 0; i < exp->nes_numsecflavor; i++)
3119 exp->nes_secflavors[i] = secflavors[i];
3127 * nfsd_fhtovp() - convert a fh to a vnode ptr
3128 * - look up fsid in mount list (if not found ret error)
3129 * - get vp and export rights by calling nfsvno_fhtovp()
3130 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3132 * - if mpp != NULL, return the mount point so that it can
3133 * be used for vn_finished_write() by the caller
3136 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3137 struct vnode **vpp, struct nfsexstuff *exp,
3138 struct mount **mpp, int startwrite)
3141 struct ucred *credanon;
3144 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3146 * Check for the special case of the nfsv4root_fh.
3148 mp = vfs_busyfs(&fhp->fh_fsid);
3153 nd->nd_repstat = ESTALE;
3158 vn_start_write(NULL, mpp, V_WAIT);
3159 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3160 lktype = LK_EXCLUSIVE;
3162 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3167 * For NFSv4 without a pseudo root fs, unexported file handles
3168 * can be returned, so that Lookup works everywhere.
3170 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3171 !(nd->nd_flag & ND_NFSV4)) {
3173 nd->nd_repstat = EACCES;
3177 * Personally, I've never seen any point in requiring a
3178 * reserved port#, since only in the rare case where the
3179 * clients are all boxes with secure system privileges,
3180 * does it provide any enhanced security, but... some people
3181 * believe it to be useful and keep putting this code back in.
3182 * (There is also some "security checker" out there that
3183 * complains if the nfs server doesn't enforce this.)
3184 * However, note the following:
3185 * RFC3530 (NFSv4) specifies that a reserved port# not be
3187 * RFC2623 recommends that, if a reserved port# is checked for,
3188 * that there be a way to turn that off--> ifdef'd.
3190 #ifdef NFS_REQRSVPORT
3191 if (!nd->nd_repstat) {
3192 struct sockaddr_in *saddr;
3193 struct sockaddr_in6 *saddr6;
3195 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3196 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3197 if (!(nd->nd_flag & ND_NFSV4) &&
3198 ((saddr->sin_family == AF_INET &&
3199 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3200 (saddr6->sin6_family == AF_INET6 &&
3201 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3203 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3206 #endif /* NFS_REQRSVPORT */
3209 * Check/setup credentials.
3211 if (!nd->nd_repstat) {
3212 nd->nd_saveduid = nd->nd_cred->cr_uid;
3213 nd->nd_repstat = nfsd_excred(nd, exp, credanon);
3217 if (credanon != NULL)
3219 if (nd->nd_repstat) {
3221 vn_finished_write(mp);
3228 NFSEXITCODE2(0, nd);
3235 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3237 struct filedesc *fdp;
3241 fdp = p->td_proc->p_fd;
3242 if (fd < 0 || fd >= fdp->fd_nfiles ||
3243 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3255 * Called from nfssvc() to update the exports list. Just call
3256 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3257 * in the mount list.
3260 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3262 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3264 struct nameidata nd;
3267 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3268 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3270 else if (error == 0) {
3271 if (nfsexargp->fspec == NULL) {
3276 * If fspec != NULL, this is the v4root path.
3278 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
3279 nfsexargp->fspec, p);
3280 if ((error = namei(&nd)) != 0)
3282 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3285 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3286 NFSBCOPY((caddr_t)&fh,
3287 nfs_rootfh.nfsrvfh_data,
3288 sizeof (fhandle_t));
3299 * This function needs to test to see if the system is near its limit
3300 * for memory allocation via malloc() or mget() and return True iff
3301 * either of these resources are near their limit.
3302 * XXX (For now, this is just a stub.)
3304 int nfsrv_testmalloclimit = 0;
3306 nfsrv_mallocmget_limit(void)
3308 static int printmesg = 0;
3309 static int testval = 1;
3311 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3312 if ((printmesg++ % 100) == 0)
3313 printf("nfsd: malloc/mget near limit\n");
3320 * BSD specific initialization of a mount point.
3325 static int inited = 0;
3330 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3331 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3332 TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3333 nfsv4root_mnt.mnt_export = NULL;
3334 TAILQ_INIT(&nfsv4root_opt);
3335 TAILQ_INIT(&nfsv4root_newopt);
3336 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3337 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3338 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3339 nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3343 * Get a vnode for a file handle, without checking exports, etc.
3346 nfsvno_getvp(fhandle_t *fhp)
3352 mp = vfs_busyfs(&fhp->fh_fsid);
3355 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3363 * Do a local VOP_ADVLOCK().
3366 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3367 u_int64_t end, struct thread *td)
3373 if (nfsrv_dolocallocks == 0)
3375 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3377 fl.l_whence = SEEK_SET;
3379 fl.l_start = (off_t)first;
3380 if (end == NFS64BITSSET) {
3384 fl.l_len = (off_t)tlen;
3387 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3388 * values for all calls, so that all locks will be held by the
3389 * nfsd server. (The nfsd server handles conflicts between the
3391 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3392 * bytes, so it can't be put in l_sysid.
3394 if (nfsv4_sysid == 0)
3395 nfsv4_sysid = nlm_acquire_next_sysid();
3396 fl.l_pid = (pid_t)0;
3397 fl.l_sysid = (int)nfsv4_sysid;
3399 if (ftype == F_UNLCK)
3400 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3401 (F_POSIX | F_REMOTE));
3403 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3404 (F_POSIX | F_REMOTE));
3412 * Check the nfsv4 root exports.
3415 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3417 struct ucred *credanon;
3418 int exflags, error = 0, numsecflavor, *secflavors, i;
3420 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3421 &credanon, &numsecflavor, &secflavors);
3423 error = NFSERR_PROGUNAVAIL;
3426 if (credanon != NULL)
3428 for (i = 0; i < numsecflavor; i++) {
3429 if (secflavors[i] == AUTH_SYS)
3430 nd->nd_flag |= ND_EXAUTHSYS;
3431 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3432 nd->nd_flag |= ND_EXGSS;
3433 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3434 nd->nd_flag |= ND_EXGSSINTEGRITY;
3435 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3436 nd->nd_flag |= ND_EXGSSPRIVACY;
3445 * Nfs server pseudo system call for the nfsd's
3451 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3454 struct nfsd_addsock_args sockarg;
3455 struct nfsd_nfsd_args nfsdarg;
3456 struct nfsd_nfsd_oargs onfsdarg;
3457 struct nfsd_pnfsd_args pnfsdarg;
3458 struct vnode *vp, *nvp, *curdvp;
3459 struct pnfsdsfile *pf;
3460 struct nfsdevice *ds, *fds;
3461 cap_rights_t rights;
3462 int buflen, error, ret;
3463 char *buf, *cp, *cp2, *cp3;
3464 char fname[PNFS_FILENAME_LEN + 1];
3466 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3467 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3471 * Since we don't know what rights might be required,
3472 * pretend that we need them all. It is better to be too
3473 * careful than too reckless.
3475 error = fget(td, sockarg.sock,
3476 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp);
3479 if (fp->f_type != DTYPE_SOCKET) {
3484 error = nfsrvd_addsock(fp);
3486 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3487 if (uap->argp == NULL) {
3491 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3492 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3494 nfsdarg.principal = onfsdarg.principal;
3495 nfsdarg.minthreads = onfsdarg.minthreads;
3496 nfsdarg.maxthreads = onfsdarg.maxthreads;
3497 nfsdarg.version = 1;
3498 nfsdarg.addr = NULL;
3499 nfsdarg.addrlen = 0;
3500 nfsdarg.dnshost = NULL;
3501 nfsdarg.dnshostlen = 0;
3502 nfsdarg.dspath = NULL;
3503 nfsdarg.dspathlen = 0;
3504 nfsdarg.mdspath = NULL;
3505 nfsdarg.mdspathlen = 0;
3506 nfsdarg.mirrorcnt = 1;
3509 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3512 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3513 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3514 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3515 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3516 nfsdarg.mirrorcnt >= 1 &&
3517 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3518 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3519 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3520 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3521 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3522 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3523 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3524 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3525 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3530 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3532 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3533 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3535 free(nfsdarg.addr, M_TEMP);
3539 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3540 nfsdarg.dnshost = cp;
3541 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3542 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3544 free(nfsdarg.addr, M_TEMP);
3545 free(nfsdarg.dnshost, M_TEMP);
3549 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3550 nfsdarg.dspath = cp;
3551 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3552 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3554 free(nfsdarg.addr, M_TEMP);
3555 free(nfsdarg.dnshost, M_TEMP);
3556 free(nfsdarg.dspath, M_TEMP);
3560 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3561 nfsdarg.mdspath = cp;
3563 nfsdarg.addr = NULL;
3564 nfsdarg.addrlen = 0;
3565 nfsdarg.dnshost = NULL;
3566 nfsdarg.dnshostlen = 0;
3567 nfsdarg.dspath = NULL;
3568 nfsdarg.dspathlen = 0;
3569 nfsdarg.mdspath = NULL;
3570 nfsdarg.mdspathlen = 0;
3571 nfsdarg.mirrorcnt = 1;
3573 error = nfsrvd_nfsd(td, &nfsdarg);
3574 free(nfsdarg.addr, M_TEMP);
3575 free(nfsdarg.dnshost, M_TEMP);
3576 free(nfsdarg.dspath, M_TEMP);
3577 free(nfsdarg.mdspath, M_TEMP);
3578 } else if (uap->flag & NFSSVC_PNFSDS) {
3579 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3580 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3581 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3582 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3583 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3586 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3588 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3589 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3590 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3591 buf = malloc(buflen, M_TEMP, M_WAITOK);
3592 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3594 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3595 if (error == 0 && pnfsdarg.dspath != NULL) {
3596 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3597 error = copyinstr(pnfsdarg.dspath, cp2,
3598 PATH_MAX + 1, NULL);
3599 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3603 if (error == 0 && pnfsdarg.curdspath != NULL) {
3604 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3605 error = copyinstr(pnfsdarg.curdspath, cp3,
3606 PATH_MAX + 1, NULL);
3607 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3614 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3615 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3617 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3619 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3620 printf("copymr: dsdir out of range\n");
3623 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3624 error = nfsrv_copymr(vp, nvp,
3625 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3626 (struct pnfsdsfile *)buf,
3627 buflen / sizeof(*pf), td->td_ucred, td);
3630 if (fds != NULL && error == 0) {
3631 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3632 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3634 nfsrv_dsremove(curdvp, fname,
3636 NFSVOPUNLOCK(curdvp);
3639 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3647 error = nfssvc_srvcall(td, uap, td->td_ucred);
3656 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3658 struct nfsex_args export;
3659 struct file *fp = NULL;
3661 struct nfsd_clid adminrevoke;
3662 struct nfsd_dumplist dumplist;
3663 struct nfsd_dumpclients *dumpclients;
3664 struct nfsd_dumplocklist dumplocklist;
3665 struct nfsd_dumplocks *dumplocks;
3666 struct nameidata nd;
3668 int error = EINVAL, igotlock;
3670 static int suspend_nfsd = 0;
3672 if (uap->flag & NFSSVC_PUBLICFH) {
3673 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3674 sizeof (fhandle_t));
3675 error = copyin(uap->argp,
3676 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3679 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) {
3680 error = copyin(uap->argp,(caddr_t)&export,
3681 sizeof (struct nfsex_args));
3683 error = nfsrv_v4rootexport(&export, cred, p);
3684 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3687 } else if (uap->flag & NFSSVC_STABLERESTART) {
3688 error = copyin(uap->argp, (caddr_t)&stablefd,
3691 error = fp_getfvp(p, stablefd, &fp, &vp);
3692 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3694 if (!error && newnfs_numnfsd != 0)
3697 nfsrv_stablefirst.nsf_fp = fp;
3698 nfsrv_setupstable(p);
3700 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3701 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3702 sizeof (struct nfsd_clid));
3704 error = nfsrv_adminrevoke(&adminrevoke, p);
3705 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3706 error = copyin(uap->argp, (caddr_t)&dumplist,
3707 sizeof (struct nfsd_dumplist));
3708 if (!error && (dumplist.ndl_size < 1 ||
3709 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3712 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3713 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3714 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3715 error = copyout(dumpclients, dumplist.ndl_list, len);
3716 free(dumpclients, M_TEMP);
3718 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3719 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3720 sizeof (struct nfsd_dumplocklist));
3721 if (!error && (dumplocklist.ndllck_size < 1 ||
3722 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3725 error = nfsrv_lookupfilename(&nd,
3726 dumplocklist.ndllck_fname, p);
3728 len = sizeof (struct nfsd_dumplocks) *
3729 dumplocklist.ndllck_size;
3730 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3731 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3732 dumplocklist.ndllck_size, p);
3734 error = copyout(dumplocks, dumplocklist.ndllck_list,
3736 free(dumplocks, M_TEMP);
3738 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3741 nfsd_master_pid = procp->p_pid;
3742 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3743 nfsd_master_start = procp->p_stats->p_start;
3744 nfsd_master_proc = procp;
3746 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
3747 NFSLOCKV4ROOTMUTEX();
3748 if (suspend_nfsd == 0) {
3749 /* Lock out all nfsd threads */
3751 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
3752 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
3753 } while (igotlock == 0 && suspend_nfsd == 0);
3756 NFSUNLOCKV4ROOTMUTEX();
3758 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
3759 NFSLOCKV4ROOTMUTEX();
3760 if (suspend_nfsd != 0) {
3761 nfsv4_unlock(&nfsd_suspend_lock, 0);
3764 NFSUNLOCKV4ROOTMUTEX();
3774 * Returns 0 if ok, 1 otherwise.
3777 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
3782 * This seems odd, but allow the case where the security flavor
3783 * list is empty. This happens when NFSv4 is traversing non-exported
3784 * file systems. Exported file systems should always have a non-empty
3785 * security flavor list.
3787 if (exp->nes_numsecflavor == 0)
3790 for (i = 0; i < exp->nes_numsecflavor; i++) {
3792 * The tests for privacy and integrity must be first,
3793 * since ND_GSS is set for everything but AUTH_SYS.
3795 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
3796 (nd->nd_flag & ND_GSSPRIVACY))
3798 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
3799 (nd->nd_flag & ND_GSSINTEGRITY))
3801 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
3802 (nd->nd_flag & ND_GSS))
3804 if (exp->nes_secflavors[i] == AUTH_SYS &&
3805 (nd->nd_flag & ND_GSS) == 0)
3812 * Calculate a hash value for the fid in a file handle.
3815 nfsrv_hashfh(fhandle_t *fhp)
3819 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
3824 * Calculate a hash value for the sessionid.
3827 nfsrv_hashsessionid(uint8_t *sessionid)
3831 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
3836 * Signal the userland master nfsd to backup the stable restart file.
3839 nfsrv_backupstable(void)
3843 if (nfsd_master_proc != NULL) {
3844 procp = pfind(nfsd_master_pid);
3845 /* Try to make sure it is the correct process. */
3846 if (procp == nfsd_master_proc &&
3847 procp->p_stats->p_start.tv_sec ==
3848 nfsd_master_start.tv_sec &&
3849 procp->p_stats->p_start.tv_usec ==
3850 nfsd_master_start.tv_usec &&
3851 strcmp(procp->p_comm, nfsd_master_comm) == 0)
3852 kern_psignal(procp, SIGUSR2);
3854 nfsd_master_proc = NULL;
3862 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
3863 * The arguments are in a structure, so that they can be passed through
3864 * taskqueue for a kernel process to execute this function.
3866 struct nfsrvdscreate {
3870 struct ucred *tcred;
3873 struct pnfsdsfile *pf;
3877 struct vattr createva;
3881 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
3882 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
3883 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
3886 struct nameidata named;
3891 struct nfsmount *nmp;
3894 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
3895 LOCKPARENT | LOCKLEAF | SAVESTART | NOCACHE);
3896 nfsvno_setpathbuf(&named, &bufp, &hashp);
3897 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
3898 named.ni_cnd.cn_thread = p;
3899 named.ni_cnd.cn_nameptr = bufp;
3900 if (fnamep != NULL) {
3901 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
3902 named.ni_cnd.cn_namelen = strlen(bufp);
3904 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
3905 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
3907 /* Create the date file in the DS mount. */
3908 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
3910 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
3913 /* Set the ownership of the file. */
3914 error = VOP_SETATTR(nvp, nvap, tcred);
3915 NFSD_DEBUG(4, "nfsrv_dscreate:"
3916 " setattr-uid=%d\n", error);
3921 printf("pNFS: pnfscreate failed=%d\n", error);
3923 printf("pNFS: pnfscreate vnlock=%d\n", error);
3926 nmp = VFSTONFS(nvp->v_mount);
3927 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
3928 != 0 || nmp->nm_nam->sa_len > sizeof(
3929 struct sockaddr_in6) ||
3930 np->n_fhp->nfh_len != NFSX_MYFH) {
3931 printf("Bad DS file: fstype=%s salen=%d"
3933 nvp->v_mount->mnt_vfc->vfc_name,
3934 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
3938 /* Set extattrs for the DS on the MDS file. */
3941 error = VOP_GETATTR(nvp, &va, tcred);
3943 dsa->dsa_filerev = va.va_filerev;
3944 dsa->dsa_size = va.va_size;
3945 dsa->dsa_atime = va.va_atime;
3946 dsa->dsa_mtime = va.va_mtime;
3947 dsa->dsa_bytes = va.va_bytes;
3951 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
3953 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
3954 nmp->nm_nam->sa_len);
3955 NFSBCOPY(named.ni_cnd.cn_nameptr,
3957 sizeof(pf->dsf_filename));
3960 printf("pNFS: pnfscreate can't get DS"
3961 " attr=%d\n", error);
3962 if (nvpp != NULL && error == 0)
3967 nfsvno_relpathbuf(&named);
3972 * Start up the thread that will execute nfsrv_dscreate().
3975 start_dscreate(void *arg, int pending)
3977 struct nfsrvdscreate *dsc;
3979 dsc = (struct nfsrvdscreate *)arg;
3980 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
3981 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
3983 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
3987 * Create a pNFS data file on the Data Server(s).
3990 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
3993 struct nfsrvdscreate *dsc, *tdsc = NULL;
3994 struct nfsdevice *ds, *tds, *fds;
3996 struct pnfsdsfile *pf, *tpf;
3997 struct pnfsdsattr dsattr;
3999 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4000 struct nfsmount *nmp;
4005 struct ucred *tcred;
4006 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4009 /* Get a DS server directory in a round-robin order. */
4015 * Search for the first entry that handles this MDS fs, but use the
4016 * first entry for all MDS fs's otherwise.
4018 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4019 if (tds->nfsdev_nmp != NULL) {
4020 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4022 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4023 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4031 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4034 i = dsdir[0] = ds->nfsdev_nextdir;
4035 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4036 dvp[0] = ds->nfsdev_dsdir[i];
4037 tds = TAILQ_NEXT(ds, nfsdev_list);
4038 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4039 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4040 if (tds->nfsdev_nmp != NULL &&
4041 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4042 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4043 fsidcmp(&mp->mnt_stat.f_fsid,
4044 &tds->nfsdev_mdsfsid) == 0))) {
4045 dsdir[mirrorcnt] = i;
4046 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4048 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4053 /* Put at end of list to implement round-robin usage. */
4054 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4055 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4059 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4061 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4064 error = nfsvno_getfh(vp, &fh, p);
4066 error = VOP_GETATTR(vp, &va, cred);
4068 /* Set the attributes for "vp" to Setattr the DS vp. */
4071 vamode = va.va_mode;
4075 va.va_mode = vamode;
4078 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4080 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4082 /* Make data file name based on FH. */
4083 tcred = newnfs_getcred();
4086 * Create the file on each DS mirror, using kernel process(es) for the
4087 * additional mirrors.
4090 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4091 tpf->dsf_dir = dsdir[i];
4092 tdsc->tcred = tcred;
4095 tdsc->createva = *vap;
4096 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4103 if (nfs_pnfsiothreads != 0) {
4104 ret = nfs_pnfsio(start_dscreate, tdsc);
4105 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4108 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4109 NULL, tcred, p, NULL);
4111 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4113 if (failpos == -1 && nfsds_failerr(ret))
4121 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4122 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4123 &dsattr, NULL, tcred, p, NULL);
4124 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4125 failpos = mirrorcnt - 1;
4129 timo = hz / 50; /* Wait for 20msec. */
4132 /* Wait for kernel task(s) to complete. */
4133 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4134 while (tdsc->inprog != 0 && tdsc->done == 0)
4135 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4136 if (tdsc->err != 0) {
4137 if (failpos == -1 && nfsds_failerr(tdsc->err))
4139 else if (error == 0)
4145 * If failpos has been set, that mirror has failed, so it needs
4149 nmp = VFSTONFS(dvp[failpos]->v_mount);
4151 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4152 NFSMNTP_CANCELRPCS)) == 0) {
4153 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4155 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4156 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4159 nfsrv_killrpcs(nmp);
4161 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4169 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4171 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4172 mirrorcnt, nfsrv_maxpnfsmirror);
4174 * For all mirrors that couldn't be created, fill in the
4175 * *pf structure, but with an IP address == 0.0.0.0.
4177 tpf = pf + mirrorcnt;
4178 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4180 tpf->dsf_sin.sin_family = AF_INET;
4181 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4182 tpf->dsf_sin.sin_addr.s_addr = 0;
4183 tpf->dsf_sin.sin_port = 0;
4186 error = vn_extattr_set(vp, IO_NODELOCKED,
4187 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4188 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4190 error = vn_extattr_set(vp, IO_NODELOCKED,
4191 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4192 sizeof(dsattr), (char *)&dsattr, p);
4194 printf("pNFS: pnfscreate setextattr=%d\n",
4197 printf("pNFS: pnfscreate=%d\n", error);
4203 * Get the information needed to remove the pNFS Data Server file from the
4204 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4205 * DS directory vnode. The caller must unlock *ddvp when done with it.
4208 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4209 int *mirrorcntp, char *fname, fhandle_t *fhp)
4212 struct ucred *tcred;
4217 /* If not an exported regular file or not a pNFS server, just return. */
4218 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4219 nfsrv_devidcnt == 0)
4222 /* Check to see if this is the last hard link. */
4223 tcred = newnfs_getcred();
4224 error = VOP_GETATTR(vp, &va, tcred);
4227 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4230 if (va.va_nlink > 1)
4233 error = nfsvno_getfh(vp, fhp, p);
4235 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4240 buf = malloc(buflen, M_TEMP, M_WAITOK);
4241 /* Get the directory vnode for the DS mount and the file handle. */
4242 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4243 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4246 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4250 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4251 * The arguments are in a structure, so that they can be passed through
4252 * taskqueue for a kernel process to execute this function.
4254 struct nfsrvdsremove {
4258 struct ucred *tcred;
4262 char fname[PNFS_FILENAME_LEN + 1];
4266 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4269 struct nameidata named;
4275 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4278 named.ni_cnd.cn_nameiop = DELETE;
4279 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4280 named.ni_cnd.cn_cred = tcred;
4281 named.ni_cnd.cn_thread = p;
4282 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
4283 nfsvno_setpathbuf(&named, &bufp, &hashp);
4284 named.ni_cnd.cn_nameptr = bufp;
4285 named.ni_cnd.cn_namelen = strlen(fname);
4286 strlcpy(bufp, fname, NAME_MAX);
4287 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4288 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4289 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4291 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4295 nfsvno_relpathbuf(&named);
4297 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4302 * Start up the thread that will execute nfsrv_dsremove().
4305 start_dsremove(void *arg, int pending)
4307 struct nfsrvdsremove *dsrm;
4309 dsrm = (struct nfsrvdsremove *)arg;
4310 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4313 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4317 * Remove a pNFS data file from a Data Server.
4318 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4319 * removed to set up the dvp and fill in the FH.
4322 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4325 struct ucred *tcred;
4326 struct nfsrvdsremove *dsrm, *tdsrm;
4327 struct nfsdevice *ds;
4328 struct nfsmount *nmp;
4329 int failpos, i, ret, timo;
4331 tcred = newnfs_getcred();
4334 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4336 * Remove the file on each DS mirror, using kernel process(es) for the
4337 * additional mirrors.
4340 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4341 tdsrm->tcred = tcred;
4343 tdsrm->dvp = dvp[i];
4344 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4349 if (nfs_pnfsiothreads != 0) {
4350 ret = nfs_pnfsio(start_dsremove, tdsrm);
4351 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4354 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4355 if (failpos == -1 && nfsds_failerr(ret))
4359 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4360 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4361 failpos = mirrorcnt - 1;
4362 timo = hz / 50; /* Wait for 20msec. */
4365 /* Wait for kernel task(s) to complete. */
4366 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4367 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4368 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4369 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4374 * If failpos has been set, that mirror has failed, so it needs
4378 nmp = VFSTONFS(dvp[failpos]->v_mount);
4380 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4381 NFSMNTP_CANCELRPCS)) == 0) {
4382 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4384 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4385 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4388 nfsrv_killrpcs(nmp);
4390 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4396 /* Get rid all layouts for the file. */
4397 nfsrv_freefilelayouts(fhp);
4404 * Generate a file name based on the file handle and put it in *bufp.
4405 * Return the number of bytes generated.
4408 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4412 const uint8_t *hexdigits = "0123456789abcdef";
4414 cp = (uint8_t *)fhp;
4415 for (i = 0; i < sizeof(*fhp); i++) {
4416 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4417 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4424 * Update the Metadata file's attributes from the DS file when a Read/Write
4425 * layout is returned.
4426 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4427 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4430 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4432 struct ucred *tcred;
4435 /* Do this as root so that it won't fail with EACCES. */
4436 tcred = newnfs_getcred();
4437 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4438 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4444 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4447 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4452 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4453 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4458 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4459 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4460 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4461 off_t *offp, int content, bool *eofp)
4463 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4464 fhandle_t fh[NFSDEV_MAXMIRRORS];
4465 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4466 struct nfsdevice *ds;
4467 struct pnfsdsattr dsattr;
4468 struct opnfsdsattr odsattr;
4470 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4472 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4474 * If not a regular file, not exported or not a pNFS server,
4475 * just return ENOENT.
4477 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4478 nfsrv_devidcnt == 0)
4482 buf = malloc(buflen, M_TEMP, M_WAITOK);
4486 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4487 * from the MetaData file's extended attribute.
4489 if (ioproc == NFSPROC_GETATTR) {
4490 error = vn_extattr_get(vp, IO_NODELOCKED,
4491 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4494 if (buflen == sizeof(odsattr)) {
4495 NFSBCOPY(buf, &odsattr, buflen);
4496 nap->na_filerev = odsattr.dsa_filerev;
4497 nap->na_size = odsattr.dsa_size;
4498 nap->na_atime = odsattr.dsa_atime;
4499 nap->na_mtime = odsattr.dsa_mtime;
4501 * Fake na_bytes by rounding up na_size.
4502 * Since we don't know the block size, just
4503 * use BLKDEV_IOSIZE.
4505 nap->na_bytes = (odsattr.dsa_size +
4506 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4507 } else if (buflen == sizeof(dsattr)) {
4508 NFSBCOPY(buf, &dsattr, buflen);
4509 nap->na_filerev = dsattr.dsa_filerev;
4510 nap->na_size = dsattr.dsa_size;
4511 nap->na_atime = dsattr.dsa_atime;
4512 nap->na_mtime = dsattr.dsa_mtime;
4513 nap->na_bytes = dsattr.dsa_bytes;
4519 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4520 * returns 0, just return now. nfsrv_checkdsattr()
4521 * returns 0 if there is no Read/Write layout
4522 * plus either an Open/Write_access or Write
4523 * delegation issued to a client for the file.
4525 if (nfsrv_pnfsgetdsattr == 0 ||
4526 nfsrv_checkdsattr(vp, p) == 0) {
4533 * Clear ENOATTR so the code below will attempt to do a
4534 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4535 * the extended attribute.
4537 if (error == ENOATTR)
4546 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4548 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4549 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4550 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4553 for (i = 0; i < mirrorcnt; i++)
4554 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4556 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4558 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4561 if (origmircnt == -1)
4562 origmircnt = mirrorcnt;
4564 * If failpos is set to a mirror#, then that mirror has
4565 * failed and will be disabled. For Read, Getattr and Seek, the
4566 * function only tries one mirror, so if that mirror has
4567 * failed, it will need to be retried. As such, increment
4568 * tryitagain for these cases.
4569 * For Write, Setattr and Setacl, the function tries all
4570 * mirrors and will not return an error for the case where
4571 * one mirror has failed. For these cases, the functioning
4572 * mirror(s) will have been modified, so a retry isn't
4573 * necessary. These functions will set failpos for the
4576 if (ioproc == NFSPROC_READDS) {
4577 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4579 if (nfsds_failerr(error) && mirrorcnt > 1) {
4581 * Setting failpos will cause the mirror
4582 * to be disabled and then a retry of this
4589 } else if (ioproc == NFSPROC_WRITEDS)
4590 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4591 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4592 else if (ioproc == NFSPROC_SETATTR)
4593 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4594 mirrorcnt, nap, &failpos);
4595 else if (ioproc == NFSPROC_SETACL)
4596 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4597 mirrorcnt, aclp, &failpos);
4598 else if (ioproc == NFSPROC_SEEKDS) {
4599 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4601 if (nfsds_failerr(error) && mirrorcnt > 1) {
4603 * Setting failpos will cause the mirror
4604 * to be disabled and then a retry of this
4611 } else if (ioproc == NFSPROC_ALLOCATE)
4612 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4613 &nmp[0], mirrorcnt, &failpos);
4615 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4616 vp, nmp[mirrorcnt - 1], nap);
4617 if (nfsds_failerr(error) && mirrorcnt > 1) {
4619 * Setting failpos will cause the mirror
4620 * to be disabled and then a retry of this
4621 * getattr is required.
4623 failpos = mirrorcnt - 1;
4630 failnmp = nmp[failpos];
4631 NFSLOCKMNT(failnmp);
4632 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4633 NFSMNTP_CANCELRPCS)) == 0) {
4634 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4635 NFSUNLOCKMNT(failnmp);
4636 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4638 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4641 nfsrv_killrpcs(failnmp);
4642 NFSLOCKMNT(failnmp);
4643 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4646 NFSUNLOCKMNT(failnmp);
4648 for (i = 0; i < mirrorcnt; i++)
4649 NFSVOPUNLOCK(dvp[i]);
4650 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4652 /* Try the Read/Getattr again if a mirror was deleted. */
4653 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4656 /* Return ENOENT for any Extended Attribute error. */
4660 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4665 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4667 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4668 * to be checked. If it points to a NULL nmp, then it returns
4669 * a suitable destination.
4670 * curnmp - If non-NULL, it is the source mount for the copy.
4673 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4674 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4675 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4676 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4678 struct vnode *dvp, *nvp = NULL, **tdvpp;
4680 struct nfsmount *nmp, *newnmp;
4681 struct sockaddr *sad;
4682 struct sockaddr_in *sin;
4683 struct nfsdevice *ds, *tds, *fndds;
4684 struct pnfsdsfile *pf;
4686 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4688 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4697 if (newnmpp != NULL)
4702 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4703 "pnfsd.dsfile", buflenp, buf, p);
4704 mirrorcnt = *buflenp / sizeof(*pf);
4705 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4706 *buflenp != sizeof(*pf) * mirrorcnt))
4709 pf = (struct pnfsdsfile *)buf;
4710 /* If curnmp != NULL, check for a match in the mirror list. */
4711 if (curnmp != NULL && error == 0) {
4713 for (i = 0; i < mirrorcnt; i++, pf++) {
4714 sad = (struct sockaddr *)&pf->dsf_sin;
4715 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
4727 pf = (struct pnfsdsfile *)buf;
4728 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
4730 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
4732 sad = (struct sockaddr *)&pf->dsf_sin;
4734 dsdir = pf->dsf_dir;
4735 if (dsdir >= nfsrv_dsdirsize) {
4736 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
4738 } else if (nvpp != NULL && newnmp != NULL &&
4739 nfsaddr2_match(sad, newnmp->nm_nam))
4742 if (ippos != NULL && curnmp == NULL &&
4743 sad->sa_family == AF_INET &&
4744 sin->sin_addr.s_addr == 0)
4746 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
4748 /* Use the socket address to find the mount point. */
4751 /* Find a match for the IP address. */
4752 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
4753 if (ds->nfsdev_nmp != NULL) {
4754 dvp = ds->nfsdev_dvp;
4755 nmp = VFSTONFS(dvp->v_mount);
4756 if (nmp != ds->nfsdev_nmp)
4757 printf("different2 nmp %p %p\n",
4758 nmp, ds->nfsdev_nmp);
4759 if (nfsaddr2_match(sad, nmp->nm_nam)) {
4765 if (fndds != NULL && newnmpp != NULL &&
4767 /* Search for a place to make a mirror copy. */
4768 TAILQ_FOREACH(tds, &nfsrv_devidhead,
4770 if (tds->nfsdev_nmp != NULL &&
4772 ((tds->nfsdev_mdsisset == 0 &&
4773 fndds->nfsdev_mdsisset == 0) ||
4774 (tds->nfsdev_mdsisset != 0 &&
4775 fndds->nfsdev_mdsisset != 0 &&
4776 fsidcmp(&tds->nfsdev_mdsfsid,
4777 &mp->mnt_stat.f_fsid) == 0))) {
4778 *newnmpp = tds->nfsdev_nmp;
4784 * Move this entry to the end of the
4785 * list, so it won't be selected as
4786 * easily the next time.
4788 TAILQ_REMOVE(&nfsrv_devidhead, tds,
4790 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
4795 if (fndds != NULL) {
4796 dvp = fndds->nfsdev_dsdir[dsdir];
4797 if (lktype != 0 || fhiszero != 0 ||
4798 (nvpp != NULL && *nvpp == NULL)) {
4800 error = vn_lock(dvp,
4802 else if (lktype != 0)
4803 error = vn_lock(dvp, lktype);
4805 error = vn_lock(dvp, LK_SHARED);
4807 * If the file handle is all 0's, try to
4808 * do a Lookup against the DS to acquire
4810 * If dvpp == NULL or the Lookup fails,
4811 * unlock dvp after the call.
4813 if (error == 0 && (fhiszero != 0 ||
4814 (nvpp != NULL && *nvpp == NULL))) {
4815 error = nfsrv_pnfslookupds(vp,
4831 if (error != 0 || lktype == 0)
4837 NFSD_DEBUG(4, "gotone=%d\n", gotone);
4838 if (devid != NULL) {
4839 NFSBCOPY(fndds->nfsdev_deviceid,
4840 devid, NFSX_V4DEVICEID);
4841 devid += NFSX_V4DEVICEID;
4846 NFSBCOPY(&pf->dsf_fh, fhp++,
4848 if (fnamep != NULL && gotone == 1)
4851 sizeof(pf->dsf_filename));
4853 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
4858 if (error == 0 && gotone == 0)
4861 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
4864 *mirrorcntp = gotone;
4866 if (gotone > 0 && dvpp != NULL) {
4868 * If the error didn't occur on the first one and
4869 * dvpp != NULL, the one(s) prior to the failure will
4870 * have locked dvp's that need to be unlocked.
4872 for (i = 0; i < gotone; i++) {
4873 NFSVOPUNLOCK(*dvpp);
4878 * If it found the vnode to be copied from before a failure,
4879 * it needs to be vput()'d.
4881 if (nvpp != NULL && *nvpp != NULL) {
4890 * Set the extended attribute for the Change attribute.
4893 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4895 struct pnfsdsattr dsattr;
4898 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
4899 dsattr.dsa_filerev = nap->na_filerev;
4900 dsattr.dsa_size = nap->na_size;
4901 dsattr.dsa_atime = nap->na_atime;
4902 dsattr.dsa_mtime = nap->na_mtime;
4903 dsattr.dsa_bytes = nap->na_bytes;
4904 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4905 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
4907 printf("pNFS: setextattr=%d\n", error);
4912 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
4913 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
4916 struct nfsrv_descript *nd;
4918 struct mbuf *m, *m2;
4919 int error = 0, retlen, tlen, trimlen;
4921 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
4922 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
4925 * Use a stateid where other is an alternating 01010 pattern and
4926 * seqid is 0xffffffff. This value is not defined as special by
4927 * the RFC and is used by the FreeBSD NFS server to indicate an
4928 * MDS->DS proxy operation.
4930 st.other[0] = 0x55555555;
4931 st.other[1] = 0x55555555;
4932 st.other[2] = 0x55555555;
4933 st.seqid = 0xffffffff;
4934 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
4936 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
4937 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
4938 txdr_hyper(off, tl);
4939 *(tl + 2) = txdr_unsigned(len);
4940 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
4941 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
4946 if (nd->nd_repstat == 0) {
4947 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
4948 NFSM_STRSIZ(retlen, len);
4950 /* Trim off the pre-data XDR from the mbuf chain. */
4952 while (m != NULL && m != nd->nd_md) {
4953 if (m->m_next == nd->nd_md) {
4955 m_freem(nd->nd_mrep);
4956 nd->nd_mrep = m = nd->nd_md;
4961 printf("nfsrv_readdsrpc: busted mbuf list\n");
4967 * Now, adjust first mbuf so that any XDR before the
4968 * read data is skipped over.
4970 trimlen = nd->nd_dpos - mtod(m, char *);
4972 m->m_len -= trimlen;
4973 NFSM_DATAP(m, trimlen);
4977 * Truncate the mbuf chain at retlen bytes of data,
4978 * plus XDR padding that brings the length up to a
4981 tlen = NFSM_RNDUP(retlen);
4983 if (m->m_len >= tlen) {
4993 } while (m != NULL);
4995 printf("nfsrv_readdsrpc: busted mbuf list\n");
5004 error = nd->nd_repstat;
5006 /* If nd->nd_mrep is already NULL, this is a no-op. */
5007 m_freem(nd->nd_mrep);
5009 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5014 * Do a write RPC on a DS data file, using this structure for the arguments,
5015 * so that this function can be executed by a separate kernel process.
5017 struct nfsrvwritedsdorpc {
5024 struct nfsmount *nmp;
5032 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5033 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5036 struct nfsrv_descript *nd;
5037 nfsattrbit_t attrbits;
5039 int commit, error, retlen;
5041 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5042 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5043 sizeof(fhandle_t), NULL, NULL, 0, 0);
5046 * Use a stateid where other is an alternating 01010 pattern and
5047 * seqid is 0xffffffff. This value is not defined as special by
5048 * the RFC and is used by the FreeBSD NFS server to indicate an
5049 * MDS->DS proxy operation.
5051 st.other[0] = 0x55555555;
5052 st.other[1] = 0x55555555;
5053 st.other[2] = 0x55555555;
5054 st.seqid = 0xffffffff;
5055 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5056 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5057 txdr_hyper(off, tl);
5060 * Do all writes FileSync, since the server doesn't hold onto dirty
5061 * buffers. Since clients should be accessing the DS servers directly
5062 * using the pNFS layouts, this just needs to work correctly as a
5065 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5066 *tl = txdr_unsigned(len);
5067 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5069 /* Put data in mbuf chain. */
5070 nd->nd_mb->m_next = m;
5072 /* Set nd_mb and nd_bpos to end of data. */
5073 while (m->m_next != NULL)
5076 nfsm_set(nd, m->m_len);
5077 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5079 /* Do a Getattr for the attributes that change upon writing. */
5080 NFSZERO_ATTRBIT(&attrbits);
5081 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5082 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5083 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5084 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5085 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5086 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5087 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5088 (void) nfsrv_putattrbit(nd, &attrbits);
5089 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5090 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5095 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5096 /* Get rid of weak cache consistency data for now. */
5097 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5098 (ND_NFSV4 | ND_V4WCCATTR)) {
5099 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5100 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5101 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5105 * Get rid of Op# and status for next op.
5107 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5109 nd->nd_flag |= ND_NOMOREDATA;
5111 if (nd->nd_repstat == 0) {
5112 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5113 retlen = fxdr_unsigned(int, *tl++);
5114 commit = fxdr_unsigned(int, *tl);
5115 if (commit != NFSWRITE_FILESYNC)
5117 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5118 retlen, commit, error);
5120 error = nd->nd_repstat;
5121 /* We have no use for the Write Verifier since we use FileSync. */
5124 * Get the Change, Size, Access Time and Modify Time attributes and set
5125 * on the Metadata file, so its attributes will be what the file's
5126 * would be if it had been written.
5129 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5130 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5131 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5133 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5135 m_freem(nd->nd_mrep);
5137 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5142 * Start up the thread that will execute nfsrv_writedsdorpc().
5145 start_writedsdorpc(void *arg, int pending)
5147 struct nfsrvwritedsdorpc *drpc;
5149 drpc = (struct nfsrvwritedsdorpc *)arg;
5150 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5151 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5153 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5157 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5158 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5159 struct mbuf **mpp, char *cp, int *failposp)
5161 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5164 int error, i, offs, ret, timo;
5166 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5167 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5170 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5173 /* Calculate offset in mbuf chain that data starts. */
5174 offs = cp - mtod(*mpp, char *);
5175 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5178 * Do the write RPC for every DS, using a separate kernel process
5179 * for every DS except the last one.
5182 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5184 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5192 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5194 if (nfs_pnfsiothreads != 0) {
5195 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5196 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5200 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5202 if (nfsds_failerr(ret) && *failposp == -1)
5204 else if (error == 0 && ret != 0)
5210 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5211 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5212 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5213 *failposp = mirrorcnt - 1;
5214 else if (error == 0 && ret != 0)
5217 error = nfsrv_setextattr(vp, &na, p);
5218 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5220 timo = hz / 50; /* Wait for 20msec. */
5223 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5224 /* Wait for RPCs on separate threads to complete. */
5225 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5226 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5227 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5229 else if (error == 0 && tdrpc->err != 0)
5237 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5238 * so that this function can be executed by a separate kernel process.
5240 struct nfsrvallocatedsdorpc {
5247 struct nfsmount *nmp;
5254 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5255 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5258 struct nfsrv_descript *nd;
5259 nfsattrbit_t attrbits;
5263 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5264 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5265 sizeof(fhandle_t), NULL, NULL, 0, 0);
5268 * Use a stateid where other is an alternating 01010 pattern and
5269 * seqid is 0xffffffff. This value is not defined as special by
5270 * the RFC and is used by the FreeBSD NFS server to indicate an
5271 * MDS->DS proxy operation.
5273 st.other[0] = 0x55555555;
5274 st.other[1] = 0x55555555;
5275 st.other[2] = 0x55555555;
5276 st.seqid = 0xffffffff;
5277 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5278 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5279 txdr_hyper(off, tl); tl += 2;
5280 txdr_hyper(len, tl); tl += 2;
5281 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5283 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5284 NFSGETATTR_ATTRBIT(&attrbits);
5285 nfsrv_putattrbit(nd, &attrbits);
5286 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5287 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5292 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5294 if (nd->nd_repstat == 0) {
5295 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5296 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5297 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5299 error = nd->nd_repstat;
5300 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5302 m_freem(nd->nd_mrep);
5304 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5309 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5312 start_allocatedsdorpc(void *arg, int pending)
5314 struct nfsrvallocatedsdorpc *drpc;
5316 drpc = (struct nfsrvallocatedsdorpc *)arg;
5317 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5318 drpc->len, NULL, drpc->cred, drpc->p);
5320 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5324 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5325 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5328 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5330 int error, i, ret, timo;
5332 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5335 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5339 * Do the allocate RPC for every DS, using a separate kernel process
5340 * for every DS except the last one.
5343 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5345 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5354 if (nfs_pnfsiothreads != 0) {
5355 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5356 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5360 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5362 if (nfsds_failerr(ret) && *failposp == -1)
5364 else if (error == 0 && ret != 0)
5370 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5371 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5372 *failposp = mirrorcnt - 1;
5373 else if (error == 0 && ret != 0)
5376 error = nfsrv_setextattr(vp, &na, p);
5377 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5379 timo = hz / 50; /* Wait for 20msec. */
5382 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5383 /* Wait for RPCs on separate threads to complete. */
5384 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5385 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5386 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5388 else if (error == 0 && tdrpc->err != 0)
5396 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5397 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5398 struct nfsvattr *dsnap)
5401 struct nfsrv_descript *nd;
5403 nfsattrbit_t attrbits;
5406 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5407 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5409 * Use a stateid where other is an alternating 01010 pattern and
5410 * seqid is 0xffffffff. This value is not defined as special by
5411 * the RFC and is used by the FreeBSD NFS server to indicate an
5412 * MDS->DS proxy operation.
5414 st.other[0] = 0x55555555;
5415 st.other[1] = 0x55555555;
5416 st.other[2] = 0x55555555;
5417 st.seqid = 0xffffffff;
5418 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5420 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5421 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5423 /* Do a Getattr for the attributes that change due to writing. */
5424 NFSZERO_ATTRBIT(&attrbits);
5425 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5426 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5427 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5428 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5429 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5430 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5431 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5432 (void) nfsrv_putattrbit(nd, &attrbits);
5433 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5434 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5439 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5441 /* Get rid of weak cache consistency data for now. */
5442 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5443 (ND_NFSV4 | ND_V4WCCATTR)) {
5444 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5445 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5446 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5450 * Get rid of Op# and status for next op.
5452 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5454 nd->nd_flag |= ND_NOMOREDATA;
5456 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5459 if (nd->nd_repstat != 0)
5460 error = nd->nd_repstat;
5462 * Get the Change, Size, Access Time and Modify Time attributes and set
5463 * on the Metadata file, so its attributes will be what the file's
5464 * would be if it had been written.
5467 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5468 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5469 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5471 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5473 m_freem(nd->nd_mrep);
5475 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5479 struct nfsrvsetattrdsdorpc {
5484 struct nfsmount *nmp;
5489 struct nfsvattr dsna;
5494 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5497 start_setattrdsdorpc(void *arg, int pending)
5499 struct nfsrvsetattrdsdorpc *drpc;
5501 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5502 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5503 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5508 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5509 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5510 struct nfsvattr *nap, int *failposp)
5512 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5514 int error, i, ret, timo;
5516 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5519 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5523 * Do the setattr RPC for every DS, using a separate kernel process
5524 * for every DS except the last one.
5527 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5530 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5538 if (nfs_pnfsiothreads != 0) {
5539 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
5540 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
5544 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
5546 if (nfsds_failerr(ret) && *failposp == -1)
5548 else if (error == 0 && ret != 0)
5554 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
5555 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5556 *failposp = mirrorcnt - 1;
5557 else if (error == 0 && ret != 0)
5560 error = nfsrv_setextattr(vp, &na, p);
5561 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
5563 timo = hz / 50; /* Wait for 20msec. */
5566 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5567 /* Wait for RPCs on separate threads to complete. */
5568 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5569 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
5570 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5572 else if (error == 0 && tdrpc->err != 0)
5580 * Do a Setattr of an NFSv4 ACL on the DS file.
5583 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5584 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
5586 struct nfsrv_descript *nd;
5588 nfsattrbit_t attrbits;
5591 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
5592 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5594 * Use a stateid where other is an alternating 01010 pattern and
5595 * seqid is 0xffffffff. This value is not defined as special by
5596 * the RFC and is used by the FreeBSD NFS server to indicate an
5597 * MDS->DS proxy operation.
5599 st.other[0] = 0x55555555;
5600 st.other[1] = 0x55555555;
5601 st.other[2] = 0x55555555;
5602 st.seqid = 0xffffffff;
5603 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5605 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5606 NFSZERO_ATTRBIT(&attrbits);
5607 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
5609 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
5610 * so passing in the metadata "vp" will be ok, since it is of
5611 * the same type (VREG).
5613 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
5614 NULL, 0, 0, 0, 0, 0, NULL);
5615 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5616 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5621 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
5623 error = nd->nd_repstat;
5624 m_freem(nd->nd_mrep);
5629 struct nfsrvsetacldsdorpc {
5634 struct nfsmount *nmp;
5643 * Start up the thread that will execute nfsrv_setacldsdorpc().
5646 start_setacldsdorpc(void *arg, int pending)
5648 struct nfsrvsetacldsdorpc *drpc;
5650 drpc = (struct nfsrvsetacldsdorpc *)arg;
5651 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
5652 drpc->vp, drpc->nmp, drpc->aclp);
5657 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5658 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
5661 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
5662 int error, i, ret, timo;
5664 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
5667 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5671 * Do the setattr RPC for every DS, using a separate kernel process
5672 * for every DS except the last one.
5675 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5678 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5686 if (nfs_pnfsiothreads != 0) {
5687 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
5688 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
5692 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
5694 if (nfsds_failerr(ret) && *failposp == -1)
5696 else if (error == 0 && ret != 0)
5702 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
5703 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5704 *failposp = mirrorcnt - 1;
5705 else if (error == 0 && ret != 0)
5707 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
5709 timo = hz / 50; /* Wait for 20msec. */
5712 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5713 /* Wait for RPCs on separate threads to complete. */
5714 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5715 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
5716 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5718 else if (error == 0 && tdrpc->err != 0)
5726 * Getattr call to the DS for the attributes that change due to writing.
5729 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5730 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
5732 struct nfsrv_descript *nd;
5734 nfsattrbit_t attrbits;
5736 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
5737 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5738 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
5739 sizeof(fhandle_t), NULL, NULL, 0, 0);
5740 NFSZERO_ATTRBIT(&attrbits);
5741 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5742 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5743 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5744 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5745 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5746 (void) nfsrv_putattrbit(nd, &attrbits);
5747 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5748 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5753 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
5755 if (nd->nd_repstat == 0) {
5756 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
5757 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
5760 * We can only save the updated values in the extended
5761 * attribute if the vp is exclusively locked.
5762 * This should happen when any of the following operations
5763 * occur on the vnode:
5764 * Close, Delegreturn, LayoutCommit, LayoutReturn
5765 * As such, the updated extended attribute should get saved
5766 * before nfsrv_checkdsattr() returns 0 and allows the cached
5767 * attributes to be returned without calling this function.
5769 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
5770 error = nfsrv_setextattr(vp, nap, p);
5771 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
5775 error = nd->nd_repstat;
5776 m_freem(nd->nd_mrep);
5778 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
5783 * Seek call to a DS.
5786 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
5787 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
5790 struct nfsrv_descript *nd;
5794 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
5796 * Use a stateid where other is an alternating 01010 pattern and
5797 * seqid is 0xffffffff. This value is not defined as special by
5798 * the RFC and is used by the FreeBSD NFS server to indicate an
5799 * MDS->DS proxy operation.
5801 st.other[0] = 0x55555555;
5802 st.other[1] = 0x55555555;
5803 st.other[2] = 0x55555555;
5804 st.seqid = 0xffffffff;
5805 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5806 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
5807 sizeof(fhandle_t), NULL, NULL, 0, 0);
5808 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5809 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
5810 txdr_hyper(*offp, tl); tl += 2;
5811 *tl = txdr_unsigned(content);
5812 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5813 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5818 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
5819 if (nd->nd_repstat == 0) {
5820 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
5821 if (*tl++ == newnfs_true)
5825 *offp = fxdr_hyper(tl);
5827 error = nd->nd_repstat;
5829 m_freem(nd->nd_mrep);
5831 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
5836 * Get the device id and file handle for a DS file.
5839 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
5840 fhandle_t *fhp, char *devid)
5846 buf = malloc(buflen, M_TEMP, M_WAITOK);
5847 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
5848 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
5854 * Do a Lookup against the DS for the filename.
5857 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
5858 struct vnode **nvpp, NFSPROC_T *p)
5860 struct nameidata named;
5861 struct ucred *tcred;
5867 tcred = newnfs_getcred();
5868 named.ni_cnd.cn_nameiop = LOOKUP;
5869 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
5870 named.ni_cnd.cn_cred = tcred;
5871 named.ni_cnd.cn_thread = p;
5872 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF | SAVENAME;
5873 nfsvno_setpathbuf(&named, &bufp, &hashp);
5874 named.ni_cnd.cn_nameptr = bufp;
5875 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
5876 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
5877 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
5878 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
5879 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
5881 nfsvno_relpathbuf(&named);
5884 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
5889 * Set the file handle to the correct one.
5892 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
5893 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
5899 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
5901 * We can only do a vn_set_extattr() if the vnode is exclusively
5902 * locked and vn_start_write() has been done. If devid != NULL or
5903 * fnamep != NULL or the vnode is shared locked, vn_start_write()
5904 * may not have been done.
5905 * If not done now, it will be done on a future call.
5907 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
5909 ret = vn_extattr_set(vp, IO_NODELOCKED,
5910 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
5912 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
5916 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
5917 * when the DS has failed.
5920 nfsrv_killrpcs(struct nfsmount *nmp)
5924 * Call newnfs_nmcancelreqs() to cause
5925 * any RPCs in progress on the mount point to
5927 * This will cause any process waiting for an
5928 * RPC to complete while holding a vnode lock
5929 * on the mounted-on vnode (such as "df" or
5930 * a non-forced "umount") to fail.
5931 * This will unlock the mounted-on vnode so
5932 * a forced dismount can succeed.
5933 * The NFSMNTP_CANCELRPCS flag should be set when this function is
5936 newnfs_nmcancelreqs(nmp);
5940 * Sum up the statfs info for each of the DSs, so that the client will
5941 * receive the total for all DSs.
5944 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
5947 struct nfsdevice *ds;
5948 struct vnode **dvpp, **tdvpp, *dvp;
5950 int cnt, error = 0, i;
5952 if (nfsrv_devidcnt <= 0)
5954 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
5955 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
5957 /* Get an array of the dvps for the DSs. */
5961 /* First, search for matches for same file system. */
5962 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5963 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
5964 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
5965 if (++i > nfsrv_devidcnt)
5967 *tdvpp++ = ds->nfsdev_dvp;
5971 * If no matches for same file system, total all servers not assigned
5975 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5976 if (ds->nfsdev_nmp != NULL &&
5977 ds->nfsdev_mdsisset == 0) {
5978 if (++i > nfsrv_devidcnt)
5980 *tdvpp++ = ds->nfsdev_dvp;
5987 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
5989 for (i = 0; i < cnt && error == 0; i++) {
5991 error = VFS_STATFS(dvp->v_mount, tsf);
5993 if (sf->f_bsize == 0) {
5994 if (tsf->f_bsize > 0)
5995 sf->f_bsize = tsf->f_bsize;
5999 if (tsf->f_blocks > 0) {
6000 if (sf->f_bsize != tsf->f_bsize) {
6001 tot = tsf->f_blocks * tsf->f_bsize;
6002 sf->f_blocks += (tot / sf->f_bsize);
6004 sf->f_blocks += tsf->f_blocks;
6006 if (tsf->f_bfree > 0) {
6007 if (sf->f_bsize != tsf->f_bsize) {
6008 tot = tsf->f_bfree * tsf->f_bsize;
6009 sf->f_bfree += (tot / sf->f_bsize);
6011 sf->f_bfree += tsf->f_bfree;
6013 if (tsf->f_bavail > 0) {
6014 if (sf->f_bsize != tsf->f_bsize) {
6015 tot = tsf->f_bavail * tsf->f_bsize;
6016 sf->f_bavail += (tot / sf->f_bsize);
6018 sf->f_bavail += tsf->f_bavail;
6031 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6035 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6036 error = NFSERR_ATTRNOTSUPP;
6040 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6041 * Make sure it has enough room for that - splitting every entry
6042 * into two and appending "canonical six" entries at the end.
6043 * Cribbed out of kern/vfs_acl.c - Rick M.
6045 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6046 error = NFSERR_ATTRNOTSUPP;
6049 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6051 error = nfsrv_dssetacl(vp, aclp, cred, p);
6052 if (error == ENOENT)
6062 * Seek vnode op call (actually it is a VOP_IOCTL()).
6063 * This function is called with the vnode locked, but unlocks and vrele()s
6064 * the vp before returning.
6067 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6068 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6073 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6075 * Attempt to seek on a DS file. A return of ENOENT implies
6076 * there is no DS file to seek on.
6078 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6079 NULL, NULL, NULL, NULL, offp, content, eofp);
6080 if (error != ENOENT) {
6086 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6087 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6088 * NFSv4.2 is *eofp == true and error == 0 for this case.
6091 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6093 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6094 /* Handle the cases where we might be at EOF. */
6095 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6096 if (ret == 0 && *offp == at.na_size) {
6100 if (ret != 0 && error == 0)
6109 * Allocate vnode op call.
6112 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6117 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6119 * Attempt to allocate on a DS file. A return of ENOENT implies
6120 * there is no DS file to allocate on.
6122 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6123 NULL, NULL, NULL, NULL, &len, 0, NULL);
6124 if (error != ENOENT)
6129 * Do the actual VOP_ALLOCATE(), looping a reasonable number of
6130 * times to achieve completion.
6133 while (error == 0 && len > 0 && trycnt++ < 20)
6134 error = VOP_ALLOCATE(vp, &off, &len);
6135 if (error == 0 && len > 0)
6142 * Get Extended Atribute vnode op into an mbuf list.
6145 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6146 struct ucred *cred, struct thread *p, struct mbuf **mpp,
6147 struct mbuf **mpendp, int *lenp)
6150 struct uio io, *uiop = &io;
6151 struct mbuf *m, *m2;
6152 int alen, error, len, tlen;
6155 /* First, find out the size of the extended attribute. */
6156 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6159 return (NFSERR_NOXATTR);
6160 if (siz > maxresp - NFS_MAXXDR)
6161 return (NFSERR_XATTR2BIG);
6163 tlen = NFSM_RNDUP(len);
6165 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2, &iv);
6168 uiop->uio_iovcnt = 0;
6169 uiop->uio_iov = iv = NULL;
6172 uiop->uio_offset = 0;
6173 uiop->uio_resid = tlen;
6174 uiop->uio_rw = UIO_READ;
6175 uiop->uio_segflg = UIO_SYSSPACE;
6178 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6185 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6189 if (uiop->uio_resid > 0) {
6191 len = tlen - uiop->uio_resid;
6192 tlen = NFSM_RNDUP(len);
6194 printf("nfsvno_getxattr: weird size read\n");
6195 nfsrv_adj(m, alen - tlen, tlen - len);
6213 * Set Extended attribute vnode op from an mbuf list.
6216 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6217 char *cp, struct ucred *cred, struct thread *p)
6220 struct uio uio, *uiop = &uio;
6225 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6231 uiop->uio_rw = UIO_WRITE;
6232 uiop->uio_segflg = UIO_SYSSPACE;
6234 uiop->uio_offset = 0;
6235 uiop->uio_resid = len;
6237 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6239 uiop->uio_iovcnt = cnt;
6241 uiop->uio_iov = iv = NULL;
6242 uiop->uio_iovcnt = 0;
6245 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6256 * Remove Extended attribute vnode op.
6259 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6260 struct ucred *cred, struct thread *p)
6265 * Get rid of any delegations. I am not sure why this is required,
6266 * but RFC-8276 says so.
6268 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6272 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6278 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6279 if (error == EOPNOTSUPP)
6280 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6288 * List Extended Atribute vnode op into an mbuf list.
6291 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6292 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6300 /* First, find out the size of the extended attribute. */
6301 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6304 return (NFSERR_NOXATTR);
6305 if (siz <= cookie) {
6310 if (siz > cookie + *lenp) {
6311 siz = cookie + *lenp;
6315 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6316 if (siz > 10 * 1024 * 1024) {
6317 error = NFSERR_XATTR2BIG;
6320 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6321 iv.iov_base = *bufp;
6327 io.uio_rw = UIO_READ;
6328 io.uio_segflg = UIO_SYSSPACE;
6331 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6336 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6340 if (io.uio_resid > 0)
6341 siz -= io.uio_resid;
6346 free(*bufp, M_TEMP);
6353 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
6356 * Called once to initialize data structures...
6359 nfsd_modevent(module_t mod, int type, void *data)
6362 static int loaded = 0;
6369 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6370 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
6372 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
6375 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
6376 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
6377 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
6378 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
6379 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
6380 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
6387 #ifdef VV_DISABLEDELEG
6388 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
6389 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
6391 nfsd_call_servertimer = nfsrv_servertimer;
6392 nfsd_call_nfsd = nfssvc_nfsd;
6397 if (newnfs_numnfsd != 0) {
6402 #ifdef VV_DISABLEDELEG
6403 vn_deleg_ops.vndeleg_recall = NULL;
6404 vn_deleg_ops.vndeleg_disable = NULL;
6406 nfsd_call_servertimer = NULL;
6407 nfsd_call_nfsd = NULL;
6409 /* Clean out all NFSv4 state. */
6410 nfsrv_throwawayallstate(curthread);
6412 /* Clean the NFS server reply cache */
6413 nfsrvd_cleancache();
6415 /* Free up the krpc server pool. */
6416 if (nfsrvd_pool != NULL)
6417 svcpool_destroy(nfsrvd_pool);
6419 /* and get rid of the locks */
6420 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
6421 mtx_destroy(&nfsrchash_table[i].mtx);
6422 mtx_destroy(&nfsrcahash_table[i].mtx);
6424 mtx_destroy(&nfsrc_udpmtx);
6425 mtx_destroy(&nfs_v4root_mutex);
6426 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
6427 mtx_destroy(&nfsrv_dontlistlock_mtx);
6428 mtx_destroy(&nfsrv_recalllock_mtx);
6429 for (i = 0; i < nfsrv_sessionhashsize; i++)
6430 mtx_destroy(&nfssessionhash[i].mtx);
6431 if (nfslayouthash != NULL) {
6432 for (i = 0; i < nfsrv_layouthashsize; i++)
6433 mtx_destroy(&nfslayouthash[i].mtx);
6434 free(nfslayouthash, M_NFSDSESSION);
6436 lockdestroy(&nfsv4root_mnt.mnt_explock);
6437 free(nfsclienthash, M_NFSDCLIENT);
6438 free(nfslockhash, M_NFSDLOCKFILE);
6439 free(nfssessionhash, M_NFSDSESSION);
6451 static moduledata_t nfsd_mod = {
6456 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
6458 /* So that loader and kldload(2) can find us, wherever we are.. */
6459 MODULE_VERSION(nfsd, 1);
6460 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
6461 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
6462 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
6463 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);