2014-07-30 02:50:08 +08:00
|
|
|
/****************************************************************************
|
2014-09-29 01:28:17 +08:00
|
|
|
* fs/mount/fs_automount.c
|
2014-07-30 02:50:08 +08:00
|
|
|
*
|
2017-02-05 01:21:44 +08:00
|
|
|
* Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
|
2014-07-30 02:50:08 +08:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#if defined(CONFIG_FS_AUTOMOUNTER_DEBUG) && !defined(CONFIG_DEBUG_FS)
|
|
|
|
# define CONFIG_DEBUG_FS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-08-22 01:16:55 +08:00
|
|
|
#include <nuttx/wdog.h>
|
2014-07-30 02:50:08 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
#include <nuttx/fs/automount.h>
|
|
|
|
|
2014-09-29 21:14:38 +08:00
|
|
|
#include "inode/inode.h"
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
/* Configuration ************************************************************
|
|
|
|
*
|
|
|
|
* CONFIG_FS_AUTOMOUNTER - Enables AUTOMOUNT support
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pre-requisites */
|
|
|
|
|
|
|
|
#ifndef CONFIG_SCHED_WORKQUEUE
|
|
|
|
# error Work queue support is required (CONFIG_SCHED_WORKQUEUE)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Return Values */
|
|
|
|
|
|
|
|
#define OK_EXIST 0
|
|
|
|
#define OK_NOENT 1
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
/* This structure describes the state of the automounter */
|
|
|
|
|
|
|
|
struct automounter_state_s
|
|
|
|
{
|
|
|
|
FAR const struct automount_lower_s *lower; /* Board level interfaces */
|
|
|
|
struct work_s work; /* Work queue support */
|
|
|
|
WDOG_ID wdog; /* Delay to retry un-mounts */
|
|
|
|
bool mounted; /* True: Volume has been mounted */
|
|
|
|
bool inserted; /* True: Media has been inserted */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int automount_findinode(FAR const char *path);
|
|
|
|
static void automount_mount(FAR struct automounter_state_s *priv);
|
|
|
|
static int automount_unmount(FAR struct automounter_state_s *priv);
|
|
|
|
static void automount_timeout(int argc, uint32_t arg1, ...);
|
|
|
|
static void automount_worker(FAR void *arg);
|
|
|
|
static int automount_interrupt(FAR const struct automount_lower_s *lower,
|
|
|
|
FAR void *arg, bool inserted);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_findinode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the mountpoint inode in the inode tree.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mntpath - Mountpoint path
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK_EXIST if the inode exists
|
|
|
|
* OK_NOENT if the indoe does not exist
|
|
|
|
* Negated errno if some failure occurs
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int automount_findinode(FAR const char *path)
|
|
|
|
{
|
2017-02-05 01:21:44 +08:00
|
|
|
struct inode_search_s desc;
|
2014-07-30 02:50:08 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make sure that we were given an absolute path */
|
|
|
|
|
|
|
|
DEBUGASSERT(path && path[0] == '/');
|
|
|
|
|
|
|
|
/* Get exclusive access to the in-memory inode tree. */
|
|
|
|
|
|
|
|
inode_semtake();
|
|
|
|
|
|
|
|
/* Find the inode */
|
|
|
|
|
2017-02-06 04:25:45 +08:00
|
|
|
SETUP_SEARCH(&desc, path, false);
|
2017-02-05 01:21:44 +08:00
|
|
|
|
|
|
|
ret = inode_search(&desc);
|
|
|
|
|
2014-07-30 02:50:08 +08:00
|
|
|
/* Did we find it? */
|
|
|
|
|
2017-02-05 01:21:44 +08:00
|
|
|
if (ret < 0)
|
2014-07-30 02:50:08 +08:00
|
|
|
{
|
|
|
|
/* No.. Not found */
|
|
|
|
|
|
|
|
ret = OK_NOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. is it a mount point? */
|
|
|
|
|
2017-02-05 01:21:44 +08:00
|
|
|
else if (INODE_IS_MOUNTPT(desc.node))
|
2014-07-30 02:50:08 +08:00
|
|
|
{
|
|
|
|
/* Yes.. we found a mountpoint at this path */
|
|
|
|
|
|
|
|
ret = OK_EXIST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-05 01:21:44 +08:00
|
|
|
/* No.. then something is in the way */
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
ret = -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Relinquish our exclusive access to the inode try and return the result */
|
|
|
|
|
|
|
|
inode_semgive();
|
2017-02-06 04:25:45 +08:00
|
|
|
RELEASE_SEARCH(&desc);
|
2014-07-30 02:50:08 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_mount
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Media has been inserted, mount the volume.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to out private state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void automount_mount(FAR struct automounter_state_s *priv)
|
|
|
|
{
|
|
|
|
FAR const struct automount_lower_s *lower = priv->lower;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Mounting %s\n", lower->mountpoint);
|
2014-07-30 05:34:31 +08:00
|
|
|
|
2014-07-30 02:50:08 +08:00
|
|
|
/* Check if the something is already mounted at the mountpoint. */
|
|
|
|
|
|
|
|
ret = automount_findinode(lower->mountpoint);
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case OK_EXIST:
|
|
|
|
/* REVISIT: What should we do in this case? I think that this would
|
|
|
|
* happen only if a previous unmount failed? I suppose that we should
|
|
|
|
* try to unmount again because the mount might be stale.
|
|
|
|
*/
|
|
|
|
|
2016-06-12 07:14:02 +08:00
|
|
|
fwarn("WARNING: Mountpoint %s already exists\n", lower->mountpoint);
|
2014-07-30 02:50:08 +08:00
|
|
|
ret = automount_unmount(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* We failed to unmount (again?). Complain and abort. */
|
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: automount_unmount failed: %d\n", ret);
|
2014-07-30 02:50:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We successfully unmounted the file system. Fall through to
|
|
|
|
* mount it again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case OK_NOENT:
|
|
|
|
/* If we get here, then the volume must not be mounted */
|
|
|
|
|
|
|
|
DEBUGASSERT(!priv->mounted);
|
|
|
|
|
|
|
|
/* Mount the file system */
|
|
|
|
|
|
|
|
ret = mount(lower->blockdev, lower->mountpoint, lower->fstype,
|
|
|
|
0, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = get_errno();
|
|
|
|
DEBUGASSERT(errcode > 0);
|
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Mount failed: %d\n", errcode);
|
2014-07-30 02:50:08 +08:00
|
|
|
UNUSED(errcode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Indicate that the volume is mounted */
|
|
|
|
|
|
|
|
priv->mounted = true;
|
|
|
|
break;
|
2015-10-05 05:28:54 +08:00
|
|
|
|
2014-07-30 02:50:08 +08:00
|
|
|
default:
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: automount_findinode failed: %d\n", ret);
|
2014-07-30 02:50:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_unmount
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Media has been removed, unmount the volume.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* priv - A reference to out private state structure
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK if the volume was successfully mounted. A negated errno value
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int automount_unmount(FAR struct automounter_state_s *priv)
|
|
|
|
{
|
|
|
|
FAR const struct automount_lower_s *lower = priv->lower;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Unmounting %s\n", lower->mountpoint);
|
2014-07-30 05:34:31 +08:00
|
|
|
|
2014-07-30 02:50:08 +08:00
|
|
|
/* Check if the something is already mounted at the mountpoint. */
|
|
|
|
|
|
|
|
ret = automount_findinode(lower->mountpoint);
|
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case OK_EXIST:
|
|
|
|
/* If we get here, then the volume must be mounted */
|
|
|
|
|
|
|
|
DEBUGASSERT(priv->mounted);
|
|
|
|
|
|
|
|
/* Un-mount the volume */
|
|
|
|
|
2015-03-15 06:48:45 +08:00
|
|
|
ret = umount2(lower->mountpoint, MNT_FORCE);
|
2014-07-30 02:50:08 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = get_errno();
|
|
|
|
DEBUGASSERT(errcode > 0);
|
|
|
|
|
|
|
|
/* We expect the error to be EBUSY meaning that the volume could
|
|
|
|
* not be unmounted because there are currently reference via open
|
|
|
|
* files or directories.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (errcode == EBUSY)
|
|
|
|
{
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("WARNING: Volume is busy, try again later\n");
|
2014-07-30 02:50:08 +08:00
|
|
|
|
2015-03-15 06:48:45 +08:00
|
|
|
/* Start a timer to retry the umount2 after a delay */
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
ret = wd_start(priv->wdog, lower->udelay, automount_timeout, 1,
|
|
|
|
(uint32_t)((uintptr_t)priv));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = get_errno();
|
|
|
|
DEBUGASSERT(errcode > 0);
|
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: wd_start failed: %d\n", errcode);
|
2014-07-30 02:50:08 +08:00
|
|
|
return -ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Other errors are fatal */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2016-06-21 01:59:15 +08:00
|
|
|
ferr("ERROR: umount2 failed: %d\n", errcode);
|
2014-07-30 02:50:08 +08:00
|
|
|
return -errcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 01:05:08 +08:00
|
|
|
/* Fall through */
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
case OK_NOENT:
|
2014-07-31 01:05:08 +08:00
|
|
|
/* The mountpoint is not present. This is normal behavior in the
|
|
|
|
* case where the user manually un-mounted the volume before removing
|
|
|
|
* media. Nice job, Mr. user.
|
|
|
|
*/
|
2014-07-30 02:50:08 +08:00
|
|
|
|
2014-07-31 01:05:08 +08:00
|
|
|
priv->mounted = false;
|
2014-07-30 02:50:08 +08:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
default:
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: automount_findinode failed: %d\n", ret);
|
2014-07-30 02:50:08 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_timeout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A previous unmount failed because the volume was busy... busy meaning
|
|
|
|
* the volume could not be unmounted because there are open references
|
|
|
|
* the files or directories in the volume. When this failure occurred,
|
|
|
|
* the unmount logic setup a delay and this function is called as a result
|
|
|
|
* of that delay timeout.
|
|
|
|
*
|
|
|
|
* This function will attempt the unmount again.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* Standard wdog timeout parameters
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void automount_timeout(int argc, uint32_t arg1, ...)
|
|
|
|
{
|
|
|
|
FAR struct automounter_state_s *priv =
|
|
|
|
(FAR struct automounter_state_s *)((uintptr_t)arg1);
|
|
|
|
int ret;
|
|
|
|
|
2016-06-21 01:59:15 +08:00
|
|
|
finfo("Timeout!\n");
|
2014-07-30 02:50:08 +08:00
|
|
|
DEBUGASSERT(argc == 1 && priv);
|
|
|
|
|
|
|
|
/* Check the state of things. This timeout at the interrupt level and
|
|
|
|
* will cancel the timeout if there is any change in the insertion
|
|
|
|
* state. So we should still have the saved state as NOT inserted and
|
|
|
|
* there should be no pending work.
|
|
|
|
*/
|
|
|
|
|
2016-06-21 01:59:15 +08:00
|
|
|
finfo("inserted=%d\n", priv->inserted);
|
2014-07-30 02:50:08 +08:00
|
|
|
DEBUGASSERT(!priv->inserted && work_available(&priv->work));
|
|
|
|
|
|
|
|
/* Queue work to occur immediately. */
|
|
|
|
|
2014-07-30 05:34:31 +08:00
|
|
|
ret = work_queue(LPWORK, &priv->work, automount_worker, priv, 0);
|
2014-07-30 02:50:08 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-07-31 00:19:09 +08:00
|
|
|
/* NOTE: Currently, work_queue only returns success */
|
2014-07-30 02:50:08 +08:00
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to schedule work: %d\n", ret);
|
2014-07-30 02:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_worker
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-30 06:36:15 +08:00
|
|
|
* Performs auto-mount actions on the worker thread.
|
2014-07-30 02:50:08 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* arg - Work argument set by work_queue()
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void automount_worker(FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct automounter_state_s *priv = (FAR struct automounter_state_s *)arg;
|
|
|
|
FAR const struct automount_lower_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->lower);
|
|
|
|
lower = priv->lower;
|
|
|
|
|
|
|
|
/* Disable interrupts. We are commit now and everything must remain
|
|
|
|
* stable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
AUTOMOUNT_DISABLE(lower);
|
|
|
|
|
|
|
|
/* Are we mounting or unmounting? */
|
|
|
|
|
|
|
|
if (priv->inserted)
|
|
|
|
{
|
|
|
|
/* We are mounting */
|
|
|
|
|
|
|
|
automount_mount(priv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are unmounting */
|
|
|
|
|
|
|
|
(void)automount_unmount(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-enable interrupts */
|
|
|
|
|
|
|
|
AUTOMOUNT_ENABLE(lower);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_interrupt
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called (probably from the interrupt level) when a media change event
|
|
|
|
* has been detected.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lower - Persistent board configuration data
|
2014-07-30 06:36:15 +08:00
|
|
|
* arg - Data associated with the auto-mounter
|
2014-07-30 02:50:08 +08:00
|
|
|
* inserted - True: Media has been inserted. False: media has been removed
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* OK is returned on success; a negated errno value is returned on failure.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Interrupts are disabled so that there is no race condition with the
|
|
|
|
* timer expiry.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int automount_interrupt(FAR const struct automount_lower_s *lower,
|
|
|
|
FAR void *arg, bool inserted)
|
|
|
|
{
|
|
|
|
FAR struct automounter_state_s *priv = (FAR struct automounter_state_s *)arg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(lower && priv && priv->lower == lower);
|
|
|
|
|
2016-06-21 01:59:15 +08:00
|
|
|
finfo("inserted=%d\n", inserted);
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
/* Cancel any pending work. We could get called multiple times if, for
|
|
|
|
* example there is bounce in the detection mechanism. Work is performed
|
|
|
|
* the low priority work queue if it is available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = work_cancel(LPWORK, &priv->work);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* NOTE: Currently, work_cancel only returns success */
|
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to cancel work: %d\n", ret);
|
2014-07-30 02:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the media insertion/removal state */
|
|
|
|
|
|
|
|
priv->inserted = inserted;
|
|
|
|
|
|
|
|
/* Queue work to occur after a delay. The delays performs debouncing:
|
|
|
|
* If the insertion/removal detection logic has "chatter", then we may
|
|
|
|
* receive this interrupt numerous times. Each time, the previous work
|
2016-12-10 08:39:40 +08:00
|
|
|
* will be canceled (above) and the new work will scheduled with the
|
2014-07-30 02:50:08 +08:00
|
|
|
* delay. So the final mount operation will not be performed until the
|
|
|
|
* insertion state is stable for that delay.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = work_queue(LPWORK, &priv->work, automount_worker, priv,
|
|
|
|
priv->lower->ddelay);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-07-31 00:19:09 +08:00
|
|
|
/* NOTE: Currently, work_queue only returns success */
|
2014-07-30 02:50:08 +08:00
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to schedule work: %d\n", ret);
|
2014-07-30 02:50:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Cancel any retry delays */
|
|
|
|
|
|
|
|
(void)wd_cancel(priv->wdog);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: automount_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2014-07-30 06:36:15 +08:00
|
|
|
* Configure the auto mounter.
|
2014-07-30 02:50:08 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* lower - Persistent board configuration data
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2014-07-30 06:36:15 +08:00
|
|
|
* A void* handle. The only use for this handle is with automount_uninitialize().
|
2014-07-30 02:50:08 +08:00
|
|
|
* NULL is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR void *automount_initialize(FAR const struct automount_lower_s *lower)
|
|
|
|
{
|
|
|
|
FAR struct automounter_state_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("lower=%p\n", lower);
|
2014-07-30 02:50:08 +08:00
|
|
|
DEBUGASSERT(lower);
|
|
|
|
|
2014-07-30 06:36:15 +08:00
|
|
|
/* Allocate an auto-mounter state structure */
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
priv = (FAR struct automounter_state_s *)
|
2014-09-01 07:34:44 +08:00
|
|
|
kmm_zalloc(sizeof(struct automounter_state_s));
|
2014-07-30 02:50:08 +08:00
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to allocate state structure\n");
|
2014-07-30 02:50:08 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the automounter state structure */
|
|
|
|
|
|
|
|
priv->lower = lower;
|
|
|
|
|
|
|
|
/* Get a timer to handle unmount retries */
|
|
|
|
|
|
|
|
priv->wdog = wd_create();
|
|
|
|
if (!priv->wdog)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to create a timer\n");
|
2014-07-30 06:36:15 +08:00
|
|
|
automount_uninitialize(priv);
|
2014-07-30 02:50:08 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-30 03:00:35 +08:00
|
|
|
/* Handle the initial state of the mount on the caller's thread */
|
|
|
|
|
|
|
|
priv->inserted = AUTOMOUNT_INSERTED(lower);
|
2014-07-30 05:34:31 +08:00
|
|
|
|
|
|
|
/* Set up the first action at a delay from the initialization time (to
|
|
|
|
* allow time for any extended block driver initialization to complete.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = work_queue(LPWORK, &priv->work, automount_worker, priv,
|
|
|
|
priv->lower->ddelay);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-07-31 00:19:09 +08:00
|
|
|
/* NOTE: Currently, work_queue only returns success */
|
2014-07-30 05:34:31 +08:00
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to schedule work: %d\n", ret);
|
2014-07-30 05:34:31 +08:00
|
|
|
}
|
2014-07-30 03:00:35 +08:00
|
|
|
|
2014-07-30 02:50:08 +08:00
|
|
|
/* Attach and enable automounter interrupts */
|
|
|
|
|
|
|
|
ret = AUTOMOUNT_ATTACH(lower, automount_interrupt, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to attach automount interrupt: %d\n", ret);
|
2014-07-30 06:36:15 +08:00
|
|
|
automount_uninitialize(priv);
|
2014-07-30 02:50:08 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUTOMOUNT_ENABLE(lower);
|
|
|
|
return priv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-07-30 06:36:15 +08:00
|
|
|
* Name: automount_uninitialize
|
2014-07-30 02:50:08 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-30 03:00:35 +08:00
|
|
|
* Stop the automounter and free resources that it used. NOTE that the
|
|
|
|
* mount is left in its last state mounted/unmounted state.
|
2014-07-30 02:50:08 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - The value previously returned by automount_initialize();
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-30 06:36:15 +08:00
|
|
|
void automount_uninitialize(FAR void *handle)
|
2014-07-30 02:50:08 +08:00
|
|
|
{
|
|
|
|
FAR struct automounter_state_s *priv = (FAR struct automounter_state_s *)handle;
|
|
|
|
FAR const struct automount_lower_s *lower;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv && priv->lower);
|
|
|
|
lower = priv->lower;
|
|
|
|
|
|
|
|
/* Disable and detach interrupts */
|
|
|
|
|
|
|
|
AUTOMOUNT_DISABLE(lower);
|
|
|
|
(void)AUTOMOUNT_DETACH(lower);
|
|
|
|
|
|
|
|
/* Release resources */
|
|
|
|
|
|
|
|
(void)wd_delete(priv->wdog);
|
|
|
|
|
|
|
|
/* And free the state structure */
|
|
|
|
|
2014-09-01 07:04:02 +08:00
|
|
|
kmm_free(priv);
|
2014-07-30 02:50:08 +08:00
|
|
|
}
|