summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/cachefs/cachefs_fscache.c
blob: 6dcb5430f27158627fd3bda3b2ee177e5d6e5ff7 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/file.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/pathname.h>
#include <sys/uio.h>
#include <sys/tiuser.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/mount.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/utsname.h>
#include <sys/modctl.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/fbuf.h>
#include <rpc/types.h>

#include <vm/hat.h>
#include <vm/as.h>
#include <vm/page.h>
#include <vm/pvn.h>
#include <vm/seg.h>
#include <vm/seg_map.h>
#include <vm/seg_vn.h>
#include <vm/rm.h>
#include <sys/fs/cachefs_fs.h>
#include <sys/fs/cachefs_dlog.h>
#include <sys/fs/cachefs_ioctl.h>

/* external references */
extern struct cachefsops nopcfsops, strictcfsops, codcfsops;

/* forward references */
int fscdir_create(cachefscache_t *cachep, char *namep, fscache_t *fscp);
int fscdir_find(cachefscache_t *cachep, ino64_t fsid, fscache_t *fscp);
static int fscache_info_sync(fscache_t *fscp);

struct kmem_cache *cachefs_fscache_cache = NULL;

/*
 * ------------------------------------------------------------------
 *
 *		fscache_create
 *
 * Description:
 *	Creates a fscache object.
 * Arguments:
 *	cachep		cache to create fscache object for
 * Returns:
 *	Returns a fscache object.
 * Preconditions:
 *	precond(cachep)
 */

