1 (* 'df' command for virtual domains.
2 (C) Copyright 2007 Richard W.M. Jones, Red Hat Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 (* Private data that we'll save about the FAT filesystem. *)
32 fat_dev : device; (* Device. *)
33 fat_blocksize : int63; (* Cluster size. *)
34 fat_sectorsize : int63; (* Sector size. *)
35 fat_sectors_per_cluster : int63; (* Sectors per cluster. *)
36 fat_number_of_sectors : int63; (* Total number of sectors in dev. *)
37 fat_reserved_sectors : int63; (* Offset of the first FAT. *)
38 fat_sectors_per_fat : int63; (* Size of each FAT. *)
39 fat_root_directory_cluster : int63; (* Cluster offset of root dir. *)
42 (* Private data functions. *)
43 let attach_private_data, get_private_data =
44 private_data_functions (fun {fs_cb = {fs_cb_uq = u}} -> u)
50 | Used of int63 (* Used, points to next cluster *)
51 | Reserved of int63 (* 0x_FFFFFF0 - 0x_FFFFFF6 *)
52 | Bad (* Bad / reserved 0x_FFFFFF7 *)
53 | Last of int63 (* Last cluster in file *)
56 let fs = probe_superblock dev in
59 and probe_superblock dev =
60 (* Load the boot sector / superblock. *)
61 let bits = dev#read_bitstring ~^0 ~^512 in
63 (* Standard stuff in a boot sector. *)
65 | { _ : 24; (* Jump to boot up code. *)
66 oem_name : 64 : string; (* OEM name. *)
67 bytes_per_sector : 16 : littleendian;
68 sectors_per_cluster : 8 : littleendian;
69 reserved_sectors : 16 : littleendian;
70 2 : 8; (* Number of FATs. *)
71 0 : 16; (* Root dir entries, 0 for FAT32 *)
72 0 : 16; (* Old number of sectors - unused. *)
73 _ : 8; (* Media type, probably 'f8' = HDD *)
74 _ : 16; (* Sectors per FAT, not for FAT32 *)
75 _ : 16; (* Sectors per track. *)
77 _ : 32; (* Hidden sectors. *)
78 number_of_sectors : 32 : littleendian;
79 sectors_per_fat : 32 : littleendian;
80 _ : 16; (* FAT32 flags *)
81 _ : 16; (* FAT32 version *)
82 root_directory_cluster : 32 : littleendian;
83 _ : 16; (* FS information sector *)
84 _ : 16; (* Backup of boot sector *)
85 _ : 12*8 : bitstring; (* Reserved *)
86 _ : 8; (* Physical drive number *)
88 _ : 8; (* Extended boot signature *)
89 serial : 32 : littleendian; (* Serial number. *)
90 volume_label : 88 : string; (* Volume label. *)
91 "FAT32 " : 64 : string; (* FAT32 identifier. *)
92 _ : 420*8 : bitstring; (* Boot code. *)
93 0x55AA : 16 } -> (* End of bootsector magic. *)
95 let blocksize = bytes_per_sector * sectors_per_cluster in
97 (* FAT32 forbids cluster size > 32K. *)
98 if blocksize > 32768 then (
99 prerr_endline "FAT: cluster size > 32K";
104 eprintf "%s: found FAT32 filesystem with clustersize = %d, label %s\n"
105 dev#name blocksize volume_label;
107 let blocksize = Int63.of_int blocksize in
108 let sectorsize = Int63.of_int bytes_per_sector in
109 let sectors_per_cluster = Int63.of_int sectors_per_cluster in
110 let number_of_sectors = Int63.of_int32 number_of_sectors in
111 let reserved_sectors = Int63.of_int reserved_sectors in
112 let sectors_per_fat = Int63.of_int32 sectors_per_fat in
113 let root_directory_cluster = Int63.of_int32 root_directory_cluster in
115 (* The blocksize of the filesystem is likely to be quite different
116 * from that of the underlying device, so create an overlay device
117 * with the natural filesystem blocksize.
119 let fs_dev = new blocksize_overlay blocksize dev in
123 fat_blocksize = blocksize;
124 fat_sectorsize = sectorsize;
125 fat_sectors_per_cluster = sectors_per_cluster;
126 fat_number_of_sectors = number_of_sectors;
127 fat_reserved_sectors = reserved_sectors;
128 fat_sectors_per_fat = sectors_per_fat;
129 fat_root_directory_cluster = root_directory_cluster
132 (* Parse the first FAT to get free space. *)
133 let blocks_used, blocks_avail = parse_fat_freespace fat in
135 (* Calculate reserved space. *)
136 let blocks_reserved =
137 let sectors_reserved = reserved_sectors +^ ~^2 *^ sectors_per_fat in
138 sectors_reserved *^ sectors_per_cluster in
140 (* Create a filesystem structure. *)
142 fs_cb = callbacks ();
144 fs_blocksize = blocksize;
145 fs_blocks_total = number_of_sectors *^ sectorsize /^ blocksize;
147 fs_blocks_reserved = blocks_reserved;
148 fs_blocks_avail = blocks_avail;
149 fs_blocks_used = blocks_used;
150 fs_inodes_total = ~^0;
151 fs_inodes_reserved = ~^0;
152 fs_inodes_avail = ~^0;
153 fs_inodes_used = ~^0;
156 attach_private_data fs fat;
159 | { _ } -> raise Not_found (* Not FAT32 *)
161 (* Find amount of free space by reading the FAT. *)
162 and parse_fat_freespace fat =
163 let used = ref ~^0 and free = ref ~^0 in
164 iter_fat_entries fat (
167 | Free -> free := !free +^ ~^1
168 | _ -> used := !used +^ ~^1
172 (* Iterate over the FAT. We ignore the first two FAT entries. *)
173 and iter_fat_entries { fat_dev = dev; fat_sectorsize = sectorsize;
174 fat_reserved_sectors = start;
175 fat_sectors_per_fat = size } f =
178 for i = 0 to (Int63.to_int size)-1 do
180 dev#read_bitstring ((start +^ Int63.of_int i) *^ sectorsize) sectorsize in
183 | { e : 28 : littleendian; _ : 4; rest : -1 : bitstring } ->
188 | (0xffffff0|0xffffff1|0xffffff2|0xffffff3|
189 0xffffff4|0xffffff5|0xffffff6) as r ->
190 Reserved (Int63.of_int r)
192 | (0xffffff8|0xffffff9|0xffffffa|0xffffffb|
193 0xffffffc|0xffffffd|0xffffffe|
194 0xfffffff) as r -> Last (Int63.of_int r)
195 | _ -> Used (Int63.of_int e) in
197 if !cn >= ~^2 then f !cn e;
203 eprintf "iter_fat_entries: unknown FAT entry, ignored\n%!";
209 and offset_is_free _ _ = false
214 fs_cb_uq = (incr i; !i);
216 fs_cb_printable_name = "DOS/Windows";
217 fs_cb_offset_is_free = offset_is_free;
220 (* Register the plugin. *)
221 let () = register_plugin ~filesystem:probe id