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) ((uint32_t) (1 << (bs->bytes_per_sector)))
|
---|
53 | #define SPC(bs) ((uint32_t)(1 << (bs->sec_per_cluster)))
|
---|
54 | #define BPC(bs) ((uint32_t)(BPS(bs)*SPC(bs)))
|
---|
55 | #define VOL_FS(bs) uint64_t_le2host(bs->volume_start)
|
---|
56 | #define VOL_CNT(bs) uint64_t_le2host(bs->volume_count)
|
---|
57 | #define FAT_FS(bs) uint32_t_le2host(bs->fat_sector_start)
|
---|
58 | #define FAT_CNT(bs) uint32_t_le2host(bs->fat_sector_count)
|
---|
59 | #define DATA_FS(bs) uint32_t_le2host(bs->data_start_sector)
|
---|
60 | #define DATA_CNT(bs) uint32_t_le2host(bs->data_clusters)
|
---|
61 | #define ROOT_FC(bs) uint32_t_le2host(bs->rootdir_cluster)
|
---|
62 | #define VOL_FLAGS(bs) uint16_t_le2host(bs->volume_flags)
|
---|
63 |
|
---|
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))
|
---|
67 |
|
---|
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 |
|
---|
95 | typedef enum {
|
---|
96 | EXFAT_UNKNOW,
|
---|
97 | EXFAT_DIRECTORY,
|
---|
98 | EXFAT_FILE,
|
---|
99 | EXFAT_BITMAP,
|
---|
100 | EXFAT_UCTABLE
|
---|
101 | } exfat_node_type_t;
|
---|
102 |
|
---|
103 | struct exfat_node;
|
---|
104 | struct exfat_idx_t;
|
---|
105 |
|
---|
106 | typedef struct {
|
---|
107 | /** Used indices (position) hash table link. */
|
---|
108 | link_t uph_link;
|
---|
109 | /** Used indices (index) hash table link. */
|
---|
110 | link_t uih_link;
|
---|
111 |
|
---|
112 | fibril_mutex_t lock;
|
---|
113 | devmap_handle_t devmap_handle;
|
---|
114 | fs_index_t index;
|
---|
115 |
|
---|
116 | /* Does parent node fragmented or not? */
|
---|
117 | bool parent_fragmented;
|
---|
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;
|
---|
151 | /* Should we do walk-on-FAT or not */
|
---|
152 | bool fragmented;
|
---|
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 |
|
---|
167 |
|
---|
168 | extern vfs_out_ops_t exfat_ops;
|
---|
169 | extern libfs_ops_t exfat_libfs_ops;
|
---|
170 |
|
---|
171 | extern int exfat_idx_get_new(exfat_idx_t **, devmap_handle_t);
|
---|
172 | extern exfat_idx_t *exfat_idx_get_by_pos(devmap_handle_t, exfat_cluster_t, unsigned);
|
---|
173 | extern exfat_idx_t *exfat_idx_get_by_index(devmap_handle_t, fs_index_t);
|
---|
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 |
|
---|
178 | extern int exfat_idx_init(void);
|
---|
179 | extern void exfat_idx_fini(void);
|
---|
180 | extern int exfat_idx_init_by_devmap_handle(devmap_handle_t);
|
---|
181 | extern void exfat_idx_fini_by_devmap_handle(devmap_handle_t);
|
---|
182 |
|
---|
183 | extern int exfat_node_put(fs_node_t *);
|
---|
184 | extern int exfat_bitmap_get(fs_node_t **, devmap_handle_t);
|
---|
185 | /*
|
---|
186 | static int exfat_uctable_get(fs_node_t **, devmap_handle_t);
|
---|
187 | */
|
---|
188 |
|
---|
189 |
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * @}
|
---|
194 | */
|
---|