lockd: Support non-AF_INET addresses in nlm_lookup_host()
[safe/jmp/linux-2.6] / kernel / resource.c
index 7b9a497..03d796c 100644 (file)
@@ -8,7 +8,6 @@
  */
 
 #include <linux/module.h>
-#include <linux/sched.h>
 #include <linux/errno.h>
 #include <linux/ioport.h>
 #include <linux/init.h>
@@ -17,6 +16,7 @@
 #include <linux/fs.h>
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
+#include <linux/device.h>
 #include <asm/io.h>
 
 
@@ -131,14 +131,8 @@ static const struct file_operations proc_iomem_operations = {
 
 static int __init ioresources_init(void)
 {
-       struct proc_dir_entry *entry;
-
-       entry = create_proc_entry("ioports", 0, NULL);
-       if (entry)
-               entry->proc_fops = &proc_ioports_operations;
-       entry = create_proc_entry("iomem", 0, NULL);
-       if (entry)
-               entry->proc_fops = &proc_iomem_operations;
+       proc_create("ioports", 0, NULL, &proc_ioports_operations);
+       proc_create("iomem", 0, NULL, &proc_iomem_operations);
        return 0;
 }
 __initcall(ioresources_init);
@@ -213,27 +207,6 @@ int request_resource(struct resource *root, struct resource *new)
 EXPORT_SYMBOL(request_resource);
 
 /**
- * ____request_resource - reserve a resource, with resource conflict returned
- * @root: root resource descriptor
- * @new: resource descriptor desired by caller
- *
- * Returns:
- * On success, NULL is returned.
- * On error, a pointer to the conflicting resource is returned.
- */
-struct resource *____request_resource(struct resource *root, struct resource *new)
-{
-       struct resource *conflict;
-
-       write_lock(&resource_lock);
-       conflict = __request_resource(root, new);
-       write_unlock(&resource_lock);
-       return conflict;
-}
-
-EXPORT_SYMBOL(____request_resource);
-
-/**
  * release_resource - release a previously reserved resource
  * @old: resource pointer
  */
@@ -249,13 +222,13 @@ int release_resource(struct resource *old)
 
 EXPORT_SYMBOL(release_resource);
 
-#ifdef CONFIG_MEMORY_HOTPLUG
+#if defined(CONFIG_MEMORY_HOTPLUG) && !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
 /*
  * Finds the lowest memory reosurce exists within [res->start.res->end)
  * the caller must specify res->start, res->end, res->flags.
  * If found, returns 0, res is overwritten, if not found, returns -1.
  */
-int find_next_system_ram(struct resource *res)
+static int find_next_system_ram(struct resource *res)
 {
        resource_size_t start, end;
        struct resource *p;
@@ -288,6 +261,30 @@ int find_next_system_ram(struct resource *res)
                res->end = p->end;
        return 0;
 }
+int
+walk_memory_resource(unsigned long start_pfn, unsigned long nr_pages, void *arg,
+                       int (*func)(unsigned long, unsigned long, void *))
+{
+       struct resource res;
+       unsigned long pfn, len;
+       u64 orig_end;
+       int ret = -1;
+       res.start = (u64) start_pfn << PAGE_SHIFT;
+       res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
+       res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
+       orig_end = res.end;
+       while ((res.start < res.end) && (find_next_system_ram(&res) >= 0)) {
+               pfn = (unsigned long)(res.start >> PAGE_SHIFT);
+               len = (unsigned long)((res.end + 1 - res.start) >> PAGE_SHIFT);
+               ret = (*func)(pfn, len, arg);
+               if (ret)
+                       break;
+               res.start = res.end + 1;
+               res.end = orig_end;
+       }
+       return ret;
+}
+
 #endif
 
 /*
@@ -365,35 +362,21 @@ int allocate_resource(struct resource *root, struct resource *new,
 
 EXPORT_SYMBOL(allocate_resource);
 
-/**
- * insert_resource - Inserts a resource in the resource tree
- * @parent: parent of the new resource
- * @new: new resource to insert
- *
- * Returns 0 on success, -EBUSY if the resource can't be inserted.
- *
- * This function is equivalent to request_resource when no conflict
- * happens. If a conflict happens, and the conflicting resources
- * entirely fit within the range of the new resource, then the new
- * resource is inserted and the conflicting resources become children of
- * the new resource.
+/*
+ * Insert a resource into the resource tree. If successful, return NULL,
+ * otherwise return the conflicting resource (compare to __request_resource())
  */
-int insert_resource(struct resource *parent, struct resource *new)
+static struct resource * __insert_resource(struct resource *parent, struct resource *new)
 {
-       int result;
        struct resource *first, *next;
 
-       write_lock(&resource_lock);
-
        for (;; parent = first) {
-               result = 0;
                first = __request_resource(parent, new);
                if (!first)
-                       goto out;
+                       return first;
 
-               result = -EBUSY;
                if (first == parent)
-                       goto out;
+                       return first;
 
                if ((first->start > new->start) || (first->end < new->end))
                        break;
@@ -404,15 +387,13 @@ int insert_resource(struct resource *parent, struct resource *new)
        for (next = first; ; next = next->sibling) {
                /* Partial overlap? Bad, and unfixable */
                if (next->start < new->start || next->end > new->end)
-                       goto out;
+                       return next;
                if (!next->sibling)
                        break;
                if (next->sibling->start > new->end)
                        break;
        }
 
-       result = 0;
-
        new->parent = parent;
        new->sibling = next->sibling;
        new->child = first;
@@ -429,10 +410,64 @@ int insert_resource(struct resource *parent, struct resource *new)
                        next = next->sibling;
                next->sibling = new;
        }
+       return NULL;
+}
 
- out:
+/**
+ * insert_resource - Inserts a resource in the resource tree
+ * @parent: parent of the new resource
+ * @new: new resource to insert
+ *
+ * Returns 0 on success, -EBUSY if the resource can't be inserted.
+ *
+ * This function is equivalent to request_resource when no conflict
+ * happens. If a conflict happens, and the conflicting resources
+ * entirely fit within the range of the new resource, then the new
+ * resource is inserted and the conflicting resources become children of
+ * the new resource.
+ */
+int insert_resource(struct resource *parent, struct resource *new)
+{
+       struct resource *conflict;
+
+       write_lock(&resource_lock);
+       conflict = __insert_resource(parent, new);
+       write_unlock(&resource_lock);
+       return conflict ? -EBUSY : 0;
+}
+
+/**
+ * insert_resource_expand_to_fit - Insert a resource into the resource tree
+ * @root: root resource descriptor
+ * @new: new resource to insert
+ *
+ * Insert a resource into the resource tree, possibly expanding it in order
+ * to make it encompass any conflicting resources.
+ */
+void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
+{
+       if (new->parent)
+               return;
+
+       write_lock(&resource_lock);
+       for (;;) {
+               struct resource *conflict;
+
+               conflict = __insert_resource(root, new);
+               if (!conflict)
+                       break;
+               if (conflict == root)
+                       break;
+
+               /* Ok, expand resource to cover the conflict, then try again .. */
+               if (conflict->start < new->start)
+                       new->start = conflict->start;
+               if (conflict->end > new->end)
+                       new->end = conflict->end;
+
+               printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
+       }
        write_unlock(&resource_lock);
-       return result;
 }
 
 /**
@@ -483,6 +518,24 @@ int adjust_resource(struct resource *res, resource_size_t start, resource_size_t
 
 EXPORT_SYMBOL(adjust_resource);
 
+/**
+ * resource_alignment - calculate resource's alignment
+ * @res: resource pointer
+ *
+ * Returns alignment on success, 0 (invalid alignment) on failure.
+ */
+resource_size_t resource_alignment(struct resource *res)
+{
+       switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
+       case IORESOURCE_SIZEALIGN:
+               return resource_size(res);
+       case IORESOURCE_STARTALIGN:
+               return res->start;
+       default:
+               return 0;
+       }
+}
+
 /*
  * This is compatibility stuff for IO resources.
  *
@@ -618,6 +671,67 @@ void __release_region(struct resource *parent, resource_size_t start,
 EXPORT_SYMBOL(__release_region);
 
 /*
+ * Managed region resource
+ */
+struct region_devres {
+       struct resource *parent;
+       resource_size_t start;
+       resource_size_t n;
+};
+
+static void devm_region_release(struct device *dev, void *res)
+{
+       struct region_devres *this = res;
+
+       __release_region(this->parent, this->start, this->n);
+}
+
+static int devm_region_match(struct device *dev, void *res, void *match_data)
+{
+       struct region_devres *this = res, *match = match_data;
+
+       return this->parent == match->parent &&
+               this->start == match->start && this->n == match->n;
+}
+
+struct resource * __devm_request_region(struct device *dev,
+                               struct resource *parent, resource_size_t start,
+                               resource_size_t n, const char *name)
+{
+       struct region_devres *dr = NULL;
+       struct resource *res;
+
+       dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
+                         GFP_KERNEL);
+       if (!dr)
+               return NULL;
+
+       dr->parent = parent;
+       dr->start = start;
+       dr->n = n;
+
+       res = __request_region(parent, start, n, name);
+       if (res)
+               devres_add(dev, dr);
+       else
+               devres_free(dr);
+
+       return res;
+}
+EXPORT_SYMBOL(__devm_request_region);
+
+void __devm_release_region(struct device *dev, struct resource *parent,
+                          resource_size_t start, resource_size_t n)
+{
+       struct region_devres match_data = { parent, start, n };
+
+       __release_region(parent, start, n);
+       WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
+                              &match_data));
+}
+EXPORT_SYMBOL(__devm_release_region);
+
+/*
  * Called from init/main.c to reserve IO ports.
  */
 #define MAXRESERVE 4