diff options
author | Alex Auvolat <alex.auvolat@ens.fr> | 2015-02-19 20:29:30 +0100 |
---|---|---|
committer | Alex Auvolat <alex.auvolat@ens.fr> | 2015-02-19 20:29:30 +0100 |
commit | adc5a421917dd6e23a2fc01dc9fb2a9f881c291d (patch) | |
tree | 36b2abb5ef724cb53747a323a370421c7f6ffe19 /src/kernel | |
parent | f876e66e717c4dd853da12892048262afe47ffdf (diff) | |
download | kogata-adc5a421917dd6e23a2fc01dc9fb2a9f881c291d.tar.gz kogata-adc5a421917dd6e23a2fc01dc9fb2a9f881c291d.zip |
Remove global locking on handles by VFS ; adapt nullfs.
Diffstat (limited to 'src/kernel')
-rw-r--r-- | src/kernel/include/vfs.h | 1 | ||||
-rw-r--r-- | src/kernel/user/nullfs.c | 39 | ||||
-rw-r--r-- | src/kernel/user/vfs.c | 32 |
3 files changed, 32 insertions, 40 deletions
diff --git a/src/kernel/include/vfs.h b/src/kernel/include/vfs.h index bae4e6f..c5777a2 100644 --- a/src/kernel/include/vfs.h +++ b/src/kernel/include/vfs.h @@ -57,7 +57,6 @@ typedef struct fs_handle { struct fs_node *node; int refs; - mutex_t lock; // These fields are filled by the FS's specific open() code fs_handle_ops_t *ops; diff --git a/src/kernel/user/nullfs.c b/src/kernel/user/nullfs.c index 50baeff..c3f83cf 100644 --- a/src/kernel/user/nullfs.c +++ b/src/kernel/user/nullfs.c @@ -472,10 +472,11 @@ void nullfs_d_dispose(fs_node_ptr n) { bool nullfs_dh_readdir(fs_handle_ptr f, dirent_t *d) { nullfs_dh_t *h = (nullfs_dh_t*)f; - if (h->i >= h->nitems) { + int i = h->i; + if (i >= h->nitems) { return false; } else { - memcpy(d, &h->items[h->i], sizeof(dirent_t)); + memcpy(d, &h->items[i], sizeof(dirent_t)); h->i++; return true; } @@ -495,14 +496,16 @@ bool nullfs_f_open(fs_node_ptr n, int mode, fs_handle_t *s) { if (mode & ~f->ok_modes) return false; - mutex_lock(&f->lock); - if (mode & FM_TRUNC) { // truncate file + mutex_lock(&f->lock); + if (f->own_data) free(f->data); f->size = 0; f->own_data = false; f->data = 0; + + mutex_unlock(&f->lock); } s->mode = mode; @@ -514,11 +517,13 @@ bool nullfs_f_open(fs_node_ptr n, int mode, fs_handle_t *s) { bool nullfs_f_stat(fs_node_ptr n, stat_t *st) { nullfs_file_t *f = (nullfs_file_t*)n; + mutex_lock(&f->lock); st->type = FT_REGULAR; st->access = f->ok_modes; st->size = f->size; + mutex_unlock(&f->lock); return true; } @@ -530,21 +535,31 @@ void nullfs_f_dispose(fs_node_ptr n) { static size_t nullfs_fh_read(fs_handle_ptr h, size_t offset, size_t len, char* buf) { nullfs_file_t *f = (nullfs_file_t*)h; + mutex_lock(&f->lock); + + size_t ret = 0; - if (offset >= f->size) return 0; + if (offset >= f->size) goto end_read; if (offset + len > f->size) len = f->size - offset; memcpy(buf, f->data + offset, len); - return len; + ret = len; + +end_read: + mutex_unlock(&f->lock); + return ret; } static size_t nullfs_fh_write(fs_handle_ptr h, size_t offset, size_t len, const char* buf) { nullfs_file_t *f = (nullfs_file_t*)h; + mutex_lock(&f->lock); + + size_t ret = 0; if (offset + len > f->size) { // resize buffer (zero out new portion) void* new_buffer = malloc(offset + len); - if (new_buffer == 0) return 0; + if (new_buffer == 0) goto end_write; memcpy(new_buffer, f->data, f->size); if (offset > f->size) @@ -557,13 +572,15 @@ static size_t nullfs_fh_write(fs_handle_ptr h, size_t offset, size_t len, const } memcpy(f->data + offset, buf, len); - return len; + ret = len; + +end_write: + mutex_unlock(&f->lock); + return ret; } static void nullfs_fh_close(fs_handle_ptr h) { - nullfs_file_t *f = (nullfs_file_t*)h; - - mutex_unlock(&f->lock); + // nothing to do } /* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/src/kernel/user/vfs.c b/src/kernel/user/vfs.c index ab49553..31f162a 100644 --- a/src/kernel/user/vfs.c +++ b/src/kernel/user/vfs.c @@ -387,7 +387,6 @@ fs_handle_t* fs_open(fs_t *fs, const char* file, int mode) { if (h == 0) goto error; h->refs = 1; - h->lock = MUTEX_UNLOCKED; h->fs = fs; h->node = n; @@ -407,21 +406,16 @@ error: } void ref_file(fs_handle_t *file) { - mutex_lock(&file->lock); file->refs++; - mutex_unlock(&file->lock); } void unref_file(fs_handle_t *file) { - mutex_lock(&file->lock); file->refs--; if (file->refs == 0) { file->ops->close(file->data); unref_fs_node(file->node); unref_fs(file->fs); free(file); - } else { - mutex_unlock(&file->lock); } } @@ -434,11 +428,7 @@ size_t file_read(fs_handle_t *f, size_t offset, size_t len, char* buf) { if (f->ops->read == 0) return 0; - mutex_lock(&f->lock); - size_t ret = f->ops->read(f->data, offset, len, buf); - mutex_unlock(&f->lock); - - return ret; + return f->ops->read(f->data, offset, len, buf); } size_t file_write(fs_handle_t *f, size_t offset, size_t len, const char* buf) { @@ -446,28 +436,18 @@ size_t file_write(fs_handle_t *f, size_t offset, size_t len, const char* buf) { if (f->ops->write == 0) return 0; - mutex_lock(&f->lock); - size_t ret = f->ops->write(f->data, offset, len, buf); - mutex_unlock(&f->lock); - - return ret; + return f->ops->write(f->data, offset, len, buf); } bool file_stat(fs_handle_t *f, stat_t *st) { - mutex_lock(&f->node->lock); - bool ret = f->node->ops->stat && f->node->ops->stat(f->node->data, st); - mutex_unlock(&f->node->lock); - - return ret; + return f->node->ops->stat && f->node->ops->stat(f->node->data, st); } int file_ioctl(fs_handle_t *f, int command, void* data) { if (!(f->mode & FM_IOCTL)) return -1; - mutex_lock(&f->node->lock); int ret = -1; if (f->node->ops->ioctl) ret = f->node->ops->ioctl(f->node->data, command, data); - mutex_unlock(&f->node->lock); return ret; } @@ -475,11 +455,7 @@ int file_ioctl(fs_handle_t *f, int command, void* data) { bool file_readdir(fs_handle_t *f, dirent_t *d) { if (!(f->mode & FM_READDIR)) return 0; - mutex_lock(&f->lock); - bool ret = f->ops->readdir && f->ops->readdir(f->data, d); - mutex_unlock(&f->lock); - - return ret; + return f->ops->readdir && f->ops->readdir(f->data, d); } /* vim: set ts=4 sw=4 tw=0 noet :*/ |