cc48747331dc6f3c6d161583499f5ca2d9ce5524
[petitboot] / discover / device-handler.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <mntent.h>
8 #include <locale.h>
9 #include <sys/stat.h>
10 #include <sys/wait.h>
11 #include <sys/mount.h>
12
13 #include <talloc/talloc.h>
14 #include <list/list.h>
15 #include <log/log.h>
16 #include <types/types.h>
17 #include <system/system.h>
18 #include <process/process.h>
19 #include <url/url.h>
20 #include <i18n/i18n.h>
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netdb.h>
25 #include <arpa/inet.h>
26
27 #include "device-handler.h"
28 #include "discover-server.h"
29 #include "devmapper.h"
30 #include "user-event.h"
31 #include "platform.h"
32 #include "event.h"
33 #include "parser.h"
34 #include "resource.h"
35 #include "paths.h"
36 #include "sysinfo.h"
37 #include "boot.h"
38 #include "udev.h"
39 #include "network.h"
40 #include "ipmi.h"
41
42 enum default_priority {
43         DEFAULT_PRIORITY_REMOTE         = 1,
44         DEFAULT_PRIORITY_LOCAL_FIRST    = 2,
45         DEFAULT_PRIORITY_LOCAL_LAST     = 0xfe,
46         DEFAULT_PRIORITY_DISABLED       = 0xff,
47 };
48
49 struct device_handler {
50         struct discover_server  *server;
51         int                     dry_run;
52
53         struct pb_udev          *udev;
54         struct network          *network;
55         struct user_event       *user_event;
56
57         struct discover_device  **devices;
58         unsigned int            n_devices;
59
60         struct ramdisk_device   **ramdisks;
61         unsigned int            n_ramdisks;
62
63         struct waitset          *waitset;
64         struct waiter           *timeout_waiter;
65         bool                    autoboot_enabled;
66         unsigned int            sec_to_boot;
67
68         struct discover_boot_option *default_boot_option;
69         int                     default_boot_option_priority;
70
71         struct list             unresolved_boot_options;
72
73         struct boot_task        *pending_boot;
74         bool                    pending_boot_is_default;
75 };
76
77 static int mount_device(struct discover_device *dev);
78 static int umount_device(struct discover_device *dev);
79
80 static int device_handler_init_sources(struct device_handler *handler);
81 static void device_handler_reinit_sources(struct device_handler *handler);
82
83 static void device_handler_update_lang(const char *lang);
84
85 void discover_context_add_boot_option(struct discover_context *ctx,
86                 struct discover_boot_option *boot_option)
87 {
88         boot_option->source = ctx->parser;
89         list_add_tail(&ctx->boot_options, &boot_option->list);
90         talloc_steal(ctx, boot_option);
91 }
92
93 /**
94  * device_handler_get_device_count - Get the count of current handler devices.
95  */
96
97 int device_handler_get_device_count(const struct device_handler *handler)
98 {
99         return handler->n_devices;
100 }
101
102 /**
103  * device_handler_get_device - Get a handler device by index.
104  */
105
106 const struct discover_device *device_handler_get_device(
107         const struct device_handler *handler, unsigned int index)
108 {
109         if (index >= handler->n_devices) {
110                 assert(0 && "bad index");
111                 return NULL;
112         }
113
114         return handler->devices[index];
115 }
116
117 struct discover_boot_option *discover_boot_option_create(
118                 struct discover_context *ctx,
119                 struct discover_device *device)
120 {
121         struct discover_boot_option *opt;
122
123         opt = talloc_zero(ctx, struct discover_boot_option);
124         opt->option = talloc_zero(opt, struct boot_option);
125         opt->device = device;
126
127         return opt;
128 }
129
130 static int device_match_uuid(struct discover_device *dev, const char *uuid)
131 {
132         return dev->uuid && !strcmp(dev->uuid, uuid);
133 }
134
135 static int device_match_label(struct discover_device *dev, const char *label)
136 {
137         return dev->label && !strcmp(dev->label, label);
138 }
139
140 static int device_match_id(struct discover_device *dev, const char *id)
141 {
142         return !strcmp(dev->device->id, id);
143 }
144
145 static int device_match_serial(struct discover_device *dev, const char *serial)
146 {
147         const char *val = discover_device_get_param(dev, "ID_SERIAL");
148         return val && !strcmp(val, serial);
149 }
150
151 static struct discover_device *device_lookup(
152                 struct device_handler *device_handler,
153                 int (match_fn)(struct discover_device *, const char *),
154                 const char *str)
155 {
156         struct discover_device *dev;
157         unsigned int i;
158
159         if (!str)
160                 return NULL;
161
162         for (i = 0; i < device_handler->n_devices; i++) {
163                 dev = device_handler->devices[i];
164
165                 if (match_fn(dev, str))
166                         return dev;
167         }
168
169         return NULL;
170 }
171
172 struct discover_device *device_lookup_by_name(struct device_handler *handler,
173                 const char *name)
174 {
175         if (!strncmp(name, "/dev/", strlen("/dev/")))
176                 name += strlen("/dev/");
177
178         return device_lookup_by_id(handler, name);
179 }
180
181 struct discover_device *device_lookup_by_uuid(
182                 struct device_handler *device_handler,
183                 const char *uuid)
184 {
185         return device_lookup(device_handler, device_match_uuid, uuid);
186 }
187
188 struct discover_device *device_lookup_by_label(
189                 struct device_handler *device_handler,
190                 const char *label)
191 {
192         return device_lookup(device_handler, device_match_label, label);
193 }
194
195 struct discover_device *device_lookup_by_id(
196                 struct device_handler *device_handler,
197                 const char *id)
198 {
199         return device_lookup(device_handler, device_match_id, id);
200 }
201
202 struct discover_device *device_lookup_by_serial(
203                 struct device_handler *device_handler,
204                 const char *serial)
205 {
206         return device_lookup(device_handler, device_match_serial, serial);
207 }
208
209 void device_handler_destroy(struct device_handler *handler)
210 {
211         talloc_free(handler);
212 }
213
214 static int destroy_device(void *arg)
215 {
216         struct discover_device *dev = arg;
217
218         umount_device(dev);
219
220         return 0;
221 }
222
223 struct discover_device *discover_device_create(struct device_handler *handler,
224                 const char *id)
225 {
226         struct discover_device *dev;
227
228         dev = device_lookup_by_id(handler, id);
229         if (dev)
230                 return dev;
231
232         dev = talloc_zero(handler, struct discover_device);
233         dev->device = talloc_zero(dev, struct device);
234         dev->device->id = talloc_strdup(dev->device, id);
235         list_init(&dev->params);
236         list_init(&dev->boot_options);
237
238         talloc_set_destructor(dev, destroy_device);
239
240         return dev;
241 }
242
243 struct discover_device_param {
244         char                    *name;
245         char                    *value;
246         struct list_item        list;
247 };
248
249 void discover_device_set_param(struct discover_device *device,
250                 const char *name, const char *value)
251 {
252         struct discover_device_param *param;
253         bool found = false;
254
255         list_for_each_entry(&device->params, param, list) {
256                 if (!strcmp(param->name, name)) {
257                         found = true;
258                         break;
259                 }
260         }
261
262         if (!found) {
263                 if (!value)
264                         return;
265                 param = talloc(device, struct discover_device_param);
266                 param->name = talloc_strdup(param, name);
267                 list_add(&device->params, &param->list);
268         } else {
269                 if (!value) {
270                         list_remove(&param->list);
271                         talloc_free(param);
272                         return;
273                 }
274                 talloc_free(param->value);
275         }
276
277         param->value = talloc_strdup(param, value);
278 }
279
280 const char *discover_device_get_param(struct discover_device *device,
281                 const char *name)
282 {
283         struct discover_device_param *param;
284
285         list_for_each_entry(&device->params, param, list) {
286                 if (!strcmp(param->name, name))
287                         return param->value;
288         }
289         return NULL;
290 }
291
292 struct device_handler *device_handler_init(struct discover_server *server,
293                 struct waitset *waitset, int dry_run)
294 {
295         struct device_handler *handler;
296         int rc;
297
298         handler = talloc_zero(NULL, struct device_handler);
299         handler->server = server;
300         handler->waitset = waitset;
301         handler->dry_run = dry_run;
302         handler->autoboot_enabled = config_get()->autoboot_enabled;
303
304         list_init(&handler->unresolved_boot_options);
305
306         /* set up our mount point base */
307         pb_mkdir_recursive(mount_base());
308
309         parser_init();
310
311         if (config_get()->safe_mode)
312                 return handler;
313
314         rc = device_handler_init_sources(handler);
315         if (rc) {
316                 talloc_free(handler);
317                 return NULL;
318         }
319
320         return handler;
321 }
322
323 void device_handler_reinit(struct device_handler *handler)
324 {
325         struct discover_boot_option *opt, *tmp;
326         struct ramdisk_device *ramdisk;
327         unsigned int i;
328
329         device_handler_cancel_default(handler);
330
331         /* free unresolved boot options */
332         list_for_each_entry_safe(&handler->unresolved_boot_options,
333                         opt, tmp, list)
334                 talloc_free(opt);
335         list_init(&handler->unresolved_boot_options);
336
337         /* drop all devices */
338         for (i = 0; i < handler->n_devices; i++) {
339                 discover_server_notify_device_remove(handler->server,
340                                 handler->devices[i]->device);
341                 ramdisk = handler->devices[i]->ramdisk;
342                 talloc_free(handler->devices[i]);
343                 talloc_free(ramdisk);
344         }
345
346         talloc_free(handler->devices);
347         handler->devices = NULL;
348         handler->n_devices = 0;
349         talloc_free(handler->ramdisks);
350         handler->ramdisks = NULL;
351         handler->n_ramdisks = 0;
352
353         device_handler_reinit_sources(handler);
354 }
355
356 void device_handler_remove(struct device_handler *handler,
357                 struct discover_device *device)
358 {
359         struct discover_boot_option *opt, *tmp;
360         unsigned int i;
361
362         for (i = 0; i < handler->n_devices; i++)
363                 if (handler->devices[i] == device)
364                         break;
365
366         if (i == handler->n_devices) {
367                 talloc_free(device);
368                 return;
369         }
370
371         /* Free any unresolved options, as they're currently allocated
372          * against the handler */
373         list_for_each_entry_safe(&handler->unresolved_boot_options,
374                         opt, tmp, list) {
375                 if (opt->device != device)
376                         continue;
377                 list_remove(&opt->list);
378                 talloc_free(opt);
379         }
380
381         /* if this is a network device, we have to unregister it from the
382          * network code */
383         if (device->device->type == DEVICE_TYPE_NETWORK)
384                 network_unregister_device(handler->network, device);
385
386         handler->n_devices--;
387         memmove(&handler->devices[i], &handler->devices[i + 1],
388                 (handler->n_devices - i) * sizeof(handler->devices[0]));
389         handler->devices = talloc_realloc(handler, handler->devices,
390                 struct discover_device *, handler->n_devices);
391
392         if (device->notified)
393                 discover_server_notify_device_remove(handler->server,
394                                                         device->device);
395
396         talloc_free(device);
397 }
398
399 static void boot_status(void *arg, struct boot_status *status)
400 {
401         struct device_handler *handler = arg;
402
403         discover_server_notify_boot_status(handler->server, status);
404 }
405
406 static void countdown_status(struct device_handler *handler,
407                 struct discover_boot_option *opt, unsigned int sec)
408 {
409         struct boot_status status;
410
411         status.type = BOOT_STATUS_INFO;
412         status.progress = -1;
413         status.detail = NULL;
414         status.message = talloc_asprintf(handler,
415                         _("Booting in %d sec: %s"), sec, opt->option->name);
416
417         discover_server_notify_boot_status(handler->server, &status);
418
419         talloc_free(status.message);
420 }
421
422 static int default_timeout(void *arg)
423 {
424         struct device_handler *handler = arg;
425         struct discover_boot_option *opt;
426
427         if (!handler->default_boot_option)
428                 return 0;
429
430         if (handler->pending_boot)
431                 return 0;
432
433         opt = handler->default_boot_option;
434
435         if (handler->sec_to_boot) {
436                 countdown_status(handler, opt, handler->sec_to_boot);
437                 handler->sec_to_boot--;
438                 handler->timeout_waiter = waiter_register_timeout(
439                                                 handler->waitset, 1000,
440                                                 default_timeout, handler);
441                 return 0;
442         }
443
444         handler->timeout_waiter = NULL;
445
446         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
447
448         platform_pre_boot();
449
450         handler->pending_boot = boot(handler, handler->default_boot_option,
451                         NULL, handler->dry_run, boot_status, handler);
452         handler->pending_boot_is_default = true;
453         return 0;
454 }
455
456 struct {
457         enum ipmi_bootdev       ipmi_type;
458         enum device_type        device_type;
459 } device_type_map[] = {
460         { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
461         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
462         { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
463         { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
464 };
465
466 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
467                 enum device_type device_type)
468 {
469         unsigned int i;
470
471         for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
472                 if (device_type_map[i].device_type == device_type)
473                         return device_type_map[i].ipmi_type == ipmi_type;
474         }
475
476         return false;
477 }
478
479 static int autoboot_option_priority(const struct config *config,
480                                 struct discover_boot_option *opt)
481 {
482         enum device_type type = opt->device->device->type;
483         const char *uuid = opt->device->uuid;
484         struct autoboot_option *auto_opt;
485         unsigned int i;
486
487         for (i = 0; i < config->n_autoboot_opts; i++) {
488                 auto_opt = &config->autoboot_opts[i];
489                 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
490                         if (!strcmp(auto_opt->uuid, uuid))
491                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
492
493                 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
494                         if (auto_opt->type == type ||
495                             auto_opt->type == DEVICE_TYPE_ANY)
496                                 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
497         }
498
499         return -1;
500 }
501
502 /*
503  * We have different priorities to resolve conflicts between boot options that
504  * report to be the default for their device. This function assigns a priority
505  * for these options.
506  */
507 static enum default_priority default_option_priority(
508                 struct discover_boot_option *opt)
509 {
510         const struct config *config;
511
512         config = config_get();
513
514         /* We give highest priority to IPMI-configured boot options. If
515          * we have an IPMI bootdev configuration set, then we don't allow
516          * any other defaults */
517         if (config->ipmi_bootdev) {
518                 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
519                                 opt->device->device->type);
520                 if (ipmi_match)
521                         return DEFAULT_PRIORITY_REMOTE;
522
523                 pb_debug("handler: disabled default priority due to "
524                                 "non-matching IPMI type %x\n",
525                                 config->ipmi_bootdev);
526                 return DEFAULT_PRIORITY_DISABLED;
527         }
528
529         /* Next, try to match the option against the user-defined autoboot
530          * options, either by device UUID or type. */
531         if (config->n_autoboot_opts) {
532                 int boot_match = autoboot_option_priority(config, opt);
533                 if (boot_match > 0)
534                         return boot_match;
535         }
536
537         /* If the option didn't match any entry in the array, it is disabled */
538         pb_debug("handler: disabled default priority due to "
539                         "non-matching UUID or type\n");
540         return DEFAULT_PRIORITY_DISABLED;
541 }
542
543 static void set_default(struct device_handler *handler,
544                 struct discover_boot_option *opt)
545 {
546         enum default_priority cur_prio, new_prio;
547
548         if (!handler->autoboot_enabled)
549                 return;
550
551         pb_debug("handler: new default option: %s\n", opt->option->id);
552
553         new_prio = default_option_priority(opt);
554
555         /* Anything outside our range prevents a default boot */
556         if (new_prio >= DEFAULT_PRIORITY_DISABLED)
557                 return;
558
559         pb_debug("handler: calculated priority %d\n", new_prio);
560
561         /* Resolve any conflicts: if we have a new default option, it only
562          * replaces the current if it has a higher priority. */
563         if (handler->default_boot_option) {
564
565                 cur_prio = handler->default_boot_option_priority;
566
567                 if (new_prio < cur_prio) {
568                         pb_log("handler: new prio %d beats "
569                                         "old prio %d for %s\n",
570                                         new_prio, cur_prio,
571                                         handler->default_boot_option
572                                                 ->option->id);
573                         handler->default_boot_option = opt;
574                         handler->default_boot_option_priority = new_prio;
575                         /* extend the timeout a little, so the user sees some
576                          * indication of the change */
577                         handler->sec_to_boot += 2;
578                 }
579
580                 return;
581         }
582
583         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
584         handler->default_boot_option = opt;
585         handler->default_boot_option_priority = new_prio;
586
587         pb_log("handler: boot option %s set as default, timeout %u sec.\n",
588                opt->option->id, handler->sec_to_boot);
589
590         default_timeout(handler);
591 }
592
593 static bool resource_is_resolved(struct resource *res)
594 {
595         return !res || res->resolved;
596 }
597
598 /* We only use this in an assert, which will disappear if we're compiling
599  * with NDEBUG, so we need the 'used' attribute for these builds */
600 static bool __attribute__((used)) boot_option_is_resolved(
601                 struct discover_boot_option *opt)
602 {
603         return resource_is_resolved(opt->boot_image) &&
604                 resource_is_resolved(opt->initrd) &&
605                 resource_is_resolved(opt->dtb) &&
606                 resource_is_resolved(opt->icon);
607 }
608
609 static bool resource_resolve(struct resource *res, const char *name,
610                 struct discover_boot_option *opt,
611                 struct device_handler *handler)
612 {
613         struct parser *parser = opt->source;
614
615         if (resource_is_resolved(res))
616                 return true;
617
618         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
619                         opt->option->id, name, parser->name);
620         parser->resolve_resource(handler, res);
621
622         return res->resolved;
623 }
624
625 static bool boot_option_resolve(struct discover_boot_option *opt,
626                 struct device_handler *handler)
627 {
628         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
629                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
630                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
631                 resource_resolve(opt->icon, "icon", opt, handler);
632 }
633
634 static void boot_option_finalise(struct device_handler *handler,
635                 struct discover_boot_option *opt)
636 {
637         assert(boot_option_is_resolved(opt));
638
639         /* check that the parsers haven't set any of the final data */
640         assert(!opt->option->boot_image_file);
641         assert(!opt->option->initrd_file);
642         assert(!opt->option->dtb_file);
643         assert(!opt->option->icon_file);
644         assert(!opt->option->device_id);
645
646         if (opt->boot_image)
647                 opt->option->boot_image_file = opt->boot_image->url->full;
648         if (opt->initrd)
649                 opt->option->initrd_file = opt->initrd->url->full;
650         if (opt->dtb)
651                 opt->option->dtb_file = opt->dtb->url->full;
652         if (opt->icon)
653                 opt->option->icon_file = opt->icon->url->full;
654
655         opt->option->device_id = opt->device->device->id;
656
657         if (opt->option->is_default)
658                 set_default(handler, opt);
659 }
660
661 static void notify_boot_option(struct device_handler *handler,
662                 struct discover_boot_option *opt)
663 {
664         struct discover_device *dev = opt->device;
665
666         if (!dev->notified)
667                 discover_server_notify_device_add(handler->server,
668                                                   opt->device->device);
669         dev->notified = true;
670         discover_server_notify_boot_option_add(handler->server, opt->option);
671 }
672
673 static void process_boot_option_queue(struct device_handler *handler)
674 {
675         struct discover_boot_option *opt, *tmp;
676
677         list_for_each_entry_safe(&handler->unresolved_boot_options,
678                         opt, tmp, list) {
679
680                 pb_debug("queue: attempting resolution for %s\n",
681                                 opt->option->id);
682
683                 if (!boot_option_resolve(opt, handler))
684                         continue;
685
686                 pb_debug("\tresolved!\n");
687
688                 list_remove(&opt->list);
689                 list_add_tail(&opt->device->boot_options, &opt->list);
690                 talloc_steal(opt->device, opt);
691                 boot_option_finalise(handler, opt);
692                 notify_boot_option(handler, opt);
693         }
694 }
695
696 struct discover_context *device_handler_discover_context_create(
697                 struct device_handler *handler,
698                 struct discover_device *device)
699 {
700         struct discover_context *ctx;
701
702         ctx = talloc_zero(handler, struct discover_context);
703         ctx->device = device;
704         ctx->network = handler->network;
705         list_init(&ctx->boot_options);
706
707         return ctx;
708 }
709
710 /**
711  * context_commit - Commit a temporary discovery context to the handler,
712  * and notify the clients about any new options / devices
713  */
714 void device_handler_discover_context_commit(struct device_handler *handler,
715                 struct discover_context *ctx)
716 {
717         struct discover_device *dev = ctx->device;
718         struct discover_boot_option *opt, *tmp;
719
720         if (!device_lookup_by_id(handler, dev->device->id))
721                 device_handler_add_device(handler, dev);
722
723         /* move boot options from the context to the device */
724         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
725                 list_remove(&opt->list);
726
727                 if (boot_option_resolve(opt, handler)) {
728                         pb_log("boot option %s is resolved, "
729                                         "sending to clients\n",
730                                         opt->option->id);
731                         list_add_tail(&dev->boot_options, &opt->list);
732                         talloc_steal(dev, opt);
733                         boot_option_finalise(handler, opt);
734                         notify_boot_option(handler, opt);
735                 } else {
736                         if (!opt->source->resolve_resource) {
737                                 pb_log("parser %s gave us an unresolved "
738                                         "resource (%s), but no way to "
739                                         "resolve it\n",
740                                         opt->source->name, opt->option->id);
741                                 talloc_free(opt);
742                         } else {
743                                 pb_log("boot option %s is unresolved, "
744                                                 "adding to queue\n",
745                                                 opt->option->id);
746                                 list_add(&handler->unresolved_boot_options,
747                                                 &opt->list);
748                                 talloc_steal(handler, opt);
749                         }
750                 }
751         }
752 }
753
754 void device_handler_add_device(struct device_handler *handler,
755                 struct discover_device *device)
756 {
757         handler->n_devices++;
758         handler->devices = talloc_realloc(handler, handler->devices,
759                                 struct discover_device *, handler->n_devices);
760         handler->devices[handler->n_devices - 1] = device;
761
762         if (device->device->type == DEVICE_TYPE_NETWORK)
763                 network_register_device(handler->network, device);
764 }
765
766 void device_handler_add_ramdisk(struct device_handler *handler,
767                 const char *path)
768 {
769         struct ramdisk_device *dev;
770         unsigned int i;
771
772         if (!path)
773                 return;
774
775         for (i = 0; i < handler->n_ramdisks; i++)
776                 if (!strcmp(handler->ramdisks[i]->path, path))
777                         return;
778
779         dev = talloc_zero(handler, struct ramdisk_device);
780         if (!dev) {
781                 pb_log("Failed to allocate memory to track %s\n", path);
782                 return;
783         }
784
785         dev->path = talloc_strdup(handler, path);
786
787         handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
788                                 struct ramdisk_device *,
789                                 handler->n_ramdisks + 1);
790         if (!handler->ramdisks) {
791                 pb_log("Failed to reallocate memory"
792                        "- ramdisk tracking inconsistent!\n");
793                 return;
794         }
795
796         handler->ramdisks[i] = dev;
797         i = handler->n_ramdisks++;
798 }
799
800 struct ramdisk_device *device_handler_get_ramdisk(
801                 struct device_handler *handler)
802 {
803         unsigned int i;
804         char *name;
805         dev_t id;
806
807         /* Check if free ramdisk exists */
808         for (i = 0; i < handler->n_ramdisks; i++)
809                 if (!handler->ramdisks[i]->snapshot &&
810                     !handler->ramdisks[i]->origin &&
811                     !handler->ramdisks[i]->base)
812                         return handler->ramdisks[i];
813
814         /* Otherwise create a new one */
815         name = talloc_asprintf(handler, "/dev/ram%d",
816                         handler->n_ramdisks);
817         if (!name) {
818                 pb_debug("Failed to allocate memory to name /dev/ram%d",
819                         handler->n_ramdisks);
820                 return NULL;
821         }
822
823         id = makedev(1, handler->n_ramdisks);
824         if (mknod(name, S_IFBLK, id)) {
825                 if (errno == EEXIST) {
826                         /* We haven't yet received updates for existing
827                          * ramdisks - add and use this one */
828                         pb_debug("Using untracked ramdisk %s\n", name);
829                 } else {
830                         pb_log("Failed to create new ramdisk %s: %s\n",
831                                name, strerror(errno));
832                         return NULL;
833                 }
834         }
835         device_handler_add_ramdisk(handler, name);
836         talloc_free(name);
837
838         return handler->ramdisks[i];
839 }
840
841 void device_handler_release_ramdisk(struct discover_device *device)
842 {
843         struct ramdisk_device *ramdisk = device->ramdisk;
844
845         talloc_free(ramdisk->snapshot);
846         talloc_free(ramdisk->origin);
847         talloc_free(ramdisk->base);
848
849         ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
850         ramdisk->sectors = 0;
851
852         device->ramdisk = NULL;
853 }
854
855 /* Start discovery on a hotplugged device. The device will be in our devices
856  * array, but has only just been initialised by the hotplug source.
857  */
858 int device_handler_discover(struct device_handler *handler,
859                 struct discover_device *dev)
860 {
861         struct discover_context *ctx;
862         struct boot_status *status;
863         int rc;
864
865         status = talloc_zero(handler, struct boot_status);
866         status->type = BOOT_STATUS_INFO;
867         /*
868          * TRANSLATORS: this string will be passed the type and identifier
869          * of the device. For example, the first parameter could be "Disk",
870          * (which will be translated accordingly) and the second a Linux device
871          * identifier like 'sda1' (which will not be translated)
872          */
873         status->message = talloc_asprintf(status, _("Processing %s device %s"),
874                                 device_type_display_name(dev->device->type),
875                                 dev->device->id);
876         boot_status(handler, status);
877
878         process_boot_option_queue(handler);
879
880         /* create our context */
881         ctx = device_handler_discover_context_create(handler, dev);
882
883         rc = mount_device(dev);
884         if (rc)
885                 goto out;
886
887         /* add this device to our system info */
888         system_info_register_blockdev(dev->device->id, dev->uuid,
889                         dev->mount_path);
890
891         /* run the parsers. This will populate the ctx's boot_option list. */
892         iterate_parsers(ctx);
893
894         /* add discovered stuff to the handler */
895         device_handler_discover_context_commit(handler, ctx);
896
897 out:
898         /*
899          * TRANSLATORS: the format specifier in this string is a Linux
900          * device identifier, like 'sda1'
901          */
902         status->message = talloc_asprintf(status,_("Processing %s complete"),
903                                 dev->device->id);
904         boot_status(handler, status);
905
906         talloc_free(status);
907         talloc_free(ctx);
908
909         return 0;
910 }
911
912 /* Incoming dhcp event */
913 int device_handler_dhcp(struct device_handler *handler,
914                 struct discover_device *dev, struct event *event)
915 {
916         struct discover_context *ctx;
917         struct boot_status *status;
918
919         status = talloc_zero(handler, struct boot_status);
920         status->type = BOOT_STATUS_INFO;
921         /*
922          * TRANSLATORS: this format specifier will be the name of a network
923          * device, like 'eth0'.
924          */
925         status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
926                                 dev->device->id);
927         boot_status(handler, status);
928
929         /* create our context */
930         ctx = device_handler_discover_context_create(handler, dev);
931         ctx->event = event;
932
933         iterate_parsers(ctx);
934
935         device_handler_discover_context_commit(handler, ctx);
936
937         /*
938          * TRANSLATORS: this format specifier will be the name of a network
939          * device, like 'eth0'.
940          */
941         status->message = talloc_asprintf(status,_("Processing %s complete"),
942                                 dev->device->id);
943         boot_status(handler, status);
944
945         talloc_free(status);
946         talloc_free(ctx);
947
948         return 0;
949 }
950
951 /* incoming conf event */
952 int device_handler_conf(struct device_handler *handler,
953                 struct discover_device *dev, struct pb_url *url)
954 {
955         struct discover_context *ctx;
956         struct boot_status *status;
957
958         status = talloc_zero(handler, struct boot_status);
959         status->type = BOOT_STATUS_INFO;
960         status->message = talloc_asprintf(status, _("Processing user config"));
961         boot_status(handler, status);
962
963         /* create our context */
964         ctx = device_handler_discover_context_create(handler, dev);
965         ctx->conf_url = url;
966
967         iterate_parsers(ctx);
968
969         device_handler_discover_context_commit(handler, ctx);
970
971         status->message = talloc_asprintf(status,
972                                 _("Processing user config complete"));
973         boot_status(handler, status);
974
975         talloc_free(status);
976         talloc_free(ctx);
977
978         return 0;
979 }
980
981 static struct discover_boot_option *find_boot_option_by_id(
982                 struct device_handler *handler, const char *id)
983 {
984         unsigned int i;
985
986         for (i = 0; i < handler->n_devices; i++) {
987                 struct discover_device *dev = handler->devices[i];
988                 struct discover_boot_option *opt;
989
990                 list_for_each_entry(&dev->boot_options, opt, list)
991                         if (!strcmp(opt->option->id, id))
992                                 return opt;
993         }
994
995         return NULL;
996 }
997
998 void device_handler_boot(struct device_handler *handler,
999                 struct boot_command *cmd)
1000 {
1001         struct discover_boot_option *opt = NULL;
1002
1003         if (cmd->option_id && strlen(cmd->option_id))
1004                 opt = find_boot_option_by_id(handler, cmd->option_id);
1005
1006         if (handler->pending_boot)
1007                 boot_cancel(handler->pending_boot);
1008
1009         platform_pre_boot();
1010
1011         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
1012                         boot_status, handler);
1013         handler->pending_boot_is_default = false;
1014 }
1015
1016 void device_handler_cancel_default(struct device_handler *handler)
1017 {
1018         struct boot_status status;
1019
1020         if (handler->timeout_waiter)
1021                 waiter_remove(handler->timeout_waiter);
1022
1023         handler->timeout_waiter = NULL;
1024         handler->autoboot_enabled = false;
1025
1026         /* we only send status if we had a default boot option queued */
1027         if (!handler->default_boot_option)
1028                 return;
1029
1030         pb_log("Cancelling default boot option\n");
1031
1032         if (handler->pending_boot && handler->pending_boot_is_default) {
1033                 boot_cancel(handler->pending_boot);
1034                 handler->pending_boot = NULL;
1035                 handler->pending_boot_is_default = false;
1036         }
1037
1038         handler->default_boot_option = NULL;
1039
1040         status.type = BOOT_STATUS_INFO;
1041         status.progress = -1;
1042         status.detail = NULL;
1043         status.message = _("Default boot cancelled");
1044
1045         discover_server_notify_boot_status(handler->server, &status);
1046 }
1047
1048 void device_handler_update_config(struct device_handler *handler,
1049                 struct config *config)
1050 {
1051         int rc;
1052
1053         rc = config_set(config);
1054         if (rc)
1055                 return;
1056
1057         discover_server_notify_config(handler->server, config);
1058         device_handler_update_lang(config->lang);
1059         device_handler_reinit(handler);
1060 }
1061
1062 static char *device_from_addr(void *ctx, struct pb_url *url)
1063 {
1064         char *ipaddr, *buf, *tok, *dev = NULL;
1065         const char *delim = " ";
1066         struct sockaddr_in *ip;
1067         struct sockaddr_in si;
1068         struct addrinfo *res;
1069         struct process *p;
1070         int rc;
1071
1072         /* Note: IPv4 only */
1073         rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1074         if (rc > 0) {
1075                 ipaddr = url->host;
1076         } else {
1077                 /* need to turn hostname into a valid IP */
1078                 rc = getaddrinfo(url->host, NULL, NULL, &res);
1079                 if (rc) {
1080                         pb_debug("%s: Invalid URL\n",__func__);
1081                         return NULL;
1082                 }
1083                 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1084                 ip = (struct sockaddr_in *) res->ai_addr;
1085                 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1086                 freeaddrinfo(res);
1087         }
1088
1089         const char *argv[] = {
1090                 pb_system_apps.ip,
1091                 "route", "show", "to", "match",
1092                 ipaddr,
1093                 NULL
1094         };
1095
1096         p = process_create(ctx);
1097
1098         p->path = pb_system_apps.ip;
1099         p->argv = argv;
1100         p->keep_stdout = true;
1101
1102         rc = process_run_sync(p);
1103
1104         if (rc || p->exit_status) {
1105                 /* ip has complained for some reason; most likely
1106                  * there is no route to the host - bail out */
1107                 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1108                 pb_debug("ip buf: %s\n", p->stdout_buf);
1109                 process_release(p);
1110                 return NULL;
1111         }
1112
1113         buf = p->stdout_buf;
1114         /* If a route is found, ip-route output will be of the form
1115          * "... dev DEVNAME ... " */
1116         tok = strtok(buf, delim);
1117         while (tok) {
1118                 if (!strcmp(tok, "dev")) {
1119                         tok = strtok(NULL, delim);
1120                         dev = talloc_strdup(ctx, tok);
1121                         break;
1122                 }
1123                 tok = strtok(NULL, delim);
1124         }
1125
1126         process_release(p);
1127         if (dev)
1128                 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1129         return dev;
1130 }
1131
1132 void device_handler_process_url(struct device_handler *handler,
1133                 const char *url, const char *mac, const char *ip)
1134 {
1135         struct discover_context *ctx;
1136         struct discover_device *dev;
1137         struct boot_status *status;
1138         struct pb_url *pb_url;
1139         struct event *event;
1140         struct param *param;
1141
1142         status = talloc(handler, struct boot_status);
1143
1144         status->type = BOOT_STATUS_ERROR;
1145         status->progress = 0;
1146         status->detail = talloc_asprintf(status,
1147                         _("Received config URL %s"), url);
1148
1149         if (!handler->network) {
1150                 status->message = talloc_asprintf(handler,
1151                                         _("No network configured"));
1152                 goto msg;
1153         }
1154
1155         event = talloc(handler, struct event);
1156         event->type = EVENT_TYPE_USER;
1157         event->action = EVENT_ACTION_CONF;
1158
1159         if (url[strlen(url) - 1] == '/') {
1160                 event->params = talloc_array(event, struct param, 3);
1161                 param = &event->params[0];
1162                 param->name = talloc_strdup(event, "pxepathprefix");
1163                 param->value = talloc_strdup(event, url);
1164                 param = &event->params[1];
1165                 param->name = talloc_strdup(event, "mac");
1166                 param->value = talloc_strdup(event, mac);
1167                 param = &event->params[2];
1168                 param->name = talloc_strdup(event, "ip");
1169                 param->value = talloc_strdup(event, ip);
1170                 event->n_params = 3;
1171         } else {
1172                 event->params = talloc_array(event, struct param, 1);
1173                 param = &event->params[0];
1174                 param->name = talloc_strdup(event, "pxeconffile");
1175                 param->value = talloc_strdup(event, url);
1176                 event->n_params = 1;
1177         }
1178
1179         pb_url = pb_url_parse(event, event->params->value);
1180         if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1181                 status->message = talloc_asprintf(handler,
1182                                         _("Invalid config URL!"));
1183                 goto msg;
1184         }
1185
1186         if (pb_url->scheme == pb_url_file)
1187                 event->device = talloc_asprintf(event, "local");
1188         else
1189                 event->device = device_from_addr(event, pb_url);
1190
1191         if (!event->device) {
1192                 status->message = talloc_asprintf(status,
1193                                         _("Unable to route to host %s"),
1194                                         pb_url->host);
1195                 goto msg;
1196         }
1197
1198         dev = discover_device_create(handler, event->device);
1199         if (pb_url->scheme == pb_url_file)
1200                 dev->device->type = DEVICE_TYPE_ANY;
1201         ctx = device_handler_discover_context_create(handler, dev);
1202         ctx->event = event;
1203
1204         iterate_parsers(ctx);
1205
1206         device_handler_discover_context_commit(handler, ctx);
1207
1208         talloc_free(ctx);
1209
1210         status->type = BOOT_STATUS_INFO;
1211         status->message = talloc_asprintf(status, _("Config file %s parsed"),
1212                                         pb_url->file);
1213 msg:
1214         boot_status(handler, status);
1215         talloc_free(status);
1216 }
1217
1218 #ifndef PETITBOOT_TEST
1219
1220 static void device_handler_update_lang(const char *lang)
1221 {
1222         const char *cur_lang;
1223
1224         if (!lang)
1225                 return;
1226
1227         cur_lang = setlocale(LC_ALL, NULL);
1228         if (cur_lang && !strcmp(cur_lang, lang))
1229                 return;
1230
1231         setlocale(LC_ALL, lang);
1232 }
1233
1234 static int device_handler_init_sources(struct device_handler *handler)
1235 {
1236         /* init our device sources: udev, network and user events */
1237         handler->udev = udev_init(handler, handler->waitset);
1238         if (!handler->udev)
1239                 return -1;
1240
1241         handler->network = network_init(handler, handler->waitset,
1242                         handler->dry_run);
1243         if (!handler->network)
1244                 return -1;
1245
1246         handler->user_event = user_event_init(handler, handler->waitset);
1247         if (!handler->user_event)
1248                 return -1;
1249
1250         return 0;
1251 }
1252
1253 static void device_handler_reinit_sources(struct device_handler *handler)
1254 {
1255         /* if we haven't initialised sources previously (becuase we started in
1256          * safe mode), then init once here. */
1257         if (!(handler->udev || handler->network || handler->user_event)) {
1258                 device_handler_init_sources(handler);
1259                 return;
1260         }
1261
1262         udev_reinit(handler->udev);
1263
1264         network_shutdown(handler->network);
1265         handler->network = network_init(handler, handler->waitset,
1266                         handler->dry_run);
1267 }
1268
1269 static inline const char *get_device_path(struct discover_device *dev)
1270 {
1271         return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1272 }
1273
1274 static char *check_subvols(struct discover_device *dev)
1275 {
1276         const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1277         struct stat sb;
1278         char *path;
1279         int rc;
1280
1281         if (strncmp(fstype, "btrfs", strlen("btrfs")))
1282                 return dev->mount_path;
1283
1284         /* On btrfs a device's root may be under a subvolume path */
1285         path = join_paths(dev, dev->mount_path, "@");
1286         rc = stat(path, &sb);
1287         if (!rc && S_ISDIR(sb.st_mode)) {
1288                 pb_debug("Using '%s' for btrfs root path\n", path);
1289                 return path;
1290         }
1291
1292         talloc_free(path);
1293         return dev->mount_path;
1294 }
1295
1296 static bool check_existing_mount(struct discover_device *dev)
1297 {
1298         struct stat devstat, mntstat;
1299         const char *device_path;
1300         struct mntent *mnt;
1301         FILE *fp;
1302         int rc;
1303
1304         device_path = get_device_path(dev);
1305
1306         rc = stat(device_path, &devstat);
1307         if (rc) {
1308                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1309                 return false;
1310         }
1311
1312         if (!S_ISBLK(devstat.st_mode)) {
1313                 pb_debug("%s: %s isn't a block device?\n", __func__,
1314                                 dev->device_path);
1315                 return false;
1316         }
1317
1318         fp = fopen("/proc/self/mounts", "r");
1319
1320         for (;;) {
1321                 mnt = getmntent(fp);
1322                 if (!mnt)
1323                         break;
1324
1325                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1326                         continue;
1327
1328                 rc = stat(mnt->mnt_fsname, &mntstat);
1329                 if (rc)
1330                         continue;
1331
1332                 if (!S_ISBLK(mntstat.st_mode))
1333                         continue;
1334
1335                 if (mntstat.st_rdev == devstat.st_rdev) {
1336                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1337                         dev->root_path = check_subvols(dev);
1338                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
1339                         dev->mounted = true;
1340                         dev->unmount = false;
1341
1342                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
1343                                         __func__, dev->device_path,
1344                                         dev->mounted_rw ? 'w' : 'o',
1345                                         mnt->mnt_dir);
1346                         break;
1347                 }
1348         }
1349
1350         fclose(fp);
1351
1352         return mnt != NULL;
1353 }
1354
1355 /*
1356  * Attempt to mount a filesystem safely, while handling certain filesytem-
1357  * specific options
1358  */
1359 static int try_mount(const char *device_path, const char *mount_path,
1360                              const char *fstype, unsigned long flags,
1361                              bool have_snapshot)
1362 {
1363         const char *fs, *safe_opts;
1364         int rc;
1365
1366         /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1367         if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1368                 pb_debug("Mounting ext3 filesystem as ext4\n");
1369                 fs = "ext4";
1370         } else
1371                 fs = fstype;
1372
1373         if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1374             strncmp(fs, "ext4", strlen("ext4")) == 0)
1375                 safe_opts = "norecovery";
1376         else
1377                 safe_opts = NULL;
1378
1379         errno = 0;
1380         /* If no snapshot is available don't attempt recovery */
1381         if (!have_snapshot)
1382                 return mount(device_path, mount_path, fs, flags, safe_opts);
1383
1384         rc = mount(device_path, mount_path, fs, flags, NULL);
1385
1386         if (!rc)
1387                 return rc;
1388
1389         /* Mounting failed; some filesystems will fail to mount if a recovery
1390          * journal exists (eg. cross-endian XFS), so try again with norecovery
1391          * where that option is available.
1392          * If mounting read-write just return the error as norecovery is not a
1393          * valid option */
1394         if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1395                 return rc;
1396
1397         errno = 0;
1398         return mount(device_path, mount_path, fs, flags, safe_opts);
1399 }
1400
1401 static int mount_device(struct discover_device *dev)
1402 {
1403         const char *fstype, *device_path;
1404         int rc;
1405
1406         if (!dev->device_path)
1407                 return -1;
1408
1409         if (dev->mounted)
1410                 return 0;
1411
1412         if (check_existing_mount(dev))
1413                 return 0;
1414
1415         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1416         if (!fstype)
1417                 return 0;
1418
1419         dev->mount_path = join_paths(dev, mount_base(),
1420                                         dev->device_path);
1421
1422         if (pb_mkdir_recursive(dev->mount_path)) {
1423                 pb_log("couldn't create mount directory %s: %s\n",
1424                                 dev->mount_path, strerror(errno));
1425                 goto err_free;
1426         }
1427
1428         device_path = get_device_path(dev);
1429
1430         pb_log("mounting device %s read-only\n", dev->device_path);
1431         rc = try_mount(device_path, dev->mount_path, fstype,
1432                        MS_RDONLY | MS_SILENT, dev->ramdisk);
1433
1434         if (!rc) {
1435                 dev->mounted = true;
1436                 dev->mounted_rw = false;
1437                 dev->unmount = true;
1438                 dev->root_path = check_subvols(dev);
1439                 return 0;
1440         }
1441
1442         pb_log("couldn't mount device %s: mount failed: %s\n",
1443                         device_path, strerror(errno));
1444
1445         /* If mount fails clean up any snapshot */
1446         devmapper_destroy_snapshot(dev);
1447
1448         pb_rmdir_recursive(mount_base(), dev->mount_path);
1449 err_free:
1450         talloc_free(dev->mount_path);
1451         dev->mount_path = NULL;
1452         return -1;
1453 }
1454
1455 static int umount_device(struct discover_device *dev)
1456 {
1457         const char *device_path;
1458         int rc;
1459
1460         if (!dev->mounted || !dev->unmount)
1461                 return 0;
1462
1463         device_path = get_device_path(dev);
1464
1465         pb_log("unmounting device %s\n", device_path);
1466         rc = umount(dev->mount_path);
1467         if (rc)
1468                 return -1;
1469
1470         dev->mounted = false;
1471         devmapper_destroy_snapshot(dev);
1472
1473         pb_rmdir_recursive(mount_base(), dev->mount_path);
1474
1475         talloc_free(dev->mount_path);
1476         dev->mount_path = NULL;
1477         dev->root_path = NULL;
1478
1479         return 0;
1480 }
1481
1482 int device_request_write(struct discover_device *dev, bool *release)
1483 {
1484         const char *fstype, *device_path;
1485         const struct config *config;
1486         int rc;
1487
1488         *release = false;
1489
1490         config = config_get();
1491         if (!config->allow_writes)
1492                 return -1;
1493
1494         if (!dev->mounted)
1495                 return -1;
1496
1497         if (dev->mounted_rw)
1498                 return 0;
1499
1500         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1501
1502         device_path = get_device_path(dev);
1503
1504         pb_log("remounting device %s read-write\n", device_path);
1505
1506         rc = umount(dev->mount_path);
1507         if (rc) {
1508                 pb_log("Failed to unmount %s: %s\n",
1509                        dev->mount_path, strerror(errno));
1510                 return -1;
1511         }
1512
1513         rc = try_mount(device_path, dev->mount_path, fstype,
1514                        MS_SILENT, dev->ramdisk);
1515         if (rc)
1516                 goto mount_ro;
1517
1518         dev->mounted_rw = true;
1519         *release = true;
1520         return 0;
1521
1522 mount_ro:
1523         pb_log("Unable to remount device %s read-write: %s\n",
1524                device_path, strerror(errno));
1525         rc = try_mount(device_path, dev->mount_path, fstype,
1526                        MS_RDONLY | MS_SILENT, dev->ramdisk);
1527         if (rc)
1528                 pb_log("Unable to recover mount for %s: %s\n",
1529                        device_path, strerror(errno));
1530         return -1;
1531 }
1532
1533 void device_release_write(struct discover_device *dev, bool release)
1534 {
1535         const char *fstype, *device_path;
1536
1537         if (!release)
1538                 return;
1539
1540         device_path = get_device_path(dev);
1541
1542         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1543
1544         pb_log("remounting device %s read-only\n", device_path);
1545
1546         if (umount(dev->mount_path)) {
1547                 pb_log("Failed to unmount %s\n", dev->mount_path);
1548                 return;
1549         }
1550         dev->mounted_rw = dev->mounted = false;
1551
1552         if (dev->ramdisk) {
1553                 devmapper_merge_snapshot(dev);
1554                 /* device_path becomes stale after merge */
1555                 device_path = get_device_path(dev);
1556         }
1557
1558         if (try_mount(device_path, dev->mount_path, fstype,
1559                        MS_RDONLY | MS_SILENT, dev->ramdisk))
1560                 pb_log("Failed to remount %s read-only: %s\n",
1561                        device_path, strerror(errno));
1562         else
1563                 dev->mounted = true;
1564 }
1565
1566 #else
1567
1568 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1569 {
1570 }
1571
1572 static int device_handler_init_sources(
1573                 struct device_handler *handler __attribute__((unused)))
1574 {
1575         return 0;
1576 }
1577
1578 static void device_handler_reinit_sources(
1579                 struct device_handler *handler __attribute__((unused)))
1580 {
1581 }
1582
1583 static int umount_device(struct discover_device *dev __attribute__((unused)))
1584 {
1585         return 0;
1586 }
1587
1588 static int __attribute__((unused)) mount_device(
1589                 struct discover_device *dev __attribute__((unused)))
1590 {
1591         return 0;
1592 }
1593
1594 int device_request_write(struct discover_device *dev __attribute__((unused)),
1595                 bool *release)
1596 {
1597         *release = true;
1598         return 0;
1599 }
1600
1601 void device_release_write(struct discover_device *dev __attribute__((unused)),
1602         bool release __attribute__((unused)))
1603 {
1604 }
1605
1606 #endif
1607