aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2/dlm/dlmrecovery.c
blob: b3fdd1a323d6be070f2c85cdd4387fc2eb1547af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
	if (command[0] == &apos1848


		/* Check to see if the bond already exists. */
		list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
			if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
				printk(KERN_ERR DRV_NAME
					": cannot add bond %s; it already exists\n",
					ifname);
				res = -EPERM;
				goto out;
			}

		printk(KERN_INFO DRV_NAME
			": %s is being created...\n", ifname);
		if (bond_create(ifname, &bonding_defaults, &bond)) {
			printk(KERN_INFO DRV_NAME
			": %s interface already exists. Bond creation failed.\n",
			ifname);
			res = -EPERM;
		}
		goto out;
	}

	if (command[0] == '-') {
		list_for_each_entry_safe(bond, nxt, &bond_dev_list, bond_list)
			if (strnicmp(bond->dev->name, ifname, IFNAMSIZ) == 0) {
				rtnl_lock();
				/* check the ref count on the bond's kobject.
				 * If it's > expected, then there's a file open,
				 * and we have to fail.
				 */
				if (atomic_read(&bond->dev->class_dev.kobj.kref.refcount)
							> expected_refcount){
					rtnl_unlock();
					printk(KERN_INFO DRV_NAME
						": Unable remove bond %s due to open references.\n",
						ifname);
					res = -EPERM;
					goto out;
				}
				printk(KERN_INFO DRV_NAME
					": %s is being deleted...\n",
					bond->dev->name);
				unregister_netdevice(bond->dev);
				bond_deinit(bond->dev);
		        	bond_destroy_sysfs_entry(bond);
				rtnl_unlock();
				goto out;
			}

		printk(KERN_ERR DRV_NAME
			": unable to delete non-existent bond %s\n", ifname);
		res = -ENODEV;
		goto out;
	}

err_no_cmd:
	printk(KERN_ERR DRV_NAME
		": no command found in bonding_masters. Use +ifname or -ifname.\n");
	res = -EPERM;

	/* Always return either count or an error.  If you return 0, you'll
	 * get called forever, which is bad.
	 */
out:
	up_write(&(bonding_rwsem));
	return res;
}
/* class attribute for bond_masters file.  This ends up in /sys/class/net */
static CLASS_ATTR(bonding_masters,  S_IWUSR | S_IRUGO,
		  bonding_show_bonds, bonding_store_bonds);

int bond_create_slave_symlinks(struct net_device *master, struct net_device *slave)
{
	char linkname[IFNAMSIZ+7];
	int ret = 0;

	/* first, create a link from the slave back to the master */
	ret = sysfs_create_link(&(slave->class_dev.kobj), &(master->class_dev.kobj),
				"master");
	if (ret)
		return ret;
	/* next, create a link from the master to the slave */
	sprintf(linkname,"slave_%s",slave->name);
	ret = sysfs_create_link(&(master->class_dev.kobj), &(slave->class_dev.kobj),
				linkname);
	return ret;

}

void bond_destroy_slave_symlinks(struct net_device *master, struct net_device *slave)
{
	char linkname[IFNAMSIZ+7];

	sysfs_remove_link(&(slave->class_dev.kobj), "master");
	sprintf(linkname,"slave_%s",slave->name);
	sysfs_remove_link(&(master->class_dev.kobj), linkname);
}


/*
 * Show the slaves in the current bond.
 */
static ssize_t bonding_show_slaves(struct class_device *cd, char *buf)
{
	struct slave *slave;
	int i, res = 0;
	struct bonding *bond = to_bond(cd);

	read_lock_bh(&bond->lock);
	bond_for_each_slave(bond, slave, i) {
		if (res > (PAGE_SIZE - IFNAMSIZ)) {
			/* not enough space for another interface name */
			if ((PAGE_SIZE - res) > 10)
				res = PAGE_SIZE - 10;
			res += sprintf(buf + res, "++more++");
			break;
		}
		res += sprintf(buf + res, "%s ", slave->dev->name);
	}
	read_unlock_bh(&bond->lock);
	res += sprintf(buf + res, "\n");
	res++;
	return res;
}

/*
 * Set the slaves in the current bond.  The bond interface must be
 * up for this to succeed.
 * This function is largely the same flow as bonding_update_bonds().
 */
static ssize_t bonding_store_slaves(struct class_device *cd, const char *buffer, size_t count)
{
	char command[IFNAMSIZ + 1] = { 0, };
	char *ifname;
	int i, res, found, ret = count;
	struct slave *slave;
	struct net_device *dev = NULL;
	struct bonding *bond = to_bond(cd);

	/* Quick sanity check -- is the bond interface up? */
	if (!(bond->dev->flags & IFF_UP)) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Unable to update slaves because interface is down.\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	/* Note:  We can't hold bond->lock here, as bond_create grabs it. */

	sscanf(buffer, "%16s", command); /* IFNAMSIZ*/
	ifname = command + 1;
	if ((strlen(command) <= 1) ||
	    !dev_valid_name(ifname))
		goto err_no_cmd;

	if (command[0] == '+') {

		/* Got a slave name in ifname.  Is it already in the list? */
		found = 0;
		read_lock_bh(&bond->lock);
		bond_for_each_slave(bond, slave, i)
			if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
				printk(KERN_ERR DRV_NAME
				       ": %s: Interface %s is already enslaved!\n",
				       bond->dev->name, ifname);
				ret = -EPERM;
				read_unlock_bh(&bond->lock);
				goto out;
			}

		read_unlock_bh(&bond->lock);
		printk(KERN_INFO DRV_NAME ": %s: Adding slave %s.\n",
		       bond->dev->name, ifname);
		dev = dev_get_by_name(ifname);
		if (!dev) {
			printk(KERN_INFO DRV_NAME
			       ": %s: Interface %s does not exist!\n",
			       bond->dev->name, ifname);
			ret = -EPERM;
			goto out;
		}
		else
			dev_put(dev);

		if (dev->flags & IFF_UP) {
			printk(KERN_ERR DRV_NAME
			       ": %s: Error: Unable to enslave %s "
			       "because it is already up.\n",
			       bond->dev->name, dev->name);
			ret = -EPERM;
			goto out;
		}
		/* If this is the first slave, then we need to set
		   the master's hardware address to be the same as the
		   slave's. */
		if (!(*((u32 *) & (bond->dev->dev_addr[0])))) {
			memcpy(bond->dev->dev_addr, dev->dev_addr,
			       dev->addr_len);
		}

		/* Set the slave's MTU to match the bond */
		if (dev->mtu != bond->dev->mtu) {
			if (dev->change_mtu) {
				res = dev->change_mtu(dev,
						      bond->dev->mtu);
				if (res) {
					ret = res;
					goto out;
				}
			} else {
				dev->mtu = bond->dev->mtu;
			}
		}
		rtnl_lock();
		res = bond_enslave(bond->dev, dev);
		rtnl_unlock();
		if (res) {
			ret = res;
		}
		goto out;
	}

	if (command[0] == '-') {
		dev = NULL;
		bond_for_each_slave(bond, slave, i)
			if (strnicmp(slave->dev->name, ifname, IFNAMSIZ) == 0) {
				dev = slave->dev;
				break;
			}
		if (dev) {
			printk(KERN_INFO DRV_NAME ": %s: Removing slave %s\n",
				bond->dev->name, dev->name);
			rtnl_lock();
			res = bond_release(bond->dev, dev);
			rtnl_unlock();
			if (res) {
				ret = res;
				goto out;
			}
			/* set the slave MTU to the default */
			if (dev->change_mtu) {
				dev->change_mtu(dev, 1500);
			} else {
				dev->mtu = 1500;
			}
		}
		else {
			printk(KERN_ERR DRV_NAME ": unable to remove non-existent slave %s for bond %s.\n",
				ifname, bond->dev->name);
			ret = -ENODEV;
		}
		goto out;
	}

err_no_cmd:
	printk(KERN_ERR DRV_NAME ": no command found in slaves file for bond %s. Use +ifname or -ifname.\n", bond->dev->name);
	ret = -EPERM;

out:
	return ret;
}

static CLASS_DEVICE_ATTR(slaves, S_IRUGO | S_IWUSR, bonding_show_slaves, bonding_store_slaves);

/*
 * Show and set the bonding mode.  The bond interface must be down to
 * change the mode.
 */
static ssize_t bonding_show_mode(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%s %d\n",
			bond_mode_tbl[bond->params.mode].modename,
			bond->params.mode) + 1;
}

