Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4580 → Rev 4581

/branches/network/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_MOUNTED:
devfs_mounted(callid, &call);
break;
case VFS_MOUNT:
devfs_mount(callid, &call);
break;
case VFS_LOOKUP:
devfs_lookup(callid, &call);
break;
case VFS_OPEN_NODE:
devfs_open_node(callid, &call);
break;
case VFS_DEVICE:
devfs_device(callid, &call);
break;
case VFS_READ:
devfs_read(callid, &call);
break;
case VFS_WRITE:
devfs_write(callid, &call);
break;
case VFS_TRUNCATE:
devfs_truncate(callid, &call);
break;
case VFS_CLOSE:
devfs_close(callid, &call);
break;
case VFS_SYNC:
devfs_sync(callid, &call);
break;
case VFS_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/network/uspace/srv/fs/devfs/devfs_ops.c
0,0 → 1,513
/*
* 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 "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_DIRECTORY)
ipc_answer_5(rid, EOK, devfs_reg.fs_handle, dev_handle, 0, 0, 0);
else
ipc_answer_0(rid, ENOENT);
} else {
if (lflag & L_FILE) {
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_device(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;
}
fibril_mutex_unlock(&devices_mutex);
ipc_answer_1(rid, EOK, (ipcarg_t) index);
} else
ipc_answer_0(rid, ENOTSUP);
}
 
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/network/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_device(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/network/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/network/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/network/uspace/srv/fs/tmpfs/tmpfs.h
38,12 → 38,15
#include <atomic.h>
#include <sys/types.h>
#include <bool.h>
#include <libadt/hash_table.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,
50,18 → 53,26
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 dh_link; /**< Dentries hash table link. */
struct tmpfs_dentry *sibling;
struct tmpfs_dentry *child;
hash_table_t names; /**< All names linking to this TMPFS node. */
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. */
} tmpfs_dentry_t;
link_t cs_head; /**< Head of child's siblings list. */
} tmpfs_node_t;
 
extern fs_reg_t tmpfs_reg;
 
75,7 → 86,11
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_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_device(ipc_callid_t, ipc_call_t *);
extern void tmpfs_sync(ipc_callid_t, ipc_call_t *);
 
extern bool tmpfs_restore(dev_handle_t);
 
/branches/network/uspace/srv/fs/tmpfs/tmpfs_dump.c
54,8 → 54,8
} __attribute__((packed));
 
