123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- #include <types.h>
- #include <kern/errno.h>
- #include <kern/fcntl.h>
- #include <stat.h>
- #include <lib.h>
- #include <uio.h>
- #include <synch.h>
- #include <vnode.h>
- #include <device.h>
- static
- int
- dev_open(struct vnode *v, int flags)
- {
- struct device *d = v->vn_data;
- if (flags & (O_CREAT | O_TRUNC | O_EXCL | O_APPEND)) {
- return EINVAL;
- }
- return d->d_open(d, flags);
- }
- static
- int
- dev_close(struct vnode *v)
- {
- struct device *d = v->vn_data;
- return d->d_close(d);
- }
- static
- int
- dev_reclaim(struct vnode *v)
- {
- (void)v;
-
- return 0;
- }
- static
- int
- dev_read(struct vnode *v, struct uio *uio)
- {
- struct device *d = v->vn_data;
- KASSERT(uio->uio_rw == UIO_READ);
- return d->d_io(d, uio);
- }
- static
- int
- null_io(struct vnode *v, struct uio *uio)
- {
- (void)v;
- (void)uio;
- return EINVAL;
- }
- static
- int
- dev_write(struct vnode *v, struct uio *uio)
- {
- struct device *d = v->vn_data;
- KASSERT(uio->uio_rw == UIO_WRITE);
- return d->d_io(d, uio);
- }
- static
- int
- dev_ioctl(struct vnode *v, int op, userptr_t data)
- {
- struct device *d = v->vn_data;
- return d->d_ioctl(d, op, data);
- }
- static
- int
- dev_stat(struct vnode *v, struct stat *statbuf)
- {
- struct device *d = v->vn_data;
- int result;
- bzero(statbuf, sizeof(struct stat));
- if (d->d_blocks > 0) {
- statbuf->st_size = d->d_blocks * d->d_blocksize;
- statbuf->st_blksize = d->d_blocksize;
- }
- else {
- statbuf->st_size = 0;
- }
- result = VOP_GETTYPE(v, &statbuf->st_mode);
- if (result) {
- return result;
- }
-
- statbuf->st_mode |= 0600;
- statbuf->st_nlink = 1;
- statbuf->st_blocks = d->d_blocks;
-
- statbuf->st_dev = 0;
-
- statbuf->st_rdev = d->d_devnumber;
- return 0;
- }
- static
- int
- dev_gettype(struct vnode *v, mode_t *ret)
- {
- struct device *d = v->vn_data;
- if (d->d_blocks > 0) {
- *ret = S_IFBLK;
- }
- else {
- *ret = S_IFCHR;
- }
- return 0;
- }
- static
- int
- dev_tryseek(struct vnode *v, off_t pos)
- {
- struct device *d = v->vn_data;
- if (d->d_blocks > 0) {
- if ((pos % d->d_blocksize)!=0) {
-
- return EINVAL;
- }
- if (pos < 0) {
-
- return EINVAL;
- }
- if (pos / d->d_blocksize >= d->d_blocks) {
-
- return EINVAL;
- }
- }
- else {
- return ESPIPE;
- }
- return 0;
- }
- static
- int
- null_fsync(struct vnode *v)
- {
- (void)v;
- return 0;
- }
- static
- int
- dev_mmap(struct vnode *v )
- {
- (void)v;
- return EUNIMP;
- }
- static
- int
- dev_truncate(struct vnode *v, off_t len)
- {
- struct device *d = v->vn_data;
-
- if (d->d_blocks > 0 && (off_t)(d->d_blocks*d->d_blocksize) == len) {
- return 0;
- }
- return EINVAL;
- }
- static
- int
- dev_namefile(struct vnode *v, struct uio *uio)
- {
-
- (void)v;
- (void)uio;
- return 0;
- }
- static
- int
- null_creat(struct vnode *v, const char *name, bool excl, mode_t mode,
- struct vnode **result)
- {
- (void)v;
- (void)name;
- (void)excl;
- (void)mode;
- (void)result;
- return ENOTDIR;
- }
- static
- int
- null_mkdir(struct vnode *v, const char *name, mode_t mode)
- {
- (void)v;
- (void)name;
- (void)mode;
- return ENOTDIR;
- }
- static
- int
- null_symlink(struct vnode *v, const char *contents, const char *name)
- {
- (void)v;
- (void)contents;
- (void)name;
- return ENOTDIR;
- }
- static
- int
- null_nameop(struct vnode *v, const char *name)
- {
- (void)v;
- (void)name;
- return ENOTDIR;
- }
- static
- int
- null_link(struct vnode *v, const char *name, struct vnode *file)
- {
- (void)v;
- (void)name;
- (void)file;
- return ENOTDIR;
- }
- static
- int
- null_rename(struct vnode *v, const char *n1, struct vnode *v2, const char *n2)
- {
- (void)v;
- (void)n1;
- (void)v2;
- (void)n2;
- return ENOTDIR;
- }
- static
- int
- dev_lookup(struct vnode *dir,
- char *pathname, struct vnode **result)
- {
-
- if (strlen(pathname)>0) {
- return ENOENT;
- }
- VOP_INCREF(dir);
- *result = dir;
- return 0;
- }
- static
- int
- dev_lookparent(struct vnode *dir,
- char *pathname, struct vnode **result,
- char *namebuf, size_t buflen)
- {
-
- (void)dir;
- (void)pathname;
- (void)result;
- (void)namebuf;
- (void)buflen;
- return ENOTDIR;
- }
- static const struct vnode_ops dev_vnode_ops = {
- VOP_MAGIC,
- dev_open,
- dev_close,
- dev_reclaim,
- dev_read,
- null_io,
- null_io,
- dev_write,
- dev_ioctl,
- dev_stat,
- dev_gettype,
- dev_tryseek,
- null_fsync,
- dev_mmap,
- dev_truncate,
- dev_namefile,
- null_creat,
- null_symlink,
- null_mkdir,
- null_link,
- null_nameop,
- null_nameop,
- null_rename,
- dev_lookup,
- dev_lookparent,
- };
- struct vnode *
- dev_create_vnode(struct device *dev)
- {
- int result;
- struct vnode *v;
- v = kmalloc(sizeof(struct vnode));
- if (v==NULL) {
- return NULL;
- }
- result = VOP_INIT(v, &dev_vnode_ops, NULL, dev);
- if (result != 0) {
- panic("While creating vnode for device: VOP_INIT: %s\n",
- strerror(result));
- }
- return v;
- }
|