summaryrefslogtreecommitdiffstats
path: root/tools/sfex_lib.c
blob: 2d01602fb29a8f4638ef74c41e58dfbdf9758b77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*-------------------------------------------------------------------------
 * 
 * Shared Disk File EXclusiveness Control Program(SF-EX)
 *
 * sfex_lib.c --- Libraries for other SF-EX modules.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301, USA.
 *
 * Copyright (c) 2007 NIPPON TELEGRAPH AND TELEPHONE CORPORATION
 *
 * $Id$
 *
 *-------------------------------------------------------------------------*/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <sys/ioctl.h>
#include <syslog.h>
#include <linux/fs.h>

#include "sfex.h"
#include "sfex_lib.h"

static void *locked_mem;
static int dev_fd;
unsigned long sector_size = 0;

int
prepare_lock (const char *device)
{
  int sec_tmp = 0;

  do {
    dev_fd = open (device, O_RDWR | O_DIRECT | O_SYNC);
    if (dev_fd == -1) {
      if (errno == EINTR || errno == EAGAIN)
	continue;
      cl_log(LOG_ERR, "can't open device %s: %s\n",
		    device, strerror (errno));
      exit (3);
    }
    break;
  }
  while (1);

  ioctl(dev_fd, BLKSSZGET, &sec_tmp);
  sector_size = (unsigned long)sec_tmp;
  if (sector_size == 0) {
	  cl_log(LOG_ERR, "Get sector size failed: %s\n", strerror(errno));
	  exit(EXIT_FAILURE);
  }

  if (posix_memalign
      ((void **) (&locked_mem), SFEX_ODIRECT_ALIGNMENT,
       sector_size) != 0) {
    cl_log(LOG_ERR, "Failed to allocate aligned memory\n");
    exit (3);
  }
  memset (locked_mem, 0, sector_size);

  return 0;
}

/*
 * get_progname --- a program name
 *
 * We get program name from directory path. It does not include delimiter 
 * characters. Return value is pointer that point string of program name. 
 * We assume delimiter is '/'.
 */
const char *
get_progname (const char *argv0)
{
  char *p;

  p = strrchr (argv0, '/');
  if (p)
    return p + 1;
  else
    return argv0;
}

/*
 * get_nodename --- get a node name(hostname)
 *
 * We get a node name by using uname(2) and return pointer of it.
 * The error checks are done in this function. The caller does not have 
 * to check return value.
 */
char *
get_nodename (void)
{
  struct utsname u;
  char *n;

  if (uname (&u)) {
    cl_log(LOG_ERR, "%s\n", strerror (errno));
    exit (3);
  }
  if (strlen (u.nodename) > SFEX_MAX_NODENAME) {
    cl_log(LOG_ERR,
      "nodename %s is too long. must be less than %lu byte.\n",
       u.nodename, (unsigned long)SFEX_MAX_NODENAME);
    exit (3);
  }
  n = strdup (&u.nodename[0]);
  if (!n) {
    cl_log(LOG_ERR, "%s\n", strerror (errno));
    exit (3);
  }
  return n;
}

/*
 * init_controldata --- initialize control data
 *
 * We initialize each member of sfex_controldata structure.
 */
void
init_controldata (sfex_controldata * cdata, size_t blocksize, int numlocks)
{
  memcpy (cdata->magic, SFEX_MAGIC, sizeof (cdata->magic));
  cdata->version = SFEX_VERSION;
  cdata->revision = SFEX_REVISION;
  cdata->blocksize = blocksize;
  cdata->numlocks = numlocks;
}

/*
 * init_lockdata --- initialize lock data
 *
 * We initialize each member of sfex_lockdata structure.
 */
void
init_lockdata (sfex_lockdata * ldata)
{
  ldata->status = SFEX_STATUS_UNLOCK;
  ldata->count = 0;
  ldata->nodename[0] = 0;
}

/*
 * write_controldata --- write control data into file
 *
 * We write sfex_controldata struct into file. We open a file with 
 * synchronization mode and write out control data.
 *
 * cdata --- pointer of control data
 *
 * device --- name of target file
 */
