Index: uspace/srv/fs/exfat/exfat_fat.c
===================================================================
--- uspace/srv/fs/exfat/exfat_fat.c	(revision 7f0c08c5aa8d178e0a896447ae1d5182b5360932)
+++ uspace/srv/fs/exfat/exfat_fat.c	(revision f3d4cd356d288ae72ef46534d513305b306c5d5e)
@@ -56,5 +56,5 @@
  * deallocation of clusters.
  */
-static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
+static FIBRIL_MUTEX_INITIALIZE(exfat_alloc_lock);
 
 /** Walk the cluster chain.
@@ -72,5 +72,5 @@
  */
 int
-fat_cluster_walk(exfat_bs_t *bs, devmap_handle_t devmap_handle, 
+exfat_cluster_walk(exfat_bs_t *bs, devmap_handle_t devmap_handle, 
     exfat_cluster_t firstc, exfat_cluster_t *lastc, uint32_t *numc,
     uint32_t max_clusters)
@@ -89,10 +89,10 @@
 	}
 
-	while (clst <= EXFAT_CLST_LAST && clusters < max_clusters) {
+	while (clst != EXFAT_CLST_EOF && clusters < max_clusters) {
 		assert(clst >= EXFAT_CLST_FIRST);
 		if (lastc)
 			*lastc = clst;	/* remember the last cluster number */
 
-		rc = fat_get_cluster(bs, devmap_handle, clst, &clst);
+		rc = exfat_get_cluster(bs, devmap_handle, clst, &clst);
 		if (rc != EOK)
 			return rc;
@@ -102,5 +102,5 @@
 	}
 
-	if (lastc && clst <= EXFAT_CLST_LAST)
+	if (lastc && clst != EXFAT_CLST_EOF)
 		*lastc = clst;
 	if (numc)
@@ -202,5 +202,5 @@
 	} else {
 		max_clusters = bn / SPC(bs);
-		rc = fat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
+		rc = exfat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
 		if (rc != EOK)
 			return rc;
@@ -228,5 +228,5 @@
  */
 int
-fat_get_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
+exfat_get_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
     exfat_cluster_t clst, exfat_cluster_t *value)
 {
@@ -258,5 +258,5 @@
  */
 int
-fat_set_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
+exfat_set_cluster(exfat_bs_t *bs, devmap_handle_t devmap_handle,
     exfat_cluster_t clst, exfat_cluster_t value)
 {
@@ -278,4 +278,234 @@
 }
 
+/** Allocate clusters in FAT.
+ *
+ * This function will attempt to allocate the requested number of clusters in
+ * the FAT.  The FAT will be altered so that the allocated
+ * clusters form an independent chain (i.e. a chain which does not belong to any
+ * file yet).
+ *
+ * @param bs		Buffer holding the boot sector of the file system.
+ * @param devmap_handle	Device handle of the file system.
+ * @param nclsts	Number of clusters to allocate.
+ * @param mcl		Output parameter where the first cluster in the chain
+ *			will be returned.
+ * @param lcl		Output parameter where the last cluster in the chain
+ *			will be returned.
+ *
+ * @return		EOK on success, a negative error code otherwise.
+ */
+int
+exfat_alloc_clusters(exfat_bs_t *bs, devmap_handle_t devmap_handle, unsigned nclsts,
+    exfat_cluster_t *mcl, exfat_cluster_t *lcl)
+{
+	exfat_cluster_t *lifo;    /* stack for storing free cluster numbers */
+	unsigned found = 0;     /* top of the free cluster number stack */
+	exfat_cluster_t clst, value;
+	int rc = EOK;
+
+	lifo = (exfat_cluster_t *) malloc(nclsts * sizeof(exfat_cluster_t));
+	if (!lifo)
+		return ENOMEM;
+
+	fibril_mutex_lock(&exfat_alloc_lock);
+	for (clst=EXFAT_CLST_FIRST; clst < DATA_CNT(bs)+2 && found < nclsts; clst++) {
+		rc = exfat_get_cluster(bs, devmap_handle, clst, &value);
+		if (rc != EOK)
+			break;
+
+		if (value == 0) {
+		   /*
+			* The cluster is free. Put it into our stack
+			* of found clusters and mark it as non-free.
+			*/
+			lifo[found] = clst;
+			rc = exfat_set_cluster(bs, devmap_handle, clst,
+				(found == 0) ?  EXFAT_CLST_EOF : lifo[found - 1]);
+			if (rc != EOK)
+				break;
+
+			found++;
+		}
+	}
+
+	if (rc == EOK && found == nclsts) {
+		*mcl = lifo[found - 1];
+		*lcl = lifo[0];
+		free(lifo);
+		fibril_mutex_unlock(&exfat_alloc_lock);
+		return EOK;
+	}
+
+	/* If something wrong - free the clusters */
+	if (found > 0) {
+		while (found--)
+			rc = exfat_set_cluster(bs, devmap_handle, lifo[found], 0);
+	}
+
+	free(lifo);
+	fibril_mutex_unlock(&exfat_alloc_lock);
+	return ENOSPC;
+}
+
+/** Free clusters forming a cluster chain in FAT.
+ *
+ * @param bs		Buffer hodling the boot sector of the file system.
+ * @param devmap_handle	Device handle of the file system.
+ * @param firstc	First cluster in the chain which is to be freed.
+ *
+ * @return		EOK on success or a negative return code.
+ */
+int
+exfat_free_clusters(exfat_bs_t *bs, devmap_handle_t devmap_handle, exfat_cluster_t firstc)
+{
+	exfat_cluster_t nextc;
+	int rc;
+
+	/* Mark all clusters in the chain as free */
+	while (firstc != EXFAT_CLST_EOF) {
+		assert(firstc >= EXFAT_CLST_FIRST && firstc < EXFAT_CLST_BAD);
+		rc = exfat_get_cluster(bs, devmap_handle, firstc, &nextc);
+		if (rc != EOK)
+			return rc;
+		rc = exfat_set_cluster(bs, devmap_handle, firstc, 0);
+		if (rc != EOK)
+			return rc;
+		firstc = nextc;
+	}
+
+	return EOK;
+}
+
+/** Append a cluster chain to the last file cluster in FAT.
+ *
+ * @param bs		Buffer holding the boot sector of the file system.
+ * @param nodep		Node representing the file.
+ * @param mcl		First cluster of the cluster chain to append.
+ * @param lcl		Last cluster of the cluster chain to append.
+ *
+ * @return		EOK on success or a negative error code.
+ */
+int
+exfat_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t mcl,
+    exfat_cluster_t lcl)
+{
+	devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
+	exfat_cluster_t lastc;
+	int rc;
+
+	if (nodep->firstc == 0) {
+		/* No clusters allocated to the node yet. */
+		nodep->firstc = mcl;
+		nodep->dirty = true;	/* need to sync node */
+	} else {
+		if (nodep->lastc_cached_valid) {
+			lastc = nodep->lastc_cached_value;
+			nodep->lastc_cached_valid = false;
+		} else {
+			rc = exfat_cluster_walk(bs, devmap_handle, nodep->firstc,
+			    &lastc, NULL, (uint16_t) -1);
+			if (rc != EOK)
+				return rc;
+		}
+
+		rc = exfat_set_cluster(bs, nodep->idx->devmap_handle, lastc, mcl);
+		if (rc != EOK)
+			return rc;
+	}
+
+	nodep->lastc_cached_valid = true;
+	nodep->lastc_cached_value = lcl;
+
+	return EOK;
+}
+
+/** Chop off node clusters in FAT.
+ *
+ * @param bs		Buffer holding the boot sector of the file system.
+ * @param nodep		FAT node where the chopping will take place.
+ * @param lcl		Last cluster which will remain in the node. If this
+ *			argument is FAT_CLST_RES0, then all clusters will
+ *			be chopped off.
+ *
+ * @return		EOK on success or a negative return code.
+ */
+int exfat_chop_clusters(exfat_bs_t *bs, exfat_node_t *nodep, exfat_cluster_t lcl)
+{
+	int rc;
+	devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
+
+	/*
+	 * Invalidate cached cluster numbers.
+	 */
+	nodep->lastc_cached_valid = false;
+	if (nodep->currc_cached_value != lcl)
+		nodep->currc_cached_valid = false;
+
+	if (lcl == 0) {
+		/* The node will have zero size and no clusters allocated. */
+		rc = exfat_free_clusters(bs, devmap_handle, nodep->firstc);
+		if (rc != EOK)
+			return rc;
+		nodep->firstc = 0;
+		nodep->dirty = true;		/* need to sync node */
+	} else {
+		exfat_cluster_t nextc;
+
+		rc = exfat_get_cluster(bs, devmap_handle, lcl, &nextc);
+		if (rc != EOK)
+			return rc;
+
+		/* Terminate the cluster chain */
+		rc = exfat_set_cluster(bs, devmap_handle, lcl, EXFAT_CLST_EOF);
+		if (rc != EOK)
+			return rc;
+
+		/* Free all following clusters. */
+		rc = exfat_free_clusters(bs, devmap_handle, nextc);
+		if (rc != EOK)
+			return rc;
+	}
+
+	/*
+	 * Update and re-enable the last cluster cache.
+	 */
+	nodep->lastc_cached_valid = true;
+	nodep->lastc_cached_value = lcl;
+
+	return EOK;
+}
+
+int bitmap_alloc_clusters(exfat_bs_t *bs, devmap_handle_t devmap_handle, 
+    exfat_cluster_t *firstc, exfat_cluster_t count)
+{
+	/* TODO */
+	return EOK;
+}
+
+
+int bitmap_append_clusters(exfat_bs_t *bs, exfat_node_t *nodep, 
+    exfat_cluster_t count)
+{
+	/* TODO */
+	return EOK;
+}
+
+
+int bitmap_free_clusters(exfat_bs_t *bs, exfat_node_t *nodep, 
+    exfat_cluster_t count)
+{
+	/* TODO */
+	return EOK;
+}
+
+
+int bitmap_replicate_clusters(exfat_bs_t *bs, exfat_node_t *nodep)
+{
+	/* TODO */
+	return EOK;
+}
+
+
+
 /** Perform basic sanity checks on the file system.
  *
Index: uspace/srv/fs/exfat/exfat_fat.h
===================================================================
--- uspace/srv/fs/exfat/exfat_fat.h	(revision 7f0c08c5aa8d178e0a896447ae1d5182b5360932)
+++ uspace/srv/fs/exfat/exfat_fat.h	(revision f3d4cd356d288ae72ef46534d513305b306c5d5e)
@@ -59,7 +59,7 @@
 
 
-#define fat_clusters_get(numc, bs, dh, fc) \
-    fat_cluster_walk((bs), (dh), (fc), NULL, (numc), (uint32_t) -1)
-extern int fat_cluster_walk(struct exfat_bs *bs, devmap_handle_t devmap_handle, 
+#define exfat_clusters_get(numc, bs, dh, fc) \
+    exfat_cluster_walk((bs), (dh), (fc), NULL, (numc), (uint32_t) -1)
+extern int exfat_cluster_walk(struct exfat_bs *bs, devmap_handle_t devmap_handle, 
     exfat_cluster_t firstc, exfat_cluster_t *lastc, uint32_t *numc,
     uint32_t max_clusters);
@@ -70,10 +70,25 @@
     exfat_cluster_t *clp, aoff64_t bn, int flags);
 
-extern int fat_get_cluster(struct exfat_bs *bs, devmap_handle_t devmap_handle,
+extern int exfat_get_cluster(struct exfat_bs *bs, devmap_handle_t devmap_handle,
     exfat_cluster_t clst, exfat_cluster_t *value);
-extern int fat_set_cluster(struct exfat_bs *bs, devmap_handle_t devmap_handle,
+extern int exfat_set_cluster(struct exfat_bs *bs, devmap_handle_t devmap_handle,
     exfat_cluster_t clst, exfat_cluster_t value);
 extern int exfat_sanity_check(struct exfat_bs *, devmap_handle_t);
 
+extern int bitmap_alloc_clusters(struct exfat_bs *bs, devmap_handle_t devmap_handle, 
+    exfat_cluster_t *firstc, exfat_cluster_t count);
+extern int bitmap_append_clusters(struct exfat_bs *bs, struct exfat_node *nodep, 
+    exfat_cluster_t count);
+extern int bitmap_free_clusters(struct exfat_bs *bs, struct exfat_node *nodep, 
+    exfat_cluster_t count);
+extern int bitmap_replicate_clusters(struct exfat_bs *bs, struct exfat_node *nodep); 
+
+extern int exfat_append_clusters(struct exfat_bs *, struct exfat_node *,
+    exfat_cluster_t, exfat_cluster_t);
+extern int exfat_chop_clusters(struct exfat_bs *, struct exfat_node *,
+    exfat_cluster_t);
+extern int exfat_alloc_clusters(struct exfat_bs *, devmap_handle_t, unsigned,
+    exfat_cluster_t *, exfat_cluster_t *);
+extern int exfat_free_clusters(struct exfat_bs *, devmap_handle_t, exfat_cluster_t);
 
 
