9 char Ebadoff[] = "bad file offset or count";
10 char Eexist[] = "file already exists";
11 char Enomem[] = "no memory";
12 char Eperm[] = "permission denied";
13 char Enotowner[] = "not owner";
14 char Elocked[] = "file locked";
23 #define MAXFSIZE ((0x7fffffffll/sizeof(Ram*))*ESIZE)
25 typedef struct Ram Ram;
48 rammoved(void*, void *to)
50 Ram **link, **elink, *x = to;
57 for(elink = link + (x->size / sizeof(Ram*)); link < elink; link++)
58 if((x = *link) != nil)
69 .maxsize= 800*1024*1024,
80 accessfile(File *f, int a)
99 off = r->ifcall.offset;
100 count = r->ifcall.count;
102 if(count == 0 || off >= f->length || f->aux == nil){
118 p = (char*)r->ofcall.data;
124 if(i < (x->size / sizeof(Ram*)))
128 if(x != nil && o < x->size){
132 memmove(p, (char*)&x[1] + o, n);
143 accessfile(f, AREAD);
145 r->ofcall.count = p - (char*)r->ofcall.data;
159 off = r->ifcall.offset;
160 count = r->ifcall.count;
162 if(f->mode & DMAPPEND)
177 n = ((top + ESIZE-1)/ESIZE) * sizeof(Ram*);
179 if(x == nil || x->size < n){
180 x = poolrealloc(&rampool, x, sizeof(Ram) + n);
185 link = (Ram**)&f->aux;
187 memset(x, 0, sizeof(Ram));
191 } else if(x != *link)
193 memset((char*)&x[1] + x->size, 0, n - x->size);
197 p = (char*)r->ifcall.data;
206 x = ((Ram*)f->aux)->ent[i];
207 if(x == nil || x->size < o+n){
208 x = poolrealloc(&rampool, x, sizeof(Ram) + o+n);
213 link = &((Ram*)f->aux)->ent[i];
215 memset(x, 0, sizeof(Ram));
219 memset((char*)&x[1] + x->size, 0, o - x->size);
225 memmove((char*)&x[1] + o, p, n);
233 accessfile(f, AWRITE);
235 r->ofcall.count = p - (char*)r->ifcall.data;
240 truncfile(File *f, vlong l)
247 n = x->size / sizeof(Ram*);
251 if(o != 0 && x->ent[i] != nil){
252 x->ent[i]->size = o * sizeof(Ram*);
256 if(x->ent[i] != nil){
257 poolfree(&rampool, x->ent[i]);
264 poolfree(&rampool, (Ram*)f->aux);
281 * To change length, must have write permission on file.
283 if(r->d.length != ~0 && r->d.length != f->length){
284 if(r->d.length > MAXFSIZE){
288 if(!hasperm(f, u, AWRITE) || (f->mode & DMDIR) != 0)
293 * To change name, must have write permission in parent.
295 if(r->d.name[0] != '\0' && strcmp(r->d.name, f->name) != 0){
296 if((w = f->parent) == nil)
299 if(!hasperm(w, u, AWRITE)){
303 if((w = walkfile(w, r->d.name)) != nil){
311 * To change mode, must be owner or group leader.
312 * Because of lack of users file, leader=>group itself.
314 if(r->d.mode != ~0 && f->mode != r->d.mode){
315 if(strcmp(u, f->uid) != 0)
316 if(strcmp(u, f->gid) != 0){
317 respond(r, Enotowner);
323 * To change group, must be owner and member of new group,
324 * or leader of current group and leader of new group.
325 * Second case cannot happen, but we check anyway.
327 while(r->d.gid[0] != '\0' && strcmp(f->gid, r->d.gid) != 0){
328 if(strcmp(u, f->uid) == 0)
330 if(strcmp(u, f->gid) == 0)
331 if(strcmp(u, r->d.gid) == 0)
333 respond(r, Enotowner);
338 f->mode = (r->d.mode & ~DMDIR) | (f->mode & DMDIR);
341 f->qid.type |= QTDIR;
342 if(f->mode & DMAPPEND)
343 f->qid.type |= QTAPPEND;
345 f->qid.type |= QTEXCL;
347 if(r->d.name[0] != '\0'){
349 f->name = estrdup9p(r->d.name);
351 if(r->d.length != ~0 && r->d.length != f->length)
352 truncfile(f, r->d.length);
354 accessfile(f, AWRITE);
355 if(r->d.mtime != ~0){
356 f->mtime = r->d.mtime;
375 p = (p & ~0777) | ((p & f->mode) & 0777);
377 p = (p & ~0666) | ((p & f->mode) & 0666);
378 if((f = createfile(f, r->ifcall.name, r->fid->uid, p, nil)) == nil){
382 f->atime = f->mtime = time(0);
385 r->ofcall.qid = f->qid;
395 if((f->mode & DMEXCL) != 0){
396 if(f->ref > 2 && (long)((ulong)time(0)-(ulong)f->atime) < 300){
401 if((f->mode & DMAPPEND) == 0 && (r->ifcall.mode & OTRUNC) != 0){
403 accessfile(f, AWRITE);
409 fsdestroyfid(Fid *fid)
414 if(fid->omode != -1 && (fid->omode & ORCLOSE) != 0 && f != nil && f->parent != nil)
419 fsdestroyfile(File *f)
431 snprint(buf, sizeof buf, "/proc/%d/ctl", getpid());
432 if((ctl = open(buf, OWRITE)) < 0)
433 sysfatal("can't protect memory: %r");
434 fprint(ctl, "noswap\n");
435 fprint(ctl, "private\n");
446 .destroyfid= fsdestroyfid,
454 fprint(2, "usage: %s [-Dipsubac] [-m mountpoint] [-S srvname]\n", argv0);
459 main(int argc, char **argv)
466 fs.tree = alloctree(nil, nil, DMDIR|0777, fsdestroyfile);
467 q = fs.tree->root->qid;
479 srvname = EARGF(usage());
483 mtpt = EARGF(usage());
494 rampool.maxsize = (uintptr)~0;
497 mountflags |= MBEFORE;
500 mountflags |= MCREATE;
503 mountflags |= MAFTER;
518 if(srvname == nil && mtpt == nil)
519 sysfatal("must specify -S, or -m option");
522 mountflags = MREPL | MCREATE;
523 postmountsrv(&fs, srvname, mtpt, mountflags);