summaryrefslogtreecommitdiffstats
path: root/discover/device-handler.c
blob: ec1eee344802a24e3c9f4febdd69d1c6f6fdc6c4 (plain)
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
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <mntent.h>
#include <locale.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/mount.h>

#include <talloc/talloc.h>
#include <list/list.h>
#include <log/log.h>
#include <types/types.h>
#include <system/system.h>
#include <process/process.h>
#include <url/url.h>
#include <i18n/i18n.h>

#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

#include "device-handler.h"
#include "discover-server.h"
#include "devmapper.h"
#include "user-event.h"
#include "platform.h"
#include "event.h"
#include "parser.h"
#include "resource.h"
#include "paths.h"
#include "sysinfo.h"
#include "boot.h"
#include "udev.h"
#include "network.h"
#include "ipmi.h"

enum default_priority {
	DEFAULT_PRIORITY_REMOTE		= 1,
	DEFAULT_PRIORITY_LOCAL_FIRST	= 2,
	DEFAULT_PRIORITY_LOCAL_LAST	= 0xfe,
	DEFAULT_PRIORITY_DISABLED	= 0xff,
};

struct progress_info {
	unsigned int			percentage;
	unsigned long			size;		/* size in bytes */

	const struct process_info	*procinfo;
	struct list_item	list;
};

struct device_handler {
	struct discover_server	*server;
	int			dry_run;

	struct pb_udev		*udev;
	struct network		*network;
	struct user_event	*user_event;

	struct discover_device	**devices;
	unsigned int		n_devices;

	struct ramdisk_device	**ramdisks;
	unsigned int		n_ramdisks;

	struct waitset		*waitset;
	struct waiter		*timeout_waiter;
	bool			autoboot_enabled;
	unsigned int		sec_to_boot;

	struct discover_boot_option *default_boot_option;
	int			default_boot_option_priority;

	struct list		unresolved_boot_options;

	struct boot_task	*pending_boot;
	bool			pending_boot_is_default;

	struct list		progress;
	unsigned int		n_progress;
};

static int mount_device(struct discover_device *dev);
static int umount_device(struct discover_device *dev);

static int device_handler_init_sources(struct device_handler *handler);
static void device_handler_reinit_sources(struct device_handler *handler);

static void device_handler_update_lang(const char *lang);

void discover_context_add_boot_option(struct discover_context *ctx,
		struct discover_boot_option *boot_option)
{
	boot_option->source = ctx->parser;
	list_add_tail(&ctx->boot_options, &boot_option->list);
	talloc_steal(ctx, boot_option);
}

/**
 * device_handler_get_device_count - Get the count of current handler devices.
 */

int device_handler_get_device_count(const struct device_handler *handler)
{
	return handler->n_devices;
}

/**
 * device_handler_get_device - Get a handler device by index.
 */

const struct discover_device *device_handler_get_device(
	const struct device_handler *handler, unsigned int index)
{
	if (index >= handler->n_devices) {
		assert(0 && "bad index");
		return NULL;
	}

	return handler->devices[index];
}

struct network *device_handler_get_network(
		const struct device_handler *handler)
{
	return handler->network;
}

struct discover_boot_option *discover_boot_option_create(
		struct discover_context *ctx,
		struct discover_device *device)
{
	struct discover_boot_option *opt;

	opt = talloc_zero(ctx, struct discover_boot_option);
	opt->option = talloc_zero(opt, struct boot_option);
	opt->device = device;

	return opt;
}

static int device_match_uuid(struct discover_device *dev, const char *uuid)
{
	return dev->uuid && !strcmp(dev->uuid, uuid);
}

static int device_match_label(struct discover_device *dev, const char *label)
{
	return dev->label && !strcmp(dev->label, label);
}

static int device_match_id(struct discover_device *dev, const char *id)
{
	return !strcmp(dev->device->id, id);
}

static int device_match_serial(struct discover_device *dev, const char *serial)
{
	const char *val = discover_device_get_param(dev, "ID_SERIAL");
	return val && !strcmp(val, serial);
}

static struct discover_device *device_lookup(
		struct device_handler *device_handler,
		int (match_fn)(struct discover_device *, const char *),
		const char *str)
{
	struct discover_device *dev;
	unsigned int i;

	if (!str)
		return NULL;

	for (i = 0; i < device_handler->n_devices; i++) {
		dev = device_handler->devices[i];

		if (match_fn(dev, str))
			return dev;
	}

	return NULL;
}

struct discover_device *device_lookup_by_name(struct device_handler *handler,
		const char *name)
{
	if (!strncmp(name, "/dev/", strlen("/dev/")))
		name += strlen("/dev/");

	return device_lookup_by_id(handler, name);
}

struct discover_device *device_lookup_by_uuid(
		struct device_handler *device_handler,
		const char *uuid)
{
	return device_lookup(device_handler, device_match_uuid, uuid);
}

struct discover_device *device_lookup_by_label(
		struct device_handler *device_handler,
		const char *label)
{
	return device_lookup(device_handler, device_match_label, label);
}

struct discover_device *device_lookup_by_id(
		struct device_handler *device_handler,
		const char *id)
{
	return device_lookup(device_handler, device_match_id, id);
}

struct discover_device *device_lookup_by_serial(
		struct device_handler *device_handler,
		const char *serial)
{
	return device_lookup(device_handler, device_match_serial, serial);
}

void device_handler_destroy(struct device_handler *handler)
{
	talloc_free(handler);
}

static int destroy_device(void *arg)
{
	struct discover_device *dev = arg;

	umount_device(dev);

	return 0;
}

