summaryrefslogtreecommitdiffstats
path: root/cpukit/libblock/src/bdbuf.c
blob: 8be4bd8180aadbbde14b25c5818a36e4cddaeff9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
/**
 * @file
 *
 * @ingroup rtems_bdbuf
 *
 * Block device buffer management.
 */

/*
 * Disk I/O buffering
 * Buffer managment
 *
 * Copyright (C) 2001 OKTET Ltd., St.-Peterburg, Russia
 * Author: Andrey G. Ivanov <Andrey.Ivanov@oktet.ru>
 *         Victor V. Vengerov <vvv@oktet.ru>
 *         Alexander Kukuta <kam@oktet.ru>
 *
 * Copyright (C) 2008 Chris Johns <chrisj@rtems.org>
 *    Rewritten to remove score mutex access. Fixes many performance
 *    issues.
 * 
 * @(#) bdbuf.c,v 1.14 2004/04/17 08:15:17 ralf Exp
 */

/**
 * Set to 1 to enable debug tracing.
 */
#define RTEMS_BDBUF_TRACE 0

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems.h>
#include <rtems/error.h>
#include <rtems/malloc.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>

#if RTEMS_BDBUF_TRACE
#include <stdio.h>
#endif

#include "rtems/bdbuf.h"

/**
 * The BD buffer context.
 */
typedef struct rtems_bdbuf_context {
  rtems_bdbuf_pool* pool;      /*< Table of buffer pools */
  int               npools;    /*< Number of entries in pool table */
  rtems_id          swapout;   /*< Swapout task ID */
  bool              swapout_enabled;
} rtems_bdbuf_context;

/**
 * Fatal errors
 */
#define RTEMS_BLKDEV_FATAL_ERROR(n) \
  (((uint32_t)'B' << 24) | ((uint32_t)(n) & (uint32_t)0x00FFFFFF))

#define RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY RTEMS_BLKDEV_FATAL_ERROR(1)
#define RTEMS_BLKDEV_FATAL_BDBUF_SWAPOUT     RTEMS_BLKDEV_FATAL_ERROR(2)
#define RTEMS_BLKDEV_FATAL_BDBUF_SYNC_LOCK   RTEMS_BLKDEV_FATAL_ERROR(3)
#define RTEMS_BLKDEV_FATAL_BDBUF_SYNC_UNLOCK RTEMS_BLKDEV_FATAL_ERROR(4)
#define RTEMS_BLKDEV_FATAL_BDBUF_POOL_LOCK   RTEMS_BLKDEV_FATAL_ERROR(5)
#define RTEMS_BLKDEV_FATAL_BDBUF_POOL_UNLOCK RTEMS_BLKDEV_FATAL_ERROR(6)
#define RTEMS_BLKDEV_FATAL_BDBUF_POOL_WAIT   RTEMS_BLKDEV_FATAL_ERROR(7)
#define RTEMS_BLKDEV_FATAL_BDBUF_POOL_WAKE   RTEMS_BLKDEV_FATAL_ERROR(8)
#define RTEMS_BLKDEV_FATAL_BDBUF_SO_WAKE     RTEMS_BLKDEV_FATAL_ERROR(9)
#define RTEMS_BLKDEV_FATAL_BDBUF_SO_NOMEM    RTEMS_BLKDEV_FATAL_ERROR(10)
#define BLKDEV_FATAL_BDBUF_SWAPOUT_RE        RTEMS_BLKDEV_FATAL_ERROR(11)
#define BLKDEV_FATAL_BDBUF_SWAPOUT_TS        RTEMS_BLKDEV_FATAL_ERROR(12)

/**
 * The events used in this code. These should be system events rather than
 * application events.
 */
#define RTEMS_BDBUF_TRANSFER_SYNC  RTEMS_EVENT_1
#define RTEMS_BDBUF_SWAPOUT_SYNC   RTEMS_EVENT_2

/**
 * The swap out task size. Should be more than enough for most drivers with
 * tracing turned on.
 */
#define SWAPOUT_TASK_STACK_SIZE (8 * 1024)

/**
 * Lock semaphore attributes. This is used for locking type mutexes.
 *
 * @warning Priority inheritance is on.
 */
#define RTEMS_BDBUF_POOL_LOCK_ATTRIBS \
  (RTEMS_PRIORITY | RTEMS_BINARY_SEMAPHORE | \
   RTEMS_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL)

/**
 * Waiter semaphore attributes.
 *
 * @warning Do not configure as inherit priority. If a driver is in the driver
 *          initialisation table this locked semaphore will have the IDLE task
 *          as the holder and a blocking task will raise the priority of the
 *          IDLE task which can cause unsual side effects.
 */
#define RTEMS_BDBUF_POOL_WAITER_ATTRIBS \
  (RTEMS_PRIORITY | RTEMS_SIMPLE_BINARY_SEMAPHORE | \
   RTEMS_NO_INHERIT_PRIORITY | RTEMS_NO_PRIORITY_CEILING | RTEMS_LOCAL)

/*
 * The swap out task.
 */
static rtems_task rtems_bdbuf_swapout_task(rtems_task_argument arg);

/**
 * The context of buffering layer.
 */
static rtems_bdbuf_context rtems_bdbuf_ctx;

/**
 * Print a message to the bdbuf trace output and flush it.
 *
 * @param format The format string. See printf for details.
 * @param ... The arguments for the format text.
 * @return int The number of bytes written to the output.
 */
#if RTEMS_BDBUF_TRACE
bool rtems_bdbuf_tracer;
static void
rtems_bdbuf_printf (const char *format, ...)
{
  va_list args;
  va_start (args, format);
  if (rtems_bdbuf_tracer)
  {
    fprintf (stdout, "bdbuf:");
    vfprintf (stdout, format, args);
    fflush (stdout);
  }
}
#endif

/**
 * The default maximum height of 32 allows for AVL trees having between
 * 5,704,880 and 4,294,967,295 nodes, depending on order of insertion.  You may
 * change this compile-time constant as you wish.
 */
#ifndef RTEMS_BDBUF_AVL_MAX_HEIGHT
#define RTEMS_BDBUF_AVL_MAX_HEIGHT (32)
#endif

/**
 * Searches for the node with specified dev/block.
 *
 * @param root pointer to the root node of the AVL-Tree
 * @param dev device search key
 * @param block block search key
 * @retval NULL node with the specified dev/block is not found
 * @return pointer to the node with specified dev/block
 */
static rtems_bdbuf_buffer *
rtems_bdbuf_avl_search (rtems_bdbuf_buffer** root,
                        dev_t                dev,
                        rtems_blkdev_bnum    block)
{
  rtems_bdbuf_buffer* p = *root;

  while ((p != NULL) && ((p->dev != dev) || (p->block != block)))
  {
    if ((p->dev < dev) || ((p->dev == dev) && (p->block < block)))
    {
      p = p->avl.right;
    }
    else
    {
      p = p->avl.left;
    }
  }

  return p;
}

/**
 * Inserts the specified node to the AVl-Tree.
 *
 * @param root pointer to the root node of the AVL-Tree
 * @param node Pointer to the node to add.
 * @retval 0 The node added successfully
 * @retval -1 An error occured
 */
