summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/cachefs/mount/mount.c
blob: e495babca361e4dce529b2217df0a8f160f27c15 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/*	    All Rights Reserved */

/*
 * Portions of this source code were derived from Berkeley 4.3 BSD
 * under license from the Regents of the University of California.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *
 *			mount.c
 *
 * Cachefs mount program.
 */

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <wait.h>
#include <ctype.h>
#include <fcntl.h>
#include <fslib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <sys/mntio.h>
#include <sys/fs/cachefs_fs.h>
#include <sys/utsname.h>
#include <rpc/rpc.h>
#include <kstat.h>
#undef MAX
#include <nfs/nfs.h>
#include <nfs/nfs_clnt.h>
#include <sys/mkdev.h>
#include "../common/subr.h"
#include "../common/cachefsd.h"

char *cfs_opts[] = {
#define	CFSOPT_BACKFSTYPE	0
	"backfstype",
#define	CFSOPT_CACHEDIR		1
	"cachedir",
#define	CFSOPT_CACHEID		2
	"cacheid",
#define	CFSOPT_BACKPATH		3
	"backpath",

#define	CFSOPT_WRITEAROUND	4
	"write-around",
#define	CFSOPT_NONSHARED	5
	"non-shared",

#define	CFSOPT_DISCONNECTABLE	6
	"disconnectable",
#define	CFSOPT_SOFT		7
	"soft",

#define	CFSOPT_NOCONST		8
	"noconst",
#define	CFSOPT_CODCONST		9
	"demandconst",

#define	CFSOPT_LOCALACCESS	10
	"local-access",
#define	CFSOPT_LAZYMOUNT	11
	"lazy-mount",

#define	CFSOPT_RW		12
	"rw",
#define	CFSOPT_RO		13
	"ro",
#define	CFSOPT_SUID		14
	"suid",
#define	CFSOPT_NOSUID		15
	"nosuid",
#define	CFSOPT_REMOUNT		16
	"remount",
#define	CFSOPT_FGSIZE		17
	"fgsize",
#define	CFSOPT_POPSIZE		18
	"popsize",
#define	CFSOPT_ACREGMIN		19
	"acregmin",
#define	CFSOPT_ACREGMAX		20
	"acregmax",
#define	CFSOPT_ACDIRMIN		21
	"acdirmin",
#define	CFSOPT_ACDIRMAX		22
	"acdirmax",
#define	CFSOPT_ACTIMEO		23
	"actimeo",
#define	CFSOPT_SLIDE		24
	"slide",
#define	CFSOPT_NOSETSEC		25
	"nosec",	/* XXX should we use MNTOPT_NOTSETSEC? */
#define	CFSOPT_LLOCK		26
	"llock",
#define	CFSOPT_NONOTIFY		27
	"nonotify",
#define	CFSOPT_SNR		28
	"snr",
#define	CFSOPT_NOFILL		29
	"nofill",
#ifdef CFS_NFSV3_PASSTHROUGH
#define	CFSOPT_NFSV3PASSTHROUGH	30
	"nfsv3pass",
#endif /* CFS_NFSV3_PASSTHROUGH */
	NULL
};

#define	MNTTYPE_CFS	"cachefs"	/* XXX - to be added to mntent.h */
					/* XXX - and should be cachefs */
#define	CFS_DEF_DIR	"/cache"	/* XXX - should be added to cfs.h */

#define	bad(val) (val == NULL || !isdigit(*val))

#define	VFS_PATH	"/usr/lib/fs"
#define	ALT_PATH	"/etc/fs"

/* forward references */
void usage(char *msgp);
void pr_err(char *fmt, ...);
int set_cfs_args(char *optionp, struct cachefs_mountargs *margsp, int *mflagp,
    char **backfstypepp, char **reducepp, int *notifyp, int *nfsv3pass);
int get_mount_point(char *cachedirp, char *specp, char **pathpp);
int dobackmnt(struct cachefs_mountargs *margsp, char *reducep, char *specp,
    char *backfstypep, char *mynamep, int readonly);
void doexec(char *fstype, char **newargv, char *myname);
char *get_back_fsid(char *specp);
char *get_cacheid(char *, char *);
void record_mount(char *mntp, char *specp, char *backfsp, char *backfstypep,
    char *cachedirp, char *cacheidp, char *optionp, char *reducep);
int daemon_notify(char *cachedirp, char *cacheidp);
int pingserver(char *backmntp);
int check_cache(char *cachedirp);
uint32_t cachefs_get_back_nfsvers(char *cfs_backfs, int nomnttab);
int cfs_nfsv4_build_opts(char *optionp, char *cfs_nfsv4ops);

int nomnttab;
int quiet;
/*
 *
 *			main
 *
 * Description:
 *	Main routine for the cachefs mount program.
 * Arguments:
 *	argc	number of command line arguments
 *	argv	list of command line arguments
 * Returns:
 *	Returns 0 for success, 1 an error was encountered.
 * Preconditions:
 */

