fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / char / drm / drm_auth.c
index b428761..c7b19d3 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * \file drm_auth.
+ * \file drm_auth.c
  * IOCTLs for authentication
  *
  * \author Rickard E. (Rik) Faith <faith@valinux.com>
 
 #include "drmP.h"
 
-/**
- * Generate a hash key from a magic.
- *
- * \param magic magic.
- * \return hash key.
- *
- * The key is the modulus of the hash table size, #DRM_HASH_SIZE, which must be
- * a power of 2.
- */
-static int drm_hash_magic(drm_magic_t magic)
-{
-       return magic & (DRM_HASH_SIZE-1);
-}
-
 /**
  * Find the file with the given magic number.
  *
@@ -56,105 +42,86 @@ static int drm_hash_magic(drm_magic_t magic)
  * \param magic magic number.
  *
  * Searches in drm_device::magiclist within all files with the same hash key
- * the one with matching magic number, while holding the drm_device::struct_sem
+ * the one with matching magic number, while holding the drm_device::struct_mutex
  * lock.
  */
-static drm_file_t *drm_find_file(drm_device_t *dev, drm_magic_t magic)
+static drm_file_t *drm_find_file(drm_device_t * dev, drm_magic_t magic)
 {
-       drm_file_t        *retval = NULL;
+       drm_file_t *retval = NULL;
        drm_magic_entry_t *pt;
-       int               hash    = drm_hash_magic(magic);
-
-       down(&dev->struct_sem);
-       for (pt = dev->magiclist[hash].head; pt; pt = pt->next) {
-               if (pt->magic == magic) {
-                       retval = pt->priv;
-                       break;
-               }
+       drm_hash_item_t *hash;
+
+       mutex_lock(&dev->struct_mutex);
+       if (!drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
+               pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item);
+               retval = pt->priv;
        }
-       up(&dev->struct_sem);
+       mutex_unlock(&dev->struct_mutex);
        return retval;
 }
 
 /**
  * Adds a magic number.
- * 
+ *
  * \param dev DRM device.
  * \param priv file private data.
  * \param magic magic number.
  *
  * Creates a drm_magic_entry structure and appends to the linked list
  * associated the magic number hash key in drm_device::magiclist, while holding
- * the drm_device::struct_sem lock.
+ * the drm_device::struct_mutex lock.
  */
-int drm_add_magic(drm_device_t *dev, drm_file_t *priv, drm_magic_t magic)
+static int drm_add_magic(drm_device_t * dev, drm_file_t * priv,
+                        drm_magic_t magic)
 {
-       int               hash;
        drm_magic_entry_t *entry;
 
        DRM_DEBUG("%d\n", magic);
 
-       hash         = drm_hash_magic(magic);
-       entry        = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
-       if (!entry) return -ENOMEM;
+       entry = drm_alloc(sizeof(*entry), DRM_MEM_MAGIC);
+       if (!entry)
+               return -ENOMEM;
        memset(entry, 0, sizeof(*entry));
-       entry->magic = magic;
-       entry->priv  = priv;
-       entry->next  = NULL;
-
-       down(&dev->struct_sem);
-       if (dev->magiclist[hash].tail) {
-               dev->magiclist[hash].tail->next = entry;
-               dev->magiclist[hash].tail       = entry;
-       } else {
-               dev->magiclist[hash].head       = entry;
-               dev->magiclist[hash].tail       = entry;
-       }
-       up(&dev->struct_sem);
+       entry->priv = priv;
+
+       entry->hash_item.key = (unsigned long)magic;
+       mutex_lock(&dev->struct_mutex);
+       drm_ht_insert_item(&dev->magiclist, &entry->hash_item);
+       list_add_tail(&entry->head, &dev->magicfree);
+       mutex_unlock(&dev->struct_mutex);
 
        return 0;
 }
 
 /**
  * Remove a magic number.
- * 
+ *
  * \param dev DRM device.
  * \param magic magic number.
  *
  * Searches and unlinks the entry in drm_device::magiclist with the magic
- * number hash key, while holding the drm_device::struct_sem lock.
+ * number hash key, while holding the drm_device::struct_mutex lock.
  */
