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/cdefs.h>
37/*
38 * These functions support the macros and help fiddle mbuf chains for
39 * the nfs op functions. They do things like create the rpc header and
40 * copy data between mbuf chains and uio lists.
41 */
42#include <fs/nfs/nfsport.h>
43
44extern struct nfsstatsv1 nfsstatsv1;
45extern int ncl_mbuf_mlen;
46extern __enum_uint8(vtype) newnv2tov_type[8];
47extern __enum_uint8(vtype) nv34tov_type[8];
48NFSCLSTATEMUTEX;
49
50/*
51 * copies a uio scatter/gather list to an mbuf chain.
52 * NOTE: can only handle iovcnt == 1
53 */
54int
55nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
56{
57	char *uiocp;
58	struct mbuf *mp, *mp2;
59	int error, xfer, left, mlen;
60	int uiosiz, clflg, rem;
61	char *mcp, *tcp;
62
63	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
64
65	if (siz > ncl_mbuf_mlen)	/* or should it >= MCLBYTES ?? */
66		clflg = 1;
67	else
68		clflg = 0;
69	rem = NFSM_RNDUP(siz) - siz;
70	mp = mp2 = nd->nd_mb;
71	mcp = nd->nd_bpos;
72	while (siz > 0) {
73		KASSERT((nd->nd_flag & ND_EXTPG) != 0 || mcp ==
74		    mtod(mp, char *) + mp->m_len, ("nfsm_uiombuf: mcp wrong"));
75		left = uiop->uio_iov->iov_len;
76		uiocp = uiop->uio_iov->iov_base;
77		if (left > siz)
78			left = siz;
79		uiosiz = left;
80		while (left > 0) {
81			if ((nd->nd_flag & ND_EXTPG) != 0)
82				mlen = nd->nd_bextpgsiz;
83			else
84				mlen = M_TRAILINGSPACE(mp);
85			if (mlen == 0) {
86				if ((nd->nd_flag & ND_EXTPG) != 0) {
87					mp = nfsm_add_ext_pgs(mp,
88					    nd->nd_maxextsiz, &nd->nd_bextpg);
89					mcp = (char *)(void *)PHYS_TO_DMAP(
90					  mp->m_epg_pa[nd->nd_bextpg]);
91					nd->nd_bextpgsiz = mlen = PAGE_SIZE;
92				} else {
93					if (clflg)
94						NFSMCLGET(mp, M_WAITOK);
95					else
96						NFSMGET(mp);
97					mp->m_len = 0;
98					mlen = M_TRAILINGSPACE(mp);
99					mcp = mtod(mp, char *);
100					mp2->m_next = mp;
101					mp2 = mp;
102				}
103			}
104			xfer = (left > mlen) ? mlen : left;
105			if (uiop->uio_segflg == UIO_SYSSPACE)
106				NFSBCOPY(uiocp, mcp, xfer);
107			else {
108				error = copyin(uiocp, mcp, xfer);
109				if (error != 0)
110					return (error);
111			}
112			mp->m_len += xfer;
113			left -= xfer;
114			uiocp += xfer;
115			mcp += xfer;
116			if ((nd->nd_flag & ND_EXTPG) != 0) {
117				nd->nd_bextpgsiz -= xfer;
118				mp->m_epg_last_len += xfer;
119			}
120			uiop->uio_offset += xfer;
121			uiop->uio_resid -= xfer;
122		}
123		tcp = (char *)uiop->uio_iov->iov_base;
124		tcp += uiosiz;
125		uiop->uio_iov->iov_base = (void *)tcp;
126		uiop->uio_iov->iov_len -= uiosiz;
127		siz -= uiosiz;
128	}
129	if (rem > 0) {
130		if ((nd->nd_flag & ND_EXTPG) == 0 && rem >
131		    M_TRAILINGSPACE(mp)) {
132			NFSMGET(mp);
133			mp->m_len = 0;
134			mp2->m_next = mp;
135			mcp = mtod(mp, char *);
136		} else if ((nd->nd_flag & ND_EXTPG) != 0 && rem >
137		    nd->nd_bextpgsiz) {
138			mp = nfsm_add_ext_pgs(mp, nd->nd_maxextsiz,
139			    &nd->nd_bextpg);
140			mcp = (char *)(void *)
141			    PHYS_TO_DMAP(mp->m_epg_pa[nd->nd_bextpg]);
142			nd->nd_bextpgsiz = PAGE_SIZE;
143		}
144		for (left = 0; left < rem; left++)
145			*mcp++ = '\0';
146		mp->m_len += rem;
147		if ((nd->nd_flag & ND_EXTPG) != 0) {
148			nd->nd_bextpgsiz -= rem;
149			mp->m_epg_last_len += rem;
150		}
151	}
152	nd->nd_bpos = mcp;
153	nd->nd_mb = mp;
154	return (0);
155}
156
157/*
158 * copies a uio scatter/gather list to an mbuf chain.
159 * This version returns the mbuf list and does not use "nd".
160 * NOTE: can only handle iovcnt == 1
161 */
162struct mbuf *
163nfsm_uiombuflist(struct uio *uiop, int siz, u_int maxext)
164{
165	char *uiocp;
166	struct mbuf *mp, *mp2, *firstmp;
167	int error, extpg, extpgsiz = 0, i, left, mlen, rem, xfer;
168	int uiosiz, clflg;
169	char *mcp, *tcp;
170
171	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
172
173	if (maxext > 0) {
174		mp = mb_alloc_ext_plus_pages(PAGE_SIZE, M_WAITOK);
175		mcp = (char *)(void *)PHYS_TO_DMAP(mp->m_epg_pa[0]);
176		extpg = 0;
177		extpgsiz = PAGE_SIZE;
178	} else {
179		if (siz > ncl_mbuf_mlen) /* or should it >= MCLBYTES ?? */
180			clflg = 1;
181		else
182			clflg = 0;
183		if (clflg != 0)
184			NFSMCLGET(mp, M_WAITOK);
185		else
186			NFSMGET(mp);
187		mcp = mtod(mp, char *);
188	}
189	mp->m_len = 0;
190	firstmp = mp2 = mp;
191	rem = NFSM_RNDUP(siz) - siz;
192	while (siz > 0) {
193		left = uiop->uio_iov->iov_len;
194		uiocp = uiop->uio_iov->iov_base;
195		if (left > siz)
196			left = siz;
197		uiosiz = left;
198		while (left > 0) {
199			if (maxext > 0)
200				mlen = extpgsiz;
201			else
202				mlen = M_TRAILINGSPACE(mp);
203			if (mlen == 0) {
204				if (maxext > 0) {
205					mp = nfsm_add_ext_pgs(mp, maxext,
206					    &extpg);
207					mlen = extpgsiz = PAGE_SIZE;
208					mcp = (char *)(void *)PHYS_TO_DMAP(
209					    mp->m_epg_pa[extpg]);
210				} else {
211					if (clflg)
212						NFSMCLGET(mp, M_WAITOK);
213					else
214						NFSMGET(mp);
215					mcp = mtod(mp, char *);
216					mlen = M_TRAILINGSPACE(mp);
217					mp->m_len = 0;
218					mp2->m_next = mp;
219					mp2 = mp;
220				}
221			}
222			xfer = (left > mlen) ? mlen : left;
223			if (uiop->uio_segflg == UIO_SYSSPACE)
224				NFSBCOPY(uiocp, mcp, xfer);
225			else {
226				error = copyin(uiocp, mcp, xfer);
227				if (error != 0) {
228					m_freem(firstmp);
229					return (NULL);
230				}
231			}
232			mp->m_len += xfer;
233			mcp += xfer;
234			if (maxext > 0) {
235				extpgsiz -= xfer;
236				mp->m_epg_last_len += xfer;
237			}
238			left -= xfer;
239			uiocp += xfer;
240			uiop->uio_offset += xfer;
241			uiop->uio_resid -= xfer;
242		}
243		tcp = (char *)uiop->uio_iov->iov_base;
244		tcp += uiosiz;
245		uiop->uio_iov->iov_base = (void *)tcp;
246		uiop->uio_iov->iov_len -= uiosiz;
247		siz -= uiosiz;
248	}
249	if (rem > 0) {
250		KASSERT((mp->m_flags & M_EXTPG) != 0 ||
251		    rem <= M_TRAILINGSPACE(mp),
252		    ("nfsm_uiombuflist: no space for padding"));
253		for (i = 0; i < rem; i++)
254			*mcp++ = '\0';
255		mp->m_len += rem;
256		if (maxext > 0)
257			mp->m_epg_last_len += rem;
258	}
259	return (firstmp);
260}
261
262/*
263 * Load vnode attributes from the xdr file attributes.
264 * Returns EBADRPC if they can't be parsed, 0 otherwise.
265 */
266int
267nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap)
268{
269	struct nfs_fattr *fp;
270	int error = 0;
271
272	if (nd->nd_flag & ND_NFSV4) {
273		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL,
274		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
275	} else if (nd->nd_flag & ND_NFSV3) {
276		NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR);
277		nap->na_type = nfsv34tov_type(fp->fa_type);
278		nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
279		nap->na_rdev = NFSMAKEDEV(
280		    fxdr_unsigned(int, fp->fa3_rdev.specdata1),
281		    fxdr_unsigned(int, fp->fa3_rdev.specdata2));
282		nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink);
283		nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
284		nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
285		nap->na_size = fxdr_hyper(&fp->fa3_size);
286		nap->na_blocksize = NFS_FABLKSIZE;
287		nap->na_bytes = fxdr_hyper(&fp->fa3_used);
288		nap->na_fileid = fxdr_hyper(&fp->fa3_fileid);
289		fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime);
290		fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime);
291		fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime);
292		nap->na_btime.tv_sec = -1;
293		nap->na_btime.tv_nsec = 0;
294		nap->na_flags = 0;
295		nap->na_gen = 0;
296		nap->na_filerev = 0;
297	} else {
298		NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR);
299		nap->na_type = nfsv2tov_type(fp->fa_type);
300		nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
301		if (nap->na_type == VNON || nap->na_type == VREG)
302			nap->na_type = IFTOVT(nap->na_mode);
303		nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev);
304
305		/*
306		 * Really ugly NFSv2 kludge.
307		 */
308		if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1))
309			nap->na_type = VFIFO;
310		nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
311		nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
312		nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
313		nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
314		nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
315		nap->na_bytes =
316		    (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
317		    NFS_FABLKSIZE;
318		nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid);
319		fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime);
320		fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime);
321		nap->na_flags = 0;
322		nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t,
323		    fp->fa2_ctime.nfsv2_sec);
324		nap->na_ctime.tv_nsec = 0;
325		nap->na_btime.tv_sec = -1;
326		nap->na_btime.tv_nsec = 0;
327		nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
328		nap->na_filerev = 0;
329	}
330nfsmout:
331	return (error);
332}
333
334/*
335 * Gets a file handle out of an nfs reply sent to the client and returns
336 * the file handle and the file's attributes.
337 * For V4, it assumes that Getfh and Getattr Op's results are here.
338 */
339int
340nfscl_mtofh(struct nfsrv_descript *nd, struct nfsfh **nfhpp,
341    struct nfsvattr *nap, int *attrflagp)
342{
343	u_int32_t *tl;
344	int error = 0, flag = 1;
345
346	*nfhpp = NULL;
347	*attrflagp = 0;
348	/*
349	 * First get the file handle and vnode.
350	 */
351	if (nd->nd_flag & ND_NFSV3) {
352		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
353		flag = fxdr_unsigned(int, *tl);
354	} else if (nd->nd_flag & ND_NFSV4) {
355		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
356		/* If the GetFH failed, clear flag. */
357		if (*++tl != 0) {
358			nd->nd_flag |= ND_NOMOREDATA;
359			flag = 0;
360			error = ENXIO;	/* Return ENXIO so *nfhpp isn't used. */
361		}
362	}
363	if (flag) {
364		error = nfsm_getfh(nd, nfhpp);
365		if (error)
366			return (error);
367	}
368
369	/*
370	 * Now, get the attributes.
371	 */
372	if (flag != 0 && (nd->nd_flag & ND_NFSV4) != 0) {
373		NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
374		if (*++tl != 0) {
375			nd->nd_flag |= ND_NOMOREDATA;
376			flag = 0;
377		}
378	} else if (nd->nd_flag & ND_NFSV3) {
379		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
380		if (flag) {
381			flag = fxdr_unsigned(int, *tl);
382		} else if (fxdr_unsigned(int, *tl)) {
383			error = nfsm_advance(nd, NFSX_V3FATTR, -1);
384			if (error)
385				return (error);
386		}
387	}
388	if (flag) {
389		error = nfsm_loadattr(nd, nap);
390		if (!error)
391			*attrflagp = 1;
392	}
393nfsmout:
394	return (error);
395}
396
397/*
398 * Initialize the owner/delegation sleep lock.
399 */
400void
401nfscl_lockinit(struct nfsv4lock *lckp)
402{
403
404	lckp->nfslock_usecnt = 0;
405	lckp->nfslock_lock = 0;
406}
407
408/*
409 * Get an exclusive lock. (Not needed for OpenBSD4, since there is only one
410 * thread for each posix process in the kernel.)
411 */
412void
413nfscl_lockexcl(struct nfsv4lock *lckp, void *mutex)
414{
415	int igotlock;
416
417	do {
418		igotlock = nfsv4_lock(lckp, 1, NULL, mutex, NULL);
419	} while (!igotlock);
420}
421
422/*
423 * Release an exclusive lock.
424 */
425void
426nfscl_lockunlock(struct nfsv4lock *lckp)
427{
428
429	nfsv4_unlock(lckp, 0);
430}
431
432/*
433 * Called to dereference a lock on a stateid (delegation or open owner).
434 */
435void
436nfscl_lockderef(struct nfsv4lock *lckp)
437{
438
439	NFSLOCKCLSTATE();
440	lckp->nfslock_usecnt--;
441	if (lckp->nfslock_usecnt == 0 && (lckp->nfslock_lock & NFSV4LOCK_WANTED)) {
442		lckp->nfslock_lock &= ~NFSV4LOCK_WANTED;
443		wakeup((caddr_t)lckp);
444	}
445	NFSUNLOCKCLSTATE();
446}
447