int
main(int argc, char **argv)
{
	char *myname;
	char *optionp;
	char *opigp;
	int mflag;
	int readonly;
	struct cachefs_mountargs margs;
	char *backfstypep;
	char *reducep;
	char *specp;
	int xx;
	int stat_loc;
	char *newargv[20];
	char *mntp;
	pid_t pid;
	int mounted;
	int c;
	int lockid;
	int Oflg;
	char *strp;
	char servname[33];
	int notify = 1;
	struct stat64 statb;
	struct mnttagdesc mtdesc;
	char mops[MAX_MNTOPT_STR];
	char cfs_nfsv4ops[MAX_MNTOPT_STR];
	uint32_t nfsvers = 0;
	uint32_t nfsvers_error = FALSE;
	int nfsv3pass = 0;
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if (argv[0]) {
		myname = strrchr(argv[0], '/');
		if (myname)
			myname++;
		else
			myname = argv[0];
	} else {
		myname = "path unknown";
	}

	optionp = NULL;
	nomnttab = 0;
	quiet = 0;
	readonly = 0;
	Oflg = 0;
	cfs_nfsv4ops[0] = '\0';

	/* process command line options */
	while ((c = getopt(argc, argv, "mo:Orq")) != EOF) {
		switch (c) {
		case 'm':	/* no entry in /etc/mnttab */
			nomnttab = 1;
			break;

		case 'o':
			optionp = optarg;
			break;

		case 'O':
			Oflg++;
			break;

		case 'r':	/* read only mount */
			readonly = 1;
			break;

		case 'q':
			quiet = 1;
			break;

		default:
			usage("invalid option");
			return (1);
		}
	}

	/* if -o not specified */
	if (optionp == NULL) {
		usage(gettext("\"-o backfstype\" must be specified"));
		return (1);
	}

	/* verify special device and mount point are specified */
	if (argc - optind < 2) {
		usage(gettext("must specify special device and mount point"));
		return (1);
	}

	/* Store mount point and special device. */
	specp = argv[argc - 2];
	mntp = argv[argc - 1];

	/* Initialize default mount values */
	margs.cfs_options.opt_flags = CFS_ACCESS_BACKFS;
	margs.cfs_options.opt_popsize = DEF_POP_SIZE;
	margs.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE;
	margs.cfs_fsid = NULL;
	memset(margs.cfs_cacheid, 0, sizeof (margs.cfs_cacheid));
	margs.cfs_cachedir = CFS_DEF_DIR;
	margs.cfs_backfs = NULL;
	margs.cfs_acregmin = 0;
	margs.cfs_acregmax = 0;
	margs.cfs_acdirmin = 0;
	margs.cfs_acdirmax = 0;
	mflag = MS_OPTIONSTR;
	if (nomnttab)
		mflag |= MS_NOMNTTAB;
	backfstypep = NULL;

	/* process -o options */
	xx = set_cfs_args(optionp, &margs, &mflag, &backfstypep, &reducep,
	    &notify, &nfsv3pass);
	if (xx) {
		return (1);
	}
	strcpy(mops, optionp);

	/* backfstype has to be specified */
	if (backfstypep == NULL) {
		usage(gettext("\"-o backfstype\" must be specified"));
		return (1);
	}

	if ((strcmp(backfstypep, "nfs") != 0) &&
				(strcmp(backfstypep, "hsfs") != 0)) {
		pr_err(gettext("%s as backfstype is not supported."),
					backfstypep);
		return (1);
	}

	/* set default write mode if not specified */
	if ((margs.cfs_options.opt_flags &
	    (CFS_WRITE_AROUND|CFS_NONSHARED)) == 0) {
		margs.cfs_options.opt_flags |= CFS_WRITE_AROUND;
		if (strcmp(backfstypep, "hsfs") == 0)
			mflag |= MS_RDONLY;
	}

	/* if read-only was specified with the -r option */
	if (readonly) {
		mflag |= MS_RDONLY;
	}

	/* if overlay was specified with -O option */
	if (Oflg) {
		mflag |= MS_OVERLAY;
	}

	/* get the fsid of the backfs and the cacheid */
	margs.cfs_fsid = get_back_fsid(specp);
	if (margs.cfs_fsid == NULL) {
		pr_err(gettext("out of memory"));
		return (1);
	}

	/*
	 * If using this cachedir to mount a file system for the first time
	 * after reboot, the ncheck for the sanity of the cachedir
	 */
	if (first_time_ab(margs.cfs_cachedir))
		if (check_cache(margs.cfs_cachedir))
			return (1);

	/* get the front file system cache id if necessary */
	if (margs.cfs_cacheid[0] == '\0') {
		char *cacheid = get_cacheid(margs.cfs_fsid, mntp);

		if (cacheid == NULL) {
			pr_err(gettext("default cacheid too long"));
			return (1);
		}

		strcpy(margs.cfs_cacheid, cacheid);
	}

	/* lock the cache directory shared */
	lockid = cachefs_dir_lock(margs.cfs_cachedir, 1);
	if (lockid == -1) {
		/* exit if could not get the lock */
		return (1);
	}

	/* if no mount point was specified and we are not remounting */
	mounted = 0;
	if ((margs.cfs_backfs == NULL) &&
	    (((mflag & MS_REMOUNT) == 0) ||
	    (margs.cfs_options.opt_flags & CFS_SLIDE))) {
		/* if a disconnectable mount */
		xx = 0;
		if (margs.cfs_options.opt_flags & CFS_DISCONNECTABLE) {
			/* see if the server is alive */
			xx = pingserver(specp);
		}

		/* attempt to mount the back file system */
		if (xx == 0) {
			xx = dobackmnt(&margs, reducep, specp, backfstypep,
			    myname, readonly);
			/*
			 * nfs mount exits with a value of 32 if a timeout
			 * error occurs trying the mount.
			 */
			if (xx && (xx != 32)) {
				cachefs_dir_unlock(lockid);
				rmdir(margs.cfs_backfs);
				return (1);
			}
			if (xx == 0)
				mounted = 1;
		}
	}

	/*
	 * At this point the back file system should be mounted.
	 * Get NFS version information for the back filesystem if
	 * it is NFS. The version information is required
	 * because NFS version 4 is incompatible with cachefs
	 * and we provide pass-through support for NFS version 4
	 * with cachefs, aka the cachefs mount is installed but
	 * there is no caching. This is indicated to the kernel
	 * during the mount by setting the CFS_BACKFS_NFSV4 flag.
	 */
	if (margs.cfs_backfs != NULL && strcmp(backfstypep, "nfs") == 0) {

		nfsvers = cachefs_get_back_nfsvers(margs.cfs_backfs, nomnttab);
		switch (nfsvers) {
		case 2:
			break;

		case 3:
			if (nfsv3pass) {
				/* Force pass through (for debugging) */
				margs.cfs_options.opt_flags = CFS_BACKFS_NFSV4;
				if (cfs_nfsv4_build_opts(optionp,
						cfs_nfsv4ops) != 0) {
					nfsvers_error = TRUE;
					goto clean_backmnt;
				}
			}
			break;

		case 4:
			/*
			 * overwrite old option flags with NFSv4 flag.
			 * Note that will also operate in strict
			 * consistency mode. Clean up the option string
			 * to get rid of the cachefs-specific options
			 * to be in sync with the opt flags, otherwise
			 * these can make it into the mnttab and cause
			 * problems (esp. the disconnected option).
			 */
			margs.cfs_options.opt_flags = CFS_BACKFS_NFSV4;
			if (cfs_nfsv4_build_opts(optionp, cfs_nfsv4ops) != 0) {
				nfsvers_error = TRUE;
				goto clean_backmnt;
			}
			break;

		default:
			/* error, unknown version */
			nfsvers_error = TRUE;
			goto clean_backmnt;
		}
	}

	/*
	 * Grab server name from special file arg if it is there or set
	 * server name to "server unknown".
	 */
	margs.cfs_hostname = servname;
	strncpy(servname, specp, sizeof (servname));
	servname[sizeof (servname) - 1] = '\0';
	strp = strchr(servname, ':');
	if (strp == NULL) {
		margs.cfs_hostname = "server unknown";
		margs.cfs_backfsname = specp;
	} else {
		*strp = '\0';
		/*
		 * The rest of the special file arg is the name of
		 * the back filesystem.
		 */
		strp++;
		margs.cfs_backfsname = strp;
	}

	/* mount the cache file system */
	xx = mount((margs.cfs_backfs != NULL) ? margs.cfs_backfs : "nobackfs",
		mntp, mflag | MS_DATA, MNTTYPE_CFS,
		&margs, sizeof (margs),
		(cfs_nfsv4ops[0] == '\0' ? mops : cfs_nfsv4ops),
		MAX_MNTOPT_STR);
clean_backmnt:
	if (xx == -1 || nfsvers_error) {
		if (nfsvers_error) {
			pr_err(gettext("nfs version error."));
		} else if (errno == ESRCH) {
			pr_err(gettext("mount failed, options do not match."));
		} else if ((errno == EAGAIN) && (margs.cfs_backfs == NULL)) {
			pr_err(gettext("mount failed, server not responding."));
		} else {
			pr_err(gettext("mount failed %s"), strerror(errno));
		}

		/* try to unmount the back file system if we mounted it */
		if (mounted) {
			xx = 1;
			newargv[xx++] = "umount";
			newargv[xx++] = margs.cfs_backfs;
			newargv[xx++] = NULL;

			/* fork */
			if ((pid = fork()) == -1) {
				pr_err(gettext("could not fork: %s"),
				    strerror(errno));
				cachefs_dir_unlock(lockid);
				return (1);
			}

			/* if the child */
			if (pid == 0) {
				/* do the unmount */
				doexec(backfstypep, newargv, "umount");
			}

			/* else if the parent */
			else {
				wait(0);
			}
			rmdir(margs.cfs_backfs);
		}

		cachefs_dir_unlock(lockid);
		return (1);
	}

	/* release the lock on the cache directory */
	cachefs_dir_unlock(lockid);

	/* record the mount information in the fscache directory */
	record_mount(mntp, specp, margs.cfs_backfs, backfstypep,
		margs.cfs_cachedir, margs.cfs_cacheid,
		(cfs_nfsv4ops[0] == '\0' ? optionp : cfs_nfsv4ops), reducep);

	/* notify the daemon of the mount */
	if (notify)
		daemon_notify(margs.cfs_cachedir, margs.cfs_cacheid);

	/* update mnttab file if necessary */
	if (!nomnttab) {
		/*
		 * If we added the back file system, tag it with ignore,
		 * however, don't fail the mount after its done
		 * if the tag can't be added (eg., this would cause
		 * automounter problems).
		 */
		if (mounted) {
			FILE *mt;
			struct extmnttab mnt;

			if ((mt = fopen(MNTTAB, "r")) == NULL)
				return (1);
			while (getextmntent(mt, &mnt, sizeof (mnt)) != -1) {
				if (mnt.mnt_mountp != NULL &&
				    strcmp(margs.cfs_backfs,
					mnt.mnt_mountp) == 0) {
					/* found it, do tag ioctl */
					mtdesc.mtd_major = mnt.mnt_major;
					mtdesc.mtd_minor = mnt.mnt_minor;
					mtdesc.mtd_mntpt = margs.cfs_backfs;
					mtdesc.mtd_tag = MNTOPT_IGNORE;

					(void) ioctl(fileno(mt),
						MNTIOC_SETTAG, &mtdesc);
					break;
				}
			}
			fclose(mt);
		}
	}

	/* return success */
	return (0);
}


