1 /****************************************************************************
2 * fs/driver/fs_closeblockdriver.c
3 *
4 * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in pathname and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of pathname code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX nor the names of its contributors may be
18 * used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************/
35
36 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39
40 #include "fs/driver.h"
41 #include "vfs_config.h"
42 #include "errno.h"
43 #include "vnode.h"
44 #include "disk.h"
45
46 /****************************************************************************
47 * Public Functions
48 ****************************************************************************/
49
50 /****************************************************************************
51 * Name: close_blockdriver
52 *
53 * Description:
54 * Call the close method and release the vnode
55 *
56 * Input Parameters:
57 * vnode - reference to the vnode of a block driver opened by open_blockdriver
58 *
59 * Returned Value:
60 * Returns zero on success or a negated errno on failure:
61 *
62 * EINVAL - vnode is NULL
63 * ENOTBLK - The vnode is not a block driver
64 *
65 ****************************************************************************/
66
close_blockdriver(struct Vnode * vnode_ptr)67 int close_blockdriver(struct Vnode *vnode_ptr)
68 {
69 int ret = 0; /* Assume success */
70 los_part *part = NULL;
71 los_disk *disk = NULL;
72 struct block_operations *bop = NULL;
73
74 /* Sanity checks */
75
76 if (vnode_ptr == NULL || vnode_ptr->data == NULL)
77 {
78 ret = -EINVAL;
79 goto errout;
80 }
81
82 bop = (struct block_operations*)(((struct drv_data*)vnode_ptr->data)->ops);
83
84 if (bop == NULL) {
85 PRINT_ERR("vnode ops is null, not a valid block driver\n");
86 ret = -EINVAL;
87 goto errout;
88 }
89
90 /* Verify that the vnode is a block driver. */
91
92 if (vnode_ptr->type != VNODE_TYPE_BLK)
93 {
94 PRINT_ERR("vnode is not a block driver\n");
95 ret = -ENOTBLK;
96 goto errout;
97 }
98
99
100 part = los_part_find(vnode_ptr);
101 if (part != NULL)
102 {
103 disk = get_disk(part->disk_id);
104 if (disk == NULL)
105 {
106 ret = -EINVAL;
107 goto errout;
108 }
109
110 if (pthread_mutex_lock(&disk->disk_mutex) != ENOERR)
111 {
112 PRINT_ERR("%s %d, mutex lock fail!\n", __FUNCTION__, __LINE__);
113 return -EAGAIN;
114 }
115
116 if (disk->disk_status == STAT_INUSED)
117 {
118 /* Close the block driver. Not that no mutually exclusive access
119 * to the driver is enforced here. That must be done in the driver
120 * if needed.
121 */
122
123 if (bop->close != NULL)
124 {
125 ret = bop->close(vnode_ptr);
126 }
127 }
128
129 if (pthread_mutex_unlock(&disk->disk_mutex) != ENOERR)
130 {
131 PRINT_ERR("%s %d, mutex unlock fail!\n", __FUNCTION__, __LINE__);
132 }
133
134 }
135 else
136 {
137 if (bop->close != NULL)
138 {
139 ret = bop->close(vnode_ptr);
140 }
141 }
142
143 errout:
144 return ret;
145 }
146