Improve NFS readdirplus to estimate response sizes.
[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 static int null_int;
13 static void *null_result = (void *)&null_int;
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 /* Look up a BlueSkyInode given an NFS filehandle.  Returns NULL if the
34  * filehandle is invalid. */
35 BlueSkyInode *lookup_fh(nfs_fh3 *fh)
36 {
37     BlueSkyInode *inode = NULL;
38     if (fh->data.data_len == 8) {
39         uint64_t inum = GUINT64_FROM_BE(*(uint64_t *)(fh->data.data_val));
40         inode = bluesky_get_inode(fs, inum);
41     }
42     return inode;
43 }
44
45 int64_t decode_nfstime3(nfstime3 *time)
46 {
47     int64_t result = (int64_t)time->seconds * 1000000;
48     result += time->nseconds / 1000;
49     return result;
50 }
51
52 void set_attributes(BlueSkyInode *inode, sattr3 *attributes)
53 {
54     int64_t now = bluesky_get_current_time();
55
56     if (attributes->mode.set) {
57         inode->mode = attributes->mode.set_uint32_u.val;
58     }
59
60     if (attributes->uid.set) {
61         inode->uid = attributes->uid.set_uint32_u.val;
62     }
63
64     if (attributes->gid.set) {
65         inode->gid = attributes->gid.set_uint32_u.val;
66     }
67
68     if (attributes->size.set) {
69         if (inode->type == BLUESKY_REGULAR) {
70             bluesky_file_truncate(inode, attributes->size.set_uint64_u.val);
71             inode->mtime = now;
72         }
73     }
74
75     switch (attributes->atime.set) {
76     case DONT_CHANGE:
77         break;
78     case SET_TO_SERVER_TIME:
79         inode->atime = now;
80         break;
81     case SET_TO_CLIENT_TIME:
82         inode->atime = decode_nfstime3(&attributes->atime.set_time_u.time);
83         break;
84     }
85
86     switch (attributes->mtime.set) {
87     case DONT_CHANGE:
88         break;
89     case SET_TO_SERVER_TIME:
90         inode->mtime = now;
91         break;
92     case SET_TO_CLIENT_TIME:
93         inode->mtime = decode_nfstime3(&attributes->mtime.set_time_u.time);
94         break;
95     }
96
97     inode->ctime = now;
98     inode->change_count++;
99 }
100
101 /* Copy inode attributes into NFS response.  The BlueSkyInode should be locked
102  * by the caller. */
103 void encode_fattr3(struct fattr3 *result, BlueSkyInode *inode)
104 {
105     result->type = inode->type;
106     result->mode = inode->mode;
107     result->nlink = inode->nlink;
108     result->uid = inode->uid;
109     result->gid = inode->gid;
110     result->size = inode->size;
111     result->used = 0;
112     result->rdev.major = 0;
113     result->rdev.minor = 0;
114     result->fsid = 0;
115     result->fileid = inode->inum;
116     result->atime.seconds = inode->atime / 1000000;
117     result->atime.nseconds = (inode->atime % 1000000) * 1000;
118     result->mtime.seconds = inode->mtime / 1000000;
119     result->mtime.nseconds = (inode->mtime % 1000000) * 1000;
120     result->ctime.seconds = inode->ctime / 1000000;
121     result->ctime.nseconds = (inode->ctime % 1000000) * 1000;
122
123     switch (inode->type) {
124     case BLUESKY_SYMLINK:
125         result->size = strlen(inode->symlink_contents);
126         break;
127     default:
128         break;
129     }
130 }
131
132 void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode)
133 {
134     wcc->before.present = TRUE;
135     wcc->before.pre_op_attr_u.attributes.size = inode->size;
136     wcc->before.pre_op_attr_u.attributes.mtime.seconds = inode->mtime / 1000000;
137     wcc->before.pre_op_attr_u.attributes.mtime.nseconds = (inode->mtime % 1000000) * 1000;
138     wcc->before.pre_op_attr_u.attributes.ctime.seconds = inode->ctime / 1000000;
139     wcc->before.pre_op_attr_u.attributes.ctime.nseconds = (inode->ctime % 1000000) * 1000;
140 }
141
142 void *
143 nfsproc3_null_3_svc(void *argp, struct svc_req *rqstp)
144 {
145     return null_result;
146 }
147
148 getattr3res *
149 nfsproc3_getattr_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
150 {
151     static getattr3res result;
152
153     BlueSkyInode *inode = lookup_fh(argp);
154     if (inode != NULL) {
155         result.status = NFS3_OK;
156         encode_fattr3(&result.getattr3res_u.attributes, inode);
157     } else {
158         result.status = NFS3ERR_STALE;
159     }
160
161     return &result;
162 }
163
164 wccstat3 *
165 nfsproc3_setattr_3_svc(setattr3args *argp, struct svc_req *rqstp)
166 {
167     static wccstat3 result;
168
169     result.wccstat3_u.wcc.before.present = FALSE;
170     result.wccstat3_u.wcc.after.present = FALSE;
171     BlueSkyInode *inode = lookup_fh(&argp->object);
172     if (inode == NULL) {
173         result.status = NFS3ERR_STALE;
174         return &result;
175     }
176
177     encode_pre_wcc(&result.wccstat3_u.wcc, inode);
178     if (argp->guard.check) {
179         if (inode->ctime != decode_nfstime3(&argp->guard.sattrguard3_u.ctime)) {
180             result.status = NFS3ERR_NOT_SYNC;
181             result.wccstat3_u.wcc.after.present = TRUE;
182             encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, inode);
183             return &result;
184         }
185     }
186
187     set_attributes(inode, &argp->new_attributes);
188
189     result.wccstat3_u.wcc.after.present = TRUE;
190     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
191                   inode);
192     result.status = NFS3_OK;
193
194     return &result;
195 }
196
197 lookup3res *
198 nfsproc3_lookup_3_svc(diropargs3 *argp, struct svc_req *rqstp)
199 {
200     static lookup3res result;
201
202     BlueSkyInode *dir = lookup_fh(&argp->dir);
203     if (dir == NULL) {
204         result.status = NFS3ERR_STALE;
205         result.lookup3res_u.resfail.present = FALSE;
206         return &result;
207     }
208
209     result.lookup3res_u.resfail.present = TRUE;
210     encode_fattr3(&result.lookup3res_u.resfail.post_op_attr_u.attributes, dir);
211     if (!validate_filename(argp->name)) {
212         if (strlen(argp->name) > 255)
213             result.status = NFS3ERR_NAMETOOLONG;
214         else
215             result.status = NFS3ERR_NOENT;
216         return &result;
217     }
218
219     /* TODO: Special-case "." and "..". */
220     uint64_t inum = bluesky_directory_lookup(dir, argp->name);
221     if (inum == 0) {
222         result.status = NFS3ERR_NOENT;
223         return &result;
224     }
225     BlueSkyInode *inode = bluesky_get_inode(fs, inum);
226     if (inode == NULL) {
227         result.status = NFS3ERR_NOENT;
228         return &result;
229     }
230
231     result.status = NFS3_OK;
232     result.lookup3res_u.resok.dir_attributes.present = TRUE;
233     encode_fattr3(&result.lookup3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
234     result.lookup3res_u.resok.obj_attributes.present = TRUE;
235     encode_fattr3(&result.lookup3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
236
237     static uint64_t fh_bytes;
238     fh_bytes = GUINT64_TO_BE(inum);
239     result.lookup3res_u.resok.object.data.data_len = 8;
240     result.lookup3res_u.resok.object.data.data_val = (char *)&fh_bytes;
241
242     return &result;
243 }
244
245 access3res *
246 nfsproc3_access_3_svc(access3args *argp, struct svc_req *rqstp)
247 {
248     static access3res result;
249
250     BlueSkyInode *inode = lookup_fh(&argp->object);
251     if (inode == NULL) {
252         result.status = NFS3ERR_STALE;
253         result.access3res_u.resfail.present = FALSE;
254         return &result;
255     }
256
257     result.status = NFS3_OK;
258     result.access3res_u.resok.obj_attributes.present = TRUE;
259     encode_fattr3(&result.access3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
260     result.access3res_u.resok.access = argp->access;
261
262     return &result;
263 }
264
265 readlink3res *
266 nfsproc3_readlink_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
267 {
268     static readlink3res result;
269     memset(&result, 0, sizeof(result));
270
271     BlueSkyInode *inode = lookup_fh(argp);
272     if (inode != NULL) {
273         if (inode->type == BLUESKY_SYMLINK) {
274             result.status = NFS3_OK;
275             result.readlink3res_u.resok.symlink_attributes.present = TRUE;
276             encode_fattr3(&result.readlink3res_u.resok.symlink_attributes.post_op_attr_u.attributes, inode);
277             result.readlink3res_u.resok.data = inode->symlink_contents;
278         } else {
279             result.status = NFS3ERR_INVAL;
280             result.readlink3res_u.resfail.present = TRUE;
281             encode_fattr3(&result.readlink3res_u.resfail.post_op_attr_u.attributes, inode);
282         }
283     } else {
284         result.status = NFS3ERR_STALE;
285     }
286
287     return &result;
288 }
289
290 read3res *
291 nfsproc3_read_3_svc(read3args *argp, struct svc_req *rqstp)
292 {
293     static read3res result;
294     static char buf[32768];
295
296     BlueSkyInode *inode = lookup_fh(&argp->file);
297     if (inode == NULL) {
298         result.status = NFS3ERR_STALE;
299         result.read3res_u.resfail.present = FALSE;
300         return &result;
301     }
302
303     int count = argp->count;
304     if (argp->offset >= inode->size) {
305         count = 0;
306         result.read3res_u.resok.eof = TRUE;
307     } else {
308         count = MIN(count, inode->size - argp->offset);
309         if (argp->offset + count == inode->size)
310             result.read3res_u.resok.eof = TRUE;
311         else
312             result.read3res_u.resok.eof = FALSE;
313
314         bluesky_file_read(inode, argp->offset, buf, count);
315     }
316
317     result.status = NFS3_OK;
318     result.read3res_u.resok.file_attributes.present = TRUE;
319     encode_fattr3(&result.read3res_u.resok.file_attributes.post_op_attr_u.attributes, inode);
320     result.read3res_u.resok.count = count;
321     result.read3res_u.resok.data.data_val = buf;
322     result.read3res_u.resok.data.data_len = count;
323
324     return &result;
325 }
326
327 write3res *
328 nfsproc3_write_3_svc(write3args *argp, struct svc_req *rqstp)
329 {
330     static write3res result;
331     struct wcc_data wcc;
332     memset(&wcc, 0, sizeof(wcc));
333
334     BlueSkyInode *inode = lookup_fh(&argp->file);
335     if (inode == NULL) {
336         result.status = NFS3ERR_STALE;
337         result.write3res_u.resfail = wcc;
338         return &result;
339     }
340
341     encode_pre_wcc(&wcc, inode);
342     if (inode->type != BLUESKY_REGULAR) {
343         result.status = NFS3ERR_INVAL;
344         result.write3res_u.resfail = wcc;
345         return &result;
346     }
347
348     uint64_t lastbyte = argp->offset + argp->count;
349     if (lastbyte > inode->size) {
350         bluesky_file_truncate(inode, lastbyte);
351     }
352
353     if (argp->data.data_len < argp->count) {
354         /* ??? */
355     } else {
356         bluesky_file_write(inode, argp->offset,
357                            argp->data.data_val, argp->count);
358     }
359
360     wcc.after.present = TRUE;
361     encode_fattr3(&wcc.after.post_op_attr_u.attributes, inode);
362     result.write3res_u.resok.file_wcc = wcc;
363     result.write3res_u.resok.count = argp->count;
364     result.write3res_u.resok.committed = FILE_SYNC;
365
366     return &result;
367 }
368
369 diropres3 *
370 nfsproc3_create_3_svc(create3args *argp, struct svc_req *rqstp)
371 {
372     static diropres3 result;
373     struct wcc_data wcc;
374     memset(&wcc, 0, sizeof(wcc));
375
376     BlueSkyInode *dir = lookup_fh(&argp->where.dir);
377     if (dir == NULL) {
378         result.status = NFS3ERR_STALE;
379         result.diropres3_u.resfail = wcc;
380         return &result;
381     }
382
383     encode_pre_wcc(&wcc, dir);
384     if (dir->type != BLUESKY_DIRECTORY) {
385         result.status = NFS3ERR_NOTDIR;
386         result.diropres3_u.resfail = wcc;
387         return &result;
388     }
389
390     if (!validate_filename(argp->where.name)
391         || strcmp(argp->where.name, ".") == 0
392         || strcmp(argp->where.name, "..") == 0)
393     {
394         result.status = NFS3ERR_EXIST;
395         result.diropres3_u.resfail = wcc;
396         return &result;
397     }
398
399     BlueSkyInode *file;
400     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_REGULAR);
401     file->nlink = 1;
402     file->mode = 0755;
403     int64_t time = bluesky_get_current_time();
404     printf("time: %"PRIi64"\n", time);
405     file->mtime = time;
406     file->ctime = time;
407     file->atime = time;
408     file->ntime = time;
409     bluesky_insert_inode(fs, file);
410     bluesky_directory_insert(dir, argp->where.name, file->inum);
411
412     dir->mtime = dir->ctime = bluesky_get_current_time();
413     dir->change_count++;
414
415     wcc.after.present = TRUE;
416     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
417     result.diropres3_u.resok.obj_attributes.present = TRUE;
418     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
419     result.diropres3_u.resok.dir_wcc = wcc;
420
421     static uint64_t fh_bytes;
422     fh_bytes = GUINT64_TO_BE(file->inum);
423     result.diropres3_u.resok.obj.present = TRUE;
424     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
425     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
426
427     return &result;
428 }
429
430 diropres3 *
431 nfsproc3_mkdir_3_svc(mkdir3args *argp, struct svc_req *rqstp)
432 {
433     static diropres3 result;
434     struct wcc_data wcc;
435     memset(&wcc, 0, sizeof(wcc));
436
437     BlueSkyInode *dir = lookup_fh(&argp->where.dir);
438     if (dir == NULL) {
439         result.status = NFS3ERR_STALE;
440         result.diropres3_u.resfail = wcc;
441         return &result;
442     }
443
444     encode_pre_wcc(&wcc, dir);
445     if (dir->type != BLUESKY_DIRECTORY) {
446         result.status = NFS3ERR_NOTDIR;
447         result.diropres3_u.resfail = wcc;
448         return &result;
449     }
450
451     if (!validate_filename(argp->where.name)
452         || strcmp(argp->where.name, ".") == 0
453         || strcmp(argp->where.name, "..") == 0)
454     {
455         result.status = NFS3ERR_EXIST;
456         result.diropres3_u.resfail = wcc;
457         return &result;
458     }
459
460     BlueSkyInode *file;
461     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_DIRECTORY);
462     file->nlink = 1;
463     file->mode = 0755;
464     int64_t time = bluesky_get_current_time();
465     file->mtime = time;
466     file->ctime = time;
467     file->atime = time;
468     file->ntime = time;
469     bluesky_insert_inode(fs, file);
470     bluesky_directory_insert(dir, argp->where.name, file->inum);
471     set_attributes(file, &argp->attributes);
472
473     dir->mtime = dir->ctime = bluesky_get_current_time();
474     dir->change_count++;
475
476     wcc.after.present = TRUE;
477     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
478     result.diropres3_u.resok.obj_attributes.present = TRUE;
479     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
480     result.diropres3_u.resok.dir_wcc = wcc;
481
482     static uint64_t fh_bytes;
483     fh_bytes = GUINT64_TO_BE(file->inum);
484     result.diropres3_u.resok.obj.present = TRUE;
485     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
486     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
487
488     return &result;
489 }
490
491 diropres3 *
492 nfsproc3_symlink_3_svc(symlink3args *argp, struct svc_req *rqstp)
493 {
494     static diropres3 result;
495     struct wcc_data wcc;
496     memset(&wcc, 0, sizeof(wcc));
497
498     BlueSkyInode *dir = lookup_fh(&argp->where.dir);
499     if (dir == NULL) {
500         result.status = NFS3ERR_STALE;
501         result.diropres3_u.resfail = wcc;
502         return &result;
503     }
504
505     encode_pre_wcc(&wcc, dir);
506     if (dir->type != BLUESKY_DIRECTORY) {
507         result.status = NFS3ERR_NOTDIR;
508         result.diropres3_u.resfail = wcc;
509         return &result;
510     }
511
512     if (!validate_filename(argp->where.name)
513         || strcmp(argp->where.name, ".") == 0
514         || strcmp(argp->where.name, "..") == 0)
515     {
516         result.status = NFS3ERR_EXIST;
517         result.diropres3_u.resfail = wcc;
518         return &result;
519     }
520
521     BlueSkyInode *file;
522     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_SYMLINK);
523     file->nlink = 1;
524     file->mode = 0755;
525     int64_t time = bluesky_get_current_time();
526     file->mtime = time;
527     file->ctime = time;
528     file->atime = time;
529     file->ntime = time;
530     file->symlink_contents = g_strdup(argp->symlink.symlink_data);
531     bluesky_insert_inode(fs, file);
532     bluesky_directory_insert(dir, argp->where.name, file->inum);
533
534     dir->mtime = dir->ctime = bluesky_get_current_time();
535     dir->change_count++;
536
537     wcc.after.present = TRUE;
538     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
539     result.diropres3_u.resok.obj_attributes.present = TRUE;
540     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
541     result.diropres3_u.resok.dir_wcc = wcc;
542
543     static uint64_t fh_bytes;
544     fh_bytes = GUINT64_TO_BE(file->inum);
545     result.diropres3_u.resok.obj.present = TRUE;
546     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
547     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
548
549     return &result;
550 }
551
552 diropres3 *
553 nfsproc3_mknod_3_svc(mknod3args *argp, struct svc_req *rqstp)
554 {
555     static diropres3 result;
556
557     result.status = NFS3ERR_NOTSUPP;
558
559     return &result;
560 }
561
562 wccstat3 *
563 nfsproc3_remove_3_svc(diropargs3 *argp, struct svc_req *rqstp)
564 {
565     static wccstat3 result;
566
567     result.wccstat3_u.wcc.before.present = FALSE;
568     result.wccstat3_u.wcc.after.present = FALSE;
569
570     BlueSkyInode *dir = lookup_fh(&argp->dir);
571     if (dir == NULL) {
572         result.status = NFS3ERR_STALE;
573         return &result;
574     }
575
576     encode_pre_wcc(&result.wccstat3_u.wcc, dir);
577
578     if (!validate_filename(argp->name)
579         || strcmp(argp->name, ".") == 0
580         || strcmp(argp->name, "..") == 0)
581     {
582         result.status = NFS3ERR_NOENT;
583         return &result;
584     }
585
586     /* TODO: Decrement link count, deallocate inode if needed. */
587
588     bluesky_directory_remove(dir, argp->name);
589
590     result.status = NFS3_OK;
591     result.wccstat3_u.wcc.after.present = TRUE;
592     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
593                   dir);
594
595     return &result;
596 }
597
598 wccstat3 *
599 nfsproc3_rmdir_3_svc(diropargs3 *argp, struct svc_req *rqstp)
600 {
601     static wccstat3 result;
602
603     result.wccstat3_u.wcc.before.present = FALSE;
604     result.wccstat3_u.wcc.after.present = FALSE;
605
606     BlueSkyInode *dir = lookup_fh(&argp->dir);
607     if (dir == NULL) {
608         result.status = NFS3ERR_STALE;
609         return &result;
610     }
611
612     encode_pre_wcc(&result.wccstat3_u.wcc, dir);
613
614     if (!validate_filename(argp->name)
615         || strcmp(argp->name, ".") == 0
616         || strcmp(argp->name, "..") == 0)
617     {
618         result.status = NFS3ERR_NOENT;
619         return &result;
620     }
621
622     uint64_t inum = bluesky_directory_lookup(dir, argp->name);
623     BlueSkyInode *inode = bluesky_get_inode(fs, inum);
624     if (inode == NULL) {
625         result.status = NFS3ERR_NOENT;
626         return &result;
627     }
628
629     if (inode->type != BLUESKY_DIRECTORY) {
630         result.status = NFS3ERR_NOTDIR;
631         return &result;
632     }
633     if (g_sequence_get_length(inode->dirents) > 0) {
634         printf("Directory not empty: %d entries\n",
635                g_sequence_get_length(inode->dirents));
636         result.status = NFS3ERR_NOTEMPTY;
637         return &result;
638     }
639
640     /* TODO: Decrement link count, deallocate inode if needed. */
641
642     bluesky_directory_remove(dir, argp->name);
643
644     result.status = NFS3_OK;
645     result.wccstat3_u.wcc.after.present = TRUE;
646     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
647                   dir);
648
649     return &result;
650 }
651
652 rename3res *
653 nfsproc3_rename_3_svc(rename3args *argp, struct svc_req *rqstp)
654 {
655     static rename3res result;
656     wcc_data *wcc1 = &result.rename3res_u.res.fromdir_wcc;
657     wcc_data *wcc2 = &result.rename3res_u.res.todir_wcc;
658     memset(wcc1, 0, sizeof(*wcc1));
659     memset(wcc2, 0, sizeof(*wcc2));
660
661     BlueSkyInode *dir1 = lookup_fh(&argp->from.dir);
662     if (dir1 == NULL) {
663         result.status = NFS3ERR_STALE;
664         return &result;
665     }
666     encode_pre_wcc(wcc1, dir1);
667
668     BlueSkyInode *dir2 = lookup_fh(&argp->to.dir);
669     if (dir2 == NULL) {
670         result.status = NFS3ERR_STALE;
671         return &result;
672     }
673     encode_pre_wcc(wcc2, dir1);
674
675     gboolean status = bluesky_rename(dir1, argp->from.name,
676                                      dir2, argp->to.name,
677                                      TRUE, TRUE);
678
679     wcc1->after.present = TRUE;
680     encode_fattr3(&wcc1->after.post_op_attr_u.attributes, dir1);
681     wcc2->after.present = TRUE;
682     encode_fattr3(&wcc2->after.post_op_attr_u.attributes, dir2);
683     if (status)
684         result.status = NFS3_OK;
685     else
686         result.status = NFS3ERR_PERM;
687
688     return &result;
689 }
690
691 link3res *
692 nfsproc3_link_3_svc(link3args *argp, struct svc_req *rqstp)
693 {
694     static link3res result;
695     struct wcc_data wcc;
696     memset(&wcc, 0, sizeof(wcc));
697
698     BlueSkyInode *inode = lookup_fh(&argp->file);
699     if (inode == NULL) {
700         result.status = NFS3ERR_STALE;
701         result.link3res_u.res.linkdir_wcc = wcc;
702         return &result;
703     }
704
705     BlueSkyInode *dir = lookup_fh(&argp->link.dir);
706     if (dir == NULL) {
707         result.status = NFS3ERR_STALE;
708         result.link3res_u.res.linkdir_wcc = wcc;
709         return &result;
710     }
711
712     encode_pre_wcc(&wcc, dir);
713     if (dir->type != BLUESKY_DIRECTORY) {
714         result.status = NFS3ERR_NOTDIR;
715         result.link3res_u.res.linkdir_wcc = wcc;
716         return &result;
717     }
718
719     if (!validate_filename(argp->link.name)
720         || strcmp(argp->link.name, ".") == 0
721         || strcmp(argp->link.name, "..") == 0
722         || bluesky_directory_lookup(dir, argp->link.name) != 0)
723     {
724         result.status = NFS3ERR_EXIST;
725         result.link3res_u.res.linkdir_wcc = wcc;
726         return &result;
727     }
728
729     if (!bluesky_directory_insert(dir, argp->link.name, inode->inum)) {
730         result.status = NFS3ERR_EXIST;
731         result.link3res_u.res.linkdir_wcc = wcc;
732         return &result;
733     }
734     inode->nlink++;
735     bluesky_inode_update_ctime(inode, 0);
736
737     result.status = NFS3_OK;
738     wcc.after.present = TRUE;
739     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
740     result.link3res_u.res.file_attributes.present = TRUE;
741     encode_fattr3(&result.link3res_u.res.file_attributes.post_op_attr_u.attributes, inode);
742     result.link3res_u.res.linkdir_wcc = wcc;
743
744     return &result;
745 }
746
747 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
748                             gpointer unused);
749
750 #define MAX_READDIR_DIRENTS 64
751 readdir3res *
752 nfsproc3_readdir_3_svc(readdir3args *argp, struct svc_req *rqstp)
753 {
754     static readdir3res result;
755
756     BlueSkyInode *dir = lookup_fh(&argp->dir);
757     if (dir == NULL) {
758         result.status = NFS3ERR_STALE;
759         result.readdir3res_u.resfail.present = FALSE;
760         return &result;
761     }
762
763     result.status = NFS3_OK;
764     result.readdir3res_u.resok.dir_attributes.present = TRUE;
765     encode_fattr3(&result.readdir3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
766     memset(result.readdir3res_u.resok.cookieverf, 0,
767            sizeof(result.readdir3res_u.resok.cookieverf));
768
769     static entry3 dirents[MAX_READDIR_DIRENTS];
770     int count = 0;
771
772     BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
773     GSequenceIter *i = g_sequence_search(dir->dirents, &start,
774                                          bluesky_dirent_compare, NULL);
775
776     while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
777         BlueSkyDirent *d = g_sequence_get(i);
778         dirents[count].fileid = d->inum;
779         dirents[count].name = d->name;
780         dirents[count].cookie = d->cookie;
781         dirents[count].nextentry = NULL;
782         if (count > 0)
783             dirents[count - 1].nextentry = &dirents[count];
784         i = g_sequence_iter_next(i);
785         count++;
786     }
787
788     if (count > 0)
789         result.readdir3res_u.resok.reply.entries = &dirents[0];
790     else
791         result.readdir3res_u.resok.reply.entries = NULL;
792     result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
793
794     return &result;
795 }
796
797 readdirplus3res *
798 nfsproc3_readdirplus_3_svc(readdirplus3args *argp, struct svc_req *rqstp)
799 {
800     /* XDR-encoded sizes:
801      *   post_op_attr: 88 bytes
802      *   base readdirplus3resok: 88 + 16 bytes
803      *   base directory entry: 24 bytes + filename
804      *   attributes/fh3: 88 + 8 + filehandle size
805      */
806     size_t dircount = 88 + 16, attrcount = 0;
807     static readdirplus3res result;
808
809     BlueSkyInode *dir = lookup_fh(&argp->dir);
810     if (dir == NULL) {
811         result.status = NFS3ERR_STALE;
812         result.readdirplus3res_u.resfail.present = FALSE;
813         return &result;
814     }
815
816     result.status = NFS3_OK;
817     result.readdirplus3res_u.resok.dir_attributes.present = TRUE;
818     encode_fattr3(&result.readdirplus3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
819     memset(result.readdirplus3res_u.resok.cookieverf, 0,
820            sizeof(result.readdirplus3res_u.resok.cookieverf));
821
822     static entryplus3 dirents[MAX_READDIR_DIRENTS];
823     static uint64_t fh_bytes[MAX_READDIR_DIRENTS];
824     int count = 0;
825
826     /* TODO: Handle dircount, maxcount arguments from client. */
827
828     BlueSkyDirent start = {NULL, NULL, argp->cookie, 0};
829     GSequenceIter *i = g_sequence_search(dir->dirents, &start,
830                                          bluesky_dirent_compare, NULL);
831
832     while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
833         BlueSkyDirent *d = g_sequence_get(i);
834         BlueSkyInode *inode = bluesky_get_inode(fs, d->inum);
835         if (inode != NULL) {
836             dircount += 24 + ((strlen(d->name) + 3) & ~3);
837             attrcount += 88 + 8 + 8;
838             if (dircount > argp->dircount
839                 || dircount + attrcount > argp->maxcount)
840                 break;
841             dirents[count].fileid = d->inum;
842             dirents[count].name = d->name;
843             dirents[count].cookie = d->cookie;
844             dirents[count].nextentry = NULL;
845             dirents[count].name_attributes.present = TRUE;
846             encode_fattr3(&dirents[count].name_attributes.post_op_attr_u.attributes, inode);
847             fh_bytes[count] = GUINT64_TO_BE(d->inum);
848             dirents[count].name_handle.present = TRUE;
849             dirents[count].name_handle.post_op_fh3_u.handle.data.data_len = 8;
850             dirents[count].name_handle.post_op_fh3_u.handle.data.data_val
851                 = (char *)&fh_bytes[count];
852             if (count > 0)
853                 dirents[count - 1].nextentry = &dirents[count];
854             count++;
855         }
856         i = g_sequence_iter_next(i);
857     }
858
859     if (count > 0)
860         result.readdirplus3res_u.resok.reply.entries = &dirents[0];
861     else
862         result.readdirplus3res_u.resok.reply.entries = NULL;
863     result.readdirplus3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
864
865     return &result;
866 }
867
868 fsstat3res *
869 nfsproc3_fsstat_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
870 {
871     static fsstat3res result;
872
873     result.status = NFS3ERR_NOTSUPP;
874
875     return &result;
876 }
877
878 fsinfo3res *
879 nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
880 {
881     static fsinfo3res result;
882
883     BlueSkyInode *inode = bluesky_get_inode(fs, 1);
884     result.status = NFS3_OK;
885     result.fsinfo3res_u.resok.obj_attributes.present = TRUE;
886     encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
887     result.fsinfo3res_u.resok.rtmax = 32768;
888     result.fsinfo3res_u.resok.rtpref = 32768;
889     result.fsinfo3res_u.resok.rtmult = 4096;
890     result.fsinfo3res_u.resok.wtmax = 32768;
891     result.fsinfo3res_u.resok.wtpref = 32768;
892     result.fsinfo3res_u.resok.wtmult = 4096;
893     result.fsinfo3res_u.resok.dtpref = 4096;
894     result.fsinfo3res_u.resok.maxfilesize = 0x7fffffffffffffffULL;
895     result.fsinfo3res_u.resok.time_delta.seconds = 0;
896     result.fsinfo3res_u.resok.time_delta.nseconds = 1000;
897     result.fsinfo3res_u.resok.properties
898         = FSF3_LINK | FSF3_SYMLINK | FSF3_HOMOGENEOUS | FSF3_CANSETTIME;
899
900     return &result;
901 }
902
903 pathconf3res *
904 nfsproc3_pathconf_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
905 {
906     static pathconf3res result;
907
908     BlueSkyInode *inode = bluesky_get_inode(fs, 1);
909     result.status = NFS3_OK;
910     result.pathconf3res_u.resok.obj_attributes.present = TRUE;
911     encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
912     result.pathconf3res_u.resok.linkmax = 0xffffffff;
913     result.pathconf3res_u.resok.name_max = 255;
914     result.pathconf3res_u.resok.no_trunc = TRUE;
915     result.pathconf3res_u.resok.chown_restricted = TRUE;
916     result.pathconf3res_u.resok.case_insensitive = FALSE;
917     result.pathconf3res_u.resok.case_preserving = TRUE;
918
919     return &result;
920 }
921
922 commit3res *
923 nfsproc3_commit_3_svc(commit3args *argp, struct svc_req *rqstp)
924 {
925     static commit3res result;
926
927     result.status = NFS3ERR_NOTSUPP;
928
929     return &result;
930 }