xref: /onnv-gate/usr/src/cmd/fs.d/ufs/roll_log/roll_log.c (revision 392:68e38aa09ba8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*392Sswilcox  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * This file contains functions that allow applications to roll the log.
310Sstevel@tonic-gate  * It is intended for use by applications that open a raw device with the
320Sstevel@tonic-gate  * understanding that it contains a Unix File System.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <fcntl.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <strings.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <sys/filio.h>
430Sstevel@tonic-gate #include <sys/mnttab.h>
440Sstevel@tonic-gate #include <sys/mntent.h>
450Sstevel@tonic-gate #include <sys/mount.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/fs/ufs_mount.h>
500Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
510Sstevel@tonic-gate #include <libintl.h>
520Sstevel@tonic-gate #include "roll_log.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * The following is the template string passed to mktemp(3C).  This
560Sstevel@tonic-gate  * string is used as the name of a temporary mount point which is
570Sstevel@tonic-gate  * used to roll the log.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate #define	RLG_TEMPLATE	".rlg.XXXXXX"
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	SYSERR		(-1)
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	RLM_RW		0
640Sstevel@tonic-gate #define	RLM_RO		1
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Structure definitions:
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate typedef struct log_info {
710Sstevel@tonic-gate 	char *li_blkname;	/* Path of block device. */
720Sstevel@tonic-gate 	char *li_mntpoint;	/* Path of mounted device. */
730Sstevel@tonic-gate 	char *li_tmpmp_parent;	/* Temporary parent directory of mount point */
740Sstevel@tonic-gate 	char *li_tmpmp;		/* Temporary mount point. */
750Sstevel@tonic-gate } log_info_t;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Static function declarations:
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate static rl_result_t	is_mounted(log_info_t *lip, char *dev);
820Sstevel@tonic-gate static void		cleanup(log_info_t *lip);
830Sstevel@tonic-gate static rl_result_t	make_mp(log_info_t *lip);
840Sstevel@tonic-gate static rl_result_t	rlflush(log_info_t *lip);
850Sstevel@tonic-gate static rl_result_t	rlmount(log_info_t *lip, int mntopt);
860Sstevel@tonic-gate static rl_result_t	rlumount(log_info_t *lip);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * NAME
900Sstevel@tonic-gate  *	rl_roll_log
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * SYNOPSIS
930Sstevel@tonic-gate  *	rl_roll_log(block_dev)
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * DESCRIPTION
960Sstevel@tonic-gate  *	Roll the log for the block device "block_dev".
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate rl_result_t
rl_roll_log(char * bdev)1000Sstevel@tonic-gate rl_roll_log(char *bdev)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	log_info_t		li;
1030Sstevel@tonic-gate 	rl_result_t		rv = RL_SUCCESS;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	(void) memset((void *)&li, 0, (size_t)sizeof (li));
1060Sstevel@tonic-gate 	if (is_mounted(&li, bdev) == RL_TRUE) {
1070Sstevel@tonic-gate 		rv = rlflush(&li);
1080Sstevel@tonic-gate 	} else {
1090Sstevel@tonic-gate 		/*
1100Sstevel@tonic-gate 		 * Device appears not to be mounted.
1110Sstevel@tonic-gate 		 * We need to mount the device read only.
1120Sstevel@tonic-gate 		 * This automatically causes the log to be rolled, then we can
1130Sstevel@tonic-gate 		 * unmount the device again.  To do the mount, we need to
1140Sstevel@tonic-gate 		 * create a temporary directory, and then remove it when we
1150Sstevel@tonic-gate 		 * are done.
1160Sstevel@tonic-gate 		 */
1170Sstevel@tonic-gate 		rv = make_mp(&li);
1180Sstevel@tonic-gate 		switch (rv) {
1190Sstevel@tonic-gate 		case RL_CORRUPT:
1200Sstevel@tonic-gate 			/* corrupt mnttab - the file sys really was mounted */
1210Sstevel@tonic-gate 			rv = rlflush(&li);
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		case RL_SUCCESS:
1240Sstevel@tonic-gate 			rv = rlmount(&li, RLM_RO);
1250Sstevel@tonic-gate 			if (rv == RL_SUCCESS) {
1260Sstevel@tonic-gate 				rv = rlflush(&li);
1270Sstevel@tonic-gate 				if (umount(li.li_blkname) == SYSERR) {
1280Sstevel@tonic-gate 					(void) fprintf(stderr,
1290Sstevel@tonic-gate 		"WARNING: rl_roll_log(): Can't unmount %s\n", li.li_blkname);
1300Sstevel@tonic-gate 				}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 			}
1330Sstevel@tonic-gate 			break;
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate 	cleanup(&li);
1370Sstevel@tonic-gate 	return (rv);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate  * Static function definitions:
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate  * NAME
1460Sstevel@tonic-gate  *	cleanup
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * SYNOPSIS
1490Sstevel@tonic-gate  *	cleanup(log_infop)
1500Sstevel@tonic-gate  *
1510Sstevel@tonic-gate  * DESCRIPTION
1520Sstevel@tonic-gate  *	Remove the temporary mount directroy and free the dynamically
1530Sstevel@tonic-gate  *	allocated memory that is pointed to by log_infop.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static void
cleanup(log_info_t * lip)1570Sstevel@tonic-gate cleanup(log_info_t *lip)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	if (lip->li_blkname != (char *)NULL) {
1600Sstevel@tonic-gate 		free(lip->li_blkname);
1610Sstevel@tonic-gate 		lip->li_blkname = (char *)NULL;
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 	if (lip->li_mntpoint != (char *)NULL) {
1640Sstevel@tonic-gate 		free(lip->li_mntpoint);
1650Sstevel@tonic-gate 		lip->li_mntpoint = (char *)NULL;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 	if (lip->li_tmpmp != (char *)NULL) {
1680Sstevel@tonic-gate 		(void) rmdir(lip->li_tmpmp);
1690Sstevel@tonic-gate 		free(lip->li_tmpmp);
1700Sstevel@tonic-gate 		lip->li_tmpmp = (char *)NULL;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	if (lip->li_tmpmp_parent != (char *)NULL) {
1730Sstevel@tonic-gate 		(void) rmdir(lip->li_tmpmp_parent);
1740Sstevel@tonic-gate 		free(lip->li_tmpmp_parent);
1750Sstevel@tonic-gate 		lip->li_tmpmp_parent = (char *)NULL;
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * NAME
1810Sstevel@tonic-gate  *	is_mounted
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * SYNOPSIS
1840Sstevel@tonic-gate  *	is_mounted(log_infop, dev)
1850Sstevel@tonic-gate  *
1860Sstevel@tonic-gate  * DESCRIPTION
1870Sstevel@tonic-gate  *	Determine if device dev is mounted, and return RL_TRUE if it is.
1880Sstevel@tonic-gate  *	As a side effect, li_blkname is set to point the the full path
1890Sstevel@tonic-gate  *	names of the block device.  Memory for this path is dynamically
1900Sstevel@tonic-gate  *	allocated and must be freed by the caller.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate extern char *getfullblkname(char *);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate static rl_result_t
is_mounted(log_info_t * lip,char * dev)1960Sstevel@tonic-gate is_mounted(log_info_t *lip, char *dev)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	struct mnttab		mntbuf;
2000Sstevel@tonic-gate 	FILE			*mnttable;
2010Sstevel@tonic-gate 	rl_result_t		rv = RL_FALSE;
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/* Make sure that we have the full path name. */
2040Sstevel@tonic-gate 	lip->li_blkname = getfullblkname(dev);
2050Sstevel@tonic-gate 	if (lip->li_blkname == NULL)
2060Sstevel@tonic-gate 		lip->li_blkname = strdup(dev);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* Search mnttab to see if it device is mounted. */
2090Sstevel@tonic-gate 	if ((mnttable = fopen(MNTTAB, "r")) == NULL)
2100Sstevel@tonic-gate 		return (rv);
2110Sstevel@tonic-gate 	while (getmntent(mnttable, &mntbuf) == NULL) {
2120Sstevel@tonic-gate 		if (strcmp(mntbuf.mnt_fstype, MNTTYPE_UFS) == 0) {
2130Sstevel@tonic-gate 			/* Entry is UFS */
2140Sstevel@tonic-gate 			if ((strcmp(mntbuf.mnt_mountp, dev) == 0) ||
2150Sstevel@tonic-gate 			    (strcmp(mntbuf.mnt_special, lip->li_blkname)
2160Sstevel@tonic-gate 				== 0) ||
2170Sstevel@tonic-gate 			    (strcmp(mntbuf.mnt_special, dev) == 0)) {
2180Sstevel@tonic-gate 				lip->li_mntpoint = strdup(mntbuf.mnt_mountp);
2190Sstevel@tonic-gate 				rv = RL_TRUE;
2200Sstevel@tonic-gate 				break;
2210Sstevel@tonic-gate 			}
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 	(void) fclose(mnttable);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	return (rv);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * NAME
2320Sstevel@tonic-gate  *	make_mp
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * SYNOPSIS
2350Sstevel@tonic-gate  *	make_mp(loginfop)
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * DESCRIPTION
2380Sstevel@tonic-gate  *	Create a temporary directory to be used as a mount point.  li_tmpmp
2390Sstevel@tonic-gate  *	will be set to the path of the mount point. li_tmpmp_parent is the
2400Sstevel@tonic-gate  *	parent directory of the mount point.  The parent directory is
2410Sstevel@tonic-gate  *	created with restrictive permissions.   Memory pointed to by
2420Sstevel@tonic-gate  *	li_tmpmp and li_tmpmp_parent should be freed by the caller.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate static rl_result_t
make_mp(log_info_t * lip)2460Sstevel@tonic-gate make_mp(log_info_t *lip)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	size_t			i;
2490Sstevel@tonic-gate 	rl_result_t		rv = RL_FAIL;
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * Note tmp_dir_list[] should all be directories in the
2520Sstevel@tonic-gate 	 * original root file system.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	static const char 	*tmp_dir_list[] = {
2550Sstevel@tonic-gate 							"/tmp/",
2560Sstevel@tonic-gate 							"/var/tmp/",
2570Sstevel@tonic-gate 							"/",
2580Sstevel@tonic-gate 						};
2590Sstevel@tonic-gate 	char			dirname[] = RLG_TEMPLATE;
2600Sstevel@tonic-gate 	char			tmp_dir[MAXPATHLEN + 1];
2610Sstevel@tonic-gate 	char			mountpt_dir[MAXPATHLEN + 1];
2620Sstevel@tonic-gate 	static size_t		list_len = sizeof (tmp_dir_list) /
2630Sstevel@tonic-gate 						sizeof (const char *);
264*392Sswilcox 	int			merr = 0;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	/*
2670Sstevel@tonic-gate 	 * Sequence of events:
2680Sstevel@tonic-gate 	 * - Create a random name using mktemp(3C) (e.g., ".rlg.123456")
2690Sstevel@tonic-gate 	 * - Cycle through tmp_dir_list to find a path where we can create
2700Sstevel@tonic-gate 	 *   a temporary parent directory (e.g., /tmp/.rlg.123456) with
2710Sstevel@tonic-gate 	 *   restrictive permissions.  This prevents any non-root processes,
2720Sstevel@tonic-gate 	 *   such as a 'find', from wandering in where it doesn't belong.
2730Sstevel@tonic-gate 	 * - Create the mount-point (/tmp/.rlg.123456/.rlg.123456).
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 	(void) mktemp(dirname);
2760Sstevel@tonic-gate 	for (i = 0; i < list_len; i++) {
2770Sstevel@tonic-gate 		/* Make the directory containing the mount-point */
278*392Sswilcox 		(void) snprintf(tmp_dir, sizeof (tmp_dir), "%s%s",
279*392Sswilcox 				tmp_dir_list[i], dirname);
2800Sstevel@tonic-gate 		if (mkdir(tmp_dir, 0) == SYSERR) {
2810Sstevel@tonic-gate 			merr = errno;
2820Sstevel@tonic-gate 			continue;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		/* Now, make the mount-point */
286*392Sswilcox 		(void) snprintf(mountpt_dir, sizeof (mountpt_dir), "%s/%s",
287*392Sswilcox 				tmp_dir, dirname);
2880Sstevel@tonic-gate 		if (mkdir(mountpt_dir, 0) == SYSERR) {
2890Sstevel@tonic-gate 			merr = errno;
2900Sstevel@tonic-gate 			continue;
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 		lip->li_tmpmp = strdup(mountpt_dir);
2930Sstevel@tonic-gate 		lip->li_tmpmp_parent = strdup(tmp_dir);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		/* Make sure that the strdup()s both succeeded */
2960Sstevel@tonic-gate 		if ((lip->li_tmpmp != NULL) && (lip->li_tmpmp_parent != NULL)) {
2970Sstevel@tonic-gate 			rv = RL_SUCCESS;
2980Sstevel@tonic-gate 		}
2990Sstevel@tonic-gate 		break;
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	/* Get some help if we cannot make the directory. */
3030Sstevel@tonic-gate 	if (rv != RL_SUCCESS) {
3040Sstevel@tonic-gate 		/*
3050Sstevel@tonic-gate 		 * If we get a read only filesystem failure (EROFS)
3060Sstevel@tonic-gate 		 * to make a directory in "/", then we must be fsck'ing
3070Sstevel@tonic-gate 		 * at boot with a incorrect mnttab.
3080Sstevel@tonic-gate 		 *
3090Sstevel@tonic-gate 		 * Just return RL_CORRUPT to indicate it really
3100Sstevel@tonic-gate 		 * was mounted.
3110Sstevel@tonic-gate 		 */
3120Sstevel@tonic-gate 		if (merr == EROFS) {
3130Sstevel@tonic-gate 			lip->li_mntpoint = strdup("/");
3140Sstevel@tonic-gate 			return (RL_CORRUPT);
3150Sstevel@tonic-gate 		}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
3180Sstevel@tonic-gate 			"Unable to create temporary "
3190Sstevel@tonic-gate 			"directory in any of the directories listed "
3200Sstevel@tonic-gate 			"below:\n"));
3210Sstevel@tonic-gate 		for (i = 0; i < list_len; i++) {
3220Sstevel@tonic-gate 			(void) fprintf(stderr, "\t%s\n", tmp_dir_list[i]);
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
3250Sstevel@tonic-gate 			"Please correct this problem "
3260Sstevel@tonic-gate 			"and rerun the program.\n"));
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	return (rv);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * NAME
3340Sstevel@tonic-gate  *	rlflush
3350Sstevel@tonic-gate  *
3360Sstevel@tonic-gate  * SYNOPSIS
3370Sstevel@tonic-gate  *	rlflush(log_infop)
3380Sstevel@tonic-gate  *
3390Sstevel@tonic-gate  * DESCRIPTION
3400Sstevel@tonic-gate  *	Open the mount point of the file system (li_mntpoint) to get a
3410Sstevel@tonic-gate  *	file descriptor.  Issue the _FIOFFS ioctl to flush the file system
3420Sstevel@tonic-gate  *	and then close the device.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate static rl_result_t
rlflush(log_info_t * lip)3460Sstevel@tonic-gate rlflush(log_info_t *lip)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	int			fd;	/* File descriptor. */
3490Sstevel@tonic-gate 	rl_result_t		rv = RL_SUCCESS;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if ((fd = open((lip->li_mntpoint ? lip->li_mntpoint : lip->li_tmpmp),
3520Sstevel@tonic-gate 		O_RDONLY)) == SYSERR) {
3530Sstevel@tonic-gate 		return (RL_SYSERR);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	if (ioctl(fd, _FIOFFS, NULL) == SYSERR) {
3560Sstevel@tonic-gate 		rv = RL_SYSERR;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 	(void) close(fd);
3590Sstevel@tonic-gate 	return (rv);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate  * NAME
3640Sstevel@tonic-gate  *	rlmount
3650Sstevel@tonic-gate  *
3660Sstevel@tonic-gate  * SYNOPSIS
3670Sstevel@tonic-gate  *	rlmount(log_infop, mntopt)
3680Sstevel@tonic-gate  *
3690Sstevel@tonic-gate  * DESCRIPTION
3700Sstevel@tonic-gate  *	Mount the device specified by li_blkname on li_tmpmp. mntopt specifies
3710Sstevel@tonic-gate  *	whether it's mounted RLM_RO or RLM_RW.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate static rl_result_t
rlmount(log_info_t * lip,int mntopt)3750Sstevel@tonic-gate rlmount(log_info_t *lip, int mntopt)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	struct ufs_args		args;
3780Sstevel@tonic-gate 	rl_result_t		rv = RL_SUCCESS;
3790Sstevel@tonic-gate 	char			opt[MAX_MNTOPT_STR];
3800Sstevel@tonic-gate 	char			*optstr;
3810Sstevel@tonic-gate 	int			optflg;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	args.flags = 0;	/* Initialize ufs_args */
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * Use a minimal restrictive set of mount options.  Make sure
3870Sstevel@tonic-gate 	 * to use "largefiles" option otherwise mount() can fail w/EFBIG.
3880Sstevel@tonic-gate 	 * (Although "nosub" isn't a currently supported option on UFS,
3890Sstevel@tonic-gate 	 * it would be a good one to have if it ever is implemented
3900Sstevel@tonic-gate 	 * since submounts would prevent a umount.)
3910Sstevel@tonic-gate 	 */
3920Sstevel@tonic-gate 	args.flags |= UFSMNT_LARGEFILES;
3930Sstevel@tonic-gate 	switch (mntopt) {
3940Sstevel@tonic-gate 	case RLM_RO:
3950Sstevel@tonic-gate 		optstr = MNTOPT_RO;
3960Sstevel@tonic-gate 		optflg = MS_RDONLY;
3970Sstevel@tonic-gate 		break;
3980Sstevel@tonic-gate 	case RLM_RW:
3990Sstevel@tonic-gate 		optstr = MNTOPT_RW;
4000Sstevel@tonic-gate 		optflg = 0;
4010Sstevel@tonic-gate 		break;
4020Sstevel@tonic-gate 	default:
4030Sstevel@tonic-gate 		return (RL_FAIL);
4040Sstevel@tonic-gate 	}
405*392Sswilcox 	(void) snprintf(opt, sizeof (opt), "%s,%s,%s",
4060Sstevel@tonic-gate 		optstr, MNTOPT_NOSUID, MNTOPT_LARGEFILES);
4070Sstevel@tonic-gate 	if (mount(lip->li_blkname, lip->li_tmpmp,
4080Sstevel@tonic-gate 			optflg | MS_DATA | MS_OPTIONSTR,
4090Sstevel@tonic-gate 			MNTTYPE_UFS, &args, sizeof (args),
4100Sstevel@tonic-gate 			opt, MAX_MNTOPT_STR) == SYSERR) {
4110Sstevel@tonic-gate 		rv = RL_SYSERR;
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 	return (rv);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * NAME
4180Sstevel@tonic-gate  *	rlumount
4190Sstevel@tonic-gate  *
4200Sstevel@tonic-gate  * SYNOPSIS
4210Sstevel@tonic-gate  *	rlumount(log_infop)
4220Sstevel@tonic-gate  *
4230Sstevel@tonic-gate  * DESCRIPTION
4240Sstevel@tonic-gate  *	Unmounts the device specified by li_blkname, printing an
4250Sstevel@tonic-gate  *	error message on failure.
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate static rl_result_t
rlumount(log_info_t * lip)4290Sstevel@tonic-gate rlumount(log_info_t *lip)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	rl_result_t		rv = RL_SUCCESS;
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	if (umount(lip->li_blkname) == SYSERR) {
4340Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4350Sstevel@tonic-gate 		"WARNING: rlumount(): Can't unmount %s\n"),
4360Sstevel@tonic-gate 			lip->li_blkname);
4370Sstevel@tonic-gate 		rv = RL_SYSERR;
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 	return (rv);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*
4430Sstevel@tonic-gate  * NAME
4440Sstevel@tonic-gate  *	rl_log_control
4450Sstevel@tonic-gate  *
4460Sstevel@tonic-gate  * SYNOPSIS
4470Sstevel@tonic-gate  *	rl_log_control(block_dev, request)
4480Sstevel@tonic-gate  *
4490Sstevel@tonic-gate  * DESCRIPTION
4500Sstevel@tonic-gate  *	Enable/disable logging for the block device "block_dev".
4510Sstevel@tonic-gate  *	The request parameter should be set to _FIOLOGENABLE or
4520Sstevel@tonic-gate  *	_FIOLOGDISABLE.
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate rl_result_t
rl_log_control(char * bdev,int request)4560Sstevel@tonic-gate rl_log_control(char *bdev, int request)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	log_info_t	li;
4590Sstevel@tonic-gate 	rl_result_t	rv = RL_SUCCESS;
4600Sstevel@tonic-gate 	rl_result_t	alreadymounted;
4610Sstevel@tonic-gate 	int		fd;
4620Sstevel@tonic-gate 	fiolog_t	fl;
463*392Sswilcox 	int		logenabled = 0;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if ((request != _FIOLOGENABLE) && (request != _FIOLOGDISABLE))
4660Sstevel@tonic-gate 		return (RL_FAIL);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	(void) memset((void *)&li, '\0', (size_t)sizeof (li));
4690Sstevel@tonic-gate 	if ((alreadymounted = is_mounted(&li, bdev)) != RL_TRUE) {
4700Sstevel@tonic-gate 		/*
4710Sstevel@tonic-gate 		 * Device is not mounted. Need to mount it rw to allow
4720Sstevel@tonic-gate 		 * the log to be enabled/disabled. To do the mount, we need
4730Sstevel@tonic-gate 		 * to create a temporary directory, and then remove it when
4740Sstevel@tonic-gate 		 * we are done.
4750Sstevel@tonic-gate 		 */
4760Sstevel@tonic-gate 		if (make_mp(&li) != RL_SUCCESS) {
4770Sstevel@tonic-gate 			cleanup(&li);
4780Sstevel@tonic-gate 			return (RL_FAIL);
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		if (rlmount(&li, RLM_RW) != RL_SUCCESS) {
4810Sstevel@tonic-gate 			cleanup(&li);
4820Sstevel@tonic-gate 			return (RL_FAIL);
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	if (alreadymounted == RL_TRUE)
4870Sstevel@tonic-gate 		fd = open(li.li_mntpoint, O_RDONLY);
4880Sstevel@tonic-gate 	else
4890Sstevel@tonic-gate 		fd = open(li.li_tmpmp, O_RDONLY);
4900Sstevel@tonic-gate 	if (fd == SYSERR) {
4910Sstevel@tonic-gate 		perror("open");
4920Sstevel@tonic-gate 		rv = RL_SYSERR;
4930Sstevel@tonic-gate 		goto out;
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	fl.nbytes_requested = 0;
4970Sstevel@tonic-gate 	fl.nbytes_actual = 0;
4980Sstevel@tonic-gate 	fl.error = FIOLOG_ENONE;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	if (ioctl(fd, request, &fl) == SYSERR) {
5010Sstevel@tonic-gate 		perror("ioctl");
5020Sstevel@tonic-gate 		(void) close(fd);
5030Sstevel@tonic-gate 		rv = RL_SYSERR;
5040Sstevel@tonic-gate 		goto out;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 	if (ioctl(fd, _FIOISLOG, &logenabled) == SYSERR) {
5070Sstevel@tonic-gate 		perror("ioctl");
5080Sstevel@tonic-gate 		(void) close(fd);
5090Sstevel@tonic-gate 		rv = RL_SYSERR;
5100Sstevel@tonic-gate 		goto out;
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	if (((request == _FIOLOGENABLE) && (!logenabled)) ||
5130Sstevel@tonic-gate 	    ((request == _FIOLOGDISABLE) && logenabled))
5140Sstevel@tonic-gate 		rv = RL_FAIL;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	(void) close(fd);
5170Sstevel@tonic-gate out:
5180Sstevel@tonic-gate 	if (alreadymounted != RL_TRUE)
5190Sstevel@tonic-gate 		(void) rlumount(&li);
5200Sstevel@tonic-gate 	cleanup(&li);
5210Sstevel@tonic-gate 	return (rv);
5220Sstevel@tonic-gate }
523