xref: /openbsd/sys/nfs/nfsm_subs.h (revision df0421f7)
1 /*	$OpenBSD: nfsm_subs.h,v 1.49 2024/09/11 12:22:34 claudio 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_mrep = NULL;
76 		*infop->nmi_errorp = error;
77 		return NULL;
78 	} else {
79 		return ret;
80 	}
81 }
82 
83 #define nfsm_rndup(a)	(((a)+3)&(~0x3))
84 
85 static inline int
nfsm_adv(struct nfsm_info * infop,int s)86 nfsm_adv(struct nfsm_info *infop, int s)
87 {
88 	int avail, error;
89 
90 	avail = mtod(infop->nmi_md, caddr_t) + infop->nmi_md->m_len -
91 	    infop->nmi_dpos;
92 	if (avail >= s) {
93 		infop->nmi_dpos += s;
94 		return 0;
95 	}
96 	error = nfs_adv(&infop->nmi_md, &infop->nmi_dpos, s, avail);
97 	if (error != 0) {
98 		m_freem(infop->nmi_mrep);
99 		infop->nmi_mrep = NULL;
100 		*infop->nmi_errorp = error;
101 		return error;
102 	}
103 	return 0;
104 }
105 
106 static inline int
nfsm_postop_attr(struct nfsm_info * infop,struct vnode ** vpp,int * attrflagp)107 nfsm_postop_attr(struct nfsm_info *infop, struct vnode **vpp, int *attrflagp)
108 {
109 	uint32_t *tl;
110 	struct vnode *ttvp;
111 	int attrflag, error;
112 
113 	if (infop->nmi_mrep == NULL)
114 		return 0;
115 
116 	ttvp = *vpp;
117 	tl = (uint32_t *)nfsm_dissect(infop, NFSX_UNSIGNED);
118 	if (tl == NULL)
119 		return 1;	/* anything nonzero */
120 	attrflag = fxdr_unsigned(int, *tl);
121 	if (attrflag != 0) {
122 		error = nfs_loadattrcache(&ttvp, &infop->nmi_md,
123 		    &infop->nmi_dpos, NULL);
124 		if (error != 0) {
125 			m_freem(infop->nmi_mrep);
126 			infop->nmi_mrep = NULL;
127 			*infop->nmi_errorp = error;
128 			return error;
129 		}
130 		*vpp = ttvp;
131 	}
132 	*attrflagp = attrflag;
133 	return 0;
134 }
135 
136 static inline int
nfsm_strsiz(struct nfsm_info * infop,int * lenp,int maxlen)137 nfsm_strsiz(struct nfsm_info *infop, int *lenp, int maxlen)
138 {
139 	uint32_t *tl = (uint32_t *)nfsm_dissect(infop, NFSX_UNSIGNED);
140 	int len;
141 	if (tl == NULL)
142 		return 1;
143 	len = fxdr_unsigned(int32_t, *tl);
144 	if (len < 0 || len > maxlen) {
145 		m_freem(infop->nmi_mrep);
146 		infop->nmi_mrep = NULL;
147 		*infop->nmi_errorp = EBADRPC;
148 		return 1;
149 	}
150 	*lenp = len;
151 	return 0;
152 }
153 
154 static inline int
nfsm_mtouio(struct nfsm_info * infop,struct uio * uiop,int len)155 nfsm_mtouio(struct nfsm_info *infop, struct uio *uiop, int len)
156 {
157 	int error;
158 
159 	if (len <= 0)
160 		return 0;
161 
162 	error = nfsm_mbuftouio(&infop->nmi_md, uiop, len, &infop->nmi_dpos);
163 	if (error != 0) {
164 		m_freem(infop->nmi_mrep);
165 		infop->nmi_mrep = NULL;
166 		*infop->nmi_errorp = error;
167 		return error;
168 	}
169 	return 0;
170 }
171 
172 static inline int
nfsm_strtom(struct nfsm_info * infop,char * str,size_t len,size_t maxlen)173 nfsm_strtom(struct nfsm_info *infop, char *str, size_t len, size_t maxlen)
174 {
175 	if (len > maxlen) {
176 		m_freem(infop->nmi_mreq);
177 		infop->nmi_mreq = NULL;
178 		*infop->nmi_errorp = ENAMETOOLONG;
179 		return 1;
180 	}
181 	nfsm_strtombuf(&infop->nmi_mb, str, len);
182 	return 0;
183 }
184 
185 #endif
186