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
|
/*
* 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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* This is mostly new code. Major revisions were made to allow multiple
* file systems to share a common cache. While this consisted primarily
* of including a "devid_t" pointer in the hash functions, I also re-
* organized everything to eliminate much of the duplicated code that
* had existed previously.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/param.h>
#include <sys/vnode.h>
#include <sys/sysmacros.h>
#include <sys/filep.h>
#include <sys/salib.h>
#include <sys/promif.h>
#ifndef ICACHE_SIZE
/*
* These should probably be defined in an architecture-specific header
* file. The values below are analogous to those used in earlier versions
* of this module.
*/
#define ICACHE_SIZE 350 /* Max number of I-node in file cache */
#define DCACHE_SIZE 1500 /* Max number of cached directories */
#define BCACHE_SIZE 250 /* Max number of cached disk blocks */
#endif
#define Next 0 /* Next pointer in Fwd/Bak link */
#define Prev 1 /* Previous pointer in Fwd/Back links */
#define Frst 0 /* Ptr to first element of a chain */
#define Last 1 /* Ptr to last element of a chain */
#define Hash 2 /* Offset of hash chain ptrs. */
typedef struct cache { /* Generic cache element: */
struct cache *link[4]; /* .. Fwd/Bak links for hash chain & LRU */
struct cache **chn; /* .. Hash chain link */
int dev; /* .. Device file handle */
void *data; /* .. Ptr to associated data */
int size; /* .. Size of cached data */
} cache_t;
typedef struct head { /* Generic cache header: */
cache_t *aged[2]; /* .. LRU list */
int (*cmp)(cache_t *); /* .. Ptr to comparison function */
int size; /* .. Size of "cache" objects */
int maxblks; /* .. Max number of cached elements */
int count; /* .. Current number of cached elements */
int hits; /* .. Total cache hits */
int searches; /* .. Total searches */
int purges; /* .. Total purges */
} head_t;
/* Constructor for cache headers: */
#define cache_head(h, f, t, n) \
{{(cache_t *)&h, (cache_t *)&h}, f, sizeof (t), n}
int read_opt; /* Number of times cache was bypassed */
static int x_dev; /* Target device ID saved here! */
static int x_len; /* length of object */
#define LOG2(x) \
(((x) <= 16) ? 4 : /* Yeah, it's ugly. But it works! */ \
(((x) <= 32) ? 5 : /* .. Binary log should be part of */ \
(((x) <= 64) ? 6 : /* .. the language! */ \
(((x) <= 128) ? 7 : 8))))
static cache_t *
get_cache(cache_t *cap, head_t *chp)
{
/*
* Search cache:
*
* The caller pass a pointer to the first "cache" object in the current
* hash chain ["cap"] and a pointer to the corresponding cache header
* ["chp"]. This routine follows the cache chain until it finds an
* entry that matches both the current device [as noted in "x_dev"]
* and the cache-specific comparison ["chp->cmp"].
*
* Returns the address of the matching cache object or null if there
* is none.
*/
while (cap) {
/*
* Check all entries on the cache chain. We expect
* chains to be relatively short, so we use a simple
* linear search.
*/
if ((x_dev == cap->dev) && (*chp->cmp)(cap)) {
/*
* Found the entry we're looking for! Move it
* to the front of the cache header's LRU list
* before returing its addres to the caller.
*/
cap->link[Next]->link[Prev] = cap->link[Prev];
cap->link[Prev]->link[Next] = cap->link[Next];
cap->link[Prev] = (cache_t *)chp->aged;
cap->link[Next] = chp->aged[Frst];
chp->aged[Frst]->link[Prev] = cap;
chp->aged[Frst] = cap;
chp->hits += 1;
break;
}
cap = cap->link[Hash+Next];
}
chp->searches += 1;
return (cap);
}
static cache_t *
reclaim_cache(head_t *chp, int dev)
{
/*
* Reclaim a cache element:
*
* This routine is used to: [a] free the oldest element from
* the cache headed at "chp" and return the address of the
* corresponding "cache_t" struct (iff dev == -1), or [b] free all
* elements on the cache headed at "chp" that belong to the
* indicated "dev"ice.
*/
cache_t *cap, *cxp;
cache_t *cpp = (cache_t *)chp;
while ((cap = cpp->link[Prev]) != (cache_t *)chp) {
/*
* We follow the cache's LRU chain from oldest to
* newest member. This ensures that we remove only
* the oldest element when we're called with a
* negative "dev" argument.
*/
if ((dev == -1) || (dev == cap->dev)) {
/*
* This is one of the (perhaps the only)
* elements we're supposed to free. Remove it
* from both the LRU list and its associated
* hash chain. Then free the data bound the
* the cache_t element and, if "dev" is
* not -1, the element itself!
*/
cap->link[Prev]->link[Next] = cap->link[Next];
cap->link[Next]->link[Prev] = cap->link[Prev];
if ((cxp = cap->link[Hash+Prev]) != 0)
cxp->link[Hash+Next] = cap->link[Hash+Next];
else
*(cap->chn) = cap->link[Hash+Next];
if ((cxp = cap->link[Hash+Next]) != 0)
cxp->link[Hash+Prev] = cap->link[Hash+Prev];
bkmem_free((caddr_t)cap->data, cap->size);
if (dev == -1)
return (cap);
bkmem_free((caddr_t)cap, chp->size);
chp->count -= 1;
} else {
/*
* Skip this element, it's not one of the
* ones we want to free up.
*/
cpp = cap;
}
};
return (0);
}
static cache_t *
set_cache(cache_t **ccp, head_t *chp, int noreclaim)
{
/*
* Install a cache element:
*
* The caller passes the address of cache descriptor ["chp"] and the
* hash chain into which the new element is to be linked ["ccp"]. This
* routine allocates a new cache_t structure (or, if the maximum number
* of elements has already been allocated, reclaims the oldest element
* from the cache), links it into the indicated hash chain, and returns
* its address to the caller.
*/
cache_t *cap;
if ((chp->count < chp->maxblks) &&
(cap = (cache_t *)bkmem_alloc(chp->size))) {
/*
* We haven't reached the maximum cache size yet.
* Allocate a new "cache_t" struct to be added to the
* cache.
*/
chp->count += 1;
} else {
if (noreclaim)
return (NULL);
/*
* Cache is full. Use the "reclaim_cache" routine to
* remove the oldest element from the cache. This
* will become the cache_t struct associated with the
* new element.
*/
cap = reclaim_cache(chp, -1);
chp->purges += 1;
}
bzero((char *)cap, chp->size);
cap->chn = ccp;
cap->link[Prev] = (cache_t *)chp;
cap->link[Next] = chp->aged[Frst];
cap->link[Prev]->link[Next] = cap->link[Next]->link[Prev] = cap;
if ((cap->link[Hash+Next] = *ccp) != 0)
(*ccp)->link[Hash+Prev] = cap;
return (*ccp = cap);
}
/*
* The File Cache:
*
* This cache (also known as the inode cache) is used to keep track of all
* files open on a given device. The only special data required to locate
* a cache entry is the file reference number which is file-system dependent
* (for UNIX file systems, it's an inode number).
*/
typedef struct icache { /* Inode cache element: */
cache_t ic_hdr; /* .. Standard header */
int ic_num; /* .. I-node number */
} ic_t;
#define IC_MAX_HDRS (1 << LOG2(ICACHE_SIZE/6))
#define IC_HASH(d, i) (((d) + (i)) & (IC_MAX_HDRS - 1))
static int x_inode;
static int /* Cache search predicate: */
cmp_icache(cache_t *p)
{
/* Just check the file number ("x_inode") ... */
return (((ic_t *)p)->ic_num == x_inode);
}
static head_t ic_head = cache_head(ic_head, cmp_icache, ic_t, ICACHE_SIZE);
static cache_t *ic_hash[IC_MAX_HDRS];
void *
get_icache(int dev, int inum)
{
/*
* Search File Cache:
*
* This routine searches the file cache looking for the entry bound to
* the given "dev"ice and file number ["inum"]. If said entry exists,
* it returns the address of the associated file structure. Otherwise
* it returns null.
*/
cache_t *icp;
x_dev = dev;
x_inode = inum;
icp = get_cache(ic_hash[IC_HASH(dev, inum)], &ic_head);
return (icp ? (caddr_t)icp->data : 0);
}
void
set_icache(int dev, int inum, void *ip, int size)
{
/*
* Build a File Cache Entry:
*
* This routne installs the "size"-byte file structure at
* "*ip" in the inode cache where it may be retrieved by
* subsequent call to get_icache.
*/
ic_t *icp = (ic_t *)set_cache(&ic_hash[IC_HASH(dev, inum)],
&ic_head, 0);
icp->ic_num = inum;
icp->ic_hdr.data = ip;
icp->ic_hdr.dev = dev;
icp->ic_hdr.size = size;
}
int
set_ricache(int dev, int inum, void *ip, int size)
{
/*
* Reliably set the icache
*
* This routine is the same as set_icache except that it
* will return 1 if the entry could not be entered into the cache
* without a purge.
*/
ic_t *icp = (ic_t *)set_cache(&ic_hash[IC_HASH(dev, inum)],
&ic_head, 1);
if (icp == NULL)
return (1);
icp->ic_num = inum;
icp->ic_hdr.data = ip;
icp->ic_hdr.dev = dev;
icp->ic_hdr.size = size;
return (0);
}
/*
* The Directory Cache:
*
* This cache is designed to speed directory searches. Each entry cor-
* responds to a directory entry that was used in a pathname resolution.
* The idea is that most files used by the boot wil be contained in a hand-
* full of directories, so we can speed searches if we know ahead of time
* just where these directories are.
*/
typedef struct dcache { /* Directory cache objects: */
cache_t dc_hdr; /* .. Standard header */
int dc_inum; /* .. File number */
int dc_pnum; /* .. Parent diretory's file number */
} dc_t;
#define DC_MAX_HDRS (1 << LOG2(DCACHE_SIZE/6))
#define DC_HASH(d, n, l) (((d) + (n)[0] + (n)[(l)-1] + (l)) & (DC_MAX_HDRS-1))
static char *x_name;
static int x_pnum;
static int
cmp_dcache(cache_t *p) /* Cache Search predicate: */
{
/* Check name, length, and parent's file number */
return ((x_len == p->size) && (x_pnum == ((dc_t *)p)->dc_pnum) &&
(strcmp((char *)p->data, x_name) == 0));
}
static head_t dc_head = cache_head(dc_head, cmp_dcache, dc_t, DCACHE_SIZE);
static cache_t *dc_hash[DC_MAX_HDRS];
int
get_dcache(int dev, char *name, int pnum)
{
/*
* Search Directory Cache:
*
* This routine searches the directory cache for an entry
* associated with directory number "pnum" from the given
* file system that de-scribes a file of the given "name".
* If we find such an entry, we return the corresponding file
* number, 0 otherwise.
*/
dc_t *dcp;
x_dev = dev;
x_len = strlen(name)+1;
x_pnum = pnum;
x_name = name;
dcp = (dc_t *)get_cache(dc_hash[DC_HASH(dev, name, x_len)], &dc_head);
return (dcp ? dcp->dc_inum : 0);
}
void
set_dcache(int dev, char *name, int pnum, int inum)
{
/*
* Build Directory Cache Entry:
*
* This routine creates directory cache entries to be retrieved later
* via "get_dcache". The cache key is composed of three parts: The
* device specifier, the file name ("name"), and the file number of
* the directory containing that name ("pnum"). The data portion of
* the entry consists of the file number ("inum").
*/
int len = strlen(name)+1;
dc_t *dcp =
(dc_t *)set_cache(&dc_hash[DC_HASH(dev, name, len)], &dc_head, 0);
if (dcp->dc_hdr.data = (void *)bkmem_alloc(len)) {
/*
* Allocate a buffer for the pathname component, and
* make this the "data" portion of the generalize
* "cache_t" struct. Also fill in the cache-specific
* fields (pnum, inum).
*/
dcp->dc_pnum = pnum;
dcp->dc_inum = inum;
dcp->dc_hdr.dev = dev;
dcp->dc_hdr.size = len;
bcopy(name, (char *)dcp->dc_hdr.data, len);
} else {
/*
* Not enough memory to make a copy of the name!
* There's probably not enough to do much else either!
*/
prom_panic("no memory for directory cache");
}
}
int
set_rdcache(int dev, char *name, int pnum, int inum)
{
/*
* Reliably set the dcache
*
* This routine is the same as set_dcache except that it
* return 1 if the entry could not be entered into
* the cache without a purge.
*/
int len = strlen(name) + 1;
dc_t *dcp =
(dc_t *)set_cache(&dc_hash[DC_HASH(dev, name, len)],
&dc_head, 1);
if (dcp == NULL)
return (1);
if ((dcp->dc_hdr.data = (void *)bkmem_alloc(len)) == NULL) {
/*
* Not enough memory to make a copy of the name!
* There's probably not enough to do much else either!
*/
prom_panic("no memory for directory cache");
/* NOTREACHED */
}
/*
* Allocate a buffer for the pathname component, and
* make this the "data" portion of the generalize
* "cache_t" struct. Also fill in the cache-specific
* fields (pnum, inum).
*/
dcp->dc_pnum = pnum;
dcp->dc_inum = inum;
dcp->dc_hdr.dev = dev;
dcp->dc_hdr.size = len;
bcopy(name, (char *)dcp->dc_hdr.data, len);
return (0);
}
/*
* Disk Block Cache:
*/
typedef struct bcache { /* Disk block cache objects: */
cache_t bc_hdr; /* .. Standard header */
unsigned long bc_blk; /* .. The block number */
} bc_t;
#define BC_MAX_HDRS (1 << LOG2(BCACHE_SIZE/6))
#define BC_HASH(d, b, l) (((d) + (b) + ((l) >> 8)) & (BC_MAX_HDRS-1))
static unsigned long x_blkno;
static int
cmp_bcache(cache_t *p) /* Cache Search predicate: */
{
/* Check block number, buffer size */
return ((x_len == p->size) && (x_blkno == ((bc_t *)p)->bc_blk));
}
static head_t bc_head = cache_head(bc_head, cmp_bcache, bc_t, BCACHE_SIZE);
static cache_t *bc_hash[BC_MAX_HDRS];
caddr_t
get_bcache(fileid_t *fp)
{
/*
* Search Disk Block Cache:
*
* This should be getting pretty monotonous by now. Aren't generalized
* subroutines ("objects", if you prefer) great?
*/
cache_t *bcp;
x_len = fp->fi_count;
x_blkno = fp->fi_blocknum;
x_dev = fp->fi_devp->di_dcookie;
bcp = get_cache(bc_hash[BC_HASH(x_dev, x_blkno, x_len)], &bc_head);
return (bcp ? (caddr_t)bcp->data : 0);
}
int
set_bcache(fileid_t *fp)
{
/*
* Insert Disk Block Cache Entry:
*
* In this case, we actually read the requested block into a
* dynamically allocated buffer before inserting it into the
* cache. If the read fails, we return a non-zero value.
*
* The search keys for disk blocks are the block number and
* buffer size. The data associated with each entry is the
* corresponding data buffer.
*/
bc_t *bcp;
if (fp->fi_memp = bkmem_alloc(x_len = fp->fi_count)) {
/*
* We were able to succesffully allocate an input
* buffer, now read the data into it.
*/
if (diskread(fp) != 0) {
/*
* I/O error on read. Free the input buffer,
* print an error message, and bail out.
*/
bkmem_free(fp->fi_memp, x_len);
printf("disk read error\n");
return (-1);
}
x_blkno = fp->fi_blocknum;
x_dev = fp->fi_devp->di_dcookie;
bcp = (bc_t *)
set_cache(&bc_hash[BC_HASH(x_dev, x_blkno, x_len)],
&bc_head, 0);
bcp->bc_blk = x_blkno;
bcp->bc_hdr.dev = x_dev;
bcp->bc_hdr.size = x_len;
bcp->bc_hdr.data = (void *)fp->fi_memp;
} else {
/*
* We could be a bit more convervative here by
* calling "set_cache" before we try to allocate a
* buffer (thereby giving us a chance to re-use a
* previously allocated buffer) but the error recovery
* is a bit trickier, and if we're that short on memory
* we'll have trouble elsewhere anyway!
*/
prom_panic("can't read - no memory");
}
return (0);
}
void
release_cache(int dev)
{
/*
* Reclaim all cache entries:
*
* This routine is called by the file-system's "closeall" method. It
* removes all cache entries associated with that file system from the
* global cache and release any resources bound to said entrires.
*/
(void) reclaim_cache(&ic_head, dev);
(void) reclaim_cache(&dc_head, dev);
(void) reclaim_cache(&bc_head, dev);
}
void
print_cache_data()
{
/*
* Print some cacheing statistics ...
*/
static char *tag[] = { "inode", "directory", "disk block", 0};
static head_t *hdp[] = { &ic_head, &dc_head, &bc_head, 0};
int j;
for (j = 0; tag[j]; j++) {
/*
* Print statistics maintained in the header
* ("head_t" struct) of each of the above caches.
*/
head_t *hp = hdp[j];
if (j)
printf("\n");
printf("%s cache:\n", tag[j]);
printf(" max size %d\n", hp->maxblks);
printf(" actual size %d\n", hp->count);
printf(" total searches %d\n", hp->searches);
printf(" cache hits %d\n", hp->hits);
printf(" cache purges %d\n", hp->purges);
}
printf("\nread opts %d\n", read_opt);
}
/*
* 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 1994-1996, 2002-2003 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/vnode.h>
#include <sys/fs/ufs_fsdir.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_inode.h>
#include <sys/sysmacros.h>
#include <sys/promif.h>
#include <sys/filep.h>
#include <sys/salib.h>
static char prom_dev_type = 0;
/*
* unix root slice offset for PROMS that do
* not know about fdisk partitions or Solaris
* slices.
* the default is 0 for machines with proms that
* do know how to interpret solaris slices.
*/
unsigned long unix_startblk = 0;
/*
* The various flavors of PROM make this grotesque.
*/
int
diskread(fileid_t *filep)
{
int err;
devid_t *devp;
uint_t blocknum;
/* add in offset of root slice */
blocknum = filep->fi_blocknum + unix_startblk;
devp = filep->fi_devp;
err = prom_seek(devp->di_dcookie,
(unsigned long long)blocknum * (unsigned long long)DEV_BSIZE);
if (err == -1) {
printf("Seek error at block %x\n", blocknum);
return (-1);
}
if ((err = prom_read(devp->di_dcookie, filep->fi_memp, filep->fi_count,
blocknum, prom_dev_type)) != filep->fi_count) {
printf("Short read. 0x%x chars read\n", err);
return (-1);
}
return (0);
}
/*
* 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 1994-1996, 2002-2003 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/sysmacros.h>
#include <sys/stat.h>
#include <sys/bootvfs.h>
#include <sys/bootsyms.h>
#include <sys/promif.h>
#include <sys/salib.h>
static struct boot_fs_ops *dfl_fsw = (struct boot_fs_ops *)NULL;
static char *fsmsg = "Fstype has not been selected yet!\n";
static char *msg_noops = "not fs_ops supplied\n";
/*
* return fs_ops pointer for a given file system name
*/
struct boot_fs_ops *
get_fs_ops_pointer(char *fsw_name)
{
int fsw_idx;
for (fsw_idx = 0; fsw_idx < boot_nfsw; fsw_idx++)
if (strcmp(boot_fsw[fsw_idx]->fsw_name, fsw_name) == 0) {
return (boot_fsw[fsw_idx]);
}
return ((struct boot_fs_ops *)NULL);
}
/*
* set default file system type
*/
void
set_default_fs(char *fsw_name)
{
int fsw_idx;
for (fsw_idx = 0; fsw_idx < boot_nfsw; fsw_idx++)
if (strcmp(boot_fsw[fsw_idx]->fsw_name, fsw_name) == 0) {
dfl_fsw = boot_fsw[fsw_idx];
return;
}
printf("Fstype <%s> is not recognized\n", fsw_name);
prom_panic("");
}
/*
* clear default file system type
*/
void
clr_default_fs(void)
{
dfl_fsw = NULL;
}
struct boot_fs_ops *
get_default_fs(void)
{
return (dfl_fsw);
}
void
boot_no_ops_void()
{
prom_panic(msg_noops);
/*NOTREACHED*/
}
int
boot_no_ops()
{
prom_panic(msg_noops);
/*NOTREACHED*/
return (0);
}
int
close(int fd)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_close)(fd));
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
mountroot(char *str)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_mountroot)(str));
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
unmountroot(void)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_unmountroot)());
prom_panic(fsmsg);
/*NOTREACHED*/
}
/*ARGSUSED*/
int
open(const char *filename, int flags)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_open)((char *)filename, flags));
prom_panic(fsmsg);
/*NOTREACHED*/
}
ssize_t
read(int fd, void *buf, size_t size)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_read)(fd, buf, size));
prom_panic(fsmsg);
/*NOTREACHED*/
}
void
closeall(int flag)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL) {
(*dfl_fsw->fsw_closeall)(flag);
return;
}
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
fstat(int fd, struct stat *sb)
{
struct bootstat buf;
int ret;
if (dfl_fsw == NULL)
prom_panic(fsmsg);
ret = (*dfl_fsw->fsw_fstat)(fd, &buf);
if (ret == -1)
return (-1);
sb->st_dev = buf.st_dev;
sb->st_ino = buf.st_ino;
sb->st_mode = buf.st_mode;
sb->st_nlink = buf.st_nlink;
sb->st_uid = buf.st_uid;
sb->st_gid = buf.st_gid;
sb->st_rdev = buf.st_rdev;
sb->st_size = (off_t)buf.st_size;
sb->st_blksize = buf.st_blksize;
sb->st_blocks = buf.st_blocks;
sb->st_atim.tv_sec = buf.st_atim.tv_sec;
sb->st_atim.tv_nsec = buf.st_atim.tv_nsec;
sb->st_mtim.tv_sec = buf.st_mtim.tv_sec;
sb->st_mtim.tv_nsec = buf.st_mtim.tv_nsec;
sb->st_ctim.tv_sec = buf.st_ctim.tv_sec;
sb->st_ctim.tv_nsec = buf.st_ctim.tv_nsec;
(void) memcpy(sb->st_fstype, buf.st_fstype, sizeof (sb->st_fstype));
return (0);
}
int
stat(const char *filename, struct stat *sb)
{
int fd, ret = -1;
if ((fd = open(filename, O_RDONLY)) != -1) {
ret = fstat(fd, sb);
(void) close(fd);
}
return (ret);
}
off_t
lseek(int filefd, off_t addr, int whence)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_lseek)(filefd, addr, whence));
prom_panic(fsmsg);
/*NOTREACHED*/
}
/*
* Kernel Interface
*/
int
kern_open(char *str, int flags)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_open)(str, flags));
prom_panic(fsmsg);
/*NOTREACHED*/
}
/*
* hi and lo refer to the MS end of the off_t word
* and the LS end of the off_t word for when we want
* to support 64-bit offsets. For now, lseek() just
* supports 32 bits.
*/
/*ARGSUSED*/
off_t
kern_lseek(int filefd, off_t hi, off_t lo)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_lseek)(filefd, lo, 0));
prom_panic(fsmsg);
/*NOTREACHED*/
}
ssize_t
kern_read(int fd, caddr_t buf, size_t size)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_read)(fd, buf, size));
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
kern_close(int fd)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_close)(fd));
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
kern_fstat(int fd, struct bootstat *buf)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_fstat)(fd, buf));
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
kern_getdents(int fd, struct dirent *buf, size_t size)
{
if (dfl_fsw != (struct boot_fs_ops *)NULL)
return ((*dfl_fsw->fsw_getdents)(fd, buf, size));
prom_panic(fsmsg);
/*NOTREACHED*/
}
int
kern_mountroot(char *path)
{
return (mountroot(path));
}
int
kern_unmountroot(void)
{
return (unmountroot());
}
/*
* 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 2008 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/sysmacros.h>
#include <sys/stat.h>
#include <sys/bootvfs.h>
#include <sys/bootsyms.h>
#include <sys/promif.h>
#include <sys/salib.h>
/*
* Function prototypes
*/
static int promfs_mountroot(char *str);
static int promfs_unmountroot(void);
static int promfs_open(char *filename, int flags);
static int promfs_close(int fd);
static ssize_t promfs_read(int fd, caddr_t buf, size_t size);
static off_t promfs_lseek(int fd, off_t offset, int whence);
static int promfs_fstat(int fd, struct bootstat *stp);
static void promfs_closeall(int flag);
struct boot_fs_ops promfs_ops = {
"promfs",
promfs_mountroot,
promfs_unmountroot,
promfs_open,
promfs_close,
promfs_read,
promfs_lseek,
promfs_fstat,
promfs_closeall,
NULL
};
static ihandle_t fsih;
static int
promfs_mountroot(char *str)
{
(void) prom_getprop(prom_chosennode(), str, (caddr_t)&fsih);
return (fsih == -1);
}
static int
promfs_unmountroot(void)
{
(void) prom_close(fsih);
return (0);
}
/*ARGSUSED*/
static int
promfs_open(char *filename, int flags)
{
return (prom_fopen(fsih, filename));
}
static int
promfs_close(int fd)
{
prom_fclose(fsih, fd);
return (0);
}
static ssize_t
promfs_read(int fd, caddr_t buf, size_t size)
{
return (prom_fread(fsih, fd, buf, size));
}
/*ARGSUSED*/
static off_t
promfs_lseek(int fd, off_t offset, int whence)
{
return (prom_fseek(fsih, fd, offset));
}
static int
promfs_fstat(int fd, struct bootstat *stp)
{
return (prom_fsize(fsih, fd, (size_t *)&stp->st_size));
}
/*ARGSUSED*/
static void
promfs_closeall(int flag)
{
}
|