static int null_int;
static void *null_result = (void *)&null_int;
+/* 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 te 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)
result->ctime.nseconds = (inode->ctime % 1000000) * 1000;
}
+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 *
nfsproc3_null_3_svc(void *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;
}
{
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;
}
nfsproc3_read_3_svc(read3args *argp, struct svc_req *rqstp)
{
static read3res result;
+ static char buf[32768];
- result.status = NFS3ERR_NOTSUPP;
+ 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 = 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;
}
nfsproc3_write_3_svc(write3args *argp, struct svc_req *rqstp)
{
static write3res 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.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);
+ }
+
+ 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;
}
nfsproc3_create_3_svc(create3args *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_REGULAR);
+ file->nlink = 1;
+ file->mode = 0755;
+ int64_t time = bluesky_get_current_time();
+ printf("time: %lld\n", time);
+ file->mtime = time;
+ file->ctime = time;
+ file->atime = time;
+ bluesky_insert_inode(fs, file);
+ bluesky_directory_insert(dir, argp->where.name, file->inum);
+
+ 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;
}
return &result;
}
+gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
+ gpointer unused);
+
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));
+
+#define MAX_READDIR_DIRENTS 4
+ static entry3 dirents[MAX_READDIR_DIRENTS];
+ int count = 0;
+
+ BlueSkyDirent start = {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;
}