xref: /titanic_52/usr/src/cmd/ramdiskadm/main.c (revision 549ec3fff108310966327d1dc9004551b63210b7)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * ramdiskadm - administer ramdisk(7d). Allows creation and deletion of
27  * ramdisks, and display status. All the ioctls are private between
28  * ramdisk and ramdiskadm, and so are very simple - device information is
29  * communicated via a name or a minor number.
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/ramdisk.h>
37 #include <sys/stat.h>
38 #include <sys/mkdev.h>
39 #include <sys/sunddi.h>
40 #include <libdevinfo.h>
41 #include <stdio.h>
42 #include <fcntl.h>
43 #include <locale.h>
44 #include <string.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <stropts.h>
49 #include <ctype.h>
50 #include "utils.h"
51 
52 #define	RD_BLOCK_DEV_PFX	"/dev/" RD_BLOCK_NAME "/"
53 #define	RD_CHAR_DEV_PFX		"/dev/" RD_CHAR_NAME "/"
54 
55 #define	HEADING	"%-*.*s %20s  %-10s\n"
56 #define	FORMAT	"%-*.*s %20llu    %s\n"
57 #define	FW	(sizeof (RD_BLOCK_DEV_PFX) - 1 + RD_NAME_LEN)
58 
59 static char *pname;
60 
61 static void
62 usage(void)
63 {
64 	(void) fprintf(stderr,
65 	    gettext("Usage: %s [ -a <name> <size>[g|m|k|b] | -d <name> ]\n"),
66 	    pname);
67 	exit(E_USAGE);
68 }
69 
70 /*
71  * This might be the first time we've used this minor device. If so,
72  * it might also be that the /dev links are in the process of being created
73  * by devfsadmd (or that they'll be created "soon"). We cannot return
74  * until they're there or the invoker of ramdiskadm might try to use them
75  * and not find them. This can happen if a shell script is running on
76  * an MP.
77  */
78 static void
79 wait_until_dev_complete(char *name)
80 {
81 	di_devlink_handle_t hdl;
82 
83 	hdl = di_devlink_init(RD_DRIVER_NAME, DI_MAKE_LINK);
84 	if (hdl == NULL) {
85 		die(gettext("couldn't create device link for\"%s\""), name);
86 	}
87 	(void) di_devlink_fini(&hdl);
88 }
89 
90 /*
91  * Create a named ramdisk.
92  */
93 static void
94 alloc_ramdisk(int ctl_fd, char *name, uint64_t size)
95 {
96 	struct rd_ioctl	ri;
97 
98 	(void) strlcpy(ri.ri_name, name, sizeof (ri.ri_name));
99 	ri.ri_size = size;
100 
101 	if (ioctl(ctl_fd, RD_CREATE_DISK, &ri) == -1) {
102 		die(gettext("couldn't create ramdisk \"%s\""), name);
103 	}
104 	wait_until_dev_complete(name);
105 
106 	(void) printf(RD_BLOCK_DEV_PFX "%s\n", name);
107 }
108 
109 /*
110  * Delete a named ramdisk.
111  */
112 static void
113 delete_ramdisk(int ctl_fd, char *name)
114 {
115 	struct rd_ioctl	ri;
116 
117 	(void) strlcpy(ri.ri_name, name, sizeof (ri.ri_name));
118 
119 	if (ioctl(ctl_fd, RD_DELETE_DISK, &ri) == -1) {
120 		die(gettext("couldn't delete ramdisk \"%s\""), name);
121 	}
122 }
123 
124 /*ARGSUSED*/
125 static int
126 di_callback(di_node_t node, di_minor_t minor, void *arg)
127 {
128 	static boolean_t	heading_done = B_FALSE;
129 	boolean_t		obp_ramdisk;
130 	char			*name;
131 	char			devnm[MAXNAMELEN];
132 	uint64_t		*sizep;
133 	char			blkpath[MAXPATHLEN];
134 
135 	/*
136 	 * Only consider block nodes bound to the ramdisk driver.
137 	 */
138 	if (strcmp(di_driver_name(node), RD_DRIVER_NAME) == 0 &&
139 	    di_minor_spectype(minor) == S_IFBLK) {
140 		/*
141 		 * Determine whether this ramdisk is pseudo or OBP-created.
142 		 */
143 		obp_ramdisk = (di_nodeid(node) == DI_PROM_NODEID);
144 
145 		/*
146 		 * If this is an OBP-created ramdisk use the node name, having
147 		 * first stripped the "ramdisk-" prefix.  For pseudo ramdisks
148 		 * use the minor name, having first stripped any ",raw" suffix.
149 		 */
150 		if (obp_ramdisk) {
151 			RD_STRIP_PREFIX(name, di_node_name(node));
152 			(void) strlcpy(devnm, name, sizeof (devnm));
153 		} else {
154 			(void) strlcpy(devnm, di_minor_name(minor),
155 			    sizeof (devnm));
156 			RD_STRIP_SUFFIX(devnm);
157 		}
158 
159 		/*
160 		 * Get the size of the ramdisk.
161 		 */
162 		if (di_prop_lookup_int64(di_minor_devt(minor), node,
163 		    "Size", (int64_t **)&sizep) == -1) {
164 			die(gettext("couldn't obtain size of ramdisk"));
165 		}
166 
167 		/*
168 		 * Print information about the ramdisk.  Prepend a heading
169 		 * if this is the first/only one.
170 		 */
171 		if (!heading_done) {
172 			(void) printf(HEADING, FW, FW, gettext("Block Device"),
173 			    gettext("Size"), gettext("Removable"));
174 			heading_done = B_TRUE;
175 		}
176 		(void) snprintf(blkpath, sizeof (blkpath),
177 		    RD_BLOCK_DEV_PFX "%s", devnm);
178 		(void) printf(FORMAT, FW, FW, blkpath, *sizep,
179 		    obp_ramdisk ? gettext("No") : gettext("Yes"));
180 	}
181 
182 	return (DI_WALK_CONTINUE);
183 }
184 
185 /*
186  * Print the list of all the ramdisks, their size, and whether they
187  * are removeable (i.e. non-OBP ramdisks).
188  */
189 static void
190 print_ramdisk(void)
191 {
192 	di_node_t	root;
193 
194 	/*
195 	 * Create a snapshot of the device tree, then walk it looking
196 	 * for, and printing information about, ramdisk nodes.
197 	 */
198 	if ((root = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
199 		die(gettext("couldn't create device tree snapshot"));
200 	}
201 
202 	if (di_walk_minor(root, DDI_PSEUDO, 0, NULL, di_callback) == -1) {
203 		di_fini(root);
204 		die(gettext("device tree walk failure"));
205 	}
206 
207 	di_fini(root);
208 }
209 
210 int
211 main(int argc, char *argv[])
212 {
213 	int		c;
214 	char		*name = NULL;
215 	int		allocflag = 0;
216 	int		deleteflag = 0;
217 	int		errflag = 0;
218 	char		*suffix;
219 	uint64_t	size;
220 	int		openflag;
221 	int		ctl_fd = 0;
222 	static char	rd_ctl[] = "/dev/" RD_CTL_NAME;
223 
224 	pname = getpname(argv[0]);
225 
226 	(void) setlocale(LC_ALL, "");
227 	(void) textdomain(TEXT_DOMAIN);
228 
229 	while ((c = getopt(argc, argv, "a:d:")) != EOF) {
230 		switch (c) {
231 		case 'a':
232 			allocflag = 1;
233 			name = optarg;
234 
235 			if (((argc - optind) <= 0) || (*argv[optind] == '-')) {
236 				warn(gettext("<size> missing\n"));
237 				usage();
238 				/*NOTREACHED*/
239 			}
240 			size = strtoll(argv[optind], &suffix, 0);
241 			if (strcmp(suffix, "b") == 0) {
242 				size *= 512;
243 				++suffix;
244 			} else if (strcmp(suffix, "k") == 0) {
245 				size *= 1024;
246 				++suffix;
247 			} else if (strcmp(suffix, "m") == 0) {
248 				size *= (1024 * 1024);
249 				++suffix;
250 			} else if (strcmp(suffix, "g") == 0) {
251 				size *= (1024 * 1024 * 1024);
252 				++suffix;
253 			}
254 			if (size == 0 || *suffix != '\0') {
255 				warn(gettext("Illegal <size> \"%s\"\n"),
256 				    argv[optind]);
257 				usage();
258 				/*NOTREACHED*/
259 			}
260 			++optind;
261 			break;
262 		case 'd':
263 			deleteflag = 1;
264 			name = optarg;
265 			break;
266 		default:
267 			errflag = 1;
268 			break;
269 		}
270 	}
271 	if (errflag || (allocflag && deleteflag) || (argc - optind) > 0) {
272 		usage();
273 		/*NOTREACHED*/
274 	}
275 
276 	if (allocflag || deleteflag) {
277 		boolean_t	nameok = B_TRUE;
278 		char		*p;
279 
280 		/*
281 		 * Strip off any leading "/dev/{r}ramdisk/" prefix.
282 		 */
283 		if (strncmp(name, RD_BLOCK_DEV_PFX,
284 		    sizeof (RD_BLOCK_DEV_PFX)-1) == 0) {
285 			name += sizeof (RD_BLOCK_DEV_PFX)-1;
286 		} else if (strncmp(name, RD_CHAR_DEV_PFX,
287 		    sizeof (RD_CHAR_DEV_PFX)-1) == 0) {
288 			name += sizeof (RD_CHAR_DEV_PFX)-1;
289 		}
290 
291 		/*
292 		 * Check that name isn't too long, and that it only contains
293 		 * valid characters, i.e. [a-zA-Z0-9_][a-zA-Z0-9_-]*
294 		 */
295 		if (name[0] == '-') {		/* permit only within name */
296 			nameok = B_FALSE;
297 		} else {
298 			for (p = name; *p != '\0'; p++) {
299 				if (!isalnum(*p) && *p != '_' && *p != '-') {
300 					nameok = B_FALSE;
301 					break;
302 				}
303 			}
304 		}
305 		if (!nameok || (p - name) > RD_NAME_LEN) {
306 			warn(gettext("illegal <name> \"%s\"\n"), name);
307 			usage();
308 			/*NOTREACHED*/
309 		}
310 	}
311 
312 	/*
313 	 * Now do the real work.
314 	 */
315 	openflag = O_EXCL;
316 	if (allocflag || deleteflag)
317 		openflag |= O_RDWR;
318 	else
319 		openflag |= O_RDONLY;
320 	ctl_fd = open(rd_ctl, openflag);
321 	if (ctl_fd == -1) {
322 		if ((errno == EPERM) || (errno == EACCES)) {
323 			die(gettext("you do not have permission to perform "
324 			    "that operation.\n"));
325 		} else {
326 			die("%s", rd_ctl);
327 		}
328 		/*NOTREACHED*/
329 	}
330 
331 	if (allocflag) {
332 		alloc_ramdisk(ctl_fd, name, size);
333 	} else if (deleteflag) {
334 		delete_ramdisk(ctl_fd, name);
335 	} else {
336 		print_ramdisk();
337 	}
338 
339 	return (E_SUCCESS);
340 }
341