xref: /freebsd/sys/fs/nfsclient/nfs_clcomsubs.c (revision 42249ef2)
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 #ifndef APPLEKEXT
45 #include <fs/nfs/nfsport.h>
46 
47 extern struct nfsstatsv1 nfsstatsv1;
48 extern struct nfsv4_opflag nfsv4_opflag[NFSV41_NOPS];
49 extern int ncl_mbuf_mlen;
50 extern enum vtype newnv2tov_type[8];
51 extern enum vtype nv34tov_type[8];
52 NFSCLSTATEMUTEX;
53 #endif	/* !APPLEKEXT */
54 
55 static nfsuint64 nfs_nullcookie = {{ 0, 0 }};
56 
57 /*
58  * copies a uio scatter/gather list to an mbuf chain.
59  * NOTE: can ony handle iovcnt == 1
60  */
61 APPLESTATIC void
62 nfsm_uiombuf(struct nfsrv_descript *nd, struct uio *uiop, int siz)
63 {
64 	char *uiocp;
65 	struct mbuf *mp, *mp2;
66 	int xfer, left, mlen;
67 	int uiosiz, clflg, rem;
68 	char *cp, *tcp;
69 
70 	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
71 
72 	if (siz > ncl_mbuf_mlen)	/* or should it >= MCLBYTES ?? */
73 		clflg = 1;
74 	else
75 		clflg = 0;
76 	rem = NFSM_RNDUP(siz) - siz;
77 	mp = mp2 = nd->nd_mb;
78 	while (siz > 0) {
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 			mlen = M_TRAILINGSPACE(mp);
86 			if (mlen == 0) {
87 				if (clflg)
88 					NFSMCLGET(mp, M_WAITOK);
89 				else
90 					NFSMGET(mp);
91 				mbuf_setlen(mp, 0);
92 				mbuf_setnext(mp2, mp);
93 				mp2 = mp;
94 				mlen = M_TRAILINGSPACE(mp);
95 			}
96 			xfer = (left > mlen) ? mlen : left;
97 #ifdef notdef
98 			/* Not Yet.. */
99 			if (uiop->uio_iov->iov_op != NULL)
100 				(*(uiop->uio_iov->iov_op))
101 				(uiocp, NFSMTOD(mp, caddr_t) + mbuf_len(mp),
102 				    xfer);
103 			else
104 #endif
105 			if (uiop->uio_segflg == UIO_SYSSPACE)
106 			    NFSBCOPY(uiocp, NFSMTOD(mp, caddr_t) + mbuf_len(mp),
107 				xfer);
108 			else
109 			    copyin(CAST_USER_ADDR_T(uiocp), NFSMTOD(mp, caddr_t)
110 				+ mbuf_len(mp), xfer);
111 			mbuf_setlen(mp, mbuf_len(mp) + xfer);
112 			left -= xfer;
113 			uiocp += xfer;
114 			uiop->uio_offset += xfer;
115 			uiop->uio_resid -= xfer;
116 		}
117 		tcp = (char *)uiop->uio_iov->iov_base;
118 		tcp += uiosiz;
119 		uiop->uio_iov->iov_base = (void *)tcp;
120 		uiop->uio_iov->iov_len -= uiosiz;
121 		siz -= uiosiz;
122 	}
123 	if (rem > 0) {
124 		if (rem > M_TRAILINGSPACE(mp)) {
125 			NFSMGET(mp);
126 			mbuf_setlen(mp, 0);
127 			mbuf_setnext(mp2, mp);
128 		}
129 		cp = NFSMTOD(mp, caddr_t) + mbuf_len(mp);
130 		for (left = 0; left < rem; left++)
131 			*cp++ = '\0';
132 		mbuf_setlen(mp, mbuf_len(mp) + rem);
133 		nd->nd_bpos = cp;
134 	} else
135 		nd->nd_bpos = NFSMTOD(mp, caddr_t) + mbuf_len(mp);
136 	nd->nd_mb = mp;
137 }
138 
139 /*
140  * copies a uio scatter/gather list to an mbuf chain.
141  * This version returns the mbuf list and does not use "nd".
142  * NOTE: can ony handle iovcnt == 1
143  */
144 struct mbuf *
145 nfsm_uiombuflist(struct uio *uiop, int siz, struct mbuf **mbp, char **cpp)
146 {
147 	char *uiocp;
148 	struct mbuf *mp, *mp2, *firstmp;
149 	int xfer, left, mlen;
150 	int uiosiz, clflg;
151 	char *tcp;
152 
153 	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
154 
155 	if (siz > ncl_mbuf_mlen)	/* or should it >= MCLBYTES ?? */
156 		clflg = 1;
157 	else
158 		clflg = 0;
159 	if (clflg != 0)
160 		NFSMCLGET(mp, M_WAITOK);
161 	else
162 		NFSMGET(mp);
163 	mbuf_setlen(mp, 0);
164 	firstmp = mp2 = mp;
165 	while (siz > 0) {
166 		left = uiop->uio_iov->iov_len;
167 		uiocp = uiop->uio_iov->iov_base;
168 		if (left > siz)
169 			left = siz;
170 		uiosiz = left;
171 		while (left > 0) {
172 			mlen = M_TRAILINGSPACE(mp);
173 			if (mlen == 0) {
174 				if (clflg)
175 					NFSMCLGET(mp, M_WAITOK);
176 				else
177 					NFSMGET(mp);
178 				mbuf_setlen(mp, 0);
179 				mbuf_setnext(mp2, mp);
180 				mp2 = mp;
181 				mlen = M_TRAILINGSPACE(mp);
182 			}
183 			xfer = (left > mlen) ? mlen : left;
184 			if (uiop->uio_segflg == UIO_SYSSPACE)
185 				NFSBCOPY(uiocp, NFSMTOD(mp, caddr_t) +
186 				    mbuf_len(mp), xfer);
187 			else
188 				copyin(uiocp, NFSMTOD(mp, caddr_t) +
189 				    mbuf_len(mp), xfer);
190 			mbuf_setlen(mp, mbuf_len(mp) + xfer);
191 			left -= xfer;
192 			uiocp += xfer;
193 			uiop->uio_offset += xfer;
194 			uiop->uio_resid -= xfer;
195 		}
196 		tcp = (char *)uiop->uio_iov->iov_base;
197 		tcp += uiosiz;
198 		uiop->uio_iov->iov_base = (void *)tcp;
199 		uiop->uio_iov->iov_len -= uiosiz;
200 		siz -= uiosiz;
201 	}
202 	if (cpp != NULL)
203 		*cpp = NFSMTOD(mp, caddr_t) + mbuf_len(mp);
204 	if (mbp != NULL)
205 		*mbp = mp;
206 	return (firstmp);
207 }
208 
209 /*
210  * Load vnode attributes from the xdr file attributes.
211  * Returns EBADRPC if they can't be parsed, 0 otherwise.
212  */
213 APPLESTATIC int
214 nfsm_loadattr(struct nfsrv_descript *nd, struct nfsvattr *nap)
215 {
216 	struct nfs_fattr *fp;
217 	int error = 0;
218 
219 	if (nd->nd_flag & ND_NFSV4) {
220 		error = nfsv4_loadattr(nd, NULL, nap, NULL, NULL, 0, NULL,
221 		    NULL, NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL);
222 	} else if (nd->nd_flag & ND_NFSV3) {
223 		NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V3FATTR);
224 		nap->na_type = nfsv34tov_type(fp->fa_type);
225 		nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
226 		nap->na_rdev = NFSMAKEDEV(
227 		    fxdr_unsigned(int, fp->fa3_rdev.specdata1),
228 		    fxdr_unsigned(int, fp->fa3_rdev.specdata2));
229 		nap->na_nlink = fxdr_unsigned(uint32_t, fp->fa_nlink);
230 		nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
231 		nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
232 		nap->na_size = fxdr_hyper(&fp->fa3_size);
233 		nap->na_blocksize = NFS_FABLKSIZE;
234 		nap->na_bytes = fxdr_hyper(&fp->fa3_used);
235 		nap->na_fileid = fxdr_hyper(&fp->fa3_fileid);
236 		fxdr_nfsv3time(&fp->fa3_atime, &nap->na_atime);
237 		fxdr_nfsv3time(&fp->fa3_ctime, &nap->na_ctime);
238 		fxdr_nfsv3time(&fp->fa3_mtime, &nap->na_mtime);
239 		nap->na_flags = 0;
240 		nap->na_filerev = 0;
241 	} else {
242 		NFSM_DISSECT(fp, struct nfs_fattr *, NFSX_V2FATTR);
243 		nap->na_type = nfsv2tov_type(fp->fa_type);
244 		nap->na_mode = fxdr_unsigned(u_short, fp->fa_mode);
245 		if (nap->na_type == VNON || nap->na_type == VREG)
246 			nap->na_type = IFTOVT(nap->na_mode);
247 		nap->na_rdev = fxdr_unsigned(dev_t, fp->fa2_rdev);
248 
249 		/*
250 		 * Really ugly NFSv2 kludge.
251 		 */
252 		if (nap->na_type == VCHR && nap->na_rdev == ((dev_t)-1))
253 			nap->na_type = VFIFO;
254 		nap->na_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
255 		nap->na_uid = fxdr_unsigned(uid_t, fp->fa_uid);
256 		nap->na_gid = fxdr_unsigned(gid_t, fp->fa_gid);
257 		nap->na_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
258 		nap->na_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
259 		nap->na_bytes =
260 		    (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks) *
261 		    NFS_FABLKSIZE;
262 		nap->na_fileid = fxdr_unsigned(uint64_t, fp->fa2_fileid);
263 		fxdr_nfsv2time(&fp->fa2_atime, &nap->na_atime);
264 		fxdr_nfsv2time(&fp->fa2_mtime, &nap->na_mtime);
265 		nap->na_flags = 0;
266 		nap->na_ctime.tv_sec = fxdr_unsigned(u_int32_t,
267 		    fp->fa2_ctime.nfsv2_sec);
268 		nap->na_ctime.tv_nsec = 0;
269 		nap->na_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
270 		nap->na_filerev = 0;
271 	}
272 nfsmout:
273 	return (error);
274 }
275 
276 /*
277  * This function finds the directory cookie that corresponds to the
278  * logical byte offset given.
279  */
280 APPLESTATIC nfsuint64 *
281 nfscl_getcookie(struct nfsnode *np, off_t off, int add)
282 {
283 	struct nfsdmap *dp, *dp2;
284 	int pos;
285 
286 	pos = off / NFS_DIRBLKSIZ;
287 	if (pos == 0) {
288 		KASSERT(!add, ("nfs getcookie add at 0"));
289 		return (&nfs_nullcookie);
290 	}
291 	pos--;
292 	dp = LIST_FIRST(&np->n_cookies);
293 	if (!dp) {
294 		if (add) {
295 			dp = malloc(sizeof (struct nfsdmap),
296 				M_NFSDIROFF, M_WAITOK);
297 			dp->ndm_eocookie = 0;
298 			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
299 		} else
300 			return (NULL);
301 	}
302 	while (pos >= NFSNUMCOOKIES) {
303 		pos -= NFSNUMCOOKIES;
304 		if (LIST_NEXT(dp, ndm_list) != NULL) {
305 			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
306 				pos >= dp->ndm_eocookie)
307 				return (NULL);
308 			dp = LIST_NEXT(dp, ndm_list);
309 		} else if (add) {
310 			dp2 = malloc(sizeof (struct nfsdmap),
311 				M_NFSDIROFF, M_WAITOK);
312 			dp2->ndm_eocookie = 0;
313 			LIST_INSERT_AFTER(dp, dp2, ndm_list);
314 			dp = dp2;
315 		} else
316 			return (NULL);
317 	}
318 	if (pos >= dp->ndm_eocookie) {
319 		if (add)
320 			dp->ndm_eocookie = pos + 1;
321 		else
322 			return (NULL);
323 	}
324 	return (&dp->ndm_cookies[pos]);
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 APPLESTATIC 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 APPLESTATIC 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 APPLESTATIC 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 APPLESTATIC void
419 nfscl_lockunlock(struct nfsv4lock *lckp)
420 {
421 
422 	nfsv4_unlock(lckp, 0);
423 }
424 
425 /*
426  * Called to derefernce a lock on a stateid (delegation or open owner).
427  */
428 APPLESTATIC 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 
441