File: lockid.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 (147 lines) | stat: -rw-r--r-- 3,749 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
/* -*- mode: c; c-basic-offset: 8; -*-
 * vim: noexpandtab sw=8 ts=8 sts=0:
 *
 * lockid.c
 *
 * Encode and decode lockres name
 *
 * Copyright (C) 2004, 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., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301 USA.
 */

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

#include <string.h>
#include <inttypes.h>

enum ocfs2_lock_type ocfs2_get_lock_type(char c)
{
	switch (c) {
	case 'M':
		return OCFS2_LOCK_TYPE_META;
	case 'D':
		return OCFS2_LOCK_TYPE_DATA;
	case 'S':
		return OCFS2_LOCK_TYPE_SUPER;
	case 'R':
		return OCFS2_LOCK_TYPE_RENAME;
	case 'W':
		return OCFS2_LOCK_TYPE_RW;
	case 'N':
		return OCFS2_LOCK_TYPE_DENTRY;
	case 'O':
		return OCFS2_LOCK_TYPE_OPEN;
	case 'F':
		return OCFS2_LOCK_TYPE_FLOCK;
	default:
		return OCFS2_NUM_LOCK_TYPES;
	}
}

/*
 * This function encodes the lockname just like the filesystem. Meaning
 * the dentry lock gets encoded in its binary form.
 */
errcode_t ocfs2_encode_lockres(enum ocfs2_lock_type type, uint64_t blkno,
			       uint32_t generation, uint64_t parent,
			       char *lockres)
{
	uint64_t b;

	if (type >= OCFS2_NUM_LOCK_TYPES)
		return OCFS2_ET_INVALID_LOCKRES;

	if (type != OCFS2_LOCK_TYPE_DENTRY) {
		snprintf(lockres, OCFS2_LOCK_ID_MAX_LEN, "%c%s%016"PRIx64"%08x",
			 ocfs2_lock_type_char(type), OCFS2_LOCK_ID_PAD,
			 blkno, generation);
	} else {
		snprintf(lockres, OCFS2_DENTRY_LOCK_INO_START, "%c%016llx",
			 ocfs2_lock_type_char(OCFS2_LOCK_TYPE_DENTRY),
			 (long long)parent);
		b = bswap_64(blkno);
		memcpy(&lockres[OCFS2_DENTRY_LOCK_INO_START], &b, sizeof(b));
	}

	return 0;
}

errcode_t ocfs2_decode_lockres(char *lockres, enum ocfs2_lock_type *type,
			       uint64_t *blkno, uint32_t *generation,
			       uint64_t *parent)
{
	int i = 0;
	enum ocfs2_lock_type t;
	char *l = lockres;
	uint64_t b = 0;
	uint64_t p = 0;
	uint32_t g = 0;
	
	if ((t = ocfs2_get_lock_type(*l)) >= OCFS2_NUM_LOCK_TYPES)
		return OCFS2_ET_INVALID_LOCKRES;

	if (t != OCFS2_LOCK_TYPE_DENTRY) {
		i = sscanf(l + 1, OCFS2_LOCK_ID_PAD"%016llx%08x",
				(unsigned long long *)&b, &g);
		if (i != 2)
			return OCFS2_ET_INVALID_LOCKRES;
	} else {
		i = sscanf(l + 1, "%016llx", (unsigned long long *)&p);
		if (i != 1)
			return OCFS2_ET_INVALID_LOCKRES;
		b = strtoull(&l[OCFS2_DENTRY_LOCK_INO_START], NULL, 16);
	}

	if (type)
		*type = t;

	if (blkno)
		*blkno = b;

	if (generation)
		*generation = g;

	if (parent)
		*parent = p;

	return 0;
}

/*
 * This function is useful when printing the dentry lock. It converts the
 * dentry lockname into a string using the same scheme as used in dlmglue.
 */
errcode_t ocfs2_printable_lockres(char *lockres, char *name, int len)
{
	uint64_t b;

	memset(name, 0, len);

	if (ocfs2_get_lock_type(*lockres) >= OCFS2_NUM_LOCK_TYPES)
		return OCFS2_ET_INVALID_LOCKRES;

	if (ocfs2_get_lock_type(*lockres) == OCFS2_LOCK_TYPE_DENTRY) {
		memcpy((uint64_t *)&b,
		       (char *)&lockres[OCFS2_DENTRY_LOCK_INO_START],
		       sizeof(uint64_t));
		snprintf(name, len, "%.*s%08x", OCFS2_DENTRY_LOCK_INO_START - 1,
			 lockres, (unsigned int)bswap_64(b));
	} else
		snprintf(name, len, "%s", lockres);

	return 0;
}