fsck_msdos: set limitation to reduce the time in extreme case

In some particular SD card, Most of the clusters are discrete.
It will take a long time to manage the runtime memory and cluster.
Set a max limitation here to limit the whole time, otherwise it will
take too much time and a bad user experience.

Change-Id: I3212f52e19c9b2c888120ada9f9ce46d7ef37d58
diff --git a/fatcache.c b/fatcache.c
index 5b9a73b..b63a13a 100644
--- a/fatcache.c
+++ b/fatcache.c
@@ -357,6 +357,20 @@
 	/*re-calc Numfree*/
 	boot->NumFree += (org_chain_len - fat->length);
 }
+
+/*
+ * This is a trade-off between time and memory
+ * In order to reduce the runtime memory consumption
+ * We change the whole strategy of cluster chain checking
+ * and managment.
+ * In some extreme cases, most of the clusters in FAT file
+ * system are discrete. that means it will take much time
+ * to manage memory and cluster chain at runtime.
+ * So set a limitation of max memory malloc here.
+ */
+int limit = 0;
+#define FSCK_MSDOS_MAX_CALLOC_TIMES	0x15000
+
 struct cluster_chain_descriptor* New_fatentry(void)
 {
 		struct cluster_chain_descriptor *fat;
@@ -366,6 +380,9 @@
 			return fat;
 		}
 		RB_SET(fat, NULL, rb);
+		if (++limit >= FSCK_MSDOS_MAX_CALLOC_TIMES)
+			exit(0);
+
 		return fat;
 }
 
@@ -375,6 +392,9 @@
 		cache = calloc(1,sizeof(struct fatcache));
 		if(!cache)
 			fsck_warn("No space \n");
+		if (++limit >= FSCK_MSDOS_MAX_CALLOC_TIMES)
+			exit(0);
+
 		return cache;
 }