summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdiskmgt/common/inuse_fs.c
blob: e3f25b2156e0e729d4b2f9966fc2b53c37747095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <synch.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfstab.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/fs/ufs_fs.h>

#include "libdiskmgt.h"
#include "disks_private.h"

/*
 * The list of filesystem heuristic programs.
 */
struct heuristic {
	struct heuristic	*next;
	char			*prog;
	char			*type;
};

struct vfstab_list {
	char	*special;
	char	*mountp;
	struct vfstab_list 	*next;
};

static struct vfstab_list	*vfstab_listp = NULL;
static	mutex_t	vfstab_lock = DEFAULTMUTEX;

static	time_t	timestamp = 0;

static struct heuristic	*hlist = NULL;
static int		initialized = 0;
static mutex_t		init_lock = DEFAULTMUTEX;

static int	has_fs(char *prog, char *slice);
static int	load_heuristics();
static int	add_use_record(struct vfstab *vp);
static int	load_vfstab();
static void	free_vfstab(struct vfstab_list *listp);

/*
 * Use the heuristics to check for a filesystem on the slice.
 */
int
inuse_fs(char *slice, nvlist_t *attrs, int *errp)
{
	struct 	heuristic	*hp;
	time_t	curr_time;
	int	found = 0;


	*errp = 0;

	if (slice == NULL) {
	    return (0);
	}

	/*
	 * We get the list of heuristic programs one time.
	 */
	(void) mutex_lock(&init_lock);
	if (!initialized) {
	    *errp = load_heuristics();

	    if (*errp == 0) {
		initialized = 1;
	    }
	}
	(void) mutex_unlock(&init_lock);

	/* Run each of the heuristics. */
	for (hp = hlist; hp; hp = hp->next) {
	    if (has_fs(hp->prog, slice)) {
		libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_FS, errp);
		libdiskmgt_add_str(attrs, DM_USED_NAME, hp->type, errp);
		found = 1;
	    }
	}

	if (*errp != 0)
		return (found);

	/*
	 * Second heuristic used is the check for an entry in vfstab
	 */

	(void) mutex_lock(&vfstab_lock);
	curr_time = time(NULL);

	if (timestamp < curr_time && (curr_time - timestamp) > 60) {
		free_vfstab(vfstab_listp);
		*errp = load_vfstab();
		timestamp = curr_time;
	}

	if (*errp == 0) {
	    struct vfstab_list	*listp;
	    listp = vfstab_listp;

	    while (listp != NULL) {
		if (strcmp(slice, listp->special) == 0) {
		    char *mountp = "";

		    if (listp->mountp != NULL)
			mountp = listp->mountp;

		    libdiskmgt_add_str(attrs, DM_USED_BY, DM_USE_VFSTAB, errp);
		    libdiskmgt_add_str(attrs, DM_USED_NAME, mountp, errp);
		    found = 1;
		}
		listp = listp->next;
	    }
	}
	(void) mutex_unlock(&vfstab_lock);
	return (found);
}

static int
has_fs(char *prog, char *slice)
{
	pid_t	pid;
	int	loc;
	mode_t	mode = S_IRUSR | S_IWUSR;

	switch ((pid = fork1())) {
	case 0:
	    /* child process */

	    closefrom(1);
	    (void) open("/dev/null", O_WRONLY, mode);
	    (void) open("/dev/null", O_WRONLY, mode);
	    (void) execl(prog, "fstyp", slice, NULL);
	    _exit(1);
	    break;

	case -1:
	    return (0);

	default:
	    /* parent process */
	    break;
	}

	(void) waitpid(pid, &loc, 0);

	if (WIFEXITED(loc) && WEXITSTATUS(loc) == 0) {
	    return (1);
	}

	return (0);
}

/*
 * Create a list of filesystem heuristic programs.
 */
