aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-03-20 14:16:20 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-03-20 14:16:20 -0400
commit4a52246302f01596f0edf7b4a3e6425e23479192 (patch)
treef384d86722d3ccfc875e3e5e8d8726e993a922ee /tools
parent9f9d2760da8c7f94fae119fac3e13d5a1702f8f0 (diff)
parentadc80ae60eae24a43a357bf5b30fb496f34aa605 (diff)
Merge tag 'driver-core-3.3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core
Pull driver core patches for 3.4-rc1 from Greg KH: "Here's the big driver core merge for 3.4-rc1. Lots of various things here, sysfs fixes/tweaks (with the nlink breakage reverted), dynamic debugging updates, w1 drivers, hyperv driver updates, and a variety of other bits and pieces, full information in the shortlog." * tag 'driver-core-3.3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core: (78 commits) Tools: hv: Support enumeration from all the pools Tools: hv: Fully support the new KVP verbs in the user level daemon Drivers: hv: Support the newly introduced KVP messages in the driver Drivers: hv: Add new message types to enhance KVP regulator: Support driver probe deferral Revert "sysfs: Kill nlink counting." uevent: send events in correct order according to seqnum (v3) driver core: minor comment formatting cleanups driver core: move the deferred probe pointer into the private area drivercore: Add driver probe deferral mechanism DS2781 Maxim Stand-Alone Fuel Gauge battery and w1 slave drivers w1_bq27000: Only one thread can access the bq27000 at a time. w1_bq27000 - remove w1_bq27000_write w1_bq27000: remove unnecessary NULL test. sysfs: Fix memory leak in sysfs_sd_setsecdata(). intel_idle: Revert change of auto_demotion_disable_flags for Nehalem w1: Fix w1_bq27000 driver-core: documentation: fix up Greg's email address powernow-k6: Really enable auto-loading powernow-k7: Fix CPU family number ...
Diffstat (limited to 'tools')
-rw-r--r--tools/hv/hv_kvp_daemon.c458
1 files changed, 412 insertions, 46 deletions
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 11224eddcdc2..146fd6147e84 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -34,21 +34,13 @@
34#include <errno.h> 34#include <errno.h>
35#include <arpa/inet.h> 35#include <arpa/inet.h>
36#include <linux/connector.h> 36#include <linux/connector.h>
37#include <linux/hyperv.h>
37#include <linux/netlink.h> 38#include <linux/netlink.h>
38#include <ifaddrs.h> 39#include <ifaddrs.h>
39#include <netdb.h> 40#include <netdb.h>
40#include <syslog.h> 41#include <syslog.h>
41 42#include <sys/stat.h>
42/* 43#include <fcntl.h>
43 * KYS: TODO. Need to register these in the kernel.
44 *
45 * The following definitions are shared with the in-kernel component; do not
46 * change any of this without making the corresponding changes in
47 * the KVP kernel component.
48 */
49#define CN_KVP_IDX 0x9 /* MSFT KVP functionality */
50#define CN_KVP_VAL 0x1 /* This supports queries from the kernel */
51#define CN_KVP_USER_VAL 0x2 /* This supports queries from the user */
52 44
53/* 45/*
54 * KVP protocol: The user mode component first registers with the 46 * KVP protocol: The user mode component first registers with the
@@ -60,25 +52,8 @@
60 * We use this infrastructure for also supporting queries from user mode 52 * We use this infrastructure for also supporting queries from user mode
61 * application for state that may be maintained in the KVP kernel component. 53 * application for state that may be maintained in the KVP kernel component.
62 * 54 *
63 * XXXKYS: Have a shared header file between the user and kernel (TODO)
64 */ 55 */
65 56
66enum kvp_op {
67 KVP_REGISTER = 0, /* Register the user mode component*/
68 KVP_KERNEL_GET, /*Kernel is requesting the value for the specified key*/
69 KVP_KERNEL_SET, /*Kernel is providing the value for the specified key*/
70 KVP_USER_GET, /*User is requesting the value for the specified key*/
71 KVP_USER_SET /*User is providing the value for the specified key*/
72};
73
74#define HV_KVP_EXCHANGE_MAX_KEY_SIZE 512
75#define HV_KVP_EXCHANGE_MAX_VALUE_SIZE 2048
76
77struct hv_ku_msg {
78 __u32 kvp_index;
79 __u8 kvp_key[HV_KVP_EXCHANGE_MAX_KEY_SIZE]; /* Key name */
80 __u8 kvp_value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE]; /* Key value */
81};
82 57
83enum key_index { 58enum key_index {
84 FullyQualifiedDomainName = 0, 59 FullyQualifiedDomainName = 0,
@@ -93,10 +68,6 @@ enum key_index {
93 ProcessorArchitecture 68 ProcessorArchitecture
94}; 69};
95 70
96/*
97 * End of shared definitions.
98 */
99
100static char kvp_send_buffer[4096]; 71static char kvp_send_buffer[4096];
101static char kvp_recv_buffer[4096]; 72static char kvp_recv_buffer[4096];
102static struct sockaddr_nl addr; 73static struct sockaddr_nl addr;
@@ -109,6 +80,345 @@ static char *os_build;
109static char *lic_version; 80static char *lic_version;
110static struct utsname uts_buf; 81static struct utsname uts_buf;
111 82
83
84#define MAX_FILE_NAME 100
85#define ENTRIES_PER_BLOCK 50
86
87struct kvp_record {
88 __u8 key[HV_KVP_EXCHANGE_MAX_KEY_SIZE];
89 __u8 value[HV_KVP_EXCHANGE_MAX_VALUE_SIZE];
90};
91
92struct kvp_file_state {
93 int fd;
94 int num_blocks;
95 struct kvp_record *records;
96 int num_records;
97 __u8 fname[MAX_FILE_NAME];
98};
99
100static struct kvp_file_state kvp_file_info[KVP_POOL_COUNT];
101
102static void kvp_acquire_lock(int pool)
103{
104 struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0};
105 fl.l_pid = getpid();
106
107 if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
108 syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
109 exit(-1);
110 }
111}
112
113static void kvp_release_lock(int pool)
114{
115 struct flock fl = {F_UNLCK, SEEK_SET, 0, 0, 0};
116 fl.l_pid = getpid();
117
118 if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
119 perror("fcntl");
120 syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
121 exit(-1);
122 }
123}
124
125static void kvp_update_file(int pool)
126{
127 FILE *filep;
128 size_t bytes_written;
129
130 /*
131 * We are going to write our in-memory registry out to
132 * disk; acquire the lock first.
133 */
134 kvp_acquire_lock(pool);
135
136 filep = fopen(kvp_file_info[pool].fname, "w");
137 if (!filep) {
138 kvp_release_lock(pool);
139 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
140 exit(-1);
141 }
142
143 bytes_written = fwrite(kvp_file_info[pool].records,
144 sizeof(struct kvp_record),
145 kvp_file_info[pool].num_records, filep);
146
147 fflush(filep);
148 kvp_release_lock(pool);
149}
150
151static void kvp_update_mem_state(int pool)
152{
153 FILE *filep;
154 size_t records_read = 0;
155 struct kvp_record *record = kvp_file_info[pool].records;
156 struct kvp_record *readp;
157 int num_blocks = kvp_file_info[pool].num_blocks;
158 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
159
160 kvp_acquire_lock(pool);
161
162 filep = fopen(kvp_file_info[pool].fname, "r");
163 if (!filep) {
164 kvp_release_lock(pool);
165 syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
166 exit(-1);
167 }
168 while (!feof(filep)) {
169 readp = &record[records_read];
170 records_read += fread(readp, sizeof(struct kvp_record),
171 ENTRIES_PER_BLOCK * num_blocks,
172 filep);
173
174 if (!feof(filep)) {
175 /*
176 * We have more data to read.
177 */
178 num_blocks++;
179 record = realloc(record, alloc_unit * num_blocks);
180
181 if (record == NULL) {
182 syslog(LOG_ERR, "malloc failed");
183 exit(-1);
184 }
185 continue;
186 }
187 break;
188 }
189
190 kvp_file_info[pool].num_blocks = num_blocks;
191 kvp_file_info[pool].records = record;
192 kvp_file_info[pool].num_records = records_read;
193
194 kvp_release_lock(pool);
195}
196static int kvp_file_init(void)
197{
198 int ret, fd;
199 FILE *filep;
200 size_t records_read;
201 __u8 *fname;
202 struct kvp_record *record;
203 struct kvp_record *readp;
204 int num_blocks;
205 int i;
206 int alloc_unit = sizeof(struct kvp_record) * ENTRIES_PER_BLOCK;
207
208 if (access("/var/opt/hyperv", F_OK)) {
209 if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
210 syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
211 exit(-1);
212 }
213 }
214
215 for (i = 0; i < KVP_POOL_COUNT; i++) {
216 fname = kvp_file_info[i].fname;
217 records_read = 0;
218 num_blocks = 1;
219 sprintf(fname, "/var/opt/hyperv/.kvp_pool_%d", i);
220 fd = open(fname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IROTH);
221
222 if (fd == -1)
223 return 1;
224
225
226 filep = fopen(fname, "r");
227 if (!filep)
228 return 1;
229
230 record = malloc(alloc_unit * num_blocks);
231 if (record == NULL) {
232 fclose(filep);
233 return 1;
234 }
235 while (!feof(filep)) {
236 readp = &record[records_read];
237 records_read += fread(readp, sizeof(struct kvp_record),
238 ENTRIES_PER_BLOCK,
239 filep);
240
241 if (!feof(filep)) {
242 /*
243 * We have more data to read.
244 */
245 num_blocks++;
246 record = realloc(record, alloc_unit *
247 num_blocks);
248 if (record == NULL) {
249 fclose(filep);
250 return 1;
251 }
252 continue;
253 }
254 break;
255 }
256 kvp_file_info[i].fd = fd;
257 kvp_file_info[i].num_blocks = num_blocks;
258 kvp_file_info[i].records = record;
259 kvp_file_info[i].num_records = records_read;
260 fclose(filep);
261
262 }
263
264 return 0;
265}
266
267static int kvp_key_delete(int pool, __u8 *key, int key_size)
268{
269 int i;
270 int j, k;
271 int num_records;
272 struct kvp_record *record;
273
274 /*
275 * First update the in-memory state.
276 */
277 kvp_update_mem_state(pool);
278
279 num_records = kvp_file_info[pool].num_records;
280 record = kvp_file_info[pool].records;
281
282 for (i = 0; i < num_records; i++) {
283 if (memcmp(key, record[i].key, key_size))
284 continue;
285 /*
286 * Found a match; just move the remaining
287 * entries up.
288 */
289 if (i == num_records) {
290 kvp_file_info[pool].num_records--;
291 kvp_update_file(pool);
292 return 0;
293 }
294
295 j = i;
296 k = j + 1;
297 for (; k < num_records; k++) {
298 strcpy(record[j].key, record[k].key);
299 strcpy(record[j].value, record[k].value);
300 j++;
301 }
302
303 kvp_file_info[pool].num_records--;
304 kvp_update_file(pool);
305 return 0;
306 }
307 return 1;
308}
309
310static int kvp_key_add_or_modify(int pool, __u8 *key, int key_size, __u8 *value,
311 int value_size)
312{
313 int i;
314 int j, k;
315 int num_records;
316 struct kvp_record *record;
317 int num_blocks;
318
319 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
320 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
321 return 1;
322
323 /*
324 * First update the in-memory state.
325 */
326 kvp_update_mem_state(pool);
327
328 num_records = kvp_file_info[pool].num_records;
329 record = kvp_file_info[pool].records;
330 num_blocks = kvp_file_info[pool].num_blocks;
331
332 for (i = 0; i < num_records; i++) {
333 if (memcmp(key, record[i].key, key_size))
334 continue;
335 /*
336 * Found a match; just update the value -
337 * this is the modify case.
338 */
339 memcpy(record[i].value, value, value_size);
340 kvp_update_file(pool);
341 return 0;
342 }
343
344 /*
345 * Need to add a new entry;
346 */
347 if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
348 /* Need to allocate a larger array for reg entries. */
349 record = realloc(record, sizeof(struct kvp_record) *
350 ENTRIES_PER_BLOCK * (num_blocks + 1));
351
352 if (record == NULL)
353 return 1;
354 kvp_file_info[pool].num_blocks++;
355
356 }
357 memcpy(record[i].value, value, value_size);
358 memcpy(record[i].key, key, key_size);
359 kvp_file_info[pool].records = record;
360 kvp_file_info[pool].num_records++;
361 kvp_update_file(pool);
362 return 0;
363}
364
365static int kvp_get_value(int pool, __u8 *key, int key_size, __u8 *value,
366 int value_size)
367{
368 int i;
369 int num_records;
370 struct kvp_record *record;
371
372 if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
373 (value_size > HV_KVP_EXCHANGE_MAX_VALUE_SIZE))
374 return 1;
375
376 /*
377 * First update the in-memory state.
378 */
379 kvp_update_mem_state(pool);
380
381 num_records = kvp_file_info[pool].num_records;
382 record = kvp_file_info[pool].records;
383
384 for (i = 0; i < num_records; i++) {
385 if (memcmp(key, record[i].key, key_size))
386 continue;
387 /*
388 * Found a match; just copy the value out.
389 */
390 memcpy(value, record[i].value, value_size);
391 return 0;
392 }
393
394 return 1;
395}
396
397static void kvp_pool_enumerate(int pool, int index, __u8 *key, int key_size,
398 __u8 *value, int value_size)
399{
400 struct kvp_record *record;
401
402 /*
403 * First update our in-memory database.
404 */
405 kvp_update_mem_state(pool);
406 record = kvp_file_info[pool].records;
407
408 if (index >= kvp_file_info[pool].num_records) {
409 /*
410 * This is an invalid index; terminate enumeration;
411 * - a NULL value will do the trick.
412 */
413 strcpy(value, "");
414 return;
415 }
416
417 memcpy(key, record[index].key, key_size);
418 memcpy(value, record[index].value, value_size);
419}
420
421
112void kvp_get_os_info(void) 422void kvp_get_os_info(void)
113{ 423{
114 FILE *file; 424 FILE *file;
@@ -332,7 +642,7 @@ int main(void)
332 struct pollfd pfd; 642 struct pollfd pfd;
333 struct nlmsghdr *incoming_msg; 643 struct nlmsghdr *incoming_msg;
334 struct cn_msg *incoming_cn_msg; 644 struct cn_msg *incoming_cn_msg;
335 struct hv_ku_msg *hv_msg; 645 struct hv_kvp_msg *hv_msg;
336 char *p; 646 char *p;
337 char *key_value; 647 char *key_value;
338 char *key_name; 648 char *key_name;
@@ -345,6 +655,11 @@ int main(void)
345 */ 655 */
346 kvp_get_os_info(); 656 kvp_get_os_info();
347 657
658 if (kvp_file_init()) {
659 syslog(LOG_ERR, "Failed to initialize the pools");
660 exit(-1);
661 }
662
348 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); 663 fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
349 if (fd < 0) { 664 if (fd < 0) {
350 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd); 665 syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
@@ -370,9 +685,11 @@ int main(void)
370 message = (struct cn_msg *)kvp_send_buffer; 685 message = (struct cn_msg *)kvp_send_buffer;
371 message->id.idx = CN_KVP_IDX; 686 message->id.idx = CN_KVP_IDX;
372 message->id.val = CN_KVP_VAL; 687 message->id.val = CN_KVP_VAL;
373 message->seq = KVP_REGISTER; 688
689 hv_msg = (struct hv_kvp_msg *)message->data;
690 hv_msg->kvp_hdr.operation = KVP_OP_REGISTER;
374 message->ack = 0; 691 message->ack = 0;
375 message->len = 0; 692 message->len = sizeof(struct hv_kvp_msg);
376 693
377 len = netlink_send(fd, message); 694 len = netlink_send(fd, message);
378 if (len < 0) { 695 if (len < 0) {
@@ -398,14 +715,15 @@ int main(void)
398 715
399 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer; 716 incoming_msg = (struct nlmsghdr *)kvp_recv_buffer;
400 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg); 717 incoming_cn_msg = (struct cn_msg *)NLMSG_DATA(incoming_msg);
718 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
401 719
402 switch (incoming_cn_msg->seq) { 720 switch (hv_msg->kvp_hdr.operation) {
403 case KVP_REGISTER: 721 case KVP_OP_REGISTER:
404 /* 722 /*
405 * Driver is registering with us; stash away the version 723 * Driver is registering with us; stash away the version
406 * information. 724 * information.
407 */ 725 */
408 p = (char *)incoming_cn_msg->data; 726 p = (char *)hv_msg->body.kvp_register.version;
409 lic_version = malloc(strlen(p) + 1); 727 lic_version = malloc(strlen(p) + 1);
410 if (lic_version) { 728 if (lic_version) {
411 strcpy(lic_version, p); 729 strcpy(lic_version, p);
@@ -416,17 +734,65 @@ int main(void)
416 } 734 }
417 continue; 735 continue;
418 736
419 case KVP_KERNEL_GET: 737 /*
738 * The current protocol with the kernel component uses a
739 * NULL key name to pass an error condition.
740 * For the SET, GET and DELETE operations,
741 * use the existing protocol to pass back error.
742 */
743
744 case KVP_OP_SET:
745 if (kvp_key_add_or_modify(hv_msg->kvp_hdr.pool,
746 hv_msg->body.kvp_set.data.key,
747 hv_msg->body.kvp_set.data.key_size,
748 hv_msg->body.kvp_set.data.value,
749 hv_msg->body.kvp_set.data.value_size))
750 strcpy(hv_msg->body.kvp_set.data.key, "");
751 break;
752
753 case KVP_OP_GET:
754 if (kvp_get_value(hv_msg->kvp_hdr.pool,
755 hv_msg->body.kvp_set.data.key,
756 hv_msg->body.kvp_set.data.key_size,
757 hv_msg->body.kvp_set.data.value,
758 hv_msg->body.kvp_set.data.value_size))
759 strcpy(hv_msg->body.kvp_set.data.key, "");
760 break;
761
762 case KVP_OP_DELETE:
763 if (kvp_key_delete(hv_msg->kvp_hdr.pool,
764 hv_msg->body.kvp_delete.key,
765 hv_msg->body.kvp_delete.key_size))
766 strcpy(hv_msg->body.kvp_delete.key, "");
420 break; 767 break;
768
421 default: 769 default:
422 continue; 770 break;
771 }
772
773 if (hv_msg->kvp_hdr.operation != KVP_OP_ENUMERATE)
774 goto kvp_done;
775
776 /*
777 * If the pool is KVP_POOL_AUTO, dynamically generate
778 * both the key and the value; if not read from the
779 * appropriate pool.
780 */
781 if (hv_msg->kvp_hdr.pool != KVP_POOL_AUTO) {
782 kvp_pool_enumerate(hv_msg->kvp_hdr.pool,
783 hv_msg->body.kvp_enum_data.index,
784 hv_msg->body.kvp_enum_data.data.key,
785 HV_KVP_EXCHANGE_MAX_KEY_SIZE,
786 hv_msg->body.kvp_enum_data.data.value,
787 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
788 goto kvp_done;
423 } 789 }
424 790
425 hv_msg = (struct hv_ku_msg *)incoming_cn_msg->data; 791 hv_msg = (struct hv_kvp_msg *)incoming_cn_msg->data;
426 key_name = (char *)hv_msg->kvp_key; 792 key_name = (char *)hv_msg->body.kvp_enum_data.data.key;
427 key_value = (char *)hv_msg->kvp_value; 793 key_value = (char *)hv_msg->body.kvp_enum_data.data.value;
428 794
429 switch (hv_msg->kvp_index) { 795 switch (hv_msg->body.kvp_enum_data.index) {
430 case FullyQualifiedDomainName: 796 case FullyQualifiedDomainName:
431 kvp_get_domain_name(key_value, 797 kvp_get_domain_name(key_value,
432 HV_KVP_EXCHANGE_MAX_VALUE_SIZE); 798 HV_KVP_EXCHANGE_MAX_VALUE_SIZE);
@@ -483,12 +849,12 @@ int main(void)
483 * already in the receive buffer. Update the cn_msg header to 849 * already in the receive buffer. Update the cn_msg header to
484 * reflect the key value that has been added to the message 850 * reflect the key value that has been added to the message
485 */ 851 */
852kvp_done:
486 853
487 incoming_cn_msg->id.idx = CN_KVP_IDX; 854 incoming_cn_msg->id.idx = CN_KVP_IDX;
488 incoming_cn_msg->id.val = CN_KVP_VAL; 855 incoming_cn_msg->id.val = CN_KVP_VAL;
489 incoming_cn_msg->seq = KVP_USER_SET;
490 incoming_cn_msg->ack = 0; 856 incoming_cn_msg->ack = 0;
491 incoming_cn_msg->len = sizeof(struct hv_ku_msg); 857 incoming_cn_msg->len = sizeof(struct hv_kvp_msg);
492 858
493 len = netlink_send(fd, incoming_cn_msg); 859 len = netlink_send(fd, incoming_cn_msg);
494 if (len < 0) { 860 if (len < 0) {