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 #include <sys/capsicum.h>
38 #include <sys/extattr.h>
41 * Functions that perform the vfs operations required by the routines in
42 * nfsd_serv.c. It is hoped that this change will make the server more
46 #include <fs/nfs/nfsport.h>
47 #include <security/mac/mac_framework.h>
48 #include <sys/callout.h>
49 #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 int nfsrv_sessionhashsize;
62 extern struct nfslayouthash *nfslayouthash;
63 extern int nfsrv_layouthashsize;
64 extern struct mtx nfsrv_dslock_mtx;
65 extern int nfs_pnfsiothreads;
66 extern volatile int nfsrv_devidcnt;
67 extern int nfsrv_maxpnfsmirror;
68 extern uint32_t nfs_srvmaxio;
69 extern int nfs_bufpackets;
70 extern u_long sb_max_adj;
72 NFSD_VNET_DECLARE(int, nfsrv_numnfsd);
73 NFSD_VNET_DECLARE(struct nfsrv_stablefirst, nfsrv_stablefirst);
74 NFSD_VNET_DECLARE(SVCPOOL *, nfsrvd_pool);
75 NFSD_VNET_DECLARE(struct nfsclienthashhead *, nfsclienthash);
76 NFSD_VNET_DECLARE(struct nfslockhashhead *, nfslockhash);
77 NFSD_VNET_DECLARE(struct nfssessionhash *, nfssessionhash);
78 NFSD_VNET_DECLARE(struct nfsv4lock, nfsd_suspend_lock);
79 NFSD_VNET_DECLARE(struct nfsstatsv1 *, nfsstatsv1_p);
83 struct mtx nfsrc_udpmtx;
84 struct mtx nfs_v4root_mutex;
85 struct mtx nfsrv_dontlistlock_mtx;
86 struct mtx nfsrv_recalllock_mtx;
87 struct nfsrvfh nfs_pubfh;
89 int nfsd_debuglevel = 0;
90 static pid_t nfsd_master_pid = (pid_t)-1;
91 static char nfsd_master_comm[MAXCOMLEN + 1];
92 static struct timeval nfsd_master_start;
93 static uint32_t nfsv4_sysid = 0;
94 static fhandle_t zerofh;
96 NFSD_VNET_DEFINE(struct proc *, nfsd_master_proc) = NULL;
97 NFSD_VNET_DEFINE(struct nfsrvhashhead *, nfsrvudphashtbl);
98 NFSD_VNET_DEFINE(struct nfsrchash_bucket *, nfsrchash_table);
99 NFSD_VNET_DEFINE(struct nfsrchash_bucket *, nfsrcahash_table);
100 NFSD_VNET_DEFINE(struct nfsrvfh, nfs_rootfh);
101 NFSD_VNET_DEFINE(int, nfs_rootfhset) = 0;
102 NFSD_VNET_DEFINE(struct callout, nfsd_callout);
103 NFSD_VNET_DEFINE_STATIC(struct mount *, nfsv4root_mnt);
104 NFSD_VNET_DEFINE_STATIC(struct vfsoptlist, nfsv4root_opt);
105 NFSD_VNET_DEFINE_STATIC(struct vfsoptlist, nfsv4root_newopt);
106 NFSD_VNET_DEFINE_STATIC(bool, nfsrv_suspend_nfsd) = false;
107 NFSD_VNET_DEFINE_STATIC(bool, nfsrv_mntinited) = false;
109 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
111 static void nfsvno_updateds(struct vnode *, struct ucred *, struct thread *);
113 int nfsrv_enable_crossmntpt = 1;
114 static int nfs_commit_blks;
115 static int nfs_commit_miss;
116 extern int nfsrv_issuedelegs;
117 extern int nfsrv_dolocallocks;
118 extern struct nfsdevicehead nfsrv_devidhead;
120 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
122 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
123 struct mbuf **, struct iovec **);
124 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
126 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
128 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
129 int *, char *, fhandle_t *);
130 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
132 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
133 struct thread *, int, struct mbuf **, char *, struct mbuf **,
134 struct nfsvattr *, struct acl *, off_t *, int, bool *);
135 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
136 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
137 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
138 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
139 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
141 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
142 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
143 static int nfsrv_deallocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
144 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
145 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
146 struct vnode *, struct nfsmount **, int, struct acl *, int *);
147 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
148 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
149 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
150 struct vnode *, struct nfsmount *, struct nfsvattr *);
151 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
152 NFSPROC_T *, struct nfsmount *);
153 static int nfsrv_putfhname(fhandle_t *, char *);
154 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
155 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
156 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
157 struct vnode *, NFSPROC_T *);
158 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
159 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
161 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
163 int nfs_pnfsio(task_fn_t *, void *);
165 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
167 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
168 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
169 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
171 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
173 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
174 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
175 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
176 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
177 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
178 0, "Debug level for NFS server");
179 NFSD_VNET_DECLARE(int, nfsd_enable_stringtouid);
180 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid,
181 CTLFLAG_NFSD_VNET | CTLFLAG_RW, &NFSD_VNET_NAME(nfsd_enable_stringtouid),
182 0, "Enable nfsd to accept numeric owner_names");
183 static int nfsrv_pnfsgetdsattr = 1;
184 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
185 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
188 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
190 * The dsN subdirectories for the increased values must have been created
191 * on all DS servers before this increase is done.
193 u_int nfsrv_dsdirsize = 20;
195 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
197 int error, newdsdirsize;
199 newdsdirsize = nfsrv_dsdirsize;
200 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
201 if (error != 0 || req->newptr == NULL)
203 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
206 nfsrv_dsdirsize = newdsdirsize;
209 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
210 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
211 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
214 * nfs_srvmaxio can only be increased and only when the nfsd threads are
215 * not running. The setting must be a power of 2, with the current limit of
219 sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)
225 newsrvmaxio = nfs_srvmaxio;
226 error = sysctl_handle_int(oidp, &newsrvmaxio, 0, req);
227 if (error != 0 || req->newptr == NULL)
229 if (newsrvmaxio == nfs_srvmaxio)
231 if (newsrvmaxio < nfs_srvmaxio) {
232 printf("nfsd: vfs.nfsd.srvmaxio can only be increased\n");
235 if (newsrvmaxio > 1048576) {
236 printf("nfsd: vfs.nfsd.srvmaxio cannot be > 1Mbyte\n");
239 if ((newsrvmaxio & (newsrvmaxio - 1)) != 0) {
240 printf("nfsd: vfs.nfsd.srvmaxio must be a power of 2\n");
245 * Check that kern.ipc.maxsockbuf is large enough for
246 * newsrviomax, given the setting of vfs.nfs.bufpackets.
248 if ((newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets >
251 * Suggest vfs.nfs.bufpackets * maximum RPC message for
254 tval = (newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets;
257 * Convert suggested sb_max_adj value to a suggested
258 * sb_max value, which is what is set via kern.ipc.maxsockbuf.
259 * Perform the inverse calculation of (from uipc_sockbuf.c):
260 * sb_max_adj = (u_quad_t)sb_max * MCLBYTES /
261 * (MSIZE + MCLBYTES);
262 * XXX If the calculation of sb_max_adj from sb_max changes,
263 * this calculation must be changed as well.
265 tval *= (MSIZE + MCLBYTES); /* Brackets for readability. */
266 tval += MCLBYTES - 1; /* Round up divide. */
268 printf("nfsd: set kern.ipc.maxsockbuf to a minimum of "
269 "%ju to support %ubyte NFS I/O\n", (uintmax_t)tval,
275 if (newnfs_numnfsd != 0) {
277 printf("nfsd: cannot set vfs.nfsd.srvmaxio when nfsd "
278 "threads are running\n");
283 nfs_srvmaxio = newsrvmaxio;
287 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, srvmaxio,
288 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
289 sysctl_srvmaxio, "IU", "Maximum I/O size in bytes");
291 #define MAX_REORDERED_RPC 16
292 #define NUM_HEURISTIC 1031
293 #define NHUSE_INIT 64
295 #define NHUSE_MAX 2048
297 static struct nfsheur {
298 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
299 off_t nh_nextoff; /* next offset for sequential detection */
300 int nh_use; /* use count for selection */
301 int nh_seqcount; /* heuristic */
302 } nfsheur[NUM_HEURISTIC];
305 * Heuristic to detect sequential operation.
307 static struct nfsheur *
308 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
313 /* Locate best candidate. */
315 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
318 if (nfsheur[hi].nh_vp == vp) {
322 if (nfsheur[hi].nh_use > 0)
323 --nfsheur[hi].nh_use;
324 hi = (hi + 1) % NUM_HEURISTIC;
325 if (nfsheur[hi].nh_use < nh->nh_use)
329 /* Initialize hint if this is a new file. */
330 if (nh->nh_vp != vp) {
332 nh->nh_nextoff = uio->uio_offset;
333 nh->nh_use = NHUSE_INIT;
334 if (uio->uio_offset == 0)
340 /* Calculate heuristic. */
341 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
342 uio->uio_offset == nh->nh_nextoff) {
343 /* See comments in vfs_vnops.c:sequential_heuristic(). */
344 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
345 if (nh->nh_seqcount > IO_SEQMAX)
346 nh->nh_seqcount = IO_SEQMAX;
347 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
348 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
349 /* Probably a reordered RPC, leave seqcount alone. */
350 } else if (nh->nh_seqcount > 1) {
351 nh->nh_seqcount /= 2;
355 nh->nh_use += NHUSE_INC;
356 if (nh->nh_use > NHUSE_MAX)
357 nh->nh_use = NHUSE_MAX;
362 * Get attributes into nfsvattr structure.
365 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
366 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
367 nfsattrbit_t *attrbitp)
369 int error, gotattr, lockedit = 0;
372 if (vpislocked == 0) {
374 * When vpislocked == 0, the vnode is either exclusively
375 * locked by this thread or not locked by this thread.
376 * As such, shared lock it, if not exclusively locked.
378 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
380 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
385 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
386 * attributes, as required.
387 * This needs to be done for regular files if:
388 * - non-NFSv4 RPCs or
389 * - when attrbitp == NULL or
390 * - an NFSv4 RPC with any of the above attributes in attrbitp.
391 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
392 * these attributes. nfsrv_proxyds() will return an error if the
393 * server is not a pNFS one.
396 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
397 (nd->nd_flag & ND_NFSV4) == 0 ||
398 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
399 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
400 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
401 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
402 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
403 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
404 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
410 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
415 * If we got the Change, Size and Modify Time from the DS,
419 nvap->na_atime = na.na_atime;
420 nvap->na_mtime = na.na_mtime;
421 nvap->na_filerev = na.na_filerev;
422 nvap->na_size = na.na_size;
423 nvap->na_bytes = na.na_bytes;
425 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
426 error, (uintmax_t)na.na_filerev);
433 * Get a file handle for a vnode.
436 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
440 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
441 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
442 error = VOP_VPTOFH(vp, &fhp->fh_fid);
449 * Perform access checking for vnodes obtained from file handles that would
450 * refer to files already opened by a Unix client. You cannot just use
451 * vn_writechk() and VOP_ACCESSX() for two reasons.
452 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
454 * 2 - The owner is to be given access irrespective of mode bits for some
455 * operations, so that processes that chmod after opening a file don't
459 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
460 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
461 u_int32_t *supportedtypep)
464 int error = 0, getret = 0;
466 if (vpislocked == 0) {
467 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
472 if (accmode & VWRITE) {
473 /* Just vn_writechk() changed to check rdonly */
475 * Disallow write attempts on read-only file systems;
476 * unless the file is a socket or a block or character
477 * device resident on the file system.
479 if (NFSVNO_EXRDONLY(exp) ||
480 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
481 switch (vp->v_type) {
491 * If there's shared text associated with
492 * the inode, try to free it up once. If
493 * we fail, we can't allow writing.
495 if (VOP_IS_TEXT(vp) && error == 0)
505 * Should the override still be applied when ACLs are enabled?
507 error = VOP_ACCESSX(vp, accmode, cred, p);
508 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
510 * Try again with VEXPLICIT_DENY, to see if the test for
511 * deletion is supported.
513 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
515 if (vp->v_type == VDIR) {
516 accmode &= ~(VDELETE | VDELETE_CHILD);
518 error = VOP_ACCESSX(vp, accmode, cred, p);
519 } else if (supportedtypep != NULL) {
520 *supportedtypep &= ~NFSACCESS_DELETE;
526 * Allow certain operations for the owner (reads and writes
527 * on files that are already open).
529 if (override != NFSACCCHK_NOOVERRIDE &&
530 (error == EPERM || error == EACCES)) {
531 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
533 else if (override & NFSACCCHK_ALLOWOWNER) {
534 getret = VOP_GETATTR(vp, &vattr, cred);
535 if (getret == 0 && cred->cr_uid == vattr.va_uid)
548 * Set attribute(s) vnop.
551 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
552 struct thread *p, struct nfsexstuff *exp)
554 u_quad_t savsize = 0;
559 * If this is an exported file system and a pNFS service is running,
560 * don't VOP_SETATTR() of size for the MDS file system.
564 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
565 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
566 nvap->na_vattr.va_size > 0) {
567 savsize = nvap->na_vattr.va_size;
568 nvap->na_vattr.va_size = VNOVAL;
569 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
570 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
571 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
572 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
573 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
579 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
581 nvap->na_vattr.va_size = savsize;
582 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
583 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
584 nvap->na_vattr.va_size != VNOVAL ||
585 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
586 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
587 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
588 /* Never modify birthtime on a DS file. */
589 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
590 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
591 /* For a pNFS server, set the attributes on the DS file. */
592 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
593 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
594 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
603 * Set up nameidata for a lookup() call and do it.
606 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
607 struct vnode *dp, int islocked, struct nfsexstuff *exp,
608 struct vnode **retdirp)
610 struct componentname *cnp = &ndp->ni_cnd;
614 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
619 cnp->cn_nameptr = cnp->cn_pnbuf;
622 * Extract and set starting directory.
624 if (dp->v_type != VDIR) {
629 nfsvno_relpathbuf(ndp);
637 if (NFSVNO_EXRDONLY(exp))
638 cnp->cn_flags |= RDONLY;
639 ndp->ni_segflg = UIO_SYSSPACE;
641 if (nd->nd_flag & ND_PUBLOOKUP) {
643 if (cnp->cn_pnbuf[0] == '/') {
646 * Check for degenerate pathnames here, since lookup()
649 for (i = 1; i < ndp->ni_pathlen; i++)
650 if (cnp->cn_pnbuf[i] != '/')
652 if (i == ndp->ni_pathlen) {
653 error = NFSERR_ACCES;
659 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
660 (nd->nd_flag & ND_NFSV4) == 0) {
662 * Only cross mount points for NFSv4 when doing a
663 * mount while traversing the file system above
664 * the mount point, unless nfsrv_enable_crossmntpt is set.
666 cnp->cn_flags |= NOCROSSMOUNT;
670 * Initialize for scan, set ni_startdir and bump ref on dp again
671 * because lookup() will dereference ni_startdir.
674 ndp->ni_startdir = dp;
675 ndp->ni_rootdir = rootvnode;
676 ndp->ni_topdir = NULL;
679 cnp->cn_flags |= LOCKLEAF;
681 cnp->cn_nameptr = cnp->cn_pnbuf;
683 * Call lookup() to do the real work. If an error occurs,
684 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
685 * we do not have to dereference anything before returning.
686 * In either case ni_startdir will be dereferenced and NULLed
689 error = vfs_lookup(ndp);
694 * Check for encountering a symbolic link. Trivial
695 * termination occurs if no symlink encountered.
697 if ((cnp->cn_flags & ISSYMLINK) == 0) {
698 if (ndp->ni_vp && !lockleaf)
699 NFSVOPUNLOCK(ndp->ni_vp);
706 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
707 NFSVOPUNLOCK(ndp->ni_dvp);
708 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
713 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
717 if (ndp->ni_pathlen > 1)
718 cp = uma_zalloc(namei_zone, M_WAITOK);
722 aiov.iov_len = MAXPATHLEN;
723 auio.uio_iov = &aiov;
726 auio.uio_rw = UIO_READ;
727 auio.uio_segflg = UIO_SYSSPACE;
729 auio.uio_resid = MAXPATHLEN;
730 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
733 if (ndp->ni_pathlen > 1)
734 uma_zfree(namei_zone, cp);
740 linklen = MAXPATHLEN - auio.uio_resid;
745 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
746 error = ENAMETOOLONG;
751 * Adjust or replace path
753 if (ndp->ni_pathlen > 1) {
754 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
755 uma_zfree(namei_zone, cnp->cn_pnbuf);
758 cnp->cn_pnbuf[linklen] = '\0';
759 ndp->ni_pathlen += linklen;
762 * Cleanup refs for next loop and check if root directory
763 * should replace current directory. Normally ni_dvp
764 * becomes the new base directory and is cleaned up when
765 * we loop. Explicitly null pointers after invalidation
766 * to clarify operation.
771 if (cnp->cn_pnbuf[0] == '/') {
773 ndp->ni_dvp = ndp->ni_rootdir;
776 ndp->ni_startdir = ndp->ni_dvp;
780 cnp->cn_flags &= ~LOCKLEAF;
784 nfsvno_relpathbuf(ndp);
787 ndp->ni_startdir = NULL;
788 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
793 NFSEXITCODE2(error, nd);
798 * Set up a pathname buffer and return a pointer to it and, optionally
799 * set a hash pointer.
802 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
804 struct componentname *cnp = &ndp->ni_cnd;
806 cnp->cn_flags |= (NOMACCHECK);
807 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
810 *bufpp = cnp->cn_pnbuf;
814 * Release the above path buffer, if not released by nfsvno_namei().
817 nfsvno_relpathbuf(struct nameidata *ndp)
820 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
821 ndp->ni_cnd.cn_pnbuf = NULL;
825 * Readlink vnode op into an mbuf list.
828 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
829 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
832 struct uio io, *uiop = &io;
833 struct mbuf *mp, *mp3;
834 int len, tlen, error = 0;
836 len = NFS_MAXPATHLEN;
838 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
841 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
843 uiop->uio_offset = 0;
844 uiop->uio_resid = len;
845 uiop->uio_rw = UIO_READ;
846 uiop->uio_segflg = UIO_SYSSPACE;
848 error = VOP_READLINK(vp, uiop, cred);
855 if (uiop->uio_resid > 0) {
856 len -= uiop->uio_resid;
857 tlen = NFSM_RNDUP(len);
861 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
862 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
875 * Create an mbuf chain and an associated iovec that can be used to Read
876 * or Getextattr of data.
877 * Upon success, return pointers to the first and last mbufs in the chain
878 * plus the malloc'd iovec and its iovlen.
881 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
884 struct mbuf *m, *m2 = NULL, *m3;
891 * Generate the mbuf list with the uio_iov ref. to it.
898 siz = min(M_TRAILINGSPACE(m), left);
907 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
913 panic("nfsrv_createiovec iov");
914 siz = min(M_TRAILINGSPACE(m), left);
916 iv->iov_base = mtod(m, caddr_t) + m->m_len;
931 * Create an mbuf chain and an associated iovec that can be used to Read
932 * or Getextattr of data.
933 * Upon success, return pointers to the first and last mbufs in the chain
934 * plus the malloc'd iovec and its iovlen.
935 * Same as above, but creates ext_pgs mbuf(s).
938 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
939 struct mbuf **mpendp, struct iovec **ivp)
941 struct mbuf *m, *m2 = NULL, *m3;
943 int i, left, pgno, siz;
948 * Generate the mbuf list with the uio_iov ref. to it.
952 siz = min(left, maxextsiz);
953 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
962 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
969 panic("nfsvno_createiovec_extpgs iov");
970 siz = min(PAGE_SIZE, left);
972 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
975 if (pgno == m->m_epg_npgs - 1)
976 m->m_epg_last_len = siz;
982 if (pgno == m->m_epg_npgs && left > 0) {
985 panic("nfsvno_createiovec_extpgs iov");
995 * Read vnode op call into mbuf list.
998 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
999 int maxextsiz, struct thread *p, struct mbuf **mpp,
1000 struct mbuf **mpendp)
1004 int error = 0, len, tlen, ioflag = 0;
1006 struct uio io, *uiop = &io;
1010 * Attempt to read from a DS file. A return of ENOENT implies
1011 * there is no DS file to read.
1013 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
1014 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
1015 if (error != ENOENT)
1018 len = NFSM_RNDUP(cnt);
1020 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
1023 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
1025 uiop->uio_offset = off;
1026 uiop->uio_resid = len;
1027 uiop->uio_rw = UIO_READ;
1028 uiop->uio_segflg = UIO_SYSSPACE;
1029 uiop->uio_td = NULL;
1030 nh = nfsrv_sequential_heuristic(uiop, vp);
1031 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
1032 /* XXX KDM make this more systematic? */
1033 NFSD_VNET(nfsstatsv1_p)->srvbytes[NFSV4OP_READ] += uiop->uio_resid;
1034 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
1041 nh->nh_nextoff = uiop->uio_offset;
1042 tlen = len - uiop->uio_resid;
1043 cnt = cnt < tlen ? cnt : tlen;
1044 tlen = NFSM_RNDUP(cnt);
1048 } else if (len != tlen || tlen != cnt)
1049 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
1059 * Create the iovec for the mbuf chain passed in as an argument.
1060 * The "cp" argument is where the data starts within the first mbuf in
1061 * the chain. It returns the iovec and the iovcnt.
1064 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
1072 * Loop through the mbuf chain, counting how many mbufs are a
1073 * part of this write operation, so the iovec size is known.
1078 i = mtod(mp, caddr_t) + mp->m_len - cp;
1092 /* Now, create the iovec. */
1094 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1097 i = mtod(mp, caddr_t) + mp->m_len - cp;
1101 panic("nfsrv_createiovecw");
1112 cp = mtod(mp, caddr_t);
1119 * Write vnode op from an mbuf list.
1122 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1123 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1126 int cnt, ioflags, error;
1127 struct uio io, *uiop = &io;
1131 * Attempt to write to a DS file. A return of ENOENT implies
1132 * there is no DS file to write.
1134 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1135 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1136 if (error != ENOENT) {
1137 *stable = NFSWRITE_FILESYNC;
1141 if (*stable == NFSWRITE_UNSTABLE)
1142 ioflags = IO_NODELOCKED;
1144 ioflags = (IO_SYNC | IO_NODELOCKED);
1145 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1149 uiop->uio_iovcnt = cnt;
1150 uiop->uio_resid = retlen;
1151 uiop->uio_rw = UIO_WRITE;
1152 uiop->uio_segflg = UIO_SYSSPACE;
1153 NFSUIOPROC(uiop, p);
1154 uiop->uio_offset = off;
1155 nh = nfsrv_sequential_heuristic(uiop, vp);
1156 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1157 /* XXX KDM make this more systematic? */
1158 NFSD_VNET(nfsstatsv1_p)->srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1159 error = VOP_WRITE(vp, uiop, ioflags, cred);
1161 nh->nh_nextoff = uiop->uio_offset;
1169 * Common code for creating a regular file (plus special files for V2).
1172 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1173 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1174 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1178 struct thread *p = curthread;
1180 error = nd->nd_repstat;
1181 if (!error && ndp->ni_vp == NULL) {
1182 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1183 error = VOP_CREATE(ndp->ni_dvp,
1184 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1185 /* For a pNFS server, create the data file on a DS. */
1186 if (error == 0 && nvap->na_type == VREG) {
1188 * Create a data file on a DS for a pNFS server.
1189 * This function just returns if not
1190 * running a pNFS DS or the creation fails.
1192 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1195 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1197 nfsvno_relpathbuf(ndp);
1199 if (*exclusive_flagp) {
1200 *exclusive_flagp = 0;
1201 NFSVNO_ATTRINIT(nvap);
1202 nvap->na_atime.tv_sec = cverf[0];
1203 nvap->na_atime.tv_nsec = cverf[1];
1204 error = VOP_SETATTR(ndp->ni_vp,
1205 &nvap->na_vattr, nd->nd_cred);
1209 error = NFSERR_NOTSUPP;
1214 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1215 * (This implies, just get out on an error.)
1217 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1218 nvap->na_type == VFIFO) {
1219 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1220 nvap->na_type = VFIFO;
1221 if (nvap->na_type != VFIFO &&
1222 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1223 nfsvno_relpathbuf(ndp);
1227 nvap->na_rdev = rdev;
1228 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1229 &ndp->ni_cnd, &nvap->na_vattr);
1230 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1232 nfsvno_relpathbuf(ndp);
1236 nfsvno_relpathbuf(ndp);
1244 * Handle cases where error is already set and/or
1246 * 1 - clean up the lookup
1247 * 2 - iff !error and na_size set, truncate it
1249 nfsvno_relpathbuf(ndp);
1251 if (ndp->ni_dvp == *vpp)
1255 if (!error && nvap->na_size != VNOVAL) {
1256 error = nfsvno_accchk(*vpp, VWRITE,
1257 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1258 NFSACCCHK_VPISLOCKED, NULL);
1260 tempsize = nvap->na_size;
1261 NFSVNO_ATTRINIT(nvap);
1262 nvap->na_size = tempsize;
1263 error = nfsvno_setattr(*vpp, nvap,
1264 nd->nd_cred, p, exp);
1277 * Do a mknod vnode op.
1280 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1284 __enum_uint8(vtype) vtyp;
1286 vtyp = nvap->na_type;
1288 * Iff doesn't exist, create it.
1291 nfsvno_relpathbuf(ndp);
1297 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1298 nfsvno_relpathbuf(ndp);
1300 error = NFSERR_BADTYPE;
1303 if (vtyp == VSOCK) {
1304 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1305 &ndp->ni_cnd, &nvap->na_vattr);
1306 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1308 nfsvno_relpathbuf(ndp);
1310 if (nvap->na_type != VFIFO &&
1311 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1312 nfsvno_relpathbuf(ndp);
1316 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1317 &ndp->ni_cnd, &nvap->na_vattr);
1318 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1320 nfsvno_relpathbuf(ndp);
1322 * Since VOP_MKNOD returns the ni_vp, I can't
1323 * see any reason to do the lookup.
1336 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1337 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1341 if (ndp->ni_vp != NULL) {
1342 if (ndp->ni_dvp == ndp->ni_vp)
1347 nfsvno_relpathbuf(ndp);
1351 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1353 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL, false);
1354 nfsvno_relpathbuf(ndp);
1365 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1366 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1367 struct nfsexstuff *exp)
1372 nfsvno_relpathbuf(ndp);
1373 if (ndp->ni_dvp == ndp->ni_vp)
1382 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1383 &nvap->na_vattr, pathcp);
1385 * Although FreeBSD still had the lookup code in
1386 * it for 7/current, there doesn't seem to be any
1387 * point, since VOP_SYMLINK() returns the ni_vp.
1388 * Just vput it for v2.
1390 VOP_VPUT_PAIR(ndp->ni_dvp, &ndp->ni_vp, !not_v2 && error == 0);
1391 nfsvno_relpathbuf(ndp);
1399 * Parse symbolic link arguments.
1400 * This function has an ugly side effect. It will malloc() an area for
1401 * the symlink and set iov_base to point to it, only if it succeeds.
1402 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1406 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1407 struct thread *p, char **pathcpp, int *lenp)
1410 char *pathcp = NULL;
1412 struct nfsv2_sattr *sp;
1416 if ((nd->nd_flag & ND_NFSV3) &&
1417 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1419 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1420 len = fxdr_unsigned(int, *tl);
1421 if (len > NFS_MAXPATHLEN || len <= 0) {
1425 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1426 error = nfsrv_mtostr(nd, pathcp, len);
1429 if (nd->nd_flag & ND_NFSV2) {
1430 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1431 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1435 NFSEXITCODE2(0, nd);
1439 free(pathcp, M_TEMP);
1440 NFSEXITCODE2(error, nd);
1445 * Remove a non-directory object.
1448 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1449 struct thread *p, struct nfsexstuff *exp)
1451 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1452 int error = 0, mirrorcnt;
1453 char fname[PNFS_FILENAME_LEN + 1];
1458 if (vp->v_type == VDIR)
1459 error = NFSERR_ISDIR;
1461 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1464 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1466 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1467 if (error == 0 && dsdvp[0] != NULL)
1468 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1469 if (ndp->ni_dvp == vp)
1474 nfsvno_relpathbuf(ndp);
1480 * Remove a directory.
1483 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1484 struct thread *p, struct nfsexstuff *exp)
1490 if (vp->v_type != VDIR) {
1495 * No rmdir "." please.
1497 if (ndp->ni_dvp == vp) {
1502 * The root of a mounted filesystem cannot be deleted.
1504 if (vp->v_vflag & VV_ROOT)
1508 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1509 if (ndp->ni_dvp == vp)
1514 nfsvno_relpathbuf(ndp);
1523 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1524 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1526 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1527 int error = 0, mirrorcnt;
1528 char fname[PNFS_FILENAME_LEN + 1];
1532 fvp = fromndp->ni_vp;
1534 vrele(fromndp->ni_dvp);
1539 tdvp = tondp->ni_dvp;
1542 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1543 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1545 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1546 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1549 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1550 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1555 * A rename to '.' or '..' results in a prematurely
1556 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1559 if ((tondp->ni_cnd.cn_namelen == 1 &&
1560 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1561 (tondp->ni_cnd.cn_namelen == 2 &&
1562 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1563 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1568 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1569 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1572 if (fvp->v_mount != tdvp->v_mount) {
1573 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1577 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1582 * If source and destination are the same, there is nothing to
1583 * do. Set error to -1 to indicate this.
1588 if (ndflag & ND_NFSV4) {
1589 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1590 error = nfsrv_checkremove(fvp, 0, NULL,
1591 (nfsquad_t)((u_quad_t)0), p);
1596 error = nfsrv_checkremove(tvp, 1, NULL,
1597 (nfsquad_t)((u_quad_t)0), p);
1600 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1601 * that the NFSv4 client won't be confused by the rename.
1602 * Since nfsd_recalldelegation() can only be called on an
1603 * unlocked vnode at this point and fvp is the file that will
1604 * still exist after the rename, just do fvp.
1606 nfsd_recalldelegation(fvp, p);
1608 if (error == 0 && tvp != NULL) {
1609 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1610 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1611 " dsdvp=%p\n", dsdvp[0]);
1615 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1616 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1625 vrele(fromndp->ni_dvp);
1632 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1633 * if the rename succeeded, the DS file for the tvp needs to be
1636 if (error == 0 && dsdvp[0] != NULL) {
1637 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1638 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1641 nfsvno_relpathbuf(tondp);
1643 nfsvno_relpathbuf(fromndp);
1652 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1653 struct thread *p, struct nfsexstuff *exp)
1663 if (vp->v_mount != xp->v_mount)
1667 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1668 if (!VN_IS_DOOMED(vp))
1669 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1672 if (ndp->ni_dvp == vp) {
1677 VOP_VPUT_PAIR(ndp->ni_dvp, &vp, true);
1680 if (ndp->ni_dvp == ndp->ni_vp)
1687 nfsvno_relpathbuf(ndp);
1693 * Do the fsync() appropriate for the commit.
1696 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1702 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1703 * file is done. At this time VOP_FSYNC does not accept offset and
1704 * byte count parameters so call VOP_FSYNC the whole file for now.
1705 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1706 * File systems that do not use the buffer cache (as indicated
1707 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1709 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1710 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1712 * Give up and do the whole thing
1714 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1715 VM_OBJECT_WLOCK(vp->v_object);
1716 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1717 VM_OBJECT_WUNLOCK(vp->v_object);
1719 error = VOP_FSYNC(vp, MNT_WAIT, td);
1722 * Locate and synchronously write any buffers that fall
1723 * into the requested range. Note: we are assuming that
1724 * f_iosize is a power of 2.
1726 int iosize = vp->v_mount->mnt_stat.f_iosize;
1727 int iomask = iosize - 1;
1732 * Align to iosize boundary, super-align to page boundary.
1735 cnt += off & iomask;
1736 off &= ~(u_quad_t)iomask;
1738 if (off & PAGE_MASK) {
1739 cnt += off & PAGE_MASK;
1740 off &= ~(u_quad_t)PAGE_MASK;
1742 lblkno = off / iosize;
1744 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1745 VM_OBJECT_WLOCK(vp->v_object);
1746 vm_object_page_clean(vp->v_object, off, off + cnt,
1748 VM_OBJECT_WUNLOCK(vp->v_object);
1757 * If we have a buffer and it is marked B_DELWRI we
1758 * have to lock and write it. Otherwise the prior
1759 * write is assumed to have already been committed.
1761 * gbincore() can return invalid buffers now so we
1762 * have to check that bit as well (though B_DELWRI
1763 * should not be set if B_INVAL is set there could be
1764 * a race here since we haven't locked the buffer).
1766 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1767 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1768 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1770 continue; /* retry */
1772 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1775 bp->b_flags &= ~B_ASYNC;
1798 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1804 if (nfsrv_devidcnt > 0) {
1805 /* For a pNFS service, get the DS numbers. */
1806 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1807 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1813 error = VFS_STATFS(vp->v_mount, sf);
1816 sf->f_blocks = tsf->f_blocks;
1817 sf->f_bavail = tsf->f_bavail;
1818 sf->f_bfree = tsf->f_bfree;
1819 sf->f_bsize = tsf->f_bsize;
1822 * Since NFS handles these values as unsigned on the
1823 * wire, there is no way to represent negative values,
1824 * so set them to 0. Without this, they will appear
1825 * to be very large positive values for clients like
1828 if (sf->f_bavail < 0)
1830 if (sf->f_ffree < 0)
1839 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1840 * must handle nfsrv_opencheck() calls after any other access checks.
1843 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1844 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1845 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1846 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, bool done_namei,
1847 struct nfsexstuff *exp, struct vnode **vpp)
1849 struct vnode *vp = NULL;
1851 struct nfsexstuff nes;
1852 struct thread *p = curthread;
1853 uint32_t oldrepstat;
1855 if (ndp->ni_vp == NULL) {
1857 * If nfsrv_opencheck() sets nd_repstat, done_namei needs to be
1858 * set true, since cleanup after nfsvno_namei() is needed.
1860 oldrepstat = nd->nd_repstat;
1861 nd->nd_repstat = nfsrv_opencheck(clientid,
1862 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1863 if (nd->nd_repstat != 0 && oldrepstat == 0)
1866 if (!nd->nd_repstat) {
1867 if (ndp->ni_vp == NULL) {
1868 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1869 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1870 /* For a pNFS server, create the data file on a DS. */
1871 if (nd->nd_repstat == 0) {
1873 * Create a data file on a DS for a pNFS server.
1874 * This function just returns if not
1875 * running a pNFS DS or the creation fails.
1877 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1880 VOP_VPUT_PAIR(ndp->ni_dvp, nd->nd_repstat == 0 ?
1881 &ndp->ni_vp : NULL, false);
1882 nfsvno_relpathbuf(ndp);
1883 if (!nd->nd_repstat) {
1884 if (*exclusive_flagp) {
1885 *exclusive_flagp = 0;
1886 NFSVNO_ATTRINIT(nvap);
1887 nvap->na_atime.tv_sec = cverf[0];
1888 nvap->na_atime.tv_nsec = cverf[1];
1889 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1890 &nvap->na_vattr, cred);
1891 if (nd->nd_repstat != 0) {
1894 nd->nd_repstat = NFSERR_NOTSUPP;
1896 NFSSETBIT_ATTRBIT(attrbitp,
1897 NFSATTRBIT_TIMEACCESS);
1899 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1900 aclp, p, attrbitp, exp);
1905 nfsvno_relpathbuf(ndp);
1907 if (create == NFSV4OPEN_CREATE) {
1908 if (ndp->ni_dvp == vp)
1913 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1914 if (ndp->ni_cnd.cn_flags & RDONLY)
1915 NFSVNO_SETEXRDONLY(&nes);
1917 NFSVNO_EXINIT(&nes);
1918 nd->nd_repstat = nfsvno_accchk(vp,
1919 VWRITE, cred, &nes, p,
1920 NFSACCCHK_NOOVERRIDE,
1921 NFSACCCHK_VPISLOCKED, NULL);
1922 nd->nd_repstat = nfsrv_opencheck(clientid,
1923 stateidp, stp, vp, nd, p, nd->nd_repstat);
1924 if (!nd->nd_repstat) {
1925 tempsize = nvap->na_size;
1926 NFSVNO_ATTRINIT(nvap);
1927 nvap->na_size = tempsize;
1928 nd->nd_repstat = nfsvno_setattr(vp,
1929 nvap, cred, p, exp);
1931 } else if (vp->v_type == VREG) {
1932 nd->nd_repstat = nfsrv_opencheck(clientid,
1933 stateidp, stp, vp, nd, p, nd->nd_repstat);
1936 } else if (done_namei) {
1937 KASSERT(create == NFSV4OPEN_CREATE,
1938 ("nfsvno_open: not create"));
1940 * done_namei is set when nfsvno_namei() has completed
1941 * successfully, but a subsequent error was set in
1942 * nd_repstat. As such, cleanup of the nfsvno_namei()
1943 * results is required.
1945 nfsvno_relpathbuf(ndp);
1946 if (ndp->ni_dvp == ndp->ni_vp)
1955 NFSEXITCODE2(0, nd);
1959 * Updates the file rev and sets the mtime and ctime
1960 * to the current clock time, returning the va_filerev and va_Xtime
1962 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1965 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1966 struct nfsrv_descript *nd, struct thread *p)
1971 vfs_timestamp(&va.va_mtime);
1972 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1973 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1974 if (VN_IS_DOOMED(vp))
1977 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1978 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1983 * Glue routine to nfsv4_fillattr().
1986 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1987 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1988 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1989 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1995 if (nfsrv_devidcnt > 0 &&
1996 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1997 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1998 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1999 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
2000 error = nfsrv_pnfsstatfs(sf, mp);
2006 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
2007 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
2008 mounted_on_fileno, sf);
2010 NFSEXITCODE2(0, nd);
2014 /* Since the Readdir vnode ops vary, put the entire functions in here. */
2016 * nfs readdir service
2017 * - mallocs what it thinks is enough to read
2018 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
2019 * - calls VOP_READDIR()
2020 * - loops around building the reply
2021 * if the output generated exceeds count break out of loop
2022 * The NFSM_CLGET macro is used here so that the reply will be packed
2023 * tightly in mbuf clusters.
2024 * - it trims out records with d_fileno == 0
2025 * this doesn't matter for Unix clients, but they might confuse clients
2027 * - it trims out records with d_type == DT_WHT
2028 * these cannot be seen through NFS (unless we extend the protocol)
2029 * The alternate call nfsrvd_readdirplus() does lookups as well.
2030 * PS: The NFS protocol spec. does not clarify what the "count" byte
2031 * argument is a count of.. just name strings and file id's or the
2032 * entire reply rpc or ...
2033 * I tried just file name and id sizes and it confused the Sun client,
2034 * so I am using the full rpc size now. The "paranoia.." comment refers
2035 * to including the status longwords that are not a part of the dir.
2036 * "entry" structures, but are in the rpc.
2039 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2040 struct vnode *vp, struct nfsexstuff *exp)
2045 char *cpos, *cend, *rbuf;
2047 int nlen, error = 0, getret = 1;
2048 int siz, cnt, fullsiz, eofflag, ncookies;
2049 u_int64_t off, toff, verf __unused;
2050 uint64_t *cookies = NULL, *cookiep;
2054 struct thread *p = curthread;
2056 if (nd->nd_repstat) {
2057 nfsrv_postopattr(nd, getret, &at);
2060 if (nd->nd_flag & ND_NFSV2) {
2061 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2062 off = fxdr_unsigned(u_quad_t, *tl++);
2064 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2065 off = fxdr_hyper(tl);
2067 verf = fxdr_hyper(tl);
2071 cnt = fxdr_unsigned(int, *tl);
2072 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2073 cnt = NFS_SRVMAXDATA(nd);
2074 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2076 if (nd->nd_flag & ND_NFSV3) {
2077 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2081 * va_filerev is not sufficient as a cookie verifier,
2082 * since it is not supposed to change when entries are
2083 * removed/added unless that offset cookies returned to
2084 * the client are no longer valid.
2086 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2087 nd->nd_repstat = NFSERR_BAD_COOKIE;
2090 if (!nd->nd_repstat && vp->v_type != VDIR)
2091 nd->nd_repstat = NFSERR_NOTDIR;
2092 if (nd->nd_repstat == 0 && cnt == 0) {
2093 if (nd->nd_flag & ND_NFSV2)
2094 /* NFSv2 does not have NFSERR_TOOSMALL */
2095 nd->nd_repstat = EPERM;
2097 nd->nd_repstat = NFSERR_TOOSMALL;
2099 if (!nd->nd_repstat)
2100 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2101 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2102 NFSACCCHK_VPISLOCKED, NULL);
2103 if (nd->nd_repstat) {
2105 if (nd->nd_flag & ND_NFSV3)
2106 nfsrv_postopattr(nd, getret, &at);
2109 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2110 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2114 free(cookies, M_TEMP);
2122 io.uio_offset = (off_t)off;
2124 io.uio_segflg = UIO_SYSSPACE;
2125 io.uio_rw = UIO_READ;
2127 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2129 off = (u_int64_t)io.uio_offset;
2131 siz -= io.uio_resid;
2133 if (!cookies && !nd->nd_repstat)
2134 nd->nd_repstat = NFSERR_PERM;
2135 if (nd->nd_flag & ND_NFSV3) {
2136 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2137 if (!nd->nd_repstat)
2138 nd->nd_repstat = getret;
2142 * Handles the failed cases. nd->nd_repstat == 0 past here.
2144 if (nd->nd_repstat) {
2148 free(cookies, M_TEMP);
2149 if (nd->nd_flag & ND_NFSV3)
2150 nfsrv_postopattr(nd, getret, &at);
2154 * If nothing read, return eof
2159 if (nd->nd_flag & ND_NFSV2) {
2160 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2162 nfsrv_postopattr(nd, getret, &at);
2163 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2164 txdr_hyper(at.na_filerev, tl);
2167 *tl++ = newnfs_false;
2170 free(cookies, M_TEMP);
2175 * Check for degenerate cases of nothing useful read.
2176 * If so go try again
2180 dp = (struct dirent *)cpos;
2184 * For some reason FreeBSD's ufs_readdir() chooses to back the
2185 * directory offset up to a block boundary, so it is necessary to
2186 * skip over the records that precede the requested offset. This
2187 * requires the assumption that file offset cookies monotonically
2190 while (cpos < cend && ncookies > 0 &&
2191 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2192 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2193 cpos += dp->d_reclen;
2194 dp = (struct dirent *)cpos;
2198 if (cpos >= cend || ncookies == 0) {
2206 * If cnt > MCLBYTES and the reply will not be saved, use
2207 * ext_pgs mbufs for TLS.
2208 * For NFSv4.0, we do not know for sure if the reply will
2209 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2211 if (cnt > MCLBYTES && siz > MCLBYTES &&
2212 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2213 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2214 nd->nd_flag |= ND_EXTPG;
2217 * dirlen is the size of the reply, including all XDR and must
2218 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2219 * if the XDR should be included in "count", but to be safe, we do.
2220 * (Include the two booleans at the end of the reply in dirlen now.)
2222 if (nd->nd_flag & ND_NFSV3) {
2223 nfsrv_postopattr(nd, getret, &at);
2224 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2225 txdr_hyper(at.na_filerev, tl);
2226 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2228 dirlen = 2 * NFSX_UNSIGNED;
2231 /* Loop through the records and build reply */
2232 while (cpos < cend && ncookies > 0) {
2233 nlen = dp->d_namlen;
2234 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2235 nlen <= NFS_MAXNAMLEN) {
2236 if (nd->nd_flag & ND_NFSV3)
2237 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2239 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2246 * Build the directory record xdr from
2249 if (nd->nd_flag & ND_NFSV3) {
2250 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2251 *tl++ = newnfs_true;
2252 txdr_hyper(dp->d_fileno, tl);
2254 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2255 *tl++ = newnfs_true;
2256 *tl = txdr_unsigned(dp->d_fileno);
2258 (void) nfsm_strtom(nd, dp->d_name, nlen);
2259 if (nd->nd_flag & ND_NFSV3) {
2260 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2261 txdr_hyper(*cookiep, tl);
2263 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2264 *tl = txdr_unsigned(*cookiep);
2267 cpos += dp->d_reclen;
2268 dp = (struct dirent *)cpos;
2274 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2275 *tl++ = newnfs_false;
2281 free(cookies, M_TEMP);
2284 NFSEXITCODE2(0, nd);
2288 NFSEXITCODE2(error, nd);
2293 * Readdirplus for V3 and Readdir for V4.
2296 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2297 struct vnode *vp, struct nfsexstuff *exp)
2302 char *cpos, *cend, *rbuf;
2305 struct nfsvattr nva, at, *nvap = &nva;
2306 struct mbuf *mb0, *mb1;
2307 struct nfsreferral *refp;
2308 int nlen, r, error = 0, getret = 1, usevget = 1;
2309 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2310 caddr_t bpos0, bpos1;
2311 u_int64_t off, toff, verf __unused;
2312 uint64_t *cookies = NULL, *cookiep;
2313 nfsattrbit_t attrbits, rderrbits, savbits;
2316 struct componentname cn;
2317 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2318 struct mount *mp, *new_mp;
2319 uint64_t mounted_on_fileno;
2320 struct thread *p = curthread;
2321 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2323 if (nd->nd_repstat) {
2324 nfsrv_postopattr(nd, getret, &at);
2327 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2328 off = fxdr_hyper(tl);
2331 verf = fxdr_hyper(tl);
2333 siz = fxdr_unsigned(int, *tl++);
2334 cnt = fxdr_unsigned(int, *tl);
2337 * Use the server's maximum data transfer size as the upper bound
2340 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2341 cnt = NFS_SRVMAXDATA(nd);
2344 * siz is a "hint" of how much directory information (name, fileid,
2345 * cookie) should be in the reply. At least one client "hints" 0,
2346 * so I set it to cnt for that case. I also round it up to the
2347 * next multiple of DIRBLKSIZ.
2348 * Since the size of a Readdirplus directory entry reply will always
2349 * be greater than a directory entry returned by VOP_READDIR(), it
2350 * does not make sense to read more than NFS_SRVMAXDATA() via
2355 else if (siz > NFS_SRVMAXDATA(nd))
2356 siz = NFS_SRVMAXDATA(nd);
2357 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2359 if (nd->nd_flag & ND_NFSV4) {
2360 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2363 NFSSET_ATTRBIT(&savbits, &attrbits);
2364 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2365 NFSZERO_ATTRBIT(&rderrbits);
2366 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2368 NFSZERO_ATTRBIT(&attrbits);
2371 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2373 if (!nd->nd_repstat) {
2374 if (off && verf != at.na_filerev) {
2376 * va_filerev is not sufficient as a cookie verifier,
2377 * since it is not supposed to change when entries are
2378 * removed/added unless that offset cookies returned to
2379 * the client are no longer valid.
2381 if (nd->nd_flag & ND_NFSV4) {
2382 nd->nd_repstat = NFSERR_NOTSAME;
2384 nd->nd_repstat = NFSERR_BAD_COOKIE;
2389 if (!nd->nd_repstat && vp->v_type != VDIR)
2390 nd->nd_repstat = NFSERR_NOTDIR;
2391 if (!nd->nd_repstat && cnt == 0)
2392 nd->nd_repstat = NFSERR_TOOSMALL;
2393 if (!nd->nd_repstat)
2394 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2395 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2396 NFSACCCHK_VPISLOCKED, NULL);
2397 if (nd->nd_repstat) {
2399 if (nd->nd_flag & ND_NFSV3)
2400 nfsrv_postopattr(nd, getret, &at);
2403 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2404 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2406 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2410 free(cookies, M_TEMP);
2418 io.uio_offset = (off_t)off;
2420 io.uio_segflg = UIO_SYSSPACE;
2421 io.uio_rw = UIO_READ;
2423 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2425 off = (u_int64_t)io.uio_offset;
2427 siz -= io.uio_resid;
2429 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2431 if (!cookies && !nd->nd_repstat)
2432 nd->nd_repstat = NFSERR_PERM;
2433 if (!nd->nd_repstat)
2434 nd->nd_repstat = getret;
2435 if (nd->nd_repstat) {
2438 free(cookies, M_TEMP);
2440 if (nd->nd_flag & ND_NFSV3)
2441 nfsrv_postopattr(nd, getret, &at);
2445 * If nothing read, return eof
2450 if (nd->nd_flag & ND_NFSV3)
2451 nfsrv_postopattr(nd, getret, &at);
2452 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2453 txdr_hyper(at.na_filerev, tl);
2455 *tl++ = newnfs_false;
2457 free(cookies, M_TEMP);
2463 * Check for degenerate cases of nothing useful read.
2464 * If so go try again
2468 dp = (struct dirent *)cpos;
2472 * For some reason FreeBSD's ufs_readdir() chooses to back the
2473 * directory offset up to a block boundary, so it is necessary to
2474 * skip over the records that precede the requested offset. This
2475 * requires the assumption that file offset cookies monotonically
2478 while (cpos < cend && ncookies > 0 &&
2479 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2480 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2481 ((nd->nd_flag & ND_NFSV4) &&
2482 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2483 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2484 cpos += dp->d_reclen;
2485 dp = (struct dirent *)cpos;
2489 if (cpos >= cend || ncookies == 0) {
2496 * Busy the file system so that the mount point won't go away
2497 * and, as such, VFS_VGET() can be used safely.
2502 nd->nd_repstat = vfs_busy(mp, 0);
2504 if (nd->nd_repstat != 0) {
2506 free(cookies, M_TEMP);
2508 if (nd->nd_flag & ND_NFSV3)
2509 nfsrv_postopattr(nd, getret, &at);
2514 * Check to see if entries in this directory can be safely acquired
2515 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2516 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2517 * automount of the snapshot directory that is required will
2519 * This needs to be done here for NFSv4, since NFSv4 never does
2520 * a VFS_VGET() for "." or "..".
2523 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2524 if (r == EOPNOTSUPP) {
2526 cn.cn_nameiop = LOOKUP;
2527 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2528 cn.cn_cred = nd->nd_cred;
2534 * If the reply is likely to exceed MCLBYTES and the reply will
2535 * not be saved, use ext_pgs mbufs for TLS.
2536 * It is difficult to predict how large each entry will be and
2537 * how many entries have been read, so just assume the directory
2538 * entries grow by a factor of 4 when attributes are included.
2539 * For NFSv4.0, we do not know for sure if the reply will
2540 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2542 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2543 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2544 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2545 nd->nd_flag |= ND_EXTPG;
2548 * Save this position, in case there is an error before one entry
2552 bpos0 = nd->nd_bpos;
2553 bextpg0 = nd->nd_bextpg;
2554 bextpgsiz0 = nd->nd_bextpgsiz;
2557 * Fill in the first part of the reply.
2558 * dirlen is the reply length in bytes and cannot exceed cnt.
2559 * (Include the two booleans at the end of the reply in dirlen now,
2560 * so we recognize when we have exceeded cnt.)
2562 if (nd->nd_flag & ND_NFSV3) {
2563 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2564 nfsrv_postopattr(nd, getret, &at);
2566 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2568 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2569 txdr_hyper(at.na_filerev, tl);
2572 * Save this position, in case there is an empty reply needed.
2575 bpos1 = nd->nd_bpos;
2576 bextpg1 = nd->nd_bextpg;
2577 bextpgsiz1 = nd->nd_bextpgsiz;
2579 /* Loop through the records and build reply */
2581 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2582 nlen = dp->d_namlen;
2583 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2584 nlen <= NFS_MAXNAMLEN &&
2585 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2586 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2587 || (nlen == 1 && dp->d_name[0] != '.'))) {
2589 * Save the current position in the reply, in case
2590 * this entry exceeds cnt.
2593 bpos1 = nd->nd_bpos;
2594 bextpg1 = nd->nd_bextpg;
2595 bextpgsiz1 = nd->nd_bextpgsiz;
2598 * For readdir_and_lookup get the vnode using
2607 mounted_on_fileno = (uint64_t)dp->d_fileno;
2608 if ((nd->nd_flag & ND_NFSV3) ||
2609 NFSNONZERO_ATTRBIT(&savbits)) {
2610 if (nd->nd_flag & ND_NFSV4)
2611 refp = nfsv4root_getreferral(NULL,
2615 r = VFS_VGET(mp, dp->d_fileno,
2619 if (r == EOPNOTSUPP) {
2622 cn.cn_nameiop = LOOKUP;
2629 cn.cn_nameptr = dp->d_name;
2630 cn.cn_namelen = nlen;
2631 cn.cn_flags = ISLASTCN |
2632 NOFOLLOW | LOCKLEAF;
2634 dp->d_name[0] == '.' &&
2635 dp->d_name[1] == '.')
2638 if (NFSVOPLOCK(vp, LK_SHARED)
2640 nd->nd_repstat = EPERM;
2643 if ((vp->v_vflag & VV_ROOT) != 0
2644 && (cn.cn_flags & ISDOTDOT)
2650 r = VOP_LOOKUP(vp, &nvp,
2658 * For NFSv4, check to see if nvp is
2659 * a mount point and get the mount
2660 * point vnode, as required.
2663 nfsrv_enable_crossmntpt != 0 &&
2664 (nd->nd_flag & ND_NFSV4) != 0 &&
2665 nvp->v_type == VDIR &&
2666 nvp->v_mountedhere != NULL) {
2667 new_mp = nvp->v_mountedhere;
2668 r = vfs_busy(new_mp, 0);
2672 r = VFS_ROOT(new_mp,
2682 * If we failed to look up the entry, then it
2683 * has become invalid, most likely removed.
2690 KASSERT(refp != NULL || nvp != NULL,
2691 ("%s: undetected lookup error", __func__));
2694 ((nd->nd_flag & ND_NFSV3) ||
2695 NFSNONZERO_ATTRBIT(&attrbits))) {
2696 r = nfsvno_getfh(nvp, &nfh, p);
2698 r = nfsvno_getattr(nvp, nvap, nd, p,
2700 if (r == 0 && is_zfs == 1 &&
2701 nfsrv_enable_crossmntpt != 0 &&
2702 (nd->nd_flag & ND_NFSV4) != 0 &&
2703 nvp->v_type == VDIR &&
2704 vp->v_mount != nvp->v_mount) {
2706 * For a ZFS snapshot, there is a
2707 * pseudo mount that does not set
2708 * v_mountedhere, so it needs to
2709 * be detected via a different
2714 new_mp = nvp->v_mount;
2719 * If we failed to get attributes of the entry,
2720 * then just skip it for NFSv3 (the traditional
2721 * behavior in the old NFS server).
2722 * For NFSv4 the behavior is controlled by
2723 * RDATTRERROR: we either ignore the error or
2725 * The exception is EOPNOTSUPP, which can be
2726 * returned by nfsvno_getfh() for certain
2727 * file systems, such as devfs. This indicates
2728 * that the file system cannot be exported,
2729 * so just skip over the entry.
2730 * Note that RDATTRERROR is never set for NFSv3.
2733 if (!NFSISSET_ATTRBIT(&attrbits,
2734 NFSATTRBIT_RDATTRERROR) ||
2737 if (needs_unbusy != 0)
2739 if ((nd->nd_flag & ND_NFSV3) ||
2749 * Build the directory record xdr
2751 if (nd->nd_flag & ND_NFSV3) {
2752 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2753 *tl++ = newnfs_true;
2754 txdr_hyper(dp->d_fileno, tl);
2755 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2756 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2757 txdr_hyper(*cookiep, tl);
2758 nfsrv_postopattr(nd, 0, nvap);
2759 dirlen += nfsm_fhtom(NULL, nd, (u_int8_t *)&nfh,
2761 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2765 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2766 *tl++ = newnfs_true;
2767 txdr_hyper(*cookiep, tl);
2768 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2770 supports_nfsv4acls =
2771 nfs_supportsnfsv4acls(nvp);
2774 supports_nfsv4acls = 0;
2776 dirlen += nfsrv_putreferralattr(nd,
2779 if (nd->nd_repstat) {
2782 if (needs_unbusy != 0)
2787 dirlen += nfsvno_fillattr(nd, new_mp,
2788 nvp, nvap, &nfh, r, &rderrbits,
2789 nd->nd_cred, p, isdgram, 0,
2790 supports_nfsv4acls, at_root,
2793 dirlen += nfsvno_fillattr(nd, new_mp,
2794 nvp, nvap, &nfh, r, &attrbits,
2795 nd->nd_cred, p, isdgram, 0,
2796 supports_nfsv4acls, at_root,
2801 dirlen += (3 * NFSX_UNSIGNED);
2803 if (needs_unbusy != 0)
2809 cpos += dp->d_reclen;
2810 dp = (struct dirent *)cpos;
2818 * If dirlen > cnt, we must strip off the last entry. If that
2819 * results in an empty reply, report NFSERR_TOOSMALL.
2821 if (dirlen > cnt || nd->nd_repstat) {
2822 if (!nd->nd_repstat && entrycnt == 0)
2823 nd->nd_repstat = NFSERR_TOOSMALL;
2824 if (nd->nd_repstat) {
2825 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2826 if (nd->nd_flag & ND_NFSV3)
2827 nfsrv_postopattr(nd, getret, &at);
2829 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2831 } else if (cpos < cend)
2833 if (!nd->nd_repstat) {
2834 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2835 *tl++ = newnfs_false;
2841 free(cookies, M_TEMP);
2845 NFSEXITCODE2(0, nd);
2849 NFSEXITCODE2(error, nd);
2854 * Get the settable attributes out of the mbuf list.
2855 * (Return 0 or EBADRPC)
2858 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2859 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2862 struct nfsv2_sattr *sp;
2863 int error = 0, toclient = 0;
2865 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2867 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2869 * Some old clients didn't fill in the high order 16bits.
2870 * --> check the low order 2 bytes for 0xffff
2872 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2873 nvap->na_mode = nfstov_mode(sp->sa_mode);
2874 if (sp->sa_uid != newnfs_xdrneg1)
2875 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2876 if (sp->sa_gid != newnfs_xdrneg1)
2877 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2878 if (sp->sa_size != newnfs_xdrneg1)
2879 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2880 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2882 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2884 nvap->na_atime.tv_sec =
2885 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2886 nvap->na_atime.tv_nsec = 0;
2889 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2890 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2893 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2894 if (*tl == newnfs_true) {
2895 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2896 nvap->na_mode = nfstov_mode(*tl);
2898 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2899 if (*tl == newnfs_true) {
2900 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2901 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2903 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2904 if (*tl == newnfs_true) {
2905 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2906 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2908 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2909 if (*tl == newnfs_true) {
2910 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2911 nvap->na_size = fxdr_hyper(tl);
2913 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2914 switch (fxdr_unsigned(int, *tl)) {
2915 case NFSV3SATTRTIME_TOCLIENT:
2916 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2917 fxdr_nfsv3time(tl, &nvap->na_atime);
2920 case NFSV3SATTRTIME_TOSERVER:
2921 vfs_timestamp(&nvap->na_atime);
2922 nvap->na_vaflags |= VA_UTIMES_NULL;
2925 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2926 switch (fxdr_unsigned(int, *tl)) {
2927 case NFSV3SATTRTIME_TOCLIENT:
2928 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2929 fxdr_nfsv3time(tl, &nvap->na_mtime);
2930 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2932 case NFSV3SATTRTIME_TOSERVER:
2933 vfs_timestamp(&nvap->na_mtime);
2935 nvap->na_vaflags |= VA_UTIMES_NULL;
2940 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2943 NFSEXITCODE2(error, nd);
2948 * Handle the setable attributes for V4.
2949 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2952 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2953 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2958 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2959 int moderet, toclient = 0;
2960 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2963 u_short mode, mask; /* Same type as va_mode. */
2966 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2969 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2970 attrsize = fxdr_unsigned(int, *tl);
2973 * Loop around getting the setable attributes. If an unsupported
2974 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2977 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2978 bitpos = NFSATTRBIT_MAX;
2983 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2984 if (attrsum > attrsize) {
2985 error = NFSERR_BADXDR;
2988 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2990 case NFSATTRBIT_SIZE:
2991 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2992 if (vp != NULL && vp->v_type != VREG) {
2993 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2997 nvap->na_size = fxdr_hyper(tl);
2998 attrsum += NFSX_HYPER;
3000 case NFSATTRBIT_ACL:
3001 error = nfsrv_dissectacl(nd, aclp, true, &aceerr,
3005 if (aceerr && !nd->nd_repstat)
3006 nd->nd_repstat = aceerr;
3009 case NFSATTRBIT_ARCHIVE:
3010 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3011 if (!nd->nd_repstat)
3012 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3013 attrsum += NFSX_UNSIGNED;
3015 case NFSATTRBIT_HIDDEN:
3016 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3017 if (!nd->nd_repstat)
3018 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3019 attrsum += NFSX_UNSIGNED;
3021 case NFSATTRBIT_MIMETYPE:
3022 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3023 i = fxdr_unsigned(int, *tl);
3024 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3027 if (!nd->nd_repstat)
3028 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3029 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3031 case NFSATTRBIT_MODE:
3032 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
3033 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3034 nvap->na_mode = nfstov_mode(*tl);
3035 attrsum += NFSX_UNSIGNED;
3037 case NFSATTRBIT_OWNER:
3038 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3039 j = fxdr_unsigned(int, *tl);
3041 error = NFSERR_BADXDR;
3044 if (j > NFSV4_SMALLSTR)
3045 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3048 error = nfsrv_mtostr(nd, cp, j);
3050 if (j > NFSV4_SMALLSTR)
3051 free(cp, M_NFSSTRING);
3054 if (!nd->nd_repstat) {
3055 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3057 if (!nd->nd_repstat)
3060 if (j > NFSV4_SMALLSTR)
3061 free(cp, M_NFSSTRING);
3062 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3064 case NFSATTRBIT_OWNERGROUP:
3065 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3066 j = fxdr_unsigned(int, *tl);
3068 error = NFSERR_BADXDR;
3071 if (j > NFSV4_SMALLSTR)
3072 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3075 error = nfsrv_mtostr(nd, cp, j);
3077 if (j > NFSV4_SMALLSTR)
3078 free(cp, M_NFSSTRING);
3081 if (!nd->nd_repstat) {
3082 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3084 if (!nd->nd_repstat)
3087 if (j > NFSV4_SMALLSTR)
3088 free(cp, M_NFSSTRING);
3089 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3091 case NFSATTRBIT_SYSTEM:
3092 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3093 if (!nd->nd_repstat)
3094 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3095 attrsum += NFSX_UNSIGNED;
3097 case NFSATTRBIT_TIMEACCESSSET:
3098 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3099 attrsum += NFSX_UNSIGNED;
3100 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3101 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3102 fxdr_nfsv4time(tl, &nvap->na_atime);
3104 attrsum += NFSX_V4TIME;
3106 vfs_timestamp(&nvap->na_atime);
3107 nvap->na_vaflags |= VA_UTIMES_NULL;
3110 case NFSATTRBIT_TIMEBACKUP:
3111 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3112 if (!nd->nd_repstat)
3113 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3114 attrsum += NFSX_V4TIME;
3116 case NFSATTRBIT_TIMECREATE:
3117 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3118 fxdr_nfsv4time(tl, &nvap->na_btime);
3119 attrsum += NFSX_V4TIME;
3121 case NFSATTRBIT_TIMEMODIFYSET:
3122 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3123 attrsum += NFSX_UNSIGNED;
3124 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3125 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3126 fxdr_nfsv4time(tl, &nvap->na_mtime);
3127 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3128 attrsum += NFSX_V4TIME;
3130 vfs_timestamp(&nvap->na_mtime);
3132 nvap->na_vaflags |= VA_UTIMES_NULL;
3135 case NFSATTRBIT_MODESETMASKED:
3136 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3137 mode = fxdr_unsigned(u_short, *tl++);
3138 mask = fxdr_unsigned(u_short, *tl);
3140 * vp == NULL implies an Open/Create operation.
3141 * This attribute can only be used for Setattr and
3142 * only for NFSv4.1 or higher.
3143 * If moderet != 0, a mode attribute has also been
3144 * specified and this attribute cannot be done in the
3145 * same Setattr operation.
3147 if ((nd->nd_flag & ND_NFSV41) == 0)
3148 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3149 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3151 nd->nd_repstat = NFSERR_INVAL;
3152 else if (moderet == 0)
3153 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3155 nvap->na_mode = (mode & mask) |
3156 (va.va_mode & ~mask);
3158 nd->nd_repstat = moderet;
3159 attrsum += 2 * NFSX_UNSIGNED;
3162 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3164 * set bitpos so we drop out of the loop.
3166 bitpos = NFSATTRBIT_MAX;
3172 * some clients pad the attrlist, so we need to skip over the
3175 if (attrsum > attrsize) {
3176 error = NFSERR_BADXDR;
3178 attrsize = NFSM_RNDUP(attrsize);
3179 if (attrsum < attrsize)
3180 error = nfsm_advance(nd, attrsize - attrsum, -1);
3183 NFSEXITCODE2(error, nd);
3188 * Check/setup export credentials.
3191 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3192 struct ucred *credanon, bool testsec)
3197 * Check/setup credentials.
3199 if (nd->nd_flag & ND_GSS)
3200 exp->nes_exflag &= ~MNT_EXPORTANON;
3203 * Check to see if the operation is allowed for this security flavor.
3207 error = nfsvno_testexp(nd, exp);
3213 * Check to see if the file system is exported V4 only.
3215 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3216 error = NFSERR_PROGNOTV4;
3221 * Now, map the user credentials.
3222 * (Note that ND_AUTHNONE will only be set for an NFSv3
3223 * Fsinfo RPC. If set for anything else, this code might need
3226 if (NFSVNO_EXPORTED(exp)) {
3227 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3228 NFSVNO_EXPORTANON(exp) ||
3229 (nd->nd_flag & ND_AUTHNONE) != 0) {
3230 nd->nd_cred->cr_uid = credanon->cr_uid;
3231 nd->nd_cred->cr_gid = credanon->cr_gid;
3232 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3233 credanon->cr_groups);
3234 } else if ((nd->nd_flag & ND_GSS) == 0) {
3236 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3237 * if there is a replacement credential with a group
3238 * list set up by "nfsuserd -manage-gids".
3239 * If there is no replacement, nfsrv_getgrpscred()
3240 * simply returns its argument.
3242 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3247 NFSEXITCODE2(error, nd);
3255 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3256 struct ucred **credp)
3263 if (mp->mnt_exjail == NULL ||
3264 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3268 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3269 &exp->nes_numsecflavor, exp->nes_secflavors);
3271 if (NFSD_VNET(nfs_rootfhset)) {
3272 exp->nes_exflag = 0;
3273 exp->nes_numsecflavor = 0;
3276 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3278 printf("nfsvno_checkexp: numsecflavors out of range\n");
3279 exp->nes_numsecflavor = 0;
3287 * Get a vnode for a file handle and export stuff.
3290 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3291 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3292 struct ucred **credp)
3297 exp->nes_numsecflavor = 0;
3298 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3300 /* Make sure the server replies ESTALE to the client. */
3302 if (nam && !error) {
3304 if (mp->mnt_exjail == NULL ||
3305 mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3309 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3310 &exp->nes_numsecflavor, exp->nes_secflavors);
3312 if (NFSD_VNET(nfs_rootfhset)) {
3313 exp->nes_exflag = 0;
3314 exp->nes_numsecflavor = 0;
3319 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3321 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3322 exp->nes_numsecflavor = 0;
3332 * nfsd_fhtovp() - convert a fh to a vnode ptr
3333 * - look up fsid in mount list (if not found ret error)
3334 * - get vp and export rights by calling nfsvno_fhtovp()
3335 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3337 * - if mpp != NULL, return the mount point so that it can
3338 * be used for vn_finished_write() by the caller
3341 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3342 struct vnode **vpp, struct nfsexstuff *exp,
3343 struct mount **mpp, int startwrite, int nextop)
3345 struct mount *mp, *mpw;
3346 struct ucred *credanon;
3353 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3354 mp = vfs_busyfs(&fhp->fh_fsid);
3356 nd->nd_repstat = ESTALE;
3362 error = vn_start_write(NULL, &mpw, V_WAIT);
3366 nd->nd_repstat = ESTALE;
3369 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3370 lktype = LK_EXCLUSIVE;
3374 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3379 * For NFSv4 without a pseudo root fs, unexported file handles
3380 * can be returned, so that Lookup works everywhere.
3382 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3383 !(nd->nd_flag & ND_NFSV4)) {
3386 nd->nd_repstat = EACCES;
3390 * Personally, I've never seen any point in requiring a
3391 * reserved port#, since only in the rare case where the
3392 * clients are all boxes with secure system privileges,
3393 * does it provide any enhanced security, but... some people
3394 * believe it to be useful and keep putting this code back in.
3395 * (There is also some "security checker" out there that
3396 * complains if the nfs server doesn't enforce this.)
3397 * However, note the following:
3398 * RFC3530 (NFSv4) specifies that a reserved port# not be
3400 * RFC2623 recommends that, if a reserved port# is checked for,
3401 * that there be a way to turn that off--> ifdef'd.
3403 #ifdef NFS_REQRSVPORT
3404 if (!nd->nd_repstat) {
3405 struct sockaddr_in *saddr;
3406 struct sockaddr_in6 *saddr6;
3408 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3409 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3410 if (!(nd->nd_flag & ND_NFSV4) &&
3411 ((saddr->sin_family == AF_INET &&
3412 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3413 (saddr6->sin6_family == AF_INET6 &&
3414 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3416 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3419 #endif /* NFS_REQRSVPORT */
3422 * Check/setup credentials.
3424 if (!nd->nd_repstat) {
3425 nd->nd_saveduid = nd->nd_cred->cr_uid;
3426 nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3427 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3431 if (credanon != NULL)
3433 if (nd->nd_repstat) {
3434 vn_finished_write(mpw);
3436 } else if (mpp != NULL) {
3441 NFSEXITCODE2(0, nd);
3448 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3450 struct filedesc *fdp;
3454 fdp = p->td_proc->p_fd;
3455 if (fd < 0 || fd >= fdp->fd_nfiles ||
3456 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3468 * Called from nfssvc() to update the exports list. Just call
3469 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3470 * in the mount list.
3473 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3475 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3477 struct nameidata nd;
3480 error = vfs_export(NFSD_VNET(nfsv4root_mnt), &nfsexargp->export, false);
3481 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3482 NFSD_VNET(nfs_rootfhset) = 0;
3483 else if (error == 0) {
3484 if (nfsexargp->fspec == NULL) {
3489 * If fspec != NULL, this is the v4root path.
3491 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec);
3492 if ((error = namei(&nd)) != 0)
3494 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3497 NFSD_VNET(nfs_rootfh).nfsrvfh_len = NFSX_MYFH;
3498 NFSBCOPY((caddr_t)&fh,
3499 NFSD_VNET(nfs_rootfh).nfsrvfh_data,
3500 sizeof (fhandle_t));
3501 NFSD_VNET(nfs_rootfhset) = 1;
3511 * This function needs to test to see if the system is near its limit
3512 * for memory allocation via malloc() or mget() and return True iff
3513 * either of these resources are near their limit.
3514 * XXX (For now, this is just a stub.)
3516 int nfsrv_testmalloclimit = 0;
3518 nfsrv_mallocmget_limit(void)
3520 static int printmesg = 0;
3521 static int testval = 1;
3523 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3524 if ((printmesg++ % 100) == 0)
3525 printf("nfsd: malloc/mget near limit\n");
3532 * BSD specific initialization of a mount point.
3539 if (NFSD_VNET(nfsrv_mntinited)) {
3543 NFSD_VNET(nfsrv_mntinited) = true;
3547 NFSD_VNET(nfsv4root_mnt) = malloc(sizeof(struct mount), M_TEMP,
3549 NFSD_VNET(nfsv4root_mnt)->mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3550 mtx_init(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3551 lockinit(&NFSD_VNET(nfsv4root_mnt)->mnt_explock, PVFS, "explock", 0, 0);
3552 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelist);
3553 TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelist);
3554 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
3555 TAILQ_INIT(&NFSD_VNET(nfsv4root_opt));
3556 TAILQ_INIT(&NFSD_VNET(nfsv4root_newopt));
3557 NFSD_VNET(nfsv4root_mnt)->mnt_opt = &NFSD_VNET(nfsv4root_opt);
3558 NFSD_VNET(nfsv4root_mnt)->mnt_optnew = &NFSD_VNET(nfsv4root_newopt);
3559 NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelistsize = 0;
3560 NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelistsize = 0;
3561 callout_init(&NFSD_VNET(nfsd_callout), 1);
3568 nfsd_timer(void *arg)
3572 vnetp = (struct vnet *)arg;
3573 NFSD_CURVNET_SET_QUIET(vnetp);
3574 nfsrv_servertimer(vnetp);
3575 callout_reset_sbt(&NFSD_VNET(nfsd_callout), SBT_1S, SBT_1S, nfsd_timer,
3577 NFSD_CURVNET_RESTORE();
3581 * Get a vnode for a file handle, without checking exports, etc.
3584 nfsvno_getvp(fhandle_t *fhp)
3590 mp = vfs_busyfs(&fhp->fh_fsid);
3593 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3601 * Do a local VOP_ADVLOCK().
3604 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3605 u_int64_t end, struct thread *td)
3611 if (nfsrv_dolocallocks == 0)
3613 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3615 fl.l_whence = SEEK_SET;
3617 fl.l_start = (off_t)first;
3618 if (end == NFS64BITSSET) {
3622 fl.l_len = (off_t)tlen;
3625 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3626 * values for all calls, so that all locks will be held by the
3627 * nfsd server. (The nfsd server handles conflicts between the
3629 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3630 * bytes, so it can't be put in l_sysid.
3632 if (nfsv4_sysid == 0)
3633 nfsv4_sysid = nlm_acquire_next_sysid();
3634 fl.l_pid = (pid_t)0;
3635 fl.l_sysid = (int)nfsv4_sysid;
3637 if (ftype == F_UNLCK)
3638 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3639 (F_POSIX | F_REMOTE));
3641 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3642 (F_POSIX | F_REMOTE));
3650 * Check the nfsv4 root exports.
3653 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3655 struct ucred *credanon;
3656 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3659 error = vfs_stdcheckexp(NFSD_VNET(nfsv4root_mnt), nd->nd_nam, &exflags,
3660 &credanon, &numsecflavor, secflavors);
3662 error = NFSERR_PROGUNAVAIL;
3665 if (credanon != NULL)
3667 for (i = 0; i < numsecflavor; i++) {
3668 if (secflavors[i] == AUTH_SYS)
3669 nd->nd_flag |= ND_EXAUTHSYS;
3670 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3671 nd->nd_flag |= ND_EXGSS;
3672 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3673 nd->nd_flag |= ND_EXGSSINTEGRITY;
3674 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3675 nd->nd_flag |= ND_EXGSSPRIVACY;
3678 /* And set ND_EXxx flags for TLS. */
3679 if ((exflags & MNT_EXTLS) != 0) {
3680 nd->nd_flag |= ND_EXTLS;
3681 if ((exflags & MNT_EXTLSCERT) != 0)
3682 nd->nd_flag |= ND_EXTLSCERT;
3683 if ((exflags & MNT_EXTLSCERTUSER) != 0)
3684 nd->nd_flag |= ND_EXTLSCERTUSER;
3693 * Nfs server pseudo system call for the nfsd's
3699 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3702 struct nfsd_addsock_args sockarg;
3703 struct nfsd_nfsd_args nfsdarg;
3704 struct nfsd_nfsd_oargs onfsdarg;
3705 struct nfsd_pnfsd_args pnfsdarg;
3706 struct vnode *vp, *nvp, *curdvp;
3707 struct pnfsdsfile *pf;
3708 struct nfsdevice *ds, *fds;
3709 cap_rights_t rights;
3710 int buflen, error, ret;
3711 char *buf, *cp, *cp2, *cp3;
3712 char fname[PNFS_FILENAME_LEN + 1];
3714 NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
3715 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3716 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3720 * Since we don't know what rights might be required,
3721 * pretend that we need them all. It is better to be too
3722 * careful than too reckless.
3724 error = fget(td, sockarg.sock,
3725 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
3728 if (fp->f_type != DTYPE_SOCKET) {
3733 error = nfsrvd_addsock(fp);
3735 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3736 if (uap->argp == NULL) {
3740 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3741 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3743 nfsdarg.principal = onfsdarg.principal;
3744 nfsdarg.minthreads = onfsdarg.minthreads;
3745 nfsdarg.maxthreads = onfsdarg.maxthreads;
3746 nfsdarg.version = 1;
3747 nfsdarg.addr = NULL;
3748 nfsdarg.addrlen = 0;
3749 nfsdarg.dnshost = NULL;
3750 nfsdarg.dnshostlen = 0;
3751 nfsdarg.dspath = NULL;
3752 nfsdarg.dspathlen = 0;
3753 nfsdarg.mdspath = NULL;
3754 nfsdarg.mdspathlen = 0;
3755 nfsdarg.mirrorcnt = 1;
3758 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3761 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3762 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3763 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3764 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3765 nfsdarg.mirrorcnt >= 1 &&
3766 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3767 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3768 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3769 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3770 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3771 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3772 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3773 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3774 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3779 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3781 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3782 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3784 free(nfsdarg.addr, M_TEMP);
3788 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3789 nfsdarg.dnshost = cp;
3790 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3791 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3793 free(nfsdarg.addr, M_TEMP);
3794 free(nfsdarg.dnshost, M_TEMP);
3798 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3799 nfsdarg.dspath = cp;
3800 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3801 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3803 free(nfsdarg.addr, M_TEMP);
3804 free(nfsdarg.dnshost, M_TEMP);
3805 free(nfsdarg.dspath, M_TEMP);
3809 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3810 nfsdarg.mdspath = cp;
3812 nfsdarg.addr = NULL;
3813 nfsdarg.addrlen = 0;
3814 nfsdarg.dnshost = NULL;
3815 nfsdarg.dnshostlen = 0;
3816 nfsdarg.dspath = NULL;
3817 nfsdarg.dspathlen = 0;
3818 nfsdarg.mdspath = NULL;
3819 nfsdarg.mdspathlen = 0;
3820 nfsdarg.mirrorcnt = 1;
3822 nfsd_timer(NFSD_TD_TO_VNET(td));
3823 error = nfsrvd_nfsd(td, &nfsdarg);
3824 callout_drain(&NFSD_VNET(nfsd_callout));
3825 free(nfsdarg.addr, M_TEMP);
3826 free(nfsdarg.dnshost, M_TEMP);
3827 free(nfsdarg.dspath, M_TEMP);
3828 free(nfsdarg.mdspath, M_TEMP);
3829 } else if (uap->flag & NFSSVC_PNFSDS) {
3830 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3831 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3832 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3833 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3834 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3837 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3839 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3840 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3841 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3842 buf = malloc(buflen, M_TEMP, M_WAITOK);
3843 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3845 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3846 if (error == 0 && pnfsdarg.dspath != NULL) {
3847 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3848 error = copyinstr(pnfsdarg.dspath, cp2,
3849 PATH_MAX + 1, NULL);
3850 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3854 if (error == 0 && pnfsdarg.curdspath != NULL) {
3855 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3856 error = copyinstr(pnfsdarg.curdspath, cp3,
3857 PATH_MAX + 1, NULL);
3858 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3865 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3866 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3868 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3870 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3871 printf("copymr: dsdir out of range\n");
3874 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3875 error = nfsrv_copymr(vp, nvp,
3876 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3877 (struct pnfsdsfile *)buf,
3878 buflen / sizeof(*pf), td->td_ucred, td);
3881 if (fds != NULL && error == 0) {
3882 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3883 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3885 nfsrv_dsremove(curdvp, fname,
3887 NFSVOPUNLOCK(curdvp);
3890 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3898 error = nfssvc_srvcall(td, uap, td->td_ucred);
3902 NFSD_CURVNET_RESTORE();
3908 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3910 struct nfsex_args export;
3911 struct nfsex_oldargs oexp;
3912 struct file *fp = NULL;
3913 int stablefd, i, len;
3914 struct nfsd_clid adminrevoke;
3915 struct nfsd_dumplist dumplist;
3916 struct nfsd_dumpclients *dumpclients;
3917 struct nfsd_dumplocklist dumplocklist;
3918 struct nfsd_dumplocks *dumplocks;
3919 struct nameidata nd;
3921 int error = EINVAL, igotlock;
3925 if (uap->flag & NFSSVC_PUBLICFH) {
3926 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3927 sizeof (fhandle_t));
3928 error = copyin(uap->argp,
3929 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3932 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3933 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3934 error = copyin(uap->argp,(caddr_t)&export,
3935 sizeof (struct nfsex_args));
3938 if (export.export.ex_ngroups > NGROUPS_MAX ||
3939 export.export.ex_ngroups < 0)
3941 else if (export.export.ex_ngroups > 0) {
3942 grps = malloc(export.export.ex_ngroups *
3943 sizeof(gid_t), M_TEMP, M_WAITOK);
3944 error = copyin(export.export.ex_groups, grps,
3945 export.export.ex_ngroups * sizeof(gid_t));
3946 export.export.ex_groups = grps;
3948 export.export.ex_groups = NULL;
3950 error = nfsrv_v4rootexport(&export, cred, p);
3953 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3954 NFSSVC_V4ROOTEXPORT) {
3955 error = copyin(uap->argp,(caddr_t)&oexp,
3956 sizeof (struct nfsex_oldargs));
3958 memset(&export.export, 0, sizeof(export.export));
3959 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3960 export.export.ex_root = oexp.export.ex_root;
3961 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3962 export.export.ex_ngroups =
3963 oexp.export.ex_anon.cr_ngroups;
3964 export.export.ex_groups = NULL;
3965 if (export.export.ex_ngroups > XU_NGROUPS ||
3966 export.export.ex_ngroups < 0)
3968 else if (export.export.ex_ngroups > 0) {
3969 export.export.ex_groups = malloc(
3970 export.export.ex_ngroups * sizeof(gid_t),
3972 for (i = 0; i < export.export.ex_ngroups; i++)
3973 export.export.ex_groups[i] =
3974 oexp.export.ex_anon.cr_groups[i];
3976 export.export.ex_addr = oexp.export.ex_addr;
3977 export.export.ex_addrlen = oexp.export.ex_addrlen;
3978 export.export.ex_mask = oexp.export.ex_mask;
3979 export.export.ex_masklen = oexp.export.ex_masklen;
3980 export.export.ex_indexfile = oexp.export.ex_indexfile;
3981 export.export.ex_numsecflavors =
3982 oexp.export.ex_numsecflavors;
3983 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3984 export.export.ex_numsecflavors < 0)
3987 for (i = 0; i < export.export.ex_numsecflavors;
3989 export.export.ex_secflavors[i] =
3990 oexp.export.ex_secflavors[i];
3992 export.fspec = oexp.fspec;
3994 error = nfsrv_v4rootexport(&export, cred, p);
3995 free(export.export.ex_groups, M_TEMP);
3997 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
4000 } else if (uap->flag & NFSSVC_STABLERESTART) {
4001 error = copyin(uap->argp, (caddr_t)&stablefd,
4004 error = fp_getfvp(p, stablefd, &fp, &vp);
4005 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
4007 if (!error && NFSD_VNET(nfsrv_numnfsd) != 0)
4010 NFSD_VNET(nfsrv_stablefirst).nsf_fp = fp;
4011 nfsrv_setupstable(p);
4013 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
4014 error = copyin(uap->argp, (caddr_t)&adminrevoke,
4015 sizeof (struct nfsd_clid));
4017 error = nfsrv_adminrevoke(&adminrevoke, p);
4018 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
4019 error = copyin(uap->argp, (caddr_t)&dumplist,
4020 sizeof (struct nfsd_dumplist));
4021 if (!error && (dumplist.ndl_size < 1 ||
4022 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
4025 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
4026 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4027 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
4028 error = copyout(dumpclients, dumplist.ndl_list, len);
4029 free(dumpclients, M_TEMP);
4031 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
4032 error = copyin(uap->argp, (caddr_t)&dumplocklist,
4033 sizeof (struct nfsd_dumplocklist));
4034 if (!error && (dumplocklist.ndllck_size < 1 ||
4035 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
4038 error = nfsrv_lookupfilename(&nd,
4039 dumplocklist.ndllck_fname, p);
4041 len = sizeof (struct nfsd_dumplocks) *
4042 dumplocklist.ndllck_size;
4043 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4044 nfsrv_dumplocks(nd.ni_vp, dumplocks,
4045 dumplocklist.ndllck_size, p);
4047 error = copyout(dumplocks, dumplocklist.ndllck_list,
4049 free(dumplocks, M_TEMP);
4051 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
4054 nfsd_master_pid = procp->p_pid;
4055 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
4056 nfsd_master_start = procp->p_stats->p_start;
4057 NFSD_VNET(nfsd_master_proc) = procp;
4059 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4060 NFSLOCKV4ROOTMUTEX();
4061 if (!NFSD_VNET(nfsrv_suspend_nfsd)) {
4062 /* Lock out all nfsd threads */
4064 igotlock = nfsv4_lock(
4065 &NFSD_VNET(nfsd_suspend_lock), 1, NULL,
4066 NFSV4ROOTLOCKMUTEXPTR, NULL);
4067 } while (igotlock == 0 &&
4068 !NFSD_VNET(nfsrv_suspend_nfsd));
4069 NFSD_VNET(nfsrv_suspend_nfsd) = true;
4071 NFSUNLOCKV4ROOTMUTEX();
4073 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4074 NFSLOCKV4ROOTMUTEX();
4075 if (NFSD_VNET(nfsrv_suspend_nfsd)) {
4076 nfsv4_unlock(&NFSD_VNET(nfsd_suspend_lock), 0);
4077 NFSD_VNET(nfsrv_suspend_nfsd) = false;
4079 NFSUNLOCKV4ROOTMUTEX();
4089 * Returns 0 if ok, 1 otherwise.
4092 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4096 if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4097 (NFSVNO_EXTLSCERT(exp) &&
4098 (nd->nd_flag & ND_TLSCERT) == 0) ||
4099 (NFSVNO_EXTLSCERTUSER(exp) &&
4100 (nd->nd_flag & ND_TLSCERTUSER) == 0)) {
4101 if ((nd->nd_flag & ND_NFSV4) != 0)
4102 return (NFSERR_WRONGSEC);
4104 /* There is currently no auth_stat for this. */
4105 else if ((nd->nd_flag & ND_TLS) == 0)
4106 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4108 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4111 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4115 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use
4116 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
4118 if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO)
4122 * This seems odd, but allow the case where the security flavor
4123 * list is empty. This happens when NFSv4 is traversing non-exported
4124 * file systems. Exported file systems should always have a non-empty
4125 * security flavor list.
4127 if (exp->nes_numsecflavor == 0)
4130 for (i = 0; i < exp->nes_numsecflavor; i++) {
4132 * The tests for privacy and integrity must be first,
4133 * since ND_GSS is set for everything but AUTH_SYS.
4135 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4136 (nd->nd_flag & ND_GSSPRIVACY))
4138 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4139 (nd->nd_flag & ND_GSSINTEGRITY))
4141 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4142 (nd->nd_flag & ND_GSS))
4144 if (exp->nes_secflavors[i] == AUTH_SYS &&
4145 (nd->nd_flag & ND_GSS) == 0)
4148 if ((nd->nd_flag & ND_NFSV4) != 0)
4149 return (NFSERR_WRONGSEC);
4150 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4154 * Calculate a hash value for the fid in a file handle.
4157 nfsrv_hashfh(fhandle_t *fhp)
4161 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4166 * Calculate a hash value for the sessionid.
4169 nfsrv_hashsessionid(uint8_t *sessionid)
4173 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4178 * Signal the userland master nfsd to backup the stable restart file.
4181 nfsrv_backupstable(void)
4185 if (NFSD_VNET(nfsd_master_proc) != NULL) {
4186 procp = pfind(nfsd_master_pid);
4187 /* Try to make sure it is the correct process. */
4188 if (procp == NFSD_VNET(nfsd_master_proc) &&
4189 procp->p_stats->p_start.tv_sec ==
4190 nfsd_master_start.tv_sec &&
4191 procp->p_stats->p_start.tv_usec ==
4192 nfsd_master_start.tv_usec &&
4193 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4194 kern_psignal(procp, SIGUSR2);
4196 NFSD_VNET(nfsd_master_proc) = NULL;
4204 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4205 * The arguments are in a structure, so that they can be passed through
4206 * taskqueue for a kernel process to execute this function.
4208 struct nfsrvdscreate {
4212 struct ucred *tcred;
4215 struct pnfsdsfile *pf;
4219 struct vattr createva;
4223 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4224 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4225 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4228 struct nameidata named;
4233 struct nfsmount *nmp;
4236 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4237 LOCKPARENT | LOCKLEAF | NOCACHE);
4238 nfsvno_setpathbuf(&named, &bufp, &hashp);
4239 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4240 named.ni_cnd.cn_nameptr = bufp;
4241 if (fnamep != NULL) {
4242 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4243 named.ni_cnd.cn_namelen = strlen(bufp);
4245 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4246 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4248 /* Create the date file in the DS mount. */
4249 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4251 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4253 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4255 /* Set the ownership of the file. */
4256 error = VOP_SETATTR(nvp, nvap, tcred);
4257 NFSD_DEBUG(4, "nfsrv_dscreate:"
4258 " setattr-uid=%d\n", error);
4263 printf("pNFS: pnfscreate failed=%d\n", error);
4265 printf("pNFS: pnfscreate vnlock=%d\n", error);
4268 nmp = VFSTONFS(nvp->v_mount);
4269 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4270 != 0 || nmp->nm_nam->sa_len > sizeof(
4271 struct sockaddr_in6) ||
4272 np->n_fhp->nfh_len != NFSX_MYFH) {
4273 printf("Bad DS file: fstype=%s salen=%d"
4275 nvp->v_mount->mnt_vfc->vfc_name,
4276 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4280 /* Set extattrs for the DS on the MDS file. */
4283 error = VOP_GETATTR(nvp, &va, tcred);
4285 dsa->dsa_filerev = va.va_filerev;
4286 dsa->dsa_size = va.va_size;
4287 dsa->dsa_atime = va.va_atime;
4288 dsa->dsa_mtime = va.va_mtime;
4289 dsa->dsa_bytes = va.va_bytes;
4293 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4295 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4296 nmp->nm_nam->sa_len);
4297 NFSBCOPY(named.ni_cnd.cn_nameptr,
4299 sizeof(pf->dsf_filename));
4302 printf("pNFS: pnfscreate can't get DS"
4303 " attr=%d\n", error);
4304 if (nvpp != NULL && error == 0)
4309 nfsvno_relpathbuf(&named);
4314 * Start up the thread that will execute nfsrv_dscreate().
4317 start_dscreate(void *arg, int pending)
4319 struct nfsrvdscreate *dsc;
4321 dsc = (struct nfsrvdscreate *)arg;
4322 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4323 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4325 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4329 * Create a pNFS data file on the Data Server(s).
4332 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4335 struct nfsrvdscreate *dsc, *tdsc = NULL;
4336 struct nfsdevice *ds, *tds, *fds;
4338 struct pnfsdsfile *pf, *tpf;
4339 struct pnfsdsattr dsattr;
4341 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4342 struct nfsmount *nmp;
4347 struct ucred *tcred;
4348 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4351 /* Get a DS server directory in a round-robin order. */
4357 * Search for the first entry that handles this MDS fs, but use the
4358 * first entry for all MDS fs's otherwise.
4360 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4361 if (tds->nfsdev_nmp != NULL) {
4362 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4364 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4365 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4373 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4376 i = dsdir[0] = ds->nfsdev_nextdir;
4377 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4378 dvp[0] = ds->nfsdev_dsdir[i];
4379 tds = TAILQ_NEXT(ds, nfsdev_list);
4380 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4381 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4382 if (tds->nfsdev_nmp != NULL &&
4383 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4384 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4385 fsidcmp(&mp->mnt_stat.f_fsid,
4386 &tds->nfsdev_mdsfsid) == 0))) {
4387 dsdir[mirrorcnt] = i;
4388 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4390 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4395 /* Put at end of list to implement round-robin usage. */
4396 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4397 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4401 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4403 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4406 error = nfsvno_getfh(vp, &fh, p);
4408 error = VOP_GETATTR(vp, &va, cred);
4410 /* Set the attributes for "vp" to Setattr the DS vp. */
4413 vamode = va.va_mode;
4417 va.va_mode = vamode;
4420 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4422 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4424 /* Make data file name based on FH. */
4425 tcred = newnfs_getcred();
4428 * Create the file on each DS mirror, using kernel process(es) for the
4429 * additional mirrors.
4432 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4433 tpf->dsf_dir = dsdir[i];
4434 tdsc->tcred = tcred;
4437 tdsc->createva = *vap;
4438 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4445 if (nfs_pnfsiothreads != 0) {
4446 ret = nfs_pnfsio(start_dscreate, tdsc);
4447 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4450 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4451 NULL, tcred, p, NULL);
4453 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4455 if (failpos == -1 && nfsds_failerr(ret))
4463 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4464 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4465 &dsattr, NULL, tcred, p, NULL);
4466 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4467 failpos = mirrorcnt - 1;
4471 timo = hz / 50; /* Wait for 20msec. */
4474 /* Wait for kernel task(s) to complete. */
4475 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4476 while (tdsc->inprog != 0 && tdsc->done == 0)
4477 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4478 if (tdsc->err != 0) {
4479 if (failpos == -1 && nfsds_failerr(tdsc->err))
4481 else if (error == 0)
4487 * If failpos has been set, that mirror has failed, so it needs
4491 nmp = VFSTONFS(dvp[failpos]->v_mount);
4493 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4494 NFSMNTP_CANCELRPCS)) == 0) {
4495 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4497 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4498 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4501 nfsrv_killrpcs(nmp);
4503 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4511 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4513 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4514 mirrorcnt, nfsrv_maxpnfsmirror);
4516 * For all mirrors that couldn't be created, fill in the
4517 * *pf structure, but with an IP address == 0.0.0.0.
4519 tpf = pf + mirrorcnt;
4520 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4522 tpf->dsf_sin.sin_family = AF_INET;
4523 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4524 tpf->dsf_sin.sin_addr.s_addr = 0;
4525 tpf->dsf_sin.sin_port = 0;
4528 error = vn_extattr_set(vp, IO_NODELOCKED,
4529 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4530 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4532 error = vn_extattr_set(vp, IO_NODELOCKED,
4533 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4534 sizeof(dsattr), (char *)&dsattr, p);
4536 printf("pNFS: pnfscreate setextattr=%d\n",
4539 printf("pNFS: pnfscreate=%d\n", error);
4545 * Get the information needed to remove the pNFS Data Server file from the
4546 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4547 * DS directory vnode. The caller must unlock *ddvp when done with it.
4550 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4551 int *mirrorcntp, char *fname, fhandle_t *fhp)
4554 struct ucred *tcred;
4559 /* If not an exported regular file or not a pNFS server, just return. */
4560 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4561 nfsrv_devidcnt == 0)
4564 /* Check to see if this is the last hard link. */
4565 tcred = newnfs_getcred();
4566 error = VOP_GETATTR(vp, &va, tcred);
4569 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4572 if (va.va_nlink > 1)
4575 error = nfsvno_getfh(vp, fhp, p);
4577 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4582 buf = malloc(buflen, M_TEMP, M_WAITOK);
4583 /* Get the directory vnode for the DS mount and the file handle. */
4584 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4585 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4588 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4592 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4593 * The arguments are in a structure, so that they can be passed through
4594 * taskqueue for a kernel process to execute this function.
4596 struct nfsrvdsremove {
4600 struct ucred *tcred;
4604 char fname[PNFS_FILENAME_LEN + 1];
4608 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4611 struct nameidata named;
4617 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4620 named.ni_cnd.cn_nameiop = DELETE;
4621 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4622 named.ni_cnd.cn_cred = tcred;
4623 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
4624 nfsvno_setpathbuf(&named, &bufp, &hashp);
4625 named.ni_cnd.cn_nameptr = bufp;
4626 named.ni_cnd.cn_namelen = strlen(fname);
4627 strlcpy(bufp, fname, NAME_MAX);
4628 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4629 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4630 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4632 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4636 nfsvno_relpathbuf(&named);
4638 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4643 * Start up the thread that will execute nfsrv_dsremove().
4646 start_dsremove(void *arg, int pending)
4648 struct nfsrvdsremove *dsrm;
4650 dsrm = (struct nfsrvdsremove *)arg;
4651 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4654 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4658 * Remove a pNFS data file from a Data Server.
4659 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4660 * removed to set up the dvp and fill in the FH.
4663 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4666 struct ucred *tcred;
4667 struct nfsrvdsremove *dsrm, *tdsrm;
4668 struct nfsdevice *ds;
4669 struct nfsmount *nmp;
4670 int failpos, i, ret, timo;
4672 tcred = newnfs_getcred();
4675 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4677 * Remove the file on each DS mirror, using kernel process(es) for the
4678 * additional mirrors.
4681 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4682 tdsrm->tcred = tcred;
4684 tdsrm->dvp = dvp[i];
4685 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4690 if (nfs_pnfsiothreads != 0) {
4691 ret = nfs_pnfsio(start_dsremove, tdsrm);
4692 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4695 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4696 if (failpos == -1 && nfsds_failerr(ret))
4700 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4701 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4702 failpos = mirrorcnt - 1;
4703 timo = hz / 50; /* Wait for 20msec. */
4706 /* Wait for kernel task(s) to complete. */
4707 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4708 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4709 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4710 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4715 * If failpos has been set, that mirror has failed, so it needs
4719 nmp = VFSTONFS(dvp[failpos]->v_mount);
4721 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4722 NFSMNTP_CANCELRPCS)) == 0) {
4723 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4725 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4726 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4729 nfsrv_killrpcs(nmp);
4731 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4737 /* Get rid all layouts for the file. */
4738 nfsrv_freefilelayouts(fhp);
4745 * Generate a file name based on the file handle and put it in *bufp.
4746 * Return the number of bytes generated.
4749 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4753 const uint8_t *hexdigits = "0123456789abcdef";
4755 cp = (uint8_t *)fhp;
4756 for (i = 0; i < sizeof(*fhp); i++) {
4757 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4758 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4765 * Update the Metadata file's attributes from the DS file when a Read/Write
4766 * layout is returned.
4767 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4768 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4771 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4773 struct ucred *tcred;
4776 /* Do this as root so that it won't fail with EACCES. */
4777 tcred = newnfs_getcred();
4778 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4779 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4785 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4788 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4793 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4794 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4799 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4800 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4801 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4802 off_t *offp, int content, bool *eofp)
4804 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4805 fhandle_t fh[NFSDEV_MAXMIRRORS];
4806 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4807 struct nfsdevice *ds;
4808 struct pnfsdsattr dsattr;
4809 struct opnfsdsattr odsattr;
4811 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4813 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4815 * If not a regular file, not exported or not a pNFS server,
4816 * just return ENOENT.
4818 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4819 nfsrv_devidcnt == 0)
4823 buf = malloc(buflen, M_TEMP, M_WAITOK);
4827 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4828 * from the MetaData file's extended attribute.
4830 if (ioproc == NFSPROC_GETATTR) {
4831 error = vn_extattr_get(vp, IO_NODELOCKED,
4832 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4835 if (buflen == sizeof(odsattr)) {
4836 NFSBCOPY(buf, &odsattr, buflen);
4837 nap->na_filerev = odsattr.dsa_filerev;
4838 nap->na_size = odsattr.dsa_size;
4839 nap->na_atime = odsattr.dsa_atime;
4840 nap->na_mtime = odsattr.dsa_mtime;
4842 * Fake na_bytes by rounding up na_size.
4843 * Since we don't know the block size, just
4844 * use BLKDEV_IOSIZE.
4846 nap->na_bytes = (odsattr.dsa_size +
4847 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4848 } else if (buflen == sizeof(dsattr)) {
4849 NFSBCOPY(buf, &dsattr, buflen);
4850 nap->na_filerev = dsattr.dsa_filerev;
4851 nap->na_size = dsattr.dsa_size;
4852 nap->na_atime = dsattr.dsa_atime;
4853 nap->na_mtime = dsattr.dsa_mtime;
4854 nap->na_bytes = dsattr.dsa_bytes;
4860 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4861 * returns 0, just return now. nfsrv_checkdsattr()
4862 * returns 0 if there is no Read/Write layout
4863 * plus either an Open/Write_access or Write
4864 * delegation issued to a client for the file.
4866 if (nfsrv_pnfsgetdsattr == 0 ||
4867 nfsrv_checkdsattr(vp, p) == 0) {
4874 * Clear ENOATTR so the code below will attempt to do a
4875 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4876 * the extended attribute.
4878 if (error == ENOATTR)
4887 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4889 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4890 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4891 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4894 for (i = 0; i < mirrorcnt; i++)
4895 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4897 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4899 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4902 if (origmircnt == -1)
4903 origmircnt = mirrorcnt;
4905 * If failpos is set to a mirror#, then that mirror has
4906 * failed and will be disabled. For Read, Getattr and Seek, the
4907 * function only tries one mirror, so if that mirror has
4908 * failed, it will need to be retried. As such, increment
4909 * tryitagain for these cases.
4910 * For Write, Setattr and Setacl, the function tries all
4911 * mirrors and will not return an error for the case where
4912 * one mirror has failed. For these cases, the functioning
4913 * mirror(s) will have been modified, so a retry isn't
4914 * necessary. These functions will set failpos for the
4917 if (ioproc == NFSPROC_READDS) {
4918 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4920 if (nfsds_failerr(error) && mirrorcnt > 1) {
4922 * Setting failpos will cause the mirror
4923 * to be disabled and then a retry of this
4930 } else if (ioproc == NFSPROC_WRITEDS)
4931 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4932 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4933 else if (ioproc == NFSPROC_SETATTR)
4934 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4935 mirrorcnt, nap, &failpos);
4936 else if (ioproc == NFSPROC_SETACL)
4937 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4938 mirrorcnt, aclp, &failpos);
4939 else if (ioproc == NFSPROC_SEEKDS) {
4940 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4942 if (nfsds_failerr(error) && mirrorcnt > 1) {
4944 * Setting failpos will cause the mirror
4945 * to be disabled and then a retry of this
4952 } else if (ioproc == NFSPROC_ALLOCATE)
4953 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4954 &nmp[0], mirrorcnt, &failpos);
4955 else if (ioproc == NFSPROC_DEALLOCATE)
4956 error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p,
4957 vp, &nmp[0], mirrorcnt, &failpos);
4959 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4960 vp, nmp[mirrorcnt - 1], nap);
4961 if (nfsds_failerr(error) && mirrorcnt > 1) {
4963 * Setting failpos will cause the mirror
4964 * to be disabled and then a retry of this
4965 * getattr is required.
4967 failpos = mirrorcnt - 1;
4974 failnmp = nmp[failpos];
4975 NFSLOCKMNT(failnmp);
4976 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4977 NFSMNTP_CANCELRPCS)) == 0) {
4978 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4979 NFSUNLOCKMNT(failnmp);
4980 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4982 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4985 nfsrv_killrpcs(failnmp);
4986 NFSLOCKMNT(failnmp);
4987 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4990 NFSUNLOCKMNT(failnmp);
4992 for (i = 0; i < mirrorcnt; i++)
4993 NFSVOPUNLOCK(dvp[i]);
4994 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4996 /* Try the Read/Getattr again if a mirror was deleted. */
4997 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
5000 /* Return ENOENT for any Extended Attribute error. */
5004 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
5009 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
5011 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
5012 * to be checked. If it points to a NULL nmp, then it returns
5013 * a suitable destination.
5014 * curnmp - If non-NULL, it is the source mount for the copy.
5017 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
5018 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
5019 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
5020 struct nfsmount *curnmp, int *ippos, int *dsdirp)
5022 struct vnode *dvp, *nvp = NULL, **tdvpp;
5024 struct nfsmount *nmp, *newnmp;
5025 struct sockaddr *sad;
5026 struct sockaddr_in *sin;
5027 struct nfsdevice *ds, *tds, *fndds;
5028 struct pnfsdsfile *pf;
5030 int error, fhiszero, fnd, gotone, i, mirrorcnt;
5032 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
5041 if (newnmpp != NULL)
5046 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5047 "pnfsd.dsfile", buflenp, buf, p);
5048 mirrorcnt = *buflenp / sizeof(*pf);
5049 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
5050 *buflenp != sizeof(*pf) * mirrorcnt))
5053 pf = (struct pnfsdsfile *)buf;
5054 /* If curnmp != NULL, check for a match in the mirror list. */
5055 if (curnmp != NULL && error == 0) {
5057 for (i = 0; i < mirrorcnt; i++, pf++) {
5058 sad = (struct sockaddr *)&pf->dsf_sin;
5059 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5071 pf = (struct pnfsdsfile *)buf;
5072 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5074 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5076 sad = (struct sockaddr *)&pf->dsf_sin;
5078 dsdir = pf->dsf_dir;
5079 if (dsdir >= nfsrv_dsdirsize) {
5080 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5082 } else if (nvpp != NULL && newnmp != NULL &&
5083 nfsaddr2_match(sad, newnmp->nm_nam))
5086 if (ippos != NULL && curnmp == NULL &&
5087 sad->sa_family == AF_INET &&
5088 sin->sin_addr.s_addr == 0)
5090 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5092 /* Use the socket address to find the mount point. */
5095 /* Find a match for the IP address. */
5096 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5097 if (ds->nfsdev_nmp != NULL) {
5098 dvp = ds->nfsdev_dvp;
5099 nmp = VFSTONFS(dvp->v_mount);
5100 if (nmp != ds->nfsdev_nmp)
5101 printf("different2 nmp %p %p\n",
5102 nmp, ds->nfsdev_nmp);
5103 if (nfsaddr2_match(sad, nmp->nm_nam)) {
5109 if (fndds != NULL && newnmpp != NULL &&
5111 /* Search for a place to make a mirror copy. */
5112 TAILQ_FOREACH(tds, &nfsrv_devidhead,
5114 if (tds->nfsdev_nmp != NULL &&
5116 ((tds->nfsdev_mdsisset == 0 &&
5117 fndds->nfsdev_mdsisset == 0) ||
5118 (tds->nfsdev_mdsisset != 0 &&
5119 fndds->nfsdev_mdsisset != 0 &&
5120 fsidcmp(&tds->nfsdev_mdsfsid,
5121 &mp->mnt_stat.f_fsid) == 0))) {
5122 *newnmpp = tds->nfsdev_nmp;
5128 * Move this entry to the end of the
5129 * list, so it won't be selected as
5130 * easily the next time.
5132 TAILQ_REMOVE(&nfsrv_devidhead, tds,
5134 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5139 if (fndds != NULL) {
5140 dvp = fndds->nfsdev_dsdir[dsdir];
5141 if (lktype != 0 || fhiszero != 0 ||
5142 (nvpp != NULL && *nvpp == NULL)) {
5144 error = vn_lock(dvp,
5146 else if (lktype != 0)
5147 error = vn_lock(dvp, lktype);
5149 error = vn_lock(dvp, LK_SHARED);
5151 * If the file handle is all 0's, try to
5152 * do a Lookup against the DS to acquire
5154 * If dvpp == NULL or the Lookup fails,
5155 * unlock dvp after the call.
5157 if (error == 0 && (fhiszero != 0 ||
5158 (nvpp != NULL && *nvpp == NULL))) {
5159 error = nfsrv_pnfslookupds(vp,
5175 if (error != 0 || lktype == 0)
5181 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5182 if (devid != NULL) {
5183 NFSBCOPY(fndds->nfsdev_deviceid,
5184 devid, NFSX_V4DEVICEID);
5185 devid += NFSX_V4DEVICEID;
5190 NFSBCOPY(&pf->dsf_fh, fhp++,
5192 if (fnamep != NULL && gotone == 1)
5195 sizeof(pf->dsf_filename));
5197 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5202 if (error == 0 && gotone == 0)
5205 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5208 *mirrorcntp = gotone;
5210 if (gotone > 0 && dvpp != NULL) {
5212 * If the error didn't occur on the first one and
5213 * dvpp != NULL, the one(s) prior to the failure will
5214 * have locked dvp's that need to be unlocked.
5216 for (i = 0; i < gotone; i++) {
5217 NFSVOPUNLOCK(*dvpp);
5222 * If it found the vnode to be copied from before a failure,
5223 * it needs to be vput()'d.
5225 if (nvpp != NULL && *nvpp != NULL) {
5234 * Set the extended attribute for the Change attribute.
5237 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5239 struct pnfsdsattr dsattr;
5242 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5243 dsattr.dsa_filerev = nap->na_filerev;
5244 dsattr.dsa_size = nap->na_size;
5245 dsattr.dsa_atime = nap->na_atime;
5246 dsattr.dsa_mtime = nap->na_mtime;
5247 dsattr.dsa_bytes = nap->na_bytes;
5248 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5249 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5251 printf("pNFS: setextattr=%d\n", error);
5256 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5257 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5260 struct nfsrv_descript *nd;
5262 struct mbuf *m, *m2;
5263 int error = 0, retlen, tlen, trimlen;
5265 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5266 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5269 * Use a stateid where other is an alternating 01010 pattern and
5270 * seqid is 0xffffffff. This value is not defined as special by
5271 * the RFC and is used by the FreeBSD NFS server to indicate an
5272 * MDS->DS proxy operation.
5274 st.other[0] = 0x55555555;
5275 st.other[1] = 0x55555555;
5276 st.other[2] = 0x55555555;
5277 st.seqid = 0xffffffff;
5278 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5279 NULL, NULL, 0, 0, cred);
5280 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5281 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5282 txdr_hyper(off, tl);
5283 *(tl + 2) = txdr_unsigned(len);
5284 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5285 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5290 if (nd->nd_repstat == 0) {
5291 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5292 NFSM_STRSIZ(retlen, len);
5294 /* Trim off the pre-data XDR from the mbuf chain. */
5296 while (m != NULL && m != nd->nd_md) {
5297 if (m->m_next == nd->nd_md) {
5299 m_freem(nd->nd_mrep);
5300 nd->nd_mrep = m = nd->nd_md;
5305 printf("nfsrv_readdsrpc: busted mbuf list\n");
5311 * Now, adjust first mbuf so that any XDR before the
5312 * read data is skipped over.
5314 trimlen = nd->nd_dpos - mtod(m, char *);
5316 m->m_len -= trimlen;
5317 NFSM_DATAP(m, trimlen);
5321 * Truncate the mbuf chain at retlen bytes of data,
5322 * plus XDR padding that brings the length up to a
5325 tlen = NFSM_RNDUP(retlen);
5327 if (m->m_len >= tlen) {
5337 } while (m != NULL);
5339 printf("nfsrv_readdsrpc: busted mbuf list\n");
5348 error = nd->nd_repstat;
5350 /* If nd->nd_mrep is already NULL, this is a no-op. */
5351 m_freem(nd->nd_mrep);
5353 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5358 * Do a write RPC on a DS data file, using this structure for the arguments,
5359 * so that this function can be executed by a separate kernel process.
5361 struct nfsrvwritedsdorpc {
5368 struct nfsmount *nmp;
5376 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5377 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5380 struct nfsrv_descript *nd;
5381 nfsattrbit_t attrbits;
5383 int commit, error, retlen;
5385 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5386 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5387 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5390 * Use a stateid where other is an alternating 01010 pattern and
5391 * seqid is 0xffffffff. This value is not defined as special by
5392 * the RFC and is used by the FreeBSD NFS server to indicate an
5393 * MDS->DS proxy operation.
5395 st.other[0] = 0x55555555;
5396 st.other[1] = 0x55555555;
5397 st.other[2] = 0x55555555;
5398 st.seqid = 0xffffffff;
5399 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5400 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5401 txdr_hyper(off, tl);
5404 * Do all writes FileSync, since the server doesn't hold onto dirty
5405 * buffers. Since clients should be accessing the DS servers directly
5406 * using the pNFS layouts, this just needs to work correctly as a
5409 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5410 *tl = txdr_unsigned(len);
5411 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5413 /* Put data in mbuf chain. */
5414 nd->nd_mb->m_next = m;
5416 /* Set nd_mb and nd_bpos to end of data. */
5417 while (m->m_next != NULL)
5420 nfsm_set(nd, m->m_len);
5421 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5423 /* Do a Getattr for the attributes that change upon 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,
5434 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5439 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5440 /* Get rid of weak cache consistency data for now. */
5441 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5442 (ND_NFSV4 | ND_V4WCCATTR)) {
5443 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5444 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5445 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5449 * Get rid of Op# and status for next op.
5451 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5453 nd->nd_flag |= ND_NOMOREDATA;
5455 if (nd->nd_repstat == 0) {
5456 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5457 retlen = fxdr_unsigned(int, *tl++);
5458 commit = fxdr_unsigned(int, *tl);
5459 if (commit != NFSWRITE_FILESYNC)
5461 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5462 retlen, commit, error);
5464 error = nd->nd_repstat;
5465 /* We have no use for the Write Verifier since we use FileSync. */
5468 * Get the Change, Size, Access Time and Modify Time attributes and set
5469 * on the Metadata file, so its attributes will be what the file's
5470 * would be if it had been written.
5473 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5474 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5475 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5477 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5479 m_freem(nd->nd_mrep);
5481 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5486 * Start up the thread that will execute nfsrv_writedsdorpc().
5489 start_writedsdorpc(void *arg, int pending)
5491 struct nfsrvwritedsdorpc *drpc;
5493 drpc = (struct nfsrvwritedsdorpc *)arg;
5494 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5495 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5497 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5501 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5502 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5503 struct mbuf **mpp, char *cp, int *failposp)
5505 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5508 int error, i, offs, ret, timo;
5510 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5511 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5514 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5517 /* Calculate offset in mbuf chain that data starts. */
5518 offs = cp - mtod(*mpp, char *);
5519 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5522 * Do the write RPC for every DS, using a separate kernel process
5523 * for every DS except the last one.
5526 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5528 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5536 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5538 if (nfs_pnfsiothreads != 0) {
5539 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5540 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5544 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5546 if (nfsds_failerr(ret) && *failposp == -1)
5548 else if (error == 0 && ret != 0)
5554 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5555 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5556 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5557 *failposp = mirrorcnt - 1;
5558 else if (error == 0 && ret != 0)
5561 error = nfsrv_setextattr(vp, &na, p);
5562 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5564 timo = hz / 50; /* Wait for 20msec. */
5567 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5568 /* Wait for RPCs on separate threads to complete. */
5569 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5570 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5571 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5573 else if (error == 0 && tdrpc->err != 0)
5581 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5582 * so that this function can be executed by a separate kernel process.
5584 struct nfsrvallocatedsdorpc {
5591 struct nfsmount *nmp;
5598 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5599 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5602 struct nfsrv_descript *nd;
5603 nfsattrbit_t attrbits;
5607 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5608 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5609 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5612 * Use a stateid where other is an alternating 01010 pattern and
5613 * seqid is 0xffffffff. This value is not defined as special by
5614 * the RFC and is used by the FreeBSD NFS server to indicate an
5615 * MDS->DS proxy operation.
5617 st.other[0] = 0x55555555;
5618 st.other[1] = 0x55555555;
5619 st.other[2] = 0x55555555;
5620 st.seqid = 0xffffffff;
5621 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5622 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5623 txdr_hyper(off, tl); tl += 2;
5624 txdr_hyper(len, tl); tl += 2;
5625 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5627 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5628 NFSGETATTR_ATTRBIT(&attrbits);
5629 nfsrv_putattrbit(nd, &attrbits);
5630 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5631 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5636 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5638 if (nd->nd_repstat == 0) {
5639 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5640 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5641 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5643 error = nd->nd_repstat;
5644 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5646 m_freem(nd->nd_mrep);
5648 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5653 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5656 start_allocatedsdorpc(void *arg, int pending)
5658 struct nfsrvallocatedsdorpc *drpc;
5660 drpc = (struct nfsrvallocatedsdorpc *)arg;
5661 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5662 drpc->len, NULL, drpc->cred, drpc->p);
5664 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5668 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5669 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5672 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5674 int error, i, ret, timo;
5676 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5679 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5683 * Do the allocate RPC for every DS, using a separate kernel process
5684 * for every DS except the last one.
5687 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5689 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5698 if (nfs_pnfsiothreads != 0) {
5699 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5700 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5704 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5706 if (nfsds_failerr(ret) && *failposp == -1)
5708 else if (error == 0 && ret != 0)
5714 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5715 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5716 *failposp = mirrorcnt - 1;
5717 else if (error == 0 && ret != 0)
5720 error = nfsrv_setextattr(vp, &na, p);
5721 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5723 timo = hz / 50; /* Wait for 20msec. */
5726 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5727 /* Wait for RPCs on separate threads to complete. */
5728 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5729 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5730 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5732 else if (error == 0 && tdrpc->err != 0)
5740 * Do a deallocate RPC on a DS data file, using this structure for the
5741 * arguments, so that this function can be executed by a separate kernel
5744 struct nfsrvdeallocatedsdorpc {
5751 struct nfsmount *nmp;
5758 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5759 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5762 struct nfsrv_descript *nd;
5763 nfsattrbit_t attrbits;
5767 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5768 nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp,
5769 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5772 * Use a stateid where other is an alternating 01010 pattern and
5773 * seqid is 0xffffffff. This value is not defined as special by
5774 * the RFC and is used by the FreeBSD NFS server to indicate an
5775 * MDS->DS proxy operation.
5777 st.other[0] = 0x55555555;
5778 st.other[1] = 0x55555555;
5779 st.other[2] = 0x55555555;
5780 st.seqid = 0xffffffff;
5781 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5782 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5783 txdr_hyper(off, tl); tl += 2;
5784 txdr_hyper(len, tl); tl += 2;
5785 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len);
5787 /* Do a Getattr for the attributes that change upon writing. */
5788 NFSZERO_ATTRBIT(&attrbits);
5789 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5790 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5791 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5792 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5793 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5794 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5795 nfsrv_putattrbit(nd, &attrbits);
5796 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5797 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5802 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n",
5804 /* Get rid of weak cache consistency data for now. */
5805 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5806 (ND_NFSV4 | ND_V4WCCATTR)) {
5807 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5808 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5809 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error);
5813 * Get rid of Op# and status for next op.
5815 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5817 nd->nd_flag |= ND_NOMOREDATA;
5819 if (nd->nd_repstat == 0) {
5820 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5821 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5822 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5824 error = nd->nd_repstat;
5825 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error);
5827 m_freem(nd->nd_mrep);
5829 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error);
5834 * Start up the thread that will execute nfsrv_deallocatedsdorpc().
5837 start_deallocatedsdorpc(void *arg, int pending)
5839 struct nfsrvdeallocatedsdorpc *drpc;
5841 drpc = (struct nfsrvdeallocatedsdorpc *)arg;
5842 drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5843 drpc->len, NULL, drpc->cred, drpc->p);
5845 NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err);
5849 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5850 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5853 struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL;
5855 int error, i, ret, timo;
5857 NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n");
5860 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5864 * Do the deallocate RPC for every DS, using a separate kernel process
5865 * for every DS except the last one.
5868 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5870 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5879 if (nfs_pnfsiothreads != 0) {
5880 ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc);
5881 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n",
5885 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len,
5887 if (nfsds_failerr(ret) && *failposp == -1)
5889 else if (error == 0 && ret != 0)
5895 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5896 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5897 *failposp = mirrorcnt - 1;
5898 else if (error == 0 && ret != 0)
5901 error = nfsrv_setextattr(vp, &na, p);
5902 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error);
5904 timo = hz / 50; /* Wait for 20msec. */
5907 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5908 /* Wait for RPCs on separate threads to complete. */
5909 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5910 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5911 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5913 else if (error == 0 && tdrpc->err != 0)
5921 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5922 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5923 struct nfsvattr *dsnap)
5926 struct nfsrv_descript *nd;
5928 nfsattrbit_t attrbits;
5931 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5932 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5934 * Use a stateid where other is an alternating 01010 pattern and
5935 * seqid is 0xffffffff. This value is not defined as special by
5936 * the RFC and is used by the FreeBSD NFS server to indicate an
5937 * MDS->DS proxy operation.
5939 st.other[0] = 0x55555555;
5940 st.other[1] = 0x55555555;
5941 st.other[2] = 0x55555555;
5942 st.seqid = 0xffffffff;
5943 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5944 NULL, NULL, 0, 0, cred);
5945 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5946 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5948 /* Do a Getattr for the attributes that change due to writing. */
5949 NFSZERO_ATTRBIT(&attrbits);
5950 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5951 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5952 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5953 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5954 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5955 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5956 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5957 (void) nfsrv_putattrbit(nd, &attrbits);
5958 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5959 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5964 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5966 /* Get rid of weak cache consistency data for now. */
5967 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5968 (ND_NFSV4 | ND_V4WCCATTR)) {
5969 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5970 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5971 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5975 * Get rid of Op# and status for next op.
5977 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5979 nd->nd_flag |= ND_NOMOREDATA;
5981 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5984 if (nd->nd_repstat != 0)
5985 error = nd->nd_repstat;
5987 * Get the Change, Size, Access Time and Modify Time attributes and set
5988 * on the Metadata file, so its attributes will be what the file's
5989 * would be if it had been written.
5992 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5993 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5994 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5996 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5998 m_freem(nd->nd_mrep);
6000 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
6004 struct nfsrvsetattrdsdorpc {
6009 struct nfsmount *nmp;
6014 struct nfsvattr dsna;
6019 * Start up the thread that will execute nfsrv_setattrdsdorpc().
6022 start_setattrdsdorpc(void *arg, int pending)
6024 struct nfsrvsetattrdsdorpc *drpc;
6026 drpc = (struct nfsrvsetattrdsdorpc *)arg;
6027 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
6028 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
6033 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6034 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6035 struct nfsvattr *nap, int *failposp)
6037 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
6039 int error, i, ret, timo;
6041 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
6044 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6048 * Do the setattr RPC for every DS, using a separate kernel process
6049 * for every DS except the last one.
6052 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6055 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6063 if (nfs_pnfsiothreads != 0) {
6064 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
6065 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
6069 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
6071 if (nfsds_failerr(ret) && *failposp == -1)
6073 else if (error == 0 && ret != 0)
6079 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
6080 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6081 *failposp = mirrorcnt - 1;
6082 else if (error == 0 && ret != 0)
6085 error = nfsrv_setextattr(vp, &na, p);
6086 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
6088 timo = hz / 50; /* Wait for 20msec. */
6091 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6092 /* Wait for RPCs on separate threads to complete. */
6093 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6094 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
6095 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6097 else if (error == 0 && tdrpc->err != 0)
6105 * Do a Setattr of an NFSv4 ACL on the DS file.
6108 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6109 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
6111 struct nfsrv_descript *nd;
6113 nfsattrbit_t attrbits;
6116 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
6117 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6119 * Use a stateid where other is an alternating 01010 pattern and
6120 * seqid is 0xffffffff. This value is not defined as special by
6121 * the RFC and is used by the FreeBSD NFS server to indicate an
6122 * MDS->DS proxy operation.
6124 st.other[0] = 0x55555555;
6125 st.other[1] = 0x55555555;
6126 st.other[2] = 0x55555555;
6127 st.seqid = 0xffffffff;
6128 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6129 NULL, NULL, 0, 0, cred);
6130 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6131 NFSZERO_ATTRBIT(&attrbits);
6132 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
6134 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
6135 * so passing in the metadata "vp" will be ok, since it is of
6136 * the same type (VREG).
6138 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
6139 NULL, 0, 0, 0, 0, 0, NULL);
6140 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6141 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6146 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
6148 error = nd->nd_repstat;
6149 m_freem(nd->nd_mrep);
6154 struct nfsrvsetacldsdorpc {
6159 struct nfsmount *nmp;
6168 * Start up the thread that will execute nfsrv_setacldsdorpc().
6171 start_setacldsdorpc(void *arg, int pending)
6173 struct nfsrvsetacldsdorpc *drpc;
6175 drpc = (struct nfsrvsetacldsdorpc *)arg;
6176 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
6177 drpc->vp, drpc->nmp, drpc->aclp);
6182 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6183 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
6186 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
6187 int error, i, ret, timo;
6189 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
6192 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6196 * Do the setattr RPC for every DS, using a separate kernel process
6197 * for every DS except the last one.
6200 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6203 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6211 if (nfs_pnfsiothreads != 0) {
6212 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
6213 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
6217 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
6219 if (nfsds_failerr(ret) && *failposp == -1)
6221 else if (error == 0 && ret != 0)
6227 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
6228 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6229 *failposp = mirrorcnt - 1;
6230 else if (error == 0 && ret != 0)
6232 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
6234 timo = hz / 50; /* Wait for 20msec. */
6237 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6238 /* Wait for RPCs on separate threads to complete. */
6239 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6240 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6241 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6243 else if (error == 0 && tdrpc->err != 0)
6251 * Getattr call to the DS for the attributes that change due to writing.
6254 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6255 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6257 struct nfsrv_descript *nd;
6259 nfsattrbit_t attrbits;
6261 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6262 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6263 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6264 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6265 NFSZERO_ATTRBIT(&attrbits);
6266 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6267 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6268 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6269 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6270 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6271 (void) nfsrv_putattrbit(nd, &attrbits);
6272 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6273 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6278 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6280 if (nd->nd_repstat == 0) {
6281 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6282 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6285 * We can only save the updated values in the extended
6286 * attribute if the vp is exclusively locked.
6287 * This should happen when any of the following operations
6288 * occur on the vnode:
6289 * Close, Delegreturn, LayoutCommit, LayoutReturn
6290 * As such, the updated extended attribute should get saved
6291 * before nfsrv_checkdsattr() returns 0 and allows the cached
6292 * attributes to be returned without calling this function.
6294 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6295 error = nfsrv_setextattr(vp, nap, p);
6296 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6300 error = nd->nd_repstat;
6301 m_freem(nd->nd_mrep);
6303 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6308 * Seek call to a DS.
6311 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6312 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6315 struct nfsrv_descript *nd;
6319 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6321 * Use a stateid where other is an alternating 01010 pattern and
6322 * seqid is 0xffffffff. This value is not defined as special by
6323 * the RFC and is used by the FreeBSD NFS server to indicate an
6324 * MDS->DS proxy operation.
6326 st.other[0] = 0x55555555;
6327 st.other[1] = 0x55555555;
6328 st.other[2] = 0x55555555;
6329 st.seqid = 0xffffffff;
6330 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6331 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6332 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6333 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6334 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6335 txdr_hyper(*offp, tl); tl += 2;
6336 *tl = txdr_unsigned(content);
6337 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6338 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6343 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6344 if (nd->nd_repstat == 0) {
6345 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6346 if (*tl++ == newnfs_true)
6350 *offp = fxdr_hyper(tl);
6352 error = nd->nd_repstat;
6354 m_freem(nd->nd_mrep);
6356 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6361 * Get the device id and file handle for a DS file.
6364 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6365 fhandle_t *fhp, char *devid)
6371 buf = malloc(buflen, M_TEMP, M_WAITOK);
6372 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6373 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6379 * Do a Lookup against the DS for the filename.
6382 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6383 struct vnode **nvpp, NFSPROC_T *p)
6385 struct nameidata named;
6386 struct ucred *tcred;
6392 tcred = newnfs_getcred();
6393 named.ni_cnd.cn_nameiop = LOOKUP;
6394 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6395 named.ni_cnd.cn_cred = tcred;
6396 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
6397 nfsvno_setpathbuf(&named, &bufp, &hashp);
6398 named.ni_cnd.cn_nameptr = bufp;
6399 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6400 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6401 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6402 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6403 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6405 nfsvno_relpathbuf(&named);
6408 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6413 * Set the file handle to the correct one.
6416 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6417 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6423 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6425 * We can only do a vn_set_extattr() if the vnode is exclusively
6426 * locked and vn_start_write() has been done. If devid != NULL or
6427 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6428 * may not have been done.
6429 * If not done now, it will be done on a future call.
6431 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6433 ret = vn_extattr_set(vp, IO_NODELOCKED,
6434 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6436 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6440 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6441 * when the DS has failed.
6444 nfsrv_killrpcs(struct nfsmount *nmp)
6448 * Call newnfs_nmcancelreqs() to cause
6449 * any RPCs in progress on the mount point to
6451 * This will cause any process waiting for an
6452 * RPC to complete while holding a vnode lock
6453 * on the mounted-on vnode (such as "df" or
6454 * a non-forced "umount") to fail.
6455 * This will unlock the mounted-on vnode so
6456 * a forced dismount can succeed.
6457 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6460 newnfs_nmcancelreqs(nmp);
6464 * Sum up the statfs info for each of the DSs, so that the client will
6465 * receive the total for all DSs.
6468 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6471 struct nfsdevice *ds;
6472 struct vnode **dvpp, **tdvpp, *dvp;
6474 int cnt, error = 0, i;
6476 if (nfsrv_devidcnt <= 0)
6478 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6479 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6481 /* Get an array of the dvps for the DSs. */
6485 /* First, search for matches for same file system. */
6486 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6487 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6488 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6489 if (++i > nfsrv_devidcnt)
6491 *tdvpp++ = ds->nfsdev_dvp;
6495 * If no matches for same file system, total all servers not assigned
6499 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6500 if (ds->nfsdev_nmp != NULL &&
6501 ds->nfsdev_mdsisset == 0) {
6502 if (++i > nfsrv_devidcnt)
6504 *tdvpp++ = ds->nfsdev_dvp;
6511 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6513 for (i = 0; i < cnt && error == 0; i++) {
6515 error = VFS_STATFS(dvp->v_mount, tsf);
6517 if (sf->f_bsize == 0) {
6518 if (tsf->f_bsize > 0)
6519 sf->f_bsize = tsf->f_bsize;
6523 if (tsf->f_blocks > 0) {
6524 if (sf->f_bsize != tsf->f_bsize) {
6525 tot = tsf->f_blocks * tsf->f_bsize;
6526 sf->f_blocks += (tot / sf->f_bsize);
6528 sf->f_blocks += tsf->f_blocks;
6530 if (tsf->f_bfree > 0) {
6531 if (sf->f_bsize != tsf->f_bsize) {
6532 tot = tsf->f_bfree * tsf->f_bsize;
6533 sf->f_bfree += (tot / sf->f_bsize);
6535 sf->f_bfree += tsf->f_bfree;
6537 if (tsf->f_bavail > 0) {
6538 if (sf->f_bsize != tsf->f_bsize) {
6539 tot = tsf->f_bavail * tsf->f_bsize;
6540 sf->f_bavail += (tot / sf->f_bsize);
6542 sf->f_bavail += tsf->f_bavail;
6555 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6559 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6560 error = NFSERR_ATTRNOTSUPP;
6564 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6565 * Make sure it has enough room for that - splitting every entry
6566 * into two and appending "canonical six" entries at the end.
6567 * Cribbed out of kern/vfs_acl.c - Rick M.
6569 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6570 error = NFSERR_ATTRNOTSUPP;
6573 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6575 error = nfsrv_dssetacl(vp, aclp, cred, p);
6576 if (error == ENOENT)
6586 * Seek vnode op call (actually it is a VOP_IOCTL()).
6587 * This function is called with the vnode locked, but unlocks and vrele()s
6588 * the vp before returning.
6591 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6592 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6597 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6599 * Attempt to seek on a DS file. A return of ENOENT implies
6600 * there is no DS file to seek on.
6602 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6603 NULL, NULL, NULL, NULL, offp, content, eofp);
6604 if (error != ENOENT) {
6610 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6611 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6612 * NFSv4.2 is *eofp == true and error == 0 for this case.
6615 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6617 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6618 /* Handle the cases where we might be at EOF. */
6619 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6620 if (ret == 0 && *offp == at.na_size) {
6624 if (ret != 0 && error == 0)
6633 * Allocate vnode op call.
6636 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6642 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6644 * Attempt to allocate on a DS file. A return of ENOENT implies
6645 * there is no DS file to allocate on.
6647 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6648 NULL, NULL, NULL, NULL, &len, 0, NULL);
6649 if (error != ENOENT)
6653 * Do the actual VOP_ALLOCATE(), looping so long as
6654 * progress is being made, to achieve completion.
6658 error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred);
6659 if (error == 0 && len > 0 && olen > len)
6661 } while (error == 0 && len > 0 && olen > len);
6662 if (error == 0 && len > 0)
6669 * Deallocate vnode op call.
6672 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6678 ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp");
6680 * Attempt to deallocate on a DS file. A return of ENOENT implies
6681 * there is no DS file to deallocate on.
6683 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL,
6684 NULL, NULL, NULL, NULL, &len, 0, NULL);
6685 if (error != ENOENT)
6689 * Do the actual VOP_DEALLOCATE(), looping so long as
6690 * progress is being made, to achieve completion.
6694 error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred);
6695 if (error == 0 && len > 0 && olen > len)
6697 } while (error == 0 && len > 0 && olen > len);
6698 if (error == 0 && len > 0)
6705 * Get Extended Atribute vnode op into an mbuf list.
6708 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6709 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6710 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6713 struct uio io, *uiop = &io;
6714 struct mbuf *m, *m2;
6715 int alen, error, len, tlen;
6718 /* First, find out the size of the extended attribute. */
6719 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6722 return (NFSERR_NOXATTR);
6723 if (siz > maxresp - NFS_MAXXDR)
6724 return (NFSERR_XATTR2BIG);
6726 tlen = NFSM_RNDUP(len);
6729 * If cnt > MCLBYTES and the reply will not be saved, use
6730 * ext_pgs mbufs for TLS.
6731 * For NFSv4.0, we do not know for sure if the reply will
6732 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6733 * Always use ext_pgs mbufs if ND_EXTPG is set.
6735 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6736 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6737 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6738 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6739 maxextsiz, &m, &m2, &iv);
6741 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6745 uiop->uio_iovcnt = 0;
6746 uiop->uio_iov = iv = NULL;
6749 uiop->uio_offset = 0;
6750 uiop->uio_resid = tlen;
6751 uiop->uio_rw = UIO_READ;
6752 uiop->uio_segflg = UIO_SYSSPACE;
6755 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6762 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6766 if (uiop->uio_resid > 0) {
6768 len = tlen - uiop->uio_resid;
6769 tlen = NFSM_RNDUP(len);
6771 printf("nfsvno_getxattr: weird size read\n");
6775 } else if (alen != tlen || tlen != len)
6776 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6794 * Set Extended attribute vnode op from an mbuf list.
6797 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6798 char *cp, struct ucred *cred, struct thread *p)
6801 struct uio uio, *uiop = &uio;
6806 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6812 uiop->uio_rw = UIO_WRITE;
6813 uiop->uio_segflg = UIO_SYSSPACE;
6815 uiop->uio_offset = 0;
6816 uiop->uio_resid = len;
6818 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6820 uiop->uio_iovcnt = cnt;
6822 uiop->uio_iov = iv = NULL;
6823 uiop->uio_iovcnt = 0;
6826 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6829 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6830 nfsvno_updateds(vp, cred, p);
6831 error = VOP_FSYNC(vp, MNT_WAIT, p);
6842 * For a pNFS server, the DS file's ctime and
6843 * va_filerev (TimeMetadata and Change) needs to
6844 * be updated. This is a hack, but works by
6845 * flipping the S_ISGID bit in va_mode and then
6847 * It does result in two MDS->DS RPCs, but creating
6848 * a custom RPC just to do this seems overkill, since
6849 * Setxattr/Rmxattr will not be done that frequently.
6850 * If it fails part way through, that is not too
6851 * serious, since the DS file is never executed.
6854 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p)
6856 struct nfsvattr nva;
6860 ret = VOP_GETATTR(vp, &nva.na_vattr, cred);
6862 tmode = nva.na_mode;
6863 NFSVNO_ATTRINIT(&nva);
6865 NFSVNO_SETATTRVAL(&nva, mode, tmode);
6866 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6867 NFSPROC_SETATTR, NULL, NULL, NULL, &nva,
6868 NULL, NULL, 0, NULL);
6871 NFSVNO_SETATTRVAL(&nva, mode, tmode);
6872 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6873 NFSPROC_SETATTR, NULL, NULL, NULL,
6874 &nva, NULL, NULL, 0, NULL);
6880 * Remove Extended attribute vnode op.
6883 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6884 struct ucred *cred, struct thread *p)
6889 * Get rid of any delegations. I am not sure why this is required,
6890 * but RFC-8276 says so.
6892 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6896 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6902 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6903 if (error == EOPNOTSUPP)
6904 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6907 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6908 nfsvno_updateds(vp, cred, p);
6909 error = VOP_FSYNC(vp, MNT_WAIT, p);
6917 * List Extended Atribute vnode op into an mbuf list.
6920 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6921 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6929 /* First, find out the size of the extended attribute. */
6930 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6933 return (NFSERR_NOXATTR);
6934 if (siz <= cookie) {
6939 if (siz > cookie + *lenp) {
6940 siz = cookie + *lenp;
6944 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6945 if (siz > 10 * 1024 * 1024) {
6946 error = NFSERR_XATTR2BIG;
6949 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6950 iv.iov_base = *bufp;
6956 io.uio_rw = UIO_READ;
6957 io.uio_segflg = UIO_SYSSPACE;
6960 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6965 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6969 if (io.uio_resid > 0)
6970 siz -= io.uio_resid;
6975 free(*bufp, M_TEMP);
6983 * Trim trailing data off the mbuf list being built.
6986 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6987 int bextpg, int bextpgsiz)
6992 if (mb->m_next != NULL) {
6993 m_freem(mb->m_next);
6996 if ((mb->m_flags & M_EXTPG) != 0) {
6997 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
6998 ("nfsm_trimtrailing: bextpg out of range"));
6999 KASSERT(bpos == (char *)(void *)
7000 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
7001 ("nfsm_trimtrailing: bextpgsiz bad!"));
7003 /* First, get rid of any pages after this position. */
7004 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
7005 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
7006 vm_page_unwire_noq(pg);
7009 mb->m_epg_npgs = bextpg + 1;
7011 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
7013 fullpgsiz = PAGE_SIZE;
7014 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
7015 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
7016 for (i = 1; i < mb->m_epg_npgs; i++)
7017 mb->m_len += m_epg_pagelen(mb, i, 0);
7018 nd->nd_bextpgsiz = bextpgsiz;
7019 nd->nd_bextpg = bextpg;
7021 mb->m_len = bpos - mtod(mb, char *);
7028 * Check to see if a put file handle operation should test for
7029 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
7030 * When Open is the next operation, NFSERR_WRONGSEC cannot be
7031 * replied for the Open cases that use a component. This can
7032 * be identified by the fact that the file handle's type is VDIR.
7035 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, __enum_uint8(vtype) vtyp)
7038 if ((nd->nd_flag & ND_NFSV4) == 0)
7041 if ((nd->nd_flag & ND_LASTOP) != 0)
7044 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
7045 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
7046 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
7047 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
7049 if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
7055 * Check DSs marked no space.
7058 nfsrv_checknospc(void)
7061 struct nfsdevice *ds;
7062 struct vnode **dvpp, **tdvpp, *dvp;
7063 char *devid, *tdevid;
7064 int cnt, error = 0, i;
7066 if (nfsrv_devidcnt <= 0)
7068 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
7069 devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK);
7070 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
7072 /* Get an array of the dvps for the DSs. */
7077 /* First, search for matches for same file system. */
7078 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7079 if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) {
7080 if (++i > nfsrv_devidcnt)
7082 *tdvpp++ = ds->nfsdev_dvp;
7083 NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID);
7084 tdevid += NFSX_V4DEVICEID;
7089 /* Do a VFS_STATFS() for each of the DSs and clear no space. */
7093 for (i = 0; i < cnt && error == 0; i++) {
7095 error = VFS_STATFS(dvp->v_mount, tsf);
7096 if (error == 0 && tsf->f_bavail > 0) {
7097 NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n");
7098 nfsrv_marknospc(tdevid, false);
7100 tdevid += NFSX_V4DEVICEID;
7104 free(devid, M_TEMP);
7108 * Initialize everything that needs to be initialized for a vnet.
7111 nfsrv_vnetinit(const void *unused __unused)
7116 VNET_SYSINIT(nfsrv_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7117 nfsrv_vnetinit, NULL);
7120 * Clean up everything that is in a vnet and needs to be
7121 * done when the jail is destroyed or the module unloaded.
7124 nfsrv_cleanup(const void *unused __unused)
7129 if (!NFSD_VNET(nfsrv_mntinited)) {
7133 NFSD_VNET(nfsrv_mntinited) = false;
7136 /* Clean out all NFSv4 state. */
7137 nfsrv_throwawayallstate(curthread);
7139 /* Clean the NFS server reply cache */
7140 nfsrvd_cleancache();
7142 /* Clean out v4root exports. */
7143 if (NFSD_VNET(nfsv4root_mnt)->mnt_export != NULL) {
7144 vfs_free_addrlist(NFSD_VNET(nfsv4root_mnt)->mnt_export);
7145 free(NFSD_VNET(nfsv4root_mnt)->mnt_export, M_MOUNT);
7146 NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
7149 /* Free up the krpc server pool. */
7150 if (NFSD_VNET(nfsrvd_pool) != NULL)
7151 svcpool_destroy(NFSD_VNET(nfsrvd_pool));
7153 /* and get rid of the locks */
7154 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7155 mtx_destroy(&NFSD_VNET(nfsrchash_table)[i].mtx);
7156 mtx_destroy(&NFSD_VNET(nfsrcahash_table)[i].mtx);
7158 mtx_destroy(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx);
7159 for (i = 0; i < nfsrv_sessionhashsize; i++)
7160 mtx_destroy(&NFSD_VNET(nfssessionhash)[i].mtx);
7161 lockdestroy(&NFSD_VNET(nfsv4root_mnt)->mnt_explock);
7162 free(NFSD_VNET(nfsrvudphashtbl), M_NFSRVCACHE);
7163 free(NFSD_VNET(nfsrchash_table), M_NFSRVCACHE);
7164 free(NFSD_VNET(nfsrcahash_table), M_NFSRVCACHE);
7165 free(NFSD_VNET(nfsclienthash), M_NFSDCLIENT);
7166 free(NFSD_VNET(nfslockhash), M_NFSDLOCKFILE);
7167 free(NFSD_VNET(nfssessionhash), M_NFSDSESSION);
7168 free(NFSD_VNET(nfsv4root_mnt), M_TEMP);
7169 NFSD_VNET(nfsv4root_mnt) = NULL;
7171 VNET_SYSUNINIT(nfsrv_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7172 nfsrv_cleanup, NULL);
7174 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
7177 * Called once to initialize data structures...
7180 nfsd_modevent(module_t mod, int type, void *data)
7183 static int loaded = 0;
7190 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
7191 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
7192 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
7193 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
7194 #ifdef VV_DISABLEDELEG
7195 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
7196 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
7198 nfsd_call_nfsd = nfssvc_nfsd;
7203 if (newnfs_numnfsd != 0) {
7208 #ifdef VV_DISABLEDELEG
7209 vn_deleg_ops.vndeleg_recall = NULL;
7210 vn_deleg_ops.vndeleg_disable = NULL;
7212 nfsd_call_nfsd = NULL;
7213 mtx_destroy(&nfsrc_udpmtx);
7214 mtx_destroy(&nfs_v4root_mutex);
7215 mtx_destroy(&nfsrv_dontlistlock_mtx);
7216 mtx_destroy(&nfsrv_recalllock_mtx);
7217 if (nfslayouthash != NULL) {
7218 for (i = 0; i < nfsrv_layouthashsize; i++)
7219 mtx_destroy(&nfslayouthash[i].mtx);
7220 free(nfslayouthash, M_NFSDSESSION);
7233 static moduledata_t nfsd_mod = {
7238 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
7240 /* So that loader and kldload(2) can find us, wherever we are.. */
7241 MODULE_VERSION(nfsd, 1);
7242 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
7243 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
7244 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
7245 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);