/*
 *
 *			usage
 *
 * Description:
 *	Prints a short usage message.
 * Arguments:
 *	msgp	message to include with the usage message
 * Returns:
 * Preconditions:
 */

void
usage(char *msgp)
{
	if (msgp) {
		pr_err(gettext("%s"), msgp);
	}

	fprintf(stderr,
	    gettext("Usage: mount -F cachefs [generic options] "
	    "-o backfstype=file_system_type[FSTypespecific_options] "
	    "special mount_point\n"));
}

/*
 *
 *			pr_err
 *
 * Description:
 *	Prints an error message to stderr.
 * Arguments:
 *	fmt	printf style format
 *	...	arguments for fmt
 * Returns:
 * Preconditions:
 *	precond(fmt)
 */

void
pr_err(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	(void) fprintf(stderr, gettext("mount -F cachefs: "));
	(void) vfprintf(stderr, fmt, ap);
	(void) fprintf(stderr, "\n");
	va_end(ap);
}

/*
 *
 *			set_cfs_args
 *
 * Description:
 *	Parse the comma delimited set of options specified by optionp
 *	and puts the results in margsp, mflagp, and backfstypepp.
 *	A string is constructed of options which are not specific to
 *	cfs and is placed in reducepp.
 *	Pointers to strings are invalid if this routine is called again.
 *	No initialization is done on margsp, mflagp, or backfstypepp.
 * Arguments:
 *	optionp		string of comma delimited options
 *	margsp		option results for the mount dataptr arg
 *	mflagp		option results for the mount mflag arg
 *	backfstypepp	set to name of back file system type
 *	reducepp	set to the option string without cfs specific options
 * Returns:
 *	Returns 0 for success, -1 for an error.
 * Preconditions:
 *	precond(optionp)
 *	precond(margsp)
 *	precond(mflagp)
 *	precond(backfstypepp)
 *	precond(reducepp)
 */