static int
rtems_bdbuf_avl_insert(rtems_bdbuf_buffer** root,
                       rtems_bdbuf_buffer*  node)
{
  dev_t             dev = node->dev;
  rtems_blkdev_bnum block = node->block;

  rtems_bdbuf_buffer*  p = *root;
  rtems_bdbuf_buffer*  q;
  rtems_bdbuf_buffer*  p1;
  rtems_bdbuf_buffer*  p2;
  rtems_bdbuf_buffer*  buf_stack[RTEMS_BDBUF_AVL_MAX_HEIGHT];
  rtems_bdbuf_buffer** buf_prev = buf_stack;

  bool modified = false;

  if (p == NULL)
  {
    *root = node;
    node->avl.left = NULL;
    node->avl.right = NULL;
    node->avl.bal = 0;
    return 0;
  }

  while (p != NULL)
  {
    *buf_prev++ = p;

    if ((p->dev < dev) || ((p->dev == dev) && (p->block < block)))
    {
      p->avl.cache = 1;
      q = p->avl.right;
      if (q == NULL)
      {
        q = node;
        p->avl.right = q = node;
        break;
      }
    }
    else if ((p->dev != dev) || (p->block != block))
    {
      p->avl.cache = -1;
      q = p->avl.left;
      if (q == NULL)
      {
        q = node;
        p->avl.left = q;
        break;
      }
    }
    else
    {
      return -1;
    }

    p = q;
  }

  q->avl.left = q->avl.right = NULL;
  q->avl.bal = 0;
  modified = true;
  buf_prev--;

  while (modified)
  {
    if (p->avl.cache == -1)
    {
      switch (p->avl.bal)
      {
        case 1:
          p->avl.bal = 0;
          modified = false;
          break;

        case 0:
          p->avl.bal = -1;
          break;

        case -1:
          p1 = p->avl.left;
          if (p1->avl.bal == -1) /* simple LL-turn */
          {
            p->avl.left = p1->avl.right;
            p1->avl.right = p;
            p->avl.bal = 0;
            p = p1;
          }
          else /* double LR-turn */
          {
            p2 = p1->avl.right;
            p1->avl.right = p2->avl.left;
            p2->avl.left = p1;
            p->avl.left = p2->avl.right;
            p2->avl.right = p;
            if (p2->avl.bal == -1) p->avl.bal = +1; else p->avl.bal = 0;
            if (p2->avl.bal == +1) p1->avl.bal = -1; else p1->avl.bal = 0;
            p = p2;
          }
          p->avl.bal = 0;
          modified = false;
          break;

        default:
          break;
      }
    }
    else
    {
      switch (p->avl.bal)
      {
        case -1:
          p->avl.bal = 0;
          modified = false;
          break;

        case 0:
          p->avl.bal = 1;
          break;

        case 1:
          p1 = p->avl.right;
          if (p1->avl.bal == 1) /* simple RR-turn */
          {
            p->avl.right = p1->avl.left;
            p1->avl.left = p;
            p->avl.bal = 0;
            p = p1;
          }
          else /* double RL-turn */
          {
            p2 = p1->avl.left;
            p1->avl.left = p2->avl.right;
            p2->avl.right = p1;
            p->avl.right = p2->avl.left;
            p2->avl.left = p;
            if (p2->avl.bal == +1) p->avl.bal = -1; else p->avl.bal = 0;
            if (p2->avl.bal == -1) p1->avl.bal = +1; else p1->avl.bal = 0;
            p = p2;
          }
          p->avl.bal = 0;
          modified = false;
          break;

        default:
          break;
      }
    }
    q = p;
    if (buf_prev > buf_stack)
    {
      p = *--buf_prev;

      if (p->avl.cache == -1)
      {
        p->avl.left = q;
      }
      else
      {
        p->avl.right = q;
      }
    }
    else
    {
      *root = p;
      break;
    }
  };

  return 0;
}


/**
 * Removes the node from the tree.
 *
 * @param root Pointer to pointer to the root node
 * @param node Pointer to the node to remove
 * @retval 0 Item removed
 * @retval -1 No such item found
 */
static int
rtems_bdbuf_avl_remove(rtems_bdbuf_buffer**      root,
                       const rtems_bdbuf_buffer* node)
{
  dev_t             dev = node->dev;
  rtems_blkdev_bnum block = node->block;

  rtems_bdbuf_buffer*  p = *root;
  rtems_bdbuf_buffer*  q;
  rtems_bdbuf_buffer*  r;
  rtems_bdbuf_buffer*  s;
  rtems_bdbuf_buffer*  p1;
  rtems_bdbuf_buffer*  p2;
  rtems_bdbuf_buffer*  buf_stack[RTEMS_BDBUF_AVL_MAX_HEIGHT];
  rtems_bdbuf_buffer** buf_prev = buf_stack;

  bool modified = false;

  memset (buf_stack, 0, sizeof(buf_stack));

  while (p != NULL)
  {
    *buf_prev++ = p;

    if ((p->dev < dev) || ((p->dev == dev) && (p->block < block)))
    {
      p->avl.cache = 1;
      p = p->avl.right;
    }
    else if ((p->dev != dev) || (p->block != block))
    {
      p->avl.cache = -1;
      p = p->avl.left;
    }
    else
    {
      /* node found */
      break;
    }
  }

  if (p == NULL)
  {
    /* there is no such node */
    return -1;
  }

  q = p;

  buf_prev--;
  if (buf_prev > buf_stack)
  {
    p = *(buf_prev - 1);
  }
  else
  {
    p = NULL;
  }

  /* at this moment q - is a node to delete, p is q's parent */
  if (q->avl.right == NULL)
  {
    r = q->avl.left;
    if (r != NULL)
    {
      r->avl.bal = 0;
    }
    q = r;
  }
  else
  {
    rtems_bdbuf_buffer **t;

    r = q->avl.right;

    if (r->avl.left == NULL)
    {
      r->avl.left = q->avl.left;
      r->avl.bal = q->avl.bal;
      r->avl.cache = 1;
      *buf_prev++ = q = r;
    }
    else
    {
      t = buf_prev++;
      s = r;

      while (s->avl.left != NULL)
      {
        *buf_prev++ = r = s;
        s = r->avl.left;
        r->avl.cache = -1;
      }

      s->avl.left = q->avl.left;
      r->avl.left = s->avl.right;
      s->avl.right = q->avl.right;
      s->avl.bal = q->avl.bal;
      s->avl.cache = 1;

      *t = q = s;
    }
  }

  if (p != NULL)
  {
    if (p->avl.cache == -1)
    {
      p->avl.left = q;
    }
    else
    {
      p->avl.right = q;
    }
  }
  else
  {
    *root = q;
  }

  modified = true;

  while (modified)
  {
    if (buf_prev > buf_stack)
    {
      p = *--buf_prev;
    }
    else
    {
      break;
    }

    if (p->avl.cache == -1)
    {
      /* rebalance left branch */
      switch (p->avl.bal)
      {
        case -1:
          p->avl.bal = 0;
          break;
        case  0:
          p->avl.bal = 1;
          modified = false;
          break;

        case +1:
          p1 = p->avl.right;

          if (p1->avl.bal >= 0) /* simple RR-turn */
          {
            p->avl.right = p1->avl.left;
            p1->avl.left = p;

            if (p1->avl.bal == 0)
            {
              p1->avl.bal = -1;
              modified = false;
            }
            else
            {
              p->avl.bal = 0;
              p1->avl.bal = 0;
            }
            p = p1;
          }
          else /* double RL-turn */
          {
            p2 = p1->avl.left;

            p1->avl.left = p2->avl.right;
            p2->avl.right = p1;
            p->avl.right = p2->avl.left;
            p2->avl.left = p;

            if (p2->avl.bal == +1) p->avl.bal = -1; else p->avl.bal = 0;
            if (p2->avl.bal == -1) p1->avl.bal = 1; else p1->avl.bal = 0;

            p = p2;
            p2->avl.bal = 0;
          }
          break;

        default:
          break;
      }
    }
    else
    {
      /* rebalance right branch */
      switch (p->avl.bal)
      {
        case +1:
          p->avl.bal = 0;
          break;

        case  0:
          p->avl.bal = -1;
          modified = false;
          break;

        case -1:
          p1 = p->avl.left;

          if (p1->avl.bal <= 0) /* simple LL-turn */
          {
            p->avl.left = p1->avl.right;
            p1->avl.right = p;
            if (p1->avl.bal == 0)
            {
              p1->avl.bal = 1;
              modified = false;
            }
            else
            {
              p->avl.bal = 0;
              p1->avl.bal = 0;
            }
            p = p1;
          }
          else /* double LR-turn */
          {
            p2 = p1->avl.right;

            p1->avl.right = p2->avl.left;
            p2->avl.left = p1;
            p->avl.left = p2->avl.right;
            p2->avl.right = p;

            if (p2->avl.bal == -1) p->avl.bal = 1; else p->avl.bal = 0;
            if (p2->avl.bal == +1) p1->avl.bal = -1; else p1->avl.bal = 0;

            p = p2;
            p2->avl.bal = 0;
          }
          break;

        default:
          break;
      }
    }

    if (buf_prev > buf_stack)
    {
      q = *(buf_prev - 1);

      if (q->avl.cache == -1)
      {
        q->avl.left = p;
      }
      else
      {
        q->avl.right = p;
      }
    }
    else
    {
      *root = p;
      break;
    }

  }

  return 0;
}

/**
 * Get the pool for the device.
 *
 * @param pid Physical disk device.
 */
static rtems_bdbuf_pool*
rtems_bdbuf_get_pool (const rtems_bdpool_id pid)
{
  return &rtems_bdbuf_ctx.pool[pid];
}

/**
 * Lock the pool. A single task can nest calls.
 *
 * @param pool The pool to lock.
 */
static void
rtems_bdbuf_lock_pool (rtems_bdbuf_pool* pool)
{
  rtems_status_code sc = rtems_semaphore_obtain (pool->lock,
                                                 RTEMS_WAIT,
                                                 RTEMS_NO_TIMEOUT);
  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_POOL_LOCK);
}

/**
 * Unlock the pool.
 *
 * @param pool The pool to unlock.
 */
static void
rtems_bdbuf_unlock_pool (rtems_bdbuf_pool* pool)
{
  rtems_status_code sc = rtems_semaphore_release (pool->lock);
  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_POOL_UNLOCK);
}

/**
 * Lock the pool's sync. A single task can nest calls.
 *
 * @param pool The pool's sync to lock.
 */
static void
rtems_bdbuf_lock_sync (rtems_bdbuf_pool* pool)
{
  rtems_status_code sc = rtems_semaphore_obtain (pool->sync_lock,
                                                 RTEMS_WAIT,
                                                 RTEMS_NO_TIMEOUT);
  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SYNC_LOCK);
}

/**
 * Unlock the pool's sync.
 *
 * @param pool The pool's sync to unlock.
 */
static void
rtems_bdbuf_unlock_sync (rtems_bdbuf_pool* pool)
{
  rtems_status_code sc = rtems_semaphore_release (pool->sync_lock);
  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SYNC_UNLOCK);
}

/**
 * Wait until woken. Semaphores are used so a number of tasks can wait and can
 * be woken at once. Task events would require we maintain a list of tasks to
 * be woken and this would require storgage and we do not know the number of
 * tasks that could be waiting.
 *
 * While we have the pool locked we can try and claim the semaphore and
 * therefore know when we release the lock to the pool we will block until the
 * semaphore is released. This may even happen before we get to block.
 *
 * A counter is used to save the release call when no one is waiting.
 *
 * The function assumes the pool is locked on entry and it will be locked on
 * exit.
 *
 * @param pool The pool to wait for a buffer to return.
 * @param sema The semaphore to block on and wait.
 * @param waiters The wait counter for this semaphore.
 */
static void
rtems_bdbuf_wait (rtems_bdbuf_pool* pool, rtems_id* sema,
                  volatile uint32_t* waiters)
{
  rtems_status_code sc;
  rtems_mode        prev_mode;
  
  /*
   * Indicate we are waiting.
   */
  *waiters += 1;

  /*
   * Disable preemption then unlock the pool and block.
   * There is no POSIX condition variable in the core API so
   * this is a work around.
   *
   * The issue is a task could preempt after the pool is unlocked
   * because it is blocking or just hits that window, and before
   * this task has blocked on the semaphore. If the preempting task
   * flushes the queue this task will not see the flush and may
   * block for ever or until another transaction flushes this
   * semaphore.
   */
  sc = rtems_task_mode (RTEMS_NO_PREEMPT, RTEMS_PREEMPT_MASK, &prev_mode);

  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_POOL_WAIT);
  
  /*
   * Unlock the pool, wait, and lock the pool when we return.
   */
  rtems_bdbuf_unlock_pool (pool);

  sc = rtems_semaphore_obtain (*sema, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  
  if (sc != RTEMS_UNSATISFIED)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_POOL_WAIT);
  
  rtems_bdbuf_lock_pool (pool);

  sc = rtems_task_mode (prev_mode, RTEMS_ALL_MODE_MASKS, &prev_mode);

  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_POOL_WAIT);
  
  *waiters -= 1;
}

/**
 * Wake a blocked resource. The resource has a counter that lets us know if
 * there are any waiters.
 *
 * @param sema The semaphore to release.
 * @param waiters The wait counter for this semaphore.
 */
static void
rtems_bdbuf_wake (rtems_id sema, volatile uint32_t* waiters)
{
  if (*waiters)
  {
    rtems_status_code sc;

    sc = rtems_semaphore_flush (sema);
  
    if (sc != RTEMS_SUCCESSFUL)
      rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_POOL_WAKE);
  }
}

/**
 * Add a buffer descriptor to the modified list. This modified list is treated
 * a litte differently to the other lists. To access it you must have the pool
 * locked and this is assumed to be the case on entry to this call.
 *
 * If the pool has a device being sync'ed and the bd is for that device the
 * call must block and wait until the sync is over before adding the bd to the
 * modified list. Once a sync happens for a device no bd's can be added the
 * modified list. The disk image is forced to be snapshot at that moment in
 * time.
 *
 * and you must
 * hold the sync lock. The sync lock is used to block writes while a sync is
 * active.
 *
 * @param pool The pool the bd belongs to.
 * @param bd The bd to queue to the pool's modified list.
 */
static void
rtems_bdbuf_append_modified (rtems_bdbuf_pool* pool, rtems_bdbuf_buffer* bd)
{
  /*
   * If the pool has a device being sync'ed check if this bd is for that
   * device. If it is unlock the pool and block on the sync lock. once we have
   * the sync lock reelase it.
   *
   * If the 
   */
  if (pool->sync_active && (pool->sync_device == bd->dev))
  {
    rtems_bdbuf_unlock_pool (pool);
    rtems_bdbuf_lock_sync (pool);
    rtems_bdbuf_unlock_sync (pool);
    rtems_bdbuf_lock_pool (pool);
  }
      
  bd->state = RTEMS_BDBUF_STATE_MODIFIED;

  rtems_chain_append (&pool->modified, &bd->link);
}

/**
 * Wait the swapper task.
 */
static void
rtems_bdbuf_wake_swapper (void)
{
  rtems_status_code sc = rtems_event_send (rtems_bdbuf_ctx.swapout,
                                           RTEMS_BDBUF_SWAPOUT_SYNC);
  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_WAKE);
}

/**
 * Initialize single buffer pool.
 *
 * @param config Buffer pool configuration
 * @param pid Pool number
 *
 * @return RTEMS_SUCCESSFUL, if buffer pool initialized successfully, or error
 *         code if error occured.
 */
static rtems_status_code
rtems_bdbuf_initialize_pool (rtems_bdbuf_pool_config* config,
                             rtems_bdpool_id          pid)
{
  int                 rv = 0;
  unsigned char*      buffer = config->mem_area;
  rtems_bdbuf_pool*   pool;
  rtems_bdbuf_buffer* bd;
  rtems_status_code   sc;
  uint32_t            b;
  int                 cache_aligment = 32 /* FIXME rtems_cache_get_data_line_size() */;

  /* For unspecified cache alignments we use the CPU alignment */
  if (cache_aligment <= 0)
  {
    cache_aligment = CPU_ALIGNMENT;
  }

  pool = rtems_bdbuf_get_pool (pid);
  
  pool->blksize        = config->size;
  pool->nblks          = config->num;
  pool->flags          = 0;
  pool->sync_active    = false;
  pool->sync_device    = -1;
  pool->sync_requester = 0;
  pool->tree           = NULL;
  pool->buffers        = NULL;

  rtems_chain_initialize_empty (&pool->ready);
  rtems_chain_initialize_empty (&pool->lru);
  rtems_chain_initialize_empty (&pool->modified);
  rtems_chain_initialize_empty (&pool->sync);

  pool->access           = 0;
  pool->access_waiters   = 0;
  pool->transfer         = 0;
  pool->transfer_waiters = 0;
  pool->waiting          = 0;
  pool->wait_waiters     = 0;
  
  /*
   * Allocate memory for buffer descriptors
   */
  pool->bds = calloc (config->num, sizeof (rtems_bdbuf_buffer));
  
  if (!pool->bds)
    return RTEMS_NO_MEMORY;

  /*
   * Allocate memory for buffers if required.  The pool memory will be cache
   * aligned.  It is possible to free the memory allocated by rtems_memalign()
   * with free().
   */
  if (buffer == NULL)
  {
    rv = rtems_memalign ((void **) &buffer,
                         cache_aligment,
                         config->num * config->size);
    if (rv != 0)
    {
      free (pool->bds);
      return RTEMS_NO_MEMORY;
    }
    pool->buffers = buffer;
  }

  for (b = 0, bd = pool->bds;
       b < pool->nblks;
       b++, bd++, buffer += pool->blksize)
  {
    bd->dev        = -1;
    bd->block      = 0;
    bd->buffer     = buffer;
    bd->avl.left   = NULL;
    bd->avl.right  = NULL;
    bd->state      = RTEMS_BDBUF_STATE_EMPTY;
    bd->pool       = pid;
    bd->error      = 0;
    bd->waiters    = 0;
    bd->hold_timer = 0;
    
    rtems_chain_append (&pool->ready, &bd->link);
  }

  sc = rtems_semaphore_create (rtems_build_name ('B', 'P', '0' + pid, 'L'),
                               1, RTEMS_BDBUF_POOL_LOCK_ATTRIBS, 0,
                               &pool->lock);
  if (sc != RTEMS_SUCCESSFUL)
  {
    free (pool->buffers);
    free (pool->bds);
    return sc;
  }

  sc = rtems_semaphore_create (rtems_build_name ('B', 'P', '0' + pid, 'S'),
                               1, RTEMS_BDBUF_POOL_LOCK_ATTRIBS, 0,
                               &pool->sync_lock);
  if (sc != RTEMS_SUCCESSFUL)
  {
    rtems_semaphore_delete (pool->lock);
    free (pool->buffers);
    free (pool->bds);
    return sc;
  }
  
  sc = rtems_semaphore_create (rtems_build_name ('B', 'P', '0' + pid, 'a'),
                               0, RTEMS_BDBUF_POOL_WAITER_ATTRIBS, 0,
                               &pool->access);
  if (sc != RTEMS_SUCCESSFUL)
  {
    rtems_semaphore_delete (pool->sync_lock);
    rtems_semaphore_delete (pool->lock);
    free (pool->buffers);
    free (pool->bds);
    return sc;
  }

  sc = rtems_semaphore_create (rtems_build_name ('B', 'P', '0' + pid, 't'),
                               0, RTEMS_BDBUF_POOL_WAITER_ATTRIBS, 0,
                               &pool->transfer);
  if (sc != RTEMS_SUCCESSFUL)
  {
    rtems_semaphore_delete (pool->access);
    rtems_semaphore_delete (pool->sync_lock);
    rtems_semaphore_delete (pool->lock);
    free (pool->buffers);
    free (pool->bds);
    return sc;
  }

  sc = rtems_semaphore_create (rtems_build_name ('B', 'P', '0' + pid, 'w'),
                               0, RTEMS_BDBUF_POOL_WAITER_ATTRIBS, 0,
                               &pool->waiting);
  if (sc != RTEMS_SUCCESSFUL)
  {
    rtems_semaphore_delete (pool->transfer);
    rtems_semaphore_delete (pool->access);
    rtems_semaphore_delete (pool->sync_lock);
    rtems_semaphore_delete (pool->lock);
    free (pool->buffers);
    free (pool->bds);
    return sc;
  }

  return RTEMS_SUCCESSFUL;
}

