annotate gsm-fw/services/ffs/reclaim.c @ 213:ef7d7da61c56

FFS code integration: remaining C files imported, but not yet integrated
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 06 Jan 2014 04:20:29 +0000
parents
children 0eb85790c0ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
213
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /******************************************************************************
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * Flash File System (ffs)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 *
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 * FFS core reclaim functionality
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 *
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 * $Id: reclaim.c 1.4.1.28 Thu, 08 Jan 2004 15:05:23 +0100 tsj $
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 *
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 ******************************************************************************/
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #ifndef TARGET
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include "ffs.cfg"
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15 #include "ffs/ffs.h"
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 #include "ffs/board/core.h"
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 #include "ffs/board/drv.h"
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 #include "ffs/board/ffstrace.h"
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 #include <stdlib.h> // rand()
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 /******************************************************************************
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 * Inodes Reclaim
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 ******************************************************************************/
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 void inodes_recurse(iref_t i)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 iref_t pi;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 struct inode_s *ip, *newip;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 tw(tr(TR_BEGIN, TrReclaimLow, "inodes_recurse(%d) {\n", i));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 ip = inode_addr(i);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 newip = (struct inode_s *) offset2addr(dev.binfo[fs.newinodes].offset) + i;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 // copy inode dir to new block, except child, sibling and copied
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 ffsdrv.write((uint32*) &newip->location, (uint32*) &ip->location, sizeof(location_t));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 ffsdrv.write_halfword((uint16*) &newip->size, ip->size);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 ffsdrv_write_byte (&newip->flags, ip->flags);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 ffsdrv.write_halfword((uint16*) &newip->sequence, ip->sequence);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 ffsdrv.write_halfword((uint16*) &newip->updates, ip->updates);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 bstat[fs.newinodes].used++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 // if no children of this dir, we have no more work to do
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 if (ip->child == (iref_t) IREF_NULL) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 tw(tr(TR_END, TrReclaimLow, "}\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 return;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 pi = -i;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 i = ip->child;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 ip = inode_addr(i);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 do {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 tw(tr(TR_FUNC, TrReclaimLow, "pi = %d, i = %d", pi, i));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 tw(tr(TR_NULL, TrReclaimLow, ", size = %d, location = 0x%x", ip->size,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 ip->location));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 tw(tr(TR_NULL, TrReclaimLow, ", name_addr = 0x%x",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 addr2name(offset2addr(location2offset(ip->location)))));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 if (is_object(ip, OT_SEGMENT))
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 tw(tr(TR_NULL, TrReclaimLow, ", (segment)\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 tw(tr(TR_NULL, TrReclaimLow, ", '%s'\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 (ip->size ? addr2name(offset2addr(location2offset(ip->location)))
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 : "(cleaned)")));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 if (is_object_valid(ip))
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 if (is_object(ip, OT_DIR)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 tw(tr(TR_NULL, TrReclaimLow, "recursing...\n", i));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 inodes_recurse(i);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 tw(tr(TR_NULL, TrReclaimLow, "copying...\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 // copy inode to new block, except child, sibling and copied
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 newip = (struct inode_s *)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 offset2addr(dev.binfo[fs.newinodes].offset) + i;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 ffsdrv.write((uint32*) &newip->location, (uint32*) &ip->location, sizeof(location_t));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 ffsdrv.write_halfword((uint16*) &newip->size, ip->size);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 ffsdrv_write_byte (&newip->flags, ip->flags);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85 ffsdrv.write_halfword((uint16*) &newip->sequence, ip->sequence);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 ffsdrv.write_halfword((uint16*) &newip->updates, ip->updates);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 bstat[fs.newinodes].used++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90 tw(tr(TR_FUNC, TrReclaimLow, "Linking: %d->%d\n",pi, i));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 // now write the child or sibling link of previous inode
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 newip = (struct inode_s *)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 offset2addr(dev.binfo[fs.newinodes].offset);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 if (pi > 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 ffsdrv.write_halfword((uint16*) &(newip + pi)->sibling, i);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 ffsdrv.write_halfword((uint16*) &(newip + (-pi))->child, i);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 pi = i; // save index of previous inode
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
100
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
101 if (ip->child != (iref_t) IREF_NULL && is_object(ip, OT_FILE)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
102 iref_t pis, is;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
103 struct inode_s *ips;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
104 pis = i;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
105 ips = ip;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
106
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
107 tw(tr(TR_FUNC, TrReclaimLow, "Follow segment head\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
108 // While child is valid
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
109 while ((is = ips->child) != (iref_t) IREF_NULL) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
110
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
111 // Get child
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
112 is = ips->child;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
113 ips = inode_addr(is);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
114 tw(tr(TR_FUNC, TrReclaimLow, "Child ok, got new child i = %d\n", is));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
115 // While object not is valid
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
116 while (!is_object_valid(ips)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
117 tw(tr(TR_FUNC, TrReclaimLow, "pi = %d, i = %d c(cleaned)\n", pis, is));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
118 // If sibling are valid
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
119 if (ips->sibling != (iref_t) IREF_NULL) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
120 // Get sibling
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
121 is = ips->sibling;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
122 ips = inode_addr(is);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
123 tw(tr(TR_FUNC, TrReclaimLow, "Sibling ok, got new sibling i = %d\n", is));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
124 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
125 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
126 tw(tr(TR_FUNC, TrReclaimLow, "Sibling = FF (%d)\n", ips->sibling));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
127 break; // Nothing more todo, child and sibling = FF
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
128 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
129 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
130 // If object is valid
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
131 if (is_object_valid(ips)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
132 tw(tr(TR_NULL, TrReclaimLow, "copying...\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
133 // copy inode to new block, except child, sibling and copied
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
134 newip = (struct inode_s *)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
135 offset2addr(dev.binfo[fs.newinodes].offset) + is;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
136 ffsdrv.write((uint32*) &newip->location, (uint32*) &ips->location, sizeof(location_t));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
137 ffsdrv.write_halfword((uint16*) &newip->size, ips->size);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
138 ffsdrv_write_byte (&newip->flags, ips->flags);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
139 ffsdrv.write_halfword((uint16*) &newip->sequence, ips->sequence);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
140 ffsdrv.write_halfword((uint16*) &newip->updates, ips->updates);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
141 bstat[fs.newinodes].used++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
142
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
143 tw(tr(TR_FUNC, TrReclaimLow, "Linking child: %d->%d\n",pis, is));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
144 // now write the child link of previous inode
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
145 newip = (struct inode_s *)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
146 offset2addr(dev.binfo[fs.newinodes].offset);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
147 ffsdrv.write_halfword((uint16*) &(newip + (pis))->child, is);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
148
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
149 pis = is; // save index of previous inode
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
150
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
151 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
152 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
153 tw(tr(TR_FUNC, TrReclaimLow, "Sibling = FF (%d, %d)\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
154 ips->sibling, ips->child));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
155 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
156
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
157 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
158 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
159 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
160 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
161 tw(tr(TR_NULL, TrReclaimLow, "(ignoring)\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
162 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
163 i = ip->sibling;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
164 ip = inode_addr(i);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
165
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
166 } while (i != (iref_t) IREF_NULL);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
167
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
168 tw(tr(TR_END, TrReclaimLow, "}\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
169 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
170
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
171 // Reclaim inodes, eg. move inodes to another block and erase old one.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
172 effs_t inodes_reclaim(void)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
173 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
174 tw(tr(TR_BEGIN, TrIReclaim, "inodes_reclaim() {\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
175 ttw(str(TTrRec, "irec{"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
176
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
177 if (fs.initerror != EFFS_OK) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
178 tw(tr(TR_END, TrIReclaim, "} %d\n", fs.initerror));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
179 ttw(ttr(TTrRec, "} %d" NL, fs.initerror));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
180 return fs.initerror;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
181 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
182
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
183 if ((fs.newinodes = block_alloc(1, BF_COPYING)) < 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
184 tw(tr(TR_END, TrIReclaim, "} %d\n", EFFS_NOBLOCKS));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
185 ttw(ttr(TTrRec, "} %d" NL, EFFS_NOBLOCKS));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
186 return EFFS_NOBLOCKS;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
187 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
188
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
189 statistics_update_irec(bstat[fs.inodes].used - bstat[fs.inodes].lost,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
190 bstat[fs.inodes].lost);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
191
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
192 // copy all inodes...
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
193 bstat[fs.newinodes].used = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
194 inodes_recurse(fs.root);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
195
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
196 block_commit();
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
197
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
198 tw(tr(TR_END, TrIReclaim, "} 0\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
199 ttw(str(TTrRec, "} 0" NL));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
200
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
201 return EFFS_OK;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
202 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
203
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
204 #if (FFS_TEST == 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
205 #define BLOCK_COMMIT_TEST(testcase, text)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
206 #else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
207 #if (TARGET == 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
208 // NOTEME: We have compressed the macro code because it will NOT compile on
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
209 // Unix otherwise. So until we find out why, we use this as a work-around.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
210 #define BLOCK_COMMIT_TEST(testcase, text) if (fs.testflags == testcase) { tw(tr(TR_FUNC, TrData, "} (" text ")\n")); return; }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
211 #else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
212 #define BLOCK_COMMIT_TEST(testcase, text) if (fs.testflags == testcase) { ttw(ttr(TTrData, "} (" text ")\n")); return; }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
213 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
214 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
215
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
216 // Inode -> Lost, Copying -> Inode, Lost -> Free
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
217 void block_commit(void)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
218 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
219 int oldinodes = fs.inodes;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
220
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
221 tw(tr(TR_BEGIN, TrIReclaim, "block_commit(%d -> %d) {\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
222 oldinodes, fs.newinodes));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
223 ttw(ttr(TTrRec, "block_commit(%d -> %d) {\n" NL,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
224 oldinodes, fs.newinodes));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
225
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
226 BLOCK_COMMIT_TEST(BLOCK_COMMIT_BEFORE, "Oops before commit");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
227
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
228 block_flags_write(oldinodes, BF_LOST);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
229
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
230 BLOCK_COMMIT_TEST(BLOCK_COMMIT_NO_VALID, "Oops no valid inode block");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
231
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
232 // Validate new block as an inodes block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
233 block_flags_write(fs.newinodes, BF_INODES);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
234
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
235 bstat[fs.newinodes].lost = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
236 bstat[fs.newinodes].objects = 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
237 inodes_set(fs.newinodes);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
238
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
239 // Free old inodes block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
240 block_free(oldinodes);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
241
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
242 BLOCK_COMMIT_TEST(BLOCK_COMMIT_OLD_FREE, "Oops after freeing old block");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
243
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
244 BLOCK_COMMIT_TEST(BLOCK_COMMIT_AFTER, "Oops after commit");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
245
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
246 ttw(str(TTrRec, "} 0" NL));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
247 tw(tr(TR_END, TrIReclaim, "}\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
248 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
249
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
250
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
251 /******************************************************************************
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
252 * Data Reclaim
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
253 ******************************************************************************/
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
254
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
255 // Important note: We must NOT perform a data reclaim when we are in the
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
256 // process of creating the journal file!
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
257
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
258 // Reclaim a data block, eg. move files to other blocks and erase old one.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
259 // When the reclaim is done, we must completely delete the old inodes which
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
260 // are pointing into the old data sector which is going to be erased now.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
261 iref_t data_reclaim(int space)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
262 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
263 iref_t error;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
264
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
265 tw(tr(TR_BEGIN, TrDReclaim, "data_reclaim(%d) {\n", space));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
266
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
267 if (fs.initerror != EFFS_OK) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
268 tw(tr(TR_END, TrDReclaim, "} %d\n", fs.initerror));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
269 return fs.initerror;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
270 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
271
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
272 error = data_reclaim_try(space);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
273
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
274 tw(tr(TR_END, TrDReclaim, "} (data_reclaim) %d\n", error));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
275
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
276 return error;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
277 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
278
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
279 int dage_max_reached(int dage_blk, int agegain)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
280 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
281 int reclaim, early, log2, mask;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
282
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
283 tw(tr(TR_BEGIN, TrDReclaim, "young(%d, %d) {\n", dage_blk, agegain));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
284
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
285 // Simple algorithm
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
286 reclaim = (dage_blk + agegain - 2 * FFS_DAGE_MAX >= 0);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
287
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
288 // Early exponential probability based reclaim
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
289 early = FFS_DAGE_MAX - dage_blk;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
290 if (agegain > dage_blk - 4 && 0 < early && early <= FFS_DAGE_EARLY_WIDTH) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
291 if (early < 4)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
292 early = 2;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
293 if (early < FFS_DAGE_EARLY_WIDTH) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
294 // Now make an exponential probability distributon by
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
295 // generating a bitmask of a size relative to (dage_blk
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
296 // - DAGE_EARLY_WIDTH)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
297 log2 = -1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
298 while (early > 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
299 early >>= 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
300 log2++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
301 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
302 reclaim = log2;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
303
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
304 mask = (1 << (log2 + 1)) - 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
305 reclaim = ((rand() & mask) == 0);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
306 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
307 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
308
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
309 // Do not perform a reclaim unless we gain a certain minimum
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
310 if (agegain < FFS_DAGE_GAIN_MIN)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
311 reclaim = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
312
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
313 tw(tr(TR_END, TrDReclaim, "} (%d)\n", reclaim));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
314 return reclaim;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
315 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
316
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
317
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
318 // Try to reclaim at least <space> bytes of data space. On success, return
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
319 // the number of bytes actually reclaimed. Otherwise, on failure, return a
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
320 // (negative) error.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
321 int data_reclaim_try(int space)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
322 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
323 // 1. Find a suitable block to reclaim.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
324 //
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
325 // 2. Relocate each valid object from old block (to another block). An
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
326 // object relocation is similar to a normal file update, e.g. similar to
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
327 // fupdate().
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
328 //
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
329 // 3. If there is not enough space to relocate a file, we must alloc a
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
330 // new block then data_format() it.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
331 //
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
332 // 4. set BF_CLEANING flag of old block.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
333 //
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
334 // 5. ALL inodes (also invalid an erased ones) referring into reclaimed
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
335 // block must now be totally wiped out.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
336 //
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
337 // 6. Free (invalidate) old block.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
338
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
339 int result = 0, reserved_ok = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
340 bref_t b, blocks_free;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
341 bref_t brc_young_b, brc_lost_b, brc_unused_b;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
342
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
343 blocksize_t brc_lost_lost, brc_lost_unused;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
344 blocksize_t brc_unused_unused;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
345 blocksize_t unused, unused_total, lost, lost_total, free;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
346
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
347 age_t brc_young_dage, free_dage, dage;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
348 struct block_header_s *bhp;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
349 // Note gain can be negative if the free block is younger than the youngest data block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
350 int age_gain;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
351
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
352 tw(tr(TR_BEGIN, TrDReclaim, "data_reclaim_try(%d) {\n", space));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
353 ttw(str(TTrRec, "drec{" NL));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
354
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
355 // While searching for a block to reclaim, we maintain three block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
356 // reclaim candidates (brc): One with the maximum number of lost bytes,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
357 // one with the maximum number of unused bytes and another for the
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
358 // youngest block, e.g. the one with the largest age distance to
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
359 // fs.age_max. The candidates are tried in the order mentioned.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
360
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
361 // This counts free blocks, so we initialize to number of blocks minus
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
362 // one for inodes.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
363 blocks_free = dev.numblocks - 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
364
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
365 // Initialize Block Reclaim Candidate (brc) variables
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
366 brc_lost_b = -1; brc_lost_unused = 0; brc_lost_lost = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
367 brc_unused_b = -1; brc_unused_unused = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
368
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
369 brc_young_b = -1; brc_young_dage = 0; free_dage = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
370
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
371 lost_total = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
372 unused_total = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
373
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
374 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
375 "blk unused lost w/age age dist objs\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
376 for (b = 0; b < dev.numblocks; b++)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
377 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
378 bhp = (struct block_header_s *) offset2addr(dev.binfo[b].offset);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
379
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
380 if (is_block(b, BF_IS_DATA))
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
381 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
382 // Record number of lost bytes and number of unused bytes,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
383 // eg. total space that would be freed if this block was
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
384 // reclaimed
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
385 lost = bstat[b].lost;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
386 unused = dev.blocksize - (bstat[b].used - bstat[b].lost);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
387 free = dev.blocksize - bstat[b].used;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
388
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
389 lost_total += lost;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
390 unused_total += unused;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
391
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
392 if (free >= RESERVED_LOW)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
393 reserved_ok = 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
394 if (lost > brc_lost_lost) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
395 brc_lost_b = b;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
396 brc_lost_lost = lost;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
397 brc_lost_unused = unused;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
398 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
399 if (unused > brc_unused_unused) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
400 brc_unused_b = b;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
401 brc_unused_unused = unused;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
402 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
403
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
404 tw(tr(TR_FUNC, TrDReclaim, "%3d %7d %7d ", b, unused, lost));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
405
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
406 dage = saturate_dage(fs.age_max - bhp->age);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
407
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
408 tw(tr(TR_NULL, TrDReclaim, "%6d %5d %4d %3d\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
409 lost, bhp->age, dage, bstat[b].objects));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
410
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
411 if (dage >= brc_young_dage) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
412 brc_young_b = b;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
413 brc_young_dage = dage;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
414 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
415 blocks_free--;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
416 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
417 else if (is_block(b, BF_IS_FREE)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
418 unused_total += dev.blocksize;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
419
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
420 // Find youngest free block (in must cases we will only have one free b)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
421 dage = saturate_dage(fs.age_max - bhp->age);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
422
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
423 if (dage >= free_dage)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
424 free_dage = dage; // Delta age of youngest free block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
425 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
426 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
427 tw(tr(TR_FUNC, TrDReclaim, "sum %7d %7d\n", unused_total, lost_total));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
428 tw(tr(TR_FUNC, TrDReclaim, "blocks_free = %d, fs.age_max = %d\n", blocks_free, fs.age_max));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
429
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
430 age_gain = brc_young_dage - free_dage; // Same as free - block age
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
431
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
432 if (space > unused_total) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
433 // We will never be able to reclaim this amount...
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
434 result = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
435 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
436 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
437 // No additional blocks (apart from spare block) are free...
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
438 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
439 "brc_young_dage = %d, brc_lost_unused = %d, brc_unused_unused = %d\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
440 brc_young_dage, brc_lost_unused, brc_unused_unused));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
441
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
442 if (reserved_ok == 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
443 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
444 "No reserved, reclaim most-lost block (%d)\n", brc_unused_b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
445 result = data_block_reclaim(brc_lost_b, MOST_LOST);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
446 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
447 else if (dage_max_reached(brc_young_dage, age_gain) > 0 ) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
448 tw(tr(TR_FUNC, TrDReclaim, "Reclaiming youngest block (%d)\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
449 brc_young_b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
450 result = data_block_reclaim(brc_young_b, YOUNGEST);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
451 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
452 else if (brc_lost_unused >= space) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
453 tw(tr(TR_FUNC, TrDReclaim, "Reclaiming most-lost block (%d)\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
454 brc_lost_b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
455 result = data_block_reclaim(brc_lost_b, MOST_LOST);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
456 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
457 else if (brc_unused_unused >= space) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
458 tw(tr(TR_FUNC, TrDReclaim, "Reclaiming most-unused block (%d)\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
459 brc_unused_b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
460 result = data_block_reclaim(brc_unused_b, MOST_UNUSED);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
461 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
462 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
463 tw(tr(TR_FUNC, TrDReclaim, "Reclaiming most-lost blockx (%d)\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
464 brc_lost_b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
465 result = data_block_reclaim(brc_lost_b, MOST_LOST);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
466 if (result >= 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
467 result = 0; // We reclaimed a block but we still need more space
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
468 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
469
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
470 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
471 tw(tr(TR_END, TrDReclaim, "} (data_reclaim_try) %d\n", result));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
472
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
473 return result;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
474 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
475
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
476
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
477 #if (FFS_TEST == 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
478 #define BLOCK_RECLAIM_TEST(testcase, text)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
479 #else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
480 #if (TARGET == 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
481 // NOTEME: We have compressed the macro code because it will NOT compile on
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
482 // Unix otherwise. So until we find out why, we use this as a work-around.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
483 #define BLOCK_RECLAIM_TEST(testcase, text) if (fs.testflags == testcase) { tw(tr(TR_FUNC, TrTestHigh, "(" text ")\n")); tw(tr(TR_END, TrDReclaim, "} (Test) -100\n", result));return -100; }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
484 #else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
485 #define BLOCK_RECLAIM_TEST(testcase, text) if (fs.testflags == testcase) { ttw(ttr(TTrData, "} (" text ")"NL)); ttw(ttr(TTrRec, "} (Test) -100" NL));return -100; }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
486 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
487 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
488
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
489 #if (FFS_TEST == 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
490 #define BLOCK_RECOVER_TEST_INIT(testcase, text)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
491 #define BLOCK_RECOVER_TEST(testcase, text)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
492 #else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
493 #if (TARGET == 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
494 #define BLOCK_RECOVER_TEST_INIT(testcase, text) int rand_object; if (fs.testflags == testcase) { rand_object = rand() % bstat[b].objects; tw(tr(TR_FUNC, TrTestHigh, "Fail when object nr %d is relocated\n", rand_object)); }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
495
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
496 #define BLOCK_RECOVER_TEST(testcase, text) if (fs.testflags == testcase) {if (rand_object == n) { tw(tr(TR_FUNC, TrTestHigh, "(" text ")\n")); tw(tr(TR_END, TrDReclaim, "} (Test) -101\n", result)); return -101; } }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
497
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
498 #else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
499 #define BLOCK_RECOVER_TEST_INIT(testcase, text) int rand_object; if (fs.testflags == testcase) { rand_object = rand() % bstat[b].objects; ttw(ttr(TTrData, "Fail when object nr %d is relocated" NL, rand_object)); }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
500 #define BLOCK_RECOVER_TEST(testcase, text) if (fs.testflags == testcase) {if (rand_object == n) { ttw(ttr(TTrData, "(" text ")" NL)); ttw(ttr(TTrRec, "} (Test) -101" NL, result)); return -101; } }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
501 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
502 #endif
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
503
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
504 iref_t data_block_reclaim(bref_t b, int candidate)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
505 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
506 iref_t i, n, j;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
507 blocksize_t used_old, lost_old;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
508 int org_res_space, result = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
509 iref_t org_block_files_reserved;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
510 offset_t lower, upper;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
511 struct inode_s *ip;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
512 static int is_reclaim_running = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
513
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
514 tw(tr(TR_BEGIN, TrDReclaim, "data_block_reclaim(%d) {\n", b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
515
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
516 // In case of no free blocks (after sudden power off) or if the file
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
517 // system is near full we risk to be reentered (infinity recursively
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
518 // loop) and we can not allow that, so just return.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
519 if (is_reclaim_running == 1) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
520 tw(tr(TR_END, TrDReclaim, "} (reenteret skip reclaim) 0\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
521 return EFFS_RECLAIMLOOP;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
522 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
523
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
524 is_reclaim_running = 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
525
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
526 // If there are more objects in this block than there are remaining
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
527 // free inodes, we have to make an inodes_reclaim() first.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
528 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
529 "block_objects, fs.inodes_max, inodes: used, free\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
530 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
531 "%10d, %13d, %15d, %4d\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
532 bstat[b].objects,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
533 fs.inodes_max, bstat[fs.inodes].used,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
534 fs.inodes_max - (bstat[fs.inodes].used + bstat[fs.inodes].lost)));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
535
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
536 if (bstat[b].objects >= (fs.inodes_max - (bstat[fs.inodes].used +
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
537 bstat[fs.inodes].lost +
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
538 FFS_INODES_MARGIN))) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
539 tw(tr(TR_FUNC, TrInode, "NOTE: Will run out of free inodes...\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
540 inodes_reclaim();
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
541 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
542
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
543 // Allocate a new block. NOTE: we don't return an error because if we
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
544 // get in the situation where we don't have any free blocks this is the
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
545 // only way to recover.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
546 if ((result = block_alloc(1, BF_DATA)) < 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
547 tw(tr(TR_FUNC, TrAll, "WARNING: block_alloc failed\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
548 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
549
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
550 BLOCK_RECLAIM_TEST(BLOCK_RECLAIM_ALLOC, "Oops after ffs_block_alloc()");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
551
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
552 // If there are any objects at all to reclaim...
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
553 if (bstat[b].objects > 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
554 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
555 BLOCK_RECOVER_TEST_INIT(BLOCK_RECOVER_OBJECTS, "Dummy")
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
556 // Save the current journal state
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
557 if (journal_push() != EFFS_OK) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
558 is_reclaim_running = 0; // NOTEME: change to goto?
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
559 return EFFS_CORRUPTED;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
560 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
561
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
562 // We simulate that this block is completely full, such that we
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
563 // don't relocate files to the end of the block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
564 used_old = bstat[b].used;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
565 lost_old = bstat[b].lost; // For statistics
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
566 bstat[b].used = dev.blocksize - 1;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
567
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
568
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
569 // Compute lower (inclusive) and upper (exclusive) bounds of the
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
570 // location of files in this block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
571 lower = offset2location(dev.binfo[b].offset);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
572 upper = offset2location(dev.binfo[b].offset + dev.blocksize);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
573
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
574 tw(tr(TR_FUNC, TrDReclaim, "Block addr range = 0x%X..0x%X\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
575 location2offset(lower), location2offset(upper)));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
576
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
577 // This is the only time we are allowed to use the reserved
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
578 org_block_files_reserved= fs.block_files_reserved;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
579 fs.block_files_reserved = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
580
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
581 org_res_space = fs.reserved_space;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
582 fs.reserved_space = RESERVED_NONE;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
583
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
584 ip = inode_addr(1);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
585 for (i = 1, n = 0; i < fs.inodes_max; i++, ip++)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
586 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
587 BLOCK_RECOVER_TEST(BLOCK_RECOVER_OBJECTS, "Oops before relocate all objects");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
588 // Ensure object is valid and within the block to be reclaimed
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
589 if (is_object_valid(ip) &&
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
590 lower <= ip->location && ip->location < upper)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
591 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
592 if ((result = object_relocate(i)) < 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
593 tw(tr(TR_FUNC, TrAll, "FATAL object_relocate failed\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
594 break;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
595 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
596
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
597 // If we reclaim a segment head or wch that is in use we must
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
598 // update the file descriptor as well
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
599 for (j = 0; j < fs.fd_max; j++) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
600 if (i == fs.fd[j].seghead) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
601 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
602 "Updated seghead %d -> %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
603 fs.fd[j].seghead, result));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
604 fs.fd[j].seghead = result;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
605 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
606 if (i == fs.fd[j].wch) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
607 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
608 "Updated wch %d -> %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
609 fs.fd[j].wch, result));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
610 fs.fd[j].wch = result;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
611 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
612 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
613
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
614 // If we have just reclaimed an object which we started on
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
615 // updating we must also update ojournal
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
616 if (i == fs.ojournal.oldi) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
617 struct inode_s *ip = inode_addr(result);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
618 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
619 "Updated ojournal oldi %d -> %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
620 fs.ojournal.oldi, result));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
621 fs.ojournal.oldi = result;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
622 fs.ojournal.location = ip->location;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
623 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
624
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
625 if (i == fs.ojournal.diri || i == -fs.ojournal.diri) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
626 fs.ojournal.diri = (fs.ojournal.diri < 0 ? -result : result);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
627 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
628 "Updated ojournal: diri %d -> %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
629 i, fs.ojournal.diri));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
630 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
631
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
632 if (i == fs.ojournal.repli || i == -fs.ojournal.repli) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
633 fs.ojournal.repli = (fs.ojournal.repli < 0 ? -result : result);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
634 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
635 "Updated ojournal: repli %d -> %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
636 i, fs.ojournal.repli));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
637 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
638
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
639 if (i == fs.i_backup || i == -fs.i_backup) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
640 fs.i_backup = (fs.i_backup < 0 ? -result : result);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
641 tw(tr(TR_FUNC, TrDReclaim,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
642 "Updated i_backup: %d -> %d \n", i, fs.i_backup));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
643 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
644
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
645 n++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
646 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
647 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
648
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
649 fs.block_files_reserved = org_block_files_reserved; // Restore
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
650 fs.reserved_space = org_res_space;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
651
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
652 tw(tr(TR_FUNC, TrDReclaim, "Reclaimed %d objects\n", n));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
653 if (result >= 0)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
654 result = n; // We return number of objects relocated
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
655
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
656 if (i < fs.inodes_max) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
657 // We did not finish, so restore the old bstat[].used of the block.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
658 bstat[b].used = used_old;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
659 tw(tr(TR_FUNC, TrAll,
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
660 "WARNING: data_block_reclaim() not completed\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
661 result = EFFS_DBR;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
662 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
663
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
664 // Restore the saved journal state
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
665 if (journal_pop() != EFFS_OK) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
666 is_reclaim_running = 0; // NOTEME: change to goto?
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
667 return EFFS_CORRUPTED;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
668 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
669 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
670 BLOCK_RECLAIM_TEST(BLOCK_RECLAIM_NO_CLEAN, "Oops before clean old data block");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
671
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
672 if (result >= 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
673 // Clean the block (remove all inodes that refer to this block)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
674 block_flags_write(b, BF_CLEANING);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
675 block_clean(b);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
676
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
677 statistics_update_drec(used_old - lost_old, lost_old, candidate);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
678
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
679 BLOCK_RECLAIM_TEST(BLOCK_RECLAIM_CLEANING, "Oops before free old data block");
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
680
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
681 // Free the old block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
682 block_free(b);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
683 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
684
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
685 is_reclaim_running = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
686
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
687 tw(tr(TR_END, TrDReclaim, "} (data_block_reclaim) %d\n", result));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
688 ttw(ttr(TTrRec, "} %d" NL, result));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
689
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
690 return result;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
691 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
692
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
693 // Relocate object represented by inode reference <i>.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
694 iref_t object_relocate(iref_t oldi)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
695 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
696 iref_t newi;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
697 struct inode_s *oldip;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
698 char *olddata, *oldname;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
699 int oldsize;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
700
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
701 tw(tr(TR_BEGIN, TrReclaimLow, "object_relocate(%d) {\n", oldi));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
702
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
703 journal_begin(oldi);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
704
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
705 oldip = inode_addr(oldi);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
706
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
707 oldsize = segment_datasize(oldip);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
708 olddata = offset2addr(location2offset(oldip->location));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
709 oldname = addr2name(olddata);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
710 olddata = addr2data(olddata, oldip);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
711
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
712 if (is_object(oldip, OT_SEGMENT))
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
713 newi = segment_create(olddata, oldsize, -oldi);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
714 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
715 // root inode is a special case
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
716 if (*oldname == '/')
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
717 newi = object_create(oldname, olddata, oldsize, 0);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
718 else
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
719 newi = object_create(oldname, olddata, oldsize, oldi);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
720 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
721
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
722 if (newi < 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
723 tw(tr(TR_END, TrReclaimLow, "} %d\n", newi));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
724 return newi;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
725 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
726
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
727 // root inode is a special case
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
728 if ((*oldname == '/') && !is_object(oldip, OT_SEGMENT)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
729 tw(tr(TR_FUNC, TrDReclaim, "Relocating fs.root: %d->%d\n", oldi, newi));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
730 fs.root = newi;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
731 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
732
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
733 journal_end(0);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
734
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
735 tw(tr(TR_END, TrReclaimLow, "} %d\n", newi));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
736
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
737 return newi;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
738 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
739
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
740 // Clean a block, eg. erase all inodes that refer to this block.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
741 iref_t block_clean(bref_t b)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
742 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
743 iref_t i, n;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
744 struct inode_s *ip;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
745 offset_t lower, upper;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
746
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
747 tw(tr(TR_FUNC, TrDReclaim, "block_clean(%d) { ", b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
748
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
749 // Compute lower (inclusive) and upper (exclusive) bounds of the
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
750 // location of files in this block
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
751 lower = offset2location(dev.binfo[b].offset);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
752 upper = offset2location(dev.binfo[b].offset + dev.blocksize);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
753
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
754 tw(tr(TR_FUNC, TrDReclaim, "offset range = 0x%X..0x%X: ", lower, upper));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
755
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
756 ip = inode_addr(1);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
757 for (i = 1, n = 0; i < fs.inodes_max; i++, ip++)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
758 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
759 // Ensure object is within the block to be reclaimed. Note: if ffs
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
760 // is conf. with 1MB or above will all not used inodes default have
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
761 // the location to FFFF which will trigger a clean and make a error!
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
762 if (lower <= ip->location && upper > ip->location)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
763 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
764 tw(tr(TR_NULL, TrReclaimLow, "%d ", i));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
765 // Set the size to zero so it won't be counted in ffs_initialize()
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
766 ffsdrv.write_halfword((uint16 *) &ip->size, 0);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
767 n++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
768 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
769 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
770 tw(tr(TR_NULL, TrDReclaim, "} %d\n", n));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
771
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
772 return n;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
773 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
774
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
775
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
776 /******************************************************************************
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
777 * Main and block reclaim
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
778 ******************************************************************************/
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
779
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
780 // Reclaim (erase) all blocks that are marked as invalid/reclaimable. Each
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
781 // time a block is erased, its age is incremented so as to support wear
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
782 // levelling. Also, the global age limits are updated. FIXME: Should we
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
783 // avoid having ffs_initialize() do a block_reclaim() because it delays reboot?.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
784 int blocks_reclaim(void)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
785 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
786 bref_t b, n, b_lost_space;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
787 int blocks_free = 0, lost_space;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
788
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
789 int free_space, b_free_space;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
790
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
791 tw(tr(TR_BEGIN, TrBlock, "blocks_reclaim() {\n"));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
792 ttw(str(TTrRec, "blocks_reclaim() {" NL));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
793
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
794 // Testing of fs.testflags is for the sake of testing block_commit()
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
795 if ((fs.testflags & BLOCK_COMMIT_BASE) != 0) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
796 tw(tr(TR_FUNC, TrBlock, "Bailing out because fs.testflags = 0x%X\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
797 fs.testflags));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
798 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
799 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
800 for (b = 0, n = 0; b < dev.numblocks; b++) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
801 if (is_block_flag(b, BF_LOST)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
802 block_reclaim(b);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
803 n++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
804 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
805 if (is_block(b, BF_IS_FREE)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
806 blocks_free++;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
807 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
808 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
809 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
810
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
811 // If the number of free blocks is less than fs.blocks_free_min we
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
812 // call data_block_reclaim(). We will reclaim the block with most lost
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
813 // space. This should only happend if we got a sudden power off/reset
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
814 // while we reclaimed a block.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
815 if (blocks_free < fs.blocks_free_min) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
816 lost_space = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
817 free_space = 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
818
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
819 // We most never reclaim the block with most free space because this
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
820 // is the only block we can relocate the objects to.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
821 for (b = 0; b < dev.numblocks; b++) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
822 if (is_block_flag(b, BF_DATA)) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
823 if ((dev.blocksize - bstat[b].used) > free_space) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
824 free_space = dev.blocksize - bstat[b].used;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
825 b_free_space = b;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
826 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
827 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
828 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
829 tw(tr(TR_FUNC, TrBlock, "most free space: %d in block: %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
830 free_space, b_free_space));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
831
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
832 for (b = 0; b < dev.numblocks; b++) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
833 if (is_block_flag(b, BF_DATA) && b != b_free_space) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
834 if (bstat[b].lost > lost_space) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
835 lost_space = bstat[b].lost;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
836 b_lost_space = b;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
837 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
838 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
839 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
840 tw(tr(TR_FUNC, TrBlock, "most lost space: %d in block: %d \n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
841 lost_space, b_lost_space));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
842
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
843 data_block_reclaim(b_lost_space, MOST_LOST);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
844 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
845 tw(tr(TR_END, TrBlock, "} %d\n", n));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
846 ttw(ttr(TTrRec, "} %d" NL, n));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
847
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
848 return n;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
849 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
850
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
851 int block_reclaim(bref_t b)
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
852 {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
853 age_t age;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
854 struct block_header_s *bhp;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
855
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
856 tw(tr(TR_BEGIN, TrBlock, "block_reclaim(%d) {\n", b));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
857
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
858 // In ffs_initialize() we set fs.initerror = EFFS_INVALID while we call
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
859 // blocks_fsck(). We test for that condition now, in order to avoid
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
860 // doing sector erases that will delay the whole target boot process.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
861 if (fs.initerror == EFFS_INVALID) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
862 tw(tr(TR_END, TrBlock, "} %d\n", fs.initerror));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
863 return fs.initerror;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
864 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
865
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
866 // Testing of fs.testflags is for the sake of testing block_commit()
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
867 if ((fs.testflags & BLOCK_COMMIT_BASE) != 0 &&
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
868 fs.testflags != BLOCK_COMMIT_OLD_FREE) {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
869 tw(tr(TR_FUNC, TrBlock, "Bailing out because fs.testflags = 0x%X\n",
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
870 fs.testflags));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
871 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
872 else {
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
873 // We must read block's age before we erase it.
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
874 bhp = (struct block_header_s *) offset2addr(dev.binfo[b].offset);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
875 age = bhp->age;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
876 ffsdrv.erase(b);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
877 block_preformat(b, age);
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
878 }
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
879
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
880 tw(tr(TR_END, TrBlock, "} %d\n", 0));
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
881
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
882 return 0;
ef7d7da61c56 FFS code integration: remaining C files imported, but not yet integrated
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
883 }