xref: /openbsd/sys/nfs/nfsm_subs.h (revision a43f2b81)
1 /*	$OpenBSD: nfsm_subs.h,v 1.48 2024/04/30 17:04:23 miod Exp $	*/
2 /*	$NetBSD: nfsm_subs.h,v 1.10 1996/03/20 21:59:56 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfsm_subs.h	8.2 (Berkeley) 3/30/95
36  */
37 
38 
39 #ifndef _NFS_NFSM_SUBS_H_
40 #define _NFS_NFSM_SUBS_H_
41 
42 struct nfsm_info {
43 	struct mbuf	*nmi_mreq;
44 	struct mbuf	*nmi_mrep;
45 
46 	struct proc	*nmi_procp;	/* XXX XXX XXX */
47 	struct ucred	*nmi_cred;	/* XXX XXX XXX */
48 
49 	/* Setting up / Tearing down. */
50 	struct mbuf	*nmi_md;
51 	struct mbuf	*nmi_mb;
52 	caddr_t		 nmi_dpos;
53 
54 	int		 nmi_v3;
55 
56 	int		*nmi_errorp;
57 };
58 
59 static inline void *
nfsm_dissect(struct nfsm_info * infop,int s)60 nfsm_dissect(struct nfsm_info *infop, int s)
61 {
62 	caddr_t ret;
63 	int avail, error;
64 
65 	avail = mtod(infop->nmi_md, caddr_t) + infop->nmi_md->m_len -
66 	    infop->nmi_dpos;
67 	if (avail >= s) {
68 		ret = infop->nmi_dpos;
69 		infop->nmi_dpos += s;
70 		return ret;
71 	}
72 	error = nfsm_disct(&infop->nmi_md, &infop->nmi_dpos, s, avail, &ret);
73 	if (error != 0) {
74 		m_freem(infop->nmi_mrep);
75 		*infop->nmi_errorp = error;
76 		return NULL;
77 	} else {
78 		return ret;
79 	}
80 }
81 
82 #define nfsm_rndup(a)	(((a)+3)&(~0x3))
83 
84 static inline int
nfsm_adv(struct nfsm_info * infop,int s)85 nfsm_adv(struct nfsm_info *infop, int s)
86 {
87 	int avail, error;
88 
89 	avail = mtod(infop->nmi_md, caddr_t) + infop->nmi_md->m_len -
90 	    infop->nmi_dpos;
91 	if (avail >= s) {
92 		infop->nmi_dpos += s;
93 		return 0;
94 	}
95 	error = nfs_adv(&infop->nmi_md, &infop->nmi_dpos, s, avail);
96 	if (error != 0) {
97 		m_freem(infop->nmi_mrep);
98 		*infop->nmi_errorp = error;
99 		return error;
100 	}
101 	return 0;
102 }
103 
104 static inline int
nfsm_postop_attr(struct nfsm_info * infop,struct vnode ** vpp,int * attrflagp)105 nfsm_postop_attr(struct nfsm_info *infop, struct vnode **vpp, int *attrflagp)
106 {
107 	uint32_t *tl;
108 	struct vnode *ttvp;
109 	int attrflag, error;
110 
111 	if (infop->nmi_mrep == NULL)
112 		return 0;
113 
114 	ttvp = *vpp;
115 	tl = (uint32_t *)nfsm_dissect(infop, NFSX_UNSIGNED);
116 	if (tl == NULL)
117 		return 1;	/* anything nonzero */
118 	attrflag = fxdr_unsigned(int, *tl);
119 	if (attrflag != 0) {
120 		error = nfs_loadattrcache(&ttvp, &infop->nmi_md,
121 		    &infop->nmi_dpos, NULL);
122 		if (error != 0) {
123 			m_freem(infop->nmi_mrep);
124 			*infop->nmi_errorp = error;
125 			return error;
126 		}
127 		*vpp = ttvp;
128 	}
129 	*attrflagp = attrflag;
130 	return 0;
131 }
132 
133 static inline int
nfsm_strsiz(struct nfsm_info * infop,int * lenp,int maxlen)134 nfsm_strsiz(struct nfsm_info *infop, int *lenp, int maxlen)
135 {
136 	uint32_t *tl = (uint32_t *)nfsm_dissect(infop, NFSX_UNSIGNED);
137 	int len;
138 	if (tl == NULL)
139 		return 1;
140 	len = fxdr_unsigned(int32_t, *tl);
141 	if (len < 0 || len > maxlen) {
142 		m_freem(infop->nmi_mrep);
143 		*infop->nmi_errorp = EBADRPC;
144 		return 1;
145 	}
146 	*lenp = len;
147 	return 0;
148 }
149 
150 static inline int
nfsm_mtouio(struct nfsm_info * infop,struct uio * uiop,int len)151 nfsm_mtouio(struct nfsm_info *infop, struct uio *uiop, int len)
152 {
153 	int error;
154 
155 	if (len <= 0)
156 		return 0;
157 
158 	error = nfsm_mbuftouio(&infop->nmi_md, uiop, len, &infop->nmi_dpos);
159 	if (error != 0) {
160 		m_freem(infop->nmi_mrep);
161 		*infop->nmi_errorp = error;
162 		return error;
163 	}
164 	return 0;
165 }
166 
167 static inline int
nfsm_strtom(struct nfsm_info * infop,char * str,size_t len,size_t maxlen)168 nfsm_strtom(struct nfsm_info *infop, char *str, size_t len, size_t maxlen)
169 {
170 	if (len > maxlen) {
171 		m_freem(infop->nmi_mreq);
172 		*infop->nmi_errorp = ENAMETOOLONG;
173 		return 1;
174 	}
175 	nfsm_strtombuf(&infop->nmi_mb, str, len);
176 	return 0;
177 }
178 
179 #endif
180