2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:06:21 +02:00
|
|
|
* fs/inode/fs_inode.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2017-02-02 17:39:41 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. All rights reserved.
|
2012-07-14 23:05:40 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* 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>
|
2017-02-02 20:01:21 +01:00
|
|
|
#include <limits.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <semaphore.h>
|
2017-02-02 17:39:41 +01:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2011-04-05 23:18:03 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2011-04-05 23:18:03 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
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
|
|
|
|
2017-01-27 15:50:01 +01:00
|
|
|
#define NO_HOLDER ((pid_t)-1)
|
2013-11-28 20:12:14 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2016-04-14 18:14:38 +02:00
|
|
|
|
2013-11-28 20:12:14 +01:00
|
|
|
/* Implements a re-entrant mutex for inode access. This must be re-entrant
|
|
|
|
* because there can be cycles. For example, it may be necessary to destroy
|
|
|
|
* a block driver inode on umount() after a removable block device has been
|
|
|
|
* removed. In that case umount() hold the inode semaphore, but the block
|
2013-11-28 20:30:06 +01:00
|
|
|
* driver may callback to unregister_blockdriver() after the un-mount,
|
2013-11-28 20:12:14 +01:00
|
|
|
* requiring the seamphore again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct inode_sem_s
|
|
|
|
{
|
|
|
|
sem_t sem; /* The semaphore */
|
|
|
|
pid_t holder; /* The current holder of the semaphore */
|
|
|
|
int16_t count; /* Number of counts held */
|
|
|
|
};
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-11-28 20:12:14 +01:00
|
|
|
static struct inode_sem_s g_inode_sem;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-06-11 19:24:20 +02:00
|
|
|
FAR struct inode *g_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
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Compare two inode names
|
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-08-04 00:04:14 +02:00
|
|
|
static int _inode_compare(FAR 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;
|
|
|
|
}
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* At end of node name? */
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
if (!*nname)
|
|
|
|
{
|
2012-07-14 23:05:40 +02:00
|
|
|
/* Yes.. also end of find name? */
|
|
|
|
|
|
|
|
if (!*fname || *fname == '/')
|
|
|
|
{
|
|
|
|
/* Yes.. return match */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No... return find name > node name */
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* 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 == '/')
|
|
|
|
{
|
2012-07-14 23:05:40 +02:00
|
|
|
/* Yes... return find name < node name */
|
2007-03-16 23:03:58 +01:00
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
return -1;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
/* Check for non-matching characters */
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
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.
|
|
|
|
*/
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fname++;
|
|
|
|
nname++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: _inode_dereference
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If the inode is a soft link, then (1) get the name of the full path of
|
|
|
|
* the soft link, (2) recursively look-up the inode referenced by the soft
|
|
|
|
* link, and (3) return the inode referenced by the soft link.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the g_inode_sem semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
static inline FAR struct inode *
|
|
|
|
_inode_dereference(FAR struct inode *node, FAR struct inode **peer,
|
|
|
|
FAR struct inode **parent, FAR const char **relpath)
|
|
|
|
{
|
2017-02-03 02:37:58 +01:00
|
|
|
FAR const char *copy;
|
2017-02-02 20:01:21 +01:00
|
|
|
unsigned int count = 0;
|
|
|
|
|
|
|
|
/* An infinite loop is avoided only by the loop count.
|
|
|
|
*
|
|
|
|
* REVISIT: The ELOOP error should be reported to the application in that
|
|
|
|
* case but there is no simple mechanism to do that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (node != NULL && INODE_IS_SOFTLINK(node))
|
|
|
|
{
|
2017-02-03 02:37:58 +01:00
|
|
|
/* Careful: inode_search_nofollow overwrites the input string pointer */
|
|
|
|
|
|
|
|
copy = (FAR const char *)node->u.i_link;
|
|
|
|
|
|
|
|
/* Now, look-up the inode associated with the target path */
|
|
|
|
|
|
|
|
node = inode_search_nofollow(©, peer, parent, relpath);
|
|
|
|
if (node == NULL && ++count > SYMLOOP_MAX)
|
2017-02-02 20:01:21 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
/****************************************************************************
|
2014-10-06 16:11:37 +02:00
|
|
|
* Name: inode_initialize
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 23:05:40 +02:00
|
|
|
* This is called from the OS initialization logic to configure the file
|
|
|
|
* system.
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-10-06 16:11:37 +02:00
|
|
|
void inode_initialize(void)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-10-06 16:11:37 +02:00
|
|
|
/* Initialize the semaphore to one (to support one-at-a-time access to the
|
|
|
|
* inode tree).
|
2007-03-14 23:41:09 +01:00
|
|
|
*/
|
|
|
|
|
2013-11-28 20:12:14 +01:00
|
|
|
(void)sem_init(&g_inode_sem.sem, 0, 1);
|
|
|
|
g_inode_sem.holder = NO_HOLDER;
|
|
|
|
g_inode_sem.count = 0;
|
2013-11-28 20:30:06 +01:00
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
/* 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
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2013-11-28 20:12:14 +01:00
|
|
|
* Get exclusive access to the in-memory inode tree (g_inode_sem).
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
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
|
|
|
{
|
2013-11-28 20:12:14 +01:00
|
|
|
pid_t me;
|
|
|
|
|
|
|
|
/* Do we already hold the semaphore? */
|
|
|
|
|
|
|
|
me = getpid();
|
|
|
|
if (me == g_inode_sem.holder)
|
|
|
|
{
|
|
|
|
/* Yes... just increment the count */
|
|
|
|
|
|
|
|
g_inode_sem.count++;
|
2013-11-28 20:30:06 +01:00
|
|
|
DEBUGASSERT(g_inode_sem.count > 0);
|
2013-11-28 20:12:14 +01:00
|
|
|
}
|
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2013-11-28 20:12:14 +01:00
|
|
|
else
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-11-28 20:12:14 +01:00
|
|
|
while (sem_wait(&g_inode_sem.sem) != 0)
|
|
|
|
{
|
|
|
|
/* The only case that an error should occr here is if
|
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(get_errno() == EINTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No we hold the semaphore */
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2013-11-28 20:12:14 +01:00
|
|
|
g_inode_sem.holder = me;
|
|
|
|
g_inode_sem.count = 1;
|
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
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2013-11-28 20:12:14 +01:00
|
|
|
* Relinquish exclusive access to the in-memory inode tree (g_inode_sem).
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
|
|
|
void inode_semgive(void)
|
|
|
|
{
|
2013-11-28 20:12:14 +01:00
|
|
|
DEBUGASSERT(g_inode_sem.holder == getpid());
|
|
|
|
|
|
|
|
/* Is this our last count on the semaphore? */
|
|
|
|
|
|
|
|
if (g_inode_sem.count > 1)
|
|
|
|
{
|
|
|
|
/* No.. just decrement the count */
|
|
|
|
|
|
|
|
g_inode_sem.count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. then we can really release the semaphore */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_inode_sem.holder = NO_HOLDER;
|
|
|
|
g_inode_sem.count = 0;
|
|
|
|
sem_post(&g_inode_sem.sem);
|
|
|
|
}
|
2007-03-14 23:41:09 +01:00
|
|
|
}
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2017-02-02 20:01:21 +01:00
|
|
|
* Name: inode_search and inode_search_nofollow
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 23:05:40 +02:00
|
|
|
* Find the inode associated with 'path' returning the inode references
|
|
|
|
* and references to its companion nodes.
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2017-02-02 20:01:21 +01:00
|
|
|
* Both versions will follow soft links in path leading up to the terminal
|
|
|
|
* node. inode_search() will deference that terminal node,
|
|
|
|
* inode_search_nofollow will not.
|
|
|
|
*
|
2007-03-14 23:41:09 +01:00
|
|
|
* Assumptions:
|
2013-11-28 20:12:14 +01:00
|
|
|
* The caller holds the g_inode_sem semaphore
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-03-14 23:41:09 +01:00
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
|
|
|
|
FAR struct inode *inode_search_nofollow(FAR const char **path,
|
|
|
|
FAR struct inode **peer,
|
|
|
|
FAR struct inode **parent,
|
|
|
|
FAR const char **relpath)
|
|
|
|
#else
|
2014-07-29 20:49:40 +02:00
|
|
|
FAR struct inode *inode_search(FAR const char **path,
|
2007-03-14 23:41:09 +01:00
|
|
|
FAR struct inode **peer,
|
2007-05-09 02:04:03 +02:00
|
|
|
FAR struct inode **parent,
|
2014-07-29 20:49:40 +02:00
|
|
|
FAR const char **relpath)
|
2017-02-02 20:01:21 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2014-07-29 20:49:40 +02:00
|
|
|
FAR const char *name = *path + 1; /* Skip over leading '/' */
|
2015-06-11 19:24:20 +02:00
|
|
|
FAR struct inode *node = g_root_inode;
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR struct inode *left = NULL;
|
|
|
|
FAR struct inode *above = NULL;
|
2017-02-03 02:37:58 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
FAR struct inode *newnode;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* Handle the case were the root node is a symbolic link */
|
|
|
|
|
|
|
|
#warning Missing logic
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Traverse the pseudo file system node tree until either (1) all nodes
|
|
|
|
* have been examined without finding the matching node, or (2) the
|
|
|
|
* matching node is found.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
while (node != NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2017-02-02 20:01:21 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* If the inode in the is a soft link and this is the inode at
|
|
|
|
* at the head of the peer list and not the final node in the
|
|
|
|
* path), then (1) get the name of the full path of the soft
|
|
|
|
* link, (2) recursively look-up the inode referenced by the
|
|
|
|
* soft link, and (3) use the peer of that inode instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR const char *nextname = inode_nextname(name);
|
|
|
|
if (*nextname != '\0')
|
|
|
|
{
|
2017-02-03 02:37:58 +01:00
|
|
|
newnode = _inode_dereference(node, NULL, &above, relpath);
|
|
|
|
if (newnode == NULL)
|
2017-02-02 20:01:21 +01:00
|
|
|
{
|
2017-02-03 02:37:58 +01:00
|
|
|
/* Probably means that the node is a symbolic link, but
|
|
|
|
* that the target of the symbolic link does not exist.
|
|
|
|
*/
|
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-02-03 02:37:58 +01:00
|
|
|
else if (newnode != node)
|
|
|
|
{
|
|
|
|
/* The node was a valid symbolic link and we have jumped to a
|
|
|
|
* different, spot in the the pseudo file system tree. Reset
|
|
|
|
* everything and continue looking at the next level "down"
|
|
|
|
* from that new spot in the tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
above = newnode;
|
|
|
|
left = NULL;
|
|
|
|
node = newnode->i_child;
|
|
|
|
continue;
|
|
|
|
}
|
2017-02-02 20:01:21 +01:00
|
|
|
}
|
|
|
|
#endif
|
2017-02-03 02:37:58 +01:00
|
|
|
/* Continue looking to the left */
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
left = node;
|
|
|
|
node = node->i_peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The names match */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 20:01:21 +01:00
|
|
|
/* Now there are three remaining possibilities:
|
|
|
|
* (1) This is the node that we are looking for.
|
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);
|
2017-02-02 20:01:21 +01:00
|
|
|
if (*name == '\0' || 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;
|
|
|
|
}
|
2017-02-02 17:39:41 +01:00
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* NOTE that if the terminal inode is a soft link, it is not
|
|
|
|
* deferenced in this case. The raw inode is returned.
|
|
|
|
*
|
|
|
|
* In that case a wrapper function will perform that operation.
|
|
|
|
*/
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-03 02:37:58 +01:00
|
|
|
/* More nodes to be examined in the path... */
|
2017-02-02 20:01:21 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* If this intermediate inode in the is a soft link, then (1)
|
|
|
|
* get the name of the full path of the soft link, (2) recursively
|
|
|
|
* look-up the inode referenced by the sof link, and (3)
|
|
|
|
* continue searching with that inode instead.
|
|
|
|
*/
|
|
|
|
|
2017-02-03 02:37:58 +01:00
|
|
|
newnode = _inode_dereference(node, NULL, NULL, relpath);
|
|
|
|
if (newnode == NULL)
|
2017-02-02 20:01:21 +01:00
|
|
|
{
|
2017-02-03 02:37:58 +01:00
|
|
|
/* Probably means that the node is a symbolic link, but
|
|
|
|
* that the target of the symbolic link does not exist.
|
|
|
|
*/
|
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-02-03 02:37:58 +01:00
|
|
|
else if (newnode != node)
|
|
|
|
{
|
|
|
|
/* The node was a valid symbolic link and we have jumped to a
|
|
|
|
* different, spot in the the pseudo file system tree. Reset
|
|
|
|
* everything and continue looking to the right (if possible)
|
|
|
|
* otherwise at the next level "down" from that new spot in
|
|
|
|
* the tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (newnode->i_peer != NULL)
|
|
|
|
{
|
|
|
|
above = NULL; /* REVISIT: This can't be right */
|
|
|
|
left = newnode;
|
|
|
|
node = newnode->i_peer;
|
|
|
|
|
|
|
|
/* Did the symbolic link take us to a mountpoint? */
|
|
|
|
|
|
|
|
if (INODE_IS_MOUNTPT(newnode))
|
|
|
|
{
|
|
|
|
/* Yes.. return the mountpoint information */
|
|
|
|
|
|
|
|
if (relpath)
|
|
|
|
{
|
|
|
|
*relpath = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 20:01:21 +01:00
|
|
|
#endif
|
|
|
|
/* Keep looking at the next level "down" */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
above = node;
|
|
|
|
left = NULL;
|
2017-02-02 20:01:21 +01:00
|
|
|
node = node->i_child;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
if (peer)
|
|
|
|
{
|
|
|
|
*peer = left;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
*parent = above;
|
|
|
|
}
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
*path = name;
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2017-02-02 20:01:21 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
FAR struct inode *inode_search(FAR const char **path,
|
|
|
|
FAR struct inode **peer,
|
|
|
|
FAR struct inode **parent,
|
|
|
|
FAR const char **relpath)
|
|
|
|
{
|
|
|
|
/* Lookup the terminal inode */
|
|
|
|
|
|
|
|
FAR struct inode *node = inode_search_nofollow(path, peer, parent, relpath);
|
|
|
|
|
|
|
|
/* Did we find it? */
|
|
|
|
|
|
|
|
if (node != NULL)
|
|
|
|
{
|
|
|
|
/* Yes.. If the terminal inode in the is a soft link, then (1) get
|
|
|
|
* the name of the full path of the soft link, (2) recursively
|
|
|
|
* look-up the inode referenced by the soft link, and (3)
|
|
|
|
* return that inode instead.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return _inode_dereference(node, peer, parent, relpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-05-31 22:14:15 +02:00
|
|
|
/****************************************************************************
|
2007-03-14 23:41:09 +01:00
|
|
|
* Name: inode_free
|
2012-08-04 00:04:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free resources used by an inode
|
|
|
|
*
|
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
|
|
|
{
|
2015-06-11 19:24:20 +02:00
|
|
|
/* Verify that we were passed valid pointer to an inode */
|
|
|
|
|
|
|
|
if (node != NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-02-02 17:39:41 +01:00
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* Symbol links should never have peers or children */
|
|
|
|
|
2017-02-02 22:24:39 +01:00
|
|
|
DEBUGASSERT(!INODE_IS_SOFTLINK(node) ||
|
|
|
|
(node->i_peer == NULL && node->i_child == NULL));
|
2017-02-02 17:39:41 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Free all peers and children of this i_node */
|
|
|
|
|
2007-03-14 23:41:09 +01:00
|
|
|
inode_free(node->i_peer);
|
|
|
|
inode_free(node->i_child);
|
2017-02-02 17:39:41 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
/* If the inode is a symbolic link, the free the path to the linked
|
|
|
|
* entity.
|
|
|
|
*/
|
|
|
|
|
2017-02-02 22:24:39 +01:00
|
|
|
if (INODE_IS_SOFTLINK(node) && node->u.i_link != NULL)
|
2017-02-02 17:39:41 +01:00
|
|
|
{
|
|
|
|
kmm_free(node->u.i_link);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(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:
|
2014-02-19 20:14:39 +01:00
|
|
|
* Given a path with node names separated by '/', return the next path
|
|
|
|
* segment name.
|
2007-03-14 23:41:09 +01:00
|
|
|
*
|
2008-05-31 22:14:15 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
FAR const char *inode_nextname(FAR const char *name)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2012-07-14 23:05:40 +02:00
|
|
|
/* Search for the '/' delimiter or the NUL terminator at the end of the
|
2014-02-19 20:14:39 +01:00
|
|
|
* path segment.
|
2012-07-14 23:05:40 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
while (*name && *name != '/')
|
|
|
|
{
|
|
|
|
name++;
|
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
/* If we found the '/' delimiter, then the path segment we want begins at
|
|
|
|
* the next character (which might also be the NUL terminator).
|
|
|
|
*/
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
if (*name)
|
|
|
|
{
|
|
|
|
name++;
|
|
|
|
}
|
2012-07-14 23:05:40 +02:00
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
return name;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|