static bool
tmpfs_restore_recursion(int dev, off_t *bufpos, size_t *buflen, off_t *pos,
tmpfs_dentry_t *parent)
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;
63,7 → 63,8
do {
char *fname;
tmpfs_dentry_t *node;
fs_node_t *fn;
tmpfs_node_t *nodep;
uint32_t size;
if (block_read(dev, bufpos, buflen, pos, &entry, sizeof(entry),
80,8 → 81,8
if (fname == NULL)
return false;
node = (tmpfs_dentry_t *) ops->create(dev, L_FILE);
if (node == NULL) {
fn = ops->create(dev, L_FILE);
if (fn == NULL) {
free(fname);
return false;
}
88,15 → 89,15
if (block_read(dev, bufpos, buflen, pos, fname,
entry.len, TMPFS_BLOCK_SIZE) != EOK) {
ops->destroy((void *) node);
ops->destroy(fn);
free(fname);
return false;
}
fname[entry.len] = 0;
rc = ops->link((void *) parent, (void *) node, fname);
rc = ops->link(pfn, fn, fname);
if (rc != EOK) {
ops->destroy((void *) node);
ops->destroy(fn);
free(fname);
return false;
}
108,12 → 109,13
size = uint32_t_le2host(size);
node->data = malloc(size);
if (node->data == NULL)
nodep = TMPFS_NODE(fn);
nodep->data = malloc(size);
if (nodep->data == NULL)
return false;
node->size = size;
if (block_read(dev, bufpos, buflen, pos, node->data,
nodep->size = size;
if (block_read(dev, bufpos, buflen, pos, nodep->data,
size, TMPFS_BLOCK_SIZE) != EOK)
return false;
123,8 → 125,8
if (fname == NULL)
return false;
node = (tmpfs_dentry_t *) ops->create(dev, L_DIRECTORY);
if (node == NULL) {
fn = ops->create(dev, L_DIRECTORY);
if (fn == NULL) {
free(fname);
return false;
}
131,15 → 133,15
if (block_read(dev, bufpos, buflen, pos, fname,
entry.len, TMPFS_BLOCK_SIZE) != EOK) {
ops->destroy((void *) node);
ops->destroy(fn);
free(fname);
return false;
}
fname[entry.len] = 0;
 
rc = ops->link((void *) parent, (void *) node, fname);
rc = ops->link(pfn, fn, fname);
if (rc != EOK) {
ops->destroy((void *) node);
ops->destroy(fn);
free(fname);
return false;
}
146,7 → 148,7
free(fname);
if (!tmpfs_restore_recursion(dev, bufpos, buflen, pos,
node))
fn))
return false;
break;
/branches/network/uspace/srv/fs/tmpfs/tmpfs.c
96,6 → 96,8
callid = async_get_call(&call);
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
return;
case VFS_MOUNTED:
tmpfs_mounted(callid, &call);
break;
114,9 → 116,21
case VFS_TRUNCATE:
tmpfs_truncate(callid, &call);
break;
case VFS_CLOSE:
tmpfs_close(callid, &call);
break;
case VFS_DESTROY:
tmpfs_destroy(callid, &call);
break;
case VFS_OPEN_NODE:
tmpfs_open_node(callid, &call);
break;
case VFS_DEVICE:
tmpfs_device(callid, &call);
break;
case VFS_SYNC:
tmpfs_sync(callid, &call);
break;
default:
ipc_answer_0(callid, ENOTSUP);
break;
/branches/network/uspace/srv/fs/tmpfs/tmpfs_ops.c
47,7 → 47,7
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <libadt/hash_table.h>
#include <adt/hash_table.h>
#include <as.h>
#include <libfs.h>
 
54,10 → 54,8
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
 
#define DENTRIES_BUCKETS 256
#define NODES_BUCKETS 256
 
#define NAMES_BUCKETS 4
 
/** All root nodes have index 0. */
#define TMPFS_SOME_ROOT 0
/** Global counter for assigning node indices. Shared by all instances. */
68,36 → 66,36
*/
 
/* Forward declarations of static functions. */
static void *tmpfs_match(void *, const char *);
static void *tmpfs_node_get(dev_handle_t, fs_index_t);
static void tmpfs_node_put(void *);
static void *tmpfs_create_node(dev_handle_t, int);
static int tmpfs_link_node(void *, void *, const char *);
static int tmpfs_unlink_node(void *, void *);
static int tmpfs_destroy_node(void *);
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(void *nodep)
static fs_index_t tmpfs_index_get(fs_node_t *fn)
{
return ((tmpfs_dentry_t *) nodep)->index;
return TMPFS_NODE(fn)->index;
}
 
static size_t tmpfs_size_get(void *nodep)
static size_t tmpfs_size_get(fs_node_t *fn)
{
return ((tmpfs_dentry_t *) nodep)->size;
return TMPFS_NODE(fn)->size;
}
 
static unsigned tmpfs_lnkcnt_get(void *nodep)
static unsigned tmpfs_lnkcnt_get(fs_node_t *fn)
{
return ((tmpfs_dentry_t *) nodep)->lnkcnt;
return TMPFS_NODE(fn)->lnkcnt;
}
 
static bool tmpfs_has_children(void *nodep)
static bool tmpfs_has_children(fs_node_t *fn)
{
return ((tmpfs_dentry_t *) nodep)->child != NULL;
return !list_empty(&TMPFS_NODE(fn)->cs_head);
}
 
static void *tmpfs_root_get(dev_handle_t dev_handle)
static fs_node_t *tmpfs_root_get(dev_handle_t dev_handle)
{
return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT);
}
107,14 → 105,14
return tmpfs_reg.plb_ro[pos % PLB_SIZE];
}
 
static bool tmpfs_is_directory(void *nodep)
static bool tmpfs_is_directory(fs_node_t *fn)
{
return ((tmpfs_dentry_t *) nodep)->type == TMPFS_DIRECTORY;
return TMPFS_NODE(fn)->type == TMPFS_DIRECTORY;
}
 
static bool tmpfs_is_file(void *nodep)
static bool tmpfs_is_file(fs_node_t *fn)
{
return ((tmpfs_dentry_t *) nodep)->type == TMPFS_FILE;
return TMPFS_NODE(fn)->type == TMPFS_FILE;
}
 
/** libfs operations */
136,99 → 134,60
.is_file = tmpfs_is_file
};
 
/** Hash table of all directory entries. */
hash_table_t dentries;
/** Hash table of all TMPFS nodes. */
hash_table_t nodes;
 
#define DENTRIES_KEY_INDEX 0
#define DENTRIES_KEY_DEV 1
#define NODES_KEY_INDEX 0
#define NODES_KEY_DEV 1
 
/* Implementation of hash table interface for the dentries hash table. */
static hash_index_t dentries_hash(unsigned long key[])
/* Implementation of hash table interface for the nodes hash table. */
static hash_index_t nodes_hash(unsigned long key[])
{
return key[DENTRIES_KEY_INDEX] % DENTRIES_BUCKETS;
return key[NODES_KEY_INDEX] % NODES_BUCKETS;
}
 
static int dentries_compare(unsigned long key[], hash_count_t keys,
link_t *item)
static int nodes_compare(unsigned long key[], hash_count_t keys, link_t *item)
{
tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
dh_link);
return (dentry->index == key[DENTRIES_KEY_INDEX] &&
dentry->dev_handle == key[DENTRIES_KEY_DEV]);
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 dentries_remove_callback(link_t *item)
static void nodes_remove_callback(link_t *item)
{
}
 
/** TMPFS dentries hash table operations. */
hash_table_operations_t dentries_ops = {
.hash = dentries_hash,
.compare = dentries_compare,
.remove_callback = dentries_remove_callback
/** TMPFS nodes hash table operations. */
hash_table_operations_t nodes_ops = {
.hash = nodes_hash,
.compare = nodes_compare,
.remove_callback = nodes_remove_callback
};
 
typedef struct {
char *name;
tmpfs_dentry_t *parent;
link_t link;
} tmpfs_name_t;
 
/* Implementation of hash table interface for the names hash table. */
static hash_index_t names_hash(unsigned long *key)
static void tmpfs_node_initialize(tmpfs_node_t *nodep)
{
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
return dentry->index % NAMES_BUCKETS;
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 int names_compare(unsigned long *key, hash_count_t keys, link_t *item)
static void tmpfs_dentry_initialize(tmpfs_dentry_t *dentryp)
{
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) *key;
tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
link);
return dentry == namep->parent;
link_initialize(&dentryp->link);
dentryp->name = NULL;
dentryp->node = NULL;
}
 
static void names_remove_callback(link_t *item)
{
tmpfs_name_t *namep = hash_table_get_instance(item, tmpfs_name_t,
link);
free(namep->name);
free(namep);
}
 
/** TMPFS node names hash table operations. */
static hash_table_operations_t names_ops = {
.hash = names_hash,
.compare = names_compare,
.remove_callback = names_remove_callback
};
 
static void tmpfs_name_initialize(tmpfs_name_t *namep)
{
namep->name = NULL;
namep->parent = NULL;
link_initialize(&namep->link);
}
 
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
{
dentry->index = 0;
dentry->dev_handle = 0;
dentry->sibling = NULL;
dentry->child = NULL;
dentry->type = TMPFS_NONE;
dentry->lnkcnt = 0;
dentry->size = 0;
dentry->data = NULL;
link_initialize(&dentry->dh_link);
return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
&names_ops);
}
 
bool tmpfs_init(void)
{
if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 2, &dentries_ops))
if (!hash_table_create(&nodes, NODES_BUCKETS, 2, &nodes_ops))
return false;
return true;
236,181 → 195,170
 
static bool tmpfs_instance_init(dev_handle_t dev_handle)
{
tmpfs_dentry_t *root;
fs_node_t *rfn;
root = (tmpfs_dentry_t *) tmpfs_create_node(dev_handle, L_DIRECTORY);
if (!root)
rfn = tmpfs_create_node(dev_handle, L_DIRECTORY);
if (!rfn)
return false;
root->lnkcnt = 0; /* FS root is not linked */
TMPFS_NODE(rfn)->lnkcnt = 0; /* FS root is not linked */
return true;
}
 
/** Compare one component of path to a directory entry.
*
* @param parentp Pointer to node from which we descended.
* @param childp Pointer to node to compare the path component with.
* @param component Array of characters holding component name.
*
* @return True on match, false otherwise.
*/
static bool
tmpfs_match_one(tmpfs_dentry_t *parentp, tmpfs_dentry_t *childp,
const char *component)
fs_node_t *tmpfs_match(fs_node_t *pfn, const char *component)
{
unsigned long key = (unsigned long) parentp;
link_t *hlp = hash_table_find(&childp->names, &key);
assert(hlp);
tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
return !str_cmp(namep->name, component);
}
tmpfs_node_t *parentp = TMPFS_NODE(pfn);
link_t *lnk;
 
