mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Stephen M. Cameron" <scameron@beardog.cce.hp.com>
To: linux-kernel@vger.kernel.org
Cc: linux-scsi@vger.kernel.org, akpm@linux-foundation.org,
	axboe@kernel.dk, andrew.patterson@hp.com,
	mikem@c18-ss-1-lb.cnet.com, scameron@beardog.cce.hp.com
Subject: [cciss: PATCH 05/17] Dynamically allocate struct device for each logical drive as needed.
Date: Thu, 20 Aug 2009 10:18:07 -0500	[thread overview]
Message-ID: <20090820151807.22833.62442.stgit@beardog.cce.hp.com> (raw)
In-Reply-To: <20090820151648.22833.14168.stgit@beardog.cce.hp.com>

Dynamically allocate struct device for each logical drive as needed
instead of allocating the maximum we would ever need at driver init time.

Signed-off-by: Stephen M. Cameron <scameron@beardog.cce.hp.com>
---

 drivers/block/cciss.c |   98 ++++++++++++++++++++++++++++++++++++-------------
 drivers/block/cciss.h |    2 +
 2 files changed, 73 insertions(+), 27 deletions(-)

diff --git a/drivers/block/cciss.c b/drivers/block/cciss.c
index 1d2d614..4f9ef49 100644
--- a/drivers/block/cciss.c
+++ b/drivers/block/cciss.c
@@ -197,6 +197,8 @@ static void fail_all_cmds(unsigned long ctlr);
 static int add_to_scan_list(struct ctlr_info *h);
 static int scan_thread(void *data);
 static int check_for_unit_attention(ctlr_info_t *h, CommandList_struct *c);
+static void cciss_hba_release(struct device *dev);
+static void cciss_device_release(struct device *dev);
 
 #ifdef CONFIG_PROC_FS
 static void cciss_procinit(int i);
@@ -458,7 +460,6 @@ static void __devinit cciss_procinit(int i)
 #define MAX_PRODUCT_NAME_LEN 19
 
 #define to_hba(n) container_of(n, struct ctlr_info, dev)
