File: dump_fs_locks.c

package info (click to toggle)
ocfs2-tools 1.8.5-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,828 kB
  • sloc: ansic: 85,876; sh: 5,775; python: 2,380; makefile: 1,281
file content (474 lines) | stat: -rw-r--r-- 10,505 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
474
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * dump_fs_locks.c
 *
 * Interface with the kernel and dump current fs locking state
 *
 * Copyright (C) 2005, 2008 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., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 *
 */

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

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <unistd.h>
#include <limits.h>
#include <linux/types.h>

#include <inttypes.h>
#include <stdio.h>

#include "main.h"
#include "ocfs2/byteorder.h"
#include "ocfs2_internals.h"

static char *level_str(int level)
{
	char *s;

	switch (level) {
	case LKM_IVMODE:
		s = "Invalid";
		break;
	case LKM_NLMODE:
		s = "No Lock";
		break;
	case LKM_CRMODE:
		s = "Concurrent Read";
		break;
	case LKM_CWMODE:
		s = "Concurrent Write";
		break;
	case LKM_PRMODE:
		s = "Protected Read";
		break;
	case LKM_PWMODE:
		s = "Protected Write";
		break;
	case LKM_EXMODE:
		s = "Exclusive";
		break;
	default:
		s = "Unknown";
	}

	return s;
}

static void print_flags(unsigned long flags, FILE *out)
{
	if (flags & OCFS2_LOCK_INITIALIZED )
		fprintf(out, " Initialized");

	if (flags & OCFS2_LOCK_ATTACHED)
		fprintf(out, " Attached");

	if (flags & OCFS2_LOCK_BUSY)
		fprintf(out, " Busy");

	if (flags & OCFS2_LOCK_BLOCKED)
		fprintf(out, " Blocked");

	if (flags & OCFS2_LOCK_LOCAL)
		fprintf(out, " Local");

	if (flags & OCFS2_LOCK_NEEDS_REFRESH)
		fprintf(out, " Needs Refresh");

	if (flags & OCFS2_LOCK_REFRESHING)
		fprintf(out, " Refreshing");

	if (flags & OCFS2_LOCK_FREEING)
		fprintf(out, " Freeing");

	if (flags & OCFS2_LOCK_QUEUED)
		fprintf(out, " Queued");
}

static char *action_str(unsigned int action)
{
	char *s;

	switch (action) {
	case OCFS2_AST_INVALID:
		s = "None";
		break;
	case OCFS2_AST_ATTACH:
		s = "Attach";
		break;
	case OCFS2_AST_CONVERT:
		s = "Convert";
		break;
	case OCFS2_AST_DOWNCONVERT:
		s = "Downconvert";
		break;
	default:
		s = "Unknown";
	}

	return s;
}

static char *unlock_action_str(unsigned int unlock_action)
{
	char *s;
	switch (unlock_action) {
	case OCFS2_UNLOCK_INVALID:
		s = "None";
		break;
	case OCFS2_UNLOCK_CANCEL_CONVERT:
		s = "Cancel Convert";
		break;
	case OCFS2_UNLOCK_DROP_LOCK:
		s = "Drop Lock";
		break;
	default:
		s = "Unknown";
	}

	return s;
}

static void dump_raw_lvb(const char *lvb, FILE *out)
{
	int i;

	fprintf(out, "Raw LVB:\t");

	for(i = 0; i < DLM_LVB_LEN; i++) {
		fprintf(out, "%02hhx ", lvb[i]);
		if (!((i+1) % 16) && i != (DLM_LVB_LEN-1))
			fprintf(out, "\n\t\t");
	}
	fprintf(out, "\n");
}

static void dump_meta_lvb_v1(struct ocfs2_meta_lvb_v1 *lvb, FILE *out)
{
	fprintf(out, "Decoded LVB:\t");

	fprintf(out, "Version: %u  "
		"Clusters: %u  "
		"Size: %"PRIu64"\n",
		be32_to_cpu(lvb->lvb_version),
		be32_to_cpu(lvb->lvb_iclusters),
		be64_to_cpu(lvb->lvb_isize));
	fprintf(out, "\t\tMode: 0%o  "
		"UID: %u  "
		"GID: %u  "
		"Nlink: %u\n",
		be16_to_cpu(lvb->lvb_imode),
		be32_to_cpu(lvb->lvb_iuid),
		be32_to_cpu(lvb->lvb_igid),
		be16_to_cpu(lvb->lvb_inlink));
	fprintf(out, "\t\tAtime_packed: 0x%"PRIx64"\n"
		"\t\tCtime_packed: 0x%"PRIx64"\n"
		"\t\tMtime_packed: 0x%"PRIx64"\n",
		be64_to_cpu(lvb->lvb_iatime_packed),
		be64_to_cpu(lvb->lvb_ictime_packed),
		be64_to_cpu(lvb->lvb_imtime_packed));
}

