Subversion Repositories HelenOS

Rev

Rev 3009 | Rev 3535 | 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_ops.c
  35.  * @brief   Implementation of VFS operations for the FAT file system server.
  36.  */
  37.  
  38. #include "fat.h"
  39. #include "../../vfs/vfs.h"
  40. #include <libfs.h>
  41. #include <ipc/ipc.h>
  42. #include <async.h>
  43. #include <errno.h>
  44. #include <string.h>
  45. #include <byteorder.h>
  46. #include <libadt/hash_table.h>
  47. #include <libadt/list.h>
  48. #include <assert.h>
  49. #include <futex.h>
  50.  
  51. #define BS_BLOCK        0
  52.  
  53. /** Futex protecting the list of cached free FAT nodes. */
  54. static futex_t ffn_futex = FUTEX_INITIALIZER;
  55.  
  56. /** List of cached free FAT nodes. */
  57. static LIST_INITIALIZE(ffn_head);
  58.  
  59. #define FAT_NAME_LEN        8
  60. #define FAT_EXT_LEN     3
  61.  
  62. #define FAT_PAD         ' '
  63.  
  64. #define FAT_DENTRY_UNUSED   0x00
  65. #define FAT_DENTRY_E5_ESC   0x05
  66. #define FAT_DENTRY_DOT      0x2e
  67. #define FAT_DENTRY_ERASED   0xe5
  68.  
  69. static void dentry_name_canonify(fat_dentry_t *d, char *buf)
  70. {
  71.     int i;
  72.  
  73.     for (i = 0; i < FAT_NAME_LEN; i++) {
  74.         if (d->name[i] == FAT_PAD) {
  75.             buf++;
  76.             break;
  77.         }
  78.         if (d->name[i] == FAT_DENTRY_E5_ESC)
  79.             *buf++ = 0xe5;
  80.         else
  81.             *buf++ = d->name[i];
  82.     }
  83.     if (d->ext[0] != FAT_PAD)
  84.         *buf++ = '.';
  85.     for (i = 0; i < FAT_EXT_LEN; i++) {
  86.         if (d->ext[i] == FAT_PAD) {
  87.             *buf = '\0';
  88.             return;
  89.         }
  90.         if (d->ext[i] == FAT_DENTRY_E5_ESC)
  91.             *buf++ = 0xe5;
  92.         else
  93.             *buf++ = d->ext[i];
  94.     }
  95. }
  96.  
  97. /* TODO move somewhere else */
  98. typedef struct {
  99.     void *data;
  100. } block_t;
  101.  
  102. static block_t *block_get(dev_handle_t dev_handle, off_t offset)
  103. {
  104.     return NULL;    /* TODO */
  105. }
  106.  
  107. static void block_put(block_t *block)
  108. {
  109.     /* TODO */
  110. }
  111.  
  112. #define FAT_BS(b)       ((fat_bs_t *)((b)->data))
  113.  
  114. #define FAT_CLST_RES0   0x0000
  115. #define FAT_CLST_RES1   0x0001
  116. #define FAT_CLST_FIRST  0x0002
  117. #define FAT_CLST_BAD    0xfff7
  118. #define FAT_CLST_LAST1  0xfff8
  119. #define FAT_CLST_LAST8  0xffff
  120.  
  121. /* internally used to mark root directory's parent */
  122. #define FAT_CLST_ROOTPAR    FAT_CLST_RES0
  123. /* internally used to mark root directory */
  124. #define FAT_CLST_ROOT       FAT_CLST_RES1
  125.  
  126. #define fat_block_get(np, off) \
  127.     _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
  128.  
  129. static block_t *
  130. _fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
  131. {
  132.     block_t *bb;
  133.     block_t *b;
  134.     unsigned bps;
  135.     unsigned spc;
  136.     unsigned rscnt;     /* block address of the first FAT */
  137.     unsigned fatcnt;
  138.     unsigned rde;
  139.     unsigned rds;       /* root directory size */
  140.     unsigned sf;
  141.     unsigned ssa;       /* size of the system area */
  142.     unsigned clusters;
  143.     fat_cluster_t clst = firstc;
  144.     unsigned i;
  145.  
  146.     bb = block_get(dev_handle, BS_BLOCK);
  147.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  148.     spc = FAT_BS(bb)->spc;
  149.     rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
  150.     fatcnt = FAT_BS(bb)->fatcnt;
  151.     rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
  152.     sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
  153.     block_put(bb);
  154.  
  155.     rds = (sizeof(fat_dentry_t) * rde) / bps;
  156.     rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
  157.     ssa = rscnt + fatcnt * sf + rds;
  158.  
  159.     if (firstc == FAT_CLST_ROOT) {
  160.         /* root directory special case */
  161.         assert(offset < rds);
  162.         b = block_get(dev_handle, rscnt + fatcnt * sf + offset);
  163.         return b;
  164.     }
  165.  
  166.     clusters = offset / spc;
  167.     for (i = 0; i < clusters; i++) {
  168.         unsigned fsec;  /* sector offset relative to FAT1 */
  169.         unsigned fidx;  /* FAT1 entry index */
  170.  
  171.         assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
  172.         fsec = (clst * sizeof(fat_cluster_t)) / bps;
  173.         fidx = clst % (bps / sizeof(fat_cluster_t));
  174.         /* read FAT1 */
  175.         b = block_get(dev_handle, rscnt + fsec);
  176.         clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
  177.         assert(clst != FAT_CLST_BAD);
  178.         assert(clst < FAT_CLST_LAST1);
  179.         block_put(b);
  180.     }
  181.  
  182.     b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
  183.         offset % spc);
  184.  
  185.     return b;
  186. }
  187.  
  188. static void fat_node_initialize(fat_node_t *node)
  189. {
  190.     futex_initialize(&node->lock, 1);
  191.     node->idx = NULL;
  192.     node->type = 0;
  193.     link_initialize(&node->ffn_link);
  194.     node->size = 0;
  195.     node->lnkcnt = 0;
  196.     node->refcnt = 0;
  197.     node->dirty = false;
  198. }
  199.  
  200. static uint16_t fat_bps_get(dev_handle_t dev_handle)
  201. {
  202.     block_t *bb;
  203.     uint16_t bps;
  204.    
  205.     bb = block_get(dev_handle, BS_BLOCK);
  206.     assert(bb != NULL);
  207.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  208.     block_put(bb);
  209.  
  210.     return bps;
  211. }
  212.  
  213. typedef enum {
  214.     FAT_DENTRY_SKIP,
  215.     FAT_DENTRY_LAST,
  216.     FAT_DENTRY_VALID
  217. } fat_dentry_clsf_t;
  218.  
  219. static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
  220. {
  221.     if (d->attr & FAT_ATTR_VOLLABEL) {
  222.         /* volume label entry */
  223.         return FAT_DENTRY_SKIP;
  224.     }
  225.     if (d->name[0] == FAT_DENTRY_ERASED) {
  226.         /* not-currently-used entry */
  227.         return FAT_DENTRY_SKIP;
  228.     }
  229.     if (d->name[0] == FAT_DENTRY_UNUSED) {
  230.         /* never used entry */
  231.         return FAT_DENTRY_LAST;
  232.     }
  233.     if (d->name[0] == FAT_DENTRY_DOT) {
  234.         /*
  235.          * Most likely '.' or '..'.
  236.          * It cannot occur in a regular file name.
  237.          */
  238.         return FAT_DENTRY_SKIP;
  239.     }
  240.     return FAT_DENTRY_VALID;
  241. }
  242.  
  243. static void fat_node_sync(fat_node_t *node)
  244. {
  245.     /* TODO */
  246. }
  247.  
  248. /** Internal version of fat_node_get().
  249.  *
  250.  * @param idxp      Locked index structure.
  251.  */
  252. static void *fat_node_get_core(fat_idx_t *idxp)
  253. {
  254.     block_t *b;
  255.     fat_dentry_t *d;
  256.     fat_node_t *nodep;
  257.     unsigned bps;
  258.     unsigned dps;
  259.  
  260.     if (idxp->nodep) {
  261.         /*
  262.          * We are lucky.
  263.          * The node is already instantiated in memory.
  264.          */
  265.         futex_down(&idxp->nodep->lock);
  266.         if (!idxp->nodep->refcnt++)
  267.             list_remove(&nodep->ffn_link);
  268.         futex_up(&idxp->nodep->lock);
  269.         return idxp->nodep;
  270.     }
  271.  
  272.     /*
  273.      * We must instantiate the node from the file system.
  274.      */
  275.    
  276.     assert(idxp->pfc);
  277.  
  278.     futex_down(&ffn_futex);
  279.     if (!list_empty(&ffn_head)) {
  280.         /* Try to use a cached free node structure. */
  281.         fat_idx_t *idxp_tmp;
  282.         nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
  283.         if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
  284.             goto skip_cache;
  285.         idxp_tmp = nodep->idx;
  286.         if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
  287.             futex_up(&nodep->lock);
  288.             goto skip_cache;
  289.         }
  290.         list_remove(&nodep->ffn_link);
  291.         futex_up(&ffn_futex);
  292.         if (nodep->dirty)
  293.             fat_node_sync(nodep);
  294.         idxp_tmp->nodep = NULL;
  295.         futex_up(&nodep->lock);
  296.         futex_up(&idxp_tmp->lock);
  297.     } else {
  298. skip_cache:
  299.         /* Try to allocate a new node structure. */
  300.         futex_up(&ffn_futex);
  301.         nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
  302.         if (!nodep)
  303.             return NULL;
  304.     }
  305.     fat_node_initialize(nodep);
  306.  
  307.     bps = fat_bps_get(idxp->dev_handle);
  308.     dps = bps / sizeof(fat_dentry_t);
  309.  
  310.     /* Read the block that contains the dentry of interest. */
  311.     b = _fat_block_get(idxp->dev_handle, idxp->pfc,
  312.         (idxp->pdi * sizeof(fat_dentry_t)) / bps);
  313.     assert(b);
  314.  
  315.     d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
  316.     if (d->attr & FAT_ATTR_SUBDIR) {
  317.         /*
  318.          * The only directory which does not have this bit set is the
  319.          * root directory itself. The root directory node is handled
  320.          * and initialized elsewhere.
  321.          */
  322.         nodep->type = FAT_DIRECTORY;
  323.     } else {
  324.         nodep->type = FAT_FILE;
  325.     }
  326.     nodep->firstc = uint16_t_le2host(d->firstc);
  327.     nodep->size = uint32_t_le2host(d->size);
  328.     nodep->lnkcnt = 1;
  329.     nodep->refcnt = 1;
  330.  
  331.     block_put(b);
  332.  
  333.     /* Link the idx structure with the node structure. */
  334.     nodep->idx = idxp;
  335.     idxp->nodep = nodep;
  336.  
  337.     return nodep;
  338. }
  339.  
  340. /** Instantiate a FAT in-core node. */
  341. static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
  342. {
  343.     void *node;
  344.     fat_idx_t *idxp;
  345.  
  346.     idxp = fat_idx_get_by_index(dev_handle, index);
  347.     if (!idxp)
  348.         return NULL;
  349.     /* idxp->lock held */
  350.     node = fat_node_get_core(idxp);
  351.     futex_up(&idxp->lock);
  352.     return node;
  353. }
  354.  
  355. static void fat_node_put(void *node)
  356. {
  357.     fat_node_t *nodep = (fat_node_t *)node;
  358.  
  359.     futex_down(&nodep->lock);
  360.     if (!--nodep->refcnt) {
  361.         futex_down(&ffn_futex);
  362.         list_append(&nodep->ffn_link, &ffn_head);
  363.         futex_up(&ffn_futex);
  364.     }
  365.     futex_up(&nodep->lock);
  366. }
  367.  
  368. static void *fat_create(int flags)
  369. {
  370.     return NULL;    /* not supported at the moment */
  371. }
  372.  
  373. static int fat_destroy(void *node)
  374. {
  375.     return ENOTSUP; /* not supported at the moment */
  376. }
  377.  
  378. static bool fat_link(void *prnt, void *chld, const char *name)
  379. {
  380.     return false;   /* not supported at the moment */
  381. }
  382.  
  383. static int fat_unlink(void *prnt, void *chld)
  384. {
  385.     return ENOTSUP; /* not supported at the moment */
  386. }
  387.  
  388. static void *fat_match(void *prnt, const char *component)
  389. {
  390.     fat_node_t *parentp = (fat_node_t *)prnt;
  391.     char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
  392.     unsigned i, j;
  393.     unsigned bps;       /* bytes per sector */
  394.     unsigned dps;       /* dentries per sector */
  395.     unsigned blocks;
  396.     fat_dentry_t *d;
  397.     block_t *b;
  398.  
  399.     futex_down(&parentp->idx->lock);
  400.     bps = fat_bps_get(parentp->idx->dev_handle);
  401.     dps = bps / sizeof(fat_dentry_t);
  402.     blocks = parentp->size / bps + (parentp->size % bps != 0);
  403.     for (i = 0; i < blocks; i++) {
  404.         unsigned dentries;
  405.        
  406.         b = fat_block_get(parentp, i);
  407.         dentries = (i == blocks - 1) ?
  408.             parentp->size % sizeof(fat_dentry_t) :
  409.             dps;
  410.         for (j = 0; j < dentries; j++) {
  411.             d = ((fat_dentry_t *)b->data) + j;
  412.             switch (fat_classify_dentry(d)) {
  413.             case FAT_DENTRY_SKIP:
  414.                 continue;
  415.             case FAT_DENTRY_LAST:
  416.                 block_put(b);
  417.                 futex_up(&parentp->idx->lock);
  418.                 return NULL;
  419.             default:
  420.             case FAT_DENTRY_VALID:
  421.                 dentry_name_canonify(d, name);
  422.                 break;
  423.             }
  424.             if (strcmp(name, component) == 0) {
  425.                 /* hit */
  426.                 void *node;
  427.                 /*
  428.                  * Assume tree hierarchy for locking.  We
  429.                  * already have the parent and now we are going
  430.                  * to lock the child.  Never lock in the oposite
  431.                  * order.
  432.                  */
  433.                 fat_idx_t *idx = fat_idx_get_by_pos(
  434.                     parentp->idx->dev_handle, parentp->firstc,
  435.                     i * dps + j);
  436.                 futex_up(&parentp->idx->lock);
  437.                 if (!idx) {
  438.                     /*
  439.                      * Can happen if memory is low or if we
  440.                      * run out of 32-bit indices.
  441.                      */
  442.                     block_put(b);
  443.                     return NULL;
  444.                 }
  445.                 node = fat_node_get_core(idx);
  446.                 futex_up(&idx->lock);
  447.                 block_put(b);
  448.                 return node;
  449.             }
  450.         }
  451.         block_put(b);
  452.     }
  453.     futex_up(&parentp->idx->lock);
  454.     return NULL;
  455. }
  456.  
  457. static fs_index_t fat_index_get(void *node)
  458. {
  459.     fat_node_t *fnodep = (fat_node_t *)node;
  460.     if (!fnodep)
  461.         return 0;
  462.     return fnodep->idx->index;
  463. }
  464.  
  465. static size_t fat_size_get(void *node)
  466. {
  467.     return ((fat_node_t *)node)->size;
  468. }
  469.  
  470. static unsigned fat_lnkcnt_get(void *node)
  471. {
  472.     return ((fat_node_t *)node)->lnkcnt;
  473. }
  474.  
  475. static bool fat_has_children(void *node)
  476. {
  477.     fat_node_t *nodep = (fat_node_t *)node;
  478.     unsigned bps;
  479.     unsigned dps;
  480.     unsigned blocks;
  481.     block_t *b;
  482.     unsigned i, j;
  483.  
  484.     if (nodep->type != FAT_DIRECTORY)
  485.         return false;
  486.  
  487.     futex_down(&nodep->idx->lock);
  488.     bps = fat_bps_get(nodep->idx->dev_handle);
  489.     dps = bps / sizeof(fat_dentry_t);
  490.  
  491.     blocks = nodep->size / bps + (nodep->size % bps != 0);
  492.  
  493.     for (i = 0; i < blocks; i++) {
  494.         unsigned dentries;
  495.         fat_dentry_t *d;
  496.    
  497.         b = fat_block_get(nodep, i);
  498.         dentries = (i == blocks - 1) ?
  499.             nodep->size % sizeof(fat_dentry_t) :
  500.             dps;
  501.         for (j = 0; j < dentries; j++) {
  502.             d = ((fat_dentry_t *)b->data) + j;
  503.             switch (fat_classify_dentry(d)) {
  504.             case FAT_DENTRY_SKIP:
  505.                 continue;
  506.             case FAT_DENTRY_LAST:
  507.                 block_put(b);
  508.                 futex_up(&nodep->idx->lock);
  509.                 return false;
  510.             default:
  511.             case FAT_DENTRY_VALID:
  512.                 block_put(b);
  513.                 futex_up(&nodep->idx->lock);
  514.                 return true;
  515.             }
  516.             block_put(b);
  517.             futex_up(&nodep->idx->lock);
  518.             return true;
  519.         }
  520.         block_put(b);
  521.     }
  522.  
  523.     futex_up(&nodep->idx->lock);
  524.     return false;
  525. }
  526.  
  527. static void *fat_root_get(dev_handle_t dev_handle)
  528. {
  529.     return fat_node_get(dev_handle, 0);
  530. }
  531.  
  532. static char fat_plb_get_char(unsigned pos)
  533. {
  534.     return fat_reg.plb_ro[pos % PLB_SIZE];
  535. }
  536.  
  537. static bool fat_is_directory(void *node)
  538. {
  539.     return ((fat_node_t *)node)->type == FAT_DIRECTORY;
  540. }
  541.  
  542. static bool fat_is_file(void *node)
  543. {
  544.     return ((fat_node_t *)node)->type == FAT_FILE;
  545. }
  546.  
  547. /** libfs operations */
  548. libfs_ops_t fat_libfs_ops = {
  549.     .match = fat_match,
  550.     .node_get = fat_node_get,
  551.     .node_put = fat_node_put,
  552.     .create = fat_create,
  553.     .destroy = fat_destroy,
  554.     .link = fat_link,
  555.     .unlink = fat_unlink,
  556.     .index_get = fat_index_get,
  557.     .size_get = fat_size_get,
  558.     .lnkcnt_get = fat_lnkcnt_get,
  559.     .has_children = fat_has_children,
  560.     .root_get = fat_root_get,
  561.     .plb_get_char = fat_plb_get_char,
  562.     .is_directory = fat_is_directory,
  563.     .is_file = fat_is_file
  564. };
  565.  
  566. void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
  567. {
  568.     dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
  569.     block_t *bb;
  570.     uint16_t rde;
  571.     int rc;
  572.  
  573.     /* Read the number of root directory entries. */
  574.     bb = block_get(dev_handle, BS_BLOCK);
  575.     rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
  576.     block_put(bb);
  577.  
  578.     rc = fat_idx_init_by_dev_handle(dev_handle);
  579.     if (rc != EOK) {
  580.         ipc_answer_0(rid, rc);
  581.         return;
  582.     }
  583.  
  584.     /* Initialize the root node. */
  585.     fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
  586.     if (!rootp) {
  587.         fat_idx_fini_by_dev_handle(dev_handle);
  588.         ipc_answer_0(rid, ENOMEM);
  589.         return;
  590.     }
  591.     fat_node_initialize(rootp);
  592.  
  593.     fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
  594.     if (!ridxp) {
  595.         free(rootp);
  596.         fat_idx_fini_by_dev_handle(dev_handle);
  597.         ipc_answer_0(rid, ENOMEM);
  598.         return;
  599.     }
  600.     assert(ridxp->index == 0);
  601.     /* ridxp->lock held */
  602.  
  603.     rootp->type = FAT_DIRECTORY;
  604.     rootp->firstc = FAT_CLST_ROOT;
  605.     rootp->refcnt = 1;
  606.     rootp->size = rde * sizeof(fat_dentry_t);
  607.     rootp->idx = ridxp;
  608.     ridxp->nodep = rootp;
  609.    
  610.     futex_up(&ridxp->lock);
  611.  
  612.     ipc_answer_0(rid, EOK);
  613. }
  614.  
  615. void fat_mount(ipc_callid_t rid, ipc_call_t *request)
  616. {
  617.     ipc_answer_0(rid, ENOTSUP);
  618. }
  619.  
  620. void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
  621. {
  622.     libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
  623. }
  624.  
  625. /**
  626.  * @}
  627.  */
  628.