int
set_cfs_args(char *optionp, struct cachefs_mountargs *margsp, int *mflagp,
    char **backfstypepp, char **reducepp, int *notifyp, int *nfsv3pass)
{
	static char *optstrp = NULL;
	static char *reducep = NULL;
	char *savep, *strp, *valp;
	int badopt;
	int ret;
	int o_backpath = 0;
	int o_writemode = 0;
	int xx;
	uint_t yy;
	struct stat64 sinfo;
	char *pbuf;

	/* free up any previous options */
	free(optstrp);
	optstrp = NULL;
	free(reducep);
	reducep = NULL;

	/* make a copy of the options so we can modify it */
	optstrp = strp = strdup(optionp);
	reducep = malloc(strlen(optionp) + 1000);
	if ((strp == NULL) || (reducep == NULL)) {
		pr_err(gettext("out of memory"));
		return (-1);
	}
	*reducep = '\0';

	/* parse the options */
	badopt = 0;
	ret = 0;
	while (*strp) {
		savep = strp;
		switch (getsubopt(&strp, cfs_opts, &valp)) {

		case CFSOPT_BACKFSTYPE:
			if (valp == NULL)
				badopt = 1;
			else
				*backfstypepp = valp;
			break;

		case CFSOPT_CACHEDIR:
			if (valp == NULL)
				badopt = 1;
			else {
				margsp->cfs_cachedir = valp;
				if (valp[0] != '/') {
				    pbuf = (char *)malloc(MAXPATHLEN +
						strlen(valp) + 3);
				    if (pbuf == NULL) {
					pr_err(gettext("out of memory"));
					badopt = 1;
					break;
				    }
				    if (getcwd(pbuf, MAXPATHLEN+1) == NULL) {
					pr_err(gettext("cachedir too long"));
					badopt = 1;
					break;
				    }
				    if (pbuf[strlen(pbuf)-1] != '/')
					strcat(pbuf, "/");
				    strcat(pbuf, valp);
				    margsp->cfs_cachedir = pbuf;
				}
			}
			break;

		case CFSOPT_CACHEID:
			if (valp == NULL) {
				badopt = 1;
				break;
			}

			if (strlen(valp) >= (size_t)C_MAX_MOUNT_FSCDIRNAME) {
				pr_err(gettext("cacheid too long"));
				badopt = 1;
				break;
			}

			memset(margsp->cfs_cacheid, 0, C_MAX_MOUNT_FSCDIRNAME);
			strcpy(margsp->cfs_cacheid, valp);
			break;

		case CFSOPT_BACKPATH:
			if (valp == NULL)
				badopt = 1;
			else {
				margsp->cfs_backfs = valp;
				o_backpath = 1;
			}
			break;

		case CFSOPT_WRITEAROUND:
			margsp->cfs_options.opt_flags |= CFS_WRITE_AROUND;
			o_writemode++;
			break;

		case CFSOPT_NONSHARED:
			margsp->cfs_options.opt_flags |= CFS_NONSHARED;
			o_writemode++;
			break;

		case CFSOPT_NOCONST:
			margsp->cfs_options.opt_flags |= CFS_NOCONST_MODE;
			break;

		case CFSOPT_CODCONST:
			margsp->cfs_options.opt_flags |= CFS_CODCONST_MODE;
			break;

		case CFSOPT_LOCALACCESS:
			margsp->cfs_options.opt_flags &= ~CFS_ACCESS_BACKFS;
			break;

		case CFSOPT_NOSETSEC:
			margsp->cfs_options.opt_flags |= CFS_NOACL;
			break;

		case CFSOPT_LLOCK:
			margsp->cfs_options.opt_flags |= CFS_LLOCK;
			strcat(reducep, ",");
			strcat(reducep, savep);
			break;

		case CFSOPT_REMOUNT:
			*mflagp |= MS_REMOUNT;
			break;

		case CFSOPT_SLIDE:
			margsp->cfs_options.opt_flags |= CFS_SLIDE;
			break;

		case CFSOPT_FGSIZE:
			if (bad(valp))
				badopt = 1;
			else
				margsp->cfs_options.opt_fgsize = atoi(valp);
			break;

		case CFSOPT_POPSIZE:
			if (bad(valp))
				badopt = 1;
			else
				margsp->cfs_options.opt_popsize =
				    atoi(valp) * 1024;
			break;

		case CFSOPT_ACREGMIN:
			if (bad(valp))
				badopt = 1;
			else
				margsp->cfs_acregmin = atoi(valp);
			break;

		case CFSOPT_ACREGMAX:
			if (bad(valp))
				badopt = 1;
			else
				margsp->cfs_acregmax = atoi(valp);
			break;

		case CFSOPT_ACDIRMIN:
			if (bad(valp))
				badopt = 1;
			else
				margsp->cfs_acdirmin = atoi(valp);
			break;

		case CFSOPT_ACDIRMAX:
			if (bad(valp))
				badopt = 1;
			else
				margsp->cfs_acdirmax = atoi(valp);
			break;

		case CFSOPT_ACTIMEO:
			if (bad(valp))
				badopt = 1;
			else {
				yy = atoi(valp);
				margsp->cfs_acregmin = yy;
				margsp->cfs_acregmax = yy;
				margsp->cfs_acdirmin = yy;
				margsp->cfs_acdirmax = yy;
			}
			/*
			 * Note that we do not pass the actimeo options
			 * to the back file system.  This change was
			 * made for Chart.  Chart needs noac or actimeo=0
			 * so it makes no sense to pass these options on.
			 * In theory it should be okay to not pass these
			 * options on for regular cachefs mounts since
			 * cachefs perform the required attribute caching.
			 */
			break;

#if 0
		case CFSOPT_LAZYMOUNT:
			margsp->cfs_options.opt_flags |= CFS_LAZYMOUNT;
			break;
#endif

		case CFSOPT_DISCONNECTABLE:
		case CFSOPT_SNR:
			margsp->cfs_options.opt_flags |= CFS_DISCONNECTABLE;
			break;

		case CFSOPT_NOFILL:
			margsp->cfs_options.opt_flags |= CFS_NOFILL;
			break;

		case CFSOPT_SOFT:
			margsp->cfs_options.opt_flags |= CFS_SOFT;
			break;

		case CFSOPT_NONOTIFY:
			*notifyp = 0;
			break;

#ifdef CFS_NFSV3_PASSTHROUGH
		case CFSOPT_NFSV3PASSTHROUGH:
			*nfsv3pass = 1;
			break;
#endif /* CFS_NFSV3_PASSTHROUGH */

		default:
			/*
			 * unknown or vfs layer option, save for the back
			 * file system
			 */
			strcat(reducep, ",");
			strcat(reducep, savep);
			break;
		}

		/* if a lexical error occurred */
		if (badopt) {
			pr_err(gettext("invalid argument to option: \"%s\""),
			    savep);
			badopt = 0;
			ret = -1;
		}
	}

	/*
	 * Should mount backfs soft if disconnectable & non-shared options
	 * are used. NFS soft option allows reads and writes to TIMEOUT
	 * when the server is not responding, which is crucial for
	 * disconnectable option to work all the time in non-shared mode.
	 *
	 * Should mount backfs semisoft if disconnectable & write-around
	 * are used. NFS semisoft option allows reads to TIMEOUT and
	 * write to block when the server is not responding, which is
	 * good for write around option because it is shared.
	 *
	 * Since disconnectable and strict options are conflicting,
	 * when disconnectable option is used, default option is set to
	 * demandconst.
	 */

	if (margsp->cfs_options.opt_flags & (CFS_DISCONNECTABLE | CFS_SOFT))
		if (margsp->cfs_options.opt_flags & CFS_NONSHARED) {
			strcat(reducep, ",soft,noprint");
			margsp->cfs_options.opt_flags |= CFS_CODCONST_MODE;
		}
		else
			strcat(reducep, ",semisoft,noprint");

	if (!(margsp->cfs_options.opt_flags & CFS_DISCONNECTABLE)) {
		/* not snr, no need to notify the cachefsd */
		*notifyp = 0;
	}

	/* additional nfs options needed so disconnectable will work */
	if (margsp->cfs_options.opt_flags & CFS_DISCONNECTABLE) {
		/*
		 * retry=0 so cachefs can mount if nfs mount fails
		 *   even with this nfs takes 3 minutes to give up
		 * actimeo=0 because NFS does not pick up new ctime after
		 *	rename
		 */
		strcat(reducep, ",retry=0");
		if (margsp->cfs_options.opt_flags & CFS_NONSHARED)
			strcat(reducep, ",actimeo=0");
	}

	/* check for conflicting options */
	xx = margsp->cfs_options.opt_flags;
	if (o_backpath & (xx & CFS_DISCONNECTABLE)) {
		pr_err(gettext("backpath cannot be used with disconnectable"));
		ret = -1;
	}
	if (margsp->cfs_acregmin > margsp->cfs_acregmax) {
		pr_err(gettext("acregmin cannot be greater than acregmax"));
		ret = -1;
	}
	if (margsp->cfs_acdirmin > margsp->cfs_acdirmax) {
		pr_err(gettext("acdirmin cannot be greater than acdirmax"));
		ret = -1;
	}

	xx = CFS_NOCONST_MODE | CFS_CODCONST_MODE;
	if ((margsp->cfs_options.opt_flags & xx) == xx) {
		pr_err(gettext("only one of noconst and demandconst"
			" may be specified"));
		ret = -1;
	}

	if (o_writemode > 1) {
		pr_err(gettext(
		    "only one of write-around or non-shared"
		    " may be specified"));
		ret = -1;
	}

	/* if an error occured */
	if (ret)
		return (-1);

	/* if there are any options which are not mount specific */
	if (*reducep)
		*reducepp = reducep + 1;
	else
		*reducepp = NULL;

	/* return success */
	return (0);
}

