9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 4ecb8231adc2587faeaa35718f26906c6eaabd9d
parent 1b28b899cb089e23b2c776b2922ad19a7e2ae71d
Author: Roberto Vargas <roberto.vargas@arm.com>
Date:   Thu, 28 Feb 2019 16:40:43 +0000

[dev] Move bind to dev.c

It is easy to implement bind dev.c and having the functionality
for all the devs without problems.

Change-Id: Ida7d855cf0b3175893701e7cbc86983f287a65f5

Diffstat:
Mdrivers/dev.c | 84++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
Mdrivers/dev.h | 3---
Mdrivers/devblob.c | 1-
Mdrivers/devroot.c | 38+-------------------------------------
Mdrivers/devuart.c | 1-
5 files changed, 59 insertions(+), 68 deletions(-)

diff --git a/drivers/dev.c b/drivers/dev.c @@ -8,9 +8,17 @@ #include "dev.h" +#define NR_MPOINTS 4 + +struct mpoint { + Chan *where; + Chan *new; +}; + static Chan fdset[NR_CHANS]; static Chan slash; static mutex_t chanlock; +static struct mpoint mpoints[NR_MPOINTS]; static Chan * newchan(unsigned char type) @@ -118,12 +126,44 @@ chanclose(Chan *c) c->type = NODEV; } +static Chan * +mntpoint(Chan *c) +{ + Chan *cw; + struct mpoint *mp; + + for (mp = mpoints; mp < &mpoints[NR_MPOINTS]; mp++) { + if ((cw = mp->where) == NULL) + continue; + if (cw->type == c->type && cw->qid == c->qid) + return mp->new; + } + + return NULL; +} + +static Chan * +devattach(int id, int dev) +{ + Chan *c; + int type; + + if ((type = devtype(id)) < 0) + return NULL; + if ((c = newchan(type)) == NULL) + return NULL; + c->dev = dev; + c->qid = CHDIR; + + return c; +} + Chan * namec(const char *name, int mode) { int type, n, i; const char *s; - Chan *c; + Chan *bp, *c; char elem[NAMELEN]; switch (name[0]) { @@ -163,6 +203,9 @@ namec(const char *name, int mode) goto notfound; if (devtab[c->type]->walk(c, elem) < 0) goto notfound; + bp = mntpoint(c); + if (bp) + clone(bp, c); } if (!s) goto notfound; @@ -191,22 +234,6 @@ devclone(Chan *c, Chan *nc) return nc; } -Chan * -devattach(int id, int dev) -{ - Chan *c; - int type; - - if ((type = devtype(id)) < 0) - return NULL; - if ((c = newchan(type)) == NULL) - return NULL; - c->dev = dev; - c->qid = CHDIR; - - return c; -} - int devwalk(Chan *c, const char *name, const Dirtab *tab, int ntab, Devgen *gen) { @@ -369,15 +396,10 @@ seek(int fd, long long off, int whence) } int -devbind(Chan *new, Chan *where) -{ - errno = EINVAL; - return -1; -} - -int bind(char *new, char *where) { + int i; + struct mpoint *mp; Chan *cw, *cn; if ((cw = namec(where, O_READ)) == NULL) @@ -391,9 +413,19 @@ bind(char *new, char *where) if ((cn = namec(new, O_BIND)) == NULL) goto err1; - if (devtab[cw->type]->bind(cn, cw) < 0) + for (i = NR_MPOINTS-1; i >= 0; i--) { + mp = &mpoints[i]; + if (!mp->where) + break; + } + + if (i < 0) { + errno = ENOMEM; goto err2; - chanclose(cw); + } + + mp->where = cw; + mp->new = cn; return 0; diff --git a/drivers/dev.h b/drivers/dev.h @@ -26,7 +26,6 @@ struct dev { int (*walk)(Chan *c, const char *name); int (*read)(Chan *c, void *buf, int n); int (*write)(Chan *c, void *buf, int n); - int (*bind)(Chan *cw, Chan *cn); }; struct chan { @@ -43,10 +42,8 @@ struct attr { }; extern Chan *devclone(Chan *c, Chan *nc); -extern Chan *devattach(int id, int dev); extern Chan *devclone(Chan *c, Chan *nc); extern void devlink(void); -extern int devbind(Chan *new, Chan *where); extern int devwrite(Chan *c, void *buf, int n); extern void chanclose(Chan *c); extern int devgen(Chan *c, diff --git a/drivers/devblob.c b/drivers/devblob.c @@ -150,5 +150,4 @@ const Dev blobdevtab = { .walk = blobwalk, .read = blobread, .write = blobwrite, - .bind = devbind, }; diff --git a/drivers/devroot.c b/drivers/devroot.c @@ -22,8 +22,6 @@ static const Dirtab devfstab[] = { {"uart", CHDIR | Qdevuart, 0, O_READ}, }; -static Chan *bindpoint[Qmax]; - static int rootgen(Chan *c, const Dirtab *tab, int ntab, int n, Dir *dir) { @@ -50,40 +48,7 @@ rootgen(Chan *c, const Dirtab *tab, int ntab, int n, Dir *dir) static int rootwalk(Chan *c, const char *name) { - Qid qid; - Chan *mnt; - - if (devwalk(c, name, NULL, 0, rootgen) < 0) - return -1; - - qid = c->qid & ~CHDIR; - if (qid >= Qmax) - panic("rootwalk"); - - mnt = bindpoint[qid]; - if (mnt != NULL) - *c = *mnt; - - return 1; -} - -static int -rootbind(Chan *cn, Chan *cw) -{ - Qid qid; - Chan *mnt; - - qid = cw->qid & ~CHDIR; - if (qid >= Qmax) - panic("rootbind"); - - mnt = bindpoint[qid]; - if (mnt) - chanclose(mnt); - - bindpoint[qid] = cn; - - return 0; + return devwalk(c, name, NULL, 0, rootgen); } static int @@ -99,5 +64,4 @@ const Dev rootdevtab = { .walk = rootwalk, .read = rootread, .write = devwrite, - .bind = rootbind, }; diff --git a/drivers/devuart.c b/drivers/devuart.c @@ -267,5 +267,4 @@ const Dev uartdevtab = { .clone = devclone, .read = uartread, .write = uartwrite, - .bind = devbind, };