diff options
author | julian <julian@FreeBSD.org> | 1995-04-20 03:31:34 +0000 |
---|---|---|
committer | julian <julian@FreeBSD.org> | 1995-04-20 03:31:34 +0000 |
commit | 52ea19b9e28a056df29a693ccae2d7b8cdc058c1 (patch) | |
tree | 08c2fd96f9e11046a9ad834e25b601cb89ee5dc3 | |
parent | 266ce25fcbbdf2996fd32fc626b3cccf02ccff2f (diff) | |
download | FreeBSD-src-52ea19b9e28a056df29a693ccae2d7b8cdc058c1.zip FreeBSD-src-52ea19b9e28a056df29a693ccae2d7b8cdc058c1.tar.gz |
Reviewed by: no-one yet, but nonintrusive until configed in.. :)
Submitted by: julian@freebsd.org
Obtained from: written from scratch
-rw-r--r-- | sys/miscfs/devfs/devfs_back.c | 500 | ||||
-rw-r--r-- | sys/miscfs/devfs/devfs_front.c | 481 | ||||
-rw-r--r-- | sys/miscfs/devfs/devfs_proto.h | 31 | ||||
-rw-r--r-- | sys/miscfs/devfs/devfs_vfsops.c | 234 | ||||
-rw-r--r-- | sys/miscfs/devfs/devfs_vnops.c | 1362 | ||||
-rw-r--r-- | sys/miscfs/devfs/devfsdefs.h | 195 | ||||
-rw-r--r-- | sys/miscfs/devfs/reproto.sh | 2 |
7 files changed, 2805 insertions, 0 deletions
diff --git a/sys/miscfs/devfs/devfs_back.c b/sys/miscfs/devfs/devfs_back.c new file mode 100644 index 0000000..718f740 --- /dev/null +++ b/sys/miscfs/devfs/devfs_back.c @@ -0,0 +1,500 @@ + +/* + * Written by Julian Elischer (julian@DIALix.oz.au) + * + * $Header: /sys/miscfs/devfs/RCS/devfs_back.c,v 1.3 1995/01/07 04:20:25 root Exp root $ + */ + +#include "param.h" +#include "systm.h" +#include "types.h" +#include "kernel.h" +#include "file.h" /* define FWRITE ... */ +#include "conf.h" +#include "stat.h" +#include "mount.h" +#include "vnode.h" +#include "malloc.h" +#include "dir.h" /* defines dirent structure */ +#include "devfsdefs.h" + + + +devb_p dev_root; /* root of the backing tree */ +int devfs_set_up = 0; /* note tha we HAVE set up the backing tree */ + +/* + * Set up the root directory node in the backing plane + * This is happenning before the vfs system has been + * set up yet, so be careful about what we reference.. + * Notice that the ops are by indirection.. as they haven't + * been set up yet! + */ +void devfs_back_init() /*proto*/ +{ + + devb_p devbp; + dn_p dnp; + /* + * This may be called several times.. only do it if it needs + * to be done. + */ + if(!devfs_set_up) + { + /* + * Allocate and fill out a new backing node + */ + if(!(devbp = (devb_p)malloc(sizeof(devb_t), + M_DEVFSBACK, M_NOWAIT))) + { + return ; + } + bzero(devbp,sizeof(devb_t)); + /* + * And the devnode associated with it + */ + if(!(dnp = (dn_p)malloc(sizeof(devnode_t), + M_DEVFSNODE, M_NOWAIT))) + { + free(devbp,M_DEVFSBACK); + return ; + } + bzero(dnp,sizeof(devnode_t)); + /* + * Link the two together + */ + devbp->dnp = dnp; + dnp->links = 1; + /* + * set up the directory node for the root + * and put in all the usual entries for a directory node + */ + dnp->type = DEV_DIR; + dnp->links++; /* for .*/ + /* root loops to self */ + dnp->by.BackDir.parent = dnp; + dnp->links++; /* for ..*/ + /* + * set up the list of children (none so far) + */ + dnp->by.BackDir.dirlist = (devb_p)0; + dnp->by.BackDir.dirlast = + &dnp->by.BackDir.dirlist; + dnp->by.BackDir.myname = devbp; + /* + * set up a pointer to directory type ops + */ + dnp->ops = &devfs_vnodeop_p; + dnp->mode |= 0555; /* default perms */ + /* + * note creation times etc, as now (boot time) + */ + TIMEVAL_TO_TIMESPEC(&time,&(dnp->ctime)) + dnp->mtime = dnp->ctime; + dnp->atime = dnp->ctime; + + /* + * and the list of layers + */ + devbp->fronts = NULL; + devbp->lastfront = &(devbp->fronts); + + + /* + * next time, we don't need to do all this + */ + dev_root = devbp; + devfs_set_up = 1; + } +} + +/***********************************************************************\ +* Given a starting node (0 for root) and a pathname, return the node * +* for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE' * +* option is true, then create any missing nodes in the path and create * +* and return the final node as well. * +* Generally, this MUST be the first function called by any module * +* as it also calls the initial setup code, in case it has never been * +* done yet. * +* This is used to set up a directory, before making nodes in it.. * +* * +* Warning: This function is RECURSIVE. * +* char *path, find this dir (err if not dir) * +* dn_p dirnode, starting point (0 = root) * +* int create, create path if not found * +* dn_p *dn_pp) where to return the node of the dir * +\***********************************************************************/ +int dev_finddir(char *orig_path, dn_p dirnode, int create, dn_p *dn_pp) /*proto*/ +{ + devb_p devbp; + char pathbuf[DEVMAXPATHSIZE]; + char *path; + char *name; + register char *cp; + int retval; + + + DBPRINT(("dev_finddir\n")); + devfs_back_init(); /* in case we are the first */ + if(!dirnode) dirnode = dev_root->dnp; + if(dirnode->type != DEV_DIR) return ENOTDIR; + if(strlen(orig_path) > (DEVMAXPATHSIZE - 1)) return ENAMETOOLONG; + path = pathbuf; + strcpy(path,orig_path); + while(*path == '/') path++; /* always absolute, skip leading / */ + /***************************************\ + * find the next segment of the name * + \***************************************/ + cp = name = path; + while((*cp != '/') && (*cp != 0)) + { + cp++; + } + /***********************************************\ + * Check to see if it's the last component * + \***********************************************/ + if(*cp) + { + path = cp + 1; /* path refers to the rest */ + *cp = 0; /* name is now a separate string */ + if(!(*path)) + { + path = (char *)0; /* was trailing slash */ + } + } + else + { + path = (char *)0; /* no more to do */ + } + + /***************************************\ + * Start scanning along the linked list * + \***************************************/ + devbp = dirnode->by.BackDir.dirlist; + while(devbp && strcmp(devbp->name,name)) + { + devbp = devbp->next; + } + if(devbp) + { /* check it's a directory */ + if(devbp->dnp->type != DEV_DIR) return ENOTDIR; + } + else + { + /***************************************\ + * The required element does not exist * + * So we will add it if asked to. * + \***************************************/ + if(!create) return ENOENT; + + if(retval = dev_add_node(name, dirnode ,DEV_DIR, + NULL, &devbp)) + { + return retval; + } + } + if(path) /* decide whether to recurse more or return */ + { + return (dev_finddir(path,devbp->dnp,create,dn_pp)); + } + else + { + *dn_pp = devbp->dnp; + return 0; + } +} + +/***********************************************************************\ +* Add a new element to the devfs backing structure. * +\***********************************************************************/ +int dev_add_node(char *name, dn_p dirnode, int entrytype, union typeinfo *by, devb_p *devb_pp) /*proto*/ +{ + devb_p devbp; + devb_p realthing; /* needed to create an alias */ + dn_p dnp; + int retval; + + DBPRINT(("dev_add_node\n")); + if(dirnode->type != DEV_DIR) return(ENOTDIR); + if(strlen(name) > (DEVMAXNAMESIZE - 1)) return (ENAMETOOLONG); + + retval = dev_finddir(name,dirnode,0,&dnp); /*don't create!*/ + dnp = NULL; /*just want the return code..*/ + if(retval != ENOENT) /* only acceptable answer */ + return(EEXIST); + /* + * Allocate and fill out a new backing node + */ + if(!(devbp = (devb_p)malloc(sizeof(devb_t), + M_DEVFSBACK, M_NOWAIT))) + { + return ENOMEM; + } + bzero(devbp,sizeof(devb_t)); + if(!(dnp = (dn_p)malloc(sizeof(devnode_t), + M_DEVFSNODE, M_NOWAIT))) + { + free(devbp,M_DEVFSBACK); + return ENOMEM; + } + bzero(dnp,sizeof(devnode_t)); + devbp->dnp = dnp; + dnp->links = 1; /* implicit from our own name-node */ + + /* + * note the node type we are adding + * and set the creation times to NOW + * put in it's name + * include the implicit link in the count of links to the devnode.. + * this stops it from being accidentally freed later. + */ + strcpy(devbp->name,name); + dnp->type = entrytype; + TIMEVAL_TO_TIMESPEC(&time,&(dnp->ctime)) + dnp->mtime = dnp->ctime; + dnp->atime = dnp->ctime; + + /* + * And set up a new 'clones' list (empty) + */ + devbp->lastfront = &(devbp->fronts); + + /* + * Put it on the END of the linked list of directory entries + */ + devbp->parent = dirnode; + devbp->prevp = dirnode->by.BackDir.dirlast; + devbp->next = *(devbp->prevp); /* should be NULL */ /*right?*/ + *(devbp->prevp) = devbp; + dirnode->by.BackDir.dirlast = &(devbp->next); + dirnode->by.BackDir.entrycount++; + + /* + * return the answer + */ + switch(entrytype) { + case DEV_DIR: + /* + * As it's a directory, make sure it has a null entries list + */ + dnp->by.BackDir.dirlast = + &(dnp->by.BackDir.dirlist); + dnp->by.BackDir.dirlist = (devb_p)0; + dnp->by.BackDir.parent = (dn_p)dirnode; + dnp->by.BackDir.myname = devbp; + /* + * make sure that the ops associated with it are the ops + * that we use (by default) for directories + */ + dnp->ops = &devfs_vnodeop_p; + dnp->mode |= 0555; /* default perms */ + break; + case DEV_BDEV: + /* + * Make sure it has DEVICE type ops + * and device specific fields are correct + */ + dnp->ops = &dev_spec_vnodeop_p; + dnp->by.Bdev.bdevsw = by->Bdev.bdevsw; + dnp->by.Bdev.dev = by->Bdev.dev; + break; + case DEV_CDEV: + /* + * Make sure it has DEVICE type ops + * and device specific fields are correct + */ + dnp->ops = &dev_spec_vnodeop_p; + dnp->by.Cdev.cdevsw = by->Cdev.cdevsw; + dnp->by.Cdev.dev = by->Cdev.dev; + break; + case DEV_DDEV: + /* + * store the address of (the address of) the ops + * and the magic cookie to use with them + */ + dnp->by.Ddev.arg = by->Ddev.arg; + dnp->ops = by->Ddev.ops; + break; + + + case DEV_ALIAS: + /* + * point to the node we want to shadow + * Also store the fact we exist so that aliases + * can be deleted accuratly when the original node + * is deleted.. (i.e. when device is removed) + */ + realthing = by->Alias.realthing; + dnp->by.Alias.realthing = realthing; + dnp->by.Alias.next = realthing->aliases; + realthing->aliases = devbp; + realthing->alias_count++; + break; + } + + if(retval = devfs_add_fronts(dirnode->by.BackDir.myname/*XXX*/,devbp)) + { + /*XXX*//* no idea what to do if it fails... */ + return retval; + } + + *devb_pp = devbp; + return 0 ; +} + +/*********************************************************************** + * remove all fronts to this dev and also it's aliases, + * Then remove this node. + * For now only allow DEVICE nodes to go.. XXX + * directory nodes are more complicated and may need more work.. + */ +int dev_remove(devb_p devbp) /*proto*/ +{ + devb_p alias; + + DBPRINT(("dev_remove\n")); + /* + * Check the type of the node.. for now don't allow dirs + */ + switch(devbp->dnp->type) + { + case DEV_BDEV: + case DEV_CDEV: + case DEV_DDEV: + case DEV_ALIAS: + case DEV_SLNK: + break; + case DEV_DIR: + default: + return(EINVAL); + } + /* + * Free each alias + */ + while ( devbp->alias_count) + { + alias = devbp->aliases; + devbp->aliases = alias->dnp->by.Alias.next; + devbp->alias_count--; + devfs_dn_free(alias->dnp); + free (alias, M_DEVFSBACK); + } + /* + * Now remove front items of the Main node itself + */ + devfs_remove_fronts(devbp); + + /* + * now we should free the main node + */ + devfs_dn_free(devbp->dnp); + free (devbp, M_DEVFSBACK); + return 0; +} + +int dev_touch(devb_p key) /* update the node for this dev */ /*proto*/ +{ + DBPRINT(("dev_touch\n")); + TIMEVAL_TO_TIMESPEC(&time,&(key->dnp->mtime)) + return 0; /*XXX*/ +} + +void devfs_dn_free(dn_p dnp) /*proto*/ +{ + if(dnp->links <= 0) + { + printf("devfs node reference count bogus\n"); + Debugger("devfs_dn_free"); + return; + } + if(--dnp->links == 0 ) + { + devfs_dropvnode(dnp); + free (dnp, M_DEVFSNODE); + } +} +/***********************************************************************\ +* UTILITY routine: * +* Return the major number for the cdevsw entry containing the given * +* address. * +\***********************************************************************/ +int get_cdev_major_num(caddr_t addr) /*proto*/ +{ + int index = 0; + + DBPRINT(("get_cdev_major_num\n")); + while (index < nchrdev) + { + if(((caddr_t)(cdevsw[index].d_open) == addr) + ||((caddr_t)(cdevsw[index].d_read) == addr) + ||((caddr_t)(cdevsw[index].d_ioctl) == addr)) + { + return index; + } + index++; + } + return -1; +} + +int get_bdev_major_num(caddr_t addr) /*proto*/ +{ + int index = 0; + + DBPRINT(("get_bdev_major_num\n")); + while (index < nblkdev) + { + if(((caddr_t)(bdevsw[index].d_open) == addr) + ||((caddr_t)(bdevsw[index].d_strategy) == addr) + ||((caddr_t)(bdevsw[index].d_ioctl) == addr)) + { + return index; + } + index++; + } + return -1; +} + +/***********************************************************************\ +* Add the named device entry into the given directory, and make it * +* The appropriate type... (called (sometimes indirectly) by drivers..) * +\***********************************************************************/ +devb_p dev_add(char *path,char *name,caddr_t funct,int minor,int chrblk,uid_t uid,gid_t gid, int perms) /*proto*/ +{ + devb_p new_dev; + dn_p dnp; /* devnode for parent directory */ + int retval; + int major ; + union typeinfo by; + + DBPRINT(("dev_add\n")); + retval = dev_finddir(path,NULL,1,&dnp); + if (retval) return 0; + switch(chrblk) + { + case 0: + major = get_cdev_major_num(funct); + by.Cdev.cdevsw = cdevsw + major; + by.Cdev.dev = makedev(major, minor); + if( dev_add_node(name, dnp, DEV_CDEV, + &by,&new_dev)) + return 0; + break; + case 1: + major = get_bdev_major_num(funct); + by.Bdev.bdevsw = bdevsw + major; + by.Bdev.dev = makedev(major, minor); + if( dev_add_node(name, dnp, DEV_BDEV, + &by, &new_dev)) + return 0; + break; + default: + return(0); + } + new_dev->dnp->gid = gid; + new_dev->dnp->uid = uid; + new_dev->dnp->mode |= perms; + return new_dev; +} + + + diff --git a/sys/miscfs/devfs/devfs_front.c b/sys/miscfs/devfs/devfs_front.c new file mode 100644 index 0000000..ce2f7d7 --- /dev/null +++ b/sys/miscfs/devfs/devfs_front.c @@ -0,0 +1,481 @@ +/* + * Written by Julian Elischer (julian@DIALix.oz.au) + * + * $Header: /sys/miscfs/devfs/RCS/devfs_front.c,v 1.3 1995/01/07 04:20:25 root Exp root $ + * + */ + +#include "param.h" +#include "systm.h" +#include "kernel.h" +#include "file.h" /* define FWRITE ... */ +#include "conf.h" +#include "stat.h" +#include "mount.h" +#include "vnode.h" +#include "malloc.h" +#include "dir.h" /* defines dirent structure */ +#include "devfsdefs.h" + + + +/***********************************************************************\ +* Given a directory backing node, and a child backing node, add the * +* appropriate front nodes to the front nodes of the directory to * +* represent the child node to the user * +* * +* on failure, front nodes will either be correct or not exist for each * +* front dir, however dirs completed will not be stripped of completed * +* frontnodes on failure of a later parent frontnode * +* * +\***********************************************************************/ +int devfs_add_fronts(devb_p parent,devb_p child) /*proto*/ +{ + devf_p newfp; + devf_p falias; + + DBPRINT((" devfs_add_fronts\n")); + /***********************************************\ + * Find the frontnodes of the parent node * + \***********************************************/ + for (falias = parent->fronts; falias; falias = falias->next_front) + { + if(dev_findfront(falias->dnp,child->name)) + { + printf("Device %s not created, already exists\n", + child->name); + continue; + } + if( dev_mk_front(falias->dnp,child,&newfp,NULL)) + { + printf("Device %s: allocation failed\n", + child->name); + continue; + } + + } + return(0); /* for now always succeed */ +} + +/***************************************************************\ +* Search down the linked list off a front dir to find "name" * +* return the dn_p for that node. +\***************************************************************/ +dn_p dev_findfront(dn_p dir,char *name) /*proto*/ +{ + devf_p newfp; + DBPRINT((" dev_findfront(%s)\n",name)); + if(dir->type != DEV_DIR) return 0;/*XXX*/ /* printf?*/ + + if(name[0] == '.') + { + if(name[1] == 0) + { + return dir; + } + if((name[1] == '.') && (name[2] == 0)) + { + if(dir->by.Dir.parent == dir) /* root? */ + return dir; + else + return dir->by.Dir.parent; + } + } + newfp = dir->by.Dir.dirlist; + while(newfp) + { + if(!(strcmp(name,newfp->name))) + break; + newfp = newfp->next; + } + if(newfp) + return newfp->dnp; + else + return (dn_p)0; +} + +/***************************************************************\ +* Create and link in a new front element.. * +* Parent can be 0 for a root node * +* Not presently usable to make a symlink XXX * +* Must teach this to handle where there is no back node * +* maybe split into two bits? * +\***************************************************************/ +int dev_mk_front(dn_p parent,devb_p back,devf_p *devf_pp , struct devfsmount *dvm) /*proto*/ +{ + devf_p newfp; + struct devfsmount *dmt; + devb_p newback; + devf_p newfront; + int error; + + DBPRINT((" dev_mk_front\n")); + if(parent && (parent->type != DEV_DIR)) return EINVAL; + /*XXX*/ /* printf?*/ + if(!(newfp = malloc(sizeof(*newfp),M_DEVFSFRONT,M_NOWAIT))) + { + return(ENOMEM); + } + bzero(newfp,sizeof(*newfp)); + strcpy(newfp->name,back->name); + + /*******************************************************\ + * If we are creating an alias, Then we need to find the * + * real object's file_node. (It must pre-exist) * + * this means that aliases have no front nodes... * + * In effect ALIAS back nodes are just place markers * + * Check the removal code for this! XXX * + \*******************************************************/ + if(back->dnp->type == DEV_ALIAS) + { + back = back->dnp->by.Alias.realthing; + } + + /* + * either use the existing devnode or make our own, + * depending on if we are a dev or a dir. + */ + switch(back->dnp->type) { + case DEV_BDEV: + case DEV_CDEV: + case DEV_DDEV: + newfp->dnp = back->dnp; + newfp->dnp->links++; /* wherever it is.....*/ + break; + case DEV_DIR: + newfp->dnp = malloc(sizeof(devnode_t), + M_DEVFSNODE,M_NOWAIT); + if(!(newfp->dnp)) + { + free(newfp,M_DEVFSFRONT); + return ENOMEM; + } + /* + * we have two options.. bcopy and reset some items, + * or bzero and reset or copy some items... + */ + bcopy(back->dnp,newfp->dnp,sizeof(devnode_t)); + newfp->dnp->links = 1; /* EXTRA from '.' */ + newfp->dnp->links++; /* wherever it is.....*/ + newfp->dnp->by.Dir.dirlast = + &newfp->dnp->by.Dir.dirlist; + newfp->dnp->by.Dir.dirlist = NULL; + newfp->dnp->by.Dir.entrycount = 0; + newfp->dnp->vn = NULL; + newfp->dnp->vn_id = 0; + break; + case DEV_SLNK: /* should never happen */ + default: + printf("unknown DEV type\n"); + return EINVAL; + } + /*******************************************************\ + * Put it in the parent's directory list (at the end). * + \*******************************************************/ + if(parent) + { + newfp->next = *parent->by.Dir.dirlast; + newfp->prevp = parent->by.Dir.dirlast; + *parent->by.Dir.dirlast = newfp; + parent->by.Dir.dirlast = &newfp->next; + parent->by.Dir.entrycount++; + newfp->dnp->dvm = parent->dvm; /* XXX bad for devs */ + if(back->dnp->type == DEV_DIR) + { + newfp->dnp->by.Dir.parent + = parent; + parent->links++; /* only dirs have '..'*/ + } + parent->len += strlen(newfp->name) + 8;/*ok, ok?*/ + } else { + /* + * it's the root node, put in the dvm + * and link it to itself... + */ + newfp->dnp->by.Dir.parent = newfp->dnp; + newfp->dnp->links++; /* extra for '..'*/ + newfp->dnp->dvm = dvm; + } + + /* + * not accounted for in the link counts.. + * only used to get from the front name entries + * to the total length of the names + * which is stored in the parent's devnode + */ + newfp->parent = parent; /* is NULL for root */ + /*******************************************************\ + * Put it in the appropriate back/front list too. * + \*******************************************************/ + newfp->next_front = *back->lastfront; + newfp->prev_frontp = back->lastfront; + *back->lastfront = newfp; + back->lastfront = &(newfp->next_front); + back->frontcount++; + newfp->realthing = back; + + /* + * If it is a directory, then recurse down all the other + * subnodes in it.... + */ + if ( newfp->dnp->type == DEV_DIR) + { + for(newback = back->dnp->by.BackDir.dirlist; + newback; newback = newback->next) + { + if(error = dev_mk_front(newfp->dnp, + newback, &newfront, NULL)) + { + return error; + } + } + } + *devf_pp = newfp; + return(0); +} + +/* + * duplicate the backing tree into a tree of nodes hung off the + * mount point given as the argument. Do this by + * calling dev_mk_front() which recurses all the way + * up the tree.. + */ +int devfs_make_plane(struct devfsmount *devfs_mp_p) /*proto*/ +{ + devf_p parent; + devf_p new; + devb_p realthing; + int error; + + DBPRINT((" devfs_make_plane\n")); + realthing = dev_root; + if(error = dev_mk_front(0, realthing,&new, devfs_mp_p)) + { + return error; + } + devfs_mp_p->plane_root = new; + + return error; +} + +void devfs_free_plane(struct devfsmount *devfs_mp_p) /*proto*/ +{ + devf_p devfp; + + DBPRINT((" devfs_free_plane\n")); + devfp = devfs_mp_p->plane_root; + if(devfp) dev_free_front(devfp); +} + +/* + * Remove all the front nodes associated with a backing node + */ +void devfs_remove_fronts(devb_p devbp) /*proto*/ +{ + while(devbp->fronts) + { + dev_free_front(devbp->fronts); + } +} +/***************************************************************\ +* Free a front node (and any below it of it's a directory node) * +\***************************************************************/ +void dev_free_front(devf_p devfp) /*proto*/ +{ + dn_p parent = devfp->parent; + devb_p back; + + DBPRINT((" dev_free_front\n")); + if(devfp->dnp->type == DEV_DIR) + { + while(devfp->dnp->by.Dir.dirlist) + { + dev_free_front(devfp->dnp->by.Dir.dirlist); + } + /* + * drop the reference counts on our and our parent's + * nodes for "." and ".." (root has ".." -> "." ) + */ + devfs_dn_free(devfp->dnp); /* account for '.' */ + devfs_dn_free(devfp->dnp->by.Dir.parent); /* and '..' */ + } + /* + * unlink ourselves from the directory on this plane + */ + if(parent) /* if not fs root */ + { + if( *devfp->prevp = devfp->next)/* yes, assign */ + { + devfp->next->prevp = devfp->prevp; + } + else + { + parent->by.Dir.dirlast + = devfp->prevp; + } + parent->by.Dir.entrycount--; + parent->len -= strlen(devfp->name); + } + /* + * If the node has a backing pointer we need to free ourselves + * from that.. + * Remember that we may not HAVE a backing node. + */ + if (back = devfp->realthing) /* yes an assign */ + { + if( *devfp->prev_frontp = devfp->next_front)/* yes, assign */ + { + devfp->next_front->prev_frontp = devfp->prev_frontp; + } + else + { + back->lastfront = devfp->prev_frontp; + } + back->frontcount--; + } + /***************************************************************\ + * If the front node has it's own devnode structure, * + * then free it. * + \***************************************************************/ + devfs_dn_free(devfp->dnp); + free(devfp,M_DEVFSFRONT); + return; +} + +/*******************************************************\ +* Theoretically this could be called for any kind of * +* vnode, however in practice it must be a DEVFS vnode * +\*******************************************************/ +int devfs_vntodn(struct vnode *vn_p, dn_p *dn_pp) /*proto*/ +{ + +DBPRINT((" vntodn ")); + if(vn_p->v_tag != VT_DEVFS) + { + printf("bad-tag "); + Debugger("bad-tag "); + return(EINVAL); + } + if(vn_p->v_usecount == 0) + { + printf("not locked! "); + } + if((vn_p->v_type == VBAD) || (vn_p->v_type == VNON)) + { + printf("bad-type "); + return(EINVAL); + } + *dn_pp = (dn_p)vn_p->v_data; + + return(0); +} + +/***************************************************************\ +* Think about this: * +* Though this routine uses a front node, it also uses a backing * +* node indirectly, via the 'realthing' link. This may prove bad * +* in the case of a user-added slink, where there migh not be a * +* backing node. (e.g. if a slink points out of the fs it CAN'T * +* have a backing node, unlike a hardlink which does..) * +* we are going to have to think very carefully about slinks.. * +\***************************************************************/ +int devfs_dntovn(dn_p front, struct vnode **vn_pp) /*proto*/ +{ + struct vnode *vn_p, *nvp; + int error = 0; + + vn_p = front->vn; +DBPRINT(("dntovn ")); + if( vn_p) + { + if(vn_p->v_id != front->vn_id) + { + printf("bad-id "); + goto skip; + } + if(vn_p->v_tag != VT_DEVFS) + { + printf("bad-tag "); + goto skip; + } + if(vn_p->v_op != *(front->ops)) + { + printf("bad-ops "); + goto skip; + } + if((dn_p)(vn_p->v_data) != front) + { + printf("bad-rev_link "); + goto skip; + } + if(vn_p->v_type != VNON) + { + vget(vn_p,0/*lockflag ?*/); /*XXX*/ + *vn_pp = vn_p; + return(0); + } + else + { + printf("bad-type"); + } +skip: + vn_p = (struct vnode *) 0; + } + if(!(error = getnewvnode(VT_DEVFS, + front->dvm->mount, + *(front->ops), + &vn_p))) + { + front->vn = vn_p; + front->vn_id = vn_p->v_id; + *vn_pp = vn_p; +DBPRINT(("(New vnode)")); + switch(front->type) + { + case DEV_SLNK: + break; + case DEV_DIR: + if(front->by.Dir.parent == front) + { + vn_p->v_flag |= VROOT; + } + vn_p->v_type = VDIR; + break; + case DEV_BDEV: + vn_p->v_type = VBLK; + if (nvp = checkalias(vn_p, + front->by.Bdev.dev, + (struct mount *)0)) + { + vput(vn_p); + vn_p = nvp; + } + break; + case DEV_CDEV: + vn_p->v_type = VCHR; + if (nvp = checkalias(vn_p, + front->by.Cdev.dev, + (struct mount *)0)) + { + vput(vn_p); + vn_p = nvp; + } + break; + case DEV_DDEV: + break; + } + if ( vn_p) + { + vn_p->v_mount = front->dvm->mount;/* Duplicated */ + *vn_pp = vn_p; + vn_p->v_data = (void *)front; +/*XXX*/ /* maybe not.. I mean what if it's a dev... (vnode at back)*/ + } + else + { + error = EINVAL; + } + } + return(error); +} + diff --git a/sys/miscfs/devfs/devfs_proto.h b/sys/miscfs/devfs/devfs_proto.h new file mode 100644 index 0000000..6a08657 --- /dev/null +++ b/sys/miscfs/devfs/devfs_proto.h @@ -0,0 +1,31 @@ +void devfs_back_init() /*proto*/; +int dev_finddir(char *orig_path, dn_p dirnode, int create, dn_p *dn_pp) /*proto*/; +int dev_add_node(char *name, dn_p dirnode, int entrytype, union typeinfo *by, devb_p *devb_pp) /*proto*/; +int dev_remove(devb_p devbp) /*proto*/; +int dev_touch(devb_p key) /* update the node for this dev */ /*proto*/; +void devfs_dn_free(dn_p dnp) /*proto*/; +int get_cdev_major_num(caddr_t addr) /*proto*/; +int get_bdev_major_num(caddr_t addr) /*proto*/; +devb_p dev_add(char *path,char *name,caddr_t funct,int minor,int chrblk,uid_t uid,gid_t gid, int perms) /*proto*/; +int devfs_add_fronts(devb_p parent,devb_p child) /*proto*/; +dn_p dev_findfront(dn_p dir,char *name) /*proto*/; +int dev_mk_front(dn_p parent,devb_p back,devf_p *devf_pp , struct devfsmount *dvm) /*proto*/; +int devfs_make_plane(struct devfsmount *devfs_mp_p) /*proto*/; +void devfs_free_plane(struct devfsmount *devfs_mp_p) /*proto*/; +void devfs_remove_fronts(devb_p devbp) /*proto*/; +void dev_free_front(devf_p devfp) /*proto*/; +int devfs_vntodn(struct vnode *vn_p, dn_p *dn_pp) /*proto*/; +int devfs_dntovn(dn_p front, struct vnode **vn_pp) /*proto*/; +int devfs_init(void) /*proto*/; +int devfs_mount( struct mount *mp, char *path, caddr_t data, struct nameidata *ndp, struct proc *p) /*proto*/; +int mountdevfs( struct mount *mp, struct proc *p) /*proto*/; +int devfs_start(struct mount *mp, int flags, struct proc *p) /*proto*/; +int devfs_unmount( struct mount *mp, int mntflags, struct proc *p) /*proto*/; +int devfs_root(struct mount *mp, struct vnode **vpp) /*proto*/; +int devfs_quotactl( struct mount *mp, int cmds, uid_t uid, caddr_t arg, struct proc *p) /*proto*/; +int devfs_statfs( struct mount *mp, struct statfs *sbp, struct proc *p) /*proto*/; +int devfs_sync(struct mount *mp, int waitfor,struct ucred *cred,struct proc *p) /*proto*/; +int devfs_vget(struct mount *mp, ino_t ino,struct vnode **vpp) /*proto*/; +int devfs_fhtovp (struct mount *mp, struct fid *fhp, struct mbuf *nam, struct vnode **vpp, int *exflagsp, struct ucred **credanonp) /*proto*/; +int devfs_vptofh (struct vnode *vp, struct fid *fhp) /*proto*/; +void devfs_dropvnode(dn_p dnp) /*proto*/; diff --git a/sys/miscfs/devfs/devfs_vfsops.c b/sys/miscfs/devfs/devfs_vfsops.c new file mode 100644 index 0000000..96c34f44 --- /dev/null +++ b/sys/miscfs/devfs/devfs_vfsops.c @@ -0,0 +1,234 @@ +/* + * Written by Julian Elischer (julian@DIALix.oz.au) + * + * $Header: /sys/miscfs/devfs/RCS/devfs_vfsops.c,v 1.4 1995/01/07 04:20:25 root Exp root $ + * + * + */ + +#include "param.h" +#include "systm.h" +#include "namei.h" +#include "proc.h" +#include "kernel.h" +#include "vnode.h" +#include "miscfs/specfs/specdev.h" /* defines v_rdev */ +#include "mount.h" +#include "buf.h" +#include "file.h" +#include "malloc.h" +#include "devfsdefs.h" + + +int devfs_init(void) /*proto*/ +{ + printf("devfs initialised\n"); + devfs_back_init(); + /* devfs_front_init();*/ /* nothing to do at the moment */ + return 0; /*XXX*/ +} + +/* + * mp - pointer to 'mount' structure + * path - addr in user space of mount point (ie /usr or whatever) + * data - addr in user space of mount params including the + * name of the block special file to treat as a filesystem. + * ndp - namei data pointer + * p - proc pointer + * devfs is special in that it doesn't require any device to be mounted.. + * It makes up it's data as it goes along. + * it must be mounted during single user.. until it is, only std{in/out/err} + * and the root filesystem are available. + */ +int devfs_mount( struct mount *mp, char *path, caddr_t data, struct nameidata *ndp, struct proc *p) /*proto*/ +{ + struct devfsmount *devfs_mp_p; /* devfs specific mount control block */ + int error; + u_int size; + +DBPRINT(("mount ")); +/* + * If they just want to update, we don't need to do anything. + */ + if (mp->mnt_flag & MNT_UPDATE) + { + return 0; + } + +/* + * Well, it's not an update, it's a real mount request. + * Time to get dirty. + * HERE we should check to see if we are already mounted here. + */ + if(error = mountdevfs( mp, p)) + return (error); + +/* + * Copy in the name of the directory the filesystem + * is to be mounted on. + * And we clear the remainder of the character strings + * to be tidy. + * Then, we try to fill in the filesystem stats structure + * as best we can with whatever we can think of at the time + */ + devfs_mp_p = (struct devfsmount *)mp->mnt_data; + copyinstr(path, (caddr_t)mp->mnt_stat.f_mntonname, + sizeof(mp->mnt_stat.f_mntonname)-1, &size); + bzero(mp->mnt_stat.f_mntonname + size, + sizeof(mp->mnt_stat.f_mntonname) - size); + bzero(mp->mnt_stat.f_mntfromname , MNAMELEN ); + bcopy("devfs",mp->mnt_stat.f_mntfromname, 5); + (void)devfs_statfs(mp, &mp->mnt_stat, p); + return 0; +} + +int mountdevfs( struct mount *mp, struct proc *p) /*proto*/ +{ + int error = 0; + int ronly = (mp->mnt_flag & MNT_RDONLY) != 0; + struct devfsmount *devfs_mp_p; + + + devfs_mp_p = (struct devfsmount *)malloc(sizeof *devfs_mp_p, + M_DEVFSMNT, M_WAITOK); + bzero(devfs_mp_p,sizeof(*devfs_mp_p)); + devfs_mp_p->mount = mp; + +/* + * Fill out some fields + */ + mp->mnt_data = (qaddr_t)devfs_mp_p; + mp->mnt_stat.f_type = MOUNT_DEVFS; + mp->mnt_stat.f_fsid.val[0] = (long)devfs_mp_p; + mp->mnt_stat.f_fsid.val[1] = MOUNT_DEVFS; + mp->mnt_flag |= MNT_LOCAL; + + if(error = devfs_make_plane(devfs_mp_p)) + { + mp->mnt_data = (qaddr_t)0; + free((caddr_t)devfs_mp_p, M_DEVFSMNT); + } + return error; +} + +int devfs_start(struct mount *mp, int flags, struct proc *p) /*proto*/ +{ +DBPRINT(("start ")); + return 0; +} + +/* + * Unmount the filesystem described by mp. + * Note: vnodes from this FS may hang around if being used.. + * This should not be a problem, they should be self contained. + */ +int devfs_unmount( struct mount *mp, int mntflags, struct proc *p) /*proto*/ +{ + int flags = 0; + int error = 0; + struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data; + +DBPRINT(("unmount ")); + devfs_free_plane(devfs_mp_p); + free((caddr_t)devfs_mp_p, M_DEVFSMNT); + mp->mnt_data = (qaddr_t)0; + mp->mnt_flag &= ~MNT_LOCAL; + return error; +} + +/* return the address of the root vnode in *vpp */ +int devfs_root(struct mount *mp, struct vnode **vpp) /*proto*/ +{ + struct denode *ndep; + struct devfsmount *devfs_mp_p = (struct devfsmount *)(mp->mnt_data); + +DBPRINT(("root ")); + devfs_dntovn(devfs_mp_p->plane_root->dnp,vpp); + return 0; +} + +int devfs_quotactl( struct mount *mp, int cmds, uid_t uid, caddr_t arg, struct proc *p) /*proto*/ +{ +DBPRINT(("quotactl ")); + return EOPNOTSUPP; +} + +int devfs_statfs( struct mount *mp, struct statfs *sbp, struct proc *p) /*proto*/ +{ + struct devfsmount *devfs_mp_p = (struct devfsmount *)mp->mnt_data; + +/* + * Fill in the stat block. + */ +DBPRINT(("statfs ")); + sbp->f_type = MOUNT_DEVFS; + sbp->f_flags = 0; /* XXX */ + sbp->f_bsize = 128; + sbp->f_iosize = 1024; /* XXX*/ + sbp->f_blocks = 128; + sbp->f_bfree = 0; + sbp->f_bavail = 0; + sbp->f_files = 128; + sbp->f_ffree = 0; /* what to put in here? */ + sbp->f_fsid.val[0] = (long)devfs_mp_p; + sbp->f_fsid.val[1] = MOUNT_DEVFS; + +/* + * Copy the mounted on and mounted from names into + * the passed in stat block, if it is not the one + * in the mount structure. + */ + if (sbp != &mp->mnt_stat) { + bcopy((caddr_t)mp->mnt_stat.f_mntonname, + (caddr_t)&sbp->f_mntonname[0], MNAMELEN); + bcopy((caddr_t)mp->mnt_stat.f_mntfromname, + (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); + } + return 0; +} + +int devfs_sync(struct mount *mp, int waitfor,struct ucred *cred,struct proc *p) /*proto*/ +{ +DBPRINT(("sync ")); + return 0; +} + +int devfs_vget(struct mount *mp, ino_t ino,struct vnode **vpp) /*proto*/ +{ +DBPRINT(("vget ")); + return EOPNOTSUPP; +} + +/************************************************************* + * The concept of exporting a kernel generated devfs is stupid + * So don't handle filehandles + */ + +int devfs_fhtovp (struct mount *mp, struct fid *fhp, struct mbuf *nam, struct vnode **vpp, int *exflagsp, struct ucred **credanonp) /*proto*/ +{ +DBPRINT(("fhtovp ")); + return (EINVAL); +} + + +int devfs_vptofh (struct vnode *vp, struct fid *fhp) /*proto*/ +{ +DBPRINT(("vptofh ")); + return (EINVAL); +} + +struct vfsops devfs_vfsops = { + devfs_mount, + devfs_start, + devfs_unmount, + devfs_root, + devfs_quotactl, + devfs_statfs, + devfs_sync, + devfs_vget, + devfs_fhtovp, + devfs_vptofh, + devfs_init +}; + +VFS_SET(devfs_vfsops, devfs, MOUNT_DEVFS, 0); diff --git a/sys/miscfs/devfs/devfs_vnops.c b/sys/miscfs/devfs/devfs_vnops.c new file mode 100644 index 0000000..4fd330d6 --- /dev/null +++ b/sys/miscfs/devfs/devfs_vnops.c @@ -0,0 +1,1362 @@ +/* + * Written by Julian Elischer (julian@DIALix.oz.au) + * + * $Header: /sys/miscfs/devfs/RCS/devfs_vnops.c,v 1.3 1995/01/07 04:20:25 root Exp root $ + * + * symlinks can wait 'til later. + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/namei.h> +#include <sys/resourcevar.h> /* defines plimit structure in proc struct */ +#include <sys/kernel.h> +#include <sys/file.h> /* define FWRITE ... */ +#include <sys/stat.h> +#include <sys/buf.h> +#include <sys/proc.h> +#include <sys/mount.h> +#include <sys/vnode.h> +/*#include <miscfs/specfs/specdev.h>*/ /* plimit structure in the proc struct */ +#include <sys/malloc.h> +#include <sys/dir.h> /* defines dirent structure */ +/*#include "vnode_if.h"*/ /* must be included elsewhere (vnode.h?)*/ +#include "devfsdefs.h" + +/*extern struct timeval time,boottime;*/ +/* + * Insert description here + */ + + +/* + * Convert a component of a pathname into a pointer to a locked devfs_front. + * This is a very central and rather complicated routine. + * If the file system is not maintained in a strict tree hierarchy, + * this can result in a deadlock situation (see comments in code below). + * + * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on + * whether the name is to be looked up, created, renamed, or deleted. + * When CREATE, RENAME, or DELETE is specified, information usable in + * creating, renaming, or deleting a directory entry may be calculated. + * If flag has LOCKPARENT or'ed into it and the target of the pathname + * exists, lookup returns both the target and its parent directory locked. + * When creating or renaming and LOCKPARENT is specified, the target may + * not be ".". When deleting and LOCKPARENT is specified, the target may + * be "."., but the caller must check to ensure it does an vrele and DNUNLOCK + * instead of two DNUNLOCKs. + * + * Overall outline of devfs_lookup: + * + * check accessibility of directory + * null terminate the component (lookup leaves the whole string alone) + * look for name in cache, if found, then if at end of path + * and deleting or creating, drop it, else return name + * search for name in directory, to found or notfound + * notfound: + * if creating, return locked directory, + * else return error + * found: + * if at end of path and deleting, return information to allow delete + * if at end of path and rewriting (RENAME and LOCKPARENT), lock target + * devfs_front and return info to allow rewrite + * if not at end, add name to cache; if at end and neither creating + * nor deleting, add name to cache + * On return to lookup, remove the null termination we put in at the start. + * + * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent devfs_front unlocked. + */ +int devfs_lookup(ap) + struct vop_lookup_args /* { + struct vnode * a_dvp; directory vnode ptr + struct vnode ** a_vpp; where to put the result + struct componentname * a_cnp; the name we want + } */ *ap; +{ + struct componentname *cnp = ap->a_cnp; + struct vnode *dir_vnode = ap->a_dvp; + struct vnode **result_vnode = ap->a_vpp; + dn_p dir_node; /* the directory we are searching */ + dn_p new_node; /* the node we are searching for */ + int flags = cnp->cn_flags; + int op = cnp->cn_nameiop; /* LOOKUP, CREATE, RENAME, or DELETE */ + int lockparent = flags & LOCKPARENT; + int wantparent = flags & (LOCKPARENT|WANTPARENT); + int error = 0; + struct proc *p = cnp->cn_proc; + char heldchar; /* the char at the end of the name componet */ + + *result_vnode = NULL; /* safe not sorry */ /*XXX*/ + +DBPRINT(("lookup\n")); + + if(devfs_vntodn(dir_vnode,&dir_node)) + { + printf("vnode has changed?\n"); + vprint("=",dir_vnode); + return(EINVAL); + } + + /* + * Check accessiblity of directory. + */ + if (dir_node->type != DEV_DIR) + { + return (ENOTDIR); + } + if (error = VOP_ACCESS(dir_vnode, VEXEC, cnp->cn_cred, cnp->cn_proc)) + { + return (error); + } + + /* + * We now have a segment name to search for, and a directory to search. + * + * Before tediously performing a linear scan of the directory, + * check the name cache to see if the directory/name pair + * we are looking for is known already. + */ + +#ifdef NOT_AT_THE_MOMENT + if (error = cache_lookup(dir_vnode, result_vnode, cnp)) { + int vpid; /* capability number of vnode */ + + if (error == ENOENT) + return (error); +DBPRINT(("cached ")); + /* + * Claim the next vnode in the path. + * See comment below starting `Step through' for + * an explaination of the locking protocol. + */ + if(devfs_vntodn(*result_vnode,&new_node)) + { + printf("vnode has changed!?\n"); + vprint("=",*result_vnode); + return(EINVAL); + } + vpid = (*result_vnode)->v_id; + if (dir_node == new_node) { /* is "." */ + VREF(*result_vnode); /* not a full vget() */ + error = 0; + } else if (flags & ISDOTDOT) {/* do a locking dance */ + VOP_UNLOCK(dir_vnode); + error = vget(*result_vnode,1); + if (!error && lockparent && (flags & ISLASTCN)) + VOP_LOCK(dir_vnode); + } else { + error = vget(*result_vnode,1); + if (!lockparent || error || !(flags & ISLASTCN)) + VOP_UNLOCK(dir_vnode); + } + /* + * Check that the capability number did not change + * while we were waiting for the lock. + */ + if (!error) { + if (vpid == (*result_vnode)->v_id) + return (0); /* SUCCCESS, return! */ + vput((*result_vnode)); /* pretend we failed */ + if (lockparent + && (dir_node != new_node) + && (flags & ISLASTCN)) + VOP_UNLOCK(dir_vnode); + } + if( error = VOP_LOCK(dir_vnode)) + return error; + *result_vnode = NULL; /* safe not sorry */ +DBPRINT(("errr, maybe not cached ")); + } +#endif +/***********************************************************************\ +* SEARCH FOR NAME * +* while making sure the component is null terminated for the strcmp * +\***********************************************************************/ + + heldchar = cnp->cn_nameptr[cnp->cn_namelen]; + cnp->cn_nameptr[cnp->cn_namelen] = '\0'; + new_node = dev_findfront(dir_node,cnp->cn_nameptr); + cnp->cn_nameptr[cnp->cn_namelen] = heldchar; + if(new_node) + { + goto found; + } +/***********************************************************************\ +* Failed to find it.. (That may be good) * +\***********************************************************************/ +/* notfound: */ +/*XXX*/ /* possibly release some resources here */ + /* + * If creating, and at end of pathname + * then can consider + * allowing file to be created. + * XXX original code (ufs_lookup) checked for . being deleted + */ + if ((op == CREATE || op == RENAME) && (flags & ISLASTCN)) { + + /* + * Access for write is interpreted as allowing + * creation of files in the directory. + */ + if (error = VOP_ACCESS(dir_vnode, VWRITE, + cnp->cn_cred, cnp->cn_proc)) + { +DBPRINT(("MKACCESS ")); + return (error); + } + dir_node->flags |= IUPD|ICHG;/*XXX*/ + /* + * We return with the directory locked, so that + * the parameters we set up above will still be + * valid if we actually decide to do a direnter(). + * We return ni_vp == NULL to indicate that the entry + * does not currently exist; we leave a pointer to + * the (locked) directory devfs_front in namei_data->ni_dvp. + * The pathname buffer is saved so that the name + * can be obtained later. + * + * NB - if the directory is unlocked, then this + * information cannot be used. + */ + cnp->cn_flags |= SAVENAME; + if (!lockparent) + VOP_UNLOCK(dir_vnode); + /* DON't make a cache entry... status changing */ + return (EJUSTRETURN); + } + /* + * Insert name into cache (as non-existent) if appropriate. + */ + if ((cnp->cn_flags & MAKEENTRY) && op != CREATE) + cache_enter(dir_vnode, *result_vnode, cnp); +DBPRINT(("NOT\n")); + return (ENOENT); + +/***********************************************************************\ +* Found it.. this is not always a good thing.. * +\***********************************************************************/ +found: +/*XXX*/ /* possibly release some resources here */ + + + /* + * If deleting, and at end of pathname, return + * parameters which can be used to remove file. + * If the wantparent flag isn't set, we return only + * the directory (in namei_data->ni_dvp), otherwise we go + * on and lock the devfs_front, being careful with ".". + */ + if (op == DELETE && (flags & ISLASTCN)) { + /* + * Write access to directory required to delete files. + */ + if (error = VOP_ACCESS(dir_vnode, VWRITE, + cnp->cn_cred, cnp->cn_proc)) + return (error); + /* + */ + if (dir_node == new_node) { + VREF(dir_vnode); + *result_vnode = dir_vnode; + return (0); + } + /* + * If directory is "sticky", then user must own + * the directory, or the file in it, else she + * may not delete it (unless she's root). This + * implements append-only directories. + */ + devfs_dntovn(new_node,result_vnode); + VOP_LOCK((*result_vnode)); +#ifdef NOTYET + if ((dir_node->mode & ISVTX) && + cnp->cn_cred->cr_uid != 0 && + cnp->cn_cred->cr_uid != dir_node->uid && + new_node->uid != cnp->cn_cred->cr_uid) { + VOP_UNLOCK((*result_vnode)); + return (EPERM); + } +#endif + if (!lockparent) + VOP_UNLOCK(dir_vnode); + return (0); + } + + /* + * If rewriting (RENAME), return the devfs_front and the + * information required to rewrite the present directory + * Must get devfs_front of directory entry to verify it's a + * regular file, or empty directory. + */ + if (op == RENAME && wantparent && (flags & ISLASTCN)) { + if (error = VOP_ACCESS(dir_vnode, VWRITE, + cnp->cn_cred, cnp->cn_proc)) + return (error); + /* + * Careful about locking second devfs_front. + * This can only occur if the target is ".". + */ + if (dir_node == new_node) + return (EISDIR); + devfs_dntovn(new_node,result_vnode); + VOP_LOCK(*result_vnode); + cnp->cn_flags |= SAVENAME; + if (!lockparent) + VOP_UNLOCK(dir_vnode); + return (0); + } + + /* + * Step through the translation in the name. We do not `DNUNLOCK' the + * directory because we may need it again if a symbolic link + * is relative to the current directory. Instead we save it + * unlocked as "saved_dir_node" XXX. We must get the target + * devfs_front before unlocking + * the directory to insure that the devfs_front will not be removed + * before we get it. We prevent deadlock by always fetching + * devfs_fronts from the root, moving down the directory tree. Thus + * when following backward pointers ".." we must unlock the + * parent directory before getting the requested directory. + * There is a potential race condition here if both the current + * and parent directories are removed before the `DNLOCK' for the + * devfs_front associated with ".." returns. We hope that this occurs + * infrequently since we cannot avoid this race condition without + * implementing a sophisticated deadlock detection algorithm. + * Note also that this simple deadlock detection scheme will not + * work if the file system has any hard links other than ".." + * that point backwards in the directory structure. + */ + if (flags & ISDOTDOT) { + VOP_UNLOCK(dir_vnode); /* race to get the devfs_front */ + devfs_dntovn(new_node,result_vnode); + VOP_LOCK(*result_vnode); + if (lockparent && (flags & ISLASTCN)) + VOP_LOCK(dir_vnode); + } else if (dir_node == new_node) { + VREF(dir_vnode); /* we want ourself, ie "." */ + *result_vnode = dir_vnode; + } else { + devfs_dntovn(new_node,result_vnode); + VOP_LOCK(*result_vnode); + if (!lockparent || (flags & ISLASTCN)) + VOP_UNLOCK(dir_vnode); + } + + /* + * Insert name into cache if appropriate. + */ + if (cnp->cn_flags & MAKEENTRY) + cache_enter(dir_vnode, *result_vnode, cnp); +DBPRINT(("GOT\n")); + return (0); +} + + + + +/* + * Create a regular file. + * We must also free the pathname buffer pointed at + * by ndp->ni_pnbuf, always on error, or only if the + * SAVESTART bit in ni_nameiop is clear on success. + * <still true in 4.4?> + * + * Always error... no such thing in this FS + */ +int devfs_create(ap) + struct vop_mknod_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ +DBPRINT(("create\n")); + return EINVAL; +} + +int devfs_mknod(ap) + struct vop_mknod_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ + int error; + + +DBPRINT(("mknod\n")); + switch (ap->a_vap->va_type) { + case VDIR: +#ifdef VNSLEAZE + return devfs_mkdir(ap); + /*XXX check for WILLRELE settings (different)*/ +#else + error = VOP_MKDIR(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap); +#endif + break; + +/* + * devfs_create() sets ndp->ni_vp. + */ + case VREG: +#ifdef VNSLEAZE + return devfs_create(ap); + /*XXX check for WILLRELE settings (different)*/ +#else + error = VOP_CREATE(ap->a_dvp, ap->a_vpp, ap->a_cnp, ap->a_vap); +#endif + break; + + default: + return EINVAL; + break; + } + return error; +} + +int devfs_open(ap) + struct vop_open_args /* { + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +DBPRINT(("open\n")); + return 0; +} + +int devfs_close(ap) + struct vop_close_args /* { + struct vnode *a_vp; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +DBPRINT(("close\n")); + return 0; +} + +int devfs_access(ap) + struct vop_access_args /* { + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + /* + * mode is filled with a combination of VREAD, VWRITE, + * and/or VEXEC bits turned on. In an octal number these + * are the Y in 0Y00. + */ + struct vnode *vp = ap->a_vp; + int mode = ap->a_mode; + struct ucred *cred = ap->a_cred; + struct proc *p = ap->a_p; + dn_p file_node; + int error; + gid_t *gp; + int i; + +DBPRINT(("access\n")); + if (error = devfs_vntodn(vp,&file_node)) + { + printf("devfs_vntodn returned %d ",error); + return error; + } + /* + * Root gets to do anything. + */ + if (cred->cr_uid == 0) + return 0; + + /* + * Access check is based on only one of owner, group, public. + * If not owner, then check group. If not a member of the + * group, then check public access. + */ + if (cred->cr_uid != file_node->uid) + { + /* failing that.. try groups */ + mode >>= 3; + gp = cred->cr_groups; + for (i = 0; i < cred->cr_ngroups; i++, gp++) + { + if (file_node->gid == *gp) + { + goto found; + } + } + /* failing that.. try general access */ + mode >>= 3; +found: + ; + } + if ((file_node->mode & mode) == mode) + return (0); + return (EACCES); +} + +int devfs_getattr(ap) + struct vop_getattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + struct vattr *vap = ap->a_vap; + struct ucred *cred = ap->a_cred; + struct proc *p = ap->a_p; + dn_p file_node; + int error; + +DBPRINT(("getattr\n")); + if (error = devfs_vntodn(vp,&file_node)) + { + printf("devfs_vntodn returned %d ",error); + return error; + } + vap->va_rdev = 0;/* default value only */ + vap->va_mode = file_node->mode; + switch (file_node->type) + { + case DEV_DIR: + vap->va_rdev = (dev_t)file_node->dvm; + vap->va_mode |= (S_IFDIR); + break; + case DEV_CDEV: + vap->va_rdev = file_node->by.Cdev.dev; + vap->va_mode |= (S_IFCHR); + break; + case DEV_BDEV: + vap->va_rdev = file_node->by.Bdev.dev; + vap->va_mode |= (S_IFBLK); + break; + case DEV_SLNK: + break; + } + vap->va_type = vp->v_type; + vap->va_nlink = file_node->links; + vap->va_uid = file_node->uid; + vap->va_gid = file_node->gid; + vap->va_fsid = (long)file_node->dvm; + vap->va_fileid = (long)file_node; + vap->va_size = file_node->len; /* now a u_quad_t */ + vap->va_blocksize = 512; + vap->va_atime = file_node->atime; + vap->va_mtime = file_node->mtime; + vap->va_ctime = file_node->ctime; + vap->va_gen = 0; + vap->va_flags = 0; + vap->va_bytes = file_node->len; /* u_quad_t */ + vap->va_filerev = 0; /* XXX */ /* u_quad_t */ + vap->va_vaflags = 0; /* XXX */ + return 0; +} + +int devfs_setattr(ap) + struct vop_setattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + struct vattr *vap = ap->a_vap; + struct ucred *cred = ap->a_cred; + struct proc *p = ap->a_p; + int error = 0; + dn_p file_node; + + if (error = devfs_vntodn(vp,&file_node)) + { + printf("devfs_vntodn returned %d ",error); + return error; + } +DBPRINT(("setattr\n")); + if ((vap->va_type != VNON) || + (vap->va_nlink != VNOVAL) || + (vap->va_fsid != VNOVAL) || + (vap->va_fileid != VNOVAL) || + (vap->va_blocksize != VNOVAL) || + (vap->va_rdev != VNOVAL) || + (vap->va_bytes != VNOVAL) || + (vap->va_gen != VNOVAL) || + (vap->va_atime.ts_sec != VNOVAL)) + { + return EINVAL; + } + + if (vap->va_size != VNOVAL) { + return error; /*XXX (?) */ + } + if (vap->va_atime.ts_sec != VNOVAL) + { + file_node->atime = vap->va_atime; + } + + if (vap->va_mtime.ts_sec != VNOVAL) + { + file_node->mtime = vap->va_mtime; + } + + if (vap->va_ctime.ts_sec != VNOVAL) + { + file_node->ctime = vap->va_ctime; + } + + if (vap->va_mode != (u_short)VNOVAL) + { + /* set drwxwxrwx stuff */ + file_node->mode &= ~07777; + file_node->mode |= vap->va_mode & 07777; + } + + if (vap->va_uid != (uid_t)VNOVAL) + { + file_node->uid = vap->va_uid; + } + if (vap->va_gid != (gid_t)VNOVAL) + { + file_node->gid = vap->va_gid; + } + if (vap->va_flags != VNOVAL) { + if (error = suser(cred, &p->p_acflag)) + return error; + if (cred->cr_uid == 0) + ; + else { + } + } + return error; +} + + +int devfs_read(ap) + struct vop_read_args /* { + struct vnode *a_vp; + struct uio *a_uio; + int a_ioflag; + struct ucred *a_cred; + } */ *ap; +{ + int eof; + int error = 0; + dn_p file_node; + +DBPRINT(("read\n")); + if (error = devfs_vntodn(ap->a_vp,&file_node)) + { + printf("devfs_vntodn returned %d ",error); + return error; + } + + + switch (ap->a_vp->v_type) { + case VREG: + return(EINVAL); + case VDIR: + return VOP_READDIR(ap->a_vp,ap->a_uio,ap->a_cred); + case VCHR: + case VBLK: + error = spec_read(ap); + TIMEVAL_TO_TIMESPEC(&time,&(file_node->atime)) + return(error); + + default: + panic("devfs_read(): bad file type"); + break; + } +} + +/* + * Write data to a file or directory. + */ +int devfs_write(ap) + struct vop_write_args /* { + struct vnode *a_vp; + struct uio *a_uio; + int a_ioflag; + struct ucred *a_cred; + } */ *ap; +{ + dn_p file_node; + int error; + + if (error = devfs_vntodn(ap->a_vp,&file_node)) + { + printf("devfs_vntodn returned %d ",error); + return error; + } + + +DBPRINT(("write\n")); + switch (ap->a_vp->v_type) { + case VREG: + return(EINVAL); + case VDIR: + return(EISDIR); + case VCHR: + case VBLK: + error = spec_write(ap); + TIMEVAL_TO_TIMESPEC(&time,&(file_node->mtime)) + return(error); + + default: + panic("devfs_write(): bad file type"); + break; + } +} + +int devfs_ioctl(ap) + struct vop_ioctl_args /* { + struct vnode *a_vp; + int a_command; + caddr_t a_data; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +DBPRINT(("ioctl\n")); + return ENOTTY; +} + +int devfs_select(ap) + struct vop_select_args /* { + struct vnode *a_vp; + int a_which; + int a_fflags; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +DBPRINT(("select\n")); + return 1; /* DOS filesystems never block? */ +} + +int devfs_mmap(ap) + struct vop_mmap_args /* { + struct vnode *a_vp; + int a_fflags; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ +DBPRINT(("mmap\n")); + return EINVAL; +} + +/* + * Flush the blocks of a file to disk. + */ +int devfs_fsync(ap) + struct vop_fsync_args /* { + struct vnode *a_vp; + struct ucred *a_cred; + int a_waitfor; + struct proc *a_p; + } */ *ap; +{ +DBPRINT(("fsync\n")); + return(0); +} + +int devfs_seek(ap) + struct vop_seek_args /* { + struct vnode *a_vp; + off_t a_oldoff; + off_t a_newoff; + struct ucred *a_cred; + } */ *ap; +{ + int error = 0; +DBPRINT(("seek\n")); + return 0; +} + +int devfs_remove(ap) + struct vop_remove_args /* { + struct vnode *a_dvp; + struct vnode *a_vp; + struct componentname *a_cnp; + } */ *ap; +{ + int error = 0; + /*vrele(DETOV(dep));*/ +DBPRINT(("remove\n")); + return error; +} + +/* + */ +int devfs_link(ap) + struct vop_link_args /* { + struct vnode *a_vp; + struct vnode *a_tdvp; + struct componentname *a_cnp; + } */ *ap; +{ +DBPRINT(("link\n")); + return 0; +} + +int devfs_rename(ap) + struct vop_rename_args /* { + struct vnode *a_fdvp; + struct vnode *a_fvp; + struct componentname *a_fcnp; + struct vnode *a_tdvp; + struct vnode *a_tvp; + struct componentname *a_tcnp; + } */ *ap; +{ +DBPRINT(("rename\n")); + return 0; +} + + +int devfs_mkdir(ap) + struct vop_mkdir_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ +DBPRINT(("mkdir\n")); + return EINVAL; +} + +int devfs_rmdir(ap) + struct vop_rmdir_args /* { + struct vnode *a_dvp; + struct vnode *a_vp; + struct componentname *a_cnp; + } */ *ap; +{ +DBPRINT(("rmdir\n")); + return 0; +} + +int devfs_symlink(ap) + struct vop_symlink_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + char *a_target; + } */ *ap; +{ + return EINVAL; +DBPRINT(("symlink\n")); +} + +/* + * Vnode op for readdir + */ +int devfs_readdir(ap) + struct vop_readdir_args /* { + struct vnode *a_vp; + struct uio *a_uio; + struct ucred *a_cred; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + struct uio *uio = ap->a_uio; + struct ucred *cred = ap->a_cred; + struct dirent dirent; + dn_p dir_node; + devf_p name_node; + char *name; + int error = 0; + int reclen; + int nodenumber; + int startpos,pos; + +DBPRINT(("readdir\n")); + +/* set up refs to dir */ + if (error = devfs_vntodn(vp,&dir_node)) + return error; + if(dir_node->type != DEV_DIR) + return(ENOTDIR); + + pos = 0; + startpos = uio->uio_offset; + name_node = dir_node->by.Dir.dirlist; + nodenumber = 0; + TIMEVAL_TO_TIMESPEC(&time,&(dir_node->atime)) + + while ((name_node || (nodenumber < 2)) && (uio->uio_resid > 0)) + { + switch(nodenumber) + { + case 0: + dirent.d_fileno = (unsigned long int)dir_node; + name = "."; + dirent.d_namlen = 1; + break; + case 1: + if(dir_node->by.Dir.parent) + dirent.d_fileno + = (unsigned long int)dir_node->by.Dir.parent; + else + dirent.d_fileno = (unsigned long int)dir_node; + name = ".."; + dirent.d_namlen = 2; + break; + default: + dirent.d_fileno = + (unsigned long int)name_node->dnp; + dirent.d_namlen = strlen(name_node->name); + name = name_node->name; + } + + reclen = dirent.d_reclen = DIRSIZ (&dirent); + + if(pos >= startpos) /* made it to the offset yet? */ + { + if (uio->uio_resid < reclen) /* will it fit? */ + break; + strcpy( dirent.d_name,name); + if (error = uiomove ((caddr_t)&dirent, + dirent.d_reclen, uio)) + break; + } + pos += reclen; + if((nodenumber >1) && name_node) + name_node = name_node->next; + nodenumber++; + } + uio->uio_offset = pos; + + return (error); +} + + +/* + */ +int devfs_readlink(ap) + struct vop_readlink_args /* { + struct vnode *a_vp; + struct uio *a_uio; + struct ucred *a_cred; + } */ *ap; +{ +DBPRINT(("readlink\n")); + return 0; +} + +int devfs_abortop(ap) + struct vop_abortop_args /* { + struct vnode *a_dvp; + struct componentname *a_cnp; + } */ *ap; +{ +DBPRINT(("abortop\n")); + return 0; +} + +int devfs_inactive(ap) + struct vop_inactive_args /* { + struct vnode *a_vp; + } */ *ap; +{ +DBPRINT(("inactive\n")); + return 0; +} +int devfs_lock(ap) + struct vop_lock_args *ap; +{ +DBPRINT(("lock\n")); + return 0; +} + +int devfs_unlock(ap) + struct vop_unlock_args *ap; +{ +DBPRINT(("unlock\n")); + return 0; +} + +int devfs_islocked(ap) + struct vop_islocked_args /* { + struct vnode *a_vp; + } */ *ap; +{ +DBPRINT(("islocked\n")); + return 0; +} + +int devfs_bmap(ap) + struct vop_bmap_args /* { + struct vnode *a_vp; + daddr_t a_bn; + struct vnode **a_vpp; + daddr_t *a_bnp; + int *a_runp; + } */ *ap; +{ +DBPRINT(("bmap\n")); + return 0; +} + +int devfs_strategy(ap) + struct vop_strategy_args /* { + struct buf *a_bp; + } */ *ap; +{ + struct vnode *vp; + int error; +DBPRINT(("strategy\n")); + + if (ap->a_bp->b_vp->v_type == VBLK || ap->a_bp->b_vp->v_type == VCHR) + printf("devfs_strategy: spec"); + return 0; +} + + +int devfs_advlock(ap) + struct vop_advlock_args /* { + struct vnode *a_vp; + caddr_t a_id; + int a_op; + struct flock *a_fl; + int a_flags; + } */ *ap; +{ +DBPRINT(("advlock\n")); + return EINVAL; /* we don't do locking yet */ +} + +int devfs_reclaim(ap) + struct vop_reclaim_args /* { + struct vnode *a_vp; + } */ *ap; +{ + dn_p file_node; + int error; + +DBPRINT(("reclaim\n")); + if (error = devfs_vntodn(ap->a_vp,&file_node)) + { + printf("devfs_vntodn returned %d ",error); + return error; + } + + file_node->vn = 0; + file_node->vn_id = 0; + return(0); +} + +/* + * Return POSIX pathconf information applicable to special devices. + */ +int +devfs_pathconf(ap) + struct vop_pathconf_args /* { + struct vnode *a_vp; + int a_name; + int *a_retval; + } */ *ap; +{ + + switch (ap->a_name) { + case _PC_LINK_MAX: + *ap->a_retval = LINK_MAX; + return (0); + case _PC_MAX_CANON: + *ap->a_retval = MAX_CANON; + return (0); + case _PC_MAX_INPUT: + *ap->a_retval = MAX_INPUT; + return (0); + case _PC_PIPE_BUF: + *ap->a_retval = PIPE_BUF; + return (0); + case _PC_CHOWN_RESTRICTED: + *ap->a_retval = 1; + return (0); + case _PC_VDISABLE: + *ap->a_retval = _POSIX_VDISABLE; + return (0); + default: + return (EINVAL); + } + /* NOTREACHED */ +} + + +/* + * Print out the contents of a /devfs vnode. + */ +/* ARGSUSED */ +int +devfs_print(ap) + struct vop_print_args /* { + struct vnode *a_vp; + } */ *ap; +{ + + printf("tag VT_DEVFS, devfs vnode\n"); + return (0); +} + +/*void*/ +int +devfs_vfree(ap) + struct vop_vfree_args /* { + struct vnode *a_pvp; + ino_t a_ino; + int a_mode; + } */ *ap; +{ + + return (0); +} + +/**************************************************************************\ +* pseudo ops * +\**************************************************************************/ + +/* + * /devfs vnode unsupported operation + */ +int +devfs_enotsupp() +{ + + return (EOPNOTSUPP); +} + +/* + * /devfs "should never get here" operation + */ +int +devfs_badop() +{ + + panic("devfs: bad op"); + /* NOTREACHED */ +} + +/* + * devfs vnode null operation + */ +int +devfs_nullop() +{ + + return (0); +} + + +void devfs_dropvnode(dn_p dnp) /*proto*/ +{ + struct vnode *vn_p; + + if(!dnp) + { + printf("devfs: dn count dropped too early\n"); + } + vn_p = dnp->vn; + if(!vn_p) + { + printf("devfs: vn count dropped too early\n"); + } + /* + * check if we have a vnode....... + */ + if((vn_p) && ( dnp->vn_id == vn_p->v_id) && (dnp == (dn_p)vn_p->v_data)) + { + vgoneall(vn_p); + } + dnp->vn = NULL; /* be pedantic about this */ +} + +#define devfs_create ((int (*) __P((struct vop_create_args *)))devfs_enotsupp) +#define devfs_mknod ((int (*) __P((struct vop_mknod_args *)))devfs_enotsupp) +#define devfs_close ((int (*) __P((struct vop_close_args *)))nullop) +#define devfs_ioctl ((int (*) __P((struct vop_ioctl_args *)))devfs_enotsupp) +#define devfs_select ((int (*) __P((struct vop_select_args *)))devfs_enotsupp) +#define devfs_mmap ((int (*) __P((struct vop_mmap_args *)))devfs_enotsupp) +#define devfs_fsync ((int (*) __P((struct vop_fsync_args *)))nullop) +#define devfs_seek ((int (*) __P((struct vop_seek_args *)))nullop) +#define devfs_remove ((int (*) __P((struct vop_remove_args *)))devfs_enotsupp) +#define devfs_link ((int (*) __P((struct vop_link_args *)))devfs_enotsupp) +#define devfs_rename ((int (*) __P((struct vop_rename_args *)))devfs_enotsupp) +#define devfs_mkdir ((int (*) __P((struct vop_mkdir_args *)))devfs_enotsupp) +#define devfs_rmdir ((int (*) __P((struct vop_rmdir_args *)))devfs_enotsupp) +#define devfs_symlink ((int (*) __P((struct vop_symlink_args *)))devfs_enotsupp) +#define devfs_readlink \ + ((int (*) __P((struct vop_readlink_args *)))devfs_enotsupp) +#define devfs_abortop ((int (*) __P((struct vop_abortop_args *)))nullop) +#define devfs_lock ((int (*) __P((struct vop_lock_args *)))nullop) +#define devfs_unlock ((int (*) __P((struct vop_unlock_args *)))nullop) +#define devfs_bmap ((int (*) __P((struct vop_bmap_args *)))devfs_badop) +#define devfs_strategy ((int (*) __P((struct vop_strategy_args *)))devfs_badop) +#define devfs_islocked ((int (*) __P((struct vop_islocked_args *)))nullop) +#define devfs_advlock ((int (*) __P((struct vop_advlock_args *)))devfs_enotsupp) +#define devfs_blkatoff \ + ((int (*) __P((struct vop_blkatoff_args *)))devfs_enotsupp) +#define devfs_valloc ((int(*) __P(( \ + struct vnode *pvp, \ + int mode, \ + struct ucred *cred, \ + struct vnode **vpp))) devfs_enotsupp) +#define devfs_truncate \ + ((int (*) __P((struct vop_truncate_args *)))devfs_enotsupp) +#define devfs_update ((int (*) __P((struct vop_update_args *)))devfs_enotsupp) +#define devfs_bwrite ((int (*) __P((struct vop_bwrite_args *)))devfs_enotsupp) + +/* These are the operations used by directories etc in a devfs */ + +int (**devfs_vnodeop_p)(); +struct vnodeopv_entry_desc devfs_vnodeop_entries[] = { + { &vop_default_desc, vn_default_error }, + { &vop_lookup_desc, devfs_lookup }, /* lookup */ + { &vop_create_desc, devfs_create }, /* create */ + { &vop_mknod_desc, devfs_mknod }, /* mknod */ + { &vop_open_desc, devfs_open }, /* open */ + { &vop_close_desc, devfs_close }, /* close */ + { &vop_access_desc, devfs_access }, /* access */ + { &vop_getattr_desc, devfs_getattr }, /* getattr */ + { &vop_setattr_desc, devfs_setattr }, /* setattr */ + { &vop_read_desc, devfs_read }, /* read */ + { &vop_write_desc, devfs_write }, /* write */ + { &vop_ioctl_desc, devfs_ioctl }, /* ioctl */ + { &vop_select_desc, devfs_select }, /* select */ + { &vop_mmap_desc, devfs_mmap }, /* mmap */ + { &vop_fsync_desc, devfs_fsync }, /* fsync */ + { &vop_seek_desc, devfs_seek }, /* seek */ + { &vop_remove_desc, devfs_remove }, /* remove */ + { &vop_link_desc, devfs_link }, /* link */ + { &vop_rename_desc, devfs_rename }, /* rename */ + { &vop_mkdir_desc, devfs_mkdir }, /* mkdir */ + { &vop_rmdir_desc, devfs_rmdir }, /* rmdir */ + { &vop_symlink_desc, devfs_symlink }, /* symlink */ + { &vop_readdir_desc, devfs_readdir }, /* readdir */ + { &vop_readlink_desc, devfs_readlink }, /* readlink */ + { &vop_abortop_desc, devfs_abortop }, /* abortop */ + { &vop_inactive_desc, devfs_inactive }, /* inactive */ + { &vop_reclaim_desc, devfs_reclaim }, /* reclaim */ + { &vop_lock_desc, devfs_lock }, /* lock */ + { &vop_unlock_desc, devfs_unlock }, /* unlock */ + { &vop_bmap_desc, devfs_bmap }, /* bmap */ + { &vop_strategy_desc, devfs_strategy }, /* strategy */ + { &vop_print_desc, devfs_print }, /* print */ + { &vop_islocked_desc, devfs_islocked }, /* islocked */ + { &vop_pathconf_desc, devfs_pathconf }, /* pathconf */ + { &vop_advlock_desc, devfs_advlock }, /* advlock */ + { &vop_blkatoff_desc, devfs_blkatoff }, /* blkatoff */ + { &vop_valloc_desc, devfs_valloc }, /* valloc */ + { &vop_vfree_desc, devfs_vfree }, /* vfree */ + { &vop_truncate_desc, devfs_truncate }, /* truncate */ + { &vop_update_desc, devfs_update }, /* update */ + { &vop_bwrite_desc, devfs_bwrite }, /* bwrite */ + { (struct vnodeop_desc*)NULL, (int(*)())NULL } +}; +struct vnodeopv_desc devfs_vnodeop_opv_desc = + { &devfs_vnodeop_p, devfs_vnodeop_entries }; + +VNODEOP_SET(devfs_vnodeop_opv_desc); + + +/*copied in from specfs/spec_vnops.c.. (spot the changes )*/ +/* These are the operations used by special devices in a devfs */ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)spec_vnops.c 8.6 (Berkeley) 4/9/94 + * spec_vnops.c,v 1.9 1994/11/14 13:22:52 bde Exp + */ + +#include "../specfs/specdev.h" /* for all the definitions and prototypes */ + +int (**dev_spec_vnodeop_p)(); +struct vnodeopv_entry_desc dev_spec_vnodeop_entries[] = { + { &vop_default_desc, vn_default_error }, + { &vop_lookup_desc, spec_lookup }, /* lookup */ + { &vop_create_desc, spec_create }, /* create */ + { &vop_mknod_desc, spec_mknod }, /* mknod */ + { &vop_open_desc, spec_open }, /* open */ + { &vop_close_desc, spec_close }, /* close */ + { &vop_access_desc, devfs_access }, /* access */ + { &vop_getattr_desc, devfs_getattr }, /* getattr */ + { &vop_setattr_desc, devfs_setattr }, /* setattr */ + { &vop_read_desc, spec_read }, /* read */ + { &vop_write_desc, spec_write }, /* write */ + { &vop_ioctl_desc, spec_ioctl }, /* ioctl */ + { &vop_select_desc, spec_select }, /* select */ + { &vop_mmap_desc, spec_mmap }, /* mmap */ + { &vop_fsync_desc, spec_fsync }, /* fsync */ + { &vop_seek_desc, spec_seek }, /* seek */ + { &vop_remove_desc, spec_remove }, /* remove */ + { &vop_link_desc, spec_link }, /* link */ + { &vop_rename_desc, spec_rename }, /* rename */ + { &vop_mkdir_desc, spec_mkdir }, /* mkdir */ + { &vop_rmdir_desc, spec_rmdir }, /* rmdir */ + { &vop_symlink_desc, spec_symlink }, /* symlink */ + { &vop_readdir_desc, spec_readdir }, /* readdir */ + { &vop_readlink_desc, spec_readlink }, /* readlink */ + { &vop_abortop_desc, spec_abortop }, /* abortop */ + { &vop_inactive_desc, spec_inactive }, /* inactive */ + { &vop_reclaim_desc, spec_reclaim }, /* reclaim */ + { &vop_lock_desc, spec_lock }, /* lock */ + { &vop_unlock_desc, spec_unlock }, /* unlock */ + { &vop_bmap_desc, spec_bmap }, /* bmap */ + { &vop_strategy_desc, spec_strategy }, /* strategy */ + { &vop_print_desc, spec_print }, /* print */ + { &vop_islocked_desc, spec_islocked }, /* islocked */ + { &vop_pathconf_desc, spec_pathconf }, /* pathconf */ + { &vop_advlock_desc, spec_advlock }, /* advlock */ + { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */ + { &vop_valloc_desc, spec_valloc }, /* valloc */ + { &vop_vfree_desc, spec_vfree }, /* vfree */ + { &vop_truncate_desc, spec_truncate }, /* truncate */ + { &vop_update_desc, spec_update }, /* update */ + { &vop_bwrite_desc, spec_bwrite }, /* bwrite */ + { (struct vnodeop_desc*)NULL, (int(*)())NULL } +}; +struct vnodeopv_desc dev_spec_vnodeop_opv_desc = + { &dev_spec_vnodeop_p, dev_spec_vnodeop_entries }; + +VNODEOP_SET(dev_spec_vnodeop_opv_desc); + diff --git a/sys/miscfs/devfs/devfsdefs.h b/sys/miscfs/devfs/devfsdefs.h new file mode 100644 index 0000000..8f5e7b9d3 --- /dev/null +++ b/sys/miscfs/devfs/devfsdefs.h @@ -0,0 +1,195 @@ +#define DEBUG 1 +#ifdef DEBUG +#define DBPRINT(A) printf(A) +#else +#define DBPRINT(A) +#endif + +/* + * Written by Julian Elischer (julian@DIALIX.oz.au) + * + * $Header: /sys/miscfs/devfs/RCS/devfsdefs.h,v 1.2 1994/12/28 02:43:47 root Exp root $ + */ + +/* first a couple of defines for compatibility with inodes */ + +#define ISUID 04000 /* set user identifier when exec'ing */ +#define ISGID 02000 /* set group identifier when exec'ing */ +#define ISVTX 01000 /* save execution information on exit */ +#define IREAD 0400 /* read permission */ +#define IWRITE 0200 /* write permission */ +#define IEXEC 0100 /* execute permission */ + + +#define ILOCKED 0x0001 /* inode is locked */ +#define IWANT 0x0002 /* some process waiting on lock */ +#define IRENAME 0x0004 /* inode is being renamed */ +#define IUPD 0x0010 /* file has been modified */ +#define IACC 0x0020 /* inode access time to be updated */ +#define ICHG 0x0040 /* inode has been changed */ +#define IMOD 0x0080 /* inode has been modified */ +#define ISHLOCK 0x0100 /* file has shared lock */ +#define IEXLOCK 0x0200 /* file has exclusive lock */ +#define ILWAIT 0x0400 /* someone waiting on file lock */ + +/* + * Lock and unlock inodes. + */ +#ifdef notdef +#define DNLOCK(ip) { \ + while ((ip)->i_flag & ILOCKED) { \ + (ip)->i_flag |= IWANT; \ + (void) sleep((caddr_t)(ip), PINOD); \ + } \ + (ip)->i_flag |= ILOCKED; \ +} + +#define DNUNLOCK(ip) { \ + (ip)->i_flag &= ~ILOCKED; \ + if ((ip)->i_flag&IWANT) { \ + (ip)->i_flag &= ~IWANT; \ + wakeup((caddr_t)(ip)); \ + } \ +} +#else +#define DNLOCK(ip) +#define DNUNLOCK(ip) +#endif + + +#define DEVMAXNAMESIZE 32 +#define DEVMAXPATHSIZE 128 +#define DEV_DIR 1 +#define DEV_BDEV 2 +#define DEV_CDEV 3 +#define DEV_DDEV 4 +#define DEV_ALIAS 5 +#define DEV_SLNK 6 + + +extern int (**devfs_vnodeop_p)(); /* our own vector array for dirs */ +extern int (**dev_spec_vnodeop_p)(); /* our own vector array for devs */ + +typedef struct dev_back *devb_p; +typedef struct dev_front *devf_p; +typedef struct devnode *dn_p; + +struct devnode +{ + u_short type; + int flags; /* more inode compatible for now *//*XXXkill*/ + u_short mode; /* basically inode compatible (drwxrwxrwx) */ + u_short uid; /* basically inode compatible */ + u_short gid; /* basically inode compatible */ + struct timespec atime; /* time of last access */ + struct timespec mtime; /* time of last modification */ + struct timespec ctime; /* time file changed */ + int links; /* how many file links does this node have? */ + struct devfsmount *dvm; /* the mount structure for this 'plane' */ + struct vnode *vn; /* address of last vnode that represented us */ + u_long vn_id; /* make sure we have the right vnode */ + int (***ops)(); /* yuk... pointer to pointer(s) to funcs */ + int len; /* of any associated info (e.g. dir data) */ + union typeinfo { + struct { + struct cdevsw *cdevsw; + dev_t dev; + }Cdev; + struct { + struct bdevsw *bdevsw; + dev_t dev; + }Bdev; + struct { + int (***ops)(); /* duplicate, used in dev_add_node */ + int arg; + }Ddev; + struct { + devf_p dirlist; + devf_p *dirlast; + dn_p parent; + devf_p myname; + int entrycount; + }Dir; + struct { + devb_p dirlist; + devb_p *dirlast; + dn_p parent; + devb_p myname; + int entrycount; + }BackDir; + struct { + char *name; /* must be allocated separatly */ + int namelen; + }Slnk; + struct { + devb_p realthing; + devb_p next; + }Alias; + }by; +}; +typedef struct devnode devnode_t; + +struct dev_back +{ + devb_p next; /* next object in this directory */ + devb_p *prevp; /* previous pointer in directory linked list */ + devb_p aliases; /* chain of aliases (kill if we are deleted)*/ + int alias_count; /* how many 'alias' nodes reference us. */ + devf_p fronts; /* the linked list of all our front nodes */ + devf_p *lastfront; /* the end of the front node chain */ + int frontcount; /* number of front nodes that reference us*/ + dn_p parent; /* backpointer to the directory itself */ + dn_p dnp; /* info a STAT would look at */ + char name[DEVMAXNAMESIZE]; +}; + +typedef struct dev_back devb_t; +extern struct dev_back *dev_root; /* always exists */ + + +/* + * Rules for front nodes: + * Dirs hava a strict 1:1 relationship with their OWN devnode + * Symlinks similarly + * Device Nodes ALWAYS point to the devnode that is linked + * to the Backing node. (with a ref count) + */ +struct dev_front +{ + devf_p next; /* next item in this directory chain */ + devf_p *prevp; /* previous pointer in the directory */ + devf_p file_node; /* the file node this represents */ + devf_p next_front; /* pointer to next item for this object */ + devf_p *prev_frontp; /* previous pointer in object chain */ + devb_p realthing; /* backpointer to the backing object */ + dn_p parent; /* our PARENT directory node */ + dn_p dnp; /* info a STAT would look at */ + char name[DEVMAXNAMESIZE]; +}; +typedef struct dev_front devf_t; + + + +/* + * DEVFS specific per/mount information, used to link a monted fs to a + * particular 'plane' of front nodes. + */ +struct devfsmount +{ + struct mount *mount; /* vfs mount struct for this fs */ + devf_p plane_root; /* the root of this 'plane' */ + int flags; /* usefule some day 8-) */ +}; + +struct dev_vn_data +{ + char magic[6]; /* = "devfs" if correct */ + devf_p front; + devb_p back; +}; + +extern struct vnodeops spec_vnodeops,devfs_vnodeops; +/* + * Prototypes for DEVFS virtual filesystem operations + */ +#include "devfs_proto.h" diff --git a/sys/miscfs/devfs/reproto.sh b/sys/miscfs/devfs/reproto.sh new file mode 100644 index 0000000..dc4b731 --- /dev/null +++ b/sys/miscfs/devfs/reproto.sh @@ -0,0 +1,2 @@ +#!/bin/sh +grep -h '/\*proto\*/' *.c |awk '{print $0 ";"}' >devfs_proto.h |