/*
 *
 *			get_mount_point
 *
 * Description:
 *	Makes a suitable mount point for the back file system.
 *	The name of the mount point created is stored in a malloced
 *	buffer in pathpp
 * Arguments:
 *	cachedirp	the name of the cache directory
 *	specp		the special name of the device for the file system
 *	pathpp		where to store the mount point
 * Returns:
 *	Returns 0 for success, -1 for an error.
 * Preconditions:
 *	precond(cachedirp)
 *	precond(specp)
 *	precond(pathpp)
 */

int
get_mount_point(char *cachedirp, char *specp, char **pathpp)
{
	char *strp;
	char *namep;
	struct stat64 stat1, stat2;
	int xx;
	int index;
	int max;

	/* make a copy of the special device name */
	specp = strdup(specp);
	if (specp == NULL) {
		pr_err(gettext("out of memory"));
		return (-1);
	}

	/* convert the special device name into a file name */
	strp = specp;
	while (strp = strchr(strp, '/')) {
		*strp = '_';
	}

	/* get some space for the path name */
	strp = malloc(MAXPATHLEN);
	if (strp == NULL) {
		pr_err(gettext("out of memory"));
		return (-1);
	}

	/* see if the mount directory is valid */
	/* backfs can contain large files */
	sprintf(strp, "%s/%s", cachedirp, BACKMNT_NAME);
	xx = stat64(strp, &stat1);
	if ((xx == -1) || !S_ISDIR(stat1.st_mode)) {
		pr_err(gettext("%s is not a valid cache."), strp);
		return (-1);
	}

	/* find a directory name we can use */
	max = 10000;
	namep = strp + strlen(strp);
	for (index = 1; index < max; index++) {

		/* construct a directory name to consider */
		if (index == 1)
			sprintf(namep, "/%s", specp);
		else
			sprintf(namep, "/%s_%d", specp, index);

		/* try to create the directory */
		xx = mkdir(strp, 0755);
		if (xx == 0) {
			/* done if the create succeeded */
			break;
		}
	}

	/* if the search failed */
	if (index >= max) {
		pr_err(gettext("could not create a directory"));
		return (-1);
	}

	/* return success */
	*pathpp = strp;
	return (0);
}


