summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ndmpd/ndmp/ndmpd_tar.c
blob: 89dc3430517a51a975534f9bd51ce912b5fa58ca (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
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
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * BSD 3 Clause License
 *
 * Copyright (c) 2007, The Storage Networking Industry Association.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *	- Redistributions of source code must retain the above copyright
 *	  notice, this list of conditions and the following disclaimer.
 *
 *	- Redistributions in binary form must reproduce the above copyright
 *	  notice, this list of conditions and the following disclaimer in
 *	  the documentation and/or other materials provided with the
 *	  distribution.
 *
 *	- Neither the name of The Storage Networking Industry Association (SNIA)
 *	  nor the names of its contributors may be used to endorse or promote
 *	  products derived from this software without specific prior written
 *	  permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
/* Copyright (c) 2007, The Storage Networking Industry Association. */
/* Copyright (c) 1996, 1997 PDC, Network Appliance. All Rights Reserved */
/* Copyright 2014 Nexenta Systems, Inc. All rights reserved. */

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <cstack.h>
#include <dirent.h>
#include <traverse.h>
#include "bitmap.h"
#include "ndmpd.h"
#include "tlm_buffers.h"


typedef struct ndmp_run_args {
	char *nr_chkp_nm;
	char *nr_unchkp_nm;
	char **nr_excls;
} ndmp_run_args_t;


/*
 * backup_create_structs
 *
 * Allocate the structures before performing backup
 *
 * Parameters:
 *   sesison (input) - session handle
 *   jname (input) - backup job name
 *
 * Returns:
 *   0: on success
 *  -1: otherwise
 */
static int
backup_create_structs(ndmpd_session_t *session, char *jname)
{
	int n;
	long xfer_size;
	ndmp_lbr_params_t *nlp;
	tlm_commands_t *cmds;

	if ((nlp = ndmp_get_nlp(session)) == NULL) {
		NDMP_LOG(LOG_DEBUG, "nlp == NULL");
		return (-1);
	}

	if ((nlp->nlp_jstat = tlm_new_job_stats(jname)) == NULL) {
		NDMP_LOG(LOG_DEBUG, "Creating job stats");
		return (-1);
	}

	cmds = &nlp->nlp_cmds;
	(void) memset(cmds, 0, sizeof (*cmds));

	xfer_size = ndmp_buffer_get_size(session);
	if (xfer_size < 512*KILOBYTE) {
		/*
		 * Read multiple of mover_record_size near to 512K.  This
		 * will prevent the data being copied in the mover buffer
		 * when we write the data.
		 */
		if ((n = (512 * KILOBYTE/xfer_size)) <= 0)
			n = 1;
		xfer_size *= n;
		NDMP_LOG(LOG_DEBUG, "Adjusted read size: %d", xfer_size);
	}

	cmds->tcs_command = tlm_create_reader_writer_ipc(TRUE, xfer_size);
	if (cmds->tcs_command == NULL) {
		NDMP_LOG(LOG_DEBUG, "Error creating ipc buffers");
		tlm_un_ref_job_stats(jname);
		return (-1);
	}

	nlp->nlp_logcallbacks = lbrlog_callbacks_init(session,
	    ndmpd_file_history_path,
	    ndmpd_file_history_dir,
	    ndmpd_file_history_node);
	if (nlp->nlp_logcallbacks == NULL) {
		tlm_release_reader_writer_ipc(cmds->tcs_command);
		tlm_un_ref_job_stats(jname);
		return (-1);
	}
	nlp->nlp_jstat->js_callbacks = (void *)(nlp->nlp_logcallbacks);

	return (0);
}


/*
 * restore_create_structs
 *
 * Allocate structures for performing a restore
 *
 * Parameters:
 *   sesison (input) - session handle
 *   jname (input) - backup job name
 *
 * Returns:
 *   0: on success
 *  -1: otherwise
 */
static int
restore_create_structs(ndmpd_session_t *session, char *jname)
{
	int i;
	long xfer_size;
	ndmp_lbr_params_t *nlp;
	tlm_commands_t *cmds;

	if ((nlp = ndmp_get_nlp(session)) == NULL) {
		NDMP_LOG(LOG_DEBUG, "nlp == NULL");
		return (-1);
	}
	if ((nlp->nlp_jstat = tlm_new_job_stats(jname)) == NULL) {
		NDMP_LOG(LOG_DEBUG, "Creating job stats");
		return (-1);
	}

	cmds = &nlp->nlp_cmds;
	(void) memset(cmds, 0, sizeof (*cmds));

	xfer_size = ndmp_buffer_get_size(session);
	cmds->tcs_command = tlm_create_reader_writer_ipc(FALSE, xfer_size);
	if (cmds->tcs_command == NULL) {
		NDMP_LOG(LOG_DEBUG, "Error creating ipc buffers");
		tlm_un_ref_job_stats(jname);
		return (-1);
	}

	nlp->nlp_logcallbacks = lbrlog_callbacks_init(session,
	    ndmpd_path_restored, NULL, NULL);
	if (nlp->nlp_logcallbacks == NULL) {
		tlm_release_reader_writer_ipc(cmds->tcs_command);
		tlm_un_ref_job_stats(jname);
		return (-1);
	}
	nlp->nlp_jstat->js_callbacks = (void *)(nlp->nlp_logcallbacks);

	nlp->nlp_restored = ndmp_malloc(sizeof (boolean_t) * nlp->nlp_nfiles);
	if (nlp->nlp_restored == NULL) {
		lbrlog_callbacks_done(nlp->nlp_logcallbacks);
		tlm_release_reader_writer_ipc(cmds->tcs_command);
		tlm_un_ref_job_stats(jname);
		return (-1);
	}
	for (i = 0; i < (int)nlp->nlp_nfiles; i++)
		nlp->nlp_restored[i] = FALSE;

	return (0);
}


/*
 * send_unrecovered_list
 *
 * Creates a list of restored files
 *
 * Parameters:
 *   params (input) - NDMP parameters
 *   nlp (input) - NDMP/LBR parameters
 *
 * Returns:
 *   0: on success
 *  -1: otherwise
 */
static int
send_unrecovered_list(ndmpd_module_params_t *params, ndmp_lbr_params_t *nlp)
{
	int i, rv;
	ndmp_name *ent;

	if (params == NULL) {
		NDMP_LOG(LOG_DEBUG, "params == NULL");
		return (-1);
	}
	if (nlp == NULL) {
		NDMP_LOG(LOG_DEBUG, "nlp == NULL");
		return (-1);
	}

	rv = 0;
	for (i = 0; i < (int)nlp->nlp_nfiles; i++) {
		NDMP_LOG(LOG_DEBUG, "nlp->nlp_restored[%d]: %s", i,
		    nlp->nlp_restored[i] ? "TRUE" : "FALSE");

		if (!nlp->nlp_restored[i]) {
			ent = (ndmp_name *)MOD_GETNAME(params, i);
			if (ent == NULL) {
				NDMP_LOG(LOG_DEBUG, "ent == NULL");
				rv = -1;
				break;
			}
			if (ent->name == NULL) {
				NDMP_LOG(LOG_DEBUG, "ent->name == NULL");
				rv = -1;
				break;
			}

			NDMP_LOG(LOG_DEBUG, "ent.name: \"%s\"", ent->name);

			rv = MOD_FILERECOVERD(params, ent->name, ENOENT);
			if (rv < 0)
				break;
		}
	}

	return (rv);
}


/*
 * backup_release_structs
 *
 * Deallocated the NDMP/LBR specific parameters
 *
 * Parameters:
 *   session (input) - session handle
 *
 * Returns:
 *   void
 */
/*ARGSUSED*/
static void
backup_release_structs(ndmpd_session_t *session)
{
	ndmp_lbr_params_t *nlp;
	tlm_commands_t *cmds;

	if ((nlp = ndmp_get_nlp(session)) == NULL) {
		NDMP_LOG(LOG_DEBUG, "nlp == NULL");
		return;
	}
	cmds = &nlp->nlp_cmds;
	if (cmds == NULL) {
		NDMP_LOG(LOG_DEBUG, "cmds == NULL");
		return;
	}

	if (nlp->nlp_logcallbacks != NULL) {
		lbrlog_callbacks_done(nlp->nlp_logcallbacks);
		nlp->nlp_logcallbacks = NULL;
	} else {
		NDMP_LOG(LOG_DEBUG, "FH CALLBACKS == NULL");
	}

	if (cmds->tcs_command != NULL) {
		if (cmds->tcs_command->tc_buffers != NULL)
			tlm_release_reader_writer_ipc(cmds->tcs_command);
		else
			NDMP_LOG(LOG_DEBUG, "BUFFERS == NULL");
		cmds->tcs_command = NULL;
	} else {
		NDMP_LOG(LOG_DEBUG, "COMMAND == NULL");
	}

	if (nlp->nlp_bkmap >= 0) {
		(void) dbm_free(nlp->nlp_bkmap);
		nlp->nlp_bkmap = -1;
	}

	if (session->ns_data.dd_operation == NDMP_DATA_OP_RECOVER &&
	    nlp->nlp_restored != NULL) {
		free(nlp->nlp_restored);
		nlp->nlp_restored = NULL;
	} else {
		NDMP_LOG(LOG_DEBUG, "nlp_restored == NULL");
	}
}

/*
 * ndmp_write_utf8magic
 *
 * Write a magic pattern to the tar header. This is used
 * as a crest to indicate that tape belongs to us.
 */
int
ndmp_write_utf8magic(tlm_cmd_t *cmd)
{
	char *cp;
	long actual_size;

	if (cmd->tc_buffers == NULL) {
		NDMP_LOG(LOG_DEBUG, "cmd->tc_buffers == NULL");
		return (-1);
	}

	cp = tlm_get_write_buffer(RECORDSIZE, &actual_size,
	    cmd->tc_buffers, TRUE);
	if (actual_size < RECORDSIZE) {
		NDMP_LOG(LOG_DEBUG, "Couldn't get enough buffer");
		return (-1);
	}

	(void) strlcpy(cp, NDMPUTF8MAGIC, RECORDSIZE);
	return (0);
}


/*
 * timecmp
 *
 * This callback function is used during backup.  It checks
 * if the object specified by the 'attr' should be backed
 * up or not.
 *
 * Directories are backed up anyways for dump format.
 * If this function is called, then the directory is
 * marked in the bitmap vector, it shows that either the
 * directory itself is modified or there is something below
 * it that will be backed up.
 *
 * Directories for tar format are backed up if and only if
 * they are modified.
 *
 * By setting ndmp_force_bk_dirs global variable to a non-zero
 * value, directories are backed up anyways.
 *
 * Backing up the directories unconditionally, helps
 * restoring the metadata of directories as well, when one
 * of the objects below them are being restored.
 *
 * For non-directory objects, if the modification or change
 * time of the object is after the date specified by the
 * bk_selector_t, the the object must be backed up.
 *
 */
static boolean_t
timecmp(bk_selector_t *bksp, struct stat64 *attr)
{
	ndmp_lbr_params_t *nlp;

	nlp = (ndmp_lbr_params_t *)bksp->bs_cookie;
	if (S_ISDIR(attr->st_mode) && ndmp_force_bk_dirs) {
		NDMP_LOG(LOG_DEBUG, "d(%lu)",
		    (uint_t)attr->st_ino);
		return (TRUE);
	}
	if (S_ISDIR(attr->st_mode) &&
	    dbm_getone(nlp->nlp_bkmap, (u_longlong_t)attr->st_ino) &&
	    ((NLP_ISDUMP(nlp) && ndmp_dump_path_node) ||
	    (NLP_ISTAR(nlp) && ndmp_tar_path_node))) {
		/*
		 * If the object is a directory and it leads to a modified
		 * object (that should be backed up) and for that type of
		 * backup the path nodes should be backed up, then return
		 * TRUE.
		 *
		 * This is required by some DMAs like Backup Express, which
		 * needs to receive ADD_NODE (for dump) or ADD_PATH (for tar)
		 * for the intermediate directories of a modified object.
		 * Other DMAs, like net_backup and net_worker, do not have such
		 * requirement.  This requirement makes sense for dump format
		 * but for 'tar' format, it does not.  In provision to the
		 * NDMP-v4 spec, for 'tar' format the intermediate directories
		 * need not to be reported.
		 */
		NDMP_LOG(LOG_DEBUG, "p(%lu)", (u_longlong_t)attr->st_ino);
		return (TRUE);
	}
	if (attr->st_mtime > bksp->bs_ldate) {
		NDMP_LOG(LOG_DEBUG, "m(%lu): %lu > %lu",
		    (uint_t)attr->st_ino, (uint_t)attr->st_mtime,
		    (uint_t)bksp->bs_ldate);
		return (TRUE);
	}
	if (attr->st_ctime > bksp->bs_ldate) {
		if (NLP_IGNCTIME(nlp)) {
			NDMP_LOG(LOG_DEBUG, "ign c(%lu): %lu > %lu",
			    (uint_t)attr->st_ino, (uint_t)attr->st_ctime,
			    (uint_t)bksp->bs_ldate);
			return (FALSE);
		}
		NDMP_LOG(LOG_DEBUG, "c(%lu): %lu > %lu",
		    (uint_t)attr->st_ino, (uint_t)attr->st_ctime,
		    (uint_t)bksp->bs_ldate);
		return (TRUE);
	}
	NDMP_LOG(LOG_DEBUG, "mc(%lu): (%lu,%lu) < %lu",
	    (uint_t)attr->st_ino, (uint_t)attr->st_mtime,
	    (uint_t)attr->st_ctime, (uint_t)bksp->bs_ldate);
	return (FALSE);
}


/*
 * get_acl_info
 *
 * load up all the access and attribute info
 */
static int
get_acl_info(char *name, tlm_acls_t *tlm_acls)
{
	int erc;
	acl_t *aclp = NULL;
	char *acltp;

	erc = lstat64(name, &tlm_acls->acl_attr);
	if (erc != 0) {
		NDMP_LOG(LOG_ERR, "Could not find file %s.", name);
		erc = TLM_NO_SOURCE_FILE;
		return (erc);
	}
	erc = acl_get(name, ACL_NO_TRIVIAL, &aclp);
	if (erc != 0) {
		NDMP_LOG(LOG_DEBUG,
		    "Could not read ACL for file [%s]", name);
		erc = TLM_NO_SOURCE_FILE;
		return (erc);
	}
	if (aclp && (acltp = acl_totext(aclp,
	    ACL_APPEND_ID | ACL_SID_FMT | ACL_COMPACT_FMT)) != NULL) {
		(void) strlcpy(tlm_acls->acl_info.attr_info, acltp,
		    TLM_MAX_ACL_TXT);
		acl_free(aclp);
		free(acltp);
	}
	return (erc);
}
/*
 * get_dir_acl_info
 *
 * load up all ACL and attr info about a directory
 */
static int
get_dir_acl_info(char *dir, tlm_acls_t *tlm_acls, tlm_job_stats_t *js)
{
	int	erc;
	char	*checkpointed_dir;
	char	root_dir[TLM_VOLNAME_MAX_LENGTH];
	char	*spot;
	char	*fil;
	acl_t	*aclp = NULL;
	char	*acltp;

	checkpointed_dir = ndmp_malloc(TLM_MAX_PATH_NAME);
	if (checkpointed_dir == NULL)
		return (-1);

	if (tlm_acls->acl_checkpointed)
		fil = tlm_build_snapshot_name(dir, checkpointed_dir,
		    js->js_job_name);
	else
		fil = dir;
	erc = lstat64(fil, &tlm_acls->acl_attr);
	if (erc != 0) {
		NDMP_LOG(LOG_ERR, "Could not find directory %s.", dir);
		free(checkpointed_dir);
		return (-1);
	}

	spot = strchr(&fil[1], '/');
	if (spot == NULL) {
		(void) strlcpy(root_dir, fil, TLM_VOLNAME_MAX_LENGTH);
	} else {
		*spot = 0;
		(void) strlcpy(root_dir, fil, TLM_VOLNAME_MAX_LENGTH);
		*spot = '/';
	}
	if (strcmp(root_dir, tlm_acls->acl_root_dir) != 0) {
		struct stat64 attr;

		erc = lstat64(root_dir, &attr);
		if (erc != 0) {
			NDMP_LOG(LOG_ERR, "Cannot find root directory %s.",
			    root_dir);
			free(checkpointed_dir);
			return (-1);
		}
		(void) strlcpy(tlm_acls->acl_root_dir, root_dir,
		    TLM_VOLNAME_MAX_LENGTH);
	}
	erc = acl_get(fil, ACL_NO_TRIVIAL, &aclp);
	if (erc != 0) {
		NDMP_LOG(LOG_DEBUG,
		    "Could not read metadata for directory [%s]", dir);
		free(checkpointed_dir);
		return (-1);
	}
	if (aclp && (acltp = acl_totext(aclp,
	    ACL_APPEND_ID | ACL_SID_FMT | ACL_COMPACT_FMT)) != NULL) {
		(void) strlcpy(tlm_acls->acl_info.attr_info, acltp,
		    TLM_MAX_ACL_TXT);
		acl_free(aclp);
		free(acltp);
	}

	free(checkpointed_dir);
	return (0);
}

/*
 * backup_dir
 *
 * Create a TAR entry record for a directory
 */
static int
backup_dir(char *dir, tlm_acls_t *tlm_acls,
    tlm_cmd_t *local_commands, tlm_job_stats_t *job_stats,
    bk_selector_t *bksp)
{
	int erc;

	NDMP_LOG(LOG_DEBUG, "\"%s\"", dir);

	erc = get_dir_acl_info(dir, tlm_acls, job_stats);
	if (erc != 0) {
		NDMP_LOG(LOG_DEBUG,
		    "Could not read directory info for %s", dir);
		job_stats->js_errors++;
	} else {
		/*
		 * See if the directory must be backed up.
		 */
		if (bksp && !(*bksp->bs_fn)(bksp, &tlm_acls->acl_attr)) {
			NDMP_LOG(LOG_DEBUG, "[%s] dir skipped", dir);
			return (erc);
		}

		if (tm_tar_ops.tm_putdir != NULL)
			(void) (tm_tar_ops.tm_putdir)(dir, tlm_acls,
			    local_commands, job_stats);
	}

	return (erc);
}


/*
 * backup_file
 *
 * Create a TAR record entry for a file
 */
static longlong_t
backup_file(char *dir, char *name, tlm_acls_t *tlm_acls,
    tlm_commands_t *commands, tlm_cmd_t *local_commands,
    tlm_job_stats_t *job_stats, bk_selector_t *bksp)
{

	int erc;
	char buf[TLM_MAX_PATH_NAME];
	longlong_t rv;

	NDMP_LOG(LOG_DEBUG, "\"%s/%s\"", dir, name);

	(void) strlcpy(buf, dir, sizeof (buf));
	(void) strlcat(buf, "/", sizeof (buf));
	(void) strlcat(buf, name, sizeof (buf));

	/*
	 * get_acl_info extracts file handle, attributes and ACLs of the file.
	 * This is not efficient when the attributes and file handle of
	 * the file is already known.
	 */
	erc = get_acl_info(buf, tlm_acls);
	if (erc != TLM_NO_ERRORS) {
		NDMP_LOG(LOG_ERR, "Could not open file %s/%s.", dir, name);
		return (-ENOENT);
	}

	/* Should the file be backed up? */
	if (!bksp) {
		NDMP_LOG(LOG_DEBUG,
		    "[%s/%s] has no selection criteria", dir, name);

	} else if (!((*bksp->bs_fn)(bksp, &tlm_acls->acl_attr))) {
		NDMP_LOG(LOG_DEBUG, "[%s/%s] file skipped", dir, name);
		return (0);
	}

	/* Only the regular files and symbolic links can be backed up. */
	if (!S_ISLNK(tlm_acls->acl_attr.st_mode) &&
	    !S_ISREG(tlm_acls->acl_attr.st_mode)) {
		NDMP_LOG(LOG_DEBUG,
		    "Warning: skip backing up [%s][%s]", dir, name);
		return (-EINVAL);
	}


	if (tm_tar_ops.tm_putfile != NULL)
		rv = (tm_tar_ops.tm_putfile)(dir, name, tlm_acls, commands,
		    local_commands, job_stats);

	return (rv);
}



/*
 * backup_work
 *
 * Start the NDMP backup (V2 only).
 */
int
backup_work(char *bk_path, tlm_job_stats_t *job_stats,
    ndmp_run_args_t *np, tlm_commands_t *commands,
    ndmp_lbr_params_t *nlp)
{
	struct full_dir_info dir_info; /* the blob to push/pop with cstack_t */
	struct full_dir_info *t_dir_info, *p_dir_info;
	struct stat64 ret_attr; /* attributes of current file name */
	fs_fhandle_t ret_fh;
	char *first_name; /* where the first name is located */
	char *dname;
	int erc;
	int retval;
	cstack_t *stk;
	unsigned long fileid;
	tlm_acls_t tlm_acls;
	int dname_size;
	longlong_t fsize;
	bk_selector_t bks;
	tlm_cmd_t *local_commands;
	long	dpos;

	NDMP_LOG(LOG_DEBUG, "nr_chkpnted %d nr_ldate: %u bk_path: \"%s\"",
	    NLP_ISCHKPNTED(nlp), nlp->nlp_ldate, bk_path);

	/* Get every name in this directory */
	dname = ndmp_malloc(TLM_MAX_PATH_NAME);
	if (dname == NULL)
		return (-ENOMEM);

	local_commands = commands->tcs_command;
	retval = 0;
	(void) memset(&bks, 0, sizeof (bks));
	bks.bs_cookie = (void *)nlp;
	bks.bs_level = nlp->nlp_clevel;
	bks.bs_ldate = nlp->nlp_ldate;
	bks.bs_fn = timecmp;

	/*
	 * should we skip the whole thing?
	 */
	if (tlm_is_excluded("", bk_path, np->nr_excls)) {
		NDMP_LOG(LOG_DEBUG, "%s excluded", bk_path);
		free(dname);
		return (0);
	}

	/*
	 * Search for the top-level file-directory
	 */
	if (NLP_ISCHKPNTED(nlp)) {
		first_name = np->nr_chkp_nm;
		(void) strlcpy(first_name, bk_path, TLM_MAX_PATH_NAME);
	} else {
		first_name = tlm_build_snapshot_name(bk_path, np->nr_chkp_nm,
		    nlp->nlp_jstat->js_job_name);
	}

	(void) memset(&ret_fh, 0, sizeof (ret_fh));
	erc = fs_getstat(first_name, &ret_fh, &ret_attr);
	if (erc != 0) {
		NDMP_LOG(LOG_ERR, "Path %s not found.", first_name);
		free(dname);
		return (-EINVAL);
	}

	if ((stk = cstack_new()) == NULL) {
		free(dname);
		NDMP_LOG(LOG_DEBUG, "cstack_new failed");
		return (-ENOMEM);
	}
	(void) strlcpy(dir_info.fd_dir_name, first_name, TLM_MAX_PATH_NAME);
	(void) memcpy(&dir_info.fd_dir_fh, &ret_fh, sizeof (fs_fhandle_t));
	p_dir_info = dup_dir_info(&dir_info);

	/*
	 * Push the first name onto the stack so that we can pop it back
	 * off as part of the normal cycle
	 */
	if (cstack_push(stk, p_dir_info, 0)) {
		free(dname);
		free(p_dir_info);
		cstack_delete(stk);
		NDMP_LOG(LOG_DEBUG, "cstack_push failed");
		return (-ENOMEM);
	}

	(void) memset(&tlm_acls, 0, sizeof (tlm_acls));
	/*
	 * Did NDMP create a checkpoint?
	 */
	if (NLP_ISCHKPNTED(nlp) || fs_is_rdonly(bk_path)) {
		tlm_acls.acl_checkpointed = FALSE;
	} else {
		/* Use the checkpoint created by NDMP */
		tlm_acls.acl_checkpointed = TRUE;
	}

	/*
	 * This is level-backup.  It never resets the archive bit.
	 */
	tlm_acls.acl_clear_archive = FALSE;

	NDMP_LOG(LOG_DEBUG, "acls.chkpnt: %c acls.clear_arcbit: %c",
	    NDMP_YORN(tlm_acls.acl_checkpointed),
	    NDMP_YORN(tlm_acls.acl_clear_archive));

	while (commands->tcs_reader == TLM_BACKUP_RUN &&
	    local_commands->tc_reader == TLM_BACKUP_RUN &&
	    cstack_pop(stk, (void **)&p_dir_info, 0) == 0) {

		if (NLP_ISCHKPNTED(nlp))
			(void) strlcpy(np->nr_unchkp_nm,
			    p_dir_info->fd_dir_name, TLM_MAX_PATH_NAME);
		else
			(void) tlm_remove_checkpoint(p_dir_info->fd_dir_name,
			    np->nr_unchkp_nm);

		(void) backup_dir(np->nr_unchkp_nm, &tlm_acls, local_commands,
		    job_stats, &bks);


		while (commands->tcs_reader == TLM_BACKUP_RUN &&
		    local_commands->tc_reader == TLM_BACKUP_RUN) {

			dname_size = TLM_MAX_PATH_NAME - 1;

			NDMP_LOG(LOG_DEBUG,
			    "dir_name: %s", p_dir_info->fd_dir_name);

			(void) memset(&ret_fh, 0, sizeof (ret_fh));
			erc = fs_readdir(&p_dir_info->fd_dir_fh,
			    p_dir_info->fd_dir_name, &dpos,
			    dname, &dname_size, &ret_fh, &ret_attr);
			if (erc == 0) {
				fileid = ret_fh.fh_fid;
			} else {
				NDMP_LOG(LOG_DEBUG,
				    "Filesystem readdir in [%s]",
				    p_dir_info->fd_dir_name);
				retval = -ENOENT;
				break;
			}

			/* an empty name size marks the end of the list */
			if (dname_size == 0)
				break;
			dname[dname_size] = '\0';

			NDMP_LOG(LOG_DEBUG, "dname: \"%s\"", dname);

			/*
			 * If name refers to a directory, push its file
			 *   handle onto the stack  (skip "." and "..").
			 */
			if (rootfs_dot_or_dotdot(dname)) {
				fileid = 0;
				continue;
			}

			/*
			 * Skip the:
			 * non-dir entries which should not be backed up
			 * Or
			 * dir-type entries which have have nothing under
			 * their hierarchy to be backed up.
			 */
			if (!dbm_getone(nlp->nlp_bkmap, (u_longlong_t)fileid)) {
				NDMP_LOG(LOG_DEBUG, "Skipping %s/%s",
				    p_dir_info->fd_dir_name, dname);
				fileid = 0;
				continue;
			}

			if (tlm_is_excluded(np->nr_unchkp_nm, dname,
			    np->nr_excls)) {
				fileid = 0;
				continue;
			}
			if (S_ISDIR(ret_attr.st_mode)) {
				/*
				 * only directories get pushed onto this stack,
				 * so we do not have to test for regular files.
				 */
				t_dir_info = tlm_new_dir_info(&ret_fh,
				    p_dir_info->fd_dir_name, dname);
				if (t_dir_info == NULL) {
					NDMP_LOG(LOG_DEBUG,
					    "While backing up [%s][%s]",
					    p_dir_info->fd_dir_name, dname);
				} else if (cstack_push(stk, t_dir_info,
				    0) != 0) {
					NDMP_LOG(LOG_DEBUG,
					    "No enough memory stack_push");
					retval = -ENOMEM;
					break;
				}
			} else if (S_ISREG(ret_attr.st_mode) ||
			    S_ISLNK(ret_attr.st_mode)) {

				fsize = backup_file(np->nr_unchkp_nm, dname,
				    &tlm_acls, commands, local_commands,
				    job_stats, &bks);

				if (fsize >= 0) {
					job_stats->js_files_so_far++;
					job_stats->js_bytes_total += fsize;
				} else
					job_stats->js_errors++;
				fileid = 0;
			}
		}
		fileid = 0;
		free(p_dir_info);
		if (retval != 0)
			break;
	}

	free(dname);

	while (cstack_pop(stk, (void **)&p_dir_info, 0) == 0) {
		free(p_dir_info);
	}

	cstack_delete(stk);
	return (retval);
}


/*
 * free_paths
 *
 * Free the path names
 */
static void
free_paths(ndmp_run_args_t *np)
{
	free(np->nr_chkp_nm);
	free(np->nr_unchkp_nm);
	free(np->nr_excls);
}


/*
 * malloc_paths
 *
 * Allocate the path names (direct and checkpointed paths)
 */
static boolean_t
malloc_paths(ndmp_run_args_t *np)
{
	boolean_t rv;

	rv = TRUE;
	np->nr_chkp_nm = ndmp_malloc(TLM_MAX_PATH_NAME);
	np->nr_unchkp_nm = ndmp_malloc(TLM_MAX_PATH_NAME);
	if (!np->nr_chkp_nm || !np->nr_unchkp_nm) {
		free_paths(np);
		rv = FALSE;
	} else if ((np->nr_excls = ndmpd_make_exc_list()) == NULL) {
		free_paths(np);
		rv = FALSE;
	}
	return (rv);
}


/*
 * ndmp_backup_reader
 *
 * Backup reader thread which uses backup_work to read and TAR
 * the files/dirs to be backed up (V2 only)
 */
static int
ndmp_backup_reader(tlm_commands_t *commands, ndmp_lbr_params_t *nlp,
    char *job_name)
{
	int retval;
	ndmp_run_args_t np;
	tlm_job_stats_t *job_stats;
	tlm_cmd_t *local_commands;

	NDMP_LOG(LOG_DEBUG, "bk_path: \"%s\"", nlp->nlp_backup_path);

	local_commands = commands->tcs_command;
	(void) memset(&np, 0, sizeof (np));
	if (!malloc_paths(&np))
		return (-1);
	local_commands->tc_ref++;
	commands->tcs_reader_count++;

	job_stats = tlm_ref_job_stats(job_name);

	retval = backup_work(nlp->nlp_backup_path, job_stats, &np,
	    commands, nlp);
	write_tar_eof(local_commands);

	commands->tcs_reader_count--;
	local_commands->tc_writer = TLM_STOP;
	tlm_release_reader_writer_ipc(local_commands);
	tlm_un_ref_job_stats(job_name);

	free_paths(&np);
	return (retval);

}


/*
 * ndmp_tar_writer
 *
 * The backup writer thread that writes the TAR records to the
 * tape media (V2 only)
 */
int
ndmp_tar_writer(ndmpd_session_t *session, ndmpd_module_params_t *mod_params,
    tlm_commands_t *cmds)
{
	int bidx, nw;
	int err;
	tlm_buffer_t *buf;
	tlm_buffers_t *bufs;
	tlm_cmd_t *lcmd;	/* Local command */

	err = 0;
	if (session == NULL) {
		NDMP_LOG(LOG_DEBUG, "session == NULL");
		err = -1;
	} else if (mod_params == NULL) {
		NDMP_LOG(LOG_DEBUG, "mod_params == NULL");
		err = -1;
	} else if (cmds == NULL) {
		NDMP_LOG(LOG_DEBUG, "cmds == NULL");
		err = -1;
	}

	if (err != 0)
		return (err);

	lcmd = cmds->tcs_command;
	bufs = lcmd->tc_buffers;

	lcmd->tc_ref++;
	cmds->tcs_writer_count++;

	nw = 0;
	buf = tlm_buffer_out_buf(bufs, &bidx);
	while (cmds->tcs_writer != (int)TLM_ABORT &&
	    lcmd->tc_writer != (int)TLM_ABORT) {
		if (buf->tb_full) {
			NDMP_LOG(LOG_DEBUG, "w%d", bidx);

			if (MOD_WRITE(mod_params, buf->tb_buffer_data,
			    buf->tb_buffer_size) != 0) {
				NDMP_LOG(LOG_DEBUG,
				    "Writing buffer %d, pos: %lld",
				    bidx, session->ns_mover.md_position);
				err = -1;
				break;
			}

			tlm_buffer_mark_empty(buf);
			(void) tlm_buffer_advance_out_idx(bufs);
			buf = tlm_buffer_out_buf(bufs, &bidx);
			tlm_buffer_release_out_buf(bufs);
			nw++;
		} else {
			if (lcmd->tc_writer != TLM_BACKUP_RUN) {
				/* No more data is comming; time to exit. */
				NDMP_LOG(LOG_DEBUG,
				    "tc_writer!=TLM_BACKUP_RUN; time to exit");
				break;
			} else {
				NDMP_LOG(LOG_DEBUG, "W%d", bidx);
				tlm_buffer_in_buf_timed_wait(bufs, 100);
			}
		}
	}

	NDMP_LOG(LOG_DEBUG, "nw: %d", nw);
	if (cmds->tcs_writer != (int)TLM_ABORT) {
		NDMP_LOG(LOG_DEBUG, "tcs_writer != TLM_ABORT");
	} else {
		NDMP_LOG(LOG_DEBUG, "tcs_writer == TLM_ABORT");
	}

	if (lcmd->tc_writer != (int)TLM_ABORT) {
		NDMP_LOG(LOG_DEBUG, "tc_writer != TLM_ABORT");
	} else {
		NDMP_LOG(LOG_DEBUG, "tc_writer == TLM_ABORT");
	}
	cmds->tcs_writer_count--;
	lcmd->tc_reader = TLM_STOP;
	lcmd->tc_ref--;

	return (err);
}


/*
 * read_one_buf
 *
 * Read one buffer from the tape
 */
static int
read_one_buf(ndmpd_module_params_t *mod_params, tlm_buffers_t *bufs,
    tlm_buffer_t *buf)
{
	int rv;

	if ((rv = MOD_READ(mod_params, buf->tb_buffer_data,
	    bufs->tbs_data_transfer_size)) == 0) {
		buf->tb_eof = buf->tb_eot = FALSE;
		buf->tb_errno = 0;
		buf->tb_buffer_size = bufs->tbs_data_transfer_size;
		buf->tb_buffer_spot = 0;
		buf->tb_full = TRUE;
		(void) tlm_buffer_advance_in_idx(bufs);
	}

	return (rv);
}


/*
 * ndmp_tar_reader
 *
 * NDMP Tar reader thread. This threads keep reading the tar
 * file from the tape and wakes up the consumer thread to extract
 * it on the disk
 */
void *
ndmp_tar_reader(void *ptr)
{
	int bidx;
	int err;
	tlm_buffer_t *buf;
	tlm_buffers_t *bufs;
	tlm_cmd_t *lcmd;	/* Local command */
	ndmpd_session_t *session;
	ndmpd_module_params_t *mod_params;
	tlm_commands_t *cmds;
	ndmp_tar_reader_arg_t *argp = ptr;

	if (!argp)
		return ((void *)(uintptr_t)-1);

	session = argp->tr_session;
	mod_params = argp->tr_mod_params;
	cmds = argp->tr_cmds;

	err = 0;
	if (session == NULL) {
		NDMP_LOG(LOG_DEBUG, "session == NULL");
		err = -1;
	} else if (cmds == NULL) {
		NDMP_LOG(LOG_DEBUG, "cmds == NULL");
		err = -1;
	}

	if (err != 0) {
		tlm_cmd_signal(cmds->tcs_command, TLM_TAR_READER);
		return ((void *)(uintptr_t)err);
	}

	lcmd = cmds->tcs_command;
	bufs = lcmd->tc_buffers;

	lcmd->tc_ref++;
	cmds->tcs_reader_count++;

	/*
	 * Synchronize with our parent thread.
	 */
	tlm_cmd_signal(cmds->tcs_command, TLM_TAR_READER);

	buf = tlm_buffer_in_buf(bufs, &bidx);
	while (cmds->tcs_reader == TLM_RESTORE_RUN &&
	    lcmd->tc_reader == TLM_RESTORE_RUN) {

		if (buf->tb_full) {
			NDMP_LOG(LOG_DEBUG, "R%d", bidx);
			/*
			 * The buffer is still full, wait for the consumer
			 * thread to use it.
			 */
			tlm_buffer_out_buf_timed_wait(bufs, 100);
			buf = tlm_buffer_in_buf(bufs, NULL);
		} else {
			NDMP_LOG(LOG_DEBUG, "r%d", bidx);

			err = read_one_buf(mod_params, bufs, buf);
			if (err < 0) {
				NDMP_LOG(LOG_DEBUG,
				    "Reading buffer %d, pos: %lld",
				    bidx, session->ns_mover.md_position);

				/* Force the writer to stop. */
				buf->tb_eot = buf->tb_eof = TRUE;
				break;
			} else if (err == 1) {
				NDMP_LOG(LOG_DEBUG,
				    "operation aborted or session terminated");
				err = 0;
				break;
			}

			buf = tlm_buffer_in_buf(bufs, &bidx);
			tlm_buffer_release_in_buf(bufs);
		}
	}

	/*
	 * If the consumer is waiting for us, wake it up so that it detects
	 * we're quiting.
	 */
	lcmd->tc_writer = TLM_STOP;
	tlm_buffer_release_in_buf(bufs);
	(void) usleep(1000);

	/*
	 * Clean up.
	 */
	cmds->tcs_reader_count--;
	lcmd->tc_ref--;
	return ((void *)(uintptr_t)err);
}


/*
 * ndmpd_tar_backup
 *
 * Check must have been done that backup work directory exists, before
 * calling this function.
 */
static int
ndmpd_tar_backup(ndmpd_session_t *session, ndmpd_module_params_t *mod_params,
    ndmp_lbr_params_t *nlp)
{
	char jname[TLM_MAX_BACKUP_JOB_NAME];
	int err;
	tlm_commands_t *cmds;

	if (mod_params->mp_operation != NDMP_DATA_OP_BACKUP) {
		NDMP_LOG(LOG_DEBUG,
		    "mod_params->mp_operation != NDMP_DATA_OP_BACKUP");
		err = -1;
	} else {
		if (ndmpd_mark_inodes_v2(session, nlp) != 0)
			err = -1;
		else if (ndmp_get_bk_dir_ino(nlp))
			err = -1;
		else
			err = 0;
	}

	if (err != 0)
		return (err);

	(void) ndmp_new_job_name(jname);
	if (backup_create_structs(session, jname) < 0)
		return (-1);

	nlp->nlp_jstat->js_start_ltime = time(NULL);
	nlp->nlp_jstat->js_start_time = nlp->nlp_jstat->js_start_ltime;
	nlp->nlp_jstat->js_chkpnt_time = nlp->nlp_cdate;

	if (!session->ns_data.dd_abort) {

		cmds = &nlp->nlp_cmds;
		cmds->tcs_reader = cmds->tcs_writer = TLM_BACKUP_RUN;
		cmds->tcs_command->tc_reader = TLM_BACKUP_RUN;
		cmds->tcs_command->tc_writer = TLM_BACKUP_RUN;

		if (ndmp_write_utf8magic(cmds->tcs_command) < 0) {
			backup_release_structs(session);
			return (-1);
		}

		NDMP_LOG(LOG_DEBUG, "Backing up \"%s\" started.",
		    nlp->nlp_backup_path);

		err = ndmp_backup_reader(cmds, nlp, jname);
		if (err != 0) {
			backup_release_structs(session);
			NDMP_LOG(LOG_DEBUG, "Launch ndmp_backup_reader: %s",
			    strerror(err));
			return (-1);
		}

		/* Act as the writer thread. */
		err = ndmp_tar_writer(session, mod_params, cmds);

		nlp->nlp_jstat->js_stop_time = time(NULL);

		NDMP_LOG(LOG_DEBUG,
		    "Runtime [%s] %llu bytes (%llu): %d seconds",
		    nlp->nlp_backup_path, session->ns_mover.md_data_written,
		    session->ns_mover.md_data_written,
		    nlp->nlp_jstat->js_stop_time -
		    nlp->nlp_jstat->js_start_ltime);
		MOD_LOG(mod_params,
		    "Runtime [%s] %llu bytes (%llu): %d seconds",
		    nlp->nlp_backup_path, session->ns_mover.md_data_written,
		    session->ns_mover.md_data_written,
		    nlp->nlp_jstat->js_stop_time -
		    nlp->nlp_jstat->js_start_ltime);

		if (session->ns_data.dd_abort)
			err = -1;

		NDMP_LOG(LOG_DEBUG, "Backing up \"%s\" finished. (%d)",
		    nlp->nlp_backup_path, err);
	} else {
		nlp->nlp_jstat->js_stop_time = time(NULL);
		NDMP_LOG(LOG_DEBUG, "Backing up \"%s\" aborted.",
		    nlp->nlp_backup_path);
		err = 0;
	}

	backup_release_structs(session);
	return (err);
}


/*
 * ndmpd_tar_restore
 *
 * Restore function that launches TAR reader thread to read from the
 * tape and writes the extracted files/dirs to the filesystem
 */
static int
ndmpd_tar_restore(ndmpd_session_t *session, ndmpd_module_params_t *mod_params,
    ndmp_lbr_params_t *nlp)
{
	char jname[TLM_MAX_BACKUP_JOB_NAME];
	char *rspath;
	int err;
	tlm_commands_t *cmds;
	ndmp_tar_reader_arg_t arg;
	tlm_backup_restore_arg_t tlm_arg;
	ndmp_name *ent;
	pthread_t rdtp, wrtp;
	int i;

	if (mod_params->mp_operation != NDMP_DATA_OP_RECOVER) {
		NDMP_LOG(LOG_DEBUG,
		    "mod_params->mp_operation != NDMP_DATA_OP_RECOVER");
		return (-1);
	}

	if (nlp->nlp_restore_path[0] != '\0')
		rspath = nlp->nlp_restore_path;
	else if (nlp->nlp_restore_bk_path[0] != '\0')
		rspath = nlp->nlp_restore_bk_path;
	else
		rspath = "";

	(void) ndmp_new_job_name(jname);
	if (restore_create_structs(session, jname) < 0)
		return (-1);

	nlp->nlp_jstat->js_start_ltime = time(NULL);
	nlp->nlp_jstat->js_start_time = time(NULL);

	if (!session->ns_data.dd_abort) {
		cmds = &nlp->nlp_cmds;
		cmds->tcs_reader = cmds->tcs_writer = TLM_RESTORE_RUN;
		cmds->tcs_command->tc_reader = TLM_RESTORE_RUN;
		cmds->tcs_command->tc_writer = TLM_RESTORE_RUN;

		NDMP_LOG(LOG_DEBUG, "Restoring to \"%s\" started.", rspath);
		NDMP_LOG(LOG_DEBUG, "Restoring from %s tape(s).",
		    ndmp_data_get_mover_mode(session));

		arg.tr_session = session;
		arg.tr_mod_params = mod_params;
		arg.tr_cmds = cmds;

		err = pthread_create(&rdtp, NULL, ndmp_tar_reader, &arg);
		if (err == 0) {
			tlm_cmd_wait(cmds->tcs_command, TLM_TAR_READER);
		} else {
			NDMP_LOG(LOG_DEBUG, "Launch ndmp_tar_reader: %m");
			return (-1);
		}

		if (!ndmp_check_utf8magic(cmds->tcs_command)) {
			NDMP_LOG(LOG_DEBUG, "UTF8Magic not found!");
		} else {
			NDMP_LOG(LOG_DEBUG, "UTF8Magic found");
		}

		(void) memset(&tlm_arg, 0, sizeof (tlm_backup_restore_arg_t));
		(void) pthread_barrier_init(&tlm_arg.ba_barrier, 0, 2);

		/*
		 * Set up restore parameters
		 */
		tlm_arg.ba_commands = cmds;
		tlm_arg.ba_cmd = cmds->tcs_command;
		tlm_arg.ba_job = nlp->nlp_jstat->js_job_name;
		tlm_arg.ba_dir = nlp->nlp_restore_path;
		for (i = 0; i < nlp->nlp_nfiles; i++) {
			ent = (ndmp_name *)MOD_GETNAME(mod_params, i);
			tlm_arg.ba_sels[i] = ent->name;
		}


		if (tm_tar_ops.tm_getfile != NULL) {
			err = pthread_create(&wrtp, NULL, tm_tar_ops.tm_getfile,
			    &tlm_arg);
		} else {
			(void) pthread_barrier_destroy(&tlm_arg.ba_barrier);
			NDMP_LOG(LOG_DEBUG,
			    "Thread create tm_getfile: ops NULL");
			return (-1);
		}
		if (err == 0) {
			(void) pthread_barrier_wait(&tlm_arg.ba_barrier);
		} else {
			(void) pthread_barrier_destroy(&tlm_arg.ba_barrier);
			NDMP_LOG(LOG_DEBUG, "thread create tm_getfile: %m");
			return (-1);
		}

		(void) pthread_join(rdtp, NULL);
		(void) pthread_join(wrtp, NULL);
		(void) pthread_barrier_destroy(&tlm_arg.ba_barrier);

		nlp->nlp_jstat->js_stop_time = time(NULL);

		/* Send the list of un-recovered files/dirs to the client.  */
		(void) send_unrecovered_list(mod_params, nlp);

		ndmp_stop_local_reader(session, cmds);
		ndmp_wait_for_reader(cmds);
		ndmp_stop_remote_reader(session);
		NDMP_LOG(LOG_DEBUG, "Restoring to \"%s\" finished. (%d)",
		    rspath, err);
	} else {
		nlp->nlp_jstat->js_stop_time = time(NULL);

		/* nothing restored. */
		(void) send_unrecovered_list(mod_params, nlp);
		NDMP_LOG(LOG_DEBUG, "Restoring to \"%s\" aborted.",
		    rspath);
		err = -1;
	}

	NDMP_FREE(nlp->nlp_restore_path);
	backup_release_structs(session);

	return (err);
}


/*
 * prefixdir
 *
 * Extract the path for a given full path entry
 */
static char *
prefixdir(char *dir, char *suffix)
{
	static char tmp[TLM_MAX_PATH_NAME];
	char *tend, *send; /* tmp and suffix end */

	if (dir == NULL || suffix == NULL)
		return (NULL);

	if (*suffix == '\0')
		return (dir);

	if (*dir == '\0')
		return (NULL);

	(void) strlcpy(tmp, dir, TLM_MAX_PATH_NAME);
	tend = &tmp[strlen(tmp)];
	send = &suffix[strlen(suffix)];

	/*
	 * Move backward as far as the last part of the dir and
	 * the suffix match.
	 */
	while (tend >= tmp && send >= suffix)
		if (*tend == *send)
			tend--, send--;
		else
			break;

	*++tend = '\0';
	return (tmp);
}


/*
 * get_nfiles
 *
 * Get the count of files to be restored
 */
static int
get_nfiles(ndmpd_session_t *session, ndmpd_module_params_t *params)
{
	if (session->ns_data.dd_nlist_len == 0) {
		MOD_LOG(params, "Error: nothing specified to be restored.\n");
		return (-1);
	}

	return (session->ns_data.dd_nlist_len);
}


/*
 * get_restore_dest
 *
 * Get the full pathname of where the entries should be restored to.
 */
static char *
get_restore_dest(ndmpd_module_params_t *params)
{
	ndmp_name *ent;
	char *cp;

	/*
	 * Destination of restore:
	 * NetBackup of Veritas(C) sends the entries like this:
	 *
	 *	ent[i].name: is the relative pathname of what is selected in
	 *	  the GUI.
	 *	ent[i].dest: is the full pathname of where the dir/file must
	 *	  be restored to.
	 *	ent[i].ssi: 0
	 *	ent[i].fh_info: 0
	 *
	 */
	ent = (ndmp_name *)MOD_GETNAME(params, 0);
	cp = prefixdir(ent->dest, ent->name);
	if (cp == NULL) {
		MOD_LOG(params, "Error: empty restore path.\n");
		return (NULL);
	}

	return (cp);
}


/*
 * correct_ents
 *
 * Correct the entries in the restore list by appending the appropriate
 * path to them
 */
static int
correct_ents(ndmpd_module_params_t *params, int n, char *bkpath)
{
	char *cp, *pathname;
	int i, len, rv;
	ndmp_name *ent;

	if ((pathname = ndmp_malloc(TLM_MAX_PATH_NAME)) == NULL) {
		MOD_LOG(params, "Error: insufficient memory.\n");
		return (-1);
	}

	rv = 0;
	/* Append the backup path to all the "ent[].name"s. */
	for (i = 0; i < n; i++) {
		ent = (ndmp_name *)MOD_GETNAME(params, i);

		NDMP_LOG(LOG_DEBUG,
		    "Old: ent[%d].name: \"%s\"", i, ent->name);
		NDMP_LOG(LOG_DEBUG,
		    "Old: ent[%d].dest: \"%s\"", i, ent->dest);

		/* remove trailing slash */
		len = strlen(ent->name);
		if (ent->name[len - 1] == '/')
			ent->name[len - 1] = '\0';

		if (!tlm_cat_path(pathname, bkpath, ent->name)) {
			MOD_LOG(params, "Error: path too long.\n");
			rv = -1;
			break;
		}

		/* Make a copy of the new string and save it in ent->name. */
		cp = strdup(pathname);
		if (cp == NULL) {
			MOD_LOG(params, "Error: insufficient memory.\n");
			rv = -1;
			break;
		}
		free(ent->name);
		ent->name = cp;

		NDMP_LOG(LOG_DEBUG,
		    "New: ent[%d].name: \"%s\"", i, ent->name);
	}

	free(pathname);
	return (rv);
}


/*
 * check_restore_paths
 *
 * Go through the restore list and check the validity of the
 * restore path.
 */
static int
check_restore_paths(ndmpd_module_params_t *params, int n, char *rspath)
{
	int i, rv;
	ndmp_name *ent;

	rv = 0;
	if (rspath != NULL && *rspath != '\0') {
		NDMP_LOG(LOG_DEBUG, "rspath: \"%s\"", rspath);
		if (!fs_volexist(rspath)) {
			MOD_LOG(params,
			    "Error: Invalid volume name for restore.");
			rv = -1;
		}
	} else {
		for (i = 0; i < n; i++) {
			ent = (ndmp_name *)MOD_GETNAME(params, i);
			NDMP_LOG(LOG_DEBUG,
			    "ent[%d].name: \"%s\"", i, ent->name);

			if (!fs_volexist(ent->name)) {
				MOD_LOG(params,
				    "Error: Invalid volume name for restore.",
				    ent->name);
				rv = -1;
				break;
			}
		}
	}

	return (rv);
}


/*
 * check_backup_dir_validity
 *
 * Check if the backup directory is valid. Make sure it exists and
 * is writable. Check for snapshot and readonly cases.
 */
static int
check_backup_dir_validity(ndmpd_module_params_t *params, char *bkpath)
{
	char *msg;
	int rv;
	struct stat64 st;

	rv = NDMP_NO_ERR;
	if (stat64(bkpath, &st) < 0) {
		msg = strerror(errno);
		MOD_LOG(params, "Error: stat(%s): %s.\n", bkpath, msg);
		rv = NDMP_ILLEGAL_ARGS_ERR;
	} else if (!S_ISDIR(st.st_mode)) {
		MOD_LOG(params, "Error: %s is not a directory.\n", bkpath);
		rv = NDMP_ILLEGAL_ARGS_ERR;
	} else if (fs_is_rdonly(bkpath) && !fs_is_chkpntvol(bkpath) &&
	    fs_is_chkpnt_enabled(bkpath)) {
		MOD_LOG(params, "Error: %s is not a checkpointed path.\n",
		    bkpath);
		rv = NDMP_BAD_FILE_ERR;
	}

	return (rv);
}


/*
 * ndmp_backup_extract_params
 *
 * Go through the backup parameters and check the validity
 * for each one. Then set the NLP flags according to the parameters.
 */
int
ndmp_backup_extract_params(ndmpd_session_t *session,
    ndmpd_module_params_t *params)
{
	char *cp;
	int rv;
	ndmp_lbr_params_t *nlp;

	/* Extract directory to be backed up from env variables */
	if ((nlp = ndmp_get_nlp(session)) == NULL) {
		MOD_LOG(params, "Error: Internal error: nlp == NULL.\n");
		return (NDMP_ILLEGAL_ARGS_ERR);
	}
	if ((nlp->nlp_backup_path = get_backup_path_v2(params)) == NULL)
		return (NDMP_FILE_NOT_FOUND_ERR);

	if ((rv = check_backup_dir_validity(params,
	    nlp->nlp_backup_path)) != NDMP_NO_ERR)
		return (rv);

	/* Should the st_ctime be ignored when backing up? */
	if (ndmp_ignore_ctime) {
		NDMP_LOG(LOG_DEBUG, "ignoring st_ctime");
		NLP_SET(nlp, NLPF_IGNCTIME);
	} else
		NLP_UNSET(nlp, NLPF_IGNCTIME);

	/* Should the st_lmtime be ignored when backing up? */
	if (ndmp_include_lmtime) {
		NDMP_LOG(LOG_DEBUG, "including st_lmtime");
		NLP_SET(nlp, NLPF_INCLMTIME);
	} else
		NLP_UNSET(nlp, NLPF_INCLMTIME);

	NDMP_LOG(LOG_DEBUG, "flags %x", nlp->nlp_flags);

	/* Is backup history requested? */
	cp = MOD_GETENV(params, "HIST");
	if (cp == NULL) {
		NDMP_LOG(LOG_DEBUG, "env(HIST) not specified");
		NLP_UNSET(nlp, NLPF_FH);
	} else {
		NDMP_LOG(LOG_DEBUG, "env(HIST): \"%s\"", cp);

		if (strchr("t_ty_y", *cp))
			NLP_SET(nlp, NLPF_FH);
		else
			NLP_UNSET(nlp, NLPF_FH);
	}

	nlp->nlp_clevel = 0;
	/* Is it an incremental backup? */
	cp = MOD_GETENV(params, "LEVEL");
	if (cp == NULL) {
		NDMP_LOG(LOG_DEBUG,
		    "env(LEVEL) not specified, default to 0");
	} else if (*cp < '0' || *cp > '9' || *(cp+1) != '\0') {
		NDMP_LOG(LOG_DEBUG, "Invalid backup level '%s'", cp);
		return (NDMP_ILLEGAL_ARGS_ERR);
	} else
		nlp->nlp_clevel = *cp - '0';

	/* Extract last backup time from the dumpdates file */
	nlp->nlp_llevel = nlp->nlp_clevel;
	nlp->nlp_ldate = 0;
	if (ndmpd_get_dumptime(nlp->nlp_backup_path, &nlp->nlp_llevel,
	    &nlp->nlp_ldate) < 0) {
		MOD_LOG(params, "Error: getting dumpdate for %s level %d\n",
		    nlp->nlp_backup_path, nlp->nlp_clevel);
		return (NDMP_NO_MEM_ERR);
	}

	NDMP_LOG(LOG_DEBUG,
	    "Date of this level %d on \"%s\": %s",
	    nlp->nlp_clevel, nlp->nlp_backup_path, cctime(&nlp->nlp_cdate));
	NDMP_LOG(LOG_DEBUG,
	    "Date of last level %d on \"%s\": %s",
	    nlp->nlp_llevel, nlp->nlp_backup_path, cctime(&nlp->nlp_ldate));

	/* Should the dumpdate file be updated? */
	cp = MOD_GETENV(params, "UPDATE");
	if (cp == NULL) {
		NDMP_LOG(LOG_DEBUG,
		    "env(UPDATE) not specified, default to TRUE");
		NLP_SET(nlp, NLPF_UPDATE);
	} else {
		NDMP_LOG(LOG_DEBUG, "env(UPDATE): \"%s\"", cp);
		if (strchr("t_ty_y", *cp) != NULL)
			NLP_SET(nlp, NLPF_UPDATE);
		else
			NLP_UNSET(nlp, NLPF_UPDATE);
	}

	return (NDMP_NO_ERR);
}



/*
 * log_bk_params_v2
 *
 * Dump the value of the parameters in the log file for debugging.
 */
void
log_bk_params_v2(ndmpd_session_t *session, ndmpd_module_params_t *params,
    ndmp_lbr_params_t *nlp)
{
	MOD_LOG(params, "Date of this level %d on \"%s\": %s\n",
	    nlp->nlp_clevel, nlp->nlp_backup_path, cctime(&nlp->nlp_cdate));
	MOD_LOG(params, "Date of last level %d on \"%s\": %s\n",
	    nlp->nlp_llevel, nlp->nlp_backup_path, cctime(&nlp->nlp_ldate));

	MOD_LOG(params, "Backing up: \"%s\".\n", nlp->nlp_backup_path);
	MOD_LOG(params, "Record size: %d\n", session->ns_mover.md_record_size);
	MOD_LOG(params, "File history: %c.\n",
	    NDMP_YORN(NLP_ISSET(nlp, NLPF_FH)));
	MOD_LOG(params, "Update: %s\n",
	    NLP_ISSET(nlp, NLPF_UPDATE) ? "TRUE" : "FALSE");

}


/*
 * same_path
 *
 * Find out if the paths are the same regardless of the ending slash
 *
 * Examples :
 *   /a/b/c == /a/b/c
 *   /a/b/c/ == /a/b/c
 *   /a/b/c == /a/b/c/
 */
static boolean_t
same_path(char *s, char *t)
{
	boolean_t rv;
	int slen, tlen;

	rv = FALSE;
	slen = strlen(s);
	tlen = strlen(t);
	if (slen == tlen && strcmp(s, t) == 0) {
		rv = TRUE;
	} else {
		if (slen == tlen - 1) {
			if (strncmp(s, t, slen) == 0 && t[tlen - 1] == '/')
				rv = TRUE;
		} else if (tlen == slen -1) {
			if (strncmp(s, t, tlen) == 0 && s[slen - 1] == '/')
				rv = TRUE;
		}
	}

	NDMP_LOG(LOG_DEBUG, "rv: %d", rv);
	return (rv);
}


/*
 * ndmp_restore_extract_params
 *
 * Go through the restore parameters and check them and extract them
 * by setting NLP flags and other values.
 *
 * Parameters:
 *
 * Returns:
 *   0: on success
 *  -1: otherwise
 */
int
ndmp_restore_extract_params(ndmpd_session_t *session,
    ndmpd_module_params_t *params)
{
	char *bkpath, *rspath;
	ndmp_lbr_params_t *nlp;

	if ((nlp = ndmp_get_nlp(session)) == NULL) {
		NDMP_LOG(LOG_DEBUG, "nlp == NULL");
		return (-1);
	}

	/* Extract directory from where the backup was made. */
	if ((bkpath = get_backup_path_v2(params)) == NULL)
		return (NDMP_ILLEGAL_ARGS_ERR);

	nlp->nlp_restore_bk_path = bkpath;

	/* The number of the selections. */
	if ((nlp->nlp_nfiles = get_nfiles(session, params)) == 0)
		return (NDMP_ILLEGAL_ARGS_ERR);

	NDMP_LOG(LOG_DEBUG, "nfiles: %d", nlp->nlp_nfiles);

	if ((rspath = get_restore_dest(params)) == NULL)
		return (NDMP_ILLEGAL_ARGS_ERR);

	if (fs_is_rdonly(rspath)) {
		MOD_LOG(params,
		    "Error: Can't restore to a read-only volume: \"%s\"\n",
		    rspath);
		return (NDMP_ILLEGAL_ARGS_ERR);
	}
	if (fs_is_chkpntvol(rspath)) {
		MOD_LOG(params,
		    "Error: Can't restore to a checkpoint: \"%s\"\n", rspath);
		return (NDMP_ILLEGAL_ARGS_ERR);
	}

	if (same_path(bkpath, rspath))
		rspath = "";

	if ((nlp->nlp_restore_path = strdup(rspath)) == NULL)
		return (NDMP_NO_MEM_ERR);

	bkpath = trim_name(bkpath);
	if (correct_ents(params, nlp->nlp_nfiles, bkpath) < 0) {
		free(nlp->nlp_restore_path);
		return (NDMP_ILLEGAL_ARGS_ERR);
	}

	if (check_restore_paths(params, nlp->nlp_nfiles, rspath) < 0) {
		free(nlp->nlp_restore_path);
		return (NDMP_ILLEGAL_ARGS_ERR);
	}

	MOD_LOG(params, "Restoring %d files.\n", nlp->nlp_nfiles);
	MOD_LOG(params, "Restoring to: \"%s\".\n", nlp->nlp_restore_path);
	MOD_LOG(params, "Record size: %d\n", session->ns_mover.md_record_size);

	return (NDMP_NO_ERR);
}

/*
 * ndmpd_tar_backup_starter (V2 only)
 *
 * The main backup starter function. It creates a snapshot if necessary
 * and calls ndmp_tar_backup to perform the actual backup. It does the cleanup
 * and release the snapshot at the end.
 */
void *
ndmpd_tar_backup_starter(void *arg)
{
	ndmpd_module_params_t *mod_params = arg;
	int err;
	ndmpd_session_t *session;
	ndmp_lbr_params_t *nlp;

	session = (ndmpd_session_t *)(mod_params->mp_daemon_cookie);
	*(mod_params->mp_module_cookie) = nlp = ndmp_get_nlp(session);
	ndmp_session_ref(session);

	err = 0;
	if (fs_is_chkpntvol(nlp->nlp_backup_path) ||
	    fs_is_rdonly(nlp->nlp_backup_path) ||
	    !fs_is_chkpnt_enabled(nlp->nlp_backup_path))
		NLP_SET(nlp, NLPF_CHKPNTED_PATH);
	else {
		NLP_UNSET(nlp, NLPF_CHKPNTED_PATH);
		if (ndmp_create_snapshot(nlp->nlp_backup_path,
		    nlp->nlp_jstat->js_job_name) < 0) {
			MOD_LOG(mod_params,
			    "Error: creating checkpoint on %s\n",
			    nlp->nlp_backup_path);
			/* -1 causes halt reason to become internal error. */
			err = -1;
		}
	}

	NDMP_LOG(LOG_DEBUG, "NLPF_CHKPNTED_PATH: %c",
	    NDMP_YORN(NLP_ISCHKPNTED(nlp)));
	NDMP_LOG(LOG_DEBUG, "err: %d, update %c",
	    err, NDMP_YORN(NLP_SHOULD_UPDATE(nlp)));

	if (err == 0) {
		err = ndmp_get_cur_bk_time(nlp, &nlp->nlp_cdate,
		    nlp->nlp_jstat->js_job_name);
		if (err != 0) {
			NDMP_LOG(LOG_DEBUG, "err %d", err);
		} else {
			log_bk_params_v2(session, mod_params, nlp);
			err = ndmpd_tar_backup(session, mod_params, nlp);
		}
	}

	if (nlp->nlp_bkmap >= 0) {
		(void) dbm_free(nlp->nlp_bkmap);
		nlp->nlp_bkmap = -1;
	}

	if (!NLP_ISCHKPNTED(nlp))
		(void) ndmp_remove_snapshot(nlp->nlp_backup_path,
		    nlp->nlp_jstat->js_job_name);

	NDMP_LOG(LOG_DEBUG, "err %d, update %c",
	    err, NDMP_YORN(NLP_SHOULD_UPDATE(nlp)));

	if (err == 0 && NLP_SHOULD_UPDATE(nlp)) {
		if (ndmpd_put_dumptime(nlp->nlp_backup_path, nlp->nlp_clevel,
		    nlp->nlp_cdate) < 0) {
			err = EPERM;
			MOD_LOG(mod_params,
			    "Error: updating the dumpdates file on %s\n",
			    nlp->nlp_backup_path);
		}
	}

	MOD_DONE(mod_params, err);

	/* nlp_params is allocated in start_backup() */
	NDMP_FREE(nlp->nlp_params);

	NS_DEC(nbk);
	ndmp_session_unref(session);
	return ((void *)(uintptr_t)err);
}


/*
 * ndmpd_tar_backup_abort
 *
 * Abort the running backup by stopping the reader thread (V2 only)
 */
int
ndmpd_tar_backup_abort(void *module_cookie)
{
	ndmp_lbr_params_t *nlp;

	nlp = (ndmp_lbr_params_t *)module_cookie;
	if (nlp != NULL && nlp->nlp_session != NULL) {
		if (nlp->nlp_session->ns_data.dd_mover.addr_type ==
		    NDMP_ADDR_TCP && nlp->nlp_session->ns_data.dd_sock != -1) {
			(void) close(nlp->nlp_session->ns_data.dd_sock);
			nlp->nlp_session->ns_data.dd_sock = -1;
		}
		ndmp_stop_reader_thread(nlp->nlp_session);
	}

	return (0);
}

/*
 * ndmpd_tar_restore_starter
 *
 * Starts the restore by running ndmpd_tar_restore function (V2 only)
 */

void *
ndmpd_tar_restore_starter(void *arg)
{
	ndmpd_module_params_t *mod_params = arg;
	int err;
	ndmpd_session_t *session;
	ndmp_lbr_params_t *nlp;

	session = (ndmpd_session_t *)(mod_params->mp_daemon_cookie);
	*(mod_params->mp_module_cookie) = nlp = ndmp_get_nlp(session);
	ndmp_session_ref(session);

	err = ndmpd_tar_restore(session, mod_params, nlp);
	MOD_DONE(mod_params, err);

	/* nlp_params is allocated in start_recover() */
	NDMP_FREE(nlp->nlp_params);

	NS_DEC(nrs);
	ndmp_session_unref(session);
	return ((void *)(uintptr_t)err);
}


/*
 * ndmpd_tar_restore_abort
 *
 * Aborts the restore operation by stopping the writer thread (V2 only)
 */
int
ndmpd_tar_restore_abort(void *module_cookie)
{
	ndmp_lbr_params_t *nlp;

	nlp = (ndmp_lbr_params_t *)module_cookie;
	if (nlp != NULL && nlp->nlp_session != NULL) {
		(void) mutex_lock(&nlp->nlp_mtx);
		if (nlp->nlp_session->ns_data.dd_mover.addr_type ==
		    NDMP_ADDR_TCP && nlp->nlp_session->ns_data.dd_sock != -1) {
			(void) close(nlp->nlp_session->ns_data.dd_sock);
			nlp->nlp_session->ns_data.dd_sock = -1;
		}
		(void) cond_broadcast(&nlp->nlp_cv);
		(void) mutex_unlock(&nlp->nlp_mtx);
		ndmp_stop_writer_thread(nlp->nlp_session);
	}

	return (0);
}