Subversion Repositories HelenOS

Rev

Rev 3513 | Rev 3517 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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