source: mainline/uspace/srv/fs/fat/fat_fat.c@ 913a821c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 913a821c was 913a821c, checked in by Jakub Jermar <jakub@…>, 17 years ago

Finish implementation of fat_truncate() and fix several warnings.

  • Property mode set to 100644
File size: 13.3 KB
RevLine 
[6ebaff9]1/*
2 * Copyright (c) 2008 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33/**
34 * @file fat_fat.c
[0ec862d]35 * @brief Functions that manipulate the File Allocation Tables.
[6ebaff9]36 */
37
[0f57d0e]38#include "fat_fat.h"
39#include "fat_dentry.h"
40#include "fat.h"
41#include "../../vfs/vfs.h"
42#include <libfs.h>
[fc840d9]43#include <libblock.h>
[0f57d0e]44#include <errno.h>
45#include <byteorder.h>
46#include <align.h>
47#include <assert.h>
[0ec862d]48#include <futex.h>
49
50/**
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
53 * deallocation of clusters.
54 */
55static futex_t fat_alloc_lock = FUTEX_INITIALIZER;
[0f57d0e]56
[4f1c0b4]57/** Walk the cluster chain.
[9f95a80]58 *
[4f1c0b4]59 * @param bs Buffer holding the boot sector for the file.
60 * @param dev_handle Device handle of the device with the file.
61 * @param firstc First cluster to start the walk with.
[6c8d267]62 * @param lastc If non-NULL, output argument hodling the last cluster number visited.
[4f1c0b4]63 * @param max_clusters Maximum number of clusters to visit.
[9f95a80]64 *
[4f1c0b4]65 * @return Number of clusters seen during the walk.
[9f95a80]66 */
[4f1c0b4]67uint16_t
68fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
[6c8d267]69 fat_cluster_t *lastc, uint16_t max_clusters)
[0f57d0e]70{
71 block_t *b;
72 unsigned bps;
73 unsigned rscnt; /* block address of the first FAT */
[4f1c0b4]74 uint16_t clusters = 0;
[0f57d0e]75 fat_cluster_t clst = firstc;
76
[cb682eb]77 bps = uint16_t_le2host(bs->bps);
78 rscnt = uint16_t_le2host(bs->rscnt);
[0f57d0e]79
[4f1c0b4]80 if (firstc == FAT_CLST_RES0) {
81 /* No space allocated to the file. */
[6c8d267]82 if (lastc)
83 *lastc = firstc;
[4f1c0b4]84 return 0;
[0f57d0e]85 }
86
[4f1c0b4]87 while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
[6c8d267]88 bn_t fsec; /* sector offset relative to FAT1 */
[0f57d0e]89 unsigned fidx; /* FAT1 entry index */
90
[4f1c0b4]91 assert(clst >= FAT_CLST_FIRST);
[6c8d267]92 if (lastc)
93 *lastc = clst; /* remember the last cluster number */
[0f57d0e]94 fsec = (clst * sizeof(fat_cluster_t)) / bps;
95 fidx = clst % (bps / sizeof(fat_cluster_t));
96 /* read FAT1 */
[6132b59]97 b = block_get(dev_handle, rscnt + fsec);
[0f57d0e]98 clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
99 assert(clst != FAT_CLST_BAD);
100 block_put(b);
[4f1c0b4]101 clusters++;
[0f57d0e]102 }
103
[6c8d267]104 if (lastc && clst < FAT_CLST_LAST1)
105 *lastc = clst;
[0f57d0e]106
[4f1c0b4]107 return clusters;
[0f57d0e]108}
109
[4f1c0b4]110/** Read block from file located on a FAT file system.
[0f57d0e]111 *
[4f1c0b4]112 * @param bs Buffer holding the boot sector 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
115 * is empty.
[6c8d267]116 * @param bn Block number.
[0f57d0e]117 *
[4f1c0b4]118 * @return Block structure holding the requested block.
[0f57d0e]119 */
[4f1c0b4]120block_t *
121_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
[6c8d267]122 bn_t bn)
[0f57d0e]123{
124 block_t *b;
125 unsigned bps;
126 unsigned rscnt; /* block address of the first FAT */
[4f1c0b4]127 unsigned rde;
128 unsigned rds; /* root directory size */
129 unsigned sf;
130 unsigned ssa; /* size of the system area */
131 unsigned clusters, max_clusters;
[913a821c]132 fat_cluster_t lastc;
[0f57d0e]133
[cb682eb]134 bps = uint16_t_le2host(bs->bps);
135 rscnt = uint16_t_le2host(bs->rscnt);
[4f1c0b4]136 rde = uint16_t_le2host(bs->root_ent_max);
137 sf = uint16_t_le2host(bs->sec_per_fat);
[0f57d0e]138
[4f1c0b4]139 rds = (sizeof(fat_dentry_t) * rde) / bps;
140 rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
141 ssa = rscnt + bs->fatcnt * sf + rds;
142
143 if (firstc == FAT_CLST_ROOT) {
144 /* root directory special case */
[6c8d267]145 assert(bn < rds);
146 b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn);
[4f1c0b4]147 return b;
[0f57d0e]148 }
149
[6c8d267]150 max_clusters = bn / bs->spc;
151 clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
[4f1c0b4]152 max_clusters);
153 assert(clusters == max_clusters);
[0f57d0e]154
[4f1c0b4]155 b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc +
[6c8d267]156 bn % bs->spc);
[0f57d0e]157
[4f1c0b4]158 return b;
[0f57d0e]159}
160
161/** Fill the gap between EOF and a new file position.
162 *
[cb682eb]163 * @param bs Buffer holding the boot sector for nodep.
[0f57d0e]164 * @param nodep FAT node with the gap.
165 * @param mcl First cluster in an independent cluster chain that will
166 * be later appended to the end of the node's own cluster
167 * chain. If pos is still in the last allocated cluster,
168 * this argument is ignored.
169 * @param pos Position in the last node block.
170 */
[cb682eb]171void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
[0f57d0e]172{
173 uint16_t bps;
174 unsigned spc;
[cb682eb]175 block_t *b;
[0f57d0e]176 off_t o, boundary;
177
[cb682eb]178 bps = uint16_t_le2host(bs->bps);
179 spc = bs->spc;
[0f57d0e]180
181 boundary = ROUND_UP(nodep->size, bps * spc);
182
183 /* zero out already allocated space */
184 for (o = nodep->size - 1; o < pos && o < boundary;
185 o = ALIGN_DOWN(o + bps, bps)) {
[cb682eb]186 b = fat_block_get(bs, nodep, o / bps);
[0f57d0e]187 memset(b->data + o % bps, 0, bps - o % bps);
188 b->dirty = true; /* need to sync node */
189 block_put(b);
190 }
191
192 if (o >= pos)
193 return;
194
195 /* zero out the initial part of the new cluster chain */
196 for (o = boundary; o < pos; o += bps) {
[cb682eb]197 b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
[0f57d0e]198 (o - boundary) / bps);
199 memset(b->data, 0, min(bps, pos - o));
200 b->dirty = true; /* need to sync node */
201 block_put(b);
202 }
203}
204
[913a821c]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 */
213fat_cluster_t
214fat_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
232/** Set cluster in one instance of FAT.
[9f95a80]233 *
234 * @param bs Buffer holding the boot sector for the file system.
235 * @param dev_handle Device handle for the file system.
236 * @param fatno Number of the FAT instance where to make the change.
[913a821c]237 * @param clst Cluster which is to be set.
238 * @param value Value to set the cluster with.
[9f95a80]239 */
[0f57d0e]240void
[913a821c]241fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
[cb682eb]242 fat_cluster_t clst, fat_cluster_t value)
[0f57d0e]243{
[cb682eb]244 block_t *b;
[23b56ca]245 uint16_t bps;
246 uint16_t rscnt;
247 uint16_t sf;
248 fat_cluster_t *cp;
249
[cb682eb]250 bps = uint16_t_le2host(bs->bps);
251 rscnt = uint16_t_le2host(bs->rscnt);
252 sf = uint16_t_le2host(bs->sec_per_fat);
[23b56ca]253
[cb682eb]254 assert(fatno < bs->fatcnt);
255 b = block_get(dev_handle, rscnt + sf * fatno +
[6132b59]256 (clst * sizeof(fat_cluster_t)) / bps);
[cb682eb]257 cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
[23b56ca]258 *cp = host2uint16_t_le(value);
[cb682eb]259 b->dirty = true; /* need to sync block */
260 block_put(b);
[0f57d0e]261}
262
[9f95a80]263/** Replay the allocatoin of clusters in all shadow instances of FAT.
264 *
265 * @param bs Buffer holding the boot sector of the file system.
266 * @param dev_handle Device handle of the file system.
267 * @param lifo Chain of allocated clusters.
268 * @param nclsts Number of clusters in the lifo chain.
269 */
[cb682eb]270void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
271 fat_cluster_t *lifo, unsigned nclsts)
[0f57d0e]272{
[b1178d0]273 uint8_t fatno;
274 unsigned c;
275
[cb682eb]276 for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
[b1178d0]277 for (c = 0; c < nclsts; c++) {
[913a821c]278 fat_set_cluster(bs, dev_handle, fatno, lifo[c],
[b1178d0]279 c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
280 }
281 }
[0f57d0e]282}
283
[9f95a80]284/** Allocate clusters in FAT1.
285 *
286 * This function will attempt to allocate the requested number of clusters in
287 * the first FAT instance. The FAT will be altered so that the allocated
288 * clusters form an independent chain (i.e. a chain which does not belong to any
289 * file yet).
290 *
291 * @param bs Buffer holding the boot sector of the file system.
292 * @param dev_handle Device handle of the file system.
293 * @param nclsts Number of clusters to allocate.
294 * @param mcl Output parameter where the first cluster in the chain
295 * will be returned.
296 * @param lcl Output parameter where the last cluster in the chain
297 * will be returned.
298 *
299 * @return EOK on success, a negative error code otherwise.
300 */
[0f57d0e]301int
[cb682eb]302fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
303 fat_cluster_t *mcl, fat_cluster_t *lcl)
[0f57d0e]304{
305 uint16_t bps;
306 uint16_t rscnt;
307 uint16_t sf;
[cb682eb]308 block_t *blk;
[0f57d0e]309 fat_cluster_t *lifo; /* stack for storing free cluster numbers */
310 unsigned found = 0; /* top of the free cluster number stack */
311 unsigned b, c, cl;
312
313 lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
314 if (lifo)
315 return ENOMEM;
316
[cb682eb]317 bps = uint16_t_le2host(bs->bps);
318 rscnt = uint16_t_le2host(bs->rscnt);
319 sf = uint16_t_le2host(bs->sec_per_fat);
[0f57d0e]320
321 /*
322 * Search FAT1 for unused clusters.
323 */
[0ec862d]324 futex_down(&fat_alloc_lock);
[0f57d0e]325 for (b = 0, cl = 0; b < sf; blk++) {
[6132b59]326 blk = block_get(dev_handle, rscnt + b);
[0f57d0e]327 for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
328 fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
[a429bfb]329 if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
[0f57d0e]330 /*
331 * The cluster is free. Put it into our stack
332 * of found clusters and mark it as non-free.
333 */
334 lifo[found] = cl;
[a429bfb]335 *clst = (found == 0) ?
336 host2uint16_t_le(FAT_CLST_LAST1) :
337 host2uint16_t_le(lifo[found - 1]);
[0f57d0e]338 blk->dirty = true; /* need to sync block */
339 if (++found == nclsts) {
340 /* we are almost done */
341 block_put(blk);
342 /* update the shadow copies of FAT */
[cb682eb]343 fat_alloc_shadow_clusters(bs,
344 dev_handle, lifo, nclsts);
[0f57d0e]345 *mcl = lifo[found - 1];
346 *lcl = lifo[0];
347 free(lifo);
[0ec862d]348 futex_up(&fat_alloc_lock);
[0f57d0e]349 return EOK;
350 }
351 }
352 }
353 block_put(blk);
354 }
[0ec862d]355 futex_up(&fat_alloc_lock);
[0f57d0e]356
357 /*
358 * We could not find enough clusters. Now we need to free the clusters
359 * we have allocated so far.
360 */
[cb682eb]361 while (found--) {
[913a821c]362 fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
[cb682eb]363 FAT_CLST_RES0);
364 }
[0f57d0e]365
366 free(lifo);
367 return ENOSPC;
368}
369
[913a821c]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 */
376void
377fat_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
[9f95a80]393/** Append a cluster chain to the last file cluster in all FATs.
394 *
[913a821c]395 * @param bs Buffer holding the boot sector of the file system.
[9f95a80]396 * @param nodep Node representing the file.
397 * @param mcl First cluster of the cluster chain to append.
398 */
[cb682eb]399void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
[0f57d0e]400{
[cb682eb]401 dev_handle_t dev_handle = nodep->idx->dev_handle;
[e17d986]402 fat_cluster_t lcl;
[cb682eb]403 uint8_t fatno;
[e17d986]404
[913a821c]405 if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
[6c8d267]406 (uint16_t) -1) == 0) {
[4f1c0b4]407 /* No clusters allocated to the node yet. */
[e17d986]408 nodep->firstc = host2uint16_t_le(mcl);
409 nodep->dirty = true; /* need to sync node */
410 return;
411 }
412
[cb682eb]413 for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
[913a821c]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 */
425void 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 }
[0f57d0e]446}
[6ebaff9]447
448/**
449 * @}
450 */
Note: See TracBrowser for help on using the repository browser.