void
write_controldata (const sfex_controldata * cdata)
{
  sfex_controldata_ondisk *block;
  int fd;

  block = (sfex_controldata_ondisk *) (locked_mem);

  /* We write control data into the buffer with given format. */
  /* We write the offset value of each field of the control data directly.
   * Because a point using this value is limited to two places, we do not 
   * use macro. If you change the following offset values, you must change 
   * values in the read_controldata() function.
   */
  memset (block, 0, cdata->blocksize);
  memcpy (block->magic, cdata->magic, sizeof (block->magic));
  snprintf ((char *) (block->version), sizeof (block->version), "%d",
	    cdata->version);
  snprintf ((char *) (block->revision), sizeof (block->revision), "%d",
	    cdata->revision);
  snprintf ((char *) (block->blocksize), sizeof (block->blocksize), "%u",
	    (unsigned)cdata->blocksize);
  snprintf ((char *) (block->numlocks), sizeof (block->numlocks), "%d",
	    cdata->numlocks);

  fd = dev_fd;
  if (lseek (fd, 0, SEEK_SET) == -1) {
    cl_log(LOG_ERR, "can't seek file pointer: %s\n",
		  strerror (errno));
    exit (3);
  }

  /* write buffer into a file  */
  do {
	  ssize_t s = write (fd, block, cdata->blocksize);
	  if (s == -1) {
		  if (errno == EINTR || errno == EAGAIN)
			  continue;
		  cl_log(LOG_ERR, "can't write meta-data: %s\n",
				strerror (errno));
		  exit (3);
	  }
	  else
		  break;
  }
  while (1);
}

/*
 * write_lockdata --- write lock data into file
 *
 * We write sfex_lockdata into file and seek file pointer to the given
 * position of lock data.
 *
 * cdata --- pointer for control data
 *
 * ldata --- pointer for lock data
 *
 * device --- file name for write
 *
 * index --- index number for lock data. 1 origine.
 */
int
write_lockdata (const sfex_controldata * cdata, const sfex_lockdata * ldata,
		int index)
{
  sfex_lockdata_ondisk *block;
  int fd;

  block = (sfex_lockdata_ondisk *) locked_mem;
  /* We write lock data into buffer with given format */
  /* We write the offset value of each field of the control data directly.
   * Because a point using this value is limited to two places, we do not 
   * use macro. If you chage the following offset values, you must change 
   * values in the read_lockdata() function.
   */
  memset (block, 0, cdata->blocksize);
  block->status = ldata->status;
  snprintf ((char *) (block->count), sizeof (block->count), "%d",
	    ldata->count);
  snprintf ((char *) (block->nodename), sizeof (block->nodename), "%s",
	    ldata->nodename);

  fd = dev_fd;

  /* seek a file pointer to given position */
  if (lseek (fd, cdata->blocksize * index, SEEK_SET) == -1) {
    cl_log(LOG_ERR, "can't seek file pointer: %s\n",
		  strerror (errno));
    return -1;
  }

  /* write buffer into file */
  do {
    ssize_t s = write (fd, block, cdata->blocksize);
    if (s == -1) {
      if (errno == EINTR || errno == EAGAIN)
	continue;
      cl_log(LOG_ERR, "can't write meta-data: %s\n",
		    strerror (errno));
      return -1;
    }
    else if (s != cdata->blocksize) {
      /* if writing atomically failed, this process is error */
      cl_log(LOG_ERR, "can't write meta-data atomically.\n");
      return -1;
    }
    break;
  }
  while (1);
  return 0;
}

/*
 * read_controldata --- read control data from file
 *
 * read sfex_controldata structure from file.
 *
 * cdata --- pointer for control data
 *
 * device --- file name for reading
 */
int
read_controldata (sfex_controldata * cdata)
{
  sfex_controldata_ondisk *block;

  block = (sfex_controldata_ondisk *) (locked_mem);

  if (lseek (dev_fd, 0, SEEK_SET) == -1) {
    cl_log(LOG_ERR, "can't seek file pointer: %s\n",
		  strerror (errno));
    return -1;
  }

  /* read data from file */
  do {
	  ssize_t s = read (dev_fd, block, sector_size);
	  if (s == -1) {
		  if (errno == EINTR || errno == EAGAIN)
			  continue;
		  cl_log(LOG_ERR,
			  "can't read controldata meta-data: %s\n",
			   strerror (errno));
		  return -1;
	  }
	  else
		  break;
  } while (1);

  /* read control data from buffer */
  /* 1. check the magic number.  2. check null terminator of each field 
     3. check the version number.  4. Unmuch of revision number is allowed  */
  /* We write the offset value of each field of the control data directly.
   * Because a point using this value is limited to two places, we do not 
   * use macro. If you chage the following offset values, you must change 
   * values in the write_controldata() function.
   */
  memcpy (cdata->magic, block->magic, 4);
  if (memcmp (cdata->magic, SFEX_MAGIC, sizeof (cdata->magic))) {
    cl_log(LOG_ERR, "magic number mismatched. %c%c%c%c <-> %s\n", block->magic[0], block->magic[1], block->magic[2], block->magic[3], SFEX_MAGIC);
    return -1;
  }
  if (block->version[sizeof (block->version)-1]
      || block->revision[sizeof (block->revision)-1]
      || block->blocksize[sizeof (block->blocksize)-1]
      || block->numlocks[sizeof (block->numlocks)-1]) {
    cl_log(LOG_ERR, "control data format error.\n");
    return -1;
  }
  cdata->version = atoi ((char *) (block->version));
  if (cdata->version != SFEX_VERSION) {
    cl_log(LOG_ERR,
      "version number mismatched. program is %d, data is %d.\n",
       SFEX_VERSION, cdata->version);
    return -1;
  }
  cdata->revision = atoi ((char *) (block->revision));
  cdata->blocksize = atoi ((char *) (block->blocksize));
  cdata->numlocks = atoi ((char *) (block->numlocks));

  return 0;
}

