1
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License, Version 1.0 only
7 * (the "License"). You may not use this file except in compliance
8 * with the License.
9 *
10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing permissions
13 * and limitations under the License.
14 *
15 * When distributing Covered Code, include this CDDL HEADER in each
16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 * If applicable, add the following below this CDDL HEADER, with the
18 * fields enclosed by brackets "[]" replaced with your own identifying
19 * information: Portions Copyright [yyyy] [name of copyright owner]
20 *
21 * CDDL HEADER END
22 */
23 /*
24 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
26 */
27
28 /* Copyright (c) 1988 AT&T */
29 /* All Rights Reserved */
30
31 /*-
32 * Copyright (c) 2010 Pawel Jakub Dawidek <pjd@FreeBSD.org>
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #include <sys/types.h>
58 #include <sys/sunddi.h>
59 #include <sys/debug.h>
60 #include <sys/errno.h>
61 #include <sys/param.h>
62 #include <sys/lwp.h>
63 #include <sys/kernel.h>
64 #include <sys/kmem.h>
65 #include <sys/cmn_err.h>
66 #include <sys/namei.h>
67 #include <sys/stat.h>
68 #include <sys/vnode.h>
69 #include <sys/vfs_syscalls.h>
70
__strong_alias(ddi_strtol,ddi_strtoul)71 __strong_alias(ddi_strtol,ddi_strtoul)
72
73 /*
74 * String to integer conversion routines.
75 *
76 * This file is derived from usr/src/common/util/strtol.c
77 *
78 * We cannot use the user land versions as there is no errno to report
79 * error in kernel. So the return value is used to return an error,
80 * and the result is stored in an extra parameter passed by reference.
81 * Otherwise, the following functions are identical to the user land
82 * versions.
83 */
84
85 /*
86 * We should have a kernel version of ctype.h.
87 */
88 #define isalnum(ch) (isalpha(ch) || isdigit(ch))
89 #define isalpha(ch) (isupper(ch) || islower(ch))
90 #define isdigit(ch) ((ch) >= '0' && (ch) <= '9')
91 #define islower(ch) ((ch) >= 'a' && (ch) <= 'z')
92 #define isspace(ch) (((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n') || \
93 ((ch) == '\t') || ((ch) == '\f'))
94 #define isupper(ch) ((ch) >= 'A' && (ch) <= 'Z')
95 #define isxdigit(ch) (isdigit(ch) || ((ch) >= 'a' && (ch) <= 'f') || \
96 ((ch) >= 'A' && (ch) <= 'F'))
97
98 #define DIGIT(x) \
99 (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
100
101 #define MBASE ('z' - 'a' + 1 + 10)
102
103 /*
104 * The following macro is a local version of isalnum() which limits
105 * alphabetic characters to the ranges a-z and A-Z; locale dependent
106 * characters will not return 1. The members of a-z and A-Z are
107 * assumed to be in ascending order and contiguous
108 */
109 #define lisalnum(x) \
110 (isdigit(x) || ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
111
112 static int
113 do_mkdirp(const char *path)
114 {
115 struct lwp *l = curlwp;
116 int mode;
117 int error;
118 register_t ret;
119
120 const char *s, *e;
121 char *here;
122
123 error = 0;
124 mode = 493;
125
126 if (*path != '/')
127 panic("Not an absolute path");
128
129 here = PNBUF_GET();
130 for (s = path;; s = e) {
131 e = strchr(s + 1, '/');
132 if (e == NULL)
133 break;
134
135 strlcpy(here, path, e - path + 1);
136 error = do_sys_mkdir((const char *)here, mode, UIO_SYSSPACE);
137 }
138 PNBUF_PUT(here);
139
140 if (error == EEXIST)
141 error = 0;
142
143 return error;
144 }
145
146 static void
do_rmdirp(const char * path)147 do_rmdirp(const char *path)
148 {
149 struct pathbuf *pb;
150 struct nameidata nd;
151 char *here, *e;
152 int error;
153
154 here = PNBUF_GET();
155 strlcpy(here, path, MAXPATHLEN);
156 while ((e = strrchr(here, '/')) && e != here) {
157 *e = '\0';
158 pb = pathbuf_create(here);
159 if (pb == NULL)
160 break;
161 /* XXX need do_sys_rmdir()? */
162 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
163 error = namei(&nd);
164 if (error) {
165 pathbuf_destroy(pb);
166 break;
167 }
168 if ((nd.ni_vp->v_vflag & VV_ROOT) ||
169 nd.ni_vp->v_type != VDIR ||
170 nd.ni_vp->v_mountedhere ||
171 nd.ni_vp == nd.ni_dvp) {
172 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
173 if (nd.ni_vp == nd.ni_dvp)
174 vrele(nd.ni_dvp);
175 else
176 vput(nd.ni_dvp);
177 vput(nd.ni_vp);
178 pathbuf_destroy(pb);
179 break;
180 }
181 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
182 vput(nd.ni_dvp);
183 pathbuf_destroy(pb);
184 if (error)
185 break;
186 }
187 PNBUF_PUT(here);
188 }
189
190 int
ddi_strtoul(const char * str,char ** nptr,int base,unsigned long * result)191 ddi_strtoul(const char *str, char **nptr, int base, unsigned long *result)
192 {
193 unsigned long val;
194 int c;
195 int xx;
196 unsigned long multmax;
197 int neg = 0;
198 const char **ptr = (const char **)nptr;
199 const unsigned char *ustr = (const unsigned char *)str;
200
201 if (ptr != (const char **)0)
202 *ptr = (char *)ustr; /* in case no number is formed */
203 if (base < 0 || base > MBASE || base == 1) {
204 /* base is invalid -- should be a fatal error */
205 return (EINVAL);
206 }
207 if (!isalnum(c = *ustr)) {
208 while (isspace(c))
209 c = *++ustr;
210 switch (c) {
211 case '-':
212 neg++;
213 /* FALLTHROUGH */
214 case '+':
215 c = *++ustr;
216 }
217 }
218 if (base == 0)
219 if (c != '0')
220 base = 10;
221 else if (ustr[1] == 'x' || ustr[1] == 'X')
222 base = 16;
223 else
224 base = 8;
225 /*
226 * for any base > 10, the digits incrementally following
227 * 9 are assumed to be "abc...z" or "ABC...Z"
228 */
229 if (!lisalnum(c) || (xx = DIGIT(c)) >= base)
230 return (EINVAL); /* no number formed */
231 if (base == 16 && c == '0' && (ustr[1] == 'x' || ustr[1] == 'X') &&
232 isxdigit(ustr[2]))
233 c = *(ustr += 2); /* skip over leading "0x" or "0X" */
234
235 multmax = ULONG_MAX / (unsigned long)base;
236 val = DIGIT(c);
237 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; ) {
238 if (val > multmax)
239 goto overflow;
240 val *= base;
241 if (ULONG_MAX - val < xx)
242 goto overflow;
243 val += xx;
244 c = *++ustr;
245 }
246 if (ptr != (const char **)0)
247 *ptr = (char *)ustr;
248 *result = neg ? -val : val;
249 return (0);
250
251 overflow:
252 for (c = *++ustr; lisalnum(c) && (xx = DIGIT(c)) < base; (c = *++ustr))
253 ;
254 if (ptr != (const char **)0)
255 *ptr = (char *)ustr;
256 return (ERANGE);
257 }
258
259 int
ddi_strtoull(const char * str,char ** nptr,int base,unsigned long long * result)260 ddi_strtoull(const char *str, char **nptr, int base, unsigned long long *result)
261 {
262
263 *result = (unsigned long long)strtoull(str, nptr, base);
264 if (*result == 0)
265 return (EINVAL);
266 else if (*result == ULLONG_MAX)
267 return (ERANGE);
268 return (0);
269 }
270
271 /*
272 * Find first bit set in a mask (returned counting from 1 up)
273 */
274
275 int
ddi_ffs(long mask)276 ddi_ffs(long mask)
277 {
278 return (ffs(mask));
279 }
280
281 /*
282 * Find last bit set. Take mask and clear
283 * all but the most significant bit, and
284 * then let ffs do the rest of the work.
285 *
286 * Algorithm courtesy of Steve Chessin.
287 */
288
289 int
ddi_fls(long mask)290 ddi_fls(long mask)
291 {
292 while (mask) {
293 long nx;
294
295 if ((nx = (mask & (mask - 1))) == 0)
296 break;
297 mask = nx;
298 }
299 return (ffs(mask));
300 }
301
302 /*
303 * The next five routines comprise generic storage management utilities
304 * for driver soft state structures (in "the old days," this was done
305 * with a statically sized array - big systems and dynamic loading
306 * and unloading make heap allocation more attractive)
307 */
308
309 /*
310 * Allocate a set of pointers to 'n_items' objects of size 'size'
311 * bytes. Each pointer is initialized to nil.
312 *
313 * The 'size' and 'n_items' values are stashed in the opaque
314 * handle returned to the caller.
315 *
316 * This implementation interprets 'set of pointers' to mean 'array
317 * of pointers' but note that nothing in the interface definition
318 * precludes an implementation that uses, for example, a linked list.
319 * However there should be a small efficiency gain from using an array
320 * at lookup time.
321 *
322 * NOTE As an optimization, we make our growable array allocations in
323 * powers of two (bytes), since that's how much kmem_alloc (currently)
324 * gives us anyway. It should save us some free/realloc's ..
325 *
326 * As a further optimization, we make the growable array start out
327 * with MIN_N_ITEMS in it.
328 */
329
330 /*
331 * This data structure is entirely private to the soft state allocator.
332 */
333 struct i_ddi_soft_state {
334 void **array; /* the array of pointers */
335 kmutex_t lock; /* serialize access to this struct */
336 size_t size; /* how many bytes per state struct */
337 size_t n_items; /* how many structs herein */
338 struct i_ddi_soft_state *next; /* 'dirty' elements */
339 };
340
341 #define MIN_N_ITEMS 8 /* 8 void *'s == 32 bytes */
342
343 int
ddi_soft_state_init(void ** state_p,size_t size,size_t n_items)344 ddi_soft_state_init(void **state_p, size_t size, size_t n_items)
345 {
346 struct i_ddi_soft_state *ss;
347
348 if (state_p == NULL || *state_p != NULL || size == 0)
349 return (EINVAL);
350
351 ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
352 mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
353 ss->size = size;
354
355 if (n_items < MIN_N_ITEMS)
356 ss->n_items = MIN_N_ITEMS;
357 else {
358 int bitlog;
359
360 if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items))
361 bitlog--;
362 ss->n_items = 1 << bitlog;
363 }
364
365 ASSERT(ss->n_items >= n_items);
366
367 ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);
368
369 *state_p = ss;
370
371 return (0);
372 }
373
374
375 /*
376 * Allocate a state structure of size 'size' to be associated
377 * with item 'item'.
378 *
379 * In this implementation, the array is extended to
380 * allow the requested offset, if needed.
381 */
382 int
ddi_soft_state_zalloc(void * state,int item)383 ddi_soft_state_zalloc(void *state, int item)
384 {
385 struct i_ddi_soft_state *ss;
386 void **array;
387 void *new_element;
388
389 if ((ss = state) == NULL || item < 0)
390 return (DDI_FAILURE);
391
392 mutex_enter(&ss->lock);
393 if (ss->size == 0) {
394 mutex_exit(&ss->lock);
395 cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle");
396 return (DDI_FAILURE);
397 }
398
399 array = ss->array; /* NULL if ss->n_items == 0 */
400 ASSERT(ss->n_items != 0 && array != NULL);
401
402 /*
403 * refuse to tread on an existing element
404 */
405 if (item < ss->n_items && array[item] != NULL) {
406 mutex_exit(&ss->lock);
407 return (DDI_FAILURE);
408 }
409
410 /*
411 * Allocate a new element to plug in
412 */
413 new_element = kmem_zalloc(ss->size, KM_SLEEP);
414
415 /*
416 * Check if the array is big enough, if not, grow it.
417 */
418 if (item >= ss->n_items) {
419 void **new_array;
420 size_t new_n_items;
421 struct i_ddi_soft_state *dirty;
422
423 /*
424 * Allocate a new array of the right length, copy
425 * all the old pointers to the new array, then
426 * if it exists at all, put the old array on the
427 * dirty list.
428 *
429 * Note that we can't kmem_free() the old array.
430 *
431 * Why -- well the 'get' operation is 'mutex-free', so we
432 * can't easily catch a suspended thread that is just about
433 * to dereference the array we just grew out of. So we
434 * cons up a header and put it on a list of 'dirty'
435 * pointer arrays. (Dirty in the sense that there may
436 * be suspended threads somewhere that are in the middle
437 * of referencing them). Fortunately, we -can- garbage
438 * collect it all at ddi_soft_state_fini time.
439 */
440 new_n_items = ss->n_items;
441 while (new_n_items < (1 + item))
442 new_n_items <<= 1; /* double array size .. */
443
444 ASSERT(new_n_items >= (1 + item)); /* sanity check! */
445
446 new_array = kmem_zalloc(new_n_items * sizeof (void *),
447 KM_SLEEP);
448 /*
449 * Copy the pointers into the new array
450 */
451 bcopy(array, new_array, ss->n_items * sizeof (void *));
452
453 /*
454 * Save the old array on the dirty list
455 */
456 dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP);
457 dirty->array = ss->array;
458 dirty->n_items = ss->n_items;
459 dirty->next = ss->next;
460 ss->next = dirty;
461
462 ss->array = (array = new_array);
463 ss->n_items = new_n_items;
464 }
465
466 ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);
467
468 array[item] = new_element;
469
470 mutex_exit(&ss->lock);
471 return (DDI_SUCCESS);
472 }
473
474
475 /*
476 * Fetch a pointer to the allocated soft state structure.
477 *
478 * This is designed to be cheap.
479 *
480 * There's an argument that there should be more checking for
481 * nil pointers and out of bounds on the array.. but we do a lot
482 * of that in the alloc/free routines.
483 *
484 * An array has the convenience that we don't need to lock read-access
485 * to it c.f. a linked list. However our "expanding array" strategy
486 * means that we should hold a readers lock on the i_ddi_soft_state
487 * structure.
488 *
489 * However, from a performance viewpoint, we need to do it without
490 * any locks at all -- this also makes it a leaf routine. The algorithm
491 * is 'lock-free' because we only discard the pointer arrays at
492 * ddi_soft_state_fini() time.
493 */
494 void *
ddi_get_soft_state(void * state,int item)495 ddi_get_soft_state(void *state, int item)
496 {
497 struct i_ddi_soft_state *ss = state;
498
499 ASSERT(ss != NULL && item >= 0);
500
501 if (item < ss->n_items && ss->array != NULL)
502 return (ss->array[item]);
503 return (NULL);
504 }
505
506 /*
507 * Free the state structure corresponding to 'item.' Freeing an
508 * element that has either gone or was never allocated is not
509 * considered an error. Note that we free the state structure, but
510 * we don't shrink our pointer array, or discard 'dirty' arrays,
511 * since even a few pointers don't really waste too much memory.
512 *
513 * Passing an item number that is out of bounds, or a null pointer will
514 * provoke an error message.
515 */
516 void
ddi_soft_state_free(void * state,int item)517 ddi_soft_state_free(void *state, int item)
518 {
519 struct i_ddi_soft_state *ss;
520 void **array;
521 void *element;
522 static char msg[] = "ddi_soft_state_free:";
523
524 if ((ss = state) == NULL) {
525 cmn_err(CE_WARN, "%s null handle",
526 msg);
527 return;
528 }
529
530 element = NULL;
531
532 mutex_enter(&ss->lock);
533
534 if ((array = ss->array) == NULL || ss->size == 0) {
535 cmn_err(CE_WARN, "%s bad handle",
536 msg);
537 } else if (item < 0 || item >= ss->n_items) {
538 cmn_err(CE_WARN, "%s item %d not in range [0..%lu]",
539 msg, item, ss->n_items - 1);
540 } else if (array[item] != NULL) {
541 element = array[item];
542 array[item] = NULL;
543 }
544
545 mutex_exit(&ss->lock);
546
547 if (element)
548 kmem_free(element, ss->size);
549 }
550
551
552 /*
553 * Free the entire set of pointers, and any
554 * soft state structures contained therein.
555 *
556 * Note that we don't grab the ss->lock mutex, even though
557 * we're inspecting the various fields of the data structure.
558 *
559 * There is an implicit assumption that this routine will
560 * never run concurrently with any of the above on this
561 * particular state structure i.e. by the time the driver
562 * calls this routine, there should be no other threads
563 * running in the driver.
564 */
565 void
ddi_soft_state_fini(void ** state_p)566 ddi_soft_state_fini(void **state_p)
567 {
568 struct i_ddi_soft_state *ss, *dirty;
569 int item;
570 static char msg[] = "ddi_soft_state_fini:";
571
572 if (state_p == NULL || (ss = *state_p) == NULL) {
573 cmn_err(CE_WARN, "%s null handle",
574 msg);
575 return;
576 }
577
578 if (ss->size == 0) {
579 cmn_err(CE_WARN, "%s bad handle",
580 msg);
581 return;
582 }
583
584 if (ss->n_items > 0) {
585 for (item = 0; item < ss->n_items; item++)
586 ddi_soft_state_free(ss, item);
587 kmem_free(ss->array, ss->n_items * sizeof (void *));
588 }
589
590 /*
591 * Now delete any dirty arrays from previous 'grow' operations
592 */
593 for (dirty = ss->next; dirty; dirty = ss->next) {
594 ss->next = dirty->next;
595 kmem_free(dirty->array, dirty->n_items * sizeof (void *));
596 kmem_free(dirty, sizeof (*dirty));
597 }
598
599 mutex_destroy(&ss->lock);
600 kmem_free(ss, sizeof (*ss));
601
602 *state_p = NULL;
603 }
604
605 int
ddi_create_minor_node(dev_info_t * dip,char * name,int spec_type,minor_t minor_num,char * node_type,int flag)606 ddi_create_minor_node(dev_info_t *dip, char *name, int spec_type,
607 minor_t minor_num, char *node_type, int flag)
608 {
609 struct lwp *l = curlwp;
610 vnode_t *vp;
611 enum vtype vtype;
612 struct stat sb;
613 char *pn;
614 dev_t dev;
615 int error;
616
617 pn = PNBUF_GET();
618 if (spec_type == S_IFCHR) {
619 vtype = VCHR;
620 dev = makedev(dip->di_cmajor, minor_num);
621 snprintf(pn, MAXPATHLEN, "/dev/zvol/rdsk/%s", name);
622 } else if (spec_type == S_IFBLK) {
623 vtype = VBLK;
624 dev = makedev(dip->di_bmajor, minor_num);
625 snprintf(pn, MAXPATHLEN, "/dev/zvol/dsk/%s", name);
626 } else {
627 panic("bad spectype %#x", spec_type);
628 }
629 spec_type |= (S_IRUSR | S_IWUSR);
630
631 /* Create missing directories. */
632 if ((error = do_mkdirp(pn)) != 0)
633 goto exit;
634
635 /*
636 * If node exists and has correct type and rdev all done,
637 * otherwise unlink the node.
638 */
639 if (namei_simple_kernel(pn, NSM_NOFOLLOW_NOEMULROOT, &vp) == 0) {
640 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
641 error = vn_stat(vp, &sb);
642 VOP_UNLOCK(vp, 0);
643 if (error == 0 && vp->v_type == vtype && sb.st_rdev == dev) {
644 vrele(vp);
645 return 0;
646 }
647 vrele(vp);
648 (void)do_sys_unlink(pn, UIO_SYSSPACE);
649 }
650
651 error = do_sys_mknod(l, pn, spec_type, dev, UIO_SYSSPACE);
652
653 exit:
654 PNBUF_PUT(pn);
655
656 return error;
657 }
658
659 void
ddi_remove_minor_node(dev_info_t * dip,char * name)660 ddi_remove_minor_node(dev_info_t *dip, char *name)
661 {
662 char *pn;
663
664 /* Unlink block device and remove empty directories. */
665 pn = PNBUF_GET();
666 snprintf(pn, MAXPATHLEN, "/dev/zvol/dsk/%s", name);
667 (void)do_sys_unlink(pn, UIO_SYSSPACE);
668 do_rmdirp(pn);
669 PNBUF_PUT(pn);
670
671 /* Unlink raw device and remove empty directories. */
672 pn = PNBUF_GET();
673 snprintf(pn, MAXPATHLEN, "/dev/zvol/rdsk/%s", name);
674 (void)do_sys_unlink(pn, UIO_SYSSPACE);
675 do_rmdirp(pn);
676 PNBUF_PUT(pn);
677 }
678