summaryrefslogtreecommitdiffstats
path: root/cpukit/libblock/src/media.c
blob: a81a94dcae7ddab69ce7414475a3a577debef564 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/**
 * @file
 *
 * @ingroup RTEMSMedia
 *
 * @brief Media implementation.
 */

/*
 * Copyright (c) 2009-2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.com/license/LICENSE.
 */

#include <string.h>
#include <assert.h>
#include <unistd.h>

#include <rtems.h>
#include <rtems/bdbuf.h>
#include <rtems/blkdev.h>
#include <rtems/bdpart.h>
#include <rtems/libio.h>
#include <rtems/dosfs.h>

#include <rtems/media.h>

typedef struct {
  rtems_bdpart_partition *partitions;
  size_t *count;
} partition_table;

typedef struct {
  size_t index;
  rtems_blkdev_bnum begin;
  rtems_blkdev_bnum count;
} partition;

typedef struct media_item {
  rtems_chain_node node;
  struct media_item *parent;
  char *disk_path;
  char *mount_path;
} media_item;

typedef struct listener_item {
  rtems_chain_node node;
  rtems_media_listener listener;
  void *listener_arg;
} listener_item;

static RTEMS_CHAIN_DEFINE_EMPTY(listener_item_chain);

static RTEMS_CHAIN_DEFINE_EMPTY(media_item_chain);

static rtems_id media_mutex = RTEMS_ID_NONE;

static rtems_status_code lock(void)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = rtems_semaphore_obtain(media_mutex, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
  if (sc != RTEMS_SUCCESSFUL) {
    sc = RTEMS_NOT_CONFIGURED;
  }

  return sc;
}

static void unlock(void)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = rtems_semaphore_release(media_mutex);
  assert(sc == RTEMS_SUCCESSFUL);
}

static listener_item *find_listener(
  rtems_media_listener listener,
  void *listener_arg
)
{
  rtems_chain_node *node = rtems_chain_first(&listener_item_chain);

  while (!rtems_chain_is_tail(&listener_item_chain, node)) {
    listener_item *item = (listener_item *) node;

    if (item->listener == listener && item->listener_arg == listener_arg) {
      return item;
    }

    node = rtems_chain_next(node);
  }

  return NULL;
}

rtems_status_code rtems_media_listener_add(
  rtems_media_listener listener,
  void *listener_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = lock();
  if (sc == RTEMS_SUCCESSFUL) {
    listener_item *item = find_listener(listener, listener_arg);

    if (item == NULL) {
      item = malloc(sizeof(*item));
      if (item != NULL) {
        item->listener = listener;
        item->listener_arg = listener_arg;
        rtems_chain_append_unprotected(&listener_item_chain, &item->node);
      } else {
        sc = RTEMS_NO_MEMORY;
      }
    } else {
      sc = RTEMS_TOO_MANY;
    }

    unlock();
  }

  return sc;
}

rtems_status_code rtems_media_listener_remove(
  rtems_media_listener listener,
  void *listener_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = lock();
  if (sc == RTEMS_SUCCESSFUL) {
    listener_item *item = find_listener(listener, listener_arg);

    if (item != NULL) {
      rtems_chain_extract_unprotected(&item->node);
      free(item);
    } else {
      sc = RTEMS_INVALID_ID;
    }

    unlock();
  }

  return sc;
}

static rtems_status_code notify(
  rtems_media_event event,
  rtems_media_state state,
  const char *src,
  const char *dest
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_status_code rsc = RTEMS_SUCCESSFUL;
  rtems_chain_node *node = rtems_chain_first(&listener_item_chain);

  while (!rtems_chain_is_tail(&listener_item_chain, node)) {
    listener_item *item = (listener_item *) node;

    sc = (*item->listener)(event, state, src, dest, item->listener_arg);
    if (sc != RTEMS_SUCCESSFUL) {
      rsc = sc;
    }

    node = rtems_chain_next(node);
  }

  return rsc;
}

