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*39c23413Seschrock * Common Development and Distribution License (the "License").
6*39c23413Seschrock * 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*39c23413Seschrock * Copyright 2007 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 <dirent.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <synch.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <sys/wait.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
437c478bd9Sstevel@tonic-gate #include "disks_private.h"
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * The list of filesystem heuristic programs.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate struct heuristic {
497c478bd9Sstevel@tonic-gate struct heuristic *next;
507c478bd9Sstevel@tonic-gate char *prog;
517c478bd9Sstevel@tonic-gate char *type;
527c478bd9Sstevel@tonic-gate };
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate struct vfstab_list {
557c478bd9Sstevel@tonic-gate char *special;
567c478bd9Sstevel@tonic-gate char *mountp;
577c478bd9Sstevel@tonic-gate struct vfstab_list *next;
587c478bd9Sstevel@tonic-gate };
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate static struct vfstab_list *vfstab_listp = NULL;
617c478bd9Sstevel@tonic-gate static mutex_t vfstab_lock = DEFAULTMUTEX;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate static time_t timestamp = 0;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate static struct heuristic *hlist = NULL;
667c478bd9Sstevel@tonic-gate static int initialized = 0;
677c478bd9Sstevel@tonic-gate static mutex_t init_lock = DEFAULTMUTEX;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate static int has_fs(char *prog, char *slice);
707c478bd9Sstevel@tonic-gate static int load_heuristics();
717c478bd9Sstevel@tonic-gate static int add_use_record(struct vfstab *vp);
727c478bd9Sstevel@tonic-gate static int load_vfstab();
737c478bd9Sstevel@tonic-gate static void free_vfstab(struct vfstab_list *listp);
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate * Use the heuristics to check for a filesystem on the slice.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate int
inuse_fs(char * slice,nvlist_t * attrs,int * errp)797c478bd9Sstevel@tonic-gate inuse_fs(char *slice, nvlist_t *attrs, int *errp)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate struct heuristic *hp;
827c478bd9Sstevel@tonic-gate time_t curr_time;
837c478bd9Sstevel@tonic-gate int found = 0;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate *errp = 0;
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate if (slice == NULL) {
897c478bd9Sstevel@tonic-gate return (0);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * We get the list of heuristic programs one time.
947c478bd9Sstevel@tonic-gate */
957c478bd9Sstevel@tonic-gate (void) mutex_lock(&init_lock);
967c478bd9Sstevel@tonic-gate if (!initialized) {
977c478bd9Sstevel@tonic-gate *errp = load_heuristics();
987c478bd9Sstevel@tonic-gate
997c478bd9Sstevel@tonic-gate if (*errp == 0) {
1007c478bd9Sstevel@tonic-gate initialized = 1;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate (void) mutex_unlock(&init_lock);
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /* Run each of the heuristics. */
1067c478bd9Sstevel@tonic-gate for (hp = hlist; hp; hp = hp->next) {
1077c478bd9Sstevel@tonic-gate if (has_fs(hp->prog, slice)) {
1087c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_FS, errp);
1097c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, hp->type, errp);
1107c478bd9Sstevel@tonic-gate found = 1;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate if (*errp != 0)
1157c478bd9Sstevel@tonic-gate return (found);
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate * Second heuristic used is the check for an entry in vfstab
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate (void) mutex_lock(&vfstab_lock);
1227c478bd9Sstevel@tonic-gate curr_time = time(NULL);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate if (timestamp < curr_time && (curr_time - timestamp) > 60) {
1257c478bd9Sstevel@tonic-gate free_vfstab(vfstab_listp);
1267c478bd9Sstevel@tonic-gate *errp = load_vfstab();
1277c478bd9Sstevel@tonic-gate timestamp = curr_time;
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate if (*errp == 0) {
1317c478bd9Sstevel@tonic-gate struct vfstab_list *listp;
1327c478bd9Sstevel@tonic-gate listp = vfstab_listp;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate while (listp != NULL) {
1357c478bd9Sstevel@tonic-gate if (strcmp(slice, listp->special) == 0) {
1367c478bd9Sstevel@tonic-gate char *mountp = "";
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (listp->mountp != NULL)
1397c478bd9Sstevel@tonic-gate mountp = listp->mountp;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_VFSTAB, errp);
1427c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, mountp, errp);
1437c478bd9Sstevel@tonic-gate found = 1;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate listp = listp->next;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vfstab_lock);
1497c478bd9Sstevel@tonic-gate return (found);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate static int
has_fs(char * prog,char * slice)1537c478bd9Sstevel@tonic-gate has_fs(char *prog, char *slice)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate pid_t pid;
1567c478bd9Sstevel@tonic-gate int loc;
1577c478bd9Sstevel@tonic-gate mode_t mode = S_IRUSR | S_IWUSR;
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate switch ((pid = fork1())) {
1607c478bd9Sstevel@tonic-gate case 0:
1617c478bd9Sstevel@tonic-gate /* child process */
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate closefrom(1);
1647c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY, mode);
1657c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY, mode);
1667c478bd9Sstevel@tonic-gate (void) execl(prog, "fstyp", slice, NULL);
1677c478bd9Sstevel@tonic-gate _exit(1);
1687c478bd9Sstevel@tonic-gate break;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate case -1:
1717c478bd9Sstevel@tonic-gate return (0);
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate default:
1747c478bd9Sstevel@tonic-gate /* parent process */
1757c478bd9Sstevel@tonic-gate break;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate (void) waitpid(pid, &loc, 0);
1797c478bd9Sstevel@tonic-gate
1807c478bd9Sstevel@tonic-gate if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) {
1817c478bd9Sstevel@tonic-gate return (1);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate return (0);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * Create a list of filesystem heuristic programs.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate static int
load_heuristics()1917c478bd9Sstevel@tonic-gate load_heuristics()
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate DIR *dirp;
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate if ((dirp = opendir("/usr/lib/fs")) != NULL) {
1967c478bd9Sstevel@tonic-gate struct dirent *dp;
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) {
1997c478bd9Sstevel@tonic-gate char path[MAXPATHLEN];
2007c478bd9Sstevel@tonic-gate struct stat buf;
2017c478bd9Sstevel@tonic-gate DIR *subdirp;
2027c478bd9Sstevel@tonic-gate
2037c478bd9Sstevel@tonic-gate /* skip known dirs */
2047c478bd9Sstevel@tonic-gate if (strcmp(dp->d_name, ".") == 0 ||
2057c478bd9Sstevel@tonic-gate strcmp(dp->d_name, "..") == 0) {
2067c478bd9Sstevel@tonic-gate continue;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
209*39c23413Seschrock /*
210*39c23413Seschrock * Skip checking for ZFS filesystems. We know that
211*39c23413Seschrock * inuse_zpool() will have already been called, which does a
212*39c23413Seschrock * better job of checking anyway. More importantly, an unused
213*39c23413Seschrock * hot spare will still claim to have a ZFS filesystem because
214*39c23413Seschrock * it doesn't do the same level of checks.
215*39c23413Seschrock */
216*39c23413Seschrock if (strcmp(dp->d_name, "zfs") == 0)
217*39c23413Seschrock continue;
218*39c23413Seschrock
2197c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "/usr/lib/fs/%s",
2207c478bd9Sstevel@tonic-gate dp->d_name);
2217c478bd9Sstevel@tonic-gate
2224bc0a2efScasper if (stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) {
2237c478bd9Sstevel@tonic-gate continue;
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate if ((subdirp = opendir(path)) != NULL) {
2277c478bd9Sstevel@tonic-gate struct dirent *sdp;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate while ((sdp = readdir(subdirp)) != NULL) {
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate if (strcmp(sdp->d_name, "fstyp") == 0) {
2327c478bd9Sstevel@tonic-gate char progpath[MAXPATHLEN];
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate (void) snprintf(progpath, sizeof (progpath),
2357c478bd9Sstevel@tonic-gate "/usr/lib/fs/%s/fstyp", dp->d_name);
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate if (stat(progpath, &buf) == 0 &&
2384bc0a2efScasper S_ISREG(buf.st_mode)) {
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate struct heuristic *hp;
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate hp = (struct heuristic *)
2437c478bd9Sstevel@tonic-gate malloc(sizeof (struct heuristic));
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if (hp == NULL) {
2467c478bd9Sstevel@tonic-gate (void) closedir(subdirp);
2477c478bd9Sstevel@tonic-gate (void) closedir(dirp);
2487c478bd9Sstevel@tonic-gate return (ENOMEM);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate if ((hp->prog = strdup(progpath)) == NULL) {
2527c478bd9Sstevel@tonic-gate (void) closedir(subdirp);
2537c478bd9Sstevel@tonic-gate (void) closedir(dirp);
2547c478bd9Sstevel@tonic-gate return (ENOMEM);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate if ((hp->type = strdup(dp->d_name)) == NULL) {
2587c478bd9Sstevel@tonic-gate (void) closedir(subdirp);
2597c478bd9Sstevel@tonic-gate (void) closedir(dirp);
2607c478bd9Sstevel@tonic-gate return (ENOMEM);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate hp->next = hlist;
2647c478bd9Sstevel@tonic-gate hlist = hp;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate break;
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate
2717c478bd9Sstevel@tonic-gate (void) closedir(subdirp);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate (void) closedir(dirp);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate return (0);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate static int
load_vfstab()2827c478bd9Sstevel@tonic-gate load_vfstab()
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate FILE *fp;
2857c478bd9Sstevel@tonic-gate struct vfstab vp;
2867c478bd9Sstevel@tonic-gate int status = 1;
2877c478bd9Sstevel@tonic-gate
2887c478bd9Sstevel@tonic-gate fp = fopen(VFSTAB, "r");
2897c478bd9Sstevel@tonic-gate if (fp != NULL) {
2907c478bd9Sstevel@tonic-gate (void) memset(&vp, 0, sizeof (struct vfstab));
2917c478bd9Sstevel@tonic-gate while (getvfsent(fp, &vp) == 0) {
2927c478bd9Sstevel@tonic-gate status = add_use_record(&vp);
2937c478bd9Sstevel@tonic-gate if (status != 0) {
2947c478bd9Sstevel@tonic-gate (void) fclose(fp);
2957c478bd9Sstevel@tonic-gate return (status);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate (void) memset(&vp, 0, sizeof (struct vfstab));
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate (void) fclose(fp);
3007c478bd9Sstevel@tonic-gate status = 0;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate
3037c478bd9Sstevel@tonic-gate return (status);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate static int
add_use_record(struct vfstab * vp)3077c478bd9Sstevel@tonic-gate add_use_record(struct vfstab *vp)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate struct vfstab_list *vfsp;
3107c478bd9Sstevel@tonic-gate
3117c478bd9Sstevel@tonic-gate vfsp = (struct vfstab_list *)malloc(sizeof (struct vfstab_list));
3127c478bd9Sstevel@tonic-gate if (vfsp == NULL) {
3137c478bd9Sstevel@tonic-gate return (ENOMEM);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate vfsp->special = strdup(vp->vfs_special);
3177c478bd9Sstevel@tonic-gate if (vfsp->special == NULL) {
3187c478bd9Sstevel@tonic-gate free(vfsp);
3197c478bd9Sstevel@tonic-gate return (ENOMEM);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate if (vp->vfs_mountp != NULL) {
3237c478bd9Sstevel@tonic-gate vfsp->mountp = strdup(vp->vfs_mountp);
3247c478bd9Sstevel@tonic-gate if (vfsp->mountp == NULL) {
3257c478bd9Sstevel@tonic-gate free(vfsp);
3267c478bd9Sstevel@tonic-gate return (ENOMEM);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate } else {
3297c478bd9Sstevel@tonic-gate vfsp->mountp = NULL;
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate vfsp->next = vfstab_listp;
3337c478bd9Sstevel@tonic-gate vfstab_listp = vfsp;
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate static void
free_vfstab(struct vfstab_list * listp)3397c478bd9Sstevel@tonic-gate free_vfstab(struct vfstab_list *listp)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate struct vfstab_list *nextp;
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate while (listp != NULL) {
3447c478bd9Sstevel@tonic-gate nextp = listp->next;
3457c478bd9Sstevel@tonic-gate free((void *)listp->special);
3467c478bd9Sstevel@tonic-gate free((void *)listp->mountp);
3477c478bd9Sstevel@tonic-gate free((void *)listp);
3487c478bd9Sstevel@tonic-gate listp = nextp;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate vfstab_listp = NULL;
3527c478bd9Sstevel@tonic-gate }
353