aboutsummaryrefslogtreecommitdiffstats
path: root/fs/logfs/readwrite.c
blob: f1cb512c5019dacf057391ed857a9697f0b98422 (plain)
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
/*
 * fs/logfs/readwrite.c
 *
 * As should be obvious for Linux kernel code, license is GPLv2
 *
 * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
 *
 *
 * Actually contains five sets of very similar functions:
 * read		read blocks from a file
 * seek_hole	find next hole
 * seek_data	find next data block
 * valid	check whether a block still belongs to a file
 * write	write blocks to a file
 * delete	delete a block (for directories and ifile)
 * rewrite	move existing blocks of a file to a new location (gc helper)
 * truncate	truncate a file
 */
#include "logfs.h"
#include <linux/sched.h>
#include <linux/slab.h>

static u64 adjust_bix(u64 bix, level_t level)
{
	switch (level) {
	case 0:
		return bix;
	case LEVEL(1):
		return max_t(u64, bix, I0_BLOCKS);
	case LEVEL(2):
		return max_t(u64, bix, I1_BLOCKS);
	case LEVEL(3):
		return max_t(u64, bix, I2_BLOCKS);
	case LEVEL(4):
		return max_t(u64, bix, I3_BLOCKS);
	case LEVEL(5):
		return max_t(u64, bix, I4_BLOCKS);
	default:
		WARN_ON(1);
		return bix;
	}
}

static inline u64 maxbix(u8 height)
{
	return 1ULL << (LOGFS_BLOCK_BITS * height);
}

/**
 * The inode address space is cut in two halves.  Lower half belongs to data
 * pages, upper half to indirect blocks.  If the high bit (INDIRECT_BIT) is
 * set, the actual block index (bix) and level can be derived from the page
 * index.
 *
 * The lowest three bits of the block index are set to 0 after packing and
 * unpacking.  Since the lowest n bits (9 for 4KiB blocksize) are ignored
 * anyway this is harmless.
 */
#define ARCH_SHIFT	(BITS_PER_LONG - 32)
#define INDIRECT_BIT	(0x80000000UL << ARCH_SHIFT)
#define LEVEL_SHIFT	(28 + ARCH_SHIFT)
static inline pgoff_t first_indirect_block(void)
{
	return INDIRECT_BIT | (1ULL << LEVEL_SHIFT);
}

pgoff_t logfs_pack_index(u64 bix, level_t level)
{
	pgoff_t index;

	BUG_ON(bix >= INDIRECT_BIT);
	if (level == 0)
		return bix;

	index  = INDIRECT_BIT;
	index |= (__force long)level << LEVEL_SHIFT;
	index |= bix >> ((__force u8)level * LOGFS_BLOCK_BITS);
	return index;
}

void logfs_unpack_index(pgoff_t index, u64 *bix, level_t *level)
{
	u8 __level;

	if (!(index & INDIRECT_BIT)) {
		*bix = index;
		*level = 0;
		return;
	}

	__level = (index & ~INDIRECT_BIT) >> LEVEL_SHIFT;
	*level = LEVEL(__level);
	*bix = (index << (__level * LOGFS_BLOCK_BITS)) & ~INDIRECT_BIT;
	*bix = adjust_bix(*bix, *level);
	return;
}
#undef ARCH_SHIFT
#undef INDIRECT_BIT
#undef LEVEL_SHIFT

/*
 * Time is stored as nanoseconds since the epoch.
 */
static struct timespec be64_to_timespec(__be64 betime)
{
	return ns_to_timespec(be64_to_cpu(betime));
}

static __be64 timespec_to_be64(struct timespec tsp)
{
	return cpu_to_be64((u64)tsp.tv_sec * NSEC_PER_SEC + tsp.tv_nsec);
}

static void logfs_disk_to_inode(struct logfs_disk_inode *di, struct inode*inode)
{
	struct logfs_inode *li = logfs_inode(inode);
	int i;

	inode->i_mode	= be16_to_cpu(di->di_mode);
	li->li_height	= di->di_height;
	li->li_flags	= be32_to_cpu(di->di_flags);
	inode->i_uid	= be32_to_cpu(di->di_uid);
	inode->i_gid	= be32_to_cpu(di->di_gid);
	inode->i_size	= be64_to_cpu(di->di_size);
	logfs_set_blocks(inode, be64_to_cpu(di->di_used_bytes));
	inode->i_atime	= be64_to_timespec(di->di_atime);
	inode->i_ctime	= be64_to_timespec(di->di_ctime);
	inode->i_mtime	= be64_to_timespec(di->di_mtime);
	set_nlink(inode, be32_to_cpu(di->di_refcount));
	inode->i_generation = be32_to_cpu(di->di_generation);

	switch (inode->i_mode & S_IFMT) {
	case S_IFSOCK:	/* fall through */
	case S_IFBLK:	/* fall through */
	case S_IFCHR:	/* fall through */
	case S_IFIFO:
		inode->i_rdev = be64_to_cpu(di->di_data[0]);
		break;
	case S_IFDIR:	/* fall through */
	case S_IFREG:	/* fall through */
	case S_IFLNK:
		for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
			li->li_data[i] = be64_to_cpu(di->di_data[i]);
		break;
	default:
		BUG();
	}
}

static void logfs_inode_to_disk(struct inode *inode, struct logfs_disk_inode*di)
{
	struct logfs_inode *li = logfs_inode(inode);
	int i;

	di->di_mode	= cpu_to_be16(inode->i_mode);
	di->di_height	= li->li_height;
	di->di_pad	= 0;
	di->di_flags	= cpu_to_be32(li->li_flags);
	di->di_uid	= cpu_to_be32(inode->i_uid);
	di->di_gid	= cpu_to_be32(inode->i_gid);
	di->di_size	= cpu_to_be64(i_size_read(inode));
	di->di_used_bytes = cpu_to_be64(li->li_used_bytes);
	di->di_atime	= timespec_to_be64(inode->i_atime);
	di->di_ctime	= timespec_to_be64(inode->i_ctime);
	di->di_mtime	= timespec_to_be64(inode->i_mtime);
	di->di_refcount	= cpu_to_be32(inode->i_nlink);
	di->di_generation = cpu_to_be32(inode->i_generation);

	switch (inode->i_mode & S_IFMT) {
	case S_IFSOCK:	/* fall through */
	case S_IFBLK:	/* fall through */
	case S_IFCHR:	/* fall through */
	case S_IFIFO:
		di->di_data[0] = cpu_to_be64(inode->i_rdev);
		break;
	case S_IFDIR:	/* fall through */
	case S_IFREG:	/* fall through */
	case S_IFLNK:
		for (i = 0; i < LOGFS_EMBEDDED_FIELDS; i++)
			di->di_data[i] = cpu_to_be64(li->li_data[i]);
		break;
	default:
		BUG();
	}
}

static void __logfs_set_blocks(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;
	struct logfs_inode *li = logfs_inode(inode);

	inode->i_blocks = ULONG_MAX;
	if (li->li_used_bytes >> sb->s_blocksize_bits < ULONG_MAX)
		inode->i_blocks = ALIGN(li->li_used_bytes, 512) >> 9;
}

void logfs_set_blocks(struct inode *inode, u64 bytes)
{
	struct logfs_inode *li = logfs_inode(inode);

	li->li_used_bytes = bytes;
	__logfs_set_blocks(inode);
}

static void prelock_page(struct super_block *sb, struct page *page, int lock)
{
	struct logfs_super *super = logfs_super(sb);

	BUG_ON(!PageLocked(page));
	if (lock) {
		BUG_ON(PagePreLocked(page));
		SetPagePreLocked(page);
	} else {
		/* We are in GC path. */
		if (PagePreLocked(page))
			super->s_lock_count++;
		else
			SetPagePreLocked(page);
	}
}

static void preunlock_page(struct super_block *sb, struct page *page, int lock)
{
	struct logfs_super *super = logfs_super(sb);

	BUG_ON(!PageLocked(page));
	if (lock)
		ClearPagePreLocked(page);
	else {
		/* We are in GC path. */
		BUG_ON(!PagePreLocked(page));
		if (super->s_lock_count)
			super->s_lock_count--;
		else
			ClearPagePreLocked(page);
	}
}

/*
 * Logfs is prone to an AB-BA deadlock where one task tries to acquire
 * s_write_mutex with a locked page and GC tries to get that page while holding
 * s_write_mutex.
 * To solve this issue logfs will ignore the page lock iff the page in question
 * is waiting for s_write_mutex.  We annotate this fact by setting PG_pre_locked
 * in addition to PG_locked.
 */