struct discover_device *discover_device_create(struct device_handler *handler,
		const char *uuid, const char *id)
{
	struct discover_device *dev;

	if (uuid)
		dev = device_lookup_by_uuid(handler, uuid);
	else
		dev = device_lookup_by_id(handler, id);

	if (dev)
		return dev;

	dev = talloc_zero(handler, struct discover_device);
	dev->device = talloc_zero(dev, struct device);
	dev->device->id = talloc_strdup(dev->device, id);
	dev->uuid = talloc_strdup(dev, uuid);
	list_init(&dev->params);
	list_init(&dev->boot_options);

	talloc_set_destructor(dev, destroy_device);

	return dev;
}

struct discover_device_param {
	char			*name;
	char			*value;
	struct list_item	list;
};

void discover_device_set_param(struct discover_device *device,
		const char *name, const char *value)
{
	struct discover_device_param *param;
	bool found = false;

	list_for_each_entry(&device->params, param, list) {
		if (!strcmp(param->name, name)) {
			found = true;
			break;
		}
	}

	if (!found) {
		if (!value)
			return;
		param = talloc(device, struct discover_device_param);
		param->name = talloc_strdup(param, name);
		list_add(&device->params, &param->list);
	} else {
		if (!value) {
			list_remove(&param->list);
			talloc_free(param);
			return;
		}
		talloc_free(param->value);
	}

	param->value = talloc_strdup(param, value);
}

const char *discover_device_get_param(struct discover_device *device,
		const char *name)
{
	struct discover_device_param *param;

	list_for_each_entry(&device->params, param, list) {
		if (!strcmp(param->name, name))
			return param->value;
	}
	return NULL;
}

struct device_handler *device_handler_init(struct discover_server *server,
		struct waitset *waitset, int dry_run)
{
	struct device_handler *handler;
	int rc;

	handler = talloc_zero(NULL, struct device_handler);
	handler->server = server;
	handler->waitset = waitset;
	handler->dry_run = dry_run;
	handler->autoboot_enabled = config_autoboot_active(config_get());

	list_init(&handler->unresolved_boot_options);

	list_init(&handler->progress);

	/* set up our mount point base */
	pb_mkdir_recursive(mount_base());

	parser_init();

	if (config_get()->safe_mode)
		return handler;

	rc = device_handler_init_sources(handler);
	if (rc) {
		talloc_free(handler);
		return NULL;
	}

	return handler;
}

void device_handler_reinit(struct device_handler *handler)
{
	struct discover_boot_option *opt, *tmp;
	struct ramdisk_device *ramdisk;
	unsigned int i;

	device_handler_cancel_default(handler);
	/* Cancel any pending non-default boot */
	if (handler->pending_boot) {
		boot_cancel(handler->pending_boot);
		handler->pending_boot = NULL;
		handler->pending_boot_is_default = false;
	}

	/* Cancel any remaining async jobs */
	process_stop_async_all();

	/* free unresolved boot options */
	list_for_each_entry_safe(&handler->unresolved_boot_options,
			opt, tmp, list)
		talloc_free(opt);
	list_init(&handler->unresolved_boot_options);

	/* drop all devices */
	for (i = 0; i < handler->n_devices; i++) {
		discover_server_notify_device_remove(handler->server,
				handler->devices[i]->device);
		ramdisk = handler->devices[i]->ramdisk;
		talloc_free(handler->devices[i]);
		talloc_free(ramdisk);
	}

	talloc_free(handler->devices);
	handler->devices = NULL;
	handler->n_devices = 0;
	talloc_free(handler->ramdisks);
	handler->ramdisks = NULL;
	handler->n_ramdisks = 0;

	device_handler_reinit_sources(handler);
}

void device_handler_remove(struct device_handler *handler,
		struct discover_device *device)
{
	struct discover_boot_option *opt, *tmp;
	unsigned int i;

	list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
		if (opt == handler->default_boot_option) {
			pb_log("Default option %s cancelled since device removed",
					opt->option->name);
			device_handler_cancel_default(handler);
			break;
		}
	}

	for (i = 0; i < handler->n_devices; i++)
		if (handler->devices[i] == device)
			break;

	if (i == handler->n_devices) {
		talloc_free(device);
		return;
	}

	/* Free any unresolved options, as they're currently allocated
	 * against the handler */
	list_for_each_entry_safe(&handler->unresolved_boot_options,
			opt, tmp, list) {
		if (opt->device != device)
			continue;
		list_remove(&opt->list);
		talloc_free(opt);
	}

	/* if this is a network device, we have to unregister it from the
	 * network code */
	if (device->device->type == DEVICE_TYPE_NETWORK)
		network_unregister_device(handler->network, device);

	handler->n_devices--;
	memmove(&handler->devices[i], &handler->devices[i + 1],
		(handler->n_devices - i) * sizeof(handler->devices[0]));
	handler->devices = talloc_realloc(handler, handler->devices,
		struct discover_device *, handler->n_devices);

	if (device->notified)
		discover_server_notify_device_remove(handler->server,
							device->device);

	talloc_free(device);
}

void device_handler_status(struct device_handler *handler,
		struct status *status)
{
	discover_server_notify_boot_status(handler->server, status);
}

static void _device_handler_vstatus(struct device_handler *handler,
		enum status_type type, const char *fmt, va_list ap)
{
	struct status status;

	status.type = type;
	status.message = talloc_vasprintf(handler, fmt, ap);
	status.backlog = false;

	device_handler_status(handler, &status);

	talloc_free(status.message);
}

static void _device_handler_vdevstatus(struct device_handler *handler,
		struct discover_device *device, enum status_type type,
		const char *fmt, va_list ap)
{
	char *msg;

	msg = talloc_asprintf(handler, "[%s] %s",
			device ? device->device->id : "unknown", fmt);
	_device_handler_vstatus(handler, type, msg, ap);
	talloc_free(msg);
}

void device_handler_status_dev_info(struct device_handler *handler,
		struct discover_device *dev, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	_device_handler_vdevstatus(handler, dev, STATUS_INFO, fmt, ap);
	va_end(ap);
}