-int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
+static int drm_remove_magic(drm_device_t * dev, drm_magic_t magic)
 {
-       drm_magic_entry_t *prev = NULL;
        drm_magic_entry_t *pt;
-       int               hash;
-
+       drm_hash_item_t *hash;
 
        DRM_DEBUG("%d\n", magic);
-       hash = drm_hash_magic(magic);
-
-       down(&dev->struct_sem);
-       for (pt = dev->magiclist[hash].head; pt; prev = pt, pt = pt->next) {
-               if (pt->magic == magic) {
-                       if (dev->magiclist[hash].head == pt) {
-                               dev->magiclist[hash].head = pt->next;
-                       }
-                       if (dev->magiclist[hash].tail == pt) {
-                               dev->magiclist[hash].tail = prev;
-                       }
-                       if (prev) {
-                               prev->next = pt->next;
-                       }
-                       up(&dev->struct_sem);
-                       return 0;
-               }
+
+       mutex_lock(&dev->struct_mutex);
+       if (drm_ht_find_item(&dev->magiclist, (unsigned long)magic, &hash)) {
+               mutex_unlock(&dev->struct_mutex);
+               return -EINVAL;
        }
-       up(&dev->struct_sem);
+       pt = drm_hash_entry(hash, drm_magic_entry_t, hash_item);
+       drm_ht_remove_item(&dev->magiclist, hash);
+       list_del(&pt->head);
+       mutex_unlock(&dev->struct_mutex);
 
        drm_free(pt, sizeof(*pt), DRM_MEM_MAGIC);
 
-       return -EINVAL;
+       return 0;
 }
 
 /**
@@ -171,21 +138,22 @@ int drm_remove_magic(drm_device_t *dev, drm_magic_t magic)
  * filp.
  */
 int drm_getmagic(struct inode *inode, struct file *filp,
-                 unsigned int cmd, unsigned long arg)
+                unsigned int cmd, unsigned long arg)
 {
        static drm_magic_t sequence = 0;
        static DEFINE_SPINLOCK(lock);
-       drm_file_t         *priv    = filp->private_data;
-       drm_device_t       *dev     = priv->head->dev;
-       drm_auth_t         auth;
+       drm_file_t *priv = filp->private_data;
+       drm_device_t *dev = priv->head->dev;
+       drm_auth_t auth;
 
-                               /* Find unique magic */
+       /* Find unique magic */
        if (priv->magic) {
                auth.magic = priv->magic;
        } else {
                do {
                        spin_lock(&lock);
-                       if (!sequence) ++sequence; /* reserve 0 */
+                       if (!sequence)
+                               ++sequence;     /* reserve 0 */
                        auth.magic = sequence++;
                        spin_unlock(&lock);
                } while (drm_find_file(dev, auth.magic));
@@ -194,7 +162,7 @@ int drm_getmagic(struct inode *inode, struct file *filp,
        }
 
        DRM_DEBUG("%u\n", auth.magic);
-       if (copy_to_user((drm_auth_t __user *)arg, &auth, sizeof(auth)))
+       if (copy_to_user((drm_auth_t __user *) arg, &auth, sizeof(auth)))
                return -EFAULT;
        return 0;
 }
@@ -211,14 +179,14 @@ int drm_getmagic(struct inode *inode, struct file *filp,
  * Checks if \p filp is associated with the magic number passed in \arg.
  */
 int drm_authmagic(struct inode *inode, struct file *filp,
-                  unsigned int cmd, unsigned long arg)
+                 unsigned int cmd, unsigned long arg)
 {
-       drm_file_t         *priv    = filp->private_data;
-       drm_device_t       *dev     = priv->head->dev;
-       drm_auth_t         auth;
-       drm_file_t         *file;
+       drm_file_t *priv = filp->private_data;
+       drm_device_t *dev = priv->head->dev;
+       drm_auth_t auth;
+       drm_file_t *file;
 
-       if (copy_from_user(&auth, (drm_auth_t __user *)arg, sizeof(auth)))
+       if (copy_from_user(&auth, (drm_auth_t __user *) arg, sizeof(auth)))
                return -EFAULT;
        DRM_DEBUG("%u\n", auth.magic);
        if ((file = drm_find_file(dev, auth.magic))) {