2007-05-12 22:07:51 +02:00
|
|
|
/****************************************************************************
|
2015-03-14 23:48:45 +01:00
|
|
|
* fs/mount/fs_umount2.c
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2017-02-04 18:46:54 +01:00
|
|
|
* Copyright (C) 2007-2009, 2015, 2017 Gregory Nutt. All rights reserved.
|
2012-07-14 23:05:40 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-05-12 22:07:51 +02: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-09-12 16:46:44 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-05-12 22:07:51 +02: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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2007-05-12 22:07:51 +02:00
|
|
|
#include <sys/mount.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2007-05-12 22:07:51 +02:00
|
|
|
#include <errno.h>
|
2015-06-11 18:10:25 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2009-12-15 01:18:32 +01:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-03-14 23:48:45 +01:00
|
|
|
* Name: umount2
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* umount() detaches the filesystem mounted at the path specified by
|
|
|
|
* 'target.'
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-05-12 22:07:51 +02:00
|
|
|
* Zero is returned on success; -1 is returned on an error and errno is
|
|
|
|
* set appropriately:
|
|
|
|
*
|
|
|
|
* EACCES A component of a path was not searchable or mounting a read-only
|
|
|
|
* filesystem was attempted without giving the MS_RDONLY flag.
|
|
|
|
* EBUSY The target could not be unmounted because it is busy.
|
|
|
|
* EFAULT The pointer argument points outside the user address space.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-14 23:48:45 +01:00
|
|
|
int umount2(FAR const char *target, unsigned int flags)
|
2007-05-12 22:07:51 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *mountpt_inode;
|
2007-05-27 21:10:40 +02:00
|
|
|
FAR struct inode *blkdrvr_inode = NULL;
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2007-05-27 21:10:40 +02:00
|
|
|
int errcode = OK;
|
2015-03-15 00:22:02 +01:00
|
|
|
int ret;
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
/* Verify required pointer arguments */
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
errcode = EFAULT;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the mountpt */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, target, false);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret < 0)
|
2007-05-12 22:07:51 +02:00
|
|
|
{
|
|
|
|
errcode = ENOENT;
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2007-05-12 22:07:51 +02:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
/* Get the search results */
|
|
|
|
|
|
|
|
mountpt_inode = desc.node;
|
|
|
|
DEBUGASSERT(mountpt_inode != NULL);
|
|
|
|
|
2007-05-12 22:07:51 +02:00
|
|
|
/* Verify that the inode is a mountpoint */
|
|
|
|
|
|
|
|
if (!INODE_IS_MOUNTPT(mountpt_inode))
|
2014-04-13 21:18:06 +02:00
|
|
|
{
|
2007-05-12 22:07:51 +02:00
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout_with_mountpt;
|
2014-04-13 21:18:06 +02:00
|
|
|
}
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
/* Unbind the block driver from the file system (destroying any fs
|
|
|
|
* private data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!mountpt_inode->u.i_mops->unbind)
|
|
|
|
{
|
|
|
|
/* The filesystem does not support the unbind operation ??? */
|
|
|
|
|
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout_with_mountpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The unbind method returns the number of references to the
|
|
|
|
* filesystem (i.e., open files), zero if the unbind was
|
|
|
|
* performed, or a negated error code on a failure.
|
|
|
|
*/
|
|
|
|
|
2007-05-14 13:12:09 +02:00
|
|
|
inode_semtake(); /* Hold the semaphore through the unbind logic */
|
2015-03-15 00:22:02 +01:00
|
|
|
ret = mountpt_inode->u.i_mops->unbind(mountpt_inode->i_private,
|
|
|
|
&blkdrvr_inode, flags);
|
|
|
|
if (ret < 0)
|
2007-05-12 22:07:51 +02:00
|
|
|
{
|
|
|
|
/* The inode is unhappy with the blkdrvr for some reason */
|
|
|
|
|
2015-03-15 00:22:02 +01:00
|
|
|
errcode = -ret;
|
2007-05-14 13:12:09 +02:00
|
|
|
goto errout_with_semaphore;
|
2007-05-12 22:07:51 +02:00
|
|
|
}
|
2015-03-15 00:22:02 +01:00
|
|
|
else if (ret > 0)
|
2007-05-12 22:07:51 +02:00
|
|
|
{
|
|
|
|
errcode = EBUSY;
|
2007-05-14 13:12:09 +02:00
|
|
|
goto errout_with_semaphore;
|
2007-05-12 22:07:51 +02:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:10:25 +02:00
|
|
|
/* Successfully unbound. Convert the mountpoint inode to regular
|
|
|
|
* pseudo-file inode.
|
|
|
|
*/
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
|
2015-06-11 18:10:25 +02:00
|
|
|
mountpt_inode->i_flags &= ~FSNODEFLAG_TYPE_MASK;
|
|
|
|
mountpt_inode->i_private = NULL;
|
|
|
|
mountpt_inode->u.i_mops = NULL;
|
2007-05-14 13:12:09 +02:00
|
|
|
|
2015-06-11 18:10:25 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
/* If the node has children, then do not delete it. */
|
2007-05-27 21:10:40 +02:00
|
|
|
|
2015-06-11 18:10:25 +02:00
|
|
|
if (mountpt_inode->i_child != NULL)
|
|
|
|
{
|
|
|
|
/* Just decrement the reference count (without deleting it) */
|
2007-05-27 21:10:40 +02:00
|
|
|
|
2015-06-11 18:10:25 +02:00
|
|
|
DEBUGASSERT(mountpt_inode->i_crefs > 0);
|
|
|
|
mountpt_inode->i_crefs--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2007-05-27 21:10:40 +02:00
|
|
|
{
|
2015-06-11 18:10:25 +02:00
|
|
|
/* Remove the mountpoint inode from the inode tree. The inode will
|
|
|
|
* not be deleted yet because there is still at least reference on
|
|
|
|
* it (from the mount)
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = inode_remove(target);
|
|
|
|
inode_semgive();
|
|
|
|
|
|
|
|
/* The return value of -EBUSY is normal (in fact, it should
|
|
|
|
* not be OK)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != OK && ret != -EBUSY)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_mountpt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the mountpoint inode and any block driver inode
|
|
|
|
* returned by the file system unbind above. This should cause
|
|
|
|
* the inode to be deleted (unless there are other references)
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode_release(mountpt_inode);
|
2007-05-27 21:10:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Did the unbind method return a contained block driver */
|
|
|
|
|
|
|
|
if (blkdrvr_inode)
|
|
|
|
{
|
|
|
|
inode_release(blkdrvr_inode);
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2007-05-27 21:10:40 +02:00
|
|
|
return OK;
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
/* A lot of goto's! But they make the error handling much simpler */
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
errout_with_semaphore:
|
2007-05-12 22:07:51 +02:00
|
|
|
inode_semgive();
|
2017-02-05 21:25:45 +01:00
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
errout_with_mountpt:
|
2007-05-14 13:12:09 +02:00
|
|
|
inode_release(mountpt_inode);
|
2007-05-27 21:10:40 +02:00
|
|
|
if (blkdrvr_inode)
|
|
|
|
{
|
|
|
|
inode_release(blkdrvr_inode);
|
|
|
|
}
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
|
|
|
|
2012-07-14 23:05:40 +02:00
|
|
|
errout:
|
|
|
|
set_errno(errcode);
|
2007-05-12 22:07:51 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|