-#define to_drv(n) container_of(n, drive_info_struct, dev)
 
 static ssize_t host_store_rescan(struct device *dev,
 				 struct device_attribute *attr,
@@ -478,8 +479,8 @@ static ssize_t dev_show_unique_id(struct device *dev,
 				 struct device_attribute *attr,
 				 char *buf)
 {
-	drive_info_struct *drv = to_drv(dev);
-	struct ctlr_info *h = to_hba(drv->dev.parent);
+	drive_info_struct *drv = dev_get_drvdata(dev);
+	struct ctlr_info *h = to_hba(drv->dev->parent);
 	__u8 sn[16];
 	unsigned long flags;
 	int ret = 0;
@@ -508,8 +509,8 @@ static ssize_t dev_show_vendor(struct device *dev,
 			       struct device_attribute *attr,
 			       char *buf)
 {
-	drive_info_struct *drv = to_drv(dev);
-	struct ctlr_info *h = to_hba(drv->dev.parent);
+	drive_info_struct *drv = dev_get_drvdata(dev);
+	struct ctlr_info *h = to_hba(drv->dev->parent);
 	char vendor[VENDOR_LEN + 1];
 	unsigned long flags;
 	int ret = 0;
@@ -532,8 +533,8 @@ static ssize_t dev_show_model(struct device *dev,
 			      struct device_attribute *attr,
 			      char *buf)
 {
-	drive_info_struct *drv = to_drv(dev);
-	struct ctlr_info *h = to_hba(drv->dev.parent);
+	drive_info_struct *drv = dev_get_drvdata(dev);
+	struct ctlr_info *h = to_hba(drv->dev->parent);
 	char model[MODEL_LEN + 1];
 	unsigned long flags;
 	int ret = 0;
@@ -556,8 +557,8 @@ static ssize_t dev_show_rev(struct device *dev,
 			    struct device_attribute *attr,
 			    char *buf)
 {
-	drive_info_struct *drv = to_drv(dev);
-	struct ctlr_info *h = to_hba(drv->dev.parent);
+	drive_info_struct *drv = dev_get_drvdata(dev);
+	struct ctlr_info *h = to_hba(drv->dev->parent);
 	char rev[REV_LEN + 1];
 	unsigned long flags;
 	int ret = 0;
@@ -593,6 +594,7 @@ static struct attribute_group *cciss_host_attr_groups[] = {
 static struct device_type cciss_host_type = {
 	.name		= "cciss_host",
 	.groups		= cciss_host_attr_groups,
+	.release	= cciss_hba_release,
 };
 
 static struct attribute *cciss_dev_attrs[] = {
@@ -615,12 +617,24 @@ static struct attribute_group *cciss_dev_attr_groups[] = {
 static struct device_type cciss_dev_type = {
 	.name		= "cciss_device",
 	.groups		= cciss_dev_attr_groups,
+	.release	= cciss_device_release,
 };
 
 static struct bus_type cciss_bus_type = {
 	.name		= "cciss",
 };
 
+/*
+ * cciss_hba_release is called when the reference count
+ * of h->dev goes to zero.
+ */
+static void cciss_hba_release(struct device *dev)
+{
+	/*
+	 * nothing to do, but need this to avoid a warning
+	 * about not having a release handler from lib/kref.c.
+	 */
+}
 
 /*
  * Initialize sysfs entry for each controller.  This sets up and registers
@@ -644,6 +658,15 @@ static int cciss_create_hba_sysfs_entry(struct ctlr_info *h)
 static void cciss_destroy_hba_sysfs_entry(struct ctlr_info *h)
 {
 	device_del(&h->dev);
+	put_device(&h->dev); /* final put. */
+}
+
+/* cciss_device_release is called when the reference count
+ * of h->drv[x].dev goes to zero.
+ */
+static void cciss_device_release(struct device *dev)
+{
+	kfree(dev);
 }
 
 /*
@@ -652,24 +675,33 @@ static void cciss_destroy_hba_sysfs_entry(struct ctlr_info *h)
  * /sys/bus/pci/devices/<dev/ccis#/. We also create a link from
  * /sys/block/cciss!c#d# to this entry.
  */
-static int cciss_create_ld_sysfs_entry(struct ctlr_info *h,
-				       drive_info_struct *drv,
+static long cciss_create_ld_sysfs_entry(struct ctlr_info *h,
 				       int drv_index)
 {
-	device_initialize(&drv->dev);
-	drv->dev.type = &cciss_dev_type;
-	drv->dev.bus = &cciss_bus_type;
-	dev_set_name(&drv->dev, "c%dd%d", h->ctlr, drv_index);
-	drv->dev.parent = &h->dev;
-	return device_add(&drv->dev);
+	struct device *dev;
+
+	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	if (!dev)
+		return -ENOMEM;
+	device_initialize(dev);
+	dev->type = &cciss_dev_type;
+	dev->bus = &cciss_bus_type;
+	dev_set_name(dev, "c%dd%d", h->ctlr, drv_index);
+	dev->parent = &h->dev;
+	h->drv[drv_index].dev = dev;
+	dev_set_drvdata(dev, &h->drv[drv_index]);
+	return device_add(dev);
 }
 
 /*
  * Remove sysfs entries for a logical drive.
  */
-static void cciss_destroy_ld_sysfs_entry(drive_info_struct *drv)
+static void cciss_destroy_ld_sysfs_entry(struct ctlr_info *h, int drv_index)
 {
-	device_del(&drv->dev);
+	struct device *dev = h->drv[drv_index].dev;
+	device_del(dev);
+	put_device(dev); /* the "final" put. */
+	h->drv[drv_index].dev = NULL;
 }
 
 /*
@@ -1650,7 +1682,10 @@ static void cciss_get_serial_no(int ctlr, int logvol, int withirq,
 	return;
 }
 
-static void cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
+/*
+ * cciss_add_disk sets up the block device queue for a logical drive
+ */
+static int cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
 				int drv_index)
 {
 	disk->queue = blk_init_queue(do_cciss_request, &h->lock);
@@ -1658,8 +1693,12 @@ static void cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
 	disk->major = h->major;
 	disk->first_minor = drv_index << NWD_SHIFT;
 	disk->fops = &cciss_fops;
+	if (h->drv[drv_index].dev == NULL) {
+		if (cciss_create_ld_sysfs_entry(h, drv_index))
+			goto cleanup_queue;
+	}
 	disk->private_data = &h->drv[drv_index];
-	disk->driverfs_dev = &h->drv[drv_index].dev;
+	disk->driverfs_dev = h->drv[drv_index].dev;
 
 	/* Set up queue information */
 	blk_queue_bounce_limit(disk->queue, h->pdev->dma_mask);
@@ -1685,6 +1724,12 @@ static void cciss_add_disk(ctlr_info_t *h, struct gendisk *disk,
 	wmb();
 	h->drv[drv_index].queue = disk->queue;
 	add_disk(disk);
+	return 0;
+
+cleanup_queue:
+	blk_cleanup_queue(disk->queue);
+	disk->queue = NULL;
+	return -1;
 }
 
 /* This function will check the usage_count of the drive to be updated/added.
@@ -1870,7 +1915,7 @@ static int cciss_add_gendisk(ctlr_info_t *h, __u32 lunid, int controller_node)
 		}
 	}
 	h->drv[drv_index].LunID = lunid;
-	if (cciss_create_ld_sysfs_entry(h, &h->drv[drv_index], drv_index))
+	if (cciss_create_ld_sysfs_entry(h, drv_index))
 		goto err_free_disk;
 
 	/* Don't need to mark this busy because nobody */
@@ -2144,7 +2189,7 @@ static int deregister_disk(ctlr_info_t *h, int drv_index,
 				 * indicate that this element of the drive
 				 * array is free.
 				 */
-	cciss_destroy_ld_sysfs_entry(drv);
+	cciss_destroy_ld_sysfs_entry(h, drv_index);
 
 	if (clear_all) {
 		/* check to see if it was the last disk */
@@ -4267,8 +4312,9 @@ static void __devexit cciss_remove_one(struct pci_dev *pdev)
 			if (q)
 				blk_cleanup_queue(q);
 		}
-		if (hba[i]->drv[j].raid_level != -1)
-			cciss_destroy_ld_sysfs_entry(&hba[i]->drv[j]);
+		if (hba[i]->drv[j].dev != NULL &&
+			(j == 0 || hba[i]->drv[j].raid_level != -1))
+				cciss_destroy_ld_sysfs_entry(hba[i], j);
 
 	}
 
@@ -4344,7 +4390,7 @@ static int __init cciss_init(void)
 	if (err)
 		goto err_thread_stop;
 
-	return 0;
+	return err;
 
 err_thread_stop:
 	kthread_stop(cciss_scan_thread);
diff --git a/drivers/block/cciss.h b/drivers/block/cciss.h
index 4fb3639..9679342 100644
--- a/drivers/block/cciss.h
+++ b/drivers/block/cciss.h
@@ -45,7 +45,7 @@ typedef struct _drive_info_struct
 				   * to prevent it from being opened or it's
 				   * queue from being started.
 				   */
-	struct	device dev;
+	struct	device *dev;
 	__u8 serial_no[16]; /* from inquiry page 0x83,
 			     * not necc. null terminated.
 			     */


  parent reply	other threads:[~2009-08-20 15:18 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-20 15:17 [cciss: PATCH 00/17] fix scan_thread, sysfs stuff, LUN addressing problem Stephen M. Cameron
2009-08-20 15:17 ` [cciss: PATCH 01/17] Remove sysfs entries for logical drives on driver cleanup Stephen M. Cameron
2009-08-20 15:17 ` [cciss: PATCH 02/17] Use one scan thread per controller and fix hang during rmmod Stephen M. Cameron
2009-08-20 15:17 ` [cciss: PATCH 03/17] Allow triggering of rescan of logical drive topology via sysfs entry Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 04/17] Remove some unused code in rebuild_lun_table() Stephen M. Cameron
2009-08-20 15:18 ` Stephen M. Cameron [this message]
2009-08-20 15:18 ` [cciss: PATCH 06/17] Rearrange logical drive sysfs code to make the "changing a disk" path work Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 07/17] Handle failure of blk_init_queue gracefully in cciss_add_disk Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 08/17] Handle cases when cciss_add_disk fails Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 09/17] Handle special case for sysfs attributes of the first logical drive Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 10/17] Clear all sysfs-exposed data for deleted logical drives Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 11/17] Fix usage_count check in rebuild_lun_table when triggered via sysfs Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 12/17] Fix excessive gendisk freeing bug on driver unload Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 13/17] Silence noisy per-disk messages output by cciss_read_capacity Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 14/17] Preserve all 8 bytes of LUN ID for logical drives Stephen M. Cameron
2009-08-20 15:18 ` [cciss: PATCH 15/17] Add lunid attribute to each logical drive in /sys Stephen M. Cameron
2009-08-20 15:19 ` [cciss: PATCH 16/17] Add a "raid_level" " Stephen M. Cameron
2009-08-20 15:19 ` [cciss: PATCH 17/17] Add usage_count " Stephen M. Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090820151807.22833.62442.stgit@beardog.cce.hp.com \
    --to=scameron@beardog.cce.hp.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrew.patterson@hp.com \
    --cc=axboe@kernel.dk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mikem@c18-ss-1-lb.cnet.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome