commit 98c40d6851efa9370cb8dd470f841d61cfd2539e
parent 79d65ee7842c7f23b7d07efbc4c99eb37cf00cba
Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com>
Date: Wed, 23 Nov 2022 16:46:59 +0100
os9: Differentiate between dev and chan functions
Dev functions must be internal functions used in dev/ while
chan functions that are used in the kernel to operate with
channels.
Diffstat:
15 files changed, 77 insertions(+), 65 deletions(-)
diff --git a/include/os9/os9.h b/include/os9/os9.h
@@ -330,10 +330,10 @@ extern void idle(void);
/* dev functions */
extern void idev(void);
-extern Chan *devopen(char *name, int mode);
-extern int devwrite(Chan *c, void *buf, int n);
-extern int devread(Chan *c, void *buf, int n);
-extern int devclose(Chan *c);
+extern Chan *chanopen(char *name, int mode);
+extern int chanwrite(Chan *c, void *buf, int n);
+extern int chanread(Chan *c, void *buf, int n);
+extern int chanclose(Chan *c);
extern int mount(char *, char *, char *);
extern int bind(char *, char *);
extern Nspace *newspace(Nspace *);
diff --git a/src/libk/getline.c b/src/libk/getline.c
@@ -9,7 +9,7 @@ getline(Chan *c, char *line, int size)
char ch;
for (ch = n = 0; n < size-1 && ch != '\n'; ++n) {
- if ((r = devread(c, &ch, 1)) < 0)
+ if ((r = chanread(c, &ch, 1)) < 0)
return -1;
if (r == 0)
break;
diff --git a/src/os9/arch/arm64/main.c b/src/os9/arch/arm64/main.c
@@ -13,7 +13,7 @@ getch(void)
if (!console)
panic("getch out of sync");
- devread(console, &ch, 1);
+ chanread(console, &ch, 1);
return ch;
}
diff --git a/src/os9/dev/dev.c b/src/os9/dev/dev.c
@@ -26,7 +26,7 @@ sameqid(Qid q1, Qid q2)
return q1.type == q2.type && q1.vers == q2.vers && q1.path == q2.path;
}
-Chan *
+static Chan *
newchan(unsigned char type)
{
Chan *c, **bp;
@@ -111,7 +111,31 @@ newfds(Fdset *from)
return fds;
}
-void
+static void
+delchan(Chan *c)
+{
+ Chan **bp;
+ Fdset *fds = proc->fds;
+
+ if (!decref(&c->ref))
+ return;
+
+ lock(&fds->m);
+ for (bp = fds->fdset; bp < &fds->fdset[NR_CHANS]; ++bp) {
+ if (*bp == c)
+ break;
+ }
+ if (bp == &fds->fdset[NR_CHANS])
+ panic("corrupted channel");
+
+ *bp = NULL;
+ unlock(&fds->m);
+ unlock(&c->mutex);
+
+ freechan(c);
+}
+
+static void
delmpoint(Mpoint *mp)
{
if (!decref(&mp->ref))
@@ -153,35 +177,11 @@ delfds(Fdset *fds)
if (!c)
continue;
lock(&c->mutex);
- devclose(c);
+ chanclose(c);
}
freefds(fds);
}
-void
-delchan(Chan *c)
-{
- Chan **bp;
- Fdset *fds = proc->fds;
-
- if (!decref(&c->ref))
- return;
-
- lock(&fds->m);
- for (bp = fds->fdset; bp < &fds->fdset[NR_CHANS]; ++bp) {
- if (*bp == c)
- break;
- }
- if (bp == &fds->fdset[NR_CHANS])
- panic("corrupted channel");
-
- *bp = NULL;
- unlock(&fds->m);
- unlock(&c->mutex);
-
- freechan(c);
-}
-
static Chan *
fd2chan(int fd)
{
@@ -492,7 +492,7 @@ devgen(Chan *c, Dirtab *tab, int ntab, int n, Dir *dir)
}
Chan *
-devopen(char *fname, int mode)
+chanopen(char *fname, int mode)
{
if (!validmode(mode))
return NULL;
@@ -506,7 +506,7 @@ open(char *fname, int mode)
Chan *c, **bp;
Fdset *fds = proc->fds;
- if ((c = devopen(fname, mode)) == NULL)
+ if ((c = chanopen(fname, mode)) == NULL)
return -1;
lock(&fds->m);
@@ -523,6 +523,12 @@ open(char *fname, int mode)
}
int
+chanclose(Chan *c)
+{
+ return devtab[c->type]->close(c);
+}
+
+int
devclose(Chan *c)
{
delchan(c);
@@ -536,7 +542,7 @@ close(int fd)
if ((c = fd2chan(fd)) == NULL)
return -1;
- return devclose(c);
+ return chanclose(c);
}
int
@@ -618,7 +624,7 @@ stat(char *path, void *buf, int n)
}
int
-devread(Chan *c, void *buf, int n)
+chanread(Chan *c, void *buf, int n)
{
int r = -1;
@@ -640,14 +646,14 @@ read(int fd, void *buf, int n)
if ((c = fd2chan(fd)) == NULL)
return -1;
- r = devread(c, buf, n);
+ r = chanread(c, buf, n);
unlock(&c->mutex);
return r;
}
int
-devwrite(Chan *c, void *buf, int n)
+chanwrite(Chan *c, void *buf, int n)
{
int r = -1;
@@ -669,7 +675,7 @@ write(int fd, void *buf, int n)
if ((c = fd2chan(fd)) == NULL)
return -1;
- r = devwrite(c, buf, n);
+ r = chanwrite(c, buf, n);
unlock(&c->mutex);
return r;
diff --git a/src/os9/dev/dev.h b/src/os9/dev/dev.h
@@ -1,7 +1,6 @@
#include <stddef.h>
#define NR_FILES 20
-#define NODEV 255
#define CHDIR 0x80
#define CHFILE 0x00
@@ -29,9 +28,10 @@ struct dev {
int (*write)(Chan *c, void *buf, int n);
int (*seek)(Chan *c, long off, int whence);
int (*sync)(Chan *c, int what);
+ int (*close)(Chan *c);
Chan *(*clone)(Chan *c, Chan *nc);
Chan *(*attach)(int id, int dev);
- Chan *(*mount)(Chan *c, char *spec);
+ Chan *(*mount)(Chan *c, char *spec);
};
struct attr {
@@ -43,8 +43,6 @@ extern int sameqid(Qid q1, Qid qid2);
extern Chan *namec(char *name, int mode);
extern Chan *clone(Chan *c, Chan *nc);
extern Chan *attach(int id, int dev);
-extern Chan *newchan(unsigned char type);
-extern void delchan(Chan *c);
extern int buf2chan(Chan *c,
void *dst, void *src,
int nbytes, long len);
@@ -53,6 +51,7 @@ extern void mkentry(Chan *c, Dir *dir,
long length, Qid qid, unsigned mode);
extern void devlink(void);
+extern int devclose(Chan *);
extern Chan *devattach(int id, int dev);
extern int devseek(Chan *c, long off, int whence);
extern int devsync(Chan *c, int what);
diff --git a/src/os9/dev/devar.c b/src/os9/dev/devar.c
@@ -204,7 +204,7 @@ armount(Chan *c, char *spec)
errno = ENOMEM;
err:
- delchan(cspec);
+ chanclose(cspec);
return NULL;
}
@@ -219,4 +219,5 @@ Dev ardevtab = {
.mount = armount,
.seek = devseek,
.sync = devsync,
+ .close = devclose,
};
diff --git a/src/os9/dev/devblk.c b/src/os9/dev/devblk.c
@@ -296,4 +296,5 @@ Dev blkdevtab = {
.mount = deverrmount,
.seek = devseek,
.sync = blksync,
+ .close = devclose,
};
diff --git a/src/os9/dev/devcons.c b/src/os9/dev/devcons.c
@@ -168,7 +168,7 @@ consdelin(struct conscmd *cmd, char *s)
return -1;
}
- delchan(in);
+ chanclose(in);
in = NULL;
inname[0] = '\0';
return 0;
@@ -181,7 +181,7 @@ consdelout(struct conscmd *cmd, char *s)
for (i = 0; i < CONSOUT; i++) {
if (out[i] && !strcmp(s, outname[i])) {
- delchan(out[i]);
+ chanclose(out[i]);
out[i] = NULL;
outname[i][0] = '\0';
return 0;
@@ -410,4 +410,5 @@ Dev consdevtab = {
.write = conswrite,
.seek = deverrseek,
.sync = devsync,
+ .close = devclose,
};
diff --git a/src/os9/dev/devfip.c b/src/os9/dev/devfip.c
@@ -289,7 +289,7 @@ fipmount(Chan *c, char *spec)
errno = ENOMEM;
err:
- delchan(cspec);
+ chanclose(cspec);
return NULL;
}
@@ -304,4 +304,5 @@ Dev fipdevtab = {
.mount = fipmount,
.seek = devseek,
.sync = devsync,
+ .close = devclose,
};
diff --git a/src/os9/dev/devproc.c b/src/os9/dev/devproc.c
@@ -188,4 +188,5 @@ Dev procdevtab = {
.mount = deverrmount,
.seek = devseek,
.sync = devsync,
+ .close = devclose,
};
diff --git a/src/os9/dev/devroot.c b/src/os9/dev/devroot.c
@@ -123,4 +123,5 @@ Dev rootdevtab = {
.mount = deverrmount,
.seek = devseek,
.sync = devsync,
+ .close = devclose,
};
diff --git a/src/os9/dev/devuart.c b/src/os9/dev/devuart.c
@@ -259,4 +259,5 @@ Dev uartdevtab = {
.mount = deverrmount,
.seek = deverrseek,
.sync = devsync,
+ .close = devclose,
};
diff --git a/src/os9/dlang.c b/src/os9/dlang.c
@@ -89,12 +89,12 @@ do_read(const struct cmd *cmd, struct args *args)
Chan *c;
char buf[100], *bp;
- if ((c = devopen(args->argv[1], O_READ)) == NULL)
+ if ((c = chanopen(args->argv[1], O_READ)) == NULL)
goto err;
ch = -1;
kprint(PREFIX2);
- while ((n = devread(c, buf, sizeof(buf))) > 0) {
+ while ((n = chanread(c, buf, sizeof(buf))) > 0) {
for (bp = buf; bp < &buf[n]; bp++) {
if (ch == '\n')
kprint(PREFIX2);
@@ -105,7 +105,7 @@ do_read(const struct cmd *cmd, struct args *args)
if (ch != '\n')
kprint("\n");
- if (devclose(c) < 0 || n < 0)
+ if (chanclose(c) < 0 || n < 0)
goto err;
return 0;
@@ -123,16 +123,16 @@ do_ls(const struct cmd *cmd, struct args *args)
Dir dentry;
unsigned char buf[DIRLEN];
- if ((c = devopen(args->argv[1], O_READ)) == NULL)
+ if ((c = chanopen(args->argv[1], O_READ)) == NULL)
goto err;
- while ((n = devread(c, buf, DIRLEN)) > 0) {
+ while ((n = chanread(c, buf, DIRLEN)) > 0) {
if (p9todir(&dentry, buf, n) < 0)
goto err;
kprint(PREFIX2 "%s\n", dentry.name);
}
- if (devclose(c) || n < 0)
+ if (chanclose(c) || n < 0)
goto err;
return 0;
@@ -149,7 +149,7 @@ do_write(const struct cmd *cmd, struct args *args)
Chan *c;
char *s, buffer[LINELEN];
- if ((c = devopen(args->argv[1], O_WRITE)) == NULL)
+ if ((c = chanopen(args->argv[1], O_WRITE)) == NULL)
goto err;
offset = 0;
@@ -163,9 +163,9 @@ do_write(const struct cmd *cmd, struct args *args)
buffer[offset++] = ' ';
}
buffer[offset] = '\n';
- n = devwrite(c, buffer, offset);
+ n = chanwrite(c, buffer, offset);
- if (devclose(c) < 0 || n < 0)
+ if (chanclose(c) < 0 || n < 0)
goto err;
return 0;
diff --git a/src/os9/hosted/main.c b/src/os9/hosted/main.c
@@ -19,7 +19,7 @@ getch(void)
char ch;
if (console) {
- devread(console, &ch, 1);
+ chanread(console, &ch, 1);
} else {
_read(0, &ch, 1);
}
@@ -33,7 +33,7 @@ putch(int c)
char ch = c;
if (console) {
- devwrite(console, &ch, 1);
+ chanwrite(console, &ch, 1);
} else {
_write(1, &ch, 1);
}
diff --git a/src/os9/sys.c b/src/os9/sys.c
@@ -12,7 +12,7 @@ Chan *console;
static void
icons(void)
{
- if ((console = devopen("#c/raw", O_RDWR)) == NULL)
+ if ((console = chanopen("#c/raw", O_RDWR)) == NULL)
panic("icons:#c/raw open");
}
@@ -29,11 +29,11 @@ writeline(char *line)
*p++ = '\0';
data = p;
- if ((c = devopen(file, O_WRITE)) < 0)
+ if ((c = chanopen(file, O_WRITE)) < 0)
panic("writeline:open");
- if (devwrite(c, data, strlen(data)) < 0)
+ if (chanwrite(c, data, strlen(data)) < 0)
panic("writeline:write");
- devclose(c);
+ chanclose(c);
}
static void
@@ -69,7 +69,7 @@ iconf(void)
Chan *c;
char line[80], *s, *p;
- if ((c = devopen("/blobs/conf", O_READ)) == NULL) {
+ if ((c = chanopen("/blobs/conf", O_READ)) == NULL) {
kprint("missed kernel namespace configuration\n");
return;
}
@@ -101,7 +101,7 @@ iconf(void)
if (n < 0)
kprint("iconf: error reading configuration:%r\n");
- devclose(c);
+ chanclose(c);
}
static void