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
|
/*
* 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.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#include <sys/condvar_impl.h>
#include <sys/types.h>
#include <sys/t_lock.h>
#include <sys/debug.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/signal.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/disp.h>
#include <sys/user.h>
#include <sys/buf.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/acl.h>
#include <sys/fs/ufs_fs.h>
#include <sys/fs/ufs_inode.h>
#include <sys/fs/ufs_acl.h>
#include <sys/fs/ufs_bio.h>
#include <sys/fs/ufs_quota.h>
#include <sys/kmem.h>
#include <sys/fs/ufs_trans.h>
#include <sys/fs/ufs_panic.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/sysmacros.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/flock.h>
#include <fs/fs_subr.h>
#include <sys/cmn_err.h>
#include <sys/policy.h>
#include <sys/fs/ufs_log.h>
static ino_t hashalloc();
static daddr_t fragextend();
static daddr_t alloccg();
static daddr_t alloccgblk();
static ino_t ialloccg();
static daddr_t mapsearch();
static int findlogstartcg();
extern int inside[], around[];
extern uchar_t *fragtbl[];
void delay();
/*
* Allocate a block in the file system.
*
* The size of the requested block is given, which must be some
* multiple of fs_fsize and <= fs_bsize.
* A preference may be optionally specified. If a preference is given
* the following hierarchy is used to allocate a block:
* 1) allocate the requested block.
* 2) allocate a rotationally optimal block in the same cylinder.
* 3) allocate a block in the same cylinder group.
* 4) quadratically rehash into other cylinder groups, until an
* available block is located.
* If no block preference is given the following hierarchy is used
* to allocate a block:
* 1) allocate a block in the cylinder group that contains the
* inode for the file.
* 2) quadratically rehash into other cylinder groups, until an
* available block is located.
*/
int
alloc(struct inode *ip, daddr_t bpref, int size, daddr_t *bnp, cred_t *cr)
{
struct fs *fs;
struct ufsvfs *ufsvfsp;
daddr_t bno;
int cg;
int err;
char *errmsg = NULL;
size_t len;
ufsvfsp = ip->i_ufsvfs;
fs = ufsvfsp->vfs_fs;
if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
err = ufs_fault(ITOV(ip), "alloc: bad size, dev = 0x%lx,"
" bsize = %d, size = %d, fs = %s\n",
ip->i_dev, fs->fs_bsize, size, fs->fs_fsmnt);
return (err);
}
if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
goto nospace;
if (freespace(fs, ufsvfsp) <= 0 &&
secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0)
goto nospace;
err = chkdq(ip, (long)btodb(size), 0, cr, &errmsg, &len);
/* Note that may not have err, but may have errmsg */
if (errmsg != NULL) {
uprintf(errmsg);
kmem_free(errmsg, len);
errmsg = NULL;
}
if (err)
return (err);
if (bpref >= fs->fs_size)
bpref = 0;
if (bpref == 0)
cg = (int)itog(fs, ip->i_number);
else
cg = dtog(fs, bpref);
bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
(ulong_t (*)())alloccg);
if (bno > 0) {
*bnp = bno;
return (0);
}
/*
* hashalloc() failed because some other thread grabbed
* the last block so unwind the quota operation. We can
* ignore the return because subtractions don't fail and
* size is guaranteed to be >= zero by our caller.
*/
(void) chkdq(ip, -(long)btodb(size), 0, cr, (char **)NULL,
(size_t *)NULL);
nospace:
mutex_enter(&ufsvfsp->vfs_lock);
if ((lbolt - ufsvfsp->vfs_lastwhinetime) > (hz << 2) &&
(!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) {
ufsvfsp->vfs_lastwhinetime = lbolt;
cmn_err(CE_NOTE, "alloc: %s: file system full", fs->fs_fsmnt);
}
mutex_exit(&ufsvfsp->vfs_lock);
return (ENOSPC);
}
/*
* Reallocate a fragment to a bigger size
*
* The number and size of the old block is given, and a preference
* and new size is also specified. The allocator attempts to extend
* the original block. Failing that, the regular block allocator is
* invoked to get an appropriate block.
*/
int
realloccg(struct inode *ip, daddr_t bprev, daddr_t bpref, int osize,
int nsize, daddr_t *bnp, cred_t *cr)
{
daddr_t bno;
struct fs *fs;
struct ufsvfs *ufsvfsp;
int cg, request;
int err;
char *errmsg = NULL;
size_t len;
ufsvfsp = ip->i_ufsvfs;
fs = ufsvfsp->vfs_fs;
if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
(unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
err = ufs_fault(ITOV(ip),
"realloccg: bad size, dev=0x%lx, bsize=%d, "
"osize=%d, nsize=%d, fs=%s\n",
ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
return (err);
}
if (freespace(fs, ufsvfsp) <= 0 &&
secpolicy_fs_minfree(cr, ufsvfsp->vfs_vfs) != 0)
goto nospace;
if (bprev == 0) {
err = ufs_fault(ITOV(ip),
"realloccg: bad bprev, dev = 0x%lx, bsize = %d,"
" bprev = %ld, fs = %s\n", ip->i_dev, fs->fs_bsize, bprev,
fs->fs_fsmnt);
return (err);
}
err = chkdq(ip, (long)btodb(nsize - osize), 0, cr, &errmsg, &len);
/* Note that may not have err, but may have errmsg */
if (errmsg != NULL) {
uprintf(errmsg);
kmem_free(errmsg, len);
errmsg = NULL;
}
if (err)
return (err);
cg = dtog(fs, bprev);
bno = fragextend(ip, cg, (long)bprev, osize, nsize);
if (bno != 0) {
*bnp = bno;
return (0);
}
if (bpref >= fs->fs_size)
bpref = 0;
/*
* When optimizing for time we allocate a full block and
* then only use the upper portion for this request. When
* this file grows again it will grow into the unused portion
* of the block (See fragextend() above). This saves time
* because an extra disk write would be needed if the frags
* following the current allocation were not free. The extra
* disk write is needed to move the data from its current
* location into the newly allocated position.
*
* When optimizing for space we allocate a run of frags
* that is just the right size for this request.
*/
request = (fs->fs_optim == FS_OPTTIME) ? fs->fs_bsize : nsize;
bno = (daddr_t)hashalloc(ip, cg, (long)bpref, request,
(ulong_t (*)())alloccg);
if (bno > 0) {
*bnp = bno;
if (nsize < request)
(void) free(ip, bno + numfrags(fs, nsize),
(off_t)(request - nsize), I_NOCANCEL);
return (0);
}
/*
* hashalloc() failed because some other thread grabbed
* the last block so unwind the quota operation. We can
* ignore the return because subtractions don't fail, and
* our caller guarantees nsize >= osize.
*/
(void) chkdq(ip, -(long)btodb(nsize - osize), 0, cr, (char **)NULL,
(size_t *)NULL);
nospace:
mutex_enter(&ufsvfsp->vfs_lock);
if ((lbolt - ufsvfsp->vfs_lastwhinetime) > (hz << 2) &&
(!(TRANS_ISTRANS(ufsvfsp)) || !(ip->i_flag & IQUIET))) {
ufsvfsp->vfs_lastwhinetime = lbolt;
cmn_err(CE_NOTE,
"realloccg %s: file system full", fs->fs_fsmnt);
}
mutex_exit(&ufsvfsp->vfs_lock);
return (ENOSPC);
}
/*
* Allocate an inode in the file system.
*
* A preference may be optionally specified. If a preference is given
* the following hierarchy is used to allocate an inode:
* 1) allocate the requested inode.
* 2) allocate an inode in the same cylinder group.
* 3) quadratically rehash into other cylinder groups, until an
* available inode is located.
* If no inode preference is given the following hierarchy is used
* to allocate an inode:
* 1) allocate an inode in cylinder group 0.
* 2) quadratically rehash into other cylinder groups, until an
* available inode is located.
*/
int
ufs_ialloc(struct inode *pip,
ino_t ipref, mode_t mode, struct inode **ipp, cred_t *cr)
{
struct inode *ip;
struct fs *fs;
int cg;
ino_t ino;
int err;
int nifree;
struct ufsvfs *ufsvfsp = pip->i_ufsvfs;
char *errmsg = NULL;
size_t len;
ASSERT(RW_WRITE_HELD(&pip->i_rwlock));
fs = pip->i_fs;
loop:
nifree = fs->fs_cstotal.cs_nifree;
if (nifree == 0)
goto noinodes;
/*
* Shadow inodes don't count against a user's inode allocation.
* They are an implementation method and not a resource.
*/
if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
err = chkiq((struct ufsvfs *)ITOV(pip)->v_vfsp->vfs_data,
/* change */ 1, (struct inode *)NULL, crgetuid(cr), 0,
cr, &errmsg, &len);
/*
* As we haven't acquired any locks yet, dump the message
* now.
*/
if (errmsg != NULL) {
uprintf(errmsg);
kmem_free(errmsg, len);
errmsg = NULL;
}
if (err)
return (err);
}
if (ipref >= (ulong_t)(fs->fs_ncg * fs->fs_ipg))
ipref = 0;
cg = (int)itog(fs, ipref);
ino = (ino_t)hashalloc(pip, cg, (long)ipref, (int)mode,
(ulong_t (*)())ialloccg);
if (ino == 0) {
if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
/*
* We can safely ignore the return from chkiq()
* because deallocations can only fail if we
* can't get the user's quota info record off
* the disk due to an I/O error. In that case,
* the quota subsystem is already messed up.
*/
(void) chkiq(ufsvfsp, /* change */ -1,
(struct inode *)NULL, crgetuid(cr), 0, cr,
(char **)NULL, (size_t *)NULL);
}
goto noinodes;
}
err = ufs_iget(pip->i_vfs, ino, ipp, cr);
if (err) {
if ((mode != IFSHAD) && (mode != IFATTRDIR)) {
/*
* See above comment about why it is safe to ignore an
* error return here.
*/
(void) chkiq(ufsvfsp, /* change */ -1,
(struct inode *)NULL, crgetuid(cr), 0, cr,
(char **)NULL, (size_t *)NULL);
}
ufs_ifree(pip, ino, 0);
return (err);
}
ip = *ipp;
ASSERT(!ip->i_ufs_acl);
ASSERT(!ip->i_dquot);
rw_enter(&ip->i_contents, RW_WRITER);
/*
* Check if we really got a free inode, if not then complain
* and mark the inode ISTALE so that it will be freed by the
* ufs idle thread eventually and will not be sent to ufs_delete().
*/
if (ip->i_mode || (ip->i_nlink > 0)) {
ip->i_flag |= ISTALE;
rw_exit(&ip->i_contents);
VN_RELE(ITOV(ip));
cmn_err(CE_WARN,
"%s: unexpected allocated inode %d, run fsck(1M)%s",
fs->fs_fsmnt, (int)ino,
(TRANS_ISTRANS(ufsvfsp) ? " -o f" : ""));
goto loop;
}
/*
* Check the inode has no size or data blocks.
* This could have happened if the truncation failed when
* deleting the inode. It used to be possible for this to occur
* if a block allocation failed when iteratively truncating a
* large file using logging and with a full file system.
* This was fixed with bug fix 4348738. However, truncation may
* still fail on an IO error. So in all cases for safety and
* security we clear out the size; the blocks allocated; and
* pointers to the blocks. This will ultimately cause a fsck
* error of un-accounted for blocks, but its a fairly benign error,
* and possibly the correct thing to do anyway as accesssing those
* blocks agains may lead to more IO errors.
*/
if (ip->i_size || ip->i_blocks) {
int i;
if (ip->i_size) {
cmn_err(CE_WARN,
"%s: free inode %d had size 0x%llx, run fsck(1M)%s",
fs->fs_fsmnt, (int)ino, ip->i_size,
(TRANS_ISTRANS(ufsvfsp) ? " -o f" : ""));
}
/*
* Clear any garbage left behind.
*/
ip->i_size = (u_offset_t)0;
ip->i_blocks = 0;
for (i = 0; i < NDADDR; i++)
ip->i_db[i] = 0;
for (i = 0; i < NIADDR; i++)
ip->i_ib[i] = 0;
}
/*
* Initialize the link count
*/
ip->i_nlink = 0;
/*
* Clear the old flags
*/
ip->i_flag &= IREF;
/*
* Access times are not really defined if the fs is mounted
* with 'noatime'. But it can cause nfs clients to fail
* open() if the atime is not a legal value. Set a legal value
* here when the inode is allocated.
*/
if (ufsvfsp->vfs_noatime) {
mutex_enter(&ufs_iuniqtime_lock);
ip->i_atime = iuniqtime;
mutex_exit(&ufs_iuniqtime_lock);
}
rw_exit(&ip->i_contents);
return (0);
noinodes:
if (!(TRANS_ISTRANS(ufsvfsp)) || !(pip->i_flag & IQUIET))
cmn_err(CE_NOTE, "%s: out of inodes\n", fs->fs_fsmnt);
return (ENOSPC);
}
/*
* Find a cylinder group to place a directory.
* Returns an inumber within the selected cylinder group.
* Note, the vfs_lock is not needed as we don't require exact cg summary info.
*
* If the switch ufs_close_dirs is set, then the policy is to use
* the current cg if it has more than 25% free inodes and more
* than 25% free blocks. Otherwise the cgs are searched from
* the beginning and the first cg with the same criteria is
* used. If that is also null then we revert to the old algorithm.
* This tends to cluster files at the beginning of the disk
* until the disk gets full.
*
* Otherwise if ufs_close_dirs is not set then the original policy is
* used which is to select from among those cylinder groups with
* above the average number of free inodes, the one with the smallest
* number of directories.
*/
int ufs_close_dirs = 1; /* allocate directories close as possible */
ino_t
dirpref(inode_t *dp)
{
int cg, minndir, mincg, avgifree, mininode, minbpg, ifree;
struct fs *fs = dp->i_fs;
cg = itog(fs, dp->i_number);
mininode = fs->fs_ipg >> 2;
minbpg = fs->fs_maxbpg >> 2;
if (ufs_close_dirs &&
(fs->fs_cs(fs, cg).cs_nifree > mininode) &&
(fs->fs_cs(fs, cg).cs_nbfree > minbpg)) {
return (dp->i_number);
}
avgifree = fs->fs_cstotal.cs_nifree / fs->fs_ncg;
minndir = fs->fs_ipg;
mincg = 0;
for (cg = 0; cg < fs->fs_ncg; cg++) {
ifree = fs->fs_cs(fs, cg).cs_nifree;
if (ufs_close_dirs &&
(ifree > mininode) &&
(fs->fs_cs(fs, cg).cs_nbfree > minbpg)) {
return ((ino_t)(fs->fs_ipg * cg));
}
if ((fs->fs_cs(fs, cg).cs_ndir < minndir) &&
(ifree >= avgifree)) {
mincg = cg;
minndir = fs->fs_cs(fs, cg).cs_ndir;
}
}
return ((ino_t)(fs->fs_ipg * mincg));
}
/*
* Select the desired position for the next block in a file. The file is
* logically divided into sections. The first section is composed of the
* direct blocks. Each additional section contains fs_maxbpg blocks.
*
* If no blocks have been allocated in the first section, the policy is to
* request a block in the same cylinder group as the inode that describes
* the file. If no blocks have been allocated in any other section, the
* policy is to place the section in a cylinder group with a greater than
* average number of free blocks. An appropriate cylinder group is found
* by using a rotor that sweeps the cylinder groups. When a new group of
* blocks is needed, the sweep begins in the cylinder group following the
* cylinder group from which the previous allocation was made. The sweep
* continues until a cylinder group with greater than the average number
* of free blocks is found. If the allocation is for the first block in an
* indirect block, the information on the previous allocation is unavailable;
* here a best guess is made based upon the logical block number being
* allocated.
*
* If a section is already partially allocated, the policy is to
* contiguously allocate fs_maxcontig blocks. The end of one of these
* contiguous blocks and the beginning of the next is physically separated
* so that the disk head will be in transit between them for at least
* fs_rotdelay milliseconds. This is to allow time for the processor to
* schedule another I/O transfer.
*/
daddr_t
blkpref(struct inode *ip, daddr_t lbn, int indx, daddr32_t *bap)
{
struct fs *fs;
struct ufsvfs *ufsvfsp;
int cg;
int avgbfree, startcg;
daddr_t nextblk;
ufsvfsp = ip->i_ufsvfs;
fs = ip->i_fs;
if (indx % fs->fs_maxbpg == 0 || bap[indx - 1] == 0) {
if (lbn < NDADDR) {
cg = itog(fs, ip->i_number);
return (fs->fs_fpg * cg + fs->fs_frag);
}
/*
* Find a cylinder with greater than average
* number of unused data blocks.
*/
if (indx == 0 || bap[indx - 1] == 0)
startcg = itog(fs, ip->i_number) + lbn / fs->fs_maxbpg;
else
startcg = dtog(fs, bap[indx - 1]) + 1;
startcg %= fs->fs_ncg;
mutex_enter(&ufsvfsp->vfs_lock);
avgbfree = fs->fs_cstotal.cs_nbfree / fs->fs_ncg;
/*
* used for computing log space for writes/truncs
*/
ufsvfsp->vfs_avgbfree = avgbfree;
for (cg = startcg; cg < fs->fs_ncg; cg++)
if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
fs->fs_cgrotor = cg;
mutex_exit(&ufsvfsp->vfs_lock);
return (fs->fs_fpg * cg + fs->fs_frag);
}
for (cg = 0; cg <= startcg; cg++)
if (fs->fs_cs(fs, cg).cs_nbfree >= avgbfree) {
fs->fs_cgrotor = cg;
mutex_exit(&ufsvfsp->vfs_lock);
return (fs->fs_fpg * cg + fs->fs_frag);
}
mutex_exit(&ufsvfsp->vfs_lock);
return (NULL);
}
/*
* One or more previous blocks have been laid out. If less
* than fs_maxcontig previous blocks are contiguous, the
* next block is requested contiguously, otherwise it is
* requested rotationally delayed by fs_rotdelay milliseconds.
*/
nextblk = bap[indx - 1];
/*
* Provision for fallocate to return positive
* blk preference based on last allocation
*/
if (nextblk < 0 && nextblk != UFS_HOLE) {
nextblk = (-bap[indx - 1]) + fs->fs_frag;
} else {
nextblk = bap[indx - 1] + fs->fs_frag;
}
if (indx > fs->fs_maxcontig && bap[indx - fs->fs_maxcontig] +
blkstofrags(fs, fs->fs_maxcontig) != nextblk) {
return (nextblk);
}
if (fs->fs_rotdelay != 0)
/*
* Here we convert ms of delay to frags as:
* (frags) = (ms) * (rev/sec) * (sect/rev) /
* ((sect/frag) * (ms/sec))
* then round up to the next block.
*/
nextblk += roundup(fs->fs_rotdelay * fs->fs_rps * fs->fs_nsect /
(NSPF(fs) * 1000), fs->fs_frag);
return (nextblk);
}
/*
* Free a block or fragment.
*
* The specified block or fragment is placed back in the
* free map. If a fragment is deallocated, a possible
* block reassembly is checked.
*/
void
free(struct inode *ip, daddr_t bno, off_t size, int flags)
{
struct fs *fs = ip->i_fs;
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
struct ufs_q *delq = &ufsvfsp->vfs_delete;
struct ufs_delq_info *delq_info = &ufsvfsp->vfs_delete_info;
struct cg *cgp;
struct buf *bp;
int cg, bmap, bbase;
int i;
uchar_t *blksfree;
int *blktot;
short *blks;
daddr_t blkno, cylno, rpos;
/*
* fallocate'd files will have negative block address.
* So negate it again to get original block address.
*/
if (bno < 0 && (bno % fs->fs_frag == 0) && bno != UFS_HOLE) {
bno = -bno;
}
if ((unsigned long)size > fs->fs_bsize || fragoff(fs, size) != 0) {
(void) ufs_fault(ITOV(ip),
"free: bad size, dev = 0x%lx, bsize = %d, size = %d, "
"fs = %s\n", ip->i_dev, fs->fs_bsize,
(int)size, fs->fs_fsmnt);
return;
}
cg = dtog(fs, bno);
ASSERT(!ufs_badblock(ip, bno));
bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
(int)fs->fs_cgsize);
cgp = bp->b_un.b_cg;
if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
brelse(bp);
return;
}
if (!(flags & I_NOCANCEL))
TRANS_CANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size, flags);
if (flags & (I_DIR|I_IBLK|I_SHAD|I_QUOTA)) {
TRANS_MATA_FREE(ufsvfsp, ldbtob(fsbtodb(fs, bno)), size);
}
blksfree = cg_blksfree(cgp);
blktot = cg_blktot(cgp);
mutex_enter(&ufsvfsp->vfs_lock);
cgp->cg_time = gethrestime_sec();
bno = dtogd(fs, bno);
if (size == fs->fs_bsize) {
blkno = fragstoblks(fs, bno);
cylno = cbtocylno(fs, bno);
rpos = cbtorpos(ufsvfsp, bno);
blks = cg_blks(ufsvfsp, cgp, cylno);
if (!isclrblock(fs, blksfree, blkno)) {
mutex_exit(&ufsvfsp->vfs_lock);
brelse(bp);
(void) ufs_fault(ITOV(ip), "free: freeing free block, "
"dev:0x%lx, block:%ld, ino:%lu, fs:%s",
ip->i_dev, bno, ip->i_number, fs->fs_fsmnt);
return;
}
setblock(fs, blksfree, blkno);
blks[rpos]++;
blktot[cylno]++;
cgp->cg_cs.cs_nbfree++; /* Log below */
fs->fs_cstotal.cs_nbfree++;
fs->fs_cs(fs, cg).cs_nbfree++;
if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) {
mutex_enter(&delq->uq_mutex);
delq_info->delq_unreclaimed_blocks -=
btodb(fs->fs_bsize);
mutex_exit(&delq->uq_mutex);
}
} else {
bbase = bno - fragnum(fs, bno);
/*
* Decrement the counts associated with the old frags
*/
bmap = blkmap(fs, blksfree, bbase);
fragacct(fs, bmap, cgp->cg_frsum, -1);
/*
* Deallocate the fragment
*/
for (i = 0; i < numfrags(fs, size); i++) {
if (isset(blksfree, bno + i)) {
brelse(bp);
mutex_exit(&ufsvfsp->vfs_lock);
(void) ufs_fault(ITOV(ip),
"free: freeing free frag, "
"dev:0x%lx, blk:%ld, cg:%d, "
"ino:%lu, fs:%s",
ip->i_dev,
bno + i,
cgp->cg_cgx,
ip->i_number,
fs->fs_fsmnt);
return;
}
setbit(blksfree, bno + i);
}
cgp->cg_cs.cs_nffree += i;
fs->fs_cstotal.cs_nffree += i;
fs->fs_cs(fs, cg).cs_nffree += i;
if (TRANS_ISTRANS(ufsvfsp) && (flags & I_ACCT)) {
mutex_enter(&delq->uq_mutex);
delq_info->delq_unreclaimed_blocks -=
btodb(i * fs->fs_fsize);
mutex_exit(&delq->uq_mutex);
}
/*
* Add back in counts associated with the new frags
*/
bmap = blkmap(fs, blksfree, bbase);
fragacct(fs, bmap, cgp->cg_frsum, 1);
/*
* If a complete block has been reassembled, account for it
*/
blkno = fragstoblks(fs, bbase);
if (isblock(fs, blksfree, blkno)) {
cylno = cbtocylno(fs, bbase);
rpos = cbtorpos(ufsvfsp, bbase);
blks = cg_blks(ufsvfsp, cgp, cylno);
blks[rpos]++;
blktot[cylno]++;
cgp->cg_cs.cs_nffree -= fs->fs_frag;
fs->fs_cstotal.cs_nffree -= fs->fs_frag;
fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
cgp->cg_cs.cs_nbfree++;
fs->fs_cstotal.cs_nbfree++;
fs->fs_cs(fs, cg).cs_nbfree++;
}
}
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
}
/*
* Free an inode.
*
* The specified inode is placed back in the free map.
*/
void
ufs_ifree(struct inode *ip, ino_t ino, mode_t mode)
{
struct fs *fs = ip->i_fs;
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
struct cg *cgp;
struct buf *bp;
unsigned int inot;
int cg;
char *iused;
if (ip->i_number == ino && ip->i_mode != 0) {
(void) ufs_fault(ITOV(ip),
"ufs_ifree: illegal mode: (imode) %o, (omode) %o, ino %d, "
"fs = %s\n",
ip->i_mode, mode, (int)ip->i_number, fs->fs_fsmnt);
return;
}
if (ino >= fs->fs_ipg * fs->fs_ncg) {
(void) ufs_fault(ITOV(ip),
"ifree: range, dev = 0x%x, ino = %d, fs = %s\n",
(int)ip->i_dev, (int)ino, fs->fs_fsmnt);
return;
}
cg = (int)itog(fs, ino);
bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
(int)fs->fs_cgsize);
cgp = bp->b_un.b_cg;
if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
brelse(bp);
return;
}
mutex_enter(&ufsvfsp->vfs_lock);
cgp->cg_time = gethrestime_sec();
iused = cg_inosused(cgp);
inot = (unsigned int)(ino % (ulong_t)fs->fs_ipg);
if (isclr(iused, inot)) {
mutex_exit(&ufsvfsp->vfs_lock);
brelse(bp);
(void) ufs_fault(ITOV(ip), "ufs_ifree: freeing free inode, "
"mode: (imode) %o, (omode) %o, ino:%d, "
"fs:%s",
ip->i_mode, mode, (int)ino, fs->fs_fsmnt);
return;
}
clrbit(iused, inot);
if (inot < (ulong_t)cgp->cg_irotor)
cgp->cg_irotor = inot;
cgp->cg_cs.cs_nifree++;
fs->fs_cstotal.cs_nifree++;
fs->fs_cs(fs, cg).cs_nifree++;
if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) {
cgp->cg_cs.cs_ndir--;
fs->fs_cstotal.cs_ndir--;
fs->fs_cs(fs, cg).cs_ndir--;
}
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
}
/*
* Implement the cylinder overflow algorithm.
*
* The policy implemented by this algorithm is:
* 1) allocate the block in its requested cylinder group.
* 2) quadratically rehash on the cylinder group number.
* 3) brute force search for a free block.
* The size parameter means size for data blocks, mode for inodes.
*/
static ino_t
hashalloc(struct inode *ip, int cg, long pref, int size, ulong_t (*allocator)())
{
struct fs *fs;
int i;
long result;
int icg = cg;
fs = ip->i_fs;
/*
* 1: preferred cylinder group
*/
result = (*allocator)(ip, cg, pref, size);
if (result)
return (result);
/*
* 2: quadratic rehash
*/
for (i = 1; i < fs->fs_ncg; i *= 2) {
cg += i;
if (cg >= fs->fs_ncg)
cg -= fs->fs_ncg;
result = (*allocator)(ip, cg, 0, size);
if (result)
return (result);
}
/*
* 3: brute force search
* Note that we start at i == 2, since 0 was checked initially,
* and 1 is always checked in the quadratic rehash.
*/
cg = (icg + 2) % fs->fs_ncg;
for (i = 2; i < fs->fs_ncg; i++) {
result = (*allocator)(ip, cg, 0, size);
if (result)
return (result);
cg++;
if (cg == fs->fs_ncg)
cg = 0;
}
return (NULL);
}
/*
* Determine whether a fragment can be extended.
*
* Check to see if the necessary fragments are available, and
* if they are, allocate them.
*/
static daddr_t
fragextend(struct inode *ip, int cg, long bprev, int osize, int nsize)
{
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
struct fs *fs = ip->i_fs;
struct buf *bp;
struct cg *cgp;
uchar_t *blksfree;
long bno;
int frags, bbase;
int i, j;
if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
return (NULL);
frags = numfrags(fs, nsize);
bbase = (int)fragnum(fs, bprev);
if (bbase > fragnum(fs, (bprev + frags - 1))) {
/* cannot extend across a block boundary */
return (NULL);
}
bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
(int)fs->fs_cgsize);
cgp = bp->b_un.b_cg;
if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
brelse(bp);
return (NULL);
}
blksfree = cg_blksfree(cgp);
mutex_enter(&ufsvfsp->vfs_lock);
bno = dtogd(fs, bprev);
for (i = numfrags(fs, osize); i < frags; i++) {
if (isclr(blksfree, bno + i)) {
mutex_exit(&ufsvfsp->vfs_lock);
brelse(bp);
return (NULL);
}
if ((TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, bprev + i)),
fs->fs_fsize))) {
mutex_exit(&ufsvfsp->vfs_lock);
brelse(bp);
return (NULL);
}
}
cgp->cg_time = gethrestime_sec();
/*
* The current fragment can be extended,
* deduct the count on fragment being extended into
* increase the count on the remaining fragment (if any)
* allocate the extended piece.
*/
for (i = frags; i < fs->fs_frag - bbase; i++)
if (isclr(blksfree, bno + i))
break;
j = i - numfrags(fs, osize);
cgp->cg_frsum[j]--;
ASSERT(cgp->cg_frsum[j] >= 0);
if (i != frags)
cgp->cg_frsum[i - frags]++;
for (i = numfrags(fs, osize); i < frags; i++) {
clrbit(blksfree, bno + i);
cgp->cg_cs.cs_nffree--;
fs->fs_cs(fs, cg).cs_nffree--;
fs->fs_cstotal.cs_nffree--;
}
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
return ((daddr_t)bprev);
}
/*
* Determine whether a block can be allocated.
*
* Check to see if a block of the apprpriate size
* is available, and if it is, allocate it.
*/
static daddr_t
alloccg(struct inode *ip, int cg, daddr_t bpref, int size)
{
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
struct fs *fs = ip->i_fs;
struct buf *bp;
struct cg *cgp;
uchar_t *blksfree;
int bno, frags;
int allocsiz;
int i;
/*
* Searching for space could be time expensive so do some
* up front checking to verify that there is actually space
* available (free blocks or free frags).
*/
if (fs->fs_cs(fs, cg).cs_nbfree == 0) {
if (size == fs->fs_bsize)
return (0);
/*
* If there are not enough free frags then return.
*/
if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, size))
return (0);
}
bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
(int)fs->fs_cgsize);
cgp = bp->b_un.b_cg;
if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
(cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
brelse(bp);
return (0);
}
blksfree = cg_blksfree(cgp);
mutex_enter(&ufsvfsp->vfs_lock);
cgp->cg_time = gethrestime_sec();
if (size == fs->fs_bsize) {
if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0)
goto errout;
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
return (bno);
}
/*
* Check fragment bitmap to see if any fragments are already available.
* mapsearch() may fail because the fragment that fits this request
* might still be on the cancel list and not available for re-use yet.
* Look for a bigger sized fragment to allocate first before we have
* to give up and fragment a whole new block eventually.
*/
frags = numfrags(fs, size);
allocsiz = frags;
next_size:
for (; allocsiz < fs->fs_frag; allocsiz++)
if (cgp->cg_frsum[allocsiz] != 0)
break;
if (allocsiz != fs->fs_frag) {
bno = mapsearch(ufsvfsp, cgp, bpref, allocsiz);
if (bno < 0 && allocsiz < (fs->fs_frag - 1)) {
allocsiz++;
goto next_size;
}
}
if (allocsiz == fs->fs_frag || bno < 0) {
/*
* No fragments were available, so a block
* will be allocated and hacked up.
*/
if (cgp->cg_cs.cs_nbfree == 0)
goto errout;
if ((bno = alloccgblk(ufsvfsp, cgp, bpref, bp)) == 0)
goto errout;
bpref = dtogd(fs, bno);
for (i = frags; i < fs->fs_frag; i++)
setbit(blksfree, bpref + i);
i = fs->fs_frag - frags;
cgp->cg_cs.cs_nffree += i;
fs->fs_cstotal.cs_nffree += i;
fs->fs_cs(fs, cg).cs_nffree += i;
cgp->cg_frsum[i]++;
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
return (bno);
}
for (i = 0; i < frags; i++)
clrbit(blksfree, bno + i);
cgp->cg_cs.cs_nffree -= frags;
fs->fs_cstotal.cs_nffree -= frags;
fs->fs_cs(fs, cg).cs_nffree -= frags;
cgp->cg_frsum[allocsiz]--;
ASSERT(cgp->cg_frsum[allocsiz] >= 0);
if (frags != allocsiz) {
cgp->cg_frsum[allocsiz - frags]++;
}
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
return (cg * fs->fs_fpg + bno);
errout:
mutex_exit(&ufsvfsp->vfs_lock);
brelse(bp);
return (0);
}
/*
* Allocate a block in a cylinder group.
*
* This algorithm implements the following policy:
* 1) allocate the requested block.
* 2) allocate a rotationally optimal block in the same cylinder.
* 3) allocate the next available block on the block rotor for the
* specified cylinder group.
* Note that this routine only allocates fs_bsize blocks; these
* blocks may be fragmented by the routine that allocates them.
*/
static daddr_t
alloccgblk(
struct ufsvfs *ufsvfsp,
struct cg *cgp,
daddr_t bpref,
struct buf *bp)
{
daddr_t bno;
int cylno, pos, delta, rotbl_size;
short *cylbp;
int i;
struct fs *fs;
uchar_t *blksfree;
daddr_t blkno, rpos, frag;
short *blks;
int32_t *blktot;
ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock));
fs = ufsvfsp->vfs_fs;
blksfree = cg_blksfree(cgp);
if (bpref == 0) {
bpref = cgp->cg_rotor;
goto norot;
}
bpref = blknum(fs, bpref);
bpref = dtogd(fs, bpref);
/*
* If the requested block is available, use it.
*/
if (isblock(fs, blksfree, (daddr_t)fragstoblks(fs, bpref))) {
bno = bpref;
goto gotit;
}
/*
* Check for a block available on the same cylinder.
*/
cylno = cbtocylno(fs, bpref);
if (cg_blktot(cgp)[cylno] == 0)
goto norot;
if (fs->fs_cpc == 0) {
/*
* Block layout info is not available, so just
* have to take any block in this cylinder.
*/
bpref = howmany(fs->fs_spc * cylno, NSPF(fs));
goto norot;
}
/*
* Check the summary information to see if a block is
* available in the requested cylinder starting at the
* requested rotational position and proceeding around.
*/
cylbp = cg_blks(ufsvfsp, cgp, cylno);
pos = cbtorpos(ufsvfsp, bpref);
for (i = pos; i < ufsvfsp->vfs_nrpos; i++)
if (cylbp[i] > 0)
break;
if (i == ufsvfsp->vfs_nrpos)
for (i = 0; i < pos; i++)
if (cylbp[i] > 0)
break;
if (cylbp[i] > 0) {
/*
* Found a rotational position, now find the actual
* block. A "panic" if none is actually there.
*/
/*
* Up to this point, "pos" has referred to the rotational
* position of the desired block. From now on, it holds
* the offset of the current cylinder within a cylinder
* cycle. (A cylinder cycle refers to a set of cylinders
* which are described by a single rotational table; the
* size of the cycle is fs_cpc.)
*
* bno is set to the block number of the first block within
* the current cylinder cycle.
*/
pos = cylno % fs->fs_cpc;
bno = (cylno - pos) * fs->fs_spc / NSPB(fs);
/*
* The blocks within a cylinder are grouped into equivalence
* classes according to their "rotational position." There
* are two tables used to determine these classes.
*
* The positional offset table (fs_postbl) has an entry for
* each rotational position of each cylinder in a cylinder
* cycle. This entry contains the relative block number
* (counting from the start of the cylinder cycle) of the
* first block in the equivalence class for that position
* and that cylinder. Positions for which no blocks exist
* are indicated by a -1.
*
* The rotational delta table (fs_rotbl) has an entry for
* each block in a cylinder cycle. This entry contains
* the offset from that block to the next block in the
* same equivalence class. The last block in the class
* is indicated by a zero in the table.
*
* The following code, then, walks through all of the blocks
* in the cylinder (cylno) which we're allocating within
* which are in the equivalence class for the rotational
* position (i) which we're allocating within.
*/
if (fs_postbl(ufsvfsp, pos)[i] == -1) {
(void) ufs_fault(ufsvfsp->vfs_root,
"alloccgblk: cyl groups corrupted, pos = %d, "
"i = %d, fs = %s\n", pos, i, fs->fs_fsmnt);
return (0);
}
/*
* There is one entry in the rotational table for each block
* in the cylinder cycle. These are whole blocks, not frags.
*/
rotbl_size = (fs->fs_cpc * fs->fs_spc) >>
(fs->fs_fragshift + fs->fs_fsbtodb);
/*
* As we start, "i" is the rotational position within which
* we're searching. After the next line, it will be a block
* number (relative to the start of the cylinder cycle)
* within the equivalence class of that rotational position.
*/
i = fs_postbl(ufsvfsp, pos)[i];
for (;;) {
if (isblock(fs, blksfree, (daddr_t)(bno + i))) {
bno = blkstofrags(fs, (bno + i));
goto gotit;
}
delta = fs_rotbl(fs)[i];
if (delta <= 0 || /* End of chain, or */
delta + i > rotbl_size) /* end of table? */
break; /* If so, panic. */
i += delta;
}
(void) ufs_fault(ufsvfsp->vfs_root,
"alloccgblk: can't find blk in cyl, pos:%d, i:%d, "
"fs:%s bno: %x\n", pos, i, fs->fs_fsmnt, (int)bno);
return (0);
}
norot:
/*
* No blocks in the requested cylinder, so take
* next available one in this cylinder group.
*/
bno = mapsearch(ufsvfsp, cgp, bpref, (int)fs->fs_frag);
if (bno < 0)
return (0);
cgp->cg_rotor = bno;
gotit:
blkno = fragstoblks(fs, bno);
frag = (cgp->cg_cgx * fs->fs_fpg) + bno;
if (TRANS_ISCANCEL(ufsvfsp, ldbtob(fsbtodb(fs, frag)), fs->fs_bsize))
goto norot;
clrblock(fs, blksfree, (long)blkno);
/*
* the other cg/sb/si fields are TRANS'ed by the caller
*/
cgp->cg_cs.cs_nbfree--;
fs->fs_cstotal.cs_nbfree--;
fs->fs_cs(fs, cgp->cg_cgx).cs_nbfree--;
cylno = cbtocylno(fs, bno);
blks = cg_blks(ufsvfsp, cgp, cylno);
rpos = cbtorpos(ufsvfsp, bno);
blktot = cg_blktot(cgp);
blks[rpos]--;
blktot[cylno]--;
TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
fs->fs_fmod = 1;
return (frag);
}
/*
* Determine whether an inode can be allocated.
*
* Check to see if an inode is available, and if it is,
* allocate it using the following policy:
* 1) allocate the requested inode.
* 2) allocate the next available inode after the requested
* inode in the specified cylinder group.
*/
static ino_t
ialloccg(struct inode *ip, int cg, daddr_t ipref, int mode)
{
struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
struct fs *fs = ip->i_fs;
struct cg *cgp;
struct buf *bp;
int start, len, loc, map, i;
char *iused;
if (fs->fs_cs(fs, cg).cs_nifree == 0)
return (0);
bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, cgtod(fs, cg)),
(int)fs->fs_cgsize);
cgp = bp->b_un.b_cg;
if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
cgp->cg_cs.cs_nifree == 0) {
brelse(bp);
return (0);
}
iused = cg_inosused(cgp);
mutex_enter(&ufsvfsp->vfs_lock);
/*
* While we are waiting for the mutex, someone may have taken
* the last available inode. Need to recheck.
*/
if (cgp->cg_cs.cs_nifree == 0) {
mutex_exit(&ufsvfsp->vfs_lock);
brelse(bp);
return (0);
}
cgp->cg_time = gethrestime_sec();
if (ipref) {
ipref %= fs->fs_ipg;
if (isclr(iused, ipref))
goto gotit;
}
start = cgp->cg_irotor / NBBY;
len = howmany(fs->fs_ipg - cgp->cg_irotor, NBBY);
loc = skpc(0xff, (uint_t)len, &iused[start]);
if (loc == 0) {
len = start + 1;
start = 0;
loc = skpc(0xff, (uint_t)len, &iused[0]);
if (loc == 0) {
mutex_exit(&ufsvfsp->vfs_lock);
(void) ufs_fault(ITOV(ip),
"ialloccg: map corrupted, cg = %d, irotor = %d, "
"fs = %s\n", cg, (int)cgp->cg_irotor, fs->fs_fsmnt);
return (0);
}
}
i = start + len - loc;
map = iused[i];
ipref = i * NBBY;
for (i = 1; i < (1 << NBBY); i <<= 1, ipref++) {
if ((map & i) == 0) {
cgp->cg_irotor = ipref;
goto gotit;
}
}
mutex_exit(&ufsvfsp->vfs_lock);
(void) ufs_fault(ITOV(ip), "ialloccg: block not in mapfs = %s",
fs->fs_fsmnt);
return (0);
gotit:
setbit(iused, ipref);
cgp->cg_cs.cs_nifree--;
fs->fs_cstotal.cs_nifree--;
fs->fs_cs(fs, cg).cs_nifree--;
if (((mode & IFMT) == IFDIR) || ((mode & IFMT) == IFATTRDIR)) {
cgp->cg_cs.cs_ndir++;
fs->fs_cstotal.cs_ndir++;
fs->fs_cs(fs, cg).cs_ndir++;
}
fs->fs_fmod = 1;
ufs_notclean(ufsvfsp);
TRANS_BUF(ufsvfsp, 0, fs->fs_cgsize, bp, DT_CG);
TRANS_SI(ufsvfsp, fs, cg);
bdrwrite(bp);
return (cg * fs->fs_ipg + ipref);
}
/*
* Find a block of the specified size in the specified cylinder group.
*
* It is a panic if a request is made to find a block if none are
* available.
*/
static daddr_t
mapsearch(struct ufsvfs *ufsvfsp, struct cg *cgp, daddr_t bpref,
int allocsiz)
{
struct fs *fs = ufsvfsp->vfs_fs;
daddr_t bno, cfrag;
int start, len, loc, i, last, first, secondtime;
int blk, field, subfield, pos;
int gotit;
/*
* ufsvfs->vfs_lock is held when calling this.
*/
/*
* Find the fragment by searching through the
* free block map for an appropriate bit pattern.
*/
if (bpref)
start = dtogd(fs, bpref) / NBBY;
else
start = cgp->cg_frotor / NBBY;
/*
* the following loop performs two scans -- the first scan
* searches the bottom half of the array for a match and the
* second scan searches the top half of the array. The loops
* have been merged just to make things difficult.
*/
first = start;
last = howmany(fs->fs_fpg, NBBY);
secondtime = 0;
cfrag = cgp->cg_cgx * fs->fs_fpg;
while (first < last) {
len = last - first;
/*
* search the array for a match
*/
loc = scanc((unsigned)len, (uchar_t *)&cg_blksfree(cgp)[first],
(uchar_t *)fragtbl[fs->fs_frag],
(int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
/*
* match found
*/
if (loc) {
bno = (last - loc) * NBBY;
/*
* Found the byte in the map, sift
* through the bits to find the selected frag
*/
cgp->cg_frotor = bno;
gotit = 0;
for (i = bno + NBBY; bno < i; bno += fs->fs_frag) {
blk = blkmap(fs, cg_blksfree(cgp), bno);
blk <<= 1;
field = around[allocsiz];
subfield = inside[allocsiz];
for (pos = 0;
pos <= fs->fs_frag - allocsiz;
pos++) {
if ((blk & field) == subfield) {
gotit++;
break;
}
field <<= 1;
subfield <<= 1;
}
if (gotit)
break;
}
bno += pos;
/*
* success if block is *not* being converted from
* metadata into userdata (harpy). If so, ignore.
*/
if (!TRANS_ISCANCEL(ufsvfsp,
ldbtob(fsbtodb(fs, (cfrag+bno))),
allocsiz * fs->fs_fsize))
return (bno);
/*
* keep looking -- this block is being converted
*/
first = (last - loc) + 1;
loc = 0;
if (first < last)
continue;
}
/*
* no usable matches in bottom half -- now search the top half
*/
if (secondtime)
/*
* no usable matches in top half -- all done
*/
break;
secondtime = 1;
last = start + 1;
first = 0;
}
/*
* no usable matches
*/
return ((daddr_t)-1);
}
#define UFSNADDR (NDADDR + NIADDR) /* NADDR applies to (obsolete) S5FS */
#define IB(i) (NDADDR + (i)) /* index of i'th indirect block ptr */
#define SINGLE 0 /* single indirect block ptr */
#define DOUBLE 1 /* double indirect block ptr */
#define TRIPLE 2 /* triple indirect block ptr */
/*
* Acquire a write lock, and keep trying till we get it
*/
static int
allocsp_wlockfs(struct vnode *vp, struct lockfs *lf)
{
int err = 0;
lockagain:
do {
err = ufs_fiolfss(vp, lf);
if (err)
return (err);
} while (!LOCKFS_IS_ULOCK(lf));
lf->lf_lock = LOCKFS_WLOCK;
lf->lf_flags = 0;
lf->lf_comment = NULL;
err = ufs__fiolfs(vp, lf, 1, 0);
if (err == EBUSY || err == EINVAL)
goto lockagain;
return (err);
}
/*
* Release the write lock
*/
static int
allocsp_unlockfs(struct vnode *vp, struct lockfs *lf)
{
int err = 0;
lf->lf_lock = LOCKFS_ULOCK;
lf->lf_flags = 0;
err = ufs__fiolfs(vp, lf, 1, 0);
return (err);
}
struct allocsp_undo {
daddr_t offset;
daddr_t blk;
struct allocsp_undo *next;
};
/*
* ufs_allocsp() can be used to pre-allocate blocks for a file on a given
* file system. For direct blocks, the blocks are allocated from the offset
* requested to the block boundary, then any full blocks are allocated,
* and finally any remainder.
* For indirect blocks the blocks are not initialized and are
* only marked as allocated. These addresses are then stored as negative
* block numbers in the inode to imply special handling. UFS has been modified
* where necessary to understand this new notion.
* Successfully fallocated files will have IFALLOCATE cflag set in the inode.
*/
int
ufs_allocsp(struct vnode *vp, struct flock64 *lp, cred_t *cr)
{
struct lockfs lf;
int berr, err, resv, issync;
off_t istart, len; /* istart, special for idb */
struct inode *ip;
struct fs *fs;
struct ufsvfs *ufsvfsp;
u_offset_t resid, i, uoff;
daddr32_t db_undo[NDADDR]; /* old direct blocks */
struct allocsp_undo *ib_undo = NULL; /* ib undo */
struct allocsp_undo *undo = NULL;
u_offset_t osz; /* old file size */
int chunkblks = 0; /* # of blocks in 1 allocation */
int cnt = 0;
daddr_t allocblk;
daddr_t totblks = 0;
struct ulockfs *ulp;
size_t done_len;
int nbytes, offsetn;
ASSERT(vp->v_type == VREG);
ip = VTOI(vp);
fs = ip->i_fs;
if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
err = EIO;
goto out_allocsp;
}
istart = blkroundup(fs, (lp->l_start));
len = blkroundup(fs, (lp->l_len));
chunkblks = blkroundup(fs, ufsvfsp->vfs_iotransz) / fs->fs_bsize;
ulp = &ufsvfsp->vfs_ulockfs;
if (lp->l_start < 0 || lp->l_len <= 0)
return (EINVAL);
/* Quickly check to make sure we have space before we proceed */
if (lblkno(fs, len) > fs->fs_cstotal.cs_nbfree) {
if (TRANS_ISTRANS(ufsvfsp)) {
ufs_delete_drain_wait(ufsvfsp, 1);
if (lblkno(fs, len) > fs->fs_cstotal.cs_nbfree)
return (ENOSPC);
} else
return (ENOSPC);
}
/*
* We will keep i_rwlock locked as WRITER through out the function
* since we don't want anyone else reading or writing to the inode
* while we are in the middle of fallocating the file.
*/
rw_enter(&ip->i_rwlock, RW_WRITER);
/* Back up the direct block list, used for undo later if necessary */
rw_enter(&ip->i_contents, RW_READER);
for (i = 0; i < NDADDR; i++)
db_undo[i] = ip->i_db[i];
osz = ip->i_size;
rw_exit(&ip->i_contents);
/* Write lock the file system */
if (err = allocsp_wlockfs(vp, &lf))
goto exit;
/*
* Allocate any direct blocks now.
* Blocks are allocated from the offset requested to the block
* boundary, then any full blocks are allocated, and finally any
* remainder.
*/
if (lblkno(fs, lp->l_start) < NDADDR) {
ufs_trans_trunc_resv(ip, ip->i_size + (NDADDR * fs->fs_bsize),
&resv, &resid);
TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
rw_enter(&ip->i_contents, RW_WRITER);
done_len = 0;
while ((done_len < lp->l_len) &&
(lblkno(fs, lp->l_start + done_len) < NDADDR)) {
uoff = (offset_t)(lp->l_start + done_len);
offsetn = (int)blkoff(fs, uoff);
nbytes = (int)MIN(fs->fs_bsize - offsetn,
lp->l_len - done_len);
berr = bmap_write(ip, uoff, offsetn + nbytes,
BI_FALLOCATE, &allocblk, cr);
/* Yikes error, quit */
if (berr) {
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
TRANS_END_CSYNC(ufsvfsp, err, issync,
TOP_ALLOCSP, resv);
err = allocsp_unlockfs(vp, &lf);
goto exit;
}
if (allocblk) {
totblks++;
if ((uoff + nbytes) > ip->i_size)
ip->i_size = (uoff + nbytes);
}
done_len += nbytes;
}
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv);
/* start offset for indirect allocation */
istart = (uoff + nbytes);
}
/* Break the transactions into vfs_iotransz units */
ufs_trans_trunc_resv(ip, ip->i_size +
blkroundup(fs, ufsvfsp->vfs_iotransz), &resv, &resid);
TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
rw_enter(&ip->i_contents, RW_WRITER);
/* Now go about fallocating necessary indirect blocks */
for (i = istart; i < (lp->l_start + lp->l_len); i += fs->fs_bsize) {
berr = bmap_write(ip, i, fs->fs_bsize, BI_FALLOCATE,
&allocblk, cr);
if (berr) {
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
TRANS_END_CSYNC(ufsvfsp, err, issync,
TOP_ALLOCSP, resv);
err = allocsp_unlockfs(vp, &lf);
goto exit;
}
/* Update the blk counter only if new block was added */
if (allocblk) {
/* Save undo information */
undo = kmem_alloc(sizeof (struct allocsp_undo),
KM_SLEEP);
undo->offset = i;
undo->blk = allocblk;
undo->next = ib_undo;
ib_undo = undo;
totblks++;
if (i >= ip->i_size)
ip->i_size += fs->fs_bsize;
}
cnt++;
/* Being a good UFS citizen, let others get a share */
if (cnt == chunkblks) {
/*
* If there are waiters or the fs is hard locked,
* error locked, or read-only error locked,
* quit with EIO
*/
if (ULOCKFS_IS_HLOCK(ulp) || ULOCKFS_IS_ELOCK(ulp) ||
ULOCKFS_IS_ROELOCK(ulp)) {
ip->i_cflags |= IFALLOCATE;
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
TRANS_END_CSYNC(ufsvfsp, err, issync,
TOP_ALLOCSP, resv);
rw_exit(&ip->i_rwlock);
(void) allocsp_unlockfs(vp, &lf);
return (EIO);
}
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
/* End the current transaction */
TRANS_END_CSYNC(ufsvfsp, err, issync,
TOP_ALLOCSP, resv);
if (CV_HAS_WAITERS(&ulp->ul_cv)) {
/* Release the write lock */
if (err = allocsp_unlockfs(vp, &lf))
goto exit;
/* Wake up others waiting to do operations */
mutex_enter(&ulp->ul_lock);
cv_broadcast(&ulp->ul_cv);
mutex_exit(&ulp->ul_lock);
/* Grab the write lock again */
if (err = allocsp_wlockfs(vp, &lf))
goto exit;
} /* end of CV_HAS_WAITERS(&ulp->ul_cv) */
/* Reserve more space in log for this file */
ufs_trans_trunc_resv(ip,
ip->i_size + blkroundup(fs, ufsvfsp->vfs_iotransz),
&resv, &resid);
TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
rw_enter(&ip->i_contents, RW_WRITER);
cnt = 0; /* reset cnt b/c of new transaction */
}
}
if (!err && !berr)
ip->i_cflags |= IFALLOCATE;
/* If the file has grown then correct the file size */
if (osz < (lp->l_start + lp->l_len))
ip->i_size = (lp->l_start + lp->l_len);
/* Release locks, end log transaction and unlock fs */
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv);
err = allocsp_unlockfs(vp, &lf);
/*
* @ exit label, we should no longer be holding the fs write lock, and
* all logging transactions should have been ended. We still hold
* ip->i_rwlock.
*/
exit:
/*
* File has grown larger than 2GB. Set flag
* in superblock to indicate this, if it
* is not already set.
*/
if ((ip->i_size > MAXOFF32_T) &&
!(fs->fs_flags & FSLARGEFILES)) {
ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
mutex_enter(&ufsvfsp->vfs_lock);
fs->fs_flags |= FSLARGEFILES;
ufs_sbwrite(ufsvfsp);
mutex_exit(&ufsvfsp->vfs_lock);
}
/*
* Since we couldn't allocate completely, we will undo the allocations.
*/
if (berr) {
ufs_trans_trunc_resv(ip, totblks * fs->fs_bsize, &resv, &resid);
TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_ALLOCSP, resv);
rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
rw_enter(&ip->i_contents, RW_WRITER);
/* Direct blocks */
for (i = 0; i < NDADDR; i++) {
/*
* Only free the block if they are not same, and
* the old one isn't zero (the fragment was
* re-allocated).
*/
if (db_undo[i] != ip->i_db[i] && db_undo[i] == 0) {
free(ip, ip->i_db[i], fs->fs_bsize, 0);
ip->i_db[i] = 0;
}
}
/* Undo the indirect blocks */
while (ib_undo != NULL) {
undo = ib_undo;
err = bmap_set_bn(vp, undo->offset, 0);
if (err)
cmn_err(CE_PANIC, "ufs_allocsp(): failed to "
"undo allocation of block %ld",
undo->offset);
free(ip, undo->blk, fs->fs_bsize, I_IBLK);
ib_undo = undo->next;
kmem_free(undo, sizeof (struct allocsp_undo));
}
ip->i_size = osz;
TRANS_INODE(ufsvfsp, ip);
rw_exit(&ip->i_contents);
rw_exit(&ufsvfsp->vfs_dqrwlock);
TRANS_END_CSYNC(ufsvfsp, err, issync, TOP_ALLOCSP, resv);
rw_exit(&ip->i_rwlock);
return (berr);
}
/*
* Don't forget to free the undo chain :)
*/
while (ib_undo != NULL) {
undo = ib_undo;
ib_undo = undo->next;
kmem_free(undo, sizeof (struct allocsp_undo));
}
rw_exit(&ip->i_rwlock);
out_allocsp:
return (err);
}
/*
* Free storage space associated with the specified inode. The portion
* to be freed is specified by lp->l_start and lp->l_len (already
* normalized to a "whence" of 0).
*
* This is an experimental facility whose continued existence is not
* guaranteed. Currently, we only support the special case
* of l_len == 0, meaning free to end of file.
*
* Blocks are freed in reverse order. This FILO algorithm will tend to
* maintain a contiguous free list much longer than FIFO.
* See also ufs_itrunc() in ufs_inode.c.
*
* Bug: unused bytes in the last retained block are not cleared.
* This may result in a "hole" in the file that does not read as zeroes.
*/
/* ARGSUSED */
int
ufs_freesp(struct vnode *vp, struct flock64 *lp, int flag, cred_t *cr)
{
int i;
struct inode *ip = VTOI(vp);
int error;
ASSERT(vp->v_type == VREG);
ASSERT(lp->l_start >= 0); /* checked by convoff */
if (lp->l_len != 0)
return (EINVAL);
rw_enter(&ip->i_contents, RW_READER);
if (ip->i_size == (u_offset_t)lp->l_start) {
rw_exit(&ip->i_contents);
return (0);
}
/*
* Check if there is any active mandatory lock on the
* range that will be truncated/expanded.
*/
if (MANDLOCK(vp, ip->i_mode)) {
offset_t save_start;
save_start = lp->l_start;
if (ip->i_size < lp->l_start) {
/*
* "Truncate up" case: need to make sure there
* is no lock beyond current end-of-file. To
* do so, we need to set l_start to the size
* of the file temporarily.
*/
lp->l_start = ip->i_size;
}
lp->l_type = F_WRLCK;
lp->l_sysid = 0;
lp->l_pid = ttoproc(curthread)->p_pid;
i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK;
rw_exit(&ip->i_contents);
if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 ||
lp->l_type != F_UNLCK) {
return (i ? i : EAGAIN);
}
rw_enter(&ip->i_contents, RW_READER);
lp->l_start = save_start;
}
/*
* Make sure a write isn't in progress (allocating blocks)
* by acquiring i_rwlock (we promised ufs_bmap we wouldn't
* truncate while it was allocating blocks).
* Grab the locks in the right order.
*/
rw_exit(&ip->i_contents);
rw_enter(&ip->i_rwlock, RW_WRITER);
error = TRANS_ITRUNC(ip, (u_offset_t)lp->l_start, 0, cr);
rw_exit(&ip->i_rwlock);
return (error);
}
/*
* Find a cg with as close to nb contiguous bytes as possible
* THIS MAY TAKE MANY DISK READS!
*
* Implemented in an attempt to allocate contiguous blocks for
* writing the ufs log file to, minimizing future disk head seeking
*/
daddr_t
contigpref(ufsvfs_t *ufsvfsp, size_t nb, size_t minb)
{
struct fs *fs = ufsvfsp->vfs_fs;
daddr_t nblk = lblkno(fs, blkroundup(fs, nb));
daddr_t minblk = lblkno(fs, blkroundup(fs, minb));
daddr_t savebno, curbno, cgbno;
int cg, cgblks, savecg, savenblk, curnblk, startcg;
uchar_t *blksfree;
buf_t *bp;
struct cg *cgp;
savenblk = 0;
savecg = 0;
savebno = 0;
if ((startcg = findlogstartcg(fs, nblk, minblk)) == -1)
cg = 0; /* Nothing suitable found */
else
cg = startcg;
for (; cg < fs->fs_ncg; ++cg) {
/*
* find the largest contiguous range in this cg
*/
bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev,
(daddr_t)fsbtodb(fs, cgtod(fs, cg)),
(int)fs->fs_cgsize);
cgp = bp->b_un.b_cg;
if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
brelse(bp);
continue;
}
blksfree = cg_blksfree(cgp); /* free array */
cgblks = fragstoblks(fs, fs->fs_fpg); /* blks in free array */
cgbno = 0;
while (cgbno < cgblks && savenblk < nblk) {
/* find a free block */
for (; cgbno < cgblks; ++cgbno) {
if (isblock(fs, blksfree, cgbno)) {
if (startcg != -1) {
brelse(bp);
savecg = startcg;
savebno = cgbno;
goto done;
} else
break;
}
}
curbno = cgbno;
/* count the number of free blocks */
for (curnblk = 0; cgbno < cgblks; ++cgbno) {
if (!isblock(fs, blksfree, cgbno))
break;
if (++curnblk >= nblk)
break;
}
if (curnblk > savenblk) {
savecg = cg;
savenblk = curnblk;
savebno = curbno;
}
}
brelse(bp);
if (savenblk >= nblk)
break;
}
done:
/* convert block offset in cg to frag offset in cg */
savebno = blkstofrags(fs, savebno);
/* convert frag offset in cg to frag offset in fs */
savebno += (savecg * fs->fs_fpg);
return (savebno);
}
/*
* The object of this routine is to find a start point for the UFS log.
* Ideally the space should be allocated from the smallest possible number
* of contiguous cylinder groups. This is found by using a sliding window
* technique. The smallest window of contiguous cylinder groups, which is
* still able to accommodate the target, is found by moving the window
* through the cylinder groups in a single pass. The end of the window is
* advanced until the space is accommodated, then the start is advanced until
* it no longer fits, the end is then advanced again and so on until the
* final cylinder group is reached. The first suitable instance is recorded
* and its starting cg number is returned.
*
* If we are not able to find a minimum amount of space, represented by
* minblk, or to do so uses more than the available extents, then return -1.
*/
int
findlogstartcg(struct fs *fs, daddr_t requested, daddr_t minblk)
{
int ncgs; /* number of cylinder groups */
daddr_t target; /* amount of space sought */
int cwidth, ctotal; /* current window width and total */
int bwidth, btotal; /* best window width and total so far */
int s; /* index of the first element in the current window */
int e; /* index of the first element + the width */
/* (i.e. 1 + index of last element) */
int bs; /* index of the first element in the best window so far */
int header, max_extents;
target = requested;
ncgs = fs->fs_ncg;
header = sizeof (extent_block_t) - sizeof (extent_t);
max_extents = ((fs->fs_bsize)-header) / sizeof (extent_t);
cwidth = ctotal = 0;
btotal = -1;
bwidth = ncgs;
s = e = 0;
while (e < ncgs) {
/* Advance the end of the window until it accommodates the target. */
while (ctotal < target && e < ncgs) {
ctotal += fs->fs_cs(fs, e).cs_nbfree;
e++;
}
/*
* Advance the start of the window until it no longer
* accommodates the target.
*/
while (ctotal >= target && s < e) {
/* See if this is the smallest window so far. */
cwidth = e - s;
if (cwidth <= bwidth) {
if (cwidth == bwidth && ctotal <= btotal)
goto more;
bwidth = cwidth;
btotal = ctotal;
bs = s;
}
more:
ctotal -= fs->fs_cs(fs, s).cs_nbfree;
s++;
}
}
/*
* If we cannot allocate the minimum required or we use too many
* extents to do so, return -1.
*/
if (btotal < minblk || bwidth > max_extents)
bs = -1;
return (bs);
}
|