int
dobackmnt(struct cachefs_mountargs *margsp, char *reducep, char *specp,
    char *backfstypep, char *mynamep, int readonly)
{
	int xx;
	pid_t pid;
	char *newargv[20];
	int stat_loc;

	/* get a suitable mount point */
	xx = get_mount_point(margsp->cfs_cachedir, specp, &margsp->cfs_backfs);
	if (xx)
		return (1);

	/* construct argument list for mounting the back file system */
	xx = 1;
	newargv[xx++] = "mount";
	if (readonly)
		newargv[xx++] = "-r";
	if (nomnttab)
		newargv[xx++] = "-m";
	if (quiet)
		newargv[xx++] = "-q";
	if (reducep) {
		newargv[xx++] = "-o";
		newargv[xx++] = reducep;
	}
	newargv[xx++] = specp;
	newargv[xx++] = margsp->cfs_backfs;
	newargv[xx++] = NULL;

	/* fork */
	if ((pid = fork()) == -1) {
		pr_err(gettext("could not fork %s"), strerror(errno));
		return (1);
	}

	/* if the child */
	if (pid == 0) {
		/* do the mount */
		doexec(backfstypep, newargv, mynamep);
	}

	/* else if the parent */
	else {
		/* wait for the child to exit */
		if (wait(&stat_loc) == -1) {
			pr_err(gettext("wait failed %s"), strerror(errno));
			return (1);
		}

		if (!WIFEXITED(stat_loc)) {
			pr_err(gettext("back mount did not exit"));
			return (1);
		}

		xx = WEXITSTATUS(stat_loc);
		if (xx) {
			pr_err(gettext("back mount failed"));
			return (xx);
		}
	}

	return (0);
}

/*
 *
 *			doexec
 *
 * Description:
 *	Execs the specified program with the specified command line arguments.
 *	This function never returns.
 * Arguments:
 *	fstype		type of file system
 *	newargv		command line arguments
 *	progp		name of program to exec
 * Returns:
 * Preconditions:
 *	precond(fstype)
 *	precond(newargv)
 */

void
doexec(char *fstype, char *newargv[], char *progp)
{
	char	full_path[PATH_MAX];
	char	alter_path[PATH_MAX];
	char	*vfs_path = VFS_PATH;
	char	*alt_path = ALT_PATH;

	/* build the full pathname of the fstype dependent command. */
	sprintf(full_path, "%s/%s/%s", vfs_path, fstype, progp);
	sprintf(alter_path, "%s/%s/%s", alt_path, fstype, progp);

	/* if the program exists */
	if (access(full_path, 0) == 0) {
		/* invoke the program */
		execv(full_path, &newargv[1]);

		/* if wrong permissions */
		if (errno == EACCES) {
			pr_err(gettext("cannot execute %s %s"),
			    full_path, strerror(errno));
		}

		/* if it did not work and the shell might make it */
		if (errno == ENOEXEC) {
			newargv[0] = "sh";
			newargv[1] = full_path;
			execv("/sbin/sh", &newargv[0]);
		}
	}

	/* try the alternate path */
	execv(alter_path, &newargv[1]);

	/* if wrong permissions */
	if (errno == EACCES) {
		pr_err(gettext("cannot execute %s %s"),
		    alter_path, strerror(errno));
	}

	/* if it did not work and the shell might make it */
	if (errno == ENOEXEC) {
		newargv[0] = "sh";
		newargv[1] = alter_path;
		execv("/sbin/sh", &newargv[0]);
	}

	pr_err(gettext("operation not applicable to FSType %s"), fstype);
	exit(1);
}

/*
 *
 *			get_back_fsid
 *
 * Description:
 *	Determines a unique identifier for the back file system.
 * Arguments:
 *	specp	the special file of the back fs
 * Returns:
 *	Returns a malloc string which is the unique identifer
 *	or NULL on failure.  NULL is only returned if malloc fails.
 * Preconditions:
 *	precond(specp)
 */

char *
get_back_fsid(char *specp)
{
	return (strdup(specp));
}

