/** 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 service_id Service ID 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, service_id_t service_id, 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; 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++) { /* Need to rewrite because of multiple exfat_bitmap_get calls */ if (exfat_bitmap_is_free(bs, service_id, clst) == EOK) { /* * 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, service_id, clst, (found == 0) ? EXFAT_CLST_EOF : lifo[found - 1]); if (rc != EOK) goto exit_error; found++; rc = exfat_bitmap_set_cluster(bs, service_id, clst); if (rc != EOK) goto exit_error; } } if (rc == EOK && found == nclsts) { *mcl = lifo[found - 1]; *lcl = lifo[0]; free(lifo); fibril_mutex_unlock(&exfat_alloc_lock); return EOK; } rc = ENOSPC; exit_error: /* If something wrong - free the clusters */ while (found--) { (void) exfat_bitmap_clear_cluster(bs, service_id, lifo[found]); (void) exfat_set_cluster(bs, service_id, lifo[found], 0); } free(lifo); fibril_mutex_unlock(&exfat_alloc_lock); return rc; }
int exfat_bitmap_replicate_clusters(exfat_bs_t *bs, exfat_node_t *nodep) { int rc; exfat_cluster_t lastc, clst; service_id_t service_id = nodep->idx->service_id; lastc = nodep->firstc + ROUND_UP(nodep->size, BPC(bs)) / BPC(bs) - 1; for (clst = nodep->firstc; clst < lastc; clst++) { rc = exfat_set_cluster(bs, service_id, clst, clst + 1); if (rc != EOK) return rc; } return exfat_set_cluster(bs, service_id, lastc, EXFAT_CLST_EOF); }
/** 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) { service_id_t service_id = nodep->idx->service_id; exfat_cluster_t lastc = 0; 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, service_id, nodep->firstc, &lastc, NULL, (uint16_t) -1); if (rc != EOK) return rc; } rc = exfat_set_cluster(bs, nodep->idx->service_id, 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; service_id_t service_id = nodep->idx->service_id; /* * 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, service_id, 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, service_id, lcl, &nextc); if (rc != EOK) return rc; /* Terminate the cluster chain */ rc = exfat_set_cluster(bs, service_id, lcl, EXFAT_CLST_EOF); if (rc != EOK) return rc; /* Free all following clusters. */ rc = exfat_free_clusters(bs, service_id, 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; }
/** Free clusters forming a cluster chain in FAT. * * @param bs Buffer hodling the boot sector of the file system. * @param service_id Service ID 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, service_id_t service_id, 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, service_id, firstc, &nextc); if (rc != EOK) return rc; rc = exfat_set_cluster(bs, service_id, firstc, 0); if (rc != EOK) return rc; rc = exfat_bitmap_clear_cluster(bs, service_id, firstc); if (rc != EOK) return rc; firstc = nextc; } return EOK; }