void logfs_get_wblocks(struct super_block *sb, struct page *page, int lock)
{
	struct logfs_super *super = logfs_super(sb);

	if (page)
		prelock_page(sb, page, lock);

	if (lock) {
		mutex_lock(&super->s_write_mutex);
		logfs_gc_pass(sb);
		/* FIXME: We also have to check for shadowed space
		 * and mempool fill grade */
	}
}

void logfs_put_wblocks(struct super_block *sb, struct page *page, int lock)
{
	struct logfs_super *super = logfs_super(sb);

	if (page)
		preunlock_page(sb, page, lock);
	/* Order matters - we must clear PG_pre_locked before releasing
	 * s_write_mutex or we could race against another task. */
	if (lock)
		mutex_unlock(&super->s_write_mutex);
}

static struct page *logfs_get_read_page(struct inode *inode, u64 bix,
		level_t level)
{
	return find_or_create_page(inode->i_mapping,
			logfs_pack_index(bix, level), GFP_NOFS);
}

static void logfs_put_read_page(struct page *page)
{
	unlock_page(page);
	page_cache_release(page);
}

static void logfs_lock_write_page(struct page *page)
{
	int loop = 0;

	while (unlikely(!trylock_page(page))) {
		if (loop++ > 0x1000) {
			/* Has been observed once so far... */
			printk(KERN_ERR "stack at %p\n", &loop);
			BUG();
		}
		if (PagePreLocked(page)) {
			/* Holder of page lock is waiting for us, it
			 * is safe to use this page. */
			break;
		}
		/* Some other process has this page locked and has
		 * nothing to do with us.  Wait for it to finish.
		 */
		schedule();
	}
	BUG_ON(!PageLocked(page));
}

static struct page *logfs_get_write_page(struct inode *inode, u64 bix,
		level_t level)
{
	struct address_space *mapping = inode->i_mapping;
	pgoff_t index = logfs_pack_index(bix, level);
	struct page *page;
	int err;

repeat:
	page = find_get_page(mapping, index);
	if (!page) {
		page = __page_cache_alloc(GFP_NOFS);
		if (!page)
			return NULL;
		err = add_to_page_cache_lru(page, mapping, index, GFP_NOFS);
		if (unlikely(err)) {
			page_cache_release(page);
			if (err == -EEXIST)
				goto repeat;
			return NULL;
		}
	} else logfs_lock_write_page(page);
	BUG_ON(!PageLocked(page));
	return page;
}

static void logfs_unlock_write_page(struct page *page)
{
	if (!PagePreLocked(page))
		unlock_page(page);
}

static void logfs_put_write_page(struct page *page)
{
	logfs_unlock_write_page(page);
	page_cache_release(page);
}

static struct page *logfs_get_page(struct inode *inode, u64 bix, level_t level,
		int rw)
{
	if (rw == READ)
		return logfs_get_read_page(inode, bix, level);
	else
		return logfs_get_write_page(inode, bix, level);
}

static void logfs_put_page(struct page *page, int rw)
{
	if (rw == READ)
		logfs_put_read_page(page);
	else
		logfs_put_write_page(page);
}

static unsigned long __get_bits(u64 val, int skip, int no)
{
	u64 ret = val;

	ret >>= skip * no;
	ret <<= 64 - no;
	ret >>= 64 - no;
	return ret;
}

static unsigned long get_bits(u64 val, level_t skip)
{
	return __get_bits(val, (__force int)skip, LOGFS_BLOCK_BITS);
}

static inline void init_shadow_tree(struct super_block *sb,
		struct shadow_tree *tree)
{
	struct logfs_super *super = logfs_super(sb);

	btree_init_mempool64(&tree->new, super->s_btree_pool);
	btree_init_mempool64(&tree->old, super->s_btree_pool);
}

static void indirect_write_block(struct logfs_block *block)
{
	struct page *page;
	struct inode *inode;
	int ret;

	page = block->page;
	inode = page->mapping->host;
	logfs_lock_write_page(page);
	ret = logfs_write_buf(inode, page, 0);
	logfs_unlock_write_page(page);
	/*
	 * This needs some rework.  Unless you want your filesystem to run
	 * completely synchronously (you don't), the filesystem will always
	 * report writes as 'successful' before the actual work has been
	 * done.  The actual work gets done here and this is where any errors
	 * will show up.  And there isn't much we can do about it, really.
	 *
	 * Some attempts to fix the errors (move from bad blocks, retry io,...)
	 * have already been done, so anything left should be either a broken
	 * device or a bug somewhere in logfs itself.  Being relatively new,
	 * the odds currently favor a bug, so for now the line below isn't
	 * entirely tasteles.
	 */
	BUG_ON(ret);
}

static void inode_write_block(struct logfs_block *block)
{
	struct inode *inode;
	int ret;

	inode = block->inode;
	if (inode->i_ino == LOGFS_INO_MASTER)
		logfs_write_anchor(inode->i_sb);
	else {
		ret = __logfs_write_inode(inode, NULL, 0);
		/* see indirect_write_block comment */
		BUG_ON(ret);
	}
}

/*
 * This silences a false, yet annoying gcc warning.  I hate it when my editor
 * jumps into bitops.h each time I recompile this file.
 * TODO: Complain to gcc folks about this and upgrade compiler.
 */
static unsigned long fnb(const unsigned long *addr,
		unsigned long size, unsigned long offset)
{
	return find_next_bit(addr, size, offset);
}

static __be64 inode_val0(struct inode *inode)
{
	struct logfs_inode *li = logfs_inode(inode);
	u64 val;

	/*
	 * Explicit shifting generates good code, but must match the format
	 * of the structure.  Add some paranoia just in case.
	 */
	BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_mode) != 0);
	BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_height) != 2);
	BUILD_BUG_ON(offsetof(struct logfs_disk_inode, di_flags) != 4);

	val =	(u64)inode->i_mode << 48 |
		(u64)li->li_height << 40 |
		(u64)li->li_flags;
	return cpu_to_be64(val);
}

static int inode_write_alias(struct super_block *sb,
		struct logfs_block *block, write_alias_t *write_one_alias)
{
	struct inode *inode = block->inode;
	struct logfs_inode *li = logfs_inode(inode);
	unsigned long pos;
	u64 ino , bix;
	__be64 val;
	level_t level;
	int err;

	for (pos = 0; ; pos++) {
		pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
		if (pos >= LOGFS_EMBEDDED_FIELDS + INODE_POINTER_OFS)
			return 0;

		switch (pos) {
		case INODE_HEIGHT_OFS:
			val = inode_val0(inode);
			break;
		case INODE_USED_OFS:
			val = cpu_to_be64(li->li_used_bytes);
			break;
		case INODE_SIZE_OFS:
			val = cpu_to_be64(i_size_read(inode));
			break;
		case INODE_POINTER_OFS ... INODE_POINTER_OFS + LOGFS_EMBEDDED_FIELDS - 1:
			val = cpu_to_be64(li->li_data[pos - INODE_POINTER_OFS]);
			break;
		default:
			BUG();
		}

		ino = LOGFS_INO_MASTER;
		bix = inode->i_ino;
		level = LEVEL(0);
		err = write_one_alias(sb, ino, bix, level, pos, val);
		if (err)
			return err;
	}
}

static int indirect_write_alias(struct super_block *sb,
		struct logfs_block *block, write_alias_t *write_one_alias)
{
	unsigned long pos;
	struct page *page = block->page;
	u64 ino , bix;
	__be64 *child, val;
	level_t level;
	int err;

	for (pos = 0; ; pos++) {
		pos = fnb(block->alias_map, LOGFS_BLOCK_FACTOR, pos);
		if (pos >= LOGFS_BLOCK_FACTOR)
			return 0;

		ino = page->mapping->host->i_ino;
		logfs_unpack_index(page->index, &bix, &level);
		child = kmap_atomic(page);
		val = child[pos];
		kunmap_atomic(child);
		err = write_one_alias(sb, ino, bix, level, pos, val);
		if (err)
			return err;
	}
}

int logfs_write_obj_aliases_pagecache(struct super_block *sb)
{
	struct logfs_super *super = logfs_super(sb);
	struct logfs_block *block;
	int err;

	list_for_each_entry(block, &super->s_object_alias, alias_list) {
		err = block->ops->write_alias(sb, block, write_alias_journal);
		if (err)
			return err;
	}
	return 0;
}

void __free_block(struct super_block *sb, struct logfs_block *block)
{
	BUG_ON(!list_empty(&block->item_list));
	list_del(&block->alias_list);
	mempool_free(block, logfs_super(sb)->s_block_pool);
}

static void inode_free_block(struct super_block *sb, struct logfs_block *block)
{
	struct inode *inode = block->inode;

	logfs_inode(inode)->li_block = NULL;
	__free_block(sb, block);
}

