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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 233e1bd7a2Ssjelinek * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <dirent.h> 307c478bd9Sstevel@tonic-gate #include <stdlib.h> 317c478bd9Sstevel@tonic-gate #include <stdio.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <synch.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <sys/errno.h> 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <sys/vfstab.h> 397c478bd9Sstevel@tonic-gate #include <fcntl.h> 407c478bd9Sstevel@tonic-gate #include <sys/wait.h> 417c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "libdiskmgt.h" 447c478bd9Sstevel@tonic-gate #include "disks_private.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * The list of filesystem heuristic programs. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate struct heuristic { 507c478bd9Sstevel@tonic-gate struct heuristic *next; 517c478bd9Sstevel@tonic-gate char *prog; 527c478bd9Sstevel@tonic-gate char *type; 537c478bd9Sstevel@tonic-gate }; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate struct vfstab_list { 567c478bd9Sstevel@tonic-gate char *special; 577c478bd9Sstevel@tonic-gate char *mountp; 587c478bd9Sstevel@tonic-gate struct vfstab_list *next; 597c478bd9Sstevel@tonic-gate }; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate static struct vfstab_list *vfstab_listp = NULL; 627c478bd9Sstevel@tonic-gate static mutex_t vfstab_lock = DEFAULTMUTEX; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static time_t timestamp = 0; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static struct heuristic *hlist = NULL; 677c478bd9Sstevel@tonic-gate static int initialized = 0; 687c478bd9Sstevel@tonic-gate static mutex_t init_lock = DEFAULTMUTEX; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate static int has_fs(char *prog, char *slice); 717c478bd9Sstevel@tonic-gate static int load_heuristics(); 727c478bd9Sstevel@tonic-gate static int add_use_record(struct vfstab *vp); 737c478bd9Sstevel@tonic-gate static int load_vfstab(); 747c478bd9Sstevel@tonic-gate static void free_vfstab(struct vfstab_list *listp); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Use the heuristics to check for a filesystem on the slice. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate int 807c478bd9Sstevel@tonic-gate inuse_fs(char *slice, nvlist_t *attrs, int *errp) 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate struct heuristic *hp; 837c478bd9Sstevel@tonic-gate time_t curr_time; 847c478bd9Sstevel@tonic-gate int found = 0; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate *errp = 0; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if (slice == NULL) { 907c478bd9Sstevel@tonic-gate return (0); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * We get the list of heuristic programs one time. 957c478bd9Sstevel@tonic-gate */ 967c478bd9Sstevel@tonic-gate (void) mutex_lock(&init_lock); 977c478bd9Sstevel@tonic-gate if (!initialized) { 987c478bd9Sstevel@tonic-gate *errp = load_heuristics(); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (*errp == 0) { 1017c478bd9Sstevel@tonic-gate initialized = 1; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate (void) mutex_unlock(&init_lock); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* Run each of the heuristics. */ 1077c478bd9Sstevel@tonic-gate for (hp = hlist; hp; hp = hp->next) { 1087c478bd9Sstevel@tonic-gate if (has_fs(hp->prog, slice)) { 1097c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_FS, errp); 1107c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, hp->type, errp); 1117c478bd9Sstevel@tonic-gate found = 1; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate if (*errp != 0) 1167c478bd9Sstevel@tonic-gate return (found); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * Second heuristic used is the check for an entry in vfstab 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate (void) mutex_lock(&vfstab_lock); 1237c478bd9Sstevel@tonic-gate curr_time = time(NULL); 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if (timestamp < curr_time && (curr_time - timestamp) > 60) { 1267c478bd9Sstevel@tonic-gate free_vfstab(vfstab_listp); 1277c478bd9Sstevel@tonic-gate *errp = load_vfstab(); 1287c478bd9Sstevel@tonic-gate timestamp = curr_time; 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate if (*errp == 0) { 1327c478bd9Sstevel@tonic-gate struct vfstab_list *listp; 1337c478bd9Sstevel@tonic-gate listp = vfstab_listp; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate while (listp != NULL) { 1367c478bd9Sstevel@tonic-gate if (strcmp(slice, listp->special) == 0) { 1377c478bd9Sstevel@tonic-gate char *mountp = ""; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (listp->mountp != NULL) 1407c478bd9Sstevel@tonic-gate mountp = listp->mountp; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_VFSTAB, errp); 1437c478bd9Sstevel@tonic-gate libdiskmgt_add_str(attrs, DM_USED_NAME, mountp, errp); 1447c478bd9Sstevel@tonic-gate found = 1; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate listp = listp->next; 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate (void) mutex_unlock(&vfstab_lock); 1507c478bd9Sstevel@tonic-gate return (found); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate static int 1547c478bd9Sstevel@tonic-gate has_fs(char *prog, char *slice) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate pid_t pid; 1577c478bd9Sstevel@tonic-gate int loc; 1587c478bd9Sstevel@tonic-gate mode_t mode = S_IRUSR | S_IWUSR; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate switch ((pid = fork1())) { 1617c478bd9Sstevel@tonic-gate case 0: 1627c478bd9Sstevel@tonic-gate /* child process */ 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate closefrom(1); 1657c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY, mode); 1667c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY, mode); 1677c478bd9Sstevel@tonic-gate (void) execl(prog, "fstyp", slice, NULL); 1687c478bd9Sstevel@tonic-gate _exit(1); 1697c478bd9Sstevel@tonic-gate break; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate case -1: 1727c478bd9Sstevel@tonic-gate return (0); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate default: 1757c478bd9Sstevel@tonic-gate /* parent process */ 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate (void) waitpid(pid, &loc, 0); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) { 1827c478bd9Sstevel@tonic-gate return (1); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate return (0); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate /* 1897c478bd9Sstevel@tonic-gate * Create a list of filesystem heuristic programs. 1907c478bd9Sstevel@tonic-gate */ 1917c478bd9Sstevel@tonic-gate static int 1927c478bd9Sstevel@tonic-gate load_heuristics() 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate DIR *dirp; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if ((dirp = opendir("/usr/lib/fs")) != NULL) { 1977c478bd9Sstevel@tonic-gate struct dirent *dp; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 2007c478bd9Sstevel@tonic-gate char path[MAXPATHLEN]; 2017c478bd9Sstevel@tonic-gate struct stat buf; 2027c478bd9Sstevel@tonic-gate DIR *subdirp; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* skip known dirs */ 2057c478bd9Sstevel@tonic-gate if (strcmp(dp->d_name, ".") == 0 || 2067c478bd9Sstevel@tonic-gate strcmp(dp->d_name, "..") == 0) { 2077c478bd9Sstevel@tonic-gate continue; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "/usr/lib/fs/%s", 2117c478bd9Sstevel@tonic-gate dp->d_name); 2127c478bd9Sstevel@tonic-gate 213*4bc0a2efScasper if (stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) { 2147c478bd9Sstevel@tonic-gate continue; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate if ((subdirp = opendir(path)) != NULL) { 2187c478bd9Sstevel@tonic-gate struct dirent *sdp; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate while ((sdp = readdir(subdirp)) != NULL) { 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate if (strcmp(sdp->d_name, "fstyp") == 0) { 2237c478bd9Sstevel@tonic-gate char progpath[MAXPATHLEN]; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate (void) snprintf(progpath, sizeof (progpath), 2267c478bd9Sstevel@tonic-gate "/usr/lib/fs/%s/fstyp", dp->d_name); 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate if (stat(progpath, &buf) == 0 && 229*4bc0a2efScasper S_ISREG(buf.st_mode)) { 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate struct heuristic *hp; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate hp = (struct heuristic *) 2347c478bd9Sstevel@tonic-gate malloc(sizeof (struct heuristic)); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate if (hp == NULL) { 2377c478bd9Sstevel@tonic-gate (void) closedir(subdirp); 2387c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2397c478bd9Sstevel@tonic-gate return (ENOMEM); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate if ((hp->prog = strdup(progpath)) == NULL) { 2437c478bd9Sstevel@tonic-gate (void) closedir(subdirp); 2447c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2457c478bd9Sstevel@tonic-gate return (ENOMEM); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate if ((hp->type = strdup(dp->d_name)) == NULL) { 2497c478bd9Sstevel@tonic-gate (void) closedir(subdirp); 2507c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2517c478bd9Sstevel@tonic-gate return (ENOMEM); 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate hp->next = hlist; 2557c478bd9Sstevel@tonic-gate hlist = hp; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate break; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate (void) closedir(subdirp); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate return (0); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate static int 2737c478bd9Sstevel@tonic-gate load_vfstab() 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate FILE *fp; 2767c478bd9Sstevel@tonic-gate struct vfstab vp; 2777c478bd9Sstevel@tonic-gate int status = 1; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate fp = fopen(VFSTAB, "r"); 2807c478bd9Sstevel@tonic-gate if (fp != NULL) { 2817c478bd9Sstevel@tonic-gate (void) memset(&vp, 0, sizeof (struct vfstab)); 2827c478bd9Sstevel@tonic-gate while (getvfsent(fp, &vp) == 0) { 2837c478bd9Sstevel@tonic-gate status = add_use_record(&vp); 2847c478bd9Sstevel@tonic-gate if (status != 0) { 2857c478bd9Sstevel@tonic-gate (void) fclose(fp); 2867c478bd9Sstevel@tonic-gate return (status); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate (void) memset(&vp, 0, sizeof (struct vfstab)); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate (void) fclose(fp); 2917c478bd9Sstevel@tonic-gate status = 0; 2927c478bd9Sstevel@tonic-gate } 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate return (status); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate static int 2987c478bd9Sstevel@tonic-gate add_use_record(struct vfstab *vp) 2997c478bd9Sstevel@tonic-gate { 3007c478bd9Sstevel@tonic-gate struct vfstab_list *vfsp; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate vfsp = (struct vfstab_list *)malloc(sizeof (struct vfstab_list)); 3037c478bd9Sstevel@tonic-gate if (vfsp == NULL) { 3047c478bd9Sstevel@tonic-gate return (ENOMEM); 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate vfsp->special = strdup(vp->vfs_special); 3087c478bd9Sstevel@tonic-gate if (vfsp->special == NULL) { 3097c478bd9Sstevel@tonic-gate free(vfsp); 3107c478bd9Sstevel@tonic-gate return (ENOMEM); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate if (vp->vfs_mountp != NULL) { 3147c478bd9Sstevel@tonic-gate vfsp->mountp = strdup(vp->vfs_mountp); 3157c478bd9Sstevel@tonic-gate if (vfsp->mountp == NULL) { 3167c478bd9Sstevel@tonic-gate free(vfsp); 3177c478bd9Sstevel@tonic-gate return (ENOMEM); 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate } else { 3207c478bd9Sstevel@tonic-gate vfsp->mountp = NULL; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate vfsp->next = vfstab_listp; 3247c478bd9Sstevel@tonic-gate vfstab_listp = vfsp; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate return (0); 3277c478bd9Sstevel@tonic-gate } 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate static void 3307c478bd9Sstevel@tonic-gate free_vfstab(struct vfstab_list *listp) 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate struct vfstab_list *nextp; 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate while (listp != NULL) { 3357c478bd9Sstevel@tonic-gate nextp = listp->next; 3367c478bd9Sstevel@tonic-gate free((void *)listp->special); 3377c478bd9Sstevel@tonic-gate free((void *)listp->mountp); 3387c478bd9Sstevel@tonic-gate free((void *)listp); 3397c478bd9Sstevel@tonic-gate listp = nextp; 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate vfstab_listp = NULL; 3437c478bd9Sstevel@tonic-gate } 344