Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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>
27
28 struct uuid {
29         __u32   time_low;
30         __u16   time_mid;
31         __u16   time_hi_and_version;
32         __u16   clock_seq;
33         __u8    node[6];
34 };
35
36 static void uuid_unpack(class_uuid_t in, struct uuid *uu)
37 {
38         __u8    *ptr = in;
39         __u32   tmp;
40
41         tmp = *ptr++;
42         tmp = (tmp << 8) | *ptr++;
43         tmp = (tmp << 8) | *ptr++;
44         tmp = (tmp << 8) | *ptr++;
45         uu->time_low = tmp;
46
47         tmp = *ptr++;
48         tmp = (tmp << 8) | *ptr++;
49         uu->time_mid = tmp;
50
51         tmp = *ptr++;
52         tmp = (tmp << 8) | *ptr++;
53         uu->time_hi_and_version = tmp;
54
55         tmp = *ptr++;
56         tmp = (tmp << 8) | *ptr++;
57         uu->clock_seq = tmp;
58
59         memcpy(uu->node, ptr, 6);
60 }
61
62 #if 0
63 static void uuid_pack(struct uuid *uu, class_uuid_t ptr)
64 {
65         __u32   tmp;
66         unsigned char   *out = ptr;
67
68         tmp = uu->time_low;
69         out[3] = (unsigned char) tmp;
70         tmp >>= 8;
71         out[2] = (unsigned char) tmp;
72         tmp >>= 8;
73         out[1] = (unsigned char) tmp;
74         tmp >>= 8;
75         out[0] = (unsigned char) tmp;
76
77         tmp = uu->time_mid;
78         out[5] = (unsigned char) tmp;
79         tmp >>= 8;
80         out[4] = (unsigned char) tmp;
81
82         tmp = uu->time_hi_and_version;
83         out[7] = (unsigned char) tmp;
84         tmp >>= 8;
85         out[6] = (unsigned char) tmp;
86
87         tmp = uu->clock_seq;
88         out[9] = (unsigned char) tmp;
89         tmp >>= 8;
90         out[8] = (unsigned char) tmp;
91
92         memcpy(out+10, uu->node, 6);
93 }
94
95 int class_uuid_parse(struct obd_uuid in, class_uuid_t uu)
96 {
97         struct uuid uuid;
98         int i;
99         char *cp, buf[3];
100
101         if (strlen(in) != 36)
102                 return -1;
103         for (i=0, cp = in; i <= 36; i++,cp++) {
104                 if ((i == 8) || (i == 13) || (i == 18) ||
105                     (i == 23))
106                         if (*cp == '-')
107                                 continue;
108                 if (i== 36)
109                         if (*cp == 0)
110                                 continue;
111                 if (!isxdigit(*cp))
112                         return -1;
113         }
114         uuid.time_low = simple_strtoul(in, NULL, 16);
115         uuid.time_mid = simple_strtoul(in+9, NULL, 16);
116         uuid.time_hi_and_version = simple_strtoul(in+14, NULL, 16);
117         uuid.clock_seq = simple_strtoul(in+19, NULL, 16);
118         cp = in+24;
119         buf[2] = 0;
120         for (i=0; i < 6; i++) {
121                 buf[0] = *cp++;
122                 buf[1] = *cp++;
123                 uuid.node[i] = simple_strtoul(buf, NULL, 16);
124         }
125
126         uuid_pack(&uuid, uu);
127         return 0;
128 }
129 #endif
130
131 void class_uuid_unparse(class_uuid_t uu, struct obd_uuid *out)
132 {
133         struct uuid uuid;
134
135         uuid_unpack(uu, &uuid);
136         sprintf(out->uuid,
137                 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
138                 uuid.time_low, uuid.time_mid, uuid.time_hi_and_version,
139                 uuid.clock_seq >> 8, uuid.clock_seq & 0xFF,
140                 uuid.node[0], uuid.node[1], uuid.node[2],
141                 uuid.node[3], uuid.node[4], uuid.node[5]);
142 }
143
144 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid)
145 {
146         int i;
147
148         for (i = 0; i < MAX_OBD_DEVICES; i++) {
149                 struct obd_device *obd = &obd_dev[i];
150                 if (obd->obd_type == NULL)
151                         continue;
152                 if ((strncmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME,
153                              sizeof LUSTRE_OSC_NAME) == 0) ||
154                     (strncmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME,
155                              sizeof LUSTRE_MDC_NAME) == 0)) {
156                         struct client_obd *cli = &obd->u.cli;
157                         struct obd_import *imp = cli->cl_import;
158                         if (strncmp(tgtuuid->uuid, imp->imp_target_uuid.uuid,
159                                     sizeof(imp->imp_target_uuid)) == 0)
160                                 return obd;
161                 }
162         }
163
164         return NULL;
165 }