static void error(
  rtems_media_state state,
  const char *src,
  const char *dest
)
{
  notify(RTEMS_MEDIA_EVENT_ERROR, state, src, dest);
}

static media_item *get_media_item(
  const char *disk_path,
  const char *mount_path
)
{
  rtems_chain_node *node = rtems_chain_first(&media_item_chain);

  while (!rtems_chain_is_tail(&media_item_chain, node)) {
    media_item *item = (media_item *) node;

    if (
      (disk_path == NULL || strcmp(disk_path, item->disk_path) == 0)
        && (mount_path == NULL || strcmp(mount_path, item->mount_path) == 0)
    ) {
      return item;
    }

    node = rtems_chain_next(node);
  }

  return NULL;
}

static void free_item(media_item *item)
{
  rtems_chain_extract_unprotected(&item->node);
  free(item->mount_path);
  free(item);
}

static void create_item(
  media_item *parent,
  const char *disk_path,
  const char *mount_path
)
{
  size_t disk_path_size = strlen(disk_path) + 1;
  media_item *item = malloc(sizeof(*item) + disk_path_size);

  if (item != NULL) {
    if (mount_path != NULL) {
      item->mount_path = strdup(mount_path);

      if (item->mount_path == NULL) {
        free(item);

        return;
      }
    } else {
      item->mount_path = NULL;
    }

    item->parent = parent;
    item->disk_path = (char *) item + sizeof(*item);
    memcpy(item->disk_path, disk_path, disk_path_size);
    rtems_chain_append(&media_item_chain, &item->node);
  }
}

static void remove_mount_point(const char *mount_path)
{
  media_item *item = get_media_item(NULL, mount_path);

  if (item != NULL) {
    free(item->mount_path);
    item->mount_path = NULL;
  } else {
    error(RTEMS_MEDIA_ERROR_MOUNT_POINT_UNKNOWN, mount_path, NULL);
  }
}

static void remove_partition(const char *partition_path)
{
  media_item *item = get_media_item(partition_path, NULL);

  if (item != NULL) {
    if (item->mount_path != NULL) {
      error(
        RTEMS_MEDIA_ERROR_PARTITION_DETACH_WITH_MOUNT,
        partition_path,
        item->mount_path
      );
    }
    free_item(item);
  } else {
    error(RTEMS_MEDIA_ERROR_PARTITION_UNKNOWN, partition_path, NULL);
  }
}

static void remove_disk(const char *disk_path)
{
  media_item *item = get_media_item(disk_path, NULL);

  if (item != NULL) {
    rtems_chain_node *node = rtems_chain_first(&media_item_chain);

    while (!rtems_chain_is_tail(&media_item_chain, node)) {
      media_item *child = (media_item *) node;

      node = rtems_chain_next(node);

      if (child->parent == item) {
        if (child->mount_path != NULL) {
          error(
            RTEMS_MEDIA_ERROR_MOUNT_POINT_ORPHAN,
            child->mount_path,
            disk_path
          );
        }
        error(RTEMS_MEDIA_ERROR_PARTITION_ORPHAN, child->disk_path, disk_path);
        free_item(child);
      }
    }

    free_item(item);
  } else {
    error(RTEMS_MEDIA_ERROR_DISK_UNKNOWN, disk_path, NULL);
  }
}

static void add_disk(const char *disk_path)
{
  media_item *item = get_media_item(disk_path, NULL);

  if (item != NULL) {
    error(RTEMS_MEDIA_ERROR_DISK_EXISTS, disk_path, NULL);
    remove_disk(disk_path);
  }

  create_item(NULL, disk_path, NULL);
}

static void add_partition(const char *disk_path, const char *partition_path)
{
  media_item *item = get_media_item(partition_path, NULL);
  media_item *parent = get_media_item(disk_path, NULL);

  if (item != NULL) {
    error(RTEMS_MEDIA_ERROR_DISK_OR_PARTITION_EXISTS, partition_path, NULL);
    remove_disk(partition_path);
  }

  if (parent != NULL) {
    create_item(parent, partition_path, NULL);
  } else {
    error(
      RTEMS_MEDIA_ERROR_PARTITION_WITH_UNKNOWN_DISK,
      partition_path,
      disk_path
    );
  }
}

