2015-11-22 00:14:01 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/driver/fs_blockproxy.c
|
|
|
|
*
|
2020-03-30 23:00:51 +08: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
|
2015-11-22 00:14:01 +08:00
|
|
|
*
|
2020-03-30 23:00:51 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-22 00:14:01 +08:00
|
|
|
*
|
2020-03-30 23:00:51 +08: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.
|
2015-11-22 00:14:01 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2023-08-08 06:11:20 +08:00
|
|
|
#include <nuttx/lib/lib.h>
|
2016-07-21 03:15:37 +08:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2018-09-16 00:49:41 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2022-09-06 14:18:45 +08:00
|
|
|
#include <nuttx/mutex.h>
|
2015-11-22 00:14:01 +08:00
|
|
|
|
2023-05-30 12:51:09 +08:00
|
|
|
#include "driver.h"
|
|
|
|
|
2017-03-04 22:25:20 +08:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
|
|
|
|
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint32_t g_devno;
|
2022-09-06 14:18:45 +08:00
|
|
|
static mutex_t g_devno_lock = NXMUTEX_INITIALIZER;
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unique_chardev
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a unique temporary device name in the /dev/ directory of the
|
2018-09-06 01:56:14 +08:00
|
|
|
* pseudo-file system. We cannot use mktemp for this because it will
|
2015-11-22 00:14:01 +08:00
|
|
|
* attempt to open() the file.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Input Parameters:
|
2015-11-22 00:14:01 +08:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The allocated path to the device. This must be released by the caller
|
|
|
|
* to prevent memory links. NULL will be returned only the case where
|
|
|
|
* we fail to allocate memory.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR char *unique_chardev(void)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
char devbuf[16];
|
|
|
|
uint32_t devno;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Loop until we get a unique device name */
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
2022-09-06 14:18:45 +08:00
|
|
|
/* Get the mutex protecting the path number */
|
2015-11-22 00:14:01 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&g_devno_lock);
|
2020-03-30 23:00:51 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 14:18:45 +08:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2020-03-30 23:00:51 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
/* Get the next device number and release the semaphore */
|
|
|
|
|
|
|
|
devno = ++g_devno;
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&g_devno_lock);
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
/* Construct the full device number */
|
|
|
|
|
|
|
|
devno &= 0xffffff;
|
2024-08-25 00:54:12 +08:00
|
|
|
snprintf(devbuf, sizeof(devbuf), "/dev/tmpc%06lx",
|
|
|
|
(unsigned long)devno);
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
/* Make sure that file name is not in use */
|
|
|
|
|
2020-10-01 19:21:36 +08:00
|
|
|
ret = nx_stat(devbuf, &statbuf, 1);
|
2015-11-22 00:14:01 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-10-01 19:21:36 +08:00
|
|
|
DEBUGASSERT(ret == -ENOENT);
|
2015-11-22 00:14:01 +08:00
|
|
|
return strdup(devbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is in use, try again */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: block_proxy
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-22 01:24:55 +08:00
|
|
|
* Create a temporary char driver using drivers/bch to mediate character
|
2015-11-22 00:14:01 +08:00
|
|
|
* oriented accessed to the block driver.
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Input Parameters:
|
2021-01-22 21:25:03 +08:00
|
|
|
* filep - The caller provided location in which to return the 'struct
|
|
|
|
* file' instance.
|
2015-11-22 00:14:01 +08:00
|
|
|
* blkdev - The path to the block driver
|
2015-11-22 01:24:55 +08:00
|
|
|
* oflags - Character driver open flags
|
2015-11-22 00:14:01 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2021-01-22 21:25:03 +08:00
|
|
|
* Zero (OK) is returned on success. On failure, a negated errno value is
|
|
|
|
* returned.
|
2015-11-22 00:14:01 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-01 19:21:36 +08:00
|
|
|
int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
|
2015-11-22 00:14:01 +08:00
|
|
|
{
|
|
|
|
FAR char *chardev;
|
|
|
|
bool readonly;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(blkdev);
|
|
|
|
|
|
|
|
/* Create a unique temporary file name for the character device */
|
|
|
|
|
|
|
|
chardev = unique_chardev();
|
|
|
|
if (chardev == NULL)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to create temporary device name\n");
|
2015-11-22 00:14:01 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should this character driver be read-only? */
|
|
|
|
|
2015-11-22 01:24:55 +08:00
|
|
|
readonly = ((oflags & O_WROK) == 0);
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
/* Wrap the block driver with an instance of the BCH driver */
|
|
|
|
|
|
|
|
ret = bchdev_register(blkdev, chardev, readonly);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: bchdev_register(%s, %s) failed: %d\n",
|
2015-11-22 00:14:01 +08:00
|
|
|
blkdev, chardev, ret);
|
|
|
|
|
|
|
|
goto errout_with_chardev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the newly created character driver */
|
|
|
|
|
2015-11-22 01:24:55 +08:00
|
|
|
oflags &= ~(O_CREAT | O_EXCL | O_APPEND | O_TRUNC);
|
2020-10-01 19:21:36 +08:00
|
|
|
ret = file_open(filep, chardev, oflags);
|
|
|
|
if (ret < 0)
|
2015-11-22 00:14:01 +08:00
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to open %s: %d\n", chardev, ret);
|
2015-11-22 00:14:01 +08:00
|
|
|
goto errout_with_bchdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlink the character device name. The driver instance will persist,
|
|
|
|
* provided that CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y (otherwise, we have
|
|
|
|
* a problem here!)
|
|
|
|
*/
|
|
|
|
|
2021-01-22 21:36:58 +08:00
|
|
|
ret = nx_unlink(chardev);
|
2015-11-22 00:14:01 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Failed to unlink %s: %d\n", chardev, ret);
|
2020-07-30 10:24:53 +08:00
|
|
|
goto errout_with_chardev;
|
2015-11-22 00:14:01 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 21:25:03 +08:00
|
|
|
/* Free the allocated character driver name. */
|
2015-11-22 00:14:01 +08:00
|
|
|
|
2023-08-08 06:11:20 +08:00
|
|
|
lib_free(chardev);
|
2020-10-01 19:21:36 +08:00
|
|
|
return OK;
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
errout_with_bchdev:
|
2021-01-22 21:36:58 +08:00
|
|
|
nx_unlink(chardev);
|
2015-11-22 00:14:01 +08:00
|
|
|
|
|
|
|
errout_with_chardev:
|
2023-08-08 06:11:20 +08:00
|
|
|
lib_free(chardev);
|
2015-11-22 00:14:01 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-04 22:25:20 +08:00
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && !CONFIG_DISABLE_PSEUDOFS_OPERATIONS */
|