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