145916cd2Sjpk /*
245916cd2Sjpk * CDDL HEADER START
345916cd2Sjpk *
445916cd2Sjpk * The contents of this file are subject to the terms of the
545916cd2Sjpk * Common Development and Distribution License (the "License").
645916cd2Sjpk * You may not use this file except in compliance with the License.
745916cd2Sjpk *
845916cd2Sjpk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945916cd2Sjpk * or http://www.opensolaris.org/os/licensing.
1045916cd2Sjpk * See the License for the specific language governing permissions
1145916cd2Sjpk * and limitations under the License.
1245916cd2Sjpk *
1345916cd2Sjpk * When distributing Covered Code, include this CDDL HEADER in each
1445916cd2Sjpk * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545916cd2Sjpk * If applicable, add the following below this CDDL HEADER, with the
1645916cd2Sjpk * fields enclosed by brackets "[]" replaced with your own identifying
1745916cd2Sjpk * information: Portions Copyright [yyyy] [name of copyright owner]
1845916cd2Sjpk *
1945916cd2Sjpk * CDDL HEADER END
2045916cd2Sjpk */
2145916cd2Sjpk
2245916cd2Sjpk /*
237e3e5701SJan Parcel * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2445916cd2Sjpk * Use is subject to license terms.
2545916cd2Sjpk */
2645916cd2Sjpk
2745916cd2Sjpk /*
2845916cd2Sjpk * Device allocation related work.
2945916cd2Sjpk */
3045916cd2Sjpk
3145916cd2Sjpk #include <stdio.h>
3245916cd2Sjpk #include <stdlib.h>
3345916cd2Sjpk #include <errno.h>
3445916cd2Sjpk #include <string.h>
3545916cd2Sjpk #include <strings.h>
3645916cd2Sjpk #include <unistd.h>
3745916cd2Sjpk #include <fcntl.h>
3845916cd2Sjpk #include <sys/types.h>
3945916cd2Sjpk #include <sys/stat.h>
4045916cd2Sjpk #include <sys/dkio.h>
4145916cd2Sjpk #include <sys/wait.h>
4245916cd2Sjpk #include <bsm/devalloc.h>
4345916cd2Sjpk
4445916cd2Sjpk #define DEALLOCATE "/usr/sbin/deallocate"
4545916cd2Sjpk #define MKDEVALLOC "/usr/sbin/mkdevalloc"
4645916cd2Sjpk
477e3e5701SJan Parcel static char *_update_dev(deventry_t *, int, const char *, char *, char *);
4845916cd2Sjpk static int _make_db();
497e3e5701SJan Parcel extern int event_driven;
5045916cd2Sjpk
5145916cd2Sjpk
5245916cd2Sjpk /*
5345916cd2Sjpk * _da_check_for_usb
546e670f77Saj * returns 1 if device pointed by 'link' is a removable hotplugged disk,
5545916cd2Sjpk * else returns 0.
5645916cd2Sjpk */
5745916cd2Sjpk int
_da_check_for_usb(char * link,char * root_dir)5845916cd2Sjpk _da_check_for_usb(char *link, char *root_dir)
5945916cd2Sjpk {
6045916cd2Sjpk int fd = -1;
6145916cd2Sjpk int len, dstsize;
6245916cd2Sjpk int removable = 0;
636e670f77Saj int hotpluggable = 0;
6445916cd2Sjpk char *p = NULL;
656e670f77Saj char path[MAXPATHLEN + 4];
666e670f77Saj char rpath[MAXPATHLEN + 4]; /* for ",raw" */
6745916cd2Sjpk
6845916cd2Sjpk dstsize = sizeof (path);
6945916cd2Sjpk if (strcmp(root_dir, "") != 0) {
7045916cd2Sjpk if (strlcat(path, root_dir, dstsize) >= dstsize)
7145916cd2Sjpk return (0);
7245916cd2Sjpk len = strlen(path);
7345916cd2Sjpk } else {
7445916cd2Sjpk len = 0;
7545916cd2Sjpk }
7645916cd2Sjpk (void) snprintf(path, dstsize - len, "%s", link);
776e670f77Saj if ((p = realpath(path, rpath)) == NULL) {
786e670f77Saj p = path;
7945916cd2Sjpk } else {
806e670f77Saj if (strstr(link, "rdsk")) {
816e670f77Saj p = rpath;
826e670f77Saj } else {
836e670f77Saj (void) snprintf(path, dstsize, "%s%s", rpath, ",raw");
846e670f77Saj p = path;
8545916cd2Sjpk }
866e670f77Saj }
876e670f77Saj if ((fd = open(p, O_RDONLY | O_NONBLOCK)) < 0)
8845916cd2Sjpk return (0);
8945916cd2Sjpk (void) ioctl(fd, DKIOCREMOVABLE, &removable);
906e670f77Saj (void) ioctl(fd, DKIOCHOTPLUGGABLE, &hotpluggable);
9145916cd2Sjpk (void) close(fd);
9245916cd2Sjpk
936e670f77Saj if (removable && hotpluggable)
946e670f77Saj return (1);
956e670f77Saj
966e670f77Saj return (0);
9745916cd2Sjpk }
9845916cd2Sjpk
9945916cd2Sjpk /*
10045916cd2Sjpk * _reset_devalloc
10145916cd2Sjpk * If device allocation is being turned on, creates device_allocate
10245916cd2Sjpk * device_maps if they do not exist.
10345916cd2Sjpk * Puts DEVICE_ALLOCATION=ON/OFF in device_allocate to indicate if
10445916cd2Sjpk * device allocation is on/off.
10545916cd2Sjpk */
10645916cd2Sjpk void
_reset_devalloc(int action)10745916cd2Sjpk _reset_devalloc(int action)
10845916cd2Sjpk {
10945916cd2Sjpk da_args dargs;
11045916cd2Sjpk
11145916cd2Sjpk if (action == DA_ON)
11245916cd2Sjpk (void) _make_db();
11345916cd2Sjpk else if ((action == DA_OFF) && (open(DEVALLOC, O_RDONLY) == -1))
11445916cd2Sjpk return;
11545916cd2Sjpk
11645916cd2Sjpk if (action == DA_ON)
11745916cd2Sjpk dargs.optflag = DA_ON;
11845916cd2Sjpk else if (action == DA_OFF)
11945916cd2Sjpk dargs.optflag = DA_OFF | DA_ALLOC_ONLY;
12045916cd2Sjpk
12145916cd2Sjpk dargs.rootdir = NULL;
12245916cd2Sjpk dargs.devnames = NULL;
12345916cd2Sjpk dargs.devinfo = NULL;
12445916cd2Sjpk
12545916cd2Sjpk (void) da_update_device(&dargs);
12645916cd2Sjpk }
12745916cd2Sjpk
12845916cd2Sjpk /*
12945916cd2Sjpk * _make_db
13045916cd2Sjpk * execs /usr/sbin/mkdevalloc to create device_allocate and
13145916cd2Sjpk * device_maps.
13245916cd2Sjpk */
13345916cd2Sjpk static int
_make_db()13445916cd2Sjpk _make_db()
13545916cd2Sjpk {
13645916cd2Sjpk int status;
13745916cd2Sjpk pid_t pid, wpid;
13845916cd2Sjpk
13945916cd2Sjpk pid = vfork();
14045916cd2Sjpk switch (pid) {
14145916cd2Sjpk case -1:
14245916cd2Sjpk return (1);
14345916cd2Sjpk case 0:
14445916cd2Sjpk if (execl(MKDEVALLOC, MKDEVALLOC, DA_IS_LABELED, NULL) == -1)
14545916cd2Sjpk exit((errno == ENOENT) ? 0 : 1);
146*902eb929SToomas Soome return (1);
14745916cd2Sjpk default:
14845916cd2Sjpk for (;;) {
14945916cd2Sjpk wpid = waitpid(pid, &status, 0);
15045916cd2Sjpk if (wpid == (pid_t)-1) {
15145916cd2Sjpk if (errno == EINTR)
15245916cd2Sjpk continue;
15345916cd2Sjpk else
15445916cd2Sjpk return (1);
15545916cd2Sjpk } else {
15645916cd2Sjpk break;
15745916cd2Sjpk }
15845916cd2Sjpk }
15945916cd2Sjpk break;
16045916cd2Sjpk }
16145916cd2Sjpk
16245916cd2Sjpk return ((WIFEXITED(status) == 0) ? 1 : WEXITSTATUS(status));
16345916cd2Sjpk }
16445916cd2Sjpk
16545916cd2Sjpk
16645916cd2Sjpk /*
16745916cd2Sjpk * _update_devalloc_db
16845916cd2Sjpk * Forms allocatable device entries to be written to device_allocate and
16945916cd2Sjpk * device_maps.
1707e3e5701SJan Parcel *
1717e3e5701SJan Parcel * Or finds the correct entry to remove, and removes it.
1727e3e5701SJan Parcel *
1737e3e5701SJan Parcel * Note: devname is a /devices link in the REMOVE case.
17445916cd2Sjpk */
17545916cd2Sjpk /* ARGSUSED */
17645916cd2Sjpk void
_update_devalloc_db(devlist_t * devlist,int devflag,int action,char * devname,char * root_dir)17745916cd2Sjpk _update_devalloc_db(devlist_t *devlist, int devflag, int action, char *devname,
17845916cd2Sjpk char *root_dir)
17945916cd2Sjpk {
18045916cd2Sjpk int i;
18145916cd2Sjpk deventry_t *entry = NULL, *dentry = NULL;
1827e3e5701SJan Parcel char *typestring;
1837e3e5701SJan Parcel char *nickname; /* typestring + instance */
18445916cd2Sjpk
18545916cd2Sjpk if (action == DA_ADD) {
18645916cd2Sjpk for (i = 0; i < DA_COUNT; i++) {
18745916cd2Sjpk switch (i) {
18845916cd2Sjpk case 0:
18945916cd2Sjpk dentry = devlist->audio;
19045916cd2Sjpk break;
19145916cd2Sjpk case 1:
19245916cd2Sjpk dentry = devlist->cd;
19345916cd2Sjpk break;
19445916cd2Sjpk case 2:
19545916cd2Sjpk dentry = devlist->floppy;
19645916cd2Sjpk break;
19745916cd2Sjpk case 3:
19845916cd2Sjpk dentry = devlist->tape;
19945916cd2Sjpk break;
20045916cd2Sjpk case 4:
20145916cd2Sjpk dentry = devlist->rmdisk;
20245916cd2Sjpk break;
20345916cd2Sjpk default:
20445916cd2Sjpk return;
20545916cd2Sjpk }
20645916cd2Sjpk if (dentry)
2077e3e5701SJan Parcel (void) _update_dev(dentry, action, NULL, NULL,
2087e3e5701SJan Parcel NULL);
20945916cd2Sjpk }
21045916cd2Sjpk } else if (action == DA_REMOVE) {
2117e3e5701SJan Parcel if (devflag & DA_AUDIO) {
21245916cd2Sjpk dentry = devlist->audio;
2137e3e5701SJan Parcel typestring = DA_AUDIO_TYPE;
2147e3e5701SJan Parcel } else if (devflag & DA_CD) {
21545916cd2Sjpk dentry = devlist->cd;
2167e3e5701SJan Parcel typestring = DA_CD_TYPE;
2177e3e5701SJan Parcel } else if (devflag & DA_FLOPPY) {
21845916cd2Sjpk dentry = devlist->floppy;
2197e3e5701SJan Parcel typestring = DA_FLOPPY_TYPE;
2207e3e5701SJan Parcel } else if (devflag & DA_TAPE) {
22145916cd2Sjpk dentry = devlist->tape;
2227e3e5701SJan Parcel typestring = DA_TAPE_TYPE;
2237e3e5701SJan Parcel } else if (devflag & DA_RMDISK) {
22445916cd2Sjpk dentry = devlist->rmdisk;
2257e3e5701SJan Parcel typestring = DA_RMDISK_TYPE;
2267e3e5701SJan Parcel } else
22745916cd2Sjpk return;
22845916cd2Sjpk
2297e3e5701SJan Parcel if (event_driven) {
2307e3e5701SJan Parcel nickname = _update_dev(NULL, action, typestring, NULL,
2317e3e5701SJan Parcel devname);
2327e3e5701SJan Parcel
2337e3e5701SJan Parcel if (nickname != NULL) {
2347e3e5701SJan Parcel (void) da_rm_list_entry(devlist, devname,
2357e3e5701SJan Parcel devflag, nickname);
2367e3e5701SJan Parcel free(nickname);
2377e3e5701SJan Parcel }
2387e3e5701SJan Parcel return;
2397e3e5701SJan Parcel }
2407e3e5701SJan Parcel /*
2417e3e5701SJan Parcel * Not reached as of now, could be reached if devfsadm is
2427e3e5701SJan Parcel * enhanced to clean up devalloc database more thoroughly.
2437e3e5701SJan Parcel * Will not reliably match for event-driven removes
2447e3e5701SJan Parcel */
24545916cd2Sjpk for (entry = dentry; entry != NULL; entry = entry->next) {
24645916cd2Sjpk if (strcmp(entry->devinfo.devname, devname) == 0)
24745916cd2Sjpk break;
24845916cd2Sjpk }
2497e3e5701SJan Parcel (void) _update_dev(entry, action, NULL, devname, NULL);
25045916cd2Sjpk }
25145916cd2Sjpk }
25245916cd2Sjpk
2537e3e5701SJan Parcel /*
2547e3e5701SJan Parcel * _update_dev: Update device_allocate and/or device_maps files
2557e3e5701SJan Parcel *
2567e3e5701SJan Parcel * If adding a device:
2577e3e5701SJan Parcel * dentry: A linked list of allocatable devices
2587e3e5701SJan Parcel * action: DA_ADD or DA_REMOVE
2597e3e5701SJan Parcel * devtype: type of device linked list to update on removal
2607e3e5701SJan Parcel * devname: short name (i.e. rmdisk5, cdrom0) of device if known
2617e3e5701SJan Parcel * rm_link: name of real /device from hot_cleanup
2627e3e5701SJan Parcel *
2637e3e5701SJan Parcel * If the action is ADD or if the action is triggered by an event
2647e3e5701SJan Parcel * from syseventd, read the files FIRST and treat their data as
2657e3e5701SJan Parcel * more-accurate than the dentry list, adjusting dentry contents if needed.
2667e3e5701SJan Parcel *
2677e3e5701SJan Parcel * For DA_ADD, try to add each device in the list to the files.
2687e3e5701SJan Parcel *
2697e3e5701SJan Parcel * If the action is DA_REMOVE and not a hotplug remove, adjust the files
2707e3e5701SJan Parcel * as indicated by the linked list.
2717e3e5701SJan Parcel *
2727e3e5701SJan Parcel * RETURNS:
2737e3e5701SJan Parcel * If we successfully remove a device from the files, returns
2747e3e5701SJan Parcel * a char * to strdup'd devname of the device removed.
2757e3e5701SJan Parcel *
2767e3e5701SJan Parcel * The caller is responsible for freeing the return value.
2777e3e5701SJan Parcel *
2787e3e5701SJan Parcel * NULL for all other cases, both success and failure.
2797e3e5701SJan Parcel *
2807e3e5701SJan Parcel */
2817e3e5701SJan Parcel static char *
_update_dev(deventry_t * dentry,int action,const char * devtype,char * devname,char * rm_link)2827e3e5701SJan Parcel _update_dev(deventry_t *dentry, int action, const char *devtype, char *devname,
2837e3e5701SJan Parcel char *rm_link)
28445916cd2Sjpk {
28545916cd2Sjpk da_args dargs;
28645916cd2Sjpk deventry_t newentry, *entry;
2877e3e5701SJan Parcel int status;
28845916cd2Sjpk
28945916cd2Sjpk dargs.rootdir = NULL;
29045916cd2Sjpk dargs.devnames = NULL;
29145916cd2Sjpk
2927e3e5701SJan Parcel if (event_driven)
2937e3e5701SJan Parcel dargs.optflag = DA_EVENT;
2947e3e5701SJan Parcel else
2957e3e5701SJan Parcel dargs.optflag = 0;
2967e3e5701SJan Parcel
29745916cd2Sjpk if (action == DA_ADD) {
2987e3e5701SJan Parcel dargs.optflag |= DA_ADD;
2997e3e5701SJan Parcel /*
3007e3e5701SJan Parcel * Add Events do not have enough information to overrride the
3017e3e5701SJan Parcel * existing file contents.
3027e3e5701SJan Parcel */
3037e3e5701SJan Parcel
30445916cd2Sjpk for (entry = dentry; entry != NULL; entry = entry->next) {
30545916cd2Sjpk dargs.devinfo = &(entry->devinfo);
30645916cd2Sjpk (void) da_update_device(&dargs);
30745916cd2Sjpk }
30845916cd2Sjpk } else if (action == DA_REMOVE) {
3097e3e5701SJan Parcel dargs.optflag |= DA_REMOVE;
31045916cd2Sjpk if (dentry) {
31145916cd2Sjpk entry = dentry;
3127e3e5701SJan Parcel } else if (dargs.optflag & DA_EVENT) {
3137e3e5701SJan Parcel if (devname == NULL)
3147e3e5701SJan Parcel newentry.devinfo.devname = NULL;
3157e3e5701SJan Parcel else
3167e3e5701SJan Parcel newentry.devinfo.devname = strdup(devname);
3177e3e5701SJan Parcel newentry.devinfo.devtype = (char *)devtype;
3187e3e5701SJan Parcel newentry.devinfo.devauths =
3197e3e5701SJan Parcel newentry.devinfo.devopts =
3207e3e5701SJan Parcel newentry.devinfo.devexec = NULL;
3217e3e5701SJan Parcel newentry.devinfo.devlist = strdup(rm_link);
3227e3e5701SJan Parcel newentry.devinfo.instance = 0;
3237e3e5701SJan Parcel newentry.next = NULL;
3247e3e5701SJan Parcel entry = &newentry;
32545916cd2Sjpk } else {
32645916cd2Sjpk newentry.devinfo.devname = strdup(devname);
3277e3e5701SJan Parcel newentry.devinfo.devtype = (char *)devtype;
32845916cd2Sjpk newentry.devinfo.devauths =
32945916cd2Sjpk newentry.devinfo.devexec =
33045916cd2Sjpk newentry.devinfo.devopts =
33145916cd2Sjpk newentry.devinfo.devlist = NULL;
33245916cd2Sjpk newentry.devinfo.instance = 0;
33345916cd2Sjpk newentry.next = NULL;
33445916cd2Sjpk entry = &newentry;
33545916cd2Sjpk }
33645916cd2Sjpk dargs.devinfo = &(entry->devinfo);
3377e3e5701SJan Parcel /*
3387e3e5701SJan Parcel * da_update_device will fill in entry devname if
3397e3e5701SJan Parcel * event_driven is true and device is in the file
3407e3e5701SJan Parcel */
3417e3e5701SJan Parcel status = da_update_device(&dargs);
3427e3e5701SJan Parcel if (event_driven)
3437e3e5701SJan Parcel if (newentry.devinfo.devlist != NULL)
3447e3e5701SJan Parcel free(newentry.devinfo.devlist);
3457e3e5701SJan Parcel if (status == 0)
3467e3e5701SJan Parcel return (dargs.devinfo->devname);
3477e3e5701SJan Parcel else free(dargs.devinfo->devname);
34845916cd2Sjpk }
3497e3e5701SJan Parcel return (NULL);
35045916cd2Sjpk }
351