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.
12 #define NFS_BLOCKSIZE 32768
13 #define NFS_MAXSIZE (1 << 20)
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 the 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 /* Arrange for a reference to an inode to be dropped when the RPC request
35 void schedule_inode_unref(RPCRequest *req, BlueSkyInode *inode)
37 struct cleanup_list *c = g_new(struct cleanup_list, 1);
38 c->func = (void (*)(void *))bluesky_inode_unref;
40 c->next = req->cleanup;
44 /* Look up a BlueSkyInode given an NFS filehandle. Returns NULL if the
45 * filehandle is invalid. */
46 BlueSkyInode *lookup_fh(RPCRequest *req, nfs_fh3 *fh)
48 BlueSkyInode *inode = NULL;
49 if (fh->data.data_len == 8) {
50 uint64_t inum = GUINT64_FROM_BE(*(uint64_t *)(fh->data.data_val));
51 inode = bluesky_get_inode(fs, inum);
53 schedule_inode_unref(req, inode);
58 int64_t decode_nfstime3(nfstime3 *time)
60 int64_t result = (int64_t)time->seconds * 1000000;
61 result += time->nseconds / 1000;
65 void set_attributes(BlueSkyInode *inode, sattr3 *attributes)
67 int64_t now = bluesky_get_current_time();
69 if (attributes->mode.set) {
70 inode->mode = attributes->mode.set_uint32_u.val;
73 if (attributes->uid.set) {
74 inode->uid = attributes->uid.set_uint32_u.val;
77 if (attributes->gid.set) {
78 inode->gid = attributes->gid.set_uint32_u.val;
81 if (attributes->size.set) {
82 if (inode->type == BLUESKY_REGULAR) {
83 bluesky_file_truncate(inode, attributes->size.set_uint64_u.val);
88 switch (attributes->atime.set) {
91 case SET_TO_SERVER_TIME:
94 case SET_TO_CLIENT_TIME:
95 inode->atime = decode_nfstime3(&attributes->atime.set_time_u.time);
99 switch (attributes->mtime.set) {
102 case SET_TO_SERVER_TIME:
105 case SET_TO_CLIENT_TIME:
106 inode->mtime = decode_nfstime3(&attributes->mtime.set_time_u.time);
110 bluesky_inode_update_ctime(inode, FALSE);
113 /* Copy inode attributes into NFS response. The BlueSkyInode should be locked
115 void encode_fattr3(struct fattr3 *result, BlueSkyInode *inode)
117 result->type = inode->type;
118 result->mode = inode->mode;
119 result->nlink = inode->nlink;
120 result->uid = inode->uid;
121 result->gid = inode->gid;
122 result->size = inode->size;
124 result->rdev.major = 0;
125 result->rdev.minor = 0;
127 result->fileid = inode->inum;
128 result->atime.seconds = inode->atime / 1000000;
129 result->atime.nseconds = (inode->atime % 1000000) * 1000;
130 result->mtime.seconds = inode->mtime / 1000000;
131 result->mtime.nseconds = (inode->mtime % 1000000) * 1000;
132 result->ctime.seconds = inode->ctime / 1000000;
133 result->ctime.nseconds = (inode->ctime % 1000000) * 1000;
135 switch (inode->type) {
136 case BLUESKY_SYMLINK:
137 result->size = strlen(inode->symlink_contents);
144 void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode)
146 wcc->before.present = TRUE;
147 wcc->before.pre_op_attr_u.attributes.size = inode->size;
148 wcc->before.pre_op_attr_u.attributes.mtime.seconds = inode->mtime / 1000000;
149 wcc->before.pre_op_attr_u.attributes.mtime.nseconds = (inode->mtime % 1000000) * 1000;
150 wcc->before.pre_op_attr_u.attributes.ctime.seconds = inode->ctime / 1000000;
151 wcc->before.pre_op_attr_u.attributes.ctime.nseconds = (inode->ctime % 1000000) * 1000;
154 void nfsproc3_null_3_svc(void *argp, RPCRequest *req)
156 async_rpc_send_reply(req, NULL);
159 void nfsproc3_getattr_3_svc(nfs_fh3 *argp, RPCRequest *req)
162 memset(&result, 0, sizeof(result));
164 BlueSkyInode *inode = lookup_fh(req, argp);
166 result.status = NFS3_OK;
167 g_mutex_lock(inode->lock);
168 encode_fattr3(&result.getattr3res_u.attributes, inode);
169 g_mutex_unlock(inode->lock);
171 result.status = NFS3ERR_STALE;
174 async_rpc_send_reply(req, &result);
177 void nfsproc3_setattr_3_svc(setattr3args *argp, RPCRequest *req)
180 memset(&result, 0, sizeof(result));
182 result.wccstat3_u.wcc.before.present = FALSE;
183 result.wccstat3_u.wcc.after.present = FALSE;
184 BlueSkyInode *inode = lookup_fh(req, &argp->object);
186 result.status = NFS3ERR_STALE;
187 async_rpc_send_reply(req, &result);
191 g_mutex_lock(inode->lock);
192 encode_pre_wcc(&result.wccstat3_u.wcc, inode);
193 if (argp->guard.check) {
194 if (inode->ctime != decode_nfstime3(&argp->guard.sattrguard3_u.ctime)) {
195 result.status = NFS3ERR_NOT_SYNC;
196 result.wccstat3_u.wcc.after.present = TRUE;
197 encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, inode);
198 g_mutex_unlock(inode->lock);
199 async_rpc_send_reply(req, &result);
204 set_attributes(inode, &argp->new_attributes);
206 result.wccstat3_u.wcc.after.present = TRUE;
207 encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
209 result.status = NFS3_OK;
211 g_mutex_unlock(inode->lock);
212 async_rpc_send_reply(req, &result);
215 void nfsproc3_lookup_3_svc(diropargs3 *argp, RPCRequest *req)
218 memset(&result, 0, sizeof(result));
220 BlueSkyInode *dir = lookup_fh(req, &argp->dir);
222 result.status = NFS3ERR_STALE;
223 result.lookup3res_u.resfail.present = FALSE;
224 async_rpc_send_reply(req, &result);
228 g_mutex_lock(dir->lock);
229 result.lookup3res_u.resfail.present = TRUE;
230 encode_fattr3(&result.lookup3res_u.resfail.post_op_attr_u.attributes, dir);
231 if (!validate_filename(argp->name)) {
232 if (strlen(argp->name) > 255)
233 result.status = NFS3ERR_NAMETOOLONG;
235 result.status = NFS3ERR_NOENT;
236 g_mutex_unlock(dir->lock);
237 async_rpc_send_reply(req, &result);
241 /* TODO: Special-case "." and "..". */
242 uint64_t inum = bluesky_directory_lookup(dir, argp->name);
244 result.status = NFS3ERR_NOENT;
245 g_mutex_unlock(dir->lock);
246 async_rpc_send_reply(req, &result);
250 result.lookup3res_u.resok.dir_attributes.present = TRUE;
251 encode_fattr3(&result.lookup3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
252 g_mutex_unlock(dir->lock);
254 BlueSkyInode *inode = bluesky_get_inode(fs, inum);
256 result.status = NFS3ERR_NOENT;
257 async_rpc_send_reply(req, &result);
260 g_mutex_lock(inode->lock);
261 schedule_inode_unref(req, inode);
263 result.status = NFS3_OK;
264 result.lookup3res_u.resok.obj_attributes.present = TRUE;
265 encode_fattr3(&result.lookup3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
268 fh_bytes = GUINT64_TO_BE(inum);
269 result.lookup3res_u.resok.object.data.data_len = 8;
270 result.lookup3res_u.resok.object.data.data_val = (char *)&fh_bytes;
272 g_mutex_unlock(inode->lock);
273 async_rpc_send_reply(req, &result);
276 void nfsproc3_access_3_svc(access3args *argp, RPCRequest *req)
279 memset(&result, 0, sizeof(result));
281 BlueSkyInode *inode = lookup_fh(req, &argp->object);
283 result.status = NFS3ERR_STALE;
284 result.access3res_u.resfail.present = FALSE;
285 async_rpc_send_reply(req, &result);
289 g_mutex_lock(inode->lock);
290 result.status = NFS3_OK;
291 result.access3res_u.resok.obj_attributes.present = TRUE;
292 encode_fattr3(&result.access3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
293 result.access3res_u.resok.access = argp->access;
294 g_mutex_unlock(inode->lock);
296 async_rpc_send_reply(req, &result);
299 void nfsproc3_readlink_3_svc(nfs_fh3 *argp, RPCRequest *req)
302 memset(&result, 0, sizeof(result));
304 BlueSkyInode *inode = lookup_fh(req, argp);
306 g_mutex_lock(inode->lock);
307 if (inode->type == BLUESKY_SYMLINK) {
308 result.status = NFS3_OK;
309 result.readlink3res_u.resok.symlink_attributes.present = TRUE;
310 encode_fattr3(&result.readlink3res_u.resok.symlink_attributes.post_op_attr_u.attributes, inode);
311 result.readlink3res_u.resok.data = inode->symlink_contents;
313 result.status = NFS3ERR_INVAL;
314 result.readlink3res_u.resfail.present = TRUE;
315 encode_fattr3(&result.readlink3res_u.resfail.post_op_attr_u.attributes, inode);
317 g_mutex_unlock(inode->lock);
319 result.status = NFS3ERR_STALE;
322 async_rpc_send_reply(req, &result);
325 void nfsproc3_read_3_svc(read3args *argp, RPCRequest *req)
328 memset(&result, 0, sizeof(result));
329 char buf[NFS_MAXSIZE];
331 bluesky_flushd_invoke_conditional(fs);
333 BlueSkyInode *inode = lookup_fh(req, &argp->file);
335 result.status = NFS3ERR_STALE;
336 result.read3res_u.resfail.present = FALSE;
337 async_rpc_send_reply(req, &result);
341 g_mutex_lock(inode->lock);
343 int count = argp->count;
344 if (argp->offset >= inode->size) {
346 result.read3res_u.resok.eof = TRUE;
348 count = MIN(count, NFS_MAXSIZE);
349 count = MIN(count, inode->size - argp->offset);
350 if (argp->offset + count == inode->size)
351 result.read3res_u.resok.eof = TRUE;
353 result.read3res_u.resok.eof = FALSE;
355 bluesky_file_read(inode, argp->offset, buf, count);
358 result.status = NFS3_OK;
359 result.read3res_u.resok.file_attributes.present = TRUE;
360 encode_fattr3(&result.read3res_u.resok.file_attributes.post_op_attr_u.attributes, inode);
361 result.read3res_u.resok.count = count;
362 result.read3res_u.resok.data.data_val = buf;
363 result.read3res_u.resok.data.data_len = count;
365 g_mutex_unlock(inode->lock);
367 async_rpc_send_reply(req, &result);
370 void nfsproc3_write_3_svc(write3args *argp, RPCRequest *req)
373 memset(&result, 0, sizeof(result));
375 memset(&wcc, 0, sizeof(wcc));
377 bluesky_flushd_invoke_conditional(fs);
379 BlueSkyInode *inode = lookup_fh(req, &argp->file);
381 result.status = NFS3ERR_STALE;
382 result.write3res_u.resfail = wcc;
383 async_rpc_send_reply(req, &result);
388 /* FIXME: Hack to throttle writes when there is too much dirty data still
389 * to be written out. */
390 while (g_atomic_int_get(&fs->cache_dirty) > 4096
391 || g_atomic_int_get(&fs->cache_total) > 8192) {
392 g_print("Too many dirty pages (%d) or total pages (%d); throttling writes...\n",
393 g_atomic_int_get(&fs->cache_dirty),
394 g_atomic_int_get(&fs->cache_total));
395 struct timespec delay;
398 nanosleep(&delay, NULL);
402 g_mutex_lock(inode->lock);
404 encode_pre_wcc(&wcc, inode);
405 if (inode->type != BLUESKY_REGULAR) {
406 result.status = NFS3ERR_INVAL;
407 result.write3res_u.resfail = wcc;
408 g_mutex_unlock(inode->lock);
409 async_rpc_send_reply(req, &result);
413 uint64_t lastbyte = argp->offset + argp->count;
414 if (lastbyte > inode->size) {
415 bluesky_file_truncate(inode, lastbyte);
418 if (argp->data.data_len < argp->count) {
421 bluesky_file_write(inode, argp->offset,
422 argp->data.data_val, argp->count);
425 wcc.after.present = TRUE;
426 encode_fattr3(&wcc.after.post_op_attr_u.attributes, inode);
427 result.write3res_u.resok.file_wcc = wcc;
428 result.write3res_u.resok.count = argp->count;
429 result.write3res_u.resok.committed = FILE_SYNC;
431 g_mutex_unlock(inode->lock);
433 async_rpc_send_reply(req, &result);
436 void nfsproc3_create_3_svc(create3args *argp, RPCRequest *req)
439 memset(&result, 0, sizeof(result));
441 memset(&wcc, 0, sizeof(wcc));
443 BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
445 result.status = NFS3ERR_STALE;
446 result.diropres3_u.resfail = wcc;
447 async_rpc_send_reply(req, &result);
451 g_mutex_lock(dir->lock);
453 encode_pre_wcc(&wcc, dir);
454 if (dir->type != BLUESKY_DIRECTORY) {
455 result.status = NFS3ERR_NOTDIR;
456 result.diropres3_u.resfail = wcc;
457 g_mutex_unlock(dir->lock);
458 async_rpc_send_reply(req, &result);
462 if (!validate_filename(argp->where.name)
463 || strcmp(argp->where.name, ".") == 0
464 || strcmp(argp->where.name, "..") == 0)
466 result.status = NFS3ERR_EXIST;
467 result.diropres3_u.resfail = wcc;
468 g_mutex_unlock(dir->lock);
469 async_rpc_send_reply(req, &result);
474 file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_REGULAR);
477 int64_t time = bluesky_get_current_time();
478 printf("time: %"PRIi64"\n", time);
483 g_mutex_lock(file->lock);
484 bluesky_insert_inode(fs, file);
485 bluesky_directory_insert(dir, argp->where.name, file->inum);
487 bluesky_inode_update_ctime(dir, TRUE);
489 wcc.after.present = TRUE;
490 encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
491 result.diropres3_u.resok.obj_attributes.present = TRUE;
492 encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
493 result.diropres3_u.resok.dir_wcc = wcc;
496 fh_bytes = GUINT64_TO_BE(file->inum);
497 result.diropres3_u.resok.obj.present = TRUE;
498 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
499 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
501 g_mutex_unlock(file->lock);
502 g_mutex_unlock(dir->lock);
504 async_rpc_send_reply(req, &result);
507 void nfsproc3_mkdir_3_svc(mkdir3args *argp, RPCRequest *req)
510 memset(&result, 0, sizeof(result));
512 memset(&wcc, 0, sizeof(wcc));
514 BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
516 result.status = NFS3ERR_STALE;
517 result.diropres3_u.resfail = wcc;
518 async_rpc_send_reply(req, &result);
522 g_mutex_lock(dir->lock);
524 encode_pre_wcc(&wcc, dir);
525 if (dir->type != BLUESKY_DIRECTORY) {
526 result.status = NFS3ERR_NOTDIR;
527 result.diropres3_u.resfail = wcc;
528 g_mutex_unlock(dir->lock);
529 async_rpc_send_reply(req, &result);
533 if (!validate_filename(argp->where.name)
534 || strcmp(argp->where.name, ".") == 0
535 || strcmp(argp->where.name, "..") == 0)
537 result.status = NFS3ERR_EXIST;
538 result.diropres3_u.resfail = wcc;
539 g_mutex_unlock(dir->lock);
540 async_rpc_send_reply(req, &result);
545 file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_DIRECTORY);
548 int64_t time = bluesky_get_current_time();
553 g_mutex_lock(file->lock);
554 bluesky_insert_inode(fs, file);
555 bluesky_directory_insert(dir, argp->where.name, file->inum);
556 set_attributes(file, &argp->attributes);
558 bluesky_inode_update_ctime(dir, TRUE);
560 wcc.after.present = TRUE;
561 encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
562 result.diropres3_u.resok.obj_attributes.present = TRUE;
563 encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
564 result.diropres3_u.resok.dir_wcc = wcc;
567 fh_bytes = GUINT64_TO_BE(file->inum);
568 result.diropres3_u.resok.obj.present = TRUE;
569 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
570 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
572 g_mutex_unlock(file->lock);
573 g_mutex_unlock(dir->lock);
574 async_rpc_send_reply(req, &result);
577 void nfsproc3_symlink_3_svc(symlink3args *argp, RPCRequest *req)
580 memset(&result, 0, sizeof(result));
582 memset(&wcc, 0, sizeof(wcc));
584 BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
586 result.status = NFS3ERR_STALE;
587 result.diropres3_u.resfail = wcc;
588 async_rpc_send_reply(req, &result);
591 g_mutex_lock(dir->lock);
593 encode_pre_wcc(&wcc, dir);
594 if (dir->type != BLUESKY_DIRECTORY) {
595 result.status = NFS3ERR_NOTDIR;
596 result.diropres3_u.resfail = wcc;
597 g_mutex_unlock(dir->lock);
598 async_rpc_send_reply(req, &result);
602 if (!validate_filename(argp->where.name)
603 || strcmp(argp->where.name, ".") == 0
604 || strcmp(argp->where.name, "..") == 0)
606 result.status = NFS3ERR_EXIST;
607 result.diropres3_u.resfail = wcc;
608 g_mutex_unlock(dir->lock);
609 async_rpc_send_reply(req, &result);
614 file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_SYMLINK);
617 int64_t time = bluesky_get_current_time();
622 file->symlink_contents = g_strdup(argp->symlink.symlink_data);
623 g_mutex_lock(file->lock);
624 bluesky_insert_inode(fs, file);
625 bluesky_directory_insert(dir, argp->where.name, file->inum);
627 bluesky_inode_update_ctime(dir, TRUE);
629 wcc.after.present = TRUE;
630 encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
631 result.diropres3_u.resok.obj_attributes.present = TRUE;
632 encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
633 result.diropres3_u.resok.dir_wcc = wcc;
636 fh_bytes = GUINT64_TO_BE(file->inum);
637 result.diropres3_u.resok.obj.present = TRUE;
638 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
639 result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
641 g_mutex_unlock(file->lock);
642 g_mutex_unlock(dir->lock);
643 async_rpc_send_reply(req, &result);
646 void nfsproc3_mknod_3_svc(mknod3args *argp, RPCRequest *req)
649 memset(&result, 0, sizeof(result));
651 result.status = NFS3ERR_NOTSUPP;
653 async_rpc_send_reply(req, &result);
656 void nfsproc3_remove_3_svc(diropargs3 *argp, RPCRequest *req)
659 memset(&result, 0, sizeof(result));
661 BlueSkyInode *dir = lookup_fh(req, &argp->dir);
663 result.status = NFS3ERR_STALE;
664 async_rpc_send_reply(req, &result);
668 g_mutex_lock(dir->lock);
670 encode_pre_wcc(&result.wccstat3_u.wcc, dir);
672 if (!validate_filename(argp->name)
673 || strcmp(argp->name, ".") == 0
674 || strcmp(argp->name, "..") == 0)
676 result.status = NFS3ERR_NOENT;
677 g_mutex_unlock(dir->lock);
678 async_rpc_send_reply(req, &result);
682 /* TODO: Decrement link count, deallocate inode if needed. */
684 bluesky_directory_remove(dir, argp->name);
686 result.status = NFS3_OK;
687 result.wccstat3_u.wcc.after.present = TRUE;
688 encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
691 g_mutex_unlock(dir->lock);
692 async_rpc_send_reply(req, &result);
695 void nfsproc3_rmdir_3_svc(diropargs3 *argp, RPCRequest *req)
698 memset(&result, 0, sizeof(result));
700 BlueSkyInode *dir = lookup_fh(req, &argp->dir);
702 result.status = NFS3ERR_STALE;
703 async_rpc_send_reply(req, &result);
707 g_mutex_lock(dir->lock);
709 encode_pre_wcc(&result.wccstat3_u.wcc, dir);
711 if (!validate_filename(argp->name)
712 || strcmp(argp->name, ".") == 0
713 || strcmp(argp->name, "..") == 0)
715 result.status = NFS3ERR_NOENT;
716 g_mutex_unlock(dir->lock);
717 async_rpc_send_reply(req, &result);
721 uint64_t inum = bluesky_directory_lookup(dir, argp->name);
722 BlueSkyInode *inode = bluesky_get_inode(fs, inum);
724 result.status = NFS3ERR_NOENT;
725 g_mutex_unlock(dir->lock);
726 async_rpc_send_reply(req, &result);
729 g_mutex_lock(inode->lock);
730 schedule_inode_unref(req, inode);
732 if (inode->type != BLUESKY_DIRECTORY) {
733 result.status = NFS3ERR_NOTDIR;
734 g_mutex_unlock(inode->lock);
735 g_mutex_unlock(dir->lock);
736 async_rpc_send_reply(req, &result);
739 if (g_sequence_get_length(inode->dirents) > 0) {
740 printf("Directory not empty: %d entries\n",
741 g_sequence_get_length(inode->dirents));
742 result.status = NFS3ERR_NOTEMPTY;
743 g_mutex_unlock(inode->lock);
744 g_mutex_unlock(dir->lock);
745 async_rpc_send_reply(req, &result);
749 /* TODO: Decrement link count, deallocate inode if needed. */
751 bluesky_directory_remove(dir, argp->name);
753 result.status = NFS3_OK;
754 result.wccstat3_u.wcc.after.present = TRUE;
755 encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
758 g_mutex_unlock(inode->lock);
759 g_mutex_unlock(dir->lock);
760 async_rpc_send_reply(req, &result);
763 void nfsproc3_rename_3_svc(rename3args *argp, RPCRequest *req)
766 memset(&result, 0, sizeof(result));
767 wcc_data *wcc1 = &result.rename3res_u.res.fromdir_wcc;
768 wcc_data *wcc2 = &result.rename3res_u.res.todir_wcc;
770 BlueSkyInode *dir1 = lookup_fh(req, &argp->from.dir);
772 result.status = NFS3ERR_STALE;
773 async_rpc_send_reply(req, &result);
777 BlueSkyInode *dir2 = lookup_fh(req, &argp->to.dir);
779 result.status = NFS3ERR_STALE;
780 async_rpc_send_reply(req, &result);
784 if (dir1->inum < dir2->inum) {
785 g_mutex_lock(dir1->lock);
786 g_mutex_lock(dir2->lock);
787 } else if (dir1->inum > dir2->inum) {
788 g_mutex_lock(dir2->lock);
789 g_mutex_lock(dir1->lock);
791 encode_pre_wcc(wcc1, dir1);
792 encode_pre_wcc(wcc2, dir1);
794 gboolean status = bluesky_rename(dir1, argp->from.name,
798 wcc1->after.present = TRUE;
799 encode_fattr3(&wcc1->after.post_op_attr_u.attributes, dir1);
800 wcc2->after.present = TRUE;
801 encode_fattr3(&wcc2->after.post_op_attr_u.attributes, dir2);
803 result.status = NFS3_OK;
805 result.status = NFS3ERR_PERM;
807 g_mutex_unlock(dir1->lock);
808 if (dir1->inum != dir2->inum)
809 g_mutex_unlock(dir2->lock);
810 async_rpc_send_reply(req, &result);
813 void nfsproc3_link_3_svc(link3args *argp, RPCRequest *req)
816 memset(&result, 0, sizeof(result));
818 memset(&wcc, 0, sizeof(wcc));
820 BlueSkyInode *inode = lookup_fh(req, &argp->file);
822 result.status = NFS3ERR_STALE;
823 result.link3res_u.res.linkdir_wcc = wcc;
824 async_rpc_send_reply(req, &result);
827 g_mutex_lock(inode->lock);
829 BlueSkyInode *dir = lookup_fh(req, &argp->link.dir);
831 result.status = NFS3ERR_STALE;
832 result.link3res_u.res.linkdir_wcc = wcc;
833 g_mutex_unlock(inode->lock);
834 async_rpc_send_reply(req, &result);
837 g_mutex_lock(dir->lock);
839 encode_pre_wcc(&wcc, dir);
840 if (dir->type != BLUESKY_DIRECTORY) {
841 result.status = NFS3ERR_NOTDIR;
842 result.link3res_u.res.linkdir_wcc = wcc;
843 g_mutex_unlock(inode->lock);
844 g_mutex_unlock(dir->lock);
845 async_rpc_send_reply(req, &result);
849 if (!validate_filename(argp->link.name)
850 || strcmp(argp->link.name, ".") == 0
851 || strcmp(argp->link.name, "..") == 0
852 || bluesky_directory_lookup(dir, argp->link.name) != 0)
854 result.status = NFS3ERR_EXIST;
855 result.link3res_u.res.linkdir_wcc = wcc;
856 g_mutex_unlock(inode->lock);
857 g_mutex_unlock(dir->lock);
858 async_rpc_send_reply(req, &result);
862 if (!bluesky_directory_insert(dir, argp->link.name, inode->inum)) {
863 result.status = NFS3ERR_EXIST;
864 result.link3res_u.res.linkdir_wcc = wcc;
865 g_mutex_unlock(inode->lock);
866 g_mutex_unlock(dir->lock);
867 async_rpc_send_reply(req, &result);
871 bluesky_inode_update_ctime(inode, FALSE);
872 bluesky_inode_update_ctime(dir, TRUE);
874 result.status = NFS3_OK;
875 wcc.after.present = TRUE;
876 encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
877 result.link3res_u.res.file_attributes.present = TRUE;
878 encode_fattr3(&result.link3res_u.res.file_attributes.post_op_attr_u.attributes, inode);
879 result.link3res_u.res.linkdir_wcc = wcc;
881 g_mutex_unlock(inode->lock);
882 g_mutex_unlock(dir->lock);
883 async_rpc_send_reply(req, &result);
886 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
889 #define MAX_READDIR_DIRENTS 64
890 void nfsproc3_readdir_3_svc(readdir3args *argp, RPCRequest *req)
893 memset(&result, 0, sizeof(result));
895 BlueSkyInode *dir = lookup_fh(req, &argp->dir);
897 result.status = NFS3ERR_STALE;
898 result.readdir3res_u.resfail.present = FALSE;
899 async_rpc_send_reply(req, &result);
902 g_mutex_lock(dir->lock);
904 result.status = NFS3_OK;
905 result.readdir3res_u.resok.dir_attributes.present = TRUE;
906 encode_fattr3(&result.readdir3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
907 memset(result.readdir3res_u.resok.cookieverf, 0,
908 sizeof(result.readdir3res_u.resok.cookieverf));
910 entry3 dirents[MAX_READDIR_DIRENTS];
913 BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
914 GSequenceIter *i = g_sequence_search(dir->dirents, &start,
915 bluesky_dirent_compare, NULL);
917 while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
918 BlueSkyDirent *d = g_sequence_get(i);
919 dirents[count].fileid = d->inum;
920 dirents[count].name = d->name;
921 dirents[count].cookie = d->cookie;
922 dirents[count].nextentry = NULL;
924 dirents[count - 1].nextentry = &dirents[count];
925 i = g_sequence_iter_next(i);
930 result.readdir3res_u.resok.reply.entries = &dirents[0];
932 result.readdir3res_u.resok.reply.entries = NULL;
933 result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
935 g_mutex_unlock(dir->lock);
936 async_rpc_send_reply(req, &result);
939 void nfsproc3_readdirplus_3_svc(readdirplus3args *argp, RPCRequest *req)
941 /* XDR-encoded sizes:
942 * post_op_attr: 88 bytes
943 * base readdirplus3resok: 88 + 16 bytes
944 * base directory entry: 24 bytes + filename
945 * attributes/fh3: 88 + 8 + filehandle size
947 size_t dircount = 88 + 16, attrcount = 0;
948 readdirplus3res result;
949 memset(&result, 0, sizeof(result));
951 BlueSkyInode *dir = lookup_fh(req, &argp->dir);
953 result.status = NFS3ERR_STALE;
954 result.readdirplus3res_u.resfail.present = FALSE;
955 async_rpc_send_reply(req, &result);
958 g_mutex_lock(dir->lock);
960 result.status = NFS3_OK;
961 result.readdirplus3res_u.resok.dir_attributes.present = TRUE;
962 encode_fattr3(&result.readdirplus3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
963 memset(result.readdirplus3res_u.resok.cookieverf, 0,
964 sizeof(result.readdirplus3res_u.resok.cookieverf));
966 entryplus3 dirents[MAX_READDIR_DIRENTS];
967 uint64_t fh_bytes[MAX_READDIR_DIRENTS];
971 BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
973 /* Perform a prefetch pass on inodes: for all the inodes we think we will
974 * return information about, try to load each one but don't wait. This
975 * should let multiple inodes be fetched in parallel, instead of
976 * sequentially in the loop that follows. */
977 i = g_sequence_search(dir->dirents, &start, bluesky_dirent_compare, NULL);
978 while (count < MAX_READDIR_DIRENTS
979 && !g_sequence_iter_is_end(i)
980 && dircount <= argp->dircount
981 && dircount + attrcount <= argp->maxcount)
983 BlueSkyDirent *d = g_sequence_get(i);
984 BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
986 bluesky_inode_unref(inode);
987 dircount += 24 + ((strlen(d->name) + 3) & ~3);
988 attrcount += 88 + 8 + 8;
989 i = g_sequence_iter_next(i);
992 i = g_sequence_search(dir->dirents, &start, bluesky_dirent_compare, NULL);
996 while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
997 BlueSkyDirent *d = g_sequence_get(i);
998 BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
1000 g_mutex_lock(inode->lock);
1001 dircount += 24 + ((strlen(d->name) + 3) & ~3);
1002 attrcount += 88 + 8 + 8;
1003 if (dircount > argp->dircount
1004 || dircount + attrcount > argp->maxcount)
1006 g_mutex_unlock(inode->lock);
1007 bluesky_inode_unref(inode);
1010 dirents[count].fileid = d->inum;
1011 dirents[count].name = d->name;
1012 dirents[count].cookie = d->cookie;
1013 dirents[count].nextentry = NULL;
1014 dirents[count].name_attributes.present = TRUE;
1015 encode_fattr3(&dirents[count].name_attributes.post_op_attr_u.attributes, inode);
1016 fh_bytes[count] = GUINT64_TO_BE(d->inum);
1017 dirents[count].name_handle.present = TRUE;
1018 dirents[count].name_handle.post_op_fh3_u.handle.data.data_len = 8;
1019 dirents[count].name_handle.post_op_fh3_u.handle.data.data_val
1020 = (char *)&fh_bytes[count];
1022 dirents[count - 1].nextentry = &dirents[count];
1024 g_mutex_unlock(inode->lock);
1025 bluesky_inode_unref(inode);
1027 i = g_sequence_iter_next(i);
1031 result.readdirplus3res_u.resok.reply.entries = &dirents[0];
1033 result.readdirplus3res_u.resok.reply.entries = NULL;
1034 result.readdirplus3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
1036 g_mutex_unlock(dir->lock);
1037 async_rpc_send_reply(req, &result);
1040 void nfsproc3_fsstat_3_svc(nfs_fh3 *argp, RPCRequest *req)
1043 memset(&result, 0, sizeof(result));
1045 BlueSkyInode *inode = lookup_fh(req, argp);
1046 if (inode == NULL) {
1047 result.status = NFS3ERR_STALE;
1048 result.fsstat3res_u.resfail.present = FALSE;
1049 async_rpc_send_reply(req, &result);
1052 g_mutex_lock(inode->lock);
1054 result.status = NFS3_OK;
1055 result.fsstat3res_u.resok.obj_attributes.present = TRUE;
1056 encode_fattr3(&result.fsstat3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
1058 result.fsstat3res_u.resok.tbytes = (1 << 30);
1059 result.fsstat3res_u.resok.fbytes = (1 << 30);
1060 result.fsstat3res_u.resok.abytes = (1 << 30);
1061 result.fsstat3res_u.resok.tfiles = 0;
1062 result.fsstat3res_u.resok.ffiles = 0;
1063 result.fsstat3res_u.resok.afiles = 0;
1064 result.fsstat3res_u.resok.invarsec = 0;
1066 g_mutex_unlock(inode->lock);
1067 async_rpc_send_reply(req, &result);
1070 void nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, RPCRequest *req)
1073 memset(&result, 0, sizeof(result));
1075 BlueSkyInode *inode = bluesky_get_inode(fs, 1);
1076 g_mutex_lock(inode->lock);
1077 result.status = NFS3_OK;
1078 result.fsinfo3res_u.resok.obj_attributes.present = TRUE;
1079 encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
1080 result.fsinfo3res_u.resok.rtmax = NFS_MAXSIZE;
1081 result.fsinfo3res_u.resok.rtpref = NFS_MAXSIZE;
1082 result.fsinfo3res_u.resok.rtmult = NFS_BLOCKSIZE;
1083 result.fsinfo3res_u.resok.wtmax = NFS_MAXSIZE;
1084 result.fsinfo3res_u.resok.wtpref = NFS_MAXSIZE;
1085 result.fsinfo3res_u.resok.wtmult = NFS_BLOCKSIZE;
1086 result.fsinfo3res_u.resok.dtpref = NFS_BLOCKSIZE;
1087 result.fsinfo3res_u.resok.maxfilesize = 0x7fffffffffffffffULL;
1088 result.fsinfo3res_u.resok.time_delta.seconds = 0;
1089 result.fsinfo3res_u.resok.time_delta.nseconds = 1000;
1090 result.fsinfo3res_u.resok.properties
1091 = FSF3_LINK | FSF3_SYMLINK | FSF3_HOMOGENEOUS | FSF3_CANSETTIME;
1093 g_mutex_unlock(inode->lock);
1094 bluesky_inode_unref(inode);
1095 async_rpc_send_reply(req, &result);
1098 void nfsproc3_pathconf_3_svc(nfs_fh3 *argp, RPCRequest *req)
1100 pathconf3res result;
1101 memset(&result, 0, sizeof(result));
1103 BlueSkyInode *inode = bluesky_get_inode(fs, 1);
1104 g_mutex_lock(inode->lock);
1105 result.status = NFS3_OK;
1106 result.pathconf3res_u.resok.obj_attributes.present = TRUE;
1107 encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
1108 result.pathconf3res_u.resok.linkmax = 0xffffffff;
1109 result.pathconf3res_u.resok.name_max = 255;
1110 result.pathconf3res_u.resok.no_trunc = TRUE;
1111 result.pathconf3res_u.resok.chown_restricted = TRUE;
1112 result.pathconf3res_u.resok.case_insensitive = FALSE;
1113 result.pathconf3res_u.resok.case_preserving = TRUE;
1115 g_mutex_unlock(inode->lock);
1116 bluesky_inode_unref(inode);
1117 async_rpc_send_reply(req, &result);
1120 void nfsproc3_commit_3_svc(commit3args *argp, RPCRequest *req)
1123 memset(&result, 0, sizeof(result));
1125 result.status = NFS3_OK;
1127 BlueSkyInode *inode = lookup_fh(req, &argp->file);
1128 if (inode == NULL) {
1129 result.status = NFS3ERR_STALE;
1130 async_rpc_send_reply(req, &result);
1134 g_mutex_lock(inode->lock);
1135 encode_pre_wcc(&result.commit3res_u.resok.file_wcc, inode);
1137 //bluesky_inode_do_sync(inode);
1139 result.commit3res_u.resok.file_wcc.after.present = TRUE;
1140 encode_fattr3(&result.commit3res_u.resok.file_wcc.after.post_op_attr_u.attributes, inode);
1142 g_mutex_unlock(inode->lock);
1144 async_rpc_send_reply(req, &result);