[db9aa04] | 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 | * @{
|
---|
[1b20da0] | 32 | */
|
---|
[db9aa04] | 33 |
|
---|
| 34 | #ifndef EXFAT_EXFAT_H_
|
---|
[944aa24] | 35 | #define EXFAT_EXFAT_H_
|
---|
[db9aa04] | 36 |
|
---|
[4c3c4a5] | 37 | #include "exfat_fat.h"
|
---|
[db9aa04] | 38 | #include <fibril_synch.h>
|
---|
| 39 | #include <libfs.h>
|
---|
| 40 | #include <atomic.h>
|
---|
[8d2dd7f2] | 41 | #include <stdint.h>
|
---|
[3e6a98c5] | 42 | #include <stdbool.h>
|
---|
[db9aa04] | 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 |
|
---|
[7be14db] | 52 | #define BPS(bs) ((uint32_t) (1 << (bs->bytes_per_sector)))
|
---|
[ff0c270] | 53 | #define SPC(bs) ((uint32_t) (1 << (bs->sec_per_cluster)))
|
---|
| 54 | #define BPC(bs) ((uint32_t) (BPS(bs) * SPC(bs)))
|
---|
[e584623] | 55 | #define VOL_FS(bs) uint64_t_le2host(bs->volume_start)
|
---|
[6d57e1c] | 56 | #define VOL_CNT(bs) uint64_t_le2host(bs->volume_count)
|
---|
[e584623] | 57 | #define FAT_FS(bs) uint32_t_le2host(bs->fat_sector_start)
|
---|
[6d57e1c] | 58 | #define FAT_CNT(bs) uint32_t_le2host(bs->fat_sector_count)
|
---|
[e584623] | 59 | #define DATA_FS(bs) uint32_t_le2host(bs->data_start_sector)
|
---|
[ff0c270] | 60 | #define DATA_CNT(bs) uint32_t_le2host(bs->data_clusters)
|
---|
[e584623] | 61 | #define ROOT_FC(bs) uint32_t_le2host(bs->rootdir_cluster)
|
---|
[ff0c270] | 62 | #define VOL_FLAGS(bs) uint16_t_le2host(bs->volume_flags)
|
---|
[db9aa04] | 63 |
|
---|
[5d5863c] | 64 | #define EXFAT_NODE(node) ((node) ? (exfat_node_t *) (node)->data : NULL)
|
---|
| 65 | #define FS_NODE(node) ((node) ? (node)->bp : NULL)
|
---|
| 66 | #define DPS(bs) (BPS((bs)) / sizeof(exfat_dentry_t))
|
---|
[db9aa04] | 67 |
|
---|
[6d57e1c] | 68 | typedef struct exfat_bs {
|
---|
| 69 | uint8_t jump[3]; /* 0x00 jmp and nop instructions */
|
---|
| 70 | uint8_t oem_name[8]; /* 0x03 "EXFAT " */
|
---|
| 71 | uint8_t __reserved[53]; /* 0x0B always 0 */
|
---|
| 72 | uint64_t volume_start; /* 0x40 partition first sector */
|
---|
| 73 | uint64_t volume_count; /* 0x48 partition sectors count */
|
---|
| 74 | uint32_t fat_sector_start; /* 0x50 FAT first sector */
|
---|
| 75 | uint32_t fat_sector_count; /* 0x54 FAT sectors count */
|
---|
| 76 | uint32_t data_start_sector; /* 0x58 Data region first cluster sector */
|
---|
| 77 | uint32_t data_clusters; /* 0x5C total clusters count */
|
---|
| 78 | uint32_t rootdir_cluster; /* 0x60 first cluster of the root dir */
|
---|
| 79 | uint32_t volume_serial; /* 0x64 volume serial number */
|
---|
| 80 | struct { /* 0x68 FS version */
|
---|
| 81 | uint8_t minor;
|
---|
| 82 | uint8_t major;
|
---|
| 83 | } __attribute__ ((packed)) version;
|
---|
| 84 | uint16_t volume_flags; /* 0x6A volume state flags */
|
---|
| 85 | uint8_t bytes_per_sector; /* 0x6C sector size as (1 << n) */
|
---|
| 86 | uint8_t sec_per_cluster; /* 0x6D sectors per cluster as (1 << n) */
|
---|
| 87 | uint8_t fat_count; /* 0x6E always 1 */
|
---|
| 88 | uint8_t drive_no; /* 0x6F always 0x80 */
|
---|
| 89 | uint8_t allocated_percent; /* 0x70 percentage of allocated space */
|
---|
| 90 | uint8_t _reserved2[7]; /* 0x71 reserved */
|
---|
| 91 | uint8_t bootcode[390]; /* Boot code */
|
---|
| 92 | uint16_t signature; /* the value of 0xAA55 */
|
---|
| 93 | } __attribute__((__packed__)) exfat_bs_t;
|
---|
| 94 |
|
---|
[4c3c4a5] | 95 | typedef enum {
|
---|
[0b0b69f] | 96 | EXFAT_UNKNOW,
|
---|
[4c3c4a5] | 97 | EXFAT_DIRECTORY,
|
---|
[0b0b69f] | 98 | EXFAT_FILE,
|
---|
| 99 | EXFAT_BITMAP,
|
---|
| 100 | EXFAT_UCTABLE
|
---|
[4c3c4a5] | 101 | } exfat_node_type_t;
|
---|
| 102 |
|
---|
| 103 | struct exfat_node;
|
---|
[e584623] | 104 | struct exfat_idx_t;
|
---|
[4c3c4a5] | 105 |
|
---|
| 106 | typedef struct {
|
---|
| 107 | /** Used indices (position) hash table link. */
|
---|
[062d900] | 108 | ht_link_t uph_link;
|
---|
[4c3c4a5] | 109 | /** Used indices (index) hash table link. */
|
---|
[062d900] | 110 | ht_link_t uih_link;
|
---|
[4c3c4a5] | 111 |
|
---|
| 112 | fibril_mutex_t lock;
|
---|
[375ab5e] | 113 | service_id_t service_id;
|
---|
[4c3c4a5] | 114 | fs_index_t index;
|
---|
[e584623] | 115 |
|
---|
| 116 | /* Does parent node fragmented or not? */
|
---|
| 117 | bool parent_fragmented;
|
---|
[4c3c4a5] | 118 | /**
|
---|
| 119 | * Parent node's first cluster.
|
---|
| 120 | * Zero is used if this node is not linked, in which case nodep must
|
---|
| 121 | * contain a pointer to the in-core node structure.
|
---|
| 122 | * One is used when the parent is the root directory.
|
---|
| 123 | */
|
---|
| 124 | exfat_cluster_t pfc;
|
---|
| 125 | /** Directory entry index within the parent node. */
|
---|
| 126 | unsigned pdi;
|
---|
| 127 | /** Pointer to in-core node instance. */
|
---|
| 128 | struct exfat_node *nodep;
|
---|
| 129 | } exfat_idx_t;
|
---|
| 130 |
|
---|
| 131 | /** exFAT in-core node. */
|
---|
| 132 | typedef struct exfat_node {
|
---|
| 133 | /** Back pointer to the FS node. */
|
---|
| 134 | fs_node_t *bp;
|
---|
| 135 |
|
---|
| 136 | fibril_mutex_t lock;
|
---|
| 137 | exfat_node_type_t type;
|
---|
| 138 | exfat_idx_t *idx;
|
---|
| 139 | /**
|
---|
| 140 | * Node's first cluster.
|
---|
| 141 | * Zero is used for zero-length nodes.
|
---|
| 142 | * One is used to mark root directory.
|
---|
| 143 | */
|
---|
| 144 | exfat_cluster_t firstc;
|
---|
| 145 | /** exFAT in-core node free list link. */
|
---|
| 146 | link_t ffn_link;
|
---|
| 147 | aoff64_t size;
|
---|
| 148 | unsigned lnkcnt;
|
---|
| 149 | unsigned refcnt;
|
---|
| 150 | bool dirty;
|
---|
[0b0b69f] | 151 | /* Should we do walk-on-FAT or not */
|
---|
| 152 | bool fragmented;
|
---|
[4c3c4a5] | 153 |
|
---|
| 154 | /*
|
---|
| 155 | * Cache of the node's last and "current" cluster to avoid some
|
---|
| 156 | * unnecessary FAT walks.
|
---|
| 157 | */
|
---|
| 158 | /* Node's last cluster in FAT. */
|
---|
| 159 | bool lastc_cached_valid;
|
---|
| 160 | exfat_cluster_t lastc_cached_value;
|
---|
| 161 | /* Node's "current" cluster, i.e. where the last I/O took place. */
|
---|
| 162 | bool currc_cached_valid;
|
---|
| 163 | aoff64_t currc_cached_bn;
|
---|
| 164 | exfat_cluster_t currc_cached_value;
|
---|
| 165 | } exfat_node_t;
|
---|
| 166 |
|
---|
[6d57e1c] | 167 |
|
---|
[f1119e5] | 168 | extern vfs_out_ops_t exfat_ops;
|
---|
| 169 | extern libfs_ops_t exfat_libfs_ops;
|
---|
[4c3c4a5] | 170 |
|
---|
[b7fd2a0] | 171 | extern errno_t exfat_idx_get_new(exfat_idx_t **, service_id_t);
|
---|
[375ab5e] | 172 | extern exfat_idx_t *exfat_idx_get_by_pos(service_id_t, exfat_cluster_t, unsigned);
|
---|
| 173 | extern exfat_idx_t *exfat_idx_get_by_index(service_id_t, fs_index_t);
|
---|
[4c3c4a5] | 174 | extern void exfat_idx_destroy(exfat_idx_t *);
|
---|
| 175 | extern void exfat_idx_hashin(exfat_idx_t *);
|
---|
| 176 | extern void exfat_idx_hashout(exfat_idx_t *);
|
---|
| 177 |
|
---|
[b7fd2a0] | 178 | extern errno_t exfat_idx_init(void);
|
---|
[4c3c4a5] | 179 | extern void exfat_idx_fini(void);
|
---|
[b7fd2a0] | 180 | extern errno_t exfat_idx_init_by_service_id(service_id_t);
|
---|
[375ab5e] | 181 | extern void exfat_idx_fini_by_service_id(service_id_t);
|
---|
[db9aa04] | 182 |
|
---|
[b7fd2a0] | 183 | extern errno_t exfat_node_expand(service_id_t, exfat_node_t *, exfat_cluster_t);
|
---|
| 184 | extern errno_t exfat_node_put(fs_node_t *);
|
---|
| 185 | extern errno_t exfat_bitmap_get(fs_node_t **, service_id_t);
|
---|
| 186 | extern errno_t exfat_uctable_get(fs_node_t **, service_id_t);
|
---|
[5d5863c] | 187 |
|
---|
| 188 |
|
---|
[db9aa04] | 189 | #endif
|
---|
| 190 |
|
---|
| 191 | /**
|
---|
| 192 | * @}
|
---|
| 193 | */
|
---|