From b76c4672d64d226e6ea5c44fa7d3b95e2df8482c Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Tue, 15 Sep 2020 17:42:42 +0800 Subject: [PATCH] vfs: Create a node as the root of pseudo file system to remove the special process for root Signed-off-by: Xiang Xiao --- fs/dirent/fs_opendir.c | 56 +++++++++++--------------------------- fs/inode/fs_inode.c | 4 +++ fs/inode/fs_inoderemove.c | 17 ++++-------- fs/inode/fs_inodereserve.c | 32 ++++++++++++---------- fs/inode/fs_inodesearch.c | 24 +++------------- fs/inode/inode.h | 10 +++++++ fs/vfs/fs_rename.c | 32 ---------------------- fs/vfs/fs_stat.c | 21 -------------- fs/vfs/fs_statfs.c | 7 ----- 9 files changed, 57 insertions(+), 146 deletions(-) diff --git a/fs/dirent/fs_opendir.c b/fs/dirent/fs_opendir.c index a8af368ae0d..9082aa48c00 100644 --- a/fs/dirent/fs_opendir.c +++ b/fs/dirent/fs_opendir.c @@ -218,7 +218,6 @@ FAR DIR *opendir(FAR const char *path) FAR const char *relpath = NULL; #endif FAR char *alloc = NULL; - bool isroot = false; int len; int ret; @@ -277,32 +276,24 @@ FAR DIR *opendir(FAR const char *path) goto errout_with_alloc; } - if (path == NULL || *path == '\0' || strcmp(path, "/") == 0) + /* We don't know what to do with relative paths */ + + if (*path != '/') { - inode = g_root_inode; - isroot = true; + ret = ENOTDIR; + goto errout_with_semaphore; } - else + + /* Find the node matching the path. */ + + ret = inode_search(&desc); + if (ret >= 0) { - /* We don't know what to do with relative paths */ - - if (*path != '/') - { - ret = ENOTDIR; - goto errout_with_semaphore; - } - - /* Find the node matching the path. */ - - ret = inode_search(&desc); - if (ret >= 0) - { - inode = desc.node; - DEBUGASSERT(inode != NULL); + inode = desc.node; + DEBUGASSERT(inode != NULL); #ifndef CONFIG_DISABLE_MOUNTPOINT - relpath = desc.relpath; + relpath = desc.relpath; #endif - } } /* Did we get an inode? */ @@ -335,25 +326,10 @@ FAR DIR *opendir(FAR const char *path) dir->fd_position = 0; /* This is the position in the read stream */ - /* First, handle the special case of the root inode. This must be - * special-cased here because the root inode might ALSO be a mountpoint. - */ - - if (isroot) - { - /* Whatever payload the root inode carries, the root inode is always - * a directory inode in the pseudo-file system - */ - - open_pseudodir(inode, dir); - } - - /* Is this a node in the pseudo filesystem? Or a mountpoint? If the node - * is the root (isroot == TRUE), then this is a special case. - */ + /* Is this a node in the pseudo filesystem? Or a mountpoint? */ #ifndef CONFIG_DISABLE_MOUNTPOINT - else if (INODE_IS_MOUNTPT(inode)) + if (INODE_IS_MOUNTPT(inode)) { /* Yes, the node is a file system mountpoint */ @@ -367,8 +343,8 @@ FAR DIR *opendir(FAR const char *path) goto errout_with_direntry; } } -#endif else +#endif { /* The node is part of the root pseudo file system. Does the inode * have a child? If so that the child would be the 'root' of a list diff --git a/fs/inode/fs_inode.c b/fs/inode/fs_inode.c index e048004685c..1e8793f549e 100644 --- a/fs/inode/fs_inode.c +++ b/fs/inode/fs_inode.c @@ -87,6 +87,10 @@ void inode_initialize(void) g_inode_sem.holder = NO_HOLDER; g_inode_sem.count = 0; + /* Reserve the root node */ + + inode_root_reserve(); + /* Initialize files array (if it is used) */ #ifdef CONFIG_HAVE_WEAKFUNCTIONS diff --git a/fs/inode/fs_inoderemove.c b/fs/inode/fs_inoderemove.c index 8effd4c7530..e11f7f8c75e 100644 --- a/fs/inode/fs_inoderemove.c +++ b/fs/inode/fs_inoderemove.c @@ -100,20 +100,12 @@ FAR struct inode *inode_unlink(FAR const char *path) desc.peer->i_peer = node->i_peer; } - /* If parent is non-null, then remove the node from head of - * of the list of children. - */ - - else if (desc.parent) - { - desc.parent->i_child = node->i_peer; - } - - /* Otherwise, we must be removing the root inode. */ + /* Then remove the node from head of the list of children. */ else { - g_root_inode = node->i_peer; + DEBUGASSERT(desc.parent != NULL); + desc.parent->i_child = node->i_peer; } node->i_peer = NULL; @@ -162,7 +154,8 @@ int inode_remove(FAR const char *path) else { /* And delete it now -- recursively to delete all of its children. - * Since it has been unlinked, then the peer pointer should be NULL. + * Since it has been unlinked, then the peer pointer should be + * NULL. */ DEBUGASSERT(node->i_peer == NULL); diff --git a/fs/inode/fs_inodereserve.c b/fs/inode/fs_inodereserve.c index ef8c17e61fb..3f2c789d42a 100644 --- a/fs/inode/fs_inodereserve.c +++ b/fs/inode/fs_inodereserve.c @@ -1,5 +1,5 @@ /**************************************************************************** - * fs/inode/fs_registerreserve.c + * fs/inode/fs_inodereserve.c * * Copyright (C) 2007-2009, 2011-2012, 2015, 2017 Gregory Nutt. All * rights reserved. @@ -118,22 +118,13 @@ static void inode_insert(FAR struct inode *node, peer->i_peer = node; } - /* If parent is non-null, then it must go at the head of its - * list of children. - */ - - else if (parent) - { - node->i_peer = parent->i_child; - parent->i_child = node; - } - - /* Otherwise, this must be the new root_inode */ + /* Then it must go at the head of parent's list of children. */ else { - node->i_peer = g_root_inode; - g_root_inode = node; + DEBUGASSERT(parent != NULL); + node->i_peer = parent->i_child; + parent->i_child = node; } } @@ -141,6 +132,19 @@ static void inode_insert(FAR struct inode *node, * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: inode_root_reserve + * + * Description: + * Reserve the root inode for the pseudo file system. + * + ****************************************************************************/ + +void inode_root_reserve(void) +{ + g_root_inode = inode_alloc(""); +} + /**************************************************************************** * Name: inode_reserve * diff --git a/fs/inode/fs_inodesearch.c b/fs/inode/fs_inodesearch.c index c525478022c..68810c64a82 100644 --- a/fs/inode/fs_inodesearch.c +++ b/fs/inode/fs_inodesearch.c @@ -1,7 +1,8 @@ /**************************************************************************** * fs/inode/fs_inodesearch.c * - * Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011-2012, 2016-2017 Gregory Nutt. + * All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -255,24 +256,6 @@ static int _inode_search(FAR struct inode_search_s *desc) return -EINVAL; } - /* Skip over the leading '/' */ - - while (*name == '/') - { - name++; - } - - /* Special case the root directory. There is no root inode and there is - * no name for the root. - */ - - if (*name == '\0') - { - /* This is a bug. I don't know how to handle this case yet. */ - - return -ENOSYS; - } - /* 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. @@ -347,7 +330,8 @@ static int _inode_search(FAR struct inode_search_s *desc) /* 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 soft - * link, and (3) continue searching with that inode instead. + * link, and (3) continue searching with that inode + * instead. */ status = _inode_linktarget(node, desc); diff --git a/fs/inode/inode.h b/fs/inode/inode.h index eb9ef58f62f..6275c4e59ab 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -283,6 +283,16 @@ void inode_free(FAR struct inode *node); const char *inode_nextname(FAR const char *name); +/**************************************************************************** + * Name: inode_root_reserve + * + * Description: + * Reserve the root node for the pseudo file system. + * + ****************************************************************************/ + +void inode_root_reserve(void); + /**************************************************************************** * Name: inode_reserve * diff --git a/fs/vfs/fs_rename.c b/fs/vfs/fs_rename.c index cabc8ba5e79..0007ada6e18 100644 --- a/fs/vfs/fs_rename.c +++ b/fs/vfs/fs_rename.c @@ -65,40 +65,8 @@ static int pseudorename(FAR const char *oldpath, FAR struct inode *oldinode, struct inode_search_s newdesc; FAR struct inode *newinode; FAR char *subdir = NULL; - FAR const char *name; int ret; - /* Special case the root directory. There is no root inode and there is - * no name for the root. inode_find() will fail to the find the root - * inode -- because there isn't one. - */ - - name = newpath; - while (*name == '/') - { - name++; - } - - if (*name == '\0') - { - FAR char *subdirname; - - /* In the newpath is the root directory, the target of the rename must - * be a directory entry under the root. - */ - - subdirname = basename((FAR char *)oldpath); - - asprintf(&subdir, "/%s", subdirname); - if (subdir == NULL) - { - ret = -ENOMEM; - goto errout; - } - - newpath = subdir; - } - /* According to POSIX, any old inode at this path should be removed * first, provided that it is not a directory. */ diff --git a/fs/vfs/fs_stat.c b/fs/vfs/fs_stat.c index de62586adf5..c1a6b54831d 100644 --- a/fs/vfs/fs_stat.c +++ b/fs/vfs/fs_stat.c @@ -57,7 +57,6 @@ * Private Function Prototypes ****************************************************************************/ -static inline int statroot(FAR struct stat *buf); static int stat_recursive(FAR const char *path, FAR struct stat *buf, int resolve); @@ -65,19 +64,6 @@ static int stat_recursive(FAR const char *path, * Private Functions ****************************************************************************/ -/**************************************************************************** - * Name: statroot - ****************************************************************************/ - -static inline int statroot(FAR struct stat *buf) -{ - /* There is no inode associated with the fake root directory */ - - RESET_BUF(buf); - buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR; - return OK; -} - /**************************************************************************** * Name: stat_recursive * @@ -187,13 +173,6 @@ int nx_stat(FAR const char *path, FAR struct stat *buf, int resolve) return -ENOENT; } - /* Check for the fake root directory (which has no inode) */ - - if (strcmp(path, "/") == 0) - { - return statroot(buf); - } - /* The perform the stat() operation on the path. This is potentially * recursive if soft link support is enabled. */ diff --git a/fs/vfs/fs_statfs.c b/fs/vfs/fs_statfs.c index ce7e5d28dcd..8d8446369ae 100644 --- a/fs/vfs/fs_statfs.c +++ b/fs/vfs/fs_statfs.c @@ -104,13 +104,6 @@ int statfs(FAR const char *path, FAR struct statfs *buf) goto errout; } - /* Check for the fake root directory (which has no inode) */ - - if (strcmp(path, "/") == 0) - { - return statpseudofs(NULL, buf); - } - /* Get an inode for this file */ SETUP_SEARCH(&desc, path, false);