xref: /titanic_41/usr/src/cmd/devfsadm/tape_link.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <devfsadm.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <limits.h>
32*45916cd2Sjpk #include <bsm/devalloc.h>
337c478bd9Sstevel@tonic-gate 
34*45916cd2Sjpk extern int system_labeled;
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static int tape_process(di_minor_t minor, di_node_t node);
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate static devfsadm_create_t tape_cbt[] = {
397c478bd9Sstevel@tonic-gate 	{ "tape", "ddi_byte:tape", NULL,
407c478bd9Sstevel@tonic-gate 	TYPE_EXACT, ILEVEL_0,	tape_process
417c478bd9Sstevel@tonic-gate 	},
427c478bd9Sstevel@tonic-gate };
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(tape_cbt);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	TAPE_LINK_RE "^rmt/[0-9]+[cbhlmnu]*"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static devfsadm_remove_t tape_remove_cbt[] = {
497c478bd9Sstevel@tonic-gate 	{ "tape", TAPE_LINK_RE, RM_PRE, ILEVEL_0, devfsadm_rm_all
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate };
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(tape_remove_cbt);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * This function is called for every tape minor node.
587c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical tape id, and then
597c478bd9Sstevel@tonic-gate  * devfsadm_mklink to make the link.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate static int
tape_process(di_minor_t minor,di_node_t node)627c478bd9Sstevel@tonic-gate tape_process(di_minor_t minor, di_node_t node)
637c478bd9Sstevel@tonic-gate {
64*45916cd2Sjpk 	int flags = 0;
657c478bd9Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
667c478bd9Sstevel@tonic-gate 	char *buf;
677c478bd9Sstevel@tonic-gate 	char *mn;
687c478bd9Sstevel@tonic-gate 	char *devfspath;
697c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {"rmt/([0-9]+)", 1, MATCH_ADDR};
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((mn != NULL) && (*mn >= '0') && (*mn <= '9')) {
757c478bd9Sstevel@tonic-gate 		/*
767c478bd9Sstevel@tonic-gate 		 * first character cannot be a digit as it would combine
777c478bd9Sstevel@tonic-gate 		 * with the tape instance number to make an ambiguous quantity.
787c478bd9Sstevel@tonic-gate 		 */
797c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	devfspath = di_devfs_path(node);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, devfspath);
857c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, ":");
867c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, mn);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 *  devfsadm_enumerate finds the logical tape id from the physical path,
927c478bd9Sstevel@tonic-gate 	 *  omitting minor name field. The logical tape id is returned in buf.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	if (devfsadm_enumerate_int(l_path, 0, &buf, rules, 1)) {
957c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, "rmt/");
997c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, buf);
1007c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, mn);
1017c478bd9Sstevel@tonic-gate 	free(buf);
1027c478bd9Sstevel@tonic-gate 
103*45916cd2Sjpk 	if (system_labeled)
104*45916cd2Sjpk 		flags = DA_ADD|DA_TAPE;
105*45916cd2Sjpk 
106*45916cd2Sjpk 	(void) devfsadm_mklink(l_path, node, minor, flags);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1097c478bd9Sstevel@tonic-gate }
110