| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Maurizio Lombardi
|
|---|
| 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 | #ifndef _MFS_H_
|
|---|
| 34 | #define _MFS_H_
|
|---|
| 35 |
|
|---|
| 36 | #include <minix.h>
|
|---|
| 37 | #include <macros.h>
|
|---|
| 38 | #include <libblock.h>
|
|---|
| 39 | #include <libfs.h>
|
|---|
| 40 | #include <adt/list.h>
|
|---|
| 41 | #include <malloc.h>
|
|---|
| 42 | #include <mem.h>
|
|---|
| 43 | #include <stdio.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <assert.h>
|
|---|
| 46 | #include "../../vfs/vfs.h"
|
|---|
| 47 |
|
|---|
| 48 | #define NAME "mfs"
|
|---|
| 49 |
|
|---|
| 50 | /* #define DEBUG_MODE */
|
|---|
| 51 |
|
|---|
| 52 | #define min(a, b) ((a) < (b) ? (a) : (b))
|
|---|
| 53 |
|
|---|
| 54 | #ifdef DEBUG_MODE
|
|---|
| 55 | #define mfsdebug(...) printf(__VA_ARGS__)
|
|---|
| 56 | #else
|
|---|
| 57 | #define mfsdebug(...)
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | typedef uint32_t bitchunk_t;
|
|---|
| 61 |
|
|---|
| 62 | typedef enum {
|
|---|
| 63 | BMAP_ZONE,
|
|---|
| 64 | BMAP_INODE
|
|---|
| 65 | } bmap_id_t;
|
|---|
| 66 |
|
|---|
| 67 | typedef enum {
|
|---|
| 68 | MFS_VERSION_V1 = 1,
|
|---|
| 69 | MFS_VERSION_V2,
|
|---|
| 70 | MFS_VERSION_V3
|
|---|
| 71 | } mfs_version_t;
|
|---|
| 72 |
|
|---|
| 73 | /* Generic MinixFS superblock */
|
|---|
| 74 | struct mfs_sb_info {
|
|---|
| 75 | uint32_t ninodes;
|
|---|
| 76 | uint32_t nzones;
|
|---|
| 77 | unsigned long ibmap_blocks;
|
|---|
| 78 | unsigned long zbmap_blocks;
|
|---|
| 79 | unsigned long firstdatazone;
|
|---|
| 80 | int log2_zone_size;
|
|---|
| 81 | int block_size;
|
|---|
| 82 | uint32_t max_file_size;
|
|---|
| 83 | uint16_t magic;
|
|---|
| 84 | uint16_t state;
|
|---|
| 85 |
|
|---|
| 86 | /* The following fields do not exist on disk but only in memory */
|
|---|
| 87 | unsigned long itable_size;
|
|---|
| 88 | mfs_version_t fs_version;
|
|---|
| 89 | int ino_per_block;
|
|---|
| 90 | size_t dirsize;
|
|---|
| 91 | int itable_off;
|
|---|
| 92 | unsigned max_name_len;
|
|---|
| 93 | bool long_names;
|
|---|
| 94 | bool native;
|
|---|
| 95 | unsigned isearch;
|
|---|
| 96 | unsigned zsearch;
|
|---|
| 97 | };
|
|---|
| 98 |
|
|---|
| 99 | /* Generic MinixFS inode */
|
|---|
| 100 | struct mfs_ino_info {
|
|---|
| 101 | uint16_t i_mode;
|
|---|
| 102 | uint16_t i_nlinks;
|
|---|
| 103 | int16_t i_uid;
|
|---|
| 104 | uint16_t i_gid;
|
|---|
| 105 | size_t i_size;
|
|---|
| 106 | int32_t i_atime;
|
|---|
| 107 | int32_t i_mtime;
|
|---|
| 108 | int32_t i_ctime;
|
|---|
| 109 | /* Block numbers for direct zones */
|
|---|
| 110 | uint32_t i_dzone[V2_NR_DIRECT_ZONES];
|
|---|
| 111 | /* Block numbers for indirect zones */
|
|---|
| 112 | uint32_t i_izone[V2_NR_INDIRECT_ZONES];
|
|---|
| 113 |
|
|---|
| 114 | /* The following fields do not exist on disk but only in memory */
|
|---|
| 115 | bool dirty;
|
|---|
| 116 | fs_index_t index;
|
|---|
| 117 | };
|
|---|
| 118 |
|
|---|
| 119 | /* Generic MFS directory entry */
|
|---|
| 120 | struct mfs_dentry_info {
|
|---|
| 121 | uint32_t d_inum;
|
|---|
| 122 | char d_name[MFS3_MAX_NAME_LEN + 1];
|
|---|
| 123 |
|
|---|
| 124 | /* The following fields do not exist on disk but only in memory */
|
|---|
| 125 |
|
|---|
| 126 | /* Index of the dentry in the list */
|
|---|
| 127 | unsigned index;
|
|---|
| 128 | /* Pointer to the node at witch the dentry belongs */
|
|---|
| 129 | struct mfs_node *node;
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | struct mfs_instance {
|
|---|
| 133 | service_id_t service_id;
|
|---|
| 134 | struct mfs_sb_info *sbi;
|
|---|
| 135 | unsigned open_nodes_cnt;
|
|---|
| 136 | };
|
|---|
| 137 |
|
|---|
| 138 | /* MinixFS node in core */
|
|---|
| 139 | struct mfs_node {
|
|---|
| 140 | struct mfs_ino_info *ino_i;
|
|---|
| 141 | struct mfs_instance *instance;
|
|---|
| 142 | unsigned refcnt;
|
|---|
| 143 | fs_node_t *fsnode;
|
|---|
| 144 | link_t link;
|
|---|
| 145 | };
|
|---|
| 146 |
|
|---|
| 147 | /* mfs_ops.c */
|
|---|
| 148 | extern vfs_out_ops_t mfs_ops;
|
|---|
| 149 | extern libfs_ops_t mfs_libfs_ops;
|
|---|
| 150 |
|
|---|
| 151 | extern int
|
|---|
| 152 | mfs_global_init(void);
|
|---|
| 153 |
|
|---|
| 154 | /* mfs_inode.c */
|
|---|
| 155 | extern int
|
|---|
| 156 | mfs_get_inode(struct mfs_instance *inst, struct mfs_ino_info **ino_i,
|
|---|
| 157 | fs_index_t index);
|
|---|
| 158 |
|
|---|
| 159 | extern int
|
|---|
| 160 | mfs_put_inode(struct mfs_node *mnode);
|
|---|
| 161 |
|
|---|
| 162 | extern int
|
|---|
| 163 | mfs_inode_shrink(struct mfs_node *mnode, size_t size_shrink);
|
|---|
| 164 |
|
|---|
| 165 | /* mfs_rw.c */
|
|---|
| 166 | extern int
|
|---|
| 167 | mfs_read_map(uint32_t *b, const struct mfs_node *mnode, const uint32_t pos);
|
|---|
| 168 |
|
|---|
| 169 | extern int
|
|---|
| 170 | mfs_write_map(struct mfs_node *mnode, uint32_t pos, uint32_t new_zone,
|
|---|
| 171 | uint32_t *old_zone);
|
|---|
| 172 |
|
|---|
| 173 | extern int
|
|---|
| 174 | mfs_prune_ind_zones(struct mfs_node *mnode, size_t new_size);
|
|---|
| 175 |
|
|---|
| 176 | /* mfs_dentry.c */
|
|---|
| 177 | extern int
|
|---|
| 178 | mfs_read_dentry(struct mfs_node *mnode,
|
|---|
| 179 | struct mfs_dentry_info *d_info, unsigned index);
|
|---|
| 180 |
|
|---|
| 181 | extern int
|
|---|
| 182 | mfs_write_dentry(struct mfs_dentry_info *d_info);
|
|---|
| 183 |
|
|---|
| 184 | extern int
|
|---|
| 185 | mfs_remove_dentry(struct mfs_node *mnode, const char *d_name);
|
|---|
| 186 |
|
|---|
| 187 | extern int
|
|---|
| 188 | mfs_insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum);
|
|---|
| 189 |
|
|---|
| 190 | /* mfs_balloc.c */
|
|---|
| 191 | extern int
|
|---|
| 192 | mfs_alloc_inode(struct mfs_instance *inst, uint32_t *inum);
|
|---|
| 193 |
|
|---|
| 194 | extern int
|
|---|
| 195 | mfs_free_inode(struct mfs_instance *inst, uint32_t inum);
|
|---|
| 196 |
|
|---|
| 197 | extern int
|
|---|
| 198 | mfs_alloc_zone(struct mfs_instance *inst, uint32_t *zone);
|
|---|
| 199 |
|
|---|
| 200 | extern int
|
|---|
| 201 | mfs_free_zone(struct mfs_instance *inst, uint32_t zone);
|
|---|
| 202 |
|
|---|
| 203 | /* mfs_utils.c */
|
|---|
| 204 | extern uint16_t
|
|---|
| 205 | conv16(bool native, uint16_t n);
|
|---|
| 206 |
|
|---|
| 207 | extern uint32_t
|
|---|
| 208 | conv32(bool native, uint32_t n);
|
|---|
| 209 |
|
|---|
| 210 | extern uint64_t
|
|---|
| 211 | conv64(bool native, uint64_t n);
|
|---|
| 212 |
|
|---|
| 213 | #endif
|
|---|
| 214 |
|
|---|
| 215 | /**
|
|---|
| 216 | * @}
|
|---|
| 217 | */
|
|---|
| 218 |
|
|---|