static void add_mount_point(const char *disk_path, const char *mount_path)
{
  media_item *item = get_media_item(disk_path, NULL);

  if (item != NULL) {
    if (item->mount_path != NULL) {
      error(RTEMS_MEDIA_ERROR_MOUNT_POINT_EXISTS, item->mount_path, NULL);
      free(item->mount_path);
    }
    item->mount_path = strdup(mount_path);
  } else {
    error(RTEMS_MEDIA_ERROR_DISK_OR_PARTITION_UNKNOWN, disk_path, NULL);
  }
}

static bool is_add_state(rtems_media_state state)
{
  return state == RTEMS_MEDIA_STATE_SUCCESS;
}

static bool is_remove_state(rtems_media_state state)
{
  return state == RTEMS_MEDIA_STATE_SUCCESS
    || state == RTEMS_MEDIA_STATE_FAILED;
}

static rtems_status_code remember_event(
  rtems_media_event event,
  rtems_media_state state,
  const char *src,
  const char *dest
)
{
  switch (event) {
    case RTEMS_MEDIA_EVENT_DISK_ATTACH:
      if (is_add_state(state)) {
        add_disk(dest);
      }
      break;
    case RTEMS_MEDIA_EVENT_PARTITION_ATTACH:
      if (is_add_state(state)) {
        add_partition(src, dest);
      }
      break;
    case RTEMS_MEDIA_EVENT_MOUNT:
      if (is_add_state(state)) {
        add_mount_point(src, dest);
      }
      break;
    case RTEMS_MEDIA_EVENT_UNMOUNT:
      if (is_remove_state(state)) {
        remove_mount_point(src);
      }
      break;
    case RTEMS_MEDIA_EVENT_PARTITION_DETACH:
      if (is_remove_state(state)) {
        remove_partition(src);
      }
      break;
    case RTEMS_MEDIA_EVENT_DISK_DETACH:
      if (is_remove_state(state)) {
        remove_disk(src);
      }
      break;
    default:
      break;
  }

  return RTEMS_SUCCESSFUL;
}

static rtems_status_code process_event(
  rtems_media_event event,
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_media_state state = RTEMS_MEDIA_STATE_FAILED;
  char *dest = NULL;

  sc = notify(event, RTEMS_MEDIA_STATE_INQUIRY, src, NULL);
  if (sc == RTEMS_SUCCESSFUL) {
    state = RTEMS_MEDIA_STATE_READY;
  } else {
    state = RTEMS_MEDIA_STATE_ABORTED;
  }

  sc = (*worker)(state, src, &dest, worker_arg);
  if (state == RTEMS_MEDIA_STATE_READY) {
    if (sc == RTEMS_SUCCESSFUL) {
      state = RTEMS_MEDIA_STATE_SUCCESS;
    } else {
      state = RTEMS_MEDIA_STATE_FAILED;
    }
  }

  notify(event, state, src, dest);
  remember_event(event, state, src, dest);

  if (state == RTEMS_MEDIA_STATE_SUCCESS) {
    sc = RTEMS_SUCCESSFUL;
  } else if (state == RTEMS_MEDIA_STATE_ABORTED) {
    sc = RTEMS_UNSATISFIED;
  } else {
    sc = RTEMS_IO_ERROR;
  }

  if (dest_ptr != NULL && sc == RTEMS_SUCCESSFUL) {
    *dest_ptr = dest;
  } else {
    free(dest);
  }

  return sc;
}

