1
/*-------------------------------------------------------------------------
3
* Shared Disk File EXclusiveness Control Program(SF-EX)
5
* sfex_init.c --- Initialize SF-EX meta-data.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
* Copyright (c) 2007 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
26
*-------------------------------------------------------------------------
28
* sfex_init [-b <blocksize>] [-n <numlocks>] <device>
30
* -b <blocksize> --- The size of the block is specified by the number of
31
* bytes. In general, to prevent a partial writing to the disk, the size
32
* of block is set to 512 bytes etc.
33
* Note a set value because this value is used also for the alignment
34
* adjustment in the input-output buffer in the program when direct I/O
35
* is used(When you specify --enable-directio option for configure script).
36
* (In Linux kernel 2.6, "direct I/O " does not work if this value is not
37
* a multiple of 512.) Default is 512 bytes.
39
* -n <numlocks> --- The number of storing lock data is specified by integer
40
* of one or more. When you want to control two or more resources by one
41
* meta-data, you set the value of two or more to numlocks. A necessary disk
42
* area for meta data are (blocksize*(1+numlocks))bytes. Default is 1.
44
* <device> --- This is file path which stored meta-data. It is usually
45
* expressed in "/dev/...", because it is partition on the shared disk.
47
* exit code --- 0 - Normal end. 3 - Error occurs while processing it.
48
* The content of the error is displayed into stderr. 4 - The mistake is
49
* found in the command line parameter.
51
*-------------------------------------------------------------------------*/
55
#include <sys/types.h>
69
* usage --- display command line syntax
71
* display command line syntax. By the purpose, it can specify destination
72
* stream. stdout or stderr is set usually.
74
* dist --- destination stream of the command line syntax, such as stderr.
76
* return value --- void
78
static void usage(FILE *dist) {
79
fprintf(dist, "usage: %s [-n <numlocks>] <device>\n", progname);
83
* main --- main function
85
* entry point of sfex_init command.
87
* exit code --- 0 - Normal end. 3 - Error occurs while processing it.
88
* The content of the error is displayed into stderr. 4 - The mistake is
89
* found in the command line parameter.
92
main(int argc, char *argv[]) {
93
sfex_controldata cdata;
96
/* command line parameter */
97
int numlocks = 1; /* default 1 locks */
105
openlog("SFex Init", LOG_PID|LOG_CONS, LOG_USER);
108
/* get a program name */
109
progname = get_progname(argv[0]);
111
/* read command line option */
114
int c = getopt(argc, argv, "hn:");
121
case 'n': /* -n <numlocks> */
123
unsigned long l = strtoul(optarg, NULL, 10);
124
if (l < SFEX_MIN_NUMLOCKS || l > SFEX_MAX_NUMLOCKS) {
126
"%s: ERROR: numlocks %s is out of range or invalid. it must be integer value between %lu and %lu.\n",
128
(unsigned long)SFEX_MIN_NUMLOCKS,
129
(unsigned long)SFEX_MAX_NUMLOCKS);
135
case '?': /* error */
141
/* check parameter except the option */
142
if (optind >= argc) {
143
fprintf(stderr, "%s: ERROR: no device specified.\n", progname);
146
} else if (optind + 1 < argc) {
147
fprintf(stderr, "%s: ERROR: too many arguments.\n", progname);
151
device = argv[optind];
153
prepare_lock(device);
155
/* main processes start */
157
/* get a node name */
158
nodename = get_nodename();
160
/* create and control data and lock data */
161
init_controldata(&cdata, sector_size, numlocks);
162
init_lockdata(&ldata);
164
/* write out control data and lock data */
165
write_controldata(&cdata);
168
for (index = 1; index <= numlocks; index++)
169
write_lockdata(&cdata, &ldata, index);