Subversion Repositories HelenOS

Rev

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