Index: share/man/man9/Makefile =================================================================== --- share/man/man9/Makefile +++ share/man/man9/Makefile @@ -1014,6 +1014,7 @@ make_dev.9 make_dev_cred.9 \ make_dev.9 make_dev_credf.9 \ make_dev.9 make_dev_p.9 + make_dev.9 make_dev_s.9 MLINKS+=malloc.9 free.9 \ malloc.9 MALLOC_DECLARE.9 \ malloc.9 MALLOC_DEFINE.9 \ Index: share/man/man9/make_dev.9 =================================================================== --- share/man/man9/make_dev.9 +++ share/man/man9/make_dev.9 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd Dec 22, 2012 +.Dd Jan 3, 2016 .Dt MAKE_DEV 9 .Os .Sh NAME @@ -32,6 +32,7 @@ .Nm make_dev_cred , .Nm make_dev_credf , .Nm make_dev_p , +.Nm make_dev_s , .Nm make_dev_alias , .Nm make_dev_alias_p , .Nm destroy_dev , @@ -45,16 +46,10 @@ .Sh SYNOPSIS .In sys/param.h .In sys/conf.h -.Ft struct cdev * -.Fn make_dev "struct cdevsw *cdevsw" "int unit" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ... -.Ft struct cdev * -.Fn make_dev_cred "struct cdevsw *cdevsw" "int unit" "struct ucred *cr" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ... -.Ft struct cdev * -.Fn make_dev_credf "int flags" "struct cdevsw *cdevsw" "int unit" "struct ucred *cr" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ... +.Ft void +.Fn make_dev_args_init "struct make_dev_args *args" .Ft int -.Fn make_dev_p "int flags" "struct cdev **cdev" "struct cdevsw *devsw" "struct ucred *cr" "uid_t uid" "gid_t gid" "int mode" "const char *fmt" ... -.Ft struct cdev * -.Fn make_dev_alias "struct cdev *pdev" "const char *fmt" ... +.Fn make_dev_s "struct make_dev_args *args" "struct cdev **cdev" "const char *fmt" ... .Ft int .Fn make_dev_alias_p "int flags" "struct cdev **cdev" "struct cdev *pdev" "const char *fmt" ... .Ft void @@ -67,12 +62,26 @@ .Fn destroy_dev_drain "struct cdevsw *csw" .Ft void .Fn dev_depends "struct cdev *pdev" "struct cdev *cdev" +.Pp +LEGACY INTERFACES +.Ft struct cdev * +.Fn make_dev "struct cdevsw *cdevsw" "int unit" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ... +.Ft struct cdev * +.Fn make_dev_cred "struct cdevsw *cdevsw" "int unit" "struct ucred *cr" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ... +.Ft struct cdev * +.Fn make_dev_credf "int flags" "struct cdevsw *cdevsw" "int unit" "struct ucred *cr" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" ... +.Ft int +.Fn make_dev_p "int flags" "struct cdev **cdev" "struct cdevsw *devsw" "struct ucred *cr" "uid_t uid" "gid_t gid" "int mode" "const char *fmt" ... +.Ft struct cdev * +.Fn make_dev_alias "struct cdev *pdev" "const char *fmt" ... .Sh DESCRIPTION The -.Fn make_dev_credf +.Fn make_dev_s function creates a .Fa cdev -structure for a new device. +structure for a new device, which is returned into the +.Fa cdev +argument. It also notifies .Xr devfs 5 of the presence of the new device, that causes corresponding nodes @@ -80,10 +89,34 @@ Besides this, a .Xr devctl 4 notification is sent. -The device will be owned by -.Va uid , +The function takes the structure +.Va struct make_dev_args args , +which specifies the parameters for the device creation: +.Pp +.Bd -literal -offset indent -compact +struct make_dev_args { + size_t mda_size; + int mda_flags; + struct cdevsw *mda_devsw; + struct ucred *mda_cr; + uid_t mda_uid; + gid_t mda_gid; + int mda_mode; + int mda_unit; + void *mda_si_drv1; + void *mda_si_drv2; +}; +.Ed +Before use and filling with the desired values, the structure must be +initialized by the +.Fn make_dev_args_init +function, which ensures that future kernel interface expansion does +not affect driver source code or binary interface. +.Pp +The created device will be owned by +.Va args.mda_uid , with the group ownership as -.Va gid . +.Va args.mda_gid . The name is the expansion of .Va fmt and following arguments as @@ -97,7 +130,7 @@ .Ql / characters to denote subdirectories. The permissions of the file specified in -.Va perms +.Va args.mda_mode are defined in .In sys/stat.h : .Pp @@ -126,29 +159,28 @@ .Ed .Pp The -.Va cr +.Va args.mda_cr argument specifies credentials that will be stored in the .Fa si_cred member of the initialized .Fa struct cdev . +.Pp The -.Va flags +.Va args.mda_flags argument alters the operation of -.Fn make_dev_credf -or -.Fn make_dev_p . +.Fn make_dev_s. The following values are currently accepted: .Pp -.Bl -tag -width "MAKEDEV_CHECKNAME" -compact -offset indent -.It MAKEDEV_REF +.Bl -tag -width "It Dv MAKEDEV_CHECKNAME" -compact -offset indent +.It Dv MAKEDEV_REF reference the created device -.It MAKEDEV_NOWAIT +.It Dv MAKEDEV_NOWAIT do not sleep, the call may fail -.It MAKEDEV_WAITOK +.It Dv MAKEDEV_WAITOK allow the function to sleep to satisfy malloc -.It MAKEDEV_ETERNAL +.It Dv MAKEDEV_ETERNAL created device will be never destroyed -.It MAKEDEV_CHECKNAME +.It Dv MAKEDEV_CHECKNAME return an error if the device name is invalid or already exists .El .Pp @@ -189,10 +221,49 @@ flag for the code that can be compiled into kernel or loaded (and unloaded) as loadable module. .Pp -A panic will occur if the MAKEDEV_CHECKNAME flag is not specified +A panic will occur if the +.Dv MAKEDEV_CHECKNAME +flag is not specified and the device name is invalid or already exists. .Pp The +.Fn make_dev_p +use of the form +.Bd -literal -offset indent +struct cdev *dev; +int res; +res = make_dev_p(flags, &dev, cdevsw, cred, uid, gid, perms, name); +.Ed +is equivalent to the code +.Bd -literal -offset indent +struct cdev *dev; +struct make_dev_args args; +int res; + +make_dev_args_init(&args); +args.mda_flags = flags; +args.mda_devsw = cdevsw; +args.mda_cred = cred; +args.mda_uid = uid; +args.mda_gid = gid; +args.mda_mode = perms; +res = make_dev_s(&args, &dev, name); +.Ed +.Pp +Similarly, the +.Fn make_dev_credf +function call is equivalent to +.Bd -literal -offset indent + (void) make_dev_s(&args, &dev, name); +.Ed +In other words, +.Fn make_dev_credf +does not allow the caller to obtain the return value, and in +kernels compiled with the +.Va INVARIANTS +options, the function asserts that the device creation succeeded. +.Pp +The .Fn make_dev_cred function is equivalent to the call .Bd -literal -offset indent @@ -207,45 +278,55 @@ .Ed .Pp The -.Fn make_dev_p -function is similar to -.Fn make_dev_credf -but it may return an error number and takes a pointer to the resulting -.Ft *cdev -as an argument. -.Pp -The -.Fn make_dev_alias +.Fn make_dev_alias_p function takes the returned .Ft cdev from .Fn make_dev and makes another (aliased) name for this device. It is an error to call -.Fn make_dev_alias +.Fn make_dev_alias_p prior to calling .Fn make_dev . .Pp -.Fn make_dev_alias_p +The +.Fn make_dev_alias function is similar to .Fn make_dev_alias -but it takes a pointer to the resulting +but it returns the resulting aliasing .Ft *cdev -as an argument and may return an error. +and may not return an error. .Pp The .Fa cdev returned by -.Fn make_dev +.Fn make_dev_s and -.Fn make_dev_alias +.Fn make_dev_alias_p has two fields, .Fa si_drv1 and .Fa si_drv2 , that are available to store state. Both fields are of type -.Ft void * . +.Ft void * , +and can be initialized simultaneously with the +.Va cdev +allocation by filling +.Va args.mda_si_drv1 +and +.Va args.mda_si_drv2 +members of the +.Fn make_dev_s +argument structure, or filled after the +.Va cdev +is allocated, if using legacy interfaces. +In the latter case, the driver should handle the race of +accessing uninitialized +.Va si_drv1 +and +.Va si_drv2 +itself. These are designed to replace the .Fa unit argument to @@ -331,8 +412,10 @@ is actually finished for all of them. .Sh RETURN VALUES If successful, +.Fn make_dev_s +and .Fn make_dev_p -will return 0, otherwise it will return an error. +will return 0, otherwise they will return an error. If successful, .Fn make_dev_credf will return a valid @@ -341,10 +424,11 @@ .Dv NULL . .Sh ERRORS The +.Fn make_dev_s , .Fn make_dev_p and .Fn make_dev_alias_p -call will fail and the device will be not registered if: +calls will fail and the device will be not registered if: .Bl -tag -width Er .It Bq Er ENOMEM The @@ -403,3 +487,7 @@ .Fn make_dev_p first appeared in .Fx 8.2 . +The function +.Fn make_dev_s +first appeared in +.Fx 11.0 . Index: sys/cam/ctl/ctl.c =================================================================== --- sys/cam/ctl/ctl.c +++ sys/cam/ctl/ctl.c @@ -1778,6 +1778,7 @@ static int ctl_init(void) { + struct make_dev_args args; struct ctl_softc *softc; void *other_pool; int i, error; @@ -1785,9 +1786,17 @@ softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, M_WAITOK | M_ZERO); - softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, - "cam/ctl"); - softc->dev->si_drv1 = softc; + make_dev_args_init(&args); + args.mda_devsw = &ctl_cdevsw; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0600; + args.mda_si_drv1 = softc; + error = make_dev_s(&args, &softc->dev, "cam/ctl"); + if (error != 0) { + free(control_softc, M_DEVBUF); + return (error); + } sysctl_ctx_init(&softc->sysctl_ctx); softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, Index: sys/cam/scsi/scsi_ch.c =================================================================== --- sys/cam/scsi/scsi_ch.c +++ sys/cam/scsi/scsi_ch.c @@ -372,6 +372,8 @@ struct ch_softc *softc; struct ccb_getdev *cgd; struct ccb_pathinq cpi; + struct make_dev_args args; + int error; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -431,11 +433,20 @@ /* Register the device */ - softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT, - GID_OPERATOR, 0600, "%s%d", periph->periph_name, - periph->unit_number); + make_dev_args_init(&args); + args.mda_devsw = &ch_cdevsw; + args.mda_unit = periph->unit_number; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0600; + args.mda_si_drv1 = periph; + error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, + periph->unit_number); cam_periph_lock(periph); - softc->dev->si_drv1 = periph; + if (error != 0) { + cam_periph_release_locked(periph); + return (CAM_REQ_CMP_ERR); + } /* * Add an async callback so that we get @@ -507,8 +518,6 @@ struct mtx *mtx; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return(ENXIO); mtx = cam_periph_mtx(periph); mtx_lock(mtx); @@ -754,9 +763,6 @@ int error; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return(ENXIO); - cam_periph_lock(periph); CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n")); Index: sys/cam/scsi/scsi_enc.c =================================================================== --- sys/cam/scsi/scsi_enc.c +++ sys/cam/scsi/scsi_enc.c @@ -264,10 +264,6 @@ int error = 0; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) { - return (ENXIO); - } - if (cam_periph_acquire(periph) != CAM_REQ_CMP) return (ENXIO); @@ -302,8 +298,6 @@ struct mtx *mtx; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); mtx = cam_periph_mtx(periph); mtx_lock(mtx); @@ -364,9 +358,6 @@ addr = NULL; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering encioctl\n")); cam_periph_lock(periph); @@ -905,6 +896,7 @@ enc_softc_t *enc; struct ccb_getdev *cgd; char *tname; + struct make_dev_args args; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -987,12 +979,20 @@ return (CAM_REQ_CMP_ERR); } - enc->enc_dev = make_dev(&enc_cdevsw, periph->unit_number, - UID_ROOT, GID_OPERATOR, 0600, "%s%d", - periph->periph_name, periph->unit_number); - + make_dev_args_init(&args); + args.mda_devsw = &enc_cdevsw; + args.mda_unit = periph->unit_number; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0600; + args.mda_si_drv1 = periph; + err = make_dev_s(&args, &enc->enc_dev, "%s%d", periph->periph_name, + periph->unit_number); cam_periph_lock(periph); - enc->enc_dev->si_drv1 = periph; + if (err != 0) { + cam_periph_release_locked(periph); + return (CAM_REQ_CMP_ERR); + } enc->enc_flags |= ENC_FLAG_INITIALIZED; Index: sys/cam/scsi/scsi_pass.c =================================================================== --- sys/cam/scsi/scsi_pass.c +++ sys/cam/scsi/scsi_pass.c @@ -548,7 +548,8 @@ struct pass_softc *softc; struct ccb_getdev *cgd; struct ccb_pathinq cpi; - int no_tags; + struct make_dev_args args; + int error, no_tags; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -648,9 +649,20 @@ } /* Register the device */ - softc->dev = make_dev(&pass_cdevsw, periph->unit_number, - UID_ROOT, GID_OPERATOR, 0600, "%s%d", - periph->periph_name, periph->unit_number); + make_dev_args_init(&args); + args.mda_devsw = &pass_cdevsw; + args.mda_unit = periph->unit_number; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0600; + args.mda_si_drv1 = periph; + error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, + periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + cam_periph_release_locked(periph); + return (CAM_REQ_CMP_ERR); + } /* * Hold a reference to the periph before we create the physical @@ -664,7 +676,6 @@ } cam_periph_lock(periph); - softc->dev->si_drv1 = periph; TASK_INIT(&softc->add_physpath_task, /*priority*/0, pass_add_physpath, periph); @@ -754,8 +765,6 @@ struct mtx *mtx; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); mtx = cam_periph_mtx(periph); mtx_lock(mtx); @@ -1759,9 +1768,6 @@ uint32_t priority; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return(ENXIO); - cam_periph_lock(periph); softc = (struct pass_softc *)periph->softc; @@ -2068,9 +2074,6 @@ int revents; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - softc = (struct pass_softc *)periph->softc; revents = poll_events & (POLLOUT | POLLWRNORM); @@ -2095,9 +2098,6 @@ struct pass_softc *softc; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - softc = (struct pass_softc *)periph->softc; kn->kn_hook = (caddr_t)periph; Index: sys/cam/scsi/scsi_pt.c =================================================================== --- sys/cam/scsi/scsi_pt.c +++ sys/cam/scsi/scsi_pt.c @@ -173,9 +173,6 @@ struct pt_softc *softc; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - softc = (struct pt_softc *)periph->softc; cam_periph_lock(periph); @@ -252,6 +249,8 @@ struct pt_softc *softc; struct ccb_getdev *cgd; struct ccb_pathinq cpi; + struct make_dev_args args; + int error; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -282,6 +281,21 @@ xpt_action((union ccb *)&cpi); cam_periph_unlock(periph); + + make_dev_args_init(&args); + args.mda_devsw = &pt_cdevsw; + args.mda_unit = periph->unit_number; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0600; + args.mda_si_drv1 = periph; + error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, + periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + return (CAM_REQ_CMP_ERR); + } + softc->device_stats = devstat_new_entry("pt", periph->unit_number, 0, DEVSTAT_NO_BLOCKSIZE, @@ -289,11 +303,7 @@ XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_OTHER); - softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT, - GID_OPERATOR, 0600, "%s%d", periph->periph_name, - periph->unit_number); cam_periph_lock(periph); - softc->dev->si_drv1 = periph; /* * Add async callbacks for bus reset and @@ -571,9 +581,6 @@ int error = 0; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return(ENXIO); - softc = (struct pt_softc *)periph->softc; cam_periph_lock(periph); Index: sys/cam/scsi/scsi_sa.c =================================================================== --- sys/cam/scsi/scsi_sa.c +++ sys/cam/scsi/scsi_sa.c @@ -731,9 +731,6 @@ mode = SAMODE(dev); periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - cam_periph_lock(periph); softc = (struct sa_softc *)periph->softc; @@ -906,10 +903,6 @@ return; } periph = (struct cam_periph *)bp->bio_dev->si_drv1; - if (periph == NULL) { - biofinish(bp, NULL, ENXIO); - return; - } cam_periph_lock(periph); softc = (struct sa_softc *)periph->softc; @@ -1517,9 +1510,6 @@ spaceop = 0; /* shut up gcc */ periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - cam_periph_lock(periph); softc = (struct sa_softc *)periph->softc; @@ -2285,7 +2275,7 @@ static void sasetupdev(struct sa_softc *softc, struct cdev *dev) { - dev->si_drv1 = softc->periph; + dev->si_iosize_max = softc->maxio; dev->si_flags |= softc->si_flags; /* @@ -2347,8 +2337,10 @@ struct sa_softc *softc; struct ccb_getdev *cgd; struct ccb_pathinq cpi; + struct make_dev_args args; caddr_t match; char tmpstr[80]; + int error; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -2506,25 +2498,48 @@ return (CAM_REQ_CMP_ERR); } - softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV, - SA_ATYPE_R), UID_ROOT, GID_OPERATOR, - 0660, "%s%d.ctl", periph->periph_name, periph->unit_number); + make_dev_args_init(&args); + args.mda_devsw = &sa_cdevsw; + args.mda_si_drv1 = softc->periph; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0660; + + args.mda_unit = SAMINOR(SA_CTLDEV, SA_ATYPE_R); + error = make_dev_s(&args, &softc->devs.ctl_dev, "%s%d.ctl", + periph->periph_name, periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + return (CAM_REQ_CMP_ERR); + } sasetupdev(softc, softc->devs.ctl_dev); - softc->devs.r_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV, - SA_ATYPE_R), UID_ROOT, GID_OPERATOR, - 0660, "%s%d", periph->periph_name, periph->unit_number); + args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_R); + error = make_dev_s(&args, &softc->devs.r_dev, "%s%d", + periph->periph_name, periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + return (CAM_REQ_CMP_ERR); + } sasetupdev(softc, softc->devs.r_dev); - softc->devs.nr_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV, - SA_ATYPE_NR), UID_ROOT, GID_OPERATOR, - 0660, "n%s%d", periph->periph_name, periph->unit_number); + args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_NR); + error = make_dev_s(&args, &softc->devs.nr_dev, "n%s%d", + periph->periph_name, periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + return (CAM_REQ_CMP_ERR); + } sasetupdev(softc, softc->devs.nr_dev); - softc->devs.er_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV, - SA_ATYPE_ER), UID_ROOT, GID_OPERATOR, - 0660, "e%s%d", periph->periph_name, periph->unit_number); - sasetupdev(softc, softc->devs.er_dev); + args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_ER); + error = make_dev_s(&args, &softc->devs.er_dev, "e%s%d", + periph->periph_name, periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + return (CAM_REQ_CMP_ERR); + } + sasetupdev(softc, softc->devs.er_dev); cam_periph_lock(periph); Index: sys/cam/scsi/scsi_sg.c =================================================================== --- sys/cam/scsi/scsi_sg.c +++ sys/cam/scsi/scsi_sg.c @@ -300,7 +300,8 @@ struct sg_softc *softc; struct ccb_getdev *cgd; struct ccb_pathinq cpi; - int no_tags; + struct make_dev_args args; + int no_tags, error; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -361,9 +362,20 @@ } /* Register the device */ - softc->dev = make_dev(&sg_cdevsw, periph->unit_number, - UID_ROOT, GID_OPERATOR, 0600, "%s%d", - periph->periph_name, periph->unit_number); + make_dev_args_init(&args); + args.mda_devsw = &sg_cdevsw; + args.mda_unit = periph->unit_number; + args.mda_uid = UID_ROOT; + args.mda_gid = GID_OPERATOR; + args.mda_mode = 0600; + args.mda_si_drv1 = periph; + error = make_dev_s(&args, &softc->dev, "%s%d", + periph->periph_name, periph->unit_number); + if (error != 0) { + cam_periph_lock(periph); + cam_periph_release_locked(periph); + return (CAM_REQ_CMP_ERR); + } if (periph->unit_number < 26) { (void)make_dev_alias(softc->dev, "sg%c", periph->unit_number + 'a'); @@ -373,7 +385,6 @@ (periph->unit_number % 26) + 'a'); } cam_periph_lock(periph); - softc->dev->si_drv1 = periph; /* * Add as async callback so that we get @@ -429,9 +440,6 @@ int error = 0; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - if (cam_periph_acquire(periph) != CAM_REQ_CMP) return (ENXIO); @@ -468,8 +476,6 @@ struct mtx *mtx; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); mtx = cam_periph_mtx(periph); mtx_lock(mtx); @@ -506,9 +512,6 @@ int dir, error; periph = (struct cam_periph *)dev->si_drv1; - if (periph == NULL) - return (ENXIO); - cam_periph_lock(periph); softc = (struct sg_softc *)periph->softc; Index: sys/kern/kern_conf.c =================================================================== --- sys/kern/kern_conf.c +++ sys/kern/kern_conf.c @@ -566,22 +566,26 @@ } static struct cdev * -newdev(struct cdevsw *csw, int unit, struct cdev *si) +newdev(struct make_dev_args *args, struct cdev *si) { struct cdev *si2; + struct cdevsw *csw; mtx_assert(&devmtx, MA_OWNED); + csw = args->mda_devsw; if (csw->d_flags & D_NEEDMINOR) { /* We may want to return an existing device */ LIST_FOREACH(si2, &csw->d_devs, si_list) { - if (dev2unit(si2) == unit) { + if (dev2unit(si2) == args->mda_unit) { dev_free_devlocked(si); return (si2); } } } - si->si_drv0 = unit; + si->si_drv0 = args->mda_unit; si->si_devsw = csw; + si->si_drv1 = args->mda_si_drv1; + si->si_drv2 = args->mda_si_drv2; LIST_INSERT_HEAD(&csw->d_devs, si, si_list); return (si); } @@ -737,33 +741,46 @@ return (0); } +void +make_dev_args_init_impl(struct make_dev_args *args, size_t sz) +{ + + bzero(args, sz); + args->mda_size = sz; +} + static int -make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit, - struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, - va_list ap) +make_dev_sv(struct make_dev_args *args1, struct cdev **dres, + const char *fmt, va_list ap) { struct cdev *dev, *dev_new; + struct make_dev_args args; int res; - KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0, - ("make_dev_credv: both WAITOK and NOWAIT specified")); - dev_new = devfs_alloc(flags); + bzero(&args, sizeof(args)); + if (sizeof(args) < args1->mda_size) + return (EINVAL); + bcopy(args1, &args, args1->mda_size); + KASSERT((args.mda_flags & MAKEDEV_WAITOK) == 0 || + (args.mda_flags & MAKEDEV_NOWAIT) == 0, + ("make_dev_sv: both WAITOK and NOWAIT specified")); + dev_new = devfs_alloc(args.mda_flags); if (dev_new == NULL) return (ENOMEM); dev_lock(); - res = prep_cdevsw(devsw, flags); + res = prep_cdevsw(args.mda_devsw, args.mda_flags); if (res != 0) { dev_unlock(); devfs_free(dev_new); return (res); } - dev = newdev(devsw, unit, dev_new); + dev = newdev(&args, dev_new); if ((dev->si_flags & SI_NAMED) == 0) { res = prep_devname(dev, fmt, ap); if (res != 0) { - if ((flags & MAKEDEV_CHECKNAME) == 0) { + if ((args.mda_flags & MAKEDEV_CHECKNAME) == 0) { panic( - "make_dev_credv: bad si_name (error=%d, si_name=%s)", + "make_dev_sv: bad si_name (error=%d, si_name=%s)", res, dev->si_name); } if (dev == dev_new) { @@ -775,9 +792,9 @@ return (res); } } - if (flags & MAKEDEV_REF) + if ((args.mda_flags & MAKEDEV_REF) != 0) dev_refl(dev); - if (flags & MAKEDEV_ETERNAL) + if ((args.mda_flags & MAKEDEV_ETERNAL) != 0) dev->si_flags |= SI_ETERNAL; if (dev->si_flags & SI_CHEAPCLONE && dev->si_flags & SI_NAMED) { @@ -792,24 +809,55 @@ } KASSERT(!(dev->si_flags & SI_NAMED), ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)", - devsw->d_name, dev2unit(dev), devtoname(dev))); + args.mda_devsw->d_name, dev2unit(dev), devtoname(dev))); dev->si_flags |= SI_NAMED; - if (cr != NULL) - dev->si_cred = crhold(cr); - dev->si_uid = uid; - dev->si_gid = gid; - dev->si_mode = mode; + if (args.mda_cr != NULL) + dev->si_cred = crhold(args.mda_cr); + dev->si_uid = args.mda_uid; + dev->si_gid = args.mda_gid; + dev->si_mode = args.mda_mode; devfs_create(dev); clean_unrhdrl(devfs_inos); dev_unlock_and_free(); - notify_create(dev, flags); + notify_create(dev, args.mda_flags); *dres = dev; return (0); } +int +make_dev_s(struct make_dev_args *args, struct cdev **dres, + const char *fmt, ...) +{ + va_list ap; + int res; + + va_start(ap, fmt); + res = make_dev_sv(args, dres, fmt, ap); + va_end(ap); + return (res); +} + +static int +make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit, + struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, + va_list ap) +{ + struct make_dev_args args; + + make_dev_args_init(&args); + args.mda_flags = flags; + args.mda_devsw = devsw; + args.mda_cr = cr; + args.mda_uid = uid; + args.mda_gid = gid; + args.mda_mode = mode; + args.mda_unit = unit; + return (make_dev_sv(&args, dres, fmt, ap)); +} + struct cdev * make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode, const char *fmt, ...) @@ -1247,6 +1295,7 @@ { struct clonedevs *cd; struct cdev *dev, *ndev, *dl, *de; + struct make_dev_args args; int unit, low, u; KASSERT(*cdp != NULL, @@ -1298,7 +1347,10 @@ } if (unit == -1) unit = low & CLONE_UNITMASK; - dev = newdev(csw, unit | extra, ndev); + make_dev_args_init(&args); + args.mda_unit = unit | extra; + args.mda_devsw = csw; + dev = newdev(&args, ndev); if (dev->si_flags & SI_CLONELIST) { printf("dev %p (%s) is on clonelist\n", dev, dev->si_name); printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra); Index: sys/kern/tty.c =================================================================== --- sys/kern/tty.c +++ sys/kern/tty.c @@ -237,14 +237,10 @@ ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) { struct tty *tp; - int error = 0; - - while ((tp = dev->si_drv1) == NULL) { - error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1); - if (error != EWOULDBLOCK) - return (error); - } + int error; + tp = dev->si_drv1; + error = 0; tty_lock(tp); if (tty_gone(tp)) { /* Device is already gone. */ @@ -755,13 +751,10 @@ ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td) { struct tty *tp; - int error = 0; + int error; - while ((tp = dev->si_drv1) == NULL) { - error = tsleep(&dev->si_drv1, PCATCH, "ttdrv1", 1); - if (error != EWOULDBLOCK) - return (error); - } + tp = dev->si_drv1; + error = 0; tty_lock(tp); if (tty_gone(tp)) error = ENODEV; @@ -1189,6 +1182,7 @@ const char *fmt, ...) { va_list ap; + struct make_dev_args args; struct cdev *dev, *init, *lock, *cua, *cinit, *clock; const char *prefix = "tty"; char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */ @@ -1221,71 +1215,72 @@ flags |= MAKEDEV_CHECKNAME; /* Master call-in device. */ - error = make_dev_p(flags, &dev, &ttydev_cdevsw, cred, uid, gid, mode, - "%s%s", prefix, name); - if (error) + make_dev_args_init(&args); + args.mda_flags = flags; + args.mda_devsw = &ttydev_cdevsw; + args.mda_cr = cred; + args.mda_uid = uid; + args.mda_gid = gid; + args.mda_mode = mode; + args.mda_si_drv1 = tp; + error = make_dev_s(&args, &dev, "%s%s", prefix, name); + if (error != 0) return (error); - dev->si_drv1 = tp; - wakeup(&dev->si_drv1); tp->t_dev = dev; init = lock = cua = cinit = clock = NULL; /* Slave call-in devices. */ if (tp->t_flags & TF_INITLOCK) { - error = make_dev_p(flags, &init, &ttyil_cdevsw, cred, uid, - gid, mode, "%s%s.init", prefix, name); - if (error) + args.mda_devsw = &ttyil_cdevsw; + args.mda_unit = TTYUNIT_INIT; + args.mda_si_drv1 = tp; + args.mda_si_drv2 = &tp->t_termios_init_in; + error = make_dev_s(&args, &init, "%s%s.init", prefix, name); + if (error != 0) goto fail; dev_depends(dev, init); - dev2unit(init) = TTYUNIT_INIT; - init->si_drv1 = tp; - wakeup(&init->si_drv1); - init->si_drv2 = &tp->t_termios_init_in; - error = make_dev_p(flags, &lock, &ttyil_cdevsw, cred, uid, - gid, mode, "%s%s.lock", prefix, name); - if (error) + args.mda_unit = TTYUNIT_LOCK; + args.mda_si_drv2 = &tp->t_termios_lock_in; + error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name); + if (error != 0) goto fail; dev_depends(dev, lock); - dev2unit(lock) = TTYUNIT_LOCK; - lock->si_drv1 = tp; - wakeup(&lock->si_drv1); - lock->si_drv2 = &tp->t_termios_lock_in; } /* Call-out devices. */ if (tp->t_flags & TF_CALLOUT) { - error = make_dev_p(flags, &cua, &ttydev_cdevsw, cred, - UID_UUCP, GID_DIALER, 0660, "cua%s", name); - if (error) + make_dev_args_init(&args); + args.mda_flags = flags; + args.mda_devsw = &ttydev_cdevsw; + args.mda_cr = cred; + args.mda_uid = UID_UUCP; + args.mda_gid = GID_DIALER; + args.mda_mode = 0660; + args.mda_unit = TTYUNIT_CALLOUT; + args.mda_si_drv1 = tp; + error = make_dev_s(&args, &cua, "cua%s", name); + if (error != 0) goto fail; dev_depends(dev, cua); - dev2unit(cua) = TTYUNIT_CALLOUT; - cua->si_drv1 = tp; - wakeup(&cua->si_drv1); /* Slave call-out devices. */ if (tp->t_flags & TF_INITLOCK) { - error = make_dev_p(flags, &cinit, &ttyil_cdevsw, cred, - UID_UUCP, GID_DIALER, 0660, "cua%s.init", name); - if (error) + args.mda_devsw = &ttyil_cdevsw; + args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT; + args.mda_si_drv2 = &tp->t_termios_init_out; + error = make_dev_s(&args, &cinit, "cua%s.init", name); + if (error != 0) goto fail; dev_depends(dev, cinit); - dev2unit(cinit) = TTYUNIT_CALLOUT | TTYUNIT_INIT; - cinit->si_drv1 = tp; - wakeup(&cinit->si_drv1); - cinit->si_drv2 = &tp->t_termios_init_out; - error = make_dev_p(flags, &clock, &ttyil_cdevsw, cred, - UID_UUCP, GID_DIALER, 0660, "cua%s.lock", name); - if (error) + args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK; + args.mda_si_drv2 = &tp->t_termios_lock_out; + error = make_dev_s(&args, &clock, "cua%s.lock", name); + if (error != 0) goto fail; dev_depends(dev, clock); - dev2unit(clock) = TTYUNIT_CALLOUT | TTYUNIT_LOCK; - clock->si_drv1 = tp; - wakeup(&clock->si_drv1); - clock->si_drv2 = &tp->t_termios_lock_out; } } Index: sys/sys/conf.h =================================================================== --- sys/sys/conf.h +++ sys/sys/conf.h @@ -226,6 +226,28 @@ #define CLONE_FLAG0 (CLONE_UNITMASK + 1) int clone_create(struct clonedevs **, struct cdevsw *, int *unit, struct cdev **dev, int extra); +#define MAKEDEV_REF 0x01 +#define MAKEDEV_WHTOUT 0x02 +#define MAKEDEV_NOWAIT 0x04 +#define MAKEDEV_WAITOK 0x08 +#define MAKEDEV_ETERNAL 0x10 +#define MAKEDEV_CHECKNAME 0x20 +struct make_dev_args { + size_t mda_size; + int mda_flags; + struct cdevsw *mda_devsw; + struct ucred *mda_cr; + uid_t mda_uid; + gid_t mda_gid; + int mda_mode; + int mda_unit; + void *mda_si_drv1; + void *mda_si_drv2; +}; +void make_dev_args_init_impl(struct make_dev_args *_args, size_t _sz); +#define make_dev_args_init(a) \ + make_dev_args_init_impl((a), sizeof(struct make_dev_args)) + int count_dev(struct cdev *_dev); void delist_dev(struct cdev *_dev); void destroy_dev(struct cdev *_dev); @@ -245,12 +267,6 @@ struct cdev *make_dev_cred(struct cdevsw *_devsw, int _unit, struct ucred *_cr, uid_t _uid, gid_t _gid, int _perms, const char *_fmt, ...) __printflike(7, 8); -#define MAKEDEV_REF 0x01 -#define MAKEDEV_WHTOUT 0x02 -#define MAKEDEV_NOWAIT 0x04 -#define MAKEDEV_WAITOK 0x08 -#define MAKEDEV_ETERNAL 0x10 -#define MAKEDEV_CHECKNAME 0x20 struct cdev *make_dev_credf(int _flags, struct cdevsw *_devsw, int _unit, struct ucred *_cr, uid_t _uid, gid_t _gid, int _mode, @@ -258,6 +274,8 @@ int make_dev_p(int _flags, struct cdev **_cdev, struct cdevsw *_devsw, struct ucred *_cr, uid_t _uid, gid_t _gid, int _mode, const char *_fmt, ...) __printflike(8, 9); +int make_dev_s(struct make_dev_args *_args, struct cdev **_cdev, + const char *_fmt, ...) __printflike(3, 4); struct cdev *make_dev_alias(struct cdev *_pdev, const char *_fmt, ...) __printflike(2, 3); int make_dev_alias_p(int _flags, struct cdev **_cdev, struct cdev *_pdev,