xref: /illumos-gate/usr/src/cmd/devfsadm/tape_link.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 #include <devfsadm.h>
277c478bd9Sstevel@tonic-gate #include <strings.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <limits.h>
30*45916cd2Sjpk #include <bsm/devalloc.h>
317c478bd9Sstevel@tonic-gate 
32*45916cd2Sjpk extern int system_labeled;
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static int tape_process(di_minor_t minor, di_node_t node);
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static devfsadm_create_t tape_cbt[] = {
377c478bd9Sstevel@tonic-gate 	{ "tape", "ddi_byte:tape", NULL,
387c478bd9Sstevel@tonic-gate 	TYPE_EXACT, ILEVEL_0,	tape_process
397c478bd9Sstevel@tonic-gate 	},
407c478bd9Sstevel@tonic-gate };
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate DEVFSADM_CREATE_INIT_V0(tape_cbt);
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #define	TAPE_LINK_RE "^rmt/[0-9]+[cbhlmnu]*"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static devfsadm_remove_t tape_remove_cbt[] = {
477c478bd9Sstevel@tonic-gate 	{ "tape", TAPE_LINK_RE, RM_PRE, ILEVEL_0, devfsadm_rm_all
487c478bd9Sstevel@tonic-gate 	}
497c478bd9Sstevel@tonic-gate };
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate DEVFSADM_REMOVE_INIT_V0(tape_remove_cbt);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * This function is called for every tape minor node.
567c478bd9Sstevel@tonic-gate  * Calls enumerate to assign a logical tape id, and then
577c478bd9Sstevel@tonic-gate  * devfsadm_mklink to make the link.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate static int
tape_process(di_minor_t minor,di_node_t node)607c478bd9Sstevel@tonic-gate tape_process(di_minor_t minor, di_node_t node)
617c478bd9Sstevel@tonic-gate {
62*45916cd2Sjpk 	int flags = 0;
637c478bd9Sstevel@tonic-gate 	char l_path[PATH_MAX + 1];
647c478bd9Sstevel@tonic-gate 	char *buf;
657c478bd9Sstevel@tonic-gate 	char *mn;
667c478bd9Sstevel@tonic-gate 	char *devfspath;
677c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {"rmt/([0-9]+)", 1, MATCH_ADDR};
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if ((mn != NULL) && (*mn >= '0') && (*mn <= '9')) {
737c478bd9Sstevel@tonic-gate 		/*
747c478bd9Sstevel@tonic-gate 		 * first character cannot be a digit as it would combine
757c478bd9Sstevel@tonic-gate 		 * with the tape instance number to make an ambiguous quantity.
767c478bd9Sstevel@tonic-gate 		 */
777c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	devfspath = di_devfs_path(node);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, devfspath);
837c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, ":");
847c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, mn);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	di_devfs_path_free(devfspath);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 *  devfsadm_enumerate finds the logical tape id from the physical path,
907c478bd9Sstevel@tonic-gate 	 *  omitting minor name field. The logical tape id is returned in buf.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	if (devfsadm_enumerate_int(l_path, 0, &buf, rules, 1)) {
937c478bd9Sstevel@tonic-gate 		return (DEVFSADM_CONTINUE);
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	(void) strcpy(l_path, "rmt/");
977c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, buf);
987c478bd9Sstevel@tonic-gate 	(void) strcat(l_path, mn);
997c478bd9Sstevel@tonic-gate 	free(buf);
1007c478bd9Sstevel@tonic-gate 
101*45916cd2Sjpk 	if (system_labeled)
102*45916cd2Sjpk 		flags = DA_ADD|DA_TAPE;
103*45916cd2Sjpk 
104*45916cd2Sjpk 	(void) devfsadm_mklink(l_path, node, minor, flags);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	return (DEVFSADM_CONTINUE);
1077c478bd9Sstevel@tonic-gate }
108