void device_handler_status_dev_err(struct device_handler *handler,
		struct discover_device *dev, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	_device_handler_vdevstatus(handler, dev, STATUS_ERROR, fmt, ap);
	va_end(ap);
}

void device_handler_status_info(struct device_handler *handler,
		const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	_device_handler_vstatus(handler, STATUS_INFO, fmt, ap);
	va_end(ap);
}

void device_handler_status_err(struct device_handler *handler,
		const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	_device_handler_vstatus(handler, STATUS_ERROR, fmt, ap);
	va_end(ap);
}

void device_handler_status_download(struct device_handler *handler,
		const struct process_info *procinfo,
		unsigned int percentage, unsigned int size, char suffix)
{
	struct progress_info *p, *progress = NULL;
	uint64_t current_converted, current = 0;
	const char *units = " kMGTP";
	unsigned long size_bytes;
	char *update = NULL;
	double total = 0;
	unsigned int i;
	int unit = 0;

	list_for_each_entry(&handler->progress, p, list)
		if (p->procinfo == procinfo)
			progress = p;

	if (!progress) {
		pb_log("Registering new progress struct\n");
		progress = talloc_zero(handler, struct progress_info);
		if (!progress) {
			pb_log("Failed to allocate room for progress struct\n");
			return;
		}
		progress->procinfo = procinfo;
		list_add(&handler->progress, &progress->list);
		handler->n_progress++;
	}

	size_bytes = size;
	for (i = 0; i < strlen(units); i++) {
		if (units[i] == suffix)
			break;
	}

	if (i >= strlen(units)) {
	    pb_log("Couldn't recognise suffix '%c'\n", suffix);
	    size_bytes = 0;
	} else {
		while (i--)
			size_bytes <<= 10;
	}

	progress->percentage = percentage;
	progress->size = size_bytes;

	/*
	 * Aggregate the info we have and update status. If a progress struct
	 * has zero for both percentage and size we assume progress information
	 * is unavailable and fall back to a generic progress message.
	 */
	list_for_each_entry(&handler->progress, p, list) {
		uint64_t c;
		double t;
		if (!p->percentage || !p->size) {
			update = talloc_asprintf(handler,
					_("%u downloads in progress..."),
					handler->n_progress);
			current = total = 0;
			break;
		}

		c = p->size;
		t = (100 * c) / p->percentage;

		current += c;
		total += t;
	}

	if (total) {
		current_converted = current;
		while (current_converted >= 1000) {
			current_converted >>= 10;
			unit++;
		}
		update = talloc_asprintf(handler,
				_("%u %s downloading: %.0f%% - %lu%cB"),
				handler->n_progress,
				ngettext("item", "items", handler->n_progress),
				(current / total) * 100, current_converted,
				units[unit]);
	}

	if (!update) {
		pb_log("%s: failed to allocate new status\n", __func__);
	} else {
		device_handler_status_info(handler, "%s\n", update);
		talloc_free(update);
	}
}

void device_handler_status_download_remove(struct device_handler *handler,
		struct process_info *procinfo)
{
	struct progress_info *p, *tmp;

	list_for_each_entry_safe(&handler->progress, p, tmp, list)
		if (p->procinfo == procinfo) {
			list_remove(&p->list);
			talloc_free(p);
			handler->n_progress--;
		}
}

static void device_handler_boot_status_cb(void *arg, struct status *status)
{
	device_handler_status(arg, status);
}

static void countdown_status(struct device_handler *handler,
		struct discover_boot_option *opt, unsigned int sec)
{
	struct status status;

	status.type = STATUS_INFO;
	status.message = talloc_asprintf(handler,
			_("Booting in %d sec: %s"), sec, opt->option->name);
	status.backlog = false;

	device_handler_status(handler, &status);

	talloc_free(status.message);
}

static int default_timeout(void *arg)
{
	struct device_handler *handler = arg;
	struct discover_boot_option *opt;

	if (!handler->default_boot_option)
		return 0;

	if (handler->pending_boot)
		return 0;

	opt = handler->default_boot_option;

	if (handler->sec_to_boot) {
		countdown_status(handler, opt, handler->sec_to_boot);
		handler->sec_to_boot--;
		handler->timeout_waiter = waiter_register_timeout(
						handler->waitset, 1000,
						default_timeout, handler);
		return 0;
	}

	handler->timeout_waiter = NULL;

	pb_log("Timeout expired, booting default option %s\n", opt->option->id);

	platform_pre_boot();

	handler->pending_boot = boot(handler, handler->default_boot_option,
			NULL, handler->dry_run, device_handler_boot_status_cb,
			handler);
	handler->pending_boot_is_default = true;
	return 0;
}

