Rev 3535 | Rev 3588 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3535 | Rev 3561 | ||
---|---|---|---|
Line 52... | Line 52... | ||
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 | /** Walk the cluster chain. |
58 | * |
58 | * |
59 | * @param bs Buffer holding the boot sector of the file system. |
59 | * @param bs Buffer holding the boot sector for the file. |
60 | * @param dev_handle Device handle of the file system. |
60 | * @param dev_handle Device handle of the device with the file. |
61 | * @param firstc First cluster used by the file. Can be zero if the file |
61 | * @param firstc First cluster to start the walk with. |
- | 62 | * @param penult If non-NULL, output argument hodling the |
|
- | 63 | * the penultimate cluster visited. |
|
- | 64 | * @param ult If non-NULL, output argument holding the |
|
62 | * is empty. |
65 | * ultimate cluster visited. |
63 | * @param offset Offset in blocks. |
66 | * @param max_clusters Maximum number of clusters to visit. |
64 | * |
67 | * |
65 | * @return Block structure holding the requested block. |
68 | * @return Number of clusters seen during the walk. |
66 | */ |
69 | */ |
67 | block_t * |
70 | uint16_t |
68 | _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
71 | fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
69 | off_t offset) |
72 | fat_cluster_t *penult, fat_cluster_t *ult, uint16_t max_clusters) |
70 | { |
73 | { |
71 | block_t *b; |
74 | block_t *b; |
72 | unsigned bps; |
75 | unsigned bps; |
73 | unsigned spc; |
- | |
74 | unsigned rscnt; /* block address of the first FAT */ |
76 | unsigned rscnt; /* block address of the first FAT */ |
75 | unsigned fatcnt; |
- | |
76 | unsigned rde; |
- | |
77 | unsigned rds; /* root directory size */ |
- | |
78 | unsigned sf; |
- | |
79 | unsigned ssa; /* size of the system area */ |
- | |
80 | unsigned clusters; |
77 | uint16_t clusters = 0; |
81 | fat_cluster_t clst = firstc; |
78 | fat_cluster_t clst = firstc; |
82 | unsigned i; |
- | |
83 | 79 | ||
84 | bps = uint16_t_le2host(bs->bps); |
80 | bps = uint16_t_le2host(bs->bps); |
85 | spc = bs->spc; |
- | |
86 | rscnt = uint16_t_le2host(bs->rscnt); |
81 | rscnt = uint16_t_le2host(bs->rscnt); |
87 | fatcnt = bs->fatcnt; |
- | |
88 | rde = uint16_t_le2host(bs->root_ent_max); |
- | |
89 | sf = uint16_t_le2host(bs->sec_per_fat); |
- | |
90 | 82 | ||
91 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
- | |
92 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
- | |
93 | ssa = rscnt + fatcnt * sf + rds; |
- | |
94 | - | ||
95 | if (firstc == FAT_CLST_ROOT) { |
83 | if (firstc == FAT_CLST_RES0) { |
96 | /* root directory special case */ |
84 | /* No space allocated to the file. */ |
97 | assert(offset < rds); |
85 | if (ult) |
98 | b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps); |
86 | *ult = firstc; |
99 | return b; |
87 | return 0; |
100 | } |
88 | } |
101 | 89 | ||
- | 90 | /* At this point, the meaning of penult is not well-defined. */ |
|
- | 91 | if (penult) |
|
102 | clusters = offset / spc; |
92 | *penult = FAT_CLST_RES0; |
- | 93 | ||
103 | for (i = 0; i < clusters; i++) { |
94 | while (clst < FAT_CLST_LAST1 && clusters < max_clusters) { |
104 | unsigned fsec; /* sector offset relative to FAT1 */ |
95 | unsigned fsec; /* sector offset relative to FAT1 */ |
105 | unsigned fidx; /* FAT1 entry index */ |
96 | unsigned fidx; /* FAT1 entry index */ |
106 | 97 | ||
107 | assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD); |
98 | assert(clst >= FAT_CLST_FIRST); |
- | 99 | if (penult) |
|
- | 100 | *penult = clst; /* remember the penultimate cluster */ |
|
108 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
101 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
109 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
102 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
110 | /* read FAT1 */ |
103 | /* read FAT1 */ |
111 | b = block_get(dev_handle, rscnt + fsec, bps); |
104 | b = block_get(dev_handle, rscnt + fsec); |
112 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
105 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
113 | assert(clst != FAT_CLST_BAD); |
106 | assert(clst != FAT_CLST_BAD); |
114 | assert(clst < FAT_CLST_LAST1); |
- | |
115 | block_put(b); |
107 | block_put(b); |
- | 108 | clusters++; |
|
116 | } |
109 | } |
117 | 110 | ||
118 | b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc + |
111 | if (ult) |
119 | offset % spc, bps); |
112 | *ult = clst; |
120 | 113 | ||
121 | return b; |
114 | return clusters; |
122 | } |
115 | } |
123 | 116 | ||
124 | /** Return number of blocks allocated to a file. |
117 | /** Read block from file located on a FAT file system. |
125 | * |
118 | * |
126 | * @param bs Buffer holding the boot sector for the file. |
119 | * @param bs Buffer holding the boot sector of the file system. |
127 | * @param dev_handle Device handle of the device with the file. |
120 | * @param dev_handle Device handle of the file system. |
128 | * @param firstc First cluster of the file. |
121 | * @param firstc First cluster used by the file. Can be zero if the file |
129 | * @param lastc If non-NULL, output argument holding the |
- | |
130 | * last cluster. |
122 | * is empty. |
- | 123 | * @param offset Offset in blocks. |
|
131 | * |
124 | * |
132 | * @return Number of blocks allocated to the file. |
125 | * @return Block structure holding the requested block. |
133 | */ |
126 | */ |
134 | uint16_t |
127 | block_t * |
135 | _fat_blcks_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
128 | _fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
136 | fat_cluster_t *lastc) |
129 | off_t offset) |
137 | { |
130 | { |
138 | block_t *b; |
131 | block_t *b; |
139 | unsigned bps; |
132 | unsigned bps; |
140 | unsigned spc; |
- | |
141 | unsigned rscnt; /* block address of the first FAT */ |
133 | unsigned rscnt; /* block address of the first FAT */ |
- | 134 | unsigned rde; |
|
- | 135 | unsigned rds; /* root directory size */ |
|
- | 136 | unsigned sf; |
|
- | 137 | unsigned ssa; /* size of the system area */ |
|
142 | unsigned clusters = 0; |
138 | unsigned clusters, max_clusters; |
143 | fat_cluster_t clst = firstc; |
139 | fat_cluster_t lastc, clst = firstc; |
144 | 140 | ||
145 | bps = uint16_t_le2host(bs->bps); |
141 | bps = uint16_t_le2host(bs->bps); |
146 | spc = bs->spc; |
- | |
147 | rscnt = uint16_t_le2host(bs->rscnt); |
142 | rscnt = uint16_t_le2host(bs->rscnt); |
- | 143 | rde = uint16_t_le2host(bs->root_ent_max); |
|
- | 144 | sf = uint16_t_le2host(bs->sec_per_fat); |
|
148 | 145 | ||
- | 146 | rds = (sizeof(fat_dentry_t) * rde) / bps; |
|
- | 147 | rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
|
- | 148 | ssa = rscnt + bs->fatcnt * sf + rds; |
|
- | 149 | ||
149 | if (firstc == FAT_CLST_RES0) { |
150 | if (firstc == FAT_CLST_ROOT) { |
150 | /* No space allocated to the file. */ |
151 | /* root directory special case */ |
151 | if (lastc) |
152 | assert(offset < rds); |
152 | *lastc = firstc; |
153 | b = block_get(dev_handle, rscnt + bs->fatcnt * sf + offset); |
153 | return 0; |
154 | return b; |
154 | } |
155 | } |
155 | 156 | ||
156 | while (clst < FAT_CLST_LAST1) { |
157 | max_clusters = offset / bs->spc; |
157 | unsigned fsec; /* sector offset relative to FAT1 */ |
158 | clusters = fat_cluster_walk(bs, dev_handle, firstc, NULL, &lastc, |
- | 159 | max_clusters); |
|
158 | unsigned fidx; /* FAT1 entry index */ |
160 | assert(clusters == max_clusters); |
159 | 161 | ||
160 | assert(clst >= FAT_CLST_FIRST); |
- | |
161 | if (lastc) |
- | |
162 | *lastc = clst; /* remember the last cluster */ |
- | |
163 | fsec = (clst * sizeof(fat_cluster_t)) / bps; |
- | |
164 | fidx = clst % (bps / sizeof(fat_cluster_t)); |
- | |
165 | /* read FAT1 */ |
- | |
166 | b = block_get(dev_handle, rscnt + fsec, bps); |
162 | b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc + |
167 | clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
- | |
168 | assert(clst != FAT_CLST_BAD); |
- | |
169 | block_put(b); |
163 | offset % bs->spc); |
170 | clusters++; |
- | |
171 | } |
- | |
172 | 164 | ||
173 | if (lastc) |
- | |
174 | *lastc = clst; |
- | |
175 | return clusters * spc; |
165 | return b; |
176 | } |
166 | } |
177 | 167 | ||
- | 168 | ||
178 | /** Fill the gap between EOF and a new file position. |
169 | /** Fill the gap between EOF and a new file position. |
179 | * |
170 | * |
180 | * @param bs Buffer holding the boot sector for nodep. |
171 | * @param bs Buffer holding the boot sector for nodep. |
181 | * @param nodep FAT node with the gap. |
172 | * @param nodep FAT node with the gap. |
182 | * @param mcl First cluster in an independent cluster chain that will |
173 | * @param mcl First cluster in an independent cluster chain that will |
Line 241... | Line 232... | ||
241 | rscnt = uint16_t_le2host(bs->rscnt); |
232 | rscnt = uint16_t_le2host(bs->rscnt); |
242 | sf = uint16_t_le2host(bs->sec_per_fat); |
233 | sf = uint16_t_le2host(bs->sec_per_fat); |
243 | 234 | ||
244 | assert(fatno < bs->fatcnt); |
235 | assert(fatno < bs->fatcnt); |
245 | b = block_get(dev_handle, rscnt + sf * fatno + |
236 | b = block_get(dev_handle, rscnt + sf * fatno + |
246 | (clst * sizeof(fat_cluster_t)) / bps, bps); |
237 | (clst * sizeof(fat_cluster_t)) / bps); |
247 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
238 | cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
248 | *cp = host2uint16_t_le(value); |
239 | *cp = host2uint16_t_le(value); |
249 | b->dirty = true; /* need to sync block */ |
240 | b->dirty = true; /* need to sync block */ |
250 | block_put(b); |
241 | block_put(b); |
251 | } |
242 | } |
Line 311... | Line 302... | ||
311 | /* |
302 | /* |
312 | * Search FAT1 for unused clusters. |
303 | * Search FAT1 for unused clusters. |
313 | */ |
304 | */ |
314 | futex_down(&fat_alloc_lock); |
305 | futex_down(&fat_alloc_lock); |
315 | for (b = 0, cl = 0; b < sf; blk++) { |
306 | for (b = 0, cl = 0; b < sf; blk++) { |
316 | blk = block_get(dev_handle, rscnt + b, bps); |
307 | blk = block_get(dev_handle, rscnt + b); |
317 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
308 | for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
318 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
309 | fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
319 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
310 | if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
320 | /* |
311 | /* |
321 | * The cluster is free. Put it into our stack |
312 | * The cluster is free. Put it into our stack |
Line 367... | Line 358... | ||
367 | { |
358 | { |
368 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
359 | dev_handle_t dev_handle = nodep->idx->dev_handle; |
369 | fat_cluster_t lcl; |
360 | fat_cluster_t lcl; |
370 | uint8_t fatno; |
361 | uint8_t fatno; |
371 | 362 | ||
372 | if (_fat_blcks_get(bs, dev_handle, nodep->firstc, &lcl) == 0) { |
363 | if (fat_cluster_walk(bs, nodep->idx->dev_handle, nodep->firstc, &lcl, |
- | 364 | NULL, (uint16_t) -1) == 0) { |
|
- | 365 | /* No clusters allocated to the node yet. */ |
|
373 | nodep->firstc = host2uint16_t_le(mcl); |
366 | nodep->firstc = host2uint16_t_le(mcl); |
374 | nodep->dirty = true; /* need to sync node */ |
367 | nodep->dirty = true; /* need to sync node */ |
375 | return; |
368 | return; |
376 | } |
369 | } |
377 | 370 |