Subversion Repositories HelenOS

Rev

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