/**
 * Free resources allocated for buffer pool with specified number.
 *
 * @param pid Buffer pool number
 *
 * @retval RTEMS_SUCCESSFUL
 */
static rtems_status_code
rtems_bdbuf_release_pool (rtems_bdpool_id pid)
{
  rtems_bdbuf_pool* pool = rtems_bdbuf_get_pool (pid);
  
  rtems_bdbuf_lock_pool (pool);

  rtems_semaphore_delete (pool->waiting);
  rtems_semaphore_delete (pool->transfer);
  rtems_semaphore_delete (pool->access);
  rtems_semaphore_delete (pool->lock);
  
  free (pool->buffers);
  free (pool->bds);
  
  return RTEMS_SUCCESSFUL;
}

rtems_status_code
rtems_bdbuf_init (void)
{
  rtems_bdpool_id   p;
  rtems_status_code sc;

#if RTEMS_BDBUF_TRACE
  rtems_bdbuf_printf ("init\n");
#endif

  if (rtems_bdbuf_pool_configuration_size <= 0)
    return RTEMS_INVALID_SIZE;

  if (rtems_bdbuf_ctx.npools)
    return RTEMS_RESOURCE_IN_USE;

  rtems_bdbuf_ctx.npools = rtems_bdbuf_pool_configuration_size;

  /*
   * Allocate memory for buffer pool descriptors
   */
  rtems_bdbuf_ctx.pool = calloc (rtems_bdbuf_pool_configuration_size,
                                 sizeof (rtems_bdbuf_pool));
  
  if (rtems_bdbuf_ctx.pool == NULL)
    return RTEMS_NO_MEMORY;

  /*
   * Initialize buffer pools and roll out if something failed,
   */
  for (p = 0; p < rtems_bdbuf_ctx.npools; p++)
  {
    sc = rtems_bdbuf_initialize_pool (&rtems_bdbuf_pool_configuration[p], p);
    if (sc != RTEMS_SUCCESSFUL)
    {
      rtems_bdpool_id j;
      for (j = 0; j < p - 1; j++)
        rtems_bdbuf_release_pool (j);
      return sc;
    }
  }

  /*
   * Create and start swapout task
   */

  rtems_bdbuf_ctx.swapout_enabled = true;
  
  sc = rtems_task_create (rtems_build_name('B', 'S', 'W', 'P'),
                          (rtems_bdbuf_configuration.swapout_priority ?
                           rtems_bdbuf_configuration.swapout_priority :
                           RTEMS_BDBUF_SWAPOUT_TASK_PRIORITY_DEFAULT),
                          SWAPOUT_TASK_STACK_SIZE,
                          RTEMS_PREEMPT | RTEMS_NO_TIMESLICE | RTEMS_NO_ASR,
                          RTEMS_LOCAL | RTEMS_NO_FLOATING_POINT,
                          &rtems_bdbuf_ctx.swapout);
  if (sc != RTEMS_SUCCESSFUL)
  {
    for (p = 0; p < rtems_bdbuf_ctx.npools; p++)
      rtems_bdbuf_release_pool (p);
    free (rtems_bdbuf_ctx.pool);
    return sc;
  }

  sc = rtems_task_start (rtems_bdbuf_ctx.swapout,
                         rtems_bdbuf_swapout_task,
                         (rtems_task_argument) &rtems_bdbuf_ctx);
  if (sc != RTEMS_SUCCESSFUL)
  {
    rtems_task_delete (rtems_bdbuf_ctx.swapout);
    for (p = 0; p < rtems_bdbuf_ctx.npools; p++)
      rtems_bdbuf_release_pool (p);
    free (rtems_bdbuf_ctx.pool);
    return sc;
  }

  return RTEMS_SUCCESSFUL;
}

/**
 * Get a buffer for this device and block. This function returns a buffer once
 * placed into the AVL tree. If no buffer is available and it is not a read
 * ahead request and no buffers are waiting to the written to disk wait until
 * one is available. If buffers are waiting to be written to disk and non are
 * available expire the hold timer and wake the swap out task. If the buffer is
 * for a read ahead transfer return NULL if there is not buffer or it is in the
 * cache.
 *
 * The AVL tree of buffers for the pool is searched and if not located check
 * obtain a buffer and insert it into the AVL tree. Buffers are first obtained
 * from the ready list until all empty/ready buffers are used. Once all buffers
 * are in use buffers are taken from the LRU list with the least recently used
 * buffer taken first. A buffer taken from the LRU list is removed from the AVL
 * tree. The ready list or LRU list buffer is initialised to this device and
 * block. If no buffers are available due to the ready and LRU lists being
 * empty a check is made of the modified list. Buffers may be queued waiting
 * for the hold timer to expire. These buffers should be written to disk and
 * returned to the LRU list where they can be used rather than this call
 * blocking. If buffers are on the modified list the max. write block size of
 * buffers have their hold timer expired and the swap out task woken. The
 * caller then blocks on the waiting semaphore and counter. When buffers return
 * from the upper layers (access) or lower driver (transfer) the blocked caller
 * task is woken and this procedure is repeated. The repeat handles a case of a
 * another thread pre-empting getting a buffer first and adding it to the AVL
 * tree.
 *
 * A buffer located in the AVL tree means it is already in the cache and maybe
 * in use somewhere. The buffer can be either:
 *
 * # Cached. Not being accessed or part of a media transfer.
 * # Access or modifed access. Is with an upper layer being accessed.
 * # Transfer. Is with the driver and part of a media transfer.
 *
 * If cached we assign the new state, extract it from any list it maybe part of
 * and return to the user.
 *
 * This function assumes the pool the buffer is being taken from is locked and
 * it will make sure the pool is locked when it returns. The pool will be
 * unlocked if the call could block.
 *
 * @param pdd The physical disk device
 * @param pool The pool reference
 * @param block Absolute media block number
 * @param read_ahead The get is for a read ahead buffer
 *
 * @return RTEMS status code ( if operation completed successfully or error
 *         code if error is occured)
 */
