summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/ufs/lockfs/lockfs.c
blob: b10bdae5225d0b54b69ebc2140019ef316bcdef0 (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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * 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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

/*
 * lockfs
 *	user interface to lockfs functionality
 */
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <errno.h>
#include <sys/lockfs.h>
#include <sys/filio.h>

#define	bzero(s, n)	memset(s, 0, n);

/*
 * command line processing
 */
extern char	*optarg;
extern int	optind;
extern int	opterr;

extern void exit();

static void exitusage();
static void printstatusline(char *, char *, char *);
static void printstatus(char *);
static void flushfs(char *);
static void lockfs(char *);
static void getmntnames();
static void getcmdnames(int, char **, int);

/*
 * -a = all
 * -v = verbose
 */
int all		= 0;
int verbose	= 0;

/*
 * exitstatus
 *	0 all ok
 *	1 internal error
 *	2 system call error
 */
int exitstatus	= 0;

/*
 * list of filenames
 */
struct filename {
	struct filename	*fn_next;
	char		*fn_name;
};
struct filename	*fnanchor	= 0;

/*
 * default request is `file system lock status'
 * default lock type is `unlock'
 * -wnduhfe changes them
 */
int request	= _FIOLFSS;
ushort_t	lock	= LOCKFS_ULOCK;

/*
 * default comment is null
 *	-c changes it
 */
caddr_t comment	= 0;
ulong_t	comlen	= 0;

/*
 * for prettyprint
 */
int firsttime	= 0;

/*
 * no unlocks printed
 */
int no_unlocks_printed	= 0;

/*
 * file system was modified during hlock/wlock/elock
 */
#define	LOCKWARN(FN, S)	\
{ \
	if (verbose) \
		printf("WARNING: %s was modified while %s locked\n", FN, S); \
	exitstatus = 2; \
}

/*
 * forward reference
 */
char	*malloc();

int
main(int argc, char *argv[])
{
	int		c;
	struct filename	*fnp;

	exitstatus = 0;

	/*
	 * process command line
	 */
	opterr = 0;
	optarg = 0;

	while ((c = getopt(argc, argv, "vfwnduheac:")) != -1)
		switch (c) {
		case 'v':
			verbose = 1;
			break;
		case 'f':
			request = _FIOFFS;
			break;
		case 'w':
			lock    = LOCKFS_WLOCK;
			request = _FIOLFS;
			break;
		case 'n':
			lock    = LOCKFS_NLOCK;
			request = _FIOLFS;
			break;
		case 'd':
			lock    = LOCKFS_DLOCK;
			request = _FIOLFS;
			break;
		case 'h':
			lock    = LOCKFS_HLOCK;
			request = _FIOLFS;
			break;
		case 'e':
			lock	= LOCKFS_ELOCK;
			request = _FIOLFS;
			break;
		case 'u':
			lock    = LOCKFS_ULOCK;
			request = _FIOLFS;
			break;
		case 'a':
			all = 1;
			break;
		case 'c':
			comment = optarg;
			comlen  = strlen(optarg)+1;
			request = _FIOLFS;
			break;
		default:
			exitusage();
			break;
		}

	if (argc == 1) {
		no_unlocks_printed = 1;
		all = 1;
	}

	if (all)
		/*
		 * use /etc/mtab
		 */
		getmntnames();
	else
		/*
		 * use command line
		 */
		getcmdnames(argc, argv, optind);

	/*
	 * for each filename, doit
	 */
	for (fnp = fnanchor; fnp; fnp = fnp->fn_next) {
		switch (request) {
		case _FIOLFSS:
			printstatus(fnp->fn_name);
			break;
		case _FIOLFS:
			lockfs(fnp->fn_name);
			break;
		case _FIOFFS:
			flushfs(fnp->fn_name);
			break;
		default:
			break;
		}
	}

	/*
	 * all done
	 */
	return (exitstatus);
}
/*
 * exitusage
 *	bad command line, give hint
 */
void
exitusage()
{
	printf("usage: lockfs [-dfhnuw] [-c string] [-a] [file system ...]\n");
	exit(1);
}
/*
 * printstatusline
 * 	prettyprint the status line
 */
void
printstatusline(char *fn, char *locktype, char *comment)
{
	if (firsttime++ == 0)
		printf("%-20s %-10s %s\n", "Filesystem", "Locktype", "Comment");
	printf("%-20s %-10s %s\n", fn, locktype, comment);
}
/*
 * printstatus
 *	get and prettyprint file system lock status
 */
void
printstatus(char *fn)
{
	int		fd;
	int		fsmod	= 0;
	char		*locktype;
	char		commentbuffer[LOCKFS_MAXCOMMENTLEN+1];
	struct lockfs	lf;

	fd = open64(fn, O_RDONLY);
	if (fd == -1) {
		if (errno == EIO)
			printstatusline(fn, "EIO", "May be hard locked");
		else
			perror(fn);
		exitstatus = 2;
		return;
	}

	bzero((caddr_t)&lf, sizeof (struct lockfs));

	lf.lf_flags   = LOCKFS_MOD;
	lf.lf_comlen  = LOCKFS_MAXCOMMENTLEN;
	lf.lf_comment = commentbuffer;

	if (ioctl(fd, _FIOLFSS, &lf) == -1) {
		perror(fn);
		close(fd);
		exitstatus = 2;
		return;
	}
	switch (lf.lf_lock) {
	case LOCKFS_ULOCK:
		if (no_unlocks_printed)
			goto out;
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(unlock)";
		else
			locktype = "unlock";
		break;
	case LOCKFS_WLOCK:
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(write)";
		else {
			locktype = "write";
			fsmod = LOCKFS_IS_MOD(&lf);
		}
		break;
	case LOCKFS_NLOCK:
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(name)";
		else
			locktype = "name";
		break;
	case LOCKFS_DLOCK:
		locktype = "delete";
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(delete)";
		else
			locktype = "delete";
		break;
	case LOCKFS_HLOCK:
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(hard)";
		else {
			locktype = "hard";
			fsmod = LOCKFS_IS_MOD(&lf);
		}
		break;
	case LOCKFS_ELOCK:
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(error)";
		else {
			locktype = "error";
			fsmod = LOCKFS_IS_MOD(&lf);
		}
		break;
	default:
		if (LOCKFS_IS_BUSY(&lf))
			locktype = "(unknown)";
		else
			locktype = "unknown";
		break;
	}
	lf.lf_comment[lf.lf_comlen] = '\0';
	printstatusline(fn, locktype, lf.lf_comment);
	if (fsmod)
		LOCKWARN(fn, locktype);
out:
	close(fd);
}
/*
 * flushfs
 *	push and invalidate at least the data that is *currently* dirty
 */
void
flushfs(char *fn)
{
	int		fd;

	fd = open64(fn, O_RDONLY);
	if (fd == -1) {
		perror(fn);
		exitstatus = 2;
		return;
	}

	if (ioctl(fd, _FIOFFS, NULL) == -1) {
		perror(fn);
		close(fd);
		exitstatus = 2;
		return;
	}
	close(fd);
}
/*
 * lockfs
 *	lock the file system
 */
void
lockfs(char *fn)
{
	int		fd;
	struct lockfs	lf;

	fd = open64(fn, O_RDONLY);
	if (fd == -1) {
		perror(fn);
		exitstatus = 2;
		return;
	}

	bzero((caddr_t)&lf, sizeof (struct lockfs));

	lf.lf_flags = LOCKFS_MOD;
	if (ioctl(fd, _FIOLFSS, &lf) == -1) {
		perror(fn);
		close(fd);
		exitstatus = 2;
		return;
	}

	if (!LOCKFS_IS_BUSY(&lf) && LOCKFS_IS_MOD(&lf)) {
		if (LOCKFS_IS_HLOCK(&lf))
			LOCKWARN(fn, "hard");
		if (LOCKFS_IS_ELOCK(&lf))
			LOCKWARN(fn, "error");
		if (LOCKFS_IS_WLOCK(&lf))
			LOCKWARN(fn, "write");
	}

	lf.lf_lock	= lock;
	lf.lf_flags	= 0;
	lf.lf_key	= lf.lf_key;
	lf.lf_comment	= comment;
	lf.lf_comlen	= (comment) ? strlen(comment)+1 : 0;

	if (ioctl(fd, _FIOLFS, &lf) == -1) {
		perror(fn);
		close(fd);
		exitstatus = 2;
		return;
	}
	close(fd);
}
/*
 * getmntnames
 *	file names from /etc/mtab
 */
void
getmntnames()
{
	int		fnlen;
	struct filename	*fnp;
	struct filename	*fnpc;
	FILE		*mnttab;
	struct mnttab	mnt, *mntp = &mnt;

	fnpc = fnanchor;

	if ((mnttab = fopen(MNTTAB, "r")) == NULL) {
		fprintf(stderr, "Can't open %s\n", MNTTAB);
		perror(MNTTAB);
		exit(32);
	}
	while ((getmntent(mnttab, mntp)) == 0) {
		if (strcmp(mntp->mnt_fstype, MNTTYPE_UFS) != 0)
			continue;
		fnlen = strlen(mntp->mnt_mountp) + 1;
		fnp = (struct filename *)malloc(sizeof (struct filename));
		fnp->fn_name = malloc((uint_t)fnlen);
		strcpy(fnp->fn_name, mntp->mnt_mountp);
		fnp->fn_next = NULL;
		if (fnpc)
			fnpc->fn_next = fnp;
		else
			fnanchor = fnp;
		fnpc = fnp;
	}
	fclose(mnttab);
}
/*
 * getcmdnames
 *	file names from command line
 */
void
getcmdnames(int argc, char **argv, int i)
{
	struct filename	*fnp;
	struct filename	*fnpc;

	for (fnpc = fnanchor; i < argc; ++i) {
		fnp = (struct filename *)malloc(sizeof (struct filename));
		fnp->fn_name = *(argv+i);
		fnp->fn_next = NULL;
		if (fnpc)
			fnpc->fn_next = fnp;
		else
			fnanchor = fnp;
		fnpc = fnp;
	}
}