static rtems_status_code mount_worker(
  rtems_media_state state,
  const char *src,
  char **dest,
  void *worker_arg
)
{
  int rv = 0;

  if (state == RTEMS_MEDIA_STATE_READY) {
    rtems_dosfs_mount_options mount_options;
    char *mount_path = NULL;

    if (worker_arg == NULL) {
      mount_path = rtems_media_replace_prefix(RTEMS_MEDIA_MOUNT_BASE, src);
    } else {
      mount_path = strdup(worker_arg);
    }

    if (mount_path == NULL) {
      return RTEMS_IO_ERROR;
    }

    rv = rtems_mkdir(mount_path, S_IRWXU | S_IRWXG | S_IRWXO);
    if (rv != 0) {
      free(mount_path);

      return RTEMS_IO_ERROR;
    }

    memset(&mount_options, 0, sizeof(mount_options));

    /* In case this fails, we fall back to use the default converter */
    mount_options.converter = rtems_dosfs_create_utf8_converter("CP850");

    rv = mount(
      src,
      mount_path,
      RTEMS_FILESYSTEM_TYPE_DOSFS,
      RTEMS_FILESYSTEM_READ_WRITE,
      &mount_options
    );
    if (rv != 0) {
      rmdir(mount_path);
      free(mount_path);

      return RTEMS_IO_ERROR;
    }

    *dest = mount_path;
  }

  return RTEMS_SUCCESSFUL;
}

static rtems_status_code do_mount(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  if (worker == NULL) {
    worker = mount_worker;
  }

  return process_event(
    RTEMS_MEDIA_EVENT_MOUNT,
    src,
    dest_ptr,
    worker,
    worker_arg
  );
}

static rtems_status_code do_partition_attach(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  char *part_path = NULL;

  if (worker != NULL) {
    sc = process_event(
      RTEMS_MEDIA_EVENT_PARTITION_ATTACH,
      src,
      &part_path,
      worker,
      worker_arg
    );

    if (sc == RTEMS_SUCCESSFUL) {
      sc = do_mount(part_path, NULL, NULL, NULL);
    }
  } else {
    sc = RTEMS_INVALID_ADDRESS;
  }

  if (dest_ptr != NULL && sc == RTEMS_SUCCESSFUL) {
    *dest_ptr = part_path;
  } else {
    free(part_path);
  }

  return sc;
}

static rtems_status_code partition_attach_worker(
  rtems_media_state state,
  const char *src,
  char **dest,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if (state == RTEMS_MEDIA_STATE_READY) {
    partition *part = worker_arg;
    char *part_path = rtems_media_append_minor(src, part->index);

    if (part_path == NULL) {
      return RTEMS_IO_ERROR;
    }

    sc = rtems_blkdev_create_partition(
      part_path,
      src,
      part->begin,
      part->count
    );
    if (sc != RTEMS_SUCCESSFUL) {
      free(part_path);

      return RTEMS_IO_ERROR;
    }

    *dest = part_path;
  }

  return RTEMS_SUCCESSFUL;
}

static rtems_status_code attach_and_mount_partitions(
  const char *disk_path,
  const rtems_bdpart_partition *partitions,
  size_t count
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  size_t i = 0;

  for (i = 0; i < count; ++i) {
    partition part_desc = {
      .index = i,
      .begin = partitions [i].begin,
      .count = partitions [i].end - partitions [i].begin
    };
    char *part_path = NULL;

    sc = process_event(
      RTEMS_MEDIA_EVENT_PARTITION_ATTACH,
      disk_path,
      &part_path,
      partition_attach_worker,
      &part_desc
    );

    if (sc == RTEMS_SUCCESSFUL) {
      sc = do_mount(part_path, NULL, NULL, NULL);
    }

    free(part_path);
  }

  return sc;
}

static rtems_status_code partition_inquiry_worker(
  rtems_media_state state,
  const char *src,
  char **dest __attribute__((unused)),
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if (state == RTEMS_MEDIA_STATE_READY) {
    partition_table *pt = worker_arg;
    rtems_bdpart_format format;

    sc = rtems_bdpart_read(src, &format, pt->partitions, pt->count);
    if (sc != RTEMS_SUCCESSFUL || *pt->count == 0) {
      return RTEMS_IO_ERROR;
    }
  }

  return RTEMS_SUCCESSFUL;
}

