2015-11-21 17:14:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/driver/fs_blockproxy.c
|
|
|
|
*
|
2020-03-30 17:00:51 +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
|
2015-11-21 17:14:01 +01:00
|
|
|
*
|
2020-03-30 17:00:51 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-21 17:14:01 +01:00
|
|
|
*
|
2020-03-30 17:00:51 +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.
|
2015-11-21 17:14:01 +01: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>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2016-07-20 21:15:37 +02:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2018-09-15 18:49:41 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2020-02-01 08:17:32 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-11-21 17:14:01 +01:00
|
|
|
|
2017-03-04 15:25:20 +01:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
|
|
|
|
!defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
|
2015-11-21 17:14:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint32_t g_devno;
|
|
|
|
static sem_t g_devno_sem = SEM_INITIALIZER(1);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: unique_chardev
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a unique temporary device name in the /dev/ directory of the
|
2018-09-05 19:56:14 +02:00
|
|
|
* pseudo-file system. We cannot use mktemp for this because it will
|
2015-11-21 17:14:01 +01:00
|
|
|
* attempt to open() the file.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-11-21 17:14:01 +01: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 (; ; )
|
|
|
|
{
|
|
|
|
/* Get the semaphore protecting the path number */
|
|
|
|
|
2020-03-30 17:00:51 +02:00
|
|
|
ret = nxsem_wait_uninterruptible(&g_devno_sem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: nxsem_wait_uninterruptible failed: %d\n", ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-21 17:14:01 +01:00
|
|
|
|
|
|
|
/* Get the next device number and release the semaphore */
|
|
|
|
|
|
|
|
devno = ++g_devno;
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&g_devno_sem);
|
2015-11-21 17:14:01 +01:00
|
|
|
|
|
|
|
/* Construct the full device number */
|
|
|
|
|
|
|
|
devno &= 0xffffff;
|
2018-11-08 16:59:18 +01:00
|
|
|
snprintf(devbuf, 16, "/dev/tmpc%06lx", (unsigned long)devno);
|
2015-11-21 17:14:01 +01:00
|
|
|
|
|
|
|
/* Make sure that file name is not in use */
|
|
|
|
|
|
|
|
ret = stat(devbuf, &statbuf);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(errno == ENOENT);
|
|
|
|
return strdup(devbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is in use, try again */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: block_proxy
|
|
|
|
*
|
|
|
|
* Description:
|
2015-11-21 18:24:55 +01:00
|
|
|
* Create a temporary char driver using drivers/bch to mediate character
|
2015-11-21 17:14:01 +01:00
|
|
|
* oriented accessed to the block driver.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-11-21 17:14:01 +01:00
|
|
|
* blkdev - The path to the block driver
|
2015-11-21 18:24:55 +01:00
|
|
|
* oflags - Character driver open flags
|
2015-11-21 17:14:01 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If positive, non-zero file descriptor is returned on success. This
|
|
|
|
* is the file descriptor of the nameless character driver that mediates
|
|
|
|
* accesses to the block driver.
|
|
|
|
*
|
|
|
|
* Errors that may be returned:
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* ENOMEM - Failed to create a temporary path name.
|
2015-11-21 17:14:01 +01:00
|
|
|
*
|
|
|
|
* Plus:
|
|
|
|
*
|
|
|
|
* - Errors reported from bchdev_register()
|
|
|
|
* - Errors reported from open() or unlink()
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-11-21 18:24:55 +01:00
|
|
|
int block_proxy(FAR const char *blkdev, int oflags)
|
2015-11-21 17:14:01 +01:00
|
|
|
{
|
|
|
|
FAR char *chardev;
|
|
|
|
bool readonly;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
DEBUGASSERT(blkdev);
|
|
|
|
|
|
|
|
/* Create a unique temporary file name for the character device */
|
|
|
|
|
|
|
|
chardev = unique_chardev();
|
|
|
|
if (chardev == NULL)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to create temporary device name\n");
|
2015-11-21 17:14:01 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Should this character driver be read-only? */
|
|
|
|
|
2015-11-21 18:24:55 +01:00
|
|
|
readonly = ((oflags & O_WROK) == 0);
|
2015-11-21 17:14:01 +01:00
|
|
|
|
|
|
|
/* Wrap the block driver with an instance of the BCH driver */
|
|
|
|
|
|
|
|
ret = bchdev_register(blkdev, chardev, readonly);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: bchdev_register(%s, %s) failed: %d\n",
|
2015-11-21 17:14:01 +01:00
|
|
|
blkdev, chardev, ret);
|
|
|
|
|
|
|
|
goto errout_with_chardev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the newly created character driver */
|
|
|
|
|
2015-11-21 18:24:55 +01:00
|
|
|
oflags &= ~(O_CREAT | O_EXCL | O_APPEND | O_TRUNC);
|
2018-09-15 18:49:41 +02:00
|
|
|
fd = nx_open(chardev, oflags);
|
2015-11-21 17:14:01 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = fd;
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to open %s: %d\n", chardev, ret);
|
2015-11-21 17:14:01 +01: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!)
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = unlink(chardev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to unlink %s: %d\n", chardev, ret);
|
2020-07-30 04:24:53 +02:00
|
|
|
goto errout_with_chardev;
|
2015-11-21 17:14:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the allocate character driver name and return the open file
|
|
|
|
* descriptor.
|
|
|
|
*/
|
|
|
|
|
|
|
|
kmm_free(chardev);
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
errout_with_bchdev:
|
2020-01-02 17:49:34 +01:00
|
|
|
unlink(chardev);
|
2015-11-21 17:14:01 +01:00
|
|
|
|
|
|
|
errout_with_chardev:
|
|
|
|
kmm_free(chardev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-04 15:25:20 +01:00
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && !CONFIG_DISABLE_PSEUDOFS_OPERATIONS */
|