void *tmpfs_match(void *prnt, const char *component)
{
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
tmpfs_dentry_t *childp = parentp->child;
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);
}
 
while (childp && !tmpfs_match_one(parentp, childp, component))
childp = childp->sibling;
 
return (void *) childp;
return NULL;
}
 
void *
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
fs_node_t *tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
{
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = index,
[DENTRIES_KEY_DEV] = dev_handle
[NODES_KEY_INDEX] = index,
[NODES_KEY_DEV] = dev_handle
};
link_t *lnk = hash_table_find(&dentries, key);
link_t *lnk = hash_table_find(&nodes, key);
if (!lnk)
return NULL;
return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
return FS_NODE(hash_table_get_instance(lnk, tmpfs_node_t, nh_link));
}
 
void tmpfs_node_put(void *node)
void tmpfs_node_put(fs_node_t *fn)
{
/* nothing to do */
}
 
void *tmpfs_create_node(dev_handle_t dev_handle, int lflag)
fs_node_t *tmpfs_create_node(dev_handle_t dev_handle, int lflag)
{
assert((lflag & L_FILE) ^ (lflag & L_DIRECTORY));
 
tmpfs_dentry_t *node = malloc(sizeof(tmpfs_dentry_t));
if (!node)
tmpfs_node_t *nodep = malloc(sizeof(tmpfs_node_t));
if (!nodep)
return NULL;
 
if (!tmpfs_dentry_initialize(node)) {
free(node);
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))
node->index = TMPFS_SOME_ROOT;
nodep->index = TMPFS_SOME_ROOT;
else
node->index = tmpfs_next_index++;
node->dev_handle = dev_handle;
nodep->index = tmpfs_next_index++;
nodep->dev_handle = dev_handle;
if (lflag & L_DIRECTORY)
node->type = TMPFS_DIRECTORY;
nodep->type = TMPFS_DIRECTORY;
else
node->type = TMPFS_FILE;
nodep->type = TMPFS_FILE;
 
/* Insert the new node into the dentry hash table. */
/* Insert the new node into the nodes hash table. */
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = node->index,
[DENTRIES_KEY_DEV] = node->dev_handle
[NODES_KEY_INDEX] = nodep->index,
[NODES_KEY_DEV] = nodep->dev_handle
};
hash_table_insert(&dentries, key, &node->dh_link);
return (void *) node;
hash_table_insert(&nodes, key, &nodep->nh_link);
return FS_NODE(nodep);
}
 
int tmpfs_link_node(void *prnt, void *chld, const char *nm)
int tmpfs_link_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
{
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
tmpfs_dentry_t *childp = (tmpfs_dentry_t *) chld;
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);
 
tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
if (!namep)
/* 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_name_initialize(namep);
tmpfs_dentry_initialize(dentryp);
 
/* Populate and link the new dentry. */
size_t size = str_size(nm);
namep->name = malloc(size + 1);
if (!namep->name) {
free(namep);
dentryp->name = malloc(size + 1);
if (!dentryp->name) {
free(dentryp);
return ENOMEM;
}
str_cpy(namep->name, size + 1, nm);
namep->parent = parentp;
str_cpy(dentryp->name, size + 1, nm);
dentryp->node = childp;
childp->lnkcnt++;
list_append(&dentryp->link, &parentp->cs_head);
 
unsigned long key = (unsigned long) parentp;
hash_table_insert(&childp->names, &key, &namep->link);
 
/* Insert the new node into the namespace. */
if (parentp->child) {
tmpfs_dentry_t *tmp = parentp->child;
while (tmp->sibling)
tmp = tmp->sibling;
tmp->sibling = childp;
} else {
parentp->child = childp;
}
 
return EOK;
}
 
int tmpfs_unlink_node(void *prnt, void *chld)
int tmpfs_unlink_node(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
{
tmpfs_dentry_t *parentp = (tmpfs_dentry_t *)prnt;
tmpfs_dentry_t *childp = (tmpfs_dentry_t *)chld;
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->child)
if (!childp)
return ENOENT;
if ((childp->lnkcnt == 1) && !list_empty(&childp->cs_head))
return ENOTEMPTY;
 
if (parentp->child == childp) {
parentp->child = childp->sibling;
} else {
/* TODO: consider doubly linked list for organizing siblings. */
tmpfs_dentry_t *tmp = parentp->child;
while (tmp->sibling != childp)
tmp = tmp->sibling;
tmp->sibling = childp->sibling;
}
childp->sibling = NULL;
 
unsigned long key = (unsigned long) parentp;
hash_table_remove(&childp->names, &key, 1);
 
list_remove(&dentryp->link);
free(dentryp);
childp->lnkcnt--;
 
return EOK;
}
 
int tmpfs_destroy_node(void *nodep)
int tmpfs_destroy_node(fs_node_t *fn)
{
tmpfs_dentry_t *dentry = (tmpfs_dentry_t *) nodep;
tmpfs_node_t *nodep = TMPFS_NODE(fn);
assert(!dentry->lnkcnt);
assert(!dentry->child);
assert(!dentry->sibling);
assert(!nodep->lnkcnt);
assert(list_empty(&nodep->cs_head));
 
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = dentry->index,
[DENTRIES_KEY_DEV] = dentry->dev_handle
[NODES_KEY_INDEX] = nodep->index,
[NODES_KEY_DEV] = nodep->dev_handle
};
hash_table_remove(&dentries, key, 2);
hash_table_remove(&nodes, key, 2);
 
hash_table_destroy(&dentry->names);
 
if (dentry->type == TMPFS_FILE)
free(dentry->data);
free(dentry);
if (nodep->type == TMPFS_FILE)
free(nodep->data);
free(nodep->bp);
free(nodep);
return EOK;
}
 
446,26 → 394,22
return;
}
 
tmpfs_dentry_t *root = tmpfs_root_get(dev_handle);
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, root->index, root->size,
root->lnkcnt);
ipc_answer_3(rid, EOK, rootp->index, rootp->size,
rootp->lnkcnt);
else
ipc_answer_0(rid, ELIMIT);
} else {
ipc_answer_3(rid, EOK, root->index, root->size, root->lnkcnt);
ipc_answer_3(rid, EOK, rootp->index, rootp->size,
rootp->lnkcnt);
}
}
 
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
{
dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request);
fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
ipc_answer_0(rid, ENOTSUP);
libfs_mount(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
}
 
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
480,20 → 424,20
off_t pos = (off_t)IPC_GET_ARG3(*request);
 
