/* * returns a locked file structure */ File* filep(Chan *cp, uint32_t fid, int flag) { File *f; int h; if(fid == NOFID) return 0; h = (int32_t)(uintptr)cp + fid; if(h < 0) h = ~h; h %= nelem(flist); loop: lock(&flock); for(f=flist[h]; f; f=f->next) if(f->fid == fid && f->cp == cp){ /* * Already in use is an error * when called from attach or clone (walk * in 9P2000). The console uses FID[12] and * never clunks them so catch that case. */ if(flag == 0 || cp == cons.chan) goto out; unlock(&flock); return 0; } if(flag) { f = newfp(); if(f) { f->fid = fid; f->cp = cp; f->wpath = 0; f->tlock = 0; f->doffset = 0; f->dslot = 0; f->auth = 0; f->next = flist[h]; flist[h] = f; goto out; } } unlock(&flock); return 0; out: unlock(&flock); qlock(f); if(f->fid == fid && f->cp == cp) return f; qunlock(f); goto loop; }
/* * returns a locked file structure */ File* filep(Chan *cp, int fid, int flag) { File *f, *prev; if(fid == NOF) return 0; loop: lock(&cp->flock); for(prev=0,f=cp->flist; f; prev=f,f=f->next) { if(f->fid != fid) continue; if(prev) { prev->next = f->next; f->next = cp->flist; cp->flist = f; } goto out; } if(flag) { f = newfp(cp); if(f) { f->fid = fid; goto out; } } else print("cannot find %p.%d (list=%p)\n", cp, fid, cp->flist); unlock(&cp->flock); return 0; out: unlock(&cp->flock); qlock(f); if(f->fid != fid) { qunlock(f); goto loop; } return f; }