Rev 3571 | Rev 3573 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3571 | Rev 3572 | ||
---|---|---|---|
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_fat.c |
34 | * @file fat_fat.c |
35 | * @brief Functions that manipulate the File Allocation Tables. |
35 | * @brief Functions that manipulate the File Allocation Tables. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include "fat_fat.h" |
38 | #include "fat_fat.h" |
39 | #include "fat_dentry.h" |
39 | #include "fat_dentry.h" |
40 | #include "fat.h" |
40 | #include "fat.h" |
41 | #include "../../vfs/vfs.h" |
41 | #include "../../vfs/vfs.h" |
42 | #include <libfs.h> |
42 | #include <libfs.h> |
43 | #include <libblock.h> |
43 | #include <libblock.h> |
44 | #include <errno.h> |
44 | #include <errno.h> |
45 | #include <byteorder.h> |
45 | #include <byteorder.h> |
46 | #include <align.h> |
46 | #include <align.h> |
47 | #include <assert.h> |
47 | #include <assert.h> |
48 | #include <futex.h> |
48 | #include <futex.h> |
49 | 49 | ||
50 | /** |
50 | /** |
51 | * The fat_alloc_lock futex protects all copies of the File Allocation Table |
51 | * The fat_alloc_lock futex protects all copies of the File Allocation Table |
52 | * during allocation of clusters. The lock does not have to be held durring |
52 | * during allocation of clusters. The lock does not have to be held durring |
53 | * deallocation of clusters. |
53 | * deallocation of clusters. |
54 | */ |
54 | */ |
55 | static futex_t fat_alloc_lock = FUTEX_INITIALIZER; |
55 | static futex_t fat_alloc_lock = FUTEX_INITIALIZER; |
56 | 56 | ||
57 | /** Walk the cluster chain. |
57 | /** Walk the cluster chain. |
58 | * |
58 | * |
59 | * @param bs Buffer holding the boot sector for the file. |
59 | * @param bs Buffer holding the boot sector for the file. |
60 | * @param dev_handle Device handle of the device with the file. |
60 | * @param dev_handle Device handle of the device with the file. |
61 | * @param firstc First cluster to start the walk with. |
61 | * @param firstc First cluster to start the walk with. |
62 | * @param lastc If non-NULL, output argument hodling the last cluster number visited. |
62 | * @param lastc If non-NULL, output argument hodling the last cluster number visited. |
63 | * @param max_clusters Maximum number of clusters to visit. |
63 | * @param max_clusters Maximum number of clusters to visit. |
64 | * |
64 | * |
65 | * @return Number of clusters seen during the walk. |
65 | * @return Number of clusters seen during the walk. |
66 | */ |
66 | */ |
67 | uint16_t |
67 | uint16_t |
68 | fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
68 | fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
69 | fat_cluster_t *lastc, uint16_t max_clusters) |
69 | fat_cluster_t *lastc, uint16_t max_clusters) |
70 | { |
70 | { |
71 | block_t *b; |
71 | block_t *b; |
72 | unsigned bps; |
72 | unsigned bps; |
73 | unsigned rscnt; /* block address of the first FAT */ |
73 | unsigned rscnt; /* block address of the first FAT */ |
74 | uint16_t clusters = 0; |
74 | uint16_t clusters = 0; |
75 | fat_cluster_t clst = firstc; |
75 | fat_cluster_t clst = firstc; |
76 | 76 | ||
77 | bps = uint16_t_le2host(bs->bps); |
77 | bps = uint16_t_le2host(bs->bps); |
78 | rscnt = uint16_t_le2host(bs->rscnt); |
78 | rscnt = uint16_t_le2host(bs->rscnt); |
79 | 79 | ||
80 | if (firstc == FAT_CLST_RES0) { |
80 | if (firstc == FAT_CLST_RES0) { |
81 | /* No space allocated to the file. */ |
81 | /* No space allocated to the file. */ |
82 | if (lastc) |
82 | if (lastc) |
83 | *lastc = firstc; |
83 | *lastc = firstc; |
84 | return 0; |
84 | return 0; |
85 | } |
85 | } |
86 | 86 | ||
87 | while (clst < FAT_CLST_LAST1 && clusters < max_clusters) { |
87 | while (clst < FAT_CLST_LAST1 && clusters < max_clusters) { |
88 | bn_t fsec; /* sector offset relative to FAT1 */ |
88 | bn_t fsec; /* sector offset relative to FAT1 */ |
89 | unsigned fidx; /* FAT1 entry index */ |
89 | unsigned fidx; /* FAT1 entry index */ |
90 | 90 | ||
91 | assert(clst >= FAT_CLST_FIRST); |
91 | assert(clst >= FAT_CLST_FIRST); |
92 | if (lastc) |
92 | if (lastc) |
93 | *lastc = clst; /* remember the last cluster number */ |
93 | *lastc = clst; /* remember the last cluster number */ |
94 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
94 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
95 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
95 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
96 | /* read FAT1 */ |
96 | /* read FAT1 */ |
97 | b = block_get(dev_handle, rscnt + fsec); |
97 | b = block_get(dev_handle, rscnt + fsec); |
98 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
98 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
99 | assert(clst != FAT_CLST_BAD); |
99 | assert(clst != FAT_CLST_BAD); |
100 | block_put(b); |
100 | block_put(b); |
101 | clusters++; |
101 | clusters++; |
102 | } |
102 | } |
103 | 103 | ||
104 | if (lastc && clst < FAT_CLST_LAST1) |
104 | if (lastc && clst < FAT_CLST_LAST1) |
105 | *lastc = clst; |
105 | *lastc = clst; |
106 | 106 | ||
107 | return clusters; |
107 | return clusters; |
108 | } |
108 | } |
109 | 109 | ||
110 | /** Read block from file located on a FAT file system. |
110 | /** Read block from file located on a FAT file system. |
111 | * |
111 | * |
112 | * @param bs Buffer holding the boot sector of the file system. |
112 | * @param bs Buffer holding the boot sector of the file system. |
113 | * @param dev_handle Device handle of the file system. |
113 | * @param dev_handle Device handle of the file system. |
114 | * @param firstc First cluster used by the file. Can be zero if the file |
114 | * @param firstc First cluster used by the file. Can be zero if the file |
115 | * is empty. |
115 | * is empty. |
116 | * @param bn Block number. |
116 | * @param bn Block number. |
117 | * |
117 | * |
118 | * @return Block structure holding the requested block. |
118 | * @return Block structure holding the requested block. |
119 | */ |
119 | */ |
120 | block_t * |
120 | block_t * |
121 | _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
121 | _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
122 | bn_t bn) |
122 | bn_t bn) |
123 | { |
123 | { |
124 | block_t *b; |
124 | block_t *b; |
125 | unsigned bps; |
125 | unsigned bps; |
126 | unsigned rscnt; /* block address of the first FAT */ |
126 | unsigned rscnt; /* block address of the first FAT */ |
127 | unsigned rde; |
127 | unsigned rde; |
128 | unsigned rds; /* root directory size */ |
128 | unsigned rds; /* root directory size */ |
129 | unsigned sf; |
129 | unsigned sf; |
130 | unsigned ssa; /* size of the system area */ |
130 | unsigned ssa; /* size of the system area */ |
131 | unsigned clusters, max_clusters; |
131 | unsigned clusters, max_clusters; |
132 | fat_cluster_t lastc, clst = firstc; |
132 | fat_cluster_t lastc; |
133 | 133 | ||
134 | bps = uint16_t_le2host(bs->bps); |
134 | bps = uint16_t_le2host(bs->bps); |
135 | rscnt = uint16_t_le2host(bs->rscnt); |
135 | rscnt = uint16_t_le2host(bs->rscnt); |
136 | rde = uint16_t_le2host(bs->root_ent_max); |
136 | rde = uint16_t_le2host(bs->root_ent_max); |
137 | sf = uint16_t_le2host(bs->sec_per_fat); |
137 | sf = uint16_t_le2host(bs->sec_per_fat); |
138 | 138 | ||
139 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
139 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
140 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
140 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
141 | ssa = rscnt + bs->fatcnt * sf + rds; |
141 | ssa = rscnt + bs->fatcnt * sf + rds; |
142 | 142 | ||
143 | if (firstc == FAT_CLST_ROOT) { |
143 | if (firstc == FAT_CLST_ROOT) { |
144 | /* root directory special case */ |
144 | /* root directory special case */ |
145 | assert(bn < rds); |
145 | assert(bn < rds); |
146 | b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn); |
146 | b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn); |
147 | return b; |
147 | return b; |
148 | } |
148 | } |
149 | 149 | ||
150 | max_clusters = bn / bs->spc; |
150 | max_clusters = bn / bs->spc; |
151 | clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc, |
151 | clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc, |
152 | max_clusters); |
152 | max_clusters); |
153 | assert(clusters == max_clusters); |
153 | assert(clusters == max_clusters); |
154 | 154 | ||
155 | b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc + |
155 | b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc + |
156 | bn % bs->spc); |
156 | bn % bs->spc); |
157 | 157 | ||
158 | return b; |
158 | return b; |
159 | } |
159 | } |
160 | 160 | ||
161 | - | ||
162 | /** Fill the gap between EOF and a new file position. |
161 | /** Fill the gap between EOF and a new file position. |
163 | * |
162 | * |
164 | * @param bs Buffer holding the boot sector for nodep. |
163 | * @param bs Buffer holding the boot sector for nodep. |
165 | * @param nodep FAT node with the gap. |
164 | * @param nodep FAT node with the gap. |
166 | * @param mcl First cluster in an independent cluster chain that will |
165 | * @param mcl First cluster in an independent cluster chain that will |
167 | * be later appended to the end of the node's own cluster |
166 | * be later appended to the end of the node's own cluster |
168 | * chain. If pos is still in the last allocated cluster, |
167 | * chain. If pos is still in the last allocated cluster, |
169 | * this argument is ignored. |
168 | * this argument is ignored. |
170 | * @param pos Position in the last node block. |
169 | * @param pos Position in the last node block. |
171 | */ |
170 | */ |
172 | void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
171 | void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
173 | { |
172 | { |
174 | uint16_t bps; |
173 | uint16_t bps; |
175 | unsigned spc; |
174 | unsigned spc; |
176 | block_t *b; |
175 | block_t *b; |
177 | off_t o, boundary; |
176 | off_t o, boundary; |
178 | 177 | ||
179 | bps = uint16_t_le2host(bs->bps); |
178 | bps = uint16_t_le2host(bs->bps); |
180 | spc = bs->spc; |
179 | spc = bs->spc; |
181 | 180 | ||
182 | boundary = ROUND_UP(nodep->size, bps * spc); |
181 | boundary = ROUND_UP(nodep->size, bps * spc); |
183 | 182 | ||
184 | /* zero out already allocated space */ |
183 | /* zero out already allocated space */ |
185 | for (o = nodep->size - 1; o < pos && o < boundary; |
184 | for (o = nodep->size - 1; o < pos && o < boundary; |
186 | o = ALIGN_DOWN(o + bps, bps)) { |
185 | o = ALIGN_DOWN(o + bps, bps)) { |
187 | b = fat_block_get(bs, nodep, o / bps); |
186 | b = fat_block_get(bs, nodep, o / bps); |
188 | memset(b->data + o % bps, 0, bps - o % bps); |
187 | memset(b->data + o % bps, 0, bps - o % bps); |
189 | b->dirty = true; /* need to sync node */ |
188 | b->dirty = true; /* need to sync node */ |
190 | block_put(b); |
189 | block_put(b); |
191 | } |
190 | } |
192 | 191 | ||
193 | if (o >= pos) |
192 | if (o >= pos) |
194 | return; |
193 | return; |
195 | 194 | ||
196 | /* zero out the initial part of the new cluster chain */ |
195 | /* zero out the initial part of the new cluster chain */ |
197 | for (o = boundary; o < pos; o += bps) { |
196 | for (o = boundary; o < pos; o += bps) { |
198 | b = _fat_block_get(bs, nodep->idx->dev_handle, mcl, |
197 | b = _fat_block_get(bs, nodep->idx->dev_handle, mcl, |
199 | (o - boundary) / bps); |
198 | (o - boundary) / bps); |
200 | memset(b->data, 0, min(bps, pos - o)); |
199 | memset(b->data, 0, min(bps, pos - o)); |
201 | b->dirty = true; /* need to sync node */ |
200 | b->dirty = true; /* need to sync node */ |
202 | block_put(b); |
201 | block_put(b); |
203 | } |
202 | } |
204 | } |
203 | } |
205 | 204 | ||
- | 205 | /** Get cluster from the first FAT. |
|
- | 206 | * |
|
- | 207 | * @param bs Buffer holding the boot sector for the file system. |
|
- | 208 | * @param dev_handle Device handle for the file system. |
|
- | 209 | * @param clst Cluster which to get. |
|
- | 210 | * |
|
- | 211 | * @return Value found in the cluster. |
|
- | 212 | */ |
|
- | 213 | fat_cluster_t |
|
- | 214 | fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst) |
|
- | 215 | { |
|
- | 216 | block_t *b; |
|
- | 217 | uint16_t bps; |
|
- | 218 | uint16_t rscnt; |
|
- | 219 | fat_cluster_t *cp, value; |
|
- | 220 | ||
- | 221 | bps = uint16_t_le2host(bs->bps); |
|
- | 222 | rscnt = uint16_t_le2host(bs->rscnt); |
|
- | 223 | ||
- | 224 | b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps); |
|
- | 225 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
|
- | 226 | value = uint16_t_le2host(*cp); |
|
- | 227 | block_put(b); |
|
- | 228 | ||
- | 229 | return value; |
|
- | 230 | } |
|
- | 231 | ||
206 | /** Mark cluster in one instance of FAT. |
232 | /** Set cluster in one instance of FAT. |
207 | * |
233 | * |
208 | * @param bs Buffer holding the boot sector for the file system. |
234 | * @param bs Buffer holding the boot sector for the file system. |
209 | * @param dev_handle Device handle for the file system. |
235 | * @param dev_handle Device handle for the file system. |
210 | * @param fatno Number of the FAT instance where to make the change. |
236 | * @param fatno Number of the FAT instance where to make the change. |
211 | * @param clst Cluster which is to be marked. |
237 | * @param clst Cluster which is to be set. |
212 | * @param value Value mark the cluster with. |
238 | * @param value Value to set the cluster with. |
213 | */ |
239 | */ |
214 | void |
240 | void |
215 | fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, |
241 | fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, |
216 | fat_cluster_t clst, fat_cluster_t value) |
242 | fat_cluster_t clst, fat_cluster_t value) |
217 | { |
243 | { |
218 | block_t *b; |
244 | block_t *b; |
219 | uint16_t bps; |
245 | uint16_t bps; |
220 | uint16_t rscnt; |
246 | uint16_t rscnt; |
221 | uint16_t sf; |
247 | uint16_t sf; |
222 | fat_cluster_t *cp; |
248 | fat_cluster_t *cp; |
223 | 249 | ||
224 | bps = uint16_t_le2host(bs->bps); |
250 | bps = uint16_t_le2host(bs->bps); |
225 | rscnt = uint16_t_le2host(bs->rscnt); |
251 | rscnt = uint16_t_le2host(bs->rscnt); |
226 | sf = uint16_t_le2host(bs->sec_per_fat); |
252 | sf = uint16_t_le2host(bs->sec_per_fat); |
227 | 253 | ||
228 | assert(fatno < bs->fatcnt); |
254 | assert(fatno < bs->fatcnt); |
229 | b = block_get(dev_handle, rscnt + sf * fatno + |
255 | b = block_get(dev_handle, rscnt + sf * fatno + |
230 | (clst * sizeof(fat_cluster_t)) / bps); |
256 | (clst * sizeof(fat_cluster_t)) / bps); |
231 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
257 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
232 | *cp = host2uint16_t_le(value); |
258 | *cp = host2uint16_t_le(value); |
233 | b->dirty = true; /* need to sync block */ |
259 | b->dirty = true; /* need to sync block */ |
234 | block_put(b); |
260 | block_put(b); |
235 | } |
261 | } |
236 | 262 | ||
237 | /** Replay the allocatoin of clusters in all shadow instances of FAT. |
263 | /** Replay the allocatoin of clusters in all shadow instances of FAT. |
238 | * |
264 | * |
239 | * @param bs Buffer holding the boot sector of the file system. |
265 | * @param bs Buffer holding the boot sector of the file system. |
240 | * @param dev_handle Device handle of the file system. |
266 | * @param dev_handle Device handle of the file system. |
241 | * @param lifo Chain of allocated clusters. |
267 | * @param lifo Chain of allocated clusters. |
242 | * @param nclsts Number of clusters in the lifo chain. |
268 | * @param nclsts Number of clusters in the lifo chain. |
243 | */ |
269 | */ |
244 | void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, |
270 | void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, |
245 | fat_cluster_t *lifo, unsigned nclsts) |
271 | fat_cluster_t *lifo, unsigned nclsts) |
246 | { |
272 | { |
247 | uint8_t fatno; |
273 | uint8_t fatno; |
248 | unsigned c; |
274 | unsigned c; |
249 | 275 | ||
250 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { |
276 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { |
251 | for (c = 0; c < nclsts; c++) { |
277 | for (c = 0; c < nclsts; c++) { |
252 | fat_mark_cluster(bs, dev_handle, fatno, lifo[c], |
278 | fat_set_cluster(bs, dev_handle, fatno, lifo[c], |
253 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
279 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
254 | } |
280 | } |
255 | } |
281 | } |
256 | } |
282 | } |
257 | 283 | ||
258 | /** Allocate clusters in FAT1. |
284 | /** Allocate clusters in FAT1. |
259 | * |
285 | * |
260 | * This function will attempt to allocate the requested number of clusters in |
286 | * This function will attempt to allocate the requested number of clusters in |
261 | * the first FAT instance. The FAT will be altered so that the allocated |
287 | * the first FAT instance. The FAT will be altered so that the allocated |
262 | * clusters form an independent chain (i.e. a chain which does not belong to any |
288 | * clusters form an independent chain (i.e. a chain which does not belong to any |
263 | * file yet). |
289 | * file yet). |
264 | * |
290 | * |
265 | * @param bs Buffer holding the boot sector of the file system. |
291 | * @param bs Buffer holding the boot sector of the file system. |
266 | * @param dev_handle Device handle of the file system. |
292 | * @param dev_handle Device handle of the file system. |
267 | * @param nclsts Number of clusters to allocate. |
293 | * @param nclsts Number of clusters to allocate. |
268 | * @param mcl Output parameter where the first cluster in the chain |
294 | * @param mcl Output parameter where the first cluster in the chain |
269 | * will be returned. |
295 | * will be returned. |
270 | * @param lcl Output parameter where the last cluster in the chain |
296 | * @param lcl Output parameter where the last cluster in the chain |
271 | * will be returned. |
297 | * will be returned. |
272 | * |
298 | * |
273 | * @return EOK on success, a negative error code otherwise. |
299 | * @return EOK on success, a negative error code otherwise. |
274 | */ |
300 | */ |
275 | int |
301 | int |
276 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts, |
302 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts, |
277 | fat_cluster_t *mcl, fat_cluster_t *lcl) |
303 | fat_cluster_t *mcl, fat_cluster_t *lcl) |
278 | { |
304 | { |
279 | uint16_t bps; |
305 | uint16_t bps; |
280 | uint16_t rscnt; |
306 | uint16_t rscnt; |
281 | uint16_t sf; |
307 | uint16_t sf; |
282 | block_t *blk; |
308 | block_t *blk; |
283 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
309 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
284 | unsigned found = 0; /* top of the free cluster number stack */ |
310 | unsigned found = 0; /* top of the free cluster number stack */ |
285 | unsigned b, c, cl; |
311 | unsigned b, c, cl; |
286 | 312 | ||
287 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
313 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
288 | if (lifo) |
314 | if (lifo) |
289 | return ENOMEM; |
315 | return ENOMEM; |
290 | 316 | ||
291 | bps = uint16_t_le2host(bs->bps); |
317 | bps = uint16_t_le2host(bs->bps); |
292 | rscnt = uint16_t_le2host(bs->rscnt); |
318 | rscnt = uint16_t_le2host(bs->rscnt); |
293 | sf = uint16_t_le2host(bs->sec_per_fat); |
319 | sf = uint16_t_le2host(bs->sec_per_fat); |
294 | 320 | ||
295 | /* |
321 | /* |
296 | * Search FAT1 for unused clusters. |
322 | * Search FAT1 for unused clusters. |
297 | */ |
323 | */ |
298 | futex_down(&fat_alloc_lock); |
324 | futex_down(&fat_alloc_lock); |
299 | for (b = 0, cl = 0; b < sf; blk++) { |
325 | for (b = 0, cl = 0; b < sf; blk++) { |
300 | blk = block_get(dev_handle, rscnt + b); |
326 | blk = block_get(dev_handle, rscnt + b); |
301 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
327 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
302 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
328 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
303 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
329 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
304 | /* |
330 | /* |
305 | * The cluster is free. Put it into our stack |
331 | * The cluster is free. Put it into our stack |
306 | * of found clusters and mark it as non-free. |
332 | * of found clusters and mark it as non-free. |
307 | */ |
333 | */ |
308 | lifo[found] = cl; |
334 | lifo[found] = cl; |
309 | *clst = (found == 0) ? |
335 | *clst = (found == 0) ? |
310 | host2uint16_t_le(FAT_CLST_LAST1) : |
336 | host2uint16_t_le(FAT_CLST_LAST1) : |
311 | host2uint16_t_le(lifo[found - 1]); |
337 | host2uint16_t_le(lifo[found - 1]); |
312 | blk->dirty = true; /* need to sync block */ |
338 | blk->dirty = true; /* need to sync block */ |
313 | if (++found == nclsts) { |
339 | if (++found == nclsts) { |
314 | /* we are almost done */ |
340 | /* we are almost done */ |
315 | block_put(blk); |
341 | block_put(blk); |
316 | /* update the shadow copies of FAT */ |
342 | /* update the shadow copies of FAT */ |
317 | fat_alloc_shadow_clusters(bs, |
343 | fat_alloc_shadow_clusters(bs, |
318 | dev_handle, lifo, nclsts); |
344 | dev_handle, lifo, nclsts); |
319 | *mcl = lifo[found - 1]; |
345 | *mcl = lifo[found - 1]; |
320 | *lcl = lifo[0]; |
346 | *lcl = lifo[0]; |
321 | free(lifo); |
347 | free(lifo); |
322 | futex_up(&fat_alloc_lock); |
348 | futex_up(&fat_alloc_lock); |
323 | return EOK; |
349 | return EOK; |
324 | } |
350 | } |
325 | } |
351 | } |
326 | } |
352 | } |
327 | block_put(blk); |
353 | block_put(blk); |
328 | } |
354 | } |
329 | futex_up(&fat_alloc_lock); |
355 | futex_up(&fat_alloc_lock); |
330 | 356 | ||
331 | /* |
357 | /* |
332 | * We could not find enough clusters. Now we need to free the clusters |
358 | * We could not find enough clusters. Now we need to free the clusters |
333 | * we have allocated so far. |
359 | * we have allocated so far. |
334 | */ |
360 | */ |
335 | while (found--) { |
361 | while (found--) { |
336 | fat_mark_cluster(bs, dev_handle, FAT1, lifo[found], |
362 | fat_set_cluster(bs, dev_handle, FAT1, lifo[found], |
337 | FAT_CLST_RES0); |
363 | FAT_CLST_RES0); |
338 | } |
364 | } |
339 | 365 | ||
340 | free(lifo); |
366 | free(lifo); |
341 | return ENOSPC; |
367 | return ENOSPC; |
342 | } |
368 | } |
343 | 369 | ||
- | 370 | /** Free clusters forming a cluster chain in all copies of FAT. |
|
- | 371 | * |
|
- | 372 | * @param bs Buffer hodling the boot sector of the file system. |
|
- | 373 | * @param dev_handle Device handle of the file system. |
|
- | 374 | * @param firstc First cluster in the chain which is to be freed. |
|
- | 375 | */ |
|
- | 376 | void |
|
- | 377 | fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc) |
|
- | 378 | { |
|
- | 379 | unsigned fatno; |
|
- | 380 | fat_cluster_t nextc; |
|
- | 381 | ||
- | 382 | /* Mark all clusters in the chain as free in all copies of FAT. */ |
|
- | 383 | while (firstc < FAT_CLST_LAST1) { |
|
- | 384 | nextc = fat_get_cluster(bs, dev_handle, firstc); |
|
- | 385 | assert(nextc >= FAT_CLST_FIRST && nextc < FAT_CLST_BAD); |
|
- | 386 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
|
- | 387 | fat_set_cluster(bs, dev_handle, fatno, firstc, |
|
- | 388 | FAT_CLST_RES0); |
|
- | 389 | firstc = nextc; |
|
- | 390 | } |
|
- | 391 | } |
|
- | 392 | ||
344 | /** Append a cluster chain to the last file cluster in all FATs. |
393 | /** Append a cluster chain to the last file cluster in all FATs. |
345 | * |
394 | * |
346 | * @param bs Buffer holding boot sector of the file system. |
395 | * @param bs Buffer holding the boot sector of the file system. |
347 | * @param nodep Node representing the file. |
396 | * @param nodep Node representing the file. |
348 | * @param mcl First cluster of the cluster chain to append. |
397 | * @param mcl First cluster of the cluster chain to append. |
349 | */ |
398 | */ |
350 | void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) |
399 | void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) |
351 | { |
400 | { |
352 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
401 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
353 | fat_cluster_t lcl; |
402 | fat_cluster_t lcl; |
354 | uint8_t fatno; |
403 | uint8_t fatno; |
355 | 404 | ||
356 | if (fat_cluster_walk(bs, nodep->idx->dev_handle, nodep->firstc, &lcl, |
405 | if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, |
357 | (uint16_t) -1) == 0) { |
406 | (uint16_t) -1) == 0) { |
358 | /* No clusters allocated to the node yet. */ |
407 | /* No clusters allocated to the node yet. */ |
359 | nodep->firstc = host2uint16_t_le(mcl); |
408 | nodep->firstc = host2uint16_t_le(mcl); |
360 | nodep->dirty = true; /* need to sync node */ |
409 | nodep->dirty = true; /* need to sync node */ |
361 | return; |
410 | return; |
362 | } |
411 | } |
363 | 412 | ||
364 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
413 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
365 | fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); |
414 | fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); |
- | 415 | } |
|
- | 416 | ||
- | 417 | /** Chop off node clusters in all copies of FAT. |
|
- | 418 | * |
|
- | 419 | * @param bs Buffer holding the boot sector of the file system. |
|
- | 420 | * @param nodep FAT node where the chopping will take place. |
|
- | 421 | * @param lastc Last cluster which will remain in the node. If this |
|
- | 422 | * argument is FAT_CLST_RES0, then all clusters will |
|
- | 423 | * be choped off. |
|
- | 424 | */ |
|
- | 425 | void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc) |
|
- | 426 | { |
|
- | 427 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
|
- | 428 | if (lastc == FAT_CLST_RES0) { |
|
- | 429 | /* The node will have zero size and no clusters allocated. */ |
|
- | 430 | fat_free_clusters(bs, dev_handle, nodep->firstc); |
|
- | 431 | nodep->firstc = FAT_CLST_RES0; |
|
- | 432 | nodep->dirty = true; /* need to sync node */ |
|
- | 433 | } else { |
|
- | 434 | fat_cluster_t nextc; |
|
- | 435 | unsigned fatno; |
|
- | 436 | ||
- | 437 | nextc = fat_get_cluster(bs, dev_handle, lastc); |
|
- | 438 | ||
- | 439 | /* Terminate the cluster chain in all copies of FAT. */ |
|
- | 440 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
|
- | 441 | fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1); |
|
- | 442 | ||
- | 443 | /* Free all following clusters. */ |
|
- | 444 | fat_free_clusters(bs, dev_handle, nextc); |
|
- | 445 | } |
|
366 | } |
446 | } |
367 | 447 | ||
368 | /** |
448 | /** |
369 | * @} |
449 | * @} |
370 | */ |
450 | */ |
371 | 451 |