static void indirect_free_block(struct super_block *sb,
		struct logfs_block *block)
{
	struct page *page = block->page;

	if (PagePrivate(page)) {
		ClearPagePrivate(page);
		page_cache_release(page);
		set_page_private(page, 0);
	}
	__free_block(sb, block);
}


static struct logfs_block_ops inode_block_ops = {
	.write_block = inode_write_block,
	.free_block = inode_free_block,
	.write_alias = inode_write_alias,
};

struct logfs_block_ops indirect_block_ops = {
	.write_block = indirect_write_block,
	.free_block = indirect_free_block,
	.write_alias = indirect_write_alias,
};

struct logfs_block *__alloc_block(struct super_block *sb,
		u64 ino, u64 bix, level_t level)
{
	struct logfs_super *super = logfs_super(sb);
	struct logfs_block *block;

	block = mempool_alloc(super->s_block_pool, GFP_NOFS);
	memset(block, 0, sizeof(*block));
	INIT_LIST_HEAD(&block->alias_list);
	INIT_LIST_HEAD(&block->item_list);
	block->sb = sb;
	block->ino = ino;
	block->bix = bix;
	block->level = level;
	return block;
}

static void alloc_inode_block(struct inode *inode)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct logfs_block *block;

	if (li->li_block)
		return;

	block = __alloc_block(inode->i_sb, LOGFS_INO_MASTER, inode->i_ino, 0);
	block->inode = inode;
	li->li_block = block;
	block->ops = &inode_block_ops;
}

void initialize_block_counters(struct page *page, struct logfs_block *block,
		__be64 *array, int page_is_empty)
{
	u64 ptr;
	int i, start;

	block->partial = 0;
	block->full = 0;
	start = 0;
	if (page->index < first_indirect_block()) {
		/* Counters are pointless on level 0 */
		return;
	}
	if (page->index == first_indirect_block()) {
		/* Skip unused pointers */
		start = I0_BLOCKS;
		block->full = I0_BLOCKS;
	}
	if (!page_is_empty) {
		for (i = start; i < LOGFS_BLOCK_FACTOR; i++) {
			ptr = be64_to_cpu(array[i]);
			if (ptr)
				block->partial++;
			if (ptr & LOGFS_FULLY_POPULATED)
				block->full++;
		}
	}
}

static void alloc_data_block(struct inode *inode, struct page *page)
{
	struct logfs_block *block;
	u64 bix;
	level_t level;

	if (PagePrivate(page))
		return;

	logfs_unpack_index(page->index, &bix, &level);
	block = __alloc_block(inode->i_sb, inode->i_ino, bix, level);
	block->page = page;

	SetPagePrivate(page);
	page_cache_get(page);
	set_page_private(page, (unsigned long) block);

	block->ops = &indirect_block_ops;
}

static void alloc_indirect_block(struct inode *inode, struct page *page,
		int page_is_empty)
{
	struct logfs_block *block;
	__be64 *array;

	if (PagePrivate(page))
		return;

	alloc_data_block(inode, page);

	block = logfs_block(page);
	array = kmap_atomic(page);
	initialize_block_counters(page, block, array, page_is_empty);
	kunmap_atomic(array);
}

static void block_set_pointer(struct page *page, int index, u64 ptr)
{
	struct logfs_block *block = logfs_block(page);
	__be64 *array;
	u64 oldptr;

	BUG_ON(!block);
	array = kmap_atomic(page);
	oldptr = be64_to_cpu(array[index]);
	array[index] = cpu_to_be64(ptr);
	kunmap_atomic(array);
	SetPageUptodate(page);

	block->full += !!(ptr & LOGFS_FULLY_POPULATED)
		- !!(oldptr & LOGFS_FULLY_POPULATED);
	block->partial += !!ptr - !!oldptr;
}

static u64 block_get_pointer(struct page *page, int index)
{
	__be64 *block;
	u64 ptr;

	block = kmap_atomic(page);
	ptr = be64_to_cpu(block[index]);
	kunmap_atomic(block);
	return ptr;
}

static int logfs_read_empty(struct page *page)
{
	zero_user_segment(page, 0, PAGE_CACHE_SIZE);
	return 0;
}

static int logfs_read_direct(struct inode *inode, struct page *page)
{
	struct logfs_inode *li = logfs_inode(inode);
	pgoff_t index = page->index;
	u64 block;

	block = li->li_data[index];
	if (!block)
		return logfs_read_empty(page);

	return logfs_segment_read(inode, page, block, index, 0);
}

static int logfs_read_loop(struct inode *inode, struct page *page,
		int rw_context)
{
	struct logfs_inode *li = logfs_inode(inode);
	u64 bix, bofs = li->li_data[INDIRECT_INDEX];
	level_t level, target_level;
	int ret;
	struct page *ipage;

	logfs_unpack_index(page->index, &bix, &target_level);
	if (!bofs)
		return logfs_read_empty(page);

	if (bix >= maxbix(li->li_height))
		return logfs_read_empty(page);

	for (level = LEVEL(li->li_height);
			(__force u8)level > (__force u8)target_level;
			level = SUBLEVEL(level)){
		ipage = logfs_get_page(inode, bix, level, rw_context);
		if (!ipage)
			return -ENOMEM;

		ret = logfs_segment_read(inode, ipage, bofs, bix, level);
		if (ret) {
			logfs_put_read_page(ipage);
			return ret;
		}

		bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
		logfs_put_page(ipage, rw_context);
		if (!bofs)
			return logfs_read_empty(page);
	}

	return logfs_segment_read(inode, page, bofs, bix, 0);
}

static int logfs_read_block(struct inode *inode, struct page *page,
		int rw_context)
{
	pgoff_t index = page->index;

	if (index < I0_BLOCKS)
		return logfs_read_direct(inode, page);
	return logfs_read_loop(inode, page, rw_context);
}

static int logfs_exist_loop(struct inode *inode, u64 bix)
{
	struct logfs_inode *li = logfs_inode(inode);
	u64 bofs = li->li_data[INDIRECT_INDEX];
	level_t level;
	int ret;
	struct page *ipage;

	if (!bofs)
		return 0;
	if (bix >= maxbix(li->li_height))
		return 0;

	for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
		ipage = logfs_get_read_page(inode, bix, level);
		if (!ipage)
			return -ENOMEM;

		ret = logfs_segment_read(inode, ipage, bofs, bix, level);
		if (ret) {
			logfs_put_read_page(ipage);
			return ret;
		}

		bofs = block_get_pointer(ipage, get_bits(bix, SUBLEVEL(level)));
		logfs_put_read_page(ipage);
		if (!bofs)
			return 0;
	}

	return 1;
}

int logfs_exist_block(struct inode *inode, u64 bix)
{
	struct logfs_inode *li = logfs_inode(inode);

	if (bix < I0_BLOCKS)
		return !!li->li_data[bix];
	return logfs_exist_loop(inode, bix);
}

static u64 seek_holedata_direct(struct inode *inode, u64 bix, int data)
{
	struct logfs_inode *li = logfs_inode(inode);

	for (; bix < I0_BLOCKS; bix++)
		if (data ^ (li->li_data[bix] == 0))
			return bix;
	return I0_BLOCKS;
}

static u64 seek_holedata_loop(struct inode *inode, u64 bix, int data)
{
	struct logfs_inode *li = logfs_inode(inode);
	__be64 *rblock;
	u64 increment, bofs = li->li_data[INDIRECT_INDEX];
	level_t level;
	int ret, slot;
	struct page *page;

	BUG_ON(!bofs);

	for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)) {
		increment = 1 << (LOGFS_BLOCK_BITS * ((__force u8)level-1));
		page = logfs_get_read_page(inode, bix, level);
		if (!page)
			return bix;

		ret = logfs_segment_read(inode, page, bofs, bix, level);
		if (ret) {
			logfs_put_read_page(page);
			return bix;
		}

		slot = get_bits(bix, SUBLEVEL(level));
		rblock = kmap_atomic(page);
		while (slot < LOGFS_BLOCK_FACTOR) {
			if (data && (rblock[slot] != 0))
				break;
			if (!data && !(be64_to_cpu(rblock[slot]) & LOGFS_FULLY_POPULATED))
				break;
			slot++;
			bix += increment;
			bix &= ~(increment - 1);
		}
		if (slot >= LOGFS_BLOCK_FACTOR) {
			kunmap_atomic(rblock);
			logfs_put_read_page(page);
			return bix;
		}
		bofs = be64_to_cpu(rblock[slot]);
		kunmap_atomic(rblock);
		logfs_put_read_page(page);
		if (!bofs) {
			BUG_ON(data);
			return bix;
		}
	}
	return bix;
}

