yaffsfs.c: Fix NULL dereference in yaffs_unmount2_reldev()
[yaffs2.git] / rtems / rtems_yaffs_os_context.c
1 /*
2  * YAFFS port to RTEMS
3  *
4  * Copyright (c) 2011 embedded brains GmbH.  All rights reserved.
5  *
6  *  embedded brains GmbH
7  *  Obere Lagerstr. 30
8  *  82178 Puchheim
9  *  Germany
10  *  <rtems@embedded-brains.de>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  * As a special exception, linking other files with the object code from
17  * this one to produce an executable application does not by itself cause
18  * the resulting executable application to be covered by the GNU General
19  * Public License.
20  * This exception does not however invalidate any other reasons why the
21  * executable file might be covered by the GNU Public License. In particular,
22  * the other YAFFS files are not covered by this exception, and using them
23  * in a proprietary application requires a paid license from Aleph One.
24  */
25
26 #include "rtems_yaffs.h"
27
28 #include <assert.h>
29 #include <errno.h>
30
31 static void rtems_yaffs_default_lock(struct yaffs_dev *dev, void *arg)
32 {
33         rtems_status_code sc = RTEMS_SUCCESSFUL;
34         rtems_yaffs_default_os_context *os_context = arg;
35
36         sc = rtems_semaphore_obtain(
37                 os_context->semaphore_id,
38                 RTEMS_WAIT,
39                 RTEMS_NO_TIMEOUT
40         );
41         assert(sc == RTEMS_SUCCESSFUL);
42 }
43
44 static void rtems_yaffs_default_unlock(struct yaffs_dev *dev, void *arg)
45 {
46         rtems_status_code sc = RTEMS_SUCCESSFUL;
47         rtems_yaffs_default_os_context *os_context = arg;
48
49         sc = rtems_semaphore_release(os_context->semaphore_id);
50         assert(sc == RTEMS_SUCCESSFUL);
51 }
52
53 static void rtems_yaffs_default_unmount(struct yaffs_dev *dev, void *arg)
54 {
55         rtems_status_code sc = RTEMS_SUCCESSFUL;
56         rtems_yaffs_default_os_context *os_context = arg;
57
58         sc = rtems_semaphore_delete(os_context->semaphore_id);
59         assert(sc == RTEMS_SUCCESSFUL);
60 }
61
62 int rtems_yaffs_initialize_default_os_context(
63         rtems_yaffs_default_os_context *os_context
64 )
65 {
66         rtems_status_code sc = RTEMS_SUCCESSFUL;
67
68         os_context->os_context.lock = rtems_yaffs_default_lock;
69         os_context->os_context.unlock = rtems_yaffs_default_unlock;
70         os_context->os_context.unmount = rtems_yaffs_default_unmount;
71
72         sc = rtems_semaphore_create(
73                 rtems_build_name('Y', 'A', 'F', 'S'),
74                 1,
75                 RTEMS_LOCAL
76                         | RTEMS_BINARY_SEMAPHORE
77                         | RTEMS_INHERIT_PRIORITY
78                         | RTEMS_PRIORITY,
79                 0,
80                 &os_context->semaphore_id
81         );
82         if (sc == RTEMS_SUCCESSFUL) {
83                 return 0;
84         } else {
85                 errno = ENOMEM;
86
87                 return -1;
88         }
89 }