X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=nfs3%2Fnfs3.c;h=2a25e0bdfc0c995be90a2adb7f9e91fca31cac61;hb=6f5f0f5a8d42eee4270c93d89af61586128c2cfe;hp=a5439f8c5894c6dfa5d5e03f0812b4c043678e10;hpb=14a09b1c9822ee4a86a009eee212da8fdc325d3f;p=bluesky.git diff --git a/nfs3/nfs3.c b/nfs3/nfs3.c index a5439f8..2a25e0b 100644 --- a/nfs3/nfs3.c +++ b/nfs3/nfs3.c @@ -16,7 +16,7 @@ static void *null_result = (void *)&null_int; * 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 te string is allowed as a filename. */ + * Returns TRUE if the string is allowed as a filename. */ gboolean validate_filename(const char *filename) { if (filename == NULL || filename[0] == '\0') @@ -49,6 +49,55 @@ int64_t decode_nfstime3(nfstime3 *time) 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) @@ -70,6 +119,14 @@ void encode_fattr3(struct fattr3 *result, BlueSkyInode *inode) 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) @@ -81,6 +138,7 @@ void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode) 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 * nfsproc3_null_3_svc(void *argp, struct svc_req *rqstp) { @@ -126,50 +184,7 @@ nfsproc3_setattr_3_svc(setattr3args *argp, struct svc_req *rqstp) } } - if (argp->new_attributes.mode.set) { - inode->mode = argp->new_attributes.mode.set_uint32_u.val; - } - - if (argp->new_attributes.uid.set) { - inode->uid = argp->new_attributes.uid.set_uint32_u.val; - } - - if (argp->new_attributes.gid.set) { - inode->gid = argp->new_attributes.gid.set_uint32_u.val; - } - - int64_t now = bluesky_get_current_time(); - - if (argp->new_attributes.size.set) { - bluesky_file_truncate(inode, - argp->new_attributes.size.set_uint64_u.val); - inode->mtime = now; - } - - switch (argp->new_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(&argp->new_attributes.atime.set_time_u.time); - break; - } - - switch (argp->new_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(&argp->new_attributes.mtime.set_time_u.time); - break; - } - - inode->ctime = now; - inode->change_count++; + 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, @@ -251,8 +266,23 @@ readlink3res * nfsproc3_readlink_3_svc(nfs_fh3 *argp, struct svc_req *rqstp) { static readlink3res result; + memset(&result, 0, sizeof(result)); - result.status = NFS3ERR_NOTSUPP; + 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; } @@ -370,13 +400,17 @@ nfsproc3_create_3_svc(create3args *argp, struct svc_req *rqstp) file->nlink = 1; file->mode = 0755; int64_t time = bluesky_get_current_time(); - printf("time: %lld\n", 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; @@ -396,8 +430,59 @@ diropres3 * nfsproc3_mkdir_3_svc(mkdir3args *argp, struct svc_req *rqstp) { static diropres3 result; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); - result.status = NFS3ERR_NOTSUPP; + 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; } @@ -406,8 +491,59 @@ diropres3 * nfsproc3_symlink_3_svc(symlink3args *argp, struct svc_req *rqstp) { static diropres3 result; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); - result.status = NFS3ERR_NOTSUPP; + 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; } @@ -427,7 +563,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; } @@ -456,8 +618,54 @@ link3res * nfsproc3_link_3_svc(link3args *argp, struct svc_req *rqstp) { static link3res result; + struct wcc_data wcc; + memset(&wcc, 0, sizeof(wcc)); - result.status = NFS3ERR_NOTSUPP; + 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 = 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; } @@ -487,7 +695,7 @@ nfsproc3_readdir_3_svc(readdir3args *argp, struct svc_req *rqstp) static entry3 dirents[MAX_READDIR_DIRENTS]; int count = 0; - BlueSkyDirent start = {NULL, argp->cookie, 0}; + BlueSkyDirent start = {NULL, NULL, argp->cookie, 0}; GSequenceIter *i = g_sequence_search(dir->dirents, &start, bluesky_dirent_compare, NULL);