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