static rtems_bdbuf_buffer*
rtems_bdbuf_get_buffer (rtems_disk_device* pdd,
                        rtems_bdbuf_pool*  pool,
                        rtems_blkdev_bnum  block,
                        bool               read_ahead)
{
  dev_t               device = pdd->dev;
  rtems_bdbuf_buffer* bd;
  bool                available;

  /*
   * Loop until we get a buffer. Under load we could find no buffers are
   * available requiring this task to wait until some become available before
   * proceeding. There is no timeout. If the call is to block and the buffer is
   * for a read ahead buffer return NULL.
   *
   * The search procedure is repeated as another thread could have pre-empted
   * us while we waited for a buffer, obtained an empty buffer and loaded the
   * AVL tree with the one we are after.
   */
  do
  {
    /*
     * Search for buffer descriptor for this dev/block key.
     */
    bd = rtems_bdbuf_avl_search (&pool->tree, device, block);

    /*
     * No buffer in the cache for this block. We need to obtain a buffer and
     * this means take a buffer that is ready to use. If all buffers are in use
     * take the least recently used buffer. If there are none then the cache is
     * empty. All the buffers are either queued to be written to disk or with
     * the user. We cannot do much with the buffers with the user how-ever with
     * the modified buffers waiting to be written to disk flush the maximum
     * number transfered in a block to disk. After this all that can be done is
     * to wait for a buffer to return to the cache.
     */
    if (!bd)
    {
      /*
       * Assign new buffer descriptor from the empty list if one is present. If
       * the empty queue is empty get the oldest buffer from LRU list. If the
       * LRU list is empty there are no available buffers check the modified
       * list.
       */
      if (rtems_chain_is_empty (&pool->ready))
      {
        /*
         * No unsed or read-ahead buffers.
         *
         * If this is a read ahead buffer just return. No need to place further
         * pressure on the cache by reading something that may be needed when
         * we have data in the cache that was needed and may still be.
         */
        if (read_ahead)
          return NULL;

        /*
         * Check the LRU list.
         */
        bd = (rtems_bdbuf_buffer *) rtems_chain_get (&pool->lru);
        
        if (bd)
        {
          /*
           * Remove the buffer from the AVL tree.
           */
          if (rtems_bdbuf_avl_remove (&pool->tree, bd) != 0)
            rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY);
        }
        else
        {
          /*
           * If there are buffers on the modified list expire the hold timer
           * and wake the swap out task then wait else just go and wait.
           */
          if (!rtems_chain_is_empty (&pool->modified))
          {
            rtems_chain_node* node = rtems_chain_head (&pool->modified);
            uint32_t          write_blocks = 0;
            
            node = node->next;
            while ((write_blocks < rtems_bdbuf_configuration.max_write_blocks) &&
                   !rtems_chain_is_tail (&pool->modified, node))
            {
              rtems_bdbuf_buffer* bd = (rtems_bdbuf_buffer*) node;
              bd->hold_timer = 0;
              write_blocks++;
              node = node->next;
            }

            rtems_bdbuf_wake_swapper ();
          }
          
          /*
           * Wait for a buffer to be returned to the pool. The buffer will be
           * placed on the LRU list.
           */
          rtems_bdbuf_wait (pool, &pool->waiting, &pool->wait_waiters);
        }
      }
      else
      {
        bd = (rtems_bdbuf_buffer *) rtems_chain_get (&(pool->ready));

        if ((bd->state != RTEMS_BDBUF_STATE_EMPTY) &&
            (bd->state != RTEMS_BDBUF_STATE_READ_AHEAD))
          rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY);

        if (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD)
        {
          if (rtems_bdbuf_avl_remove (&pool->tree, bd) != 0)
            rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY);
        }
      }

      if (bd)
      {
        bd->dev       = device;
        bd->block     = block;
        bd->avl.left  = NULL;
        bd->avl.right = NULL;
        bd->state     = RTEMS_BDBUF_STATE_EMPTY;
        bd->error     = 0;
        bd->waiters   = 0;

        if (rtems_bdbuf_avl_insert (&pool->tree, bd) != 0)
          rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY);

        return bd;
      }
    }
  }
  while (!bd);

  /*
   * If the buffer is for read ahead and it exists in the AVL cache or is being
   * accessed or being transfered then return NULL.
   */
  if (read_ahead)
    return NULL;

  /*
   * Loop waiting for the buffer to enter the cached state. If the buffer is in
   * the access or transfer state then wait until it is not.
   */
  available = false;
  while (!available)
  {
    switch (bd->state)
    {
      case RTEMS_BDBUF_STATE_CACHED:
      case RTEMS_BDBUF_STATE_MODIFIED:
      case RTEMS_BDBUF_STATE_READ_AHEAD:
        available = true;
        break;

      case RTEMS_BDBUF_STATE_ACCESS:
      case RTEMS_BDBUF_STATE_ACCESS_MODIFIED:
        bd->waiters++;
        rtems_bdbuf_wait (pool, &pool->access, &pool->access_waiters);
        bd->waiters--;
        break;

      case RTEMS_BDBUF_STATE_SYNC:
      case RTEMS_BDBUF_STATE_TRANSFER:
        bd->waiters++;
        rtems_bdbuf_wait (pool, &pool->transfer, &pool->transfer_waiters);
        bd->waiters--;
        break;

      default:
        rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY);
    }
  }

  /*
   * Buffer is linked to the LRU, modifed, or sync lists. Remove it from there.
   */
  rtems_chain_extract (&bd->link);

  return bd;
}

rtems_status_code
rtems_bdbuf_get (dev_t                device,
                 rtems_blkdev_bnum    block,
                 rtems_bdbuf_buffer** bdp)
{
  rtems_disk_device*  dd;
  rtems_bdbuf_pool*   pool;
  rtems_bdbuf_buffer* bd;

  /*
   * Do not hold the pool lock when obtaining the disk table.
   */
  dd = rtems_disk_obtain (device);
  if (dd == NULL)
    return RTEMS_INVALID_ID;

  if (block >= dd->size)
  {
    rtems_disk_release (dd);
    return RTEMS_INVALID_NUMBER;
  }

  pool = rtems_bdbuf_get_pool (dd->phys_dev->pool);
  
  rtems_bdbuf_lock_pool (pool);

#if RTEMS_BDBUF_TRACE
  /* Print the block index relative to the physical disk */
  rtems_bdbuf_printf ("get: %d (dev = %08x)\n", block + dd->start, device);
#endif

  bd = rtems_bdbuf_get_buffer (dd->phys_dev, pool, block + dd->start, false);

  if (bd->state == RTEMS_BDBUF_STATE_MODIFIED)
    bd->state = RTEMS_BDBUF_STATE_ACCESS_MODIFIED;
  else
    bd->state = RTEMS_BDBUF_STATE_ACCESS;
  
  rtems_bdbuf_unlock_pool (pool);

  rtems_disk_release(dd);

  *bdp = bd;
  
  return RTEMS_SUCCESSFUL;
}

/**
 * Call back handler called by the low level driver when the transfer has
 * completed. This function may be invoked from interrupt handler.
 *
 * @param arg Arbitrary argument specified in block device request
 *            structure (in this case - pointer to the appropriate
 *            block device request structure).
 * @param status I/O completion status
 * @param error errno error code if status != RTEMS_SUCCESSFUL
 */
static void
rtems_bdbuf_read_done (void* arg, rtems_status_code status, int error)
{
  rtems_blkdev_request* req = (rtems_blkdev_request*) arg;

  req->error = error;
  req->status = status;

  rtems_event_send (req->io_task, RTEMS_BDBUF_TRANSFER_SYNC);
}