/**
 * logfs_seek_hole - find next hole starting at a given block index
 * @inode:		inode to search in
 * @bix:		block index to start searching
 *
 * Returns next hole.  If the file doesn't contain any further holes, the
 * block address next to eof is returned instead.
 */
u64 logfs_seek_hole(struct inode *inode, u64 bix)
{
	struct logfs_inode *li = logfs_inode(inode);

	if (bix < I0_BLOCKS) {
		bix = seek_holedata_direct(inode, bix, 0);
		if (bix < I0_BLOCKS)
			return bix;
	}

	if (!li->li_data[INDIRECT_INDEX])
		return bix;
	else if (li->li_data[INDIRECT_INDEX] & LOGFS_FULLY_POPULATED)
		bix = maxbix(li->li_height);
	else if (bix >= maxbix(li->li_height))
		return bix;
	else {
		bix = seek_holedata_loop(inode, bix, 0);
		if (bix < maxbix(li->li_height))
			return bix;
		/* Should not happen anymore.  But if some port writes semi-
		 * corrupt images (as this one used to) we might run into it.
		 */
		WARN_ON_ONCE(bix == maxbix(li->li_height));
	}

	return bix;
}

static u64 __logfs_seek_data(struct inode *inode, u64 bix)
{
	struct logfs_inode *li = logfs_inode(inode);

	if (bix < I0_BLOCKS) {
		bix = seek_holedata_direct(inode, bix, 1);
		if (bix < I0_BLOCKS)
			return bix;
	}

	if (bix < maxbix(li->li_height)) {
		if (!li->li_data[INDIRECT_INDEX])
			bix = maxbix(li->li_height);
		else
			return seek_holedata_loop(inode, bix, 1);
	}

	return bix;
}

/**
 * logfs_seek_data - find next data block after a given block index
 * @inode:		inode to search in
 * @bix:		block index to start searching
 *
 * Returns next data block.  If the file doesn't contain any further data
 * blocks, the last block in the file is returned instead.
 */
u64 logfs_seek_data(struct inode *inode, u64 bix)
{
	struct super_block *sb = inode->i_sb;
	u64 ret, end;

	ret = __logfs_seek_data(inode, bix);
	end = i_size_read(inode) >> sb->s_blocksize_bits;
	if (ret >= end)
		ret = max(bix, end);
	return ret;
}

static int logfs_is_valid_direct(struct logfs_inode *li, u64 bix, u64 ofs)
{
	return pure_ofs(li->li_data[bix]) == ofs;
}

static int __logfs_is_valid_loop(struct inode *inode, u64 bix,
		u64 ofs, u64 bofs)
{
	struct logfs_inode *li = logfs_inode(inode);
	level_t level;
	int ret;
	struct page *page;

	for (level = LEVEL(li->li_height); level != 0; level = SUBLEVEL(level)){
		page = logfs_get_write_page(inode, bix, level);
		BUG_ON(!page);

		ret = logfs_segment_read(inode, page, bofs, bix, level);
		if (ret) {
			logfs_put_write_page(page);
			return 0;
		}

		bofs = block_get_pointer(page, get_bits(bix, SUBLEVEL(level)));
		logfs_put_write_page(page);
		if (!bofs)
			return 0;

		if (pure_ofs(bofs) == ofs)
			return 1;
	}
	return 0;
}

static int logfs_is_valid_loop(struct inode *inode, u64 bix, u64 ofs)
{
	struct logfs_inode *li = logfs_inode(inode);
	u64 bofs = li->li_data[INDIRECT_INDEX];

	if (!bofs)
		return 0;

	if (bix >= maxbix(li->li_height))
		return 0;

	if (pure_ofs(bofs) == ofs)
		return 1;

	return __logfs_is_valid_loop(inode, bix, ofs, bofs);
}

static int __logfs_is_valid_block(struct inode *inode, u64 bix, u64 ofs)
{
	struct logfs_inode *li = logfs_inode(inode);

	if ((inode->i_nlink == 0) && atomic_read(&inode->i_count) == 1)
		return 0;

	if (bix < I0_BLOCKS)
		return logfs_is_valid_direct(li, bix, ofs);
	return logfs_is_valid_loop(inode, bix, ofs);
}

/**
 * logfs_is_valid_block - check whether this block is still valid
 *
 * @sb	- superblock
 * @ofs	- block physical offset
 * @ino	- block inode number
 * @bix	- block index
 * @level - block level
 *
 * Returns 0 if the block is invalid, 1 if it is valid and 2 if it will
 * become invalid once the journal is written.
 */
int logfs_is_valid_block(struct super_block *sb, u64 ofs, u64 ino, u64 bix,
		gc_level_t gc_level)
{
	struct logfs_super *super = logfs_super(sb);
	struct inode *inode;
	int ret, cookie;

	/* Umount closes a segment with free blocks remaining.  Those
	 * blocks are by definition invalid. */
	if (ino == -1)
		return 0;

	LOGFS_BUG_ON((u64)(u_long)ino != ino, sb);

	inode = logfs_safe_iget(sb, ino, &cookie);
	if (IS_ERR(inode))
		goto invalid;

	ret = __logfs_is_valid_block(inode, bix, ofs);
	logfs_safe_iput(inode, cookie);
	if (ret)
		return ret;

invalid:
	/* Block is nominally invalid, but may still sit in the shadow tree,
	 * waiting for a journal commit.
	 */
	if (btree_lookup64(&super->s_shadow_tree.old, ofs))
		return 2;
	return 0;
}

int logfs_readpage_nolock(struct page *page)
{
	struct inode *inode = page->mapping->host;
	int ret = -EIO;

	ret = logfs_read_block(inode, page, READ);

	if (ret) {
		ClearPageUptodate(page);
		SetPageError(page);
	} else {
		SetPageUptodate(page);
		ClearPageError(page);
	}
	flush_dcache_page(page);

	return ret;
}

static int logfs_reserve_bytes(struct inode *inode, int bytes)
{
	struct logfs_super *super = logfs_super(inode->i_sb);
	u64 available = super->s_free_bytes + super->s_dirty_free_bytes
			- super->s_dirty_used_bytes - super->s_dirty_pages;

	if (!bytes)
		return 0;

	if (available < bytes)
		return -ENOSPC;

	if (available < bytes + super->s_root_reserve &&
			!capable(CAP_SYS_RESOURCE))
		return -ENOSPC;

	return 0;
}

int get_page_reserve(struct inode *inode, struct page *page)
{
	struct logfs_super *super = logfs_super(inode->i_sb);
	struct logfs_block *block = logfs_block(page);
	int ret;

	if (block && block->reserved_bytes)
		return 0;

	logfs_get_wblocks(inode->i_sb, page, WF_LOCK);
	while ((ret = logfs_reserve_bytes(inode, 6 * LOGFS_MAX_OBJECTSIZE)) &&
			!list_empty(&super->s_writeback_list)) {
		block = list_entry(super->s_writeback_list.next,
				struct logfs_block, alias_list);
		block->ops->write_block(block);
	}
	if (!ret) {
		alloc_data_block(inode, page);
		block = logfs_block(page);
		block->reserved_bytes += 6 * LOGFS_MAX_OBJECTSIZE;
		super->s_dirty_pages += 6 * LOGFS_MAX_OBJECTSIZE;
		list_move_tail(&block->alias_list, &super->s_writeback_list);
	}
	logfs_put_wblocks(inode->i_sb, page, WF_LOCK);
	return ret;
}

/*
 * We are protected by write lock.  Push victims up to superblock level
 * and release transaction when appropriate.
 */
/* FIXME: This is currently called from the wrong spots. */
static void logfs_handle_transaction(struct inode *inode,
		struct logfs_transaction *ta)
{
	struct logfs_super *super = logfs_super(inode->i_sb);

	if (!ta)
		return;
	logfs_inode(inode)->li_block->ta = NULL;

	if (inode->i_ino != LOGFS_INO_MASTER) {
		BUG(); /* FIXME: Yes, this needs more thought */
		/* just remember the transaction until inode is written */
		//BUG_ON(logfs_inode(inode)->li_transaction);
		//logfs_inode(inode)->li_transaction = ta;
		return;
	}

