Subject: [PATCH net-next 1/3] bonding: update the primary slave when slave's name changed

From: Ding Tianhong <[email-masked]>
Date: Tue, 14 Jan 2014 10:36:56 +0800

If the slave's name changed, and the bond params primary is exist,
the bond should deal with the situation in two ways:

1) If the slave is the primary slave yet, clean the primary slave
   and reselect active slave.
2) If the slave's new name is as same as bond primary, set the slave
   as primary slave and reselect active slave.

Thanks for Veaceslav's suggestion.

Suggested-by: Veaceslav Falico <[email-masked]>
Signed-off-by: Ding Tianhong <[email-masked]>
---
 drivers/net/bonding/bond_main.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/drivers/net/bonding/bond_main.c b/drivers/net/bonding/bond_main.c
index e06c445..63d6533 100644
--- a/drivers/net/bonding/bond_main.c
+++ b/drivers/net/bonding/bond_main.c
@@ -2860,9 +2860,35 @@ static int bond_slave_netdev_event(unsigned long event,
 		 */
 		break;
 	case NETDEV_CHANGENAME:
-		/*
-		 * TODO: handle changing the primary's name
+		/* Handle changing the slave's name:
+		 * 1) If the slave is primary save yet,
+		 * clean the primary slave and reselect
+		 * active slave.
+		 * 2) If the slave's new name is bond
+		 * primary, set the slave as primary
+		 * slave and reselect active slave.
 		 */
+		if (USES_PRIMARY(bond->params.mode) &&
+		    bond->params.primary[0]) {
+			if (bond->primary_slave &&
+			    slave == bond->primary_slave) {
+				pr_info("%s: Setting primary slave to None.\n",
+					bond->dev->name);
+				bond->primary_slave = NULL;
+				write_lock_bh(&bond->curr_slave_lock);
+				bond_select_active_slave(bond);
+				write_unlock_bh(&bond->curr_slave_lock);
+			} else if (!bond->primary_slave &&
+				   !strcmp(bond->params.primary,
+					  slave_dev->name)) {
+				pr_info("%s: Setting %s as primary slave.\n",
+					bond->dev->name, slave_dev->name);
+				bond->primary_slave = slave;
+				write_lock_bh(&bond->curr_slave_lock);
+				bond_select_active_slave(bond);
+				write_unlock_bh(&bond->curr_slave_lock);
+			}
+		}
 		break;
 	case NETDEV_FEAT_CHANGE:
 		bond_compute_features(bond);
-- 
1.8.0