Rev 3510 | Rev 3513 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3510 | Rev 3512 | ||
---|---|---|---|
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 <errno.h> |
43 | #include <errno.h> |
44 | #include <byteorder.h> |
44 | #include <byteorder.h> |
45 | #include <align.h> |
45 | #include <align.h> |
46 | #include <assert.h> |
46 | #include <assert.h> |
47 | 47 | ||
48 | block_t * |
48 | block_t * |
49 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset) |
49 | _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset) |
50 | { |
50 | { |
51 | block_t *bb; |
51 | block_t *bb; |
52 | block_t *b; |
52 | block_t *b; |
53 | unsigned bps; |
53 | unsigned bps; |
54 | unsigned spc; |
54 | unsigned spc; |
55 | unsigned rscnt; /* block address of the first FAT */ |
55 | unsigned rscnt; /* block address of the first FAT */ |
56 | unsigned fatcnt; |
56 | unsigned fatcnt; |
57 | unsigned rde; |
57 | unsigned rde; |
58 | unsigned rds; /* root directory size */ |
58 | unsigned rds; /* root directory size */ |
59 | unsigned sf; |
59 | unsigned sf; |
60 | unsigned ssa; /* size of the system area */ |
60 | unsigned ssa; /* size of the system area */ |
61 | unsigned clusters; |
61 | unsigned clusters; |
62 | fat_cluster_t clst = firstc; |
62 | fat_cluster_t clst = firstc; |
63 | unsigned i; |
63 | unsigned i; |
64 | 64 | ||
65 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
65 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
66 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
66 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
67 | spc = FAT_BS(bb)->spc; |
67 | spc = FAT_BS(bb)->spc; |
68 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
68 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
69 | fatcnt = FAT_BS(bb)->fatcnt; |
69 | fatcnt = FAT_BS(bb)->fatcnt; |
70 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
70 | rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max); |
71 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
71 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
72 | block_put(bb); |
72 | block_put(bb); |
73 | 73 | ||
74 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
74 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
75 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
75 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
76 | ssa = rscnt + fatcnt * sf + rds; |
76 | ssa = rscnt + fatcnt * sf + rds; |
77 | 77 | ||
78 | if (firstc == FAT_CLST_ROOT) { |
78 | if (firstc == FAT_CLST_ROOT) { |
79 | /* root directory special case */ |
79 | /* root directory special case */ |
80 | assert(offset < rds); |
80 | assert(offset < rds); |
81 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
81 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
82 | return b; |
82 | return b; |
83 | } |
83 | } |
84 | 84 | ||
85 | clusters = offset / spc; |
85 | clusters = offset / spc; |
86 | for (i = 0; i < clusters; i++) { |
86 | for (i = 0; i < clusters; i++) { |
87 | unsigned fsec; /* sector offset relative to FAT1 */ |
87 | unsigned fsec; /* sector offset relative to FAT1 */ |
88 | unsigned fidx; /* FAT1 entry index */ |
88 | unsigned fidx; /* FAT1 entry index */ |
89 | 89 | ||
90 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
90 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
91 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
91 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
92 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
92 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
93 | /* read FAT1 */ |
93 | /* read FAT1 */ |
94 | b = block_get(dev_handle, rscnt + fsec, bps); |
94 | b = block_get(dev_handle, rscnt + fsec, bps); |
95 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
95 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
96 | assert(clst != FAT_CLST_BAD); |
96 | assert(clst != FAT_CLST_BAD); |
97 | assert(clst < FAT_CLST_LAST1); |
97 | assert(clst < FAT_CLST_LAST1); |
98 | block_put(b); |
98 | block_put(b); |
99 | } |
99 | } |
100 | 100 | ||
101 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
101 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
102 | offset % spc, bps); |
102 | offset % spc, bps); |
103 | 103 | ||
104 | return b; |
104 | return b; |
105 | } |
105 | } |
106 | 106 | ||
107 | /** Return number of blocks allocated to a file. |
107 | /** Return number of blocks allocated to a file. |
108 | * |
108 | * |
109 | * @param dev_handle Device handle of the device with the file. |
109 | * @param dev_handle Device handle of the device with the file. |
110 | * @param firstc First cluster of the file. |
110 | * @param firstc First cluster of the file. |
111 | * |
111 | * |
112 | * @return Number of blocks allocated to the file. |
112 | * @return Number of blocks allocated to the file. |
113 | */ |
113 | */ |
114 | uint16_t |
114 | uint16_t |
115 | _fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc) |
115 | _fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc) |
116 | { |
116 | { |
117 | block_t *bb; |
117 | block_t *bb; |
118 | block_t *b; |
118 | block_t *b; |
119 | unsigned bps; |
119 | unsigned bps; |
120 | unsigned spc; |
120 | unsigned spc; |
121 | unsigned rscnt; /* block address of the first FAT */ |
121 | unsigned rscnt; /* block address of the first FAT */ |
122 | unsigned clusters = 0; |
122 | unsigned clusters = 0; |
123 | fat_cluster_t clst = firstc; |
123 | fat_cluster_t clst = firstc; |
124 | 124 | ||
125 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
125 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
126 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
126 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
127 | spc = FAT_BS(bb)->spc; |
127 | spc = FAT_BS(bb)->spc; |
128 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
128 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
129 | block_put(bb); |
129 | block_put(bb); |
130 | 130 | ||
131 | if (firstc == FAT_CLST_RES0) { |
131 | if (firstc == FAT_CLST_RES0) { |
132 | /* No space allocated to the file. */ |
132 | /* No space allocated to the file. */ |
133 | return 0; |
133 | return 0; |
134 | } |
134 | } |
135 | 135 | ||
136 | while (clst < FAT_CLST_LAST1) { |
136 | while (clst < FAT_CLST_LAST1) { |
137 | unsigned fsec; /* sector offset relative to FAT1 */ |
137 | unsigned fsec; /* sector offset relative to FAT1 */ |
138 | unsigned fidx; /* FAT1 entry index */ |
138 | unsigned fidx; /* FAT1 entry index */ |
139 | 139 | ||
140 | assert(clst >= FAT_CLST_FIRST); |
140 | assert(clst >= FAT_CLST_FIRST); |
141 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
141 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
142 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
142 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
143 | /* read FAT1 */ |
143 | /* read FAT1 */ |
144 | b = block_get(dev_handle, rscnt + fsec, bps); |
144 | b = block_get(dev_handle, rscnt + fsec, bps); |
145 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
145 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
146 | assert(clst != FAT_CLST_BAD); |
146 | assert(clst != FAT_CLST_BAD); |
147 | block_put(b); |
147 | block_put(b); |
148 | clusters++; |
148 | clusters++; |
149 | } |
149 | } |
150 | 150 | ||
151 | return clusters * spc; |
151 | return clusters * spc; |
152 | } |
152 | } |
153 | 153 | ||
154 | uint16_t fat_bps_get(dev_handle_t dev_handle) |
154 | uint16_t fat_bps_get(dev_handle_t dev_handle) |
155 | { |
155 | { |
156 | block_t *bb; |
156 | block_t *bb; |
157 | uint16_t bps; |
157 | uint16_t bps; |
158 | 158 | ||
159 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
159 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
160 | assert(bb != NULL); |
160 | assert(bb != NULL); |
161 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
161 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
162 | block_put(bb); |
162 | block_put(bb); |
163 | 163 | ||
164 | return bps; |
164 | return bps; |
165 | } |
165 | } |
166 | 166 | ||
167 | /** Fill the gap between EOF and a new file position. |
167 | /** Fill the gap between EOF and a new file position. |
168 | * |
168 | * |
169 | * @param nodep FAT node with the gap. |
169 | * @param nodep FAT node with the gap. |
170 | * @param mcl First cluster in an independent cluster chain that will |
170 | * @param mcl First cluster in an independent cluster chain that will |
171 | * be later appended to the end of the node's own cluster |
171 | * be later appended to the end of the node's own cluster |
172 | * chain. If pos is still in the last allocated cluster, |
172 | * chain. If pos is still in the last allocated cluster, |
173 | * this argument is ignored. |
173 | * this argument is ignored. |
174 | * @param pos Position in the last node block. |
174 | * @param pos Position in the last node block. |
175 | */ |
175 | */ |
176 | void fat_fill_gap(fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
176 | void fat_fill_gap(fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
177 | { |
177 | { |
178 | uint16_t bps; |
178 | uint16_t bps; |
179 | unsigned spc; |
179 | unsigned spc; |
180 | block_t *bb, *b; |
180 | block_t *bb, *b; |
181 | off_t o, boundary; |
181 | off_t o, boundary; |
182 | 182 | ||
183 | bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE); |
183 | bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE); |
184 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
184 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
185 | spc = FAT_BS(bb)->spc; |
185 | spc = FAT_BS(bb)->spc; |
186 | block_put(bb); |
186 | block_put(bb); |
187 | 187 | ||
188 | boundary = ROUND_UP(nodep->size, bps * spc); |
188 | boundary = ROUND_UP(nodep->size, bps * spc); |
189 | 189 | ||
190 | /* zero out already allocated space */ |
190 | /* zero out already allocated space */ |
191 | for (o = nodep->size - 1; o < pos && o < boundary; |
191 | for (o = nodep->size - 1; o < pos && o < boundary; |
192 | o = ALIGN_DOWN(o + bps, bps)) { |
192 | o = ALIGN_DOWN(o + bps, bps)) { |
193 | b = fat_block_get(nodep, o / bps); |
193 | b = fat_block_get(nodep, o / bps); |
194 | memset(b->data + o % bps, 0, bps - o % bps); |
194 | memset(b->data + o % bps, 0, bps - o % bps); |
195 | b->dirty = true; /* need to sync node */ |
195 | b->dirty = true; /* need to sync node */ |
196 | block_put(b); |
196 | block_put(b); |
197 | } |
197 | } |
198 | 198 | ||
199 | if (o >= pos) |
199 | if (o >= pos) |
200 | return; |
200 | return; |
201 | 201 | ||
202 | /* zero out the initial part of the new cluster chain */ |
202 | /* zero out the initial part of the new cluster chain */ |
203 | for (o = boundary; o < pos; o += bps) { |
203 | for (o = boundary; o < pos; o += bps) { |
204 | b = _fat_block_get(nodep->idx->dev_handle, mcl, |
204 | b = _fat_block_get(nodep->idx->dev_handle, mcl, |
205 | (o - boundary) / bps); |
205 | (o - boundary) / bps); |
206 | memset(b->data, 0, min(bps, pos - o)); |
206 | memset(b->data, 0, min(bps, pos - o)); |
207 | b->dirty = true; /* need to sync node */ |
207 | b->dirty = true; /* need to sync node */ |
208 | block_put(b); |
208 | block_put(b); |
209 | } |
209 | } |
210 | } |
210 | } |
211 | 211 | ||
212 | void |
212 | void |
213 | fat_mark_cluster(dev_handle_t dev_handle, unsigned fatno, fat_cluster_t clst, |
213 | fat_mark_cluster(dev_handle_t dev_handle, unsigned fatno, fat_cluster_t clst, |
214 | fat_cluster_t value) |
214 | fat_cluster_t value) |
215 | { |
215 | { |
216 | block_t *bb, *blk; |
216 | block_t *bb, *blk; |
217 | uint16_t bps; |
217 | uint16_t bps; |
218 | uint16_t rscnt; |
218 | uint16_t rscnt; |
219 | uint16_t sf; |
219 | uint16_t sf; |
220 | uint8_t fatcnt; |
220 | uint8_t fatcnt; |
221 | fat_cluster_t *cp; |
221 | fat_cluster_t *cp; |
222 | 222 | ||
223 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
223 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
224 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
224 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
225 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
225 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
226 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
226 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
227 | fatcnt = FAT_BS(bb)->fatcnt; |
227 | fatcnt = FAT_BS(bb)->fatcnt; |
228 | block_put(bb); |
228 | block_put(bb); |
229 | 229 | ||
230 | assert(fatno < fatcnt); |
230 | assert(fatno < fatcnt); |
231 | blk = block_get(dev_handle, rscnt + sf * fatno + |
231 | blk = block_get(dev_handle, rscnt + sf * fatno + |
232 | (clst * sizeof(fat_cluster_t)) / bps, bps); |
232 | (clst * sizeof(fat_cluster_t)) / bps, bps); |
233 | cp = (fat_cluster_t *)blk->data + clst % (bps / sizeof(fat_cluster_t)); |
233 | cp = (fat_cluster_t *)blk->data + clst % (bps / sizeof(fat_cluster_t)); |
234 | *cp = host2uint16_t_le(value); |
234 | *cp = host2uint16_t_le(value); |
235 | blk->dirty = true; /* need to sync block */ |
235 | blk->dirty = true; /* need to sync block */ |
236 | block_put(blk); |
236 | block_put(blk); |
237 | } |
237 | } |
238 | 238 | ||
239 | void fat_alloc_shadow_clusters(dev_handle_t dev_handle, fat_cluster_t *lifo, |
239 | void fat_alloc_shadow_clusters(dev_handle_t dev_handle, fat_cluster_t *lifo, |
240 | unsigned nclsts) |
240 | unsigned nclsts) |
241 | { |
241 | { |
242 | uint8_t fatcnt; |
242 | uint8_t fatcnt; |
243 | uint8_t fatno; |
243 | uint8_t fatno; |
244 | unsigned c; |
244 | unsigned c; |
245 | block_t *bb; |
245 | block_t *bb; |
246 | 246 | ||
247 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
247 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
248 | fatcnt = FAT_BS(bb)->fatcnt; |
248 | fatcnt = FAT_BS(bb)->fatcnt; |
249 | block_put(bb); |
249 | block_put(bb); |
250 | 250 | ||
251 | for (fatno = FAT1 + 1; fatno < fatcnt; fatno++) { |
251 | for (fatno = FAT1 + 1; fatno < fatcnt; fatno++) { |
252 | for (c = 0; c < nclsts; c++) { |
252 | for (c = 0; c < nclsts; c++) { |
253 | fat_mark_cluster(dev_handle, fatno, lifo[c], |
253 | fat_mark_cluster(dev_handle, fatno, lifo[c], |
254 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
254 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
255 | } |
255 | } |
256 | } |
256 | } |
257 | } |
257 | } |
258 | 258 | ||
259 | int |
259 | int |
260 | fat_alloc_clusters(dev_handle_t dev_handle, unsigned nclsts, fat_cluster_t *mcl, |
260 | fat_alloc_clusters(dev_handle_t dev_handle, unsigned nclsts, fat_cluster_t *mcl, |
261 | fat_cluster_t *lcl) |
261 | fat_cluster_t *lcl) |
262 | { |
262 | { |
263 | uint16_t bps; |
263 | uint16_t bps; |
264 | uint16_t rscnt; |
264 | uint16_t rscnt; |
265 | uint16_t sf; |
265 | uint16_t sf; |
266 | block_t *bb, *blk; |
266 | block_t *bb, *blk; |
267 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
267 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
268 | unsigned found = 0; /* top of the free cluster number stack */ |
268 | unsigned found = 0; /* top of the free cluster number stack */ |
269 | unsigned b, c, cl; |
269 | unsigned b, c, cl; |
270 | 270 | ||
271 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
271 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
272 | if (lifo) |
272 | if (lifo) |
273 | return ENOMEM; |
273 | return ENOMEM; |
274 | 274 | ||
275 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
275 | bb = block_get(dev_handle, BS_BLOCK, BS_SIZE); |
276 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
276 | bps = uint16_t_le2host(FAT_BS(bb)->bps); |
277 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
277 | rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt); |
278 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
278 | sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat); |
279 | block_put(bb); |
279 | block_put(bb); |
280 | 280 | ||
281 | /* |
281 | /* |
282 | * Search FAT1 for unused clusters. |
282 | * Search FAT1 for unused clusters. |
283 | */ |
283 | */ |
284 | for (b = 0, cl = 0; b < sf; blk++) { |
284 | for (b = 0, cl = 0; b < sf; blk++) { |
285 | blk = block_get(dev_handle, rscnt + b, bps); |
285 | blk = block_get(dev_handle, rscnt + b, bps); |
286 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
286 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
287 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
287 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
288 | if (*clst == FAT_CLST_RES0) { |
288 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
289 | /* |
289 | /* |
290 | * The cluster is free. Put it into our stack |
290 | * The cluster is free. Put it into our stack |
291 | * of found clusters and mark it as non-free. |
291 | * of found clusters and mark it as non-free. |
292 | */ |
292 | */ |
293 | lifo[found] = cl; |
293 | lifo[found] = cl; |
294 | if (found == 0) |
294 | *clst = (found == 0) ? |
295 | *clst = FAT_CLST_LAST1; |
295 | host2uint16_t_le(FAT_CLST_LAST1) : |
296 | else |
- | |
297 | *clst = lifo[found - 1]; |
296 | host2uint16_t_le(lifo[found - 1]); |
298 | blk->dirty = true; /* need to sync block */ |
297 | blk->dirty = true; /* need to sync block */ |
299 | if (++found == nclsts) { |
298 | if (++found == nclsts) { |
300 | /* we are almost done */ |
299 | /* we are almost done */ |
301 | block_put(blk); |
300 | block_put(blk); |
302 | /* update the shadow copies of FAT */ |
301 | /* update the shadow copies of FAT */ |
303 | fat_alloc_shadow_clusters(dev_handle, |
302 | fat_alloc_shadow_clusters(dev_handle, |
304 | lifo, nclsts); |
303 | lifo, nclsts); |
305 | *mcl = lifo[found - 1]; |
304 | *mcl = lifo[found - 1]; |
306 | *lcl = lifo[0]; |
305 | *lcl = lifo[0]; |
307 | free(lifo); |
306 | free(lifo); |
308 | return EOK; |
307 | return EOK; |
309 | } |
308 | } |
310 | } |
309 | } |
311 | } |
310 | } |
312 | block_put(blk); |
311 | block_put(blk); |
313 | } |
312 | } |
314 | 313 | ||
315 | /* |
314 | /* |
316 | * We could not find enough clusters. Now we need to free the clusters |
315 | * We could not find enough clusters. Now we need to free the clusters |
317 | * we have allocated so far. |
316 | * we have allocated so far. |
318 | */ |
317 | */ |
319 | while (found--) |
318 | while (found--) |
320 | fat_mark_cluster(dev_handle, FAT1, lifo[found], FAT_CLST_RES0); |
319 | fat_mark_cluster(dev_handle, FAT1, lifo[found], FAT_CLST_RES0); |
321 | 320 | ||
322 | free(lifo); |
321 | free(lifo); |
323 | return ENOSPC; |
322 | return ENOSPC; |
324 | } |
323 | } |
325 | 324 | ||
326 | void fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl) |
325 | void fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl) |
327 | { |
326 | { |
328 | } |
327 | } |
329 | 328 | ||
330 | /** |
329 | /** |
331 | * @} |
330 | * @} |
332 | */ |
331 | */ |
333 | 332 |