struct {
	enum ipmi_bootdev	ipmi_type;
	enum device_type	device_type;
} device_type_map[] = {
	{ IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
	{ IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
	{ IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
	{ IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
};

static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
		enum device_type device_type)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
		if (device_type_map[i].device_type == device_type)
			return device_type_map[i].ipmi_type == ipmi_type;
	}

	return false;
}

static int autoboot_option_priority(const struct config *config,
				struct discover_boot_option *opt)
{
	enum device_type type = opt->device->device->type;
	const char *uuid = opt->device->uuid;
	struct autoboot_option *auto_opt;
	unsigned int i;

	for (i = 0; i < config->n_autoboot_opts; i++) {
		auto_opt = &config->autoboot_opts[i];
		if (auto_opt->boot_type == BOOT_DEVICE_UUID)
			if (!strcmp(auto_opt->uuid, uuid))
				return DEFAULT_PRIORITY_LOCAL_FIRST + i;

		if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
			if (auto_opt->type == type ||
			    auto_opt->type == DEVICE_TYPE_ANY)
				return DEFAULT_PRIORITY_LOCAL_FIRST + i;
	}

	return -1;
}

/*
 * We have different priorities to resolve conflicts between boot options that
 * report to be the default for their device. This function assigns a priority
 * for these options.
 */
static enum default_priority default_option_priority(
		struct discover_boot_option *opt)
{
	const struct config *config;

	config = config_get();

	/* We give highest priority to IPMI-configured boot options. If
	 * we have an IPMI bootdev configuration set, then we don't allow
	 * any other defaults */
	if (config->ipmi_bootdev) {
		bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
				opt->device->device->type);
		if (ipmi_match)
			return DEFAULT_PRIORITY_REMOTE;

		pb_debug("handler: disabled default priority due to "
				"non-matching IPMI type %x\n",
				config->ipmi_bootdev);
		return DEFAULT_PRIORITY_DISABLED;
	}

	/* Next, try to match the option against the user-defined autoboot
	 * options, either by device UUID or type. */
	if (config->n_autoboot_opts) {
		int boot_match = autoboot_option_priority(config, opt);
		if (boot_match > 0)
			return boot_match;
	}

	/* If the option didn't match any entry in the array, it is disabled */
	pb_debug("handler: disabled default priority due to "
			"non-matching UUID or type\n");
	return DEFAULT_PRIORITY_DISABLED;
}

static void set_default(struct device_handler *handler,
		struct discover_boot_option *opt)
{
	enum default_priority cur_prio, new_prio;

	if (!handler->autoboot_enabled)
		return;

	pb_debug("handler: new default option: %s\n", opt->option->id);

	new_prio = default_option_priority(opt);

	/* Anything outside our range prevents a default boot */
	if (new_prio >= DEFAULT_PRIORITY_DISABLED)
		return;

	pb_debug("handler: calculated priority %d\n", new_prio);

	/* Resolve any conflicts: if we have a new default option, it only
	 * replaces the current if it has a higher priority. */
	if (handler->default_boot_option) {

		cur_prio = handler->default_boot_option_priority;

		if (new_prio < cur_prio) {
			pb_log("handler: new prio %d beats "
					"old prio %d for %s\n",
					new_prio, cur_prio,
					handler->default_boot_option
						->option->id);
			handler->default_boot_option = opt;
			handler->default_boot_option_priority = new_prio;
			/* extend the timeout a little, so the user sees some
			 * indication of the change */
			handler->sec_to_boot += 2;
		}

		return;
	}

	handler->sec_to_boot = config_get()->autoboot_timeout_sec;
	handler->default_boot_option = opt;
	handler->default_boot_option_priority = new_prio;

	pb_log("handler: boot option %s set as default, timeout %u sec.\n",
	       opt->option->id, handler->sec_to_boot);

	default_timeout(handler);
}

static bool resource_is_resolved(struct resource *res)
{
	return !res || res->resolved;
}

/* We only use this in an assert, which will disappear if we're compiling
 * with NDEBUG, so we need the 'used' attribute for these builds */
static bool __attribute__((used)) boot_option_is_resolved(
		struct discover_boot_option *opt)
{
	return resource_is_resolved(opt->boot_image) &&
		resource_is_resolved(opt->initrd) &&
		resource_is_resolved(opt->dtb) &&
		resource_is_resolved(opt->args_sig_file) &&
		resource_is_resolved(opt->icon);
}

static bool resource_resolve(struct resource *res, const char *name,
		struct discover_boot_option *opt,
		struct device_handler *handler)
{
	struct parser *parser = opt->source;

	if (resource_is_resolved(res))
		return true;

	pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
			opt->option->id, name, parser->name);
	parser->resolve_resource(handler, res);

	return res->resolved;
}

static bool boot_option_resolve(struct discover_boot_option *opt,
		struct device_handler *handler)
{
	return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
		resource_resolve(opt->initrd, "initrd", opt, handler) &&
		resource_resolve(opt->dtb, "dtb", opt, handler) &&
		resource_resolve(opt->args_sig_file, "args_sig_file", opt,
			handler) &&
		resource_resolve(opt->icon, "icon", opt, handler);
}

static void boot_option_finalise(struct device_handler *handler,
		struct discover_boot_option *opt)
{
	assert(boot_option_is_resolved(opt));

	/* check that the parsers haven't set any of the final data */
	assert(!opt->option->boot_image_file);
	assert(!opt->option->initrd_file);
	assert(!opt->option->dtb_file);
	assert(!opt->option->icon_file);
	assert(!opt->option->device_id);
	assert(!opt->option->args_sig_file);

	if (opt->boot_image)
		opt->option->boot_image_file = opt->boot_image->url->full;
	if (opt->initrd)
		opt->option->initrd_file = opt->initrd->url->full;
	if (opt->dtb)
		opt->option->dtb_file = opt->dtb->url->full;
	if (opt->icon)
		opt->option->icon_file = opt->icon->url->full;
	if (opt->args_sig_file)
		opt->option->args_sig_file = opt->args_sig_file->url->full;

	opt->option->device_id = opt->device->device->id;

	if (opt->option->is_default)
		set_default(handler, opt);
}

static void notify_boot_option(struct device_handler *handler,
		struct discover_boot_option *opt)
{
	struct discover_device *dev = opt->device;

	if (!dev->notified)
		discover_server_notify_device_add(handler->server,
						  opt->device->device);
	dev->notified = true;
	discover_server_notify_boot_option_add(handler->server, opt->option);
}

static void process_boot_option_queue(struct device_handler *handler)
{
	struct discover_boot_option *opt, *tmp;

	list_for_each_entry_safe(&handler->unresolved_boot_options,
			opt, tmp, list) {

		pb_debug("queue: attempting resolution for %s\n",
				opt->option->id);

		if (!boot_option_resolve(opt, handler))
			continue;

		pb_debug("\tresolved!\n");

		list_remove(&opt->list);
		list_add_tail(&opt->device->boot_options, &opt->list);
		talloc_steal(opt->device, opt);
		boot_option_finalise(handler, opt);
		notify_boot_option(handler, opt);
	}
}