static void dump_meta_lvb_v2(struct ocfs2_meta_lvb_v2 *lvb, FILE *out)
{
	fprintf(out, "Decoded LVB:\t");

	fprintf(out, "Version: %u  "
		"Clusters: %u  "
		"Size: %"PRIu64"\n",
		be32_to_cpu(lvb->lvb_version),
		be32_to_cpu(lvb->lvb_iclusters),
		be64_to_cpu(lvb->lvb_isize));
	fprintf(out, "\t\tMode: 0%o  "
		"UID: %u  "
		"GID: %u  "
		"Nlink: %u\n",
		be16_to_cpu(lvb->lvb_imode),
		be32_to_cpu(lvb->lvb_iuid),
		be32_to_cpu(lvb->lvb_igid),
		be16_to_cpu(lvb->lvb_inlink));
	fprintf(out, "\t\tAtime_packed: 0x%"PRIx64"\n"
		"\t\tCtime_packed: 0x%"PRIx64"\n"
		"\t\tMtime_packed: 0x%"PRIx64"\n",
		be64_to_cpu(lvb->lvb_iatime_packed),
		be64_to_cpu(lvb->lvb_ictime_packed),
		be64_to_cpu(lvb->lvb_imtime_packed));
}

static void dump_meta_lvb(const char *raw_lvb, FILE *out)
{
	struct ocfs2_meta_lvb_v1 *lvb1 = (struct ocfs2_meta_lvb_v1 *) raw_lvb;
	struct ocfs2_meta_lvb_v2 *lvb2 = (struct ocfs2_meta_lvb_v2 *) raw_lvb;

	if (!lvb1->lvb_old_seq && be32_to_cpu(lvb1->lvb_version) == 1)
		dump_meta_lvb_v1(lvb1, out);
	else if (be32_to_cpu(lvb2->lvb_version) == 2)
		dump_meta_lvb_v2(lvb2, out);
}

/* 0 = eof, > 0 = success, < 0 = error */
static int dump_version_two_and_three(FILE *file, FILE *out, int v3)
{
	unsigned long long num_prmode, num_exmode;
	unsigned int num_prmode_failed, num_exmode_failed;
	unsigned long long  total_prmode, total_exmode;
	unsigned long long  avg_prmode = 0, avg_exmode = 0;
	unsigned int max_prmode, max_exmode, num_refresh;
	int ret;

#define NSEC_PER_USEC   1000

	ret = fscanf(file, "%llu\t"
		     "%llu\t"
		     "%u\t"
		     "%u\t"
		     "%llu\t"
		     "%llu\t"
		     "%u\t"
		     "%u\t"
		     "%u",
		     &num_prmode,
		     &num_exmode,
		     &num_prmode_failed,
		     &num_exmode_failed,
		     &total_prmode,
		     &total_exmode,
		     &max_prmode,
		     &max_exmode,
		     &num_refresh);
	if (ret != 9) {
		ret = -EINVAL;
		goto out;
	}

	if (!v3) {
		max_prmode /= NSEC_PER_USEC;
		max_exmode /= NSEC_PER_USEC;
	}

	if (num_prmode)
		avg_prmode = total_prmode/num_prmode;

	if (num_exmode)
		avg_exmode = total_exmode/num_exmode;

	fprintf(out, "PR > Gets: %llu  Fails: %u    Waits Total: %lluus  "
		"Max: %uus  Avg: %lluns\n",
		num_prmode, num_prmode_failed, total_prmode/NSEC_PER_USEC,
		max_prmode, avg_prmode);
	fprintf(out, "EX > Gets: %llu  Fails: %u    Waits Total: %lluus  "
		"Max: %uus  Avg: %lluns\n",
		num_exmode, num_exmode_failed, total_exmode/NSEC_PER_USEC,
		max_exmode, avg_exmode);
	fprintf(out, "Disk Refreshes: %u\n", num_refresh);

	ret = 1;
out:
	return ret;
}

