Subversion Repositories HelenOS

Rev

Rev 3521 | Rev 3530 | 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 "fat_dentry.h"
  40. #include "fat_fat.h"
  41. #include "../../vfs/vfs.h"
  42. #include <libfs.h>
  43. #include <libblock.h>
  44. #include <ipc/ipc.h>
  45. #include <ipc/services.h>
  46. #include <ipc/devmap.h>
  47. #include <async.h>
  48. #include <errno.h>
  49. #include <string.h>
  50. #include <byteorder.h>
  51. #include <libadt/hash_table.h>
  52. #include <libadt/list.h>
  53. #include <assert.h>
  54. #include <futex.h>
  55. #include <sys/mman.h>
  56. #include <align.h>
  57.  
  58. /** Futex protecting the list of cached free FAT nodes. */
  59. static futex_t ffn_futex = FUTEX_INITIALIZER;
  60.  
  61. /** List of cached free FAT nodes. */
  62. static LIST_INITIALIZE(ffn_head);
  63.  
  64. static void fat_node_initialize(fat_node_t *node)
  65. {
  66.     futex_initialize(&node->lock, 1);
  67.     node->idx = NULL;
  68.     node->type = 0;
  69.     link_initialize(&node->ffn_link);
  70.     node->size = 0;
  71.     node->lnkcnt = 0;
  72.     node->refcnt = 0;
  73.     node->dirty = false;
  74. }
  75.  
  76. static void fat_node_sync(fat_node_t *node)
  77. {
  78.     block_t *bb, *b;
  79.     fat_dentry_t *d;
  80.     uint16_t bps;
  81.     unsigned dps;
  82.    
  83.     assert(node->dirty);
  84.  
  85.     bb = block_get(node->idx->dev_handle, BS_BLOCK, BS_SIZE);
  86.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  87.     dps = bps / sizeof(fat_dentry_t);
  88.    
  89.     /* Read the block that contains the dentry of interest. */
  90.     b = _fat_block_get(bb->data, node->idx->dev_handle, node->idx->pfc,
  91.         (node->idx->pdi * sizeof(fat_dentry_t)) / bps);
  92.  
  93.     d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
  94.  
  95.     d->firstc = host2uint16_t_le(node->firstc);
  96.     if (node->type == FAT_FILE)
  97.         d->size = host2uint32_t_le(node->size);
  98.     /* TODO: update other fields? (e.g time fields, attr field) */
  99.    
  100.     b->dirty = true;        /* need to sync block */
  101.     block_put(b);
  102.     block_put(bb);
  103. }
  104.  
  105. /** Internal version of fat_node_get().
  106.  *
  107.  * @param idxp      Locked index structure.
  108.  */
  109. static void *fat_node_get_core(fat_idx_t *idxp)
  110. {
  111.     block_t *bb, *b;
  112.     fat_dentry_t *d;
  113.     fat_node_t *nodep = NULL;
  114.     unsigned bps;
  115.     unsigned dps;
  116.  
  117.     if (idxp->nodep) {
  118.         /*
  119.          * We are lucky.
  120.          * The node is already instantiated in memory.
  121.          */
  122.         futex_down(&idxp->nodep->lock);
  123.         if (!idxp->nodep->refcnt++)
  124.             list_remove(&idxp->nodep->ffn_link);
  125.         futex_up(&idxp->nodep->lock);
  126.         return idxp->nodep;
  127.     }
  128.  
  129.     /*
  130.      * We must instantiate the node from the file system.
  131.      */
  132.    
  133.     assert(idxp->pfc);
  134.  
  135.     futex_down(&ffn_futex);
  136.     if (!list_empty(&ffn_head)) {
  137.         /* Try to use a cached free node structure. */
  138.         fat_idx_t *idxp_tmp;
  139.         nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
  140.         if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
  141.             goto skip_cache;
  142.         idxp_tmp = nodep->idx;
  143.         if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
  144.             futex_up(&nodep->lock);
  145.             goto skip_cache;
  146.         }
  147.         list_remove(&nodep->ffn_link);
  148.         futex_up(&ffn_futex);
  149.         if (nodep->dirty)
  150.             fat_node_sync(nodep);
  151.         idxp_tmp->nodep = NULL;
  152.         futex_up(&nodep->lock);
  153.         futex_up(&idxp_tmp->lock);
  154.     } else {
  155. skip_cache:
  156.         /* Try to allocate a new node structure. */
  157.         futex_up(&ffn_futex);
  158.         nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
  159.         if (!nodep)
  160.             return NULL;
  161.     }
  162.     fat_node_initialize(nodep);
  163.  
  164.     bb = block_get(idxp->dev_handle, BS_BLOCK, BS_SIZE);
  165.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  166.     dps = bps / sizeof(fat_dentry_t);
  167.  
  168.     /* Read the block that contains the dentry of interest. */
  169.     b = _fat_block_get(bb->data, idxp->dev_handle, idxp->pfc,
  170.         (idxp->pdi * sizeof(fat_dentry_t)) / bps);
  171.     assert(b);
  172.  
  173.     d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
  174.     if (d->attr & FAT_ATTR_SUBDIR) {
  175.         /*
  176.          * The only directory which does not have this bit set is the
  177.          * root directory itself. The root directory node is handled
  178.          * and initialized elsewhere.
  179.          */
  180.         nodep->type = FAT_DIRECTORY;
  181.         /*
  182.          * Unfortunately, the 'size' field of the FAT dentry is not
  183.          * defined for the directory entry type. We must determine the
  184.          * size of the directory by walking the FAT.
  185.          */
  186.         nodep->size = bps * _fat_blcks_get(bb->data, idxp->dev_handle,
  187.             uint16_t_le2host(d->firstc), NULL);
  188.     } else {
  189.         nodep->type = FAT_FILE;
  190.         nodep->size = uint32_t_le2host(d->size);
  191.     }
  192.     nodep->firstc = uint16_t_le2host(d->firstc);
  193.     nodep->lnkcnt = 1;
  194.     nodep->refcnt = 1;
  195.  
  196.     block_put(b);
  197.     block_put(bb);
  198.  
  199.     /* Link the idx structure with the node structure. */
  200.     nodep->idx = idxp;
  201.     idxp->nodep = nodep;
  202.  
  203.     return nodep;
  204. }
  205.  
  206. /** Instantiate a FAT in-core node. */
  207. static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
  208. {
  209.     void *node;
  210.     fat_idx_t *idxp;
  211.  
  212.     idxp = fat_idx_get_by_index(dev_handle, index);
  213.     if (!idxp)
  214.         return NULL;
  215.     /* idxp->lock held */
  216.     node = fat_node_get_core(idxp);
  217.     futex_up(&idxp->lock);
  218.     return node;
  219. }
  220.  
  221. static void fat_node_put(void *node)
  222. {
  223.     fat_node_t *nodep = (fat_node_t *)node;
  224.  
  225.     futex_down(&nodep->lock);
  226.     if (!--nodep->refcnt) {
  227.         futex_down(&ffn_futex);
  228.         list_append(&nodep->ffn_link, &ffn_head);
  229.         futex_up(&ffn_futex);
  230.     }
  231.     futex_up(&nodep->lock);
  232. }
  233.  
  234. static void *fat_create(int flags)
  235. {
  236.     return NULL;    /* not supported at the moment */
  237. }
  238.  
  239. static int fat_destroy(void *node)
  240. {
  241.     return ENOTSUP; /* not supported at the moment */
  242. }
  243.  
  244. static bool fat_link(void *prnt, void *chld, const char *name)
  245. {
  246.     return false;   /* not supported at the moment */
  247. }
  248.  
  249. static int fat_unlink(void *prnt, void *chld)
  250. {
  251.     return ENOTSUP; /* not supported at the moment */
  252. }
  253.  
  254. static void *fat_match(void *prnt, const char *component)
  255. {
  256.     fat_node_t *parentp = (fat_node_t *)prnt;
  257.     char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
  258.     unsigned i, j;
  259.     unsigned bps;       /* bytes per sector */
  260.     unsigned dps;       /* dentries per sector */
  261.     unsigned blocks;
  262.     fat_dentry_t *d;
  263.     block_t *bb, *b;
  264.  
  265.     futex_down(&parentp->idx->lock);
  266.     bb = block_get(parentp->idx->dev_handle, BS_BLOCK, BS_SIZE);
  267.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  268.     dps = bps / sizeof(fat_dentry_t);
  269.     blocks = parentp->size / bps;
  270.     for (i = 0; i < blocks; i++) {
  271.         b = fat_block_get(bb->data, parentp, i);
  272.         for (j = 0; j < dps; j++) {
  273.             d = ((fat_dentry_t *)b->data) + j;
  274.             switch (fat_classify_dentry(d)) {
  275.             case FAT_DENTRY_SKIP:
  276.                 continue;
  277.             case FAT_DENTRY_LAST:
  278.                 block_put(b);
  279.                 block_put(bb);
  280.                 futex_up(&parentp->idx->lock);
  281.                 return NULL;
  282.             default:
  283.             case FAT_DENTRY_VALID:
  284.                 dentry_name_canonify(d, name);
  285.                 break;
  286.             }
  287.             if (stricmp(name, component) == 0) {
  288.                 /* hit */
  289.                 void *node;
  290.                 /*
  291.                  * Assume tree hierarchy for locking.  We
  292.                  * already have the parent and now we are going
  293.                  * to lock the child.  Never lock in the oposite
  294.                  * order.
  295.                  */
  296.                 fat_idx_t *idx = fat_idx_get_by_pos(
  297.                     parentp->idx->dev_handle, parentp->firstc,
  298.                     i * dps + j);
  299.                 futex_up(&parentp->idx->lock);
  300.                 if (!idx) {
  301.                     /*
  302.                      * Can happen if memory is low or if we
  303.                      * run out of 32-bit indices.
  304.                      */
  305.                     block_put(b);
  306.                     block_put(bb);
  307.                     return NULL;
  308.                 }
  309.                 node = fat_node_get_core(idx);
  310.                 futex_up(&idx->lock);
  311.                 block_put(b);
  312.                 block_put(bb);
  313.                 return node;
  314.             }
  315.         }
  316.         block_put(b);
  317.     }
  318.     block_put(bb);
  319.  
  320.     futex_up(&parentp->idx->lock);
  321.     return NULL;
  322. }
  323.  
  324. static fs_index_t fat_index_get(void *node)
  325. {
  326.     fat_node_t *fnodep = (fat_node_t *)node;
  327.     if (!fnodep)
  328.         return 0;
  329.     return fnodep->idx->index;
  330. }
  331.  
  332. static size_t fat_size_get(void *node)
  333. {
  334.     return ((fat_node_t *)node)->size;
  335. }
  336.  
  337. static unsigned fat_lnkcnt_get(void *node)
  338. {
  339.     return ((fat_node_t *)node)->lnkcnt;
  340. }
  341.  
  342. static bool fat_has_children(void *node)
  343. {
  344.     fat_node_t *nodep = (fat_node_t *)node;
  345.     unsigned bps;
  346.     unsigned dps;
  347.     unsigned blocks;
  348.     block_t *bb, *b;
  349.     unsigned i, j;
  350.  
  351.     if (nodep->type != FAT_DIRECTORY)
  352.         return false;
  353.    
  354.     futex_down(&nodep->idx->lock);
  355.     bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE);
  356.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  357.     dps = bps / sizeof(fat_dentry_t);
  358.  
  359.     blocks = nodep->size / bps;
  360.  
  361.     for (i = 0; i < blocks; i++) {
  362.         fat_dentry_t *d;
  363.    
  364.         b = fat_block_get(bb->data, nodep, i);
  365.         for (j = 0; j < dps; j++) {
  366.             d = ((fat_dentry_t *)b->data) + j;
  367.             switch (fat_classify_dentry(d)) {
  368.             case FAT_DENTRY_SKIP:
  369.                 continue;
  370.             case FAT_DENTRY_LAST:
  371.                 block_put(b);
  372.                 block_put(bb);
  373.                 futex_up(&nodep->idx->lock);
  374.                 return false;
  375.             default:
  376.             case FAT_DENTRY_VALID:
  377.                 block_put(b);
  378.                 block_put(bb);
  379.                 futex_up(&nodep->idx->lock);
  380.                 return true;
  381.             }
  382.             block_put(b);
  383.             block_put(bb);
  384.             futex_up(&nodep->idx->lock);
  385.             return true;
  386.         }
  387.         block_put(b);
  388.     }
  389.     block_put(bb);
  390.  
  391.     futex_up(&nodep->idx->lock);
  392.     return false;
  393. }
  394.  
  395. static void *fat_root_get(dev_handle_t dev_handle)
  396. {
  397.     return fat_node_get(dev_handle, 0);
  398. }
  399.  
  400. static char fat_plb_get_char(unsigned pos)
  401. {
  402.     return fat_reg.plb_ro[pos % PLB_SIZE];
  403. }
  404.  
  405. static bool fat_is_directory(void *node)
  406. {
  407.     return ((fat_node_t *)node)->type == FAT_DIRECTORY;
  408. }
  409.  
  410. static bool fat_is_file(void *node)
  411. {
  412.     return ((fat_node_t *)node)->type == FAT_FILE;
  413. }
  414.  
  415. /** libfs operations */
  416. libfs_ops_t fat_libfs_ops = {
  417.     .match = fat_match,
  418.     .node_get = fat_node_get,
  419.     .node_put = fat_node_put,
  420.     .create = fat_create,
  421.     .destroy = fat_destroy,
  422.     .link = fat_link,
  423.     .unlink = fat_unlink,
  424.     .index_get = fat_index_get,
  425.     .size_get = fat_size_get,
  426.     .lnkcnt_get = fat_lnkcnt_get,
  427.     .has_children = fat_has_children,
  428.     .root_get = fat_root_get,
  429.     .plb_get_char = fat_plb_get_char,
  430.     .is_directory = fat_is_directory,
  431.     .is_file = fat_is_file
  432. };
  433.  
  434. void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
  435. {
  436.     dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
  437.     block_t *bb;
  438.     uint16_t bps;
  439.     uint16_t rde;
  440.     int rc;
  441.  
  442.     /*
  443.      * For now, we don't bother to remember dev_handle, dev_phone or
  444.      * dev_buffer in some data structure. We use global variables because we
  445.      * know there will be at most one mount on this file system.
  446.      * Of course, this is a huge TODO item.
  447.      */
  448.     dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
  449.         MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  450.    
  451.     if (!dev_buffer) {
  452.         ipc_answer_0(rid, ENOMEM);
  453.         return;
  454.     }
  455.  
  456.     dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
  457.         DEVMAP_CONNECT_TO_DEVICE, dev_handle);
  458.  
  459.     if (dev_phone < 0) {
  460.         munmap(dev_buffer, BS_SIZE);
  461.         ipc_answer_0(rid, dev_phone);
  462.         return;
  463.     }
  464.  
  465.     rc = ipc_share_out_start(dev_phone, dev_buffer,
  466.         AS_AREA_READ | AS_AREA_WRITE);
  467.     if (rc != EOK) {
  468.             munmap(dev_buffer, BS_SIZE);
  469.         ipc_answer_0(rid, rc);
  470.         return;
  471.     }
  472.  
  473.     /* Read the number of root directory entries. */
  474.     bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
  475.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  476.     rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
  477.     block_put(bb);
  478.  
  479.     if (bps != BS_SIZE) {
  480.         munmap(dev_buffer, BS_SIZE);
  481.         ipc_answer_0(rid, ENOTSUP);
  482.         return;
  483.     }
  484.  
  485.     rc = fat_idx_init_by_dev_handle(dev_handle);
  486.     if (rc != EOK) {
  487.             munmap(dev_buffer, BS_SIZE);
  488.         ipc_answer_0(rid, rc);
  489.         return;
  490.     }
  491.  
  492.     /* Initialize the root node. */
  493.     fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
  494.     if (!rootp) {
  495.             munmap(dev_buffer, BS_SIZE);
  496.         fat_idx_fini_by_dev_handle(dev_handle);
  497.         ipc_answer_0(rid, ENOMEM);
  498.         return;
  499.     }
  500.     fat_node_initialize(rootp);
  501.  
  502.     fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
  503.     if (!ridxp) {
  504.             munmap(dev_buffer, BS_SIZE);
  505.         free(rootp);
  506.         fat_idx_fini_by_dev_handle(dev_handle);
  507.         ipc_answer_0(rid, ENOMEM);
  508.         return;
  509.     }
  510.     assert(ridxp->index == 0);
  511.     /* ridxp->lock held */
  512.  
  513.     rootp->type = FAT_DIRECTORY;
  514.     rootp->firstc = FAT_CLST_ROOT;
  515.     rootp->refcnt = 1;
  516.     rootp->lnkcnt = 0;  /* FS root is not linked */
  517.     rootp->size = rde * sizeof(fat_dentry_t);
  518.     rootp->idx = ridxp;
  519.     ridxp->nodep = rootp;
  520.    
  521.     futex_up(&ridxp->lock);
  522.  
  523.     ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
  524. }
  525.  
  526. void fat_mount(ipc_callid_t rid, ipc_call_t *request)
  527. {
  528.     ipc_answer_0(rid, ENOTSUP);
  529. }
  530.  
  531. void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
  532. {
  533.     libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
  534. }
  535.  
  536. void fat_read(ipc_callid_t rid, ipc_call_t *request)
  537. {
  538.     dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
  539.     fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
  540.     off_t pos = (off_t)IPC_GET_ARG3(*request);
  541.     fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
  542.     uint16_t bps;
  543.     size_t bytes;
  544.     block_t *bb, *b;
  545.  
  546.     if (!nodep) {
  547.         ipc_answer_0(rid, ENOENT);
  548.         return;
  549.     }
  550.  
  551.     ipc_callid_t callid;
  552.     size_t len;
  553.     if (!ipc_data_read_receive(&callid, &len)) {
  554.         fat_node_put(nodep);
  555.         ipc_answer_0(callid, EINVAL);
  556.         ipc_answer_0(rid, EINVAL);
  557.         return;
  558.     }
  559.  
  560.     bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
  561.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  562.  
  563.     if (nodep->type == FAT_FILE) {
  564.         /*
  565.          * Our strategy for regular file reads is to read one block at
  566.          * most and make use of the possibility to return less data than
  567.          * requested. This keeps the code very simple.
  568.          */
  569.         bytes = min(len, bps - pos % bps);
  570.         b = fat_block_get(bb->data, nodep, pos / bps);
  571.         (void) ipc_data_read_finalize(callid, b->data + pos % bps,
  572.             bytes);
  573.         block_put(b);
  574.     } else {
  575.         unsigned bnum;
  576.         off_t spos = pos;
  577.         char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
  578.         fat_dentry_t *d;
  579.  
  580.         assert(nodep->type == FAT_DIRECTORY);
  581.         assert(nodep->size % bps == 0);
  582.         assert(bps % sizeof(fat_dentry_t) == 0);
  583.  
  584.         /*
  585.          * Our strategy for readdir() is to use the position pointer as
  586.          * an index into the array of all dentries. On entry, it points
  587.          * to the first unread dentry. If we skip any dentries, we bump
  588.          * the position pointer accordingly.
  589.          */
  590.         bnum = (pos * sizeof(fat_dentry_t)) / bps;
  591.         while (bnum < nodep->size / bps) {
  592.             off_t o;
  593.  
  594.             b = fat_block_get(bb->data, nodep, bnum);
  595.             for (o = pos % (bps / sizeof(fat_dentry_t));
  596.                 o < bps / sizeof(fat_dentry_t);
  597.                 o++, pos++) {
  598.                 d = ((fat_dentry_t *)b->data) + o;
  599.                 switch (fat_classify_dentry(d)) {
  600.                 case FAT_DENTRY_SKIP:
  601.                     continue;
  602.                 case FAT_DENTRY_LAST:
  603.                     block_put(b);
  604.                     goto miss;
  605.                 default:
  606.                 case FAT_DENTRY_VALID:
  607.                     dentry_name_canonify(d, name);
  608.                     block_put(b);
  609.                     goto hit;
  610.                 }
  611.             }
  612.             block_put(b);
  613.             bnum++;
  614.         }
  615. miss:
  616.         fat_node_put(nodep);
  617.         block_put(bb);
  618.         ipc_answer_0(callid, ENOENT);
  619.         ipc_answer_1(rid, ENOENT, 0);
  620.         return;
  621. hit:
  622.         (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
  623.         bytes = (pos - spos) + 1;
  624.     }
  625.  
  626.     fat_node_put(nodep);
  627.     block_put(bb);
  628.     ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
  629. }
  630.  
  631. void fat_write(ipc_callid_t rid, ipc_call_t *request)
  632. {
  633.     dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
  634.     fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
  635.     off_t pos = (off_t)IPC_GET_ARG3(*request);
  636.     fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
  637.     size_t bytes;
  638.     block_t *b, *bb;
  639.     uint16_t bps;
  640.     unsigned spc;
  641.     off_t boundary;
  642.    
  643.     if (!nodep) {
  644.         ipc_answer_0(rid, ENOENT);
  645.         return;
  646.     }
  647.    
  648.     /* XXX remove me when you are ready */
  649.     {
  650.         ipc_answer_0(rid, ENOTSUP);
  651.         fat_node_put(nodep);
  652.         return;
  653.     }
  654.  
  655.     ipc_callid_t callid;
  656.     size_t len;
  657.     if (!ipc_data_write_receive(&callid, &len)) {
  658.         fat_node_put(nodep);
  659.         ipc_answer_0(callid, EINVAL);
  660.         ipc_answer_0(rid, EINVAL);
  661.         return;
  662.     }
  663.  
  664.     /*
  665.      * In all scenarios, we will attempt to write out only one block worth
  666.      * of data at maximum. There might be some more efficient approaches,
  667.      * but this one greatly simplifies fat_write(). Note that we can afford
  668.      * to do this because the client must be ready to handle the return
  669.      * value signalizing a smaller number of bytes written.
  670.      */
  671.     bytes = min(len, bps - pos % bps);
  672.  
  673.     bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
  674.     bps = uint16_t_le2host(FAT_BS(bb)->bps);
  675.     spc = FAT_BS(bb)->spc;
  676.    
  677.     boundary = ROUND_UP(nodep->size, bps * spc);
  678.     if (pos < boundary) {
  679.         /*
  680.          * This is the easier case - we are either overwriting already
  681.          * existing contents or writing behind the EOF, but still within
  682.          * the limits of the last cluster. The node size may grow to the
  683.          * next block size boundary.
  684.          */
  685.         fat_fill_gap(bb->data, nodep, FAT_CLST_RES0, pos);
  686.         b = fat_block_get(bb->data, nodep, pos / bps);
  687.         (void) ipc_data_write_finalize(callid, b->data + pos % bps,
  688.             bytes);
  689.         b->dirty = true;        /* need to sync block */
  690.         block_put(b);
  691.         if (pos + bytes > nodep->size) {
  692.             nodep->size = pos + bytes;
  693.             nodep->dirty = true;    /* need to sync node */
  694.         }
  695.         fat_node_put(nodep);
  696.         block_put(bb);
  697.         ipc_answer_1(rid, EOK, bytes); 
  698.         return;
  699.     } else {
  700.         /*
  701.          * This is the more difficult case. We must allocate new
  702.          * clusters for the node and zero them out.
  703.          */
  704.         int status;
  705.         unsigned nclsts;
  706.         fat_cluster_t mcl, lcl;
  707.    
  708.         nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
  709.             bps * spc;
  710.         /* create an independent chain of nclsts clusters in all FATs */
  711.         status = fat_alloc_clusters(bb->data, dev_handle, nclsts, &mcl,
  712.             &lcl);
  713.         if (status != EOK) {
  714.             /* could not allocate a chain of nclsts clusters */
  715.             fat_node_put(nodep);
  716.             block_put(bb);
  717.             ipc_answer_0(callid, status);
  718.             ipc_answer_0(rid, status);
  719.             return;
  720.         }
  721.         /* zero fill any gaps */
  722.         fat_fill_gap(bb->data, nodep, mcl, pos);
  723.         b = _fat_block_get(bb->data, dev_handle, lcl,
  724.             (pos / bps) % spc);
  725.         (void) ipc_data_write_finalize(callid, b->data + pos % bps,
  726.             bytes);
  727.         b->dirty = true;        /* need to sync block */
  728.         block_put(b);
  729.         /*
  730.          * Append the cluster chain starting in mcl to the end of the
  731.          * node's cluster chain.
  732.          */
  733.         fat_append_clusters(bb->data, nodep, mcl);
  734.         nodep->size = pos + bytes;
  735.         nodep->dirty = true;        /* need to sync node */
  736.         fat_node_put(nodep);
  737.         block_put(bb);
  738.         ipc_answer_1(rid, EOK, bytes);
  739.         return;
  740.     }
  741. }
  742.  
  743. /**
  744.  * @}
  745.  */
  746.