Home | History | Annotate | Line # | Download | only in mount_nilfs
      1  1.4      maya /* $NetBSD: mount_nilfs.c,v 1.4 2019/10/16 21:52:22 maya Exp $ */
      2  1.1   reinoud 
      3  1.1   reinoud /*
      4  1.1   reinoud  * Copyright (c) 2008, 2009 Reinoud Zandijk
      5  1.1   reinoud  * All rights reserved.
      6  1.1   reinoud  *
      7  1.1   reinoud  * Redistribution and use in source and binary forms, with or without
      8  1.1   reinoud  * modification, are permitted provided that the following conditions
      9  1.1   reinoud  * are met:
     10  1.1   reinoud  * 1. Redistributions of source code must retain the above copyright
     11  1.1   reinoud  *    notice, this list of conditions and the following disclaimer.
     12  1.1   reinoud  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1   reinoud  *    notice, this list of conditions and the following disclaimer in the
     14  1.1   reinoud  *    documentation and/or other materials provided with the distribution.
     15  1.1   reinoud  *
     16  1.1   reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1   reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1   reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1   reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1   reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1   reinoud  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1   reinoud  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1   reinoud  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1   reinoud  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1   reinoud  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1   reinoud  *
     27  1.1   reinoud  */
     28  1.1   reinoud 
     29  1.1   reinoud 
     30  1.1   reinoud #include <sys/cdefs.h>
     31  1.1   reinoud #ifndef lint
     32  1.4      maya __RCSID("$NetBSD: mount_nilfs.c,v 1.4 2019/10/16 21:52:22 maya Exp $");
     33  1.1   reinoud #endif /* not lint */
     34  1.1   reinoud 
     35  1.1   reinoud 
     36  1.1   reinoud #include <sys/param.h>
     37  1.1   reinoud #include <sys/mount.h>
     38  1.1   reinoud #include <sys/stat.h>
     39  1.1   reinoud 
     40  1.1   reinoud #include <assert.h>
     41  1.1   reinoud #include <err.h>
     42  1.1   reinoud #include <errno.h>
     43  1.1   reinoud #include <grp.h>
     44  1.1   reinoud #include <pwd.h>
     45  1.1   reinoud #include <stdio.h>
     46  1.1   reinoud #include <stdlib.h>
     47  1.1   reinoud #include <string.h>
     48  1.1   reinoud #include <time.h>
     49  1.1   reinoud #include <unistd.h>
     50  1.1   reinoud #include <util.h>
     51  1.1   reinoud 
     52  1.1   reinoud 
     53  1.1   reinoud /* mount specific options */
     54  1.1   reinoud #include <fs/nilfs/nilfs_mount.h>
     55  1.1   reinoud #include <mntopts.h>
     56  1.1   reinoud 
     57  1.1   reinoud #include "mountprog.h"
     58  1.1   reinoud #include "mount_nilfs.h"
     59  1.1   reinoud 
     60  1.1   reinoud /* options to pass on to the `mount' call */
     61  1.1   reinoud static const struct mntopt mopts[] = {
     62  1.1   reinoud 	MOPT_STDOPTS,		/* `normal' options		*/
     63  1.1   reinoud 	MOPT_ASYNC,		/* default			*/
     64  1.1   reinoud 	MOPT_NOATIME,		/* dont update access times	*/
     65  1.3  christos 	MOPT_RELATIME,		/* update access times on change*/
     66  1.1   reinoud 	MOPT_UPDATE,		/* not yet supported		*/
     67  1.1   reinoud 	MOPT_GETARGS,		/* printing			*/
     68  1.1   reinoud 	MOPT_NULL,
     69  1.1   reinoud };
     70  1.1   reinoud 
     71  1.1   reinoud 
     72  1.1   reinoud /* prototypes */
     73  1.1   reinoud static void	usage(void) __dead;
     74  1.1   reinoud 
     75  1.1   reinoud 
     76  1.1   reinoud /* code */
     77  1.1   reinoud 
     78  1.1   reinoud static void
     79  1.1   reinoud usage(void)
     80  1.1   reinoud {
     81  1.1   reinoud 	(void)fprintf(stderr, "Usage: %s [-o options] [-c cpno] "
     82  1.1   reinoud 	    "[-t gmtoff] special node\n", getprogname());
     83  1.1   reinoud 	exit(EXIT_FAILURE);
     84  1.1   reinoud }
     85  1.1   reinoud 
     86  1.1   reinoud 
     87  1.1   reinoud #ifndef MOUNT_NOMAIN
     88  1.1   reinoud int
     89  1.1   reinoud main(int argc, char **argv)
     90  1.1   reinoud {
     91  1.1   reinoud 
     92  1.1   reinoud 	setprogname(argv[0]);
     93  1.1   reinoud 	return mount_nilfs(argc, argv);
     94  1.1   reinoud }
     95  1.1   reinoud #endif
     96  1.1   reinoud 
     97  1.1   reinoud 
     98  1.1   reinoud /* main routine */
     99  1.1   reinoud void
    100  1.1   reinoud mount_nilfs_parseargs(int argc, char **argv,
    101  1.1   reinoud 	struct nilfs_args *args, int *mntflags,
    102  1.1   reinoud 	char *canon_dev, char *canon_dir)
    103  1.1   reinoud {
    104  1.1   reinoud 	struct tm *tm;
    105  1.1   reinoud 	time_t	 now;
    106  1.1   reinoud 	int	 ch, set_gmtoff;
    107  1.1   reinoud 	mntoptparse_t mp;
    108  1.1   reinoud 
    109  1.1   reinoud 	/* initialise */
    110  1.1   reinoud 	(void)memset(args, 0, sizeof(*args));
    111  1.1   reinoud 
    112  1.1   reinoud 	set_gmtoff = *mntflags = 0;
    113  1.1   reinoud 
    114  1.1   reinoud 	while ((ch = getopt(argc, argv, "c:o:t:")) != -1) {
    115  1.1   reinoud 		switch (ch) {
    116  1.1   reinoud 		case 'o' :
    117  1.1   reinoud 			/* process generic mount options */
    118  1.1   reinoud 			mp = getmntopts(optarg, mopts, mntflags, 0);
    119  1.1   reinoud 			if (mp == NULL)
    120  1.1   reinoud 				err(EXIT_FAILURE, "getmntopts");
    121  1.1   reinoud 			freemntopts(mp);
    122  1.1   reinoud 			break;
    123  1.1   reinoud 		case 'c' :
    124  1.1   reinoud 			args->cpno = a_num(optarg, "checkpoint number");
    125  1.1   reinoud 			break;
    126  1.1   reinoud 		case 't' :
    127  1.1   reinoud 			args->gmtoff = a_num(optarg, "gmtoff");
    128  1.1   reinoud 			set_gmtoff  = 1;
    129  1.1   reinoud 			break;
    130  1.1   reinoud 		default  :
    131  1.1   reinoud 			usage();
    132  1.1   reinoud 			/* NOTREACHED */
    133  1.1   reinoud 		}
    134  1.1   reinoud 	}
    135  1.1   reinoud 
    136  1.1   reinoud 	if (optind + 2 != argc)
    137  1.1   reinoud 		usage();
    138  1.1   reinoud 
    139  1.1   reinoud 	if (!set_gmtoff) {
    140  1.1   reinoud 		/* use user's time zone as default */
    141  1.1   reinoud 		(void)time(&now);
    142  1.1   reinoud 		tm = localtime(&now);
    143  1.1   reinoud 		args->gmtoff = tm->tm_gmtoff;
    144  1.1   reinoud 	}
    145  1.1   reinoud 
    146  1.1   reinoud 	/* get device and directory specifier */
    147  1.1   reinoud 	pathadj(argv[optind], canon_dev);
    148  1.1   reinoud 	pathadj(argv[optind+1], canon_dir);
    149  1.1   reinoud 
    150  1.1   reinoud 	args->version = NILFSMNT_VERSION;
    151  1.1   reinoud 	args->fspec = canon_dev;
    152  1.1   reinoud }
    153  1.1   reinoud 
    154  1.1   reinoud int
    155  1.1   reinoud mount_nilfs(int argc, char *argv[])
    156  1.1   reinoud {
    157  1.1   reinoud 	struct nilfs_args args;
    158  1.1   reinoud 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    159  1.1   reinoud 	int mntflags;
    160  1.1   reinoud 
    161  1.1   reinoud 	mount_nilfs_parseargs(argc, argv, &args, &mntflags, canon_dev, canon_dir);
    162  1.1   reinoud 
    163  1.1   reinoud 	/* mount it! :) */
    164  1.1   reinoud 	if (mount(MOUNT_NILFS, canon_dir, mntflags, &args, sizeof args) == -1)
    165  1.1   reinoud 		err(EXIT_FAILURE, "Cannot mount %s on %s", canon_dev,canon_dir);
    166  1.1   reinoud 
    167  1.1   reinoud 	if (mntflags & MNT_GETARGS) {
    168  1.1   reinoud 		char buf[1024];
    169  1.1   reinoud 
    170  1.1   reinoud 		(void)snprintb(buf, sizeof(buf), NILFSMNT_BITS,
    171  1.1   reinoud 		    (uint64_t)args.nilfsmflags);
    172  1.1   reinoud 		(void)printf("gmtoffset = %d, cpno = %"PRIu64", flags = %s\n",
    173  1.1   reinoud 		    args.gmtoff, args.cpno, buf);
    174  1.1   reinoud 	}
    175  1.1   reinoud 
    176  1.1   reinoud 	return EXIT_SUCCESS;
    177  1.1   reinoud }
    178