/*
 *
 *			get_cacheid
 *
 * Description:
 *	Determines an identifier for the front file system cache.
 *	The returned string points to a static buffer which is
 *	overwritten on each call.
 *	The length of the returned string is < C_MAX_MOUNT_FSCDIRNAME.
 * Arguments:
 *	fsidp	back file system id
 *	mntp	front file system mount point
 * Returns:
 *	Returns a pointer to the string identifier, or NULL if the
 *	identifier was overflowed.
 * Preconditions:
 *	precond(fsidp)
 *	precond(mntp)
 */

char *
get_cacheid(char *fsidp, char *mntp)
{
	char *c1;
	static char buf[PATH_MAX];
	char mnt_copy[PATH_MAX];

	/* strip off trailing space in mountpoint -- autofs fallout */
	if (strlen(mntp) >= sizeof (mnt_copy))
		return (NULL);
	(void) strcpy(mnt_copy, mntp);
	c1 = mnt_copy + strlen(mnt_copy) - 1;
	if (*c1 == ' ')
		*c1 = '\0';

	if ((strlen(fsidp) + strlen(mnt_copy) + 2) >=
	    (size_t)C_MAX_MOUNT_FSCDIRNAME)
		return (NULL);

	strcpy(buf, fsidp);
	strcat(buf, ":");
	strcat(buf, mnt_copy);
	c1 = buf;
	while ((c1 = strpbrk(c1, "/")) != NULL)
		*c1 = '_';
	return (buf);
}


/*
 *
 *			check_cache
 *
 * Description:
 *	Checks the cache we are about to use.
 * Arguments:
 *	cachedirp	cachedirectory to check
 * Returns:
 *	Returns 0 for success, -1 for an error.
 * Preconditions:
 */
int
check_cache(cachedirp)
	char *cachedirp;
{
	char *fsck_argv[4];
	int status = 0;
	pid_t pid;

	fsck_argv[1] = "fsck";
	fsck_argv[2] = cachedirp;
	fsck_argv[3] = NULL;

	/* fork */
	if ((pid = fork()) == -1) {
		pr_err(gettext("could not fork %s"),
		    strerror(errno));
		return (1);
	}

	if (pid == 0) {
		/* do the fsck */
		doexec("cachefs", fsck_argv, "fsck");
	} else {
		/* wait for the child to exit */
		if (wait(&status) == -1) {
			pr_err(gettext("wait failed %s"),
			    strerror(errno));
			return (1);
		}

		if (!WIFEXITED(status)) {
			pr_err(gettext("cache fsck did not exit"));
			return (1);
		}

		if (WEXITSTATUS(status) != 0) {
			pr_err(gettext("cache fsck mount failed"));
			return (1);
		}
	}
	return (0);
}

/*
 *
 *			record_mount
 *
 * Description:
 *	Records mount information in a file in the fscache directory.
 * Arguments:
 * Returns:
 * Preconditions:
 */

void
record_mount(char *mntp, char *specp, char *backfsp, char *backfstypep,
    char *cachedirp, char *cacheidp, char *optionp, char *reducep)
{
	char buf[MAXPATHLEN*2];
	FILE *fout;
	time_t tval;

	tval = time(NULL);

	/* this file is < 2GB */
	sprintf(buf, "%s/%s/%s", cachedirp, cacheidp, CACHEFS_MNT_FILE);
	fout = fopen(buf, "w");
	if (fout == NULL) {
		pr_err(gettext("could not open %s, %d"), buf, errno);
		return;
	}

	fprintf(fout, "cachedir: %s\n", cachedirp);
	fprintf(fout, "mnt_point: %s\n", mntp);
	if (specp) {
		fprintf(fout, "special: %s\n", specp);
	}
	if (backfsp)
		fprintf(fout, "backpath: %s\n", backfsp);
	fprintf(fout, "backfstype: %s\n", backfstypep);
	fprintf(fout, "cacheid: %s\n", cacheidp);
	fprintf(fout, "cachefs_options: %s\n", optionp);
	if (reducep)
		fprintf(fout, "backfs_options: %s\n", reducep);
	fprintf(fout, "mount_time: %u\n", tval);

	fclose(fout);
}

int
daemon_notify(char *cachedirp, char *cacheidp)
{
	CLIENT *clnt;
	enum clnt_stat retval;
	int ret;
	int xx;
	int result;
	char *hostp;
	struct utsname info;
	struct cachefsd_fs_mounted args;

	/* get the host name */
	xx = uname(&info);
	if (xx == -1) {
		pr_err(gettext("cannot get host name, errno %d"), errno);
		return (1);
	}
	hostp = info.nodename;

	/* creat the connection to the daemon */
	clnt = clnt_create(hostp, CACHEFSDPROG, CACHEFSDVERS, "local");
	if (clnt == NULL) {
		pr_err(gettext("cachefsd is not running"));
		return (1);
	}

	args.mt_cachedir = cachedirp;
	args.mt_cacheid = cacheidp;
	retval = cachefsd_fs_mounted_1(&args, NULL, clnt);
	if (retval != RPC_SUCCESS) {
		clnt_perror(clnt, gettext("cachefsd is not responding"));
		clnt_destroy(clnt);
		return (1);
	}

	ret = 0;

	clnt_destroy(clnt);

	return (ret);
}

/* returns 0 if the server is alive, -1 if an error */
int
pingserver(char *backmntp)
{
	CLIENT *clnt;
	static struct timeval TIMEOUT = { 25, 0 };
	enum clnt_stat retval;
	int ret;
	int xx;
	char *hostp;
	char buf[MAXPATHLEN];
	char *pc;

	/* get the host name */
	strcpy(buf, backmntp);
	pc = strchr(buf, ':');
	if (pc == NULL) {
		/* no host name, pretend it works */
		return (0);
	}
	*pc = '\0';
	hostp = buf;

	/* create the connection to the mount daemon */
	clnt = clnt_create(hostp, NFS_PROGRAM, NFS_VERSION, "udp");
	if (clnt == NULL) {
		return (-1);
	}

	ret = 0;

	/* see if the mountd responds */
	retval = clnt_call(clnt, 0, xdr_void, NULL, xdr_void, NULL,
	    TIMEOUT);
	if (retval != RPC_SUCCESS) {
		ret = -1;
	}

	clnt_destroy(clnt);

	return (ret);
}

