Index: uspace/app/mkexfat/mkexfat.c
===================================================================
--- uspace/app/mkexfat/mkexfat.c	(revision 9ce1acf417431ed94ffd4815c2302618aa6c5771)
+++ uspace/app/mkexfat/mkexfat.c	(revision d7f095832c766c4d9d12c87a6855f0e6be8a329a)
@@ -146,13 +146,12 @@
 	aoff64_t const volume_bytes = (cfg->volume_count - FAT_SECTOR_START) *
 	    cfg->sector_size;
-	aoff64_t n_req_clusters;
 
 	if (cfg->cluster_size != 0) {
 		/* The user already choose the cluster size he wants */
-		n_req_clusters = volume_bytes / cfg->cluster_size;
+		cfg->total_clusters = volume_bytes / cfg->cluster_size;
 		goto skip_cluster_size_set;
 	}
 
-	n_req_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
+	cfg->total_clusters = volume_bytes / DEFAULT_CLUSTER_SIZE;
 	cfg->cluster_size = DEFAULT_CLUSTER_SIZE;
 
@@ -161,18 +160,16 @@
 	 * size less or equal to 64 Mb.
 	 */
-	while (n_req_clusters > 16000000ULL &&
+	while (cfg->total_clusters > 16000000ULL &&
 	    (cfg->cluster_size < 32 * 1024 * 1024)) {
 
 		cfg->cluster_size <<= 1;
-		n_req_clusters = div_round_up(volume_bytes, cfg->cluster_size);
+		cfg->total_clusters = div_round_up(volume_bytes,
+		    cfg->cluster_size);
 	}
 
 skip_cluster_size_set:
 
-	/* The first two clusters are reserved */
-	cfg->total_clusters = n_req_clusters + 2;
-
 	/* Compute the FAT size in sectors */
-	fat_bytes = (cfg->total_clusters + 1) * 4;
+	fat_bytes = (cfg->total_clusters + 3) * sizeof(uint32_t);
 	cfg->fat_sector_count = div_round_up(fat_bytes, cfg->sector_size);
 
@@ -182,5 +179,5 @@
 
 	/* Compute the bitmap size */
-	cfg->bitmap_size = div_round_up(n_req_clusters, 8);
+	cfg->bitmap_size = div_round_up(cfg->total_clusters, 8);
 
 	/* Compute the number of clusters reserved to the bitmap */
@@ -194,4 +191,10 @@
 	cfg->allocated_clusters += div_round_up(sizeof(upcase_table),
 	    cfg->cluster_size);
+
+	/* Subtract the FAT and bootsector space from the total
+	 * number of available clusters.
+	 */
+	cfg->total_clusters -= div_round_up(cfg->data_start_sector *
+	    cfg->sector_size, cfg->cluster_size);
 
 	/* Will be set later */
@@ -219,4 +222,5 @@
 	printf(NAME ": Data start sector:     %lu\n", cfg->data_start_sector);
 	printf(NAME ": Total num of clusters: %lu\n", cfg->total_clusters);
+	printf(NAME ": Total used clusters:   %lu\n", cfg->allocated_clusters);
 	printf(NAME ": Bitmap size:           %lu\n", (unsigned long)
 	    div_round_up(cfg->bitmap_size, cfg->cluster_size));
@@ -251,7 +255,5 @@
 	mbs->data_start_sector = host2uint32_t_le(cfg->data_start_sector);
 
-	mbs->data_clusters = host2uint32_t_le(cfg->total_clusters - 
-	    div_round_up(cfg->data_start_sector * cfg->sector_size,
-	    cfg->cluster_size));
+	mbs->data_clusters = host2uint32_t_le(cfg->total_clusters);
 
 	mbs->rootdir_cluster = host2uint32_t_le(cfg->rootdir_cluster);
@@ -818,4 +820,9 @@
 	cfg_print_info(&cfg);
 
+	if ((cfg.total_clusters - cfg.allocated_clusters) <= 2) {
+		printf(NAME ": Error, insufficient disk space on device.\n");
+		return 2;
+	}
+
 	printf(NAME ": Writing the allocation table.\n");
 
