summaryrefslogtreecommitdiff
path: root/libparted/fs/r/fat/calc.c
blob: 45c17090726ef3fb9c969b3c412352829d4292eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
    libparted
    Copyright (C) 1998-2000, 2002, 2007, 2009-2014, 2019-2022 Free Software
    Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>
#include "fat.h"

#ifndef DISCOVER_ONLY

/* returns the minimum size of clusters for a given file system type */
PedSector _GL_ATTRIBUTE_CONST
fat_min_cluster_size (FatType fat_type) {
	switch (fat_type) {
		case FAT_TYPE_FAT12: return 1;
		case FAT_TYPE_FAT16: return 1024/512;
		case FAT_TYPE_FAT32: return 4096/512;
	}
	return 0;
}

static PedSector _GL_ATTRIBUTE_CONST
_smallest_power2_over (PedSector ceiling)
{
	PedSector	result = 1;

	while (result < ceiling)
		result *= 2;

	return result;
}

/* returns the minimum size of clusters for a given file system type */
PedSector _GL_ATTRIBUTE_CONST
fat_recommend_min_cluster_size (FatType fat_type, PedSector size) {
	switch (fat_type) {
		case FAT_TYPE_FAT12: return 1;
		case FAT_TYPE_FAT16: return fat_min_cluster_size(fat_type);
		case FAT_TYPE_FAT32:
			return PED_MAX(_smallest_power2_over(size
						/ MAX_FAT32_CLUSTERS),
				       fat_min_cluster_size (fat_type));
	}
	return 0;
}

/* returns the maxmimum size of clusters for a given file system type */
PedSector _GL_ATTRIBUTE_CONST
fat_max_cluster_size (FatType fat_type) {
	switch (fat_type) {
		case FAT_TYPE_FAT12: return 1;	/* dunno... who cares? */
		case FAT_TYPE_FAT16: return 65536/512;
		case FAT_TYPE_FAT32: return 65536/512;
	}
	return 0;
}

/* returns the minimum number of clusters for a given file system type */
FatCluster _GL_ATTRIBUTE_CONST
fat_min_cluster_count (FatType fat_type) {
	switch (fat_type) {
		case FAT_TYPE_FAT12:
		case FAT_TYPE_FAT16:
			return fat_max_cluster_count (fat_type) / 2;

		case FAT_TYPE_FAT32: return 0xfff0;
	}
	return 0;
}

/* returns the maximum number of clusters for a given file system type */
FatCluster _GL_ATTRIBUTE_CONST
fat_max_cluster_count (FatType fat_type) {
	switch (fat_type) {
		case FAT_TYPE_FAT12: return 0xff0;
		case FAT_TYPE_FAT16: return 0xfff0;
		case FAT_TYPE_FAT32: return 0x0ffffff0;
	}
	return 0;
}

/* what is this supposed to be?  What drugs are M$ on?  (Can I have some? :-) */
PedSector _GL_ATTRIBUTE_CONST
fat_min_reserved_sector_count (FatType fat_type)
{
	return (fat_type == FAT_TYPE_FAT32) ? 32 : 1;
}

int
fat_check_resize_geometry (const PedFileSystem* fs,
			   const PedGeometry* geom,
			   PedSector new_cluster_sectors,
			   FatCluster new_cluster_count)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);
	PedSector	free_space;
	PedSector	min_free_space;
	PedSector	total_space;
	PedSector	new_total_space;
	PedSector	dir_space;

	PED_ASSERT (geom != NULL);

	dir_space = fs_info->total_dir_clusters * fs_info->cluster_sectors;
	free_space = fs_info->fat->free_cluster_count
			* fs_info->cluster_sectors;
	total_space = fs_info->fat->cluster_count * fs_info->cluster_sectors;
	new_total_space = new_cluster_count * new_cluster_sectors;
	min_free_space = total_space - new_total_space + dir_space;

	PED_ASSERT (new_cluster_count
		    <= fat_max_cluster_count (FAT_TYPE_FAT32));

	if (free_space < min_free_space) {
		char* needed = ped_unit_format (geom->dev, min_free_space);
		char* have = ped_unit_format (geom->dev, free_space);
		ped_exception_throw (
			PED_EXCEPTION_ERROR,
			PED_EXCEPTION_CANCEL,
			_("You need %s of free disk space to shrink this "
			  "partition to this size.  Currently, only %s is "
			  "free."),
			needed, have);
		free (needed);
		free (have);
		return 0;
	}

	return 1;
}


/******************************************************************************/

/* DO NOT EDIT THIS ALGORITHM!
 * As far as I can tell, this is the same algorithm used by Microsoft to
 * calculate the size of the file allocaion tables, and the number of clusters.
 * I have not verified this by dissassembling Microsoft code - I came to this
 * conclusion by empirical analysis (i.e. trial and error - this was HORRIBLE).
 *
 * If you think this code makes no sense, then you are right.  I will restrain
 * the urge to inflict serious bodily harm on Microsoft people.
 */

