patch-2.4.7 linux/fs/devfs/util.c
Next file: linux/fs/dquot.c
Previous file: linux/fs/devfs/base.c
Back to the patch index
Back to the overall index
- Lines: 319
- Date:
Wed Jul 11 14:55:41 2001
- Orig file:
v2.4.6/linux/fs/devfs/util.c
- Orig date:
Tue Jul 3 17:08:21 2001
diff -u --recursive --new-file v2.4.6/linux/fs/devfs/util.c linux/fs/devfs/util.c
@@ -35,12 +35,18 @@
Took account of interface change to <devfs_mk_dir>.
20010519 Richard Gooch <rgooch@atnf.csiro.au>
Documentation cleanup.
+ 20010709 Richard Gooch <rgooch@atnf.csiro.au>
+ Created <devfs_*alloc_major> and <devfs_*alloc_devnum>.
+ 20010710 Richard Gooch <rgooch@atnf.csiro.au>
+ Created <devfs_*alloc_unique_number>.
*/
#include <linux/module.h>
#include <linux/init.h>
-#include <linux/locks.h>
-#include <linux/kdev_t.h>
#include <linux/devfs_fs_kernel.h>
+#include <linux/malloc.h>
+#include <linux/vmalloc.h>
+
+#include <asm/bitops.h>
/* Private functions follow */
@@ -166,3 +172,297 @@
}
} /* End Function devfs_register_series */
EXPORT_SYMBOL(devfs_register_series);
+
+
+struct major_list
+{
+ spinlock_t lock;
+ __u32 bits[8];
+};
+
+/* Block majors already assigned:
+ 0-3, 7-9, 11-12, 13-63, 65-93, 95-99, 101, 103-111, 120-127, 199, 201,
+ 240-255
+*/
+static struct major_list block_major_list =
+{SPIN_LOCK_UNLOCKED,
+ {0xfffffb8f, /* Majors 0 to 31 */
+ 0xffffffff, /* Majors 32 to 63 */
+ 0xbffffffe, /* Majors 64 to 95 */
+ 0xff00ffaf, /* Majors 96 to 127 */
+ 0x00000000, /* Majors 128 to 159 */
+ 0x00000000, /* Majors 160 to 191 */
+ 0x00000280, /* Majors 192 to 223 */
+ 0xffff0000} /* Majors 224 to 255 */
+};
+
+/* Char majors already assigned:
+ 0-7, 9-151, 154-158, 160-195, 198-211, 216-221, 224-225, 240-255
+*/
+static struct major_list char_major_list =
+{SPIN_LOCK_UNLOCKED,
+ {0xfffffeff, /* Majors 0 to 31 */
+ 0xffffffff, /* Majors 32 to 63 */
+ 0xffffffff, /* Majors 64 to 95 */
+ 0xffffffff, /* Majors 96 to 127 */
+ 0x7cffffff, /* Majors 128 to 159 */
+ 0xffffffff, /* Majors 160 to 191 */
+ 0x3f0fffcf, /* Majors 192 to 223 */
+ 0xffff0003} /* Majors 224 to 255 */
+};
+
+
+/**
+ * devfs_alloc_major - Allocate a major number.
+ * @type: The type of the major (DEVFS_SPECIAL_CHR or DEVFS_SPECIAL_BLOCK)
+
+ * Returns the allocated major, else -1 if none are available.
+ * This routine is thread safe and does not block.
+ */
+
+int devfs_alloc_major (char type)
+{
+ int major;
+ struct major_list *list;
+
+ list = (type == DEVFS_SPECIAL_CHR) ? &char_major_list : &block_major_list;
+ spin_lock (&list->lock);
+ major = find_first_zero_bit (list->bits, 256);
+ if (major < 256) __set_bit (major, list->bits);
+ else major = -1;
+ spin_unlock (&list->lock);
+ return major;
+} /* End Function devfs_alloc_major */
+EXPORT_SYMBOL(devfs_alloc_major);
+
+
+/**
+ * devfs_dealloc_major - Deallocate a major number.
+ * @type: The type of the major (DEVFS_SPECIAL_CHR or DEVFS_SPECIAL_BLOCK)
+ * @major: The major number.
+ * This routine is thread safe and does not block.
+ */
+
+void devfs_dealloc_major (char type, int major)
+{
+ int was_set;
+ struct major_list *list;
+
+ if (major < 0) return;
+ list = (type == DEVFS_SPECIAL_CHR) ? &char_major_list : &block_major_list;
+ spin_lock (&list->lock);
+ was_set = __test_and_clear_bit (major, list->bits);
+ spin_unlock (&list->lock);
+ if (!was_set)
+ printk (KERN_ERR __FUNCTION__ "(): major %d was already free\n",
+ major);
+} /* End Function devfs_dealloc_major */
+EXPORT_SYMBOL(devfs_dealloc_major);
+
+
+struct minor_list
+{
+ int major;
+ __u32 bits[8];
+ struct minor_list *next;
+};
+
+struct device_list
+{
+ struct minor_list *first, *last;
+ int none_free;
+};
+
+DECLARE_MUTEX (block_semaphore);
+static struct device_list block_list;
+
+DECLARE_MUTEX (char_semaphore);
+static struct device_list char_list;
+
+
+/**
+ * devfs_alloc_devnum - Allocate a device number.
+ * @type: The type (DEVFS_SPECIAL_CHR or DEVFS_SPECIAL_BLOCK).
+ *
+ * Returns the allocated device number, else NODEV if none are available.
+ * This routine is thread safe and may block.
+ */
+
+kdev_t devfs_alloc_devnum (char type)
+{
+ int minor;
+ struct semaphore *semaphore;
+ struct device_list *list;
+ struct minor_list *entry;
+
+ if (type == DEVFS_SPECIAL_CHR)
+ {
+ semaphore = &char_semaphore;
+ list = &char_list;
+ }
+ else
+ {
+ semaphore = &block_semaphore;
+ list = &block_list;
+ }
+ if (list->none_free) return NODEV; /* Fast test */
+ down (semaphore);
+ if (list->none_free)
+ {
+ up (semaphore);
+ return NODEV;
+ }
+ for (entry = list->first; entry != NULL; entry = entry->next)
+ {
+ minor = find_first_zero_bit (entry->bits, 256);
+ if (minor >= 256) continue;
+ __set_bit (minor, entry->bits);
+ up (semaphore);
+ return MKDEV (entry->major, minor);
+ }
+ /* Need to allocate a new major */
+ if ( ( entry = kmalloc (sizeof *entry, GFP_KERNEL) ) == NULL )
+ {
+ list->none_free = 1;
+ up (semaphore);
+ return NODEV;
+ }
+ memset (entry, 0, sizeof *entry);
+ if ( ( entry->major = devfs_alloc_major (type) ) < 0 )
+ {
+ list->none_free = 1;
+ up (semaphore);
+ kfree (entry);
+ return NODEV;
+ }
+ __set_bit (0, entry->bits);
+ if (list->first == NULL) list->first = entry;
+ else list->last->next = entry;
+ list->last = entry;
+ up (semaphore);
+ return MKDEV (entry->major, 0);
+} /* End Function devfs_alloc_devnum */
+EXPORT_SYMBOL(devfs_alloc_devnum);
+
+
+/**
+ * devfs_dealloc_devnum - Dellocate a device number.
+ * @type: The type (DEVFS_SPECIAL_CHR or DEVFS_SPECIAL_BLOCK).
+ * @devnum: The device number.
+ *
+ * This routine is thread safe and does not block.
+ */
+
+void devfs_dealloc_devnum (char type, kdev_t devnum)
+{
+ int major, minor;
+ struct semaphore *semaphore;
+ struct device_list *list;
+ struct minor_list *entry;
+
+ if (devnum == NODEV) return;
+ if (type == DEVFS_SPECIAL_CHR)
+ {
+ semaphore = &char_semaphore;
+ list = &char_list;
+ }
+ else
+ {
+ semaphore = &block_semaphore;
+ list = &block_list;
+ }
+ major = MAJOR (devnum);
+ minor = MINOR (devnum);
+ down (semaphore);
+ for (entry = list->first; entry != NULL; entry = entry->next)
+ {
+ int was_set;
+
+ if (entry->major != major) continue;
+ was_set = __test_and_clear_bit (minor, entry->bits);
+ if (was_set) list->none_free = 0;
+ up (semaphore);
+ if (!was_set)
+ printk ( KERN_ERR __FUNCTION__ "(): device %s was already free\n",
+ kdevname (devnum) );
+ return;
+ }
+ up (semaphore);
+ printk ( KERN_ERR __FUNCTION__ "(): major for %s not previously allocated\n",
+ kdevname (devnum) );
+} /* End Function devfs_dealloc_devnum */
+EXPORT_SYMBOL(devfs_dealloc_devnum);
+
+
+/**
+ * devfs_alloc_unique_number - Allocate a unique (positive) number.
+ * @space: The number space to allocate from.
+ *
+ * Returns the allocated unique number, else a negative error code.
+ * This routine is thread safe and may block.
+ */
+
+int devfs_alloc_unique_number (struct unique_numspace *space)
+{
+ int number;
+ unsigned int length;
+ __u32 *bits;
+
+ /* Get around stupid lack of semaphore initialiser */
+ spin_lock (&space->init_lock);
+ if (!space->sem_initialised)
+ {
+ sema_init (&space->semaphore, 1);
+ space->sem_initialised = 1;
+ }
+ spin_unlock (&space->init_lock);
+ down (&space->semaphore);
+ if (space->num_free < 1)
+ {
+ if (space->length < 16) length = 16;
+ else length = space->length << 1;
+ if ( ( bits = vmalloc (length) ) == NULL )
+ {
+ up (&space->semaphore);
+ return -ENOMEM;
+ }
+ if (space->bits != NULL)
+ {
+ memcpy (bits, space->bits, space->length);
+ vfree (space->bits);
+ }
+ space->num_free = (length - space->length) << 3;
+ space->bits = bits;
+ memset (bits + space->length, 0, length - space->length);
+ space->length = length;
+ }
+ number = find_first_zero_bit (space->bits, space->length << 3);
+ __set_bit (number, space->bits);
+ up (&space->semaphore);
+ return number;
+} /* End Function devfs_alloc_unique_number */
+EXPORT_SYMBOL(devfs_alloc_unique_number);
+
+
+/**
+ * devfs_dealloc_unique_number - Deallocate a unique (positive) number.
+ * @space: The number space to deallocate from.
+ * @number: The number to deallocate.
+ *
+ * This routine is thread safe and may block.
+ */
+
+void devfs_dealloc_unique_number (struct unique_numspace *space, int number)
+{
+ int was_set;
+
+ if (number < 0) return;
+ down (&space->semaphore);
+ was_set = __test_and_clear_bit (number, space->bits);
+ if (was_set) ++space->num_free;
+ up (&space->semaphore);
+ if (!was_set)
+ printk (KERN_ERR __FUNCTION__ "(): number %d was already free\n",
+ number);
+} /* End Function devfs_dealloc_unique_number */
+EXPORT_SYMBOL(devfs_dealloc_unique_number);
FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)