]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/bzfs/oramfs.c
stats: show amount of reclaimable pages (add -r flag)
[plan9front.git] / sys / src / cmd / bzfs / oramfs.c
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <fcall.h>
5 #include "bzfs.h"
6
7 /*
8  * Rather than reading /adm/users, which is a lot of work for
9  * a toy program, we assume all groups have the form
10  *      NNN:user:user:
11  * meaning that each user is the leader of his own group.
12  */
13
14 enum
15 {
16         OPERM   = 0x3,          /* mask of all permission types in open mode */
17         Nram    = 512,
18         Maxsize = 512*1024*1024,
19         Maxfdata        = 8192,
20 };
21
22 typedef struct Fid Fid;
23 typedef struct Ram Ram;
24
25 struct Fid
26 {
27         short   busy;
28         short   open;
29         short   rclose;
30         int     fid;
31         Fid     *next;
32         char    *user;
33         Ram     *ram;
34 };
35
36 struct Ram
37 {
38         short   busy;
39         short   open;
40         long    parent;         /* index in Ram array */
41         Qid     qid;
42         long    perm;
43         char    *name;
44         ulong   atime;
45         ulong   mtime;
46         char    *user;
47         char    *group;
48         char    *muid;
49         char    *data;
50         long    ndata;
51 };
52
53 enum
54 {
55         Pexec =         1,
56         Pwrite =        2,
57         Pread =         4,
58         Pother =        1,
59         Pgroup =        8,
60         Powner =        64,
61 };
62
63 ulong   path;           /* incremented for each new file */
64 Fid     *fids;
65 Ram     ram[Nram];
66 int     nram;
67 int     mfd[2];
68 char    *user;
69 uchar   mdata[IOHDRSZ+Maxfdata];
70 uchar   rdata[Maxfdata];        /* buffer for data in reply */
71 uchar statbuf[STATMAX];
72 Fcall thdr;
73 Fcall   rhdr;
74 int     messagesize = sizeof mdata;
75
76 Fid *   newfid(int);
77 uint    ramstat(Ram*, uchar*, uint);
78 void    io(void);
79 void    *erealloc(void*, ulong);
80 void    *emalloc(ulong);
81 char    *estrdup(char*);
82 void    ramfsusage(void);
83 int     perm(Fid*, Ram*, int);
84 char *atom(char*);
85
86 char    *rflush(Fid*), *rversion(Fid*), *rauth(Fid*),
87         *rattach(Fid*), *rwalk(Fid*),
88         *ropen(Fid*), *rcreate(Fid*),
89         *rread(Fid*), *rwrite(Fid*), *rclunk(Fid*),
90         *rremove(Fid*), *rstat(Fid*), *rwstat(Fid*);
91
92 char    *(*fcalls[])(Fid*) = {
93         [Tversion]      rversion,
94         [Tflush]        rflush,
95         [Tauth] rauth,
96         [Tattach]       rattach,
97         [Twalk]         rwalk,
98         [Topen]         ropen,
99         [Tcreate]       rcreate,
100         [Tread]         rread,
101         [Twrite]        rwrite,
102         [Tclunk]        rclunk,
103         [Tremove]       rremove,
104         [Tstat]         rstat,
105         [Twstat]        rwstat,
106 };
107
108 char    Eperm[] =       "permission denied";
109 char    Enotdir[] =     "not a directory";
110 char    Enoauth[] =     "no authentication in ramfs";
111 char    Enotexist[] =   "file does not exist";
112 char    Einuse[] =      "file in use";
113 char    Eexist[] =      "file exists";
114 char    Eisdir[] =      "file is a directory";
115 char    Enotowner[] =   "not owner";
116 char    Eisopen[] =     "file already open for I/O";
117 char    Excl[] =        "exclusive use file already open";
118 char    Ename[] =       "illegal name";
119 char    Eversion[] =    "unknown 9P version";
120
121 int debug;
122
123 void
124 notifyf(void *a, char *s)
125 {
126         USED(a);
127         if(strncmp(s, "interrupt", 9) == 0)
128                 noted(NCONT);
129         noted(NDFLT);
130 }
131
132 void
133 ramfsmain(int argc, char *argv[])
134 {
135         Ram *r;
136         char *defmnt;
137         int p[2];
138         char buf[32];
139         int fd, srvfd;
140         int stdio = 0;
141
142         srvfd = -1;
143         defmnt = "/tmp";
144         ARGBEGIN{
145         case 'D':
146                 debug = 1;
147                 break;
148         case 'i':               /* this is DIFFERENT from normal ramfs; use 1 for both for kernel */
149                 defmnt = 0;
150                 stdio = 1;
151                 srvfd = 0;
152                 mfd[0] = 1;
153                 mfd[1] = 1;
154                 break;
155         case 's':
156                 defmnt = 0;
157                 break;
158         case 'm':
159                 defmnt = ARGF();
160                 break;
161         default:
162                 ramfsusage();
163         }ARGEND
164
165         if(!stdio){
166                 if(pipe(p) < 0)
167                         error("pipe failed");
168                 srvfd = p[1];
169                 mfd[0] = p[0];
170                 mfd[1] = p[0];
171                 if(defmnt == 0){
172                         fd = create("#s/ramfs", OWRITE, 0666);
173                         if(fd < 0)
174                                 error("create of /srv/ramfs failed");
175                         sprint(buf, "%d", p[1]);
176                         if(write(fd, buf, strlen(buf)) < 0)
177                                 error("writing /srv/ramfs");
178                 }
179         }
180
181         user = atom(getuser());
182         notify(notifyf);
183         nram = 1;
184         r = &ram[0];
185         r->busy = 1;
186         r->data = 0;
187         r->ndata = 0;
188         r->perm = DMDIR | 0775;
189         r->qid.type = QTDIR;
190         r->qid.path = 0LL;
191         r->qid.vers = 0;
192         r->parent = 0;
193         r->user = user;
194         r->group = user;
195         r->muid = user;
196         r->atime = time(0);
197         r->mtime = r->atime;
198         r->name = estrdup(".");
199
200         if(debug)
201                 fmtinstall('F', fcallfmt);
202         switch(rfork(RFFDG|RFPROC|RFNAMEG|RFNOTEG)){
203         case -1:
204                 error("fork");
205         case 0:
206                 close(srvfd);
207                 io();
208                 break;
209         default:
210                 close(mfd[0]);  /* don't deadlock if child fails */
211                 if(defmnt && mount(srvfd, -1, defmnt, MREPL|MCREATE, "") < 0)
212                         error("mount failed: %r");
213         }
214 }
215
216 char*
217 rversion(Fid*)
218 {
219         Fid *f;
220
221         for(f = fids; f; f = f->next)
222                 if(f->busy)
223                         rclunk(f);
224         if(thdr.msize < 256)
225                 return "message size too small";
226         if(thdr.msize > sizeof mdata)
227                 rhdr.msize = sizeof mdata;
228         else
229                 rhdr.msize = thdr.msize;
230         messagesize = rhdr.msize;
231         if(strncmp(thdr.version, "9P2000", 6) != 0)
232                 return Eversion;
233         rhdr.version = "9P2000";
234         return 0;
235 }
236
237 char*
238 rauth(Fid*)
239 {
240         return "ramfs: no authentication required";
241 }
242
243 char*
244 rflush(Fid *f)
245 {
246         USED(f);
247         return 0;
248 }
249
250 char*
251 rattach(Fid *f)
252 {
253         /* no authentication! */
254         f->busy = 1;
255         f->rclose = 0;
256         f->ram = &ram[0];
257         rhdr.qid = f->ram->qid;
258         if(thdr.uname[0])
259                 f->user = atom(thdr.uname);
260         else
261                 f->user = atom("none");
262         if(strcmp(user, "none") == 0)
263                 user = f->user;
264         return 0;
265 }
266
267 char*
268 clone(Fid *f, Fid **nf)
269 {
270         if(f->open)
271                 return Eisopen;
272         if(f->ram->busy == 0)
273                 return Enotexist;
274         *nf = newfid(thdr.newfid);
275         (*nf)->busy = 1;
276         (*nf)->open = 0;
277         (*nf)->rclose = 0;
278         (*nf)->ram = f->ram;
279         (*nf)->user = f->user;  /* no ref count; the leakage is minor */
280         return 0;
281 }
282
283 char*
284 rwalk(Fid *f)
285 {
286         Ram *r, *fram;
287         char *name;
288         Ram *parent;
289         Fid *nf;
290         char *err;
291         ulong t;
292         int i;
293
294         err = nil;
295         nf = nil;
296         rhdr.nwqid = 0;
297         if(rhdr.newfid != rhdr.fid){
298                 err = clone(f, &nf);
299                 if(err)
300                         return err;
301                 f = nf; /* walk the new fid */
302         }
303         fram = f->ram;
304         if(thdr.nwname > 0){
305                 t = time(0);
306                 for(i=0; i<thdr.nwname && i<MAXWELEM; i++){
307                         if((fram->qid.type & QTDIR) == 0){
308                                 err = Enotdir;
309                                 break;
310                         }
311                         if(fram->busy == 0){
312                                 err = Enotexist;
313                                 break;
314                         }
315                         fram->atime = t;
316                         name = thdr.wname[i];
317                         if(strcmp(name, ".") == 0){
318     Found:
319                                 rhdr.nwqid++;
320                                 rhdr.wqid[i] = fram->qid;
321                                 continue;
322                         }
323                         parent = &ram[fram->parent];
324 #ifdef CHECKS
325                         if(!perm(f, parent, Pexec)){
326                                 err = Eperm;
327                                 break;
328                         }
329 #endif
330                         if(strcmp(name, "..") == 0){
331                                 fram = parent;
332                                 goto Found;
333                         }
334                         for(r=ram; r < &ram[nram]; r++)
335                                 if(r->busy && r->parent==fram-ram && strcmp(name, r->name)==0){
336                                         fram = r;
337                                         goto Found;
338                                 }
339                         break;
340                 }
341                 if(i==0 && err == nil)
342                         err = Enotexist;
343         }
344         if(nf != nil && (err!=nil || rhdr.nwqid<thdr.nwname)){
345                 /* clunk the new fid, which is the one we walked */
346                 f->busy = 0;
347                 f->ram = nil;
348         }
349         if(rhdr.nwqid == thdr.nwname)   /* update the fid after a successful walk */
350                 f->ram = fram;
351         return err;
352 }
353
354 char *
355 ropen(Fid *f)
356 {
357         Ram *r;
358         int mode, trunc;
359
360         if(f->open)
361                 return Eisopen;
362         r = f->ram;
363         if(r->busy == 0)
364                 return Enotexist;
365         if(r->perm & DMEXCL)
366                 if(r->open)
367                         return Excl;
368         mode = thdr.mode;
369         if(r->qid.type & QTDIR){
370                 if(mode != OREAD)
371                         return Eperm;
372                 rhdr.qid = r->qid;
373                 return 0;
374         }
375         if(mode & ORCLOSE){
376                 /* can't remove root; must be able to write parent */
377                 if(r->qid.path==0 || !perm(f, &ram[r->parent], Pwrite))
378                         return Eperm;
379                 f->rclose = 1;
380         }
381         trunc = mode & OTRUNC;
382         mode &= OPERM;
383         if(mode==OWRITE || mode==ORDWR || trunc)
384                 if(!perm(f, r, Pwrite))
385                         return Eperm;
386         if(mode==OREAD || mode==ORDWR)
387                 if(!perm(f, r, Pread))
388                         return Eperm;
389         if(mode==OEXEC)
390                 if(!perm(f, r, Pexec))
391                         return Eperm;
392         if(trunc && (r->perm&DMAPPEND)==0){
393                 r->ndata = 0;
394                 if(r->data)
395                         free(r->data);
396                 r->data = 0;
397                 r->qid.vers++;
398         }
399         rhdr.qid = r->qid;
400         rhdr.iounit = messagesize-IOHDRSZ;
401         f->open = 1;
402         r->open++;
403         return 0;
404 }
405
406 char *
407 rcreate(Fid *f)
408 {
409         Ram *r;
410         char *name;
411         long parent, prm;
412
413         if(f->open)
414                 return Eisopen;
415         if(f->ram->busy == 0)
416                 return Enotexist;
417         parent = f->ram - ram;
418         if((f->ram->qid.type&QTDIR) == 0)
419                 return Enotdir;
420         /* must be able to write parent */
421 #ifdef CHECKS
422         if(!perm(f, f->ram, Pwrite))
423                 return Eperm;
424 #endif
425         prm = thdr.perm;
426         name = thdr.name;
427         if(strcmp(name, ".")==0 || strcmp(name, "..")==0)
428                 return Ename;
429         for(r=ram; r<&ram[nram]; r++)
430                 if(r->busy && parent==r->parent)
431                 if(strcmp((char*)name, r->name)==0)
432                         return Einuse;
433         for(r=ram; r->busy; r++)
434                 if(r == &ram[Nram-1])
435                         return "no free ram resources";
436         r->busy = 1;
437         r->qid.path = ++path;
438         r->qid.vers = 0;
439         if(prm & DMDIR)
440                 r->qid.type |= QTDIR;
441         r->parent = parent;
442         free(r->name);
443         r->name = estrdup(name);
444         r->user = f->user;
445         r->group = f->ram->group;
446         r->muid = f->ram->muid;
447         if(prm & DMDIR)
448                 prm = (prm&~0777) | (f->ram->perm&prm&0777);
449         else
450                 prm = (prm&(~0777|0111)) | (f->ram->perm&prm&0666);
451         r->perm = prm;
452         r->ndata = 0;
453         if(r-ram >= nram)
454                 nram = r - ram + 1;
455         r->atime = time(0);
456         r->mtime = r->atime;
457         f->ram->mtime = r->atime;
458         f->ram = r;
459         rhdr.qid = r->qid;
460         rhdr.iounit = messagesize-IOHDRSZ;
461         f->open = 1;
462         if(thdr.mode & ORCLOSE)
463                 f->rclose = 1;
464         r->open++;
465         return 0;
466 }
467
468 char*
469 rread(Fid *f)
470 {
471         Ram *r;
472         uchar *buf;
473         long off;
474         int n, m, cnt;
475
476         if(f->ram->busy == 0)
477                 return Enotexist;
478         n = 0;
479         rhdr.count = 0;
480         off = thdr.offset;
481         buf = rdata;
482         cnt = thdr.count;
483         if(cnt > messagesize)   /* shouldn't happen, anyway */
484                 cnt = messagesize;
485         if(f->ram->qid.type & QTDIR){
486                 for(r=ram+1; off > 0; r++){
487                         if(r->busy && r->parent==f->ram-ram)
488                                 off -= ramstat(r, statbuf, sizeof statbuf);
489                         if(r == &ram[nram-1])
490                                 return 0;
491                 }
492                 for(; r<&ram[nram] && n < cnt; r++){
493                         if(!r->busy || r->parent!=f->ram-ram)
494                                 continue;
495                         m = ramstat(r, buf+n, cnt-n);
496                         if(m == 0)
497                                 break;
498                         n += m;
499                 }
500                 rhdr.data = (char*)rdata;
501                 rhdr.count = n;
502                 return 0;
503         }
504         r = f->ram;
505         if(off >= r->ndata)
506                 return 0;
507         r->atime = time(0);
508         n = cnt;
509         if(off+n > r->ndata)
510                 n = r->ndata - off;
511         rhdr.data = r->data+off;
512         rhdr.count = n;
513         return 0;
514 }
515
516 char*
517 rwrite(Fid *f)
518 {
519         Ram *r;
520         ulong off;
521         int cnt;
522
523         r = f->ram;
524         if(r->busy == 0)
525                 return Enotexist;
526         off = thdr.offset;
527         if(r->perm & DMAPPEND)
528                 off = r->ndata;
529         cnt = thdr.count;
530         if(r->qid.type & QTDIR)
531                 return Eisdir;
532         if(off+cnt >= Maxsize)          /* sanity check */
533                 return "write too big";
534         if(off+cnt > r->ndata)
535                 r->data = erealloc(r->data, off+cnt);
536         if(off > r->ndata)
537                 memset(r->data+r->ndata, 0, off-r->ndata);
538         if(off+cnt > r->ndata)
539                 r->ndata = off+cnt;
540         memmove(r->data+off, thdr.data, cnt);
541         r->qid.vers++;
542         r->mtime = time(0);
543         rhdr.count = cnt;
544         return 0;
545 }
546
547 void
548 realremove(Ram *r)
549 {
550         r->ndata = 0;
551         if(r->data)
552                 free(r->data);
553         r->data = 0;
554         r->parent = 0;
555         memset(&r->qid, 0, sizeof r->qid);
556         free(r->name);
557         r->name = nil;
558         r->busy = 0;
559 }
560
561 char *
562 rclunk(Fid *f)
563 {
564         if(f->open)
565                 f->ram->open--;
566         if(f->rclose)
567                 realremove(f->ram);
568         f->busy = 0;
569         f->open = 0;
570         f->ram = 0;
571         return 0;
572 }
573
574 char *
575 rremove(Fid *f)
576 {
577         Ram *r;
578
579         if(f->open)
580                 f->ram->open--;
581         f->busy = 0;
582         f->open = 0;
583         r = f->ram;
584         f->ram = 0;
585 #ifdef CHECKS
586         if(r->qid.path == 0 || !perm(f, &ram[r->parent], Pwrite))
587                 return Eperm;
588 #endif
589         ram[r->parent].mtime = time(0);
590         realremove(r);
591         return 0;
592 }
593
594 char *
595 rstat(Fid *f)
596 {
597         if(f->ram->busy == 0)
598                 return Enotexist;
599         rhdr.nstat = ramstat(f->ram, statbuf, sizeof statbuf);
600         rhdr.stat = statbuf;
601         return 0;
602 }
603
604 char *
605 rwstat(Fid *f)
606 {
607         Ram *r, *s;
608         Dir dir;
609
610         if(f->ram->busy == 0)
611                 return Enotexist;
612         convM2D(thdr.stat, thdr.nstat, &dir, (char*)statbuf);
613         r = f->ram;
614
615         /*
616          * To change length, must have write permission on file.
617          */
618 #ifdef CHECKS
619         if(dir.length!=~0 && dir.length!=r->ndata){
620                 if(!perm(f, r, Pwrite))
621                         return Eperm;
622         }
623 #endif
624
625         /*
626          * To change name, must have write permission in parent
627          * and name must be unique.
628          */
629         if(dir.name[0]!='\0' && strcmp(dir.name, r->name)!=0){
630 #ifdef CHECKS
631                 if(!perm(f, &ram[r->parent], Pwrite))
632                         return Eperm;
633 #endif
634                 for(s=ram; s<&ram[nram]; s++)
635                         if(s->busy && s->parent==r->parent)
636                         if(strcmp(dir.name, s->name)==0)
637                                 return Eexist;
638         }
639
640 #ifdef OWNERS
641         /*
642          * To change mode, must be owner or group leader.
643          * Because of lack of users file, leader=>group itself.
644          */
645         if(dir.mode!=~0 && r->perm!=dir.mode){
646                 if(strcmp(f->user, r->user) != 0)
647                 if(strcmp(f->user, r->group) != 0)
648                         return Enotowner;
649         }
650
651         /*
652          * To change group, must be owner and member of new group,
653          * or leader of current group and leader of new group.
654          * Second case cannot happen, but we check anyway.
655          */
656         if(dir.gid[0]!='\0' && strcmp(r->group, dir.gid)!=0){
657                 if(strcmp(f->user, r->user) == 0)
658                 if(strcmp(f->user, dir.gid) == 0)
659                         goto ok;
660                 if(strcmp(f->user, r->group) == 0)
661                 if(strcmp(f->user, dir.gid) == 0)
662                         goto ok;
663                 return Enotowner;
664                 ok:;
665         }
666 #endif
667
668         /* all ok; do it */
669         if(dir.mode != ~0){
670                 dir.mode &= ~DMDIR;     /* cannot change dir bit */
671                 dir.mode |= r->perm&DMDIR;
672                 r->perm = dir.mode;
673         }
674         if(dir.name[0] != '\0'){
675                 free(r->name);
676                 r->name = estrdup(dir.name);
677         }
678         if(dir.gid[0] != '\0')
679                 r->group = atom(dir.gid);
680
681         if(dir.uid[0] != '\0')
682                 r->user = atom(dir.uid);
683
684         if(dir.length!=~0 && dir.length!=r->ndata){
685                 r->data = erealloc(r->data, dir.length);
686                 if(r->ndata < dir.length)
687                         memset(r->data+r->ndata, 0, dir.length-r->ndata);
688                 r->ndata = dir.length;
689         }
690
691         if(dir.mtime != ~0)
692                 r->mtime = dir.mtime;
693
694         ram[r->parent].mtime = time(0);
695         return 0;
696 }
697
698 uint
699 ramstat(Ram *r, uchar *buf, uint nbuf)
700 {
701         Dir dir;
702
703         dir.name = r->name;
704         dir.qid = r->qid;
705         dir.mode = r->perm;
706         dir.length = r->ndata;
707         dir.uid = r->user;
708         dir.gid = r->group;
709         dir.muid = r->muid;
710         dir.atime = r->atime;
711         dir.mtime = r->mtime;
712         return convD2M(&dir, buf, nbuf);
713 }
714
715 Fid *
716 newfid(int fid)
717 {
718         Fid *f, *ff;
719
720         ff = 0;
721         for(f = fids; f; f = f->next)
722                 if(f->fid == fid)
723                         return f;
724                 else if(!ff && !f->busy)
725                         ff = f;
726         if(ff){
727                 ff->fid = fid;
728                 return ff;
729         }
730         f = emalloc(sizeof *f);
731         f->ram = nil;
732         f->fid = fid;
733         f->next = fids;
734         fids = f;
735         return f;
736 }
737
738 void
739 io(void)
740 {
741         char *err;
742         int n, pid;
743
744         pid = getpid();
745
746         while((n = read9pmsg(mfd[0], mdata, messagesize)) != 0){
747                 if(n < 0)
748                         error("mount read: %r");
749                 if(convM2S(mdata, n, &thdr) != n)
750                         error("convM2S format error: %r");
751
752                 if(debug)
753                         fprint(2, "ramfs %d:<-%F\n", pid, &thdr);
754
755                 if(!fcalls[thdr.type])
756                         err = "bad fcall type";
757                 else
758                         err = (*fcalls[thdr.type])(newfid(thdr.fid));
759                 if(err){
760                         rhdr.type = Rerror;
761                         rhdr.ename = err;
762                 }else{
763                         rhdr.type = thdr.type + 1;
764                         rhdr.fid = thdr.fid;
765                 }
766                 rhdr.tag = thdr.tag;
767                 if(debug)
768                         fprint(2, "ramfs %d:->%F\n", pid, &rhdr);/**/
769                 n = convS2M(&rhdr, mdata, messagesize);
770                 if(n == 0)
771                         error("convS2M error on write");
772                 if(write(mfd[1], mdata, n) != n)
773                         error("mount write");
774         }
775 }
776
777 int
778 perm(Fid *f, Ram *r, int p)
779 {
780         if((p*Pother) & r->perm)
781                 return 1;
782         if(strcmp(f->user, r->group)==0 && ((p*Pgroup) & r->perm))
783                 return 1;
784         if(strcmp(f->user, r->user)==0 && ((p*Powner) & r->perm))
785                 return 1;
786         return 0;
787 }
788
789 void *
790 emalloc(ulong n)
791 {
792         void *p;
793
794         p = malloc(n);
795         if(!p)
796                 error("out of memory");
797         memset(p, 0, n);
798         return p;
799 }
800
801 void *
802 erealloc(void *p, ulong n)
803 {
804         p = realloc(p, n);
805         if(!p && n)
806                 error("out of memory");
807         return p;
808 }
809
810 char *
811 estrdup(char *q)
812 {
813         char *p;
814         int n;
815
816         n = strlen(q)+1;
817         p = malloc(n);
818         if(!p)
819                 error("out of memory");
820         memmove(p, q, n);
821         return p;
822 }
823
824 void
825 ramfsusage(void)
826 {
827         fprint(2, "usage: %s [-is] [-m mountpoint]\n", argv0);
828         exits("usage");
829 }
830
831 /*
832  *      Custom allocators to avoid malloc overheads on small objects.
833  *      We never free these.  (See below.)
834  */
835 typedef struct Stringtab        Stringtab;
836 struct Stringtab {
837         Stringtab *link;
838         char *str;
839 };
840 static Stringtab*
841 taballoc(void)
842 {
843         static Stringtab *t;
844         static uint nt;
845
846         if(nt == 0){
847                 t = malloc(64*sizeof(Stringtab));
848                 if(t == 0)
849                         sysfatal("out of memory");
850                 nt = 64;
851         }
852         nt--;
853         return t++;
854 }
855
856 static char*
857 xstrdup(char *s)
858 {
859         char *r;
860         int len;
861         static char *t;
862         static int nt;
863
864         len = strlen(s)+1;
865         if(len >= 8192)
866                 sysfatal("strdup big string");
867
868         if(nt < len){
869                 t = malloc(8192);
870                 if(t == 0)
871                         sysfatal("out of memory");
872                 nt = 8192;
873         }
874         r = t;
875         t += len;
876         nt -= len;
877         strcpy(r, s);
878         return r;
879 }
880
881 /*
882  *      Return a uniquely allocated copy of a string.
883  *      Don't free these -- they stay in the table for the 
884  *      next caller who wants that particular string.
885  *      String comparison can be done with pointer comparison 
886  *      if you know both strings are atoms.
887  */
888 static Stringtab *stab[1024];
889
890 static uint
891 hash(char *s)
892 {
893         uint h;
894         uchar *p;
895
896         h = 0;
897         for(p=(uchar*)s; *p; p++)
898                 h = h*37 + *p;
899         return h;
900 }
901
902 char*
903 atom(char *str)
904 {
905         uint h;
906         Stringtab *tab;
907         
908         h = hash(str) % nelem(stab);
909         for(tab=stab[h]; tab; tab=tab->link)
910                 if(strcmp(str, tab->str) == 0)
911                         return tab->str;
912
913         tab = taballoc();
914         tab->str = xstrdup(str);
915         tab->link = stab[h];
916         stab[h] = tab;
917         return tab->str;
918 }