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