2 * This is sample code generated by rpcgen.
3 * These are only templates and you can use them
4 * as a guideline for developing your own functions.
13 static void *null_result = (void *)&null_int;
15 /* Check that a string is a valid file name. We require that it be valid
16 * UTF-8, that it not be empty, and that it not contain embedded forward
17 * slashes. Also checks that the length of the string is not more than the
18 * maximum allowed length. This function does allow the names "." and "..".
19 * Returns TRUE if te string is allowed as a filename. */
20 gboolean validate_filename(const char *filename)
22 if (filename == NULL || filename[0] == '\0')
24 if (strlen(filename) > 255)
26 if (!g_utf8_validate(filename, -1, NULL))
28 if (strchr(filename, '/') != NULL)
33 /* Look up a BlueSkyInode given an NFS filehandle. Returns NULL if the
34 * filehandle is invalid. */
35 BlueSkyInode *lookup_fh(nfs_fh3 *fh)
37 BlueSkyInode *inode = NULL;
38 if (fh->data.data_len == 8) {
39 uint64_t inum = GUINT64_FROM_BE(*(uint64_t *)(fh->data.data_val));
40 inode = bluesky_get_inode(fs, inum);
45 int64_t decode_nfstime3(nfstime3 *time)
47 int64_t result = (int64_t)time->seconds * 1000000;
48 result += time->nseconds / 1000;
52 /* Copy inode attributes into NFS response. The BlueSkyInode should be locked
54 void encode_fattr3(struct fattr3 *result, BlueSkyInode *inode)
56 result->type = inode->type;
57 result->mode = inode->mode;
58 result->nlink = inode->nlink;
59 result->uid = inode->uid;
60 result->gid = inode->gid;
61 result->size = inode->size;
63 result->rdev.major = 0;
64 result->rdev.minor = 0;
66 result->fileid = inode->inum;
67 result->atime.seconds = inode->atime / 1000000;
68 result->atime.nseconds = (inode->atime % 1000000) * 1000;
69 result->mtime.seconds = inode->mtime / 1000000;
70 result->mtime.nseconds = (inode->mtime % 1000000) * 1000;
71 result->ctime.seconds = inode->ctime / 1000000;
72 result->ctime.nseconds = (inode->ctime % 1000000) * 1000;
75 void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode)
77 wcc->before.present = TRUE;
78 wcc->before.pre_op_attr_u.attributes.size = inode->size;
79 wcc->before.pre_op_attr_u.attributes.mtime.seconds = inode->mtime / 1000000;
80 wcc->before.pre_op_attr_u.attributes.mtime.nseconds = (inode->mtime % 1000000) * 1000;
81 wcc->before.pre_op_attr_u.attributes.ctime.seconds = inode->ctime / 1000000;
82 wcc->before.pre_op_attr_u.attributes.ctime.nseconds = (inode->ctime % 1000000) * 1000;
85 nfsproc3_null_3_svc(void *argp, struct svc_req *rqstp)
91 nfsproc3_getattr_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
93 static getattr3res result;
95 BlueSkyInode *inode = lookup_fh(argp);
97 result.status = NFS3_OK;
98 encode_fattr3(&result.getattr3res_u.attributes, inode);
100 result.status = NFS3ERR_STALE;
107 nfsproc3_setattr_3_svc(setattr3args *argp, struct svc_req *rqstp)
109 static wccstat3 result;
111 result.wccstat3_u.wcc.before.present = FALSE;
112 result.wccstat3_u.wcc.after.present = FALSE;
113 BlueSkyInode *inode = lookup_fh(&argp->object);
115 result.status = NFS3ERR_STALE;
119 encode_pre_wcc(&result.wccstat3_u.wcc, inode);
120 if (argp->guard.check) {
121 if (inode->ctime != decode_nfstime3(&argp->guard.sattrguard3_u.ctime)) {
122 result.status = NFS3ERR_NOT_SYNC;
123 result.wccstat3_u.wcc.after.present = TRUE;
124 encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, inode);
129 if (argp->new_attributes.mode.set) {
130 inode->mode = argp->new_attributes.mode.set_uint32_u.val;
133 if (argp->new_attributes.uid.set) {
134 inode->uid = argp->new_attributes.uid.set_uint32_u.val;
137 if (argp->new_attributes.gid.set) {
138 inode->gid = argp->new_attributes.gid.set_uint32_u.val;
141 int64_t now = bluesky_get_current_time();
143 if (argp->new_attributes.size.set) {
144 bluesky_file_truncate(inode,
145 argp->new_attributes.size.set_uint64_u.val);
149 switch (argp->new_attributes.atime.set) {
152 case SET_TO_SERVER_TIME:
155 case SET_TO_CLIENT_TIME:
156 inode->atime = decode_nfstime3(&argp->new_attributes.atime.set_time_u.time);
160 switch (argp->new_attributes.mtime.set) {
163 case SET_TO_SERVER_TIME:
166 case SET_TO_CLIENT_TIME:
167 inode->mtime = decode_nfstime3(&argp->new_attributes.mtime.set_time_u.time);
172 inode->change_count++;
174 result.wccstat3_u.wcc.after.present = TRUE;
175 encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
177 result.status = NFS3_OK;
183 nfsproc3_lookup_3_svc(diropargs3 *argp, struct svc_req *rqstp)
185 static lookup3res result;
187 BlueSkyInode *dir = lookup_fh(&argp->dir);
189 result.status = NFS3ERR_STALE;
190 result.lookup3res_u.resfail.present = FALSE;
194 result.lookup3res_u.resfail.present = TRUE;
195 encode_fattr3(&result.lookup3res_u.resfail.post_op_attr_u.attributes, dir);
196 if (!validate_filename(argp->name)) {
197 if (strlen(argp->name) > 255)
198 result.status = NFS3ERR_NAMETOOLONG;
200 result.status = NFS3ERR_NOENT;
204 /* TODO: Special-case "." and "..". */
205 uint64_t inum = bluesky_directory_lookup(dir, argp->name);
207 result.status = NFS3ERR_NOENT;
210 BlueSkyInode *inode = bluesky_get_inode(fs, inum);
212 result.status = NFS3ERR_NOENT;
216 result.status = NFS3_OK;
217 result.lookup3res_u.resok.dir_attributes.present = TRUE;
218 encode_fattr3(&result.lookup3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
219 result.lookup3res_u.resok.obj_attributes.present = TRUE;
220 encode_fattr3(&result.lookup3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
222 static uint64_t fh_bytes;
223 fh_bytes = GUINT64_TO_BE(inum);
224 result.lookup3res_u.resok.object.data.data_len = 8;
225 result.lookup3res_u.resok.object.data.data_val = (char *)&fh_bytes;
231 nfsproc3_access_3_svc(access3args *argp, struct svc_req *rqstp)
233 static access3res result;
235 BlueSkyInode *inode = lookup_fh(&argp->object);
237 result.status = NFS3ERR_STALE;
238 result.access3res_u.resfail.present = FALSE;
242 result.status = NFS3_OK;
243 result.access3res_u.resok.obj_attributes.present = TRUE;
244 encode_fattr3(&result.access3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
245 result.access3res_u.resok.access = argp->access;
251 nfsproc3_readlink_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
253 static readlink3res result;
255 result.status = NFS3ERR_NOTSUPP;
261 nfsproc3_read_3_svc(read3args *argp, struct svc_req *rqstp)
263 static read3res result;
264 static char buf[32768];
266 BlueSkyInode *inode = lookup_fh(&argp->file);
268 result.status = NFS3ERR_STALE;
269 result.read3res_u.resfail.present = FALSE;
273 int count = argp->count;
274 if (argp->offset >= inode->size) {
276 result.read3res_u.resok.eof = TRUE;
278 count = MIN(count, inode->size - argp->offset);
279 if (argp->offset + count == inode->size)
280 result.read3res_u.resok.eof = TRUE;
282 result.read3res_u.resok.eof = FALSE;
284 bluesky_file_read(inode, argp->offset, buf, count);
287 result.status = NFS3_OK;
288 result.read3res_u.resok.file_attributes.present = TRUE;
289 encode_fattr3(&result.read3res_u.resok.file_attributes.post_op_attr_u.attributes, inode);
290 result.read3res_u.resok.count = count;
291 result.read3res_u.resok.data.data_val = buf;
292 result.read3res_u.resok.data.data_len = count;
298 nfsproc3_write_3_svc(write3args *argp, struct svc_req *rqstp)
300 static write3res result;
302 memset(&wcc, 0, sizeof(wcc));
304 BlueSkyInode *inode = lookup_fh(&argp->file);
306 result.status = NFS3ERR_STALE;
307 result.write3res_u.resfail = wcc;
311 encode_pre_wcc(&wcc, inode);
312 if (inode->type != BLUESKY_REGULAR) {
313 result.status = NFS3ERR_INVAL;
314 result.write3res_u.resfail = wcc;
318 uint64_t lastbyte = argp->offset + argp->count;
319 if (lastbyte > inode->size) {
320 bluesky_file_truncate(inode, lastbyte);
323 if (argp->data.data_len < argp->count) {
326 bluesky_file_write(inode, argp->offset,
327 argp->data.data_val, argp->count);
330 encode_fattr3(&wcc.after.post_op_attr_u.attributes, inode);
331 result.write3res_u.resok.file_wcc = wcc;
332 result.write3res_u.resok.count = argp->count;
333 result.write3res_u.resok.committed = FILE_SYNC;
339 nfsproc3_create_3_svc(create3args *argp, struct svc_req *rqstp)
341 static diropres3 result;
343 memset(&wcc, 0, sizeof(wcc));
345 BlueSkyInode *dir = lookup_fh(&argp->where.dir);
347 result.status = NFS3ERR_STALE;
348 result.diropres3_u.resfail = wcc;
352 encode_pre_wcc(&wcc, dir);
353 if (dir->type != BLUESKY_DIRECTORY) {
354 result.status = NFS3ERR_NOTDIR;
355 result.diropres3_u.resfail = wcc;
359 if (!validate_filename(argp->where.name)
360 || strcmp(argp->where.name, ".") == 0
361 || strcmp(argp->where.name, "..") == 0)
363 result.status = NFS3ERR_EXIST;
364 result.diropres3_u.resfail = wcc;
369 file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_REGULAR);
372 int64_t time = bluesky_get_current_time();
373 printf("time: %lld\n", time);
378 bluesky_insert_inode(fs, file);
379 bluesky_directory_insert(dir, argp->where.name, file->inum);
381 wcc.after.present = TRUE;
382 encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
383 result.diropres3_u.resok.obj_attributes.present = TRUE;
384 encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
385 result.diropres3_u.resok.dir_wcc = wcc;
387 static uint64_t fh_bytes;
388 fh_bytes = GUINT64_TO_BE(file->inum);
389 result.diropres3_u.resok.obj.present = TRUE;
390 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
391 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
397 nfsproc3_mkdir_3_svc(mkdir3args *argp, struct svc_req *rqstp)
399 static diropres3 result;
401 result.status = NFS3ERR_NOTSUPP;
407 nfsproc3_symlink_3_svc(symlink3args *argp, struct svc_req *rqstp)
409 static diropres3 result;
411 result.status = NFS3ERR_NOTSUPP;
417 nfsproc3_mknod_3_svc(mknod3args *argp, struct svc_req *rqstp)
419 static diropres3 result;
421 result.status = NFS3ERR_NOTSUPP;
427 nfsproc3_remove_3_svc(diropargs3 *argp, struct svc_req *rqstp)
429 static wccstat3 result;
431 result.status = NFS3ERR_NOTSUPP;
437 nfsproc3_rmdir_3_svc(diropargs3 *argp, struct svc_req *rqstp)
439 static wccstat3 result;
441 result.status = NFS3ERR_NOTSUPP;
447 nfsproc3_rename_3_svc(rename3args *argp, struct svc_req *rqstp)
449 static rename3res result;
451 result.status = NFS3ERR_NOTSUPP;
457 nfsproc3_link_3_svc(link3args *argp, struct svc_req *rqstp)
459 static link3res result;
461 result.status = NFS3ERR_NOTSUPP;
466 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
470 nfsproc3_readdir_3_svc(readdir3args *argp, struct svc_req *rqstp)
472 static readdir3res result;
474 BlueSkyInode *dir = lookup_fh(&argp->dir);
476 result.status = NFS3ERR_STALE;
477 result.readdir3res_u.resfail.present = FALSE;
481 result.status = NFS3_OK;
482 result.readdir3res_u.resok.dir_attributes.present = TRUE;
483 encode_fattr3(&result.readdir3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
484 memset(result.readdir3res_u.resok.cookieverf, 0,
485 sizeof(result.readdir3res_u.resok.cookieverf));
487 #define MAX_READDIR_DIRENTS 4
488 static entry3 dirents[MAX_READDIR_DIRENTS];
491 BlueSkyDirent start = {NULL, argp->cookie, 0};
492 GSequenceIter *i = g_sequence_search(dir->dirents, &start,
493 bluesky_dirent_compare, NULL);
495 while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
496 BlueSkyDirent *d = g_sequence_get(i);
497 dirents[count].fileid = d->inum;
498 dirents[count].name = d->name;
499 dirents[count].cookie = d->cookie;
500 dirents[count].nextentry = NULL;
502 dirents[count - 1].nextentry = &dirents[count];
503 i = g_sequence_iter_next(i);
508 result.readdir3res_u.resok.reply.entries = &dirents[0];
510 result.readdir3res_u.resok.reply.entries = NULL;
511 result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
517 nfsproc3_readdirplus_3_svc(readdirplus3args *argp, struct svc_req *rqstp)
519 static readdirplus3res result;
521 result.status = NFS3ERR_NOTSUPP;
527 nfsproc3_fsstat_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
529 static fsstat3res result;
531 result.status = NFS3ERR_NOTSUPP;
537 nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
539 static fsinfo3res result;
541 BlueSkyInode *inode = bluesky_get_inode(fs, 1);
542 result.status = NFS3_OK;
543 result.fsinfo3res_u.resok.obj_attributes.present = TRUE;
544 encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
545 result.fsinfo3res_u.resok.rtmax = 32768;
546 result.fsinfo3res_u.resok.rtpref = 32768;
547 result.fsinfo3res_u.resok.rtmult = 4096;
548 result.fsinfo3res_u.resok.wtmax = 32768;
549 result.fsinfo3res_u.resok.wtpref = 32768;
550 result.fsinfo3res_u.resok.wtmult = 4096;
551 result.fsinfo3res_u.resok.dtpref = 4096;
552 result.fsinfo3res_u.resok.maxfilesize = 0x7fffffffffffffffULL;
553 result.fsinfo3res_u.resok.time_delta.seconds = 0;
554 result.fsinfo3res_u.resok.time_delta.nseconds = 1000;
555 result.fsinfo3res_u.resok.properties
556 = FSF3_LINK | FSF3_SYMLINK | FSF3_HOMOGENEOUS | FSF3_CANSETTIME;
562 nfsproc3_pathconf_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
564 static pathconf3res result;
566 BlueSkyInode *inode = bluesky_get_inode(fs, 1);
567 result.status = NFS3_OK;
568 result.pathconf3res_u.resok.obj_attributes.present = TRUE;
569 encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
570 result.pathconf3res_u.resok.linkmax = 0xffffffff;
571 result.pathconf3res_u.resok.name_max = 255;
572 result.pathconf3res_u.resok.no_trunc = TRUE;
573 result.pathconf3res_u.resok.chown_restricted = TRUE;
574 result.pathconf3res_u.resok.case_insensitive = FALSE;
575 result.pathconf3res_u.resok.case_preserving = TRUE;
581 nfsproc3_commit_3_svc(commit3args *argp, struct svc_req *rqstp)
583 static commit3res result;
585 result.status = NFS3ERR_NOTSUPP;