FreeBSD kernel kern code
kern_conf.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 1999-2002 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$BSDSUniX$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/bio.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/conf.h>
40 #include <sys/vnode.h>
41 #include <sys/queue.h>
42 #include <sys/poll.h>
43 #include <sys/sx.h>
44 #include <sys/ctype.h>
45 #include <sys/ucred.h>
46 #include <sys/taskqueue.h>
47 #include <machine/stdarg.h>
48 
49 #include <fs/devfs/devfs_int.h>
50 #include <vm/vm.h>
51 
52 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage");
53 
54 struct mtx devmtx;
55 static void destroy_devl(struct cdev *dev);
56 static int destroy_dev_sched_cbl(struct cdev *dev,
57  void (*cb)(void *), void *arg);
58 static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw,
59  int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
60  va_list ap);
61 
62 static struct cdev_priv_list cdevp_free_list =
63  TAILQ_HEAD_INITIALIZER(cdevp_free_list);
64 static SLIST_HEAD(free_cdevsw, cdevsw) cdevsw_gt_post_list =
65  SLIST_HEAD_INITIALIZER(cdevsw_gt_post_list);
66 
67 void
68 dev_lock(void)
69 {
70 
71  mtx_lock(&devmtx);
72 }
73 
74 /*
75  * Free all the memory collected while the cdev mutex was
76  * locked. Since devmtx is after the system map mutex, free() cannot
77  * be called immediately and is postponed until cdev mutex can be
78  * dropped.
79  */
80 static void
82 {
83  struct cdev_priv_list cdp_free;
84  struct free_cdevsw csw_free;
85  struct cdev_priv *cdp;
86  struct cdevsw *csw;
87 
88  mtx_assert(&devmtx, MA_OWNED);
89 
90  /*
91  * Make the local copy of the list heads while the dev_mtx is
92  * held. Free it later.
93  */
94  TAILQ_INIT(&cdp_free);
95  TAILQ_CONCAT(&cdp_free, &cdevp_free_list, cdp_list);
96  csw_free = cdevsw_gt_post_list;
97  SLIST_INIT(&cdevsw_gt_post_list);
98 
99  mtx_unlock(&devmtx);
100 
101  while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) {
102  TAILQ_REMOVE(&cdp_free, cdp, cdp_list);
103  devfs_free(&cdp->cdp_c);
104  }
105  while ((csw = SLIST_FIRST(&csw_free)) != NULL) {
106  SLIST_REMOVE_HEAD(&csw_free, d_postfree_list);
107  free(csw, M_DEVT);
108  }
109 }
110 
111 static void
112 dev_free_devlocked(struct cdev *cdev)
113 {
114  struct cdev_priv *cdp;
115 
116  mtx_assert(&devmtx, MA_OWNED);
117  cdp = cdev2priv(cdev);
118  KASSERT((cdp->cdp_flags & CDP_UNREF_DTR) == 0,
119  ("destroy_dev() was not called after delist_dev(%p)", cdev));
120  TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list);
121 }
122 
123 static void
124 cdevsw_free_devlocked(struct cdevsw *csw)
125 {
126 
127  mtx_assert(&devmtx, MA_OWNED);
128  SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list);
129 }
130 
131 void
133 {
134 
135  mtx_unlock(&devmtx);
136 }
137 
138 void
139 dev_ref(struct cdev *dev)
140 {
141 
142  mtx_assert(&devmtx, MA_NOTOWNED);
143  mtx_lock(&devmtx);
144  dev->si_refcount++;
145  mtx_unlock(&devmtx);
146 }
147 
148 void
149 dev_refl(struct cdev *dev)
150 {
151 
152  mtx_assert(&devmtx, MA_OWNED);
153  dev->si_refcount++;
154 }
155 
156 void
157 dev_rel(struct cdev *dev)
158 {
159  int flag = 0;
160 
161  mtx_assert(&devmtx, MA_NOTOWNED);
162  dev_lock();
163  dev->si_refcount--;
164  KASSERT(dev->si_refcount >= 0,
165  ("dev_rel(%s) gave negative count", devtoname(dev)));
166 #if 0
167  if (dev->si_usecount == 0 &&
168  (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED))
169  ;
170  else
171 #endif
172  if (dev->si_devsw == NULL && dev->si_refcount == 0) {
173  LIST_REMOVE(dev, si_list);
174  flag = 1;
175  }
176  dev_unlock();
177  if (flag)
178  devfs_free(dev);
179 }
180 
181 struct cdevsw *
182 dev_refthread(struct cdev *dev, int *ref)
183 {
184  struct cdevsw *csw;
185  struct cdev_priv *cdp;
186 
187  mtx_assert(&devmtx, MA_NOTOWNED);
188  if ((dev->si_flags & SI_ETERNAL) != 0) {
189  *ref = 0;
190  return (dev->si_devsw);
191  }
192  dev_lock();
193  csw = dev->si_devsw;
194  if (csw != NULL) {
195  cdp = cdev2priv(dev);
196  if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0)
197  dev->si_threadcount++;
198  else
199  csw = NULL;
200  }
201  dev_unlock();
202  *ref = 1;
203  return (csw);
204 }
205 
206 struct cdevsw *
207 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref)
208 {
209  struct cdevsw *csw;
210  struct cdev_priv *cdp;
211  struct cdev *dev;
212 
213  mtx_assert(&devmtx, MA_NOTOWNED);
214  if ((vp->v_vflag & VV_ETERNALDEV) != 0) {
215  dev = vp->v_rdev;
216  if (dev == NULL)
217  return (NULL);
218  KASSERT((dev->si_flags & SI_ETERNAL) != 0,
219  ("Not eternal cdev"));
220  *ref = 0;
221  csw = dev->si_devsw;
222  KASSERT(csw != NULL, ("Eternal cdev is destroyed"));
223  *devp = dev;
224  return (csw);
225  }
226 
227  csw = NULL;
228  dev_lock();
229  dev = vp->v_rdev;
230  if (dev == NULL) {
231  dev_unlock();
232  return (NULL);
233  }
234  cdp = cdev2priv(dev);
235  if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) {
236  csw = dev->si_devsw;
237  if (csw != NULL)
238  dev->si_threadcount++;
239  }
240  dev_unlock();
241  if (csw != NULL) {
242  *devp = dev;
243  *ref = 1;
244  }
245  return (csw);
246 }
247 
248 void
249 dev_relthread(struct cdev *dev, int ref)
250 {
251 
252  mtx_assert(&devmtx, MA_NOTOWNED);
253  if (!ref)
254  return;
255  dev_lock();
256  KASSERT(dev->si_threadcount > 0,
257  ("%s threadcount is wrong", dev->si_name));
258  dev->si_threadcount--;
259  dev_unlock();
260 }
261 
262 int
263 nullop(void)
264 {
265 
266  return (0);
267 }
268 
269 int
271 {
272 
273  return (EOPNOTSUPP);
274 }
275 
276 static int
277 enxio(void)
278 {
279  return (ENXIO);
280 }
281 
282 static int
283 enodev(void)
284 {
285  return (ENODEV);
286 }
287 
288 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
289 
290 #define dead_open (d_open_t *)enxio
291 #define dead_close (d_close_t *)enxio
292 #define dead_read (d_read_t *)enxio
293 #define dead_write (d_write_t *)enxio
294 #define dead_ioctl (d_ioctl_t *)enxio
295 #define dead_poll (d_poll_t *)enodev
296 #define dead_mmap (d_mmap_t *)enodev
297 
298 static void
299 dead_strategy(struct bio *bp)
300 {
301 
302  biofinish(bp, NULL, ENXIO);
303 }
304 
305 #define dead_dump (dumper_t *)enxio
306 #define dead_kqfilter (d_kqfilter_t *)enxio
307 #define dead_mmap_single (d_mmap_single_t *)enodev
308 
309 static struct cdevsw dead_cdevsw = {
310  .d_version = D_VERSION,
311  .d_open = dead_open,
312  .d_close = dead_close,
313  .d_read = dead_read,
314  .d_write = dead_write,
315  .d_ioctl = dead_ioctl,
316  .d_poll = dead_poll,
317  .d_mmap = dead_mmap,
318  .d_strategy = dead_strategy,
319  .d_name = "dead",
320  .d_dump = dead_dump,
321  .d_kqfilter = dead_kqfilter,
322  .d_mmap_single = dead_mmap_single
323 };
324 
325 /* Default methods if driver does not specify method */
326 
327 #define null_open (d_open_t *)nullop
328 #define null_close (d_close_t *)nullop
329 #define no_read (d_read_t *)enodev
330 #define no_write (d_write_t *)enodev
331 #define no_ioctl (d_ioctl_t *)enodev
332 #define no_mmap (d_mmap_t *)enodev
333 #define no_kqfilter (d_kqfilter_t *)enodev
334 #define no_mmap_single (d_mmap_single_t *)enodev
335 
336 static void
337 no_strategy(struct bio *bp)
338 {
339 
340  biofinish(bp, NULL, ENODEV);
341 }
342 
343 static int
344 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
345 {
346 
347  return (poll_no_poll(events));
348 }
349 
350 #define no_dump (dumper_t *)enodev
351 
352 static int
353 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
354 {
355  struct cdevsw *dsw;
356  int ref, retval;
357 
358  dsw = dev_refthread(dev, &ref);
359  if (dsw == NULL)
360  return (ENXIO);
361  mtx_lock(&Giant);
362  retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td);
363  mtx_unlock(&Giant);
364  dev_relthread(dev, ref);
365  return (retval);
366 }
367 
368 static int
369 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp)
370 {
371  struct cdevsw *dsw;
372  int ref, retval;
373 
374  dsw = dev_refthread(dev, &ref);
375  if (dsw == NULL)
376  return (ENXIO);
377  mtx_lock(&Giant);
378  retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fp);
379  mtx_unlock(&Giant);
380  dev_relthread(dev, ref);
381  return (retval);
382 }
383 
384 static int
385 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
386 {
387  struct cdevsw *dsw;
388  int ref, retval;
389 
390  dsw = dev_refthread(dev, &ref);
391  if (dsw == NULL)
392  return (ENXIO);
393  mtx_lock(&Giant);
394  retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td);
395  mtx_unlock(&Giant);
396  dev_relthread(dev, ref);
397  return (retval);
398 }
399 
400 static void
401 giant_strategy(struct bio *bp)
402 {
403  struct cdevsw *dsw;
404  struct cdev *dev;
405  int ref;
406 
407  dev = bp->bio_dev;
408  dsw = dev_refthread(dev, &ref);
409  if (dsw == NULL) {
410  biofinish(bp, NULL, ENXIO);
411  return;
412  }
413  mtx_lock(&Giant);
414  dsw->d_gianttrick->d_strategy(bp);
415  mtx_unlock(&Giant);
416  dev_relthread(dev, ref);
417 }
418 
419 static int
420 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
421 {
422  struct cdevsw *dsw;
423  int ref, retval;
424 
425  dsw = dev_refthread(dev, &ref);
426  if (dsw == NULL)
427  return (ENXIO);
428  mtx_lock(&Giant);
429  retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td);
430  mtx_unlock(&Giant);
431  dev_relthread(dev, ref);
432  return (retval);
433 }
434 
435 static int
436 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
437 {
438  struct cdevsw *dsw;
439  int ref, retval;
440 
441  dsw = dev_refthread(dev, &ref);
442  if (dsw == NULL)
443  return (ENXIO);
444  mtx_lock(&Giant);
445  retval = dsw->d_gianttrick->d_read(dev, uio, ioflag);
446  mtx_unlock(&Giant);
447  dev_relthread(dev, ref);
448  return (retval);
449 }
450 
451 static int
452 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
453 {
454  struct cdevsw *dsw;
455  int ref, retval;
456 
457  dsw = dev_refthread(dev, &ref);
458  if (dsw == NULL)
459  return (ENXIO);
460  mtx_lock(&Giant);
461  retval = dsw->d_gianttrick->d_write(dev, uio, ioflag);
462  mtx_unlock(&Giant);
463  dev_relthread(dev, ref);
464  return (retval);
465 }
466 
467 static int
468 giant_poll(struct cdev *dev, int events, struct thread *td)
469 {
470  struct cdevsw *dsw;
471  int ref, retval;
472 
473  dsw = dev_refthread(dev, &ref);
474  if (dsw == NULL)
475  return (ENXIO);
476  mtx_lock(&Giant);
477  retval = dsw->d_gianttrick->d_poll(dev, events, td);
478  mtx_unlock(&Giant);
479  dev_relthread(dev, ref);
480  return (retval);
481 }
482 
483 static int
484 giant_kqfilter(struct cdev *dev, struct knote *kn)
485 {
486  struct cdevsw *dsw;
487  int ref, retval;
488 
489  dsw = dev_refthread(dev, &ref);
490  if (dsw == NULL)
491  return (ENXIO);
492  mtx_lock(&Giant);
493  retval = dsw->d_gianttrick->d_kqfilter(dev, kn);
494  mtx_unlock(&Giant);
495  dev_relthread(dev, ref);
496  return (retval);
497 }
498 
499 static int
500 giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
501  vm_memattr_t *memattr)
502 {
503  struct cdevsw *dsw;
504  int ref, retval;
505 
506  dsw = dev_refthread(dev, &ref);
507  if (dsw == NULL)
508  return (ENXIO);
509  mtx_lock(&Giant);
510  retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot,
511  memattr);
512  mtx_unlock(&Giant);
513  dev_relthread(dev, ref);
514  return (retval);
515 }
516 
517 static int
518 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size,
519  vm_object_t *object, int nprot)
520 {
521  struct cdevsw *dsw;
522  int ref, retval;
523 
524  dsw = dev_refthread(dev, &ref);
525  if (dsw == NULL)
526  return (ENXIO);
527  mtx_lock(&Giant);
528  retval = dsw->d_gianttrick->d_mmap_single(dev, offset, size, object,
529  nprot);
530  mtx_unlock(&Giant);
531  dev_relthread(dev, ref);
532  return (retval);
533 }
534 
535 static void
536 notify(struct cdev *dev, const char *ev, int flags)
537 {
538  static const char prefix[] = "cdev=";
539  char *data;
540  int namelen, mflags;
541 
542  if (cold)
543  return;
544  mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
545  namelen = strlen(dev->si_name);
546  data = malloc(namelen + sizeof(prefix), M_TEMP, mflags);
547  if (data == NULL)
548  return;
549  memcpy(data, prefix, sizeof(prefix) - 1);
550  memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
551  devctl_notify_f("DEVFS", "CDEV", ev, data, mflags);
552  free(data, M_TEMP);
553 }
554 
555 static void
556 notify_create(struct cdev *dev, int flags)
557 {
558 
559  notify(dev, "CREATE", flags);
560 }
561 
562 static void
563 notify_destroy(struct cdev *dev)
564 {
565 
566  notify(dev, "DESTROY", MAKEDEV_WAITOK);
567 }
568 
569 static struct cdev *
570 newdev(struct cdevsw *csw, int unit, struct cdev *si)
571 {
572  struct cdev *si2;
573 
574  mtx_assert(&devmtx, MA_OWNED);
575  if (csw->d_flags & D_NEEDMINOR) {
576  /* We may want to return an existing device */
577  LIST_FOREACH(si2, &csw->d_devs, si_list) {
578  if (dev2unit(si2) == unit) {
579  dev_free_devlocked(si);
580  return (si2);
581  }
582  }
583  }
584  si->si_drv0 = unit;
585  si->si_devsw = csw;
586  LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
587  return (si);
588 }
589 
590 static void
591 fini_cdevsw(struct cdevsw *devsw)
592 {
593  struct cdevsw *gt;
594 
595  if (devsw->d_gianttrick != NULL) {
596  gt = devsw->d_gianttrick;
597  memcpy(devsw, gt, sizeof *devsw);
599  devsw->d_gianttrick = NULL;
600  }
601  devsw->d_flags &= ~D_INIT;
602 }
603 
604 static int
605 prep_cdevsw(struct cdevsw *devsw, int flags)
606 {
607  struct cdevsw *dsw2;
608 
609  mtx_assert(&devmtx, MA_OWNED);
610  if (devsw->d_flags & D_INIT)
611  return (0);
612  if (devsw->d_flags & D_NEEDGIANT) {
613  dev_unlock();
614  dsw2 = malloc(sizeof *dsw2, M_DEVT,
615  (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK);
616  dev_lock();
617  if (dsw2 == NULL && !(devsw->d_flags & D_INIT))
618  return (ENOMEM);
619  } else
620  dsw2 = NULL;
621  if (devsw->d_flags & D_INIT) {
622  if (dsw2 != NULL)
623  cdevsw_free_devlocked(dsw2);
624  return (0);
625  }
626 
627  if (devsw->d_version != D_VERSION_03) {
628  printf(
629  "WARNING: Device driver \"%s\" has wrong version %s\n",
630  devsw->d_name == NULL ? "???" : devsw->d_name,
631  "and is disabled. Recompile KLD module.");
632  devsw->d_open = dead_open;
633  devsw->d_close = dead_close;
634  devsw->d_read = dead_read;
635  devsw->d_write = dead_write;
636  devsw->d_ioctl = dead_ioctl;
637  devsw->d_poll = dead_poll;
638  devsw->d_mmap = dead_mmap;
639  devsw->d_mmap_single = dead_mmap_single;
640  devsw->d_strategy = dead_strategy;
641  devsw->d_dump = dead_dump;
642  devsw->d_kqfilter = dead_kqfilter;
643  }
644 
645  if (devsw->d_flags & D_NEEDGIANT) {
646  if (devsw->d_gianttrick == NULL) {
647  memcpy(dsw2, devsw, sizeof *dsw2);
648  devsw->d_gianttrick = dsw2;
649  dsw2 = NULL;
650  }
651  }
652 
653 #define FIXUP(member, noop, giant) \
654  do { \
655  if (devsw->member == NULL) { \
656  devsw->member = noop; \
657  } else if (devsw->d_flags & D_NEEDGIANT) \
658  devsw->member = giant; \
659  } \
660  while (0)
661 
662  FIXUP(d_open, null_open, giant_open);
663  FIXUP(d_fdopen, NULL, giant_fdopen);
664  FIXUP(d_close, null_close, giant_close);
665  FIXUP(d_read, no_read, giant_read);
666  FIXUP(d_write, no_write, giant_write);
667  FIXUP(d_ioctl, no_ioctl, giant_ioctl);
668  FIXUP(d_poll, no_poll, giant_poll);
669  FIXUP(d_mmap, no_mmap, giant_mmap);
670  FIXUP(d_strategy, no_strategy, giant_strategy);
671  FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter);
672  FIXUP(d_mmap_single, no_mmap_single, giant_mmap_single);
673 
674  if (devsw->d_dump == NULL) devsw->d_dump = no_dump;
675 
676  LIST_INIT(&devsw->d_devs);
677 
678  devsw->d_flags |= D_INIT;
679 
680  if (dsw2 != NULL)
681  cdevsw_free_devlocked(dsw2);
682  return (0);
683 }
684 
685 static int
686 prep_devname(struct cdev *dev, const char *fmt, va_list ap)
687 {
688  int len;
689  char *from, *q, *s, *to;
690 
691  mtx_assert(&devmtx, MA_OWNED);
692 
693  len = vsnrprintf(dev->__si_namebuf, sizeof(dev->__si_namebuf), 32,
694  fmt, ap);
695  if (len > sizeof(dev->__si_namebuf) - 1)
696  return (ENAMETOOLONG);
697 
698  /* Strip leading slashes. */
699  for (from = dev->__si_namebuf; *from == '/'; from++)
700  ;
701 
702  for (to = dev->__si_namebuf; *from != '\0'; from++, to++) {
703  /* Treat multiple sequential slashes as single. */
704  while (from[0] == '/' && from[1] == '/')
705  from++;
706  /* Trailing slash is considered invalid. */
707  if (from[0] == '/' && from[1] == '\0')
708  return (EINVAL);
709  *to = *from;
710  }
711  *to = '\0';
712 
713  if (dev->__si_namebuf[0] == '\0')
714  return (EINVAL);
715 
716  /* Disallow "." and ".." components. */
717  for (s = dev->__si_namebuf;;) {
718  for (q = s; *q != '/' && *q != '\0'; q++)
719  ;
720  if (q - s == 1 && s[0] == '.')
721  return (EINVAL);
722  if (q - s == 2 && s[0] == '.' && s[1] == '.')
723  return (EINVAL);
724  if (*q != '/')
725  break;
726  s = q + 1;
727  }
728 
729  if (devfs_dev_exists(dev->__si_namebuf) != 0)
730  return (EEXIST);
731 
732  return (0);
733 }
734 
735 static int
736 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit,
737  struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
738  va_list ap)
739 {
740  struct cdev *dev, *dev_new;
741  int res;
742 
743  KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0,
744  ("make_dev_credv: both WAITOK and NOWAIT specified"));
745  dev_new = devfs_alloc(flags);
746  if (dev_new == NULL)
747  return (ENOMEM);
748  dev_lock();
749  res = prep_cdevsw(devsw, flags);
750  if (res != 0) {
751  dev_unlock();
752  devfs_free(dev_new);
753  return (res);
754  }
755  dev = newdev(devsw, unit, dev_new);
756  if ((dev->si_flags & SI_NAMED) == 0) {
757  res = prep_devname(dev, fmt, ap);
758  if (res != 0) {
759  if ((flags & MAKEDEV_CHECKNAME) == 0) {
760  panic(
761  "make_dev_credv: bad si_name (error=%d, si_name=%s)",
762  res, dev->si_name);
763  }
764  if (dev == dev_new) {
765  LIST_REMOVE(dev, si_list);
766  dev_unlock();
767  devfs_free(dev);
768  } else
769  dev_unlock();
770  return (res);
771  }
772  }
773  if (flags & MAKEDEV_REF)
774  dev_refl(dev);
775  if (flags & MAKEDEV_ETERNAL)
776  dev->si_flags |= SI_ETERNAL;
777  if (dev->si_flags & SI_CHEAPCLONE &&
778  dev->si_flags & SI_NAMED) {
779  /*
780  * This is allowed as it removes races and generally
781  * simplifies cloning devices.
782  * XXX: still ??
783  */
785  *dres = dev;
786  return (0);
787  }
788  KASSERT(!(dev->si_flags & SI_NAMED),
789  ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
790  devsw->d_name, dev2unit(dev), devtoname(dev)));
791  dev->si_flags |= SI_NAMED;
792  if (cr != NULL)
793  dev->si_cred = crhold(cr);
794  dev->si_uid = uid;
795  dev->si_gid = gid;
796  dev->si_mode = mode;
797 
798  devfs_create(dev);
799  clean_unrhdrl(devfs_inos);
801 
802  notify_create(dev, flags);
803 
804  *dres = dev;
805  return (0);
806 }
807 
808 struct cdev *
809 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode,
810  const char *fmt, ...)
811 {
812  struct cdev *dev;
813  va_list ap;
814  int res;
815 
816  va_start(ap, fmt);
817  res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt,
818  ap);
819  va_end(ap);
820  KASSERT(res == 0 && dev != NULL,
821  ("make_dev: failed make_dev_credv (error=%d)", res));
822  return (dev);
823 }
824 
825 struct cdev *
826 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid,
827  gid_t gid, int mode, const char *fmt, ...)
828 {
829  struct cdev *dev;
830  va_list ap;
831  int res;
832 
833  va_start(ap, fmt);
834  res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap);
835  va_end(ap);
836 
837  KASSERT(res == 0 && dev != NULL,
838  ("make_dev_cred: failed make_dev_credv (error=%d)", res));
839  return (dev);
840 }
841 
842 struct cdev *
843 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr,
844  uid_t uid, gid_t gid, int mode, const char *fmt, ...)
845 {
846  struct cdev *dev;
847  va_list ap;
848  int res;
849 
850  va_start(ap, fmt);
851  res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode,
852  fmt, ap);
853  va_end(ap);
854 
855  KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) ||
856  ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0,
857  ("make_dev_credf: failed make_dev_credv (error=%d)", res));
858  return (res == 0 ? dev : NULL);
859 }
860 
861 int
862 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw,
863  struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...)
864 {
865  va_list ap;
866  int res;
867 
868  va_start(ap, fmt);
869  res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode,
870  fmt, ap);
871  va_end(ap);
872 
873  KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) ||
874  ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0,
875  ("make_dev_p: failed make_dev_credv (error=%d)", res));
876  return (res);
877 }
878 
879 static void
880 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
881 {
882 
883  cdev->si_parent = pdev;
884  cdev->si_flags |= SI_CHILD;
885  LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
886 }
887 
888 
889 void
890 dev_depends(struct cdev *pdev, struct cdev *cdev)
891 {
892 
893  dev_lock();
894  dev_dependsl(pdev, cdev);
895  dev_unlock();
896 }
897 
898 static int
899 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev,
900  const char *fmt, va_list ap)
901 {
902  struct cdev *dev;
903  int error;
904 
905  KASSERT(pdev != NULL, ("make_dev_alias_v: pdev is NULL"));
906  KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0,
907  ("make_dev_alias_v: both WAITOK and NOWAIT specified"));
908  KASSERT((flags & ~(MAKEDEV_WAITOK | MAKEDEV_NOWAIT |
909  MAKEDEV_CHECKNAME)) == 0,
910  ("make_dev_alias_v: invalid flags specified (flags=%02x)", flags));
911 
912  dev = devfs_alloc(flags);
913  if (dev == NULL)
914  return (ENOMEM);
915  dev_lock();
916  dev->si_flags |= SI_ALIAS;
917  error = prep_devname(dev, fmt, ap);
918  if (error != 0) {
919  if ((flags & MAKEDEV_CHECKNAME) == 0) {
920  panic("make_dev_alias_v: bad si_name "
921  "(error=%d, si_name=%s)", error, dev->si_name);
922  }
923  dev_unlock();
924  devfs_free(dev);
925  return (error);
926  }
927  dev->si_flags |= SI_NAMED;
928  devfs_create(dev);
929  dev_dependsl(pdev, dev);
930  clean_unrhdrl(devfs_inos);
931  dev_unlock();
932 
933  notify_create(dev, flags);
934  *cdev = dev;
935 
936  return (0);
937 }
938 
939 struct cdev *
940 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
941 {
942  struct cdev *dev;
943  va_list ap;
944  int res;
945 
946  va_start(ap, fmt);
947  res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap);
948  va_end(ap);
949 
950  KASSERT(res == 0 && dev != NULL,
951  ("make_dev_alias: failed make_dev_alias_v (error=%d)", res));
952  return (dev);
953 }
954 
955 int
956 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev,
957  const char *fmt, ...)
958 {
959  va_list ap;
960  int res;
961 
962  va_start(ap, fmt);
963  res = make_dev_alias_v(flags, cdev, pdev, fmt, ap);
964  va_end(ap);
965  return (res);
966 }
967 
968 int
969 make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev,
970  struct cdev *old_alias, const char *physpath)
971 {
972  char *devfspath;
973  int physpath_len;
974  int max_parentpath_len;
975  int parentpath_len;
976  int devfspathbuf_len;
977  int mflags;
978  int ret;
979 
980  *cdev = NULL;
981  devfspath = NULL;
982  physpath_len = strlen(physpath);
983  ret = EINVAL;
984  if (physpath_len == 0)
985  goto out;
986 
987  if (strncmp("id1,", physpath, 4) == 0) {
988  physpath += 4;
989  physpath_len -= 4;
990  if (physpath_len == 0)
991  goto out;
992  }
993 
994  max_parentpath_len = SPECNAMELEN - physpath_len - /*/*/1;
995  parentpath_len = strlen(pdev->si_name);
996  if (max_parentpath_len < parentpath_len) {
997  if (bootverbose)
998  printf("WARNING: Unable to alias %s "
999  "to %s/%s - path too long\n",
1000  pdev->si_name, physpath, pdev->si_name);
1001  ret = ENAMETOOLONG;
1002  goto out;
1003  }
1004 
1005  mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
1006  devfspathbuf_len = physpath_len + /*/*/1 + parentpath_len + /*NUL*/1;
1007  devfspath = malloc(devfspathbuf_len, M_DEVBUF, mflags);
1008  if (devfspath == NULL) {
1009  ret = ENOMEM;
1010  goto out;
1011  }
1012 
1013  sprintf(devfspath, "%s/%s", physpath, pdev->si_name);
1014  if (old_alias != NULL && strcmp(old_alias->si_name, devfspath) == 0) {
1015  /* Retain the existing alias. */
1016  *cdev = old_alias;
1017  old_alias = NULL;
1018  ret = 0;
1019  } else {
1020  ret = make_dev_alias_p(flags, cdev, pdev, "%s", devfspath);
1021  }
1022 out:
1023  if (old_alias != NULL)
1024  destroy_dev(old_alias);
1025  if (devfspath != NULL)
1026  free(devfspath, M_DEVBUF);
1027  return (ret);
1028 }
1029 
1030 static void
1031 destroy_devl(struct cdev *dev)
1032 {
1033  struct cdevsw *csw;
1034  struct cdev_privdata *p;
1035  struct cdev_priv *cdp;
1036 
1037  mtx_assert(&devmtx, MA_OWNED);
1038  KASSERT(dev->si_flags & SI_NAMED,
1039  ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev)));
1040  KASSERT((dev->si_flags & SI_ETERNAL) == 0,
1041  ("WARNING: Driver mistake: destroy_dev on eternal %d\n",
1042  dev2unit(dev)));
1043 
1044  cdp = cdev2priv(dev);
1045  if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
1046  /*
1047  * Avoid race with dev_rel(), e.g. from the populate
1048  * loop. If CDP_UNREF_DTR flag is set, the reference
1049  * to be dropped at the end of destroy_devl() was
1050  * already taken by delist_dev_locked().
1051  */
1052  dev_refl(dev);
1053 
1054  devfs_destroy(dev);
1055  }
1056 
1057  /* Remove name marking */
1058  dev->si_flags &= ~SI_NAMED;
1059 
1060  /* If we are a child, remove us from the parents list */
1061  if (dev->si_flags & SI_CHILD) {
1062  LIST_REMOVE(dev, si_siblings);
1063  dev->si_flags &= ~SI_CHILD;
1064  }
1065 
1066  /* Kill our children */
1067  while (!LIST_EMPTY(&dev->si_children))
1068  destroy_devl(LIST_FIRST(&dev->si_children));
1069 
1070  /* Remove from clone list */
1071  if (dev->si_flags & SI_CLONELIST) {
1072  LIST_REMOVE(dev, si_clone);
1073  dev->si_flags &= ~SI_CLONELIST;
1074  }
1075 
1076  csw = dev->si_devsw;
1077  dev->si_devsw = NULL; /* already NULL for SI_ALIAS */
1078  while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
1079  csw->d_purge(dev);
1080  msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
1081  if (dev->si_threadcount)
1082  printf("Still %lu threads in %s\n",
1083  dev->si_threadcount, devtoname(dev));
1084  }
1085  while (dev->si_threadcount != 0) {
1086  /* Use unique dummy wait ident */
1087  msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10);
1088  }
1089 
1090  dev_unlock();
1091  if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
1092  /* avoid out of order notify events */
1093  notify_destroy(dev);
1094  }
1095  mtx_lock(&cdevpriv_mtx);
1096  while ((p = LIST_FIRST(&cdp->cdp_fdpriv)) != NULL) {
1097  devfs_destroy_cdevpriv(p);
1098  mtx_lock(&cdevpriv_mtx);
1099  }
1100  mtx_unlock(&cdevpriv_mtx);
1101  dev_lock();
1102 
1103  dev->si_drv1 = 0;
1104  dev->si_drv2 = 0;
1105  bzero(&dev->__si_u, sizeof(dev->__si_u));
1106 
1107  if (!(dev->si_flags & SI_ALIAS)) {
1108  /* Remove from cdevsw list */
1109  LIST_REMOVE(dev, si_list);
1110 
1111  /* If cdevsw has no more struct cdev *'s, clean it */
1112  if (LIST_EMPTY(&csw->d_devs)) {
1113  fini_cdevsw(csw);
1114  wakeup(&csw->d_devs);
1115  }
1116  }
1117  dev->si_flags &= ~SI_ALIAS;
1118  cdp->cdp_flags &= ~CDP_UNREF_DTR;
1119  dev->si_refcount--;
1120 
1121  if (dev->si_refcount > 0)
1122  LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
1123  else
1124  dev_free_devlocked(dev);
1125 }
1126 
1127 static void
1128 delist_dev_locked(struct cdev *dev)
1129 {
1130  struct cdev_priv *cdp;
1131  struct cdev *child;
1132 
1133  mtx_assert(&devmtx, MA_OWNED);
1134  cdp = cdev2priv(dev);
1135  if ((cdp->cdp_flags & CDP_UNREF_DTR) != 0)
1136  return;
1137  cdp->cdp_flags |= CDP_UNREF_DTR;
1138  dev_refl(dev);
1139  devfs_destroy(dev);
1140  LIST_FOREACH(child, &dev->si_children, si_siblings)
1141  delist_dev_locked(child);
1142  dev_unlock();
1143  /* ensure the destroy event is queued in order */
1144  notify_destroy(dev);
1145  dev_lock();
1146 }
1147 
1148 /*
1149  * This function will delist a character device and its children from
1150  * the directory listing and create a destroy event without waiting
1151  * for all character device references to go away. At some later point
1152  * destroy_dev() must be called to complete the character device
1153  * destruction. After calling this function the character device name
1154  * can instantly be re-used.
1155  */
1156 void
1157 delist_dev(struct cdev *dev)
1158 {
1159 
1160  WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "delist_dev");
1161  dev_lock();
1162  delist_dev_locked(dev);
1163  dev_unlock();
1164 }
1165 
1166 void
1167 destroy_dev(struct cdev *dev)
1168 {
1169 
1170  WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev");
1171  dev_lock();
1172  destroy_devl(dev);
1174 }
1175 
1176 const char *
1177 devtoname(struct cdev *dev)
1178 {
1179 
1180  return (dev->si_name);
1181 }
1182 
1183 int
1184 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
1185 {
1186  int u, i;
1187 
1188  i = strlen(stem);
1189  if (bcmp(stem, name, i) != 0)
1190  return (0);
1191  if (!isdigit(name[i]))
1192  return (0);
1193  u = 0;
1194  if (name[i] == '0' && isdigit(name[i+1]))
1195  return (0);
1196  while (isdigit(name[i])) {
1197  u *= 10;
1198  u += name[i++] - '0';
1199  }
1200  if (u > 0xffffff)
1201  return (0);
1202  *unit = u;
1203  if (namep)
1204  *namep = &name[i];
1205  if (name[i])
1206  return (2);
1207  return (1);
1208 }
1209 
1210 /*
1211  * Helper functions for cloning device drivers.
1212  *
1213  * The objective here is to make it unnecessary for the device drivers to
1214  * use rman or similar to manage their unit number space. Due to the way
1215  * we do "on-demand" devices, using rman or other "private" methods
1216  * will be very tricky to lock down properly once we lock down this file.
1217  *
1218  * Instead we give the drivers these routines which puts the struct cdev *'s
1219  * that are to be managed on their own list, and gives the driver the ability
1220  * to ask for the first free unit number or a given specified unit number.
1221  *
1222  * In addition these routines support paired devices (pty, nmdm and similar)
1223  * by respecting a number of "flag" bits in the minor number.
1224  *
1225  */
1226 
1227 struct clonedevs {
1228  LIST_HEAD(,cdev) head;
1229 };
1230 
1231 void
1232 clone_setup(struct clonedevs **cdp)
1233 {
1234 
1235  *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
1236  LIST_INIT(&(*cdp)->head);
1237 }
1238 
1239 int
1240 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up,
1241  struct cdev **dp, int extra)
1242 {
1243  struct clonedevs *cd;
1244  struct cdev *dev, *ndev, *dl, *de;
1245  int unit, low, u;
1246 
1247  KASSERT(*cdp != NULL,
1248  ("clone_setup() not called in driver \"%s\"", csw->d_name));
1249  KASSERT(!(extra & CLONE_UNITMASK),
1250  ("Illegal extra bits (0x%x) in clone_create", extra));
1251  KASSERT(*up <= CLONE_UNITMASK,
1252  ("Too high unit (0x%x) in clone_create", *up));
1253  KASSERT(csw->d_flags & D_NEEDMINOR,
1254  ("clone_create() on cdevsw without minor numbers"));
1255 
1256 
1257  /*
1258  * Search the list for a lot of things in one go:
1259  * A preexisting match is returned immediately.
1260  * The lowest free unit number if we are passed -1, and the place
1261  * in the list where we should insert that new element.
1262  * The place to insert a specified unit number, if applicable
1263  * the end of the list.
1264  */
1265  unit = *up;
1266  ndev = devfs_alloc(MAKEDEV_WAITOK);
1267  dev_lock();
1268  prep_cdevsw(csw, MAKEDEV_WAITOK);
1269  low = extra;
1270  de = dl = NULL;
1271  cd = *cdp;
1272  LIST_FOREACH(dev, &cd->head, si_clone) {
1273  KASSERT(dev->si_flags & SI_CLONELIST,
1274  ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
1275  u = dev2unit(dev);
1276  if (u == (unit | extra)) {
1277  *dp = dev;
1278  dev_unlock();
1279  devfs_free(ndev);
1280  return (0);
1281  }
1282  if (unit == -1 && u == low) {
1283  low++;
1284  de = dev;
1285  continue;
1286  } else if (u < (unit | extra)) {
1287  de = dev;
1288  continue;
1289  } else if (u > (unit | extra)) {
1290  dl = dev;
1291  break;
1292  }
1293  }
1294  if (unit == -1)
1295  unit = low & CLONE_UNITMASK;
1296  dev = newdev(csw, unit | extra, ndev);
1297  if (dev->si_flags & SI_CLONELIST) {
1298  printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
1299  printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
1300  LIST_FOREACH(dev, &cd->head, si_clone) {
1301  printf("\t%p %s\n", dev, dev->si_name);
1302  }
1303  panic("foo");
1304  }
1305  KASSERT(!(dev->si_flags & SI_CLONELIST),
1306  ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
1307  if (dl != NULL)
1308  LIST_INSERT_BEFORE(dl, dev, si_clone);
1309  else if (de != NULL)
1310  LIST_INSERT_AFTER(de, dev, si_clone);
1311  else
1312  LIST_INSERT_HEAD(&cd->head, dev, si_clone);
1313  dev->si_flags |= SI_CLONELIST;
1314  *up = unit;
1316  return (1);
1317 }
1318 
1319 /*
1320  * Kill everything still on the list. The driver should already have
1321  * disposed of any softc hung of the struct cdev *'s at this time.
1322  */
1323 void
1325 {
1326  struct cdev *dev;
1327  struct cdev_priv *cp;
1328  struct clonedevs *cd;
1329 
1330  cd = *cdp;
1331  if (cd == NULL)
1332  return;
1333  dev_lock();
1334  while (!LIST_EMPTY(&cd->head)) {
1335  dev = LIST_FIRST(&cd->head);
1336  LIST_REMOVE(dev, si_clone);
1337  KASSERT(dev->si_flags & SI_CLONELIST,
1338  ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
1339  dev->si_flags &= ~SI_CLONELIST;
1340  cp = cdev2priv(dev);
1341  if (!(cp->cdp_flags & CDP_SCHED_DTR)) {
1342  cp->cdp_flags |= CDP_SCHED_DTR;
1343  KASSERT(dev->si_flags & SI_NAMED,
1344  ("Driver has goofed in cloning underways udev %x unit %x", dev2udev(dev), dev2unit(dev)));
1345  destroy_devl(dev);
1346  }
1347  }
1349  free(cd, M_DEVBUF);
1350  *cdp = NULL;
1351 }
1352 
1353 static TAILQ_HEAD(, cdev_priv) dev_ddtr =
1354  TAILQ_HEAD_INITIALIZER(dev_ddtr);
1355 static struct task dev_dtr_task;
1356 
1357 static void
1358 destroy_dev_tq(void *ctx, int pending)
1359 {
1360  struct cdev_priv *cp;
1361  struct cdev *dev;
1362  void (*cb)(void *);
1363  void *cb_arg;
1364 
1365  dev_lock();
1366  while (!TAILQ_EMPTY(&dev_ddtr)) {
1367  cp = TAILQ_FIRST(&dev_ddtr);
1368  dev = &cp->cdp_c;
1369  KASSERT(cp->cdp_flags & CDP_SCHED_DTR,
1370  ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp));
1371  TAILQ_REMOVE(&dev_ddtr, cp, cdp_dtr_list);
1372  cb = cp->cdp_dtr_cb;
1373  cb_arg = cp->cdp_dtr_cb_arg;
1374  destroy_devl(dev);
1376  dev_rel(dev);
1377  if (cb != NULL)
1378  cb(cb_arg);
1379  dev_lock();
1380  }
1381  dev_unlock();
1382 }
1383 
1384 /*
1385  * devmtx shall be locked on entry. devmtx will be unlocked after
1386  * function return.
1387  */
1388 static int
1389 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg)
1390 {
1391  struct cdev_priv *cp;
1392 
1393  mtx_assert(&devmtx, MA_OWNED);
1394  cp = cdev2priv(dev);
1395  if (cp->cdp_flags & CDP_SCHED_DTR) {
1396  dev_unlock();
1397  return (0);
1398  }
1399  dev_refl(dev);
1400  cp->cdp_flags |= CDP_SCHED_DTR;
1401  cp->cdp_dtr_cb = cb;
1402  cp->cdp_dtr_cb_arg = arg;
1403  TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list);
1404  dev_unlock();
1405  taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task);
1406  return (1);
1407 }
1408 
1409 int
1410 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg)
1411 {
1412 
1413  dev_lock();
1414  return (destroy_dev_sched_cbl(dev, cb, arg));
1415 }
1416 
1417 int
1418 destroy_dev_sched(struct cdev *dev)
1419 {
1420 
1421  return (destroy_dev_sched_cb(dev, NULL, NULL));
1422 }
1423 
1424 void
1425 destroy_dev_drain(struct cdevsw *csw)
1426 {
1427 
1428  dev_lock();
1429  while (!LIST_EMPTY(&csw->d_devs)) {
1430  msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10);
1431  }
1432  dev_unlock();
1433 }
1434 
1435 void
1437 {
1438 
1439  sx_xlock(&clone_drain_lock);
1440  sx_xunlock(&clone_drain_lock);
1441 }
1442 
1443 static void
1444 devdtr_init(void *dummy __unused)
1445 {
1446 
1447  TASK_INIT(&dev_dtr_task, 0, destroy_dev_tq, NULL);
1448 }
1449 
1450 SYSINIT(devdtr, SI_SUB_DEVFS, SI_ORDER_SECOND, devdtr_init, NULL);
1451 
1452 #include "opt_ddb.h"
1453 #ifdef DDB
1454 #include <sys/kernel.h>
1455 
1456 #include <ddb/ddb.h>
1457 
1458 DB_SHOW_COMMAND(cdev, db_show_cdev)
1459 {
1460  struct cdev_priv *cdp;
1461  struct cdev *dev;
1462  u_int flags;
1463  char buf[512];
1464 
1465  if (!have_addr) {
1466  TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) {
1467  dev = &cdp->cdp_c;
1468  db_printf("%s %p\n", dev->si_name, dev);
1469  if (db_pager_quit)
1470  break;
1471  }
1472  return;
1473  }
1474 
1475  dev = (struct cdev *)addr;
1476  cdp = cdev2priv(dev);
1477  db_printf("dev %s ref %d use %ld thr %ld inuse %u fdpriv %p\n",
1478  dev->si_name, dev->si_refcount, dev->si_usecount,
1479  dev->si_threadcount, cdp->cdp_inuse, cdp->cdp_fdpriv.lh_first);
1480  db_printf("devsw %p si_drv0 %d si_drv1 %p si_drv2 %p\n",
1481  dev->si_devsw, dev->si_drv0, dev->si_drv1, dev->si_drv2);
1482  flags = dev->si_flags;
1483 #define SI_FLAG(flag) do { \
1484  if (flags & (flag)) { \
1485  if (buf[0] != '\0') \
1486  strlcat(buf, ", ", sizeof(buf)); \
1487  strlcat(buf, (#flag) + 3, sizeof(buf)); \
1488  flags &= ~(flag); \
1489  } \
1490 } while (0)
1491  buf[0] = '\0';
1492  SI_FLAG(SI_ETERNAL);
1493  SI_FLAG(SI_ALIAS);
1494  SI_FLAG(SI_NAMED);
1495  SI_FLAG(SI_CHEAPCLONE);
1496  SI_FLAG(SI_CHILD);
1497  SI_FLAG(SI_DEVOPEN);
1498  SI_FLAG(SI_CONSOPEN);
1499  SI_FLAG(SI_DUMPDEV);
1500  SI_FLAG(SI_CANDELETE);
1501  SI_FLAG(SI_CLONELIST);
1502  db_printf("si_flags %s\n", buf);
1503 
1504  flags = cdp->cdp_flags;
1505 #define CDP_FLAG(flag) do { \
1506  if (flags & (flag)) { \
1507  if (buf[0] != '\0') \
1508  strlcat(buf, ", ", sizeof(buf)); \
1509  strlcat(buf, (#flag) + 4, sizeof(buf)); \
1510  flags &= ~(flag); \
1511  } \
1512 } while (0)
1513  buf[0] = '\0';
1514  CDP_FLAG(CDP_ACTIVE);
1515  CDP_FLAG(CDP_SCHED_DTR);
1516  db_printf("cdp_flags %s\n", buf);
1517 }
1518 #endif
int make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev, struct cdev *old_alias, const char *physpath)
Definition: kern_conf.c:969
void drain_dev_clone_events(void)
Definition: kern_conf.c:1436
int vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
Definition: subr_prf.c:541
#define dead_mmap_single
Definition: kern_conf.c:307
int destroy_dev_sched_cb(struct cdev *dev, void(*cb)(void *), void *arg)
Definition: kern_conf.c:1410
int dev_stdclone(char *name, char **namep, const char *stem, int *unit)
Definition: kern_conf.c:1184
void dev_refl(struct cdev *dev)
Definition: kern_conf.c:149
struct buf * buf
Definition: vfs_bio.c:97
static void no_strategy(struct bio *bp)
Definition: kern_conf.c:337
#define no_read
Definition: kern_conf.c:329
static int giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
Definition: kern_conf.c:385
#define dead_open
Definition: kern_conf.c:290
void dev_rel(struct cdev *dev)
Definition: kern_conf.c:157
int mode
struct cdev * make_dev_alias(struct cdev *pdev, const char *fmt,...)
Definition: kern_conf.c:940
struct cdevsw * dev_refthread(struct cdev *dev, int *ref)
Definition: kern_conf.c:182
#define dead_dump
Definition: kern_conf.c:305
int sprintf(char *buf, const char *cfmt,...)
Definition: subr_prf.c:480
static LIST_HEAD(alq)
Definition: kern_alq.c:97
void * malloc(unsigned long size, struct malloc_type *mtp, int flags)
Definition: kern_malloc.c:454
void clone_cleanup(struct clonedevs **cdp)
Definition: kern_conf.c:1324
static int giant_poll(struct cdev *dev, int events, struct thread *td)
Definition: kern_conf.c:468
void dev_depends(struct cdev *pdev, struct cdev *cdev)
Definition: kern_conf.c:890
void panic(const char *fmt,...)
static int giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size, vm_object_t *object, int nprot)
Definition: kern_conf.c:518
void clone_setup(struct clonedevs **cdp)
Definition: kern_conf.c:1232
void knote(struct knlist *list, long hint, int lockflags)
Definition: kern_event.c:1806
void dev_relthread(struct cdev *dev, int ref)
Definition: kern_conf.c:249
struct cdev * make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
Definition: kern_conf.c:826
static void devdtr_init(void *dummy __unused)
Definition: kern_conf.c:1444
static void dev_free_devlocked(struct cdev *cdev)
Definition: kern_conf.c:112
static int make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev, const char *fmt, va_list ap)
Definition: kern_conf.c:899
const char * name
Definition: kern_fail.c:97
static TAILQ_HEAD(cdev_priv)
Definition: kern_conf.c:1353
int eopnotsupp(void)
Definition: kern_conf.c:270
struct cdev * make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
Definition: kern_conf.c:843
int nullop(void)
Definition: kern_conf.c:263
struct cdev * make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode, const char *fmt,...)
Definition: kern_conf.c:809
static void notify(struct cdev *dev, const char *ev, int flags)
Definition: kern_conf.c:536
void delist_dev(struct cdev *dev)
Definition: kern_conf.c:1157
int make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev, const char *fmt,...)
Definition: kern_conf.c:956
void biofinish(struct bio *bp, struct devstat *stat, int error)
Definition: vfs_bio.c:3699
static int destroy_dev_sched_cbl(struct cdev *dev, void(*cb)(void *), void *arg)
Definition: kern_conf.c:1389
static int giant_write(struct cdev *dev, struct uio *uio, int ioflag)
Definition: kern_conf.c:452
void destroy_dev(struct cdev *dev)
Definition: kern_conf.c:1167
static int giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, vm_memattr_t *memattr)
Definition: kern_conf.c:500
static void fini_cdevsw(struct cdevsw *devsw)
Definition: kern_conf.c:591
static void giant_strategy(struct bio *bp)
Definition: kern_conf.c:401
static int prep_devname(struct cdev *dev, const char *fmt, va_list ap)
Definition: kern_conf.c:686
static SLIST_HEAD(free_cdevsw, cdevsw)
Definition: kern_conf.c:64
static int dummy
static struct cdev * newdev(struct cdevsw *csw, int unit, struct cdev *si)
Definition: kern_conf.c:570
#define FIXUP(member, noop, giant)
static int giant_kqfilter(struct cdev *dev, struct knote *kn)
Definition: kern_conf.c:484
static MALLOC_DEFINE(M_DEVT,"cdev","cdev storage")
static int giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
Definition: kern_conf.c:353
struct mtx Giant
Definition: kern_mutex.c:140
#define dead_mmap
Definition: kern_conf.c:296
#define null_open
Definition: kern_conf.c:327
#define dead_ioctl
Definition: kern_conf.c:294
static int prep_cdevsw(struct cdevsw *devsw, int flags)
Definition: kern_conf.c:605
static int giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp)
Definition: kern_conf.c:369
#define no_kqfilter
Definition: kern_conf.c:333
static int giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
Definition: kern_conf.c:420
#define dead_close
Definition: kern_conf.c:291
int poll_no_poll(int events)
Definition: sys_generic.c:774
void dev_ref(struct cdev *dev)
Definition: kern_conf.c:139
void clean_unrhdrl(struct unrhdr *uh)
Definition: subr_unit.c:295
#define no_ioctl
Definition: kern_conf.c:331
static void dev_dependsl(struct cdev *pdev, struct cdev *cdev)
Definition: kern_conf.c:880
__FBSDID("$BSDSUniX$")
#define no_mmap
Definition: kern_conf.c:332
static int no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
Definition: kern_conf.c:344
#define dead_read
Definition: kern_conf.c:292
static struct cdevsw dead_cdevsw
Definition: kern_conf.c:309
#define dead_kqfilter
Definition: kern_conf.c:306
struct cdevsw * devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref)
Definition: kern_conf.c:207
#define dead_poll
Definition: kern_conf.c:295
#define null_close
Definition: kern_conf.c:328
struct ucred * crhold(struct ucred *cr)
Definition: kern_prot.c:1824
#define no_mmap_single
Definition: kern_conf.c:334
SYSINIT(devdtr, SI_SUB_DEVFS, SI_ORDER_SECOND, devdtr_init, NULL)
void devctl_notify_f(const char *system, const char *subsystem, const char *type, const char *data, int flags)
Send a 'notification' to userland, using standard ways.
Definition: subr_bus.c:603
int make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,...)
Definition: kern_conf.c:862
void free(void *addr, struct malloc_type *mtp)
Definition: kern_malloc.c:554
int printf(const char *fmt,...)
Definition: subr_prf.c:367
int taskqueue_enqueue(struct taskqueue *queue, struct task *task)
void dev_unlock(void)
Definition: kern_conf.c:132
int clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, int extra)
Definition: kern_conf.c:1240
static void dead_strategy(struct bio *bp)
Definition: kern_conf.c:299
struct mtx devmtx
Definition: kern_conf.c:54
#define no_dump
Definition: kern_conf.c:350
void wakeup(void *ident)
Definition: kern_synch.c:378
static int enxio(void)
Definition: kern_conf.c:277
static void notify_destroy(struct cdev *dev)
Definition: kern_conf.c:563
static void dev_unlock_and_free(void)
Definition: kern_conf.c:81
static void cdevsw_free_devlocked(struct cdevsw *csw)
Definition: kern_conf.c:124
static int giant_read(struct cdev *dev, struct uio *uio, int ioflag)
Definition: kern_conf.c:436
const char * devtoname(struct cdev *dev)
Definition: kern_conf.c:1177
static void delist_dev_locked(struct cdev *dev)
Definition: kern_conf.c:1128
int destroy_dev_sched(struct cdev *dev)
Definition: kern_conf.c:1418
#define dead_write
Definition: kern_conf.c:293
static void destroy_devl(struct cdev *dev)
Definition: kern_conf.c:1031
void destroy_dev_drain(struct cdevsw *csw)
Definition: kern_conf.c:1425
static void notify_create(struct cdev *dev, int flags)
Definition: kern_conf.c:556
#define no_write
Definition: kern_conf.c:330
int flag
static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, va_list ap)
Definition: kern_conf.c:736
static int enodev(void)
Definition: kern_conf.c:283
int hz
Definition: subr_param.c:84
static struct cdev_priv_list cdevp_free_list
Definition: kern_conf.c:62