X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=nfs3%2Fnfs3.c;h=9fccd00f2fe31b55121111555b1cf9cc784800a3;hb=3ec75fa45814e987f77224887350a9f736ed5b92;hp=cbf5650a13fba3ebc636d39a84a1c2999ad620e3;hpb=2246171d841d34e6368e340a6b76b7ee9d9a1084;p=bluesky.git diff --git a/nfs3/nfs3.c b/nfs3/nfs3.c index cbf5650..9fccd00 100644 --- a/nfs3/nfs3.c +++ b/nfs3/nfs3.c @@ -5,29 +5,138 @@ */ #include "nfs3_prot.h" +#include "bluesky.h" + +extern BlueSkyFS *fs; static int null_int; static void *null_result = (void *)&null_int; -void encode_fattr3(struct fattr3 *result, uint64_t inum) +/* Check that a string is a valid file name. We require that it be valid + * UTF-8, that it not be empty, and that it not contain embedded forward + * slashes. Also checks that the length of the string is not more than the + * maximum allowed length. This function does allow the names "." and "..". + * Returns TRUE if the string is allowed as a filename. */ +gboolean validate_filename(const char *filename) +{ + if (filename == NULL || filename[0] == '\0') + return FALSE; + if (strlen(filename) > 255) + return FALSE; + if (!g_utf8_validate(filename, -1, NULL)) + return FALSE; + if (strchr(filename, '/') != NULL) + return FALSE; + return TRUE; +} + +/* Look up a BlueSkyInode given an NFS filehandle. Returns NULL if the + * filehandle is invalid. */ +BlueSkyInode *lookup_fh(nfs_fh3 *fh) +{ + BlueSkyInode *inode = NULL; + if (fh->data.data_len == 8) { + uint64_t inum = GUINT64_FROM_BE(*(uint64_t *)(fh->data.data_val)); + inode = bluesky_get_inode(fs, inum); + } + return inode; +} + +int64_t decode_nfstime3(nfstime3 *time) +{ + int64_t result = (int64_t)time->seconds * 1000000; + result += time->nseconds / 1000; + return result; +} + +void set_attributes(BlueSkyInode *inode, sattr3 *attributes) +{ + int64_t now = bluesky_get_current_time(); + + if (attributes->mode.set) { + inode->mode = attributes->mode.set_uint32_u.val; + } + + if (attributes->uid.set) { + inode->uid = attributes->uid.set_uint32_u.val; + } + + if (attributes->gid.set) { + inode->gid = attributes->gid.set_uint32_u.val; + } + + if (attributes->size.set) { + if (inode->type == BLUESKY_REGULAR) { + bluesky_file_truncate(inode, attributes->size.set_uint64_u.val); + inode->mtime = now; + } + } + + switch (attributes->atime.set) { + case DONT_CHANGE: + break; + case SET_TO_SERVER_TIME: + inode->atime = now; + break; + case SET_TO_CLIENT_TIME: + inode->atime = decode_nfstime3(&attributes->atime.set_time_u.time); + break; + } + + switch (attributes->mtime.set) { + case DONT_CHANGE: + break; + case SET_TO_SERVER_TIME: + inode->mtime = now; + break; + case SET_TO_CLIENT_TIME: + inode->mtime = decode_nfstime3(&attributes->mtime.set_time_u.time); + break; + } + + inode->ctime = now; + inode->change_count++; +} + +/* Copy inode attributes into NFS response. The BlueSkyInode should be locked + * by the caller. */ +void encode_fattr3(struct fattr3 *result, BlueSkyInode *inode) { - result->type = NF3DIR; - result->mode = 0755; - result->nlink = 1; - result->uid = 0; - result->gid = 0; - result->size = 0; + result->type = inode->type; + result->mode = inode->mode; + result->nlink = inode->nlink; + result->uid = inode->uid; + result->gid = inode->gid; + result->size = inode->size; result->used = 0; result->rdev.major = 0; result->rdev.minor = 0; result->fsid = 0; - result->fileid = inum; - result->atime.seconds = 0; - result->atime.nseconds = 0; - result->mtime.seconds = 0; - result->mtime.nseconds = 0; - result->ctime.seconds = 0; - result->ctime.nseconds = 0; + result->fileid = inode->inum; + result->atime.seconds = inode->atime / 1000000; + result->atime.nseconds = (inode->atime % 1000000) * 1000; + result->mtime.seconds = inode->mtime / 1000000; + result->mtime.nseconds = (inode->mtime % 1000000) * 1000; + result->ctime.seconds = inode->ctime / 1000000; + result->ctime.nseconds = (inode->ctime % 1000000) * 1000; + + switch (inode->type) { + case BLUESKY_SYMLINK: + result->size = strlen(inode->symlink_contents); + break; + default: + break; + } +} + +void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode) +{ + wcc->before.present = TRUE; + wcc->before.pre_op_attr_u.attributes.size = inode->size; + wcc->before.pre_op_attr_u.attributes.mtime.seconds = inode->mtime / 1000000; + wcc->before.pre_op_attr_u.attributes.mtime.nseconds = (inode->mtime % 1000000) * 1000; + wcc->before.pre_op_attr_u.attributes.ctime.seconds = inode->ctime / 1000000; + wcc->before.pre_op_attr_u.attributes.ctime.nseconds = (inode->ctime % 1000000) * 1000; } void * @@ -41,8 +150,13 @@ nfsproc3_getattr_3_svc(nfs_fh3 *argp, struct svc_req *rqstp) { static getattr3res result; - result.status = NFS3_OK; - encode_fattr3(&result.getattr3res_u.attributes, 1); + BlueSkyInode *inode = lookup_fh(argp); + if (inode != NULL) { + result.status = NFS3_OK; + encode_fattr3(&result.getattr3res_u.attributes, inode); + } else { + result.status = NFS3ERR_STALE; + } return &result; } @@ -52,7 +166,30 @@ nfsproc3_setattr_3_svc(setattr3args *argp, struct svc_req *rqstp) { static wccstat3 result; - result.status = NFS3ERR_NOTSUPP; + result.wccstat3_u.wcc.before.present = FALSE; + result.wccstat3_u.wcc.after.present = FALSE; + BlueSkyInode *inode = lookup_fh(&argp->object); + if (inode == NULL) { + result.status = NFS3ERR_STALE; + return &result; + } + + 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); + return &result; + } + } + + set_attributes(inode, &argp->new_attributes); + + result.wccstat3_u.wcc.after.present = TRUE; + encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, + inode); + result.status = NFS3_OK; return &result; } @@ -62,7 +199,45 @@ nfsproc3_lookup_3_svc(diropargs3 *argp, struct svc_req *rqstp) { static lookup3res result; - result.status = NFS3ERR_NOTSUPP; + BlueSkyInode *dir = lookup_fh(&argp->dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.lookup3res_u.resfail.present = FALSE; + return &result; + } + + result.lookup3res_u.resfail.present = TRUE; + encode_fattr3(&result.lookup3res_u.resfail.post_op_attr_u.attributes, dir); + if (!validate_filename(argp->name)) { + if (strlen(argp->name) > 255) + result.status = NFS3ERR_NAMETOOLONG; + else + result.status = NFS3ERR_NOENT; + return &result; + } + + /* TODO: Special-case "." and "..". */ + uint64_t inum = bluesky_directory_lookup(dir, argp->name); + if (inum == 0) { + result.status = NFS3ERR_NOENT; + return &result; + } + BlueSkyInode *inode = bluesky_get_inode(fs, inum); + if (inode == NULL) { + result.status = NFS3ERR_NOENT; + return &result; + } + + result.status = NFS3_OK; + result.lookup3res_u.resok.dir_attributes.present = TRUE; + encode_fattr3(&result.lookup3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir); + result.lookup3res_u.resok.obj_attributes.present = TRUE; + encode_fattr3(&result.lookup3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode); + + static uint64_t fh_bytes; + fh_bytes = GUINT64_TO_BE(inum); + result.lookup3res_u.resok.object.data.data_len = 8; + result.lookup3res_u.resok.object.data.data_val = (char *)&fh_bytes; return &result; } @@ -72,7 +247,17 @@ nfsproc3_access_3_svc(access3args *argp, struct svc_req *rqstp) { static access3res result; - result.status = NFS3ERR_NOTSUPP; + BlueSkyInode *inode = lookup_fh(&argp->object); + if (inode == NULL) { + result.status = NFS3ERR_STALE; + result.access3res_u.resfail.present = FALSE; + return &result; + } + + 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; return &result; } @@ -81,8 +266,23 @@ readlink3res * nfsproc3_readlink_3_svc(nfs_fh3 *argp, struct svc_req *rqstp) { static readlink3res result; - - result.status = NFS3ERR_NOTSUPP; + memset(&result, 0, sizeof(result)); + + BlueSkyInode *inode = lookup_fh(argp); + if (inode != NULL) { + if (inode->type == BLUESKY_SYMLINK) { + result.status = NFS3_OK; + result.readlink3res_u.resok.symlink_attributes.present = TRUE; + encode_fattr3(&result.readlink3res_u.resok.symlink_attributes.post_op_attr_u.attributes, inode); + result.readlink3res_u.resok.data = inode->symlink_contents; + } else { + result.status = NFS3ERR_INVAL; + result.readlink3res_u.resfail.present = TRUE; + encode_fattr3(&result.readlink3res_u.resfail.post_op_attr_u.attributes, inode); + } + } else { + result.status = NFS3ERR_STALE; + } return &result; } @@ -91,8 +291,35 @@ read3res * nfsproc3_read_3_svc(read3args *argp, struct svc_req *rqstp) { static read3res result; + static char buf[32768]; + + BlueSkyInode *inode = lookup_fh(&argp->file); + if (inode == NULL) { + result.status = NFS3ERR_STALE; + result.read3res_u.resfail.present = FALSE; + return &result; + } + + int count = argp->count; + if (argp->offset >= inode->size) { + count = 0; + result.read3res_u.resok.eof = TRUE; + } else { + count = MIN(count, inode->size - argp->offset); + if (argp->offset + count == inode->size) + result.read3res_u.resok.eof = TRUE; + else + result.read3res_u.resok.eof = FALSE; + + bluesky_file_read(inode, argp->offset, buf, count); + } - result.status = NFS3ERR_NOTSUPP; + result.status = NFS3_OK; + result.read3res_u.resok.file_attributes.present = TRUE; + encode_fattr3(&result.read3res_u.resok.file_attributes.post_op_attr_u.attributes, inode); + result.read3res_u.resok.count = count; + result.read3res_u.resok.data.data_val = buf; + result.read3res_u.resok.data.data_len = count; return &result; } @@ -101,8 +328,40 @@ write3res * nfsproc3_write_3_svc(write3args *argp, struct svc_req *rqstp) { static write3res result; - - result.status = NFS3ERR_NOTSUPP; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); + + BlueSkyInode *inode = lookup_fh(&argp->file); + if (inode == NULL) { + result.status = NFS3ERR_STALE; + result.write3res_u.resfail = wcc; + return &result; + } + + encode_pre_wcc(&wcc, inode); + if (inode->type != BLUESKY_REGULAR) { + result.status = NFS3ERR_INVAL; + result.write3res_u.resfail = wcc; + return &result; + } + + uint64_t lastbyte = argp->offset + argp->count; + if (lastbyte > inode->size) { + bluesky_file_truncate(inode, lastbyte); + } + + if (argp->data.data_len < argp->count) { + /* ??? */ + } else { + bluesky_file_write(inode, argp->offset, + argp->data.data_val, argp->count); + } + + wcc.after.present = TRUE; + encode_fattr3(&wcc.after.post_op_attr_u.attributes, inode); + result.write3res_u.resok.file_wcc = wcc; + result.write3res_u.resok.count = argp->count; + result.write3res_u.resok.committed = FILE_SYNC; return &result; } @@ -111,8 +370,59 @@ diropres3 * nfsproc3_create_3_svc(create3args *argp, struct svc_req *rqstp) { static diropres3 result; - - result.status = NFS3ERR_NOTSUPP; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); + + BlueSkyInode *dir = lookup_fh(&argp->where.dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.diropres3_u.resfail = wcc; + return &result; + } + + encode_pre_wcc(&wcc, dir); + if (dir->type != BLUESKY_DIRECTORY) { + result.status = NFS3ERR_NOTDIR; + result.diropres3_u.resfail = wcc; + return &result; + } + + if (!validate_filename(argp->where.name) + || strcmp(argp->where.name, ".") == 0 + || strcmp(argp->where.name, "..") == 0) + { + result.status = NFS3ERR_EXIST; + result.diropres3_u.resfail = wcc; + return &result; + } + + BlueSkyInode *file; + file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_REGULAR); + file->nlink = 1; + file->mode = 0755; + int64_t time = bluesky_get_current_time(); + printf("time: %"PRIi64"\n", time); + file->mtime = time; + file->ctime = time; + file->atime = time; + file->ntime = time; + 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++; + + wcc.after.present = TRUE; + encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir); + result.diropres3_u.resok.obj_attributes.present = TRUE; + encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file); + result.diropres3_u.resok.dir_wcc = wcc; + + static uint64_t fh_bytes; + fh_bytes = GUINT64_TO_BE(file->inum); + result.diropres3_u.resok.obj.present = TRUE; + 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; return &result; } @@ -121,8 +431,59 @@ diropres3 * nfsproc3_mkdir_3_svc(mkdir3args *argp, struct svc_req *rqstp) { static diropres3 result; - - result.status = NFS3ERR_NOTSUPP; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); + + BlueSkyInode *dir = lookup_fh(&argp->where.dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.diropres3_u.resfail = wcc; + return &result; + } + + encode_pre_wcc(&wcc, dir); + if (dir->type != BLUESKY_DIRECTORY) { + result.status = NFS3ERR_NOTDIR; + result.diropres3_u.resfail = wcc; + return &result; + } + + if (!validate_filename(argp->where.name) + || strcmp(argp->where.name, ".") == 0 + || strcmp(argp->where.name, "..") == 0) + { + result.status = NFS3ERR_EXIST; + result.diropres3_u.resfail = wcc; + return &result; + } + + BlueSkyInode *file; + file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_DIRECTORY); + file->nlink = 1; + file->mode = 0755; + int64_t time = bluesky_get_current_time(); + file->mtime = time; + file->ctime = time; + file->atime = time; + file->ntime = time; + 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++; + + wcc.after.present = TRUE; + encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir); + result.diropres3_u.resok.obj_attributes.present = TRUE; + encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file); + result.diropres3_u.resok.dir_wcc = wcc; + + static uint64_t fh_bytes; + fh_bytes = GUINT64_TO_BE(file->inum); + result.diropres3_u.resok.obj.present = TRUE; + 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; return &result; } @@ -131,8 +492,59 @@ diropres3 * nfsproc3_symlink_3_svc(symlink3args *argp, struct svc_req *rqstp) { static diropres3 result; - - result.status = NFS3ERR_NOTSUPP; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); + + BlueSkyInode *dir = lookup_fh(&argp->where.dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.diropres3_u.resfail = wcc; + return &result; + } + + encode_pre_wcc(&wcc, dir); + if (dir->type != BLUESKY_DIRECTORY) { + result.status = NFS3ERR_NOTDIR; + result.diropres3_u.resfail = wcc; + return &result; + } + + if (!validate_filename(argp->where.name) + || strcmp(argp->where.name, ".") == 0 + || strcmp(argp->where.name, "..") == 0) + { + result.status = NFS3ERR_EXIST; + result.diropres3_u.resfail = wcc; + return &result; + } + + BlueSkyInode *file; + file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_SYMLINK); + file->nlink = 1; + file->mode = 0755; + int64_t time = bluesky_get_current_time(); + file->mtime = time; + file->ctime = time; + file->atime = time; + file->ntime = time; + file->symlink_contents = g_strdup(argp->symlink.symlink_data); + 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++; + + wcc.after.present = TRUE; + encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir); + result.diropres3_u.resok.obj_attributes.present = TRUE; + encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file); + result.diropres3_u.resok.dir_wcc = wcc; + + static uint64_t fh_bytes; + fh_bytes = GUINT64_TO_BE(file->inum); + result.diropres3_u.resok.obj.present = TRUE; + 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; return &result; } @@ -152,7 +564,33 @@ nfsproc3_remove_3_svc(diropargs3 *argp, struct svc_req *rqstp) { static wccstat3 result; - result.status = NFS3ERR_NOTSUPP; + result.wccstat3_u.wcc.before.present = FALSE; + result.wccstat3_u.wcc.after.present = FALSE; + + BlueSkyInode *dir = lookup_fh(&argp->dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + return &result; + } + + encode_pre_wcc(&result.wccstat3_u.wcc, dir); + + if (!validate_filename(argp->name) + || strcmp(argp->name, ".") == 0 + || strcmp(argp->name, "..") == 0) + { + result.status = NFS3ERR_NOENT; + return &result; + } + + /* TODO: Decrement link count, deallocate inode if needed. */ + + bluesky_directory_remove(dir, argp->name); + + result.status = NFS3_OK; + result.wccstat3_u.wcc.after.present = TRUE; + encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, + dir); return &result; } @@ -162,7 +600,51 @@ nfsproc3_rmdir_3_svc(diropargs3 *argp, struct svc_req *rqstp) { static wccstat3 result; - result.status = NFS3ERR_NOTSUPP; + result.wccstat3_u.wcc.before.present = FALSE; + result.wccstat3_u.wcc.after.present = FALSE; + + BlueSkyInode *dir = lookup_fh(&argp->dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + return &result; + } + + encode_pre_wcc(&result.wccstat3_u.wcc, dir); + + if (!validate_filename(argp->name) + || strcmp(argp->name, ".") == 0 + || strcmp(argp->name, "..") == 0) + { + result.status = NFS3ERR_NOENT; + return &result; + } + + uint64_t inum = bluesky_directory_lookup(dir, argp->name); + BlueSkyInode *inode = bluesky_get_inode(fs, inum); + if (inode == NULL) { + result.status = NFS3ERR_NOENT; + return &result; + } + + if (inode->type != BLUESKY_DIRECTORY) { + result.status = NFS3ERR_NOTDIR; + return &result; + } + if (g_sequence_get_length(inode->dirents) > 0) { + printf("Directory not empty: %d entries\n", + g_sequence_get_length(inode->dirents)); + result.status = NFS3ERR_NOTEMPTY; + return &result; + } + + /* TODO: Decrement link count, deallocate inode if needed. */ + + bluesky_directory_remove(dir, argp->name); + + result.status = NFS3_OK; + result.wccstat3_u.wcc.after.present = TRUE; + encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, + dir); return &result; } @@ -171,8 +653,37 @@ rename3res * nfsproc3_rename_3_svc(rename3args *argp, struct svc_req *rqstp) { static rename3res result; - - result.status = NFS3ERR_NOTSUPP; + wcc_data *wcc1 = &result.rename3res_u.res.fromdir_wcc; + wcc_data *wcc2 = &result.rename3res_u.res.todir_wcc; + memset(wcc1, 0, sizeof(*wcc1)); + memset(wcc2, 0, sizeof(*wcc2)); + + BlueSkyInode *dir1 = lookup_fh(&argp->from.dir); + if (dir1 == NULL) { + result.status = NFS3ERR_STALE; + return &result; + } + encode_pre_wcc(wcc1, dir1); + + BlueSkyInode *dir2 = lookup_fh(&argp->to.dir); + if (dir2 == NULL) { + result.status = NFS3ERR_STALE; + return &result; + } + encode_pre_wcc(wcc2, dir1); + + gboolean status = bluesky_rename(dir1, argp->from.name, + dir2, argp->to.name, + TRUE, TRUE); + + wcc1->after.present = TRUE; + encode_fattr3(&wcc1->after.post_op_attr_u.attributes, dir1); + wcc2->after.present = TRUE; + encode_fattr3(&wcc2->after.post_op_attr_u.attributes, dir2); + if (status) + result.status = NFS3_OK; + else + result.status = NFS3ERR_PERM; return &result; } @@ -181,18 +692,104 @@ link3res * nfsproc3_link_3_svc(link3args *argp, struct svc_req *rqstp) { static link3res result; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); + + BlueSkyInode *inode = lookup_fh(&argp->file); + if (inode == NULL) { + result.status = NFS3ERR_STALE; + result.link3res_u.res.linkdir_wcc = wcc; + return &result; + } + + BlueSkyInode *dir = lookup_fh(&argp->link.dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.link3res_u.res.linkdir_wcc = wcc; + return &result; + } + + encode_pre_wcc(&wcc, dir); + if (dir->type != BLUESKY_DIRECTORY) { + result.status = NFS3ERR_NOTDIR; + result.link3res_u.res.linkdir_wcc = wcc; + return &result; + } + + if (!validate_filename(argp->link.name) + || strcmp(argp->link.name, ".") == 0 + || strcmp(argp->link.name, "..") == 0 + || bluesky_directory_lookup(dir, argp->link.name) != 0) + { + result.status = NFS3ERR_EXIST; + result.link3res_u.res.linkdir_wcc = wcc; + return &result; + } + + if (!bluesky_directory_insert(dir, argp->link.name, inode->inum)) { + result.status = NFS3ERR_EXIST; + result.link3res_u.res.linkdir_wcc = wcc; + return &result; + } + inode->nlink++; + bluesky_inode_update_ctime(inode, 0); - result.status = NFS3ERR_NOTSUPP; + result.status = NFS3_OK; + wcc.after.present = TRUE; + encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir); + result.link3res_u.res.file_attributes.present = TRUE; + encode_fattr3(&result.link3res_u.res.file_attributes.post_op_attr_u.attributes, inode); + result.link3res_u.res.linkdir_wcc = wcc; return &result; } +gint bluesky_dirent_compare(gconstpointer a, gconstpointer b, + gpointer unused); + +#define MAX_READDIR_DIRENTS 64 readdir3res * nfsproc3_readdir_3_svc(readdir3args *argp, struct svc_req *rqstp) { static readdir3res result; - result.status = NFS3ERR_NOTSUPP; + BlueSkyInode *dir = lookup_fh(&argp->dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.readdir3res_u.resfail.present = FALSE; + return &result; + } + + result.status = NFS3_OK; + result.readdir3res_u.resok.dir_attributes.present = TRUE; + encode_fattr3(&result.readdir3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir); + memset(result.readdir3res_u.resok.cookieverf, 0, + sizeof(result.readdir3res_u.resok.cookieverf)); + + static entry3 dirents[MAX_READDIR_DIRENTS]; + int count = 0; + + BlueSkyDirent start = {NULL, NULL, argp->cookie, 0}; + GSequenceIter *i = g_sequence_search(dir->dirents, &start, + bluesky_dirent_compare, NULL); + + while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) { + BlueSkyDirent *d = g_sequence_get(i); + dirents[count].fileid = d->inum; + dirents[count].name = d->name; + dirents[count].cookie = d->cookie; + dirents[count].nextentry = NULL; + if (count > 0) + dirents[count - 1].nextentry = &dirents[count]; + i = g_sequence_iter_next(i); + count++; + } + + if (count > 0) + result.readdir3res_u.resok.reply.entries = &dirents[0]; + else + result.readdir3res_u.resok.reply.entries = NULL; + result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i); return &result; } @@ -200,9 +797,70 @@ nfsproc3_readdir_3_svc(readdir3args *argp, struct svc_req *rqstp) readdirplus3res * nfsproc3_readdirplus_3_svc(readdirplus3args *argp, struct svc_req *rqstp) { + /* XDR-encoded sizes: + * post_op_attr: 88 bytes + * base readdirplus3resok: 88 + 16 bytes + * base directory entry: 24 bytes + filename + * attributes/fh3: 88 + 8 + filehandle size + */ + size_t dircount = 88 + 16, attrcount = 0; static readdirplus3res result; - result.status = NFS3ERR_NOTSUPP; + BlueSkyInode *dir = lookup_fh(&argp->dir); + if (dir == NULL) { + result.status = NFS3ERR_STALE; + result.readdirplus3res_u.resfail.present = FALSE; + return &result; + } + + result.status = NFS3_OK; + result.readdirplus3res_u.resok.dir_attributes.present = TRUE; + encode_fattr3(&result.readdirplus3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir); + memset(result.readdirplus3res_u.resok.cookieverf, 0, + sizeof(result.readdirplus3res_u.resok.cookieverf)); + + static entryplus3 dirents[MAX_READDIR_DIRENTS]; + static uint64_t fh_bytes[MAX_READDIR_DIRENTS]; + int count = 0; + + /* TODO: Handle dircount, maxcount arguments from client. */ + + BlueSkyDirent start = {NULL, NULL, argp->cookie, 0}; + GSequenceIter *i = g_sequence_search(dir->dirents, &start, + bluesky_dirent_compare, NULL); + + 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); + if (inode != NULL) { + dircount += 24 + ((strlen(d->name) + 3) & ~3); + attrcount += 88 + 8 + 8; + if (dircount > argp->dircount + || dircount + attrcount > argp->maxcount) + break; + dirents[count].fileid = d->inum; + dirents[count].name = d->name; + dirents[count].cookie = d->cookie; + dirents[count].nextentry = NULL; + dirents[count].name_attributes.present = TRUE; + encode_fattr3(&dirents[count].name_attributes.post_op_attr_u.attributes, inode); + fh_bytes[count] = GUINT64_TO_BE(d->inum); + dirents[count].name_handle.present = TRUE; + dirents[count].name_handle.post_op_fh3_u.handle.data.data_len = 8; + dirents[count].name_handle.post_op_fh3_u.handle.data.data_val + = (char *)&fh_bytes[count]; + if (count > 0) + dirents[count - 1].nextentry = &dirents[count]; + count++; + } + i = g_sequence_iter_next(i); + } + + if (count > 0) + result.readdirplus3res_u.resok.reply.entries = &dirents[0]; + else + result.readdirplus3res_u.resok.reply.entries = NULL; + result.readdirplus3res_u.resok.reply.eof = g_sequence_iter_is_end(i); return &result; } @@ -212,7 +870,24 @@ nfsproc3_fsstat_3_svc(nfs_fh3 *argp, struct svc_req *rqstp) { static fsstat3res result; - result.status = NFS3ERR_NOTSUPP; + BlueSkyInode *inode = lookup_fh(argp); + if (inode == NULL) { + result.status = NFS3ERR_STALE; + result.fsstat3res_u.resfail.present = FALSE; + return &result; + } + + result.status = NFS3_OK; + result.fsstat3res_u.resok.obj_attributes.present = TRUE; + encode_fattr3(&result.fsstat3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode); + + result.fsstat3res_u.resok.tbytes = (1 << 30); + result.fsstat3res_u.resok.fbytes = (1 << 30); + result.fsstat3res_u.resok.abytes = (1 << 30); + result.fsstat3res_u.resok.tfiles = 0; + result.fsstat3res_u.resok.ffiles = 0; + result.fsstat3res_u.resok.afiles = 0; + result.fsstat3res_u.resok.invarsec = 0; return &result; } @@ -222,9 +897,10 @@ nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, struct svc_req *rqstp) { static fsinfo3res result; + BlueSkyInode *inode = bluesky_get_inode(fs, 1); 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, 1); + encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode); result.fsinfo3res_u.resok.rtmax = 32768; result.fsinfo3res_u.resok.rtpref = 32768; result.fsinfo3res_u.resok.rtmult = 4096; @@ -246,9 +922,10 @@ nfsproc3_pathconf_3_svc(nfs_fh3 *argp, struct svc_req *rqstp) { static pathconf3res result; + BlueSkyInode *inode = bluesky_get_inode(fs, 1); 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, 1); + encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode); result.pathconf3res_u.resok.linkmax = 0xffffffff; result.pathconf3res_u.resok.name_max = 255; result.pathconf3res_u.resok.no_trunc = TRUE;