1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <meta.h> 29 #include <regex.h> 30 #include <devfsadm.h> 31 #include <stdio.h> 32 #include <strings.h> 33 #include <stdlib.h> 34 #include <limits.h> 35 #include <sys/mkdev.h> 36 37 #define MD_LINK_RE_DEVICES "^md/r?dsk/.+$" 38 #define MD_LINK_RE_SHARED "^md/shared/[0-9]+/r?dsk/.+$" 39 #define MD_LINK_RE_ADMIN "^md/admin" 40 41 /* 42 * The devfsadm link module require the next section to 43 * be defined in order to know what and when to call functions 44 * in the module on device creation and removal. 45 */ 46 47 /* setup for device creation */ 48 49 static int md_create(di_minor_t minor, di_node_t node); 50 51 static devfsadm_create_t md_cbt[] = { 52 { "pseudo", "ddi_pseudo", "md", 53 TYPE_EXACT | DRV_EXACT, ILEVEL_0, md_create, 54 }, 55 }; 56 57 DEVFSADM_CREATE_INIT_V0(md_cbt); 58 59 /* 60 * remove devices - always allow disks to be dynamically removed. Only allow 61 * admin device to be removed at reboot. 62 */ 63 64 static devfsadm_remove_t md_remove_cbt[] = { 65 {"pseudo", MD_LINK_RE_DEVICES, RM_ALWAYS | RM_PRE | RM_HOT, 66 ILEVEL_0, devfsadm_rm_all}, 67 {"pseudo", MD_LINK_RE_SHARED, RM_ALWAYS | RM_PRE | RM_HOT, 68 ILEVEL_0, devfsadm_rm_all}, 69 {"pseudo", MD_LINK_RE_ADMIN, RM_ALWAYS | RM_PRE, 70 ILEVEL_0, devfsadm_rm_all}, 71 }; 72 73 DEVFSADM_REMOVE_INIT_V0(md_remove_cbt); 74 75 76 /* 77 * For the admin device: 78 * /dev/md/admin -> /devices/pseudo/md@0:admin 79 * 80 * For metadevice: 81 * /dev/md/dsk/foobar --> /devices/pseudo/md@0:0,100,blk 82 * /dev/md/rdsk/foobar --> /devices/pseudo/md@0:0,100,raw 83 * 84 * Where 'foobar' is user specified arbitrary name and '100' 85 * is the minor number returned by MD_IOCMAKE_DEV ioctl 86 * 87 */ 88 static int 89 md_create(di_minor_t minor, di_node_t node) 90 { 91 char mn[MAXNAMELEN + 1]; 92 char path[PATH_MAX + 1]; 93 char set_path[PATH_MAX +1]; 94 char sym_path[PATH_MAX + 1]; 95 int set = -1, ret; 96 char *type, *dir; 97 char *device_name; 98 dev_t minor_devt = di_minor_devt(minor); 99 int key; 100 mdsetname_t *sp = NULL; 101 md_error_t ep; 102 103 (void) strcpy(mn, di_minor_name(minor)); 104 105 /* 106 * Check whether we are being requested to setup the admin 107 * device link or one of the metadevice links. They need 108 * to be treated differently. 109 */ 110 111 if (strcmp(mn, "admin") == 0) { 112 /* there is only one admin link and always in /dev/md/admin */ 113 (void) devfsadm_mklink("md/admin", node, minor, 0); 114 } else { 115 /* 116 * Extract out the minor components and create the 117 * appropriate links. The node looks like: 118 * md@<set>,<mdev>,<type> 119 * where the <set> number is the named diskset, 120 * <mdev> is the metadevice number, and <type> 121 * is the trailing "blk" or "raw" indication. 122 * 123 * NOTE: when <set> is non-zero, we need to create 124 * under the "shared" directory entry instead of linking 125 * into the top level dsk/rdsk directories. 126 */ 127 ret = sscanf(mn, "%d,", &set); 128 if (ret == 1 && (type = strrchr(mn, ',')) != NULL) { 129 type++; 130 if (strcmp(type, "blk") == 0) { 131 dir = "dsk"; 132 } else { 133 dir = "rdsk"; 134 } 135 136 (void) memset(&ep, '\0', sizeof (ep)); 137 if ((device_name = meta_getnmentbydev(set, 138 MD_SIDEWILD, minor_devt, NULL, NULL, 139 &key, &ep)) == NULL) { 140 (void) close_admin(&ep); 141 return (DEVFSADM_CONTINUE); 142 } 143 144 if (set == 0) { 145 /* this is a simple md */ 146 (void) snprintf(path, sizeof (path), 147 "md/%s/%s", dir, basename(device_name)); 148 } else { 149 /* this is a shared md */ 150 (void) snprintf(path, sizeof (path), 151 "md/shared/%d/%s/%s", set, dir, 152 basename(device_name)); 153 154 /* 155 * flush the caches so the next call to 156 * metasetnosetname will get us the 157 * updated cache. 158 */ 159 metaflushnames(0); 160 if ((sp = metasetnosetname(set, &ep)) 161 != NULL) { 162 (void) snprintf(set_path, 163 sizeof (set_path), "md/shared/%d", 164 sp->setno); 165 (void) snprintf(sym_path, 166 sizeof (sym_path), "md/%s", 167 sp->setname); 168 } 169 } 170 (void) devfsadm_mklink(path, node, minor, 0); 171 Free(device_name); 172 173 if (sp != NULL) { 174 (void) devfsadm_secondary_link(sym_path, 175 set_path, 0); 176 } 177 } 178 } 179 180 (void) close_admin(&ep); 181 return (DEVFSADM_CONTINUE); 182 } 183