]> git.ozlabs.org Git - petitboot/blob - discover/device-handler.c
discover: Call mount syscall directly
[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 <sys/stat.h>
9 #include <sys/wait.h>
10 #include <sys/mount.h>
11
12 #include <talloc/talloc.h>
13 #include <list/list.h>
14 #include <log/log.h>
15 #include <types/types.h>
16 #include <system/system.h>
17 #include <process/process.h>
18 #include <url/url.h>
19
20 #include "device-handler.h"
21 #include "discover-server.h"
22 #include "platform.h"
23 #include "event.h"
24 #include "parser.h"
25 #include "resource.h"
26 #include "paths.h"
27 #include "sysinfo.h"
28 #include "boot.h"
29
30 struct device_handler {
31         struct discover_server  *server;
32         int                     dry_run;
33
34         struct discover_device  **devices;
35         unsigned int            n_devices;
36
37         struct waitset          *waitset;
38         struct waiter           *timeout_waiter;
39         bool                    autoboot_enabled;
40         unsigned int            sec_to_boot;
41
42         struct discover_boot_option *default_boot_option;
43         struct list             unresolved_boot_options;
44
45         struct boot_task        *pending_boot;
46         bool                    pending_boot_is_default;
47 };
48
49 static int mount_device(struct discover_device *dev);
50 static int umount_device(struct discover_device *dev);
51
52 void discover_context_add_boot_option(struct discover_context *ctx,
53                 struct discover_boot_option *boot_option)
54 {
55         boot_option->source = ctx->parser;
56         list_add_tail(&ctx->boot_options, &boot_option->list);
57         talloc_steal(ctx, boot_option);
58 }
59
60 /**
61  * device_handler_get_device_count - Get the count of current handler devices.
62  */
63
64 int device_handler_get_device_count(const struct device_handler *handler)
65 {
66         return handler->n_devices;
67 }
68
69 /**
70  * device_handler_get_device - Get a handler device by index.
71  */
72
73 const struct discover_device *device_handler_get_device(
74         const struct device_handler *handler, unsigned int index)
75 {
76         if (index >= handler->n_devices) {
77                 assert(0 && "bad index");
78                 return NULL;
79         }
80
81         return handler->devices[index];
82 }
83
84 struct discover_boot_option *discover_boot_option_create(
85                 struct discover_context *ctx,
86                 struct discover_device *device)
87 {
88         struct discover_boot_option *opt;
89
90         opt = talloc_zero(ctx, struct discover_boot_option);
91         opt->option = talloc_zero(opt, struct boot_option);
92         opt->device = device;
93
94         return opt;
95 }
96
97 static int device_match_uuid(struct discover_device *dev, const char *uuid)
98 {
99         return dev->uuid && !strcmp(dev->uuid, uuid);
100 }
101
102 static int device_match_label(struct discover_device *dev, const char *label)
103 {
104         return dev->label && !strcmp(dev->label, label);
105 }
106
107 static int device_match_id(struct discover_device *dev, const char *id)
108 {
109         return !strcmp(dev->device->id, id);
110 }
111
112 static int device_match_serial(struct discover_device *dev, const char *serial)
113 {
114         const char *val = discover_device_get_param(dev, "ID_SERIAL");
115         return val && !strcmp(val, serial);
116 }
117
118 static struct discover_device *device_lookup(
119                 struct device_handler *device_handler,
120                 int (match_fn)(struct discover_device *, const char *),
121                 const char *str)
122 {
123         struct discover_device *dev;
124         unsigned int i;
125
126         if (!str)
127                 return NULL;
128
129         for (i = 0; i < device_handler->n_devices; i++) {
130                 dev = device_handler->devices[i];
131
132                 if (match_fn(dev, str))
133                         return dev;
134         }
135
136         return NULL;
137 }
138
139 struct discover_device *device_lookup_by_name(struct device_handler *handler,
140                 const char *name)
141 {
142         if (!strncmp(name, "/dev/", strlen("/dev/")))
143                 name += strlen("/dev/");
144
145         return device_lookup_by_id(handler, name);
146 }
147
148 struct discover_device *device_lookup_by_uuid(
149                 struct device_handler *device_handler,
150                 const char *uuid)
151 {
152         return device_lookup(device_handler, device_match_uuid, uuid);
153 }
154
155 struct discover_device *device_lookup_by_label(
156                 struct device_handler *device_handler,
157                 const char *label)
158 {
159         return device_lookup(device_handler, device_match_label, label);
160 }
161
162 struct discover_device *device_lookup_by_id(
163                 struct device_handler *device_handler,
164                 const char *id)
165 {
166         return device_lookup(device_handler, device_match_id, id);
167 }
168
169 struct discover_device *device_lookup_by_serial(
170                 struct device_handler *device_handler,
171                 const char *serial)
172 {
173         return device_lookup(device_handler, device_match_serial, serial);
174 }
175
176 void device_handler_destroy(struct device_handler *handler)
177 {
178         talloc_free(handler);
179 }
180
181 static int destroy_device(void *arg)
182 {
183         struct discover_device *dev = arg;
184
185         umount_device(dev);
186
187         return 0;
188 }
189
190 struct discover_device *discover_device_create(struct device_handler *handler,
191                 const char *id)
192 {
193         struct discover_device *dev;
194
195         dev = device_lookup_by_id(handler, id);
196         if (dev)
197                 return dev;
198
199         dev = talloc_zero(handler, struct discover_device);
200         dev->device = talloc_zero(dev, struct device);
201         dev->device->id = talloc_strdup(dev->device, id);
202         list_init(&dev->params);
203         list_init(&dev->boot_options);
204
205         talloc_set_destructor(dev, destroy_device);
206
207         return dev;
208 }
209
210 struct discover_device_param {
211         char                    *name;
212         char                    *value;
213         struct list_item        list;
214 };
215
216 void discover_device_set_param(struct discover_device *device,
217                 const char *name, const char *value)
218 {
219         struct discover_device_param *param;
220         bool found = false;
221
222         list_for_each_entry(&device->params, param, list) {
223                 if (!strcmp(param->name, name)) {
224                         found = true;
225                         break;
226                 }
227         }
228
229         if (!found) {
230                 if (!value)
231                         return;
232                 param = talloc(device, struct discover_device_param);
233                 param->name = talloc_strdup(param, name);
234                 list_add(&device->params, &param->list);
235         } else {
236                 if (!value) {
237                         list_remove(&param->list);
238                         talloc_free(param);
239                         return;
240                 }
241                 talloc_free(param->value);
242         }
243
244         param->value = talloc_strdup(param, value);
245 }
246
247 const char *discover_device_get_param(struct discover_device *device,
248                 const char *name)
249 {
250         struct discover_device_param *param;
251
252         list_for_each_entry(&device->params, param, list) {
253                 if (!strcmp(param->name, name))
254                         return param->value;
255         }
256         return NULL;
257 }
258
259 struct device_handler *device_handler_init(struct discover_server *server,
260                 struct waitset *waitset, int dry_run)
261 {
262         struct device_handler *handler;
263
264         handler = talloc_zero(NULL, struct device_handler);
265         handler->server = server;
266         handler->waitset = waitset;
267         handler->dry_run = dry_run;
268         handler->autoboot_enabled = config_get()->autoboot_enabled;
269
270         list_init(&handler->unresolved_boot_options);
271
272         /* set up our mount point base */
273         pb_mkdir_recursive(mount_base());
274
275         parser_init();
276
277         return handler;
278 }
279
280 void device_handler_remove(struct device_handler *handler,
281                 struct discover_device *device)
282 {
283         struct discover_boot_option *opt, *tmp;
284         unsigned int i;
285
286         for (i = 0; i < handler->n_devices; i++)
287                 if (handler->devices[i] == device)
288                         break;
289
290         if (i == handler->n_devices) {
291                 talloc_free(device);
292                 return;
293         }
294
295         /* Free any unresolved options, as they're currently allocated
296          * against the handler */
297         list_for_each_entry_safe(&handler->unresolved_boot_options,
298                         opt, tmp, list) {
299                 if (opt->device != device)
300                         continue;
301                 list_remove(&opt->list);
302                 talloc_free(opt);
303         }
304
305         handler->n_devices--;
306         memmove(&handler->devices[i], &handler->devices[i + 1],
307                 (handler->n_devices - i) * sizeof(handler->devices[0]));
308         handler->devices = talloc_realloc(handler, handler->devices,
309                 struct discover_device *, handler->n_devices);
310
311         if (device->notified)
312                 discover_server_notify_device_remove(handler->server,
313                                                         device->device);
314
315         talloc_free(device);
316 }
317
318 static void boot_status(void *arg, struct boot_status *status)
319 {
320         struct device_handler *handler = arg;
321
322         discover_server_notify_boot_status(handler->server, status);
323 }
324
325 static void countdown_status(struct device_handler *handler,
326                 struct discover_boot_option *opt, unsigned int sec)
327 {
328         struct boot_status status;
329
330         status.type = BOOT_STATUS_INFO;
331         status.progress = -1;
332         status.detail = NULL;
333         status.message = talloc_asprintf(handler,
334                         "Booting %s in %u sec", opt->option->name, sec);
335
336         discover_server_notify_boot_status(handler->server, &status);
337
338         talloc_free(status.message);
339 }
340
341 static int default_timeout(void *arg)
342 {
343         struct device_handler *handler = arg;
344         struct discover_boot_option *opt;
345
346         if (!handler->default_boot_option)
347                 return 0;
348
349         if (handler->pending_boot)
350                 return 0;
351
352         opt = handler->default_boot_option;
353
354         if (handler->sec_to_boot) {
355                 countdown_status(handler, opt, handler->sec_to_boot);
356                 handler->sec_to_boot--;
357                 handler->timeout_waiter = waiter_register_timeout(
358                                                 handler->waitset, 1000,
359                                                 default_timeout, handler);
360                 return 0;
361         }
362
363         handler->timeout_waiter = NULL;
364
365         pb_log("Timeout expired, booting default option %s\n", opt->option->id);
366
367         handler->pending_boot = boot(handler, handler->default_boot_option,
368                         NULL, handler->dry_run, boot_status, handler);
369         handler->pending_boot_is_default = true;
370         return 0;
371 }
372
373 static bool priority_match(struct boot_priority *prio,
374                 struct discover_boot_option *opt)
375 {
376         return prio->type == opt->device->device->type ||
377                 prio->type == DEVICE_TYPE_ANY;
378 }
379
380 static int default_option_priority(struct discover_boot_option *opt)
381 {
382         const struct config *config;
383         struct boot_priority *prio;
384         unsigned int i;
385
386         config = config_get();
387
388         for (i = 0; i < config->n_boot_priorities; i++) {
389                 prio = &config->boot_priorities[i];
390                 if (priority_match(prio, opt))
391                         return prio->priority;
392         }
393
394         return 0;
395 }
396
397 static void set_default(struct device_handler *handler,
398                 struct discover_boot_option *opt)
399 {
400         int new_prio;
401
402         if (!handler->autoboot_enabled)
403                 return;
404
405         new_prio = default_option_priority(opt);
406
407         /* A negative priority indicates that we don't want to boot this device
408          * by default */
409         if (new_prio < 0)
410                 return;
411
412         /* Resolve any conflicts: if we have a new default option, it only
413          * replaces the current if it has a higher priority. */
414         if (handler->default_boot_option) {
415                 int cur_prio;
416
417                 cur_prio = default_option_priority(
418                                         handler->default_boot_option);
419
420                 if (new_prio > cur_prio) {
421                         handler->default_boot_option = opt;
422                         /* extend the timeout a little, so the user sees some
423                          * indication of the change */
424                         handler->sec_to_boot += 2;
425                 }
426
427                 return;
428         }
429
430         handler->sec_to_boot = config_get()->autoboot_timeout_sec;
431         handler->default_boot_option = opt;
432
433         pb_log("Boot option %s set as default, timeout %u sec.\n",
434                opt->option->id, handler->sec_to_boot);
435
436         default_timeout(handler);
437 }
438
439 static bool resource_is_resolved(struct resource *res)
440 {
441         return !res || res->resolved;
442 }
443
444 /* We only use this in an assert, which will disappear if we're compiling
445  * with NDEBUG, so we need the 'used' attribute for these builds */
446 static bool __attribute__((used)) boot_option_is_resolved(
447                 struct discover_boot_option *opt)
448 {
449         return resource_is_resolved(opt->boot_image) &&
450                 resource_is_resolved(opt->initrd) &&
451                 resource_is_resolved(opt->dtb) &&
452                 resource_is_resolved(opt->icon);
453 }
454
455 static bool resource_resolve(struct resource *res, const char *name,
456                 struct discover_boot_option *opt,
457                 struct device_handler *handler)
458 {
459         struct parser *parser = opt->source;
460
461         if (resource_is_resolved(res))
462                 return true;
463
464         pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
465                         opt->option->id, name, parser->name);
466         parser->resolve_resource(handler, res);
467
468         return res->resolved;
469 }
470
471 static bool boot_option_resolve(struct discover_boot_option *opt,
472                 struct device_handler *handler)
473 {
474         return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
475                 resource_resolve(opt->initrd, "initrd", opt, handler) &&
476                 resource_resolve(opt->dtb, "dtb", opt, handler) &&
477                 resource_resolve(opt->icon, "icon", opt, handler);
478 }
479
480 static void boot_option_finalise(struct device_handler *handler,
481                 struct discover_boot_option *opt)
482 {
483         assert(boot_option_is_resolved(opt));
484
485         /* check that the parsers haven't set any of the final data */
486         assert(!opt->option->boot_image_file);
487         assert(!opt->option->initrd_file);
488         assert(!opt->option->dtb_file);
489         assert(!opt->option->icon_file);
490         assert(!opt->option->device_id);
491
492         if (opt->boot_image)
493                 opt->option->boot_image_file = opt->boot_image->url->full;
494         if (opt->initrd)
495                 opt->option->initrd_file = opt->initrd->url->full;
496         if (opt->dtb)
497                 opt->option->dtb_file = opt->dtb->url->full;
498         if (opt->icon)
499                 opt->option->icon_file = opt->icon->url->full;
500
501         opt->option->device_id = opt->device->device->id;
502
503         if (opt->option->is_default)
504                 set_default(handler, opt);
505 }
506
507 static void notify_boot_option(struct device_handler *handler,
508                 struct discover_boot_option *opt)
509 {
510         struct discover_device *dev = opt->device;
511
512         if (!dev->notified)
513                 discover_server_notify_device_add(handler->server,
514                                                   opt->device->device);
515         dev->notified = true;
516         discover_server_notify_boot_option_add(handler->server, opt->option);
517 }
518
519 static void process_boot_option_queue(struct device_handler *handler)
520 {
521         struct discover_boot_option *opt, *tmp;
522
523         list_for_each_entry_safe(&handler->unresolved_boot_options,
524                         opt, tmp, list) {
525
526                 pb_debug("queue: attempting resolution for %s\n",
527                                 opt->option->id);
528
529                 if (!boot_option_resolve(opt, handler))
530                         continue;
531
532                 pb_debug("\tresolved!\n");
533
534                 list_remove(&opt->list);
535                 list_add_tail(&opt->device->boot_options, &opt->list);
536                 talloc_steal(opt->device, opt);
537                 boot_option_finalise(handler, opt);
538                 notify_boot_option(handler, opt);
539         }
540 }
541
542 struct discover_context *device_handler_discover_context_create(
543                 struct device_handler *handler,
544                 struct discover_device *device)
545 {
546         struct discover_context *ctx;
547
548         ctx = talloc_zero(handler, struct discover_context);
549         ctx->device = device;
550         list_init(&ctx->boot_options);
551
552         return ctx;
553 }
554
555 /**
556  * context_commit - Commit a temporary discovery context to the handler,
557  * and notify the clients about any new options / devices
558  */
559 void device_handler_discover_context_commit(struct device_handler *handler,
560                 struct discover_context *ctx)
561 {
562         struct discover_device *dev = ctx->device;
563         struct discover_boot_option *opt, *tmp;
564
565         if (!device_lookup_by_id(handler, dev->device->id))
566                 device_handler_add_device(handler, dev);
567
568         /* move boot options from the context to the device */
569         list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
570                 list_remove(&opt->list);
571
572                 if (boot_option_resolve(opt, handler)) {
573                         pb_log("boot option %s is resolved, "
574                                         "sending to clients\n",
575                                         opt->option->id);
576                         list_add_tail(&dev->boot_options, &opt->list);
577                         talloc_steal(dev, opt);
578                         boot_option_finalise(handler, opt);
579                         notify_boot_option(handler, opt);
580                 } else {
581                         if (!opt->source->resolve_resource) {
582                                 pb_log("parser %s gave us an unresolved "
583                                         "resource (%s), but no way to "
584                                         "resolve it\n",
585                                         opt->source->name, opt->option->id);
586                                 talloc_free(opt);
587                         } else {
588                                 pb_log("boot option %s is unresolved, "
589                                                 "adding to queue\n",
590                                                 opt->option->id);
591                                 list_add(&handler->unresolved_boot_options,
592                                                 &opt->list);
593                                 talloc_steal(handler, opt);
594                         }
595                 }
596         }
597 }
598
599 void device_handler_add_device(struct device_handler *handler,
600                 struct discover_device *device)
601 {
602         handler->n_devices++;
603         handler->devices = talloc_realloc(handler, handler->devices,
604                                 struct discover_device *, handler->n_devices);
605         handler->devices[handler->n_devices - 1] = device;
606
607 }
608
609 /* Start discovery on a hotplugged device. The device will be in our devices
610  * array, but has only just been initialised by the hotplug source.
611  */
612 int device_handler_discover(struct device_handler *handler,
613                 struct discover_device *dev)
614 {
615         struct discover_context *ctx;
616         int rc;
617
618         process_boot_option_queue(handler);
619
620         /* create our context */
621         ctx = device_handler_discover_context_create(handler, dev);
622
623         rc = mount_device(dev);
624         if (rc)
625                 goto out;
626
627         /* add this device to our system info */
628         system_info_register_blockdev(dev->device->id, dev->uuid,
629                         dev->mount_path);
630
631         /* run the parsers. This will populate the ctx's boot_option list. */
632         iterate_parsers(ctx);
633
634         /* add discovered stuff to the handler */
635         device_handler_discover_context_commit(handler, ctx);
636
637 out:
638         talloc_free(ctx);
639
640         return 0;
641 }
642
643 /* Incoming dhcp event */
644 int device_handler_dhcp(struct device_handler *handler,
645                 struct discover_device *dev, struct event *event)
646 {
647         struct discover_context *ctx;
648
649         /* create our context */
650         ctx = device_handler_discover_context_create(handler, dev);
651         ctx->event = event;
652
653         iterate_parsers(ctx);
654
655         device_handler_discover_context_commit(handler, ctx);
656
657         talloc_free(ctx);
658
659         return 0;
660 }
661
662 /* incoming conf event */
663 int device_handler_conf(struct device_handler *handler,
664                 struct discover_device *dev, struct pb_url *url)
665 {
666         struct discover_context *ctx;
667
668         /* create our context */
669         ctx = device_handler_discover_context_create(handler, dev);
670         ctx->conf_url = url;
671
672         iterate_parsers(ctx);
673
674         device_handler_discover_context_commit(handler, ctx);
675
676         talloc_free(ctx);
677
678         return 0;
679 }
680
681 static struct discover_boot_option *find_boot_option_by_id(
682                 struct device_handler *handler, const char *id)
683 {
684         unsigned int i;
685
686         for (i = 0; i < handler->n_devices; i++) {
687                 struct discover_device *dev = handler->devices[i];
688                 struct discover_boot_option *opt;
689
690                 list_for_each_entry(&dev->boot_options, opt, list)
691                         if (!strcmp(opt->option->id, id))
692                                 return opt;
693         }
694
695         return NULL;
696 }
697
698 void device_handler_boot(struct device_handler *handler,
699                 struct boot_command *cmd)
700 {
701         struct discover_boot_option *opt = NULL;
702
703         if (cmd->option_id && strlen(cmd->option_id))
704                 opt = find_boot_option_by_id(handler, cmd->option_id);
705
706         if (handler->pending_boot)
707                 boot_cancel(handler->pending_boot);
708         handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
709                         boot_status, handler);
710         handler->pending_boot_is_default = false;
711 }
712
713 void device_handler_cancel_default(struct device_handler *handler)
714 {
715         struct boot_status status;
716
717         if (handler->timeout_waiter)
718                 waiter_remove(handler->timeout_waiter);
719
720         handler->timeout_waiter = NULL;
721         handler->autoboot_enabled = false;
722
723         /* we only send status if we had a default boot option queued */
724         if (!handler->default_boot_option)
725                 return;
726
727         pb_log("Cancelling default boot option\n");
728
729         if (handler->pending_boot && handler->pending_boot_is_default) {
730                 boot_cancel(handler->pending_boot);
731                 handler->pending_boot = NULL;
732                 handler->pending_boot_is_default = false;
733         }
734
735         handler->default_boot_option = NULL;
736
737         status.type = BOOT_STATUS_INFO;
738         status.progress = -1;
739         status.detail = NULL;
740         status.message = "Default boot cancelled";
741
742         discover_server_notify_boot_status(handler->server, &status);
743 }
744
745 void device_handler_update_config(struct device_handler *handler,
746                 struct config *config)
747 {
748         config_set(config);
749         discover_server_notify_config(handler->server, config);
750 }
751
752 #ifndef PETITBOOT_TEST
753 static bool check_existing_mount(struct discover_device *dev)
754 {
755         struct stat devstat, mntstat;
756         struct mntent *mnt;
757         FILE *fp;
758         int rc;
759
760         rc = stat(dev->device_path, &devstat);
761         if (rc) {
762                 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
763                 return false;
764         }
765
766         if (!S_ISBLK(devstat.st_mode)) {
767                 pb_debug("%s: %s isn't a block device?\n", __func__,
768                                 dev->device_path);
769                 return false;
770         }
771
772         fp = fopen("/proc/self/mounts", "r");
773
774         for (;;) {
775                 mnt = getmntent(fp);
776                 if (!mnt)
777                         break;
778
779                 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
780                         continue;
781
782                 rc = stat(mnt->mnt_fsname, &mntstat);
783                 if (rc)
784                         continue;
785
786                 if (!S_ISBLK(mntstat.st_mode))
787                         continue;
788
789                 if (mntstat.st_rdev == devstat.st_rdev) {
790                         dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
791                         dev->mounted_rw = !!hasmntopt(mnt, "rw");
792                         dev->mounted = true;
793                         dev->unmount = false;
794
795                         pb_debug("%s: %s is already mounted (r%c) at %s\n",
796                                         __func__, dev->device_path,
797                                         dev->mounted_rw ? 'w' : 'o',
798                                         mnt->mnt_dir);
799                         break;
800                 }
801         }
802
803         fclose(fp);
804
805         return mnt != NULL;
806 }
807
808 static int mount_device(struct discover_device *dev)
809 {
810         const char *fstype;
811         int rc;
812
813         if (!dev->device_path)
814                 return -1;
815
816         if (dev->mounted)
817                 return 0;
818
819         if (check_existing_mount(dev))
820                 return 0;
821
822         fstype = discover_device_get_param(dev, "ID_FS_TYPE");
823         if (!fstype)
824                 return 0;
825
826         dev->mount_path = join_paths(dev, mount_base(),
827                                         dev->device_path);
828
829         if (pb_mkdir_recursive(dev->mount_path)) {
830                 pb_log("couldn't create mount directory %s: %s\n",
831                                 dev->mount_path, strerror(errno));
832                 goto err_free;
833         }
834
835         pb_log("mounting device %s read-only\n", dev->device_path);
836         errno = 0;
837         rc = mount(dev->device_path, dev->mount_path, fstype,
838                         MS_RDONLY | MS_SILENT, "");
839         if (!rc) {
840                 dev->mounted = true;
841                 dev->mounted_rw = false;
842                 dev->unmount = true;
843                 return 0;
844         }
845
846         pb_log("couldn't mount device %s: mount failed: %s\n",
847                         dev->device_path, strerror(errno));
848
849         pb_rmdir_recursive(mount_base(), dev->mount_path);
850 err_free:
851         talloc_free(dev->mount_path);
852         dev->mount_path = NULL;
853         return -1;
854 }
855
856 static int umount_device(struct discover_device *dev)
857 {
858         int rc;
859
860         if (!dev->mounted || !dev->unmount)
861                 return 0;
862
863         pb_log("unmounting device %s\n", dev->device_path);
864         rc = umount(dev->mount_path);
865         if (rc)
866                 return -1;
867
868         dev->mounted = false;
869
870         pb_rmdir_recursive(mount_base(), dev->mount_path);
871
872         talloc_free(dev->mount_path);
873         dev->mount_path = NULL;
874
875         return 0;
876 }
877
878 int device_request_write(struct discover_device *dev, bool *release)
879 {
880         int rc;
881
882         *release = false;
883
884         if (!dev->mounted)
885                 return -1;
886
887         if (dev->mounted_rw)
888                 return 0;
889
890         pb_log("remounting device %s read-write\n", dev->device_path);
891         rc = mount(dev->device_path, dev->mount_path, "",
892                         MS_REMOUNT | MS_SILENT, "");
893         if (rc)
894                 return -1;
895
896         dev->mounted_rw = true;
897         *release = true;
898         return 0;
899 }
900
901 void device_release_write(struct discover_device *dev, bool release)
902 {
903         if (!release)
904                 return;
905
906         pb_log("remounting device %s read-only\n", dev->device_path);
907         mount(dev->device_path, dev->mount_path, "",
908                         MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
909         dev->mounted_rw = false;
910 }
911
912 #else
913
914 static int umount_device(struct discover_device *dev __attribute__((unused)))
915 {
916         return 0;
917 }
918
919 static int __attribute__((unused)) mount_device(
920                 struct discover_device *dev __attribute__((unused)))
921 {
922         return 0;
923 }
924
925 int device_request_write(struct discover_device *dev __attribute__((unused)),
926                 bool *release)
927 {
928         *release = true;
929         return 0;
930 }
931
932 void device_release_write(struct discover_device *dev __attribute__((unused)),
933         bool release __attribute__((unused)))
934 {
935 }
936
937 #endif
938