2008-11-15 01:14:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/bch/bchlib_write.c
|
|
|
|
*
|
2021-03-04 07:10:42 +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-15 01:14:05 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-15 01:14:05 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +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-15 01:14:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2008-11-15 01:14:05 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2023-05-30 06:51:09 +02:00
|
|
|
#include <nuttx/drivers/drivers.h>
|
2008-11-15 01:14:05 +01:00
|
|
|
|
2015-11-21 16:15:12 +01:00
|
|
|
#include "bch.h"
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: bchlib_write
|
|
|
|
*
|
|
|
|
* Description:
|
2020-03-11 06:44:32 +01:00
|
|
|
* Write to the block device set-up by bchlib_setup as if it were a
|
|
|
|
* character device.
|
2008-11-15 01:14:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-20 21:15:37 +02:00
|
|
|
ssize_t bchlib_write(FAR void *handle, FAR const char *buffer, size_t offset,
|
|
|
|
size_t len)
|
2008-11-15 01:14:05 +01:00
|
|
|
{
|
2008-11-15 17:36:32 +01:00
|
|
|
FAR struct bchlib_s *bch = (FAR struct bchlib_s *)handle;
|
2009-12-15 15:25:14 +01:00
|
|
|
size_t nsectors;
|
|
|
|
size_t sector;
|
|
|
|
uint16_t sectoffset;
|
|
|
|
size_t nbytes;
|
|
|
|
size_t byteswritten;
|
|
|
|
int ret;
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/* Get rid of this special case right away */
|
|
|
|
|
|
|
|
if (len < 1)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
/* Convert the file position into a sector number and offset. */
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
sector = offset / bch->sectsize;
|
|
|
|
sectoffset = offset - sector * bch->sectsize;
|
|
|
|
|
|
|
|
if (sector >= bch->nsectors)
|
|
|
|
{
|
|
|
|
return -EFBIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the initial partial sector */
|
|
|
|
|
|
|
|
byteswritten = 0;
|
|
|
|
if (sectoffset > 0)
|
|
|
|
{
|
|
|
|
/* Read the full sector into the sector buffer */
|
|
|
|
|
2021-08-04 07:04:10 +02:00
|
|
|
ret = bchlib_readsector(bch, sector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/* Copy the tail end of the sector from the user buffer */
|
|
|
|
|
|
|
|
if (sectoffset + len > bch->sectsize)
|
|
|
|
{
|
|
|
|
nbytes = bch->sectsize - sectoffset;
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
else
|
2008-11-15 01:14:05 +01:00
|
|
|
{
|
|
|
|
nbytes = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&bch->buffer[sectoffset], buffer, nbytes);
|
2009-12-15 15:25:14 +01:00
|
|
|
bch->dirty = true;
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/* Adjust pointers and counts */
|
|
|
|
|
|
|
|
sector++;
|
|
|
|
|
|
|
|
if (sector >= bch->nsectors)
|
|
|
|
{
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
byteswritten = nbytes;
|
|
|
|
buffer += nbytes;
|
|
|
|
len -= nbytes;
|
|
|
|
}
|
|
|
|
|
2011-08-29 18:07:29 +02:00
|
|
|
/* Then write all of the full sectors following the partial sector
|
|
|
|
* directly from the user buffer.
|
|
|
|
*/
|
2008-11-15 01:14:05 +01:00
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
if (len >= bch->sectsize)
|
2008-11-15 01:14:05 +01:00
|
|
|
{
|
|
|
|
nsectors = len / bch->sectsize;
|
|
|
|
if (sector + nsectors > bch->nsectors)
|
|
|
|
{
|
|
|
|
nsectors = bch->nsectors - sector;
|
|
|
|
}
|
|
|
|
|
2020-07-13 13:40:07 +02:00
|
|
|
/* Flush the dirty sector to keep the sector sequence */
|
|
|
|
|
2023-07-29 04:37:17 +02:00
|
|
|
ret = bchlib_flushsector(bch, sector <= bch->sector &&
|
|
|
|
bch->sector < (sector + nsectors));
|
2020-07-13 13:40:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Flush failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-11-15 01:14:05 +01:00
|
|
|
/* Write the contiguous sectors */
|
|
|
|
|
2011-08-29 18:07:29 +02:00
|
|
|
ret = bch->inode->u.i_bops->write(bch->inode, (FAR uint8_t *)buffer,
|
|
|
|
sector, nsectors);
|
2008-11-15 01:14:05 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
ferr("ERROR: Write failed: %d\n", ret);
|
2008-11-15 01:14:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adjust pointers and counts */
|
|
|
|
|
|
|
|
sector += nsectors;
|
|
|
|
nbytes = nsectors * bch->sectsize;
|
|
|
|
byteswritten += nbytes;
|
|
|
|
|
|
|
|
if (sector >= bch->nsectors)
|
|
|
|
{
|
|
|
|
return byteswritten;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer += nbytes;
|
|
|
|
len -= nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then write any partial final sector */
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
/* Read the sector into the sector buffer */
|
|
|
|
|
2021-08-04 07:04:10 +02:00
|
|
|
ret = bchlib_readsector(bch, sector);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/* Copy the head end of the sector from the user buffer */
|
|
|
|
|
|
|
|
memcpy(bch->buffer, buffer, len);
|
2009-12-15 15:25:14 +01:00
|
|
|
bch->dirty = true;
|
2008-11-15 01:14:05 +01:00
|
|
|
|
|
|
|
/* Adjust counts */
|
|
|
|
|
|
|
|
byteswritten += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byteswritten;
|
|
|
|
}
|