55,9 → 55,6 |
#include <sys/mman.h> |
#include <align.h> |
|
#define FAT_NODE(node) ((node) ? (fat_node_t *) (node)->data : NULL) |
#define FS_NODE(node) ((node) ? (node)->bp : NULL) |
|
/** Futex protecting the list of cached free FAT nodes. */ |
static futex_t ffn_futex = FUTEX_INITIALIZER; |
|
67,7 → 64,6 |
static void fat_node_initialize(fat_node_t *node) |
{ |
futex_initialize(&node->lock, 1); |
node->bp = NULL; |
node->idx = NULL; |
node->type = 0; |
link_initialize(&node->ffn_link); |
112,7 → 108,6 |
|
static fat_node_t *fat_node_get_new(void) |
{ |
fs_node_t *fn; |
fat_node_t *nodep; |
|
futex_down(&ffn_futex); |
134,23 → 129,15 |
idxp_tmp->nodep = NULL; |
futex_up(&nodep->lock); |
futex_up(&idxp_tmp->lock); |
fn = FS_NODE(nodep); |
} else { |
skip_cache: |
/* Try to allocate a new node structure. */ |
futex_up(&ffn_futex); |
fn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
if (!fn) |
return NULL; |
nodep = (fat_node_t *)malloc(sizeof(fat_node_t)); |
if (!nodep) { |
free(fn); |
if (!nodep) |
return NULL; |
} |
} |
fat_node_initialize(nodep); |
fn->data = nodep; |
nodep->bp = fn; |
|
return nodep; |
} |
159,7 → 146,7 |
* |
* @param idxp Locked index structure. |
*/ |
static fat_node_t *fat_node_get_core(fat_idx_t *idxp) |
static void *fat_node_get_core(fat_idx_t *idxp) |
{ |
block_t *b; |
fat_bs_t *bs; |
236,21 → 223,21 |
/* |
* Forward declarations of FAT libfs operations. |
*/ |
static fs_node_t *fat_node_get(dev_handle_t, fs_index_t); |
static void fat_node_put(fs_node_t *); |
static fs_node_t *fat_create_node(dev_handle_t, int); |
static int fat_destroy_node(fs_node_t *); |
static int fat_link(fs_node_t *, fs_node_t *, const char *); |
static int fat_unlink(fs_node_t *, fs_node_t *, const char *); |
static fs_node_t *fat_match(fs_node_t *, const char *); |
static fs_index_t fat_index_get(fs_node_t *); |
static size_t fat_size_get(fs_node_t *); |
static unsigned fat_lnkcnt_get(fs_node_t *); |
static bool fat_has_children(fs_node_t *); |
static fs_node_t *fat_root_get(dev_handle_t); |
static void *fat_node_get(dev_handle_t, fs_index_t); |
static void fat_node_put(void *); |
static void *fat_create_node(dev_handle_t, int); |
static int fat_destroy_node(void *); |
static int fat_link(void *, void *, const char *); |
static int fat_unlink(void *, void *); |
static void *fat_match(void *, const char *); |
static fs_index_t fat_index_get(void *); |
static size_t fat_size_get(void *); |
static unsigned fat_lnkcnt_get(void *); |
static bool fat_has_children(void *); |
static void *fat_root_get(dev_handle_t); |
static char fat_plb_get_char(unsigned); |
static bool fat_is_directory(fs_node_t *); |
static bool fat_is_file(fs_node_t *node); |
static bool fat_is_directory(void *); |
static bool fat_is_file(void *node); |
|
/* |
* FAT libfs operations. |
257,9 → 244,9 |
*/ |
|
/** Instantiate a FAT in-core node. */ |
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
{ |
fat_node_t *nodep; |
void *node; |
fat_idx_t *idxp; |
|
idxp = fat_idx_get_by_index(dev_handle, index); |
266,14 → 253,14 |
if (!idxp) |
return NULL; |
/* idxp->lock held */ |
nodep = fat_node_get_core(idxp); |
node = fat_node_get_core(idxp); |
futex_up(&idxp->lock); |
return FS_NODE(nodep); |
return node; |
} |
|
void fat_node_put(fs_node_t *fn) |
void fat_node_put(void *node) |
{ |
fat_node_t *nodep = FAT_NODE(fn); |
fat_node_t *nodep = (fat_node_t *)node; |
bool destroy = false; |
|
futex_down(&nodep->lock); |
293,13 → 280,11 |
} |
} |
futex_up(&nodep->lock); |
if (destroy) { |
free(nodep->bp); |
free(nodep); |
} |
if (destroy) |
free(node); |
} |
|
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags) |
void *fat_create_node(dev_handle_t dev_handle, int flags) |
{ |
fat_idx_t *idxp; |
fat_node_t *nodep; |
325,7 → 310,7 |
idxp = fat_idx_get_new(dev_handle); |
if (!idxp) { |
fat_free_clusters(bs, dev_handle, mcl); |
fat_node_put(FS_NODE(nodep)); |
fat_node_put(nodep); |
return NULL; |
} |
/* idxp->lock held */ |
360,12 → 345,12 |
idxp->nodep = nodep; |
|
futex_up(&idxp->lock); |
return FS_NODE(nodep); |
return nodep; |
} |
|
int fat_destroy_node(fs_node_t *fn) |
int fat_destroy_node(void *node) |
{ |
fat_node_t *nodep = FAT_NODE(fn); |
fat_node_t *nodep = (fat_node_t *)node; |
fat_bs_t *bs; |
|
/* |
379,7 → 364,7 |
/* |
* The node may not have any children. |
*/ |
assert(fat_has_children(fn) == false); |
assert(fat_has_children(node) == false); |
|
bs = block_bb_get(nodep->idx->dev_handle); |
if (nodep->firstc != FAT_CLST_RES0) { |
389,15 → 374,14 |
} |
|
fat_idx_destroy(nodep->idx); |
free(nodep->bp); |
free(nodep); |
return EOK; |
} |
|
int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name) |
int fat_link(void *prnt, void *chld, const char *name) |
{ |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *childp = FAT_NODE(cfn); |
fat_node_t *parentp = (fat_node_t *)prnt; |
fat_node_t *childp = (fat_node_t *)chld; |
fat_dentry_t *d; |
fat_bs_t *bs; |
block_t *b; |
543,21 → 527,15 |
return EOK; |
} |
|
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
int fat_unlink(void *prnt, void *chld) |
{ |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *childp = FAT_NODE(cfn); |
fat_node_t *parentp = (fat_node_t *)prnt; |
fat_node_t *childp = (fat_node_t *)chld; |
fat_bs_t *bs; |
fat_dentry_t *d; |
uint16_t bps; |
block_t *b; |
|
if (!parentp) |
return EBUSY; |
|
if (fat_has_children(cfn)) |
return ENOTEMPTY; |
|
futex_down(&parentp->lock); |
futex_down(&childp->lock); |
assert(childp->lnkcnt == 1); |
589,10 → 567,10 |
return EOK; |
} |
|
fs_node_t *fat_match(fs_node_t *pfn, const char *component) |
void *fat_match(void *prnt, const char *component) |
{ |
fat_bs_t *bs; |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *parentp = (fat_node_t *)prnt; |
char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
unsigned i, j; |
unsigned bps; /* bytes per sector */ |
625,7 → 603,7 |
} |
if (fat_dentry_namecmp(name, component) == 0) { |
/* hit */ |
fat_node_t *nodep; |
void *node; |
/* |
* Assume tree hierarchy for locking. We |
* already have the parent and now we are going |
644,10 → 622,10 |
block_put(b); |
return NULL; |
} |
nodep = fat_node_get_core(idx); |
node = fat_node_get_core(idx); |
futex_up(&idx->lock); |
block_put(b); |
return FS_NODE(nodep); |
return node; |
} |
} |
block_put(b); |
657,25 → 635,28 |
return NULL; |
} |
|
fs_index_t fat_index_get(fs_node_t *fn) |
fs_index_t fat_index_get(void *node) |
{ |
return FAT_NODE(fn)->idx->index; |
fat_node_t *fnodep = (fat_node_t *)node; |
if (!fnodep) |
return 0; |
return fnodep->idx->index; |
} |
|
size_t fat_size_get(fs_node_t *fn) |
size_t fat_size_get(void *node) |
{ |
return FAT_NODE(fn)->size; |
return ((fat_node_t *)node)->size; |
} |
|
unsigned fat_lnkcnt_get(fs_node_t *fn) |
unsigned fat_lnkcnt_get(void *node) |
{ |
return FAT_NODE(fn)->lnkcnt; |
return ((fat_node_t *)node)->lnkcnt; |
} |
|
bool fat_has_children(fs_node_t *fn) |
bool fat_has_children(void *node) |
{ |
fat_bs_t *bs; |
fat_node_t *nodep = FAT_NODE(fn); |
fat_node_t *nodep = (fat_node_t *)node; |
unsigned bps; |
unsigned dps; |
unsigned blocks; |
723,7 → 704,7 |
return false; |
} |
|
fs_node_t *fat_root_get(dev_handle_t dev_handle) |
void *fat_root_get(dev_handle_t dev_handle) |
{ |
return fat_node_get(dev_handle, 0); |
} |
733,14 → 714,14 |
return fat_reg.plb_ro[pos % PLB_SIZE]; |
} |
|
bool fat_is_directory(fs_node_t *fn) |
bool fat_is_directory(void *node) |
{ |
return FAT_NODE(fn)->type == FAT_DIRECTORY; |
return ((fat_node_t *)node)->type == FAT_DIRECTORY; |
} |
|
bool fat_is_file(fs_node_t *fn) |
bool fat_is_file(void *node) |
{ |
return FAT_NODE(fn)->type == FAT_FILE; |
return ((fat_node_t *)node)->type == FAT_FILE; |
} |
|
/** libfs operations */ |
840,16 → 821,8 |
} |
|
/* Initialize the root node. */ |
fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t)); |
if (!rfn) { |
block_fini(dev_handle); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t)); |
if (!rootp) { |
free(rfn); |
block_fini(dev_handle); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
859,9 → 832,8 |
|
fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
if (!ridxp) { |
free(rfn); |
block_fini(dev_handle); |
free(rootp); |
block_fini(dev_handle); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
return; |
876,8 → 848,6 |
rootp->size = rde * sizeof(fat_dentry_t); |
rootp->idx = ridxp; |
ridxp->nodep = rootp; |
rootp->bp = rfn; |
rfn->data = rootp; |
|
futex_up(&ridxp->lock); |
|
899,23 → 869,21 |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
fs_node_t *fn = fat_node_get(dev_handle, index); |
fat_node_t *nodep; |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fat_bs_t *bs; |
uint16_t bps; |
size_t bytes; |
block_t *b; |
|
if (!fn) { |
if (!nodep) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
|
ipc_callid_t callid; |
size_t len; |
if (!ipc_data_read_receive(&callid, &len)) { |
fat_node_put(fn); |
fat_node_put(nodep); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
986,7 → 954,7 |
bnum++; |
} |
miss: |
fat_node_put(fn); |
fat_node_put(nodep); |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
995,7 → 963,7 |
bytes = (pos - spos) + 1; |
} |
|
fat_node_put(fn); |
fat_node_put(nodep); |
ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
} |
|
1004,8 → 972,7 |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
off_t pos = (off_t)IPC_GET_ARG3(*request); |
fs_node_t *fn = fat_node_get(dev_handle, index); |
fat_node_t *nodep; |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fat_bs_t *bs; |
size_t bytes; |
block_t *b; |
1015,16 → 982,15 |
off_t boundary; |
int flags = BLOCK_FLAGS_NONE; |
|
if (!fn) { |
if (!nodep) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
|
ipc_callid_t callid; |
size_t len; |
if (!ipc_data_write_receive(&callid, &len)) { |
fat_node_put(fn); |
fat_node_put(nodep); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
1065,7 → 1031,7 |
nodep->dirty = true; /* need to sync node */ |
} |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(fn); |
fat_node_put(nodep); |
return; |
} else { |
/* |
1081,7 → 1047,7 |
status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl); |
if (status != EOK) { |
/* could not allocate a chain of nclsts clusters */ |
fat_node_put(fn); |
fat_node_put(nodep); |
ipc_answer_0(callid, status); |
ipc_answer_0(rid, status); |
return; |
1102,7 → 1068,7 |
nodep->size = pos + bytes; |
nodep->dirty = true; /* need to sync node */ |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(fn); |
fat_node_put(nodep); |
return; |
} |
} |
1112,8 → 1078,7 |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
size_t size = (off_t)IPC_GET_ARG3(*request); |
fs_node_t *fn = fat_node_get(dev_handle, index); |
fat_node_t *nodep; |
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index); |
fat_bs_t *bs; |
uint16_t bps; |
uint8_t spc; |
1120,11 → 1085,10 |
unsigned bpc; /* bytes per cluster */ |
int rc; |
|
if (!fn) { |
if (!nodep) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
|
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
1162,7 → 1126,7 |
nodep->dirty = true; /* need to sync node */ |
rc = EOK; |
} |
fat_node_put(fn); |
fat_node_put(nodep); |
ipc_answer_0(rid, rc); |
return; |
} |
1173,13 → 1137,13 |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
int rc; |
|
fs_node_t *fn = fat_node_get(dev_handle, index); |
if (!fn) { |
fat_node_t *nodep = fat_node_get(dev_handle, index); |
if (!nodep) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
|
rc = fat_destroy_node(fn); |
rc = fat_destroy_node(nodep); |
ipc_answer_0(rid, rc); |
} |
|