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
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.
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]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI"
37 #include <sys/fstyp.h>
39 #include <sys/vfstab.h>
40 #include <sys/types.h>
44 #include <libdiskmgt.h>
48 static int match(char **opts
, char *s
);
49 static int has_Nflag(char *opts
);
53 #define VFS_PATH "/usr/lib/fs"
54 #define ALT_PATH "/etc/fs"
56 extern char *default_fstype();
57 void stat_snap(char *, char *, char *);
58 char *special
= NULL
; /* device special name */
59 char *fstype
= NULL
; /* fstype name is filled in here */
60 char *cbasename
; /* name of command */
61 char *newargv
[ARGV_MAX
]; /* args for the fstype specific command */
62 char vfstab
[] = VFSTAB
;
66 * TRANSLATION_NOTE - the usage strings in the c_usgstr[] of the
67 * following structures should be given a translation; the call to gettext
68 * is in the usage() function. The strings are the ones containing
75 char *c_usgstr
[4]; /* make sure as large as largest array size */
79 "[-F FSType] [-V] special inumber ...",
84 "[-F FSType] [-V] [-m] [-o specific_options] special ",
89 "[-F FSType] [-V] special inumber ...",
94 "[-F FSType] [-V] [-o specific_options] special",
99 "[-F FSType] [-V] -o special_options /mount/point",
100 "-d [-F FSType] [-V] /mount/point | dev",
101 "-i [-F FSType] [-V] [-o special-options] [/mount/point | dev]",
104 "labelit", "F:o:?nV",
106 "[-F FSType] [-V] [-o specific_options] special [operands]",
111 "[-F FSType] [-V] [-o specific_options] special [operands]",
115 struct commands
*c_ptr
;
117 static void usage(char *cmd
, char **usg
);
118 static void lookup(void);
121 main(int argc
, char *argv
[])
124 char full_path
[PATH_MAX
];
125 char *vfs_path
= VFS_PATH
;
126 char *alt_path
= ALT_PATH
;
129 int verbose
= 0; /* set if -V is specified */
134 char *tmpopts
= NULL
; /* used for in use checking */
137 int arg
; /* argument from getopt() */
140 extern char *optarg
; /* getopt specific */
144 (void) setlocale(LC_ALL
, "");
146 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
147 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
150 (void) textdomain(TEXT_DOMAIN
);
152 cbasename
= ptr
= argv
[0];
160 for (c_ptr
= cmd_data
; ((c_ptr
->c_basename
!= NULL
) &&
161 (strcmp(c_ptr
->c_basename
, cbasename
) != 0)); c_ptr
++)
163 usage(cbasename
, c_ptr
->c_usgstr
);
167 for (c_ptr
= cmd_data
; ((c_ptr
->c_basename
!= NULL
) &&
168 (strcmp(c_ptr
->c_basename
, cbasename
) != 0)); c_ptr
++)
170 while ((arg
= getopt(argc
, argv
, c_ptr
->c_optstr
)) != -1) {
172 case 'V': /* echo complete command line */
175 case 'F': /* FSType specified */
179 case 'o': /* FSType specific arguments */
180 newargv
[newargc
++] = "-o";
181 newargv
[newargc
++] = optarg
;
185 Nflag
= has_Nflag(tmpopts
);
188 case '?': /* print usage message */
189 newargv
[newargc
++] = "-?";
192 case 'm': /* FSType specific arguments */
194 newargv
[newargc
] = (char *)malloc(3);
195 sprintf(newargv
[newargc
++], "-%c", arg
);
197 newargv
[newargc
++] = optarg
;
199 case 'i': /* fssnap only */
203 newargv
[newargc
] = (char *)malloc(3);
204 sprintf(newargv
[newargc
++], "-%c", arg
);
206 newargv
[newargc
++] = optarg
;
212 (void) fprintf(stderr
,
213 gettext("%s: more than one FSType specified\n"),
215 usage(cbasename
, c_ptr
->c_usgstr
);
218 if (fstype
!= NULL
) {
219 if (strlen(fstype
) > FSTYPE_MAX
) {
220 (void) fprintf(stderr
,
221 gettext("%s: FSType %s exceeds %d characters\n"),
222 cbasename
, fstype
, FSTYPE_MAX
);
227 /* perform a lookup if fstype is not specified */
228 special
= argv
[optind
];
231 /* handle -i (fssnap command only) */
233 int diff
= argc
- optind
;
235 * There is no reason to ever call a file system specific
236 * version since its all in kstats.
238 if (diff
> 0) /* gave more than one mountpoint or device */
239 usage(cbasename
, c_ptr
->c_usgstr
);
240 stat_snap(cbasename
, diff
== 0 ? argv
[argc
-1] : NULL
, oopts
);
244 if ((special
== NULL
) && (!usgflag
)) {
245 (void) fprintf(stderr
, gettext("%s: special not specified\n"),
247 usage(cbasename
, c_ptr
->c_usgstr
);
251 if ((fstype
== NULL
) && (usgflag
))
252 usage(cbasename
, c_ptr
->c_usgstr
);
255 if (fstype
== NULL
) {
256 (void) fprintf(stderr
,
257 gettext("%s: FSType cannot be identified\n"), cbasename
);
258 usage(cbasename
, c_ptr
->c_usgstr
);
261 newargv
[newargc
++] = special
;
262 for (; optind
< argc
; optind
++)
263 newargv
[newargc
++] = argv
[optind
];
265 /* build the full pathname of the fstype dependent command */
266 sprintf(full_path
, "%s/%s/%s", vfs_path
, fstype
, cbasename
);
268 newargv
[1] = cbasename
;
271 printf("%s -F %s ", cbasename
, fstype
);
272 for (i
= 2; newargv
[i
]; i
++)
273 printf("%s ", newargv
[i
]);
279 * Prior to executing the command for mkfs check for device in use.
280 * If the mflag is set, user wants to see command that created
281 * an already existing filesystem. Do not check for in use in this
282 * case. If Nflag is set user wants to see what the parameters
283 * would be to create the filesystem. Do not check for in use in
286 if (strcmp(cbasename
, "mkfs") == 0 && !mflag
&& !Nflag
) {
287 if (dm_inuse(special
, &msg
, DM_WHO_MKFS
, &error
) ||
290 (void) fprintf(stderr
, gettext("Error occurred"
291 " with device in use checking: %s\n"),
294 (void) fprintf(stderr
, "%s", msg
);
302 * Execute the FSType specific command.
304 execv(full_path
, &newargv
[1]);
305 if ((errno
== ENOENT
) || (errno
== EACCES
)) {
306 /* build the alternate pathname */
307 sprintf(full_path
, "%s/%s/%s", alt_path
, fstype
, cbasename
);
309 printf("%s -F %s ", cbasename
, fstype
);
310 for (i
= 2; newargv
[i
]; i
++)
311 printf("%s ", newargv
[i
]);
315 execv(full_path
, &newargv
[1]);
317 if (errno
== ENOEXEC
) {
319 newargv
[1] = full_path
;
320 execv("/sbin/sh", &newargv
[0]);
322 if (errno
!= ENOENT
) {
324 (void) fprintf(stderr
, gettext("%s: cannot execute %s\n"),
325 cbasename
, full_path
);
329 if (sysfs(GETFSIND
, fstype
) == (-1)) {
330 (void) fprintf(stderr
,
331 gettext("%s: FSType %s not installed in the kernel\n"),
335 (void) fprintf(stderr
,
336 gettext("%s: Operation not applicable for FSType %s \n"),
342 usage(char *cmd
, char **usg
)
345 (void) fprintf(stderr
, gettext("Usage:\n"));
346 for (i
= 0; usg
[i
] != NULL
; i
++)
347 (void) fprintf(stderr
, "%s %s\n", gettext(cmd
),
354 * This looks up the /etc/vfstab entry given the device 'special'.
355 * It is called when the fstype is not specified on the command line.
357 * The following global variables are used:
366 struct vfstab vget
, vref
;
368 if ((fd
= fopen(vfstab
, "r")) == NULL
) {
369 (void) fprintf(stderr
, gettext("%s: cannot open vfstab\n"),
374 vref
.vfs_special
= special
;
375 ret
= getvfsany(fd
, &vget
, &vref
);
379 vref
.vfs_fsckdev
= special
;
380 ret
= getvfsany(fd
, &vget
, &vref
);
386 fstype
= default_fstype(special
);
389 fstype
= vget
.vfs_fstype
;
392 (void) fprintf(stderr
,
393 gettext("%s: line in vfstab exceeds %d characters\n"),
394 cbasename
, VFS_LINE_MAX
-2);
398 (void) fprintf(stderr
,
399 gettext("%s: line in vfstab has too few entries\n"),
407 stat_snap(char *cmd
, char *mountpoint
, char *opts
)
409 int fd
; /* check mount point if given */
414 if ((fd
= open(mountpoint
, O_RDONLY
)) < 0) {
416 errstr
= strerror(errno
);
418 errstr
= gettext("Unknown error");
420 (void) fprintf(stderr
,
421 gettext("%s: %s: error %d: %s\n"),
422 cmd
, mountpoint
, en
, errstr
);
428 fssnap_show_status(mountpoint
, opts
, 1, (opts
? 0 : 1));
431 has_Nflag(char *opts
)
433 while (opts
!= NULL
&& *opts
!= '\0') {
434 if (match(&opts
, "N")) {
447 * Parses the -o [fs specific options string] to search for the UFS -N flag.
448 * Return the opts string pointing to the next position in the string if
449 * match is not found. A delimiter of , or ' ' can be used depending on the
450 * caller, newfs or mkfs.
453 match(char **opts
, char *s
)
460 while (*cs
++ == *s
) {
467 * If we cannot find the delimiter it means we
468 * have hit the end of the string.
470 tmp_str
= strchr(*opts
, ',');
472 tmp_str
= strchr(*opts
, ' ');