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
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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"
|
2023-08-18 14:14:38 +02:00
|
|
|
#include "notify/notify.h"
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-05-04 15:49:07 +02:00
|
|
|
* Name: nx_umount2
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-05-04 15:49:07 +02:00
|
|
|
* nx_umount2() is similar to the standard 'umount2' interface except that
|
|
|
|
* is not a cancellation point and it does not modify the errno variable.
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2020-05-04 15:49:07 +02:00
|
|
|
* nx_umount2() is an internal NuttX interface and should not be called
|
|
|
|
* from applications.
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
2020-05-04 15:49:07 +02:00
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success; a negated value is returned on any failure.
|
2007-05-12 22:07:51 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-04 15:49:07 +02:00
|
|
|
int nx_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;
|
2015-03-15 00:22:02 +01:00
|
|
|
int ret;
|
2007-05-12 22:07:51 +02:00
|
|
|
|
|
|
|
/* Verify required pointer arguments */
|
|
|
|
|
|
|
|
if (!target)
|
|
|
|
{
|
2020-05-04 15:49:07 +02:00
|
|
|
ret = -EFAULT;
|
2007-05-12 22:07:51 +02:00
|
|
|
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
|
|
|
{
|
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
|
|
|
{
|
2020-05-04 15:49:07 +02:00
|
|
|
ret = -EINVAL;
|
2007-05-12 22:07:51 +02:00
|
|
|
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 ??? */
|
|
|
|
|
2020-05-04 15:49:07 +02:00
|
|
|
ret = -EINVAL;
|
2007-05-12 22:07:51 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
/* Hold the semaphore through the unbind logic */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = inode_lock();
|
2020-03-30 01:36:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_mountpt;
|
|
|
|
}
|
|
|
|
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
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
|
|
|
{
|
2020-05-04 15:49:07 +02:00
|
|
|
ret = -EBUSY;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
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)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Just decrement the reference count (without deleting it) */
|
2007-05-27 21:10:40 +02:00
|
|
|
|
2024-07-23 13:30:19 +02:00
|
|
|
atomic_fetch_sub(&mountpt_inode->i_crefs, 1);
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_unlock();
|
2015-06-11 18:10:25 +02:00
|
|
|
}
|
|
|
|
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);
|
2022-09-06 08:18:45 +02:00
|
|
|
inode_unlock();
|
2015-06-11 18:10:25 +02:00
|
|
|
|
|
|
|
/* The return value of -EBUSY is normal (in fact, it should
|
|
|
|
* not be OK)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != OK && ret != -EBUSY)
|
|
|
|
{
|
|
|
|
goto errout_with_mountpt;
|
2019-10-27 18:48:14 +01:00
|
|
|
}
|
2015-06-11 18:10:25 +02:00
|
|
|
|
|
|
|
/* 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);
|
2023-08-18 14:14:38 +02:00
|
|
|
#ifdef CONFIG_FS_NOTIFY
|
|
|
|
notify_unmount(target);
|
|
|
|
#endif
|
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 */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
inode_unlock();
|
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:
|
2020-05-04 15:49:07 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: umount2
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* umount() detaches the filesystem mounted at the path specified by
|
|
|
|
* 'target.'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int umount2(FAR const char *target, unsigned int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nx_umount2(target, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2007-05-12 22:07:51 +02:00
|
|
|
}
|