|
root / base / usr / src / cmd / fs.d / ufs / roll_log
roll_log Plain Text 639 lines 14.9 KB
  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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (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 (c) 1996,1997 by Sun Microsystems, Inc.
# All rights reserved.
#

include		../../Makefile.fstype

INCLUDES=	roll_log.h
SRCS=		roll_log.c
OBJS=		$(SRCS:%.c=$(MACH)/%.o)
POFILE=		roll_log.po

CERRWARN +=	-Wno-switch

.KEEP_STATE:

all install:	$(MACH) .WAIT $(OBJS)

$(OBJS):	$(INCLUDES)

$(MACH):
	$(MKDIR) -p $@

$(MACH)/%.o:	%.c
	$(COMPILE.c) -o $@ $<

clean:
	$(RM) $(OBJS)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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.
 */

/*
 * This file contains functions that allow applications to roll the log.
 * It is intended for use by applications that open a raw device with the
 * understanding that it contains a Unix File System.
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/filio.h>
#include <sys/mnttab.h>
#include <sys/mntent.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fs/ufs_mount.h>
#include <sys/fs/ufs_log.h>
#include <libintl.h>
#include "roll_log.h"

/*
 * The following is the template string passed to mktemp(3C).  This
 * string is used as the name of a temporary mount point which is
 * used to roll the log.
 */
#define	RLG_TEMPLATE	".rlg.XXXXXX"

#define	SYSERR		(-1)

#define	RLM_RW		0
#define	RLM_RO		1

/*
 * Structure definitions:
 */

typedef struct log_info {
	char *li_blkname;	/* Path of block device. */
	char *li_mntpoint;	/* Path of mounted device. */
	char *li_tmpmp_parent;	/* Temporary parent directory of mount point */
	char *li_tmpmp;		/* Temporary mount point. */
} log_info_t;

/*
 * Static function declarations:
 */

static rl_result_t	is_mounted(log_info_t *lip, char *dev);
static void		cleanup(log_info_t *lip);
static rl_result_t	make_mp(log_info_t *lip);
static rl_result_t	rlflush(log_info_t *lip);
static rl_result_t	rlmount(log_info_t *lip, int mntopt);
static rl_result_t	rlumount(log_info_t *lip);

/*
 * NAME
 *	rl_roll_log
 *
 * SYNOPSIS
 *	rl_roll_log(block_dev)
 *
 * DESCRIPTION
 *	Roll the log for the block device "block_dev".
 */

rl_result_t
rl_roll_log(char *bdev)
{
	log_info_t		li;
	rl_result_t		rv = RL_SUCCESS;

	(void) memset((void *)&li, 0, (size_t)sizeof (li));
	if (is_mounted(&li, bdev) == RL_TRUE) {
		rv = rlflush(&li);
	} else {
		/*
		 * Device appears not to be mounted.
		 * We need to mount the device read only.
		 * This automatically causes the log to be rolled, then we can
		 * unmount the device again.  To do the mount, we need to
		 * create a temporary directory, and then remove it when we
		 * are done.
		 */
		rv = make_mp(&li);
		switch (rv) {
		case RL_CORRUPT:
			/* corrupt mnttab - the file sys really was mounted */
			rv = rlflush(&li);
			break;
		case RL_SUCCESS:
			rv = rlmount(&li, RLM_RO);
			if (rv == RL_SUCCESS) {
				rv = rlflush(&li);
				if (umount(li.li_blkname) == SYSERR) {
					(void) fprintf(stderr,
		"WARNING: rl_roll_log(): Can't unmount %s\n", li.li_blkname);
				}

			}
			break;
		}
	}
	cleanup(&li);
	return (rv);
}

/*
 * Static function definitions:
 */

/*
 * NAME
 *	cleanup
 *
 * SYNOPSIS
 *	cleanup(log_infop)
 *
 * DESCRIPTION
 *	Remove the temporary mount directroy and free the dynamically
 *	allocated memory that is pointed to by log_infop.
 */

static void
cleanup(log_info_t *lip)
{
	if (lip->li_blkname != (char *)NULL) {
		free(lip->li_blkname);
		lip->li_blkname = (char *)NULL;
	}
	if (lip->li_mntpoint != (char *)NULL) {
		free(lip->li_mntpoint);
		lip->li_mntpoint = (char *)NULL;
	}
	if (lip->li_tmpmp != (char *)NULL) {
		(void) rmdir(lip->li_tmpmp);
		free(lip->li_tmpmp);
		lip->li_tmpmp = (char *)NULL;
	}
	if (lip->li_tmpmp_parent != (char *)NULL) {
		(void) rmdir(lip->li_tmpmp_parent);
		free(lip->li_tmpmp_parent);
		lip->li_tmpmp_parent = (char *)NULL;
	}
}

/*
 * NAME
 *	is_mounted
 *
 * SYNOPSIS
 *	is_mounted(log_infop, dev)
 *
 * DESCRIPTION
 *	Determine if device dev is mounted, and return RL_TRUE if it is.
 *	As a side effect, li_blkname is set to point the the full path
 *	names of the block device.  Memory for this path is dynamically
 *	allocated and must be freed by the caller.
 */

extern char *getfullblkname(char *);

static rl_result_t
is_mounted(log_info_t *lip, char *dev)
{

	struct mnttab		mntbuf;
	FILE			*mnttable;
	rl_result_t		rv = RL_FALSE;

	/* Make sure that we have the full path name. */
	lip->li_blkname = getfullblkname(dev);
	if (lip->li_blkname == NULL)
		lip->li_blkname = strdup(dev);

	/* Search mnttab to see if it device is mounted. */
	if ((mnttable = fopen(MNTTAB, "r")) == NULL)
		return (rv);
	while (getmntent(mnttable, &mntbuf) == 0) {
		if (strcmp(mntbuf.mnt_fstype, MNTTYPE_UFS) == 0) {
			/* Entry is UFS */
			if ((strcmp(mntbuf.mnt_mountp, dev) == 0) ||
			    (strcmp(mntbuf.mnt_special, lip->li_blkname)
			    == 0) ||
			    (strcmp(mntbuf.mnt_special, dev) == 0)) {
				lip->li_mntpoint = strdup(mntbuf.mnt_mountp);
				rv = RL_TRUE;
				break;
			}
		}
	}
	(void) fclose(mnttable);


	return (rv);
}

/*
 * NAME
 *	make_mp
 *
 * SYNOPSIS
 *	make_mp(loginfop)
 *
 * DESCRIPTION
 *	Create a temporary directory to be used as a mount point.  li_tmpmp
 *	will be set to the path of the mount point. li_tmpmp_parent is the
 *	parent directory of the mount point.  The parent directory is
 *	created with restrictive permissions.   Memory pointed to by
 *	li_tmpmp and li_tmpmp_parent should be freed by the caller.
 */

static rl_result_t
make_mp(log_info_t *lip)
{
	size_t			i;
	rl_result_t		rv = RL_FAIL;
	/*
	 * Note tmp_dir_list[] should all be directories in the
	 * original root file system.
	 */
	static const char	*tmp_dir_list[] = {
							"/tmp/",
							"/var/tmp/",
							"/",
						};
	char			dirname[] = RLG_TEMPLATE;
	char			tmp_dir[MAXPATHLEN + 1];
	char			mountpt_dir[MAXPATHLEN + 1];
	static size_t		list_len = sizeof (tmp_dir_list) /
	    sizeof (const char *);
	int			merr = 0;

	/*
	 * Sequence of events:
	 * - Create a random name using mktemp(3C) (e.g., ".rlg.123456")
	 * - Cycle through tmp_dir_list to find a path where we can create
	 *   a temporary parent directory (e.g., /tmp/.rlg.123456) with
	 *   restrictive permissions.  This prevents any non-root processes,
	 *   such as a 'find', from wandering in where it doesn't belong.
	 * - Create the mount-point (/tmp/.rlg.123456/.rlg.123456).
	 */
	(void) mktemp(dirname);
	for (i = 0; i < list_len; i++) {
		/* Make the directory containing the mount-point */
		(void) snprintf(tmp_dir, sizeof (tmp_dir), "%s%s",
		    tmp_dir_list[i], dirname);
		if (mkdir(tmp_dir, 0) == SYSERR) {
			merr = errno;
			continue;
		}

		/* Now, make the mount-point */
		(void) snprintf(mountpt_dir, sizeof (mountpt_dir), "%s/%s",
		    tmp_dir, dirname);
		if (mkdir(mountpt_dir, 0) == SYSERR) {
			merr = errno;
			continue;
		}
		lip->li_tmpmp = strdup(mountpt_dir);
		lip->li_tmpmp_parent = strdup(tmp_dir);

		/* Make sure that the strdup()s both succeeded */
		if ((lip->li_tmpmp != NULL) && (lip->li_tmpmp_parent != NULL)) {
			rv = RL_SUCCESS;
		}
		break;
	}

	/* Get some help if we cannot make the directory. */
	if (rv != RL_SUCCESS) {
		/*
		 * If we get a read only filesystem failure (EROFS)
		 * to make a directory in "/", then we must be fsck'ing
		 * at boot with a incorrect mnttab.
		 *
		 * Just return RL_CORRUPT to indicate it really
		 * was mounted.
		 */
		if (merr == EROFS) {
			lip->li_mntpoint = strdup("/");
			return (RL_CORRUPT);
		}

		(void) fprintf(stderr, gettext(
		    "Unable to create temporary "
		    "directory in any of the directories listed "
		    "below:\n"));
		for (i = 0; i < list_len; i++) {
			(void) fprintf(stderr, "\t%s\n", tmp_dir_list[i]);
		}
		(void) fprintf(stderr, gettext(
		    "Please correct this problem "
		    "and rerun the program.\n"));
	}

	return (rv);
}

/*
 * NAME
 *	rlflush
 *
 * SYNOPSIS
 *	rlflush(log_infop)
 *
 * DESCRIPTION
 *	Open the mount point of the file system (li_mntpoint) to get a
 *	file descriptor.  Issue the _FIOFFS ioctl to flush the file system
 *	and then close the device.
 */

static rl_result_t
rlflush(log_info_t *lip)
{
	int			fd;	/* File descriptor. */
	rl_result_t		rv = RL_SUCCESS;

	if ((fd = open((lip->li_mntpoint ? lip->li_mntpoint : lip->li_tmpmp),
	    O_RDONLY)) == SYSERR) {
		return (RL_SYSERR);
	}
	if (ioctl(fd, _FIOFFS, NULL) == SYSERR) {
		rv = RL_SYSERR;
	}
	(void) close(fd);
	return (rv);
}

/*
 * NAME
 *	rlmount
 *
 * SYNOPSIS
 *	rlmount(log_infop, mntopt)
 *
 * DESCRIPTION
 *	Mount the device specified by li_blkname on li_tmpmp. mntopt specifies
 *	whether it's mounted RLM_RO or RLM_RW.
 */

static rl_result_t
rlmount(log_info_t *lip, int mntopt)
{
	struct ufs_args		args;
	rl_result_t		rv = RL_SUCCESS;
	char			opt[MAX_MNTOPT_STR];
	char			*optstr;
	int			optflg;

	args.flags = 0;	/* Initialize ufs_args */

	/*
	 * Use a minimal restrictive set of mount options.  Make sure
	 * to use "largefiles" option otherwise mount() can fail w/EFBIG.
	 * (Although "nosub" isn't a currently supported option on UFS,
	 * it would be a good one to have if it ever is implemented
	 * since submounts would prevent a umount.)
	 */
	args.flags |= UFSMNT_LARGEFILES;
	switch (mntopt) {
	case RLM_RO:
		optstr = MNTOPT_RO;
		optflg = MS_RDONLY;
		break;
	case RLM_RW:
		optstr = MNTOPT_RW;
		optflg = 0;
		break;
	default:
		return (RL_FAIL);
	}
	(void) snprintf(opt, sizeof (opt), "%s,%s,%s",
	    optstr, MNTOPT_NOSUID, MNTOPT_LARGEFILES);
	if (mount(lip->li_blkname, lip->li_tmpmp,
	    optflg | MS_DATA | MS_OPTIONSTR,
	    MNTTYPE_UFS, &args, sizeof (args),
	    opt, MAX_MNTOPT_STR) == SYSERR) {
		rv = RL_SYSERR;
	}
	return (rv);
}

/*
 * NAME
 *	rlumount
 *
 * SYNOPSIS
 *	rlumount(log_infop)
 *
 * DESCRIPTION
 *	Unmounts the device specified by li_blkname, printing an
 *	error message on failure.
 */

static rl_result_t
rlumount(log_info_t *lip)
{
	rl_result_t		rv = RL_SUCCESS;

	if (umount(lip->li_blkname) == SYSERR) {
		(void) fprintf(stderr, gettext(
		    "WARNING: rlumount(): Can't unmount %s\n"),
		    lip->li_blkname);
		rv = RL_SYSERR;
	}
	return (rv);
}

/*
 * NAME
 *	rl_log_control
 *
 * SYNOPSIS
 *	rl_log_control(block_dev, request)
 *
 * DESCRIPTION
 *	Enable/disable logging for the block device "block_dev".
 *	The request parameter should be set to _FIOLOGENABLE or
 *	_FIOLOGDISABLE.
 */

rl_result_t
rl_log_control(char *bdev, int request)
{
	log_info_t	li;
	rl_result_t	rv = RL_SUCCESS;
	rl_result_t	alreadymounted;
	int		fd;
	fiolog_t	fl;
	int		logenabled = 0;

	if ((request != _FIOLOGENABLE) && (request != _FIOLOGDISABLE))
		return (RL_FAIL);

	(void) memset((void *)&li, '\0', (size_t)sizeof (li));
	if ((alreadymounted = is_mounted(&li, bdev)) != RL_TRUE) {
		/*
		 * Device is not mounted. Need to mount it rw to allow
		 * the log to be enabled/disabled. To do the mount, we need
		 * to create a temporary directory, and then remove it when
		 * we are done.
		 */
		if (make_mp(&li) != RL_SUCCESS) {
			cleanup(&li);
			return (RL_FAIL);
		}
		if (rlmount(&li, RLM_RW) != RL_SUCCESS) {
			cleanup(&li);
			return (RL_FAIL);
		}
	}

	if (alreadymounted == RL_TRUE)
		fd = open(li.li_mntpoint, O_RDONLY);
	else
		fd = open(li.li_tmpmp, O_RDONLY);
	if (fd == SYSERR) {
		perror("open");
		rv = RL_SYSERR;
		goto out;
	}

	fl.nbytes_requested = 0;
	fl.nbytes_actual = 0;
	fl.error = FIOLOG_ENONE;

	if (ioctl(fd, request, &fl) == SYSERR) {
		perror("ioctl");
		(void) close(fd);
		rv = RL_SYSERR;
		goto out;
	}
	if (ioctl(fd, _FIOISLOG, &logenabled) == SYSERR) {
		perror("ioctl");
		(void) close(fd);
		rv = RL_SYSERR;
		goto out;
	}
	if (((request == _FIOLOGENABLE) && (!logenabled)) ||
	    ((request == _FIOLOGDISABLE) && logenabled))
		rv = RL_FAIL;

	(void) close(fd);
out:
	if (alreadymounted != RL_TRUE)
		(void) rlumount(&li);
	cleanup(&li);
	return (rv);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 (c) 1996,1997,2001 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#ifndef	_ROLL_LOG_H
#define	_ROLL_LOG_H

#include <sys/fs/ufs_fs.h>

#ifdef	__cplusplus
extern "C" {
#endif

/*
 * This file contains definitions for the module that rolls the Unix File
 * System log.
 */

/*
 * rl_result_t defines the type of the value that is returned by all roll
 * log functions.
 */

typedef enum rl_result {
	/*
	 * Choose values so that all passing returns are >= 0, and all
	 * failing returns are < 0.
	 */

	RL_CORRUPT = -4,		/* Corrupted on disk structure. */
	RL_FAIL = -3,			/* Generic failure. */
	RL_SYSERR = -2,			/* Failing system call. */
	RL_FALSE = -1,
	RL_SUCCESS = 0,
	RL_TRUE = 1
} rl_result_t;

/* Functions defined in roll_log.c */

extern rl_result_t	rl_roll_log(char *dev);
extern rl_result_t	rl_log_control(char *dev, int request);

#ifdef	__cplusplus
}
#endif

#endif	/* _ROLL_LOG_H */