2013-12-06 12:04:52 +01:00
|
|
|
/* $NetBSD: chfs_wbuf.c,v 1.5 2012/10/19 12:44:39 ttoth Exp $ */
|
2012-03-13 02:37:22 +01:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 Department of Software Engineering,
|
|
|
|
* University of Szeged, Hungary
|
|
|
|
* Copyright (C) 2010 Tamas Toth <ttoth@inf.u-szeged.hu>
|
|
|
|
* Copyright (C) 2010 Adam Hoka <ahoka@NetBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by the Department of Software Engineering, University of Szeged, Hungary
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dev/flash/flash.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include "chfs.h"
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#define DBG_WBUF 1 /* XXX unused, but should be */
|
2012-03-13 02:37:22 +01:00
|
|
|
|
|
|
|
#define PAD(x) (((x)+3)&~3)
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#define EB_ADDRESS(x) ( rounddown((x), chmp->chm_ebh->eb_size) )
|
2012-03-13 02:37:22 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
#define PAGE_DIV(x) ( rounddown((x), chmp->chm_wbuf_pagesize) )
|
|
|
|
#define PAGE_MOD(x) ( (x) % (chmp->chm_wbuf_pagesize) )
|
2012-03-13 02:37:22 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* writebuffer options */
|
|
|
|
enum {
|
|
|
|
WBUF_NOPAD,
|
|
|
|
WBUF_SETPAD
|
|
|
|
};
|
2012-03-13 02:37:22 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/*
|
2012-03-13 02:37:22 +01:00
|
|
|
* chfs_flush_wbuf - write wbuf to the flash
|
|
|
|
* Returns zero in case of success.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
chfs_flush_wbuf(struct chfs_mount *chmp, int pad)
|
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
int ret;
|
|
|
|
size_t retlen;
|
|
|
|
struct chfs_node_ref *nref;
|
|
|
|
struct chfs_flash_padding_node* padnode;
|
2012-03-13 02:37:22 +01:00
|
|
|
|
|
|
|
KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
|
|
|
|
KASSERT(mutex_owned(&chmp->chm_lock_sizes));
|
|
|
|
KASSERT(rw_write_held(&chmp->chm_lock_wbuf));
|
2013-12-06 12:04:52 +01:00
|
|
|
KASSERT(pad == WBUF_SETPAD || pad == WBUF_NOPAD);
|
2012-03-13 02:37:22 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* check padding option */
|
|
|
|
if (pad == WBUF_SETPAD) {
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_wbuf_len = PAD(chmp->chm_wbuf_len);
|
2013-12-06 12:04:52 +01:00
|
|
|
memset(chmp->chm_wbuf + chmp->chm_wbuf_len, 0,
|
|
|
|
chmp->chm_wbuf_pagesize - chmp->chm_wbuf_len);
|
2012-03-13 02:37:22 +01:00
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* add a padding node */
|
|
|
|
padnode = (void *)(chmp->chm_wbuf + chmp->chm_wbuf_len);
|
2012-03-13 02:37:22 +01:00
|
|
|
padnode->magic = htole16(CHFS_FS_MAGIC_BITMASK);
|
|
|
|
padnode->type = htole16(CHFS_NODETYPE_PADDING);
|
2013-12-06 12:04:52 +01:00
|
|
|
padnode->length = htole32(chmp->chm_wbuf_pagesize
|
|
|
|
- chmp->chm_wbuf_len);
|
|
|
|
padnode->hdr_crc = htole32(crc32(0, (uint8_t *)padnode,
|
|
|
|
sizeof(*padnode)-4));
|
2012-03-13 02:37:22 +01:00
|
|
|
|
|
|
|
nref = chfs_alloc_node_ref(chmp->chm_nextblock);
|
|
|
|
nref->nref_offset = chmp->chm_wbuf_ofs + chmp->chm_wbuf_len;
|
|
|
|
nref->nref_offset = CHFS_GET_OFS(nref->nref_offset) |
|
|
|
|
CHFS_OBSOLETE_NODE_MASK;
|
|
|
|
chmp->chm_wbuf_len = chmp->chm_wbuf_pagesize;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* change sizes after padding node */
|
|
|
|
chfs_change_size_free(chmp, chmp->chm_nextblock,
|
|
|
|
-padnode->length);
|
|
|
|
chfs_change_size_wasted(chmp, chmp->chm_nextblock,
|
|
|
|
padnode->length);
|
2012-03-13 02:37:22 +01:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* write out the buffer */
|
|
|
|
ret = chfs_write_leb(chmp, chmp->chm_nextblock->lnr, chmp->chm_wbuf,
|
|
|
|
chmp->chm_wbuf_ofs, chmp->chm_wbuf_len, &retlen);
|
|
|
|
if (ret) {
|
2012-03-13 02:37:22 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* reset the buffer */
|
|
|
|
memset(chmp->chm_wbuf, 0xff, chmp->chm_wbuf_pagesize);
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_wbuf_ofs += chmp->chm_wbuf_pagesize;
|
|
|
|
chmp->chm_wbuf_len = 0;
|
2013-12-06 12:04:52 +01:00
|
|
|
|
2012-03-13 02:37:22 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/*
|
|
|
|
* chfs_fill_wbuf - write data to wbuf
|
2012-03-13 02:37:22 +01:00
|
|
|
* Return the len of the buf what we didn't write to the wbuf.
|
|
|
|
*/
|
|
|
|
static size_t
|
|
|
|
chfs_fill_wbuf(struct chfs_mount *chmp, const u_char *buf, size_t len)
|
|
|
|
{
|
2013-12-06 12:04:52 +01:00
|
|
|
/* check available space */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (len && !chmp->chm_wbuf_len && (len >= chmp->chm_wbuf_pagesize)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
/* check buffer's length */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (len > (chmp->chm_wbuf_pagesize - chmp->chm_wbuf_len)) {
|
|
|
|
len = chmp->chm_wbuf_pagesize - chmp->chm_wbuf_len;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
/* write into the wbuf */
|
2012-03-13 02:37:22 +01:00
|
|
|
memcpy(chmp->chm_wbuf + chmp->chm_wbuf_len, buf, len);
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* update the actual length of writebuffer */
|
2012-03-13 02:37:22 +01:00
|
|
|
chmp->chm_wbuf_len += (int) len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/*
|
2012-03-13 02:37:22 +01:00
|
|
|
* chfs_write_wbuf - write to wbuf and then the flash
|
|
|
|
* Returns zero in case of success.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
chfs_write_wbuf(struct chfs_mount* chmp, const struct iovec *invecs, long count,
|
|
|
|
off_t to, size_t *retlen)
|
|
|
|
{
|
|
|
|
int invec, ret = 0;
|
|
|
|
size_t wbuf_retlen, donelen = 0;
|
|
|
|
int outvec_to = to;
|
|
|
|
|
|
|
|
int lnr = chmp->chm_nextblock->lnr;
|
|
|
|
|
|
|
|
KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
|
|
|
|
KASSERT(mutex_owned(&chmp->chm_lock_sizes));
|
|
|
|
KASSERT(!rw_write_held(&chmp->chm_lock_wbuf));
|
|
|
|
|
|
|
|
rw_enter(&chmp->chm_lock_wbuf, RW_WRITER);
|
|
|
|
|
|
|
|
if (chmp->chm_wbuf_ofs == 0xffffffff) {
|
|
|
|
chmp->chm_wbuf_ofs = PAGE_DIV(to);
|
|
|
|
chmp->chm_wbuf_len = PAGE_MOD(to);
|
|
|
|
memset(chmp->chm_wbuf, 0xff, chmp->chm_wbuf_pagesize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EB_ADDRESS(to) != EB_ADDRESS(chmp->chm_wbuf_ofs)) {
|
|
|
|
if (chmp->chm_wbuf_len) {
|
2013-12-06 12:04:52 +01:00
|
|
|
ret = chfs_flush_wbuf(chmp, WBUF_SETPAD);
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ret)
|
|
|
|
goto outerr;
|
|
|
|
}
|
|
|
|
chmp->chm_wbuf_ofs = PAGE_DIV(to);
|
|
|
|
chmp->chm_wbuf_len = PAGE_MOD(to);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to != PAD(chmp->chm_wbuf_ofs + chmp->chm_wbuf_len)) {
|
|
|
|
dbg("to: %llu != %zu\n", (unsigned long long)to,
|
|
|
|
PAD(chmp->chm_wbuf_ofs + chmp->chm_wbuf_len));
|
|
|
|
dbg("Non-contiguous write\n");
|
|
|
|
panic("BUG\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* adjust alignment offset */
|
|
|
|
if (chmp->chm_wbuf_len != PAGE_MOD(to)) {
|
|
|
|
chmp->chm_wbuf_len = PAGE_MOD(to);
|
2013-12-06 12:04:52 +01:00
|
|
|
/* take care of alignement to next page */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (!chmp->chm_wbuf_len) {
|
|
|
|
chmp->chm_wbuf_len += chmp->chm_wbuf_pagesize;
|
2013-12-06 12:04:52 +01:00
|
|
|
ret = chfs_flush_wbuf(chmp, WBUF_NOPAD);
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ret)
|
|
|
|
goto outerr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (invec = 0; invec < count; invec++) {
|
|
|
|
int vlen = invecs[invec].iov_len;
|
|
|
|
u_char* v = invecs[invec].iov_base;
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/* fill the whole wbuf */
|
2012-03-13 02:37:22 +01:00
|
|
|
wbuf_retlen = chfs_fill_wbuf(chmp, v, vlen);
|
|
|
|
if (chmp->chm_wbuf_len == chmp->chm_wbuf_pagesize) {
|
2013-12-06 12:04:52 +01:00
|
|
|
ret = chfs_flush_wbuf(chmp, WBUF_NOPAD);
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ret) {
|
|
|
|
goto outerr;
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
|
2012-03-13 02:37:22 +01:00
|
|
|
vlen -= wbuf_retlen;
|
|
|
|
outvec_to += wbuf_retlen;
|
|
|
|
v += wbuf_retlen;
|
|
|
|
donelen += wbuf_retlen;
|
2013-12-06 12:04:52 +01:00
|
|
|
|
|
|
|
/* if there is more residual data than the length of the wbuf
|
|
|
|
* write it out directly until it's fit in the wbuf */
|
2012-03-13 02:37:22 +01:00
|
|
|
if (vlen >= chmp->chm_wbuf_pagesize) {
|
|
|
|
ret = chfs_write_leb(chmp, lnr, v, outvec_to, PAGE_DIV(vlen), &wbuf_retlen);
|
|
|
|
vlen -= wbuf_retlen;
|
|
|
|
outvec_to += wbuf_retlen;
|
|
|
|
chmp->chm_wbuf_ofs = outvec_to;
|
|
|
|
v += wbuf_retlen;
|
|
|
|
donelen += wbuf_retlen;
|
|
|
|
}
|
2013-12-06 12:04:52 +01:00
|
|
|
|
|
|
|
/* write the residual data to the wbuf */
|
2012-03-13 02:37:22 +01:00
|
|
|
wbuf_retlen = chfs_fill_wbuf(chmp, v, vlen);
|
|
|
|
if (chmp->chm_wbuf_len == chmp->chm_wbuf_pagesize) {
|
2013-12-06 12:04:52 +01:00
|
|
|
ret = chfs_flush_wbuf(chmp, WBUF_NOPAD);
|
2012-03-13 02:37:22 +01:00
|
|
|
if (ret)
|
|
|
|
goto outerr;
|
|
|
|
}
|
|
|
|
|
|
|
|
outvec_to += wbuf_retlen;
|
|
|
|
donelen += wbuf_retlen;
|
|
|
|
}
|
|
|
|
*retlen = donelen;
|
|
|
|
rw_exit(&chmp->chm_lock_wbuf);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
outerr:
|
|
|
|
*retlen = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-12-06 12:04:52 +01:00
|
|
|
/*
|
|
|
|
* chfs_flush_peding_wbuf - write wbuf to the flash
|
|
|
|
* Used when we must flush wbuf right now.
|
|
|
|
* If wbuf has free space, pad it to the size of wbuf and write out.
|
|
|
|
*/
|
2012-03-13 02:37:22 +01:00
|
|
|
int chfs_flush_pending_wbuf(struct chfs_mount *chmp)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
KASSERT(mutex_owned(&chmp->chm_lock_mountfields));
|
|
|
|
mutex_enter(&chmp->chm_lock_sizes);
|
|
|
|
rw_enter(&chmp->chm_lock_wbuf, RW_WRITER);
|
2013-12-06 12:04:52 +01:00
|
|
|
err = chfs_flush_wbuf(chmp, WBUF_SETPAD);
|
2012-03-13 02:37:22 +01:00
|
|
|
rw_exit(&chmp->chm_lock_wbuf);
|
|
|
|
mutex_exit(&chmp->chm_lock_sizes);
|
|
|
|
return err;
|
|
|
|
}
|