/*
 * read_lockdata --- read lock data from file
 *
 * read sfex_lockdata from file and seek file pointer to head position of the 
 * file.
 *
 * cdata --- pointer for control data
 *
 * ldata --- pointer for lock data. Read lock data are stored into this 
 * pointed area.
 *
 * device --- file name of source file
 *
 * index --- index number. 1 origin.
 */
int
read_lockdata (const sfex_controldata * cdata, sfex_lockdata * ldata,
	       int index)
{
  sfex_lockdata_ondisk *block;
  int fd;

  block = (sfex_lockdata_ondisk *) (locked_mem);

  fd = dev_fd;

  /* seek a file pointer to given position */
  if (lseek (fd, cdata->blocksize * index, SEEK_SET) == -1) {
    cl_log(LOG_ERR, "can't seek file pointer: %s\n",
		  strerror (errno));
    return -1;
  }

  /* read from file */
  do {
    ssize_t s = read (fd, block, cdata->blocksize);
    if (s == -1) {
      if (errno == EINTR || errno == EAGAIN)
	continue;
      cl_log(LOG_ERR, "can't read lockdata meta-data: %s\n",
		    strerror (errno));
      return -1;
    }
    else if (s != cdata->blocksize) {
      cl_log(LOG_ERR, "can't read meta-data atomically.\n");
      return -1;
    }
    break;
  }
  while (1);

  /* read control data form buffer */
  /* 1. check null terminator of each field 2. check the status */
  /* We write the offset value of each field of the control data directly.
   * Because a point using this value is limited to two places, we do not 
   * use macro. If you chage the following offset values, you must change 
   * values in the write_lockdata() function.
   */
  if (block->count[sizeof(block->count)-1] || block->nodename[sizeof(block->nodename)-1]) {
    cl_log(LOG_ERR, "lock data format error.\n");
    return -1;
  }
  ldata->status = block->status;
  if (ldata->status != SFEX_STATUS_UNLOCK
      && ldata->status != SFEX_STATUS_LOCK) {
    cl_log(LOG_ERR, "lock data format error.\n");
    return -1;
  }
  ldata->count = atoi ((char *) (block->count));
  strncpy ((char *) (ldata->nodename), (const char *) (block->nodename), sizeof(ldata->nodename));

#ifdef SFEX_DEBUG
  cl_log(LOG_INFO, "status: %c\n", ldata->status);
  cl_log(LOG_INFO, "count: %d\n", ldata->count);
  cl_log(LOG_INFO, "nodename: %s\n", ldata->nodename);
#endif
  return 0;
}

/*
 * lock_index_check --- check the value of index
 *
 * The lock_index_check function checks whether the value of index exceeds
 * the number of lock data on the shared disk.
 *
 * cdata --- pointer for control data
 *
 * index --- index number
 */
int
lock_index_check(sfex_controldata * cdata, int index)
{
        if (read_controldata(cdata) == -1) {
                cl_log(LOG_ERR, "%s\n", "read_controldata failed in lock_index_check");
                return -1;
        }
#ifdef SFEX_DEBUG
        cl_log(LOG_INFO, "version: %d\n", cdata->version);
        cl_log(LOG_INFO, "revision: %d\n", cdata->revision);
        cl_log(LOG_INFO, "blocksize: %d\n", cdata->blocksize);
        cl_log(LOG_INFO, "numlocks: %d\n", cdata->numlocks);
#endif

        if (index > cdata->numlocks) {
                cl_log(LOG_ERR, "index %d is too large. %d locks are stored.\n",
                                index, cdata->numlocks);
                return -1;
        }

        if (cdata->blocksize != sector_size) {
                cl_log(LOG_ERR, "sector_size is not the same as the blocksize.\n");
                return -1;
        }
        return 0;
}