struct discover_context *device_handler_discover_context_create(
		struct device_handler *handler,
		struct discover_device *device)
{
	struct discover_context *ctx;

	ctx = talloc_zero(handler, struct discover_context);
	ctx->handler = handler;
	ctx->device = device;
	list_init(&ctx->boot_options);

	return ctx;
}

void device_handler_add_device(struct device_handler *handler,
		struct discover_device *device)
{
	handler->n_devices++;
	handler->devices = talloc_realloc(handler, handler->devices,
				struct discover_device *, handler->n_devices);
	handler->devices[handler->n_devices - 1] = device;

	if (device->device->type == DEVICE_TYPE_NETWORK)
		network_register_device(handler->network, device);
}

void device_handler_add_ramdisk(struct device_handler *handler,
		const char *path)
{
	struct ramdisk_device *dev;
	unsigned int i;

	if (!path)
		return;

	for (i = 0; i < handler->n_ramdisks; i++)
		if (!strcmp(handler->ramdisks[i]->path, path))
			return;

	dev = talloc_zero(handler, struct ramdisk_device);
	if (!dev) {
		pb_log("Failed to allocate memory to track %s\n", path);
		return;
	}

	dev->path = talloc_strdup(handler, path);

	handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
				struct ramdisk_device *,
				handler->n_ramdisks + 1);
	if (!handler->ramdisks) {
		pb_log("Failed to reallocate memory"
		       "- ramdisk tracking inconsistent!\n");
		return;
	}

	handler->ramdisks[i] = dev;
	i = handler->n_ramdisks++;
}

struct ramdisk_device *device_handler_get_ramdisk(
		struct device_handler *handler)
{
	unsigned int i;
	char *name;
	dev_t id;

	/* Check if free ramdisk exists */
	for (i = 0; i < handler->n_ramdisks; i++)
		if (!handler->ramdisks[i]->snapshot &&
		    !handler->ramdisks[i]->origin &&
		    !handler->ramdisks[i]->base)
			return handler->ramdisks[i];

	/* Otherwise create a new one */
	name = talloc_asprintf(handler, "/dev/ram%d",
			handler->n_ramdisks);
	if (!name) {
		pb_debug("Failed to allocate memory to name /dev/ram%d",
			handler->n_ramdisks);
		return NULL;
	}

	id = makedev(1, handler->n_ramdisks);
	if (mknod(name, S_IFBLK, id)) {
		if (errno == EEXIST) {
			/* We haven't yet received updates for existing
			 * ramdisks - add and use this one */
			pb_debug("Using untracked ramdisk %s\n", name);
		} else {
			pb_log("Failed to create new ramdisk %s: %s\n",
			       name, strerror(errno));
			return NULL;
		}
	}
	device_handler_add_ramdisk(handler, name);
	talloc_free(name);

	return handler->ramdisks[i];
}

void device_handler_release_ramdisk(struct discover_device *device)
{
	struct ramdisk_device *ramdisk = device->ramdisk;

	talloc_free(ramdisk->snapshot);
	talloc_free(ramdisk->origin);
	talloc_free(ramdisk->base);

	ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
	ramdisk->sectors = 0;

	device->ramdisk = NULL;
}

/* Start discovery on a hotplugged device. The device will be in our devices
 * array, but has only just been initialised by the hotplug source.
 */
int device_handler_discover(struct device_handler *handler,
		struct discover_device *dev)
{
	struct discover_context *ctx;
	int rc;

	device_handler_status_dev_info(handler, dev,
		/*
		 * TRANSLATORS: this string will be passed the type of the
		 * device (eg "disk" or "network"), which will be translated
		 * accordingly.
		 */
		_("Processing new %s device"),
		device_type_display_name(dev->device->type));

	/* create our context */
	ctx = device_handler_discover_context_create(handler, dev);

	rc = mount_device(dev);
	if (rc)
		goto out;

	/* add this device to our system info */
	system_info_register_blockdev(dev->device->id, dev->uuid,
			dev->mount_path);

	/* run the parsers. This will populate the ctx's boot_option list. */
	iterate_parsers(ctx);

	/* add discovered stuff to the handler */
	device_handler_discover_context_commit(handler, ctx);

	process_boot_option_queue(handler);
out:
	talloc_unlink(handler, ctx);

	return 0;
}

/* Incoming dhcp event */
int device_handler_dhcp(struct device_handler *handler,
		struct discover_device *dev, struct event *event)
{
	struct discover_context *ctx;

	device_handler_status_dev_info(handler, dev,
			_("Processing DHCP lease response (ip: %s)"),
			event_get_param(event, "ip"));

	/* create our context */
	ctx = device_handler_discover_context_create(handler, dev);
	talloc_steal(ctx, event);
	ctx->event = event;

	iterate_parsers(ctx);

	device_handler_discover_context_commit(handler, ctx);

	talloc_unlink(handler, ctx);

	return 0;
}

static struct discover_boot_option *find_boot_option_by_id(
		struct device_handler *handler, const char *id)
{
	unsigned int i;

	for (i = 0; i < handler->n_devices; i++) {
		struct discover_device *dev = handler->devices[i];
		struct discover_boot_option *opt;

		list_for_each_entry(&dev->boot_options, opt, list)
			if (!strcmp(opt->option->id, id))
				return opt;
	}

	return NULL;
}

void device_handler_boot(struct device_handler *handler,
		struct boot_command *cmd)
{
	struct discover_boot_option *opt = NULL;

	if (cmd->option_id && strlen(cmd->option_id))
		opt = find_boot_option_by_id(handler, cmd->option_id);