fscache_t *
fscache_create(cachefscache_t *cachep)
{
	fscache_t *fscp;

	/* create and initialize the fscache object */
	fscp = kmem_cache_alloc(cachefs_fscache_cache, KM_SLEEP);

	bzero(fscp, sizeof (*fscp));

	mutex_init(&fscp->fs_fslock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&fscp->fs_idlelock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&fscp->fs_dlock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&fscp->fs_cdlock, NULL, MUTEX_DEFAULT, NULL);
	cv_init(&fscp->fs_cdwaitcv, NULL, CV_DEFAULT, NULL);

	fscp->fs_cache = cachep;
	fscp->fs_info.fi_mntflags = CFS_WRITE_AROUND;
	fscp->fs_info.fi_popsize = DEF_POP_SIZE;
	fscp->fs_info.fi_fgsize = DEF_FILEGRP_SIZE;
	fscp->fs_cfsops = &nopcfsops;
	fscp->fs_consttype = CFS_FS_CONST_NOCONST;
	fscp->fs_acregmin = 30;
	fscp->fs_acregmax = 30;
	fscp->fs_acdirmin = 30;
	fscp->fs_acdirmax = 30;
	fscp->fs_cdconnected = CFS_CD_CONNECTED;
	fscp->fs_mntpt = NULL;
	fscp->fs_hostname = NULL;
	fscp->fs_backfsname = NULL;
	cachefs_workq_init(&fscp->fs_workq);
	return (fscp);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_destroy
 *
 * Description:
 *	Destroys the fscache object.
 * Arguments:
 *	fscp	the fscache object to destroy
 * Returns:
 * Preconditions:
 *	precond(fscp)
 *	precond(fs_ref == 0)
 */

void
fscache_destroy(fscache_t *fscp)
{
	size_t strl;

	ASSERT(fscp->fs_ref == 0);

	(void) fscache_info_sync(fscp);

	if (fscp->fs_mntpt) {
		strl = strlen(fscp->fs_mntpt);
		if (strl != 0)
			kmem_free(fscp->fs_mntpt, strl + 1);
	}
	if (fscp->fs_hostname) {
		strl = strlen(fscp->fs_hostname);
		if (strl != 0)
			kmem_free(fscp->fs_hostname, strl + 1);
	}
	if (fscp->fs_backfsname) {
		strl = strlen(fscp->fs_backfsname);
		if (strl != 0)
			kmem_free(fscp->fs_backfsname, strl + 1);
	}

	/* drop the inum translation table */
	if (fscp->fs_inum_size > 0)
		cachefs_kmem_free(fscp->fs_inum_trans,
		    fscp->fs_inum_size * sizeof (cachefs_inum_trans_t));

	/* drop references to the fscache directory */
	if (fscp->fs_fscdirvp)
		VN_RELE(fscp->fs_fscdirvp);
	if (fscp->fs_fsattrdir)
		VN_RELE(fscp->fs_fsattrdir);
	if (fscp->fs_infovp)
		VN_RELE(fscp->fs_infovp);

	/* drop logging references */
	cachefs_dlog_teardown(fscp);

	mutex_destroy(&fscp->fs_fslock);
	mutex_destroy(&fscp->fs_idlelock);
	mutex_destroy(&fscp->fs_dlock);
	mutex_destroy(&fscp->fs_cdlock);
	cv_destroy(&fscp->fs_cdwaitcv);

	kmem_cache_free(cachefs_fscache_cache, fscp);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_setup
 *
 * Description:
 *	Activates a fscache by associating the fscache object
 *	with on disk data.
 *	If the fscache directory of the specified fsid exists then
 *	it will be used.
 *	Otherwise a new fscache directory will be created using namep
 *	and optp with fsid being ignored.  However if namep or optp
 *	are not NULL or the cache is in NOFILL then this routine fails.
 * Arguments:
 *	fscp	the fscache object to activate
 *	fsid	unique identifier for the cache
 *	namep	name of the cache
 *	optp	options for the cache
 * Returns:
 *	Returns 0 for success, !0 on failure.
 * Preconditions:
 *	precond(fscp)
 *	precond(the cache must not be in NOCACHE mode)
 *	precond(the cache must not alread by active)
 */

static int
fscache_setup(fscache_t *fscp, ino64_t fsid, char *namep,
    struct cachefsoptions *optp, ino64_t backfileno, int setflags)
{
	int error;
	cachefscache_t *cachep = fscp->fs_cache;

	ASSERT((cachep->c_flags & CACHE_NOCACHE) == 0);

	/* see if the fscache directory already exists */
	error =	fscdir_find(cachep, fsid, fscp);
	if (error) {
		/* return error if cannot create the directory */
		if ((namep == NULL) || (optp == NULL) ||
		    (cachep->c_flags & CACHE_NOFILL)) {
			return (error);
		}
		if (backfileno == 0)
			return (EAGAIN);

		/* remember the root back fileno for disconnected mounts */
		fscp->fs_info.fi_root = backfileno;

		/* copy options into the fscache */
		fscp->fs_info.fi_mntflags = optp->opt_flags;
		fscp->fs_info.fi_popsize = optp->opt_popsize;
		fscp->fs_info.fi_fgsize = optp->opt_fgsize;
		fscp->fs_flags |= CFS_FS_DIRTYINFO;

		/* create the directory */
		error = fscdir_create(cachep, namep, fscp);
		if (error) {
			if (error == ENOSPC)
				cmn_err(CE_WARN,
				    "CacheFS: not enough space to create %s",
				    namep);
			else
				cmn_err(CE_WARN,
				    "CacheFS: error %d creating %s",
				    error, namep);
			return (error);
		}
	} else if (optp) {
		/* compare the options to make sure they are compatible */
		error = fscache_compare_options(fscp, optp);
		if (error) {
			cmn_err(CE_WARN,
				"CacheFS: mount failed, options do not match.");
			return (error);
		}

		/* copy options into the fscache */
		fscp->fs_info.fi_mntflags = optp->opt_flags;
		fscp->fs_info.fi_popsize = optp->opt_popsize;
		fscp->fs_info.fi_fgsize = optp->opt_fgsize;
		fscp->fs_flags |= CFS_FS_DIRTYINFO;

		/*
		 * The fileid of the root of the filesystem can change
		 * in NFSv4, so make sure we update the fi_root
		 * with the new filenumber.
		 */
		if (CFS_ISFS_BACKFS_NFSV4(fscp) &&
		    fscp->fs_info.fi_root != backfileno) {
			fscp->fs_info.fi_root = backfileno;
		}
	}

	if (setflags) {
		mutex_enter(&fscp->fs_fslock);
		fscp->fs_flags |= CFS_FS_READ;
		if ((cachep->c_flags & CACHE_NOFILL) == 0)
			fscp->fs_flags |= CFS_FS_WRITE;
		mutex_exit(&fscp->fs_fslock);
	}

	return (0);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_activate
 *
 * Description:
 *	A wrapper routine for fscache_setup, telling it to setup the
 *	fscache for general use.
 *
 */
int
fscache_activate(fscache_t *fscp, ino64_t fsid, char *namep,
    struct cachefsoptions *optp, ino64_t backfileno)
{
	return (fscache_setup(fscp, fsid, namep, optp, backfileno, 1));
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_enable
 *
 * Description:
 *	A wrapper routine for fscache_setup, telling it to create a
 *	fscache that can be used during remount.  In this case the
 *	fscache flags that allow general use are not yet turned on.
 *	A later call to fscache_activate_rw will set the flags.
 *
 */
int
fscache_enable(fscache_t *fscp, ino64_t fsid, char *namep,
    struct cachefsoptions *optp, ino64_t backfileno)
{
	return (fscache_setup(fscp, fsid, namep, optp, backfileno, 0));
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_activate_rw
 *
 * Description:
 *	Makes the fscache both readable and writable.
 * Arguments:
 *	fscp		fscache object
 * Returns:
 * Preconditions:
 *	precond(fscp)
 */

void
fscache_activate_rw(fscache_t *fscp)
{
	mutex_enter(&fscp->fs_fslock);
	fscp->fs_flags |= (CFS_FS_WRITE|CFS_FS_READ);
	mutex_exit(&fscp->fs_fslock);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_hold
 *
 * Description:
 *	Increments the reference count on the fscache object
 * Arguments:
 *	fscp		fscache object to incriment reference count on
 * Returns:
 * Preconditions:
 *	precond(fscp)
 */

void
fscache_hold(fscache_t *fscp)
{
	mutex_enter(&fscp->fs_fslock);
	fscp->fs_ref++;
	ASSERT(fscp->fs_ref > 0);
	mutex_exit(&fscp->fs_fslock);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_rele
 *
 * Description:
 *	Decriments the reference count on the fscache object
 * Arguments:
 *	fscp		fscache object to decriment reference count on
 * Returns:
 * Preconditions:
 *	precond(fscp)
 */

void
fscache_rele(fscache_t *fscp)
{
	mutex_enter(&fscp->fs_fslock);
	ASSERT(fscp->fs_ref > 0);
	fscp->fs_ref--;
	mutex_exit(&fscp->fs_fslock);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_cnodecnt
 *
 * Description:
 *	Changes the count of number of cnodes on this fscache
 *	by the specified amount.
 * Arguments:
 *	fscp		fscache object to to modify count on
 *	cnt		amount to adjust by
 * Returns:
 *	Returns new count of number of cnodes.
 * Preconditions:
 *	precond(fscp)
 */

int
fscache_cnodecnt(fscache_t *fscp, int cnt)
{
	int xx;

	mutex_enter(&fscp->fs_fslock);
	fscp->fs_cnodecnt += cnt;
	ASSERT(fscp->fs_cnodecnt >= 0);
	xx = fscp->fs_cnodecnt;
	mutex_exit(&fscp->fs_fslock);
	return (xx);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_mounted
 *
 * Description:
 *	Called to indicate the the fscache is mounted.
 * Arguments:
 *	fscp		fscache object
 *	cfsvfsp		cachefs vfsp
 *	backvfsp	vfsp of back file system
 * Returns:
 *	Returns 0 for success, -1 if the cache is already mounted.
 * Preconditions:
 *	precond(fscp)
 */

int
fscache_mounted(fscache_t *fscp, struct vfs *cfsvfsp, struct vfs *backvfsp)
{
	int error = 0;

	mutex_enter(&fscp->fs_fslock);
	if (fscp->fs_flags & CFS_FS_MOUNTED) {
		error = -1;
		goto out;
	}

	fscp->fs_backvfsp = backvfsp;
	fscp->fs_cfsvfsp = cfsvfsp;
	gethrestime(&fscp->fs_cod_time);
	fscp->fs_flags |= CFS_FS_MOUNTED;

	if (CFS_ISFS_SNR(fscp)) {
		/*
		 * If there is a dlog file present, then we assume the cache
		 * was left in disconnected mode.
		 * Also if the back file system was not mounted we also
		 * start off in disconnected mode.
		 */
		error = cachefs_dlog_setup(fscp, 0);
		if (!error || (backvfsp == NULL)) {
			mutex_enter(&fscp->fs_cdlock);
			fscp->fs_cdconnected = CFS_CD_DISCONNECTED;
			fscp->fs_cdtransition = 0;
			cv_broadcast(&fscp->fs_cdwaitcv);
			mutex_exit(&fscp->fs_cdlock);
		}

		/* invalidate any local fileno mappings */
		fscp->fs_info.fi_resetfileno++;
		fscp->fs_flags |= CFS_FS_DIRTYINFO;

		/* if connected, invalidate any local time mappings */
		if (backvfsp)
			fscp->fs_info.fi_resettimes++;
	}

		error = 0;

	/* set up the consistency mode */
	if (fscp->fs_info.fi_mntflags & CFS_NOCONST_MODE) {
		fscp->fs_cfsops = &nopcfsops;
		fscp->fs_consttype = CFS_FS_CONST_NOCONST;
	} else if (fscp->fs_info.fi_mntflags & CFS_CODCONST_MODE) {
		fscp->fs_cfsops = &codcfsops;
		fscp->fs_consttype = CFS_FS_CONST_CODCONST;
	} else {
		fscp->fs_cfsops = &strictcfsops;
		fscp->fs_consttype = CFS_FS_CONST_STRICT;
	}

out:
	mutex_exit(&fscp->fs_fslock);
	(void) fscache_info_sync(fscp);
	return (error);
}

/*
 * Compares fscache state with new mount options
 * to make sure compatible.
 * Returns ESRCH if not compatible or 0 for success.
 */
int
fscache_compare_options(fscache_t *fscp, struct cachefsoptions *optp)
{
	if ((fscp->fs_info.fi_popsize == optp->opt_popsize) &&
	    (fscp->fs_info.fi_fgsize == optp->opt_fgsize)) {
		return (0);
	} else {
		return (ESRCH);
	}
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_sync
 *
 * Description:
 *	Syncs any data for this fscache to the front file system.
 * Arguments:
 *	fscp	fscache to sync
 * Returns:
 * Preconditions:
 *	precond(fscp)
 */

void
fscache_sync(struct fscache *fscp)
{
	struct filegrp *fgp;
	int xx;

	(void) fscache_info_sync(fscp);

	/* sync the cnodes */
	cachefs_cnode_traverse(fscp, cachefs_cnode_sync);

	mutex_enter(&fscp->fs_fslock);

	/* sync the attrcache files */
	for (xx = 0; xx < CFS_FS_FGP_BUCKET_SIZE; xx++) {
		for (fgp = fscp->fs_filegrp[xx]; fgp != NULL;
			fgp = fgp->fg_next) {
			(void) filegrp_sync(fgp);
		}
	}

	/* garbage collect any unused file groups */
	filegrp_list_gc(fscp);

	mutex_exit(&fscp->fs_fslock);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_acset
 *
 * Description:
 *	Sets the ac timeout values for the fscache.
 * Arguments:
 *	fscp	fscache object
 * Returns:
 * Preconditions:
 *	precond(fscp)
 */

void
fscache_acset(fscache_t *fscp,
	uint_t acregmin, uint_t acregmax, uint_t acdirmin, uint_t acdirmax)
{
	mutex_enter(&fscp->fs_fslock);
	if (acregmin > acregmax)
		acregmin = acregmax;
	if (acdirmin > acdirmax)
		acdirmin = acdirmax;
	if (acregmin != 0)
		fscp->fs_acregmin = acregmin;
	if (acregmax != 0)
		fscp->fs_acregmax = acregmax;
	if (acdirmin != 0)
		fscp->fs_acdirmin = acdirmin;
	if (acdirmax != 0)
		fscp->fs_acdirmax = acdirmax;
	mutex_exit(&fscp->fs_fslock);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_list_find
 *
 * Description:
 *	Finds the desired fscache structure on a cache's
 *	file system list.
 * Arguments:
 *	cachep	holds the list of fscache objects to search
 *	fsid	the numeric identifier of the fscache
 * Returns:
 *	Returns an fscache object on success or NULL on failure.
 * Preconditions:
 *	precond(cachep)
 *	precond(the fslistlock must be held)
 */

fscache_t *
fscache_list_find(cachefscache_t *cachep, ino64_t fsid)
{
	fscache_t *fscp = cachep->c_fslist;

	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));

	while (fscp != NULL) {
		if (fscp->fs_cfsid == fsid) {
			ASSERT(fscp->fs_cache == cachep);
			break;
		}
		fscp = fscp->fs_next;
	}

	return (fscp);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_list_add
 *
 * Description:
 *	Adds the specified fscache object to the list on
 *	the specified cachep.
 * Arguments:
 *	cachep	holds the list of fscache objects
 *	fscp	fscache object to add to list
 * Returns:
 * Preconditions:
 *	precond(cachep)
 *	precond(fscp)
 *	precond(fscp cannot already be on a list)
 *	precond(the fslistlock must be held)
 */

void
fscache_list_add(cachefscache_t *cachep, fscache_t *fscp)
{
	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));

	fscp->fs_next = cachep->c_fslist;
	cachep->c_fslist = fscp;
	cachep->c_refcnt++;
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_list_remove
 *
 * Description:
 *	Removes the specified fscache object from the list
 *	on the specified cachep.
 * Arguments:
 *	cachep	holds the list of fscache objects
 *	fscp	fscache object to remove from list
 * Returns:
 * Preconditions:
 *	precond(cachep)
 *	precond(fscp)
 *	precond(the fslistlock must be held)
 */

void
fscache_list_remove(cachefscache_t *cachep, fscache_t *fscp)
{
	struct fscache **pfscp = &cachep->c_fslist;

	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));

	while (*pfscp != NULL) {
		if (fscp == *pfscp) {
			*pfscp = fscp->fs_next;
			cachep->c_refcnt--;
			break;
		}
		pfscp = &(*pfscp)->fs_next;
	}
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_list_gc
 *
 * Description:
 *	Traverses the list of fscache objects on the cachep
 *	list and destroys any that are not mounted and
 *	that are not referenced.
 * Arguments:
 *	cachep	holds the list of fscache objects
 * Returns:
 * Preconditions:
 *	precond(cachep)
 *	precond(the fslistlock must be held)
 */

void
fscache_list_gc(cachefscache_t *cachep)
{
	struct fscache *next, *fscp;

	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));

	for (fscp = cachep->c_fslist; fscp != NULL; fscp = next) {
		next = fscp->fs_next;
		mutex_enter(&fscp->fs_fslock);
		if (((fscp->fs_flags & CFS_FS_MOUNTED) == 0) &&
		    (fscp->fs_ref == 0)) {
			mutex_exit(&fscp->fs_fslock);
			fscache_list_remove(cachep, fscp);
			fscache_destroy(fscp);
		} else {
			mutex_exit(&fscp->fs_fslock);
		}
	}
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_list_mounted
 *
 * Description:
 *	Returns the number of fscache objects that are mounted.
 * Arguments:
 *	cachep	holds the list of fscache objects
 * Returns:
 * Preconditions:
 *	precond(cachep)
 *	precond(the fslistlock must be held)
 */

int
fscache_list_mounted(cachefscache_t *cachep)
{
	struct fscache *fscp;
	int count;

	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));

	count = 0;
	for (fscp = cachep->c_fslist; fscp != NULL; fscp = fscp->fs_next) {
		mutex_enter(&fscp->fs_fslock);
		if (fscp->fs_flags & CFS_FS_MOUNTED)
			count++;
		mutex_exit(&fscp->fs_fslock);
	}

	return (count);
}

/*
 * Creates the fs cache directory.
 * The directory name is the ascii version of the fsid.
 * Also makes a symlink to the directory using the specified name.
 */
int
fscdir_create(cachefscache_t *cachep, char *namep, fscache_t *fscp)
{
	int error;
	vnode_t *fscdirvp = NULL;
	vnode_t *infovp = NULL;
	vnode_t *attrvp = NULL;
	struct vattr *attrp = (struct vattr *)NULL;
	char name[CFS_FRONTFILE_NAME_SIZE];
	int files;
	int blocks = 0;
	cfs_cid_t cid;
	ino64_t fsid;

	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));
	ASSERT(fscp->fs_infovp == NULL);
	ASSERT(fscp->fs_fscdirvp == NULL);
	ASSERT(fscp->fs_fsattrdir == NULL);

	/* directory, symlink and options file + attrcache dir */
	files = 0;
	while (files < 4) {
		error = cachefs_allocfile(cachep);
		if (error)
			goto out;
		files++;
	}
	error = cachefs_allocblocks(cachep, 4, CACHEFS_RL_NONE);
	if (error)
		goto out;
	blocks = 4;

	attrp = cachefs_kmem_alloc(sizeof (struct vattr), KM_SLEEP);
	attrp->va_mode = S_IFDIR | 0777;
	attrp->va_uid = 0;
	attrp->va_gid = 0;
	attrp->va_type = VDIR;
	attrp->va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
	error = VOP_MKDIR(cachep->c_dirvp, namep, attrp, &fscdirvp, kcred,
	    NULL, 0, NULL);
	if (error) {
		cmn_err(CE_WARN, "Can't create fs cache directory");
		goto out;
	}

	/*
	 * Created the directory. Get the fileno. That'll be the cachefs_fsid.
	 */
	attrp->va_mask = AT_NODEID;
	error = VOP_GETATTR(fscdirvp, attrp, 0, kcred, NULL);
	if (error) {
		goto out;
	}
	fsid = attrp->va_nodeid;
	attrp->va_mode = S_IFREG | 0666;
	attrp->va_uid = 0;
	attrp->va_gid = 0;
	attrp->va_type = VREG;
	attrp->va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
	error = VOP_CREATE(fscdirvp, CACHEFS_FSINFO, attrp, EXCL,
			0600, &infovp, kcred, 0, NULL, NULL);
	if (error) {
		cmn_err(CE_WARN, "Can't create fs option file");
		goto out;
	}
	attrp->va_size = MAXBSIZE;
	attrp->va_mask = AT_SIZE;
	error = VOP_SETATTR(infovp, attrp, 0, kcred, NULL);
	if (error) {
		cmn_err(CE_WARN, "Can't set size of fsinfo file");
		goto out;
	}

	/* write out the info file */
	fscp->fs_flags |= CFS_FS_DIRTYINFO;
	error = fscache_info_sync(fscp);
	if (error)
		goto out;

	/*
	 * Install the symlink from cachefs_fsid -> directory.
	 */
	cid.cid_flags = 0;
	cid.cid_fileno = fsid;
	make_ascii_name(&cid, name);
	error = VOP_RENAME(cachep->c_dirvp, namep, cachep->c_dirvp,
		name, kcred, NULL, 0);
	if (error) {
		cmn_err(CE_WARN, "Can't rename cache directory");
		goto out;
	}
	attrp->va_mask = AT_MODE | AT_TYPE;
	attrp->va_mode = 0777;
	attrp->va_type = VLNK;
	error = VOP_SYMLINK(cachep->c_dirvp, namep, attrp, name, kcred, NULL,
	    0);
	if (error) {
		cmn_err(CE_WARN, "Can't create cache directory symlink");
		goto out;
	}

	/*
	 * Finally, make the attrcache directory
	 */
	attrp->va_mode = S_IFDIR | 0777;
	attrp->va_uid = 0;
	attrp->va_gid = 0;
	attrp->va_type = VDIR;
	attrp->va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
	error = VOP_MKDIR(fscdirvp, ATTRCACHE_NAME, attrp, &attrvp, kcred, NULL,
	    0, NULL);
	if (error) {
		cmn_err(CE_WARN, "Can't create attrcache dir for fscache");
		goto out;
	}

	mutex_enter(&fscp->fs_fslock);
	fscp->fs_cfsid = fsid;
	fscp->fs_fscdirvp = fscdirvp;
	fscp->fs_fsattrdir = attrvp;
	fscp->fs_infovp = infovp;
	mutex_exit(&fscp->fs_fslock);

out:

	if (error) {
		while (files-- > 0)
			cachefs_freefile(cachep);
		if (fscdirvp)
			VN_RELE(fscdirvp);
		if (blocks)
			cachefs_freeblocks(cachep, blocks, CACHEFS_RL_NONE);
		if (attrvp)
			VN_RELE(attrvp);
		if (infovp)
			VN_RELE(infovp);
	}
	if (attrp)
		cachefs_kmem_free(attrp, sizeof (struct vattr));
	return (error);
}

/*
 * Tries to find the fscache directory indicated by fsid.
 */
int
fscdir_find(cachefscache_t *cachep, ino64_t fsid, fscache_t *fscp)
{
	int error;
	vnode_t *infovp = NULL;
	vnode_t *fscdirvp = NULL;
	vnode_t *attrvp = NULL;
	char dirname[CFS_FRONTFILE_NAME_SIZE];
	cfs_cid_t cid;
	cachefs_fsinfo_t fsinfo;
	caddr_t addr;

	ASSERT(MUTEX_HELD(&cachep->c_fslistlock));
	ASSERT(fscp->fs_infovp == NULL);
	ASSERT(fscp->fs_fscdirvp == NULL);
	ASSERT(fscp->fs_fsattrdir == NULL);

	/* convert the fsid value to the name of the directory */
	cid.cid_flags = 0;
	cid.cid_fileno = fsid;
	make_ascii_name(&cid, dirname);

	/* try to find the directory */
	error = VOP_LOOKUP(cachep->c_dirvp, dirname, &fscdirvp, NULL,
			0, NULL, kcred, NULL, NULL, NULL);
	if (error)
		goto out;

	/* this better be a directory or we are hosed */
	if (fscdirvp->v_type != VDIR) {
		cmn_err(CE_WARN, "cachefs: fscdir_find_a: cache corruption"
			" run fsck, %s", dirname);
		error = ENOTDIR;
		goto out;
	}

	/* try to find the info file */
	error = VOP_LOOKUP(fscdirvp, CACHEFS_FSINFO, &infovp,
	    NULL, 0, NULL, kcred, NULL, NULL, NULL);
	if (error) {
		cmn_err(CE_WARN, "cachefs: fscdir_find_b: cache corruption"
			" run fsck, %s", dirname);
		goto out;
	}

	/* read in info struct */
	addr = segmap_getmapflt(segkmap, infovp, (offset_t)0,
				MAXBSIZE, 1, S_READ);

	/*LINTED alignment okay*/
	fsinfo = *(cachefs_fsinfo_t *)addr;
	error =  segmap_release(segkmap, addr, 0);
	if (error) {
		cmn_err(CE_WARN, "cachefs: fscdir_find_c: cache corruption"
			" run fsck, %s", dirname);
		goto out;
	}

	/* try to find the attrcache directory */
	error = VOP_LOOKUP(fscdirvp, ATTRCACHE_NAME,
	    &attrvp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
	if (error) {
		cmn_err(CE_WARN, "cachefs: fscdir_find_d: cache corruption"
			" run fsck, %s", dirname);
		goto out;
	}

	mutex_enter(&fscp->fs_fslock);
	fscp->fs_info = fsinfo;
	fscp->fs_cfsid = fsid;
	fscp->fs_fscdirvp = fscdirvp;
	fscp->fs_fsattrdir = attrvp;
	fscp->fs_infovp = infovp;
	mutex_exit(&fscp->fs_fslock);

out:
	if (error) {
		if (infovp)
			VN_RELE(infovp);
		if (fscdirvp)
			VN_RELE(fscdirvp);
	}
	return (error);
}

/*
 * fscache_info_sync
 * Writes out the fs_info data if necessary.
 */
static int
fscache_info_sync(fscache_t *fscp)
{
	caddr_t addr;
	int error = 0;

	mutex_enter(&fscp->fs_fslock);

	if (fscp->fs_cache->c_flags & CACHE_NOFILL) {
		error = EROFS;
		goto out;
	}

	/* if the data is dirty and we have the file vnode */
	if ((fscp->fs_flags & CFS_FS_DIRTYINFO) && fscp->fs_infovp) {
		addr = segmap_getmapflt(segkmap, fscp->fs_infovp, 0,
					MAXBSIZE, 1, S_WRITE);

		/*LINTED alignment okay*/
		*(cachefs_fsinfo_t *)addr = fscp->fs_info;
		error = segmap_release(segkmap, addr, SM_WRITE);

		if (error) {
			cmn_err(CE_WARN,
			    "cachefs: Can not write to info file.");
		} else {
			fscp->fs_flags &= ~CFS_FS_DIRTYINFO;
		}
	}

out:

	mutex_exit(&fscp->fs_fslock);

	return (error);
}

/*
 * ------------------------------------------------------------------
 *
 *		fscache_name_to_fsid
 *
 * Description:
 *	Takes the name of a cache and determines it corresponding
 *	fsid.
 * Arguments:
 *	cachep	cache object to find name of fs cache in
 *	namep	the name of the fs cache
 *	fsidp	set to the fsid if found
 * Returns:
 *	Returns 0 on success, !0 on error.
 * Preconditions:
 *	precond(cachep)
 *	precond(namep)
 *	precond(fsidp)
 */

int
fscache_name_to_fsid(cachefscache_t *cachep, char *namep, ino64_t *fsidp)
{
	int error;
	char dirname[CFS_FRONTFILE_NAME_SIZE];
	vnode_t *linkvp = NULL;
	struct uio uio;
	struct iovec iov;
	ino64_t nodeid;
	char *pd;
	int xx;
	int c;

	/* get the vnode of the name */
	error = VOP_LOOKUP(cachep->c_dirvp, namep, &linkvp, NULL, 0, NULL,
		kcred, NULL, NULL, NULL);
	if (error)
		goto out;

	/* the vnode had better be a link */
	if (linkvp->v_type != VLNK) {
		error = EINVAL;
		goto out;
	}

	/* read the contents of the link */
	iov.iov_len = CFS_FRONTFILE_NAME_SIZE;
	iov.iov_base = dirname;
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_resid = iov.iov_len;
	uio.uio_segflg = UIO_SYSSPACE;
	uio.uio_loffset = 0;
	uio.uio_fmode = 0;
	uio.uio_extflg = UIO_COPY_CACHED;
	error = VOP_READLINK(linkvp, &uio, kcred, NULL);
	if (error) {
		cmn_err(CE_WARN, "cachefs: Can't read filesystem cache link");
		goto out;
	}

	/* convert the contents of the link to a ino64_t */
	nodeid = 0;
	pd = dirname;
	for (xx = 0; xx < (CFS_FRONTFILE_NAME_SIZE - 2); xx++) {
		nodeid <<= 4;
		c = *pd++;
		if (c <= '9')
			c -= '0';
		else if (c <= 'F')
			c = c - 'A' + 10;
		else
			c = c - 'a' + 10;
		nodeid += c;
	}
	*fsidp = nodeid;
out:
	if (linkvp)
		VN_RELE(linkvp);

	return (error);
}


/*
 * Suspends the thread until access to the cache is granted.
 * If !SOFT then
 *	waitconnected == 1 means wait until connected
 *	waitconnected == 0 means wait until connected or disconnected
 * else then
 *	wait until connected or disconnected
 * writing is set to 1 if writing, 0 if reading
 * Returns 0, EINTR, or ETIMEDOUT.
 */
int
cachefs_cd_access(fscache_t *fscp, int waitconnected, int writing)
{
	int nosig;
	int error = 0;
	cachefscache_t *cachep;
	int waithappens = 0;
	pid_t pid;

	mutex_enter(&fscp->fs_cdlock);

#ifdef CFS_CD_DEBUG
	ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif

	for (;;) {
		/* if we have to wait */
		if (waithappens ||
		    (waitconnected &&
		    (fscp->fs_cdconnected != CFS_CD_CONNECTED))) {

			/* do not make soft mounts wait until connected */
			if ((waithappens == 0) && CFS_ISFS_SOFT(fscp)) {
				error = ETIMEDOUT;
				break;
			}

			/* wait for a wakeup or a signal */
			nosig = cv_wait_sig(&fscp->fs_cdwaitcv,
			    &fscp->fs_cdlock);

			/* if we got a signal */
			if (nosig == 0) {
				error = EINTR;
				break;
			}

			if (waitconnected &&
			    (fscp->fs_cdconnected == CFS_CD_CONNECTED))
				waitconnected = 0;

			/* try again to get access */
			waithappens = 0;
			continue;
		}

		/* if transitioning modes */
		if (fscp->fs_cdtransition) {
			waithappens = 1;
			continue;
		}

		/* if rolling the log */
		if (fscp->fs_cdconnected == CFS_CD_RECONNECTING) {
			pid = ttoproc(curthread)->p_pid;
			cachep = fscp->fs_cache;

			/* if writing or not the cachefsd */
			if (writing ||
			    ((fscp->fs_cddaemonid != pid) &&
			    (cachep->c_rootdaemonid != pid))) {
				waithappens = 1;
				continue;
			}
		}

		/* if the daemon is not running */
		if (fscp->fs_cddaemonid == 0) {
			/* if writing and not connected */
			if (writing &&
			    (fscp->fs_cdconnected != CFS_CD_CONNECTED)) {
				waithappens = 1;
				continue;
			}
		}

		/*
		 * Verify don't set wait for NFSv4 (doesn't support
		 * disconnected behavior).
		 */
		ASSERT(!CFS_ISFS_BACKFS_NFSV4(fscp) ||
				(waithappens == 0 && waitconnected == 0));

		ASSERT(fscp->fs_cdrefcnt >= 0);
		fscp->fs_cdrefcnt++;
#ifdef CFS_CD_DEBUG
		curthread->t_flag |= T_CD_HELD;
#endif
		break;
	}
	mutex_exit(&fscp->fs_cdlock);

	return (error);
}

/*
 * Call to check if can have access after a cache miss has occurred.
 * Only read access is allowed, do not call this routine if want
 * to write.
 * Returns 1 if yes, 0 if no.
 */
int
cachefs_cd_access_miss(fscache_t *fscp)
{
	cachefscache_t *cachep;
	pid_t pid;

#ifdef CFS_CD_DEBUG
	ASSERT(curthread->t_flag & T_CD_HELD);
#endif

	/* should not get called if connected */
	ASSERT(fscp->fs_cdconnected != CFS_CD_CONNECTED);

	/* if no back file system, then no */
	if (fscp->fs_backvfsp == NULL)
		return (0);

	/* if daemon is not running, then yes */
	if (fscp->fs_cddaemonid == 0) {
		return (1);
	}

	pid = ttoproc(curthread)->p_pid;
	cachep = fscp->fs_cache;

	/* if daemon is running, only daemon is allowed to have access */
	if ((fscp->fs_cddaemonid != pid) &&
	    (cachep->c_rootdaemonid != pid)) {
		return (0);
	}

	return (1);
}

/*
 * Releases an access to the file system.
 */
void
cachefs_cd_release(fscache_t *fscp)
{
	mutex_enter(&fscp->fs_cdlock);

#ifdef CFS_CD_DEBUG
	ASSERT(curthread->t_flag & T_CD_HELD);
	curthread->t_flag &= ~T_CD_HELD;
#endif
	/* decriment hold on file system */
	fscp->fs_cdrefcnt--;
	ASSERT(fscp->fs_cdrefcnt >= 0);

	/* Verify no connected state transitions for NFSv4 */
	ASSERT(!CFS_ISFS_BACKFS_NFSV4(fscp) || fscp->fs_cdtransition == 0);

	/* wake up cachefsd */
	if ((fscp->fs_cdrefcnt == 0) && fscp->fs_cdtransition)
		cv_broadcast(&fscp->fs_cdwaitcv);

	mutex_exit(&fscp->fs_cdlock);
}

/*
 * Called when a network timeout error has occurred.
 * If connected, switches state to disconnected.
 */
void
cachefs_cd_timedout(fscache_t *fscp)
{
	int state;

	/* nothing to do if not snr or not connected */
	if (!CFS_ISFS_SNR(fscp) || (fscp->fs_cdconnected != CFS_CD_CONNECTED))
		return;

#ifdef CFS_CD_DEBUG
	ASSERT((curthread->t_flag & T_CD_HELD) == 0);
#endif

	/* Verify no state changes done for NFSv4 */
	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);

	state = CFS_FS_DISCONNECTED;
	(void) cachefs_io_stateset(fscp->fs_rootvp, &state, NULL);
}