xref: /freebsd/sys/fs/nfsserver/nfs_nfsdport.c (revision cfbe7a62dc62e8a5d7520cb5eb8ad7c4a9418e26)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/capsicum.h>
37 #include <sys/extattr.h>
38 
39 /*
40  * Functions that perform the vfs operations required by the routines in
41  * nfsd_serv.c. It is hoped that this change will make the server more
42  * portable.
43  */
44 
45 #include <fs/nfs/nfsport.h>
46 #include <security/mac/mac_framework.h>
47 #include <sys/callout.h>
48 #include <sys/filio.h>
49 #include <sys/hash.h>
50 #include <sys/osd.h>
51 #include <sys/sysctl.h>
52 #include <nlm/nlm_prot.h>
53 #include <nlm/nlm.h>
54 #include <vm/vm_param.h>
55 #include <vm/vnode_pager.h>
56 
57 FEATURE(nfsd, "NFSv4 server");
58 
59 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;
60 extern int nfsrv_useacl;
61 extern int newnfs_numnfsd;
62 extern int nfsrv_sessionhashsize;
63 extern struct nfslayouthash *nfslayouthash;
64 extern int nfsrv_layouthashsize;
65 extern struct mtx nfsrv_dslock_mtx;
66 extern int nfs_pnfsiothreads;
67 extern volatile int nfsrv_devidcnt;
68 extern int nfsrv_maxpnfsmirror;
69 extern uint32_t nfs_srvmaxio;
70 extern int nfs_bufpackets;
71 extern u_long sb_max_adj;
72 extern struct nfsv4lock nfsv4rootfs_lock;
73 
74 NFSD_VNET_DECLARE(int, nfsrv_numnfsd);
75 NFSD_VNET_DECLARE(struct nfsrv_stablefirst, nfsrv_stablefirst);
76 NFSD_VNET_DECLARE(SVCPOOL *, nfsrvd_pool);
77 NFSD_VNET_DECLARE(struct nfsclienthashhead *, nfsclienthash);
78 NFSD_VNET_DECLARE(struct nfslockhashhead *, nfslockhash);
79 NFSD_VNET_DECLARE(struct nfssessionhash *, nfssessionhash);
80 NFSD_VNET_DECLARE(struct nfsv4lock, nfsd_suspend_lock);
81 NFSD_VNET_DECLARE(struct nfsstatsv1 *, nfsstatsv1_p);
82 
83 NFSDLOCKMUTEX;
84 NFSSTATESPINLOCK;
85 struct mtx nfsrc_udpmtx;
86 struct mtx nfs_v4root_mutex;
87 struct mtx nfsrv_dontlistlock_mtx;
88 struct mtx nfsrv_recalllock_mtx;
89 struct nfsrvfh nfs_pubfh;
90 int nfs_pubfhset = 0;
91 int nfsd_debuglevel = 0;
92 static pid_t nfsd_master_pid = (pid_t)-1;
93 static char nfsd_master_comm[MAXCOMLEN + 1];
94 static struct timeval nfsd_master_start;
95 static uint32_t nfsv4_sysid = 0;
96 static fhandle_t zerofh;
97 
98 NFSD_VNET_DEFINE(struct proc *, nfsd_master_proc) = NULL;
99 NFSD_VNET_DEFINE(struct nfsrvhashhead *, nfsrvudphashtbl);
100 NFSD_VNET_DEFINE(struct nfsrchash_bucket *, nfsrchash_table);
101 NFSD_VNET_DEFINE(struct nfsrchash_bucket *, nfsrcahash_table);
102 NFSD_VNET_DEFINE(struct nfsrvfh, nfs_rootfh);
103 NFSD_VNET_DEFINE(int, nfs_rootfhset) = 0;
104 NFSD_VNET_DEFINE(struct callout, nfsd_callout);
105 NFSD_VNET_DEFINE_STATIC(struct mount *, nfsv4root_mnt);
106 NFSD_VNET_DEFINE_STATIC(struct vfsoptlist, nfsv4root_opt);
107 NFSD_VNET_DEFINE_STATIC(struct vfsoptlist, nfsv4root_newopt);
108 NFSD_VNET_DEFINE_STATIC(bool, nfsrv_suspend_nfsd) = false;
109 NFSD_VNET_DEFINE_STATIC(bool, nfsrv_mntinited) = false;
110 
111 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *,
112     struct ucred *);
113 static void nfsvno_updateds(struct vnode *, struct ucred *, struct thread *);
114 
115 int nfsrv_enable_crossmntpt = 1;
116 static int nfs_commit_blks;
117 static int nfs_commit_miss;
118 extern int nfsrv_issuedelegs;
119 extern int nfsrv_dolocallocks;
120 extern struct nfsdevicehead nfsrv_devidhead;
121 
122 /* Map d_type to vnode type. */
123 static uint8_t dtype_to_vnode[DT_WHT + 1] = { VNON, VFIFO, VCHR, VNON, VDIR,
124     VNON, VBLK, VNON, VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON };
125 #define	NFS_DTYPETOVTYPE(t)	((t) <= DT_WHT ? dtype_to_vnode[(t)] : VNON)
126 
127 static int nfsrv_createiovec(int, struct mbuf **, struct mbuf **,
128     struct iovec **);
129 static int nfsrv_createiovec_extpgs(int, int, struct mbuf **,
130     struct mbuf **, struct iovec **);
131 static int nfsrv_createiovecw(int, struct mbuf *, char *, struct iovec **,
132     int *);
133 static void nfsrv_pnfscreate(struct vnode *, struct vattr *, struct ucred *,
134     NFSPROC_T *);
135 static void nfsrv_pnfsremovesetup(struct vnode *, NFSPROC_T *, struct vnode **,
136     int *, char *, fhandle_t *);
137 static void nfsrv_pnfsremove(struct vnode **, int, char *, fhandle_t *,
138     NFSPROC_T *);
139 static int nfsrv_proxyds(struct vnode *, off_t, int, struct ucred *,
140     struct thread *, int, struct mbuf **, char *, struct mbuf **,
141     struct nfsvattr *, struct acl *, off_t *, int, bool *);
142 static int nfsrv_setextattr(struct vnode *, struct nfsvattr *, NFSPROC_T *);
143 static int nfsrv_readdsrpc(fhandle_t *, off_t, int, struct ucred *,
144     NFSPROC_T *, struct nfsmount *, struct mbuf **, struct mbuf **);
145 static int nfsrv_writedsrpc(fhandle_t *, off_t, int, struct ucred *,
146     NFSPROC_T *, struct vnode *, struct nfsmount **, int, struct mbuf **,
147     char *, int *);
148 static int nfsrv_allocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
149     NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
150 static int nfsrv_deallocatedsrpc(fhandle_t *, off_t, off_t, struct ucred *,
151     NFSPROC_T *, struct vnode *, struct nfsmount **, int, int *);
152 static int nfsrv_setacldsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
153     struct vnode *, struct nfsmount **, int, struct acl *, int *);
154 static int nfsrv_setattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
155     struct vnode *, struct nfsmount **, int, struct nfsvattr *, int *);
156 static int nfsrv_getattrdsrpc(fhandle_t *, struct ucred *, NFSPROC_T *,
157     struct vnode *, struct nfsmount *, struct nfsvattr *);
158 static int nfsrv_seekdsrpc(fhandle_t *, off_t *, int, bool *, struct ucred *,
159     NFSPROC_T *, struct nfsmount *);
160 static int nfsrv_putfhname(fhandle_t *, char *);
161 static int nfsrv_pnfslookupds(struct vnode *, struct vnode *,
162     struct pnfsdsfile *, struct vnode **, NFSPROC_T *);
163 static void nfsrv_pnfssetfh(struct vnode *, struct pnfsdsfile *, char *, char *,
164     struct vnode *, NFSPROC_T *);
165 static int nfsrv_dsremove(struct vnode *, char *, struct ucred *, NFSPROC_T *);
166 static int nfsrv_dssetacl(struct vnode *, struct acl *, struct ucred *,
167     NFSPROC_T *);
168 static int nfsrv_pnfsstatfs(struct statfs *, struct mount *);
169 
170 int nfs_pnfsio(task_fn_t *, void *);
171 
172 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
173     "NFS server");
174 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW,
175     &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points");
176 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks,
177     0, "");
178 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss,
179     0, "");
180 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW,
181     &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations");
182 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel,
183     0, "Debug level for NFS server");
184 NFSD_VNET_DECLARE(int, nfsd_enable_stringtouid);
185 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid,
186     CTLFLAG_NFSD_VNET | CTLFLAG_RW, &NFSD_VNET_NAME(nfsd_enable_stringtouid),
187     0, "Enable nfsd to accept numeric owner_names");
188 static int nfsrv_pnfsgetdsattr = 1;
189 SYSCTL_INT(_vfs_nfsd, OID_AUTO, pnfsgetdsattr, CTLFLAG_RW,
190     &nfsrv_pnfsgetdsattr, 0, "When set getattr gets DS attributes via RPC");
191 
192 /*
193  * nfsrv_dsdirsize can only be increased and only when the nfsd threads are
194  * not running.
195  * The dsN subdirectories for the increased values must have been created
196  * on all DS servers before this increase is done.
197  */
198 u_int	nfsrv_dsdirsize = 20;
199 static int
sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)200 sysctl_dsdirsize(SYSCTL_HANDLER_ARGS)
201 {
202 	int error, newdsdirsize;
203 
204 	newdsdirsize = nfsrv_dsdirsize;
205 	error = sysctl_handle_int(oidp, &newdsdirsize, 0, req);
206 	if (error != 0 || req->newptr == NULL)
207 		return (error);
208 	if (newdsdirsize <= nfsrv_dsdirsize || newdsdirsize > 10000 ||
209 	    newnfs_numnfsd != 0)
210 		return (EINVAL);
211 	nfsrv_dsdirsize = newdsdirsize;
212 	return (0);
213 }
214 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, dsdirsize,
215     CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(nfsrv_dsdirsize),
216     sysctl_dsdirsize, "IU", "Number of dsN subdirs on the DS servers");
217 
218 /*
219  * nfs_srvmaxio can only be increased and only when the nfsd threads are
220  * not running.  The setting must be a power of 2, with the current limit of
221  * 1Mbyte.
222  */
223 static int
sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)224 sysctl_srvmaxio(SYSCTL_HANDLER_ARGS)
225 {
226 	int error;
227 	u_int newsrvmaxio;
228 	uint64_t tval;
229 
230 	newsrvmaxio = nfs_srvmaxio;
231 	error = sysctl_handle_int(oidp, &newsrvmaxio, 0, req);
232 	if (error != 0 || req->newptr == NULL)
233 		return (error);
234 	if (newsrvmaxio == nfs_srvmaxio)
235 		return (0);
236 	if (newsrvmaxio < nfs_srvmaxio) {
237 		printf("nfsd: vfs.nfsd.srvmaxio can only be increased\n");
238 		return (EINVAL);
239 	}
240 	if (newsrvmaxio > 1048576) {
241 		printf("nfsd: vfs.nfsd.srvmaxio cannot be > 1Mbyte\n");
242 		return (EINVAL);
243 	}
244 	if ((newsrvmaxio & (newsrvmaxio - 1)) != 0) {
245 		printf("nfsd: vfs.nfsd.srvmaxio must be a power of 2\n");
246 		return (EINVAL);
247 	}
248 
249 	/*
250 	 * Check that kern.ipc.maxsockbuf is large enough for
251 	 * newsrviomax, given the setting of vfs.nfs.bufpackets.
252 	 */
253 	if ((newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets >
254 	    sb_max_adj) {
255 		/*
256 		 * Suggest vfs.nfs.bufpackets * maximum RPC message for
257 		 * sb_max_adj.
258 		 */
259 		tval = (newsrvmaxio + NFS_MAXXDR) * nfs_bufpackets;
260 
261 		/*
262 		 * Convert suggested sb_max_adj value to a suggested
263 		 * sb_max value, which is what is set via kern.ipc.maxsockbuf.
264 		 * Perform the inverse calculation of (from uipc_sockbuf.c):
265 		 * sb_max_adj = (u_quad_t)sb_max * MCLBYTES /
266 		 *     (MSIZE + MCLBYTES);
267 		 * XXX If the calculation of sb_max_adj from sb_max changes,
268 		 *     this calculation must be changed as well.
269 		 */
270 		tval *= (MSIZE + MCLBYTES);  /* Brackets for readability. */
271 		tval += MCLBYTES - 1;        /* Round up divide. */
272 		tval /= MCLBYTES;
273 		printf("nfsd: set kern.ipc.maxsockbuf to a minimum of "
274 		    "%ju to support %ubyte NFS I/O\n", (uintmax_t)tval,
275 		    newsrvmaxio);
276 		return (EINVAL);
277 	}
278 
279 	NFSD_LOCK();
280 	if (newnfs_numnfsd != 0) {
281 		NFSD_UNLOCK();
282 		printf("nfsd: cannot set vfs.nfsd.srvmaxio when nfsd "
283 		    "threads are running\n");
284 		return (EINVAL);
285 	}
286 
287 
288 	nfs_srvmaxio = newsrvmaxio;
289 	NFSD_UNLOCK();
290 	return (0);
291 }
292 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, srvmaxio,
293     CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
294     sysctl_srvmaxio, "IU", "Maximum I/O size in bytes");
295 
296 static int
sysctl_dolocallocks(SYSCTL_HANDLER_ARGS)297 sysctl_dolocallocks(SYSCTL_HANDLER_ARGS)
298 {
299 	int error, igotlock, newdolocallocks;
300 
301 	newdolocallocks = nfsrv_dolocallocks;
302 	error = sysctl_handle_int(oidp, &newdolocallocks, 0, req);
303 	if (error != 0 || req->newptr == NULL)
304 		return (error);
305 	if (newdolocallocks == nfsrv_dolocallocks)
306 		return (0);
307 	if (jailed(curthread->td_ucred))
308 		return (EINVAL);
309 
310 	NFSLOCKV4ROOTMUTEX();
311 	do {
312 		igotlock = nfsv4_lock(&nfsv4rootfs_lock, 1, NULL,
313 		    NFSV4ROOTLOCKMUTEXPTR, NULL);
314 	} while (!igotlock);
315 	NFSUNLOCKV4ROOTMUTEX();
316 
317 	nfsrv_dolocallocks = newdolocallocks;
318 
319 	NFSLOCKV4ROOTMUTEX();
320 	nfsv4_unlock(&nfsv4rootfs_lock, 0);
321 	NFSUNLOCKV4ROOTMUTEX();
322 	return (0);
323 }
324 SYSCTL_PROC(_vfs_nfsd, OID_AUTO, enable_locallocks,
325     CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
326     sysctl_dolocallocks, "IU", "Enable nfsd to acquire local locks on files");
327 
328 #define	MAX_REORDERED_RPC	16
329 #define	NUM_HEURISTIC		1031
330 #define	NHUSE_INIT		64
331 #define	NHUSE_INC		16
332 #define	NHUSE_MAX		2048
333 
334 static struct nfsheur {
335 	struct vnode *nh_vp;	/* vp to match (unreferenced pointer) */
336 	off_t nh_nextoff;	/* next offset for sequential detection */
337 	int nh_use;		/* use count for selection */
338 	int nh_seqcount;	/* heuristic */
339 } nfsheur[NUM_HEURISTIC];
340 
341 /*
342  * Heuristic to detect sequential operation.
343  */
344 static struct nfsheur *
nfsrv_sequential_heuristic(struct uio * uio,struct vnode * vp)345 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp)
346 {
347 	struct nfsheur *nh;
348 	int hi, try;
349 
350 	/* Locate best candidate. */
351 	try = 32;
352 	hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC;
353 	nh = &nfsheur[hi];
354 	while (try--) {
355 		if (nfsheur[hi].nh_vp == vp) {
356 			nh = &nfsheur[hi];
357 			break;
358 		}
359 		if (nfsheur[hi].nh_use > 0)
360 			--nfsheur[hi].nh_use;
361 		hi = (hi + 1) % NUM_HEURISTIC;
362 		if (nfsheur[hi].nh_use < nh->nh_use)
363 			nh = &nfsheur[hi];
364 	}
365 
366 	/* Initialize hint if this is a new file. */
367 	if (nh->nh_vp != vp) {
368 		nh->nh_vp = vp;
369 		nh->nh_nextoff = uio->uio_offset;
370 		nh->nh_use = NHUSE_INIT;
371 		if (uio->uio_offset == 0)
372 			nh->nh_seqcount = 4;
373 		else
374 			nh->nh_seqcount = 1;
375 	}
376 
377 	/* Calculate heuristic. */
378 	if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) ||
379 	    uio->uio_offset == nh->nh_nextoff) {
380 		/* See comments in vfs_vnops.c:sequential_heuristic(). */
381 		nh->nh_seqcount += howmany(uio->uio_resid, 16384);
382 		if (nh->nh_seqcount > IO_SEQMAX)
383 			nh->nh_seqcount = IO_SEQMAX;
384 	} else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC *
385 	    imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) {
386 		/* Probably a reordered RPC, leave seqcount alone. */
387 	} else if (nh->nh_seqcount > 1) {
388 		nh->nh_seqcount /= 2;
389 	} else {
390 		nh->nh_seqcount = 0;
391 	}
392 	nh->nh_use += NHUSE_INC;
393 	if (nh->nh_use > NHUSE_MAX)
394 		nh->nh_use = NHUSE_MAX;
395 	return (nh);
396 }
397 
398 /*
399  * Get attributes into nfsvattr structure.
400  */
401 int
nfsvno_getattr(struct vnode * vp,struct nfsvattr * nvap,struct nfsrv_descript * nd,struct thread * p,int vpislocked,nfsattrbit_t * attrbitp)402 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap,
403     struct nfsrv_descript *nd, struct thread *p, int vpislocked,
404     nfsattrbit_t *attrbitp)
405 {
406 	int error, gotattr, lockedit = 0;
407 	struct nfsvattr na;
408 
409 	if (vpislocked == 0) {
410 		/*
411 		 * When vpislocked == 0, the vnode is either exclusively
412 		 * locked by this thread or not locked by this thread.
413 		 * As such, shared lock it, if not exclusively locked.
414 		 */
415 		if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
416 			lockedit = 1;
417 			NFSVOPLOCK(vp, LK_SHARED | LK_RETRY);
418 		}
419 	}
420 
421 	/*
422 	 * Acquire the Change, Size, TimeAccess, TimeModify and SpaceUsed
423 	 * attributes, as required.
424 	 * This needs to be done for regular files if:
425 	 * - non-NFSv4 RPCs or
426 	 * - when attrbitp == NULL or
427 	 * - an NFSv4 RPC with any of the above attributes in attrbitp.
428 	 * A return of 0 for nfsrv_proxyds() indicates that it has acquired
429 	 * these attributes.  nfsrv_proxyds() will return an error if the
430 	 * server is not a pNFS one.
431 	 */
432 	gotattr = 0;
433 	if (vp->v_type == VREG && nfsrv_devidcnt > 0 && (attrbitp == NULL ||
434 	    (nd->nd_flag & ND_NFSV4) == 0 ||
435 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_CHANGE) ||
436 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SIZE) ||
437 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEACCESS) ||
438 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_TIMEMODIFY) ||
439 	    NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEUSED))) {
440 		error = nfsrv_proxyds(vp, 0, 0, nd->nd_cred, p,
441 		    NFSPROC_GETATTR, NULL, NULL, NULL, &na, NULL, NULL, 0,
442 		    NULL);
443 		if (error == 0)
444 			gotattr = 1;
445 	}
446 
447 	error = VOP_GETATTR(vp, &nvap->na_vattr, nd->nd_cred);
448 	if (lockedit != 0)
449 		NFSVOPUNLOCK(vp);
450 
451 	/*
452 	 * If we got the Change, Size and Modify Time from the DS,
453 	 * replace them.
454 	 */
455 	if (gotattr != 0) {
456 		nvap->na_atime = na.na_atime;
457 		nvap->na_mtime = na.na_mtime;
458 		nvap->na_filerev = na.na_filerev;
459 		nvap->na_size = na.na_size;
460 		nvap->na_bytes = na.na_bytes;
461 	}
462 	NFSD_DEBUG(4, "nfsvno_getattr: gotattr=%d err=%d chg=%ju\n", gotattr,
463 	    error, (uintmax_t)na.na_filerev);
464 
465 	NFSEXITCODE(error);
466 	return (error);
467 }
468 
469 /*
470  * Get a file handle for a vnode.
471  */
472 int
nfsvno_getfh(struct vnode * vp,fhandle_t * fhp,struct thread * p)473 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p)
474 {
475 	int error;
476 
477 	NFSBZERO((caddr_t)fhp, sizeof(fhandle_t));
478 	fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid;
479 	error = VOP_VPTOFH(vp, &fhp->fh_fid);
480 
481 	NFSEXITCODE(error);
482 	return (error);
483 }
484 
485 /*
486  * Perform access checking for vnodes obtained from file handles that would
487  * refer to files already opened by a Unix client. You cannot just use
488  * vn_writechk() and VOP_ACCESSX() for two reasons.
489  * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write
490  *     case.
491  * 2 - The owner is to be given access irrespective of mode bits for some
492  *     operations, so that processes that chmod after opening a file don't
493  *     break.
494  */
495 int
nfsvno_accchk(struct vnode * vp,accmode_t accmode,struct ucred * cred,struct nfsexstuff * exp,struct thread * p,int override,int vpislocked,u_int32_t * supportedtypep)496 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred,
497     struct nfsexstuff *exp, struct thread *p, int override, int vpislocked,
498     u_int32_t *supportedtypep)
499 {
500 	struct vattr vattr;
501 	int error = 0, getret = 0;
502 
503 	if (vpislocked == 0) {
504 		if (NFSVOPLOCK(vp, LK_SHARED) != 0) {
505 			error = EPERM;
506 			goto out;
507 		}
508 	}
509 	if (accmode & VWRITE) {
510 		/* Just vn_writechk() changed to check rdonly */
511 		/*
512 		 * Disallow write attempts on read-only file systems;
513 		 * unless the file is a socket or a block or character
514 		 * device resident on the file system.
515 		 */
516 		if (NFSVNO_EXRDONLY(exp) ||
517 		    (vp->v_mount->mnt_flag & MNT_RDONLY)) {
518 			switch (vp->v_type) {
519 			case VREG:
520 			case VDIR:
521 			case VLNK:
522 				error = EROFS;
523 			default:
524 				break;
525 			}
526 		}
527 		/*
528 		 * If there's shared text associated with
529 		 * the inode, try to free it up once.  If
530 		 * we fail, we can't allow writing.
531 		 */
532 		if (VOP_IS_TEXT(vp) && error == 0)
533 			error = ETXTBSY;
534 	}
535 	if (error != 0) {
536 		if (vpislocked == 0)
537 			NFSVOPUNLOCK(vp);
538 		goto out;
539 	}
540 
541 	/*
542 	 * Should the override still be applied when ACLs are enabled?
543 	 */
544 	error = VOP_ACCESSX(vp, accmode, cred, p);
545 	if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) {
546 		/*
547 		 * Try again with VEXPLICIT_DENY, to see if the test for
548 		 * deletion is supported.
549 		 */
550 		error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p);
551 		if (error == 0) {
552 			if (vp->v_type == VDIR) {
553 				accmode &= ~(VDELETE | VDELETE_CHILD);
554 				accmode |= VWRITE;
555 				error = VOP_ACCESSX(vp, accmode, cred, p);
556 			} else if (supportedtypep != NULL) {
557 				*supportedtypep &= ~NFSACCESS_DELETE;
558 			}
559 		}
560 	}
561 
562 	/*
563 	 * Allow certain operations for the owner (reads and writes
564 	 * on files that are already open).
565 	 */
566 	if (override != NFSACCCHK_NOOVERRIDE &&
567 	    (error == EPERM || error == EACCES)) {
568 		if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT))
569 			error = 0;
570 		else if (override & NFSACCCHK_ALLOWOWNER) {
571 			getret = VOP_GETATTR(vp, &vattr, cred);
572 			if (getret == 0 && cred->cr_uid == vattr.va_uid)
573 				error = 0;
574 		}
575 	}
576 	if (vpislocked == 0)
577 		NFSVOPUNLOCK(vp);
578 
579 out:
580 	NFSEXITCODE(error);
581 	return (error);
582 }
583 
584 /*
585  * Set attribute(s) vnop.
586  */
587 int
nfsvno_setattr(struct vnode * vp,struct nfsvattr * nvap,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)588 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred,
589     struct thread *p, struct nfsexstuff *exp)
590 {
591 	u_quad_t savsize = 0;
592 	int error, savedit;
593 	time_t savbtime;
594 
595 	/*
596 	 * If this is an exported file system and a pNFS service is running,
597 	 * don't VOP_SETATTR() of size for the MDS file system.
598 	 */
599 	savedit = 0;
600 	error = 0;
601 	if (vp->v_type == VREG && (vp->v_mount->mnt_flag & MNT_EXPORTED) != 0 &&
602 	    nfsrv_devidcnt != 0 && nvap->na_vattr.va_size != VNOVAL &&
603 	    nvap->na_vattr.va_size > 0) {
604 		savsize = nvap->na_vattr.va_size;
605 		nvap->na_vattr.va_size = VNOVAL;
606 		if (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
607 		    nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
608 		    nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
609 		    nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
610 		    nvap->na_vattr.va_mtime.tv_sec != VNOVAL)
611 			savedit = 1;
612 		else
613 			savedit = 2;
614 	}
615 	if (savedit != 2)
616 		error = VOP_SETATTR(vp, &nvap->na_vattr, cred);
617 	if (savedit != 0)
618 		nvap->na_vattr.va_size = savsize;
619 	if (error == 0 && (nvap->na_vattr.va_uid != (uid_t)VNOVAL ||
620 	    nvap->na_vattr.va_gid != (gid_t)VNOVAL ||
621 	    nvap->na_vattr.va_size != VNOVAL ||
622 	    nvap->na_vattr.va_mode != (mode_t)VNOVAL ||
623 	    nvap->na_vattr.va_atime.tv_sec != VNOVAL ||
624 	    nvap->na_vattr.va_mtime.tv_sec != VNOVAL)) {
625 		/* Never modify birthtime on a DS file. */
626 		savbtime = nvap->na_vattr.va_birthtime.tv_sec;
627 		nvap->na_vattr.va_birthtime.tv_sec = VNOVAL;
628 		/* For a pNFS server, set the attributes on the DS file. */
629 		error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETATTR,
630 		    NULL, NULL, NULL, nvap, NULL, NULL, 0, NULL);
631 		nvap->na_vattr.va_birthtime.tv_sec = savbtime;
632 		if (error == ENOENT)
633 			error = 0;
634 	}
635 	NFSEXITCODE(error);
636 	return (error);
637 }
638 
639 /*
640  * Set up nameidata for a lookup() call and do it.
641  */
642 int
nfsvno_namei(struct nfsrv_descript * nd,struct nameidata * ndp,struct vnode * dp,int islocked,struct nfsexstuff * exp,struct vnode ** retdirp)643 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp,
644     struct vnode *dp, int islocked, struct nfsexstuff *exp,
645     struct vnode **retdirp)
646 {
647 	struct componentname *cnp = &ndp->ni_cnd;
648 	int i;
649 	struct iovec aiov;
650 	struct uio auio;
651 	int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen;
652 	int error = 0;
653 	char *cp;
654 
655 	*retdirp = NULL;
656 	cnp->cn_nameptr = cnp->cn_pnbuf;
657 	ndp->ni_lcf = 0;
658 	/*
659 	 * Extract and set starting directory.
660 	 */
661 	if (dp->v_type != VDIR) {
662 		if (islocked)
663 			vput(dp);
664 		else
665 			vrele(dp);
666 		nfsvno_relpathbuf(ndp);
667 		error = ENOTDIR;
668 		goto out1;
669 	}
670 	if (islocked)
671 		NFSVOPUNLOCK(dp);
672 	VREF(dp);
673 	*retdirp = dp;
674 	if (NFSVNO_EXRDONLY(exp))
675 		cnp->cn_flags |= RDONLY;
676 	ndp->ni_segflg = UIO_SYSSPACE;
677 
678 	if (nd->nd_flag & ND_PUBLOOKUP) {
679 		ndp->ni_loopcnt = 0;
680 		if (cnp->cn_pnbuf[0] == '/') {
681 			vrele(dp);
682 			/*
683 			 * Check for degenerate pathnames here, since lookup()
684 			 * panics on them.
685 			 */
686 			for (i = 1; i < ndp->ni_pathlen; i++)
687 				if (cnp->cn_pnbuf[i] != '/')
688 					break;
689 			if (i == ndp->ni_pathlen) {
690 				error = NFSERR_ACCES;
691 				goto out;
692 			}
693 			dp = rootvnode;
694 			VREF(dp);
695 		}
696 	} else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) ||
697 	    (nd->nd_flag & ND_NFSV4) == 0) {
698 		/*
699 		 * Only cross mount points for NFSv4 when doing a
700 		 * mount while traversing the file system above
701 		 * the mount point, unless nfsrv_enable_crossmntpt is set.
702 		 */
703 		cnp->cn_flags |= NOCROSSMOUNT;
704 	}
705 
706 	/*
707 	 * Initialize for scan, set ni_startdir and bump ref on dp again
708 	 * because lookup() will dereference ni_startdir.
709 	 */
710 
711 	ndp->ni_startdir = dp;
712 	ndp->ni_rootdir = rootvnode;
713 	ndp->ni_topdir = NULL;
714 
715 	if (!lockleaf)
716 		cnp->cn_flags |= LOCKLEAF;
717 	for (;;) {
718 		cnp->cn_nameptr = cnp->cn_pnbuf;
719 		/*
720 		 * Call lookup() to do the real work.  If an error occurs,
721 		 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and
722 		 * we do not have to dereference anything before returning.
723 		 * In either case ni_startdir will be dereferenced and NULLed
724 		 * out.
725 		 */
726 		error = vfs_lookup(ndp);
727 		if (error)
728 			break;
729 
730 		/*
731 		 * Check for encountering a symbolic link.  Trivial
732 		 * termination occurs if no symlink encountered.
733 		 */
734 		if ((cnp->cn_flags & ISSYMLINK) == 0) {
735 			if (ndp->ni_vp && !lockleaf)
736 				NFSVOPUNLOCK(ndp->ni_vp);
737 			break;
738 		}
739 
740 		/*
741 		 * Validate symlink
742 		 */
743 		if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
744 			NFSVOPUNLOCK(ndp->ni_dvp);
745 		if (!(nd->nd_flag & ND_PUBLOOKUP)) {
746 			error = EINVAL;
747 			goto badlink2;
748 		}
749 
750 		if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
751 			error = ELOOP;
752 			goto badlink2;
753 		}
754 		if (ndp->ni_pathlen > 1)
755 			cp = uma_zalloc(namei_zone, M_WAITOK);
756 		else
757 			cp = cnp->cn_pnbuf;
758 		aiov.iov_base = cp;
759 		aiov.iov_len = MAXPATHLEN;
760 		auio.uio_iov = &aiov;
761 		auio.uio_iovcnt = 1;
762 		auio.uio_offset = 0;
763 		auio.uio_rw = UIO_READ;
764 		auio.uio_segflg = UIO_SYSSPACE;
765 		auio.uio_td = NULL;
766 		auio.uio_resid = MAXPATHLEN;
767 		error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
768 		if (error) {
769 		badlink1:
770 			if (ndp->ni_pathlen > 1)
771 				uma_zfree(namei_zone, cp);
772 		badlink2:
773 			vrele(ndp->ni_dvp);
774 			vput(ndp->ni_vp);
775 			break;
776 		}
777 		linklen = MAXPATHLEN - auio.uio_resid;
778 		if (linklen == 0) {
779 			error = ENOENT;
780 			goto badlink1;
781 		}
782 		if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
783 			error = ENAMETOOLONG;
784 			goto badlink1;
785 		}
786 
787 		/*
788 		 * Adjust or replace path
789 		 */
790 		if (ndp->ni_pathlen > 1) {
791 			NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
792 			uma_zfree(namei_zone, cnp->cn_pnbuf);
793 			cnp->cn_pnbuf = cp;
794 		} else
795 			cnp->cn_pnbuf[linklen] = '\0';
796 		ndp->ni_pathlen += linklen;
797 
798 		/*
799 		 * Cleanup refs for next loop and check if root directory
800 		 * should replace current directory.  Normally ni_dvp
801 		 * becomes the new base directory and is cleaned up when
802 		 * we loop.  Explicitly null pointers after invalidation
803 		 * to clarify operation.
804 		 */
805 		vput(ndp->ni_vp);
806 		ndp->ni_vp = NULL;
807 
808 		if (cnp->cn_pnbuf[0] == '/') {
809 			vrele(ndp->ni_dvp);
810 			ndp->ni_dvp = ndp->ni_rootdir;
811 			VREF(ndp->ni_dvp);
812 		}
813 		ndp->ni_startdir = ndp->ni_dvp;
814 		ndp->ni_dvp = NULL;
815 	}
816 	if (!lockleaf)
817 		cnp->cn_flags &= ~LOCKLEAF;
818 
819 out:
820 	if (error) {
821 		nfsvno_relpathbuf(ndp);
822 		ndp->ni_vp = NULL;
823 		ndp->ni_dvp = NULL;
824 		ndp->ni_startdir = NULL;
825 	} else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) {
826 		ndp->ni_dvp = NULL;
827 	}
828 
829 out1:
830 	NFSEXITCODE2(error, nd);
831 	return (error);
832 }
833 
834 /*
835  * Set up a pathname buffer and return a pointer to it and, optionally
836  * set a hash pointer.
837  */
838 void
nfsvno_setpathbuf(struct nameidata * ndp,char ** bufpp,u_long ** hashpp)839 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp)
840 {
841 	struct componentname *cnp = &ndp->ni_cnd;
842 
843 	cnp->cn_flags |= (NOMACCHECK);
844 	cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
845 	if (hashpp != NULL)
846 		*hashpp = NULL;
847 	*bufpp = cnp->cn_pnbuf;
848 }
849 
850 /*
851  * Release the above path buffer, if not released by nfsvno_namei().
852  */
853 void
nfsvno_relpathbuf(struct nameidata * ndp)854 nfsvno_relpathbuf(struct nameidata *ndp)
855 {
856 
857 	uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
858 	ndp->ni_cnd.cn_pnbuf = NULL;
859 }
860 
861 /*
862  * Readlink vnode op into an mbuf list.
863  */
864 int
nfsvno_readlink(struct vnode * vp,struct ucred * cred,int maxextsiz,struct thread * p,struct mbuf ** mpp,struct mbuf ** mpendp,int * lenp)865 nfsvno_readlink(struct vnode *vp, struct ucred *cred, int maxextsiz,
866     struct thread *p, struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
867 {
868 	struct iovec *iv;
869 	struct uio io, *uiop = &io;
870 	struct mbuf *mp, *mp3;
871 	int len, tlen, error = 0;
872 
873 	len = NFS_MAXPATHLEN;
874 	if (maxextsiz > 0)
875 		uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
876 		    &mp3, &mp, &iv);
877 	else
878 		uiop->uio_iovcnt = nfsrv_createiovec(len, &mp3, &mp, &iv);
879 	uiop->uio_iov = iv;
880 	uiop->uio_offset = 0;
881 	uiop->uio_resid = len;
882 	uiop->uio_rw = UIO_READ;
883 	uiop->uio_segflg = UIO_SYSSPACE;
884 	uiop->uio_td = NULL;
885 	error = VOP_READLINK(vp, uiop, cred);
886 	free(iv, M_TEMP);
887 	if (error) {
888 		m_freem(mp3);
889 		*lenp = 0;
890 		goto out;
891 	}
892 	if (uiop->uio_resid > 0) {
893 		len -= uiop->uio_resid;
894 		tlen = NFSM_RNDUP(len);
895 		if (tlen == 0) {
896 			m_freem(mp3);
897 			mp3 = mp = NULL;
898 		} else if (tlen != NFS_MAXPATHLEN || tlen != len)
899 			mp = nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen,
900 			    tlen - len);
901 	}
902 	*lenp = len;
903 	*mpp = mp3;
904 	*mpendp = mp;
905 
906 out:
907 	NFSEXITCODE(error);
908 	return (error);
909 }
910 
911 /*
912  * Create an mbuf chain and an associated iovec that can be used to Read
913  * or Getextattr of data.
914  * Upon success, return pointers to the first and last mbufs in the chain
915  * plus the malloc'd iovec and its iovlen.
916  */
917 static int
nfsrv_createiovec(int len,struct mbuf ** mpp,struct mbuf ** mpendp,struct iovec ** ivp)918 nfsrv_createiovec(int len, struct mbuf **mpp, struct mbuf **mpendp,
919     struct iovec **ivp)
920 {
921 	struct mbuf *m, *m2 = NULL, *m3;
922 	struct iovec *iv;
923 	int i, left, siz;
924 
925 	left = len;
926 	m3 = NULL;
927 	/*
928 	 * Generate the mbuf list with the uio_iov ref. to it.
929 	 */
930 	i = 0;
931 	while (left > 0) {
932 		NFSMGET(m);
933 		MCLGET(m, M_WAITOK);
934 		m->m_len = 0;
935 		siz = min(M_TRAILINGSPACE(m), left);
936 		left -= siz;
937 		i++;
938 		if (m3)
939 			m2->m_next = m;
940 		else
941 			m3 = m;
942 		m2 = m;
943 	}
944 	*ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
945 	m = m3;
946 	left = len;
947 	i = 0;
948 	while (left > 0) {
949 		if (m == NULL)
950 			panic("nfsrv_createiovec iov");
951 		siz = min(M_TRAILINGSPACE(m), left);
952 		if (siz > 0) {
953 			iv->iov_base = mtod(m, caddr_t) + m->m_len;
954 			iv->iov_len = siz;
955 			m->m_len += siz;
956 			left -= siz;
957 			iv++;
958 			i++;
959 		}
960 		m = m->m_next;
961 	}
962 	*mpp = m3;
963 	*mpendp = m2;
964 	return (i);
965 }
966 
967 /*
968  * Create an mbuf chain and an associated iovec that can be used to Read
969  * or Getextattr of data.
970  * Upon success, return pointers to the first and last mbufs in the chain
971  * plus the malloc'd iovec and its iovlen.
972  * Same as above, but creates ext_pgs mbuf(s).
973  */
974 static int
nfsrv_createiovec_extpgs(int len,int maxextsiz,struct mbuf ** mpp,struct mbuf ** mpendp,struct iovec ** ivp)975 nfsrv_createiovec_extpgs(int len, int maxextsiz, struct mbuf **mpp,
976     struct mbuf **mpendp, struct iovec **ivp)
977 {
978 	struct mbuf *m, *m2 = NULL, *m3;
979 	struct iovec *iv;
980 	int i, left, pgno, siz;
981 
982 	left = len;
983 	m3 = NULL;
984 	/*
985 	 * Generate the mbuf list with the uio_iov ref. to it.
986 	 */
987 	i = 0;
988 	while (left > 0) {
989 		siz = min(left, maxextsiz);
990 		m = mb_alloc_ext_plus_pages(siz, M_WAITOK);
991 		left -= siz;
992 		i += m->m_epg_npgs;
993 		if (m3 != NULL)
994 			m2->m_next = m;
995 		else
996 			m3 = m;
997 		m2 = m;
998 	}
999 	*ivp = iv = malloc(i * sizeof (struct iovec), M_TEMP, M_WAITOK);
1000 	m = m3;
1001 	left = len;
1002 	i = 0;
1003 	pgno = 0;
1004 	while (left > 0) {
1005 		if (m == NULL)
1006 			panic("nfsvno_createiovec_extpgs iov");
1007 		siz = min(PAGE_SIZE, left);
1008 		if (siz > 0) {
1009 			iv->iov_base = (void *)PHYS_TO_DMAP(m->m_epg_pa[pgno]);
1010 			iv->iov_len = siz;
1011 			m->m_len += siz;
1012 			if (pgno == m->m_epg_npgs - 1)
1013 				m->m_epg_last_len = siz;
1014 			left -= siz;
1015 			iv++;
1016 			i++;
1017 			pgno++;
1018 		}
1019 		if (pgno == m->m_epg_npgs && left > 0) {
1020 			m = m->m_next;
1021 			if (m == NULL)
1022 				panic("nfsvno_createiovec_extpgs iov");
1023 			pgno = 0;
1024 		}
1025 	}
1026 	*mpp = m3;
1027 	*mpendp = m2;
1028 	return (i);
1029 }
1030 
1031 /*
1032  * Read vnode op call into mbuf list.
1033  */
1034 int
nfsvno_read(struct vnode * vp,off_t off,int cnt,struct ucred * cred,int maxextsiz,struct thread * p,struct mbuf ** mpp,struct mbuf ** mpendp)1035 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
1036     int maxextsiz, struct thread *p, struct mbuf **mpp,
1037     struct mbuf **mpendp)
1038 {
1039 	struct mbuf *m;
1040 	struct iovec *iv;
1041 	int error = 0, len, tlen, ioflag = 0;
1042 	struct mbuf *m3;
1043 	struct uio io, *uiop = &io;
1044 	struct nfsheur *nh;
1045 
1046 	/*
1047 	 * Attempt to read from a DS file. A return of ENOENT implies
1048 	 * there is no DS file to read.
1049 	 */
1050 	error = nfsrv_proxyds(vp, off, cnt, cred, p, NFSPROC_READDS, mpp,
1051 	    NULL, mpendp, NULL, NULL, NULL, 0, NULL);
1052 	if (error != ENOENT)
1053 		return (error);
1054 
1055 	len = NFSM_RNDUP(cnt);
1056 	if (maxextsiz > 0)
1057 		uiop->uio_iovcnt = nfsrv_createiovec_extpgs(len, maxextsiz,
1058 		    &m3, &m, &iv);
1059 	else
1060 		uiop->uio_iovcnt = nfsrv_createiovec(len, &m3, &m, &iv);
1061 	uiop->uio_iov = iv;
1062 	uiop->uio_offset = off;
1063 	uiop->uio_resid = len;
1064 	uiop->uio_rw = UIO_READ;
1065 	uiop->uio_segflg = UIO_SYSSPACE;
1066 	uiop->uio_td = NULL;
1067 	nh = nfsrv_sequential_heuristic(uiop, vp);
1068 	ioflag |= nh->nh_seqcount << IO_SEQSHIFT;
1069 	/* XXX KDM make this more systematic? */
1070 	NFSD_VNET(nfsstatsv1_p)->srvbytes[NFSV4OP_READ] += uiop->uio_resid;
1071 	error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred);
1072 	free(iv, M_TEMP);
1073 	if (error) {
1074 		m_freem(m3);
1075 		*mpp = NULL;
1076 		goto out;
1077 	}
1078 	nh->nh_nextoff = uiop->uio_offset;
1079 	tlen = len - uiop->uio_resid;
1080 	cnt = cnt < tlen ? cnt : tlen;
1081 	tlen = NFSM_RNDUP(cnt);
1082 	if (tlen == 0) {
1083 		m_freem(m3);
1084 		m3 = m = NULL;
1085 	} else if (len != tlen || tlen != cnt)
1086 		m = nfsrv_adj(m3, len - tlen, tlen - cnt);
1087 	*mpp = m3;
1088 	*mpendp = m;
1089 
1090 out:
1091 	NFSEXITCODE(error);
1092 	return (error);
1093 }
1094 
1095 /*
1096  * Create the iovec for the mbuf chain passed in as an argument.
1097  * The "cp" argument is where the data starts within the first mbuf in
1098  * the chain. It returns the iovec and the iovcnt.
1099  */
1100 static int
nfsrv_createiovecw(int retlen,struct mbuf * m,char * cp,struct iovec ** ivpp,int * iovcntp)1101 nfsrv_createiovecw(int retlen, struct mbuf *m, char *cp, struct iovec **ivpp,
1102     int *iovcntp)
1103 {
1104 	struct mbuf *mp;
1105 	struct iovec *ivp;
1106 	int cnt, i, len;
1107 
1108 	/*
1109 	 * Loop through the mbuf chain, counting how many mbufs are a
1110 	 * part of this write operation, so the iovec size is known.
1111 	 */
1112 	cnt = 0;
1113 	len = retlen;
1114 	mp = m;
1115 	i = mtod(mp, caddr_t) + mp->m_len - cp;
1116 	while (len > 0) {
1117 		if (i > 0) {
1118 			len -= i;
1119 			cnt++;
1120 		}
1121 		mp = mp->m_next;
1122 		if (!mp) {
1123 			if (len > 0)
1124 				return (EBADRPC);
1125 		} else
1126 			i = mp->m_len;
1127 	}
1128 
1129 	/* Now, create the iovec. */
1130 	mp = m;
1131 	*ivpp = ivp = malloc(cnt * sizeof (struct iovec), M_TEMP,
1132 	    M_WAITOK);
1133 	*iovcntp = cnt;
1134 	i = mtod(mp, caddr_t) + mp->m_len - cp;
1135 	len = retlen;
1136 	while (len > 0) {
1137 		if (mp == NULL)
1138 			panic("nfsrv_createiovecw");
1139 		if (i > 0) {
1140 			i = min(i, len);
1141 			ivp->iov_base = cp;
1142 			ivp->iov_len = i;
1143 			ivp++;
1144 			len -= i;
1145 		}
1146 		mp = mp->m_next;
1147 		if (mp) {
1148 			i = mp->m_len;
1149 			cp = mtod(mp, caddr_t);
1150 		}
1151 	}
1152 	return (0);
1153 }
1154 
1155 /*
1156  * Write vnode op from an mbuf list.
1157  */
1158 int
nfsvno_write(struct vnode * vp,off_t off,int retlen,int * stable,struct mbuf * mp,char * cp,struct ucred * cred,struct thread * p)1159 nfsvno_write(struct vnode *vp, off_t off, int retlen, int *stable,
1160     struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p)
1161 {
1162 	struct iovec *iv;
1163 	int cnt, ioflags, error;
1164 	struct uio io, *uiop = &io;
1165 	struct nfsheur *nh;
1166 
1167 	/*
1168 	 * Attempt to write to a DS file. A return of ENOENT implies
1169 	 * there is no DS file to write.
1170 	 */
1171 	error = nfsrv_proxyds(vp, off, retlen, cred, p, NFSPROC_WRITEDS,
1172 	    &mp, cp, NULL, NULL, NULL, NULL, 0, NULL);
1173 	if (error != ENOENT) {
1174 		*stable = NFSWRITE_FILESYNC;
1175 		return (error);
1176 	}
1177 
1178 	if (*stable == NFSWRITE_UNSTABLE)
1179 		ioflags = IO_NODELOCKED;
1180 	else
1181 		ioflags = (IO_SYNC | IO_NODELOCKED);
1182 	error = nfsrv_createiovecw(retlen, mp, cp, &iv, &cnt);
1183 	if (error != 0)
1184 		return (error);
1185 	uiop->uio_iov = iv;
1186 	uiop->uio_iovcnt = cnt;
1187 	uiop->uio_resid = retlen;
1188 	uiop->uio_rw = UIO_WRITE;
1189 	uiop->uio_segflg = UIO_SYSSPACE;
1190 	NFSUIOPROC(uiop, p);
1191 	uiop->uio_offset = off;
1192 	nh = nfsrv_sequential_heuristic(uiop, vp);
1193 	ioflags |= nh->nh_seqcount << IO_SEQSHIFT;
1194 	/* XXX KDM make this more systematic? */
1195 	NFSD_VNET(nfsstatsv1_p)->srvbytes[NFSV4OP_WRITE] += uiop->uio_resid;
1196 	error = VOP_WRITE(vp, uiop, ioflags, cred);
1197 	if (error == 0)
1198 		nh->nh_nextoff = uiop->uio_offset;
1199 	free(iv, M_TEMP);
1200 
1201 	NFSEXITCODE(error);
1202 	return (error);
1203 }
1204 
1205 /*
1206  * Common code for creating a regular file (plus special files for V2).
1207  */
1208 int
nfsvno_createsub(struct nfsrv_descript * nd,struct nameidata * ndp,struct vnode ** vpp,struct nfsvattr * nvap,int * exclusive_flagp,int32_t * cverf,NFSDEV_T rdev,struct nfsexstuff * exp)1209 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp,
1210     struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp,
1211     int32_t *cverf, NFSDEV_T rdev, struct nfsexstuff *exp)
1212 {
1213 	u_quad_t tempsize;
1214 	int error;
1215 	struct thread *p = curthread;
1216 
1217 	error = nd->nd_repstat;
1218 	if (!error && ndp->ni_vp == NULL) {
1219 		if (nvap->na_type == VREG || nvap->na_type == VSOCK) {
1220 			error = VOP_CREATE(ndp->ni_dvp,
1221 			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1222 			/* For a pNFS server, create the data file on a DS. */
1223 			if (error == 0 && nvap->na_type == VREG) {
1224 				/*
1225 				 * Create a data file on a DS for a pNFS server.
1226 				 * This function just returns if not
1227 				 * running a pNFS DS or the creation fails.
1228 				 */
1229 				nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1230 				    nd->nd_cred, p);
1231 			}
1232 			VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1233 			    NULL, false);
1234 			nfsvno_relpathbuf(ndp);
1235 			if (!error) {
1236 				if (*exclusive_flagp) {
1237 					*exclusive_flagp = 0;
1238 					NFSVNO_ATTRINIT(nvap);
1239 					nvap->na_atime.tv_sec = cverf[0];
1240 					nvap->na_atime.tv_nsec = cverf[1];
1241 					error = VOP_SETATTR(ndp->ni_vp,
1242 					    &nvap->na_vattr, nd->nd_cred);
1243 					if (error != 0) {
1244 						vput(ndp->ni_vp);
1245 						ndp->ni_vp = NULL;
1246 						error = NFSERR_NOTSUPP;
1247 					}
1248 				}
1249 			}
1250 		/*
1251 		 * NFS V2 Only. nfsrvd_mknod() does this for V3.
1252 		 * (This implies, just get out on an error.)
1253 		 */
1254 		} else if (nvap->na_type == VCHR || nvap->na_type == VBLK ||
1255 			nvap->na_type == VFIFO) {
1256 			if (nvap->na_type == VCHR && rdev == 0xffffffff)
1257 				nvap->na_type = VFIFO;
1258                         if (nvap->na_type != VFIFO &&
1259 			    (error = priv_check_cred(nd->nd_cred, PRIV_VFS_MKNOD_DEV))) {
1260 				nfsvno_relpathbuf(ndp);
1261 				vput(ndp->ni_dvp);
1262 				goto out;
1263 			}
1264 			nvap->na_rdev = rdev;
1265 			error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1266 			    &ndp->ni_cnd, &nvap->na_vattr);
1267 			VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp :
1268 			    NULL, false);
1269 			nfsvno_relpathbuf(ndp);
1270 			if (error)
1271 				goto out;
1272 		} else {
1273 			nfsvno_relpathbuf(ndp);
1274 			vput(ndp->ni_dvp);
1275 			error = ENXIO;
1276 			goto out;
1277 		}
1278 		*vpp = ndp->ni_vp;
1279 	} else {
1280 		/*
1281 		 * Handle cases where error is already set and/or
1282 		 * the file exists.
1283 		 * 1 - clean up the lookup
1284 		 * 2 - iff !error and na_size set, truncate it
1285 		 */
1286 		nfsvno_relpathbuf(ndp);
1287 		*vpp = ndp->ni_vp;
1288 		if (ndp->ni_dvp == *vpp)
1289 			vrele(ndp->ni_dvp);
1290 		else
1291 			vput(ndp->ni_dvp);
1292 		if (!error && nvap->na_size != VNOVAL) {
1293 			error = nfsvno_accchk(*vpp, VWRITE,
1294 			    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
1295 			    NFSACCCHK_VPISLOCKED, NULL);
1296 			if (!error) {
1297 				tempsize = nvap->na_size;
1298 				NFSVNO_ATTRINIT(nvap);
1299 				nvap->na_size = tempsize;
1300 				error = nfsvno_setattr(*vpp, nvap,
1301 				    nd->nd_cred, p, exp);
1302 			}
1303 		}
1304 		if (error)
1305 			vput(*vpp);
1306 	}
1307 
1308 out:
1309 	NFSEXITCODE(error);
1310 	return (error);
1311 }
1312 
1313 /*
1314  * Do a mknod vnode op.
1315  */
1316 int
nfsvno_mknod(struct nameidata * ndp,struct nfsvattr * nvap,struct ucred * cred,struct thread * p)1317 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred,
1318     struct thread *p)
1319 {
1320 	int error = 0;
1321 	__enum_uint8(vtype) vtyp;
1322 
1323 	vtyp = nvap->na_type;
1324 	/*
1325 	 * Iff doesn't exist, create it.
1326 	 */
1327 	if (ndp->ni_vp) {
1328 		nfsvno_relpathbuf(ndp);
1329 		vput(ndp->ni_dvp);
1330 		vrele(ndp->ni_vp);
1331 		error = EEXIST;
1332 		goto out;
1333 	}
1334 	if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) {
1335 		nfsvno_relpathbuf(ndp);
1336 		vput(ndp->ni_dvp);
1337 		error = NFSERR_BADTYPE;
1338 		goto out;
1339 	}
1340 	if (vtyp == VSOCK) {
1341 		error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
1342 		    &ndp->ni_cnd, &nvap->na_vattr);
1343 		VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1344 		    false);
1345 		nfsvno_relpathbuf(ndp);
1346 	} else {
1347 		if (nvap->na_type != VFIFO &&
1348 		    (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV))) {
1349 			nfsvno_relpathbuf(ndp);
1350 			vput(ndp->ni_dvp);
1351 			goto out;
1352 		}
1353 		error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp,
1354 		    &ndp->ni_cnd, &nvap->na_vattr);
1355 		VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL,
1356 		    false);
1357 		nfsvno_relpathbuf(ndp);
1358 		/*
1359 		 * Since VOP_MKNOD returns the ni_vp, I can't
1360 		 * see any reason to do the lookup.
1361 		 */
1362 	}
1363 
1364 out:
1365 	NFSEXITCODE(error);
1366 	return (error);
1367 }
1368 
1369 /*
1370  * Mkdir vnode op.
1371  */
1372 int
nfsvno_mkdir(struct nameidata * ndp,struct nfsvattr * nvap,uid_t saved_uid,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1373 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid,
1374     struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1375 {
1376 	int error = 0;
1377 
1378 	if (ndp->ni_vp != NULL) {
1379 		if (ndp->ni_dvp == ndp->ni_vp)
1380 			vrele(ndp->ni_dvp);
1381 		else
1382 			vput(ndp->ni_dvp);
1383 		vrele(ndp->ni_vp);
1384 		nfsvno_relpathbuf(ndp);
1385 		error = EEXIST;
1386 		goto out;
1387 	}
1388 	error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1389 	    &nvap->na_vattr);
1390 	VOP_VPUT_PAIR(ndp->ni_dvp, error == 0 ? &ndp->ni_vp : NULL, false);
1391 	nfsvno_relpathbuf(ndp);
1392 
1393 out:
1394 	NFSEXITCODE(error);
1395 	return (error);
1396 }
1397 
1398 /*
1399  * symlink vnode op.
1400  */
1401 int
nfsvno_symlink(struct nameidata * ndp,struct nfsvattr * nvap,char * pathcp,int pathlen,int not_v2,uid_t saved_uid,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1402 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp,
1403     int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p,
1404     struct nfsexstuff *exp)
1405 {
1406 	int error = 0;
1407 
1408 	if (ndp->ni_vp) {
1409 		nfsvno_relpathbuf(ndp);
1410 		if (ndp->ni_dvp == ndp->ni_vp)
1411 			vrele(ndp->ni_dvp);
1412 		else
1413 			vput(ndp->ni_dvp);
1414 		vrele(ndp->ni_vp);
1415 		error = EEXIST;
1416 		goto out;
1417 	}
1418 
1419 	error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd,
1420 	    &nvap->na_vattr, pathcp);
1421 	/*
1422 	 * Although FreeBSD still had the lookup code in
1423 	 * it for 7/current, there doesn't seem to be any
1424 	 * point, since VOP_SYMLINK() returns the ni_vp.
1425 	 * Just vput it for v2.
1426 	 */
1427 	VOP_VPUT_PAIR(ndp->ni_dvp, &ndp->ni_vp, !not_v2 && error == 0);
1428 	nfsvno_relpathbuf(ndp);
1429 
1430 out:
1431 	NFSEXITCODE(error);
1432 	return (error);
1433 }
1434 
1435 /*
1436  * Parse symbolic link arguments.
1437  * This function has an ugly side effect. It will malloc() an area for
1438  * the symlink and set iov_base to point to it, only if it succeeds.
1439  * So, if it returns with uiop->uio_iov->iov_base != NULL, that must
1440  * be FREE'd later.
1441  */
1442 int
nfsvno_getsymlink(struct nfsrv_descript * nd,struct nfsvattr * nvap,struct thread * p,char ** pathcpp,int * lenp)1443 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap,
1444     struct thread *p, char **pathcpp, int *lenp)
1445 {
1446 	u_int32_t *tl;
1447 	char *pathcp = NULL;
1448 	int error = 0, len;
1449 	struct nfsv2_sattr *sp;
1450 
1451 	*pathcpp = NULL;
1452 	*lenp = 0;
1453 	if ((nd->nd_flag & ND_NFSV3) &&
1454 	    (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p)))
1455 		goto nfsmout;
1456 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1457 	len = fxdr_unsigned(int, *tl);
1458 	if (len > NFS_MAXPATHLEN || len <= 0) {
1459 		error = EBADRPC;
1460 		goto nfsmout;
1461 	}
1462 	pathcp = malloc(len + 1, M_TEMP, M_WAITOK);
1463 	error = nfsrv_mtostr(nd, pathcp, len);
1464 	if (error)
1465 		goto nfsmout;
1466 	if (nd->nd_flag & ND_NFSV2) {
1467 		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
1468 		nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode);
1469 	}
1470 	*pathcpp = pathcp;
1471 	*lenp = len;
1472 	NFSEXITCODE2(0, nd);
1473 	return (0);
1474 nfsmout:
1475 	if (pathcp)
1476 		free(pathcp, M_TEMP);
1477 	NFSEXITCODE2(error, nd);
1478 	return (error);
1479 }
1480 
1481 /*
1482  * Remove a non-directory object.
1483  */
1484 int
nfsvno_removesub(struct nameidata * ndp,int is_v4,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1485 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1486     struct thread *p, struct nfsexstuff *exp)
1487 {
1488 	struct vnode *vp, *dsdvp[NFSDEV_MAXMIRRORS];
1489 	int error = 0, mirrorcnt;
1490 	char fname[PNFS_FILENAME_LEN + 1];
1491 	fhandle_t fh;
1492 
1493 	vp = ndp->ni_vp;
1494 	dsdvp[0] = NULL;
1495 	if (vp->v_type == VDIR)
1496 		error = NFSERR_ISDIR;
1497 	else if (is_v4)
1498 		error = nfsrv_checkremove(vp, 1, NULL, (nfsquad_t)((u_quad_t)0),
1499 		    p);
1500 	if (error == 0)
1501 		nfsrv_pnfsremovesetup(vp, p, dsdvp, &mirrorcnt, fname, &fh);
1502 	if (!error)
1503 		error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd);
1504 	if (error == 0 && dsdvp[0] != NULL)
1505 		nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1506 	if (ndp->ni_dvp == vp)
1507 		vrele(ndp->ni_dvp);
1508 	else
1509 		vput(ndp->ni_dvp);
1510 	vput(vp);
1511 	nfsvno_relpathbuf(ndp);
1512 	NFSEXITCODE(error);
1513 	return (error);
1514 }
1515 
1516 /*
1517  * Remove a directory.
1518  */
1519 int
nfsvno_rmdirsub(struct nameidata * ndp,int is_v4,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1520 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred,
1521     struct thread *p, struct nfsexstuff *exp)
1522 {
1523 	struct vnode *vp;
1524 	int error = 0;
1525 
1526 	vp = ndp->ni_vp;
1527 	if (vp->v_type != VDIR) {
1528 		error = ENOTDIR;
1529 		goto out;
1530 	}
1531 	/*
1532 	 * No rmdir "." please.
1533 	 */
1534 	if (ndp->ni_dvp == vp) {
1535 		error = EINVAL;
1536 		goto out;
1537 	}
1538 	/*
1539 	 * The root of a mounted filesystem cannot be deleted.
1540 	 */
1541 	if (vp->v_vflag & VV_ROOT)
1542 		error = EBUSY;
1543 out:
1544 	if (!error)
1545 		error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd);
1546 	if (ndp->ni_dvp == vp)
1547 		vrele(ndp->ni_dvp);
1548 	else
1549 		vput(ndp->ni_dvp);
1550 	vput(vp);
1551 	nfsvno_relpathbuf(ndp);
1552 	NFSEXITCODE(error);
1553 	return (error);
1554 }
1555 
1556 /*
1557  * Rename vnode op.
1558  */
1559 int
nfsvno_rename(struct nameidata * fromndp,struct nameidata * tondp,u_int32_t ndstat,u_int32_t ndflag,struct ucred * cred,struct thread * p)1560 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp,
1561     u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p)
1562 {
1563 	struct vnode *fvp, *tvp, *tdvp, *dsdvp[NFSDEV_MAXMIRRORS];
1564 	int error = 0, mirrorcnt;
1565 	char fname[PNFS_FILENAME_LEN + 1];
1566 	fhandle_t fh;
1567 
1568 	dsdvp[0] = NULL;
1569 	fvp = fromndp->ni_vp;
1570 	if (ndstat) {
1571 		vrele(fromndp->ni_dvp);
1572 		vrele(fvp);
1573 		error = ndstat;
1574 		goto out1;
1575 	}
1576 	tdvp = tondp->ni_dvp;
1577 	tvp = tondp->ni_vp;
1578 	if (tvp != NULL) {
1579 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1580 			error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST;
1581 			goto out;
1582 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1583 			error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST;
1584 			goto out;
1585 		}
1586 		if (tvp->v_type == VDIR && tvp->v_mountedhere) {
1587 			error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1588 			goto out;
1589 		}
1590 
1591 		/*
1592 		 * A rename to '.' or '..' results in a prematurely
1593 		 * unlocked vnode on FreeBSD5, so I'm just going to fail that
1594 		 * here.
1595 		 */
1596 		if ((tondp->ni_cnd.cn_namelen == 1 &&
1597 		     tondp->ni_cnd.cn_nameptr[0] == '.') ||
1598 		    (tondp->ni_cnd.cn_namelen == 2 &&
1599 		     tondp->ni_cnd.cn_nameptr[0] == '.' &&
1600 		     tondp->ni_cnd.cn_nameptr[1] == '.')) {
1601 			error = EINVAL;
1602 			goto out;
1603 		}
1604 	}
1605 	if (fvp->v_type == VDIR && fvp->v_mountedhere) {
1606 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1607 		goto out;
1608 	}
1609 	if (fvp->v_mount != tdvp->v_mount) {
1610 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV;
1611 		goto out;
1612 	}
1613 	if (fvp == tdvp) {
1614 		error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL;
1615 		goto out;
1616 	}
1617 	if (fvp == tvp) {
1618 		/*
1619 		 * If source and destination are the same, there is nothing to
1620 		 * do. Set error to -1 to indicate this.
1621 		 */
1622 		error = -1;
1623 		goto out;
1624 	}
1625 	if (ndflag & ND_NFSV4) {
1626 		if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) {
1627 			error = nfsrv_checkremove(fvp, 0, NULL,
1628 			    (nfsquad_t)((u_quad_t)0), p);
1629 			NFSVOPUNLOCK(fvp);
1630 		} else
1631 			error = EPERM;
1632 		if (tvp && !error)
1633 			error = nfsrv_checkremove(tvp, 1, NULL,
1634 			    (nfsquad_t)((u_quad_t)0), p);
1635 	} else {
1636 		/*
1637 		 * For NFSv2 and NFSv3, try to get rid of the delegation, so
1638 		 * that the NFSv4 client won't be confused by the rename.
1639 		 * Since nfsd_recalldelegation() can only be called on an
1640 		 * unlocked vnode at this point and fvp is the file that will
1641 		 * still exist after the rename, just do fvp.
1642 		 */
1643 		nfsd_recalldelegation(fvp, p);
1644 	}
1645 	if (error == 0 && tvp != NULL) {
1646 		nfsrv_pnfsremovesetup(tvp, p, dsdvp, &mirrorcnt, fname, &fh);
1647 		NFSD_DEBUG(4, "nfsvno_rename: pnfsremovesetup"
1648 		    " dsdvp=%p\n", dsdvp[0]);
1649 	}
1650 out:
1651 	if (!error) {
1652 		error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp,
1653 		    &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp,
1654 		    &tondp->ni_cnd);
1655 	} else {
1656 		if (tdvp == tvp)
1657 			vrele(tdvp);
1658 		else
1659 			vput(tdvp);
1660 		if (tvp)
1661 			vput(tvp);
1662 		vrele(fromndp->ni_dvp);
1663 		vrele(fvp);
1664 		if (error == -1)
1665 			error = 0;
1666 	}
1667 
1668 	/*
1669 	 * If dsdvp[0] != NULL, it was set up by nfsrv_pnfsremovesetup() and
1670 	 * if the rename succeeded, the DS file for the tvp needs to be
1671 	 * removed.
1672 	 */
1673 	if (error == 0 && dsdvp[0] != NULL) {
1674 		nfsrv_pnfsremove(dsdvp, mirrorcnt, fname, &fh, p);
1675 		NFSD_DEBUG(4, "nfsvno_rename: pnfsremove\n");
1676 	}
1677 
1678 	nfsvno_relpathbuf(tondp);
1679 out1:
1680 	nfsvno_relpathbuf(fromndp);
1681 	NFSEXITCODE(error);
1682 	return (error);
1683 }
1684 
1685 /*
1686  * Link vnode op.
1687  */
1688 int
nfsvno_link(struct nameidata * ndp,struct vnode * vp,nfsquad_t clientid,struct ucred * cred,struct thread * p,struct nfsexstuff * exp)1689 nfsvno_link(struct nameidata *ndp, struct vnode *vp, nfsquad_t clientid,
1690     struct ucred *cred, struct thread *p, struct nfsexstuff *exp)
1691 {
1692 	struct vnode *xp;
1693 	int error = 0;
1694 
1695 	xp = ndp->ni_vp;
1696 	if (xp != NULL) {
1697 		error = EEXIST;
1698 	} else {
1699 		xp = ndp->ni_dvp;
1700 		if (vp->v_mount != xp->v_mount)
1701 			error = EXDEV;
1702 	}
1703 	if (!error) {
1704 		NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY);
1705 		if (!VN_IS_DOOMED(vp)) {
1706 			error = nfsrv_checkremove(vp, 0, NULL, clientid, p);
1707 			if (error == 0)
1708 				error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd);
1709 		} else
1710 			error = EPERM;
1711 		if (ndp->ni_dvp == vp) {
1712 			vrele(ndp->ni_dvp);
1713 			NFSVOPUNLOCK(vp);
1714 		} else {
1715 			vref(vp);
1716 			VOP_VPUT_PAIR(ndp->ni_dvp, &vp, true);
1717 		}
1718 	} else {
1719 		if (ndp->ni_dvp == ndp->ni_vp)
1720 			vrele(ndp->ni_dvp);
1721 		else
1722 			vput(ndp->ni_dvp);
1723 		if (ndp->ni_vp)
1724 			vrele(ndp->ni_vp);
1725 	}
1726 	nfsvno_relpathbuf(ndp);
1727 	NFSEXITCODE(error);
1728 	return (error);
1729 }
1730 
1731 /*
1732  * Do the fsync() appropriate for the commit.
1733  */
1734 int
nfsvno_fsync(struct vnode * vp,u_int64_t off,int cnt,struct ucred * cred,struct thread * td)1735 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred,
1736     struct thread *td)
1737 {
1738 	int error = 0;
1739 
1740 	/*
1741 	 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of
1742 	 * file is done.  At this time VOP_FSYNC does not accept offset and
1743 	 * byte count parameters so call VOP_FSYNC the whole file for now.
1744 	 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3.
1745 	 * File systems that do not use the buffer cache (as indicated
1746 	 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC().
1747 	 */
1748 	if (cnt == 0 || cnt > MAX_COMMIT_COUNT ||
1749 	    (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) {
1750 		/*
1751 		 * Give up and do the whole thing
1752 		 */
1753 		vnode_pager_clean_sync(vp);
1754 		error = VOP_FSYNC(vp, MNT_WAIT, td);
1755 	} else {
1756 		/*
1757 		 * Locate and synchronously write any buffers that fall
1758 		 * into the requested range.  Note:  we are assuming that
1759 		 * f_iosize is a power of 2.
1760 		 */
1761 		int iosize = vp->v_mount->mnt_stat.f_iosize;
1762 		int iomask = iosize - 1;
1763 		struct bufobj *bo;
1764 		daddr_t lblkno;
1765 
1766 		/*
1767 		 * Align to iosize boundary, super-align to page boundary.
1768 		 */
1769 		if (off & iomask) {
1770 			cnt += off & iomask;
1771 			off &= ~(u_quad_t)iomask;
1772 		}
1773 		if (off & PAGE_MASK) {
1774 			cnt += off & PAGE_MASK;
1775 			off &= ~(u_quad_t)PAGE_MASK;
1776 		}
1777 		lblkno = off / iosize;
1778 
1779 		if (vp->v_object && vm_object_mightbedirty(vp->v_object)) {
1780 			VM_OBJECT_WLOCK(vp->v_object);
1781 			vm_object_page_clean(vp->v_object, off, off + cnt,
1782 			    OBJPC_SYNC);
1783 			VM_OBJECT_WUNLOCK(vp->v_object);
1784 		}
1785 
1786 		bo = &vp->v_bufobj;
1787 		BO_LOCK(bo);
1788 		while (cnt > 0) {
1789 			struct buf *bp;
1790 
1791 			/*
1792 			 * If we have a buffer and it is marked B_DELWRI we
1793 			 * have to lock and write it.  Otherwise the prior
1794 			 * write is assumed to have already been committed.
1795 			 *
1796 			 * gbincore() can return invalid buffers now so we
1797 			 * have to check that bit as well (though B_DELWRI
1798 			 * should not be set if B_INVAL is set there could be
1799 			 * a race here since we haven't locked the buffer).
1800 			 */
1801 			if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) {
1802 				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1803 				    LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) {
1804 					BO_LOCK(bo);
1805 					continue; /* retry */
1806 				}
1807 			    	if ((bp->b_flags & (B_DELWRI|B_INVAL)) ==
1808 				    B_DELWRI) {
1809 					bremfree(bp);
1810 					bp->b_flags &= ~B_ASYNC;
1811 					bwrite(bp);
1812 					++nfs_commit_miss;
1813 				} else
1814 					BUF_UNLOCK(bp);
1815 				BO_LOCK(bo);
1816 			}
1817 			++nfs_commit_blks;
1818 			if (cnt < iosize)
1819 				break;
1820 			cnt -= iosize;
1821 			++lblkno;
1822 		}
1823 		BO_UNLOCK(bo);
1824 	}
1825 	NFSEXITCODE(error);
1826 	return (error);
1827 }
1828 
1829 /*
1830  * Statfs vnode op.
1831  */
1832 int
nfsvno_statfs(struct vnode * vp,struct statfs * sf)1833 nfsvno_statfs(struct vnode *vp, struct statfs *sf)
1834 {
1835 	struct statfs *tsf;
1836 	int error;
1837 
1838 	tsf = NULL;
1839 	if (nfsrv_devidcnt > 0) {
1840 		/* For a pNFS service, get the DS numbers. */
1841 		tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK | M_ZERO);
1842 		error = nfsrv_pnfsstatfs(tsf, vp->v_mount);
1843 		if (error != 0) {
1844 			free(tsf, M_TEMP);
1845 			tsf = NULL;
1846 		}
1847 	}
1848 	error = VFS_STATFS(vp->v_mount, sf);
1849 	if (error == 0) {
1850 		if (tsf != NULL) {
1851 			sf->f_blocks = tsf->f_blocks;
1852 			sf->f_bavail = tsf->f_bavail;
1853 			sf->f_bfree = tsf->f_bfree;
1854 			sf->f_bsize = tsf->f_bsize;
1855 		}
1856 		/*
1857 		 * Since NFS handles these values as unsigned on the
1858 		 * wire, there is no way to represent negative values,
1859 		 * so set them to 0. Without this, they will appear
1860 		 * to be very large positive values for clients like
1861 		 * Solaris10.
1862 		 */
1863 		if (sf->f_bavail < 0)
1864 			sf->f_bavail = 0;
1865 		if (sf->f_ffree < 0)
1866 			sf->f_ffree = 0;
1867 	}
1868 	free(tsf, M_TEMP);
1869 	NFSEXITCODE(error);
1870 	return (error);
1871 }
1872 
1873 /*
1874  * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but
1875  * must handle nfsrv_opencheck() calls after any other access checks.
1876  */
1877 void
nfsvno_open(struct nfsrv_descript * nd,struct nameidata * ndp,nfsquad_t clientid,nfsv4stateid_t * stateidp,struct nfsstate * stp,int * exclusive_flagp,struct nfsvattr * nvap,int32_t * cverf,int create,NFSACL_T * aclp,nfsattrbit_t * attrbitp,struct ucred * cred,bool done_namei,struct nfsexstuff * exp,struct vnode ** vpp)1878 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp,
1879     nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp,
1880     int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create,
1881     NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, bool done_namei,
1882     struct nfsexstuff *exp, struct vnode **vpp)
1883 {
1884 	struct vnode *vp = NULL;
1885 	u_quad_t tempsize;
1886 	struct nfsexstuff nes;
1887 	struct thread *p = curthread;
1888 	uint32_t oldrepstat;
1889 
1890 	if (ndp->ni_vp == NULL) {
1891 		/*
1892 		 * If nfsrv_opencheck() sets nd_repstat, done_namei needs to be
1893 		 * set true, since cleanup after nfsvno_namei() is needed.
1894 		 */
1895 		oldrepstat = nd->nd_repstat;
1896 		nd->nd_repstat = nfsrv_opencheck(clientid,
1897 		    stateidp, stp, NULL, nd, p, nd->nd_repstat);
1898 		if (nd->nd_repstat != 0 && oldrepstat == 0)
1899 			done_namei = true;
1900 	}
1901 	if (!nd->nd_repstat) {
1902 		if (ndp->ni_vp == NULL) {
1903 			nd->nd_repstat = VOP_CREATE(ndp->ni_dvp,
1904 			    &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr);
1905 			/* For a pNFS server, create the data file on a DS. */
1906 			if (nd->nd_repstat == 0) {
1907 				/*
1908 				 * Create a data file on a DS for a pNFS server.
1909 				 * This function just returns if not
1910 				 * running a pNFS DS or the creation fails.
1911 				 */
1912 				nfsrv_pnfscreate(ndp->ni_vp, &nvap->na_vattr,
1913 				    cred, p);
1914 			}
1915 			VOP_VPUT_PAIR(ndp->ni_dvp, nd->nd_repstat == 0 ?
1916 			    &ndp->ni_vp : NULL, false);
1917 			nfsvno_relpathbuf(ndp);
1918 			if (!nd->nd_repstat) {
1919 				if (*exclusive_flagp) {
1920 					*exclusive_flagp = 0;
1921 					NFSVNO_ATTRINIT(nvap);
1922 					nvap->na_atime.tv_sec = cverf[0];
1923 					nvap->na_atime.tv_nsec = cverf[1];
1924 					nd->nd_repstat = VOP_SETATTR(ndp->ni_vp,
1925 					    &nvap->na_vattr, cred);
1926 					if (nd->nd_repstat != 0) {
1927 						vput(ndp->ni_vp);
1928 						ndp->ni_vp = NULL;
1929 						nd->nd_repstat = NFSERR_NOTSUPP;
1930 					} else
1931 						NFSSETBIT_ATTRBIT(attrbitp,
1932 						    NFSATTRBIT_TIMEACCESS);
1933 				} else {
1934 					nfsrv_fixattr(nd, ndp->ni_vp, nvap,
1935 					    aclp, p, attrbitp, exp);
1936 				}
1937 			}
1938 			vp = ndp->ni_vp;
1939 		} else {
1940 			nfsvno_relpathbuf(ndp);
1941 			vp = ndp->ni_vp;
1942 			if (create == NFSV4OPEN_CREATE) {
1943 				if (ndp->ni_dvp == vp)
1944 					vrele(ndp->ni_dvp);
1945 				else
1946 					vput(ndp->ni_dvp);
1947 			}
1948 			if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) {
1949 				if (ndp->ni_cnd.cn_flags & RDONLY)
1950 					NFSVNO_SETEXRDONLY(&nes);
1951 				else
1952 					NFSVNO_EXINIT(&nes);
1953 				nd->nd_repstat = nfsvno_accchk(vp,
1954 				    VWRITE, cred, &nes, p,
1955 				    NFSACCCHK_NOOVERRIDE,
1956 				    NFSACCCHK_VPISLOCKED, NULL);
1957 				nd->nd_repstat = nfsrv_opencheck(clientid,
1958 				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1959 				if (!nd->nd_repstat) {
1960 					tempsize = nvap->na_size;
1961 					NFSVNO_ATTRINIT(nvap);
1962 					nvap->na_size = tempsize;
1963 					nd->nd_repstat = nfsvno_setattr(vp,
1964 					    nvap, cred, p, exp);
1965 				}
1966 			} else if (vp->v_type == VREG) {
1967 				nd->nd_repstat = nfsrv_opencheck(clientid,
1968 				    stateidp, stp, vp, nd, p, nd->nd_repstat);
1969 			}
1970 		}
1971 	} else if (done_namei) {
1972 		KASSERT(create == NFSV4OPEN_CREATE,
1973 		    ("nfsvno_open: not create"));
1974 		/*
1975 		 * done_namei is set when nfsvno_namei() has completed
1976 		 * successfully, but a subsequent error was set in
1977 		 * nd_repstat.  As such, cleanup of the nfsvno_namei()
1978 		 * results is required.
1979 		 */
1980 		nfsvno_relpathbuf(ndp);
1981 		if (ndp->ni_dvp == ndp->ni_vp)
1982 			vrele(ndp->ni_dvp);
1983 		else
1984 			vput(ndp->ni_dvp);
1985 		if (ndp->ni_vp)
1986 			vput(ndp->ni_vp);
1987 	}
1988 	*vpp = vp;
1989 
1990 	NFSEXITCODE2(0, nd);
1991 }
1992 
1993 /*
1994  * Updates the file rev and sets the mtime and ctime
1995  * to the current clock time, returning the va_filerev and va_Xtime
1996  * values.
1997  * Return ESTALE to indicate the vnode is VIRF_DOOMED.
1998  */
1999 int
nfsvno_updfilerev(struct vnode * vp,struct nfsvattr * nvap,struct nfsrv_descript * nd,struct thread * p)2000 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap,
2001     struct nfsrv_descript *nd, struct thread *p)
2002 {
2003 	struct vattr va;
2004 
2005 	VATTR_NULL(&va);
2006 	vfs_timestamp(&va.va_mtime);
2007 	if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) {
2008 		NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY);
2009 		if (VN_IS_DOOMED(vp))
2010 			return (ESTALE);
2011 	}
2012 	(void) VOP_SETATTR(vp, &va, nd->nd_cred);
2013 	(void) nfsvno_getattr(vp, nvap, nd, p, 1, NULL);
2014 	return (0);
2015 }
2016 
2017 /*
2018  * Glue routine to nfsv4_fillattr().
2019  */
2020 int
nfsvno_fillattr(struct nfsrv_descript * nd,struct mount * mp,struct vnode * vp,struct nfsvattr * nvap,fhandle_t * fhp,int rderror,nfsattrbit_t * attrbitp,struct ucred * cred,struct thread * p,int isdgram,int reterr,int supports_nfsv4acls,int at_root,uint64_t mounted_on_fileno)2021 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp,
2022     struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp,
2023     struct ucred *cred, struct thread *p, int isdgram, int reterr,
2024     int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno)
2025 {
2026 	struct statfs *sf;
2027 	int error;
2028 
2029 	sf = NULL;
2030 	if (nfsrv_devidcnt > 0 &&
2031 	    (NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEAVAIL) ||
2032 	     NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACEFREE) ||
2033 	     NFSISSET_ATTRBIT(attrbitp, NFSATTRBIT_SPACETOTAL))) {
2034 		sf = malloc(sizeof(*sf), M_TEMP, M_WAITOK | M_ZERO);
2035 		error = nfsrv_pnfsstatfs(sf, mp);
2036 		if (error != 0) {
2037 			free(sf, M_TEMP);
2038 			sf = NULL;
2039 		}
2040 	}
2041 	error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror,
2042 	    attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root,
2043 	    mounted_on_fileno, sf);
2044 	free(sf, M_TEMP);
2045 	NFSEXITCODE2(0, nd);
2046 	return (error);
2047 }
2048 
2049 /* Since the Readdir vnode ops vary, put the entire functions in here. */
2050 /*
2051  * nfs readdir service
2052  * - mallocs what it thinks is enough to read
2053  *	count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR
2054  * - calls VOP_READDIR()
2055  * - loops around building the reply
2056  *	if the output generated exceeds count break out of loop
2057  *	The NFSM_CLGET macro is used here so that the reply will be packed
2058  *	tightly in mbuf clusters.
2059  * - it trims out records with d_fileno == 0
2060  *	this doesn't matter for Unix clients, but they might confuse clients
2061  *	for other os'.
2062  * - it trims out records with d_type == DT_WHT
2063  *	these cannot be seen through NFS (unless we extend the protocol)
2064  *     The alternate call nfsrvd_readdirplus() does lookups as well.
2065  * PS: The NFS protocol spec. does not clarify what the "count" byte
2066  *	argument is a count of.. just name strings and file id's or the
2067  *	entire reply rpc or ...
2068  *	I tried just file name and id sizes and it confused the Sun client,
2069  *	so I am using the full rpc size now. The "paranoia.." comment refers
2070  *	to including the status longwords that are not a part of the dir.
2071  *	"entry" structures, but are in the rpc.
2072  */
2073 int
nfsrvd_readdir(struct nfsrv_descript * nd,int isdgram,struct vnode * vp,struct nfsexstuff * exp)2074 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram,
2075     struct vnode *vp, struct nfsexstuff *exp)
2076 {
2077 	struct dirent *dp;
2078 	u_int32_t *tl;
2079 	int dirlen;
2080 	char *cpos, *cend, *rbuf;
2081 	struct nfsvattr at;
2082 	int nlen, error = 0, getret = 1;
2083 	int siz, cnt, fullsiz, eofflag, ncookies;
2084 	u_int64_t off, toff, verf __unused;
2085 	uint64_t *cookies = NULL, *cookiep;
2086 	struct uio io;
2087 	struct iovec iv;
2088 	int is_ufs;
2089 	struct thread *p = curthread;
2090 
2091 	if (nd->nd_repstat) {
2092 		nfsrv_postopattr(nd, getret, &at);
2093 		goto out;
2094 	}
2095 	if (nd->nd_flag & ND_NFSV2) {
2096 		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2097 		off = fxdr_unsigned(u_quad_t, *tl++);
2098 	} else {
2099 		NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
2100 		off = fxdr_hyper(tl);
2101 		tl += 2;
2102 		verf = fxdr_hyper(tl);
2103 		tl += 2;
2104 	}
2105 	toff = off;
2106 	cnt = fxdr_unsigned(int, *tl);
2107 	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2108 		cnt = NFS_SRVMAXDATA(nd);
2109 	siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2110 	fullsiz = siz;
2111 	if (nd->nd_flag & ND_NFSV3) {
2112 		nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1,
2113 		    NULL);
2114 #if 0
2115 		/*
2116 		 * va_filerev is not sufficient as a cookie verifier,
2117 		 * since it is not supposed to change when entries are
2118 		 * removed/added unless that offset cookies returned to
2119 		 * the client are no longer valid.
2120 		 */
2121 		if (!nd->nd_repstat && toff && verf != at.na_filerev)
2122 			nd->nd_repstat = NFSERR_BAD_COOKIE;
2123 #endif
2124 	}
2125 	if (!nd->nd_repstat && vp->v_type != VDIR)
2126 		nd->nd_repstat = NFSERR_NOTDIR;
2127 	if (nd->nd_repstat == 0 && cnt == 0) {
2128 		if (nd->nd_flag & ND_NFSV2)
2129 			/* NFSv2 does not have NFSERR_TOOSMALL */
2130 			nd->nd_repstat = EPERM;
2131 		else
2132 			nd->nd_repstat = NFSERR_TOOSMALL;
2133 	}
2134 	if (!nd->nd_repstat)
2135 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2136 		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2137 		    NFSACCCHK_VPISLOCKED, NULL);
2138 	if (nd->nd_repstat) {
2139 		vput(vp);
2140 		if (nd->nd_flag & ND_NFSV3)
2141 			nfsrv_postopattr(nd, getret, &at);
2142 		goto out;
2143 	}
2144 	is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2145 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
2146 again:
2147 	eofflag = 0;
2148 	if (cookies) {
2149 		free(cookies, M_TEMP);
2150 		cookies = NULL;
2151 	}
2152 
2153 	iv.iov_base = rbuf;
2154 	iv.iov_len = siz;
2155 	io.uio_iov = &iv;
2156 	io.uio_iovcnt = 1;
2157 	io.uio_offset = (off_t)off;
2158 	io.uio_resid = siz;
2159 	io.uio_segflg = UIO_SYSSPACE;
2160 	io.uio_rw = UIO_READ;
2161 	io.uio_td = NULL;
2162 	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2163 	    &cookies);
2164 	off = (u_int64_t)io.uio_offset;
2165 	if (io.uio_resid)
2166 		siz -= io.uio_resid;
2167 
2168 	if (!cookies && !nd->nd_repstat)
2169 		nd->nd_repstat = NFSERR_PERM;
2170 	if (nd->nd_flag & ND_NFSV3) {
2171 		getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2172 		if (!nd->nd_repstat)
2173 			nd->nd_repstat = getret;
2174 	}
2175 
2176 	/*
2177 	 * Handles the failed cases. nd->nd_repstat == 0 past here.
2178 	 */
2179 	if (nd->nd_repstat) {
2180 		vput(vp);
2181 		free(rbuf, M_TEMP);
2182 		if (cookies)
2183 			free(cookies, M_TEMP);
2184 		if (nd->nd_flag & ND_NFSV3)
2185 			nfsrv_postopattr(nd, getret, &at);
2186 		goto out;
2187 	}
2188 	/*
2189 	 * If nothing read, return eof
2190 	 * rpc reply
2191 	 */
2192 	if (siz == 0) {
2193 		vput(vp);
2194 		if (nd->nd_flag & ND_NFSV2) {
2195 			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2196 		} else {
2197 			nfsrv_postopattr(nd, getret, &at);
2198 			NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2199 			txdr_hyper(at.na_filerev, tl);
2200 			tl += 2;
2201 		}
2202 		*tl++ = newnfs_false;
2203 		*tl = newnfs_true;
2204 		free(rbuf, M_TEMP);
2205 		free(cookies, M_TEMP);
2206 		goto out;
2207 	}
2208 
2209 	/*
2210 	 * Check for degenerate cases of nothing useful read.
2211 	 * If so go try again
2212 	 */
2213 	cpos = rbuf;
2214 	cend = rbuf + siz;
2215 	dp = (struct dirent *)cpos;
2216 	cookiep = cookies;
2217 
2218 	/*
2219 	 * For some reason FreeBSD's ufs_readdir() chooses to back the
2220 	 * directory offset up to a block boundary, so it is necessary to
2221 	 * skip over the records that precede the requested offset. This
2222 	 * requires the assumption that file offset cookies monotonically
2223 	 * increase.
2224 	 */
2225 	while (cpos < cend && ncookies > 0 &&
2226 	    (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2227 	     (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) {
2228 		cpos += dp->d_reclen;
2229 		dp = (struct dirent *)cpos;
2230 		cookiep++;
2231 		ncookies--;
2232 	}
2233 	if (cpos >= cend || ncookies == 0) {
2234 		siz = fullsiz;
2235 		toff = off;
2236 		goto again;
2237 	}
2238 	vput(vp);
2239 
2240 	/*
2241 	 * If cnt > MCLBYTES and the reply will not be saved, use
2242 	 * ext_pgs mbufs for TLS.
2243 	 * For NFSv4.0, we do not know for sure if the reply will
2244 	 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2245 	 */
2246 	if (cnt > MCLBYTES && siz > MCLBYTES &&
2247 	    (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2248 	    (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2249 		nd->nd_flag |= ND_EXTPG;
2250 
2251 	/*
2252 	 * dirlen is the size of the reply, including all XDR and must
2253 	 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate
2254 	 * if the XDR should be included in "count", but to be safe, we do.
2255 	 * (Include the two booleans at the end of the reply in dirlen now.)
2256 	 */
2257 	if (nd->nd_flag & ND_NFSV3) {
2258 		nfsrv_postopattr(nd, getret, &at);
2259 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2260 		txdr_hyper(at.na_filerev, tl);
2261 		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2262 	} else {
2263 		dirlen = 2 * NFSX_UNSIGNED;
2264 	}
2265 
2266 	/* Loop through the records and build reply */
2267 	while (cpos < cend && ncookies > 0) {
2268 		nlen = dp->d_namlen;
2269 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2270 			nlen <= NFS_MAXNAMLEN) {
2271 			if (nd->nd_flag & ND_NFSV3)
2272 				dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2273 			else
2274 				dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen));
2275 			if (dirlen > cnt) {
2276 				eofflag = 0;
2277 				break;
2278 			}
2279 
2280 			/*
2281 			 * Build the directory record xdr from
2282 			 * the dirent entry.
2283 			 */
2284 			if (nd->nd_flag & ND_NFSV3) {
2285 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2286 				*tl++ = newnfs_true;
2287 				txdr_hyper(dp->d_fileno, tl);
2288 			} else {
2289 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2290 				*tl++ = newnfs_true;
2291 				*tl = txdr_unsigned(dp->d_fileno);
2292 			}
2293 			(void) nfsm_strtom(nd, dp->d_name, nlen);
2294 			if (nd->nd_flag & ND_NFSV3) {
2295 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2296 				txdr_hyper(*cookiep, tl);
2297 			} else {
2298 				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
2299 				*tl = txdr_unsigned(*cookiep);
2300 			}
2301 		}
2302 		cpos += dp->d_reclen;
2303 		dp = (struct dirent *)cpos;
2304 		cookiep++;
2305 		ncookies--;
2306 	}
2307 	if (cpos < cend)
2308 		eofflag = 0;
2309 	NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2310 	*tl++ = newnfs_false;
2311 	if (eofflag)
2312 		*tl = newnfs_true;
2313 	else
2314 		*tl = newnfs_false;
2315 	free(rbuf, M_TEMP);
2316 	free(cookies, M_TEMP);
2317 
2318 out:
2319 	NFSEXITCODE2(0, nd);
2320 	return (0);
2321 nfsmout:
2322 	vput(vp);
2323 	NFSEXITCODE2(error, nd);
2324 	return (error);
2325 }
2326 
2327 /*
2328  * Readdirplus for V3 and Readdir for V4.
2329  */
2330 int
nfsrvd_readdirplus(struct nfsrv_descript * nd,int isdgram,struct vnode * vp,struct nfsexstuff * exp)2331 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram,
2332     struct vnode *vp, struct nfsexstuff *exp)
2333 {
2334 	struct dirent *dp;
2335 	u_int32_t *tl;
2336 	int dirlen;
2337 	char *cpos, *cend, *rbuf;
2338 	struct vnode *nvp;
2339 	fhandle_t nfh;
2340 	struct nfsvattr nva, at, *nvap = &nva;
2341 	struct mbuf *mb0, *mb1;
2342 	struct nfsreferral *refp;
2343 	int nlen, r, error = 0, getret = 1, usevget = 1;
2344 	int siz, cnt, fullsiz, eofflag, ncookies, entrycnt;
2345 	caddr_t bpos0, bpos1;
2346 	u_int64_t off, toff, verf __unused;
2347 	uint64_t *cookies = NULL, *cookiep;
2348 	nfsattrbit_t attrbits, rderrbits, savbits, refbits;
2349 	struct uio io;
2350 	struct iovec iv;
2351 	struct componentname cn;
2352 	int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls;
2353 	struct mount *mp, *new_mp;
2354 	uint64_t mounted_on_fileno;
2355 	struct thread *p = curthread;
2356 	int bextpg0, bextpg1, bextpgsiz0, bextpgsiz1;
2357 
2358 	if (nd->nd_repstat) {
2359 		nfsrv_postopattr(nd, getret, &at);
2360 		goto out;
2361 	}
2362 	NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED);
2363 	off = fxdr_hyper(tl);
2364 	toff = off;
2365 	tl += 2;
2366 	verf = fxdr_hyper(tl);
2367 	tl += 2;
2368 	siz = fxdr_unsigned(int, *tl++);
2369 	cnt = fxdr_unsigned(int, *tl);
2370 
2371 	/*
2372 	 * Use the server's maximum data transfer size as the upper bound
2373 	 * on reply datalen.
2374 	 */
2375 	if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0)
2376 		cnt = NFS_SRVMAXDATA(nd);
2377 
2378 	/*
2379 	 * siz is a "hint" of how much directory information (name, fileid,
2380 	 * cookie) should be in the reply. At least one client "hints" 0,
2381 	 * so I set it to cnt for that case. I also round it up to the
2382 	 * next multiple of DIRBLKSIZ.
2383 	 * Since the size of a Readdirplus directory entry reply will always
2384 	 * be greater than a directory entry returned by VOP_READDIR(), it
2385 	 * does not make sense to read more than NFS_SRVMAXDATA() via
2386 	 * VOP_READDIR().
2387 	 */
2388 	if (siz <= 0)
2389 		siz = cnt;
2390 	else if (siz > NFS_SRVMAXDATA(nd))
2391 		siz = NFS_SRVMAXDATA(nd);
2392 	siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1));
2393 
2394 	if (nd->nd_flag & ND_NFSV4) {
2395 		error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
2396 		if (error)
2397 			goto nfsmout;
2398 		NFSSET_ATTRBIT(&savbits, &attrbits);
2399 		NFSSET_ATTRBIT(&refbits, &attrbits);
2400 		NFSCLRNOTFILLABLE_ATTRBIT(&attrbits, nd);
2401 		NFSZERO_ATTRBIT(&rderrbits);
2402 		NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR);
2403 		/*
2404 		 * If these 4 bits are the only attributes requested by the
2405 		 * client, they can be satisfied without acquiring the vnode
2406 		 * for the file object unless it is a directory.
2407 		 * This will be indicated by savbits being all 0s.
2408 		 */
2409 		NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_TYPE);
2410 		NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_FILEID);
2411 		NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_MOUNTEDONFILEID);
2412 		NFSCLRBIT_ATTRBIT(&savbits, NFSATTRBIT_RDATTRERROR);
2413 	} else {
2414 		NFSZERO_ATTRBIT(&attrbits);
2415 	}
2416 	fullsiz = siz;
2417 	nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2418 #if 0
2419 	if (!nd->nd_repstat) {
2420 	    if (off && verf != at.na_filerev) {
2421 		/*
2422 		 * va_filerev is not sufficient as a cookie verifier,
2423 		 * since it is not supposed to change when entries are
2424 		 * removed/added unless that offset cookies returned to
2425 		 * the client are no longer valid.
2426 		 */
2427 		if (nd->nd_flag & ND_NFSV4) {
2428 			nd->nd_repstat = NFSERR_NOTSAME;
2429 		} else {
2430 			nd->nd_repstat = NFSERR_BAD_COOKIE;
2431 		}
2432 	    }
2433 	}
2434 #endif
2435 	if (!nd->nd_repstat && vp->v_type != VDIR)
2436 		nd->nd_repstat = NFSERR_NOTDIR;
2437 	if (!nd->nd_repstat && cnt == 0)
2438 		nd->nd_repstat = NFSERR_TOOSMALL;
2439 	if (!nd->nd_repstat)
2440 		nd->nd_repstat = nfsvno_accchk(vp, VEXEC,
2441 		    nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE,
2442 		    NFSACCCHK_VPISLOCKED, NULL);
2443 	if (nd->nd_repstat) {
2444 		vput(vp);
2445 		if (nd->nd_flag & ND_NFSV3)
2446 			nfsrv_postopattr(nd, getret, &at);
2447 		goto out;
2448 	}
2449 	is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0;
2450 	is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0;
2451 
2452 	rbuf = malloc(siz, M_TEMP, M_WAITOK);
2453 again:
2454 	eofflag = 0;
2455 	if (cookies) {
2456 		free(cookies, M_TEMP);
2457 		cookies = NULL;
2458 	}
2459 
2460 	iv.iov_base = rbuf;
2461 	iv.iov_len = siz;
2462 	io.uio_iov = &iv;
2463 	io.uio_iovcnt = 1;
2464 	io.uio_offset = (off_t)off;
2465 	io.uio_resid = siz;
2466 	io.uio_segflg = UIO_SYSSPACE;
2467 	io.uio_rw = UIO_READ;
2468 	io.uio_td = NULL;
2469 	nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies,
2470 	    &cookies);
2471 	off = (u_int64_t)io.uio_offset;
2472 	if (io.uio_resid)
2473 		siz -= io.uio_resid;
2474 
2475 	getret = nfsvno_getattr(vp, &at, nd, p, 1, NULL);
2476 
2477 	if (!cookies && !nd->nd_repstat)
2478 		nd->nd_repstat = NFSERR_PERM;
2479 	if (!nd->nd_repstat)
2480 		nd->nd_repstat = getret;
2481 	if (nd->nd_repstat) {
2482 		vput(vp);
2483 		if (cookies)
2484 			free(cookies, M_TEMP);
2485 		free(rbuf, M_TEMP);
2486 		if (nd->nd_flag & ND_NFSV3)
2487 			nfsrv_postopattr(nd, getret, &at);
2488 		goto out;
2489 	}
2490 	/*
2491 	 * If nothing read, return eof
2492 	 * rpc reply
2493 	 */
2494 	if (siz == 0) {
2495 		vput(vp);
2496 		if (nd->nd_flag & ND_NFSV3)
2497 			nfsrv_postopattr(nd, getret, &at);
2498 		NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED);
2499 		txdr_hyper(at.na_filerev, tl);
2500 		tl += 2;
2501 		*tl++ = newnfs_false;
2502 		*tl = newnfs_true;
2503 		free(cookies, M_TEMP);
2504 		free(rbuf, M_TEMP);
2505 		goto out;
2506 	}
2507 
2508 	/*
2509 	 * Check for degenerate cases of nothing useful read.
2510 	 * If so go try again
2511 	 */
2512 	cpos = rbuf;
2513 	cend = rbuf + siz;
2514 	dp = (struct dirent *)cpos;
2515 	cookiep = cookies;
2516 
2517 	/*
2518 	 * For some reason FreeBSD's ufs_readdir() chooses to back the
2519 	 * directory offset up to a block boundary, so it is necessary to
2520 	 * skip over the records that precede the requested offset. This
2521 	 * requires the assumption that file offset cookies monotonically
2522 	 * increase.
2523 	 */
2524 	while (cpos < cend && ncookies > 0 &&
2525 	  (dp->d_fileno == 0 || dp->d_type == DT_WHT ||
2526 	   (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) ||
2527 	   ((nd->nd_flag & ND_NFSV4) &&
2528 	    ((dp->d_namlen == 1 && dp->d_name[0] == '.') ||
2529 	     (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) {
2530 		cpos += dp->d_reclen;
2531 		dp = (struct dirent *)cpos;
2532 		cookiep++;
2533 		ncookies--;
2534 	}
2535 	if (cpos >= cend || ncookies == 0) {
2536 		siz = fullsiz;
2537 		toff = off;
2538 		goto again;
2539 	}
2540 
2541 	/*
2542 	 * Busy the file system so that the mount point won't go away
2543 	 * and, as such, VFS_VGET() can be used safely.
2544 	 */
2545 	mp = vp->v_mount;
2546 	vfs_ref(mp);
2547 	NFSVOPUNLOCK(vp);
2548 	nd->nd_repstat = vfs_busy(mp, 0);
2549 	vfs_rel(mp);
2550 	if (nd->nd_repstat != 0) {
2551 		vrele(vp);
2552 		free(cookies, M_TEMP);
2553 		free(rbuf, M_TEMP);
2554 		if (nd->nd_flag & ND_NFSV3)
2555 			nfsrv_postopattr(nd, getret, &at);
2556 		goto out;
2557 	}
2558 
2559 	/*
2560 	 * Check to see if entries in this directory can be safely acquired
2561 	 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required.
2562 	 * ZFS snapshot directories need VOP_LOOKUP(), so that any
2563 	 * automount of the snapshot directory that is required will
2564 	 * be done.
2565 	 * This needs to be done here for NFSv4, since NFSv4 never does
2566 	 * a VFS_VGET() for "." or "..".
2567 	 */
2568 	if (is_zfs == 1) {
2569 		r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp);
2570 		if (r == EOPNOTSUPP) {
2571 			usevget = 0;
2572 			cn.cn_nameiop = LOOKUP;
2573 			cn.cn_lkflags = LK_SHARED | LK_RETRY;
2574 			cn.cn_cred = nd->nd_cred;
2575 		} else if (r == 0)
2576 			vput(nvp);
2577 	}
2578 
2579 	/*
2580 	 * If the reply is likely to exceed MCLBYTES and the reply will
2581 	 * not be saved, use ext_pgs mbufs for TLS.
2582 	 * It is difficult to predict how large each entry will be and
2583 	 * how many entries have been read, so just assume the directory
2584 	 * entries grow by a factor of 4 when attributes are included.
2585 	 * For NFSv4.0, we do not know for sure if the reply will
2586 	 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
2587 	 */
2588 	if (cnt > MCLBYTES && siz > MCLBYTES / 4 &&
2589 	    (nd->nd_flag & (ND_TLS | ND_EXTPG | ND_SAVEREPLY)) == ND_TLS &&
2590 	    (nd->nd_flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4)
2591 		nd->nd_flag |= ND_EXTPG;
2592 
2593 	/*
2594 	 * Save this position, in case there is an error before one entry
2595 	 * is created.
2596 	 */
2597 	mb0 = nd->nd_mb;
2598 	bpos0 = nd->nd_bpos;
2599 	bextpg0 = nd->nd_bextpg;
2600 	bextpgsiz0 = nd->nd_bextpgsiz;
2601 
2602 	/*
2603 	 * Fill in the first part of the reply.
2604 	 * dirlen is the reply length in bytes and cannot exceed cnt.
2605 	 * (Include the two booleans at the end of the reply in dirlen now,
2606 	 *  so we recognize when we have exceeded cnt.)
2607 	 */
2608 	if (nd->nd_flag & ND_NFSV3) {
2609 		dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED;
2610 		nfsrv_postopattr(nd, getret, &at);
2611 	} else {
2612 		dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED;
2613 	}
2614 	NFSM_BUILD(tl, u_int32_t *, NFSX_VERF);
2615 	txdr_hyper(at.na_filerev, tl);
2616 
2617 	/*
2618 	 * Save this position, in case there is an empty reply needed.
2619 	 */
2620 	mb1 = nd->nd_mb;
2621 	bpos1 = nd->nd_bpos;
2622 	bextpg1 = nd->nd_bextpg;
2623 	bextpgsiz1 = nd->nd_bextpgsiz;
2624 
2625 	/* Loop through the records and build reply */
2626 	entrycnt = 0;
2627 	while (cpos < cend && ncookies > 0 && dirlen < cnt) {
2628 		nlen = dp->d_namlen;
2629 		if (dp->d_fileno != 0 && dp->d_type != DT_WHT &&
2630 		    nlen <= NFS_MAXNAMLEN &&
2631 		    ((nd->nd_flag & ND_NFSV3) || nlen > 2 ||
2632 		     (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.'))
2633 		      || (nlen == 1 && dp->d_name[0] != '.'))) {
2634 			/*
2635 			 * Save the current position in the reply, in case
2636 			 * this entry exceeds cnt.
2637 			 */
2638 			mb1 = nd->nd_mb;
2639 			bpos1 = nd->nd_bpos;
2640 			bextpg1 = nd->nd_bextpg;
2641 			bextpgsiz1 = nd->nd_bextpgsiz;
2642 
2643 			/*
2644 			 * For readdir_and_lookup get the vnode using
2645 			 * the file number.
2646 			 */
2647 			nvp = NULL;
2648 			refp = NULL;
2649 			r = 0;
2650 			at_root = 0;
2651 			needs_unbusy = 0;
2652 			new_mp = mp;
2653 			mounted_on_fileno = (uint64_t)dp->d_fileno;
2654 			if ((nd->nd_flag & ND_NFSV3) ||
2655 			    NFSNONZERO_ATTRBIT(&savbits) ||
2656 			    dp->d_type == DT_UNKNOWN ||
2657 			    (dp->d_type == DT_DIR &&
2658 			     nfsrv_enable_crossmntpt != 0)) {
2659 				if (nd->nd_flag & ND_NFSV4)
2660 					refp = nfsv4root_getreferral(NULL,
2661 					    vp, dp->d_fileno);
2662 				if (refp == NULL) {
2663 					if (usevget)
2664 						r = VFS_VGET(mp, dp->d_fileno,
2665 						    LK_SHARED, &nvp);
2666 					else
2667 						r = EOPNOTSUPP;
2668 					if (r == EOPNOTSUPP) {
2669 						if (usevget) {
2670 							usevget = 0;
2671 							cn.cn_nameiop = LOOKUP;
2672 							cn.cn_lkflags =
2673 							    LK_SHARED |
2674 							    LK_RETRY;
2675 							cn.cn_cred =
2676 							    nd->nd_cred;
2677 						}
2678 						cn.cn_nameptr = dp->d_name;
2679 						cn.cn_namelen = nlen;
2680 						cn.cn_flags = ISLASTCN |
2681 						    NOFOLLOW | LOCKLEAF;
2682 						if (nlen == 2 &&
2683 						    dp->d_name[0] == '.' &&
2684 						    dp->d_name[1] == '.')
2685 							cn.cn_flags |=
2686 							    ISDOTDOT;
2687 						if (NFSVOPLOCK(vp, LK_SHARED)
2688 						    != 0) {
2689 							nd->nd_repstat = EPERM;
2690 							break;
2691 						}
2692 						if ((vp->v_vflag & VV_ROOT) != 0
2693 						    && (cn.cn_flags & ISDOTDOT)
2694 						    != 0) {
2695 							vref(vp);
2696 							nvp = vp;
2697 							r = 0;
2698 						} else {
2699 							r = VOP_LOOKUP(vp, &nvp,
2700 							    &cn);
2701 							if (vp != nvp)
2702 								NFSVOPUNLOCK(vp);
2703 						}
2704 					}
2705 
2706 					/*
2707 					 * For NFSv4, check to see if nvp is
2708 					 * a mount point and get the mount
2709 					 * point vnode, as required.
2710 					 */
2711 					if (r == 0 &&
2712 					    nfsrv_enable_crossmntpt != 0 &&
2713 					    (nd->nd_flag & ND_NFSV4) != 0 &&
2714 					    nvp->v_type == VDIR &&
2715 					    nvp->v_mountedhere != NULL) {
2716 						new_mp = nvp->v_mountedhere;
2717 						r = vfs_busy(new_mp, 0);
2718 						vput(nvp);
2719 						nvp = NULL;
2720 						if (r == 0) {
2721 							r = VFS_ROOT(new_mp,
2722 							    LK_SHARED, &nvp);
2723 							needs_unbusy = 1;
2724 							if (r == 0)
2725 								at_root = 1;
2726 						}
2727 					}
2728 				}
2729 
2730 				/*
2731 				 * If we failed to look up the entry, then it
2732 				 * has become invalid, most likely removed.
2733 				 */
2734 				if (r != 0) {
2735 					if (needs_unbusy)
2736 						vfs_unbusy(new_mp);
2737 					goto invalid;
2738 				}
2739 				KASSERT(refp != NULL || nvp != NULL,
2740 				    ("%s: undetected lookup error", __func__));
2741 
2742 				if (refp == NULL &&
2743 				    ((nd->nd_flag & ND_NFSV3) ||
2744 				     NFSNONZERO_ATTRBIT(&attrbits))) {
2745 					r = nfsvno_getfh(nvp, &nfh, p);
2746 					if (!r)
2747 					    r = nfsvno_getattr(nvp, nvap, nd, p,
2748 						1, &attrbits);
2749 					if (r == 0 && is_zfs == 1 &&
2750 					    nfsrv_enable_crossmntpt != 0 &&
2751 					    (nd->nd_flag & ND_NFSV4) != 0 &&
2752 					    nvp->v_type == VDIR &&
2753 					    vp->v_mount != nvp->v_mount) {
2754 					    /*
2755 					     * For a ZFS snapshot, there is a
2756 					     * pseudo mount that does not set
2757 					     * v_mountedhere, so it needs to
2758 					     * be detected via a different
2759 					     * mount structure.
2760 					     */
2761 					    at_root = 1;
2762 					    if (new_mp == mp)
2763 						new_mp = nvp->v_mount;
2764 					}
2765 				}
2766 
2767 				/*
2768 				 * If we failed to get attributes of the entry,
2769 				 * then just skip it for NFSv3 (the traditional
2770 				 * behavior in the old NFS server).
2771 				 * For NFSv4 the behavior is controlled by
2772 				 * RDATTRERROR: we either ignore the error or
2773 				 * fail the request.
2774 				 * The exception is EOPNOTSUPP, which can be
2775 				 * returned by nfsvno_getfh() for certain
2776 				 * file systems, such as devfs.  This indicates
2777 				 * that the file system cannot be exported,
2778 				 * so just skip over the entry.
2779 				 * Note that RDATTRERROR is never set for NFSv3.
2780 				 */
2781 				if (r != 0) {
2782 					if (!NFSISSET_ATTRBIT(&attrbits,
2783 					    NFSATTRBIT_RDATTRERROR) ||
2784 					    r == EOPNOTSUPP) {
2785 						vput(nvp);
2786 						if (needs_unbusy != 0)
2787 							vfs_unbusy(new_mp);
2788 						if ((nd->nd_flag & ND_NFSV3) ||
2789 						    r == EOPNOTSUPP)
2790 							goto invalid;
2791 						nd->nd_repstat = r;
2792 						break;
2793 					}
2794 				}
2795 			} else if (NFSNONZERO_ATTRBIT(&attrbits)) {
2796 				/* Only need Type and/or Fileid. */
2797 				VATTR_NULL(&nvap->na_vattr);
2798 				nvap->na_fileid = dp->d_fileno;
2799 				nvap->na_type = NFS_DTYPETOVTYPE(dp->d_type);
2800 			}
2801 
2802 			/*
2803 			 * Build the directory record xdr
2804 			 */
2805 			if (nd->nd_flag & ND_NFSV3) {
2806 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2807 				*tl++ = newnfs_true;
2808 				txdr_hyper(dp->d_fileno, tl);
2809 				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2810 				NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2811 				txdr_hyper(*cookiep, tl);
2812 				nfsrv_postopattr(nd, 0, nvap);
2813 				dirlen += nfsm_fhtom(NULL, nd, (u_int8_t *)&nfh,
2814 				    0, 1);
2815 				dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR);
2816 				if (nvp != NULL)
2817 					vput(nvp);
2818 			} else {
2819 				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
2820 				*tl++ = newnfs_true;
2821 				txdr_hyper(*cookiep, tl);
2822 				dirlen += nfsm_strtom(nd, dp->d_name, nlen);
2823 				if (nvp != NULL) {
2824 					supports_nfsv4acls =
2825 					    nfs_supportsnfsv4acls(nvp);
2826 					NFSVOPUNLOCK(nvp);
2827 				} else
2828 					supports_nfsv4acls = 0;
2829 				if (refp != NULL) {
2830 					dirlen += nfsrv_putreferralattr(nd,
2831 					    &refbits, refp, 0,
2832 					    &nd->nd_repstat);
2833 					if (nd->nd_repstat) {
2834 						if (nvp != NULL)
2835 							vrele(nvp);
2836 						if (needs_unbusy != 0)
2837 							vfs_unbusy(new_mp);
2838 						break;
2839 					}
2840 				} else if (r) {
2841 					dirlen += nfsvno_fillattr(nd, new_mp,
2842 					    nvp, nvap, &nfh, r, &rderrbits,
2843 					    nd->nd_cred, p, isdgram, 0,
2844 					    supports_nfsv4acls, at_root,
2845 					    mounted_on_fileno);
2846 				} else {
2847 					dirlen += nfsvno_fillattr(nd, new_mp,
2848 					    nvp, nvap, &nfh, r, &attrbits,
2849 					    nd->nd_cred, p, isdgram, 0,
2850 					    supports_nfsv4acls, at_root,
2851 					    mounted_on_fileno);
2852 				}
2853 				if (nvp != NULL)
2854 					vrele(nvp);
2855 				dirlen += (3 * NFSX_UNSIGNED);
2856 			}
2857 			if (needs_unbusy != 0)
2858 				vfs_unbusy(new_mp);
2859 			if (dirlen <= cnt)
2860 				entrycnt++;
2861 		}
2862 invalid:
2863 		cpos += dp->d_reclen;
2864 		dp = (struct dirent *)cpos;
2865 		cookiep++;
2866 		ncookies--;
2867 	}
2868 	vrele(vp);
2869 	vfs_unbusy(mp);
2870 
2871 	/*
2872 	 * If dirlen > cnt, we must strip off the last entry. If that
2873 	 * results in an empty reply, report NFSERR_TOOSMALL.
2874 	 */
2875 	if (dirlen > cnt || nd->nd_repstat) {
2876 		if (!nd->nd_repstat && entrycnt == 0)
2877 			nd->nd_repstat = NFSERR_TOOSMALL;
2878 		if (nd->nd_repstat) {
2879 			nfsm_trimtrailing(nd, mb0, bpos0, bextpg0, bextpgsiz0);
2880 			if (nd->nd_flag & ND_NFSV3)
2881 				nfsrv_postopattr(nd, getret, &at);
2882 		} else
2883 			nfsm_trimtrailing(nd, mb1, bpos1, bextpg1, bextpgsiz1);
2884 		eofflag = 0;
2885 	} else if (cpos < cend)
2886 		eofflag = 0;
2887 	if (!nd->nd_repstat) {
2888 		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2889 		*tl++ = newnfs_false;
2890 		if (eofflag)
2891 			*tl = newnfs_true;
2892 		else
2893 			*tl = newnfs_false;
2894 	}
2895 	free(cookies, M_TEMP);
2896 	free(rbuf, M_TEMP);
2897 
2898 out:
2899 	NFSEXITCODE2(0, nd);
2900 	return (0);
2901 nfsmout:
2902 	vput(vp);
2903 	NFSEXITCODE2(error, nd);
2904 	return (error);
2905 }
2906 
2907 /*
2908  * Get the settable attributes out of the mbuf list.
2909  * (Return 0 or EBADRPC)
2910  */
2911 int
nfsrv_sattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSACL_T * aclp,struct thread * p)2912 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
2913     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
2914 {
2915 	u_int32_t *tl;
2916 	struct nfsv2_sattr *sp;
2917 	int error = 0, toclient = 0;
2918 
2919 	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
2920 	case ND_NFSV2:
2921 		NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
2922 		/*
2923 		 * Some old clients didn't fill in the high order 16bits.
2924 		 * --> check the low order 2 bytes for 0xffff
2925 		 */
2926 		if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff)
2927 			nvap->na_mode = nfstov_mode(sp->sa_mode);
2928 		if (sp->sa_uid != newnfs_xdrneg1)
2929 			nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid);
2930 		if (sp->sa_gid != newnfs_xdrneg1)
2931 			nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid);
2932 		if (sp->sa_size != newnfs_xdrneg1)
2933 			nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size);
2934 		if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) {
2935 #ifdef notyet
2936 			fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime);
2937 #else
2938 			nvap->na_atime.tv_sec =
2939 				fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec);
2940 			nvap->na_atime.tv_nsec = 0;
2941 #endif
2942 		}
2943 		if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1)
2944 			fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime);
2945 		break;
2946 	case ND_NFSV3:
2947 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2948 		if (*tl == newnfs_true) {
2949 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2950 			nvap->na_mode = nfstov_mode(*tl);
2951 		}
2952 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2953 		if (*tl == newnfs_true) {
2954 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2955 			nvap->na_uid = fxdr_unsigned(uid_t, *tl);
2956 		}
2957 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2958 		if (*tl == newnfs_true) {
2959 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2960 			nvap->na_gid = fxdr_unsigned(gid_t, *tl);
2961 		}
2962 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2963 		if (*tl == newnfs_true) {
2964 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2965 			nvap->na_size = fxdr_hyper(tl);
2966 		}
2967 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2968 		switch (fxdr_unsigned(int, *tl)) {
2969 		case NFSV3SATTRTIME_TOCLIENT:
2970 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2971 			fxdr_nfsv3time(tl, &nvap->na_atime);
2972 			toclient = 1;
2973 			break;
2974 		case NFSV3SATTRTIME_TOSERVER:
2975 			vfs_timestamp(&nvap->na_atime);
2976 			nvap->na_vaflags |= VA_UTIMES_NULL;
2977 			break;
2978 		}
2979 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
2980 		switch (fxdr_unsigned(int, *tl)) {
2981 		case NFSV3SATTRTIME_TOCLIENT:
2982 			NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
2983 			fxdr_nfsv3time(tl, &nvap->na_mtime);
2984 			nvap->na_vaflags &= ~VA_UTIMES_NULL;
2985 			break;
2986 		case NFSV3SATTRTIME_TOSERVER:
2987 			vfs_timestamp(&nvap->na_mtime);
2988 			if (!toclient)
2989 				nvap->na_vaflags |= VA_UTIMES_NULL;
2990 			break;
2991 		}
2992 		break;
2993 	case ND_NFSV4:
2994 		error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p);
2995 	}
2996 nfsmout:
2997 	NFSEXITCODE2(error, nd);
2998 	return (error);
2999 }
3000 
3001 /*
3002  * Handle the setable attributes for V4.
3003  * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise.
3004  */
3005 int
nfsv4_sattr(struct nfsrv_descript * nd,vnode_t vp,struct nfsvattr * nvap,nfsattrbit_t * attrbitp,NFSACL_T * aclp,struct thread * p)3006 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap,
3007     nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p)
3008 {
3009 	u_int32_t *tl;
3010 	int attrsum = 0;
3011 	int i, j;
3012 	int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0;
3013 	int moderet, toclient = 0;
3014 	u_char *cp, namestr[NFSV4_SMALLSTR + 1];
3015 	uid_t uid;
3016 	gid_t gid;
3017 	u_short mode, mask;		/* Same type as va_mode. */
3018 	struct vattr va;
3019 
3020 	error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup);
3021 	if (error)
3022 		goto nfsmout;
3023 	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3024 	attrsize = fxdr_unsigned(int, *tl);
3025 
3026 	/*
3027 	 * Loop around getting the setable attributes. If an unsupported
3028 	 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return.
3029 	 * Once nd_repstat != 0, do not set the attribute value, but keep
3030 	 * parsing the attribute(s).
3031 	 */
3032 	if (retnotsup) {
3033 		nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3034 		bitpos = NFSATTRBIT_MAX;
3035 	} else {
3036 		bitpos = 0;
3037 	}
3038 	moderet = 0;
3039 	for (; bitpos < NFSATTRBIT_MAX; bitpos++) {
3040 	    if (attrsum > attrsize) {
3041 		error = NFSERR_BADXDR;
3042 		goto nfsmout;
3043 	    }
3044 	    if (NFSISSET_ATTRBIT(attrbitp, bitpos))
3045 		switch (bitpos) {
3046 		case NFSATTRBIT_SIZE:
3047 			NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER);
3048 			if (!nd->nd_repstat) {
3049 				if (vp != NULL && vp->v_type != VREG)
3050 					nd->nd_repstat = (vp->v_type == VDIR) ?
3051 					    NFSERR_ISDIR : NFSERR_INVAL;
3052 				else
3053 					nvap->na_size = fxdr_hyper(tl);
3054 			}
3055 			attrsum += NFSX_HYPER;
3056 			break;
3057 		case NFSATTRBIT_ACL:
3058 			error = nfsrv_dissectacl(nd, aclp, true, &aceerr,
3059 			    &aclsize, p);
3060 			if (error)
3061 				goto nfsmout;
3062 			if (aceerr && !nd->nd_repstat)
3063 				nd->nd_repstat = aceerr;
3064 			attrsum += aclsize;
3065 			break;
3066 		case NFSATTRBIT_ARCHIVE:
3067 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3068 			if (!nd->nd_repstat)
3069 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3070 			attrsum += NFSX_UNSIGNED;
3071 			break;
3072 		case NFSATTRBIT_HIDDEN:
3073 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3074 			if (!nd->nd_repstat)
3075 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3076 			attrsum += NFSX_UNSIGNED;
3077 			break;
3078 		case NFSATTRBIT_MIMETYPE:
3079 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3080 			i = fxdr_unsigned(int, *tl);
3081 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
3082 			if (error)
3083 				goto nfsmout;
3084 			if (!nd->nd_repstat)
3085 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3086 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i));
3087 			break;
3088 		case NFSATTRBIT_MODE:
3089 			moderet = NFSERR_INVAL;	/* Can't do MODESETMASKED. */
3090 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3091 			if (!nd->nd_repstat)
3092 				nvap->na_mode = nfstov_mode(*tl);
3093 			attrsum += NFSX_UNSIGNED;
3094 			break;
3095 		case NFSATTRBIT_OWNER:
3096 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3097 			j = fxdr_unsigned(int, *tl);
3098 			if (j < 0) {
3099 				error = NFSERR_BADXDR;
3100 				goto nfsmout;
3101 			}
3102 			if (j > NFSV4_SMALLSTR)
3103 				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3104 			else
3105 				cp = namestr;
3106 			error = nfsrv_mtostr(nd, cp, j);
3107 			if (error) {
3108 				if (j > NFSV4_SMALLSTR)
3109 					free(cp, M_NFSSTRING);
3110 				goto nfsmout;
3111 			}
3112 			if (!nd->nd_repstat) {
3113 				nd->nd_repstat = nfsv4_strtouid(nd, cp, j,
3114 				    &uid);
3115 				if (!nd->nd_repstat)
3116 					nvap->na_uid = uid;
3117 			}
3118 			if (j > NFSV4_SMALLSTR)
3119 				free(cp, M_NFSSTRING);
3120 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3121 			break;
3122 		case NFSATTRBIT_OWNERGROUP:
3123 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3124 			j = fxdr_unsigned(int, *tl);
3125 			if (j < 0) {
3126 				error = NFSERR_BADXDR;
3127 				goto nfsmout;
3128 			}
3129 			if (j > NFSV4_SMALLSTR)
3130 				cp = malloc(j + 1, M_NFSSTRING, M_WAITOK);
3131 			else
3132 				cp = namestr;
3133 			error = nfsrv_mtostr(nd, cp, j);
3134 			if (error) {
3135 				if (j > NFSV4_SMALLSTR)
3136 					free(cp, M_NFSSTRING);
3137 				goto nfsmout;
3138 			}
3139 			if (!nd->nd_repstat) {
3140 				nd->nd_repstat = nfsv4_strtogid(nd, cp, j,
3141 				    &gid);
3142 				if (!nd->nd_repstat)
3143 					nvap->na_gid = gid;
3144 			}
3145 			if (j > NFSV4_SMALLSTR)
3146 				free(cp, M_NFSSTRING);
3147 			attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j));
3148 			break;
3149 		case NFSATTRBIT_SYSTEM:
3150 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3151 			if (!nd->nd_repstat)
3152 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3153 			attrsum += NFSX_UNSIGNED;
3154 			break;
3155 		case NFSATTRBIT_TIMEACCESSSET:
3156 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3157 			attrsum += NFSX_UNSIGNED;
3158 			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3159 			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3160 			    if (!nd->nd_repstat)
3161 				fxdr_nfsv4time(tl, &nvap->na_atime);
3162 			    toclient = 1;
3163 			    attrsum += NFSX_V4TIME;
3164 			} else if (!nd->nd_repstat) {
3165 			    vfs_timestamp(&nvap->na_atime);
3166 			    nvap->na_vaflags |= VA_UTIMES_NULL;
3167 			}
3168 			break;
3169 		case NFSATTRBIT_TIMEBACKUP:
3170 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3171 			if (!nd->nd_repstat)
3172 				nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3173 			attrsum += NFSX_V4TIME;
3174 			break;
3175 		case NFSATTRBIT_TIMECREATE:
3176 			NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3177 			if (!nd->nd_repstat)
3178 				fxdr_nfsv4time(tl, &nvap->na_btime);
3179 			attrsum += NFSX_V4TIME;
3180 			break;
3181 		case NFSATTRBIT_TIMEMODIFYSET:
3182 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
3183 			attrsum += NFSX_UNSIGNED;
3184 			if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) {
3185 			    NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME);
3186 			    if (!nd->nd_repstat)
3187 				fxdr_nfsv4time(tl, &nvap->na_mtime);
3188 			    nvap->na_vaflags &= ~VA_UTIMES_NULL;
3189 			    attrsum += NFSX_V4TIME;
3190 			} else if (!nd->nd_repstat) {
3191 			    vfs_timestamp(&nvap->na_mtime);
3192 			    if (!toclient)
3193 				nvap->na_vaflags |= VA_UTIMES_NULL;
3194 			}
3195 			break;
3196 		case NFSATTRBIT_MODESETMASKED:
3197 			NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3198 			mode = fxdr_unsigned(u_short, *tl++);
3199 			mask = fxdr_unsigned(u_short, *tl);
3200 			/*
3201 			 * vp == NULL implies an Open/Create operation.
3202 			 * This attribute can only be used for Setattr and
3203 			 * only for NFSv4.1 or higher.
3204 			 * If moderet != 0, a mode attribute has also been
3205 			 * specified and this attribute cannot be done in the
3206 			 * same Setattr operation.
3207 			 */
3208 			if (!nd->nd_repstat) {
3209 				if ((nd->nd_flag & ND_NFSV41) == 0)
3210 					nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3211 				else if ((mode & ~07777) != 0 ||
3212 				    (mask & ~07777) != 0 || vp == NULL)
3213 					nd->nd_repstat = NFSERR_INVAL;
3214 				else if (moderet == 0)
3215 					moderet = VOP_GETATTR(vp, &va,
3216 					    nd->nd_cred);
3217 				if (moderet == 0)
3218 					nvap->na_mode = (mode & mask) |
3219 					    (va.va_mode & ~mask);
3220 				else
3221 					nd->nd_repstat = moderet;
3222 			}
3223 			attrsum += 2 * NFSX_UNSIGNED;
3224 			break;
3225 		case NFSATTRBIT_MODEUMASK:
3226 			NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
3227 			mode = fxdr_unsigned(u_short, *tl++);
3228 			mask = fxdr_unsigned(u_short, *tl);
3229 			/*
3230 			 * If moderet != 0, mode has already been done.
3231 			 * If vp != NULL, this is not a file object creation.
3232 			 */
3233 			if (!nd->nd_repstat) {
3234 				if ((nd->nd_flag & ND_NFSV42) == 0)
3235 					nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3236 				else if ((mask & ~0777) != 0 || vp != NULL ||
3237 				    moderet != 0)
3238 					nd->nd_repstat = NFSERR_INVAL;
3239 				else
3240 					nvap->na_mode = (mode & ~mask);
3241 			}
3242 			attrsum += 2 * NFSX_UNSIGNED;
3243 			break;
3244 		default:
3245 			nd->nd_repstat = NFSERR_ATTRNOTSUPP;
3246 			/*
3247 			 * set bitpos so we drop out of the loop.
3248 			 */
3249 			bitpos = NFSATTRBIT_MAX;
3250 			break;
3251 		}
3252 	}
3253 
3254 	/*
3255 	 * some clients pad the attrlist, so we need to skip over the
3256 	 * padding.  This also skips over unparsed non-supported attributes.
3257 	 */
3258 	if (attrsum > attrsize) {
3259 		error = NFSERR_BADXDR;
3260 	} else {
3261 		attrsize = NFSM_RNDUP(attrsize);
3262 		if (attrsum < attrsize)
3263 			error = nfsm_advance(nd, attrsize - attrsum, -1);
3264 	}
3265 nfsmout:
3266 	NFSEXITCODE2(error, nd);
3267 	return (error);
3268 }
3269 
3270 /*
3271  * Check/setup export credentials.
3272  */
3273 int
nfsd_excred(struct nfsrv_descript * nd,struct nfsexstuff * exp,struct ucred * credanon,bool testsec)3274 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp,
3275     struct ucred *credanon, bool testsec)
3276 {
3277 	int error;
3278 
3279 	/*
3280 	 * Check/setup credentials.
3281 	 */
3282 	if (nd->nd_flag & ND_GSS)
3283 		exp->nes_exflag &= ~MNT_EXPORTANON;
3284 
3285 	/*
3286 	 * Check to see if the operation is allowed for this security flavor.
3287 	 */
3288 	error = 0;
3289 	if (testsec) {
3290 		error = nfsvno_testexp(nd, exp);
3291 		if (error != 0)
3292 			goto out;
3293 	}
3294 
3295 	/*
3296 	 * Check to see if the file system is exported V4 only.
3297 	 */
3298 	if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) {
3299 		error = NFSERR_PROGNOTV4;
3300 		goto out;
3301 	}
3302 
3303 	/*
3304 	 * Now, map the user credentials.
3305 	 * (Note that ND_AUTHNONE will only be set for an NFSv3
3306 	 *  Fsinfo RPC. If set for anything else, this code might need
3307 	 *  to change.)
3308 	 */
3309 	if (NFSVNO_EXPORTED(exp)) {
3310 		if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) ||
3311 		     NFSVNO_EXPORTANON(exp) ||
3312 		     (nd->nd_flag & ND_AUTHNONE) != 0) {
3313 			nd->nd_cred->cr_uid = credanon->cr_uid;
3314 			/*
3315 			 * 'credanon' is already a 'struct ucred' that was built
3316 			 * internally with calls to crsetgroups_fallback(), so
3317 			 * we don't need a fallback here.
3318 			 */
3319 			crsetgroups(nd->nd_cred, credanon->cr_ngroups,
3320 			    credanon->cr_groups);
3321 		} else if ((nd->nd_flag & ND_GSS) == 0) {
3322 			/*
3323 			 * If using AUTH_SYS, call nfsrv_getgrpscred() to see
3324 			 * if there is a replacement credential with a group
3325 			 * list set up by "nfsuserd -manage-gids".
3326 			 * If there is no replacement, nfsrv_getgrpscred()
3327 			 * simply returns its argument.
3328 			 */
3329 			nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred);
3330 		}
3331 	}
3332 
3333 out:
3334 	NFSEXITCODE2(error, nd);
3335 	return (error);
3336 }
3337 
3338 /*
3339  * Check exports.
3340  */
3341 int
nfsvno_checkexp(struct mount * mp,struct sockaddr * nam,struct nfsexstuff * exp,struct ucred ** credp)3342 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp,
3343     struct ucred **credp)
3344 {
3345 	int error;
3346 
3347 	error = 0;
3348 	*credp = NULL;
3349 	MNT_ILOCK(mp);
3350 	if (mp->mnt_exjail == NULL ||
3351 	    mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3352 		error = EACCES;
3353 	MNT_IUNLOCK(mp);
3354 	if (error == 0)
3355 		error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3356 		    &exp->nes_numsecflavor, exp->nes_secflavors);
3357 	if (error) {
3358 		if (NFSD_VNET(nfs_rootfhset)) {
3359 			exp->nes_exflag = 0;
3360 			exp->nes_numsecflavor = 0;
3361 			error = 0;
3362 		}
3363 	} else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3364 	    MAXSECFLAVORS) {
3365 		printf("nfsvno_checkexp: numsecflavors out of range\n");
3366 		exp->nes_numsecflavor = 0;
3367 		error = EACCES;
3368 	}
3369 	NFSEXITCODE(error);
3370 	return (error);
3371 }
3372 
3373 /*
3374  * Get a vnode for a file handle and export stuff.
3375  */
3376 int
nfsvno_fhtovp(struct mount * mp,fhandle_t * fhp,struct sockaddr * nam,int lktype,struct vnode ** vpp,struct nfsexstuff * exp,struct ucred ** credp)3377 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam,
3378     int lktype, struct vnode **vpp, struct nfsexstuff *exp,
3379     struct ucred **credp)
3380 {
3381 	int error;
3382 
3383 	*credp = NULL;
3384 	exp->nes_numsecflavor = 0;
3385 	error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp);
3386 	if (error != 0)
3387 		/* Make sure the server replies ESTALE to the client. */
3388 		error = ESTALE;
3389 	if (nam && !error) {
3390 		MNT_ILOCK(mp);
3391 		if (mp->mnt_exjail == NULL ||
3392 		    mp->mnt_exjail->cr_prison != curthread->td_ucred->cr_prison)
3393 			error = EACCES;
3394 		MNT_IUNLOCK(mp);
3395 		if (error == 0)
3396 			error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp,
3397 			    &exp->nes_numsecflavor, exp->nes_secflavors);
3398 		if (error) {
3399 			if (NFSD_VNET(nfs_rootfhset)) {
3400 				exp->nes_exflag = 0;
3401 				exp->nes_numsecflavor = 0;
3402 				error = 0;
3403 			} else {
3404 				vput(*vpp);
3405 			}
3406 		} else if (exp->nes_numsecflavor < 1 || exp->nes_numsecflavor >
3407 		    MAXSECFLAVORS) {
3408 			printf("nfsvno_fhtovp: numsecflavors out of range\n");
3409 			exp->nes_numsecflavor = 0;
3410 			error = EACCES;
3411 			vput(*vpp);
3412 		}
3413 	}
3414 	NFSEXITCODE(error);
3415 	return (error);
3416 }
3417 
3418 /*
3419  * nfsd_fhtovp() - convert a fh to a vnode ptr
3420  * 	- look up fsid in mount list (if not found ret error)
3421  *	- get vp and export rights by calling nfsvno_fhtovp()
3422  *	- if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
3423  *	  for AUTH_SYS
3424  *	- if mpp != NULL, return the mount point so that it can
3425  *	  be used for vn_finished_write() by the caller
3426  */
3427 void
nfsd_fhtovp(struct nfsrv_descript * nd,struct nfsrvfh * nfp,int lktype,struct vnode ** vpp,struct nfsexstuff * exp,struct mount ** mpp,int startwrite,int nextop)3428 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype,
3429     struct vnode **vpp, struct nfsexstuff *exp,
3430     struct mount **mpp, int startwrite, int nextop)
3431 {
3432 	struct mount *mp, *mpw;
3433 	struct ucred *credanon;
3434 	fhandle_t *fhp;
3435 	int error;
3436 
3437 	if (mpp != NULL)
3438 		*mpp = NULL;
3439 	*vpp = NULL;
3440 	fhp = (fhandle_t *)nfp->nfsrvfh_data;
3441 	mp = vfs_busyfs(&fhp->fh_fsid);
3442 	if (mp == NULL) {
3443 		nd->nd_repstat = ESTALE;
3444 		goto out;
3445 	}
3446 
3447 	if (startwrite) {
3448 		mpw = mp;
3449 		error = vn_start_write(NULL, &mpw, V_WAIT);
3450 		if (error != 0) {
3451 			mpw = NULL;
3452 			vfs_unbusy(mp);
3453 			nd->nd_repstat = ESTALE;
3454 			goto out;
3455 		}
3456 		if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp)))
3457 			lktype = LK_EXCLUSIVE;
3458 	} else
3459 		mpw = NULL;
3460 
3461 	nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp,
3462 	    &credanon);
3463 	vfs_unbusy(mp);
3464 
3465 	/*
3466 	 * For NFSv4 without a pseudo root fs, unexported file handles
3467 	 * can be returned, so that Lookup works everywhere.
3468 	 */
3469 	if (!nd->nd_repstat && exp->nes_exflag == 0 &&
3470 	    !(nd->nd_flag & ND_NFSV4)) {
3471 		vput(*vpp);
3472 		*vpp = NULL;
3473 		nd->nd_repstat = EACCES;
3474 	}
3475 
3476 	/*
3477 	 * Personally, I've never seen any point in requiring a
3478 	 * reserved port#, since only in the rare case where the
3479 	 * clients are all boxes with secure system privileges,
3480 	 * does it provide any enhanced security, but... some people
3481 	 * believe it to be useful and keep putting this code back in.
3482 	 * (There is also some "security checker" out there that
3483 	 *  complains if the nfs server doesn't enforce this.)
3484 	 * However, note the following:
3485 	 * RFC3530 (NFSv4) specifies that a reserved port# not be
3486 	 *	required.
3487 	 * RFC2623 recommends that, if a reserved port# is checked for,
3488 	 *	that there be a way to turn that off--> ifdef'd.
3489 	 */
3490 #ifdef NFS_REQRSVPORT
3491 	if (!nd->nd_repstat) {
3492 		struct sockaddr_in *saddr;
3493 		struct sockaddr_in6 *saddr6;
3494 
3495 		saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *);
3496 		saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *);
3497 		if (!(nd->nd_flag & ND_NFSV4) &&
3498 		    ((saddr->sin_family == AF_INET &&
3499 		      ntohs(saddr->sin_port) >= IPPORT_RESERVED) ||
3500 		     (saddr6->sin6_family == AF_INET6 &&
3501 		      ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) {
3502 			vput(*vpp);
3503 			nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK);
3504 		}
3505 	}
3506 #endif	/* NFS_REQRSVPORT */
3507 
3508 	/*
3509 	 * Check/setup credentials.
3510 	 */
3511 	if (!nd->nd_repstat) {
3512 		nd->nd_saveduid = nd->nd_cred->cr_uid;
3513 		nd->nd_repstat = nfsd_excred(nd, exp, credanon,
3514 		    nfsrv_checkwrongsec(nd, nextop, (*vpp)->v_type));
3515 		if (nd->nd_repstat)
3516 			vput(*vpp);
3517 	}
3518 	if (credanon != NULL)
3519 		crfree(credanon);
3520 	if (nd->nd_repstat) {
3521 		vn_finished_write(mpw);
3522 		*vpp = NULL;
3523 	} else if (mpp != NULL) {
3524 		*mpp = mpw;
3525 	}
3526 
3527 out:
3528 	NFSEXITCODE2(0, nd);
3529 }
3530 
3531 /*
3532  * glue for fp.
3533  */
3534 static int
fp_getfvp(struct thread * p,int fd,struct file ** fpp,struct vnode ** vpp)3535 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp)
3536 {
3537 	struct filedesc *fdp;
3538 	struct file *fp;
3539 	int error = 0;
3540 
3541 	fdp = p->td_proc->p_fd;
3542 	if (fd < 0 || fd >= fdp->fd_nfiles ||
3543 	    (fp = fdp->fd_ofiles[fd].fde_file) == NULL) {
3544 		error = EBADF;
3545 		goto out;
3546 	}
3547 	*fpp = fp;
3548 
3549 out:
3550 	NFSEXITCODE(error);
3551 	return (error);
3552 }
3553 
3554 /*
3555  * Called from nfssvc() to update the exports list. Just call
3556  * vfs_export(). This has to be done, since the v4 root fake fs isn't
3557  * in the mount list.
3558  */
3559 int
nfsrv_v4rootexport(void * argp,struct ucred * cred,struct thread * p)3560 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p)
3561 {
3562 	struct nfsex_args *nfsexargp = (struct nfsex_args *)argp;
3563 	int error = 0;
3564 	struct nameidata nd;
3565 	fhandle_t fh;
3566 
3567 	error = vfs_export(NFSD_VNET(nfsv4root_mnt), &nfsexargp->export, false);
3568 	if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0)
3569 		NFSD_VNET(nfs_rootfhset) = 0;
3570 	else if (error == 0) {
3571 		if (nfsexargp->fspec == NULL) {
3572 			error = EPERM;
3573 			goto out;
3574 		}
3575 		/*
3576 		 * If fspec != NULL, this is the v4root path.
3577 		 */
3578 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, nfsexargp->fspec);
3579 		if ((error = namei(&nd)) != 0)
3580 			goto out;
3581 		error = nfsvno_getfh(nd.ni_vp, &fh, p);
3582 		vrele(nd.ni_vp);
3583 		if (!error) {
3584 			NFSD_VNET(nfs_rootfh).nfsrvfh_len = NFSX_MYFH;
3585 			NFSBCOPY((caddr_t)&fh,
3586 			    NFSD_VNET(nfs_rootfh).nfsrvfh_data,
3587 			    sizeof (fhandle_t));
3588 			NFSD_VNET(nfs_rootfhset) = 1;
3589 		}
3590 	}
3591 
3592 out:
3593 	NFSEXITCODE(error);
3594 	return (error);
3595 }
3596 
3597 /*
3598  * This function needs to test to see if the system is near its limit
3599  * for memory allocation via malloc() or mget() and return True iff
3600  * either of these resources are near their limit.
3601  * XXX (For now, this is just a stub.)
3602  */
3603 int nfsrv_testmalloclimit = 0;
3604 int
nfsrv_mallocmget_limit(void)3605 nfsrv_mallocmget_limit(void)
3606 {
3607 	static int printmesg = 0;
3608 	static int testval = 1;
3609 
3610 	if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) {
3611 		if ((printmesg++ % 100) == 0)
3612 			printf("nfsd: malloc/mget near limit\n");
3613 		return (1);
3614 	}
3615 	return (0);
3616 }
3617 
3618 /*
3619  * BSD specific initialization of a mount point.
3620  */
3621 void
nfsd_mntinit(void)3622 nfsd_mntinit(void)
3623 {
3624 
3625 	NFSD_LOCK();
3626 	if (NFSD_VNET(nfsrv_mntinited)) {
3627 		NFSD_UNLOCK();
3628 		return;
3629 	}
3630 	NFSD_VNET(nfsrv_mntinited) = true;
3631 	nfsrvd_init(0);
3632 	NFSD_UNLOCK();
3633 
3634 	NFSD_VNET(nfsv4root_mnt) = malloc(sizeof(struct mount), M_TEMP,
3635 	    M_WAITOK | M_ZERO);
3636 	NFSD_VNET(nfsv4root_mnt)->mnt_flag = (MNT_RDONLY | MNT_EXPORTED);
3637 	mtx_init(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx, "nfs4mnt", NULL, MTX_DEF);
3638 	lockinit(&NFSD_VNET(nfsv4root_mnt)->mnt_explock, PVFS, "explock", 0, 0);
3639 	TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelist);
3640 	TAILQ_INIT(&NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelist);
3641 	NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
3642 	TAILQ_INIT(&NFSD_VNET(nfsv4root_opt));
3643 	TAILQ_INIT(&NFSD_VNET(nfsv4root_newopt));
3644 	NFSD_VNET(nfsv4root_mnt)->mnt_opt = &NFSD_VNET(nfsv4root_opt);
3645 	NFSD_VNET(nfsv4root_mnt)->mnt_optnew = &NFSD_VNET(nfsv4root_newopt);
3646 	NFSD_VNET(nfsv4root_mnt)->mnt_nvnodelistsize = 0;
3647 	NFSD_VNET(nfsv4root_mnt)->mnt_lazyvnodelistsize = 0;
3648 	callout_init(&NFSD_VNET(nfsd_callout), 1);
3649 
3650 	nfsrvd_initcache();
3651 	nfsd_init();
3652 }
3653 
3654 static void
nfsd_timer(void * arg)3655 nfsd_timer(void *arg)
3656 {
3657 	struct vnet *vnetp;
3658 
3659 	vnetp = (struct vnet *)arg;
3660 	NFSD_CURVNET_SET_QUIET(vnetp);
3661 	nfsrv_servertimer(vnetp);
3662 	callout_reset_sbt(&NFSD_VNET(nfsd_callout), SBT_1S, SBT_1S, nfsd_timer,
3663 	    arg, 0);
3664 	NFSD_CURVNET_RESTORE();
3665 }
3666 
3667 /*
3668  * Get a vnode for a file handle, without checking exports, etc.
3669  */
3670 struct vnode *
nfsvno_getvp(fhandle_t * fhp)3671 nfsvno_getvp(fhandle_t *fhp)
3672 {
3673 	struct mount *mp;
3674 	struct vnode *vp;
3675 	int error;
3676 
3677 	mp = vfs_busyfs(&fhp->fh_fsid);
3678 	if (mp == NULL)
3679 		return (NULL);
3680 	error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp);
3681 	vfs_unbusy(mp);
3682 	if (error)
3683 		return (NULL);
3684 	return (vp);
3685 }
3686 
3687 /*
3688  * Do a local VOP_ADVLOCK().
3689  */
3690 int
nfsvno_advlock(struct vnode * vp,int ftype,u_int64_t first,u_int64_t end,struct thread * td)3691 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first,
3692     u_int64_t end, struct thread *td)
3693 {
3694 	int error = 0;
3695 	struct flock fl;
3696 	u_int64_t tlen;
3697 
3698 	if (nfsrv_dolocallocks == 0)
3699 		goto out;
3700 	ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked");
3701 
3702 	fl.l_whence = SEEK_SET;
3703 	fl.l_type = ftype;
3704 	fl.l_start = (off_t)first;
3705 	if (end == NFS64BITSSET) {
3706 		fl.l_len = 0;
3707 	} else {
3708 		tlen = end - first;
3709 		fl.l_len = (off_t)tlen;
3710 	}
3711 	/*
3712 	 * For FreeBSD8, the l_pid and l_sysid must be set to the same
3713 	 * values for all calls, so that all locks will be held by the
3714 	 * nfsd server. (The nfsd server handles conflicts between the
3715 	 * various clients.)
3716 	 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024
3717 	 * bytes, so it can't be put in l_sysid.
3718 	 */
3719 	if (nfsv4_sysid == 0)
3720 		nfsv4_sysid = nlm_acquire_next_sysid();
3721 	fl.l_pid = (pid_t)0;
3722 	fl.l_sysid = (int)nfsv4_sysid;
3723 
3724 	if (ftype == F_UNLCK)
3725 		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl,
3726 		    (F_POSIX | F_REMOTE));
3727 	else
3728 		error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl,
3729 		    (F_POSIX | F_REMOTE));
3730 
3731 out:
3732 	NFSEXITCODE(error);
3733 	return (error);
3734 }
3735 
3736 /*
3737  * Check the nfsv4 root exports.
3738  */
3739 int
nfsvno_v4rootexport(struct nfsrv_descript * nd)3740 nfsvno_v4rootexport(struct nfsrv_descript *nd)
3741 {
3742 	struct ucred *credanon;
3743 	int error = 0, numsecflavor, secflavors[MAXSECFLAVORS], i;
3744 	uint64_t exflags;
3745 
3746 	error = vfs_stdcheckexp(NFSD_VNET(nfsv4root_mnt), nd->nd_nam, &exflags,
3747 	    &credanon, &numsecflavor, secflavors);
3748 	if (error) {
3749 		error = NFSERR_PROGUNAVAIL;
3750 		goto out;
3751 	}
3752 	if (credanon != NULL)
3753 		crfree(credanon);
3754 	for (i = 0; i < numsecflavor; i++) {
3755 		if (secflavors[i] == AUTH_SYS)
3756 			nd->nd_flag |= ND_EXAUTHSYS;
3757 		else if (secflavors[i] == RPCSEC_GSS_KRB5)
3758 			nd->nd_flag |= ND_EXGSS;
3759 		else if (secflavors[i] == RPCSEC_GSS_KRB5I)
3760 			nd->nd_flag |= ND_EXGSSINTEGRITY;
3761 		else if (secflavors[i] == RPCSEC_GSS_KRB5P)
3762 			nd->nd_flag |= ND_EXGSSPRIVACY;
3763 	}
3764 
3765 	/* And set ND_EXxx flags for TLS. */
3766 	if ((exflags & MNT_EXTLS) != 0) {
3767 		nd->nd_flag |= ND_EXTLS;
3768 		if ((exflags & MNT_EXTLSCERT) != 0)
3769 			nd->nd_flag |= ND_EXTLSCERT;
3770 		if ((exflags & MNT_EXTLSCERTUSER) != 0)
3771 			nd->nd_flag |= ND_EXTLSCERTUSER;
3772 	}
3773 
3774 out:
3775 	NFSEXITCODE(error);
3776 	return (error);
3777 }
3778 
3779 /*
3780  * Nfs server pseudo system call for the nfsd's
3781  */
3782 /*
3783  * MPSAFE
3784  */
3785 static int
nfssvc_nfsd(struct thread * td,struct nfssvc_args * uap)3786 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap)
3787 {
3788 	struct file *fp;
3789 	struct nfsd_addsock_args sockarg;
3790 	struct nfsd_nfsd_args nfsdarg;
3791 	struct nfsd_nfsd_oargs onfsdarg;
3792 	struct nfsd_pnfsd_args pnfsdarg;
3793 	struct vnode *vp, *nvp, *curdvp;
3794 	struct pnfsdsfile *pf;
3795 	struct nfsdevice *ds, *fds;
3796 	cap_rights_t rights;
3797 	int buflen, error, ret;
3798 	char *buf, *cp, *cp2, *cp3;
3799 	char fname[PNFS_FILENAME_LEN + 1];
3800 
3801 	NFSD_CURVNET_SET(NFSD_TD_TO_VNET(td));
3802 	if (uap->flag & NFSSVC_NFSDADDSOCK) {
3803 		error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg));
3804 		if (error)
3805 			goto out;
3806 		/*
3807 		 * Since we don't know what rights might be required,
3808 		 * pretend that we need them all. It is better to be too
3809 		 * careful than too reckless.
3810 		 */
3811 		error = fget(td, sockarg.sock,
3812 		    cap_rights_init_one(&rights, CAP_SOCK_SERVER), &fp);
3813 		if (error != 0)
3814 			goto out;
3815 		if (fp->f_type != DTYPE_SOCKET) {
3816 			fdrop(fp, td);
3817 			error = EPERM;
3818 			goto out;
3819 		}
3820 		error = nfsrvd_addsock(fp);
3821 		fdrop(fp, td);
3822 	} else if (uap->flag & NFSSVC_NFSDNFSD) {
3823 		if (uap->argp == NULL) {
3824 			error = EINVAL;
3825 			goto out;
3826 		}
3827 		if ((uap->flag & NFSSVC_NEWSTRUCT) == 0) {
3828 			error = copyin(uap->argp, &onfsdarg, sizeof(onfsdarg));
3829 			if (error == 0) {
3830 				nfsdarg.principal = onfsdarg.principal;
3831 				nfsdarg.minthreads = onfsdarg.minthreads;
3832 				nfsdarg.maxthreads = onfsdarg.maxthreads;
3833 				nfsdarg.version = 1;
3834 				nfsdarg.addr = NULL;
3835 				nfsdarg.addrlen = 0;
3836 				nfsdarg.dnshost = NULL;
3837 				nfsdarg.dnshostlen = 0;
3838 				nfsdarg.dspath = NULL;
3839 				nfsdarg.dspathlen = 0;
3840 				nfsdarg.mdspath = NULL;
3841 				nfsdarg.mdspathlen = 0;
3842 				nfsdarg.mirrorcnt = 1;
3843 			}
3844 		} else
3845 			error = copyin(uap->argp, &nfsdarg, sizeof(nfsdarg));
3846 		if (error)
3847 			goto out;
3848 		if (nfsdarg.addrlen > 0 && nfsdarg.addrlen < 10000 &&
3849 		    nfsdarg.dnshostlen > 0 && nfsdarg.dnshostlen < 10000 &&
3850 		    nfsdarg.dspathlen > 0 && nfsdarg.dspathlen < 10000 &&
3851 		    nfsdarg.mdspathlen > 0 && nfsdarg.mdspathlen < 10000 &&
3852 		    nfsdarg.mirrorcnt >= 1 &&
3853 		    nfsdarg.mirrorcnt <= NFSDEV_MAXMIRRORS &&
3854 		    nfsdarg.addr != NULL && nfsdarg.dnshost != NULL &&
3855 		    nfsdarg.dspath != NULL && nfsdarg.mdspath != NULL) {
3856 			NFSD_DEBUG(1, "addrlen=%d dspathlen=%d dnslen=%d"
3857 			    " mdspathlen=%d mirrorcnt=%d\n", nfsdarg.addrlen,
3858 			    nfsdarg.dspathlen, nfsdarg.dnshostlen,
3859 			    nfsdarg.mdspathlen, nfsdarg.mirrorcnt);
3860 			cp = malloc(nfsdarg.addrlen + 1, M_TEMP, M_WAITOK);
3861 			error = copyin(nfsdarg.addr, cp, nfsdarg.addrlen);
3862 			if (error != 0) {
3863 				free(cp, M_TEMP);
3864 				goto out;
3865 			}
3866 			cp[nfsdarg.addrlen] = '\0';	/* Ensure nul term. */
3867 			nfsdarg.addr = cp;
3868 			cp = malloc(nfsdarg.dnshostlen + 1, M_TEMP, M_WAITOK);
3869 			error = copyin(nfsdarg.dnshost, cp, nfsdarg.dnshostlen);
3870 			if (error != 0) {
3871 				free(nfsdarg.addr, M_TEMP);
3872 				free(cp, M_TEMP);
3873 				goto out;
3874 			}
3875 			cp[nfsdarg.dnshostlen] = '\0';	/* Ensure nul term. */
3876 			nfsdarg.dnshost = cp;
3877 			cp = malloc(nfsdarg.dspathlen + 1, M_TEMP, M_WAITOK);
3878 			error = copyin(nfsdarg.dspath, cp, nfsdarg.dspathlen);
3879 			if (error != 0) {
3880 				free(nfsdarg.addr, M_TEMP);
3881 				free(nfsdarg.dnshost, M_TEMP);
3882 				free(cp, M_TEMP);
3883 				goto out;
3884 			}
3885 			cp[nfsdarg.dspathlen] = '\0';	/* Ensure nul term. */
3886 			nfsdarg.dspath = cp;
3887 			cp = malloc(nfsdarg.mdspathlen + 1, M_TEMP, M_WAITOK);
3888 			error = copyin(nfsdarg.mdspath, cp, nfsdarg.mdspathlen);
3889 			if (error != 0) {
3890 				free(nfsdarg.addr, M_TEMP);
3891 				free(nfsdarg.dnshost, M_TEMP);
3892 				free(nfsdarg.dspath, M_TEMP);
3893 				free(cp, M_TEMP);
3894 				goto out;
3895 			}
3896 			cp[nfsdarg.mdspathlen] = '\0';	/* Ensure nul term. */
3897 			nfsdarg.mdspath = cp;
3898 		} else {
3899 			nfsdarg.addr = NULL;
3900 			nfsdarg.addrlen = 0;
3901 			nfsdarg.dnshost = NULL;
3902 			nfsdarg.dnshostlen = 0;
3903 			nfsdarg.dspath = NULL;
3904 			nfsdarg.dspathlen = 0;
3905 			nfsdarg.mdspath = NULL;
3906 			nfsdarg.mdspathlen = 0;
3907 			nfsdarg.mirrorcnt = 1;
3908 		}
3909 		nfsd_timer(NFSD_TD_TO_VNET(td));
3910 		error = nfsrvd_nfsd(td, &nfsdarg);
3911 		callout_drain(&NFSD_VNET(nfsd_callout));
3912 		free(nfsdarg.addr, M_TEMP);
3913 		free(nfsdarg.dnshost, M_TEMP);
3914 		free(nfsdarg.dspath, M_TEMP);
3915 		free(nfsdarg.mdspath, M_TEMP);
3916 	} else if (uap->flag & NFSSVC_PNFSDS) {
3917 		error = copyin(uap->argp, &pnfsdarg, sizeof(pnfsdarg));
3918 		if (error == 0 && (pnfsdarg.op == PNFSDOP_DELDSSERVER ||
3919 		    pnfsdarg.op == PNFSDOP_FORCEDELDS)) {
3920 			cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3921 			error = copyinstr(pnfsdarg.dspath, cp, PATH_MAX + 1,
3922 			    NULL);
3923 			if (error == 0)
3924 				error = nfsrv_deldsserver(pnfsdarg.op, cp, td);
3925 			free(cp, M_TEMP);
3926 		} else if (error == 0 && pnfsdarg.op == PNFSDOP_COPYMR) {
3927 			cp = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3928 			buflen = sizeof(*pf) * NFSDEV_MAXMIRRORS;
3929 			buf = malloc(buflen, M_TEMP, M_WAITOK);
3930 			error = copyinstr(pnfsdarg.mdspath, cp, PATH_MAX + 1,
3931 			    NULL);
3932 			NFSD_DEBUG(4, "pnfsdcopymr cp mdspath=%d\n", error);
3933 			if (error == 0 && pnfsdarg.dspath != NULL) {
3934 				cp2 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3935 				error = copyinstr(pnfsdarg.dspath, cp2,
3936 				    PATH_MAX + 1, NULL);
3937 				NFSD_DEBUG(4, "pnfsdcopymr cp dspath=%d\n",
3938 				    error);
3939 			} else
3940 				cp2 = NULL;
3941 			if (error == 0 && pnfsdarg.curdspath != NULL) {
3942 				cp3 = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
3943 				error = copyinstr(pnfsdarg.curdspath, cp3,
3944 				    PATH_MAX + 1, NULL);
3945 				NFSD_DEBUG(4, "pnfsdcopymr cp curdspath=%d\n",
3946 				    error);
3947 			} else
3948 				cp3 = NULL;
3949 			curdvp = NULL;
3950 			fds = NULL;
3951 			if (error == 0)
3952 				error = nfsrv_mdscopymr(cp, cp2, cp3, buf,
3953 				    &buflen, fname, td, &vp, &nvp, &pf, &ds,
3954 				    &fds);
3955 			NFSD_DEBUG(4, "nfsrv_mdscopymr=%d\n", error);
3956 			if (error == 0) {
3957 				if (pf->dsf_dir >= nfsrv_dsdirsize) {
3958 					printf("copymr: dsdir out of range\n");
3959 					pf->dsf_dir = 0;
3960 				}
3961 				NFSD_DEBUG(4, "copymr: buflen=%d\n", buflen);
3962 				error = nfsrv_copymr(vp, nvp,
3963 				    ds->nfsdev_dsdir[pf->dsf_dir], ds, pf,
3964 				    (struct pnfsdsfile *)buf,
3965 				    buflen / sizeof(*pf), td->td_ucred, td);
3966 				vput(vp);
3967 				vput(nvp);
3968 				if (fds != NULL && error == 0) {
3969 					curdvp = fds->nfsdev_dsdir[pf->dsf_dir];
3970 					ret = vn_lock(curdvp, LK_EXCLUSIVE);
3971 					if (ret == 0) {
3972 						nfsrv_dsremove(curdvp, fname,
3973 						    td->td_ucred, td);
3974 						NFSVOPUNLOCK(curdvp);
3975 					}
3976 				}
3977 				NFSD_DEBUG(4, "nfsrv_copymr=%d\n", error);
3978 			}
3979 			free(cp, M_TEMP);
3980 			free(cp2, M_TEMP);
3981 			free(cp3, M_TEMP);
3982 			free(buf, M_TEMP);
3983 		}
3984 	} else {
3985 		error = nfssvc_srvcall(td, uap, td->td_ucred);
3986 	}
3987 
3988 out:
3989 	NFSD_CURVNET_RESTORE();
3990 	NFSEXITCODE(error);
3991 	return (error);
3992 }
3993 
3994 static int
nfssvc_srvcall(struct thread * p,struct nfssvc_args * uap,struct ucred * cred)3995 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred)
3996 {
3997 	struct nfsex_args export;
3998 	struct nfsex_oldargs oexp;
3999 	struct file *fp = NULL;
4000 	int stablefd, i, len;
4001 	struct nfsd_clid adminrevoke;
4002 	struct nfsd_dumplist dumplist;
4003 	struct nfsd_dumpclients *dumpclients;
4004 	struct nfsd_dumplocklist dumplocklist;
4005 	struct nfsd_dumplocks *dumplocks;
4006 	struct nameidata nd;
4007 	vnode_t vp;
4008 	int error = EINVAL, igotlock;
4009 	struct proc *procp;
4010 	gid_t *grps;
4011 
4012 	if (uap->flag & NFSSVC_PUBLICFH) {
4013 		NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data,
4014 		    sizeof (fhandle_t));
4015 		error = copyin(uap->argp,
4016 		    &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t));
4017 		if (!error)
4018 			nfs_pubfhset = 1;
4019 	} else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
4020 	    (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) {
4021 		error = copyin(uap->argp,(caddr_t)&export,
4022 		    sizeof (struct nfsex_args));
4023 		if (!error) {
4024 			grps = NULL;
4025 			if (export.export.ex_ngroups > NGROUPS_MAX ||
4026 			    export.export.ex_ngroups < 0)
4027 				error = EINVAL;
4028 			else if (export.export.ex_ngroups > 0) {
4029 				grps = malloc(export.export.ex_ngroups *
4030 				    sizeof(gid_t), M_TEMP, M_WAITOK);
4031 				error = copyin(export.export.ex_groups, grps,
4032 				    export.export.ex_ngroups * sizeof(gid_t));
4033 				export.export.ex_groups = grps;
4034 			} else
4035 				export.export.ex_groups = NULL;
4036 			if (!error)
4037 				error = nfsrv_v4rootexport(&export, cred, p);
4038 			free(grps, M_TEMP);
4039 		}
4040 	} else if ((uap->flag & (NFSSVC_V4ROOTEXPORT | NFSSVC_NEWSTRUCT)) ==
4041 	    NFSSVC_V4ROOTEXPORT) {
4042 		error = copyin(uap->argp,(caddr_t)&oexp,
4043 		    sizeof (struct nfsex_oldargs));
4044 		if (!error) {
4045 			memset(&export.export, 0, sizeof(export.export));
4046 			export.export.ex_flags = (uint64_t)oexp.export.ex_flags;
4047 			export.export.ex_root = oexp.export.ex_root;
4048 			export.export.ex_uid = oexp.export.ex_anon.cr_uid;
4049 			export.export.ex_ngroups =
4050 			    oexp.export.ex_anon.cr_ngroups;
4051 			export.export.ex_groups = NULL;
4052 			if (export.export.ex_ngroups > XU_NGROUPS ||
4053 			    export.export.ex_ngroups < 0)
4054 				error = EINVAL;
4055 			else if (export.export.ex_ngroups > 0) {
4056 				export.export.ex_groups = malloc(
4057 				    export.export.ex_ngroups * sizeof(gid_t),
4058 				    M_TEMP, M_WAITOK);
4059 				for (i = 0; i < export.export.ex_ngroups; i++)
4060 					export.export.ex_groups[i] =
4061 					    oexp.export.ex_anon.cr_groups[i];
4062 			}
4063 			export.export.ex_addr = oexp.export.ex_addr;
4064 			export.export.ex_addrlen = oexp.export.ex_addrlen;
4065 			export.export.ex_mask = oexp.export.ex_mask;
4066 			export.export.ex_masklen = oexp.export.ex_masklen;
4067 			export.export.ex_indexfile = oexp.export.ex_indexfile;
4068 			export.export.ex_numsecflavors =
4069 			    oexp.export.ex_numsecflavors;
4070 			if (export.export.ex_numsecflavors >= MAXSECFLAVORS ||
4071 			    export.export.ex_numsecflavors < 0)
4072 				error = EINVAL;
4073 			else {
4074 				for (i = 0; i < export.export.ex_numsecflavors;
4075 				    i++)
4076 					export.export.ex_secflavors[i] =
4077 					    oexp.export.ex_secflavors[i];
4078 			}
4079 			export.fspec = oexp.fspec;
4080 			if (error == 0)
4081 				error = nfsrv_v4rootexport(&export, cred, p);
4082 			free(export.export.ex_groups, M_TEMP);
4083 		}
4084 	} else if (uap->flag & NFSSVC_NOPUBLICFH) {
4085 		nfs_pubfhset = 0;
4086 		error = 0;
4087 	} else if (uap->flag & NFSSVC_STABLERESTART) {
4088 		error = copyin(uap->argp, (caddr_t)&stablefd,
4089 		    sizeof (int));
4090 		if (!error)
4091 			error = fp_getfvp(p, stablefd, &fp, &vp);
4092 		if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE))
4093 			error = EBADF;
4094 		if (!error && NFSD_VNET(nfsrv_numnfsd) != 0)
4095 			error = ENXIO;
4096 		if (!error) {
4097 			NFSD_VNET(nfsrv_stablefirst).nsf_fp = fp;
4098 			nfsrv_setupstable(p);
4099 		}
4100 	} else if (uap->flag & NFSSVC_ADMINREVOKE) {
4101 		error = copyin(uap->argp, (caddr_t)&adminrevoke,
4102 		    sizeof (struct nfsd_clid));
4103 		if (!error)
4104 			error = nfsrv_adminrevoke(&adminrevoke, p);
4105 	} else if (uap->flag & NFSSVC_DUMPCLIENTS) {
4106 		error = copyin(uap->argp, (caddr_t)&dumplist,
4107 		    sizeof (struct nfsd_dumplist));
4108 		if (!error && (dumplist.ndl_size < 1 ||
4109 			dumplist.ndl_size > NFSRV_MAXDUMPLIST))
4110 			error = EPERM;
4111 		if (!error) {
4112 		    len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size;
4113 		    dumpclients = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4114 		    nfsrv_dumpclients(dumpclients, dumplist.ndl_size);
4115 		    error = copyout(dumpclients, dumplist.ndl_list, len);
4116 		    free(dumpclients, M_TEMP);
4117 		}
4118 	} else if (uap->flag & NFSSVC_DUMPLOCKS) {
4119 		error = copyin(uap->argp, (caddr_t)&dumplocklist,
4120 		    sizeof (struct nfsd_dumplocklist));
4121 		if (!error && (dumplocklist.ndllck_size < 1 ||
4122 			dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST))
4123 			error = EPERM;
4124 		if (!error)
4125 			error = nfsrv_lookupfilename(&nd,
4126 				dumplocklist.ndllck_fname, p);
4127 		if (!error) {
4128 			len = sizeof (struct nfsd_dumplocks) *
4129 				dumplocklist.ndllck_size;
4130 			dumplocks = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
4131 			nfsrv_dumplocks(nd.ni_vp, dumplocks,
4132 			    dumplocklist.ndllck_size, p);
4133 			vput(nd.ni_vp);
4134 			error = copyout(dumplocks, dumplocklist.ndllck_list,
4135 			    len);
4136 			free(dumplocks, M_TEMP);
4137 		}
4138 	} else if (uap->flag & NFSSVC_BACKUPSTABLE) {
4139 		procp = p->td_proc;
4140 		PROC_LOCK(procp);
4141 		nfsd_master_pid = procp->p_pid;
4142 		bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1);
4143 		nfsd_master_start = procp->p_stats->p_start;
4144 		NFSD_VNET(nfsd_master_proc) = procp;
4145 		PROC_UNLOCK(procp);
4146 	} else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) {
4147 		NFSLOCKV4ROOTMUTEX();
4148 		if (!NFSD_VNET(nfsrv_suspend_nfsd)) {
4149 			/* Lock out all nfsd threads */
4150 			do {
4151 				igotlock = nfsv4_lock(
4152 				    &NFSD_VNET(nfsd_suspend_lock), 1, NULL,
4153 				    NFSV4ROOTLOCKMUTEXPTR, NULL);
4154 			} while (igotlock == 0 &&
4155 			    !NFSD_VNET(nfsrv_suspend_nfsd));
4156 			NFSD_VNET(nfsrv_suspend_nfsd) = true;
4157 		}
4158 		NFSUNLOCKV4ROOTMUTEX();
4159 		error = 0;
4160 	} else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) {
4161 		NFSLOCKV4ROOTMUTEX();
4162 		if (NFSD_VNET(nfsrv_suspend_nfsd)) {
4163 			nfsv4_unlock(&NFSD_VNET(nfsd_suspend_lock), 0);
4164 			NFSD_VNET(nfsrv_suspend_nfsd) = false;
4165 		}
4166 		NFSUNLOCKV4ROOTMUTEX();
4167 		error = 0;
4168 	}
4169 
4170 	NFSEXITCODE(error);
4171 	return (error);
4172 }
4173 
4174 /*
4175  * Check exports.
4176  * Returns 0 if ok, 1 otherwise.
4177  */
4178 int
nfsvno_testexp(struct nfsrv_descript * nd,struct nfsexstuff * exp)4179 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp)
4180 {
4181 	int i;
4182 
4183 	if ((NFSVNO_EXTLS(exp) && (nd->nd_flag & ND_TLS) == 0) ||
4184 	    (NFSVNO_EXTLSCERT(exp) &&
4185 	     (nd->nd_flag & ND_TLSCERT) == 0) ||
4186 	    (NFSVNO_EXTLSCERTUSER(exp) &&
4187 	     (nd->nd_flag & ND_TLSCERTUSER) == 0)) {
4188 		if ((nd->nd_flag & ND_NFSV4) != 0)
4189 			return (NFSERR_WRONGSEC);
4190 #ifdef notnow
4191 		/* There is currently no auth_stat for this. */
4192 		else if ((nd->nd_flag & ND_TLS) == 0)
4193 			return (NFSERR_AUTHERR | AUTH_NEEDS_TLS);
4194 		else
4195 			return (NFSERR_AUTHERR | AUTH_NEEDS_TLS_MUTUAL_HOST);
4196 #endif
4197 		else
4198 			return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4199 	}
4200 
4201 	/*
4202 	 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to use
4203 	 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS.
4204 	 */
4205 	if ((nd->nd_flag & ND_NFSV3) != 0 && nd->nd_procnum == NFSPROC_FSINFO)
4206 		return (0);
4207 
4208 	/*
4209 	 * This seems odd, but allow the case where the security flavor
4210 	 * list is empty. This happens when NFSv4 is traversing non-exported
4211 	 * file systems. Exported file systems should always have a non-empty
4212 	 * security flavor list.
4213 	 */
4214 	if (exp->nes_numsecflavor == 0)
4215 		return (0);
4216 
4217 	for (i = 0; i < exp->nes_numsecflavor; i++) {
4218 		/*
4219 		 * The tests for privacy and integrity must be first,
4220 		 * since ND_GSS is set for everything but AUTH_SYS.
4221 		 */
4222 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P &&
4223 		    (nd->nd_flag & ND_GSSPRIVACY))
4224 			return (0);
4225 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I &&
4226 		    (nd->nd_flag & ND_GSSINTEGRITY))
4227 			return (0);
4228 		if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 &&
4229 		    (nd->nd_flag & ND_GSS))
4230 			return (0);
4231 		if (exp->nes_secflavors[i] == AUTH_SYS &&
4232 		    (nd->nd_flag & ND_GSS) == 0)
4233 			return (0);
4234 	}
4235 	if ((nd->nd_flag & ND_NFSV4) != 0)
4236 		return (NFSERR_WRONGSEC);
4237 	return (NFSERR_AUTHERR | AUTH_TOOWEAK);
4238 }
4239 
4240 /*
4241  * Calculate a hash value for the fid in a file handle.
4242  */
4243 uint32_t
nfsrv_hashfh(fhandle_t * fhp)4244 nfsrv_hashfh(fhandle_t *fhp)
4245 {
4246 	uint32_t hashval;
4247 
4248 	hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0);
4249 	return (hashval);
4250 }
4251 
4252 /*
4253  * Calculate a hash value for the sessionid.
4254  */
4255 uint32_t
nfsrv_hashsessionid(uint8_t * sessionid)4256 nfsrv_hashsessionid(uint8_t *sessionid)
4257 {
4258 	uint32_t hashval;
4259 
4260 	hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0);
4261 	return (hashval);
4262 }
4263 
4264 /*
4265  * Signal the userland master nfsd to backup the stable restart file.
4266  */
4267 void
nfsrv_backupstable(void)4268 nfsrv_backupstable(void)
4269 {
4270 	struct proc *procp;
4271 
4272 	if (NFSD_VNET(nfsd_master_proc) != NULL) {
4273 		procp = pfind(nfsd_master_pid);
4274 		/* Try to make sure it is the correct process. */
4275 		if (procp == NFSD_VNET(nfsd_master_proc) &&
4276 		    procp->p_stats->p_start.tv_sec ==
4277 		    nfsd_master_start.tv_sec &&
4278 		    procp->p_stats->p_start.tv_usec ==
4279 		    nfsd_master_start.tv_usec &&
4280 		    strcmp(procp->p_comm, nfsd_master_comm) == 0)
4281 			kern_psignal(procp, SIGUSR2);
4282 		else
4283 			NFSD_VNET(nfsd_master_proc) = NULL;
4284 
4285 		if (procp != NULL)
4286 			PROC_UNLOCK(procp);
4287 	}
4288 }
4289 
4290 /*
4291  * Create a DS data file for nfsrv_pnfscreate(). Called for each mirror.
4292  * The arguments are in a structure, so that they can be passed through
4293  * taskqueue for a kernel process to execute this function.
4294  */
4295 struct nfsrvdscreate {
4296 	int			done;
4297 	int			inprog;
4298 	struct task		tsk;
4299 	struct ucred		*tcred;
4300 	struct vnode		*dvp;
4301 	NFSPROC_T		*p;
4302 	struct pnfsdsfile	*pf;
4303 	int			err;
4304 	fhandle_t		fh;
4305 	struct vattr		va;
4306 	struct vattr		createva;
4307 };
4308 
4309 int
nfsrv_dscreate(struct vnode * dvp,struct vattr * vap,struct vattr * nvap,fhandle_t * fhp,struct pnfsdsfile * pf,struct pnfsdsattr * dsa,char * fnamep,struct ucred * tcred,NFSPROC_T * p,struct vnode ** nvpp)4310 nfsrv_dscreate(struct vnode *dvp, struct vattr *vap, struct vattr *nvap,
4311     fhandle_t *fhp, struct pnfsdsfile *pf, struct pnfsdsattr *dsa,
4312     char *fnamep, struct ucred *tcred, NFSPROC_T *p, struct vnode **nvpp)
4313 {
4314 	struct vnode *nvp;
4315 	struct nameidata named;
4316 	struct vattr va;
4317 	char *bufp;
4318 	u_long *hashp;
4319 	struct nfsnode *np;
4320 	struct nfsmount *nmp;
4321 	int error;
4322 
4323 	NFSNAMEICNDSET(&named.ni_cnd, tcred, CREATE,
4324 	    LOCKPARENT | LOCKLEAF | NOCACHE);
4325 	nfsvno_setpathbuf(&named, &bufp, &hashp);
4326 	named.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
4327 	named.ni_cnd.cn_nameptr = bufp;
4328 	if (fnamep != NULL) {
4329 		strlcpy(bufp, fnamep, PNFS_FILENAME_LEN + 1);
4330 		named.ni_cnd.cn_namelen = strlen(bufp);
4331 	} else
4332 		named.ni_cnd.cn_namelen = nfsrv_putfhname(fhp, bufp);
4333 	NFSD_DEBUG(4, "nfsrv_dscreate: dvp=%p fname=%s\n", dvp, bufp);
4334 
4335 	/* Create the date file in the DS mount. */
4336 	error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4337 	if (error == 0) {
4338 		error = VOP_CREATE(dvp, &nvp, &named.ni_cnd, vap);
4339 		vref(dvp);
4340 		VOP_VPUT_PAIR(dvp, error == 0 ? &nvp : NULL, false);
4341 		if (error == 0) {
4342 			/* Set the ownership of the file. */
4343 			error = VOP_SETATTR(nvp, nvap, tcred);
4344 			NFSD_DEBUG(4, "nfsrv_dscreate:"
4345 			    " setattr-uid=%d\n", error);
4346 			if (error != 0)
4347 				vput(nvp);
4348 		}
4349 		if (error != 0)
4350 			printf("pNFS: pnfscreate failed=%d\n", error);
4351 	} else
4352 		printf("pNFS: pnfscreate vnlock=%d\n", error);
4353 	if (error == 0) {
4354 		np = VTONFS(nvp);
4355 		nmp = VFSTONFS(nvp->v_mount);
4356 		if (strcmp(nvp->v_mount->mnt_vfc->vfc_name, "nfs")
4357 		    != 0 || nmp->nm_nam->sa_len > sizeof(
4358 		    struct sockaddr_in6) ||
4359 		    np->n_fhp->nfh_len != NFSX_MYFH) {
4360 			printf("Bad DS file: fstype=%s salen=%d"
4361 			    " fhlen=%d\n",
4362 			    nvp->v_mount->mnt_vfc->vfc_name,
4363 			    nmp->nm_nam->sa_len, np->n_fhp->nfh_len);
4364 			error = ENOENT;
4365 		}
4366 
4367 		/* Set extattrs for the DS on the MDS file. */
4368 		if (error == 0) {
4369 			if (dsa != NULL) {
4370 				error = VOP_GETATTR(nvp, &va, tcred);
4371 				if (error == 0) {
4372 					dsa->dsa_filerev = va.va_filerev;
4373 					dsa->dsa_size = va.va_size;
4374 					dsa->dsa_atime = va.va_atime;
4375 					dsa->dsa_mtime = va.va_mtime;
4376 					dsa->dsa_bytes = va.va_bytes;
4377 				}
4378 			}
4379 			if (error == 0) {
4380 				NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh,
4381 				    NFSX_MYFH);
4382 				NFSBCOPY(nmp->nm_nam, &pf->dsf_sin,
4383 				    nmp->nm_nam->sa_len);
4384 				NFSBCOPY(named.ni_cnd.cn_nameptr,
4385 				    pf->dsf_filename,
4386 				    sizeof(pf->dsf_filename));
4387 			}
4388 		} else
4389 			printf("pNFS: pnfscreate can't get DS"
4390 			    " attr=%d\n", error);
4391 		if (nvpp != NULL && error == 0)
4392 			*nvpp = nvp;
4393 		else
4394 			vput(nvp);
4395 	}
4396 	nfsvno_relpathbuf(&named);
4397 	return (error);
4398 }
4399 
4400 /*
4401  * Start up the thread that will execute nfsrv_dscreate().
4402  */
4403 static void
start_dscreate(void * arg,int pending)4404 start_dscreate(void *arg, int pending)
4405 {
4406 	struct nfsrvdscreate *dsc;
4407 
4408 	dsc = (struct nfsrvdscreate *)arg;
4409 	dsc->err = nfsrv_dscreate(dsc->dvp, &dsc->createva, &dsc->va, &dsc->fh,
4410 	    dsc->pf, NULL, NULL, dsc->tcred, dsc->p, NULL);
4411 	dsc->done = 1;
4412 	NFSD_DEBUG(4, "start_dscreate: err=%d\n", dsc->err);
4413 }
4414 
4415 /*
4416  * Create a pNFS data file on the Data Server(s).
4417  */
4418 static void
nfsrv_pnfscreate(struct vnode * vp,struct vattr * vap,struct ucred * cred,NFSPROC_T * p)4419 nfsrv_pnfscreate(struct vnode *vp, struct vattr *vap, struct ucred *cred,
4420     NFSPROC_T *p)
4421 {
4422 	struct nfsrvdscreate *dsc, *tdsc = NULL;
4423 	struct nfsdevice *ds, *tds, *fds;
4424 	struct mount *mp;
4425 	struct pnfsdsfile *pf, *tpf;
4426 	struct pnfsdsattr dsattr;
4427 	struct vattr va;
4428 	struct vnode *dvp[NFSDEV_MAXMIRRORS];
4429 	struct nfsmount *nmp;
4430 	fhandle_t fh;
4431 	uid_t vauid;
4432 	gid_t vagid;
4433 	u_short vamode;
4434 	struct ucred *tcred;
4435 	int dsdir[NFSDEV_MAXMIRRORS], error, i, mirrorcnt, ret;
4436 	int failpos, timo;
4437 
4438 	/* Get a DS server directory in a round-robin order. */
4439 	mirrorcnt = 1;
4440 	mp = vp->v_mount;
4441 	ds = fds = NULL;
4442 	NFSDDSLOCK();
4443 	/*
4444 	 * Search for the first entry that handles this MDS fs, but use the
4445 	 * first entry for all MDS fs's otherwise.
4446 	 */
4447 	TAILQ_FOREACH(tds, &nfsrv_devidhead, nfsdev_list) {
4448 		if (tds->nfsdev_nmp != NULL) {
4449 			if (tds->nfsdev_mdsisset == 0 && ds == NULL)
4450 				ds = tds;
4451 			else if (tds->nfsdev_mdsisset != 0 && fsidcmp(
4452 			    &mp->mnt_stat.f_fsid, &tds->nfsdev_mdsfsid) == 0) {
4453 				ds = fds = tds;
4454 				break;
4455 			}
4456 		}
4457 	}
4458 	if (ds == NULL) {
4459 		NFSDDSUNLOCK();
4460 		NFSD_DEBUG(4, "nfsrv_pnfscreate: no srv\n");
4461 		return;
4462 	}
4463 	i = dsdir[0] = ds->nfsdev_nextdir;
4464 	ds->nfsdev_nextdir = (ds->nfsdev_nextdir + 1) % nfsrv_dsdirsize;
4465 	dvp[0] = ds->nfsdev_dsdir[i];
4466 	tds = TAILQ_NEXT(ds, nfsdev_list);
4467 	if (nfsrv_maxpnfsmirror > 1 && tds != NULL) {
4468 		TAILQ_FOREACH_FROM(tds, &nfsrv_devidhead, nfsdev_list) {
4469 			if (tds->nfsdev_nmp != NULL &&
4470 			    ((tds->nfsdev_mdsisset == 0 && fds == NULL) ||
4471 			     (tds->nfsdev_mdsisset != 0 && fds != NULL &&
4472 			      fsidcmp(&mp->mnt_stat.f_fsid,
4473 			      &tds->nfsdev_mdsfsid) == 0))) {
4474 				dsdir[mirrorcnt] = i;
4475 				dvp[mirrorcnt] = tds->nfsdev_dsdir[i];
4476 				mirrorcnt++;
4477 				if (mirrorcnt >= nfsrv_maxpnfsmirror)
4478 					break;
4479 			}
4480 		}
4481 	}
4482 	/* Put at end of list to implement round-robin usage. */
4483 	TAILQ_REMOVE(&nfsrv_devidhead, ds, nfsdev_list);
4484 	TAILQ_INSERT_TAIL(&nfsrv_devidhead, ds, nfsdev_list);
4485 	NFSDDSUNLOCK();
4486 	dsc = NULL;
4487 	if (mirrorcnt > 1)
4488 		tdsc = dsc = malloc(sizeof(*dsc) * (mirrorcnt - 1), M_TEMP,
4489 		    M_WAITOK | M_ZERO);
4490 	tpf = pf = malloc(sizeof(*pf) * nfsrv_maxpnfsmirror, M_TEMP, M_WAITOK |
4491 	    M_ZERO);
4492 
4493 	error = nfsvno_getfh(vp, &fh, p);
4494 	if (error == 0)
4495 		error = VOP_GETATTR(vp, &va, cred);
4496 	if (error == 0) {
4497 		/* Set the attributes for "vp" to Setattr the DS vp. */
4498 		vauid = va.va_uid;
4499 		vagid = va.va_gid;
4500 		vamode = va.va_mode;
4501 		VATTR_NULL(&va);
4502 		va.va_uid = vauid;
4503 		va.va_gid = vagid;
4504 		va.va_mode = vamode;
4505 		va.va_size = 0;
4506 	} else
4507 		printf("pNFS: pnfscreate getfh+attr=%d\n", error);
4508 
4509 	NFSD_DEBUG(4, "nfsrv_pnfscreate: cruid=%d crgid=%d\n", cred->cr_uid,
4510 	    cred->cr_gid);
4511 	/* Make data file name based on FH. */
4512 	tcred = newnfs_getcred();
4513 
4514 	/*
4515 	 * Create the file on each DS mirror, using kernel process(es) for the
4516 	 * additional mirrors.
4517 	 */
4518 	failpos = -1;
4519 	for (i = 0; i < mirrorcnt - 1 && error == 0; i++, tpf++, tdsc++) {
4520 		tpf->dsf_dir = dsdir[i];
4521 		tdsc->tcred = tcred;
4522 		tdsc->p = p;
4523 		tdsc->pf = tpf;
4524 		tdsc->createva = *vap;
4525 		NFSBCOPY(&fh, &tdsc->fh, sizeof(fh));
4526 		tdsc->va = va;
4527 		tdsc->dvp = dvp[i];
4528 		tdsc->done = 0;
4529 		tdsc->inprog = 0;
4530 		tdsc->err = 0;
4531 		ret = EIO;
4532 		if (nfs_pnfsiothreads != 0) {
4533 			ret = nfs_pnfsio(start_dscreate, tdsc);
4534 			NFSD_DEBUG(4, "nfsrv_pnfscreate: nfs_pnfsio=%d\n", ret);
4535 		}
4536 		if (ret != 0) {
4537 			ret = nfsrv_dscreate(dvp[i], vap, &va, &fh, tpf, NULL,
4538 			    NULL, tcred, p, NULL);
4539 			if (ret != 0) {
4540 				KASSERT(error == 0, ("nfsrv_dscreate err=%d",
4541 				    error));
4542 				if (failpos == -1 && nfsds_failerr(ret))
4543 					failpos = i;
4544 				else
4545 					error = ret;
4546 			}
4547 		}
4548 	}
4549 	if (error == 0) {
4550 		tpf->dsf_dir = dsdir[mirrorcnt - 1];
4551 		error = nfsrv_dscreate(dvp[mirrorcnt - 1], vap, &va, &fh, tpf,
4552 		    &dsattr, NULL, tcred, p, NULL);
4553 		if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(error)) {
4554 			failpos = mirrorcnt - 1;
4555 			error = 0;
4556 		}
4557 	}
4558 	timo = hz / 50;		/* Wait for 20msec. */
4559 	if (timo < 1)
4560 		timo = 1;
4561 	/* Wait for kernel task(s) to complete. */
4562 	for (tdsc = dsc, i = 0; i < mirrorcnt - 1; i++, tdsc++) {
4563 		while (tdsc->inprog != 0 && tdsc->done == 0)
4564 			tsleep(&tdsc->tsk, PVFS, "srvdcr", timo);
4565 		if (tdsc->err != 0) {
4566 			if (failpos == -1 && nfsds_failerr(tdsc->err))
4567 				failpos = i;
4568 			else if (error == 0)
4569 				error = tdsc->err;
4570 		}
4571 	}
4572 
4573 	/*
4574 	 * If failpos has been set, that mirror has failed, so it needs
4575 	 * to be disabled.
4576 	 */
4577 	if (failpos >= 0) {
4578 		nmp = VFSTONFS(dvp[failpos]->v_mount);
4579 		NFSLOCKMNT(nmp);
4580 		if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4581 		     NFSMNTP_CANCELRPCS)) == 0) {
4582 			nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4583 			NFSUNLOCKMNT(nmp);
4584 			ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4585 			NFSD_DEBUG(4, "dscreatfail fail=%d ds=%p\n", failpos,
4586 			    ds);
4587 			if (ds != NULL)
4588 				nfsrv_killrpcs(nmp);
4589 			NFSLOCKMNT(nmp);
4590 			nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4591 			wakeup(nmp);
4592 		}
4593 		NFSUNLOCKMNT(nmp);
4594 	}
4595 
4596 	NFSFREECRED(tcred);
4597 	if (error == 0) {
4598 		ASSERT_VOP_ELOCKED(vp, "nfsrv_pnfscreate vp");
4599 
4600 		NFSD_DEBUG(4, "nfsrv_pnfscreate: mirrorcnt=%d maxmirror=%d\n",
4601 		    mirrorcnt, nfsrv_maxpnfsmirror);
4602 		/*
4603 		 * For all mirrors that couldn't be created, fill in the
4604 		 * *pf structure, but with an IP address == 0.0.0.0.
4605 		 */
4606 		tpf = pf + mirrorcnt;
4607 		for (i = mirrorcnt; i < nfsrv_maxpnfsmirror; i++, tpf++) {
4608 			*tpf = *pf;
4609 			tpf->dsf_sin.sin_family = AF_INET;
4610 			tpf->dsf_sin.sin_len = sizeof(struct sockaddr_in);
4611 			tpf->dsf_sin.sin_addr.s_addr = 0;
4612 			tpf->dsf_sin.sin_port = 0;
4613 		}
4614 
4615 		error = vn_extattr_set(vp, IO_NODELOCKED,
4616 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile",
4617 		    sizeof(*pf) * nfsrv_maxpnfsmirror, (char *)pf, p);
4618 		if (error == 0)
4619 			error = vn_extattr_set(vp, IO_NODELOCKED,
4620 			    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr",
4621 			    sizeof(dsattr), (char *)&dsattr, p);
4622 		if (error != 0)
4623 			printf("pNFS: pnfscreate setextattr=%d\n",
4624 			    error);
4625 	} else
4626 		printf("pNFS: pnfscreate=%d\n", error);
4627 	free(pf, M_TEMP);
4628 	free(dsc, M_TEMP);
4629 }
4630 
4631 /*
4632  * Get the information needed to remove the pNFS Data Server file from the
4633  * Metadata file.  Upon success, ddvp is set non-NULL to the locked
4634  * DS directory vnode.  The caller must unlock *ddvp when done with it.
4635  */
4636 static void
nfsrv_pnfsremovesetup(struct vnode * vp,NFSPROC_T * p,struct vnode ** dvpp,int * mirrorcntp,char * fname,fhandle_t * fhp)4637 nfsrv_pnfsremovesetup(struct vnode *vp, NFSPROC_T *p, struct vnode **dvpp,
4638     int *mirrorcntp, char *fname, fhandle_t *fhp)
4639 {
4640 	struct vattr va;
4641 	struct ucred *tcred;
4642 	char *buf;
4643 	int buflen, error;
4644 
4645 	dvpp[0] = NULL;
4646 	/* If not an exported regular file or not a pNFS server, just return. */
4647 	if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4648 	    nfsrv_devidcnt == 0)
4649 		return;
4650 
4651 	/* Check to see if this is the last hard link. */
4652 	tcred = newnfs_getcred();
4653 	error = VOP_GETATTR(vp, &va, tcred);
4654 	NFSFREECRED(tcred);
4655 	if (error != 0) {
4656 		printf("pNFS: nfsrv_pnfsremovesetup getattr=%d\n", error);
4657 		return;
4658 	}
4659 	if (va.va_nlink > 1)
4660 		return;
4661 
4662 	error = nfsvno_getfh(vp, fhp, p);
4663 	if (error != 0) {
4664 		printf("pNFS: nfsrv_pnfsremovesetup getfh=%d\n", error);
4665 		return;
4666 	}
4667 
4668 	buflen = 1024;
4669 	buf = malloc(buflen, M_TEMP, M_WAITOK);
4670 	/* Get the directory vnode for the DS mount and the file handle. */
4671 	error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, dvpp,
4672 	    NULL, NULL, fname, NULL, NULL, NULL, NULL, NULL);
4673 	free(buf, M_TEMP);
4674 	if (error != 0)
4675 		printf("pNFS: nfsrv_pnfsremovesetup getsockmnt=%d\n", error);
4676 }
4677 
4678 /*
4679  * Remove a DS data file for nfsrv_pnfsremove(). Called for each mirror.
4680  * The arguments are in a structure, so that they can be passed through
4681  * taskqueue for a kernel process to execute this function.
4682  */
4683 struct nfsrvdsremove {
4684 	int			done;
4685 	int			inprog;
4686 	struct task		tsk;
4687 	struct ucred		*tcred;
4688 	struct vnode		*dvp;
4689 	NFSPROC_T		*p;
4690 	int			err;
4691 	char			fname[PNFS_FILENAME_LEN + 1];
4692 };
4693 
4694 static int
nfsrv_dsremove(struct vnode * dvp,char * fname,struct ucred * tcred,NFSPROC_T * p)4695 nfsrv_dsremove(struct vnode *dvp, char *fname, struct ucred *tcred,
4696     NFSPROC_T *p)
4697 {
4698 	struct nameidata named;
4699 	struct vnode *nvp;
4700 	char *bufp;
4701 	u_long *hashp;
4702 	int error;
4703 
4704 	error = NFSVOPLOCK(dvp, LK_EXCLUSIVE);
4705 	if (error != 0)
4706 		return (error);
4707 	named.ni_cnd.cn_nameiop = DELETE;
4708 	named.ni_cnd.cn_lkflags = LK_EXCLUSIVE | LK_RETRY;
4709 	named.ni_cnd.cn_cred = tcred;
4710 	named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
4711 	nfsvno_setpathbuf(&named, &bufp, &hashp);
4712 	named.ni_cnd.cn_nameptr = bufp;
4713 	named.ni_cnd.cn_namelen = strlen(fname);
4714 	strlcpy(bufp, fname, NAME_MAX);
4715 	NFSD_DEBUG(4, "nfsrv_pnfsremove: filename=%s\n", bufp);
4716 	error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
4717 	NFSD_DEBUG(4, "nfsrv_pnfsremove: aft LOOKUP=%d\n", error);
4718 	if (error == 0) {
4719 		error = VOP_REMOVE(dvp, nvp, &named.ni_cnd);
4720 		vput(nvp);
4721 	}
4722 	NFSVOPUNLOCK(dvp);
4723 	nfsvno_relpathbuf(&named);
4724 	if (error != 0)
4725 		printf("pNFS: nfsrv_pnfsremove failed=%d\n", error);
4726 	return (error);
4727 }
4728 
4729 /*
4730  * Start up the thread that will execute nfsrv_dsremove().
4731  */
4732 static void
start_dsremove(void * arg,int pending)4733 start_dsremove(void *arg, int pending)
4734 {
4735 	struct nfsrvdsremove *dsrm;
4736 
4737 	dsrm = (struct nfsrvdsremove *)arg;
4738 	dsrm->err = nfsrv_dsremove(dsrm->dvp, dsrm->fname, dsrm->tcred,
4739 	    dsrm->p);
4740 	dsrm->done = 1;
4741 	NFSD_DEBUG(4, "start_dsremove: err=%d\n", dsrm->err);
4742 }
4743 
4744 /*
4745  * Remove a pNFS data file from a Data Server.
4746  * nfsrv_pnfsremovesetup() must have been called before the MDS file was
4747  * removed to set up the dvp and fill in the FH.
4748  */
4749 static void
nfsrv_pnfsremove(struct vnode ** dvp,int mirrorcnt,char * fname,fhandle_t * fhp,NFSPROC_T * p)4750 nfsrv_pnfsremove(struct vnode **dvp, int mirrorcnt, char *fname, fhandle_t *fhp,
4751     NFSPROC_T *p)
4752 {
4753 	struct ucred *tcred;
4754 	struct nfsrvdsremove *dsrm, *tdsrm;
4755 	struct nfsdevice *ds;
4756 	struct nfsmount *nmp;
4757 	int failpos, i, ret, timo;
4758 
4759 	tcred = newnfs_getcred();
4760 	dsrm = NULL;
4761 	if (mirrorcnt > 1)
4762 		dsrm = malloc(sizeof(*dsrm) * mirrorcnt - 1, M_TEMP, M_WAITOK);
4763 	/*
4764 	 * Remove the file on each DS mirror, using kernel process(es) for the
4765 	 * additional mirrors.
4766 	 */
4767 	failpos = -1;
4768 	for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4769 		tdsrm->tcred = tcred;
4770 		tdsrm->p = p;
4771 		tdsrm->dvp = dvp[i];
4772 		strlcpy(tdsrm->fname, fname, PNFS_FILENAME_LEN + 1);
4773 		tdsrm->inprog = 0;
4774 		tdsrm->done = 0;
4775 		tdsrm->err = 0;
4776 		ret = EIO;
4777 		if (nfs_pnfsiothreads != 0) {
4778 			ret = nfs_pnfsio(start_dsremove, tdsrm);
4779 			NFSD_DEBUG(4, "nfsrv_pnfsremove: nfs_pnfsio=%d\n", ret);
4780 		}
4781 		if (ret != 0) {
4782 			ret = nfsrv_dsremove(dvp[i], fname, tcred, p);
4783 			if (failpos == -1 && nfsds_failerr(ret))
4784 				failpos = i;
4785 		}
4786 	}
4787 	ret = nfsrv_dsremove(dvp[mirrorcnt - 1], fname, tcred, p);
4788 	if (failpos == -1 && mirrorcnt > 1 && nfsds_failerr(ret))
4789 		failpos = mirrorcnt - 1;
4790 	timo = hz / 50;		/* Wait for 20msec. */
4791 	if (timo < 1)
4792 		timo = 1;
4793 	/* Wait for kernel task(s) to complete. */
4794 	for (tdsrm = dsrm, i = 0; i < mirrorcnt - 1; i++, tdsrm++) {
4795 		while (tdsrm->inprog != 0 && tdsrm->done == 0)
4796 			tsleep(&tdsrm->tsk, PVFS, "srvdsrm", timo);
4797 		if (failpos == -1 && nfsds_failerr(tdsrm->err))
4798 			failpos = i;
4799 	}
4800 
4801 	/*
4802 	 * If failpos has been set, that mirror has failed, so it needs
4803 	 * to be disabled.
4804 	 */
4805 	if (failpos >= 0) {
4806 		nmp = VFSTONFS(dvp[failpos]->v_mount);
4807 		NFSLOCKMNT(nmp);
4808 		if ((nmp->nm_privflag & (NFSMNTP_FORCEDISM |
4809 		     NFSMNTP_CANCELRPCS)) == 0) {
4810 			nmp->nm_privflag |= NFSMNTP_CANCELRPCS;
4811 			NFSUNLOCKMNT(nmp);
4812 			ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER, nmp, p);
4813 			NFSD_DEBUG(4, "dsremovefail fail=%d ds=%p\n", failpos,
4814 			    ds);
4815 			if (ds != NULL)
4816 				nfsrv_killrpcs(nmp);
4817 			NFSLOCKMNT(nmp);
4818 			nmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
4819 			wakeup(nmp);
4820 		}
4821 		NFSUNLOCKMNT(nmp);
4822 	}
4823 
4824 	/* Get rid all layouts for the file. */
4825 	nfsrv_freefilelayouts(fhp);
4826 
4827 	NFSFREECRED(tcred);
4828 	free(dsrm, M_TEMP);
4829 }
4830 
4831 /*
4832  * Generate a file name based on the file handle and put it in *bufp.
4833  * Return the number of bytes generated.
4834  */
4835 static int
nfsrv_putfhname(fhandle_t * fhp,char * bufp)4836 nfsrv_putfhname(fhandle_t *fhp, char *bufp)
4837 {
4838 	int i;
4839 	uint8_t *cp;
4840 	const uint8_t *hexdigits = "0123456789abcdef";
4841 
4842 	cp = (uint8_t *)fhp;
4843 	for (i = 0; i < sizeof(*fhp); i++) {
4844 		bufp[2 * i] = hexdigits[(*cp >> 4) & 0xf];
4845 		bufp[2 * i + 1] = hexdigits[*cp++ & 0xf];
4846 	}
4847 	bufp[2 * i] = '\0';
4848 	return (2 * i);
4849 }
4850 
4851 /*
4852  * Update the Metadata file's attributes from the DS file when a Read/Write
4853  * layout is returned.
4854  * Basically just call nfsrv_proxyds() with procedure == NFSPROC_LAYOUTRETURN
4855  * so that it does a nfsrv_getattrdsrpc() and nfsrv_setextattr() on the DS file.
4856  */
4857 int
nfsrv_updatemdsattr(struct vnode * vp,struct nfsvattr * nap,NFSPROC_T * p)4858 nfsrv_updatemdsattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
4859 {
4860 	struct ucred *tcred;
4861 	int error;
4862 
4863 	/* Do this as root so that it won't fail with EACCES. */
4864 	tcred = newnfs_getcred();
4865 	error = nfsrv_proxyds(vp, 0, 0, tcred, p, NFSPROC_LAYOUTRETURN,
4866 	    NULL, NULL, NULL, nap, NULL, NULL, 0, NULL);
4867 	NFSFREECRED(tcred);
4868 	return (error);
4869 }
4870 
4871 /*
4872  * Set the NFSv4 ACL on the DS file to the same ACL as the MDS file.
4873  */
4874 static int
nfsrv_dssetacl(struct vnode * vp,struct acl * aclp,struct ucred * cred,NFSPROC_T * p)4875 nfsrv_dssetacl(struct vnode *vp, struct acl *aclp, struct ucred *cred,
4876     NFSPROC_T *p)
4877 {
4878 	int error;
4879 
4880 	error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SETACL,
4881 	    NULL, NULL, NULL, NULL, aclp, NULL, 0, NULL);
4882 	return (error);
4883 }
4884 
4885 static int
nfsrv_proxyds(struct vnode * vp,off_t off,int cnt,struct ucred * cred,struct thread * p,int ioproc,struct mbuf ** mpp,char * cp,struct mbuf ** mpp2,struct nfsvattr * nap,struct acl * aclp,off_t * offp,int content,bool * eofp)4886 nfsrv_proxyds(struct vnode *vp, off_t off, int cnt, struct ucred *cred,
4887     struct thread *p, int ioproc, struct mbuf **mpp, char *cp,
4888     struct mbuf **mpp2, struct nfsvattr *nap, struct acl *aclp,
4889     off_t *offp, int content, bool *eofp)
4890 {
4891 	struct nfsmount *nmp[NFSDEV_MAXMIRRORS], *failnmp;
4892 	fhandle_t fh[NFSDEV_MAXMIRRORS];
4893 	struct vnode *dvp[NFSDEV_MAXMIRRORS];
4894 	struct nfsdevice *ds;
4895 	struct pnfsdsattr dsattr;
4896 	struct opnfsdsattr odsattr;
4897 	char *buf;
4898 	int buflen, error, failpos, i, mirrorcnt, origmircnt, trycnt;
4899 
4900 	NFSD_DEBUG(4, "in nfsrv_proxyds\n");
4901 	/*
4902 	 * If not a regular file, not exported or not a pNFS server,
4903 	 * just return ENOENT.
4904 	 */
4905 	if (vp->v_type != VREG || (vp->v_mount->mnt_flag & MNT_EXPORTED) == 0 ||
4906 	    nfsrv_devidcnt == 0)
4907 		return (ENOENT);
4908 
4909 	buflen = 1024;
4910 	buf = malloc(buflen, M_TEMP, M_WAITOK);
4911 	error = 0;
4912 
4913 	/*
4914 	 * For Getattr, get the Change attribute (va_filerev) and size (va_size)
4915 	 * from the MetaData file's extended attribute.
4916 	 */
4917 	if (ioproc == NFSPROC_GETATTR) {
4918 		error = vn_extattr_get(vp, IO_NODELOCKED,
4919 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsattr", &buflen, buf,
4920 		    p);
4921 		if (error == 0) {
4922 			if (buflen == sizeof(odsattr)) {
4923 				NFSBCOPY(buf, &odsattr, buflen);
4924 				nap->na_filerev = odsattr.dsa_filerev;
4925 				nap->na_size = odsattr.dsa_size;
4926 				nap->na_atime = odsattr.dsa_atime;
4927 				nap->na_mtime = odsattr.dsa_mtime;
4928 				/*
4929 				 * Fake na_bytes by rounding up na_size.
4930 				 * Since we don't know the block size, just
4931 				 * use BLKDEV_IOSIZE.
4932 				 */
4933 				nap->na_bytes = (odsattr.dsa_size +
4934 				    BLKDEV_IOSIZE - 1) & ~(BLKDEV_IOSIZE - 1);
4935 			} else if (buflen == sizeof(dsattr)) {
4936 				NFSBCOPY(buf, &dsattr, buflen);
4937 				nap->na_filerev = dsattr.dsa_filerev;
4938 				nap->na_size = dsattr.dsa_size;
4939 				nap->na_atime = dsattr.dsa_atime;
4940 				nap->na_mtime = dsattr.dsa_mtime;
4941 				nap->na_bytes = dsattr.dsa_bytes;
4942 			} else
4943 				error = ENXIO;
4944 		}
4945 		if (error == 0) {
4946 			/*
4947 			 * If nfsrv_pnfsgetdsattr is 0 or nfsrv_checkdsattr()
4948 			 * returns 0, just return now.  nfsrv_checkdsattr()
4949 			 * returns 0 if there is no Read/Write layout
4950 			 * plus either an Open/Write_access or Write
4951 			 * delegation issued to a client for the file.
4952 			 */
4953 			if (nfsrv_pnfsgetdsattr == 0 ||
4954 			    nfsrv_checkdsattr(vp, p) == 0) {
4955 				free(buf, M_TEMP);
4956 				return (error);
4957 			}
4958 		}
4959 
4960 		/*
4961 		 * Clear ENOATTR so the code below will attempt to do a
4962 		 * nfsrv_getattrdsrpc() to get the attributes and (re)create
4963 		 * the extended attribute.
4964 		 */
4965 		if (error == ENOATTR)
4966 			error = 0;
4967 	}
4968 
4969 	origmircnt = -1;
4970 	trycnt = 0;
4971 tryagain:
4972 	if (error == 0) {
4973 		buflen = 1024;
4974 		if (ioproc == NFSPROC_READDS && NFSVOPISLOCKED(vp) ==
4975 		    LK_EXCLUSIVE)
4976 			printf("nfsrv_proxyds: Readds vp exclusively locked\n");
4977 		error = nfsrv_dsgetsockmnt(vp, LK_SHARED, buf, &buflen,
4978 		    &mirrorcnt, p, dvp, fh, NULL, NULL, NULL, NULL, NULL,
4979 		    NULL, NULL);
4980 		if (error == 0) {
4981 			for (i = 0; i < mirrorcnt; i++)
4982 				nmp[i] = VFSTONFS(dvp[i]->v_mount);
4983 		} else
4984 			printf("pNFS: proxy getextattr sockaddr=%d\n", error);
4985 	} else
4986 		printf("pNFS: nfsrv_dsgetsockmnt=%d\n", error);
4987 	if (error == 0) {
4988 		failpos = -1;
4989 		if (origmircnt == -1)
4990 			origmircnt = mirrorcnt;
4991 		/*
4992 		 * If failpos is set to a mirror#, then that mirror has
4993 		 * failed and will be disabled. For Read, Getattr and Seek, the
4994 		 * function only tries one mirror, so if that mirror has
4995 		 * failed, it will need to be retried. As such, increment
4996 		 * tryitagain for these cases.
4997 		 * For Write, Setattr and Setacl, the function tries all
4998 		 * mirrors and will not return an error for the case where
4999 		 * one mirror has failed. For these cases, the functioning
5000 		 * mirror(s) will have been modified, so a retry isn't
5001 		 * necessary. These functions will set failpos for the
5002 		 * failed mirror#.
5003 		 */
5004 		if (ioproc == NFSPROC_READDS) {
5005 			error = nfsrv_readdsrpc(fh, off, cnt, cred, p, nmp[0],
5006 			    mpp, mpp2);
5007 			if (nfsds_failerr(error) && mirrorcnt > 1) {
5008 				/*
5009 				 * Setting failpos will cause the mirror
5010 				 * to be disabled and then a retry of this
5011 				 * read is required.
5012 				 */
5013 				failpos = 0;
5014 				error = 0;
5015 				trycnt++;
5016 			}
5017 		} else if (ioproc == NFSPROC_WRITEDS)
5018 			error = nfsrv_writedsrpc(fh, off, cnt, cred, p, vp,
5019 			    &nmp[0], mirrorcnt, mpp, cp, &failpos);
5020 		else if (ioproc == NFSPROC_SETATTR)
5021 			error = nfsrv_setattrdsrpc(fh, cred, p, vp, &nmp[0],
5022 			    mirrorcnt, nap, &failpos);
5023 		else if (ioproc == NFSPROC_SETACL)
5024 			error = nfsrv_setacldsrpc(fh, cred, p, vp, &nmp[0],
5025 			    mirrorcnt, aclp, &failpos);
5026 		else if (ioproc == NFSPROC_SEEKDS) {
5027 			error = nfsrv_seekdsrpc(fh, offp, content, eofp, cred,
5028 			    p, nmp[0]);
5029 			if (nfsds_failerr(error) && mirrorcnt > 1) {
5030 				/*
5031 				 * Setting failpos will cause the mirror
5032 				 * to be disabled and then a retry of this
5033 				 * read is required.
5034 				 */
5035 				failpos = 0;
5036 				error = 0;
5037 				trycnt++;
5038 			}
5039 		} else if (ioproc == NFSPROC_ALLOCATE)
5040 			error = nfsrv_allocatedsrpc(fh, off, *offp, cred, p, vp,
5041 			    &nmp[0], mirrorcnt, &failpos);
5042 		else if (ioproc == NFSPROC_DEALLOCATE)
5043 			error = nfsrv_deallocatedsrpc(fh, off, *offp, cred, p,
5044 			    vp, &nmp[0], mirrorcnt, &failpos);
5045 		else {
5046 			error = nfsrv_getattrdsrpc(&fh[mirrorcnt - 1], cred, p,
5047 			    vp, nmp[mirrorcnt - 1], nap);
5048 			if (nfsds_failerr(error) && mirrorcnt > 1) {
5049 				/*
5050 				 * Setting failpos will cause the mirror
5051 				 * to be disabled and then a retry of this
5052 				 * getattr is required.
5053 				 */
5054 				failpos = mirrorcnt - 1;
5055 				error = 0;
5056 				trycnt++;
5057 			}
5058 		}
5059 		ds = NULL;
5060 		if (failpos >= 0) {
5061 			failnmp = nmp[failpos];
5062 			NFSLOCKMNT(failnmp);
5063 			if ((failnmp->nm_privflag & (NFSMNTP_FORCEDISM |
5064 			     NFSMNTP_CANCELRPCS)) == 0) {
5065 				failnmp->nm_privflag |= NFSMNTP_CANCELRPCS;
5066 				NFSUNLOCKMNT(failnmp);
5067 				ds = nfsrv_deldsnmp(PNFSDOP_DELDSSERVER,
5068 				    failnmp, p);
5069 				NFSD_DEBUG(4, "dsldsnmp fail=%d ds=%p\n",
5070 				    failpos, ds);
5071 				if (ds != NULL)
5072 					nfsrv_killrpcs(failnmp);
5073 				NFSLOCKMNT(failnmp);
5074 				failnmp->nm_privflag &= ~NFSMNTP_CANCELRPCS;
5075 				wakeup(failnmp);
5076 			}
5077 			NFSUNLOCKMNT(failnmp);
5078 		}
5079 		for (i = 0; i < mirrorcnt; i++)
5080 			NFSVOPUNLOCK(dvp[i]);
5081 		NFSD_DEBUG(4, "nfsrv_proxyds: aft RPC=%d trya=%d\n", error,
5082 		    trycnt);
5083 		/* Try the Read/Getattr again if a mirror was deleted. */
5084 		if (ds != NULL && trycnt > 0 && trycnt < origmircnt)
5085 			goto tryagain;
5086 	} else {
5087 		/* Return ENOENT for any Extended Attribute error. */
5088 		error = ENOENT;
5089 	}
5090 	free(buf, M_TEMP);
5091 	NFSD_DEBUG(4, "nfsrv_proxyds: error=%d\n", error);
5092 	return (error);
5093 }
5094 
5095 /*
5096  * Get the DS mount point, fh and directory from the "pnfsd.dsfile" extended
5097  * attribute.
5098  * newnmpp - If it points to a non-NULL nmp, that is the destination and needs
5099  *           to be checked.  If it points to a NULL nmp, then it returns
5100  *           a suitable destination.
5101  * curnmp - If non-NULL, it is the source mount for the copy.
5102  */
5103 int
nfsrv_dsgetsockmnt(struct vnode * vp,int lktype,char * buf,int * buflenp,int * mirrorcntp,NFSPROC_T * p,struct vnode ** dvpp,fhandle_t * fhp,char * devid,char * fnamep,struct vnode ** nvpp,struct nfsmount ** newnmpp,struct nfsmount * curnmp,int * ippos,int * dsdirp)5104 nfsrv_dsgetsockmnt(struct vnode *vp, int lktype, char *buf, int *buflenp,
5105     int *mirrorcntp, NFSPROC_T *p, struct vnode **dvpp, fhandle_t *fhp,
5106     char *devid, char *fnamep, struct vnode **nvpp, struct nfsmount **newnmpp,
5107     struct nfsmount *curnmp, int *ippos, int *dsdirp)
5108 {
5109 	struct vnode *dvp, *nvp = NULL, **tdvpp;
5110 	struct mount *mp;
5111 	struct nfsmount *nmp, *newnmp;
5112 	struct sockaddr *sad;
5113 	struct sockaddr_in *sin;
5114 	struct nfsdevice *ds, *tds, *fndds;
5115 	struct pnfsdsfile *pf;
5116 	uint32_t dsdir;
5117 	int error, fhiszero, fnd, gotone, i, mirrorcnt;
5118 
5119 	ASSERT_VOP_LOCKED(vp, "nfsrv_dsgetsockmnt vp");
5120 	*mirrorcntp = 1;
5121 	tdvpp = dvpp;
5122 	if (nvpp != NULL)
5123 		*nvpp = NULL;
5124 	if (dvpp != NULL)
5125 		*dvpp = NULL;
5126 	if (ippos != NULL)
5127 		*ippos = -1;
5128 	if (newnmpp != NULL)
5129 		newnmp = *newnmpp;
5130 	else
5131 		newnmp = NULL;
5132 	mp = vp->v_mount;
5133 	error = vn_extattr_get(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5134 	    "pnfsd.dsfile", buflenp, buf, p);
5135 	mirrorcnt = *buflenp / sizeof(*pf);
5136 	if (error == 0 && (mirrorcnt < 1 || mirrorcnt > NFSDEV_MAXMIRRORS ||
5137 	    *buflenp != sizeof(*pf) * mirrorcnt))
5138 		error = ENOATTR;
5139 
5140 	pf = (struct pnfsdsfile *)buf;
5141 	/* If curnmp != NULL, check for a match in the mirror list. */
5142 	if (curnmp != NULL && error == 0) {
5143 		fnd = 0;
5144 		for (i = 0; i < mirrorcnt; i++, pf++) {
5145 			sad = (struct sockaddr *)&pf->dsf_sin;
5146 			if (nfsaddr2_match(sad, curnmp->nm_nam)) {
5147 				if (ippos != NULL)
5148 					*ippos = i;
5149 				fnd = 1;
5150 				break;
5151 			}
5152 		}
5153 		if (fnd == 0)
5154 			error = ENXIO;
5155 	}
5156 
5157 	gotone = 0;
5158 	pf = (struct pnfsdsfile *)buf;
5159 	NFSD_DEBUG(4, "nfsrv_dsgetsockmnt: mirrorcnt=%d err=%d\n", mirrorcnt,
5160 	    error);
5161 	for (i = 0; i < mirrorcnt && error == 0; i++, pf++) {
5162 		fhiszero = 0;
5163 		sad = (struct sockaddr *)&pf->dsf_sin;
5164 		sin = &pf->dsf_sin;
5165 		dsdir = pf->dsf_dir;
5166 		if (dsdir >= nfsrv_dsdirsize) {
5167 			printf("nfsrv_dsgetsockmnt: dsdir=%d\n", dsdir);
5168 			error = ENOATTR;
5169 		} else if (nvpp != NULL && newnmp != NULL &&
5170 		    nfsaddr2_match(sad, newnmp->nm_nam))
5171 			error = EEXIST;
5172 		if (error == 0) {
5173 			if (ippos != NULL && curnmp == NULL &&
5174 			    sad->sa_family == AF_INET &&
5175 			    sin->sin_addr.s_addr == 0)
5176 				*ippos = i;
5177 			if (NFSBCMP(&zerofh, &pf->dsf_fh, sizeof(zerofh)) == 0)
5178 				fhiszero = 1;
5179 			/* Use the socket address to find the mount point. */
5180 			fndds = NULL;
5181 			NFSDDSLOCK();
5182 			/* Find a match for the IP address. */
5183 			TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
5184 				if (ds->nfsdev_nmp != NULL) {
5185 					dvp = ds->nfsdev_dvp;
5186 					nmp = VFSTONFS(dvp->v_mount);
5187 					if (nmp != ds->nfsdev_nmp)
5188 						printf("different2 nmp %p %p\n",
5189 						    nmp, ds->nfsdev_nmp);
5190 					if (nfsaddr2_match(sad, nmp->nm_nam)) {
5191 						fndds = ds;
5192 						break;
5193 					}
5194 				}
5195 			}
5196 			if (fndds != NULL && newnmpp != NULL &&
5197 			    newnmp == NULL) {
5198 				/* Search for a place to make a mirror copy. */
5199 				TAILQ_FOREACH(tds, &nfsrv_devidhead,
5200 				    nfsdev_list) {
5201 					if (tds->nfsdev_nmp != NULL &&
5202 					    fndds != tds &&
5203 					    ((tds->nfsdev_mdsisset == 0 &&
5204 					      fndds->nfsdev_mdsisset == 0) ||
5205 					     (tds->nfsdev_mdsisset != 0 &&
5206 					      fndds->nfsdev_mdsisset != 0 &&
5207 					      fsidcmp(&tds->nfsdev_mdsfsid,
5208 					      &mp->mnt_stat.f_fsid) == 0))) {
5209 						*newnmpp = tds->nfsdev_nmp;
5210 						break;
5211 					}
5212 				}
5213 				if (tds != NULL) {
5214 					/*
5215 					 * Move this entry to the end of the
5216 					 * list, so it won't be selected as
5217 					 * easily the next time.
5218 					 */
5219 					TAILQ_REMOVE(&nfsrv_devidhead, tds,
5220 					    nfsdev_list);
5221 					TAILQ_INSERT_TAIL(&nfsrv_devidhead, tds,
5222 					    nfsdev_list);
5223 				}
5224 			}
5225 			NFSDDSUNLOCK();
5226 			if (fndds != NULL) {
5227 				dvp = fndds->nfsdev_dsdir[dsdir];
5228 				if (lktype != 0 || fhiszero != 0 ||
5229 				    (nvpp != NULL && *nvpp == NULL)) {
5230 					if (fhiszero != 0)
5231 						error = vn_lock(dvp,
5232 						    LK_EXCLUSIVE);
5233 					else if (lktype != 0)
5234 						error = vn_lock(dvp, lktype);
5235 					else
5236 						error = vn_lock(dvp, LK_SHARED);
5237 					/*
5238 					 * If the file handle is all 0's, try to
5239 					 * do a Lookup against the DS to acquire
5240 					 * it.
5241 					 * If dvpp == NULL or the Lookup fails,
5242 					 * unlock dvp after the call.
5243 					 */
5244 					if (error == 0 && (fhiszero != 0 ||
5245 					    (nvpp != NULL && *nvpp == NULL))) {
5246 						error = nfsrv_pnfslookupds(vp,
5247 						    dvp, pf, &nvp, p);
5248 						if (error == 0) {
5249 							if (fhiszero != 0)
5250 								nfsrv_pnfssetfh(
5251 								    vp, pf,
5252 								    devid,
5253 								    fnamep,
5254 								    nvp, p);
5255 							if (nvpp != NULL &&
5256 							    *nvpp == NULL) {
5257 								*nvpp = nvp;
5258 								*dsdirp = dsdir;
5259 							} else
5260 								vput(nvp);
5261 						}
5262 						if (error != 0 || lktype == 0)
5263 							NFSVOPUNLOCK(dvp);
5264 					}
5265 				}
5266 				if (error == 0) {
5267 					gotone++;
5268 					NFSD_DEBUG(4, "gotone=%d\n", gotone);
5269 					if (devid != NULL) {
5270 						NFSBCOPY(fndds->nfsdev_deviceid,
5271 						    devid, NFSX_V4DEVICEID);
5272 						devid += NFSX_V4DEVICEID;
5273 					}
5274 					if (dvpp != NULL)
5275 						*tdvpp++ = dvp;
5276 					if (fhp != NULL)
5277 						NFSBCOPY(&pf->dsf_fh, fhp++,
5278 						    NFSX_MYFH);
5279 					if (fnamep != NULL && gotone == 1)
5280 						strlcpy(fnamep,
5281 						    pf->dsf_filename,
5282 						    sizeof(pf->dsf_filename));
5283 				} else
5284 					NFSD_DEBUG(4, "nfsrv_dsgetsockmnt "
5285 					    "err=%d\n", error);
5286 			}
5287 		}
5288 	}
5289 	if (error == 0 && gotone == 0)
5290 		error = ENOENT;
5291 
5292 	NFSD_DEBUG(4, "eo nfsrv_dsgetsockmnt: gotone=%d err=%d\n", gotone,
5293 	    error);
5294 	if (error == 0)
5295 		*mirrorcntp = gotone;
5296 	else {
5297 		if (gotone > 0 && dvpp != NULL) {
5298 			/*
5299 			 * If the error didn't occur on the first one and
5300 			 * dvpp != NULL, the one(s) prior to the failure will
5301 			 * have locked dvp's that need to be unlocked.
5302 			 */
5303 			for (i = 0; i < gotone; i++) {
5304 				NFSVOPUNLOCK(*dvpp);
5305 				*dvpp++ = NULL;
5306 			}
5307 		}
5308 		/*
5309 		 * If it found the vnode to be copied from before a failure,
5310 		 * it needs to be vput()'d.
5311 		 */
5312 		if (nvpp != NULL && *nvpp != NULL) {
5313 			vput(*nvpp);
5314 			*nvpp = NULL;
5315 		}
5316 	}
5317 	return (error);
5318 }
5319 
5320 /*
5321  * Set the extended attribute for the Change attribute.
5322  */
5323 static int
nfsrv_setextattr(struct vnode * vp,struct nfsvattr * nap,NFSPROC_T * p)5324 nfsrv_setextattr(struct vnode *vp, struct nfsvattr *nap, NFSPROC_T *p)
5325 {
5326 	struct pnfsdsattr dsattr;
5327 	int error;
5328 
5329 	ASSERT_VOP_ELOCKED(vp, "nfsrv_setextattr vp");
5330 	dsattr.dsa_filerev = nap->na_filerev;
5331 	dsattr.dsa_size = nap->na_size;
5332 	dsattr.dsa_atime = nap->na_atime;
5333 	dsattr.dsa_mtime = nap->na_mtime;
5334 	dsattr.dsa_bytes = nap->na_bytes;
5335 	error = vn_extattr_set(vp, IO_NODELOCKED, EXTATTR_NAMESPACE_SYSTEM,
5336 	    "pnfsd.dsattr", sizeof(dsattr), (char *)&dsattr, p);
5337 	if (error != 0)
5338 		printf("pNFS: setextattr=%d\n", error);
5339 	return (error);
5340 }
5341 
5342 static int
nfsrv_readdsrpc(fhandle_t * fhp,off_t off,int len,struct ucred * cred,NFSPROC_T * p,struct nfsmount * nmp,struct mbuf ** mpp,struct mbuf ** mpendp)5343 nfsrv_readdsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5344     NFSPROC_T *p, struct nfsmount *nmp, struct mbuf **mpp, struct mbuf **mpendp)
5345 {
5346 	uint32_t *tl;
5347 	struct nfsrv_descript *nd;
5348 	nfsv4stateid_t st;
5349 	struct mbuf *m, *m2;
5350 	int error = 0, retlen, tlen, trimlen;
5351 
5352 	NFSD_DEBUG(4, "in nfsrv_readdsrpc\n");
5353 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5354 	*mpp = NULL;
5355 	/*
5356 	 * Use a stateid where other is an alternating 01010 pattern and
5357 	 * seqid is 0xffffffff.  This value is not defined as special by
5358 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5359 	 * MDS->DS proxy operation.
5360 	 */
5361 	st.other[0] = 0x55555555;
5362 	st.other[1] = 0x55555555;
5363 	st.other[2] = 0x55555555;
5364 	st.seqid = 0xffffffff;
5365 	nfscl_reqstart(nd, NFSPROC_READDS, nmp, (u_int8_t *)fhp, sizeof(*fhp),
5366 	    NULL, NULL, 0, 0, cred);
5367 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5368 	NFSM_BUILD(tl, uint32_t *, NFSX_UNSIGNED * 3);
5369 	txdr_hyper(off, tl);
5370 	*(tl + 2) = txdr_unsigned(len);
5371 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
5372 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5373 	if (error != 0) {
5374 		free(nd, M_TEMP);
5375 		return (error);
5376 	}
5377 	if (nd->nd_repstat == 0) {
5378 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
5379 		NFSM_STRSIZ(retlen, len);
5380 		if (retlen > 0) {
5381 			/* Trim off the pre-data XDR from the mbuf chain. */
5382 			m = nd->nd_mrep;
5383 			while (m != NULL && m != nd->nd_md) {
5384 				if (m->m_next == nd->nd_md) {
5385 					m->m_next = NULL;
5386 					m_freem(nd->nd_mrep);
5387 					nd->nd_mrep = m = nd->nd_md;
5388 				} else
5389 					m = m->m_next;
5390 			}
5391 			if (m == NULL) {
5392 				printf("nfsrv_readdsrpc: busted mbuf list\n");
5393 				error = ENOENT;
5394 				goto nfsmout;
5395 			}
5396 
5397 			/*
5398 			 * Now, adjust first mbuf so that any XDR before the
5399 			 * read data is skipped over.
5400 			 */
5401 			trimlen = nd->nd_dpos - mtod(m, char *);
5402 			if (trimlen > 0) {
5403 				m->m_len -= trimlen;
5404 				NFSM_DATAP(m, trimlen);
5405 			}
5406 
5407 			/*
5408 			 * Truncate the mbuf chain at retlen bytes of data,
5409 			 * plus XDR padding that brings the length up to a
5410 			 * multiple of 4.
5411 			 */
5412 			tlen = NFSM_RNDUP(retlen);
5413 			do {
5414 				if (m->m_len >= tlen) {
5415 					m->m_len = tlen;
5416 					tlen = 0;
5417 					m2 = m->m_next;
5418 					m->m_next = NULL;
5419 					m_freem(m2);
5420 					break;
5421 				}
5422 				tlen -= m->m_len;
5423 				m = m->m_next;
5424 			} while (m != NULL);
5425 			if (tlen > 0) {
5426 				printf("nfsrv_readdsrpc: busted mbuf list\n");
5427 				error = ENOENT;
5428 				goto nfsmout;
5429 			}
5430 			*mpp = nd->nd_mrep;
5431 			*mpendp = m;
5432 			nd->nd_mrep = NULL;
5433 		}
5434 	} else
5435 		error = nd->nd_repstat;
5436 nfsmout:
5437 	/* If nd->nd_mrep is already NULL, this is a no-op. */
5438 	m_freem(nd->nd_mrep);
5439 	free(nd, M_TEMP);
5440 	NFSD_DEBUG(4, "nfsrv_readdsrpc error=%d\n", error);
5441 	return (error);
5442 }
5443 
5444 /*
5445  * Do a write RPC on a DS data file, using this structure for the arguments,
5446  * so that this function can be executed by a separate kernel process.
5447  */
5448 struct nfsrvwritedsdorpc {
5449 	int			done;
5450 	int			inprog;
5451 	struct task		tsk;
5452 	fhandle_t		fh;
5453 	off_t			off;
5454 	int			len;
5455 	struct nfsmount		*nmp;
5456 	struct ucred		*cred;
5457 	NFSPROC_T		*p;
5458 	struct mbuf		*m;
5459 	int			err;
5460 };
5461 
5462 static int
nfsrv_writedsdorpc(struct nfsmount * nmp,fhandle_t * fhp,off_t off,int len,struct nfsvattr * nap,struct mbuf * m,struct ucred * cred,NFSPROC_T * p)5463 nfsrv_writedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off, int len,
5464     struct nfsvattr *nap, struct mbuf *m, struct ucred *cred, NFSPROC_T *p)
5465 {
5466 	uint32_t *tl;
5467 	struct nfsrv_descript *nd;
5468 	nfsattrbit_t attrbits;
5469 	nfsv4stateid_t st;
5470 	int commit, error, retlen;
5471 
5472 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5473 	nfscl_reqstart(nd, NFSPROC_WRITE, nmp, (u_int8_t *)fhp,
5474 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5475 
5476 	/*
5477 	 * Use a stateid where other is an alternating 01010 pattern and
5478 	 * seqid is 0xffffffff.  This value is not defined as special by
5479 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5480 	 * MDS->DS proxy operation.
5481 	 */
5482 	st.other[0] = 0x55555555;
5483 	st.other[1] = 0x55555555;
5484 	st.other[2] = 0x55555555;
5485 	st.seqid = 0xffffffff;
5486 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5487 	NFSM_BUILD(tl, u_int32_t *, NFSX_HYPER + 2 * NFSX_UNSIGNED);
5488 	txdr_hyper(off, tl);
5489 	tl += 2;
5490 	/*
5491 	 * Do all writes FileSync, since the server doesn't hold onto dirty
5492 	 * buffers.  Since clients should be accessing the DS servers directly
5493 	 * using the pNFS layouts, this just needs to work correctly as a
5494 	 * fallback.
5495 	 */
5496 	*tl++ = txdr_unsigned(NFSWRITE_FILESYNC);
5497 	*tl = txdr_unsigned(len);
5498 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: len=%d\n", len);
5499 
5500 	/* Put data in mbuf chain. */
5501 	nd->nd_mb->m_next = m;
5502 
5503 	/* Set nd_mb and nd_bpos to end of data. */
5504 	while (m->m_next != NULL)
5505 		m = m->m_next;
5506 	nd->nd_mb = m;
5507 	nfsm_set(nd, m->m_len);
5508 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: lastmb len=%d\n", m->m_len);
5509 
5510 	/* Do a Getattr for the attributes that change upon writing. */
5511 	NFSZERO_ATTRBIT(&attrbits);
5512 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5513 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5514 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5515 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5516 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5517 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
5518 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5519 	(void) nfsrv_putattrbit(nd, &attrbits);
5520 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5521 	    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5522 	if (error != 0) {
5523 		free(nd, M_TEMP);
5524 		return (error);
5525 	}
5526 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft writerpc=%d\n", nd->nd_repstat);
5527 	/* Get rid of weak cache consistency data for now. */
5528 	if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5529 	    (ND_NFSV4 | ND_V4WCCATTR)) {
5530 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5531 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5532 		NFSD_DEBUG(4, "nfsrv_writedsdorpc: wcc attr=%d\n", error);
5533 		if (error != 0)
5534 			goto nfsmout;
5535 		/*
5536 		 * Get rid of Op# and status for next op.
5537 		 */
5538 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5539 		if (*++tl != 0)
5540 			nd->nd_flag |= ND_NOMOREDATA;
5541 	}
5542 	if (nd->nd_repstat == 0) {
5543 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED + NFSX_VERF);
5544 		retlen = fxdr_unsigned(int, *tl++);
5545 		commit = fxdr_unsigned(int, *tl);
5546 		if (commit != NFSWRITE_FILESYNC)
5547 			error = NFSERR_IO;
5548 		NFSD_DEBUG(4, "nfsrv_writedsdorpc:retlen=%d commit=%d err=%d\n",
5549 		    retlen, commit, error);
5550 	} else
5551 		error = nd->nd_repstat;
5552 	/* We have no use for the Write Verifier since we use FileSync. */
5553 
5554 	/*
5555 	 * Get the Change, Size, Access Time and Modify Time attributes and set
5556 	 * on the Metadata file, so its attributes will be what the file's
5557 	 * would be if it had been written.
5558 	 */
5559 	if (error == 0) {
5560 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5561 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5562 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5563 	}
5564 	NFSD_DEBUG(4, "nfsrv_writedsdorpc: aft loadattr=%d\n", error);
5565 nfsmout:
5566 	m_freem(nd->nd_mrep);
5567 	free(nd, M_TEMP);
5568 	NFSD_DEBUG(4, "nfsrv_writedsdorpc error=%d\n", error);
5569 	return (error);
5570 }
5571 
5572 /*
5573  * Start up the thread that will execute nfsrv_writedsdorpc().
5574  */
5575 static void
start_writedsdorpc(void * arg,int pending)5576 start_writedsdorpc(void *arg, int pending)
5577 {
5578 	struct nfsrvwritedsdorpc *drpc;
5579 
5580 	drpc = (struct nfsrvwritedsdorpc *)arg;
5581 	drpc->err = nfsrv_writedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5582 	    drpc->len, NULL, drpc->m, drpc->cred, drpc->p);
5583 	drpc->done = 1;
5584 	NFSD_DEBUG(4, "start_writedsdorpc: err=%d\n", drpc->err);
5585 }
5586 
5587 static int
nfsrv_writedsrpc(fhandle_t * fhp,off_t off,int len,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,struct mbuf ** mpp,char * cp,int * failposp)5588 nfsrv_writedsrpc(fhandle_t *fhp, off_t off, int len, struct ucred *cred,
5589     NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5590     struct mbuf **mpp, char *cp, int *failposp)
5591 {
5592 	struct nfsrvwritedsdorpc *drpc, *tdrpc = NULL;
5593 	struct nfsvattr na;
5594 	struct mbuf *m;
5595 	int error, i, offs, ret, timo;
5596 
5597 	NFSD_DEBUG(4, "in nfsrv_writedsrpc\n");
5598 	KASSERT(*mpp != NULL, ("nfsrv_writedsrpc: NULL mbuf chain"));
5599 	drpc = NULL;
5600 	if (mirrorcnt > 1)
5601 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5602 		    M_WAITOK);
5603 
5604 	/* Calculate offset in mbuf chain that data starts. */
5605 	offs = cp - mtod(*mpp, char *);
5606 	NFSD_DEBUG(4, "nfsrv_writedsrpc: mcopy offs=%d len=%d\n", offs, len);
5607 
5608 	/*
5609 	 * Do the write RPC for every DS, using a separate kernel process
5610 	 * for every DS except the last one.
5611 	 */
5612 	error = 0;
5613 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5614 		tdrpc->done = 0;
5615 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5616 		tdrpc->off = off;
5617 		tdrpc->len = len;
5618 		tdrpc->nmp = *nmpp;
5619 		tdrpc->cred = cred;
5620 		tdrpc->p = p;
5621 		tdrpc->inprog = 0;
5622 		tdrpc->err = 0;
5623 		tdrpc->m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5624 		ret = EIO;
5625 		if (nfs_pnfsiothreads != 0) {
5626 			ret = nfs_pnfsio(start_writedsdorpc, tdrpc);
5627 			NFSD_DEBUG(4, "nfsrv_writedsrpc: nfs_pnfsio=%d\n",
5628 			    ret);
5629 		}
5630 		if (ret != 0) {
5631 			ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, NULL,
5632 			    tdrpc->m, cred, p);
5633 			if (nfsds_failerr(ret) && *failposp == -1)
5634 				*failposp = i;
5635 			else if (error == 0 && ret != 0)
5636 				error = ret;
5637 		}
5638 		nmpp++;
5639 		fhp++;
5640 	}
5641 	m = m_copym(*mpp, offs, NFSM_RNDUP(len), M_WAITOK);
5642 	ret = nfsrv_writedsdorpc(*nmpp, fhp, off, len, &na, m, cred, p);
5643 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5644 		*failposp = mirrorcnt - 1;
5645 	else if (error == 0 && ret != 0)
5646 		error = ret;
5647 	if (error == 0)
5648 		error = nfsrv_setextattr(vp, &na, p);
5649 	NFSD_DEBUG(4, "nfsrv_writedsrpc: aft setextat=%d\n", error);
5650 	tdrpc = drpc;
5651 	timo = hz / 50;		/* Wait for 20msec. */
5652 	if (timo < 1)
5653 		timo = 1;
5654 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5655 		/* Wait for RPCs on separate threads to complete. */
5656 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
5657 			tsleep(&tdrpc->tsk, PVFS, "srvwrds", timo);
5658 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5659 			*failposp = i;
5660 		else if (error == 0 && tdrpc->err != 0)
5661 			error = tdrpc->err;
5662 	}
5663 	free(drpc, M_TEMP);
5664 	return (error);
5665 }
5666 
5667 /*
5668  * Do a allocate RPC on a DS data file, using this structure for the arguments,
5669  * so that this function can be executed by a separate kernel process.
5670  */
5671 struct nfsrvallocatedsdorpc {
5672 	int			done;
5673 	int			inprog;
5674 	struct task		tsk;
5675 	fhandle_t		fh;
5676 	off_t			off;
5677 	off_t			len;
5678 	struct nfsmount		*nmp;
5679 	struct ucred		*cred;
5680 	NFSPROC_T		*p;
5681 	int			err;
5682 };
5683 
5684 static int
nfsrv_allocatedsdorpc(struct nfsmount * nmp,fhandle_t * fhp,off_t off,off_t len,struct nfsvattr * nap,struct ucred * cred,NFSPROC_T * p)5685 nfsrv_allocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5686     off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5687 {
5688 	uint32_t *tl;
5689 	struct nfsrv_descript *nd;
5690 	nfsattrbit_t attrbits;
5691 	nfsv4stateid_t st;
5692 	int error;
5693 
5694 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5695 	nfscl_reqstart(nd, NFSPROC_ALLOCATE, nmp, (u_int8_t *)fhp,
5696 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5697 
5698 	/*
5699 	 * Use a stateid where other is an alternating 01010 pattern and
5700 	 * seqid is 0xffffffff.  This value is not defined as special by
5701 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5702 	 * MDS->DS proxy operation.
5703 	 */
5704 	st.other[0] = 0x55555555;
5705 	st.other[1] = 0x55555555;
5706 	st.other[2] = 0x55555555;
5707 	st.seqid = 0xffffffff;
5708 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5709 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5710 	txdr_hyper(off, tl); tl += 2;
5711 	txdr_hyper(len, tl); tl += 2;
5712 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: len=%jd\n", (intmax_t)len);
5713 
5714 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5715 	NFSGETATTR_ATTRBIT(&attrbits);
5716 	nfsrv_putattrbit(nd, &attrbits);
5717 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5718 	    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5719 	if (error != 0) {
5720 		free(nd, M_TEMP);
5721 		return (error);
5722 	}
5723 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft allocaterpc=%d\n",
5724 	    nd->nd_repstat);
5725 	if (nd->nd_repstat == 0) {
5726 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5727 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5728 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5729 	} else
5730 		error = nd->nd_repstat;
5731 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc: aft loadattr=%d\n", error);
5732 nfsmout:
5733 	m_freem(nd->nd_mrep);
5734 	free(nd, M_TEMP);
5735 	NFSD_DEBUG(4, "nfsrv_allocatedsdorpc error=%d\n", error);
5736 	return (error);
5737 }
5738 
5739 /*
5740  * Start up the thread that will execute nfsrv_allocatedsdorpc().
5741  */
5742 static void
start_allocatedsdorpc(void * arg,int pending)5743 start_allocatedsdorpc(void *arg, int pending)
5744 {
5745 	struct nfsrvallocatedsdorpc *drpc;
5746 
5747 	drpc = (struct nfsrvallocatedsdorpc *)arg;
5748 	drpc->err = nfsrv_allocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5749 	    drpc->len, NULL, drpc->cred, drpc->p);
5750 	drpc->done = 1;
5751 	NFSD_DEBUG(4, "start_allocatedsdorpc: err=%d\n", drpc->err);
5752 }
5753 
5754 static int
nfsrv_allocatedsrpc(fhandle_t * fhp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,int * failposp)5755 nfsrv_allocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5756     NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5757     int *failposp)
5758 {
5759 	struct nfsrvallocatedsdorpc *drpc, *tdrpc = NULL;
5760 	struct nfsvattr na;
5761 	int error, i, ret, timo;
5762 
5763 	NFSD_DEBUG(4, "in nfsrv_allocatedsrpc\n");
5764 	drpc = NULL;
5765 	if (mirrorcnt > 1)
5766 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5767 		    M_WAITOK);
5768 
5769 	/*
5770 	 * Do the allocate RPC for every DS, using a separate kernel process
5771 	 * for every DS except the last one.
5772 	 */
5773 	error = 0;
5774 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5775 		tdrpc->done = 0;
5776 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5777 		tdrpc->off = off;
5778 		tdrpc->len = len;
5779 		tdrpc->nmp = *nmpp;
5780 		tdrpc->cred = cred;
5781 		tdrpc->p = p;
5782 		tdrpc->inprog = 0;
5783 		tdrpc->err = 0;
5784 		ret = EIO;
5785 		if (nfs_pnfsiothreads != 0) {
5786 			ret = nfs_pnfsio(start_allocatedsdorpc, tdrpc);
5787 			NFSD_DEBUG(4, "nfsrv_allocatedsrpc: nfs_pnfsio=%d\n",
5788 			    ret);
5789 		}
5790 		if (ret != 0) {
5791 			ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, NULL,
5792 			    cred, p);
5793 			if (nfsds_failerr(ret) && *failposp == -1)
5794 				*failposp = i;
5795 			else if (error == 0 && ret != 0)
5796 				error = ret;
5797 		}
5798 		nmpp++;
5799 		fhp++;
5800 	}
5801 	ret = nfsrv_allocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5802 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5803 		*failposp = mirrorcnt - 1;
5804 	else if (error == 0 && ret != 0)
5805 		error = ret;
5806 	if (error == 0)
5807 		error = nfsrv_setextattr(vp, &na, p);
5808 	NFSD_DEBUG(4, "nfsrv_allocatedsrpc: aft setextat=%d\n", error);
5809 	tdrpc = drpc;
5810 	timo = hz / 50;		/* Wait for 20msec. */
5811 	if (timo < 1)
5812 		timo = 1;
5813 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5814 		/* Wait for RPCs on separate threads to complete. */
5815 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
5816 			tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5817 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5818 			*failposp = i;
5819 		else if (error == 0 && tdrpc->err != 0)
5820 			error = tdrpc->err;
5821 	}
5822 	free(drpc, M_TEMP);
5823 	return (error);
5824 }
5825 
5826 /*
5827  * Do a deallocate RPC on a DS data file, using this structure for the
5828  * arguments, so that this function can be executed by a separate kernel
5829  * process.
5830  */
5831 struct nfsrvdeallocatedsdorpc {
5832 	int			done;
5833 	int			inprog;
5834 	struct task		tsk;
5835 	fhandle_t		fh;
5836 	off_t			off;
5837 	off_t			len;
5838 	struct nfsmount		*nmp;
5839 	struct ucred		*cred;
5840 	NFSPROC_T		*p;
5841 	int			err;
5842 };
5843 
5844 static int
nfsrv_deallocatedsdorpc(struct nfsmount * nmp,fhandle_t * fhp,off_t off,off_t len,struct nfsvattr * nap,struct ucred * cred,NFSPROC_T * p)5845 nfsrv_deallocatedsdorpc(struct nfsmount *nmp, fhandle_t *fhp, off_t off,
5846     off_t len, struct nfsvattr *nap, struct ucred *cred, NFSPROC_T *p)
5847 {
5848 	uint32_t *tl;
5849 	struct nfsrv_descript *nd;
5850 	nfsattrbit_t attrbits;
5851 	nfsv4stateid_t st;
5852 	int error;
5853 
5854 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
5855 	nfscl_reqstart(nd, NFSPROC_DEALLOCATE, nmp, (u_int8_t *)fhp,
5856 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
5857 
5858 	/*
5859 	 * Use a stateid where other is an alternating 01010 pattern and
5860 	 * seqid is 0xffffffff.  This value is not defined as special by
5861 	 * the RFC and is used by the FreeBSD NFS server to indicate an
5862 	 * MDS->DS proxy operation.
5863 	 */
5864 	st.other[0] = 0x55555555;
5865 	st.other[1] = 0x55555555;
5866 	st.other[2] = 0x55555555;
5867 	st.seqid = 0xffffffff;
5868 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
5869 	NFSM_BUILD(tl, uint32_t *, 2 * NFSX_HYPER + NFSX_UNSIGNED);
5870 	txdr_hyper(off, tl); tl += 2;
5871 	txdr_hyper(len, tl); tl += 2;
5872 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: len=%jd\n", (intmax_t)len);
5873 
5874 	/* Do a Getattr for the attributes that change upon writing. */
5875 	NFSZERO_ATTRBIT(&attrbits);
5876 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
5877 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
5878 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
5879 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
5880 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
5881 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
5882 	nfsrv_putattrbit(nd, &attrbits);
5883 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p,
5884 	    cred, NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
5885 	if (error != 0) {
5886 		free(nd, M_TEMP);
5887 		return (error);
5888 	}
5889 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft deallocaterpc=%d\n",
5890 	    nd->nd_repstat);
5891 	/* Get rid of weak cache consistency data for now. */
5892 	if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
5893 	    (ND_NFSV4 | ND_V4WCCATTR)) {
5894 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5895 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5896 		NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: wcc attr=%d\n", error);
5897 		if (error != 0)
5898 			goto nfsmout;
5899 		/*
5900 		 * Get rid of Op# and status for next op.
5901 		 */
5902 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5903 		if (*++tl != 0)
5904 			nd->nd_flag |= ND_NOMOREDATA;
5905 	}
5906 	if (nd->nd_repstat == 0) {
5907 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
5908 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL, NULL,
5909 		    NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
5910 	} else
5911 		error = nd->nd_repstat;
5912 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc: aft loadattr=%d\n", error);
5913 nfsmout:
5914 	m_freem(nd->nd_mrep);
5915 	free(nd, M_TEMP);
5916 	NFSD_DEBUG(4, "nfsrv_deallocatedsdorpc error=%d\n", error);
5917 	return (error);
5918 }
5919 
5920 /*
5921  * Start up the thread that will execute nfsrv_deallocatedsdorpc().
5922  */
5923 static void
start_deallocatedsdorpc(void * arg,int pending)5924 start_deallocatedsdorpc(void *arg, int pending)
5925 {
5926 	struct nfsrvdeallocatedsdorpc *drpc;
5927 
5928 	drpc = (struct nfsrvdeallocatedsdorpc *)arg;
5929 	drpc->err = nfsrv_deallocatedsdorpc(drpc->nmp, &drpc->fh, drpc->off,
5930 	    drpc->len, NULL, drpc->cred, drpc->p);
5931 	drpc->done = 1;
5932 	NFSD_DEBUG(4, "start_deallocatedsdorpc: err=%d\n", drpc->err);
5933 }
5934 
5935 static int
nfsrv_deallocatedsrpc(fhandle_t * fhp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,int * failposp)5936 nfsrv_deallocatedsrpc(fhandle_t *fhp, off_t off, off_t len, struct ucred *cred,
5937     NFSPROC_T *p, struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
5938     int *failposp)
5939 {
5940 	struct nfsrvdeallocatedsdorpc *drpc, *tdrpc = NULL;
5941 	struct nfsvattr na;
5942 	int error, i, ret, timo;
5943 
5944 	NFSD_DEBUG(4, "in nfsrv_deallocatedsrpc\n");
5945 	drpc = NULL;
5946 	if (mirrorcnt > 1)
5947 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
5948 		    M_WAITOK);
5949 
5950 	/*
5951 	 * Do the deallocate RPC for every DS, using a separate kernel process
5952 	 * for every DS except the last one.
5953 	 */
5954 	error = 0;
5955 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5956 		tdrpc->done = 0;
5957 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
5958 		tdrpc->off = off;
5959 		tdrpc->len = len;
5960 		tdrpc->nmp = *nmpp;
5961 		tdrpc->cred = cred;
5962 		tdrpc->p = p;
5963 		tdrpc->inprog = 0;
5964 		tdrpc->err = 0;
5965 		ret = EIO;
5966 		if (nfs_pnfsiothreads != 0) {
5967 			ret = nfs_pnfsio(start_deallocatedsdorpc, tdrpc);
5968 			NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: nfs_pnfsio=%d\n",
5969 			    ret);
5970 		}
5971 		if (ret != 0) {
5972 			ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len,
5973 			    NULL, cred, p);
5974 			if (nfsds_failerr(ret) && *failposp == -1)
5975 				*failposp = i;
5976 			else if (error == 0 && ret != 0)
5977 				error = ret;
5978 		}
5979 		nmpp++;
5980 		fhp++;
5981 	}
5982 	ret = nfsrv_deallocatedsdorpc(*nmpp, fhp, off, len, &na, cred, p);
5983 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
5984 		*failposp = mirrorcnt - 1;
5985 	else if (error == 0 && ret != 0)
5986 		error = ret;
5987 	if (error == 0)
5988 		error = nfsrv_setextattr(vp, &na, p);
5989 	NFSD_DEBUG(4, "nfsrv_deallocatedsrpc: aft setextat=%d\n", error);
5990 	tdrpc = drpc;
5991 	timo = hz / 50;		/* Wait for 20msec. */
5992 	if (timo < 1)
5993 		timo = 1;
5994 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
5995 		/* Wait for RPCs on separate threads to complete. */
5996 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
5997 			tsleep(&tdrpc->tsk, PVFS, "srvalds", timo);
5998 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
5999 			*failposp = i;
6000 		else if (error == 0 && tdrpc->err != 0)
6001 			error = tdrpc->err;
6002 	}
6003 	free(drpc, M_TEMP);
6004 	return (error);
6005 }
6006 
6007 static int
nfsrv_setattrdsdorpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct nfsvattr * nap,struct nfsvattr * dsnap)6008 nfsrv_setattrdsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6009     struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap,
6010     struct nfsvattr *dsnap)
6011 {
6012 	uint32_t *tl;
6013 	struct nfsrv_descript *nd;
6014 	nfsv4stateid_t st;
6015 	nfsattrbit_t attrbits;
6016 	int error;
6017 
6018 	NFSD_DEBUG(4, "in nfsrv_setattrdsdorpc\n");
6019 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6020 	/*
6021 	 * Use a stateid where other is an alternating 01010 pattern and
6022 	 * seqid is 0xffffffff.  This value is not defined as special by
6023 	 * the RFC and is used by the FreeBSD NFS server to indicate an
6024 	 * MDS->DS proxy operation.
6025 	 */
6026 	st.other[0] = 0x55555555;
6027 	st.other[1] = 0x55555555;
6028 	st.other[2] = 0x55555555;
6029 	st.seqid = 0xffffffff;
6030 	nfscl_reqstart(nd, NFSPROC_SETATTR, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6031 	    NULL, NULL, 0, 0, cred);
6032 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6033 	nfscl_fillsattr(nd, &nap->na_vattr, vp, NFSSATTR_FULL, 0);
6034 
6035 	/* Do a Getattr for the attributes that change due to writing. */
6036 	NFSZERO_ATTRBIT(&attrbits);
6037 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6038 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6039 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6040 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6041 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6042 	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
6043 	*tl = txdr_unsigned(NFSV4OP_GETATTR);
6044 	(void) nfsrv_putattrbit(nd, &attrbits);
6045 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6046 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6047 	if (error != 0) {
6048 		free(nd, M_TEMP);
6049 		return (error);
6050 	}
6051 	NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattrrpc=%d\n",
6052 	    nd->nd_repstat);
6053 	/* Get rid of weak cache consistency data for now. */
6054 	if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) ==
6055 	    (ND_NFSV4 | ND_V4WCCATTR)) {
6056 		error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
6057 		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
6058 		NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: wcc attr=%d\n", error);
6059 		if (error != 0)
6060 			goto nfsmout;
6061 		/*
6062 		 * Get rid of Op# and status for next op.
6063 		 */
6064 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6065 		if (*++tl != 0)
6066 			nd->nd_flag |= ND_NOMOREDATA;
6067 	}
6068 	error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL);
6069 	if (error != 0)
6070 		goto nfsmout;
6071 	if (nd->nd_repstat != 0)
6072 		error = nd->nd_repstat;
6073 	/*
6074 	 * Get the Change, Size, Access Time and Modify Time attributes and set
6075 	 * on the Metadata file, so its attributes will be what the file's
6076 	 * would be if it had been written.
6077 	 */
6078 	if (error == 0) {
6079 		NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_UNSIGNED);
6080 		error = nfsv4_loadattr(nd, NULL, dsnap, NULL, NULL, 0, NULL,
6081 		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
6082 	}
6083 	NFSD_DEBUG(4, "nfsrv_setattrdsdorpc: aft setattr loadattr=%d\n", error);
6084 nfsmout:
6085 	m_freem(nd->nd_mrep);
6086 	free(nd, M_TEMP);
6087 	NFSD_DEBUG(4, "nfsrv_setattrdsdorpc error=%d\n", error);
6088 	return (error);
6089 }
6090 
6091 struct nfsrvsetattrdsdorpc {
6092 	int			done;
6093 	int			inprog;
6094 	struct task		tsk;
6095 	fhandle_t		fh;
6096 	struct nfsmount		*nmp;
6097 	struct vnode		*vp;
6098 	struct ucred		*cred;
6099 	NFSPROC_T		*p;
6100 	struct nfsvattr		na;
6101 	struct nfsvattr		dsna;
6102 	int			err;
6103 };
6104 
6105 /*
6106  * Start up the thread that will execute nfsrv_setattrdsdorpc().
6107  */
6108 static void
start_setattrdsdorpc(void * arg,int pending)6109 start_setattrdsdorpc(void *arg, int pending)
6110 {
6111 	struct nfsrvsetattrdsdorpc *drpc;
6112 
6113 	drpc = (struct nfsrvsetattrdsdorpc *)arg;
6114 	drpc->err = nfsrv_setattrdsdorpc(&drpc->fh, drpc->cred, drpc->p,
6115 	    drpc->vp, drpc->nmp, &drpc->na, &drpc->dsna);
6116 	drpc->done = 1;
6117 }
6118 
6119 static int
nfsrv_setattrdsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,struct nfsvattr * nap,int * failposp)6120 nfsrv_setattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6121     struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt,
6122     struct nfsvattr *nap, int *failposp)
6123 {
6124 	struct nfsrvsetattrdsdorpc *drpc, *tdrpc = NULL;
6125 	struct nfsvattr na;
6126 	int error, i, ret, timo;
6127 
6128 	NFSD_DEBUG(4, "in nfsrv_setattrdsrpc\n");
6129 	drpc = NULL;
6130 	if (mirrorcnt > 1)
6131 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6132 		    M_WAITOK);
6133 
6134 	/*
6135 	 * Do the setattr RPC for every DS, using a separate kernel process
6136 	 * for every DS except the last one.
6137 	 */
6138 	error = 0;
6139 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6140 		tdrpc->done = 0;
6141 		tdrpc->inprog = 0;
6142 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6143 		tdrpc->nmp = *nmpp;
6144 		tdrpc->vp = vp;
6145 		tdrpc->cred = cred;
6146 		tdrpc->p = p;
6147 		tdrpc->na = *nap;
6148 		tdrpc->err = 0;
6149 		ret = EIO;
6150 		if (nfs_pnfsiothreads != 0) {
6151 			ret = nfs_pnfsio(start_setattrdsdorpc, tdrpc);
6152 			NFSD_DEBUG(4, "nfsrv_setattrdsrpc: nfs_pnfsio=%d\n",
6153 			    ret);
6154 		}
6155 		if (ret != 0) {
6156 			ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap,
6157 			    &na);
6158 			if (nfsds_failerr(ret) && *failposp == -1)
6159 				*failposp = i;
6160 			else if (error == 0 && ret != 0)
6161 				error = ret;
6162 		}
6163 		nmpp++;
6164 		fhp++;
6165 	}
6166 	ret = nfsrv_setattrdsdorpc(fhp, cred, p, vp, *nmpp, nap, &na);
6167 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6168 		*failposp = mirrorcnt - 1;
6169 	else if (error == 0 && ret != 0)
6170 		error = ret;
6171 	if (error == 0)
6172 		error = nfsrv_setextattr(vp, &na, p);
6173 	NFSD_DEBUG(4, "nfsrv_setattrdsrpc: aft setextat=%d\n", error);
6174 	tdrpc = drpc;
6175 	timo = hz / 50;		/* Wait for 20msec. */
6176 	if (timo < 1)
6177 		timo = 1;
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, "srvsads", timo);
6182 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6183 			*failposp = i;
6184 		else if (error == 0 && tdrpc->err != 0)
6185 			error = tdrpc->err;
6186 	}
6187 	free(drpc, M_TEMP);
6188 	return (error);
6189 }
6190 
6191 /*
6192  * Do a Setattr of an NFSv4 ACL on the DS file.
6193  */
6194 static int
nfsrv_setacldsdorpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct acl * aclp)6195 nfsrv_setacldsdorpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6196     struct vnode *vp, struct nfsmount *nmp, struct acl *aclp)
6197 {
6198 	struct nfsrv_descript *nd;
6199 	nfsv4stateid_t st;
6200 	nfsattrbit_t attrbits;
6201 	int error;
6202 
6203 	NFSD_DEBUG(4, "in nfsrv_setacldsdorpc\n");
6204 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6205 	/*
6206 	 * Use a stateid where other is an alternating 01010 pattern and
6207 	 * seqid is 0xffffffff.  This value is not defined as special by
6208 	 * the RFC and is used by the FreeBSD NFS server to indicate an
6209 	 * MDS->DS proxy operation.
6210 	 */
6211 	st.other[0] = 0x55555555;
6212 	st.other[1] = 0x55555555;
6213 	st.other[2] = 0x55555555;
6214 	st.seqid = 0xffffffff;
6215 	nfscl_reqstart(nd, NFSPROC_SETACL, nmp, (u_int8_t *)fhp, sizeof(*fhp),
6216 	    NULL, NULL, 0, 0, cred);
6217 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6218 	NFSZERO_ATTRBIT(&attrbits);
6219 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_ACL);
6220 	/*
6221 	 * The "vp" argument to nfsv4_fillattr() is only used for vnode_type(),
6222 	 * so passing in the metadata "vp" will be ok, since it is of
6223 	 * the same type (VREG).
6224 	 */
6225 	nfsv4_fillattr(nd, NULL, vp, aclp, NULL, NULL, 0, &attrbits, NULL,
6226 	    NULL, 0, 0, 0, 0, 0, NULL);
6227 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6228 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6229 	if (error != 0) {
6230 		free(nd, M_TEMP);
6231 		return (error);
6232 	}
6233 	NFSD_DEBUG(4, "nfsrv_setacldsdorpc: aft setaclrpc=%d\n",
6234 	    nd->nd_repstat);
6235 	error = nd->nd_repstat;
6236 	m_freem(nd->nd_mrep);
6237 	free(nd, M_TEMP);
6238 	return (error);
6239 }
6240 
6241 struct nfsrvsetacldsdorpc {
6242 	int			done;
6243 	int			inprog;
6244 	struct task		tsk;
6245 	fhandle_t		fh;
6246 	struct nfsmount		*nmp;
6247 	struct vnode		*vp;
6248 	struct ucred		*cred;
6249 	NFSPROC_T		*p;
6250 	struct acl		*aclp;
6251 	int			err;
6252 };
6253 
6254 /*
6255  * Start up the thread that will execute nfsrv_setacldsdorpc().
6256  */
6257 static void
start_setacldsdorpc(void * arg,int pending)6258 start_setacldsdorpc(void *arg, int pending)
6259 {
6260 	struct nfsrvsetacldsdorpc *drpc;
6261 
6262 	drpc = (struct nfsrvsetacldsdorpc *)arg;
6263 	drpc->err = nfsrv_setacldsdorpc(&drpc->fh, drpc->cred, drpc->p,
6264 	    drpc->vp, drpc->nmp, drpc->aclp);
6265 	drpc->done = 1;
6266 }
6267 
6268 static int
nfsrv_setacldsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount ** nmpp,int mirrorcnt,struct acl * aclp,int * failposp)6269 nfsrv_setacldsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6270     struct vnode *vp, struct nfsmount **nmpp, int mirrorcnt, struct acl *aclp,
6271     int *failposp)
6272 {
6273 	struct nfsrvsetacldsdorpc *drpc, *tdrpc = NULL;
6274 	int error, i, ret, timo;
6275 
6276 	NFSD_DEBUG(4, "in nfsrv_setacldsrpc\n");
6277 	drpc = NULL;
6278 	if (mirrorcnt > 1)
6279 		tdrpc = drpc = malloc(sizeof(*drpc) * (mirrorcnt - 1), M_TEMP,
6280 		    M_WAITOK);
6281 
6282 	/*
6283 	 * Do the setattr RPC for every DS, using a separate kernel process
6284 	 * for every DS except the last one.
6285 	 */
6286 	error = 0;
6287 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6288 		tdrpc->done = 0;
6289 		tdrpc->inprog = 0;
6290 		NFSBCOPY(fhp, &tdrpc->fh, sizeof(*fhp));
6291 		tdrpc->nmp = *nmpp;
6292 		tdrpc->vp = vp;
6293 		tdrpc->cred = cred;
6294 		tdrpc->p = p;
6295 		tdrpc->aclp = aclp;
6296 		tdrpc->err = 0;
6297 		ret = EIO;
6298 		if (nfs_pnfsiothreads != 0) {
6299 			ret = nfs_pnfsio(start_setacldsdorpc, tdrpc);
6300 			NFSD_DEBUG(4, "nfsrv_setacldsrpc: nfs_pnfsio=%d\n",
6301 			    ret);
6302 		}
6303 		if (ret != 0) {
6304 			ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp,
6305 			    aclp);
6306 			if (nfsds_failerr(ret) && *failposp == -1)
6307 				*failposp = i;
6308 			else if (error == 0 && ret != 0)
6309 				error = ret;
6310 		}
6311 		nmpp++;
6312 		fhp++;
6313 	}
6314 	ret = nfsrv_setacldsdorpc(fhp, cred, p, vp, *nmpp, aclp);
6315 	if (nfsds_failerr(ret) && *failposp == -1 && mirrorcnt > 1)
6316 		*failposp = mirrorcnt - 1;
6317 	else if (error == 0 && ret != 0)
6318 		error = ret;
6319 	NFSD_DEBUG(4, "nfsrv_setacldsrpc: aft setextat=%d\n", error);
6320 	tdrpc = drpc;
6321 	timo = hz / 50;		/* Wait for 20msec. */
6322 	if (timo < 1)
6323 		timo = 1;
6324 	for (i = 0; i < mirrorcnt - 1; i++, tdrpc++) {
6325 		/* Wait for RPCs on separate threads to complete. */
6326 		while (tdrpc->inprog != 0 && tdrpc->done == 0)
6327 			tsleep(&tdrpc->tsk, PVFS, "srvacds", timo);
6328 		if (nfsds_failerr(tdrpc->err) && *failposp == -1)
6329 			*failposp = i;
6330 		else if (error == 0 && tdrpc->err != 0)
6331 			error = tdrpc->err;
6332 	}
6333 	free(drpc, M_TEMP);
6334 	return (error);
6335 }
6336 
6337 /*
6338  * Getattr call to the DS for the attributes that change due to writing.
6339  */
6340 static int
nfsrv_getattrdsrpc(fhandle_t * fhp,struct ucred * cred,NFSPROC_T * p,struct vnode * vp,struct nfsmount * nmp,struct nfsvattr * nap)6341 nfsrv_getattrdsrpc(fhandle_t *fhp, struct ucred *cred, NFSPROC_T *p,
6342     struct vnode *vp, struct nfsmount *nmp, struct nfsvattr *nap)
6343 {
6344 	struct nfsrv_descript *nd;
6345 	int error;
6346 	nfsattrbit_t attrbits;
6347 
6348 	NFSD_DEBUG(4, "in nfsrv_getattrdsrpc\n");
6349 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6350 	nfscl_reqstart(nd, NFSPROC_GETATTR, nmp, (u_int8_t *)fhp,
6351 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6352 	NFSZERO_ATTRBIT(&attrbits);
6353 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
6354 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_CHANGE);
6355 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESS);
6356 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFY);
6357 	NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SPACEUSED);
6358 	(void) nfsrv_putattrbit(nd, &attrbits);
6359 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6360 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6361 	if (error != 0) {
6362 		free(nd, M_TEMP);
6363 		return (error);
6364 	}
6365 	NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft getattrrpc=%d\n",
6366 	    nd->nd_repstat);
6367 	if (nd->nd_repstat == 0) {
6368 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0,
6369 		    NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL,
6370 		    NULL, NULL);
6371 		/*
6372 		 * We can only save the updated values in the extended
6373 		 * attribute if the vp is exclusively locked.
6374 		 * This should happen when any of the following operations
6375 		 * occur on the vnode:
6376 		 *    Close, Delegreturn, LayoutCommit, LayoutReturn
6377 		 * As such, the updated extended attribute should get saved
6378 		 * before nfsrv_checkdsattr() returns 0 and allows the cached
6379 		 * attributes to be returned without calling this function.
6380 		 */
6381 		if (error == 0 && VOP_ISLOCKED(vp) == LK_EXCLUSIVE) {
6382 			error = nfsrv_setextattr(vp, nap, p);
6383 			NFSD_DEBUG(4, "nfsrv_getattrdsrpc: aft setextat=%d\n",
6384 			    error);
6385 		}
6386 	} else
6387 		error = nd->nd_repstat;
6388 	m_freem(nd->nd_mrep);
6389 	free(nd, M_TEMP);
6390 	NFSD_DEBUG(4, "nfsrv_getattrdsrpc error=%d\n", error);
6391 	return (error);
6392 }
6393 
6394 /*
6395  * Seek call to a DS.
6396  */
6397 static int
nfsrv_seekdsrpc(fhandle_t * fhp,off_t * offp,int content,bool * eofp,struct ucred * cred,NFSPROC_T * p,struct nfsmount * nmp)6398 nfsrv_seekdsrpc(fhandle_t *fhp, off_t *offp, int content, bool *eofp,
6399     struct ucred *cred, NFSPROC_T *p, struct nfsmount *nmp)
6400 {
6401 	uint32_t *tl;
6402 	struct nfsrv_descript *nd;
6403 	nfsv4stateid_t st;
6404 	int error;
6405 
6406 	NFSD_DEBUG(4, "in nfsrv_seekdsrpc\n");
6407 	/*
6408 	 * Use a stateid where other is an alternating 01010 pattern and
6409 	 * seqid is 0xffffffff.  This value is not defined as special by
6410 	 * the RFC and is used by the FreeBSD NFS server to indicate an
6411 	 * MDS->DS proxy operation.
6412 	 */
6413 	st.other[0] = 0x55555555;
6414 	st.other[1] = 0x55555555;
6415 	st.other[2] = 0x55555555;
6416 	st.seqid = 0xffffffff;
6417 	nd = malloc(sizeof(*nd), M_TEMP, M_WAITOK | M_ZERO);
6418 	nfscl_reqstart(nd, NFSPROC_SEEKDS, nmp, (u_int8_t *)fhp,
6419 	    sizeof(fhandle_t), NULL, NULL, 0, 0, cred);
6420 	nfsm_stateidtom(nd, &st, NFSSTATEID_PUTSTATEID);
6421 	NFSM_BUILD(tl, uint32_t *, NFSX_HYPER + NFSX_UNSIGNED);
6422 	txdr_hyper(*offp, tl); tl += 2;
6423 	*tl = txdr_unsigned(content);
6424 	error = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, NULL, p, cred,
6425 	    NFS_PROG, NFS_VER4, NULL, 1, NULL, NULL);
6426 	if (error != 0) {
6427 		free(nd, M_TEMP);
6428 		return (error);
6429 	}
6430 	NFSD_DEBUG(4, "nfsrv_seekdsrpc: aft seekrpc=%d\n", nd->nd_repstat);
6431 	if (nd->nd_repstat == 0) {
6432 		NFSM_DISSECT(tl, uint32_t *, NFSX_UNSIGNED + NFSX_HYPER);
6433 		if (*tl++ == newnfs_true)
6434 			*eofp = true;
6435 		else
6436 			*eofp = false;
6437 		*offp = fxdr_hyper(tl);
6438 	} else
6439 		error = nd->nd_repstat;
6440 nfsmout:
6441 	m_freem(nd->nd_mrep);
6442 	free(nd, M_TEMP);
6443 	NFSD_DEBUG(4, "nfsrv_seekdsrpc error=%d\n", error);
6444 	return (error);
6445 }
6446 
6447 /*
6448  * Get the device id and file handle for a DS file.
6449  */
6450 int
nfsrv_dsgetdevandfh(struct vnode * vp,NFSPROC_T * p,int * mirrorcntp,fhandle_t * fhp,char * devid)6451 nfsrv_dsgetdevandfh(struct vnode *vp, NFSPROC_T *p, int *mirrorcntp,
6452     fhandle_t *fhp, char *devid)
6453 {
6454 	int buflen, error;
6455 	char *buf;
6456 
6457 	buflen = 1024;
6458 	buf = malloc(buflen, M_TEMP, M_WAITOK);
6459 	error = nfsrv_dsgetsockmnt(vp, 0, buf, &buflen, mirrorcntp, p, NULL,
6460 	    fhp, devid, NULL, NULL, NULL, NULL, NULL, NULL);
6461 	free(buf, M_TEMP);
6462 	return (error);
6463 }
6464 
6465 /*
6466  * Do a Lookup against the DS for the filename.
6467  */
6468 static int
nfsrv_pnfslookupds(struct vnode * vp,struct vnode * dvp,struct pnfsdsfile * pf,struct vnode ** nvpp,NFSPROC_T * p)6469 nfsrv_pnfslookupds(struct vnode *vp, struct vnode *dvp, struct pnfsdsfile *pf,
6470     struct vnode **nvpp, NFSPROC_T *p)
6471 {
6472 	struct nameidata named;
6473 	struct ucred *tcred;
6474 	char *bufp;
6475 	u_long *hashp;
6476 	struct vnode *nvp;
6477 	int error;
6478 
6479 	tcred = newnfs_getcred();
6480 	named.ni_cnd.cn_nameiop = LOOKUP;
6481 	named.ni_cnd.cn_lkflags = LK_SHARED | LK_RETRY;
6482 	named.ni_cnd.cn_cred = tcred;
6483 	named.ni_cnd.cn_flags = ISLASTCN | LOCKPARENT | LOCKLEAF;
6484 	nfsvno_setpathbuf(&named, &bufp, &hashp);
6485 	named.ni_cnd.cn_nameptr = bufp;
6486 	named.ni_cnd.cn_namelen = strlen(pf->dsf_filename);
6487 	strlcpy(bufp, pf->dsf_filename, NAME_MAX);
6488 	NFSD_DEBUG(4, "nfsrv_pnfslookupds: filename=%s\n", bufp);
6489 	error = VOP_LOOKUP(dvp, &nvp, &named.ni_cnd);
6490 	NFSD_DEBUG(4, "nfsrv_pnfslookupds: aft LOOKUP=%d\n", error);
6491 	NFSFREECRED(tcred);
6492 	nfsvno_relpathbuf(&named);
6493 	if (error == 0)
6494 		*nvpp = nvp;
6495 	NFSD_DEBUG(4, "eo nfsrv_pnfslookupds=%d\n", error);
6496 	return (error);
6497 }
6498 
6499 /*
6500  * Set the file handle to the correct one.
6501  */
6502 static void
nfsrv_pnfssetfh(struct vnode * vp,struct pnfsdsfile * pf,char * devid,char * fnamep,struct vnode * nvp,NFSPROC_T * p)6503 nfsrv_pnfssetfh(struct vnode *vp, struct pnfsdsfile *pf, char *devid,
6504     char *fnamep, struct vnode *nvp, NFSPROC_T *p)
6505 {
6506 	struct nfsnode *np;
6507 	int ret = 0;
6508 
6509 	np = VTONFS(nvp);
6510 	NFSBCOPY(np->n_fhp->nfh_fh, &pf->dsf_fh, NFSX_MYFH);
6511 	/*
6512 	 * We can only do a vn_set_extattr() if the vnode is exclusively
6513 	 * locked and vn_start_write() has been done.  If devid != NULL or
6514 	 * fnamep != NULL or the vnode is shared locked, vn_start_write()
6515 	 * may not have been done.
6516 	 * If not done now, it will be done on a future call.
6517 	 */
6518 	if (devid == NULL && fnamep == NULL && NFSVOPISLOCKED(vp) ==
6519 	    LK_EXCLUSIVE)
6520 		ret = vn_extattr_set(vp, IO_NODELOCKED,
6521 		    EXTATTR_NAMESPACE_SYSTEM, "pnfsd.dsfile", sizeof(*pf),
6522 		    (char *)pf, p);
6523 	NFSD_DEBUG(4, "eo nfsrv_pnfssetfh=%d\n", ret);
6524 }
6525 
6526 /*
6527  * Cause RPCs waiting on "nmp" to fail.  This is called for a DS mount point
6528  * when the DS has failed.
6529  */
6530 void
nfsrv_killrpcs(struct nfsmount * nmp)6531 nfsrv_killrpcs(struct nfsmount *nmp)
6532 {
6533 
6534 	/*
6535 	 * Call newnfs_nmcancelreqs() to cause
6536 	 * any RPCs in progress on the mount point to
6537 	 * fail.
6538 	 * This will cause any process waiting for an
6539 	 * RPC to complete while holding a vnode lock
6540 	 * on the mounted-on vnode (such as "df" or
6541 	 * a non-forced "umount") to fail.
6542 	 * This will unlock the mounted-on vnode so
6543 	 * a forced dismount can succeed.
6544 	 * The NFSMNTP_CANCELRPCS flag should be set when this function is
6545 	 * called.
6546 	 */
6547 	newnfs_nmcancelreqs(nmp);
6548 }
6549 
6550 /*
6551  * Sum up the statfs info for each of the DSs, so that the client will
6552  * receive the total for all DSs.
6553  */
6554 static int
nfsrv_pnfsstatfs(struct statfs * sf,struct mount * mp)6555 nfsrv_pnfsstatfs(struct statfs *sf, struct mount *mp)
6556 {
6557 	struct statfs *tsf;
6558 	struct nfsdevice *ds;
6559 	struct vnode **dvpp, **tdvpp, *dvp;
6560 	uint64_t tot;
6561 	int cnt, error = 0, i;
6562 
6563 	if (nfsrv_devidcnt <= 0)
6564 		return (ENXIO);
6565 	dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
6566 	tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
6567 
6568 	/* Get an array of the dvps for the DSs. */
6569 	tdvpp = dvpp;
6570 	i = 0;
6571 	NFSDDSLOCK();
6572 	/* First, search for matches for same file system. */
6573 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6574 		if (ds->nfsdev_nmp != NULL && ds->nfsdev_mdsisset != 0 &&
6575 		    fsidcmp(&ds->nfsdev_mdsfsid, &mp->mnt_stat.f_fsid) == 0) {
6576 			if (++i > nfsrv_devidcnt)
6577 				break;
6578 			*tdvpp++ = ds->nfsdev_dvp;
6579 		}
6580 	}
6581 	/*
6582 	 * If no matches for same file system, total all servers not assigned
6583 	 * to a file system.
6584 	 */
6585 	if (i == 0) {
6586 		TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
6587 			if (ds->nfsdev_nmp != NULL &&
6588 			    ds->nfsdev_mdsisset == 0) {
6589 				if (++i > nfsrv_devidcnt)
6590 					break;
6591 				*tdvpp++ = ds->nfsdev_dvp;
6592 			}
6593 		}
6594 	}
6595 	NFSDDSUNLOCK();
6596 	cnt = i;
6597 
6598 	/* Do a VFS_STATFS() for each of the DSs and sum them up. */
6599 	tdvpp = dvpp;
6600 	for (i = 0; i < cnt && error == 0; i++) {
6601 		dvp = *tdvpp++;
6602 		error = VFS_STATFS(dvp->v_mount, tsf);
6603 		if (error == 0) {
6604 			if (sf->f_bsize == 0) {
6605 				if (tsf->f_bsize > 0)
6606 					sf->f_bsize = tsf->f_bsize;
6607 				else
6608 					sf->f_bsize = 8192;
6609 			}
6610 			if (tsf->f_blocks > 0) {
6611 				if (sf->f_bsize != tsf->f_bsize) {
6612 					tot = tsf->f_blocks * tsf->f_bsize;
6613 					sf->f_blocks += (tot / sf->f_bsize);
6614 				} else
6615 					sf->f_blocks += tsf->f_blocks;
6616 			}
6617 			if (tsf->f_bfree > 0) {
6618 				if (sf->f_bsize != tsf->f_bsize) {
6619 					tot = tsf->f_bfree * tsf->f_bsize;
6620 					sf->f_bfree += (tot / sf->f_bsize);
6621 				} else
6622 					sf->f_bfree += tsf->f_bfree;
6623 			}
6624 			if (tsf->f_bavail > 0) {
6625 				if (sf->f_bsize != tsf->f_bsize) {
6626 					tot = tsf->f_bavail * tsf->f_bsize;
6627 					sf->f_bavail += (tot / sf->f_bsize);
6628 				} else
6629 					sf->f_bavail += tsf->f_bavail;
6630 			}
6631 		}
6632 	}
6633 	free(tsf, M_TEMP);
6634 	free(dvpp, M_TEMP);
6635 	return (error);
6636 }
6637 
6638 /*
6639  * Set an NFSv4 acl.
6640  */
6641 int
nfsrv_setacl(struct vnode * vp,NFSACL_T * aclp,struct ucred * cred,NFSPROC_T * p)6642 nfsrv_setacl(struct vnode *vp, NFSACL_T *aclp, struct ucred *cred, NFSPROC_T *p)
6643 {
6644 	int error;
6645 
6646 	if (nfsrv_useacl == 0 || nfs_supportsnfsv4acls(vp) == 0) {
6647 		error = NFSERR_ATTRNOTSUPP;
6648 		goto out;
6649 	}
6650 	/*
6651 	 * With NFSv4 ACLs, chmod(2) may need to add additional entries.
6652 	 * Make sure it has enough room for that - splitting every entry
6653 	 * into two and appending "canonical six" entries at the end.
6654 	 * Cribbed out of kern/vfs_acl.c - Rick M.
6655 	 */
6656 	if (aclp->acl_cnt > (ACL_MAX_ENTRIES - 6) / 2) {
6657 		error = NFSERR_ATTRNOTSUPP;
6658 		goto out;
6659 	}
6660 	error = VOP_SETACL(vp, ACL_TYPE_NFS4, aclp, cred, p);
6661 	if (error == 0) {
6662 		error = nfsrv_dssetacl(vp, aclp, cred, p);
6663 		if (error == ENOENT)
6664 			error = 0;
6665 	}
6666 
6667 out:
6668 	NFSEXITCODE(error);
6669 	return (error);
6670 }
6671 
6672 /*
6673  * Seek vnode op call (actually it is a VOP_IOCTL()).
6674  * This function is called with the vnode locked, but unlocks and vrele()s
6675  * the vp before returning.
6676  */
6677 int
nfsvno_seek(struct nfsrv_descript * nd,struct vnode * vp,u_long cmd,off_t * offp,int content,bool * eofp,struct ucred * cred,NFSPROC_T * p)6678 nfsvno_seek(struct nfsrv_descript *nd, struct vnode *vp, u_long cmd,
6679     off_t *offp, int content, bool *eofp, struct ucred *cred, NFSPROC_T *p)
6680 {
6681 	struct nfsvattr at;
6682 	int error, ret;
6683 
6684 	ASSERT_VOP_LOCKED(vp, "nfsvno_seek vp");
6685 	/*
6686 	 * Attempt to seek on a DS file. A return of ENOENT implies
6687 	 * there is no DS file to seek on.
6688 	 */
6689 	error = nfsrv_proxyds(vp, 0, 0, cred, p, NFSPROC_SEEKDS, NULL,
6690 	    NULL, NULL, NULL, NULL, offp, content, eofp);
6691 	if (error != ENOENT) {
6692 		vput(vp);
6693 		return (error);
6694 	}
6695 
6696 	/*
6697 	 * Do the VOP_IOCTL() call.  For the case where *offp == file_size,
6698 	 * VOP_IOCTL() will return ENXIO.  However, the correct reply for
6699 	 * NFSv4.2 is *eofp == true and error == 0 for this case.
6700 	 */
6701 	NFSVOPUNLOCK(vp);
6702 	error = VOP_IOCTL(vp, cmd, offp, 0, cred, p);
6703 	*eofp = false;
6704 	if (error == ENXIO || (error == 0 && cmd == FIOSEEKHOLE)) {
6705 		/* Handle the cases where we might be at EOF. */
6706 		ret = nfsvno_getattr(vp, &at, nd, p, 0, NULL);
6707 		if (ret == 0 && *offp == at.na_size) {
6708 			*eofp = true;
6709 			error = 0;
6710 		}
6711 		if (ret != 0 && error == 0)
6712 			error = ret;
6713 	}
6714 	vrele(vp);
6715 	NFSEXITCODE(error);
6716 	return (error);
6717 }
6718 
6719 /*
6720  * Allocate vnode op call.
6721  */
6722 int
nfsvno_allocate(struct vnode * vp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p)6723 nfsvno_allocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6724     NFSPROC_T *p)
6725 {
6726 	int error;
6727 	off_t olen;
6728 
6729 	ASSERT_VOP_ELOCKED(vp, "nfsvno_allocate vp");
6730 	/*
6731 	 * Attempt to allocate on a DS file. A return of ENOENT implies
6732 	 * there is no DS file to allocate on.
6733 	 */
6734 	error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_ALLOCATE, NULL,
6735 	    NULL, NULL, NULL, NULL, &len, 0, NULL);
6736 	if (error != ENOENT)
6737 		return (error);
6738 
6739 	/*
6740 	 * Do the actual VOP_ALLOCATE(), looping so long as
6741 	 * progress is being made, to achieve completion.
6742 	 */
6743 	do {
6744 		olen = len;
6745 		error = VOP_ALLOCATE(vp, &off, &len, IO_SYNC, cred);
6746 		if (error == 0 && len > 0 && olen > len)
6747 			maybe_yield();
6748 	} while (error == 0 && len > 0 && olen > len);
6749 	if (error == 0 && len > 0)
6750 		error = NFSERR_IO;
6751 	NFSEXITCODE(error);
6752 	return (error);
6753 }
6754 
6755 /*
6756  * Deallocate vnode op call.
6757  */
6758 int
nfsvno_deallocate(struct vnode * vp,off_t off,off_t len,struct ucred * cred,NFSPROC_T * p)6759 nfsvno_deallocate(struct vnode *vp, off_t off, off_t len, struct ucred *cred,
6760     NFSPROC_T *p)
6761 {
6762 	int error;
6763 	off_t olen;
6764 
6765 	ASSERT_VOP_ELOCKED(vp, "nfsvno_deallocate vp");
6766 	/*
6767 	 * Attempt to deallocate on a DS file. A return of ENOENT implies
6768 	 * there is no DS file to deallocate on.
6769 	 */
6770 	error = nfsrv_proxyds(vp, off, 0, cred, p, NFSPROC_DEALLOCATE, NULL,
6771 	    NULL, NULL, NULL, NULL, &len, 0, NULL);
6772 	if (error != ENOENT)
6773 		return (error);
6774 
6775 	/*
6776 	 * Do the actual VOP_DEALLOCATE(), looping so long as
6777 	 * progress is being made, to achieve completion.
6778 	 */
6779 	do {
6780 		olen = len;
6781 		error = VOP_DEALLOCATE(vp, &off, &len, 0, IO_SYNC, cred);
6782 		if (error == 0 && len > 0 && olen > len)
6783 			maybe_yield();
6784 	} while (error == 0 && len > 0 && olen > len);
6785 	if (error == 0 && len > 0)
6786 		error = NFSERR_IO;
6787 	NFSEXITCODE(error);
6788 	return (error);
6789 }
6790 
6791 /*
6792  * Get Extended Atribute vnode op into an mbuf list.
6793  */
6794 int
nfsvno_getxattr(struct vnode * vp,char * name,uint32_t maxresp,struct ucred * cred,uint64_t flag,int maxextsiz,struct thread * p,struct mbuf ** mpp,struct mbuf ** mpendp,int * lenp)6795 nfsvno_getxattr(struct vnode *vp, char *name, uint32_t maxresp,
6796     struct ucred *cred, uint64_t flag, int maxextsiz, struct thread *p,
6797     struct mbuf **mpp, struct mbuf **mpendp, int *lenp)
6798 {
6799 	struct iovec *iv;
6800 	struct uio io, *uiop = &io;
6801 	struct mbuf *m, *m2;
6802 	int alen, error, len, tlen;
6803 	size_t siz;
6804 
6805 	/* First, find out the size of the extended attribute. */
6806 	error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6807 	    &siz, cred, p);
6808 	if (error != 0)
6809 		return (NFSERR_NOXATTR);
6810 	if (siz > maxresp - NFS_MAXXDR)
6811 		return (NFSERR_XATTR2BIG);
6812 	len = siz;
6813 	tlen = NFSM_RNDUP(len);
6814 	if (tlen > 0) {
6815 		/*
6816 		 * If cnt > MCLBYTES and the reply will not be saved, use
6817 		 * ext_pgs mbufs for TLS.
6818 		 * For NFSv4.0, we do not know for sure if the reply will
6819 		 * be saved, so do not use ext_pgs mbufs for NFSv4.0.
6820 		 * Always use ext_pgs mbufs if ND_EXTPG is set.
6821 		 */
6822 		if ((flag & ND_EXTPG) != 0 || (tlen > MCLBYTES &&
6823 		    (flag & (ND_TLS | ND_SAVEREPLY)) == ND_TLS &&
6824 		    (flag & (ND_NFSV4 | ND_NFSV41)) != ND_NFSV4))
6825 			uiop->uio_iovcnt = nfsrv_createiovec_extpgs(tlen,
6826 			    maxextsiz, &m, &m2, &iv);
6827 		else
6828 			uiop->uio_iovcnt = nfsrv_createiovec(tlen, &m, &m2,
6829 			    &iv);
6830 		uiop->uio_iov = iv;
6831 	} else {
6832 		uiop->uio_iovcnt = 0;
6833 		uiop->uio_iov = iv = NULL;
6834 		m = m2 = NULL;
6835 	}
6836 	uiop->uio_offset = 0;
6837 	uiop->uio_resid = tlen;
6838 	uiop->uio_rw = UIO_READ;
6839 	uiop->uio_segflg = UIO_SYSSPACE;
6840 	uiop->uio_td = p;
6841 #ifdef MAC
6842 	error = mac_vnode_check_getextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6843 	    name);
6844 	if (error != 0)
6845 		goto out;
6846 #endif
6847 
6848 	if (tlen > 0)
6849 		error = VOP_GETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6850 		    NULL, cred, p);
6851 	if (error != 0)
6852 		goto out;
6853 	if (uiop->uio_resid > 0) {
6854 		alen = tlen;
6855 		len = tlen - uiop->uio_resid;
6856 		tlen = NFSM_RNDUP(len);
6857 		if (alen != tlen)
6858 			printf("nfsvno_getxattr: weird size read\n");
6859 		if (tlen == 0) {
6860 			m_freem(m);
6861 			m = m2 = NULL;
6862 		} else if (alen != tlen || tlen != len)
6863 			m2 = nfsrv_adj(m, alen - tlen, tlen - len);
6864 	}
6865 	*lenp = len;
6866 	*mpp = m;
6867 	*mpendp = m2;
6868 
6869 out:
6870 	if (error != 0) {
6871 		if (m != NULL)
6872 			m_freem(m);
6873 		*lenp = 0;
6874 	}
6875 	free(iv, M_TEMP);
6876 	NFSEXITCODE(error);
6877 	return (error);
6878 }
6879 
6880 /*
6881  * Set Extended attribute vnode op from an mbuf list.
6882  */
6883 int
nfsvno_setxattr(struct vnode * vp,char * name,int len,struct mbuf * m,char * cp,struct ucred * cred,struct thread * p)6884 nfsvno_setxattr(struct vnode *vp, char *name, int len, struct mbuf *m,
6885     char *cp, struct ucred *cred, struct thread *p)
6886 {
6887 	struct iovec *iv;
6888 	struct uio uio, *uiop = &uio;
6889 	int cnt, error;
6890 
6891 	error = 0;
6892 #ifdef MAC
6893 	error = mac_vnode_check_setextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6894 	    name);
6895 #endif
6896 	if (error != 0)
6897 		goto out;
6898 
6899 	uiop->uio_rw = UIO_WRITE;
6900 	uiop->uio_segflg = UIO_SYSSPACE;
6901 	uiop->uio_td = p;
6902 	uiop->uio_offset = 0;
6903 	uiop->uio_resid = len;
6904 	if (len > 0) {
6905 		error = nfsrv_createiovecw(len, m, cp, &iv, &cnt);
6906 		uiop->uio_iov = iv;
6907 		uiop->uio_iovcnt = cnt;
6908 	} else {
6909 		uiop->uio_iov = iv = NULL;
6910 		uiop->uio_iovcnt = 0;
6911 	}
6912 	if (error == 0) {
6913 		error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, uiop,
6914 		    cred, p);
6915 		if (error == 0) {
6916 			if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6917 				nfsvno_updateds(vp, cred, p);
6918 			error = VOP_FSYNC(vp, MNT_WAIT, p);
6919 		}
6920 		free(iv, M_TEMP);
6921 	}
6922 
6923 out:
6924 	NFSEXITCODE(error);
6925 	return (error);
6926 }
6927 
6928 /*
6929  * For a pNFS server, the DS file's ctime and
6930  * va_filerev (TimeMetadata and Change) needs to
6931  * be updated.  This is a hack, but works by
6932  * flipping the S_ISGID bit in va_mode and then
6933  * flipping it back.
6934  * It does result in two MDS->DS RPCs, but creating
6935  * a custom RPC just to do this seems overkill, since
6936  * Setxattr/Rmxattr will not be done that frequently.
6937  * If it fails part way through, that is not too
6938  * serious, since the DS file is never executed.
6939  */
6940 static void
nfsvno_updateds(struct vnode * vp,struct ucred * cred,NFSPROC_T * p)6941 nfsvno_updateds(struct vnode *vp, struct ucred *cred, NFSPROC_T *p)
6942 {
6943 	struct nfsvattr nva;
6944 	int ret;
6945 	u_short tmode;
6946 
6947 	ret = VOP_GETATTR(vp, &nva.na_vattr, cred);
6948 	if (ret == 0) {
6949 		tmode = nva.na_mode;
6950 		NFSVNO_ATTRINIT(&nva);
6951 		tmode ^= S_ISGID;
6952 		NFSVNO_SETATTRVAL(&nva, mode, tmode);
6953 		ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6954 		    NFSPROC_SETATTR, NULL, NULL, NULL, &nva,
6955 		    NULL, NULL, 0, NULL);
6956 		if (ret == 0) {
6957 			tmode ^= S_ISGID;
6958 			NFSVNO_SETATTRVAL(&nva, mode, tmode);
6959 			ret = nfsrv_proxyds(vp, 0, 0, cred, p,
6960 			    NFSPROC_SETATTR, NULL, NULL, NULL,
6961 			    &nva, NULL, NULL, 0, NULL);
6962 		}
6963 	}
6964 }
6965 
6966 /*
6967  * Remove Extended attribute vnode op.
6968  */
6969 int
nfsvno_rmxattr(struct nfsrv_descript * nd,struct vnode * vp,char * name,struct ucred * cred,struct thread * p)6970 nfsvno_rmxattr(struct nfsrv_descript *nd, struct vnode *vp, char *name,
6971     struct ucred *cred, struct thread *p)
6972 {
6973 	int error;
6974 
6975 	/*
6976 	 * Get rid of any delegations.  I am not sure why this is required,
6977 	 * but RFC-8276 says so.
6978 	 */
6979 	error = nfsrv_checkremove(vp, 0, nd, nd->nd_clientid, p);
6980 	if (error != 0)
6981 		goto out;
6982 #ifdef MAC
6983 	error = mac_vnode_check_deleteextattr(cred, vp, EXTATTR_NAMESPACE_USER,
6984 	    name);
6985 	if (error != 0)
6986 		goto out;
6987 #endif
6988 
6989 	error = VOP_DELETEEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, cred, p);
6990 	if (error == EOPNOTSUPP)
6991 		error = VOP_SETEXTATTR(vp, EXTATTR_NAMESPACE_USER, name, NULL,
6992 		    cred, p);
6993 	if (error == 0) {
6994 		if (vp->v_type == VREG && nfsrv_devidcnt != 0)
6995 			nfsvno_updateds(vp, cred, p);
6996 		error = VOP_FSYNC(vp, MNT_WAIT, p);
6997 	}
6998 out:
6999 	NFSEXITCODE(error);
7000 	return (error);
7001 }
7002 
7003 /*
7004  * List Extended Atribute vnode op into an mbuf list.
7005  */
7006 int
nfsvno_listxattr(struct vnode * vp,uint64_t cookie,struct ucred * cred,struct thread * p,u_char ** bufp,uint32_t * lenp,bool * eofp)7007 nfsvno_listxattr(struct vnode *vp, uint64_t cookie, struct ucred *cred,
7008     struct thread *p, u_char **bufp, uint32_t *lenp, bool *eofp)
7009 {
7010 	struct iovec iv;
7011 	struct uio io;
7012 	int error;
7013 	size_t siz;
7014 
7015 	*bufp = NULL;
7016 	/* First, find out the size of the extended attribute. */
7017 	error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, NULL, &siz, cred,
7018 	    p);
7019 	if (error != 0)
7020 		return (NFSERR_NOXATTR);
7021 	if (siz <= cookie) {
7022 		*lenp = 0;
7023 		*eofp = true;
7024 		goto out;
7025 	}
7026 	if (siz > cookie + *lenp) {
7027 		siz = cookie + *lenp;
7028 		*eofp = false;
7029 	} else
7030 		*eofp = true;
7031 	/* Just choose a sanity limit of 10Mbytes for malloc(M_TEMP). */
7032 	if (siz > 10 * 1024 * 1024) {
7033 		error = NFSERR_XATTR2BIG;
7034 		goto out;
7035 	}
7036 	*bufp = malloc(siz, M_TEMP, M_WAITOK);
7037 	iv.iov_base = *bufp;
7038 	iv.iov_len = siz;
7039 	io.uio_iovcnt = 1;
7040 	io.uio_iov = &iv;
7041 	io.uio_offset = 0;
7042 	io.uio_resid = siz;
7043 	io.uio_rw = UIO_READ;
7044 	io.uio_segflg = UIO_SYSSPACE;
7045 	io.uio_td = p;
7046 #ifdef MAC
7047 	error = mac_vnode_check_listextattr(cred, vp, EXTATTR_NAMESPACE_USER);
7048 	if (error != 0)
7049 		goto out;
7050 #endif
7051 
7052 	error = VOP_LISTEXTATTR(vp, EXTATTR_NAMESPACE_USER, &io, NULL, cred,
7053 	    p);
7054 	if (error != 0)
7055 		goto out;
7056 	if (io.uio_resid > 0)
7057 		siz -= io.uio_resid;
7058 	*lenp = siz;
7059 
7060 out:
7061 	if (error != 0) {
7062 		free(*bufp, M_TEMP);
7063 		*bufp = NULL;
7064 	}
7065 	NFSEXITCODE(error);
7066 	return (error);
7067 }
7068 
7069 /*
7070  * Trim trailing data off the mbuf list being built.
7071  */
7072 void
nfsm_trimtrailing(struct nfsrv_descript * nd,struct mbuf * mb,char * bpos,int bextpg,int bextpgsiz)7073 nfsm_trimtrailing(struct nfsrv_descript *nd, struct mbuf *mb, char *bpos,
7074     int bextpg, int bextpgsiz)
7075 {
7076 	vm_page_t pg;
7077 	int fullpgsiz, i;
7078 
7079 	if (mb->m_next != NULL) {
7080 		m_freem(mb->m_next);
7081 		mb->m_next = NULL;
7082 	}
7083 	if ((mb->m_flags & M_EXTPG) != 0) {
7084 		KASSERT(bextpg >= 0 && bextpg < mb->m_epg_npgs,
7085 		    ("nfsm_trimtrailing: bextpg out of range"));
7086 		KASSERT(bpos == (char *)(void *)
7087 		    PHYS_TO_DMAP(mb->m_epg_pa[bextpg]) + PAGE_SIZE - bextpgsiz,
7088 		    ("nfsm_trimtrailing: bextpgsiz bad!"));
7089 
7090 		/* First, get rid of any pages after this position. */
7091 		for (i = mb->m_epg_npgs - 1; i > bextpg; i--) {
7092 			pg = PHYS_TO_VM_PAGE(mb->m_epg_pa[i]);
7093 			vm_page_unwire_noq(pg);
7094 			vm_page_free(pg);
7095 		}
7096 		mb->m_epg_npgs = bextpg + 1;
7097 		if (bextpg == 0)
7098 			fullpgsiz = PAGE_SIZE - mb->m_epg_1st_off;
7099 		else
7100 			fullpgsiz = PAGE_SIZE;
7101 		mb->m_epg_last_len = fullpgsiz - bextpgsiz;
7102 		mb->m_len = m_epg_pagelen(mb, 0, mb->m_epg_1st_off);
7103 		for (i = 1; i < mb->m_epg_npgs; i++)
7104 			mb->m_len += m_epg_pagelen(mb, i, 0);
7105 		nd->nd_bextpgsiz = bextpgsiz;
7106 		nd->nd_bextpg = bextpg;
7107 	} else
7108 		mb->m_len = bpos - mtod(mb, char *);
7109 	nd->nd_mb = mb;
7110 	nd->nd_bpos = bpos;
7111 }
7112 
7113 
7114 /*
7115  * Check to see if a put file handle operation should test for
7116  * NFSERR_WRONGSEC, although NFSv3 actually returns NFSERR_AUTHERR.
7117  * When Open is the next operation, NFSERR_WRONGSEC cannot be
7118  * replied for the Open cases that use a component.  This can
7119  * be identified by the fact that the file handle's type is VDIR.
7120  */
7121 bool
nfsrv_checkwrongsec(struct nfsrv_descript * nd,int nextop,__enum_uint8 (vtype)vtyp)7122 nfsrv_checkwrongsec(struct nfsrv_descript *nd, int nextop, __enum_uint8(vtype) vtyp)
7123 {
7124 
7125 	if ((nd->nd_flag & ND_NFSV4) == 0)
7126 		return (true);
7127 
7128 	if ((nd->nd_flag & ND_LASTOP) != 0)
7129 		return (false);
7130 
7131 	if (nextop == NFSV4OP_PUTROOTFH || nextop == NFSV4OP_PUTFH ||
7132 	    nextop == NFSV4OP_PUTPUBFH || nextop == NFSV4OP_RESTOREFH ||
7133 	    nextop == NFSV4OP_LOOKUP || nextop == NFSV4OP_LOOKUPP ||
7134 	    nextop == NFSV4OP_SECINFO || nextop == NFSV4OP_SECINFONONAME)
7135 		return (false);
7136 	if (nextop == NFSV4OP_OPEN && vtyp == VDIR)
7137 		return (false);
7138 	return (true);
7139 }
7140 
7141 /*
7142  * Check DSs marked no space.
7143  */
7144 void
nfsrv_checknospc(void)7145 nfsrv_checknospc(void)
7146 {
7147 	struct statfs *tsf;
7148 	struct nfsdevice *ds;
7149 	struct vnode **dvpp, **tdvpp, *dvp;
7150 	char *devid, *tdevid;
7151 	int cnt, error = 0, i;
7152 
7153 	if (nfsrv_devidcnt <= 0)
7154 		return;
7155 	dvpp = mallocarray(nfsrv_devidcnt, sizeof(*dvpp), M_TEMP, M_WAITOK);
7156 	devid = malloc(nfsrv_devidcnt * NFSX_V4DEVICEID, M_TEMP, M_WAITOK);
7157 	tsf = malloc(sizeof(*tsf), M_TEMP, M_WAITOK);
7158 
7159 	/* Get an array of the dvps for the DSs. */
7160 	tdvpp = dvpp;
7161 	tdevid = devid;
7162 	i = 0;
7163 	NFSDDSLOCK();
7164 	/* First, search for matches for same file system. */
7165 	TAILQ_FOREACH(ds, &nfsrv_devidhead, nfsdev_list) {
7166 		if (ds->nfsdev_nmp != NULL && ds->nfsdev_nospc) {
7167 			if (++i > nfsrv_devidcnt)
7168 				break;
7169 			*tdvpp++ = ds->nfsdev_dvp;
7170 			NFSBCOPY(ds->nfsdev_deviceid, tdevid, NFSX_V4DEVICEID);
7171 			tdevid += NFSX_V4DEVICEID;
7172 		}
7173 	}
7174 	NFSDDSUNLOCK();
7175 
7176 	/* Do a VFS_STATFS() for each of the DSs and clear no space. */
7177 	cnt = i;
7178 	tdvpp = dvpp;
7179 	tdevid = devid;
7180 	for (i = 0; i < cnt && error == 0; i++) {
7181 		dvp = *tdvpp++;
7182 		error = VFS_STATFS(dvp->v_mount, tsf);
7183 		if (error == 0 && tsf->f_bavail > 0) {
7184 			NFSD_DEBUG(1, "nfsrv_checknospc: reset nospc\n");
7185 			nfsrv_marknospc(tdevid, false);
7186 		}
7187 		tdevid += NFSX_V4DEVICEID;
7188 	}
7189 	free(tsf, M_TEMP);
7190 	free(dvpp, M_TEMP);
7191 	free(devid, M_TEMP);
7192 }
7193 
7194 /*
7195  * Initialize everything that needs to be initialized for a vnet.
7196  */
7197 static void
nfsrv_vnetinit(const void * unused __unused)7198 nfsrv_vnetinit(const void *unused __unused)
7199 {
7200 
7201 	nfsd_mntinit();
7202 }
7203 VNET_SYSINIT(nfsrv_vnetinit, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7204     nfsrv_vnetinit, NULL);
7205 
7206 /*
7207  * Clean up everything that is in a vnet and needs to be
7208  * done when the jail is destroyed or the module unloaded.
7209  */
7210 static void
nfsrv_cleanup(const void * unused __unused)7211 nfsrv_cleanup(const void *unused __unused)
7212 {
7213 	int i;
7214 
7215 	NFSD_LOCK();
7216 	if (!NFSD_VNET(nfsrv_mntinited)) {
7217 		NFSD_UNLOCK();
7218 		return;
7219 	}
7220 	NFSD_VNET(nfsrv_mntinited) = false;
7221 	NFSD_UNLOCK();
7222 
7223 	/* Clean out all NFSv4 state. */
7224 	nfsrv_throwawayallstate(curthread);
7225 
7226 	/* Clean the NFS server reply cache */
7227 	nfsrvd_cleancache();
7228 
7229 	/* Clean out v4root exports. */
7230 	if (NFSD_VNET(nfsv4root_mnt)->mnt_export != NULL) {
7231 		vfs_free_addrlist(NFSD_VNET(nfsv4root_mnt)->mnt_export);
7232 		free(NFSD_VNET(nfsv4root_mnt)->mnt_export, M_MOUNT);
7233 		NFSD_VNET(nfsv4root_mnt)->mnt_export = NULL;
7234 	}
7235 
7236 	/* Free up the krpc server pool. */
7237 	if (NFSD_VNET(nfsrvd_pool) != NULL)
7238 		svcpool_destroy(NFSD_VNET(nfsrvd_pool));
7239 
7240 	/* and get rid of the locks */
7241 	for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) {
7242 		mtx_destroy(&NFSD_VNET(nfsrchash_table)[i].mtx);
7243 		mtx_destroy(&NFSD_VNET(nfsrcahash_table)[i].mtx);
7244 	}
7245 	mtx_destroy(&NFSD_VNET(nfsv4root_mnt)->mnt_mtx);
7246 	for (i = 0; i < nfsrv_sessionhashsize; i++)
7247 		mtx_destroy(&NFSD_VNET(nfssessionhash)[i].mtx);
7248 	lockdestroy(&NFSD_VNET(nfsv4root_mnt)->mnt_explock);
7249 	free(NFSD_VNET(nfsrvudphashtbl), M_NFSRVCACHE);
7250 	free(NFSD_VNET(nfsrchash_table), M_NFSRVCACHE);
7251 	free(NFSD_VNET(nfsrcahash_table), M_NFSRVCACHE);
7252 	free(NFSD_VNET(nfsclienthash), M_NFSDCLIENT);
7253 	free(NFSD_VNET(nfslockhash), M_NFSDLOCKFILE);
7254 	free(NFSD_VNET(nfssessionhash), M_NFSDSESSION);
7255 	free(NFSD_VNET(nfsv4root_mnt), M_TEMP);
7256 	NFSD_VNET(nfsv4root_mnt) = NULL;
7257 }
7258 VNET_SYSUNINIT(nfsrv_cleanup, SI_SUB_VNET_DONE, SI_ORDER_ANY,
7259     nfsrv_cleanup, NULL);
7260 
7261 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *);
7262 
7263 /*
7264  * Called once to initialize data structures...
7265  */
7266 static int
nfsd_modevent(module_t mod,int type,void * data)7267 nfsd_modevent(module_t mod, int type, void *data)
7268 {
7269 	int error = 0, i;
7270 	static int loaded = 0;
7271 
7272 	switch (type) {
7273 	case MOD_LOAD:
7274 		if (loaded)
7275 			goto out;
7276 		newnfs_portinit();
7277 		mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF);
7278 		mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF);
7279 		mtx_init(&nfsrv_dontlistlock_mtx, "nfs4dnl", NULL, MTX_DEF);
7280 		mtx_init(&nfsrv_recalllock_mtx, "nfs4rec", NULL, MTX_DEF);
7281 #ifdef VV_DISABLEDELEG
7282 		vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation;
7283 		vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation;
7284 #endif
7285 		nfsd_call_nfsd = nfssvc_nfsd;
7286 		loaded = 1;
7287 		break;
7288 
7289 	case MOD_UNLOAD:
7290 		if (newnfs_numnfsd != 0) {
7291 			error = EBUSY;
7292 			break;
7293 		}
7294 
7295 #ifdef VV_DISABLEDELEG
7296 		vn_deleg_ops.vndeleg_recall = NULL;
7297 		vn_deleg_ops.vndeleg_disable = NULL;
7298 #endif
7299 		nfsd_call_nfsd = NULL;
7300 		mtx_destroy(&nfsrc_udpmtx);
7301 		mtx_destroy(&nfs_v4root_mutex);
7302 		mtx_destroy(&nfsrv_dontlistlock_mtx);
7303 		mtx_destroy(&nfsrv_recalllock_mtx);
7304 		if (nfslayouthash != NULL) {
7305 			for (i = 0; i < nfsrv_layouthashsize; i++)
7306 				mtx_destroy(&nfslayouthash[i].mtx);
7307 			free(nfslayouthash, M_NFSDSESSION);
7308 		}
7309 		loaded = 0;
7310 		break;
7311 	default:
7312 		error = EOPNOTSUPP;
7313 		break;
7314 	}
7315 
7316 out:
7317 	NFSEXITCODE(error);
7318 	return (error);
7319 }
7320 static moduledata_t nfsd_mod = {
7321 	"nfsd",
7322 	nfsd_modevent,
7323 	NULL,
7324 };
7325 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY);
7326 
7327 /* So that loader and kldload(2) can find us, wherever we are.. */
7328 MODULE_VERSION(nfsd, 1);
7329 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1);
7330 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1);
7331 MODULE_DEPEND(nfsd, krpc, 1, 1, 1);
7332 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1);
7333