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