rtems_status_code
rtems_bdbuf_read (dev_t                device,
                  rtems_blkdev_bnum    block,
                  rtems_bdbuf_buffer** bdp)
{
  rtems_disk_device*    dd;
  rtems_bdbuf_pool*     pool;
  rtems_bdbuf_buffer*   bd = NULL;
  uint32_t              read_ahead_count;
  rtems_blkdev_request* req;
  
  /*
   * @todo This type of request structure is wrong and should be removed.
   */
#define bdbuf_alloc(size) __builtin_alloca (size)

  req = bdbuf_alloc (sizeof (rtems_blkdev_request) +
                     (sizeof ( rtems_blkdev_sg_buffer) *
                      rtems_bdbuf_configuration.max_read_ahead_blocks));

  /*
   * Do not hold the pool lock when obtaining the disk table.
   */
  dd = rtems_disk_obtain (device);
  if (dd == NULL)
    return RTEMS_INVALID_ID;
  
  if (block >= dd->size) {
    rtems_disk_release(dd);
    return RTEMS_INVALID_NUMBER;
  }
  
#if RTEMS_BDBUF_TRACE
  /* Print the block index relative to the physical disk */
  rtems_bdbuf_printf ("read: %d (dev = %08x)\n", block + dd->start, device);
#endif

  req->bufnum = 0;

  /*
   * Read the block plus the required number of blocks ahead. The number of
   * blocks to read ahead is configured by the user and limited by the size of
   * the disk or reaching a read ahead block that is also cached.
   *
   * Limit the blocks read by the size of the disk.
   */
  if ((rtems_bdbuf_configuration.max_read_ahead_blocks + block) < dd->size)
    read_ahead_count = rtems_bdbuf_configuration.max_read_ahead_blocks;
  else
    read_ahead_count = dd->size - block;

  pool = rtems_bdbuf_get_pool (dd->phys_dev->pool);

  rtems_bdbuf_lock_pool (pool);

  while (req->bufnum < read_ahead_count)
  {
    /*
     * Get the buffer for the requested block. If the block is cached then
     * return it. If it is not cached transfer the block from the disk media
     * into memory.
     *
     * We need to clean up any buffers allocated and not passed back to the
     * caller.
     */
    bd = rtems_bdbuf_get_buffer (dd->phys_dev, pool,
                                 block + dd->start + req->bufnum,
                                 req->bufnum == 0 ? false : true);

    /*
     * Read ahead buffer is in the cache or none available. Read what we
     * can.
     */
    if (!bd)
      break;

    /*
     * Is the block we are interested in the cache ?
     */
    if ((bd->state == RTEMS_BDBUF_STATE_CACHED) ||
        (bd->state == RTEMS_BDBUF_STATE_MODIFIED))
      break;

    bd->state = RTEMS_BDBUF_STATE_TRANSFER;
    bd->error = 0;

    /*
     * @todo The use of these req blocks is not a great design. The req is a
     *       struct with a single 'bufs' declared in the req struct and the
     *       others are added in the outer level struct. This relies on the
     *       structs joining as a single array and that assumes the compiler
     *       packs the structs. Why not just place on a list ? The BD has a
     *       node that can be used.
     */
    req->bufs[req->bufnum].user   = bd;
    req->bufs[req->bufnum].block  = bd->block;
    req->bufs[req->bufnum].length = dd->block_size;
    req->bufs[req->bufnum].buffer = bd->buffer;
    req->bufnum++;
  }

  /*
   * Transfer any requested buffers. If the request count is 0 we have found
   * the block in the cache so return it.
   */
  if (req->bufnum)
  {
    /*
     * Unlock the pool. We have the buffer for the block and it will be in the
     * access or transfer state. We may also have a number of read ahead blocks
     * if we need to transfer data. At this point any other threads can gain
     * access to the pool and if they are after any of the buffers we have they
     * will block and be woken when the buffer is returned to the pool.
     *
     * If a transfer is needed the I/O operation will occur with pre-emption
     * enabled and the pool unlocked. This is a change to the previous version
     * of the bdbuf code.
     */
    rtems_event_set out;
    int             result;
    uint32_t        b;

    /*
     * Flush any events.
     */
    rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC,
                         RTEMS_EVENT_ALL | RTEMS_NO_WAIT,
                         0, &out);
                         
    rtems_bdbuf_unlock_pool (pool);

    req->req = RTEMS_BLKDEV_REQ_READ;
    req->req_done = rtems_bdbuf_read_done;
    req->done_arg = req;
    req->io_task = rtems_task_self ();
    req->status = RTEMS_RESOURCE_IN_USE;
    req->error = 0;
  
    result = dd->ioctl (dd->phys_dev->dev, RTEMS_BLKIO_REQUEST, req);

    /*
     * Inspection of the DOS FS code shows the result from this function is
     * handled and a buffer must be returned.
     */
    if (result < 0)
    {
      req->error = errno;
      req->status = RTEMS_IO_ERROR;
    }
    else
    {
      rtems_status_code sc;
      
      sc = rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC,
                                RTEMS_EVENT_ALL | RTEMS_WAIT,
                                0, &out);

      if (sc != RTEMS_SUCCESSFUL)
        rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE);
    }

    rtems_bdbuf_lock_pool (pool);

    for (b = 1; b < req->bufnum; b++)
    {
      bd = req->bufs[b].user;
      bd->error = req->error;
      bd->state = RTEMS_BDBUF_STATE_READ_AHEAD;
      rtems_bdbuf_release (bd);
    }

    bd = req->bufs[0].user;
  }

  /*
   * The data for this block is cached in the buffer.
   */
  if (bd->state == RTEMS_BDBUF_STATE_MODIFIED)
    bd->state = RTEMS_BDBUF_STATE_ACCESS_MODIFIED;
  else
    bd->state = RTEMS_BDBUF_STATE_ACCESS;

  rtems_bdbuf_unlock_pool (pool);
  rtems_disk_release (dd);

  *bdp = bd;

  return RTEMS_SUCCESSFUL;
}

rtems_status_code
rtems_bdbuf_release (rtems_bdbuf_buffer* bd)
{
  rtems_bdbuf_pool* pool;

  if (bd == NULL)
    return RTEMS_INVALID_ADDRESS;

  pool = rtems_bdbuf_get_pool (bd->pool);

  rtems_bdbuf_lock_pool (pool);

#if RTEMS_BDBUF_TRACE
  rtems_bdbuf_printf ("release: %d\n", bd->block);
#endif
  
  if (bd->state == RTEMS_BDBUF_STATE_ACCESS_MODIFIED)
  {
    rtems_bdbuf_append_modified (pool, bd);
  }
  else
  {
    /*
     * If this is a read ahead buffer place the ready queue. Buffers are taken
     * from here first. If we prepend then get from the queue the buffers
     * furthermost from the read buffer will be used.
     */
    if (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD)
      rtems_chain_prepend (&pool->ready, &bd->link);
    else
    {
      bd->state = RTEMS_BDBUF_STATE_CACHED;
      rtems_chain_append (&pool->lru, &bd->link);
    }
  }
  
  /*
   * If there are threads waiting to access the buffer wake them. Wake any
   * waiters if this is the first buffer to placed back onto the queue.
   */
  if (bd->waiters)
    rtems_bdbuf_wake (pool->access, &pool->access_waiters);
  else
  {
    if (bd->state == RTEMS_BDBUF_STATE_READ_AHEAD)
    {
      if (rtems_chain_has_only_one_node (&pool->ready))
        rtems_bdbuf_wake (pool->waiting, &pool->wait_waiters);
    }
    else
    {
      if (rtems_chain_has_only_one_node (&pool->lru))
        rtems_bdbuf_wake (pool->waiting, &pool->wait_waiters);
    }
  }
  
  rtems_bdbuf_unlock_pool (pool);

  return RTEMS_SUCCESSFUL;
}

rtems_status_code
rtems_bdbuf_release_modified (rtems_bdbuf_buffer* bd)
{
  rtems_bdbuf_pool* pool;

  if (bd == NULL)
    return RTEMS_INVALID_ADDRESS;

  pool = rtems_bdbuf_get_pool (bd->pool);

  rtems_bdbuf_lock_pool (pool);

#if RTEMS_BDBUF_TRACE
  rtems_bdbuf_printf ("release modified: %d\n", bd->block);
#endif

  bd->hold_timer = rtems_bdbuf_configuration.swap_block_hold;
  
  rtems_bdbuf_append_modified (pool, bd);

  if (bd->waiters)
    rtems_bdbuf_wake (pool->access, &pool->access_waiters);
  
  rtems_bdbuf_unlock_pool (pool);

  return RTEMS_SUCCESSFUL;
}

rtems_status_code
rtems_bdbuf_sync (rtems_bdbuf_buffer* bd)
{
  rtems_bdbuf_pool* pool;
  bool              available;

#if RTEMS_BDBUF_TRACE
  rtems_bdbuf_printf ("sync: %d\n", bd->block);
#endif
  
  if (bd == NULL)
    return RTEMS_INVALID_ADDRESS;

  pool = rtems_bdbuf_get_pool (bd->pool);

  rtems_bdbuf_lock_pool (pool);

  bd->state = RTEMS_BDBUF_STATE_SYNC;

  rtems_chain_append (&pool->sync, &bd->link);

  rtems_bdbuf_wake_swapper ();

  available = false;
  while (!available)
  {
    switch (bd->state)
    {
      case RTEMS_BDBUF_STATE_CACHED:
      case RTEMS_BDBUF_STATE_READ_AHEAD:
      case RTEMS_BDBUF_STATE_MODIFIED:
      case RTEMS_BDBUF_STATE_ACCESS:
      case RTEMS_BDBUF_STATE_ACCESS_MODIFIED:
        available = true;
        break;

      case RTEMS_BDBUF_STATE_SYNC:
      case RTEMS_BDBUF_STATE_TRANSFER:
        bd->waiters++;
        rtems_bdbuf_wait (pool, &pool->transfer, &pool->transfer_waiters);
        bd->waiters--;
        break;

      default:
        rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_CONSISTENCY);
    }
  }

  rtems_bdbuf_unlock_pool (pool);
  
  return RTEMS_SUCCESSFUL;
}