/*
 * first_time_ab  : first time after boot - returns non-zero value
 *                  if the cachedir is being used for the first time
 *                  after the system reboot, otherwise zero.
 */
int
first_time_ab(char *buf)
{
	struct stat sinfo;
	char name[MAXPATHLEN];
	int ufd;
	time32_t btime;

	sprintf(name, "%s/%s", buf, CACHEFS_UNMNT_FILE);
	if (stat(name, &sinfo) != 0)
		return (1);
	if (sinfo.st_size == 0)
		return (1);
	if ((ufd = open(name, O_RDONLY)) == -1)
		return (1);
	if (read(ufd, &btime, sizeof (time32_t)) == -1)
		return (1);
	close(ufd);
	if (get_boottime() != btime)
		return (1);
	return (0);
}

/*
 * cachefs_get_back_nfsvers
 *
 * Returns:	nfs version
 *
 * Params:
 *		cfs_backfs	- backfile system mountpoint
 *		nomnttab	- mnttab entry does not exist
 *
 * Uses the kstat interface to extract the nfs version for
 * the mount.
 */
uint32_t
cachefs_get_back_nfsvers(char *cfs_backfs, int nomnttab)
{
	kstat_ctl_t *kc = NULL;
	FILE *mnttab = NULL;
	struct extmnttab mnt;
	kstat_t *ksp;
	dev_t my_fsid = NODEV;
	struct mntinfo_kstat mik;
	uint32_t nfsvers = 0;
	struct stat64 st;

	/*
	 * Initialize kernel statistics facility.
	 */
	if ((kc = kstat_open()) == NULL) {
		pr_err(gettext("kstat_open() can't open /dev/kstat: %s"),
			strerror(errno));
		goto end;
	}

	/*
	 * Locate the mount information in the mnttab if the nomnttab
	 * flag is not set, otherwise look for the entry by doing
	 * stat'ting the mountpoint.
	 */
	if (!nomnttab) {
		if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
			pr_err(gettext("can't open /etc/mnttab: %s"),
				strerror(errno));
			goto end;
		}

		while (getextmntent(mnttab, &mnt, sizeof (mnt)) != -1) {
			if (mnt.mnt_mountp == NULL ||
			    strcmp(cfs_backfs, mnt.mnt_mountp) != 0) {
				continue;
			}
			my_fsid = makedev(mnt.mnt_major, mnt.mnt_minor);
			break;
		}
	}

	if (my_fsid == NODEV) {
		if (stat64(cfs_backfs, &st) == -1) {
			pr_err(gettext("can't stat mountpoint: %s"),
				strerror(errno));
			goto end;
		} else {
			my_fsid = st.st_dev;
		}

	}

	/*
	 * Walk the kstat control structures to locate the
	 * structure that describes the nfs module/mntinfo
	 * statistics for the mounted backfilesystem.
	 */
	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {

		if (ksp->ks_type != KSTAT_TYPE_RAW)
			continue;
		if (strcmp(ksp->ks_module, "nfs") != 0)
			continue;
		if (strcmp(ksp->ks_name, "mntinfo") != 0)
			continue;
		if ((my_fsid & MAXMIN) != ksp->ks_instance)
			continue;

		/*
		 * At this point we have located the
		 * kstat info for the mount, read the
		 * statistics and return version info.
		 */
		if (kstat_read(kc, ksp, &mik) == -1) {
			pr_err(gettext("kstat_read() can't read %s/%s: %s"),
				ksp->ks_module, ksp->ks_name, strerror(errno));
			goto end;
		}

		nfsvers = mik.mik_vers;
		break;
	}

end:
	if (kc)
		kstat_close(kc);
	if (mnttab)
		fclose(mnttab);

	return (nfsvers);
}

/*
 * cfs_nfsv4_build_opts
 *
 * Returns: 0 on success, -1 on failure
 *
 * Params:
 *	optionp		- original option pointer
 *	cfs_nfsv4ops	- modified options for nfsv4 cachefs mount
 *
 * Parse the comma delimited set of options specified by optionp
 * and clean out options that we don't want to use with NFSv4.
 */
int
cfs_nfsv4_build_opts(char *optionp, char *cfs_nfsv4ops)
{
	char *optstrp;
	char *strp;
	char *savep;
	char *valp;
	uint32_t first = TRUE;

	/* Make a copy of the options so we can modify it */
	optstrp = strp = strdup(optionp);
	if (strp == NULL) {
		pr_err(gettext("out of memory"));
		return (-1);
	}

	/* Parse the options, cfs_nfsv4ops is initialized in main */
	while (*strp) {
		savep = strp;
		switch (getsubopt(&strp, cfs_opts, &valp)) {

		/* Ignore options that set cfs option flags */
		case CFSOPT_WRITEAROUND:
		case CFSOPT_NONSHARED:
		case CFSOPT_NOCONST:
		case CFSOPT_CODCONST:
		case CFSOPT_LOCALACCESS:
		case CFSOPT_NOSETSEC:
		case CFSOPT_LLOCK:
		case CFSOPT_SLIDE:
		case CFSOPT_DISCONNECTABLE:
		case CFSOPT_SNR:
		case CFSOPT_NOFILL:
		case CFSOPT_SOFT:
			break;

		default:
			/*
			 * Copy in option for cachefs nfsv4 mount.
			 */
			snprintf(cfs_nfsv4ops, MAX_MNTOPT_STR,
				"%s%s%s", cfs_nfsv4ops, first ? "" : ",",
				savep);
			first = FALSE;
			break;
		}
	}
	free(optstrp);

	return (0);
}