/** Read block from file located on a exFAT file system. * * @param block Pointer to a block pointer for storing result. * @param bs Buffer holding the boot sector of the file system. * @param service_id Service ID of the file system. * @param fcl First cluster used by the file. Can be zero if the file * is empty. * @param clp If not NULL, address where the cluster containing bn * will be stored. * stored * @param bn Block number. * @param flags Flags passed to libblock. * * @return EOK on success or a negative error code. */ int exfat_block_get_by_clst(block_t **block, exfat_bs_t *bs, service_id_t service_id, bool fragmented, exfat_cluster_t fcl, exfat_cluster_t *clp, aoff64_t bn, int flags) { uint32_t clusters; uint32_t max_clusters; exfat_cluster_t c; int rc; if (fcl < EXFAT_CLST_FIRST || fcl > DATA_CNT(bs) + 2) return ELIMIT; if (!fragmented) { rc = block_get(block, service_id, DATA_FS(bs) + (fcl - EXFAT_CLST_FIRST)*SPC(bs) + bn, flags); } else { max_clusters = bn / SPC(bs); rc = exfat_cluster_walk(bs, service_id, fcl, &c, &clusters, max_clusters); if (rc != EOK) return rc; assert(clusters == max_clusters); rc = block_get(block, service_id, DATA_FS(bs) + (c - EXFAT_CLST_FIRST) * SPC(bs) + (bn % SPC(bs)), flags); if (clp) *clp = c; } return rc; }
/** 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_alloc_clusters(exfat_bs_t *bs, service_id_t service_id, exfat_cluster_t *firstc, exfat_cluster_t count) { exfat_cluster_t startc, endc; startc = EXFAT_CLST_FIRST; while (startc < DATA_CNT(bs) + 2) { endc = startc; while (exfat_bitmap_is_free(bs, service_id, endc) == EOK) { if ((endc - startc) + 1 == count) { *firstc = startc; return exfat_bitmap_set_clusters(bs, service_id, startc, count); } else endc++; } startc = endc+1; } return ENOSPC; }