	if (handler->pending_boot)
		boot_cancel(handler->pending_boot);

	platform_pre_boot();

	handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
			device_handler_boot_status_cb, handler);
	handler->pending_boot_is_default = false;
}

void device_handler_cancel_default(struct device_handler *handler)
{
	if (handler->timeout_waiter)
		waiter_remove(handler->timeout_waiter);

	handler->timeout_waiter = NULL;
	handler->autoboot_enabled = false;

	/* we only send status if we had a default boot option queued */
	if (!handler->default_boot_option)
		return;

	pb_log("Cancelling default boot option\n");

	if (handler->pending_boot && handler->pending_boot_is_default) {
		boot_cancel(handler->pending_boot);
		handler->pending_boot = NULL;
		handler->pending_boot_is_default = false;
	}

	handler->default_boot_option = NULL;

	device_handler_status_info(handler, _("Default boot cancelled"));
}

void device_handler_update_config(struct device_handler *handler,
		struct config *config)
{
	int rc;

	rc = config_set(config);
	if (rc)
		return;

	discover_server_notify_config(handler->server, config);
	device_handler_update_lang(config->lang);
	device_handler_reinit(handler);
}

static char *device_from_addr(void *ctx, struct pb_url *url)
{
	char *ipaddr, *buf, *tok, *dev = NULL;
	const char *delim = " ";
	struct sockaddr_in *ip;
	struct sockaddr_in si;
	struct addrinfo *res;
	struct process *p;
	int rc;

	/* Note: IPv4 only */
	rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
	if (rc > 0) {
		ipaddr = url->host;
	} else {
		/* need to turn hostname into a valid IP */
		rc = getaddrinfo(url->host, NULL, NULL, &res);
		if (rc) {
			pb_debug("%s: Invalid URL\n",__func__);
			return NULL;
		}
		ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
		ip = (struct sockaddr_in *) res->ai_addr;
		inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
		freeaddrinfo(res);
	}

	const char *argv[] = {
		pb_system_apps.ip,
		"route", "show", "to", "match",
		ipaddr,
		NULL
	};

	p = process_create(ctx);

	p->path = pb_system_apps.ip;
	p->argv = argv;
	p->keep_stdout = true;

	rc = process_run_sync(p);

	if (rc || p->exit_status) {
		/* ip has complained for some reason; most likely
		 * there is no route to the host - bail out */
		pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
		pb_debug("ip buf: %s\n", p->stdout_buf);
		process_release(p);
		return NULL;
	}

	buf = p->stdout_buf;
	/* If a route is found, ip-route output will be of the form
	 * "... dev DEVNAME ... " */
	tok = strtok(buf, delim);
	while (tok) {
		if (!strcmp(tok, "dev")) {
			tok = strtok(NULL, delim);
			dev = talloc_strdup(ctx, tok);
			break;
		}
		tok = strtok(NULL, delim);
	}

	process_release(p);
	if (dev)
		pb_debug("%s: Found interface '%s'\n", __func__,dev);
	return dev;
}

void device_handler_process_url(struct device_handler *handler,
		const char *url, const char *mac, const char *ip)
{
	struct discover_context *ctx;
	struct discover_device *dev;
	struct pb_url *pb_url;
	struct event *event;
	struct param *param;

	if (!handler->network) {
		device_handler_status_err(handler, _("No network configured"));
		return;
	}

	event = talloc(handler, struct event);
	event->type = EVENT_TYPE_USER;
	event->action = EVENT_ACTION_URL;

	if (url[strlen(url) - 1] == '/') {
		event->params = talloc_array(event, struct param, 3);
		param = &event->params[0];
		param->name = talloc_strdup(event, "pxepathprefix");
		param->value = talloc_strdup(event, url);
		param = &event->params[1];
		param->name = talloc_strdup(event, "mac");
		param->value = talloc_strdup(event, mac);
		param = &event->params[2];
		param->name = talloc_strdup(event, "ip");
		param->value = talloc_strdup(event, ip);
		event->n_params = 3;
	} else {
		event->params = talloc_array(event, struct param, 1);
		param = &event->params[0];
		param->name = talloc_strdup(event, "pxeconffile");
		param->value = talloc_strdup(event, url);
		event->n_params = 1;
	}

	pb_url = pb_url_parse(event, event->params->value);
	if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
		device_handler_status_err(handler, _("Invalid config URL!"));
		return;
	}

	if (pb_url->scheme == pb_url_file)
		event->device = talloc_asprintf(event, "local");
	else
		event->device = device_from_addr(event, pb_url);

	if (!event->device) {
		device_handler_status_err(handler,
					_("Unable to route to host %s"),
					pb_url->host);
		return;
	}

	dev = discover_device_create(handler, mac, event->device);
	if (pb_url->scheme == pb_url_file)
		dev->device->type = DEVICE_TYPE_ANY;
	ctx = device_handler_discover_context_create(handler, dev);
	talloc_steal(ctx, event);
	ctx->event = event;

	iterate_parsers(ctx);

	device_handler_discover_context_commit(handler, ctx);

	talloc_unlink(handler, ctx);
}

#ifndef PETITBOOT_TEST

/**
 * context_commit - Commit a temporary discovery context to the handler,
 * and notify the clients about any new options / devices
 */
void device_handler_discover_context_commit(struct device_handler *handler,
		struct discover_context *ctx)
{
	struct discover_device *dev = ctx->device;
	struct discover_boot_option *opt, *tmp;

	if (!device_lookup_by_uuid(handler, dev->uuid))
		device_handler_add_device(handler, dev);

	/* move boot options from the context to the device */
	list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
		list_remove(&opt->list);

		/* All boot options need at least a kernel image */
		if (!opt->boot_image || !opt->boot_image->url) {
			pb_log("boot option %s is missing boot image, ignoring\n",
				opt->option->id);
			talloc_free(opt);
			continue;
		}