static ssize_t bonding_store_mode(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (bond->dev->flags & IFF_UP) {
		printk(KERN_ERR DRV_NAME
		       ": unable to update mode of %s because interface is up.\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	new_value = bond_parse_parm((char *)buf, bond_mode_tbl);
	if (new_value < 0)  {
		printk(KERN_ERR DRV_NAME
		       ": %s: Ignoring invalid mode value %.*s.\n",
		       bond->dev->name,
		       (int)strlen(buf) - 1, buf);
		ret = -EINVAL;
		goto out;
	} else {
		bond->params.mode = new_value;
		bond_set_mode_ops(bond, bond->params.mode);
		printk(KERN_INFO DRV_NAME ": %s: setting mode to %s (%d).\n",
			bond->dev->name, bond_mode_tbl[new_value].modename, new_value);
	}
out:
	return ret;
}
static CLASS_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, bonding_show_mode, bonding_store_mode);

/*
 * Show and set the bonding transmit hash method.  The bond interface must be down to
 * change the xmit hash policy.
 */
static ssize_t bonding_show_xmit_hash(struct class_device *cd, char *buf)
{
	int count;
	struct bonding *bond = to_bond(cd);

	if ((bond->params.mode != BOND_MODE_XOR) &&
	    (bond->params.mode != BOND_MODE_8023AD)) {
		// Not Applicable
		count = sprintf(buf, "NA\n") + 1;
	} else {
		count = sprintf(buf, "%s %d\n",
			xmit_hashtype_tbl[bond->params.xmit_policy].modename,
			bond->params.xmit_policy) + 1;
	}

	return count;
}

static ssize_t bonding_store_xmit_hash(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (bond->dev->flags & IFF_UP) {
		printk(KERN_ERR DRV_NAME
		       "%s: Interface is up. Unable to update xmit policy.\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	if ((bond->params.mode != BOND_MODE_XOR) &&
	    (bond->params.mode != BOND_MODE_8023AD)) {
		printk(KERN_ERR DRV_NAME
		       "%s: Transmit hash policy is irrelevant in this mode.\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	new_value = bond_parse_parm((char *)buf, xmit_hashtype_tbl);
	if (new_value < 0)  {
		printk(KERN_ERR DRV_NAME
		       ": %s: Ignoring invalid xmit hash policy value %.*s.\n",
		       bond->dev->name,
		       (int)strlen(buf) - 1, buf);
		ret = -EINVAL;
		goto out;
	} else {
		bond->params.xmit_policy = new_value;
		bond_set_mode_ops(bond, bond->params.mode);
		printk(KERN_INFO DRV_NAME ": %s: setting xmit hash policy to %s (%d).\n",
			bond->dev->name, xmit_hashtype_tbl[new_value].modename, new_value);
	}
out:
	return ret;
}
static CLASS_DEVICE_ATTR(xmit_hash_policy, S_IRUGO | S_IWUSR, bonding_show_xmit_hash, bonding_store_xmit_hash);

/*
 * Show and set the arp timer interval.  There are two tricky bits
 * here.  First, if ARP monitoring is activated, then we must disable
 * MII monitoring.  Second, if the ARP timer isn't running, we must
 * start it.
 */
static ssize_t bonding_show_arp_interval(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%d\n", bond->params.arp_interval) + 1;
}

static ssize_t bonding_store_arp_interval(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (sscanf(buf, "%d", &new_value) != 1) {
		printk(KERN_ERR DRV_NAME
		       ": %s: no arp_interval value specified.\n",
		       bond->dev->name);
		ret = -EINVAL;
		goto out;
	}
	if (new_value < 0) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Invalid arp_interval value %d not in range 1-%d; rejected.\n",
		       bond->dev->name, new_value, INT_MAX);
		ret = -EINVAL;
		goto out;
	}

	printk(KERN_INFO DRV_NAME
	       ": %s: Setting ARP monitoring interval to %d.\n",
	       bond->dev->name, new_value);
	bond->params.arp_interval = new_value;
	if (bond->params.miimon) {
		printk(KERN_INFO DRV_NAME
		       ": %s: ARP monitoring cannot be used with MII monitoring. "
		       "%s Disabling MII monitoring.\n",
		       bond->dev->name, bond->dev->name);
		bond->params.miimon = 0;
		/* Kill MII timer, else it brings bond's link down */
		if (bond->arp_timer.function) {
			printk(KERN_INFO DRV_NAME
			": %s: Kill MII timer, else it brings bond's link down...\n",
		       bond->dev->name);
			del_timer_sync(&bond->mii_timer);
		}
	}
	if (!bond->params.arp_targets[0]) {
		printk(KERN_INFO DRV_NAME
		       ": %s: ARP monitoring has been set up, "
		       "but no ARP targets have been specified.\n",
		       bond->dev->name);
	}
	if (bond->dev->flags & IFF_UP) {
		/* If the interface is up, we may need to fire off
		 * the ARP timer.  If the interface is down, the
		 * timer will get fired off when the open function
		 * is called.
		 */
		if (bond->arp_timer.function) {
			/* The timer's already set up, so fire it off */
			mod_timer(&bond->arp_timer, jiffies + 1);
		} else {
			/* Set up the timer. */
			init_timer(&bond->arp_timer);
			bond->arp_timer.expires = jiffies + 1;
			bond->arp_timer.data =
				(unsigned long) bond->dev;
			if (bond->params.mode == BOND_MODE_ACTIVEBACKUP) {
				bond->arp_timer.function =
					(void *)
					&bond_activebackup_arp_mon;
			} else {
				bond->arp_timer.function =
					(void *)
					&bond_loadbalance_arp_mon;
			}
			add_timer(&bond->arp_timer);
		}
	}

out:
	return ret;
}
static CLASS_DEVICE_ATTR(arp_interval, S_IRUGO | S_IWUSR , bonding_show_arp_interval, bonding_store_arp_interval);

/*
 * Show and set the arp targets.
 */
static ssize_t bonding_show_arp_targets(struct class_device *cd, char *buf)
{
	int i, res = 0;
	struct bonding *bond = to_bond(cd);

	for (i = 0; i < BOND_MAX_ARP_TARGETS; i++) {
		if (bond->params.arp_targets[i])
			res += sprintf(buf + res, "%u.%u.%u.%u ",
			       NIPQUAD(bond->params.arp_targets[i]));
	}
	if (res)
		res--;  /* eat the leftover space */
	res += sprintf(buf + res, "\n");
	res++;
	return res;
}

static ssize_t bonding_store_arp_targets(struct class_device *cd, const char *buf, size_t count)
{
	u32 newtarget;
	int i = 0, done = 0, ret = count;
	struct bonding *bond = to_bond(cd);
	u32 *targets;

	targets = bond->params.arp_targets;
	newtarget = in_aton(buf + 1);
	/* look for adds */
	if (buf[0] == '+') {
		if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
			printk(KERN_ERR DRV_NAME
			       ": %s: invalid ARP target %u.%u.%u.%u specified for addition\n",
 			       bond->dev->name, NIPQUAD(newtarget));
			ret = -EINVAL;
			goto out;
		}
		/* look for an empty slot to put the target in, and check for dupes */
		for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
			if (targets[i] == newtarget) { /* duplicate */
				printk(KERN_ERR DRV_NAME
				       ": %s: ARP target %u.%u.%u.%u is already present\n",
				       bond->dev->name, NIPQUAD(newtarget));
				if (done)
					targets[i] = 0;
				ret = -EINVAL;
				goto out;
			}
			if (targets[i] == 0 && !done) {
				printk(KERN_INFO DRV_NAME
				       ": %s: adding ARP target %d.%d.%d.%d.\n",
				       bond->dev->name, NIPQUAD(newtarget));
				done = 1;
				targets[i] = newtarget;
			}
		}
		if (!done) {
			printk(KERN_ERR DRV_NAME
			       ": %s: ARP target table is full!\n",
			       bond->dev->name);
			ret = -EINVAL;
			goto out;
		}

	}
	else if (buf[0] == '-')	{
		if ((newtarget == 0) || (newtarget == INADDR_BROADCAST)) {
			printk(KERN_ERR DRV_NAME
			       ": %s: invalid ARP target %d.%d.%d.%d specified for removal\n",
			       bond->dev->name, NIPQUAD(newtarget));
			ret = -EINVAL;
			goto out;
		}

		for (i = 0; (i < BOND_MAX_ARP_TARGETS); i++) {
			if (targets[i] == newtarget) {
				printk(KERN_INFO DRV_NAME
				       ": %s: removing ARP target %d.%d.%d.%d.\n",
				       bond->dev->name, NIPQUAD(newtarget));
				targets[i] = 0;
				done = 1;
			}
		}
		if (!done) {
			printk(KERN_INFO DRV_NAME
			       ": %s: unable to remove nonexistent ARP target %d.%d.%d.%d.\n",
			       bond->dev->name, NIPQUAD(newtarget));
			ret = -EINVAL;
			goto out;
		}
	}
	else {
		printk(KERN_ERR DRV_NAME ": no command found in arp_ip_targets file for bond %s. Use +<addr> or -<addr>.\n",
			bond->dev->name);
		ret = -EPERM;
		goto out;
	}

out:
	return ret;
}
static CLASS_DEVICE_ATTR(arp_ip_target, S_IRUGO | S_IWUSR , bonding_show_arp_targets, bonding_store_arp_targets);

/*
 * Show and set the up and down delays.  These must be multiples of the
 * MII monitoring value, and are stored internally as the multiplier.
 * Thus, we must translate to MS for the real world.
 */
static ssize_t bonding_show_downdelay(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%d\n", bond->params.downdelay * bond->params.miimon) + 1;
}

static ssize_t bonding_store_downdelay(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (!(bond->params.miimon)) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Unable to set down delay as MII monitoring is disabled\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	if (sscanf(buf, "%d", &new_value) != 1) {
		printk(KERN_ERR DRV_NAME
		       ": %s: no down delay value specified.\n",
		       bond->dev->name);
		ret = -EINVAL;
		goto out;
	}
	if (new_value < 0) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
		       bond->dev->name, new_value, 1, INT_MAX);
		ret = -EINVAL;
		goto out;
	} else {
		if ((new_value % bond->params.miimon) != 0) {
			printk(KERN_WARNING DRV_NAME
			       ": %s: Warning: down delay (%d) is not a multiple "
			       "of miimon (%d), delay rounded to %d ms\n",
			       bond->dev->name, new_value, bond->params.miimon,
			       (new_value / bond->params.miimon) *
			       bond->params.miimon);
		}
		bond->params.downdelay = new_value / bond->params.miimon;
		printk(KERN_INFO DRV_NAME ": %s: Setting down delay to %d.\n",
		       bond->dev->name, bond->params.downdelay * bond->params.miimon);

	}

out:
	return ret;
}
static CLASS_DEVICE_ATTR(downdelay, S_IRUGO | S_IWUSR , bonding_show_downdelay, bonding_store_downdelay);

static ssize_t bonding_show_updelay(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%d\n", bond->params.updelay * bond->params.miimon) + 1;

}

static ssize_t bonding_store_updelay(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (!(bond->params.miimon)) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Unable to set up delay as MII monitoring is disabled\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	if (sscanf(buf, "%d", &new_value) != 1) {
		printk(KERN_ERR DRV_NAME
		       ": %s: no up delay value specified.\n",
		       bond->dev->name);
		ret = -EINVAL;
		goto out;
	}
	if (new_value < 0) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Invalid down delay value %d not in range %d-%d; rejected.\n",
		       bond->dev->name, new_value, 1, INT_MAX);
		ret = -EINVAL;
		goto out;
	} else {
		if ((new_value % bond->params.miimon) != 0) {
			printk(KERN_WARNING DRV_NAME
			       ": %s: Warning: up delay (%d) is not a multiple "
			       "of miimon (%d), updelay rounded to %d ms\n",
			       bond->dev->name, new_value, bond->params.miimon,
			       (new_value / bond->params.miimon) *
			       bond->params.miimon);
		}
		bond->params.updelay = new_value / bond->params.miimon;
		printk(KERN_INFO DRV_NAME ": %s: Setting up delay to %d.\n",
		       bond->dev->name, bond->params.updelay * bond->params.miimon);

	}

out:
	return ret;
}
static CLASS_DEVICE_ATTR(updelay, S_IRUGO | S_IWUSR , bonding_show_updelay, bonding_store_updelay);

/*
 * Show and set the LACP interval.  Interface must be down, and the mode
 * must be set to 802.3ad mode.
 */
static ssize_t bonding_show_lacp(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%s %d\n",
		bond_lacp_tbl[bond->params.lacp_fast].modename,
		bond->params.lacp_fast) + 1;
}

static ssize_t bonding_store_lacp(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (bond->dev->flags & IFF_UP) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Unable to update LACP rate because interface is up.\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	if (bond->params.mode != BOND_MODE_8023AD) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Unable to update LACP rate because bond is not in 802.3ad mode.\n",
		       bond->dev->name);
		ret = -EPERM;
		goto out;
	}

	new_value = bond_parse_parm((char *)buf, bond_lacp_tbl);

	if ((new_value == 1) || (new_value == 0)) {
		bond->params.lacp_fast = new_value;
		printk(KERN_INFO DRV_NAME
		       ": %s: Setting LACP rate to %s (%d).\n",
		       bond->dev->name, bond_lacp_tbl[new_value].modename, new_value);
	} else {
		printk(KERN_ERR DRV_NAME
		       ": %s: Ignoring invalid LACP rate value %.*s.\n",
		     	bond->dev->name, (int)strlen(buf) - 1, buf);
		ret = -EINVAL;
	}
out:
	return ret;
}
static CLASS_DEVICE_ATTR(lacp_rate, S_IRUGO | S_IWUSR, bonding_show_lacp, bonding_store_lacp);

/*
 * Show and set the MII monitor interval.  There are two tricky bits
 * here.  First, if MII monitoring is activated, then we must disable
 * ARP monitoring.  Second, if the timer isn't running, we must
 * start it.
 */
static ssize_t bonding_show_miimon(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%d\n", bond->params.miimon) + 1;
}