static rtems_status_code do_partition_inquiry(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if (worker == NULL) {
    rtems_bdpart_partition partitions [RTEMS_BDPART_PARTITION_NUMBER_HINT];
    size_t count = RTEMS_BDPART_PARTITION_NUMBER_HINT;
    partition_table pt = {
     .partitions = partitions,
     .count = &count
    };

    sc = process_event(
      RTEMS_MEDIA_EVENT_PARTITION_INQUIRY,
      src,
      dest_ptr,
      partition_inquiry_worker,
      &pt
    );

    if (sc == RTEMS_SUCCESSFUL) {
      sc = attach_and_mount_partitions(src, partitions, count);
    }
  } else {
    sc = process_event(
      RTEMS_MEDIA_EVENT_PARTITION_INQUIRY,
      src,
      dest_ptr,
      worker,
      worker_arg
    );
  }

  return sc;
}

static rtems_status_code do_disk_attach(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_status_code rsc = RTEMS_SUCCESSFUL;
  char *disk_path = NULL;

  if (worker != NULL) {
    rsc = process_event(
      RTEMS_MEDIA_EVENT_DISK_ATTACH,
      src,
      &disk_path,
      worker,
      worker_arg
    );
    
    if (rsc == RTEMS_SUCCESSFUL) {
      sc = do_mount(disk_path, NULL, NULL, NULL);

      if (sc != RTEMS_SUCCESSFUL) {
        do_partition_inquiry(disk_path, NULL, NULL, NULL);
      }
    }
  } else {
    rsc = RTEMS_INVALID_ADDRESS;
  }

  if (dest_ptr != NULL && rsc == RTEMS_SUCCESSFUL) {
    *dest_ptr = disk_path;
  } else {
    free(disk_path);
  }

  return rsc;
}

static rtems_status_code unmount_worker(
  rtems_media_state state,
  const char *src,
  char **dest __attribute__((unused)),
  void *worker_arg __attribute__((unused))
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if (state == RTEMS_MEDIA_STATE_READY) {
    int rv = unmount(src);

    if (rv == 0) {
      rv = rmdir(src);
      if (rv != 0) {
        sc = RTEMS_IO_ERROR;
      }
    } else {
      sc = RTEMS_IO_ERROR;
    }
  }

  return sc;
}

static rtems_status_code do_unmount(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  if (worker == NULL) {
    worker = unmount_worker;
    worker_arg = NULL;
  }

  return process_event(
    RTEMS_MEDIA_EVENT_UNMOUNT,
    src,
    dest_ptr,
    worker,
    worker_arg
  );
}

static rtems_status_code disk_detach_worker(
  rtems_media_state state,
  const char *src,
  char **dest __attribute__((unused)),
  void *worker_arg __attribute__((unused))
)
{
  rtems_status_code rsc = RTEMS_SUCCESSFUL;

  if (state == RTEMS_MEDIA_STATE_READY) {
    int rv = unlink(src);

    if (rv != 0) {
      rsc = RTEMS_IO_ERROR;
    }
  }

  return rsc;
}

static rtems_status_code detach_item(rtems_media_event event, media_item *item)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_status_code rsc = RTEMS_SUCCESSFUL;

  if (item->mount_path != NULL) {
    sc = do_unmount(item->mount_path, NULL, NULL, NULL);
    if (sc != RTEMS_SUCCESSFUL) {
      rsc = RTEMS_IO_ERROR;
    }
  }

  sc = process_event(event, item->disk_path, NULL, disk_detach_worker, NULL);
  if (sc != RTEMS_SUCCESSFUL) {
    rsc = RTEMS_IO_ERROR;
  }

  return rsc;
}

