Possible null pointer dereference fix.
[bluesky.git] / nfs3 / nfs3.c
1 /*
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.
5  */
6
7 #include "nfs3_prot.h"
8 #include "bluesky.h"
9
10 extern BlueSkyFS *fs;
11
12 #define NFS_BLOCKSIZE 32768
13 #define NFS_MAXSIZE (1 << 20)
14
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)
21 {
22     if (filename == NULL || filename[0] == '\0')
23         return FALSE;
24     if (strlen(filename) > 255)
25         return FALSE;
26     if (!g_utf8_validate(filename, -1, NULL))
27         return FALSE;
28     if (strchr(filename, '/') != NULL)
29         return FALSE;
30     return TRUE;
31 }
32
33 /* Arrange for a reference to an inode to be dropped when the RPC request
34  * completes. */
35 void schedule_inode_unref(RPCRequest *req, BlueSkyInode *inode)
36 {
37     struct cleanup_list *c = g_new(struct cleanup_list, 1);
38     c->func = (void (*)(void *))bluesky_inode_unref;
39     c->arg = inode;
40     c->next = req->cleanup;
41     req->cleanup = c;
42 }
43
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)
47 {
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);
52         if (inode != NULL)
53             schedule_inode_unref(req, inode);
54     }
55     return inode;
56 }
57
58 int64_t decode_nfstime3(nfstime3 *time)
59 {
60     int64_t result = (int64_t)time->seconds * 1000000;
61     result += time->nseconds / 1000;
62     return result;
63 }
64
65 void set_attributes(BlueSkyInode *inode, sattr3 *attributes)
66 {
67     int64_t now = bluesky_get_current_time();
68
69     if (attributes->mode.set) {
70         inode->mode = attributes->mode.set_uint32_u.val;
71     }
72
73     if (attributes->uid.set) {
74         inode->uid = attributes->uid.set_uint32_u.val;
75     }
76
77     if (attributes->gid.set) {
78         inode->gid = attributes->gid.set_uint32_u.val;
79     }
80
81     if (attributes->size.set) {
82         if (inode->type == BLUESKY_REGULAR) {
83             bluesky_file_truncate(inode, attributes->size.set_uint64_u.val);
84             inode->mtime = now;
85         }
86     }
87
88     switch (attributes->atime.set) {
89     case DONT_CHANGE:
90         break;
91     case SET_TO_SERVER_TIME:
92         inode->atime = now;
93         break;
94     case SET_TO_CLIENT_TIME:
95         inode->atime = decode_nfstime3(&attributes->atime.set_time_u.time);
96         break;
97     }
98
99     switch (attributes->mtime.set) {
100     case DONT_CHANGE:
101         break;
102     case SET_TO_SERVER_TIME:
103         inode->mtime = now;
104         break;
105     case SET_TO_CLIENT_TIME:
106         inode->mtime = decode_nfstime3(&attributes->mtime.set_time_u.time);
107         break;
108     }
109
110     bluesky_inode_update_ctime(inode, FALSE);
111 }
112
113 /* Copy inode attributes into NFS response.  The BlueSkyInode should be locked
114  * by the caller. */
115 void encode_fattr3(struct fattr3 *result, BlueSkyInode *inode)
116 {
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;
123     result->used = 0;
124     result->rdev.major = 0;
125     result->rdev.minor = 0;
126     result->fsid = 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;
134
135     switch (inode->type) {
136     case BLUESKY_SYMLINK:
137         result->size = strlen(inode->symlink_contents);
138         break;
139     default:
140         break;
141     }
142 }
143
144 void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode)
145 {
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;
152 }
153
154 void nfsproc3_null_3_svc(void *argp, RPCRequest *req)
155 {
156     async_rpc_send_reply(req, NULL);
157 }
158
159 void nfsproc3_getattr_3_svc(nfs_fh3 *argp, RPCRequest *req)
160 {
161     getattr3res result;
162     memset(&result, 0, sizeof(result));
163
164     BlueSkyInode *inode = lookup_fh(req, argp);
165     if (inode != NULL) {
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);
170     } else {
171         result.status = NFS3ERR_STALE;
172     }
173
174     async_rpc_send_reply(req, &result);
175 }
176
177 void nfsproc3_setattr_3_svc(setattr3args *argp, RPCRequest *req)
178 {
179     wccstat3 result;
180     memset(&result, 0, sizeof(result));
181
182     result.wccstat3_u.wcc.before.present = FALSE;
183     result.wccstat3_u.wcc.after.present = FALSE;
184     BlueSkyInode *inode = lookup_fh(req, &argp->object);
185     if (inode == NULL) {
186         result.status = NFS3ERR_STALE;
187         async_rpc_send_reply(req, &result);
188         return;
189     }
190
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);
200             return;
201         }
202     }
203
204     set_attributes(inode, &argp->new_attributes);
205
206     result.wccstat3_u.wcc.after.present = TRUE;
207     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
208                   inode);
209     result.status = NFS3_OK;
210
211     g_mutex_unlock(inode->lock);
212     async_rpc_send_reply(req, &result);
213 }
214
215 void nfsproc3_lookup_3_svc(diropargs3 *argp, RPCRequest *req)
216 {
217     lookup3res result;
218     memset(&result, 0, sizeof(result));
219
220     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
221     if (dir == NULL) {
222         result.status = NFS3ERR_STALE;
223         result.lookup3res_u.resfail.present = FALSE;
224         async_rpc_send_reply(req, &result);
225         return;
226     }
227
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;
234         else
235             result.status = NFS3ERR_NOENT;
236         g_mutex_unlock(dir->lock);
237         async_rpc_send_reply(req, &result);
238         return;
239     }
240
241     /* TODO: Special-case "." and "..". */
242     uint64_t inum = bluesky_directory_lookup(dir, argp->name);
243     if (inum == 0) {
244         result.status = NFS3ERR_NOENT;
245         g_mutex_unlock(dir->lock);
246         async_rpc_send_reply(req, &result);
247         return;
248     }
249
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);
253
254     BlueSkyInode *inode = bluesky_get_inode(fs, inum);
255     if (inode == NULL) {
256         result.status = NFS3ERR_NOENT;
257         async_rpc_send_reply(req, &result);
258         return;
259     }
260     g_mutex_lock(inode->lock);
261     schedule_inode_unref(req, inode);
262
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);
266
267     uint64_t fh_bytes;
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;
271
272     g_mutex_unlock(inode->lock);
273     async_rpc_send_reply(req, &result);
274 }
275
276 void nfsproc3_access_3_svc(access3args *argp, RPCRequest *req)
277 {
278     access3res result;
279     memset(&result, 0, sizeof(result));
280
281     BlueSkyInode *inode = lookup_fh(req, &argp->object);
282     if (inode == NULL) {
283         result.status = NFS3ERR_STALE;
284         result.access3res_u.resfail.present = FALSE;
285         async_rpc_send_reply(req, &result);
286         return;
287     }
288
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);
295
296     async_rpc_send_reply(req, &result);
297 }
298
299 void nfsproc3_readlink_3_svc(nfs_fh3 *argp, RPCRequest *req)
300 {
301     readlink3res result;
302     memset(&result, 0, sizeof(result));
303
304     BlueSkyInode *inode = lookup_fh(req, argp);
305     if (inode != NULL) {
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;
312         } else {
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);
316         }
317         g_mutex_unlock(inode->lock);
318     } else {
319         result.status = NFS3ERR_STALE;
320     }
321
322     async_rpc_send_reply(req, &result);
323 }
324
325 void nfsproc3_read_3_svc(read3args *argp, RPCRequest *req)
326 {
327     read3res result;
328     memset(&result, 0, sizeof(result));
329     char buf[NFS_MAXSIZE];
330
331     bluesky_flushd_invoke_conditional(fs);
332
333     BlueSkyInode *inode = lookup_fh(req, &argp->file);
334     if (inode == NULL) {
335         result.status = NFS3ERR_STALE;
336         result.read3res_u.resfail.present = FALSE;
337         async_rpc_send_reply(req, &result);
338         return;
339     }
340
341     g_mutex_lock(inode->lock);
342
343     int count = argp->count;
344     if (argp->offset >= inode->size) {
345         count = 0;
346         result.read3res_u.resok.eof = TRUE;
347     } else {
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;
352         else
353             result.read3res_u.resok.eof = FALSE;
354
355         bluesky_file_read(inode, argp->offset, buf, count);
356     }
357
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;
364
365     g_mutex_unlock(inode->lock);
366
367     async_rpc_send_reply(req, &result);
368 }
369
370 void nfsproc3_write_3_svc(write3args *argp, RPCRequest *req)
371 {
372     write3res result;
373     memset(&result, 0, sizeof(result));
374     struct wcc_data wcc;
375     memset(&wcc, 0, sizeof(wcc));
376
377     bluesky_flushd_invoke_conditional(fs);
378
379     BlueSkyInode *inode = lookup_fh(req, &argp->file);
380     if (inode == NULL) {
381         result.status = NFS3ERR_STALE;
382         result.write3res_u.resfail = wcc;
383         async_rpc_send_reply(req, &result);
384         return;
385     }
386
387 #if 0
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;
396         delay.tv_sec = 2;
397         delay.tv_nsec = 0;
398         nanosleep(&delay, NULL);
399     }
400 #endif
401
402     g_mutex_lock(inode->lock);
403
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);
410         return;
411     }
412
413     uint64_t lastbyte = argp->offset + argp->count;
414     if (lastbyte > inode->size) {
415         bluesky_file_truncate(inode, lastbyte);
416     }
417
418     if (argp->data.data_len < argp->count) {
419         /* ??? */
420     } else {
421         bluesky_file_write(inode, argp->offset,
422                            argp->data.data_val, argp->count);
423     }
424
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;
430
431     g_mutex_unlock(inode->lock);
432
433     async_rpc_send_reply(req, &result);
434 }
435
436 void nfsproc3_create_3_svc(create3args *argp, RPCRequest *req)
437 {
438     diropres3 result;
439     memset(&result, 0, sizeof(result));
440     struct wcc_data wcc;
441     memset(&wcc, 0, sizeof(wcc));
442
443     BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
444     if (dir == NULL) {
445         result.status = NFS3ERR_STALE;
446         result.diropres3_u.resfail = wcc;
447         async_rpc_send_reply(req, &result);
448         return;
449     }
450
451     g_mutex_lock(dir->lock);
452
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);
459         return;
460     }
461
462     if (!validate_filename(argp->where.name)
463         || strcmp(argp->where.name, ".") == 0
464         || strcmp(argp->where.name, "..") == 0)
465     {
466         result.status = NFS3ERR_EXIST;
467         result.diropres3_u.resfail = wcc;
468         g_mutex_unlock(dir->lock);
469         async_rpc_send_reply(req, &result);
470         return;
471     }
472
473     BlueSkyInode *file;
474     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_REGULAR);
475     file->nlink = 1;
476     file->mode = 0755;
477     int64_t time = bluesky_get_current_time();
478     printf("time: %"PRIi64"\n", time);
479     file->mtime = time;
480     file->ctime = time;
481     file->atime = time;
482     file->ntime = time;
483     g_mutex_lock(file->lock);
484     bluesky_insert_inode(fs, file);
485     bluesky_directory_insert(dir, argp->where.name, file->inum);
486
487     bluesky_inode_update_ctime(dir, TRUE);
488
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;
494
495     uint64_t fh_bytes;
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;
500
501     g_mutex_unlock(file->lock);
502     g_mutex_unlock(dir->lock);
503
504     async_rpc_send_reply(req, &result);
505 }
506
507 void nfsproc3_mkdir_3_svc(mkdir3args *argp, RPCRequest *req)
508 {
509     diropres3 result;
510     memset(&result, 0, sizeof(result));
511     struct wcc_data wcc;
512     memset(&wcc, 0, sizeof(wcc));
513
514     BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
515     if (dir == NULL) {
516         result.status = NFS3ERR_STALE;
517         result.diropres3_u.resfail = wcc;
518         async_rpc_send_reply(req, &result);
519         return;
520     }
521
522     g_mutex_lock(dir->lock);
523
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);
530         return;
531     }
532
533     if (!validate_filename(argp->where.name)
534         || strcmp(argp->where.name, ".") == 0
535         || strcmp(argp->where.name, "..") == 0)
536     {
537         result.status = NFS3ERR_EXIST;
538         result.diropres3_u.resfail = wcc;
539         g_mutex_unlock(dir->lock);
540         async_rpc_send_reply(req, &result);
541         return;
542     }
543
544     BlueSkyInode *file;
545     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_DIRECTORY);
546     file->nlink = 1;
547     file->mode = 0755;
548     int64_t time = bluesky_get_current_time();
549     file->mtime = time;
550     file->ctime = time;
551     file->atime = time;
552     file->ntime = 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);
557
558     bluesky_inode_update_ctime(dir, TRUE);
559
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;
565
566     uint64_t fh_bytes;
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;
571
572     g_mutex_unlock(file->lock);
573     g_mutex_unlock(dir->lock);
574     async_rpc_send_reply(req, &result);
575 }
576
577 void nfsproc3_symlink_3_svc(symlink3args *argp, RPCRequest *req)
578 {
579     diropres3 result;
580     memset(&result, 0, sizeof(result));
581     struct wcc_data wcc;
582     memset(&wcc, 0, sizeof(wcc));
583
584     BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
585     if (dir == NULL) {
586         result.status = NFS3ERR_STALE;
587         result.diropres3_u.resfail = wcc;
588         async_rpc_send_reply(req, &result);
589         return;
590     }
591     g_mutex_lock(dir->lock);
592
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);
599         return;
600     }
601
602     if (!validate_filename(argp->where.name)
603         || strcmp(argp->where.name, ".") == 0
604         || strcmp(argp->where.name, "..") == 0)
605     {
606         result.status = NFS3ERR_EXIST;
607         result.diropres3_u.resfail = wcc;
608         g_mutex_unlock(dir->lock);
609         async_rpc_send_reply(req, &result);
610         return;
611     }
612
613     BlueSkyInode *file;
614     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_SYMLINK);
615     file->nlink = 1;
616     file->mode = 0755;
617     int64_t time = bluesky_get_current_time();
618     file->mtime = time;
619     file->ctime = time;
620     file->atime = time;
621     file->ntime = 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);
626
627     bluesky_inode_update_ctime(dir, TRUE);
628
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;
634
635     uint64_t fh_bytes;
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;
640
641     g_mutex_unlock(file->lock);
642     g_mutex_unlock(dir->lock);
643     async_rpc_send_reply(req, &result);
644 }
645
646 void nfsproc3_mknod_3_svc(mknod3args *argp, RPCRequest *req)
647 {
648     diropres3 result;
649     memset(&result, 0, sizeof(result));
650
651     result.status = NFS3ERR_NOTSUPP;
652
653     async_rpc_send_reply(req, &result);
654 }
655
656 void nfsproc3_remove_3_svc(diropargs3 *argp, RPCRequest *req)
657 {
658     wccstat3 result;
659     memset(&result, 0, sizeof(result));
660
661     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
662     if (dir == NULL) {
663         result.status = NFS3ERR_STALE;
664         async_rpc_send_reply(req, &result);
665         return;
666     }
667
668     g_mutex_lock(dir->lock);
669
670     encode_pre_wcc(&result.wccstat3_u.wcc, dir);
671
672     if (!validate_filename(argp->name)
673         || strcmp(argp->name, ".") == 0
674         || strcmp(argp->name, "..") == 0)
675     {
676         result.status = NFS3ERR_NOENT;
677         g_mutex_unlock(dir->lock);
678         async_rpc_send_reply(req, &result);
679         return;
680     }
681
682     /* TODO: Decrement link count, deallocate inode if needed. */
683
684     bluesky_directory_remove(dir, argp->name);
685
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,
689                   dir);
690
691     g_mutex_unlock(dir->lock);
692     async_rpc_send_reply(req, &result);
693 }
694
695 void nfsproc3_rmdir_3_svc(diropargs3 *argp, RPCRequest *req)
696 {
697     wccstat3 result;
698     memset(&result, 0, sizeof(result));
699
700     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
701     if (dir == NULL) {
702         result.status = NFS3ERR_STALE;
703         async_rpc_send_reply(req, &result);
704         return;
705     }
706
707     g_mutex_lock(dir->lock);
708
709     encode_pre_wcc(&result.wccstat3_u.wcc, dir);
710
711     if (!validate_filename(argp->name)
712         || strcmp(argp->name, ".") == 0
713         || strcmp(argp->name, "..") == 0)
714     {
715         result.status = NFS3ERR_NOENT;
716         g_mutex_unlock(dir->lock);
717         async_rpc_send_reply(req, &result);
718         return;
719     }
720
721     uint64_t inum = bluesky_directory_lookup(dir, argp->name);
722     BlueSkyInode *inode = bluesky_get_inode(fs, inum);
723     if (inode == NULL) {
724         result.status = NFS3ERR_NOENT;
725         g_mutex_unlock(dir->lock);
726         async_rpc_send_reply(req, &result);
727         return;
728     }
729     g_mutex_lock(inode->lock);
730     schedule_inode_unref(req, inode);
731
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);
737         return;
738     }
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);
746         return;
747     }
748
749     /* TODO: Decrement link count, deallocate inode if needed. */
750
751     bluesky_directory_remove(dir, argp->name);
752
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,
756                   dir);
757
758     g_mutex_unlock(inode->lock);
759     g_mutex_unlock(dir->lock);
760     async_rpc_send_reply(req, &result);
761 }
762
763 void nfsproc3_rename_3_svc(rename3args *argp, RPCRequest *req)
764 {
765     rename3res result;
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;
769
770     BlueSkyInode *dir1 = lookup_fh(req, &argp->from.dir);
771     if (dir1 == NULL) {
772         result.status = NFS3ERR_STALE;
773         async_rpc_send_reply(req, &result);
774         return;
775     }
776
777     BlueSkyInode *dir2 = lookup_fh(req, &argp->to.dir);
778     if (dir2 == NULL) {
779         result.status = NFS3ERR_STALE;
780         async_rpc_send_reply(req, &result);
781         return;
782     }
783
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);
790     }
791     encode_pre_wcc(wcc1, dir1);
792     encode_pre_wcc(wcc2, dir1);
793
794     gboolean status = bluesky_rename(dir1, argp->from.name,
795                                      dir2, argp->to.name,
796                                      TRUE, TRUE);
797
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);
802     if (status)
803         result.status = NFS3_OK;
804     else
805         result.status = NFS3ERR_PERM;
806
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);
811 }
812
813 void nfsproc3_link_3_svc(link3args *argp, RPCRequest *req)
814 {
815     link3res result;
816     memset(&result, 0, sizeof(result));
817     struct wcc_data wcc;
818     memset(&wcc, 0, sizeof(wcc));
819
820     BlueSkyInode *inode = lookup_fh(req, &argp->file);
821     if (inode == NULL) {
822         result.status = NFS3ERR_STALE;
823         result.link3res_u.res.linkdir_wcc = wcc;
824         async_rpc_send_reply(req, &result);
825         return;
826     }
827     g_mutex_lock(inode->lock);
828
829     BlueSkyInode *dir = lookup_fh(req, &argp->link.dir);
830     if (dir == NULL) {
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);
835         return;
836     }
837     g_mutex_lock(dir->lock);
838
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);
846         return;
847     }
848
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)
853     {
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);
859         return;
860     }
861
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);
868         return;
869     }
870     inode->nlink++;
871     bluesky_inode_update_ctime(inode, FALSE);
872     bluesky_inode_update_ctime(dir, TRUE);
873
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;
880
881     g_mutex_unlock(inode->lock);
882     g_mutex_unlock(dir->lock);
883     async_rpc_send_reply(req, &result);
884 }
885
886 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
887                             gpointer unused);
888
889 #define MAX_READDIR_DIRENTS 64
890 void nfsproc3_readdir_3_svc(readdir3args *argp, RPCRequest *req)
891 {
892     readdir3res result;
893     memset(&result, 0, sizeof(result));
894
895     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
896     if (dir == NULL) {
897         result.status = NFS3ERR_STALE;
898         result.readdir3res_u.resfail.present = FALSE;
899         async_rpc_send_reply(req, &result);
900         return;
901     }
902     g_mutex_lock(dir->lock);
903
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));
909
910     entry3 dirents[MAX_READDIR_DIRENTS];
911     int count = 0;
912
913     BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
914     GSequenceIter *i = g_sequence_search(dir->dirents, &start,
915                                          bluesky_dirent_compare, NULL);
916
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;
923         if (count > 0)
924             dirents[count - 1].nextentry = &dirents[count];
925         i = g_sequence_iter_next(i);
926         count++;
927     }
928
929     if (count > 0)
930         result.readdir3res_u.resok.reply.entries = &dirents[0];
931     else
932         result.readdir3res_u.resok.reply.entries = NULL;
933     result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
934
935     g_mutex_unlock(dir->lock);
936     async_rpc_send_reply(req, &result);
937 }
938
939 void nfsproc3_readdirplus_3_svc(readdirplus3args *argp, RPCRequest *req)
940 {
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
946      */
947     size_t dircount = 88 + 16, attrcount = 0;
948     readdirplus3res result;
949     memset(&result, 0, sizeof(result));
950
951     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
952     if (dir == NULL) {
953         result.status = NFS3ERR_STALE;
954         result.readdirplus3res_u.resfail.present = FALSE;
955         async_rpc_send_reply(req, &result);
956         return;
957     }
958     g_mutex_lock(dir->lock);
959
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));
965
966     entryplus3 dirents[MAX_READDIR_DIRENTS];
967     uint64_t fh_bytes[MAX_READDIR_DIRENTS];
968     int count = 0;
969
970     GSequenceIter *i;
971     BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
972
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)
982     {
983         BlueSkyDirent *d = g_sequence_get(i);
984         BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
985         if (inode != NULL)
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);
990     }
991
992     i = g_sequence_search(dir->dirents, &start, bluesky_dirent_compare, NULL);
993     count = 0;
994     dircount = 88 + 16;
995     attrcount = 0;
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);
999         if (inode != NULL) {
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)
1005             {
1006                 g_mutex_unlock(inode->lock);
1007                 bluesky_inode_unref(inode);
1008                 break;
1009             }
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];
1021             if (count > 0)
1022                 dirents[count - 1].nextentry = &dirents[count];
1023             count++;
1024             g_mutex_unlock(inode->lock);
1025             bluesky_inode_unref(inode);
1026         }
1027         i = g_sequence_iter_next(i);
1028     }
1029
1030     if (count > 0)
1031         result.readdirplus3res_u.resok.reply.entries = &dirents[0];
1032     else
1033         result.readdirplus3res_u.resok.reply.entries = NULL;
1034     result.readdirplus3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
1035
1036     g_mutex_unlock(dir->lock);
1037     async_rpc_send_reply(req, &result);
1038 }
1039
1040 void nfsproc3_fsstat_3_svc(nfs_fh3 *argp, RPCRequest *req)
1041 {
1042     fsstat3res result;
1043     memset(&result, 0, sizeof(result));
1044
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);
1050         return;
1051     }
1052     g_mutex_lock(inode->lock);
1053
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);
1057
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;
1065
1066     g_mutex_unlock(inode->lock);
1067     async_rpc_send_reply(req, &result);
1068 }
1069
1070 void nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, RPCRequest *req)
1071 {
1072     fsinfo3res result;
1073     memset(&result, 0, sizeof(result));
1074
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;
1092
1093     g_mutex_unlock(inode->lock);
1094     bluesky_inode_unref(inode);
1095     async_rpc_send_reply(req, &result);
1096 }
1097
1098 void nfsproc3_pathconf_3_svc(nfs_fh3 *argp, RPCRequest *req)
1099 {
1100     pathconf3res result;
1101     memset(&result, 0, sizeof(result));
1102
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;
1114
1115     g_mutex_unlock(inode->lock);
1116     bluesky_inode_unref(inode);
1117     async_rpc_send_reply(req, &result);
1118 }
1119
1120 void nfsproc3_commit_3_svc(commit3args *argp, RPCRequest *req)
1121 {
1122     commit3res result;
1123     memset(&result, 0, sizeof(result));
1124
1125     result.status = NFS3_OK;
1126
1127     BlueSkyInode *inode = lookup_fh(req, &argp->file);
1128     if (inode == NULL) {
1129         result.status = NFS3ERR_STALE;
1130         async_rpc_send_reply(req, &result);
1131         return;
1132     }
1133
1134     g_mutex_lock(inode->lock);
1135     encode_pre_wcc(&result.commit3res_u.resok.file_wcc, inode);
1136
1137     //bluesky_inode_do_sync(inode);
1138
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);
1141
1142     g_mutex_unlock(inode->lock);
1143
1144     async_rpc_send_reply(req, &result);
1145 }