~wb-munzinger/+junk/sanlock

« back to all changes in this revision

Viewing changes to direct_lib.c

  • Committer: David Weber
  • Date: 2012-01-18 13:00:36 UTC
  • Revision ID: wb@munzinger.de-20120118130036-9a7wvhhmfuip7zx5
Tags: upstream-1.9
Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010-2011 Red Hat, Inc.
 
3
 *
 
4
 * This copyrighted material is made available to anyone wishing to use,
 
5
 * modify, copy, or redistribute it subject to the terms and conditions
 
6
 * of the GNU General Public License v2 or (at your option) any later version.
 
7
 */
 
8
 
 
9
#include <inttypes.h>
 
10
#include <unistd.h>
 
11
#include <stdio.h>
 
12
#include <stdlib.h>
 
13
#include <stdint.h>
 
14
#include <stddef.h>
 
15
#include <errno.h>
 
16
 
 
17
#define EXTERN
 
18
#include "sanlock_internal.h"
 
19
#include "sanlock_direct.h"
 
20
#include "diskio.h"
 
21
#include "direct.h"
 
22
#include "task.h"
 
23
 
 
24
void log_level(uint32_t space_id GNUC_UNUSED, uint32_t token_id GNUC_UNUSED,
 
25
               char *name GNUC_UNUSED,
 
26
               int level GNUC_UNUSED, const char *fmt GNUC_UNUSED, ...);
 
27
 
 
28
void log_level(uint32_t space_id GNUC_UNUSED, uint32_t token_id GNUC_UNUSED,
 
29
               char *name GNUC_UNUSED,
 
30
               int level GNUC_UNUSED, const char *fmt GNUC_UNUSED, ...)
 
31
{
 
32
}
 
33
 
 
34
int lockspace_disk(char *space_name GNUC_UNUSED, struct sync_disk *disk GNUC_UNUSED);
 
35
 
 
36
int lockspace_disk(char *space_name GNUC_UNUSED, struct sync_disk *disk GNUC_UNUSED)
 
37
{
 
38
        return -1;
 
39
}
 
40
 
 
41
int host_info(char *space_name, uint64_t host_id, struct host_status *hs_out);
 
42
 
 
43
int host_info(char *space_name GNUC_UNUSED, uint64_t host_id GNUC_UNUSED, struct host_status *hs_out GNUC_UNUSED)
 
44
{
 
45
        return -1;
 
46
}
 
47
 
 
48
/* copied from host_id.c */
 
49
 
 
50
int test_id_bit(int host_id, char *bitmap);
 
51
 
 
52
int test_id_bit(int host_id, char *bitmap)
 
53
{
 
54
        char *byte = bitmap + ((host_id - 1) / 8);
 
55
        unsigned int bit = (host_id - 1) % 8;
 
56
        char mask;
 
57
 
 
58
        mask = 1 << bit;
 
59
 
 
60
        return (*byte & mask);
 
61
}
 
62
 
 
63
int get_rand(int a, int b);
 
64
 
 
65
int get_rand(int a, int b)
 
66
{
 
67
        return a + (int) (((float)(b - a + 1)) * random() / (RAND_MAX+1.0));
 
68
}
 
69
 
 
70
static void setup_task_lib(struct task *task, int use_aio, int io_timeout_sec)
 
71
{
 
72
        memset(task, 0, sizeof(struct task));
 
73
        if (!io_timeout_sec)
 
74
                io_timeout_sec = DEFAULT_IO_TIMEOUT;
 
75
        setup_task_timeouts(task, io_timeout_sec);
 
76
        setup_task_aio(task, use_aio, LIB_AIO_CB_SIZE);
 
77
        sprintf(task->name, "%s", "lib");
 
78
}
 
79
 
 
80
 
 
81
int sanlock_direct_read_id(struct sanlk_lockspace *ls,
 
82
                           uint64_t *timestamp,
 
83
                           uint64_t *owner_id,
 
84
                           uint64_t *owner_generation,
 
85
                           int use_aio,
 
86
                           int io_timeout_sec)
 
87
{
 
88
        struct task task;
 
89
        int rv;
 
90
 
 
91
        setup_task_lib(&task, use_aio, io_timeout_sec);
 
92
 
 
93
        rv = direct_read_id(&task, ls, timestamp, owner_id, owner_generation);
 
94
 
 
95
        close_task_aio(&task);
 
96
 
 
97
        return rv;
 
98
}
 
99
 
 
100
int sanlock_direct_live_id(struct sanlk_lockspace *ls,
 
101
                           uint64_t *timestamp,
 
102
                           uint64_t *owner_id,
 
103
                           uint64_t *owner_generation,
 
104
                           int *live,
 
105
                           int use_aio,
 
106
                           int io_timeout_sec)
 
107
{
 
108
        struct task task;
 
109
        int rv;
 
110
 
 
111
        setup_task_lib(&task, use_aio, io_timeout_sec);
 
112
 
 
113
        rv = direct_live_id(&task, ls, timestamp, owner_id, owner_generation, live);
 
114
 
 
115
        close_task_aio(&task);
 
116
 
 
117
        return rv;
 
118
}
 
119
 
 
120
int sanlock_direct_init(struct sanlk_lockspace *ls,
 
121
                        struct sanlk_resource *res,
 
122
                        int max_hosts, int num_hosts, int use_aio)
 
123
{
 
124
        struct task task;
 
125
        int rv;
 
126
 
 
127
        setup_task_lib(&task, use_aio, DEFAULT_IO_TIMEOUT);
 
128
 
 
129
        rv = direct_init(&task, ls, res, max_hosts, num_hosts);
 
130
 
 
131
        close_task_aio(&task);
 
132
 
 
133
        return rv;
 
134
}
 
135
 
 
136
int sanlock_direct_align(struct sanlk_disk *disk_in)
 
137
{
 
138
        struct sync_disk disk;
 
139
        int align_size, rv;
 
140
 
 
141
        memset(&disk, 0, sizeof(disk));
 
142
 
 
143
        memcpy(disk.path, disk_in->path, SANLK_PATH_LEN);
 
144
 
 
145
        rv = open_disk(&disk);
 
146
        if (rv < 0)
 
147
                return rv;
 
148
 
 
149
        align_size = direct_align(&disk);
 
150
 
 
151
        close(disk.fd);
 
152
 
 
153
        return align_size;
 
154
}
 
155