1 /****************************************************************************
2 * fs/driver/fs_findblockdriver.c
3 *
4 * Copyright (C) 2008, 2017 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 "vfs_config.h"
41
42 #include "sys/types.h"
43 #include "sys/mount.h"
44 #include "errno.h"
45 #include "fs/driver.h"
46 #include "string.h"
47
48 /****************************************************************************
49 * Public Functions
50 ****************************************************************************/
51
52 /****************************************************************************
53 * Name: find_blockdriver
54 *
55 * Description:
56 * Return the inode of the block driver specified by 'pathname'
57 *
58 * Input Parameters:
59 * pathname - The full path to the block driver to be located
60 * mountflags - If MS_RDONLY is not set, then driver must support write
61 * operations (see include/sys/mount.h)
62 * ppinode - Address of the location to return the inode reference
63 *
64 * Returned Value:
65 * Returns zero on success or a negated errno on failure:
66 *
67 * ENOENT - No block driver of this name is registered
68 * ENOTBLK - The inode associated with the pathname is not a block driver
69 * EACCESS - The MS_RDONLY option was not set but this driver does not
70 * support write access
71 *
72 ****************************************************************************/
find_blockdriver(const char * pathname,int mountflags,struct Vnode ** vpp)73 int find_blockdriver(const char *pathname, int mountflags, struct Vnode **vpp)
74 {
75 int ret;
76 struct Vnode *vp = NULL;
77
78 /* Sanity checks */
79
80 /* Find the vnode registered with this pathname */
81 VnodeHold();
82 ret = VnodeLookup(pathname, &vp, V_DUMMY);
83 if (ret < 0)
84 {
85 ret = -EACCES;
86 goto errout;
87 }
88
89 /* Verify that the vnode is a block driver. */
90 if (vp->type != VNODE_TYPE_BLK)
91 {
92 PRINT_DEBUG("%s is not a block driver\n", pathname);
93 ret = -ENOTBLK;
94 goto errout;
95 }
96
97 /* Make sure that the vnode supports the requested access */
98
99 struct block_operations *i_bops = (struct block_operations *)((struct drv_data *)vp->data)->ops;
100
101 if (i_bops == NULL || i_bops->read == NULL || (i_bops->write == NULL && (mountflags & MS_RDONLY) == 0))
102 {
103 PRINT_DEBUG("%s does not support requested access\n", pathname);
104 ret = -EACCES;
105 goto errout;
106 }
107
108 *vpp = vp;
109 VnodeDrop();
110 return OK;
111
112 errout:
113 VnodeDrop();
114 return ret;
115 }
116