rtems_status_code
rtems_bdbuf_syncdev (dev_t dev)
{
  rtems_disk_device*  dd;
  rtems_bdbuf_pool*   pool;
  rtems_status_code   sc;
  rtems_event_set     out;

#if RTEMS_BDBUF_TRACE
  rtems_bdbuf_printf ("syncdev: %08x\n", dev);
#endif

  /*
   * Do not hold the pool lock when obtaining the disk table.
   */
  dd = rtems_disk_obtain (dev);
  if (dd == NULL)
    return RTEMS_INVALID_ID;

  pool = rtems_bdbuf_get_pool (dd->pool);

  /*
   * Take the sync lock before locking the pool. Once we have the sync lock
   * we can lock the pool. If another thread has the sync lock it will cause
   * this thread to block until it owns the sync lock then it can own the
   * pool. The sync lock can only be obtained with the pool unlocked.
   */
  
  rtems_bdbuf_lock_sync (pool);
  rtems_bdbuf_lock_pool (pool);  

  /*
   * Set the pool to have a sync active for a specific device and let the swap
   * out task know the id of the requester to wake when done.
   *
   * The swap out task will negate the sync active flag when no more buffers
   * for the device are held on the modified for sync queues.
   */
  pool->sync_active    = true;
  pool->sync_requester = rtems_task_self ();
  pool->sync_device    = dev;
  
  rtems_bdbuf_wake_swapper ();
  rtems_bdbuf_unlock_pool (pool);
  
  sc = rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC,
                            RTEMS_EVENT_ALL | RTEMS_WAIT,
                            0, &out);

  if (sc != RTEMS_SUCCESSFUL)
    rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE);
      
  rtems_bdbuf_unlock_sync (pool);
  
  return rtems_disk_release(dd);
}

/**
 * Call back handler called by the low level driver when the transfer has
 * completed. This function may be invoked from interrupt handler.
 *
 * @param arg Arbitrary argument specified in block device request
 *            structure (in this case - pointer to the appropriate
 *            block device request structure).
 * @param status I/O completion status
 * @param error errno error code if status != RTEMS_SUCCESSFUL
 */
static void
rtems_bdbuf_write_done(void *arg, rtems_status_code status, int error)
{
  rtems_blkdev_request* req = (rtems_blkdev_request*) arg;

  req->error = error;
  req->status = status;

  rtems_event_send (req->io_task, RTEMS_BDBUF_TRANSFER_SYNC);
}

/**
 * Process the modified list of buffers. There us a sync or modified list that
 * needs to be handled.
 *
 * @param pid The pool id to process modified buffers on.
 * @param dev The device to handle. If -1 no device is selected so select the
 *            device of the first buffer to be written to disk.
 * @param chain The modified chain to process.
 * @param transfer The chain to append buffers to be written too.
 * @param sync_active If true this is a sync operation so expire all timers.
 * @param update_timers If true update the timers.
 * @param timer_delta It update_timers is true update the timers by this
 *                    amount.
 */
static void
rtems_bdbuf_swapout_modified_processing (rtems_bdpool_id      pid,
                                         dev_t*               dev,
                                         rtems_chain_control* chain,
                                         rtems_chain_control* transfer,
                                         bool                 sync_active,
                                         bool                 update_timers,
                                         uint32_t             timer_delta)
{
  if (!rtems_chain_is_empty (chain))
  {
    rtems_chain_node* node = rtems_chain_head (chain);
    node = node->next;

    while (!rtems_chain_is_tail (chain, node))
    {
      rtems_bdbuf_buffer* bd = (rtems_bdbuf_buffer*) node;
    
      if (bd->pool == pid)
      {
        /*
         * Check if the buffer's hold timer has reached 0. If a sync is active
         * force all the timers to 0.
         *
         * @note Lots of sync requests will skew this timer. It should be based
         *       on TOD to be accurate. Does it matter ?
         */
        if (sync_active)
          bd->hold_timer = 0;
  
        if (bd->hold_timer)
        {
          if (update_timers)
          {
            if (bd->hold_timer > timer_delta)
              bd->hold_timer -= timer_delta;
            else
              bd->hold_timer = 0;
          }

          if (bd->hold_timer)
          {
            node = node->next;
            continue;
          }
        }

        /*
         * This assumes we can set dev_t to -1 which is just an
         * assumption. Cannot use the transfer list being empty the sync dev
         * calls sets the dev to use.
         */
        if (*dev == (dev_t)-1)
          *dev = bd->dev;

        if (bd->dev == *dev)
        {
          rtems_chain_node* next_node = node->next;
          rtems_chain_node* tnode = rtems_chain_tail (transfer);
    
          /*
           * The blocks on the transfer list are sorted in block order. This
           * means multi-block transfers for drivers that require consecutive
           * blocks perform better with sorted blocks and for real disks it may
           * help lower head movement.
           */

          bd->state = RTEMS_BDBUF_STATE_TRANSFER;

          rtems_chain_extract (node);

          tnode = tnode->previous;
          
          while (node && !rtems_chain_is_head (transfer, tnode))
          {
            rtems_bdbuf_buffer* tbd = (rtems_bdbuf_buffer*) tnode;

            if (bd->block > tbd->block)
            {
              rtems_chain_insert (tnode, node);
              node = NULL;
            }
            else
              tnode = tnode->previous;
          }

          if (node)
            rtems_chain_prepend (transfer, node);
          
          node = next_node;
        }
        else
        {
          node = node->next;
        }
      }
    }
  }
}

/**
 * Process a pool's modified buffers. Check the sync list first then the
 * modified list extracting the buffers suitable to be written to disk. We have
 * a device at a time. The task level loop will repeat this operation while
 * there are buffers to be written. If the transfer fails place the buffers
 * back on the modified list and try again later. The pool is unlocked while
 * the buffers are beign written to disk.
 *
 * @param pid The pool id to process modified buffers on.
 * @param timer_delta It update_timers is true update the timers by this
 *                    amount.
 * @param update_timers If true update the timers.
 * @param write_req The write request structure. There is only one.
 *
 * @retval true Buffers where written to disk so scan again.
 * @retval false No buffers where written to disk.
 */
