Rev 3521 | Rev 3561 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3521 | Rev 3535 | ||
---|---|---|---|
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 | /** Read block from file located on a FAT file system. |
57 | /** Read block from file located on a FAT file system. |
58 | * |
58 | * |
59 | * @param bs Buffer holding the boot sector of the file system. |
59 | * @param bs Buffer holding the boot sector of the file system. |
60 | * @param dev_handle Device handle of the file system. |
60 | * @param dev_handle Device handle of the file system. |
61 | * @param firstc First cluster used by the file. Can be zero if the file |
61 | * @param firstc First cluster used by the file. Can be zero if the file |
62 | * is empty. |
62 | * is empty. |
63 | * @param offset Offset in blocks. |
63 | * @param offset Offset in blocks. |
64 | * |
64 | * |
65 | * @return Block structure holding the requested block. |
65 | * @return Block structure holding the requested block. |
66 | */ |
66 | */ |
67 | block_t * |
67 | block_t * |
68 | _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
68 | _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
69 | off_t offset) |
69 | off_t offset) |
70 | { |
70 | { |
71 | block_t *b; |
71 | block_t *b; |
72 | unsigned bps; |
72 | unsigned bps; |
73 | unsigned spc; |
73 | unsigned spc; |
74 | unsigned rscnt; /* block address of the first FAT */ |
74 | unsigned rscnt; /* block address of the first FAT */ |
75 | unsigned fatcnt; |
75 | unsigned fatcnt; |
76 | unsigned rde; |
76 | unsigned rde; |
77 | unsigned rds; /* root directory size */ |
77 | unsigned rds; /* root directory size */ |
78 | unsigned sf; |
78 | unsigned sf; |
79 | unsigned ssa; /* size of the system area */ |
79 | unsigned ssa; /* size of the system area */ |
80 | unsigned clusters; |
80 | unsigned clusters; |
81 | fat_cluster_t clst = firstc; |
81 | fat_cluster_t clst = firstc; |
82 | unsigned i; |
82 | unsigned i; |
83 | 83 | ||
84 | bps = uint16_t_le2host(bs->bps); |
84 | bps = uint16_t_le2host(bs->bps); |
85 | spc = bs->spc; |
85 | spc = bs->spc; |
86 | rscnt = uint16_t_le2host(bs->rscnt); |
86 | rscnt = uint16_t_le2host(bs->rscnt); |
87 | fatcnt = bs->fatcnt; |
87 | fatcnt = bs->fatcnt; |
88 | rde = uint16_t_le2host(bs->root_ent_max); |
88 | rde = uint16_t_le2host(bs->root_ent_max); |
89 | sf = uint16_t_le2host(bs->sec_per_fat); |
89 | sf = uint16_t_le2host(bs->sec_per_fat); |
90 | 90 | ||
91 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
91 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
92 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
92 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
93 | ssa = rscnt + fatcnt * sf + rds; |
93 | ssa = rscnt + fatcnt * sf + rds; |
94 | 94 | ||
95 | if (firstc == FAT_CLST_ROOT) { |
95 | if (firstc == FAT_CLST_ROOT) { |
96 | /* root directory special case */ |
96 | /* root directory special case */ |
97 | assert(offset < rds); |
97 | assert(offset < rds); |
98 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
98 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
99 | return b; |
99 | return b; |
100 | } |
100 | } |
101 | 101 | ||
102 | clusters = offset / spc; |
102 | clusters = offset / spc; |
103 | for (i = 0; i < clusters; i++) { |
103 | for (i = 0; i < clusters; i++) { |
104 | unsigned fsec; /* sector offset relative to FAT1 */ |
104 | unsigned fsec; /* sector offset relative to FAT1 */ |
105 | unsigned fidx; /* FAT1 entry index */ |
105 | unsigned fidx; /* FAT1 entry index */ |
106 | 106 | ||
107 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
107 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
108 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
108 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
109 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
109 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
110 | /* read FAT1 */ |
110 | /* read FAT1 */ |
111 | b = block_get(dev_handle, rscnt + fsec, bps); |
111 | b = block_get(dev_handle, rscnt + fsec, bps); |
112 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
112 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
113 | assert(clst != FAT_CLST_BAD); |
113 | assert(clst != FAT_CLST_BAD); |
114 | assert(clst < FAT_CLST_LAST1); |
114 | assert(clst < FAT_CLST_LAST1); |
115 | block_put(b); |
115 | block_put(b); |
116 | } |
116 | } |
117 | 117 | ||
118 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
118 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
119 | offset % spc, bps); |
119 | offset % spc, bps); |
120 | 120 | ||
121 | return b; |
121 | return b; |
122 | } |
122 | } |
123 | 123 | ||
124 | /** Return number of blocks allocated to a file. |
124 | /** Return number of blocks allocated to a file. |
125 | * |
125 | * |
126 | * @param bs Buffer holding the boot sector for the file. |
126 | * @param bs Buffer holding the boot sector for the file. |
127 | * @param dev_handle Device handle of the device with the file. |
127 | * @param dev_handle Device handle of the device with the file. |
128 | * @param firstc First cluster of the file. |
128 | * @param firstc First cluster of the file. |
129 | * @param lastc If non-NULL, output argument holding the |
129 | * @param lastc If non-NULL, output argument holding the |
130 | * last cluster. |
130 | * last cluster. |
131 | * |
131 | * |
132 | * @return Number of blocks allocated to the file. |
132 | * @return Number of blocks allocated to the file. |
133 | */ |
133 | */ |
134 | uint16_t |
134 | uint16_t |
135 | _fat_blcks_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
135 | _fat_blcks_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
136 | fat_cluster_t *lastc) |
136 | fat_cluster_t *lastc) |
137 | { |
137 | { |
138 | block_t *b; |
138 | block_t *b; |
139 | unsigned bps; |
139 | unsigned bps; |
140 | unsigned spc; |
140 | unsigned spc; |
141 | unsigned rscnt; /* block address of the first FAT */ |
141 | unsigned rscnt; /* block address of the first FAT */ |
142 | unsigned clusters = 0; |
142 | unsigned clusters = 0; |
143 | fat_cluster_t clst = firstc; |
143 | fat_cluster_t clst = firstc; |
144 | 144 | ||
145 | bps = uint16_t_le2host(bs->bps); |
145 | bps = uint16_t_le2host(bs->bps); |
146 | spc = bs->spc; |
146 | spc = bs->spc; |
147 | rscnt = uint16_t_le2host(bs->rscnt); |
147 | rscnt = uint16_t_le2host(bs->rscnt); |
148 | 148 | ||
149 | if (firstc == FAT_CLST_RES0) { |
149 | if (firstc == FAT_CLST_RES0) { |
150 | /* No space allocated to the file. */ |
150 | /* No space allocated to the file. */ |
151 | if (lastc) |
151 | if (lastc) |
152 | *lastc = firstc; |
152 | *lastc = firstc; |
153 | return 0; |
153 | return 0; |
154 | } |
154 | } |
155 | 155 | ||
156 | while (clst < FAT_CLST_LAST1) { |
156 | while (clst < FAT_CLST_LAST1) { |
157 | unsigned fsec; /* sector offset relative to FAT1 */ |
157 | unsigned fsec; /* sector offset relative to FAT1 */ |
158 | unsigned fidx; /* FAT1 entry index */ |
158 | unsigned fidx; /* FAT1 entry index */ |
159 | 159 | ||
160 | assert(clst >= FAT_CLST_FIRST); |
160 | assert(clst >= FAT_CLST_FIRST); |
161 | if (lastc) |
161 | if (lastc) |
162 | *lastc = clst; /* remember the last cluster */ |
162 | *lastc = clst; /* remember the last cluster */ |
163 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
163 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
164 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
164 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
165 | /* read FAT1 */ |
165 | /* read FAT1 */ |
166 | b = block_get(dev_handle, rscnt + fsec, bps); |
166 | b = block_get(dev_handle, rscnt + fsec, bps); |
167 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
167 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
168 | assert(clst != FAT_CLST_BAD); |
168 | assert(clst != FAT_CLST_BAD); |
169 | block_put(b); |
169 | block_put(b); |
170 | clusters++; |
170 | clusters++; |
171 | } |
171 | } |
172 | 172 | ||
173 | if (lastc) |
173 | if (lastc) |
174 | *lastc = clst; |
174 | *lastc = clst; |
175 | return clusters * spc; |
175 | return clusters * spc; |
176 | } |
176 | } |
177 | 177 | ||
178 | /** Fill the gap between EOF and a new file position. |
178 | /** Fill the gap between EOF and a new file position. |
179 | * |
179 | * |
180 | * @param bs Buffer holding the boot sector for nodep. |
180 | * @param bs Buffer holding the boot sector for nodep. |
181 | * @param nodep FAT node with the gap. |
181 | * @param nodep FAT node with the gap. |
182 | * @param mcl First cluster in an independent cluster chain that will |
182 | * @param mcl First cluster in an independent cluster chain that will |
183 | * be later appended to the end of the node's own cluster |
183 | * be later appended to the end of the node's own cluster |
184 | * chain. If pos is still in the last allocated cluster, |
184 | * chain. If pos is still in the last allocated cluster, |
185 | * this argument is ignored. |
185 | * this argument is ignored. |
186 | * @param pos Position in the last node block. |
186 | * @param pos Position in the last node block. |
187 | */ |
187 | */ |
188 | void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
188 | void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
189 | { |
189 | { |
190 | uint16_t bps; |
190 | uint16_t bps; |
191 | unsigned spc; |
191 | unsigned spc; |
192 | block_t *b; |
192 | block_t *b; |
193 | off_t o, boundary; |
193 | off_t o, boundary; |
194 | 194 | ||
195 | bps = uint16_t_le2host(bs->bps); |
195 | bps = uint16_t_le2host(bs->bps); |
196 | spc = bs->spc; |
196 | spc = bs->spc; |
197 | 197 | ||
198 | boundary = ROUND_UP(nodep->size, bps * spc); |
198 | boundary = ROUND_UP(nodep->size, bps * spc); |
199 | 199 | ||
200 | /* zero out already allocated space */ |
200 | /* zero out already allocated space */ |
201 | for (o = nodep->size - 1; o < pos && o < boundary; |
201 | for (o = nodep->size - 1; o < pos && o < boundary; |
202 | o = ALIGN_DOWN(o + bps, bps)) { |
202 | o = ALIGN_DOWN(o + bps, bps)) { |
203 | b = fat_block_get(bs, nodep, o / bps); |
203 | b = fat_block_get(bs, nodep, o / bps); |
204 | memset(b->data + o % bps, 0, bps - o % bps); |
204 | memset(b->data + o % bps, 0, bps - o % bps); |
205 | b->dirty = true; /* need to sync node */ |
205 | b->dirty = true; /* need to sync node */ |
206 | block_put(b); |
206 | block_put(b); |
207 | } |
207 | } |
208 | 208 | ||
209 | if (o >= pos) |
209 | if (o >= pos) |
210 | return; |
210 | return; |
211 | 211 | ||
212 | /* zero out the initial part of the new cluster chain */ |
212 | /* zero out the initial part of the new cluster chain */ |
213 | for (o = boundary; o < pos; o += bps) { |
213 | for (o = boundary; o < pos; o += bps) { |
214 | b = _fat_block_get(bs, nodep->idx->dev_handle, mcl, |
214 | b = _fat_block_get(bs, nodep->idx->dev_handle, mcl, |
215 | (o - boundary) / bps); |
215 | (o - boundary) / bps); |
216 | memset(b->data, 0, min(bps, pos - o)); |
216 | memset(b->data, 0, min(bps, pos - o)); |
217 | b->dirty = true; /* need to sync node */ |
217 | b->dirty = true; /* need to sync node */ |
218 | block_put(b); |
218 | block_put(b); |
219 | } |
219 | } |
220 | } |
220 | } |
221 | 221 | ||
222 | /** Mark cluster in one instance of FAT. |
222 | /** Mark cluster in one instance of FAT. |
223 | * |
223 | * |
224 | * @param bs Buffer holding the boot sector for the file system. |
224 | * @param bs Buffer holding the boot sector for the file system. |
225 | * @param dev_handle Device handle for the file system. |
225 | * @param dev_handle Device handle for the file system. |
226 | * @param fatno Number of the FAT instance where to make the change. |
226 | * @param fatno Number of the FAT instance where to make the change. |
227 | * @param clst Cluster which is to be marked. |
227 | * @param clst Cluster which is to be marked. |
228 | * @param value Value mark the cluster with. |
228 | * @param value Value mark the cluster with. |
229 | */ |
229 | */ |
230 | void |
230 | void |
231 | fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, |
231 | fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, |
232 | fat_cluster_t clst, fat_cluster_t value) |
232 | fat_cluster_t clst, fat_cluster_t value) |
233 | { |
233 | { |
234 | block_t *b; |
234 | block_t *b; |
235 | uint16_t bps; |
235 | uint16_t bps; |
236 | uint16_t rscnt; |
236 | uint16_t rscnt; |
237 | uint16_t sf; |
237 | uint16_t sf; |
238 | fat_cluster_t *cp; |
238 | fat_cluster_t *cp; |
239 | 239 | ||
240 | bps = uint16_t_le2host(bs->bps); |
240 | bps = uint16_t_le2host(bs->bps); |
241 | rscnt = uint16_t_le2host(bs->rscnt); |
241 | rscnt = uint16_t_le2host(bs->rscnt); |
242 | sf = uint16_t_le2host(bs->sec_per_fat); |
242 | sf = uint16_t_le2host(bs->sec_per_fat); |
243 | 243 | ||
244 | assert(fatno < bs->fatcnt); |
244 | assert(fatno < bs->fatcnt); |
245 | b = block_get(dev_handle, rscnt + sf * fatno + |
245 | b = block_get(dev_handle, rscnt + sf * fatno + |
246 | (clst * sizeof(fat_cluster_t)) / bps, bps); |
246 | (clst * sizeof(fat_cluster_t)) / bps, bps); |
247 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
247 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
248 | *cp = host2uint16_t_le(value); |
248 | *cp = host2uint16_t_le(value); |
249 | b->dirty = true; /* need to sync block */ |
249 | b->dirty = true; /* need to sync block */ |
250 | block_put(b); |
250 | block_put(b); |
251 | } |
251 | } |
252 | 252 | ||
253 | /** Replay the allocatoin of clusters in all shadow instances of FAT. |
253 | /** Replay the allocatoin of clusters in all shadow instances of FAT. |
254 | * |
254 | * |
255 | * @param bs Buffer holding the boot sector of the file system. |
255 | * @param bs Buffer holding the boot sector of the file system. |
256 | * @param dev_handle Device handle of the file system. |
256 | * @param dev_handle Device handle of the file system. |
257 | * @param lifo Chain of allocated clusters. |
257 | * @param lifo Chain of allocated clusters. |
258 | * @param nclsts Number of clusters in the lifo chain. |
258 | * @param nclsts Number of clusters in the lifo chain. |
259 | */ |
259 | */ |
260 | void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, |
260 | void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, |
261 | fat_cluster_t *lifo, unsigned nclsts) |
261 | fat_cluster_t *lifo, unsigned nclsts) |
262 | { |
262 | { |
263 | uint8_t fatno; |
263 | uint8_t fatno; |
264 | unsigned c; |
264 | unsigned c; |
265 | 265 | ||
266 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { |
266 | for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { |
267 | for (c = 0; c < nclsts; c++) { |
267 | for (c = 0; c < nclsts; c++) { |
268 | fat_mark_cluster(bs, dev_handle, fatno, lifo[c], |
268 | fat_mark_cluster(bs, dev_handle, fatno, lifo[c], |
269 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
269 | c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
270 | } |
270 | } |
271 | } |
271 | } |
272 | } |
272 | } |
273 | 273 | ||
274 | /** Allocate clusters in FAT1. |
274 | /** Allocate clusters in FAT1. |
275 | * |
275 | * |
276 | * This function will attempt to allocate the requested number of clusters in |
276 | * This function will attempt to allocate the requested number of clusters in |
277 | * the first FAT instance. The FAT will be altered so that the allocated |
277 | * the first FAT instance. The FAT will be altered so that the allocated |
278 | * clusters form an independent chain (i.e. a chain which does not belong to any |
278 | * clusters form an independent chain (i.e. a chain which does not belong to any |
279 | * file yet). |
279 | * file yet). |
280 | * |
280 | * |
281 | * @param bs Buffer holding the boot sector of the file system. |
281 | * @param bs Buffer holding the boot sector of the file system. |
282 | * @param dev_handle Device handle of the file system. |
282 | * @param dev_handle Device handle of the file system. |
283 | * @param nclsts Number of clusters to allocate. |
283 | * @param nclsts Number of clusters to allocate. |
284 | * @param mcl Output parameter where the first cluster in the chain |
284 | * @param mcl Output parameter where the first cluster in the chain |
285 | * will be returned. |
285 | * will be returned. |
286 | * @param lcl Output parameter where the last cluster in the chain |
286 | * @param lcl Output parameter where the last cluster in the chain |
287 | * will be returned. |
287 | * will be returned. |
288 | * |
288 | * |
289 | * @return EOK on success, a negative error code otherwise. |
289 | * @return EOK on success, a negative error code otherwise. |
290 | */ |
290 | */ |
291 | int |
291 | int |
292 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts, |
292 | fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts, |
293 | fat_cluster_t *mcl, fat_cluster_t *lcl) |
293 | fat_cluster_t *mcl, fat_cluster_t *lcl) |
294 | { |
294 | { |
295 | uint16_t bps; |
295 | uint16_t bps; |
296 | uint16_t rscnt; |
296 | uint16_t rscnt; |
297 | uint16_t sf; |
297 | uint16_t sf; |
298 | block_t *blk; |
298 | block_t *blk; |
299 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
299 | fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
300 | unsigned found = 0; /* top of the free cluster number stack */ |
300 | unsigned found = 0; /* top of the free cluster number stack */ |
301 | unsigned b, c, cl; |
301 | unsigned b, c, cl; |
302 | 302 | ||
303 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
303 | lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
304 | if (lifo) |
304 | if (lifo) |
305 | return ENOMEM; |
305 | return ENOMEM; |
306 | 306 | ||
307 | bps = uint16_t_le2host(bs->bps); |
307 | bps = uint16_t_le2host(bs->bps); |
308 | rscnt = uint16_t_le2host(bs->rscnt); |
308 | rscnt = uint16_t_le2host(bs->rscnt); |
309 | sf = uint16_t_le2host(bs->sec_per_fat); |
309 | sf = uint16_t_le2host(bs->sec_per_fat); |
310 | 310 | ||
311 | /* |
311 | /* |
312 | * Search FAT1 for unused clusters. |
312 | * Search FAT1 for unused clusters. |
313 | */ |
313 | */ |
314 | futex_down(&fat_alloc_lock); |
314 | futex_down(&fat_alloc_lock); |
315 | for (b = 0, cl = 0; b < sf; blk++) { |
315 | for (b = 0, cl = 0; b < sf; blk++) { |
316 | blk = block_get(dev_handle, rscnt + b, bps); |
316 | blk = block_get(dev_handle, rscnt + b, bps); |
317 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
317 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
318 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
318 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
319 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
319 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
320 | /* |
320 | /* |
321 | * The cluster is free. Put it into our stack |
321 | * The cluster is free. Put it into our stack |
322 | * of found clusters and mark it as non-free. |
322 | * of found clusters and mark it as non-free. |
323 | */ |
323 | */ |
324 | lifo[found] = cl; |
324 | lifo[found] = cl; |
325 | *clst = (found == 0) ? |
325 | *clst = (found == 0) ? |
326 | host2uint16_t_le(FAT_CLST_LAST1) : |
326 | host2uint16_t_le(FAT_CLST_LAST1) : |
327 | host2uint16_t_le(lifo[found - 1]); |
327 | host2uint16_t_le(lifo[found - 1]); |
328 | blk->dirty = true; /* need to sync block */ |
328 | blk->dirty = true; /* need to sync block */ |
329 | if (++found == nclsts) { |
329 | if (++found == nclsts) { |
330 | /* we are almost done */ |
330 | /* we are almost done */ |
331 | block_put(blk); |
331 | block_put(blk); |
332 | /* update the shadow copies of FAT */ |
332 | /* update the shadow copies of FAT */ |
333 | fat_alloc_shadow_clusters(bs, |
333 | fat_alloc_shadow_clusters(bs, |
334 | dev_handle, lifo, nclsts); |
334 | dev_handle, lifo, nclsts); |
335 | *mcl = lifo[found - 1]; |
335 | *mcl = lifo[found - 1]; |
336 | *lcl = lifo[0]; |
336 | *lcl = lifo[0]; |
337 | free(lifo); |
337 | free(lifo); |
338 | futex_up(&fat_alloc_lock); |
338 | futex_up(&fat_alloc_lock); |
339 | return EOK; |
339 | return EOK; |
340 | } |
340 | } |
341 | } |
341 | } |
342 | } |
342 | } |
343 | block_put(blk); |
343 | block_put(blk); |
344 | } |
344 | } |
345 | futex_up(&fat_alloc_lock); |
345 | futex_up(&fat_alloc_lock); |
346 | 346 | ||
347 | /* |
347 | /* |
348 | * We could not find enough clusters. Now we need to free the clusters |
348 | * We could not find enough clusters. Now we need to free the clusters |
349 | * we have allocated so far. |
349 | * we have allocated so far. |
350 | */ |
350 | */ |
351 | while (found--) { |
351 | while (found--) { |
352 | fat_mark_cluster(bs, dev_handle, FAT1, lifo[found], |
352 | fat_mark_cluster(bs, dev_handle, FAT1, lifo[found], |
353 | FAT_CLST_RES0); |
353 | FAT_CLST_RES0); |
354 | } |
354 | } |
355 | 355 | ||
356 | free(lifo); |
356 | free(lifo); |
357 | return ENOSPC; |
357 | return ENOSPC; |
358 | } |
358 | } |
359 | 359 | ||
360 | /** Append a cluster chain to the last file cluster in all FATs. |
360 | /** Append a cluster chain to the last file cluster in all FATs. |
361 | * |
361 | * |
362 | * @param bs Buffer holding boot sector of the file system. |
362 | * @param bs Buffer holding boot sector of the file system. |
363 | * @param nodep Node representing the file. |
363 | * @param nodep Node representing the file. |
364 | * @param mcl First cluster of the cluster chain to append. |
364 | * @param mcl First cluster of the cluster chain to append. |
365 | */ |
365 | */ |
366 | void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) |
366 | void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) |
367 | { |
367 | { |
368 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
368 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
369 | fat_cluster_t lcl; |
369 | fat_cluster_t lcl; |
370 | uint8_t fatno; |
370 | uint8_t fatno; |
371 | 371 | ||
372 | if (_fat_blcks_get(bs, dev_handle, nodep->firstc, &lcl) == 0) { |
372 | if (_fat_blcks_get(bs, dev_handle, nodep->firstc, &lcl) == 0) { |
373 | nodep->firstc = host2uint16_t_le(mcl); |
373 | nodep->firstc = host2uint16_t_le(mcl); |
374 | nodep->dirty = true; /* need to sync node */ |
374 | nodep->dirty = true; /* need to sync node */ |
375 | return; |
375 | return; |
376 | } |
376 | } |
377 | 377 | ||
378 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
378 | for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
379 | fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); |
379 | fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); |
380 | } |
380 | } |
381 | 381 | ||
382 | /** |
382 | /** |
383 | * @} |
383 | * @} |
384 | */ |
384 | */ |
385 | 385 |