	switch (ta->state) {
	case CREATE_1: /* fall through */
	case UNLINK_1:
		BUG_ON(super->s_victim_ino);
		super->s_victim_ino = ta->ino;
		break;
	case CREATE_2: /* fall through */
	case UNLINK_2:
		BUG_ON(super->s_victim_ino != ta->ino);
		super->s_victim_ino = 0;
		/* transaction ends here - free it */
		kfree(ta);
		break;
	case CROSS_RENAME_1:
		BUG_ON(super->s_rename_dir);
		BUG_ON(super->s_rename_pos);
		super->s_rename_dir = ta->dir;
		super->s_rename_pos = ta->pos;
		break;
	case CROSS_RENAME_2:
		BUG_ON(super->s_rename_dir != ta->dir);
		BUG_ON(super->s_rename_pos != ta->pos);
		super->s_rename_dir = 0;
		super->s_rename_pos = 0;
		kfree(ta);
		break;
	case TARGET_RENAME_1:
		BUG_ON(super->s_rename_dir);
		BUG_ON(super->s_rename_pos);
		BUG_ON(super->s_victim_ino);
		super->s_rename_dir = ta->dir;
		super->s_rename_pos = ta->pos;
		super->s_victim_ino = ta->ino;
		break;
	case TARGET_RENAME_2:
		BUG_ON(super->s_rename_dir != ta->dir);
		BUG_ON(super->s_rename_pos != ta->pos);
		BUG_ON(super->s_victim_ino != ta->ino);
		super->s_rename_dir = 0;
		super->s_rename_pos = 0;
		break;
	case TARGET_RENAME_3:
		BUG_ON(super->s_rename_dir);
		BUG_ON(super->s_rename_pos);
		BUG_ON(super->s_victim_ino != ta->ino);
		super->s_victim_ino = 0;
		kfree(ta);
		break;
	default:
		BUG();
	}
}

/*
 * Not strictly a reservation, but rather a check that we still have enough
 * space to satisfy the write.
 */
static int logfs_reserve_blocks(struct inode *inode, int blocks)
{
	return logfs_reserve_bytes(inode, blocks * LOGFS_MAX_OBJECTSIZE);
}

struct write_control {
	u64 ofs;
	long flags;
};

static struct logfs_shadow *alloc_shadow(struct inode *inode, u64 bix,
		level_t level, u64 old_ofs)
{
	struct logfs_super *super = logfs_super(inode->i_sb);
	struct logfs_shadow *shadow;

	shadow = mempool_alloc(super->s_shadow_pool, GFP_NOFS);
	memset(shadow, 0, sizeof(*shadow));
	shadow->ino = inode->i_ino;
	shadow->bix = bix;
	shadow->gc_level = expand_level(inode->i_ino, level);
	shadow->old_ofs = old_ofs & ~LOGFS_FULLY_POPULATED;
	return shadow;
}

static void free_shadow(struct inode *inode, struct logfs_shadow *shadow)
{
	struct logfs_super *super = logfs_super(inode->i_sb);

	mempool_free(shadow, super->s_shadow_pool);
}

static void mark_segment(struct shadow_tree *tree, u32 segno)
{
	int err;

	if (!btree_lookup32(&tree->segment_map, segno)) {
		err = btree_insert32(&tree->segment_map, segno, (void *)1,
				GFP_NOFS);
		BUG_ON(err);
		tree->no_shadowed_segments++;
	}
}

/**
 * fill_shadow_tree - Propagate shadow tree changes due to a write
 * @inode:	Inode owning the page
 * @page:	Struct page that was written
 * @shadow:	Shadow for the current write
 *
 * Writes in logfs can result in two semi-valid objects.  The old object
 * is still valid as long as it can be reached by following pointers on
 * the medium.  Only when writes propagate all the way up to the journal
 * has the new object safely replaced the old one.
 *
 * To handle this problem, a struct logfs_shadow is used to represent
 * every single write.  It is attached to the indirect block, which is
 * marked dirty.  When the indirect block is written, its shadows are
 * handed up to the next indirect block (or inode).  Untimately they
 * will reach the master inode and be freed upon journal commit.
 *
 * This function handles a single step in the propagation.  It adds the
 * shadow for the current write to the tree, along with any shadows in
 * the page's tree, in case it was an indirect block.  If a page is
 * written, the inode parameter is left NULL, if an inode is written,
 * the page parameter is left NULL.
 */
static void fill_shadow_tree(struct inode *inode, struct page *page,
		struct logfs_shadow *shadow)
{
	struct logfs_super *super = logfs_super(inode->i_sb);
	struct logfs_block *block = logfs_block(page);
	struct shadow_tree *tree = &super->s_shadow_tree;

	if (PagePrivate(page)) {
		if (block->alias_map)
			super->s_no_object_aliases -= bitmap_weight(
					block->alias_map, LOGFS_BLOCK_FACTOR);
		logfs_handle_transaction(inode, block->ta);
		block->ops->free_block(inode->i_sb, block);
	}
	if (shadow) {
		if (shadow->old_ofs)
			btree_insert64(&tree->old, shadow->old_ofs, shadow,
					GFP_NOFS);
		else
			btree_insert64(&tree->new, shadow->new_ofs, shadow,
					GFP_NOFS);

		super->s_dirty_used_bytes += shadow->new_len;
		super->s_dirty_free_bytes += shadow->old_len;
		mark_segment(tree, shadow->old_ofs >> super->s_segshift);
		mark_segment(tree, shadow->new_ofs >> super->s_segshift);
	}
}

static void logfs_set_alias(struct super_block *sb, struct logfs_block *block,
		long child_no)
{
	struct logfs_super *super = logfs_super(sb);

	if (block->inode && block->inode->i_ino == LOGFS_INO_MASTER) {
		/* Aliases in the master inode are pointless. */
		return;
	}

	if (!test_bit(child_no, block->alias_map)) {
		set_bit(child_no, block->alias_map);
		super->s_no_object_aliases++;
	}
	list_move_tail(&block->alias_list, &super->s_object_alias);
}

/*
 * Object aliases can and often do change the size and occupied space of a
 * file.  So not only do we have to change the pointers, we also have to
 * change inode->i_size and li->li_used_bytes.  Which is done by setting
 * another two object aliases for the inode itself.
 */
static void set_iused(struct inode *inode, struct logfs_shadow *shadow)
{
	struct logfs_inode *li = logfs_inode(inode);

	if (shadow->new_len == shadow->old_len)
		return;

	alloc_inode_block(inode);
	li->li_used_bytes += shadow->new_len - shadow->old_len;
	__logfs_set_blocks(inode);
	logfs_set_alias(inode->i_sb, li->li_block, INODE_USED_OFS);
	logfs_set_alias(inode->i_sb, li->li_block, INODE_SIZE_OFS);
}

static int logfs_write_i0(struct inode *inode, struct page *page,
		struct write_control *wc)
{
	struct logfs_shadow *shadow;
	u64 bix;
	level_t level;
	int full, err = 0;

	logfs_unpack_index(page->index, &bix, &level);
	if (wc->ofs == 0)
		if (logfs_reserve_blocks(inode, 1))
			return -ENOSPC;

	shadow = alloc_shadow(inode, bix, level, wc->ofs);
	if (wc->flags & WF_WRITE)
		err = logfs_segment_write(inode, page, shadow);
	if (wc->flags & WF_DELETE)
		logfs_segment_delete(inode, shadow);
	if (err) {
		free_shadow(inode, shadow);
		return err;
	}

	set_iused(inode, shadow);
	full = 1;
	if (level != 0) {
		alloc_indirect_block(inode, page, 0);
		full = logfs_block(page)->full == LOGFS_BLOCK_FACTOR;
	}
	fill_shadow_tree(inode, page, shadow);
	wc->ofs = shadow->new_ofs;
	if (wc->ofs && full)
		wc->ofs |= LOGFS_FULLY_POPULATED;
	return 0;
}

static int logfs_write_direct(struct inode *inode, struct page *page,
		long flags)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct write_control wc = {
		.ofs = li->li_data[page->index],
		.flags = flags,
	};
	int err;

	alloc_inode_block(inode);

	err = logfs_write_i0(inode, page, &wc);
	if (err)
		return err;

	li->li_data[page->index] = wc.ofs;
	logfs_set_alias(inode->i_sb, li->li_block,
			page->index + INODE_POINTER_OFS);
	return 0;
}

static int ptr_change(u64 ofs, struct page *page)
{
	struct logfs_block *block = logfs_block(page);
	int empty0, empty1, full0, full1;

	empty0 = ofs == 0;
	empty1 = block->partial == 0;
	if (empty0 != empty1)
		return 1;

	/* The !! is necessary to shrink result to int */
	full0 = !!(ofs & LOGFS_FULLY_POPULATED);
	full1 = block->full == LOGFS_BLOCK_FACTOR;
	if (full0 != full1)
		return 1;
	return 0;
}