static bool
rtems_bdbuf_swapout_pool_processing (rtems_bdpool_id       pid,
                                     unsigned long         timer_delta,
                                     bool                  update_timers,
                                     rtems_blkdev_request* write_req)
{
  rtems_bdbuf_pool*   pool = rtems_bdbuf_get_pool (pid);
  rtems_chain_control transfer;
  dev_t               dev = -1;
  rtems_disk_device*  dd;
  bool                transfered_buffers = true;

  rtems_chain_initialize_empty (&transfer);
    
  rtems_bdbuf_lock_pool (pool);

  /*
   * When the sync is for a device limit the sync to that device. If the sync
   * is for a buffer handle the devices in the order on the sync list. This
   * means the dev is -1.
   */
  if (pool->sync_active)
    dev = pool->sync_device;

  /*
   * If we have any buffers in the sync queue move then to the modified
   * list. The first sync buffer will select the device we use.
   */
  rtems_bdbuf_swapout_modified_processing (pid, &dev,
                                           &pool->sync, &transfer,
                                           true, false,
                                           timer_delta);

  /*
   * Process the pool's modified list.
   */
  rtems_bdbuf_swapout_modified_processing (pid, &dev,
                                           &pool->modified, &transfer,
                                           pool->sync_active,
                                           update_timers,
                                           timer_delta);

  /*
   * We have all the buffers that have been modified for this device so
   * the pool can be unlocked because the state is set to TRANSFER.
   */

  rtems_bdbuf_unlock_pool (pool);

  /*
   * If there are buffers to transfer to the media tranfer them.
   */
  if (rtems_chain_is_empty (&transfer))
    transfered_buffers = false;
  else
  {
    /*
     * Obtain the disk device. Release the pool mutex to avoid a dead
     * lock.
     */
    dd = rtems_disk_obtain (dev);
    if (dd == NULL)
       transfered_buffers = false;
    else
    {
      /*
       * The last block number used when the driver only supports
       * continuous blocks in a single request.
       */
      uint32_t last_block = 0;
      
      /*
       * Take as many buffers as configured and pass to the driver. Note, the
       * API to the drivers has the array of buffers and if a chain was passed
       * we could have just passed the list. If the driver API is updated it
       * should be possible to make this change with little effect in this
       * code. The array that is passed is broken in design and should be
       * removed. Merging to members of a struct into the first member is
       * trouble waiting to happen.
       */

      write_req->status = RTEMS_RESOURCE_IN_USE;
      write_req->error = 0;
      write_req->bufnum = 0;

      while (!rtems_chain_is_empty (&transfer))
      {
        rtems_bdbuf_buffer* bd =
          (rtems_bdbuf_buffer*) rtems_chain_get (&transfer);

        bool write = false;
        
        /*
         * If the device only accepts sequential buffers and this is not the
         * first buffer (the first is always sequential, and the buffer is not
         * sequential then put the buffer back on the transfer chain and write
         * the committed buffers.
         */
        
        if ((dd->capabilities & RTEMS_BLKDEV_CAP_MULTISECTOR_CONT) &&
            write_req->bufnum &&
            (bd->block != (last_block + 1)))
        {
          rtems_chain_prepend (&transfer, &bd->link);
          write = true;
        }
        else
        {
          write_req->bufs[write_req->bufnum].user   = bd;
          write_req->bufs[write_req->bufnum].block  = bd->block;
          write_req->bufs[write_req->bufnum].length = dd->block_size;
          write_req->bufs[write_req->bufnum].buffer = bd->buffer;
          write_req->bufnum++;
          last_block = bd->block;
        }

        /*
         * Perform the transfer if there are no more buffers, or the transfer
         * size has reached the configured max. value.
         */

        if (rtems_chain_is_empty (&transfer) ||
            (write_req->bufnum >= rtems_bdbuf_configuration.max_write_blocks))
          write = true;

        if (write)
        {
          int result;
          uint32_t b;

          /*
           * Perform the transfer. No pool locks, no preemption, only the disk
           * device is being held.
           */
          result = dd->ioctl (dd->phys_dev->dev,
                              RTEMS_BLKIO_REQUEST, write_req);

          if (result < 0)
          {
            rtems_bdbuf_lock_pool (pool);
              
            for (b = 0; b < write_req->bufnum; b++)
            {
              bd = write_req->bufs[b].user;
              bd->state  = RTEMS_BDBUF_STATE_MODIFIED;
              bd->error = errno;

              /*
               * Place back on the pools modified queue and try again.
               *
               * @warning Not sure this is the best option but I do not know
               *          what else can be done.
               */
              rtems_chain_append (&pool->modified, &bd->link);
            }
          }
          else
          {
            rtems_status_code sc = 0;
            rtems_event_set   out;

            sc = rtems_event_receive (RTEMS_BDBUF_TRANSFER_SYNC,
                                      RTEMS_EVENT_ALL | RTEMS_WAIT,
                                      0, &out);

            if (sc != RTEMS_SUCCESSFUL)
              rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE);

            rtems_bdbuf_lock_pool (pool);

            for (b = 0; b < write_req->bufnum; b++)
            {
              bd = write_req->bufs[b].user;
              bd->state = RTEMS_BDBUF_STATE_CACHED;
              bd->error = 0;

              rtems_chain_append (&pool->lru, &bd->link);
              
              if (bd->waiters)
                rtems_bdbuf_wake (pool->transfer, &pool->transfer_waiters);
              else
              {
                if (rtems_chain_has_only_one_node (&pool->lru))
                  rtems_bdbuf_wake (pool->waiting, &pool->wait_waiters);
              }
            }
          }

          rtems_bdbuf_unlock_pool (pool);

          write_req->status = RTEMS_RESOURCE_IN_USE;
          write_req->error = 0;
          write_req->bufnum = 0;
        }
      }
          
      rtems_disk_release (dd);
    }
  }

  if (pool->sync_active && !  transfered_buffers)
  {
    rtems_id sync_requester = pool->sync_requester;
    pool->sync_active = false;
    pool->sync_requester = 0;
    if (sync_requester)
      rtems_event_send (sync_requester, RTEMS_BDBUF_TRANSFER_SYNC);
  }
  
  return  transfered_buffers;
}

/**
 * Body of task which takes care on flushing modified buffers to the disk.
 *
 * @param arg The task argument which is the context.
 */
static rtems_task
rtems_bdbuf_swapout_task (rtems_task_argument arg)
{
  rtems_bdbuf_context*  context = (rtems_bdbuf_context*) arg;
  rtems_blkdev_request* write_req;
  uint32_t              period_in_ticks;
  const uint32_t        period_in_msecs = rtems_bdbuf_configuration.swapout_period;
  uint32_t              timer_delta;
  rtems_status_code     sc;

  /*
   * @note chrisj The rtems_blkdev_request and the array at the end is a hack.
   * I am disappointment at finding code like this in RTEMS. The request should
   * have been a rtems_chain_control. Simple, fast and less storage as the node
   * is already part of the buffer structure.
   */
  write_req =
    malloc (sizeof (rtems_blkdev_request) +
            (rtems_bdbuf_configuration.max_write_blocks *
             sizeof (rtems_blkdev_sg_buffer)));

  if (!write_req)
    rtems_fatal_error_occurred (RTEMS_BLKDEV_FATAL_BDBUF_SO_NOMEM);

  write_req->req = RTEMS_BLKDEV_REQ_WRITE;
  write_req->req_done = rtems_bdbuf_write_done;
  write_req->done_arg = write_req;
  write_req->io_task = rtems_task_self ();

  period_in_ticks = RTEMS_MICROSECONDS_TO_TICKS (period_in_msecs * 1000);

  /*
   * This is temporary. Needs to be changed to use the real time clock.
   */
  timer_delta = period_in_msecs;

  while (context->swapout_enabled)
  {
    rtems_event_set out;

    /*
     * Only update the timers once in the processing cycle.
     */
    bool update_timers = true;
    
    /*
     * If we write buffers to any disk perform a check again. We only write a
     * single device at a time and a pool may have more than one devices
     * buffers modified waiting to be written.
     */
    bool transfered_buffers;

    do
    {
      rtems_bdpool_id pid;
    
      transfered_buffers = false;

      /*
       * Loop over each pool extacting all the buffers we find for a specific
       * device. The device is the first one we find on a modified list of a
       * pool. Process the sync queue of buffers first.
       */
      for (pid = 0; pid < context->npools; pid++)
      {
        if (rtems_bdbuf_swapout_pool_processing (pid,
                                                 timer_delta,
                                                 update_timers,
                                                 write_req))
        {
          transfered_buffers = true;
        }
      }

      /*
       * Only update the timers once.
       */
      update_timers = false;
    }
    while (transfered_buffers);

    sc = rtems_event_receive (RTEMS_BDBUF_SWAPOUT_SYNC,
                              RTEMS_EVENT_ALL | RTEMS_WAIT,
                              period_in_ticks,
                              &out);

    if ((sc != RTEMS_SUCCESSFUL) && (sc != RTEMS_TIMEOUT))
      rtems_fatal_error_occurred (BLKDEV_FATAL_BDBUF_SWAPOUT_RE);
  }

  free (write_req);

  rtems_task_delete (RTEMS_SELF);
}

rtems_status_code
rtems_bdbuf_find_pool (uint32_t block_size, rtems_bdpool_id *pool)
{
  rtems_bdbuf_pool* p;
  rtems_bdpool_id   i;
  rtems_bdpool_id   curid = -1;
  bool              found = false;
  uint32_t          cursize = UINT_MAX;
  int               j;

  for (j = block_size; (j != 0) && ((j & 1) == 0); j >>= 1);
  if (j != 1)
    return RTEMS_INVALID_SIZE;

  for (i = 0; i < rtems_bdbuf_ctx.npools; i++)
  {
    p = rtems_bdbuf_get_pool (i);
    if ((p->blksize >= block_size) &&
        (p->blksize < cursize))
    {
      curid = i;
      cursize = p->blksize;
      found = true;
    }
  }

  if (found)
  {
    if (pool != NULL)
      *pool = curid;
    return RTEMS_SUCCESSFUL;
  }
  else
  {
    return RTEMS_NOT_DEFINED;
  }
}

rtems_status_code rtems_bdbuf_get_pool_info(
  rtems_bdpool_id pool,
  uint32_t *block_size,
  uint32_t *blocks
)
{
  if (pool >= rtems_bdbuf_ctx.npools)
    return RTEMS_INVALID_NUMBER;

  if (block_size != NULL)
  {
    *block_size = rtems_bdbuf_ctx.pool[pool].blksize;
  }

  if (blocks != NULL)
  {
    *blocks = rtems_bdbuf_ctx.pool[pool].nblks;
  }

  return RTEMS_SUCCESSFUL;
}