Disable most debugging print messages; should help with performance.
[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     file->mtime = time;
479     file->ctime = time;
480     file->atime = time;
481     file->ntime = time;
482     g_mutex_lock(file->lock);
483     bluesky_insert_inode(fs, file);
484     bluesky_directory_insert(dir, argp->where.name, file->inum);
485
486     bluesky_inode_update_ctime(dir, TRUE);
487
488     wcc.after.present = TRUE;
489     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
490     result.diropres3_u.resok.obj_attributes.present = TRUE;
491     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
492     result.diropres3_u.resok.dir_wcc = wcc;
493
494     uint64_t fh_bytes;
495     fh_bytes = GUINT64_TO_BE(file->inum);
496     result.diropres3_u.resok.obj.present = TRUE;
497     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
498     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
499
500     g_mutex_unlock(file->lock);
501     g_mutex_unlock(dir->lock);
502
503     async_rpc_send_reply(req, &result);
504 }
505
506 void nfsproc3_mkdir_3_svc(mkdir3args *argp, RPCRequest *req)
507 {
508     diropres3 result;
509     memset(&result, 0, sizeof(result));
510     struct wcc_data wcc;
511     memset(&wcc, 0, sizeof(wcc));
512
513     BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
514     if (dir == NULL) {
515         result.status = NFS3ERR_STALE;
516         result.diropres3_u.resfail = wcc;
517         async_rpc_send_reply(req, &result);
518         return;
519     }
520
521     g_mutex_lock(dir->lock);
522
523     encode_pre_wcc(&wcc, dir);
524     if (dir->type != BLUESKY_DIRECTORY) {
525         result.status = NFS3ERR_NOTDIR;
526         result.diropres3_u.resfail = wcc;
527         g_mutex_unlock(dir->lock);
528         async_rpc_send_reply(req, &result);
529         return;
530     }
531
532     if (!validate_filename(argp->where.name)
533         || strcmp(argp->where.name, ".") == 0
534         || strcmp(argp->where.name, "..") == 0)
535     {
536         result.status = NFS3ERR_EXIST;
537         result.diropres3_u.resfail = wcc;
538         g_mutex_unlock(dir->lock);
539         async_rpc_send_reply(req, &result);
540         return;
541     }
542
543     BlueSkyInode *file;
544     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_DIRECTORY);
545     file->nlink = 1;
546     file->mode = 0755;
547     int64_t time = bluesky_get_current_time();
548     file->mtime = time;
549     file->ctime = time;
550     file->atime = time;
551     file->ntime = time;
552     g_mutex_lock(file->lock);
553     bluesky_insert_inode(fs, file);
554     bluesky_directory_insert(dir, argp->where.name, file->inum);
555     set_attributes(file, &argp->attributes);
556
557     bluesky_inode_update_ctime(dir, TRUE);
558
559     wcc.after.present = TRUE;
560     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
561     result.diropres3_u.resok.obj_attributes.present = TRUE;
562     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
563     result.diropres3_u.resok.dir_wcc = wcc;
564
565     uint64_t fh_bytes;
566     fh_bytes = GUINT64_TO_BE(file->inum);
567     result.diropres3_u.resok.obj.present = TRUE;
568     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
569     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
570
571     g_mutex_unlock(file->lock);
572     g_mutex_unlock(dir->lock);
573     async_rpc_send_reply(req, &result);
574 }
575
576 void nfsproc3_symlink_3_svc(symlink3args *argp, RPCRequest *req)
577 {
578     diropres3 result;
579     memset(&result, 0, sizeof(result));
580     struct wcc_data wcc;
581     memset(&wcc, 0, sizeof(wcc));
582
583     BlueSkyInode *dir = lookup_fh(req, &argp->where.dir);
584     if (dir == NULL) {
585         result.status = NFS3ERR_STALE;
586         result.diropres3_u.resfail = wcc;
587         async_rpc_send_reply(req, &result);
588         return;
589     }
590     g_mutex_lock(dir->lock);
591
592     encode_pre_wcc(&wcc, dir);
593     if (dir->type != BLUESKY_DIRECTORY) {
594         result.status = NFS3ERR_NOTDIR;
595         result.diropres3_u.resfail = wcc;
596         g_mutex_unlock(dir->lock);
597         async_rpc_send_reply(req, &result);
598         return;
599     }
600
601     if (!validate_filename(argp->where.name)
602         || strcmp(argp->where.name, ".") == 0
603         || strcmp(argp->where.name, "..") == 0)
604     {
605         result.status = NFS3ERR_EXIST;
606         result.diropres3_u.resfail = wcc;
607         g_mutex_unlock(dir->lock);
608         async_rpc_send_reply(req, &result);
609         return;
610     }
611
612     BlueSkyInode *file;
613     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_SYMLINK);
614     file->nlink = 1;
615     file->mode = 0755;
616     int64_t time = bluesky_get_current_time();
617     file->mtime = time;
618     file->ctime = time;
619     file->atime = time;
620     file->ntime = time;
621     file->symlink_contents = g_strdup(argp->symlink.symlink_data);
622     g_mutex_lock(file->lock);
623     bluesky_insert_inode(fs, file);
624     bluesky_directory_insert(dir, argp->where.name, file->inum);
625
626     bluesky_inode_update_ctime(dir, TRUE);
627
628     wcc.after.present = TRUE;
629     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
630     result.diropres3_u.resok.obj_attributes.present = TRUE;
631     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
632     result.diropres3_u.resok.dir_wcc = wcc;
633
634     uint64_t fh_bytes;
635     fh_bytes = GUINT64_TO_BE(file->inum);
636     result.diropres3_u.resok.obj.present = TRUE;
637     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
638     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
639
640     g_mutex_unlock(file->lock);
641     g_mutex_unlock(dir->lock);
642     async_rpc_send_reply(req, &result);
643 }
644
645 void nfsproc3_mknod_3_svc(mknod3args *argp, RPCRequest *req)
646 {
647     diropres3 result;
648     memset(&result, 0, sizeof(result));
649
650     result.status = NFS3ERR_NOTSUPP;
651
652     async_rpc_send_reply(req, &result);
653 }
654
655 void nfsproc3_remove_3_svc(diropargs3 *argp, RPCRequest *req)
656 {
657     wccstat3 result;
658     memset(&result, 0, sizeof(result));
659
660     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
661     if (dir == NULL) {
662         result.status = NFS3ERR_STALE;
663         async_rpc_send_reply(req, &result);
664         return;
665     }
666
667     g_mutex_lock(dir->lock);
668
669     encode_pre_wcc(&result.wccstat3_u.wcc, dir);
670
671     if (!validate_filename(argp->name)
672         || strcmp(argp->name, ".") == 0
673         || strcmp(argp->name, "..") == 0)
674     {
675         result.status = NFS3ERR_NOENT;
676         g_mutex_unlock(dir->lock);
677         async_rpc_send_reply(req, &result);
678         return;
679     }
680
681     /* TODO: Decrement link count, deallocate inode if needed. */
682
683     bluesky_directory_remove(dir, argp->name);
684
685     result.status = NFS3_OK;
686     result.wccstat3_u.wcc.after.present = TRUE;
687     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
688                   dir);
689
690     g_mutex_unlock(dir->lock);
691     async_rpc_send_reply(req, &result);
692 }
693
694 void nfsproc3_rmdir_3_svc(diropargs3 *argp, RPCRequest *req)
695 {
696     wccstat3 result;
697     memset(&result, 0, sizeof(result));
698
699     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
700     if (dir == NULL) {
701         result.status = NFS3ERR_STALE;
702         async_rpc_send_reply(req, &result);
703         return;
704     }
705
706     g_mutex_lock(dir->lock);
707
708     encode_pre_wcc(&result.wccstat3_u.wcc, dir);
709
710     if (!validate_filename(argp->name)
711         || strcmp(argp->name, ".") == 0
712         || strcmp(argp->name, "..") == 0)
713     {
714         result.status = NFS3ERR_NOENT;
715         g_mutex_unlock(dir->lock);
716         async_rpc_send_reply(req, &result);
717         return;
718     }
719
720     uint64_t inum = bluesky_directory_lookup(dir, argp->name);
721     BlueSkyInode *inode = bluesky_get_inode(fs, inum);
722     if (inode == NULL) {
723         result.status = NFS3ERR_NOENT;
724         g_mutex_unlock(dir->lock);
725         async_rpc_send_reply(req, &result);
726         return;
727     }
728     g_mutex_lock(inode->lock);
729     schedule_inode_unref(req, inode);
730
731     if (inode->type != BLUESKY_DIRECTORY) {
732         result.status = NFS3ERR_NOTDIR;
733         g_mutex_unlock(inode->lock);
734         g_mutex_unlock(dir->lock);
735         async_rpc_send_reply(req, &result);
736         return;
737     }
738     if (g_sequence_get_length(inode->dirents) > 0) {
739         printf("Directory not empty: %d entries\n",
740                g_sequence_get_length(inode->dirents));
741         result.status = NFS3ERR_NOTEMPTY;
742         g_mutex_unlock(inode->lock);
743         g_mutex_unlock(dir->lock);
744         async_rpc_send_reply(req, &result);
745         return;
746     }
747
748     /* TODO: Decrement link count, deallocate inode if needed. */
749
750     bluesky_directory_remove(dir, argp->name);
751
752     result.status = NFS3_OK;
753     result.wccstat3_u.wcc.after.present = TRUE;
754     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
755                   dir);
756
757     g_mutex_unlock(inode->lock);
758     g_mutex_unlock(dir->lock);
759     async_rpc_send_reply(req, &result);
760 }
761
762 void nfsproc3_rename_3_svc(rename3args *argp, RPCRequest *req)
763 {
764     rename3res result;
765     memset(&result, 0, sizeof(result));
766     wcc_data *wcc1 = &result.rename3res_u.res.fromdir_wcc;
767     wcc_data *wcc2 = &result.rename3res_u.res.todir_wcc;
768
769     BlueSkyInode *dir1 = lookup_fh(req, &argp->from.dir);
770     if (dir1 == NULL) {
771         result.status = NFS3ERR_STALE;
772         async_rpc_send_reply(req, &result);
773         return;
774     }
775
776     BlueSkyInode *dir2 = lookup_fh(req, &argp->to.dir);
777     if (dir2 == NULL) {
778         result.status = NFS3ERR_STALE;
779         async_rpc_send_reply(req, &result);
780         return;
781     }
782
783     if (dir1->inum < dir2->inum) {
784         g_mutex_lock(dir1->lock);
785         g_mutex_lock(dir2->lock);
786     } else if (dir1->inum > dir2->inum) {
787         g_mutex_lock(dir2->lock);
788         g_mutex_lock(dir1->lock);
789     }
790     encode_pre_wcc(wcc1, dir1);
791     encode_pre_wcc(wcc2, dir1);
792
793     gboolean status = bluesky_rename(dir1, argp->from.name,
794                                      dir2, argp->to.name,
795                                      TRUE, TRUE);
796
797     wcc1->after.present = TRUE;
798     encode_fattr3(&wcc1->after.post_op_attr_u.attributes, dir1);
799     wcc2->after.present = TRUE;
800     encode_fattr3(&wcc2->after.post_op_attr_u.attributes, dir2);
801     if (status)
802         result.status = NFS3_OK;
803     else
804         result.status = NFS3ERR_PERM;
805
806     g_mutex_unlock(dir1->lock);
807     if (dir1->inum != dir2->inum)
808         g_mutex_unlock(dir2->lock);
809     async_rpc_send_reply(req, &result);
810 }
811
812 void nfsproc3_link_3_svc(link3args *argp, RPCRequest *req)
813 {
814     link3res result;
815     memset(&result, 0, sizeof(result));
816     struct wcc_data wcc;
817     memset(&wcc, 0, sizeof(wcc));
818
819     BlueSkyInode *inode = lookup_fh(req, &argp->file);
820     if (inode == NULL) {
821         result.status = NFS3ERR_STALE;
822         result.link3res_u.res.linkdir_wcc = wcc;
823         async_rpc_send_reply(req, &result);
824         return;
825     }
826     g_mutex_lock(inode->lock);
827
828     BlueSkyInode *dir = lookup_fh(req, &argp->link.dir);
829     if (dir == NULL) {
830         result.status = NFS3ERR_STALE;
831         result.link3res_u.res.linkdir_wcc = wcc;
832         g_mutex_unlock(inode->lock);
833         async_rpc_send_reply(req, &result);
834         return;
835     }
836     g_mutex_lock(dir->lock);
837
838     encode_pre_wcc(&wcc, dir);
839     if (dir->type != BLUESKY_DIRECTORY) {
840         result.status = NFS3ERR_NOTDIR;
841         result.link3res_u.res.linkdir_wcc = wcc;
842         g_mutex_unlock(inode->lock);
843         g_mutex_unlock(dir->lock);
844         async_rpc_send_reply(req, &result);
845         return;
846     }
847
848     if (!validate_filename(argp->link.name)
849         || strcmp(argp->link.name, ".") == 0
850         || strcmp(argp->link.name, "..") == 0
851         || bluesky_directory_lookup(dir, argp->link.name) != 0)
852     {
853         result.status = NFS3ERR_EXIST;
854         result.link3res_u.res.linkdir_wcc = wcc;
855         g_mutex_unlock(inode->lock);
856         g_mutex_unlock(dir->lock);
857         async_rpc_send_reply(req, &result);
858         return;
859     }
860
861     if (!bluesky_directory_insert(dir, argp->link.name, inode->inum)) {
862         result.status = NFS3ERR_EXIST;
863         result.link3res_u.res.linkdir_wcc = wcc;
864         g_mutex_unlock(inode->lock);
865         g_mutex_unlock(dir->lock);
866         async_rpc_send_reply(req, &result);
867         return;
868     }
869     inode->nlink++;
870     bluesky_inode_update_ctime(inode, FALSE);
871     bluesky_inode_update_ctime(dir, TRUE);
872
873     result.status = NFS3_OK;
874     wcc.after.present = TRUE;
875     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
876     result.link3res_u.res.file_attributes.present = TRUE;
877     encode_fattr3(&result.link3res_u.res.file_attributes.post_op_attr_u.attributes, inode);
878     result.link3res_u.res.linkdir_wcc = wcc;
879
880     g_mutex_unlock(inode->lock);
881     g_mutex_unlock(dir->lock);
882     async_rpc_send_reply(req, &result);
883 }
884
885 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
886                             gpointer unused);
887
888 #define MAX_READDIR_DIRENTS 64
889 void nfsproc3_readdir_3_svc(readdir3args *argp, RPCRequest *req)
890 {
891     readdir3res result;
892     memset(&result, 0, sizeof(result));
893
894     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
895     if (dir == NULL) {
896         result.status = NFS3ERR_STALE;
897         result.readdir3res_u.resfail.present = FALSE;
898         async_rpc_send_reply(req, &result);
899         return;
900     }
901     g_mutex_lock(dir->lock);
902
903     result.status = NFS3_OK;
904     result.readdir3res_u.resok.dir_attributes.present = TRUE;
905     encode_fattr3(&result.readdir3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
906     memset(result.readdir3res_u.resok.cookieverf, 0,
907            sizeof(result.readdir3res_u.resok.cookieverf));
908
909     entry3 dirents[MAX_READDIR_DIRENTS];
910     int count = 0;
911
912     BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
913     GSequenceIter *i = g_sequence_search(dir->dirents, &start,
914                                          bluesky_dirent_compare, NULL);
915
916     while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
917         BlueSkyDirent *d = g_sequence_get(i);
918         dirents[count].fileid = d->inum;
919         dirents[count].name = d->name;
920         dirents[count].cookie = d->cookie;
921         dirents[count].nextentry = NULL;
922         if (count > 0)
923             dirents[count - 1].nextentry = &dirents[count];
924         i = g_sequence_iter_next(i);
925         count++;
926     }
927
928     if (count > 0)
929         result.readdir3res_u.resok.reply.entries = &dirents[0];
930     else
931         result.readdir3res_u.resok.reply.entries = NULL;
932     result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
933
934     g_mutex_unlock(dir->lock);
935     async_rpc_send_reply(req, &result);
936 }
937
938 void nfsproc3_readdirplus_3_svc(readdirplus3args *argp, RPCRequest *req)
939 {
940     /* XDR-encoded sizes:
941      *   post_op_attr: 88 bytes
942      *   base readdirplus3resok: 88 + 16 bytes
943      *   base directory entry: 24 bytes + filename
944      *   attributes/fh3: 88 + 8 + filehandle size
945      */
946     size_t dircount = 88 + 16, attrcount = 0;
947     readdirplus3res result;
948     memset(&result, 0, sizeof(result));
949
950     BlueSkyInode *dir = lookup_fh(req, &argp->dir);
951     if (dir == NULL) {
952         result.status = NFS3ERR_STALE;
953         result.readdirplus3res_u.resfail.present = FALSE;
954         async_rpc_send_reply(req, &result);
955         return;
956     }
957     g_mutex_lock(dir->lock);
958
959     result.status = NFS3_OK;
960     result.readdirplus3res_u.resok.dir_attributes.present = TRUE;
961     encode_fattr3(&result.readdirplus3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
962     memset(result.readdirplus3res_u.resok.cookieverf, 0,
963            sizeof(result.readdirplus3res_u.resok.cookieverf));
964
965     entryplus3 dirents[MAX_READDIR_DIRENTS];
966     uint64_t fh_bytes[MAX_READDIR_DIRENTS];
967     int count = 0;
968
969     GSequenceIter *i;
970     BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
971
972     /* Perform a prefetch pass on inodes: for all the inodes we think we will
973      * return information about, try to load each one but don't wait.  This
974      * should let multiple inodes be fetched in parallel, instead of
975      * sequentially in the loop that follows. */
976     i = g_sequence_search(dir->dirents, &start, bluesky_dirent_compare, NULL);
977     while (count < MAX_READDIR_DIRENTS
978            && !g_sequence_iter_is_end(i)
979            && dircount <= argp->dircount
980            && dircount + attrcount <= argp->maxcount)
981     {
982         BlueSkyDirent *d = g_sequence_get(i);
983         BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
984         if (inode != NULL)
985             bluesky_inode_unref(inode);
986         dircount += 24 + ((strlen(d->name) + 3) & ~3);
987         attrcount += 88 + 8 + 8;
988         i = g_sequence_iter_next(i);
989     }
990
991     i = g_sequence_search(dir->dirents, &start, bluesky_dirent_compare, NULL);
992     count = 0;
993     dircount = 88 + 16;
994     attrcount = 0;
995     while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
996         BlueSkyDirent *d = g_sequence_get(i);
997         BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
998         if (inode != NULL) {
999             g_mutex_lock(inode->lock);
1000             dircount += 24 + ((strlen(d->name) + 3) & ~3);
1001             attrcount += 88 + 8 + 8;
1002             if (dircount > argp->dircount
1003                 || dircount + attrcount > argp->maxcount)
1004             {
1005                 g_mutex_unlock(inode->lock);
1006                 bluesky_inode_unref(inode);
1007                 break;
1008             }
1009             dirents[count].fileid = d->inum;
1010             dirents[count].name = d->name;
1011             dirents[count].cookie = d->cookie;
1012             dirents[count].nextentry = NULL;
1013             dirents[count].name_attributes.present = TRUE;
1014             encode_fattr3(&dirents[count].name_attributes.post_op_attr_u.attributes, inode);
1015             fh_bytes[count] = GUINT64_TO_BE(d->inum);
1016             dirents[count].name_handle.present = TRUE;
1017             dirents[count].name_handle.post_op_fh3_u.handle.data.data_len = 8;
1018             dirents[count].name_handle.post_op_fh3_u.handle.data.data_val
1019                 = (char *)&fh_bytes[count];
1020             if (count > 0)
1021                 dirents[count - 1].nextentry = &dirents[count];
1022             count++;
1023             g_mutex_unlock(inode->lock);
1024             bluesky_inode_unref(inode);
1025         }
1026         i = g_sequence_iter_next(i);
1027     }
1028
1029     if (count > 0)
1030         result.readdirplus3res_u.resok.reply.entries = &dirents[0];
1031     else
1032         result.readdirplus3res_u.resok.reply.entries = NULL;
1033     result.readdirplus3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
1034
1035     g_mutex_unlock(dir->lock);
1036     async_rpc_send_reply(req, &result);
1037 }
1038
1039 void nfsproc3_fsstat_3_svc(nfs_fh3 *argp, RPCRequest *req)
1040 {
1041     fsstat3res result;
1042     memset(&result, 0, sizeof(result));
1043
1044     BlueSkyInode *inode = lookup_fh(req, argp);
1045     if (inode == NULL) {
1046         result.status = NFS3ERR_STALE;
1047         result.fsstat3res_u.resfail.present = FALSE;
1048         async_rpc_send_reply(req, &result);
1049         return;
1050     }
1051     g_mutex_lock(inode->lock);
1052
1053     result.status = NFS3_OK;
1054     result.fsstat3res_u.resok.obj_attributes.present = TRUE;
1055     encode_fattr3(&result.fsstat3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
1056
1057     result.fsstat3res_u.resok.tbytes = (1 << 30);
1058     result.fsstat3res_u.resok.fbytes = (1 << 30);
1059     result.fsstat3res_u.resok.abytes = (1 << 30);
1060     result.fsstat3res_u.resok.tfiles = 0;
1061     result.fsstat3res_u.resok.ffiles = 0;
1062     result.fsstat3res_u.resok.afiles = 0;
1063     result.fsstat3res_u.resok.invarsec = 0;
1064
1065     g_mutex_unlock(inode->lock);
1066     async_rpc_send_reply(req, &result);
1067 }
1068
1069 void nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, RPCRequest *req)
1070 {
1071     fsinfo3res result;
1072     memset(&result, 0, sizeof(result));
1073
1074     BlueSkyInode *inode = bluesky_get_inode(fs, 1);
1075     g_mutex_lock(inode->lock);
1076     result.status = NFS3_OK;
1077     result.fsinfo3res_u.resok.obj_attributes.present = TRUE;
1078     encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
1079     result.fsinfo3res_u.resok.rtmax = NFS_MAXSIZE;
1080     result.fsinfo3res_u.resok.rtpref = NFS_MAXSIZE;
1081     result.fsinfo3res_u.resok.rtmult = NFS_BLOCKSIZE;
1082     result.fsinfo3res_u.resok.wtmax = NFS_MAXSIZE;
1083     result.fsinfo3res_u.resok.wtpref = NFS_MAXSIZE;
1084     result.fsinfo3res_u.resok.wtmult = NFS_BLOCKSIZE;
1085     result.fsinfo3res_u.resok.dtpref = NFS_BLOCKSIZE;
1086     result.fsinfo3res_u.resok.maxfilesize = 0x7fffffffffffffffULL;
1087     result.fsinfo3res_u.resok.time_delta.seconds = 0;
1088     result.fsinfo3res_u.resok.time_delta.nseconds = 1000;
1089     result.fsinfo3res_u.resok.properties
1090         = FSF3_LINK | FSF3_SYMLINK | FSF3_HOMOGENEOUS | FSF3_CANSETTIME;
1091
1092     g_mutex_unlock(inode->lock);
1093     bluesky_inode_unref(inode);
1094     async_rpc_send_reply(req, &result);
1095 }
1096
1097 void nfsproc3_pathconf_3_svc(nfs_fh3 *argp, RPCRequest *req)
1098 {
1099     pathconf3res result;
1100     memset(&result, 0, sizeof(result));
1101
1102     BlueSkyInode *inode = bluesky_get_inode(fs, 1);
1103     g_mutex_lock(inode->lock);
1104     result.status = NFS3_OK;
1105     result.pathconf3res_u.resok.obj_attributes.present = TRUE;
1106     encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
1107     result.pathconf3res_u.resok.linkmax = 0xffffffff;
1108     result.pathconf3res_u.resok.name_max = 255;
1109     result.pathconf3res_u.resok.no_trunc = TRUE;
1110     result.pathconf3res_u.resok.chown_restricted = TRUE;
1111     result.pathconf3res_u.resok.case_insensitive = FALSE;
1112     result.pathconf3res_u.resok.case_preserving = TRUE;
1113
1114     g_mutex_unlock(inode->lock);
1115     bluesky_inode_unref(inode);
1116     async_rpc_send_reply(req, &result);
1117 }
1118
1119 void nfsproc3_commit_3_svc(commit3args *argp, RPCRequest *req)
1120 {
1121     commit3res result;
1122     memset(&result, 0, sizeof(result));
1123
1124     result.status = NFS3_OK;
1125
1126     BlueSkyInode *inode = lookup_fh(req, &argp->file);
1127     if (inode == NULL) {
1128         result.status = NFS3ERR_STALE;
1129         async_rpc_send_reply(req, &result);
1130         return;
1131     }
1132
1133     g_mutex_lock(inode->lock);
1134     encode_pre_wcc(&result.commit3res_u.resok.file_wcc, inode);
1135
1136     //bluesky_inode_do_sync(inode);
1137
1138     result.commit3res_u.resok.file_wcc.after.present = TRUE;
1139     encode_fattr3(&result.commit3res_u.resok.file_wcc.after.post_op_attr_u.attributes, inode);
1140
1141     g_mutex_unlock(inode->lock);
1142
1143     async_rpc_send_reply(req, &result);
1144 }