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
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright (c) 2015 Joyent, Inc. All rights reserved.
*/
/*
* This file takes care of reading the boot time modules and constructing them
* into the appropriate series of vnodes.
*/
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/vfs.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <sys/fs/bootfs_impl.h>
kmem_cache_t *bootfs_node_cache;
static const vattr_t bootfs_vattr_dir = {
AT_ALL, /* va_mask */
VDIR, /* va_type */
S_IFDIR | 0555, /* va_mode */
0, /* va_uid */
0, /* va_gid */
0, /* va_fsid */
0, /* va_nodeid */
1, /* va_nlink */
0, /* va_size */
0, /* va_atime */
0, /* va_mtime */
0, /* va_ctime */
0, /* va_rdev */
0, /* va_blksize */
0, /* va_nblocks */
0 /* va_seq */
};
static const vattr_t bootfs_vattr_reg = {
AT_ALL, /* va_mask */
VREG, /* va_type */
S_IFREG | 0555, /* va_mode */
0, /* va_uid */
0, /* va_gid */
0, /* va_fsid */
0, /* va_nodeid */
1, /* va_nlink */
0, /* va_size */
0, /* va_atime */
0, /* va_mtime */
0, /* va_ctime */
0, /* va_rdev */
0, /* va_blksize */
0, /* va_nblocks */
0 /* va_seq */
};
/*ARGSUSED*/
int
bootfs_node_constructor(void *buf, void *arg, int kmflags)
{
bootfs_node_t *bnp = buf;
bnp->bvn_vnp = vn_alloc(kmflags);
if (bnp->bvn_vnp == NULL)
return (-1);
return (0);
}
/*ARGSUSED*/
void
bootfs_node_destructor(void *buf, void *arg)
{
bootfs_node_t *bnp = buf;
vn_free(bnp->bvn_vnp);
}
static int
bootfs_comparator(const void *a, const void *b)
{
const bootfs_node_t *lfs, *rfs;
int ret;
lfs = a;
rfs = b;
ret = strcmp(lfs->bvn_name, rfs->bvn_name);
if (ret > 0)
ret = 1;
if (ret < 0)
ret = -1;
return (ret);
}
static void
bootfs_node_init(bootfs_t *bfs, bootfs_node_t *bnp, const struct vattr *vap,
const char *name, size_t namelen)
{
timestruc_t now;
vn_reinit(bnp->bvn_vnp);
bnp->bvn_vnp->v_flag |= VNOSWAP;
bnp->bvn_vnp->v_type = vap->va_type;
bnp->bvn_vnp->v_vfsp = bfs->bfs_vfsp;
bnp->bvn_vnp->v_rdev = 0;
bnp->bvn_vnp->v_data = (caddr_t)bnp;
vn_setops(bnp->bvn_vnp, bootfs_vnodeops);
bnp->bvn_name = kmem_alloc(namelen + 1, KM_SLEEP);
bcopy(name, bnp->bvn_name, namelen);
bnp->bvn_name[namelen] = '\0';
if (vap->va_type == VDIR) {
avl_create(&bnp->bvn_dir, bootfs_comparator,
sizeof (bootfs_node_t),
offsetof(bootfs_node_t, bvn_link));
}
bzero(&bnp->bvn_link, sizeof (avl_node_t));
bcopy(vap, &bnp->bvn_attr, sizeof (vattr_t));
gethrestime(&now);
bnp->bvn_attr.va_atime = now;
bnp->bvn_attr.va_ctime = now;
bnp->bvn_attr.va_mtime = now;
bnp->bvn_attr.va_fsid = makedevice(bootfs_major, bfs->bfs_minor);
bnp->bvn_attr.va_nodeid = bfs->bfs_ninode;
bnp->bvn_attr.va_blksize = PAGESIZE;
bfs->bfs_ninode++;
list_insert_tail(&bfs->bfs_nodes, bnp);
}
static void
bootfs_mkroot(bootfs_t *bfs)
{
bootfs_node_t *bnp;
bnp = kmem_cache_alloc(bootfs_node_cache, KM_SLEEP);
bootfs_node_init(bfs, bnp, &bootfs_vattr_dir, "/", 1);
bnp->bvn_vnp->v_flag |= VROOT;
bnp->bvn_parent = bnp;
bfs->bfs_rootvn = bnp;
bfs->bfs_stat.bfss_ndirs.value.ui32++;
vn_exists(bnp->bvn_vnp);
}
static int
bootfs_mknode(bootfs_t *bfs, bootfs_node_t *parent, bootfs_node_t **outp,
const char *name, size_t namelen, const vattr_t *vap, uintptr_t addr,
uint64_t size)
{
bootfs_node_t *bnp;
bootfs_node_t sn;
avl_index_t where;
char *buf;
ASSERT(parent->bvn_attr.va_type == VDIR);
buf = kmem_alloc(namelen + 1, KM_SLEEP);
bcopy(name, buf, namelen);
buf[namelen] = '\0';
sn.bvn_name = buf;
if ((bnp = avl_find(&parent->bvn_dir, &sn, &where)) != NULL) {
kmem_free(buf, namelen + 1);
/* Directories can collide, files cannot */
if (vap->va_type == VDIR) {
*outp = bnp;
return (0);
}
return (EEXIST);
}
kmem_free(buf, namelen + 1);
bnp = kmem_cache_alloc(bootfs_node_cache, KM_SLEEP);
bootfs_node_init(bfs, bnp, vap, name, namelen);
bnp->bvn_parent = parent;
avl_add(&parent->bvn_dir, bnp);
*outp = bnp;
if (vap->va_type == VDIR) {
parent->bvn_attr.va_size++;
parent->bvn_attr.va_nlink++;
bfs->bfs_stat.bfss_ndirs.value.ui32++;
} else {
bnp->bvn_addr = addr;
bnp->bvn_size = size;
bfs->bfs_stat.bfss_nfiles.value.ui32++;
bfs->bfs_stat.bfss_nbytes.value.ui64 += size;
bnp->bvn_attr.va_nblocks = P2ROUNDUP(size, 512) >> 9;
bnp->bvn_attr.va_size = size;
}
vn_exists(bnp->bvn_vnp);
return (0);
}
/*
* Given the address, size, and path a boot-time module would like, go through
* and create all of the directory entries that are required and then the file
* itself. If someone has passed in a module that has the same name as another
* one, we honor the first one.
*/
static int
bootfs_construct_entry(bootfs_t *bfs, uintptr_t addr, uint64_t size,
const char *mname)
{
char *sp;
size_t nlen;
int ret;
bootfs_node_t *nbnp;
const char *p = mname;
bootfs_node_t *bnp = bfs->bfs_rootvn;
if (*p == '\0')
return (EINVAL);
for (;;) {
/* First eliminate all leading / characters. */
while (*p == '/')
p++;
/* A name with all slashes or ending in a / */
if (*p == '\0')
return (EINVAL);
sp = strchr(p, '/');
if (sp == NULL)
break;
nlen = (ptrdiff_t)sp - (ptrdiff_t)p;
if (strncmp(p, ".", nlen) == 0) {
p = sp + 1;
continue;
}
if (strncmp(p, "..", nlen) == 0) {
bnp = bnp->bvn_parent;
p = sp + 1;
continue;
}
VERIFY(bootfs_mknode(bfs, bnp, &nbnp, p, nlen,
&bootfs_vattr_dir, addr, size) == 0);
p = sp + 1;
bnp = nbnp;
}
nlen = strlen(p);
ret = bootfs_mknode(bfs, bnp, &nbnp, p, nlen, &bootfs_vattr_reg,
addr, size);
if (ret != 0)
return (ret);
return (0);
}
/*
* We're going to go through every boot time module and construct the
* appropriate vnodes for them now. Because there are very few of these that
* exist, generally on the order of a handful, we're going to create them all
* when the file system is initialized and then tear them all down when the
* module gets unloaded.
*
* The information about the modules is contained in properties on the root of
* the devinfo tree. Specifically there are three properties per module:
*
* - module-size-%d int64_t size, in bytes, of the boot time module.
* - module-addr-%d The address of the boot time module
* - module-name-%d The string name of the boot time module
*
* Note that the module-size and module-addr fields are always 64-bit values
* regardless of being on a 32-bit or 64-bit kernel. module-name is a string
* property.
*
* There is no property that indicates the total number of such modules. Modules
* start at 0 and work their way up incrementally. The first time we can't find
* a module or a property, then we stop.
*/
void
bootfs_construct(bootfs_t *bfs)
{
uint_t id = 0;
char paddr[64], psize[64], pname[64], *mname;
dev_info_t *root;
uint64_t size, addr;
int ret;
bootfs_mkroot(bfs);
root = ddi_root_node();
for (;;) {
if (id == UINT32_MAX)
break;
if (snprintf(paddr, sizeof (paddr), "module-addr-%d", id) >
sizeof (paddr))
break;
if (snprintf(psize, sizeof (paddr), "module-size-%d", id) >
sizeof (paddr))
break;
if (snprintf(pname, sizeof (paddr), "module-name-%d", id) >
sizeof (paddr))
break;
addr = (uint64_t)ddi_prop_get_int64(DDI_DEV_T_ANY, root,
DDI_PROP_DONTPASS, paddr, 0);
if (addr == 0 || addr == DDI_PROP_NOT_FOUND)
break;
size = (uint64_t)ddi_prop_get_int64(DDI_DEV_T_ANY, root,
DDI_PROP_DONTPASS, psize, 0);
if (size == 0 || size == DDI_PROP_NOT_FOUND)
break;
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, root,
DDI_PROP_DONTPASS, pname, &mname) != DDI_PROP_SUCCESS)
break;
ret = bootfs_construct_entry(bfs, addr, size, mname);
if (ret == EINVAL)
bfs->bfs_stat.bfss_ndiscards.value.ui32++;
if (ret == EEXIST)
bfs->bfs_stat.bfss_ndups.value.ui32++;
ddi_prop_free(mname);
id++;
}
}
void
bootfs_destruct(bootfs_t *bfs)
{
bootfs_node_t *bnp;
while ((bnp = list_remove_head(&bfs->bfs_nodes)) != NULL) {
ASSERT(bnp->bvn_vnp->v_count == 1);
VN_RELE(bnp->bvn_vnp);
kmem_free(bnp->bvn_name, strlen(bnp->bvn_name) + 1);
kmem_cache_free(bootfs_node_cache, bnp);
}
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright (c) 2015 Joyent, Inc.
*/
#include <sys/errno.h>
#include <sys/modctl.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/systm.h>
#include <sys/id_space.h>
#include <sys/cmn_err.h>
#include <sys/ksynch.h>
#include <sys/policy.h>
#include <sys/mount.h>
#include <sys/sysmacros.h>
#include <sys/fs/bootfs_impl.h>
/*
* While booting, additional types of modules and files can be passed in to the
* loader. These include the familiar boot archive, as well as, a module hash
* and additional modules that are interpreted as files. As part of the handoff
* in early boot, information about these modules are saved as properties on the
* root of the devinfo tree, similar to other boot-time properties.
*
* This file system provides a read-only view of those additional files. Due to
* its limited scope, it has a slightly simpler construction than several other
* file systems. When mounted, it looks for the corresponding properties and
* creates bootfs_node_t's and vnodes for all of the corresponding files and
* directories that exist along the way. At this time, there are currently a
* rather small number of files passed in this way.
*
* This does lead to one behavior that folks used to other file systems might
* find peculiar. Because we are not always actively creating and destroying the
* required vnodes on demand, the count on the root vnode will not be going up
* accordingly with the existence of other vnodes. This means that a bootfs file
* system that is not in use will have all of its vnodes exist with a v_count of
* one.
*/
major_t bootfs_major;
static int bootfs_fstype;
static id_space_t *bootfs_idspace;
static uint64_t bootfs_nactive;
static kmutex_t bootfs_lock;
static const char *bootfs_name = "bootfs";
static int
bootfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
{
int ret;
bootfs_t *bfs;
struct pathname dpn;
dev_t fsdev;
if ((ret = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
return (ret);
if (mvp->v_type != VDIR)
return (ENOTDIR);
if (uap->flags & MS_REMOUNT)
return (EBUSY);
mutex_enter(&mvp->v_lock);
if ((uap->flags & MS_OVERLAY) == 0 &&
(mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
mutex_exit(&mvp->v_lock);
return (EBUSY);
}
mutex_exit(&mvp->v_lock);
/*
* We indicate that the backing store is bootfs. We don't want to use
* swap, because folks might think that this is putting all the data
* into memory ala tmpfs. Rather these modules are always in memory and
* there's nothing to be done about that.
*/
vfs_setresource(vfsp, bootfs_name, 0);
bfs = kmem_zalloc(sizeof (bootfs_t), KM_NOSLEEP_LAZY);
if (bfs == NULL)
return (ENOMEM);
ret = pn_get(uap->dir,
(uap->flags & MS_SYSSPACE) ? UIO_SYSSPACE : UIO_USERSPACE, &dpn);
if (ret != 0) {
kmem_free(bfs, sizeof (bfs));
return (ret);
}
bfs->bfs_minor = id_alloc(bootfs_idspace);
bfs->bfs_kstat = kstat_create_zone("bootfs", bfs->bfs_minor, "bootfs",
"fs", KSTAT_TYPE_NAMED,
sizeof (bootfs_stat_t) / sizeof (kstat_named_t),
KSTAT_FLAG_VIRTUAL, GLOBAL_ZONEID);
if (bfs->bfs_kstat == NULL) {
id_free(bootfs_idspace, bfs->bfs_minor);
pn_free(&dpn);
kmem_free(bfs, sizeof (bfs));
return (ENOMEM);
}
bfs->bfs_kstat->ks_data = &bfs->bfs_stat;
fsdev = makedevice(bootfs_major, bfs->bfs_minor);
bfs->bfs_vfsp = vfsp;
vfsp->vfs_data = (caddr_t)bfs;
vfsp->vfs_fstype = bootfs_fstype;
vfsp->vfs_dev = fsdev;
vfsp->vfs_bsize = PAGESIZE;
vfsp->vfs_flag |= VFS_RDONLY | VFS_NOSETUID | VFS_NOTRUNC |
VFS_UNLINKABLE;
vfs_make_fsid(&vfsp->vfs_fsid, fsdev, bootfs_fstype);
bfs->bfs_mntpath = kmem_alloc(dpn.pn_pathlen + 1, KM_SLEEP);
bcopy(dpn.pn_path, bfs->bfs_mntpath, dpn.pn_pathlen);
bfs->bfs_mntpath[dpn.pn_pathlen] = '\0';
pn_free(&dpn);
list_create(&bfs->bfs_nodes, sizeof (bootfs_node_t),
offsetof(bootfs_node_t, bvn_alink));
kstat_named_init(&bfs->bfs_stat.bfss_nfiles, "nfiles",
KSTAT_DATA_UINT32);
kstat_named_init(&bfs->bfs_stat.bfss_ndirs, "ndirs",
KSTAT_DATA_UINT32);
kstat_named_init(&bfs->bfs_stat.bfss_nbytes, "nbytes",
KSTAT_DATA_UINT64);
kstat_named_init(&bfs->bfs_stat.bfss_ndups, "ndup",
KSTAT_DATA_UINT32);
kstat_named_init(&bfs->bfs_stat.bfss_ndiscards, "ndiscard",
KSTAT_DATA_UINT32);
bootfs_construct(bfs);
kstat_install(bfs->bfs_kstat);
return (0);
}
static int
bootfs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
{
int ret;
bootfs_t *bfs = vfsp->vfs_data;
bootfs_node_t *bnp;
if ((ret = secpolicy_fs_unmount(cr, vfsp)) != 0)
return (ret);
if (flag & MS_FORCE)
return (ENOTSUP);
for (bnp = list_head(&bfs->bfs_nodes); bnp != NULL;
bnp = list_next(&bfs->bfs_nodes, bnp)) {
mutex_enter(&bnp->bvn_vnp->v_lock);
if (bnp->bvn_vnp->v_count > 1) {
mutex_exit(&bnp->bvn_vnp->v_lock);
return (EBUSY);
}
mutex_exit(&bnp->bvn_vnp->v_lock);
}
kstat_delete(bfs->bfs_kstat);
bootfs_destruct(bfs);
list_destroy(&bfs->bfs_nodes);
kmem_free(bfs->bfs_mntpath, strlen(bfs->bfs_mntpath) + 1);
id_free(bootfs_idspace, bfs->bfs_minor);
kmem_free(bfs, sizeof (bootfs_t));
return (0);
}
static int
bootfs_root(vfs_t *vfsp, vnode_t **vpp)
{
bootfs_t *bfs;
bfs = (bootfs_t *)vfsp->vfs_data;
*vpp = bfs->bfs_rootvn->bvn_vnp;
VN_HOLD(*vpp)
return (0);
}
static int
bootfs_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
{
const bootfs_t *bfs = (bootfs_t *)vfsp;
dev32_t d32;
sbp->f_bsize = PAGESIZE;
sbp->f_frsize = PAGESIZE;
sbp->f_blocks = bfs->bfs_stat.bfss_nbytes.value.ui64 >> PAGESHIFT;
sbp->f_bfree = 0;
sbp->f_bavail = 0;
sbp->f_files = bfs->bfs_stat.bfss_nfiles.value.ui32 +
bfs->bfs_stat.bfss_ndirs.value.ui32;
sbp->f_ffree = 0;
sbp->f_favail = 0;
(void) cmpldev(&d32, vfsp->vfs_dev);
sbp->f_fsid = d32;
(void) strlcpy(sbp->f_basetype, bootfs_name, FSTYPSZ);
bzero(sbp->f_fstr, sizeof (sbp->f_fstr));
return (0);
}
static const fs_operation_def_t bootfs_vfsops_tmpl[] = {
VFSNAME_MOUNT, { .vfs_mount = bootfs_mount },
VFSNAME_UNMOUNT, { .vfs_unmount = bootfs_unmount },
VFSNAME_ROOT, { .vfs_root = bootfs_root },
VFSNAME_STATVFS, { .vfs_statvfs = bootfs_statvfs },
NULL, NULL
};
static int
bootfs_init(int fstype, char *name)
{
int ret;
bootfs_fstype = fstype;
ASSERT(bootfs_fstype != 0);
ret = vfs_setfsops(fstype, bootfs_vfsops_tmpl, NULL);
if (ret != 0)
return (ret);
ret = vn_make_ops(name, bootfs_vnodeops_template, &bootfs_vnodeops);
if (ret != 0) {
(void) vfs_freevfsops_by_type(bootfs_fstype);
return (ret);
}
bootfs_major = getudev();
if (bootfs_major == (major_t)-1) {
cmn_err(CE_WARN, "bootfs_init: Can't get unique device number");
bootfs_major = 0;
}
bootfs_nactive = 0;
return (0);
}
static mntopts_t bootfs_mntopts = {
0, NULL
};
static vfsdef_t bootfs_vfsdef = {
VFSDEF_VERSION,
"bootfs",
bootfs_init,
VSW_HASPROTO|VSW_STATS,
&bootfs_mntopts
};
static struct modlfs bootfs_modlfs = {
&mod_fsops, "boot-time modules file system", &bootfs_vfsdef
};
static struct modlinkage bootfs_modlinkage = {
MODREV_1, &bootfs_modlfs, NULL
};
int
_init(void)
{
bootfs_node_cache = kmem_cache_create("bootfs_node_cache",
sizeof (bootfs_node_t), 0, bootfs_node_constructor,
bootfs_node_destructor, NULL, NULL, NULL, 0);
bootfs_idspace = id_space_create("bootfs_minors", 1, INT32_MAX);
mutex_init(&bootfs_lock, NULL, MUTEX_DEFAULT, NULL);
return (mod_install(&bootfs_modlinkage));
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&bootfs_modlinkage, modinfop));
}
int
_fini(void)
{
int err;
mutex_enter(&bootfs_lock);
if (bootfs_nactive > 0) {
mutex_exit(&bootfs_lock);
return (EBUSY);
}
mutex_exit(&bootfs_lock);
err = mod_remove(&bootfs_modlinkage);
if (err != 0)
return (err);
(void) vfs_freevfsops_by_type(bootfs_fstype);
vn_freevnodeops(bootfs_vnodeops);
id_space_destroy(bootfs_idspace);
mutex_destroy(&bootfs_lock);
kmem_cache_destroy(bootfs_node_cache);
return (err);
}
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright (c) 2015 Joyent, Inc. All rights reserved.
*/
/*
* bootfs vnode operations
*/
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/sunddi.h>
#include <sys/errno.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/mman.h>
#include <fs/fs_subr.h>
#include <sys/policy.h>
#include <sys/sysmacros.h>
#include <sys/dirent.h>
#include <sys/uio.h>
#include <vm/pvn.h>
#include <vm/hat.h>
#include <vm/seg_map.h>
#include <vm/seg_vn.h>
#include <sys/vmsystm.h>
#include <sys/fs/bootfs_impl.h>
struct vnodeops *bootfs_vnodeops;
/*ARGSUSED*/
static int
bootfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
{
return (0);
}
/*ARGSUSED*/
static int
bootfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
caller_context_t *ct)
{
return (0);
}
/*ARGSUSED*/
static int
bootfs_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
caller_context_t *ct)
{
int err;
ssize_t sres = uiop->uio_resid;
bootfs_node_t *bnp = vp->v_data;
if (vp->v_type == VDIR)
return (EISDIR);
if (vp->v_type != VREG)
return (EINVAL);
if (uiop->uio_loffset < 0)
return (EINVAL);
if (uiop->uio_loffset >= bnp->bvn_size)
return (0);
err = 0;
while (uiop->uio_resid != 0) {
caddr_t base;
long offset, frem;
ulong_t poff, segoff;
size_t bytes;
int relerr;
offset = uiop->uio_loffset;
poff = offset & PAGEOFFSET;
bytes = MIN(PAGESIZE - poff, uiop->uio_resid);
frem = bnp->bvn_size - offset;
if (frem <= 0) {
err = 0;
break;
}
/* Don't read past EOF */
bytes = MIN(bytes, frem);
/*
* Segmaps are likely larger than our page size, so make sure we
* have the proper offfset into the resulting segmap data.
*/
segoff = (offset & PAGEMASK) & MAXBOFFSET;
base = segmap_getmapflt(segkmap, vp, offset & MAXBMASK, bytes,
1, S_READ);
err = uiomove(base + segoff + poff, bytes, UIO_READ, uiop);
relerr = segmap_release(segkmap, base, 0);
if (err == 0)
err = relerr;
if (err != 0)
break;
}
/* Even if we had an error in a partial read, return success */
if (uiop->uio_resid > sres)
err = 0;
gethrestime(&bnp->bvn_attr.va_atime);
return (err);
}
/*ARGSUSED*/
static int
bootfs_ioctl(vnode_t *vp, int cmd, intptr_t data, int flag,
cred_t *cr, int *rvalp, caller_context_t *ct)
{
return (ENOTTY);
}
/*ARGSUSED*/
static int
bootfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
caller_context_t *ct)
{
uint32_t mask;
bootfs_node_t *bpn = (bootfs_node_t *)vp->v_data;
mask = vap->va_mask;
bcopy(&bpn->bvn_attr, vap, sizeof (vattr_t));
vap->va_mask = mask;
return (0);
}
/*ARGSUSED*/
static int
bootfs_access(vnode_t *vp, int mode, int flags, cred_t *cr,
caller_context_t *ct)
{
int shift = 0;
bootfs_node_t *bpn = (bootfs_node_t *)vp->v_data;
if (crgetuid(cr) != bpn->bvn_attr.va_uid) {
shift += 3;
if (groupmember(bpn->bvn_attr.va_gid, cr) == 0)
shift += 3;
}
return (secpolicy_vnode_access2(cr, vp, bpn->bvn_attr.va_uid,
bpn->bvn_attr.va_mode << shift, mode));
}
/*ARGSUSED*/
static int
bootfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
int *direntflags, pathname_t *realpnp)
{
avl_index_t where;
bootfs_node_t sn, *bnp;
bootfs_node_t *bpp = (bootfs_node_t *)dvp->v_data;
if (flags & LOOKUP_XATTR)
return (EINVAL);
if (bpp->bvn_attr.va_type != VDIR)
return (ENOTDIR);
if (*nm == '\0' || strcmp(nm, ".") == 0) {
VN_HOLD(dvp);
*vpp = dvp;
return (0);
}
if (strcmp(nm, "..") == 0) {
VN_HOLD(bpp->bvn_parent->bvn_vnp);
*vpp = bpp->bvn_parent->bvn_vnp;
return (0);
}
sn.bvn_name = nm;
bnp = avl_find(&bpp->bvn_dir, &sn, &where);
if (bnp == NULL)
return (ENOENT);
VN_HOLD(bnp->bvn_vnp);
*vpp = bnp->bvn_vnp;
return (0);
}
/*ARGSUSED*/
static int
bootfs_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
caller_context_t *ct, int flags)
{
bootfs_node_t *bnp = (bootfs_node_t *)vp->v_data;
dirent64_t *dp;
void *buf;
ulong_t bsize, brem;
offset_t coff, roff;
int dlen, ret;
bootfs_node_t *dnp;
boolean_t first = B_TRUE;
if (uiop->uio_loffset >= MAXOFF_T) {
if (eofp != NULL)
*eofp = 1;
return (0);
}
if (uiop->uio_iovcnt != 1)
return (EINVAL);
if (!(uiop->uio_iov->iov_len > 0))
return (EINVAL);
if (vp->v_type != VDIR)
return (ENOTDIR);
roff = uiop->uio_loffset;
coff = 0;
brem = bsize = uiop->uio_iov->iov_len;
buf = kmem_alloc(bsize, KM_SLEEP);
dp = buf;
/*
* Recall that offsets here are done based on the name of the dirent
* excluding the null terminator. Therefore `.` is always at 0, `..` is
* always at 1, and then the first real dirent is at 3. This offset is
* what's actually stored when we update the offset in the structure.
*/
if (roff == 0) {
dlen = DIRENT64_RECLEN(1);
if (first == B_TRUE) {
if (dlen > brem) {
kmem_free(buf, bsize);
return (EINVAL);
}
first = B_FALSE;
}
dp->d_ino = (ino64_t)bnp->bvn_attr.va_nodeid;
dp->d_off = 0;
dp->d_reclen = (ushort_t)dlen;
(void) strncpy(dp->d_name, ".", DIRENT64_NAMELEN(dlen));
dp = (struct dirent64 *)((uintptr_t)dp + dp->d_reclen);
brem -= dlen;
}
if (roff <= 1) {
dlen = DIRENT64_RECLEN(2);
if (first == B_TRUE) {
if (dlen > brem) {
kmem_free(buf, bsize);
return (EINVAL);
}
first = B_FALSE;
}
dp->d_ino = (ino64_t)bnp->bvn_parent->bvn_attr.va_nodeid;
dp->d_off = 1;
dp->d_reclen = (ushort_t)dlen;
(void) strncpy(dp->d_name, "..", DIRENT64_NAMELEN(dlen));
dp = (struct dirent64 *)((uintptr_t)dp + dp->d_reclen);
brem -= dlen;
}
coff = 3;
for (dnp = avl_first(&bnp->bvn_dir); dnp != NULL;
dnp = AVL_NEXT(&bnp->bvn_dir, dnp)) {
size_t nlen = strlen(dnp->bvn_name);
if (roff > coff) {
coff += nlen;
continue;
}
dlen = DIRENT64_RECLEN(nlen);
if (dlen > brem) {
if (first == B_TRUE) {
kmem_free(buf, bsize);
return (EINVAL);
}
break;
}
first = B_FALSE;
dp->d_ino = (ino64_t)dnp->bvn_attr.va_nodeid;
dp->d_off = coff;
dp->d_reclen = (ushort_t)dlen;
(void) strncpy(dp->d_name, dnp->bvn_name,
DIRENT64_NAMELEN(dlen));
dp = (struct dirent64 *)((uintptr_t)dp + dp->d_reclen);
brem -= dlen;
coff += nlen;
}
ret = uiomove(buf, (bsize - brem), UIO_READ, uiop);
if (ret == 0) {
if (dnp == NULL) {
coff++;
if (eofp != NULL)
*eofp = 1;
} else if (eofp != NULL) {
*eofp = 0;
}
uiop->uio_loffset = coff;
}
gethrestime(&bnp->bvn_attr.va_atime);
kmem_free(buf, bsize);
return (ret);
}
/*ARGSUSED*/
static void
bootfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
{
}
/*ARGSUSED*/
static int
bootfs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ct)
{
if (write_lock != 0)
return (EINVAL);
return (0);
}
/*ARGSUSED*/
static void
bootfs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ct)
{
}
/*ARGSUSED*/
static int
bootfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
caller_context_t *ct)
{
bootfs_node_t *bnp = (bootfs_node_t *)vp->v_data;
if (vp->v_type == VDIR)
return (0);
return ((*noffp < 0 || *noffp > bnp->bvn_size ? EINVAL : 0));
}
/*
* We need to fill in a single page of a vnode's memory based on the actual data
* from the kernel. We'll use this node's sliding window into physical memory
* and update one page at a time.
*/
/*ARGSUSED*/
static int
bootfs_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, enum seg_rw rw,
cred_t *cr)
{
bootfs_node_t *bnp = vp->v_data;
page_t *pp, *fpp;
pfn_t pfn;
for (;;) {
/* Easy case where the page exists */
pp = page_lookup(vp, off, rw == S_CREATE ? SE_EXCL : SE_SHARED);
if (pp != NULL) {
if (pl != NULL) {
pl[0] = pp;
pl[1] = NULL;
} else {
page_unlock(pp);
}
return (0);
}
pp = page_create_va(vp, off, PAGESIZE, PG_EXCL | PG_WAIT, seg,
addr);
/*
* If we didn't get the page, that means someone else beat us to
* creating this so we need to try again.
*/
if (pp != NULL)
break;
}
pfn = btop((bnp->bvn_addr + off) & PAGEMASK);
fpp = page_numtopp_nolock(pfn);
if (ppcopy(fpp, pp) == 0) {
pvn_read_done(pp, B_ERROR);
return (EIO);
}
if (pl != NULL) {
pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
} else {
pvn_io_done(pp);
}
return (0);
}
/*ARGSUSED*/
static int
bootfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, enum seg_rw rw,
cred_t *cr, caller_context_t *ct)
{
int err;
bootfs_node_t *bnp = vp->v_data;
if (off + len > bnp->bvn_size + PAGEOFFSET)
return (EFAULT);
if (protp != NULL)
*protp = PROT_ALL;
if (len <= PAGESIZE)
err = bootfs_getapage(vp, (u_offset_t)off, len, protp, pl,
plsz, seg, addr, rw, cr);
else
err = pvn_getpages(bootfs_getapage, vp, (u_offset_t)off, len,
protp, pl, plsz, seg, addr, rw, cr);
return (err);
}
/*ARGSUSED*/
static int
bootfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
caller_context_t *ct)
{
int ret;
segvn_crargs_t vn_a;
#ifdef _ILP32
if (len > MAXOFF_T)
return (ENOMEM);
#endif
if (vp->v_flag & VNOMAP)
return (ENOSYS);
if (off < 0 || off > MAXOFFSET_T - off)
return (ENXIO);
if (vp->v_type != VREG)
return (ENODEV);
if ((prot & PROT_WRITE) && (flags & MAP_SHARED))
return (ENOTSUP);
as_rangelock(as);
ret = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
if (ret != 0) {
as_rangeunlock(as);
return (ret);
}
vn_a.vp = vp;
vn_a.offset = (u_offset_t)off;
vn_a.type = flags & MAP_TYPE;
vn_a.prot = prot;
vn_a.maxprot = maxprot;
vn_a.cred = cr;
vn_a.amp = NULL;
vn_a.flags = flags & ~MAP_TYPE;
vn_a.szc = 0;
vn_a.lgrp_mem_policy_flags = 0;
ret = as_map(as, *addrp, len, segvn_create, &vn_a);
as_rangeunlock(as);
return (ret);
}
/*ARGSUSED*/
static int
bootfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
caller_context_t *ct)
{
return (0);
}
/*ARGSUSED*/
static int
bootfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
caller_context_t *ct)
{
return (0);
}
static int
bootfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
caller_context_t *ct)
{
int ret;
switch (cmd) {
case _PC_TIMESTAMP_RESOLUTION:
*valp = 1L;
ret = 0;
break;
default:
ret = fs_pathconf(vp, cmd, valp, cr, ct);
}
return (ret);
}
const fs_operation_def_t bootfs_vnodeops_template[] = {
VOPNAME_OPEN, { .vop_open = bootfs_open },
VOPNAME_CLOSE, { .vop_close = bootfs_close },
VOPNAME_READ, { .vop_read = bootfs_read },
VOPNAME_IOCTL, { .vop_ioctl = bootfs_ioctl },
VOPNAME_GETATTR, { .vop_getattr = bootfs_getattr },
VOPNAME_ACCESS, { .vop_access = bootfs_access },
VOPNAME_LOOKUP, { .vop_lookup = bootfs_lookup },
VOPNAME_READDIR, { .vop_readdir = bootfs_readdir },
VOPNAME_INACTIVE, { .vop_inactive = bootfs_inactive },
VOPNAME_RWLOCK, { .vop_rwlock = bootfs_rwlock },
VOPNAME_RWUNLOCK, { .vop_rwunlock = bootfs_rwunlock },
VOPNAME_SEEK, { .vop_seek = bootfs_seek },
VOPNAME_GETPAGE, { .vop_getpage = bootfs_getpage },
VOPNAME_MAP, { .vop_map = bootfs_map },
VOPNAME_ADDMAP, { .vop_addmap = bootfs_addmap },
VOPNAME_DELMAP, { .vop_delmap = bootfs_delmap },
VOPNAME_PATHCONF, { .vop_pathconf = bootfs_pathconf },
VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_nosupport },
NULL, NULL
};
|