static ssize_t bonding_store_miimon(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);

	if (sscanf(buf, "%d", &new_value) != 1) {
		printk(KERN_ERR DRV_NAME
		       ": %s: no miimon value specified.\n",
		       bond->dev->name);
		ret = -EINVAL;
		goto out;
	}
	if (new_value < 0) {
		printk(KERN_ERR DRV_NAME
		       ": %s: Invalid miimon value %d not in range %d-%d; rejected.\n",
		       bond->dev->name, new_value, 1, INT_MAX);
		ret = -EINVAL;
		goto out;
	} else {
		printk(KERN_INFO DRV_NAME
		       ": %s: Setting MII monitoring interval to %d.\n",
		       bond->dev->name, new_value);
		bond->params.miimon = new_value;
		if(bond->params.updelay)
			printk(KERN_INFO DRV_NAME
			      ": %s: Note: Updating updelay (to %d) "
			      "since it is a multiple of the miimon value.\n",
			      bond->dev->name,
			      bond->params.updelay * bond->params.miimon);
		if(bond->params.downdelay)
			printk(KERN_INFO DRV_NAME
			      ": %s: Note: Updating downdelay (to %d) "
			      "since it is a multiple of the miimon value.\n",
			      bond->dev->name,
			      bond->params.downdelay * bond->params.miimon);
		if (bond->params.arp_interval) {
			printk(KERN_INFO DRV_NAME
			       ": %s: MII monitoring cannot be used with "
			       "ARP monitoring. Disabling ARP monitoring...\n",
			       bond->dev->name);
			bond->params.arp_interval = 0;
			/* Kill ARP timer, else it brings bond's link down */
			if (bond->mii_timer.function) {
				printk(KERN_INFO DRV_NAME
				": %s: Kill ARP timer, else it brings bond's link down...\n",
			       bond->dev->name);
				del_timer_sync(&bond->arp_timer);
			}
		}

		if (bond->dev->flags & IFF_UP) {
			/* If the interface is up, we may need to fire off
			 * the MII timer. If the interface is down, the
			 * timer will get fired off when the open function
			 * is called.
			 */
			if (bond->mii_timer.function) {
				/* The timer's already set up, so fire it off */
				mod_timer(&bond->mii_timer, jiffies + 1);
			} else {
				/* Set up the timer. */
				init_timer(&bond->mii_timer);
				bond->mii_timer.expires = jiffies + 1;
				bond->mii_timer.data =
					(unsigned long) bond->dev;
				bond->mii_timer.function =
					(void *) &bond_mii_monitor;
				add_timer(&bond->mii_timer);
			}
		}
	}
out:
	return ret;
}
static CLASS_DEVICE_ATTR(miimon, S_IRUGO | S_IWUSR, bonding_show_miimon, bonding_store_miimon);

/*
 * Show and set the primary slave.  The store function is much
 * simpler than bonding_store_slaves function because it only needs to
 * handle one interface name.
 * The bond must be a mode that supports a primary for this be
 * set.
 */
static ssize_t bonding_show_primary(struct class_device *cd, char *buf)
{
	int count = 0;
	struct bonding *bond = to_bond(cd);

	if (bond->primary_slave)
		count = sprintf(buf, "%s\n", bond->primary_slave->dev->name) + 1;
	else
		count = sprintf(buf, "\n") + 1;

	return count;
}

static ssize_t bonding_store_primary(struct class_device *cd, const char *buf, size_t count)
{
	int i;
	struct slave *slave;
	struct bonding *bond = to_bond(cd);

	write_lock_bh(&bond->lock);
	if (!USES_PRIMARY(bond->params.mode)) {
		printk(KERN_INFO DRV_NAME
		       ": %s: Unable to set primary slave; %s is in mode %d\n",
		       bond->dev->name, bond->dev->name, bond->params.mode);
	} else {
		bond_for_each_slave(bond, slave, i) {
			if (strnicmp
			    (slave->dev->name, buf,
			     strlen(slave->dev->name)) == 0) {
				printk(KERN_INFO DRV_NAME
				       ": %s: Setting %s as primary slave.\n",
				       bond->dev->name, slave->dev->name);
				bond->primary_slave = slave;
				bond_select_active_slave(bond);
				goto out;
			}
		}

		/* if we got here, then we didn't match the name of any slave */

		if (strlen(buf) == 0 || buf[0] == '\n') {
			printk(KERN_INFO DRV_NAME
			       ": %s: Setting primary slave to None.\n",
			       bond->dev->name);
			bond->primary_slave = NULL;
				bond_select_active_slave(bond);
		} else {
			printk(KERN_INFO DRV_NAME
			       ": %s: Unable to set %.*s as primary slave as it is not a slave.\n",
			       bond->dev->name, (int)strlen(buf) - 1, buf);
		}
	}
out:
	write_unlock_bh(&bond->lock);
	return count;
}
static CLASS_DEVICE_ATTR(primary, S_IRUGO | S_IWUSR, bonding_show_primary, bonding_store_primary);

/*
 * Show and set the use_carrier flag.
 */
static ssize_t bonding_show_carrier(struct class_device *cd, char *buf)
{
	struct bonding *bond = to_bond(cd);

	return sprintf(buf, "%d\n", bond->params.use_carrier) + 1;
}

static ssize_t bonding_store_carrier(struct class_device *cd, const char *buf, size_t count)
{
	int new_value, ret = count;
	struct bonding *bond = to_bond(cd);


	if (sscanf(buf, "%d", &new_value) != 1) {
		printk(KERN_ERR DRV_NAME
		       ": %s: no use_carrier value specified.\n",
		       bond->dev->name);
		ret = -EINVAL;
		goto out;
	}
	if ((new_value == 0) || (new_value == 1)) {
		bond->params.use_carrier = new_value;
		printk(KERN_INFO DRV_NAME ": %s: Setting use_carrier to %d.\n",
		       bond->dev->name, new_value);
	} else {
		printk(KERN_INFO DRV_NAME
		       ": %s: Ignoring invalid use_carrier value %d.\n",
		       bond->dev->name, new_value);
	}
out:
	return count;
}
static CLASS_DEVICE_ATTR(use_carrier, S_IRUGO | S_IWUSR, bonding_show_carrier, bonding_store_carrier);


/*
 * Show and set currently active_slave.
 */
static ssize_t bonding_show_active_slave(struct class_device *cd, char *buf)
{
	struct slave *curr;
	struct bonding *bond = to_bond(cd);
	int count;


	read_lock(&bond->curr_slave_lock);
	curr = bond->curr_active_slave;
	read_unlock(&bond->curr_slave_lock);

	if (USES_PRIMARY(bond->params.mode) && curr)
		count = sprintf(buf, "%s\n", curr->dev->name) + 1;
	else
		count = sprintf(buf, "\n") + 1;
	return count;
}

static ssize_t bonding_store_active_slave(struct class_device *cd, const char *buf, size_t count)
{
	int i;
	struct slave *slave;
        struct slave *old_active = NULL;
        struct slave *new_active = NULL;
	struct bonding *bond = to_bond(cd);

	write_lock_bh(&bond->lock);
	if (!USES_PRIMARY(bond->params.mode)) {
		printk(KERN_INFO DRV_NAME
		       ": %s: Unable to change active slave; %s is in mode %d\n",
		       bond->dev->name, bond->dev->name, bond->params.mode);
	} else {
		bond_for_each_slave(bond, slave, i) {
			if (strnicmp
			    (slave->dev->name, buf,
			     strlen(slave->dev->name)) == 0) {
        			old_active = bond->curr_active_slave;
        			new_active = slave;
        			if (new_active && (new_active == old_active)) {
					/* do nothing */
					printk(KERN_INFO DRV_NAME
				       	       ": %s: %s is already the current active slave.\n",
				               bond->dev->name, slave->dev->name);
					goto out;
				}
				else {
        				if ((new_active) &&
            				    (old_active) &&
				            (new_active->link == BOND_LINK_UP) &&
				            IS_UP(new_active->dev)) {
						printk(KERN_INFO DRV_NAME
				       	              ": %s: Setting %s as active slave.\n",
				                      bond->dev->name, slave->dev->name);
                				bond_change_active_slave(bond, new_active);
        				}
					else {
						printk(KERN_INFO DRV_NAME
				       	              ": %s: Could not set %s as active slave; "
						      "either %s is down or the link is down.\n",
				                      bond->dev->name, slave->dev->name,
						      slave->dev->name);
					}
					goto out;
				}
			}
		}

		/* if we got here, then we didn't match the name of any slave */

		if (strlen(buf) == 0 || buf[0] == '\n') {
			printk(KERN_INFO DRV_NAME
			       ": %s: Setting active slave to None.\n",
			       bond->dev->name);
			bond->primary_slave = NULL;
				bond_select_active_slave(bond);
		} else {
			printk(KERN_INFO DRV_NAME
			       ": %s: Unable to set %.*s as active slave as it is not a slave.\n",
			       bond->dev->name, (int)strlen(buf) - 1, buf);
		}
	}
out:
	write_unlock_bh(&bond->lock);
	return count;

}
static CLASS_DEVICE_ATTR(active_slave, S_IRUGO | S_IWUSR, bonding_show_active_slave, bonding_store_active_slave);


/*
 * Show link status of the bond interface.
 */
static ssize_t bonding_show_mii_status(struct class_device *cd, char *buf)
{
	struct slave *curr;
	struct bonding *bond = to_bond(cd);

	read_lock(&bond->curr_slave_lock);
	curr = bond->curr_active_slave;
	read_unlock(&bond->curr_slave_lock);

	return sprintf(buf, "%s\n", (curr) ? "up" : "down") + 1;
}
static CLASS_DEVICE_ATTR(mii_status, S_IRUGO, bonding_show_mii_status, NULL);


/*
 * Show current 802.3ad aggregator ID.
 */
static ssize_t bonding_show_ad_aggregator(struct class_device *cd, char *buf)
{
	int count = 0;
	struct bonding *bond = to_bond(cd);

	if (bond->params.mode == BOND_MODE_8023AD) {
		struct ad_info ad_info;
		count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0 : ad_info.aggregator_id) + 1;
	}
	else
		count = sprintf(buf, "\n") + 1;

	return count;
}
static CLASS_DEVICE_ATTR(ad_aggregator, S_IRUGO, bonding_show_ad_aggregator, NULL);


/*
 * Show number of active 802.3ad ports.
 */
static ssize_t bonding_show_ad_num_ports(struct class_device *cd, char *buf)
{
	int count = 0;
	struct bonding *bond = to_bond(cd);

	if (bond->params.mode == BOND_MODE_8023AD) {
		struct ad_info ad_info;
		count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0: ad_info.ports) + 1;
	}
	else
		count = sprintf(buf, "\n") + 1;

	return count;
}
static CLASS_DEVICE_ATTR(ad_num_ports, S_IRUGO, bonding_show_ad_num_ports, NULL);


/*
 * Show current 802.3ad actor key.
 */
static ssize_t bonding_show_ad_actor_key(struct class_device *cd, char *buf)
{
	int count = 0;
	struct bonding *bond = to_bond(cd);

	if (bond->params.mode == BOND_MODE_8023AD) {
		struct ad_info ad_info;
		count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0 : ad_info.actor_key) + 1;
	}
	else
		count = sprintf(buf, "\n") + 1;

	return count;
}
static CLASS_DEVICE_ATTR(ad_actor_key, S_IRUGO, bonding_show_ad_actor_key, NULL);


/*
 * Show current 802.3ad partner key.
 */
static ssize_t bonding_show_ad_partner_key(struct class_device *cd, char *buf)
{
	int count = 0;
	struct bonding *bond = to_bond(cd);

	if (bond->params.mode == BOND_MODE_8023AD) {
		struct ad_info ad_info;
		count = sprintf(buf, "%d\n", (bond_3ad_get_active_agg_info(bond, &ad_info)) ?  0 : ad_info.partner_key) + 1;
	}
	else
		count = sprintf(buf, "\n") + 1;

	return count;
}
static CLASS_DEVICE_ATTR(ad_partner_key, S_IRUGO, bonding_show_ad_partner_key, NULL);


/*
 * Show current 802.3ad partner mac.
 */
static ssize_t bonding_show_ad_partner_mac(struct class_device *cd, char *buf)
{
	int count = 0;
	struct bonding *bond = to_bond(cd);

	if (bond->params.mode == BOND_MODE_8023AD) {
		struct ad_info ad_info;
		if (!bond_3ad_get_active_agg_info(bond, &ad_info)) {
			count = sprintf(buf,"%02x:%02x:%02x:%02x:%02x:%02x\n",
				       ad_info.partner_system[0],
				       ad_info.partner_system[1],
				       ad_info.partner_system[2],
				       ad_info.partner_system[3],
				       ad_info.partner_system[4],
				       ad_info.partner_system[5]) + 1;
		}
	}
	else
		count = sprintf(buf, "\n") + 1;

	return count;
}
static CLASS_DEVICE_ATTR(ad_partner_mac, S_IRUGO, bonding_show_ad_partner_mac, NULL);



