+/*
+ * Copy the "hostname" variable into a caller-provided buffer
+ * Returns: 0 for success, ENAMETOOLONG for insufficient buffer space.
+ * On success, "len" will be set to the number of characters preceding
+ * the NULL character in the hostname.
+ */
+int
+bsd_hostname(char *buf, int bufsize, int *len)
+{
+ /*
+ * "hostname" is null-terminated, and "hostnamelen" is equivalent to strlen(hostname).
+ */
+ if (hostnamelen < bufsize) {
+ strlcpy(buf, hostname, bufsize);
+ *len = hostnamelen;
+ return 0;
+ } else {
+ return ENAMETOOLONG;
+ }
+}
+
+void
+devsw_lock(dev_t dev, int mode)
+{
+ devsw_lock_t newlock, tmplock;
+ int res;
+
+ assert(0 <= major(dev) && major(dev) < nchrdev);
+ assert(mode == S_IFCHR || mode == S_IFBLK);
+
+ MALLOC(newlock, devsw_lock_t, sizeof(struct devsw_lock), M_TEMP, M_WAITOK | M_ZERO);
+ newlock->dl_dev = dev;
+ newlock->dl_thread = current_thread();
+ newlock->dl_mode = mode;
+
+ lck_mtx_lock_spin(&devsw_lock_list_mtx);
+retry:
+ TAILQ_FOREACH(tmplock, &devsw_locks, dl_list) {
+ if (tmplock->dl_dev == dev && tmplock->dl_mode == mode) {
+ res = msleep(tmplock, &devsw_lock_list_mtx, PVFS, "devsw_lock", NULL);
+ assert(res == 0);
+ goto retry;
+ }
+ }
+
+ TAILQ_INSERT_TAIL(&devsw_locks, newlock, dl_list);
+ lck_mtx_unlock(&devsw_lock_list_mtx);
+
+}
+void
+devsw_unlock(dev_t dev, int mode)
+{
+ devsw_lock_t tmplock;
+
+ assert(0 <= major(dev) && major(dev) < nchrdev);
+
+ lck_mtx_lock_spin(&devsw_lock_list_mtx);
+
+ TAILQ_FOREACH(tmplock, &devsw_locks, dl_list) {
+ if (tmplock->dl_dev == dev && tmplock->dl_mode == mode) {
+ break;
+ }
+ }
+
+ if (tmplock == NULL) {
+ panic("Trying to unlock, and couldn't find lock.");
+ }
+
+ if (tmplock->dl_thread != current_thread()) {
+ panic("Trying to unlock, but I don't hold the lock.");
+ }
+
+ wakeup(tmplock);
+ TAILQ_REMOVE(&devsw_locks, tmplock, dl_list);
+
+ lck_mtx_unlock(&devsw_lock_list_mtx);
+
+ FREE(tmplock, M_TEMP);
+}
+
+void
+devsw_init()
+{
+ devsw_lock_grp = lck_grp_alloc_init("devsw", NULL);
+ assert(devsw_lock_grp != NULL);
+
+ lck_mtx_init(&devsw_lock_list_mtx, devsw_lock_grp, NULL);
+ TAILQ_INIT(&devsw_locks);
+}