/*
* Lookup the respective dentry.
* Lookup the respective TMPFS node.
*/
link_t *hlp;
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = index,
[DENTRIES_KEY_DEV] = dev_handle,
[NODES_KEY_INDEX] = index,
[NODES_KEY_DEV] = dev_handle,
};
hlp = hash_table_find(&dentries, key);
hlp = hash_table_find(&nodes, key);
if (!hlp) {
ipc_answer_0(rid, ENOENT);
return;
}
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
dh_link);
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
nh_link);
 
/*
* Receive the read request.
507,15 → 451,16
}
 
size_t bytes;
if (dentry->type == TMPFS_FILE) {
bytes = max(0, min(dentry->size - pos, size));
(void) ipc_data_read_finalize(callid, dentry->data + pos,
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;
tmpfs_dentry_t *cur;
assert(dentry->type == TMPFS_DIRECTORY);
assert(nodep->type == TMPFS_DIRECTORY);
/*
* Yes, we really use O(n) algorithm here.
522,24 → 467,21
* If it bothers someone, it could be fixed by introducing a
* hash table.
*/
for (i = 0, cur = dentry->child; i < pos && cur; i++,
cur = cur->sibling)
for (i = 0, lnk = nodep->cs_head.next;
i < pos && lnk != &nodep->cs_head;
i++, lnk = lnk->next)
;
 
if (!cur) {
if (lnk == &nodep->cs_head) {
ipc_answer_0(callid, ENOENT);
ipc_answer_1(rid, ENOENT, 0);
return;
}
 
unsigned long key = (unsigned long) dentry;
link_t *hlp = hash_table_find(&cur->names, &key);
assert(hlp);
tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
link);
dentryp = list_get_instance(lnk, tmpfs_dentry_t, link);
 
(void) ipc_data_read_finalize(callid, namep->name,
str_size(namep->name) + 1);
(void) ipc_data_read_finalize(callid, dentryp->name,
str_size(dentryp->name) + 1);
bytes = 1;
}
 
556,20 → 498,20
off_t pos = (off_t)IPC_GET_ARG3(*request);
 
/*
* Lookup the respective dentry.
* Lookup the respective TMPFS node.
*/
link_t *hlp;
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = index,
[DENTRIES_KEY_DEV] = dev_handle
[NODES_KEY_INDEX] = index,
[NODES_KEY_DEV] = dev_handle
};
hlp = hash_table_find(&dentries, key);
hlp = hash_table_find(&nodes, key);
if (!hlp) {
ipc_answer_0(rid, ENOENT);
return;
}
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
dh_link);
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
nh_link);
 
/*
* Receive the write request.
585,13 → 527,13
/*
* Check whether the file needs to grow.
*/
if (pos + size <= dentry->size) {
if (pos + size <= nodep->size) {
/* The file size is not changing. */
(void) ipc_data_write_finalize(callid, dentry->data + pos, size);
ipc_answer_2(rid, EOK, size, dentry->size);
(void) ipc_data_write_finalize(callid, nodep->data + pos, size);
ipc_answer_2(rid, EOK, size, nodep->size);
return;
}
size_t delta = (pos + size) - dentry->size;
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
599,18 → 541,18
* our heap allocator can save us and just grow the block whenever
* possible.
*/
void *newdata = realloc(dentry->data, dentry->size + delta);
void *newdata = realloc(nodep->data, nodep->size + delta);
if (!newdata) {
ipc_answer_0(callid, ENOMEM);
ipc_answer_2(rid, EOK, 0, dentry->size);
ipc_answer_2(rid, EOK, 0, nodep->size);
return;
}
/* Clear any newly allocated memory in order to emulate gaps. */
memset(newdata + dentry->size, 0, delta);
dentry->size += delta;
dentry->data = newdata;
(void) ipc_data_write_finalize(callid, dentry->data + pos, size);
ipc_answer_2(rid, EOK, size, dentry->size);
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)
620,40 → 562,45
size_t size = (off_t)IPC_GET_ARG3(*request);
 
/*
* Lookup the respective dentry.
* Lookup the respective TMPFS node.
*/
link_t *hlp;
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = index,
[DENTRIES_KEY_DEV] = dev_handle
[NODES_KEY_INDEX] = index,
[NODES_KEY_DEV] = dev_handle
};
hlp = hash_table_find(&dentries, key);
hlp = hash_table_find(&nodes, key);
if (!hlp) {
ipc_answer_0(rid, ENOENT);
return;
}
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
dh_link);
tmpfs_node_t *nodep = hash_table_get_instance(hlp, tmpfs_node_t,
nh_link);
 
if (size == dentry->size) {
if (size == nodep->size) {
ipc_answer_0(rid, EOK);
return;
}
 
void *newdata = realloc(dentry->data, size);
void *newdata = realloc(nodep->data, size);
if (!newdata) {
ipc_answer_0(rid, ENOMEM);
return;
}
if (size > dentry->size) {
size_t delta = size - dentry->size;
memset(newdata + dentry->size, 0, delta);
if (size > nodep->size) {
size_t delta = size - nodep->size;
memset(newdata + nodep->size, 0, delta);
}
dentry->size = size;
dentry->data = newdata;
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);
662,20 → 609,36
 
link_t *hlp;
unsigned long key[] = {
[DENTRIES_KEY_INDEX] = index,
[DENTRIES_KEY_DEV] = dev_handle
[NODES_KEY_INDEX] = index,
[NODES_KEY_DEV] = dev_handle
};
hlp = hash_table_find(&dentries, key);
hlp = hash_table_find(&nodes, key);
if (!hlp) {
ipc_answer_0(rid, ENOENT);
return;
}
tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
dh_link);
rc = tmpfs_destroy_node(dentry);
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_device(ipc_callid_t rid, ipc_call_t *request)
{
ipc_answer_0(rid, ENOTSUP);
}
 
void tmpfs_sync(ipc_callid_t rid, ipc_call_t *request)
{
/* Dummy implementation */
ipc_answer_0(rid, EOK);
}
 
/**
* @}
*/
*/
/branches/network/uspace/srv/fs/tmpfs/Makefile
72,7 → 72,7
disasm: $(OUTPUT).disasm
 
$(OUTPUT).disasm: $(OUTPUT)
$(OBJDUMP) -d $< >$@
$(OBJDUMP) -d $< > $@
 
%.o: %.S
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@
/branches/network/uspace/srv/fs/fat/fat_idx.c
39,10 → 39,10
#include "../../vfs/vfs.h"
#include <errno.h>
#include <string.h>
#include <libadt/hash_table.h>
#include <libadt/list.h>
#include <adt/hash_table.h>
#include <adt/list.h>
#include <assert.h>
#include <futex.h>
#include <fibril_sync.h>
 
/** Each instance of this type describes one interval of freed VFS indices. */
typedef struct {
68,8 → 68,8
link_t freed_head;
} unused_t;
 