static int __logfs_write_rec(struct inode *inode, struct page *page,
		struct write_control *this_wc,
		pgoff_t bix, level_t target_level, level_t level)
{
	int ret, page_empty = 0;
	int child_no = get_bits(bix, SUBLEVEL(level));
	struct page *ipage;
	struct write_control child_wc = {
		.flags = this_wc->flags,
	};

	ipage = logfs_get_write_page(inode, bix, level);
	if (!ipage)
		return -ENOMEM;

	if (this_wc->ofs) {
		ret = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
		if (ret)
			goto out;
	} else if (!PageUptodate(ipage)) {
		page_empty = 1;
		logfs_read_empty(ipage);
	}

	child_wc.ofs = block_get_pointer(ipage, child_no);

	if ((__force u8)level-1 > (__force u8)target_level)
		ret = __logfs_write_rec(inode, page, &child_wc, bix,
				target_level, SUBLEVEL(level));
	else
		ret = logfs_write_i0(inode, page, &child_wc);

	if (ret)
		goto out;

	alloc_indirect_block(inode, ipage, page_empty);
	block_set_pointer(ipage, child_no, child_wc.ofs);
	/* FIXME: first condition seems superfluous */
	if (child_wc.ofs || logfs_block(ipage)->partial)
		this_wc->flags |= WF_WRITE;
	/* the condition on this_wc->ofs ensures that we won't consume extra
	 * space for indirect blocks in the future, which we cannot reserve */
	if (!this_wc->ofs || ptr_change(this_wc->ofs, ipage))
		ret = logfs_write_i0(inode, ipage, this_wc);
	else
		logfs_set_alias(inode->i_sb, logfs_block(ipage), child_no);
out:
	logfs_put_write_page(ipage);
	return ret;
}

static int logfs_write_rec(struct inode *inode, struct page *page,
		pgoff_t bix, level_t target_level, long flags)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct write_control wc = {
		.ofs = li->li_data[INDIRECT_INDEX],
		.flags = flags,
	};
	int ret;

	alloc_inode_block(inode);

	if (li->li_height > (__force u8)target_level)
		ret = __logfs_write_rec(inode, page, &wc, bix, target_level,
				LEVEL(li->li_height));
	else
		ret = logfs_write_i0(inode, page, &wc);
	if (ret)
		return ret;

	if (li->li_data[INDIRECT_INDEX] != wc.ofs) {
		li->li_data[INDIRECT_INDEX] = wc.ofs;
		logfs_set_alias(inode->i_sb, li->li_block,
				INDIRECT_INDEX + INODE_POINTER_OFS);
	}
	return ret;
}

void logfs_add_transaction(struct inode *inode, struct logfs_transaction *ta)
{
	alloc_inode_block(inode);
	logfs_inode(inode)->li_block->ta = ta;
}

void logfs_del_transaction(struct inode *inode, struct logfs_transaction *ta)
{
	struct logfs_block *block = logfs_inode(inode)->li_block;

	if (block && block->ta)
		block->ta = NULL;
}

static int grow_inode(struct inode *inode, u64 bix, level_t level)
{
	struct logfs_inode *li = logfs_inode(inode);
	u8 height = (__force u8)level;
	struct page *page;
	struct write_control wc = {
		.flags = WF_WRITE,
	};
	int err;

	BUG_ON(height > 5 || li->li_height > 5);
	while (height > li->li_height || bix >= maxbix(li->li_height)) {
		page = logfs_get_write_page(inode, I0_BLOCKS + 1,
				LEVEL(li->li_height + 1));
		if (!page)
			return -ENOMEM;
		logfs_read_empty(page);
		alloc_indirect_block(inode, page, 1);
		block_set_pointer(page, 0, li->li_data[INDIRECT_INDEX]);
		err = logfs_write_i0(inode, page, &wc);
		logfs_put_write_page(page);
		if (err)
			return err;
		li->li_data[INDIRECT_INDEX] = wc.ofs;
		wc.ofs = 0;
		li->li_height++;
		logfs_set_alias(inode->i_sb, li->li_block, INODE_HEIGHT_OFS);
	}
	return 0;
}

static int __logfs_write_buf(struct inode *inode, struct page *page, long flags)
{
	struct logfs_super *super = logfs_super(inode->i_sb);
	pgoff_t index = page->index;
	u64 bix;
	level_t level;
	int err;

	flags |= WF_WRITE | WF_DELETE;
	inode->i_ctime = inode->i_mtime = CURRENT_TIME;

	logfs_unpack_index(index, &bix, &level);
	if (logfs_block(page) && logfs_block(page)->reserved_bytes)
		super->s_dirty_pages -= logfs_block(page)->reserved_bytes;

	if (index < I0_BLOCKS)
		return logfs_write_direct(inode, page, flags);

	bix = adjust_bix(bix, level);
	err = grow_inode(inode, bix, level);
	if (err)
		return err;
	return logfs_write_rec(inode, page, bix, level, flags);
}

int logfs_write_buf(struct inode *inode, struct page *page, long flags)
{
	struct super_block *sb = inode->i_sb;
	int ret;

	logfs_get_wblocks(sb, page, flags & WF_LOCK);
	ret = __logfs_write_buf(inode, page, flags);
	logfs_put_wblocks(sb, page, flags & WF_LOCK);
	return ret;
}

static int __logfs_delete(struct inode *inode, struct page *page)
{
	long flags = WF_DELETE;
	int err;

	inode->i_ctime = inode->i_mtime = CURRENT_TIME;

	if (page->index < I0_BLOCKS)
		return logfs_write_direct(inode, page, flags);
	err = grow_inode(inode, page->index, 0);
	if (err)
		return err;
	return logfs_write_rec(inode, page, page->index, 0, flags);
}

int logfs_delete(struct inode *inode, pgoff_t index,
		struct shadow_tree *shadow_tree)
{
	struct super_block *sb = inode->i_sb;
	struct page *page;
	int ret;

	page = logfs_get_read_page(inode, index, 0);
	if (!page)
		return -ENOMEM;

	logfs_get_wblocks(sb, page, 1);
	ret = __logfs_delete(inode, page);
	logfs_put_wblocks(sb, page, 1);

	logfs_put_read_page(page);

	return ret;
}

int logfs_rewrite_block(struct inode *inode, u64 bix, u64 ofs,
		gc_level_t gc_level, long flags)
{
	level_t level = shrink_level(gc_level);
	struct page *page;
	int err;

	page = logfs_get_write_page(inode, bix, level);
	if (!page)
		return -ENOMEM;

	err = logfs_segment_read(inode, page, ofs, bix, level);
	if (!err) {
		if (level != 0)
			alloc_indirect_block(inode, page, 0);
		err = logfs_write_buf(inode, page, flags);
		if (!err && shrink_level(gc_level) == 0) {
			/* Rewrite cannot mark the inode dirty but has to
			 * write it immediately.
			 * Q: Can't we just create an alias for the inode
			 * instead?  And if not, why not?
			 */
			if (inode->i_ino == LOGFS_INO_MASTER)
				logfs_write_anchor(inode->i_sb);
			else {
				err = __logfs_write_inode(inode, page, flags);
			}
		}
	}
	logfs_put_write_page(page);
	return err;
}

static int truncate_data_block(struct inode *inode, struct page *page,
		u64 ofs, struct logfs_shadow *shadow, u64 size)
{
	loff_t pageofs = page->index << inode->i_sb->s_blocksize_bits;
	u64 bix;
	level_t level;
	int err;

	/* Does truncation happen within this page? */
	if (size <= pageofs || size - pageofs >= PAGE_SIZE)
		return 0;

	logfs_unpack_index(page->index, &bix, &level);
	BUG_ON(level != 0);

	err = logfs_segment_read(inode, page, ofs, bix, level);
	if (err)
		return err;

	zero_user_segment(page, size - pageofs, PAGE_CACHE_SIZE);
	return logfs_segment_write(inode, page, shadow);
}

static int logfs_truncate_i0(struct inode *inode, struct page *page,
		struct write_control *wc, u64 size)
{
	struct logfs_shadow *shadow;
	u64 bix;
	level_t level;
	int err = 0;

	logfs_unpack_index(page->index, &bix, &level);
	BUG_ON(level != 0);
	shadow = alloc_shadow(inode, bix, level, wc->ofs);

	err = truncate_data_block(inode, page, wc->ofs, shadow, size);
	if (err) {
		free_shadow(inode, shadow);
		return err;
	}

	logfs_segment_delete(inode, shadow);
	set_iused(inode, shadow);
	fill_shadow_tree(inode, page, shadow);
	wc->ofs = shadow->new_ofs;
	return 0;
}

static int logfs_truncate_direct(struct inode *inode, u64 size)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct write_control wc;
	struct page *page;
	int e;
	int err;

	alloc_inode_block(inode);

	for (e = I0_BLOCKS - 1; e >= 0; e--) {
		if (size > (e+1) * LOGFS_BLOCKSIZE)
			break;

		wc.ofs = li->li_data[e];
		if (!wc.ofs)
			continue;

		page = logfs_get_write_page(inode, e, 0);
		if (!page)
			return -ENOMEM;
		err = logfs_segment_read(inode, page, wc.ofs, e, 0);
		if (err) {
			logfs_put_write_page(page);
			return err;
		}
		err = logfs_truncate_i0(inode, page, &wc, size);
		logfs_put_write_page(page);
		if (err)
			return err;

		li->li_data[e] = wc.ofs;
	}
	return 0;
}