static int
entries_per_sector (FatType fat_type)
{
	switch (fat_type) {
		case FAT_TYPE_FAT12:
			return 512 * 3 / 2;
		case FAT_TYPE_FAT16:
			return 512 / 2;
		case FAT_TYPE_FAT32:
			return 512 / 4;
	}
	return 0;
}

static int
calc_sizes (PedSector size, PedSector align, FatType fat_type,
	    PedSector root_dir_sectors, PedSector cluster_sectors,
	    FatCluster* out_cluster_count, PedSector* out_fat_size)
{
	PedSector	data_fat_space; /* space available to clusters + FAT */
	PedSector	fat_space;	/* space taken by each FAT */
	PedSector	cluster_space;	/* space taken by clusters */
	FatCluster	cluster_count;
	int		i;

	PED_ASSERT (out_cluster_count != NULL);
	PED_ASSERT (out_fat_size != NULL);

	data_fat_space = size - fat_min_reserved_sector_count (fat_type)
			 - align;
	if (fat_type == FAT_TYPE_FAT16)
		data_fat_space -= root_dir_sectors;

	fat_space = 0;
	for (i = 0; i < 2; i++) {
		if (fat_type == FAT_TYPE_FAT32)
			cluster_space = data_fat_space - fat_space;
		else
			cluster_space = data_fat_space - 2 * fat_space;

		cluster_count = cluster_space / cluster_sectors;
		fat_space = ped_div_round_up (cluster_count + 2,
					      entries_per_sector (fat_type));
	}

	cluster_space = data_fat_space - 2 * fat_space;
	cluster_count = cluster_space / cluster_sectors;

	/* looks like this should be part of the loop condition?
	 * Need to build the Big Table TM again to check
	 */
	if (fat_space < ped_div_round_up (cluster_count + 2,
				          entries_per_sector (fat_type))) {
		fat_space = ped_div_round_up (cluster_count + 2,
					      entries_per_sector (fat_type));
	}

	if (cluster_count > fat_max_cluster_count (fat_type)
	    || cluster_count < fat_min_cluster_count (fat_type))
		return 0;

	*out_cluster_count = cluster_count;
	*out_fat_size = fat_space;

	return 1;
}

/****************************************************************************/

int
fat_calc_sizes (PedSector size, PedSector align, FatType fat_type,
		PedSector root_dir_sectors,
		PedSector* out_cluster_sectors, FatCluster* out_cluster_count,
		PedSector* out_fat_size)
{
	PedSector	cluster_sectors;

	PED_ASSERT (out_cluster_sectors != NULL);
	PED_ASSERT (out_cluster_count != NULL);
	PED_ASSERT (out_fat_size != NULL);

	for (cluster_sectors = fat_recommend_min_cluster_size (fat_type, size);
	     cluster_sectors <= fat_max_cluster_size (fat_type);
	     cluster_sectors *= 2) {
		if (calc_sizes (size, align, fat_type, root_dir_sectors,
				cluster_sectors,
			        out_cluster_count, out_fat_size)) {
			*out_cluster_sectors = cluster_sectors;
			return 1;
		}
	}

	for (cluster_sectors = fat_recommend_min_cluster_size (fat_type, size);
	     cluster_sectors >= fat_min_cluster_size (fat_type);
	     cluster_sectors /= 2) {
		if (calc_sizes (size, align, fat_type, root_dir_sectors,
				cluster_sectors,
			        out_cluster_count, out_fat_size)) {
			*out_cluster_sectors = cluster_sectors;
			return 1;
		}
	}

	/* only make the cluster size really small (<4k) if a bigger one is
	 * isn't possible.  Windows never makes FS's like this, but it
	 * seems to work...  (do more tests!)
	 */
	for (cluster_sectors = 4; cluster_sectors > 0; cluster_sectors /= 2) {
		if (calc_sizes (size, align, fat_type, root_dir_sectors,
				cluster_sectors,
				out_cluster_count, out_fat_size)) {
			*out_cluster_sectors = cluster_sectors;
			return 1;
		}
	}

	return 0;
}

/* Same as fat_calc_sizes, except it only attempts to match a particular
 * cluster size.  This is useful, because the FAT resizer can only shrink the
 * cluster size.
 */