/** Futex protecting the list of unused structures. */
static futex_t unused_futex = FUTEX_INITIALIZER;
/** Mutex protecting the list of unused structures. */
static FIBRIL_MUTEX_INITIALIZE(unused_lock);
 
/** List of unused structures. */
static LIST_INITIALIZE(unused_head);
89,7 → 89,7
link_t *l;
 
if (lock)
futex_down(&unused_futex);
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)
96,12 → 96,12
return u;
}
if (lock)
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return NULL;
}
 
/** Futex protecting the up_hash and ui_hash. */
static futex_t used_futex = FUTEX_INITIALIZER;
/** Mutex protecting the up_hash and ui_hash. */
static FIBRIL_MUTEX_INITIALIZE(used_lock);
 
/**
* Global hash table of all used fat_idx_t structures.
231,7 → 231,7
*/
*index = u->next++;
--u->remaining;
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return true;
}
} else {
244,7 → 244,7
list_remove(&f->link);
free(f);
}
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return true;
}
/*
252,7 → 252,7
* theoretically still possible (e.g. too many open unlinked nodes or
* too many zero-sized nodes).
*/
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return false;
}
 
302,7 → 302,7
if (lnk->prev != &u->freed_head)
try_coalesce_intervals(lnk->prev, lnk,
lnk);
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return;
}
if (f->last == index - 1) {
310,7 → 310,7
if (lnk->next != &u->freed_head)
try_coalesce_intervals(lnk, lnk->next,
lnk);
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return;
}
if (index > f->first) {
321,7 → 321,7
n->first = index;
n->last = index;
list_insert_before(&n->link, lnk);
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return;
}
 
335,7 → 335,7
n->last = index;
list_append(&n->link, &u->freed_head);
}
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
}
 
static fat_idx_t *fat_idx_create(dev_handle_t dev_handle)
352,7 → 352,7
link_initialize(&fidx->uph_link);
link_initialize(&fidx->uih_link);
futex_initialize(&fidx->lock, 1);
fibril_mutex_initialize(&fidx->lock);
fidx->dev_handle = dev_handle;
fidx->pfc = FAT_CLST_RES0; /* no parent yet */
fidx->pdi = 0;
365,10 → 365,10
{
fat_idx_t *fidx;
 
futex_down(&used_futex);
fibril_mutex_lock(&used_lock);
fidx = fat_idx_create(dev_handle);
if (!fidx) {
futex_up(&used_futex);
fibril_mutex_unlock(&used_lock);
return NULL;
}
378,8 → 378,8
};
hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
futex_down(&fidx->lock);
futex_up(&used_futex);
fibril_mutex_lock(&fidx->lock);
fibril_mutex_unlock(&used_lock);
 
return fidx;
}
395,7 → 395,7
[UPH_PDI_KEY] = pdi,
};
 
futex_down(&used_futex);
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);
402,7 → 402,7
} else {
fidx = fat_idx_create(dev_handle);
if (!fidx) {
futex_up(&used_futex);
fibril_mutex_unlock(&used_lock);
return NULL;
}
417,8 → 417,8
hash_table_insert(&up_hash, pkey, &fidx->uph_link);
hash_table_insert(&ui_hash, ikey, &fidx->uih_link);
}
futex_down(&fidx->lock);
futex_up(&used_futex);
fibril_mutex_lock(&fidx->lock);
fibril_mutex_unlock(&used_lock);
 
return fidx;
}
431,9 → 431,9
[UPH_PDI_KEY] = idx->pdi,
};
 
futex_down(&used_futex);
fibril_mutex_lock(&used_lock);
hash_table_insert(&up_hash, pkey, &idx->uph_link);
futex_up(&used_futex);
fibril_mutex_unlock(&used_lock);
}
 
void fat_idx_hashout(fat_idx_t *idx)
444,9 → 444,9
[UPH_PDI_KEY] = idx->pdi,
};
 
futex_down(&used_futex);
fibril_mutex_lock(&used_lock);
hash_table_remove(&up_hash, pkey, 3);
futex_up(&used_futex);
fibril_mutex_unlock(&used_lock);
}
 
fat_idx_t *
459,13 → 459,13
[UIH_INDEX_KEY] = index,
};
 
futex_down(&used_futex);
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);
futex_down(&fidx->lock);
fibril_mutex_lock(&fidx->lock);
}
futex_up(&used_futex);
fibril_mutex_unlock(&used_lock);
 
return fidx;
}
483,7 → 483,7
 
assert(idx->pfc == FAT_CLST_RES0);
 
futex_down(&used_futex);
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
490,7 → 490,7
* the index hash only.
*/
hash_table_remove(&ui_hash, ikey, 2);
futex_up(&used_futex);
fibril_mutex_unlock(&used_lock);
/* Release the VFS index. */
fat_index_free(idx->dev_handle, idx->index);
/* Deallocate the structure. */
524,12 → 524,12
if (!u)
return ENOMEM;
unused_initialize(u, dev_handle);
futex_down(&unused_futex);
fibril_mutex_lock(&unused_lock);
if (!unused_find(dev_handle, false))
list_append(&u->link, &unused_head);
else
rc = EEXIST;
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
return rc;
}
 
540,7 → 540,7
u = unused_find(dev_handle, true);
assert(u);
list_remove(&u->link);
futex_up(&unused_futex);
fibril_mutex_unlock(&unused_lock);
 
while (!list_empty(&u->freed_head)) {
freed_t *f;
/branches/network/uspace/srv/fs/fat/fat.h
35,6 → 35,7
 
#include "fat_fat.h"
#include <ipc/ipc.h>
#include <fibril_sync.h>
#include <libfs.h>
#include <atomic.h>
#include <sys/types.h>
160,7 → 161,7
/** Used indices (index) hash table link. */
link_t uih_link;
 
futex_t lock;
fibril_mutex_t lock;
dev_handle_t dev_handle;
fs_index_t index;
/**
178,7 → 179,10
 
/** FAT in-core node. */
typedef struct fat_node {
futex_t lock;
/** Back pointer to the FS node. */
fs_node_t *bp;
fibril_mutex_t lock;
fat_node_type_t type;
fat_idx_t *idx;
/**
203,7 → 207,11
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_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_device(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);
/branches/network/uspace/srv/fs/fat/fat_dentry.c
114,37 → 114,51
 
void fat_dentry_name_get(const fat_dentry_t *d, char *buf)
{
int i;
 
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
*buf++ = d->name[i];
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
*buf++ = d->ext[i];
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)
{
int i;
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:
156,12 → 170,19
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:
172,10 → 193,25
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)
/branches/network/uspace/srv/fs/fat/fat_dentry.h
47,6 → 47,9
#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
65,7 → 68,7
uint8_t name[8];
uint8_t ext[3];
uint8_t attr;
uint8_t reserved;
uint8_t lcase;
uint8_t ctime_fine;
uint16_t ctime;
uint16_t cdate;
/branches/network/uspace/srv/fs/fat/fat_fat.c
45,14 → 45,15
#include <byteorder.h>
#include <align.h>
#include <assert.h>
#include <futex.h>
#include <fibril_sync.h>
#include <mem.h>
 
/**
* The fat_alloc_lock futex protects all copies of the File Allocation Table
* 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 futex_t fat_alloc_lock = FUTEX_INITIALIZER;
static FIBRIL_MUTEX_INITIALIZE(fat_alloc_lock);
 
/** Walk the cluster chain.
*
325,7 → 326,7
/*
* Search FAT1 for unused clusters.
*/
futex_down(&fat_alloc_lock);
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++) {
349,7 → 350,7
*mcl = lifo[found - 1];
*lcl = lifo[0];
free(lifo);
futex_up(&fat_alloc_lock);
fibril_mutex_unlock(&fat_alloc_lock);
return EOK;
}
}
356,7 → 357,7
}
block_put(blk);
}
futex_up(&fat_alloc_lock);
fibril_mutex_unlock(&fat_alloc_lock);
 