static struct attribute *per_bond_attrs[] = {
	&class_device_attr_slaves.attr,
	&class_device_attr_mode.attr,
	&class_device_attr_arp_interval.attr,
	&class_device_attr_arp_ip_target.attr,
	&class_device_attr_downdelay.attr,
	&class_device_attr_updelay.attr,
	&class_device_attr_lacp_rate.attr,
	&class_device_attr_xmit_hash_policy.attr,
	&class_device_attr_miimon.attr,
	&class_device_attr_primary.attr,
	&class_device_attr_use_carrier.attr,
	&class_device_attr_active_slave.attr,
	&class_device_attr_mii_status.attr,
	&class_device_attr_ad_aggregator.attr,
	&class_device_attr_ad_num_ports.attr,
	&class_device_attr_ad_actor_key.attr,
	&class_device_attr_ad_partner_key.attr,
	&class_device_attr_ad_partner_mac.attr,
	NULL,
};

static struct attribute_group bonding_group = {
	.name = "bonding",
	.attrs = per_bond_attrs,
};

/*
 * Initialize sysfs.  This sets up the bonding_masters file in
 * /sys/class/net.
 */
int bond_create_sysfs(void)
{
	int ret = 0;
	struct bonding *firstbond;

	init_rwsem(&bonding_rwsem);

	/* get the netdev class pointer */
	firstbond = container_of(bond_dev_list.next, struct bonding, bond_list);
	if (!firstbond)
		return -ENODEV;

	netdev_class = firstbond->dev->class_dev.class;
	if (!netdev_class)
		return -ENODEV;

	ret = class_create_file(netdev_class, &class_attr_bonding_masters);

	return ret;

}

/*
 * Remove /sys/class/net/bonding_masters.
 */
void bond_destroy_sysfs(void)
{
	if (netdev_class)
		class_remove_file(netdev_class, &class_attr_bonding_masters);
}

/*
 * Initialize sysfs for each bond.  This sets up and registers
 * the 'bondctl' directory for each individual bond under /sys/class/net.
 */
int bond_create_sysfs_entry(struct bonding *bond)
{
	struct net_device *dev = bond->dev;
	int err;

	err = sysfs_create_group(&(dev->class_dev.kobj), &bonding_group);
	if (err) {
		printk(KERN_EMERG "eek! didn't create group!\n");
	}

	if (expected_refcount < 1)
		expected_refcount = atomic_read(&bond->dev->class_dev.kobj.kref.refcount);

	return err;
}
/*
 * Remove sysfs entries for each bond.
 */
void bond_destroy_sysfs_entry(struct bonding *bond)
{
	struct net_device *dev = bond->dev;

	sysfs_remove_group(&(dev->class_dev.kobj), &bonding_group);
}

