2 * fat-handler - FAT12/16/32 filesystem handler
4 * Copyright © 2006 Marek Szyprowski
5 * Copyright © 2007-2018 The AROS Development Team
7 * This program is free software; you can redistribute it and/or modify it
8 * under the same terms as AROS itself.
16 #define DEBUG_DIRENTRY 0
21 #define DEBUG_NOTIFY 0
23 #define DEBUG_PACKETS 0
24 #define DEBUG_CACHESTATS 0
28 #include <exec/interrupts.h>
30 #include "fat_struct.h"
34 /* Supported Filesystem Ids */
35 #if !defined(ID_FAT12_DISK)
36 #define ID_FAT12_DISK 0x46415400UL
38 #if !defined(ID_FAT16_DISK)
39 #define ID_FAT16_DISK 0x46415401UL
41 #if !defined(ID_FAT32_DISK)
42 #define ID_FAT32_DISK 0x46415402UL
44 #if !defined(ID_FAT_DISK)
45 #define ID_FAT_DISK ID_FAT12_DISK
48 /* Filesystem structures */
50 #define ACTION_VOLUME_ADD 16000
51 #define ACTION_VOLUME_REMOVE 16001
53 #define DEF_POOL_SIZE 65536
54 #define DEF_POOL_THRESHOLD DEF_POOL_SIZE
57 /* A handle on something, file or directory */
60 struct FSSuper
*sb
; /* filesystem data */
62 ULONG first_cluster
; /* first cluster of this file */
63 ULONG cur_cluster
; /* cluster that the current sector is within */
65 ULONG cluster_offset
; /* cluster number of this cluster within the current file */
67 ULONG first_sector
; /* first sector in the first cluster, for fat12/16 root dir */
68 ULONG cur_sector
; /* sector number our block is currently in */
70 ULONG sector_offset
; /* current sector as an offset in the current cluster
71 i.e. cur = sector(cur_cluster) + offset */
73 APTR block
; /* current block from the cache */
74 UBYTE
*data
; /* current data buffer (from cache) */
77 /* A handle on a directory */
82 ULONG cur_index
; /* last entry returned, for GetNextDirEntry */
85 /* Single directory entry */
88 struct FSSuper
*sb
; /* filesystem data */
90 ULONG cluster
; /* cluster the containing directory starts at */
91 ULONG index
; /* index of this entry */
93 ULONG pos
; /* byte offset within directory that the entry came from */
96 struct FATDirEntry entry
;
97 struct FATLongDirEntry long_entry
;
101 #define FAT_ROOTDIR_MARK 0xFFFFFFFFlu
107 /* struct FileLock */
111 struct MsgPort
* fl_Task
;
114 ULONG magic
; /* we set this to ID_FAT_DISK so we can tell
115 our locks from those of other filesystems */
117 struct MinNode node
; /* node in the list of locks attached to the global lock */
118 struct GlobalLock
*gl
; /* pointer to the global lock for this file */
120 struct IOHandle ioh
; /* handle for reads and writes */
121 ULONG pos
; /* current seek position within the file */
123 BOOL do_notify
; /* if set, send notification on file close (ACTION_END) */
124 struct FSSuper
*sb
; /* pointer to sb, for unlocking when volume is removed */
131 ULONG dir_cluster
; /* first cluster of the directory we're in */
132 ULONG dir_entry
; /* this is our dir entry within dir_cluster */
134 LONG access
; /* access mode, shared or exclusive */
136 ULONG first_cluster
; /* first data cluster */
138 ULONG attr
; /* file attributes, from the dir entry */
139 ULONG size
; /* file size, from the dir entry */
141 UBYTE name
[FAT_MAX_LONG_FILENAME
]; /* copy of the name (bstr) */
143 UBYTE shortname
[FAT_MAX_SHORT_NAME
+ 2]; /* copy of the short name (bstr) */
146 struct MinList locks
; /* list of ExtFileLocks opened on this file */
149 /* A node in the list of notification requests */
154 struct GlobalLock
*gl
; /* pointer to global lock if this file is
155 locked. if it's not, this is NULL */
157 struct NotifyRequest
*nr
; /* the request that DOS passed us */
164 struct MinList locks
; /* global locks */
165 struct GlobalLock root_lock
;
166 struct MinList notifies
;
169 struct VolumeIdentity
171 UBYTE name
[FAT_MAX_SHORT_NAME
+ 2]; /* BCPL string */
172 struct DateStamp create_time
;
179 struct Globals
*glob
;
180 struct DosList
*doslist
;
182 struct VolumeInfo
*info
;
185 ULONG first_device_sector
;
188 ULONG sectorsize_bits
;
190 ULONG cluster_sectors
;
192 ULONG clustersize_bits
;
193 ULONG cluster_sectors_bits
;
195 ULONG first_fat_sector
;
196 ULONG first_data_sector
;
197 ULONG first_rootdir_sector
;
199 ULONG rootdir_sectors
;
202 ULONG clusters_count
;
215 ULONG fat_blocks_count
;
218 ULONG fat_cachesize_bits
;
219 ULONG fat_cache_block
;
220 UWORD fat_cache_no
; /* FAT number that cached FAT blocks belong to */
223 struct FATFSInfo
*fsinfo_buffer
;
225 ULONG rootdir_cluster
;
226 ULONG rootdir_sector
;
228 struct VolumeIdentity volume
;
231 ULONG (*func_get_fat_entry
)(struct FSSuper
*sb
, ULONG n
);
232 BOOL (*func_set_fat_entry
)(struct FSSuper
*sb
, ULONG n
, ULONG val
);
239 struct ExecBase
*gl_SysBase
;
240 struct DosLibrary
*gl_DOSBase
;
241 struct Library
*gl_UtilityBase
;
242 struct Device
*gl_TimerBase
;
245 struct Task
*ourtask
;
246 struct MsgPort
*ourport
;
249 struct MsgPort
*notifyport
;
252 struct DosList
*devnode
;
253 struct FileSysStartupMsg
*fssm
;
255 struct DosPacket
*death_packet
;
259 struct IOExtTD
*diskioreq
;
260 struct IOExtTD
*diskchgreq
;
261 struct MsgPort
*diskport
;
262 ULONG diskchgsig_bit
;
263 struct timerequest
*timereq
;
264 struct MsgPort
*timerport
;
265 ULONG last_num
; /* last block number that was outside boundaries */
272 struct FSSuper
*sb
; /* current sb */
273 struct MinList sblist
; /* sbs with outstanding locks or notifies */
280 /* Character sets translation */
281 UBYTE from_unicode
[65536];
282 UWORD to_unicode
[256];
284 /* Disk change interrupt */
286 struct Interrupt Interrupt
;
287 struct ExecBase
*SysBase
;
293 #define DOSBase (glob->gl_DOSBase)
294 #define SysBase (glob->gl_SysBase)
295 #define UtilityBase (glob->gl_UtilityBase)
296 #define TimerBase (glob->gl_TimerBase)
300 /* Get the first sector of a cluster */
301 #define SECTOR_FROM_CLUSTER(sb,cl) \
302 ((ULONG) (((cl-2) << sb->cluster_sectors_bits) + sb->first_data_sector))
304 #define FIRST_FILE_CLUSTER(de) \
305 (AROS_LE2WORD((de)->e.entry.first_cluster_lo) | \
306 (((ULONG) AROS_LE2WORD((de)->e.entry.first_cluster_hi)) << 16))
308 #define RESET_HANDLE(ioh) \
311 (ioh)->cluster_offset = (ioh)->sector_offset = 0xffffffff; \
312 if ((ioh)->block != NULL) \
314 Cache_FreeBlock((ioh)->sb->cache, (ioh)->block); \
315 (ioh)->block = NULL; \
320 #define RESET_DIRHANDLE(dh) \
323 RESET_HANDLE(&((dh)->ioh)); \
324 (dh)->cur_index = 0xffffffff; \
328 #define GET_NEXT_CLUSTER(sb,cl) (sb->func_get_fat_entry(sb,cl))
329 #define SET_NEXT_CLUSTER(sb,cl,val) (sb->func_set_fat_entry(sb,cl,val))
331 #define CALC_SHORT_NAME_CHECKSUM(name,checksum) \
336 for (i = 0; i < FAT_MAX_SHORT_NAME; i++) \
338 ((checksum & 1) ? 0x80 : 0) + (checksum >> 1) + name[i]; \
342 #define LOCKFROMNODE(A) \
343 ((struct ExtFileLock *) \
344 (((BYTE *)(A)) - (IPTR)&((struct ExtFileLock *)NULL)->node))