/*
* We could not find enough clusters. Now we need to free the clusters
/branches/network/uspace/srv/fs/fat/fat.c
89,6 → 89,8
callid = async_get_call(&call);
switch (IPC_GET_METHOD(call)) {
case IPC_M_PHONE_HUNGUP:
return;
case VFS_MOUNTED:
fat_mounted(callid, &call);
break;
107,9 → 109,21
case VFS_TRUNCATE:
fat_truncate(callid, &call);
break;
case VFS_CLOSE:
fat_close(callid, &call);
break;
case VFS_DESTROY:
fat_destroy(callid, &call);
break;
case VFS_OPEN_NODE:
fat_open_node(callid, &call);
break;
case VFS_DEVICE:
fat_device(callid, &call);
break;
case VFS_SYNC:
fat_sync(callid, &call);
break;
default:
ipc_answer_0(callid, ENOTSUP);
break;
/branches/network/uspace/srv/fs/fat/fat_ops.c
48,22 → 48,26
#include <errno.h>
#include <string.h>
#include <byteorder.h>
#include <libadt/hash_table.h>
#include <libadt/list.h>
#include <adt/hash_table.h>
#include <adt/list.h>
#include <assert.h>
#include <futex.h>
#include <fibril_sync.h>
#include <sys/mman.h>
#include <align.h>
 
/** Futex protecting the list of cached free FAT nodes. */
static futex_t ffn_futex = FUTEX_INITIALIZER;
#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)
{
futex_initialize(&node->lock, 1);
fibril_mutex_initialize(&node->lock);
node->bp = NULL;
node->idx = NULL;
node->type = 0;
link_initialize(&node->ffn_link);
108,36 → 112,46
 
static fat_node_t *fat_node_get_new(void)
{
fs_node_t *fn;
fat_node_t *nodep;
 
futex_down(&ffn_futex);
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 (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
if (!fibril_mutex_trylock(&nodep->lock))
goto skip_cache;
idxp_tmp = nodep->idx;
if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
futex_up(&nodep->lock);
if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
fibril_mutex_unlock(&nodep->lock);
goto skip_cache;
}
list_remove(&nodep->ffn_link);
futex_up(&ffn_futex);
fibril_mutex_unlock(&ffn_mutex);
if (nodep->dirty)
fat_node_sync(nodep);
idxp_tmp->nodep = NULL;
futex_up(&nodep->lock);
futex_up(&idxp_tmp->lock);
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. */
futex_up(&ffn_futex);
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)
if (!nodep) {
free(fn);
return NULL;
}
}
fat_node_initialize(nodep);
fs_node_initialize(fn);
fn->data = nodep;
nodep->bp = fn;
return nodep;
}
146,7 → 160,7
*
* @param idxp Locked index structure.
*/
static void *fat_node_get_core(fat_idx_t *idxp)
static fat_node_t *fat_node_get_core(fat_idx_t *idxp)
{
block_t *b;
fat_bs_t *bs;
161,10 → 175,10
* We are lucky.
* The node is already instantiated in memory.
*/
futex_down(&idxp->nodep->lock);
fibril_mutex_lock(&idxp->nodep->lock);
if (!idxp->nodep->refcnt++)
list_remove(&idxp->nodep->ffn_link);
futex_up(&idxp->nodep->lock);
fibril_mutex_unlock(&idxp->nodep->lock);
return idxp->nodep;
}
 
223,21 → 237,21
/*
* Forward declarations of FAT libfs operations.
*/
static void *fat_node_get(dev_handle_t, fs_index_t);
static void fat_node_put(void *);
static void *fat_create_node(dev_handle_t, int);
static int fat_destroy_node(void *);
static int fat_link(void *, void *, const char *);
static int fat_unlink(void *, void *);
static void *fat_match(void *, const char *);
static fs_index_t fat_index_get(void *);
static size_t fat_size_get(void *);
static unsigned fat_lnkcnt_get(void *);
static bool fat_has_children(void *);
static void *fat_root_get(dev_handle_t);
static 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(void *);
static bool fat_is_file(void *node);
static bool fat_is_directory(fs_node_t *);
static bool fat_is_file(fs_node_t *node);
 
/*
* FAT libfs operations.
244,9 → 258,9
*/
 
/** Instantiate a FAT in-core node. */
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
{
void *node;
fat_node_t *nodep;
fat_idx_t *idxp;
 
idxp = fat_idx_get_by_index(dev_handle, index);
253,22 → 267,22
if (!idxp)
return NULL;
/* idxp->lock held */
node = fat_node_get_core(idxp);
futex_up(&idxp->lock);
return node;
nodep = fat_node_get_core(idxp);
fibril_mutex_unlock(&idxp->lock);
return FS_NODE(nodep);
}
 