		if (boot_option_resolve(opt, handler)) {
			pb_log("boot option %s is resolved, "
					"sending to clients\n",
					opt->option->id);
			list_add_tail(&dev->boot_options, &opt->list);
			talloc_steal(dev, opt);
			boot_option_finalise(handler, opt);
			notify_boot_option(handler, opt);
		} else {
			if (!opt->source->resolve_resource) {
				pb_log("parser %s gave us an unresolved "
					"resource (%s), but no way to "
					"resolve it\n",
					opt->source->name, opt->option->id);
				talloc_free(opt);
			} else {
				pb_log("boot option %s is unresolved, "
						"adding to queue\n",
						opt->option->id);
				list_add(&handler->unresolved_boot_options,
						&opt->list);
				talloc_steal(handler, opt);
			}
		}
	}
}

static void device_handler_update_lang(const char *lang)
{
	const char *cur_lang;

	if (!lang)
		return;

	cur_lang = setlocale(LC_ALL, NULL);
	if (cur_lang && !strcmp(cur_lang, lang))
		return;

	setlocale(LC_ALL, lang);
}

static int device_handler_init_sources(struct device_handler *handler)
{
	/* init our device sources: udev, network and user events */
	handler->udev = udev_init(handler, handler->waitset);
	if (!handler->udev)
		return -1;

	handler->network = network_init(handler, handler->waitset,
			handler->dry_run);
	if (!handler->network)
		return -1;

	handler->user_event = user_event_init(handler, handler->waitset);
	if (!handler->user_event)
		return -1;

	return 0;
}

static void device_handler_reinit_sources(struct device_handler *handler)
{
	/* if we haven't initialised sources previously (becuase we started in
	 * safe mode), then init once here. */
	if (!(handler->udev || handler->network || handler->user_event)) {
		device_handler_init_sources(handler);
		return;
	}

	udev_reinit(handler->udev);

	network_shutdown(handler->network);
	handler->network = network_init(handler, handler->waitset,
			handler->dry_run);
}

static inline const char *get_device_path(struct discover_device *dev)
{
	return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
}

static char *check_subvols(struct discover_device *dev)
{
	const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
	struct stat sb;
	char *path;
	int rc;

	if (strncmp(fstype, "btrfs", strlen("btrfs")))
		return dev->mount_path;

	/* On btrfs a device's root may be under a subvolume path */
	path = join_paths(dev, dev->mount_path, "@");
	rc = stat(path, &sb);
	if (!rc && S_ISDIR(sb.st_mode)) {
		pb_debug("Using '%s' for btrfs root path\n", path);
		return path;
	}

	talloc_free(path);
	return dev->mount_path;
}

static bool check_existing_mount(struct discover_device *dev)
{
	struct stat devstat, mntstat;
	const char *device_path;
	struct mntent *mnt;
	FILE *fp;
	int rc;

	device_path = get_device_path(dev);

	rc = stat(device_path, &devstat);
	if (rc) {
		pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
		return false;
	}

	if (!S_ISBLK(devstat.st_mode)) {
		pb_debug("%s: %s isn't a block device?\n", __func__,
				dev->device_path);
		return false;
	}

	fp = fopen("/proc/self/mounts", "r");

	for (;;) {
		mnt = getmntent(fp);
		if (!mnt)
			break;

		if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
			continue;

		rc = stat(mnt->mnt_fsname, &mntstat);
		if (rc)
			continue;

		if (!S_ISBLK(mntstat.st_mode))
			continue;

		if (mntstat.st_rdev == devstat.st_rdev) {
			dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
			dev->root_path = check_subvols(dev);
			dev->mounted_rw = !!hasmntopt(mnt, "rw");
			dev->mounted = true;
			dev->unmount = false;

			pb_debug("%s: %s is already mounted (r%c) at %s\n",
					__func__, dev->device_path,
					dev->mounted_rw ? 'w' : 'o',
					mnt->mnt_dir);
			break;
		}
	}

	fclose(fp);

	return mnt != NULL;
}

/*
 * Attempt to mount a filesystem safely, while handling certain filesytem-
 * specific options
 */
static int try_mount(const char *device_path, const char *mount_path,
			     const char *fstype, unsigned long flags,
			     bool have_snapshot)
{
	const char *fs, *safe_opts;
	int rc;

	/* Mount ext3 as ext4 instead so 'norecovery' can be used */
	if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
		pb_debug("Mounting ext3 filesystem as ext4\n");
		fs = "ext4";
	} else
		fs = fstype;

	if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
	    strncmp(fs, "ext4", strlen("ext4")) == 0)
		safe_opts = "norecovery";
	else
		safe_opts = NULL;

	errno = 0;
	/* If no snapshot is available don't attempt recovery */
	if (!have_snapshot)
		return mount(device_path, mount_path, fs, flags, safe_opts);

	rc = mount(device_path, mount_path, fs, flags, NULL);

	if (!rc)
		return rc;

	/* Mounting failed; some filesystems will fail to mount if a recovery
	 * journal exists (eg. cross-endian XFS), so try again with norecovery
	 * where that option is available.
	 * If mounting read-write just return the error as norecovery is not a
	 * valid option */
	if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
		return rc;

	errno = 0;
	return mount(device_path, mount_path, fs, flags, safe_opts);
}

