[Open-FCoE] [PATCH V2 08/13] fcoe: fcoe_interface create, destroy and refcounting

Chris Leech christopher.leech at intel.com
Wed Jul 8 17:40:13 UTC 2009


Up to this point the fcoe_instance structure was simply kzalloc/kfreed.  This
patch introduces create and destroy functions as well as kref based reference
counting.  The create function will grow as the initialization code is moved
there.

Signed-off-by: Chris Leech <christopher.leech at intel.com>
---

 drivers/scsi/fcoe/fcoe.c |   97 +++++++++++++++++++++++++++++++++++++---------
 drivers/scsi/fcoe/fcoe.h |    1 
 2 files changed, 79 insertions(+), 19 deletions(-)

diff --git a/drivers/scsi/fcoe/fcoe.c b/drivers/scsi/fcoe/fcoe.c
index 7779e42..aa7089d 100644
--- a/drivers/scsi/fcoe/fcoe.c
+++ b/drivers/scsi/fcoe/fcoe.c
@@ -134,6 +134,58 @@ static struct scsi_host_template fcoe_shost_template = {
 };
 
 /**
+ * fcoe_interface_create()
+ * @netdev: network interface
+ *
+ * Returns: pointer to a struct fcoe_interface or NULL on error
+ */
+static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
+{
+	struct fcoe_interface *fcoe;
+
+	fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
+	if (!fcoe) {
+		FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
+		return NULL;
+	}
+
+	kref_init(&fcoe->kref);
+	fcoe->real_dev = netdev;
+
+	return fcoe;
+}
+
+/**
+ * fcoe_interface_release() - fcoe_port kref release function
+ * @kref: embedded reference count in an fcoe_interface struct
+ */
+static void fcoe_interface_release(struct kref *kref)
+{
+	struct fcoe_interface *fcoe;
+
+	fcoe = container_of(kref, struct fcoe_interface, kref);
+	kfree(fcoe);
+}
+
+/**
+ * fcoe_interface_get()
+ * @fcoe:
+ */
+static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
+{
+	kref_get(&fcoe->kref);
+}
+
+/**
+ * fcoe_interface_put()
+ * @fcoe:
+ */
+static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
+{
+	kref_put(&fcoe->kref, fcoe_interface_release);
+}
+
+/**
  * fcoe_fip_recv - handle a received FIP frame.
  * @skb: the receive skb
  * @dev: associated &net_device
@@ -550,7 +602,7 @@ static void fcoe_if_destroy(struct fc_lport *lport)
 	/* Release the net_device and Scsi_Host */
 	dev_put(fcoe->real_dev);
 	scsi_host_put(lport->host);
-	kfree(fcoe);		/* TODO, should be refcounted */
+	fcoe_interface_put(fcoe);
 }
 
 /*
@@ -596,8 +648,8 @@ static struct libfc_function_template fcoe_libfc_fcn_templ = {
 };
 
 /**
- * fcoe_if_create() - this function creates the fcoe interface
- * @netdev: pointer the associated netdevice
+ * fcoe_if_create() - this function creates the fcoe port
+ * @fcoe: fcoe_interface structure to create an fc_lport instance on
  * @parent: device pointer to be the parent in sysfs for the SCSI host
  *
  * Creates fc_lport struct and scsi_host for lport, configures lport
@@ -605,30 +657,23 @@ static struct libfc_function_template fcoe_libfc_fcn_templ = {
  *
  * Returns : The allocated fc_lport or an error pointer
  */
-static struct fc_lport *fcoe_if_create(struct net_device *netdev,
+static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
 				       struct device *parent)
 {
 	int rc;
 	struct fc_lport *lport = NULL;
 	struct fcoe_port *port;
-	struct fcoe_interface *fcoe;
 	struct Scsi_Host *shost;
+	struct net_device *netdev = fcoe->real_dev;
 
 	FCOE_NETDEV_DBG(netdev, "Create Interface\n");
 
-	fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
-	if (!fcoe) {
-		FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
-		rc = -ENOMEM;
-		goto out;
-	}
-
 	shost = libfc_host_alloc(&fcoe_shost_template,
 				 sizeof(struct fcoe_port));
 	if (!shost) {
 		FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
 		rc = -ENOMEM;
-		goto out_kfree_port;
+		goto out;
 	}
 	lport = shost_priv(shost);
 	port = lport_priv(lport);
@@ -700,7 +745,7 @@ static struct fc_lport *fcoe_if_create(struct net_device *netdev,
 		fcoe_ctlr_link_up(&fcoe->ctlr);
 
 	dev_hold(netdev);
-
+	fcoe_interface_get(fcoe);
 	return lport;
 
 out_lp_destroy:
@@ -709,8 +754,6 @@ out_netdev_cleanup:
 	fcoe_netdev_cleanup(port);
 out_host_put:
 	scsi_host_put(lport->host);
-out_kfree_port:
-	kfree(fcoe);
 out:
 	return ERR_PTR(rc);
 }
@@ -1616,6 +1659,8 @@ static int fcoe_ethdrv_put(const struct net_device *netdev)
 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
 {
 	struct net_device *netdev;
+	struct fcoe_interface *fcoe;
+	struct fcoe_port *port;
 	struct fc_lport *lport;
 	int rc;
 
@@ -1630,6 +1675,8 @@ static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
 		rc = -ENODEV;
 		goto out_putdev;
 	}
+	port = lport_priv(lport);
+	fcoe = port->fcoe;
 	fcoe_if_destroy(lport);
 	fcoe_ethdrv_put(netdev);
 	rc = 0;
@@ -1649,6 +1696,7 @@ out_nodev:
 static int fcoe_create(const char *buffer, struct kernel_param *kp)
 {
 	int rc;
+	struct fcoe_interface *fcoe;
 	struct fc_lport *lport;
 	struct net_device *netdev;
 
@@ -1664,15 +1712,26 @@ static int fcoe_create(const char *buffer, struct kernel_param *kp)
 	}
 	fcoe_ethdrv_get(netdev);
 
-	lport = fcoe_if_create(netdev, &netdev->dev);
+	fcoe = fcoe_interface_create(netdev);
+	if (!fcoe) {
+		rc = -ENOMEM;
+		goto out_putdev;
+	}
+
+	lport = fcoe_if_create(fcoe, &netdev->dev);
 	if (IS_ERR(lport)) {
 		printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
 		       netdev->name);
 		fcoe_ethdrv_put(netdev);
 		rc = -EIO;
-		goto out_putdev;
+		goto out_free;
 	}
-	rc = 0;
+
+	dev_put(netdev);
+	return 0;
+
+out_free:
+	fcoe_interface_put(fcoe);
 out_putdev:
 	dev_put(netdev);
 out_nodev:
diff --git a/drivers/scsi/fcoe/fcoe.h b/drivers/scsi/fcoe/fcoe.h
index b97956a..9781f58 100644
--- a/drivers/scsi/fcoe/fcoe.h
+++ b/drivers/scsi/fcoe/fcoe.h
@@ -85,6 +85,7 @@ struct fcoe_interface {
 	struct packet_type  fip_packet_type;
 	struct fcoe_ctlr ctlr;
 	struct fc_exch_mgr *oem;		/* offload exchange manger */
+	struct kref kref;
 };
 
 /*




More information about the devel mailing list