static rtems_status_code detach_parent_item(media_item *parent)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;
  rtems_status_code rsc = RTEMS_SUCCESSFUL;

  rtems_chain_node *node = rtems_chain_first(&media_item_chain);

  while (!rtems_chain_is_tail(&media_item_chain, node)) {
    media_item *child = (media_item *) node;

    node = rtems_chain_next(node);

    if (child->parent == parent) {
      sc = detach_item(RTEMS_MEDIA_EVENT_PARTITION_DETACH, child);
      if (sc != RTEMS_SUCCESSFUL) {
        rsc = RTEMS_IO_ERROR;
      }
    }
  }

  sc = detach_item(RTEMS_MEDIA_EVENT_DISK_DETACH, parent);
  if (sc != RTEMS_SUCCESSFUL) {
    rsc = RTEMS_IO_ERROR;
  }

  return rsc;
}

static rtems_status_code do_disk_detach(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  if (worker == NULL) {
    media_item *parent = get_media_item(src, NULL);

    if (parent != NULL) {
      return detach_parent_item(parent);
    }

    worker = disk_detach_worker;
    worker_arg = NULL;
  }

  return process_event(
    RTEMS_MEDIA_EVENT_DISK_DETACH,
    src,
    dest_ptr,
    worker,
    worker_arg
  );
}

static rtems_status_code do_partition_detach(
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  if (worker == NULL) {
    media_item *item = get_media_item(src, NULL);

    if (item != NULL) {
      return detach_item(RTEMS_MEDIA_EVENT_PARTITION_DETACH, item);
    }

    worker = disk_detach_worker;
    worker_arg = NULL;
  }

  return process_event(
    RTEMS_MEDIA_EVENT_PARTITION_DETACH,
    src,
    dest_ptr,
    worker,
    worker_arg
  );
}

rtems_status_code rtems_media_post_event(
  rtems_media_event event,
  const char *src,
  char **dest_ptr,
  rtems_media_worker worker,
  void *worker_arg
)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  sc = lock();
  if (sc != RTEMS_SUCCESSFUL) {
    return sc;
  }

  switch (event) {
    case RTEMS_MEDIA_EVENT_DISK_ATTACH:
      sc = do_disk_attach(src, dest_ptr, worker, worker_arg);
      break;
    case RTEMS_MEDIA_EVENT_DISK_DETACH:
      sc = do_disk_detach(src, dest_ptr, worker, worker_arg);
      break;
    case RTEMS_MEDIA_EVENT_MOUNT:
      sc = do_mount(src, dest_ptr, worker, worker_arg);
      break;
    case RTEMS_MEDIA_EVENT_UNMOUNT:
      sc = do_unmount(src, dest_ptr, worker, worker_arg);
      break;
    case RTEMS_MEDIA_EVENT_PARTITION_INQUIRY:
      sc = do_partition_inquiry(src, dest_ptr, worker, worker_arg);
      break;
    case RTEMS_MEDIA_EVENT_PARTITION_ATTACH:
      sc = do_partition_attach(src, dest_ptr, worker, worker_arg);
      break;
    case RTEMS_MEDIA_EVENT_PARTITION_DETACH:
      sc = do_partition_detach(src, dest_ptr, worker, worker_arg);
      break;
    default:
      sc = RTEMS_INVALID_ID;
      break;
  }

  unlock();

  return sc;
}

rtems_status_code rtems_media_initialize(void)
{
  rtems_status_code sc = RTEMS_SUCCESSFUL;

  if (media_mutex == RTEMS_ID_NONE) {
    sc = rtems_semaphore_create(
      rtems_build_name('M', 'D', 'I', 'A'),
      1,
      RTEMS_LOCAL | RTEMS_PRIORITY
        | RTEMS_INHERIT_PRIORITY | RTEMS_BINARY_SEMAPHORE,
      0,
      &media_mutex
    );
    if (sc != RTEMS_SUCCESSFUL) {
      sc = RTEMS_NO_MEMORY;
    }
  }

  return sc;
}