xref: /illumos-gate/usr/src/lib/libdiskmgt/common/inuse_fs.c (revision 012e6ce759c490003aed29439cc47d3d73a99ad3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <dirent.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <synch.h>
31 #include <unistd.h>
32 #include <sys/errno.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/vfstab.h>
36 #include <fcntl.h>
37 #include <sys/wait.h>
38 #include <sys/fs/ufs_fs.h>
39 
40 #include "libdiskmgt.h"
41 #include "disks_private.h"
42 
43 /*
44  * The list of filesystem heuristic programs.
45  */
46 struct heuristic {
47 	struct heuristic	*next;
48 	char			*prog;
49 	char			*type;
50 };
51 
52 struct vfstab_list {
53 	char	*special;
54 	char	*mountp;
55 	struct vfstab_list 	*next;
56 };
57 
58 static struct vfstab_list	*vfstab_listp = NULL;
59 static	mutex_t	vfstab_lock = DEFAULTMUTEX;
60 
61 static	time_t	timestamp = 0;
62 
63 static struct heuristic	*hlist = NULL;
64 static int		initialized = 0;
65 static mutex_t		init_lock = DEFAULTMUTEX;
66 
67 static int	has_fs(char *prog, char *slice);
68 static int	load_heuristics();
69 static int	add_use_record(struct vfstab *vp);
70 static int	load_vfstab();
71 static void	free_vfstab(struct vfstab_list *listp);
72 
73 /*
74  * Use the heuristics to check for a filesystem on the slice.
75  */
76 int
77 inuse_fs(char *slice, nvlist_t *attrs, int *errp)
78 {
79 	struct 	heuristic	*hp;
80 	time_t	curr_time;
81 	int	found = 0;
82 
83 
84 	*errp = 0;
85 
86 	if (slice == NULL) {
87 	    return (0);
88 	}
89 
90 	/*
91 	 * We get the list of heuristic programs one time.
92 	 */
93 	(void) mutex_lock(&init_lock);
94 	if (!initialized) {
95 	    *errp = load_heuristics();
96 
97 	    if (*errp == 0) {
98 		initialized = 1;
99 	    }
100 	}
101 	(void) mutex_unlock(&init_lock);
102 
103 	/* Run each of the heuristics. */
104 	for (hp = hlist; hp; hp = hp->next) {
105 	    if (has_fs(hp->prog, slice)) {
106 		libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_FS, errp);
107 		libdiskmgt_add_str(attrs, DM_USED_NAME, hp->type, errp);
108 		found = 1;
109 	    }
110 	}
111 
112 	if (*errp != 0)
113 		return (found);
114 
115 	/*
116 	 * Second heuristic used is the check for an entry in vfstab
117 	 */
118 
119 	(void) mutex_lock(&vfstab_lock);
120 	curr_time = time(NULL);
121 
122 	if (timestamp < curr_time && (curr_time - timestamp) > 60) {
123 		free_vfstab(vfstab_listp);
124 		*errp = load_vfstab();
125 		timestamp = curr_time;
126 	}
127 
128 	if (*errp == 0) {
129 	    struct vfstab_list	*listp;
130 	    listp = vfstab_listp;
131 
132 	    while (listp != NULL) {
133 		if (strcmp(slice, listp->special) == 0) {
134 		    char *mountp = "";
135 
136 		    if (listp->mountp != NULL)
137 			mountp = listp->mountp;
138 
139 		    libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_VFSTAB, errp);
140 		    libdiskmgt_add_str(attrs, DM_USED_NAME, mountp, errp);
141 		    found = 1;
142 		}
143 		listp = listp->next;
144 	    }
145 	}
146 	(void) mutex_unlock(&vfstab_lock);
147 	return (found);
148 }
149 
150 static int
151 has_fs(char *prog, char *slice)
152 {
153 	pid_t	pid;
154 	int	loc;
155 	mode_t	mode = S_IRUSR | S_IWUSR;
156 
157 	switch ((pid = fork1())) {
158 	case 0:
159 	    /* child process */
160 
161 	    closefrom(1);
162 	    (void) open("/dev/null", O_WRONLY, mode);
163 	    (void) open("/dev/null", O_WRONLY, mode);
164 	    (void) execl(prog, "fstyp", slice, NULL);
165 	    _exit(1);
166 	    break;
167 
168 	case -1:
169 	    return (0);
170 
171 	default:
172 	    /* parent process */
173 	    break;
174 	}
175 
176 	(void) waitpid(pid, &loc, 0);
177 
178 	if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) {
179 	    return (1);
180 	}
181 
182 	return (0);
183 }
184 
185 /*
186  * Create a list of filesystem heuristic programs.
187  */
188 static int
189 load_heuristics()
190 {
191 	DIR	*dirp;
192 
193 	if ((dirp = opendir("/usr/lib/fs")) != NULL) {
194 	    struct dirent   *dp;
195 
196 	    while ((dp = readdir(dirp)) != NULL) {
197 		char		path[MAXPATHLEN];
198 		struct stat	buf;
199 		DIR		*subdirp;
200 
201 		/* skip known dirs */
202 		if (strcmp(dp->d_name, ".") == 0 ||
203 		    strcmp(dp->d_name, "..") == 0) {
204 		    continue;
205 		}
206 
207 		/*
208 		 * Skip checking for ZFS filesystems.  We know that
209 		 * inuse_zpool() will have already been called, which does a
210 		 * better job of checking anyway.  More importantly, an unused
211 		 * hot spare will still claim to have a ZFS filesystem because
212 		 * it doesn't do the same level of checks.
213 		 */
214 		if (strcmp(dp->d_name, "zfs") == 0)
215 			continue;
216 
217 		(void) snprintf(path, sizeof (path), "/usr/lib/fs/%s",
218 		    dp->d_name);
219 
220 		if (stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) {
221 		    continue;
222 		}
223 
224 		if ((subdirp = opendir(path)) != NULL) {
225 		    struct dirent   *sdp;
226 
227 		    while ((sdp = readdir(subdirp)) != NULL) {
228 
229 			if (strcmp(sdp->d_name, "fstyp") == 0) {
230 			    char progpath[MAXPATHLEN];
231 
232 			    (void) snprintf(progpath, sizeof (progpath),
233 				"/usr/lib/fs/%s/fstyp", dp->d_name);
234 
235 			    if (stat(progpath, &buf) == 0 &&
236 				S_ISREG(buf.st_mode)) {
237 
238 				struct heuristic *hp;
239 
240 				hp = (struct heuristic *)
241 				    malloc(sizeof (struct heuristic));
242 
243 				if (hp == NULL) {
244 				    (void) closedir(subdirp);
245 				    (void) closedir(dirp);
246 				    return (ENOMEM);
247 				}
248 
249 				if ((hp->prog = strdup(progpath)) == NULL) {
250 				    (void) closedir(subdirp);
251 				    (void) closedir(dirp);
252 				    return (ENOMEM);
253 				}
254 
255 				if ((hp->type = strdup(dp->d_name)) == NULL) {
256 				    (void) closedir(subdirp);
257 				    (void) closedir(dirp);
258 				    return (ENOMEM);
259 				}
260 
261 				hp->next = hlist;
262 				hlist = hp;
263 			    }
264 
265 			    break;
266 			}
267 		    }
268 
269 		    (void) closedir(subdirp);
270 		}
271 	    }
272 
273 	    (void) closedir(dirp);
274 	}
275 
276 	return (0);
277 }
278 
279 static int
280 load_vfstab()
281 {
282 	FILE	*fp;
283 	struct	vfstab vp;
284 	int	status = 1;
285 
286 	fp = fopen(VFSTAB, "r");
287 	if (fp != NULL) {
288 	    (void) memset(&vp, 0, sizeof (struct vfstab));
289 	    while (getvfsent(fp, &vp) == 0) {
290 		    status = add_use_record(&vp);
291 		    if (status != 0) {
292 			(void) fclose(fp);
293 			return (status);
294 		    }
295 		(void) memset(&vp, 0, sizeof (struct vfstab));
296 	    }
297 	    (void) fclose(fp);
298 	    status = 0;
299 	}
300 
301 	return (status);
302 }
303 
304 static int
305 add_use_record(struct vfstab *vp)
306 {
307 	struct 	vfstab_list	*vfsp;
308 
309 	vfsp = (struct vfstab_list *)malloc(sizeof (struct vfstab_list));
310 	if (vfsp == NULL) {
311 	    return (ENOMEM);
312 	}
313 
314 	vfsp->special = strdup(vp->vfs_special);
315 	if (vfsp->special == NULL) {
316 	    free(vfsp);
317 	    return (ENOMEM);
318 	}
319 
320 	if (vp->vfs_mountp != NULL) {
321 	    vfsp->mountp = strdup(vp->vfs_mountp);
322 	    if (vfsp->mountp == NULL) {
323 		free(vfsp);
324 		return (ENOMEM);
325 	    }
326 	} else {
327 	    vfsp->mountp = NULL;
328 	}
329 
330 	vfsp->next = vfstab_listp;
331 	vfstab_listp = vfsp;
332 
333 	return (0);
334 }
335 
336 static void
337 free_vfstab(struct vfstab_list *listp)
338 {
339 	struct vfstab_list	*nextp;
340 
341 	while (listp != NULL) {
342 	    nextp = listp->next;
343 	    free((void *)listp->special);
344 	    free((void *)listp->mountp);
345 	    free((void *)listp);
346 	    listp = nextp;
347 	}
348 
349 	vfstab_listp = NULL;
350 }
351