Rev 2881 | Rev 2889 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2881 | Rev 2884 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jakub Jermar |
2 | * Copyright (c) 2008 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup fs |
29 | /** @addtogroup fs |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file fat_idx.c |
34 | * @file fat_idx.c |
35 | * @brief Layer for translating FAT entities to VFS node indices. |
35 | * @brief Layer for translating FAT entities to VFS node indices. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat.h" |
38 | #include "fat.h" |
39 | #include "../../vfs/vfs.h" |
39 | #include "../../vfs/vfs.h" |
40 | #include <errno.h> |
40 | #include <errno.h> |
41 | #include <string.h> |
41 | #include <string.h> |
42 | #include <libadt/hash_table.h> |
42 | #include <libadt/hash_table.h> |
43 | #include <libadt/list.h> |
43 | #include <libadt/list.h> |
44 | #include <assert.h> |
44 | #include <assert.h> |
45 | #include <futex.h> |
45 | #include <futex.h> |
46 | 46 | ||
47 | /** Each instance of this type describes one interval of freed VFS indices. */ |
47 | /** Each instance of this type describes one interval of freed VFS indices. */ |
48 | typedef struct { |
48 | typedef struct { |
49 | link_t link; |
49 | link_t link; |
50 | fs_index_t first; |
50 | fs_index_t first; |
51 | fs_index_t last; |
51 | fs_index_t last; |
52 | } freed_t; |
52 | } freed_t; |
53 | 53 | ||
54 | /** |
54 | /** |
55 | * Each instance of this type describes state of all VFS indices that |
55 | * Each instance of this type describes state of all VFS indices that |
56 | * are currently unused. |
56 | * are currently unused. |
57 | */ |
57 | */ |
58 | typedef struct { |
58 | typedef struct { |
59 | link_t link; |
59 | link_t link; |
60 | dev_handle_t dev_handle; |
60 | dev_handle_t dev_handle; |
61 | 61 | ||
62 | /** Next unassigned index. */ |
62 | /** Next unassigned index. */ |
63 | fs_index_t next; |
63 | fs_index_t next; |
64 | /** Number of remaining unassigned indices. */ |
64 | /** Number of remaining unassigned indices. */ |
65 | uint64_t remaining; |
65 | uint64_t remaining; |
66 | 66 | ||
67 | /** Sorted list of intervals of freed indices. */ |
67 | /** Sorted list of intervals of freed indices. */ |
68 | link_t freed_head; |
68 | link_t freed_head; |
69 | } unused_t; |
69 | } unused_t; |
70 | 70 | ||
71 | static LIST_INITIALIZE(unused_head); |
71 | static LIST_INITIALIZE(unused_head); |
72 | 72 | ||
73 | /** Allocate a VFS index which is not currently in use. */ |
73 | /** Allocate a VFS index which is not currently in use. */ |
74 | static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index) |
74 | static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index) |
75 | { |
75 | { |
76 | link_t *l; |
76 | link_t *l; |
77 | unused_t *u; |
77 | unused_t *u; |
78 | 78 | ||
79 | assert(index); |
79 | assert(index); |
80 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
80 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
81 | u = list_get_instance(l, unused_t, link); |
81 | u = list_get_instance(l, unused_t, link); |
82 | if (u->dev_handle == dev_handle) |
82 | if (u->dev_handle == dev_handle) |
83 | goto hit; |
83 | goto hit; |
84 | } |
84 | } |
85 | 85 | ||
86 | /* dev_handle not found */ |
86 | /* dev_handle not found */ |
87 | return false; |
87 | return false; |
88 | 88 | ||
89 | hit: |
89 | hit: |
90 | if (list_empty(&u->freed_head)) { |
90 | if (list_empty(&u->freed_head)) { |
91 | if (u->remaining) { |
91 | if (u->remaining) { |
92 | /* |
92 | /* |
93 | * There are no freed indices, allocate one directly |
93 | * There are no freed indices, allocate one directly |
94 | * from the counter. |
94 | * from the counter. |
95 | */ |
95 | */ |
96 | *index = u->next++; |
96 | *index = u->next++; |
97 | --u->remaining; |
97 | --u->remaining; |
98 | return true; |
98 | return true; |
99 | } |
99 | } |
100 | } else { |
100 | } else { |
101 | /* There are some freed indices which we can reuse. */ |
101 | /* There are some freed indices which we can reuse. */ |
102 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
102 | freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
103 | link); |
103 | link); |
104 | *index = f->first; |
104 | *index = f->first; |
105 | if (f->first++ == f->last) { |
105 | if (f->first++ == f->last) { |
106 | /* Destroy the interval. */ |
106 | /* Destroy the interval. */ |
107 | list_remove(&f->link); |
107 | list_remove(&f->link); |
108 | free(f); |
108 | free(f); |
109 | } |
109 | } |
110 | return true; |
110 | return true; |
111 | } |
111 | } |
112 | /* |
112 | /* |
113 | * We ran out of indices, which is extremely unlikely with FAT16, but |
113 | * We ran out of indices, which is extremely unlikely with FAT16, but |
114 | * theoretically still possible (e.g. too many open unlinked nodes or |
114 | * theoretically still possible (e.g. too many open unlinked nodes or |
115 | * too many zero-sized nodes). |
115 | * too many zero-sized nodes). |
116 | */ |
116 | */ |
117 | return false; |
117 | return false; |
118 | } |
118 | } |
119 | 119 | ||
120 | /** If possible, merge two intervals of freed indices. */ |
120 | /** If possible, coalesce two intervals of freed indices. */ |
121 | static void try_merge_intervals(link_t *l, link_t *r, link_t *cur) |
121 | static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
122 | { |
122 | { |
123 | freed_t *fl = list_get_instance(l, freed_t, link); |
123 | freed_t *fl = list_get_instance(l, freed_t, link); |
124 | freed_t *fr = list_get_instance(r, freed_t, link); |
124 | freed_t *fr = list_get_instance(r, freed_t, link); |
125 | 125 | ||
126 | if (fl->last + 1 == fr->first) { |
126 | if (fl->last + 1 == fr->first) { |
127 | if (cur == l) { |
127 | if (cur == l) { |
128 | fl->last = fr->last; |
128 | fl->last = fr->last; |
129 | list_remove(r); |
129 | list_remove(r); |
130 | free(r); |
130 | free(r); |
131 | } else { |
131 | } else { |
132 | fr->first = fl->first; |
132 | fr->first = fl->first; |
133 | list_remove(l); |
133 | list_remove(l); |
134 | free(l); |
134 | free(l); |
135 | } |
135 | } |
136 | } |
136 | } |
137 | } |
137 | } |
138 | 138 | ||
139 | /** Free a VFS index, which is no longer in use. */ |
139 | /** Free a VFS index, which is no longer in use. */ |
140 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
140 | static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index) |
141 | { |
141 | { |
142 | link_t *l; |
142 | link_t *l; |
143 | unused_t *u; |
143 | unused_t *u; |
144 | 144 | ||
145 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
145 | for (l = unused_head.next; l != &unused_head; l = l->next) { |
146 | u = list_get_instance(l, unused_t, link); |
146 | u = list_get_instance(l, unused_t, link); |
147 | if (u->dev_handle == dev_handle) |
147 | if (u->dev_handle == dev_handle) |
148 | goto hit; |
148 | goto hit; |
149 | } |
149 | } |
150 | 150 | ||
151 | /* should not happen */ |
151 | /* should not happen */ |
152 | assert(0); |
152 | assert(0); |
153 | 153 | ||
154 | hit: |
154 | hit: |
155 | if (u->next == index + 1) { |
155 | if (u->next == index + 1) { |
156 | /* The index can be returned directly to the counter. */ |
156 | /* The index can be returned directly to the counter. */ |
157 | u->next--; |
157 | u->next--; |
158 | u->remaining++; |
158 | u->remaining++; |
159 | return; |
159 | return; |
160 | } else { |
160 | } else { |
161 | /* |
161 | /* |
162 | * The index must be returned either to an existing freed |
162 | * The index must be returned either to an existing freed |
163 | * interval or a new interval must be created. |
163 | * interval or a new interval must be created. |
164 | */ |
164 | */ |
165 | link_t *lnk; |
165 | link_t *lnk; |
166 | freed_t *n; |
166 | freed_t *n; |
167 | for (lnk = u->freed_head.next; lnk != &u->freed_head; |
167 | for (lnk = u->freed_head.next; lnk != &u->freed_head; |
168 | lnk = lnk->next) { |
168 | lnk = lnk->next) { |
169 | freed_t *f = list_get_instance(lnk, freed_t, link); |
169 | freed_t *f = list_get_instance(lnk, freed_t, link); |
170 | if (f->first == index + 1) { |
170 | if (f->first == index + 1) { |
171 | f->first--; |
171 | f->first--; |
172 | if (lnk->prev != &u->freed_head) |
172 | if (lnk->prev != &u->freed_head) |
173 | try_merge_intervals(lnk->prev, lnk, |
173 | try_coalesce_intervals(lnk->prev, lnk, |
174 | lnk); |
174 | lnk); |
175 | return; |
175 | return; |
176 | } |
176 | } |
177 | if (f->last == index - 1) { |
177 | if (f->last == index - 1) { |
178 | f->last++; |
178 | f->last++; |
179 | if (lnk->next != &u->freed_head) |
179 | if (lnk->next != &u->freed_head) |
180 | try_merge_intervals(lnk, lnk->next, |
180 | try_coalesce_intervals(lnk, lnk->next, |
181 | lnk); |
181 | lnk); |
182 | return; |
182 | return; |
183 | } |
183 | } |
184 | if (index > f->first) { |
184 | if (index > f->first) { |
185 | n = malloc(sizeof(freed_t)); |
185 | n = malloc(sizeof(freed_t)); |
186 | /* TODO: sleep until allocation succeeds */ |
186 | /* TODO: sleep until allocation succeeds */ |
187 | assert(n); |
187 | assert(n); |
188 | link_initialize(&n->link); |
188 | link_initialize(&n->link); |
189 | n->first = index; |
189 | n->first = index; |
190 | n->last = index; |
190 | n->last = index; |
191 | list_insert_before(&n->link, lnk); |
191 | list_insert_before(&n->link, lnk); |
192 | return; |
192 | return; |
193 | } |
193 | } |
194 | 194 | ||
195 | } |
195 | } |
196 | /* The index will form the last interval. */ |
196 | /* The index will form the last interval. */ |
197 | n = malloc(sizeof(freed_t)); |
197 | n = malloc(sizeof(freed_t)); |
198 | /* TODO: sleep until allocation succeeds */ |
198 | /* TODO: sleep until allocation succeeds */ |
199 | assert(n); |
199 | assert(n); |
200 | link_initialize(&n->link); |
200 | link_initialize(&n->link); |
201 | n->first = index; |
201 | n->first = index; |
202 | n->last = index; |
202 | n->last = index; |
203 | list_append(&n->link, &u->freed_head); |
203 | list_append(&n->link, &u->freed_head); |
204 | } |
204 | } |
205 | } |
205 | } |
206 | 206 | ||
207 | fat_idx_t *fat_idx_map(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
207 | fat_idx_t *fat_idx_map(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
208 | { |
208 | { |
209 | return NULL; /* TODO */ |
209 | return NULL; /* TODO */ |
210 | } |
210 | } |
211 | 211 |