Index: uspace/srv/fs/fat/fat_fat.c
===================================================================
--- uspace/srv/fs/fat/fat_fat.c	(revision c7bbf029477192dc16abf12a6c8de1c3181d7867)
+++ uspace/srv/fs/fat/fat_fat.c	(revision 3dbe4ca2665cf8a432e3a111f27932a595dceba1)
@@ -29,5 +29,5 @@
 /** @addtogroup fs
  * @{
- */ 
+ */
 
 /**
@@ -54,8 +54,4 @@
  * primitive boot sector members.
  */
-#define RDS(bs)		((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \
-			(((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)
-#define SSA(bs)		(RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))
-
 #define CLBN2PBN(bs, cl, bn) \
 	(SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
@@ -65,5 +61,5 @@
  * during allocation of clusters. The lock does not have to be held durring
  * deallocation of clusters.
- */  
+ */
 static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
 
@@ -77,15 +73,15 @@
  * @param numc		If non-NULL, output argument holding the number of
  *			clusters seen during the walk.
- * @param max_clusters	Maximum number of clusters to visit.	
+ * @param max_clusters	Maximum number of clusters to visit.
  *
  * @return		EOK on success or a negative error code.
  */
-int 
+int
 fat_cluster_walk(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc,
     fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
 {
-	block_t *b;
 	uint16_t clusters = 0;
-	fat_cluster_t clst = firstc;
+	fat_cluster_t clst = firstc, clst_last1 = FAT_CLST_LAST1(bs);
+	fat_cluster_t clst_bad = FAT_CLST_BAD(bs);
 	int rc;
 
@@ -99,27 +95,19 @@
 	}
 
-	while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
-		aoff64_t fsec;	/* sector offset relative to FAT1 */
-		unsigned fidx;	/* FAT1 entry index */
-
+	while (clst < clst_last1 && clusters < max_clusters) {
 		assert(clst >= FAT_CLST_FIRST);
 		if (lastc)
 			*lastc = clst;	/* remember the last cluster number */
-		fsec = (clst * sizeof(fat_cluster_t)) / BPS(bs);
-		fidx = clst % (BPS(bs) / sizeof(fat_cluster_t));
+
 		/* read FAT1 */
-		rc = block_get(&b, devmap_handle, RSCNT(bs) + fsec,
-		    BLOCK_FLAGS_NONE);
-		if (rc != EOK)
-			return rc;
-		clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
-		assert(clst != FAT_CLST_BAD);
-		rc = block_put(b);
-		if (rc != EOK)
-			return rc;
+		rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &clst);
+		if (rc != EOK)
+			return rc;
+
+		assert(clst != clst_bad);
 		clusters++;
 	}
 
-	if (lastc && clst < FAT_CLST_LAST1)
+	if (lastc && clst < clst_last1)
 		*lastc = clst;
 	if (numc)
@@ -151,5 +139,5 @@
 		return ELIMIT;
 
-	if (nodep->firstc == FAT_CLST_ROOT) 
+	if (nodep->firstc == FAT_CLST_ROOT)
 		goto fall_through;
 
@@ -178,5 +166,5 @@
 	if (rc != EOK)
 		return rc;
-	
+
 	/*
 	 * Update the "current" cluster cache.
@@ -198,5 +186,5 @@
  * @param clp		If not NULL, address where the cluster containing bn
  *			will be stored.
- *			stored 
+ *			stored
  * @param bn		Block number.
  * @param flags		Flags passed to libblock.
@@ -275,8 +263,8 @@
 			return rc;
 	}
-	
+
 	if (o >= pos)
 		return EOK;
-	
+
 	/* zero out the initial part of the new cluster chain */
 	for (o = boundary; o < pos; o += BPS(bs)) {
@@ -308,17 +296,63 @@
     fat_cluster_t clst, fat_cluster_t *value)
 {
-	block_t *b;
-	fat_cluster_t *cp;
-	int rc;
+	block_t *b, *b1;
+	aoff64_t offset;
+	int rc;
+
+	assert(fatno < FATCNT(bs));
+
+	if (FAT_IS_FAT12(bs))
+		offset = (clst + clst/2);
+	else
+		offset = (clst * sizeof(fat_cluster_t));
 
 	rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
-	    (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
+	    offset / BPS(bs), BLOCK_FLAGS_NONE);
 	if (rc != EOK)
 		return rc;
-	cp = (fat_cluster_t *)b->data +
-	    clst % (BPS(bs) / sizeof(fat_cluster_t));
-	*value = uint16_t_le2host(*cp);
+
+	/* This cluster access spans a sector boundary. Check only for FAT12 */
+	if (FAT_IS_FAT12(bs) && (offset % BPS(bs)+1 == BPS(bs))) {
+		/* Is it last sector of FAT? */
+		if (offset / BPS(bs) < SF(bs)) {
+			/* No. Reading next sector */
+			rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
+			    SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
+			if (rc != EOK) {
+				block_put(b);
+				return rc;
+			}
+			/*
+			 * Combining value with last byte of current sector and
+			 * first byte of next sector
+			 */
+			*value  = *(uint8_t *)(b->data + BPS(bs) - 1);
+			*value |= *(uint8_t *)(b1->data);
+
+			rc = block_put(b1);
+			if (rc != EOK) {
+				block_put(b);
+				return rc;
+			}
+		}
+		else {
+			/* Yes. It is last sector of FAT */
+			block_put(b);
+			return ERANGE;
+		}
+	}
+	else
+		*value = *(fat_cluster_t *)(b->data + offset % BPS(bs));
+
+	if (FAT_IS_FAT12(bs)) {
+		if (clst & 0x0001)
+		*value = (*value) >> 4;
+		else
+		*value = (*value) & 0x0fff;
+	}
+
+	*value = uint16_t_le2host(*value);
 	rc = block_put(b);
-	
+
 	return rc;
 }
@@ -338,17 +372,79 @@
     fat_cluster_t clst, fat_cluster_t value)
 {
-	block_t *b;
-	fat_cluster_t *cp;
-	int rc;
+	block_t *b, *b1;
+	aoff64_t offset;
+	fat_cluster_t *cp, temp;
+	int rc;
+	int spans = 0;
 
 	assert(fatno < FATCNT(bs));
+
+	if (FAT_IS_FAT12(bs))
+		offset = (clst + clst/2);
+	else
+		offset = (clst * sizeof(fat_cluster_t));
+
 	rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
-	    (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
+	    offset / BPS(bs), BLOCK_FLAGS_NONE);
 	if (rc != EOK)
 		return rc;
-	cp = (fat_cluster_t *)b->data +
-	    clst % (BPS(bs) / sizeof(fat_cluster_t));
-	*cp = host2uint16_t_le(value);
-	b->dirty = true;		/* need to sync block */
+
+	/* This cluster access spans a sector boundary. Check only for FAT12 */
+	if (FAT_IS_FAT12(bs) && (offset % BPS(bs)+1 == BPS(bs))) {
+		/* Is it last sector of FAT? */
+		if (offset / BPS(bs) < SF(bs)) {
+			/* No. Reading next sector */
+			rc = block_get(&b1, devmap_handle, 1 + RSCNT(bs) +
+			    SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
+			if (rc != EOK) {
+				block_put(b);
+				return rc;
+			}
+			/*
+			 * Combining value with last byte of current sector and
+			 * first byte of next sector
+			 */
+			spans=1;
+			cp = &temp;
+			*cp  = *(uint8_t *)(b->data + BPS(bs) - 1);
+			*cp |= *(uint8_t *)(b1->data);
+		}
+		else {
+			/* Yes. It is last sector of fat */
+			block_put(b);
+			return ERANGE;
+		}
+	}
+	else
+		cp = (fat_cluster_t *)(b->data + offset % BPS(bs));
+
+	value = host2uint16_t_le(value);
+	if (FAT_IS_FAT12(bs)) {
+		if (clst & 0x0001) {
+			*cp &= 0x000f;
+			*cp |= value << 4;
+		}
+		else {
+			*cp &= 0xf000;
+			*cp |= value & 0x0fff;
+		}
+
+		if (spans)
+		{
+			*(uint8_t *)(b->data + BPS(bs) - 1) = cp[0];
+			*(uint8_t *)(b1->data) = cp[1];
+
+			b1->dirty = true;
+			rc = block_put(b1);
+			if (rc != EOK) {
+				block_put(b);
+				return rc;
+			}
+		}
+	}
+	else
+		*cp = value;
+
+	b->dirty = true;	/* need to sync block */
 	rc = block_put(b);
 	return rc;
@@ -369,4 +465,5 @@
 	uint8_t fatno;
 	unsigned c;
+	fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
 	int rc;
 
@@ -374,5 +471,5 @@
 		for (c = 0; c < nclsts; c++) {
 			rc = fat_set_cluster(bs, devmap_handle, fatno, lifo[c],
-			    c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
+			    c == 0 ? clst_last1 : lifo[c - 1]);
 			if (rc != EOK)
 				return rc;
@@ -404,94 +501,57 @@
     fat_cluster_t *mcl, fat_cluster_t *lcl)
 {
-	block_t *blk;
-	fat_cluster_t *lifo;	/* stack for storing free cluster numbers */ 
-	unsigned found = 0;	/* top of the free cluster number stack */
-	unsigned b, c, cl; 
-	int rc;
+	fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
+	unsigned found = 0;     /* top of the free cluster number stack */
+	fat_cluster_t clst, value, clst_last1 = FAT_CLST_LAST1(bs);
+	int rc = EOK;
 
 	lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t));
 	if (!lifo)
 		return ENOMEM;
-	
 	/*
 	 * Search FAT1 for unused clusters.
 	 */
 	fibril_mutex_lock(&fat_alloc_lock);
-	for (b = 0, cl = 0; b < SF(bs); b++) {
-		rc = block_get(&blk, devmap_handle, RSCNT(bs) + b,
-		    BLOCK_FLAGS_NONE);
-		if (rc != EOK)
-			goto error;
-		for (c = 0; c < BPS(bs) / sizeof(fat_cluster_t); c++, cl++) {
-			/*
-			 * Check if the entire cluster is physically there.
-			 * This check becomes necessary when the file system is
-			 * created with fewer total sectors than how many is
-			 * inferred from the size of the file allocation table
-			 * or when the last cluster ends beyond the end of the
-			 * device.
-			 */
-			if ((cl >= FAT_CLST_FIRST) &&
-			    CLBN2PBN(bs, cl, SPC(bs) - 1) >= TS(bs)) {
-				rc = block_put(blk);
-				if (rc != EOK)
-					goto error;
-				goto out;
-			}
-
-			fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
-			if (uint16_t_le2host(*clst) == FAT_CLST_RES0) {
-				/*
-				 * The cluster is free. Put it into our stack
-				 * of found clusters and mark it as non-free.
-				 */
-				lifo[found] = cl;
-				*clst = (found == 0) ?
-				    host2uint16_t_le(FAT_CLST_LAST1) :
-				    host2uint16_t_le(lifo[found - 1]);
-				blk->dirty = true;	/* need to sync block */
-				if (++found == nclsts) {
-					/* we are almost done */
-					rc = block_put(blk);
-					if (rc != EOK)
-						goto error;
-					/* update the shadow copies of FAT */
-					rc = fat_alloc_shadow_clusters(bs,
-					    devmap_handle, lifo, nclsts);
-					if (rc != EOK)
-						goto error;
-					*mcl = lifo[found - 1];
-					*lcl = lifo[0];
-					free(lifo);
-					fibril_mutex_unlock(&fat_alloc_lock);
-					return EOK;
-				}
-			}
-		}
-		rc = block_put(blk);
-		if (rc != EOK) {
-error:
+	for (clst=FAT_CLST_FIRST; clst < CC(bs)+2 && found < nclsts; clst++) {
+		rc = fat_get_cluster(bs, devmap_handle, FAT1, clst, &value);
+		if (rc != EOK)
+		break;
+
+		if (value == FAT_CLST_RES0) {
+		/*
+		 * The cluster is free. Put it into our stack
+		 * of found clusters and mark it as non-free.
+		 */
+		lifo[found] = clst;
+		rc = fat_set_cluster(bs, devmap_handle, FAT1, clst,
+		    (found == 0) ?  clst_last1 : lifo[found - 1]);
+		if (rc != EOK)
+			break;
+
+		found++;
+		}
+	}
+
+	if (rc == EOK && found == nclsts) {
+		rc = fat_alloc_shadow_clusters(bs, devmap_handle, lifo, nclsts);
+		if (rc == EOK) {
+			*mcl = lifo[found - 1];
+			*lcl = lifo[0];
+			free(lifo);
 			fibril_mutex_unlock(&fat_alloc_lock);
-			free(lifo);
-			return rc;
-		}
-	}
-out:
-	fibril_mutex_unlock(&fat_alloc_lock);
-
-	/*
-	 * We could not find enough clusters. Now we need to free the clusters
-	 * we have allocated so far.
-	 */
-	while (found--) {
+			return EOK;
+		}
+	}
+
+	/* If something wrong - free the clusters */
+	if (found > 0) {
+		while (found--) {
 		rc = fat_set_cluster(bs, devmap_handle, FAT1, lifo[found],
 		    FAT_CLST_RES0);
-		if (rc != EOK) {
-			free(lifo);
-			return rc;
-		}
-	}
-	
+		}
+	}
+
 	free(lifo);
+	fibril_mutex_unlock(&fat_alloc_lock);
 	return ENOSPC;
 }
@@ -509,10 +569,10 @@
 {
 	unsigned fatno;
-	fat_cluster_t nextc;
+	fat_cluster_t nextc, clst_bad = FAT_CLST_BAD(bs);
 	int rc;
 
 	/* Mark all clusters in the chain as free in all copies of FAT. */
-	while (firstc < FAT_CLST_LAST1) {
-		assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
+	while (firstc < FAT_CLST_LAST1(bs)) {
+		assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad);
 		rc = fat_get_cluster(bs, devmap_handle, FAT1, firstc, &nextc);
 		if (rc != EOK)
@@ -565,6 +625,6 @@
 
 		for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
-			rc = fat_set_cluster(bs, nodep->idx->devmap_handle, fatno,
-			    lastc, mcl);
+			rc = fat_set_cluster(bs, nodep->idx->devmap_handle,
+			    fatno, lastc, mcl);
 			if (rc != EOK)
 				return rc;
@@ -590,4 +650,5 @@
 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
 {
+	fat_cluster_t clst_last1 = FAT_CLST_LAST1(bs);
 	int rc;
 	devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
@@ -618,5 +679,5 @@
 		for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
 			rc = fat_set_cluster(bs, devmap_handle, fatno, lcl,
-			    FAT_CLST_LAST1);
+			    clst_last1);
 			if (rc != EOK)
 				return rc;
@@ -677,10 +738,9 @@
 
 	/* Check total number of sectors. */
-
 	if (bs->totsec16 == 0 && bs->totsec32 == 0)
 		return ENOTSUP;
 
 	if (bs->totsec16 != 0 && bs->totsec32 != 0 &&
-	    bs->totsec16 != bs->totsec32) 
+	    bs->totsec16 != bs->totsec32)
 		return ENOTSUP;
 
@@ -705,5 +765,4 @@
 
 	/* Check signature of each FAT. */
-
 	for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
 		rc = fat_get_cluster(bs, devmap_handle, fat_no, 0, &e0);
@@ -723,5 +782,5 @@
 		 * set to one.
 		 */
-		if ((e0 >> 8) != 0xff || e1 != 0xffff)
+                if (!FAT_IS_FAT12(bs) && ((e0 >> 8) != 0xff || e1 != 0xffff))
 			return ENOTSUP;
 	}
@@ -732,3 +791,3 @@
 /**
  * @}
- */ 
+ */