void fat_node_put(void *node)
void fat_node_put(fs_node_t *fn)
{
fat_node_t *nodep = (fat_node_t *)node;
fat_node_t *nodep = FAT_NODE(fn);
bool destroy = false;
 
futex_down(&nodep->lock);
fibril_mutex_lock(&nodep->lock);
if (!--nodep->refcnt) {
if (nodep->idx) {
futex_down(&ffn_futex);
fibril_mutex_lock(&ffn_mutex);
list_append(&nodep->ffn_link, &ffn_head);
futex_up(&ffn_futex);
fibril_mutex_unlock(&ffn_mutex);
} else {
/*
* The node does not have any index structure associated
279,12 → 293,14
destroy = true;
}
}
futex_up(&nodep->lock);
if (destroy)
free(node);
fibril_mutex_unlock(&nodep->lock);
if (destroy) {
free(nodep->bp);
free(nodep);
}
}
 
void *fat_create_node(dev_handle_t dev_handle, int flags)
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags)
{
fat_idx_t *idxp;
fat_node_t *nodep;
310,7 → 326,7
idxp = fat_idx_get_new(dev_handle);
if (!idxp) {
fat_free_clusters(bs, dev_handle, mcl);
fat_node_put(nodep);
fat_node_put(FS_NODE(nodep));
return NULL;
}
/* idxp->lock held */
344,13 → 360,13
nodep->idx = idxp;
idxp->nodep = nodep;
 
futex_up(&idxp->lock);
return nodep;
fibril_mutex_unlock(&idxp->lock);
return FS_NODE(nodep);
}
 
int fat_destroy_node(void *node)
int fat_destroy_node(fs_node_t *fn)
{
fat_node_t *nodep = (fat_node_t *)node;
fat_node_t *nodep = FAT_NODE(fn);
fat_bs_t *bs;
 
/*
364,7 → 380,7
/*
* The node may not have any children.
*/
assert(fat_has_children(node) == false);
assert(fat_has_children(fn) == false);
 
bs = block_bb_get(nodep->idx->dev_handle);
if (nodep->firstc != FAT_CLST_RES0) {
374,14 → 390,15
}
 
fat_idx_destroy(nodep->idx);
free(nodep->bp);
free(nodep);
return EOK;
}
 
int fat_link(void *prnt, void *chld, const char *name)
int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
{
fat_node_t *parentp = (fat_node_t *)prnt;
fat_node_t *childp = (fat_node_t *)chld;
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;
392,16 → 409,16
fat_cluster_t mcl, lcl;
int rc;
 
futex_down(&childp->lock);
fibril_mutex_lock(&childp->lock);
if (childp->lnkcnt == 1) {
/*
* On FAT, we don't support multiple hard links.
*/
futex_up(&childp->lock);
fibril_mutex_unlock(&childp->lock);
return EMLINK;
}
assert(childp->lnkcnt == 0);
futex_up(&childp->lock);
fibril_mutex_unlock(&childp->lock);
 
if (!fat_dentry_name_verify(name)) {
/*
415,7 → 432,7
* a new one.
*/
futex_down(&parentp->idx->lock);
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);
446,12 → 463,12
*/
if (parentp->idx->pfc == FAT_CLST_ROOT) {
/* Can't grow the root directory. */
futex_up(&parentp->idx->lock);
fibril_mutex_unlock(&parentp->idx->lock);
return ENOSPC;
}
rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
if (rc != EOK) {
futex_up(&parentp->idx->lock);
fibril_mutex_unlock(&parentp->idx->lock);
return rc;
}
fat_append_clusters(bs, parentp, mcl);
474,9 → 491,9
fat_dentry_name_set(d, name);
b->dirty = true; /* need to sync block */
block_put(b);
futex_up(&parentp->idx->lock);
fibril_mutex_unlock(&parentp->idx->lock);
 
futex_down(&childp->idx->lock);
fibril_mutex_lock(&childp->idx->lock);
/*
* If possible, create the Sub-directory Identifier Entry and the
512,12 → 529,12
 
childp->idx->pfc = parentp->firstc;
childp->idx->pdi = i * dps + j;
futex_up(&childp->idx->lock);
fibril_mutex_unlock(&childp->idx->lock);
 
futex_down(&childp->lock);
fibril_mutex_lock(&childp->lock);
childp->lnkcnt = 1;
childp->dirty = true; /* need to sync node */
futex_up(&childp->lock);
fibril_mutex_unlock(&childp->lock);
 
/*
* Hash in the index structure into the position hash.
527,19 → 544,25
return EOK;
}
 
int fat_unlink(void *prnt, void *chld)
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
{
fat_node_t *parentp = (fat_node_t *)prnt;
fat_node_t *childp = (fat_node_t *)chld;
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;
 
futex_down(&parentp->lock);
futex_down(&childp->lock);
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);
futex_down(&childp->idx->lock);
fibril_mutex_lock(&childp->idx->lock);
bs = block_bb_get(childp->idx->dev_handle);
bps = uint16_t_le2host(bs->bps);
 
558,19 → 581,19
/* clear position information */
childp->idx->pfc = FAT_CLST_RES0;
childp->idx->pdi = 0;
futex_up(&childp->idx->lock);
fibril_mutex_unlock(&childp->idx->lock);
childp->lnkcnt = 0;
childp->dirty = true;
futex_up(&childp->lock);
futex_up(&parentp->lock);
fibril_mutex_unlock(&childp->lock);
fibril_mutex_unlock(&parentp->lock);
 
return EOK;
}
 
void *fat_match(void *prnt, const char *component)
fs_node_t *fat_match(fs_node_t *pfn, const char *component)
{
fat_bs_t *bs;
fat_node_t *parentp = (fat_node_t *)prnt;
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 */
579,7 → 602,7
fat_dentry_t *d;
block_t *b;
 
futex_down(&parentp->idx->lock);
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);
594,7 → 617,7
continue;
case FAT_DENTRY_LAST:
block_put(b);
futex_up(&parentp->idx->lock);
fibril_mutex_unlock(&parentp->idx->lock);
return NULL;
default:
case FAT_DENTRY_VALID:
603,7 → 626,7
}
if (fat_dentry_namecmp(name, component) == 0) {
/* hit */
void *node;
fat_node_t *nodep;
/*
* Assume tree hierarchy for locking. We
* already have the parent and now we are going
613,7 → 636,7
fat_idx_t *idx = fat_idx_get_by_pos(
parentp->idx->dev_handle, parentp->firstc,
i * dps + j);
futex_up(&parentp->idx->lock);
fibril_mutex_unlock(&parentp->idx->lock);
if (!idx) {
/*
* Can happen if memory is low or if we
622,41 → 645,38
block_put(b);
return NULL;
}
node = fat_node_get_core(idx);
futex_up(&idx->lock);
nodep = fat_node_get_core(idx);
fibril_mutex_unlock(&idx->lock);
block_put(b);
return node;
return FS_NODE(nodep);
}
}
block_put(b);
}
 
futex_up(&parentp->idx->lock);
fibril_mutex_unlock(&parentp->idx->lock);
return NULL;
}
 
fs_index_t fat_index_get(void *node)
fs_index_t fat_index_get(fs_node_t *fn)
{
fat_node_t *fnodep = (fat_node_t *)node;
if (!fnodep)
return 0;
return fnodep->idx->index;
return FAT_NODE(fn)->idx->index;
}
 
size_t fat_size_get(void *node)
size_t fat_size_get(fs_node_t *fn)
{
return ((fat_node_t *)node)->size;
return FAT_NODE(fn)->size;
}
 
unsigned fat_lnkcnt_get(void *node)
unsigned fat_lnkcnt_get(fs_node_t *fn)
{
return ((fat_node_t *)node)->lnkcnt;
return FAT_NODE(fn)->lnkcnt;
}
 
bool fat_has_children(void *node)
bool fat_has_children(fs_node_t *fn)
{
fat_bs_t *bs;
fat_node_t *nodep = (fat_node_t *)node;
fat_node_t *nodep = FAT_NODE(fn);
unsigned bps;
unsigned dps;
unsigned blocks;
666,7 → 686,7
if (nodep->type != FAT_DIRECTORY)
return false;
futex_down(&nodep->idx->lock);
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);
685,26 → 705,26
continue;
case FAT_DENTRY_LAST:
block_put(b);
futex_up(&nodep->idx->lock);
fibril_mutex_unlock(&nodep->idx->lock);
return false;
default:
case FAT_DENTRY_VALID:
block_put(b);
futex_up(&nodep->idx->lock);
fibril_mutex_unlock(&nodep->idx->lock);
return true;
}
block_put(b);
futex_up(&nodep->idx->lock);
fibril_mutex_unlock(&nodep->idx->lock);
return true;
}
block_put(b);
}
 
futex_up(&nodep->idx->lock);
fibril_mutex_unlock(&nodep->idx->lock);
return false;
}
 
void *fat_root_get(dev_handle_t dev_handle)
fs_node_t *fat_root_get(dev_handle_t dev_handle)
{
return fat_node_get(dev_handle, 0);
}
714,14 → 734,14
return fat_reg.plb_ro[pos % PLB_SIZE];
}
 
bool fat_is_directory(void *node)
bool fat_is_directory(fs_node_t *fn)
{
return ((fat_node_t *)node)->type == FAT_DIRECTORY;
return FAT_NODE(fn)->type == FAT_DIRECTORY;
}
 
bool fat_is_file(void *node)
bool fat_is_file(fs_node_t *fn)
{
return ((fat_node_t *)node)->type == FAT_FILE;
return FAT_NODE(fn)->type == FAT_FILE;
}
 
/** libfs operations */
750,6 → 770,7
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;
777,6 → 798,12
}
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) {
806,7 → 833,7
}
 
