forked from nuttx/nuttx-update
drivers/bch: Add support for delayed unlinking of BCH driver
This commit is contained in:
parent
8914400750
commit
f03e435e43
10 changed files with 109 additions and 24 deletions
|
@ -1,7 +1,7 @@
|
|||
/****************************************************************************
|
||||
* drivers/bch/bch_internal.h
|
||||
* drivers/bch/bch.h
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2014 Gregory Nutt. All rights reserved.
|
||||
* Copyright (C) 2008-2009, 2014-2015 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -33,8 +33,8 @@
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __FS_BCH_INTERNAL_H
|
||||
#define __FS_BCH_INTERNAL_H
|
||||
#ifndef __DRIVERS_BCH_BCH_H
|
||||
#define __DRIVERS_BCH_BCH_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
|
@ -67,8 +67,9 @@ struct bchlib_s
|
|||
size_t sector; /* The current sector in the buffer */
|
||||
uint16_t sectsize; /* The size of one sector on the device */
|
||||
uint8_t refs; /* Number of references */
|
||||
bool dirty; /* Data has been written to the buffer */
|
||||
bool readonly; /* true: Only read operations are supported */
|
||||
bool dirty; /* true: Data has been written to the buffer */
|
||||
bool readonly; /* true: Only read operations are supported */
|
||||
bool unlinked; /* true: The driver has been unlinked */
|
||||
FAR uint8_t *buffer; /* One sector buffer */
|
||||
|
||||
#if defined(CONFIG_BCH_ENCRYPTION)
|
||||
|
@ -103,4 +104,4 @@ EXTERN int bchlib_readsector(FAR struct bchlib_s *bch, size_t sector);
|
|||
}
|
||||
#endif
|
||||
|
||||
#endif /* __FS_BCH_INTERNAL_H */
|
||||
#endif /* __DRIVERS_BCH_BCH_H */
|
|
@ -58,7 +58,7 @@
|
|||
#include <nuttx/fs/fs.h>
|
||||
#include <nuttx/fs/ioctl.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
|
@ -77,6 +77,9 @@ static ssize_t bch_write(FAR struct file *filep, FAR const char *buffer,
|
|||
size_t buflen);
|
||||
static int bch_ioctl(FAR struct file *filep, int cmd,
|
||||
unsigned long arg);
|
||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||
static int bch_unlink(FAR struct inode *inode);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
|
@ -84,14 +87,17 @@ static int bch_ioctl(FAR struct file *filep, int cmd,
|
|||
|
||||
const struct file_operations bch_fops =
|
||||
{
|
||||
bch_open, /* open */
|
||||
bch_close, /* close */
|
||||
bch_read, /* read */
|
||||
bch_write, /* write */
|
||||
bch_seek, /* seek */
|
||||
bch_ioctl /* ioctl */
|
||||
bch_open, /* open */
|
||||
bch_close, /* close */
|
||||
bch_read, /* read */
|
||||
bch_write, /* write */
|
||||
bch_seek, /* seek */
|
||||
bch_ioctl /* ioctl */
|
||||
#ifndef CONFIG_DISABLE_POLL
|
||||
, 0 /* poll */
|
||||
, 0 /* poll */
|
||||
#endif
|
||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||
, bch_unlink /* unlink */
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -152,7 +158,8 @@ static int bch_close(FAR struct file *filep)
|
|||
(void)bchlib_flushsector(bch);
|
||||
|
||||
/* Decrement the reference count (I don't use bchlib_decref() because I
|
||||
* want the entire close operation to be atomic wrt other driver operations.
|
||||
* want the entire close operation to be atomic wrt other driver
|
||||
* operations.
|
||||
*/
|
||||
|
||||
if (bch->refs == 0)
|
||||
|
@ -162,6 +169,30 @@ static int bch_close(FAR struct file *filep)
|
|||
else
|
||||
{
|
||||
bch->refs--;
|
||||
|
||||
/* If the reference count decremented to zero AND if the character
|
||||
* driver has been unlinked, then teardown the BCH device now.
|
||||
*/
|
||||
|
||||
if (bch->refs == 0 && bch->unlinked)
|
||||
{
|
||||
/* Tear the driver down now. */
|
||||
|
||||
ret = bchlib_teardown((FAR void *)bch);
|
||||
|
||||
/* bchlib_teardown() would only fail if there are outstanding
|
||||
* references on the device. Since we know that is not true, it
|
||||
* should not fail at all.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(ret >= 0);
|
||||
if (ret >= 0)
|
||||
{
|
||||
/* Return without releasing the stale semaphore */
|
||||
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bchlib_semgive(bch);
|
||||
|
@ -349,6 +380,59 @@ static int bch_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: bch_unlink
|
||||
*
|
||||
* Handle unlinking of the BCH device
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
||||
static int bch_unlink(FAR struct inode *inode)
|
||||
{
|
||||
FAR struct bchlib_s *bch;
|
||||
int ret = OK;
|
||||
|
||||
DEBUGASSERT(inode && inode->i_private);
|
||||
bch = (FAR struct bchlib_s *)inode->i_private;
|
||||
|
||||
/* Get exclusive access to the BCH device */
|
||||
|
||||
bchlib_semtake(bch);
|
||||
|
||||
/* Indicate that the driver has been unlinked */
|
||||
|
||||
bch->unlinked = true;
|
||||
|
||||
/* If there are no open references to the drvier then teardown the BCH
|
||||
* device now.
|
||||
*/
|
||||
|
||||
if (bch->refs == 0)
|
||||
{
|
||||
/* Tear the driver down now. */
|
||||
|
||||
ret = bchlib_teardown((FAR void *)bch);
|
||||
|
||||
/* bchlib_teardown() would only fail if there are outstanding
|
||||
* references on the device. Since we know that is not true, it
|
||||
* should not fail at all.
|
||||
*/
|
||||
|
||||
DEBUGASSERT(ret >= 0);
|
||||
if (ret >= 0)
|
||||
{
|
||||
/* Return without releasing the stale semaphore */
|
||||
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
bchlib_semgive(bch);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
|
|
@ -44,7 +44,7 @@
|
|||
#include <assert.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
#include <nuttx/fs/fs.h>
|
||||
#include <nuttx/fs/ioctl.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
#if defined(CONFIG_BCH_ENCRYPTION)
|
||||
# include <crypto/crypto.h>
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
#include <assert.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
|
||||
#include <nuttx/fs/fs.h>
|
||||
|
||||
#include "bch_internal.h"
|
||||
#include "bch.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
|
|
Loading…
Reference in a new issue