subr_xxx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* $OpenBSD: subr_xxx.c,v 1.14 2015/03/14 03:38:50 jsg Exp $ */
  2. /* $NetBSD: subr_xxx.c,v 1.10 1996/02/04 02:16:51 christos Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)subr_xxx.c 8.1 (Berkeley) 6/10/93
  32. */
  33. /*
  34. * Miscellaneous trivial functions, including many
  35. * that are often inline-expanded or done in assembler.
  36. */
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/conf.h>
  40. /*
  41. * Unsupported device function (e.g. writing to read-only device).
  42. */
  43. int
  44. enodev(void)
  45. {
  46. return (ENODEV);
  47. }
  48. /*
  49. * Unconfigured device function; driver not configured.
  50. */
  51. int
  52. enxio(void)
  53. {
  54. return (ENXIO);
  55. }
  56. /*
  57. * Unsupported ioctl function.
  58. */
  59. int
  60. enoioctl(void)
  61. {
  62. return (ENOTTY);
  63. }
  64. /*
  65. * Unsupported system function.
  66. * This is used for an otherwise-reasonable operation
  67. * that is not supported by the current system binary.
  68. */
  69. int
  70. enosys(void)
  71. {
  72. return (ENOSYS);
  73. }
  74. /*
  75. * Return error for operation not supported
  76. * on a specific object or file type.
  77. */
  78. /*ARGSUSED*/
  79. int
  80. eopnotsupp(void *v)
  81. {
  82. return (EOPNOTSUPP);
  83. }
  84. /*
  85. * Generic null operation, always returns success.
  86. */
  87. /*ARGSUSED*/
  88. int
  89. nullop(void *v)
  90. {
  91. return (0);
  92. }
  93. struct bdevsw *
  94. bdevsw_lookup(dev_t dev)
  95. {
  96. return (&bdevsw[major(dev)]);
  97. }
  98. struct cdevsw *
  99. cdevsw_lookup(dev_t dev)
  100. {
  101. return (&cdevsw[major(dev)]);
  102. }
  103. /*
  104. * Convert a character device number to a block device number.
  105. */
  106. dev_t
  107. chrtoblk(dev_t dev)
  108. {
  109. int blkmaj;
  110. if (major(dev) >= nchrdev || major(dev) >= nchrtoblktbl)
  111. return (NODEV);
  112. blkmaj = chrtoblktbl[major(dev)];
  113. if (blkmaj == NODEV)
  114. return (NODEV);
  115. return (makedev(blkmaj, minor(dev)));
  116. }
  117. /*
  118. * Convert a block device number to a character device number.
  119. */
  120. dev_t
  121. blktochr(dev_t dev)
  122. {
  123. int blkmaj = major(dev);
  124. int i;
  125. if (blkmaj >= nblkdev)
  126. return (NODEV);
  127. for (i = 0; i < nchrtoblktbl; i++)
  128. if (blkmaj == chrtoblktbl[i])
  129. return (makedev(i, minor(dev)));
  130. return (NODEV);
  131. }
  132. /*
  133. * Check that we're in a context where it's okay to sleep.
  134. */
  135. void
  136. assertwaitok(void)
  137. {
  138. splassert(IPL_NONE);
  139. #ifdef DIAGNOSTIC
  140. if (curcpu()->ci_mutex_level != 0)
  141. panic("assertwaitok: non-zero mutex count: %d",
  142. curcpu()->ci_mutex_level);
  143. #endif
  144. }