static int mount_device(struct discover_device *dev)
{
	const char *fstype, *device_path;
	int rc;

	if (!dev->device_path)
		return -1;

	if (dev->mounted)
		return 0;

	if (check_existing_mount(dev))
		return 0;

	fstype = discover_device_get_param(dev, "ID_FS_TYPE");
	if (!fstype)
		return 0;

	dev->mount_path = join_paths(dev, mount_base(),
					dev->device_path);

	if (pb_mkdir_recursive(dev->mount_path)) {
		pb_log("couldn't create mount directory %s: %s\n",
				dev->mount_path, strerror(errno));
		goto err_free;
	}

	device_path = get_device_path(dev);

	pb_log("mounting device %s read-only\n", dev->device_path);
	rc = try_mount(device_path, dev->mount_path, fstype,
		       MS_RDONLY | MS_SILENT, dev->ramdisk);

	if (!rc) {
		dev->mounted = true;
		dev->mounted_rw = false;
		dev->unmount = true;
		dev->root_path = check_subvols(dev);
		return 0;
	}

	pb_log("couldn't mount device %s: mount failed: %s\n",
			device_path, strerror(errno));

	/* If mount fails clean up any snapshot */
	devmapper_destroy_snapshot(dev);

	pb_rmdir_recursive(mount_base(), dev->mount_path);
err_free:
	talloc_free(dev->mount_path);
	dev->mount_path = NULL;
	return -1;
}

static int umount_device(struct discover_device *dev)
{
	const char *device_path;
	int rc;

	if (!dev->mounted || !dev->unmount)
		return 0;

	device_path = get_device_path(dev);

	pb_log("unmounting device %s\n", device_path);
	rc = umount(dev->mount_path);
	if (rc)
		return -1;

	dev->mounted = false;
	devmapper_destroy_snapshot(dev);

	pb_rmdir_recursive(mount_base(), dev->mount_path);

	talloc_free(dev->mount_path);
	dev->mount_path = NULL;
	dev->root_path = NULL;

	return 0;
}

int device_request_write(struct discover_device *dev, bool *release)
{
	const char *fstype, *device_path;
	const struct config *config;
	int rc;

	*release = false;

	config = config_get();
	if (!config->allow_writes)
		return -1;

	if (!dev->mounted)
		return -1;

	if (dev->mounted_rw)
		return 0;

	fstype = discover_device_get_param(dev, "ID_FS_TYPE");

	device_path = get_device_path(dev);

	pb_log("remounting device %s read-write\n", device_path);

	rc = umount(dev->mount_path);
	if (rc) {
		pb_log("Failed to unmount %s: %s\n",
		       dev->mount_path, strerror(errno));
		return -1;
	}

	rc = try_mount(device_path, dev->mount_path, fstype,
		       MS_SILENT, dev->ramdisk);
	if (rc)
		goto mount_ro;

	dev->mounted_rw = true;
	*release = true;
	return 0;

mount_ro:
	pb_log("Unable to remount device %s read-write: %s\n",
	       device_path, strerror(errno));
	rc = try_mount(device_path, dev->mount_path, fstype,
		       MS_RDONLY | MS_SILENT, dev->ramdisk);
	if (rc)
		pb_log("Unable to recover mount for %s: %s\n",
		       device_path, strerror(errno));
	return -1;
}

void device_release_write(struct discover_device *dev, bool release)
{
	const char *fstype, *device_path;

	if (!release)
		return;

	device_path = get_device_path(dev);

	fstype = discover_device_get_param(dev, "ID_FS_TYPE");

	pb_log("remounting device %s read-only\n", device_path);

	if (umount(dev->mount_path)) {
		pb_log("Failed to unmount %s\n", dev->mount_path);
		return;
	}
	dev->mounted_rw = dev->mounted = false;

	if (dev->ramdisk) {
		devmapper_merge_snapshot(dev);
		/* device_path becomes stale after merge */
		device_path = get_device_path(dev);
	}

	if (try_mount(device_path, dev->mount_path, fstype,
		       MS_RDONLY | MS_SILENT, dev->ramdisk))
		pb_log("Failed to remount %s read-only: %s\n",
		       device_path, strerror(errno));
	else
		dev->mounted = true;
}

void device_sync_snapshots(struct device_handler *handler, const char *device)
{
	struct discover_device *dev = NULL;
	unsigned int i;

	if (device) {
		/* Find matching device and sync */
		dev = device_lookup_by_name(handler, device);
		if (!dev) {
			pb_log("%s: device name '%s' unrecognised\n",
				__func__, device);
			return;
		}
		if (dev->ramdisk)
			device_release_write(dev, true);
		else
			pb_log("%s has no snapshot to merge, skipping\n",
				dev->device->id);
		return;
	}

	/* Otherwise sync all relevant devices */
	for (i = 0; i < handler->n_devices; i++) {
		dev = handler->devices[i];
		if (dev->device->type != DEVICE_TYPE_DISK &&
			dev->device->type != DEVICE_TYPE_USB)
			continue;
		if (dev->ramdisk)
			device_release_write(dev, true);
		else
			pb_log("%s has no snapshot to merge, skipping\n",
				dev->device->id);
	}
}

#else

void device_handler_discover_context_commit(
		struct device_handler *handler __attribute__((unused)),
		struct discover_context *ctx __attribute__((unused)))
{
	pb_log("%s stubbed out for test cases\n", __func__);
}

static void device_handler_update_lang(const char *lang __attribute__((unused)))
{
}

static int device_handler_init_sources(
		struct device_handler *handler __attribute__((unused)))
{
	return 0;
}

static void device_handler_reinit_sources(
		struct device_handler *handler __attribute__((unused)))
{
}

static int umount_device(struct discover_device *dev __attribute__((unused)))
{
	return 0;
}

static int __attribute__((unused)) mount_device(
		struct discover_device *dev __attribute__((unused)))
{
	return 0;
}

int device_request_write(struct discover_device *dev __attribute__((unused)),
		bool *release)
{
	*release = true;
	return 0;
}

void device_release_write(struct discover_device *dev __attribute__((unused)),
	bool release __attribute__((unused)))
{
}

void device_sync_snapshots(
		struct device_handler *handler __attribute__((unused)),
		const char *device __attribute__((unused)))
{
}

#endif
OpenPOWER on IntegriCloud