/* FIXME: these need to become per-sb once we support different blocksizes */
static u64 __logfs_step[] = {
	1,
	I1_BLOCKS,
	I2_BLOCKS,
	I3_BLOCKS,
};

static u64 __logfs_start_index[] = {
	I0_BLOCKS,
	I1_BLOCKS,
	I2_BLOCKS,
	I3_BLOCKS
};

static inline u64 logfs_step(level_t level)
{
	return __logfs_step[(__force u8)level];
}

static inline u64 logfs_factor(u8 level)
{
	return __logfs_step[level] * LOGFS_BLOCKSIZE;
}

static inline u64 logfs_start_index(level_t level)
{
	return __logfs_start_index[(__force u8)level];
}

static void logfs_unpack_raw_index(pgoff_t index, u64 *bix, level_t *level)
{
	logfs_unpack_index(index, bix, level);
	if (*bix <= logfs_start_index(SUBLEVEL(*level)))
		*bix = 0;
}

static int __logfs_truncate_rec(struct inode *inode, struct page *ipage,
		struct write_control *this_wc, u64 size)
{
	int truncate_happened = 0;
	int e, err = 0;
	u64 bix, child_bix, next_bix;
	level_t level;
	struct page *page;
	struct write_control child_wc = { /* FIXME: flags */ };

	logfs_unpack_raw_index(ipage->index, &bix, &level);
	err = logfs_segment_read(inode, ipage, this_wc->ofs, bix, level);
	if (err)
		return err;

	for (e = LOGFS_BLOCK_FACTOR - 1; e >= 0; e--) {
		child_bix = bix + e * logfs_step(SUBLEVEL(level));
		next_bix = child_bix + logfs_step(SUBLEVEL(level));
		if (size > next_bix * LOGFS_BLOCKSIZE)
			break;

		child_wc.ofs = pure_ofs(block_get_pointer(ipage, e));
		if (!child_wc.ofs)
			continue;

		page = logfs_get_write_page(inode, child_bix, SUBLEVEL(level));
		if (!page)
			return -ENOMEM;

		if ((__force u8)level > 1)
			err = __logfs_truncate_rec(inode, page, &child_wc, size);
		else
			err = logfs_truncate_i0(inode, page, &child_wc, size);
		logfs_put_write_page(page);
		if (err)
			return err;

		truncate_happened = 1;
		alloc_indirect_block(inode, ipage, 0);
		block_set_pointer(ipage, e, child_wc.ofs);
	}

	if (!truncate_happened) {
		printk("ineffectual truncate (%lx, %lx, %llx)\n", inode->i_ino, ipage->index, size);
		return 0;
	}

	this_wc->flags = WF_DELETE;
	if (logfs_block(ipage)->partial)
		this_wc->flags |= WF_WRITE;

	return logfs_write_i0(inode, ipage, this_wc);
}

static int logfs_truncate_rec(struct inode *inode, u64 size)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct write_control wc = {
		.ofs = li->li_data[INDIRECT_INDEX],
	};
	struct page *page;
	int err;

	alloc_inode_block(inode);

	if (!wc.ofs)
		return 0;

	page = logfs_get_write_page(inode, 0, LEVEL(li->li_height));
	if (!page)
		return -ENOMEM;

	err = __logfs_truncate_rec(inode, page, &wc, size);
	logfs_put_write_page(page);
	if (err)
		return err;

	if (li->li_data[INDIRECT_INDEX] != wc.ofs)
		li->li_data[INDIRECT_INDEX] = wc.ofs;
	return 0;
}

static int __logfs_truncate(struct inode *inode, u64 size)
{
	int ret;

	if (size >= logfs_factor(logfs_inode(inode)->li_height))
		return 0;

	ret = logfs_truncate_rec(inode, size);
	if (ret)
		return ret;

	return logfs_truncate_direct(inode, size);
}

/*
 * Truncate, by changing the segment file, can consume a fair amount
 * of resources.  So back off from time to time and do some GC.
 * 8 or 2048 blocks should be well within safety limits even if
 * every single block resided in a different segment.
 */
#define TRUNCATE_STEP	(8 * 1024 * 1024)
int logfs_truncate(struct inode *inode, u64 target)
{
	struct super_block *sb = inode->i_sb;
	u64 size = i_size_read(inode);
	int err = 0;

	size = ALIGN(size, TRUNCATE_STEP);
	while (size > target) {
		if (size > TRUNCATE_STEP)
			size -= TRUNCATE_STEP;
		else
			size = 0;
		if (size < target)
			size = target;

		logfs_get_wblocks(sb, NULL, 1);
		err = __logfs_truncate(inode, size);
		if (!err)
			err = __logfs_write_inode(inode, NULL, 0);
		logfs_put_wblocks(sb, NULL, 1);
	}

	if (!err)
		err = vmtruncate(inode, target);

	/* I don't trust error recovery yet. */
	WARN_ON(err);
	return err;
}

static void move_page_to_inode(struct inode *inode, struct page *page)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct logfs_block *block = logfs_block(page);

	if (!block)
		return;

	log_blockmove("move_page_to_inode(%llx, %llx, %x)\n",
			block->ino, block->bix, block->level);
	BUG_ON(li->li_block);
	block->ops = &inode_block_ops;
	block->inode = inode;
	li->li_block = block;

	block->page = NULL;
	if (PagePrivate(page)) {
		ClearPagePrivate(page);
		page_cache_release(page);
		set_page_private(page, 0);
	}
}

static void move_inode_to_page(struct page *page, struct inode *inode)
{
	struct logfs_inode *li = logfs_inode(inode);
	struct logfs_block *block = li->li_block;

	if (!block)
		return;

	log_blockmove("move_inode_to_page(%llx, %llx, %x)\n",
			block->ino, block->bix, block->level);
	BUG_ON(PagePrivate(page));
	block->ops = &indirect_block_ops;
	block->page = page;

	if (!PagePrivate(page)) {
		SetPagePrivate(page);
		page_cache_get(page);
		set_page_private(page, (unsigned long) block);
	}

	block->inode = NULL;
	li->li_block = NULL;
}

int logfs_read_inode(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;
	struct logfs_super *super = logfs_super(sb);
	struct inode *master_inode = super->s_master_inode;
	struct page *page;
	struct logfs_disk_inode *di;
	u64 ino = inode->i_ino;

	if (ino << sb->s_blocksize_bits > i_size_read(master_inode))
		return -ENODATA;
	if (!logfs_exist_block(master_inode, ino))
		return -ENODATA;

	page = read_cache_page(master_inode->i_mapping, ino,
			(filler_t *)logfs_readpage, NULL);
	if (IS_ERR(page))
		return PTR_ERR(page);

	di = kmap_atomic(page);
	logfs_disk_to_inode(di, inode);
	kunmap_atomic(di);
	move_page_to_inode(inode, page);
	page_cache_release(page);
	return 0;
}

/* Caller must logfs_put_write_page(page); */
static struct page *inode_to_page(struct inode *inode)
{
	struct inode *master_inode = logfs_super(inode->i_sb)->s_master_inode;
	struct logfs_disk_inode *di;
	struct page *page;

	BUG_ON(inode->i_ino == LOGFS_INO_MASTER);

	page = logfs_get_write_page(master_inode, inode->i_ino, 0);
	if (!page)
		return NULL;

	di = kmap_atomic(page);
	logfs_inode_to_disk(inode, di);
	kunmap_atomic(di);
	move_inode_to_page(page, inode);
	return page;
}

static int do_write_inode(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;
	struct inode *master_inode = logfs_super(sb)->s_master_inode;
	loff_t size = (inode->i_ino + 1) << inode->i_sb->s_blocksize_bits;
	struct page *page;
	int err;

	BUG_ON(inode->i_ino == LOGFS_INO_MASTER);
	/* FIXME: lock inode */

	if (i_size_read(master_inode) < size)
		i_size_write(master_inode, size);

	/* TODO: Tell vfs this inode is clean now */

	page = inode_to_page(inode);
	if (!page)
		return -ENOMEM;

	/* FIXME: transaction is part of logfs_block now.  Is that enough? */
	err = logfs_write_buf(master_inode, page, 0);
	if (err)
		move_page_to_inode(inode, page);

	logfs_put_write_page(page);
	return err;
}

