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
|
/*
* 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.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Common subroutines used by the programs in these subdirectories.
*/
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <wait.h>
#include <ctype.h>
#include <fcntl.h>
#include <ftw.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/time.h>
#include <utmpx.h>
#include <sys/uio.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/mman.h>
#include <sys/fs/cachefs_fs.h>
#include "subr.h"
/*
*
* cachefs_dir_lock
*
* Description:
* Gets a lock on the cache directory.
* To release the lock, call cachefs_dir_unlock
* with the returned value.
* Arguments:
* cachedirp name of the cache directory
* shared 1 if shared, 0 if not
* Returns:
* Returns -1 if the lock cannot be obtained immediatly.
* If the lock is obtained, returns a value >= 0.
* Preconditions:
* precond(cachedirp)
*/
int
cachefs_dir_lock(const char *cachedirp, int shared)
{
int fd;
int xx;
int len;
char buf[MAXPATHLEN];
struct flock fl;
char *strp;
struct stat statb;
/* make a path prefix to the cache directory lock file */
strp = CACHEFS_ROOTRUN;
xx = stat(strp, &statb);
if ((xx < 0) || ((statb.st_mode & S_IFMT) != S_IFDIR))
strp = "/tmp";
/* won't overflow */
len = snprintf(buf, sizeof (buf), "%s/%s", strp, CACHEFS_LOCKDIR_PRE);
if (strlcat(buf, cachedirp, sizeof (buf)) >= sizeof (buf)) {
pr_err(gettext("Cache directory name %s is too long"),
cachedirp);
return (-1);
}
strp = &buf[len];
while (strp = strchr(strp, '/')) { /* convert path to a file */
*strp = '_';
}
/*
* Create and open the cache directory lock file.
* This file will be <2G.
*/
fd = open(buf, O_RDWR | O_CREAT, 0700);
if (fd == -1) {
pr_err(gettext("Cannot open lock file %s"), buf);
return (-1);
}
/* try to set the lock */
fl.l_type = (shared == 1) ? F_RDLCK : F_WRLCK;
fl.l_whence = 0;
fl.l_start = 1024;
fl.l_len = 1024;
fl.l_sysid = 0;
fl.l_pid = 0;
/* CACHEFS_LOCK_FILE will be <2GB */
xx = fcntl(fd, F_SETLKW, &fl);
if (xx == -1) {
if (errno == EAGAIN) {
pr_err(gettext("Cannot gain access to the "
"cache directory %s."), cachedirp);
} else {
pr_err(gettext("Unexpected failure on lock file %s %s"),
buf, strerror(errno));
}
close(fd);
return (-1);
}
/* return the file descriptor which can be used to release the lock */
return (fd);
}
/*
*
* cachefs_dir_unlock
*
* Description:
* Releases an advisory lock on the cache directory.
* Arguments:
* fd cookie returned by cachefs_dir_lock
* Returns:
* Returns -1 if the lock cannot be released or 0 for success.
* Preconditions:
*/
int
cachefs_dir_unlock(int fd)
{
struct flock fl;
int error = 0;
int xx;
/* release the lock */
fl.l_type = F_UNLCK;
fl.l_whence = 0;
fl.l_start = 1024;
fl.l_len = 1024;
fl.l_sysid = 0;
fl.l_pid = 0;
/* fd will be <2GB */
xx = fcntl(fd, F_SETLK, &fl);
if (xx == -1) {
pr_err(gettext("Unexpected failure releasing lock file %s"),
strerror(errno));
error = -1;
}
/* close the lock file */
close(fd);
return (error);
}
/*
*
* cachefs_label_file_get
*
* Description:
* Gets the contents of a cache label file.
* Performs error checking on the file.
* Arguments:
* filep name of the cache label file
* clabelp where to put the file contents
* Returns:
* Returns 0 for success or -1 if an error occurs.
* Preconditions:
* precond(filep)
* precond(clabelp)
*/
int
cachefs_label_file_get(const char *filep, struct cache_label *clabelp)
{
int xx;
int fd;
struct stat64 statinfo;
/* get info on the file */
xx = lstat64(filep, &statinfo);
if (xx == -1) {
if (errno != ENOENT) {
pr_err(gettext("Cannot stat file %s: %s"),
filep, strerror(errno));
} else {
pr_err(gettext("File %s does not exist."), filep);
}
return (-1);
}
/* if the file is the wrong type */
if (!S_ISREG(statinfo.st_mode)) {
pr_err(gettext("Cache label file %s corrupted"), filep);
return (-1);
}
/* if the file is the wrong size; it will be <2GB */
if (statinfo.st_size != (offset_t)sizeof (struct cache_label)) {
pr_err(gettext("Cache label file %s wrong size"), filep);
return (-1);
}
/* open the cache label file */
fd = open(filep, O_RDONLY);
if (fd == -1) {
pr_err(gettext("Error opening %s: %s"), filep,
strerror(errno));
return (-1);
}
/* read the current set of parameters */
xx = read(fd, clabelp, sizeof (struct cache_label));
if (xx != sizeof (struct cache_label)) {
pr_err(gettext("Reading %s failed: %s\n"), filep,
strerror(errno));
close(fd);
return (-1);
}
close(fd);
/* return success */
return (0);
}
/*
*
* cachefs_label_file_put
*
* Description:
* Outputs the contents of a cache label object to a file.
* Arguments:
* filep name of the cache label file
* clabelp where to get the file contents
* Returns:
* Returns 0 for success or -1 if an error occurs.
* Preconditions:
* precond(filep)
* precond(clabelp)
*/
int
cachefs_label_file_put(const char *filep, struct cache_label *clabelp)
{
int xx;
int fd;
/* get rid of the file if it already exists */
xx = unlink(filep);
if ((xx == -1) && (errno != ENOENT)) {
pr_err(gettext("Could not remove %s: %s"), filep,
strerror(errno));
return (-1);
}
/* open the cache label file; this file will be <2GB */
fd = open(filep, O_CREAT | O_RDWR, 0600);
if (fd == -1) {
pr_err(gettext("Error creating %s: %s"), filep,
strerror(errno));
return (-1);
}
/* write out the cache label object */
xx = write(fd, clabelp, sizeof (struct cache_label));
if (xx != sizeof (struct cache_label)) {
pr_err(gettext("Writing %s failed: %s"), filep,
strerror(errno));
close(fd);
return (-1);
}
/* make sure the contents get to disk */
if (fsync(fd) != 0) {
pr_err(gettext("Writing %s failed on sync: %s"), filep,
strerror(errno));
close(fd);
return (-1);
}
close(fd);
/* return success */
return (0);
}
int
cachefs_label_file_vcheck(char *filep, struct cache_label *clabelp)
{
/* check for an invalid version number */
if (clabelp->cl_cfsversion != CFSVERSION) {
pr_err(gettext("Cache label file %s corrupted"), filep);
return (-1);
}
return (0);
}
/*
*
* cachefs_inuse
*
* Description:
* Tests whether or not the cache directory is in use by
* the cache file system.
* Arguments:
* cachedirp name of the file system cache directory
* Returns:
* Returns 1 if the cache is in use or an error, 0 if not.
* Preconditions:
* precond(cachedirp)
*/
int
cachefs_inuse(const char *cachedirp)
{
int fd;
int xx;
char buf[MAXPATHLEN];
char *lockp = CACHEFS_LOCK_FILE;
struct flock fl;
/* see if path name is too long */
xx = strlen(cachedirp) + strlen(lockp) + 3;
if (xx >= MAXPATHLEN) {
pr_err(gettext("Cache directory name %s is too long"),
cachedirp);
return (1);
}
/* make a path to the cache directory lock file */
snprintf(buf, sizeof (buf), "%s/%s", cachedirp, lockp);
/* Open the kernel in use lock file. This file will be <2GB. */
fd = open(buf, O_RDWR, 0700);
if (fd == -1) {
pr_err(gettext("Cannot open lock file %s"), buf);
return (1);
}
/* test the lock status */
fl.l_type = F_WRLCK;
fl.l_whence = 0;
fl.l_start = 0;
fl.l_len = 1024;
fl.l_sysid = 0;
fl.l_pid = 0;
xx = fcntl(fd, F_GETLK, &fl);
if (xx == -1) {
pr_err(gettext("Unexpected failure on lock file %s %s"),
buf, strerror(errno));
close(fd);
return (1);
}
close(fd);
if (fl.l_type == F_UNLCK)
xx = 0;
else
xx = 1;
/* return whether or not the cache is in use */
return (xx);
}
/*
*
* cachefs_resouce_size
*
* Description:
* Returns information about a resource file.
* Arguments:
* maxinodes number of inodes to be managed by the resource file
* rinfop set to info about the resource file
* Returns:
* Preconditions:
* precond(rinfop)
*/
void
cachefs_resource_size(int maxinodes, struct cachefs_rinfo *rinfop)
{
int fsize;
fsize = MAXBSIZE;
rinfop->r_ptroffset = fsize;
fsize += MAXBSIZE * (maxinodes / CACHEFS_RLPMBS);
if ((maxinodes % CACHEFS_RLPMBS) != 0)
fsize += MAXBSIZE;
rinfop->r_fsize = fsize;
}
/*
*
* cachefs_create_cache
*
* Description:
* Creates the specified cache directory and populates it as
* needed by CFS.
* Arguments:
* dirp the name of the cache directory
* uv user values (may be NULL)
* clabel label file contents, or placeholder for this
* Returns:
* Returns 0 for success or:
* -1 for an error
* -2 for an error and cache directory partially created
* Preconditions:
* precond(dirp)
*/
int
cachefs_create_cache(char *dirp, struct cachefs_user_values *uv,
struct cache_label *clabel)
{
int xx;
char path[CACHEFS_XMAXPATH];
int fd;
void *bufp;
int cnt;
struct cache_usage cu;
FILE *fp;
char *parent;
struct statvfs64 svfs;
cu.cu_blksused = 0;
cu.cu_filesused = 0;
cu.cu_flags = 0;
/* make sure cache dir name is not too long */
if (strlen(dirp) > (size_t)PATH_MAX) {
pr_err(gettext("path name %s is too long."), dirp);
return (-1);
}
/* ensure the path isn't in cachefs */
parent = cachefs_file_to_dir(dirp);
if (parent == NULL) {
pr_err(gettext("Out of memory"));
return (-1);
}
if (statvfs64(parent, &svfs) != 0) {
pr_err(gettext("%s: %s"), parent, strerror(errno));
free(parent);
return (-1);
}
if (strcmp(svfs.f_basetype, CACHEFS_BASETYPE) == 0) {
pr_err(gettext("Cannot create cache in cachefs filesystem"));
free(parent);
return (-1);
}
free(parent);
/* make the directory */
if (mkdir(dirp, 0) == -1) {
switch (errno) {
case EEXIST:
pr_err(gettext("%s already exists."), dirp);
break;
default:
pr_err(gettext("mkdir %s failed: %s"),
dirp, strerror(errno));
}
return (-1);
}
cu.cu_filesused += 1;
cu.cu_blksused += 1;
/* convert user values to a cache_label */
if (uv != NULL) {
xx = cachefs_convert_uv2cl(uv, clabel, dirp);
if (xx)
return (-2);
}
/*
* Create the cache directory lock file.
* Used by the kernel module to indicate the cache is in use.
* This file will be <2G.
*/
snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOCK_FILE);
fd = open(path, O_RDWR | O_CREAT, 0700);
if (fd == -1) {
pr_err(gettext("Cannot create lock file %s"), path);
return (-1);
}
close(fd);
/* make the directory for the back file system mount points */
/* note: we do not count this directory in the resources */
snprintf(path, sizeof (path), "%s/%s", dirp, BACKMNT_NAME);
if (mkdir(path, 0700) == -1) {
pr_err(gettext("mkdir %s failed: %s"), path,
strerror(errno));
return (-2);
}
/* make the directory for lost+found */
/* note: we do not count this directory in the resources */
snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOSTFOUND_NAME);
if (mkdir(path, 0700) == -1) {
pr_err(gettext("mkdir %s failed: %s"), path,
strerror(errno));
return (-2);
}
/* make the networker "don't back up" file; this file is <2GB */
xx = 0;
snprintf(path, sizeof (path), "%s/%s", dirp, NOBACKUP_NAME);
if ((fp = fopen(path, "w")) != NULL) {
if (realpath(dirp, path) != NULL) {
fprintf(fp, "<< ./ >>\n");
fprintf(fp, "+skip: .?* *\n");
if (fclose(fp) == 0)
xx = 1;
}
}
if (xx == 0) {
snprintf(path, sizeof (path), "%s/%s", dirp, NOBACKUP_NAME);
pr_err(gettext("can't create %s"), path);
(void) unlink(path);
} else {
cu.cu_filesused += 1;
cu.cu_blksused += 1;
}
/* create the unmount file */
xx = 0;
snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_UNMNT_FILE);
if ((fp = fopen(path, "w")) != NULL) {
time32_t btime;
btime = get_boottime();
fwrite((void *)&btime, sizeof (btime), 1, fp);
if (fclose(fp) == 0)
xx = 1;
}
if (xx == 0)
pr_err(gettext("can't create .cfs_unmnt file"));
/* create the cache label file */
snprintf(path, sizeof (path), "%s/%s", dirp, CACHELABEL_NAME);
xx = cachefs_label_file_put(path, clabel);
if (xx == -1) {
pr_err(gettext("creating %s failed."), path);
return (-2);
}
cu.cu_filesused += 1;
cu.cu_blksused += 1;
/* create the cache label duplicate file */
snprintf(path, sizeof (path), "%s/%s.dup", dirp, CACHELABEL_NAME);
xx = cachefs_label_file_put(path, clabel);
if (xx == -1) {
pr_err(gettext("creating %s failed."), path);
return (-2);
}
cu.cu_filesused += 1;
cu.cu_blksused += 1;
/* create the resouce file; this file will be <2GB */
snprintf(path, sizeof (path), "%s/%s", dirp, RESOURCE_NAME);
fd = open(path, O_CREAT | O_RDWR, 0600);
if (fd == -1) {
pr_err(gettext("create %s failed: %s"), path,
strerror(errno));
return (-2);
}
cu.cu_filesused += 1;
/* allocate a zeroed buffer for filling the resouce file */
bufp = calloc(1, MAXBSIZE);
if (bufp == NULL) {
pr_err(gettext("out of space %d."), MAXBSIZE);
close(fd);
return (-2);
}
/* determine number of MAXBSIZE chunks to make the file */
cnt = 1; /* for the header */
cnt += clabel->cl_maxinodes / CACHEFS_RLPMBS;
if ((clabel->cl_maxinodes % CACHEFS_RLPMBS) != 0)
++cnt;
/* fill up the file with zeros */
for (xx = 0; xx < cnt; xx++) {
if (write(fd, bufp, MAXBSIZE) != MAXBSIZE) {
pr_err(gettext("write %s failed: %s"), path,
strerror(errno));
close(fd);
free(bufp);
return (-2);
}
}
free(bufp);
cu.cu_blksused += cnt;
/* position to the begining of the file */
if (lseek(fd, 0, SEEK_SET) == -1) {
pr_err(gettext("lseek %s failed: %s"), path,
strerror(errno));
close(fd);
return (-2);
}
/* write the cache usage structure */
xx = sizeof (struct cache_usage);
if (write(fd, &cu, xx) != xx) {
pr_err(gettext("cu write %s failed: %s"), path,
strerror(errno));
close(fd);
return (-2);
}
/* make sure the contents get to disk */
if (fsync(fd) != 0) {
pr_err(gettext("fsync %s failed: %s"), path,
strerror(errno));
close(fd);
return (-2);
}
close(fd);
/* return success */
return (0);
}
/*
*
* cachefs_delete_all_cache
*
* Description:
* Delete all caches in cache directory.
* Arguments:
* dirp the pathname of of the cache directory to delete
* Returns:
* Returns 0 for success or -1 for an error.
* Preconditions:
* precond(dirp)
*/
int
cachefs_delete_all_cache(char *dirp)
{
DIR *dp;
struct dirent64 *dep;
int xx;
char path[CACHEFS_XMAXPATH];
struct stat64 statinfo;
/* make sure cache dir name is not too long */
if (strlen(dirp) > (size_t)PATH_MAX) {
pr_err(gettext("path name %s is too long."),
dirp);
return (-1);
}
/* check that dirp is probably a cachefs directory */
snprintf(path, sizeof (path), "%s/%s", dirp, BACKMNT_NAME);
xx = access(path, R_OK | W_OK | X_OK);
snprintf(path, sizeof (path), "%s/%s", dirp, CACHELABEL_NAME);
xx |= access(path, R_OK | W_OK);
if (xx) {
pr_err(gettext("%s does not appear to be a "
"cachefs cache directory."), dirp);
return (-1);
}
/* remove the lost+found directory if it exists and is empty */
snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOSTFOUND_NAME);
xx = rmdir(path);
if (xx == -1) {
if (errno == EEXIST) {
pr_err(gettext("Cannot delete cache '%s'. "
"First move files in '%s' to a safe location."),
dirp, path);
return (-1);
} else if (errno != ENOENT) {
pr_err(gettext("rmdir %s failed: %s"), path,
strerror(errno));
return (-1);
}
}
/* delete the back FS mount point directory if it exists */
snprintf(path, sizeof (path), "%s/%s", dirp, BACKMNT_NAME);
xx = lstat64(path, &statinfo);
if (xx == -1) {
if (errno != ENOENT) {
pr_err(gettext("lstat %s failed: %s"), path,
strerror(errno));
return (-1);
}
} else {
xx = nftw64(path, cachefs_delete_file, 16,
FTW_PHYS | FTW_DEPTH | FTW_MOUNT);
if (xx == -1) {
pr_err(gettext("unable to delete %s"), path);
return (-1);
}
}
/* open the cache directory specified */
if ((dp = opendir(dirp)) == NULL) {
pr_err(gettext("cannot open cache directory %s: %s"),
dirp, strerror(errno));
return (-1);
}
/* read the file names in the cache directory */
while ((dep = readdir64(dp)) != NULL) {
/* ignore . and .. */
if (strcmp(dep->d_name, ".") == 0 ||
strcmp(dep->d_name, "..") == 0)
continue;
/* stat the file */
snprintf(path, sizeof (path), "%s/%s", dirp, dep->d_name);
xx = lstat64(path, &statinfo);
if (xx == -1) {
if (errno == ENOENT) {
/* delete_cache may have nuked a directory */
continue;
}
pr_err(gettext("lstat %s failed: %s"),
path, strerror(errno));
closedir(dp);
return (-1);
}
/* ignore anything that is not a link */
if (!S_ISLNK(statinfo.st_mode))
continue;
/* delete the file system cache directory */
xx = cachefs_delete_cache(dirp, dep->d_name);
if (xx) {
closedir(dp);
return (-1);
}
}
closedir(dp);
/* remove the cache dir unmount file */
snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_UNMNT_FILE);
xx = unlink(path);
if ((xx == -1) && (errno != ENOENT)) {
pr_err(gettext("unlink %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* remove the cache label file */
snprintf(path, sizeof (path), "%s/%s", dirp, CACHELABEL_NAME);
xx = unlink(path);
if ((xx == -1) && (errno != ENOENT)) {
pr_err(gettext("unlink %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* remove the cache label duplicate file */
snprintf(path, sizeof (path), "%s/%s.dup", dirp, CACHELABEL_NAME);
xx = unlink(path);
if ((xx == -1) && (errno != ENOENT)) {
pr_err(gettext("unlink %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* remove the resource file */
snprintf(path, sizeof (path), "%s/%s", dirp, RESOURCE_NAME);
xx = unlink(path);
if ((xx == -1) && (errno != ENOENT)) {
pr_err(gettext("unlink %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* remove the cachefslog file if it exists */
snprintf(path, sizeof (path), "%s/%s", dirp, LOG_STATUS_NAME);
(void) unlink(path);
/* remove the networker "don't back up" file if it exists */
snprintf(path, sizeof (path), "%s/%s", dirp, NOBACKUP_NAME);
(void) unlink(path);
/* remove the lock file */
snprintf(path, sizeof (path), "%s/%s", dirp, CACHEFS_LOCK_FILE);
xx = unlink(path);
if ((xx == -1) && (errno != ENOENT)) {
pr_err(gettext("unlink %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* remove the directory */
xx = rmdir(dirp);
if (xx == -1) {
pr_err(gettext("rmdir %s failed: %s"), dirp,
strerror(errno));
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_delete_cache
*
* Description:
* Deletes the specified file system cache.
* Arguments:
* dirp cache directory name
* namep file system cache directory to delete
* Returns:
* Returns 0 for success, -1 for failure.
* Preconditions:
* precond(dirp)
* precond(namep)
*/
int
cachefs_delete_cache(char *dirp, char *namep)
{
char path[CACHEFS_XMAXPATH];
char buf[CACHEFS_XMAXPATH];
int xx;
struct stat64 statinfo;
/* make sure cache dir name is not too long */
if (strlen(dirp) > (size_t)PATH_MAX) {
pr_err(gettext("path name %s is too long."),
dirp);
return (-1);
}
/* construct the path name of the file system cache directory */
snprintf(path, sizeof (path), "%s/%s", dirp, namep);
/* stat the specified file */
xx = lstat64(path, &statinfo);
if (xx == -1) {
pr_err(gettext("lstat %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* make sure name is a symbolic link */
if (!S_ISLNK(statinfo.st_mode)) {
pr_err(gettext("\"%s\" is not a valid cache id."), namep);
return (-1);
}
/* read the contents of the symbolic link */
xx = readlink(path, buf, sizeof (buf));
if (xx == -1) {
pr_err(gettext("Readlink of %s failed: %s"), path,
strerror(errno));
return (-1);
}
buf[xx] = '\0';
/* remove the directory */
snprintf(path, sizeof (path), "%s/%s", dirp, buf);
xx = nftw64(path, cachefs_delete_file, 16,
FTW_PHYS | FTW_DEPTH | FTW_MOUNT);
if (xx == -1) {
pr_err(gettext("directory walk of %s failed."), dirp);
return (-1);
}
/* delete the link */
snprintf(path, sizeof (path), "%s/%s", dirp, namep);
if (unlink(path) == -1) {
pr_err(gettext("unlink %s failed: %s"), path,
strerror(errno));
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_delete_file
*
* Description:
* Remove a file or directory; called by nftw64().
* Arguments:
* namep pathname of the file
* statp stat info about the file
* flg info about file
* ftwp depth information
* Returns:
* Returns 0 for success, -1 for failure.
* Preconditions:
* precond(namep)
*/
int
cachefs_delete_file(const char *namep, const struct stat64 *statp, int flg,
struct FTW *ftwp)
{
/* ignore . and .. */
if (strcmp(namep, ".") == 0 || strcmp(namep, "..") == 0)
return (0);
switch (flg) {
case FTW_F: /* files */
case FTW_SL:
if (unlink(namep) == -1) {
pr_err(gettext("unlink %s failed: %s"),
namep, strerror(errno));
return (-1);
}
break;
case FTW_DP: /* directories that have their children processed */
if (rmdir(namep) == -1) {
pr_err(gettext("rmdir %s failed: %s"),
namep, strerror(errno));
return (-1);
}
break;
case FTW_D: /* ignore directories if children not processed */
break;
default:
pr_err(gettext("failure on file %s, flg %d."),
namep, flg);
return (-1);
}
/* return success */
return (0);
}
/*
*
* cachefs_convert_uv2cl
*
* Description:
* Copies the contents of a cachefs_user_values object into a
* cache_label object, performing the necessary conversions.
* Arguments:
* uvp cachefs_user_values to copy from
* clp cache_label to copy into
* dirp cache directory
* Returns:
* Returns 0 for success, -1 for an error.
* Preconditions:
* precond(uvp)
* precond(clp)
* precond(dirp)
*/
int
cachefs_convert_uv2cl(const struct cachefs_user_values *uvp,
struct cache_label *clp, const char *dirp)
{
struct statvfs64 fs;
int xx;
double ftmp;
double temp;
/* get file system information */
xx = statvfs64(dirp, &fs);
if (xx == -1) {
pr_err(gettext("statvfs %s failed: %s"), dirp,
strerror(errno));
return (-1);
}
ftmp = (double)fs.f_frsize / (double)MAXBSIZE;
/* front fs is less than 1 terabyte */
temp = (double)uvp->uv_maxblocks / 100.0 *
(double)fs.f_blocks * ftmp + .5;
clp->cl_maxblks = temp < (double)INT_MAX ? (int)temp : INT_MAX;
temp = (double)uvp->uv_minblocks / 100.0 *
(double)fs.f_blocks * ftmp + .5;
clp->cl_blockmin = temp < (double)INT_MAX ? (int)temp : INT_MAX;
temp = (double)uvp->uv_threshblocks / 100.0 *
(double)fs.f_blocks * ftmp + .5;
clp->cl_blocktresh = temp < (double)INT_MAX ? (int)temp : INT_MAX;
temp = (double)uvp->uv_maxfiles / 100.0 * (double)fs.f_files + .5;
clp->cl_maxinodes = temp < (double)INT_MAX ? (int)temp : INT_MAX;
temp = (double)uvp->uv_minfiles / 100.0 * (double)fs.f_files + .5;
clp->cl_filemin = temp < (double)INT_MAX ? (int)temp : INT_MAX;
temp = (double)uvp->uv_threshfiles / 100.0 * (double)fs.f_files +.5;
clp->cl_filetresh = temp < (double)INT_MAX ? (int)temp : INT_MAX;
ftmp = (double)(1024 * 1024) / (double)MAXBSIZE;
clp->cl_maxfiles = uvp->uv_maxfilesize * ftmp + .5;
clp->cl_blkhiwat = uvp->uv_hiblocks / 100.0 * clp->cl_maxblks + .5;
clp->cl_blklowat = uvp->uv_lowblocks / 100.0 * clp->cl_maxblks + .5;
clp->cl_filehiwat = uvp->uv_hifiles / 100.0 * clp->cl_maxinodes + .5;
clp->cl_filelowat = uvp->uv_lowfiles / 100.0 * clp->cl_maxinodes + .5;
clp->cl_cfsversion = CFSVERSION;
/* return success */
return (0);
}
/*
*
* cachefs_convert_cl2uv
*
* Description:
* Copies the contents of a cache_label object into a
* cachefs_user_values object, performing the necessary conversions.
* Arguments:
* clp cache_label to copy from
* uvp cachefs_user_values to copy into
* dirp cache directory
* Returns:
* Returns 0 for success, -1 for an error.
* Preconditions:
* precond(clp)
* precond(uvp)
* precond(dirp)
*/
int
cachefs_convert_cl2uv(const struct cache_label *clp,
struct cachefs_user_values *uvp, const char *dirp)
{
struct statvfs64 fs;
int xx;
double temp;
double ftmp;
long long ltmp;
/* get file system information */
xx = statvfs64(dirp, &fs);
if (xx == -1) {
pr_err(gettext("statvfs %s failed: %s"), dirp,
strerror(errno));
return (-1);
}
#define BOUND(yy) \
yy = (yy < 0) ? 0 : yy; \
yy = (yy > 100) ? 100 : yy;
ftmp = (double)MAXBSIZE / (double)fs.f_frsize;
temp = (double)clp->cl_maxblks * ftmp /
(double)fs.f_blocks * 100. + .5;
BOUND(temp);
uvp->uv_maxblocks = (int)temp;
temp = (double)clp->cl_blockmin * ftmp /
(double)fs.f_blocks * 100. + .5;
BOUND(temp);
uvp->uv_minblocks = (int)temp;
temp = (double)clp->cl_blocktresh * ftmp /
(double)fs.f_blocks * 100. + .5;
BOUND(temp);
uvp->uv_threshblocks = (int)temp;
temp = ((double)clp->cl_maxinodes / fs.f_files) * 100. + .5;
BOUND(temp);
uvp->uv_maxfiles = (int)temp;
temp = ((double)clp->cl_filemin / fs.f_files) * 100. + .5;
BOUND(temp);
uvp->uv_minfiles = (int)temp;
temp = ((double)clp->cl_filetresh / fs.f_files) * 100. + .5;
BOUND(temp);
uvp->uv_threshfiles = (int)temp;
ltmp = ((long long)clp->cl_maxfiles * MAXBSIZE);
uvp->uv_maxfilesize = (ltmp + (MAXBSIZE / 2)) / (1024 * 1024);
xx = ((double)clp->cl_blkhiwat / clp->cl_maxblks) * 100. + .5;
BOUND(xx);
uvp->uv_hiblocks = xx;
xx = ((double)clp->cl_blklowat / clp->cl_maxblks) * 100. + .5;
BOUND(xx);
uvp->uv_lowblocks = xx;
xx = ((double)clp->cl_filehiwat / clp->cl_maxinodes) * 100. + .5;
BOUND(xx);
uvp->uv_hifiles = xx;
xx = ((double)clp->cl_filelowat / clp->cl_maxinodes) * 100. + .5;
BOUND(xx);
uvp->uv_lowfiles = xx;
/* return success */
return (0);
}
/*
* cachefs_file_to_dir
*
* takes in a path, and returns the parent directory of that path.
*
* it's the caller's responsibility to free the pointer returned by
* this function.
*/
char *
cachefs_file_to_dir(const char *path)
{
char *rc, *cp;
if (path == NULL)
return (NULL);
rc = strdup(path);
if (rc == NULL)
return (NULL);
if ((cp = strrchr(rc, '/')) == NULL) {
/*
* if no slashes at all, return "." (current directory).
*/
(void) free(rc);
rc = strdup(".");
} else if (cp == rc) {
/*
* else, if the last '/' is the first character, chop
* off from there (i.e. return "/").
*/
rc[1] = '\0';
} else {
/*
* else, we have a path like /foo/bar or foo/bar.
* chop off from the last '/'.
*/
*cp = '\0';
}
return (rc);
}
/*
* cachefs_clean_flag_test
*
* Description:
* Tests whether or not the clean flag on the file system
* is set.
* Arguments:
* cachedirp name of the the file system cache directory
* Returns:
* Returns 1 if the cache was shut down cleanly, 0 if not.
* Preconditions:
* precond(cachedirp)
*/
int
cachefs_clean_flag_test(const char *cachedirp)
{
char *namep;
int xx;
char buf[MAXPATHLEN];
int fd;
struct cache_usage cu;
/* construct the path name of the resource file */
namep = RESOURCE_NAME;
xx = strlen(cachedirp) + strlen(namep) + 3;
if (xx >= MAXPATHLEN) {
pr_err(gettext("Path name too long %s/%s"),
cachedirp, namep);
return (39);
}
snprintf(buf, sizeof (buf), "%s/%s", cachedirp, namep);
/* open the file; it will be <2GB */
fd = open(buf, O_RDONLY);
if (fd == -1) {
pr_err(gettext("Cannot open %s: %s"), buf, strerror(errno));
return (0);
}
/* read the cache_usage structure */
xx = read(fd, &cu, sizeof (cu));
if (xx != sizeof (cu)) {
pr_err(gettext("Error reading %s: %d %s"), buf,
xx, strerror(errno));
close(fd);
return (0);
}
close(fd);
/* return state of the cache */
return ((cu.cu_flags & CUSAGE_ACTIVE) == 0);
}
time32_t
get_boottime()
{
struct utmpx id, *putmp;
id.ut_type = BOOT_TIME;
setutxent();
if ((putmp = getutxid(&id)) != NULL)
return ((time32_t)putmp->ut_tv.tv_sec);
return (-1);
}
|