%u, " "my node=%u, dead node=%u\n", send_to, done_msg.node_idx, done_msg.dead_node); ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg, sizeof(done_msg), send_to, &tmpret); if (ret < 0) { mlog(ML_ERROR, "%s: Error %d send RECO_DATA_DONE to node %u " "to recover dead node %u\n", dlm->name, ret, send_to, dead_node); if (!dlm_is_host_down(ret)) { BUG(); } } else ret = tmpret; return ret; } int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data, void **ret_data) { struct dlm_ctxt *dlm = data; struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf; struct dlm_reco_node_data *ndata = NULL; int ret = -EINVAL; if (!dlm_grab(dlm)) return -EINVAL; mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, " "node_idx=%u, this node=%u\n", done->dead_node, dlm->reco.dead_node, done->node_idx, dlm->node_num); mlog_bug_on_msg((done->dead_node != dlm->reco.dead_node), "Got DATA DONE: dead_node=%u, reco.dead_node=%u, " "node_idx=%u, this node=%u\n", done->dead_node, dlm->reco.dead_node, done->node_idx, dlm->node_num); spin_lock(&dlm_reco_state_lock); list_for_each_entry(ndata, &dlm->reco.node_data, list) { if (ndata->node_num != done->node_idx) continue; switch (ndata->state) { /* should have moved beyond INIT but not to FINALIZE yet */ case DLM_RECO_NODE_DATA_INIT: case DLM_RECO_NODE_DATA_DEAD: case DLM_RECO_NODE_DATA_FINALIZE_SENT: mlog(ML_ERROR, "bad ndata state for node %u:" " state=%d\n", ndata->node_num, ndata->state); BUG(); break; /* these states are possible at this point, anywhere along * the line of recovery */ case DLM_RECO_NODE_DATA_DONE: case DLM_RECO_NODE_DATA_RECEIVING: case DLM_RECO_NODE_DATA_REQUESTED: case DLM_RECO_NODE_DATA_REQUESTING: mlog(0, "node %u is DONE sending " "recovery data!\n", ndata->node_num); ndata->state = DLM_RECO_NODE_DATA_DONE; ret = 0; break; } } spin_unlock(&dlm_reco_state_lock); /* wake the recovery thread, some node is done */ if (!ret) dlm_kick_recovery_thread(dlm); if (ret < 0) mlog(ML_ERROR, "failed to find recovery node data for node " "%u\n", done->node_idx); dlm_put(dlm); mlog(0, "leaving reco data done handler, ret=%d\n", ret); return ret; } static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm, struct list_head *list, u8 dead_node) { struct dlm_lock_resource *res, *next; struct dlm_lock *lock; spin_lock(&dlm->spinlock); list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) { /* always prune any $RECOVERY entries for dead nodes, * otherwise hangs can occur during later recovery */ if (dlm_is_recovery_lock(res->lockname.name, res->lockname.len)) { spin_lock(&res->spinlock); list_for_each_entry(lock, &res->granted, list) { if (lock->ml.node == dead_node) { mlog(0, "AHA! there was " "a $RECOVERY lock for dead " "node %u (%s)!\n", dead_node, dlm->name); list_del_init(&lock->list); dlm_lock_put(lock); break; } } spin_unlock(&res->spinlock); continue; } if (res->owner == dead_node) { mlog(0, "found lockres owned by dead node while " "doing recovery for node %u. sending it.\n", dead_node); list_move_tail(&res->recovering, list); } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) { mlog(0, "found UNKNOWN owner while doing recovery " "for node %u. sending it.\n", dead_node); list_move_tail(&res->recovering, list); } } spin_unlock(&dlm->spinlock); } static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res) { int total_locks = 0; struct list_head *iter, *queue = &res->granted; int i; for (i=0; i<3; i++) { list_for_each(iter, queue) total_locks++; queue++; } return total_locks; } static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm, struct dlm_migratable_lockres *mres, u8 send_to, struct dlm_lock_resource *res, int total_locks) { u64 mig_cookie = be64_to_cpu(mres->mig_cookie); int mres_total_locks = be32_to_cpu(mres->total_locks); int sz, ret = 0, status = 0; u8 orig_flags = mres->flags, orig_master = mres->master; BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS); if (!mres->num_locks) return 0; sz = sizeof(struct dlm_migratable_lockres) + (mres->num_locks * sizeof(struct dlm_migratable_lock)); /* add an all-done flag if we reached the last lock */ orig_flags = mres->flags; BUG_ON(total_locks > mres_total_locks); if (total_locks == mres_total_locks) mres->flags |= DLM_MRES_ALL_DONE; mlog(0, "%s:%.*s: sending mig lockres (%s) to %u\n", dlm->name, res->lockname.len, res->lockname.name, orig_flags & DLM_MRES_MIGRATION ? "migration" : "recovery", send_to); /* send it */ ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres, sz, send_to, &status); if (ret < 0) { /* XXX: negative status is not handled. * this will end up killing this node. */ mlog(ML_ERROR, "%s: res %.*s, Error %d send MIG_LOCKRES to " "node %u (%s)\n", dlm->name, mres->lockname_len, mres->lockname, ret, send_to, (orig_flags & DLM_MRES_MIGRATION ? "migration" : "recovery")); } else { /* might get an -ENOMEM back here */ ret = status; if (ret < 0) { mlog_errno(ret); if (ret == -EFAULT) { mlog(ML_ERROR, "node %u told me to kill " "myself!\n", send_to); BUG(); } } } /* zero and reinit the message buffer */ dlm_init_migratable_lockres(mres, res->lockname.name, res->lockname.len, mres_total_locks, mig_cookie, orig_flags, orig_master); return ret; } static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres, const char *lockname, int namelen, int total_locks, u64 cookie, u8 flags, u8 master) { /* mres here is one full page */ clear_page(mres); mres->lockname_len = namelen; memcpy(mres->lockname, lockname, namelen); mres->num_locks = 0; mres->total_locks = cpu_to_be32(total_locks); mres->mig_cookie = cpu_to_be64(cookie); mres->flags = flags; mres->master = master; } static void dlm_prepare_lvb_for_migration(struct dlm_lock *lock, struct dlm_migratable_lockres *mres, int queue) { if (!lock->lksb) return; /* Ignore lvb in all locks in the blocked list */ if (queue == DLM_BLOCKED_LIST) return; /* Only consider lvbs in locks with granted EX or PR lock levels */ if (lock->ml.type != LKM_EXMODE && lock->ml.type != LKM_PRMODE) return; if (dlm_lvb_is_empty(mres->lvb)) { memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN); return; } /* Ensure the lvb copied for migration matches in other valid locks */ if (!memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN)) return; mlog(ML_ERROR, "Mismatched lvb in lock cookie=%u:%llu, name=%.*s, " "node=%u\n", dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)), dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)), lock->lockres->lockname.len, lock->lockres->lockname.name, lock->ml.node); dlm_print_one_lock_resource(lock->lockres); BUG(); } /* returns 1 if this lock fills the network structure, * 0 otherwise */ static int dlm_add_lock_to_array(struct dlm_lock *lock, struct dlm_migratable_lockres *mres, int queue) { struct dlm_migratable_lock *ml; int lock_num = mres->num_locks; ml = &(mres->ml[lock_num]); ml->cookie = lock->ml.cookie; ml->type = lock->ml.type; ml->convert_type = lock->ml.convert_type; ml->highest_blocked = lock->ml.highest_blocked; ml->list = queue; if (lock->lksb) { ml->flags = lock->lksb->flags; dlm_prepare_lvb_for_migration(lock, mres, queue); } ml->node = lock->ml.node; mres->num_locks++; /* we reached the max, send this network message */ if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS) return 1; return 0; } static void dlm_add_dummy_lock(struct dlm_ctxt *dlm, struct dlm_migratable_lockres *mres) { struct dlm_lock dummy; memset(&dummy, 0, sizeof(dummy)); dummy.ml.cookie = 0; dummy.ml.type = LKM_IVMODE; dummy.ml.convert_type = LKM_IVMODE; dummy.ml.highest_blocked = LKM_IVMODE; dummy.lksb = NULL; dummy.ml.node = dlm->node_num; dlm_add_lock_to_array(&dummy, mres, DLM_BLOCKED_LIST); } static inline int dlm_is_dummy_lock(struct dlm_ctxt *dlm, struct dlm_migratable_lock *ml, u8 *nodenum) { if (unlikely(ml->cookie == 0 && ml->type == LKM_IVMODE && ml->convert_type == LKM_IVMODE && ml->highest_blocked == LKM_IVMODE && ml->list == DLM_BLOCKED_LIST)) { *nodenum = ml->node; return 1; } return 0; } int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, struct dlm_migratable_lockres *mres, u8 send_to, u8 flags) { struct list_head *queue; int total_locks, i; u64 mig_cookie = 0; struct dlm_lock *lock; int ret = 0; BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION))); mlog(0, "sending to %u\n", send_to); total_locks = dlm_num_locks_in_lockres(res); if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) { /* rare, but possible */ mlog(0, "argh. lockres has %d locks. this will " "require more than one network packet to " "migrate\n", total_locks); mig_cookie = dlm_get_next_mig_cookie(); } dlm_init_migratable_lockres(mres, res->lockname.name, res->lockname.len, total_locks, mig_cookie, flags, res->owner); total_locks = 0; for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) { queue = dlm_list_idx_to_ptr(res, i); list_for_each_entry(lock, queue, list) { /* add another lock. */ total_locks++; if (!dlm_add_lock_to_array(lock, mres, i)) continue; /* this filled the lock message, * we must send it immediately. */ ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks); if (ret < 0) goto error; } } if (total_locks == 0) { /* send a dummy lock to indicate a mastery reference only */ mlog(0, "%s:%.*s: sending dummy lock to %u, %s\n", dlm->name, res->lockname.len, res->lockname.name, send_to, flags & DLM_MRES_RECOVERY ? "recovery" : "migration"); dlm_add_dummy_lock(dlm, mres); } /* flush any remaining locks */ ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks); if (ret < 0) goto error; return ret; error: mlog(ML_ERROR, "%s: dlm_send_mig_lockres_msg returned %d\n", dlm->name, ret); if (!dlm_is_host_down(ret)) BUG(); mlog(0, "%s: node %u went down while sending %s " "lockres %.*s\n", dlm->name, send_to, flags & DLM_MRES_RECOVERY ? "recovery" : "migration", res->lockname.len, res->lockname.name); return ret; } /* * this message will contain no more than one page worth of * recovery data, and it will work on only one lockres. * there may be many locks in this page, and we may need to wait * for additional packets to complete all the locks (rare, but * possible). */ /* * NOTE: the allocation error cases here are scary * we really cannot afford to fail an alloc in recovery * do we spin? returning an error only delays the problem really */ int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data, void **ret_data) { struct dlm_ctxt *dlm = data; struct dlm_migratable_lockres *mres = (struct dlm_migratable_lockres *)msg->buf; int ret = 0; u8 real_master; u8 extra_refs = 0; char *buf = NULL; struct dlm_work_item *item = NULL; struct dlm_lock_resource *res = NULL; if (!dlm_grab(dlm)) return -EINVAL; BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION))); real_master = mres->master; if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) { /* cannot migrate a lockres with no master */ BUG_ON(!(mres->flags & DLM_MRES_RECOVERY)); } mlog(0, "%s message received from node %u\n", (mres->flags & DLM_MRES_RECOVERY) ? "recovery" : "migration", mres->master); if (mres->flags & DLM_MRES_ALL_DONE) mlog(0, "all done flag. all lockres data received!\n"); ret = -ENOMEM; buf = kmalloc(be16_to_cpu(msg->data_len), GFP_NOFS); item = kzalloc(sizeof(*item), GFP_NOFS); if (!buf || !item) goto leave; /* lookup the lock to see if we have a secondary queue for this * already... just add the locks in and this will have its owner * and RECOVERY flag changed when it completes. */ res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len); if (res) { /* this will get a ref on res */ /* mark it as recovering/migrating and hash it */ spin_lock(&res->spinlock); if (mres->flags & DLM_MRES_RECOVERY) { res->state |= DLM_LOCK_RES_RECOVERING; } else { if (res->state & DLM_LOCK_RES_MIGRATING) { /* this is at least the second * lockres message */ mlog(0, "lock %.*s is already migrating\n", mres->lockname_len, mres->lockname); } else if (res->state & DLM_LOCK_RES_RECOVERING) { /* caller should BUG */ mlog(ML_ERROR, "node is attempting to migrate " "lock %.*s, but marked as recovering!\n", mres->lockname_len, mres->lockname); ret = -EFAULT; spin_unlock(&res->spinlock); goto leave; } res->state |= DLM_LOCK_RES_MIGRATING; } spin_unlock(&res->spinlock); } else { /* need to allocate, just like if it was * mastered here normally */ res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len); if (!res) goto leave; /* to match the ref that we would have gotten if * dlm_lookup_lockres had succeeded */ dlm_lockres_get(res); /* mark it as recovering/migrating and hash it */ if (mres->flags & DLM_MRES_RECOVERY) res->state |= DLM_LOCK_RES_RECOVERING; else res->state |= DLM_LOCK_RES_MIGRATING; spin_lock(&dlm->spinlock); __dlm_insert_lockres(dlm, res); spin_unlock(&dlm->spinlock); /* Add an extra ref for this lock-less lockres lest the * dlm_thread purges it before we get the chance to add * locks to it */ dlm_lockres_get(res); /* There are three refs that need to be put. * 1. Taken above. * 2. kref_init in dlm_new_lockres()->dlm_init_lockres(). * 3. dlm_lookup_lockres() * The first one is handled at the end of this function. The * other two are handled in the worker thread after locks have * been attached. Yes, we don't wait for purge time to match * kref_init. The lockres will still have atleast one ref * added because it is in the hash __dlm_insert_lockres() */ extra_refs++; /* now that the new lockres is inserted, * make it usable by other processes */ spin_lock(&res->spinlock); res->state &= ~DLM_LOCK_RES_IN_PROGRESS; spin_unlock(&res->spinlock); wake_up(&res->wq); } /* at this point we have allocated everything we need, * and we have a hashed lockres with an extra ref and * the proper res->state flags. */ ret = 0; spin_lock(&res->spinlock); /* drop this either when master requery finds a different master * or when a lock is added by the recovery worker */ dlm_lockres_grab_inflight_ref(dlm, res); if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) { /* migration cannot have an unknown master */ BUG_ON(!(mres->flags & DLM_MRES_RECOVERY)); mlog(0, "recovery has passed me a lockres with an " "unknown owner.. will need to requery: " "%.*s\n", mres->lockname_len, mres->lockname); } else { /* take a reference now to pin the lockres, drop it * when locks are added in the worker */ dlm_change_lockres_owner(dlm, res, dlm->node_num); } spin_unlock(&res->spinlock); /* queue up work for dlm_mig_lockres_worker */ dlm_grab(dlm); /* get an extra ref for the work item */ memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */ dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf); item->u.ml.lockres = res; /* already have a ref */ item->u.ml.real_master = real_master; item->u.ml.extra_ref = extra_refs; spin_lock(&dlm->work_lock); list_add_tail(&item->list, &dlm->work_list); spin_unlock(&dlm->work_lock); queue_work(dlm->dlm_worker, &dlm->dispatched_work); leave: /* One extra ref taken needs to be put here */ if (extra_refs) dlm_lockres_put(res); dlm_put(dlm); if (ret < 0) { kfree(buf); kfree(item); mlog_errno(ret); } return ret; } static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data) { struct dlm_ctxt *dlm; struct dlm_migratable_lockres *mres; int ret = 0; struct dlm_lock_resource *res; u8 real_master; u8 extra_ref; dlm = item->dlm; mres = (struct dlm_migratable_lockres *)data; res = item->u.ml.lockres; real_master = item->u.ml.real_master; extra_ref = item->u.ml.extra_ref; if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) { /* this case is super-rare. only occurs if * node death happens during migration. */ again: ret = dlm_lockres_master_requery(dlm, res, &real_master); if (ret < 0) { mlog(0, "dlm_lockres_master_requery ret=%d\n", ret); goto again; } if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) { mlog(0, "lockres %.*s not claimed. " "this node will take it.\n", res->lockname.len, res->lockname.name); } else { spin_lock(&res->spinlock); dlm_lockres_drop_inflight_ref(dlm, res); spin_unlock(&res->spinlock); mlog(0, "master needs to respond to sender " "that node %u still owns %.*s\n", real_master, res->lockname.len, res->lockname.name); /* cannot touch this lockres */ goto leave; } } ret = dlm_process_recovery_data(dlm, res, mres); if (ret < 0) mlog(0, "dlm_process_recovery_data returned %d\n", ret); else mlog(0, "dlm_process_recovery_data succeeded\n"); if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) == (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) { ret = dlm_finish_migration(dlm, res, mres->master); if (ret < 0) mlog_errno(ret); } leave: /* See comment in dlm_mig_lockres_handler() */ if (res) { if (extra_ref) dlm_lockres_put(res); dlm_lockres_put(res); } kfree(data); } static int dlm_lockres_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, u8 *real_master) { struct dlm_node_iter iter; int nodenum; int ret = 0; *real_master = DLM_LOCK_RES_OWNER_UNKNOWN; /* we only reach here if one of the two nodes in a * migration died while the migration was in progress. * at this point we need to requery the master. we * know that the new_master got as far as creating * an mle on at least one node, but we do not know * if any nodes had actually cleared the mle and set * the master to the new_master. the old master * is supposed to set the owner to UNKNOWN in the * event of a new_master death, so the only possible * responses that we can get from nodes here are * that the master is new_master, or that the master * is UNKNOWN. * if all nodes come back with UNKNOWN then we know * the lock needs remastering here. * if any node comes back with a valid master, check * to see if that master is the one that we are * recovering. if so, then the new_master died and * we need to remaster this lock. if not, then the * new_master survived and that node will respond to * other nodes about the owner. * if there is an owner, this node needs to dump this * lockres and alert the sender that this lockres * was rejected. */ spin_lock(&dlm->spinlock); dlm_node_iter_init(dlm->domain_map, &iter); spin_unlock(&dlm->spinlock); while ((nodenum = dlm_node_iter_next(&iter)) >= 0) { /* do not send to self */ if (nodenum == dlm->node_num) continue; ret = dlm_do_master_requery(dlm, res, nodenum, real_master); if (ret < 0) { mlog_errno(ret); if (!dlm_is_host_down(ret)) BUG(); /* host is down, so answer for that node would be * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */ } if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) { mlog(0, "lock master is %u\n", *real_master); break; } } return ret; } int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, u8 nodenum, u8 *real_master) { int ret = -EINVAL; struct dlm_master_requery req; int status = DLM_LOCK_RES_OWNER_UNKNOWN; memset(&req, 0, sizeof(req)); req.node_idx = dlm->node_num; req.namelen = res->lockname.len; memcpy(req.name, res->lockname.name, res->lockname.len); ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key, &req, sizeof(req), nodenum, &status); /* XXX: negative status not handled properly here. */ if (ret < 0) mlog(ML_ERROR, "Error %d when sending message %u (key " "0x%x) to node %u\n", ret, DLM_MASTER_REQUERY_MSG, dlm->key, nodenum); else { BUG_ON(status < 0); BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN); *real_master = (u8) (status & 0xff); mlog(0, "node %u responded to master requery with %u\n", nodenum, *real_master); ret = 0; } return ret; } /* this function cannot error, so unless the sending * or receiving of the message failed, the owner can * be trusted */ int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data, void **ret_data) { struct dlm_ctxt *dlm = data; struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf; struct dlm_lock_resource *res = NULL; unsigned int hash; int master = DLM_LOCK_RES_OWNER_UNKNOWN; u32 flags = DLM_ASSERT_MASTER_REQUERY; if (!dlm_grab(dlm)) { /* since the domain has gone away on this * node, the proper response is UNKNOWN */ return master; } hash = dlm_lockid_hash(req->name, req->namelen); spin_lock(&dlm->spinlock); res = __dlm_lookup_lockres(dlm, req->name, req->namelen, hash); if (res) { spin_lock(&res->spinlock); master = res->owner; if (master == dlm->node_num) { int ret = dlm_dispatch_assert_master(dlm, res, 0, 0, flags); if (ret < 0) { mlog_errno(-ENOMEM); /* retry!? */ BUG(); } } else /* put.. incase we are not the master */ dlm_lockres_put(res); spin_unlock(&res->spinlock); } spin_unlock(&dlm->spinlock); dlm_put(dlm); return master; } static inline struct list_head * dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num) { struct list_head *ret; BUG_ON(list_num < 0); BUG_ON(list_num > 2); ret = &(res->granted); ret += list_num; return ret; } /* TODO: do ast flush business * TODO: do MIGRATING and RECOVERING spinning */ /* * NOTE about in-flight requests during migration: * * Before attempting the migrate, the master has marked the lockres as * MIGRATING and then flushed all of its pending ASTS. So any in-flight * requests either got queued before the MIGRATING flag got set, in which * case the lock data will reflect the change and a return message is on * the way, or the request failed to get in before MIGRATING got set. In * this case, the caller will be told to spin and wait for the MIGRATING * flag to be dropped, then recheck the master. * This holds true for the convert, cancel and unlock cases, and since lvb * updates are tied to these same messages, it applies to lvb updates as * well. For the lock case, there is no way a lock can be on the master * queue and not be on the secondary queue since the lock is always added * locally first. This means that the new target node will never be sent * a lock that he doesn't already have on the list. * In total, this means that the local lock is correct and should not be * updated to match the one sent by the master. Any messages sent back * from the master before the MIGRATING flag will bring the lock properly * up-to-date, and the change will be ordered properly for the waiter. * We will *not* attempt to modify the lock underneath the waiter. */ static int dlm_process_recovery_data(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, struct dlm_migratable_lockres *mres) { struct dlm_migratable_lock *ml; struct list_head *queue; struct list_head *tmpq = NULL; struct dlm_lock *newlock = NULL; struct dlm_lockstatus *lksb = NULL; int ret = 0; int i, j, bad; struct dlm_lock *lock = NULL; u8 from = O2NM_MAX_NODES; unsigned int added = 0; __be64 c; mlog(0, "running %d locks for this lockres\n", mres->num_locks); for (i=0; i<mres->num_locks; i++) { ml = &(mres->ml[i]); if (dlm_is_dummy_lock(dlm, ml, &from)) { /* placeholder, just need to set the refmap bit */ BUG_ON(mres->num_locks != 1); mlog(0, "%s:%.*s: dummy lock for %u\n", dlm->name, mres->lockname_len, mres->lockname, from); spin_lock(&res->spinlock); dlm_lockres_set_refmap_bit(dlm, res, from); spin_unlock(&res->spinlock); added++; break; } BUG_ON(ml->highest_blocked != LKM_IVMODE); newlock = NULL; lksb = NULL; queue = dlm_list_num_to_pointer(res, ml->list); tmpq = NULL; /* if the lock is for the local node it needs to * be moved to the proper location within the queue. * do not allocate a new lock structure. */ if (ml->node == dlm->node_num) { /* MIGRATION ONLY! */ BUG_ON(!(mres->flags & DLM_MRES_MIGRATION)); spin_lock(&res->spinlock); for (j = DLM_GRANTED_LIST; j <= DLM_BLOCKED_LIST; j++) { tmpq = dlm_list_idx_to_ptr(res, j); list_for_each_entry(lock, tmpq, list) { if (lock->ml.cookie != ml->cookie) lock = NULL; else break; } if (lock) break; } /* lock is always created locally first, and * destroyed locally last. it must be on the list */ if (!lock) { c = ml->cookie; mlog(ML_ERROR, "Could not find local lock " "with cookie %u:%llu, node %u, " "list %u, flags 0x%x, type %d, " "conv %d, highest blocked %d\n", dlm_get_lock_cookie_node(be64_to_cpu(c)), dlm_get_lock_cookie_seq(be64_to_cpu(c)), ml->node, ml->list, ml->flags, ml->type, ml->convert_type, ml->highest_blocked); __dlm_print_one_lock_resource(res); BUG(); } if (lock->ml.node != ml->node) { c = lock->ml.cookie; mlog(ML_ERROR, "Mismatched node# in lock " "cookie %u:%llu, name %.*s, node %u\n", dlm_get_lock_cookie_node(be64_to_cpu(c)), dlm_get_lock_cookie_seq(be64_to_cpu(c)), res->lockname.len, res->lockname.name, lock->ml.node); c = ml->cookie; mlog(ML_ERROR, "Migrate lock cookie %u:%llu, " "node %u, list %u, flags 0x%x, type %d, " "conv %d, highest blocked %d\n", dlm_get_lock_cookie_node(be64_to_cpu(c)), dlm_get_lock_cookie_seq(be64_to_cpu(c)), ml->node, ml->list, ml->flags, ml->type, ml->convert_type, ml->highest_blocked); __dlm_print_one_lock_resource(res); BUG(); } if (tmpq != queue) { c = ml->cookie; mlog(0, "Lock cookie %u:%llu was on list %u " "instead of list %u for %.*s\n", dlm_get_lock_cookie_node(be64_to_cpu(c)), dlm_get_lock_cookie_seq(be64_to_cpu(c)), j, ml->list, res->lockname.len, res->lockname.name); __dlm_print_one_lock_resource(res); spin_unlock(&res->spinlock); continue; } /* see NOTE above about why we do not update * to match the master here */ /* move the lock to its proper place */ /* do not alter lock refcount. switching lists. */ list_move_tail(&lock->list, queue); spin_unlock(&res->spinlock); added++; mlog(0, "just reordered a local lock!\n"); continue; } /* lock is for another node. */ newlock = dlm_new_lock(ml->type, ml->node, be64_to_cpu(ml->cookie), NULL); if (!newlock) { ret = -ENOMEM; goto leave; } lksb = newlock->lksb; dlm_lock_attach_lockres(newlock, res); if (ml->convert_type != LKM_IVMODE) { BUG_ON(queue != &res->converting); newlock->ml.convert_type = ml->convert_type; } lksb->flags |= (ml->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB)); if (ml->type == LKM_NLMODE) goto skip_lvb; if (!dlm_lvb_is_empty(mres->lvb)) { if (lksb->flags & DLM_LKSB_PUT_LVB) { /* other node was trying to update * lvb when node died. recreate the * lksb with the updated lvb. */ memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN); /* the lock resource lvb update must happen * NOW, before the spinlock is dropped. * we no longer wait for the AST to update * the lvb. */ memcpy(res->lvb, mres->lvb, DLM_LVB_LEN); } else { /* otherwise, the node is sending its * most recent valid lvb info */ BUG_ON(ml->type != LKM_EXMODE && ml->type != LKM_PRMODE); if (!dlm_lvb_is_empty(res->lvb) && (ml->type == LKM_EXMODE || memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) { int i; mlog(ML_ERROR, "%s:%.*s: received bad " "lvb! type=%d\n", dlm->name, res->lockname.len, res->lockname.name, ml->type); printk("lockres lvb=["); for (i=0; i<DLM_LVB_LEN; i++) printk("%02x", res->lvb[i]); printk("]\nmigrated lvb=["); for (i=0; i<DLM_LVB_LEN; i++) printk("%02x", mres->lvb[i]); printk("]\n"); dlm_print_one_lock_resource(res); BUG(); } memcpy(res->lvb, mres->lvb, DLM_LVB_LEN); } } skip_lvb: /* NOTE: * wrt lock queue ordering and recovery: * 1. order of locks on granted queue is * meaningless. * 2. order of locks on converting queue is * LOST with the node death. sorry charlie. * 3. order of locks on the blocked queue is * also LOST. * order of locks does not affect integrity, it * just means that a lock request may get pushed * back in line as a result of the node death. * also note that for a given node the lock order * for its secondary queue locks is preserved * relative to each other, but clearly *not* * preserved relative to locks from other nodes. */ bad = 0; spin_lock(&res->spinlock); list_for_each_entry(lock, queue, list) { if (lock->ml.cookie == ml->cookie) { c = lock->ml.cookie; mlog(ML_ERROR, "%s:%.*s: %u:%llu: lock already " "exists on this lockres!\n", dlm->name, res->lockname.len, res->lockname.name, dlm_get_lock_cookie_node(be64_to_cpu(c)), dlm_get_lock_cookie_seq(be64_to_cpu(c))); mlog(ML_NOTICE, "sent lock: type=%d, conv=%d, " "node=%u, cookie=%u:%llu, queue=%d\n", ml->type, ml->convert_type, ml->node, dlm_get_lock_cookie_node(be64_to_cpu(ml->cookie)), dlm_get_lock_cookie_seq(be64_to_cpu(ml->cookie)), ml->list); __dlm_print_one_lock_resource(res); bad = 1; break; } } if (!bad) { dlm_lock_get(newlock); list_add_tail(&newlock->list, queue); mlog(0, "%s:%.*s: added lock for node %u, " "setting refmap bit\n", dlm->name, res->lockname.len, res->lockname.name, ml->node); dlm_lockres_set_refmap_bit(dlm, res, ml->node); added++; } spin_unlock(&res->spinlock); } mlog(0, "done running all the locks\n"); leave: /* balance the ref taken when the work was queued */ spin_lock(&res->spinlock); dlm_lockres_drop_inflight_ref(dlm, res); spin_unlock(&res->spinlock); if (ret < 0) { mlog_errno(ret); if (newlock) dlm_lock_put(newlock); } return ret; } void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm, struct dlm_lock_resource *res) { int i; struct list_head *queue; struct dlm_lock *lock, *next; assert_spin_locked(&dlm->spinlock); assert_spin_locked(&res->spinlock); res->state |= DLM_LOCK_RES_RECOVERING; if (!list_empty(&res->recovering)) { mlog(0, "Recovering res %s:%.*s, is already on recovery list!\n", dlm->name, res->lockname.len, res->lockname.name); list_del_init(&res->recovering); dlm_lockres_put(res); } /* We need to hold a reference while on the recovery list */ dlm_lockres_get(res); list_add_tail(&res->recovering, &dlm->reco.resources); /* find any pending locks and put them back on proper list */ for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) { queue = dlm_list_idx_to_ptr(res, i); list_for_each_entry_safe(lock, next, queue, list) { dlm_lock_get(lock); if (lock->convert_pending) { /* move converting lock back to granted */ BUG_ON(i != DLM_CONVERTING_LIST); mlog(0, "node died with convert pending " "on %.*s. move back to granted list.\n", res->lockname.len, res->lockname.name); dlm_revert_pending_convert(res, lock); lock->convert_pending = 0; } else if (lock->lock_pending) { /* remove pending lock requests completely */ BUG_ON(i != DLM_BLOCKED_LIST); mlog(0, "node died with lock pending " "on %.*s. remove from blocked list and skip.\n", res->lockname.len, res->lockname.name); /* lock will be floating until ref in * dlmlock_remote is freed after the network * call returns. ok for it to not be on any * list since no ast can be called * (the master is dead). */ dlm_revert_pending_lock(res, lock); lock->lock_pending = 0; } else if (lock->unlock_pending) { /* if an unlock was in progress, treat as * if this had completed successfully * before sending this lock state to the * new master. note that the dlm_unlock * call is still responsible for calling * the unlockast. that will happen after * the network call times out. for now, * just move lists to prepare the new * recovery master. */ BUG_ON(i != DLM_GRANTED_LIST); mlog(0, "node died with unlock pending " "on %.*s. remove from blocked list and skip.\n", res->lockname.len, res->lockname.name); dlm_commit_pending_unlock(res, lock); lock->unlock_pending = 0; } else if (lock->cancel_pending) { /* if a cancel was in progress, treat as * if this had completed successfully * before sending this lock state to the * new master */ BUG_ON(i != DLM_CONVERTING_LIST); mlog(0, "node died with cancel pending " "on %.*s. move back to granted list.\n", res->lockname.len, res->lockname.name); dlm_commit_pending_cancel(res, lock); lock->cancel_pending = 0; } dlm_lock_put(lock); } } } /* removes all recovered locks from the recovery list. * sets the res->owner to the new master. * unsets the RECOVERY flag and wakes waiters. */ static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm, u8 dead_node, u8 new_master) { int i; struct hlist_head *bucket; struct dlm_lock_resource *res, *next; assert_spin_locked(&dlm->spinlock); list_for_each_entry_safe(res, next, &dlm->reco.resources, recovering) { if (res->owner == dead_node) { mlog(0, "%s: res %.*s, Changing owner from %u to %u\n", dlm->name, res->lockname.len, res->lockname.name, res->owner, new_master); list_del_init(&res->recovering); spin_lock(&res->spinlock); /* new_master has our reference from * the lock state sent during recovery */ dlm_change_lockres_owner(dlm, res, new_master); res->state &= ~DLM_LOCK_RES_RECOVERING; if (__dlm_lockres_has_locks(res)) __dlm_dirty_lockres(dlm, res); spin_unlock(&res->spinlock); wake_up(&res->wq); dlm_lockres_put(res); } } /* this will become unnecessary eventually, but * for now we need to run the whole hash, clear * the RECOVERING state and set the owner * if necessary */ for (i = 0; i < DLM_HASH_BUCKETS; i++) { bucket = dlm_lockres_hash(dlm, i); hlist_for_each_entry(res, bucket, hash_node) { if (!(res->state & DLM_LOCK_RES_RECOVERING)) continue; if (res->owner != dead_node && res->owner != dlm->node_num) continue; if (!list_empty(&res->recovering)) { list_del_init(&res->recovering); dlm_lockres_put(res); } /* new_master has our reference from * the lock state sent during recovery */ mlog(0, "%s: res %.*s, Changing owner from %u to %u\n", dlm->name, res->lockname.len, res->lockname.name, res->owner, new_master); spin_lock(&res->spinlock); dlm_change_lockres_owner(dlm, res, new_master); res->state &= ~DLM_LOCK_RES_RECOVERING; if (__dlm_lockres_has_locks(res)) __dlm_dirty_lockres(dlm, res); spin_unlock(&res->spinlock); wake_up(&res->wq); } } } static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local) { if (local) { if (lock->ml.type != LKM_EXMODE && lock->ml.type != LKM_PRMODE) return 1; } else if (lock->ml.type == LKM_EXMODE) return 1; return 0; } static void dlm_revalidate_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, u8 dead_node) { struct list_head *queue; struct dlm_lock *lock; int blank_lvb = 0, local = 0; int i; u8 search_node; assert_spin_locked(&dlm->spinlock); assert_spin_locked(&res->spinlock); if (res->owner == dlm->node_num) /* if this node owned the lockres, and if the dead node * had an EX when he died, blank out the lvb */ search_node = dead_node; else { /* if this is a secondary lockres, and we had no EX or PR * locks granted, we can no longer trust the lvb */ search_node = dlm->node_num; local = 1; /* check local state for valid lvb */ } for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) { queue = dlm_list_idx_to_ptr(res, i); list_for_each_entry(lock, queue, list) { if (lock->ml.node == search_node) { if (dlm_lvb_needs_invalidation(lock, local)) { /* zero the lksb lvb and lockres lvb */ blank_lvb = 1; memset(lock->lksb->lvb, 0, DLM_LVB_LEN); } } } } if (blank_lvb) { mlog(0, "clearing %.*s lvb, dead node %u had EX\n", res->lockname.len, res->lockname.name, dead_node); memset(res->lvb, 0, DLM_LVB_LEN); } } static void dlm_free_dead_locks(struct dlm_ctxt *dlm, struct dlm_lock_resource *res, u8 dead_node) { struct dlm_lock *lock, *next; unsigned int freed = 0; /* this node is the lockres master: * 1) remove any stale locks for the dead node * 2) if the dead node had an EX when he died, blank out the lvb */ assert_spin_locked(&dlm->spinlock); assert_spin_locked(&res->spinlock); /* We do two dlm_lock_put(). One for removing from list and the other is * to force the DLM_UNLOCK_FREE_LOCK action so as to free the locks */ /* TODO: check pending_asts, pending_basts here */ list_for_each_entry_safe(lock, next, &res->granted, list) { if (lock->ml.node == dead_node) { list_del_init(&lock->list); dlm_lock_put(lock); /* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */ dlm_lock_put(lock); freed++; } } list_for_each_entry_safe(lock, next, &res->converting, list) { if (lock->ml.node == dead_node) { list_del_init(&lock->list); dlm_lock_put(lock); /* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */ dlm_lock_put(lock); freed++; } } list_for_each_entry_safe(lock, next, &res->blocked, list) { if (lock->ml.node == dead_node) { list_del_init(&lock->list); dlm_lock_put(lock); /* Can't schedule DLM_UNLOCK_FREE_LOCK - do manually */ dlm_lock_put(lock); freed++; } } if (freed) { mlog(0, "%s:%.*s: freed %u locks for dead node %u, " "dropping ref from lockres\n", dlm->name, res->lockname.len, res->lockname.name, freed, dead_node); if(!test_bit(dead_node, res->refmap)) { mlog(ML_ERROR, "%s:%.*s: freed %u locks for dead node %u, " "but ref was not set\n", dlm->name, res->lockname.len, res->lockname.name, freed, dead_node); __dlm_print_one_lock_resource(res); } dlm_lockres_clear_refmap_bit(dlm, res, dead_node); } else if (test_bit(dead_node, res->refmap)) { mlog(0, "%s:%.*s: dead node %u had a ref, but had " "no locks and had not purged before dying\n", dlm->name, res->lockname.len, res->lockname.name, dead_node); dlm_lockres_clear_refmap_bit(dlm, res, dead_node); } /* do not kick thread yet */ __dlm_dirty_lockres(dlm, res); } /* if this node is the recovery master, and there are no * locks for a given lockres owned by this node that are in * either PR or EX mode, zero out the lvb before requesting. * */ static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node) { struct dlm_lock_resource *res; int i; struct hlist_head *bucket; struct dlm_lock *lock; /* purge any stale mles */ dlm_clean_master_list(dlm, dead_node); /* * now clean up all lock resources. there are two rules: * * 1) if the dead node was the master, move the lockres * to the recovering list. set the RECOVERING flag. * this lockres needs to be cleaned up before it can * be used further. * * 2) if this node was the master, remove all locks from * each of the lockres queues that were owned by the * dead node. once recovery finishes, the dlm thread * can be kicked again to see if any ASTs or BASTs * need to be fired as a result. */ for (i = 0; i < DLM_HASH_BUCKETS; i++) { bucket = dlm_lockres_hash(dlm, i); hlist_for_each_entry(res, bucket, hash_node) { /* always prune any $RECOVERY entries for dead nodes, * otherwise hangs can occur during later recovery */ if (dlm_is_recovery_lock(res->lockname.name, res->lockname.len)) { spin_lock(&res->spinlock); list_for_each_entry(lock, &res->granted, list) { if (lock->ml.node == dead_node) { mlog(0, "AHA! there was " "a $RECOVERY lock for dead " "node %u (%s)!\n", dead_node, dlm->name); list_del_init(&lock->list); dlm_lock_put(lock); break; } } spin_unlock(&res->spinlock); continue; } spin_lock(&res->spinlock); /* zero the lvb if necessary */ dlm_revalidate_lvb(dlm, res, dead_node); if (res->owner == dead_node) { if (res->state & DLM_LOCK_RES_DROPPING_REF) { mlog(ML_NOTICE, "%s: res %.*s, Skip " "recovery as it is being freed\n", dlm->name, res->lockname.len, res->lockname.name); } else dlm_move_lockres_to_recovery_list(dlm, res); } else if (res->owner == dlm->node_num) { dlm_free_dead_locks(dlm, res, dead_node); __dlm_lockres_calc_usage(dlm, res); } spin_unlock(&res->spinlock); } } } static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx) { assert_spin_locked(&dlm->spinlock); if (dlm->reco.new_master == idx) { mlog(0, "%s: recovery master %d just died\n", dlm->name, idx); if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) { /* finalize1 was reached, so it is safe to clear * the new_master and dead_node. that recovery * is complete. */ mlog(0, "%s: dead master %d had reached " "finalize1 state, clearing\n", dlm->name, idx); dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE; __dlm_reset_recovery(dlm); } } /* Clean up join state on node death. */ if (dlm->joining_node == idx) { mlog(0, "Clearing join state for node %u\n", idx); __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN); } /* check to see if the node is already considered dead */ if (!test_bit(idx, dlm->live_nodes_map)) { mlog(0, "for domain %s, node %d is already dead. " "another node likely did recovery already.\n", dlm->name, idx); return; } /* check to see if we do not care about this node */ if (!test_bit(idx, dlm->domain_map)) { /* This also catches the case that we get a node down * but haven't joined the domain yet. */ mlog(0, "node %u already removed from domain!\n", idx); return; } clear_bit(idx, dlm->live_nodes_map); /* make sure local cleanup occurs before the heartbeat events */ if (!test_bit(idx, dlm->recovery_map)) dlm_do_local_recovery_cleanup(dlm, idx); /* notify anything attached to the heartbeat events */ dlm_hb_event_notify_attached(dlm, idx, 0); mlog(0, "node %u being removed from domain map!\n", idx); clear_bit(idx, dlm->domain_map); clear_bit(idx, dlm->exit_domain_map); /* wake up migration waiters if a node goes down. * perhaps later we can genericize this for other waiters. */ wake_up(&dlm->migration_wq); if (test_bit(idx, dlm->recovery_map)) mlog(0, "domain %s, node %u already added " "to recovery map!\n", dlm->name, idx); else set_bit(idx, dlm->recovery_map); } void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data) { struct dlm_ctxt *dlm = data; if (!dlm_grab(dlm)) return; /* * This will notify any dlm users that a node in our domain * went away without notifying us first. */ if (test_bit(idx, dlm->domain_map)) dlm_fire_domain_eviction_callbacks(dlm, idx); spin_lock(&dlm->spinlock); __dlm_hb_node_down(dlm, idx); spin_unlock(&dlm->spinlock); dlm_put(dlm); } void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data) { struct dlm_ctxt *dlm = data; if (!dlm_grab(dlm)) return; spin_lock(&dlm->spinlock); set_bit(idx, dlm->live_nodes_map); /* do NOT notify mle attached to the heartbeat events. * new nodes are not interesting in mastery until joined. */ spin_unlock(&dlm->spinlock); dlm_put(dlm); } static void dlm_reco_ast(void *astdata) { struct dlm_ctxt *dlm = astdata; mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n", dlm->node_num, dlm->name); } static void dlm_reco_bast(void *astdata, int blocked_type) { struct dlm_ctxt *dlm = astdata; mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n", dlm->node_num, dlm->name); } static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st) { mlog(0, "unlockast for recovery lock fired!\n"); } /* * dlm_pick_recovery_master will continually attempt to use * dlmlock() on the special "$RECOVERY" lockres with the * LKM_NOQUEUE flag to get an EX. every thread that enters * this function on each node racing to become the recovery * master will not stop attempting this until either: * a) this node gets the EX (and becomes the recovery master), * or b) dlm->reco.new_master gets set to some nodenum * != O2NM_INVALID_NODE_NUM (another node will do the reco). * so each time a recovery master is needed, the entire cluster * will sync at this point. if the new master dies, that will * be detected in dlm_do_recovery */ static int dlm_pick_recovery_master(struct dlm_ctxt *dlm) { enum dlm_status ret; struct dlm_lockstatus lksb; int status = -EINVAL; mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n", dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num); again: memset(&lksb, 0, sizeof(lksb)); ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY, DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN, dlm_reco_ast, dlm, dlm_reco_bast); mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n", dlm->name, ret, lksb.status); if (ret == DLM_NORMAL) { mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n", dlm->name, dlm->node_num); /* got the EX lock. check to see if another node * just became the reco master */ if (dlm_reco_master_ready(dlm)) { mlog(0, "%s: got reco EX lock, but %u will " "do the recovery\n", dlm->name, dlm->reco.new_master); status = -EEXIST; } else { status = 0; /* see if recovery was already finished elsewhere */ spin_lock(&dlm->spinlock); if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) { status = -EINVAL; mlog(0, "%s: got reco EX lock, but " "node got recovered already\n", dlm->name); if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) { mlog(ML_ERROR, "%s: new master is %u " "but no dead node!\n", dlm->name, dlm->reco.new_master); BUG(); } } spin_unlock(&dlm->spinlock); } /* if this node has actually become the recovery master, * set the master and send the messages to begin recovery */ if (!status) { mlog(0, "%s: dead=%u, this=%u, sending " "begin_reco now\n", dlm->name, dlm->reco.dead_node, dlm->node_num); status = dlm_send_begin_reco_message(dlm, dlm->reco.dead_node); /* this always succeeds */ BUG_ON(status); /* set the new_master to this node */ spin_lock(&dlm->spinlock); dlm_set_reco_master(dlm, dlm->node_num); spin_unlock(&dlm->spinlock); } /* recovery lock is a special case. ast will not get fired, * so just go ahead and unlock it. */ ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm); if (ret == DLM_DENIED) { mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n"); ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm); } if (ret != DLM_NORMAL) { /* this would really suck. this could only happen * if there was a network error during the unlock * because of node death. this means the unlock * is actually "done" and the lock structure is * even freed. we can continue, but only * because this specific lock name is special. */ mlog(ML_ERROR, "dlmunlock returned %d\n", ret); } } else if (ret == DLM_NOTQUEUED) { mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n", dlm->name, dlm->node_num); /* another node is master. wait on * reco.new_master != O2NM_INVALID_NODE_NUM * for at most one second */ wait_event_timeout(dlm->dlm_reco_thread_wq, dlm_reco_master_ready(dlm), msecs_to_jiffies(1000)); if (!dlm_reco_master_ready(dlm)) { mlog(0, "%s: reco master taking awhile\n", dlm->name); goto again; } /* another node has informed this one that it is reco master */ mlog(0, "%s: reco master %u is ready to recover %u\n", dlm->name, dlm->reco.new_master, dlm->reco.dead_node); status = -EEXIST; } else if (ret == DLM_RECOVERING) { mlog(0, "dlm=%s dlmlock says master node died (this=%u)\n", dlm->name, dlm->node_num); goto again; } else { struct dlm_lock_resource *res; /* dlmlock returned something other than NOTQUEUED or NORMAL */ mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), " "lksb.status=%s\n", dlm->name, dlm_errname(ret), dlm_errname(lksb.status)); res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN); if (res) { dlm_print_one_lock_resource(res); dlm_lockres_put(res); } else { mlog(ML_ERROR, "recovery lock not found\n"); } BUG(); } return status; } static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node) { struct dlm_begin_reco br; int ret = 0; struct dlm_node_iter iter; int nodenum; int status; mlog(0, "%s: dead node is %u\n", dlm->name, dead_node); spin_lock(&dlm->spinlock); dlm_node_iter_init(dlm->domain_map, &iter); spin_unlock(&dlm->spinlock); clear_bit(dead_node, iter.node_map); memset(&br, 0, sizeof(br)); br.node_idx = dlm->node_num; br.dead_node = dead_node; while ((nodenum = dlm_node_iter_next(&iter)) >= 0) { ret = 0; if (nodenum == dead_node) { mlog(0, "not sending begin reco to dead node " "%u\n", dead_node); continue; } if (nodenum == dlm->node_num) { mlog(0, "not sending begin reco to self\n"); continue; } retry: ret = -EINVAL; mlog(0, "attempting to send begin reco msg to %d\n", nodenum); ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key, &br, sizeof(br), nodenum, &status); /* negative status is handled ok by caller here */ if (ret >= 0) ret = status; if (dlm_is_host_down(ret)) { /* node is down. not involved in recovery * so just keep going */ mlog(ML_NOTICE, "%s: node %u was down when sending " "begin reco msg (%d)\n", dlm->name, nodenum, ret); ret = 0; } /* * Prior to commit aad1b15310b9bcd59fa81ab8f2b1513b59553ea8, * dlm_begin_reco_handler() returned EAGAIN and not -EAGAIN. * We are handling both for compatibility reasons. */ if (ret == -EAGAIN || ret == EAGAIN) { mlog(0, "%s: trying to start recovery of node " "%u, but node %u is waiting for last recovery " "to complete, backoff for a bit\n", dlm->name, dead_node, nodenum); msleep(100); goto retry; } if (ret < 0) { struct dlm_lock_resource *res; /* this is now a serious problem, possibly ENOMEM * in the network stack. must retry */ mlog_errno(ret); mlog(ML_ERROR, "begin reco of dlm %s to node %u " "returned %d\n", dlm->name, nodenum, ret); res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME, DLM_RECOVERY_LOCK_NAME_LEN); if (res) { dlm_print_one_lock_resource(res); dlm_lockres_put(res); } else { mlog(ML_ERROR, "recovery lock not found\n"); } /* sleep for a bit in hopes that we can avoid * another ENOMEM */ msleep(100); goto retry; } } return ret; } int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data, void **ret_data) { struct dlm_ctxt *dlm = data; struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf; /* ok to return 0, domain has gone away */ if (!dlm_grab(dlm)) return 0; spin_lock(&dlm->spinlock); if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) { mlog(0, "%s: node %u wants to recover node %u (%u:%u) " "but this node is in finalize state, waiting on finalize2\n", dlm->name, br->node_idx, br->dead_node, dlm->reco.dead_node, dlm->reco.new_master); spin_unlock(&dlm->spinlock); return -EAGAIN; } spin_unlock(&dlm->spinlock); mlog(0, "%s: node %u wants to recover node %u (%u:%u)\n", dlm->name, br->node_idx, br->dead_node, dlm->reco.dead_node, dlm->reco.new_master); dlm_fire_domain_eviction_callbacks(dlm, br->dead_node); spin_lock(&dlm->spinlock); if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) { if (test_bit(dlm->reco.new_master, dlm->recovery_map)) { mlog(0, "%s: new_master %u died, changing " "to %u\n", dlm->name, dlm->reco.new_master, br->node_idx); } else { mlog(0, "%s: new_master %u NOT DEAD, changing " "to %u\n", dlm->name, dlm->reco.new_master, br->node_idx); /* may not have seen the new master as dead yet */ } } if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) { mlog(ML_NOTICE, "%s: dead_node previously set to %u, " "node %u changing it to %u\n", dlm->name, dlm->reco.dead_node, br->node_idx, br->dead_node); } dlm_set_reco_master(dlm, br->node_idx); dlm_set_reco_dead_node(dlm, br->dead_node); if (!test_bit(br->dead_node, dlm->recovery_map)) { mlog(0, "recovery master %u sees %u as dead, but this " "node has not yet. marking %u as dead\n", br->node_idx, br->dead_node, br->dead_node); if (!test_bit(br->dead_node, dlm->domain_map) || !test_bit(br->dead_node, dlm->live_nodes_map)) mlog(0, "%u not in domain/live_nodes map " "so setting it in reco map manually\n", br->dead_node); /* force the recovery cleanup in __dlm_hb_node_down * both of these will be cleared in a moment */ set_bit(br->dead_node, dlm->domain_map); set_bit(br->dead_node, dlm->live_nodes_map); __dlm_hb_node_down(dlm, br->dead_node); } spin_unlock(&dlm->spinlock); dlm_kick_recovery_thread(dlm); mlog(0, "%s: recovery started by node %u, for %u (%u:%u)\n", dlm->name, br->node_idx, br->dead_node, dlm->reco.dead_node, dlm->reco.new_master); dlm_put(dlm); return 0; } #define DLM_FINALIZE_STAGE2 0x01 static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm) { int ret = 0; struct dlm_finalize_reco fr; struct dlm_node_iter iter; int nodenum; int status; int stage = 1; mlog(0, "finishing recovery for node %s:%u, " "stage %d\n", dlm->name, dlm->reco.dead_node, stage); spin_lock(&dlm->spinlock); dlm_node_iter_init(dlm->domain_map, &iter); spin_unlock(&dlm->spinlock); stage2: memset(&fr, 0, sizeof(fr)); fr.node_idx = dlm->node_num; fr.dead_node = dlm->reco.dead_node; if (stage == 2) fr.flags |= DLM_FINALIZE_STAGE2; while ((nodenum = dlm_node_iter_next(&iter)) >= 0) { if (nodenum == dlm->node_num) continue; ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key, &fr, sizeof(fr), nodenum, &status); if (ret >= 0) ret = status; if (ret < 0) { mlog(ML_ERROR, "Error %d when sending message %u (key " "0x%x) to node %u\n", ret, DLM_FINALIZE_RECO_MSG, dlm->key, nodenum); if (dlm_is_host_down(ret)) { /* this has no effect on this recovery * session, so set the status to zero to * finish out the last recovery */ mlog(ML_ERROR, "node %u went down after this " "node finished recovery.\n", nodenum); ret = 0; continue; } break; } } if (stage == 1) { /* reset the node_iter back to the top and send finalize2 */ iter.curnode = -1; stage = 2; goto stage2; } return ret; } int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data, void **ret_data) { struct dlm_ctxt *dlm = data; struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf; int stage = 1; /* ok to return 0, domain has gone away */ if (!dlm_grab(dlm)) return 0; if (fr->flags & DLM_FINALIZE_STAGE2) stage = 2; mlog(0, "%s: node %u finalizing recovery stage%d of " "node %u (%u:%u)\n", dlm->name, fr->node_idx, stage, fr->dead_node, dlm->reco.dead_node, dlm->reco.new_master); spin_lock(&dlm->spinlock); if (dlm->reco.new_master != fr->node_idx) { mlog(ML_ERROR, "node %u sent recovery finalize msg, but node " "%u is supposed to be the new master, dead=%u\n", fr->node_idx, dlm->reco.new_master, fr->dead_node); BUG(); } if (dlm->reco.dead_node != fr->dead_node) { mlog(ML_ERROR, "node %u sent recovery finalize msg for dead " "node %u, but node %u is supposed to be dead\n", fr->node_idx, fr->dead_node, dlm->reco.dead_node); BUG(); } switch (stage) { case 1: dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx); if (dlm->reco.state & DLM_RECO_STATE_FINALIZE) { mlog(ML_ERROR, "%s: received finalize1 from " "new master %u for dead node %u, but " "this node has already received it!\n", dlm->name, fr->node_idx, fr->dead_node); dlm_print_reco_node_status(dlm); BUG(); } dlm->reco.state |= DLM_RECO_STATE_FINALIZE; spin_unlock(&dlm->spinlock); break; case 2: if (!(dlm->reco.state & DLM_RECO_STATE_FINALIZE)) { mlog(ML_ERROR, "%s: received finalize2 from " "new master %u for dead node %u, but " "this node did not have finalize1!\n", dlm->name, fr->node_idx, fr->dead_node); dlm_print_reco_node_status(dlm); BUG(); } dlm->reco.state &= ~DLM_RECO_STATE_FINALIZE; spin_unlock(&dlm->spinlock); dlm_reset_recovery(dlm); dlm_kick_recovery_thread(dlm); break; default: BUG(); } mlog(0, "%s: recovery done, reco master was %u, dead now %u, master now %u\n", dlm->name, fr->node_idx, dlm->reco.dead_node, dlm->reco.new_master); dlm_put(dlm); return 0; }