/branches/arm/uspace/srv/fs/devfs/devfs_ops.c |
---|
0,0 → 1,531 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file devfs_ops.c |
* @brief Implementation of VFS operations for the devfs file system server. |
*/ |
#include <ipc/ipc.h> |
#include <bool.h> |
#include <errno.h> |
#include <malloc.h> |
#include <string.h> |
#include <libfs.h> |
#include <fibril_sync.h> |
#include <adt/hash_table.h> |
#include <sys/stat.h> |
#include "devfs.h" |
#include "devfs_ops.h" |
#define PLB_GET_CHAR(pos) (devfs_reg.plb_ro[pos % PLB_SIZE]) |
/** Opened devices structure */ |
typedef struct { |
dev_handle_t handle; |
int phone; |
size_t refcount; |
link_t link; |
} device_t; |
/** Hash table of opened devices */ |
static hash_table_t devices; |
/** Hash table mutex */ |
static FIBRIL_MUTEX_INITIALIZE(devices_mutex); |
#define DEVICES_KEYS 1 |
#define DEVICES_KEY_HANDLE 0 |
#define DEVICES_BUCKETS 256 |
/* Implementation of hash table interface for the nodes hash table. */ |
static hash_index_t devices_hash(unsigned long key[]) |
{ |
return key[DEVICES_KEY_HANDLE] % DEVICES_BUCKETS; |
} |
static int devices_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
device_t *dev = hash_table_get_instance(item, device_t, link); |
return (dev->handle == (dev_handle_t) key[DEVICES_KEY_HANDLE]); |
} |
static void devices_remove_callback(link_t *item) |
{ |
free(hash_table_get_instance(item, device_t, link)); |
} |
static hash_table_operations_t devices_ops = { |
.hash = devices_hash, |
.compare = devices_compare, |
.remove_callback = devices_remove_callback |
}; |
bool devfs_init(void) |
{ |
if (!hash_table_create(&devices, DEVICES_BUCKETS, |
DEVICES_KEYS, &devices_ops)) |
return false; |
if (devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING) < 0) |
return false; |
return true; |
} |
void devfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* Accept the mount options */ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *opts = malloc(size + 1); |
if (!opts) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(opts); |
return; |
} |
free(opts); |
ipc_answer_3(rid, EOK, 0, 0, 0); |
} |
void devfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipcarg_t first = IPC_GET_ARG1(*request); |
ipcarg_t last = IPC_GET_ARG2(*request); |
dev_handle_t dev_handle = IPC_GET_ARG3(*request); |
ipcarg_t lflag = IPC_GET_ARG4(*request); |
fs_index_t index = IPC_GET_ARG5(*request); |
/* Hierarchy is flat, no altroot is supported */ |
if (index != 0) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
if ((lflag & L_LINK) || (lflag & L_UNLINK)) { |
ipc_answer_0(rid, ENOTSUP); |
return; |
} |
/* Eat slash */ |
if (PLB_GET_CHAR(first) == '/') { |
first++; |
first %= PLB_SIZE; |
} |
if (first >= last) { |
/* Root entry */ |
if (!(lflag & L_FILE)) |
ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0); |
else |
ipc_answer_0(rid, ENOENT); |
} else { |
if (!(lflag & L_DIRECTORY)) { |
size_t len; |
if (last >= first) |
len = last - first + 1; |
else |
len = first + PLB_SIZE - last + 1; |
char *name = (char *) malloc(len + 1); |
if (name == NULL) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
size_t i; |
for (i = 0; i < len; i++) |
name[i] = PLB_GET_CHAR(first + i); |
name[len] = 0; |
dev_handle_t handle; |
if (devmap_device_get_handle(name, &handle, 0) != EOK) { |
free(name); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
if (lflag & L_OPEN) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) handle |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
int phone = devmap_device_connect(handle, 0); |
if (phone < 0) { |
fibril_mutex_unlock(&devices_mutex); |
free(name); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = (device_t *) malloc(sizeof(device_t)); |
if (dev == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
free(name); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
dev->handle = handle; |
dev->phone = phone; |
dev->refcount = 1; |
hash_table_insert(&devices, key, &dev->link); |
} else { |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount++; |
} |
fibril_mutex_unlock(&devices_mutex); |
} |
free(name); |
ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, handle, 0, 1); |
} else |
ipc_answer_0(rid, ENOENT); |
} |
} |
void devfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t handle = IPC_GET_ARG2(*request); |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) handle |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
int phone = devmap_device_connect(handle, 0); |
if (phone < 0) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = (device_t *) malloc(sizeof(device_t)); |
if (dev == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
dev->handle = handle; |
dev->phone = phone; |
dev->refcount = 1; |
hash_table_insert(&devices, key, &dev->link); |
} else { |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount++; |
} |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_3(rid, EOK, 0, 1, L_FILE); |
} |
void devfs_stat(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size) || |
size != sizeof(struct stat)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
struct stat stat; |
memset(&stat, 0, sizeof(struct stat)); |
stat.fs_handle = devfs_reg.fs_handle; |
stat.dev_handle = dev_handle; |
stat.index = index; |
stat.lnkcnt = 1; |
stat.is_file = (index != 0); |
stat.size = 0; |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk != NULL) |
stat.devfs_stat.device = (dev_handle_t)index; |
fibril_mutex_unlock(&devices_mutex); |
} |
ipc_data_read_finalize(callid, &stat, sizeof(struct stat)); |
ipc_answer_0(rid, EOK); |
} |
void devfs_read(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
off_t pos = (off_t) IPC_GET_ARG3(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
ipc_callid_t callid; |
if (!ipc_data_read_receive(&callid, NULL)) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
/* Make a request at the driver */ |
ipc_call_t answer; |
aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
IPC_GET_ARG3(*request), &answer); |
/* Forward the IPC_M_DATA_READ request to the driver */ |
ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
fibril_mutex_unlock(&devices_mutex); |
/* Wait for reply from the driver. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
size_t bytes = IPC_GET_ARG1(answer); |
/* Driver reply is the final result of the whole operation */ |
ipc_answer_1(rid, rc, bytes); |
} else { |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
size_t count = devmap_device_get_count(); |
dev_desc_t *desc = malloc(count * sizeof(dev_desc_t)); |
if (desc == NULL) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_1(rid, ENOMEM, 0); |
return; |
} |
size_t max = devmap_device_get_devices(count, desc); |
if (pos < max) { |
ipc_data_read_finalize(callid, desc[pos].name, str_size(desc[pos].name) + 1); |
} else { |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
} |
free(desc); |
ipc_answer_1(rid, EOK, 1); |
} |
} |
void devfs_write(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
off_t pos = (off_t) IPC_GET_ARG3(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
ipc_callid_t callid; |
if (!ipc_data_write_receive(&callid, NULL)) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
/* Make a request at the driver */ |
ipc_call_t answer; |
aid_t msg = async_send_3(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), |
IPC_GET_ARG3(*request), &answer); |
/* Forward the IPC_M_DATA_WRITE request to the driver */ |
ipc_forward_fast(callid, dev->phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
fibril_mutex_unlock(&devices_mutex); |
/* Wait for reply from the driver. */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
size_t bytes = IPC_GET_ARG1(answer); |
/* Driver reply is the final result of the whole operation */ |
ipc_answer_1(rid, rc, bytes); |
} else { |
/* Read-only filesystem */ |
ipc_answer_0(rid, ENOTSUP); |
} |
} |
void devfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
dev->refcount--; |
if (dev->refcount == 0) { |
ipc_hangup(dev->phone); |
hash_table_remove(&devices, key, DEVICES_KEYS); |
} |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, EOK); |
} else |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request); |
if (index != 0) { |
unsigned long key[] = { |
[DEVICES_KEY_HANDLE] = (unsigned long) index |
}; |
fibril_mutex_lock(&devices_mutex); |
link_t *lnk = hash_table_find(&devices, key); |
if (lnk == NULL) { |
fibril_mutex_unlock(&devices_mutex); |
ipc_answer_0(rid, ENOENT); |
return; |
} |
device_t *dev = hash_table_get_instance(lnk, device_t, link); |
/* Make a request at the driver */ |
ipc_call_t answer; |
aid_t msg = async_send_2(dev->phone, IPC_GET_METHOD(*request), |
IPC_GET_ARG1(*request), IPC_GET_ARG2(*request), &answer); |
fibril_mutex_unlock(&devices_mutex); |
/* Wait for reply from the driver */ |
ipcarg_t rc; |
async_wait_for(msg, &rc); |
/* Driver reply is the final result of the whole operation */ |
ipc_answer_0(rid, rc); |
} else |
ipc_answer_0(rid, ENOTSUP); |
} |
void devfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, ENOTSUP); |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/devfs/devfs.c |
---|
0,0 → 1,141 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file devfs.c |
* @brief Devices file system. |
* |
* Every device registered to device mapper is represented as a file in this |
* file system. |
*/ |
#include <stdio.h> |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <async.h> |
#include <errno.h> |
#include <libfs.h> |
#include "devfs.h" |
#include "devfs_ops.h" |
#define NAME "devfs" |
static vfs_info_t devfs_vfs_info = { |
.name = "devfs", |
}; |
fs_reg_t devfs_reg; |
static void devfs_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
if (iid) |
ipc_answer_0(iid, EOK); |
while (true) { |
ipc_call_t call; |
ipc_callid_t callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
return; |
case VFS_OUT_MOUNTED: |
devfs_mounted(callid, &call); |
break; |
case VFS_OUT_MOUNT: |
devfs_mount(callid, &call); |
break; |
case VFS_OUT_LOOKUP: |
devfs_lookup(callid, &call); |
break; |
case VFS_OUT_OPEN_NODE: |
devfs_open_node(callid, &call); |
break; |
case VFS_OUT_STAT: |
devfs_stat(callid, &call); |
break; |
case VFS_OUT_READ: |
devfs_read(callid, &call); |
break; |
case VFS_OUT_WRITE: |
devfs_write(callid, &call); |
break; |
case VFS_OUT_TRUNCATE: |
devfs_truncate(callid, &call); |
break; |
case VFS_OUT_CLOSE: |
devfs_close(callid, &call); |
break; |
case VFS_OUT_SYNC: |
devfs_sync(callid, &call); |
break; |
case VFS_OUT_DESTROY: |
devfs_destroy(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
} |
} |
} |
int main(int argc, char *argv[]) |
{ |
printf(NAME ": HelenOS Device Filesystem\n"); |
if (!devfs_init()) { |
printf(NAME ": failed to initialize devfs\n"); |
return -1; |
} |
int vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
if (vfs_phone < EOK) { |
printf(NAME ": Unable to connect to VFS\n"); |
return -1; |
} |
int rc = fs_register(vfs_phone, &devfs_reg, &devfs_vfs_info, |
devfs_connection); |
if (rc != EOK) { |
printf(NAME ": Failed to register file system (%d)\n", rc); |
return rc; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* Not reached */ |
return 0; |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/devfs/devfs_ops.h |
---|
0,0 → 1,57 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef DEVFS_DEVFS_OPS_H_ |
#define DEVFS_DEVFS_OPS_H_ |
#include <ipc/ipc.h> |
#include <bool.h> |
extern bool devfs_init(void); |
extern void devfs_mounted(ipc_callid_t, ipc_call_t *); |
extern void devfs_mount(ipc_callid_t, ipc_call_t *); |
extern void devfs_lookup(ipc_callid_t, ipc_call_t *); |
extern void devfs_open_node(ipc_callid_t, ipc_call_t *); |
extern void devfs_stat(ipc_callid_t, ipc_call_t *); |
extern void devfs_sync(ipc_callid_t, ipc_call_t *); |
extern void devfs_read(ipc_callid_t, ipc_call_t *); |
extern void devfs_write(ipc_callid_t, ipc_call_t *); |
extern void devfs_truncate(ipc_callid_t, ipc_call_t *); |
extern void devfs_close(ipc_callid_t, ipc_call_t *); |
extern void devfs_destroy(ipc_callid_t, ipc_call_t *); |
#endif |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/devfs/Makefile |
---|
0,0 → 1,82 |
# |
# Copyright (c) 2005 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
LIBFS_PREFIX = ../../../lib/libfs |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -I $(LIBFS_PREFIX) |
LIBS = \ |
$(LIBFS_PREFIX)/libfs.a \ |
$(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = devfs |
SOURCES = \ |
devfs.c \ |
devfs_ops.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/arm/uspace/srv/fs/devfs/devfs.h |
---|
0,0 → 1,44 |
/* |
* Copyright (c) 2009 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef DEVFS_DEVFS_H_ |
#define DEVFS_DEVFS_H_ |
#include <libfs.h> |
extern fs_reg_t devfs_reg; |
#endif |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/tmpfs/tmpfs_ops.c |
---|
0,0 → 1,644 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file tmpfs_ops.c |
* @brief Implementation of VFS operations for the TMPFS file system |
* server. |
*/ |
#include "tmpfs.h" |
#include "../../vfs/vfs.h" |
#include <ipc/ipc.h> |
#include <async.h> |
#include <errno.h> |
#include <atomic.h> |
#include <stdlib.h> |
#include <string.h> |
#include <stdio.h> |
#include <assert.h> |
#include <sys/types.h> |
#include <adt/hash_table.h> |
#include <as.h> |
#include <libfs.h> |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
#define max(a, b) ((a) > (b) ? (a) : (b)) |
#define NODES_BUCKETS 256 |
/** All root nodes have index 0. */ |
#define TMPFS_SOME_ROOT 0 |
/** Global counter for assigning node indices. Shared by all instances. */ |
fs_index_t tmpfs_next_index = 1; |
/* |
* Implementation of the libfs interface. |
*/ |
/* Forward declarations of static functions. */ |
static fs_node_t *tmpfs_match(fs_node_t *, const char *); |
static fs_node_t *tmpfs_node_get(dev_handle_t, fs_index_t); |
static void tmpfs_node_put(fs_node_t *); |
static fs_node_t *tmpfs_create_node(dev_handle_t, int); |
static int tmpfs_link_node(fs_node_t *, fs_node_t *, const char *); |
static int tmpfs_unlink_node(fs_node_t *, fs_node_t *, const char *); |
static int tmpfs_destroy_node(fs_node_t *); |
/* Implementation of helper functions. */ |
static fs_index_t tmpfs_index_get(fs_node_t *fn) |
{ |
return TMPFS_NODE(fn)->index; |
} |
static size_t tmpfs_size_get(fs_node_t *fn) |
{ |
return TMPFS_NODE(fn)->size; |
} |
static unsigned tmpfs_lnkcnt_get(fs_node_t *fn) |
{ |
return TMPFS_NODE(fn)->lnkcnt; |
} |
static bool tmpfs_has_children(fs_node_t *fn) |
{ |
return !list_empty(&TMPFS_NODE(fn)->cs_head); |
} |
static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle) |
{ |
return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT); |
} |
static char tmpfs_plb_get_char(unsigned pos) |
{ |
return tmpfs_reg.plb_ro[pos % PLB_SIZE]; |
} |
static bool tmpfs_is_directory(fs_node_t *fn) |
{ |
return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY; |
} |
static bool tmpfs_is_file(fs_node_t *fn) |
{ |
return TMPFS_NODE(fn)->type == TMPFS_FILE; |
} |
/** libfs operations */ |
libfs_ops_t tmpfs_libfs_ops = { |
.match = tmpfs_match, |
.node_get = tmpfs_node_get, |
.node_put = tmpfs_node_put, |
.create = tmpfs_create_node, |
.destroy = tmpfs_destroy_node, |
.link = tmpfs_link_node, |
.unlink = tmpfs_unlink_node, |
.index_get = tmpfs_index_get, |
.size_get = tmpfs_size_get, |
.lnkcnt_get = tmpfs_lnkcnt_get, |
.has_children = tmpfs_has_children, |
.root_get = tmpfs_root_get, |
.plb_get_char = tmpfs_plb_get_char, |
.is_directory = tmpfs_is_directory, |
.is_file = tmpfs_is_file |
}; |
/** Hash table of all TMPFS nodes. */ |
hash_table_t nodes; |
#define NODES_KEY_INDEX 0 |
#define NODES_KEY_DEV 1 |
/* Implementation of hash table interface for the nodes hash table. */ |
static hash_index_t nodes_hash(unsigned long key[]) |
{ |
return key[NODES_KEY_INDEX] % NODES_BUCKETS; |
} |
static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
tmpfs_node_t *nodep = hash_table_get_instance(item, tmpfs_node_t, |
nh_link); |
return (nodep->index == key[NODES_KEY_INDEX] && |
nodep->dev_handle == key[NODES_KEY_DEV]); |
} |
static void nodes_remove_callback(link_t *item) |
{ |
} |
/** TMPFS nodes hash table operations. */ |
hash_table_operations_t nodes_ops = { |
.hash = nodes_hash, |
.compare = nodes_compare, |
.remove_callback = nodes_remove_callback |
}; |
static void tmpfs_node_initialize(tmpfs_node_t *nodep) |
{ |
nodep->bp = NULL; |
nodep->index = 0; |
nodep->dev_handle = 0; |
nodep->type = TMPFS_NONE; |
nodep->lnkcnt = 0; |
nodep->size = 0; |
nodep->data = NULL; |
link_initialize(&nodep->nh_link); |
list_initialize(&nodep->cs_head); |
} |
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp) |
{ |
link_initialize(&dentryp->link); |
dentryp->name = NULL; |
dentryp->node = NULL; |
} |
bool tmpfs_init(void) |
{ |
if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops)) |
return false; |
return true; |
} |
static bool tmpfs_instance_init(dev_handle_t dev_handle) |
{ |
fs_node_t *rfn; |
rfn = tmpfs_create_node(dev_handle, L_DIRECTORY); |
if (!rfn) |
return false; |
TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */ |
return true; |
} |
fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component) |
{ |
tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
link_t *lnk; |
for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
lnk = lnk->next) { |
tmpfs_dentry_t *dentryp = list_get_instance(lnk, tmpfs_dentry_t, |
link); |
if (!str_cmp(dentryp->name, component)) |
return FS_NODE(dentryp->node); |
} |
return NULL; |
} |
fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index) |
{ |
unsigned long key[] = { |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
link_t *lnk = hash_table_find(&nodes, key); |
if (!lnk) |
return NULL; |
return FS_NODE(hash_table_get_instance(lnk, tmpfs_node_t, nh_link)); |
} |
void tmpfs_node_put(fs_node_t *fn) |
{ |
/* nothing to do */ |
} |
fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag) |
{ |
assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY)); |
tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t)); |
if (!nodep) |
return NULL; |
tmpfs_node_initialize(nodep); |
nodep->bp = malloc(sizeof(fs_node_t)); |
if (!nodep->bp) { |
free(nodep); |
return NULL; |
} |
fs_node_initialize(nodep->bp); |
nodep->bp->data = nodep; /* link the FS and TMPFS nodes */ |
if (!tmpfs_root_get(dev_handle)) |
nodep->index = TMPFS_SOME_ROOT; |
else |
nodep->index = tmpfs_next_index++; |
nodep->dev_handle = dev_handle; |
if (lflag & L_DIRECTORY) |
nodep->type = TMPFS_DIRECTORY; |
else |
nodep->type = TMPFS_FILE; |
/* Insert the new node into the nodes hash table. */ |
unsigned long key[] = { |
[NODES_KEY_INDEX] = nodep->index, |
[NODES_KEY_DEV] = nodep->dev_handle |
}; |
hash_table_insert(&nodes, key, &nodep->nh_link); |
return FS_NODE(nodep); |
} |
int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
{ |
tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
tmpfs_node_t *childp = TMPFS_NODE(cfn); |
tmpfs_dentry_t *dentryp; |
link_t *lnk; |
assert(parentp->type == TMPFS_DIRECTORY); |
/* Check for duplicit entries. */ |
for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
lnk = lnk->next) { |
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
if (!str_cmp(dentryp->name, nm)) |
return EEXIST; |
} |
/* Allocate and initialize the dentry. */ |
dentryp = malloc(sizeof(tmpfs_dentry_t)); |
if (!dentryp) |
return ENOMEM; |
tmpfs_dentry_initialize(dentryp); |
/* Populate and link the new dentry. */ |
size_t size = str_size(nm); |
dentryp->name = malloc(size + 1); |
if (!dentryp->name) { |
free(dentryp); |
return ENOMEM; |
} |
str_cpy(dentryp->name, size + 1, nm); |
dentryp->node = childp; |
childp->lnkcnt++; |
list_append(&dentryp->link, &parentp->cs_head); |
return EOK; |
} |
int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
{ |
tmpfs_node_t *parentp = TMPFS_NODE(pfn); |
tmpfs_node_t *childp = NULL; |
tmpfs_dentry_t *dentryp; |
link_t *lnk; |
if (!parentp) |
return EBUSY; |
for (lnk = parentp->cs_head.next; lnk != &parentp->cs_head; |
lnk = lnk->next) { |
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
if (!str_cmp(dentryp->name, nm)) { |
childp = dentryp->node; |
assert(FS_NODE(childp) == cfn); |
break; |
} |
} |
if (!childp) |
return ENOENT; |
if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head)) |
return ENOTEMPTY; |
list_remove(&dentryp->link); |
free(dentryp); |
childp->lnkcnt--; |
return EOK; |
} |
int tmpfs_destroy_node(fs_node_t *fn) |
{ |
tmpfs_node_t *nodep = TMPFS_NODE(fn); |
assert(!nodep->lnkcnt); |
assert(list_empty(&nodep->cs_head)); |
unsigned long key[] = { |
[NODES_KEY_INDEX] = nodep->index, |
[NODES_KEY_DEV] = nodep->dev_handle |
}; |
hash_table_remove(&nodes, key, 2); |
if (nodep->type == TMPFS_FILE) |
free(nodep->data); |
free(nodep->bp); |
free(nodep); |
return EOK; |
} |
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
/* accept the mount options */ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *opts = malloc(size + 1); |
if (!opts) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(opts); |
return; |
} |
opts[size] = '\0'; |
/* Initialize TMPFS instance. */ |
if (!tmpfs_instance_init(dev_handle)) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
tmpfs_node_t *rootp = TMPFS_NODE(tmpfs_root_get(dev_handle)); |
if (str_cmp(opts, "restore") == 0) { |
if (tmpfs_restore(dev_handle)) |
ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
rootp->lnkcnt); |
else |
ipc_answer_0(rid, ELIMIT); |
} else { |
ipc_answer_3(rid, EOK, rootp->index, rootp->size, |
rootp->lnkcnt); |
} |
} |
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request) |
{ |
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); |
/* |
* Lookup the respective TMPFS node. |
*/ |
link_t *hlp; |
unsigned long key[] = { |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle, |
}; |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
/* |
* Receive the read request. |
*/ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_read_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
size_t bytes; |
if (nodep->type == TMPFS_FILE) { |
bytes = max(0, min(nodep->size - pos, size)); |
(void) ipc_data_read_finalize(callid, nodep->data + pos, |
bytes); |
} else { |
tmpfs_dentry_t *dentryp; |
link_t *lnk; |
int i; |
assert(nodep->type == TMPFS_DIRECTORY); |
/* |
* Yes, we really use O(n) algorithm here. |
* If it bothers someone, it could be fixed by introducing a |
* hash table. |
*/ |
for (i = 0, lnk = nodep->cs_head.next; |
i < pos && lnk != &nodep->cs_head; |
i++, lnk = lnk->next) |
; |
if (lnk == &nodep->cs_head) { |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
} |
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link); |
(void) ipc_data_read_finalize(callid, dentryp->name, |
str_size(dentryp->name) + 1); |
bytes = 1; |
} |
/* |
* Answer the VFS_READ call. |
*/ |
ipc_answer_1(rid, EOK, bytes); |
} |
void tmpfs_write(ipc_callid_t rid, ipc_call_t *request) |
{ |
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); |
/* |
* Lookup the respective TMPFS node. |
*/ |
link_t *hlp; |
unsigned long key[] = { |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
/* |
* Receive the write request. |
*/ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
/* |
* Check whether the file needs to grow. |
*/ |
if (pos + size <= nodep->size) { |
/* The file size is not changing. */ |
(void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
ipc_answer_2(rid, EOK, size, nodep->size); |
return; |
} |
size_t delta = (pos + size) - nodep->size; |
/* |
* At this point, we are deliberately extremely straightforward and |
* simply realloc the contents of the file on every write that grows the |
* file. In the end, the situation might not be as bad as it may look: |
* our heap allocator can save us and just grow the block whenever |
* possible. |
*/ |
void *newdata = realloc(nodep->data, nodep->size + delta); |
if (!newdata) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_2(rid, EOK, 0, nodep->size); |
return; |
} |
/* Clear any newly allocated memory in order to emulate gaps. */ |
memset(newdata + nodep->size, 0, delta); |
nodep->size += delta; |
nodep->data = newdata; |
(void) ipc_data_write_finalize(callid, nodep->data + pos, size); |
ipc_answer_2(rid, EOK, size, nodep->size); |
} |
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request) |
{ |
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); |
/* |
* Lookup the respective TMPFS node. |
*/ |
link_t *hlp; |
unsigned long key[] = { |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
if (size == nodep->size) { |
ipc_answer_0(rid, EOK); |
return; |
} |
void *newdata = realloc(nodep->data, size); |
if (!newdata) { |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
if (size > nodep->size) { |
size_t delta = size - nodep->size; |
memset(newdata + nodep->size, 0, delta); |
} |
nodep->size = size; |
nodep->data = newdata; |
ipc_answer_0(rid, EOK); |
} |
void tmpfs_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, EOK); |
} |
void tmpfs_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request); |
int rc; |
link_t *hlp; |
unsigned long key[] = { |
[NODES_KEY_INDEX] = index, |
[NODES_KEY_DEV] = dev_handle |
}; |
hlp = hash_table_find(&nodes, key); |
if (!hlp) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t, |
nh_link); |
rc = tmpfs_destroy_node(FS_NODE(nodep)); |
ipc_answer_0(rid, rc); |
} |
void tmpfs_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_open_node(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
void tmpfs_stat(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_stat(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request); |
} |
void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* Dummy implementation */ |
ipc_answer_0(rid, EOK); |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/tmpfs/tmpfs.h |
---|
0,0 → 1,101 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef TMPFS_TMPFS_H_ |
#define TMPFS_TMPFS_H_ |
#include <ipc/ipc.h> |
#include <libfs.h> |
#include <atomic.h> |
#include <sys/types.h> |
#include <bool.h> |
#include <adt/hash_table.h> |
#ifndef dprintf |
#define dprintf(...) printf(__VA_ARGS__) |
#endif |
#define TMPFS_NODE(node) ((node) ? (tmpfs_node_t *)(node)->data : NULL) |
#define FS_NODE(node) ((node) ? (node)->bp : NULL) |
typedef enum { |
TMPFS_NONE, |
TMPFS_FILE, |
TMPFS_DIRECTORY |
} tmpfs_dentry_type_t; |
/* forward declaration */ |
struct tmpfs_node; |
typedef struct tmpfs_dentry { |
link_t link; /**< Linkage for the list of siblings. */ |
struct tmpfs_node *node;/**< Back pointer to TMPFS node. */ |
char *name; /**< Name of dentry. */ |
} tmpfs_dentry_t; |
typedef struct tmpfs_node { |
fs_node_t *bp; /**< Back pointer to the FS node. */ |
fs_index_t index; /**< TMPFS node index. */ |
dev_handle_t dev_handle;/**< Device handle. */ |
link_t nh_link; /**< Nodes hash table link. */ |
tmpfs_dentry_type_t type; |
unsigned lnkcnt; /**< Link count. */ |
size_t size; /**< File size if type is TMPFS_FILE. */ |
void *data; /**< File content's if type is TMPFS_FILE. */ |
link_t cs_head; /**< Head of child's siblings list. */ |
} tmpfs_node_t; |
extern fs_reg_t tmpfs_reg; |
extern libfs_ops_t tmpfs_libfs_ops; |
extern bool tmpfs_init(void); |
extern void tmpfs_mounted(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_mount(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_lookup(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_read(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_write(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_truncate(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_stat(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_close(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_destroy(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_open_node(ipc_callid_t, ipc_call_t *); |
extern void tmpfs_sync(ipc_callid_t, ipc_call_t *); |
extern bool tmpfs_restore(dev_handle_t); |
#endif |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/tmpfs/tmpfs.c |
---|
0,0 → 1,171 |
/* |
* Copyright (c) 2006 Martin Decky |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file tmpfs.c |
* @brief File system driver for in-memory file system. |
* |
* Every instance of tmpfs exists purely in memory and has neither a disk layout |
* nor any permanent storage (e.g. disk blocks). With each system reboot, data |
* stored in a tmpfs file system is lost. |
*/ |
#include "tmpfs.h" |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <async.h> |
#include <errno.h> |
#include <unistd.h> |
#include <stdio.h> |
#include <libfs.h> |
#include "../../vfs/vfs.h" |
#define NAME "tmpfs" |
vfs_info_t tmpfs_vfs_info = { |
.name = "tmpfs", |
}; |
fs_reg_t tmpfs_reg; |
/** |
* This connection fibril processes VFS requests from VFS. |
* |
* In order to support simultaneous VFS requests, our design is as follows. |
* The connection fibril accepts VFS requests from VFS. If there is only one |
* instance of the fibril, VFS will need to serialize all VFS requests it sends |
* to FAT. To overcome this bottleneck, VFS can send TMPFS the |
* IPC_M_CONNECT_ME_TO call. In that case, a new connection fibril will be |
* created, which in turn will accept the call. Thus, a new phone will be |
* opened for VFS. |
* |
* There are few issues with this arrangement. First, VFS can run out of |
* available phones. In that case, VFS can close some other phones or use one |
* phone for more serialized requests. Similarily, TMPFS can refuse to duplicate |
* the connection. VFS should then just make use of already existing phones and |
* route its requests through them. To avoid paying the fibril creation price |
* upon each request, TMPFS might want to keep the connections open after the |
* request has been completed. |
*/ |
static void tmpfs_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
if (iid) { |
/* |
* This only happens for connections opened by |
* IPC_M_CONNECT_ME_TO calls as opposed to callback connections |
* created by IPC_M_CONNECT_TO_ME. |
*/ |
ipc_answer_0(iid, EOK); |
} |
dprintf("VFS-TMPFS connection established.\n"); |
while (1) { |
ipc_callid_t callid; |
ipc_call_t call; |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
return; |
case VFS_OUT_MOUNTED: |
tmpfs_mounted(callid, &call); |
break; |
case VFS_OUT_MOUNT: |
tmpfs_mount(callid, &call); |
break; |
case VFS_OUT_LOOKUP: |
tmpfs_lookup(callid, &call); |
break; |
case VFS_OUT_READ: |
tmpfs_read(callid, &call); |
break; |
case VFS_OUT_WRITE: |
tmpfs_write(callid, &call); |
break; |
case VFS_OUT_TRUNCATE: |
tmpfs_truncate(callid, &call); |
break; |
case VFS_OUT_CLOSE: |
tmpfs_close(callid, &call); |
break; |
case VFS_OUT_DESTROY: |
tmpfs_destroy(callid, &call); |
break; |
case VFS_OUT_OPEN_NODE: |
tmpfs_open_node(callid, &call); |
break; |
case VFS_OUT_STAT: |
tmpfs_stat(callid, &call); |
break; |
case VFS_OUT_SYNC: |
tmpfs_sync(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
} |
} |
} |
int main(int argc, char **argv) |
{ |
printf(NAME ": HelenOS TMPFS file system server\n"); |
if (!tmpfs_init()) { |
printf(NAME ": failed to initialize TMPFS\n"); |
return -1; |
} |
int vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
if (vfs_phone < EOK) { |
printf(NAME ": Unable to connect to VFS\n"); |
return -1; |
} |
int rc = fs_register(vfs_phone, &tmpfs_reg, &tmpfs_vfs_info, |
tmpfs_connection); |
if (rc != EOK) { |
printf(NAME ": Failed to register file system (%d)\n", rc); |
return rc; |
} |
printf(NAME ": Accepting connections\n"); |
async_manager(); |
/* not reached */ |
return 0; |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/tmpfs/tmpfs_dump.c |
---|
0,0 → 1,199 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* Copyright (c) 2008 Martin Decky |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file tmpfs_dump.c |
* @brief Support for loading TMPFS file system dump. |
*/ |
#include "tmpfs.h" |
#include "../../vfs/vfs.h" |
#include <errno.h> |
#include <stdlib.h> |
#include <string.h> |
#include <sys/types.h> |
#include <as.h> |
#include <libblock.h> |
#include <byteorder.h> |
#define TMPFS_BLOCK_SIZE 1024 |
struct rdentry { |
uint8_t type; |
uint32_t len; |
} __attribute__((packed)); |
static bool |
tmpfs_restore_recursion(dev_handle_t dev, off_t *bufpos, size_t *buflen, |
off_t *pos, fs_node_t *pfn) |
{ |
struct rdentry entry; |
libfs_ops_t *ops = &tmpfs_libfs_ops; |
int rc; |
do { |
char *fname; |
fs_node_t *fn; |
tmpfs_node_t *nodep; |
uint32_t size; |
if (block_seqread(dev, bufpos, buflen, pos, &entry, |
sizeof(entry), TMPFS_BLOCK_SIZE) != EOK) |
return false; |
entry.len = uint32_t_le2host(entry.len); |
switch (entry.type) { |
case TMPFS_NONE: |
break; |
case TMPFS_FILE: |
fname = malloc(entry.len + 1); |
if (fname == NULL) |
return false; |
fn = ops->create(dev, L_FILE); |
if (fn == NULL) { |
free(fname); |
return false; |
} |
if (block_seqread(dev, bufpos, buflen, pos, fname, |
entry.len, TMPFS_BLOCK_SIZE) != EOK) { |
ops->destroy(fn); |
free(fname); |
return false; |
} |
fname[entry.len] = 0; |
rc = ops->link(pfn, fn, fname); |
if (rc != EOK) { |
ops->destroy(fn); |
free(fname); |
return false; |
} |
free(fname); |
if (block_seqread(dev, bufpos, buflen, pos, &size, |
sizeof(size), TMPFS_BLOCK_SIZE) != EOK) |
return false; |
size = uint32_t_le2host(size); |
nodep = TMPFS_NODE(fn); |
nodep->data = malloc(size); |
if (nodep->data == NULL) |
return false; |
nodep->size = size; |
if (block_seqread(dev, bufpos, buflen, pos, nodep->data, |
size, TMPFS_BLOCK_SIZE) != EOK) |
return false; |
break; |
case TMPFS_DIRECTORY: |
fname = malloc(entry.len + 1); |
if (fname == NULL) |
return false; |
fn = ops->create(dev, L_DIRECTORY); |
if (fn == NULL) { |
free(fname); |
return false; |
} |
if (block_seqread(dev, bufpos, buflen, pos, fname, |
entry.len, TMPFS_BLOCK_SIZE) != EOK) { |
ops->destroy(fn); |
free(fname); |
return false; |
} |
fname[entry.len] = 0; |
rc = ops->link(pfn, fn, fname); |
if (rc != EOK) { |
ops->destroy(fn); |
free(fname); |
return false; |
} |
free(fname); |
if (!tmpfs_restore_recursion(dev, bufpos, buflen, pos, |
fn)) |
return false; |
break; |
default: |
return false; |
} |
} while (entry.type != TMPFS_NONE); |
return true; |
} |
bool tmpfs_restore(dev_handle_t dev) |
{ |
libfs_ops_t *ops = &tmpfs_libfs_ops; |
int rc; |
rc = block_init(dev, TMPFS_BLOCK_SIZE); |
if (rc != EOK) |
return false; |
off_t bufpos = 0; |
size_t buflen = 0; |
off_t pos = 0; |
char tag[6]; |
if (block_seqread(dev, &bufpos, &buflen, &pos, tag, 5, |
TMPFS_BLOCK_SIZE) != EOK) |
goto error; |
tag[5] = 0; |
if (str_cmp(tag, "TMPFS") != 0) |
goto error; |
if (!tmpfs_restore_recursion(dev, &bufpos, &buflen, &pos, |
ops->root_get(dev))) |
goto error; |
block_fini(dev); |
return true; |
error: |
block_fini(dev); |
return false; |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/tmpfs/Makefile |
---|
0,0 → 1,84 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
LIBFS_PREFIX = ../../../lib/libfs |
LIBBLOCK_PREFIX = ../../../lib/libblock |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -I $(LIBFS_PREFIX) -I $(LIBBLOCK_PREFIX) |
LIBS = \ |
$(LIBFS_PREFIX)/libfs.a \ |
$(LIBBLOCK_PREFIX)/libblock.a \ |
$(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = tmpfs |
SOURCES = \ |
tmpfs.c \ |
tmpfs_ops.c \ |
tmpfs_dump.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/arm/uspace/srv/fs/fat/fat_ops.c |
---|
0,0 → 1,1218 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file fat_ops.c |
* @brief Implementation of VFS operations for the FAT file system server. |
*/ |
#include "fat.h" |
#include "fat_dentry.h" |
#include "fat_fat.h" |
#include "../../vfs/vfs.h" |
#include <libfs.h> |
#include <libblock.h> |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <ipc/devmap.h> |
#include <async.h> |
#include <errno.h> |
#include <string.h> |
#include <byteorder.h> |
#include <adt/hash_table.h> |
#include <adt/list.h> |
#include <assert.h> |
#include <fibril_sync.h> |
#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) |
/** Mutex protecting the list of cached free FAT nodes. */ |
static FIBRIL_MUTEX_INITIALIZE(ffn_mutex); |
/** List of cached free FAT nodes. */ |
static LIST_INITIALIZE(ffn_head); |
static void fat_node_initialize(fat_node_t *node) |
{ |
fibril_mutex_initialize(&node->lock); |
node->bp = NULL; |
node->idx = NULL; |
node->type = 0; |
link_initialize(&node->ffn_link); |
node->size = 0; |
node->lnkcnt = 0; |
node->refcnt = 0; |
node->dirty = false; |
} |
static void fat_node_sync(fat_node_t *node) |
{ |
block_t *b; |
fat_bs_t *bs; |
fat_dentry_t *d; |
uint16_t bps; |
unsigned dps; |
assert(node->dirty); |
bs = block_bb_get(node->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
/* Read the block that contains the dentry of interest. */ |
b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc, |
(node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps); |
d->firstc = host2uint16_t_le(node->firstc); |
if (node->type == FAT_FILE) { |
d->size = host2uint32_t_le(node->size); |
} else if (node->type == FAT_DIRECTORY) { |
d->attr = FAT_ATTR_SUBDIR; |
} |
/* TODO: update other fields? (e.g time fields) */ |
b->dirty = true; /* need to sync block */ |
block_put(b); |
} |
static fat_node_t *fat_node_get_new(void) |
{ |
fs_node_t *fn; |
fat_node_t *nodep; |
fibril_mutex_lock(&ffn_mutex); |
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 (!fibril_mutex_trylock(&nodep->lock)) |
goto skip_cache; |
idxp_tmp = nodep->idx; |
if (!fibril_mutex_trylock(&idxp_tmp->lock)) { |
fibril_mutex_unlock(&nodep->lock); |
goto skip_cache; |
} |
list_remove(&nodep->ffn_link); |
fibril_mutex_unlock(&ffn_mutex); |
if (nodep->dirty) |
fat_node_sync(nodep); |
idxp_tmp->nodep = NULL; |
fibril_mutex_unlock(&nodep->lock); |
fibril_mutex_unlock(&idxp_tmp->lock); |
fn = FS_NODE(nodep); |
} else { |
skip_cache: |
/* Try to allocate a new node structure. */ |
fibril_mutex_unlock(&ffn_mutex); |
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); |
return NULL; |
} |
} |
fat_node_initialize(nodep); |
fs_node_initialize(fn); |
fn->data = nodep; |
nodep->bp = fn; |
return nodep; |
} |
/** Internal version of fat_node_get(). |
* |
* @param idxp Locked index structure. |
*/ |
static fat_node_t *fat_node_get_core(fat_idx_t *idxp) |
{ |
block_t *b; |
fat_bs_t *bs; |
fat_dentry_t *d; |
fat_node_t *nodep = NULL; |
unsigned bps; |
unsigned spc; |
unsigned dps; |
if (idxp->nodep) { |
/* |
* We are lucky. |
* The node is already instantiated in memory. |
*/ |
fibril_mutex_lock(&idxp->nodep->lock); |
if (!idxp->nodep->refcnt++) |
list_remove(&idxp->nodep->ffn_link); |
fibril_mutex_unlock(&idxp->nodep->lock); |
return idxp->nodep; |
} |
/* |
* We must instantiate the node from the file system. |
*/ |
assert(idxp->pfc); |
nodep = fat_node_get_new(); |
if (!nodep) |
return NULL; |
bs = block_bb_get(idxp->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
spc = bs->spc; |
dps = bps / sizeof(fat_dentry_t); |
/* Read the block that contains the dentry of interest. */ |
b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc, |
(idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE); |
assert(b); |
d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps); |
if (d->attr & FAT_ATTR_SUBDIR) { |
/* |
* The only directory which does not have this bit set is the |
* root directory itself. The root directory node is handled |
* and initialized elsewhere. |
*/ |
nodep->type = FAT_DIRECTORY; |
/* |
* Unfortunately, the 'size' field of the FAT dentry is not |
* defined for the directory entry type. We must determine the |
* size of the directory by walking the FAT. |
*/ |
nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle, |
uint16_t_le2host(d->firstc)); |
} else { |
nodep->type = FAT_FILE; |
nodep->size = uint32_t_le2host(d->size); |
} |
nodep->firstc = uint16_t_le2host(d->firstc); |
nodep->lnkcnt = 1; |
nodep->refcnt = 1; |
block_put(b); |
/* Link the idx structure with the node structure. */ |
nodep->idx = idxp; |
idxp->nodep = nodep; |
return nodep; |
} |
/* |
* 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 char fat_plb_get_char(unsigned); |
static bool fat_is_directory(fs_node_t *); |
static bool fat_is_file(fs_node_t *node); |
/* |
* FAT libfs operations. |
*/ |
/** Instantiate a FAT in-core node. */ |
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index) |
{ |
fat_node_t *nodep; |
fat_idx_t *idxp; |
idxp = fat_idx_get_by_index(dev_handle, index); |
if (!idxp) |
return NULL; |
/* idxp->lock held */ |
nodep = fat_node_get_core(idxp); |
fibril_mutex_unlock(&idxp->lock); |
return FS_NODE(nodep); |
} |
void fat_node_put(fs_node_t *fn) |
{ |
fat_node_t *nodep = FAT_NODE(fn); |
bool destroy = false; |
fibril_mutex_lock(&nodep->lock); |
if (!--nodep->refcnt) { |
if (nodep->idx) { |
fibril_mutex_lock(&ffn_mutex); |
list_append(&nodep->ffn_link, &ffn_head); |
fibril_mutex_unlock(&ffn_mutex); |
} else { |
/* |
* The node does not have any index structure associated |
* with itself. This can only mean that we are releasing |
* the node after a failed attempt to allocate the index |
* structure for it. |
*/ |
destroy = true; |
} |
} |
fibril_mutex_unlock(&nodep->lock); |
if (destroy) { |
free(nodep->bp); |
free(nodep); |
} |
} |
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags) |
{ |
fat_idx_t *idxp; |
fat_node_t *nodep; |
fat_bs_t *bs; |
fat_cluster_t mcl, lcl; |
uint16_t bps; |
int rc; |
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
if (flags & L_DIRECTORY) { |
/* allocate a cluster */ |
rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl); |
if (rc != EOK) |
return NULL; |
} |
nodep = fat_node_get_new(); |
if (!nodep) { |
fat_free_clusters(bs, dev_handle, mcl); |
return NULL; |
} |
idxp = fat_idx_get_new(dev_handle); |
if (!idxp) { |
fat_free_clusters(bs, dev_handle, mcl); |
fat_node_put(FS_NODE(nodep)); |
return NULL; |
} |
/* idxp->lock held */ |
if (flags & L_DIRECTORY) { |
int i; |
block_t *b; |
/* |
* Populate the new cluster with unused dentries. |
*/ |
for (i = 0; i < bs->spc; i++) { |
b = _fat_block_get(bs, dev_handle, mcl, i, |
BLOCK_FLAGS_NOREAD); |
/* mark all dentries as never-used */ |
memset(b->data, 0, bps); |
b->dirty = false; |
block_put(b); |
} |
nodep->type = FAT_DIRECTORY; |
nodep->firstc = mcl; |
nodep->size = bps * bs->spc; |
} else { |
nodep->type = FAT_FILE; |
nodep->firstc = FAT_CLST_RES0; |
nodep->size = 0; |
} |
nodep->lnkcnt = 0; /* not linked anywhere */ |
nodep->refcnt = 1; |
nodep->dirty = true; |
nodep->idx = idxp; |
idxp->nodep = nodep; |
fibril_mutex_unlock(&idxp->lock); |
return FS_NODE(nodep); |
} |
int fat_destroy_node(fs_node_t *fn) |
{ |
fat_node_t *nodep = FAT_NODE(fn); |
fat_bs_t *bs; |
/* |
* The node is not reachable from the file system. This means that the |
* link count should be zero and that the index structure cannot be |
* found in the position hash. Obviously, we don't need to lock the node |
* nor its index structure. |
*/ |
assert(nodep->lnkcnt == 0); |
/* |
* The node may not have any children. |
*/ |
assert(fat_has_children(fn) == false); |
bs = block_bb_get(nodep->idx->dev_handle); |
if (nodep->firstc != FAT_CLST_RES0) { |
assert(nodep->size); |
/* Free all clusters allocated to the node. */ |
fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc); |
} |
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) |
{ |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *childp = FAT_NODE(cfn); |
fat_dentry_t *d; |
fat_bs_t *bs; |
block_t *b; |
int i, j; |
uint16_t bps; |
unsigned dps; |
unsigned blocks; |
fat_cluster_t mcl, lcl; |
int rc; |
fibril_mutex_lock(&childp->lock); |
if (childp->lnkcnt == 1) { |
/* |
* On FAT, we don't support multiple hard links. |
*/ |
fibril_mutex_unlock(&childp->lock); |
return EMLINK; |
} |
assert(childp->lnkcnt == 0); |
fibril_mutex_unlock(&childp->lock); |
if (!fat_dentry_name_verify(name)) { |
/* |
* Attempt to create unsupported name. |
*/ |
return ENOTSUP; |
} |
/* |
* Get us an unused parent node's dentry or grow the parent and allocate |
* a new one. |
*/ |
fibril_mutex_lock(&parentp->idx->lock); |
bs = block_bb_get(parentp->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
blocks = parentp->size / bps; |
for (i = 0; i < blocks; i++) { |
b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
for (j = 0; j < dps; j++) { |
d = ((fat_dentry_t *)b->data) + j; |
switch (fat_classify_dentry(d)) { |
case FAT_DENTRY_SKIP: |
case FAT_DENTRY_VALID: |
/* skipping used and meta entries */ |
continue; |
case FAT_DENTRY_FREE: |
case FAT_DENTRY_LAST: |
/* found an empty slot */ |
goto hit; |
} |
} |
block_put(b); |
} |
j = 0; |
/* |
* We need to grow the parent in order to create a new unused dentry. |
*/ |
if (parentp->idx->pfc == FAT_CLST_ROOT) { |
/* Can't grow the root directory. */ |
fibril_mutex_unlock(&parentp->idx->lock); |
return ENOSPC; |
} |
rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl); |
if (rc != EOK) { |
fibril_mutex_unlock(&parentp->idx->lock); |
return rc; |
} |
fat_append_clusters(bs, parentp, mcl); |
b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD); |
d = (fat_dentry_t *)b->data; |
/* |
* Clear all dentries in the block except for the first one (the first |
* dentry will be cleared in the next step). |
*/ |
memset(d + 1, 0, bps - sizeof(fat_dentry_t)); |
hit: |
/* |
* At this point we only establish the link between the parent and the |
* child. The dentry, except of the name and the extension, will remain |
* uninitialized until the corresponding node is synced. Thus the valid |
* dentry data is kept in the child node structure. |
*/ |
memset(d, 0, sizeof(fat_dentry_t)); |
fat_dentry_name_set(d, name); |
b->dirty = true; /* need to sync block */ |
block_put(b); |
fibril_mutex_unlock(&parentp->idx->lock); |
fibril_mutex_lock(&childp->idx->lock); |
/* |
* If possible, create the Sub-directory Identifier Entry and the |
* Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries |
* are not mandatory according to Standard ECMA-107 and HelenOS VFS does |
* not use them anyway, so this is rather a sign of our good will. |
*/ |
b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE); |
d = (fat_dentry_t *)b->data; |
if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
str_cmp(d->name, FAT_NAME_DOT) == 0) { |
memset(d, 0, sizeof(fat_dentry_t)); |
str_cpy(d->name, 8, FAT_NAME_DOT); |
str_cpy(d->ext, 3, FAT_EXT_PAD); |
d->attr = FAT_ATTR_SUBDIR; |
d->firstc = host2uint16_t_le(childp->firstc); |
/* TODO: initialize also the date/time members. */ |
} |
d++; |
if (fat_classify_dentry(d) == FAT_DENTRY_LAST || |
str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) { |
memset(d, 0, sizeof(fat_dentry_t)); |
str_cpy(d->name, 8, FAT_NAME_DOT_DOT); |
str_cpy(d->ext, 3, FAT_EXT_PAD); |
d->attr = FAT_ATTR_SUBDIR; |
d->firstc = (parentp->firstc == FAT_CLST_ROOT) ? |
host2uint16_t_le(FAT_CLST_RES0) : |
host2uint16_t_le(parentp->firstc); |
/* TODO: initialize also the date/time members. */ |
} |
b->dirty = true; /* need to sync block */ |
block_put(b); |
childp->idx->pfc = parentp->firstc; |
childp->idx->pdi = i * dps + j; |
fibril_mutex_unlock(&childp->idx->lock); |
fibril_mutex_lock(&childp->lock); |
childp->lnkcnt = 1; |
childp->dirty = true; /* need to sync node */ |
fibril_mutex_unlock(&childp->lock); |
/* |
* Hash in the index structure into the position hash. |
*/ |
fat_idx_hashin(childp->idx); |
return EOK; |
} |
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm) |
{ |
fat_node_t *parentp = FAT_NODE(pfn); |
fat_node_t *childp = FAT_NODE(cfn); |
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; |
fibril_mutex_lock(&parentp->lock); |
fibril_mutex_lock(&childp->lock); |
assert(childp->lnkcnt == 1); |
fibril_mutex_lock(&childp->idx->lock); |
bs = block_bb_get(childp->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc, |
(childp->idx->pdi * sizeof(fat_dentry_t)) / bps, |
BLOCK_FLAGS_NONE); |
d = (fat_dentry_t *)b->data + |
(childp->idx->pdi % (bps / sizeof(fat_dentry_t))); |
/* mark the dentry as not-currently-used */ |
d->name[0] = FAT_DENTRY_ERASED; |
b->dirty = true; /* need to sync block */ |
block_put(b); |
/* remove the index structure from the position hash */ |
fat_idx_hashout(childp->idx); |
/* clear position information */ |
childp->idx->pfc = FAT_CLST_RES0; |
childp->idx->pdi = 0; |
fibril_mutex_unlock(&childp->idx->lock); |
childp->lnkcnt = 0; |
childp->dirty = true; |
fibril_mutex_unlock(&childp->lock); |
fibril_mutex_unlock(&parentp->lock); |
return EOK; |
} |
fs_node_t *fat_match(fs_node_t *pfn, const char *component) |
{ |
fat_bs_t *bs; |
fat_node_t *parentp = FAT_NODE(pfn); |
char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
unsigned i, j; |
unsigned bps; /* bytes per sector */ |
unsigned dps; /* dentries per sector */ |
unsigned blocks; |
fat_dentry_t *d; |
block_t *b; |
fibril_mutex_lock(&parentp->idx->lock); |
bs = block_bb_get(parentp->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
blocks = parentp->size / bps; |
for (i = 0; i < blocks; i++) { |
b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE); |
for (j = 0; j < dps; j++) { |
d = ((fat_dentry_t *)b->data) + j; |
switch (fat_classify_dentry(d)) { |
case FAT_DENTRY_SKIP: |
case FAT_DENTRY_FREE: |
continue; |
case FAT_DENTRY_LAST: |
block_put(b); |
fibril_mutex_unlock(&parentp->idx->lock); |
return NULL; |
default: |
case FAT_DENTRY_VALID: |
fat_dentry_name_get(d, name); |
break; |
} |
if (fat_dentry_namecmp(name, component) == 0) { |
/* hit */ |
fat_node_t *nodep; |
/* |
* Assume tree hierarchy for locking. We |
* already have the parent and now we are going |
* to lock the child. Never lock in the oposite |
* order. |
*/ |
fat_idx_t *idx = fat_idx_get_by_pos( |
parentp->idx->dev_handle, parentp->firstc, |
i * dps + j); |
fibril_mutex_unlock(&parentp->idx->lock); |
if (!idx) { |
/* |
* Can happen if memory is low or if we |
* run out of 32-bit indices. |
*/ |
block_put(b); |
return NULL; |
} |
nodep = fat_node_get_core(idx); |
fibril_mutex_unlock(&idx->lock); |
block_put(b); |
return FS_NODE(nodep); |
} |
} |
block_put(b); |
} |
fibril_mutex_unlock(&parentp->idx->lock); |
return NULL; |
} |
fs_index_t fat_index_get(fs_node_t *fn) |
{ |
return FAT_NODE(fn)->idx->index; |
} |
size_t fat_size_get(fs_node_t *fn) |
{ |
return FAT_NODE(fn)->size; |
} |
unsigned fat_lnkcnt_get(fs_node_t *fn) |
{ |
return FAT_NODE(fn)->lnkcnt; |
} |
bool fat_has_children(fs_node_t *fn) |
{ |
fat_bs_t *bs; |
fat_node_t *nodep = FAT_NODE(fn); |
unsigned bps; |
unsigned dps; |
unsigned blocks; |
block_t *b; |
unsigned i, j; |
if (nodep->type != FAT_DIRECTORY) |
return false; |
fibril_mutex_lock(&nodep->idx->lock); |
bs = block_bb_get(nodep->idx->dev_handle); |
bps = uint16_t_le2host(bs->bps); |
dps = bps / sizeof(fat_dentry_t); |
blocks = nodep->size / bps; |
for (i = 0; i < blocks; i++) { |
fat_dentry_t *d; |
b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE); |
for (j = 0; j < dps; j++) { |
d = ((fat_dentry_t *)b->data) + j; |
switch (fat_classify_dentry(d)) { |
case FAT_DENTRY_SKIP: |
case FAT_DENTRY_FREE: |
continue; |
case FAT_DENTRY_LAST: |
block_put(b); |
fibril_mutex_unlock(&nodep->idx->lock); |
return false; |
default: |
case FAT_DENTRY_VALID: |
block_put(b); |
fibril_mutex_unlock(&nodep->idx->lock); |
return true; |
} |
block_put(b); |
fibril_mutex_unlock(&nodep->idx->lock); |
return true; |
} |
block_put(b); |
} |
fibril_mutex_unlock(&nodep->idx->lock); |
return false; |
} |
fs_node_t *fat_root_get(dev_handle_t dev_handle) |
{ |
return fat_node_get(dev_handle, 0); |
} |
char fat_plb_get_char(unsigned pos) |
{ |
return fat_reg.plb_ro[pos % PLB_SIZE]; |
} |
bool fat_is_directory(fs_node_t *fn) |
{ |
return FAT_NODE(fn)->type == FAT_DIRECTORY; |
} |
bool fat_is_file(fs_node_t *fn) |
{ |
return FAT_NODE(fn)->type == FAT_FILE; |
} |
/** libfs operations */ |
libfs_ops_t fat_libfs_ops = { |
.match = fat_match, |
.node_get = fat_node_get, |
.node_put = fat_node_put, |
.create = fat_create_node, |
.destroy = fat_destroy_node, |
.link = fat_link, |
.unlink = fat_unlink, |
.index_get = fat_index_get, |
.size_get = fat_size_get, |
.lnkcnt_get = fat_lnkcnt_get, |
.has_children = fat_has_children, |
.root_get = fat_root_get, |
.plb_get_char = fat_plb_get_char, |
.is_directory = fat_is_directory, |
.is_file = fat_is_file |
}; |
/* |
* VFS operations. |
*/ |
void fat_mounted(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request); |
enum cache_mode cmode; |
fat_bs_t *bs; |
uint16_t bps; |
uint16_t rde; |
int rc; |
/* accept the mount options */ |
ipc_callid_t callid; |
size_t size; |
if (!ipc_data_write_receive(&callid, &size)) { |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
char *opts = malloc(size + 1); |
if (!opts) { |
ipc_answer_0(callid, ENOMEM); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
ipcarg_t retval = ipc_data_write_finalize(callid, opts, size); |
if (retval != EOK) { |
ipc_answer_0(rid, retval); |
free(opts); |
return; |
} |
opts[size] = '\0'; |
/* Check for option enabling write through. */ |
if (str_cmp(opts, "wtcache") == 0) |
cmode = CACHE_MODE_WT; |
else |
cmode = CACHE_MODE_WB; |
/* initialize libblock */ |
rc = block_init(dev_handle, BS_SIZE); |
if (rc != EOK) { |
ipc_answer_0(rid, rc); |
return; |
} |
/* prepare the boot block */ |
rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE); |
if (rc != EOK) { |
block_fini(dev_handle); |
ipc_answer_0(rid, rc); |
return; |
} |
/* get the buffer with the boot sector */ |
bs = block_bb_get(dev_handle); |
/* Read the number of root directory entries. */ |
bps = uint16_t_le2host(bs->bps); |
rde = uint16_t_le2host(bs->root_ent_max); |
if (bps != BS_SIZE) { |
block_fini(dev_handle); |
ipc_answer_0(rid, ENOTSUP); |
return; |
} |
/* Initialize the block cache */ |
rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode); |
if (rc != EOK) { |
block_fini(dev_handle); |
ipc_answer_0(rid, rc); |
return; |
} |
rc = fat_idx_init_by_dev_handle(dev_handle); |
if (rc != EOK) { |
block_fini(dev_handle); |
ipc_answer_0(rid, rc); |
return; |
} |
/* 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; |
} |
fs_node_initialize(rfn); |
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); |
return; |
} |
fat_node_initialize(rootp); |
fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0); |
if (!ridxp) { |
free(rfn); |
free(rootp); |
block_fini(dev_handle); |
fat_idx_fini_by_dev_handle(dev_handle); |
ipc_answer_0(rid, ENOMEM); |
return; |
} |
assert(ridxp->index == 0); |
/* ridxp->lock held */ |
rootp->type = FAT_DIRECTORY; |
rootp->firstc = FAT_CLST_ROOT; |
rootp->refcnt = 1; |
rootp->lnkcnt = 0; /* FS root is not linked */ |
rootp->size = rde * sizeof(fat_dentry_t); |
rootp->idx = ridxp; |
ridxp->nodep = rootp; |
rootp->bp = rfn; |
rfn->data = rootp; |
fibril_mutex_unlock(&ridxp->lock); |
ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt); |
} |
void fat_mount(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
} |
void fat_lookup(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
} |
void fat_read(ipc_callid_t rid, ipc_call_t *request) |
{ |
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_bs_t *bs; |
uint16_t bps; |
size_t bytes; |
block_t *b; |
if (!fn) { |
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); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
return; |
} |
bs = block_bb_get(dev_handle); |
bps = uint16_t_le2host(bs->bps); |
if (nodep->type == FAT_FILE) { |
/* |
* Our strategy for regular file reads is to read one block at |
* most and make use of the possibility to return less data than |
* requested. This keeps the code very simple. |
*/ |
if (pos >= nodep->size) { |
/* reading beyond the EOF */ |
bytes = 0; |
(void) ipc_data_read_finalize(callid, NULL, 0); |
} else { |
bytes = min(len, bps - pos % bps); |
bytes = min(bytes, nodep->size - pos); |
b = fat_block_get(bs, nodep, pos / bps, |
BLOCK_FLAGS_NONE); |
(void) ipc_data_read_finalize(callid, b->data + pos % bps, |
bytes); |
block_put(b); |
} |
} else { |
unsigned bnum; |
off_t spos = pos; |
char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1]; |
fat_dentry_t *d; |
assert(nodep->type == FAT_DIRECTORY); |
assert(nodep->size % bps == 0); |
assert(bps % sizeof(fat_dentry_t) == 0); |
/* |
* Our strategy for readdir() is to use the position pointer as |
* an index into the array of all dentries. On entry, it points |
* to the first unread dentry. If we skip any dentries, we bump |
* the position pointer accordingly. |
*/ |
bnum = (pos * sizeof(fat_dentry_t)) / bps; |
while (bnum < nodep->size / bps) { |
off_t o; |
b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE); |
for (o = pos % (bps / sizeof(fat_dentry_t)); |
o < bps / sizeof(fat_dentry_t); |
o++, pos++) { |
d = ((fat_dentry_t *)b->data) + o; |
switch (fat_classify_dentry(d)) { |
case FAT_DENTRY_SKIP: |
case FAT_DENTRY_FREE: |
continue; |
case FAT_DENTRY_LAST: |
block_put(b); |
goto miss; |
default: |
case FAT_DENTRY_VALID: |
fat_dentry_name_get(d, name); |
block_put(b); |
goto hit; |
} |
} |
block_put(b); |
bnum++; |
} |
miss: |
fat_node_put(fn); |
ipc_answer_0(callid, ENOENT); |
ipc_answer_1(rid, ENOENT, 0); |
return; |
hit: |
(void) ipc_data_read_finalize(callid, name, str_size(name) + 1); |
bytes = (pos - spos) + 1; |
} |
fat_node_put(fn); |
ipc_answer_1(rid, EOK, (ipcarg_t)bytes); |
} |
void fat_write(ipc_callid_t rid, ipc_call_t *request) |
{ |
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_bs_t *bs; |
size_t bytes; |
block_t *b; |
uint16_t bps; |
unsigned spc; |
unsigned bpc; /* bytes per cluster */ |
off_t boundary; |
int flags = BLOCK_FLAGS_NONE; |
if (!fn) { |
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); |
ipc_answer_0(callid, EINVAL); |
ipc_answer_0(rid, EINVAL); |
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, |
* but this one greatly simplifies fat_write(). Note that we can afford |
* to do this because the client must be ready to handle the return |
* value signalizing a smaller number of bytes written. |
*/ |
bytes = min(len, bps - pos % bps); |
if (bytes == bps) |
flags |= BLOCK_FLAGS_NOREAD; |
boundary = ROUND_UP(nodep->size, bpc); |
if (pos < boundary) { |
/* |
* This is the easier case - we are either overwriting already |
* existing contents or writing behind the EOF, but still within |
* the limits of the last cluster. The node size may grow to the |
* next block size boundary. |
*/ |
fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos); |
b = fat_block_get(bs, nodep, pos / bps, flags); |
(void) ipc_data_write_finalize(callid, b->data + pos % bps, |
bytes); |
b->dirty = true; /* need to sync block */ |
block_put(b); |
if (pos + bytes > nodep->size) { |
nodep->size = pos + bytes; |
nodep->dirty = true; /* need to sync node */ |
} |
ipc_answer_2(rid, EOK, bytes, nodep->size); |
fat_node_put(fn); |
return; |
} else { |
/* |
* This is the more difficult case. We must allocate new |
* clusters for the node and zero them out. |
*/ |
int status; |
unsigned nclsts; |
fat_cluster_t mcl, lcl; |
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); |
if (status != EOK) { |
/* could not allocate a chain of nclsts clusters */ |
fat_node_put(fn); |
ipc_answer_0(callid, status); |
ipc_answer_0(rid, status); |
return; |
} |
/* zero fill any gaps */ |
fat_fill_gap(bs, nodep, mcl, pos); |
b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc, |
flags); |
(void) ipc_data_write_finalize(callid, b->data + pos % bps, |
bytes); |
b->dirty = true; /* need to sync block */ |
block_put(b); |
/* |
* Append the cluster chain starting in mcl to the end of the |
* node's cluster chain. |
*/ |
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(fn); |
return; |
} |
} |
void fat_truncate(ipc_callid_t rid, ipc_call_t *request) |
{ |
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_bs_t *bs; |
uint16_t bps; |
uint8_t spc; |
unsigned bpc; /* bytes per cluster */ |
int rc; |
if (!fn) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
nodep = FAT_NODE(fn); |
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) { |
/* |
* 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 node will be shrunk, clusters will be deallocated. |
*/ |
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(fn); |
ipc_answer_0(rid, rc); |
return; |
} |
void fat_close(ipc_callid_t rid, ipc_call_t *request) |
{ |
ipc_answer_0(rid, EOK); |
} |
void fat_destroy(ipc_callid_t rid, ipc_call_t *request) |
{ |
dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request); |
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) { |
ipc_answer_0(rid, ENOENT); |
return; |
} |
rc = fat_destroy_node(fn); |
ipc_answer_0(rid, rc); |
} |
void fat_open_node(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
} |
void fat_stat(ipc_callid_t rid, ipc_call_t *request) |
{ |
libfs_stat(&fat_libfs_ops, fat_reg.fs_handle, rid, request); |
} |
void fat_sync(ipc_callid_t rid, ipc_call_t *request) |
{ |
/* Dummy implementation */ |
ipc_answer_0(rid, EOK); |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/fat/fat.h |
---|
0,0 → 1,233 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef FAT_FAT_H_ |
#define FAT_FAT_H_ |
#include "fat_fat.h" |
#include <ipc/ipc.h> |
#include <fibril_sync.h> |
#include <libfs.h> |
#include <atomic.h> |
#include <sys/types.h> |
#include <bool.h> |
#include "../../vfs/vfs.h" |
#ifndef dprintf |
#define dprintf(...) printf(__VA_ARGS__) |
#endif |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
#define BS_BLOCK 0 |
#define BS_SIZE 512 |
typedef struct fat_bs { |
uint8_t ji[3]; /**< Jump instruction. */ |
uint8_t oem_name[8]; |
/* BIOS Parameter Block */ |
uint16_t bps; /**< Bytes per sector. */ |
uint8_t spc; /**< Sectors per cluster. */ |
uint16_t rscnt; /**< Reserved sector count. */ |
uint8_t fatcnt; /**< Number of FATs. */ |
uint16_t root_ent_max; /**< Maximum number of root directory |
entries. */ |
uint16_t totsec16; /**< Total sectors. 16-bit version. */ |
uint8_t mdesc; /**< Media descriptor. */ |
uint16_t sec_per_fat; /**< Sectors per FAT12/FAT16. */ |
uint16_t sec_per_track; /**< Sectors per track. */ |
uint16_t headcnt; /**< Number of heads. */ |
uint32_t hidden_sec; /**< Hidden sectors. */ |
uint32_t totsec32; /**< Total sectors. 32-bit version. */ |
union { |
struct { |
/* FAT12/FAT16 only: Extended BIOS Parameter Block */ |
/** Physical drive number. */ |
uint8_t pdn; |
uint8_t reserved; |
/** Extended boot signature. */ |
uint8_t ebs; |
/** Serial number. */ |
uint32_t id; |
/** Volume label. */ |
uint8_t label[11]; |
/** FAT type. */ |
uint8_t type[8]; |
/** Boot code. */ |
uint8_t boot_code[448]; |
/** Boot sector signature. */ |
uint16_t signature; |
} __attribute__ ((packed)); |
struct { |
/* FAT32 only */ |
/** Sectors per FAT. */ |
uint32_t sectors_per_fat; |
/** FAT flags. */ |
uint16_t flags; |
/** Version. */ |
uint16_t version; |
/** Cluster number of root directory. */ |
uint32_t root_cluster; |
/** Sector number of file system information sector. */ |
uint16_t fsinfo_sec; |
/** Sector number of boot sector copy. */ |
uint16_t bscopy_sec; |
uint8_t reserved1[12]; |
/** Physical drive number. */ |
uint8_t pdn; |
uint8_t reserved2; |
/** Extended boot signature. */ |
uint8_t ebs; |
/** Serial number. */ |
uint32_t id; |
/** Volume label. */ |
uint8_t label[11]; |
/** FAT type. */ |
uint8_t type[8]; |
/** Boot code. */ |
uint8_t boot_code[420]; |
/** Signature. */ |
uint16_t signature; |
} __attribute__ ((packed)); |
}; |
} __attribute__ ((packed)) fat_bs_t; |
typedef enum { |
FAT_INVALID, |
FAT_DIRECTORY, |
FAT_FILE |
} fat_node_type_t; |
struct fat_node; |
/** FAT index structure. |
* |
* This structure exists to help us to overcome certain limitations of the FAT |
* file system design. The problem with FAT is that it is hard to find |
* an entity which could represent a VFS index. There are two candidates: |
* |
* a) number of the node's first cluster |
* b) the pair of the parent directory's first cluster and the dentry index |
* within the parent directory |
* |
* We need VFS indices to be: |
* A) unique |
* B) stable in time, at least until the next mount |
* |
* Unfortunately a) does not meet the A) criterion because zero-length files |
* will have the first cluster field cleared. And b) does not meet the B) |
* criterion because unlink() and rename() will both free up the original |
* dentry, which contains all the essential info about the file. |
* |
* Therefore, a completely opaque indices are used and the FAT server maintains |
* a mapping between them and otherwise nice b) variant. On rename(), the VFS |
* index stays unaltered, while the internal FAT "physical tree address" |
* changes. The unlink case is also handled this way thanks to an in-core node |
* pointer embedded in the index structure. |
*/ |
typedef struct { |
/** Used indices (position) hash table link. */ |
link_t uph_link; |
/** Used indices (index) hash table link. */ |
link_t uih_link; |
fibril_mutex_t lock; |
dev_handle_t dev_handle; |
fs_index_t index; |
/** |
* Parent node's first cluster. |
* Zero is used if this node is not linked, in which case nodep must |
* contain a pointer to the in-core node structure. |
* One is used when the parent is the root directory. |
*/ |
fat_cluster_t pfc; |
/** Directory entry index within the parent node. */ |
unsigned pdi; |
/** Pointer to in-core node instance. */ |
struct fat_node *nodep; |
} fat_idx_t; |
/** FAT in-core node. */ |
typedef struct fat_node { |
/** Back pointer to the FS node. */ |
fs_node_t *bp; |
fibril_mutex_t lock; |
fat_node_type_t type; |
fat_idx_t *idx; |
/** |
* Node's first cluster. |
* Zero is used for zero-length nodes. |
* One is used to mark root directory. |
*/ |
fat_cluster_t firstc; |
/** FAT in-core node free list link. */ |
link_t ffn_link; |
size_t size; |
unsigned lnkcnt; |
unsigned refcnt; |
bool dirty; |
} fat_node_t; |
extern fs_reg_t fat_reg; |
extern void fat_mounted(ipc_callid_t, ipc_call_t *); |
extern void fat_mount(ipc_callid_t, ipc_call_t *); |
extern void fat_lookup(ipc_callid_t, ipc_call_t *); |
extern void fat_read(ipc_callid_t, ipc_call_t *); |
extern void fat_write(ipc_callid_t, ipc_call_t *); |
extern void fat_truncate(ipc_callid_t, ipc_call_t *); |
extern void fat_stat(ipc_callid_t, ipc_call_t *); |
extern void fat_close(ipc_callid_t, ipc_call_t *); |
extern void fat_destroy(ipc_callid_t, ipc_call_t *); |
extern void fat_open_node(ipc_callid_t, ipc_call_t *); |
extern void fat_stat(ipc_callid_t, ipc_call_t *); |
extern void fat_sync(ipc_callid_t, ipc_call_t *); |
extern fat_idx_t *fat_idx_get_new(dev_handle_t); |
extern fat_idx_t *fat_idx_get_by_pos(dev_handle_t, fat_cluster_t, unsigned); |
extern fat_idx_t *fat_idx_get_by_index(dev_handle_t, fs_index_t); |
extern void fat_idx_destroy(fat_idx_t *); |
extern void fat_idx_hashin(fat_idx_t *); |
extern void fat_idx_hashout(fat_idx_t *); |
extern int fat_idx_init(void); |
extern void fat_idx_fini(void); |
extern int fat_idx_init_by_dev_handle(dev_handle_t); |
extern void fat_idx_fini_by_dev_handle(dev_handle_t); |
#endif |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/fat/fat.c |
---|
0,0 → 1,171 |
/* |
* Copyright (c) 2006 Martin Decky |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file fat.c |
* @brief FAT file system driver for HelenOS. |
*/ |
#include "fat.h" |
#include <ipc/ipc.h> |
#include <ipc/services.h> |
#include <async.h> |
#include <errno.h> |
#include <unistd.h> |
#include <stdio.h> |
#include <libfs.h> |
#include "../../vfs/vfs.h" |
vfs_info_t fat_vfs_info = { |
.name = "fat", |
}; |
fs_reg_t fat_reg; |
/** |
* This connection fibril processes VFS requests from VFS. |
* |
* In order to support simultaneous VFS requests, our design is as follows. |
* The connection fibril accepts VFS requests from VFS. If there is only one |
* instance of the fibril, VFS will need to serialize all VFS requests it sends |
* to FAT. To overcome this bottleneck, VFS can send FAT the IPC_M_CONNECT_ME_TO |
* call. In that case, a new connection fibril will be created, which in turn |
* will accept the call. Thus, a new phone will be opened for VFS. |
* |
* There are few issues with this arrangement. First, VFS can run out of |
* available phones. In that case, VFS can close some other phones or use one |
* phone for more serialized requests. Similarily, FAT can refuse to duplicate |
* the connection. VFS should then just make use of already existing phones and |
* route its requests through them. To avoid paying the fibril creation price |
* upon each request, FAT might want to keep the connections open after the |
* request has been completed. |
*/ |
static void fat_connection(ipc_callid_t iid, ipc_call_t *icall) |
{ |
if (iid) { |
/* |
* This only happens for connections opened by |
* IPC_M_CONNECT_ME_TO calls as opposed to callback connections |
* created by IPC_M_CONNECT_TO_ME. |
*/ |
ipc_answer_0(iid, EOK); |
} |
dprintf("VFS-FAT connection established.\n"); |
while (1) { |
ipc_callid_t callid; |
ipc_call_t call; |
callid = async_get_call(&call); |
switch (IPC_GET_METHOD(call)) { |
case IPC_M_PHONE_HUNGUP: |
return; |
case VFS_OUT_MOUNTED: |
fat_mounted(callid, &call); |
break; |
case VFS_OUT_MOUNT: |
fat_mount(callid, &call); |
break; |
case VFS_OUT_LOOKUP: |
fat_lookup(callid, &call); |
break; |
case VFS_OUT_READ: |
fat_read(callid, &call); |
break; |
case VFS_OUT_WRITE: |
fat_write(callid, &call); |
break; |
case VFS_OUT_TRUNCATE: |
fat_truncate(callid, &call); |
break; |
case VFS_OUT_STAT: |
fat_stat(callid, &call); |
break; |
case VFS_OUT_CLOSE: |
fat_close(callid, &call); |
break; |
case VFS_OUT_DESTROY: |
fat_destroy(callid, &call); |
break; |
case VFS_OUT_OPEN_NODE: |
fat_open_node(callid, &call); |
break; |
case VFS_OUT_SYNC: |
fat_sync(callid, &call); |
break; |
default: |
ipc_answer_0(callid, ENOTSUP); |
break; |
} |
} |
} |
int main(int argc, char **argv) |
{ |
int vfs_phone; |
int rc; |
printf("fat: HelenOS FAT file system server.\n"); |
rc = fat_idx_init(); |
if (rc != EOK) |
goto err; |
vfs_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_VFS, 0, 0); |
if (vfs_phone < EOK) { |
printf("fat: failed to connect to VFS\n"); |
return -1; |
} |
rc = fs_register(vfs_phone, &fat_reg, &fat_vfs_info, fat_connection); |
if (rc != EOK) { |
fat_idx_fini(); |
goto err; |
} |
dprintf("FAT filesystem registered, fs_handle=%d.\n", |
fat_reg.fs_handle); |
async_manager(); |
/* not reached */ |
return 0; |
err: |
printf("Failed to register the FAT file system (%d)\n", rc); |
return rc; |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/fat/fat_idx.c |
---|
0,0 → 1,556 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file fat_idx.c |
* @brief Layer for translating FAT entities to VFS node indices. |
*/ |
#include "fat.h" |
#include "../../vfs/vfs.h" |
#include <errno.h> |
#include <string.h> |
#include <adt/hash_table.h> |
#include <adt/list.h> |
#include <assert.h> |
#include <fibril_sync.h> |
/** Each instance of this type describes one interval of freed VFS indices. */ |
typedef struct { |
link_t link; |
fs_index_t first; |
fs_index_t last; |
} freed_t; |
/** |
* Each instance of this type describes state of all VFS indices that |
* are currently unused. |
*/ |
typedef struct { |
link_t link; |
dev_handle_t dev_handle; |
/** Next unassigned index. */ |
fs_index_t next; |
/** Number of remaining unassigned indices. */ |
uint64_t remaining; |
/** Sorted list of intervals of freed indices. */ |
link_t freed_head; |
} unused_t; |
/** Mutex protecting the list of unused structures. */ |
static FIBRIL_MUTEX_INITIALIZE(unused_lock); |
/** List of unused structures. */ |
static LIST_INITIALIZE(unused_head); |
static void unused_initialize(unused_t *u, dev_handle_t dev_handle) |
{ |
link_initialize(&u->link); |
u->dev_handle = dev_handle; |
u->next = 0; |
u->remaining = ((uint64_t)((fs_index_t)-1)) + 1; |
list_initialize(&u->freed_head); |
} |
static unused_t *unused_find(dev_handle_t dev_handle, bool lock) |
{ |
unused_t *u; |
link_t *l; |
if (lock) |
fibril_mutex_lock(&unused_lock); |
for (l = unused_head.next; l != &unused_head; l = l->next) { |
u = list_get_instance(l, unused_t, link); |
if (u->dev_handle == dev_handle) |
return u; |
} |
if (lock) |
fibril_mutex_unlock(&unused_lock); |
return NULL; |
} |
/** Mutex protecting the up_hash and ui_hash. */ |
static FIBRIL_MUTEX_INITIALIZE(used_lock); |
/** |
* Global hash table of all used fat_idx_t structures. |
* The index structures are hashed by the dev_handle, parent node's first |
* cluster and index within the parent directory. |
*/ |
static hash_table_t up_hash; |
#define UPH_BUCKETS_LOG 12 |
#define UPH_BUCKETS (1 << UPH_BUCKETS_LOG) |
#define UPH_DH_KEY 0 |
#define UPH_PFC_KEY 1 |
#define UPH_PDI_KEY 2 |
static hash_index_t pos_hash(unsigned long key[]) |
{ |
dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
hash_index_t h; |
/* |
* The least significant half of all bits are the least significant bits |
* of the parent node's first cluster. |
* |
* The least significant half of the most significant half of all bits |
* are the least significant bits of the node's dentry index within the |
* parent directory node. |
* |
* The most significant half of the most significant half of all bits |
* are the least significant bits of the device handle. |
*/ |
h = pfc & ((1 << (UPH_BUCKETS_LOG / 2)) - 1); |
h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
(UPH_BUCKETS_LOG / 2); |
h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) << |
(3 * (UPH_BUCKETS_LOG / 4)); |
return h; |
} |
static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY]; |
fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY]; |
unsigned pdi = (unsigned)key[UPH_PDI_KEY]; |
fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link); |
return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) && |
(pdi == fidx->pdi); |
} |
static void pos_remove_callback(link_t *item) |
{ |
/* nothing to do */ |
} |
static hash_table_operations_t uph_ops = { |
.hash = pos_hash, |
.compare = pos_compare, |
.remove_callback = pos_remove_callback, |
}; |
/** |
* Global hash table of all used fat_idx_t structures. |
* The index structures are hashed by the dev_handle and index. |
*/ |
static hash_table_t ui_hash; |
#define UIH_BUCKETS_LOG 12 |
#define UIH_BUCKETS (1 << UIH_BUCKETS_LOG) |
#define UIH_DH_KEY 0 |
#define UIH_INDEX_KEY 1 |
static hash_index_t idx_hash(unsigned long key[]) |
{ |
dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
hash_index_t h; |
h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1); |
h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) << |
(UIH_BUCKETS_LOG / 2); |
return h; |
} |
static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item) |
{ |
dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY]; |
fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY]; |
fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link); |
return (dev_handle == fidx->dev_handle) && (index == fidx->index); |
} |
static void idx_remove_callback(link_t *item) |
{ |
/* nothing to do */ |
} |
static hash_table_operations_t uih_ops = { |
.hash = idx_hash, |
.compare = idx_compare, |
.remove_callback = idx_remove_callback, |
}; |
/** Allocate a VFS index which is not currently in use. */ |
static bool fat_index_alloc(dev_handle_t dev_handle, fs_index_t *index) |
{ |
unused_t *u; |
assert(index); |
u = unused_find(dev_handle, true); |
if (!u) |
return false; |
if (list_empty(&u->freed_head)) { |
if (u->remaining) { |
/* |
* There are no freed indices, allocate one directly |
* from the counter. |
*/ |
*index = u->next++; |
--u->remaining; |
fibril_mutex_unlock(&unused_lock); |
return true; |
} |
} else { |
/* There are some freed indices which we can reuse. */ |
freed_t *f = list_get_instance(u->freed_head.next, freed_t, |
link); |
*index = f->first; |
if (f->first++ == f->last) { |
/* Destroy the interval. */ |
list_remove(&f->link); |
free(f); |
} |
fibril_mutex_unlock(&unused_lock); |
return true; |
} |
/* |
* We ran out of indices, which is extremely unlikely with FAT16, but |
* theoretically still possible (e.g. too many open unlinked nodes or |
* too many zero-sized nodes). |
*/ |
fibril_mutex_unlock(&unused_lock); |
return false; |
} |
/** If possible, coalesce two intervals of freed indices. */ |
static void try_coalesce_intervals(link_t *l, link_t *r, link_t *cur) |
{ |
freed_t *fl = list_get_instance(l, freed_t, link); |
freed_t *fr = list_get_instance(r, freed_t, link); |
if (fl->last + 1 == fr->first) { |
if (cur == l) { |
fl->last = fr->last; |
list_remove(r); |
free(r); |
} else { |
fr->first = fl->first; |
list_remove(l); |
free(l); |
} |
} |
} |
/** Free a VFS index, which is no longer in use. */ |
static void fat_index_free(dev_handle_t dev_handle, fs_index_t index) |
{ |
unused_t *u; |
u = unused_find(dev_handle, true); |
assert(u); |
if (u->next == index + 1) { |
/* The index can be returned directly to the counter. */ |
u->next--; |
u->remaining++; |
} else { |
/* |
* The index must be returned either to an existing freed |
* interval or a new interval must be created. |
*/ |
link_t *lnk; |
freed_t *n; |
for (lnk = u->freed_head.next; lnk != &u->freed_head; |
lnk = lnk->next) { |
freed_t *f = list_get_instance(lnk, freed_t, link); |
if (f->first == index + 1) { |
f->first--; |
if (lnk->prev != &u->freed_head) |
try_coalesce_intervals(lnk->prev, lnk, |
lnk); |
fibril_mutex_unlock(&unused_lock); |
return; |
} |
if (f->last == index - 1) { |
f->last++; |
if (lnk->next != &u->freed_head) |
try_coalesce_intervals(lnk, lnk->next, |
lnk); |
fibril_mutex_unlock(&unused_lock); |
return; |
} |
if (index > f->first) { |
n = malloc(sizeof(freed_t)); |
/* TODO: sleep until allocation succeeds */ |
assert(n); |
link_initialize(&n->link); |
n->first = index; |
n->last = index; |
list_insert_before(&n->link, lnk); |
fibril_mutex_unlock(&unused_lock); |
return; |
} |
} |
/* The index will form the last interval. */ |
n = malloc(sizeof(freed_t)); |
/* TODO: sleep until allocation succeeds */ |
assert(n); |
link_initialize(&n->link); |
n->first = index; |
n->last = index; |
list_append(&n->link, &u->freed_head); |
} |
fibril_mutex_unlock(&unused_lock); |
} |
static fat_idx_t *fat_idx_create(dev_handle_t dev_handle) |
{ |
fat_idx_t *fidx; |
fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t)); |
if (!fidx) |
return NULL; |
if (!fat_index_alloc(dev_handle, &fidx->index)) { |
free(fidx); |
return NULL; |
} |
link_initialize(&fidx->uph_link); |
link_initialize(&fidx->uih_link); |
fibril_mutex_initialize(&fidx->lock); |
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; |
fibril_mutex_lock(&used_lock); |
fidx = fat_idx_create(dev_handle); |
if (!fidx) { |
fibril_mutex_unlock(&used_lock); |
return NULL; |
} |
unsigned long ikey[] = { |
[UIH_DH_KEY] = dev_handle, |
[UIH_INDEX_KEY] = fidx->index, |
}; |
hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
fibril_mutex_lock(&fidx->lock); |
fibril_mutex_unlock(&used_lock); |
return fidx; |
} |
fat_idx_t * |
fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi) |
{ |
fat_idx_t *fidx; |
link_t *l; |
unsigned long pkey[] = { |
[UPH_DH_KEY] = dev_handle, |
[UPH_PFC_KEY] = pfc, |
[UPH_PDI_KEY] = pdi, |
}; |
fibril_mutex_lock(&used_lock); |
l = hash_table_find(&up_hash, pkey); |
if (l) { |
fidx = hash_table_get_instance(l, fat_idx_t, uph_link); |
} else { |
fidx = fat_idx_create(dev_handle); |
if (!fidx) { |
fibril_mutex_unlock(&used_lock); |
return NULL; |
} |
unsigned long ikey[] = { |
[UIH_DH_KEY] = dev_handle, |
[UIH_INDEX_KEY] = fidx->index, |
}; |
fidx->pfc = pfc; |
fidx->pdi = pdi; |
hash_table_insert(&up_hash, pkey, &fidx->uph_link); |
hash_table_insert(&ui_hash, ikey, &fidx->uih_link); |
} |
fibril_mutex_lock(&fidx->lock); |
fibril_mutex_unlock(&used_lock); |
return fidx; |
} |
void fat_idx_hashin(fat_idx_t *idx) |
{ |
unsigned long pkey[] = { |
[UPH_DH_KEY] = idx->dev_handle, |
[UPH_PFC_KEY] = idx->pfc, |
[UPH_PDI_KEY] = idx->pdi, |
}; |
fibril_mutex_lock(&used_lock); |
hash_table_insert(&up_hash, pkey, &idx->uph_link); |
fibril_mutex_unlock(&used_lock); |
} |
void fat_idx_hashout(fat_idx_t *idx) |
{ |
unsigned long pkey[] = { |
[UPH_DH_KEY] = idx->dev_handle, |
[UPH_PFC_KEY] = idx->pfc, |
[UPH_PDI_KEY] = idx->pdi, |
}; |
fibril_mutex_lock(&used_lock); |
hash_table_remove(&up_hash, pkey, 3); |
fibril_mutex_unlock(&used_lock); |
} |
fat_idx_t * |
fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index) |
{ |
fat_idx_t *fidx = NULL; |
link_t *l; |
unsigned long ikey[] = { |
[UIH_DH_KEY] = dev_handle, |
[UIH_INDEX_KEY] = index, |
}; |
fibril_mutex_lock(&used_lock); |
l = hash_table_find(&ui_hash, ikey); |
if (l) { |
fidx = hash_table_get_instance(l, fat_idx_t, uih_link); |
fibril_mutex_lock(&fidx->lock); |
} |
fibril_mutex_unlock(&used_lock); |
return fidx; |
} |
/** Destroy the index structure. |
* |
* @param idx The index structure to be destroyed. |
*/ |
void fat_idx_destroy(fat_idx_t *idx) |
{ |
unsigned long ikey[] = { |
[UIH_DH_KEY] = idx->dev_handle, |
[UIH_INDEX_KEY] = idx->index, |
}; |
assert(idx->pfc == FAT_CLST_RES0); |
fibril_mutex_lock(&used_lock); |
/* |
* Since we can only free unlinked nodes, the index structure is not |
* present in the position hash (uph). We therefore hash it out from |
* the index hash only. |
*/ |
hash_table_remove(&ui_hash, ikey, 2); |
fibril_mutex_unlock(&used_lock); |
/* Release the VFS index. */ |
fat_index_free(idx->dev_handle, idx->index); |
/* Deallocate the structure. */ |
free(idx); |
} |
int fat_idx_init(void) |
{ |
if (!hash_table_create(&up_hash, UPH_BUCKETS, 3, &uph_ops)) |
return ENOMEM; |
if (!hash_table_create(&ui_hash, UIH_BUCKETS, 2, &uih_ops)) { |
hash_table_destroy(&up_hash); |
return ENOMEM; |
} |
return EOK; |
} |
void fat_idx_fini(void) |
{ |
/* We assume the hash tables are empty. */ |
hash_table_destroy(&up_hash); |
hash_table_destroy(&ui_hash); |
} |
int fat_idx_init_by_dev_handle(dev_handle_t dev_handle) |
{ |
unused_t *u; |
int rc = EOK; |
u = (unused_t *) malloc(sizeof(unused_t)); |
if (!u) |
return ENOMEM; |
unused_initialize(u, dev_handle); |
fibril_mutex_lock(&unused_lock); |
if (!unused_find(dev_handle, false)) |
list_append(&u->link, &unused_head); |
else |
rc = EEXIST; |
fibril_mutex_unlock(&unused_lock); |
return rc; |
} |
void fat_idx_fini_by_dev_handle(dev_handle_t dev_handle) |
{ |
unused_t *u; |
u = unused_find(dev_handle, true); |
assert(u); |
list_remove(&u->link); |
fibril_mutex_unlock(&unused_lock); |
while (!list_empty(&u->freed_head)) { |
freed_t *f; |
f = list_get_instance(u->freed_head.next, freed_t, link); |
list_remove(&f->link); |
free(f); |
} |
free(u); |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/fat/fat_fat.c |
---|
0,0 → 1,455 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file fat_fat.c |
* @brief Functions that manipulate the File Allocation Tables. |
*/ |
#include "fat_fat.h" |
#include "fat_dentry.h" |
#include "fat.h" |
#include "../../vfs/vfs.h" |
#include <libfs.h> |
#include <libblock.h> |
#include <errno.h> |
#include <byteorder.h> |
#include <align.h> |
#include <assert.h> |
#include <fibril_sync.h> |
#include <mem.h> |
/** |
* The fat_alloc_lock mutex protects all copies of the File Allocation Table |
* during allocation of clusters. The lock does not have to be held durring |
* deallocation of clusters. |
*/ |
static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock); |
/** Walk the cluster chain. |
* |
* @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 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. |
*/ |
uint16_t |
fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc, |
fat_cluster_t *lastc, uint16_t max_clusters) |
{ |
block_t *b; |
unsigned bps; |
unsigned rscnt; /* block address of the first FAT */ |
uint16_t clusters = 0; |
fat_cluster_t clst = firstc; |
bps = uint16_t_le2host(bs->bps); |
rscnt = uint16_t_le2host(bs->rscnt); |
if (firstc == FAT_CLST_RES0) { |
/* No space allocated to the file. */ |
if (lastc) |
*lastc = firstc; |
return 0; |
} |
while (clst < FAT_CLST_LAST1 && clusters < max_clusters) { |
bn_t fsec; /* sector offset relative to FAT1 */ |
unsigned fidx; /* FAT1 entry index */ |
assert(clst >= FAT_CLST_FIRST); |
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 */ |
b = block_get(dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE); |
clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]); |
assert(clst != FAT_CLST_BAD); |
block_put(b); |
clusters++; |
} |
if (lastc && clst < FAT_CLST_LAST1) |
*lastc = clst; |
return clusters; |
} |
/** Read block from file located on a FAT file system. |
* |
* @param bs Buffer holding the boot sector of the file system. |
* @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 bn Block number. |
* @param flags Flags passed to libblock. |
* |
* @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, |
bn_t bn, int flags) |
{ |
block_t *b; |
unsigned bps; |
unsigned rscnt; /* block address of the first FAT */ |
unsigned rde; |
unsigned rds; /* root directory size */ |
unsigned sf; |
unsigned ssa; /* size of the system area */ |
unsigned clusters, max_clusters; |
fat_cluster_t lastc; |
bps = uint16_t_le2host(bs->bps); |
rscnt = uint16_t_le2host(bs->rscnt); |
rde = uint16_t_le2host(bs->root_ent_max); |
sf = uint16_t_le2host(bs->sec_per_fat); |
rds = (sizeof(fat_dentry_t) * rde) / bps; |
rds += ((sizeof(fat_dentry_t) * rde) % bps != 0); |
ssa = rscnt + bs->fatcnt * sf + rds; |
if (firstc == FAT_CLST_ROOT) { |
/* root directory special case */ |
assert(bn < rds); |
b = block_get(dev_handle, rscnt + bs->fatcnt * sf + bn, flags); |
return b; |
} |
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 + |
bn % bs->spc, flags); |
return b; |
} |
/** Fill the gap between EOF and a new file position. |
* |
* @param bs Buffer holding the boot sector for nodep. |
* @param nodep FAT node with the gap. |
* @param mcl First cluster in an independent cluster chain that will |
* be later appended to the end of the node's own cluster |
* chain. If pos is still in the last allocated cluster, |
* this argument is ignored. |
* @param pos Position in the last node block. |
*/ |
void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos) |
{ |
uint16_t bps; |
unsigned spc; |
block_t *b; |
off_t o, boundary; |
bps = uint16_t_le2host(bs->bps); |
spc = bs->spc; |
boundary = ROUND_UP(nodep->size, bps * spc); |
/* zero out already allocated space */ |
for (o = nodep->size; o < pos && o < boundary; |
o = ALIGN_DOWN(o + bps, bps)) { |
int flags = (o % bps == 0) ? |
BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE; |
b = fat_block_get(bs, nodep, o / bps, flags); |
memset(b->data + o % bps, 0, bps - o % bps); |
b->dirty = true; /* need to sync node */ |
block_put(b); |
} |
if (o >= pos) |
return; |
/* zero out the initial part of the new cluster chain */ |
for (o = boundary; o < pos; o += bps) { |
b = _fat_block_get(bs, nodep->idx->dev_handle, mcl, |
(o - boundary) / bps, BLOCK_FLAGS_NOREAD); |
memset(b->data, 0, min(bps, pos - o)); |
b->dirty = true; /* need to sync node */ |
block_put(b); |
} |
} |
/** 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, |
BLOCK_FLAGS_NONE); |
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 set. |
* @param value Value to set the cluster with. |
*/ |
void |
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; |
uint16_t bps; |
uint16_t rscnt; |
uint16_t sf; |
fat_cluster_t *cp; |
bps = uint16_t_le2host(bs->bps); |
rscnt = uint16_t_le2host(bs->rscnt); |
sf = uint16_t_le2host(bs->sec_per_fat); |
assert(fatno < bs->fatcnt); |
b = block_get(dev_handle, rscnt + sf * fatno + |
(clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE); |
cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t)); |
*cp = host2uint16_t_le(value); |
b->dirty = true; /* need to sync block */ |
block_put(b); |
} |
/** Replay the allocatoin of clusters in all shadow instances of FAT. |
* |
* @param bs Buffer holding the boot sector of the file system. |
* @param dev_handle Device handle of the file system. |
* @param lifo Chain of allocated clusters. |
* @param nclsts Number of clusters in the lifo chain. |
*/ |
void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle, |
fat_cluster_t *lifo, unsigned nclsts) |
{ |
uint8_t fatno; |
unsigned c; |
for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) { |
for (c = 0; c < nclsts; c++) { |
fat_set_cluster(bs, dev_handle, fatno, lifo[c], |
c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]); |
} |
} |
} |
/** Allocate clusters in all copies of FAT. |
* |
* This function will attempt to allocate the requested number of clusters in |
* all instances of 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 dev_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 |
fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts, |
fat_cluster_t *mcl, fat_cluster_t *lcl) |
{ |
uint16_t bps; |
uint16_t rscnt; |
uint16_t sf; |
block_t *blk; |
fat_cluster_t *lifo; /* stack for storing free cluster numbers */ |
unsigned found = 0; /* top of the free cluster number stack */ |
unsigned b, c, cl; |
lifo = (fat_cluster_t *) malloc(nclsts * sizeof(fat_cluster_t)); |
if (!lifo) |
return ENOMEM; |
bps = uint16_t_le2host(bs->bps); |
rscnt = uint16_t_le2host(bs->rscnt); |
sf = uint16_t_le2host(bs->sec_per_fat); |
/* |
* Search FAT1 for unused clusters. |
*/ |
fibril_mutex_lock(&fat_alloc_lock); |
for (b = 0, cl = 0; b < sf; b++) { |
blk = block_get(dev_handle, rscnt + b, BLOCK_FLAGS_NONE); |
for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) { |
fat_cluster_t *clst = (fat_cluster_t *)blk->data + c; |
if (uint16_t_le2host(*clst) == FAT_CLST_RES0) { |
/* |
* The cluster is free. Put it into our stack |
* of found clusters and mark it as non-free. |
*/ |
lifo[found] = cl; |
*clst = (found == 0) ? |
host2uint16_t_le(FAT_CLST_LAST1) : |
host2uint16_t_le(lifo[found - 1]); |
blk->dirty = true; /* need to sync block */ |
if (++found == nclsts) { |
/* we are almost done */ |
block_put(blk); |
/* update the shadow copies of FAT */ |
fat_alloc_shadow_clusters(bs, |
dev_handle, lifo, nclsts); |
*mcl = lifo[found - 1]; |
*lcl = lifo[0]; |
free(lifo); |
fibril_mutex_unlock(&fat_alloc_lock); |
return EOK; |
} |
} |
} |
block_put(blk); |
} |
fibril_mutex_unlock(&fat_alloc_lock); |
/* |
* We could not find enough clusters. Now we need to free the clusters |
* we have allocated so far. |
*/ |
while (found--) { |
fat_set_cluster(bs, dev_handle, FAT1, lifo[found], |
FAT_CLST_RES0); |
} |
free(lifo); |
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) { |
assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD); |
nextc = fat_get_cluster(bs, dev_handle, firstc); |
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 the boot sector of the file system. |
* @param nodep Node representing the file. |
* @param mcl First cluster of the cluster chain to append. |
*/ |
void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl) |
{ |
dev_handle_t dev_handle = nodep->idx->dev_handle; |
fat_cluster_t lcl; |
uint8_t fatno; |
if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, |
(uint16_t) -1) == 0) { |
/* No clusters allocated to the node yet. */ |
nodep->firstc = mcl; |
nodep->dirty = true; /* need to sync node */ |
return; |
} |
for (fatno = FAT1; fatno < bs->fatcnt; fatno++) |
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/arm/uspace/srv/fs/fat/Makefile |
---|
0,0 → 1,86 |
# |
# Copyright (c) 2006 Martin Decky |
# All rights reserved. |
# |
# Redistribution and use in source and binary forms, with or without |
# modification, are permitted provided that the following conditions |
# are met: |
# |
# - Redistributions of source code must retain the above copyright |
# notice, this list of conditions and the following disclaimer. |
# - Redistributions in binary form must reproduce the above copyright |
# notice, this list of conditions and the following disclaimer in the |
# documentation and/or other materials provided with the distribution. |
# - The name of the author may not be used to endorse or promote products |
# derived from this software without specific prior written permission. |
# |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
# |
## Setup toolchain |
# |
LIBC_PREFIX = ../../../lib/libc |
LIBFS_PREFIX = ../../../lib/libfs |
LIBBLOCK_PREFIX = ../../../lib/libblock |
SOFTINT_PREFIX = ../../../lib/softint |
include $(LIBC_PREFIX)/Makefile.toolchain |
CFLAGS += -I $(LIBFS_PREFIX) -I $(LIBBLOCK_PREFIX) |
LIBS = \ |
$(LIBFS_PREFIX)/libfs.a \ |
$(LIBBLOCK_PREFIX)/libblock.a \ |
$(LIBC_PREFIX)/libc.a |
## Sources |
# |
OUTPUT = fat |
SOURCES = \ |
fat.c \ |
fat_ops.c \ |
fat_idx.c \ |
fat_dentry.c \ |
fat_fat.c |
OBJECTS := $(addsuffix .o,$(basename $(SOURCES))) |
.PHONY: all clean depend disasm |
all: $(OUTPUT) $(OUTPUT).disasm |
-include Makefile.depend |
clean: |
-rm -f $(OUTPUT) $(OUTPUT).map $(OUTPUT).disasm Makefile.depend $(OBJECTS) |
depend: |
$(CC) $(DEFS) $(CFLAGS) -M $(SOURCES) > Makefile.depend |
$(OUTPUT): $(OBJECTS) $(LIBS) |
$(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld $(OBJECTS) $(LIBS) $(LFLAGS) -o $@ -Map $(OUTPUT).map |
disasm: $(OUTPUT).disasm |
$(OUTPUT).disasm: $(OUTPUT) |
$(OBJDUMP) -d $< > $@ |
%.o: %.S |
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@ |
%.o: %.s |
$(AS) $(AFLAGS) $< -o $@ |
%.o: %.c |
$(CC) $(DEFS) $(CFLAGS) -c $< -o $@ |
/branches/arm/uspace/srv/fs/fat/fat_dentry.c |
---|
0,0 → 1,243 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
/** |
* @file fat_dentry.c |
* @brief Functions that work with FAT directory entries. |
*/ |
#include "fat_dentry.h" |
#include <ctype.h> |
#include <string.h> |
static bool is_d_char(const char ch) |
{ |
if (isalnum(ch) || ch == '_') |
return true; |
else |
return false; |
} |
/** Compare path component with the name read from the dentry. |
* |
* This function compares the path component with the name read from the dentry. |
* The comparison is case insensitive and tolerates a mismatch on the trailing |
* dot character at the end of the name (i.e. when there is a dot, but no |
* extension). |
* |
* @param name Node name read from the dentry. |
* @param component Path component. |
* |
* @return Zero on match, non-zero otherwise. |
*/ |
int fat_dentry_namecmp(char *name, const char *component) |
{ |
int rc; |
size_t size; |
if (!(rc = stricmp(name, component))) |
return rc; |
if (!str_chr(name, '.')) { |
/* |
* There is no '.' in the name, so we know that there is enough |
* space for appending an extra '.' to name. |
*/ |
size = str_size(name); |
name[size] = '.'; |
name[size + 1] = '\0'; |
rc = stricmp(name, component); |
} |
return rc; |
} |
bool fat_dentry_name_verify(const char *name) |
{ |
unsigned i, dot; |
bool dot_found = false; |
for (i = 0; name[i]; i++) { |
if (name[i] == '.') { |
if (dot_found) { |
return false; |
} else { |
dot_found = true; |
dot = i; |
} |
} else { |
if (!is_d_char(name[i])) |
return false; |
} |
} |
if (dot_found) { |
if (dot > FAT_NAME_LEN) |
return false; |
if (i - dot > FAT_EXT_LEN + 1) |
return false; |
} else { |
if (i > FAT_NAME_LEN) |
return false; |
} |
return true; |
} |
void fat_dentry_name_get(const fat_dentry_t *d, char *buf) |
{ |
unsigned int i; |
for (i = 0; i < FAT_NAME_LEN; i++) { |
if (d->name[i] == FAT_PAD) |
break; |
if (d->name[i] == FAT_DENTRY_E5_ESC) |
*buf++ = 0xe5; |
else { |
if (d->lcase & FAT_LCASE_LOWER_NAME) |
*buf++ = tolower(d->name[i]); |
else |
*buf++ = d->name[i]; |
} |
} |
if (d->ext[0] != FAT_PAD) |
*buf++ = '.'; |
for (i = 0; i < FAT_EXT_LEN; i++) { |
if (d->ext[i] == FAT_PAD) { |
*buf = '\0'; |
return; |
} |
if (d->ext[i] == FAT_DENTRY_E5_ESC) |
*buf++ = 0xe5; |
else { |
if (d->lcase & FAT_LCASE_LOWER_EXT) |
*buf++ = tolower(d->ext[i]); |
else |
*buf++ = d->ext[i]; |
} |
} |
*buf = '\0'; |
} |
void fat_dentry_name_set(fat_dentry_t *d, const char *name) |
{ |
unsigned int i; |
const char fake_ext[] = " "; |
bool lower_name = true; |
bool lower_ext = true; |
for (i = 0; i < FAT_NAME_LEN; i++) { |
switch ((uint8_t) *name) { |
case 0xe5: |
d->name[i] = FAT_DENTRY_E5_ESC; |
name++; |
break; |
case '\0': |
case '.': |
d->name[i] = FAT_PAD; |
break; |
default: |
if (isalpha(*name)) { |
if (!islower(*name)) |
lower_name = false; |
} |
d->name[i] = toupper(*name++); |
break; |
} |
} |
if (*name++ != '.') |
name = fake_ext; |
for (i = 0; i < FAT_EXT_LEN; i++) { |
switch ((uint8_t) *name) { |
case 0xe5: |
d->ext[i] = FAT_DENTRY_E5_ESC; |
name++; |
break; |
case '\0': |
d->ext[i] = FAT_PAD; |
break; |
default: |
if (isalpha(*name)) { |
if (!islower(*name)) |
lower_ext = false; |
} |
d->ext[i] = toupper(*name++); |
break; |
} |
} |
if (lower_name) |
d->lcase |= FAT_LCASE_LOWER_NAME; |
else |
d->lcase &= ~FAT_LCASE_LOWER_NAME; |
if (lower_ext) |
d->lcase |= FAT_LCASE_LOWER_EXT; |
else |
d->lcase &= ~FAT_LCASE_LOWER_EXT; |
} |
fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *d) |
{ |
if (d->attr & FAT_ATTR_VOLLABEL) { |
/* volume label entry */ |
return FAT_DENTRY_SKIP; |
} |
if (d->name[0] == FAT_DENTRY_ERASED) { |
/* not-currently-used entry */ |
return FAT_DENTRY_FREE; |
} |
if (d->name[0] == FAT_DENTRY_UNUSED) { |
/* never used entry */ |
return FAT_DENTRY_LAST; |
} |
if (d->name[0] == FAT_DENTRY_DOT) { |
/* |
* Most likely '.' or '..'. |
* It cannot occur in a regular file name. |
*/ |
return FAT_DENTRY_SKIP; |
} |
return FAT_DENTRY_VALID; |
} |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/fat/fat_dentry.h |
---|
0,0 → 1,99 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef FAT_FAT_DENTRY_H_ |
#define FAT_FAT_DENTRY_H_ |
#include <stdint.h> |
#include <bool.h> |
#define FAT_NAME_LEN 8 |
#define FAT_EXT_LEN 3 |
#define FAT_NAME_DOT ". " |
#define FAT_NAME_DOT_DOT ".. " |
#define FAT_EXT_PAD " " |
#define FAT_ATTR_RDONLY (1 << 0) |
#define FAT_ATTR_VOLLABEL (1 << 3) |
#define FAT_ATTR_SUBDIR (1 << 4) |
#define FAT_LCASE_LOWER_NAME 0x08 |
#define FAT_LCASE_LOWER_EXT 0x10 |
#define FAT_PAD ' ' |
#define FAT_DENTRY_UNUSED 0x00 |
#define FAT_DENTRY_E5_ESC 0x05 |
#define FAT_DENTRY_DOT 0x2e |
#define FAT_DENTRY_ERASED 0xe5 |
typedef enum { |
FAT_DENTRY_SKIP, |
FAT_DENTRY_LAST, |
FAT_DENTRY_FREE, |
FAT_DENTRY_VALID |
} fat_dentry_clsf_t; |
typedef struct { |
uint8_t name[8]; |
uint8_t ext[3]; |
uint8_t attr; |
uint8_t lcase; |
uint8_t ctime_fine; |
uint16_t ctime; |
uint16_t cdate; |
uint16_t adate; |
union { |
uint16_t eaidx; /* FAT12/FAT16 */ |
uint16_t firstc_hi; /* FAT32 */ |
} __attribute__ ((packed)); |
uint16_t mtime; |
uint16_t mdate; |
union { |
uint16_t firstc; /* FAT12/FAT16 */ |
uint16_t firstc_lo; /* FAT32 */ |
} __attribute__ ((packed)); |
uint32_t size; |
} __attribute__ ((packed)) fat_dentry_t; |
extern int fat_dentry_namecmp(char *, const char *); |
extern bool fat_dentry_name_verify(const char *); |
extern void fat_dentry_name_get(const fat_dentry_t *, char *); |
extern void fat_dentry_name_set(fat_dentry_t *, const char *); |
extern fat_dentry_clsf_t fat_classify_dentry(const fat_dentry_t *); |
#endif |
/** |
* @} |
*/ |
/branches/arm/uspace/srv/fs/fat/fat_fat.h |
---|
0,0 → 1,91 |
/* |
* Copyright (c) 2008 Jakub Jermar |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup fs |
* @{ |
*/ |
#ifndef FAT_FAT_FAT_H_ |
#define FAT_FAT_FAT_H_ |
#include "../../vfs/vfs.h" |
#include <stdint.h> |
#include <libblock.h> |
#define FAT1 0 |
#define FAT_CLST_RES0 0x0000 |
#define FAT_CLST_RES1 0x0001 |
#define FAT_CLST_FIRST 0x0002 |
#define FAT_CLST_BAD 0xfff7 |
#define FAT_CLST_LAST1 0xfff8 |
#define FAT_CLST_LAST8 0xffff |
/* internally used to mark root directory's parent */ |
#define FAT_CLST_ROOTPAR FAT_CLST_RES0 |
/* internally used to mark root directory */ |
#define FAT_CLST_ROOT FAT_CLST_RES1 |
/* forward declarations */ |
struct block; |
struct fat_node; |
struct fat_bs; |
typedef uint16_t fat_cluster_t; |
#define fat_clusters_get(bs, dh, fc) \ |
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, flags) \ |
_fat_block_get((bs), (np)->idx->dev_handle, (np)->firstc, (bn), (flags)) |
extern struct block *_fat_block_get(struct fat_bs *, dev_handle_t, |
fat_cluster_t, bn_t, int); |
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 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); |
#endif |
/** |
* @} |
*/ |