break;
}
- inode->ctime = now;
- inode->change_count++;
+ bluesky_inode_update_ctime(inode, FALSE);
}
/* Copy inode attributes into NFS response. The BlueSkyInode should be locked
BlueSkyInode *inode = lookup_fh(req, argp);
if (inode != NULL) {
result.status = NFS3_OK;
+ g_mutex_lock(inode->lock);
encode_fattr3(&result.getattr3res_u.attributes, inode);
+ g_mutex_unlock(inode->lock);
} else {
result.status = NFS3ERR_STALE;
}
return;
}
+ g_mutex_lock(inode->lock);
encode_pre_wcc(&result.wccstat3_u.wcc, inode);
if (argp->guard.check) {
if (inode->ctime != decode_nfstime3(&argp->guard.sattrguard3_u.ctime)) {
result.status = NFS3ERR_NOT_SYNC;
result.wccstat3_u.wcc.after.present = TRUE;
encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, inode);
+ g_mutex_unlock(inode->lock);
async_rpc_send_reply(req, &result);
return;
}
inode);
result.status = NFS3_OK;
+ g_mutex_unlock(inode->lock);
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(dir->lock);
result.lookup3res_u.resfail.present = TRUE;
encode_fattr3(&result.lookup3res_u.resfail.post_op_attr_u.attributes, dir);
if (!validate_filename(argp->name)) {
result.status = NFS3ERR_NAMETOOLONG;
else
result.status = NFS3ERR_NOENT;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
uint64_t inum = bluesky_directory_lookup(dir, argp->name);
if (inum == 0) {
result.status = NFS3ERR_NOENT;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
BlueSkyInode *inode = bluesky_get_inode(fs, inum);
if (inode == NULL) {
result.status = NFS3ERR_NOENT;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(inode->lock);
schedule_inode_unref(req, inode);
result.status = NFS3_OK;
result.lookup3res_u.resok.object.data.data_len = 8;
result.lookup3res_u.resok.object.data.data_val = (char *)&fh_bytes;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(inode->lock);
result.status = NFS3_OK;
result.access3res_u.resok.obj_attributes.present = TRUE;
encode_fattr3(&result.access3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
result.access3res_u.resok.access = argp->access;
+ g_mutex_unlock(inode->lock);
async_rpc_send_reply(req, &result);
}
BlueSkyInode *inode = lookup_fh(req, argp);
if (inode != NULL) {
+ g_mutex_lock(inode->lock);
if (inode->type == BLUESKY_SYMLINK) {
result.status = NFS3_OK;
result.readlink3res_u.resok.symlink_attributes.present = TRUE;
result.readlink3res_u.resfail.present = TRUE;
encode_fattr3(&result.readlink3res_u.resfail.post_op_attr_u.attributes, inode);
}
+ g_mutex_unlock(inode->lock);
} else {
result.status = NFS3ERR_STALE;
}
return;
}
+ g_mutex_lock(inode->lock);
+
int count = argp->count;
if (argp->offset >= inode->size) {
count = 0;
result.read3res_u.resok.data.data_val = buf;
result.read3res_u.resok.data.data_len = count;
+ g_mutex_unlock(inode->lock);
+
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(inode->lock);
+
encode_pre_wcc(&wcc, inode);
if (inode->type != BLUESKY_REGULAR) {
result.status = NFS3ERR_INVAL;
result.write3res_u.resfail = wcc;
+ g_mutex_unlock(inode->lock);
async_rpc_send_reply(req, &result);
return;
}
result.write3res_u.resok.count = argp->count;
result.write3res_u.resok.committed = FILE_SYNC;
+ g_mutex_unlock(inode->lock);
+
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(dir->lock);
+
encode_pre_wcc(&wcc, dir);
if (dir->type != BLUESKY_DIRECTORY) {
result.status = NFS3ERR_NOTDIR;
result.diropres3_u.resfail = wcc;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
{
result.status = NFS3ERR_EXIST;
result.diropres3_u.resfail = wcc;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
file->ctime = time;
file->atime = time;
file->ntime = time;
+ g_mutex_lock(file->lock);
bluesky_insert_inode(fs, file);
bluesky_directory_insert(dir, argp->where.name, file->inum);
- dir->mtime = dir->ctime = bluesky_get_current_time();
- dir->change_count++;
+ bluesky_inode_update_ctime(dir, TRUE);
wcc.after.present = TRUE;
encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
+ g_mutex_unlock(file->lock);
+ g_mutex_unlock(dir->lock);
+
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(dir->lock);
+
encode_pre_wcc(&wcc, dir);
if (dir->type != BLUESKY_DIRECTORY) {
result.status = NFS3ERR_NOTDIR;
result.diropres3_u.resfail = wcc;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
{
result.status = NFS3ERR_EXIST;
result.diropres3_u.resfail = wcc;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
file->ctime = time;
file->atime = time;
file->ntime = time;
+ g_mutex_lock(file->lock);
bluesky_insert_inode(fs, file);
bluesky_directory_insert(dir, argp->where.name, file->inum);
set_attributes(file, &argp->attributes);
- dir->mtime = dir->ctime = bluesky_get_current_time();
- dir->change_count++;
+ bluesky_inode_update_ctime(dir, TRUE);
wcc.after.present = TRUE;
encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
+ g_mutex_unlock(file->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(dir->lock);
encode_pre_wcc(&wcc, dir);
if (dir->type != BLUESKY_DIRECTORY) {
result.status = NFS3ERR_NOTDIR;
result.diropres3_u.resfail = wcc;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
{
result.status = NFS3ERR_EXIST;
result.diropres3_u.resfail = wcc;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
file->atime = time;
file->ntime = time;
file->symlink_contents = g_strdup(argp->symlink.symlink_data);
+ g_mutex_lock(file->lock);
bluesky_insert_inode(fs, file);
bluesky_directory_insert(dir, argp->where.name, file->inum);
- dir->mtime = dir->ctime = bluesky_get_current_time();
- dir->change_count++;
+ bluesky_inode_update_ctime(dir, TRUE);
wcc.after.present = TRUE;
encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
+ g_mutex_unlock(file->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(dir->lock);
+
encode_pre_wcc(&result.wccstat3_u.wcc, dir);
if (!validate_filename(argp->name)
|| strcmp(argp->name, "..") == 0)
{
result.status = NFS3ERR_NOENT;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
dir);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
return;
}
+ g_mutex_lock(dir->lock);
+
encode_pre_wcc(&result.wccstat3_u.wcc, dir);
if (!validate_filename(argp->name)
|| strcmp(argp->name, "..") == 0)
{
result.status = NFS3ERR_NOENT;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
BlueSkyInode *inode = bluesky_get_inode(fs, inum);
if (inode == NULL) {
result.status = NFS3ERR_NOENT;
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(inode->lock);
schedule_inode_unref(req, inode);
if (inode->type != BLUESKY_DIRECTORY) {
result.status = NFS3ERR_NOTDIR;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
printf("Directory not empty: %d entries\n",
g_sequence_get_length(inode->dirents));
result.status = NFS3ERR_NOTEMPTY;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
dir);
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(dir1->lock);
encode_pre_wcc(wcc1, dir1);
BlueSkyInode *dir2 = lookup_fh(req, &argp->to.dir);
if (dir2 == NULL) {
result.status = NFS3ERR_STALE;
+ g_mutex_unlock(dir1->lock);
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(dir2->lock);
encode_pre_wcc(wcc2, dir1);
gboolean status = bluesky_rename(dir1, argp->from.name,
else
result.status = NFS3ERR_PERM;
+ g_mutex_unlock(dir2->lock);
+ g_mutex_unlock(dir1->lock);
async_rpc_send_reply(req, &result);
}
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(inode->lock);
BlueSkyInode *dir = lookup_fh(req, &argp->link.dir);
if (dir == NULL) {
result.status = NFS3ERR_STALE;
result.link3res_u.res.linkdir_wcc = wcc;
+ g_mutex_unlock(inode->lock);
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(dir->lock);
encode_pre_wcc(&wcc, dir);
if (dir->type != BLUESKY_DIRECTORY) {
result.status = NFS3ERR_NOTDIR;
result.link3res_u.res.linkdir_wcc = wcc;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
{
result.status = NFS3ERR_EXIST;
result.link3res_u.res.linkdir_wcc = wcc;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
if (!bluesky_directory_insert(dir, argp->link.name, inode->inum)) {
result.status = NFS3ERR_EXIST;
result.link3res_u.res.linkdir_wcc = wcc;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
return;
}
inode->nlink++;
- bluesky_inode_update_ctime(inode, 0);
+ bluesky_inode_update_ctime(inode, FALSE);
+ bluesky_inode_update_ctime(dir, TRUE);
result.status = NFS3_OK;
wcc.after.present = TRUE;
encode_fattr3(&result.link3res_u.res.file_attributes.post_op_attr_u.attributes, inode);
result.link3res_u.res.linkdir_wcc = wcc;
+ g_mutex_unlock(inode->lock);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(dir->lock);
result.status = NFS3_OK;
result.readdir3res_u.resok.dir_attributes.present = TRUE;
result.readdir3res_u.resok.reply.entries = NULL;
result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(dir->lock);
result.status = NFS3_OK;
result.readdirplus3res_u.resok.dir_attributes.present = TRUE;
while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
BlueSkyDirent *d = g_sequence_get(i);
BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
+ g_mutex_lock(inode->lock);
if (inode != NULL) {
dircount += 24 + ((strlen(d->name) + 3) & ~3);
attrcount += 88 + 8 + 8;
if (dircount > argp->dircount
|| dircount + attrcount > argp->maxcount)
+ {
+ g_mutex_unlock(inode->lock);
+ bluesky_inode_unref(inode);
break;
+ }
dirents[count].fileid = d->inum;
dirents[count].name = d->name;
dirents[count].cookie = d->cookie;
if (count > 0)
dirents[count - 1].nextentry = &dirents[count];
count++;
+ g_mutex_unlock(inode->lock);
bluesky_inode_unref(inode);
}
i = g_sequence_iter_next(i);
result.readdirplus3res_u.resok.reply.entries = NULL;
result.readdirplus3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
+ g_mutex_unlock(dir->lock);
async_rpc_send_reply(req, &result);
}
async_rpc_send_reply(req, &result);
return;
}
+ g_mutex_lock(inode->lock);
result.status = NFS3_OK;
result.fsstat3res_u.resok.obj_attributes.present = TRUE;
result.fsstat3res_u.resok.afiles = 0;
result.fsstat3res_u.resok.invarsec = 0;
+ g_mutex_unlock(inode->lock);
async_rpc_send_reply(req, &result);
}
memset(&result, 0, sizeof(result));
BlueSkyInode *inode = bluesky_get_inode(fs, 1);
+ g_mutex_lock(inode->lock);
result.status = NFS3_OK;
result.fsinfo3res_u.resok.obj_attributes.present = TRUE;
encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
result.fsinfo3res_u.resok.time_delta.nseconds = 1000;
result.fsinfo3res_u.resok.properties
= FSF3_LINK | FSF3_SYMLINK | FSF3_HOMOGENEOUS | FSF3_CANSETTIME;
- bluesky_inode_unref(inode);
+ g_mutex_unlock(inode->lock);
+ bluesky_inode_unref(inode);
async_rpc_send_reply(req, &result);
}
memset(&result, 0, sizeof(result));
BlueSkyInode *inode = bluesky_get_inode(fs, 1);
+ g_mutex_lock(inode->lock);
result.status = NFS3_OK;
result.pathconf3res_u.resok.obj_attributes.present = TRUE;
encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
result.pathconf3res_u.resok.chown_restricted = TRUE;
result.pathconf3res_u.resok.case_insensitive = FALSE;
result.pathconf3res_u.resok.case_preserving = TRUE;
- bluesky_inode_unref(inode);
+ g_mutex_unlock(inode->lock);
+ bluesky_inode_unref(inode);
async_rpc_send_reply(req, &result);
}
header.verf_len = 0;
header.accept_stat = htonl(stat);
+ g_mutex_lock(req->connection->send_lock);
uint32_t fragment = htonl(sizeof(header) | 0x80000000);
async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
async_rpc_write(req->connection, (const char *)&header, sizeof(header));
g_io_channel_flush(req->connection->channel, NULL);
+ g_mutex_unlock(req->connection->send_lock);
if (req->args != NULL) {
char buf[4];
header.verf_len = 0;
header.accept_stat = 0;
+ g_mutex_lock(req->connection->send_lock);
gsize msg_size = str->len;
uint32_t fragment = htonl((msg_size + sizeof(header)) | 0x80000000);
async_rpc_write(req->connection, (const char *)&fragment, sizeof(fragment));
async_rpc_write(req->connection, (const char *)&header, sizeof(header));
async_rpc_write(req->connection, str->str, str->len);
g_io_channel_flush(req->connection->channel, NULL);
+ g_mutex_unlock(req->connection->send_lock);
time_end = bluesky_now_hires();
g_free(req);
}
+static const char *nfs_proc_names[] = {
+ [NFSPROC3_NULL] = "NULL",
+ [NFSPROC3_GETATTR] = "GETATTR",
+ [NFSPROC3_SETATTR] = "SETATTR",
+ [NFSPROC3_LOOKUP] = "LOOKUP",
+ [NFSPROC3_ACCESS] = "ACCESS",
+ [NFSPROC3_READLINK] = "READLINK",
+ [NFSPROC3_READ] = "READ",
+ [NFSPROC3_WRITE] = "WRITE",
+ [NFSPROC3_CREATE] = "CREATE",
+ [NFSPROC3_MKDIR] = "MKDIR",
+ [NFSPROC3_SYMLINK] = "SYMLINK",
+ [NFSPROC3_MKNOD] = "MKNOD",
+ [NFSPROC3_REMOVE] = "REMOVE",
+ [NFSPROC3_RMDIR] = "RMDIR",
+ [NFSPROC3_RENAME] = "RENAME",
+ [NFSPROC3_LINK] = "LINK",
+ [NFSPROC3_READDIR] = "READDIR",
+ [NFSPROC3_READDIRPLUS] = "READDIRPLUS",
+ [NFSPROC3_FSSTAT] = "FSSTAT",
+ [NFSPROC3_FSINFO] = "FSINFO",
+ [NFSPROC3_PATHCONF] = "PATHCONF",
+ [NFSPROC3_COMMIT] = "COMMIT",
+};
+
static void
nfs_program_3(RPCRequest *req)
{
xdrproc_t _xdr_argument, _xdr_result;
char *(*local)(char *, RPCRequest *);
- printf("Dispatched NFS RPC message type %d\n", req->req_proc);
+ if (req->req_proc < sizeof(nfs_proc_names) / sizeof(const char *)) {
+ printf("Dispatched NFS RPC message type %s\n",
+ nfs_proc_names[req->req_proc]);
+ } else {
+ printf("Dispatched unknown NFS RPC message type %d\n", req->req_proc);
+ }
switch (req->req_proc) {
case NFSPROC3_NULL:
static GMainContext *main_context;
static GMainLoop *main_loop;
+static GThreadPool *rpc_thread_pool;
+
static gboolean async_flushd(gpointer data)
{
bluesky_flushd_invoke(fs);
return TRUE;
}
+static void async_rpc_task(gpointer data, gpointer user_data)
+{
+ nfs_program_3((RPCRequest *)data);
+}
+
static async_rpc_init()
{
main_context = g_main_context_new();
main_loop = g_main_loop_new(main_context, FALSE);
+ rpc_thread_pool = g_thread_pool_new(async_rpc_task, NULL, -1, FALSE, NULL);
+
/* Arrange to have the cache writeback code run every five seconds. */
GSource *source = g_timeout_source_new_seconds(5);
g_source_set_callback(source, async_flushd, NULL, NULL);
req->req_proc = ntohl(header->proc);
rpc->msgbuf = g_string_new("");
- nfs_program_3(req);
+ g_thread_pool_push(rpc_thread_pool, req, NULL);
return TRUE;
}
fprintf(stderr, "Unexpected error or end of file on RPC stream %d!\n",
g_io_channel_unix_get_fd(rpc->channel));
g_io_channel_shutdown(rpc->channel, TRUE, NULL);
+ /* TODO: Clean up connection object. */
return FALSE;
}
rpc->channel = g_io_channel_unix_new(nfd);
rpc->msgbuf = g_string_new("");
g_io_channel_set_encoding(rpc->channel, NULL, NULL);
+ rpc->send_lock = g_mutex_new();
GSource *source = g_io_create_watch(rpc->channel, G_IO_IN);
g_source_set_callback(source, (GSourceFunc)async_rpc_do_read,
rpc, NULL);