/* 0 = eof, > 0 = success, < 0 = error */
static int dump_version_one(FILE *file, FILE *out, int lvbs, int only_busy,
			    struct list_head *locklist, int *skipped,
			    unsigned int version)
{
	char id[OCFS2_LOCK_ID_MAX_LEN + 1];	
	char lvb[DLM_LVB_LEN];
	int ret, i, level, requested, blocking;
	unsigned long flags;
	unsigned int action, unlock_action, ro, ex, dummy;
	const char *format;

	*skipped = 1;

	ret = fscanf(file, "%s\t"
		     "%d\t"
		     "0x%lx\t"
		     "0x%x\t"
		     "0x%x\t"
		     "%u\t"
		     "%u\t"
		     "%d\t"
		     "%d\t",
		     id,
		     &level,
		     &flags,
		     &action,
		     &unlock_action,
		     &ro,
		     &ex,
		     &requested,
		     &blocking);
	if (ret != 9) {
		ret = -EINVAL;
		goto out;
	}

	format = "0x%x\t";
	for (i = 0; i < DLM_LVB_LEN; i++) {
		/* This is the known last part of the record. If we
		 * include the field delimiting '\t' then fscanf will
		 * also catch the record delimiting '\n' character,
		 * which we want to save for the caller to find. */
		if ((i == (DLM_LVB_LEN - 1)) && (version < 2))
			format = "0x%x";

		ret = fscanf(file, format, &dummy);
		if (ret != 1) {
			ret = -EINVAL;
			goto out;
		}

		lvb[i] = (char) dummy;
	}

	if (!list_empty(locklist)) {
		if (!del_from_stringlist(id, locklist)) {
			ret = 1;
			goto out;
		}
	}

	if (only_busy) {
		if (!(flags & OCFS2_LOCK_BUSY)) {
			ret = 1;
			goto out;
		}
	}

	fprintf(out, "Lockres: %s  Mode: %s\nFlags:", id, level_str(level));
	print_flags(flags, out);
	fprintf(out, "\nRO Holders: %u  EX Holders: %u\n", ro, ex);
	fprintf(out, "Pending Action: %s  Pending Unlock Action: %s\n",
		action_str(action), unlock_action_str(unlock_action));
	fprintf(out, "Requested Mode: %s  Blocking Mode: %s\n",
		level_str(requested), level_str(blocking));

	if (lvbs) {
		dump_raw_lvb(lvb, out);
		if (id[0] == 'M')
			dump_meta_lvb(lvb, out);
	}

	*skipped = 0;

	ret = 1;
out:
	return ret;
}

static int end_line(FILE *f)
{
	int ret;

	do {
		ret = fgetc(f);
		if (ret == EOF)
			return 1;
	} while (ret != '\n');

	return 0;
}

#define CURRENT_PROTO 3
/* returns 0 on error or end of file */
static int dump_one_lockres(FILE *file, FILE *out, int lvbs, int only_busy,
			    struct list_head *locklist)
{
	unsigned int version;
	int ret, v3;
	int skipped = 0;

	ret = fscanf(file, "%x\t", &version);
	if (ret != 1)
		return 0;

	if (version > CURRENT_PROTO) {
		fprintf(stdout, "Debug string proto %u found, but %u is the "
			"highest I understand.\n", version, CURRENT_PROTO);
		return 0;
	}

	ret = dump_version_one(file, out, lvbs, only_busy, locklist, &skipped, version);
	if (ret <= 0)
		return 0;

	if (!skipped) {
		if (version > 1) {
			v3 = !!(version == 3);
			ret = dump_version_two_and_three(file, out, v3);
			if (ret <= 0)
				return 0;
		}

		fprintf(out, "\n");
	}

	/* Read to the end of the record here. Any new fields tagged
	 * onto the current format will be silently ignored. */
	ret = !end_line(file);

	return ret;
}

void dump_fs_locks(char *uuid_str, FILE *out, char *path, int dump_lvbs,
		   int only_busy, struct list_head *locklist)
{
	errcode_t ret;
	char debugfs_path[PATH_MAX];
	FILE *file;
	int show_select;

	if (path == NULL) {
		ret = get_debugfs_path(debugfs_path, sizeof(debugfs_path));
		if (ret) {
			fprintf(stderr, "Could not locate debugfs file system. "
				"Perhaps it is not mounted?\n");
			return;
		}

		ret = open_debugfs_file(debugfs_path, "ocfs2", uuid_str,
					"locking_state", &file);
		if (ret) {
			fprintf(stderr, "Could not open debug state for "
				"\"%s\".\nPerhaps that OCFS2 file system is "
				"not mounted?\n", uuid_str);
			return;
		}
	} else {
		file = fopen(path, "r");
		if (!file) {
			fprintf(stderr, "Could not open file at \"%s\"\n",
				path);
			return;
		}
	}

	show_select = !list_empty(locklist);

	while (dump_one_lockres(file, out, dump_lvbs, only_busy, locklist)) {
		if (show_select && list_empty(locklist))
			break;
	}

	fclose(file);
}