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