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
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
|
/* $NetBSD: resize_ffs.c,v 1.58 2023/01/07 19:41:30 chs Exp $ */
/* From sources sent on February 17, 2003 */
/*-
* As its sole author, I explicitly place this code in the public
* domain. Anyone may use it for any purpose (though I would
* appreciate credit where it is due).
*
* der Mouse
*
* mouse@rodents.montreal.qc.ca
* 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B
*/
/*
* resize_ffs:
*
* Resize a file system. Is capable of both growing and shrinking.
*
* Usage: resize_ffs [-s newsize] [-y] file_system
*
* Example: resize_ffs -s 29574 /dev/rsd1e
*
* newsize is in DEV_BSIZE units (ie, disk sectors, usually 512 bytes
* each).
*
* Note: this currently requires gcc to build, since it is written
* depending on gcc-specific features, notably nested function
* definitions (which in at least a few cases depend on the lexical
* scoping gcc provides, so they can't be trivially moved outside).
*
* Many thanks go to John Kohl <jtk@NetBSD.org> for finding bugs: the
* one responsible for the "realloccgblk: can't find blk in cyl"
* problem and a more minor one which left fs_dsize wrong when
* shrinking. (These actually indicate bugs in fsck too - it should
* have caught and fixed them.)
*
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: resize_ffs.c,v 1.58 2023/01/07 19:41:30 chs Exp $");
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/dkio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/param.h> /* MAXFRAG */
#include <ufs/ffs/fs.h>
#include <ufs/ffs/ffs_extern.h>
#include <ufs/ufs/dir.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ufs/ufs_bswap.h> /* ufs_rw32 */
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <util.h>
#include "progress.h"
/* new size of file system, in sectors */
static int64_t newsize;
/* fd open onto disk device or file */
static int fd;
/* disk device or file path */
const char *special;
/* must we break up big I/O operations - see checksmallio() */
static int smallio;
/* size of a cg, in bytes, rounded up to a frag boundary */
static int cgblksz;
/* possible superblock localtions */
static int search[] = SBLOCKSEARCH;
/* location of the superblock */
static off_t where;
/* Superblocks. */
static struct fs *oldsb; /* before we started */
static struct fs *newsb; /* copy to work with */
/* Buffer to hold the above. Make sure it's aligned correctly. */
static char sbbuf[2 * SBLOCKSIZE]
__attribute__((__aligned__(__alignof__(struct fs))));
union dinode {
struct ufs1_dinode dp1;
struct ufs2_dinode dp2;
};
#define DIP(dp, field) \
((is_ufs2) ? \
(dp)->dp2.field : (dp)->dp1.field)
#define DIP_ASSIGN(dp, field, value) \
do { \
if (is_ufs2) \
(dp)->dp2.field = (value); \
else \
(dp)->dp1.field = (value); \
} while (0)
/* a cg's worth of brand new squeaky-clean inodes */
static struct ufs1_dinode *zinodes1;
static struct ufs2_dinode *zinodes2;
/* pointers to the in-core cgs, read off disk and possibly modified */
static struct cg **cgs;
/* pointer to csum array - the stuff pointed to on-disk by fs_csaddr */
static struct csum *csums;
/* per-cg flags, indexed by cg number */
static unsigned char *cgflags;
#define CGF_DIRTY 0x01 /* needs to be written to disk */
#define CGF_BLKMAPS 0x02 /* block bitmaps need rebuilding */
#define CGF_INOMAPS 0x04 /* inode bitmaps need rebuilding */
/* when shrinking, these two arrays record how we want blocks to move. */
/* if blkmove[i] is j, the frag that started out as frag #i should end */
/* up as frag #j. inomove[i]=j means, similarly, that the inode that */
/* started out as inode i should end up as inode j. */
static unsigned int *blkmove;
static unsigned int *inomove;
/* in-core copies of all inodes in the fs, indexed by inumber */
union dinode *inodes;
void *ibuf; /* ptr to fs block-sized buffer for reading/writing inodes */
/* byteswapped inodes */
union dinode *sinodes;
/* per-inode flags, indexed by inumber */
static unsigned char *iflags;
#define IF_DIRTY 0x01 /* needs to be written to disk */
#define IF_BDIRTY 0x02 /* like DIRTY, but is set on first inode in a
* block of inodes, and applies to the whole
* block. */
/* resize_ffs works directly on dinodes, adapt blksize() */
#define dblksize(fs, dip, lbn, filesize) \
(((lbn) >= UFS_NDADDR || (uint64_t)(filesize) >= ffs_lblktosize(fs, (lbn) + 1)) \
? (fs)->fs_bsize \
: (ffs_fragroundup(fs, ffs_blkoff(fs, (filesize)))))
/*
* Number of disk sectors per block/fragment
*/
#define NSPB(fs) (FFS_FSBTODB((fs),1) << (fs)->fs_fragshift)
#define NSPF(fs) (FFS_FSBTODB((fs),1))
/* global flags */
int is_ufs2 = 0;
int needswap = 0;
int verbose = 0;
int progress = 0;
static void usage(void) __dead;
/*
* See if we need to break up large I/O operations. This should never
* be needed, but under at least one <version,platform> combination,
* large enough disk transfers to the raw device hang. So if we're
* talking to a character special device, play it safe; in this case,
* readat() and writeat() break everything up into pieces no larger
* than 8K, doing multiple syscalls for larger operations.
*/
static void
checksmallio(void)
{
struct stat stb;
fstat(fd, &stb);
smallio = ((stb.st_mode & S_IFMT) == S_IFCHR);
}
static int
isplainfile(void)
{
struct stat stb;
fstat(fd, &stb);
return S_ISREG(stb.st_mode);
}
/*
* Read size bytes starting at blkno into buf. blkno is in DEV_BSIZE
* units, ie, after FFS_FSBTODB(); size is in bytes.
*/
static void
readat(off_t blkno, void *buf, int size)
{
/* Seek to the correct place. */
if (lseek(fd, blkno * DEV_BSIZE, L_SET) < 0)
err(EXIT_FAILURE, "lseek failed");
/* See if we have to break up the transfer... */
if (smallio) {
char *bp; /* pointer into buf */
int left; /* bytes left to go */
int n; /* number to do this time around */
int rv; /* syscall return value */
bp = buf;
left = size;
while (left > 0) {
n = (left > 8192) ? 8192 : left;
rv = read(fd, bp, n);
if (rv < 0)
err(EXIT_FAILURE, "read failed");
if (rv != n)
errx(EXIT_FAILURE,
"read: wanted %d, got %d", n, rv);
bp += n;
left -= n;
}
} else {
int rv;
rv = read(fd, buf, size);
if (rv < 0)
err(EXIT_FAILURE, "read failed");
if (rv != size)
errx(EXIT_FAILURE, "read: wanted %d, got %d",
size, rv);
}
}
/*
* Write size bytes from buf starting at blkno. blkno is in DEV_BSIZE
* units, ie, after FFS_FSBTODB(); size is in bytes.
*/
static void
writeat(off_t blkno, const void *buf, int size)
{
/* Seek to the correct place. */
if (lseek(fd, blkno * DEV_BSIZE, L_SET) < 0)
err(EXIT_FAILURE, "lseek failed");
/* See if we have to break up the transfer... */
if (smallio) {
const char *bp; /* pointer into buf */
int left; /* bytes left to go */
int n; /* number to do this time around */
int rv; /* syscall return value */
bp = buf;
left = size;
while (left > 0) {
n = (left > 8192) ? 8192 : left;
rv = write(fd, bp, n);
if (rv < 0)
err(EXIT_FAILURE, "write failed");
if (rv != n)
errx(EXIT_FAILURE,
"write: wanted %d, got %d", n, rv);
bp += n;
left -= n;
}
} else {
int rv;
rv = write(fd, buf, size);
if (rv < 0)
err(EXIT_FAILURE, "write failed");
if (rv != size)
errx(EXIT_FAILURE,
"write: wanted %d, got %d", size, rv);
}
}
/*
* Never-fail versions of malloc() and realloc(), and an allocation
* routine (which also never fails) for allocating memory that will
* never be freed until exit.
*/
/*
* Never-fail malloc.
*/
static void *
nfmalloc(size_t nb, const char *tag)
{
void *rv;
rv = malloc(nb);
if (rv)
return (rv);
err(EXIT_FAILURE, "Can't allocate %lu bytes for %s",
(unsigned long int) nb, tag);
}
/*
* Never-fail realloc.
*/
static void *
nfrealloc(void *blk, size_t nb, const char *tag)
{
void *rv;
rv = realloc(blk, nb);
if (rv)
return (rv);
err(EXIT_FAILURE, "Can't re-allocate %lu bytes for %s",
(unsigned long int) nb, tag);
}
/*
* Allocate memory that will never be freed or reallocated. Arguably
* this routine should handle small allocations by chopping up pages,
* but that's not worth the bother; it's not called more than a
* handful of times per run, and if the allocations are that small the
* waste in giving each one its own page is ignorable.
*/
static void *
alloconce(size_t nb, const char *tag)
{
void *rv;
rv = mmap(0, nb, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
if (rv != MAP_FAILED)
return (rv);
err(EXIT_FAILURE, "Can't map %lu bytes for %s",
(unsigned long int) nb, tag);
}
/*
* Load the cgs and csums off disk. Also allocates the space to load
* them into and initializes the per-cg flags.
*/
static void
loadcgs(void)
{
uint32_t cg;
char *cgp;
cgblksz = roundup(oldsb->fs_cgsize, oldsb->fs_fsize);
cgs = nfmalloc(oldsb->fs_ncg * sizeof(*cgs), "cg pointers");
cgp = alloconce(oldsb->fs_ncg * cgblksz, "cgs");
cgflags = nfmalloc(oldsb->fs_ncg, "cg flags");
csums = nfmalloc(oldsb->fs_cssize, "cg summary");
for (cg = 0; cg < oldsb->fs_ncg; cg++) {
cgs[cg] = (struct cg *) cgp;
readat(FFS_FSBTODB(oldsb, cgtod(oldsb, cg)), cgp, cgblksz);
if (needswap)
ffs_cg_swap(cgs[cg],cgs[cg],oldsb);
cgflags[cg] = 0;
cgp += cgblksz;
}
readat(FFS_FSBTODB(oldsb, oldsb->fs_csaddr), csums, oldsb->fs_cssize);
if (needswap)
ffs_csum_swap(csums,csums,oldsb->fs_cssize);
}
/*
* Set n bits, starting with bit #base, in the bitmap pointed to by
* bitvec (which is assumed to be large enough to include bits base
* through base+n-1).
*/
static void
set_bits(unsigned char *bitvec, unsigned int base, unsigned int n)
{
if (n < 1)
return; /* nothing to do */
if (base & 7) { /* partial byte at beginning */
if (n <= 8 - (base & 7)) { /* entirely within one byte */
bitvec[base >> 3] |= (~((~0U) << n)) << (base & 7);
return;
}
bitvec[base >> 3] |= (~0U) << (base & 7);
n -= 8 - (base & 7);
base = (base & ~7) + 8;
}
if (n >= 8) { /* do full bytes */
memset(bitvec + (base >> 3), 0xff, n >> 3);
base += n & ~7;
n &= 7;
}
if (n) { /* partial byte at end */
bitvec[base >> 3] |= ~((~0U) << n);
}
}
/*
* Clear n bits, starting with bit #base, in the bitmap pointed to by
* bitvec (which is assumed to be large enough to include bits base
* through base+n-1). Code parallels set_bits().
*/
static void
clr_bits(unsigned char *bitvec, int base, int n)
{
if (n < 1)
return;
if (base & 7) {
if (n <= 8 - (base & 7)) {
bitvec[base >> 3] &= ~((~((~0U) << n)) << (base & 7));
return;
}
bitvec[base >> 3] &= ~((~0U) << (base & 7));
n -= 8 - (base & 7);
base = (base & ~7) + 8;
}
if (n >= 8) {
memset(bitvec + (base >> 3), 0, n >> 3);
base += n & ~7;
n &= 7;
}
if (n) {
bitvec[base >> 3] &= (~0U) << n;
}
}
/*
* Test whether bit #bit is set in the bitmap pointed to by bitvec.
*/
static int
bit_is_set(unsigned char *bitvec, int bit)
{
return (bitvec[bit >> 3] & (1 << (bit & 7)));
}
/*
* Test whether bit #bit is clear in the bitmap pointed to by bitvec.
*/
static int
bit_is_clr(unsigned char *bitvec, int bit)
{
return (!bit_is_set(bitvec, bit));
}
/*
* Test whether a whole block of bits is set in a bitmap. This is
* designed for testing (aligned) disk blocks in a bit-per-frag
* bitmap; it has assumptions wired into it based on that, essentially
* that the entire block fits into a single byte. This returns true
* iff _all_ the bits are set; it is not just the complement of
* blk_is_clr on the same arguments (unless blkfrags==1).
*/
static int
blk_is_set(unsigned char *bitvec, int blkbase, int blkfrags)
{
unsigned int mask;
mask = (~((~0U) << blkfrags)) << (blkbase & 7);
return ((bitvec[blkbase >> 3] & mask) == mask);
}
/*
* Test whether a whole block of bits is clear in a bitmap. See
* blk_is_set (above) for assumptions. This returns true iff _all_
* the bits are clear; it is not just the complement of blk_is_set on
* the same arguments (unless blkfrags==1).
*/
static int
blk_is_clr(unsigned char *bitvec, int blkbase, int blkfrags)
{
unsigned int mask;
mask = (~((~0U) << blkfrags)) << (blkbase & 7);
return ((bitvec[blkbase >> 3] & mask) == 0);
}
/*
* Initialize a new cg. Called when growing. Assumes memory has been
* allocated but not otherwise set up. This code sets the fields of
* the cg, initializes the bitmaps (and cluster summaries, if
* applicable), updates both per-cylinder summary info and the global
* summary info in newsb; it also writes out new inodes for the cg.
*
* This code knows it can never be called for cg 0, which makes it a
* bit simpler than it would otherwise be.
*/
static void
initcg(uint32_t cgn)
{
struct cg *cg; /* The in-core cg, of course */
int64_t base; /* Disk address of cg base */
int64_t dlow; /* Size of pre-cg data area */
int64_t dhigh; /* Offset of post-inode data area, from base */
int64_t dmax; /* Offset of end of post-inode data area */
int i; /* Generic loop index */
int n; /* Generic count */
int start; /* start of cg maps */
cg = cgs[cgn];
/* Place the data areas */
base = cgbase(newsb, cgn);
dlow = cgsblock(newsb, cgn) - base;
dhigh = cgdmin(newsb, cgn) - base;
dmax = newsb->fs_size - base;
if (dmax > newsb->fs_fpg)
dmax = newsb->fs_fpg;
start = (unsigned char *)&cg->cg_space[0] - (unsigned char *) cg;
/*
* Clear out the cg - assumes all-0-bytes is the correct way
* to initialize fields we don't otherwise touch, which is
* perhaps not the right thing to do, but it's what fsck and
* mkfs do.
*/
memset(cg, 0, newsb->fs_cgsize);
if (newsb->fs_old_flags & FS_FLAGS_UPDATED)
cg->cg_time = newsb->fs_time;
cg->cg_magic = CG_MAGIC;
cg->cg_cgx = cgn;
cg->cg_niblk = newsb->fs_ipg;
cg->cg_ndblk = dmax;
if (is_ufs2) {
cg->cg_time = newsb->fs_time;
cg->cg_initediblk = newsb->fs_ipg < 2 * FFS_INOPB(newsb) ?
newsb->fs_ipg : 2 * FFS_INOPB(newsb);
cg->cg_iusedoff = start;
} else {
cg->cg_old_time = newsb->fs_time;
cg->cg_old_niblk = cg->cg_niblk;
cg->cg_niblk = 0;
cg->cg_initediblk = 0;
cg->cg_old_ncyl = newsb->fs_old_cpg;
/* Update the cg_old_ncyl value for the last cylinder. */
if (cgn == newsb->fs_ncg - 1) {
if ((newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0)
cg->cg_old_ncyl = newsb->fs_old_ncyl %
newsb->fs_old_cpg;
}
/* Set up the bitmap pointers. We have to be careful
* to lay out the cg _exactly_ the way mkfs and fsck
* do it, since fsck compares the _entire_ cg against
* a recomputed cg, and whines if there is any
* mismatch, including the bitmap offsets. */
/* XXX update this comment when fsck is fixed */
cg->cg_old_btotoff = start;
cg->cg_old_boff = cg->cg_old_btotoff
+ (newsb->fs_old_cpg * sizeof(int32_t));
cg->cg_iusedoff = cg->cg_old_boff +
(newsb->fs_old_cpg * newsb->fs_old_nrpos * sizeof(int16_t));
}
cg->cg_freeoff = cg->cg_iusedoff + howmany(newsb->fs_ipg, NBBY);
if (newsb->fs_contigsumsize > 0) {
cg->cg_nclusterblks = cg->cg_ndblk / newsb->fs_frag;
cg->cg_clustersumoff = cg->cg_freeoff +
howmany(newsb->fs_fpg, NBBY) - sizeof(int32_t);
cg->cg_clustersumoff =
roundup(cg->cg_clustersumoff, sizeof(int32_t));
cg->cg_clusteroff = cg->cg_clustersumoff +
((newsb->fs_contigsumsize + 1) * sizeof(int32_t));
cg->cg_nextfreeoff = cg->cg_clusteroff +
howmany(ffs_fragstoblks(newsb,newsb->fs_fpg), NBBY);
n = dlow / newsb->fs_frag;
if (n > 0) {
set_bits(cg_clustersfree(cg, 0), 0, n);
cg_clustersum(cg, 0)[(n > newsb->fs_contigsumsize) ?
newsb->fs_contigsumsize : n]++;
}
} else {
cg->cg_nextfreeoff = cg->cg_freeoff +
howmany(newsb->fs_fpg, NBBY);
}
/* Mark the data areas as free; everything else is marked busy by the
* memset() up at the top. */
set_bits(cg_blksfree(cg, 0), 0, dlow);
set_bits(cg_blksfree(cg, 0), dhigh, dmax - dhigh);
/* Initialize summary info */
cg->cg_cs.cs_ndir = 0;
cg->cg_cs.cs_nifree = newsb->fs_ipg;
cg->cg_cs.cs_nbfree = dlow / newsb->fs_frag;
cg->cg_cs.cs_nffree = 0;
/* This is the simplest way of doing this; we perhaps could
* compute the correct cg_blktot()[] and cg_blks()[] values
* other ways, but it would be complicated and hardly seems
* worth the effort. (The reason there isn't
* frag-at-beginning and frag-at-end code here, like the code
* below for the post-inode data area, is that the pre-sb data
* area always starts at 0, and thus is block-aligned, and
* always ends at the sb, which is block-aligned.) */
if ((newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
int64_t di;
for (di = 0; di < dlow; di += newsb->fs_frag) {
old_cg_blktot(cg, 0)[old_cbtocylno(newsb, di)]++;
old_cg_blks(newsb, cg,
old_cbtocylno(newsb, di),
0)[old_cbtorpos(newsb, di)]++;
}
}
/* Deal with a partial block at the beginning of the post-inode area.
* I'm not convinced this can happen - I think the inodes are always
* block-aligned and always an integral number of blocks - but it's
* cheap to do the right thing just in case. */
if (dhigh % newsb->fs_frag) {
n = newsb->fs_frag - (dhigh % newsb->fs_frag);
cg->cg_frsum[n]++;
cg->cg_cs.cs_nffree += n;
dhigh += n;
}
n = (dmax - dhigh) / newsb->fs_frag;
/* We have n full-size blocks in the post-inode data area. */
if (n > 0) {
cg->cg_cs.cs_nbfree += n;
if (newsb->fs_contigsumsize > 0) {
i = dhigh / newsb->fs_frag;
set_bits(cg_clustersfree(cg, 0), i, n);
cg_clustersum(cg, 0)[(n > newsb->fs_contigsumsize) ?
newsb->fs_contigsumsize : n]++;
}
for (i = n; i > 0; i--) {
if (is_ufs2 == 0) {
old_cg_blktot(cg, 0)[old_cbtocylno(newsb,
dhigh)]++;
old_cg_blks(newsb, cg,
old_cbtocylno(newsb, dhigh),
0)[old_cbtorpos(newsb,
dhigh)]++;
}
dhigh += newsb->fs_frag;
}
}
/* Deal with any leftover frag at the end of the cg. */
i = dmax - dhigh;
if (i) {
cg->cg_frsum[i]++;
cg->cg_cs.cs_nffree += i;
}
/* Update the csum info. */
csums[cgn] = cg->cg_cs;
newsb->fs_cstotal.cs_nffree += cg->cg_cs.cs_nffree;
newsb->fs_cstotal.cs_nbfree += cg->cg_cs.cs_nbfree;
newsb->fs_cstotal.cs_nifree += cg->cg_cs.cs_nifree;
if (is_ufs2) {
/* Write out the cleared inodes. */
writeat(FFS_FSBTODB(newsb, cgimin(newsb, cgn)), zinodes2,
cg->cg_initediblk * sizeof(*zinodes2));
} else {
/* Write out the cleared inodes. */
writeat(FFS_FSBTODB(newsb, cgimin(newsb, cgn)), zinodes1,
newsb->fs_ipg * sizeof(*zinodes1));
}
/* Dirty the cg. */
cgflags[cgn] |= CGF_DIRTY;
}
/*
* Find free space, at least nfrags consecutive frags of it. Pays no
* attention to block boundaries, but refuses to straddle cg
* boundaries, even if the disk blocks involved are in fact
* consecutive. Return value is the frag number of the first frag of
* the block, or -1 if no space was found. Uses newsb for sb values,
* and assumes the cgs[] structures correctly describe the area to be
* searched.
*
* XXX is there a bug lurking in the ignoring of block boundaries by
* the routine used by fragmove() in evict_data()? Can an end-of-file
* frag legally straddle a block boundary? If not, this should be
* cloned and fixed to stop at block boundaries for that use. The
* current one may still be needed for csum info motion, in case that
* takes up more than a whole block (is the csum info allowed to begin
* partway through a block and continue into the following block?).
*
* If we wrap off the end of the file system back to the beginning, we
* can end up searching the end of the file system twice. I ignore
* this inefficiency, since if that happens we're going to croak with
* a no-space error anyway, so it happens at most once.
*/
static int
find_freespace(unsigned int nfrags)
{
static int hand = 0; /* hand rotates through all frags in the fs */
int cgsize; /* size of the cg hand currently points into */
uint32_t cgn; /* number of cg hand currently points into */
int fwc; /* frag-within-cg number of frag hand points
* to */
unsigned int run; /* length of run of free frags seen so far */
int secondpass; /* have we wrapped from end of fs to
* beginning? */
unsigned char *bits; /* cg_blksfree()[] for cg hand points into */
cgn = dtog(newsb, hand);
fwc = dtogd(newsb, hand);
secondpass = (hand == 0);
run = 0;
bits = cg_blksfree(cgs[cgn], 0);
cgsize = cgs[cgn]->cg_ndblk;
while (1) {
if (bit_is_set(bits, fwc)) {
run++;
if (run >= nfrags)
return (hand + 1 - run);
} else {
run = 0;
}
hand++;
fwc++;
if (fwc >= cgsize) {
fwc = 0;
cgn++;
if (cgn >= newsb->fs_ncg) {
hand = 0;
if (secondpass)
return (-1);
secondpass = 1;
cgn = 0;
}
bits = cg_blksfree(cgs[cgn], 0);
cgsize = cgs[cgn]->cg_ndblk;
run = 0;
}
}
}
/*
* Find a free block of disk space. Finds an entire block of frags,
* all of which are free. Return value is the frag number of the
* first frag of the block, or -1 if no space was found. Uses newsb
* for sb values, and assumes the cgs[] structures correctly describe
* the area to be searched.
*
* See find_freespace(), above, for remarks about hand wrapping around.
*/
static int
find_freeblock(void)
{
static int hand = 0; /* hand rotates through all frags in fs */
uint32_t cgn; /* cg number of cg hand points into */
int fwc; /* frag-within-cg number of frag hand points
* to */
int cgsize; /* size of cg hand points into */
int secondpass; /* have we wrapped from end to beginning? */
unsigned char *bits; /* cg_blksfree()[] for cg hand points into */
cgn = dtog(newsb, hand);
fwc = dtogd(newsb, hand);
secondpass = (hand == 0);
bits = cg_blksfree(cgs[cgn], 0);
cgsize = ffs_blknum(newsb, cgs[cgn]->cg_ndblk);
while (1) {
if (blk_is_set(bits, fwc, newsb->fs_frag))
return (hand);
fwc += newsb->fs_frag;
hand += newsb->fs_frag;
if (fwc >= cgsize) {
fwc = 0;
cgn++;
if (cgn >= newsb->fs_ncg) {
hand = 0;
if (secondpass)
return (-1);
secondpass = 1;
cgn = 0;
}
bits = cg_blksfree(cgs[cgn], 0);
cgsize = ffs_blknum(newsb, cgs[cgn]->cg_ndblk);
}
}
}
/*
* Find a free inode, returning its inumber or -1 if none was found.
* Uses newsb for sb values, and assumes the cgs[] structures
* correctly describe the area to be searched.
*
* See find_freespace(), above, for remarks about hand wrapping around.
*/
static int
find_freeinode(void)
{
static int hand = 0; /* hand rotates through all inodes in fs */
uint32_t cgn; /* cg number of cg hand points into */
uint32_t iwc; /* inode-within-cg number of inode hand points
* to */
int secondpass; /* have we wrapped from end to beginning? */
unsigned char *bits; /* cg_inosused()[] for cg hand points into */
cgn = hand / newsb->fs_ipg;
iwc = hand % newsb->fs_ipg;
secondpass = (hand == 0);
bits = cg_inosused(cgs[cgn], 0);
while (1) {
if (bit_is_clr(bits, iwc))
return (hand);
hand++;
iwc++;
if (iwc >= newsb->fs_ipg) {
iwc = 0;
cgn++;
if (cgn >= newsb->fs_ncg) {
hand = 0;
if (secondpass)
return (-1);
secondpass = 1;
cgn = 0;
}
bits = cg_inosused(cgs[cgn], 0);
}
}
}
/*
* Mark a frag as free. Sets the frag's bit in the cg_blksfree bitmap
* for the appropriate cg, and marks the cg as dirty.
*/
static void
free_frag(int fno)
{
int cgn;
cgn = dtog(newsb, fno);
set_bits(cg_blksfree(cgs[cgn], 0), dtogd(newsb, fno), 1);
cgflags[cgn] |= CGF_DIRTY | CGF_BLKMAPS;
}
/*
* Allocate a frag. Clears the frag's bit in the cg_blksfree bitmap
* for the appropriate cg, and marks the cg as dirty.
*/
static void
alloc_frag(int fno)
{
int cgn;
cgn = dtog(newsb, fno);
clr_bits(cg_blksfree(cgs[cgn], 0), dtogd(newsb, fno), 1);
cgflags[cgn] |= CGF_DIRTY | CGF_BLKMAPS;
}
/*
* Fix up the csum array. If shrinking, this involves freeing zero or
* more frags; if growing, it involves allocating them, or if the
* frags being grown into aren't free, finding space elsewhere for the
* csum info. (If the number of occupied frags doesn't change,
* nothing happens here.)
*/
static void
csum_fixup(void)
{
int nold; /* # frags in old csum info */
int ntot; /* # frags in new csum info */
int nnew; /* ntot-nold */
int newloc; /* new location for csum info, if necessary */
int i; /* generic loop index */
int j; /* generic loop index */
int f; /* "from" frag number, if moving */
int t; /* "to" frag number, if moving */
int cgn; /* cg number, used when shrinking */
ntot = howmany(newsb->fs_cssize, newsb->fs_fsize);
nold = howmany(oldsb->fs_cssize, newsb->fs_fsize);
nnew = ntot - nold;
/* First, if there's no change in frag counts, it's easy. */
if (nnew == 0)
return;
/* Next, if we're shrinking, it's almost as easy. Just free up any
* frags in the old area we no longer need. */
if (nnew < 0) {
for ((i = newsb->fs_csaddr + ntot - 1), (j = nnew);
j < 0;
i--, j++) {
free_frag(i);
}
return;
}
/* We must be growing. Check to see that the new csum area fits
* within the file system. I think this can never happen, since for
* the csum area to grow, we must be adding at least one cg, so the
* old csum area can't be this close to the end of the new file system.
* But it's a cheap check. */
/* XXX what if csum info is at end of cg and grows into next cg, what
* if it spills over onto the next cg's backup superblock? Can this
* happen? */
if (newsb->fs_csaddr + ntot <= newsb->fs_size) {
/* Okay, it fits - now, see if the space we want is free. */
for ((i = newsb->fs_csaddr + nold), (j = nnew);
j > 0;
i++, j--) {
cgn = dtog(newsb, i);
if (bit_is_clr(cg_blksfree(cgs[cgn], 0),
dtogd(newsb, i)))
break;
}
if (j <= 0) {
/* Win win - all the frags we want are free. Allocate
* 'em and we're all done. */
for ((i = newsb->fs_csaddr + ntot - nnew),
(j = nnew); j > 0; i++, j--) {
alloc_frag(i);
}
return;
}
}
/* We have to move the csum info, sigh. Look for new space, free old
* space, and allocate new. Update fs_csaddr. We don't copy anything
* on disk at this point; the csum info will be written to the
* then-current fs_csaddr as part of the final flush. */
newloc = find_freespace(ntot);
if (newloc < 0)
errx(EXIT_FAILURE, "Sorry, no space available for new csums");
for (i = 0, f = newsb->fs_csaddr, t = newloc; i < ntot; i++, f++, t++) {
if (i < nold) {
free_frag(f);
}
alloc_frag(t);
}
newsb->fs_csaddr = newloc;
}
/*
* Recompute newsb->fs_dsize. Just scans all cgs, adding the number of
* data blocks in that cg to the total.
*/
static void
recompute_fs_dsize(void)
{
uint32_t i;
newsb->fs_dsize = 0;
for (i = 0; i < newsb->fs_ncg; i++) {
int64_t dlow; /* size of before-sb data area */
int64_t dhigh; /* offset of post-inode data area */
int64_t dmax; /* total size of cg */
int64_t base; /* base of cg, since cgsblock() etc add it in */
base = cgbase(newsb, i);
dlow = cgsblock(newsb, i) - base;
dhigh = cgdmin(newsb, i) - base;
dmax = newsb->fs_size - base;
if (dmax > newsb->fs_fpg)
dmax = newsb->fs_fpg;
newsb->fs_dsize += dlow + dmax - dhigh;
}
/* Space in cg 0 before cgsblock is boot area, not free space! */
newsb->fs_dsize -= cgsblock(newsb, 0) - cgbase(newsb, 0);
/* And of course the csum info takes up space. */
newsb->fs_dsize -= howmany(newsb->fs_cssize, newsb->fs_fsize);
}
/*
* Return the current time. We call this and assign, rather than
* calling time() directly, as insulation against OSes where fs_time
* is not a time_t.
*/
static time_t
timestamp(void)
{
time_t t;
time(&t);
return (t);
}
/*
* Calculate new filesystem geometry
* return 0 if geometry actually changed
*/
static int
makegeometry(int chatter)
{
/* Update the size. */
newsb->fs_size = FFS_DBTOFSB(newsb, newsize);
if (is_ufs2)
newsb->fs_ncg = howmany(newsb->fs_size, newsb->fs_fpg);
else {
/* Update fs_old_ncyl and fs_ncg. */
newsb->fs_old_ncyl = howmany(newsb->fs_size * NSPF(newsb),
newsb->fs_old_spc);
newsb->fs_ncg = howmany(newsb->fs_old_ncyl, newsb->fs_old_cpg);
}
/* Does the last cg end before the end of its inode area? There is no
* reason why this couldn't be handled, but it would complicate a lot
* of code (in all file system code - fsck, kernel, etc) because of the
* potential partial inode area, and the gain in space would be
* minimal, at most the pre-sb data area. */
if (cgdmin(newsb, newsb->fs_ncg - 1) > newsb->fs_size) {
newsb->fs_ncg--;
if (is_ufs2)
newsb->fs_size = newsb->fs_ncg * newsb->fs_fpg;
else {
newsb->fs_old_ncyl = newsb->fs_ncg * newsb->fs_old_cpg;
newsb->fs_size = (newsb->fs_old_ncyl *
newsb->fs_old_spc) / NSPF(newsb);
}
if (chatter || verbose) {
printf("Warning: last cylinder group is too small;\n");
printf(" dropping it. New size = %lu.\n",
(unsigned long int) FFS_FSBTODB(newsb, newsb->fs_size));
}
}
/* Did we actually not grow? (This can happen if newsize is less than
* a frag larger than the old size - unlikely, but no excuse to
* misbehave if it happens.) */
if (newsb->fs_size == oldsb->fs_size)
return 1;
return 0;
}
/*
* Grow the file system.
*/
static void
grow(void)
{
uint32_t i;
if (makegeometry(1)) {
printf("New fs size %"PRIu64" = old fs size %"PRIu64
", not growing.\n", newsb->fs_size, oldsb->fs_size);
return;
}
if (verbose) {
printf("Growing fs from %"PRIu64" blocks to %"PRIu64
" blocks.\n", oldsb->fs_size, newsb->fs_size);
}
/* Update the timestamp. */
newsb->fs_time = timestamp();
/* Allocate and clear the new-inode area, in case we add any cgs. */
if (is_ufs2) {
zinodes2 = alloconce(newsb->fs_ipg * sizeof(*zinodes2),
"zeroed inodes");
memset(zinodes2, 0, newsb->fs_ipg * sizeof(*zinodes2));
} else {
zinodes1 = alloconce(newsb->fs_ipg * sizeof(*zinodes1),
"zeroed inodes");
memset(zinodes1, 0, newsb->fs_ipg * sizeof(*zinodes1));
}
/* Check that the new last sector (frag, actually) is writable. Since
* it's at least one frag larger than it used to be, we know we aren't
* overwriting anything important by this. (The choice of sbbuf as
* what to write is irrelevant; it's just something handy that's known
* to be at least one frag in size.) */
writeat(FFS_FSBTODB(newsb,newsb->fs_size - 1), &sbbuf, newsb->fs_fsize);
/* Find out how big the csum area is, and realloc csums if bigger. */
newsb->fs_cssize = ffs_fragroundup(newsb,
newsb->fs_ncg * sizeof(struct csum));
if (newsb->fs_cssize > oldsb->fs_cssize)
csums = nfrealloc(csums, newsb->fs_cssize, "new cg summary");
/* If we're adding any cgs, realloc structures and set up the new
cgs. */
if (newsb->fs_ncg > oldsb->fs_ncg) {
char *cgp;
cgs = nfrealloc(cgs, newsb->fs_ncg * sizeof(*cgs),
"cg pointers");
cgflags = nfrealloc(cgflags, newsb->fs_ncg, "cg flags");
memset(cgflags + oldsb->fs_ncg, 0,
newsb->fs_ncg - oldsb->fs_ncg);
cgp = alloconce((newsb->fs_ncg - oldsb->fs_ncg) * cgblksz,
"cgs");
for (i = oldsb->fs_ncg; i < newsb->fs_ncg; i++) {
cgs[i] = (struct cg *) cgp;
progress_bar(special, "grow cg",
i - oldsb->fs_ncg, newsb->fs_ncg - oldsb->fs_ncg);
initcg(i);
cgp += cgblksz;
}
cgs[oldsb->fs_ncg - 1]->cg_old_ncyl = oldsb->fs_old_cpg;
cgflags[oldsb->fs_ncg - 1] |= CGF_DIRTY;
}
/* If the old fs ended partway through a cg, we have to update the old
* last cg (though possibly not to a full cg!). */
if (oldsb->fs_size % oldsb->fs_fpg) {
struct cg *cg;
int64_t newcgsize;
int64_t prevcgtop;
int64_t oldcgsize;
cg = cgs[oldsb->fs_ncg - 1];
cgflags[oldsb->fs_ncg - 1] |= CGF_DIRTY | CGF_BLKMAPS;
prevcgtop = oldsb->fs_fpg * (oldsb->fs_ncg - 1);
newcgsize = newsb->fs_size - prevcgtop;
if (newcgsize > newsb->fs_fpg)
newcgsize = newsb->fs_fpg;
oldcgsize = oldsb->fs_size % oldsb->fs_fpg;
set_bits(cg_blksfree(cg, 0), oldcgsize, newcgsize - oldcgsize);
cg->cg_old_ncyl = oldsb->fs_old_cpg;
cg->cg_ndblk = newcgsize;
}
/* Fix up the csum info, if necessary. */
csum_fixup();
/* Make fs_dsize match the new reality. */
recompute_fs_dsize();
progress_done();
}
/*
* Call (*fn)() for each inode, passing the inode and its inumber. The
* number of cylinder groups is passed in, so this can be used to map
* over either the old or the new file system's set of inodes.
*/
static void
map_inodes(void (*fn) (union dinode * di, unsigned int, void *arg),
int ncg, void *cbarg) {
int i;
int ni;
ni = oldsb->fs_ipg * ncg;
for (i = 0; i < ni; i++)
(*fn) (inodes + i, i, cbarg);
}
/* Values for the third argument to the map function for
* map_inode_data_blocks. MDB_DATA indicates the block is contains
* file data; MDB_INDIR_PRE and MDB_INDIR_POST indicate that it's an
* indirect block. The MDB_INDIR_PRE call is made before the indirect
* block pointers are followed and the pointed-to blocks scanned,
* MDB_INDIR_POST after.
*/
#define MDB_DATA 1
#define MDB_INDIR_PRE 2
#define MDB_INDIR_POST 3
typedef void (*mark_callback_t) (off_t blocknum, unsigned int nfrags,
unsigned int blksize, int opcode);
/* Helper function - handles a data block. Calls the callback
* function and returns number of bytes occupied in file (actually,
* rounded up to a frag boundary). The name is historical. */
static int
markblk(mark_callback_t fn, union dinode * di, off_t bn, off_t o)
{
int sz;
int nb;
off_t filesize;
filesize = DIP(di,di_size);
if (o >= filesize)
return (0);
sz = dblksize(newsb, di, ffs_lblkno(newsb, o), filesize);
nb = (sz > filesize - o) ? filesize - o : sz;
if (bn)
(*fn) (bn, ffs_numfrags(newsb, sz), nb, MDB_DATA);
return (sz);
}
/* Helper function - handles an indirect block. Makes the
* MDB_INDIR_PRE callback for the indirect block, loops over the
* pointers and recurses, and makes the MDB_INDIR_POST callback.
* Returns the number of bytes occupied in file, as does markblk().
* For the sake of update_for_data_move(), we read the indirect block
* _after_ making the _PRE callback. The name is historical. */
static off_t
markiblk(mark_callback_t fn, union dinode * di, off_t bn, off_t o, int lev)
{
int i;
unsigned k;
off_t j, tot;
static int32_t indirblk1[howmany(MAXBSIZE, sizeof(int32_t))];
static int32_t indirblk2[howmany(MAXBSIZE, sizeof(int32_t))];
static int32_t indirblk3[howmany(MAXBSIZE, sizeof(int32_t))];
static int32_t *indirblks[3] = {
&indirblk1[0], &indirblk2[0], &indirblk3[0]
};
if (lev < 0)
return (markblk(fn, di, bn, o));
if (bn == 0) {
for (j = newsb->fs_bsize;
lev >= 0;
j *= FFS_NINDIR(newsb), lev--);
return (j);
}
(*fn) (bn, newsb->fs_frag, newsb->fs_bsize, MDB_INDIR_PRE);
readat(FFS_FSBTODB(newsb, bn), indirblks[lev], newsb->fs_bsize);
if (needswap)
for (k = 0; k < howmany(MAXBSIZE, sizeof(int32_t)); k++)
indirblks[lev][k] = bswap32(indirblks[lev][k]);
tot = 0;
for (i = 0; i < FFS_NINDIR(newsb); i++) {
j = markiblk(fn, di, indirblks[lev][i], o, lev - 1);
if (j == 0)
break;
o += j;
tot += j;
}
(*fn) (bn, newsb->fs_frag, newsb->fs_bsize, MDB_INDIR_POST);
return (tot);
}
/*
* Call (*fn)() for each data block for an inode. This routine assumes
* the inode is known to be of a type that has data blocks (file,
* directory, or non-fast symlink). The called function is:
*
* (*fn)(unsigned int blkno, unsigned int nf, unsigned int nb, int op)
*
* where blkno is the frag number, nf is the number of frags starting
* at blkno (always <= fs_frag), nb is the number of bytes that belong
* to the file (usually nf*fs_frag, often less for the last block/frag
* of a file).
*/
static void
map_inode_data_blocks(union dinode * di, mark_callback_t fn)
{
off_t o; /* offset within inode */
off_t inc; /* increment for o */
int b; /* index within di_db[] and di_ib[] arrays */
/* Scan the direct blocks... */
o = 0;
for (b = 0; b < UFS_NDADDR; b++) {
inc = markblk(fn, di, DIP(di,di_db[b]), o);
if (inc == 0)
break;
o += inc;
}
/* ...and the indirect blocks. */
if (inc) {
for (b = 0; b < UFS_NIADDR; b++) {
inc = markiblk(fn, di, DIP(di,di_ib[b]), o, b);
if (inc == 0)
return;
o += inc;
}
}
}
static void
dblk_callback(union dinode * di, unsigned int inum, void *arg)
{
mark_callback_t fn;
off_t filesize;
filesize = DIP(di,di_size);
fn = (mark_callback_t) arg;
switch (DIP(di,di_mode) & IFMT) {
case IFLNK:
if (filesize <= newsb->fs_maxsymlinklen) {
break;
}
/* FALLTHROUGH */
case IFDIR:
case IFREG:
map_inode_data_blocks(di, fn);
break;
}
}
/*
* Make a callback call, a la map_inode_data_blocks, for all data
* blocks in the entire fs. This is used only once, in
* update_for_data_move, but it's out at top level because the complex
* downward-funarg nesting that would otherwise result seems to give
* gcc gastric distress.
*/
static void
map_data_blocks(mark_callback_t fn, int ncg)
{
map_inodes(&dblk_callback, ncg, (void *) fn);
}
/*
* Initialize the blkmove array.
*/
static void
blkmove_init(void)
{
int i;
blkmove = alloconce(oldsb->fs_size * sizeof(*blkmove), "blkmove");
for (i = 0; i < oldsb->fs_size; i++)
blkmove[i] = i;
}
/*
* Load the inodes off disk. Allocates the structures and initializes
* them - the inodes from disk, the flags to zero.
*/
static void
loadinodes(void)
{
int imax, ino, j;
uint32_t i;
struct ufs1_dinode *dp1 = NULL;
struct ufs2_dinode *dp2 = NULL;
/* read inodes one fs block at a time and copy them */
inodes = alloconce(oldsb->fs_ncg * oldsb->fs_ipg *
sizeof(union dinode), "inodes");
iflags = alloconce(oldsb->fs_ncg * oldsb->fs_ipg, "inode flags");
memset(iflags, 0, oldsb->fs_ncg * oldsb->fs_ipg);
ibuf = nfmalloc(oldsb->fs_bsize,"inode block buf");
if (is_ufs2)
dp2 = (struct ufs2_dinode *)ibuf;
else
dp1 = (struct ufs1_dinode *)ibuf;
for (ino = 0,imax = oldsb->fs_ipg * oldsb->fs_ncg; ino < imax; ) {
readat(FFS_FSBTODB(oldsb, ino_to_fsba(oldsb, ino)), ibuf,
oldsb->fs_bsize);
for (i = 0; i < oldsb->fs_inopb; i++) {
if (is_ufs2) {
if (needswap) {
ffs_dinode2_swap(&(dp2[i]), &(dp2[i]));
for (j = 0; j < UFS_NDADDR; j++)
dp2[i].di_db[j] =
bswap32(dp2[i].di_db[j]);
for (j = 0; j < UFS_NIADDR; j++)
dp2[i].di_ib[j] =
bswap32(dp2[i].di_ib[j]);
}
memcpy(&inodes[ino].dp2, &dp2[i],
sizeof(inodes[ino].dp2));
} else {
if (needswap) {
ffs_dinode1_swap(&(dp1[i]), &(dp1[i]));
for (j = 0; j < UFS_NDADDR; j++)
dp1[i].di_db[j] =
bswap32(dp1[i].di_db[j]);
for (j = 0; j < UFS_NIADDR; j++)
dp1[i].di_ib[j] =
bswap32(dp1[i].di_ib[j]);
}
memcpy(&inodes[ino].dp1, &dp1[i],
sizeof(inodes[ino].dp1));
}
if (++ino > imax)
errx(EXIT_FAILURE,
"Exceeded number of inodes");
}
}
}
/*
* Report a file-system-too-full problem.
*/
__dead static void
toofull(void)
{
errx(EXIT_FAILURE, "Sorry, would run out of data blocks");
}
/*
* Record a desire to move "n" frags from "from" to "to".
*/
static void
mark_move(unsigned int from, unsigned int to, unsigned int n)
{
for (; n > 0; n--)
blkmove[from++] = to++;
}
/* Helper function - evict n frags, starting with start (cg-relative).
* The free bitmap is scanned, unallocated frags are ignored, and
* each block of consecutive allocated frags is moved as a unit.
*/
static void
fragmove(struct cg * cg, int64_t base, unsigned int start, unsigned int n)
{
unsigned int i;
int run;
run = 0;
for (i = 0; i <= n; i++) {
if ((i < n) && bit_is_clr(cg_blksfree(cg, 0), start + i)) {
run++;
} else {
if (run > 0) {
int off;
off = find_freespace(run);
if (off < 0)
toofull();
mark_move(base + start + i - run, off, run);
set_bits(cg_blksfree(cg, 0), start + i - run,
run);
clr_bits(cg_blksfree(cgs[dtog(oldsb, off)], 0),
dtogd(oldsb, off), run);
}
run = 0;
}
}
}
/*
* Evict all data blocks from the given cg, starting at minfrag (based
* at the beginning of the cg), for length nfrag. The eviction is
* assumed to be entirely data-area; this should not be called with a
* range overlapping the metadata structures in the cg. It also
* assumes minfrag points into the given cg; it will misbehave if this
* is not true.
*
* See the comment header on find_freespace() for one possible bug
* lurking here.
*/
static void
evict_data(struct cg * cg, unsigned int minfrag, int nfrag)
{
int64_t base; /* base of cg (in frags from beginning of fs) */
base = cgbase(oldsb, cg->cg_cgx);
/* Does the boundary fall in the middle of a block? To avoid
* breaking between frags allocated as consecutive, we always
* evict the whole block in this case, though one could argue
* we should check to see if the frag before or after the
* break is unallocated. */
if (minfrag % oldsb->fs_frag) {
int n;
n = minfrag % oldsb->fs_frag;
minfrag -= n;
nfrag += n;
}
/* Do whole blocks. If a block is wholly free, skip it; if
* wholly allocated, move it in toto. If neither, call
* fragmove() to move the frags to new locations. */
while (nfrag >= oldsb->fs_frag) {
if (!blk_is_set(cg_blksfree(cg, 0), minfrag, oldsb->fs_frag)) {
if (blk_is_clr(cg_blksfree(cg, 0), minfrag,
oldsb->fs_frag)) {
int off;
off = find_freeblock();
if (off < 0)
toofull();
mark_move(base + minfrag, off, oldsb->fs_frag);
set_bits(cg_blksfree(cg, 0), minfrag,
oldsb->fs_frag);
clr_bits(cg_blksfree(cgs[dtog(oldsb, off)], 0),
dtogd(oldsb, off), oldsb->fs_frag);
} else {
fragmove(cg, base, minfrag, oldsb->fs_frag);
}
}
minfrag += oldsb->fs_frag;
nfrag -= oldsb->fs_frag;
}
/* Clean up any sub-block amount left over. */
if (nfrag) {
fragmove(cg, base, minfrag, nfrag);
}
}
/*
* Move all data blocks according to blkmove. We have to be careful,
* because we may be updating indirect blocks that will themselves be
* getting moved, or inode int32_t arrays that point to indirect
* blocks that will be moved. We call this before
* update_for_data_move, and update_for_data_move does inodes first,
* then indirect blocks in preorder, so as to make sure that the
* file system is self-consistent at all points, for better crash
* tolerance. (We can get away with this only because all the writes
* done by perform_data_move() are writing into space that's not used
* by the old file system.) If we crash, some things may point to the
* old data and some to the new, but both copies are the same. The
* only wrong things should be csum info and free bitmaps, which fsck
* is entirely capable of cleaning up.
*
* Since blkmove_init() initializes all blocks to move to their current
* locations, we can have two blocks marked as wanting to move to the
* same location, but only two and only when one of them is the one
* that was already there. So if blkmove[i]==i, we ignore that entry
* entirely - for unallocated blocks, we don't want it (and may be
* putting something else there), and for allocated blocks, we don't
* want to copy it anywhere.
*/
static void
perform_data_move(void)
{
int i;
int run;
int maxrun;
char buf[65536];
maxrun = sizeof(buf) / newsb->fs_fsize;
run = 0;
for (i = 0; i < oldsb->fs_size; i++) {
if ((blkmove[i] == (unsigned)i /*XXX cast*/) ||
(run >= maxrun) ||
((run > 0) &&
(blkmove[i] != blkmove[i - 1] + 1))) {
if (run > 0) {
readat(FFS_FSBTODB(oldsb, i - run), &buf[0],
run << oldsb->fs_fshift);
writeat(FFS_FSBTODB(oldsb, blkmove[i - run]),
&buf[0], run << oldsb->fs_fshift);
}
run = 0;
}
if (blkmove[i] != (unsigned)i /*XXX cast*/)
run++;
}
if (run > 0) {
readat(FFS_FSBTODB(oldsb, i - run), &buf[0],
run << oldsb->fs_fshift);
writeat(FFS_FSBTODB(oldsb, blkmove[i - run]), &buf[0],
run << oldsb->fs_fshift);
}
}
/*
* This modifies an array of int32_t, according to blkmove. This is
* used to update inode block arrays and indirect blocks to point to
* the new locations of data blocks.
*
* Return value is the number of int32_ts that needed updating; in
* particular, the return value is zero iff nothing was modified.
*/
static int
movemap_blocks(int32_t * vec, int n)
{
int rv;
rv = 0;
for (; n > 0; n--, vec++) {
if (blkmove[*vec] != (unsigned)*vec /*XXX cast*/) {
*vec = blkmove[*vec];
rv++;
}
}
return (rv);
}
static void
moveblocks_callback(union dinode * di, unsigned int inum, void *arg)
{
int32_t *dblkptr, *iblkptr;
switch (DIP(di,di_mode) & IFMT) {
case IFLNK:
if ((off_t)DIP(di,di_size) <= oldsb->fs_maxsymlinklen) {
break;
}
/* FALLTHROUGH */
case IFDIR:
case IFREG:
if (is_ufs2) {
/* XXX these are not int32_t and this is WRONG! */
dblkptr = (void *) &(di->dp2.di_db[0]);
iblkptr = (void *) &(di->dp2.di_ib[0]);
} else {
dblkptr = &(di->dp1.di_db[0]);
iblkptr = &(di->dp1.di_ib[0]);
}
/*
* Don't || these two calls; we need their
* side-effects.
*/
if (movemap_blocks(dblkptr, UFS_NDADDR)) {
iflags[inum] |= IF_DIRTY;
}
if (movemap_blocks(iblkptr, UFS_NIADDR)) {
iflags[inum] |= IF_DIRTY;
}
break;
}
}
static void
moveindir_callback(off_t off, unsigned int nfrag, unsigned int nbytes,
int kind)
{
unsigned int i;
if (kind == MDB_INDIR_PRE) {
int32_t blk[howmany(MAXBSIZE, sizeof(int32_t))];
readat(FFS_FSBTODB(oldsb, off), &blk[0], oldsb->fs_bsize);
if (needswap)
for (i = 0; i < howmany(MAXBSIZE, sizeof(int32_t)); i++)
blk[i] = bswap32(blk[i]);
if (movemap_blocks(&blk[0], FFS_NINDIR(oldsb))) {
if (needswap)
for (i = 0; i < howmany(MAXBSIZE,
sizeof(int32_t)); i++)
blk[i] = bswap32(blk[i]);
writeat(FFS_FSBTODB(oldsb, off), &blk[0], oldsb->fs_bsize);
}
}
}
/*
* Update all inode data arrays and indirect blocks to point to the new
* locations of data blocks. See the comment header on
* perform_data_move for some ordering considerations.
*/
static void
update_for_data_move(void)
{
map_inodes(&moveblocks_callback, oldsb->fs_ncg, NULL);
map_data_blocks(&moveindir_callback, oldsb->fs_ncg);
}
/*
* Initialize the inomove array.
*/
static void
inomove_init(void)
{
int i;
inomove = alloconce(oldsb->fs_ipg * oldsb->fs_ncg * sizeof(*inomove),
"inomove");
for (i = (oldsb->fs_ipg * oldsb->fs_ncg) - 1; i >= 0; i--)
inomove[i] = i;
}
/*
* Flush all dirtied inodes to disk. Scans the inode flags array; for
* each dirty inode, it sets the BDIRTY bit on the first inode in the
* block containing the dirty inode. Then it scans by blocks, and for
* each marked block, writes it.
*/
static void
flush_inodes(void)
{
int i, j, k, ni, m;
struct ufs1_dinode *dp1 = NULL;
struct ufs2_dinode *dp2 = NULL;
ni = newsb->fs_ipg * newsb->fs_ncg;
m = FFS_INOPB(newsb) - 1;
for (i = 0; i < ni; i++) {
if (iflags[i] & IF_DIRTY) {
iflags[i & ~m] |= IF_BDIRTY;
}
}
m++;
if (is_ufs2)
dp2 = (struct ufs2_dinode *)ibuf;
else
dp1 = (struct ufs1_dinode *)ibuf;
for (i = 0; i < ni; i += m) {
if ((iflags[i] & IF_BDIRTY) == 0)
continue;
if (is_ufs2)
for (j = 0; j < m; j++) {
dp2[j] = inodes[i + j].dp2;
if (needswap) {
for (k = 0; k < UFS_NDADDR; k++)
dp2[j].di_db[k] =
bswap32(dp2[j].di_db[k]);
for (k = 0; k < UFS_NIADDR; k++)
dp2[j].di_ib[k] =
bswap32(dp2[j].di_ib[k]);
ffs_dinode2_swap(&dp2[j],
&dp2[j]);
}
}
else
for (j = 0; j < m; j++) {
dp1[j] = inodes[i + j].dp1;
if (needswap) {
for (k = 0; k < UFS_NDADDR; k++)
dp1[j].di_db[k]=
bswap32(dp1[j].di_db[k]);
for (k = 0; k < UFS_NIADDR; k++)
dp1[j].di_ib[k]=
bswap32(dp1[j].di_ib[k]);
ffs_dinode1_swap(&dp1[j],
&dp1[j]);
}
}
writeat(FFS_FSBTODB(newsb, ino_to_fsba(newsb, i)),
ibuf, newsb->fs_bsize);
}
}
/*
* Evict all inodes from the specified cg. shrink() already checked
* that there were enough free inodes, so the no-free-inodes check is
* a can't-happen. If it does trip, the file system should be in good
* enough shape for fsck to fix; see the comment on perform_data_move
* for the considerations in question.
*/
static void
evict_inodes(struct cg * cg)
{
int inum;
int fi;
uint32_t i;
inum = newsb->fs_ipg * cg->cg_cgx;
for (i = 0; i < newsb->fs_ipg; i++, inum++) {
if (DIP(inodes + inum,di_mode) != 0) {
fi = find_freeinode();
if (fi < 0)
errx(EXIT_FAILURE, "Sorry, inodes evaporated - "
"file system probably needs fsck");
inomove[inum] = fi;
clr_bits(cg_inosused(cg, 0), i, 1);
set_bits(cg_inosused(cgs[ino_to_cg(newsb, fi)], 0),
fi % newsb->fs_ipg, 1);
}
}
}
/*
* Move inodes from old locations to new. Does not actually write
* anything to disk; just copies in-core and sets dirty bits.
*
* We have to be careful here for reasons similar to those mentioned in
* the comment header on perform_data_move, above: for the sake of
* crash tolerance, we want to make sure everything is present at both
* old and new locations before we update pointers. So we call this
* first, then flush_inodes() to get them out on disk, then update
* directories to match.
*/
static void
perform_inode_move(void)
{
unsigned int i;
unsigned int ni;
ni = oldsb->fs_ipg * oldsb->fs_ncg;
for (i = 0; i < ni; i++) {
if (inomove[i] != i) {
inodes[inomove[i]] = inodes[i];
iflags[inomove[i]] = iflags[i] | IF_DIRTY;
}
}
}
/*
* Update the directory contained in the nb bytes at buf, to point to
* inodes' new locations.
*/
static int
update_dirents(char *buf, int nb)
{
int rv;
#define d ((struct direct *)buf)
#define s32(x) (needswap?bswap32((x)):(x))
#define s16(x) (needswap?bswap16((x)):(x))
rv = 0;
while (nb > 0) {
if (inomove[s32(d->d_ino)] != s32(d->d_ino)) {
rv++;
d->d_ino = s32(inomove[s32(d->d_ino)]);
}
nb -= s16(d->d_reclen);
buf += s16(d->d_reclen);
}
return (rv);
#undef d
#undef s32
#undef s16
}
/*
* Callback function for map_inode_data_blocks, for updating a
* directory to point to new inode locations.
*/
static void
update_dir_data(off_t bn, unsigned int size, unsigned int nb, int kind)
{
if (kind == MDB_DATA) {
union {
struct direct d;
char ch[MAXBSIZE];
} buf;
readat(FFS_FSBTODB(oldsb, bn), &buf, size << oldsb->fs_fshift);
if (update_dirents((char *) &buf, nb)) {
writeat(FFS_FSBTODB(oldsb, bn), &buf,
size << oldsb->fs_fshift);
}
}
}
static void
dirmove_callback(union dinode * di, unsigned int inum, void *arg)
{
switch (DIP(di,di_mode) & IFMT) {
case IFDIR:
map_inode_data_blocks(di, &update_dir_data);
break;
}
}
/*
* Update directory entries to point to new inode locations.
*/
static void
update_for_inode_move(void)
{
map_inodes(&dirmove_callback, newsb->fs_ncg, NULL);
}
/*
* Shrink the file system.
*/
static void
shrink(void)
{
uint32_t i;
if (makegeometry(1)) {
printf("New fs size %"PRIu64" = old fs size %"PRIu64
", not shrinking.\n", newsb->fs_size, oldsb->fs_size);
return;
}
/* Let's make sure we're not being shrunk into oblivion. */
if (newsb->fs_ncg < 1)
errx(EXIT_FAILURE, "Size too small - file system would "
"have no cylinders");
if (verbose) {
printf("Shrinking fs from %"PRIu64" blocks to %"PRIu64
" blocks.\n", oldsb->fs_size, newsb->fs_size);
}
/* Load the inodes off disk - we'll need 'em. */
loadinodes();
/* Update the timestamp. */
newsb->fs_time = timestamp();
/* Initialize for block motion. */
blkmove_init();
/* Update csum size, then fix up for the new size */
newsb->fs_cssize = ffs_fragroundup(newsb,
newsb->fs_ncg * sizeof(struct csum));
csum_fixup();
/* Evict data from any cgs being wholly eliminated */
for (i = newsb->fs_ncg; i < oldsb->fs_ncg; i++) {
int64_t base;
int64_t dlow;
int64_t dhigh;
int64_t dmax;
base = cgbase(oldsb, i);
dlow = cgsblock(oldsb, i) - base;
dhigh = cgdmin(oldsb, i) - base;
dmax = oldsb->fs_size - base;
if (dmax > cgs[i]->cg_ndblk)
dmax = cgs[i]->cg_ndblk;
evict_data(cgs[i], 0, dlow);
evict_data(cgs[i], dhigh, dmax - dhigh);
newsb->fs_cstotal.cs_ndir -= cgs[i]->cg_cs.cs_ndir;
newsb->fs_cstotal.cs_nifree -= cgs[i]->cg_cs.cs_nifree;
newsb->fs_cstotal.cs_nffree -= cgs[i]->cg_cs.cs_nffree;
newsb->fs_cstotal.cs_nbfree -= cgs[i]->cg_cs.cs_nbfree;
}
/* Update the new last cg. */
cgs[newsb->fs_ncg - 1]->cg_ndblk = newsb->fs_size -
((newsb->fs_ncg - 1) * newsb->fs_fpg);
/* Is the new last cg partial? If so, evict any data from the part
* being shrunken away. */
if (newsb->fs_size % newsb->fs_fpg) {
struct cg *cg;
int oldcgsize;
int newcgsize;
cg = cgs[newsb->fs_ncg - 1];
newcgsize = newsb->fs_size % newsb->fs_fpg;
oldcgsize = oldsb->fs_size - ((newsb->fs_ncg - 1) &
oldsb->fs_fpg);
if (oldcgsize > oldsb->fs_fpg)
oldcgsize = oldsb->fs_fpg;
evict_data(cg, newcgsize, oldcgsize - newcgsize);
clr_bits(cg_blksfree(cg, 0), newcgsize, oldcgsize - newcgsize);
}
/* Find out whether we would run out of inodes. (Note we
* haven't actually done anything to the file system yet; all
* those evict_data calls just update blkmove.) */
{
int slop;
slop = 0;
for (i = 0; i < newsb->fs_ncg; i++)
slop += cgs[i]->cg_cs.cs_nifree;
for (; i < oldsb->fs_ncg; i++)
slop -= oldsb->fs_ipg - cgs[i]->cg_cs.cs_nifree;
if (slop < 0)
errx(EXIT_FAILURE, "Sorry, would run out of inodes");
}
/* Copy data, then update pointers to data. See the comment
* header on perform_data_move for ordering considerations. */
perform_data_move();
update_for_data_move();
/* Now do inodes. Initialize, evict, move, update - see the
* comment header on perform_inode_move. */
inomove_init();
for (i = newsb->fs_ncg; i < oldsb->fs_ncg; i++)
evict_inodes(cgs[i]);
perform_inode_move();
flush_inodes();
update_for_inode_move();
/* Recompute all the bitmaps; most of them probably need it anyway,
* the rest are just paranoia and not wanting to have to bother
* keeping track of exactly which ones require it. */
for (i = 0; i < newsb->fs_ncg; i++)
cgflags[i] |= CGF_DIRTY | CGF_BLKMAPS | CGF_INOMAPS;
/* Update the cg_old_ncyl value for the last cylinder. */
if ((newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0)
cgs[newsb->fs_ncg - 1]->cg_old_ncyl =
newsb->fs_old_ncyl % newsb->fs_old_cpg;
/* Make fs_dsize match the new reality. */
recompute_fs_dsize();
}
/*
* Recompute the block totals, block cluster summaries, and rotational
* position summaries, for a given cg (specified by number), based on
* its free-frag bitmap (cg_blksfree()[]).
*/
static void
rescan_blkmaps(int cgn)
{
struct cg *cg;
uint32_t f;
int b;
int blkfree;
int blkrun;
int fragrun;
int fwb;
cg = cgs[cgn];
/* Subtract off the current totals from the sb's summary info */
newsb->fs_cstotal.cs_nffree -= cg->cg_cs.cs_nffree;
newsb->fs_cstotal.cs_nbfree -= cg->cg_cs.cs_nbfree;
/* Clear counters and bitmaps. */
cg->cg_cs.cs_nffree = 0;
cg->cg_cs.cs_nbfree = 0;
memset(&cg->cg_frsum[0], 0, MAXFRAG * sizeof(cg->cg_frsum[0]));
memset(&old_cg_blktot(cg, 0)[0], 0,
newsb->fs_old_cpg * sizeof(old_cg_blktot(cg, 0)[0]));
memset(&old_cg_blks(newsb, cg, 0, 0)[0], 0,
newsb->fs_old_cpg * newsb->fs_old_nrpos *
sizeof(old_cg_blks(newsb, cg, 0, 0)[0]));
if (newsb->fs_contigsumsize > 0) {
cg->cg_nclusterblks = cg->cg_ndblk / newsb->fs_frag;
memset(&cg_clustersum(cg, 0)[1], 0,
newsb->fs_contigsumsize *
sizeof(cg_clustersum(cg, 0)[1]));
if (is_ufs2)
memset(&cg_clustersfree(cg, 0)[0], 0,
howmany(newsb->fs_fpg / NSPB(newsb), NBBY));
else
memset(&cg_clustersfree(cg, 0)[0], 0,
howmany((newsb->fs_old_cpg * newsb->fs_old_spc) /
NSPB(newsb), NBBY));
}
/* Scan the free-frag bitmap. Runs of free frags are kept
* track of with fragrun, and recorded into cg_frsum[] and
* cg_cs.cs_nffree; on each block boundary, entire free blocks
* are recorded as well. */
blkfree = 1;
blkrun = 0;
fragrun = 0;
f = 0;
b = 0;
fwb = 0;
while (f < cg->cg_ndblk) {
if (bit_is_set(cg_blksfree(cg, 0), f)) {
fragrun++;
} else {
blkfree = 0;
if (fragrun > 0) {
cg->cg_frsum[fragrun]++;
cg->cg_cs.cs_nffree += fragrun;
}
fragrun = 0;
}
f++;
fwb++;
if (fwb >= newsb->fs_frag) {
if (blkfree) {
cg->cg_cs.cs_nbfree++;
if (newsb->fs_contigsumsize > 0)
set_bits(cg_clustersfree(cg, 0), b, 1);
if (is_ufs2 == 0) {
old_cg_blktot(cg, 0)[
old_cbtocylno(newsb,
f - newsb->fs_frag)]++;
old_cg_blks(newsb, cg,
old_cbtocylno(newsb,
f - newsb->fs_frag),
0)[old_cbtorpos(newsb,
f - newsb->fs_frag)]++;
}
blkrun++;
} else {
if (fragrun > 0) {
cg->cg_frsum[fragrun]++;
cg->cg_cs.cs_nffree += fragrun;
}
if (newsb->fs_contigsumsize > 0) {
if (blkrun > 0) {
cg_clustersum(cg, 0)[(blkrun
> newsb->fs_contigsumsize)
? newsb->fs_contigsumsize
: blkrun]++;
}
}
blkrun = 0;
}
fwb = 0;
b++;
blkfree = 1;
fragrun = 0;
}
}
if (fragrun > 0) {
cg->cg_frsum[fragrun]++;
cg->cg_cs.cs_nffree += fragrun;
}
if ((blkrun > 0) && (newsb->fs_contigsumsize > 0)) {
cg_clustersum(cg, 0)[(blkrun > newsb->fs_contigsumsize) ?
newsb->fs_contigsumsize : blkrun]++;
}
/*
* Put the updated summary info back into csums, and add it
* back into the sb's summary info. Then mark the cg dirty.
*/
csums[cgn] = cg->cg_cs;
newsb->fs_cstotal.cs_nffree += cg->cg_cs.cs_nffree;
newsb->fs_cstotal.cs_nbfree += cg->cg_cs.cs_nbfree;
cgflags[cgn] |= CGF_DIRTY;
}
/*
* Recompute the cg_inosused()[] bitmap, and the cs_nifree and cs_ndir
* values, for a cg, based on the in-core inodes for that cg.
*/
static void
rescan_inomaps(int cgn)
{
struct cg *cg;
int inum;
uint32_t iwc;
cg = cgs[cgn];
newsb->fs_cstotal.cs_ndir -= cg->cg_cs.cs_ndir;
newsb->fs_cstotal.cs_nifree -= cg->cg_cs.cs_nifree;
cg->cg_cs.cs_ndir = 0;
cg->cg_cs.cs_nifree = 0;
memset(&cg_inosused(cg, 0)[0], 0, howmany(newsb->fs_ipg, NBBY));
inum = cgn * newsb->fs_ipg;
if (cgn == 0) {
set_bits(cg_inosused(cg, 0), 0, 2);
iwc = 2;
inum += 2;
} else {
iwc = 0;
}
for (; iwc < newsb->fs_ipg; iwc++, inum++) {
switch (DIP(inodes + inum, di_mode) & IFMT) {
case 0:
cg->cg_cs.cs_nifree++;
break;
case IFDIR:
cg->cg_cs.cs_ndir++;
/* FALLTHROUGH */
default:
set_bits(cg_inosused(cg, 0), iwc, 1);
break;
}
}
csums[cgn] = cg->cg_cs;
newsb->fs_cstotal.cs_ndir += cg->cg_cs.cs_ndir;
newsb->fs_cstotal.cs_nifree += cg->cg_cs.cs_nifree;
cgflags[cgn] |= CGF_DIRTY;
}
/*
* Flush cgs to disk, recomputing anything they're marked as needing.
*/
static void
flush_cgs(void)
{
uint32_t i;
for (i = 0; i < newsb->fs_ncg; i++) {
progress_bar(special, "flush cg",
i, newsb->fs_ncg - 1);
if (cgflags[i] & CGF_BLKMAPS) {
rescan_blkmaps(i);
}
if (cgflags[i] & CGF_INOMAPS) {
rescan_inomaps(i);
}
if (cgflags[i] & CGF_DIRTY) {
cgs[i]->cg_rotor = 0;
cgs[i]->cg_frotor = 0;
cgs[i]->cg_irotor = 0;
if (needswap)
ffs_cg_swap(cgs[i],cgs[i],newsb);
writeat(FFS_FSBTODB(newsb, cgtod(newsb, i)), cgs[i],
cgblksz);
}
}
if (needswap)
ffs_csum_swap(csums,csums,newsb->fs_cssize);
writeat(FFS_FSBTODB(newsb, newsb->fs_csaddr), csums, newsb->fs_cssize);
progress_done();
}
/*
* Write the superblock, both to the main superblock and to each cg's
* alternative superblock.
*/
static void
write_sbs(void)
{
uint32_t i;
if (newsb->fs_magic == FS_UFS1_MAGIC &&
(newsb->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
newsb->fs_old_time = newsb->fs_time;
newsb->fs_old_size = newsb->fs_size;
/* we don't update fs_csaddr */
newsb->fs_old_dsize = newsb->fs_dsize;
newsb->fs_old_cstotal.cs_ndir = newsb->fs_cstotal.cs_ndir;
newsb->fs_old_cstotal.cs_nbfree = newsb->fs_cstotal.cs_nbfree;
newsb->fs_old_cstotal.cs_nifree = newsb->fs_cstotal.cs_nifree;
newsb->fs_old_cstotal.cs_nffree = newsb->fs_cstotal.cs_nffree;
/* fill fs_old_postbl_start with 256 bytes of 0xff? */
}
/* copy newsb back to oldsb, so we can use it for offsets if
newsb has been swapped for writing to disk */
memcpy(oldsb, newsb, SBLOCKSIZE);
if (needswap)
ffs_sb_swap(newsb,newsb);
writeat(where / DEV_BSIZE, newsb, SBLOCKSIZE);
for (i = 0; i < oldsb->fs_ncg; i++) {
progress_bar(special, "write sb",
i, oldsb->fs_ncg - 1);
writeat(FFS_FSBTODB(oldsb, cgsblock(oldsb, i)), newsb, SBLOCKSIZE);
}
progress_done();
}
/*
* Check to see whether new size changes the filesystem
* return exit code
*/
static int
checkonly(void)
{
if (makegeometry(0)) {
if (verbose) {
printf("Wouldn't change: already %" PRId64
" blocks\n", (int64_t)oldsb->fs_size);
}
return 1;
}
if (verbose) {
printf("Would change: newsize: %" PRId64 " oldsize: %"
PRId64 " fsdb: %" PRId64 "\n", FFS_DBTOFSB(oldsb, newsize),
(int64_t)oldsb->fs_size,
(int64_t)oldsb->fs_fsbtodb);
}
return 0;
}
static off_t
get_dev_size(const char *dev_name)
{
struct dkwedge_info dkw;
struct partition *pp;
struct disklabel lp;
struct stat st;
size_t ptn;
/* Get info about partition/wedge */
if (ioctl(fd, DIOCGWEDGEINFO, &dkw) != -1)
return dkw.dkw_size;
if (ioctl(fd, DIOCGDINFO, &lp) != -1) {
ptn = strchr(dev_name, '\0')[-1] - 'a';
if (ptn >= lp.d_npartitions)
return 0;
pp = &lp.d_partitions[ptn];
return pp->p_size;
}
if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode))
return st.st_size / DEV_BSIZE;
return 0;
}
/*
* main().
*/
int
main(int argc, char **argv)
{
int ch;
int CheckOnlyFlag;
int ExpertFlag;
int SFlag;
size_t i;
char specname[MAXPATHLEN];
char rawname[MAXPATHLEN];
const char *raw;
char reply[5];
newsize = 0;
ExpertFlag = 0;
SFlag = 0;
CheckOnlyFlag = 0;
while ((ch = getopt(argc, argv, "cps:vy")) != -1) {
switch (ch) {
case 'c':
CheckOnlyFlag = 1;
break;
case 'p':
progress = 1;
break;
case 's':
SFlag = 1;
newsize = strtoll(optarg, NULL, 10);
if(newsize < 1) {
usage();
}
break;
case 'v':
verbose = 1;
break;
case 'y':
ExpertFlag = 1;
break;
case '?':
/* FALLTHROUGH */
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
usage();
}
special = getfsspecname(specname, sizeof(specname), argv[0]);
if (special == NULL)
err(EXIT_FAILURE, "%s: %s", argv[0], specname);
raw = getdiskrawname(rawname, sizeof(rawname), special);
if (raw != NULL)
special = raw;
if (ExpertFlag == 0 && CheckOnlyFlag == 0) {
printf("It's required to manually run fsck on file system "
"before you can resize it\n\n"
" Did you run fsck on your disk (Yes/No) ? ");
fgets(reply, (int)sizeof(reply), stdin);
if (strcasecmp(reply, "Yes\n")) {
printf("\n Nothing done \n");
exit(EXIT_SUCCESS);
}
}
fd = open(special, O_RDWR, 0);
if (fd < 0)
err(EXIT_FAILURE, "Can't open `%s'", special);
checksmallio();
if (SFlag == 0) {
newsize = get_dev_size(special);
if (newsize == 0)
err(EXIT_FAILURE,
"Can't resize file system, newsize not known.");
}
oldsb = (struct fs *) & sbbuf;
newsb = (struct fs *) (SBLOCKSIZE + (char *) &sbbuf);
for (where = search[i = 0]; search[i] != -1; where = search[++i]) {
readat(where / DEV_BSIZE, oldsb, SBLOCKSIZE);
switch (oldsb->fs_magic) {
case FS_UFS2_MAGIC:
case FS_UFS2EA_MAGIC:
is_ufs2 = 1;
/* FALLTHROUGH */
case FS_UFS1_MAGIC:
needswap = 0;
break;
case FS_UFS2_MAGIC_SWAPPED:
case FS_UFS2EA_MAGIC_SWAPPED:
is_ufs2 = 1;
/* FALLTHROUGH */
case FS_UFS1_MAGIC_SWAPPED:
needswap = 1;
break;
default:
continue;
}
if (!is_ufs2 && where == SBLOCK_UFS2)
continue;
break;
}
if (where == (off_t)-1)
errx(EXIT_FAILURE, "Bad magic number");
if (needswap)
ffs_sb_swap(oldsb,oldsb);
if (oldsb->fs_magic == FS_UFS1_MAGIC &&
(oldsb->fs_old_flags & FS_FLAGS_UPDATED) == 0) {
oldsb->fs_csaddr = oldsb->fs_old_csaddr;
oldsb->fs_size = oldsb->fs_old_size;
oldsb->fs_dsize = oldsb->fs_old_dsize;
oldsb->fs_cstotal.cs_ndir = oldsb->fs_old_cstotal.cs_ndir;
oldsb->fs_cstotal.cs_nbfree = oldsb->fs_old_cstotal.cs_nbfree;
oldsb->fs_cstotal.cs_nifree = oldsb->fs_old_cstotal.cs_nifree;
oldsb->fs_cstotal.cs_nffree = oldsb->fs_old_cstotal.cs_nffree;
/* any others? */
printf("Resizing with ffsv1 superblock\n");
}
oldsb->fs_qbmask = ~(int64_t) oldsb->fs_bmask;
oldsb->fs_qfmask = ~(int64_t) oldsb->fs_fmask;
if (oldsb->fs_ipg % FFS_INOPB(oldsb))
errx(EXIT_FAILURE, "ipg[%d] %% FFS_INOPB[%d] != 0",
(int) oldsb->fs_ipg, (int) FFS_INOPB(oldsb));
/* The superblock is bigger than struct fs (there are trailing
* tables, of non-fixed size); make sure we copy the whole
* thing. SBLOCKSIZE may be an over-estimate, but we do this
* just once, so being generous is cheap. */
memcpy(newsb, oldsb, SBLOCKSIZE);
if (progress) {
progress_ttywidth(0);
signal(SIGWINCH, progress_ttywidth);
}
loadcgs();
if (progress && !CheckOnlyFlag) {
progress_switch(progress);
progress_init();
}
if (newsize > FFS_FSBTODB(oldsb, oldsb->fs_size)) {
if (CheckOnlyFlag)
exit(checkonly());
grow();
} else if (newsize < FFS_FSBTODB(oldsb, oldsb->fs_size)) {
if (is_ufs2)
errx(EXIT_FAILURE,"shrinking not supported for ufs2");
if (CheckOnlyFlag)
exit(checkonly());
shrink();
} else {
if (CheckOnlyFlag)
exit(checkonly());
if (verbose)
printf("No change requested: already %" PRId64
" blocks\n", (int64_t)oldsb->fs_size);
}
flush_cgs();
write_sbs();
if (isplainfile())
ftruncate(fd,newsize * DEV_BSIZE);
return 0;
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: %s [-cpvy] [-s size] special\n",
getprogname());
exit(EXIT_FAILURE);
}
|