Unsorted Block Images
Subsystems (3)
PATCH 28/44 bad block handling unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/badeb.c +234 -0
@@ -0,0 +1,234 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +#include <linux/spinlock.h> +#include <linux/types.h> +#include <mtd/ubi-header.h> +#include "ubi.h" +#include "alloc.h" +#include "badeb.h" +#include "debug.h" +#include "io.h" +#include "eba.h" +#include "account.h" +#include "wl.h" +#include "misc.h" + +/* The lowest number PEBs reserved for bad PEB handling */ +#define MIN_RESEVED_PEBS 1 + +static void calculate_reserved_max (const struct ubi_info *ubi); + +int ubi_beb_mark_bad (const struct ubi_info *ubi,int pnum) +{ +int err; +struct ubi_beb_info *beb = ubi->beb; + +ubi_assert (ubi->io->bad_allowed); + +ubi_msg ("PEB %d wend bad, mark it as bad" , pnum); + + err =ubi_io_mark_bad (ubi, pnum); +if (err) +return err; + +spin_lock (&beb->lock); +if (beb->reserved_pebs <=0 ) +ubi_warn ("no reserved physical eraseblocks!" ); + ubi->io->bad_peb_count +=1 ; + ubi->io->good_peb_count -=1 ; +calculate_reserved_max (ubi); + beb->reserved_pebs -=1 ; +if (beb->reserved_pebs < beb->reserved_max && !ubi_acc_reserve (ubi,1 )) + beb->reserved_pebs +=1 ; +spin_unlock (&beb->lock); + +return err; +} + +void ubi_beb_maintain_reserved (const struct ubi_info *ubi) +{ +int err, i, needed; +struct ubi_beb_info *beb = ubi->beb; + +if (!ubi->io->bad_allowed) +return ; + +spin_lock (&beb->lock); + needed = beb->reserved_max - beb->reserved_pebs; +for (i = needed; i >0 ; i--) { + err =ubi_acc_reserve (ubi, i); +if (!err) { +ubi_msg ("reserved %d PEBs for bad PEB handling" , i); + beb->reserved_pebs += i; +break ; + } + } +spin_unlock (&beb->lock); +} + +int ubi_beb_recover_peb (const struct ubi_info *ubi,int pnum,int vol_id, +int lnum,const void *buf,int offset,int len) +{ +int err, new_pnum, data_size, tries =0 ; +struct ubi_vid_hdr *vid_hdr; +unsigned char *new_buf; + +ubi_assert (ubi->io->bad_allowed); + +retry: + new_pnum =ubi_wl_get_peb (ubi,UBI_DATA_UNKNOWN ); +if (new_pnum <0 ) +return new_pnum; + +ubi_msg ("recover PEB %d, move its data to PEB %d" , pnum, new_pnum); + +/* At first recover the VID header */ + + vid_hdr =ubi_zalloc_vid_hdr (ubi); +if (!vid_hdr) { + err = -ENOMEM ; + goto out_put; + } + + err =ubi_io_read_vid_hdr (ubi, pnum, vid_hdr,1 ); +if (err && err !=UBI_IO_BITFLIPS ) { +if (err >0 ) + err = -EIO ; + goto out_vid_hdr; + } + + vid_hdr->leb_ver =cpu_to_ubi32 (ubi32_to_cpu (vid_hdr->leb_ver) +1 ); + err =ubi_io_write_vid_hdr (ubi, new_pnum, vid_hdr); +if (err) + goto vid_write_error; + +/* Now recover the data */ + + data_size = offset + len; + new_buf =ubi_kmalloc (data_size); +if (unlikely (!new_buf)) { + err = -ENOMEM ; + goto out_vid_hdr; + } +memset (new_buf + offset,0xFF , len); + +/* Read everything before the area where the write failure happened */ +if (offset >0 ) { + err =ubi_io_read_data (ubi, new_buf, pnum,0 , offset); +if (err && err !=UBI_IO_BITFLIPS ) + goto out_new_buf; + } + +/* +* Now we assume that before the failed write the (offset, offset+len) +* area contained all 0xFF bytes. This is true for NAND. This is not +* always true for NOR, but NOR don't admit of bad PEBs. +*/ +memcpy (new_buf + offset, buf, len); + + err =ubi_io_write_data (ubi, new_buf, new_pnum,0 , data_size); +if (err) + goto vid_data_write_error; + +ubi_kfree (new_buf); +ubi_free_vid_hdr (ubi, vid_hdr); +ubi_eba_leb_remap (ubi, vol_id, lnum, new_pnum); +ubi_wl_put_peb (ubi, pnum,1 ); +ubi_msg ("data was successfully recovered" ); +return 0 ; + +out_new_buf: +ubi_kfree (new_buf); +out_vid_hdr: +ubi_free_vid_hdr (ubi, vid_hdr); +out_put: +ubi_wl_put_peb (ubi, new_pnum,1 ); +return err; + +vid_data_write_error: +ubi_kfree (new_buf); +vid_write_error: +/* +* Bad luck? This physical eraseblock is bad too? Crud. Let's try to +* get another one. +*/ +ubi_warn ("failed to write to PEB %d" , new_pnum); +ubi_free_vid_hdr (ubi, vid_hdr); +ubi_wl_put_peb (ubi, new_pnum,1 ); +if (++tries >5 ) +/* We've tried too many times */ +return err; +ubi_msg ("try again" ); + goto retry; +} + +int ubi_beb_init (struct ubi_info *ubi) +{ +int i; +struct ubi_beb_info *beb; +const struct ubi_io_info *io = ubi->io; + + beb =ubi_kzalloc (sizeof (struct ubi_beb_info )); +if (!beb) +return -ENOMEM ; + ubi->beb = beb; + +spin_lock_init (&beb->lock); + +if (!io->bad_allowed) +return 0 ; + +calculate_reserved_max (ubi); + +for (i = beb->reserved_max; i >0 ; i--) +if (!ubi_acc_reserve (ubi, i)) { + beb->reserved_pebs = i; +break ; + } + +if (beb->reserved_pebs < beb->reserved_max) +/* No enough free physical eraseblocks */ +ubi_warn ("cannot reserve enough PEBs" ); + +return 0 ; +} + +void ubi_beb_close (struct ubi_info *ubi) +{ +ubi_kfree (ubi->beb); +} + +/** +* calculate_reserved_max - calculate how many PEBs must be reserved for bad +* eraseblock handling. +* +* @ubi: the UBI device description object +*/ +static void calculate_reserved_max (const struct ubi_info *ubi) +{ +struct ubi_beb_info *beb = ubi->beb; + +/* Reserve some amount of PEBs for bad PEB handling */ + beb->reserved_max = ubi->io->good_peb_count/100 ; + beb->reserved_max *=CONFIG_MTD_UBI_BEB_RESERVE ; +if (beb->reserved_max <MIN_RESEVED_PEBS ) + beb->reserved_max =MIN_RESEVED_PEBS ; +}
PATCH 29/44 update unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/upd.h +136 -0
@@ -0,0 +1,136 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* Copyright (C) Nokia Corporation, 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* The update unit. +* +* This unit implements the volume update operation. In the current +* implementation we use an update marker for this. The update marker is +* per-volume and is stored in the volume table. The update marker is set +* before the update starts, and removed after the update has been finished. +* So, if the update was interrupted by an unclean re-boot or due to some other +* reasons, the update marker is found and we know that the volume is damaged. +* +* Note, in general it is possible to implement the update operation as a +* transaction with a possibility to roll-back. But this is far more complex. +* +* This implementation does not support concurrent updates but it is not +* difficult to implement this. +*/ + +#ifndef __UBI_UPD_H__ +#define __UBI_UPD_H__ + +#include <linux/mutex.h> +#include <linux/types.h> + +struct ubi_info ; +struct ubi_scan_info ; + +/** +* ubi_upd_start - start update operation. +* +* @ubi: the UBI device description object +* @vol_id: volume ID to start update for +* @bytes: how many bytes will be written to the volume +* +* This function starts a volume update operation. If @bytes is zero, the +* volume will just be fully erased. This function returns zero in case of +* success and a negative error code in case of error. +* +* Note, this function does not check if the volume is being used by some other +* user. The (upper) calling layer has to do this. +*/ +int ubi_upd_start (const struct ubi_info *ubi,int vol_id,long long bytes); + +/** +* ubi_upd_write_data - write more data. +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume under update +* @buf: the data to write +* @count: how much bytes to write +* +* This function writes more data to the volume which is being updated. It may +* be called arbitrary number of times until all of the update bytes arrive. +* This function returns %0 in case of success, %1 if the update was +* successfully finished, and a negative error code in case of failure. +*/ +int ubi_upd_write_data (const struct ubi_info *ubi,int vol_id, +const void __user *buf,int count); + +/** +* ubi_upd_abort - abort an update operation. +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume to abort update operation for +* +* This function aborts an update operation. Returns zero in case of success +* and a negative error code in case of failure. +* +* If an update operation is aborted, the update marker stays on the flash +* media. A new successful update operation is required to make this volume +* usable. +*/ +int ubi_upd_abort (const struct ubi_info *ubi,int vol_id); + +/** +* ubi_upd_init_scan - initialize the update volume unit using scanning +* information. +* +* @ubi: the UBI device description object +* @si: a pointer to the scanning information +* +* This function returns zero in case of success, and a negative error code in +* case of failure. +*/ +int ubi_upd_init_scan (struct ubi_info *ubi,struct ubi_scan_info *si); + +/** +* ubi_upd_close - close the update volume unit. +* +* @ubi: the UBI device description object +*/ +void ubi_upd_close (const struct ubi_info *ubi); + +/** +* struct ubi_upd_info - UBI update unit description data structure. +* +* @updating: if any volume is being updated at the moment +* @vol_id: which volume utilizes the update marker at the moment +* @upd_ebs: how many eraseblocks are going to be updated +* @upd_received: how many bytes were already received by the update unit +* @upd_bytes: how many more bytes are expected to be received +* @upd_buf: a buffer which is used to collect update data during the update +* operation +* @mutex: serializes access to the volume update capability +*/ +struct ubi_upd_info { +int updating;/* public */ +int vol_id;/* private */ +int upd_ebs;/* private */ +long long upd_received;/* private */ +long long upd_bytes;/* private */ +void *upd_buf;/* private */ +struct mutex mutex;/* private */ +}; + +#endif /* !__UBI_UPD_H__ */
PATCH 30/44 update unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/upd.c +389 -0
@@ -0,0 +1,389 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* Copyright (C) Nokia Corporation, 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +* +* Jan 2007: Alexander Schmidt, hacked per-volume update. +*/ + +#include <linux/mutex.h> +#include <linux/err.h> +#include <linux/types.h> +#include <linux/sched.h> +#include <asm/uaccess.h> +#include <asm/div64.h> +#include "ubi.h" +#include "upd.h" +#include "wl.h" +#include "vtbl.h" +#include "io.h" +#include "eba.h" +#include "misc.h" +#include "account.h" +#include "alloc.h" +#include "scan.h" +#include "debug.h" + +static int ubi_wipe_out_volume (const struct ubi_info *ubi,int vol_id); + +int ubi_upd_start (const struct ubi_info *ubi,int vol_id,long long bytes) +{ +int err, rem; +uint64_t tmp; +const struct ubi_vtbl_vtr *vtr; +struct ubi_upd_info *upd = ubi->upd; + +dbg_upd ("start update of volume %d, %llu bytes" , vol_id, bytes); + +ubi_assert (vol_id >=0 && vol_id < ubi->acc->max_volumes); + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); +ubi_assert (bytes >=0 && + bytes <= vtr->usable_leb_size * vtr->reserved_pebs); + +mutex_lock (&upd->mutex); + +if (upd->updating ==1 ) { +dbg_err ("volume %d is being updated" , upd->vol_id); +mutex_unlock (&upd->mutex); +return -EBUSY ; + } + + upd->updating =1 ; + upd->vol_id = vol_id; + +/* Set the update marker first */ + err =ubi_vtbl_set_upd_marker (ubi, vol_id); +if (err) + goto out_unlock; + +/* Before updating, we wipe out the volume */ + err =ubi_wipe_out_volume (ubi, vol_id); +if (err) + goto out_unlock; + +if (bytes ==0 ) { +/* Zero bytes means the volume just has to be erased */ + err =ubi_vtbl_clear_upd_marker (ubi, vol_id,0 ); + goto out_unlock; + } + + tmp = bytes; + rem =do_div (tmp, vtr->usable_leb_size); + upd->upd_ebs = tmp + !!rem; + upd->upd_bytes = bytes; + upd->upd_received =0 ; + +mutex_unlock (&upd->mutex); +return 0 ; + +out_unlock: + upd->updating =0 ; +mutex_unlock (&upd->mutex); +return err; +} + +static int write_leb (const struct ubi_info *ubi,int vol_id,int lnum, +void *buf,int len,int used_ebs); + +int ubi_upd_write_data (const struct ubi_info *ubi,int vol_id, +const void __user *buf,int count) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_upd_info *upd = ubi->upd; +int lnum, offs, err =0 , len; +uint64_t tmp; + +dbg_upd ("write %d bytes requested" , count); + +ubi_assert (vol_id >=0 && vol_id < ubi->acc->max_volumes); +ubi_assert (count >=0 ); + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (unlikely (count ==0 )) +return 0 ; + +mutex_lock (&upd->mutex); + +if (unlikely (!upd->updating || upd->vol_id != vol_id)) { +dbg_err ("volume %d update was not started" , vol_id); + err = -EINVAL ; + goto out_unlock; + } + +ubi_assert (upd->upd_received >=0 && + upd->upd_received < upd->upd_bytes); + tmp = upd->upd_received; + offs =do_div (tmp, vtr->usable_leb_size); + lnum = tmp; + +if (upd->upd_received + count > upd->upd_bytes) + count = upd->upd_bytes - upd->upd_received; + +/* +* When updating volumes, we accumulate whole eraseblock and write +* it at once. +*/ + +if (offs !=0 ) { + +/* +* This is a write to the middle of the logical eraseblock. We +* copy the data to our update buffer and wait for more data or +* flush it (if the whole eraseblock is written or the update +* is finished). +*/ + + len = vtr->usable_leb_size - offs; +if (len > count) + len = count; + +dbg_upd ("copy more %d bytes of data" , len); + + err =copy_from_user (upd->upd_buf + offs, buf, len); +if (err) { +dbg_err ("memory access error" ); + err = -EFAULT ; + goto out_unlock; + } + +if (offs + len == vtr->usable_leb_size || + upd->upd_received + len == upd->upd_bytes) { +int flush_len = offs + len; + +/* +* OK, we gathered either the whole eraseblock or this +* is the last chunk, it's time to flush our buffer. +*/ + +ubi_assert (flush_len <= vtr->usable_leb_size); + + err =write_leb (ubi, vol_id, lnum, upd->upd_buf, flush_len, + upd->upd_ebs); +if (err) + goto out_unlock; + } + + upd->upd_received += len; + count -= len; + buf += len; + lnum +=1 ; + } + +/* +* If we've got more to write, let's continue. At this point we know we +* are starting from the beginning of an eraseblock. +*/ + +while (count) { +cond_resched (); + +if (count > vtr->usable_leb_size) + len = vtr->usable_leb_size; +else + len = count; + +dbg_upd ("copy %d bytes of user data" , len); + err =copy_from_user (upd->upd_buf, buf, len); +if (err) { +dbg_err ("memory access error" ); + err = -EFAULT ; + goto out_unlock; + } + +if (len == vtr->usable_leb_size || + upd->upd_received + len == upd->upd_bytes) { + err =write_leb (ubi, vol_id, lnum, upd->upd_buf, len, + upd->upd_ebs); +if (unlikely (err)) +break ; + } + + upd->upd_received += len; + count -= len; + lnum +=1 ; + buf += len; + } + +ubi_assert (upd->upd_received <= upd->upd_bytes); +if (upd->upd_received == upd->upd_bytes) { +/* The update is finished, clear the update marker */ + upd->updating =0 ; + err =ubi_vtbl_clear_upd_marker (ubi, vol_id, upd->upd_bytes); +if (err ==0 ) + err =1 ; + } + +out_unlock: +mutex_unlock (&upd->mutex); +return err; +} + +int ubi_upd_abort (const struct ubi_info *ubi,int vol_id) +{ +int err =0 ; +struct ubi_upd_info *upd = ubi->upd; + +mutex_lock (&upd->mutex); +if (upd->updating && upd->vol_id == vol_id) { +dbg_upd ("aborting volume %d update - it is damaged since now" , + vol_id); + upd->updating =0 ; + }else { +dbg_upd ("volume %d is not under update" , vol_id); + err = -EINVAL ; + } +mutex_unlock (&upd->mutex); + +return err; +} + +int ubi_upd_init_scan (struct ubi_info *ubi,struct ubi_scan_info *si) +{ +int err; +struct ubi_upd_info *upd; + +dbg_upd ("initialize the update unit" ); + + upd =ubi_kzalloc (sizeof (struct ubi_upd_info )); +if (!upd) +return -ENOMEM ; + ubi->upd = upd; + + upd->upd_buf =ubi_kmalloc (ubi->io->leb_size); +if (!upd->upd_buf) { + err = -ENOMEM ; + goto out_free_upd; + } + +mutex_init (&upd->mutex); + +dbg_upd ("the update unit is initialized" ); + +return 0 ; + +out_free_upd: +ubi_kfree (upd); +return err; +} + +void ubi_upd_close (const struct ubi_info *ubi) +{ +dbg_upd ("close the update unit" ); +ubi_kfree (ubi->upd->upd_buf); +ubi_kfree (ubi->upd); +} + +/** +* ubi_wipe_out_volume - wipe out an UBI volume. +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume to free +* +* This function erases all the volume's eraseblocks. Returns zero in case of +* success, and a negative error code in case of failure. +*/ +static int ubi_wipe_out_volume (const struct ubi_info *ubi,int vol_id) +{ +int i, err; +const struct ubi_vtbl_vtr *vtr; + +ubi_assert (vol_id >=0 && vol_id < ubi->acc->max_volumes); + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +for (i =0 ; i < vtr->reserved_pebs; i++) { +cond_resched (); + + err =ubi_eba_erase_leb (ubi, vol_id, i); +if (unlikely (err)) +return err; + } + + err =ubi_wl_flush (ubi); +return err; +} + +/** +* write_leb - write a portion of update data to a logical eraseblock. +* +* @ubi: the UBI device description object +* @vol_id: the volume ID to write to +* @lnum: the logical eraseblock number to write +* @buf: the data to write +* @len: how many bytes to write +* @used_ebs: how many logical eraseblocks will this volume contain +* +* This function writes a portion of update data to the corresponding logical +* eraseblock. If a dynamic volume is being updated, this function checks if +* the data contains 0xFF bytes at the end. If yes, the 0xFF bytes are not +* written. If the whole buffer contains only 0xFF bytes, the LEB is left +* unmapped. +* +* 0xFF bytes LEBs are skipped in case of dynamic volumes because writing of +* 0xFF bytes may have side effects and this PEB won't be writable anymore. In +* case of static volume 0xFF bytes are not got rid of because static volumes +* are treated specially in UBI: per-LEB CRC is calculated and checked and +* presence of all LEBs is taken care of. So we cannot cut amount of data +* written to LEBs of static volume or to skip some LEBs in case of static +* volumes. +*/ +static int write_leb (const struct ubi_info *ubi,int vol_id,int lnum, +void *buf,int len,int used_ebs) +{ +int err; +const struct ubi_vtbl_vtr *vtr; + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (vtr->vol_type ==UBI_DYNAMIC_VOLUME ) { +int l; + +ubi_assert (len == vtr->usable_leb_size); + +/* This is dynamic volume - skip the ending 0xFFs */ + l =ubi_calc_data_len (ubi, buf, len); +if (l ==0 ) { +dbg_upd ("LEB %d:%d contains only 0xFF bytes - skip" , + vol_id, lnum); +return 0 ; + } +if (len != l) +dbg_upd ("skip last %d bytes of data for LEB %d:%d" , + len - l, vol_id, lnum); + + err =ubi_eba_write_leb (ubi, vol_id, lnum, buf,0 , l, +UBI_DATA_UNKNOWN ); + }else { +/* +* When writing to static volumes, and this is the last logical +* eraseblock, the length (@len) does not have to be aligned to +* the minimal flash I/O unit. The 'ubi_eba_write_leb_st()' +* function needs the exact (unaligned) length to store in the +* VID header. And it will take care of proper alignment by +* padding the buffer. Here we just make sure the padding will +* contain zeros, not random trash. +*/ +memset (buf + len,0 , vtr->usable_leb_size - len); + err =ubi_eba_write_leb_st (ubi, vol_id, lnum, buf, len, +UBI_DATA_UNKNOWN , used_ebs); + } + +return err; +}
PATCH 31/44 accounting unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/account.h +118 -0
@@ -0,0 +1,118 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* UBI accounting unit. +* +* This unit is responsible for maintaining the correct physical eraseblock +* accounting to prevent overcommitment. +*/ + +#ifndef __UBI_ACCOUNT_H__ +#define __UBI_ACCOUNT_H__ + +#include <linux/spinlock.h> + +struct ubi_info ; +struct ubi_scan_info ; + +/** +* ubi_acc_mkvol - account creation of a volume. +* +* @ubi: the UBI device description object +* @reserved_pebs: how many eraseblocks are reserved for the volume +* +* This function reserves @reserved_pebs physical eraseblocks for the newly +* created volume. Returns zero in case of success and a %-ENOSPC if there are +* no enough physical eraseblocks. +*/ +int ubi_acc_mkvol (const struct ubi_info *ubi,int reserved_pebs); + +/** +* ubi_acc_rmvol - account removal of a volume. +* +* @ubi: the UBI device description object +* @reserved_pebs: how many eraseblocks were reserved for the volume +* +* This function reclaims the physical eraseblocks occupied by a volume. Note, +* UBI is trying to maintain a constant level of physical eraseblock reserved +* for bad PEB handling. So, if there is a lack of reserved physical +* eraseblock, this function will reserve them at once. +*/ +void ubi_acc_rmvol (const struct ubi_info *ubi,int reserved_pebs); + +/** +* ubi_acc_reserve - reserve a number of physical eraseblocks. +* +* @ubi: the UBI device description object +* @pebs: how many physical eraseblocks to reserve +* +* This function returns zero in case of success and %-ENOSPC if there are no +* enough physical eraseblocks. +*/ +int ubi_acc_reserve (const struct ubi_info *ubi,int pebs); + +/** +* ubi_acc_free - free a number of reserved physical eraseblocks. +* +* @ubi: the UBI device description object +* @pebs: how many physical eraseblocks to free +*/ +void ubi_acc_free (const struct ubi_info *ubi,int pebs); + +/** +* ubi_acc_init_scan - initialize the accounting unit using scanning +* information. +* +* @ubi: the UBI device description object +* @si: a pointer to the scanning information +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_acc_init_scan (struct ubi_info *ubi,struct ubi_scan_info *si); + +/** +* ubi_acc_close - close the accounting unit. +* +* @ubi: the UBI device description object +*/ +void ubi_acc_close (const struct ubi_info *ubi); + +/** +* struct ubi_acc_info - the UBI accounting unit's description data structure. +* +* @ivol_count: count of internal volumes +* @uvol_count: count of user volumes +* @rsvd_pebs: count of reserved physical eraseblocks +* @avail_pebs: count of available physical eraseblocks +* @max_volumes: maximum number of volumes that users may create +* @lock: protects the accounting data +*/ +struct ubi_acc_info { +int ivol_count;/* public */ +int uvol_count;/* public */ +int rsvd_pebs;/* public */ +int avail_pebs;/* public */ +int max_volumes;/* public */ +spinlock_t lock;/* private */ +}; + +#endif /* !__UBI_ACCOUNT_H__ */
PATCH 32/44 accounting unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/account.c +286 -0
@@ -0,0 +1,286 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +#include <linux/sched.h> +#include <linux/string.h> +#include <linux/spinlock.h> +#include <linux/err.h> +#include <linux/types.h> +#include <mtd/ubi-header.h> +#include "ubi.h" +#include "vtbl.h" +#include "account.h" +#include "scan.h" +#include "alloc.h" +#include "badeb.h" +#include "io.h" +#include "debug.h" + +int ubi_acc_mkvol (const struct ubi_info *ubi,int reserved_pebs) +{ +struct ubi_acc_info *acc = ubi->acc; + +dbg_acc ("reserve %d PEBs for a new volume (uvol_count %d" +"rsvd_pebs %d, avail_pebs %d)" , reserved_pebs, + acc->uvol_count, acc->rsvd_pebs, acc->avail_pebs); +ubi_assert (reserved_pebs >0 ); + +spin_lock (&acc->lock); +if (acc->uvol_count +1 > acc->max_volumes) { +dbg_err ("no room for the volume" ); + goto out; + } +if (reserved_pebs > acc->avail_pebs) { +dbg_err ("no enough PEBs" ); + goto out; + } + acc->uvol_count +=1 ; + acc->avail_pebs -= reserved_pebs; + acc->rsvd_pebs += reserved_pebs; +ubi_assert (acc->avail_pebs >=0 ); +spin_unlock (&acc->lock); +return 0 ; + +out: +spin_unlock (&acc->lock); +return -ENOSPC ; +} + +void ubi_acc_rmvol (const struct ubi_info *ubi,int reserved_pebs) +{ +struct ubi_acc_info *acc = ubi->acc; + +dbg_acc ("remove volume and get back %d PEBs (uvol_count %d, " +"rsvd_pebs %d, avail_pebs %d)" , reserved_pebs, + acc->uvol_count, acc->rsvd_pebs, acc->avail_pebs); +ubi_assert (reserved_pebs >0 && reserved_pebs <= acc->rsvd_pebs); + +spin_lock (&acc->lock); + acc->uvol_count -=1 ; + acc->avail_pebs += reserved_pebs; + acc->rsvd_pebs -= reserved_pebs; +ubi_assert (acc->uvol_count >=0 ); +ubi_assert (acc->rsvd_pebs >=0 ); +spin_unlock (&acc->lock); + +/* Take care about PEBs reserved for bad PEB handling */ +ubi_beb_maintain_reserved (ubi); +} + +int ubi_acc_reserve (const struct ubi_info *ubi,int pebs) +{ +struct ubi_acc_info *acc = ubi->acc; + +dbg_acc ("reserve %d PEBs (rsvd_pebs %d, avail_pebs %d)" , + pebs, acc->rsvd_pebs, acc->avail_pebs); +ubi_assert (pebs >0 ); + +spin_lock (&acc->lock); +if (unlikely (pebs > acc->avail_pebs)) { +dbg_err ("no enough PEBs" ); +spin_unlock (&acc->lock); +return -ENOSPC ; + } + acc->avail_pebs -= pebs; + acc->rsvd_pebs += pebs; +spin_unlock (&acc->lock); +return 0 ; +} + +void ubi_acc_free (const struct ubi_info *ubi,int pebs) +{ +struct ubi_acc_info *acc = ubi->acc; + +dbg_acc ("free %d PEBs (rsvd_pebs %d, avail_pebs %d)" , + pebs, acc->rsvd_pebs, acc->avail_pebs); +spin_lock (&acc->lock); +ubi_assert (pebs >0 && pebs <= acc->rsvd_pebs); + acc->rsvd_pebs -= pebs; + acc->avail_pebs += pebs; +spin_unlock (&acc->lock); +} + +static int acc_info_check (const struct ubi_info *ubi, +const struct ubi_scan_info *si); + +int ubi_acc_init_scan (struct ubi_info *ubi,struct ubi_scan_info *si) +{ +int err, i; +struct ubi_acc_info *acc; +const struct ubi_vtbl_vtr *vtr; +const struct ubi_vtbl_info *vtbl = ubi->vtbl; +const struct ubi_io_info *io = ubi->io; + +dbg_acc ("initialize the accounting unit" ); + + acc =ubi_kzalloc (sizeof (struct ubi_acc_info )); +if (!acc) +return -ENOMEM ; + ubi->acc = acc; + +spin_lock_init (&acc->lock); + acc->ivol_count =UBI_INT_VOL_COUNT ; + +for (i =0 ; i < acc->ivol_count; i++) { +cond_resched (); + vtr =ubi_vtbl_get_vtr (ubi,UBI_INTERNAL_VOL_START + i); +ubi_assert (!IS_ERR (vtr)); + acc->rsvd_pebs += vtr->reserved_pebs; + } + +/* +* The maximum number of volumes may be less then the volume table +* fits if there are too few available eraseblocks on the flash. +*/ + acc->max_volumes = vtbl->vt_slots; + i = io->good_peb_count - acc->rsvd_pebs; +if (i <=0 ) { +ubi_err ("too small flash, at least %d good physical eraseblock" +" needed" , acc->rsvd_pebs +1 ); + err = -EINVAL ; + goto out_acc; + } + +if (acc->max_volumes > i) + acc->max_volumes = i; + +for (i =0 ; i < acc->max_volumes; i++) { +cond_resched (); + vtr =ubi_vtbl_get_vtr (ubi, i); +if (IS_ERR (vtr)) +continue ; + acc->uvol_count +=1 ; + acc->rsvd_pebs += vtr->reserved_pebs; + } + + acc->rsvd_pebs += si->alien_peb_count; + acc->avail_pebs = io->good_peb_count - acc->rsvd_pebs; + +/* Check accounting information sanity and consistency */ + err =acc_info_check (ubi, si); +if (err) + goto out_acc; + +dbg_acc ("uvol_count %d, ivol_count %d, avail_pebs %d rsvd_pebs %d " +"max_volumes %d" , acc->uvol_count, acc->ivol_count, + acc->avail_pebs, acc->rsvd_pebs, acc->max_volumes); +return 0 ; + +out_acc: +ubi_kfree (acc); +return err; +} + +void ubi_acc_close (const struct ubi_info *ubi) +{ +dbg_acc ("close the accounting unit" ); +ubi_kfree (ubi->acc); +} + +/** +* acc_info_check - check sanity and consistency of accounting information. +* +* @ubi: the UBI device description object +* @si: a pointer to the scanning information which must be consistent to accounting +* information +* +* As we try not to trust the data we read from the flash media, we have to +* check that the accounting information is sane and consistent, as it is +* formed using on-flash information. This function returns zero if all is fine +* and a negative error code if some inconsistency was found. +*/ +static int acc_info_check (const struct ubi_info *ubi, +const struct ubi_scan_info *si) +{ +int i; +const struct ubi_vtbl_vtr *vtr; +const struct ubi_acc_info *acc = ubi->acc; +const struct ubi_vtbl_info *vtbl = ubi->vtbl; +const struct ubi_io_info *io = ubi->io; + +if (acc->avail_pebs <0 || acc->rsvd_pebs <0 || acc->uvol_count <0 || + acc->ivol_count <0 ) { +dbg_err ("negative values" ); + goto bad; + } + +if (acc->avail_pebs > io->good_peb_count) { +dbg_err ("bad avail_pebs" ); + goto bad; + } + +if (acc->rsvd_pebs > io->good_peb_count) { +dbg_err ("bad rsvd_pebs" ); + goto bad; + } + +if (acc->avail_pebs + acc->rsvd_pebs != io->good_peb_count) { +dbg_err ("accounting error" ); + goto bad; + } + +if (acc->max_volumes > vtbl->vt_slots) { +dbg_err ("bad max_volumes" ); + goto bad; + } + +if (acc->ivol_count + acc->uvol_count > acc->max_volumes) { +dbg_err ("vol. count (%d + %d) > max_volumes" , + acc->ivol_count, acc->uvol_count); + goto bad; + } + +/* +* Ensure that there are no volumes which exceed acc->max_volumes +* exist. +*/ +for (i = acc->max_volumes; i < vtbl->vt_slots; i++) { +cond_resched (); + vtr =ubi_vtbl_get_vtr (ubi, i); +if (unlikely (!IS_ERR (vtr))) { +dbg_err ("volume %d exists" , i); + goto bad; + } + } + +if (si->vols_found > acc->ivol_count + acc->uvol_count) { +dbg_err ("scanning found volumes %d > %d + %d" , + si->vols_found, acc->ivol_count, acc->uvol_count); + goto bad; + } + +if (si->highest_vol_id >= acc->max_volumes && + si->highest_vol_id <UBI_INTERNAL_VOL_START ) { +dbg_err ("too large volume ID %d found by scanning" , + si->highest_vol_id); + goto bad; + } + +return 0 ; + +bad: +ubi_err ("accounting check failed" ); +dbg_err ("uvol_count %d, ivol_count %d, avail_pebs %d, rsvd_pebs %d " +"io->good_peb_count %d, max_volumes %d, vtbl->vt_slots %d" , + acc->uvol_count, acc->ivol_count, acc->avail_pebs, + acc->rsvd_pebs, io->good_peb_count, acc->max_volumes, + vtbl->vt_slots); +return -EINVAL ; +}
PATCH 33/44 volume management unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/volmgmt.h +129 -0
@@ -0,0 +1,129 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* UBI volume management unit. +* +* The unit is responsible for creation, deletion, updating and resizing +* of volumes. +*/ + +#ifndef __UBI_VOLMGMT_H__ +#define __UBI_VOLMGMT_H__ + +#include <linux/mutex.h> + +struct ubi_info ; +struct ubi_scan_info ; +struct ubi_vol_info ; +struct ubi_vtbl_vtr ; + +/** +* ubi_vmt_get_data_info - get volume data description. +* +* @ubi: the UBI device description object +* @vol_id: ID of the requested volume +* +* This function returns a pointer to the corresponding data description +* object. +*/ +const struct ubi_vmt_data_info * +ubi_vmt_get_data_info (const struct ubi_info *ubi,int vol_id); + +/** +* ubi_vmt_mkvol - create a volume. +* +* @ubi: the UBI device description object +* @vol_id: ID to assign to the new volume +* @vtr: volume table record corresponding to the new volume +* +* If @vol_id id %UBI_VOL_NUM_AUTO then new volume is automatically given an +* unused volume identifier. The @vtr->usable_leb_size field is ignored. +* +* This function returns the ID of the newly created volume in case of success, +* and a negative error code in case of failure. +*/ +int ubi_vmt_mkvol (const struct ubi_info *ubi,int vol_id, +const struct ubi_vtbl_vtr *vtr); + +/** +* ubi_vmt_rmvol - remove a volume. +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume to remove +* +* This function returns zero in case of success, and a negative error code in +* case of failure. +*/ +int ubi_vmt_rmvol (const struct ubi_info *ubi,int vol_id); + +/** +* ubi_vmt_rsvol - re-size a volume. +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume to re-size +* @reserved_pebs: new volume size +* +* This function returns zero in case of success, and a negative error code in +* case of failure. +*/ +int ubi_vmt_rsvol (const struct ubi_info *ubi,int vol_id,int reserved_pebs); + +/** +* ubi_vmt_truncate_volume - make sure the volume contains only 0xFF bytes. +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume to free +* +* This function erases all the volume's eraseblocks. Returns zero in case of +* success, and a negative error code in case of failure. +*/ +int ubi_vmt_truncate_volume (const struct ubi_info *ubi,int vol_id); + +/** +* ubi_vmt_init_scan - initialize the volume management unit using scanning +* information. +* +* @ubi: the UBI device description object +* @si: a pointer to the scanning information +* +* This function returns zero in case of success, and a negative error code in +* case of failure. +*/ +int ubi_vmt_init_scan (struct ubi_info *ubi,struct ubi_scan_info *si); + +/** +* ubi_vmt_close - close the volume management unit. +* +* @ubi: the UBI device description object +*/ +void ubi_vmt_close (const struct ubi_info *ubi); + +/** +* struct ubi_vmt_info - volume management unit description data +* structure. +* +* @mutex: a mutex to serialize volume changes +*/ +struct ubi_vmt_info { +struct mutex mutex;/* private */ +}; + +#endif /* !__UBI_VOLMGMT_H__ */
PATCH 34/44 volume management unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/volmgmt.c +374 -0
@@ -0,0 +1,374 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +#include <linux/string.h> +#include <linux/sched.h> +#include <linux/err.h> +#include <linux/mutex.h> +#include "ubi.h" +#include "alloc.h" +#include "io.h" +#include "wl.h" +#include "upd.h" +#include "volmgmt.h" +#include "vtbl.h" +#include "misc.h" +#include "eba.h" +#include "account.h" +#include "scan.h" +#include "debug.h" + +#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID_VMT +static int paranoid_check_vtr (const struct ubi_info *ubi, +const struct ubi_vtbl_vtr *vtr); +#else +#define paranoid_check_vtr (ubi, vtr) 0 +#endif + +static int find_vacant_vol_id (const struct ubi_info *ubi); + +int ubi_vmt_mkvol (const struct ubi_info *ubi,int vol_id, +const struct ubi_vtbl_vtr *vtr) +{ +int i, err =0 ; +struct ubi_vmt_info *vmt = ubi->vmt; +const struct ubi_vtbl_vtr *vtr_ck; + +dbg_vmt ("create volume ID %d, reserved_pebs %d, type %d, name %s" , + vol_id, vtr->reserved_pebs, vtr->vol_type, vtr->name); + +mutex_lock (&vmt->mutex); + +if (vol_id ==UBI_VOL_NUM_AUTO ) { + vol_id =find_vacant_vol_id (ubi); +if (vol_id <0 ) { + err = vol_id; + goto out_unlock; + } + }else +ubi_assert (vol_id >=0 && vol_id < ubi->acc->max_volumes); + + err =paranoid_check_vtr (ubi, vtr); +if (err) { + err = -EINVAL ; + goto out_unlock; + } + +/* Get sure that this volume does not exist */ + err = -EEXIST ; + vtr_ck =ubi_vtbl_get_vtr (ubi, vol_id); +if (!IS_ERR (vtr_ck)) { +dbg_err ("volume %d already exists" , vol_id); + goto out_unlock; + } + +/* Ensure that this volume has a unique name */ +for (i =0 ; i < ubi->acc->max_volumes; i++) { +cond_resched (); + + vtr_ck =ubi_vtbl_get_vtr (ubi, i); +if (IS_ERR (vtr_ck)) +continue ; + +if (unlikely (vtr->name_len == vtr_ck->name_len && + !strcmp (vtr->name, vtr_ck->name))) { +dbg_err ("not unique name \"%s\", volume %d has it" , + vtr->name, i); + goto out_unlock; + } + } + + err =ubi_acc_mkvol (ubi, vtr->reserved_pebs); +if (err) + goto out_unlock; + +/* +* Finish all the pending erases because there may be some LEBs +* belonging to the same volume ID. We don't want to be messed-up. +*/ + err =ubi_wl_flush (ubi); +if (err) + goto out_acc; + + err =ubi_eba_mkvol (ubi, vol_id, vtr->reserved_pebs); +if (err) + goto out_acc; + + err =ubi_vtbl_mkvol (ubi, vol_id, vtr); +if (err) + goto out_eba; + +mutex_unlock (&vmt->mutex); +return vol_id; + +out_eba: +ubi_eba_rmvol (ubi, vol_id); +out_acc: +ubi_acc_rmvol (ubi, vtr->reserved_pebs); +out_unlock: +mutex_unlock (&vmt->mutex); +return err; +} + +int ubi_vmt_rmvol (const struct ubi_info *ubi,int vol_id) +{ +int err, reserved_pebs; +const struct ubi_vtbl_vtr *vtr; +struct ubi_vmt_info *vmt = ubi->vmt; + +dbg_vmt ("remove volume %d" , vol_id); +ubi_assert (vol_id >=0 && vol_id < ubi->acc->max_volumes); + +mutex_lock (&vmt->mutex); + +/* Ensure that this volume exists */ + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +if (IS_ERR (vtr)) { + err =PTR_ERR (vtr); + goto out_unlock; + } + + reserved_pebs = vtr->reserved_pebs; + + err =ubi_vtbl_rmvol (ubi, vol_id); +if (err) + goto out_unlock; + + err =ubi_eba_rmvol (ubi, vol_id); +if (err) + goto out_unlock; + +ubi_acc_rmvol (ubi, reserved_pebs); + +out_unlock: +mutex_unlock (&vmt->mutex); +return err; +} + +int ubi_vmt_rsvol (const struct ubi_info *ubi,int vol_id,int reserved_pebs) +{ +int err, pebs, old_reserved_pebs; +struct ubi_vmt_info *vmt = ubi->vmt; +const struct ubi_vtbl_vtr *vtr; + +dbg_vmt ("re-size volume %d to %d PEBs" , vol_id, reserved_pebs); +ubi_assert (vol_id >=0 && vol_id < ubi->acc->max_volumes); +ubi_assert (reserved_pebs >0 ); + +mutex_lock (&vmt->mutex); + +/* Ensure that this volume exists */ + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +if (IS_ERR (vtr)) { + err =PTR_ERR (vtr); + goto out_unlock; + } + +if (vtr->vol_type ==UBI_STATIC_VOLUME && + reserved_pebs < vtr->used_ebs) { +dbg_err ("too small size %d, static volume %d has %d used LEBs" , + reserved_pebs, vol_id, vtr->used_ebs); + err = -EINVAL ; + goto out_unlock; + } + +/* If the size is the same, we have nathing to do */ +if (reserved_pebs == vtr->reserved_pebs) { + err =0 ; + goto out_unlock; + } + + old_reserved_pebs = vtr->reserved_pebs; + + err =ubi_vtbl_rsvol (ubi, vol_id, reserved_pebs); +if (err) + goto out_unlock; + + pebs = reserved_pebs - old_reserved_pebs; +if (pebs >0 ) { + err =ubi_acc_reserve (ubi, pebs); +if (err) + goto out_unlock; + }else +ubi_acc_free (ubi, -pebs); + + err =ubi_eba_rsvol (ubi, vol_id, reserved_pebs); +if (err) + goto out_unlock; + +out_unlock: +mutex_unlock (&vmt->mutex); +return err; +} + +int ubi_vmt_init_scan (struct ubi_info *ubi,struct ubi_scan_info *si) +{ +int err; +struct ubi_vmt_info *vmt; + +dbg_vmt ("initialize the volume management unit" ); + + vmt =ubi_kzalloc (sizeof (struct ubi_vmt_info )); +if (!vmt) +return -ENOMEM ; + ubi->vmt = vmt; + +mutex_init (&vmt->mutex); + + err =ubi_vtbl_init_scan (ubi, si); +if (err) + goto out_vmt; + + err =ubi_acc_init_scan (ubi, si); +if (err) + goto out_vtbl; + + err =ubi_upd_init_scan (ubi, si); +if (err) + goto out_acc; + +dbg_vmt ("the volume management unit is initialized" ); +return 0 ; + +out_acc: +ubi_acc_close (ubi); +out_vtbl: +ubi_vtbl_close (ubi); +out_vmt: +ubi_kfree (vmt); +return err; +} + +void ubi_vmt_close (const struct ubi_info *ubi) +{ +dbg_vmt ("close the volume management unit" ); +ubi_upd_close (ubi); +ubi_acc_close (ubi); +ubi_vtbl_close (ubi); +ubi_kfree (ubi->vmt); +} + +/** +* find_vacant_vol_id - find an unused volume ID. +* +* @ubi: the UBI device description object +* +* This function returns a positive volume ID or %-ENOSPC if there are no free +* volume IDs. +*/ +static int find_vacant_vol_id (const struct ubi_info *ubi) +{ +int i; + +for (i =0 ; i < ubi->acc->max_volumes; i++) { +const struct ubi_vtbl_vtr *vtr; + +cond_resched (); + + vtr =ubi_vtbl_get_vtr (ubi, i); +if (IS_ERR (vtr)) { +dbg_vmt ("found volume ID %d" , i); +return i; + } + } + +dbg_vmt ("vacant volume ID not found" ); +return -ENOSPC ; +} + +#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID_VMT + +/** +* paranoid_check_vtr - check sanity of a &struct ubi_vtbl_vtr object. +* +* @ubi: the UBI device description object +* @vtr: an object to check +* +* This function returns zero if the volume table record is sane, and %1 if +* not. +*/ +static int paranoid_check_vtr (const struct ubi_info *ubi, +const struct ubi_vtbl_vtr *vtr) +{ +int n; +const struct ubi_io_info *io = ubi->io; + +if (vtr->reserved_pebs ==0 ) +return 0 ; + +if (unlikely (vtr->reserved_pebs <0 || vtr->alignment <0 || + vtr->data_pad <0 || vtr->name_len <0 )) { +ubi_err ("negative values" ); + goto bad; + } + +if (unlikely (vtr->alignment > io->leb_size)) { +ubi_err ("too large alignment" ); + goto bad; + } + +if (unlikely (vtr->alignment ==0 )) { +ubi_err ("zero alignment" ); + goto bad; + } + + n = vtr->alignment % io->min_io_size; +if (vtr->alignment !=1 &&unlikely (n)) { +ubi_err ("alignment is not multiple of min I/O unit size" ); + goto bad; + } + + n = io->leb_size % vtr->alignment; +if (unlikely (vtr->data_pad != n)) { +ubi_err ("bad data_pad, has to be %d" , n); + goto bad; + } + +if (unlikely (vtr->vol_type !=UBI_DYNAMIC_VOLUME && + vtr->vol_type !=UBI_STATIC_VOLUME )) { +ubi_err ("bad vol_type" ); + goto bad; + } + +if (unlikely (vtr->name_len >UBI_VOL_NAME_MAX )) { +ubi_err ("too long volume name, max is %d" ,UBI_VOL_NAME_MAX ); + goto bad; + } + +if (unlikely (!vtr->name)) { +ubi_err ("NULL volume name" ); + goto bad; + } + + n =strnlen (vtr->name, vtr->name_len +1 ); +if (unlikely (n != vtr->name_len)) { +ubi_err ("bad name_len" ); + goto bad; + } + +return 0 ; + +bad: +ubi_err ("volume table record paranoid check failed" ); +ubi_dbg_dump_vtr (vtr); +return 1 ; +} + +#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID_VMT */
PATCH 35/44 user-interfaces unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/uif.h +182 -0
@@ -0,0 +1,182 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* UBI user interface unit. +* +* This unit implements all the UBI user interfaces: kernel interfaces, +* character device interfaces, and sysfs interfaces. +* +* There are two kinds of character devices: UBI character devices and volume +* character devices. UBI character devices allow users to manipulate by whole +* volumes: create, remove, and resize them. Volume character devices provide +* volume read and update capabilities. +* +* Major and minor numbers are assigned dynamically to both UBI and volume +* character devices. +*/ + +#ifndef __UBI_UIF_H__ +#define __UBI_UIF_H__ + +#include <linux/init.h> +#include <linux/spinlock.h> +#include <linux/fs.h> +#include <linux/cdev.h> +#include <linux/list.h> +#include <linux/device.h> +#include <linux/mutex.h> +#include "gluebi.h" + +#define UBI_NAME_STR "ubi" + +struct ubi_info ; +struct ubi_vol_desc ; + +/** +* ubi_uif_mkvol - create a volume. +* +* @ubi: the UBI device description object +* @vol_id: ID of the new volume +* +* This functions creates all the user interface-related data structures of a +* new volume. Returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_uif_mkvol (const struct ubi_info *ubi,int vol_id); + +/** +* ubi_uif_close_and_rmvol - close a and remove a volume. +* +* @desc: volume descriptor +* +* This functions closes a volume and removes all the user interface-related +* data structures of this volume. Returns zero in case of success and a +* negative error code in case of failure. +*/ +int ubi_uif_close_and_rmvol (struct ubi_vol_desc *desc); + +/** +* ubi_uif_init - initialize the UBI user interface unit for an UBI device. +* +* @ubi: the UBI device description object +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_uif_init (struct ubi_info *ubi); + +/** +* ubi_uif_close - close the UBI user interface unit for an UBI device. +* +* @ubi: the UBI device description object +*/ +void ubi_uif_close (const struct ubi_info *ubi); + +/** +* ubi_uif_global_init - initialize the UBI user interface unit. +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int __init ubi_uif_global_init (void ); + +/** +* ubi_uif_global_close - close the UBI user interface unit. +*/ +void ubi_uif_global_close (void ); + +/** +* struct ubi_uif_volume - a per-volume user interface data structure. +* +* @dev: a class device object to make use of the the Linux device model +* @cdev: a Linux character device object to create a character device of this +* volume +* @ubi: a reference to the UBI description object this volume belongs to +* @vol_id: volume ID +* @list: the link in the list of UIF volume information +* @readers: number of users who are using this volume in read-only mode +* @writers: number of users who are using this volume in read-write mode +* @exclusive: whether somebody is using this volume in exclusive mode +* @removed: if the volume was removed from the UBI device +* @checked: if this static volume was checked +* @vol_lock: protects the @readers, @writers, @exclusive, and @removed fields +* @updating: whether the volume is being updated +* @gluebi_vol: gluebi-specific information +*/ +struct ubi_uif_volume { +struct class_device dev; +struct cdev cdev; +const struct ubi_info *ubi; +int vol_id; +struct list_head list; +int readers; +int writers; +int exclusive; +int removed; +int checked; +spinlock_t vol_lock; +int updating; +struct ubi_gluebi_volume gluebi_vol; +}; + +/** +* struct ubi_vol_desc - UBI opened volume descriptor +* +* @vol: reference to the corresponding volume description object +* @mode: volume open mode +*/ +struct ubi_vol_desc { +struct ubi_uif_volume *vol; +enum ubi_open_mode mode; +}; + +/** +* struct ubi_uif_info - UBI user interfaces unit description structure. +* +* @cdev: a Linux character device object to create a character device of this +* UBI device +* @dev: the class device structure to use the the Linux device model +* @ubi: a reference to the UBI description structure this volume belongs to +* @major: major number of the UBI character device +* @ubi_name: name of this UBI device +* @volumes: a list of 'struct ubi_uif_volume' object for all existing volumes +* @volumes_list_lock: protects the the @volumes list +* @vol_check: serializes volume checking +* +* The @volumes_list_lock mutex protects the list of volumes of this UBI device +* from being changed. So it has to be locked when the @volumes list is being +* accessed. +* +* The @vol_check lock is used only when checking static volumes consistency to +* prevent from simultanious volume checks. +*/ +struct ubi_uif_info { +struct cdev cdev; +struct class_device dev; +struct ubi_info *ubi; +int major; +char *ubi_name; +struct list_head volumes; +struct mutex volumes_list_lock; +struct mutex vol_check; +}; + +#endif /* !__UBI_UIF_H__ */
PATCH 36/44 user-interfaces unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/uif.c +786 -0
@@ -0,0 +1,786 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +#include <linux/init.h> +#include <linux/module.h> +#include <linux/spinlock.h> +#include <linux/sched.h> +#include <linux/err.h> +#include <linux/list.h> +#include <linux/mutex.h> +#include <linux/types.h> +#include <asm/div64.h> +#include <mtd/ubi-header.h> +#include "ubi.h" +#include "misc.h" +#include "sysfs.h" +#include "upd.h" +#include "wl.h" +#include "uif.h" +#include "cdev.h" +#include "alloc.h" +#include "debug.h" +#include "vtbl.h" +#include "volmgmt.h" +#include "io.h" +#include "eba.h" +#include "account.h" + +int ubi_get_device_info (int ubi_num,struct ubi_dev_info *di) +{ +const struct ubi_info *ubi; +int err = -ENODEV ; + +if (!try_module_get (THIS_MODULE )) +return err; + +if (ubi_num <0 || ubi_num >=UBI_MAX_INSTANCES ) { +dbg_err ("bad UBI device number %d" , ubi_num); + goto out_put; + } + +if (!ubis[ubi_num]) { +dbg_err ("UBI device %d does not exist" , ubi_num); + goto out_put; + } + ubi = ubis[ubi_num]; + + di->ubi_num = ubi->ubi_num; + di->leb_size = ubi->io->leb_size; + di->min_io_size = ubi->io->min_io_size; + di->ro_mode = ubi->io->ro_mode; + di->cdev =MKDEV (ubi->uif->major,0 ); + err =0 ; + +out_put: +module_put (THIS_MODULE ); +return err; +} +EXPORT_SYMBOL_GPL (ubi_get_device_info); + +static void fill_ubi_vol_info (const struct ubi_info *ubi,int vol_id, +struct ubi_vol_info *vi); + +void ubi_get_volume_info (struct ubi_vol_desc *udesc,struct ubi_vol_info *vi) +{ +struct ubi_vol_desc *desc = udesc; +const struct ubi_info *ubi = desc->vol->ubi; +int vol_id = desc->vol->vol_id; + +fill_ubi_vol_info (ubi, vol_id, vi); +} +EXPORT_SYMBOL_GPL (ubi_get_volume_info); + +struct ubi_vol_desc *ubi_open_volume (int ubi_num,int vol_id, +enum ubi_open_mode mode) +{ +int err, found =0 ; +struct ubi_vol_desc *desc; +const struct ubi_info *ubi; +struct ubi_uif_info *uif; +struct ubi_uif_volume *vol; + +dbg_uif ("open device %d volume %d, mode %d" , ubi_num, vol_id, mode); + + err = -ENODEV ; +if (!try_module_get (THIS_MODULE )) +return ERR_PTR (err); + +if (ubi_num <0 || ubi_num >=UBI_MAX_INSTANCES ) { +dbg_err ("bad UBI device number %d" , ubi_num); + goto out_put; + } + +if (!ubis[ubi_num]) { +dbg_err ("UBI device %d does not exist" , ubi_num); + goto out_put; + } + + ubi = ubis[ubi_num]; +ubi_assert (ubi->ubi_num == ubi_num); + uif = ubi->uif; + + desc =ubi_kzalloc (sizeof (struct ubi_vol_desc )); +if (!desc) { + err = -ENOMEM ; + goto out_put; + } + + err = -EINVAL ; +if (vol_id <0 || vol_id >= ubi->acc->max_volumes) { +dbg_err ("bad vol_id %d" , vol_id); + goto out_free; + } + +if (mode !=UBI_READONLY && mode !=UBI_READWRITE && + mode !=UBI_EXCLUSIVE ) { +dbg_err ("bad mode %d" , mode); + goto out_free; + } + +mutex_lock (&uif->volumes_list_lock); +list_for_each_entry (vol, &uif->volumes, list) +if (vol->vol_id == vol_id) { + found =1 ; +break ; + } +if (!found) { +dbg_err ("volume %d does not exist" , vol_id); + err = -ENODEV ; + goto out_unlock; + } + + err = -EBUSY ; +spin_lock (&vol->vol_lock); +if (vol->updating) { +/* If the volume is being updated, no one can open it */ +dbg_err ("device busy - updating" ); + goto out_unlock_vol; + } + +switch (mode) { +case UBI_READONLY : +if (vol->exclusive) { +dbg_err ("device is busy - exclusive" ); + goto out_unlock_vol; + } + vol->readers +=1 ; +break ; + +case UBI_READWRITE : +if (vol->exclusive || vol->writers >0 ) { +if (vol->exclusive) +dbg_err ("device is busy - exclusive" ); +else +dbg_err ("device is busy - writers" ); + goto out_unlock_vol; + } + vol->writers +=1 ; +break ; + +case UBI_EXCLUSIVE : +if (vol->exclusive || vol->writers || vol->readers) { +if (vol->exclusive) +dbg_err ("device is busy - exclusive" ); +else if (vol->writers) +dbg_err ("device is busy - writers" ); +else +dbg_err ("device is busy - readers" ); + goto out_unlock_vol; + } + vol->exclusive =1 ; +break ; + } +spin_unlock (&vol->vol_lock); +mutex_unlock (&uif->volumes_list_lock); + + desc->vol = vol; + desc->mode = mode; + +mutex_lock (&uif->vol_check); +if (!vol->checked) { +/* +* This is the first time this volume is being opened, we have +* to check it. If the volume is corrupted, we still return +* success. +*/ + err =ubi_check_volume (ubi, vol_id); +if (err <0 ) + goto unlock_close; + +if (err ==1 ) { +ubi_warn ("volume %d on UBI device %d is corrupted" , + vol_id, ubi->ubi_num); + err =ubi_vtbl_set_corrupted (ubi, vol_id); +if (err) + goto unlock_close; + } + vol->checked =1 ; + } +mutex_unlock (&uif->vol_check); + +return desc; + +out_unlock_vol: +spin_unlock (&vol->vol_lock); +out_unlock: +mutex_unlock (&uif->volumes_list_lock); +out_free: +ubi_kfree (desc); +out_put: +module_put (THIS_MODULE ); +return ERR_PTR (err); + +unlock_close: +mutex_unlock (&uif->vol_check); +ubi_close_volume (desc); +return ERR_PTR (err); +} +EXPORT_SYMBOL_GPL (ubi_open_volume); + +struct ubi_vol_desc *ubi_open_volume_nm (int ubi_num,const char *name, +enum ubi_open_mode mode) +{ +int err, vol_id =-1 , len; +struct ubi_vol_desc *ret; +const struct ubi_info *ubi; +struct ubi_uif_info *uif; +struct ubi_uif_volume *vol; + +dbg_uif ("open volume by name %s, mode %d" , name, mode); + +if (!name) { +dbg_err ("bad name" ); +return ERR_PTR (-EINVAL ); + } + len =strnlen (name,UBI_VOL_NAME_MAX +1 ); +if (len >UBI_VOL_NAME_MAX ) { +dbg_err ("bad name" ); +return ERR_PTR (-EINVAL ); + } + + err = -ENODEV ; +if (!try_module_get (THIS_MODULE )) +return ERR_PTR (err); + +if (ubi_num <0 || ubi_num >=UBI_MAX_INSTANCES ) { +dbg_err ("bad UBI device number %d" , ubi_num); + goto out_put; + } + +if (!ubis[ubi_num]) { +dbg_err ("UBI device %d does not exist" , ubi_num); + goto out_put; + } + + ubi = ubis[ubi_num]; +ubi_assert (ubi->ubi_num == ubi_num); + uif = ubi->uif; + +/* Walk all volumes of this UBI device */ +mutex_lock (&uif->volumes_list_lock); +list_for_each_entry (vol, &uif->volumes, list) { +const struct ubi_vtbl_vtr *vtr; + +spin_lock (&vol->vol_lock); + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); +if (len == vtr->name_len && !strcmp (name, vtr->name)) { + vol_id = vol->vol_id; +dbg_err ("found volume volume %d" , vol_id); +spin_unlock (&vol->vol_lock); +break ; + } +spin_unlock (&vol->vol_lock); + } +mutex_unlock (&uif->volumes_list_lock); + +if (vol_id <0 ) { +dbg_err ("volume %s does not exist" , name); + goto out_put; + } + + ret =ubi_open_volume (ubi_num, vol_id, mode); +if (!IS_ERR (ret)) +return ret; + + err =PTR_ERR (ret); + +out_put: +module_put (THIS_MODULE ); +return ERR_PTR (err); +} +EXPORT_SYMBOL_GPL (ubi_open_volume_nm); + +void ubi_close_volume (struct ubi_vol_desc *udesc) +{ +struct ubi_vol_desc *desc = udesc; +struct ubi_uif_volume *vol = desc->vol; + +dbg_uif ("close volume %d, mode %d" , vol->vol_id, desc->mode); + +spin_lock (&vol->vol_lock); +switch (desc->mode) { +case UBI_READONLY : +ubi_assert (vol->readers >0 ); + vol->readers -=1 ; +break ; + +case UBI_READWRITE : +ubi_assert (vol->writers >0 ); + vol->writers -=1 ; +break ; + +case UBI_EXCLUSIVE : +ubi_assert (vol->exclusive >0 ); + vol->exclusive =0 ; + } +spin_unlock (&vol->vol_lock); + +ubi_kfree (desc); +module_put (THIS_MODULE ); +} +EXPORT_SYMBOL_GPL (ubi_close_volume); + +int ubi_eraseblock_read (struct ubi_vol_desc *udesc,int lnum,char *buf, +int offset,int len,int check) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = udesc; +const struct ubi_info *ubi = desc->vol->ubi; +int err, vol_id = desc->vol->vol_id; + +dbg_uif ("read %d bytes from offset %d of LEB %d:%d" , + len, offset, vol_id, lnum); + +if (unlikely (vol_id <0 || vol_id >= ubi->acc->max_volumes)) { +dbg_err ("bad vol_id %d" , vol_id); +return -EINVAL ; + } + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (unlikely (lnum <0 || lnum >= vtr->used_ebs)) { +dbg_err ("bad lnum %d" , lnum); +return -EINVAL ; + } + +if (unlikely (offset <0 || len <0 || + offset + len > vtr->usable_leb_size)) { +dbg_err ("bad offset %d or len %d" , offset, len); +return -EINVAL ; + } + +if (unlikely (vtr->vol_type ==UBI_STATIC_VOLUME && + lnum == vtr->used_ebs -1 && + offset + len > vtr->last_eb_bytes)) { +dbg_err ("bad offset %d or len %d for last LEB" , offset, len); +return -EINVAL ; + } + +if (unlikely (len ==0 )) +return 0 ; + +if (unlikely (vtr->upd_marker)) { +dbg_err ("reading from update-interrupted volume" ); +return -EBADF ; + } + + err =ubi_eba_read_leb (ubi, vol_id, lnum, buf, offset, len, check); +if (unlikely (err)) +return err; +if (unlikely (vtr->corrupted)) { +ubi_assert (vtr->vol_type ==UBI_STATIC_VOLUME ); +dbg_err ("corrupted volume is read" ); + err = -EUCLEAN ; + } +return err; +} +EXPORT_SYMBOL_GPL (ubi_eraseblock_read); + +int ubi_eraseblock_write (struct ubi_vol_desc *udesc,int lnum,const void *buf, +int offset,int len,enum ubi_data_type dtype) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = udesc; +const struct ubi_info *ubi = desc->vol->ubi; +const struct ubi_io_info *io = ubi->io; +int vol_id = desc->vol->vol_id; + +dbg_uif ("write %zd bytes at offset %d of LEB %d:%d" , + len, offset, vol_id, lnum); + +if (unlikely (vol_id <0 || vol_id >= ubi->acc->max_volumes)) { +dbg_err ("bad vol_id %d" , vol_id); +return -EINVAL ; + } + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (unlikely (desc->mode ==UBI_READONLY )) { +dbg_err ("read-only mode" ); +return -EROFS ; + } + +if (unlikely (vtr->vol_type ==UBI_STATIC_VOLUME )) { +dbg_err ("static volume" ); +return -EROFS ; + } + +if (unlikely (lnum <0 || lnum >= vtr->reserved_pebs)) { +dbg_err ("bad lnum %d" , lnum); +return -EINVAL ; + } + +if (unlikely (offset <0 || len <0 || + offset + len > vtr->usable_leb_size)) { +dbg_err ("bad offset %d or len %zd" , offset, len); +return -EINVAL ; + } + +if (unlikely (offset % io->min_io_size || len % io->min_io_size)) { +dbg_err ("unaligned offset %d or len %zd" , offset, len); +return -EINVAL ; + } + +if (unlikely (dtype !=UBI_DATA_LONGTERM && + dtype !=UBI_DATA_SHORTTERM && + dtype !=UBI_DATA_UNKNOWN )) { +dbg_err ("bad dtype %d" , dtype); +return -EINVAL ; + } + +if (unlikely (len ==0 )) +return 0 ; + +if (unlikely (vtr->upd_marker)) { +dbg_err ("writing update-interrupted volume" ); +return -EBADF ; + } + +return ubi_eba_write_leb (ubi, vol_id, lnum, buf, offset, len, dtype); +} +EXPORT_SYMBOL_GPL (ubi_eraseblock_write); + +int ubi_eraseblock_erase (struct ubi_vol_desc *udesc,int lnum) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = udesc; +const struct ubi_info *ubi = desc->vol->ubi; +int vol_id = desc->vol->vol_id; + +dbg_uif ("erase LEB %d:%d" , vol_id, lnum); + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (unlikely (desc->mode ==UBI_READONLY )) { +dbg_err ("read-only mode" ); +return -EROFS ; + } + +if (unlikely (vtr->vol_type ==UBI_STATIC_VOLUME )) { +dbg_err ("static volume" ); +return -EROFS ; + } + +if (unlikely (lnum <0 || lnum >= vtr->reserved_pebs)) { +dbg_err ("bad lnum %d" , lnum); +return -EINVAL ; + } + +if (unlikely (vtr->upd_marker)) { +dbg_err ("erasing update-interrupted volume" ); +return -EBADF ; + } + +return ubi_eba_erase_leb (ubi, vol_id, lnum); +} +EXPORT_SYMBOL_GPL (ubi_eraseblock_erase); + +int ubi_eraseblock_unmap (struct ubi_vol_desc *udesc,int lnum) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = udesc; +const struct ubi_info *ubi = desc->vol->ubi; +int err, vol_id = desc->vol->vol_id; + +dbg_uif ("erase LEB %d:%d" , vol_id, lnum); + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (unlikely (desc->mode ==UBI_READONLY )) { +dbg_err ("read-only mode" ); +return -EROFS ; + } + +if (unlikely (vtr->vol_type ==UBI_STATIC_VOLUME )) { +dbg_err ("static volume" ); +return -EROFS ; + } + +if (unlikely (lnum <0 || lnum >= vtr->reserved_pebs)) { +dbg_err ("bad lnum %d" , lnum); +return -EINVAL ; + } + +if (unlikely (vtr->upd_marker)) { +dbg_err ("unmapping eraseblock of an update-interrupted " +"volume" ); +return -EBADF ; + } + + err =ubi_eba_erase_leb (ubi, vol_id, lnum); +if (unlikely (err)) +return err; + +return ubi_wl_flush (ubi); +} +EXPORT_SYMBOL_GPL (ubi_eraseblock_unmap); + +int ubi_eraseblock_is_mapped (struct ubi_vol_desc *udesc,int lnum) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = udesc; +const struct ubi_info *ubi = desc->vol->ubi; +int vol_id = desc->vol->vol_id; + +dbg_uif ("check LEB %d:%d" , vol_id, lnum); + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +if (unlikely (lnum <0 || lnum >= vtr->reserved_pebs)) { +dbg_err ("bad lnum %d" , lnum); +return -EINVAL ; + } + +if (unlikely (vtr->upd_marker)) { +dbg_err ("update-interrupted volume" ); +return -EBADF ; + } + +return ubi_eba_leb_is_mapped (ubi, vol_id, lnum); +} +EXPORT_SYMBOL_GPL (ubi_eraseblock_is_mapped); + +int ubi_uif_mkvol (const struct ubi_info *ubi,int vol_id) +{ +int err; +struct ubi_uif_volume *vol; +struct ubi_uif_info *uif = ubi->uif; +const struct ubi_vtbl_vtr *vtr; + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + +dbg_uif ("create volume %d, size %d, type %d" , + vol_id, vtr->reserved_pebs, vtr->vol_type); + + vol =ubi_kzalloc (sizeof (struct ubi_uif_volume )); +if (!vol) +return -ENOMEM ; + vol->ubi = ubi; + vol->vol_id = vol_id; + vol->exclusive =1 ; +spin_lock_init (&vol->vol_lock); + +mutex_lock (&uif->volumes_list_lock); +list_add (&vol->list, &uif->volumes); +mutex_unlock (&uif->volumes_list_lock); + + err =ubi_sysfs_vol_init (ubi, vol); +if (err) + goto out_sysfs; + + err =ubi_cdev_vol_init (ubi, vol); +if (err) + goto out_sysfs; + + err =ubi_gluebi_vol_init (ubi, vol); +if (err) + goto out_cdev; + +spin_lock (&vol->vol_lock); + vol->exclusive =0 ; +spin_unlock (&vol->vol_lock); + +return 0 ; + +out_cdev: +ubi_cdev_vol_close (vol); +out_sysfs: +mutex_lock (&uif->volumes_list_lock); +spin_lock (&vol->vol_lock); + vol->removed =1 ; +spin_unlock (&vol->vol_lock); +list_del (&vol->list); +mutex_unlock (&uif->volumes_list_lock); +ubi_sysfs_vol_close (vol); +return err; +} + +int ubi_uif_close_and_rmvol (struct ubi_vol_desc *desc) +{ +int err; +struct ubi_uif_volume *vol = desc->vol; +struct ubi_uif_info *uif = vol->ubi->uif; + +dbg_uif ("remove UBI volume %d" , vol->vol_id); +ubi_assert (desc->mode ==UBI_EXCLUSIVE ); + + err =ubi_gluebi_vol_close (vol); +if (err) +/* Somebody still holds the emulated MTD device */ +return err; + +spin_lock (&vol->vol_lock); + vol->removed =1 ; +spin_unlock (&vol->vol_lock); + +mutex_lock (&uif->volumes_list_lock); +list_del (&vol->list); +mutex_unlock (&uif->volumes_list_lock); + +ubi_cdev_vol_close (vol); +ubi_sysfs_vol_close (vol); +ubi_kfree (desc); +module_put (THIS_MODULE ); +return err; +} + +static void delete_volumes (const struct ubi_info *ubi); + +int ubi_uif_init (struct ubi_info *ubi) +{ +int i, err; +struct ubi_uif_info *uif; +const struct ubi_acc_info *acc = ubi->acc; + +dbg_uif ("initialize the user interface unit" ); + + uif =ubi_kzalloc (sizeof (struct ubi_uif_info )); +if (!uif) +return -ENOMEM ; + ubi->uif = uif; + uif->ubi = ubi; + +mutex_init (&uif->volumes_list_lock); +mutex_init (&uif->vol_check); +INIT_LIST_HEAD (&uif->volumes); + + uif->ubi_name =ubi_kmalloc (sizeof (UBI_NAME_STR ) +5 ); +if (!uif->ubi_name) { + err = -ENOMEM ; + goto out_uif; + } +sprintf (uif->ubi_name,UBI_NAME_STR "%d" , ubi->ubi_num); + + err =ubi_cdev_init (ubi); +if (err) + goto out_name; + + err =ubi_sysfs_init (ubi); +if (err) + goto out_cdev; + +for (i =0 ; i < acc->max_volumes; i++) { +const struct ubi_vtbl_vtr *vtr; + +cond_resched (); + + vtr =ubi_vtbl_get_vtr (ubi, i); +if (IS_ERR (vtr)) +continue ; + + err =ubi_uif_mkvol (ubi, i); +if (unlikely (err)) + goto out_volumes; + } + +dbg_uif ("the user interface unit is initialized" ); +return 0 ; + +out_volumes: +delete_volumes (ubi); +ubi_sysfs_close (ubi); + +out_cdev: +ubi_cdev_close (ubi); + +out_name: +ubi_kfree (uif->ubi_name); + +out_uif: +ubi_kfree (uif); +return err; +} + +void ubi_uif_close (const struct ubi_info *ubi) +{ +struct ubi_uif_info *uif = ubi->uif; + +dbg_uif ("close the user interface unit for %s" , uif->ubi_name); + +delete_volumes (ubi); +ubi_sysfs_close (ubi); +ubi_cdev_close (ubi); +ubi_kfree (uif->ubi_name); +ubi_kfree (uif); +} + +int __init ubi_uif_global_init (void ) +{ +return ubi_sysfs_global_init (); +} + +void ubi_uif_global_close (void ) +{ +return ubi_sysfs_global_close (); +} + +/** +* delete_volumes - delete all the volume information. +* +* @ubi: the UBI device description object +*/ +static void delete_volumes (const struct ubi_info *ubi) +{ +struct ubi_uif_volume *vol, *vol_tmp; +struct ubi_uif_info *uif = ubi->uif; + +list_for_each_entry_safe (vol, vol_tmp, &uif->volumes, list) { +list_del (&vol->list); + vol->removed =1 ; +ubi_gluebi_vol_close (vol); +ubi_cdev_vol_close (vol); +ubi_sysfs_vol_close (vol); + } +} + +/** +* fill_ubi_vol_info - fill an "user volume information data structure". +* +* @ubi: the UBI device description object +* @vol_id: ID of the volume +* @vi: the volume information to fill +* +* This function must be invoked when the @vol_id volume will for sure not go. +*/ +static void fill_ubi_vol_info (const struct ubi_info *ubi,int vol_id, +struct ubi_vol_info *vi) +{ +const struct ubi_vtbl_vtr *vtr; + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); + + vi->vol_id = vol_id; + vi->ubi_num = ubi->ubi_num; + vi->size = vtr->reserved_pebs; + vi->used_bytes = vtr->used_bytes; + vi->vol_type = vtr->vol_type; + vi->corrupted = vtr->corrupted; + vi->upd_marker = vtr->upd_marker; + vi->alignment = vtr->alignment; + vi->usable_leb_size = vtr->usable_leb_size; + vi->name_len = vtr->name_len; + vi->name = vtr->name; + vi->cdev =MKDEV (ubi->uif->major, vi->vol_id +1 ); +}
PATCH 37/44 sysfs handling unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/sysfs.h +82 -0
@@ -0,0 +1,82 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* This is a part of the UBI user interface unit and contains all the +* sysfs-related stuff. +*/ + +#ifndef __UBI_SYSFS_H__ +#define __UBI_SYSFS_H__ + +#include <linux/init.h> + +struct ubi_info ; +struct ubi_uif_volume ; + +/** +* ubi_sysfs_vol_init - initialize sysfs for an UBI volume. +* +* @ubi: the UBI device description object +* @vol: user interfaces unit volume description object +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_sysfs_vol_init (const struct ubi_info *ubi,struct ubi_uif_volume *vol); + +/** +* ubi_sysfs_vol_close - close sysfs for an UBI volume. +* +* @vol: user interfaces unit volume description object +*/ +void ubi_sysfs_vol_close (struct ubi_uif_volume *vol); + +/** +* ubi_sysfs_init - initialize sysfs for an UBI device. +* +* @ubi: the UBI device description object +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_sysfs_init (const struct ubi_info *ubi); + +/** +* ubi_sysfs_close - close sysfs for an UBI device. +* +* @ubi: the UBI device description object +*/ +void ubi_sysfs_close (const struct ubi_info *ubi); + +/** +* ubi_sysfs_global_init - initialize UBI sysfs support. +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int __init ubi_sysfs_global_init (void ); + +/** +* ubi_sysfs_global_close - close UBI sysfs support. +*/ +void ubi_sysfs_global_close (void ); + +#endif /* !__UBI_SYSFS_H__ */
PATCH 38/44 sysfs handling unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/sysfs.c +614 -0
@@ -0,0 +1,614 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +#include <linux/init.h> +#include <linux/kobject.h> +#include <linux/device.h> +#include <linux/spinlock.h> +#include <linux/stat.h> +#include <linux/types.h> +#include <mtd/ubi-header.h> +#include "ubi.h" +#include "uif.h" +#include "upd.h" +#include "debug.h" +#include "sysfs.h" +#include "io.h" +#include "wl.h" +#include "badeb.h" +#include "account.h" +#include "vtbl.h" +#include "alloc.h" +#include "background.h" + +static struct class *ubi_class; + +static ssize_t ubi_version_show (struct class *class,char *buf); + +/* Class attributes corresponding to files in '/<sysfs>/class/ubi/' */ +static struct class_attribute ubi_version = +__ATTR (version,S_IRUGO , ubi_version_show,NULL ); + +int __init ubi_sysfs_global_init (void ) +{ +int err; + + ubi_class =class_create (THIS_MODULE ,UBI_NAME_STR ); +if (IS_ERR (ubi_class)) { + err =PTR_ERR (ubi_class); + goto out; + } + + err =class_create_file (ubi_class, &ubi_version); +if (err) + goto out_class; + +return 0 ; + +out_class: +class_destroy (ubi_class); +out: +return err; +} + +void ubi_sysfs_global_close (void ) +{ +class_remove_file (ubi_class, &ubi_version); +class_destroy (ubi_class); +} + +static void dev_release (struct class_device *dev); +static ssize_t dev_eraseblock_size_show (struct class_device *dev,char *buf); +static ssize_t dev_avail_eraseblocks_show (struct class_device *dev,char *buf); +static ssize_t dev_total_eraseblocks_show (struct class_device *dev,char *buf); +static ssize_t dev_volumes_count_show (struct class_device *dev,char *buf); +static ssize_t dev_max_ec_show (struct class_device *dev,char *buf); +static ssize_t dev_update_show (struct class_device *dev,char *buf); +static ssize_t dev_reserved_for_bad_show (struct class_device *dev,char *buf); +static ssize_t dev_bad_peb_count_show (struct class_device *dev,char *buf); +static ssize_t dev_max_vol_count_show (struct class_device *dev,char *buf); +static ssize_t dev_min_io_size_show (struct class_device *dev,char *buf); +static ssize_t dev_bgt_enabled_show (struct class_device *dev,char *buf); +static ssize_t dev_bgt_enabled_store (struct class_device *dev,const char *buf, +size_t count); + +/* +* Class device attributes corresponding to files in '/<sysfs>/class/ubi/ubiX'. +*/ +static struct class_device_attribute dev_eraseblock_size = +__ATTR (eraseblock_size,S_IRUGO , dev_eraseblock_size_show,NULL ); +static struct class_device_attribute dev_avail_eraseblocks = +__ATTR (avail_eraseblocks,S_IRUGO , dev_avail_eraseblocks_show,NULL ); +static struct class_device_attribute dev_total_eraseblocks = +__ATTR (total_eraseblocks,S_IRUGO , dev_total_eraseblocks_show,NULL ); +static struct class_device_attribute dev_volumes_count = +__ATTR (volumes_count,S_IRUGO , dev_volumes_count_show,NULL ); +static struct class_device_attribute dev_max_ec = +__ATTR (max_ec,S_IRUGO , dev_max_ec_show,NULL ); +static struct class_device_attribute dev_update = +__ATTR (update,S_IRUGO , dev_update_show,NULL ); +static struct class_device_attribute dev_reserved_for_bad = +__ATTR (reserved_for_bad,S_IRUGO , dev_reserved_for_bad_show,NULL ); +static struct class_device_attribute dev_bad_peb_count = +__ATTR (bad_peb_count,S_IRUGO , dev_bad_peb_count_show,NULL ); +static struct class_device_attribute dev_max_vol_count = +__ATTR (max_vol_count,S_IRUGO , dev_max_vol_count_show,NULL ); +static struct class_device_attribute dev_min_io_size = +__ATTR (min_io_size,S_IRUGO , dev_min_io_size_show,NULL ); +static struct class_device_attribute dev_bgt_enabled = +__ATTR (bgt_enabled,S_IRUGO |S_IWUSR , + dev_bgt_enabled_show, dev_bgt_enabled_store); + +int ubi_sysfs_init (const struct ubi_info *ubi) +{ +int err; +struct ubi_uif_info *uif = ubi->uif; + + uif->dev.release = dev_release; + uif->dev.devt =MKDEV (uif->major,0 ); + uif->dev.class = ubi_class; +sprintf (&uif->dev.class_id[0 ],UBI_NAME_STR "%d" , ubi->ubi_num); + err =class_device_register (&uif->dev); +if (err) + goto out; + + err =class_device_create_file (&uif->dev, &dev_eraseblock_size); +if (err) + goto out_unregister; + err =class_device_create_file (&uif->dev, &dev_avail_eraseblocks); +if (err) + goto out_eraseblock_size; + err =class_device_create_file (&uif->dev, &dev_total_eraseblocks); +if (err) + goto out_avail_eraseblocks; + err =class_device_create_file (&uif->dev, &dev_volumes_count); +if (err) + goto out_total_eraseblocks; + err =class_device_create_file (&uif->dev, &dev_max_ec); +if (err) + goto out_volumes_count; + err =class_device_create_file (&uif->dev, &dev_update); +if (err) + goto out_volumes_max_ec; + err =class_device_create_file (&uif->dev, &dev_reserved_for_bad); +if (err) + goto out_update; + err =class_device_create_file (&uif->dev, &dev_bad_peb_count); +if (err) + goto out_reserved_for_bad; + err =class_device_create_file (&uif->dev, &dev_max_vol_count); +if (err) + goto out_bad_peb_count; + err =class_device_create_file (&uif->dev, &dev_min_io_size); +if (err) + goto out_max_vol_count; + err =class_device_create_file (&uif->dev, &dev_bgt_enabled); +if (err) + goto out_min_io_size; + +return 0 ; + +out_min_io_size: +class_device_remove_file (&uif->dev, &dev_min_io_size); +out_max_vol_count: +class_device_remove_file (&uif->dev, &dev_max_vol_count); +out_bad_peb_count: +class_device_remove_file (&uif->dev, &dev_bad_peb_count); +out_reserved_for_bad: +class_device_remove_file (&uif->dev, &dev_reserved_for_bad); +out_update: +class_device_remove_file (&uif->dev, &dev_update); +out_volumes_max_ec: +class_device_remove_file (&uif->dev, &dev_max_ec); +out_volumes_count: +class_device_remove_file (&uif->dev, &dev_volumes_count); +out_total_eraseblocks: +class_device_remove_file (&uif->dev, &dev_total_eraseblocks); +out_avail_eraseblocks: +class_device_remove_file (&uif->dev, &dev_avail_eraseblocks); +out_eraseblock_size: +class_device_remove_file (&uif->dev, &dev_eraseblock_size); +out_unregister: +class_device_unregister (&uif->dev); +out: +ubi_err ("failed to initialize sysfs for UBI device %d" , ubi->ubi_num); +return err; +} + +void ubi_sysfs_close (const struct ubi_info *ubi) +{ +struct ubi_uif_info *uif = ubi->uif; + +class_device_remove_file (&uif->dev, &dev_bgt_enabled); +class_device_remove_file (&uif->dev, &dev_min_io_size); +class_device_remove_file (&uif->dev, &dev_max_vol_count); +class_device_remove_file (&uif->dev, &dev_bad_peb_count); +class_device_remove_file (&uif->dev, &dev_reserved_for_bad); +class_device_remove_file (&uif->dev, &dev_update); +class_device_remove_file (&uif->dev, &dev_max_ec); +class_device_remove_file (&uif->dev, &dev_volumes_count); +class_device_remove_file (&uif->dev, &dev_total_eraseblocks); +class_device_remove_file (&uif->dev, &dev_avail_eraseblocks); +class_device_remove_file (&uif->dev, &dev_eraseblock_size); +class_device_unregister (&uif->dev); +} + +static void vol_release (struct class_device *dev); +static ssize_t vol_reserved_ebs_show (struct class_device *dev,char *buf); +static ssize_t vol_type_show (struct class_device *dev,char *buf); +static ssize_t vol_name_show (struct class_device *dev,char *buf); +static ssize_t vol_corrupted_show (struct class_device *dev,char *buf); +static ssize_t vol_alignment_show (struct class_device *dev,char *buf); +static ssize_t vol_usable_eb_size_show (struct class_device *dev,char *buf); +static ssize_t vol_data_bytes_show (struct class_device *dev,char *buf); +static ssize_t vol_upd_marker_show (struct class_device *dev,char *buf); + +/* +* Class device attributes corresponding to files in +* '/<sysfs>/class/ubi/ubiX/Y'. +*/ +static struct class_device_attribute vol_reserved_ebs = +__ATTR (reserved_ebs,S_IRUGO , vol_reserved_ebs_show,NULL ); +static struct class_device_attribute vol_type = +__ATTR (type,S_IRUGO , vol_type_show,NULL ); +static struct class_device_attribute vol_name = +__ATTR (name,S_IRUGO , vol_name_show,NULL ); +static struct class_device_attribute vol_corrupted = +__ATTR (corrupted,S_IRUGO , vol_corrupted_show,NULL ); +static struct class_device_attribute vol_alignment = +__ATTR (alignment,S_IRUGO , vol_alignment_show,NULL ); +static struct class_device_attribute vol_usable_eb_size = +__ATTR (usable_eb_size,S_IRUGO , vol_usable_eb_size_show,NULL ); +static struct class_device_attribute vol_data_bytes = +__ATTR (data_bytes,S_IRUGO , vol_data_bytes_show,NULL ); +static struct class_device_attribute vol_upd_marker = +__ATTR (upd_marker,S_IRUGO , vol_upd_marker_show,NULL ); + +/* +* Note, this function does not free allocated resources in case of failure - +* the caller does it. This is because this would cause release() here and the +* caller would oops. +*/ +int ubi_sysfs_vol_init (const struct ubi_info *ubi,struct ubi_uif_volume *vol) +{ +int err; + + vol->dev.release = vol_release; + vol->dev.parent = &ubi->uif->dev; + vol->dev.devt =MKDEV (ubi->uif->major, vol->vol_id +1 ); + vol->dev.class = ubi_class; +sprintf (&vol->dev.class_id[0 ],"%d" , vol->vol_id); + err =class_device_register (&vol->dev); +if (err) +return err; + + err =class_device_create_file (&vol->dev, &vol_reserved_ebs); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_type); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_name); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_corrupted); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_alignment); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_usable_eb_size); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_data_bytes); +if (err) +return err; + err =class_device_create_file (&vol->dev, &vol_upd_marker); +if (err) +return err; +return 0 ; +} + +void ubi_sysfs_vol_close (struct ubi_uif_volume *vol) +{ +class_device_remove_file (&vol->dev, &vol_upd_marker); +class_device_remove_file (&vol->dev, &vol_data_bytes); +class_device_remove_file (&vol->dev, &vol_usable_eb_size); +class_device_remove_file (&vol->dev, &vol_alignment); +class_device_remove_file (&vol->dev, &vol_corrupted); +class_device_remove_file (&vol->dev, &vol_name); +class_device_remove_file (&vol->dev, &vol_type); +class_device_remove_file (&vol->dev, &vol_reserved_ebs); +class_device_unregister (&vol->dev); +} + +/** +* dev2ubi -- find UBI device description object by the pointer to the class +* device object. +* +* @dev: class device object pointer +* +* This function returns a pointer to the UBI device description object. +*/ +static inline struct ubi_info *dev2ubi (struct class_device *dev) +{ +struct ubi_uif_info *uif; + + uif = container_of(dev, struct ubi_uif_info, dev); +return uif->ubi; +} + +/* "Show" and "store" methods for files in '/<sysfs>/class/ubi/' */ +static ssize_t ubi_version_show (struct class *class,char *buf) +{ +return sprintf (buf,"%d\n" ,UBI_VERSION ); +} + +/* "Release" method for UBI devices */ +static void dev_release (struct class_device *dev) +{ +return ; +} + +/* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */ +static ssize_t dev_eraseblock_size_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->io->leb_size); +} + +static ssize_t dev_avail_eraseblocks_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->acc->avail_pebs); +} + +static ssize_t dev_total_eraseblocks_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->io->good_peb_count); +} + +static ssize_t dev_volumes_count_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->acc->uvol_count); +} + +static ssize_t dev_max_ec_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->wl->max_ec); +} + +static ssize_t dev_update_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); +int vol_id = ubi->upd->vol_id; + +if (vol_id ==-1 ) +return 0 ; +return sprintf (buf,"%d\n" , vol_id); +} + +static ssize_t dev_reserved_for_bad_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->beb->reserved_pebs); +} + +static ssize_t dev_bad_peb_count_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->io->bad_peb_count); +} + +static ssize_t dev_max_vol_count_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->acc->max_volumes); +} + +static ssize_t dev_min_io_size_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->io->min_io_size); +} + +static ssize_t dev_bgt_enabled_show (struct class_device *dev,char *buf) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +return sprintf (buf,"%d\n" , ubi->bgt->enabled); +} + +static ssize_t dev_bgt_enabled_store (struct class_device *dev,const char *buf, +size_t count) +{ +const struct ubi_info *ubi =dev2ubi (dev); + +if (count >2 ) +return -EINVAL ; + +if (count ==2 && buf[1 ] !='\n' ) +return -EINVAL ; + +if (buf[0 ] =='1' ) +ubi_bgt_enable (ubi); +else if (buf[0 ] =='0' ) +ubi_bgt_disable (ubi); +else +return -EINVAL ; + +return count; +} + +/** +* dev2ubi -- find volume description object by the pointer to the class device +* object. +* +* @dev: class device object pointer +* +* This function returns a pointer to the UBI volume description object. +*/ +static inline struct ubi_uif_volume *dev2vol (struct class_device *dev) +{ + return container_of(dev, struct ubi_uif_volume, dev); +} + +/* Release method for volume devices */ +static void vol_release (struct class_device *dev) +{ +const struct ubi_uif_volume *vol =dev2vol (dev); + +dbg_uif ("release volume %d" , vol->vol_id); +ubi_kfree (vol); +} + +/* +* "Show" methods for files in '/<sysfs>/class/ubi/ubiX/Y/'. +* +* Consider a situation: +* A. process 1 opens a sysfs file related to volume Y, say +* /<sysfs>/class/ubi/ubiX/Y/reserved_ebs; +* B. process 2 removes volume Y; +* C. process 1 starts reading the /<sysfs>/class/ubi/ubiX/Y/reserved_ebs file; +* +* What we want to do in a situation like that is to return error when the file +* is read. This is done by means of the 'removed' flag and the 'vol_lock' of +* the UBI UIF volume information structure. +*/ + +static ssize_t vol_reserved_ebs_show (struct class_device *dev,char *buf) +{ +int ret; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + ret =sprintf (buf,"%d\n" , vtr->reserved_pebs); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_type_show (struct class_device *dev,char *buf) +{ +int ret; +const char *tp; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + tp = vtr->vol_type ==UBI_DYNAMIC_VOLUME ?"dynamic" :"static" ; + ret =sprintf (buf,"%s\n" , tp); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_name_show (struct class_device *dev,char *buf) +{ +int ret; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + ret =sprintf (buf,"%s\n" , vtr->name); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_corrupted_show (struct class_device *dev,char *buf) +{ +int ret; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + ret =sprintf (buf,"%d\n" , vtr->corrupted); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_alignment_show (struct class_device *dev,char *buf) +{ +int ret; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + ret =sprintf (buf,"%d\n" , vtr->alignment); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_usable_eb_size_show (struct class_device *dev,char *buf) +{ +int ret, usable_eb_size; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); +const struct ubi_io_info *io = vol->ubi->io; + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + usable_eb_size = io->leb_size - vtr->data_pad; + ret =sprintf (buf,"%d\n" , usable_eb_size); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_data_bytes_show (struct class_device *dev,char *buf) +{ +int ret; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + ret =sprintf (buf,"%lld\n" , vtr->used_bytes); +spin_unlock (&vol->vol_lock); +return ret; +} + +static ssize_t vol_upd_marker_show (struct class_device *dev,char *buf) +{ +int ret; +const struct ubi_vtbl_vtr *vtr; +struct ubi_uif_volume *vol =dev2vol (dev); + +spin_lock (&vol->vol_lock); +if (vol->removed) { +spin_unlock (&vol->vol_lock); +dbg_uif ("volume %d was removed" , vol->vol_id); +return -EIO ; + } + vtr =ubi_vtbl_get_vtr (vol->ubi, vol->vol_id); + ret =sprintf (buf,"%d\n" , vtr->upd_marker); +spin_unlock (&vol->vol_lock); +return ret; +}
PATCH 39/44 character devices handling sub-unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/cdev.h +82 -0
@@ -0,0 +1,82 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* This is a part of the user interface unit and includes all the character +* device-related stuff. +*/ + +#ifndef __UBI_CDEV_H__ +#define __UBI_CDEV_H__ + +/* Maximum sequence numbers of UBI and volume character device IOCTLs */ +#define UBI_CDEV_IOC_MAX_SEQ 2 + +#ifndef CONFIG_MTD_UBI_USERSPACE_IO +#define VOL_CDEV_IOC_MAX_SEQ 1 +#else +#define VOL_CDEV_IOC_MAX_SEQ 2 +#endif + +struct ubi_info ; +struct inode ; +struct file ; +struct ubi_uif_volume ; + +/** +* ubi_cdev_vol_init - initialize all the character device-related stuff for +* an UBI volume. +* +* @ubi: the UBI device description object +* @vol: user interfaces unit volume description object +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_cdev_vol_init (const struct ubi_info *ubi,struct ubi_uif_volume *vol); + +/** +* ubi_cdev_vol_close - close all the character device-related stuff for an +* UBI volume. +* +* @vol: user interfaces unit volume description object +*/ +void ubi_cdev_vol_close (struct ubi_uif_volume *vol); + +/** +* ubi_cdev_init - initialize all the character device-related stuff for +* an UBI device. +* +* @ubi: the UBI device description object +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +int ubi_cdev_init (struct ubi_info *ubi); + +/** +* ubi_cdev_close - close all the character device-related stuff for an UBI +* device. +* +* @ubi: the UBI device description object +*/ +void ubi_cdev_close (const struct ubi_info *ubi); + +#endif /* !__UBI_CDEV_H__ */
PATCH 40/44 character devices handling sub-unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/cdev.c +1033 -0
@@ -0,0 +1,1033 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +#include <linux/init.h> +#include <linux/module.h> +#include <linux/fs.h> +#include <linux/stat.h> +#include <linux/sched.h> +#include <linux/ioctl.h> +#include <linux/cdev.h> +#include <linux/capability.h> +#include <linux/types.h> +#include <linux/mtd/ubi.h> +#include <mtd/ubi-user.h> +#include <mtd/ubi-header.h> +#include <asm/uaccess.h> +#include <asm/div64.h> +#include "ubi.h" +#include "uif.h" +#include "upd.h" +#include "eba.h" +#include "cdev.h" +#include "account.h" +#include "debug.h" +#include "alloc.h" +#include "vtbl.h" +#include "io.h" +#include "volmgmt.h" +#include "misc.h" + +static int vol_cdev_open (struct inode *inode,struct file *file); +static int vol_cdev_release (struct inode *inode,struct file *file); +static loff_t vol_cdev_llseek (struct file *file,loff_t offset,int origin); +static ssize_t vol_cdev_read (struct file *file,__user char *buf, +size_t count,loff_t * offp); +static ssize_t vol_cdev_write (struct file *file,const char __user *buf, +size_t count,loff_t *offp); +static int vol_cdev_ioctl (struct inode *inode,struct file *file, +unsigned int cmd,unsigned long arg); +#ifdef CONFIG_MTD_UBI_USERSPACE_IO +static ssize_t vol_cdev_direct_write (struct file *file,const char __user *buf, +size_t count,loff_t *offp); +#else +#define vol_cdev_direct_write (file, buf, count, offp) -EROFS +#endif + +/* Volume character device operations */ +static struct file_operations vol_cdev_operations = { + .owner =THIS_MODULE , + .open = vol_cdev_open, + .release = vol_cdev_release, + .llseek = vol_cdev_llseek, + .read = vol_cdev_read, + .write = vol_cdev_write, + .ioctl = vol_cdev_ioctl +}; + +int ubi_cdev_vol_init (const struct ubi_info *ubi,struct ubi_uif_volume *vol) +{ +int err; +const struct ubi_uif_info *uif = ubi->uif; + +cdev_init (&vol->cdev, &vol_cdev_operations); + vol->cdev.owner =THIS_MODULE ; + err =cdev_add (&vol->cdev,MKDEV (uif->major, vol->vol_id +1 ),1 ); +if (err) { +ubi_err ("cannot add char dev for volume %d" , vol->vol_id); + goto out; + } + +return 0 ; +out: +cdev_del (&vol->cdev); +return err; +} + +void ubi_cdev_vol_close (struct ubi_uif_volume *vol) +{ +cdev_del (&vol->cdev); +} + +static int ubi_cdev_ioctl (struct inode *inode,struct file *file, +unsigned int cmd,unsigned long arg); + +/* UBI character device operations */ +static struct file_operations ubi_cdev_operations = { + .owner =THIS_MODULE , + .ioctl = ubi_cdev_ioctl, + .llseek = no_llseek +}; + +int ubi_cdev_init (struct ubi_info *ubi) +{ +int err; +dev_t dev; +struct ubi_uif_info *uif = ubi->uif; +const struct ubi_acc_info *acc = ubi->acc; + +/* +* Major numbers for the UBI character devices are allocated +* dynamically. Major numbers of volume character devices are +* equivalent to ones of the corresponding UBI character device. Minor +* numbers of UBI character devices are 0, while minor numbers of +* volume character devices start from 1. Thus, we allocate one major +* number and acc->max_volumes + 1 minor numbers. +*/ + err =alloc_chrdev_region (&dev,0 , acc->max_volumes +1 , uif->ubi_name); +if (err) { +ubi_err ("cannot register UBI char devs" ); +return err; + } + +cdev_init (&uif->cdev, &ubi_cdev_operations); + uif->major =MAJOR (dev); + uif->cdev.owner =THIS_MODULE ; + + dev =MKDEV (uif->major,0 ); + err =cdev_add (&uif->cdev, dev,1 ); +if (err) { +ubi_err ("cannot add char dev %s" , uif->ubi_name); + goto out_unreg; + } + +dbg_cdev ("%s major:minor is %u:0" , uif->ubi_name, uif->major); +return 0 ; + +out_unreg: +unregister_chrdev_region (MKDEV (uif->major,0 ), acc->max_volumes +1 ); +return err; + +} + +void ubi_cdev_close (const struct ubi_info *ubi) +{ +struct ubi_uif_info *uif = ubi->uif; +const struct ubi_acc_info *acc = ubi->acc; + +cdev_del (&uif->cdev); +unregister_chrdev_region (MKDEV (uif->major,0 ), acc->max_volumes +1 ); +} + +static struct ubi_info *major2ubi_info (int major); + +/** +* vol_cdev_open - open method of volume character devices. +* +* @inode: inode of the volume character device +* @file: &struct file object of the volume character device +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static int vol_cdev_open (struct inode *inode,struct file *file) +{ +struct ubi_vol_desc *desc; +const struct ubi_info *ubi =major2ubi_info (imajor (inode)); +int vol_id =iminor (inode) -1 ; +int mode; + +if (file->f_mode &FMODE_WRITE ) + mode =UBI_READWRITE ; +else + mode =UBI_READONLY ; + +dbg_cdev ("open volume %d, mode %d" , vol_id, mode); + + desc =ubi_open_volume (ubi->ubi_num, vol_id, mode); +if (IS_ERR (desc)) +return PTR_ERR (desc); + + file->private_data = desc; +return 0 ; +} + +/** +* vol_cdev_release - release method of volume character devices. +* +* @inode: inode of the volume character device +* @file: &struct file object of the volume character device +*/ +static int vol_cdev_release (struct inode *inode,struct file *file) +{ +int err =0 ; +struct ubi_vol_desc *desc = file->private_data; +struct ubi_uif_volume *vol = desc->vol; + +dbg_cdev ("release volume %d, mode %d" , vol->vol_id, desc->mode); + +if (vol->updating) { +ubi_warn ("update of volume %d was not finished" , vol->vol_id); + err =ubi_upd_abort (vol->ubi, vol->vol_id); + vol->updating =0 ; + } + +ubi_close_volume (desc); +return err; +} + +/** +* vol_cdev_llseek - llseek method of volume character devices. +* +* @file: &struct file object of the volume character device +* @offset: file offset +* @origin: defines the starting point of the @offset +* +* If an update is in progress, seeking is prohibited. This function returns +* positive offset in case of success and a negative error code in case of +* failure. +*/ +static loff_t vol_cdev_llseek (struct file *file,loff_t offset,int origin) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = file->private_data; +const struct ubi_info *ubi = desc->vol->ubi; +struct ubi_uif_volume *vol = desc->vol; +loff_t new_offset; + +dbg_cdev ("seek volume %d, offset %lld, origin %d" , + vol->vol_id, offset, origin); + +if (vol->updating) { +dbg_cdev ("updating" ); +return -EBUSY ; + } + + vtr =ubi_vtbl_get_vtr (ubi, vol->vol_id); +ubi_assert (!IS_ERR (vtr)); + +switch (origin) { +case 0 :/* SEEK_SET */ + new_offset = offset; +break ; +case 1 :/* SEEK_CUR */ + new_offset = file->f_pos + offset; +break ; +case 2 :/* SEEK_END */ + new_offset = vtr->used_bytes + offset; +break ; +default : +return -EINVAL ; + } + +if (new_offset <0 || new_offset > vtr->used_bytes) { +dbg_err ("bad seek (%lld)" , new_offset); +return -EINVAL ; + } + +dbg_cdev ("set volume %d offset at %lld" , vol->vol_id, new_offset); + file->f_pos = new_offset; +return new_offset; +} + +/** +* vol_cdev_read - read method of volume character devices +* +* @file: &struct file object of volume character device +* @buf: user-space buffer where to put read data +* @count: how many bytes to read +* @offp: the read position +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static ssize_t vol_cdev_read (struct file *file,__user char *buf, +size_t count,loff_t * offp) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = file->private_data; +struct ubi_uif_volume *vol = desc->vol; +const struct ubi_info *ubi = vol->ubi; +const struct ubi_io_info *io = ubi->io; +int err, lnum, off, len, tbuf_size, vol_id = desc->vol->vol_id; +size_t count_save = count; +void *tbuf; +uint64_t tmp; + +dbg_cdev ("request: read %zd bytes from offset %lld of volume %u" , + count, *offp, vol_id); + +if (unlikely (count ==0 )) +return 0 ; + +if (vol->updating) { +dbg_err ("updating" ); +return -EBUSY ; + } + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); +ubi_assert (!IS_ERR (vtr)); +ubi_assert (*offp >=0 && *offp <= vtr->used_bytes); + +if (vtr->upd_marker) { +dbg_err ("damaged volume, update marker is set" ); +return -EBADF ; + } + +if (*offp == vtr->used_bytes) +return 0 ; + +if (unlikely (vtr->corrupted)) +dbg_err ("read from corrupted volume %d" , vol_id); + +if (*offp + count > vtr->used_bytes) + count_save = count = vtr->used_bytes - *offp; + +/* +* To optimize reading, we read in fractions of the minimum +* input/output units of the flash. +*/ + tbuf_size = (PAGE_SIZE / io->min_io_size) * io->min_io_size; +if (tbuf_size ==0 ) + tbuf_size = io->min_io_size; +if (tbuf_size > io->leb_size) + tbuf_size = io->leb_size; + + tbuf =ubi_kmalloc (tbuf_size); +if (!tbuf) +return -ENOMEM ; + +/* +* We read in portions of the minimal flash input/output unit. If we are +* requested to read form a non-aligned offset, we first read up to the +* nearest boundary, and later only read in units of 'tbuf_size'. +*/ +if (count > tbuf_size) { +int rem; + + tmp = *offp; + rem =do_div (tmp, io->min_io_size); +if (rem ==0 ) + len = tbuf_size; +else + len = io->min_io_size - rem; + }else + len = count; + + tmp = *offp; + off =do_div (tmp, vtr->usable_leb_size); + lnum = tmp; + +do { +cond_resched (); + +if (off + len >= vtr->usable_leb_size) + len = vtr->usable_leb_size - off; + + err =ubi_eba_read_leb (ubi, vol_id, lnum, tbuf, off, len,0 ); +if (unlikely (err)) +break ; + + off += len; +if (off == vtr->usable_leb_size) { + lnum +=1 ; + off -= vtr->usable_leb_size; + } + + count -= len; + *offp += len; + + err =copy_to_user (buf, tbuf, len); +if (unlikely (err)) { +dbg_err ("memory access error" ); +break ; + } + + buf += len; + len = count > tbuf_size ? tbuf_size : count; + }while (count); + +ubi_kfree (tbuf); +return err ? err : count_save - count; +} + +/** +* vol_cdev_write - write method of volume character devices +* +* @file: &struct file object of volume character device +* @buf: user-space buffer with the data to write +* @count: how many bytes to write +* @offp: the write position +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static ssize_t vol_cdev_write (struct file *file,const char __user *buf, +size_t count,loff_t *offp) +{ +int err =0 ; +struct ubi_vol_desc *desc = file->private_data; +struct ubi_uif_volume *vol = desc->vol; +const struct ubi_info *ubi = vol->ubi; + +dbg_cdev ("requested: write %zd bytes to offset %lld of volume %u" , + count, *offp, vol->vol_id); + +if (!vol->updating) +return vol_cdev_direct_write (file, buf, count, offp); + + err =ubi_upd_write_data (ubi, vol->vol_id, buf, count); +if (err <0 ) { +dbg_err ("cannot write %zd bytes of update data" , count); +return err; + } + +if (err) { +/* Update is finished */ + vol->updating =0 ; + + err =ubi_check_volume (ubi, vol->vol_id); +if (err <0 ) +return err; + +if (err ==1 ) { +ubi_warn ("volume %d on UBI device %d is corrupted" , + vol->vol_id, ubi->ubi_num); + err =ubi_vtbl_set_corrupted (ubi, vol->vol_id); +if (err) +return err; + } + vol->checked =1 ; + } + + *offp += count; +return count; +} + +/** +* vol_cdev_ioctl - ioctl method of volume character devices. +* +* @inode: inode of the volume character device +* @file: &struct file object of the volume character device +* @cmd: ioctl command +* @arg: ioctl arguments +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static int vol_cdev_ioctl (struct inode *inode,struct file *file, +unsigned int cmd,unsigned long arg) +{ +int err =0 ; +struct ubi_vol_desc *desc = file->private_data; +const struct ubi_info *ubi = desc->vol->ubi; +void __user *argp = (void __user *)arg; + +if (_IOC_NR (cmd) >VOL_CDEV_IOC_MAX_SEQ || +_IOC_TYPE (cmd) !=UBI_VOL_IOC_MAGIC ) { +dbg_err ("bad ioctl command" ); +return -ENOTTY ; + } + +if (_IOC_DIR (cmd) && _IOC_READ) + err = !access_ok (VERIFY_WRITE , argp,_IOC_SIZE (cmd)); +else if (_IOC_DIR (cmd) && _IOC_WRITE) + err = !access_ok (VERIFY_READ , argp,_IOC_SIZE (cmd)); +if (err) { +dbg_err ("memory access error" ); +return -EFAULT ; + } + +switch (cmd) { + +/* Volume update command */ +case UBI_IOCVOLUP : + { +int64_t bytes, rsvd_bytes; +const struct ubi_vtbl_vtr *vtr; + +if (!capable (CAP_SYS_RESOURCE )) { +dbg_err ("no rights" ); + err = -EPERM ; +break ; + } + + err =copy_from_user (&bytes, argp,sizeof (int64_t )); +if (err) { +dbg_err ("memory access error" ); + err = -EFAULT ; +break ; + } + +dbg_cdev ("update volume %u, %lld bytes" , + desc->vol->vol_id, (long long )bytes); + +if (desc->mode ==UBI_READONLY ) { +dbg_err ("read-only mode" ); + err = -EROFS ; +break ; + } + + vtr =ubi_vtbl_get_vtr (ubi, desc->vol->vol_id); + rsvd_bytes = vtr->reserved_pebs * + (ubi->io->leb_size - vtr->data_pad); +if (bytes <0 || bytes > rsvd_bytes) { +dbg_err ("bad data size %lld" , (long long )bytes); + err = -EINVAL ; +break ; + } + + desc->vol->updating =1 ; + err =ubi_upd_start (ubi, desc->vol->vol_id, bytes); +if (bytes ==0 ) +/* It was just volume truncation */ + desc->vol->updating =0 ; + file->f_pos =0 ; +break ; + } + +#ifdef CONFIG_MTD_UBI_USERSPACE_IO +/* An eraseblock erasure command */ +case UBI_IOCEBER : + { +int32_t lnum; +const struct ubi_vtbl_vtr *vtr; + + err =__get_user (lnum, (__user int32_t *)argp); +if (err) { +dbg_err ("memory access error" ); + err = -EFAULT ; +break ; + } + +if (desc->mode ==UBI_READONLY ) { +dbg_err ("read-only mode" ); + err = -EROFS ; +break ; + } + + vtr =ubi_vtbl_get_vtr (ubi, desc->vol->vol_id); +ubi_assert (!IS_ERR (vtr)); +if (lnum <0 || lnum >= vtr->reserved_pebs) { +dbg_err ("bad lnum %d" , lnum); + err = -EINVAL ; +break ; + } + +if (vtr->vol_type !=UBI_DYNAMIC_VOLUME ) { +dbg_err ("static volume" ); + err = -EROFS ; +break ; + } + +dbg_cdev ("erase LEB %d:%d" , desc->vol->vol_id, lnum); + err =ubi_eba_erase_leb (ubi, desc->vol->vol_id, lnum); +break ; + } +#endif + +default : + err = -ENOTTY ; +break ; + } + +return err; +} + +static int check_mkvol_req (const struct ubi_info *ubi, +const struct ubi_mkvol_req *req); + +static int check_rsvol_req (const struct ubi_info *ubi, +const struct ubi_rsvol_req *req); + +/** +* ubi_cdev_ioctl - ioctl method of UBI character devices. +* +* @inode: inode of the UBI character device +* @file: &struct file object of the UBI character device +* @cmd: ioctl command +* @arg: ioctl arguments +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static int ubi_cdev_ioctl (struct inode *inode,struct file *file, +unsigned int cmd,unsigned long arg) +{ +int err =0 ; +const struct ubi_info *ubi; +void __user *argp = (void __user *)arg; + +if (_IOC_NR (cmd) >UBI_CDEV_IOC_MAX_SEQ || +_IOC_TYPE (cmd) !=UBI_IOC_MAGIC ) { +dbg_err ("bad ioctl command" ); +return -ENOTTY ; + } + +if (_IOC_DIR (cmd) && _IOC_READ) + err = !access_ok (VERIFY_WRITE , argp,_IOC_SIZE (cmd)); +else if (_IOC_DIR (cmd) && _IOC_WRITE) + err = !access_ok (VERIFY_READ , argp,_IOC_SIZE (cmd)); +if (err) +return -EFAULT ; + +if (!capable (CAP_SYS_RESOURCE )) { +dbg_err ("no rights" ); +return -EPERM ; + } + + ubi =major2ubi_info (imajor (inode)); +if (IS_ERR (ubi)) +return PTR_ERR (ubi); + +switch (cmd) { + +/* Create volume command */ +case UBI_IOCMKVOL : + { +char *name; +struct ubi_vtbl_vtr vtr; +struct ubi_mkvol_req req; +const struct ubi_io_info *io = ubi->io; +int pebs; + + err =__copy_from_user (&req, argp, +sizeof (struct ubi_mkvol_req )); +if (err) { + err = -EFAULT ; +break ; + } + +/* Make sure that user passed us sane data */ + pebs =check_mkvol_req (ubi, &req); +if (pebs <0 ) { + err = pebs; +break ; + } + + name =ubi_kmalloc (req.name_len +1 ); +if (!name) { + err = -ENOMEM ; +break ; + } + + err =copy_from_user (name, req.name, req.name_len +1 ); +if (err) { + err = -EFAULT ; + goto out_free; + } + + vtr.reserved_pebs = pebs; + vtr.alignment = req.alignment; + vtr.vol_type = req.vol_type; + vtr.name_len = req.name_len; + vtr.name = name; + vtr.data_pad = io->leb_size % vtr.alignment; + +dbg_cdev ("create volume ID %d, size %d, type %d, name %s" , + req.vol_id, vtr.reserved_pebs, vtr.vol_type, + vtr.name); + + err =ubi_vmt_mkvol (ubi, req.vol_id, &vtr); +if (err <0 ) + goto out_free; + + req.vol_id = err; + err =ubi_uif_mkvol (ubi, req.vol_id); +if (err) + goto out_rmvol; + +ubi_kfree (name); + + err =__copy_to_user (argp, &req, +sizeof (struct ubi_mkvol_req )); +if (err) { + err = -EFAULT ; +break ; + } + +break ; + +out_rmvol: +ubi_vmt_rmvol (ubi, req.vol_id); +out_free: +ubi_kfree (name); +break ; + } + +/* Remove volume command */ +case UBI_IOCRMVOL : + { +int32_t vol_id; +struct ubi_vol_desc *desc; + + err =__get_user (vol_id, (__user int32_t *)argp); +if (err) { + err = -EFAULT ; +break ; + } + +dbg_cdev ("remove volume %u" , vol_id); + + desc =ubi_open_volume (ubi->ubi_num, vol_id,UBI_EXCLUSIVE ); +if (IS_ERR (desc)) { + err =PTR_ERR (desc); +break ; + } + + err =ubi_uif_close_and_rmvol (desc); +if (err) { +ubi_close_volume (desc); +break ; + } + + err =ubi_vmt_rmvol (ubi, vol_id); +break ; + } + +/* Re-size volume command */ +case UBI_IOCRSVOL : + { +int rem, pebs; +uint64_t tmp; +const struct ubi_vtbl_vtr *vtr; +struct ubi_rsvol_req req; +struct ubi_vol_desc *desc; + + err =__copy_from_user (&req, argp, +sizeof (struct ubi_rsvol_req )); +if (err) { + err = -EFAULT ; +break ; + } + +/* Make sure that user passed us sane data */ + err =check_rsvol_req (ubi, &req); +if (err) +break ; + +dbg_cdev ("re-size volume %d to size %lld bytes" , + req.vol_id, (long long )req.bytes); + + desc =ubi_open_volume (ubi->ubi_num, req.vol_id,UBI_EXCLUSIVE ); +if (IS_ERR (desc)) { + err =PTR_ERR (desc); +break ; + } + + vtr =ubi_vtbl_get_vtr (ubi, req.vol_id); +ubi_assert (!IS_ERR (vtr)); + + tmp = req.bytes; + rem =do_div (tmp, vtr->usable_leb_size); + pebs = tmp; +if (rem) + pebs +=1 ; + + err =ubi_vmt_rsvol (ubi, req.vol_id, pebs); +ubi_close_volume (desc); +break ; + } + +default : + err = -ENOTTY ; +break ; + } + +return err; +} + +/** +* major2ubi_info - find the UBI device description by major number of the +* corresponding character device. +* +* @major: major number +* +* This function returns a pointer to the UBI description object. +*/ +static struct ubi_info *major2ubi_info (int major) +{ +int i; + +for (i =0 ; i < ubis_num; i++) +if (ubis[i] && ubis[i]->uif->major == major) +return ubis[i]; + +BUG (); +return NULL ; +} + +/** +* check_mkvol_req - check sanity of a volume creation request. +* +* @ubi: the UBI device description object +* @req: the request to check +* +* This function returns a positive volume size in eraseblocks if the request +* is sane, and %-EINVAL if not. +*/ +static int check_mkvol_req (const struct ubi_info *ubi, +const struct ubi_mkvol_req *req) +{ +int n, err, rem, ebs, usable_leb_size; +char *name =NULL ; +const struct ubi_io_info *io = ubi->io; +uint64_t bytes; + + name =ubi_kmalloc (req->name_len +1 ); +if (!name) +return -ENOMEM ; + +if (req->bytes <0 || req->alignment <0 || req->vol_type <0 || + req->name_len <0 ) { +dbg_err ("negative values" ); + goto bad; + } + +if ((req->vol_id <0 || req->vol_id >= ubi->acc->max_volumes) && + req->vol_id !=UBI_VOL_NUM_AUTO ) { +dbg_err ("bad vol_id %d" , req->vol_id); + goto bad; + } + +if (req->alignment ==0 ) { +dbg_err ("zero alignment" ); + goto bad; + } + +if (req->bytes ==0 ) { +dbg_err ("zero bytes" ); + goto bad; + } + +if (req->vol_type !=UBI_DYNAMIC_VOLUME && + req->vol_type !=UBI_STATIC_VOLUME ) { +dbg_err ("bad vol_type" ); + goto bad; + } + +if (req->alignment > io->leb_size) { +dbg_err ("too large alignment" ); + goto bad; + } + + n = req->alignment % io->min_io_size; +if (req->alignment !=1 && n) { +dbg_err ("alignment is not multiple of min I/O unit size" ); + goto bad; + } + +if (req->name_len >UBI_VOL_NAME_MAX ) { +dbg_err ("too long volume name, max is %d" ,UBI_VOL_NAME_MAX ); + goto bad; + } + +if (!req->name) { +dbg_err ("NULL volume name" ); + goto bad; + } + + err =copy_from_user (name, req->name, req->name_len +1 ); +if (err) +return err; + + n =strnlen (name, req->name_len +1 ); +if (n != req->name_len) { +dbg_err ("bad name_len" ); + goto bad; + } + +ubi_kfree (name); + +/* Calculate how many eraseblocks are requested */ + usable_leb_size = io->leb_size - io->leb_size % req->alignment; + bytes = req->bytes; + rem =do_div (bytes, usable_leb_size); + ebs = bytes; +if (rem) + ebs +=1 ; + +return ebs; + +bad: +dbg_err ("bad volume creation request" ); +ubi_dbg_dump_mkvol_req (req, name); +ubi_kfree (name); +return -EINVAL ; +} + +/** +* check_rsvol_req - check sanity of a volume re-size request. +* +* @ubi: the UBI device description object +* @req: the re-size request to check +* +* This function returns zero if the request is sane, and %-EINVAL if not. +*/ +static int check_rsvol_req (const struct ubi_info *ubi, +const struct ubi_rsvol_req *req) +{ +if (req->bytes <=0 ) { +dbg_err ("bad bytes %lld" , (long long )req->bytes); +return -EINVAL ; + } + +if (req->vol_id <0 || req->vol_id >= ubi->acc->max_volumes) { +dbg_err ("bad vol_id %d" , req->vol_id); +return -EINVAL ; + } + +return 0 ; +} + +#ifdef CONFIG_MTD_UBI_USERSPACE_IO + +/** +* vol_cdev_direct_write - write to volume character devices directly (nothing +* to do with O_DIRECT!). +* +* @file: &struct file object of volume character device +* @buf: user-space buffer with the data to write +* @count: how many bytes to write +* @offp: the write position +* +* This function allows to directly write to dynamic UBI volumes, without +* issuing the volume update operation. This function returns zero in case of +* success and a negative error code in case of failure. +*/ +static ssize_t vol_cdev_direct_write (struct file *file,const char __user *buf, +size_t count,loff_t *offp) +{ +const struct ubi_vtbl_vtr *vtr; +struct ubi_vol_desc *desc = file->private_data; +struct ubi_uif_volume *vol = desc->vol; +const struct ubi_info *ubi = vol->ubi; +const struct ubi_io_info *io = ubi->io; +int lnum, off, len, tbuf_size, vol_id = vol->vol_id, err =0 ; +size_t count_save = count; +char *tbuf; +uint64_t tmp; + +dbg_cdev ("requested: write %zd bytes to offset %lld of volume %u" , + count, *offp, desc->vol->vol_id); + + vtr =ubi_vtbl_get_vtr (ubi, vol_id); + +ubi_assert (!IS_ERR (vtr)); +ubi_assert (!vol->updating); +ubi_assert (*offp >=0 && *offp <= vtr->used_bytes); + +if (vtr->vol_type ==UBI_STATIC_VOLUME ) { +dbg_err ("static volume" ); +return -EROFS ; + } + + tmp = *offp; + off =do_div (tmp, vtr->usable_leb_size); + lnum = tmp; + +if (off % io->min_io_size) { +dbg_err ("unaligned position" ); +return -EIO ; + } + +if (*offp + count > vtr->used_bytes) + count_save = count = vtr->used_bytes - *offp; + +/* +* We can only write in fractions of the minimum input/output unit of +* the flash. +*/ +if (count % io->min_io_size) { +dbg_err ("unaligned write length" ); +return -EINVAL ; + } + + tbuf_size = (PAGE_SIZE / io->min_io_size) * io->min_io_size; +if (tbuf_size ==0 ) + tbuf_size = io->min_io_size; +if (tbuf_size > io->leb_size) + tbuf_size = io->leb_size; + + tbuf =ubi_kmalloc (tbuf_size); +if (!tbuf) +return -ENOMEM ; + + len = count > tbuf_size ? tbuf_size : count; + +while (count) { +cond_resched (); + +if (off + len >= vtr->usable_leb_size) + len = vtr->usable_leb_size - off; + +dbg_cdev ("copy %d bytes of user data" , len); + err =copy_from_user (tbuf, buf, len); +if (err) { +dbg_err ("memory access error" ); + err = -EFAULT ; +break ; + } + +dbg_cdev ("write %d bytes to LEB %d:%d, offset %d" , + len, vol_id, lnum, off); + + err =ubi_eba_write_leb (ubi, vol_id, lnum, tbuf, off, len, +UBI_DATA_UNKNOWN ); +if (unlikely (err)) +break ; + + off += len; +if (off == vtr->usable_leb_size) { + lnum +=1 ; + off -= vtr->usable_leb_size; + } + + count -= len; + *offp += len; + buf += len; + len = count > tbuf_size ? tbuf_size : count; + } + +ubi_kfree (tbuf); +return err ? err : count_save - count; +} + +#endif /* CONFIG_MTD_UBI_USERSPACE_IO */
PATCH 41/44 gluebi unit header
In reply to Artem Bityutskiydrivers/mtd/ubi/gluebi.h +88 -0
@@ -0,0 +1,88 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy +*/ + +/* +* This unit is responsible for emulating MTD devices on top of UBI devices. +* This sounds strange, but it is in fact quite useful to make legacy software +* work on top of UBI. New software should use native UBI API instead. +* +* Gluebi emulated MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit +* size (mtd->writesize) is equivalent to the underlying flash minimal I/O +* unit. The eraseblock size is equivalent to the logical UBI volume eraseblock +* size. +*/ + +#ifndef __UBI_GLUEBI_H__ +#define __UBI_GLUEBI_H__ + +#include <linux/mtd/mtd.h> + +#if defined(CONFIG_MTD_UBI_GLUEBI ) + +struct ubi_uif_volume ; + +/** +* struct ubi_gluebi_volume - emulated MTD device description data structure. +* +* @desc: UBI volume descriptor +* @mtd: MTD device description object +*/ +struct ubi_gluebi_volume +{ +struct ubi_vol_desc *desc; +int refcount; +struct mtd_info mtd; +}; + +/** +* ubi_gluebi_vol_init - initialize all the gluebi-related stuff for an UBI +* volume. +* +* @ubi: the UBI device description object +* @vol: user interfaces unit volume description object +* +* This function is called when an UBI volume is created in order to create +* corresponding MTD device. Returns zero in case of success and a negative +* error code in case of failure. +*/ +int ubi_gluebi_vol_init (const struct ubi_info *ubi,struct ubi_uif_volume *vol); + +/** +* ubi_gluebi_vol_close - close all the gluebi-related stuff for an UBI volume. +* +* @vol: user interfaces unit volume description object +* +* This function is called when an UBI volume is removed in order to remove +* corresponding MTD device. Returns zero in case of success and a negative +* error code in case of failure. +*/ +int ubi_gluebi_vol_close (struct ubi_uif_volume *vol); + +#else + +struct ubi_gluebi_volume +{ +}; + +#define ubi_gluebi_vol_init (ubi, vol) ({int __ret; __ret = 0;}) +#define ubi_gluebi_vol_close (vol) ({int __ret; __ret = 0;}) + +#endif /* CONFIG_MTD_UBI_GLUEBI */ +#endif /* __UBI_GLUEBI_H__ */
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
Quoted 8 lines · Artem Bityutskiy
+ * This unit is responsible for emulating MTD devices on top of UBI devices.
+ * This sounds strange, but it is in fact quite useful to make legacy software
+ * work on top of UBI. New software should use native UBI API instead.
+ *
+ * Gluebi emulated MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
+ * size (mtd->writesize) is equivalent to the underlying flash minimal I/O
+ * unit. The eraseblock size is equivalent to the logical UBI volume eraseblock
+ * size.
This approach doesn't seem to make sense at all. If the MTD device interface is flawed, the right approach should be to fix that instead. After all, there are not many users of the MTD interface, so you should be able to adapt them.
In fact, I would expect that there is much more reason to merge the existing MTD interface with the block interface in the kernel, but you now introduce a third interface that is unrelated to the first two, and make another conversion to convert it back?
Let's assume I want to use the wear levelling capabilities of UBI on top of an SD card, and use the ext3 file system on top of it. I get a stack of
1. MMC
2. block2mtd
3. UBI
4. gluebi
5. mtdblock
6. VFS
when in an ideal world, it should just be
1. MMC
2. UBI
3. VFS
Arnd <><
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
Quoted 10 lines · Arnd Bergmann
Quoted 8 lines · Artem Bityutskiy
+ * This unit is responsible for emulating MTD devices on top of UBI devices.
+ * This sounds strange, but it is in fact quite useful to make legacy software
+ * work on top of UBI. New software should use native UBI API instead.
+ *
+ * Gluebi emulated MTD devices of "MTD_UBIVOLUME" type. Their minimal I/O unit
+ * size (mtd->writesize) is equivalent to the underlying flash minimal I/O
+ * unit. The eraseblock size is equivalent to the logical UBI volume eraseblock
+ * size.
Quoted 4 lines
This approach doesn't seem to make sense at all. If the MTD device interface is flawed, the right approach should be to fix that instead. After all, there are not many users of the MTD interface, so you should be able to adapt them.
No, the MTD interface isn't flawed. gluebi is present to make things like JFFS2 work on top of UBI volumes with very little adaptations. If you go changing _every_ MTD user to now use either an MTD device or a native UBI device, then the code for those users just gets bloated.
Quoted 8 lines
In fact, I would expect that there is much more reason to merge the existing MTD interface with the block interface in the kernel, but you now introduce a third interface that is unrelated to the first two, and make another conversion to convert it back?
Let's assume I want to use the wear levelling capabilities of UBI on top of an SD card, and use the ext3 file system on top of it. I get a stack of
Quoted 6 lines
1. MMC
2. block2mtd
3. UBI
4. gluebi
5. mtdblock
6. VFS
Assuming your SD card isn't doing wear-leveling itself within the device, yes that is what you would get. Or you could do something slightly more sane and use:
1. MMC
2. block2mtd
3. JFFS2
Quoted 5 lines
when in an ideal world, it should just be
1. MMC
2. UBI
3. VFS
This could perhaps still be done. UBI has a general concept of an io_unit so theoretically it could be adapted to work with the block layer in the kernel.
josh
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
Quoted 4 lines · Josh Boyer
No, the MTD interface isn't flawed. gluebi is present to make things like JFFS2 work on top of UBI volumes with very little adaptations. If you go changing _every_ MTD user to now use either an MTD device or a native UBI device, then the code for those users just gets bloated.
Right, that was my point. If the MTD API in the kernel is not flawed, why do we need the 'native' UBI interface? Just merge gluebi into UBI and get rid of the extra abstraction.
Quoted 2 lines
Assuming your SD card isn't doing wear-leveling itself within the device, yes that is what you would get.
While probably all modern SD cards have some amount of wear leveling built in, I wouldn't want to rely on that for anything but the simple large-file-on-fatfs (jpeg or mp3) case. Using UBI on top of the native wear-leveling sounds like the right solution.
Quoted 6 lines
Or you could do something slightly more sane
and use:1. MMC
2. block2mtd
3. JFFS2
Not on a 4GB SD medium, with the current jffs2 version. The problem is that jffs2 doesn't scale that well, so you want a different fs. Since logfs isn't stable yet, you end up with something like ext3, which in turn means that you need a UBI-like concept to avoid wearing out the blocks that store your metadata.
Arnd <><
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
Quoted 9 lines · Arnd Bergmann
Quoted 4 lines · Josh Boyer
No, the MTD interface isn't flawed. gluebi is present to make things like JFFS2 work on top of UBI volumes with very little adaptations. If you go changing _every_ MTD user to now use either an MTD device or a native UBI device, then the code for those users just gets bloated.
Right, that was my point. If the MTD API in the kernel is not flawed, why do we need the 'native' UBI interface? Just merge gluebi into UBI and get rid of the extra abstraction.
That suggestion came up several times. gluebi represents a compromise between the two groups. IIRC, the issue was that representing UBI volumes as MTD devices only makes sense in the dynamic volume case. Static UBI volumes require special write/update handling and so there was a need for a native interface anyway.
Quoted 8 lines
Quoted 3 lines
Assuming your SD card isn't doing wear-leveling itself within the device, yes that is what you would get.
While probably all modern SD cards have some amount of wear leveling built in, I wouldn't want to rely on that for anything but the simple large-file-on-fatfs (jpeg or mp3) case. Using UBI on top of the native wear-leveling sounds like the right solution.
Yeah. Unfortunately, SD/USB/CF cards are all in sort of an awkward spot when it comes to things like that. They don't expose the raw flash underneath, and they don't provide any indication of how robust the built in wear-leveling is. Ugh.
Quoted 9 lines
Quoted 6 lines
Or you could do something slightly more sane
and use:1. MMC
2. block2mtd
3. JFFS2Not on a 4GB SD medium, with the current jffs2 version. The problem is that jffs2 doesn't scale that well, so you want a different fs.
Oh, believe me I know. :)
Quoted 3 lines
Since logfs isn't stable yet, you end up with something like ext3, which in turn means that you need a UBI-like concept to avoid wearing out the blocks that store your metadata.
That just sounds like we need Jörn to get off his butt and finish logfs ;) Seriously, until something like that is done we'll be stuck with some not so pleasant solutions for these kind of devices.
josh
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
Quoted 11 lines · On Sunday 18 February 2007 04:02:17 Josh Boyer wrote:
Quoted 9 lines · Arnd Bergmann
Quoted 4 lines · Josh Boyer
No, the MTD interface isn't flawed. gluebi is present to make things like JFFS2 work on top of UBI volumes with very little adaptations. If you go changing _every_ MTD user to now use either an MTD device or a native UBI device, then the code for those users just gets bloated.
Right, that was my point. If the MTD API in the kernel is not flawed, why do we need the 'native' UBI interface? Just merge gluebi into UBI and get rid of the extra abstraction.
Quoted 5 lines
That suggestion came up several times. gluebi represents a compromise between the two groups. IIRC, the issue was that representing UBI volumes as MTD devices only makes sense in the dynamic volume case. Static UBI volumes require special write/update handling and so there was a need for a native interface anyway.
Which brings be back to my original point ;-)
I'm sure this has been discussed before, but I'd still like to understand what is so special with 'static UBI volumes' that they can't be used with a slightly extended MTD interface.
Arnd <><
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
Quoted 18 lines · Arnd Bergmann
Quoted 11 lines · On Sunday 18 February 2007 04:02:17 Josh Boyer wrote:
Quoted 9 lines · Arnd Bergmann
Quoted 4 lines · Josh Boyer
No, the MTD interface isn't flawed. gluebi is present to make things like JFFS2 work on top of UBI volumes with very little adaptations. If you go changing _every_ MTD user to now use either an MTD device or a native UBI device, then the code for those users just gets bloated.
Right, that was my point. If the MTD API in the kernel is not flawed, why do we need the 'native' UBI interface? Just merge gluebi into UBI and get rid of the extra abstraction.
Quoted 5 lines
That suggestion came up several times. gluebi represents a compromise between the two groups. IIRC, the issue was that representing UBI volumes as MTD devices only makes sense in the dynamic volume case. Static UBI volumes require special write/update handling and so there was a need for a native interface anyway.
Quoted 5 lines
Which brings be back to my original point ;-)
I'm sure this has been discussed before, but I'd still like to understand what is so special with 'static UBI volumes' that they can't be used with a slightly extended MTD interface.
Since I personally loath the standard "RTFM" response, I'll try and explain briefly.
Think of static volumes as containers for chunks of whole binary data. You don't want partial updates to this data, or it's inconsistent. So a static volume must be written wholesale. As UBI writes out the data, each block has a field in the VID header with the data size and the CRC for that block.
Take a vmlinux binary for example. By putting that in a static UBI volume, a bootloader can read each block in the volume in order, verify the block CRC and know that the resulting reconstructed image is valid. If the CRC doesn't match on a block, the data is invalid and the bootloader can do whatever it determines to be the correct behavior.
One could arguably create a new MTD interface called "update" and provide only that and the read function for static volumes. But that is essentially what has been done within UBI itself. I won't speak for Artem, who could explain much more than I as to why it was done the way it currently is.
josh
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
In reply to Arnd BergmannQuoted 5 lines · Arnd Bergmann
Which brings be back to my original point ;-)
I'm sure this has been discussed before, but I'd still like to understand what is so special with 'static UBI volumes' that they can't be used with a slightly extended MTD interface.
Let me provide a list of new things.
* Two types of character devices: UBI devices and UBI volumes. MTD is aware only of one type of device - just MTD device.
* Two types of volumes.
* New volume update operation.
* Write hints - you may inform UBI which kind of data you are writing - long term data, short-term data, or just unknown. Depending on the hint it will pick physical eraseblock with high erase counter low or medium.
* Asynchronous eraseblock erasure operation
* Atomic eraseblock change operation.
* When you read static volume, you may select whether you want UBI to check CRC or not (CRCs are per-eraseblock, so often it is not reasonable to check it on any read operation).
* Resizing of volume, and all the things related to their dynamic nature.
* some other small new interfaces.
The whole idea of MTD interface it to provide _uniform_ method to access _all_ flashes. It does not look reasonable for UBI software to use MTD interface, because it is _designed_ for UBI, not for MTD.
So, having native interface for ubi-only software looks reasonable. But we also able to integrate UBI into MTD for MTD software, which looks a good design decision and a good compromise.
P.S: Also, I'd ask you to look at the monster mtd_info data structure and be scared :-) Imagine we add more there.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
In reply to Josh BoyerQuoted 2 lines · Josh Boyer
That just sounds like we need Jörn to get off his butt and finish logfs ;)
He's working on it this very moment. Help in debugging would be appreciated, if progress is too slow for people. That part seems to consume a lot of time and scale fairly well.
Jörn
Signature · 4 lines
Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don't get fancy. -- Rob Pike
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
In reply to Josh BoyerQuoted 4 lines · Josh Boyer
No, the MTD interface isn't flawed. gluebi is present to make things like JFFS2 work on top of UBI volumes with very little adaptations. If you go changing _every_ MTD user to now use either an MTD device or a native UBI device, then the code for those users just gets bloated.
That's not what Arnd means. We really want only one mtd interface that works transparently with both physical and stackable devices. That means instead of adding a new ubi interface you want to add support for stackable devices to mtd.
Re: [PATCH 41/44 take 2] [UBI] gluebi unit header
In reply to Arnd BergmannQuoted 4 lines · Arnd Bergmann
This approach doesn't seem to make sense at all. If the MTD device interface is flawed, the right approach should be to fix that instead. After all, there are not many users of the MTD interface, so you should be able to adapt them.
MTD interface is not flawed, why? It is a good abstraction for flash chips.
UBI provide too many new services to utilize MTD interface. UBI != MTD, but UBI may behave like MTD, although it is wider.
Quoted 2 lines
In fact, I would expect that there is much more reason to merge the existing MTD interface with the block interface in the kernel
I do not think so, but the idea sounds exciting, please, talk to dwmw2 about this. But surely you know how different flash devices and HDD's
are: http://www.linux-mtd.infradead.org/faq/general.html
MTD devices are bare flashes.
Block devices are HDDs, MMCs, SDs, USB sticks, etc.
Quoted 2 lines
but you now introduce
a third interface that is unrelated to the first two
Why not? UBI is something which works on top of MTD, so it does relate to MTD. But yes, it has nothing to do with block devices, I do not why you talk about them. They are just irrelevant in my opinion, lets remove them from discussion.
Quoted 2 lines
, and make another
conversion to convert it back?
All the the conversion things were created as debugging tools. I have not heard anybody used them in production. But may be someone do, but this is rare though and they must have _really good reasons_ for this.
Quoted 2 lines
Let's assume I want to use the wear levelling capabilities of UBI on top of an SD card, and use the ext3 file system on top of it.
I do not see any point in this. SD card is a block device. It was designed to be a block device. Using it for different purpose does not look reasonable. Use bare flashes instead.
But technically it is possible to add block device back-end support to UBI, but I do not know any real use-case for this.
Quoted 3 lines
I get a stack of
1. MMC
Block device.
Quoted 1 line
2. block2mtd
A debugging tool to develop flash software on host. Not normally used for other purposes.
Quoted 1 line
3. UBI
Close to MTD but also have a lot of new services.
Quoted 1 line
4. gluebi
MTD devices emulated by UBI.
Quoted 1 line
5. mtdblock
Stupid FTL driver, to emulated block devices on top of MTD. Too straightforward, may only be used in RO mode. Any use in RW mode is dangerous as you loose whole eraseblock in case of an unclean reboot.
Quoted 1 line
6. VFS
Generalized FS view of the kernel.
Quoted 3 lines
when in an ideal world, it should just be
1. MMC
Just all block devices.
Quoted 1 line
2. UBI
Is MTD here?
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
PATCH 42/44 gluebi unit implementation
In reply to Artem Bityutskiydrivers/mtd/ubi/gluebi.c +363 -0
@@ -0,0 +1,363 @@ +/* +* Copyright (c) International Business Machines Corp., 2006 +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See +* the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +* Author: Artem B. Bityutskiy (based on code written by Joern Engel) +*/ + +#include <linux/mtd/mtd.h> +#include <linux/mtd/ubi.h> +#include <asm/div64.h> +#include "ubi.h" +#include "uif.h" +#include "gluebi.h" +#include "vtbl.h" +#include "misc.h" +#include "debug.h" +#include "alloc.h" +#include "io.h" +#include "eba.h" +#include "wl.h" + +static int gluebi_get_device (struct mtd_info *mtd); +static void gluebi_put_device (struct mtd_info *mtd); +static int gluebi_read (struct mtd_info *mtd,loff_t from,size_t len, +size_t *retlen,unsigned char *buf); +static int gluebi_write (struct mtd_info *mtd,loff_t to,size_t len, +size_t *retlen,const u_char *buf); +static int gluebi_erase (struct mtd_info *mtd,struct erase_info *instr); + +/** +* mtd2vol - take the user interface volume description object by an MTD object. +* +* @mtd: the MTD object +* +* This function returns the user interface volume description object +* corresponding to the @mtd object. +*/ +static inline struct ubi_uif_volume *mtd2vol (struct mtd_info *mtd) +{ +struct ubi_gluebi_volume *gluebi_vol; +struct ubi_uif_volume *vol; + + gluebi_vol = container_of(mtd, struct ubi_gluebi_volume, mtd); + vol = container_of(gluebi_vol, struct ubi_uif_volume, gluebi_vol); +return vol; +} + +int ubi_gluebi_vol_init (const struct ubi_info *ubi,struct ubi_uif_volume *vol) +{ +int err; +struct mtd_info *mtd = &vol->gluebi_vol.mtd; +const struct ubi_vtbl_vtr *vtr; +const struct ubi_io_info *io = ubi->io; + + vtr =ubi_vtbl_get_vtr (ubi, vol->vol_id); +ubi_assert (!IS_ERR (vtr)); + + mtd->name =strdup_len (vtr->name, vtr->name_len); +if (!mtd->name) +return -ENOMEM ; + + mtd->type =MTD_UBIVOLUME ; +if (!io->ro_mode) + mtd->flags =MTD_WRITEABLE ; + mtd->writesize = io->min_io_size; + mtd->owner =THIS_MODULE ; + mtd->size = vtr->usable_leb_size * vtr->reserved_pebs; + mtd->erasesize = vtr->usable_leb_size; + mtd->read = gluebi_read; + mtd->write = gluebi_write; + mtd->erase = gluebi_erase; + mtd->get_device = gluebi_get_device; + mtd->put_device = gluebi_put_device; + +if (add_mtd_device (mtd)) { +ubi_err ("cannot not add MTD device\n" ); + +/* +* Unfortunately, add_mtd_device() does not return sane error +* code. So, let's name it -ENOMEM; +*/ + err = -ENOMEM ; + goto out_free; + } + +dbg_gluebi ("added mtd%d (\"%s\"), size %u, EB size %u" , + mtd->index, mtd->name, mtd->size, mtd->erasesize); + +return 0 ; + +out_free: +ubi_kfree (mtd->name); +return err; +} + +int ubi_gluebi_vol_close (struct ubi_uif_volume *vol) +{ +int err; +struct mtd_info *mtd = &vol->gluebi_vol.mtd; + +dbg_gluebi ("remove mtd%d" , mtd->index); + + err =del_mtd_device (mtd); +if (err) +return err; + +ubi_kfree (mtd->name); +return 0 ; +} + +/** +* gluebi_get_device - get MTD device reference. +* +* @mtd: the MTD device description object +* +* This function is called every time the MTD device is being got. Returns +* zero in case of success and a negative error code in case of failure. +*/ +static int gluebi_get_device (struct mtd_info *mtd) +{ +struct ubi_uif_volume *vol =mtd2vol (mtd); +struct ubi_gluebi_volume *gluebi_vol = &vol->gluebi_vol; + +/* +* We do not introduce locks for gluebi reference count because the +* get_device()/put_device() calls are already serialized. +*/ +if (gluebi_vol->refcount >0 ) { +/* +* The MTD device is already referenced and this is just one +* more reference. MTD allows opening many users to open the +* same volume simultaniously and do not distinguish between +* readers/writers/exclusive openers as UBI does. So we do not +* open the UBI volume again - just increase the reference +* counter and return. +*/ + gluebi_vol->refcount +=1 ; +return 0 ; + } + +/* +* This is the first reference to this UBI volume via the MTD device +* interface. Open the corresponding volume in read-write mode. +*/ + gluebi_vol->desc =ubi_open_volume (vol->ubi->ubi_num, vol->vol_id, +UBI_READWRITE ); +if (IS_ERR (gluebi_vol->desc)) +return PTR_ERR (gluebi_vol->desc); + gluebi_vol->refcount +=1 ; +return 0 ; +} + +/** +* gluebi_put_device - put MTD device reference. +* +* @mtd: the MTD device description object +* +* This function is called every time the MTD device is being put. Returns +* zero in case of success and a negative error code in case of failure. +*/ +static void gluebi_put_device (struct mtd_info *mtd) +{ +struct ubi_gluebi_volume *gluebi_vol = &mtd2vol (mtd)->gluebi_vol; + + gluebi_vol->refcount -=1 ; +ubi_assert (gluebi_vol->refcount >=0 ); +if (gluebi_vol->refcount ==0 ) +ubi_close_volume (gluebi_vol->desc); +} + +/** +* gluebi_read - read operation of emulated MTD devices. +* +* @mtd: the MTD device description object +* @from: absolute offset from where to read +* @len: how many bytes to read +* @retlen: count of read bytes is returned here +* @buf: the buffer to store the data to +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static int gluebi_read (struct mtd_info *mtd,loff_t from,size_t len, +size_t *retlen,unsigned char *buf) +{ +int err =0 , lnum, offs, total_read; +struct ubi_uif_volume *vol; +const struct ubi_info *ubi; + +dbg_gluebi ("read %zd bytes from offset %lld" , len, from); + +if (unlikely (len <0 || from <0 || from + len > mtd->size)) +return -EINVAL ; + + vol =mtd2vol (mtd); + ubi = vol->ubi; + + offs =do_div (from, mtd->erasesize); + lnum = from; + + total_read = len; +while (total_read) { +size_t to_read = mtd->erasesize - offs; + +if (to_read > total_read) + to_read = total_read; + +dbg_gluebi ("read %zd bytes from LEB %d:%d, offset %d" , + to_read, vol->vol_id, lnum, offs); + + err =ubi_eba_read_leb (ubi, vol->vol_id, lnum, buf, offs, + to_read,0 ); +if (unlikely (err)) +break ; + + lnum +=1 ; + offs =0 ; + total_read -= to_read; + buf += to_read; + } + + *retlen = len - total_read; +return err; +} + +/** +* gluebi_write - write operation of emulated MTD devices. +* +* @mtd: the MTD device description object +* @to: absolute offset where to write +* @len: how many bytes to write +* @retlen: count of written bytes is returned here +* @buf: the buffer with data to write +* +* This function returns zero in case of success and a negative error code in +* case of failure. +*/ +static int gluebi_write (struct mtd_info *mtd,loff_t to,size_t len, +size_t *retlen,const u_char *buf) +{ +int err =0 , lnum, offs, total_written; +struct ubi_uif_volume *vol; +const struct ubi_info *ubi; + +dbg_gluebi ("write %zd bytes to offset %lld" , len, to); + +if (unlikely (len <0 || to <0 || len + to > mtd->size)) +return -EINVAL ; + + vol =mtd2vol (mtd); + ubi = vol->ubi; + +if (unlikely (ubi->io->ro_mode)) +return -EROFS ; + + offs =do_div (to, mtd->erasesize); + lnum = to; + +if (unlikely (len % mtd->writesize || offs % mtd->writesize)) +return -EINVAL ; + + total_written = len; +while (total_written) { +size_t to_write = mtd->erasesize - offs; + +if (to_write > total_written) + to_write = total_written; + +dbg_gluebi ("write %zd bytes to LEB %d:%d, offset %d" , + to_write, vol->vol_id, lnum, offs); + + err =ubi_eba_write_leb (ubi, vol->vol_id, lnum, buf, offs, + to_write,UBI_DATA_UNKNOWN ); +if (unlikely (err)) +break ; + + lnum +=1 ; + offs =0 ; + total_written -= to_write; + buf += to_write; + } + + *retlen = len - total_written; +return err; +} + +/** +* gluebi_erase - erase operation of emulated MTD devices. +* +* @mtd: the MTD device description object +* @instr: the erase operation description +* +* This function calls the erase callback when finishes. Returns zero in case +* of success and a negative error code in case of failure. +*/ +static int gluebi_erase (struct mtd_info *mtd,struct erase_info *instr) +{ +int err, i, lnum, count; +struct ubi_uif_volume *vol; +const struct ubi_info *ubi; + +dbg_gluebi ("erase %u bytes at offset %u" , instr->len, instr->addr); + +if (unlikely (instr->addr <0 || + instr->addr > mtd->size - mtd->erasesize)) +return -EINVAL ; + +if (unlikely (instr->len <0 || + instr->addr + instr->len > mtd->size)) +return -EINVAL ; + +if (unlikely (instr->addr % mtd->writesize || + instr->len % mtd->writesize)) +return -EINVAL ; + + lnum = instr->addr / mtd->erasesize; + count = instr->len / mtd->erasesize; + + vol =mtd2vol (mtd); + ubi = vol->ubi; + +if (unlikely (ubi->io->ro_mode)) +return -EROFS ; + +for (i =0 ; i < count; i++) { +dbg_gluebi ("erase LEB %d" , lnum); + + err =ubi_eba_erase_leb (ubi, vol->vol_id, lnum + i); +if (unlikely (err)) + goto out_err; + } + +/* +* MTD erase operations are synchronous, so we have to make sure the +* physical eraseblock is wiped out. +*/ + err =ubi_wl_flush (ubi); +if (unlikely (err)) + goto out_err; + + instr->state =MTD_ERASE_DONE ; +mtd_erase_callback (instr); + +return 0 ; + +out_err: + instr->state =MTD_ERASE_FAILED ; + instr->fail_addr = lnum * mtd->erasesize; +return err; +}
PATCH 43/44 JFFS2 UBI support
In reply to Artem Bityutskiyfs/jffs2/fs.c +12 -0
@@ -672,6 +672,13 @@ static int jffs2_flash_setup(struct jffsreturn ret; } +/* and an UBI volume */ +if (jffs2_ubivol (c)) { + ret =jffs2_ubivol_setup (c); +if (ret) +return ret; + } +return ret; }
@@ -690,4 +697,9 @@ void jffs2_flash_cleanup(struct jffs2_sbif (jffs2_nor_wbuf_flash (c)) {jffs2_nor_wbuf_flash_cleanup (c); } + +/* and an UBI volume */ +if (jffs2_ubivol (c)) { +jffs2_ubivol_cleanup (c); + } }
fs/jffs2/os-linux.h +6 -0
@@ -98,6 +98,9 @@ static inline void jffs2_init_inode_info#define jffs2_nor_wbuf_flash (c) (0)#define jffs2_nor_wbuf_flash_setup (c) (0)#define jffs2_nor_wbuf_flash_cleanup (c) do {} while (0) +#define jffs2_ubivol (c) (0) +#define jffs2_ubivol_setup (c) (0) +#define jffs2_ubivol_cleanup (c) do {} while (0)#else /* NAND and/or ECC'd NOR support present */
@@ -133,6 +136,9 @@ void jffs2_nand_flash_cleanup(struct jff#define jffs2_dataflash (c) (c->mtd->type == MTD_DATAFLASH)int jffs2_dataflash_setup (struct jffs2_sb_info *c);void jffs2_dataflash_cleanup (struct jffs2_sb_info *c); +#define jffs2_ubivol (c) (c->mtd->type == MTD_UBIVOLUME) +int jffs2_ubivol_setup (struct jffs2_sb_info *c); +void jffs2_ubivol_cleanup (struct jffs2_sb_info *c);#define jffs2_nor_wbuf_flash (c) (c->mtd->type == MTD_NORFLASH && ! (c->mtd->flags & MTD_BIT_WRITEABLE))int jffs2_nor_wbuf_flash_setup (struct jffs2_sb_info *c);
fs/jffs2/wbuf.c +24 -0
@@ -1274,3 +1274,27 @@ int jffs2_nor_wbuf_flash_setup(struct jfvoid jffs2_nor_wbuf_flash_cleanup (struct jffs2_sb_info *c) {kfree (c->wbuf); } + +int jffs2_ubivol_setup (struct jffs2_sb_info *c) { + c->cleanmarker_size =0 ; + +if (c->mtd->writesize ==1 ) +/* We do not need write-buffer */ +return 0 ; + +init_rwsem (&c->wbuf_sem); + + c->wbuf_pagesize = c->mtd->writesize; + c->wbuf_ofs =0xFFFFFFFF ; + c->wbuf =kmalloc (c->wbuf_pagesize,GFP_KERNEL ); +if (!c->wbuf) +return -ENOMEM ; + +printk (KERN_INFO "JFFS2 write-buffering enabled buffer (%d) erasesize (%d)\n" , c->wbuf_pagesize, c->sector_size); + +return 0 ; +} + +void jffs2_ubivol_cleanup (struct jffs2_sb_info *c) { +kfree (c->wbuf); +}
include/mtd/mtd-abi.h +1 -0
@@ -24,6 +24,7 @@ struct mtd_oob_buf {#define MTD_NORFLASH 3#define MTD_NANDFLASH 4#define MTD_DATAFLASH 6 +#define MTD_UBIVOLUME 7#define MTD_WRITEABLE 0x400/* Device is writeable */ #define MTD_BIT_WRITEABLE 0x800/* Single bits can be flipped */
PATCH 44/44 update MAINTAINERS
In reply to Artem BityutskiyMAINTAINERS +8 -0
@@ -2176,6 +2176,14 @@ L: [email protected] T: git git://git.infradead.org/mtd-2.6.git S: Maintained +UNSORTED BLOCK IMAGES (UBI) +P: Artem Bityutskiy +M: [email protected] +W: http://www.linux-mtd.infradead.org/ +L: [email protected] +T: git git://git.infradead.org/ubi-2.6.git +S: Maintained + MICROTEK X6 SCANNER P: Oliver Neukum M: [email protected]
Quoted 7 lines · Artem Bityutskiy
The structure of the UBI code is very simple. Whole UBI consists of units. Each unit has one .c file which implements it and one .h file which defines the interface of this unit. So I've split the UBI code so that there is a distinct patch for each unit header and implementations. Plus there are several patches like JFFS2 support, user-space-visible headers, etc. To realize what is the unit responsible for, glance at its header - there is a short description.
Artem,
This patch introduces the Makefile before any of the source
files, which means it will break "git bisect" operations. Could you please refactor your patches so that the tree will build after any point in your patch series?
BTW, I'm not personally found of having a separate .h file for
every .c file; in my mind a much better division is a public and private header file.
- Ted
Theodore,
Quoted 4 lines · Theodore Tso
This patch introduces the
Makefilebefore any of the source
files, which means it will break "git bisect" operations. Could you please refactor your patches so that the tree will build after any point in your patch series?
I actually did not mean these patches should be included to a git. We have UBI git to pull from for these purposes. I basically manually split the UBI sources to make UBI easier to review. I should have added an "RFC" tag, apologies.
Quoted 3 lines
BTW, I'm not personally found of having a separate .h file for
every .c file; in my mind a much better division is a public and private header file.
This reflects the way of my thinking. I see UBI as a set of units with defined interfaces. So I even physically split the interface description into files. I still think it is easier to grasp the architecture this way.
So, it is not distinct header for _.c_ file, it is a distinct header for each _unit_, which is a basic architecture element of UBI.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Quoted 4 lines · Artem Bityutskiy
I actually did not mean these patches should be included to a git. We have UBI git to pull from for these purposes. I basically manually split the UBI sources to make UBI easier to review. I should have added an "RFC" tag, apologies.
It made it much, much, MUCH harder to review. Especially given that the documentation was separated from the implementation. As I looked at the implementation, there was no way to look and what it was supposed to do without flipping back to a previous e-mail message and losing my place.
Quoted 4 lines
This reflects the way of my thinking. I see UBI as a set of units with defined interfaces. So I even physically split the interface description into files. I still think it is easier to grasp the architecture this way.
Speaking as someone who was coming into it cold, it actually made it far more difficult. Your units were too small, so that meant the number of interfaces that were created as a result were huge! (Around 20 _sets_ of interfaces, all of which had to be comprehended for what should have been a relatively simple set of functionality!)
And when you create that many interfaces, it adds inertia to changing the interfaces later on, because it's sometimes not clear how many users of the interface there really are. My general rule of thumb is that if an interface only has one user, then it may be a good idea to combine it with the user of that interface, and then make the functions involved be a static, so that it becomes clear the only user of that functoin is within that one file. You can take this too far, and to extremes it doesn't work all that well, but the UBI layer has gone waaaaaay off the deep end in terms of functional decomposition.
Adding lots of units so you can have formal functional interfaces is not always the right answer in terms of making code more readable or maintainable. And in my humble opinion, UBI is a picture-perfect demonstration of this.
Regards,
- Ted
Quoted 5 lines · Theodore Tso
It made it much, much, MUCH harder to review. Especially given that the documentation was separated from the implementation. As I looked at the implementation, there was no way to look and what it was supposed to do without flipping back to a previous e-mail message and losing my place.
I will send Build stuff as the last patch next time, thanks, point taken.
I just used different concept: one looks at declaration and the overall picture becomes clear because _there is_ documentation. One does not look at the implementation to grasp picture on surface.
But your point is fair. I assume _programmers_ look in .c first. Users may always generate a pdf. I will do what you advise.
May be a good compromise would be to have just brief comments at headers, and full specification at .c. I will think about this, thanks.
Quoted 10 lines
Quoted 4 lines
This reflects the way of my thinking. I see UBI as a set of units with defined interfaces. So I even physically split the interface description into files. I still think it is easier to grasp the architecture this way.
Speaking as someone who was coming into it cold, it actually made it far more difficult. Your units were too small, so that meant the number of interfaces that were created as a result were huge! (Around 20 _sets_ of interfaces, all of which had to be comprehended for what should have been a relatively simple set of functionality!)
Why not? Some stuff may probably be merged. _Specific_ advices are welcome.
Quoted 9 lines
And when you create that many interfaces, it adds inertia to changing the interfaces later on, because it's sometimes not clear how many users of the interface there really are. My general rule of thumb is that if an interface only has one user, then it may be a good idea to combine it with the user of that interface, and then make the functions involved be a static, so that it becomes clear the only user of that functoin is within that one file. You can take this too far, and to extremes it doesn't work all that well, but the UBI layer has gone waaaaaay off the deep end in terms of functional decomposition.
Well... I do not want any flame on this topic. It is about taste, trade-offs, compromises. It is difficult to provide _objective_ and killing arguments here. But I will think on this, point taken, thanks.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Quoted 6 lines · Artem Bityutskiy
I just used different concept: one looks at declaration and the overall picture becomes clear because
_thereis_documentation. One does not look at the implementation to grasp picture on surface.But your point is fair. I assume _programmers_ look in .c first. Users may always generate a pdf. I will do what you advise.
First of all, you are writing kernel code for programmers, not end-users.
Secondly, and this is a big issue, you haven't specified which "units" are exporting interfaces that will be used outside of UBI, and which are being used only internally inside UBI. Interfaces which will be used outside of UBI have to be carefully controlled. Interfaces only used inside UBI are private interfaces which can be easily changed.
Also, if you are going to use this horrible, theoretical, pendantic Computer Science coding stlye, at the very least you should have included a module dependency diagram so it was easy to understand what the heck was going on. This would make it easier to make specific suggestions, because after I looked at it for a while, I decided it was too hard to figure out the overall architecture, and not worth my time, and so I gave up on the review. Not that my opinion is the only one you need to pay attention to, but if everyone is telling you that need to simplify the number of interfaces, you may want to listen since your code is going to need adequate review if you want to get it into mainline.
- Ted
Quoted 5 lines · Theodore Tso
Not that my opinion is the only
one you need to pay attention to, but if everyone is telling you that need to simplify the number of interfaces, you may want to listen since your code is going to need adequate review if you want to get it into mainline.
Theodore, thanks for even more vivid explanation. As I wrote, point taken. I was probably vague, sorry, but let me say this more bluntly: I will think how to change this. At the very least I _will_ dispose of 3-5 units and the situation will be better.
Thank you,
Artem.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Quoted 10 lines · Artem Bityutskiy
Quoted 9 lines
And when you create that many interfaces, it adds inertia to changing the interfaces later on, because it's sometimes not clear how many users of the interface there really are. My general rule of thumb is that if an interface only has one user, then it may be a good idea to combine it with the user of that interface, and then make the functions involved be a static, so that it becomes clear the only user of that functoin is within that one file. You can take this too far, and to extremes it doesn't work all that well, but the UBI layer has gone waaaaaay off the deep end in terms of functional decomposition.
Quoted 3 lines
Well... I do not want any flame on this topic. It is about taste, trade-offs, compromises. It is difficult to provide _objective_ and killing arguments here. But I will think on this, point taken, thanks.
Codingstyle is and always will be about tastes, but it usually helps a lot if you try to understand and follow the style of the project you want to contribute to :)
Quoted 7 lines · Christoph Hellwig
Quoted 3 lines
Well... I do not want any flame on this topic. It is about taste, trade-offs, compromises. It is difficult to provide _objective_ and killing arguments here. But I will think on this, point taken, thanks.
Codingstyle is and always will be about tastes, but it usually helps a lot if you try to understand and follow the style of the project you want to contribute to :)
Will be done at the next submit iteration.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Quoted 7 lines · Artem Bityutskiy
The structure of the UBI code is very simple. Whole UBI consists of units. Each unit has one .c file which implements it and one .h file which defines the interface of this unit. So I've split the UBI code so that there is a distinct patch for each unit header and implementations. Plus there are several patches like JFFS2 support, user-space-visible headers, etc. To realize what is the unit responsible for, glance at its header - there is a short description.
I think this is the wrong approach. For one thing the unit terms is rather foregin in Linux, and second this one header file "unit" is definitly too much. Normally you'd have as little as possible and at most four:
(1) internal definitions of the driver
(2) external kernel interface of the driver if nessecary
(3) user interface of the driver if nessecary
(4) ondisk structure if nessecary
Also please make sure you submit patches at this driver level. You really want one git commit per driver that is totally self-contained. If nessecary you'd split this into multiple patches if a single one would be bigger then the message size limit.
Quoted 2 lines · Christoph Hellwig
I think this is the wrong approach. For one thing the unit terms is rather foregin in Linux
I would rather disagree. Subjective. Unit is a generic word, just like subsystem. Unit-tests for example is a widespread word it refer to internal units of a big system.
Quoted 2 lines
, and second this one header file "unit" is
definitly too much.
It is .c + .h.
Why definitely, where is the definition? I appreciate your opinion, but do not want any endless discussion about "how the life has to go", though.
Quoted 7 lines
Normally you'd have as little as possible and at
most four:(1) internal definitions of the driver
(2) external kernel interface of the driver if nessecary
(3) user interface of the driver if nessecary
(4) ondisk structure if nessecary
Where does this rather restrictive model come from? What does it mean in practice for UBI? Please, be more specific.
Quoted 4 lines
Also please make sure you submit patches at this driver level. You really want one git commit per driver that is totally self-contained. If nessecary you'd split this into multiple patches if a single one would be bigger then the message size limit.
Please, refine what does this exactly mean. I do not see how I should have sent it, sorry. OK, I've separated external headers, JFFS2 support, build stuff. What next?
And I sent it just for review, as you requested, I assumed that for the pull we have git, and I specified the URL.
Thanks,
Artem.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Quoted 7 lines · Artem Bityutskiy
Quoted 2 lines · Christoph Hellwig
I think this is the wrong approach. For one thing the unit terms is rather foregin in Linux
I would rather disagree. Subjective. Unit is a generic word, just like subsystem. Unit-tests for example is a widespread word it refer to internal units of a big system.
What I meant is that we currently do not use the 'unit' term for code modules in the linux kernel at all.
Quoted 3 lines
Please, refine what does this exactly mean. I do not see how I should have sent it, sorry. OK, I've separated external headers, JFFS2 support, build stuff. What next?
You should not separate more but less. Generelly the review-"unit" should be a functionally separate kenrel module, not microscopic pieces of it. Unfortunately there are mailinglist limitations why we sometimes have to split things up a little further, but it's usually really hard to do useful splits at this boundary.
Artem> This patch-set contains UBI, which stands for Unsorted Block Artem> Images. This is closely related to the memory technology Artem> devices Linux subsystem (MTD), so this new piece of software is Artem> from drivers/mtd/ubi.
Can you define UBI in each and every file you create? This is a completely unique acronym and I'm sure a bunch of people will be going "wtf" when they read this, I know I was.
Artem> In short, UBI is kind of LVM layer but for flash (MTD) Artem> devices. It makes it possible to dynamically create, delete and Artem> re-size volumes. But the analogy is not full. UBI also takes Artem> care of wear-leveling and bad eraseblocks handling, so UBI Artem> completely hides 2 aspects of flash chips which make them very Artem> difficult to work with:
Neat stuff! Too bad you don't have a more descriptive name for it, UBI just doesn't seem to have any connection to LVM for Flash plus wear leveling.
/grin/
John
John,
Quoted 3 lines · John Stoffel
Can you define UBI in each and every file you create? This is a completely unique acronym and I'm sure a bunch of people will be going "wtf" when they read this, I know I was.
Do you mean adding something like "This is file is a part of the Unsorted Block Images (UBI) subsystem" to the header of each file?
Quoted 3 lines
Neat stuff! Too bad you don't have a more descriptive name for it, UBI just doesn't seem to have any connection to LVM for Flash plus wear leveling.
Well, this name was born inside IBM, the guys in CC and may comment on this. Feel free to advice a better name, although renaming this is something I really do not want to do.
And it indeed has no connection to LVM, I just used the analogy to quickly provide the idea what is this about.
Signature · 2 lines
Best regards, Artem Bityutskiy (Битюцкий Артём)
Quoted 6 lines · John Stoffel
Artem> This patch-set contains UBI, which stands for Unsorted Block Artem> Images. This is closely related to the memory technology Artem> devices Linux subsystem (MTD), so this new piece of software is Artem> from
drivers/mtd/ubi.Can you define UBI in each and every file you create? This is a
That is pretty pointless work. It's expanded in the Kconfig menu, which people need to select before using it anyway.
Quoted 10 lines
Artem> In short, UBI is kind of LVM layer but for flash (MTD) Artem> devices. It makes it possible to dynamically create, delete and Artem> re-size volumes. But the analogy is not full. UBI also takes Artem> care of wear-leveling and bad eraseblocks handling, so UBI Artem> completely hides 2 aspects of flash chips which make them very Artem> difficult to work with:
Neat stuff! Too bad you don't have a more descriptive name for it, UBI just doesn't seem to have any connection to LVM for Flash plus wear leveling.
Given that it's contained under the MTD tree, it should be fairly obvious it's flash related. And the Kconfig help text for UBI specifically mentions it's purpose and analogy to LVM.
josh
Quoted 9 lines · John Stoffel
Artem> This patch-set contains UBI, which stands for Unsorted Block Artem> Images. This is closely related to the memory technology Artem> devices Linux subsystem (MTD), so this new piece of software is Artem> from
drivers/mtd/ubi.Can you define UBI in each and every file you create? This is a completely unique acronym and I'm sure a bunch of people will be going "wtf" when they read this, I know I was.
We're not in kindergarden. An explanation in Kconfig or the main header file should be more than enough.