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