| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2011 Oleg Romanenko
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup fs
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | #ifndef EXFAT_EXFAT_H_
|
|---|
| 35 | #define EXFAT_EXFAT_H_
|
|---|
| 36 |
|
|---|
| 37 | #include "exfat_fat.h"
|
|---|
| 38 | #include <fibril_synch.h>
|
|---|
| 39 | #include <libfs.h>
|
|---|
| 40 | #include <atomic.h>
|
|---|
| 41 | #include <sys/types.h>
|
|---|
| 42 | #include <bool.h>
|
|---|
| 43 | #include "../../vfs/vfs.h"
|
|---|
| 44 |
|
|---|
| 45 | #ifndef dprintf
|
|---|
| 46 | #define dprintf(...) printf(__VA_ARGS__)
|
|---|
| 47 | #endif
|
|---|
| 48 |
|
|---|
| 49 | #define BS_BLOCK 0
|
|---|
| 50 | #define BS_SIZE 512
|
|---|
| 51 |
|
|---|
| 52 | #define BPS(bs) (1 << (bs->bytes_per_sector))
|
|---|
| 53 | #define SPC(bs) (1 << (bs->sec_per_cluster))
|
|---|
| 54 | #define VOL_FS(bs) uint64_t_le2host(bs->volume_start)
|
|---|
| 55 | #define VOL_CNT(bs) uint64_t_le2host(bs->volume_count)
|
|---|
| 56 | #define FAT_FS(bs) uint32_t_le2host(bs->fat_sector_start)
|
|---|
| 57 | #define FAT_CNT(bs) uint32_t_le2host(bs->fat_sector_count)
|
|---|
| 58 | #define DATA_FS(bs) uint32_t_le2host(bs->data_start_sector)
|
|---|
| 59 | #define DATA_CNT(bs) uint32_t_le2host(bs->data_clusters)
|
|---|
| 60 | #define ROOT_FC(bs) uint32_t_le2host(bs->rootdir_cluster)
|
|---|
| 61 | #define VOL_FLAGS(bs) uint16_t_le2host(bs->volume_flags)
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | typedef struct exfat_bs {
|
|---|
| 65 | uint8_t jump[3]; /* 0x00 jmp and nop instructions */
|
|---|
| 66 | uint8_t oem_name[8]; /* 0x03 "EXFAT " */
|
|---|
| 67 | uint8_t __reserved[53]; /* 0x0B always 0 */
|
|---|
| 68 | uint64_t volume_start; /* 0x40 partition first sector */
|
|---|
| 69 | uint64_t volume_count; /* 0x48 partition sectors count */
|
|---|
| 70 | uint32_t fat_sector_start; /* 0x50 FAT first sector */
|
|---|
| 71 | uint32_t fat_sector_count; /* 0x54 FAT sectors count */
|
|---|
| 72 | uint32_t data_start_sector; /* 0x58 Data region first cluster sector */
|
|---|
| 73 | uint32_t data_clusters; /* 0x5C total clusters count */
|
|---|
| 74 | uint32_t rootdir_cluster; /* 0x60 first cluster of the root dir */
|
|---|
| 75 | uint32_t volume_serial; /* 0x64 volume serial number */
|
|---|
| 76 | struct { /* 0x68 FS version */
|
|---|
| 77 | uint8_t minor;
|
|---|
| 78 | uint8_t major;
|
|---|
| 79 | } __attribute__ ((packed)) version;
|
|---|
| 80 | uint16_t volume_flags; /* 0x6A volume state flags */
|
|---|
| 81 | uint8_t bytes_per_sector; /* 0x6C sector size as (1 << n) */
|
|---|
| 82 | uint8_t sec_per_cluster; /* 0x6D sectors per cluster as (1 << n) */
|
|---|
| 83 | uint8_t fat_count; /* 0x6E always 1 */
|
|---|
| 84 | uint8_t drive_no; /* 0x6F always 0x80 */
|
|---|
| 85 | uint8_t allocated_percent; /* 0x70 percentage of allocated space */
|
|---|
| 86 | uint8_t _reserved2[7]; /* 0x71 reserved */
|
|---|
| 87 | uint8_t bootcode[390]; /* Boot code */
|
|---|
| 88 | uint16_t signature; /* the value of 0xAA55 */
|
|---|
| 89 | } __attribute__((__packed__)) exfat_bs_t;
|
|---|
| 90 |
|
|---|
| 91 | typedef enum {
|
|---|
| 92 | EXFAT_UNKNOW,
|
|---|
| 93 | EXFAT_DIRECTORY,
|
|---|
| 94 | EXFAT_FILE,
|
|---|
| 95 | EXFAT_BITMAP,
|
|---|
| 96 | EXFAT_UCTABLE
|
|---|
| 97 | } exfat_node_type_t;
|
|---|
| 98 |
|
|---|
| 99 | struct exfat_node;
|
|---|
| 100 | struct exfat_idx_t;
|
|---|
| 101 |
|
|---|
| 102 | typedef struct {
|
|---|
| 103 | /** Used indices (position) hash table link. */
|
|---|
| 104 | link_t uph_link;
|
|---|
| 105 | /** Used indices (index) hash table link. */
|
|---|
| 106 | link_t uih_link;
|
|---|
| 107 |
|
|---|
| 108 | fibril_mutex_t lock;
|
|---|
| 109 | devmap_handle_t devmap_handle;
|
|---|
| 110 | fs_index_t index;
|
|---|
| 111 |
|
|---|
| 112 | /* Does parent node fragmented or not? */
|
|---|
| 113 | bool parent_fragmented;
|
|---|
| 114 | /**
|
|---|
| 115 | * Parent node's first cluster.
|
|---|
| 116 | * Zero is used if this node is not linked, in which case nodep must
|
|---|
| 117 | * contain a pointer to the in-core node structure.
|
|---|
| 118 | * One is used when the parent is the root directory.
|
|---|
| 119 | */
|
|---|
| 120 | exfat_cluster_t pfc;
|
|---|
| 121 | /** Directory entry index within the parent node. */
|
|---|
| 122 | unsigned pdi;
|
|---|
| 123 | /** Pointer to in-core node instance. */
|
|---|
| 124 | struct exfat_node *nodep;
|
|---|
| 125 | } exfat_idx_t;
|
|---|
| 126 |
|
|---|
| 127 | /** exFAT in-core node. */
|
|---|
| 128 | typedef struct exfat_node {
|
|---|
| 129 | /** Back pointer to the FS node. */
|
|---|
| 130 | fs_node_t *bp;
|
|---|
| 131 |
|
|---|
| 132 | fibril_mutex_t lock;
|
|---|
| 133 | exfat_node_type_t type;
|
|---|
| 134 | exfat_idx_t *idx;
|
|---|
| 135 | /**
|
|---|
| 136 | * Node's first cluster.
|
|---|
| 137 | * Zero is used for zero-length nodes.
|
|---|
| 138 | * One is used to mark root directory.
|
|---|
| 139 | */
|
|---|
| 140 | exfat_cluster_t firstc;
|
|---|
| 141 | /** exFAT in-core node free list link. */
|
|---|
| 142 | link_t ffn_link;
|
|---|
| 143 | aoff64_t size;
|
|---|
| 144 | unsigned lnkcnt;
|
|---|
| 145 | unsigned refcnt;
|
|---|
| 146 | bool dirty;
|
|---|
| 147 | /* Should we do walk-on-FAT or not */
|
|---|
| 148 | bool fragmented;
|
|---|
| 149 |
|
|---|
| 150 | /*
|
|---|
| 151 | * Cache of the node's last and "current" cluster to avoid some
|
|---|
| 152 | * unnecessary FAT walks.
|
|---|
| 153 | */
|
|---|
| 154 | /* Node's last cluster in FAT. */
|
|---|
| 155 | bool lastc_cached_valid;
|
|---|
| 156 | exfat_cluster_t lastc_cached_value;
|
|---|
| 157 | /* Node's "current" cluster, i.e. where the last I/O took place. */
|
|---|
| 158 | bool currc_cached_valid;
|
|---|
| 159 | aoff64_t currc_cached_bn;
|
|---|
| 160 | exfat_cluster_t currc_cached_value;
|
|---|
| 161 | } exfat_node_t;
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | extern fs_reg_t exfat_reg;
|
|---|
| 165 |
|
|---|
| 166 | extern void exfat_mounted(ipc_callid_t, ipc_call_t *);
|
|---|
| 167 | extern void exfat_mount(ipc_callid_t, ipc_call_t *);
|
|---|
| 168 | extern void exfat_unmounted(ipc_callid_t, ipc_call_t *);
|
|---|
| 169 | extern void exfat_unmount(ipc_callid_t, ipc_call_t *);
|
|---|
| 170 | extern void exfat_lookup(ipc_callid_t, ipc_call_t *);
|
|---|
| 171 | extern void exfat_read(ipc_callid_t, ipc_call_t *);
|
|---|
| 172 | extern void exfat_write(ipc_callid_t, ipc_call_t *);
|
|---|
| 173 | extern void exfat_truncate(ipc_callid_t, ipc_call_t *);
|
|---|
| 174 | extern void exfat_stat(ipc_callid_t, ipc_call_t *);
|
|---|
| 175 | extern void exfat_close(ipc_callid_t, ipc_call_t *);
|
|---|
| 176 | extern void exfat_destroy(ipc_callid_t, ipc_call_t *);
|
|---|
| 177 | extern void exfat_open_node(ipc_callid_t, ipc_call_t *);
|
|---|
| 178 | extern void exfat_stat(ipc_callid_t, ipc_call_t *);
|
|---|
| 179 | extern void exfat_sync(ipc_callid_t, ipc_call_t *);
|
|---|
| 180 |
|
|---|
| 181 | extern int exfat_idx_get_new(exfat_idx_t **, devmap_handle_t);
|
|---|
| 182 | extern exfat_idx_t *exfat_idx_get_by_pos(devmap_handle_t, exfat_cluster_t, unsigned);
|
|---|
| 183 | extern exfat_idx_t *exfat_idx_get_by_index(devmap_handle_t, fs_index_t);
|
|---|
| 184 | extern void exfat_idx_destroy(exfat_idx_t *);
|
|---|
| 185 | extern void exfat_idx_hashin(exfat_idx_t *);
|
|---|
| 186 | extern void exfat_idx_hashout(exfat_idx_t *);
|
|---|
| 187 |
|
|---|
| 188 | extern int exfat_idx_init(void);
|
|---|
| 189 | extern void exfat_idx_fini(void);
|
|---|
| 190 | extern int exfat_idx_init_by_devmap_handle(devmap_handle_t);
|
|---|
| 191 | extern void exfat_idx_fini_by_devmap_handle(devmap_handle_t);
|
|---|
| 192 |
|
|---|
| 193 | #endif
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * @}
|
|---|
| 197 | */
|
|---|