static int
load_heuristics()
{
	DIR	*dirp;

	if ((dirp = opendir("/usr/lib/fs")) != NULL) {
	    struct dirent   *dp;

	    while ((dp = readdir(dirp)) != NULL) {
		char		path[MAXPATHLEN];
		struct stat	buf;
		DIR		*subdirp;

		/* skip known dirs */
		if (strcmp(dp->d_name, ".") == 0 ||
		    strcmp(dp->d_name, "..") == 0) {
		    continue;
		}

		/*
		 * Skip checking for ZFS filesystems.  We know that
		 * inuse_zpool() will have already been called, which does a
		 * better job of checking anyway.  More importantly, an unused
		 * hot spare will still claim to have a ZFS filesystem because
		 * it doesn't do the same level of checks.
		 */
		if (strcmp(dp->d_name, "zfs") == 0)
			continue;

		(void) snprintf(path, sizeof (path), "/usr/lib/fs/%s",
		    dp->d_name);

		if (stat(path, &buf) != 0 || !S_ISDIR(buf.st_mode)) {
		    continue;
		}

		if ((subdirp = opendir(path)) != NULL) {
		    struct dirent   *sdp;

		    while ((sdp = readdir(subdirp)) != NULL) {

			if (strcmp(sdp->d_name, "fstyp") == 0) {
			    char progpath[MAXPATHLEN];

			    (void) snprintf(progpath, sizeof (progpath),
				"/usr/lib/fs/%s/fstyp", dp->d_name);

			    if (stat(progpath, &buf) == 0 &&
				S_ISREG(buf.st_mode)) {

				struct heuristic *hp;

				hp = (struct heuristic *)
				    malloc(sizeof (struct heuristic));

				if (hp == NULL) {
				    (void) closedir(subdirp);
				    (void) closedir(dirp);
				    return (ENOMEM);
				}

				if ((hp->prog = strdup(progpath)) == NULL) {
				    (void) closedir(subdirp);
				    (void) closedir(dirp);
				    return (ENOMEM);
				}

				if ((hp->type = strdup(dp->d_name)) == NULL) {
				    (void) closedir(subdirp);
				    (void) closedir(dirp);
				    return (ENOMEM);
				}

				hp->next = hlist;
				hlist = hp;
			    }

			    break;
			}
		    }

		    (void) closedir(subdirp);
		}
	    }

	    (void) closedir(dirp);
	}

	return (0);
}

static int
load_vfstab()
{
	FILE	*fp;
	struct	vfstab vp;
	int	status = 1;

	fp = fopen(VFSTAB, "r");
	if (fp != NULL) {
	    (void) memset(&vp, 0, sizeof (struct vfstab));
	    while (getvfsent(fp, &vp) == 0) {
		    status = add_use_record(&vp);
		    if (status != 0) {
			(void) fclose(fp);
			return (status);
		    }
		(void) memset(&vp, 0, sizeof (struct vfstab));
	    }
	    (void) fclose(fp);
	    status = 0;
	}

	return (status);
}

static int
add_use_record(struct vfstab *vp)
{
	struct 	vfstab_list	*vfsp;

	vfsp = (struct vfstab_list *)malloc(sizeof (struct vfstab_list));
	if (vfsp == NULL) {
	    return (ENOMEM);
	}

	vfsp->special = strdup(vp->vfs_special);
	if (vfsp->special == NULL) {
	    free(vfsp);
	    return (ENOMEM);
	}

	if (vp->vfs_mountp != NULL) {
	    vfsp->mountp = strdup(vp->vfs_mountp);
	    if (vfsp->mountp == NULL) {
		free(vfsp);
		return (ENOMEM);
	    }
	} else {
	    vfsp->mountp = NULL;
	}

	vfsp->next = vfstab_listp;
	vfstab_listp = vfsp;

	return (0);
}

static void
free_vfstab(struct vfstab_list *listp)
{
	struct vfstab_list	*nextp;

	while (listp != NULL) {
	    nextp = listp->next;
	    free((void *)listp->special);
	    free((void *)listp->mountp);
	    free((void *)listp);
	    listp = nextp;
	}

	vfstab_listp = NULL;
}