int
fat_calc_resize_sizes (
	const PedGeometry* geom,
	PedSector align,
	FatType fat_type,
	PedSector root_dir_sectors,
	PedSector cluster_sectors,
	PedSector* out_cluster_sectors,
	FatCluster* out_cluster_count,
	PedSector* out_fat_size)
{
	PED_ASSERT (geom != NULL);
	PED_ASSERT (out_cluster_sectors != NULL);
	PED_ASSERT (out_cluster_count != NULL);
	PED_ASSERT (out_fat_size != NULL);

/* libparted can only reduce the cluster size at this point */
	for (*out_cluster_sectors = cluster_sectors;
	     *out_cluster_sectors >= fat_min_cluster_size (fat_type);
	     *out_cluster_sectors /= 2) {
		if (calc_sizes (geom->length, align, fat_type, root_dir_sectors,
				*out_cluster_sectors,
				out_cluster_count, out_fat_size))
			return 1;
	}
	return 0;
}

/*  Calculates the number of sectors needed to be added to cluster_offset,
    to make the cluster on the new file system match up with the ones
    on the old file system.
	However, some space is reserved by fat_calc_resize_sizes() and
    friends, to allow room for this space.  If too much of this space is left
    over, everyone will complain, so we have to be greedy, and use it all up...
 */
PedSector _GL_ATTRIBUTE_PURE
fat_calc_align_sectors (const PedFileSystem* new_fs,
			const PedFileSystem* old_fs)
{
	FatSpecific*	old_fs_info = FAT_SPECIFIC (old_fs);
	FatSpecific*	new_fs_info = FAT_SPECIFIC (new_fs);
	PedSector	raw_old_meta_data_end;
	PedSector	new_meta_data_size;
	PedSector	min_new_meta_data_end;
	PedSector	new_data_size;
	PedSector	new_clusters_size;
	PedSector	align;

	new_meta_data_size
		= fat_min_reserved_sector_count (new_fs_info->fat_type)
		  + new_fs_info->fat_sectors * 2;

	if (new_fs_info->fat_type == FAT_TYPE_FAT16)
		new_meta_data_size += new_fs_info->root_dir_sector_count;

	raw_old_meta_data_end = old_fs->geom->start
				 + old_fs_info->cluster_offset;

	min_new_meta_data_end = new_fs->geom->start + new_meta_data_size;

	if (raw_old_meta_data_end > min_new_meta_data_end)
		align = (raw_old_meta_data_end - min_new_meta_data_end)
			% new_fs_info->cluster_sectors;
	else
		align = (new_fs_info->cluster_sectors
		         - (   (min_new_meta_data_end - raw_old_meta_data_end)
				% new_fs_info->cluster_sectors   ))
			% new_fs_info->cluster_sectors;

	new_data_size = new_fs->geom->length - new_meta_data_size;
	new_clusters_size = new_fs_info->cluster_count
				* new_fs_info->cluster_sectors;

	while (new_clusters_size + align + new_fs_info->cluster_sectors
			<= new_data_size)
		align += new_fs_info->cluster_sectors;

	return align;
}

int _GL_ATTRIBUTE_PURE
fat_is_sector_in_clusters (const PedFileSystem* fs, PedSector sector)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	return sector >= fs_info->cluster_offset
	       && sector < fs_info->cluster_offset
	      		   + fs_info->cluster_sectors * fs_info->cluster_count;
}

FatFragment _GL_ATTRIBUTE_PURE
fat_cluster_to_frag (const PedFileSystem* fs, FatCluster cluster)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (cluster >= 2 && cluster < fs_info->cluster_count + 2);

	return (cluster - 2) * fs_info->cluster_frags;
}

FatCluster _GL_ATTRIBUTE_PURE
fat_frag_to_cluster (const PedFileSystem* fs, FatFragment frag)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (frag >= 0 && frag < fs_info->frag_count);

	return frag / fs_info->cluster_frags + 2;
}

PedSector _GL_ATTRIBUTE_PURE
fat_frag_to_sector (const PedFileSystem* fs, FatFragment frag)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (frag >= 0 && frag < fs_info->frag_count);

	return frag * fs_info->frag_sectors + fs_info->cluster_offset;
}

FatFragment _GL_ATTRIBUTE_PURE
fat_sector_to_frag (const PedFileSystem* fs, PedSector sector)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (sector >= fs_info->cluster_offset);

	return (sector - fs_info->cluster_offset) / fs_info->frag_sectors;
}

PedSector _GL_ATTRIBUTE_PURE
fat_cluster_to_sector (const PedFileSystem* fs, FatCluster cluster)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (cluster >= 2 && cluster < fs_info->cluster_count + 2);

	return (cluster - 2) * fs_info->cluster_sectors
		+ fs_info->cluster_offset;
}

FatCluster _GL_ATTRIBUTE_PURE
fat_sector_to_cluster (const PedFileSystem* fs, PedSector sector)
{
	FatSpecific*	fs_info = FAT_SPECIFIC (fs);

	PED_ASSERT (sector >= fs_info->cluster_offset);

	return (sector - fs_info->cluster_offset) / fs_info->cluster_sectors
		+ 2;
}
#endif /* !DISCOVER_ONLY */