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