Print this page
11227 smb code needs smatch fixes
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/smbsrv/smb_alloc.c
+++ new/usr/src/uts/common/fs/smbsrv/smb_alloc.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24
25 +/*
26 + * Copyright 2019 Joyent, Inc.
27 + */
28 +
25 29 #include <sys/types.h>
26 30 #include <sys/sunddi.h>
27 31 #include <sys/kmem.h>
28 32 #include <sys/sysmacros.h>
29 33 #include <smbsrv/smb_kproto.h>
30 34 #include <smbsrv/alloc.h>
31 35
32 36 #define SMB_SMH_MAGIC 0x534D485F /* 'SMH_' */
33 37 #define SMB_SMH_VALID(_smh_) ASSERT((_smh_)->smh_magic == SMB_SMH_MAGIC)
34 38 #define SMB_MEM2SMH(_mem_) ((smb_mem_header_t *)(_mem_) - 1)
35 39
36 40 typedef struct smb_mem_header {
37 41 uint32_t smh_magic;
38 42 size_t smh_size;
39 43 smb_request_t *smh_sr;
40 44 list_node_t smh_lnd;
41 45 } smb_mem_header_t;
42 46
43 47 static void *smb_alloc(smb_request_t *, size_t, boolean_t);
44 48 static void smb_free(smb_request_t *, void *, boolean_t);
45 49 static void *smb_realloc(smb_request_t *, void *, size_t, boolean_t);
46 50
47 51 /*
48 52 * Allocate memory.
49 53 */
50 54 void *
51 55 smb_mem_alloc(size_t size)
52 56 {
53 57 return (smb_alloc(NULL, size, B_FALSE));
54 58 }
55 59
56 60 /*
57 61 * Allocate memory and zero it out.
58 62 */
59 63 void *
60 64 smb_mem_zalloc(size_t size)
61 65 {
62 66 return (smb_alloc(NULL, size, B_TRUE));
63 67 }
64 68
65 69 /*
66 70 * Allocate or resize memory previously allocated.
67 71 *
68 72 * The address passed in MUST be considered invalid when this function returns.
69 73 */
70 74 void *
71 75 smb_mem_realloc(void *ptr, size_t size)
72 76 {
73 77 return (smb_realloc(NULL, ptr, size, B_FALSE));
74 78 }
75 79
76 80 /*
77 81 * Allocate or resize memory previously allocated. If the new size is greater
78 82 * than the current size, the extra space is zeroed out. If the new size is less
79 83 * then the current size the space truncated is zeroed out.
80 84 *
81 85 * The address passed in MUST be considered invalid when this function returns.
82 86 */
83 87 void *
84 88 smb_mem_rezalloc(void *ptr, size_t size)
85 89 {
86 90 return (smb_realloc(NULL, ptr, size, B_TRUE));
87 91 }
88 92
89 93 /*
90 94 * Free memory previously allocated with smb_malloc(), smb_zalloc(),
91 95 * smb_remalloc() or smb_rezalloc().
92 96 */
93 97 void
94 98 smb_mem_free(void *ptr)
95 99 {
96 100 smb_free(NULL, ptr, B_FALSE);
97 101 }
98 102
99 103 /*
100 104 * Free memory previously allocated with smb_mem_malloc(), smb_mem_zalloc(),
101 105 * smb_mem_remalloc() or smb_mem_rezalloc() or smb_mem_strdup(). The memory will
102 106 * be zeroed out before being actually freed.
103 107 */
104 108 void
105 109 smb_mem_zfree(void *ptr)
106 110 {
107 111 smb_free(NULL, ptr, B_TRUE);
108 112 }
109 113
110 114 /*
111 115 * Duplicate a string.
112 116 */
113 117 char *
114 118 smb_mem_strdup(const char *ptr)
115 119 {
116 120 char *p;
117 121 size_t size;
118 122
119 123 size = strlen(ptr) + 1;
120 124 p = smb_alloc(NULL, size, B_FALSE);
121 125 bcopy(ptr, p, size);
122 126 return (p);
123 127 }
124 128
125 129 /*
126 130 * Initialize the list for request-specific temporary storage.
127 131 */
128 132 void
129 133 smb_srm_init(smb_request_t *sr)
130 134 {
131 135 list_create(&sr->sr_storage, sizeof (smb_mem_header_t),
132 136 offsetof(smb_mem_header_t, smh_lnd));
133 137 }
134 138
135 139 /*
136 140 * Free everything on the request-specific temporary storage list and destroy
137 141 * the list.
138 142 */
139 143 void
140 144 smb_srm_fini(smb_request_t *sr)
141 145 {
142 146 smb_mem_header_t *smh;
143 147
144 148 while ((smh = list_head(&sr->sr_storage)) != NULL)
145 149 smb_free(sr, ++smh, B_FALSE);
146 150 list_destroy(&sr->sr_storage);
147 151 }
148 152
149 153 /*
150 154 * Allocate memory and associate it with the specified request.
151 155 * Memory allocated here can only be used for the duration of this request; it
152 156 * will be freed automatically on completion of the request.
153 157 */
154 158 void *
155 159 smb_srm_alloc(smb_request_t *sr, size_t size)
156 160 {
157 161 return (smb_alloc(sr, size, B_FALSE));
158 162 }
159 163
160 164 /*
161 165 * Allocate memory, zero it out and associate it with the specified request.
162 166 * Memory allocated here can only be used for the duration of this request; it
163 167 * will be freed automatically on completion of the request.
164 168 */
165 169 void *
166 170 smb_srm_zalloc(smb_request_t *sr, size_t size)
167 171 {
168 172 return (smb_alloc(sr, size, B_TRUE));
169 173 }
170 174
171 175 /*
172 176 * Allocate or resize memory previously allocated for the specified request.
173 177 *
174 178 * The address passed in MUST be considered invalid when this function returns.
175 179 */
176 180 void *
177 181 smb_srm_realloc(smb_request_t *sr, void *p, size_t size)
178 182 {
179 183 return (smb_realloc(sr, p, size, B_FALSE));
180 184 }
181 185
182 186 /*
183 187 * Allocate or resize memory previously allocated for the specified request. If
184 188 * the new size is greater than the current size, the extra space is zeroed out.
185 189 * If the new size is less then the current size the space truncated is zeroed
186 190 * out.
187 191 *
188 192 * The address passed in MUST be considered invalid when this function returns.
189 193 */
190 194 void *
191 195 smb_srm_rezalloc(smb_request_t *sr, void *p, size_t size)
192 196 {
193 197 return (smb_realloc(sr, p, size, B_TRUE));
194 198 }
195 199
196 200 char *
197 201 smb_srm_strdup(smb_request_t *sr, const char *s)
198 202 {
199 203 char *p;
200 204 size_t size;
201 205
202 206 size = strlen(s) + 1;
203 207 p = smb_srm_alloc(sr, size);
204 208 bcopy(s, p, size);
205 209 return (p);
206 210 }
207 211
208 212 /*
209 213 * Allocate memory.
210 214 *
211 215 * sr If not NULL, request the memory allocated must be associated with.
212 216 *
213 217 * size Size of the meory to allocate.
214 218 *
215 219 * zero If true the memory allocated will be zeroed out.
216 220 */
217 221 static void *
218 222 smb_alloc(smb_request_t *sr, size_t size, boolean_t zero)
219 223 {
220 224 smb_mem_header_t *smh;
221 225
222 226 if (zero) {
223 227 smh = kmem_zalloc(size + sizeof (smb_mem_header_t), KM_SLEEP);
224 228 } else {
225 229 smh = kmem_alloc(size + sizeof (smb_mem_header_t), KM_SLEEP);
226 230 smh->smh_sr = NULL;
227 231 bzero(&smh->smh_lnd, sizeof (smh->smh_lnd));
228 232 }
229 233 smh->smh_sr = sr;
230 234 smh->smh_size = size;
231 235 smh->smh_magic = SMB_SMH_MAGIC;
232 236 if (sr != NULL) {
233 237 SMB_REQ_VALID(sr);
234 238 list_insert_tail(&sr->sr_storage, smh);
235 239 }
236 240 return (++smh);
237 241 }
238 242
239 243 /*
240 244 * Free memory.
241 245 *
242 246 * sr If not NULL, request the memory to free is associated with.
243 247 *
244 248 * ptr Memory address
245 249 *
246 250 * zero If true the memory is zeroed out before being freed.
247 251 */
248 252 static void
249 253 smb_free(smb_request_t *sr, void *ptr, boolean_t zero)
250 254 {
251 255 smb_mem_header_t *smh;
252 256
253 257 if (ptr != NULL) {
254 258 smh = SMB_MEM2SMH(ptr);
255 259 SMB_SMH_VALID(smh);
256 260 ASSERT(sr == smh->smh_sr);
257 261 if (sr != NULL) {
258 262 SMB_REQ_VALID(sr);
259 263 list_remove(&sr->sr_storage, smh);
260 264 }
261 265 if (zero)
262 266 bzero(ptr, smh->smh_size);
263 267
264 268 smh->smh_magic = 0;
265 269 kmem_free(smh, smh->smh_size + sizeof (smb_mem_header_t));
266 270 }
267 271 }
268 272
269 273 /*
270 274 * Allocate or resize memory previously allocated.
271 275 *
272 276 * sr If not NULL, request the memory is associated with.
273 277 *
274 278 * ptr Memory address
275 279 *
276 280 * size New size
277 281 *
278 282 * zero If true zero out the extra space or the truncated space.
279 283 */
280 284 static void *
281 285 smb_realloc(smb_request_t *sr, void *ptr, size_t size, boolean_t zero)
282 286 {
283 287 smb_mem_header_t *smh;
284 288 void *new_ptr;
285 289
286 290 if (ptr == NULL)
287 291 return (smb_alloc(sr, size, zero));
↓ open down ↓ |
253 lines elided |
↑ open up ↑ |
288 292
289 293 smh = SMB_MEM2SMH(ptr);
290 294 SMB_SMH_VALID(smh);
291 295 ASSERT(sr == smh->smh_sr);
292 296
293 297 if (size == 0) {
294 298 smb_free(sr, ptr, zero);
295 299 return (NULL);
296 300 }
297 301 if (smh->smh_size >= size) {
298 - if ((zero) & (smh->smh_size > size))
302 + if ((zero) && (smh->smh_size > size))
299 303 bzero((caddr_t)ptr + size, smh->smh_size - size);
300 304 return (ptr);
301 305 }
302 306 new_ptr = smb_alloc(sr, size, B_FALSE);
303 307 bcopy(ptr, new_ptr, smh->smh_size);
304 308 if (zero)
305 309 bzero((caddr_t)new_ptr + smh->smh_size, size - smh->smh_size);
306 310
307 311 smb_free(sr, ptr, zero);
308 312 return (new_ptr);
309 313 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX