summaryrefslogtreecommitdiff
path: root/sys/src/cmd/9nfs/xfile.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/9nfs/xfile.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/9nfs/xfile.c')
-rwxr-xr-xsys/src/cmd/9nfs/xfile.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/sys/src/cmd/9nfs/xfile.c b/sys/src/cmd/9nfs/xfile.c
new file mode 100755
index 000000000..6f294d6d9
--- /dev/null
+++ b/sys/src/cmd/9nfs/xfile.c
@@ -0,0 +1,106 @@
+#include "all.h"
+
+static Xfile * xflfree;
+static Xfid * xfdfree;
+
+#define FIDMOD 127 /* prime */
+
+static Xfile *xfiles[FIDMOD];
+static Lock xlocks[FIDMOD];
+
+Xfile *
+xfile(Qid *qid, void *s, int new)
+{
+ int k;
+ Xfile **hp, *f, *pf;
+
+ k = ((ulong)qid->path ^ (((u32int)(uintptr)s)<<24))%FIDMOD;
+ hp = &xfiles[k];
+
+ lock(&xlocks[k]);
+ for(f=*hp, pf=0; f; pf=f, f=f->next)
+ if(f->qid.path == qid->path
+ && (u32int)(uintptr)f->s == (u32int)(uintptr)s)
+ break;
+ if(f && pf){
+ pf->next = f->next;
+ f->next = *hp;
+ *hp = f;
+ }
+ if(new < 0 && f){
+ *hp = f->next;
+ f->next = xflfree;
+ xflfree = f;
+ f = 0;
+ }
+ if(new > 0 && !f){
+ if(!(f = xflfree)) /* assign = */
+ f = listalloc(1024/sizeof(Xfile), sizeof(Xfile));
+ xflfree = f->next;
+ memset(f, 0, sizeof(Xfile));
+ f->next = *hp;
+ *hp = f;
+ f->qid = *qid;
+ f->s = s;
+ }
+ unlock(&xlocks[k]);
+ return f;
+}
+
+Xfid *
+xfid(char *uid, Xfile *xp, int new)
+{
+ Xfid *f, *pf;
+
+ for(f=xp->users, pf=0; f; pf=f, f=f->next)
+ if(f->uid[0] == uid[0] && strcmp(f->uid, uid) == 0)
+ break;
+ if(f && pf){
+ pf->next = f->next;
+ f->next = xp->users;
+ xp->users = f;
+ }
+ if(new < 0 && f){
+ if(f->urfid)
+ clunkfid(xp->s, f->urfid);
+ if(f->opfid)
+ clunkfid(xp->s, f->opfid);
+ xp->users = f->next;
+ f->next = xfdfree;
+ xfdfree = f;
+ f = 0;
+ }
+ if(new > 0 && !f){
+ if(!(f = xfdfree)) /* assign = */
+ f = listalloc(1024/sizeof(Xfid), sizeof(Xfid));
+ xfdfree = f->next;
+ memset(f, 0, sizeof(Xfid));
+ f->next = xp->users;
+ xp->users = f;
+ f->xp = xp;
+ f->uid = strstore(uid);
+ f->urfid = 0;
+ f->opfid = 0;
+ }
+ return f;
+}
+
+int
+xfpurgeuid(Session *s, char *uid)
+{
+ Xfile **hp, *f;
+ Xfid *xf;
+ int k, count=0;
+
+ for(k=0; k<FIDMOD; k++){
+ lock(&xlocks[k]);
+ hp=&xfiles[k];
+ for(f=*hp; f; f=f->next)
+ if(f->s == s && (xf = xfid(uid, f, 0))){ /* assign = */
+ xfclear(xf);
+ ++count;
+ }
+ unlock(&xlocks[k]);
+ }
+ return count;
+}