static void logfs_mod_segment_entry(struct super_block *sb, u32 segno,
		int write,
		void (*change_se)(struct logfs_segment_entry *, long),
		long arg)
{
	struct logfs_super *super = logfs_super(sb);
	struct inode *inode;
	struct page *page;
	struct logfs_segment_entry *se;
	pgoff_t page_no;
	int child_no;

	page_no = segno >> (sb->s_blocksize_bits - 3);
	child_no = segno & ((sb->s_blocksize >> 3) - 1);

	inode = super->s_segfile_inode;
	page = logfs_get_write_page(inode, page_no, 0);
	BUG_ON(!page); /* FIXME: We need some reserve page for this case */
	if (!PageUptodate(page))
		logfs_read_block(inode, page, WRITE);

	if (write)
		alloc_indirect_block(inode, page, 0);
	se = kmap_atomic(page);
	change_se(se + child_no, arg);
	if (write) {
		logfs_set_alias(sb, logfs_block(page), child_no);
		BUG_ON((int)be32_to_cpu(se[child_no].valid) > super->s_segsize);
	}
	kunmap_atomic(se);

	logfs_put_write_page(page);
}

static void __get_segment_entry(struct logfs_segment_entry *se, long _target)
{
	struct logfs_segment_entry *target = (void *)_target;

	*target = *se;
}

void logfs_get_segment_entry(struct super_block *sb, u32 segno,
		struct logfs_segment_entry *se)
{
	logfs_mod_segment_entry(sb, segno, 0, __get_segment_entry, (long)se);
}

static void __set_segment_used(struct logfs_segment_entry *se, long increment)
{
	u32 valid;

	valid = be32_to_cpu(se->valid);
	valid += increment;
	se->valid = cpu_to_be32(valid);
}

void logfs_set_segment_used(struct super_block *sb, u64 ofs, int increment)
{
	struct logfs_super *super = logfs_super(sb);
	u32 segno = ofs >> super->s_segshift;

	if (!increment)
		return;

	logfs_mod_segment_entry(sb, segno, 1, __set_segment_used, increment);
}

static void __set_segment_erased(struct logfs_segment_entry *se, long ec_level)
{
	se->ec_level = cpu_to_be32(ec_level);
}

void logfs_set_segment_erased(struct super_block *sb, u32 segno, u32 ec,
		gc_level_t gc_level)
{
	u32 ec_level = ec << 4 | (__force u8)gc_level;

	logfs_mod_segment_entry(sb, segno, 1, __set_segment_erased, ec_level);
}

static void __set_segment_reserved(struct logfs_segment_entry *se, long ignore)
{
	se->valid = cpu_to_be32(RESERVED);
}

void logfs_set_segment_reserved(struct super_block *sb, u32 segno)
{
	logfs_mod_segment_entry(sb, segno, 1, __set_segment_reserved, 0);
}

static void __set_segment_unreserved(struct logfs_segment_entry *se,
		long ec_level)
{
	se->valid = 0;
	se->ec_level = cpu_to_be32(ec_level);
}

void logfs_set_segment_unreserved(struct super_block *sb, u32 segno, u32 ec)
{
	u32 ec_level = ec << 4;

	logfs_mod_segment_entry(sb, segno, 1, __set_segment_unreserved,
			ec_level);
}

int __logfs_write_inode(struct inode *inode, struct page *page, long flags)
{
	struct super_block *sb = inode->i_sb;
	int ret;

	logfs_get_wblocks(sb, page, flags & WF_LOCK);
	ret = do_write_inode(inode);
	logfs_put_wblocks(sb, page, flags & WF_LOCK);
	return ret;
}

static int do_delete_inode(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;
	struct inode *master_inode = logfs_super(sb)->s_master_inode;
	struct page *page;
	int ret;

	page = logfs_get_write_page(master_inode, inode->i_ino, 0);
	if (!page)
		return -ENOMEM;

	move_inode_to_page(page, inode);

	logfs_get_wblocks(sb, page, 1);
	ret = __logfs_delete(master_inode, page);
	logfs_put_wblocks(sb, page, 1);

	logfs_put_write_page(page);
	return ret;
}

/*
 * ZOMBIE inodes have already been deleted before and should remain dead,
 * if it weren't for valid checking.  No need to kill them again here.
 */
void logfs_evict_inode(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;
	struct logfs_inode *li = logfs_inode(inode);
	struct logfs_block *block = li->li_block;
	struct page *page;

	if (!inode->i_nlink) {
		if (!(li->li_flags & LOGFS_IF_ZOMBIE)) {
			li->li_flags |= LOGFS_IF_ZOMBIE;
			if (i_size_read(inode) > 0)
				logfs_truncate(inode, 0);
			do_delete_inode(inode);
		}
	}
	truncate_inode_pages(&inode->i_data, 0);
	clear_inode(inode);

	/* Cheaper version of write_inode.  All changes are concealed in
	 * aliases, which are moved back.  No write to the medium happens.
	 */
	/* Only deleted files may be dirty at this point */
	BUG_ON(inode->i_state & I_DIRTY && inode->i_nlink);
	if (!block)
		return;
	if ((logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN)) {
		block->ops->free_block(inode->i_sb, block);
		return;
	}

	BUG_ON(inode->i_ino < LOGFS_RESERVED_INOS);
	page = inode_to_page(inode);
	BUG_ON(!page); /* FIXME: Use emergency page */
	logfs_put_write_page(page);
}

void btree_write_block(struct logfs_block *block)
{
	struct inode *inode;
	struct page *page;
	int err, cookie;

	inode = logfs_safe_iget(block->sb, block->ino, &cookie);
	page = logfs_get_write_page(inode, block->bix, block->level);

	err = logfs_readpage_nolock(page);
	BUG_ON(err);
	BUG_ON(!PagePrivate(page));
	BUG_ON(logfs_block(page) != block);
	err = __logfs_write_buf(inode, page, 0);
	BUG_ON(err);
	BUG_ON(PagePrivate(page) || page->private);

	logfs_put_write_page(page);
	logfs_safe_iput(inode, cookie);
}

/**
 * logfs_inode_write - write inode or dentry objects
 *
 * @inode:		parent inode (ifile or directory)
 * @buf:		object to write (inode or dentry)
 * @n:			object size
 * @_pos:		object number (file position in blocks/objects)
 * @flags:		write flags
 * @lock:		0 if write lock is already taken, 1 otherwise
 * @shadow_tree:	shadow below this inode
 *
 * FIXME: All caller of this put a 200-300 byte variable on the stack,
 * only to call here and do a memcpy from that stack variable.  A good
 * example of wasted performance and stack space.
 */
int logfs_inode_write(struct inode *inode, const void *buf, size_t count,
		loff_t bix, long flags, struct shadow_tree *shadow_tree)
{
	loff_t pos = bix << inode->i_sb->s_blocksize_bits;
	int err;
	struct page *page;
	void *pagebuf;

	BUG_ON(pos & (LOGFS_BLOCKSIZE-1));
	BUG_ON(count > LOGFS_BLOCKSIZE);
	page = logfs_get_write_page(inode, bix, 0);
	if (!page)
		return -ENOMEM;

	pagebuf = kmap_atomic(page);
	memcpy(pagebuf, buf, count);
	flush_dcache_page(page);
	kunmap_atomic(pagebuf);

	if (i_size_read(inode) < pos + LOGFS_BLOCKSIZE)
		i_size_write(inode, pos + LOGFS_BLOCKSIZE);

	err = logfs_write_buf(inode, page, flags);
	logfs_put_write_page(page);
	return err;
}

int logfs_open_segfile(struct super_block *sb)
{
	struct logfs_super *super = logfs_super(sb);
	struct inode *inode;

	inode = logfs_read_meta_inode(sb, LOGFS_INO_SEGFILE);
	if (IS_ERR(inode))
		return PTR_ERR(inode);
	super->s_segfile_inode = inode;
	return 0;
}

int logfs_init_rw(struct super_block *sb)
{
	struct logfs_super *super = logfs_super(sb);
	int min_fill = 3 * super->s_no_blocks;

	INIT_LIST_HEAD(&super->s_object_alias);
	INIT_LIST_HEAD(&super->s_writeback_list);
	mutex_init(&super->s_write_mutex);
	super->s_block_pool = mempool_create_kmalloc_pool(min_fill,
			sizeof(struct logfs_block));
	super->s_shadow_pool = mempool_create_kmalloc_pool(min_fill,
			sizeof(struct logfs_shadow));
	return 0;
}

void logfs_cleanup_rw(struct super_block *sb)
{
	struct logfs_super *super = logfs_super(sb);

	logfs_mempool_destroy(super->s_block_pool);
	logfs_mempool_destroy(super->s_shadow_pool);
}