Home | History | Annotate | Line # | Download | only in chfs
      1  1.11    andvar /*	$NetBSD: ebh.c,v 1.11 2025/01/08 11:39:50 andvar Exp $	*/
      2   1.1     ahoka 
      3   1.1     ahoka /*-
      4   1.1     ahoka  * Copyright (c) 2010 Department of Software Engineering,
      5   1.1     ahoka  *		      University of Szeged, Hungary
      6   1.1     ahoka  * Copyright (C) 2009 Ferenc Havasi <havasi (at) inf.u-szeged.hu>
      7   1.1     ahoka  * Copyright (C) 2009 Zoltan Sogor <weth (at) inf.u-szeged.hu>
      8   1.1     ahoka  * Copyright (C) 2009 David Tengeri <dtengeri (at) inf.u-szeged.hu>
      9   1.1     ahoka  * Copyright (C) 2009 Tamas Toth <ttoth (at) inf.u-szeged.hu>
     10   1.1     ahoka  * Copyright (C) 2010 Adam Hoka <ahoka (at) NetBSD.org>
     11   1.1     ahoka  * All rights reserved.
     12   1.1     ahoka  *
     13   1.1     ahoka  * This code is derived from software contributed to The NetBSD Foundation
     14   1.1     ahoka  * by the Department of Software Engineering, University of Szeged, Hungary
     15   1.1     ahoka  *
     16   1.1     ahoka  * Redistribution and use in source and binary forms, with or without
     17   1.1     ahoka  * modification, are permitted provided that the following conditions
     18   1.1     ahoka  * are met:
     19   1.1     ahoka  * 1. Redistributions of source code must retain the above copyright
     20   1.1     ahoka  *    notice, this list of conditions and the following disclaimer.
     21   1.1     ahoka  * 2. Redistributions in binary form must reproduce the above copyright
     22   1.1     ahoka  *    notice, this list of conditions and the following disclaimer in the
     23   1.1     ahoka  *    documentation and/or other materials provided with the distribution.
     24   1.1     ahoka  *
     25   1.1     ahoka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     26   1.1     ahoka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     27   1.1     ahoka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     28   1.1     ahoka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     29   1.1     ahoka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     30   1.1     ahoka  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     31   1.1     ahoka  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     32   1.1     ahoka  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     33   1.1     ahoka  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1     ahoka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1     ahoka  * SUCH DAMAGE.
     36   1.1     ahoka  */
     37   1.1     ahoka 
     38   1.1     ahoka #include "ebh.h"
     39   1.1     ahoka 
     40   1.1     ahoka /*****************************************************************************/
     41   1.1     ahoka /* Flash specific operations						     */
     42   1.1     ahoka /*****************************************************************************/
     43   1.1     ahoka int nor_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr);
     44   1.1     ahoka int nand_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr);
     45   1.1     ahoka int nor_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset);
     46   1.1     ahoka int nand_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset);
     47   1.1     ahoka int nor_read_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr);
     48   1.1     ahoka int nand_read_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr);
     49   1.1     ahoka int nor_write_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr);
     50   1.1     ahoka int nand_write_eb_hdr(struct chfs_ebh *ebh, int pebnr,struct chfs_eb_hdr *ebhdr);
     51   1.1     ahoka int nor_check_eb_hdr(struct chfs_ebh *ebh, void *buf);
     52   1.1     ahoka int nand_check_eb_hdr(struct chfs_ebh *ebh, void *buf);
     53   1.1     ahoka int nor_mark_eb_hdr_dirty_flash(struct chfs_ebh *ebh, int pebnr, int lid);
     54   1.1     ahoka int nor_invalidate_eb_hdr(struct chfs_ebh *ebh, int pebnr);
     55   1.1     ahoka int mark_eb_hdr_free(struct chfs_ebh *ebh, int pebnr, int ec);
     56   1.1     ahoka 
     57   1.1     ahoka int ltree_entry_cmp(struct chfs_ltree_entry *le1, struct chfs_ltree_entry *le2);
     58   1.1     ahoka int peb_in_use_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2);
     59   1.1     ahoka int peb_free_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2);
     60   1.1     ahoka int add_peb_to_erase_queue(struct chfs_ebh *ebh, int pebnr, int ec,struct peb_queue *queue);
     61   1.1     ahoka struct chfs_peb * find_peb_in_use(struct chfs_ebh *ebh, int pebnr);
     62   1.1     ahoka int add_peb_to_free(struct chfs_ebh *ebh, int pebnr, int ec);
     63   1.1     ahoka int add_peb_to_in_use(struct chfs_ebh *ebh, int pebnr, int ec);
     64   1.1     ahoka void erase_callback(struct flash_erase_instruction *ei);
     65   1.1     ahoka int free_peb(struct chfs_ebh *ebh);
     66   1.1     ahoka int release_peb(struct chfs_ebh *ebh, int pebnr);
     67   1.1     ahoka void erase_thread(void *data);
     68   1.1     ahoka static void erase_thread_start(struct chfs_ebh *ebh);
     69   1.1     ahoka static void erase_thread_stop(struct chfs_ebh *ebh);
     70   1.1     ahoka int scan_leb_used_cmp(struct chfs_scan_leb *sleb1, struct chfs_scan_leb *sleb2);
     71   1.1     ahoka int nor_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,struct chfs_eb_hdr *ebhdr, int pebnr, int leb_status);
     72   1.1     ahoka int nor_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
     73   1.1     ahoka     int pebnr, struct chfs_eb_hdr *ebhdr);
     74   1.1     ahoka int nand_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,struct chfs_eb_hdr *ebhdr, int pebnr);
     75   1.1     ahoka int nand_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
     76   1.1     ahoka     int pebnr, struct chfs_eb_hdr *ebhdr);
     77   1.1     ahoka struct chfs_scan_info *chfs_scan(struct chfs_ebh *ebh);
     78   1.1     ahoka void scan_info_destroy(struct chfs_scan_info *si);
     79   1.1     ahoka int scan_media(struct chfs_ebh *ebh);
     80   1.1     ahoka int get_peb(struct chfs_ebh *ebh);
     81   1.1     ahoka /**
     82   1.1     ahoka  * nor_create_eb_hdr - creates an eraseblock header for NOR flash
     83   1.1     ahoka  * @ebhdr: ebhdr to set
     84   1.1     ahoka  * @lnr: LEB number
     85   1.1     ahoka  */
     86   1.1     ahoka int
     87   1.1     ahoka nor_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr)
     88   1.1     ahoka {
     89   1.1     ahoka 	ebhdr->u.nor_hdr.lid = htole32(lnr);
     90   1.1     ahoka 	return 0;
     91   1.1     ahoka }
     92   1.1     ahoka 
     93   1.1     ahoka /**
     94   1.1     ahoka  * nand_create_eb_hdr - creates an eraseblock header for NAND flash
     95   1.1     ahoka  * @ebhdr: ebhdr to set
     96   1.1     ahoka  * @lnr: LEB number
     97   1.1     ahoka  */
     98   1.1     ahoka int
     99   1.1     ahoka nand_create_eb_hdr(struct chfs_eb_hdr *ebhdr, int lnr)
    100   1.1     ahoka {
    101   1.1     ahoka 	ebhdr->u.nand_hdr.lid = htole32(lnr);
    102   1.1     ahoka 	return 0;
    103   1.1     ahoka }
    104   1.1     ahoka 
    105   1.1     ahoka /**
    106   1.1     ahoka  * nor_calc_data_offs - calculates data offset on NOR flash
    107   1.1     ahoka  * @ebh: chfs eraseblock handler
    108   1.1     ahoka  * @pebnr: eraseblock number
    109   1.1     ahoka  * @offset: offset within the eraseblock
    110   1.1     ahoka  */
    111   1.1     ahoka int
    112   1.1     ahoka nor_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset)
    113   1.1     ahoka {
    114   1.1     ahoka 	return pebnr * ebh->flash_if->erasesize + offset +
    115   1.1     ahoka 	    CHFS_EB_EC_HDR_SIZE + CHFS_EB_HDR_NOR_SIZE;
    116   1.1     ahoka }
    117   1.1     ahoka 
    118   1.1     ahoka /**
    119   1.1     ahoka  * nand_calc_data_offs - calculates data offset on NAND flash
    120   1.1     ahoka  * @ebh: chfs eraseblock handler
    121   1.1     ahoka  * @pebnr: eraseblock number
    122   1.1     ahoka  * @offset: offset within the eraseblock
    123   1.1     ahoka  */
    124   1.1     ahoka int
    125   1.1     ahoka nand_calc_data_offs(struct chfs_ebh *ebh, int pebnr, int offset)
    126   1.1     ahoka {
    127   1.1     ahoka 	return pebnr * ebh->flash_if->erasesize + offset +
    128   1.1     ahoka 	    2 * ebh->flash_if->page_size;
    129   1.1     ahoka }
    130   1.1     ahoka 
    131   1.1     ahoka /**
    132   1.9    andvar  * nor_read_eb_hdr - read eraseblock header from NOR flash
    133   1.1     ahoka  *
    134   1.1     ahoka  * @ebh: chfs eraseblock handler
    135   1.1     ahoka  * @pebnr: eraseblock number
    136   1.1     ahoka  * @ebhdr: whereto store the data
    137   1.1     ahoka  *
    138   1.1     ahoka  * Reads the eraseblock header from media.
    139   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    140   1.1     ahoka  */
    141   1.1     ahoka int
    142   1.1     ahoka nor_read_eb_hdr(struct chfs_ebh *ebh,
    143   1.1     ahoka     int pebnr, struct chfs_eb_hdr *ebhdr)
    144   1.1     ahoka {
    145   1.1     ahoka 	int ret;
    146   1.1     ahoka 	size_t retlen;
    147   1.1     ahoka 	off_t ofs = pebnr * ebh->flash_if->erasesize;
    148   1.1     ahoka 
    149   1.1     ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    150   1.1     ahoka 
    151   1.1     ahoka 	ret = flash_read(ebh->flash_dev,
    152   1.1     ahoka 	    ofs, CHFS_EB_EC_HDR_SIZE,
    153   1.1     ahoka 	    &retlen, (unsigned char *) &ebhdr->ec_hdr);
    154   1.1     ahoka 
    155   1.1     ahoka 	if (ret || retlen != CHFS_EB_EC_HDR_SIZE)
    156   1.1     ahoka 		return ret;
    157   1.1     ahoka 
    158   1.1     ahoka 	ofs += CHFS_EB_EC_HDR_SIZE;
    159   1.1     ahoka 	ret = flash_read(ebh->flash_dev,
    160   1.1     ahoka 	    ofs, CHFS_EB_HDR_NOR_SIZE,
    161   1.1     ahoka 	    &retlen, (unsigned char *) &ebhdr->u.nor_hdr);
    162   1.1     ahoka 
    163   1.1     ahoka 	if (ret || retlen != CHFS_EB_HDR_NOR_SIZE)
    164   1.1     ahoka 		return ret;
    165   1.1     ahoka 
    166   1.1     ahoka 	return 0;
    167   1.1     ahoka }
    168   1.1     ahoka 
    169   1.1     ahoka /**
    170   1.9    andvar  * nand_read_eb_hdr - read eraseblock header from NAND flash
    171   1.1     ahoka  *
    172   1.1     ahoka  * @ebh: chfs eraseblock handler
    173   1.1     ahoka  * @pebnr: eraseblock number
    174   1.1     ahoka  * @ebhdr: whereto store the data
    175   1.1     ahoka  *
    176   1.1     ahoka  * Reads the eraseblock header from media. It is on the first two page.
    177   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    178   1.1     ahoka  */
    179   1.1     ahoka int
    180   1.1     ahoka nand_read_eb_hdr(struct chfs_ebh *ebh, int pebnr,
    181   1.1     ahoka     struct chfs_eb_hdr *ebhdr)
    182   1.1     ahoka {
    183   1.1     ahoka 	int ret;
    184   1.1     ahoka 	size_t retlen;
    185   1.1     ahoka 	off_t ofs;
    186   1.1     ahoka 
    187   1.1     ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    188   1.1     ahoka 
    189   1.1     ahoka 	/* Read erase counter header from the first page. */
    190   1.1     ahoka 	ofs = pebnr * ebh->flash_if->erasesize;
    191   1.1     ahoka 	ret = flash_read(ebh->flash_dev,
    192   1.1     ahoka 	    ofs, CHFS_EB_EC_HDR_SIZE, &retlen,
    193   1.1     ahoka 	    (unsigned char *) &ebhdr->ec_hdr);
    194   1.1     ahoka 	if (ret || retlen != CHFS_EB_EC_HDR_SIZE)
    195   1.1     ahoka 		return ret;
    196   1.1     ahoka 
    197   1.1     ahoka 	/* Read NAND eraseblock header from the second page */
    198   1.1     ahoka 	ofs += ebh->flash_if->page_size;
    199   1.1     ahoka 	ret = flash_read(ebh->flash_dev,
    200   1.1     ahoka 	    ofs, CHFS_EB_HDR_NAND_SIZE, &retlen,
    201   1.1     ahoka 	    (unsigned char *) &ebhdr->u.nand_hdr);
    202   1.1     ahoka 	if (ret || retlen != CHFS_EB_HDR_NAND_SIZE)
    203   1.1     ahoka 		return ret;
    204   1.1     ahoka 
    205   1.1     ahoka 	return 0;
    206   1.1     ahoka }
    207   1.1     ahoka 
    208   1.1     ahoka /**
    209   1.9    andvar  * nor_write_eb_hdr - write eraseblock header to NOR flash
    210   1.1     ahoka  *
    211   1.1     ahoka  * @ebh: chfs eraseblock handler
    212   1.1     ahoka  * @pebnr: eraseblock number whereto write
    213   1.1     ahoka  * @ebh: ebh to write
    214   1.1     ahoka  *
    215   1.1     ahoka  * Writes the eraseblock header to media.
    216   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    217   1.1     ahoka  */
    218   1.1     ahoka int
    219   1.1     ahoka nor_write_eb_hdr(struct chfs_ebh *ebh, int pebnr, struct chfs_eb_hdr *ebhdr)
    220   1.1     ahoka {
    221   1.1     ahoka 	int ret, crc;
    222   1.1     ahoka 	size_t retlen;
    223   1.1     ahoka 
    224   1.1     ahoka 	off_t ofs = pebnr * ebh->flash_if->erasesize + CHFS_EB_EC_HDR_SIZE;
    225   1.1     ahoka 
    226   1.1     ahoka 	ebhdr->u.nor_hdr.lid = ebhdr->u.nor_hdr.lid
    227   1.1     ahoka 	    | htole32(CHFS_LID_NOT_DIRTY_BIT);
    228   1.1     ahoka 
    229   1.1     ahoka 	crc = crc32(0, (uint8_t *)&ebhdr->u.nor_hdr + 4,
    230   1.1     ahoka 	    CHFS_EB_HDR_NOR_SIZE - 4);
    231   1.1     ahoka 	ebhdr->u.nand_hdr.crc = htole32(crc);
    232   1.1     ahoka 
    233   1.1     ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    234   1.1     ahoka 
    235   1.1     ahoka 	ret = flash_write(ebh->flash_dev,
    236   1.1     ahoka 	    ofs, CHFS_EB_HDR_NOR_SIZE, &retlen,
    237   1.1     ahoka 	    (unsigned char *) &ebhdr->u.nor_hdr);
    238   1.1     ahoka 
    239   1.1     ahoka 	if (ret || retlen != CHFS_EB_HDR_NOR_SIZE)
    240   1.1     ahoka 		return ret;
    241   1.1     ahoka 
    242   1.1     ahoka 	return 0;
    243   1.1     ahoka }
    244   1.1     ahoka 
    245   1.1     ahoka /**
    246   1.9    andvar  * nand_write_eb_hdr - write eraseblock header to NAND flash
    247   1.1     ahoka  *
    248   1.1     ahoka  * @ebh: chfs eraseblock handler
    249   1.1     ahoka  * @pebnr: eraseblock number whereto write
    250   1.1     ahoka  * @ebh: ebh to write
    251   1.1     ahoka  *
    252   1.1     ahoka  * Writes the eraseblock header to media.
    253   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    254   1.1     ahoka  */
    255   1.1     ahoka int
    256   1.1     ahoka nand_write_eb_hdr(struct chfs_ebh *ebh, int pebnr,
    257   1.1     ahoka     struct chfs_eb_hdr *ebhdr)
    258   1.1     ahoka {
    259   1.1     ahoka 	int ret, crc;
    260   1.1     ahoka 	size_t retlen;
    261   1.1     ahoka 	flash_off_t ofs;
    262   1.1     ahoka 
    263   1.1     ahoka 	KASSERT(pebnr >= 0 && pebnr < ebh->peb_nr);
    264   1.1     ahoka 
    265   1.1     ahoka 	ofs = pebnr * ebh->flash_if->erasesize +
    266   1.1     ahoka 	    ebh->flash_if->page_size;
    267   1.1     ahoka 
    268   1.1     ahoka 	ebhdr->u.nand_hdr.serial = htole64(++(*ebh->max_serial));
    269   1.1     ahoka 
    270   1.1     ahoka 	crc = crc32(0, (uint8_t *)&ebhdr->u.nand_hdr + 4,
    271   1.1     ahoka 	    CHFS_EB_HDR_NAND_SIZE - 4);
    272   1.1     ahoka 	ebhdr->u.nand_hdr.crc = htole32(crc);
    273   1.1     ahoka 
    274   1.1     ahoka 	ret = flash_write(ebh->flash_dev, ofs,
    275   1.1     ahoka 	    CHFS_EB_HDR_NAND_SIZE, &retlen,
    276   1.1     ahoka 	    (unsigned char *) &ebhdr->u.nand_hdr);
    277   1.1     ahoka 
    278   1.1     ahoka 	if (ret || retlen != CHFS_EB_HDR_NAND_SIZE)
    279   1.1     ahoka 		return ret;
    280   1.1     ahoka 
    281   1.1     ahoka 	return 0;
    282   1.1     ahoka }
    283   1.1     ahoka 
    284   1.1     ahoka /**
    285   1.9    andvar  * nor_check_eb_hdr - check eraseblock header read from NOR flash
    286   1.1     ahoka  *
    287   1.1     ahoka  * @ebh: chfs eraseblock handler
    288   1.1     ahoka  * @buf: eraseblock header to check
    289   1.1     ahoka  *
    290   1.1     ahoka  * Returns eraseblock header status.
    291   1.1     ahoka  */
    292   1.1     ahoka int
    293   1.1     ahoka nor_check_eb_hdr(struct chfs_ebh *ebh, void *buf)
    294   1.1     ahoka {
    295   1.1     ahoka 	uint32_t magic, crc, hdr_crc;
    296   1.1     ahoka 	struct chfs_eb_hdr *ebhdr = buf;
    297   1.1     ahoka 	le32 lid_save;
    298   1.1     ahoka 
    299   1.1     ahoka 	//check is there a header
    300   1.1     ahoka 	if (check_pattern((void *) &ebhdr->ec_hdr,
    301   1.1     ahoka 		0xFF, 0, CHFS_EB_EC_HDR_SIZE)) {
    302   1.1     ahoka 		dbg_ebh("no header found\n");
    303   1.1     ahoka 		return EBHDR_LEB_NO_HDR;
    304   1.1     ahoka 	}
    305   1.1     ahoka 
    306   1.1     ahoka 	// check magic
    307   1.1     ahoka 	magic = le32toh(ebhdr->ec_hdr.magic);
    308   1.1     ahoka 	if (magic != CHFS_MAGIC_BITMASK) {
    309   1.1     ahoka 		dbg_ebh("bad magic bitmask(exp: %x found %x)\n",
    310   1.1     ahoka 		    CHFS_MAGIC_BITMASK, magic);
    311   1.1     ahoka 		return EBHDR_LEB_BADMAGIC;
    312   1.1     ahoka 	}
    313   1.1     ahoka 
    314   1.1     ahoka 	// check CRC_EC
    315   1.1     ahoka 	hdr_crc = le32toh(ebhdr->ec_hdr.crc_ec);
    316   1.1     ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->ec_hdr + 8, 4);
    317   1.1     ahoka 	if (hdr_crc != crc) {
    318   1.1     ahoka 		dbg_ebh("bad crc_ec found\n");
    319   1.1     ahoka 		return EBHDR_LEB_BADCRC;
    320   1.1     ahoka 	}
    321   1.1     ahoka 
    322   1.1     ahoka 	/* check if the PEB is free: magic, crc_ec and erase_cnt is good and
    323   1.1     ahoka 	 * everything else is FFF..
    324   1.1     ahoka 	 */
    325   1.1     ahoka 	if (check_pattern((void *) &ebhdr->u.nor_hdr, 0xFF, 0,
    326   1.1     ahoka 		CHFS_EB_HDR_NOR_SIZE)) {
    327   1.1     ahoka 		dbg_ebh("free peb found\n");
    328   1.1     ahoka 		return EBHDR_LEB_FREE;
    329   1.1     ahoka 	}
    330   1.1     ahoka 
    331   1.1     ahoka 	// check invalidated (CRC == LID == 0)
    332   1.1     ahoka 	if (ebhdr->u.nor_hdr.crc == 0 && ebhdr->u.nor_hdr.lid == 0) {
    333   1.1     ahoka 		dbg_ebh("invalidated ebhdr found\n");
    334   1.1     ahoka 		return EBHDR_LEB_INVALIDATED;
    335   1.1     ahoka 	}
    336   1.1     ahoka 
    337   1.1     ahoka 	// check CRC
    338   1.1     ahoka 	hdr_crc = le32toh(ebhdr->u.nor_hdr.crc);
    339   1.1     ahoka 	lid_save = ebhdr->u.nor_hdr.lid;
    340   1.1     ahoka 
    341   1.1     ahoka 	// mark lid as not dirty for crc calc
    342   1.1     ahoka 	ebhdr->u.nor_hdr.lid = ebhdr->u.nor_hdr.lid | htole32(
    343   1.1     ahoka 		CHFS_LID_NOT_DIRTY_BIT);
    344   1.1     ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->u.nor_hdr + 4,
    345   1.1     ahoka 	    CHFS_EB_HDR_NOR_SIZE - 4);
    346   1.1     ahoka 	// restore the original lid value in ebh
    347   1.1     ahoka 	ebhdr->u.nor_hdr.lid = lid_save;
    348   1.1     ahoka 
    349   1.1     ahoka 	if (crc != hdr_crc) {
    350   1.1     ahoka 		dbg_ebh("bad crc found\n");
    351   1.1     ahoka 		return EBHDR_LEB_BADCRC;
    352   1.1     ahoka 	}
    353   1.1     ahoka 
    354   1.1     ahoka 	// check dirty
    355   1.1     ahoka 	if (!(le32toh(lid_save) & CHFS_LID_NOT_DIRTY_BIT)) {
    356   1.1     ahoka 		dbg_ebh("dirty ebhdr found\n");
    357   1.1     ahoka 		return EBHDR_LEB_DIRTY;
    358   1.1     ahoka 	}
    359   1.1     ahoka 
    360   1.1     ahoka 	return EBHDR_LEB_OK;
    361   1.1     ahoka }
    362   1.1     ahoka 
    363   1.1     ahoka /**
    364   1.9    andvar  * nand_check_eb_hdr - check eraseblock header read from NAND flash
    365   1.1     ahoka  *
    366   1.1     ahoka  * @ebh: chfs eraseblock handler
    367   1.1     ahoka  * @buf: eraseblock header to check
    368   1.1     ahoka  *
    369   1.1     ahoka  * Returns eraseblock header status.
    370   1.1     ahoka  */
    371   1.1     ahoka int
    372   1.1     ahoka nand_check_eb_hdr(struct chfs_ebh *ebh, void *buf)
    373   1.1     ahoka {
    374   1.1     ahoka 	uint32_t magic, crc, hdr_crc;
    375   1.1     ahoka 	struct chfs_eb_hdr *ebhdr = buf;
    376   1.1     ahoka 
    377   1.1     ahoka 	//check is there a header
    378   1.1     ahoka 	if (check_pattern((void *) &ebhdr->ec_hdr,
    379   1.1     ahoka 		0xFF, 0, CHFS_EB_EC_HDR_SIZE)) {
    380   1.1     ahoka 		dbg_ebh("no header found\n");
    381   1.1     ahoka 		return EBHDR_LEB_NO_HDR;
    382   1.1     ahoka 	}
    383   1.1     ahoka 
    384   1.1     ahoka 	// check magic
    385   1.1     ahoka 	magic = le32toh(ebhdr->ec_hdr.magic);
    386   1.1     ahoka 	if (magic != CHFS_MAGIC_BITMASK) {
    387   1.1     ahoka 		dbg_ebh("bad magic bitmask(exp: %x found %x)\n",
    388   1.1     ahoka 		    CHFS_MAGIC_BITMASK, magic);
    389   1.1     ahoka 		return EBHDR_LEB_BADMAGIC;
    390   1.1     ahoka 	}
    391   1.1     ahoka 
    392   1.1     ahoka 	// check CRC_EC
    393   1.1     ahoka 	hdr_crc = le32toh(ebhdr->ec_hdr.crc_ec);
    394   1.1     ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->ec_hdr + 8, 4);
    395   1.1     ahoka 	if (hdr_crc != crc) {
    396   1.1     ahoka 		dbg_ebh("bad crc_ec found\n");
    397   1.1     ahoka 		return EBHDR_LEB_BADCRC;
    398   1.1     ahoka 	}
    399   1.1     ahoka 
    400   1.1     ahoka 	/* check if the PEB is free: magic, crc_ec and erase_cnt is good and
    401   1.1     ahoka 	 * everything else is FFF..
    402   1.1     ahoka 	 */
    403   1.1     ahoka 	if (check_pattern((void *) &ebhdr->u.nand_hdr, 0xFF, 0,
    404   1.1     ahoka 		CHFS_EB_HDR_NAND_SIZE)) {
    405   1.1     ahoka 		dbg_ebh("free peb found\n");
    406   1.1     ahoka 		return EBHDR_LEB_FREE;
    407   1.1     ahoka 	}
    408   1.1     ahoka 
    409   1.1     ahoka 	// check CRC
    410   1.1     ahoka 	hdr_crc = le32toh(ebhdr->u.nand_hdr.crc);
    411   1.1     ahoka 
    412   1.1     ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->u.nand_hdr + 4,
    413   1.1     ahoka 	    CHFS_EB_HDR_NAND_SIZE - 4);
    414   1.1     ahoka 
    415   1.1     ahoka 	if (crc != hdr_crc) {
    416   1.1     ahoka 		dbg_ebh("bad crc found\n");
    417   1.1     ahoka 		return EBHDR_LEB_BADCRC;
    418   1.1     ahoka 	}
    419   1.1     ahoka 
    420   1.1     ahoka 	return EBHDR_LEB_OK;
    421   1.1     ahoka }
    422   1.1     ahoka 
    423   1.1     ahoka /**
    424   1.9    andvar  * nor_mark_eb_hdr_dirty_flash- mark eraseblock header dirty on NOR flash
    425   1.1     ahoka  *
    426   1.1     ahoka  * @ebh: chfs eraseblock handler
    427   1.1     ahoka  * @pebnr: eraseblock number
    428   1.5       snj  * @lid: leb id (its bit number 31 will be set to 0)
    429   1.1     ahoka  *
    430   1.1     ahoka  * It pulls the CHFS_LID_NOT_DIRTY_BIT to zero on flash.
    431   1.1     ahoka  *
    432   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    433   1.1     ahoka  */
    434   1.1     ahoka int
    435   1.1     ahoka nor_mark_eb_hdr_dirty_flash(struct chfs_ebh *ebh, int pebnr, int lid)
    436   1.1     ahoka {
    437   1.1     ahoka 	int ret;
    438   1.1     ahoka 	size_t retlen;
    439   1.1     ahoka 	off_t ofs;
    440   1.1     ahoka 
    441   1.1     ahoka 	/* mark leb id dirty */
    442   1.1     ahoka 	lid = htole32(lid & CHFS_LID_DIRTY_BIT_MASK);
    443   1.1     ahoka 
    444   1.1     ahoka 	/* calculate position */
    445   1.1     ahoka 	ofs = pebnr * ebh->flash_if->erasesize + CHFS_EB_EC_HDR_SIZE
    446   1.1     ahoka 	    + CHFS_GET_MEMBER_POS(struct chfs_nor_eb_hdr , lid);
    447   1.1     ahoka 
    448   1.1     ahoka 	ret = flash_write(ebh->flash_dev, ofs, sizeof(lid), &retlen,
    449   1.1     ahoka 	    (unsigned char *) &lid);
    450   1.1     ahoka 	if (ret || retlen != sizeof(lid)) {
    451   1.1     ahoka 		chfs_err("can't mark peb dirty");
    452   1.1     ahoka 		return ret;
    453   1.1     ahoka 	}
    454   1.1     ahoka 
    455   1.1     ahoka 	return 0;
    456   1.1     ahoka }
    457   1.1     ahoka 
    458   1.1     ahoka /**
    459   1.9    andvar  * nor_invalidate_eb_hdr - invalidate eraseblock header on NOR flash
    460   1.1     ahoka  *
    461   1.1     ahoka  * @ebh: chfs eraseblock handler
    462   1.1     ahoka  * @pebnr: eraseblock number
    463   1.1     ahoka  *
    464   1.1     ahoka  * Sets crc and lip field to zero.
    465   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    466   1.1     ahoka  */
    467   1.1     ahoka int
    468   1.1     ahoka nor_invalidate_eb_hdr(struct chfs_ebh *ebh, int pebnr)
    469   1.1     ahoka {
    470   1.1     ahoka 	int ret;
    471   1.1     ahoka 	size_t retlen;
    472   1.1     ahoka 	off_t ofs;
    473   1.1     ahoka 	char zero_buf[CHFS_INVALIDATE_SIZE];
    474   1.1     ahoka 
    475   1.1     ahoka 	/* fill with zero */
    476   1.1     ahoka 	memset(zero_buf, 0x0, CHFS_INVALIDATE_SIZE);
    477   1.1     ahoka 
    478   1.1     ahoka 	/* calculate position (!!! lid is directly behind crc !!!) */
    479   1.1     ahoka 	ofs = pebnr * ebh->flash_if->erasesize + CHFS_EB_EC_HDR_SIZE
    480   1.1     ahoka 	    + CHFS_GET_MEMBER_POS(struct chfs_nor_eb_hdr, crc);
    481   1.1     ahoka 
    482   1.1     ahoka 	ret = flash_write(ebh->flash_dev,
    483   1.1     ahoka 	    ofs, CHFS_INVALIDATE_SIZE, &retlen,
    484   1.1     ahoka 	    (unsigned char *) &zero_buf);
    485   1.1     ahoka 	if (ret || retlen != CHFS_INVALIDATE_SIZE) {
    486   1.1     ahoka 		chfs_err("can't invalidate peb");
    487   1.1     ahoka 		return ret;
    488   1.1     ahoka 	}
    489   1.1     ahoka 
    490   1.1     ahoka 	return 0;
    491   1.1     ahoka }
    492   1.1     ahoka 
    493   1.1     ahoka /**
    494   1.9    andvar  * mark_eb_hdr_free - free eraseblock header on NOR or NAND flash
    495   1.1     ahoka  *
    496   1.1     ahoka  * @ebh: chfs eraseblock handler
    497   1.1     ahoka  * @pebnr: eraseblock number
    498   1.1     ahoka  * @ec: erase counter of PEB
    499   1.1     ahoka  *
    500   1.1     ahoka  * Write out the magic and erase counter to the physical eraseblock.
    501   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    502   1.1     ahoka  */
    503   1.1     ahoka int
    504   1.1     ahoka mark_eb_hdr_free(struct chfs_ebh *ebh, int pebnr, int ec)
    505   1.1     ahoka {
    506   1.1     ahoka 	int ret, crc;
    507   1.1     ahoka 	size_t retlen;
    508   1.1     ahoka 	off_t ofs;
    509   1.1     ahoka 	struct chfs_eb_hdr *ebhdr;
    510   1.1     ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
    511   1.1     ahoka 
    512   1.1     ahoka 	ebhdr->ec_hdr.magic = htole32(CHFS_MAGIC_BITMASK);
    513   1.1     ahoka 	ebhdr->ec_hdr.erase_cnt = htole32(ec);
    514   1.1     ahoka 	crc = crc32(0, (uint8_t *) &ebhdr->ec_hdr + 8, 4);
    515   1.1     ahoka 	ebhdr->ec_hdr.crc_ec = htole32(crc);
    516   1.1     ahoka 
    517   1.1     ahoka 	ofs = pebnr * ebh->flash_if->erasesize;
    518   1.1     ahoka 
    519   1.1     ahoka 	KASSERT(sizeof(ebhdr->ec_hdr) == CHFS_EB_EC_HDR_SIZE);
    520   1.1     ahoka 
    521   1.1     ahoka 	ret = flash_write(ebh->flash_dev,
    522   1.1     ahoka 	    ofs, CHFS_EB_EC_HDR_SIZE, &retlen,
    523   1.1     ahoka 	    (unsigned char *) &ebhdr->ec_hdr);
    524   1.1     ahoka 
    525   1.1     ahoka 	if (ret || retlen != CHFS_EB_EC_HDR_SIZE) {
    526   1.1     ahoka 		chfs_err("can't mark peb as free: %d\n", pebnr);
    527   1.1     ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
    528   1.1     ahoka 		return ret;
    529   1.1     ahoka 	}
    530   1.1     ahoka 
    531   1.1     ahoka 	kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
    532   1.1     ahoka 	return 0;
    533   1.1     ahoka }
    534   1.1     ahoka 
    535   1.1     ahoka /*****************************************************************************/
    536   1.1     ahoka /* End of Flash specific operations					     */
    537   1.1     ahoka /*****************************************************************************/
    538   1.1     ahoka 
    539   1.1     ahoka /*****************************************************************************/
    540   1.1     ahoka /* Lock Tree								     */
    541   1.1     ahoka /*****************************************************************************/
    542   1.1     ahoka 
    543   1.1     ahoka int
    544   1.1     ahoka ltree_entry_cmp(struct chfs_ltree_entry *le1,
    545   1.1     ahoka     struct chfs_ltree_entry *le2)
    546   1.1     ahoka {
    547   1.1     ahoka 	return (le1->lnr - le2->lnr);
    548   1.1     ahoka }
    549   1.1     ahoka 
    550   1.1     ahoka /* Generate functions for Lock tree's red-black tree */
    551   1.1     ahoka RB_PROTOTYPE( ltree_rbtree, chfs_ltree_entry, rb, ltree_entry_cmp);
    552   1.1     ahoka RB_GENERATE( ltree_rbtree, chfs_ltree_entry, rb, ltree_entry_cmp);
    553   1.1     ahoka 
    554   1.1     ahoka 
    555   1.1     ahoka /**
    556   1.1     ahoka  * ltree_lookup - looks up a logical eraseblock in the lock tree
    557   1.1     ahoka  * @ebh: chfs eraseblock handler
    558   1.1     ahoka  * @lid: identifier of the logical eraseblock
    559   1.1     ahoka  *
    560   1.1     ahoka  * This function returns a pointer to the wanted &struct chfs_ltree_entry
    561   1.1     ahoka  * if the logical eraseblock is in the lock tree, so it is locked, NULL
    562   1.1     ahoka  * otherwise.
    563   1.1     ahoka  * @ebh->ltree_lock has to be locked!
    564   1.1     ahoka  */
    565   1.1     ahoka static struct chfs_ltree_entry *
    566   1.1     ahoka ltree_lookup(struct chfs_ebh *ebh, int lnr)
    567   1.1     ahoka {
    568   1.1     ahoka 	struct chfs_ltree_entry le, *result;
    569   1.1     ahoka 	le.lnr = lnr;
    570   1.1     ahoka 	result = RB_FIND(ltree_rbtree, &ebh->ltree, &le);
    571   1.1     ahoka 	return result;
    572   1.1     ahoka }
    573   1.1     ahoka 
    574   1.1     ahoka /**
    575   1.1     ahoka  * ltree_add_entry - add an entry to the lock tree
    576   1.1     ahoka  * @ebh: chfs eraseblock handler
    577   1.1     ahoka  * @lnr: identifier of the logical eraseblock
    578   1.1     ahoka  *
    579   1.1     ahoka  * This function adds a new logical eraseblock entry identified with @lnr to the
    580   1.1     ahoka  * lock tree. If the entry is already in the tree, it increases the user
    581   1.1     ahoka  * counter.
    582   1.1     ahoka  * Returns NULL if can not allocate memory for lock tree entry, or a pointer
    583   1.1     ahoka  * to the inserted entry otherwise.
    584   1.1     ahoka  */
    585   1.1     ahoka static struct chfs_ltree_entry *
    586   1.1     ahoka ltree_add_entry(struct chfs_ebh *ebh, int lnr)
    587   1.1     ahoka {
    588   1.1     ahoka 	struct chfs_ltree_entry *le, *result;
    589   1.1     ahoka 
    590   1.1     ahoka 	le = kmem_alloc(sizeof(struct chfs_ltree_entry), KM_SLEEP);
    591   1.1     ahoka 
    592   1.1     ahoka 	le->lnr = lnr;
    593   1.1     ahoka 	le->users = 1;
    594   1.1     ahoka 	rw_init(&le->mutex);
    595   1.1     ahoka 
    596   1.1     ahoka 	//dbg_ebh("enter ltree lock\n");
    597   1.1     ahoka 	mutex_enter(&ebh->ltree_lock);
    598   1.1     ahoka 	//dbg_ebh("insert\n");
    599   1.1     ahoka 	result = RB_INSERT(ltree_rbtree, &ebh->ltree, le);
    600   1.1     ahoka 	//dbg_ebh("inserted\n");
    601   1.1     ahoka 	if (result) {
    602   1.1     ahoka 		//The entry is already in the tree
    603   1.1     ahoka 		result->users++;
    604   1.1     ahoka 		kmem_free(le, sizeof(struct chfs_ltree_entry));
    605   1.1     ahoka 	}
    606   1.1     ahoka 	else {
    607   1.1     ahoka 		result = le;
    608   1.1     ahoka 	}
    609   1.1     ahoka 	mutex_exit(&ebh->ltree_lock);
    610   1.1     ahoka 
    611   1.1     ahoka 	return result;
    612   1.1     ahoka }
    613   1.1     ahoka 
    614   1.1     ahoka /**
    615   1.1     ahoka  * leb_read_lock - lock a logical eraseblock for read
    616   1.1     ahoka  * @ebh: chfs eraseblock handler
    617   1.1     ahoka  * @lnr: identifier of the logical eraseblock
    618   1.1     ahoka  *
    619   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    620   1.1     ahoka  */
    621   1.1     ahoka static int
    622   1.1     ahoka leb_read_lock(struct chfs_ebh *ebh, int lnr)
    623   1.1     ahoka {
    624   1.1     ahoka 	struct chfs_ltree_entry *le;
    625   1.1     ahoka 
    626   1.1     ahoka 	le = ltree_add_entry(ebh, lnr);
    627   1.1     ahoka 	if (!le)
    628   1.1     ahoka 		return ENOMEM;
    629   1.1     ahoka 
    630   1.1     ahoka 	rw_enter(&le->mutex, RW_READER);
    631   1.1     ahoka 	return 0;
    632   1.1     ahoka }
    633   1.1     ahoka 
    634   1.1     ahoka /**
    635   1.1     ahoka  * leb_read_unlock - unlock a logical eraseblock from read
    636   1.1     ahoka  * @ebh: chfs eraseblock handler
    637   1.1     ahoka  * @lnr: identifier of the logical eraseblock
    638   1.1     ahoka  *
    639   1.1     ahoka  * This function unlocks a logical eraseblock from read and delete it from the
    640   1.1     ahoka  * lock tree is there are no more users of it.
    641   1.1     ahoka  */
    642   1.1     ahoka static void
    643   1.1     ahoka leb_read_unlock(struct chfs_ebh *ebh, int lnr)
    644   1.1     ahoka {
    645   1.1     ahoka 	struct chfs_ltree_entry *le;
    646   1.1     ahoka 
    647   1.1     ahoka 	mutex_enter(&ebh->ltree_lock);
    648   1.1     ahoka 	//dbg_ebh("LOCK: ebh->ltree_lock spin locked in leb_read_unlock()\n");
    649   1.1     ahoka 	le = ltree_lookup(ebh, lnr);
    650   1.1     ahoka 	if (!le)
    651   1.1     ahoka 		goto out;
    652   1.1     ahoka 
    653   1.1     ahoka 	le->users -= 1;
    654   1.1     ahoka 	KASSERT(le->users >= 0);
    655   1.1     ahoka 	rw_exit(&le->mutex);
    656   1.1     ahoka 	if (le->users == 0) {
    657   1.1     ahoka 		le = RB_REMOVE(ltree_rbtree, &ebh->ltree, le);
    658   1.1     ahoka 		if (le) {
    659   1.1     ahoka 			rw_destroy(&le->mutex);
    660   1.1     ahoka 
    661   1.1     ahoka 			kmem_free(le, sizeof(struct chfs_ltree_entry));
    662   1.1     ahoka 		}
    663   1.1     ahoka 	}
    664   1.1     ahoka 
    665   1.1     ahoka out:
    666   1.1     ahoka 	mutex_exit(&ebh->ltree_lock);
    667   1.1     ahoka 	//dbg_ebh("UNLOCK: ebh->ltree_lock spin unlocked in leb_read_unlock()\n");
    668   1.1     ahoka }
    669   1.1     ahoka 
    670   1.1     ahoka /**
    671   1.1     ahoka  * leb_write_lock - lock a logical eraseblock for write
    672   1.1     ahoka  * @ebh: chfs eraseblock handler
    673   1.1     ahoka  * @lnr: identifier of the logical eraseblock
    674   1.1     ahoka  *
    675   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    676   1.1     ahoka  */
    677   1.1     ahoka static int
    678   1.1     ahoka leb_write_lock(struct chfs_ebh *ebh, int lnr)
    679   1.1     ahoka {
    680   1.1     ahoka 	struct chfs_ltree_entry *le;
    681   1.1     ahoka 
    682   1.1     ahoka 	le = ltree_add_entry(ebh, lnr);
    683   1.1     ahoka 	if (!le)
    684   1.1     ahoka 		return ENOMEM;
    685   1.1     ahoka 
    686   1.1     ahoka 	rw_enter(&le->mutex, RW_WRITER);
    687   1.1     ahoka 	return 0;
    688   1.1     ahoka }
    689   1.1     ahoka 
    690   1.1     ahoka /**
    691   1.1     ahoka  * leb_write_unlock - unlock a logical eraseblock from write
    692   1.1     ahoka  * @ebh: chfs eraseblock handler
    693   1.1     ahoka  * @lnr: identifier of the logical eraseblock
    694   1.1     ahoka  *
    695   1.1     ahoka  * This function unlocks a logical eraseblock from write and delete it from the
    696   1.1     ahoka  * lock tree is there are no more users of it.
    697   1.1     ahoka  */
    698   1.1     ahoka static void
    699   1.1     ahoka leb_write_unlock(struct chfs_ebh *ebh, int lnr)
    700   1.1     ahoka {
    701   1.1     ahoka 	struct chfs_ltree_entry *le;
    702   1.1     ahoka 
    703   1.1     ahoka 	mutex_enter(&ebh->ltree_lock);
    704   1.1     ahoka 	//dbg_ebh("LOCK: ebh->ltree_lock spin locked in leb_write_unlock()\n");
    705   1.1     ahoka 	le = ltree_lookup(ebh, lnr);
    706   1.1     ahoka 	if (!le)
    707   1.1     ahoka 		goto out;
    708   1.1     ahoka 
    709   1.1     ahoka 	le->users -= 1;
    710   1.1     ahoka 	KASSERT(le->users >= 0);
    711   1.1     ahoka 	rw_exit(&le->mutex);
    712   1.1     ahoka 	if (le->users == 0) {
    713   1.1     ahoka 		RB_REMOVE(ltree_rbtree, &ebh->ltree, le);
    714   1.1     ahoka 
    715   1.1     ahoka 		rw_destroy(&le->mutex);
    716   1.1     ahoka 
    717   1.1     ahoka 		kmem_free(le, sizeof(struct chfs_ltree_entry));
    718   1.1     ahoka 	}
    719   1.1     ahoka 
    720   1.1     ahoka out:
    721   1.1     ahoka 	mutex_exit(&ebh->ltree_lock);
    722   1.1     ahoka 	//dbg_ebh("UNLOCK: ebh->ltree_lock spin unlocked in leb_write_unlock()\n");
    723   1.1     ahoka }
    724   1.1     ahoka 
    725   1.1     ahoka /*****************************************************************************/
    726   1.1     ahoka /* End of Lock Tree							     */
    727   1.1     ahoka /*****************************************************************************/
    728   1.1     ahoka 
    729   1.1     ahoka /*****************************************************************************/
    730   1.1     ahoka /* Erase related operations						     */
    731   1.1     ahoka /*****************************************************************************/
    732   1.1     ahoka 
    733   1.1     ahoka /**
    734   1.1     ahoka  * If the first argument is smaller than the second, the function
    735   1.1     ahoka  * returns a value smaller than zero. If they are equal, the function re-
    736   1.1     ahoka  * turns zero. Otherwise, it should return a value greater than zero.
    737   1.1     ahoka  */
    738   1.1     ahoka int
    739   1.1     ahoka peb_in_use_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2)
    740   1.1     ahoka {
    741   1.1     ahoka 	return (peb1->pebnr - peb2->pebnr);
    742   1.1     ahoka }
    743   1.1     ahoka 
    744   1.1     ahoka int
    745   1.1     ahoka peb_free_cmp(struct chfs_peb *peb1, struct chfs_peb *peb2)
    746   1.1     ahoka {
    747   1.1     ahoka 	int comp;
    748   1.1     ahoka 
    749   1.1     ahoka 	comp = peb1->erase_cnt - peb2->erase_cnt;
    750   1.1     ahoka 	if (0 == comp)
    751   1.1     ahoka 		comp = peb1->pebnr - peb2->pebnr;
    752   1.1     ahoka 
    753   1.1     ahoka 	return comp;
    754   1.1     ahoka }
    755   1.1     ahoka 
    756   1.1     ahoka /* Generate functions for in use PEB's red-black tree */
    757   1.1     ahoka RB_PROTOTYPE(peb_in_use_rbtree, chfs_peb, u.rb, peb_in_use_cmp);
    758   1.1     ahoka RB_GENERATE(peb_in_use_rbtree, chfs_peb, u.rb, peb_in_use_cmp);
    759   1.1     ahoka RB_PROTOTYPE(peb_free_rbtree, chfs_peb, u.rb, peb_free_cmp);
    760   1.1     ahoka RB_GENERATE(peb_free_rbtree, chfs_peb, u.rb, peb_free_cmp);
    761   1.1     ahoka 
    762   1.1     ahoka /**
    763   1.1     ahoka  * add_peb_to_erase_queue: adds a PEB to to_erase/fully_erased queue
    764   1.1     ahoka  * @ebh - chfs eraseblock handler
    765   1.1     ahoka  * @pebnr - physical eraseblock's number
    766   1.1     ahoka  * @ec - erase counter of PEB
    767   1.1     ahoka  * @queue: the queue to add to
    768   1.1     ahoka  *
    769   1.1     ahoka  * This function adds a PEB to the erase queue specified by @queue.
    770   1.1     ahoka  * The @ebh->erase_lock must be locked before using this.
    771   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    772   1.1     ahoka  */
    773   1.1     ahoka int
    774   1.1     ahoka add_peb_to_erase_queue(struct chfs_ebh *ebh, int pebnr, int ec,
    775   1.1     ahoka     struct peb_queue *queue)
    776   1.1     ahoka {
    777   1.1     ahoka 	struct chfs_peb *peb;
    778   1.1     ahoka 
    779   1.1     ahoka 	peb = kmem_alloc(sizeof(struct chfs_peb), KM_SLEEP);
    780   1.1     ahoka 
    781   1.1     ahoka 	peb->erase_cnt = ec;
    782   1.1     ahoka 	peb->pebnr = pebnr;
    783   1.1     ahoka 
    784   1.1     ahoka 	TAILQ_INSERT_TAIL(queue, peb, u.queue);
    785   1.1     ahoka 
    786   1.1     ahoka 	return 0;
    787   1.1     ahoka 
    788   1.1     ahoka }
    789   1.1     ahoka //TODO
    790   1.1     ahoka /**
    791   1.1     ahoka  * find_peb_in_use - looks up a PEB in the RB-tree of used blocks
    792   1.1     ahoka  * @ebh - chfs eraseblock handler
    793   1.1     ahoka  *
    794   1.1     ahoka  * This function returns a pointer to the PEB found in the tree,
    795   1.1     ahoka  * NULL otherwise.
    796   1.1     ahoka  * The @ebh->erase_lock must be locked before using this.
    797   1.1     ahoka  */
    798   1.1     ahoka struct chfs_peb *
    799   1.1     ahoka find_peb_in_use(struct chfs_ebh *ebh, int pebnr)
    800   1.1     ahoka {
    801   1.1     ahoka 	struct chfs_peb peb, *result;
    802   1.1     ahoka 	peb.pebnr = pebnr;
    803   1.1     ahoka 	result = RB_FIND(peb_in_use_rbtree, &ebh->in_use, &peb);
    804   1.1     ahoka 	return result;
    805   1.1     ahoka }
    806   1.1     ahoka 
    807   1.1     ahoka /**
    808   1.1     ahoka  * add_peb_to_free - adds a PEB to the RB-tree of free PEBs
    809   1.1     ahoka  * @ebh - chfs eraseblock handler
    810   1.1     ahoka  * @pebnr - physical eraseblock's number
    811   1.1     ahoka  * @ec - erase counter of PEB
    812   1.1     ahoka  *
    813   1.1     ahoka  *
    814   1.1     ahoka  * This function adds a physical eraseblock to the RB-tree of free PEBs
    815   1.1     ahoka  * stored in the @ebh. The key is the erase counter and pebnr.
    816   1.1     ahoka  * The @ebh->erase_lock must be locked before using this.
    817   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    818   1.1     ahoka  */
    819   1.1     ahoka int
    820   1.1     ahoka add_peb_to_free(struct chfs_ebh *ebh, int pebnr, int ec)
    821   1.1     ahoka {
    822   1.1     ahoka 	struct chfs_peb *peb, *result;
    823   1.1     ahoka 
    824   1.1     ahoka 	peb = kmem_alloc(sizeof(struct chfs_peb), KM_SLEEP);
    825   1.1     ahoka 
    826   1.1     ahoka 	peb->erase_cnt = ec;
    827   1.1     ahoka 	peb->pebnr = pebnr;
    828   1.1     ahoka 	result = RB_INSERT(peb_free_rbtree, &ebh->free, peb);
    829   1.4        he 	if (result) {
    830   1.4        he 		kmem_free(peb, sizeof(struct chfs_peb));
    831   1.1     ahoka 		return 1;
    832   1.4        he 	}
    833   1.1     ahoka 
    834   1.1     ahoka 	return 0;
    835   1.1     ahoka }
    836   1.1     ahoka 
    837   1.1     ahoka /**
    838   1.1     ahoka  * add_peb_to_in_use - adds a PEB to the RB-tree of used PEBs
    839   1.1     ahoka  * @ebh - chfs eraseblock handler
    840   1.1     ahoka  * @pebnr - physical eraseblock's number
    841   1.1     ahoka  * @ec - erase counter of PEB
    842   1.1     ahoka  *
    843   1.1     ahoka  *
    844   1.1     ahoka  * This function adds a physical eraseblock to the RB-tree of used PEBs
    845   1.1     ahoka  * stored in the @ebh. The key is pebnr.
    846   1.1     ahoka  * The @ebh->erase_lock must be locked before using this.
    847   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
    848   1.1     ahoka  */
    849   1.1     ahoka int
    850   1.1     ahoka add_peb_to_in_use(struct chfs_ebh *ebh, int pebnr, int ec)
    851   1.1     ahoka {
    852   1.1     ahoka 	struct chfs_peb *peb, *result;
    853   1.1     ahoka 
    854   1.1     ahoka 	peb = kmem_alloc(sizeof(struct chfs_peb), KM_SLEEP);
    855   1.1     ahoka 
    856   1.1     ahoka 	peb->erase_cnt = ec;
    857   1.1     ahoka 	peb->pebnr = pebnr;
    858   1.1     ahoka 	result = RB_INSERT(peb_in_use_rbtree, &ebh->in_use, peb);
    859   1.4        he 	if (result) {
    860   1.4        he 		kmem_free(peb, sizeof(struct chfs_peb));
    861   1.1     ahoka 		return 1;
    862   1.4        he 	}
    863   1.1     ahoka 
    864   1.1     ahoka 	return 0;
    865   1.1     ahoka }
    866   1.1     ahoka 
    867   1.1     ahoka /**
    868   1.1     ahoka  * erase_callback - callback function for flash erase
    869   1.1     ahoka  * @ei: erase information
    870   1.1     ahoka  */
    871   1.1     ahoka void
    872   1.1     ahoka erase_callback(struct flash_erase_instruction *ei)
    873   1.1     ahoka {
    874   1.1     ahoka 	int err;
    875   1.1     ahoka 	struct chfs_erase_info_priv *priv = (void *) ei->ei_priv;
    876   1.1     ahoka 	//dbg_ebh("ERASE_CALLBACK() CALLED\n");
    877   1.1     ahoka 	struct chfs_ebh *ebh = priv->ebh;
    878   1.1     ahoka 	struct chfs_peb *peb = priv->peb;
    879   1.1     ahoka 
    880   1.1     ahoka 	peb->erase_cnt += 1;
    881   1.1     ahoka 
    882   1.1     ahoka 	if (ei->ei_state == FLASH_ERASE_DONE) {
    883   1.1     ahoka 
    884   1.1     ahoka 		/* Write out erase counter */
    885   1.1     ahoka 		err = ebh->ops->mark_eb_hdr_free(ebh,
    886   1.1     ahoka 		    peb->pebnr, peb->erase_cnt);
    887   1.1     ahoka 		if (err) {
    888   1.1     ahoka 			/* cannot mark PEB as free,so erase it again */
    889   1.1     ahoka 			chfs_err(
    890   1.1     ahoka 				"cannot mark eraseblock as free, PEB: %d\n",
    891   1.1     ahoka 				peb->pebnr);
    892   1.1     ahoka 			mutex_enter(&ebh->erase_lock);
    893   1.1     ahoka 			/*dbg_ebh("LOCK: ebh->erase_lock spin locked in erase_callback() "
    894   1.1     ahoka 			  "after mark ebhdr free\n");*/
    895   1.1     ahoka 			add_peb_to_erase_queue(ebh, peb->pebnr, peb->erase_cnt,
    896   1.1     ahoka 			    &ebh->to_erase);
    897   1.1     ahoka 			mutex_exit(&ebh->erase_lock);
    898   1.1     ahoka 			/*dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_callback() "
    899   1.1     ahoka 			  "after mark ebhdr free\n");*/
    900   1.1     ahoka 			kmem_free(peb, sizeof(struct chfs_peb));
    901   1.1     ahoka 			return;
    902   1.1     ahoka 		}
    903   1.1     ahoka 
    904   1.1     ahoka 		mutex_enter(&ebh->erase_lock);
    905   1.1     ahoka 		/*dbg_ebh("LOCK: ebh->erase_lock spin locked in erase_callback()\n");*/
    906   1.1     ahoka 		err = add_peb_to_free(ebh, peb->pebnr, peb->erase_cnt);
    907   1.1     ahoka 		mutex_exit(&ebh->erase_lock);
    908   1.1     ahoka 		/*dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_callback()\n");*/
    909   1.1     ahoka 		kmem_free(peb, sizeof(struct chfs_peb));
    910   1.1     ahoka 	} else {
    911   1.1     ahoka 		/*
    912   1.1     ahoka 		 * Erase is finished, but there was a problem,
    913   1.1     ahoka 		 * so erase PEB again
    914   1.1     ahoka 		 */
    915   1.1     ahoka 		chfs_err("erase failed, state is: 0x%x\n", ei->ei_state);
    916   1.1     ahoka 		add_peb_to_erase_queue(ebh, peb->pebnr, peb->erase_cnt, &ebh->to_erase);
    917   1.1     ahoka 		kmem_free(peb, sizeof(struct chfs_peb));
    918   1.1     ahoka 	}
    919   1.1     ahoka }
    920   1.1     ahoka 
    921   1.1     ahoka /**
    922   1.1     ahoka  * free_peb: free a PEB
    923   1.1     ahoka  * @ebh: chfs eraseblock handler
    924   1.1     ahoka  *
    925   1.1     ahoka  * This function erases the first physical eraseblock from one of the erase
    926   1.1     ahoka  * lists and adds to the RB-tree of free PEBs.
    927  1.10    andvar  * Returns zero in case of success, error code in case of fail.
    928   1.1     ahoka  */
    929   1.1     ahoka int
    930   1.1     ahoka free_peb(struct chfs_ebh *ebh)
    931   1.1     ahoka {
    932   1.1     ahoka 	int err, retries = 0;
    933   1.1     ahoka 	off_t ofs;
    934   1.1     ahoka 	struct chfs_peb *peb = NULL;
    935   1.1     ahoka 	struct flash_erase_instruction *ei;
    936   1.1     ahoka 
    937   1.1     ahoka 	KASSERT(mutex_owned(&ebh->erase_lock));
    938   1.1     ahoka 
    939   1.1     ahoka 	if (!TAILQ_EMPTY(&ebh->fully_erased)) {
    940   1.1     ahoka 		//dbg_ebh("[FREE PEB] got a fully erased block\n");
    941   1.1     ahoka 		peb = TAILQ_FIRST(&ebh->fully_erased);
    942   1.1     ahoka 		TAILQ_REMOVE(&ebh->fully_erased, peb, u.queue);
    943   1.1     ahoka 		err = ebh->ops->mark_eb_hdr_free(ebh,
    944   1.1     ahoka 		    peb->pebnr, peb->erase_cnt);
    945   1.1     ahoka 		if (err) {
    946   1.1     ahoka 			goto out_free;
    947   1.1     ahoka 		}
    948   1.1     ahoka 		err = add_peb_to_free(ebh, peb->pebnr, peb->erase_cnt);
    949   1.1     ahoka 		goto out_free;
    950   1.1     ahoka 	}
    951   1.1     ahoka 	/* Erase PEB */
    952  1.11    andvar 	//dbg_ebh("[FREE PEB] erasing a block\n");
    953   1.1     ahoka 	peb = TAILQ_FIRST(&ebh->to_erase);
    954   1.1     ahoka 	TAILQ_REMOVE(&ebh->to_erase, peb, u.queue);
    955   1.1     ahoka 	mutex_exit(&ebh->erase_lock);
    956   1.1     ahoka 	//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in free_peb()\n");
    957   1.1     ahoka 	ofs = peb->pebnr * ebh->flash_if->erasesize;
    958   1.1     ahoka 
    959   1.1     ahoka 	/* XXX where do we free this? */
    960   1.1     ahoka 	ei = kmem_alloc(sizeof(struct flash_erase_instruction)
    961   1.1     ahoka 	    + sizeof(struct chfs_erase_info_priv), KM_SLEEP);
    962   1.1     ahoka retry:
    963   1.1     ahoka 	memset(ei, 0, sizeof(*ei));
    964   1.1     ahoka 
    965   1.1     ahoka //	ei->ei_if = ebh->flash_if;
    966   1.1     ahoka 	ei->ei_addr = ofs;
    967   1.1     ahoka 	ei->ei_len = ebh->flash_if->erasesize;
    968   1.1     ahoka 	ei->ei_callback = erase_callback;
    969   1.1     ahoka 	ei->ei_priv = (unsigned long) (&ei[1]);
    970   1.1     ahoka 
    971   1.1     ahoka 	((struct chfs_erase_info_priv *) ei->ei_priv)->ebh = ebh;
    972   1.1     ahoka 	((struct chfs_erase_info_priv *) ei->ei_priv)->peb = peb;
    973   1.1     ahoka 
    974   1.1     ahoka 	err = flash_erase(ebh->flash_dev, ei);
    975   1.1     ahoka 	dbg_ebh("erased peb: %d\n", peb->pebnr);
    976   1.1     ahoka 
    977   1.1     ahoka 	/* einval would mean we did something wrong */
    978   1.1     ahoka 	KASSERT(err != EINVAL);
    979   1.1     ahoka 
    980   1.1     ahoka 	if (err) {
    981   1.1     ahoka 		dbg_ebh("errno: %d, ei->ei_state: %d\n", err, ei->ei_state);
    982   1.1     ahoka 		if (CHFS_MAX_GET_PEB_RETRIES < ++retries &&
    983   1.1     ahoka 		    ei->ei_state == FLASH_ERASE_FAILED) {
    984   1.1     ahoka 			/* The block went bad mark it */
    985   1.1     ahoka 			dbg_ebh("ebh markbad! 0x%jx\n", (uintmax_t )ofs);
    986   1.1     ahoka 			err = flash_block_markbad(ebh->flash_dev, ofs);
    987   1.1     ahoka 			if (!err) {
    988   1.1     ahoka 				ebh->peb_nr--;
    989   1.1     ahoka 			}
    990   1.1     ahoka 
    991   1.1     ahoka 			goto out;
    992   1.1     ahoka 		}
    993   1.1     ahoka 		chfs_err("can not erase PEB: %d, try again\n", peb->pebnr);
    994   1.1     ahoka 		goto retry;
    995   1.1     ahoka 	}
    996   1.1     ahoka 
    997   1.1     ahoka out:
    998   1.1     ahoka 	/* lock the erase_lock, because it was locked
    999   1.1     ahoka 	 * when the function was called */
   1000   1.1     ahoka 	mutex_enter(&ebh->erase_lock);
   1001   1.1     ahoka 	return err;
   1002   1.1     ahoka 
   1003   1.1     ahoka out_free:
   1004   1.1     ahoka 	kmem_free(peb, sizeof(struct chfs_peb));
   1005   1.1     ahoka 	return err;
   1006   1.1     ahoka }
   1007   1.1     ahoka 
   1008   1.1     ahoka /**
   1009   1.1     ahoka  * release_peb - schedule an erase for the PEB
   1010   1.1     ahoka  * @ebh: chfs eraseblock handler
   1011   1.1     ahoka  * @pebnr: physical eraseblock number
   1012   1.1     ahoka  *
   1013   1.1     ahoka  * This function get the peb identified by @pebnr from the in_use RB-tree of
   1014   1.1     ahoka  * @ebh, removes it and schedule an erase for it.
   1015   1.1     ahoka  *
   1016   1.1     ahoka  * Returns zero on success, error code in case of fail.
   1017   1.1     ahoka  */
   1018   1.1     ahoka int
   1019   1.1     ahoka release_peb(struct chfs_ebh *ebh, int pebnr)
   1020   1.1     ahoka {
   1021   1.1     ahoka 	int err = 0;
   1022   1.1     ahoka 	struct chfs_peb *peb;
   1023   1.1     ahoka 
   1024   1.1     ahoka 	mutex_enter(&ebh->erase_lock);
   1025   1.1     ahoka 
   1026   1.1     ahoka 	//dbg_ebh("LOCK: ebh->erase_lock spin locked in release_peb()\n");
   1027   1.1     ahoka 	peb = find_peb_in_use(ebh, pebnr);
   1028   1.1     ahoka 	if (!peb) {
   1029   1.1     ahoka 		chfs_err("LEB is mapped, but is not in the 'in_use' "
   1030   1.1     ahoka 		    "tree of ebh\n");
   1031   1.1     ahoka 		goto out_unlock;
   1032   1.1     ahoka 	}
   1033   1.1     ahoka 	err = add_peb_to_erase_queue(ebh, peb->pebnr, peb->erase_cnt,
   1034   1.1     ahoka 	    &ebh->to_erase);
   1035   1.1     ahoka 
   1036   1.1     ahoka 	if (err)
   1037   1.1     ahoka 		goto out_unlock;
   1038   1.1     ahoka 
   1039   1.1     ahoka 	RB_REMOVE(peb_in_use_rbtree, &ebh->in_use, peb);
   1040   1.1     ahoka out_unlock:
   1041   1.1     ahoka 	mutex_exit(&ebh->erase_lock);
   1042   1.1     ahoka 	//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in release_peb()"
   1043   1.1     ahoka 	//		" at out_unlock\n");
   1044   1.1     ahoka 	return err;
   1045   1.1     ahoka }
   1046   1.1     ahoka 
   1047   1.1     ahoka /**
   1048   1.1     ahoka  * erase_thread - background thread for erasing PEBs
   1049   1.1     ahoka  * @data: pointer to the eraseblock handler
   1050   1.1     ahoka  */
   1051   1.1     ahoka /*void
   1052   1.1     ahoka   erase_thread(void *data)
   1053   1.1     ahoka   {
   1054   1.1     ahoka   struct chfs_ebh *ebh = data;
   1055   1.1     ahoka 
   1056   1.1     ahoka   dbg_ebh("erase thread started\n");
   1057   1.1     ahoka   while (ebh->bg_erase.eth_running) {
   1058   1.1     ahoka   int err;
   1059   1.1     ahoka 
   1060   1.1     ahoka   mutex_enter(&ebh->erase_lock);
   1061   1.1     ahoka   dbg_ebh("LOCK: ebh->erase_lock spin locked in erase_thread()\n");
   1062   1.1     ahoka   if (TAILQ_EMPTY(&ebh->to_erase) && TAILQ_EMPTY(&ebh->fully_erased)) {
   1063   1.1     ahoka   dbg_ebh("thread has nothing to do\n");
   1064   1.1     ahoka   mutex_exit(&ebh->erase_lock);
   1065   1.1     ahoka   mutex_enter(&ebh->bg_erase.eth_thread_mtx);
   1066   1.1     ahoka   cv_timedwait_sig(&ebh->bg_erase.eth_wakeup,
   1067   1.1     ahoka   &ebh->bg_erase.eth_thread_mtx, mstohz(100));
   1068   1.1     ahoka   mutex_exit(&ebh->bg_erase.eth_thread_mtx);
   1069   1.1     ahoka 
   1070   1.1     ahoka   dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_thread()\n");
   1071   1.1     ahoka   continue;
   1072   1.1     ahoka   }
   1073   1.1     ahoka   mutex_exit(&ebh->erase_lock);
   1074   1.1     ahoka   dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in erase_thread()\n");
   1075   1.1     ahoka 
   1076   1.1     ahoka   err = free_peb(ebh);
   1077   1.1     ahoka   if (err)
   1078   1.1     ahoka   chfs_err("freeing PEB failed in the background thread: %d\n", err);
   1079   1.1     ahoka 
   1080   1.1     ahoka   }
   1081   1.1     ahoka   dbg_ebh("erase thread stopped\n");
   1082   1.1     ahoka   kthread_exit(0);
   1083   1.1     ahoka   }*/
   1084   1.1     ahoka 
   1085   1.1     ahoka /**
   1086   1.1     ahoka  * erase_thread - background thread for erasing PEBs
   1087   1.1     ahoka  * @data: pointer to the eraseblock handler
   1088   1.1     ahoka  */
   1089   1.1     ahoka void
   1090   1.1     ahoka erase_thread(void *data) {
   1091   1.1     ahoka 	dbg_ebh("[EBH THREAD] erase thread started\n");
   1092   1.1     ahoka 
   1093   1.1     ahoka 	struct chfs_ebh *ebh = data;
   1094   1.1     ahoka 	int err;
   1095   1.1     ahoka 
   1096   1.1     ahoka 	mutex_enter(&ebh->erase_lock);
   1097   1.1     ahoka 	while (ebh->bg_erase.eth_running) {
   1098   1.1     ahoka 		if (TAILQ_EMPTY(&ebh->to_erase) &&
   1099   1.1     ahoka 		    TAILQ_EMPTY(&ebh->fully_erased)) {
   1100   1.1     ahoka 			cv_timedwait_sig(&ebh->bg_erase.eth_wakeup,
   1101   1.1     ahoka 			    &ebh->erase_lock, mstohz(100));
   1102   1.1     ahoka 		} else {
   1103   1.1     ahoka 			/* XXX exiting this mutex is a bit odd here as
   1104   1.1     ahoka 			 * free_peb instantly reenters it...
   1105   1.1     ahoka 			 */
   1106   1.1     ahoka 			err = free_peb(ebh);
   1107   1.1     ahoka 			mutex_exit(&ebh->erase_lock);
   1108   1.1     ahoka 			if (err) {
   1109   1.1     ahoka 				chfs_err("freeing PEB failed in the"
   1110   1.1     ahoka 				    " background thread: %d\n", err);
   1111   1.1     ahoka 			}
   1112   1.1     ahoka 			mutex_enter(&ebh->erase_lock);
   1113   1.1     ahoka 		}
   1114   1.1     ahoka 	}
   1115   1.1     ahoka 	mutex_exit(&ebh->erase_lock);
   1116   1.1     ahoka 
   1117   1.1     ahoka 	dbg_ebh("[EBH THREAD] erase thread stopped\n");
   1118   1.1     ahoka 	kthread_exit(0);
   1119   1.1     ahoka }
   1120   1.1     ahoka 
   1121   1.1     ahoka /**
   1122   1.1     ahoka  * erase_thread_start - init and start erase thread
   1123   1.1     ahoka  * @ebh: eraseblock handler
   1124   1.1     ahoka  */
   1125   1.1     ahoka static void
   1126   1.1     ahoka erase_thread_start(struct chfs_ebh *ebh)
   1127   1.1     ahoka {
   1128   1.1     ahoka 	cv_init(&ebh->bg_erase.eth_wakeup, "ebheracv");
   1129   1.1     ahoka 
   1130   1.1     ahoka 	ebh->bg_erase.eth_running = true;
   1131   1.1     ahoka 	kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
   1132   1.1     ahoka 	    erase_thread, ebh, &ebh->bg_erase.eth_thread, "ebherase");
   1133   1.1     ahoka }
   1134   1.1     ahoka 
   1135   1.1     ahoka /**
   1136   1.1     ahoka  * erase_thread_stop - stop background erase thread
   1137   1.1     ahoka  * @ebh: eraseblock handler
   1138   1.1     ahoka  */
   1139   1.1     ahoka static void
   1140   1.1     ahoka erase_thread_stop(struct chfs_ebh *ebh)
   1141   1.1     ahoka {
   1142   1.1     ahoka 	ebh->bg_erase.eth_running = false;
   1143   1.1     ahoka 	cv_signal(&ebh->bg_erase.eth_wakeup);
   1144   1.1     ahoka 	dbg_ebh("[EBH THREAD STOP] signaled\n");
   1145   1.1     ahoka 
   1146   1.1     ahoka 	kthread_join(ebh->bg_erase.eth_thread);
   1147   1.1     ahoka #ifdef BROKEN_KTH_JOIN
   1148   1.1     ahoka 	kpause("chfsebhjointh", false, mstohz(1000), NULL);
   1149   1.1     ahoka #endif
   1150   1.1     ahoka 
   1151   1.1     ahoka 	cv_destroy(&ebh->bg_erase.eth_wakeup);
   1152   1.1     ahoka }
   1153   1.1     ahoka 
   1154   1.1     ahoka /*****************************************************************************/
   1155   1.1     ahoka /* End of Erase related operations					     */
   1156   1.1     ahoka /*****************************************************************************/
   1157   1.1     ahoka 
   1158   1.1     ahoka /*****************************************************************************/
   1159   1.1     ahoka /* Scan related operations						     */
   1160   1.1     ahoka /*****************************************************************************/
   1161   1.1     ahoka int
   1162   1.1     ahoka scan_leb_used_cmp(struct chfs_scan_leb *sleb1, struct chfs_scan_leb *sleb2)
   1163   1.1     ahoka {
   1164   1.1     ahoka 	return (sleb1->lnr - sleb2->lnr);
   1165   1.1     ahoka }
   1166   1.1     ahoka 
   1167   1.1     ahoka RB_PROTOTYPE(scan_leb_used_rbtree, chfs_scan_leb, u.rb, scan_leb_used_cmp);
   1168   1.1     ahoka RB_GENERATE(scan_leb_used_rbtree, chfs_scan_leb, u.rb, scan_leb_used_cmp);
   1169   1.1     ahoka 
   1170   1.1     ahoka /**
   1171   1.1     ahoka  * scan_add_to_queue - adds a physical eraseblock to one of the
   1172   1.1     ahoka  *                     eraseblock queue
   1173   1.1     ahoka  * @si: chfs scanning information
   1174   1.1     ahoka  * @pebnr: physical eraseblock number
   1175   1.1     ahoka  * @erase_cnt: erase counter of the physical eraseblock
   1176   1.1     ahoka  * @list: the list to add to
   1177   1.1     ahoka  *
   1178   1.1     ahoka  * This function adds a physical eraseblock to one of the lists in the scanning
   1179   1.1     ahoka  * information.
   1180   1.1     ahoka  * Returns zero in case of success, negative error code in case of fail.
   1181   1.1     ahoka  */
   1182   1.1     ahoka static int
   1183   1.1     ahoka scan_add_to_queue(struct chfs_scan_info *si, int pebnr, int erase_cnt,
   1184   1.1     ahoka     struct scan_leb_queue *queue)
   1185   1.1     ahoka {
   1186   1.1     ahoka 	struct chfs_scan_leb *sleb;
   1187   1.1     ahoka 
   1188   1.1     ahoka 	sleb = kmem_alloc(sizeof(struct chfs_scan_leb), KM_SLEEP);
   1189   1.1     ahoka 
   1190   1.1     ahoka 	sleb->pebnr = pebnr;
   1191   1.1     ahoka 	sleb->erase_cnt = erase_cnt;
   1192   1.1     ahoka 	TAILQ_INSERT_TAIL(queue, sleb, u.queue);
   1193   1.1     ahoka 	return 0;
   1194   1.1     ahoka }
   1195   1.1     ahoka 
   1196   1.1     ahoka /*
   1197   1.1     ahoka  * nor_scan_add_to_used - add a physical eraseblock to the
   1198   1.1     ahoka  *                        used tree of scan info
   1199   1.1     ahoka  * @ebh: chfs eraseblock handler
   1200   1.1     ahoka  * @si: chfs scanning information
   1201   1.1     ahoka  * @ebhdr: eraseblock header
   1202   1.1     ahoka  * @pebnr: physical eraseblock number
   1203   1.1     ahoka  * @leb_status: the status of the PEB's eraseblock header
   1204   1.1     ahoka  *
   1205   1.1     ahoka  * This function adds a PEB to the used tree of the scanning information.
   1206   1.1     ahoka  * It handles the situations if there are more physical eraseblock referencing
   1207   1.1     ahoka  * to the same logical eraseblock.
   1208   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1209   1.1     ahoka  */
   1210   1.1     ahoka int
   1211   1.1     ahoka nor_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1212   1.1     ahoka     struct chfs_eb_hdr *ebhdr, int pebnr, int leb_status)
   1213   1.1     ahoka {
   1214   1.1     ahoka 	int err, lnr, ec;
   1215   1.1     ahoka 	struct chfs_scan_leb *sleb, *old;
   1216   1.1     ahoka 
   1217   1.1     ahoka 	lnr = CHFS_GET_LID(ebhdr->u.nor_hdr.lid);
   1218   1.1     ahoka 	ec = le32toh(ebhdr->ec_hdr.erase_cnt);
   1219   1.1     ahoka 
   1220   1.1     ahoka 	sleb = kmem_alloc(sizeof(struct chfs_scan_leb), KM_SLEEP);
   1221   1.1     ahoka 
   1222   1.1     ahoka 	sleb->erase_cnt = ec;
   1223   1.1     ahoka 	sleb->lnr = lnr;
   1224   1.1     ahoka 	sleb->pebnr = pebnr;
   1225   1.1     ahoka 	sleb->info = leb_status;
   1226   1.1     ahoka 
   1227   1.1     ahoka 	old = RB_INSERT(scan_leb_used_rbtree, &si->used, sleb);
   1228   1.1     ahoka 	if (old) {
   1229   1.1     ahoka 		kmem_free(sleb, sizeof(struct chfs_scan_leb));
   1230   1.1     ahoka 		/* There is already an eraseblock in the used tree */
   1231   1.1     ahoka 		/* If the new one is bad */
   1232   1.1     ahoka 		if (EBHDR_LEB_DIRTY == leb_status &&
   1233   1.1     ahoka 		    EBHDR_LEB_OK == old->info) {
   1234   1.1     ahoka 			return scan_add_to_queue(si, pebnr, ec, &si->erase);
   1235   1.1     ahoka 		} else {
   1236   1.1     ahoka 			err = scan_add_to_queue(si, old->pebnr,
   1237   1.1     ahoka 			    old->erase_cnt, &si->erase);
   1238   1.1     ahoka 			if (err) {
   1239   1.1     ahoka 				return err;
   1240   1.1     ahoka 			}
   1241   1.1     ahoka 
   1242   1.1     ahoka 			old->erase_cnt = ec;
   1243   1.1     ahoka 			old->lnr = lnr;
   1244   1.1     ahoka 			old->pebnr = pebnr;
   1245   1.1     ahoka 			old->info = leb_status;
   1246   1.1     ahoka 			return 0;
   1247   1.1     ahoka 		}
   1248   1.1     ahoka 	}
   1249   1.1     ahoka 	return 0;
   1250   1.1     ahoka }
   1251   1.1     ahoka 
   1252   1.1     ahoka /**
   1253   1.1     ahoka  * nor_process eb -read the headers from NOR flash, check them and add to
   1254   1.1     ahoka  * 				   the scanning information
   1255   1.1     ahoka  * @ebh: chfs eraseblock handler
   1256   1.1     ahoka  * @si: chfs scanning information
   1257   1.1     ahoka  * @pebnr: physical eraseblock number
   1258   1.1     ahoka  *
   1259   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1260   1.1     ahoka  */
   1261   1.1     ahoka int
   1262   1.1     ahoka nor_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1263   1.1     ahoka     int pebnr, struct chfs_eb_hdr *ebhdr)
   1264   1.1     ahoka {
   1265   1.1     ahoka 	int err, erase_cnt, leb_status;
   1266   1.1     ahoka 
   1267   1.1     ahoka 	err = ebh->ops->read_eb_hdr(ebh, pebnr, ebhdr);
   1268   1.1     ahoka 	if (err)
   1269   1.1     ahoka 		return err;
   1270   1.1     ahoka 
   1271   1.1     ahoka 	erase_cnt = le32toh(ebhdr->ec_hdr.erase_cnt);
   1272   1.1     ahoka 	dbg_ebh("erase_cnt: %d\n", erase_cnt);
   1273   1.1     ahoka 	leb_status = ebh->ops->check_eb_hdr(ebh, ebhdr);
   1274   1.1     ahoka 	if (EBHDR_LEB_BADMAGIC == leb_status ||
   1275   1.1     ahoka 	    EBHDR_LEB_BADCRC == leb_status) {
   1276   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->corrupted);
   1277   1.1     ahoka 		return err;
   1278   1.1     ahoka 	}
   1279   1.1     ahoka 	else if (EBHDR_LEB_FREE == leb_status) {
   1280   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->free);
   1281   1.1     ahoka 		goto count_mean;
   1282   1.1     ahoka 	}
   1283   1.1     ahoka 	else if (EBHDR_LEB_NO_HDR == leb_status) {
   1284   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->erased);
   1285   1.1     ahoka 		return err;
   1286   1.1     ahoka 	}
   1287   1.1     ahoka 	else if (EBHDR_LEB_INVALIDATED == leb_status) {
   1288   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->erase);
   1289   1.1     ahoka 		return err;
   1290   1.1     ahoka 	}
   1291   1.1     ahoka 
   1292   1.1     ahoka 	err = nor_scan_add_to_used(ebh, si, ebhdr, pebnr, leb_status);
   1293   1.1     ahoka 	if (err)
   1294   1.1     ahoka 		return err;
   1295   1.1     ahoka 
   1296   1.1     ahoka 
   1297   1.1     ahoka count_mean:
   1298   1.1     ahoka 	si->sum_of_ec += erase_cnt;
   1299   1.1     ahoka 	si->num_of_eb++;
   1300   1.1     ahoka 
   1301   1.1     ahoka 	return err;
   1302   1.1     ahoka }
   1303   1.1     ahoka 
   1304   1.1     ahoka /*
   1305   1.1     ahoka  * nand_scan_add_to_used - add a physical eraseblock to the
   1306   1.1     ahoka  *                         used tree of scan info
   1307   1.1     ahoka  * @ebh: chfs eraseblock handler
   1308   1.1     ahoka  * @si: chfs scanning information
   1309   1.1     ahoka  * @ebhdr: eraseblock header
   1310   1.1     ahoka  * @pebnr: physical eraseblock number
   1311   1.1     ahoka  * @leb_status: the status of the PEB's eraseblock header
   1312   1.1     ahoka  *
   1313   1.1     ahoka  * This function adds a PEB to the used tree of the scanning information.
   1314   1.1     ahoka  * It handles the situations if there are more physical eraseblock referencing
   1315   1.1     ahoka  * to the same logical eraseblock.
   1316   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1317   1.1     ahoka  */
   1318   1.1     ahoka int
   1319   1.1     ahoka nand_scan_add_to_used(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1320   1.1     ahoka     struct chfs_eb_hdr *ebhdr, int pebnr)
   1321   1.1     ahoka {
   1322   1.1     ahoka 	int err, lnr, ec;
   1323   1.1     ahoka 	struct chfs_scan_leb *sleb, *old;
   1324   1.1     ahoka 	uint64_t serial = le64toh(ebhdr->u.nand_hdr.serial);
   1325   1.1     ahoka 
   1326   1.1     ahoka 	lnr = CHFS_GET_LID(ebhdr->u.nor_hdr.lid);
   1327   1.1     ahoka 	ec = le32toh(ebhdr->ec_hdr.erase_cnt);
   1328   1.1     ahoka 
   1329   1.1     ahoka 	sleb = kmem_alloc(sizeof(struct chfs_scan_leb), KM_SLEEP);
   1330   1.1     ahoka 
   1331   1.1     ahoka 	sleb->erase_cnt = ec;
   1332   1.1     ahoka 	sleb->lnr = lnr;
   1333   1.1     ahoka 	sleb->pebnr = pebnr;
   1334   1.1     ahoka 	sleb->info = serial;
   1335   1.1     ahoka 
   1336   1.1     ahoka 	old = RB_INSERT(scan_leb_used_rbtree, &si->used, sleb);
   1337   1.1     ahoka 	if (old) {
   1338   1.1     ahoka 		kmem_free(sleb, sizeof(struct chfs_scan_leb));
   1339   1.1     ahoka 		/* There is already an eraseblock in the used tree */
   1340   1.1     ahoka 		/* If the new one is bad */
   1341   1.1     ahoka 		if (serial < old->info)
   1342   1.1     ahoka 			return scan_add_to_queue(si, pebnr, ec, &si->erase);
   1343   1.1     ahoka 		else {
   1344   1.1     ahoka 			err = scan_add_to_queue(si,
   1345   1.1     ahoka 			    old->pebnr, old->erase_cnt, &si->erase);
   1346   1.1     ahoka 			if (err)
   1347   1.1     ahoka 				return err;
   1348   1.1     ahoka 
   1349   1.1     ahoka 			old->erase_cnt = ec;
   1350   1.1     ahoka 			old->lnr = lnr;
   1351   1.1     ahoka 			old->pebnr = pebnr;
   1352   1.1     ahoka 			old->info = serial;
   1353   1.1     ahoka 			return 0;
   1354   1.1     ahoka 		}
   1355   1.1     ahoka 	}
   1356   1.1     ahoka 	return 0;
   1357   1.1     ahoka }
   1358   1.1     ahoka 
   1359   1.1     ahoka /**
   1360   1.1     ahoka  * nand_process eb -read the headers from NAND flash, check them and add to the
   1361   1.1     ahoka  * 					scanning information
   1362   1.1     ahoka  * @ebh: chfs eraseblock handler
   1363   1.1     ahoka  * @si: chfs scanning information
   1364   1.1     ahoka  * @pebnr: physical eraseblock number
   1365   1.1     ahoka  *
   1366   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1367   1.1     ahoka  */
   1368   1.1     ahoka int
   1369   1.1     ahoka nand_process_eb(struct chfs_ebh *ebh, struct chfs_scan_info *si,
   1370   1.1     ahoka     int pebnr, struct chfs_eb_hdr *ebhdr)
   1371   1.1     ahoka {
   1372   1.1     ahoka 	int err, erase_cnt, leb_status;
   1373   1.1     ahoka 	uint64_t max_serial;
   1374   1.2     ahoka 	/* isbad() is defined on some ancient platforms, heh */
   1375   1.2     ahoka 	bool is_bad;
   1376   1.1     ahoka 
   1377   1.1     ahoka 	/* Check block is bad */
   1378   1.1     ahoka 	err = flash_block_isbad(ebh->flash_dev,
   1379   1.2     ahoka 	    pebnr * ebh->flash_if->erasesize, &is_bad);
   1380   1.1     ahoka 	if (err) {
   1381   1.1     ahoka 		chfs_err("checking block is bad failed\n");
   1382   1.1     ahoka 		return err;
   1383   1.1     ahoka 	}
   1384   1.2     ahoka 	if (is_bad) {
   1385   1.1     ahoka 		si->bad_peb_cnt++;
   1386   1.1     ahoka 		return 0;
   1387   1.1     ahoka 	}
   1388   1.1     ahoka 
   1389   1.1     ahoka 	err = ebh->ops->read_eb_hdr(ebh, pebnr, ebhdr);
   1390   1.1     ahoka 	if (err)
   1391   1.1     ahoka 		return err;
   1392   1.1     ahoka 
   1393   1.1     ahoka 	erase_cnt = le32toh(ebhdr->ec_hdr.erase_cnt);
   1394   1.1     ahoka 	leb_status = ebh->ops->check_eb_hdr(ebh, ebhdr);
   1395   1.1     ahoka 	if (EBHDR_LEB_BADMAGIC == leb_status ||
   1396   1.1     ahoka 	    EBHDR_LEB_BADCRC == leb_status) {
   1397   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->corrupted);
   1398   1.1     ahoka 		return err;
   1399   1.1     ahoka 	}
   1400   1.1     ahoka 	else if (EBHDR_LEB_FREE == leb_status) {
   1401   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->free);
   1402   1.1     ahoka 		goto count_mean;
   1403   1.1     ahoka 	}
   1404   1.1     ahoka 	else if (EBHDR_LEB_NO_HDR == leb_status) {
   1405   1.1     ahoka 		err = scan_add_to_queue(si, pebnr, erase_cnt, &si->erased);
   1406   1.1     ahoka 		return err;
   1407   1.1     ahoka 	}
   1408   1.1     ahoka 
   1409   1.1     ahoka 	err = nand_scan_add_to_used(ebh, si, ebhdr, pebnr);
   1410   1.1     ahoka 	if (err)
   1411   1.1     ahoka 		return err;
   1412   1.1     ahoka 
   1413   1.1     ahoka 	max_serial = le64toh(ebhdr->u.nand_hdr.serial);
   1414   1.1     ahoka 	if (max_serial > *ebh->max_serial) {
   1415   1.1     ahoka 		*ebh->max_serial = max_serial;
   1416   1.1     ahoka 	}
   1417   1.1     ahoka 
   1418   1.1     ahoka count_mean:
   1419   1.1     ahoka 	si->sum_of_ec += erase_cnt;
   1420   1.1     ahoka 	si->num_of_eb++;
   1421   1.1     ahoka 
   1422   1.1     ahoka 	return err;
   1423   1.1     ahoka }
   1424   1.1     ahoka 
   1425   1.1     ahoka /**
   1426   1.9    andvar  * chfs_scan - scans the media and returns information about it
   1427   1.1     ahoka  * @ebh: chfs eraseblock handler
   1428   1.1     ahoka  *
   1429   1.1     ahoka  * This function scans through the media and returns information about it or if
   1430   1.1     ahoka  * it fails NULL will be returned.
   1431   1.1     ahoka  */
   1432   1.1     ahoka struct chfs_scan_info *
   1433   1.1     ahoka chfs_scan(struct chfs_ebh *ebh)
   1434   1.1     ahoka {
   1435   1.1     ahoka 	struct chfs_scan_info *si;
   1436   1.1     ahoka 	struct chfs_eb_hdr *ebhdr;
   1437   1.1     ahoka 	int pebnr, err;
   1438   1.1     ahoka 
   1439   1.1     ahoka 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
   1440   1.1     ahoka 
   1441   1.1     ahoka 	TAILQ_INIT(&si->corrupted);
   1442   1.1     ahoka 	TAILQ_INIT(&si->free);
   1443   1.1     ahoka 	TAILQ_INIT(&si->erase);
   1444   1.1     ahoka 	TAILQ_INIT(&si->erased);
   1445   1.1     ahoka 	RB_INIT(&si->used);
   1446   1.1     ahoka 	si->bad_peb_cnt = 0;
   1447   1.1     ahoka 	si->num_of_eb = 0;
   1448   1.1     ahoka 	si->sum_of_ec = 0;
   1449   1.1     ahoka 
   1450   1.1     ahoka 	ebhdr = kmem_alloc(sizeof(*ebhdr), KM_SLEEP);
   1451   1.1     ahoka 
   1452   1.1     ahoka 	for (pebnr = 0; pebnr < ebh->peb_nr; pebnr++) {
   1453   1.1     ahoka 		dbg_ebh("processing PEB %d\n", pebnr);
   1454   1.1     ahoka 		err = ebh->ops->process_eb(ebh, si, pebnr, ebhdr);
   1455   1.1     ahoka 		if (err < 0)
   1456   1.1     ahoka 			goto out_ebhdr;
   1457   1.1     ahoka 	}
   1458   1.1     ahoka 	kmem_free(ebhdr, sizeof(*ebhdr));
   1459   1.1     ahoka 	dbg_ebh("[CHFS_SCAN] scanning information collected\n");
   1460   1.1     ahoka 	return si;
   1461   1.1     ahoka 
   1462   1.1     ahoka out_ebhdr:
   1463   1.1     ahoka 	kmem_free(ebhdr, sizeof(*ebhdr));
   1464   1.1     ahoka 	kmem_free(si, sizeof(*si));
   1465   1.1     ahoka 	return NULL;
   1466   1.1     ahoka }
   1467   1.1     ahoka 
   1468   1.1     ahoka /**
   1469   1.1     ahoka  * scan_info_destroy - frees all lists and trees in the scanning information
   1470   1.1     ahoka  * @si: the scanning information
   1471   1.1     ahoka  */
   1472   1.1     ahoka void
   1473   1.1     ahoka scan_info_destroy(struct chfs_scan_info *si)
   1474   1.1     ahoka {
   1475   1.1     ahoka 	EBH_QUEUE_DESTROY(&si->corrupted,
   1476   1.1     ahoka 	    struct chfs_scan_leb, u.queue);
   1477   1.1     ahoka 
   1478   1.1     ahoka 	EBH_QUEUE_DESTROY(&si->erase,
   1479   1.1     ahoka 	    struct chfs_scan_leb, u.queue);
   1480   1.1     ahoka 
   1481   1.1     ahoka 	EBH_QUEUE_DESTROY(&si->erased,
   1482   1.1     ahoka 	    struct chfs_scan_leb, u.queue);
   1483   1.1     ahoka 
   1484   1.1     ahoka 	EBH_QUEUE_DESTROY(&si->free,
   1485   1.1     ahoka 	    struct chfs_scan_leb, u.queue);
   1486   1.1     ahoka 
   1487   1.1     ahoka 	EBH_TREE_DESTROY(scan_leb_used_rbtree,
   1488   1.1     ahoka 	    &si->used, struct chfs_scan_leb);
   1489   1.1     ahoka 
   1490   1.1     ahoka 	kmem_free(si, sizeof(*si));
   1491   1.1     ahoka 	dbg_ebh("[SCAN_INFO_DESTROY] scanning information destroyed\n");
   1492   1.1     ahoka }
   1493   1.1     ahoka 
   1494   1.1     ahoka /**
   1495   1.1     ahoka  * scan_media - scan media
   1496   1.1     ahoka  *
   1497   1.1     ahoka  * @ebh - chfs eraseblock handler
   1498   1.1     ahoka  *
   1499   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1500   1.1     ahoka  */
   1501   1.1     ahoka 
   1502   1.1     ahoka int
   1503   1.1     ahoka scan_media(struct chfs_ebh *ebh)
   1504   1.1     ahoka {
   1505   1.1     ahoka 	int err, i, avg_ec;
   1506   1.1     ahoka 	struct chfs_scan_info *si;
   1507   1.1     ahoka 	struct chfs_scan_leb *sleb;
   1508   1.1     ahoka 
   1509   1.1     ahoka 	si = chfs_scan(ebh);
   1510   1.1     ahoka 	/*
   1511   1.1     ahoka 	 * Process the scan info, manage the eraseblock lists
   1512   1.1     ahoka 	 */
   1513   1.1     ahoka 	mutex_init(&ebh->ltree_lock, MUTEX_DEFAULT, IPL_NONE);
   1514   1.1     ahoka 	mutex_init(&ebh->erase_lock, MUTEX_DEFAULT, IPL_NONE);
   1515   1.1     ahoka 	RB_INIT(&ebh->ltree);
   1516   1.1     ahoka 	RB_INIT(&ebh->free);
   1517   1.1     ahoka 	RB_INIT(&ebh->in_use);
   1518   1.1     ahoka 	TAILQ_INIT(&ebh->to_erase);
   1519   1.1     ahoka 	TAILQ_INIT(&ebh->fully_erased);
   1520   1.1     ahoka 	mutex_init(&ebh->alc_mutex, MUTEX_DEFAULT, IPL_NONE);
   1521   1.1     ahoka 
   1522   1.1     ahoka 	ebh->peb_nr -= si->bad_peb_cnt;
   1523   1.1     ahoka 
   1524   1.1     ahoka 	/*
   1525   1.1     ahoka 	 * Create background thread for erasing
   1526   1.1     ahoka 	 */
   1527   1.1     ahoka 	erase_thread_start(ebh);
   1528   1.1     ahoka 
   1529   1.1     ahoka 	ebh->lmap = kmem_alloc(ebh->peb_nr * sizeof(int), KM_SLEEP);
   1530   1.1     ahoka 
   1531   1.1     ahoka 	for (i = 0; i < ebh->peb_nr; i++) {
   1532   1.1     ahoka 		ebh->lmap[i] = EBH_LEB_UNMAPPED;
   1533   1.1     ahoka 	}
   1534   1.1     ahoka 
   1535   1.1     ahoka 	if (si->num_of_eb == 0) {
   1536   1.1     ahoka 		/* The flash contains no data. */
   1537   1.1     ahoka 		avg_ec = 0;
   1538   1.1     ahoka 	}
   1539   1.1     ahoka 	else {
   1540   1.1     ahoka 		avg_ec = (int) (si->sum_of_ec / si->num_of_eb);
   1541   1.1     ahoka 	}
   1542   1.1     ahoka 	dbg_ebh("num_of_eb: %d\n", si->num_of_eb);
   1543   1.1     ahoka 
   1544   1.1     ahoka 	mutex_enter(&ebh->erase_lock);
   1545   1.1     ahoka 
   1546   1.1     ahoka 	RB_FOREACH(sleb, scan_leb_used_rbtree, &si->used) {
   1547   1.1     ahoka 		ebh->lmap[sleb->lnr] = sleb->pebnr;
   1548   1.1     ahoka 		err = add_peb_to_in_use(ebh, sleb->pebnr, sleb->erase_cnt);
   1549   1.1     ahoka 		if (err)
   1550   1.1     ahoka 			goto out_free;
   1551   1.1     ahoka 	}
   1552   1.1     ahoka 
   1553   1.1     ahoka 	TAILQ_FOREACH(sleb, &si->erased, u.queue) {
   1554   1.1     ahoka 		err = add_peb_to_erase_queue(ebh, sleb->pebnr, avg_ec,
   1555   1.1     ahoka 		    &ebh->fully_erased);
   1556   1.1     ahoka 		if (err)
   1557   1.1     ahoka 			goto out_free;
   1558   1.1     ahoka 	}
   1559   1.1     ahoka 
   1560   1.1     ahoka 	TAILQ_FOREACH(sleb, &si->erase, u.queue) {
   1561   1.1     ahoka 		err = add_peb_to_erase_queue(ebh, sleb->pebnr, avg_ec,
   1562   1.1     ahoka 		    &ebh->to_erase);
   1563   1.1     ahoka 		if (err)
   1564   1.1     ahoka 			goto out_free;
   1565   1.1     ahoka 	}
   1566   1.1     ahoka 
   1567   1.1     ahoka 	TAILQ_FOREACH(sleb, &si->free, u.queue) {
   1568   1.1     ahoka 		err = add_peb_to_free(ebh, sleb->pebnr, sleb->erase_cnt);
   1569   1.1     ahoka 		if (err)
   1570   1.1     ahoka 			goto out_free;
   1571   1.1     ahoka 	}
   1572   1.1     ahoka 
   1573   1.1     ahoka 	TAILQ_FOREACH(sleb, &si->corrupted, u.queue) {
   1574   1.1     ahoka 		err = add_peb_to_erase_queue(ebh, sleb->pebnr, avg_ec,
   1575   1.1     ahoka 		    &ebh->to_erase);
   1576   1.1     ahoka 		if (err)
   1577   1.1     ahoka 			goto out_free;
   1578   1.1     ahoka 	}
   1579   1.1     ahoka 	mutex_exit(&ebh->erase_lock);
   1580   1.1     ahoka 	scan_info_destroy(si);
   1581   1.1     ahoka 	return 0;
   1582   1.1     ahoka 
   1583   1.1     ahoka out_free:
   1584   1.1     ahoka 	mutex_exit(&ebh->erase_lock);
   1585   1.1     ahoka 	kmem_free(ebh->lmap, ebh->peb_nr * sizeof(int));
   1586   1.1     ahoka 	scan_info_destroy(si);
   1587   1.1     ahoka 	dbg_ebh("[SCAN_MEDIA] returning with error: %d\n", err);
   1588   1.1     ahoka 	return err;
   1589   1.1     ahoka }
   1590   1.1     ahoka 
   1591   1.1     ahoka /*****************************************************************************/
   1592   1.1     ahoka /* End of Scan related operations					     */
   1593   1.1     ahoka /*****************************************************************************/
   1594   1.1     ahoka 
   1595   1.1     ahoka /**
   1596   1.9    andvar  * ebh_open - opens mtd device and init eraseblock header
   1597   1.1     ahoka  * @ebh: eraseblock handler
   1598   1.1     ahoka  * @flash_nr: flash device number to use
   1599   1.1     ahoka  *
   1600   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1601   1.1     ahoka  */
   1602   1.1     ahoka int
   1603   1.1     ahoka ebh_open(struct chfs_ebh *ebh, dev_t dev)
   1604   1.1     ahoka {
   1605   1.1     ahoka 	int err;
   1606   1.1     ahoka 
   1607   1.1     ahoka 	ebh->flash_dev = flash_get_device(dev);
   1608   1.1     ahoka 	if (!ebh->flash_dev) {
   1609   1.1     ahoka 		aprint_error("ebh_open: cant get flash device\n");
   1610   1.1     ahoka 		return ENODEV;
   1611   1.1     ahoka 	}
   1612   1.1     ahoka 
   1613   1.1     ahoka 	ebh->flash_if = flash_get_interface(dev);
   1614   1.1     ahoka 	if (!ebh->flash_if) {
   1615   1.1     ahoka 		aprint_error("ebh_open: cant get flash interface\n");
   1616   1.1     ahoka 		return ENODEV;
   1617   1.1     ahoka 	}
   1618   1.1     ahoka 
   1619   1.1     ahoka 	ebh->flash_size = flash_get_size(dev);
   1620   1.1     ahoka 	ebh->peb_nr = ebh->flash_size / ebh->flash_if->erasesize;
   1621   1.1     ahoka //	ebh->peb_nr = ebh->flash_if->size / ebh->flash_if->erasesize;
   1622   1.1     ahoka 	/* Set up flash operations based on flash type */
   1623   1.1     ahoka 	ebh->ops = kmem_alloc(sizeof(struct chfs_ebh_ops), KM_SLEEP);
   1624   1.1     ahoka 
   1625   1.1     ahoka 	switch (ebh->flash_if->type) {
   1626   1.1     ahoka 	case FLASH_TYPE_NOR:
   1627   1.1     ahoka 		ebh->eb_size = ebh->flash_if->erasesize -
   1628   1.1     ahoka 		    CHFS_EB_EC_HDR_SIZE - CHFS_EB_HDR_NOR_SIZE;
   1629   1.1     ahoka 
   1630   1.1     ahoka 		ebh->ops->read_eb_hdr = nor_read_eb_hdr;
   1631   1.1     ahoka 		ebh->ops->write_eb_hdr = nor_write_eb_hdr;
   1632   1.1     ahoka 		ebh->ops->check_eb_hdr = nor_check_eb_hdr;
   1633   1.1     ahoka 		ebh->ops->mark_eb_hdr_dirty_flash =
   1634   1.1     ahoka 		    nor_mark_eb_hdr_dirty_flash;
   1635   1.1     ahoka 		ebh->ops->invalidate_eb_hdr = nor_invalidate_eb_hdr;
   1636   1.1     ahoka 		ebh->ops->mark_eb_hdr_free = mark_eb_hdr_free;
   1637   1.1     ahoka 
   1638   1.1     ahoka 		ebh->ops->process_eb = nor_process_eb;
   1639   1.1     ahoka 
   1640   1.1     ahoka 		ebh->ops->create_eb_hdr = nor_create_eb_hdr;
   1641   1.1     ahoka 		ebh->ops->calc_data_offs = nor_calc_data_offs;
   1642   1.1     ahoka 
   1643   1.1     ahoka 		ebh->max_serial = NULL;
   1644   1.1     ahoka 		break;
   1645   1.1     ahoka 	case FLASH_TYPE_NAND:
   1646   1.1     ahoka 		ebh->eb_size = ebh->flash_if->erasesize -
   1647   1.1     ahoka 		    2 * ebh->flash_if->page_size;
   1648   1.1     ahoka 
   1649   1.1     ahoka 		ebh->ops->read_eb_hdr = nand_read_eb_hdr;
   1650   1.1     ahoka 		ebh->ops->write_eb_hdr = nand_write_eb_hdr;
   1651   1.1     ahoka 		ebh->ops->check_eb_hdr = nand_check_eb_hdr;
   1652   1.1     ahoka 		ebh->ops->mark_eb_hdr_free = mark_eb_hdr_free;
   1653   1.1     ahoka 		ebh->ops->mark_eb_hdr_dirty_flash = NULL;
   1654   1.1     ahoka 		ebh->ops->invalidate_eb_hdr = NULL;
   1655   1.1     ahoka 
   1656   1.1     ahoka 		ebh->ops->process_eb = nand_process_eb;
   1657   1.1     ahoka 
   1658   1.1     ahoka 		ebh->ops->create_eb_hdr = nand_create_eb_hdr;
   1659   1.1     ahoka 		ebh->ops->calc_data_offs = nand_calc_data_offs;
   1660   1.1     ahoka 
   1661   1.1     ahoka 		ebh->max_serial = kmem_alloc(sizeof(uint64_t), KM_SLEEP);
   1662   1.1     ahoka 
   1663   1.1     ahoka 		*ebh->max_serial = 0;
   1664   1.1     ahoka 		break;
   1665   1.1     ahoka 	default:
   1666   1.1     ahoka 		return 1;
   1667   1.1     ahoka 	}
   1668   1.1     ahoka 	printf("opening ebh: eb_size: %zu\n", ebh->eb_size);
   1669   1.1     ahoka 	err = scan_media(ebh);
   1670   1.1     ahoka 	if (err) {
   1671   1.1     ahoka 		dbg_ebh("Scan failed.");
   1672   1.1     ahoka 		kmem_free(ebh->ops, sizeof(struct chfs_ebh_ops));
   1673   1.1     ahoka 		kmem_free(ebh, sizeof(struct chfs_ebh));
   1674   1.1     ahoka 		return err;
   1675   1.1     ahoka 	}
   1676   1.1     ahoka 	return 0;
   1677   1.1     ahoka }
   1678   1.1     ahoka 
   1679   1.1     ahoka /**
   1680   1.1     ahoka  * ebh_close - close ebh
   1681   1.1     ahoka  * @ebh: eraseblock handler
   1682   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1683   1.1     ahoka  */
   1684   1.1     ahoka int
   1685   1.1     ahoka ebh_close(struct chfs_ebh *ebh)
   1686   1.1     ahoka {
   1687   1.1     ahoka 	erase_thread_stop(ebh);
   1688   1.1     ahoka 
   1689   1.1     ahoka 	EBH_TREE_DESTROY(peb_free_rbtree, &ebh->free, struct chfs_peb);
   1690   1.1     ahoka 	EBH_TREE_DESTROY(peb_in_use_rbtree, &ebh->in_use, struct chfs_peb);
   1691   1.1     ahoka 
   1692   1.1     ahoka 	EBH_QUEUE_DESTROY(&ebh->fully_erased, struct chfs_peb, u.queue);
   1693   1.1     ahoka 	EBH_QUEUE_DESTROY(&ebh->to_erase, struct chfs_peb, u.queue);
   1694   1.1     ahoka 
   1695   1.1     ahoka 	/* XXX HACK, see ebh.h */
   1696   1.1     ahoka 	EBH_TREE_DESTROY_MUTEX(ltree_rbtree, &ebh->ltree,
   1697   1.1     ahoka 	    struct chfs_ltree_entry);
   1698   1.1     ahoka 
   1699   1.1     ahoka 	KASSERT(!mutex_owned(&ebh->ltree_lock));
   1700   1.1     ahoka 	KASSERT(!mutex_owned(&ebh->alc_mutex));
   1701   1.1     ahoka 	KASSERT(!mutex_owned(&ebh->erase_lock));
   1702   1.1     ahoka 
   1703   1.1     ahoka 	mutex_destroy(&ebh->ltree_lock);
   1704   1.1     ahoka 	mutex_destroy(&ebh->alc_mutex);
   1705   1.1     ahoka 	mutex_destroy(&ebh->erase_lock);
   1706   1.1     ahoka 
   1707   1.1     ahoka 	kmem_free(ebh->ops, sizeof(struct chfs_ebh_ops));
   1708   1.1     ahoka 	kmem_free(ebh, sizeof(struct chfs_ebh));
   1709   1.1     ahoka 
   1710   1.1     ahoka 	return 0;
   1711   1.1     ahoka }
   1712   1.1     ahoka 
   1713   1.1     ahoka /**
   1714   1.1     ahoka  * ebh_read_leb - read data from leb
   1715   1.1     ahoka  * @ebh: eraseblock handler
   1716   1.1     ahoka  * @lnr: logical eraseblock number
   1717   1.1     ahoka  * @buf: buffer to read to
   1718   1.1     ahoka  * @offset: offset from where to read
   1719   1.1     ahoka  * @len: bytes number to read
   1720   1.1     ahoka  *
   1721   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1722   1.1     ahoka  */
   1723   1.1     ahoka int
   1724   1.1     ahoka ebh_read_leb(struct chfs_ebh *ebh, int lnr, char *buf, uint32_t offset,
   1725   1.1     ahoka     size_t len, size_t *retlen)
   1726   1.1     ahoka {
   1727   1.1     ahoka 	int err, pebnr;
   1728   1.1     ahoka 	off_t data_offset;
   1729   1.1     ahoka 
   1730   1.1     ahoka 	KASSERT(offset + len <= ebh->eb_size);
   1731   1.1     ahoka 
   1732   1.1     ahoka 	err = leb_read_lock(ebh, lnr);
   1733   1.1     ahoka 	if (err)
   1734   1.1     ahoka 		return err;
   1735   1.3     ttoth 
   1736   1.1     ahoka 	pebnr = ebh->lmap[lnr];
   1737   1.1     ahoka 	/* If PEB is not mapped the buffer is filled with 0xFF */
   1738   1.1     ahoka 	if (EBH_LEB_UNMAPPED == pebnr) {
   1739   1.1     ahoka 		leb_read_unlock(ebh, lnr);
   1740   1.1     ahoka 		memset(buf, 0xFF, len);
   1741   1.1     ahoka 		return 0;
   1742   1.1     ahoka 	}
   1743   1.1     ahoka 
   1744   1.1     ahoka 	/* Read data */
   1745   1.1     ahoka 	data_offset = ebh->ops->calc_data_offs(ebh, pebnr, offset);
   1746   1.1     ahoka 	err = flash_read(ebh->flash_dev, data_offset, len, retlen,
   1747   1.1     ahoka 	    (unsigned char *) buf);
   1748   1.1     ahoka 	if (err)
   1749   1.1     ahoka 		goto out_free;
   1750   1.1     ahoka 
   1751   1.1     ahoka 	KASSERT(len == *retlen);
   1752   1.1     ahoka 
   1753   1.1     ahoka out_free:
   1754   1.1     ahoka 	leb_read_unlock(ebh, lnr);
   1755   1.1     ahoka 	return err;
   1756   1.1     ahoka }
   1757   1.1     ahoka 
   1758   1.1     ahoka /**
   1759   1.1     ahoka  * get_peb: get a free physical eraseblock
   1760   1.1     ahoka  * @ebh - chfs eraseblock handler
   1761   1.1     ahoka  *
   1762   1.1     ahoka  * This function gets a free eraseblock from the ebh->free RB-tree.
   1763   1.8    andvar  * The first entry will be returned and deleted from the tree.
   1764   1.1     ahoka  * The entries sorted by the erase counters, so the PEB with the smallest
   1765   1.1     ahoka  * erase counter will be added back.
   1766   1.1     ahoka  * If something goes bad a negative value will be returned.
   1767   1.1     ahoka  */
   1768   1.1     ahoka int
   1769   1.1     ahoka get_peb(struct chfs_ebh *ebh)
   1770   1.1     ahoka {
   1771   1.1     ahoka 	int err, pebnr;
   1772   1.1     ahoka 	struct chfs_peb *peb;
   1773   1.1     ahoka 
   1774   1.1     ahoka retry:
   1775   1.1     ahoka 	mutex_enter(&ebh->erase_lock);
   1776   1.1     ahoka 	//dbg_ebh("LOCK: ebh->erase_lock spin locked in get_peb()\n");
   1777   1.1     ahoka 	if (RB_EMPTY(&ebh->free)) {
   1778   1.1     ahoka 		/*There is no more free PEBs in the tree*/
   1779   1.1     ahoka 		if (TAILQ_EMPTY(&ebh->to_erase) &&
   1780   1.1     ahoka 		    TAILQ_EMPTY(&ebh->fully_erased)) {
   1781   1.1     ahoka 			mutex_exit(&ebh->erase_lock);
   1782   1.1     ahoka 			//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in get_peb()\n");
   1783   1.1     ahoka 			return ENOSPC;
   1784   1.1     ahoka 		}
   1785   1.1     ahoka 		err = free_peb(ebh);
   1786   1.1     ahoka 
   1787   1.1     ahoka 		mutex_exit(&ebh->erase_lock);
   1788   1.1     ahoka 		//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in get_peb()\n");
   1789   1.1     ahoka 
   1790   1.1     ahoka 		if (err)
   1791   1.1     ahoka 			return err;
   1792   1.1     ahoka 		goto retry;
   1793   1.1     ahoka 	}
   1794   1.1     ahoka 	peb = RB_MIN(peb_free_rbtree, &ebh->free);
   1795   1.1     ahoka 	pebnr = peb->pebnr;
   1796   1.1     ahoka 	RB_REMOVE(peb_free_rbtree, &ebh->free, peb);
   1797   1.1     ahoka 	err = add_peb_to_in_use(ebh, peb->pebnr, peb->erase_cnt);
   1798   1.1     ahoka 	if (err)
   1799   1.1     ahoka 		pebnr = err;
   1800   1.1     ahoka 
   1801   1.1     ahoka 	kmem_free(peb, sizeof(struct chfs_peb));
   1802   1.1     ahoka 
   1803   1.1     ahoka 	mutex_exit(&ebh->erase_lock);
   1804   1.1     ahoka 	//dbg_ebh("UNLOCK: ebh->erase_lock spin unlocked in get_peb()\n");
   1805   1.1     ahoka 
   1806   1.1     ahoka 	return pebnr;
   1807   1.1     ahoka }
   1808   1.1     ahoka 
   1809   1.1     ahoka /**
   1810   1.1     ahoka  * ebh_write_leb - write data to leb
   1811   1.1     ahoka  * @ebh: eraseblock handler
   1812   1.1     ahoka  * @lnr: logical eraseblock number
   1813   1.1     ahoka  * @buf: data to write
   1814   1.1     ahoka  * @offset: offset where to write
   1815   1.1     ahoka  * @len: bytes number to write
   1816   1.1     ahoka  *
   1817   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1818   1.1     ahoka  */
   1819   1.1     ahoka int
   1820   1.1     ahoka ebh_write_leb(struct chfs_ebh *ebh, int lnr, char *buf, uint32_t offset,
   1821   1.1     ahoka     size_t len, size_t *retlen)
   1822   1.1     ahoka {
   1823   1.1     ahoka 	int err, pebnr, retries = 0;
   1824   1.1     ahoka 	off_t data_offset;
   1825   1.1     ahoka 	struct chfs_eb_hdr *ebhdr;
   1826   1.1     ahoka 
   1827   1.1     ahoka 	dbg("offset: %d | len: %zu | (offset+len): %zu "
   1828   1.1     ahoka 	    " | ebsize: %zu\n", offset, len, (offset+len), ebh->eb_size);
   1829   1.1     ahoka 
   1830   1.1     ahoka 	KASSERT(offset + len <= ebh->eb_size);
   1831   1.1     ahoka 
   1832   1.1     ahoka 	err = leb_write_lock(ebh, lnr);
   1833   1.1     ahoka 	if (err)
   1834   1.1     ahoka 		return err;
   1835   1.1     ahoka 
   1836   1.1     ahoka 	pebnr = ebh->lmap[lnr];
   1837   1.1     ahoka 	/* If the LEB is mapped write out data */
   1838   1.1     ahoka 	if (pebnr != EBH_LEB_UNMAPPED) {
   1839   1.1     ahoka 		data_offset = ebh->ops->calc_data_offs(ebh, pebnr, offset);
   1840   1.1     ahoka 		err = flash_write(ebh->flash_dev, data_offset, len, retlen,
   1841   1.1     ahoka 		    (unsigned char *) buf);
   1842   1.1     ahoka 
   1843   1.1     ahoka 		if (err) {
   1844   1.1     ahoka 			chfs_err("error %d while writing %zu bytes to PEB "
   1845   1.1     ahoka 			    "%d:%ju, written %zu bytes\n",
   1846   1.1     ahoka 			    err, len, pebnr, (uintmax_t )offset, *retlen);
   1847   1.1     ahoka 		} else {
   1848   1.1     ahoka 			KASSERT(len == *retlen);
   1849   1.1     ahoka 		}
   1850   1.1     ahoka 
   1851   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   1852   1.1     ahoka 		return err;
   1853   1.1     ahoka 	}
   1854   1.1     ahoka 
   1855   1.1     ahoka 	/*
   1856   1.1     ahoka 	 * If the LEB is unmapped, get a free PEB and write the
   1857   1.1     ahoka 	 * eraseblock header first
   1858   1.1     ahoka 	 */
   1859   1.1     ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
   1860   1.1     ahoka 
   1861   1.1     ahoka 	/* Setting up eraseblock header properties */
   1862   1.1     ahoka 	ebh->ops->create_eb_hdr(ebhdr, lnr);
   1863   1.1     ahoka 
   1864   1.1     ahoka retry:
   1865   1.1     ahoka 	/* Getting a physical eraseblock from the wear leveling system */
   1866   1.1     ahoka 	pebnr = get_peb(ebh);
   1867   1.1     ahoka 	if (pebnr < 0) {
   1868   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   1869   1.1     ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1870   1.1     ahoka 		return pebnr;
   1871   1.1     ahoka 	}
   1872   1.1     ahoka 
   1873   1.1     ahoka 	/* Write the eraseblock header to the media */
   1874   1.1     ahoka 	err = ebh->ops->write_eb_hdr(ebh, pebnr, ebhdr);
   1875   1.1     ahoka 	if (err) {
   1876   1.1     ahoka 		chfs_warn(
   1877   1.1     ahoka 			"error writing eraseblock header: LEB %d , PEB %d\n",
   1878   1.1     ahoka 			lnr, pebnr);
   1879   1.1     ahoka 		goto write_error;
   1880   1.1     ahoka 	}
   1881   1.1     ahoka 
   1882   1.1     ahoka 	/* Write out data */
   1883   1.1     ahoka 	if (len) {
   1884   1.1     ahoka 		data_offset = ebh->ops->calc_data_offs(ebh, pebnr, offset);
   1885   1.1     ahoka 		err = flash_write(ebh->flash_dev,
   1886   1.1     ahoka 		    data_offset, len, retlen, (unsigned char *) buf);
   1887   1.1     ahoka 		if (err) {
   1888   1.1     ahoka 			chfs_err("error %d while writing %zu bytes to PEB "
   1889   1.1     ahoka 			    " %d:%ju, written %zu bytes\n",
   1890   1.1     ahoka 			    err, len, pebnr, (uintmax_t )offset, *retlen);
   1891   1.1     ahoka 			goto write_error;
   1892   1.1     ahoka 		}
   1893   1.1     ahoka 	}
   1894   1.1     ahoka 
   1895   1.1     ahoka 	ebh->lmap[lnr] = pebnr;
   1896   1.1     ahoka 	leb_write_unlock(ebh, lnr);
   1897   1.1     ahoka 	kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1898   1.1     ahoka 
   1899   1.1     ahoka 	return 0;
   1900   1.1     ahoka 
   1901   1.1     ahoka write_error: err = release_peb(ebh, pebnr);
   1902   1.1     ahoka 	// max retries (NOW: 2)
   1903   1.1     ahoka 	if (err || CHFS_MAX_GET_PEB_RETRIES < ++retries) {
   1904   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   1905   1.1     ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1906   1.1     ahoka 		return err;
   1907   1.1     ahoka 	}
   1908   1.1     ahoka 	goto retry;
   1909   1.1     ahoka }
   1910   1.1     ahoka 
   1911   1.1     ahoka /**
   1912   1.1     ahoka  * ebh_erase_leb - erase a leb
   1913   1.1     ahoka  * @ebh: eraseblock handler
   1914   1.1     ahoka  * @lnr: leb number
   1915   1.1     ahoka  *
   1916   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   1917   1.1     ahoka  */
   1918   1.1     ahoka int
   1919   1.1     ahoka ebh_erase_leb(struct chfs_ebh *ebh, int lnr)
   1920   1.1     ahoka {
   1921   1.1     ahoka 	int err, pebnr;
   1922   1.1     ahoka 
   1923   1.1     ahoka 	leb_write_lock(ebh, lnr);
   1924   1.1     ahoka 
   1925   1.1     ahoka 	pebnr = ebh->lmap[lnr];
   1926   1.1     ahoka 	if (pebnr < 0) {
   1927   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   1928   1.1     ahoka 		return EBH_LEB_UNMAPPED;
   1929   1.1     ahoka 	}
   1930   1.1     ahoka 	err = release_peb(ebh, pebnr);
   1931   1.1     ahoka 	if (err)
   1932   1.1     ahoka 		goto out_unlock;
   1933   1.1     ahoka 
   1934   1.1     ahoka 	ebh->lmap[lnr] = EBH_LEB_UNMAPPED;
   1935   1.1     ahoka 	cv_signal(&ebh->bg_erase.eth_wakeup);
   1936   1.1     ahoka out_unlock:
   1937   1.1     ahoka 	leb_write_unlock(ebh, lnr);
   1938   1.1     ahoka 	return err;
   1939   1.1     ahoka }
   1940   1.1     ahoka 
   1941   1.1     ahoka /**
   1942   1.1     ahoka  * ebh_map_leb - maps a PEB to LEB
   1943   1.1     ahoka  * @ebh: eraseblock handler
   1944   1.1     ahoka  * @lnr: leb number
   1945   1.1     ahoka  *
   1946   1.1     ahoka  * Returns zero on success, error code in case of fail
   1947   1.1     ahoka  */
   1948   1.1     ahoka int
   1949   1.1     ahoka ebh_map_leb(struct chfs_ebh *ebh, int lnr)
   1950   1.1     ahoka {
   1951   1.1     ahoka 	int err, pebnr, retries = 0;
   1952   1.1     ahoka 	struct chfs_eb_hdr *ebhdr;
   1953   1.1     ahoka 
   1954   1.1     ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
   1955   1.1     ahoka 
   1956   1.1     ahoka 	err = leb_write_lock(ebh, lnr);
   1957   1.6  christos 	if (err) {
   1958   1.6  christos 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1959   1.1     ahoka 		return err;
   1960   1.6  christos 	}
   1961   1.1     ahoka 
   1962   1.1     ahoka retry:
   1963   1.1     ahoka 	pebnr = get_peb(ebh);
   1964   1.1     ahoka 	if (pebnr < 0) {
   1965   1.1     ahoka 		err = pebnr;
   1966   1.1     ahoka 		goto out_unlock;
   1967   1.1     ahoka 	}
   1968   1.1     ahoka 
   1969   1.1     ahoka 	ebh->ops->create_eb_hdr(ebhdr, lnr);
   1970   1.1     ahoka 
   1971   1.1     ahoka 	err = ebh->ops->write_eb_hdr(ebh, pebnr, ebhdr);
   1972   1.1     ahoka 	if (err) {
   1973   1.1     ahoka 		chfs_warn(
   1974   1.1     ahoka 			"error writing eraseblock header: LEB %d , PEB %d\n",
   1975   1.1     ahoka 			lnr, pebnr);
   1976   1.1     ahoka 		goto write_error;
   1977   1.1     ahoka 	}
   1978   1.1     ahoka 
   1979   1.1     ahoka 	ebh->lmap[lnr] = pebnr;
   1980   1.1     ahoka 
   1981   1.1     ahoka out_unlock:
   1982   1.1     ahoka 	leb_write_unlock(ebh, lnr);
   1983   1.1     ahoka 	return err;
   1984   1.1     ahoka 
   1985   1.1     ahoka write_error:
   1986   1.1     ahoka 	err = release_peb(ebh, pebnr);
   1987   1.1     ahoka 	// max retries (NOW: 2)
   1988   1.1     ahoka 	if (err || CHFS_MAX_GET_PEB_RETRIES < ++retries) {
   1989   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   1990   1.1     ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   1991   1.1     ahoka 		return err;
   1992   1.1     ahoka 	}
   1993   1.1     ahoka 	goto retry;
   1994   1.1     ahoka }
   1995   1.1     ahoka 
   1996   1.1     ahoka /**
   1997   1.1     ahoka  * ebh_unmap_leb -
   1998   1.1     ahoka  * @ebh: eraseblock handler
   1999   1.1     ahoka  * @lnr: leb number
   2000   1.1     ahoka  *
   2001   1.9    andvar  * Returns zero on success, error code in case of fail.
   2002   1.1     ahoka  */
   2003   1.1     ahoka int
   2004   1.1     ahoka ebh_unmap_leb(struct chfs_ebh *ebh, int lnr)
   2005   1.1     ahoka {
   2006   1.1     ahoka 	int err;
   2007   1.1     ahoka 
   2008   1.1     ahoka 	if (ebh_is_mapped(ebh, lnr) < 0)
   2009   1.1     ahoka 		/* If the eraseblock already unmapped */
   2010   1.1     ahoka 		return 0;
   2011   1.1     ahoka 
   2012   1.1     ahoka 	err = ebh_erase_leb(ebh, lnr);
   2013   1.1     ahoka 
   2014   1.1     ahoka 	return err;
   2015   1.1     ahoka }
   2016   1.1     ahoka 
   2017   1.1     ahoka /**
   2018   1.1     ahoka  * ebh_is_mapped - check if a PEB is mapped to @lnr
   2019   1.1     ahoka  * @ebh: eraseblock handler
   2020   1.1     ahoka  * @lnr: leb number
   2021   1.1     ahoka  *
   2022   1.9    andvar  * Returns 0 if the logical eraseblock is mapped, negative error code otherwise.
   2023   1.1     ahoka  */
   2024   1.1     ahoka int
   2025   1.1     ahoka ebh_is_mapped(struct chfs_ebh *ebh, int lnr)
   2026   1.1     ahoka {
   2027   1.1     ahoka 	int err, result;
   2028   1.1     ahoka 	err = leb_read_lock(ebh, lnr);
   2029   1.1     ahoka 	if (err)
   2030   1.1     ahoka 		return err;
   2031   1.1     ahoka 
   2032   1.1     ahoka 	result = ebh->lmap[lnr];
   2033   1.1     ahoka 	leb_read_unlock(ebh, lnr);
   2034   1.1     ahoka 
   2035   1.1     ahoka 	return result;
   2036   1.1     ahoka }
   2037   1.1     ahoka 
   2038   1.1     ahoka /**
   2039   1.1     ahoka  * ebh_change_leb - write the LEB to another PEB
   2040   1.1     ahoka  * @ebh: eraseblock handler
   2041   1.1     ahoka  * @lnr: leb number
   2042   1.1     ahoka  * @buf: data to write
   2043   1.1     ahoka  * @len: length of data
   2044   1.1     ahoka  * Returns zero in case of success, error code in case of fail.
   2045   1.1     ahoka  */
   2046   1.1     ahoka int
   2047   1.1     ahoka ebh_change_leb(struct chfs_ebh *ebh, int lnr, char *buf, size_t len,
   2048   1.1     ahoka     size_t *retlen)
   2049   1.1     ahoka {
   2050   1.1     ahoka 	int err, pebnr, pebnr_old, retries = 0;
   2051   1.1     ahoka 	off_t data_offset;
   2052   1.1     ahoka 
   2053   1.1     ahoka 	struct chfs_peb *peb = NULL;
   2054   1.1     ahoka 	struct chfs_eb_hdr *ebhdr;
   2055   1.1     ahoka 
   2056   1.1     ahoka 	if (ebh_is_mapped(ebh, lnr) < 0)
   2057   1.1     ahoka 		return EBH_LEB_UNMAPPED;
   2058   1.1     ahoka 
   2059   1.1     ahoka 	if (len == 0) {
   2060   1.1     ahoka 		err = ebh_unmap_leb(ebh, lnr);
   2061   1.1     ahoka 		if (err)
   2062   1.1     ahoka 			return err;
   2063   1.1     ahoka 		return ebh_map_leb(ebh, lnr);
   2064   1.1     ahoka 	}
   2065   1.1     ahoka 
   2066   1.1     ahoka 	ebhdr = kmem_alloc(sizeof(struct chfs_eb_hdr), KM_SLEEP);
   2067   1.1     ahoka 
   2068   1.1     ahoka 	pebnr_old = ebh->lmap[lnr];
   2069   1.1     ahoka 
   2070   1.1     ahoka 	mutex_enter(&ebh->alc_mutex);
   2071   1.1     ahoka 	err = leb_write_lock(ebh, lnr);
   2072   1.1     ahoka 	if (err)
   2073   1.1     ahoka 		goto out_mutex;
   2074   1.1     ahoka 
   2075   1.1     ahoka 	if (ebh->ops->mark_eb_hdr_dirty_flash) {
   2076   1.1     ahoka 		err = ebh->ops->mark_eb_hdr_dirty_flash(ebh, pebnr_old, lnr);
   2077   1.1     ahoka 		if (err)
   2078   1.1     ahoka 			goto out_unlock;
   2079   1.1     ahoka 	}
   2080   1.1     ahoka 
   2081   1.1     ahoka 	/* Setting up eraseblock header properties */
   2082   1.1     ahoka 	ebh->ops->create_eb_hdr(ebhdr, lnr);
   2083   1.1     ahoka 
   2084   1.1     ahoka retry:
   2085   1.1     ahoka 	/* Getting a physical eraseblock from the wear leveling system */
   2086   1.1     ahoka 	pebnr = get_peb(ebh);
   2087   1.1     ahoka 	if (pebnr < 0) {
   2088   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   2089   1.1     ahoka 		mutex_exit(&ebh->alc_mutex);
   2090   1.1     ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   2091   1.1     ahoka 		return pebnr;
   2092   1.1     ahoka 	}
   2093   1.1     ahoka 
   2094   1.1     ahoka 	err = ebh->ops->write_eb_hdr(ebh, pebnr, ebhdr);
   2095   1.1     ahoka 	if (err) {
   2096   1.1     ahoka 		chfs_warn(
   2097   1.1     ahoka 			"error writing eraseblock header: LEB %d , PEB %d",
   2098   1.1     ahoka 			lnr, pebnr);
   2099   1.1     ahoka 		goto write_error;
   2100   1.1     ahoka 	}
   2101   1.1     ahoka 
   2102   1.1     ahoka 	/* Write out data */
   2103   1.1     ahoka 	data_offset = ebh->ops->calc_data_offs(ebh, pebnr, 0);
   2104   1.1     ahoka 	err = flash_write(ebh->flash_dev, data_offset, len, retlen,
   2105   1.1     ahoka 	    (unsigned char *) buf);
   2106   1.1     ahoka 	if (err) {
   2107   1.1     ahoka 		chfs_err("error %d while writing %zu bytes to PEB %d:%ju,"
   2108   1.1     ahoka 		    " written %zu bytes",
   2109   1.1     ahoka 		    err, len, pebnr, (uintmax_t)data_offset, *retlen);
   2110   1.1     ahoka 		goto write_error;
   2111   1.1     ahoka 	}
   2112   1.1     ahoka 
   2113   1.1     ahoka 	ebh->lmap[lnr] = pebnr;
   2114   1.1     ahoka 
   2115   1.1     ahoka 	if (ebh->ops->invalidate_eb_hdr) {
   2116   1.1     ahoka 		err = ebh->ops->invalidate_eb_hdr(ebh, pebnr_old);
   2117   1.1     ahoka 		if (err)
   2118   1.1     ahoka 			goto out_unlock;
   2119   1.1     ahoka 	}
   2120   1.1     ahoka 	peb = find_peb_in_use(ebh, pebnr_old);
   2121   1.1     ahoka 	err = release_peb(ebh, peb->pebnr);
   2122   1.1     ahoka 
   2123   1.1     ahoka out_unlock:
   2124   1.1     ahoka 	leb_write_unlock(ebh, lnr);
   2125   1.1     ahoka 
   2126   1.1     ahoka out_mutex:
   2127   1.1     ahoka 	mutex_exit(&ebh->alc_mutex);
   2128   1.1     ahoka 	kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   2129   1.1     ahoka 	kmem_free(peb, sizeof(struct chfs_peb));
   2130   1.1     ahoka 	return err;
   2131   1.1     ahoka 
   2132   1.1     ahoka write_error:
   2133   1.1     ahoka 	err = release_peb(ebh, pebnr);
   2134   1.1     ahoka 	//max retries (NOW: 2)
   2135   1.1     ahoka 	if (err || CHFS_MAX_GET_PEB_RETRIES < ++retries) {
   2136   1.1     ahoka 		leb_write_unlock(ebh, lnr);
   2137   1.1     ahoka 		mutex_exit(&ebh->alc_mutex);
   2138   1.1     ahoka 		kmem_free(ebhdr, sizeof(struct chfs_eb_hdr));
   2139   1.1     ahoka 		return err;
   2140   1.1     ahoka 	}
   2141   1.1     ahoka 	goto retry;
   2142   1.1     ahoka }
   2143   1.1     ahoka 
   2144