-
Notifications
You must be signed in to change notification settings - Fork 5
/
rootfs.c
1103 lines (916 loc) · 22.2 KB
/
rootfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file contains a simple memory based file system that is used
as the top-level name space by the vnode layer. It is a complete
file system in and of itself but it only supports creating directories
and symlinks. It is also entirely memory based so it is re-created
each time the vnode layer is initialized. It is only used to mount
other file systems (i.e. to create a directory that can be used as
a mount point for another file system).
THIS CODE COPYRIGHT DOMINIC GIAMPAOLO. NO WARRANTY IS EXPRESSED
OR IMPLIED. YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO ([email protected]).
Dominic Giampaolo
*/
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include "compat.h"
#include "skiplist.h"
#include "lock.h"
#include "fsproto.h"
typedef struct vnode vnode;
typedef struct nspace nspace;
typedef struct dirpos dirpos;
struct nspace {
nspace_id nsid;
long vnnum;
vnode * root;
vnode_id nxvnid;
lock lock;
SkipList skiplist;
};
struct vnode {
char * name;
char removed;
nspace *ns;
vnode_id vnid;
vnode *parent;
time_t crtime;
time_t mtime;
uid_t uid;
gid_t gid;
mode_t mode;
vnode * next;
vnode * prev;
vnode * head; /* for directories */
char * symlink; /* for symbolic links */
};
struct dirpos {
lock lock;
int pos;
char name[FILE_NAME_LENGTH];
};
static int rootfs_read_vnode(void *ns, vnode_id vnid, char r,
void **node);
static int rootfs_write_vnode(void *ns, void *node, char r);
static int rootfs_remove_vnode(void *ns, void *node, char r);
static int rootfs_walk(void *ns, void *base, const char *file,
char **newpath, vnode_id *vnid);
static int rootfs_access(void *ns, void *node, int mode);
static int rootfs_symlink(void *ns, void *dir, const char *name,
const char *path);
static int rootfs_mkdir(void *ns, void *dir, const char *name,
int perms);
static int rootfs_rename(void *ns, void *olddir, const char *oldname,
void *newdir, const char *newname);
static int rootfs_unlink(void *ns, void *dir, const char *name);
static int rootfs_rmdir(void *ns, void *dir, const char *name);
static int rootfs_readlink(void *ns, void *node, char *buf,
size_t *bufsize);
static int rootfs_opendir(void *ns, void *node, void **cookie);
static int rootfs_closedir(void *ns, void *node, void *cookie);
static int rootfs_free_dircookie(void *ns, void *node, void *cookie);
static int rootfs_rewinddir(void *ns, void *node, void *cookie);
static int rootfs_readdir(void *ns, void *node, void *cookie,
long *num, struct my_dirent *buf, size_t bufsize);
static int rootfs_rstat(void *ns, void *node, struct my_stat *st);
static int rootfs_wstat(void *ns, void *node, struct my_stat *st, long mask);
static int rootfs_mount(nspace_id nsid, const char *device, ulong flags,
void *parms, size_t len, void **data, vnode_id *vnid);
static int rootfs_unmount(void *ns);
static int compare_vnode(vnode *vna, vnode *vnb);
static int do_create(nspace *ns, vnode *dir, const char *name,
mode_t mode, vnode **vnp);
static int do_unlink(nspace *ns, vnode *dir, const char *name, bool isdir);
vnode_ops rootfs = {
&rootfs_read_vnode,
&rootfs_write_vnode,
&rootfs_remove_vnode,
&rootfs_walk,
&rootfs_access,
NULL,
&rootfs_mkdir,
&rootfs_symlink,
NULL,
&rootfs_rename,
&rootfs_unlink,
&rootfs_rmdir,
&rootfs_readlink,
&rootfs_opendir,
&rootfs_closedir,
&rootfs_free_dircookie,
&rootfs_rewinddir,
&rootfs_readdir,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&rootfs_rstat,
&rootfs_wstat,
NULL,
&rootfs_mount,
&rootfs_unmount,
NULL
};
#define LCK_MULTIPLE 1
#define LCK_EXCLUSIVE 1000
#define OMODE_MASK (O_RDONLY | O_WRONLY | O_RDWR)
/* ----------------------------------------------------------------- */
static int
rootfs_walk(void *_ns, void *_base, const char *file, char **newpath,
vnode_id *vnid)
{
nspace *ns;
vnode *base;
int err;
vnode *vn;
char *np;
ns = (nspace *) _ns;
base = (vnode *) _base;
LOCK(ns->lock);
/*
make sure base is a directory and that it has not been removed.
*/
if (!MY_S_ISDIR(base->mode)) {
err = ENOTDIR;
goto exit;
}
if (base->removed) {
err = ENOENT;
goto exit;
}
/*
lookup the special directory '.'
*/
if (!strcmp(file, ".")) {
err = get_vnode(ns->nsid, base->vnid, (void *)&vn);
if (!err)
*vnid = base->vnid;
goto exit;
}
/*
lookup the special directory '..'
*/
if (!strcmp(file, "..")) {
err = get_vnode(ns->nsid, base->parent->vnid, (void *)&vn);
if (!err)
*vnid = vn->vnid;
goto exit;
}
/*
lookup the name in the directory
*/
vn = base->head;
while (vn) {
if (!strcmp(vn->name, file))
break;
vn = vn->next;
}
/*
the name has not been found. advance the path pointer, update the vnid
and report an error.
*/
if (!vn) {
err = ENOENT;
goto exit;
}
/*
we have found the item.
*/
/*
it is a symbolic link that the kernel wants us to eat.
*/
if (MY_S_ISLNK(vn->mode) && newpath) {
err = new_path(vn->symlink, &np);
if (err)
goto exit;
*newpath = np;
} else {
/*
it is a directory or it is a symbolic link that the kernel does
not want to 'eat'.
*/
err = get_vnode(ns->nsid, vn->vnid, (void *)&vn);
if (!err)
*vnid = vn->vnid;
}
exit:
UNLOCK(ns->lock);
return err;
}
static int
rootfs_mkdir(void *_ns, void *_dir, const char *name, int perms)
{
nspace *ns;
vnode *dir;
int err;
vnode *vn;
ns = (nspace *) _ns;
dir = (vnode *) _dir;
LOCK(ns->lock);
err = do_create(ns, dir, name, (perms & ~MY_S_IFMT) | MY_S_IFDIR, &vn);
UNLOCK(ns->lock);
return err;
}
static int
rootfs_symlink(void *_ns, void *_dir, const char *name, const char *path)
{
nspace *ns;
vnode *dir;
int err;
char *buf;
vnode *vn;
ns = (nspace *) _ns;
dir = (vnode *) _dir;
buf = (char *) malloc(strlen(path)+1);
if (!buf) {
err = ENOMEM;
goto error1;
}
strcpy(buf, path);
LOCK(ns->lock);
err = do_create(ns, dir, name, MY_S_IFLNK, &vn);
if (err)
goto error2;
vn->symlink = buf;
UNLOCK(ns->lock);
return 0;
error2:
UNLOCK(ns->lock);
error1:
return err;
}
static int
rootfs_rename(void *_ns, void *_olddir, const char *oldname, void *_newdir,
const char *newname)
{
nspace *ns;
vnode *olddir, *newdir;
int err;
vnode *vn, *nvn, *pvn, *avn;
char *p;
ns = (nspace *) _ns;
olddir = (vnode *) _olddir;
newdir = (vnode *) _newdir;
LOCK(ns->lock);
if (!MY_S_ISDIR(olddir->mode) || !MY_S_ISDIR(newdir->mode)) {
err = ENOTDIR;
goto error1;
}
/*
find (olddir, oldname)
*/
if (!strcmp(oldname, ".") || !strcmp(oldname, "..")) {
err = EPERM;
goto error1;
}
vn = olddir->head;
while (vn) {
if (!strcmp(vn->name, oldname))
break;
vn = vn->next;
}
if (!vn) {
err = ENOENT;
goto error1;
}
/*
look for (newdir, newname)
*/
if (!strcmp(newname, ".") || !strcmp(newname, "..")) {
err = EPERM;
goto error1;
}
nvn = newdir->head;
while (nvn) {
if (!strcmp(nvn->name, newname))
break;
nvn = nvn->next;
}
/*
don't do anything if old and new are the same
*/
if (vn == nvn)
goto exit;
/*
make sure new is not a subdirectory of old
*/
avn = newdir;
while (avn != ns->root) {
avn = avn->parent;
if (avn == olddir) {
err = EINVAL;
goto error1;
}
}
if (strlen(newname) > strlen(vn->name)) {
p = (char *) realloc(vn->name, strlen(newname)+1);
if (!p) {
err = ENOMEM;
goto error1;
}
} else
p = vn->name;
/*
if (newdir, newname) exists, remove it from the name space
*/
if (nvn) {
/*
make sure it is not the root and it is not empty
*/
if (nvn == nvn->ns->root) {
err = EBUSY;
goto error1;
}
if (MY_S_ISDIR(nvn->mode) && nvn->head) {
err = ENOTEMPTY;
goto error1;
}
err = get_vnode(ns->nsid, nvn->vnid, (void *)&nvn);
if (err)
goto error1;
err = remove_vnode(ns->nsid, nvn->vnid);
if (err)
goto error1;
if (nvn->prev)
nvn->prev->next = nvn->next;
else
nvn->parent->head = nvn->next;
if (nvn->next)
nvn->next->prev = nvn->prev;
nvn->prev = nvn->next = NULL;
put_vnode(ns->nsid, nvn->vnid);
}
if (vn->prev)
vn->prev->next = vn->next;
else
vn->parent->head = vn->next;
if (vn->next)
vn->next->prev = vn->prev;
pvn = NULL;
nvn = newdir->head;
while (nvn && (strcmp(newname, nvn->name) > 0)) {
pvn = nvn;
nvn = nvn->next;
}
vn->next = nvn;
if (nvn)
nvn->prev = vn;
vn->prev = pvn;
if (pvn)
pvn->next = vn;
else
newdir->head = vn;
vn->parent = newdir;
newdir->mtime = olddir->mtime = time(NULL);
strcpy(p, newname);
vn->name = p;
exit:
UNLOCK(ns->lock);
return 0;
error1:
UNLOCK(ns->lock);
return err;
}
static int
rootfs_unlink(void *_ns, void *_dir, const char *name)
{
nspace *ns;
vnode *dir;
ns = (nspace *) _ns;
dir = (vnode *) _dir;
return do_unlink(ns, dir, name, FALSE);
}
static int
rootfs_rmdir(void *_ns, void *_dir, const char *name)
{
nspace *ns;
vnode *dir;
ns = (nspace *) _ns;
dir = (vnode *) _dir;
return do_unlink(ns, dir, name, TRUE);
}
static int
rootfs_read_vnode(void *_ns, vnode_id vnid, char r, void **node)
{
nspace *ns;
vnode *vn;
vnode fakevn;
ns = (nspace *) _ns;
if (!r)
LOCK(ns->lock);
fakevn.vnid = vnid;
fakevn.ns = ns;
vn = SearchSL(ns->skiplist, &fakevn);
if (vn)
*node = vn;
if (!r)
UNLOCK(ns->lock);
return (vn ? 0 : ENOENT);
}
static int
rootfs_write_vnode(void *_ns, void *_node, char r)
{
return 0;
}
static int
rootfs_remove_vnode(void *_ns, void *_node, char r)
{
nspace *ns;
vnode *node;
ns = (nspace *) _ns;
node = (vnode *) _node;
if (!r)
LOCK(ns->lock);
DeleteSL(ns->skiplist, node);
if (!r)
UNLOCK(ns->lock);
atomic_add(&ns->vnnum, -1);
if (node->symlink)
free(node->symlink);
free(node->name);
free(node);
return 0;
}
static int
rootfs_readlink(void *_ns, void *_node, char *buf, size_t *bufsize)
{
nspace *ns;
vnode *node;
int err;
size_t l;
ns = (nspace *) _ns;
node = (vnode *) _node;
if (!MY_S_ISLNK(node->mode)) {
err = EINVAL;
goto error1;
}
l = strlen(node->symlink);
if (l > *bufsize)
memcpy(buf, node->symlink, *bufsize);
else
memcpy(buf, node->symlink, l);
*bufsize = l;
return 0;
error1:
return err;
}
static int
rootfs_opendir(void *_ns, void *_node, void **cookie)
{
nspace *ns;
vnode *node;
int err;
dirpos *pos;
ns = (nspace *) _ns;
node = (vnode *) _node;
if (!MY_S_ISDIR(node->mode)) {
err = ENOTDIR;
goto error1;
}
pos = (dirpos *) malloc(sizeof(dirpos));
if (!pos) {
err = ENOMEM;
goto error1;
}
if (new_lock(&pos->lock, "rootdirlock") < 0) {
err = EINVAL;
goto error2;
}
pos->pos = 0;
pos->name[0] = '\0';
*cookie = pos;
return 0;
error2:
free(pos);
error1:
return err;
}
static int
rootfs_closedir(void *_ns, void *_node, void *_cookie)
{
return 0;
}
static int
rootfs_free_dircookie(void *_ns, void *_node, void *_cookie)
{
nspace *ns;
vnode *node;
dirpos *cookie;
ns = (nspace *) _ns;
node = (vnode *) _node;
cookie = (dirpos *) _cookie;
free_lock(&cookie->lock);
free(cookie);
return 0;
}
static int
rootfs_rewinddir(void *_ns, void *_node, void *_cookie)
{
nspace *ns;
vnode *node;
dirpos *cookie;
ns = (nspace *) _ns;
node = (vnode *) _node;
cookie = (dirpos *) _cookie;
LOCK(cookie->lock);
cookie->pos = 0;
cookie->name[0] = '\0';
UNLOCK(cookie->lock);
return 0;
}
static int
rootfs_readdir(void *_ns, void *_node, void *_cookie, long *num,
struct my_dirent *buf, size_t bufsize)
{
nspace *ns;
vnode *node;
dirpos *cookie;
char *e, *q;
struct my_dirent *p;
long i;
vnode *vn;
vnode_id vnid;
char *name, *last;
int sl, rl;
ns = (nspace *) _ns;
node = (vnode *) _node;
cookie = (dirpos *) _cookie;
LOCK(ns->lock);
LOCK(cookie->lock);
vn = node->head;
p = (struct my_dirent *) buf;
e = (char *) buf + bufsize;
if (cookie->pos > 2)
while (vn && (strcmp(cookie->name, vn->name) >= 0))
vn = vn->next;
for(i=0; (i < *num) && ((cookie->pos < 2) || vn); i++, cookie->pos++) {
switch(cookie->pos) {
case 0:
name = ".";
vnid = node->vnid;
break;
case 1:
name = "..";
vnid = node->parent->vnid;
break;
default:
name = vn->name;
vnid = vn->vnid;
vn = vn->next;
break;
}
sl = strlen(name) + 1;
rl = sizeof(struct my_dirent) + sl - 1;
if ((char *)p + rl > e)
break;
last = name;
p->d_reclen = (rl + 7) & ~7;
p->d_ino = vnid;
memcpy(p->d_name, name, sl);
p = (struct my_dirent *)((char *)p + p->d_reclen);
}
if ((cookie->pos > 2) && (i > 0))
strcpy(cookie->name, last);
*num = i;
exit:
UNLOCK(cookie->lock);
UNLOCK(ns->lock);
return 0;
}
static int
rootfs_rstat(void *_ns, void *_node, struct my_stat *st)
{
nspace *ns;
vnode *node;
ns = (nspace *) _ns;
node = (vnode *) _node;
LOCK(ns->lock);
st->dev = ns->nsid;
st->ino = node->vnid;
st->mode = node->mode;
st->nlink = 1;
st->uid = node->uid;
st->gid = node->gid;
st->size = 0;
st->blksize = 0;
st->atime = st->ctime = st->mtime = node->mtime;
UNLOCK(ns->lock);
return 0;
}
static int
rootfs_wstat(void *_ns, void *_node, struct my_stat *st, long mask)
{
nspace *ns;
vnode *node;
ns = (nspace *) _ns;
node = (vnode *) _node;
if (mask & WSTAT_SIZE)
return EINVAL;
LOCK(ns->lock);
if (mask & WSTAT_MODE)
node->mode = (node->mode & MY_S_IFMT) | (st->mode & ~MY_S_IFMT);
if (mask & WSTAT_UID)
node->uid = st->uid;
if (mask & WSTAT_GID)
node->gid = st->gid;
if (mask & WSTAT_MTIME)
node->mtime = st->mtime;
if (mask & WSTAT_ATIME)
node->mtime = st->atime;
UNLOCK(ns->lock);
return 0;
}
static int
rootfs_mount(nspace_id nsid, const char *device, ulong flags, void *parms,
size_t len, void **data, vnode_id *vnid)
{
int err;
nspace *ns;
vnode *root;
vnode_id rvnid;
if (device || parms || (len != 0)) {
err = EINVAL;
goto error1;
}
ns = (nspace *) malloc(sizeof(nspace));
if (!ns) {
err = ENOMEM;
goto error1;
}
root = (vnode *) malloc(sizeof(vnode));
if (!root) {
err = ENOMEM;
goto error2;
}
rvnid = 1;
ns->nsid = nsid;
ns->vnnum = 0;
ns->nxvnid = rvnid;
ns->root = root;
if (new_lock(&ns->lock, "rootfs") < 0) {
err = -1;
goto error3;
}
ns->skiplist = NewSL(&compare_vnode, NULL, NO_DUPLICATES);
if (!ns->skiplist) {
err = -1;
goto error4;
}
root->vnid = rvnid;
root->parent = root;
root->ns = ns;
root->removed = FALSE;
root->name = NULL;
root->next = root->prev = NULL;
root->head = NULL;
root->symlink = NULL;
/* ### do it for real */
root->uid = 0;
root->gid = 0;
root->mode = MY_S_IFDIR | 0777;
root->mtime = time(NULL);
err = new_vnode(nsid, rvnid, root);
if (err)
goto error5;
*data = ns;
*vnid = rvnid;
return 0;
error5:
FreeSL(ns->skiplist);
error4:
free_lock(&ns->lock);
error3:
free(root);
error2:
free(ns);
error1:
return err;
}
static int
rootfs_unmount(void *_ns)
{
nspace *ns;
vnode *vn, *avn;
ns = (nspace *) _ns;
vn = ns->root;
while (TRUE) {
while(vn->head)
vn = vn->head;
vn = vn->parent;
if (vn == ns->root)
break;
avn = vn->head;
if (avn->prev)
avn->prev->next = avn->next;
else
vn->head = avn->next;
if (avn->next)
avn->next->prev = avn->prev;
rootfs_remove_vnode(ns, avn, TRUE);
}
free(ns->root);
free_lock(&ns->lock);
FreeSL(ns->skiplist);
free(ns);
return 0;
}
/* ### should do real permission check */
static int
rootfs_access(void *_ns, void *_node, int mode)
{
return 0;
}
static int
compare_vnode(vnode *vna, vnode *vnb)
{
if (vna->vnid > vnb->vnid)
return 1;
else
if (vna->vnid < vnb->vnid)
return -1;
else
return 0;
}
static int
do_create(nspace *ns, vnode *dir, const char *name, mode_t mode, vnode **vnp)
{
int err;
int c;
vnode *vn, *pvn, *nvn;
char *buf;
vnode_id vnid;
if (!MY_S_ISDIR(dir->mode)) {
err = ENOTDIR;
goto error1;
}
/*
make sure we are not trying to create something in a directory
that has been removed.
*/
if (dir->removed) {
err = ENOENT;
goto error1;
}
/*
filter the name:
can't be '.', or '..'
*/
if (!strcmp(name, ".") || !strcmp(name, "..")) {
err = EEXIST;
goto error1;
}
/*
lookup the name in the directory
*/
vn = dir->head;
while (vn) {
c = strcmp(name, vn->name);
if (c < 0)
vn = NULL;
if (c <= 0)
break;
vn = vn->next;
}
/*
if it was found, report an error.
*/
if (vn) {
err = EEXIST;
goto error1;
}
/*
allocate a vnode and fill it
*/
vn = NULL;
buf = NULL;
vn = (vnode *) malloc(sizeof(vnode));
buf = (char *) malloc(strlen(name)+1);
if (!vn || !buf) {
err = ENOMEM;
goto error2;
}
strcpy(buf, name);
vnid = ++ns->nxvnid;
vn->vnid = vnid;
vn->parent = dir;
vn->ns = ns;
vn->removed = FALSE;
vn->name = buf;
vn->mode = mode;
vn->uid = 0;
vn->gid = 0;
dir->mtime = vn->mtime = time(NULL);
pvn = NULL;
nvn = dir->head;
while (nvn && (strcmp(name, nvn->name) > 0)) {
pvn = nvn;
nvn = nvn->next;
}
vn->next = nvn;
if (nvn)
nvn->prev = vn;
vn->prev = pvn;
if (pvn)
pvn->next = vn;
else
dir->head = vn;
vn->head = NULL;
vn->symlink = NULL;
atomic_add(&ns->vnnum, 1);
InsertSL(ns->skiplist, vn);
*vnp = vn;
return 0;
error2:
if (vn)
free(vn);
if (buf)
free(buf);
error1:
return err;