Whamcloud - gitweb
b=3031
[fs/lustre-release.git] / lustre / obdclass / uuid.c
1 /*
2  * Public include file for the UUID library
3  * 
4  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
5  * Copyright (C) 2002 Cluster File System
6  * - changed for use in lustre
7  *
8  * %Begin-Header%
9  * This file may be redistributed under the terms of the GNU 
10  * Library General Public License.
11  * %End-Header%
12  */
13 #define DEBUG_SUBSYSTEM S_CLASS
14
15 #ifdef __KERNEL__
16 # include <linux/ctype.h>
17 # include <linux/kernel.h>
18 # include <linux/sched.h>
19 # include <linux/smp_lock.h>
20 #else
21 # include <liblustre.h>
22 #endif
23
24 #include <linux/obd_support.h>
25 #include <linux/obd_class.h>
26 #include <linux/obd_ost.h> /* for LUSTRE_OST_NAME */
27 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
28
29 struct uuid {
30         __u32   time_low;
31         __u16   time_mid;
32         __u16   time_hi_and_version;
33         __u16   clock_seq;
34         __u8    node[6];
35 };
36
37 static void uuid_unpack(class_uuid_t in, struct uuid *uu)
38 {
39         __u8    *ptr = in;
40         __u32   tmp;
41
42         tmp = *ptr++;
43         tmp = (tmp << 8) | *ptr++;
44         tmp = (tmp << 8) | *ptr++;
45         tmp = (tmp << 8) | *ptr++;
46         uu->time_low = tmp;
47
48         tmp = *ptr++;
49         tmp = (tmp << 8) | *ptr++;
50         uu->time_mid = tmp;
51
52         tmp = *ptr++;
53         tmp = (tmp << 8) | *ptr++;
54         uu->time_hi_and_version = tmp;
55
56         tmp = *ptr++;
57         tmp = (tmp << 8) | *ptr++;
58         uu->clock_seq = tmp;
59
60         memcpy(uu->node, ptr, 6);
61 }
62
63 #if 0
64 static void uuid_pack(struct uuid *uu, class_uuid_t ptr)
65 {
66         __u32   tmp;
67         unsigned char   *out = ptr;
68
69         tmp = uu->time_low;
70         out[3] = (unsigned char) tmp;
71         tmp >>= 8;
72         out[2] = (unsigned char) tmp;
73         tmp >>= 8;
74         out[1] = (unsigned char) tmp;
75         tmp >>= 8;
76         out[0] = (unsigned char) tmp;
77
78         tmp = uu->time_mid;
79         out[5] = (unsigned char) tmp;
80         tmp >>= 8;
81         out[4] = (unsigned char) tmp;
82
83         tmp = uu->time_hi_and_version;
84         out[7] = (unsigned char) tmp;
85         tmp >>= 8;
86         out[6] = (unsigned char) tmp;
87
88         tmp = uu->clock_seq;
89         out[9] = (unsigned char) tmp;
90         tmp >>= 8;
91         out[8] = (unsigned char) tmp;
92
93         memcpy(out+10, uu->node, 6);
94 }
95
96 int class_uuid_parse(struct obd_uuid in, class_uuid_t uu)
97 {
98         struct uuid uuid;
99         int i;
100         char *cp, buf[3];
101
102         if (strlen(in) != 36)
103                 return -1;
104         for (i=0, cp = in; i <= 36; i++,cp++) {
105                 if ((i == 8) || (i == 13) || (i == 18) ||
106                     (i == 23))
107                         if (*cp == '-')
108                                 continue;
109                 if (i== 36)
110                         if (*cp == 0)
111                                 continue;
112                 if (!isxdigit(*cp))
113                         return -1;
114         }
115         uuid.time_low = simple_strtoul(in, NULL, 16);
116         uuid.time_mid = simple_strtoul(in+9, NULL, 16);
117         uuid.time_hi_and_version = simple_strtoul(in+14, NULL, 16);
118         uuid.clock_seq = simple_strtoul(in+19, NULL, 16);
119         cp = in+24;
120         buf[2] = 0;
121         for (i=0; i < 6; i++) {
122                 buf[0] = *cp++;
123                 buf[1] = *cp++;
124                 uuid.node[i] = simple_strtoul(buf, NULL, 16);
125         }
126
127         uuid_pack(&uuid, uu);
128         return 0;
129 }
130 #endif
131
132 void class_uuid_unparse(class_uuid_t uu, struct obd_uuid *out)
133 {
134         struct uuid uuid;
135
136         uuid_unpack(uu, &uuid);
137         sprintf(out->uuid,
138                 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
139                 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
140                 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
141                 uuid.node[0], uuid.node[1], uuid.node[2],
142                 uuid.node[3], uuid.node[4], uuid.node[5]);
143 }
144
145 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid)
146 {
147         int i;
148
149         for (i = 0; i < MAX_OBD_DEVICES; i++) {
150                 struct obd_device *obd = &obd_dev[i];
151                 if (obd->obd_type == NULL)
152                         continue;
153                 if ((strncmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME,
154                              sizeof LUSTRE_OSC_NAME) == 0) ||
155                     (strncmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME,
156                              sizeof LUSTRE_MDC_NAME) == 0)) {
157                         struct client_obd *cli = &obd->u.cli;
158                         struct obd_import *imp = cli->cl_import;
159                         if (strncmp(tgtuuid->uuid, imp->imp_target_uuid.uuid,
160                                     sizeof(imp->imp_target_uuid)) == 0)
161                                 return obd;
162                 }
163         }
164
165         return NULL;
166 }