Print this page
10097 indenting fixes in usr/src/{lib,common}
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/common/devid/devid.c
+++ new/usr/src/common/devid/devid.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, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 -#pragma ident "%Z%%M% %I% %E% SMI"
27 26
27 +/*
28 + * Copyright (c) 2018, Joyent, Inc.
29 + */
30 +
28 31 #include <sys/types.h>
29 32 #include <sys/stropts.h>
30 33 #include <sys/debug.h>
31 34 #include <sys/isa_defs.h>
32 35 #include <sys/dditypes.h>
33 36 #include <sys/ddi_impldefs.h>
34 37 #include "devid_impl.h"
35 38
36 39 static int devid_str_decode_id(char *devidstr, ddi_devid_t *devidp,
37 40 char **minor_namep, impl_devid_t *id);
38 41
39 42
40 43 /*
41 44 * Validate device id.
42 45 */
43 46 int
44 47 #ifdef _KERNEL
45 48 ddi_devid_valid(ddi_devid_t devid)
46 49 #else /* !_KERNEL */
47 50 devid_valid(ddi_devid_t devid)
48 51 #endif /* _KERNEL */
49 52 {
50 53 impl_devid_t *id = (impl_devid_t *)devid;
51 54 ushort_t type;
52 55
53 56 DEVID_ASSERT(devid != NULL);
54 57
55 58 if (id->did_magic_hi != DEVID_MAGIC_MSB)
56 59 return (DEVID_RET_INVALID);
57 60
58 61 if (id->did_magic_lo != DEVID_MAGIC_LSB)
59 62 return (DEVID_RET_INVALID);
60 63
61 64 if (id->did_rev_hi != DEVID_REV_MSB)
62 65 return (DEVID_RET_INVALID);
63 66
64 67 if (id->did_rev_lo != DEVID_REV_LSB)
65 68 return (DEVID_RET_INVALID);
66 69
67 70 type = DEVID_GETTYPE(id);
68 71 if ((type == DEVID_NONE) || (type > DEVID_MAXTYPE))
69 72 return (DEVID_RET_INVALID);
70 73
71 74 return (DEVID_RET_VALID);
72 75 }
73 76
74 77 /*
75 78 * Return the sizeof a device id. If called with NULL devid it returns
76 79 * the amount of space needed to determine the size.
77 80 */
78 81 size_t
79 82 #ifdef _KERNEL
80 83 ddi_devid_sizeof(ddi_devid_t devid)
81 84 #else /* !_KERNEL */
82 85 devid_sizeof(ddi_devid_t devid)
83 86 #endif /* _KERNEL */
84 87 {
85 88 impl_devid_t *id = (impl_devid_t *)devid;
86 89
87 90 if (id == NULL)
88 91 return (sizeof (*id) - sizeof (id->did_id));
89 92
90 93 DEVID_ASSERT(DEVID_FUNC(devid_valid)(devid) == DEVID_RET_VALID);
91 94
92 95 return (sizeof (*id) + DEVID_GETLEN(id) - sizeof (id->did_id));
93 96 }
94 97
95 98 /*
96 99 * Compare two device id's.
97 100 * -1 - less than
98 101 * 0 - equal
99 102 * 1 - greater than
100 103 */
101 104 int
102 105 #ifdef _KERNEL
103 106 ddi_devid_compare(ddi_devid_t id1, ddi_devid_t id2)
104 107 #else /* !_KERNEL */
105 108 devid_compare(ddi_devid_t id1, ddi_devid_t id2)
106 109 #endif /* _KERNEL */
107 110 {
108 111 int rval;
109 112 impl_devid_t *i_id1 = (impl_devid_t *)id1;
110 113 impl_devid_t *i_id2 = (impl_devid_t *)id2;
111 114 ushort_t i_id1_type;
112 115 ushort_t i_id2_type;
113 116
114 117 DEVID_ASSERT((id1 != NULL) && (id2 != NULL));
115 118 DEVID_ASSERT(DEVID_FUNC(devid_valid)(id1) == DEVID_RET_VALID);
116 119 DEVID_ASSERT(DEVID_FUNC(devid_valid)(id2) == DEVID_RET_VALID);
117 120
118 121 /* magic and revision comparison */
119 122 if ((rval = bcmp(id1, id2, 4)) != 0) {
120 123 return (rval);
121 124 }
122 125
123 126 /* get current devid types */
124 127 i_id1_type = DEVID_GETTYPE(i_id1);
125 128 i_id2_type = DEVID_GETTYPE(i_id2);
126 129
127 130 /*
128 131 * Originaly all page83 devids used DEVID_SCSI3_WWN.
129 132 * To avoid a possible uniqueness issue each type of page83
130 133 * encoding supported is represented as a separate
131 134 * devid type. If comparing DEVID_SCSI3_WWN against
132 135 * one of the new page83 encodings we assume that no
133 136 * uniqueness issue exists (since we had apparently been
134 137 * running with the old DEVID_SCSI3_WWN encoding without
135 138 * a problem).
136 139 */
137 140 if ((i_id1_type == DEVID_SCSI3_WWN) ||
138 141 (i_id2_type == DEVID_SCSI3_WWN)) {
139 142 /*
140 143 * Atleast one devid is using old scsi
141 144 * encode algorithm. Force devid types
142 145 * to same scheme for comparison.
143 146 */
144 147 if (IS_DEVID_SCSI3_VPD_TYPE(i_id1_type)) {
145 148 i_id1_type = DEVID_SCSI3_WWN;
146 149 }
147 150 if (IS_DEVID_SCSI3_VPD_TYPE(i_id2_type)) {
148 151 i_id2_type = DEVID_SCSI3_WWN;
149 152 }
150 153 }
151 154
152 155 /* type comparison */
153 156 if (i_id1_type != i_id2_type) {
154 157 return ((i_id1_type < i_id2_type) ? -1 : 1);
155 158 }
156 159
157 160 /* length comparison */
158 161 if (DEVID_GETLEN(i_id1) != DEVID_GETLEN(i_id2)) {
159 162 return (DEVID_GETLEN(i_id1) < DEVID_GETLEN(i_id2) ? -1 : 1);
160 163 }
161 164
162 165 /* id comparison */
163 166 rval = bcmp(i_id1->did_id, i_id2->did_id, DEVID_GETLEN(i_id1));
164 167
165 168 return (rval);
166 169 }
167 170
168 171 /*
169 172 * Free a Device Id
170 173 */
171 174 void
172 175 #ifdef _KERNEL
173 176 ddi_devid_free(ddi_devid_t devid)
174 177 #else /* !_KERNEL */
175 178 devid_free(ddi_devid_t devid)
176 179 #endif /* _KERNEL */
177 180 {
178 181 DEVID_ASSERT(devid != NULL);
179 182 DEVID_FREE(devid, DEVID_FUNC(devid_sizeof)(devid));
180 183 }
181 184
182 185 /*
183 186 * Encode a device id into a string. See ddi_impldefs.h for details.
184 187 */
185 188 char *
186 189 #ifdef _KERNEL
187 190 ddi_devid_str_encode(ddi_devid_t devid, char *minor_name)
188 191 #else /* !_KERNEL */
189 192 devid_str_encode(ddi_devid_t devid, char *minor_name)
190 193 #endif /* _KERNEL */
191 194 {
192 195 impl_devid_t *id = (impl_devid_t *)devid;
193 196 size_t driver_len, devid_len, slen;
194 197 char *sbuf, *dsp, *dp, ta;
195 198 int i, n, ascii;
196 199
197 200 /* "id0" is the encoded representation of a NULL device id */
198 201 if (devid == NULL) {
199 202 if ((sbuf = DEVID_MALLOC(4)) == NULL)
200 203 return (NULL);
201 204 *(sbuf+0) = DEVID_MAGIC_MSB;
202 205 *(sbuf+1) = DEVID_MAGIC_LSB;
203 206 *(sbuf+2) = '0';
204 207 *(sbuf+3) = 0;
205 208 return (sbuf);
206 209 }
207 210
208 211 /* verify input */
209 212 if (DEVID_FUNC(devid_valid)(devid) != DEVID_RET_VALID)
210 213 return (NULL);
211 214
212 215 /* scan the driver hint to see how long the hint is */
213 216 for (driver_len = 0; driver_len < DEVID_HINT_SIZE; driver_len++)
214 217 if (id->did_driver[driver_len] == '\0')
215 218 break;
216 219
217 220 /* scan the contained did_id to see if it meets ascii requirements */
218 221 devid_len = DEVID_GETLEN(id);
219 222 for (ascii = 1, i = 0; i < devid_len; i++)
220 223 if (!DEVID_IDBYTE_ISASCII(id->did_id[i])) {
221 224 ascii = 0;
222 225 break;
223 226 }
224 227
225 228 /* some types should always go hex even if they look ascii */
226 229 if (DEVID_TYPE_BIN_FORCEHEX(id->did_type_lo))
227 230 ascii = 0;
228 231
229 232 /* set the length of the resulting string */
230 233 slen = 2 + 1; /* <magic><rev> "id1" */
231 234 slen += 1 + driver_len + 1 + 1; /* ",<driver>@<type>" */
232 235 slen += ascii ? devid_len : (devid_len * 2); /* did_id field */
233 236 if (minor_name) {
234 237 slen += 1; /* '/' */
235 238 slen += strlen(minor_name); /* len of minor_name */
236 239 }
237 240 slen += 1; /* NULL */
238 241
239 242 /* allocate string */
240 243 if ((sbuf = DEVID_MALLOC(slen)) == NULL)
241 244 return (NULL);
242 245
243 246 /* perform encode of id to hex string */
244 247 dsp = sbuf;
245 248 *dsp++ = id->did_magic_hi;
246 249 *dsp++ = id->did_magic_lo;
247 250 *dsp++ = DEVID_REV_BINTOASCII(id->did_rev_lo);
248 251 *dsp++ = ',';
249 252 for (i = 0; i < driver_len; i++)
250 253 *dsp++ = id->did_driver[i];
251 254 *dsp++ = '@';
252 255 ta = DEVID_TYPE_BINTOASCII(id->did_type_lo);
253 256 if (ascii)
254 257 ta = DEVID_TYPE_SETASCII(ta);
255 258 *dsp++ = ta;
256 259 for (i = 0, dp = &id->did_id[0]; i < devid_len; i++, dp++) {
257 260 if (ascii) {
258 261 if (*dp == ' ')
259 262 *dsp++ = '_';
260 263 else if (*dp == 0x00)
261 264 *dsp++ = '~';
262 265 else
263 266 *dsp++ = *dp;
264 267 } else {
265 268 n = ((*dp) >> 4) & 0xF;
266 269 *dsp++ = (n < 10) ? (n + '0') : (n + ('a' - 10));
267 270 n = (*dp) & 0xF;
268 271 *dsp++ = (n < 10) ? (n + '0') : (n + ('a' - 10));
269 272 }
270 273 }
271 274
272 275 if (minor_name) {
273 276 *dsp++ = '/';
274 277 (void) strcpy(dsp, minor_name);
275 278 } else
276 279 *dsp++ = 0;
277 280
278 281 /* ensure that (strlen + 1) is correct length for free */
279 282 DEVID_ASSERT((strlen(sbuf) + 1) == slen);
280 283 return (sbuf);
281 284 }
282 285
283 286 /* free the string returned by devid_str_encode */
284 287 void
285 288 #ifdef _KERNEL
286 289 ddi_devid_str_free(char *devidstr)
287 290 #else /* !_KERNEL */
288 291 devid_str_free(char *devidstr)
289 292 #endif /* _KERNEL */
290 293 {
291 294 DEVID_FREE(devidstr, strlen(devidstr) + 1);
292 295 }
293 296
294 297 /*
295 298 * given the string representation of a device id returned by calling
296 299 * devid_str_encode (passed in as devidstr), return pointers to the
297 300 * broken out devid and minor_name as requested. Devidstr remains
298 301 * allocated and unmodified. The devid returned in *devidp should be freed by
299 302 * calling devid_free. The minor_name returned in minor_namep should
300 303 * be freed by calling devid_str_free(minor_namep).
301 304 *
302 305 * See ddi_impldefs.h for format details.
303 306 */
304 307 int
305 308 #ifdef _KERNEL
306 309 ddi_devid_str_decode(
307 310 #else /* !_KERNEL */
308 311 devid_str_decode(
309 312 #endif /* _KERNEL */
310 313 char *devidstr, ddi_devid_t *devidp, char **minor_namep)
311 314 {
312 315 return (devid_str_decode_id(devidstr, devidp, minor_namep, NULL));
313 316 }
314 317
315 318 /* implementation for (ddi_)devid_str_decode */
316 319 static int
317 320 devid_str_decode_id(char *devidstr, ddi_devid_t *devidp,
318 321 char **minor_namep, impl_devid_t *id)
319 322 {
320 323 char *str, *msp, *dsp, *dp, ta;
321 324 int slen, devid_len, ascii, i, n, c, pre_alloc = FALSE;
322 325 unsigned short id_len, type; /* for hibyte/lobyte */
323 326
324 327 if (devidp != NULL)
325 328 *devidp = NULL;
326 329 if (minor_namep != NULL)
327 330 *minor_namep = NULL;
328 331 if (id != NULL)
329 332 pre_alloc = TRUE;
330 333
331 334 if (devidstr == NULL)
332 335 return (DEVID_FAILURE);
333 336
334 337 /* the string must atleast contain the ascii two byte header */
335 338 slen = strlen(devidstr);
336 339 if ((slen < 3) || (devidstr[0] != DEVID_MAGIC_MSB) ||
337 340 (devidstr[1] != DEVID_MAGIC_LSB))
338 341 return (DEVID_FAILURE);
339 342
340 343 /* "id0" is the encoded representation of a NULL device id */
341 344 if ((devidstr[2] == '0') && (slen == 3))
342 345 return (DEVID_SUCCESS);
343 346
344 347 /* "id1,@S0" is the shortest possible, reject if shorter */
345 348 if (slen < 7)
346 349 return (DEVID_FAILURE);
347 350
348 351 /* find the optional minor name, start after ',' */
349 352 if ((msp = strchr(&devidstr[4], '/')) != NULL)
350 353 msp++;
351 354
352 355 /* skip devid processing if we are not asked to return it */
353 356 if (devidp) {
354 357 /* find the required '@' separator */
355 358 if ((str = strchr(devidstr, '@')) == NULL)
356 359 return (DEVID_FAILURE);
357 360 str++; /* skip '@' */
358 361
359 362 /* pick up <type> after the '@' and verify */
360 363 ta = *str++;
361 364 ascii = DEVID_TYPE_ISASCII(ta);
362 365 type = DEVID_TYPE_ASCIITOBIN(ta);
363 366 if (type > DEVID_MAXTYPE)
364 367 return (DEVID_FAILURE);
365 368
366 369 /* determine length of id->did_id field */
367 370 if (msp == NULL)
368 371 id_len = strlen(str);
369 372 else
370 373 id_len = msp - str - 1;
371 374
372 375 /* account for encoding: with hex, binary is half the size */
373 376 if (!ascii) {
374 377 /* hex id field must be even length */
375 378 if (id_len & 1)
376 379 return (DEVID_FAILURE);
377 380 id_len /= 2;
378 381 }
379 382
↓ open down ↓ |
342 lines elided |
↑ open up ↑ |
380 383 /* add in size of the binary devid header */
381 384 devid_len = id_len + sizeof (*id) - sizeof (id->did_id);
382 385
383 386 /*
384 387 * Allocate space for devid if we are asked to decode it
385 388 * decode it and space wasn't pre-allocated.
386 389 */
387 390 if (pre_alloc == FALSE) {
388 391 if ((id = (impl_devid_t *)DEVID_MALLOC(
389 392 devid_len)) == NULL)
390 - return (DEVID_FAILURE);
393 + return (DEVID_FAILURE);
391 394 }
392 395
393 396 /* decode header portion of the string into the binary devid */
394 397 dsp = devidstr;
395 398 id->did_magic_hi = *dsp++; /* <magic> "id" */
396 399 id->did_magic_lo = *dsp++;
397 400 id->did_rev_hi = 0;
398 401 id->did_rev_lo =
399 402 DEVID_REV_ASCIITOBIN(*dsp); /* <rev> "1" */
400 403 dsp++; /* skip "1" */
401 404 dsp++; /* skip "," */
402 405 for (i = 0; i < DEVID_HINT_SIZE; i++) { /* <driver>@ */
403 406 if (*dsp == '@')
404 407 break;
405 408 id->did_driver[i] = *dsp++;
406 409 }
407 410 for (; i < DEVID_HINT_SIZE; i++)
408 411 id->did_driver[i] = 0;
409 412
410 413 /* we must now be at the '@' */
411 414 if (*dsp != '@')
412 415 goto efree;
413 416
414 417 /* set the type and length */
415 418 DEVID_FORMTYPE(id, type);
416 419 DEVID_FORMLEN(id, id_len);
417 420
418 421 /* decode devid portion of string into the binary */
419 422 for (i = 0, dsp = str, dp = &id->did_id[0];
420 423 i < id_len; i++, dp++) {
421 424 if (ascii) {
422 425 if (*dsp == '_')
423 426 *dp = ' ';
424 427 else if (*dsp == '~')
425 428 *dp = 0x00;
426 429 else
427 430 *dp = *dsp;
428 431 dsp++;
429 432 } else {
430 433 c = *dsp++;
431 434 if (c >= '0' && c <= '9')
432 435 n = (c - '0') & 0xFF;
433 436 else if (c >= 'a' && c <= 'f')
434 437 n = (c - ('a' - 10)) & 0xFF;
435 438 else
436 439 goto efree;
437 440 n <<= 4;
438 441 c = *dsp++;
439 442 if (c >= '0' && c <= '9')
440 443 n |= (c - '0') & 0xFF;
441 444 else if (c >= 'a' && c <= 'f')
442 445 n |= (c - ('a' - 10)) & 0xFF;
443 446 else
444 447 goto efree;
445 448 *dp = n;
446 449 }
447 450 }
448 451
449 452 /* verify result */
450 453 if (DEVID_FUNC(devid_valid)((ddi_devid_t)id) != DEVID_RET_VALID)
451 454 goto efree;
452 455 }
453 456
454 457 /* duplicate minor_name if we are asked to decode it */
455 458 if (minor_namep && msp) {
456 459 if ((*minor_namep = DEVID_MALLOC(strlen(msp) + 1)) == NULL)
457 460 goto efree;
458 461 (void) strcpy(*minor_namep, msp);
459 462 }
460 463
461 464 /* return pointer to binary */
462 465 if (devidp)
463 466 *devidp = (ddi_devid_t)id;
464 467 return (DEVID_SUCCESS);
465 468
466 469 efree:
467 470 if ((pre_alloc == FALSE) && (id))
468 471 DEVID_FREE(id, devid_len);
469 472 return (DEVID_FAILURE);
470 473 }
471 474
472 475
473 476 /*
474 477 * Compare two device id's in string form
475 478 * -1 - id1 less than id2
476 479 * 0 - equal
477 480 * 1 - id1 greater than id2
478 481 */
479 482 int
480 483 #ifdef _KERNEL
481 484 ddi_devid_str_compare(char *id1_str, char *id2_str)
482 485 #else /* !_KERNEL */
483 486 devid_str_compare(char *id1_str, char *id2_str)
484 487 #endif /* _KERNEL */
485 488 {
486 489 int rval = DEVID_FAILURE;
487 490 ddi_devid_t devid1;
488 491 ddi_devid_t devid2;
489 492 #ifdef _KERNEL
490 493 /* kernel use static protected by lock. */
491 494 static kmutex_t id_lock;
492 495 static uchar_t id1[sizeof (impl_devid_t) + MAXPATHLEN];
493 496 static uchar_t id2[sizeof (impl_devid_t) + MAXPATHLEN];
494 497 #else /* !_KERNEL */
495 498 /* userland place on stack, since malloc might fail */
496 499 uchar_t id1[sizeof (impl_devid_t) + MAXPATHLEN];
497 500 uchar_t id2[sizeof (impl_devid_t) + MAXPATHLEN];
498 501 #endif /* _KERNEL */
499 502
500 503 #ifdef _KERNEL
501 504 mutex_enter(&id_lock);
502 505 #endif /* _KERNEL */
503 506
504 507 /*
505 508 * encode string form of devid
506 509 */
507 510 if ((devid_str_decode_id(id1_str, &devid1, NULL, (impl_devid_t *)id1) ==
508 511 DEVID_SUCCESS) &&
509 512 (devid_str_decode_id(id2_str, &devid2, NULL, (impl_devid_t *)id2) ==
510 513 DEVID_SUCCESS)) {
511 514 rval = DEVID_FUNC(devid_compare)(devid1, devid2);
512 515 }
513 516
514 517 #ifdef _KERNEL
515 518 mutex_exit(&id_lock);
516 519 #endif /* _KERNEL */
517 520
518 521 return (rval);
519 522 }
↓ open down ↓ |
119 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX