File: dlm.c

package info (click to toggle)
ocfs2-tools 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,232 kB
  • sloc: ansic: 86,865; sh: 5,781; python: 2,380; makefile: 1,305
file content (473 lines) | stat: -rw-r--r-- 10,718 bytes parent folder | download
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
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * dlm.c
 *
 * Interface the OCFS2 userspace library to the userspace DLM library
 *
 * Copyright (C) 2005 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License, version 2,  as published by the Free Software Foundation.
 *
 * 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 St, Fifth Floor,
 * Boston, MA  02110-1301 USA.
 *
 */

#define _XOPEN_SOURCE 600 /* Triggers magic in features.h */
#define _LARGEFILE64_SOURCE

#include "ocfs2/ocfs2.h"

#define DEFAULT_DLMFS_PATH	"/dlm/"

static errcode_t ocfs2_get_journal_blkno(ocfs2_filesys *fs, uint64_t *jrnl_blkno)
{
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
	char sysfile[OCFS2_MAX_FILENAME_LEN];
	int i;
	errcode_t ret = 0;

	for (i = 0; i < sb->s_max_slots; ++i) {
		snprintf (sysfile, sizeof(sysfile),
			  ocfs2_system_inodes[JOURNAL_SYSTEM_INODE].si_name, i);
		ret = ocfs2_lookup(fs, fs->fs_sysdir_blkno, sysfile,
				   strlen(sysfile), NULL, &(jrnl_blkno[i]));
		if (ret)
			goto bail;
	}

bail:
	return ret;
}

errcode_t ocfs2_lock_down_cluster(ocfs2_filesys *fs)
{
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
	uint64_t jrnl_blkno[OCFS2_MAX_SLOTS];
	ocfs2_cached_inode *ci;
	errcode_t ret = 0;
	int i;

	ret = ocfs2_get_journal_blkno(fs, jrnl_blkno);
	if (ret)
		goto bail;

	ret = ocfs2_super_lock(fs);
	if (ret)
		goto bail;

	for (i = 0; i < sb->s_max_slots; ++i) {
		ret = ocfs2_read_cached_inode(fs, jrnl_blkno[i], &ci);
		if (ret) {
			ocfs2_super_unlock(fs);
			goto bail;
		}

		ret = ocfs2_meta_lock(fs, ci, O2DLM_LEVEL_EXMODE, O2DLM_TRYLOCK);
		if (ret) {
			ocfs2_super_unlock(fs);
			ocfs2_free_cached_inode(fs, ci);
			goto bail;
		}

		ocfs2_meta_unlock(fs, ci);

		ocfs2_free_cached_inode(fs, ci);
	}

bail:
	return ret;
}

errcode_t ocfs2_release_cluster(ocfs2_filesys *fs)
{
	errcode_t ret = 0;

	ret = ocfs2_super_unlock(fs);
	if (ret)
		goto bail;

bail:
	return ret;
}

errcode_t ocfs2_fill_cluster_desc(ocfs2_filesys *fs,
				  struct o2cb_cluster_desc *desc)
{
	errcode_t ret = 0;
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);

	if (!ocfs2_clusterinfo_valid(sb)) {
		desc->c_stack = NULL;
		desc->c_cluster = NULL;
		desc->c_flags = 0;
		return 0;
	}

	ret = ocfs2_malloc0(OCFS2_STACK_LABEL_LEN + 1, &desc->c_stack);
	if (ret)
		return ret;

	ret = ocfs2_malloc0(OCFS2_CLUSTER_NAME_LEN + 1, &desc->c_cluster);
	if (ret) {
		ocfs2_free(&desc->c_stack);
		return ret;
	}

	memcpy(desc->c_stack, sb->s_cluster_info.ci_stack,
	       OCFS2_STACK_LABEL_LEN);
	memcpy(desc->c_cluster, sb->s_cluster_info.ci_cluster,
	       OCFS2_CLUSTER_NAME_LEN);
	desc->c_flags = sb->s_cluster_info.ci_stackflags;

	return 0;
}

static errcode_t ocfs2_set_cluster_flags(ocfs2_filesys *fs,
					 struct o2cb_cluster_desc *desc)
{
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);

	sb->s_cluster_info.ci_stackflags = 0;

	/* exit if classic o2cb (default cluster stack) */
	if (!desc->c_stack)
		goto out;

	if (strcmp(desc->c_stack, OCFS2_CLASSIC_CLUSTER_STACK))
		goto out;

	if ((desc->c_flags & OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT))
		sb->s_cluster_info.ci_stackflags |=
			OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT;
out:
	return 0;
}

static errcode_t ocfs2_set_cluster_incompats(ocfs2_filesys *fs,
					     struct o2cb_cluster_desc *desc)
{
	errcode_t ret = 0;
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);

	/* if default stack, then disable both clusterinfo and userspace */
	if (!desc->c_stack) {
		sb->s_feature_incompat &=
			~OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK;
		sb->s_feature_incompat &=
			~OCFS2_FEATURE_INCOMPAT_CLUSTERINFO;
		goto out;
	}

	/* extended slot map has to be enabled for non default stack */
	if (!ocfs2_uses_extended_slot_map(sb)) {
		sb->s_feature_incompat |=
			OCFS2_FEATURE_INCOMPAT_EXTENDED_SLOT_MAP;
		ret = ocfs2_format_slot_map(fs);
		if (ret)
			goto out;
	}

	/* if o2cb, then enable clusterinfo and disable userspace */
	if (!strcmp(desc->c_stack, OCFS2_CLASSIC_CLUSTER_STACK)) {
		sb->s_feature_incompat |=
			OCFS2_FEATURE_INCOMPAT_CLUSTERINFO;
		sb->s_feature_incompat &=
			~OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK;
		goto out;
	}

	/* if not o2cb, then enable userspace only if clusterinfo disabled */
	if (!(sb->s_feature_incompat & OCFS2_FEATURE_INCOMPAT_CLUSTERINFO))
		sb->s_feature_incompat |=
			OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK;
	else
		sb->s_feature_incompat &=
			~OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK;

out:
	return ret;
}

errcode_t ocfs2_set_cluster_desc(ocfs2_filesys *fs,
				 struct o2cb_cluster_desc *desc)
{
	errcode_t ret = OCFS2_ET_INVALID_ARGUMENT;
	struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);

	if (!desc->c_stack) {
		memset(sb->s_cluster_info.ci_stack, 0, OCFS2_STACK_LABEL_LEN);
		memset(sb->s_cluster_info.ci_cluster, 0,
		       OCFS2_CLUSTER_NAME_LEN);
	} else {
		if (!o2cb_valid_stack_name(desc->c_stack))
			goto out;

		if (!strcmp(desc->c_stack, OCFS2_CLASSIC_CLUSTER_STACK)) {
			if (!o2cb_valid_o2cb_cluster_name(desc->c_cluster))
				goto out;
		} else {
			if (!o2cb_valid_cluster_name(desc->c_cluster))
				goto out;
		}

		memcpy(sb->s_cluster_info.ci_stack, desc->c_stack,
		       OCFS2_STACK_LABEL_LEN);
		memcpy(sb->s_cluster_info.ci_cluster, desc->c_cluster,
		       OCFS2_CLUSTER_NAME_LEN);
	}

	ret = ocfs2_set_cluster_flags(fs, desc);
	if (ret)
		goto out;

	ret = ocfs2_set_cluster_incompats(fs, desc);
	if (ret)
		goto out;

	ret = ocfs2_write_super(fs);

out:
	return ret;
}

errcode_t ocfs2_initialize_dlm(ocfs2_filesys *fs, const char *service)
{
	struct o2dlm_ctxt *dlm_ctxt = NULL;
	errcode_t ret = 0;
	int stackglue_support;
	struct o2cb_cluster_desc cluster;
	struct o2cb_region_desc desc;
	char *stack_path;

	ret = ocfs2_fill_cluster_desc(fs, &cluster);
	if (ret)
		goto bail;

	ret = ocfs2_fill_heartbeat_desc(fs, &desc);
	if (ret)
		goto bail;

	ret = o2dlm_supports_stackglue(&stackglue_support);
	if (ret)
		goto bail;

	desc.r_service = (char *)service;
	desc.r_persist = 0;
	ret = o2cb_begin_group_join(&cluster, &desc);
	if (ret)
		goto bail;

	/*
	 * We want to use dlmfs if we can, as it provides the full feature
	 * set of libo2dlm.  Any dlmfs with the 'stackglue' capability will
	 * support all cluster stacks.  An empty cluster.c_stack means
	 * o2cb, which always supports dlmfs.
	 *
	 * If we're unlucky enough to have older userspace stack code,
	 * we pass NULL to avoid dlmfs.
	 */
	if (stackglue_support || !cluster.c_stack)
		stack_path = DEFAULT_DLMFS_PATH;
	else
		stack_path = NULL;
	ret = o2dlm_initialize(stack_path, fs->uuid_str, &dlm_ctxt);
	if (ret) {
		/* What to do with an error code? */

		/* Ignore the result of complete_group_join, as we want
		 * to propagate our o2dlm_initialize() error */
		o2cb_complete_group_join(&cluster, &desc, ret);
		goto bail;
	}

	ret = o2cb_complete_group_join(&cluster, &desc, 0);

	if (!ret)
		fs->fs_dlm_ctxt = dlm_ctxt;
	else
		o2dlm_destroy(dlm_ctxt);

bail:
	return ret;
}

errcode_t ocfs2_shutdown_dlm(ocfs2_filesys *fs, const char *service)
{
	errcode_t ret;
	struct o2cb_cluster_desc cluster;
	struct o2cb_region_desc desc;

	ret = o2dlm_destroy(fs->fs_dlm_ctxt);
	if (ret)
		goto bail;

	fs->fs_dlm_ctxt = NULL;

	ret = ocfs2_fill_cluster_desc(fs, &cluster);
	if (ret)
		goto bail;

	ret = ocfs2_fill_heartbeat_desc(fs, &desc);
	if (ret)
		goto bail;

	desc.r_service = (char *)service;
	desc.r_persist = 0;
	ret = o2cb_group_leave(&cluster, &desc);

bail:
	return ret;
}

errcode_t ocfs2_super_lock(ocfs2_filesys *fs)
{
	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
	errcode_t ret;

	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
			     0, 0, lock_name);

	ret = o2dlm_lock(fs->fs_dlm_ctxt, lock_name,
			 O2DLM_TRYLOCK, O2DLM_LEVEL_EXMODE);

	return ret;
}

errcode_t ocfs2_super_unlock(ocfs2_filesys *fs)
{
	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
	errcode_t ret;

	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_SUPER, OCFS2_SUPER_BLOCK_BLKNO,
			     0, 0, lock_name);

	ret = o2dlm_unlock(fs->fs_dlm_ctxt, lock_name);

	return ret;
}

errcode_t ocfs2_meta_lock(ocfs2_filesys *fs,
			  ocfs2_cached_inode *ci,
			  enum o2dlm_lock_level level,
			  int flags)
{
	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
	errcode_t ret;

	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_META, ci->ci_blkno,
			     ci->ci_inode->i_generation, 0, lock_name);

	ret = o2dlm_lock(fs->fs_dlm_ctxt, lock_name, flags, level);

	return ret;
}

errcode_t ocfs2_meta_unlock(ocfs2_filesys *fs,
			    ocfs2_cached_inode *ci)
{
	char lock_name[OCFS2_LOCK_ID_MAX_LEN];
	errcode_t ret;

	ocfs2_encode_lockres(OCFS2_LOCK_TYPE_META, ci->ci_blkno,
			     ci->ci_inode->i_generation, 0, lock_name);

	ret = o2dlm_unlock(fs->fs_dlm_ctxt, lock_name);

	return ret;

}

#ifdef DEBUG_EXE
#include <getopt.h>
#include <limits.h>
#include <stdlib.h>
#include <libgen.h>

#define DEBUG_SERVICE "debug"

static void print_usage(void)
{
	fprintf(stderr, "Usage: dlm <filename>\n");
}

extern int opterr, optind;
extern char *optarg;

int main(int argc, char *argv[])
{
	errcode_t ret;
	int c;
	char *filename;
	ocfs2_filesys *fs = NULL;
	char *progname;

	initialize_ocfs_error_table();
	initialize_o2dl_error_table();

	if (argc < 2) {
		print_usage();
		exit(1);
	}

	filename = argv[1];
	progname = basename(argv[0]);

	ret = ocfs2_open(filename, OCFS2_FLAG_RO, 0, 0, &fs);
	if (ret) {
		com_err(progname, ret,
			"while opening file \"%s\"", filename);
		goto out;
	}

	ret = ocfs2_initialize_dlm(fs, DEBUG_SERVICE);
	if (ret) {
		com_err(progname, ret, "while initializing dlm");
		goto out;
	}

	printf("DLM initialized\n");
	ret = ocfs2_lock_down_cluster(fs);
	if (ret) {
		com_err(progname, ret, "while locking cluster");
		goto out;
	}

	printf("Cluster is locked\nPress any key to continue...");
	c = getchar();

	ret = ocfs2_release_cluster(fs);
	if (ret) {
		com_err(progname, ret, "while releasing cluster");
		goto out;
	}

	printf("Cluster released\n");

out:
	if (fs->fs_dlm_ctxt) {
		ret = ocfs2_shutdown_dlm(fs, DEBUG_SERVICE);
		if (ret)
			com_err(progname, ret, "while shutting down dlm");
	}

	if (fs) {
		ret = ocfs2_close(fs);
		if (ret)
			com_err(progname, ret,
				"while closing file \"%s\"", filename);
	}

	return 0;
}
#endif  /* DEBUG_EXE */