2008-11-14 04:07:54 +01:00
|
|
|
/****************************************************************************
|
2015-11-26 00:13:56 +01:00
|
|
|
* drivers/loop/losetup.c
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
2020-03-21 17:36:26 +01: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
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
2020-03-21 17:36:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
2020-03-21 17:36:26 +01: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.
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 01:20:34 +01:00
|
|
|
#include <sys/mount.h>
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-14 04:07:54 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2008-11-14 04:07:54 +01:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-11-26 00:13:56 +01:00
|
|
|
#include <nuttx/fs/loop.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2008-11-14 04:07:54 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2009-12-15 15:25:14 +01:00
|
|
|
* Pre-processor Definitions
|
2008-11-14 04:07:54 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define MAX_OPENCNT (255) /* Limit of uint8_t */
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2008-11-14 04:07:54 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct loop_struct_s
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* For safe read-modify-write operations */
|
2009-12-15 15:25:14 +01:00
|
|
|
uint32_t nsectors; /* Number of sectors on device */
|
2008-11-15 01:20:34 +01:00
|
|
|
off_t offset; /* Offset (in bytes) to the first sector */
|
2009-12-15 15:25:14 +01:00
|
|
|
uint16_t sectsize; /* The size of one sector */
|
|
|
|
uint8_t opencnt; /* Count of open references to the loop device */
|
|
|
|
bool writeenabled; /* true: can write to device */
|
2017-11-02 23:31:56 +01:00
|
|
|
struct file devfile; /* File struct of char device/file */
|
2008-11-14 04:07:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int loop_open(FAR struct inode *inode);
|
|
|
|
static int loop_close(FAR struct inode *inode);
|
2014-11-27 16:12:15 +01:00
|
|
|
static ssize_t loop_read(FAR struct inode *inode, FAR unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t start_sector, unsigned int nsectors);
|
2014-11-27 16:12:15 +01:00
|
|
|
static ssize_t loop_write(FAR struct inode *inode,
|
|
|
|
FAR const unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t start_sector, unsigned int nsectors);
|
2014-11-27 16:12:15 +01:00
|
|
|
static int loop_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry);
|
2008-11-14 04:07:54 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct block_operations g_bops =
|
|
|
|
{
|
2016-05-26 18:01:15 +02:00
|
|
|
loop_open, /* open */
|
|
|
|
loop_close, /* close */
|
|
|
|
loop_read, /* read */
|
|
|
|
loop_write, /* write */
|
2008-11-14 04:07:54 +01:00
|
|
|
loop_geometry, /* geometry */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_open
|
|
|
|
*
|
|
|
|
* Description: Open the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int loop_open(FAR struct inode *inode)
|
|
|
|
{
|
2008-11-15 01:20:34 +01:00
|
|
|
FAR struct loop_struct_s *dev;
|
2014-11-27 16:12:15 +01:00
|
|
|
int ret;
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2008-11-16 15:54:54 +01:00
|
|
|
/* Make sure we have exclusive access to the state structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->lock);
|
2014-11-27 16:12:15 +01:00
|
|
|
if (ret == OK)
|
2008-11-15 01:20:34 +01:00
|
|
|
{
|
2014-11-27 16:12:15 +01:00
|
|
|
if (dev->opencnt == MAX_OPENCNT)
|
|
|
|
{
|
|
|
|
ret = -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Increment the open count */
|
2008-11-16 15:54:54 +01:00
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
dev->opencnt++;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2008-11-15 01:20:34 +01:00
|
|
|
}
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
return ret;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2008-11-15 01:20:34 +01:00
|
|
|
* Name: loop_close
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
|
|
|
* Description: close the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int loop_close(FAR struct inode *inode)
|
|
|
|
{
|
2008-11-15 01:20:34 +01:00
|
|
|
FAR struct loop_struct_s *dev;
|
2014-11-27 16:12:15 +01:00
|
|
|
int ret;
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2008-11-16 15:54:54 +01:00
|
|
|
/* Make sure we have exclusive access to the state structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->lock);
|
2014-11-27 16:12:15 +01:00
|
|
|
if (ret == OK)
|
2008-11-15 01:20:34 +01:00
|
|
|
{
|
2014-11-27 16:12:15 +01:00
|
|
|
if (dev->opencnt == 0)
|
|
|
|
{
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Decrement the open count */
|
|
|
|
|
|
|
|
dev->opencnt--;
|
|
|
|
}
|
2008-11-16 15:54:54 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2008-11-15 01:20:34 +01:00
|
|
|
}
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
return ret;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_read
|
|
|
|
*
|
2014-11-27 16:12:15 +01:00
|
|
|
* Description: Read the specified number of sectors
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
static ssize_t loop_read(FAR struct inode *inode, FAR unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t start_sector, unsigned int nsectors)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2008-11-15 01:20:34 +01:00
|
|
|
FAR struct loop_struct_s *dev;
|
2012-12-09 18:34:53 +01:00
|
|
|
ssize_t nbytesread;
|
2008-11-14 04:07:54 +01:00
|
|
|
off_t offset;
|
2018-01-31 00:23:10 +01:00
|
|
|
off_t ret;
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2008-11-15 01:20:34 +01:00
|
|
|
|
|
|
|
if (start_sector + nsectors > dev->nsectors)
|
|
|
|
{
|
2017-11-02 23:31:56 +01:00
|
|
|
ferr("ERROR: Read past end of file\n");
|
2008-11-15 01:20:34 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
2008-11-14 04:07:54 +01:00
|
|
|
|
|
|
|
/* Calculate the offset to read the sectors and seek to the position */
|
|
|
|
|
2008-11-15 20:18:32 +01:00
|
|
|
offset = start_sector * dev->sectsize + dev->offset;
|
2017-11-02 23:31:56 +01:00
|
|
|
ret = file_seek(&dev->devfile, offset, SEEK_SET);
|
2018-01-31 00:23:10 +01:00
|
|
|
if (ret < 0)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2018-01-31 00:23:10 +01:00
|
|
|
ferr("ERROR: Seek failed for offset=%d: %d\n", (int)offset, (int)ret);
|
2008-11-15 01:20:34 +01:00
|
|
|
return -EIO;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then read the requested number of sectors from that position */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-11-02 23:31:56 +01:00
|
|
|
nbytesread = file_read(&dev->devfile, buffer,
|
|
|
|
nsectors * dev->sectsize);
|
2017-10-11 20:13:41 +02:00
|
|
|
if (nbytesread < 0 && nbytesread != -EINTR)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2020-11-20 03:48:17 +01:00
|
|
|
ferr("ERROR: Read failed: %zd\n", nbytesread);
|
2017-10-11 20:13:41 +02:00
|
|
|
return (int)nbytesread;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbytesread < 0);
|
|
|
|
|
|
|
|
/* Return the number of sectors read */
|
|
|
|
|
|
|
|
return nbytesread / dev->sectsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_write
|
|
|
|
*
|
|
|
|
* Description: Write the specified number of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
static ssize_t loop_write(FAR struct inode *inode,
|
|
|
|
FAR const unsigned char *buffer,
|
2021-02-17 08:58:11 +01:00
|
|
|
blkcnt_t start_sector, unsigned int nsectors)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2008-11-15 01:20:34 +01:00
|
|
|
FAR struct loop_struct_s *dev;
|
2013-01-24 17:28:15 +01:00
|
|
|
ssize_t nbyteswritten;
|
2008-11-14 04:07:54 +01:00
|
|
|
off_t offset;
|
2018-01-31 00:23:10 +01:00
|
|
|
off_t ret;
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2008-11-14 04:07:54 +01:00
|
|
|
|
|
|
|
/* Calculate the offset to write the sectors and seek to the position */
|
|
|
|
|
2008-11-15 20:18:32 +01:00
|
|
|
offset = start_sector * dev->sectsize + dev->offset;
|
2017-11-02 23:31:56 +01:00
|
|
|
ret = file_seek(&dev->devfile, offset, SEEK_SET);
|
2018-01-31 00:23:10 +01:00
|
|
|
if (ret < 0)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2018-01-31 00:23:10 +01:00
|
|
|
ferr("ERROR: Seek failed for offset=%d: %d\n", (int)offset, (int)ret);
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then write the requested number of sectors to that position */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-11-02 23:31:56 +01:00
|
|
|
nbyteswritten = file_write(&dev->devfile, buffer,
|
|
|
|
nsectors * dev->sectsize);
|
2017-10-11 18:18:30 +02:00
|
|
|
if (nbyteswritten < 0 && nbyteswritten != -EINTR)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
ferr("ERROR: file_write failed: %zd\n", nbyteswritten);
|
2017-10-11 18:18:30 +02:00
|
|
|
return nbyteswritten;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (nbyteswritten < 0);
|
|
|
|
|
|
|
|
/* Return the number of sectors written */
|
|
|
|
|
|
|
|
return nbyteswritten / dev->sectsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: loop_geometry
|
|
|
|
*
|
|
|
|
* Description: Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
static int loop_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2008-11-15 01:20:34 +01:00
|
|
|
FAR struct loop_struct_s *dev;
|
2008-11-14 04:07:54 +01:00
|
|
|
|
|
|
|
if (geometry)
|
|
|
|
{
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2021-08-11 06:21:39 +02:00
|
|
|
|
|
|
|
memset(geometry, 0, sizeof(*geometry));
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
geometry->geo_available = true;
|
|
|
|
geometry->geo_mediachanged = false;
|
2008-11-14 04:07:54 +01:00
|
|
|
geometry->geo_writeenabled = dev->writeenabled;
|
|
|
|
geometry->geo_nsectors = dev->nsectors;
|
|
|
|
geometry->geo_sectorsize = dev->sectsize;
|
|
|
|
return OK;
|
|
|
|
}
|
2012-07-15 16:56:25 +02:00
|
|
|
|
2008-11-14 04:07:54 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: losetup
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-11 06:47:14 +01:00
|
|
|
* Setup the loop device so that it exports the file referenced by
|
|
|
|
* 'filename' as a block device.
|
2008-11-14 04:07:54 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
int losetup(FAR const char *devname, FAR const char *filename,
|
|
|
|
uint16_t sectsize, off_t offset, bool readonly)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2008-11-15 01:20:34 +01:00
|
|
|
FAR struct loop_struct_s *dev;
|
2008-11-14 04:07:54 +01:00
|
|
|
struct stat sb;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2017-11-02 23:31:56 +01:00
|
|
|
if (devname == NULL || filename == NULL || sectsize == 0)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Get the size of the file */
|
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
ret = nx_stat(filename, &sb, 1);
|
2008-11-14 04:07:54 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-05-02 16:30:58 +02:00
|
|
|
ferr("ERROR: Failed to stat %s: %d\n", filename, ret);
|
|
|
|
return ret;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the file system is big enough for one block */
|
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
if (sb.st_size - offset < sectsize)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2017-11-02 23:31:56 +01:00
|
|
|
ferr("ERROR: File is too small for blocksize\n");
|
2008-11-14 04:07:54 +01:00
|
|
|
return -ERANGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a loop device structure */
|
|
|
|
|
2017-11-02 23:31:56 +01:00
|
|
|
dev = (FAR struct loop_struct_s *)
|
|
|
|
kmm_zalloc(sizeof(struct loop_struct_s));
|
|
|
|
if (dev == NULL)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2008-11-15 20:18:32 +01:00
|
|
|
/* Initialize the loop device structure. */
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->lock);
|
2008-11-15 01:20:34 +01:00
|
|
|
dev->nsectors = (sb.st_size - offset) / sectsize;
|
|
|
|
dev->sectsize = sectsize;
|
|
|
|
dev->offset = offset;
|
|
|
|
|
|
|
|
/* Open the file. */
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
/* First try to open the device R/W access (unless we are asked
|
|
|
|
* to open it readonly).
|
|
|
|
*/
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = -ENOSYS;
|
2008-11-15 01:20:34 +01:00
|
|
|
if (!readonly)
|
|
|
|
{
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = file_open(&dev->devfile, filename, O_RDWR);
|
2008-11-15 01:20:34 +01:00
|
|
|
}
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
if (ret >= 0)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2009-12-15 15:25:14 +01:00
|
|
|
dev->writeenabled = true; /* Success */
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If that fails, then try to open the device read-only */
|
|
|
|
|
2018-09-15 18:49:41 +02:00
|
|
|
ret = file_open(&dev->devfile, filename, O_RDONLY);
|
|
|
|
if (ret < 0)
|
2008-11-14 04:07:54 +01:00
|
|
|
{
|
2017-11-02 23:31:56 +01:00
|
|
|
ferr("ERROR: Failed to open %s: %d\n", filename, ret);
|
2008-11-15 01:20:34 +01:00
|
|
|
goto errout_with_dev;
|
2008-11-14 04:07:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
/* Inode private data will be reference to the loop device structure */
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
ret = register_blockdriver(devname, &g_bops, 0, dev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: register_blockdriver failed: %d\n", -ret);
|
2017-11-02 23:31:56 +01:00
|
|
|
goto errout_with_file;
|
2008-11-15 01:20:34 +01:00
|
|
|
}
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
return OK;
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2017-11-02 23:31:56 +01:00
|
|
|
errout_with_file:
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&dev->devfile);
|
2017-11-02 23:31:56 +01:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
errout_with_dev:
|
2022-11-20 14:45:32 +01:00
|
|
|
nxmutex_destroy(&dev->lock);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2008-11-15 01:20:34 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2008-11-15 01:20:34 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: loteardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Undo the setup performed by losetup
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
int loteardown(FAR const char *devname)
|
2008-11-15 01:20:34 +01:00
|
|
|
{
|
|
|
|
FAR struct loop_struct_s *dev;
|
|
|
|
FAR struct inode *inode;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity check */
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2017-11-02 23:31:56 +01:00
|
|
|
if (devname == NULL)
|
2008-11-15 01:20:34 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-11 06:47:14 +01:00
|
|
|
/* Open the block driver associated with devname so that we can get the
|
|
|
|
* inode reference.
|
2008-11-15 01:20:34 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ret = open_blockdriver(devname, MS_RDONLY, &inode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-11-02 23:31:56 +01:00
|
|
|
ferr("ERROR: Failed to open %s: %d\n", devname, -ret);
|
2008-11-15 01:20:34 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-27 16:12:15 +01:00
|
|
|
/* Inode private data is a reference to the loop device structure */
|
2008-11-15 01:20:34 +01:00
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
dev = inode->i_private;
|
2008-11-15 01:20:34 +01:00
|
|
|
close_blockdriver(inode);
|
|
|
|
|
2017-11-02 23:31:56 +01:00
|
|
|
DEBUGASSERT(dev != NULL);
|
2008-11-15 01:20:34 +01:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2017-11-02 23:31:56 +01:00
|
|
|
if (dev->devfile.f_inode != NULL)
|
2008-11-15 01:20:34 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
file_close(&dev->devfile);
|
2008-11-15 01:20:34 +01:00
|
|
|
}
|
2008-11-14 04:07:54 +01:00
|
|
|
|
2022-11-20 14:45:32 +01:00
|
|
|
nxmutex_destroy(&dev->lock);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2008-11-14 04:07:54 +01:00
|
|
|
return ret;
|
|
|
|
}
|