2008-11-14 11:07:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/loop.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2008-11-15 08:20:34 +08:00
|
|
|
#include <sys/mount.h>
|
2008-11-14 11:07:54 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2008-11-15 08:20:34 +08:00
|
|
|
#include <semaphore.h>
|
2008-11-14 11:07:54 +08:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
#define loop_semgive(d) sem_post(&(d)->sem) /* To match loop_semtake */
|
|
|
|
#define MAX_OPENCNT (255) /* Limit of ubyte */
|
|
|
|
|
2008-11-14 11:07:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct loop_struct_s
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
sem_t sem; /* For safe read-modify-write operations */
|
2008-11-14 11:07:54 +08:00
|
|
|
uint32 nsectors; /* Number of sectors on device */
|
2008-11-15 08:20:34 +08:00
|
|
|
off_t offset; /* Offset (in bytes) to the first sector */
|
2008-11-14 11:07:54 +08:00
|
|
|
uint16 sectsize; /* The size of one sector */
|
2008-11-15 08:20:34 +08:00
|
|
|
ubyte opencnt; /* Count of open references to the loop device */
|
2008-11-14 11:07:54 +08:00
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
boolean writeenabled; /* TRUE: can write to device */
|
|
|
|
#endif
|
|
|
|
int fd; /* Descriptor of char device/file */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
static void loop_semtake(FAR struct loop_struct_s *dev);
|
2008-11-14 11:07:54 +08:00
|
|
|
static int loop_open(FAR struct inode *inode);
|
|
|
|
static int loop_close(FAR struct inode *inode);
|
|
|
|
static ssize_t loop_read(FAR struct inode *inode, unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors);
|
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
static ssize_t loop_write(FAR struct inode *inode, const unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors);
|
|
|
|
#endif
|
|
|
|
static int loop_geometry(FAR struct inode *inode, struct geometry *geometry);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct block_operations g_bops =
|
|
|
|
{
|
|
|
|
loop_open, /* open */
|
|
|
|
loop_close, /* close */
|
|
|
|
loop_read, /* read */
|
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
loop_write, /* write */
|
|
|
|
#else
|
|
|
|
NULL, /* write */
|
|
|
|
#endif
|
|
|
|
loop_geometry, /* geometry */
|
|
|
|
NULL /* ioctl */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_semtake
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void loop_semtake(FAR struct loop_struct_s *dev)
|
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
|
|
|
while (sem_wait(&dev->sem) != 0)
|
|
|
|
{
|
|
|
|
/* The only case that an error should occur here is if
|
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(errno == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-14 11:07:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_open
|
|
|
|
*
|
|
|
|
* Description: Open the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int loop_open(FAR struct inode *inode)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
FAR struct loop_struct_s *dev;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
dev = (FAR struct loop_struct_s *)inode->i_private;
|
|
|
|
|
2008-11-16 22:54:54 +08:00
|
|
|
/* Make sure we have exclusive access to the state structure */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
loop_semtake(dev);
|
|
|
|
if (dev->opencnt == MAX_OPENCNT)
|
|
|
|
{
|
|
|
|
return -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-16 22:54:54 +08:00
|
|
|
/* Increment the open count */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
dev->opencnt++;
|
|
|
|
}
|
|
|
|
loop_semgive(dev);
|
|
|
|
return ret;
|
2008-11-14 11:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-11-15 08:20:34 +08:00
|
|
|
* Name: loop_close
|
2008-11-14 11:07:54 +08:00
|
|
|
*
|
|
|
|
* Description: close the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int loop_close(FAR struct inode *inode)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
FAR struct loop_struct_s *dev;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
dev = (FAR struct loop_struct_s *)inode->i_private;
|
|
|
|
|
2008-11-16 22:54:54 +08:00
|
|
|
/* Make sure we have exclusive access to the state structure */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
loop_semtake(dev);
|
|
|
|
if (dev->opencnt == 0)
|
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-16 22:54:54 +08:00
|
|
|
/* Decrement the open count */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
dev->opencnt--;
|
|
|
|
}
|
|
|
|
loop_semgive(dev);
|
|
|
|
return ret;
|
2008-11-14 11:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_read
|
|
|
|
*
|
|
|
|
* Description: Read the specified numer of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t loop_read(FAR struct inode *inode, unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
FAR struct loop_struct_s *dev;
|
2008-11-14 11:07:54 +08:00
|
|
|
size_t nbytesread;
|
|
|
|
off_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2008-11-15 08:20:34 +08:00
|
|
|
dev = (FAR struct loop_struct_s *)inode->i_private;
|
|
|
|
|
|
|
|
if (start_sector + nsectors > dev->nsectors)
|
|
|
|
{
|
2008-11-16 03:18:32 +08:00
|
|
|
dbg("Read past end of file\n");
|
2008-11-15 08:20:34 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
2008-11-14 11:07:54 +08:00
|
|
|
|
|
|
|
/* Calculate the offset to read the sectors and seek to the position */
|
|
|
|
|
2008-11-16 03:18:32 +08:00
|
|
|
offset = start_sector * dev->sectsize + dev->offset;
|
2008-11-14 11:07:54 +08:00
|
|
|
ret = lseek(dev->fd, offset, SEEK_SET);
|
|
|
|
if (ret == (off_t)-1)
|
|
|
|
{
|
|
|
|
dbg("Seek failed for offset=%d: %d\n", (int)offset, errno);
|
2008-11-15 08:20:34 +08:00
|
|
|
return -EIO;
|
2008-11-14 11:07:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then read the requested number of sectors from that position */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nbytesread = read(dev->fd, buffer, nsectors * dev->sectsize);
|
|
|
|
if (nbytesread < 0 && errno != EINTR)
|
|
|
|
{
|
|
|
|
dbg("Read failed: %d\n", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread < 0);
|
|
|
|
|
|
|
|
/* Return the number of sectors read */
|
|
|
|
|
|
|
|
return nbytesread / dev->sectsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_write
|
|
|
|
*
|
|
|
|
* Description: Write the specified number of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
static ssize_t loop_write(FAR struct inode *inode, const unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
FAR struct loop_struct_s *dev;
|
2008-11-14 11:07:54 +08:00
|
|
|
size_t nbyteswritten;
|
|
|
|
off_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2008-11-15 08:20:34 +08:00
|
|
|
dev = (FAR struct loop_struct_s *)inode->i_private;
|
2008-11-14 11:07:54 +08:00
|
|
|
|
|
|
|
/* Calculate the offset to write the sectors and seek to the position */
|
|
|
|
|
2008-11-16 03:18:32 +08:00
|
|
|
offset = start_sector * dev->sectsize + dev->offset;
|
2008-11-14 11:07:54 +08:00
|
|
|
ret = lseek(dev->fd, offset, SEEK_SET);
|
|
|
|
if (ret == (off_t)-1)
|
|
|
|
{
|
|
|
|
dbg("Seek failed for offset=%d: %d\n", (int)offset, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then write the requested number of sectors to that position */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nbyteswritten = write(dev->fd, buffer, nsectors * dev->sectsize);
|
|
|
|
if (nbyteswritten < 0 && errno != EINTR)
|
|
|
|
{
|
|
|
|
dbg("Write failed: %d\n", errno);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbyteswritten < 0);
|
|
|
|
|
|
|
|
/* Return the number of sectors written */
|
|
|
|
|
|
|
|
return nbyteswritten / dev->sectsize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_geometry
|
|
|
|
*
|
|
|
|
* Description: Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int loop_geometry(FAR struct inode *inode, struct geometry *geometry)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
FAR struct loop_struct_s *dev;
|
2008-11-14 11:07:54 +08:00
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
if (geometry)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
dev = (FAR struct loop_struct_s *)inode->i_private;
|
2008-11-14 11:07:54 +08:00
|
|
|
geometry->geo_available = TRUE;
|
|
|
|
geometry->geo_mediachanged = FALSE;
|
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
geometry->geo_writeenabled = dev->writeenabled;
|
|
|
|
#else
|
|
|
|
geometry->geo_writeenabled = FALSE;
|
|
|
|
#endif
|
|
|
|
geometry->geo_nsectors = dev->nsectors;
|
|
|
|
geometry->geo_sectorsize = dev->sectsize;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: losetup
|
|
|
|
*
|
|
|
|
* Description:
|
2008-11-15 08:20:34 +08:00
|
|
|
* Setup the loop device so that it exports the file referenced by 'filename'
|
2008-11-14 11:07:54 +08:00
|
|
|
* as a block device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
int losetup(const char *devname, const char *filename, uint16 sectsize,
|
|
|
|
off_t offset, boolean readonly)
|
2008-11-14 11:07:54 +08:00
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
FAR struct loop_struct_s *dev;
|
2008-11-14 11:07:54 +08:00
|
|
|
struct stat sb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
2008-11-15 08:20:34 +08:00
|
|
|
if (!devname || !filename || !sectsize)
|
2008-11-14 11:07:54 +08:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the size of the file */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
ret = stat(filename, &sb);
|
2008-11-14 11:07:54 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
dbg("Failed to stat %s: %d\n", filename, errno);
|
2008-11-14 11:07:54 +08:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the file system is big enough for one block */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
if (sb.st_size - offset < sectsize)
|
2008-11-14 11:07:54 +08:00
|
|
|
{
|
|
|
|
dbg("File is too small for blocksize\n");
|
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a loop device structure */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
dev = (FAR struct loop_struct_s *)zalloc(sizeof(struct loop_struct_s));
|
2008-11-14 11:07:54 +08:00
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-11-16 03:18:32 +08:00
|
|
|
/* Initialize the loop device structure. */
|
2008-11-15 08:20:34 +08:00
|
|
|
|
2008-11-16 03:18:32 +08:00
|
|
|
sem_init(&dev->sem, 0, 1);
|
2008-11-15 08:20:34 +08:00
|
|
|
dev->nsectors = (sb.st_size - offset) / sectsize;
|
|
|
|
dev->sectsize = sectsize;
|
|
|
|
dev->offset = offset;
|
|
|
|
|
|
|
|
/* Open the file. */
|
2008-11-14 11:07:54 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_FS_WRITABLE
|
|
|
|
dev->writeenabled = FALSE; /* Assume failure */
|
2008-11-15 08:20:34 +08:00
|
|
|
dev->fd = -1;
|
|
|
|
|
|
|
|
/* First try to open the device R/W access (unless we are asked
|
|
|
|
* to open it readonly).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!readonly)
|
|
|
|
{
|
|
|
|
dev->fd = open(filename, O_RDWR);
|
|
|
|
}
|
2008-11-14 11:07:54 +08:00
|
|
|
|
|
|
|
if (dev->fd >= 0)
|
|
|
|
{
|
|
|
|
dev->writeenabled = TRUE; /* Success */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* If that fails, then try to open the device read-only */
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
dev->fd = open(filename, O_RDWR);
|
2008-11-14 11:07:54 +08:00
|
|
|
if (dev->fd < 0)
|
|
|
|
{
|
2008-11-15 08:20:34 +08:00
|
|
|
dbg("Failed to open %s: %d\n", filename, errno);
|
|
|
|
ret = -errno;
|
|
|
|
goto errout_with_dev;
|
2008-11-14 11:07:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
/* Inode private data will be reference to the loop device structure */
|
2008-11-14 11:07:54 +08:00
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
ret = register_blockdriver(devname, &g_bops, 0, dev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fdbg("register_blockdriver failed: %d\n", -ret);
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
2008-11-14 11:07:54 +08:00
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
return OK;
|
2008-11-14 11:07:54 +08:00
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
errout_with_fd:
|
|
|
|
close(dev->fd);
|
|
|
|
errout_with_dev:
|
|
|
|
free(dev);
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-14 11:07:54 +08:00
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: loteardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Undo the setup performed by losetup
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int loteardown(const char *devname)
|
|
|
|
{
|
|
|
|
FAR struct loop_struct_s *dev;
|
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2008-11-14 11:07:54 +08:00
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!devname)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Open the block driver associated with devname so that we can get the inode
|
|
|
|
* reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = open_blockdriver(devname, MS_RDONLY, &inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
dbg("Failed to open %s: %d\n", devname, -ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inode private data is a reference to the loop device stgructure */
|
|
|
|
|
|
|
|
dev = (FAR struct loop_struct_s *)inode->i_private;
|
|
|
|
close_blockdriver(inode);
|
|
|
|
|
|
|
|
DEBUGASSERT(dev);
|
|
|
|
|
|
|
|
/* Are there still open references to the device */
|
|
|
|
|
|
|
|
if (dev->opencnt > 0)
|
|
|
|
{
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, unregister the block device */
|
|
|
|
|
|
|
|
ret = unregister_blockdriver(devname);
|
|
|
|
|
|
|
|
/* Release the device structure */
|
|
|
|
|
|
|
|
if (dev->fd >= 0)
|
|
|
|
{
|
|
|
|
(void)close(dev->fd);
|
|
|
|
}
|
2008-11-14 11:07:54 +08:00
|
|
|
|
2008-11-15 08:20:34 +08:00
|
|
|
free(dev);
|
2008-11-14 11:07:54 +08:00
|
|
|
return ret;
|
|
|
|
}
|