1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/vnode.h>
#include <sys/buf.h>
#include <sys/errno.h>
#include <sys/fssnap_if.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_filio.h>
#include <sys/sysmacros.h>
#include <sys/modctl.h>
#include <sys/fs/ufs_log.h>
#include <sys/fs/ufs_bio.h>
#include <sys/fs/ufs_fsdir.h>
#include <sys/debug.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/inttypes.h>
#include <sys/vfs.h>
#include <sys/mntent.h>
#include <sys/conf.h>
#include <sys/param.h>
#include <sys/kstat.h>
#include <sys/cmn_err.h>
#include <sys/sdt.h>
#define LUFS_GENID_PRIME UINT64_C(4294967291)
#define LUFS_GENID_BASE UINT64_C(311)
#define LUFS_NEXT_ID(id) ((uint32_t)(((id) * LUFS_GENID_BASE) % \
LUFS_GENID_PRIME))
extern kmutex_t ufs_scan_lock;
static kmutex_t log_mutex; /* general purpose log layer lock */
kmutex_t ml_scan; /* Scan thread syncronization */
kcondvar_t ml_scan_cv; /* Scan thread syncronization */
struct kmem_cache *lufs_sv;
struct kmem_cache *lufs_bp;
/* Tunables */
uint_t ldl_maxlogsize = LDL_MAXLOGSIZE;
uint_t ldl_minlogsize = LDL_MINLOGSIZE;
uint_t ldl_softlogcap = LDL_SOFTLOGCAP;
uint32_t ldl_divisor = LDL_DIVISOR;
uint32_t ldl_mintransfer = LDL_MINTRANSFER;
uint32_t ldl_maxtransfer = LDL_MAXTRANSFER;
uint32_t ldl_minbufsize = LDL_MINBUFSIZE;
uint32_t ldl_cgsizereq = 0;
/* Generation of header ids */
static kmutex_t genid_mutex;
static uint32_t last_loghead_ident = UINT32_C(0);
/*
* Logging delta and roll statistics
*/
struct delta_kstats {
kstat_named_t ds_superblock_deltas;
kstat_named_t ds_bitmap_deltas;
kstat_named_t ds_suminfo_deltas;
kstat_named_t ds_allocblk_deltas;
kstat_named_t ds_ab0_deltas;
kstat_named_t ds_dir_deltas;
kstat_named_t ds_inode_deltas;
kstat_named_t ds_fbiwrite_deltas;
kstat_named_t ds_quota_deltas;
kstat_named_t ds_shadow_deltas;
kstat_named_t ds_superblock_rolled;
kstat_named_t ds_bitmap_rolled;
kstat_named_t ds_suminfo_rolled;
kstat_named_t ds_allocblk_rolled;
kstat_named_t ds_ab0_rolled;
kstat_named_t ds_dir_rolled;
kstat_named_t ds_inode_rolled;
kstat_named_t ds_fbiwrite_rolled;
kstat_named_t ds_quota_rolled;
kstat_named_t ds_shadow_rolled;
} dkstats = {
{ "superblock_deltas", KSTAT_DATA_UINT64 },
{ "bitmap_deltas", KSTAT_DATA_UINT64 },
{ "suminfo_deltas", KSTAT_DATA_UINT64 },
{ "allocblk_deltas", KSTAT_DATA_UINT64 },
{ "ab0_deltas", KSTAT_DATA_UINT64 },
{ "dir_deltas", KSTAT_DATA_UINT64 },
{ "inode_deltas", KSTAT_DATA_UINT64 },
{ "fbiwrite_deltas", KSTAT_DATA_UINT64 },
{ "quota_deltas", KSTAT_DATA_UINT64 },
{ "shadow_deltas", KSTAT_DATA_UINT64 },
{ "superblock_rolled", KSTAT_DATA_UINT64 },
{ "bitmap_rolled", KSTAT_DATA_UINT64 },
{ "suminfo_rolled", KSTAT_DATA_UINT64 },
{ "allocblk_rolled", KSTAT_DATA_UINT64 },
{ "ab0_rolled", KSTAT_DATA_UINT64 },
{ "dir_rolled", KSTAT_DATA_UINT64 },
{ "inode_rolled", KSTAT_DATA_UINT64 },
{ "fbiwrite_rolled", KSTAT_DATA_UINT64 },
{ "quota_rolled", KSTAT_DATA_UINT64 },
{ "shadow_rolled", KSTAT_DATA_UINT64 }
};
uint64_t delta_stats[DT_MAX];
uint64_t roll_stats[DT_MAX];
/*
* General logging kstats
*/
struct logstats logstats = {
{ "master_reads", KSTAT_DATA_UINT64 },
{ "master_writes", KSTAT_DATA_UINT64 },
{ "log_reads_inmem", KSTAT_DATA_UINT64 },
{ "log_reads", KSTAT_DATA_UINT64 },
{ "log_writes", KSTAT_DATA_UINT64 },
{ "log_master_reads", KSTAT_DATA_UINT64 },
{ "log_roll_reads", KSTAT_DATA_UINT64 },
{ "log_roll_writes", KSTAT_DATA_UINT64 }
};
int
trans_not_done(struct buf *cb)
{
sema_v(&cb->b_io);
return (0);
}
static void
trans_wait_panic(struct buf *cb)
{
while ((cb->b_flags & B_DONE) == 0)
drv_usecwait(10);
}
int
trans_not_wait(struct buf *cb)
{
/*
* In case of panic, busy wait for completion
*/
if (panicstr)
trans_wait_panic(cb);
else
sema_p(&cb->b_io);
return (geterror(cb));
}
int
trans_wait(struct buf *cb)
{
/*
* In case of panic, busy wait for completion and run md daemon queues
*/
if (panicstr)
trans_wait_panic(cb);
return (biowait(cb));
}
static void
setsum(int32_t *sp, int32_t *lp, int nb)
{
int32_t csum = 0;
*sp = 0;
nb /= sizeof (int32_t);
while (nb--)
csum += *lp++;
*sp = csum;
}
static int
checksum(int32_t *sp, int32_t *lp, int nb)
{
int32_t ssum = *sp;
setsum(sp, lp, nb);
if (ssum != *sp) {
*sp = ssum;
return (0);
}
return (1);
}
void
lufs_unsnarf(ufsvfs_t *ufsvfsp)
{
ml_unit_t *ul;
mt_map_t *mtm;
ul = ufsvfsp->vfs_log;
if (ul == NULL)
return;
mtm = ul->un_logmap;
/*
* Wait for a pending top_issue_sync which is
* dispatched (via taskq_dispatch()) but hasnt completed yet.
*/
mutex_enter(&mtm->mtm_lock);
while (mtm->mtm_taskq_sync_count != 0) {
cv_wait(&mtm->mtm_cv, &mtm->mtm_lock);
}
mutex_exit(&mtm->mtm_lock);
/* Roll committed transactions */
logmap_roll_dev(ul);
/* Kill the roll thread */
logmap_kill_roll(ul);
/* release saved alloction info */
if (ul->un_ebp)
kmem_free(ul->un_ebp, ul->un_nbeb);
/* release circular bufs */
free_cirbuf(&ul->un_rdbuf);
free_cirbuf(&ul->un_wrbuf);
/* release maps */
if (ul->un_logmap)
ul->un_logmap = map_put(ul->un_logmap);
if (ul->un_deltamap)
ul->un_deltamap = map_put(ul->un_deltamap);
if (ul->un_matamap)
ul->un_matamap = map_put(ul->un_matamap);
mutex_destroy(&ul->un_log_mutex);
mutex_destroy(&ul->un_state_mutex);
/* release state buffer MUST BE LAST!! (contains our ondisk data) */
if (ul->un_bp)
brelse(ul->un_bp);
kmem_free(ul, sizeof (*ul));
ufsvfsp->vfs_log = NULL;
}
int
lufs_snarf(ufsvfs_t *ufsvfsp, struct fs *fs, int ronly)
{
buf_t *bp, *tbp;
ml_unit_t *ul;
extent_block_t *ebp;
ic_extent_block_t *nebp;
size_t nb;
daddr_t bno; /* in disk blocks */
int i;
/* LINTED: warning: logical expression always true: op "||" */
ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
/*
* Get the allocation table
* During a remount the superblock pointed to by the ufsvfsp
* is out of date. Hence the need for the ``new'' superblock
* pointer, fs, passed in as a parameter.
*/
bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, fs->fs_logbno),
fs->fs_bsize);
if (bp->b_flags & B_ERROR) {
brelse(bp);
return (EIO);
}
ebp = (void *)bp->b_un.b_addr;
if (!checksum(&ebp->chksum, (int32_t *)bp->b_un.b_addr,
fs->fs_bsize)) {
brelse(bp);
return (ENODEV);
}
/*
* It is possible to get log blocks with all zeros.
* We should also check for nextents to be zero in such case.
*/
if (ebp->type != LUFS_EXTENTS || ebp->nextents == 0) {
brelse(bp);
return (EDOM);
}
/*
* Put allocation into memory. This requires conversion between
* on the ondisk format of the extent (type extent_t) and the
* in-core format of the extent (type ic_extent_t). The
* difference is the in-core form of the extent block stores
* the physical offset of the extent in disk blocks, which
* can require more than a 32-bit field.
*/
nb = (size_t)(sizeof (ic_extent_block_t) +
((ebp->nextents - 1) * sizeof (ic_extent_t)));
nebp = kmem_alloc(nb, KM_SLEEP);
nebp->ic_nextents = ebp->nextents;
nebp->ic_nbytes = ebp->nbytes;
nebp->ic_nextbno = ebp->nextbno;
for (i = 0; i < ebp->nextents; i++) {
nebp->ic_extents[i].ic_lbno = ebp->extents[i].lbno;
nebp->ic_extents[i].ic_nbno = ebp->extents[i].nbno;
nebp->ic_extents[i].ic_pbno =
logbtodb(fs, ebp->extents[i].pbno);
}
brelse(bp);
/*
* Get the log state
*/
bno = nebp->ic_extents[0].ic_pbno;
bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, DEV_BSIZE);
if (bp->b_flags & B_ERROR) {
brelse(bp);
bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno + 1, DEV_BSIZE);
if (bp->b_flags & B_ERROR) {
brelse(bp);
kmem_free(nebp, nb);
return (EIO);
}
}
/*
* Put ondisk struct into an anonymous buffer
* This buffer will contain the memory for the ml_odunit struct
*/
tbp = ngeteblk(dbtob(LS_SECTORS));
tbp->b_edev = bp->b_edev;
tbp->b_dev = bp->b_dev;
tbp->b_blkno = bno;
bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
bp->b_flags |= (B_STALE | B_AGE);
brelse(bp);
bp = tbp;
/*
* Verify the log state
*
* read/only mounts w/bad logs are allowed. umount will
* eventually roll the bad log until the first IO error.
* fsck will then repair the file system.
*
* read/write mounts with bad logs are not allowed.
*
*/
ul = (ml_unit_t *)kmem_zalloc(sizeof (*ul), KM_SLEEP);
bcopy(bp->b_un.b_addr, &ul->un_ondisk, sizeof (ml_odunit_t));
if ((ul->un_chksum != ul->un_head_ident + ul->un_tail_ident) ||
(ul->un_version != LUFS_VERSION_LATEST) ||
(!ronly && ul->un_badlog)) {
kmem_free(ul, sizeof (*ul));
brelse(bp);
kmem_free(nebp, nb);
return (EIO);
}
/*
* Initialize the incore-only fields
*/
if (ronly)
ul->un_flags |= LDL_NOROLL;
ul->un_bp = bp;
ul->un_ufsvfs = ufsvfsp;
ul->un_dev = ufsvfsp->vfs_dev;
ul->un_ebp = nebp;
ul->un_nbeb = nb;
ul->un_maxresv = btodb(ul->un_logsize) * LDL_USABLE_BSIZE;
ul->un_deltamap = map_get(ul, deltamaptype, DELTAMAP_NHASH);
ul->un_logmap = map_get(ul, logmaptype, LOGMAP_NHASH);
if (ul->un_debug & MT_MATAMAP)
ul->un_matamap = map_get(ul, matamaptype, DELTAMAP_NHASH);
mutex_init(&ul->un_log_mutex, NULL, MUTEX_DEFAULT, NULL);
mutex_init(&ul->un_state_mutex, NULL, MUTEX_DEFAULT, NULL);
/*
* Aquire the ufs_scan_lock before linking the mtm data
* structure so that we keep ufs_sync() and ufs_update() away
* when they execute the ufs_scan_inodes() run while we're in
* progress of enabling/disabling logging.
*/
mutex_enter(&ufs_scan_lock);
ufsvfsp->vfs_log = ul;
/* remember the state of the log before the log scan */
logmap_logscan(ul);
mutex_exit(&ufs_scan_lock);
/*
* Error during scan
*
* If this is a read/only mount; ignore the error.
* At a later time umount/fsck will repair the fs.
*
*/
if (ul->un_flags & LDL_ERROR) {
if (!ronly) {
/*
* Aquire the ufs_scan_lock before de-linking
* the mtm data structure so that we keep ufs_sync()
* and ufs_update() away when they execute the
* ufs_scan_inodes() run while we're in progress of
* enabling/disabling logging.
*/
mutex_enter(&ufs_scan_lock);
lufs_unsnarf(ufsvfsp);
mutex_exit(&ufs_scan_lock);
return (EIO);
}
ul->un_flags &= ~LDL_ERROR;
}
if (!ronly)
logmap_start_roll(ul);
return (0);
}
uint32_t
lufs_hd_genid(const ml_unit_t *up)
{
uint32_t id;
mutex_enter(&genid_mutex);
/*
* The formula below implements an exponential, modular sequence.
*
* ID(N) = (SEED * (BASE^N)) % PRIME
*
* The numbers will be pseudo random. They depend on SEED, BASE, PRIME,
* but will sweep through almost all of the range 1....PRIME-1.
* Most importantly they will not repeat for PRIME-2 (4294967289)
* repetitions. If they would repeat that could possibly cause hangs,
* panics at mount/umount and failed mount operations.
*/
id = LUFS_NEXT_ID(last_loghead_ident);
/* Checking if new identity used already */
if (up != NULL && up->un_head_ident == id) {
DTRACE_PROBE1(head_ident_collision, uint32_t, id);
/*
* The following preserves the algorithm for the fix for
* "panic: free: freeing free frag, dev:0x2000000018, blk:34605,
* cg:26, ino:148071,".
* If the header identities un_head_ident are equal to the
* present element in the sequence, the next element of the
* sequence is returned instead.
*/
id = LUFS_NEXT_ID(id);
}
last_loghead_ident = id;
mutex_exit(&genid_mutex);
return (id);
}
static void
lufs_genid_init(void)
{
uint64_t seed;
/* Initialization */
mutex_init(&genid_mutex, NULL, MUTEX_DEFAULT, NULL);
/* Seed the algorithm */
do {
timestruc_t tv;
gethrestime(&tv);
seed = (tv.tv_nsec << 3);
seed ^= tv.tv_sec;
last_loghead_ident = (uint32_t)(seed % LUFS_GENID_PRIME);
} while (last_loghead_ident == UINT32_C(0));
}
static int
lufs_initialize(
ufsvfs_t *ufsvfsp,
daddr_t bno,
size_t nb,
struct fiolog *flp)
{
ml_odunit_t *ud, *ud2;
buf_t *bp;
/* LINTED: warning: logical expression always true: op "||" */
ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
ASSERT(nb >= ldl_minlogsize);
bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev, bno, dbtob(LS_SECTORS));
bzero(bp->b_un.b_addr, bp->b_bcount);
ud = (void *)bp->b_un.b_addr;
ud->od_version = LUFS_VERSION_LATEST;
ud->od_maxtransfer = MIN(ufsvfsp->vfs_iotransz, ldl_maxtransfer);
if (ud->od_maxtransfer < ldl_mintransfer)
ud->od_maxtransfer = ldl_mintransfer;
ud->od_devbsize = DEV_BSIZE;
ud->od_requestsize = flp->nbytes_actual;
ud->od_statesize = dbtob(LS_SECTORS);
ud->od_logsize = nb - ud->od_statesize;
ud->od_statebno = INT32_C(0);
ud->od_head_ident = lufs_hd_genid(NULL);
ud->od_tail_ident = ud->od_head_ident;
ud->od_chksum = ud->od_head_ident + ud->od_tail_ident;
ud->od_bol_lof = dbtob(ud->od_statebno) + ud->od_statesize;
ud->od_eol_lof = ud->od_bol_lof + ud->od_logsize;
ud->od_head_lof = ud->od_bol_lof;
ud->od_tail_lof = ud->od_bol_lof;
ASSERT(lufs_initialize_debug(ud));
ud2 = (void *)(bp->b_un.b_addr + DEV_BSIZE);
bcopy(ud, ud2, sizeof (*ud));
UFS_BWRITE2(ufsvfsp, bp);
if (bp->b_flags & B_ERROR) {
brelse(bp);
return (EIO);
}
brelse(bp);
return (0);
}
/*
* Free log space
* Assumes the file system is write locked and is not logging
*/
static int
lufs_free(struct ufsvfs *ufsvfsp)
{
int error = 0, i, j;
buf_t *bp = NULL;
extent_t *ep;
extent_block_t *ebp;
struct fs *fs = ufsvfsp->vfs_fs;
daddr_t fno;
int32_t logbno;
long nfno;
inode_t *ip = NULL;
char clean;
/*
* Nothing to free
*/
if (fs->fs_logbno == 0)
return (0);
/*
* Mark the file system as FSACTIVE and no log but honor the
* current value of fs_reclaim. The reclaim thread could have
* been active when lufs_disable() was called and if fs_reclaim
* is reset to zero here it could lead to lost inodes.
*/
ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
mutex_enter(&ufsvfsp->vfs_lock);
clean = fs->fs_clean;
logbno = fs->fs_logbno;
fs->fs_clean = FSACTIVE;
fs->fs_logbno = INT32_C(0);
ufs_sbwrite(ufsvfsp);
mutex_exit(&ufsvfsp->vfs_lock);
ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
error = EIO;
fs->fs_clean = clean;
fs->fs_logbno = logbno;
goto errout;
}
/*
* fetch the allocation block
* superblock -> one block of extents -> log data
*/
bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, logbno),
fs->fs_bsize);
if (bp->b_flags & B_ERROR) {
error = EIO;
goto errout;
}
/*
* Free up the allocated space (dummy inode needed for free())
*/
ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
ebp = (void *)bp->b_un.b_addr;
for (i = 0, ep = &ebp->extents[0]; i < ebp->nextents; ++i, ++ep) {
fno = logbtofrag(fs, ep->pbno);
nfno = dbtofsb(fs, ep->nbno);
for (j = 0; j < nfno; j += fs->fs_frag, fno += fs->fs_frag)
free(ip, fno, fs->fs_bsize, 0);
}
free(ip, logbtofrag(fs, logbno), fs->fs_bsize, 0);
brelse(bp);
bp = NULL;
/*
* Push the metadata dirtied during the allocations
*/
ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
sbupdate(ufsvfsp->vfs_vfs);
ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
bflush(ufsvfsp->vfs_dev);
error = bfinval(ufsvfsp->vfs_dev, 0);
if (error)
goto errout;
/*
* Free the dummy inode
*/
ufs_free_inode(ip);
return (0);
errout:
/*
* Free up all resources
*/
if (bp)
brelse(bp);
if (ip)
ufs_free_inode(ip);
return (error);
}
/*
* Allocate log space
* Assumes the file system is write locked and is not logging
*/
static int
lufs_alloc(struct ufsvfs *ufsvfsp, struct fiolog *flp, size_t minb, cred_t *cr)
{
int error = 0;
buf_t *bp = NULL;
extent_t *ep, *nep;
extent_block_t *ebp;
struct fs *fs = ufsvfsp->vfs_fs;
daddr_t fno; /* in frags */
daddr_t bno; /* in disk blocks */
int32_t logbno = INT32_C(0); /* will be fs_logbno */
struct inode *ip = NULL;
size_t nb = flp->nbytes_actual;
size_t tb = 0;
/*
* Mark the file system as FSACTIVE
*/
ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
mutex_enter(&ufsvfsp->vfs_lock);
fs->fs_clean = FSACTIVE;
ufs_sbwrite(ufsvfsp);
mutex_exit(&ufsvfsp->vfs_lock);
ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
/*
* Allocate the allocation block (need dummy shadow inode;
* we use a shadow inode so the quota sub-system ignores
* the block allocations.)
* superblock -> one block of extents -> log data
*/
ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
ip->i_mode = IFSHAD; /* make the dummy a shadow inode */
rw_enter(&ip->i_contents, RW_WRITER);
fno = contigpref(ufsvfsp, nb + fs->fs_bsize, minb);
error = alloc(ip, fno, fs->fs_bsize, &fno, cr);
if (error)
goto errout;
bno = fsbtodb(fs, fno);
bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, fs->fs_bsize);
if (bp->b_flags & B_ERROR) {
error = EIO;
goto errout;
}
ebp = (void *)bp->b_un.b_addr;
ebp->type = LUFS_EXTENTS;
ebp->nextbno = UINT32_C(0);
ebp->nextents = UINT32_C(0);
ebp->chksum = INT32_C(0);
if (fs->fs_magic == FS_MAGIC)
logbno = bno;
else
logbno = dbtofsb(fs, bno);
/*
* Initialize the first extent
*/
ep = &ebp->extents[0];
error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
if (error)
goto errout;
bno = fsbtodb(fs, fno);
ep->lbno = UINT32_C(0);
if (fs->fs_magic == FS_MAGIC)
ep->pbno = (uint32_t)bno;
else
ep->pbno = (uint32_t)fno;
ep->nbno = (uint32_t)fsbtodb(fs, fs->fs_frag);
ebp->nextents = UINT32_C(1);
tb = fs->fs_bsize;
nb -= fs->fs_bsize;
while (nb) {
error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
if (error) {
if (tb < minb)
goto errout;
error = 0;
break;
}
bno = fsbtodb(fs, fno);
if ((daddr_t)((logbtodb(fs, ep->pbno) + ep->nbno) == bno))
ep->nbno += (uint32_t)(fsbtodb(fs, fs->fs_frag));
else {
nep = ep + 1;
if ((caddr_t)(nep + 1) >
(bp->b_un.b_addr + fs->fs_bsize)) {
free(ip, fno, fs->fs_bsize, 0);
break;
}
nep->lbno = ep->lbno + ep->nbno;
if (fs->fs_magic == FS_MAGIC)
nep->pbno = (uint32_t)bno;
else
nep->pbno = (uint32_t)fno;
nep->nbno = (uint32_t)(fsbtodb(fs, fs->fs_frag));
ebp->nextents++;
ep = nep;
}
tb += fs->fs_bsize;
nb -= fs->fs_bsize;
}
if (tb < minb) { /* Failed to reach minimum log size */
error = ENOSPC;
goto errout;
}
ebp->nbytes = (uint32_t)tb;
setsum(&ebp->chksum, (int32_t *)bp->b_un.b_addr, fs->fs_bsize);
UFS_BWRITE2(ufsvfsp, bp);
if (bp->b_flags & B_ERROR) {
error = EIO;
goto errout;
}
/*
* Initialize the first two sectors of the log
*/
error = lufs_initialize(ufsvfsp, logbtodb(fs, ebp->extents[0].pbno),
tb, flp);
if (error)
goto errout;
/*
* We are done initializing the allocation block and the log
*/
brelse(bp);
bp = NULL;
/*
* Update the superblock and push the dirty metadata
*/
ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
sbupdate(ufsvfsp->vfs_vfs);
ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
bflush(ufsvfsp->vfs_dev);
error = bfinval(ufsvfsp->vfs_dev, 1);
if (error)
goto errout;
if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
error = EIO;
goto errout;
}
/*
* Everything is safely on disk; update log space pointer in sb
*/
ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
mutex_enter(&ufsvfsp->vfs_lock);
fs->fs_logbno = (uint32_t)logbno;
ufs_sbwrite(ufsvfsp);
mutex_exit(&ufsvfsp->vfs_lock);
ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
/*
* Free the dummy inode
*/
rw_exit(&ip->i_contents);
ufs_free_inode(ip);
/* inform user of real log size */
flp->nbytes_actual = tb;
return (0);
errout:
/*
* Free all resources
*/
if (bp)
brelse(bp);
if (logbno) {
fs->fs_logbno = logbno;
(void) lufs_free(ufsvfsp);
}
if (ip) {
rw_exit(&ip->i_contents);
ufs_free_inode(ip);
}
return (error);
}
/*
* Disable logging
*/
int
lufs_disable(vnode_t *vp, struct fiolog *flp)
{
int error = 0;
inode_t *ip = VTOI(vp);
ufsvfs_t *ufsvfsp = ip->i_ufsvfs;
struct fs *fs = ufsvfsp->vfs_fs;
struct lockfs lf;
struct ulockfs *ulp;
flp->error = FIOLOG_ENONE;
/*
* Logging is already disabled; done
*/
if (fs->fs_logbno == 0 || ufsvfsp->vfs_log == NULL)
return (0);
/*
* Readonly file system
*/
if (fs->fs_ronly) {
flp->error = FIOLOG_EROFS;
return (0);
}
/*
* File system must be write locked to disable logging
*/
error = ufs_fiolfss(vp, &lf);
if (error) {
return (error);
}
if (!LOCKFS_IS_ULOCK(&lf)) {
flp->error = FIOLOG_EULOCK;
return (0);
}
lf.lf_lock = LOCKFS_WLOCK;
lf.lf_flags = 0;
lf.lf_comment = NULL;
error = ufs_fiolfs(vp, &lf, 1);
if (error) {
flp->error = FIOLOG_EWLOCK;
return (0);
}
if (ufsvfsp->vfs_log == NULL || fs->fs_logbno == 0)
goto errout;
/*
* WE ARE COMMITTED TO DISABLING LOGGING PAST THIS POINT
*/
/*
* Disable logging:
* Suspend the reclaim thread and force the delete thread to exit.
* When a nologging mount has completed there may still be
* work for reclaim to do so just suspend this thread until
* it's [deadlock-] safe for it to continue. The delete
* thread won't be needed as ufs_iinactive() calls
* ufs_delete() when logging is disabled.
* Freeze and drain reader ops.
* Commit any outstanding reader transactions (ufs_flush).
* Set the ``unmounted'' bit in the ufstrans struct.
* If debug, remove metadata from matamap.
* Disable matamap processing.
* NULL the trans ops table.
* Free all of the incore structs related to logging.
* Allow reader ops.
*/
ufs_thread_suspend(&ufsvfsp->vfs_reclaim);
ufs_thread_exit(&ufsvfsp->vfs_delete);
vfs_lock_wait(ufsvfsp->vfs_vfs);
ulp = &ufsvfsp->vfs_ulockfs;
mutex_enter(&ulp->ul_lock);
atomic_inc_ulong(&ufs_quiesce_pend);
(void) ufs_quiesce(ulp);
(void) ufs_flush(ufsvfsp->vfs_vfs);
TRANS_MATA_UMOUNT(ufsvfsp);
ufsvfsp->vfs_domatamap = 0;
/*
* Free all of the incore structs
* Aquire the ufs_scan_lock before de-linking the mtm data
* structure so that we keep ufs_sync() and ufs_update() away
* when they execute the ufs_scan_inodes() run while we're in
* progress of enabling/disabling logging.
*/
mutex_enter(&ufs_scan_lock);
(void) lufs_unsnarf(ufsvfsp);
mutex_exit(&ufs_scan_lock);
atomic_dec_ulong(&ufs_quiesce_pend);
mutex_exit(&ulp->ul_lock);
vfs_setmntopt(ufsvfsp->vfs_vfs, MNTOPT_NOLOGGING, NULL, 0);
vfs_unlock(ufsvfsp->vfs_vfs);
fs->fs_rolled = FS_ALL_ROLLED;
ufsvfsp->vfs_nolog_si = 0;
/*
* Free the log space and mark the superblock as FSACTIVE
*/
(void) lufs_free(ufsvfsp);
/*
* Allow the reclaim thread to continue.
*/
ufs_thread_continue(&ufsvfsp->vfs_reclaim);
/*
* Unlock the file system
*/
lf.lf_lock = LOCKFS_ULOCK;
lf.lf_flags = 0;
error = ufs_fiolfs(vp, &lf, 1);
if (error)
flp->error = FIOLOG_ENOULOCK;
return (0);
errout:
lf.lf_lock = LOCKFS_ULOCK;
lf.lf_flags = 0;
(void) ufs_fiolfs(vp, &lf, 1);
return (error);
}
/*
* Enable logging
*/
int
lufs_enable(struct vnode *vp, struct fiolog *flp, cred_t *cr)
{
int error;
int reclaim;
inode_t *ip = VTOI(vp);
ufsvfs_t *ufsvfsp = ip->i_ufsvfs;
struct fs *fs;
ml_unit_t *ul;
struct lockfs lf;
struct ulockfs *ulp;
vfs_t *vfsp = ufsvfsp->vfs_vfs;
uint64_t tmp_nbytes_actual;
uint64_t cg_minlogsize;
uint32_t cgsize;
static int minlogsizewarn = 0;
static int maxlogsizewarn = 0;
/*
* Check if logging is already enabled
*/
if (ufsvfsp->vfs_log) {
flp->error = FIOLOG_ETRANS;
/* for root ensure logging option is set */
vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
return (0);
}
fs = ufsvfsp->vfs_fs;
/*
* Come back here to recheck if we had to disable the log.
*/
recheck:
error = 0;
reclaim = 0;
flp->error = FIOLOG_ENONE;
/*
* The size of the ufs log is determined using the following rules:
*
* 1) If no size is requested the log size is calculated as a
* ratio of the total file system size. By default this is
* 1MB of log per 1GB of file system. This calculation is then
* capped at the log size specified by ldl_softlogcap.
* 2) The log size requested may then be increased based on the
* number of cylinder groups contained in the file system.
* To prevent a hang the log has to be large enough to contain a
* single transaction that alters every cylinder group in the file
* system. This is calculated as cg_minlogsize.
* 3) Finally a check is made that the log size requested is within
* the limits of ldl_minlogsize and ldl_maxlogsize.
*/
/*
* Adjust requested log size
*/
flp->nbytes_actual = flp->nbytes_requested;
if (flp->nbytes_actual == 0) {
tmp_nbytes_actual =
(((uint64_t)fs->fs_size) / ldl_divisor) << fs->fs_fshift;
flp->nbytes_actual = (uint_t)MIN(tmp_nbytes_actual, INT_MAX);
/*
* The 1MB per 1GB log size allocation only applies up to
* ldl_softlogcap size of log.
*/
flp->nbytes_actual = MIN(flp->nbytes_actual, ldl_softlogcap);
}
cgsize = ldl_cgsizereq ? ldl_cgsizereq : LDL_CGSIZEREQ(fs);
/*
* Determine the log size required based on the number of cylinder
* groups in the file system. The log has to be at least this size
* to prevent possible hangs due to log space exhaustion.
*/
cg_minlogsize = cgsize * fs->fs_ncg;
/*
* Ensure that the minimum log size isn't so small that it could lead
* to a full log hang.
*/
if (ldl_minlogsize < LDL_MINLOGSIZE) {
ldl_minlogsize = LDL_MINLOGSIZE;
if (!minlogsizewarn) {
cmn_err(CE_WARN, "ldl_minlogsize too small, increasing "
"to 0x%x", LDL_MINLOGSIZE);
minlogsizewarn = 1;
}
}
/*
* Ensure that the maximum log size isn't greater than INT_MAX as the
* logical log offset fields would overflow.
*/
if (ldl_maxlogsize > INT_MAX) {
ldl_maxlogsize = INT_MAX;
if (!maxlogsizewarn) {
cmn_err(CE_WARN, "ldl_maxlogsize too large, reducing "
"to 0x%x", INT_MAX);
maxlogsizewarn = 1;
}
}
if (cg_minlogsize > ldl_maxlogsize) {
cmn_err(CE_WARN,
"%s: reducing calculated log size from 0x%x to "
"ldl_maxlogsize (0x%x).", fs->fs_fsmnt, (int)cg_minlogsize,
ldl_maxlogsize);
}
cg_minlogsize = MAX(cg_minlogsize, ldl_minlogsize);
cg_minlogsize = MIN(cg_minlogsize, ldl_maxlogsize);
flp->nbytes_actual = MAX(flp->nbytes_actual, cg_minlogsize);
flp->nbytes_actual = MAX(flp->nbytes_actual, ldl_minlogsize);
flp->nbytes_actual = MIN(flp->nbytes_actual, ldl_maxlogsize);
flp->nbytes_actual = blkroundup(fs, flp->nbytes_actual);
/*
* logging is enabled and the log is the right size; done
*/
ul = ufsvfsp->vfs_log;
if (ul && fs->fs_logbno && (flp->nbytes_actual == ul->un_requestsize))
return (0);
/*
* Readonly file system
*/
if (fs->fs_ronly) {
flp->error = FIOLOG_EROFS;
return (0);
}
/*
* File system must be write locked to enable logging
*/
error = ufs_fiolfss(vp, &lf);
if (error) {
return (error);
}
if (!LOCKFS_IS_ULOCK(&lf)) {
flp->error = FIOLOG_EULOCK;
return (0);
}
lf.lf_lock = LOCKFS_WLOCK;
lf.lf_flags = 0;
lf.lf_comment = NULL;
error = ufs_fiolfs(vp, &lf, 1);
if (error) {
flp->error = FIOLOG_EWLOCK;
return (0);
}
/*
* Grab appropriate locks to synchronize with the rest
* of the system
*/
vfs_lock_wait(vfsp);
ulp = &ufsvfsp->vfs_ulockfs;
mutex_enter(&ulp->ul_lock);
/*
* File system must be fairly consistent to enable logging
*/
if (fs->fs_clean != FSLOG &&
fs->fs_clean != FSACTIVE &&
fs->fs_clean != FSSTABLE &&
fs->fs_clean != FSCLEAN) {
flp->error = FIOLOG_ECLEAN;
goto unlockout;
}
/*
* A write-locked file system is only active if there are
* open deleted files; so remember to set FS_RECLAIM later.
*/
if (fs->fs_clean == FSACTIVE)
reclaim = FS_RECLAIM;
/*
* Logging is already enabled; must be changing the log's size
*/
if (fs->fs_logbno && ufsvfsp->vfs_log) {
/*
* Before we can disable logging, we must give up our
* lock. As a consequence of unlocking and disabling the
* log, the fs structure may change. Because of this, when
* disabling is complete, we will go back to recheck to
* repeat all of the checks that we performed to get to
* this point. Disabling sets fs->fs_logbno to 0, so this
* will not put us into an infinite loop.
*/
mutex_exit(&ulp->ul_lock);
vfs_unlock(vfsp);
lf.lf_lock = LOCKFS_ULOCK;
lf.lf_flags = 0;
error = ufs_fiolfs(vp, &lf, 1);
if (error) {
flp->error = FIOLOG_ENOULOCK;
return (0);
}
error = lufs_disable(vp, flp);
if (error || (flp->error != FIOLOG_ENONE))
return (0);
goto recheck;
}
error = lufs_alloc(ufsvfsp, flp, cg_minlogsize, cr);
if (error)
goto errout;
/*
* Create all of the incore structs
*/
error = lufs_snarf(ufsvfsp, fs, 0);
if (error)
goto errout;
/*
* DON'T ``GOTO ERROUT'' PAST THIS POINT
*/
/*
* Pretend we were just mounted with logging enabled
* Get the ops vector
* If debug, record metadata locations with log subsystem
* Start the delete thread
* Start the reclaim thread, if necessary
*/
vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
TRANS_DOMATAMAP(ufsvfsp);
TRANS_MATA_MOUNT(ufsvfsp);
TRANS_MATA_SI(ufsvfsp, fs);
ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
if (fs->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
fs->fs_reclaim &= ~FS_RECLAIM;
fs->fs_reclaim |= FS_RECLAIMING;
ufs_thread_start(&ufsvfsp->vfs_reclaim,
ufs_thread_reclaim, vfsp);
} else
fs->fs_reclaim |= reclaim;
mutex_exit(&ulp->ul_lock);
vfs_unlock(vfsp);
/*
* Unlock the file system
*/
lf.lf_lock = LOCKFS_ULOCK;
lf.lf_flags = 0;
error = ufs_fiolfs(vp, &lf, 1);
if (error) {
flp->error = FIOLOG_ENOULOCK;
return (0);
}
/*
* There's nothing in the log yet (we've just allocated it)
* so directly write out the super block.
* Note, we have to force this sb out to disk
* (not just to the log) so that if we crash we know we are logging
*/
mutex_enter(&ufsvfsp->vfs_lock);
fs->fs_clean = FSLOG;
fs->fs_rolled = FS_NEED_ROLL; /* Mark the fs as unrolled */
UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
mutex_exit(&ufsvfsp->vfs_lock);
return (0);
errout:
/*
* Aquire the ufs_scan_lock before de-linking the mtm data
* structure so that we keep ufs_sync() and ufs_update() away
* when they execute the ufs_scan_inodes() run while we're in
* progress of enabling/disabling logging.
*/
mutex_enter(&ufs_scan_lock);
(void) lufs_unsnarf(ufsvfsp);
mutex_exit(&ufs_scan_lock);
(void) lufs_free(ufsvfsp);
unlockout:
mutex_exit(&ulp->ul_lock);
vfs_unlock(vfsp);
lf.lf_lock = LOCKFS_ULOCK;
lf.lf_flags = 0;
(void) ufs_fiolfs(vp, &lf, 1);
return (error);
}
void
lufs_read_strategy(ml_unit_t *ul, buf_t *bp)
{
mt_map_t *logmap = ul->un_logmap;
offset_t mof = ldbtob(bp->b_blkno);
off_t nb = bp->b_bcount;
mapentry_t *age;
char *va;
int (*saviodone)();
int entire_range;
/*
* get a linked list of overlapping deltas
* returns with &mtm->mtm_rwlock held
*/
entire_range = logmap_list_get(logmap, mof, nb, &age);
/*
* no overlapping deltas were found; read master
*/
if (age == NULL) {
rw_exit(&logmap->mtm_rwlock);
if (ul->un_flags & LDL_ERROR) {
bp->b_flags |= B_ERROR;
bp->b_error = EIO;
biodone(bp);
} else {
ul->un_ufsvfs->vfs_iotstamp = ddi_get_lbolt();
logstats.ls_lreads.value.ui64++;
(void) bdev_strategy(bp);
lwp_stat_update(LWP_STAT_INBLK, 1);
}
return;
}
va = bp_mapin_common(bp, VM_SLEEP);
/*
* if necessary, sync read the data from master
* errors are returned in bp
*/
if (!entire_range) {
saviodone = bp->b_iodone;
bp->b_iodone = trans_not_done;
logstats.ls_mreads.value.ui64++;
(void) bdev_strategy(bp);
lwp_stat_update(LWP_STAT_INBLK, 1);
if (trans_not_wait(bp))
ldl_seterror(ul, "Error reading master");
bp->b_iodone = saviodone;
}
/*
* sync read the data from the log
* errors are returned inline
*/
if (ldl_read(ul, va, mof, nb, age)) {
bp->b_flags |= B_ERROR;
bp->b_error = EIO;
}
/*
* unlist the deltas
*/
logmap_list_put(logmap, age);
/*
* all done
*/
if (ul->un_flags & LDL_ERROR) {
bp->b_flags |= B_ERROR;
bp->b_error = EIO;
}
biodone(bp);
}
void
lufs_write_strategy(ml_unit_t *ul, buf_t *bp)
{
offset_t mof = ldbtob(bp->b_blkno);
off_t nb = bp->b_bcount;
char *va;
mapentry_t *me;
ASSERT((nb & DEV_BMASK) == 0);
ul->un_logmap->mtm_ref = 1;
/*
* if there are deltas, move into log
*/
me = deltamap_remove(ul->un_deltamap, mof, nb);
if (me) {
va = bp_mapin_common(bp, VM_SLEEP);
ASSERT(((ul->un_debug & MT_WRITE_CHECK) == 0) ||
(ul->un_matamap == NULL)||
matamap_within(ul->un_matamap, mof, nb));
/*
* move to logmap
*/
if (ufs_crb_enable) {
logmap_add_buf(ul, va, mof, me,
bp->b_un.b_addr, nb);
} else {
logmap_add(ul, va, mof, me);
}
if (ul->un_flags & LDL_ERROR) {
bp->b_flags |= B_ERROR;
bp->b_error = EIO;
}
biodone(bp);
return;
}
if (ul->un_flags & LDL_ERROR) {
bp->b_flags |= B_ERROR;
bp->b_error = EIO;
biodone(bp);
return;
}
/*
* Check that we are not updating metadata, or if so then via B_PHYS.
*/
ASSERT((ul->un_matamap == NULL) ||
!(matamap_overlap(ul->un_matamap, mof, nb) &&
((bp->b_flags & B_PHYS) == 0)));
ul->un_ufsvfs->vfs_iotstamp = ddi_get_lbolt();
logstats.ls_lwrites.value.ui64++;
/* If snapshots are enabled, write through the snapshot driver */
if (ul->un_ufsvfs->vfs_snapshot)
fssnap_strategy(&ul->un_ufsvfs->vfs_snapshot, bp);
else
(void) bdev_strategy(bp);
lwp_stat_update(LWP_STAT_OUBLK, 1);
}
void
lufs_strategy(ml_unit_t *ul, buf_t *bp)
{
if (bp->b_flags & B_READ)
lufs_read_strategy(ul, bp);
else
lufs_write_strategy(ul, bp);
}
/* ARGSUSED */
static int
delta_stats_update(kstat_t *ksp, int rw)
{
if (rw == KSTAT_WRITE) {
delta_stats[DT_SB] = dkstats.ds_superblock_deltas.value.ui64;
delta_stats[DT_CG] = dkstats.ds_bitmap_deltas.value.ui64;
delta_stats[DT_SI] = dkstats.ds_suminfo_deltas.value.ui64;
delta_stats[DT_AB] = dkstats.ds_allocblk_deltas.value.ui64;
delta_stats[DT_ABZERO] = dkstats.ds_ab0_deltas.value.ui64;
delta_stats[DT_DIR] = dkstats.ds_dir_deltas.value.ui64;
delta_stats[DT_INODE] = dkstats.ds_inode_deltas.value.ui64;
delta_stats[DT_FBI] = dkstats.ds_fbiwrite_deltas.value.ui64;
delta_stats[DT_QR] = dkstats.ds_quota_deltas.value.ui64;
delta_stats[DT_SHAD] = dkstats.ds_shadow_deltas.value.ui64;
roll_stats[DT_SB] = dkstats.ds_superblock_rolled.value.ui64;
roll_stats[DT_CG] = dkstats.ds_bitmap_rolled.value.ui64;
roll_stats[DT_SI] = dkstats.ds_suminfo_rolled.value.ui64;
roll_stats[DT_AB] = dkstats.ds_allocblk_rolled.value.ui64;
roll_stats[DT_ABZERO] = dkstats.ds_ab0_rolled.value.ui64;
roll_stats[DT_DIR] = dkstats.ds_dir_rolled.value.ui64;
roll_stats[DT_INODE] = dkstats.ds_inode_rolled.value.ui64;
roll_stats[DT_FBI] = dkstats.ds_fbiwrite_rolled.value.ui64;
roll_stats[DT_QR] = dkstats.ds_quota_rolled.value.ui64;
roll_stats[DT_SHAD] = dkstats.ds_shadow_rolled.value.ui64;
} else {
dkstats.ds_superblock_deltas.value.ui64 = delta_stats[DT_SB];
dkstats.ds_bitmap_deltas.value.ui64 = delta_stats[DT_CG];
dkstats.ds_suminfo_deltas.value.ui64 = delta_stats[DT_SI];
dkstats.ds_allocblk_deltas.value.ui64 = delta_stats[DT_AB];
dkstats.ds_ab0_deltas.value.ui64 = delta_stats[DT_ABZERO];
dkstats.ds_dir_deltas.value.ui64 = delta_stats[DT_DIR];
dkstats.ds_inode_deltas.value.ui64 = delta_stats[DT_INODE];
dkstats.ds_fbiwrite_deltas.value.ui64 = delta_stats[DT_FBI];
dkstats.ds_quota_deltas.value.ui64 = delta_stats[DT_QR];
dkstats.ds_shadow_deltas.value.ui64 = delta_stats[DT_SHAD];
dkstats.ds_superblock_rolled.value.ui64 = roll_stats[DT_SB];
dkstats.ds_bitmap_rolled.value.ui64 = roll_stats[DT_CG];
dkstats.ds_suminfo_rolled.value.ui64 = roll_stats[DT_SI];
dkstats.ds_allocblk_rolled.value.ui64 = roll_stats[DT_AB];
dkstats.ds_ab0_rolled.value.ui64 = roll_stats[DT_ABZERO];
dkstats.ds_dir_rolled.value.ui64 = roll_stats[DT_DIR];
dkstats.ds_inode_rolled.value.ui64 = roll_stats[DT_INODE];
dkstats.ds_fbiwrite_rolled.value.ui64 = roll_stats[DT_FBI];
dkstats.ds_quota_rolled.value.ui64 = roll_stats[DT_QR];
dkstats.ds_shadow_rolled.value.ui64 = roll_stats[DT_SHAD];
}
return (0);
}
extern size_t ufs_crb_limit;
extern int ufs_max_crb_divisor;
void
lufs_init(void)
{
kstat_t *ksp;
/* Create kmem caches */
lufs_sv = kmem_cache_create("lufs_save", sizeof (lufs_save_t), 0,
NULL, NULL, NULL, NULL, NULL, 0);
lufs_bp = kmem_cache_create("lufs_bufs", sizeof (lufs_buf_t), 0,
NULL, NULL, NULL, NULL, NULL, 0);
mutex_init(&log_mutex, NULL, MUTEX_DEFAULT, NULL);
_init_top();
if (bio_lufs_strategy == NULL)
bio_lufs_strategy = (void (*) (void *, buf_t *)) lufs_strategy;
/*
* Initialise general logging and delta kstats
*/
ksp = kstat_create("ufs_log", 0, "logstats", "ufs", KSTAT_TYPE_NAMED,
sizeof (logstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
if (ksp) {
ksp->ks_data = (void *) &logstats;
kstat_install(ksp);
}
ksp = kstat_create("ufs_log", 0, "deltastats", "ufs", KSTAT_TYPE_NAMED,
sizeof (dkstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
if (ksp) {
ksp->ks_data = (void *) &dkstats;
ksp->ks_update = delta_stats_update;
kstat_install(ksp);
}
/* Initialize generation of logging ids */
lufs_genid_init();
/*
* Set up the maximum amount of kmem that the crbs (system wide)
* can use.
*/
ufs_crb_limit = kmem_maxavail() / ufs_max_crb_divisor;
}
|