NFSv3 mkdir.
[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 te 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
124 void encode_pre_wcc(struct wcc_data *wcc, BlueSkyInode *inode)
125 {
126     wcc->before.present = TRUE;
127     wcc->before.pre_op_attr_u.attributes.size = inode->size;
128     wcc->before.pre_op_attr_u.attributes.mtime.seconds = inode->mtime / 1000000;
129     wcc->before.pre_op_attr_u.attributes.mtime.nseconds = (inode->mtime % 1000000) * 1000;
130     wcc->before.pre_op_attr_u.attributes.ctime.seconds = inode->ctime / 1000000;
131     wcc->before.pre_op_attr_u.attributes.ctime.nseconds = (inode->ctime % 1000000) * 1000;
132 }
133 void *
134 nfsproc3_null_3_svc(void *argp, struct svc_req *rqstp)
135 {
136     return null_result;
137 }
138
139 getattr3res *
140 nfsproc3_getattr_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
141 {
142     static getattr3res result;
143
144     BlueSkyInode *inode = lookup_fh(argp);
145     if (inode != NULL) {
146         result.status = NFS3_OK;
147         encode_fattr3(&result.getattr3res_u.attributes, inode);
148     } else {
149         result.status = NFS3ERR_STALE;
150     }
151
152     return &result;
153 }
154
155 wccstat3 *
156 nfsproc3_setattr_3_svc(setattr3args *argp, struct svc_req *rqstp)
157 {
158     static wccstat3 result;
159
160     result.wccstat3_u.wcc.before.present = FALSE;
161     result.wccstat3_u.wcc.after.present = FALSE;
162     BlueSkyInode *inode = lookup_fh(&argp->object);
163     if (inode == NULL) {
164         result.status = NFS3ERR_STALE;
165         return &result;
166     }
167
168     encode_pre_wcc(&result.wccstat3_u.wcc, inode);
169     if (argp->guard.check) {
170         if (inode->ctime != decode_nfstime3(&argp->guard.sattrguard3_u.ctime)) {
171             result.status = NFS3ERR_NOT_SYNC;
172             result.wccstat3_u.wcc.after.present = TRUE;
173             encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes, inode);
174             return &result;
175         }
176     }
177
178     set_attributes(inode, &argp->new_attributes);
179
180     result.wccstat3_u.wcc.after.present = TRUE;
181     encode_fattr3(&result.wccstat3_u.wcc.after.post_op_attr_u.attributes,
182                   inode);
183     result.status = NFS3_OK;
184
185     return &result;
186 }
187
188 lookup3res *
189 nfsproc3_lookup_3_svc(diropargs3 *argp, struct svc_req *rqstp)
190 {
191     static lookup3res result;
192
193     BlueSkyInode *dir = lookup_fh(&argp->dir);
194     if (dir == NULL) {
195         result.status = NFS3ERR_STALE;
196         result.lookup3res_u.resfail.present = FALSE;
197         return &result;
198     }
199
200     result.lookup3res_u.resfail.present = TRUE;
201     encode_fattr3(&result.lookup3res_u.resfail.post_op_attr_u.attributes, dir);
202     if (!validate_filename(argp->name)) {
203         if (strlen(argp->name) > 255)
204             result.status = NFS3ERR_NAMETOOLONG;
205         else
206             result.status = NFS3ERR_NOENT;
207         return &result;
208     }
209
210     /* TODO: Special-case "." and "..". */
211     uint64_t inum = bluesky_directory_lookup(dir, argp->name);
212     if (inum == 0) {
213         result.status = NFS3ERR_NOENT;
214         return &result;
215     }
216     BlueSkyInode *inode = bluesky_get_inode(fs, inum);
217     if (inode == NULL) {
218         result.status = NFS3ERR_NOENT;
219         return &result;
220     }
221
222     result.status = NFS3_OK;
223     result.lookup3res_u.resok.dir_attributes.present = TRUE;
224     encode_fattr3(&result.lookup3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
225     result.lookup3res_u.resok.obj_attributes.present = TRUE;
226     encode_fattr3(&result.lookup3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
227
228     static uint64_t fh_bytes;
229     fh_bytes = GUINT64_TO_BE(inum);
230     result.lookup3res_u.resok.object.data.data_len = 8;
231     result.lookup3res_u.resok.object.data.data_val = (char *)&fh_bytes;
232
233     return &result;
234 }
235
236 access3res *
237 nfsproc3_access_3_svc(access3args *argp, struct svc_req *rqstp)
238 {
239     static access3res result;
240
241     BlueSkyInode *inode = lookup_fh(&argp->object);
242     if (inode == NULL) {
243         result.status = NFS3ERR_STALE;
244         result.access3res_u.resfail.present = FALSE;
245         return &result;
246     }
247
248     result.status = NFS3_OK;
249     result.access3res_u.resok.obj_attributes.present = TRUE;
250     encode_fattr3(&result.access3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
251     result.access3res_u.resok.access = argp->access;
252
253     return &result;
254 }
255
256 readlink3res *
257 nfsproc3_readlink_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
258 {
259     static readlink3res result;
260
261     result.status = NFS3ERR_NOTSUPP;
262
263     return &result;
264 }
265
266 read3res *
267 nfsproc3_read_3_svc(read3args *argp, struct svc_req *rqstp)
268 {
269     static read3res result;
270     static char buf[32768];
271
272     BlueSkyInode *inode = lookup_fh(&argp->file);
273     if (inode == NULL) {
274         result.status = NFS3ERR_STALE;
275         result.read3res_u.resfail.present = FALSE;
276         return &result;
277     }
278
279     int count = argp->count;
280     if (argp->offset >= inode->size) {
281         count = 0;
282         result.read3res_u.resok.eof = TRUE;
283     } else {
284         count = MIN(count, inode->size - argp->offset);
285         if (argp->offset + count == inode->size)
286             result.read3res_u.resok.eof = TRUE;
287         else
288             result.read3res_u.resok.eof = FALSE;
289
290         bluesky_file_read(inode, argp->offset, buf, count);
291     }
292
293     result.status = NFS3_OK;
294     result.read3res_u.resok.file_attributes.present = TRUE;
295     encode_fattr3(&result.read3res_u.resok.file_attributes.post_op_attr_u.attributes, inode);
296     result.read3res_u.resok.count = count;
297     result.read3res_u.resok.data.data_val = buf;
298     result.read3res_u.resok.data.data_len = count;
299
300     return &result;
301 }
302
303 write3res *
304 nfsproc3_write_3_svc(write3args *argp, struct svc_req *rqstp)
305 {
306     static write3res result;
307     struct wcc_data wcc;
308     memset(&wcc, 0, sizeof(wcc));
309
310     BlueSkyInode *inode = lookup_fh(&argp->file);
311     if (inode == NULL) {
312         result.status = NFS3ERR_STALE;
313         result.write3res_u.resfail = wcc;
314         return &result;
315     }
316
317     encode_pre_wcc(&wcc, inode);
318     if (inode->type != BLUESKY_REGULAR) {
319         result.status = NFS3ERR_INVAL;
320         result.write3res_u.resfail = wcc;
321         return &result;
322     }
323
324     uint64_t lastbyte = argp->offset + argp->count;
325     if (lastbyte > inode->size) {
326         bluesky_file_truncate(inode, lastbyte);
327     }
328
329     if (argp->data.data_len < argp->count) {
330         /* ??? */
331     } else {
332         bluesky_file_write(inode, argp->offset,
333                            argp->data.data_val, argp->count);
334     }
335
336     encode_fattr3(&wcc.after.post_op_attr_u.attributes, inode);
337     result.write3res_u.resok.file_wcc = wcc;
338     result.write3res_u.resok.count = argp->count;
339     result.write3res_u.resok.committed = FILE_SYNC;
340
341     return &result;
342 }
343
344 diropres3 *
345 nfsproc3_create_3_svc(create3args *argp, struct svc_req *rqstp)
346 {
347     static diropres3 result;
348     struct wcc_data wcc;
349     memset(&wcc, 0, sizeof(wcc));
350
351     BlueSkyInode *dir = lookup_fh(&argp->where.dir);
352     if (dir == NULL) {
353         result.status = NFS3ERR_STALE;
354         result.diropres3_u.resfail = wcc;
355         return &result;
356     }
357
358     encode_pre_wcc(&wcc, dir);
359     if (dir->type != BLUESKY_DIRECTORY) {
360         result.status = NFS3ERR_NOTDIR;
361         result.diropres3_u.resfail = wcc;
362         return &result;
363     }
364
365     if (!validate_filename(argp->where.name)
366         || strcmp(argp->where.name, ".") == 0
367         || strcmp(argp->where.name, "..") == 0)
368     {
369         result.status = NFS3ERR_EXIST;
370         result.diropres3_u.resfail = wcc;
371         return &result;
372     }
373
374     BlueSkyInode *file;
375     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_REGULAR);
376     file->nlink = 1;
377     file->mode = 0755;
378     int64_t time = bluesky_get_current_time();
379     printf("time: %lld\n", time);
380     file->mtime = time;
381     file->ctime = time;
382     file->atime = time;
383     file->ntime = time;
384     bluesky_insert_inode(fs, file);
385     bluesky_directory_insert(dir, argp->where.name, file->inum);
386
387     dir->mtime = dir->ctime = bluesky_get_current_time();
388     dir->change_count++;
389
390     wcc.after.present = TRUE;
391     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
392     result.diropres3_u.resok.obj_attributes.present = TRUE;
393     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
394     result.diropres3_u.resok.dir_wcc = wcc;
395
396     static uint64_t fh_bytes;
397     fh_bytes = GUINT64_TO_BE(file->inum);
398     result.diropres3_u.resok.obj.present = TRUE;
399     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
400     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
401
402     return &result;
403 }
404
405 diropres3 *
406 nfsproc3_mkdir_3_svc(mkdir3args *argp, struct svc_req *rqstp)
407 {
408     static diropres3 result;
409     struct wcc_data wcc;
410     memset(&wcc, 0, sizeof(wcc));
411
412     BlueSkyInode *dir = lookup_fh(&argp->where.dir);
413     if (dir == NULL) {
414         result.status = NFS3ERR_STALE;
415         result.diropres3_u.resfail = wcc;
416         return &result;
417     }
418
419     encode_pre_wcc(&wcc, dir);
420     if (dir->type != BLUESKY_DIRECTORY) {
421         result.status = NFS3ERR_NOTDIR;
422         result.diropres3_u.resfail = wcc;
423         return &result;
424     }
425
426     if (!validate_filename(argp->where.name)
427         || strcmp(argp->where.name, ".") == 0
428         || strcmp(argp->where.name, "..") == 0)
429     {
430         result.status = NFS3ERR_EXIST;
431         result.diropres3_u.resfail = wcc;
432         return &result;
433     }
434
435     BlueSkyInode *file;
436     file = bluesky_new_inode(bluesky_fs_alloc_inode(fs), fs, BLUESKY_DIRECTORY);
437     file->nlink = 1;
438     file->mode = 0755;
439     int64_t time = bluesky_get_current_time();
440     file->mtime = time;
441     file->ctime = time;
442     file->atime = time;
443     file->ntime = time;
444     bluesky_insert_inode(fs, file);
445     bluesky_directory_insert(dir, argp->where.name, file->inum);
446     set_attributes(file, &argp->attributes);
447
448     dir->mtime = dir->ctime = bluesky_get_current_time();
449     dir->change_count++;
450
451     wcc.after.present = TRUE;
452     encode_fattr3(&wcc.after.post_op_attr_u.attributes, dir);
453     result.diropres3_u.resok.obj_attributes.present = TRUE;
454     encode_fattr3(&result.diropres3_u.resok.obj_attributes.post_op_attr_u.attributes, file);
455     result.diropres3_u.resok.dir_wcc = wcc;
456
457     static uint64_t fh_bytes;
458     fh_bytes = GUINT64_TO_BE(file->inum);
459     result.diropres3_u.resok.obj.present = TRUE;
460     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_len = 8;
461     result.diropres3_u.resok.obj.post_op_fh3_u.handle.data.data_val = (char *)&fh_bytes;
462
463     return &result;
464 }
465
466 diropres3 *
467 nfsproc3_symlink_3_svc(symlink3args *argp, struct svc_req *rqstp)
468 {
469     static diropres3 result;
470
471     result.status = NFS3ERR_NOTSUPP;
472
473     return &result;
474 }
475
476 diropres3 *
477 nfsproc3_mknod_3_svc(mknod3args *argp, struct svc_req *rqstp)
478 {
479     static diropres3 result;
480
481     result.status = NFS3ERR_NOTSUPP;
482
483     return &result;
484 }
485
486 wccstat3 *
487 nfsproc3_remove_3_svc(diropargs3 *argp, struct svc_req *rqstp)
488 {
489     static wccstat3 result;
490
491     result.status = NFS3ERR_NOTSUPP;
492
493     return &result;
494 }
495
496 wccstat3 *
497 nfsproc3_rmdir_3_svc(diropargs3 *argp, struct svc_req *rqstp)
498 {
499     static wccstat3 result;
500
501     result.status = NFS3ERR_NOTSUPP;
502
503     return &result;
504 }
505
506 rename3res *
507 nfsproc3_rename_3_svc(rename3args *argp, struct svc_req *rqstp)
508 {
509     static rename3res result;
510
511     result.status = NFS3ERR_NOTSUPP;
512
513     return &result;
514 }
515
516 link3res *
517 nfsproc3_link_3_svc(link3args *argp, struct svc_req *rqstp)
518 {
519     static link3res result;
520
521     result.status = NFS3ERR_NOTSUPP;
522
523     return &result;
524 }
525
526 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
527                             gpointer unused);
528
529 readdir3res *
530 nfsproc3_readdir_3_svc(readdir3args *argp, struct svc_req *rqstp)
531 {
532     static readdir3res result;
533
534     BlueSkyInode *dir = lookup_fh(&argp->dir);
535     if (dir == NULL) {
536         result.status = NFS3ERR_STALE;
537         result.readdir3res_u.resfail.present = FALSE;
538         return &result;
539     }
540
541     result.status = NFS3_OK;
542     result.readdir3res_u.resok.dir_attributes.present = TRUE;
543     encode_fattr3(&result.readdir3res_u.resok.dir_attributes.post_op_attr_u.attributes, dir);
544     memset(result.readdir3res_u.resok.cookieverf, 0,
545            sizeof(result.readdir3res_u.resok.cookieverf));
546
547 #define MAX_READDIR_DIRENTS 4
548     static entry3 dirents[MAX_READDIR_DIRENTS];
549     int count = 0;
550
551     BlueSkyDirent start = {NULL, argp->cookie, 0};
552     GSequenceIter *i = g_sequence_search(dir->dirents, &start,
553                                          bluesky_dirent_compare, NULL);
554
555     while (count < MAX_READDIR_DIRENTS && !g_sequence_iter_is_end(i)) {
556         BlueSkyDirent *d = g_sequence_get(i);
557         dirents[count].fileid = d->inum;
558         dirents[count].name = d->name;
559         dirents[count].cookie = d->cookie;
560         dirents[count].nextentry = NULL;
561         if (count > 0)
562             dirents[count - 1].nextentry = &dirents[count];
563         i = g_sequence_iter_next(i);
564         count++;
565     }
566
567     if (count > 0)
568         result.readdir3res_u.resok.reply.entries = &dirents[0];
569     else
570         result.readdir3res_u.resok.reply.entries = NULL;
571     result.readdir3res_u.resok.reply.eof = g_sequence_iter_is_end(i);
572
573     return &result;
574 }
575
576 readdirplus3res *
577 nfsproc3_readdirplus_3_svc(readdirplus3args *argp, struct svc_req *rqstp)
578 {
579     static readdirplus3res result;
580
581     result.status = NFS3ERR_NOTSUPP;
582
583     return &result;
584 }
585
586 fsstat3res *
587 nfsproc3_fsstat_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
588 {
589     static fsstat3res result;
590
591     result.status = NFS3ERR_NOTSUPP;
592
593     return &result;
594 }
595
596 fsinfo3res *
597 nfsproc3_fsinfo_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
598 {
599     static fsinfo3res result;
600
601     BlueSkyInode *inode = bluesky_get_inode(fs, 1);
602     result.status = NFS3_OK;
603     result.fsinfo3res_u.resok.obj_attributes.present = TRUE;
604     encode_fattr3(&result.fsinfo3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
605     result.fsinfo3res_u.resok.rtmax = 32768;
606     result.fsinfo3res_u.resok.rtpref = 32768;
607     result.fsinfo3res_u.resok.rtmult = 4096;
608     result.fsinfo3res_u.resok.wtmax = 32768;
609     result.fsinfo3res_u.resok.wtpref = 32768;
610     result.fsinfo3res_u.resok.wtmult = 4096;
611     result.fsinfo3res_u.resok.dtpref = 4096;
612     result.fsinfo3res_u.resok.maxfilesize = 0x7fffffffffffffffULL;
613     result.fsinfo3res_u.resok.time_delta.seconds = 0;
614     result.fsinfo3res_u.resok.time_delta.nseconds = 1000;
615     result.fsinfo3res_u.resok.properties
616         = FSF3_LINK | FSF3_SYMLINK | FSF3_HOMOGENEOUS | FSF3_CANSETTIME;
617
618     return &result;
619 }
620
621 pathconf3res *
622 nfsproc3_pathconf_3_svc(nfs_fh3 *argp, struct svc_req *rqstp)
623 {
624     static pathconf3res result;
625
626     BlueSkyInode *inode = bluesky_get_inode(fs, 1);
627     result.status = NFS3_OK;
628     result.pathconf3res_u.resok.obj_attributes.present = TRUE;
629     encode_fattr3(&result.pathconf3res_u.resok.obj_attributes.post_op_attr_u.attributes, inode);
630     result.pathconf3res_u.resok.linkmax = 0xffffffff;
631     result.pathconf3res_u.resok.name_max = 255;
632     result.pathconf3res_u.resok.no_trunc = TRUE;
633     result.pathconf3res_u.resok.chown_restricted = TRUE;
634     result.pathconf3res_u.resok.case_insensitive = FALSE;
635     result.pathconf3res_u.resok.case_preserving = TRUE;
636
637     return &result;
638 }
639
640 commit3res *
641 nfsproc3_commit_3_svc(commit3args *argp, struct svc_req *rqstp)
642 {
643     static commit3res result;
644
645     result.status = NFS3ERR_NOTSUPP;
646
647     return &result;
648 }