summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/globals.c
blob: 459291b3309e497756c0892748d31f601961363c (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
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif /* !_CONFIG_H */

#include <pthread.h>

#include "glusterfs.h"
#include "globals.h"
#include "xlator.h"
#include "mem-pool.h"

const char *gf_fop_list[GF_FOP_MAXVALUE] = {
        [GF_FOP_NULL]        = "NULL",
        [GF_FOP_STAT]        = "STAT",
        [GF_FOP_READLINK]    = "READLINK",
        [GF_FOP_MKNOD]       = "MKNOD",
        [GF_FOP_MKDIR]       = "MKDIR",
        [GF_FOP_UNLINK]      = "UNLINK",
        [GF_FOP_RMDIR]       = "RMDIR",
        [GF_FOP_SYMLINK]     = "SYMLINK",
        [GF_FOP_RENAME]      = "RENAME",
        [GF_FOP_LINK]        = "LINK",
        [GF_FOP_TRUNCATE]    = "TRUNCATE",
        [GF_FOP_OPEN]        = "OPEN",
        [GF_FOP_READ]        = "READ",
        [GF_FOP_WRITE]       = "WRITE",
        [GF_FOP_STATFS]      = "STATFS",
        [GF_FOP_FLUSH]       = "FLUSH",
        [GF_FOP_FSYNC]       = "FSYNC",
        [GF_FOP_SETXATTR]    = "SETXATTR",
        [GF_FOP_GETXATTR]    = "GETXATTR",
        [GF_FOP_REMOVEXATTR] = "REMOVEXATTR",
        [GF_FOP_OPENDIR]     = "OPENDIR",
        [GF_FOP_FSYNCDIR]    = "FSYNCDIR",
        [GF_FOP_ACCESS]      = "ACCESS",
        [GF_FOP_CREATE]      = "CREATE",
        [GF_FOP_FTRUNCATE]   = "FTRUNCATE",
        [GF_FOP_FSTAT]       = "FSTAT",
        [GF_FOP_LK]          = "LK",
        [GF_FOP_LOOKUP]      = "LOOKUP",
        [GF_FOP_READDIR]     = "READDIR",
        [GF_FOP_INODELK]     = "INODELK",
        [GF_FOP_FINODELK]    = "FINODELK",
        [GF_FOP_ENTRYLK]     = "ENTRYLK",
        [GF_FOP_FENTRYLK]    = "FENTRYLK",
        [GF_FOP_XATTROP]     = "XATTROP",
        [GF_FOP_FXATTROP]    = "FXATTROP",
        [GF_FOP_FSETXATTR]   = "FSETXATTR",
        [GF_FOP_FGETXATTR]   = "FGETXATTR",
        [GF_FOP_RCHECKSUM]   = "RCHECKSUM",
        [GF_FOP_SETATTR]     = "SETATTR",
        [GF_FOP_FSETATTR]    = "FSETATTR",
        [GF_FOP_READDIRP]    = "READDIRP",
        [GF_FOP_GETSPEC]     = "GETSPEC",
        [GF_FOP_FORGET]      = "FORGET",
        [GF_FOP_RELEASE]     = "RELEASE",
        [GF_FOP_RELEASEDIR]  = "RELEASEDIR",
        [GF_FOP_FREMOVEXATTR]= "FREMOVEXATTR",
	[GF_FOP_FALLOCATE]   = "FALLOCATE",
	[GF_FOP_DISCARD]     = "DISCARD",
};
/* THIS */

xlator_t global_xlator;
static pthread_key_t this_xlator_key;
static pthread_key_t synctask_key;
static pthread_key_t uuid_buf_key;
static char          global_uuid_buf[GF_UUID_BUF_SIZE];
static pthread_key_t lkowner_buf_key;
static char          global_lkowner_buf[GF_LKOWNER_BUF_SIZE];


void
glusterfs_this_destroy (void *ptr)
{
        FREE (ptr);
}


int
glusterfs_this_init ()
{
        int  ret = 0;

        ret = pthread_key_create (&this_xlator_key, glusterfs_this_destroy);
        if (ret != 0) {
                gf_log ("", GF_LOG_WARNING, "failed to create the pthread key");
                return ret;
        }

        global_xlator.name = "glusterfs";
        global_xlator.type = "global";

        INIT_LIST_HEAD (&global_xlator.volume_options);

        return ret;
}


xlator_t **
__glusterfs_this_location ()
{
        xlator_t **this_location = NULL;
        int        ret = 0;

        this_location = pthread_getspecific (this_xlator_key);

        if (!this_location) {
                this_location = CALLOC (1, sizeof (*this_location));
                if (!this_location)
                        goto out;

                ret = pthread_setspecific (this_xlator_key, this_location);
                if (ret != 0) {
                        FREE (this_location);
                        this_location = NULL;
                        goto out;
                }
        }
out:
        if (this_location) {
                if (!*this_location)
                        *this_location = &global_xlator;
        }
        return this_location;
}


xlator_t *
glusterfs_this_get ()
{
        xlator_t **this_location = NULL;

        this_location = __glusterfs_this_location ();
        if (!this_location)
                return &global_xlator;

        return *this_location;
}


int
glusterfs_this_set (xlator_t *this)
{
        xlator_t **this_location = NULL;

        this_location = __glusterfs_this_location ();
        if (!this_location)
                return -ENOMEM;

        *this_location = this;

        return 0;
}

/* SYNCTASK */

int
synctask_init ()
{
        int  ret = 0;

        ret = pthread_key_create (&synctask_key, NULL);

        return ret;
}


void *
synctask_get ()
{
        void   *synctask = NULL;

        synctask = pthread_getspecific (synctask_key);

        return synctask;
}


int
synctask_set (void *synctask)
{
        int     ret = 0;

        pthread_setspecific (synctask_key, synctask);

        return ret;
}

//UUID_BUFFER

void
glusterfs_uuid_buf_destroy (void *ptr)
{
        FREE (ptr);
}

int
glusterfs_uuid_buf_init ()
{
        int ret = 0;

        ret = pthread_key_create (&uuid_buf_key,
                                  glusterfs_uuid_buf_destroy);
        return ret;
}

char *
glusterfs_uuid_buf_get ()
{
        char *buf;
        int ret = 0;

        buf = pthread_getspecific (uuid_buf_key);
        if(!buf) {
                buf = MALLOC (GF_UUID_BUF_SIZE);
                ret = pthread_setspecific (uuid_buf_key, (void *) buf);
                if (ret)
                        buf = global_uuid_buf;
        }
        return buf;
}

/* LKOWNER_BUFFER */

void
glusterfs_lkowner_buf_destroy (void *ptr)
{
        FREE (ptr);
}

int
glusterfs_lkowner_buf_init ()
{
        int ret = 0;

        ret = pthread_key_create (&lkowner_buf_key,
                                  glusterfs_lkowner_buf_destroy);
        return ret;
}

char *
glusterfs_lkowner_buf_get ()
{
        char *buf;
        int ret = 0;

        buf = pthread_getspecific (lkowner_buf_key);
        if(!buf) {
                buf = MALLOC (GF_LKOWNER_BUF_SIZE);
                ret = pthread_setspecific (lkowner_buf_key, (void *) buf);
                if (ret)
                        buf = global_lkowner_buf;
        }
        return buf;
}

int
glusterfs_globals_init (glusterfs_ctx_t *ctx)
{
        int ret = 0;

        gf_log_globals_init (ctx);

        ret = glusterfs_this_init ();
        if (ret) {
                gf_log ("", GF_LOG_CRITICAL,
                        "ERROR: glusterfs-translator init failed");
                goto out;
        }

        ret = glusterfs_uuid_buf_init ();
        if(ret) {
                gf_log ("", GF_LOG_CRITICAL,
                        "ERROR: glusterfs uuid buffer init failed");
                goto out;
        }

        ret = glusterfs_lkowner_buf_init ();
        if(ret) {
                gf_log ("", GF_LOG_CRITICAL,
                        "ERROR: glusterfs lkowner buffer init failed");
                goto out;
        }

        ret = synctask_init ();
        if (ret) {
                gf_log ("", GF_LOG_CRITICAL,
                        "ERROR: glusterfs synctask init failed");
                goto out;
        }
out:
        return ret;
}