Changeset 25696fea in mainline for uspace/srv/fs/mfs/mfs_ops.c


Ignore:
Timestamp:
2011-10-15T20:05:00Z (13 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
22ceff3a
Parents:
1ccc32f (diff), 721d4b6e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge with mainline

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/mfs/mfs_ops.c

    r1ccc32f r25696fea  
    4343
    4444static bool check_magic_number(uint16_t magic, bool *native,
    45                                mfs_version_t *version, bool *longfilenames);
     45    mfs_version_t *version, bool *longfilenames);
    4646static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
    47                              fs_index_t index);
    48 
     47    fs_index_t index);
    4948static int mfs_node_put(fs_node_t *fsnode);
    5049static int mfs_node_open(fs_node_t *fsnode);
     
    6463static hash_index_t open_nodes_hash(unsigned long key[]);
    6564static int open_nodes_compare(unsigned long key[], hash_count_t keys,
    66                 link_t *item);
     65    link_t *item);
    6766static void open_nodes_remove_cb(link_t *link);
    68 
    6967static int mfs_node_get(fs_node_t **rfn, service_id_t service_id,
    70                         fs_index_t index);
    71 static int
    72 mfs_instance_get(service_id_t service_id, struct mfs_instance **instance);
    73 
     68    fs_index_t index);
     69static int mfs_instance_get(service_id_t service_id,
     70    struct mfs_instance **instance);
     71static int mfs_check_sanity(struct mfs_sb_info *sbi);
     72static bool is_power_of_two(uint32_t n);
    7473
    7574static hash_table_t open_nodes;
     
    9695
    9796/* Hash table interface for open nodes hash table */
    98 static hash_index_t open_nodes_hash(unsigned long key[])
     97static hash_index_t
     98open_nodes_hash(unsigned long key[])
    9999{
    100100        /* TODO: This is very simple and probably can be improved */
     
    102102}
    103103
    104 static int open_nodes_compare(unsigned long key[], hash_count_t keys,
    105                 link_t *item)
     104static int
     105open_nodes_compare(unsigned long key[], hash_count_t keys,
     106    link_t *item)
    106107{
    107108        struct mfs_node *mnode = hash_table_get_instance(item, struct mfs_node, link);
     
    118119}
    119120
    120 static void open_nodes_remove_cb(link_t *link)
     121static void
     122open_nodes_remove_cb(link_t *link)
    121123{
    122124        /* We don't use remove callback for this hash table */
     
    129131};
    130132
    131 int mfs_global_init(void)
     133int
     134mfs_global_init(void)
    132135{
    133136        if (!hash_table_create(&open_nodes, OPEN_NODES_BUCKETS,
    134                         OPEN_NODES_KEYS, &open_nodes_ops)) {
     137            OPEN_NODES_KEYS, &open_nodes_ops)) {
    135138                return ENOMEM;
    136139        }
     
    140143static int
    141144mfs_mounted(service_id_t service_id, const char *opts, fs_index_t *index,
    142                 aoff64_t *size, unsigned *linkcnt)
     145    aoff64_t *size, unsigned *linkcnt)
    143146{
    144147        enum cache_mode cmode;
     
    163166                return rc;
    164167
    165         /*Allocate space for generic MFS superblock*/
     168        /* Allocate space for generic MFS superblock */
    166169        sbi = malloc(sizeof(*sbi));
    167170        if (!sbi) {
     
    170173        }
    171174
    172         /*Allocate space for filesystem instance*/
     175        /* Allocate space for filesystem instance */
    173176        instance = malloc(sizeof(*instance));
    174177        if (!instance) {
     
    191194
    192195        if (check_magic_number(sb->s_magic, &native, &version, &longnames)) {
    193                 /*This is a V1 or V2 Minix filesystem*/
     196                /* This is a V1 or V2 Minix filesystem */
    194197                magic = sb->s_magic;
    195198        } else if (check_magic_number(sb3->s_magic, &native, &version, &longnames)) {
    196                 /*This is a V3 Minix filesystem*/
     199                /* This is a V3 Minix filesystem */
    197200                magic = sb3->s_magic;
    198201        } else {
    199                 /*Not recognized*/
     202                /* Not recognized */
    200203                mfsdebug("magic number not recognized\n");
    201204                rc = ENOTSUP;
     
    205208        mfsdebug("magic number recognized = %04x\n", magic);
    206209
    207         /*Fill superblock info structure*/
     210        /* Fill superblock info structure */
    208211
    209212        sbi->fs_version = version;
     
    243246                sbi->dirsize = longnames ? MFSL_DIRSIZE : MFS_DIRSIZE;
    244247                sbi->max_name_len = longnames ? MFS_L_MAX_NAME_LEN :
    245                                     MFS_MAX_NAME_LEN;
     248                    MFS_MAX_NAME_LEN;
    246249        }
    247250
     
    259262
    260263        sbi->itable_off = 2 + sbi->ibmap_blocks + sbi->zbmap_blocks;
     264        if ((rc = mfs_check_sanity(sbi)) != EOK) {
     265                fprintf(stderr, "Filesystem corrupted, invalid superblock");
     266                goto out_error;
     267        }
    261268
    262269        rc = block_cache_init(service_id, sbi->block_size, 0, cmode);
     
    267274        }
    268275
    269         /*Initialize the instance structure and remember it*/
     276        /* Initialize the instance structure and remember it */
    270277        instance->service_id = service_id;
    271278        instance->sbi = sbi;
     
    273280        rc = fs_instance_create(service_id, instance);
    274281        if (rc != EOK) {
    275                 free(instance);
    276                 free(sbi);
    277282                block_cache_fini(service_id);
    278                 block_fini(service_id);
    279283                mfsdebug("fs instance creation failed\n");
    280                 return rc;
     284                goto out_error;
    281285        }
    282286
     
    331335}
    332336
    333 service_id_t mfs_service_get(fs_node_t *fsnode)
     337service_id_t
     338mfs_service_get(fs_node_t *fsnode)
    334339{
    335340        struct mfs_node *node = fsnode->data;
     
    337342}
    338343
    339 static int mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
     344static int
     345mfs_create_node(fs_node_t **rfn, service_id_t service_id, int flags)
    340346{
    341347        int r;
     
    351357                return r;
    352358
    353         /*Alloc a new inode*/
     359        /* Alloc a new inode */
    354360        r = mfs_alloc_inode(inst, &inum);
    355361        if (r != EOK)
     
    378384        if (flags & L_DIRECTORY) {
    379385                ino_i->i_mode = S_IFDIR;
    380                 ino_i->i_nlinks = 2; /*This accounts for the '.' dentry*/
     386                ino_i->i_nlinks = 2; /* This accounts for the '.' dentry */
    381387        } else {
    382388                ino_i->i_mode = S_IFREG;
     
    431437}
    432438
    433 static int mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
     439static int
     440mfs_match(fs_node_t **rfn, fs_node_t *pfn, const char *component)
    434441{
    435442        struct mfs_node *mnode = pfn->data;
     
    453460
    454461                if (!d_info.d_inum) {
    455                         /*This entry is not used*/
     462                        /* This entry is not used */
    456463                        continue;
    457464                }
     
    460467
    461468                if (comp_size == dentry_name_size &&
    462                         !bcmp(component, d_info.d_name, dentry_name_size)) {
    463                         /*Hit!*/
     469                    !bcmp(component, d_info.d_name, dentry_name_size)) {
     470                        /* Hit! */
    464471                        mfs_node_core_get(rfn, mnode->instance,
    465                                           d_info.d_inum);
     472                            d_info.d_inum);
    466473                        goto found;
    467474                }
     
    472479}
    473480
    474 static aoff64_t mfs_size_get(fs_node_t *node)
     481static aoff64_t
     482mfs_size_get(fs_node_t *node)
    475483{
    476484        const struct mfs_node *mnode = node->data;
     
    480488static int
    481489mfs_node_get(fs_node_t **rfn, service_id_t service_id,
    482                         fs_index_t index)
     490    fs_index_t index)
    483491{
    484492        int rc;
     
    524532}
    525533
    526 static int mfs_node_open(fs_node_t *fsnode)
     534static int
     535mfs_node_open(fs_node_t *fsnode)
    527536{
    528537        /*
     
    533542}
    534543
    535 static fs_index_t mfs_index_get(fs_node_t *fsnode)
     544static fs_index_t
     545mfs_index_get(fs_node_t *fsnode)
    536546{
    537547        struct mfs_node *mnode = fsnode->data;
     
    539549}
    540550
    541 static unsigned mfs_lnkcnt_get(fs_node_t *fsnode)
     551static unsigned
     552mfs_lnkcnt_get(fs_node_t *fsnode)
    542553{
    543554        struct mfs_node *mnode = fsnode->data;
     
    554565}
    555566
    556 static int mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
    557                              fs_index_t index)
     567static int
     568mfs_node_core_get(fs_node_t **rfn, struct mfs_instance *inst,
     569    fs_index_t index)
    558570{
    559571        fs_node_t *node = NULL;
     
    627639}
    628640
    629 static bool mfs_is_directory(fs_node_t *fsnode)
     641static bool
     642mfs_is_directory(fs_node_t *fsnode)
    630643{
    631644        const struct mfs_node *node = fsnode->data;
     
    633646}
    634647
    635 static bool mfs_is_file(fs_node_t *fsnode)
     648static bool
     649mfs_is_file(fs_node_t *fsnode)
    636650{
    637651        struct mfs_node *node = fsnode->data;
     
    639653}
    640654
    641 static int mfs_root_get(fs_node_t **rfn, service_id_t service_id)
     655static int
     656mfs_root_get(fs_node_t **rfn, service_id_t service_id)
    642657{
    643658        int rc = mfs_node_get(rfn, service_id, MFS_ROOT_INO);
     
    645660}
    646661
    647 static int mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
     662static int
     663mfs_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
    648664{
    649665        struct mfs_node *parent = pfn->data;
     
    720736}
    721737
    722 static int mfs_has_children(bool *has_children, fs_node_t *fsnode)
     738static int
     739mfs_has_children(bool *has_children, fs_node_t *fsnode)
    723740{
    724741        struct mfs_node *mnode = fsnode->data;
     
    741758
    742759                if (d_info.d_inum) {
    743                         /*A valid entry has been found*/
     760                        /* A valid entry has been found */
    744761                        *has_children = true;
    745762                        break;
     
    753770static int
    754771mfs_read(service_id_t service_id, fs_index_t index, aoff64_t pos,
    755                 size_t *rbytes)
     772    size_t *rbytes)
    756773{
    757774        int rc;
     
    783800
    784801                if (pos < 2) {
    785                         /*Skip the first two dentries ('.' and '..')*/
     802                        /* Skip the first two dentries ('.' and '..') */
    786803                        pos = 2;
    787804                }
     
    793810
    794811                        if (d_info.d_inum) {
    795                                 /*Dentry found!*/
     812                                /* Dentry found! */
    796813                                goto found;
    797814                        }
     
    809826
    810827                if (pos >= (size_t) ino_i->i_size) {
    811                         /*Trying to read beyond the end of file*/
     828                        /* Trying to read beyond the end of file */
    812829                        bytes = 0;
    813830                        (void) async_data_read_finalize(callid, NULL, 0);
     
    826843
    827844                if (zone == 0) {
    828                         /*sparse file*/
     845                        /* sparse file */
    829846                        uint8_t *buf = malloc(sbi->block_size);
    830847                        if (!buf) {
     
    834851                        memset(buf, 0, sizeof(sbi->block_size));
    835852                        async_data_read_finalize(callid,
    836                                                 buf + pos % sbi->block_size, bytes);
     853                            buf + pos % sbi->block_size, bytes);
    837854                        free(buf);
    838855                        goto out_success;
     
    844861
    845862                async_data_read_finalize(callid, b->data +
    846                                         pos % sbi->block_size, bytes);
     863                    pos % sbi->block_size, bytes);
    847864
    848865                rc = block_put(b);
     
    865882static int
    866883mfs_write(service_id_t service_id, fs_index_t index, aoff64_t pos,
    867                 size_t *wbytes, aoff64_t *nsize)
     884    size_t *wbytes, aoff64_t *nsize)
    868885{
    869886        fs_node_t *fn;
     
    900917
    901918        if (block == 0) {
    902                 /*Writing in a sparse block*/
    903919                uint32_t dummy;
    904920
     
    958974                return ENOENT;
    959975
    960         /*Destroy the inode*/
     976        /* Destroy the inode */
    961977        return mfs_destroy_node(fn);
    962978}
     
    977993        assert(!has_children);
    978994
    979         /*Free the entire inode content*/
     995        /* Free the entire inode content */
    980996        r = mfs_inode_shrink(mnode, mnode->ino_i->i_size);
    981997        if (r != EOK)
    982998                goto out;
    983999
    984         /*Mark the inode as free in the bitmap*/
     1000        /* Mark the inode as free in the bitmap */
    9851001        r = mfs_free_inode(mnode->instance, mnode->ino_i->index);
    9861002
     
    10211037
    10221038        rc = fs_instance_get(service_id, &data);
    1023         if (rc == EOK) {
     1039        if (rc == EOK)
    10241040                *instance = (struct mfs_instance *) data;
    1025         } else {
     1041        else {
    10261042                mfsdebug("instance not found\n");
    10271043        }
     
    10301046}
    10311047
    1032 static bool check_magic_number(uint16_t magic, bool *native,
    1033                                mfs_version_t *version, bool *longfilenames)
     1048static bool
     1049check_magic_number(uint16_t magic, bool *native,
     1050                mfs_version_t *version, bool *longfilenames)
    10341051{
    10351052        bool rc = true;
     
    10591076}
    10601077
     1078/** Filesystem sanity check
     1079 *
     1080 * @param Pointer to the MFS superblock.
     1081 *
     1082 * @return EOK on success, ENOTSUP otherwise.
     1083 */
     1084static int
     1085mfs_check_sanity(struct mfs_sb_info *sbi)
     1086{
     1087        if (!is_power_of_two(sbi->block_size) ||
     1088            sbi->block_size < MFS_MIN_BLOCKSIZE ||
     1089            sbi->block_size > MFS_MAX_BLOCKSIZE)
     1090                return ENOTSUP;
     1091        else if (sbi->ibmap_blocks == 0 || sbi->zbmap_blocks == 0)
     1092                return ENOTSUP;
     1093        else if (sbi->ninodes == 0 || sbi->nzones == 0)
     1094                return ENOTSUP;
     1095        else if (sbi->firstdatazone == 0)
     1096                return ENOTSUP;
     1097
     1098        return EOK;
     1099}
     1100
    10611101static int
    10621102mfs_close(service_id_t service_id, fs_index_t index)
     
    10791119
    10801120        return mfs_node_put(fn);
     1121}
     1122
     1123/** Check if a given number is a power of two.
     1124 *
     1125 * @param n     The number to check.
     1126 *
     1127 * @return      true if it is a power of two, false otherwise.
     1128 */
     1129static bool
     1130is_power_of_two(uint32_t n)
     1131{
     1132        if (n == 0)
     1133                return false;
     1134
     1135        return (n & (n - 1)) == 0;
    10811136}
    10821137
Note: See TracChangeset for help on using the changeset viewer.