2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2011-04-05 22:54:00 +02:00
|
|
|
* fs/fs_inode.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2011-04-05 22:54:00 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
2007-02-18 00:21:28 +01:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-05-31 22:14:15 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-04-05 23:18:03 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <nuttx/fs.h>
|
2011-04-05 23:18:03 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include "fs_internal.h"
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2009-12-15 01:18:32 +01:00
|
|
|
* Pre-processor Definitions
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Variables
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
static sem_t tree_sem;
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Variables
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct inode *root_inode = NULL;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: _inode_compare
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
static int _inode_compare(const char *fname,
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct inode *node)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
char *nname = node->i_name;
|
|
|
|
|
|
|
|
if (!nname)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fname)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* At end of node name? */
|
|
|
|
if (!*nname)
|
|
|
|
{
|
|
|
|
/* Yes.. also end of find name? */
|
|
|
|
if (!*fname || *fname == '/')
|
|
|
|
{
|
|
|
|
/* Yes.. return match */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No... return find name > node name */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* At end of find name?*/
|
2007-03-16 23:03:58 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
else if (!*fname || *fname == '/')
|
|
|
|
{
|
|
|
|
/* Yes... return find name < node name */
|
2007-03-16 23:03:58 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for non-matching characters */
|
|
|
|
else if (*fname > *nname)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (*fname < *nname)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not at the end of either string and all of the
|
|
|
|
* characters still match. keep looking.
|
|
|
|
*/
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fname++;
|
|
|
|
nname++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Public Functions
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: fs_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is called from the OS initialization logic to configure
|
|
|
|
* the file system.
|
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
void fs_initialize(void)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-03-14 23:41:09 +01:00
|
|
|
/* Initialize the semaphore to one (to support one-at-
|
|
|
|
* a-time access to the inode tree).
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)sem_init(&tree_sem, 0, 1);
|
|
|
|
|
|
|
|
/* Initialize files array (if it is used) */
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAVE_WEAKFUNCTIONS
|
|
|
|
if (files_initialize != NULL)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
files_initialize();
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: inode_semtake
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
void inode_semtake(void)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-03-14 23:41:09 +01:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
while (sem_wait(&tree_sem) != 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-03-14 23:41:09 +01:00
|
|
|
/* The only case that an error should occr here is if
|
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
2011-04-05 22:54:00 +02:00
|
|
|
ASSERT(errno == EINTR);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: inode_semgive
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
void inode_semgive(void)
|
|
|
|
{
|
|
|
|
sem_post(&tree_sem);
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: inode_search
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the inode associated with 'path' returning the
|
|
|
|
* inode references and references to its companion nodes.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the tree_sem
|
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
FAR struct inode *inode_search(const char **path,
|
|
|
|
FAR struct inode **peer,
|
2007-05-09 02:04:03 +02:00
|
|
|
FAR struct inode **parent,
|
|
|
|
const char **relpath)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
const char *name = *path + 1; /* Skip over leading '/' */
|
|
|
|
FAR struct inode *node = root_inode;
|
|
|
|
FAR struct inode *left = NULL;
|
|
|
|
FAR struct inode *above = NULL;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
int result = _inode_compare(name, node);
|
|
|
|
|
|
|
|
/* Case 1: The name is less than the name of the node.
|
|
|
|
* Since the names are ordered, these means that there
|
|
|
|
* is no peer node with this name and that there can be
|
|
|
|
* no match in the fileystem.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
node = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Case 2: the name is greater than the name of the node.
|
|
|
|
* In this case, the name may still be in the list to the
|
|
|
|
* "right"
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (result > 0)
|
|
|
|
{
|
|
|
|
left = node;
|
|
|
|
node = node->i_peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The names match */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2007-05-09 02:04:03 +02:00
|
|
|
/* Now there are three more possibilities:
|
2007-02-18 00:21:28 +01:00
|
|
|
* (1) This is the node that we are looking for or,
|
2007-05-09 02:04:03 +02:00
|
|
|
* (2) The node we are looking for is "below" this one.
|
|
|
|
* (3) This node is a mountpoint and will absorb all request
|
|
|
|
* below this one
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
name = inode_nextname(name);
|
2007-05-09 02:04:03 +02:00
|
|
|
if (!*name || INODE_IS_MOUNTPT(node))
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-05-09 02:04:03 +02:00
|
|
|
/* Either (1) we are at the end of the path, so this must be the
|
|
|
|
* node we are looking for or else (2) this node is a mountpoint
|
|
|
|
* and will handle the remaining part of the pathname
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2007-05-09 02:04:03 +02:00
|
|
|
if (relpath)
|
|
|
|
{
|
|
|
|
*relpath = name;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* More to go, keep looking at the next level "down" */
|
|
|
|
|
|
|
|
above = node;
|
|
|
|
left = NULL;
|
|
|
|
node = node->i_child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* node is null. This can happen in one of four cases:
|
|
|
|
* With node = NULL
|
|
|
|
* (1) We went left past the final peer: The new node
|
|
|
|
* name is larger than any existing node name at
|
|
|
|
* that level.
|
|
|
|
* (2) We broke out in the middle of the list of peers
|
|
|
|
* because the name was not found in the ordered
|
|
|
|
* list.
|
|
|
|
* (3) We went down past the final parent: The new node
|
|
|
|
* name is "deeper" than anything that we currently
|
|
|
|
* have in the tree.
|
|
|
|
* with node != NULL
|
|
|
|
* (4) When the node matching the full path is found
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (peer) *peer = left;
|
|
|
|
if (parent) *parent = above;
|
|
|
|
*path = name;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: inode_free
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
void inode_free(FAR struct inode *node)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
if (node)
|
|
|
|
{
|
2007-03-14 23:41:09 +01:00
|
|
|
inode_free(node->i_peer);
|
|
|
|
inode_free(node->i_child);
|
2011-04-05 22:54:00 +02:00
|
|
|
kfree(node);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: inode_nextname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given a path with node names separated by '/', return
|
|
|
|
* the next node name.
|
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
const char *inode_nextname(const char *name)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2007-03-14 23:41:09 +01:00
|
|
|
while (*name && *name != '/') name++;
|
|
|
|
if (*name) name++;
|
|
|
|
return name;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2007-03-16 23:03:58 +01:00
|
|
|
|