/* Initialize the block cache */
rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
if (rc != EOK) {
block_fini(dev_handle);
ipc_answer_0(rid, rc);
821,8 → 848,17
}
 
/* 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);
832,8 → 868,9
 
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);
free(rootp);
fat_idx_fini_by_dev_handle(dev_handle);
ipc_answer_0(rid, ENOMEM);
return;
848,8 → 885,10
rootp->size = rde * sizeof(fat_dentry_t);
rootp->idx = ridxp;
ridxp->nodep = rootp;
rootp->bp = rfn;
rfn->data = rootp;
futex_up(&ridxp->lock);
fibril_mutex_unlock(&ridxp->lock);
 
ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
}
856,7 → 895,7
 
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
{
ipc_answer_0(rid, ENOTSUP);
libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
}
 
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
869,21 → 908,23
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);
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
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 (!nodep) {
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(nodep);
fat_node_put(fn);
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
return;
954,7 → 995,7
bnum++;
}
miss:
fat_node_put(nodep);
fat_node_put(fn);
ipc_answer_0(callid, ENOENT);
ipc_answer_1(rid, ENOENT, 0);
return;
963,7 → 1004,7
bytes = (pos - spos) + 1;
}
 
fat_node_put(nodep);
fat_node_put(fn);
ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
}
 
972,7 → 1013,8
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);
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
fs_node_t *fn = fat_node_get(dev_handle, index);
fat_node_t *nodep;
fat_bs_t *bs;
size_t bytes;
block_t *b;
982,15 → 1024,16
off_t boundary;
int flags = BLOCK_FLAGS_NONE;
if (!nodep) {
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(nodep);
fat_node_put(fn);
ipc_answer_0(callid, EINVAL);
ipc_answer_0(rid, EINVAL);
return;
1031,7 → 1074,7
nodep->dirty = true; /* need to sync node */
}
ipc_answer_2(rid, EOK, bytes, nodep->size);
fat_node_put(nodep);
fat_node_put(fn);
return;
} else {
/*
1047,7 → 1090,7
status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
if (status != EOK) {
/* could not allocate a chain of nclsts clusters */
fat_node_put(nodep);
fat_node_put(fn);
ipc_answer_0(callid, status);
ipc_answer_0(rid, status);
return;
1068,7 → 1111,7
nodep->size = pos + bytes;
nodep->dirty = true; /* need to sync node */
ipc_answer_2(rid, EOK, bytes, nodep->size);
fat_node_put(nodep);
fat_node_put(fn);
return;
}
}
1078,7 → 1121,8
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);
fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
fs_node_t *fn = fat_node_get(dev_handle, index);
fat_node_t *nodep;
fat_bs_t *bs;
uint16_t bps;
uint8_t spc;
1085,10 → 1129,11
unsigned bpc; /* bytes per cluster */
int rc;
 
if (!nodep) {
if (!fn) {
ipc_answer_0(rid, ENOENT);
return;
}
nodep = FAT_NODE(fn);
 
bs = block_bb_get(dev_handle);
bps = uint16_t_le2host(bs->bps);
1126,11 → 1171,16
nodep->dirty = true; /* need to sync node */
rc = EOK;
}
fat_node_put(nodep);
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);
1137,16 → 1187,32
fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
int rc;
 
fat_node_t *nodep = fat_node_get(dev_handle, index);
if (!nodep) {
fs_node_t *fn = fat_node_get(dev_handle, index);
if (!fn) {
ipc_answer_0(rid, ENOENT);
return;
}
 
rc = fat_destroy_node(nodep);
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_device(ipc_callid_t rid, ipc_call_t *request)
{
ipc_answer_0(rid, ENOTSUP);
}
 
void fat_sync(ipc_callid_t rid, ipc_call_t *request)
{
/* Dummy implementation */
ipc_answer_0(rid, EOK);
}
 
/**
* @}
*/
*/
/branches/network/uspace/srv/fs/fat/Makefile
74,7 → 74,7
disasm: $(OUTPUT).disasm
 
$(OUTPUT).disasm: $(OUTPUT)
$(OBJDUMP) -d $< >$@
$(OBJDUMP) -d $< > $@
 
%.o: %.S
$(CC) $(DEFS) $(AFLAGS) $(CFLAGS) -D__ASM__ -c $< -o $@