2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/capsicum.h>
40 #include <sys/extattr.h>
43 * Functions that perform the vfs operations required by the routines in
44 * nfsd_serv.c. It is hoped that this change will make the server more
48 #include <fs/nfs/nfsport.h>
49 #include <security/mac/mac_framework.h>
50 #include <sys/callout.h>
51 #include <sys/filio.h>
53 #include <sys/sysctl.h>
54 #include <nlm/nlm_prot.h>
57 FEATURE(nfsd, "NFSv4 server");
59 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
60 extern int nfsrv_useacl;
61 extern int newnfs_numnfsd;
62 extern struct mount nfsv4root_mnt;
63 extern struct nfsrv_stablefirst nfsrv_stablefirst;
64 extern SVCPOOL *nfsrvd_pool;
65 extern struct nfsv4lock nfsd_suspend_lock;
66 extern struct nfsclienthashhead *nfsclienthash;
67 extern struct nfslockhashhead *nfslockhash;
68 extern struct nfssessionhash *nfssessionhash;
69 extern int nfsrv_sessionhashsize;
70 extern struct nfsstatsv1 nfsstatsv1;
71 extern struct nfslayouthash *nfslayouthash;
72 extern int nfsrv_layouthashsize;
73 extern struct mtx nfsrv_dslock_mtx;
74 extern int nfs_pnfsiothreads;
75 extern struct nfsdontlisthead nfsrv_dontlisthead;
76 extern volatile int nfsrv_dontlistlen;
77 extern volatile int nfsrv_devidcnt;
78 extern int nfsrv_maxpnfsmirror;
79 extern uint32_t nfs_srvmaxio;
80 extern int nfs_bufpackets;
81 extern u_long sb_max_adj;
82 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt;
85 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE];
86 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE];
87 struct mtx nfsrc_udpmtx;
88 struct mtx nfs_v4root_mutex;
89 struct mtx nfsrv_dontlistlock_mtx;
90 struct mtx nfsrv_recalllock_mtx;
91 struct nfsrvfh nfs_rootfh, nfs_pubfh;
92 int nfs_pubfhset = 0, nfs_rootfhset = 0;
93 struct proc *nfsd_master_proc = NULL;
94 int nfsd_debuglevel = 0;
95 static pid_t nfsd_master_pid = (pid_t)-1;
96 static char nfsd_master_comm[MAXCOMLEN + 1];
97 static struct timeval nfsd_master_start;
98 static uint32_t nfsv4_sysid = 0;
99 static fhandle_t zerofh;
100 struct callout nfsd_callout;
102 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
104 static void nfsvno_updateds(struct vnode *, struct ucred *, struct thread *);
106 int nfsrv_enable_crossmntpt = 1;
107 static int nfs_commit_blks;
108 static int nfs_commit_miss;
109 extern int nfsrv_issuedelegs;
110 extern int nfsrv_dolocallocks;
111 extern int nfsd_enable_stringtouid;
112 extern struct nfsdevicehead nfsrv_devidhead;
114 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
116 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
117 struct mbuf **, struct iovec **);
118 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
120 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
122 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
123 int *, char *, fhandle_t *);
124 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
126 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
127 struct thread *, int, struct mbuf **, char *, struct mbuf **,
128 struct nfsvattr *, struct acl *, off_t *, int, bool *);
129 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
130 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
131 NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
132 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
133 NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
135 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
136 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
137 static int nfsrv_deallocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
138 NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
139 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
140 struct vnode *, struct nfsmount **, int, struct acl *, int *);
141 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
142 struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
143 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
144 struct vnode *, struct nfsmount *, struct nfsvattr *);
145 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
146 NFSPROC_T *, struct nfsmount *);
147 static int nfsrv_putfhname(fhandle_t *, char *);
148 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
149 struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
150 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
151 struct vnode *, NFSPROC_T *);
152 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
153 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
155 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
157 int nfs_pnfsio(task_fn_t *, void *);
159 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
161 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
162 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
163 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
165 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
167 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
168 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
169 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW,
170 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files");
171 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
172 0, "Debug level for NFS server");
173 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW,
174 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names");
175 static int nfsrv_pnfsgetdsattr = 1;
176 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
177 &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
180 * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
182 * The dsN subdirectories for the increased values must have been created
183 * on all DS servers before this increase is done.
185 u_int nfsrv_dsdirsize = 20;
187 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
189 int error, newdsdirsize;
191 newdsdirsize = nfsrv_dsdirsize;
192 error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
193 if (error != 0 || req->newptr == NULL)
195 if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
198 nfsrv_dsdirsize = newdsdirsize;
201 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
202 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
203 sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
206 * nfs_srvmaxio can only be increased and only when the nfsd threads are
207 * not running. The setting must be a power of 2, with the current limit of
211 sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)
217 newsrvmaxio = nfs_srvmaxio;
218 error = sysctl_handle_int(oidp, &newsrvmaxio, 0, req);
219 if (error != 0 || req->newptr == NULL)
221 if (newsrvmaxio == nfs_srvmaxio)
223 if (newsrvmaxio < nfs_srvmaxio) {
224 printf("nfsd: vfs.nfsd.srvmaxio can only be increased\n");
227 if (newsrvmaxio > 1048576) {
228 printf("nfsd: vfs.nfsd.srvmaxio cannot be > 1Mbyte\n");
231 if ((newsrvmaxio & (newsrvmaxio - 1)) != 0) {
232 printf("nfsd: vfs.nfsd.srvmaxio must be a power of 2\n");
237 * Check that kern.ipc.maxsockbuf is large enough for
238 * newsrviomax, given the setting of vfs.nfs.bufpackets.
240 if ((newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets >
243 * Suggest vfs.nfs.bufpackets * maximum RPC message for
246 tval = (newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets;
249 * Convert suggested sb_max_adj value to a suggested
250 * sb_max value, which is what is set via kern.ipc.maxsockbuf.
251 * Perform the inverse calculation of (from uipc_sockbuf.c):
252 * sb_max_adj = (u_quad_t)sb_max * MCLBYTES /
253 * (MSIZE + MCLBYTES);
254 * XXX If the calculation of sb_max_adj from sb_max changes,
255 * this calculation must be changed as well.
257 tval *= (MSIZE + MCLBYTES); /* Brackets for readability. */
258 tval += MCLBYTES - 1; /* Round up divide. */
260 printf("nfsd: set kern.ipc.maxsockbuf to a minimum of "
261 "%ju to support %ubyte NFS I/O\n", (uintmax_t)tval,
267 if (newnfs_numnfsd != 0) {
269 printf("nfsd: cannot set vfs.nfsd.srvmaxio when nfsd "
270 "threads are running\n");
275 nfs_srvmaxio = newsrvmaxio;
279 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, srvmaxio,
280 CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
281 sysctl_srvmaxio, "IU", "Maximum I/O size in bytes");
283 #define MAX_REORDERED_RPC 16
284 #define NUM_HEURISTIC 1031
285 #define NHUSE_INIT 64
287 #define NHUSE_MAX 2048
289 static struct nfsheur {
290 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */
291 off_t nh_nextoff; /* next offset for sequential detection */
292 int nh_use; /* use count for selection */
293 int nh_seqcount; /* heuristic */
294 } nfsheur[NUM_HEURISTIC];
297 * Heuristic to detect sequential operation.
299 static struct nfsheur *
300 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
305 /* Locate best candidate. */
307 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
310 if (nfsheur[hi].nh_vp == vp) {
314 if (nfsheur[hi].nh_use > 0)
315 --nfsheur[hi].nh_use;
316 hi = (hi + 1) % NUM_HEURISTIC;
317 if (nfsheur[hi].nh_use < nh->nh_use)
321 /* Initialize hint if this is a new file. */
322 if (nh->nh_vp != vp) {
324 nh->nh_nextoff = uio->uio_offset;
325 nh->nh_use = NHUSE_INIT;
326 if (uio->uio_offset == 0)
332 /* Calculate heuristic. */
333 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
334 uio->uio_offset == nh->nh_nextoff) {
335 /* See comments in vfs_vnops.c:sequential_heuristic(). */
336 nh->nh_seqcount += howmany(uio->uio_resid, 16384);
337 if (nh->nh_seqcount > IO_SEQMAX)
338 nh->nh_seqcount = IO_SEQMAX;
339 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
340 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
341 /* Probably a reordered RPC, leave seqcount alone. */
342 } else if (nh->nh_seqcount > 1) {
343 nh->nh_seqcount /= 2;
347 nh->nh_use += NHUSE_INC;
348 if (nh->nh_use > NHUSE_MAX)
349 nh->nh_use = NHUSE_MAX;
354 * Get attributes into nfsvattr structure.
357 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
358 struct nfsrv_descript *nd, struct thread *p, int vpislocked,
359 nfsattrbit_t *attrbitp)
361 int error, gotattr, lockedit = 0;
364 if (vpislocked == 0) {
366 * When vpislocked == 0, the vnode is either exclusively
367 * locked by this thread or not locked by this thread.
368 * As such, shared lock it, if not exclusively locked.
370 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
372 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
377 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
378 * attributes, as required.
379 * This needs to be done for regular files if:
380 * - non-NFSv4 RPCs or
381 * - when attrbitp == NULL or
382 * - an NFSv4 RPC with any of the above attributes in attrbitp.
383 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
384 * these attributes. nfsrv_proxyds() will return an error if the
385 * server is not a pNFS one.
388 if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
389 (nd->nd_flag & ND_NFSV4) == 0 ||
390 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
391 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
392 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
393 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
394 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
395 error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
396 NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
402 error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
407 * If we got the Change, Size and Modify Time from the DS,
411 nvap->na_atime = na.na_atime;
412 nvap->na_mtime = na.na_mtime;
413 nvap->na_filerev = na.na_filerev;
414 nvap->na_size = na.na_size;
415 nvap->na_bytes = na.na_bytes;
417 NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
418 error, (uintmax_t)na.na_filerev);
425 * Get a file handle for a vnode.
428 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
432 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
433 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
434 error = VOP_VPTOFH(vp, &fhp->fh_fid);
441 * Perform access checking for vnodes obtained from file handles that would
442 * refer to files already opened by a Unix client. You cannot just use
443 * vn_writechk() and VOP_ACCESSX() for two reasons.
444 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
446 * 2 - The owner is to be given access irrespective of mode bits for some
447 * operations, so that processes that chmod after opening a file don't
451 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
452 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
453 u_int32_t *supportedtypep)
456 int error = 0, getret = 0;
458 if (vpislocked == 0) {
459 if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
464 if (accmode & VWRITE) {
465 /* Just vn_writechk() changed to check rdonly */
467 * Disallow write attempts on read-only file systems;
468 * unless the file is a socket or a block or character
469 * device resident on the file system.
471 if (NFSVNO_EXRDONLY(exp) ||
472 (vp->v_mount->mnt_flag & MNT_RDONLY)) {
473 switch (vp->v_type) {
483 * If there's shared text associated with
484 * the inode, try to free it up once. If
485 * we fail, we can't allow writing.
487 if (VOP_IS_TEXT(vp) && error == 0)
497 * Should the override still be applied when ACLs are enabled?
499 error = VOP_ACCESSX(vp, accmode, cred, p);
500 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
502 * Try again with VEXPLICIT_DENY, to see if the test for
503 * deletion is supported.
505 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
507 if (vp->v_type == VDIR) {
508 accmode &= ~(VDELETE | VDELETE_CHILD);
510 error = VOP_ACCESSX(vp, accmode, cred, p);
511 } else if (supportedtypep != NULL) {
512 *supportedtypep &= ~NFSACCESS_DELETE;
518 * Allow certain operations for the owner (reads and writes
519 * on files that are already open).
521 if (override != NFSACCCHK_NOOVERRIDE &&
522 (error == EPERM || error == EACCES)) {
523 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
525 else if (override & NFSACCCHK_ALLOWOWNER) {
526 getret = VOP_GETATTR(vp, &vattr, cred);
527 if (getret == 0 && cred->cr_uid == vattr.va_uid)
540 * Set attribute(s) vnop.
543 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
544 struct thread *p, struct nfsexstuff *exp)
546 u_quad_t savsize = 0;
551 * If this is an exported file system and a pNFS service is running,
552 * don't VOP_SETATTR() of size for the MDS file system.
556 if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
557 nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
558 nvap->na_vattr.va_size > 0) {
559 savsize = nvap->na_vattr.va_size;
560 nvap->na_vattr.va_size = VNOVAL;
561 if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
562 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
563 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
564 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
565 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
571 error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
573 nvap->na_vattr.va_size = savsize;
574 if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
575 nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
576 nvap->na_vattr.va_size != VNOVAL ||
577 nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
578 nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
579 nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
580 /* Never modify birthtime on a DS file. */
581 savbtime = nvap->na_vattr.va_birthtime.tv_sec;
582 nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
583 /* For a pNFS server, set the attributes on the DS file. */
584 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
585 NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
586 nvap->na_vattr.va_birthtime.tv_sec = savbtime;
595 * Set up nameidata for a lookup() call and do it.
598 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
599 struct vnode *dp, int islocked, struct nfsexstuff *exp,
600 struct vnode **retdirp)
602 struct componentname *cnp = &ndp->ni_cnd;
606 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
611 cnp->cn_nameptr = cnp->cn_pnbuf;
614 * Extract and set starting directory.
616 if (dp->v_type != VDIR) {
621 nfsvno_relpathbuf(ndp);
629 if (NFSVNO_EXRDONLY(exp))
630 cnp->cn_flags |= RDONLY;
631 ndp->ni_segflg = UIO_SYSSPACE;
633 if (nd->nd_flag & ND_PUBLOOKUP) {
635 if (cnp->cn_pnbuf[0] == '/') {
638 * Check for degenerate pathnames here, since lookup()
641 for (i = 1; i < ndp->ni_pathlen; i++)
642 if (cnp->cn_pnbuf[i] != '/')
644 if (i == ndp->ni_pathlen) {
645 error = NFSERR_ACCES;
651 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
652 (nd->nd_flag & ND_NFSV4) == 0) {
654 * Only cross mount points for NFSv4 when doing a
655 * mount while traversing the file system above
656 * the mount point, unless nfsrv_enable_crossmntpt is set.
658 cnp->cn_flags |= NOCROSSMOUNT;
662 * Initialize for scan, set ni_startdir and bump ref on dp again
663 * because lookup() will dereference ni_startdir.
666 ndp->ni_startdir = dp;
667 ndp->ni_rootdir = rootvnode;
668 ndp->ni_topdir = NULL;
671 cnp->cn_flags |= LOCKLEAF;
673 cnp->cn_nameptr = cnp->cn_pnbuf;
675 * Call lookup() to do the real work. If an error occurs,
676 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
677 * we do not have to dereference anything before returning.
678 * In either case ni_startdir will be dereferenced and NULLed
681 error = vfs_lookup(ndp);
686 * Check for encountering a symbolic link. Trivial
687 * termination occurs if no symlink encountered.
689 if ((cnp->cn_flags & ISSYMLINK) == 0) {
690 if (ndp->ni_vp && !lockleaf)
691 NFSVOPUNLOCK(ndp->ni_vp);
698 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
699 NFSVOPUNLOCK(ndp->ni_dvp);
700 if (!(nd->nd_flag & ND_PUBLOOKUP)) {
705 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
709 if (ndp->ni_pathlen > 1)
710 cp = uma_zalloc(namei_zone, M_WAITOK);
714 aiov.iov_len = MAXPATHLEN;
715 auio.uio_iov = &aiov;
718 auio.uio_rw = UIO_READ;
719 auio.uio_segflg = UIO_SYSSPACE;
721 auio.uio_resid = MAXPATHLEN;
722 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
725 if (ndp->ni_pathlen > 1)
726 uma_zfree(namei_zone, cp);
732 linklen = MAXPATHLEN - auio.uio_resid;
737 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
738 error = ENAMETOOLONG;
743 * Adjust or replace path
745 if (ndp->ni_pathlen > 1) {
746 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
747 uma_zfree(namei_zone, cnp->cn_pnbuf);
750 cnp->cn_pnbuf[linklen] = '\0';
751 ndp->ni_pathlen += linklen;
754 * Cleanup refs for next loop and check if root directory
755 * should replace current directory. Normally ni_dvp
756 * becomes the new base directory and is cleaned up when
757 * we loop. Explicitly null pointers after invalidation
758 * to clarify operation.
763 if (cnp->cn_pnbuf[0] == '/') {
765 ndp->ni_dvp = ndp->ni_rootdir;
768 ndp->ni_startdir = ndp->ni_dvp;
772 cnp->cn_flags &= ~LOCKLEAF;
776 nfsvno_relpathbuf(ndp);
779 ndp->ni_startdir = NULL;
780 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
785 NFSEXITCODE2(error, nd);
790 * Set up a pathname buffer and return a pointer to it and, optionally
791 * set a hash pointer.
794 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
796 struct componentname *cnp = &ndp->ni_cnd;
798 cnp->cn_flags |= (NOMACCHECK);
799 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
802 *bufpp = cnp->cn_pnbuf;
806 * Release the above path buffer, if not released by nfsvno_namei().
809 nfsvno_relpathbuf(struct nameidata *ndp)
812 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
813 ndp->ni_cnd.cn_pnbuf = NULL;
817 * Readlink vnode op into an mbuf list.
820 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
821 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
824 struct uio io, *uiop = &io;
825 struct mbuf *mp, *mp3;
826 int len, tlen, error = 0;
828 len = NFS_MAXPATHLEN;
830 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
833 uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
835 uiop->uio_offset = 0;
836 uiop->uio_resid = len;
837 uiop->uio_rw = UIO_READ;
838 uiop->uio_segflg = UIO_SYSSPACE;
840 error = VOP_READLINK(vp, uiop, cred);
847 if (uiop->uio_resid > 0) {
848 len -= uiop->uio_resid;
849 tlen = NFSM_RNDUP(len);
853 } else if (tlen != NFS_MAXPATHLEN || tlen != len)
854 mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
867 * Create an mbuf chain and an associated iovec that can be used to Read
868 * or Getextattr of data.
869 * Upon success, return pointers to the first and last mbufs in the chain
870 * plus the malloc'd iovec and its iovlen.
873 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
876 struct mbuf *m, *m2 = NULL, *m3;
883 * Generate the mbuf list with the uio_iov ref. to it.
890 siz = min(M_TRAILINGSPACE(m), left);
899 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
905 panic("nfsrv_createiovec iov");
906 siz = min(M_TRAILINGSPACE(m), left);
908 iv->iov_base = mtod(m, caddr_t) + m->m_len;
923 * Create an mbuf chain and an associated iovec that can be used to Read
924 * or Getextattr of data.
925 * Upon success, return pointers to the first and last mbufs in the chain
926 * plus the malloc'd iovec and its iovlen.
927 * Same as above, but creates ext_pgs mbuf(s).
930 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
931 struct mbuf **mpendp, struct iovec **ivp)
933 struct mbuf *m, *m2 = NULL, *m3;
935 int i, left, pgno, siz;
940 * Generate the mbuf list with the uio_iov ref. to it.
944 siz = min(left, maxextsiz);
945 m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
954 *ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
961 panic("nfsvno_createiovec_extpgs iov");
962 siz = min(PAGE_SIZE, left);
964 iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
967 if (pgno == m->m_epg_npgs - 1)
968 m->m_epg_last_len = siz;
974 if (pgno == m->m_epg_npgs && left > 0) {
977 panic("nfsvno_createiovec_extpgs iov");
987 * Read vnode op call into mbuf list.
990 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
991 int maxextsiz, struct thread *p, struct mbuf **mpp,
992 struct mbuf **mpendp)
996 int error = 0, len, tlen, ioflag = 0;
998 struct uio io, *uiop = &io;
1002 * Attempt to read from a DS file. A return of ENOENT implies
1003 * there is no DS file to read.
1005 error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
1006 NULL, mpendp, NULL, NULL, NULL, 0, NULL);
1007 if (error != ENOENT)
1010 len = NFSM_RNDUP(cnt);
1012 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
1015 uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
1017 uiop->uio_offset = off;
1018 uiop->uio_resid = len;
1019 uiop->uio_rw = UIO_READ;
1020 uiop->uio_segflg = UIO_SYSSPACE;
1021 uiop->uio_td = NULL;
1022 nh = nfsrv_sequential_heuristic(uiop, vp);
1023 ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
1024 /* XXX KDM make this more systematic? */
1025 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid;
1026 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
1033 nh->nh_nextoff = uiop->uio_offset;
1034 tlen = len - uiop->uio_resid;
1035 cnt = cnt < tlen ? cnt : tlen;
1036 tlen = NFSM_RNDUP(cnt);
1040 } else if (len != tlen || tlen != cnt)
1041 m = nfsrv_adj(m3, len - tlen, tlen - cnt);
1051 * Create the iovec for the mbuf chain passed in as an argument.
1052 * The "cp" argument is where the data starts within the first mbuf in
1053 * the chain. It returns the iovec and the iovcnt.
1056 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
1064 * Loop through the mbuf chain, counting how many mbufs are a
1065 * part of this write operation, so the iovec size is known.
1070 i = mtod(mp, caddr_t) + mp->m_len - cp;
1084 /* Now, create the iovec. */
1086 *ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1089 i = mtod(mp, caddr_t) + mp->m_len - cp;
1093 panic("nfsrv_createiovecw");
1104 cp = mtod(mp, caddr_t);
1111 * Write vnode op from an mbuf list.
1114 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1115 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1118 int cnt, ioflags, error;
1119 struct uio io, *uiop = &io;
1123 * Attempt to write to a DS file. A return of ENOENT implies
1124 * there is no DS file to write.
1126 error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1127 &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1128 if (error != ENOENT) {
1129 *stable = NFSWRITE_FILESYNC;
1133 if (*stable == NFSWRITE_UNSTABLE)
1134 ioflags = IO_NODELOCKED;
1136 ioflags = (IO_SYNC | IO_NODELOCKED);
1137 error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1141 uiop->uio_iovcnt = cnt;
1142 uiop->uio_resid = retlen;
1143 uiop->uio_rw = UIO_WRITE;
1144 uiop->uio_segflg = UIO_SYSSPACE;
1145 NFSUIOPROC(uiop, p);
1146 uiop->uio_offset = off;
1147 nh = nfsrv_sequential_heuristic(uiop, vp);
1148 ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1149 /* XXX KDM make this more systematic? */
1150 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1151 error = VOP_WRITE(vp, uiop, ioflags, cred);
1153 nh->nh_nextoff = uiop->uio_offset;
1161 * Common code for creating a regular file (plus special files for V2).
1164 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1165 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1166 int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1170 struct thread *p = curthread;
1172 error = nd->nd_repstat;
1173 if (!error && ndp->ni_vp == NULL) {
1174 if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1175 error = VOP_CREATE(ndp->ni_dvp,
1176 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1177 /* For a pNFS server, create the data file on a DS. */
1178 if (error == 0 && nvap->na_type == VREG) {
1180 * Create a data file on a DS for a pNFS server.
1181 * This function just returns if not
1182 * running a pNFS DS or the creation fails.
1184 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1187 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1189 nfsvno_relpathbuf(ndp);
1191 if (*exclusive_flagp) {
1192 *exclusive_flagp = 0;
1193 NFSVNO_ATTRINIT(nvap);
1194 nvap->na_atime.tv_sec = cverf[0];
1195 nvap->na_atime.tv_nsec = cverf[1];
1196 error = VOP_SETATTR(ndp->ni_vp,
1197 &nvap->na_vattr, nd->nd_cred);
1201 error = NFSERR_NOTSUPP;
1206 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1207 * (This implies, just get out on an error.)
1209 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1210 nvap->na_type == VFIFO) {
1211 if (nvap->na_type == VCHR && rdev == 0xffffffff)
1212 nvap->na_type = VFIFO;
1213 if (nvap->na_type != VFIFO &&
1214 (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1215 nfsvno_relpathbuf(ndp);
1219 nvap->na_rdev = rdev;
1220 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1221 &ndp->ni_cnd, &nvap->na_vattr);
1222 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1224 nfsvno_relpathbuf(ndp);
1228 nfsvno_relpathbuf(ndp);
1236 * Handle cases where error is already set and/or
1238 * 1 - clean up the lookup
1239 * 2 - iff !error and na_size set, truncate it
1241 nfsvno_relpathbuf(ndp);
1243 if (ndp->ni_dvp == *vpp)
1247 if (!error && nvap->na_size != VNOVAL) {
1248 error = nfsvno_accchk(*vpp, VWRITE,
1249 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1250 NFSACCCHK_VPISLOCKED, NULL);
1252 tempsize = nvap->na_size;
1253 NFSVNO_ATTRINIT(nvap);
1254 nvap->na_size = tempsize;
1255 error = nfsvno_setattr(*vpp, nvap,
1256 nd->nd_cred, p, exp);
1269 * Do a mknod vnode op.
1272 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1278 vtyp = nvap->na_type;
1280 * Iff doesn't exist, create it.
1283 nfsvno_relpathbuf(ndp);
1289 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1290 nfsvno_relpathbuf(ndp);
1292 error = NFSERR_BADTYPE;
1295 if (vtyp == VSOCK) {
1296 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1297 &ndp->ni_cnd, &nvap->na_vattr);
1298 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1300 nfsvno_relpathbuf(ndp);
1302 if (nvap->na_type != VFIFO &&
1303 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1304 nfsvno_relpathbuf(ndp);
1308 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1309 &ndp->ni_cnd, &nvap->na_vattr);
1310 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1312 nfsvno_relpathbuf(ndp);
1314 * Since VOP_MKNOD returns the ni_vp, I can't
1315 * see any reason to do the lookup.
1328 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1329 struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1333 if (ndp->ni_vp != NULL) {
1334 if (ndp->ni_dvp == ndp->ni_vp)
1339 nfsvno_relpathbuf(ndp);
1343 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1345 VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL, false);
1346 nfsvno_relpathbuf(ndp);
1357 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1358 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1359 struct nfsexstuff *exp)
1364 nfsvno_relpathbuf(ndp);
1365 if (ndp->ni_dvp == ndp->ni_vp)
1374 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1375 &nvap->na_vattr, pathcp);
1377 * Although FreeBSD still had the lookup code in
1378 * it for 7/current, there doesn't seem to be any
1379 * point, since VOP_SYMLINK() returns the ni_vp.
1380 * Just vput it for v2.
1382 VOP_VPUT_PAIR(ndp->ni_dvp, &ndp->ni_vp, !not_v2 && error == 0);
1383 nfsvno_relpathbuf(ndp);
1391 * Parse symbolic link arguments.
1392 * This function has an ugly side effect. It will malloc() an area for
1393 * the symlink and set iov_base to point to it, only if it succeeds.
1394 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1398 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1399 struct thread *p, char **pathcpp, int *lenp)
1402 char *pathcp = NULL;
1404 struct nfsv2_sattr *sp;
1408 if ((nd->nd_flag & ND_NFSV3) &&
1409 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1411 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1412 len = fxdr_unsigned(int, *tl);
1413 if (len > NFS_MAXPATHLEN || len <= 0) {
1417 pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1418 error = nfsrv_mtostr(nd, pathcp, len);
1421 if (nd->nd_flag & ND_NFSV2) {
1422 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1423 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1427 NFSEXITCODE2(0, nd);
1431 free(pathcp, M_TEMP);
1432 NFSEXITCODE2(error, nd);
1437 * Remove a non-directory object.
1440 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1441 struct thread *p, struct nfsexstuff *exp)
1443 struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1444 int error = 0, mirrorcnt;
1445 char fname[PNFS_FILENAME_LEN + 1];
1450 if (vp->v_type == VDIR)
1451 error = NFSERR_ISDIR;
1453 error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1456 nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1458 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1459 if (error == 0 && dsdvp[0] != NULL)
1460 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1461 if (ndp->ni_dvp == vp)
1466 nfsvno_relpathbuf(ndp);
1472 * Remove a directory.
1475 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1476 struct thread *p, struct nfsexstuff *exp)
1482 if (vp->v_type != VDIR) {
1487 * No rmdir "." please.
1489 if (ndp->ni_dvp == vp) {
1494 * The root of a mounted filesystem cannot be deleted.
1496 if (vp->v_vflag & VV_ROOT)
1500 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1501 if (ndp->ni_dvp == vp)
1506 nfsvno_relpathbuf(ndp);
1515 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1516 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1518 struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1519 int error = 0, mirrorcnt;
1520 char fname[PNFS_FILENAME_LEN + 1];
1524 fvp = fromndp->ni_vp;
1526 vrele(fromndp->ni_dvp);
1531 tdvp = tondp->ni_dvp;
1534 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1535 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1537 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1538 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1541 if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1542 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1547 * A rename to '.' or '..' results in a prematurely
1548 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1551 if ((tondp->ni_cnd.cn_namelen == 1 &&
1552 tondp->ni_cnd.cn_nameptr[0] == '.') ||
1553 (tondp->ni_cnd.cn_namelen == 2 &&
1554 tondp->ni_cnd.cn_nameptr[0] == '.' &&
1555 tondp->ni_cnd.cn_nameptr[1] == '.')) {
1560 if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1561 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1564 if (fvp->v_mount != tdvp->v_mount) {
1565 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1569 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1574 * If source and destination are the same, there is nothing to
1575 * do. Set error to -1 to indicate this.
1580 if (ndflag & ND_NFSV4) {
1581 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1582 error = nfsrv_checkremove(fvp, 0, NULL,
1583 (nfsquad_t)((u_quad_t)0), p);
1588 error = nfsrv_checkremove(tvp, 1, NULL,
1589 (nfsquad_t)((u_quad_t)0), p);
1592 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1593 * that the NFSv4 client won't be confused by the rename.
1594 * Since nfsd_recalldelegation() can only be called on an
1595 * unlocked vnode at this point and fvp is the file that will
1596 * still exist after the rename, just do fvp.
1598 nfsd_recalldelegation(fvp, p);
1600 if (error == 0 && tvp != NULL) {
1601 nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1602 NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1603 " dsdvp=%p\n", dsdvp[0]);
1607 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1608 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1617 vrele(fromndp->ni_dvp);
1624 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1625 * if the rename succeeded, the DS file for the tvp needs to be
1628 if (error == 0 && dsdvp[0] != NULL) {
1629 nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1630 NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1633 nfsvno_relpathbuf(tondp);
1635 nfsvno_relpathbuf(fromndp);
1644 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred,
1645 struct thread *p, struct nfsexstuff *exp)
1655 if (vp->v_mount != xp->v_mount)
1659 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1660 if (!VN_IS_DOOMED(vp))
1661 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1664 if (ndp->ni_dvp == vp) {
1669 VOP_VPUT_PAIR(ndp->ni_dvp, &vp, true);
1672 if (ndp->ni_dvp == ndp->ni_vp)
1679 nfsvno_relpathbuf(ndp);
1685 * Do the fsync() appropriate for the commit.
1688 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1694 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1695 * file is done. At this time VOP_FSYNC does not accept offset and
1696 * byte count parameters so call VOP_FSYNC the whole file for now.
1697 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1698 * File systems that do not use the buffer cache (as indicated
1699 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1701 if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1702 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1704 * Give up and do the whole thing
1706 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1707 VM_OBJECT_WLOCK(vp->v_object);
1708 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC);
1709 VM_OBJECT_WUNLOCK(vp->v_object);
1711 error = VOP_FSYNC(vp, MNT_WAIT, td);
1714 * Locate and synchronously write any buffers that fall
1715 * into the requested range. Note: we are assuming that
1716 * f_iosize is a power of 2.
1718 int iosize = vp->v_mount->mnt_stat.f_iosize;
1719 int iomask = iosize - 1;
1724 * Align to iosize boundary, super-align to page boundary.
1727 cnt += off & iomask;
1728 off &= ~(u_quad_t)iomask;
1730 if (off & PAGE_MASK) {
1731 cnt += off & PAGE_MASK;
1732 off &= ~(u_quad_t)PAGE_MASK;
1734 lblkno = off / iosize;
1736 if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1737 VM_OBJECT_WLOCK(vp->v_object);
1738 vm_object_page_clean(vp->v_object, off, off + cnt,
1740 VM_OBJECT_WUNLOCK(vp->v_object);
1749 * If we have a buffer and it is marked B_DELWRI we
1750 * have to lock and write it. Otherwise the prior
1751 * write is assumed to have already been committed.
1753 * gbincore() can return invalid buffers now so we
1754 * have to check that bit as well (though B_DELWRI
1755 * should not be set if B_INVAL is set there could be
1756 * a race here since we haven't locked the buffer).
1758 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1759 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1760 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1762 continue; /* retry */
1764 if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1767 bp->b_flags &= ~B_ASYNC;
1790 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1796 if (nfsrv_devidcnt > 0) {
1797 /* For a pNFS service, get the DS numbers. */
1798 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1799 error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1805 error = VFS_STATFS(vp->v_mount, sf);
1808 sf->f_blocks = tsf->f_blocks;
1809 sf->f_bavail = tsf->f_bavail;
1810 sf->f_bfree = tsf->f_bfree;
1811 sf->f_bsize = tsf->f_bsize;
1814 * Since NFS handles these values as unsigned on the
1815 * wire, there is no way to represent negative values,
1816 * so set them to 0. Without this, they will appear
1817 * to be very large positive values for clients like
1820 if (sf->f_bavail < 0)
1822 if (sf->f_ffree < 0)
1831 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1832 * must handle nfsrv_opencheck() calls after any other access checks.
1835 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1836 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1837 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1838 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, bool done_namei,
1839 struct nfsexstuff *exp, struct vnode **vpp)
1841 struct vnode *vp = NULL;
1843 struct nfsexstuff nes;
1844 struct thread *p = curthread;
1846 if (ndp->ni_vp == NULL)
1847 nd->nd_repstat = nfsrv_opencheck(clientid,
1848 stateidp, stp, NULL, nd, p, nd->nd_repstat);
1849 if (!nd->nd_repstat) {
1850 if (ndp->ni_vp == NULL) {
1851 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1852 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1853 /* For a pNFS server, create the data file on a DS. */
1854 if (nd->nd_repstat == 0) {
1856 * Create a data file on a DS for a pNFS server.
1857 * This function just returns if not
1858 * running a pNFS DS or the creation fails.
1860 nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1863 VOP_VPUT_PAIR(ndp->ni_dvp, nd->nd_repstat == 0 ?
1864 &ndp->ni_vp : NULL, false);
1865 nfsvno_relpathbuf(ndp);
1866 if (!nd->nd_repstat) {
1867 if (*exclusive_flagp) {
1868 *exclusive_flagp = 0;
1869 NFSVNO_ATTRINIT(nvap);
1870 nvap->na_atime.tv_sec = cverf[0];
1871 nvap->na_atime.tv_nsec = cverf[1];
1872 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1873 &nvap->na_vattr, cred);
1874 if (nd->nd_repstat != 0) {
1877 nd->nd_repstat = NFSERR_NOTSUPP;
1879 NFSSETBIT_ATTRBIT(attrbitp,
1880 NFSATTRBIT_TIMEACCESS);
1882 nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1883 aclp, p, attrbitp, exp);
1888 nfsvno_relpathbuf(ndp);
1890 if (create == NFSV4OPEN_CREATE) {
1891 if (ndp->ni_dvp == vp)
1896 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1897 if (ndp->ni_cnd.cn_flags & RDONLY)
1898 NFSVNO_SETEXRDONLY(&nes);
1900 NFSVNO_EXINIT(&nes);
1901 nd->nd_repstat = nfsvno_accchk(vp,
1902 VWRITE, cred, &nes, p,
1903 NFSACCCHK_NOOVERRIDE,
1904 NFSACCCHK_VPISLOCKED, NULL);
1905 nd->nd_repstat = nfsrv_opencheck(clientid,
1906 stateidp, stp, vp, nd, p, nd->nd_repstat);
1907 if (!nd->nd_repstat) {
1908 tempsize = nvap->na_size;
1909 NFSVNO_ATTRINIT(nvap);
1910 nvap->na_size = tempsize;
1911 nd->nd_repstat = nfsvno_setattr(vp,
1912 nvap, cred, p, exp);
1914 } else if (vp->v_type == VREG) {
1915 nd->nd_repstat = nfsrv_opencheck(clientid,
1916 stateidp, stp, vp, nd, p, nd->nd_repstat);
1920 nfsvno_relpathbuf(ndp);
1921 if (done_namei && create == NFSV4OPEN_CREATE) {
1922 if (ndp->ni_dvp == ndp->ni_vp)
1932 NFSEXITCODE2(0, nd);
1936 * Updates the file rev and sets the mtime and ctime
1937 * to the current clock time, returning the va_filerev and va_Xtime
1939 * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1942 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
1943 struct nfsrv_descript *nd, struct thread *p)
1948 vfs_timestamp(&va.va_mtime);
1949 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
1950 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
1951 if (VN_IS_DOOMED(vp))
1954 (void) VOP_SETATTR(vp, &va, nd->nd_cred);
1955 (void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
1960 * Glue routine to nfsv4_fillattr().
1963 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
1964 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
1965 struct ucred *cred, struct thread *p, int isdgram, int reterr,
1966 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
1972 if (nfsrv_devidcnt > 0 &&
1973 (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
1974 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
1975 NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
1976 sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
1977 error = nfsrv_pnfsstatfs(sf, mp);
1983 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
1984 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
1985 mounted_on_fileno, sf);
1987 NFSEXITCODE2(0, nd);
1991 /* Since the Readdir vnode ops vary, put the entire functions in here. */
1993 * nfs readdir service
1994 * - mallocs what it thinks is enough to read
1995 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
1996 * - calls VOP_READDIR()
1997 * - loops around building the reply
1998 * if the output generated exceeds count break out of loop
1999 * The NFSM_CLGET macro is used here so that the reply will be packed
2000 * tightly in mbuf clusters.
2001 * - it trims out records with d_fileno == 0
2002 * this doesn't matter for Unix clients, but they might confuse clients
2004 * - it trims out records with d_type == DT_WHT
2005 * these cannot be seen through NFS (unless we extend the protocol)
2006 * The alternate call nfsrvd_readdirplus() does lookups as well.
2007 * PS: The NFS protocol spec. does not clarify what the "count" byte
2008 * argument is a count of.. just name strings and file id's or the
2009 * entire reply rpc or ...
2010 * I tried just file name and id sizes and it confused the Sun client,
2011 * so I am using the full rpc size now. The "paranoia.." comment refers
2012 * to including the status longwords that are not a part of the dir.
2013 * "entry" structures, but are in the rpc.
2016 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2017 struct vnode *vp, struct nfsexstuff *exp)
2022 char *cpos, *cend, *rbuf;
2024 int nlen, error = 0, getret = 1;
2025 int siz, cnt, fullsiz, eofflag, ncookies;
2026 u_int64_t off, toff, verf __unused;
2027 uint64_t *cookies = NULL, *cookiep;
2031 struct thread *p = curthread;
2033 if (nd->nd_repstat) {
2034 nfsrv_postopattr(nd, getret, &at);
2037 if (nd->nd_flag & ND_NFSV2) {
2038 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2039 off = fxdr_unsigned(u_quad_t, *tl++);
2041 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2042 off = fxdr_hyper(tl);
2044 verf = fxdr_hyper(tl);
2048 cnt = fxdr_unsigned(int, *tl);
2049 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2050 cnt = NFS_SRVMAXDATA(nd);
2051 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2053 if (nd->nd_flag & ND_NFSV3) {
2054 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2058 * va_filerev is not sufficient as a cookie verifier,
2059 * since it is not supposed to change when entries are
2060 * removed/added unless that offset cookies returned to
2061 * the client are no longer valid.
2063 if (!nd->nd_repstat && toff && verf != at.na_filerev)
2064 nd->nd_repstat = NFSERR_BAD_COOKIE;
2067 if (!nd->nd_repstat && vp->v_type != VDIR)
2068 nd->nd_repstat = NFSERR_NOTDIR;
2069 if (nd->nd_repstat == 0 && cnt == 0) {
2070 if (nd->nd_flag & ND_NFSV2)
2071 /* NFSv2 does not have NFSERR_TOOSMALL */
2072 nd->nd_repstat = EPERM;
2074 nd->nd_repstat = NFSERR_TOOSMALL;
2076 if (!nd->nd_repstat)
2077 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2078 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2079 NFSACCCHK_VPISLOCKED, NULL);
2080 if (nd->nd_repstat) {
2082 if (nd->nd_flag & ND_NFSV3)
2083 nfsrv_postopattr(nd, getret, &at);
2086 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2087 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2091 free(cookies, M_TEMP);
2099 io.uio_offset = (off_t)off;
2101 io.uio_segflg = UIO_SYSSPACE;
2102 io.uio_rw = UIO_READ;
2104 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2106 off = (u_int64_t)io.uio_offset;
2108 siz -= io.uio_resid;
2110 if (!cookies && !nd->nd_repstat)
2111 nd->nd_repstat = NFSERR_PERM;
2112 if (nd->nd_flag & ND_NFSV3) {
2113 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2114 if (!nd->nd_repstat)
2115 nd->nd_repstat = getret;
2119 * Handles the failed cases. nd->nd_repstat == 0 past here.
2121 if (nd->nd_repstat) {
2125 free(cookies, M_TEMP);
2126 if (nd->nd_flag & ND_NFSV3)
2127 nfsrv_postopattr(nd, getret, &at);
2131 * If nothing read, return eof
2136 if (nd->nd_flag & ND_NFSV2) {
2137 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2139 nfsrv_postopattr(nd, getret, &at);
2140 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2141 txdr_hyper(at.na_filerev, tl);
2144 *tl++ = newnfs_false;
2147 free(cookies, M_TEMP);
2152 * Check for degenerate cases of nothing useful read.
2153 * If so go try again
2157 dp = (struct dirent *)cpos;
2161 * For some reason FreeBSD's ufs_readdir() chooses to back the
2162 * directory offset up to a block boundary, so it is necessary to
2163 * skip over the records that precede the requested offset. This
2164 * requires the assumption that file offset cookies monotonically
2167 while (cpos < cend && ncookies > 0 &&
2168 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2169 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2170 cpos += dp->d_reclen;
2171 dp = (struct dirent *)cpos;
2175 if (cpos >= cend || ncookies == 0) {
2183 * If cnt > MCLBYTES and the reply will not be saved, use
2184 * ext_pgs mbufs for TLS.
2185 * For NFSv4.0, we do not know for sure if the reply will
2186 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2188 if (cnt > MCLBYTES && siz > MCLBYTES &&
2189 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2190 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2191 nd->nd_flag |= ND_EXTPG;
2194 * dirlen is the size of the reply, including all XDR and must
2195 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2196 * if the XDR should be included in "count", but to be safe, we do.
2197 * (Include the two booleans at the end of the reply in dirlen now.)
2199 if (nd->nd_flag & ND_NFSV3) {
2200 nfsrv_postopattr(nd, getret, &at);
2201 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2202 txdr_hyper(at.na_filerev, tl);
2203 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2205 dirlen = 2 * NFSX_UNSIGNED;
2208 /* Loop through the records and build reply */
2209 while (cpos < cend && ncookies > 0) {
2210 nlen = dp->d_namlen;
2211 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2212 nlen <= NFS_MAXNAMLEN) {
2213 if (nd->nd_flag & ND_NFSV3)
2214 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2216 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2223 * Build the directory record xdr from
2226 if (nd->nd_flag & ND_NFSV3) {
2227 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2228 *tl++ = newnfs_true;
2231 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2232 *tl++ = newnfs_true;
2234 *tl = txdr_unsigned(dp->d_fileno);
2235 (void) nfsm_strtom(nd, dp->d_name, nlen);
2236 if (nd->nd_flag & ND_NFSV3) {
2237 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2238 txdr_hyper(*cookiep, tl);
2240 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2241 *tl = txdr_unsigned(*cookiep);
2244 cpos += dp->d_reclen;
2245 dp = (struct dirent *)cpos;
2251 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2252 *tl++ = newnfs_false;
2258 free(cookies, M_TEMP);
2261 NFSEXITCODE2(0, nd);
2265 NFSEXITCODE2(error, nd);
2270 * Readdirplus for V3 and Readdir for V4.
2273 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2274 struct vnode *vp, struct nfsexstuff *exp)
2279 char *cpos, *cend, *rbuf;
2282 struct nfsvattr nva, at, *nvap = &nva;
2283 struct mbuf *mb0, *mb1;
2284 struct nfsreferral *refp;
2285 int nlen, r, error = 0, getret = 1, usevget = 1;
2286 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2287 caddr_t bpos0, bpos1;
2288 u_int64_t off, toff, verf __unused;
2289 uint64_t *cookies = NULL, *cookiep;
2290 nfsattrbit_t attrbits, rderrbits, savbits;
2293 struct componentname cn;
2294 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2295 struct mount *mp, *new_mp;
2296 uint64_t mounted_on_fileno;
2297 struct thread *p = curthread;
2298 int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2300 if (nd->nd_repstat) {
2301 nfsrv_postopattr(nd, getret, &at);
2304 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2305 off = fxdr_hyper(tl);
2308 verf = fxdr_hyper(tl);
2310 siz = fxdr_unsigned(int, *tl++);
2311 cnt = fxdr_unsigned(int, *tl);
2314 * Use the server's maximum data transfer size as the upper bound
2317 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2318 cnt = NFS_SRVMAXDATA(nd);
2321 * siz is a "hint" of how much directory information (name, fileid,
2322 * cookie) should be in the reply. At least one client "hints" 0,
2323 * so I set it to cnt for that case. I also round it up to the
2324 * next multiple of DIRBLKSIZ.
2325 * Since the size of a Readdirplus directory entry reply will always
2326 * be greater than a directory entry returned by VOP_READDIR(), it
2327 * does not make sense to read more than NFS_SRVMAXDATA() via
2332 else if (siz > NFS_SRVMAXDATA(nd))
2333 siz = NFS_SRVMAXDATA(nd);
2334 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2336 if (nd->nd_flag & ND_NFSV4) {
2337 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2340 NFSSET_ATTRBIT(&savbits, &attrbits);
2341 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2342 NFSZERO_ATTRBIT(&rderrbits);
2343 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2345 NFSZERO_ATTRBIT(&attrbits);
2348 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2350 if (!nd->nd_repstat) {
2351 if (off && verf != at.na_filerev) {
2353 * va_filerev is not sufficient as a cookie verifier,
2354 * since it is not supposed to change when entries are
2355 * removed/added unless that offset cookies returned to
2356 * the client are no longer valid.
2358 if (nd->nd_flag & ND_NFSV4) {
2359 nd->nd_repstat = NFSERR_NOTSAME;
2361 nd->nd_repstat = NFSERR_BAD_COOKIE;
2366 if (!nd->nd_repstat && vp->v_type != VDIR)
2367 nd->nd_repstat = NFSERR_NOTDIR;
2368 if (!nd->nd_repstat && cnt == 0)
2369 nd->nd_repstat = NFSERR_TOOSMALL;
2370 if (!nd->nd_repstat)
2371 nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2372 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2373 NFSACCCHK_VPISLOCKED, NULL);
2374 if (nd->nd_repstat) {
2376 if (nd->nd_flag & ND_NFSV3)
2377 nfsrv_postopattr(nd, getret, &at);
2380 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2381 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2383 rbuf = malloc(siz, M_TEMP, M_WAITOK);
2387 free(cookies, M_TEMP);
2395 io.uio_offset = (off_t)off;
2397 io.uio_segflg = UIO_SYSSPACE;
2398 io.uio_rw = UIO_READ;
2400 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2402 off = (u_int64_t)io.uio_offset;
2404 siz -= io.uio_resid;
2406 getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2408 if (!cookies && !nd->nd_repstat)
2409 nd->nd_repstat = NFSERR_PERM;
2410 if (!nd->nd_repstat)
2411 nd->nd_repstat = getret;
2412 if (nd->nd_repstat) {
2415 free(cookies, M_TEMP);
2417 if (nd->nd_flag & ND_NFSV3)
2418 nfsrv_postopattr(nd, getret, &at);
2422 * If nothing read, return eof
2427 if (nd->nd_flag & ND_NFSV3)
2428 nfsrv_postopattr(nd, getret, &at);
2429 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2430 txdr_hyper(at.na_filerev, tl);
2432 *tl++ = newnfs_false;
2434 free(cookies, M_TEMP);
2440 * Check for degenerate cases of nothing useful read.
2441 * If so go try again
2445 dp = (struct dirent *)cpos;
2449 * For some reason FreeBSD's ufs_readdir() chooses to back the
2450 * directory offset up to a block boundary, so it is necessary to
2451 * skip over the records that precede the requested offset. This
2452 * requires the assumption that file offset cookies monotonically
2455 while (cpos < cend && ncookies > 0 &&
2456 (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2457 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2458 ((nd->nd_flag & ND_NFSV4) &&
2459 ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2460 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2461 cpos += dp->d_reclen;
2462 dp = (struct dirent *)cpos;
2466 if (cpos >= cend || ncookies == 0) {
2473 * Busy the file system so that the mount point won't go away
2474 * and, as such, VFS_VGET() can be used safely.
2479 nd->nd_repstat = vfs_busy(mp, 0);
2481 if (nd->nd_repstat != 0) {
2483 free(cookies, M_TEMP);
2485 if (nd->nd_flag & ND_NFSV3)
2486 nfsrv_postopattr(nd, getret, &at);
2491 * Check to see if entries in this directory can be safely acquired
2492 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2493 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2494 * automount of the snapshot directory that is required will
2496 * This needs to be done here for NFSv4, since NFSv4 never does
2497 * a VFS_VGET() for "." or "..".
2500 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2501 if (r == EOPNOTSUPP) {
2503 cn.cn_nameiop = LOOKUP;
2504 cn.cn_lkflags = LK_SHARED | LK_RETRY;
2505 cn.cn_cred = nd->nd_cred;
2511 * If the reply is likely to exceed MCLBYTES and the reply will
2512 * not be saved, use ext_pgs mbufs for TLS.
2513 * It is difficult to predict how large each entry will be and
2514 * how many entries have been read, so just assume the directory
2515 * entries grow by a factor of 4 when attributes are included.
2516 * For NFSv4.0, we do not know for sure if the reply will
2517 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2519 if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2520 (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2521 (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2522 nd->nd_flag |= ND_EXTPG;
2525 * Save this position, in case there is an error before one entry
2529 bpos0 = nd->nd_bpos;
2530 bextpg0 = nd->nd_bextpg;
2531 bextpgsiz0 = nd->nd_bextpgsiz;
2534 * Fill in the first part of the reply.
2535 * dirlen is the reply length in bytes and cannot exceed cnt.
2536 * (Include the two booleans at the end of the reply in dirlen now,
2537 * so we recognize when we have exceeded cnt.)
2539 if (nd->nd_flag & ND_NFSV3) {
2540 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2541 nfsrv_postopattr(nd, getret, &at);
2543 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2545 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2546 txdr_hyper(at.na_filerev, tl);
2549 * Save this position, in case there is an empty reply needed.
2552 bpos1 = nd->nd_bpos;
2553 bextpg1 = nd->nd_bextpg;
2554 bextpgsiz1 = nd->nd_bextpgsiz;
2556 /* Loop through the records and build reply */
2558 while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2559 nlen = dp->d_namlen;
2560 if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2561 nlen <= NFS_MAXNAMLEN &&
2562 ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2563 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2564 || (nlen == 1 && dp->d_name[0] != '.'))) {
2566 * Save the current position in the reply, in case
2567 * this entry exceeds cnt.
2570 bpos1 = nd->nd_bpos;
2571 bextpg1 = nd->nd_bextpg;
2572 bextpgsiz1 = nd->nd_bextpgsiz;
2575 * For readdir_and_lookup get the vnode using
2584 mounted_on_fileno = (uint64_t)dp->d_fileno;
2585 if ((nd->nd_flag & ND_NFSV3) ||
2586 NFSNONZERO_ATTRBIT(&savbits)) {
2587 if (nd->nd_flag & ND_NFSV4)
2588 refp = nfsv4root_getreferral(NULL,
2592 r = VFS_VGET(mp, dp->d_fileno,
2596 if (r == EOPNOTSUPP) {
2599 cn.cn_nameiop = LOOKUP;
2606 cn.cn_nameptr = dp->d_name;
2607 cn.cn_namelen = nlen;
2608 cn.cn_flags = ISLASTCN |
2609 NOFOLLOW | LOCKLEAF;
2611 dp->d_name[0] == '.' &&
2612 dp->d_name[1] == '.')
2615 if (NFSVOPLOCK(vp, LK_SHARED)
2617 nd->nd_repstat = EPERM;
2620 if ((vp->v_vflag & VV_ROOT) != 0
2621 && (cn.cn_flags & ISDOTDOT)
2627 r = VOP_LOOKUP(vp, &nvp,
2635 * For NFSv4, check to see if nvp is
2636 * a mount point and get the mount
2637 * point vnode, as required.
2640 nfsrv_enable_crossmntpt != 0 &&
2641 (nd->nd_flag & ND_NFSV4) != 0 &&
2642 nvp->v_type == VDIR &&
2643 nvp->v_mountedhere != NULL) {
2644 new_mp = nvp->v_mountedhere;
2645 r = vfs_busy(new_mp, 0);
2649 r = VFS_ROOT(new_mp,
2659 * If we failed to look up the entry, then it
2660 * has become invalid, most likely removed.
2667 KASSERT(refp != NULL || nvp != NULL,
2668 ("%s: undetected lookup error", __func__));
2671 ((nd->nd_flag & ND_NFSV3) ||
2672 NFSNONZERO_ATTRBIT(&attrbits))) {
2673 r = nfsvno_getfh(nvp, &nfh, p);
2675 r = nfsvno_getattr(nvp, nvap, nd, p,
2677 if (r == 0 && is_zfs == 1 &&
2678 nfsrv_enable_crossmntpt != 0 &&
2679 (nd->nd_flag & ND_NFSV4) != 0 &&
2680 nvp->v_type == VDIR &&
2681 vp->v_mount != nvp->v_mount) {
2683 * For a ZFS snapshot, there is a
2684 * pseudo mount that does not set
2685 * v_mountedhere, so it needs to
2686 * be detected via a different
2691 new_mp = nvp->v_mount;
2696 * If we failed to get attributes of the entry,
2697 * then just skip it for NFSv3 (the traditional
2698 * behavior in the old NFS server).
2699 * For NFSv4 the behavior is controlled by
2700 * RDATTRERROR: we either ignore the error or
2702 * The exception is EOPNOTSUPP, which can be
2703 * returned by nfsvno_getfh() for certain
2704 * file systems, such as devfs. This indicates
2705 * that the file system cannot be exported,
2706 * so just skip over the entry.
2707 * Note that RDATTRERROR is never set for NFSv3.
2710 if (!NFSISSET_ATTRBIT(&attrbits,
2711 NFSATTRBIT_RDATTRERROR) ||
2714 if (needs_unbusy != 0)
2716 if ((nd->nd_flag & ND_NFSV3) ||
2726 * Build the directory record xdr
2728 if (nd->nd_flag & ND_NFSV3) {
2729 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2730 *tl++ = newnfs_true;
2732 *tl = txdr_unsigned(dp->d_fileno);
2733 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2734 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2735 txdr_hyper(*cookiep, tl);
2736 nfsrv_postopattr(nd, 0, nvap);
2737 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1);
2738 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2742 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2743 *tl++ = newnfs_true;
2744 txdr_hyper(*cookiep, tl);
2745 dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2747 supports_nfsv4acls =
2748 nfs_supportsnfsv4acls(nvp);
2751 supports_nfsv4acls = 0;
2753 dirlen += nfsrv_putreferralattr(nd,
2756 if (nd->nd_repstat) {
2759 if (needs_unbusy != 0)
2764 dirlen += nfsvno_fillattr(nd, new_mp,
2765 nvp, nvap, &nfh, r, &rderrbits,
2766 nd->nd_cred, p, isdgram, 0,
2767 supports_nfsv4acls, at_root,
2770 dirlen += nfsvno_fillattr(nd, new_mp,
2771 nvp, nvap, &nfh, r, &attrbits,
2772 nd->nd_cred, p, isdgram, 0,
2773 supports_nfsv4acls, at_root,
2778 dirlen += (3 * NFSX_UNSIGNED);
2780 if (needs_unbusy != 0)
2786 cpos += dp->d_reclen;
2787 dp = (struct dirent *)cpos;
2795 * If dirlen > cnt, we must strip off the last entry. If that
2796 * results in an empty reply, report NFSERR_TOOSMALL.
2798 if (dirlen > cnt || nd->nd_repstat) {
2799 if (!nd->nd_repstat && entrycnt == 0)
2800 nd->nd_repstat = NFSERR_TOOSMALL;
2801 if (nd->nd_repstat) {
2802 nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2803 if (nd->nd_flag & ND_NFSV3)
2804 nfsrv_postopattr(nd, getret, &at);
2806 nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2808 } else if (cpos < cend)
2810 if (!nd->nd_repstat) {
2811 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2812 *tl++ = newnfs_false;
2818 free(cookies, M_TEMP);
2822 NFSEXITCODE2(0, nd);
2826 NFSEXITCODE2(error, nd);
2831 * Get the settable attributes out of the mbuf list.
2832 * (Return 0 or EBADRPC)
2835 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2836 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2839 struct nfsv2_sattr *sp;
2840 int error = 0, toclient = 0;
2842 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2844 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2846 * Some old clients didn't fill in the high order 16bits.
2847 * --> check the low order 2 bytes for 0xffff
2849 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2850 nvap->na_mode = nfstov_mode(sp->sa_mode);
2851 if (sp->sa_uid != newnfs_xdrneg1)
2852 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2853 if (sp->sa_gid != newnfs_xdrneg1)
2854 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2855 if (sp->sa_size != newnfs_xdrneg1)
2856 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2857 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2859 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2861 nvap->na_atime.tv_sec =
2862 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2863 nvap->na_atime.tv_nsec = 0;
2866 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2867 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2870 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2871 if (*tl == newnfs_true) {
2872 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2873 nvap->na_mode = nfstov_mode(*tl);
2875 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2876 if (*tl == newnfs_true) {
2877 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2878 nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2880 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2881 if (*tl == newnfs_true) {
2882 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2883 nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2885 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2886 if (*tl == newnfs_true) {
2887 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2888 nvap->na_size = fxdr_hyper(tl);
2890 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2891 switch (fxdr_unsigned(int, *tl)) {
2892 case NFSV3SATTRTIME_TOCLIENT:
2893 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2894 fxdr_nfsv3time(tl, &nvap->na_atime);
2897 case NFSV3SATTRTIME_TOSERVER:
2898 vfs_timestamp(&nvap->na_atime);
2899 nvap->na_vaflags |= VA_UTIMES_NULL;
2902 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2903 switch (fxdr_unsigned(int, *tl)) {
2904 case NFSV3SATTRTIME_TOCLIENT:
2905 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2906 fxdr_nfsv3time(tl, &nvap->na_mtime);
2907 nvap->na_vaflags &= ~VA_UTIMES_NULL;
2909 case NFSV3SATTRTIME_TOSERVER:
2910 vfs_timestamp(&nvap->na_mtime);
2912 nvap->na_vaflags |= VA_UTIMES_NULL;
2917 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2920 NFSEXITCODE2(error, nd);
2925 * Handle the setable attributes for V4.
2926 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
2929 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2930 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2935 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
2936 int moderet, toclient = 0;
2937 u_char *cp, namestr[NFSV4_SMALLSTR + 1];
2940 u_short mode, mask; /* Same type as va_mode. */
2943 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
2946 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2947 attrsize = fxdr_unsigned(int, *tl);
2950 * Loop around getting the setable attributes. If an unsupported
2951 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
2954 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2955 bitpos = NFSATTRBIT_MAX;
2960 for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
2961 if (attrsum > attrsize) {
2962 error = NFSERR_BADXDR;
2965 if (NFSISSET_ATTRBIT(attrbitp, bitpos))
2967 case NFSATTRBIT_SIZE:
2968 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
2969 if (vp != NULL && vp->v_type != VREG) {
2970 error = (vp->v_type == VDIR) ? NFSERR_ISDIR :
2974 nvap->na_size = fxdr_hyper(tl);
2975 attrsum += NFSX_HYPER;
2977 case NFSATTRBIT_ACL:
2978 error = nfsrv_dissectacl(nd, aclp, true, &aceerr,
2982 if (aceerr && !nd->nd_repstat)
2983 nd->nd_repstat = aceerr;
2986 case NFSATTRBIT_ARCHIVE:
2987 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2988 if (!nd->nd_repstat)
2989 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2990 attrsum += NFSX_UNSIGNED;
2992 case NFSATTRBIT_HIDDEN:
2993 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2994 if (!nd->nd_repstat)
2995 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
2996 attrsum += NFSX_UNSIGNED;
2998 case NFSATTRBIT_MIMETYPE:
2999 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3000 i = fxdr_unsigned(int, *tl);
3001 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3004 if (!nd->nd_repstat)
3005 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3006 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3008 case NFSATTRBIT_MODE:
3009 moderet = NFSERR_INVAL; /* Can't do MODESETMASKED. */
3010 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3011 nvap->na_mode = nfstov_mode(*tl);
3012 attrsum += NFSX_UNSIGNED;
3014 case NFSATTRBIT_OWNER:
3015 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3016 j = fxdr_unsigned(int, *tl);
3018 error = NFSERR_BADXDR;
3021 if (j > NFSV4_SMALLSTR)
3022 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3025 error = nfsrv_mtostr(nd, cp, j);
3027 if (j > NFSV4_SMALLSTR)
3028 free(cp, M_NFSSTRING);
3031 if (!nd->nd_repstat) {
3032 nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3034 if (!nd->nd_repstat)
3037 if (j > NFSV4_SMALLSTR)
3038 free(cp, M_NFSSTRING);
3039 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3041 case NFSATTRBIT_OWNERGROUP:
3042 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3043 j = fxdr_unsigned(int, *tl);
3045 error = NFSERR_BADXDR;
3048 if (j > NFSV4_SMALLSTR)
3049 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3052 error = nfsrv_mtostr(nd, cp, j);
3054 if (j > NFSV4_SMALLSTR)
3055 free(cp, M_NFSSTRING);
3058 if (!nd->nd_repstat) {
3059 nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3061 if (!nd->nd_repstat)
3064 if (j > NFSV4_SMALLSTR)
3065 free(cp, M_NFSSTRING);
3066 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3068 case NFSATTRBIT_SYSTEM:
3069 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3070 if (!nd->nd_repstat)
3071 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3072 attrsum += NFSX_UNSIGNED;
3074 case NFSATTRBIT_TIMEACCESSSET:
3075 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3076 attrsum += NFSX_UNSIGNED;
3077 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3078 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3079 fxdr_nfsv4time(tl, &nvap->na_atime);
3081 attrsum += NFSX_V4TIME;
3083 vfs_timestamp(&nvap->na_atime);
3084 nvap->na_vaflags |= VA_UTIMES_NULL;
3087 case NFSATTRBIT_TIMEBACKUP:
3088 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3089 if (!nd->nd_repstat)
3090 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3091 attrsum += NFSX_V4TIME;
3093 case NFSATTRBIT_TIMECREATE:
3094 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3095 fxdr_nfsv4time(tl, &nvap->na_btime);
3096 attrsum += NFSX_V4TIME;
3098 case NFSATTRBIT_TIMEMODIFYSET:
3099 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3100 attrsum += NFSX_UNSIGNED;
3101 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3102 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3103 fxdr_nfsv4time(tl, &nvap->na_mtime);
3104 nvap->na_vaflags &= ~VA_UTIMES_NULL;
3105 attrsum += NFSX_V4TIME;
3107 vfs_timestamp(&nvap->na_mtime);
3109 nvap->na_vaflags |= VA_UTIMES_NULL;
3112 case NFSATTRBIT_MODESETMASKED:
3113 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3114 mode = fxdr_unsigned(u_short, *tl++);
3115 mask = fxdr_unsigned(u_short, *tl);
3117 * vp == NULL implies an Open/Create operation.
3118 * This attribute can only be used for Setattr and
3119 * only for NFSv4.1 or higher.
3120 * If moderet != 0, a mode attribute has also been
3121 * specified and this attribute cannot be done in the
3122 * same Setattr operation.
3124 if ((nd->nd_flag & ND_NFSV41) == 0)
3125 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3126 else if ((mode & ~07777) != 0 || (mask & ~07777) != 0 ||
3128 nd->nd_repstat = NFSERR_INVAL;
3129 else if (moderet == 0)
3130 moderet = VOP_GETATTR(vp, &va, nd->nd_cred);
3132 nvap->na_mode = (mode & mask) |
3133 (va.va_mode & ~mask);
3135 nd->nd_repstat = moderet;
3136 attrsum += 2 * NFSX_UNSIGNED;
3139 nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3141 * set bitpos so we drop out of the loop.
3143 bitpos = NFSATTRBIT_MAX;
3149 * some clients pad the attrlist, so we need to skip over the
3152 if (attrsum > attrsize) {
3153 error = NFSERR_BADXDR;
3155 attrsize = NFSM_RNDUP(attrsize);
3156 if (attrsum < attrsize)
3157 error = nfsm_advance(nd, attrsize - attrsum, -1);
3160 NFSEXITCODE2(error, nd);
3165 * Check/setup export credentials.
3168 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3169 struct ucred *credanon, bool testsec)
3174 * Check/setup credentials.
3176 if (nd->nd_flag & ND_GSS)
3177 exp->nes_exflag &= ~MNT_EXPORTANON;
3180 * Check to see if the operation is allowed for this security flavor.
3184 error = nfsvno_testexp(nd, exp);
3190 * Check to see if the file system is exported V4 only.
3192 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3193 error = NFSERR_PROGNOTV4;
3198 * Now, map the user credentials.
3199 * (Note that ND_AUTHNONE will only be set for an NFSv3
3200 * Fsinfo RPC. If set for anything else, this code might need
3203 if (NFSVNO_EXPORTED(exp)) {
3204 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3205 NFSVNO_EXPORTANON(exp) ||
3206 (nd->nd_flag & ND_AUTHNONE) != 0) {
3207 nd->nd_cred->cr_uid = credanon->cr_uid;
3208 nd->nd_cred->cr_gid = credanon->cr_gid;
3209 crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3210 credanon->cr_groups);
3211 } else if ((nd->nd_flag & ND_GSS) == 0) {
3213 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3214 * if there is a replacement credential with a group
3215 * list set up by "nfsuserd -manage-gids".
3216 * If there is no replacement, nfsrv_getgrpscred()
3217 * simply returns its argument.
3219 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3224 NFSEXITCODE2(error, nd);
3232 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3233 struct ucred **credp)
3237 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3238 &exp->nes_numsecflavor, exp->nes_secflavors);
3240 if (nfs_rootfhset) {
3241 exp->nes_exflag = 0;
3242 exp->nes_numsecflavor = 0;
3245 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3247 printf("nfsvno_checkexp: numsecflavors out of range\n");
3248 exp->nes_numsecflavor = 0;
3256 * Get a vnode for a file handle and export stuff.
3259 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3260 int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3261 struct ucred **credp)
3266 exp->nes_numsecflavor = 0;
3267 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3269 /* Make sure the server replies ESTALE to the client. */
3271 if (nam && !error) {
3272 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3273 &exp->nes_numsecflavor, exp->nes_secflavors);
3275 if (nfs_rootfhset) {
3276 exp->nes_exflag = 0;
3277 exp->nes_numsecflavor = 0;
3282 } else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3284 printf("nfsvno_fhtovp: numsecflavors out of range\n");
3285 exp->nes_numsecflavor = 0;
3295 * nfsd_fhtovp() - convert a fh to a vnode ptr
3296 * - look up fsid in mount list (if not found ret error)
3297 * - get vp and export rights by calling nfsvno_fhtovp()
3298 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3300 * - if mpp != NULL, return the mount point so that it can
3301 * be used for vn_finished_write() by the caller
3304 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3305 struct vnode **vpp, struct nfsexstuff *exp,
3306 struct mount **mpp, int startwrite, int nextop)
3308 struct mount *mp, *mpw;
3309 struct ucred *credanon;
3316 fhp = (fhandle_t *)nfp->nfsrvfh_data;
3317 mp = vfs_busyfs(&fhp->fh_fsid);
3319 nd->nd_repstat = ESTALE;
3325 error = vn_start_write(NULL, &mpw, V_WAIT);
3329 nd->nd_repstat = ESTALE;
3332 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3333 lktype = LK_EXCLUSIVE;
3337 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3342 * For NFSv4 without a pseudo root fs, unexported file handles
3343 * can be returned, so that Lookup works everywhere.
3345 if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3346 !(nd->nd_flag & ND_NFSV4)) {
3349 nd->nd_repstat = EACCES;
3353 * Personally, I've never seen any point in requiring a
3354 * reserved port#, since only in the rare case where the
3355 * clients are all boxes with secure system privileges,
3356 * does it provide any enhanced security, but... some people
3357 * believe it to be useful and keep putting this code back in.
3358 * (There is also some "security checker" out there that
3359 * complains if the nfs server doesn't enforce this.)
3360 * However, note the following:
3361 * RFC3530 (NFSv4) specifies that a reserved port# not be
3363 * RFC2623 recommends that, if a reserved port# is checked for,
3364 * that there be a way to turn that off--> ifdef'd.
3366 #ifdef NFS_REQRSVPORT
3367 if (!nd->nd_repstat) {
3368 struct sockaddr_in *saddr;
3369 struct sockaddr_in6 *saddr6;
3371 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3372 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3373 if (!(nd->nd_flag & ND_NFSV4) &&
3374 ((saddr->sin_family == AF_INET &&
3375 ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3376 (saddr6->sin6_family == AF_INET6 &&
3377 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3379 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3382 #endif /* NFS_REQRSVPORT */
3385 * Check/setup credentials.
3387 if (!nd->nd_repstat) {
3388 nd->nd_saveduid = nd->nd_cred->cr_uid;
3389 nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3390 nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3394 if (credanon != NULL)
3396 if (nd->nd_repstat) {
3397 vn_finished_write(mpw);
3399 } else if (mpp != NULL) {
3404 NFSEXITCODE2(0, nd);
3411 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3413 struct filedesc *fdp;
3417 fdp = p->td_proc->p_fd;
3418 if (fd < 0 || fd >= fdp->fd_nfiles ||
3419 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3431 * Called from nfssvc() to update the exports list. Just call
3432 * vfs_export(). This has to be done, since the v4 root fake fs isn't
3433 * in the mount list.
3436 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3438 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3440 struct nameidata nd;
3443 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export);
3444 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3446 else if (error == 0) {
3447 if (nfsexargp->fspec == NULL) {
3452 * If fspec != NULL, this is the v4root path.
3454 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec);
3455 if ((error = namei(&nd)) != 0)
3457 error = nfsvno_getfh(nd.ni_vp, &fh, p);
3460 nfs_rootfh.nfsrvfh_len = NFSX_MYFH;
3461 NFSBCOPY((caddr_t)&fh,
3462 nfs_rootfh.nfsrvfh_data,
3463 sizeof (fhandle_t));
3474 * This function needs to test to see if the system is near its limit
3475 * for memory allocation via malloc() or mget() and return True iff
3476 * either of these resources are near their limit.
3477 * XXX (For now, this is just a stub.)
3479 int nfsrv_testmalloclimit = 0;
3481 nfsrv_mallocmget_limit(void)
3483 static int printmesg = 0;
3484 static int testval = 1;
3486 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3487 if ((printmesg++ % 100) == 0)
3488 printf("nfsd: malloc/mget near limit\n");
3495 * BSD specific initialization of a mount point.
3500 static int inited = 0;
3505 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3506 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist);
3507 TAILQ_INIT(&nfsv4root_mnt.mnt_lazyvnodelist);
3508 nfsv4root_mnt.mnt_export = NULL;
3509 TAILQ_INIT(&nfsv4root_opt);
3510 TAILQ_INIT(&nfsv4root_newopt);
3511 nfsv4root_mnt.mnt_opt = &nfsv4root_opt;
3512 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt;
3513 nfsv4root_mnt.mnt_nvnodelistsize = 0;
3514 nfsv4root_mnt.mnt_lazyvnodelistsize = 0;
3518 nfsd_timer(void *arg)
3521 nfsrv_servertimer();
3522 callout_reset_sbt(&nfsd_callout, SBT_1S, SBT_1S, nfsd_timer, NULL, 0);
3526 * Get a vnode for a file handle, without checking exports, etc.
3529 nfsvno_getvp(fhandle_t *fhp)
3535 mp = vfs_busyfs(&fhp->fh_fsid);
3538 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3546 * Do a local VOP_ADVLOCK().
3549 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3550 u_int64_t end, struct thread *td)
3556 if (nfsrv_dolocallocks == 0)
3558 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3560 fl.l_whence = SEEK_SET;
3562 fl.l_start = (off_t)first;
3563 if (end == NFS64BITSSET) {
3567 fl.l_len = (off_t)tlen;
3570 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3571 * values for all calls, so that all locks will be held by the
3572 * nfsd server. (The nfsd server handles conflicts between the
3574 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3575 * bytes, so it can't be put in l_sysid.
3577 if (nfsv4_sysid == 0)
3578 nfsv4_sysid = nlm_acquire_next_sysid();
3579 fl.l_pid = (pid_t)0;
3580 fl.l_sysid = (int)nfsv4_sysid;
3582 if (ftype == F_UNLCK)
3583 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3584 (F_POSIX | F_REMOTE));
3586 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3587 (F_POSIX | F_REMOTE));
3595 * Check the nfsv4 root exports.
3598 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3600 struct ucred *credanon;
3601 int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3604 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags,
3605 &credanon, &numsecflavor, secflavors);
3607 error = NFSERR_PROGUNAVAIL;
3610 if (credanon != NULL)
3612 for (i = 0; i < numsecflavor; i++) {
3613 if (secflavors[i] == AUTH_SYS)
3614 nd->nd_flag |= ND_EXAUTHSYS;
3615 else if (secflavors[i] == RPCSEC_GSS_KRB5)
3616 nd->nd_flag |= ND_EXGSS;
3617 else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3618 nd->nd_flag |= ND_EXGSSINTEGRITY;
3619 else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3620 nd->nd_flag |= ND_EXGSSPRIVACY;
3623 /* And set ND_EXxx flags for TLS. */
3624 if ((exflags & MNT_EXTLS) != 0) {
3625 nd->nd_flag |= ND_EXTLS;
3626 if ((exflags & MNT_EXTLSCERT) != 0)
3627 nd->nd_flag |= ND_EXTLSCERT;
3628 if ((exflags & MNT_EXTLSCERTUSER) != 0)
3629 nd->nd_flag |= ND_EXTLSCERTUSER;
3638 * Nfs server pseudo system call for the nfsd's
3644 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3647 struct nfsd_addsock_args sockarg;
3648 struct nfsd_nfsd_args nfsdarg;
3649 struct nfsd_nfsd_oargs onfsdarg;
3650 struct nfsd_pnfsd_args pnfsdarg;
3651 struct vnode *vp, *nvp, *curdvp;
3652 struct pnfsdsfile *pf;
3653 struct nfsdevice *ds, *fds;
3654 cap_rights_t rights;
3655 int buflen, error, ret;
3656 char *buf, *cp, *cp2, *cp3;
3657 char fname[PNFS_FILENAME_LEN + 1];
3659 if (uap->flag & NFSSVC_NFSDADDSOCK) {
3660 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3664 * Since we don't know what rights might be required,
3665 * pretend that we need them all. It is better to be too
3666 * careful than too reckless.
3668 error = fget(td, sockarg.sock,
3669 cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
3672 if (fp->f_type != DTYPE_SOCKET) {
3677 error = nfsrvd_addsock(fp);
3679 } else if (uap->flag & NFSSVC_NFSDNFSD) {
3680 if (uap->argp == NULL) {
3684 if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3685 error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3687 nfsdarg.principal = onfsdarg.principal;
3688 nfsdarg.minthreads = onfsdarg.minthreads;
3689 nfsdarg.maxthreads = onfsdarg.maxthreads;
3690 nfsdarg.version = 1;
3691 nfsdarg.addr = NULL;
3692 nfsdarg.addrlen = 0;
3693 nfsdarg.dnshost = NULL;
3694 nfsdarg.dnshostlen = 0;
3695 nfsdarg.dspath = NULL;
3696 nfsdarg.dspathlen = 0;
3697 nfsdarg.mdspath = NULL;
3698 nfsdarg.mdspathlen = 0;
3699 nfsdarg.mirrorcnt = 1;
3702 error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3705 if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3706 nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3707 nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3708 nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3709 nfsdarg.mirrorcnt >= 1 &&
3710 nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3711 nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3712 nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3713 NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3714 " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3715 nfsdarg.dspathlen, nfsdarg.dnshostlen,
3716 nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3717 cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3718 error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3723 cp[nfsdarg.addrlen] = '\0'; /* Ensure nul term. */
3725 cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3726 error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3728 free(nfsdarg.addr, M_TEMP);
3732 cp[nfsdarg.dnshostlen] = '\0'; /* Ensure nul term. */
3733 nfsdarg.dnshost = cp;
3734 cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3735 error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3737 free(nfsdarg.addr, M_TEMP);
3738 free(nfsdarg.dnshost, M_TEMP);
3742 cp[nfsdarg.dspathlen] = '\0'; /* Ensure nul term. */
3743 nfsdarg.dspath = cp;
3744 cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3745 error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3747 free(nfsdarg.addr, M_TEMP);
3748 free(nfsdarg.dnshost, M_TEMP);
3749 free(nfsdarg.dspath, M_TEMP);
3753 cp[nfsdarg.mdspathlen] = '\0'; /* Ensure nul term. */
3754 nfsdarg.mdspath = cp;
3756 nfsdarg.addr = NULL;
3757 nfsdarg.addrlen = 0;
3758 nfsdarg.dnshost = NULL;
3759 nfsdarg.dnshostlen = 0;
3760 nfsdarg.dspath = NULL;
3761 nfsdarg.dspathlen = 0;
3762 nfsdarg.mdspath = NULL;
3763 nfsdarg.mdspathlen = 0;
3764 nfsdarg.mirrorcnt = 1;
3767 error = nfsrvd_nfsd(td, &nfsdarg);
3768 free(nfsdarg.addr, M_TEMP);
3769 free(nfsdarg.dnshost, M_TEMP);
3770 free(nfsdarg.dspath, M_TEMP);
3771 free(nfsdarg.mdspath, M_TEMP);
3772 } else if (uap->flag & NFSSVC_PNFSDS) {
3773 error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3774 if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3775 pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3776 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3777 error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3780 error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3782 } else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3783 cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3784 buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3785 buf = malloc(buflen, M_TEMP, M_WAITOK);
3786 error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3788 NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3789 if (error == 0 && pnfsdarg.dspath != NULL) {
3790 cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3791 error = copyinstr(pnfsdarg.dspath, cp2,
3792 PATH_MAX + 1, NULL);
3793 NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3797 if (error == 0 && pnfsdarg.curdspath != NULL) {
3798 cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3799 error = copyinstr(pnfsdarg.curdspath, cp3,
3800 PATH_MAX + 1, NULL);
3801 NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3808 error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3809 &buflen, fname, td, &vp, &nvp, &pf, &ds,
3811 NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3813 if (pf->dsf_dir >= nfsrv_dsdirsize) {
3814 printf("copymr: dsdir out of range\n");
3817 NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3818 error = nfsrv_copymr(vp, nvp,
3819 ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3820 (struct pnfsdsfile *)buf,
3821 buflen / sizeof(*pf), td->td_ucred, td);
3824 if (fds != NULL && error == 0) {
3825 curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3826 ret = vn_lock(curdvp, LK_EXCLUSIVE);
3828 nfsrv_dsremove(curdvp, fname,
3830 NFSVOPUNLOCK(curdvp);
3833 NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3841 error = nfssvc_srvcall(td, uap, td->td_ucred);
3850 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3852 struct nfsex_args export;
3853 struct nfsex_oldargs oexp;
3854 struct file *fp = NULL;
3855 int stablefd, i, len;
3856 struct nfsd_clid adminrevoke;
3857 struct nfsd_dumplist dumplist;
3858 struct nfsd_dumpclients *dumpclients;
3859 struct nfsd_dumplocklist dumplocklist;
3860 struct nfsd_dumplocks *dumplocks;
3861 struct nameidata nd;
3863 int error = EINVAL, igotlock;
3866 static int suspend_nfsd = 0;
3868 if (uap->flag & NFSSVC_PUBLICFH) {
3869 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
3870 sizeof (fhandle_t));
3871 error = copyin(uap->argp,
3872 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
3875 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3876 (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
3877 error = copyin(uap->argp,(caddr_t)&export,
3878 sizeof (struct nfsex_args));
3881 if (export.export.ex_ngroups > NGROUPS_MAX ||
3882 export.export.ex_ngroups < 0)
3884 else if (export.export.ex_ngroups > 0) {
3885 grps = malloc(export.export.ex_ngroups *
3886 sizeof(gid_t), M_TEMP, M_WAITOK);
3887 error = copyin(export.export.ex_groups, grps,
3888 export.export.ex_ngroups * sizeof(gid_t));
3889 export.export.ex_groups = grps;
3891 export.export.ex_groups = NULL;
3893 error = nfsrv_v4rootexport(&export, cred, p);
3896 } else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
3897 NFSSVC_V4ROOTEXPORT) {
3898 error = copyin(uap->argp,(caddr_t)&oexp,
3899 sizeof (struct nfsex_oldargs));
3901 memset(&export.export, 0, sizeof(export.export));
3902 export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
3903 export.export.ex_root = oexp.export.ex_root;
3904 export.export.ex_uid = oexp.export.ex_anon.cr_uid;
3905 export.export.ex_ngroups =
3906 oexp.export.ex_anon.cr_ngroups;
3907 export.export.ex_groups = NULL;
3908 if (export.export.ex_ngroups > XU_NGROUPS ||
3909 export.export.ex_ngroups < 0)
3911 else if (export.export.ex_ngroups > 0) {
3912 export.export.ex_groups = malloc(
3913 export.export.ex_ngroups * sizeof(gid_t),
3915 for (i = 0; i < export.export.ex_ngroups; i++)
3916 export.export.ex_groups[i] =
3917 oexp.export.ex_anon.cr_groups[i];
3919 export.export.ex_addr = oexp.export.ex_addr;
3920 export.export.ex_addrlen = oexp.export.ex_addrlen;
3921 export.export.ex_mask = oexp.export.ex_mask;
3922 export.export.ex_masklen = oexp.export.ex_masklen;
3923 export.export.ex_indexfile = oexp.export.ex_indexfile;
3924 export.export.ex_numsecflavors =
3925 oexp.export.ex_numsecflavors;
3926 if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
3927 export.export.ex_numsecflavors < 0)
3930 for (i = 0; i < export.export.ex_numsecflavors;
3932 export.export.ex_secflavors[i] =
3933 oexp.export.ex_secflavors[i];
3935 export.fspec = oexp.fspec;
3937 error = nfsrv_v4rootexport(&export, cred, p);
3938 free(export.export.ex_groups, M_TEMP);
3940 } else if (uap->flag & NFSSVC_NOPUBLICFH) {
3943 } else if (uap->flag & NFSSVC_STABLERESTART) {
3944 error = copyin(uap->argp, (caddr_t)&stablefd,
3947 error = fp_getfvp(p, stablefd, &fp, &vp);
3948 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
3950 if (!error && newnfs_numnfsd != 0)
3953 nfsrv_stablefirst.nsf_fp = fp;
3954 nfsrv_setupstable(p);
3956 } else if (uap->flag & NFSSVC_ADMINREVOKE) {
3957 error = copyin(uap->argp, (caddr_t)&adminrevoke,
3958 sizeof (struct nfsd_clid));
3960 error = nfsrv_adminrevoke(&adminrevoke, p);
3961 } else if (uap->flag & NFSSVC_DUMPCLIENTS) {
3962 error = copyin(uap->argp, (caddr_t)&dumplist,
3963 sizeof (struct nfsd_dumplist));
3964 if (!error && (dumplist.ndl_size < 1 ||
3965 dumplist.ndl_size > NFSRV_MAXDUMPLIST))
3968 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
3969 dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3970 nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
3971 error = copyout(dumpclients, dumplist.ndl_list, len);
3972 free(dumpclients, M_TEMP);
3974 } else if (uap->flag & NFSSVC_DUMPLOCKS) {
3975 error = copyin(uap->argp, (caddr_t)&dumplocklist,
3976 sizeof (struct nfsd_dumplocklist));
3977 if (!error && (dumplocklist.ndllck_size < 1 ||
3978 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
3981 error = nfsrv_lookupfilename(&nd,
3982 dumplocklist.ndllck_fname, p);
3984 len = sizeof (struct nfsd_dumplocks) *
3985 dumplocklist.ndllck_size;
3986 dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
3987 nfsrv_dumplocks(nd.ni_vp, dumplocks,
3988 dumplocklist.ndllck_size, p);
3990 error = copyout(dumplocks, dumplocklist.ndllck_list,
3992 free(dumplocks, M_TEMP);
3994 } else if (uap->flag & NFSSVC_BACKUPSTABLE) {
3997 nfsd_master_pid = procp->p_pid;
3998 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
3999 nfsd_master_start = procp->p_stats->p_start;
4000 nfsd_master_proc = procp;
4002 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4003 NFSLOCKV4ROOTMUTEX();
4004 if (suspend_nfsd == 0) {
4005 /* Lock out all nfsd threads */
4007 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1,
4008 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL);
4009 } while (igotlock == 0 && suspend_nfsd == 0);
4012 NFSUNLOCKV4ROOTMUTEX();
4014 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4015 NFSLOCKV4ROOTMUTEX();
4016 if (suspend_nfsd != 0) {
4017 nfsv4_unlock(&nfsd_suspend_lock, 0);
4020 NFSUNLOCKV4ROOTMUTEX();
4030 * Returns 0 if ok, 1 otherwise.
4033 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4037 if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4038 (NFSVNO_EXTLSCERT(exp) &&
4039 (nd->nd_flag & ND_TLSCERT) == 0) ||
4040 (NFSVNO_EXTLSCERTUSER(exp) &&
4041 (nd->nd_flag & ND_TLSCERTUSER) == 0)) {
4042 if ((nd->nd_flag & ND_NFSV4) != 0)
4043 return (NFSERR_WRONGSEC);
4045 /* There is currently no auth_stat for this. */
4046 else if ((nd->nd_flag & ND_TLS) == 0)
4047 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4049 return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4052 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4056 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use
4057 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
4059 if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO)
4063 * This seems odd, but allow the case where the security flavor
4064 * list is empty. This happens when NFSv4 is traversing non-exported
4065 * file systems. Exported file systems should always have a non-empty
4066 * security flavor list.
4068 if (exp->nes_numsecflavor == 0)
4071 for (i = 0; i < exp->nes_numsecflavor; i++) {
4073 * The tests for privacy and integrity must be first,
4074 * since ND_GSS is set for everything but AUTH_SYS.
4076 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4077 (nd->nd_flag & ND_GSSPRIVACY))
4079 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4080 (nd->nd_flag & ND_GSSINTEGRITY))
4082 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4083 (nd->nd_flag & ND_GSS))
4085 if (exp->nes_secflavors[i] == AUTH_SYS &&
4086 (nd->nd_flag & ND_GSS) == 0)
4089 if ((nd->nd_flag & ND_NFSV4) != 0)
4090 return (NFSERR_WRONGSEC);
4091 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4095 * Calculate a hash value for the fid in a file handle.
4098 nfsrv_hashfh(fhandle_t *fhp)
4102 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4107 * Calculate a hash value for the sessionid.
4110 nfsrv_hashsessionid(uint8_t *sessionid)
4114 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4119 * Signal the userland master nfsd to backup the stable restart file.
4122 nfsrv_backupstable(void)
4126 if (nfsd_master_proc != NULL) {
4127 procp = pfind(nfsd_master_pid);
4128 /* Try to make sure it is the correct process. */
4129 if (procp == nfsd_master_proc &&
4130 procp->p_stats->p_start.tv_sec ==
4131 nfsd_master_start.tv_sec &&
4132 procp->p_stats->p_start.tv_usec ==
4133 nfsd_master_start.tv_usec &&
4134 strcmp(procp->p_comm, nfsd_master_comm) == 0)
4135 kern_psignal(procp, SIGUSR2);
4137 nfsd_master_proc = NULL;
4145 * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4146 * The arguments are in a structure, so that they can be passed through
4147 * taskqueue for a kernel process to execute this function.
4149 struct nfsrvdscreate {
4153 struct ucred *tcred;
4156 struct pnfsdsfile *pf;
4160 struct vattr createva;
4164 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4165 fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4166 char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4169 struct nameidata named;
4174 struct nfsmount *nmp;
4177 NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4178 LOCKPARENT | LOCKLEAF | NOCACHE);
4179 nfsvno_setpathbuf(&named, &bufp, &hashp);
4180 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4181 named.ni_cnd.cn_nameptr = bufp;
4182 if (fnamep != NULL) {
4183 strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4184 named.ni_cnd.cn_namelen = strlen(bufp);
4186 named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4187 NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4189 /* Create the date file in the DS mount. */
4190 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4192 error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4194 VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4196 /* Set the ownership of the file. */
4197 error = VOP_SETATTR(nvp, nvap, tcred);
4198 NFSD_DEBUG(4, "nfsrv_dscreate:"
4199 " setattr-uid=%d\n", error);
4204 printf("pNFS: pnfscreate failed=%d\n", error);
4206 printf("pNFS: pnfscreate vnlock=%d\n", error);
4209 nmp = VFSTONFS(nvp->v_mount);
4210 if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4211 != 0 || nmp->nm_nam->sa_len > sizeof(
4212 struct sockaddr_in6) ||
4213 np->n_fhp->nfh_len != NFSX_MYFH) {
4214 printf("Bad DS file: fstype=%s salen=%d"
4216 nvp->v_mount->mnt_vfc->vfc_name,
4217 nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4221 /* Set extattrs for the DS on the MDS file. */
4224 error = VOP_GETATTR(nvp, &va, tcred);
4226 dsa->dsa_filerev = va.va_filerev;
4227 dsa->dsa_size = va.va_size;
4228 dsa->dsa_atime = va.va_atime;
4229 dsa->dsa_mtime = va.va_mtime;
4230 dsa->dsa_bytes = va.va_bytes;
4234 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4236 NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4237 nmp->nm_nam->sa_len);
4238 NFSBCOPY(named.ni_cnd.cn_nameptr,
4240 sizeof(pf->dsf_filename));
4243 printf("pNFS: pnfscreate can't get DS"
4244 " attr=%d\n", error);
4245 if (nvpp != NULL && error == 0)
4250 nfsvno_relpathbuf(&named);
4255 * Start up the thread that will execute nfsrv_dscreate().
4258 start_dscreate(void *arg, int pending)
4260 struct nfsrvdscreate *dsc;
4262 dsc = (struct nfsrvdscreate *)arg;
4263 dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4264 dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4266 NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4270 * Create a pNFS data file on the Data Server(s).
4273 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4276 struct nfsrvdscreate *dsc, *tdsc = NULL;
4277 struct nfsdevice *ds, *tds, *fds;
4279 struct pnfsdsfile *pf, *tpf;
4280 struct pnfsdsattr dsattr;
4282 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4283 struct nfsmount *nmp;
4288 struct ucred *tcred;
4289 int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4292 /* Get a DS server directory in a round-robin order. */
4298 * Search for the first entry that handles this MDS fs, but use the
4299 * first entry for all MDS fs's otherwise.
4301 TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4302 if (tds->nfsdev_nmp != NULL) {
4303 if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4305 else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4306 &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4314 NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4317 i = dsdir[0] = ds->nfsdev_nextdir;
4318 ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4319 dvp[0] = ds->nfsdev_dsdir[i];
4320 tds = TAILQ_NEXT(ds, nfsdev_list);
4321 if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4322 TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4323 if (tds->nfsdev_nmp != NULL &&
4324 ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4325 (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4326 fsidcmp(&mp->mnt_stat.f_fsid,
4327 &tds->nfsdev_mdsfsid) == 0))) {
4328 dsdir[mirrorcnt] = i;
4329 dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4331 if (mirrorcnt >= nfsrv_maxpnfsmirror)
4336 /* Put at end of list to implement round-robin usage. */
4337 TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4338 TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4342 tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4344 tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4347 error = nfsvno_getfh(vp, &fh, p);
4349 error = VOP_GETATTR(vp, &va, cred);
4351 /* Set the attributes for "vp" to Setattr the DS vp. */
4354 vamode = va.va_mode;
4358 va.va_mode = vamode;
4361 printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4363 NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4365 /* Make data file name based on FH. */
4366 tcred = newnfs_getcred();
4369 * Create the file on each DS mirror, using kernel process(es) for the
4370 * additional mirrors.
4373 for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4374 tpf->dsf_dir = dsdir[i];
4375 tdsc->tcred = tcred;
4378 tdsc->createva = *vap;
4379 NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4386 if (nfs_pnfsiothreads != 0) {
4387 ret = nfs_pnfsio(start_dscreate, tdsc);
4388 NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4391 ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4392 NULL, tcred, p, NULL);
4394 KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4396 if (failpos == -1 && nfsds_failerr(ret))
4404 tpf->dsf_dir = dsdir[mirrorcnt - 1];
4405 error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4406 &dsattr, NULL, tcred, p, NULL);
4407 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4408 failpos = mirrorcnt - 1;
4412 timo = hz / 50; /* Wait for 20msec. */
4415 /* Wait for kernel task(s) to complete. */
4416 for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4417 while (tdsc->inprog != 0 && tdsc->done == 0)
4418 tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4419 if (tdsc->err != 0) {
4420 if (failpos == -1 && nfsds_failerr(tdsc->err))
4422 else if (error == 0)
4428 * If failpos has been set, that mirror has failed, so it needs
4432 nmp = VFSTONFS(dvp[failpos]->v_mount);
4434 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4435 NFSMNTP_CANCELRPCS)) == 0) {
4436 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4438 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4439 NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4442 nfsrv_killrpcs(nmp);
4444 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4452 ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4454 NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4455 mirrorcnt, nfsrv_maxpnfsmirror);
4457 * For all mirrors that couldn't be created, fill in the
4458 * *pf structure, but with an IP address == 0.0.0.0.
4460 tpf = pf + mirrorcnt;
4461 for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4463 tpf->dsf_sin.sin_family = AF_INET;
4464 tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4465 tpf->dsf_sin.sin_addr.s_addr = 0;
4466 tpf->dsf_sin.sin_port = 0;
4469 error = vn_extattr_set(vp, IO_NODELOCKED,
4470 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4471 sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4473 error = vn_extattr_set(vp, IO_NODELOCKED,
4474 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4475 sizeof(dsattr), (char *)&dsattr, p);
4477 printf("pNFS: pnfscreate setextattr=%d\n",
4480 printf("pNFS: pnfscreate=%d\n", error);
4486 * Get the information needed to remove the pNFS Data Server file from the
4487 * Metadata file. Upon success, ddvp is set non-NULL to the locked
4488 * DS directory vnode. The caller must unlock *ddvp when done with it.
4491 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4492 int *mirrorcntp, char *fname, fhandle_t *fhp)
4495 struct ucred *tcred;
4500 /* If not an exported regular file or not a pNFS server, just return. */
4501 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4502 nfsrv_devidcnt == 0)
4505 /* Check to see if this is the last hard link. */
4506 tcred = newnfs_getcred();
4507 error = VOP_GETATTR(vp, &va, tcred);
4510 printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4513 if (va.va_nlink > 1)
4516 error = nfsvno_getfh(vp, fhp, p);
4518 printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4523 buf = malloc(buflen, M_TEMP, M_WAITOK);
4524 /* Get the directory vnode for the DS mount and the file handle. */
4525 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4526 NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4529 printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4533 * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4534 * The arguments are in a structure, so that they can be passed through
4535 * taskqueue for a kernel process to execute this function.
4537 struct nfsrvdsremove {
4541 struct ucred *tcred;
4545 char fname[PNFS_FILENAME_LEN + 1];
4549 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4552 struct nameidata named;
4558 error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4561 named.ni_cnd.cn_nameiop = DELETE;
4562 named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4563 named.ni_cnd.cn_cred = tcred;
4564 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
4565 nfsvno_setpathbuf(&named, &bufp, &hashp);
4566 named.ni_cnd.cn_nameptr = bufp;
4567 named.ni_cnd.cn_namelen = strlen(fname);
4568 strlcpy(bufp, fname, NAME_MAX);
4569 NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4570 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4571 NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4573 error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4577 nfsvno_relpathbuf(&named);
4579 printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4584 * Start up the thread that will execute nfsrv_dsremove().
4587 start_dsremove(void *arg, int pending)
4589 struct nfsrvdsremove *dsrm;
4591 dsrm = (struct nfsrvdsremove *)arg;
4592 dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4595 NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4599 * Remove a pNFS data file from a Data Server.
4600 * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4601 * removed to set up the dvp and fill in the FH.
4604 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4607 struct ucred *tcred;
4608 struct nfsrvdsremove *dsrm, *tdsrm;
4609 struct nfsdevice *ds;
4610 struct nfsmount *nmp;
4611 int failpos, i, ret, timo;
4613 tcred = newnfs_getcred();
4616 dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4618 * Remove the file on each DS mirror, using kernel process(es) for the
4619 * additional mirrors.
4622 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4623 tdsrm->tcred = tcred;
4625 tdsrm->dvp = dvp[i];
4626 strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4631 if (nfs_pnfsiothreads != 0) {
4632 ret = nfs_pnfsio(start_dsremove, tdsrm);
4633 NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4636 ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4637 if (failpos == -1 && nfsds_failerr(ret))
4641 ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4642 if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4643 failpos = mirrorcnt - 1;
4644 timo = hz / 50; /* Wait for 20msec. */
4647 /* Wait for kernel task(s) to complete. */
4648 for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4649 while (tdsrm->inprog != 0 && tdsrm->done == 0)
4650 tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4651 if (failpos == -1 && nfsds_failerr(tdsrm->err))
4656 * If failpos has been set, that mirror has failed, so it needs
4660 nmp = VFSTONFS(dvp[failpos]->v_mount);
4662 if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4663 NFSMNTP_CANCELRPCS)) == 0) {
4664 nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4666 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4667 NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4670 nfsrv_killrpcs(nmp);
4672 nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4678 /* Get rid all layouts for the file. */
4679 nfsrv_freefilelayouts(fhp);
4686 * Generate a file name based on the file handle and put it in *bufp.
4687 * Return the number of bytes generated.
4690 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4694 const uint8_t *hexdigits = "0123456789abcdef";
4696 cp = (uint8_t *)fhp;
4697 for (i = 0; i < sizeof(*fhp); i++) {
4698 bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4699 bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4706 * Update the Metadata file's attributes from the DS file when a Read/Write
4707 * layout is returned.
4708 * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4709 * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4712 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4714 struct ucred *tcred;
4717 /* Do this as root so that it won't fail with EACCES. */
4718 tcred = newnfs_getcred();
4719 error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4720 NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4726 * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4729 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4734 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4735 NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4740 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4741 struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4742 struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4743 off_t *offp, int content, bool *eofp)
4745 struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4746 fhandle_t fh[NFSDEV_MAXMIRRORS];
4747 struct vnode *dvp[NFSDEV_MAXMIRRORS];
4748 struct nfsdevice *ds;
4749 struct pnfsdsattr dsattr;
4750 struct opnfsdsattr odsattr;
4752 int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4754 NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4756 * If not a regular file, not exported or not a pNFS server,
4757 * just return ENOENT.
4759 if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4760 nfsrv_devidcnt == 0)
4764 buf = malloc(buflen, M_TEMP, M_WAITOK);
4768 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4769 * from the MetaData file's extended attribute.
4771 if (ioproc == NFSPROC_GETATTR) {
4772 error = vn_extattr_get(vp, IO_NODELOCKED,
4773 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4776 if (buflen == sizeof(odsattr)) {
4777 NFSBCOPY(buf, &odsattr, buflen);
4778 nap->na_filerev = odsattr.dsa_filerev;
4779 nap->na_size = odsattr.dsa_size;
4780 nap->na_atime = odsattr.dsa_atime;
4781 nap->na_mtime = odsattr.dsa_mtime;
4783 * Fake na_bytes by rounding up na_size.
4784 * Since we don't know the block size, just
4785 * use BLKDEV_IOSIZE.
4787 nap->na_bytes = (odsattr.dsa_size +
4788 BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4789 } else if (buflen == sizeof(dsattr)) {
4790 NFSBCOPY(buf, &dsattr, buflen);
4791 nap->na_filerev = dsattr.dsa_filerev;
4792 nap->na_size = dsattr.dsa_size;
4793 nap->na_atime = dsattr.dsa_atime;
4794 nap->na_mtime = dsattr.dsa_mtime;
4795 nap->na_bytes = dsattr.dsa_bytes;
4801 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4802 * returns 0, just return now. nfsrv_checkdsattr()
4803 * returns 0 if there is no Read/Write layout
4804 * plus either an Open/Write_access or Write
4805 * delegation issued to a client for the file.
4807 if (nfsrv_pnfsgetdsattr == 0 ||
4808 nfsrv_checkdsattr(vp, p) == 0) {
4815 * Clear ENOATTR so the code below will attempt to do a
4816 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4817 * the extended attribute.
4819 if (error == ENOATTR)
4828 if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4830 printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4831 error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4832 &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4835 for (i = 0; i < mirrorcnt; i++)
4836 nmp[i] = VFSTONFS(dvp[i]->v_mount);
4838 printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4840 printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4843 if (origmircnt == -1)
4844 origmircnt = mirrorcnt;
4846 * If failpos is set to a mirror#, then that mirror has
4847 * failed and will be disabled. For Read, Getattr and Seek, the
4848 * function only tries one mirror, so if that mirror has
4849 * failed, it will need to be retried. As such, increment
4850 * tryitagain for these cases.
4851 * For Write, Setattr and Setacl, the function tries all
4852 * mirrors and will not return an error for the case where
4853 * one mirror has failed. For these cases, the functioning
4854 * mirror(s) will have been modified, so a retry isn't
4855 * necessary. These functions will set failpos for the
4858 if (ioproc == NFSPROC_READDS) {
4859 error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
4861 if (nfsds_failerr(error) && mirrorcnt > 1) {
4863 * Setting failpos will cause the mirror
4864 * to be disabled and then a retry of this
4871 } else if (ioproc == NFSPROC_WRITEDS)
4872 error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
4873 &nmp[0], mirrorcnt, mpp, cp, &failpos);
4874 else if (ioproc == NFSPROC_SETATTR)
4875 error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
4876 mirrorcnt, nap, &failpos);
4877 else if (ioproc == NFSPROC_SETACL)
4878 error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
4879 mirrorcnt, aclp, &failpos);
4880 else if (ioproc == NFSPROC_SEEKDS) {
4881 error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
4883 if (nfsds_failerr(error) && mirrorcnt > 1) {
4885 * Setting failpos will cause the mirror
4886 * to be disabled and then a retry of this
4893 } else if (ioproc == NFSPROC_ALLOCATE)
4894 error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
4895 &nmp[0], mirrorcnt, &failpos);
4896 else if (ioproc == NFSPROC_DEALLOCATE)
4897 error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p,
4898 vp, &nmp[0], mirrorcnt, &failpos);
4900 error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
4901 vp, nmp[mirrorcnt - 1], nap);
4902 if (nfsds_failerr(error) && mirrorcnt > 1) {
4904 * Setting failpos will cause the mirror
4905 * to be disabled and then a retry of this
4906 * getattr is required.
4908 failpos = mirrorcnt - 1;
4915 failnmp = nmp[failpos];
4916 NFSLOCKMNT(failnmp);
4917 if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
4918 NFSMNTP_CANCELRPCS)) == 0) {
4919 failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4920 NFSUNLOCKMNT(failnmp);
4921 ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
4923 NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
4926 nfsrv_killrpcs(failnmp);
4927 NFSLOCKMNT(failnmp);
4928 failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4931 NFSUNLOCKMNT(failnmp);
4933 for (i = 0; i < mirrorcnt; i++)
4934 NFSVOPUNLOCK(dvp[i]);
4935 NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
4937 /* Try the Read/Getattr again if a mirror was deleted. */
4938 if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
4941 /* Return ENOENT for any Extended Attribute error. */
4945 NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
4950 * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
4952 * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
4953 * to be checked. If it points to a NULL nmp, then it returns
4954 * a suitable destination.
4955 * curnmp - If non-NULL, it is the source mount for the copy.
4958 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
4959 int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
4960 char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
4961 struct nfsmount *curnmp, int *ippos, int *dsdirp)
4963 struct vnode *dvp, *nvp = NULL, **tdvpp;
4965 struct nfsmount *nmp, *newnmp;
4966 struct sockaddr *sad;
4967 struct sockaddr_in *sin;
4968 struct nfsdevice *ds, *tds, *fndds;
4969 struct pnfsdsfile *pf;
4971 int error, fhiszero, fnd, gotone, i, mirrorcnt;
4973 ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
4982 if (newnmpp != NULL)
4987 error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
4988 "pnfsd.dsfile", buflenp, buf, p);
4989 mirrorcnt = *buflenp / sizeof(*pf);
4990 if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
4991 *buflenp != sizeof(*pf) * mirrorcnt))
4994 pf = (struct pnfsdsfile *)buf;
4995 /* If curnmp != NULL, check for a match in the mirror list. */
4996 if (curnmp != NULL && error == 0) {
4998 for (i = 0; i < mirrorcnt; i++, pf++) {
4999 sad = (struct sockaddr *)&pf->dsf_sin;
5000 if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5012 pf = (struct pnfsdsfile *)buf;
5013 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5015 for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5017 sad = (struct sockaddr *)&pf->dsf_sin;
5019 dsdir = pf->dsf_dir;
5020 if (dsdir >= nfsrv_dsdirsize) {
5021 printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5023 } else if (nvpp != NULL && newnmp != NULL &&
5024 nfsaddr2_match(sad, newnmp->nm_nam))
5027 if (ippos != NULL && curnmp == NULL &&
5028 sad->sa_family == AF_INET &&
5029 sin->sin_addr.s_addr == 0)
5031 if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5033 /* Use the socket address to find the mount point. */
5036 /* Find a match for the IP address. */
5037 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5038 if (ds->nfsdev_nmp != NULL) {
5039 dvp = ds->nfsdev_dvp;
5040 nmp = VFSTONFS(dvp->v_mount);
5041 if (nmp != ds->nfsdev_nmp)
5042 printf("different2 nmp %p %p\n",
5043 nmp, ds->nfsdev_nmp);
5044 if (nfsaddr2_match(sad, nmp->nm_nam)) {
5050 if (fndds != NULL && newnmpp != NULL &&
5052 /* Search for a place to make a mirror copy. */
5053 TAILQ_FOREACH(tds, &nfsrv_devidhead,
5055 if (tds->nfsdev_nmp != NULL &&
5057 ((tds->nfsdev_mdsisset == 0 &&
5058 fndds->nfsdev_mdsisset == 0) ||
5059 (tds->nfsdev_mdsisset != 0 &&
5060 fndds->nfsdev_mdsisset != 0 &&
5061 fsidcmp(&tds->nfsdev_mdsfsid,
5062 &mp->mnt_stat.f_fsid) == 0))) {
5063 *newnmpp = tds->nfsdev_nmp;
5069 * Move this entry to the end of the
5070 * list, so it won't be selected as
5071 * easily the next time.
5073 TAILQ_REMOVE(&nfsrv_devidhead, tds,
5075 TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5080 if (fndds != NULL) {
5081 dvp = fndds->nfsdev_dsdir[dsdir];
5082 if (lktype != 0 || fhiszero != 0 ||
5083 (nvpp != NULL && *nvpp == NULL)) {
5085 error = vn_lock(dvp,
5087 else if (lktype != 0)
5088 error = vn_lock(dvp, lktype);
5090 error = vn_lock(dvp, LK_SHARED);
5092 * If the file handle is all 0's, try to
5093 * do a Lookup against the DS to acquire
5095 * If dvpp == NULL or the Lookup fails,
5096 * unlock dvp after the call.
5098 if (error == 0 && (fhiszero != 0 ||
5099 (nvpp != NULL && *nvpp == NULL))) {
5100 error = nfsrv_pnfslookupds(vp,
5116 if (error != 0 || lktype == 0)
5122 NFSD_DEBUG(4, "gotone=%d\n", gotone);
5123 if (devid != NULL) {
5124 NFSBCOPY(fndds->nfsdev_deviceid,
5125 devid, NFSX_V4DEVICEID);
5126 devid += NFSX_V4DEVICEID;
5131 NFSBCOPY(&pf->dsf_fh, fhp++,
5133 if (fnamep != NULL && gotone == 1)
5136 sizeof(pf->dsf_filename));
5138 NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5143 if (error == 0 && gotone == 0)
5146 NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5149 *mirrorcntp = gotone;
5151 if (gotone > 0 && dvpp != NULL) {
5153 * If the error didn't occur on the first one and
5154 * dvpp != NULL, the one(s) prior to the failure will
5155 * have locked dvp's that need to be unlocked.
5157 for (i = 0; i < gotone; i++) {
5158 NFSVOPUNLOCK(*dvpp);
5163 * If it found the vnode to be copied from before a failure,
5164 * it needs to be vput()'d.
5166 if (nvpp != NULL && *nvpp != NULL) {
5175 * Set the extended attribute for the Change attribute.
5178 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5180 struct pnfsdsattr dsattr;
5183 ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5184 dsattr.dsa_filerev = nap->na_filerev;
5185 dsattr.dsa_size = nap->na_size;
5186 dsattr.dsa_atime = nap->na_atime;
5187 dsattr.dsa_mtime = nap->na_mtime;
5188 dsattr.dsa_bytes = nap->na_bytes;
5189 error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5190 "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5192 printf("pNFS: setextattr=%d\n", error);
5197 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5198 NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5201 struct nfsrv_descript *nd;
5203 struct mbuf *m, *m2;
5204 int error = 0, retlen, tlen, trimlen;
5206 NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5207 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5210 * Use a stateid where other is an alternating 01010 pattern and
5211 * seqid is 0xffffffff. This value is not defined as special by
5212 * the RFC and is used by the FreeBSD NFS server to indicate an
5213 * MDS->DS proxy operation.
5215 st.other[0] = 0x55555555;
5216 st.other[1] = 0x55555555;
5217 st.other[2] = 0x55555555;
5218 st.seqid = 0xffffffff;
5219 nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5220 NULL, NULL, 0, 0, cred);
5221 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5222 NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5223 txdr_hyper(off, tl);
5224 *(tl + 2) = txdr_unsigned(len);
5225 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5226 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5231 if (nd->nd_repstat == 0) {
5232 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5233 NFSM_STRSIZ(retlen, len);
5235 /* Trim off the pre-data XDR from the mbuf chain. */
5237 while (m != NULL && m != nd->nd_md) {
5238 if (m->m_next == nd->nd_md) {
5240 m_freem(nd->nd_mrep);
5241 nd->nd_mrep = m = nd->nd_md;
5246 printf("nfsrv_readdsrpc: busted mbuf list\n");
5252 * Now, adjust first mbuf so that any XDR before the
5253 * read data is skipped over.
5255 trimlen = nd->nd_dpos - mtod(m, char *);
5257 m->m_len -= trimlen;
5258 NFSM_DATAP(m, trimlen);
5262 * Truncate the mbuf chain at retlen bytes of data,
5263 * plus XDR padding that brings the length up to a
5266 tlen = NFSM_RNDUP(retlen);
5268 if (m->m_len >= tlen) {
5278 } while (m != NULL);
5280 printf("nfsrv_readdsrpc: busted mbuf list\n");
5289 error = nd->nd_repstat;
5291 /* If nd->nd_mrep is already NULL, this is a no-op. */
5292 m_freem(nd->nd_mrep);
5294 NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5299 * Do a write RPC on a DS data file, using this structure for the arguments,
5300 * so that this function can be executed by a separate kernel process.
5302 struct nfsrvwritedsdorpc {
5309 struct nfsmount *nmp;
5317 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5318 struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5321 struct nfsrv_descript *nd;
5322 nfsattrbit_t attrbits;
5324 int commit, error, retlen;
5326 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5327 nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5328 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5331 * Use a stateid where other is an alternating 01010 pattern and
5332 * seqid is 0xffffffff. This value is not defined as special by
5333 * the RFC and is used by the FreeBSD NFS server to indicate an
5334 * MDS->DS proxy operation.
5336 st.other[0] = 0x55555555;
5337 st.other[1] = 0x55555555;
5338 st.other[2] = 0x55555555;
5339 st.seqid = 0xffffffff;
5340 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5341 NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5342 txdr_hyper(off, tl);
5345 * Do all writes FileSync, since the server doesn't hold onto dirty
5346 * buffers. Since clients should be accessing the DS servers directly
5347 * using the pNFS layouts, this just needs to work correctly as a
5350 *tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5351 *tl = txdr_unsigned(len);
5352 NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5354 /* Put data in mbuf chain. */
5355 nd->nd_mb->m_next = m;
5357 /* Set nd_mb and nd_bpos to end of data. */
5358 while (m->m_next != NULL)
5361 nfsm_set(nd, m->m_len);
5362 NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5364 /* Do a Getattr for the attributes that change upon writing. */
5365 NFSZERO_ATTRBIT(&attrbits);
5366 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5367 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5368 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5369 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5370 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5371 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5372 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5373 (void) nfsrv_putattrbit(nd, &attrbits);
5374 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5375 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5380 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5381 /* Get rid of weak cache consistency data for now. */
5382 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5383 (ND_NFSV4 | ND_V4WCCATTR)) {
5384 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5385 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5386 NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5390 * Get rid of Op# and status for next op.
5392 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5394 nd->nd_flag |= ND_NOMOREDATA;
5396 if (nd->nd_repstat == 0) {
5397 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5398 retlen = fxdr_unsigned(int, *tl++);
5399 commit = fxdr_unsigned(int, *tl);
5400 if (commit != NFSWRITE_FILESYNC)
5402 NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5403 retlen, commit, error);
5405 error = nd->nd_repstat;
5406 /* We have no use for the Write Verifier since we use FileSync. */
5409 * Get the Change, Size, Access Time and Modify Time attributes and set
5410 * on the Metadata file, so its attributes will be what the file's
5411 * would be if it had been written.
5414 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5415 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5416 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5418 NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5420 m_freem(nd->nd_mrep);
5422 NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5427 * Start up the thread that will execute nfsrv_writedsdorpc().
5430 start_writedsdorpc(void *arg, int pending)
5432 struct nfsrvwritedsdorpc *drpc;
5434 drpc = (struct nfsrvwritedsdorpc *)arg;
5435 drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5436 drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5438 NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5442 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5443 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5444 struct mbuf **mpp, char *cp, int *failposp)
5446 struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5449 int error, i, offs, ret, timo;
5451 NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5452 KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5455 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5458 /* Calculate offset in mbuf chain that data starts. */
5459 offs = cp - mtod(*mpp, char *);
5460 NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5463 * Do the write RPC for every DS, using a separate kernel process
5464 * for every DS except the last one.
5467 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5469 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5477 tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5479 if (nfs_pnfsiothreads != 0) {
5480 ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5481 NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5485 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5487 if (nfsds_failerr(ret) && *failposp == -1)
5489 else if (error == 0 && ret != 0)
5495 m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5496 ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5497 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5498 *failposp = mirrorcnt - 1;
5499 else if (error == 0 && ret != 0)
5502 error = nfsrv_setextattr(vp, &na, p);
5503 NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5505 timo = hz / 50; /* Wait for 20msec. */
5508 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5509 /* Wait for RPCs on separate threads to complete. */
5510 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5511 tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5512 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5514 else if (error == 0 && tdrpc->err != 0)
5522 * Do a allocate RPC on a DS data file, using this structure for the arguments,
5523 * so that this function can be executed by a separate kernel process.
5525 struct nfsrvallocatedsdorpc {
5532 struct nfsmount *nmp;
5539 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5540 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5543 struct nfsrv_descript *nd;
5544 nfsattrbit_t attrbits;
5548 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5549 nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5550 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5553 * Use a stateid where other is an alternating 01010 pattern and
5554 * seqid is 0xffffffff. This value is not defined as special by
5555 * the RFC and is used by the FreeBSD NFS server to indicate an
5556 * MDS->DS proxy operation.
5558 st.other[0] = 0x55555555;
5559 st.other[1] = 0x55555555;
5560 st.other[2] = 0x55555555;
5561 st.seqid = 0xffffffff;
5562 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5563 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5564 txdr_hyper(off, tl); tl += 2;
5565 txdr_hyper(len, tl); tl += 2;
5566 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5568 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5569 NFSGETATTR_ATTRBIT(&attrbits);
5570 nfsrv_putattrbit(nd, &attrbits);
5571 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5572 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5577 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5579 if (nd->nd_repstat == 0) {
5580 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5581 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5582 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5584 error = nd->nd_repstat;
5585 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5587 m_freem(nd->nd_mrep);
5589 NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5594 * Start up the thread that will execute nfsrv_allocatedsdorpc().
5597 start_allocatedsdorpc(void *arg, int pending)
5599 struct nfsrvallocatedsdorpc *drpc;
5601 drpc = (struct nfsrvallocatedsdorpc *)arg;
5602 drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5603 drpc->len, NULL, drpc->cred, drpc->p);
5605 NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5609 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5610 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5613 struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5615 int error, i, ret, timo;
5617 NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5620 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5624 * Do the allocate RPC for every DS, using a separate kernel process
5625 * for every DS except the last one.
5628 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5630 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5639 if (nfs_pnfsiothreads != 0) {
5640 ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5641 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5645 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5647 if (nfsds_failerr(ret) && *failposp == -1)
5649 else if (error == 0 && ret != 0)
5655 ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5656 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5657 *failposp = mirrorcnt - 1;
5658 else if (error == 0 && ret != 0)
5661 error = nfsrv_setextattr(vp, &na, p);
5662 NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5664 timo = hz / 50; /* Wait for 20msec. */
5667 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5668 /* Wait for RPCs on separate threads to complete. */
5669 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5670 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5671 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5673 else if (error == 0 && tdrpc->err != 0)
5681 * Do a deallocate RPC on a DS data file, using this structure for the
5682 * arguments, so that this function can be executed by a separate kernel
5685 struct nfsrvdeallocatedsdorpc {
5692 struct nfsmount *nmp;
5699 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5700 off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5703 struct nfsrv_descript *nd;
5704 nfsattrbit_t attrbits;
5708 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5709 nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp,
5710 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5713 * Use a stateid where other is an alternating 01010 pattern and
5714 * seqid is 0xffffffff. This value is not defined as special by
5715 * the RFC and is used by the FreeBSD NFS server to indicate an
5716 * MDS->DS proxy operation.
5718 st.other[0] = 0x55555555;
5719 st.other[1] = 0x55555555;
5720 st.other[2] = 0x55555555;
5721 st.seqid = 0xffffffff;
5722 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5723 NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5724 txdr_hyper(off, tl); tl += 2;
5725 txdr_hyper(len, tl); tl += 2;
5726 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len);
5728 /* Do a Getattr for the attributes that change upon writing. */
5729 NFSZERO_ATTRBIT(&attrbits);
5730 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5731 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5732 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5733 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5734 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5735 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5736 nfsrv_putattrbit(nd, &attrbits);
5737 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5738 cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5743 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n",
5745 /* Get rid of weak cache consistency data for now. */
5746 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5747 (ND_NFSV4 | ND_V4WCCATTR)) {
5748 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5749 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5750 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error);
5754 * Get rid of Op# and status for next op.
5756 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5758 nd->nd_flag |= ND_NOMOREDATA;
5760 if (nd->nd_repstat == 0) {
5761 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5762 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5763 NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5765 error = nd->nd_repstat;
5766 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error);
5768 m_freem(nd->nd_mrep);
5770 NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error);
5775 * Start up the thread that will execute nfsrv_deallocatedsdorpc().
5778 start_deallocatedsdorpc(void *arg, int pending)
5780 struct nfsrvdeallocatedsdorpc *drpc;
5782 drpc = (struct nfsrvdeallocatedsdorpc *)arg;
5783 drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5784 drpc->len, NULL, drpc->cred, drpc->p);
5786 NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err);
5790 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5791 NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5794 struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL;
5796 int error, i, ret, timo;
5798 NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n");
5801 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5805 * Do the deallocate RPC for every DS, using a separate kernel process
5806 * for every DS except the last one.
5809 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5811 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5820 if (nfs_pnfsiothreads != 0) {
5821 ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc);
5822 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n",
5826 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len,
5828 if (nfsds_failerr(ret) && *failposp == -1)
5830 else if (error == 0 && ret != 0)
5836 ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5837 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5838 *failposp = mirrorcnt - 1;
5839 else if (error == 0 && ret != 0)
5842 error = nfsrv_setextattr(vp, &na, p);
5843 NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error);
5845 timo = hz / 50; /* Wait for 20msec. */
5848 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5849 /* Wait for RPCs on separate threads to complete. */
5850 while (tdrpc->inprog != 0 && tdrpc->done == 0)
5851 tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5852 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5854 else if (error == 0 && tdrpc->err != 0)
5862 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5863 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
5864 struct nfsvattr *dsnap)
5867 struct nfsrv_descript *nd;
5869 nfsattrbit_t attrbits;
5872 NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
5873 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5875 * Use a stateid where other is an alternating 01010 pattern and
5876 * seqid is 0xffffffff. This value is not defined as special by
5877 * the RFC and is used by the FreeBSD NFS server to indicate an
5878 * MDS->DS proxy operation.
5880 st.other[0] = 0x55555555;
5881 st.other[1] = 0x55555555;
5882 st.other[2] = 0x55555555;
5883 st.seqid = 0xffffffff;
5884 nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5885 NULL, NULL, 0, 0, cred);
5886 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5887 nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
5889 /* Do a Getattr for the attributes that change due to writing. */
5890 NFSZERO_ATTRBIT(&attrbits);
5891 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5892 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5893 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5894 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5895 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5896 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5897 *tl = txdr_unsigned(NFSV4OP_GETATTR);
5898 (void) nfsrv_putattrbit(nd, &attrbits);
5899 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5900 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5905 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
5907 /* Get rid of weak cache consistency data for now. */
5908 if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5909 (ND_NFSV4 | ND_V4WCCATTR)) {
5910 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5911 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5912 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
5916 * Get rid of Op# and status for next op.
5918 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5920 nd->nd_flag |= ND_NOMOREDATA;
5922 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
5925 if (nd->nd_repstat != 0)
5926 error = nd->nd_repstat;
5928 * Get the Change, Size, Access Time and Modify Time attributes and set
5929 * on the Metadata file, so its attributes will be what the file's
5930 * would be if it had been written.
5933 NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5934 error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
5935 NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5937 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
5939 m_freem(nd->nd_mrep);
5941 NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
5945 struct nfsrvsetattrdsdorpc {
5950 struct nfsmount *nmp;
5955 struct nfsvattr dsna;
5960 * Start up the thread that will execute nfsrv_setattrdsdorpc().
5963 start_setattrdsdorpc(void *arg, int pending)
5965 struct nfsrvsetattrdsdorpc *drpc;
5967 drpc = (struct nfsrvsetattrdsdorpc *)arg;
5968 drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
5969 drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
5974 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
5975 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5976 struct nfsvattr *nap, int *failposp)
5978 struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
5980 int error, i, ret, timo;
5982 NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
5985 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5989 * Do the setattr RPC for every DS, using a separate kernel process
5990 * for every DS except the last one.
5993 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5996 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6004 if (nfs_pnfsiothreads != 0) {
6005 ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
6006 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
6010 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
6012 if (nfsds_failerr(ret) && *failposp == -1)
6014 else if (error == 0 && ret != 0)
6020 ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
6021 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6022 *failposp = mirrorcnt - 1;
6023 else if (error == 0 && ret != 0)
6026 error = nfsrv_setextattr(vp, &na, p);
6027 NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
6029 timo = hz / 50; /* Wait for 20msec. */
6032 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6033 /* Wait for RPCs on separate threads to complete. */
6034 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6035 tsleep(&tdrpc->tsk, PVFS, "srvsads", timo);
6036 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6038 else if (error == 0 && tdrpc->err != 0)
6046 * Do a Setattr of an NFSv4 ACL on the DS file.
6049 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6050 struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
6052 struct nfsrv_descript *nd;
6054 nfsattrbit_t attrbits;
6057 NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
6058 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6060 * Use a stateid where other is an alternating 01010 pattern and
6061 * seqid is 0xffffffff. This value is not defined as special by
6062 * the RFC and is used by the FreeBSD NFS server to indicate an
6063 * MDS->DS proxy operation.
6065 st.other[0] = 0x55555555;
6066 st.other[1] = 0x55555555;
6067 st.other[2] = 0x55555555;
6068 st.seqid = 0xffffffff;
6069 nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6070 NULL, NULL, 0, 0, cred);
6071 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6072 NFSZERO_ATTRBIT(&attrbits);
6073 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
6075 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
6076 * so passing in the metadata "vp" will be ok, since it is of
6077 * the same type (VREG).
6079 nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
6080 NULL, 0, 0, 0, 0, 0, NULL);
6081 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6082 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6087 NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
6089 error = nd->nd_repstat;
6090 m_freem(nd->nd_mrep);
6095 struct nfsrvsetacldsdorpc {
6100 struct nfsmount *nmp;
6109 * Start up the thread that will execute nfsrv_setacldsdorpc().
6112 start_setacldsdorpc(void *arg, int pending)
6114 struct nfsrvsetacldsdorpc *drpc;
6116 drpc = (struct nfsrvsetacldsdorpc *)arg;
6117 drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
6118 drpc->vp, drpc->nmp, drpc->aclp);
6123 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6124 struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
6127 struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
6128 int error, i, ret, timo;
6130 NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
6133 tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6137 * Do the setattr RPC for every DS, using a separate kernel process
6138 * for every DS except the last one.
6141 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6144 NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6152 if (nfs_pnfsiothreads != 0) {
6153 ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
6154 NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
6158 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
6160 if (nfsds_failerr(ret) && *failposp == -1)
6162 else if (error == 0 && ret != 0)
6168 ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
6169 if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6170 *failposp = mirrorcnt - 1;
6171 else if (error == 0 && ret != 0)
6173 NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
6175 timo = hz / 50; /* Wait for 20msec. */
6178 for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6179 /* Wait for RPCs on separate threads to complete. */
6180 while (tdrpc->inprog != 0 && tdrpc->done == 0)
6181 tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6182 if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6184 else if (error == 0 && tdrpc->err != 0)
6192 * Getattr call to the DS for the attributes that change due to writing.
6195 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6196 struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6198 struct nfsrv_descript *nd;
6200 nfsattrbit_t attrbits;
6202 NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6203 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6204 nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6205 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6206 NFSZERO_ATTRBIT(&attrbits);
6207 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6208 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6209 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6210 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6211 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6212 (void) nfsrv_putattrbit(nd, &attrbits);
6213 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6214 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6219 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6221 if (nd->nd_repstat == 0) {
6222 error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6223 NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6226 * We can only save the updated values in the extended
6227 * attribute if the vp is exclusively locked.
6228 * This should happen when any of the following operations
6229 * occur on the vnode:
6230 * Close, Delegreturn, LayoutCommit, LayoutReturn
6231 * As such, the updated extended attribute should get saved
6232 * before nfsrv_checkdsattr() returns 0 and allows the cached
6233 * attributes to be returned without calling this function.
6235 if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6236 error = nfsrv_setextattr(vp, nap, p);
6237 NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6241 error = nd->nd_repstat;
6242 m_freem(nd->nd_mrep);
6244 NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6249 * Seek call to a DS.
6252 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6253 struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6256 struct nfsrv_descript *nd;
6260 NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6262 * Use a stateid where other is an alternating 01010 pattern and
6263 * seqid is 0xffffffff. This value is not defined as special by
6264 * the RFC and is used by the FreeBSD NFS server to indicate an
6265 * MDS->DS proxy operation.
6267 st.other[0] = 0x55555555;
6268 st.other[1] = 0x55555555;
6269 st.other[2] = 0x55555555;
6270 st.seqid = 0xffffffff;
6271 nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6272 nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6273 sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6274 nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6275 NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6276 txdr_hyper(*offp, tl); tl += 2;
6277 *tl = txdr_unsigned(content);
6278 error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6279 NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6284 NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6285 if (nd->nd_repstat == 0) {
6286 NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6287 if (*tl++ == newnfs_true)
6291 *offp = fxdr_hyper(tl);
6293 error = nd->nd_repstat;
6295 m_freem(nd->nd_mrep);
6297 NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6302 * Get the device id and file handle for a DS file.
6305 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6306 fhandle_t *fhp, char *devid)
6312 buf = malloc(buflen, M_TEMP, M_WAITOK);
6313 error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6314 fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6320 * Do a Lookup against the DS for the filename.
6323 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6324 struct vnode **nvpp, NFSPROC_T *p)
6326 struct nameidata named;
6327 struct ucred *tcred;
6333 tcred = newnfs_getcred();
6334 named.ni_cnd.cn_nameiop = LOOKUP;
6335 named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6336 named.ni_cnd.cn_cred = tcred;
6337 named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
6338 nfsvno_setpathbuf(&named, &bufp, &hashp);
6339 named.ni_cnd.cn_nameptr = bufp;
6340 named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6341 strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6342 NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6343 error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6344 NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6346 nfsvno_relpathbuf(&named);
6349 NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6354 * Set the file handle to the correct one.
6357 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6358 char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6364 NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6366 * We can only do a vn_set_extattr() if the vnode is exclusively
6367 * locked and vn_start_write() has been done. If devid != NULL or
6368 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6369 * may not have been done.
6370 * If not done now, it will be done on a future call.
6372 if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6374 ret = vn_extattr_set(vp, IO_NODELOCKED,
6375 EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6377 NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6381 * Cause RPCs waiting on "nmp" to fail. This is called for a DS mount point
6382 * when the DS has failed.
6385 nfsrv_killrpcs(struct nfsmount *nmp)
6389 * Call newnfs_nmcancelreqs() to cause
6390 * any RPCs in progress on the mount point to
6392 * This will cause any process waiting for an
6393 * RPC to complete while holding a vnode lock
6394 * on the mounted-on vnode (such as "df" or
6395 * a non-forced "umount") to fail.
6396 * This will unlock the mounted-on vnode so
6397 * a forced dismount can succeed.
6398 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6401 newnfs_nmcancelreqs(nmp);
6405 * Sum up the statfs info for each of the DSs, so that the client will
6406 * receive the total for all DSs.
6409 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6412 struct nfsdevice *ds;
6413 struct vnode **dvpp, **tdvpp, *dvp;
6415 int cnt, error = 0, i;
6417 if (nfsrv_devidcnt <= 0)
6419 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6420 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6422 /* Get an array of the dvps for the DSs. */
6426 /* First, search for matches for same file system. */
6427 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6428 if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6429 fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6430 if (++i > nfsrv_devidcnt)
6432 *tdvpp++ = ds->nfsdev_dvp;
6436 * If no matches for same file system, total all servers not assigned
6440 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6441 if (ds->nfsdev_nmp != NULL &&
6442 ds->nfsdev_mdsisset == 0) {
6443 if (++i > nfsrv_devidcnt)
6445 *tdvpp++ = ds->nfsdev_dvp;
6452 /* Do a VFS_STATFS() for each of the DSs and sum them up. */
6454 for (i = 0; i < cnt && error == 0; i++) {
6456 error = VFS_STATFS(dvp->v_mount, tsf);
6458 if (sf->f_bsize == 0) {
6459 if (tsf->f_bsize > 0)
6460 sf->f_bsize = tsf->f_bsize;
6464 if (tsf->f_blocks > 0) {
6465 if (sf->f_bsize != tsf->f_bsize) {
6466 tot = tsf->f_blocks * tsf->f_bsize;
6467 sf->f_blocks += (tot / sf->f_bsize);
6469 sf->f_blocks += tsf->f_blocks;
6471 if (tsf->f_bfree > 0) {
6472 if (sf->f_bsize != tsf->f_bsize) {
6473 tot = tsf->f_bfree * tsf->f_bsize;
6474 sf->f_bfree += (tot / sf->f_bsize);
6476 sf->f_bfree += tsf->f_bfree;
6478 if (tsf->f_bavail > 0) {
6479 if (sf->f_bsize != tsf->f_bsize) {
6480 tot = tsf->f_bavail * tsf->f_bsize;
6481 sf->f_bavail += (tot / sf->f_bsize);
6483 sf->f_bavail += tsf->f_bavail;
6496 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6500 if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6501 error = NFSERR_ATTRNOTSUPP;
6505 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6506 * Make sure it has enough room for that - splitting every entry
6507 * into two and appending "canonical six" entries at the end.
6508 * Cribbed out of kern/vfs_acl.c - Rick M.
6510 if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6511 error = NFSERR_ATTRNOTSUPP;
6514 error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6516 error = nfsrv_dssetacl(vp, aclp, cred, p);
6517 if (error == ENOENT)
6527 * Seek vnode op call (actually it is a VOP_IOCTL()).
6528 * This function is called with the vnode locked, but unlocks and vrele()s
6529 * the vp before returning.
6532 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6533 off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6538 ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6540 * Attempt to seek on a DS file. A return of ENOENT implies
6541 * there is no DS file to seek on.
6543 error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6544 NULL, NULL, NULL, NULL, offp, content, eofp);
6545 if (error != ENOENT) {
6551 * Do the VOP_IOCTL() call. For the case where *offp == file_size,
6552 * VOP_IOCTL() will return ENXIO. However, the correct reply for
6553 * NFSv4.2 is *eofp == true and error == 0 for this case.
6556 error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6558 if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6559 /* Handle the cases where we might be at EOF. */
6560 ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6561 if (ret == 0 && *offp == at.na_size) {
6565 if (ret != 0 && error == 0)
6574 * Allocate vnode op call.
6577 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6583 ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6585 * Attempt to allocate on a DS file. A return of ENOENT implies
6586 * there is no DS file to allocate on.
6588 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6589 NULL, NULL, NULL, NULL, &len, 0, NULL);
6590 if (error != ENOENT)
6594 * Do the actual VOP_ALLOCATE(), looping so long as
6595 * progress is being made, to achieve completion.
6599 error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred);
6600 if (error == 0 && len > 0 && olen > len)
6602 } while (error == 0 && len > 0 && olen > len);
6603 if (error == 0 && len > 0)
6610 * Deallocate vnode op call.
6613 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6619 ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp");
6621 * Attempt to deallocate on a DS file. A return of ENOENT implies
6622 * there is no DS file to deallocate on.
6624 error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL,
6625 NULL, NULL, NULL, NULL, &len, 0, NULL);
6626 if (error != ENOENT)
6630 * Do the actual VOP_DEALLOCATE(), looping so long as
6631 * progress is being made, to achieve completion.
6635 error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred);
6636 if (error == 0 && len > 0 && olen > len)
6638 } while (error == 0 && len > 0 && olen > len);
6639 if (error == 0 && len > 0)
6646 * Get Extended Atribute vnode op into an mbuf list.
6649 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6650 struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6651 struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6654 struct uio io, *uiop = &io;
6655 struct mbuf *m, *m2;
6656 int alen, error, len, tlen;
6659 /* First, find out the size of the extended attribute. */
6660 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6663 return (NFSERR_NOXATTR);
6664 if (siz > maxresp - NFS_MAXXDR)
6665 return (NFSERR_XATTR2BIG);
6667 tlen = NFSM_RNDUP(len);
6670 * If cnt > MCLBYTES and the reply will not be saved, use
6671 * ext_pgs mbufs for TLS.
6672 * For NFSv4.0, we do not know for sure if the reply will
6673 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6674 * Always use ext_pgs mbufs if ND_EXTPG is set.
6676 if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6677 (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6678 (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6679 uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6680 maxextsiz, &m, &m2, &iv);
6682 uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6686 uiop->uio_iovcnt = 0;
6687 uiop->uio_iov = iv = NULL;
6690 uiop->uio_offset = 0;
6691 uiop->uio_resid = tlen;
6692 uiop->uio_rw = UIO_READ;
6693 uiop->uio_segflg = UIO_SYSSPACE;
6696 error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6703 error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6707 if (uiop->uio_resid > 0) {
6709 len = tlen - uiop->uio_resid;
6710 tlen = NFSM_RNDUP(len);
6712 printf("nfsvno_getxattr: weird size read\n");
6716 } else if (alen != tlen || tlen != len)
6717 m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6735 * Set Extended attribute vnode op from an mbuf list.
6738 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6739 char *cp, struct ucred *cred, struct thread *p)
6742 struct uio uio, *uiop = &uio;
6747 error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6753 uiop->uio_rw = UIO_WRITE;
6754 uiop->uio_segflg = UIO_SYSSPACE;
6756 uiop->uio_offset = 0;
6757 uiop->uio_resid = len;
6759 error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6761 uiop->uio_iovcnt = cnt;
6763 uiop->uio_iov = iv = NULL;
6764 uiop->uio_iovcnt = 0;
6767 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6770 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6771 nfsvno_updateds(vp, cred, p);
6772 error = VOP_FSYNC(vp, MNT_WAIT, p);
6783 * For a pNFS server, the DS file's ctime and
6784 * va_filerev (TimeMetadata and Change) needs to
6785 * be updated. This is a hack, but works by
6786 * flipping the S_ISGID bit in va_mode and then
6788 * It does result in two MDS->DS RPCs, but creating
6789 * a custom RPC just to do this seems overkill, since
6790 * Setxattr/Rmxattr will not be done that frequently.
6791 * If it fails part way through, that is not too
6792 * serious, since the DS file is never executed.
6795 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p)
6797 struct nfsvattr nva;
6801 ret = VOP_GETATTR(vp, &nva.na_vattr, cred);
6803 tmode = nva.na_mode;
6804 NFSVNO_ATTRINIT(&nva);
6806 NFSVNO_SETATTRVAL(&nva, mode, tmode);
6807 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6808 NFSPROC_SETATTR, NULL, NULL, NULL, &nva,
6809 NULL, NULL, 0, NULL);
6812 NFSVNO_SETATTRVAL(&nva, mode, tmode);
6813 ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6814 NFSPROC_SETATTR, NULL, NULL, NULL,
6815 &nva, NULL, NULL, 0, NULL);
6821 * Remove Extended attribute vnode op.
6824 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6825 struct ucred *cred, struct thread *p)
6830 * Get rid of any delegations. I am not sure why this is required,
6831 * but RFC-8276 says so.
6833 error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6837 error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6843 error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6844 if (error == EOPNOTSUPP)
6845 error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6848 if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6849 nfsvno_updateds(vp, cred, p);
6850 error = VOP_FSYNC(vp, MNT_WAIT, p);
6858 * List Extended Atribute vnode op into an mbuf list.
6861 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
6862 struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
6870 /* First, find out the size of the extended attribute. */
6871 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
6874 return (NFSERR_NOXATTR);
6875 if (siz <= cookie) {
6880 if (siz > cookie + *lenp) {
6881 siz = cookie + *lenp;
6885 /* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
6886 if (siz > 10 * 1024 * 1024) {
6887 error = NFSERR_XATTR2BIG;
6890 *bufp = malloc(siz, M_TEMP, M_WAITOK);
6891 iv.iov_base = *bufp;
6897 io.uio_rw = UIO_READ;
6898 io.uio_segflg = UIO_SYSSPACE;
6901 error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
6906 error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
6910 if (io.uio_resid > 0)
6911 siz -= io.uio_resid;
6916 free(*bufp, M_TEMP);
6924 * Trim trailing data off the mbuf list being built.
6927 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
6928 int bextpg, int bextpgsiz)
6933 if (mb->m_next != NULL) {
6934 m_freem(mb->m_next);
6937 if ((mb->m_flags & M_EXTPG) != 0) {
6938 KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
6939 ("nfsm_trimtrailing: bextpg out of range"));
6940 KASSERT(bpos == (char *)(void *)
6941 PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
6942 ("nfsm_trimtrailing: bextpgsiz bad!"));
6944 /* First, get rid of any pages after this position. */
6945 for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
6946 pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
6947 vm_page_unwire_noq(pg);
6950 mb->m_epg_npgs = bextpg + 1;
6952 fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
6954 fullpgsiz = PAGE_SIZE;
6955 mb->m_epg_last_len = fullpgsiz - bextpgsiz;
6956 mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
6957 for (i = 1; i < mb->m_epg_npgs; i++)
6958 mb->m_len += m_epg_pagelen(mb, i, 0);
6959 nd->nd_bextpgsiz = bextpgsiz;
6960 nd->nd_bextpg = bextpg;
6962 mb->m_len = bpos - mtod(mb, char *);
6969 * Check to see if a put file handle operation should test for
6970 * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
6971 * When Open is the next operation, NFSERR_WRONGSEC cannot be
6972 * replied for the Open cases that use a component. This can
6973 * be identified by the fact that the file handle's type is VDIR.
6976 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, enum vtype vtyp)
6979 if ((nd->nd_flag & ND_NFSV4) == 0)
6982 if ((nd->nd_flag & ND_LASTOP) != 0)
6985 if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
6986 nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
6987 nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
6988 nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
6990 if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
6996 * Check DSs marked no space.
6999 nfsrv_checknospc(void)
7002 struct nfsdevice *ds;
7003 struct vnode **dvpp, **tdvpp, *dvp;
7004 char *devid, *tdevid;
7005 int cnt, error = 0, i;
7007 if (nfsrv_devidcnt <= 0)
7009 dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
7010 devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK);
7011 tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
7013 /* Get an array of the dvps for the DSs. */
7018 /* First, search for matches for same file system. */
7019 TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7020 if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) {
7021 if (++i > nfsrv_devidcnt)
7023 *tdvpp++ = ds->nfsdev_dvp;
7024 NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID);
7025 tdevid += NFSX_V4DEVICEID;
7030 /* Do a VFS_STATFS() for each of the DSs and clear no space. */
7034 for (i = 0; i < cnt && error == 0; i++) {
7036 error = VFS_STATFS(dvp->v_mount, tsf);
7037 if (error == 0 && tsf->f_bavail > 0) {
7038 NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n");
7039 nfsrv_marknospc(tdevid, false);
7041 tdevid += NFSX_V4DEVICEID;
7045 free(devid, M_TEMP);
7048 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
7051 * Called once to initialize data structures...
7054 nfsd_modevent(module_t mod, int type, void *data)
7057 static int loaded = 0;
7064 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7065 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL,
7067 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL,
7070 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
7071 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
7072 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
7073 mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
7074 mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
7075 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0);
7076 callout_init(&nfsd_callout, 1);
7083 #ifdef VV_DISABLEDELEG
7084 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
7085 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
7087 nfsd_call_nfsd = nfssvc_nfsd;
7092 if (newnfs_numnfsd != 0) {
7097 #ifdef VV_DISABLEDELEG
7098 vn_deleg_ops.vndeleg_recall = NULL;
7099 vn_deleg_ops.vndeleg_disable = NULL;
7101 nfsd_call_nfsd = NULL;
7102 callout_drain(&nfsd_callout);
7104 /* Clean out all NFSv4 state. */
7105 nfsrv_throwawayallstate(curthread);
7107 /* Clean the NFS server reply cache */
7108 nfsrvd_cleancache();
7110 /* Free up the krpc server pool. */
7111 if (nfsrvd_pool != NULL)
7112 svcpool_destroy(nfsrvd_pool);
7114 /* and get rid of the locks */
7115 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7116 mtx_destroy(&nfsrchash_table[i].mtx);
7117 mtx_destroy(&nfsrcahash_table[i].mtx);
7119 mtx_destroy(&nfsrc_udpmtx);
7120 mtx_destroy(&nfs_v4root_mutex);
7121 mtx_destroy(&nfsv4root_mnt.mnt_mtx);
7122 mtx_destroy(&nfsrv_dontlistlock_mtx);
7123 mtx_destroy(&nfsrv_recalllock_mtx);
7124 for (i = 0; i < nfsrv_sessionhashsize; i++)
7125 mtx_destroy(&nfssessionhash[i].mtx);
7126 if (nfslayouthash != NULL) {
7127 for (i = 0; i < nfsrv_layouthashsize; i++)
7128 mtx_destroy(&nfslayouthash[i].mtx);
7129 free(nfslayouthash, M_NFSDSESSION);
7131 lockdestroy(&nfsv4root_mnt.mnt_explock);
7132 free(nfsclienthash, M_NFSDCLIENT);
7133 free(nfslockhash, M_NFSDLOCKFILE);
7134 free(nfssessionhash, M_NFSDSESSION);
7146 static moduledata_t nfsd_mod = {
7151 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
7153 /* So that loader and kldload(2) can find us, wherever we are.. */
7154 MODULE_VERSION(nfsd, 1);
7155 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
7156 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
7157 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
7158 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);