/branches/dynload/uspace/srv/fs/fat/fat_idx.c |
---|
338,6 → 338,52 |
futex_up(&unused_futex); |
} |
static fat_idx_t *fat_idx_get_new_core(dev_handle_t dev_handle) |
{ |
fat_idx_t *fidx; |
fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
if (!fidx) |
return NULL; |
if (!fat_idx_alloc(dev_handle, &fidx->index)) { |
free(fidx); |
return NULL; |
} |
link_initialize(&fidx->uph_link); |
link_initialize(&fidx->uih_link); |
futex_initialize(&fidx->lock, 1); |
fidx->dev_handle = dev_handle; |
fidx->pfc = FAT_CLST_RES0; /* no parent yet */ |
fidx->pdi = 0; |
fidx->nodep = NULL; |
return fidx; |
} |
fat_idx_t *fat_idx_get_new(dev_handle_t dev_handle) |
{ |
fat_idx_t *fidx; |
futex_down(&used_futex); |
fidx = fat_idx_get_new_core(dev_handle); |
if (!fidx) { |
futex_up(&used_futex); |
return NULL; |
} |
unsigned long ikey[] = { |
[UIH_DH_KEY] = dev_handle, |
[UIH_INDEX_KEY] = fidx->index, |
}; |
hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
futex_down(&fidx->lock); |
futex_up(&used_futex); |
return fidx; |
} |
fat_idx_t * |
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
{ |
354,16 → 400,11 |
if (l) { |
fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
} else { |
fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
fidx = fat_idx_get_new_core(dev_handle); |
if (!fidx) { |
futex_up(&used_futex); |
return NULL; |
} |
if (!fat_idx_alloc(dev_handle, &fidx->index)) { |
free(fidx); |
futex_up(&used_futex); |
return NULL; |
} |
unsigned long ikey[] = { |
[UIH_DH_KEY] = dev_handle, |
370,13 → 411,8 |
[UIH_INDEX_KEY] = fidx->index, |
}; |
link_initialize(&fidx->uph_link); |
link_initialize(&fidx->uih_link); |
futex_initialize(&fidx->lock, 1); |
fidx->dev_handle = dev_handle; |
fidx->pfc = pfc; |
fidx->pdi = pdi; |
fidx->nodep = NULL; |
hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
/branches/dynload/uspace/srv/fs/fat/fat_fat.c |
---|
59,10 → 59,7 |
* @param bs Buffer holding the boot sector for the file. |
* @param dev_handle Device handle of the device with the file. |
* @param firstc First cluster to start the walk with. |
* @param penult If non-NULL, output argument hodling the |
* the penultimate cluster visited. |
* @param ult If non-NULL, output argument holding the |
* ultimate cluster visited. |
* @param lastc If non-NULL, output argument hodling the last cluster number visited. |
* @param max_clusters Maximum number of clusters to visit. |
* |
* @return Number of clusters seen during the walk. |
69,7 → 66,7 |
*/ |
uint16_t |
fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
fat_cluster_t *penult, fat_cluster_t *ult, uint16_t max_clusters) |
fat_cluster_t *lastc, uint16_t max_clusters) |
{ |
block_t *b; |
unsigned bps; |
82,22 → 79,18 |
if (firstc == FAT_CLST_RES0) { |
/* No space allocated to the file. */ |
if (ult) |
*ult = firstc; |
if (lastc) |
*lastc = firstc; |
return 0; |
} |
/* At this point, the meaning of penult is not well-defined. */ |
if (penult) |
*penult = FAT_CLST_RES0; |
while (clst < FAT_CLST_LAST1 && clusters < max_clusters) { |
unsigned fsec; /* sector offset relative to FAT1 */ |
bn_t fsec; /* sector offset relative to FAT1 */ |
unsigned fidx; /* FAT1 entry index */ |
assert(clst >= FAT_CLST_FIRST); |
if (penult) |
*penult = clst; /* remember the penultimate cluster */ |
if (lastc) |
*lastc = clst; /* remember the last cluster number */ |
fsec = (clst * sizeof(fat_cluster_t)) / bps; |
fidx = clst % (bps / sizeof(fat_cluster_t)); |
/* read FAT1 */ |
108,8 → 101,8 |
clusters++; |
} |
if (ult) |
*ult = clst; |
if (lastc && clst < FAT_CLST_LAST1) |
*lastc = clst; |
return clusters; |
} |
120,13 → 113,13 |
* @param dev_handle Device handle of the file system. |
* @param firstc First cluster used by the file. Can be zero if the file |
* is empty. |
* @param offset Offset in blocks. |
* @param bn Block number. |
* |
* @return Block structure holding the requested block. |
*/ |
block_t * |
_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
off_t offset) |
bn_t bn) |
{ |
block_t *b; |
unsigned bps; |
136,7 → 129,7 |
unsigned sf; |
unsigned ssa; /* size of the system area */ |
unsigned clusters, max_clusters; |
fat_cluster_t lastc, clst = firstc; |
fat_cluster_t lastc; |
bps = uint16_t_le2host(bs->bps); |
rscnt = uint16_t_le2host(bs->rscnt); |
149,23 → 142,22 |
if (firstc == FAT_CLST_ROOT) { |
/* root directory special case */ |
assert(offset < rds); |
b = block_get(dev_handle, rscnt + bs->fatcnt * sf + offset); |
assert(bn < rds); |
b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn); |
return b; |
} |
max_clusters = offset / bs->spc; |
clusters = fat_cluster_walk(bs, dev_handle, firstc, NULL, &lastc, |
max_clusters = bn / bs->spc; |
clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc, |
max_clusters); |
assert(clusters == max_clusters); |
b = block_get(dev_handle, ssa + (lastc - FAT_CLST_FIRST) * bs->spc + |
offset % bs->spc); |
bn % bs->spc); |
return b; |
} |
/** Fill the gap between EOF and a new file position. |
* |
* @param bs Buffer holding the boot sector for nodep. |
210,16 → 202,43 |
} |
} |
/** Mark cluster in one instance of FAT. |
/** Get cluster from the first FAT. |
* |
* @param bs Buffer holding the boot sector for the file system. |
* @param dev_handle Device handle for the file system. |
* @param clst Cluster which to get. |
* |
* @return Value found in the cluster. |
*/ |
fat_cluster_t |
fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst) |
{ |
block_t *b; |
uint16_t bps; |
uint16_t rscnt; |
fat_cluster_t *cp, value; |
bps = uint16_t_le2host(bs->bps); |
rscnt = uint16_t_le2host(bs->rscnt); |
b = block_get(dev_handle, rscnt + (clst * sizeof(fat_cluster_t)) / bps); |
cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
value = uint16_t_le2host(*cp); |
block_put(b); |
return value; |
} |
/** Set cluster in one instance of FAT. |
* |
* @param bs Buffer holding the boot sector for the file system. |
* @param dev_handle Device handle for the file system. |
* @param fatno Number of the FAT instance where to make the change. |
* @param clst Cluster which is to be marked. |
* @param value Value mark the cluster with. |
* @param clst Cluster which is to be set. |
* @param value Value to set the cluster with. |
*/ |
void |
fat_mark_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, |
fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno, |
fat_cluster_t clst, fat_cluster_t value) |
{ |
block_t *b; |
256,7 → 275,7 |
for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { |
for (c = 0; c < nclsts; c++) { |
fat_mark_cluster(bs, dev_handle, fatno, lifo[c], |
fat_set_cluster(bs, dev_handle, fatno, lifo[c], |
c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
} |
} |
340,7 → 359,7 |
* we have allocated so far. |
*/ |
while (found--) { |
fat_mark_cluster(bs, dev_handle, FAT1, lifo[found], |
fat_set_cluster(bs, dev_handle, FAT1, lifo[found], |
FAT_CLST_RES0); |
} |
348,9 → 367,32 |
return ENOSPC; |
} |
/** Free clusters forming a cluster chain in all copies of FAT. |
* |
* @param bs Buffer hodling the boot sector of the file system. |
* @param dev_handle Device handle of the file system. |
* @param firstc First cluster in the chain which is to be freed. |
*/ |
void |
fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc) |
{ |
unsigned fatno; |
fat_cluster_t nextc; |
/* Mark all clusters in the chain as free in all copies of FAT. */ |
while (firstc < FAT_CLST_LAST1) { |
nextc = fat_get_cluster(bs, dev_handle, firstc); |
assert(nextc >= FAT_CLST_FIRST && nextc < FAT_CLST_BAD); |
for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
fat_set_cluster(bs, dev_handle, fatno, firstc, |
FAT_CLST_RES0); |
firstc = nextc; |
} |
} |
/** Append a cluster chain to the last file cluster in all FATs. |
* |
* @param bs Buffer holding boot sector of the file system. |
* @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. |
*/ |
360,8 → 402,8 |
fat_cluster_t lcl; |
uint8_t fatno; |
if (fat_cluster_walk(bs, nodep->idx->dev_handle, nodep->firstc, &lcl, |
NULL, (uint16_t) -1) == 0) { |
if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, |
(uint16_t) -1) == 0) { |
/* No clusters allocated to the node yet. */ |
nodep->firstc = host2uint16_t_le(mcl); |
nodep->dirty = true; /* need to sync node */ |
369,9 → 411,40 |
} |
for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
fat_mark_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); |
fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl); |
} |
/** Chop off node clusters in all copies of FAT. |
* |
* @param bs Buffer holding the boot sector of the file system. |
* @param nodep FAT node where the chopping will take place. |
* @param lastc Last cluster which will remain in the node. If this |
* argument is FAT_CLST_RES0, then all clusters will |
* be chopped off. |
*/ |
void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc) |
{ |
dev_handle_t dev_handle = nodep->idx->dev_handle; |
if (lastc == FAT_CLST_RES0) { |
/* The node will have zero size and no clusters allocated. */ |
fat_free_clusters(bs, dev_handle, nodep->firstc); |
nodep->firstc = FAT_CLST_RES0; |
nodep->dirty = true; /* need to sync node */ |
} else { |
fat_cluster_t nextc; |
unsigned fatno; |
nextc = fat_get_cluster(bs, dev_handle, lastc); |
/* Terminate the cluster chain in all copies of FAT. */ |
for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1); |
/* Free all following clusters. */ |
fat_free_clusters(bs, dev_handle, nextc); |
} |
} |
/** |
* @} |
*/ |
/branches/dynload/uspace/srv/fs/fat/fat_fat.h |
---|
35,6 → 35,7 |
#include "../../vfs/vfs.h" |
#include <stdint.h> |
#include <libblock.h> |
#define FAT1 0 |
58,22 → 59,27 |
typedef uint16_t fat_cluster_t; |
#define fat_clusters_get(bs, dh, fc) \ |
fat_cluster_walk((bs), (dh), (fc), NULL, NULL, (uint16_t) -1) |
#define fat_block_get(bs, np, off) \ |
_fat_block_get((bs), (np)->idx->dev_handle, (np)->firstc, (off)) |
fat_cluster_walk((bs), (dh), (fc), NULL, (uint16_t) -1) |
extern uint16_t fat_cluster_walk(struct fat_bs *, dev_handle_t, fat_cluster_t, |
fat_cluster_t *, uint16_t); |
#define fat_block_get(bs, np, bn) \ |
_fat_block_get((bs), (np)->idx->dev_handle, (np)->firstc, (bn)) |
extern struct block *_fat_block_get(struct fat_bs *, dev_handle_t, |
fat_cluster_t, off_t); |
extern uint16_t fat_cluster_walk(struct fat_bs *, dev_handle_t, fat_cluster_t, |
fat_cluster_t *, fat_cluster_t *, uint16_t); |
fat_cluster_t, bn_t); |
extern void fat_append_clusters(struct fat_bs *, struct fat_node *, |
fat_cluster_t); |
extern void fat_chop_clusters(struct fat_bs *, struct fat_node *, |
fat_cluster_t); |
extern int fat_alloc_clusters(struct fat_bs *, dev_handle_t, unsigned, |
fat_cluster_t *, fat_cluster_t *); |
extern void fat_free_clusters(struct fat_bs *, dev_handle_t, fat_cluster_t); |
extern void fat_alloc_shadow_clusters(struct fat_bs *, dev_handle_t, |
fat_cluster_t *, unsigned); |
extern void fat_mark_cluster(struct fat_bs *, dev_handle_t, unsigned, |
extern fat_cluster_t fat_get_cluster(struct fat_bs *, dev_handle_t, fat_cluster_t); |
extern void fat_set_cluster(struct fat_bs *, dev_handle_t, unsigned, |
fat_cluster_t, fat_cluster_t); |
extern void fat_fill_gap(struct fat_bs *, struct fat_node *, fat_cluster_t, |
off_t); |
/branches/dynload/uspace/srv/fs/fat/fat_ops.c |
---|
102,6 → 102,42 |
block_put(b); |
} |
static fat_node_t *fat_node_get_new(void) |
{ |
fat_node_t *nodep; |
futex_down(&ffn_futex); |
if (!list_empty(&ffn_head)) { |
/* Try to use a cached free node structure. */ |
fat_idx_t *idxp_tmp; |
nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
goto skip_cache; |
idxp_tmp = nodep->idx; |
if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
futex_up(&nodep->lock); |
goto skip_cache; |
} |
list_remove(&nodep->ffn_link); |
futex_up(&ffn_futex); |
if (nodep->dirty) |
fat_node_sync(nodep); |
idxp_tmp->nodep = NULL; |
futex_up(&nodep->lock); |
futex_up(&idxp_tmp->lock); |
} else { |
skip_cache: |
/* Try to allocate a new node structure. */ |
futex_up(&ffn_futex); |
nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
if (!nodep) |
return NULL; |
} |
fat_node_initialize(nodep); |
return nodep; |
} |
/** Internal version of fat_node_get(). |
* |
* @param idxp Locked index structure. |
134,34 → 170,9 |
assert(idxp->pfc); |
futex_down(&ffn_futex); |
if (!list_empty(&ffn_head)) { |
/* Try to use a cached free node structure. */ |
fat_idx_t *idxp_tmp; |
nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link); |
if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK) |
goto skip_cache; |
idxp_tmp = nodep->idx; |
if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) { |
futex_up(&nodep->lock); |
goto skip_cache; |
} |
list_remove(&nodep->ffn_link); |
futex_up(&ffn_futex); |
if (nodep->dirty) |
fat_node_sync(nodep); |
idxp_tmp->nodep = NULL; |
futex_up(&nodep->lock); |
futex_up(&idxp_tmp->lock); |
} else { |
skip_cache: |
/* Try to allocate a new node structure. */ |
futex_up(&ffn_futex); |
nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
if (!nodep) |
return NULL; |
} |
fat_node_initialize(nodep); |
nodep = fat_node_get_new(); |
if (!nodep) |
return NULL; |
bs = block_bb_get(idxp->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
233,7 → 244,7 |
futex_up(&nodep->lock); |
} |
static void *fat_create(int flags) |
static void *fat_create(dev_handle_t dev_handle, int flags) |
{ |
return NULL; /* not supported at the moment */ |
} |
634,6 → 645,7 |
block_t *b; |
uint16_t bps; |
unsigned spc; |
unsigned bpc; /* bytes per cluster */ |
off_t boundary; |
if (!nodep) { |
650,6 → 662,11 |
return; |
} |
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
spc = bs->spc; |
bpc = bps * spc; |
/* |
* In all scenarios, we will attempt to write out only one block worth |
* of data at maximum. There might be some more efficient approaches, |
658,12 → 675,8 |
* value signalizing a smaller number of bytes written. |
*/ |
bytes = min(len, bps - pos % bps); |
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
spc = bs->spc; |
boundary = ROUND_UP(nodep->size, bps * spc); |
boundary = ROUND_UP(nodep->size, bpc); |
if (pos < boundary) { |
/* |
* This is the easier case - we are either overwriting already |
681,8 → 694,8 |
nodep->size = pos + bytes; |
nodep->dirty = true; /* need to sync node */ |
} |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(nodep); |
ipc_answer_1(rid, EOK, bytes); |
return; |
} else { |
/* |
693,11 → 706,9 |
unsigned nclsts; |
fat_cluster_t mcl, lcl; |
nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) / |
bps * spc; |
nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc; |
/* create an independent chain of nclsts clusters in all FATs */ |
status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, |
&lcl); |
status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
if (status != EOK) { |
/* could not allocate a chain of nclsts clusters */ |
fat_node_put(nodep); |
719,8 → 730,8 |
fat_append_clusters(bs, nodep, mcl); |
nodep->size = pos + bytes; |
nodep->dirty = true; /* need to sync node */ |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(nodep); |
ipc_answer_1(rid, EOK, bytes); |
return; |
} |
} |
731,6 → 742,10 |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
size_t size = (off_t)IPC_GET_ARG3(*request); |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fat_bs_t *bs; |
uint16_t bps; |
uint8_t spc; |
unsigned bpc; /* bytes per cluster */ |
int rc; |
if (!nodep) { |
738,24 → 753,45 |
return; |
} |
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
spc = bs->spc; |
bpc = bps * spc; |
if (nodep->size == size) { |
rc = EOK; |
} else if (nodep->size < size) { |
/* |
* TODO: the standard says we have the freedom to grow the file. |
* The standard says we have the freedom to grow the node. |
* For now, we simply return an error. |
*/ |
rc = EINVAL; |
} else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) { |
/* |
* The node will be shrunk, but no clusters will be deallocated. |
*/ |
nodep->size = size; |
nodep->dirty = true; /* need to sync node */ |
rc = EOK; |
} else { |
/* |
* The file is to be shrunk. |
* The node will be shrunk, clusters will be deallocated. |
*/ |
rc = ENOTSUP; /* XXX */ |
if (size == 0) { |
fat_chop_clusters(bs, nodep, FAT_CLST_RES0); |
} else { |
fat_cluster_t lastc; |
(void) fat_cluster_walk(bs, dev_handle, nodep->firstc, |
&lastc, (size - 1) / bpc); |
fat_chop_clusters(bs, nodep, lastc); |
} |
nodep->size = size; |
nodep->dirty = true; /* need to sync node */ |
rc = EOK; |
} |
fat_node_put(nodep); |
ipc_answer_0(rid, rc); |
return; |
} |
/** |