linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
@ 2015-08-19  8:39 Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 01/27] mtd-utils: Restructure the mtd-utils source Dongsheng Yang
                   ` (29 more replies)
  0 siblings, 30 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Hi Atem, Richard and Brian,
	This patchset introduce a userspace tool named ubifs_dump
to dump data from a ubi media.
	It will dump the areas in ubifs, such as super block,
master, log, lpt and main.
	That's helpful for us to see what exactly written in
media.
	the [1/patch] is a RESEND patch, it restructures the mtd-utils.
Please Brian help to take a look at it. thanx a lot. :)

NOTE:
	This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
But you can get a full code at:
https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1

Please help to review or test it.
Thanx

Dongsheng Yang (27):
  mtd-utils: Restructure the mtd-utils source.
  ubifs: pick some common definitions into ubifs_common.h
  ubifs: move the all io related code into io.[h|c]
  ubifs: remove the including of mkfs.ubifs.h in lpt.c
  ubifs: cut off the dependence from compr.o to mkfs.ubifs
  ubifs: cut off the dependence from devtable to mkfs.ubifs.h
  ubifs: introduce ubifs-utils/include and ubifs-utils/lib
  ubifs: move more functions into io lib
  ubifs: introduce a new tool ubifs_dump
  ubifs: introduce list.h
  ubifs: copy some important data in ubifs.h from kernel to ubifs-utils
  ubifs: copy some important functions in key.h from kernel to
    ubifs-utils
  ubifs: ubifs_dump: add dump_ch and dump_node functions
  ubifs: defs.h: introduce some compatible definition for printk class
  ubifs: io: introduce ubifs_read function to read ubi volume
  ubifs: ubifs_dump: dump super block
  ubifs: introduce scan for ubifs-utils
  ubifs: add some more compatible definitions in defs.h
  ubifs: ubifs_dump: dump master node
  ubifs: ubifs_dump: dump log area
  ubifs: introduce lprops lib
  ubifs: lpt: implement functions to scan lpt
  ubifs: ubifs_dump: dump lpt area
  ubifs: ubifs_dump: dump index area
  ubifs: defs.h: introduce some compatible definitions about integer
    such as __u16
  ubifs: introduce hexdump lib
  ubifs: ubifs_dump: dump data in hex format

 Makefile                                           |   76 +-
 flash_erase.c => flash-utils/flash_erase.c         |    0
 flash_eraseall => flash-utils/flash_eraseall       |    0
 flash_lock.c => flash-utils/flash_lock.c           |    0
 flash_otp_dump.c => flash-utils/flash_otp_dump.c   |    0
 flash_otp_info.c => flash-utils/flash_otp_info.c   |    0
 flash_otp_lock.c => flash-utils/flash_otp_lock.c   |    0
 flash_otp_write.c => flash-utils/flash_otp_write.c |    0
 flash_unlock.c => flash-utils/flash_unlock.c       |    0
 flashcp.c => flash-utils/flashcp.c                 |    0
 compr.c => jffsX-utils/compr.c                     |    0
 compr.h => jffsX-utils/compr.h                     |    0
 compr_lzo.c => jffsX-utils/compr_lzo.c             |    0
 compr_rtime.c => jffsX-utils/compr_rtime.c         |    0
 compr_zlib.c => jffsX-utils/compr_zlib.c           |    0
 device_table.txt => jffsX-utils/device_table.txt   |    0
 jffs-dump.c => jffsX-utils/jffs-dump.c             |    0
 jffs2dump.c => jffsX-utils/jffs2dump.c             |    0
 jffs2reader.c => jffsX-utils/jffs2reader.c         |    0
 mkfs.jffs2.1 => jffsX-utils/mkfs.jffs2.1           |    0
 mkfs.jffs2.c => jffsX-utils/mkfs.jffs2.c           |    0
 rbtree.c => jffsX-utils/rbtree.c                   |    0
 rbtree.h => jffsX-utils/rbtree.h                   |    0
 summary.h => jffsX-utils/summary.h                 |    0
 sumtool.c => jffsX-utils/sumtool.c                 |    0
 MAKEDEV => misc-utils/MAKEDEV                      |    0
 doc_loadbios.c => misc-utils/doc_loadbios.c        |    0
 docfdisk.c => misc-utils/docfdisk.c                |    0
 fectest.c => misc-utils/fectest.c                  |    0
 ftl_check.c => misc-utils/ftl_check.c              |    0
 ftl_format.c => misc-utils/ftl_format.c            |    0
 mcast_image.h => misc-utils/mcast_image.h          |    0
 mtd_debug.c => misc-utils/mtd_debug.c              |    0
 mtdpart.c => misc-utils/mtdpart.c                  |    0
 recv_image.c => misc-utils/recv_image.c            |    0
 serve_image.c => misc-utils/serve_image.c          |    0
 mkfs.ubifs/defs.h                                  |   92 --
 mkfs.ubifs/lpt.c                                   |  578 ---------
 mkfs.ubifs/mkfs.ubifs.h                            |  150 ---
 load_nandsim.sh => nand-utils/load_nandsim.sh      |    0
 nanddump.c => nand-utils/nanddump.c                |    0
 nandtest.c => nand-utils/nandtest.c                |    0
 nandwrite.c => nand-utils/nandwrite.c              |    0
 nftl_format.c => nand-utils/nftl_format.c          |    0
 nftldump.c => nand-utils/nftldump.c                |    0
 rfddump.c => nor-utils/rfddump.c                   |    0
 rfdformat.c => nor-utils/rfdformat.c               |    0
 {mkfs.ubifs => ubifs-utils}/COPYING                |    0
 {mkfs.ubifs => ubifs-utils}/README                 |    0
 {mkfs.ubifs => ubifs-utils/include}/compr.h        |    3 +-
 {mkfs.ubifs => ubifs-utils/include}/crc16.h        |    0
 ubifs-utils/include/defs.h                         |  215 ++++
 ubifs-utils/include/devtable.h                     |   55 +
 .../hashtable => ubifs-utils/include}/hashtable.h  |    0
 .../include}/hashtable_itr.h                       |    0
 .../include}/hashtable_private.h                   |    0
 ubifs-utils/include/hexdump.h                      |   21 +
 ubifs-utils/include/io.h                           |   21 +
 {mkfs.ubifs => ubifs-utils/include}/key.h          |   76 +-
 ubifs-utils/include/list.h                         |  484 ++++++++
 ubifs-utils/include/lprops.h                       |    6 +
 {mkfs.ubifs => ubifs-utils/include}/lpt.h          |    4 +
 ubifs-utils/include/scan.h                         |    8 +
 {mkfs.ubifs => ubifs-utils/include}/ubifs.h        |   96 +-
 ubifs-utils/include/ubifs_common.h                 |   50 +
 {mkfs.ubifs => ubifs-utils/lib}/compr.c            |   20 +-
 {mkfs.ubifs => ubifs-utils/lib}/crc16.c            |    0
 {mkfs.ubifs => ubifs-utils/lib}/devtable.c         |    7 +-
 .../hashtable => ubifs-utils/lib}/hashtable.c      |    0
 .../hashtable => ubifs-utils/lib}/hashtable_itr.c  |    0
 ubifs-utils/lib/hexdump.c                          |  200 +++
 ubifs-utils/lib/io.c                               |  152 +++
 ubifs-utils/lib/lprops.c                           |   79 ++
 ubifs-utils/lib/lpt.c                              | 1275 ++++++++++++++++++++
 ubifs-utils/lib/scan.c                             |  318 +++++
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/.gitignore  |    0
 .../mkfs.ubifs}/mkfs.ubifs.c                       |  144 +--
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.h                |   54 +
 ubifs-utils/ubifs_dump/ubifs_dump.c                | 1041 ++++++++++++++++
 79 files changed, 4220 insertions(+), 1005 deletions(-)
 rename flash_erase.c => flash-utils/flash_erase.c (100%)
 rename flash_eraseall => flash-utils/flash_eraseall (100%)
 rename flash_lock.c => flash-utils/flash_lock.c (100%)
 rename flash_otp_dump.c => flash-utils/flash_otp_dump.c (100%)
 rename flash_otp_info.c => flash-utils/flash_otp_info.c (100%)
 rename flash_otp_lock.c => flash-utils/flash_otp_lock.c (100%)
 rename flash_otp_write.c => flash-utils/flash_otp_write.c (100%)
 rename flash_unlock.c => flash-utils/flash_unlock.c (100%)
 rename flashcp.c => flash-utils/flashcp.c (100%)
 rename compr.c => jffsX-utils/compr.c (100%)
 rename compr.h => jffsX-utils/compr.h (100%)
 rename compr_lzo.c => jffsX-utils/compr_lzo.c (100%)
 rename compr_rtime.c => jffsX-utils/compr_rtime.c (100%)
 rename compr_zlib.c => jffsX-utils/compr_zlib.c (100%)
 rename device_table.txt => jffsX-utils/device_table.txt (100%)
 rename jffs-dump.c => jffsX-utils/jffs-dump.c (100%)
 rename jffs2dump.c => jffsX-utils/jffs2dump.c (100%)
 rename jffs2reader.c => jffsX-utils/jffs2reader.c (100%)
 rename mkfs.jffs2.1 => jffsX-utils/mkfs.jffs2.1 (100%)
 rename mkfs.jffs2.c => jffsX-utils/mkfs.jffs2.c (100%)
 rename rbtree.c => jffsX-utils/rbtree.c (100%)
 rename rbtree.h => jffsX-utils/rbtree.h (100%)
 rename summary.h => jffsX-utils/summary.h (100%)
 rename sumtool.c => jffsX-utils/sumtool.c (100%)
 rename MAKEDEV => misc-utils/MAKEDEV (100%)
 rename doc_loadbios.c => misc-utils/doc_loadbios.c (100%)
 rename docfdisk.c => misc-utils/docfdisk.c (100%)
 rename fectest.c => misc-utils/fectest.c (100%)
 rename ftl_check.c => misc-utils/ftl_check.c (100%)
 rename ftl_format.c => misc-utils/ftl_format.c (100%)
 rename mcast_image.h => misc-utils/mcast_image.h (100%)
 rename mtd_debug.c => misc-utils/mtd_debug.c (100%)
 rename mtdpart.c => misc-utils/mtdpart.c (100%)
 rename recv_image.c => misc-utils/recv_image.c (100%)
 rename serve_image.c => misc-utils/serve_image.c (100%)
 delete mode 100644 mkfs.ubifs/defs.h
 delete mode 100644 mkfs.ubifs/lpt.c
 delete mode 100644 mkfs.ubifs/mkfs.ubifs.h
 rename load_nandsim.sh => nand-utils/load_nandsim.sh (100%)
 rename nanddump.c => nand-utils/nanddump.c (100%)
 rename nandtest.c => nand-utils/nandtest.c (100%)
 rename nandwrite.c => nand-utils/nandwrite.c (100%)
 rename nftl_format.c => nand-utils/nftl_format.c (100%)
 rename nftldump.c => nand-utils/nftldump.c (100%)
 rename rfddump.c => nor-utils/rfddump.c (100%)
 rename rfdformat.c => nor-utils/rfdformat.c (100%)
 rename {mkfs.ubifs => ubifs-utils}/COPYING (100%)
 rename {mkfs.ubifs => ubifs-utils}/README (100%)
 rename {mkfs.ubifs => ubifs-utils/include}/compr.h (97%)
 rename {mkfs.ubifs => ubifs-utils/include}/crc16.h (100%)
 create mode 100644 ubifs-utils/include/defs.h
 create mode 100644 ubifs-utils/include/devtable.h
 rename {mkfs.ubifs/hashtable => ubifs-utils/include}/hashtable.h (100%)
 rename {mkfs.ubifs/hashtable => ubifs-utils/include}/hashtable_itr.h (100%)
 rename {mkfs.ubifs/hashtable => ubifs-utils/include}/hashtable_private.h (100%)
 create mode 100644 ubifs-utils/include/hexdump.h
 create mode 100644 ubifs-utils/include/io.h
 rename {mkfs.ubifs => ubifs-utils/include}/key.h (70%)
 create mode 100644 ubifs-utils/include/list.h
 create mode 100644 ubifs-utils/include/lprops.h
 rename {mkfs.ubifs => ubifs-utils/include}/lpt.h (80%)
 create mode 100644 ubifs-utils/include/scan.h
 rename {mkfs.ubifs => ubifs-utils/include}/ubifs.h (83%)
 create mode 100644 ubifs-utils/include/ubifs_common.h
 rename {mkfs.ubifs => ubifs-utils/lib}/compr.c (92%)
 rename {mkfs.ubifs => ubifs-utils/lib}/crc16.c (100%)
 rename {mkfs.ubifs => ubifs-utils/lib}/devtable.c (99%)
 rename {mkfs.ubifs/hashtable => ubifs-utils/lib}/hashtable.c (100%)
 rename {mkfs.ubifs/hashtable => ubifs-utils/lib}/hashtable_itr.c (100%)
 create mode 100644 ubifs-utils/lib/hexdump.c
 create mode 100644 ubifs-utils/lib/io.c
 create mode 100644 ubifs-utils/lib/lprops.c
 create mode 100644 ubifs-utils/lib/lpt.c
 create mode 100644 ubifs-utils/lib/scan.c
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/.gitignore (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/mkfs.ubifs.c (94%)
 create mode 100644 ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
 create mode 100644 ubifs-utils/ubifs_dump/ubifs_dump.c

-- 
1.8.4.2

^ permalink raw reply	[flat|nested] 44+ messages in thread

* [PATCH 01/27] mtd-utils: Restructure the mtd-utils source.
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 02/27] ubifs: pick some common definitions into ubifs_common.h Dongsheng Yang
                   ` (28 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

* There is no code modification in this commit, only moving
* the files to proper place.

The user tools looks a little messy as we place almost
the all tools in the root directory of mtd-utils. To make
it more clear, I propose to introduce the following structure
for our source code.

mtd-utils/
	|-- lib
	|-- include
	|-- misc-utils
	|-- flash-utils
	|-- jffsX-utils
	|-- nand-utils
	|-- nor-utils
	|-- ubi-utils
	|-- ubifs-utils
	`-- tests

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                                           | 66 +++++++++++++---------
 flash_erase.c => flash-utils/flash_erase.c         |  0
 flash_eraseall => flash-utils/flash_eraseall       |  0
 flash_lock.c => flash-utils/flash_lock.c           |  0
 flash_otp_dump.c => flash-utils/flash_otp_dump.c   |  0
 flash_otp_info.c => flash-utils/flash_otp_info.c   |  0
 flash_otp_lock.c => flash-utils/flash_otp_lock.c   |  0
 flash_otp_write.c => flash-utils/flash_otp_write.c |  0
 flash_unlock.c => flash-utils/flash_unlock.c       |  0
 flashcp.c => flash-utils/flashcp.c                 |  0
 compr.c => jffsX-utils/compr.c                     |  0
 compr.h => jffsX-utils/compr.h                     |  0
 compr_lzo.c => jffsX-utils/compr_lzo.c             |  0
 compr_rtime.c => jffsX-utils/compr_rtime.c         |  0
 compr_zlib.c => jffsX-utils/compr_zlib.c           |  0
 device_table.txt => jffsX-utils/device_table.txt   |  0
 jffs-dump.c => jffsX-utils/jffs-dump.c             |  0
 jffs2dump.c => jffsX-utils/jffs2dump.c             |  0
 jffs2reader.c => jffsX-utils/jffs2reader.c         |  0
 mkfs.jffs2.1 => jffsX-utils/mkfs.jffs2.1           |  0
 mkfs.jffs2.c => jffsX-utils/mkfs.jffs2.c           |  0
 rbtree.c => jffsX-utils/rbtree.c                   |  0
 rbtree.h => jffsX-utils/rbtree.h                   |  0
 summary.h => jffsX-utils/summary.h                 |  0
 sumtool.c => jffsX-utils/sumtool.c                 |  0
 MAKEDEV => misc-utils/MAKEDEV                      |  0
 doc_loadbios.c => misc-utils/doc_loadbios.c        |  0
 docfdisk.c => misc-utils/docfdisk.c                |  0
 fectest.c => misc-utils/fectest.c                  |  0
 ftl_check.c => misc-utils/ftl_check.c              |  0
 ftl_format.c => misc-utils/ftl_format.c            |  0
 mcast_image.h => misc-utils/mcast_image.h          |  0
 mtd_debug.c => misc-utils/mtd_debug.c              |  0
 mtdpart.c => misc-utils/mtdpart.c                  |  0
 recv_image.c => misc-utils/recv_image.c            |  0
 serve_image.c => misc-utils/serve_image.c          |  0
 load_nandsim.sh => nand-utils/load_nandsim.sh      |  0
 nanddump.c => nand-utils/nanddump.c                |  0
 nandtest.c => nand-utils/nandtest.c                |  0
 nandwrite.c => nand-utils/nandwrite.c              |  0
 nftl_format.c => nand-utils/nftl_format.c          |  0
 nftldump.c => nand-utils/nftldump.c                |  0
 rfddump.c => nor-utils/rfddump.c                   |  0
 rfdformat.c => nor-utils/rfdformat.c               |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/.gitignore  |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/COPYING     |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/README      |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/compr.c     |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/compr.h     |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/crc16.c     |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/crc16.h     |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/defs.h      |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/devtable.c  |  0
 .../mkfs.ubifs}/hashtable/hashtable.c              |  0
 .../mkfs.ubifs}/hashtable/hashtable.h              |  0
 .../mkfs.ubifs}/hashtable/hashtable_itr.c          |  0
 .../mkfs.ubifs}/hashtable/hashtable_itr.h          |  0
 .../mkfs.ubifs}/hashtable/hashtable_private.h      |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/key.h       |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/lpt.c       |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/lpt.h       |  0
 .../mkfs.ubifs}/mkfs.ubifs.c                       |  0
 .../mkfs.ubifs}/mkfs.ubifs.h                       |  0
 {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/ubifs.h     |  0
 64 files changed, 39 insertions(+), 27 deletions(-)
 rename flash_erase.c => flash-utils/flash_erase.c (100%)
 rename flash_eraseall => flash-utils/flash_eraseall (100%)
 rename flash_lock.c => flash-utils/flash_lock.c (100%)
 rename flash_otp_dump.c => flash-utils/flash_otp_dump.c (100%)
 rename flash_otp_info.c => flash-utils/flash_otp_info.c (100%)
 rename flash_otp_lock.c => flash-utils/flash_otp_lock.c (100%)
 rename flash_otp_write.c => flash-utils/flash_otp_write.c (100%)
 rename flash_unlock.c => flash-utils/flash_unlock.c (100%)
 rename flashcp.c => flash-utils/flashcp.c (100%)
 rename compr.c => jffsX-utils/compr.c (100%)
 rename compr.h => jffsX-utils/compr.h (100%)
 rename compr_lzo.c => jffsX-utils/compr_lzo.c (100%)
 rename compr_rtime.c => jffsX-utils/compr_rtime.c (100%)
 rename compr_zlib.c => jffsX-utils/compr_zlib.c (100%)
 rename device_table.txt => jffsX-utils/device_table.txt (100%)
 rename jffs-dump.c => jffsX-utils/jffs-dump.c (100%)
 rename jffs2dump.c => jffsX-utils/jffs2dump.c (100%)
 rename jffs2reader.c => jffsX-utils/jffs2reader.c (100%)
 rename mkfs.jffs2.1 => jffsX-utils/mkfs.jffs2.1 (100%)
 rename mkfs.jffs2.c => jffsX-utils/mkfs.jffs2.c (100%)
 rename rbtree.c => jffsX-utils/rbtree.c (100%)
 rename rbtree.h => jffsX-utils/rbtree.h (100%)
 rename summary.h => jffsX-utils/summary.h (100%)
 rename sumtool.c => jffsX-utils/sumtool.c (100%)
 rename MAKEDEV => misc-utils/MAKEDEV (100%)
 rename doc_loadbios.c => misc-utils/doc_loadbios.c (100%)
 rename docfdisk.c => misc-utils/docfdisk.c (100%)
 rename fectest.c => misc-utils/fectest.c (100%)
 rename ftl_check.c => misc-utils/ftl_check.c (100%)
 rename ftl_format.c => misc-utils/ftl_format.c (100%)
 rename mcast_image.h => misc-utils/mcast_image.h (100%)
 rename mtd_debug.c => misc-utils/mtd_debug.c (100%)
 rename mtdpart.c => misc-utils/mtdpart.c (100%)
 rename recv_image.c => misc-utils/recv_image.c (100%)
 rename serve_image.c => misc-utils/serve_image.c (100%)
 rename load_nandsim.sh => nand-utils/load_nandsim.sh (100%)
 rename nanddump.c => nand-utils/nanddump.c (100%)
 rename nandtest.c => nand-utils/nandtest.c (100%)
 rename nandwrite.c => nand-utils/nandwrite.c (100%)
 rename nftl_format.c => nand-utils/nftl_format.c (100%)
 rename nftldump.c => nand-utils/nftldump.c (100%)
 rename rfddump.c => nor-utils/rfddump.c (100%)
 rename rfdformat.c => nor-utils/rfdformat.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/.gitignore (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/COPYING (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/README (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/compr.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/compr.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/crc16.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/crc16.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/defs.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/devtable.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/hashtable/hashtable.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/hashtable/hashtable.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/hashtable/hashtable_itr.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/hashtable/hashtable_itr.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/hashtable/hashtable_private.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/key.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/lpt.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/lpt.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/mkfs.ubifs.c (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/mkfs.ubifs.h (100%)
 rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/ubifs.h (100%)

diff --git a/Makefile b/Makefile
index f4ce313..bb40929 100644
--- a/Makefile
+++ b/Makefile
@@ -16,24 +16,32 @@ endif
 
 TESTS = tests
 
-MTD_BINS = \
-	ftl_format flash_erase nanddump doc_loadbios \
-	ftl_check mkfs.jffs2 flash_lock flash_unlock \
-	flash_otp_info flash_otp_dump flash_otp_lock flash_otp_write \
-	mtd_debug flashcp nandwrite nandtest mtdpart \
-	jffs2dump \
-	nftldump nftl_format docfdisk \
-	rfddump rfdformat \
-	serve_image recv_image \
-	sumtool jffs2reader
+MISC_BINS = \
+	ftl_format doc_loadbios ftl_check mtd_debug docfdisk \
+	serve_image recv_image mtdpart
 UBI_BINS = \
 	ubiupdatevol ubimkvol ubirmvol ubicrc32 ubinfo ubiattach \
 	ubidetach ubinize ubiformat ubirename mtdinfo ubirsvol ubiblock
-
-BINS = $(MTD_BINS)
-BINS += mkfs.ubifs/mkfs.ubifs
+UBIFS_BINS = \
+	mkfs.ubifs/mkfs.ubifs
+JFFSX_BINS = \
+	mkfs.jffs2 sumtool jffs2reader jffs2dump
+FLASH_BINS = \
+	flash_erase flash_lock flash_unlock flash_otp_info flash_otp_dump \
+	flash_otp_lock flash_otp_write flashcp
+NAND_BINS = \
+	nanddump nandwrite nandtest nftldump nftl_format
+NOR_BINS = \
+	rfddump rfdformat
+
+BINS = $(addprefix misc-utils/,$(MISC_BINS))
 BINS += $(addprefix ubi-utils/,$(UBI_BINS))
-SCRIPTS = flash_eraseall
+BINS += $(addprefix ubifs-utils/,$(UBIFS_BINS))
+BINS += $(addprefix jffsX-utils/,$(JFFSX_BINS))
+BINS += $(addprefix flash-utils/,$(FLASH_BINS))
+BINS += $(addprefix nand-utils/,$(NAND_BINS))
+BINS += $(addprefix nor-utils/,$(NOR_BINS))
+SCRIPTS = $(addprefix flash-utils/,flash_eraseall)
 
 TARGETS = $(BINS)
 TARGETS += lib/libmtd.a
@@ -61,11 +69,11 @@ endif
 	rm -f $(BUILDDIR)/include/version.h
 	$(MAKE) -C $(TESTS) clean
 
-install:: $(addprefix $(BUILDDIR)/,${BINS}) ${SCRIPTS}
+install:: $(addprefix $(BUILDDIR)/,${BINS} ${SCRIPTS})
 	mkdir -p ${DESTDIR}/${SBINDIR}
 	install -m 0755 $^ ${DESTDIR}/${SBINDIR}/
 	mkdir -p ${DESTDIR}/${MANDIR}/man1
-	install -m 0644 mkfs.jffs2.1 ${DESTDIR}/${MANDIR}/man1/
+	install -m 0644 jffsX-utils/mkfs.jffs2.1 ${DESTDIR}/${MANDIR}/man1/
 	-gzip -9f ${DESTDIR}/${MANDIR}/man1/*.1
 
 tests::
@@ -85,13 +93,17 @@ $(BUILDDIR)/include/version.h.tmp:
 # Utils in top level
 #
 obj-mkfs.jffs2 = compr_rtime.o compr_zlib.o compr_lzo.o compr.o rbtree.o
-LDFLAGS_mkfs.jffs2 = $(ZLIBLDFLAGS) $(LZOLDFLAGS)
+LDFLAGS_mkfs.jffs2 = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(CPPFLAGS)
 LDLIBS_mkfs.jffs2  = -lz $(LZOLDLIBS)
 
 LDFLAGS_jffs2reader = $(ZLIBLDFLAGS) $(LZOLDFLAGS)
 LDLIBS_jffs2reader  = -lz $(LZOLDLIBS)
 
-$(foreach v,$(MTD_BINS),$(eval $(call mkdep,,$(v))))
+$(foreach v,$(MISC_BINS),$(eval $(call mkdep,misc-utils/,$(v))))
+$(foreach v,$(JFFSX_BINS),$(eval $(call mkdep,jffsX-utils/,$(v))))
+$(foreach v,$(FLASH_BINS),$(eval $(call mkdep,flash-utils/,$(v))))
+$(foreach v,$(NAND_BINS),$(eval $(call mkdep,nand-utils/,$(v))))
+$(foreach v,$(NOR_BINS),$(eval $(call mkdep,nor-utils/,$(v))))
 
 #
 # Common libmtd
@@ -100,15 +112,6 @@ obj-libmtd.a = libmtd.o libmtd_legacy.o libcrc32.o libfec.o
 $(call _mkdep,lib/,libmtd.a)
 
 #
-# Utils in mkfs.ubifs subdir
-#
-obj-mkfs.ubifs = crc16.o lpt.o compr.o devtable.o \
-	hashtable/hashtable.o hashtable/hashtable_itr.o
-LDFLAGS_mkfs.ubifs = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
-LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid
-$(call mkdep,mkfs.ubifs/,mkfs.ubifs,,ubi-utils/libubi.a)
-
-#
 # Utils in ubi-utils/ subdir
 #
 obj-libiniparser.a = libiniparser.o dictionary.o
@@ -122,3 +125,12 @@ obj-ubiformat = libubigen.a libscan.a
 
 $(foreach v,libubi.a libubigen.a libiniparser.a libscan.a,$(eval $(call _mkdep,ubi-utils/,$(v))))
 $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-common.o)))
+
+#
+# Utils in ubifs-utils subdir
+#
+obj-mkfs.ubifs = crc16.o lpt.o compr.o devtable.o \
+	hashtable/hashtable.o hashtable/hashtable_itr.o
+LDFLAGS_mkfs.ubifs = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
+LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid
+$(call mkdep,ubifs-utils/mkfs.ubifs/,mkfs.ubifs,,ubi-utils/libubi.a)
diff --git a/flash_erase.c b/flash-utils/flash_erase.c
similarity index 100%
rename from flash_erase.c
rename to flash-utils/flash_erase.c
diff --git a/flash_eraseall b/flash-utils/flash_eraseall
similarity index 100%
rename from flash_eraseall
rename to flash-utils/flash_eraseall
diff --git a/flash_lock.c b/flash-utils/flash_lock.c
similarity index 100%
rename from flash_lock.c
rename to flash-utils/flash_lock.c
diff --git a/flash_otp_dump.c b/flash-utils/flash_otp_dump.c
similarity index 100%
rename from flash_otp_dump.c
rename to flash-utils/flash_otp_dump.c
diff --git a/flash_otp_info.c b/flash-utils/flash_otp_info.c
similarity index 100%
rename from flash_otp_info.c
rename to flash-utils/flash_otp_info.c
diff --git a/flash_otp_lock.c b/flash-utils/flash_otp_lock.c
similarity index 100%
rename from flash_otp_lock.c
rename to flash-utils/flash_otp_lock.c
diff --git a/flash_otp_write.c b/flash-utils/flash_otp_write.c
similarity index 100%
rename from flash_otp_write.c
rename to flash-utils/flash_otp_write.c
diff --git a/flash_unlock.c b/flash-utils/flash_unlock.c
similarity index 100%
rename from flash_unlock.c
rename to flash-utils/flash_unlock.c
diff --git a/flashcp.c b/flash-utils/flashcp.c
similarity index 100%
rename from flashcp.c
rename to flash-utils/flashcp.c
diff --git a/compr.c b/jffsX-utils/compr.c
similarity index 100%
rename from compr.c
rename to jffsX-utils/compr.c
diff --git a/compr.h b/jffsX-utils/compr.h
similarity index 100%
rename from compr.h
rename to jffsX-utils/compr.h
diff --git a/compr_lzo.c b/jffsX-utils/compr_lzo.c
similarity index 100%
rename from compr_lzo.c
rename to jffsX-utils/compr_lzo.c
diff --git a/compr_rtime.c b/jffsX-utils/compr_rtime.c
similarity index 100%
rename from compr_rtime.c
rename to jffsX-utils/compr_rtime.c
diff --git a/compr_zlib.c b/jffsX-utils/compr_zlib.c
similarity index 100%
rename from compr_zlib.c
rename to jffsX-utils/compr_zlib.c
diff --git a/device_table.txt b/jffsX-utils/device_table.txt
similarity index 100%
rename from device_table.txt
rename to jffsX-utils/device_table.txt
diff --git a/jffs-dump.c b/jffsX-utils/jffs-dump.c
similarity index 100%
rename from jffs-dump.c
rename to jffsX-utils/jffs-dump.c
diff --git a/jffs2dump.c b/jffsX-utils/jffs2dump.c
similarity index 100%
rename from jffs2dump.c
rename to jffsX-utils/jffs2dump.c
diff --git a/jffs2reader.c b/jffsX-utils/jffs2reader.c
similarity index 100%
rename from jffs2reader.c
rename to jffsX-utils/jffs2reader.c
diff --git a/mkfs.jffs2.1 b/jffsX-utils/mkfs.jffs2.1
similarity index 100%
rename from mkfs.jffs2.1
rename to jffsX-utils/mkfs.jffs2.1
diff --git a/mkfs.jffs2.c b/jffsX-utils/mkfs.jffs2.c
similarity index 100%
rename from mkfs.jffs2.c
rename to jffsX-utils/mkfs.jffs2.c
diff --git a/rbtree.c b/jffsX-utils/rbtree.c
similarity index 100%
rename from rbtree.c
rename to jffsX-utils/rbtree.c
diff --git a/rbtree.h b/jffsX-utils/rbtree.h
similarity index 100%
rename from rbtree.h
rename to jffsX-utils/rbtree.h
diff --git a/summary.h b/jffsX-utils/summary.h
similarity index 100%
rename from summary.h
rename to jffsX-utils/summary.h
diff --git a/sumtool.c b/jffsX-utils/sumtool.c
similarity index 100%
rename from sumtool.c
rename to jffsX-utils/sumtool.c
diff --git a/MAKEDEV b/misc-utils/MAKEDEV
similarity index 100%
rename from MAKEDEV
rename to misc-utils/MAKEDEV
diff --git a/doc_loadbios.c b/misc-utils/doc_loadbios.c
similarity index 100%
rename from doc_loadbios.c
rename to misc-utils/doc_loadbios.c
diff --git a/docfdisk.c b/misc-utils/docfdisk.c
similarity index 100%
rename from docfdisk.c
rename to misc-utils/docfdisk.c
diff --git a/fectest.c b/misc-utils/fectest.c
similarity index 100%
rename from fectest.c
rename to misc-utils/fectest.c
diff --git a/ftl_check.c b/misc-utils/ftl_check.c
similarity index 100%
rename from ftl_check.c
rename to misc-utils/ftl_check.c
diff --git a/ftl_format.c b/misc-utils/ftl_format.c
similarity index 100%
rename from ftl_format.c
rename to misc-utils/ftl_format.c
diff --git a/mcast_image.h b/misc-utils/mcast_image.h
similarity index 100%
rename from mcast_image.h
rename to misc-utils/mcast_image.h
diff --git a/mtd_debug.c b/misc-utils/mtd_debug.c
similarity index 100%
rename from mtd_debug.c
rename to misc-utils/mtd_debug.c
diff --git a/mtdpart.c b/misc-utils/mtdpart.c
similarity index 100%
rename from mtdpart.c
rename to misc-utils/mtdpart.c
diff --git a/recv_image.c b/misc-utils/recv_image.c
similarity index 100%
rename from recv_image.c
rename to misc-utils/recv_image.c
diff --git a/serve_image.c b/misc-utils/serve_image.c
similarity index 100%
rename from serve_image.c
rename to misc-utils/serve_image.c
diff --git a/load_nandsim.sh b/nand-utils/load_nandsim.sh
similarity index 100%
rename from load_nandsim.sh
rename to nand-utils/load_nandsim.sh
diff --git a/nanddump.c b/nand-utils/nanddump.c
similarity index 100%
rename from nanddump.c
rename to nand-utils/nanddump.c
diff --git a/nandtest.c b/nand-utils/nandtest.c
similarity index 100%
rename from nandtest.c
rename to nand-utils/nandtest.c
diff --git a/nandwrite.c b/nand-utils/nandwrite.c
similarity index 100%
rename from nandwrite.c
rename to nand-utils/nandwrite.c
diff --git a/nftl_format.c b/nand-utils/nftl_format.c
similarity index 100%
rename from nftl_format.c
rename to nand-utils/nftl_format.c
diff --git a/nftldump.c b/nand-utils/nftldump.c
similarity index 100%
rename from nftldump.c
rename to nand-utils/nftldump.c
diff --git a/rfddump.c b/nor-utils/rfddump.c
similarity index 100%
rename from rfddump.c
rename to nor-utils/rfddump.c
diff --git a/rfdformat.c b/nor-utils/rfdformat.c
similarity index 100%
rename from rfdformat.c
rename to nor-utils/rfdformat.c
diff --git a/mkfs.ubifs/.gitignore b/ubifs-utils/mkfs.ubifs/.gitignore
similarity index 100%
rename from mkfs.ubifs/.gitignore
rename to ubifs-utils/mkfs.ubifs/.gitignore
diff --git a/mkfs.ubifs/COPYING b/ubifs-utils/mkfs.ubifs/COPYING
similarity index 100%
rename from mkfs.ubifs/COPYING
rename to ubifs-utils/mkfs.ubifs/COPYING
diff --git a/mkfs.ubifs/README b/ubifs-utils/mkfs.ubifs/README
similarity index 100%
rename from mkfs.ubifs/README
rename to ubifs-utils/mkfs.ubifs/README
diff --git a/mkfs.ubifs/compr.c b/ubifs-utils/mkfs.ubifs/compr.c
similarity index 100%
rename from mkfs.ubifs/compr.c
rename to ubifs-utils/mkfs.ubifs/compr.c
diff --git a/mkfs.ubifs/compr.h b/ubifs-utils/mkfs.ubifs/compr.h
similarity index 100%
rename from mkfs.ubifs/compr.h
rename to ubifs-utils/mkfs.ubifs/compr.h
diff --git a/mkfs.ubifs/crc16.c b/ubifs-utils/mkfs.ubifs/crc16.c
similarity index 100%
rename from mkfs.ubifs/crc16.c
rename to ubifs-utils/mkfs.ubifs/crc16.c
diff --git a/mkfs.ubifs/crc16.h b/ubifs-utils/mkfs.ubifs/crc16.h
similarity index 100%
rename from mkfs.ubifs/crc16.h
rename to ubifs-utils/mkfs.ubifs/crc16.h
diff --git a/mkfs.ubifs/defs.h b/ubifs-utils/mkfs.ubifs/defs.h
similarity index 100%
rename from mkfs.ubifs/defs.h
rename to ubifs-utils/mkfs.ubifs/defs.h
diff --git a/mkfs.ubifs/devtable.c b/ubifs-utils/mkfs.ubifs/devtable.c
similarity index 100%
rename from mkfs.ubifs/devtable.c
rename to ubifs-utils/mkfs.ubifs/devtable.c
diff --git a/mkfs.ubifs/hashtable/hashtable.c b/ubifs-utils/mkfs.ubifs/hashtable/hashtable.c
similarity index 100%
rename from mkfs.ubifs/hashtable/hashtable.c
rename to ubifs-utils/mkfs.ubifs/hashtable/hashtable.c
diff --git a/mkfs.ubifs/hashtable/hashtable.h b/ubifs-utils/mkfs.ubifs/hashtable/hashtable.h
similarity index 100%
rename from mkfs.ubifs/hashtable/hashtable.h
rename to ubifs-utils/mkfs.ubifs/hashtable/hashtable.h
diff --git a/mkfs.ubifs/hashtable/hashtable_itr.c b/ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.c
similarity index 100%
rename from mkfs.ubifs/hashtable/hashtable_itr.c
rename to ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.c
diff --git a/mkfs.ubifs/hashtable/hashtable_itr.h b/ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.h
similarity index 100%
rename from mkfs.ubifs/hashtable/hashtable_itr.h
rename to ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.h
diff --git a/mkfs.ubifs/hashtable/hashtable_private.h b/ubifs-utils/mkfs.ubifs/hashtable/hashtable_private.h
similarity index 100%
rename from mkfs.ubifs/hashtable/hashtable_private.h
rename to ubifs-utils/mkfs.ubifs/hashtable/hashtable_private.h
diff --git a/mkfs.ubifs/key.h b/ubifs-utils/mkfs.ubifs/key.h
similarity index 100%
rename from mkfs.ubifs/key.h
rename to ubifs-utils/mkfs.ubifs/key.h
diff --git a/mkfs.ubifs/lpt.c b/ubifs-utils/mkfs.ubifs/lpt.c
similarity index 100%
rename from mkfs.ubifs/lpt.c
rename to ubifs-utils/mkfs.ubifs/lpt.c
diff --git a/mkfs.ubifs/lpt.h b/ubifs-utils/mkfs.ubifs/lpt.h
similarity index 100%
rename from mkfs.ubifs/lpt.h
rename to ubifs-utils/mkfs.ubifs/lpt.h
diff --git a/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
similarity index 100%
rename from mkfs.ubifs/mkfs.ubifs.c
rename to ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
diff --git a/mkfs.ubifs/mkfs.ubifs.h b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
similarity index 100%
rename from mkfs.ubifs/mkfs.ubifs.h
rename to ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
diff --git a/mkfs.ubifs/ubifs.h b/ubifs-utils/mkfs.ubifs/ubifs.h
similarity index 100%
rename from mkfs.ubifs/ubifs.h
rename to ubifs-utils/mkfs.ubifs/ubifs.h
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 02/27] ubifs: pick some common definitions into ubifs_common.h
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 01/27] mtd-utils: Restructure the mtd-utils source Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 03/27] ubifs: move the all io related code into io.[h|c] Dongsheng Yang
                   ` (27 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Currently, lpt.c is including mkfs.ubifs.h. That make
the lpt depend on mkfs.ubifs. It's not good if we want
to introduce some more tools for ubifs, such as fsck.ubifs.

This patch start to cut off the dependence from libs, such
as lpt, devtable, to mkfs.ubifs.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.h   | 46 +-------------------------------
 ubifs-utils/mkfs.ubifs/ubifs_common.h | 50 +++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 45 deletions(-)
 create mode 100644 ubifs-utils/mkfs.ubifs/ubifs_common.h

diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
index 944a159..d6d46a2 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
@@ -23,37 +23,12 @@
 #ifndef __MKFS_UBIFS_H__
 #define __MKFS_UBIFS_H__
 
-#include <unistd.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <stdint.h>
-#include <endian.h>
-#include <byteswap.h>
-#include <linux/types.h>
-#include <linux/fs.h>
-
-#include <getopt.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <fcntl.h>
-#include <dirent.h>
-#include <errno.h>
-#include <libgen.h>
-#include <ctype.h>
-#include <uuid/uuid.h>
-#include <sys/file.h>
-
-#include <mtd/ubifs-media.h>
+#include "ubifs_common.h"
 
 /* common.h requires the PROGRAM_NAME macro */
 #define PROGRAM_NAME "mkfs.ubifs"
 #include "common.h"
 
-#include "libubi.h"
-#include "defs.h"
 #include "crc16.h"
 #include "ubifs.h"
 #include "key.h"
@@ -74,25 +49,6 @@
 #error MKFS_UBIFS_COMPR_ZLIB != UBIFS_COMPR_ZLIB
 #endif
 
-extern int verbose;
-extern int debug_level;
-
-#define dbg_msg(lvl, fmt, ...) do {if (debug_level >= lvl)                \
-	printf("mkfs.ubifs: %s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__); \
-} while(0)
-
-#define err_msg(fmt, ...) ({                                \
-	fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__); \
-	-1;                                                 \
-})
-
-#define sys_err_msg(fmt, ...) ({                                         \
-	int err_ = errno;                                                \
-	fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__);              \
-	fprintf(stderr, "       %s (error %d)\n", strerror(err_), err_); \
-	-1;                                                              \
-})
-
 /**
  * struct path_htbl_element - an element of the path hash table.
  * @path: the UBIFS path the element describes (the key of the element)
diff --git a/ubifs-utils/mkfs.ubifs/ubifs_common.h b/ubifs-utils/mkfs.ubifs/ubifs_common.h
new file mode 100644
index 0000000..958c20a
--- /dev/null
+++ b/ubifs-utils/mkfs.ubifs/ubifs_common.h
@@ -0,0 +1,50 @@
+#ifndef __UBIFS_COMMON_H__
+#define __UBIFS_COMMON_H__
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <stdint.h>
+#include <endian.h>
+#include <byteswap.h>
+#include <linux/types.h>
+#include <linux/fs.h>
+
+#include <getopt.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <errno.h>
+#include <libgen.h>
+#include <ctype.h>
+#include <uuid/uuid.h>
+#include <sys/file.h>
+
+#include <mtd/ubifs-media.h>
+
+#include "libubi.h"
+#include "defs.h"
+
+extern int verbose;
+extern int debug_level;
+
+#define dbg_msg(lvl, fmt, ...) do {if (debug_level >= lvl)                \
+	printf("ubifs: %s: " fmt "\n", __FUNCTION__, ##__VA_ARGS__); \
+} while(0)
+
+#define err_msg(fmt, ...) ({                                \
+	fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__); \
+	-1;                                                 \
+})
+
+#define sys_err_msg(fmt, ...) ({                                         \
+	int err_ = errno;                                                \
+	fprintf(stderr, "Error: " fmt "\n", ##__VA_ARGS__);              \
+	fprintf(stderr, "       %s (error %d)\n", strerror(err_), err_); \
+	-1;                                                              \
+})
+#endif
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 03/27] ubifs: move the all io related code into io.[h|c]
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 01/27] mtd-utils: Restructure the mtd-utils source Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 02/27] ubifs: pick some common definitions into ubifs_common.h Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 04/27] ubifs: remove the including of mkfs.ubifs.h in lpt.c Dongsheng Yang
                   ` (26 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

To cut off the dependence from common lib to mkfs.ubifs,
this patch refactor the io related functions into a new
lib named as io.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                            |  2 +-
 ubifs-utils/mkfs.ubifs/io.c         | 33 +++++++++++++++++++++++++++++++++
 ubifs-utils/mkfs.ubifs/io.h         | 15 +++++++++++++++
 ubifs-utils/mkfs.ubifs/lpt.c        | 10 +++++-----
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 34 +++-------------------------------
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.h |  2 +-
 6 files changed, 58 insertions(+), 38 deletions(-)
 create mode 100644 ubifs-utils/mkfs.ubifs/io.c
 create mode 100644 ubifs-utils/mkfs.ubifs/io.h

diff --git a/Makefile b/Makefile
index bb40929..843108c 100644
--- a/Makefile
+++ b/Makefile
@@ -129,7 +129,7 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 #
 # Utils in ubifs-utils subdir
 #
-obj-mkfs.ubifs = crc16.o lpt.o compr.o devtable.o \
+obj-mkfs.ubifs = crc16.o lpt.o compr.o devtable.o io.o\
 	hashtable/hashtable.o hashtable/hashtable_itr.o
 LDFLAGS_mkfs.ubifs = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid
diff --git a/ubifs-utils/mkfs.ubifs/io.c b/ubifs-utils/mkfs.ubifs/io.c
new file mode 100644
index 0000000..7aba0a6
--- /dev/null
+++ b/ubifs-utils/mkfs.ubifs/io.c
@@ -0,0 +1,33 @@
+#include "io.h"
+#define PROGRAM_NAME "ubifs-io"
+#include <common.h>
+
+int out_fd;
+int out_ubi;
+libubi_t ubi;
+
+/**
+ * write_leb - copy the image of a LEB to the output target.
+ * @lnum: LEB number
+ * @len: length of data in the buffer
+ * @buf: buffer (must be at least c->leb_size bytes)
+ */
+int write_leb(struct ubifs_info *c, int lnum, int len, void *buf)
+{
+	off_t pos = (off_t)lnum * c->leb_size;
+
+	dbg_msg(3, "LEB %d len %d", lnum, len);
+	memset(buf + len, 0xff, c->leb_size - len);
+	if (out_ubi)
+		if (ubi_leb_change_start(ubi, out_fd, lnum, c->leb_size))
+			return sys_err_msg("ubi_leb_change_start failed");
+
+	if (lseek(out_fd, pos, SEEK_SET) != pos)
+		return sys_err_msg("lseek failed seeking %"PRIdoff_t, pos);
+
+	if (write(out_fd, buf, c->leb_size) != c->leb_size)
+		return sys_err_msg("write failed writing %d bytes at pos %"PRIdoff_t,
+				   c->leb_size, pos);
+
+	return 0;
+}
diff --git a/ubifs-utils/mkfs.ubifs/io.h b/ubifs-utils/mkfs.ubifs/io.h
new file mode 100644
index 0000000..e24d0c6
--- /dev/null
+++ b/ubifs-utils/mkfs.ubifs/io.h
@@ -0,0 +1,15 @@
+/**
+ * Header file for the io to ubi volume
+ */
+#ifndef __UBIFS_IO_H__
+#define __UBIFS_IO_H__
+
+#include "ubifs_common.h"
+#include "ubifs.h"
+
+extern int out_fd;
+extern int out_ubi;
+extern libubi_t ubi;
+
+int write_leb(struct ubifs_info *c, int lnum, int len, void *buf);
+#endif
diff --git a/ubifs-utils/mkfs.ubifs/lpt.c b/ubifs-utils/mkfs.ubifs/lpt.c
index 6aa0b88..cee221c 100644
--- a/ubifs-utils/mkfs.ubifs/lpt.c
+++ b/ubifs-utils/mkfs.ubifs/lpt.c
@@ -410,7 +410,7 @@ int create_lpt(struct ubifs_info *c)
 			alen = ALIGN(len, c->min_io_size);
 			set_ltab(c, lnum, c->leb_size - alen, alen - len);
 			memset(p, 0xff, alen - len);
-			err = write_leb(lnum++, alen, buf);
+			err = write_leb(c, lnum++, alen, buf);
 			if (err)
 				goto out;
 			p = buf;
@@ -452,7 +452,7 @@ int create_lpt(struct ubifs_info *c)
 				set_ltab(c, lnum, c->leb_size - alen,
 					    alen - len);
 				memset(p, 0xff, alen - len);
-				err = write_leb(lnum++, alen, buf);
+				err = write_leb(c, lnum++, alen, buf);
 				if (err)
 					goto out;
 				p = buf;
@@ -499,7 +499,7 @@ int create_lpt(struct ubifs_info *c)
 			alen = ALIGN(len, c->min_io_size);
 			set_ltab(c, lnum, c->leb_size - alen, alen - len);
 			memset(p, 0xff, alen - len);
-			err = write_leb(lnum++, alen, buf);
+			err = write_leb(c, lnum++, alen, buf);
 			if (err)
 				goto out;
 			p = buf;
@@ -522,7 +522,7 @@ int create_lpt(struct ubifs_info *c)
 		alen = ALIGN(len, c->min_io_size);
 		set_ltab(c, lnum, c->leb_size - alen, alen - len);
 		memset(p, 0xff, alen - len);
-		err = write_leb(lnum++, alen, buf);
+		err = write_leb(c, lnum++, alen, buf);
 		if (err)
 			goto out;
 		p = buf;
@@ -542,7 +542,7 @@ int create_lpt(struct ubifs_info *c)
 
 	/* Write remaining buffer */
 	memset(p, 0xff, alen - len);
-	err = write_leb(lnum, alen, buf);
+	err = write_leb(c, lnum, alen, buf);
 	if (err)
 		goto out;
 
diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
index ca17e2b..8e7e4b8 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
@@ -97,7 +97,6 @@ struct inum_mapping {
  */
 struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
-static libubi_t ubi;
 
 /* Debug levels are: 0 (none), 1 (statistics), 2 (files) ,3 (more details) */
 int debug_level;
@@ -108,8 +107,6 @@ static char *root;
 static int root_len;
 static struct stat root_st;
 static char *output;
-static int out_fd;
-static int out_ubi;
 static int squash_owner;
 
 /* The 'head' (position) which nodes are written */
@@ -739,31 +736,6 @@ static void prepare_node(void *node, int len)
 	ch->crc = cpu_to_le32(crc);
 }
 
-/**
- * write_leb - copy the image of a LEB to the output target.
- * @lnum: LEB number
- * @len: length of data in the buffer
- * @buf: buffer (must be at least c->leb_size bytes)
- */
-int write_leb(int lnum, int len, void *buf)
-{
-	off_t pos = (off_t)lnum * c->leb_size;
-
-	dbg_msg(3, "LEB %d len %d", lnum, len);
-	memset(buf + len, 0xff, c->leb_size - len);
-	if (out_ubi)
-		if (ubi_leb_change_start(ubi, out_fd, lnum, c->leb_size))
-			return sys_err_msg("ubi_leb_change_start failed");
-
-	if (lseek(out_fd, pos, SEEK_SET) != pos)
-		return sys_err_msg("lseek failed seeking %"PRIdoff_t, pos);
-
-	if (write(out_fd, buf, c->leb_size) != c->leb_size)
-		return sys_err_msg("write failed writing %d bytes at pos %"PRIdoff_t,
-				   c->leb_size, pos);
-
-	return 0;
-}
 
 /**
  * write_empty_leb - copy the image of an empty LEB to the output target.
@@ -771,7 +743,7 @@ int write_leb(int lnum, int len, void *buf)
  */
 static int write_empty_leb(int lnum)
 {
-	return write_leb(lnum, 0, leb_buf);
+	return write_leb(c, lnum, 0, leb_buf);
 }
 
 /**
@@ -827,7 +799,7 @@ static int write_node(void *node, int len, int lnum)
 
 	len = do_pad(leb_buf, len);
 
-	return write_leb(lnum, len, leb_buf);
+	return write_leb(c, lnum, len, leb_buf);
 }
 
 /**
@@ -939,7 +911,7 @@ static int flush_nodes(void)
 	if (!head_offs)
 		return 0;
 	len = do_pad(leb_buf, head_offs);
-	err = write_leb(head_lnum, len, leb_buf);
+	err = write_leb(c, head_lnum, len, leb_buf);
 	if (err)
 		return err;
 	set_lprops(head_lnum, head_offs, head_flags);
diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
index d6d46a2..ba646a6 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
@@ -34,6 +34,7 @@
 #include "key.h"
 #include "lpt.h"
 #include "compr.h"
+#include "io.h"
 
 /*
  * Compression flags are duplicated so that compr.c can compile without ubifs.h.
@@ -88,7 +89,6 @@ extern struct ubifs_info info_;
 
 struct hashtable_itr;
 
-int write_leb(int lnum, int len, void *buf);
 int parse_devtable(const char *tbl_file);
 struct path_htbl_element *devtbl_find_path(const char *path);
 struct name_htbl_element *devtbl_find_name(struct path_htbl_element *ph_elt,
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 04/27] ubifs: remove the including of mkfs.ubifs.h in lpt.c
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (2 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 03/27] ubifs: move the all io related code into io.[h|c] Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 05/27] ubifs: cut off the dependence from compr.o to mkfs.ubifs Dongsheng Yang
                   ` (25 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

As we have a ubifs_common.h and a seperate io lib, then
remove the including of mkfs.ubifs.h in lpt.c

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/mkfs.ubifs/lpt.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/ubifs-utils/mkfs.ubifs/lpt.c b/ubifs-utils/mkfs.ubifs/lpt.c
index cee221c..100d747 100644
--- a/ubifs-utils/mkfs.ubifs/lpt.c
+++ b/ubifs-utils/mkfs.ubifs/lpt.c
@@ -20,7 +20,16 @@
  *          Artem Bityutskiy
  */
 
-#include "mkfs.ubifs.h"
+#include "ubifs_common.h"
+
+/* common.h requires the PROGRAM_NAME macro */
+#define PROGRAM_NAME "ubifs-lpt"
+#include "common.h"
+
+#include "crc16.h"
+#include "ubifs.h"
+#include "lpt.h"
+#include "io.h"
 
 /**
  * do_calc_lpt_geom - calculate sizes for the LPT area.
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 05/27] ubifs: cut off the dependence from compr.o to mkfs.ubifs
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (3 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 04/27] ubifs: remove the including of mkfs.ubifs.h in lpt.c Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 06/27] ubifs: cut off the dependence from devtable to mkfs.ubifs.h Dongsheng Yang
                   ` (24 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

remove the including of mkfs.ubifs.h in compr lib and
drop the reference of globle variable info_ from compr to mkfs.ubifs.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/mkfs.ubifs/compr.c      | 20 +++++++-------------
 ubifs-utils/mkfs.ubifs/compr.h      |  3 +--
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.c |  8 ++++++--
 3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/ubifs-utils/mkfs.ubifs/compr.c b/ubifs-utils/mkfs.ubifs/compr.c
index 34b2f60..35cc447 100644
--- a/ubifs-utils/mkfs.ubifs/compr.c
+++ b/ubifs-utils/mkfs.ubifs/compr.c
@@ -20,23 +20,17 @@
  *          Zoltan Sogor
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <stdint.h>
-#include <string.h>
+#include "ubifs_common.h"
 #include <lzo/lzo1x.h>
-#include <linux/types.h>
 
 #define crc32 __zlib_crc32
 #include <zlib.h>
 #undef crc32
 
 #include "compr.h"
-#include "mkfs.ubifs.h"
 
 static void *lzo_mem;
 static unsigned long long errcnt = 0;
-static struct ubifs_info *c = &info_;
 
 #define DEFLATE_DEF_LEVEL     Z_DEFAULT_COMPRESSION
 #define DEFLATE_DEF_WINBITS   11
@@ -114,7 +108,7 @@ static int no_compress(void *in_buf, size_t in_len, void *out_buf,
 static char *zlib_buf;
 
 static int favor_lzo_compress(void *in_buf, size_t in_len, void *out_buf,
-			       size_t *out_len, int *type)
+				size_t *out_len, int *type, int lzo_percent)
 {
 	int lzo_ret, zlib_ret;
 	size_t lzo_len, zlib_len;
@@ -136,7 +130,7 @@ static int favor_lzo_compress(void *in_buf, size_t in_len, void *out_buf,
 
 		percent = (double)zlib_len / (double)lzo_len;
 		percent *= 100;
-		if (percent > 100 - c->favor_percent)
+		if (percent > 100 - lzo_percent)
 			goto select_lzo;
 		goto select_zlib;
 	}
@@ -159,8 +153,8 @@ select_zlib:
 	return 0;
 }
 
-int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
-		  int type)
+int compress_data(void *in_buf, size_t in_len, void *out_buf,
+		size_t *out_len, int type, int lzo_percent)
 {
 	int ret;
 
@@ -169,8 +163,8 @@ int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
 		return MKFS_UBIFS_COMPR_NONE;
 	}
 
-	if (c->favor_lzo)
-		ret = favor_lzo_compress(in_buf, in_len, out_buf, out_len, &type);
+	if (lzo_percent)
+		ret = favor_lzo_compress(in_buf, in_len, out_buf, out_len, &type, lzo_percent);
 	else {
 		switch (type) {
 		case MKFS_UBIFS_COMPR_LZO:
diff --git a/ubifs-utils/mkfs.ubifs/compr.h b/ubifs-utils/mkfs.ubifs/compr.h
index e3dd95c..d44a2ba 100644
--- a/ubifs-utils/mkfs.ubifs/compr.h
+++ b/ubifs-utils/mkfs.ubifs/compr.h
@@ -38,8 +38,7 @@ enum compression_type
 	MKFS_UBIFS_COMPR_ZLIB,
 };
 
-int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len,
-		  int type);
+int compress_data(void *in_buf, size_t in_len, void *out_buf, size_t *out_len, int type, int lzo_percent);
 int init_compression(void);
 void destroy_compression(void);
 
diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
index 8e7e4b8..9795282 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
@@ -1251,8 +1251,12 @@ static int add_file(const char *path_name, struct stat *st, ino_t inum,
 			use_compr = UBIFS_COMPR_LZO;
 		else
 			use_compr = c->default_compr;
-		compr_type = compress_data(buf, bytes_read, &dn->data,
-					   &out_len, use_compr);
+		if (c->favor_lzo)
+			compr_type = compress_data(buf, bytes_read, &dn->data,
+					   &out_len, use_compr, c->favor_percent);
+		else
+			compr_type = compress_data(buf, bytes_read, &dn->data,
+					   &out_len, use_compr, 0);
 		dn->compr_type = cpu_to_le16(compr_type);
 		dn_len = UBIFS_DATA_NODE_SZ + out_len;
 		/* Add data node to file system */
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 06/27] ubifs: cut off the dependence from devtable to mkfs.ubifs.h
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (4 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 05/27] ubifs: cut off the dependence from compr.o to mkfs.ubifs Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 07/27] ubifs: introduce ubifs-utils/include and ubifs-utils/lib Dongsheng Yang
                   ` (23 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Refactor the devtable related definitions in mkfs.ubifs.h
to a new devtable.h

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/mkfs.ubifs/devtable.c   |  3 +-
 ubifs-utils/mkfs.ubifs/devtable.h   | 55 +++++++++++++++++++++++++++++++++++++
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.h | 54 +-----------------------------------
 3 files changed, 58 insertions(+), 54 deletions(-)
 create mode 100644 ubifs-utils/mkfs.ubifs/devtable.h

diff --git a/ubifs-utils/mkfs.ubifs/devtable.c b/ubifs-utils/mkfs.ubifs/devtable.c
index dee035d..1fc0256 100644
--- a/ubifs-utils/mkfs.ubifs/devtable.c
+++ b/ubifs-utils/mkfs.ubifs/devtable.c
@@ -44,7 +44,8 @@
  * for more information about what the device table is.
  */
 
-#include "mkfs.ubifs.h"
+#include "ubifs_common.h"
+#include "devtable.h"
 #include "hashtable/hashtable.h"
 #include "hashtable/hashtable_itr.h"
 
diff --git a/ubifs-utils/mkfs.ubifs/devtable.h b/ubifs-utils/mkfs.ubifs/devtable.h
new file mode 100644
index 0000000..987d4d4
--- /dev/null
+++ b/ubifs-utils/mkfs.ubifs/devtable.h
@@ -0,0 +1,55 @@
+#include "ubifs_common.h"
+
+/**
+ * struct path_htbl_element - an element of the path hash table.
+ * @path: the UBIFS path the element describes (the key of the element)
+ * @name_htbl: one more (nested) hash table containing names of all
+ *             files/directories/device nodes which should be created at this
+ *             path
+ *
+ * See device table handling for more information.
+ */
+struct path_htbl_element {
+	const char *path;
+	struct hashtable *name_htbl;
+};
+
+/**
+ * struct name_htbl_element - an element in the name hash table
+ * @name: name of the file/directory/device node (the key of the element)
+ * @mode: accsess rights and file type
+ * @uid: user ID
+ * @gid: group ID
+ * @major: device node major number
+ * @minor: device node minor number
+ *
+ * This is an element of the name hash table. Name hash table sits in the path
+ * hash table elements and describes file names which should be created/changed
+ * at this path.
+ */
+struct name_htbl_element {
+	const char *name;
+	unsigned int mode;
+	unsigned int uid;
+	unsigned int gid;
+	dev_t dev;
+};
+
+extern struct ubifs_info info_;
+
+struct hashtable_itr;
+
+int parse_devtable(const char *tbl_file);
+struct path_htbl_element *devtbl_find_path(const char *path);
+struct name_htbl_element *devtbl_find_name(struct path_htbl_element *ph_elt,
+					   const char *name);
+int override_attributes(struct stat *st, struct path_htbl_element *ph_elt,
+			struct name_htbl_element *nh_elt);
+struct name_htbl_element *
+first_name_htbl_element(struct path_htbl_element *ph_elt,
+			struct hashtable_itr **itr);
+struct name_htbl_element *
+next_name_htbl_element(struct path_htbl_element *ph_elt,
+			struct hashtable_itr **itr);
+void free_devtable_info(void);
+
diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
index ba646a6..1963e12 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
@@ -34,6 +34,7 @@
 #include "key.h"
 #include "lpt.h"
 #include "compr.h"
+#include "devtable.h"
 #include "io.h"
 
 /*
@@ -50,57 +51,4 @@
 #error MKFS_UBIFS_COMPR_ZLIB != UBIFS_COMPR_ZLIB
 #endif
 
-/**
- * struct path_htbl_element - an element of the path hash table.
- * @path: the UBIFS path the element describes (the key of the element)
- * @name_htbl: one more (nested) hash table containing names of all
- *             files/directories/device nodes which should be created at this
- *             path
- *
- * See device table handling for more information.
- */
-struct path_htbl_element {
-	const char *path;
-	struct hashtable *name_htbl;
-};
-
-/**
- * struct name_htbl_element - an element in the name hash table
- * @name: name of the file/directory/device node (the key of the element)
- * @mode: accsess rights and file type
- * @uid: user ID
- * @gid: group ID
- * @major: device node major number
- * @minor: device node minor number
- *
- * This is an element of the name hash table. Name hash table sits in the path
- * hash table elements and describes file names which should be created/changed
- * at this path.
- */
-struct name_htbl_element {
-	const char *name;
-	unsigned int mode;
-	unsigned int uid;
-	unsigned int gid;
-	dev_t dev;
-};
-
-extern struct ubifs_info info_;
-
-struct hashtable_itr;
-
-int parse_devtable(const char *tbl_file);
-struct path_htbl_element *devtbl_find_path(const char *path);
-struct name_htbl_element *devtbl_find_name(struct path_htbl_element *ph_elt,
-					   const char *name);
-int override_attributes(struct stat *st, struct path_htbl_element *ph_elt,
-			struct name_htbl_element *nh_elt);
-struct name_htbl_element *
-first_name_htbl_element(struct path_htbl_element *ph_elt,
-			struct hashtable_itr **itr);
-struct name_htbl_element *
-next_name_htbl_element(struct path_htbl_element *ph_elt,
-		       struct hashtable_itr **itr);
-void free_devtable_info(void);
-
 #endif
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 07/27] ubifs: introduce ubifs-utils/include and ubifs-utils/lib
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (5 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 06/27] ubifs: cut off the dependence from devtable to mkfs.ubifs.h Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 08/27] ubifs: move more functions into io lib Dongsheng Yang
                   ` (22 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Restructure the ubifs-utils/, introducing ubifs-utils/include
and ubifs-utils/lib to make the ubifs-utils/ better for scalability.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                                                          | 7 ++++---
 ubifs-utils/{mkfs.ubifs => }/COPYING                              | 0
 ubifs-utils/{mkfs.ubifs => }/README                               | 0
 ubifs-utils/{mkfs.ubifs => include}/compr.h                       | 0
 ubifs-utils/{mkfs.ubifs => include}/crc16.h                       | 0
 ubifs-utils/{mkfs.ubifs => include}/defs.h                        | 0
 ubifs-utils/{mkfs.ubifs => include}/devtable.h                    | 0
 ubifs-utils/{mkfs.ubifs/hashtable => include}/hashtable.h         | 0
 ubifs-utils/{mkfs.ubifs/hashtable => include}/hashtable_itr.h     | 0
 ubifs-utils/{mkfs.ubifs/hashtable => include}/hashtable_private.h | 0
 ubifs-utils/{mkfs.ubifs => include}/io.h                          | 0
 ubifs-utils/{mkfs.ubifs => include}/key.h                         | 0
 ubifs-utils/{mkfs.ubifs => include}/lpt.h                         | 0
 ubifs-utils/{mkfs.ubifs => include}/ubifs.h                       | 0
 ubifs-utils/{mkfs.ubifs => include}/ubifs_common.h                | 0
 ubifs-utils/{mkfs.ubifs => lib}/compr.c                           | 0
 ubifs-utils/{mkfs.ubifs => lib}/crc16.c                           | 0
 ubifs-utils/{mkfs.ubifs => lib}/devtable.c                        | 4 ++--
 ubifs-utils/{mkfs.ubifs/hashtable => lib}/hashtable.c             | 0
 ubifs-utils/{mkfs.ubifs/hashtable => lib}/hashtable_itr.c         | 0
 ubifs-utils/{mkfs.ubifs => lib}/io.c                              | 0
 ubifs-utils/{mkfs.ubifs => lib}/lpt.c                             | 0
 22 files changed, 6 insertions(+), 5 deletions(-)
 rename ubifs-utils/{mkfs.ubifs => }/COPYING (100%)
 rename ubifs-utils/{mkfs.ubifs => }/README (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/compr.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/crc16.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/defs.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/devtable.h (100%)
 rename ubifs-utils/{mkfs.ubifs/hashtable => include}/hashtable.h (100%)
 rename ubifs-utils/{mkfs.ubifs/hashtable => include}/hashtable_itr.h (100%)
 rename ubifs-utils/{mkfs.ubifs/hashtable => include}/hashtable_private.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/io.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/key.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/lpt.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/ubifs.h (100%)
 rename ubifs-utils/{mkfs.ubifs => include}/ubifs_common.h (100%)
 rename ubifs-utils/{mkfs.ubifs => lib}/compr.c (100%)
 rename ubifs-utils/{mkfs.ubifs => lib}/crc16.c (100%)
 rename ubifs-utils/{mkfs.ubifs => lib}/devtable.c (99%)
 rename ubifs-utils/{mkfs.ubifs/hashtable => lib}/hashtable.c (100%)
 rename ubifs-utils/{mkfs.ubifs/hashtable => lib}/hashtable_itr.c (100%)
 rename ubifs-utils/{mkfs.ubifs => lib}/io.c (100%)
 rename ubifs-utils/{mkfs.ubifs => lib}/lpt.c (100%)

diff --git a/Makefile b/Makefile
index 843108c..ae02aac 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
 
 VERSION = 1.5.1
 
-CPPFLAGS += -D_GNU_SOURCE -I./include -I$(BUILDDIR)/include -I./ubi-utils/include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS) $(UUIDCPPFLAGS)
+CPPFLAGS += -D_GNU_SOURCE -I./include -I$(BUILDDIR)/include -I./ubi-utils/include -I./ubifs-utils/include $(ZLIBCPPFLAGS) $(LZOCPPFLAGS) $(UUIDCPPFLAGS)
 
 ifeq ($(WITHOUT_XATTR), 1)
   CPPFLAGS += -DWITHOUT_XATTR
@@ -129,8 +129,9 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 #
 # Utils in ubifs-utils subdir
 #
-obj-mkfs.ubifs = crc16.o lpt.o compr.o devtable.o io.o\
-	hashtable/hashtable.o hashtable/hashtable_itr.o
+$(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
+
+obj-mkfs.ubifs = $(UBIFS_LIBS)
 LDFLAGS_mkfs.ubifs = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid
 $(call mkdep,ubifs-utils/mkfs.ubifs/,mkfs.ubifs,,ubi-utils/libubi.a)
diff --git a/ubifs-utils/mkfs.ubifs/COPYING b/ubifs-utils/COPYING
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/COPYING
rename to ubifs-utils/COPYING
diff --git a/ubifs-utils/mkfs.ubifs/README b/ubifs-utils/README
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/README
rename to ubifs-utils/README
diff --git a/ubifs-utils/mkfs.ubifs/compr.h b/ubifs-utils/include/compr.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/compr.h
rename to ubifs-utils/include/compr.h
diff --git a/ubifs-utils/mkfs.ubifs/crc16.h b/ubifs-utils/include/crc16.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/crc16.h
rename to ubifs-utils/include/crc16.h
diff --git a/ubifs-utils/mkfs.ubifs/defs.h b/ubifs-utils/include/defs.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/defs.h
rename to ubifs-utils/include/defs.h
diff --git a/ubifs-utils/mkfs.ubifs/devtable.h b/ubifs-utils/include/devtable.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/devtable.h
rename to ubifs-utils/include/devtable.h
diff --git a/ubifs-utils/mkfs.ubifs/hashtable/hashtable.h b/ubifs-utils/include/hashtable.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/hashtable/hashtable.h
rename to ubifs-utils/include/hashtable.h
diff --git a/ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.h b/ubifs-utils/include/hashtable_itr.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.h
rename to ubifs-utils/include/hashtable_itr.h
diff --git a/ubifs-utils/mkfs.ubifs/hashtable/hashtable_private.h b/ubifs-utils/include/hashtable_private.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/hashtable/hashtable_private.h
rename to ubifs-utils/include/hashtable_private.h
diff --git a/ubifs-utils/mkfs.ubifs/io.h b/ubifs-utils/include/io.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/io.h
rename to ubifs-utils/include/io.h
diff --git a/ubifs-utils/mkfs.ubifs/key.h b/ubifs-utils/include/key.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/key.h
rename to ubifs-utils/include/key.h
diff --git a/ubifs-utils/mkfs.ubifs/lpt.h b/ubifs-utils/include/lpt.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/lpt.h
rename to ubifs-utils/include/lpt.h
diff --git a/ubifs-utils/mkfs.ubifs/ubifs.h b/ubifs-utils/include/ubifs.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/ubifs.h
rename to ubifs-utils/include/ubifs.h
diff --git a/ubifs-utils/mkfs.ubifs/ubifs_common.h b/ubifs-utils/include/ubifs_common.h
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/ubifs_common.h
rename to ubifs-utils/include/ubifs_common.h
diff --git a/ubifs-utils/mkfs.ubifs/compr.c b/ubifs-utils/lib/compr.c
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/compr.c
rename to ubifs-utils/lib/compr.c
diff --git a/ubifs-utils/mkfs.ubifs/crc16.c b/ubifs-utils/lib/crc16.c
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/crc16.c
rename to ubifs-utils/lib/crc16.c
diff --git a/ubifs-utils/mkfs.ubifs/devtable.c b/ubifs-utils/lib/devtable.c
similarity index 99%
rename from ubifs-utils/mkfs.ubifs/devtable.c
rename to ubifs-utils/lib/devtable.c
index 1fc0256..e6ec319 100644
--- a/ubifs-utils/mkfs.ubifs/devtable.c
+++ b/ubifs-utils/lib/devtable.c
@@ -46,8 +46,8 @@
 
 #include "ubifs_common.h"
 #include "devtable.h"
-#include "hashtable/hashtable.h"
-#include "hashtable/hashtable_itr.h"
+#include "hashtable.h"
+#include "hashtable_itr.h"
 
 /*
  * The hash table which contains paths to files/directories/device nodes
diff --git a/ubifs-utils/mkfs.ubifs/hashtable/hashtable.c b/ubifs-utils/lib/hashtable.c
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/hashtable/hashtable.c
rename to ubifs-utils/lib/hashtable.c
diff --git a/ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.c b/ubifs-utils/lib/hashtable_itr.c
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/hashtable/hashtable_itr.c
rename to ubifs-utils/lib/hashtable_itr.c
diff --git a/ubifs-utils/mkfs.ubifs/io.c b/ubifs-utils/lib/io.c
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/io.c
rename to ubifs-utils/lib/io.c
diff --git a/ubifs-utils/mkfs.ubifs/lpt.c b/ubifs-utils/lib/lpt.c
similarity index 100%
rename from ubifs-utils/mkfs.ubifs/lpt.c
rename to ubifs-utils/lib/lpt.c
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 08/27] ubifs: move more functions into io lib
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (6 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 07/27] ubifs: introduce ubifs-utils/include and ubifs-utils/lib Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 09/27] ubifs: introduce a new tool ubifs_dump Dongsheng Yang
                   ` (21 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Move some common functions in mkfs.ubifs.c to io.c
to let others can use them.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/io.h            |   4 ++
 ubifs-utils/lib/io.c                | 101 +++++++++++++++++++++++++++++++++++
 ubifs-utils/mkfs.ubifs/mkfs.ubifs.c | 102 +-----------------------------------
 3 files changed, 107 insertions(+), 100 deletions(-)

diff --git a/ubifs-utils/include/io.h b/ubifs-utils/include/io.h
index e24d0c6..920645d 100644
--- a/ubifs-utils/include/io.h
+++ b/ubifs-utils/include/io.h
@@ -10,6 +10,10 @@
 extern int out_fd;
 extern int out_ubi;
 extern libubi_t ubi;
+extern char *output;
 
 int write_leb(struct ubifs_info *c, int lnum, int len, void *buf);
+int close_target(void);
+int open_target(struct ubifs_info *c, int yes);
+int open_ubi(struct ubifs_info *c, const char *node);
 #endif
diff --git a/ubifs-utils/lib/io.c b/ubifs-utils/lib/io.c
index 7aba0a6..9817d2a 100644
--- a/ubifs-utils/lib/io.c
+++ b/ubifs-utils/lib/io.c
@@ -5,6 +5,107 @@
 int out_fd;
 int out_ubi;
 libubi_t ubi;
+char *output;
+
+/**
+ * check_volume_empty - check if the UBI volume is empty.
+ *
+ * This function checks if the UBI volume is empty by looking if its LEBs are
+ * mapped or not.
+ *
+ * Returns %0 in case of success, %1 is the volume is not empty,
+ * and a negative error code in case of failure.
+ */
+static int check_volume_empty(struct ubifs_info *c)
+{
+	int lnum, err;
+
+	for (lnum = 0; lnum < c->vi.rsvd_lebs; lnum++) {
+		err = ubi_is_mapped(out_fd, lnum);
+		if (err < 0)
+			return err;
+		if (err == 1)
+			return 1;
+	}
+	return 0;
+}
+
+/**
+ * open_ubi - open the UBI volume.
+ * @node: name of the UBI volume character device to fetch information about
+ *
+ * Returns %0 in case of success and %-1 in case of failure
+ */
+int open_ubi(struct ubifs_info *c, const char *node)
+{
+	struct stat st;
+
+	if (stat(node, &st) || !S_ISCHR(st.st_mode))
+		return -1;
+
+	ubi = libubi_open();
+	if (!ubi)
+		return -1;
+	if (ubi_get_vol_info(ubi, node, &c->vi))
+		return -1;
+	if (ubi_get_dev_info1(ubi, c->vi.dev_num, &c->di))
+		return -1;
+	return 0;
+}
+
+/**
+ * open_target - open the output target.
+ * @yes: always ansure yes
+ *
+ * Open the output target. The target can be an UBI volume
+ * or a file.
+ *
+ * Returns %0 in case of success and %-1 in case of failure.
+ */
+int open_target(struct ubifs_info *c, int yes)
+{
+	if (out_ubi) {
+		out_fd = open(output, O_RDWR | O_EXCL);
+
+		if (out_fd == -1)
+			return sys_err_msg("cannot open the UBI volume '%s'",
+					   output);
+		if (ubi_set_property(out_fd, UBI_VOL_PROP_DIRECT_WRITE, 1))
+			return sys_err_msg("ubi_set_property failed");
+
+		if (!yes && check_volume_empty(c)) {
+			if (!prompt("UBI volume is not empty.  Format anyways?", false))
+				return err_msg("UBI volume is not empty");
+		}
+	} else {
+		out_fd = open(output, O_CREAT | O_RDWR | O_TRUNC,
+			      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
+		if (out_fd == -1)
+			return sys_err_msg("cannot create output file '%s'",
+					   output);
+	}
+	return 0;
+}
+
+/**
+ * close_target - close the output target.
+ *
+ * Close the output target. If the target was an UBI
+ * volume, also close libubi.
+ *
+ * Returns %0 in case of success and %-1 in case of failure.
+ */
+int close_target(void)
+{
+	if (ubi)
+		libubi_close(ubi);
+	if (out_fd >= 0 && close(out_fd) == -1)
+		return sys_err_msg("cannot close the target '%s'", output);
+	if (output)
+		free(output);
+	return 0;
+}
+
 
 /**
  * write_leb - copy the image of a LEB to the output target.
diff --git a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
index 9795282..b19646f 100644
--- a/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
+++ b/ubifs-utils/mkfs.ubifs/mkfs.ubifs.c
@@ -106,7 +106,6 @@ int yes;
 static char *root;
 static int root_len;
 static struct stat root_st;
-static char *output;
 static int squash_owner;
 
 /* The 'head' (position) which nodes are written */
@@ -446,28 +445,6 @@ static long long get_bytes(const char *str)
 
 	return bytes;
 }
-/**
- * open_ubi - open the UBI volume.
- * @node: name of the UBI volume character device to fetch information about
- *
- * Returns %0 in case of success and %-1 in case of failure
- */
-static int open_ubi(const char *node)
-{
-	struct stat st;
-
-	if (stat(node, &st) || !S_ISCHR(st.st_mode))
-		return -1;
-
-	ubi = libubi_open();
-	if (!ubi)
-		return -1;
-	if (ubi_get_vol_info(ubi, node, &c->vi))
-		return -1;
-	if (ubi_get_dev_info1(ubi, c->vi.dev_num, &c->di))
-		return -1;
-	return 0;
-}
 
 static int get_options(int argc, char**argv)
 {
@@ -629,7 +606,7 @@ static int get_options(int argc, char**argv)
 	if (!output)
 		return err_msg("not output device or file specified");
 
-	out_ubi = !open_ubi(output);
+	out_ubi = !open_ubi(c, output);
 
 	if (out_ubi) {
 		c->min_io_size = c->di.min_io_size;
@@ -2038,81 +2015,6 @@ static int write_orphan_area(void)
 	return 0;
 }
 
-/**
- * check_volume_empty - check if the UBI volume is empty.
- *
- * This function checks if the UBI volume is empty by looking if its LEBs are
- * mapped or not.
- *
- * Returns %0 in case of success, %1 is the volume is not empty,
- * and a negative error code in case of failure.
- */
-static int check_volume_empty(void)
-{
-	int lnum, err;
-
-	for (lnum = 0; lnum < c->vi.rsvd_lebs; lnum++) {
-		err = ubi_is_mapped(out_fd, lnum);
-		if (err < 0)
-			return err;
-		if (err == 1)
-			return 1;
-	}
-	return 0;
-}
-
-/**
- * open_target - open the output target.
- *
- * Open the output target. The target can be an UBI volume
- * or a file.
- *
- * Returns %0 in case of success and %-1 in case of failure.
- */
-static int open_target(void)
-{
-	if (out_ubi) {
-		out_fd = open(output, O_RDWR | O_EXCL);
-
-		if (out_fd == -1)
-			return sys_err_msg("cannot open the UBI volume '%s'",
-					   output);
-		if (ubi_set_property(out_fd, UBI_VOL_PROP_DIRECT_WRITE, 1))
-			return sys_err_msg("ubi_set_property failed");
-
-		if (!yes && check_volume_empty()) {
-			if (!prompt("UBI volume is not empty.  Format anyways?", false))
-				return err_msg("UBI volume is not empty");
-		}
-	} else {
-		out_fd = open(output, O_CREAT | O_RDWR | O_TRUNC,
-			      S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
-		if (out_fd == -1)
-			return sys_err_msg("cannot create output file '%s'",
-					   output);
-	}
-	return 0;
-}
-
-
-/**
- * close_target - close the output target.
- *
- * Close the output target. If the target was an UBI
- * volume, also close libubi.
- *
- * Returns %0 in case of success and %-1 in case of failure.
- */
-static int close_target(void)
-{
-	if (ubi)
-		libubi_close(ubi);
-	if (out_fd >= 0 && close(out_fd) == -1)
-		return sys_err_msg("cannot close the target '%s'", output);
-	if (output)
-		free(output);
-	return 0;
-}
 
 /**
  * init - initialize things.
@@ -2279,7 +2181,7 @@ int main(int argc, char *argv[])
 	if (err)
 		return err;
 
-	err = open_target();
+	err = open_target(c, yes);
 	if (err)
 		return err;
 
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 09/27] ubifs: introduce a new tool ubifs_dump
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (7 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 08/27] ubifs: move more functions into io lib Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 10/27] ubifs: introduce list.h Dongsheng Yang
                   ` (20 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Init a new tool named as ubifs_dump

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                            |  8 ++++-
 ubifs-utils/ubifs_dump/ubifs_dump.c | 68 +++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 ubifs-utils/ubifs_dump/ubifs_dump.c

diff --git a/Makefile b/Makefile
index ae02aac..cad71dc 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,8 @@ UBI_BINS = \
 	ubiupdatevol ubimkvol ubirmvol ubicrc32 ubinfo ubiattach \
 	ubidetach ubinize ubiformat ubirename mtdinfo ubirsvol ubiblock
 UBIFS_BINS = \
-	mkfs.ubifs/mkfs.ubifs
+	mkfs.ubifs/mkfs.ubifs \
+	ubifs_dump/ubifs_dump
 JFFSX_BINS = \
 	mkfs.jffs2 sumtool jffs2reader jffs2dump
 FLASH_BINS = \
@@ -131,6 +132,11 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 #
 $(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
 
+obj-ubifs_dump = $(UBIFS_LIBS)
+LDFLAGS_ubifs_dump = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
+LDLIBS_ubifs_dump = -lz -llzo2 -lm -luuid
+$(call mkdep,ubifs-utils/ubifs_dump/,ubifs_dump,,ubi-utils/libubi.a)
+
 obj-mkfs.ubifs = $(UBIFS_LIBS)
 LDFLAGS_mkfs.ubifs = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_mkfs.ubifs = -lz -llzo2 -lm -luuid
diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
new file mode 100644
index 0000000..bbe1269
--- /dev/null
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -0,0 +1,68 @@
+#include "ubifs_common.h"
+#define PROGRAM_NAME "ubifs-dump"
+#include "common.h"
+#include "io.h"
+
+static const char *optstring = "";
+
+static const struct option longopts[] = {
+	{NULL, 0, NULL, 0}
+};
+
+struct ubifs_info info_;
+static struct ubifs_info *c = &info_;
+
+static int get_options(int argc, char**argv)
+{
+	int opt, i;
+
+	while (1) {
+		opt = getopt_long(argc, argv, optstring, longopts, &i);
+		if (opt == -1)
+			break;
+		switch (opt) {
+		default:
+			break;
+		}
+	}
+
+	if (optind != argc && !output)
+		output = xstrdup(argv[optind]);
+
+	if (!output)
+		return err_msg("not output device or file specified");
+
+	if (open_ubi(c, output))
+		return err_msg("open ubi (%s) failed.", output);
+
+	return 0;
+}
+
+static int dump()
+{
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int err;
+
+	err = get_options(argc, argv);
+	if (err)
+		return err;
+	err = open_target(c, 1);
+	if (err)
+		return err;
+
+	err = dump();
+	if (err) {
+		close_target();
+		return err;
+	}
+
+	err = close_target();
+	if (err)
+		return err;
+
+	return 0;
+}
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 10/27] ubifs: introduce list.h
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (8 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 09/27] ubifs: introduce a new tool ubifs_dump Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-10-07 20:41   ` Richard Weinberger
  2015-08-19  8:39 ` [PATCH 11/27] ubifs: copy some important data in ubifs.h from kernel to ubifs-utils Dongsheng Yang
                   ` (19 subsequent siblings)
  29 siblings, 1 reply; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Copy the list.h from kernel to ubifs-utils/

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/list.h | 484 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 484 insertions(+)
 create mode 100644 ubifs-utils/include/list.h

diff --git a/ubifs-utils/include/list.h b/ubifs-utils/include/list.h
new file mode 100644
index 0000000..0cffa33
--- /dev/null
+++ b/ubifs-utils/include/list.h
@@ -0,0 +1,484 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * 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 021110-1307, USA.
+ */
+
+#ifndef _LINUX_LIST_H
+#define _LINUX_LIST_H
+
+#define LIST_POISON1  ((struct list_head *) 0x00100100)
+#define LIST_POISON2  ((struct list_head *) 0x00200200)
+
+/*
+ * Simple doubly linked list implementation.
+ *
+ * Some of the internal functions ("__xxx") are useful when
+ * manipulating whole lists rather than single entries, as
+ * sometimes we already know the next/prev entries and we can
+ * generate better code by using them directly rather than
+ * using the generic single-entry routines.
+ */
+
+struct list_head {
+	struct list_head *next, *prev;
+};
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) \
+	struct list_head name = LIST_HEAD_INIT(name)
+
+static inline void INIT_LIST_HEAD(struct list_head *list)
+{
+	list->next = list;
+	list->prev = list;
+}
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+#ifndef CONFIG_DEBUG_LIST
+static inline void __list_add(struct list_head *xnew,
+			      struct list_head *prev,
+			      struct list_head *next)
+{
+	next->prev = xnew;
+	xnew->next = next;
+	xnew->prev = prev;
+	prev->next = xnew;
+}
+#else
+extern void __list_add(struct list_head *xnew,
+			      struct list_head *prev,
+			      struct list_head *next);
+#endif
+
+/**
+ * list_add - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+#ifndef CONFIG_DEBUG_LIST
+static inline void list_add(struct list_head *xnew, struct list_head *head)
+{
+	__list_add(xnew, head, head->next);
+}
+#else
+extern void list_add(struct list_head *xnew, struct list_head *head);
+#endif
+
+
+/**
+ * list_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+static inline void list_add_tail(struct list_head *xnew, struct list_head *head)
+{
+	__list_add(xnew, head->prev, head);
+}
+
+/*
+ * Delete a list entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_del(struct list_head * prev, struct list_head * next)
+{
+	next->prev = prev;
+	prev->next = next;
+}
+
+/**
+ * list_del - deletes entry from list.
+ * @entry: the element to delete from the list.
+ * Note: list_empty on entry does not return true after this, the entry is
+ * in an undefined state.
+ */
+#ifndef CONFIG_DEBUG_LIST
+static inline void list_del(struct list_head *entry)
+{
+	__list_del(entry->prev, entry->next);
+	entry->next = LIST_POISON1;
+	entry->prev = LIST_POISON2;
+}
+#else
+extern void list_del(struct list_head *entry);
+#endif
+
+/**
+ * list_replace - replace old entry by new one
+ * @old : the element to be replaced
+ * @new : the new element to insert
+ * Note: if 'old' was empty, it will be overwritten.
+ */
+static inline void list_replace(struct list_head *old,
+				struct list_head *xnew)
+{
+	xnew->next = old->next;
+	xnew->next->prev = xnew;
+	xnew->prev = old->prev;
+	xnew->prev->next = xnew;
+}
+
+static inline void list_replace_init(struct list_head *old,
+					struct list_head *xnew)
+{
+	list_replace(old, xnew);
+	INIT_LIST_HEAD(old);
+}
+/**
+ * list_del_init - deletes entry from list and reinitialize it.
+ * @entry: the element to delete from the list.
+ */
+static inline void list_del_init(struct list_head *entry)
+{
+	__list_del(entry->prev, entry->next);
+	INIT_LIST_HEAD(entry);
+}
+
+/**
+ * list_move - delete from one list and add as another's head
+ * @list: the entry to move
+ * @head: the head that will precede our entry
+ */
+static inline void list_move(struct list_head *list, struct list_head *head)
+{
+        __list_del(list->prev, list->next);
+        list_add(list, head);
+}
+
+/**
+ * list_move_tail - delete from one list and add as another's tail
+ * @list: the entry to move
+ * @head: the head that will follow our entry
+ */
+static inline void list_move_tail(struct list_head *list,
+				  struct list_head *head)
+{
+        __list_del(list->prev, list->next);
+        list_add_tail(list, head);
+}
+
+/**
+ * list_is_last - tests whether @list is the last entry in list @head
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_last(const struct list_head *list,
+				const struct list_head *head)
+{
+	return list->next == head;
+}
+
+/**
+ * list_empty - tests whether a list is empty
+ * @head: the list to test.
+ */
+static inline int list_empty(const struct list_head *head)
+{
+	return head->next == head;
+}
+
+/**
+ * list_empty_careful - tests whether a list is empty and not being modified
+ * @head: the list to test
+ *
+ * Description:
+ * tests whether a list is empty _and_ checks that no other CPU might be
+ * in the process of modifying either member (next or prev)
+ *
+ * NOTE: using list_empty_careful() without synchronization
+ * can only be safe if the only activity that can happen
+ * to the list entry is list_del_init(). Eg. it cannot be used
+ * if another CPU could re-list_add() it.
+ */
+static inline int list_empty_careful(const struct list_head *head)
+{
+	struct list_head *next = head->next;
+	return (next == head) && (next == head->prev);
+}
+
+static inline void __list_splice(const struct list_head *list,
+				 struct list_head *prev,
+				 struct list_head *next)
+{
+	struct list_head *first = list->next;
+	struct list_head *last = list->prev;
+
+	first->prev = prev;
+	prev->next = first;
+
+	last->next = next;
+	next->prev = last;
+}
+
+/**
+ * list_splice - join two lists
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice(struct list_head *list, struct list_head *head)
+{
+	if (!list_empty(list))
+		__list_splice(list, head, head->next);
+}
+
+/**
+ * list_splice_tail - join two lists, each list being a queue
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice_tail(struct list_head *list,
+				struct list_head *head)
+{
+	if (!list_empty(list))
+		__list_splice(list, head->prev, head);
+}
+
+/**
+ * list_splice_init - join two lists and reinitialise the emptied list.
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_init(struct list_head *list,
+				    struct list_head *head)
+{
+	if (!list_empty(list)) {
+		__list_splice(list, head, head->next);
+		INIT_LIST_HEAD(list);
+	}
+}
+
+/**
+ * list_splice_tail_init - join two lists and reinitialise the emptied list
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * Each of the lists is a queue.
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_tail_init(struct list_head *list,
+					 struct list_head *head)
+{
+	if (!list_empty(list)) {
+		__list_splice(list, head->prev, head);
+		INIT_LIST_HEAD(list);
+	}
+}
+
+/**
+ * list_entry - get the struct for this entry
+ * @ptr:	the &struct list_head pointer.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_entry(ptr, type, member) \
+	container_of(ptr, type, member)
+
+/**
+ * list_first_entry - get the first element from a list
+ * @ptr:	the list head to take the element from.
+ * @type:	the type of the struct this is embedded in.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_first_entry(ptr, type, member) \
+	list_entry((ptr)->next, type, member)
+
+/**
+ * list_next_entry - get the next element from a list
+ * @ptr:	the list head to take the element from.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Note, that next is expected to be not null.
+ */
+#define list_next_entry(ptr, member) \
+	list_entry((ptr)->member.next, typeof(*ptr), member)
+
+/**
+ * list_for_each	-	iterate over a list
+ * @pos:	the &struct list_head to use as a loop cursor.
+ * @head:	the head for your list.
+ */
+#define list_for_each(pos, head) \
+	for (pos = (head)->next; pos != (head); \
+        	pos = pos->next)
+
+/**
+ * __list_for_each	-	iterate over a list
+ * @pos:	the &struct list_head to use as a loop cursor.
+ * @head:	the head for your list.
+ *
+ * This variant differs from list_for_each() in that it's the
+ * simplest possible list iteration code, no prefetching is done.
+ * Use this for code that knows the list to be very short (empty
+ * or 1 entry) most of the time.
+ */
+#define __list_for_each(pos, head) \
+	for (pos = (head)->next; pos != (head); pos = pos->next)
+
+/**
+ * list_for_each_prev	-	iterate over a list backwards
+ * @pos:	the &struct list_head to use as a loop cursor.
+ * @head:	the head for your list.
+ */
+#define list_for_each_prev(pos, head) \
+	for (pos = (head)->prev; pos != (head); \
+        	pos = pos->prev)
+
+/**
+ * list_for_each_safe - iterate over a list safe against removal of list entry
+ * @pos:	the &struct list_head to use as a loop cursor.
+ * @n:		another &struct list_head to use as temporary storage
+ * @head:	the head for your list.
+ */
+#define list_for_each_safe(pos, n, head) \
+	for (pos = (head)->next, n = pos->next; pos != (head); \
+		pos = n, n = pos->next)
+
+/**
+ * list_for_each_entry	-	iterate over list of given type
+ * @pos:	the type * to use as a loop cursor.
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_for_each_entry(pos, head, member)				\
+	for (pos = list_entry((head)->next, typeof(*pos), member);	\
+	     &pos->member != (head); 	\
+	     pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_reverse - iterate backwards over list of given type.
+ * @pos:	the type * to use as a loop cursor.
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_reverse(pos, head, member)			\
+	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
+	     &pos->member != (head); 	\
+	     pos = list_entry(pos->member.prev, typeof(*pos), member))
+
+/**
+ * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
+ * @pos:	the type * to use as a start point
+ * @head:	the head of the list
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
+ */
+#define list_prepare_entry(pos, head, member) \
+	((pos) ? : list_entry(head, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_continue - continue iteration over list of given type
+ * @pos:	the type * to use as a loop cursor.
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Continue to iterate over list of given type, continuing after
+ * the current position.
+ */
+#define list_for_each_entry_continue(pos, head, member) 		\
+	for (pos = list_entry(pos->member.next, typeof(*pos), member);	\
+	     &pos->member != (head);	\
+	     pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_from - iterate over list of given type from the current point
+ * @pos:	the type * to use as a loop cursor.
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type, continuing from current position.
+ */
+#define list_for_each_entry_from(pos, head, member) 			\
+	for (; &pos->member != (head);	\
+	     pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @pos:	the type * to use as a loop cursor.
+ * @n:		another type * to use as temporary storage
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member)			\
+	for (pos = list_entry((head)->next, typeof(*pos), member),	\
+		n = list_entry(pos->member.next, typeof(*pos), member);	\
+	     &pos->member != (head); 					\
+	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_continue
+ * @pos:	the type * to use as a loop cursor.
+ * @n:		another type * to use as temporary storage
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type, continuing after current point,
+ * safe against removal of list entry.
+ */
+#define list_for_each_entry_safe_continue(pos, n, head, member) 		\
+	for (pos = list_entry(pos->member.next, typeof(*pos), member), 		\
+		n = list_entry(pos->member.next, typeof(*pos), member);		\
+	     &pos->member != (head);						\
+	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_from
+ * @pos:	the type * to use as a loop cursor.
+ * @n:		another type * to use as temporary storage
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Iterate over list of given type from current point, safe against
+ * removal of list entry.
+ */
+#define list_for_each_entry_safe_from(pos, n, head, member) 			\
+	for (n = list_entry(pos->member.next, typeof(*pos), member);		\
+	     &pos->member != (head);						\
+	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+/**
+ * list_for_each_entry_safe_reverse
+ * @pos:	the type * to use as a loop cursor.
+ * @n:		another type * to use as temporary storage
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ *
+ * Iterate backwards over list of given type, safe against removal
+ * of list entry.
+ */
+#define list_for_each_entry_safe_reverse(pos, n, head, member)		\
+	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
+		n = list_entry(pos->member.prev, typeof(*pos), member);	\
+	     &pos->member != (head); 					\
+	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
+
+#endif
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 11/27] ubifs: copy some important data in ubifs.h from kernel to ubifs-utils
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (9 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 10/27] ubifs: introduce list.h Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 12/27] ubifs: copy some important functions in key.h " Dongsheng Yang
                   ` (18 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

There are some definitions in ubifs.h in kernel we need but
not in userspace currently. Then copy it here.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/ubifs.h | 96 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 91 insertions(+), 5 deletions(-)

diff --git a/ubifs-utils/include/ubifs.h b/ubifs-utils/include/ubifs.h
index 434b651..3696f1a 100644
--- a/ubifs-utils/include/ubifs.h
+++ b/ubifs-utils/include/ubifs.h
@@ -25,6 +25,10 @@
 #ifndef __UBIFS_H__
 #define __UBIFS_H__
 
+#include "defs.h"
+#include "list.h"
+#include "libubi.h"
+
 /* Maximum logical eraseblock size in bytes */
 #define UBIFS_MAX_LEB_SZ (2*1024*1024)
 
@@ -34,6 +38,7 @@
 /* Largest key size supported in this implementation */
 #define CUR_MAX_KEY_LEN UBIFS_SK_LEN
 
+#define NONDATA_JHEADS_CNT 2
 /*
  * There is no notion of truncation key because truncation nodes do not exist
  * in TNC. However, when replaying, it is handy to introduce fake "truncation"
@@ -41,6 +46,7 @@
  * %UBIFS_TRUN_KEY type.
  */
 #define UBIFS_TRUN_KEY UBIFS_KEY_TYPES_CNT
+#define UBIFS_INVALID_KEY UBIFS_KEY_TYPES_CNT
 
 /* The below union makes it easier to deal with keys */
 union ubifs_key
@@ -85,25 +91,34 @@ enum {
  * @free: amount of free space in bytes
  * @dirty: amount of dirty space in bytes
  * @flags: LEB properties flags (see above)
+ * @lnum: LEB number
+ * @list: list of same-category lprops (for LPROPS_EMPTY and LPROPS_FREEABLE)
+ * @hpos: heap position in heap of same-category lprops (other categories)
  */
-struct ubifs_lprops
-{
+struct ubifs_lprops {
 	int free;
 	int dirty;
 	int flags;
+	int lnum;
+	union {
+		struct list_head list;
+		int hpos;
+	};
 };
 
 /**
  * struct ubifs_lpt_lprops - LPT logical eraseblock properties.
  * @free: amount of free space in bytes
  * @dirty: amount of dirty space in bytes
+ * @tgc: trivial GC flag (1 => unmap after commit end)
+ * @cmt: commit flag (1 => reserved for commit)
  */
-struct ubifs_lpt_lprops
-{
+struct ubifs_lpt_lprops {
 	int free;
 	int dirty;
+	unsigned tgc:1;
+	unsigned cmt:1;
 };
-
 struct ubifs_nnode;
 
 /**
@@ -254,6 +269,12 @@ struct ubifs_znode
 	struct ubifs_zbranch zbranch[];
 };
 
+enum {
+	LPT_SCAN_CONTINUE = 0,
+	LPT_SCAN_ADD = 1,
+	LPT_SCAN_STOP = 2,
+};
+
 /**
  * struct ubifs_info - UBIFS file-system description data structure
  * (per-superblock).
@@ -392,6 +413,7 @@ struct ubifs_info
 	int pnode_cnt;
 	int nnode_cnt;
 	int lpt_hght;
+	void *lpt_nod_buf;
 
 	int lpt_lnum;
 	int lpt_offs;
@@ -411,8 +433,68 @@ struct ubifs_info
 	int *lsave;
 	int lscan_lnum;
 
+	struct ubifs_nnode *nroot;
+	struct ubifs_cnode *lpt_cnext;
+
+	int min_idx_node_sz;
+	int max_idx_node_sz;
+
+	int max_znode_sz;
+};
+/**
+ * struct ubifs_scan_node - UBIFS scanned node information.
+ * @list: list of scanned nodes
+ * @key: key of node scanned (if it has one)
+ * @sqnum: sequence number
+ * @type: type of node scanned
+ * @offs: offset with LEB of node scanned
+ * @len: length of node scanned
+ * @node: raw node
+ */
+struct ubifs_scan_node {
+	struct list_head list;
+	union ubifs_key key;
+	unsigned long long sqnum;
+	int type;
+	int offs;
+	int len;
+	void *node;
+};
+
+/**
+ * struct ubifs_scan_leb - UBIFS scanned LEB information.
+ * @lnum: logical eraseblock number
+ * @nodes_cnt: number of nodes scanned
+ * @nodes: list of struct ubifs_scan_node
+ * @endpt: end point (and therefore the start of empty space)
+ * @buf: buffer containing entire LEB scanned
+ */
+struct ubifs_scan_leb {
+	int lnum;
+	int nodes_cnt;
+	struct list_head nodes;
+	int endpt;
+	void *buf;
 };
 
+/*
+ * 'ubifs_scan_a_node()' return values.
+ *
+ * SCANNED_GARBAGE:  scanned garbage
+ * SCANNED_EMPTY_SPACE: scanned empty space
+ * SCANNED_A_NODE: scanned a valid node
+ * SCANNED_A_CORRUPT_NODE: scanned a corrupted node
+ * SCANNED_A_BAD_PAD_NODE: scanned a padding node with invalid pad length
+ *
+ * Greater than zero means: 'scanned that number of padding bytes'
+ */
+enum {
+	SCANNED_GARBAGE        = 0,
+	SCANNED_EMPTY_SPACE    = -1,
+	SCANNED_A_NODE         = -2,
+	SCANNED_A_CORRUPT_NODE = -3,
+	SCANNED_A_BAD_PAD_NODE = -4,
+};
 /**
  * ubifs_idx_node_sz - return index node size.
  * @c: the UBIFS file-system description object
@@ -438,4 +520,8 @@ struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c,
 				       (UBIFS_BRANCH_SZ + c->key_len) * bnum);
 }
 
+/* Callback used by the 'ubifs_lpt_scan_nolock()' function */
+typedef int (*ubifs_lpt_scan_callback)(struct ubifs_info *c,
+				       const struct ubifs_lprops *lprops,
+				       int in_tree, void *data);
 #endif /* __UBIFS_H__ */
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 12/27] ubifs: copy some important functions in key.h from kernel to ubifs-utils
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (10 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 11/27] ubifs: copy some important data in ubifs.h from kernel to ubifs-utils Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 13/27] ubifs: ubifs_dump: add dump_ch and dump_node functions Dongsheng Yang
                   ` (17 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

We need some more functions in key.h, then copy it from kernel.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/key.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git a/ubifs-utils/include/key.h b/ubifs-utils/include/key.h
index d3a02d4..779981a 100644
--- a/ubifs-utils/include/key.h
+++ b/ubifs-utils/include/key.h
@@ -37,6 +37,8 @@
 #ifndef __UBIFS_KEY_H__
 #define __UBIFS_KEY_H__
 
+#include "defs.h"
+
 /**
  * key_mask_hash - mask a valid hash value.
  * @val: value to be masked
@@ -107,7 +109,7 @@ static inline void ino_key_init(union ubifs_key *key, ino_t inum)
  * @inum: parent inode number
  * @nm: direntry name and length
  */
-static inline void dent_key_init(const struct ubifs_info *c,
+static inline void dent_key_init(const struct ubifs_info *c __attribute__((unused)),
 				 union ubifs_key *key, ino_t inum,
 				 const struct qstr *nm)
 {
@@ -186,4 +188,76 @@ static inline int keys_cmp(const union ubifs_key *key1,
 	return 0;
 }
 
+/**
+ * key_read - transform a key to in-memory format.
+ * @c: UBIFS file-system description object
+ * @from: the key to transform
+ * @to: the key to store the result
+ */
+static inline void key_read(const struct ubifs_info *c __attribute__((unused)), const void *from,
+			    union ubifs_key *to)
+{
+	const union ubifs_key *f = from;
+
+	to->u32[0] = le32_to_cpu(f->j32[0]);
+	to->u32[1] = le32_to_cpu(f->j32[1]);
+}
+
+/**
+ * invalid_key_init - initialize invalid node key.
+ * @c: UBIFS file-system description object
+ * @key: key to initialize
+ *
+ * This is a helper function which marks a @key object as invalid.
+ */
+static inline void invalid_key_init(const struct ubifs_info *c __attribute__((unused)),
+				    union ubifs_key *key)
+{
+	key->u32[0] = 0xDEADBEAF;
+	key->u32[1] = UBIFS_INVALID_KEY;
+}
+/**
+ * key_type - get key type.
+ * @c: UBIFS file-system description object
+ * @key: key to get type of
+ */
+static inline int key_type(const struct ubifs_info *c __attribute__((unused)),
+			   const union ubifs_key *key)
+{
+	return key->u32[1] >> UBIFS_S_KEY_BLOCK_BITS;
+}
+
+/*
+ * key_hash - get directory entry hash.
+ * @c: UBIFS file-system description object
+ * @key: the key to get hash from
+ */
+static inline uint32_t key_hash(const struct ubifs_info *c __attribute__((unused)),
+				const union ubifs_key *key)
+{
+	return key->u32[1] & UBIFS_S_KEY_HASH_MASK;
+}
+
+/**
+ * key_inum - fetch inode number from key.
+ * @c: UBIFS file-system description object
+ * @k: key to fetch inode number from
+ */
+static inline ino_t key_inum(const struct ubifs_info *c __attribute__((unused)), const void *k)
+{
+	const union ubifs_key *key = k;
+
+	return key->u32[0];
+}
+
+/**
+ * key_block - get data block number.
+ * @c: UBIFS file-system description object
+ * @key: the key to get the block number from
+ */
+static inline unsigned int key_block(const struct ubifs_info *c __attribute__((unused)),
+				     const union ubifs_key *key)
+{
+	return key->u32[1] & UBIFS_S_KEY_BLOCK_MASK;
+}
 #endif /* !__UBIFS_KEY_H__ */
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 13/27] ubifs: ubifs_dump: add dump_ch and dump_node functions
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (11 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 12/27] ubifs: copy some important functions in key.h " Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 14/27] ubifs: defs.h: introduce some compatible definition for printk class Dongsheng Yang
                   ` (16 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/ubifs_dump/ubifs_dump.c | 337 ++++++++++++++++++++++++++++++++++++
 1 file changed, 337 insertions(+)

diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index bbe1269..a510aeb 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -1,7 +1,11 @@
 #include "ubifs_common.h"
 #define PROGRAM_NAME "ubifs-dump"
 #include "common.h"
+
 #include "io.h"
+#include "key.h"
+
+#define DBG_KEY_BUF_LEN		48
 
 static const char *optstring = "";
 
@@ -38,6 +42,339 @@ static int get_options(int argc, char**argv)
 	return 0;
 }
 
+static const char *node_type_to_str[] = {
+		"UBIFS_INO_NODE",
+		"UBIFS_DATA_NODE",
+		"UBIFS_DENT_NODE",
+		"UBIFS_XENT_NODE",
+		"UBIFS_TRUN_NODE",
+		"UBIFS_PAD_NODE",
+		"UBIFS_SB_NODE",
+		"UBIFS_MST_NODE",
+		"UBIFS_REF_NODE",
+		"UBIFS_IDX_NODE",
+		"UBIFS_CS_NODE",
+		"UBIFS_ORPH_NODE"
+};
+
+static const char *get_key_type(int type)
+{
+	return node_type_to_str[type];
+}
+
+static const char *group_type_to_str[] = {
+		"UBIFS_NO_NODE_GROUP",
+		"UBIFS_IN_NODE_GROUP",
+		"UBIFS_LAST_OF_NODE_GROUP"
+};
+
+static const char *hash_type_to_str[] = {
+		"UBIFS_KEY_HASH_R5",
+		"UBIFS_KEY_HASH_TEST"
+};
+
+static const char *compr_type_to_str[] = {
+		"UBIFS_COMPR_NONE",
+		"UBIFS_COMPR_LZO",
+		"UBIFS_COMPR_ZLIB",
+		"UBIFS_COMPR_TYPES_CNT",
+};
+
+static const char *key_fmt_to_str[] = {
+	"UBIFS_SIMPLE_KEY_FMT"
+};
+
+const char *dbg_snprintf_key(const struct ubifs_info *c,
+			     const union ubifs_key *key, char *buffer, int len)
+{
+	char *p = buffer;
+	int type = key_type(c, key);
+
+	if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
+		switch (type) {
+
+		case UBIFS_INO_KEY:
+			len -= snprintf(p, len, "(%lu, %s)",
+					(unsigned long)key_inum(c, key),
+					get_key_type(type));
+			break;
+		case UBIFS_DENT_KEY:
+		case UBIFS_XENT_KEY:
+			len -= snprintf(p, len, "(%lu, %s, %#08x)",
+					(unsigned long)key_inum(c, key),
+					get_key_type(type), key_hash(c, key));
+			break;
+		case UBIFS_DATA_KEY:
+			len -= snprintf(p, len, "(%lu, %s, %u)",
+					(unsigned long)key_inum(c, key),
+					get_key_type(type), key_block(c, key));
+			break;
+		case UBIFS_TRUN_KEY:
+			len -= snprintf(p, len, "(%lu, %s)",
+					(unsigned long)key_inum(c, key),
+					get_key_type(type));
+			break;
+		default:
+			len -= snprintf(p, len, "(bad key type: %#08x, %#08x)",
+					key->u32[0], key->u32[1]);
+		}
+	} else
+		len -= snprintf(p, len, "bad key format %d", c->key_fmt);
+	ubifs_assert(len > 0);
+	return p;
+}
+
+static void show_ch(const struct ubifs_ch *ch) 
+{ 
+	printf("\tCommon header: \n");
+        printf("\tmagic \t\t\t\t%#x\n", le32_to_cpu(ch->magic)); 
+        printf("\tcrc \t\t\t\t%#x\n", le32_to_cpu(ch->crc)); 
+        printf("\tnode_type \t\t\t%d (%s)\n", ch->node_type, 
+               node_type_to_str[ch->node_type]); 
+        printf("\tgroup_type \t\t\t%d (%s)\n", ch->group_type, 
+               group_type_to_str[ch->group_type]); 
+        printf("\tsqnum \t\t\t\t%llu\n", 
+               (unsigned long long)le64_to_cpu(ch->sqnum)); 
+        printf("\tlen \t\t\t\t%u\n", le32_to_cpu(ch->len)); 
+} 
+
+void dump_node(const struct ubifs_info *c, const void *node)
+{
+	int i, n;
+	union ubifs_key key;
+	const struct ubifs_ch *ch = node;
+	char key_buf[DBG_KEY_BUF_LEN];
+
+	show_ch(node);
+
+	switch (ch->node_type) {
+	case UBIFS_PAD_NODE:
+	{
+		const struct ubifs_pad_node *pad = node;
+
+		printf("\t\tpad_len \t\t\t%u\n", le32_to_cpu(pad->pad_len));
+		break;
+	}
+	case UBIFS_SB_NODE:
+	{
+		const struct ubifs_sb_node *sup = node;
+		unsigned int sup_flags = le32_to_cpu(sup->flags);
+		char uuid[40];
+
+		uuid_unparse_upper(sup->uuid, uuid);
+		printf("\t\tUUID \t\t\t\t%s\n", uuid);
+		printf("\t\tkey_hash \t\t\t%d (%s)\n",
+		       (int)sup->key_hash, hash_type_to_str[sup->key_hash]);
+		printf("\t\tkey_fmt \t\t\t%d (%s)\n",
+		       (int)sup->key_fmt, key_fmt_to_str[sup->key_fmt]);
+		printf("\t\tflags \t\t\t\t%#x\n", sup_flags);
+		printf("\t\tbig_lpt \t\t\t%u\n",
+		       !!(sup_flags & UBIFS_FLG_BIGLPT));
+		printf("\t\tspace_fixup \t\t\t%u\n",
+		       !!(sup_flags & UBIFS_FLG_SPACE_FIXUP));
+		printf("\t\tmin_io_size \t\t\t%u\n", le32_to_cpu(sup->min_io_size));
+		printf("\t\tleb_size \t\t\t%u\n", le32_to_cpu(sup->leb_size));
+		printf("\t\tleb_cnt \t\t\t%u\n", le32_to_cpu(sup->leb_cnt));
+		printf("\t\tmax_leb_cnt \t\t\t%u\n", le32_to_cpu(sup->max_leb_cnt));
+		printf("\t\tmax_bud_bytes \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(sup->max_bud_bytes));
+		printf("\t\tlog_lebs \t\t\t%u\n", le32_to_cpu(sup->log_lebs));
+		printf("\t\tlpt_lebs \t\t\t%u\n", le32_to_cpu(sup->lpt_lebs));
+		printf("\t\torph_lebs \t\t\t%u\n", le32_to_cpu(sup->orph_lebs));
+		printf("\t\tjhead_cnt \t\t\t%u\n", le32_to_cpu(sup->jhead_cnt));
+		printf("\t\tfanout \t\t\t\t%u\n", le32_to_cpu(sup->fanout));
+		printf("\t\tlsave_cnt \t\t\t%u\n", le32_to_cpu(sup->lsave_cnt));
+		printf("\t\tdefault_compr \t\t\t%u\n",
+		       (int)le16_to_cpu(sup->default_compr));
+		printf("\t\trp_size \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(sup->rp_size));
+		printf("\t\trp_uid \t\t\t\t%u\n", le32_to_cpu(sup->rp_uid));
+		printf("\t\trp_gid \t\t\t\t%u\n", le32_to_cpu(sup->rp_gid));
+		printf("\t\tfmt_version \t\t\t%u\n", le32_to_cpu(sup->fmt_version));
+		printf("\t\ttime_gran \t\t\t%u\n", le32_to_cpu(sup->time_gran));
+		break;
+	}
+	case UBIFS_MST_NODE:
+	{
+		const struct ubifs_mst_node *mst = node;
+
+		printf("\t\thighest_inum \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->highest_inum));
+		printf("\t\tcommit number \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->cmt_no));
+		printf("\t\tflags \t\t\t\t%#x\n", le32_to_cpu(mst->flags));
+		printf("\t\tlog_lnum \t\t\t%u\n", le32_to_cpu(mst->log_lnum));
+		printf("\t\troot_lnum \t\t\t%u\n", le32_to_cpu(mst->root_lnum));
+		printf("\t\troot_offs \t\t\t%u\n", le32_to_cpu(mst->root_offs));
+		printf("\t\troot_len \t\t\t%u\n", le32_to_cpu(mst->root_len));
+		printf("\t\tgc_lnum \t\t\t%u\n", le32_to_cpu(mst->gc_lnum));
+		printf("\t\tihead_lnum \t\t\t%u\n", le32_to_cpu(mst->ihead_lnum));
+		printf("\t\tihead_offs \t\t\t%u\n", le32_to_cpu(mst->ihead_offs));
+		printf("\t\tindex_size \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->index_size));
+		printf("\t\tlpt_lnum \t\t\t%u\n", le32_to_cpu(mst->lpt_lnum));
+		printf("\t\tlpt_offs \t\t\t%u\n", le32_to_cpu(mst->lpt_offs));
+		printf("\t\tnhead_lnum \t\t\t%u\n", le32_to_cpu(mst->nhead_lnum));
+		printf("\t\tnhead_offs \t\t\t%u\n", le32_to_cpu(mst->nhead_offs));
+		printf("\t\tltab_lnum \t\t\t%u\n", le32_to_cpu(mst->ltab_lnum));
+		printf("\t\tltab_offs \t\t\t%u\n", le32_to_cpu(mst->ltab_offs));
+		printf("\t\tlsave_lnum \t\t\t%u\n", le32_to_cpu(mst->lsave_lnum));
+		printf("\t\tlsave_offs \t\t\t%u\n", le32_to_cpu(mst->lsave_offs));
+		printf("\t\tlscan_lnum \t\t\t%u\n", le32_to_cpu(mst->lscan_lnum));
+		printf("\t\tleb_cnt \t\t\t%u\n", le32_to_cpu(mst->leb_cnt));
+		printf("\t\tempty_lebs \t\t\t%u\n", le32_to_cpu(mst->empty_lebs));
+		printf("\t\tidx_lebs \t\t\t%u\n", le32_to_cpu(mst->idx_lebs));
+		printf("\t\ttotal_free \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->total_free));
+		printf("\t\ttotal_dirty \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->total_dirty));
+		printf("\t\ttotal_used \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->total_used));
+		printf("\t\ttotal_dead \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->total_dead));
+		printf("\t\ttotal_dark \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(mst->total_dark));
+		break;
+	}
+	case UBIFS_REF_NODE:
+	{
+		const struct ubifs_ref_node *ref = node;
+
+		printf("\t\tlnum \t\t\t\t%u\n", le32_to_cpu(ref->lnum));
+		printf("\t\toffs \t\t\t\t%u\n", le32_to_cpu(ref->offs));
+		printf("\t\tjhead \t\t\t\t%u\n", le32_to_cpu(ref->jhead));
+		break;
+	}
+	case UBIFS_INO_NODE:
+	{
+		const struct ubifs_ino_node *ino = node;
+
+		key_read(c, &ino->key, &key);
+		printf("\t\tkey \t\t\t%s\n",
+		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
+		printf("\t\tcreat_sqnum \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(ino->creat_sqnum));
+		printf("\t\tsize \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(ino->size));
+		printf("\t\tnlink \t\t\t%u\n", le32_to_cpu(ino->nlink));
+		printf("\t\tatime \t\t\t%lld.%u\n",
+		       (long long)le64_to_cpu(ino->atime_sec),
+		       le32_to_cpu(ino->atime_nsec));
+		printf("\t\tmtime \t\t\t%lld.%u\n",
+		       (long long)le64_to_cpu(ino->mtime_sec),
+		       le32_to_cpu(ino->mtime_nsec));
+		printf("\t\tctime \t\t\t%lld.%u\n",
+		       (long long)le64_to_cpu(ino->ctime_sec),
+		       le32_to_cpu(ino->ctime_nsec));
+		printf("\t\tuid \t\t\t%u\n", le32_to_cpu(ino->uid));
+		printf("\t\tgid \t\t\t%u\n", le32_to_cpu(ino->gid));
+		printf("\t\tmode \t\t\t%u\n", le32_to_cpu(ino->mode));
+		printf("\t\tflags \t\t\t%#x\n", le32_to_cpu(ino->flags));
+		printf("\t\txattr_cnt \t\t\t%u\n", le32_to_cpu(ino->xattr_cnt));
+		printf("\t\txattr_size \t\t\t%u\n", le32_to_cpu(ino->xattr_size));
+		printf("\t\txattr_names \t\t\t%u\n", le32_to_cpu(ino->xattr_names));
+		printf("\t\tcompr_type \t\t\t%#x\n",
+		       (int)le16_to_cpu(ino->compr_type));
+		printf("\t\tdata len \t\t\t%u\n", le32_to_cpu(ino->data_len));
+		break;
+	}
+	case UBIFS_DENT_NODE:
+	case UBIFS_XENT_NODE:
+	{
+		const struct ubifs_dent_node *dent = node;
+		int nlen = le16_to_cpu(dent->nlen);
+
+		key_read(c, &dent->key, &key);
+		printf("\t\tkey \t\t\t%s\n",
+		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
+		printf("\t\tinum \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(dent->inum));
+		printf("\t\ttype \t\t\t%d\n", (int)dent->type);
+		printf("\t\tnlen \t\t\t%d\n", nlen);
+		printf("\t\tname           ");
+
+		if (nlen > UBIFS_MAX_NLEN)
+			printf("(bad name length, not printing, bad or corrupted node)");
+		else {
+			for (i = 0; i < nlen && dent->name[i]; i++)
+				printf("%c", dent->name[i]);
+		}
+		printf("\n");
+
+		break;
+	}
+	case UBIFS_DATA_NODE:
+	{
+		const struct ubifs_data_node *dn = node;
+		int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
+
+		key_read(c, &dn->key, &key);
+		printf("\t\tkey \t\t\t%s\n",
+		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
+		printf("\t\tsize \t\t\t%u\n", le32_to_cpu(dn->size));
+		printf("\t\tcompr_typ \t\t\t%d\n",
+		       (int)le16_to_cpu(dn->compr_type));
+		printf("\t\tdata size \t\t\t%d\n", dlen);
+		break;
+	}
+	case UBIFS_TRUN_NODE:
+	{
+		const struct ubifs_trun_node *trun = node;
+
+		printf("\t\tinum \t\t\t%u\n", le32_to_cpu(trun->inum));
+		printf("\t\told_size \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(trun->old_size));
+		printf("\t\tnew_size \t\t\t%llu\n",
+		       (unsigned long long)le64_to_cpu(trun->new_size));
+		break;
+	}
+	case UBIFS_IDX_NODE:
+	{
+		const struct ubifs_idx_node *idx = node;
+
+		n = le16_to_cpu(idx->child_cnt);
+		printf("\t\tchild_cnt \t\t%d\n", n);
+		printf("\t\tlevel \t\t\t%d\n", (int)le16_to_cpu(idx->level));
+		printf("\t\tBranches:\n");
+
+		for (i = 0; i < n && i < c->fanout - 1; i++) {
+			const struct ubifs_branch *br;
+
+			br = ubifs_idx_branch(c, idx, i);
+			key_read(c, &br->key, &key);
+			printf("\t\t%d: LEB %d:%d len %d key %s\n",
+			       i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs),
+			       le32_to_cpu(br->len),
+			       dbg_snprintf_key(c, &key, key_buf,
+						DBG_KEY_BUF_LEN));
+		}
+		break;
+	}
+	case UBIFS_CS_NODE:
+		break;
+	case UBIFS_ORPH_NODE:
+	{
+		const struct ubifs_orph_node *orph = node;
+
+		printf("\t\tcommit number \t\t\t%llu\n",
+		       (unsigned long long)
+				le64_to_cpu(orph->cmt_no) & LLONG_MAX);
+		printf("\t\tlast node flag \t\t\t%llu\n",
+		       (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);
+		n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3;
+		printf("\t\t%d orphan inode numbers:\n", n);
+		for (i = 0; i < n; i++)
+			printf("\t\t  ino \t\t\t%llu\n",
+			       (unsigned long long)le64_to_cpu(orph->inos[i]));
+		break;
+	}
+	default:
+		printf("node type \t\t\t%d was not recognized\n",
+		       (int)ch->node_type);
+	}
+	printf("\n");
+}
+
 static int dump()
 {
 	return 0;
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 14/27] ubifs: defs.h: introduce some compatible definition for printk class
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (12 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 13/27] ubifs: ubifs_dump: add dump_ch and dump_node functions Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 15/27] ubifs: io: introduce ubifs_read function to read ubi volume Dongsheng Yang
                   ` (15 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

There are some functions of printk class in code copied from kernel.
Then add a compatible definitions in defs.h for them.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/defs.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/ubifs-utils/include/defs.h b/ubifs-utils/include/defs.h
index 1fa3316..739af7f 100644
--- a/ubifs-utils/include/defs.h
+++ b/ubifs-utils/include/defs.h
@@ -81,6 +81,15 @@ __res = ((unsigned long) n) % (unsigned) base; \
 n = ((unsigned long) n) / (unsigned) base; \
 __res; })
 
+/*
+ * printk
+ */
+#define printk(fmt, args...) fprintf(stderr, fmt, ##args)
+#define	KERN_CRIT	""
+#define KERN_ERR	""
+#define ubifs_err(c, fmt, args...) printk(fmt, ##args)
+#define pr_err(fmt, args...) printk(fmt, ##args)
+
 #if INT_MAX != 0x7fffffff
 #error : sizeof(int) must be 4 for this program
 #endif
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 15/27] ubifs: io: introduce ubifs_read function to read ubi volume
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (13 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 14/27] ubifs: defs.h: introduce some compatible definition for printk class Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 16/27] ubifs: ubifs_dump: dump super block Dongsheng Yang
                   ` (14 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Implement a ubifs_read function in io lib. This function
will read the data at offset in length of len from ubi volume
to buf.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/io.h |  2 ++
 ubifs-utils/lib/io.c     | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/ubifs-utils/include/io.h b/ubifs-utils/include/io.h
index 920645d..11f568c 100644
--- a/ubifs-utils/include/io.h
+++ b/ubifs-utils/include/io.h
@@ -16,4 +16,6 @@ int write_leb(struct ubifs_info *c, int lnum, int len, void *buf);
 int close_target(void);
 int open_target(struct ubifs_info *c, int yes);
 int open_ubi(struct ubifs_info *c, const char *node);
+
+int ubifs_read(loff_t offset, int len, void *buf);
 #endif
diff --git a/ubifs-utils/lib/io.c b/ubifs-utils/lib/io.c
index 9817d2a..d05cf86 100644
--- a/ubifs-utils/lib/io.c
+++ b/ubifs-utils/lib/io.c
@@ -132,3 +132,21 @@ int write_leb(struct ubifs_info *c, int lnum, int len, void *buf)
 
 	return 0;
 }
+
+/**
+ * ubifs_read - read data from ubi volume.
+ * @offset: offset of data in volume
+ * @len: length of data in the buffer
+ * @buf: buffer (must be at least c->leb_size bytes)
+ */
+int ubifs_read(loff_t offset, int len, void *buf)
+{
+	if (lseek(out_fd, offset, SEEK_SET) != offset)
+		return sys_err_msg("lseek failed seeking %"PRIdoff_t, offset);
+
+	if (read(out_fd, buf, len) != len)
+		return sys_err_msg("write failed writing %d bytes at pos %"PRIdoff_t,
+				   len, offset);
+
+	return 0;
+}
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 16/27] ubifs: ubifs_dump: dump super block
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (14 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 15/27] ubifs: io: introduce ubifs_read function to read ubi volume Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 17/27] ubifs: introduce scan for ubifs-utils Dongsheng Yang
                   ` (13 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Read the super block from ubi volume and dump it out.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/lpt.h           |   1 +
 ubifs-utils/lib/lpt.c               |  29 +++++++
 ubifs-utils/ubifs_dump/ubifs_dump.c | 151 ++++++++++++++++++++++++++++++++++--
 3 files changed, 173 insertions(+), 8 deletions(-)

diff --git a/ubifs-utils/include/lpt.h b/ubifs-utils/include/lpt.h
index 4cde59d..e2f7348 100644
--- a/ubifs-utils/include/lpt.h
+++ b/ubifs-utils/include/lpt.h
@@ -24,5 +24,6 @@
 
 int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs, int *big_lpt);
 int create_lpt(struct ubifs_info *c);
+int ubifs_calc_lpt_geom(struct ubifs_info *c);
 
 #endif
diff --git a/ubifs-utils/lib/lpt.c b/ubifs-utils/lib/lpt.c
index 100d747..26fb4dd 100644
--- a/ubifs-utils/lib/lpt.c
+++ b/ubifs-utils/lib/lpt.c
@@ -585,3 +585,32 @@ out:
 	free(pnode);
 	return err;
 }
+
+/**
+ * ubifs_calc_lpt_geom - calculate and check sizes for the LPT area.
+ * @c: the UBIFS file-system description object
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int ubifs_calc_lpt_geom(struct ubifs_info *c)
+{
+	int lebs_needed;
+	long long sz;
+
+	do_calc_lpt_geom(c);
+
+	/* Verify that lpt_lebs is big enough */
+	sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
+	lebs_needed = (sz + c->leb_size - 1) / c->leb_size;
+	if (lebs_needed > c->lpt_lebs) {
+		ubifs_err(c, "too few LPT LEBs, %d", lebs_needed);
+		return -EINVAL;
+	}
+
+	/* Verify that ltab fits in a single LEB (since ltab is a single node */
+	if (c->ltab_sz > c->leb_size) {
+		ubifs_err(c, "LPT ltab too big");
+		return -EINVAL;
+	}
+	return 0;
+}
diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index a510aeb..9ddb0f7 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -4,6 +4,7 @@
 
 #include "io.h"
 #include "key.h"
+#include "lpt.h"
 
 #define DBG_KEY_BUF_LEN		48
 
@@ -16,6 +17,13 @@ static const struct option longopts[] = {
 struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
 
+/* Global buffers */
+static void *leb_buf;
+
+/* Global nodes*/
+struct ubifs_mst_node mst;
+struct ubifs_sb_node sup;
+
 static int get_options(int argc, char**argv)
 {
 	int opt, i;
@@ -73,13 +81,6 @@ static const char *hash_type_to_str[] = {
 		"UBIFS_KEY_HASH_TEST"
 };
 
-static const char *compr_type_to_str[] = {
-		"UBIFS_COMPR_NONE",
-		"UBIFS_COMPR_LZO",
-		"UBIFS_COMPR_ZLIB",
-		"UBIFS_COMPR_TYPES_CNT",
-};
-
 static const char *key_fmt_to_str[] = {
 	"UBIFS_SIMPLE_KEY_FMT"
 };
@@ -375,11 +376,145 @@ void dump_node(const struct ubifs_info *c, const void *node)
 	printf("\n");
 }
 
-static int dump()
+/**
+ * init - initialize things.
+ */
+static int init(void)
+{
+	leb_buf = malloc(c->leb_size);
+	if (!leb_buf)
+		return err_msg("out of memory");
+
+	return 0;
+}
+
+/**
+ * deinit - deinitialize things.
+ */
+static void deinit(void)
+{
+	free(leb_buf);
+}
+
+/*
+ * init_constants_sb - initialize UBIFS constants.
+ * @c: UBIFS file-system description object
+ *
+ * This is a helper function which initializes various UBIFS constants after
+ * the superblock has been read. It also checks various UBIFS parameters and
+ * makes sure they are all right. Returns zero in case of success and a
+ * negative error code in case of failure.
+ */
+static int init_constants_sb(struct ubifs_info *c)
 {
+	int tmp, err;
+
+	tmp = ubifs_idx_node_sz(c, 1);
+	c->min_idx_node_sz = ALIGN(tmp, 8);
+
+	tmp = ubifs_idx_node_sz(c, c->fanout);
+	c->max_idx_node_sz = ALIGN(tmp, 8);
+
+	c->max_znode_sz = sizeof(struct ubifs_znode) +
+			c->fanout * sizeof(struct ubifs_zbranch);
+	/* Make sure LEB size is large enough to fit full commit */
+	tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
+	tmp = ALIGN(tmp, c->min_io_size);
+	if (tmp > c->leb_size) {
+		printf("too small LEB size %d, at least %d needed",
+			  c->leb_size, tmp);
+		return -EINVAL;
+	}
+
+	err = ubifs_calc_lpt_geom(c);
+	if (err)
+		return err;
+
 	return 0;
 }
 
+static int dump_super(void)
+{
+	int err = 0;
+	unsigned long sup_flags;
+
+	memset(&sup, 0, UBIFS_SB_NODE_SZ);
+	err = ubifs_read(0, UBIFS_SB_NODE_SZ, &sup);
+	if (err)
+		return err;
+
+	printf("SUPER BLOCK: \n");
+	dump_node(c, &sup);
+
+        switch (sup.key_hash) {
+        case UBIFS_KEY_HASH_R5:
+                c->key_hash = key_r5_hash;
+                c->key_hash_type = UBIFS_KEY_HASH_R5;
+                break;
+
+        case UBIFS_KEY_HASH_TEST:
+                c->key_hash = key_test_hash;
+                c->key_hash_type = UBIFS_KEY_HASH_TEST;
+                break;
+        };
+
+	c->key_fmt = sup.key_fmt;
+
+	switch (c->key_fmt) {
+	case UBIFS_SIMPLE_KEY_FMT:
+		c->key_len = UBIFS_SK_LEN;
+		break;
+	default:
+		printf("unsupported key format");
+		err = -EINVAL;
+		return err;
+	}
+
+	c->leb_size	 = le32_to_cpu(sup.leb_size);
+	c->min_io_size	 = le32_to_cpu(sup.min_io_size);
+	c->leb_cnt       = le32_to_cpu(sup.leb_cnt);
+	c->max_leb_cnt   = le32_to_cpu(sup.max_leb_cnt);
+	c->max_bud_bytes = le64_to_cpu(sup.max_bud_bytes);
+	c->log_lebs      = le32_to_cpu(sup.log_lebs);
+	c->lpt_lebs      = le32_to_cpu(sup.lpt_lebs);
+	c->orph_lebs     = le32_to_cpu(sup.orph_lebs);
+	c->jhead_cnt     = le32_to_cpu(sup.jhead_cnt) + NONDATA_JHEADS_CNT;
+	c->fanout        = le32_to_cpu(sup.fanout);
+	c->lsave_cnt     = le32_to_cpu(sup.lsave_cnt);
+	c->rp_size       = le64_to_cpu(sup.rp_size);
+	sup_flags        = le32_to_cpu(sup.flags);
+	c->default_compr = le16_to_cpu(sup.default_compr);
+
+	c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
+	c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
+
+	/* Automatically increase file system size to the maximum size */
+	c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
+	c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
+	c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
+	c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
+	c->main_first = c->leb_cnt - c->main_lebs;
+
+	return init_constants_sb(c);
+}
+
+static int dump()
+{
+	int err = 0;
+
+	err = init();
+	if (err)
+		goto out;
+
+	err = dump_super();
+	if (err)
+		goto out;
+
+out:
+	deinit();
+	return err;
+}
+
 int main(int argc, char *argv[])
 {
 	int err;
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 17/27] ubifs: introduce scan for ubifs-utils
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (15 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 16/27] ubifs: ubifs_dump: dump super block Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 18/27] ubifs: add some more compatible definitions in defs.h Dongsheng Yang
                   ` (12 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

It's a lib to scan a leb.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/scan.h |   8 ++
 ubifs-utils/lib/scan.c     | 318 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 326 insertions(+)
 create mode 100644 ubifs-utils/include/scan.h
 create mode 100644 ubifs-utils/lib/scan.c

diff --git a/ubifs-utils/include/scan.h b/ubifs-utils/include/scan.h
new file mode 100644
index 0000000..8447adf
--- /dev/null
+++ b/ubifs-utils/include/scan.h
@@ -0,0 +1,8 @@
+#ifndef __UBIFS_SCAN_H__
+#define __UBIFS_SCAN_H__
+
+struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
+				  int offs, void *sbuf, int quiet);
+
+void ubifs_scan_destroy(struct ubifs_scan_leb *sleb);
+#endif
diff --git a/ubifs-utils/lib/scan.c b/ubifs-utils/lib/scan.c
new file mode 100644
index 0000000..69c84d1
--- /dev/null
+++ b/ubifs-utils/lib/scan.c
@@ -0,0 +1,318 @@
+/*
+ * This file is part of UBIFS.
+ *
+ * Copied from kernel
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * 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., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/*
+ * This file implements the scan which is a general-purpose function for
+ * determining what nodes are in an eraseblock. The scan is used to replay the
+ * journal, to do garbage collection. for the TNC in-the-gaps method, and by
+ * debugging functions.
+ */
+#include "ubifs_common.h"
+
+#include "ubifs.h"
+#define PROGRAM_NAME "ubifs_scan"
+#include "common.h"
+#include "key.h"
+#include "io.h"
+#include "scan.h"
+
+/**
+ * scan_padding_bytes - scan for padding bytes.
+ * @buf: buffer to scan
+ * @len: length of buffer
+ *
+ * This function returns the number of padding bytes on success and
+ * %SCANNED_GARBAGE on failure.
+ */
+static int scan_padding_bytes(void *buf, int len)
+{
+	int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
+	int *p = buf;
+
+	while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
+		pad_len += 1;
+
+	if (!pad_len || (pad_len & 7)) {
+		return SCANNED_GARBAGE;
+	}
+
+	return pad_len;
+}
+
+/**
+ * ubifs_scan_a_node - scan for a node or padding.
+ * @c: UBIFS file-system description object
+ * @buf: buffer to scan
+ * @len: length of buffer
+ * @lnum: logical eraseblock number
+ * @offs: offset within the logical eraseblock
+ * @quiet: print no messages
+ *
+ * This function returns a scanning code to indicate what was scanned.
+ */
+int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len,
+		      int lnum __attribute__((unused)), int offs,
+		      int quiet __attribute__((unused)))
+{
+	struct ubifs_ch *ch = buf;
+	uint32_t magic;
+
+	magic = le32_to_cpu(ch->magic);
+
+	if (magic == 0xFFFFFFFF)
+		return SCANNED_EMPTY_SPACE;
+
+	if (magic != UBIFS_NODE_MAGIC)
+		return scan_padding_bytes(buf, len);
+
+	if (len < UBIFS_CH_SZ) {
+		return SCANNED_GARBAGE;
+	}
+
+	if (ch->node_type == UBIFS_PAD_NODE) {
+		struct ubifs_pad_node *pad = buf;
+		int pad_len = le32_to_cpu(pad->pad_len);
+		int node_len = le32_to_cpu(ch->len);
+
+		/* Validate the padding node */
+		if (pad_len < 0 ||
+		    offs + node_len + pad_len > c->leb_size) {
+			return SCANNED_A_BAD_PAD_NODE;
+		}
+
+		/* Make the node pads to 8-byte boundary */
+		if ((node_len + pad_len) & 7) {
+			return SCANNED_A_BAD_PAD_NODE;
+		}
+
+		return node_len + pad_len;
+	}
+
+	return SCANNED_A_NODE;
+}
+
+/**
+ * ubifs_start_scan - create LEB scanning information at start of scan.
+ * @c: UBIFS file-system description object
+ * @lnum: logical eraseblock number
+ * @offs: offset to start at (usually zero)
+ * @sbuf: scan buffer (must be c->leb_size)
+ *
+ * This function returns the scanned information on success and a negative error
+ * code on failure.
+ */
+struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
+					int offs, void *sbuf)
+{
+	struct ubifs_scan_leb *sleb;
+	int err;
+
+	sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
+	if (!sleb)
+		return ERR_PTR(-ENOMEM);
+
+	sleb->lnum = lnum;
+	INIT_LIST_HEAD(&sleb->nodes);
+	sleb->buf = sbuf;
+
+	err = ubifs_read(lnum * c->leb_size + offs, c->leb_size - offs, sbuf + offs);
+	if (err && err != -EBADMSG) {
+		kfree(sleb);
+		return ERR_PTR(err);
+	}
+
+	/*
+	 * Note, we ignore integrity errors (EBASMSG) because all the nodes are
+	 * protected by CRC checksums.
+	 */
+	return sleb;
+}
+
+/**
+ * ubifs_end_scan - update LEB scanning information at end of scan.
+ * @c: UBIFS file-system description object
+ * @sleb: scanning information
+ * @lnum: logical eraseblock number
+ * @offs: offset to start at (usually zero)
+ */
+void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
+		    int lnum, int offs)
+{
+	lnum = lnum;
+	ubifs_assert(offs % c->min_io_size == 0);
+
+	sleb->endpt = ALIGN(offs, c->min_io_size);
+}
+
+/**
+ * ubifs_add_snod - add a scanned node to LEB scanning information.
+ * @c: UBIFS file-system description object
+ * @sleb: scanning information
+ * @buf: buffer containing node
+ * @offs: offset of node on flash
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
+		   void *buf, int offs)
+{
+	struct ubifs_ch *ch = buf;
+	struct ubifs_ino_node *ino = buf;
+	struct ubifs_scan_node *snod;
+
+	snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
+	if (!snod)
+		return -ENOMEM;
+
+	snod->sqnum = le64_to_cpu(ch->sqnum);
+	snod->type = ch->node_type;
+	snod->offs = offs;
+	snod->len = le32_to_cpu(ch->len);
+	snod->node = buf;
+
+	switch (ch->node_type) {
+	case UBIFS_INO_NODE:
+	case UBIFS_DENT_NODE:
+	case UBIFS_XENT_NODE:
+	case UBIFS_DATA_NODE:
+		/*
+		 * The key is in the same place in all keyed
+		 * nodes.
+		 */
+		key_read(c, &ino->key, &snod->key);
+		break;
+	default:
+		invalid_key_init(c, &snod->key);
+		break;
+	}
+	list_add_tail(&snod->list, &sleb->nodes);
+	sleb->nodes_cnt += 1;
+	return 0;
+}
+
+/**
+ * ubifs_scan - scan a logical eraseblock.
+ * @c: UBIFS file-system description object
+ * @lnum: logical eraseblock number
+ * @offs: offset to start at (usually zero)
+ * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
+ * @quiet: print no messages
+ *
+ * This function scans LEB number @lnum and returns complete information about
+ * its contents. Returns the scanned information in case of success and,
+ * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
+ * of failure.
+ *
+ * If @quiet is non-zero, this function does not print large and scary
+ * error messages and flash dumps in case of errors.
+ */
+struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
+				  int offs, void *sbuf, int quiet)
+{
+	void *buf = sbuf + offs;
+	int err, len = c->leb_size - offs;
+	struct ubifs_scan_leb *sleb;
+
+	sleb = ubifs_start_scan(c, lnum, offs, sbuf);
+	if (IS_ERR(sleb))
+		return sleb;
+
+	while (len >= 8) {
+		struct ubifs_ch *ch = buf;
+		int node_len, ret;
+
+		cond_resched();
+
+		ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
+		if (ret > 0) {
+			/* Padding bytes or a valid padding node */
+			offs += ret;
+			buf += ret;
+			len -= ret;
+			continue;
+		}
+
+		if (ret == SCANNED_EMPTY_SPACE)
+			/* Empty space is checked later */
+			break;
+
+		switch (ret) {
+		case SCANNED_GARBAGE:
+			if (offs == 0)
+				break;
+			goto corrupted;
+		case SCANNED_A_NODE:
+			break;
+		case SCANNED_A_CORRUPT_NODE:
+		case SCANNED_A_BAD_PAD_NODE:
+			goto corrupted;
+		default:
+			err = -EINVAL;
+			goto error;
+		}
+
+		err = ubifs_add_snod(c, sleb, buf, offs);
+		if (err)
+			goto error;
+
+		node_len = ALIGN(le32_to_cpu(ch->len), 8);
+		offs += node_len;
+		buf += node_len;
+		len -= node_len;
+	}
+
+	if (offs % c->min_io_size) {
+		goto corrupted;
+	}
+
+	ubifs_end_scan(c, sleb, lnum, offs);
+
+	for (; len > 2; offs += 2, buf = buf + 2, len -= 2)
+		if ((*(const uint8_t *)buf) != 0xff)
+			goto corrupted;
+
+	return sleb;
+
+corrupted:
+	err = -EUCLEAN;
+	ubifs_scan_destroy(sleb);
+	return ERR_PTR(err);
+
+error:
+	ubifs_scan_destroy(sleb);
+	return ERR_PTR(err);
+}
+
+/**
+ * ubifs_scan_destroy - destroy LEB scanning information.
+ * @sleb: scanning information to free
+ */
+void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
+{
+	struct ubifs_scan_node *node;
+	struct list_head *head;
+
+	head = &sleb->nodes;
+	while (!list_empty(head)) {
+		node = list_entry(head->next, struct ubifs_scan_node, list);
+		list_del(&node->list);
+		kfree(node);
+	}
+	kfree(sleb);
+}
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 18/27] ubifs: add some more compatible definitions in defs.h
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (16 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 17/27] ubifs: introduce scan for ubifs-utils Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 19/27] ubifs: ubifs_dump: dump master node Dongsheng Yang
                   ` (11 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Add kmalloc class compatible definitions to defs.h.
Add error pointer class compatible definitions to defs.h.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/defs.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 84 insertions(+)

diff --git a/ubifs-utils/include/defs.h b/ubifs-utils/include/defs.h
index 739af7f..ed42ab1 100644
--- a/ubifs-utils/include/defs.h
+++ b/ubifs-utils/include/defs.h
@@ -6,6 +6,37 @@
 #ifndef __UBIFS_DEFS_H__
 #define __UBIFS_DEFS_H__
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+#include <endian.h>
+#include <byteswap.h>
+#include <assert.h>
+#include <stddef.h>
+#include <linux/types.h>
+#include <stdint.h>
+
+#include <features.h>
+
+#ifndef __GLIBC__
+#define BTRFS_DISABLE_BACKTRACE
+#define __always_inline __inline __attribute__ ((__always_inline__))
+#endif
+
+#ifndef BTRFS_DISABLE_BACKTRACE
+#include <execinfo.h>
+#endif
+
+#define ptr_to_u64(x)	((u64)(uintptr_t)x)
+#define u64_to_ptr(x)	((void *)(uintptr_t)x)
+
+#ifndef READ
+#define READ 0
+#define WRITE 1
+#define READA 2
+#endif
+
 #define t16(x) ({ \
 	uint16_t __b = (x); \
 	(__LITTLE_ENDIAN==__BYTE_ORDER) ? __b : bswap_16(__b); \
@@ -82,6 +113,41 @@ n = ((unsigned long) n) / (unsigned) base; \
 __res; })
 
 /*
+ * error pointer
+ */
+#define MAX_ERRNO	4095
+#define IS_ERR_VALUE(x) ((x) >= (unsigned long)-MAX_ERRNO)
+
+static inline void *ERR_PTR(long error)
+{
+	return (void *) error;
+}
+
+static inline long PTR_ERR(const void *ptr)
+{
+	return (long) ptr;
+}
+
+static inline long IS_ERR(const void *ptr)
+{
+	return IS_ERR_VALUE((unsigned long)ptr);
+}
+
+#define cond_resched()		do { } while (0)
+#define preempt_enable()	do { } while (0)
+#define preempt_disable()	do { } while (0)
+
+/*
+ * kmalloc/kfree
+ */
+#define kmalloc(x, y) malloc(x)
+#define kzalloc(x, y) calloc(1, x)
+#define kstrdup(x, y) strdup(x)
+#define kfree(x) free(x)
+#define vmalloc(x) malloc(x)
+#define vfree(x) free(x)
+
+/*
  * printk
  */
 #define printk(fmt, args...) fprintf(stderr, fmt, ##args)
@@ -90,6 +156,24 @@ __res; })
 #define ubifs_err(c, fmt, args...) printk(fmt, ##args)
 #define pr_err(fmt, args...) printk(fmt, ##args)
 
+#define container_of(ptr, type, member) ({                      \
+        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
+	        (type *)( (char *)__mptr - offsetof(type,member) );})
+
+#define gfp_t int
+#define get_cpu_var(p) (p)
+#define __get_cpu_var(p) (p)
+#define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
+#define __GFP_BITS_SHIFT 20
+#define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
+#define GFP_KERNEL 0
+#define GFP_NOFS 0
+#define __read_mostly
+
+#ifndef ULONG_MAX
+#define ULONG_MAX       (~0UL)
+#endif
+
 #if INT_MAX != 0x7fffffff
 #error : sizeof(int) must be 4 for this program
 #endif
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 19/27] ubifs: ubifs_dump: dump master node
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (17 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 18/27] ubifs: add some more compatible definitions in defs.h Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-10-08 10:07   ` David Gstir
  2015-10-09  4:05   ` [PATCH v2] " Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 20/27] ubifs: ubifs_dump: dump log area Dongsheng Yang
                   ` (10 subsequent siblings)
  29 siblings, 2 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Search the master lebs and found the latest master node.
Then dump it out.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                            |   1 +
 ubifs-utils/ubifs_dump/ubifs_dump.c | 142 +++++++++++++++++++++++++++++-------
 2 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/Makefile b/Makefile
index cad71dc..7abbef9 100644
--- a/Makefile
+++ b/Makefile
@@ -133,6 +133,7 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 $(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
 
 obj-ubifs_dump = $(UBIFS_LIBS)
+obj-ubifs_dump += ../lib/scan.o
 LDFLAGS_ubifs_dump = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_ubifs_dump = -lz -llzo2 -lm -luuid
 $(call mkdep,ubifs-utils/ubifs_dump/,ubifs_dump,,ubi-utils/libubi.a)
diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 9ddb0f7..73f68b7 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -2,9 +2,11 @@
 #define PROGRAM_NAME "ubifs-dump"
 #include "common.h"
 
+#include "ubifs.h"
 #include "io.h"
 #include "key.h"
 #include "lpt.h"
+#include "scan.h"
 
 #define DBG_KEY_BUF_LEN		48
 
@@ -17,9 +19,6 @@ static const struct option longopts[] = {
 struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
 
-/* Global buffers */
-static void *leb_buf;
-
 /* Global nodes*/
 struct ubifs_mst_node mst;
 struct ubifs_sb_node sup;
@@ -376,26 +375,6 @@ void dump_node(const struct ubifs_info *c, const void *node)
 	printf("\n");
 }
 
-/**
- * init - initialize things.
- */
-static int init(void)
-{
-	leb_buf = malloc(c->leb_size);
-	if (!leb_buf)
-		return err_msg("out of memory");
-
-	return 0;
-}
-
-/**
- * deinit - deinitialize things.
- */
-static void deinit(void)
-{
-	free(leb_buf);
-}
-
 /*
  * init_constants_sb - initialize UBIFS constants.
  * @c: UBIFS file-system description object
@@ -498,20 +477,129 @@ static int dump_super(void)
 	return init_constants_sb(c);
 }
 
-static int dump()
+/**
+ * scan_for_master - search the valid master node.
+ * @c: UBIFS file-system description object
+ *
+ * This function scans the master node LEBs and search for the latest master
+ * node. Returns zero in case of success, %-EUCLEAN if there master area is
+ * corrupted and requires recovery, and a negative error code in case of
+ * failure.
+ */
+static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node)
 {
+	struct ubifs_scan_leb *sleb;
+	struct ubifs_scan_node *snod;
+	int lnum, offs = 0, nodes_cnt;
+	static void *leb_buf;
 	int err = 0;
+	
+	lnum = UBIFS_MST_LNUM;
 
-	err = init();
-	if (err)
+	leb_buf = malloc(c->leb_size);
+	if (!leb_buf)
+		return -ENOMEM;
+
+	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
+	if (IS_ERR(sleb)) {
+		err = PTR_ERR(sleb);
 		goto out;
+	}
+	nodes_cnt = sleb->nodes_cnt;
+	if (nodes_cnt > 0) {
+		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
+				  list);
+		if (snod->type != UBIFS_MST_NODE) {
+			err = -EINVAL;
+			goto out;
+		}
+		memcpy(mst_node, snod->node, snod->len);
+		offs = snod->offs;
+	}
+	ubifs_scan_destroy(sleb);
+
+	lnum += 1;
+
+	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
+	if (IS_ERR(sleb)) {
+		err = PTR_ERR(sleb);
+		goto out;
+	}
+	err = -EUCLEAN;
+	if (sleb->nodes_cnt != nodes_cnt)
+		goto out;
+	if (!sleb->nodes_cnt)
+		goto out;
+	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
+	if (snod->type != UBIFS_MST_NODE) {
+		err = -EINVAL;
+		goto out;
+	}
+	if (snod->offs != offs)
+		goto out;
+	if (memcmp((void *)mst_node + UBIFS_CH_SZ,
+		   (void *)snod->node + UBIFS_CH_SZ,
+		   UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
+		goto out;
+	err = 0;
+
+out:
+	free(leb_buf);
+	ubifs_scan_destroy(sleb);
+	return err;
+}
+
+static int dump_master(void)
+{
+	int err = 0;
+
+	printf("MASTER: \n");
+	err = scan_for_master(c, &mst);
+	if (err)
+		return err;
+	dump_node(c, &mst);
+	mst.flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
+
+	c->max_sqnum       = le64_to_cpu(mst.ch.sqnum);
+	c->highest_inum    = le64_to_cpu(mst.highest_inum);
+	c->zroot.lnum      = le32_to_cpu(mst.root_lnum);
+	c->zroot.offs      = le32_to_cpu(mst.root_offs);
+	c->zroot.len       = le32_to_cpu(mst.root_len);
+	c->gc_lnum         = le32_to_cpu(mst.gc_lnum);
+	c->ihead_lnum      = le32_to_cpu(mst.ihead_lnum);
+	c->ihead_offs      = le32_to_cpu(mst.ihead_offs);
+	c->lpt_lnum        = le32_to_cpu(mst.lpt_lnum);
+	c->lpt_offs        = le32_to_cpu(mst.lpt_offs);
+	c->nhead_lnum      = le32_to_cpu(mst.nhead_lnum);
+	c->nhead_offs      = le32_to_cpu(mst.nhead_offs);
+	c->ltab_lnum       = le32_to_cpu(mst.ltab_lnum);
+	c->ltab_offs       = le32_to_cpu(mst.ltab_offs);
+	c->lsave_lnum      = le32_to_cpu(mst.lsave_lnum);
+	c->lsave_offs      = le32_to_cpu(mst.lsave_offs);
+	c->lscan_lnum      = le32_to_cpu(mst.lscan_lnum);
+	c->lst.empty_lebs  = le32_to_cpu(mst.empty_lebs);
+	c->lst.idx_lebs    = le32_to_cpu(mst.idx_lebs);
+	c->lst.total_free  = le64_to_cpu(mst.total_free);
+	c->lst.total_dirty = le64_to_cpu(mst.total_dirty);
+	c->lst.total_used  = le64_to_cpu(mst.total_used);
+	c->lst.total_dead  = le64_to_cpu(mst.total_dead);
+	c->lst.total_dark  = le64_to_cpu(mst.total_dark);
+
+	return 0;
+}
+
+static int dump()
+{
+	int err = 0;
 
 	err = dump_super();
 	if (err)
 		goto out;
 
+	err = dump_master();
+	if (err)
+		goto out;
 out:
-	deinit();
 	return err;
 }
 
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 20/27] ubifs: ubifs_dump: dump log area
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (18 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 19/27] ubifs: ubifs_dump: dump master node Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 21/27] ubifs: introduce lprops lib Dongsheng Yang
                   ` (9 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

scan the log lebs and dump the log nodes out.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/ubifs_dump/ubifs_dump.c | 158 ++++++++++++++++++++++++++++++++++--
 1 file changed, 151 insertions(+), 7 deletions(-)

diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 73f68b7..5fb96ef 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -19,6 +19,8 @@ static const struct option longopts[] = {
 struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
 
+static void *leb_buf;
+
 /* Global nodes*/
 struct ubifs_mst_node mst;
 struct ubifs_sb_node sup;
@@ -491,15 +493,10 @@ static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node
 	struct ubifs_scan_leb *sleb;
 	struct ubifs_scan_node *snod;
 	int lnum, offs = 0, nodes_cnt;
-	static void *leb_buf;
 	int err = 0;
 	
 	lnum = UBIFS_MST_LNUM;
 
-	leb_buf = malloc(c->leb_size);
-	if (!leb_buf)
-		return -ENOMEM;
-
 	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
 	if (IS_ERR(sleb)) {
 		err = PTR_ERR(sleb);
@@ -544,7 +541,6 @@ static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node
 	err = 0;
 
 out:
-	free(leb_buf);
 	ubifs_scan_destroy(sleb);
 	return err;
 }
@@ -588,6 +584,142 @@ static int dump_master(void)
 	return 0;
 }
 
+static int __dump_log_leb(int leb_num)
+{
+	int err = 0;
+	struct ubifs_scan_leb *sleb;
+	struct ubifs_scan_node *snod;
+	const struct ubifs_cs_node *node;
+	static int cs_sqnum = 0;
+	int lnum = leb_num;
+	loff_t offs = 0;
+	int node_num = 0;
+
+	sleb = ubifs_scan(c, lnum, offs, leb_buf, 0);
+	if (IS_ERR(sleb)) {
+		printf("Error in scaning log leb");
+		return PTR_ERR(sleb);
+	}
+
+	if (sleb->nodes_cnt == 0) {
+		err = 1;
+		goto out;
+	}
+
+	node = sleb->buf;
+	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
+	if (cs_sqnum == 0) {
+		/*
+		 * This is the first log LEB we are looking at, make sure that
+		 * the first node is a commit start node. Also record its
+		 * sequence number so that UBIFS can determine where the log
+		 * ends, because all nodes which were have higher sequence
+		 * numbers.
+		 */
+		if (snod->type != UBIFS_CS_NODE) {
+			ubifs_err(c, "first log node at LEB %d:%lu is not CS node",
+				  lnum, offs);
+			goto out_dump;
+		}
+		if (le64_to_cpu(node->cmt_no) != mst.cmt_no) {
+			ubifs_err(c, "first CS node at LEB %d:%lu has wrong commit number %llu expected %llu",
+				  lnum, offs,
+				  (unsigned long long)le64_to_cpu(node->cmt_no),
+				  mst.cmt_no);
+			goto out_dump;
+		}
+
+		cs_sqnum = le64_to_cpu(node->ch.sqnum);
+	}
+
+	if (snod->sqnum < cs_sqnum) {
+		/*
+		 * This means that we reached end of log and now
+		 * look to the older log data, which was already
+		 * committed but the eraseblock was not erased (UBIFS
+		 * only un-maps it). So this basically means we have to
+		 * exit with "end of log" code.
+		 */
+		err = 1;
+		goto out;
+	}
+
+	/* Make sure the first node sits at offset zero of the LEB */
+	if (snod->offs != 0) {
+		ubifs_err(c, "first node is not at zero offset");
+		goto out_dump;
+	}
+
+	printf("\tLOG LEB %d\n", lnum);
+	list_for_each_entry(snod, &sleb->nodes, list) {
+		if (snod->sqnum < cs_sqnum) {
+			ubifs_err(c, "bad sqnum %llu, commit sqnum %d",
+				  snod->sqnum, cs_sqnum);
+			goto out_dump;
+		}
+
+		printf("\tNODE %d", node_num++);
+		switch (snod->type) {
+		case UBIFS_CS_NODE:
+			printf(" <START COMMIT>");
+		case UBIFS_REF_NODE: {
+			printf(":\n");
+			dump_node(c, snod->node);
+			break;
+		}
+		default:
+			ubifs_err(c, "unexpected node in log");
+			goto out_dump;
+		}
+	}
+	err = !sleb->endpt;
+out:
+	ubifs_scan_destroy(sleb);
+	return err;
+
+out_dump:
+	ubifs_err(c, "log error detected while replaying the log at LEB %d:%lu",
+		  lnum, offs + snod->offs);
+	ubifs_scan_destroy(sleb);
+	return -EINVAL;
+
+}
+
+static int dump_log(void)
+{
+	int err, lnum, ltail_lnum, log_last;
+
+	lnum = ltail_lnum = mst.log_lnum;
+	log_last = UBIFS_LOG_LNUM + sup.log_lebs - 1;
+	printf("LOG AREA:\n");
+	while (lnum <= log_last) {
+		err = __dump_log_leb(lnum);
+		if (err == 1) {
+			if (lnum != mst.log_lnum)
+				/* We hit the end of the log */
+				break;
+
+			/*
+			 * The head of the log must always start with the
+			 * "commit start" node on a properly formatted UBIFS.
+			 * But we found no nodes at all, which means that
+			 * someting went wrong and we cannot proceed mounting
+			 * the file-system.
+			 */
+			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
+				  lnum, 0);
+			err = -EINVAL;
+		}
+		if (err)
+			goto out;
+		lnum++;
+	}
+
+	err = 0;
+out:
+	return err;
+}
+
 static int dump()
 {
 	int err = 0;
@@ -596,9 +728,21 @@ static int dump()
 	if (err)
 		goto out;
 
+	leb_buf = malloc(c->leb_size);
+	if (!leb_buf) {
+		err = -ENOMEM;
+		goto out;
+	}
+
 	err = dump_master();
 	if (err)
-		goto out;
+		goto free;
+
+	err = dump_log();
+	if (err)
+		goto free;
+free:
+	free(leb_buf);
 out:
 	return err;
 }
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 21/27] ubifs: introduce lprops lib
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (19 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 20/27] ubifs: ubifs_dump: dump log area Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 22/27] ubifs: lpt: implement functions to scan lpt Dongsheng Yang
                   ` (8 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Copy lprops.c from kernel to ubifs-utils/.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/lprops.h |  6 ++++
 ubifs-utils/lib/lprops.c     | 79 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 ubifs-utils/include/lprops.h
 create mode 100644 ubifs-utils/lib/lprops.c

diff --git a/ubifs-utils/include/lprops.h b/ubifs-utils/include/lprops.h
new file mode 100644
index 0000000..5bf3108
--- /dev/null
+++ b/ubifs-utils/include/lprops.h
@@ -0,0 +1,6 @@
+#ifndef __UBIFS_LPROPS_H__
+#define __UBIFS_LPROPS_H__
+
+int ubifs_categorize_lprops(const struct ubifs_info *c,
+			    const struct ubifs_lprops *lprops);
+#endif
diff --git a/ubifs-utils/lib/lprops.c b/ubifs-utils/lib/lprops.c
new file mode 100644
index 0000000..818d220
--- /dev/null
+++ b/ubifs-utils/lib/lprops.c
@@ -0,0 +1,79 @@
+/*
+ * This file is part of UBIFS.
+ *
+ * Copyright (C) 2006-2008 Nokia Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published by
+ * the Free Software Foundation.
+ *
+ * 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., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Adrian Hunter
+ *          Artem Bityutskiy (Битюцкий Артём)
+ */
+
+/*
+ * This file implements the functions that access LEB properties and their
+ * categories. LEBs are categorized based on the needs of UBIFS, and the
+ * categories are stored as either heaps or lists to provide a fast way of
+ * finding a LEB in a particular category. For example, UBIFS may need to find
+ * an empty LEB for the journal, or a very dirty LEB for garbage collection.
+ */
+
+#include "ubifs_common.h"
+
+/* common.h requires the PROGRAM_NAME macro */
+#define PROGRAM_NAME "ubifs-lprops"
+#include "common.h"
+
+#include "ubifs.h"
+
+/**
+ * ubifs_categorize_lprops - categorize LEB properties.
+ * @c: UBIFS file-system description object
+ * @lprops: LEB properties to categorize
+ *
+ * LEB properties are categorized to enable fast find operations. This function
+ * returns the LEB category to which the LEB properties belong. Note however
+ * that if the LEB category is stored as a heap and the heap is full, the
+ * LEB properties may have their category changed to %LPROPS_UNCAT.
+ */
+int ubifs_categorize_lprops(const struct ubifs_info *c,
+			    const struct ubifs_lprops *lprops)
+{
+	if (lprops->flags & LPROPS_TAKEN)
+		return LPROPS_UNCAT;
+
+	if (lprops->free == c->leb_size) {
+		ubifs_assert(!(lprops->flags & LPROPS_INDEX));
+		return LPROPS_EMPTY;
+	}
+
+	if (lprops->free + lprops->dirty == c->leb_size) {
+		if (lprops->flags & LPROPS_INDEX)
+			return LPROPS_FRDI_IDX;
+		else
+			return LPROPS_FREEABLE;
+	}
+
+	if (lprops->flags & LPROPS_INDEX) {
+		if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
+			return LPROPS_DIRTY_IDX;
+	} else {
+		if (lprops->dirty >= c->dead_wm &&
+		    lprops->dirty > lprops->free)
+			return LPROPS_DIRTY;
+		if (lprops->free > 0)
+			return LPROPS_FREE;
+	}
+
+	return LPROPS_UNCAT;
+}
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 22/27] ubifs: lpt: implement functions to scan lpt
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (20 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 21/27] ubifs: introduce lprops lib Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 23/27] ubifs: ubifs_dump: dump lpt area Dongsheng Yang
                   ` (7 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Implement ubifs_scan_lpt_nolock() in lpt to scan lpt.
And then we can use this function to scan the lpt
and dump all lprops out.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/lpt.h |   3 +
 ubifs-utils/lib/lpt.c     | 661 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 663 insertions(+), 1 deletion(-)

diff --git a/ubifs-utils/include/lpt.h b/ubifs-utils/include/lpt.h
index e2f7348..d4264c3 100644
--- a/ubifs-utils/include/lpt.h
+++ b/ubifs-utils/include/lpt.h
@@ -25,5 +25,8 @@
 int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs, int *big_lpt);
 int create_lpt(struct ubifs_info *c);
 int ubifs_calc_lpt_geom(struct ubifs_info *c);
+int unpack_ltab(const struct ubifs_info *c, void *buf);
+int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
+			  ubifs_lpt_scan_callback scan_cb, void *data);
 
 #endif
diff --git a/ubifs-utils/lib/lpt.c b/ubifs-utils/lib/lpt.c
index 26fb4dd..52fbcfa 100644
--- a/ubifs-utils/lib/lpt.c
+++ b/ubifs-utils/lib/lpt.c
@@ -29,6 +29,7 @@
 #include "crc16.h"
 #include "ubifs.h"
 #include "lpt.h"
+#include "lprops.h"
 #include "io.h"
 
 /**
@@ -402,7 +403,6 @@ int create_lpt(struct ubifs_info *c)
 	len = 0;
 	/* Number of leaf nodes (pnodes) */
 	cnt = (c->main_lebs + UBIFS_LPT_FANOUT - 1) >> UBIFS_LPT_FANOUT_SHIFT;
-	//printf("pnode_cnt=%d\n",cnt);
 
 	/*
 	 * To calculate the internal node branches, we keep information about
@@ -614,3 +614,662 @@ int ubifs_calc_lpt_geom(struct ubifs_info *c)
 	}
 	return 0;
 }
+
+/**
+ * struct lpt_scan_node - somewhere to put nodes while we scan LPT.
+ * @nnode: where to keep a nnode
+ * @pnode: where to keep a pnode
+ * @cnode: where to keep a cnode
+ * @in_tree: is the node in the tree in memory
+ * @ptr.nnode: pointer to the nnode (if it is an nnode) which may be here or in
+ * the tree
+ * @ptr.pnode: ditto for pnode
+ * @ptr.cnode: ditto for cnode
+ */
+struct lpt_scan_node {
+	union {
+		struct ubifs_nnode nnode;
+		struct ubifs_pnode pnode;
+		struct ubifs_cnode cnode;
+	};
+	int in_tree;
+	union {
+		struct ubifs_nnode *nnode;
+		struct ubifs_pnode *pnode;
+		struct ubifs_cnode *cnode;
+	} ptr;
+};
+
+/**
+ * ubifs_unpack_bits - unpack bit fields.
+ * @addr: address at which to unpack (passed and next address returned)
+ * @pos: bit position at which to unpack (passed and next position returned)
+ * @nrbits: number of bits of value to unpack (1-32)
+ *
+ * This functions returns the value unpacked.
+ */
+uint32_t ubifs_unpack_bits(uint8_t **addr, int *pos, int nrbits)
+{
+	const int k = 32 - nrbits;
+	uint8_t *p = *addr;
+	int b = *pos;
+	uint32_t val = 0;
+	const int bytes = (nrbits + b + 7) >> 3;
+
+	ubifs_assert(nrbits > 0);
+	ubifs_assert(nrbits <= 32);
+	ubifs_assert(*pos >= 0);
+	ubifs_assert(*pos < 8);
+	if (b) {
+		switch (bytes) {
+		case 2:
+			val = p[1];
+			break;
+		case 3:
+			val = p[1] | ((uint32_t)p[2] << 8);
+			break;
+		case 4:
+			val = p[1] | ((uint32_t)p[2] << 8) |
+				     ((uint32_t)p[3] << 16);
+			break;
+		case 5:
+			val = p[1] | ((uint32_t)p[2] << 8) |
+				     ((uint32_t)p[3] << 16) |
+				     ((uint32_t)p[4] << 24);
+		}
+		val <<= (8 - b);
+		val |= *p >> b;
+		nrbits += b;
+	} else {
+		switch (bytes) {
+		case 1:
+			val = p[0];
+			break;
+		case 2:
+			val = p[0] | ((uint32_t)p[1] << 8);
+			break;
+		case 3:
+			val = p[0] | ((uint32_t)p[1] << 8) |
+				     ((uint32_t)p[2] << 16);
+			break;
+		case 4:
+			val = p[0] | ((uint32_t)p[1] << 8) |
+				     ((uint32_t)p[2] << 16) |
+				     ((uint32_t)p[3] << 24);
+			break;
+		}
+	}
+	val <<= k;
+	val >>= k;
+	b = nrbits & 7;
+	p += nrbits >> 3;
+	*addr = p;
+	*pos = b;
+	ubifs_assert((val >> nrbits) == 0 || nrbits - b == 32);
+	return val;
+}
+/**
+ * calc_nnode_num_from_parent - calculate nnode number.
+ * @c: UBIFS file-system description object
+ * @parent: parent nnode
+ * @iip: index in parent
+ *
+ * The nnode number is a number that uniquely identifies a nnode and can be used
+ * easily to traverse the tree from the root to that nnode.
+ *
+ * This function calculates and returns the nnode number based on the parent's
+ * nnode number and the index in parent.
+ */
+static int calc_nnode_num_from_parent(const struct ubifs_info *c,
+				      struct ubifs_nnode *parent, int iip)
+{
+	int num, shft;
+
+	if (!parent)
+		return 1;
+	shft = (c->lpt_hght - parent->level) * UBIFS_LPT_FANOUT_SHIFT;
+	num = parent->num ^ (1 << shft);
+	num |= (UBIFS_LPT_FANOUT + iip) << shft;
+	return num;
+}
+
+int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
+		       struct ubifs_nnode *nnode);
+/**
+ * scan_get_nnode - for the scan, get a nnode from either the tree or flash.
+ * @c: the UBIFS file-system description object
+ * @path: where to put the nnode
+ * @parent: parent of the nnode
+ * @iip: index in parent of the nnode
+ *
+ * This function returns a pointer to the nnode on success or a negative error
+ * code on failure.
+ */
+static struct ubifs_nnode *scan_get_nnode(struct ubifs_info *c,
+					  struct lpt_scan_node *path,
+					  struct ubifs_nnode *parent, int iip)
+{
+	struct ubifs_nbranch *branch;
+	struct ubifs_nnode *nnode;
+	int i = max_t(int, c->nnode_sz, c->pnode_sz);
+	void *buf = malloc(i);;
+	int err;
+
+	branch = &parent->nbranch[iip];
+	nnode = branch->nnode;
+	if (nnode) {
+		path->in_tree = 1;
+		path->ptr.nnode = nnode;
+		free(buf);
+		return nnode;
+	}
+	nnode = &path->nnode;
+	path->in_tree = 0;
+	path->ptr.nnode = nnode;
+	memset(nnode, 0, sizeof(struct ubifs_nnode));
+	if (branch->lnum == 0) {
+		/*
+		 * This nnode was not written which just means that the LEB
+		 * properties in the subtree below it describe empty LEBs. We
+		 * make the nnode as though we had read it, which in fact means
+		 * doing almost nothing.
+		 */
+		if (c->big_lpt)
+			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
+	} else {
+		err = ubifs_read(branch->lnum * c->leb_size + branch->offs, c->nnode_sz, buf);
+		if (err) {
+			free(buf);
+			return ERR_PTR(err);
+		}
+		err = ubifs_unpack_nnode(c, buf, nnode);
+		if (err) {
+			free(buf);
+			return ERR_PTR(err);
+		}
+	}
+	if (!c->big_lpt)
+		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
+	nnode->level = parent->level - 1;
+	nnode->parent = parent;
+	nnode->iip = iip;
+
+	free(buf);
+	return nnode;
+}
+/**
+ * calc_pnode_num_from_parent - calculate pnode number.
+ * @c: UBIFS file-system description object
+ * @parent: parent nnode
+ * @iip: index in parent
+ *
+ * The pnode number is a number that uniquely identifies a pnode and can be used
+ * easily to traverse the tree from the root to that pnode.
+ *
+ * This function calculates and returns the pnode number based on the parent's
+ * nnode number and the index in parent.
+ */
+static int calc_pnode_num_from_parent(const struct ubifs_info *c,
+				      struct ubifs_nnode *parent, int iip)
+{
+	int i, n = c->lpt_hght - 1, pnum = parent->num, num = 0;
+
+	for (i = 0; i < n; i++) {
+		num <<= UBIFS_LPT_FANOUT_SHIFT;
+		num |= pnum & (UBIFS_LPT_FANOUT - 1);
+		pnum >>= UBIFS_LPT_FANOUT_SHIFT;
+	}
+	num <<= UBIFS_LPT_FANOUT_SHIFT;
+	num |= iip;
+	return num;
+}
+/**
+ * set_pnode_lnum - set LEB numbers on a pnode.
+ * @c: UBIFS file-system description object
+ * @pnode: pnode to update
+ *
+ * This function calculates the LEB numbers for the LEB properties it contains
+ * based on the pnode number.
+ */
+static void set_pnode_lnum(const struct ubifs_info *c,
+			   struct ubifs_pnode *pnode)
+{
+	int i, lnum;
+
+	lnum = (pnode->num << UBIFS_LPT_FANOUT_SHIFT) + c->main_first;
+	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
+		if (lnum >= c->leb_cnt)
+			return;
+		pnode->lprops[i].lnum = lnum++;
+	}
+}
+/**
+ * check_lpt_crc - check LPT node crc is correct.
+ * @c: UBIFS file-system description object
+ * @buf: buffer containing node
+ * @len: length of node
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int check_lpt_crc(const struct ubifs_info *c __attribute__((unused)), void *buf, int len)
+{
+	int pos = 0;
+	uint8_t *addr = buf;
+	uint16_t crc, calc_crc;
+
+	crc = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_CRC_BITS);
+	calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
+			 len - UBIFS_LPT_CRC_BYTES);
+	if (crc != calc_crc) {
+		ubifs_err(c, "invalid crc in LPT node: crc %hx calc %hx",
+			  crc, calc_crc);
+		return -EINVAL;
+	}
+	return 0;
+}
+/**
+ * check_lpt_type - check LPT node type is correct.
+ * @c: UBIFS file-system description object
+ * @addr: address of type bit field is passed and returned updated here
+ * @pos: position of type bit field is passed and returned updated here
+ * @type: expected type
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int check_lpt_type(const struct ubifs_info *c __attribute__((unused)), uint8_t **addr,
+			  int *pos, int type)
+{
+	int node_type;
+
+	node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
+	if (node_type != type) {
+		ubifs_err(c, "invalid type (%d) in LPT node type %d",
+			  node_type, type);
+		return -EINVAL;
+	}
+	return 0;
+}
+/**
+ * unpack_pnode - unpack a pnode.
+ * @c: UBIFS file-system description object
+ * @buf: buffer containing packed pnode to unpack
+ * @pnode: pnode structure to fill
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+static int unpack_pnode(const struct ubifs_info *c, void *buf,
+			struct ubifs_pnode *pnode)
+{
+	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
+	int i, pos = 0, err;
+
+	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_PNODE);
+	if (err)
+		return err;
+	if (c->big_lpt)
+		pnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
+	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
+		struct ubifs_lprops * const lprops = &pnode->lprops[i];
+
+		lprops->free = ubifs_unpack_bits(&addr, &pos, c->space_bits);
+		lprops->free <<= 3;
+		lprops->dirty = ubifs_unpack_bits(&addr, &pos, c->space_bits);
+		lprops->dirty <<= 3;
+
+		if (ubifs_unpack_bits(&addr, &pos, 1))
+			lprops->flags = LPROPS_INDEX;
+		else
+			lprops->flags = 0;
+		lprops->flags |= ubifs_categorize_lprops(c, lprops);
+	}
+	err = check_lpt_crc(c, buf, c->pnode_sz);
+	return err;
+}
+/**
+ * ubifs_unpack_nnode - unpack a nnode.
+ * @c: UBIFS file-system description object
+ * @buf: buffer containing packed nnode to unpack
+ * @nnode: nnode structure to fill
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
+		       struct ubifs_nnode *nnode)
+{
+	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
+	int i, pos = 0, err;
+
+	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_NNODE);
+	if (err)
+		return err;
+	if (c->big_lpt)
+		nnode->num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits);
+	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
+		int lnum;
+
+		lnum = ubifs_unpack_bits(&addr, &pos, c->lpt_lnum_bits) +
+		       c->lpt_first;
+		if (lnum == c->lpt_last + 1)
+			lnum = 0;
+		nnode->nbranch[i].lnum = lnum;
+		nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
+						     c->lpt_offs_bits);
+	}
+	err = check_lpt_crc(c, buf, c->nnode_sz);
+	return err;
+}
+/**
+ * unpack_ltab - unpack the LPT's own lprops table.
+ * @c: UBIFS file-system description object
+ * @buf: buffer from which to unpack
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int unpack_ltab(const struct ubifs_info *c, void *buf)
+{
+	uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
+	int i, pos = 0, err;
+
+	err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LTAB);
+	if (err)
+		return err;
+	for (i = 0; i < c->lpt_lebs; i++) {
+		int free = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
+		int dirty = ubifs_unpack_bits(&addr, &pos, c->lpt_spc_bits);
+
+		if (free < 0 || free > c->leb_size || dirty < 0 ||
+		    dirty > c->leb_size || free + dirty > c->leb_size)
+			return -EINVAL;
+
+		c->ltab[i].free = free;
+		c->ltab[i].dirty = dirty;
+		c->ltab[i].tgc = 0;
+		c->ltab[i].cmt = 0;
+	}
+	return 0;
+}
+
+/**
+ * scan_get_pnode - for the scan, get a pnode from either the tree or flash.
+ * @c: the UBIFS file-system description object
+ * @path: where to put the pnode
+ * @parent: parent of the pnode
+ * @iip: index in parent of the pnode
+ *
+ * This function returns a pointer to the pnode on success or a negative error
+ * code on failure.
+ */
+static struct ubifs_pnode *scan_get_pnode(struct ubifs_info *c,
+					  struct lpt_scan_node *path,
+					  struct ubifs_nnode *parent, int iip)
+{
+	struct ubifs_nbranch *branch;
+	struct ubifs_pnode *pnode;
+	int i = max_t(int, c->nnode_sz, c->pnode_sz);
+	void *buf = malloc(i);;
+	int err;
+
+	branch = &parent->nbranch[iip];
+	pnode = branch->pnode;
+	if (pnode) {
+		path->in_tree = 1;
+		path->ptr.pnode = pnode;
+		free(buf);
+		return pnode;
+	}
+	pnode = &path->pnode;
+	path->in_tree = 0;
+	path->ptr.pnode = pnode;
+	memset(pnode, 0, sizeof(struct ubifs_pnode));
+	if (branch->lnum == 0) {
+		/*
+		 * This pnode was not written which just means that the LEB
+		 * properties in it describe empty LEBs. We make the pnode as
+		 * though we had read it.
+		 */
+		int i;
+
+		if (c->big_lpt)
+			pnode->num = calc_pnode_num_from_parent(c, parent, iip);
+		for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
+			struct ubifs_lprops * const lprops = &pnode->lprops[i];
+
+			lprops->free = c->leb_size;
+			lprops->flags = ubifs_categorize_lprops(c, lprops);
+		}
+	} else {
+		ubifs_assert(branch->lnum >= c->lpt_first &&
+			     branch->lnum <= c->lpt_last);
+		ubifs_assert(branch->offs >= 0 && branch->offs < c->leb_size);
+		err = ubifs_read(branch->lnum * c->leb_size + branch->offs, c->pnode_sz, buf);
+		if (err) {
+			free(buf);
+			return ERR_PTR(err);
+		}
+		err = unpack_pnode(c, buf, pnode);
+		if (err) {
+			free(buf);
+			return ERR_PTR(err);
+		}
+	}
+	if (!c->big_lpt)
+		pnode->num = calc_pnode_num_from_parent(c, parent, iip);
+	pnode->parent = parent;
+	pnode->iip = iip;
+	set_pnode_lnum(c, pnode);
+	free(buf);
+	return pnode;
+}
+/**
+ * ubifs_read_nnode - read a nnode from flash and link it to the tree in memory.
+ * @c: UBIFS file-system description object
+ * @parent: parent nnode (or NULL for the root)
+ * @iip: index in parent
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
+{
+	struct ubifs_nbranch *branch = NULL;
+	struct ubifs_nnode *nnode = NULL;
+	void *buf = c->lpt_nod_buf;
+	int err, lnum, offs;
+
+	if (parent) {
+		branch = &parent->nbranch[iip];
+		lnum = branch->lnum;
+		offs = branch->offs;
+	} else {
+		lnum = c->lpt_lnum;
+		offs = c->lpt_offs;
+	}
+	nnode = kzalloc(sizeof(struct ubifs_nnode), GFP_NOFS);
+	if (!nnode) {
+		err = -ENOMEM;
+		goto out;
+	}
+	if (lnum == 0) {
+		/*
+		 * This nnode was not written which just means that the LEB
+		 * properties in the subtree below it describe empty LEBs. We
+		 * make the nnode as though we had read it, which in fact means
+		 * doing almost nothing.
+		 */
+		if (c->big_lpt)
+			nnode->num = calc_nnode_num_from_parent(c, parent, iip);
+	} else {
+		err = ubifs_read(lnum * c->leb_size + offs, c->nnode_sz, buf);
+		if (err)
+			goto out;
+		err = ubifs_unpack_nnode(c, buf, nnode);
+		if (err)
+			goto out;
+	}
+	if (!c->big_lpt)
+		nnode->num = calc_nnode_num_from_parent(c, parent, iip);
+	if (parent) {
+		branch->nnode = nnode;
+		nnode->level = parent->level - 1;
+	} else {
+		c->nroot = nnode;
+		nnode->level = c->lpt_hght;
+	}
+	nnode->parent = parent;
+	nnode->iip = iip;
+	return 0;
+
+out:
+	ubifs_err(c, "error %d reading nnode at %d:%d", err, lnum, offs);
+	kfree(nnode);
+	return err;
+}
+
+/**
+ * ubifs_lpt_scan_nolock - scan the LPT.
+ * @c: the UBIFS file-system description object
+ * @start_lnum: LEB number from which to start scanning
+ * @end_lnum: LEB number at which to stop scanning
+ * @scan_cb: callback function called for each lprops
+ * @data: data to be passed to the callback function
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
+			  ubifs_lpt_scan_callback scan_cb, void *data)
+{
+	int err = 0, i, h, iip, shft;
+	struct ubifs_nnode *nnode;
+	struct ubifs_pnode *pnode;
+	struct lpt_scan_node *path;
+
+	if (start_lnum == -1) {
+		start_lnum = end_lnum + 1;
+		if (start_lnum >= c->leb_cnt)
+			start_lnum = c->main_first;
+	}
+
+	ubifs_assert(start_lnum >= c->main_first && start_lnum < c->leb_cnt);
+	ubifs_assert(end_lnum >= c->main_first && end_lnum < c->leb_cnt);
+
+	if (!c->nroot) {
+		err = ubifs_read_nnode(c, NULL, 0);
+		if (err)
+			return err;
+	}
+
+	path = kmalloc(sizeof(struct lpt_scan_node) * (c->lpt_hght + 1),
+		       GFP_NOFS);
+	if (!path)
+		return -ENOMEM;
+
+	path[0].ptr.nnode = c->nroot;
+	path[0].in_tree = 1;
+again:
+	/* Descend to the pnode containing start_lnum */
+	nnode = c->nroot;
+	i = start_lnum - c->main_first;
+	shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT;
+	for (h = 1; h < c->lpt_hght; h++) {
+		iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
+		shft -= UBIFS_LPT_FANOUT_SHIFT;
+		nnode = scan_get_nnode(c, path + h, nnode, iip);
+		if (IS_ERR(nnode)) {
+			err = PTR_ERR(nnode);
+			goto out;
+		}
+	}
+	iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
+	pnode = scan_get_pnode(c, path + h, nnode, iip);
+	if (IS_ERR(pnode)) {
+		err = PTR_ERR(pnode);
+		goto out;
+	}
+	iip = (i & (UBIFS_LPT_FANOUT - 1));
+
+	/* Loop for each lprops */
+	while (1) {
+		struct ubifs_lprops *lprops = &pnode->lprops[iip];
+		int ret, lnum = lprops->lnum;
+
+		ret = scan_cb(c, lprops, path[h].in_tree, data);
+		if (ret < 0) {
+			err = ret;
+			goto out;
+		}
+		if (ret & LPT_SCAN_ADD) {
+			/* Add all the nodes in path to the tree in memory */
+			for (h = 1; h < c->lpt_hght; h++) {
+				const size_t sz = sizeof(struct ubifs_nnode);
+				struct ubifs_nnode *parent;
+
+				if (path[h].in_tree)
+					continue;
+				nnode = malloc(sz);
+				if (!nnode) {
+					err = -ENOMEM;
+					goto out;
+				}
+				memcpy(nnode, &path[h].nnode, sz);
+				parent = nnode->parent;
+				parent->nbranch[nnode->iip].nnode = nnode;
+				path[h].ptr.nnode = nnode;
+				path[h].in_tree = 1;
+				path[h + 1].cnode.parent = nnode;
+			}
+		}
+		if (ret & LPT_SCAN_STOP) {
+			err = 0;
+			break;
+		}
+		/* Get the next lprops */
+		if (lnum == end_lnum) {
+			/*
+			 * We got to the end without finding what we were
+			 * looking for
+			 */
+			err = -ENOSPC;
+			goto out;
+		}
+		if (lnum + 1 >= c->leb_cnt) {
+			/* Wrap-around to the beginning */
+			start_lnum = c->main_first;
+			goto again;
+		}
+		if (iip + 1 < UBIFS_LPT_FANOUT) {
+			/* Next lprops is in the same pnode */
+			iip += 1;
+			continue;
+		}
+		/* We need to get the next pnode. Go up until we can go right */
+		iip = pnode->iip;
+		while (1) {
+			h -= 1;
+			ubifs_assert(h >= 0);
+			nnode = path[h].ptr.nnode;
+			if (iip + 1 < UBIFS_LPT_FANOUT)
+				break;
+			iip = nnode->iip;
+		}
+		/* Go right */
+		iip += 1;
+		/* Descend to the pnode */
+		h += 1;
+		for (; h < c->lpt_hght; h++) {
+			nnode = scan_get_nnode(c, path + h, nnode, iip);
+			if (IS_ERR(nnode)) {
+				err = PTR_ERR(nnode);
+				goto out;
+			}
+			iip = 0;
+		}
+		pnode = scan_get_pnode(c, path + h, nnode, iip);
+		if (IS_ERR(pnode)) {
+			err = PTR_ERR(pnode);
+			goto out;
+		}
+		iip = 0;
+	}
+out:
+	kfree(path);
+	return err;
+}
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 23/27] ubifs: ubifs_dump: dump lpt area
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (21 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 22/27] ubifs: lpt: implement functions to scan lpt Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 24/27] ubifs: ubifs_dump: dump index area Dongsheng Yang
                   ` (6 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

At first dump the ltab and then scan the whole lpt,
dumping every lprops in it.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                            |   2 +-
 ubifs-utils/ubifs_dump/ubifs_dump.c | 170 ++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 7abbef9..af17307 100644
--- a/Makefile
+++ b/Makefile
@@ -133,7 +133,7 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 $(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
 
 obj-ubifs_dump = $(UBIFS_LIBS)
-obj-ubifs_dump += ../lib/scan.o
+obj-ubifs_dump += ../lib/scan.o ../lib/lprops.o
 LDFLAGS_ubifs_dump = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_ubifs_dump = -lz -llzo2 -lm -luuid
 $(call mkdep,ubifs-utils/ubifs_dump/,ubifs_dump,,ubi-utils/libubi.a)
diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 5fb96ef..0d375f0 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -720,6 +720,172 @@ out:
 	return err;
 }
 
+void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)
+{
+	int spc, dark = 0, dead = 0;
+
+	if ((lp->flags & LPROPS_CAT_MASK) == LPROPS_EMPTY)
+		return;
+
+	spc = lp->free + lp->dirty;
+	dead = spc;
+
+	if (lp->flags & LPROPS_INDEX)
+		printf("\tLEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (",
+		       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
+		       lp->flags);
+	else
+		printf("\tLEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (",
+		       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc,
+		       dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);
+
+	if (lp->flags & LPROPS_TAKEN) {
+		if (lp->flags & LPROPS_INDEX)
+			printf("index, taken");
+		else
+			printf("taken");
+	} else {
+		const char *s;
+
+		if (lp->flags & LPROPS_INDEX) {
+			switch (lp->flags & LPROPS_CAT_MASK) {
+			case LPROPS_DIRTY_IDX:
+				s = "dirty index";
+				break;
+			case LPROPS_FRDI_IDX:
+				s = "freeable index";
+				break;
+			default:
+				s = "index";
+			}
+		} else {
+			switch (lp->flags & LPROPS_CAT_MASK) {
+			case LPROPS_UNCAT:
+				s = "not categorized";
+				break;
+			case LPROPS_DIRTY:
+				s = "dirty";
+				break;
+			case LPROPS_FREE:
+				s = "free";
+				break;
+			case LPROPS_EMPTY:
+				s = "empty";
+				break;
+			case LPROPS_FREEABLE:
+				s = "freeable";
+				break;
+			default:
+				s = NULL;
+				break;
+			}
+		}
+		printf("%s", s);
+	}
+
+	if (lp->lnum == c->gc_lnum)
+		printf(", GC LEB");
+	printf(")\n");
+}
+
+void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)
+{
+	printf("\tempty_lebs %d, idx_lebs  %d\n",
+	       lst->empty_lebs, lst->idx_lebs);
+	printf("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n",
+	       lst->taken_empty_lebs, lst->total_free, lst->total_dirty);
+	printf("\ttotal_used %lld, total_dark %lld, total_dead %lld\n",
+	       lst->total_used, lst->total_dark, lst->total_dead);
+	printf("\n");
+}
+
+static int scan_dump_cb(struct ubifs_info *c,
+			 const struct ubifs_lprops *lp, int in_tree __attribute__((unused)),
+			 struct ubifs_lp_stats *lst __attribute__((unused)))
+{
+	ubifs_dump_lprop(c, lp);
+	return 0;
+}
+
+static int ubifs_dump_lprops(struct ubifs_info *c)
+{
+	printf("\tLPROPS statistics: \n");
+	ubifs_dump_lstats(&c->lst);
+	printf("\tLPROPS TREE: \n");
+	c->lpt_nod_buf = malloc(min(c->nnode_sz, c->pnode_sz));
+	ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
+			  (ubifs_lpt_scan_callback)scan_dump_cb, NULL);
+	printf("\n");
+
+	return 0;
+}
+
+/**
+ * read_ltab - read LPT's own lprops table.
+ * @c: UBIFS file-system description object
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+static int read_ltab(struct ubifs_info *c)
+{
+	int err;
+	void *buf;
+
+	buf = vmalloc(c->ltab_sz);
+	if (!buf)
+		return -ENOMEM;
+	err = ubifs_read(c->ltab_lnum * c->leb_size + c->ltab_offs, c->ltab_sz, buf);
+	if (err)
+		goto out;
+	err = unpack_ltab(c, buf);
+out:
+	vfree(buf);
+	return err;
+}
+
+static int ubifs_dump_lpt_info(struct ubifs_info *c)
+{
+	int i;
+	int ret = 0;
+
+	c->ltab = malloc(c->lpt_lebs * sizeof(struct ubifs_lprops));
+	if (!c->ltab)
+		return err_msg("unable to allocate LPT ltab");
+
+	ret = read_ltab(c);
+	if (ret)
+		return ret;
+	printf("\tLPT INFO: \n");
+	printf("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs);
+	printf("\tLPT head is at %d:%d\n",
+	       c->nhead_lnum, c->nhead_offs);
+	printf("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);
+	if (c->big_lpt)
+		printf("\tLPT lsave is at %d:%d\n",
+		       c->lsave_lnum, c->lsave_offs);
+	for (i = 0; i < c->lpt_lebs; i++)
+		printf("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n",
+		       i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty,
+		       c->ltab[i].tgc, c->ltab[i].cmt);
+	printf("\n");
+	return 0;
+}
+
+static int dump_lpt(void)
+{
+	int ret = 0;
+
+	printf("LPT AREA: \n");
+	ret = ubifs_dump_lpt_info(c);
+	if (ret)
+		return ret;
+
+	ret = ubifs_dump_lprops(c);
+	if (ret)
+		return ret;
+	return 0;
+}
+
 static int dump()
 {
 	int err = 0;
@@ -741,6 +907,10 @@ static int dump()
 	err = dump_log();
 	if (err)
 		goto free;
+
+	err = dump_lpt();
+	if (err)
+		goto free;
 free:
 	free(leb_buf);
 out:
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 24/27] ubifs: ubifs_dump: dump index area
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (22 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 23/27] ubifs: ubifs_dump: dump lpt area Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-09-24  6:25   ` Richard Weinberger
  2015-08-19  8:39 ` [PATCH 25/27] ubifs: defs.h: introduce some compatible definitions about integer such as __u16 Dongsheng Yang
                   ` (5 subsequent siblings)
  29 siblings, 1 reply; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Scan the whole index tree and dump each index node.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/ubifs_dump/ubifs_dump.c | 95 +++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 0d375f0..90ac322 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -886,6 +886,97 @@ static int dump_lpt(void)
 	return 0;
 }
 
+/**
+ * ubifs_read_node - read node.
+ * @c: UBIFS file-system description object
+ * @buf: buffer to read to
+ * @type: node type
+ * @len: node length (not aligned)
+ * @lnum: logical eraseblock number
+ * @offs: offset within the logical eraseblock
+ *
+ * This function reads a node of known type and and length, checks it and
+ * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
+ * and a negative error code in case of failure.
+ */
+int ubifs_read_node(const struct ubifs_info *c, void *buf, int len,
+		    int lnum, loff_t offs)
+{
+	int err;
+
+	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
+	ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
+	ubifs_assert(!(offs & 7) && offs < c->leb_size);
+	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
+
+	err = ubifs_read((loff_t)lnum * c->leb_size + offs, len, buf);
+	if (err && err != -EBADMSG)
+		return err;
+
+	return 0;
+}
+
+static int dump_zbr(struct ubifs_info *c, struct ubifs_zbranch *zbr)
+{
+	int i = 0;
+	int err = 0;
+	struct ubifs_idx_node *idx;
+	void *buf;
+
+	buf = malloc(zbr->len);
+	if (!buf)
+		return -ENOMEM;
+
+	err = ubifs_read_node(c, buf, zbr->len, zbr->lnum, zbr->offs);
+	if (err)
+		goto out;
+
+	dump_node(c, buf);
+
+	if (le32_to_cpu(((struct ubifs_ch *)buf)->node_type) != UBIFS_IDX_NODE)
+		goto out;
+
+	idx = ((struct ubifs_idx_node *)buf);
+	for (i = 0; i < le32_to_cpu(idx->child_cnt); i++) {
+		struct ubifs_branch *br;
+		struct ubifs_zbranch child;
+
+		br = ubifs_idx_branch(c, idx, i);
+		child.lnum = le32_to_cpu(br->lnum);
+		child.offs = le32_to_cpu(br->offs);
+		child.len = le32_to_cpu(br->len);
+		err = dump_zbr(c, &child);
+		if (err)
+			goto out;
+	}
+
+out:
+	free(buf);
+	return err;
+}
+
+static int dump_index()
+{
+	int err = 0;
+
+	printf("\tINDEX AREA: \n");
+	err = dump_zbr(c, &c->zroot);
+	if (err)
+		return err;
+	return 0;
+}
+
+static int dump_main()
+{
+	int err = 0;
+	printf("MAIN AREA: \n");
+	err = dump_index();
+	if (err)
+		return err;
+
+	return 0;
+}
+
 static int dump()
 {
 	int err = 0;
@@ -911,6 +1002,10 @@ static int dump()
 	err = dump_lpt();
 	if (err)
 		goto free;
+
+	err = dump_main();
+	if (err)
+		goto out;
 free:
 	free(leb_buf);
 out:
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 25/27] ubifs: defs.h: introduce some compatible definitions about integer such as __u16
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (23 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 24/27] ubifs: ubifs_dump: dump index area Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 26/27] ubifs: introduce hexdump lib Dongsheng Yang
                   ` (4 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Add __uXX class compatible definitions in defs.h

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/defs.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/ubifs-utils/include/defs.h b/ubifs-utils/include/defs.h
index ed42ab1..91ef175 100644
--- a/ubifs-utils/include/defs.h
+++ b/ubifs-utils/include/defs.h
@@ -174,6 +174,36 @@ static inline long IS_ERR(const void *ptr)
 #define ULONG_MAX       (~0UL)
 #endif
 
+#ifndef __CHECKER__
+/*
+ * Since we're using primitive definitions from kernel-space, we need to
+ * define __KERNEL__ so that system header files know which definitions
+ * to use.
+ */
+#define __KERNEL__
+#include <asm/types.h>
+typedef __u32 u32;
+typedef __u64 u64;
+typedef __u16 u16;
+typedef __u8 u8;
+typedef __s64 s64;
+typedef __s32 s32;
+
+/*
+ * Continuing to define __KERNEL__ breaks others parts of the code, so
+ * we can just undefine it now that we have the correct headers...
+ */
+#undef __KERNEL__
+#else
+typedef unsigned int u32;
+typedef unsigned int __u32;
+typedef unsigned long long u64;
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef long long s64;
+typedef int s32;
+#endif
+
 #if INT_MAX != 0x7fffffff
 #error : sizeof(int) must be 4 for this program
 #endif
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 26/27] ubifs: introduce hexdump lib
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (24 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 25/27] ubifs: defs.h: introduce some compatible definitions about integer such as __u16 Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:39 ` [PATCH 27/27] ubifs: ubifs_dump: dump data in hex format Dongsheng Yang
                   ` (3 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

Introduce a hexdump lib to allow us to dump data in hex format

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 ubifs-utils/include/hexdump.h |  21 +++++
 ubifs-utils/lib/hexdump.c     | 200 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 221 insertions(+)
 create mode 100644 ubifs-utils/include/hexdump.h
 create mode 100644 ubifs-utils/lib/hexdump.c

diff --git a/ubifs-utils/include/hexdump.h b/ubifs-utils/include/hexdump.h
new file mode 100644
index 0000000..f5f2800
--- /dev/null
+++ b/ubifs-utils/include/hexdump.h
@@ -0,0 +1,21 @@
+#ifndef __UBIFS_HEXDUMP_H__
+#define __UBIFS_HEXDUMP_H__
+
+#include "ubifs_common.h"
+
+static const char hex_asc[] = "0123456789abcdef";
+static const char hex_asc_upper[] = "0123456789ABCDEF";
+
+#define hex_asc_lo(x)	hex_asc[((x) & 0x0f)]
+#define hex_asc_hi(x)	hex_asc[((x) & 0xf0) >> 4]
+
+enum {
+	DUMP_PREFIX_NONE,
+	DUMP_PREFIX_ADDRESS,
+	DUMP_PREFIX_OFFSET
+};
+
+void print_hex_dump(const char *prefix_str, int prefix_type,
+		    int rowsize, int groupsize,
+		    const void *buf, size_t len, int ascii);
+#endif
diff --git a/ubifs-utils/lib/hexdump.c b/ubifs-utils/lib/hexdump.c
new file mode 100644
index 0000000..c58f7cc
--- /dev/null
+++ b/ubifs-utils/lib/hexdump.c
@@ -0,0 +1,200 @@
+/*
+ * lib/hexdump.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation. See README and COPYING for
+ * more details.
+ */
+#include "hexdump.h"
+#define PROGRAM_NAME "hexdump"
+#include "common.h"
+/**
+ * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
+ * @buf: data blob to dump
+ * @len: number of bytes in the @buf
+ * @rowsize: number of bytes to print per line; must be 16 or 32
+ * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
+ * @linebuf: where to put the converted data
+ * @linebuflen: total size of @linebuf, including space for terminating NUL
+ * @ascii: include ASCII after the hex output
+ *
+ * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
+ * 16 or 32 bytes of input data converted to hex + ASCII output.
+ *
+ * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
+ * to a hex + ASCII dump at the supplied memory location.
+ * The converted output is always NUL-terminated.
+ *
+ * E.g.:
+ *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
+ *			linebuf, sizeof(linebuf), true);
+ *
+ * example output buffer:
+ * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
+ *
+ * Return:
+ * The amount of bytes placed in the buffer without terminating NUL. If the
+ * output was truncated, then the return value is the number of bytes
+ * (excluding the terminating NUL) which would have been written to the final
+ * string if enough space had been available.
+ */
+int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
+		       char *linebuf, size_t linebuflen, int ascii)
+{
+	const u8 *ptr = buf;
+	int ngroups;
+	u8 ch;
+	int j, lx = 0;
+	int ascii_column;
+	int ret;
+
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
+
+	if (len > rowsize)		/* limit to one line at a time */
+		len = rowsize;
+	if (!is_power_of_2(groupsize) || groupsize > 8)
+		groupsize = 1;
+	if ((len % groupsize) != 0)	/* no mixed size output */
+		groupsize = 1;
+
+	ngroups = len / groupsize;
+	ascii_column = rowsize * 2 + rowsize / groupsize + 1;
+
+	if (!linebuflen)
+		goto overflow1;
+
+	if (!len)
+		goto nil;
+
+	if (groupsize == 8) {
+		const u64 *ptr8 = buf;
+
+		for (j = 0; j < ngroups; j++) {
+			ret = snprintf(linebuf + lx, linebuflen - lx,
+				       "%s%16.16llx", j ? " " : "",
+				       (unsigned long long)*(ptr8 + j));
+			if (ret >= linebuflen - lx)
+				goto overflow1;
+			lx += ret;
+		}
+	} else if (groupsize == 4) {
+		const u32 *ptr4 = buf;
+
+		for (j = 0; j < ngroups; j++) {
+			ret = snprintf(linebuf + lx, linebuflen - lx,
+				       "%s%8.8x", j ? " " : "",
+				       *(ptr4 + j));
+			if (ret >= linebuflen - lx)
+				goto overflow1;
+			lx += ret;
+		}
+	} else if (groupsize == 2) {
+		const u16 *ptr2 = buf;
+
+		for (j = 0; j < ngroups; j++) {
+			ret = snprintf(linebuf + lx, linebuflen - lx,
+				       "%s%4.4x", j ? " " : "",
+				       *(ptr2 + j));
+			if (ret >= linebuflen - lx)
+				goto overflow1;
+			lx += ret;
+		}
+	} else {
+		for (j = 0; j < len; j++) {
+			if (linebuflen < lx + 3)
+				goto overflow2;
+			ch = ptr[j];
+			linebuf[lx++] = hex_asc_hi(ch);
+			linebuf[lx++] = hex_asc_lo(ch);
+			linebuf[lx++] = ' ';
+		}
+		if (j)
+			lx--;
+	}
+	if (!ascii)
+		goto nil;
+
+	while (lx < ascii_column) {
+		if (linebuflen < lx + 2)
+			goto overflow2;
+		linebuf[lx++] = ' ';
+	}
+	for (j = 0; j < len; j++) {
+		if (linebuflen < lx + 2)
+			goto overflow2;
+		ch = ptr[j];
+		linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
+	}
+nil:
+	linebuf[lx] = '\0';
+	return lx;
+overflow2:
+	linebuf[lx++] = '\0';
+overflow1:
+	return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
+}
+
+/**
+ * print_hex_dump - print a text hex dump to syslog for a binary blob of data
+ * @prefix_str: string to prefix each line with;
+ *  caller supplies trailing spaces for alignment if desired
+ * @prefix_type: controls whether prefix of an offset, address, or none
+ *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
+ * @rowsize: number of bytes to print per line; must be 16 or 32
+ * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
+ * @buf: data blob to dump
+ * @len: number of bytes in the @buf
+ * @ascii: include ASCII after the hex output
+ *
+ * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
+ * to the kernel log at the specified kernel log level, with an optional
+ * leading prefix.
+ *
+ * print_hex_dump() works on one "line" of output at a time, i.e.,
+ * 16 or 32 bytes of input data converted to hex + ASCII output.
+ * print_hex_dump() iterates over the entire input @buf, breaking it into
+ * "line size" chunks to format and print.
+ *
+ * E.g.:
+ *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
+ *		    16, 1, frame->data, frame->len, true);
+ *
+ * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
+ * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
+ * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
+ * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
+ */
+void print_hex_dump(const char *prefix_str, int prefix_type,
+		    int rowsize, int groupsize,
+		    const void *buf, size_t len, int ascii)
+{
+	const u8 *ptr = buf;
+	int i, linelen, remaining = len;
+	char linebuf[32 * 3 + 2 + 32 + 1];
+
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
+
+	for (i = 0; i < len; i += rowsize) {
+		linelen = min(remaining, rowsize);
+		remaining -= rowsize;
+
+		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+				   linebuf, sizeof(linebuf), ascii);
+
+		switch (prefix_type) {
+		case DUMP_PREFIX_ADDRESS:
+			printf("%s%p: %s\n",
+			       prefix_str, ptr + i, linebuf);
+			break;
+		case DUMP_PREFIX_OFFSET:
+			printf("%s%.8x: %s\n", prefix_str, i, linebuf);
+			break;
+		default:
+			printf("%s%s\n", prefix_str, linebuf);
+			break;
+		}
+	}
+}
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* [PATCH 27/27] ubifs: ubifs_dump: dump data in hex format
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (25 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 26/27] ubifs: introduce hexdump lib Dongsheng Yang
@ 2015-08-19  8:39 ` Dongsheng Yang
  2015-08-19  8:48 ` [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (2 subsequent siblings)
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:39 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

When we dump data node, we can dump the data in it
in hex format.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
 Makefile                            |  2 +-
 ubifs-utils/ubifs_dump/ubifs_dump.c | 42 ++++++++++++++++++++-----------------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/Makefile b/Makefile
index af17307..e3b670d 100644
--- a/Makefile
+++ b/Makefile
@@ -133,7 +133,7 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 $(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
 
 obj-ubifs_dump = $(UBIFS_LIBS)
-obj-ubifs_dump += ../lib/scan.o ../lib/lprops.o
+obj-ubifs_dump += ../lib/scan.o ../lib/lprops.o ../lib/hexdump.o
 LDFLAGS_ubifs_dump = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_ubifs_dump = -lz -llzo2 -lm -luuid
 $(call mkdep,ubifs-utils/ubifs_dump/,ubifs_dump,,ubi-utils/libubi.a)
diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 90ac322..eaf8daf 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -7,6 +7,7 @@
 #include "key.h"
 #include "lpt.h"
 #include "scan.h"
+#include "hexdump.h"
 
 #define DBG_KEY_BUF_LEN		48
 
@@ -252,26 +253,26 @@ void dump_node(const struct ubifs_info *c, const void *node)
 		const struct ubifs_ino_node *ino = node;
 
 		key_read(c, &ino->key, &key);
-		printf("\t\tkey \t\t\t%s\n",
+		printf("\t\tkey \t\t\t\t%s\n",
 		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
 		printf("\t\tcreat_sqnum \t\t\t%llu\n",
 		       (unsigned long long)le64_to_cpu(ino->creat_sqnum));
-		printf("\t\tsize \t\t\t%llu\n",
+		printf("\t\tsize \t\t\t\t%llu\n",
 		       (unsigned long long)le64_to_cpu(ino->size));
-		printf("\t\tnlink \t\t\t%u\n", le32_to_cpu(ino->nlink));
-		printf("\t\tatime \t\t\t%lld.%u\n",
+		printf("\t\tnlink \t\t\t\t%u\n", le32_to_cpu(ino->nlink));
+		printf("\t\tatime \t\t\t\t%lld.%u\n",
 		       (long long)le64_to_cpu(ino->atime_sec),
 		       le32_to_cpu(ino->atime_nsec));
-		printf("\t\tmtime \t\t\t%lld.%u\n",
+		printf("\t\tmtime \t\t\t\t%lld.%u\n",
 		       (long long)le64_to_cpu(ino->mtime_sec),
 		       le32_to_cpu(ino->mtime_nsec));
-		printf("\t\tctime \t\t\t%lld.%u\n",
+		printf("\t\tctime \t\t\t\t%lld.%u\n",
 		       (long long)le64_to_cpu(ino->ctime_sec),
 		       le32_to_cpu(ino->ctime_nsec));
-		printf("\t\tuid \t\t\t%u\n", le32_to_cpu(ino->uid));
-		printf("\t\tgid \t\t\t%u\n", le32_to_cpu(ino->gid));
-		printf("\t\tmode \t\t\t%u\n", le32_to_cpu(ino->mode));
-		printf("\t\tflags \t\t\t%#x\n", le32_to_cpu(ino->flags));
+		printf("\t\tuid \t\t\t\t%u\n", le32_to_cpu(ino->uid));
+		printf("\t\tgid \t\t\t\t%u\n", le32_to_cpu(ino->gid));
+		printf("\t\tmode \t\t\t\t%u\n", le32_to_cpu(ino->mode));
+		printf("\t\tflags \t\t\t\t%#x\n", le32_to_cpu(ino->flags));
 		printf("\t\txattr_cnt \t\t\t%u\n", le32_to_cpu(ino->xattr_cnt));
 		printf("\t\txattr_size \t\t\t%u\n", le32_to_cpu(ino->xattr_size));
 		printf("\t\txattr_names \t\t\t%u\n", le32_to_cpu(ino->xattr_names));
@@ -287,13 +288,13 @@ void dump_node(const struct ubifs_info *c, const void *node)
 		int nlen = le16_to_cpu(dent->nlen);
 
 		key_read(c, &dent->key, &key);
-		printf("\t\tkey \t\t\t%s\n",
+		printf("\t\tkey \t\t\t\t%s\n",
 		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
-		printf("\t\tinum \t\t\t%llu\n",
+		printf("\t\tinum \t\t\t\t%llu\n",
 		       (unsigned long long)le64_to_cpu(dent->inum));
-		printf("\t\ttype \t\t\t%d\n", (int)dent->type);
-		printf("\t\tnlen \t\t\t%d\n", nlen);
-		printf("\t\tname           ");
+		printf("\t\ttype \t\t\t\t%d\n", (int)dent->type);
+		printf("\t\tnlen \t\t\t\t%d\n", nlen);
+		printf("\t\tname \t\t\t\t");
 
 		if (nlen > UBIFS_MAX_NLEN)
 			printf("(bad name length, not printing, bad or corrupted node)");
@@ -311,12 +312,15 @@ void dump_node(const struct ubifs_info *c, const void *node)
 		int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;
 
 		key_read(c, &dn->key, &key);
-		printf("\t\tkey \t\t\t%s\n",
+		printf("\t\tkey \t\t\t\t%s\n",
 		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));
-		printf("\t\tsize \t\t\t%u\n", le32_to_cpu(dn->size));
+		printf("\t\tsize \t\t\t\t%u\n", le32_to_cpu(dn->size));
 		printf("\t\tcompr_typ \t\t\t%d\n",
 		       (int)le16_to_cpu(dn->compr_type));
 		printf("\t\tdata size \t\t\t%d\n", dlen);
+		printf("\t\tdata:\n");
+		print_hex_dump("\t\t", DUMP_PREFIX_OFFSET, 32, 1,
+			       (void *)&dn->data, dlen, 0);
 		break;
 	}
 	case UBIFS_TRUN_NODE:
@@ -335,8 +339,8 @@ void dump_node(const struct ubifs_info *c, const void *node)
 		const struct ubifs_idx_node *idx = node;
 
 		n = le16_to_cpu(idx->child_cnt);
-		printf("\t\tchild_cnt \t\t%d\n", n);
-		printf("\t\tlevel \t\t\t%d\n", (int)le16_to_cpu(idx->level));
+		printf("\t\tchild_cnt \t\t\t%d\n", n);
+		printf("\t\tlevel \t\t\t\t%d\n", (int)le16_to_cpu(idx->level));
 		printf("\t\tBranches:\n");
 
 		for (i = 0; i < n && i < c->fanout - 1; i++) {
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (26 preceding siblings ...)
  2015-08-19  8:39 ` [PATCH 27/27] ubifs: ubifs_dump: dump data in hex format Dongsheng Yang
@ 2015-08-19  8:48 ` Dongsheng Yang
  2015-08-19 20:45 ` Richard Weinberger
  2015-09-16 13:32 ` Richard Weinberger
  29 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-19  8:48 UTC (permalink / raw)
  To: dedekind1, richard, computersforpeace; +Cc: linux-mtd, Dongsheng Yang

[-- Attachment #1: Type: text/plain, Size: 11921 bytes --]

On 08/19/2015 04:39 PM, Dongsheng Yang wrote:
> Hi Atem, Richard and Brian,
[...]
> Please help to review or test it.
> Thanx
>
> Dongsheng Yang (27):
>    mtd-utils: Restructure the mtd-utils source.
>    ubifs: pick some common definitions into ubifs_common.h
>    ubifs: move the all io related code into io.[h|c]
>    ubifs: remove the including of mkfs.ubifs.h in lpt.c
>    ubifs: cut off the dependence from compr.o to mkfs.ubifs
>    ubifs: cut off the dependence from devtable to mkfs.ubifs.h
>    ubifs: introduce ubifs-utils/include and ubifs-utils/lib
>    ubifs: move more functions into io lib
>    ubifs: introduce a new tool ubifs_dump
>    ubifs: introduce list.h
>    ubifs: copy some important data in ubifs.h from kernel to ubifs-utils
>    ubifs: copy some important functions in key.h from kernel to
>      ubifs-utils
>    ubifs: ubifs_dump: add dump_ch and dump_node functions
>    ubifs: defs.h: introduce some compatible definition for printk class
>    ubifs: io: introduce ubifs_read function to read ubi volume
>    ubifs: ubifs_dump: dump super block
>    ubifs: introduce scan for ubifs-utils
>    ubifs: add some more compatible definitions in defs.h
>    ubifs: ubifs_dump: dump master node
>    ubifs: ubifs_dump: dump log area
>    ubifs: introduce lprops lib
>    ubifs: lpt: implement functions to scan lpt
>    ubifs: ubifs_dump: dump lpt area
>    ubifs: ubifs_dump: dump index area
>    ubifs: defs.h: introduce some compatible definitions about integer
>      such as __u16
>    ubifs: introduce hexdump lib
>    ubifs: ubifs_dump: dump data in hex format

All of these patches are for mtd-utils repo.

Really sorry for the prefix, I should have added a mtd-utils in
each subject.

About the result of ubifs_dump, I attached one of it to this mail.

The ubifs is created by following commands:
[root@atest-guest mtd-utils]# mkfs.ubifs -y /dev/ubi0_0
[root@atest-guest mtd-utils]# mount /dev/ubi0_0 /mnt/ubifs
[root@atest-guest mtd-utils]# echo "UBIFS" > /mnt/ubifs/data
[root@atest-guest mtd-utils]# umount /mnt/ubifs
[root@atest-guest mtd-utils]# ubifs_dump /dev/ubi0_0 > /tmp/ubifs_dump

And you can find the
"		name 				data"
in ubifs_dump which stands for the file name in dentry node.

Also you can find:
"		data:"
"		00000000: 55 42 49 46 53 0a"
in ubifs_dump which stands for the "UBIFS" in data node.

Yang
>
>   Makefile                                           |   76 +-
>   flash_erase.c => flash-utils/flash_erase.c         |    0
>   flash_eraseall => flash-utils/flash_eraseall       |    0
>   flash_lock.c => flash-utils/flash_lock.c           |    0
>   flash_otp_dump.c => flash-utils/flash_otp_dump.c   |    0
>   flash_otp_info.c => flash-utils/flash_otp_info.c   |    0
>   flash_otp_lock.c => flash-utils/flash_otp_lock.c   |    0
>   flash_otp_write.c => flash-utils/flash_otp_write.c |    0
>   flash_unlock.c => flash-utils/flash_unlock.c       |    0
>   flashcp.c => flash-utils/flashcp.c                 |    0
>   compr.c => jffsX-utils/compr.c                     |    0
>   compr.h => jffsX-utils/compr.h                     |    0
>   compr_lzo.c => jffsX-utils/compr_lzo.c             |    0
>   compr_rtime.c => jffsX-utils/compr_rtime.c         |    0
>   compr_zlib.c => jffsX-utils/compr_zlib.c           |    0
>   device_table.txt => jffsX-utils/device_table.txt   |    0
>   jffs-dump.c => jffsX-utils/jffs-dump.c             |    0
>   jffs2dump.c => jffsX-utils/jffs2dump.c             |    0
>   jffs2reader.c => jffsX-utils/jffs2reader.c         |    0
>   mkfs.jffs2.1 => jffsX-utils/mkfs.jffs2.1           |    0
>   mkfs.jffs2.c => jffsX-utils/mkfs.jffs2.c           |    0
>   rbtree.c => jffsX-utils/rbtree.c                   |    0
>   rbtree.h => jffsX-utils/rbtree.h                   |    0
>   summary.h => jffsX-utils/summary.h                 |    0
>   sumtool.c => jffsX-utils/sumtool.c                 |    0
>   MAKEDEV => misc-utils/MAKEDEV                      |    0
>   doc_loadbios.c => misc-utils/doc_loadbios.c        |    0
>   docfdisk.c => misc-utils/docfdisk.c                |    0
>   fectest.c => misc-utils/fectest.c                  |    0
>   ftl_check.c => misc-utils/ftl_check.c              |    0
>   ftl_format.c => misc-utils/ftl_format.c            |    0
>   mcast_image.h => misc-utils/mcast_image.h          |    0
>   mtd_debug.c => misc-utils/mtd_debug.c              |    0
>   mtdpart.c => misc-utils/mtdpart.c                  |    0
>   recv_image.c => misc-utils/recv_image.c            |    0
>   serve_image.c => misc-utils/serve_image.c          |    0
>   mkfs.ubifs/defs.h                                  |   92 --
>   mkfs.ubifs/lpt.c                                   |  578 ---------
>   mkfs.ubifs/mkfs.ubifs.h                            |  150 ---
>   load_nandsim.sh => nand-utils/load_nandsim.sh      |    0
>   nanddump.c => nand-utils/nanddump.c                |    0
>   nandtest.c => nand-utils/nandtest.c                |    0
>   nandwrite.c => nand-utils/nandwrite.c              |    0
>   nftl_format.c => nand-utils/nftl_format.c          |    0
>   nftldump.c => nand-utils/nftldump.c                |    0
>   rfddump.c => nor-utils/rfddump.c                   |    0
>   rfdformat.c => nor-utils/rfdformat.c               |    0
>   {mkfs.ubifs => ubifs-utils}/COPYING                |    0
>   {mkfs.ubifs => ubifs-utils}/README                 |    0
>   {mkfs.ubifs => ubifs-utils/include}/compr.h        |    3 +-
>   {mkfs.ubifs => ubifs-utils/include}/crc16.h        |    0
>   ubifs-utils/include/defs.h                         |  215 ++++
>   ubifs-utils/include/devtable.h                     |   55 +
>   .../hashtable => ubifs-utils/include}/hashtable.h  |    0
>   .../include}/hashtable_itr.h                       |    0
>   .../include}/hashtable_private.h                   |    0
>   ubifs-utils/include/hexdump.h                      |   21 +
>   ubifs-utils/include/io.h                           |   21 +
>   {mkfs.ubifs => ubifs-utils/include}/key.h          |   76 +-
>   ubifs-utils/include/list.h                         |  484 ++++++++
>   ubifs-utils/include/lprops.h                       |    6 +
>   {mkfs.ubifs => ubifs-utils/include}/lpt.h          |    4 +
>   ubifs-utils/include/scan.h                         |    8 +
>   {mkfs.ubifs => ubifs-utils/include}/ubifs.h        |   96 +-
>   ubifs-utils/include/ubifs_common.h                 |   50 +
>   {mkfs.ubifs => ubifs-utils/lib}/compr.c            |   20 +-
>   {mkfs.ubifs => ubifs-utils/lib}/crc16.c            |    0
>   {mkfs.ubifs => ubifs-utils/lib}/devtable.c         |    7 +-
>   .../hashtable => ubifs-utils/lib}/hashtable.c      |    0
>   .../hashtable => ubifs-utils/lib}/hashtable_itr.c  |    0
>   ubifs-utils/lib/hexdump.c                          |  200 +++
>   ubifs-utils/lib/io.c                               |  152 +++
>   ubifs-utils/lib/lprops.c                           |   79 ++
>   ubifs-utils/lib/lpt.c                              | 1275 ++++++++++++++++++++
>   ubifs-utils/lib/scan.c                             |  318 +++++
>   {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/.gitignore  |    0
>   .../mkfs.ubifs}/mkfs.ubifs.c                       |  144 +--
>   ubifs-utils/mkfs.ubifs/mkfs.ubifs.h                |   54 +
>   ubifs-utils/ubifs_dump/ubifs_dump.c                | 1041 ++++++++++++++++
>   79 files changed, 4220 insertions(+), 1005 deletions(-)
>   rename flash_erase.c => flash-utils/flash_erase.c (100%)
>   rename flash_eraseall => flash-utils/flash_eraseall (100%)
>   rename flash_lock.c => flash-utils/flash_lock.c (100%)
>   rename flash_otp_dump.c => flash-utils/flash_otp_dump.c (100%)
>   rename flash_otp_info.c => flash-utils/flash_otp_info.c (100%)
>   rename flash_otp_lock.c => flash-utils/flash_otp_lock.c (100%)
>   rename flash_otp_write.c => flash-utils/flash_otp_write.c (100%)
>   rename flash_unlock.c => flash-utils/flash_unlock.c (100%)
>   rename flashcp.c => flash-utils/flashcp.c (100%)
>   rename compr.c => jffsX-utils/compr.c (100%)
>   rename compr.h => jffsX-utils/compr.h (100%)
>   rename compr_lzo.c => jffsX-utils/compr_lzo.c (100%)
>   rename compr_rtime.c => jffsX-utils/compr_rtime.c (100%)
>   rename compr_zlib.c => jffsX-utils/compr_zlib.c (100%)
>   rename device_table.txt => jffsX-utils/device_table.txt (100%)
>   rename jffs-dump.c => jffsX-utils/jffs-dump.c (100%)
>   rename jffs2dump.c => jffsX-utils/jffs2dump.c (100%)
>   rename jffs2reader.c => jffsX-utils/jffs2reader.c (100%)
>   rename mkfs.jffs2.1 => jffsX-utils/mkfs.jffs2.1 (100%)
>   rename mkfs.jffs2.c => jffsX-utils/mkfs.jffs2.c (100%)
>   rename rbtree.c => jffsX-utils/rbtree.c (100%)
>   rename rbtree.h => jffsX-utils/rbtree.h (100%)
>   rename summary.h => jffsX-utils/summary.h (100%)
>   rename sumtool.c => jffsX-utils/sumtool.c (100%)
>   rename MAKEDEV => misc-utils/MAKEDEV (100%)
>   rename doc_loadbios.c => misc-utils/doc_loadbios.c (100%)
>   rename docfdisk.c => misc-utils/docfdisk.c (100%)
>   rename fectest.c => misc-utils/fectest.c (100%)
>   rename ftl_check.c => misc-utils/ftl_check.c (100%)
>   rename ftl_format.c => misc-utils/ftl_format.c (100%)
>   rename mcast_image.h => misc-utils/mcast_image.h (100%)
>   rename mtd_debug.c => misc-utils/mtd_debug.c (100%)
>   rename mtdpart.c => misc-utils/mtdpart.c (100%)
>   rename recv_image.c => misc-utils/recv_image.c (100%)
>   rename serve_image.c => misc-utils/serve_image.c (100%)
>   delete mode 100644 mkfs.ubifs/defs.h
>   delete mode 100644 mkfs.ubifs/lpt.c
>   delete mode 100644 mkfs.ubifs/mkfs.ubifs.h
>   rename load_nandsim.sh => nand-utils/load_nandsim.sh (100%)
>   rename nanddump.c => nand-utils/nanddump.c (100%)
>   rename nandtest.c => nand-utils/nandtest.c (100%)
>   rename nandwrite.c => nand-utils/nandwrite.c (100%)
>   rename nftl_format.c => nand-utils/nftl_format.c (100%)
>   rename nftldump.c => nand-utils/nftldump.c (100%)
>   rename rfddump.c => nor-utils/rfddump.c (100%)
>   rename rfdformat.c => nor-utils/rfdformat.c (100%)
>   rename {mkfs.ubifs => ubifs-utils}/COPYING (100%)
>   rename {mkfs.ubifs => ubifs-utils}/README (100%)
>   rename {mkfs.ubifs => ubifs-utils/include}/compr.h (97%)
>   rename {mkfs.ubifs => ubifs-utils/include}/crc16.h (100%)
>   create mode 100644 ubifs-utils/include/defs.h
>   create mode 100644 ubifs-utils/include/devtable.h
>   rename {mkfs.ubifs/hashtable => ubifs-utils/include}/hashtable.h (100%)
>   rename {mkfs.ubifs/hashtable => ubifs-utils/include}/hashtable_itr.h (100%)
>   rename {mkfs.ubifs/hashtable => ubifs-utils/include}/hashtable_private.h (100%)
>   create mode 100644 ubifs-utils/include/hexdump.h
>   create mode 100644 ubifs-utils/include/io.h
>   rename {mkfs.ubifs => ubifs-utils/include}/key.h (70%)
>   create mode 100644 ubifs-utils/include/list.h
>   create mode 100644 ubifs-utils/include/lprops.h
>   rename {mkfs.ubifs => ubifs-utils/include}/lpt.h (80%)
>   create mode 100644 ubifs-utils/include/scan.h
>   rename {mkfs.ubifs => ubifs-utils/include}/ubifs.h (83%)
>   create mode 100644 ubifs-utils/include/ubifs_common.h
>   rename {mkfs.ubifs => ubifs-utils/lib}/compr.c (92%)
>   rename {mkfs.ubifs => ubifs-utils/lib}/crc16.c (100%)
>   rename {mkfs.ubifs => ubifs-utils/lib}/devtable.c (99%)
>   rename {mkfs.ubifs/hashtable => ubifs-utils/lib}/hashtable.c (100%)
>   rename {mkfs.ubifs/hashtable => ubifs-utils/lib}/hashtable_itr.c (100%)
>   create mode 100644 ubifs-utils/lib/hexdump.c
>   create mode 100644 ubifs-utils/lib/io.c
>   create mode 100644 ubifs-utils/lib/lprops.c
>   create mode 100644 ubifs-utils/lib/lpt.c
>   create mode 100644 ubifs-utils/lib/scan.c
>   rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/.gitignore (100%)
>   rename {mkfs.ubifs => ubifs-utils/mkfs.ubifs}/mkfs.ubifs.c (94%)
>   create mode 100644 ubifs-utils/mkfs.ubifs/mkfs.ubifs.h
>   create mode 100644 ubifs-utils/ubifs_dump/ubifs_dump.c
>


[-- Attachment #2: ubifs_dump --]
[-- Type: text/plain, Size: 4640 bytes --]

SUPER BLOCK: 
	Common header: 
	magic 				0x6101831
	crc 				0x6e58535a
	node_type 			6 (UBIFS_SB_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				1
	len 				4096
		UUID 				CCE1BBC9-2113-47F4-9164-C6218F3D0137
		key_hash 			0 (UBIFS_KEY_HASH_R5)
		key_fmt 			0 (UBIFS_SIMPLE_KEY_FMT)
		flags 				0
		big_lpt 			0
		space_fixup 			0
		min_io_size 			8
		leb_size 			130944
		leb_cnt 			16401
		max_leb_cnt 			16401
		max_bud_bytes 			8388608
		log_lebs 			4
		lpt_lebs 			3
		orph_lebs 			1
		jhead_cnt 			1
		fanout 				8
		lsave_cnt 			256
		default_compr 			1
		rp_size 			0
		rp_uid 				0
		rp_gid 				0
		fmt_version 			4
		time_gran 			1000000000

MASTER: 
	Common header: 
	magic 				0x6101831
	crc 				0xe4d981b6
	node_type 			7 (UBIFS_MST_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				22
	len 				512
		highest_inum 			65
		commit number 			1
		flags 				0x2
		log_lnum 			4
		root_lnum 			13
		root_offs 			48
		root_len 			108
		gc_lnum 			12
		ihead_lnum 			13
		ihead_offs 			160
		index_size 			112
		lpt_lnum 			7
		lpt_offs 			225
		nhead_lnum 			7
		nhead_offs 			240
		ltab_lnum 			7
		ltab_offs 			120
		lsave_lnum 			0
		lsave_offs 			0
		lscan_lnum 			11
		leb_cnt 			16401
		empty_lebs 			16387
		idx_lebs 			1
		total_free 			2146171240
		total_dirty 			368
		total_used 			440
		total_dead 			0
		total_dark 			69751584

LOG AREA:
	LOG LEB 4
	NODE 0 <START COMMIT>:
	Common header: 
	magic 				0x6101831
	crc 				0x12f7d960
	node_type 			10 (UBIFS_CS_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				16
	len 				32

	NODE 1:
	Common header: 
	magic 				0x6101831
	crc 				0x46c17203
	node_type 			8 (UBIFS_REF_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				17
	len 				64
		lnum 				11
		offs 				704
		jhead 				1

	NODE 2:
	Common header: 
	magic 				0x6101831
	crc 				0x4201943
	node_type 			8 (UBIFS_REF_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				18
	len 				64
		lnum 				14
		offs 				56
		jhead 				2

LPT AREA: 
	LPT INFO: 
	LPT root is at 7:225
	LPT head is at 7:240
	LPT ltab is at 7:120
	LPT LEB 7 free 130704 dirty 123 tgc 0 cmt 0
	LPT LEB 8 free 130944 dirty 0 tgc 0 cmt 0
	LPT LEB 9 free 130944 dirty 0 tgc 0 cmt 0

	LPROPS statistics: 
	empty_lebs 16387, idx_lebs  1
	taken_empty_lebs 0, total_free 2146171240, total_dirty 368
	total_used 440, total_dark 69751584, total_dead 0

	LPROPS TREE: 
	LEB 11      free 130240   dirty 320      used 384      free + dirty 130560   dark 0    dead 130560 nodes fit 30  flags 0x3  (free)
	LEB 13      free 130784   dirty 48       used 112      free + dirty 130832   flags 0x22 (dirty index)
	LEB 14      free 130888   dirty 0        used 56       free + dirty 130888   dark 0    dead 130888 nodes fit 30  flags 0x3  (free)

MAIN AREA: 
	INDEX AREA: 
	Common header: 
	magic 				0x6101831
	crc 				0xd098ad48
	node_type 			9 (UBIFS_IDX_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				19
	len 				108
		child_cnt 			4
		level 				0
		Branches:
		0: LEB 11:384 len 160 key (1, UBIFS_INO_NODE)
		1: LEB 11:160 len 61 key (1, UBIFS_DENT_NODE, 0x18a2149)
		2: LEB 11:544 len 160 key (65, UBIFS_INO_NODE)
		3: LEB 14:0 len 54 key (65, UBIFS_DATA_NODE, 0)

	Common header: 
	magic 				0x6101831
	crc 				0xeb3f7f44
	node_type 			0 (UBIFS_INO_NODE)
	group_type 			2 (UBIFS_LAST_OF_NODE_GROUP)
	sqnum 				12
	len 				160
		key 				(1, UBIFS_INO_NODE)
		creat_sqnum 			1
		size 				224
		nlink 				2
		atime 				1440006524.0
		mtime 				1440006541.0
		ctime 				1440006541.0
		uid 				0
		gid 				0
		mode 				16877
		flags 				0x1
		xattr_cnt 			0
		xattr_size 			0
		xattr_names 			0
		compr_type 			0x1
		data len 			0

	Common header: 
	magic 				0x6101831
	crc 				0x62c6adf3
	node_type 			2 (UBIFS_DENT_NODE)
	group_type 			1 (UBIFS_IN_NODE_GROUP)
	sqnum 				10
	len 				61
		key 				(1, UBIFS_DENT_NODE, 0x18a2149)
		inum 				65
		type 				0
		nlen 				4
		name 				data

	Common header: 
	magic 				0x6101831
	crc 				0x879a9588
	node_type 			0 (UBIFS_INO_NODE)
	group_type 			2 (UBIFS_LAST_OF_NODE_GROUP)
	sqnum 				13
	len 				160
		key 				(65, UBIFS_INO_NODE)
		creat_sqnum 			8
		size 				6
		nlink 				1
		atime 				1440006541.0
		mtime 				1440006541.0
		ctime 				1440006541.0
		uid 				0
		gid 				0
		mode 				33188
		flags 				0x1
		xattr_cnt 			0
		xattr_size 			0
		xattr_names 			0
		compr_type 			0x1
		data len 			0

	Common header: 
	magic 				0x6101831
	crc 				0xe96960f6
	node_type 			1 (UBIFS_DATA_NODE)
	group_type 			0 (UBIFS_NO_NODE_GROUP)
	sqnum 				15
	len 				54
		key 				(65, UBIFS_DATA_NODE, 0)
		size 				6
		compr_typ 			0
		data size 			6
		data:
		00000000: 55 42 49 46 53 0a


^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (27 preceding siblings ...)
  2015-08-19  8:48 ` [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
@ 2015-08-19 20:45 ` Richard Weinberger
  2015-08-20  3:07   ` Dongsheng Yang
  2015-09-16 13:32 ` Richard Weinberger
  29 siblings, 1 reply; 44+ messages in thread
From: Richard Weinberger @ 2015-08-19 20:45 UTC (permalink / raw)
  To: Dongsheng Yang, dedekind1, computersforpeace; +Cc: linux-mtd

Dongsheng,

Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
> Hi Atem, Richard and Brian,
> 	This patchset introduce a userspace tool named ubifs_dump
> to dump data from a ubi media.
> 	It will dump the areas in ubifs, such as super block,
> master, log, lpt and main.
> 	That's helpful for us to see what exactly written in
> media.
> 	the [1/patch] is a RESEND patch, it restructures the mtd-utils.
> Please Brian help to take a look at it. thanx a lot. :)
> 
> NOTE:
> 	This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
> But you can get a full code at:
> https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1
> 
> Please help to review or test it.

thanks a lot for your patches, I'll happily test and review them.
But I'll be on vacation till 14th of September.
So, it has to wait a bit. :-)

Thanks,
//richard

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-08-19 20:45 ` Richard Weinberger
@ 2015-08-20  3:07   ` Dongsheng Yang
  0 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-08-20  3:07 UTC (permalink / raw)
  To: Richard Weinberger, dedekind1, computersforpeace; +Cc: linux-mtd

On 08/20/2015 04:45 AM, Richard Weinberger wrote:
> Dongsheng,
>
> Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
>> Hi Atem, Richard and Brian,
>> 	This patchset introduce a userspace tool named ubifs_dump
>> to dump data from a ubi media.
>> 	It will dump the areas in ubifs, such as super block,
>> master, log, lpt and main.
>> 	That's helpful for us to see what exactly written in
>> media.
>> 	the [1/patch] is a RESEND patch, it restructures the mtd-utils.
>> Please Brian help to take a look at it. thanx a lot. :)
>>
>> NOTE:
>> 	This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
>> But you can get a full code at:
>> https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1
>>
>> Please help to review or test it.
>
> thanks a lot for your patches, I'll happily test and review them.
> But I'll be on vacation till 14th of September.
> So, it has to wait a bit. :-)

That's okey, have a nice vacation :).

Yang
>
> Thanks,
> //richard
> .
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
                   ` (28 preceding siblings ...)
  2015-08-19 20:45 ` Richard Weinberger
@ 2015-09-16 13:32 ` Richard Weinberger
  2015-09-17  5:39   ` Dongsheng Yang
  29 siblings, 1 reply; 44+ messages in thread
From: Richard Weinberger @ 2015-09-16 13:32 UTC (permalink / raw)
  To: Dongsheng Yang, dedekind1, computersforpeace; +Cc: linux-mtd

Yang,

Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
> Hi Atem, Richard and Brian,
> 	This patchset introduce a userspace tool named ubifs_dump
> to dump data from a ubi media.
> 	It will dump the areas in ubifs, such as super block,
> master, log, lpt and main.
> 	That's helpful for us to see what exactly written in
> media.
> 	the [1/patch] is a RESEND patch, it restructures the mtd-utils.
> Please Brian help to take a look at it. thanx a lot. :)
> 
> NOTE:
> 	This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
> But you can get a full code at:
> https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1

I'm looking/testing right now your patches.
Your tools is useful. I like the idea, maybe the can change the name to
"ubifs_dump_meta"? Do you have plans to extend it?

I'm asking because I've started working on an ubifs.fsck/debugfs tool based on
some scripts and hacky other tools I wrote some time ago.
So, let's coordinate and avoid double work.

It's first feature is being able to extract all files from an UBIFS.
Scanning and identifying all UBIFS nodes, as your tool does, is a subset
of the needed functionality. As you sent patches first I'll happily rebase
my tool to your patches. One of the major differences is that it will work without the UBI/UBIFS kernel
modules. You can use /dev/ubiXY or a plain nanddump.
I hope I can release the first version soon.

Thanks,
//richard

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-09-16 13:32 ` Richard Weinberger
@ 2015-09-17  5:39   ` Dongsheng Yang
  2015-09-17 21:21     ` Richard Weinberger
  0 siblings, 1 reply; 44+ messages in thread
From: Dongsheng Yang @ 2015-09-17  5:39 UTC (permalink / raw)
  To: Richard Weinberger, dedekind1, computersforpeace; +Cc: linux-mtd

On 09/16/2015 09:32 PM, Richard Weinberger wrote:
> Yang,
>
> Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
>> Hi Atem, Richard and Brian,
>> 	This patchset introduce a userspace tool named ubifs_dump
>> to dump data from a ubi media.
>> 	It will dump the areas in ubifs, such as super block,
>> master, log, lpt and main.
>> 	That's helpful for us to see what exactly written in
>> media.
>> 	the [1/patch] is a RESEND patch, it restructures the mtd-utils.
>> Please Brian help to take a look at it. thanx a lot. :)
>>
>> NOTE:
>> 	This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
>> But you can get a full code at:
>> https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1
>
> I'm looking/testing right now your patches.
> Your tools is useful. I like the idea, maybe the can change the name to
> "ubifs_dump_meta"? Do you have plans to extend it?
Hi Richard,

ubifs_dump_meta? Hmmm, but I also dump data nodes in it. Do you have
some reason to rename it? Extend it? Of course, I would like to make it
better and better. But the first step is making it in mtd-utils.
>
> I'm asking because I've started working on an ubifs.fsck/debugfs tool based on
> some scripts and hacky other tools I wrote some time ago.
> So, let's coordinate and avoid double work.

Oh, sorry for sending my patch without any asking. I thought there is
nobody caring about it. And what's more, I am planning to do a fsck.ubifs
actually. Haha, so I think we can cooperate on it. Could you share more
about your work so far?
>
> It's first feature is being able to extract all files from an UBIFS.
> Scanning and identifying all UBIFS nodes, as your tool does, is a subset
> of the needed functionality. As you sent patches first I'll happily rebase
> my tool to your patches.

Thanx a lot. :)
> One of the major differences is that it will work without the UBI/UBIFS kernel
> modules. You can use /dev/ubiXY or a plain nanddump.
> I hope I can release the first version soon.

Yea, sounds very interesting, although I am not sure how it can be
implemented. Looking forward your release.

Thanx
Yang
>
> Thanks,
> //richard
> .
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-09-17  5:39   ` Dongsheng Yang
@ 2015-09-17 21:21     ` Richard Weinberger
  2015-09-18  2:23       ` Dongsheng Yang
  0 siblings, 1 reply; 44+ messages in thread
From: Richard Weinberger @ 2015-09-17 21:21 UTC (permalink / raw)
  To: Dongsheng Yang, dedekind1, computersforpeace; +Cc: linux-mtd

Am 17.09.2015 um 07:39 schrieb Dongsheng Yang:
> On 09/16/2015 09:32 PM, Richard Weinberger wrote:
>> Yang,
>>
>> Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
>>> Hi Atem, Richard and Brian,
>>>     This patchset introduce a userspace tool named ubifs_dump
>>> to dump data from a ubi media.
>>>     It will dump the areas in ubifs, such as super block,
>>> master, log, lpt and main.
>>>     That's helpful for us to see what exactly written in
>>> media.
>>>     the [1/patch] is a RESEND patch, it restructures the mtd-utils.
>>> Please Brian help to take a look at it. thanx a lot. :)
>>>
>>> NOTE:
>>>     This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
>>> But you can get a full code at:
>>> https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1
>>
>> I'm looking/testing right now your patches.
>> Your tools is useful. I like the idea, maybe the can change the name to
>> "ubifs_dump_meta"? Do you have plans to extend it?
> Hi Richard,
> 
> ubifs_dump_meta? Hmmm, but I also dump data nodes in it. Do you have
> some reason to rename it? Extend it? Of course, I would like to make it
> better and better. But the first step is making it in mtd-utils.

You are right.
Let's keep the name. It does the same as jffs2dump.

>> I'm asking because I've started working on an ubifs.fsck/debugfs tool based on
>> some scripts and hacky other tools I wrote some time ago.
>> So, let's coordinate and avoid double work.
> 
> Oh, sorry for sending my patch without any asking. I thought there is
> nobody caring about it. And what's more, I am planning to do a fsck.ubifs
> actually. Haha, so I think we can cooperate on it. Could you share more
> about your work so far?

It started as a simple tool to unpack UBIFS.
As unpacking involves also analyzing the whole file system we can extend
it to repair problems.
The first version will only be able to repair minor issue but I hope
we can add more advanced features step by step.

>>
>> It's first feature is being able to extract all files from an UBIFS.
>> Scanning and identifying all UBIFS nodes, as your tool does, is a subset
>> of the needed functionality. As you sent patches first I'll happily rebase
>> my tool to your patches.
> 
> Thanx a lot. :)
>> One of the major differences is that it will work without the UBI/UBIFS kernel
>> modules. You can use /dev/ubiXY or a plain nanddump.
>> I hope I can release the first version soon.
> 
> Yea, sounds very interesting, although I am not sure how it can be
> implemented. Looking forward your release.

You mean using a plain nanddump? It is not that hard, all you need is a "mini-UBI"
implementation. My current UBIFS unpacker works so far only with nanddumps.
It's the only thing I get from customers, nobody sends me an UBIFS only image. :D

Thanks,
//richard

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils
  2015-09-17 21:21     ` Richard Weinberger
@ 2015-09-18  2:23       ` Dongsheng Yang
  0 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-09-18  2:23 UTC (permalink / raw)
  To: Richard Weinberger, dedekind1, computersforpeace; +Cc: linux-mtd

On 09/18/2015 05:21 AM, Richard Weinberger wrote:
> Am 17.09.2015 um 07:39 schrieb Dongsheng Yang:
>> On 09/16/2015 09:32 PM, Richard Weinberger wrote:
>>> Yang,
>>>
>>> Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
>>>> Hi Atem, Richard and Brian,
>>>>      This patchset introduce a userspace tool named ubifs_dump
>>>> to dump data from a ubi media.
>>>>      It will dump the areas in ubifs, such as super block,
>>>> master, log, lpt and main.
>>>>      That's helpful for us to see what exactly written in
>>>> media.
>>>>      the [1/patch] is a RESEND patch, it restructures the mtd-utils.
>>>> Please Brian help to take a look at it. thanx a lot. :)
>>>>
>>>> NOTE:
>>>>      This patch set depends on a patch I sent out [ubifs: correct the size of nnode in memset]
>>>> But you can get a full code at:
>>>> https://github.com/yangdongsheng/mtd-utils.git ubifs_dump_v1
>>>
>>> I'm looking/testing right now your patches.
>>> Your tools is useful. I like the idea, maybe the can change the name to
>>> "ubifs_dump_meta"? Do you have plans to extend it?
>> Hi Richard,
>>
>> ubifs_dump_meta? Hmmm, but I also dump data nodes in it. Do you have
>> some reason to rename it? Extend it? Of course, I would like to make it
>> better and better. But the first step is making it in mtd-utils.
>
> You are right.
> Let's keep the name. It does the same as jffs2dump.
>
>>> I'm asking because I've started working on an ubifs.fsck/debugfs tool based on
>>> some scripts and hacky other tools I wrote some time ago.
>>> So, let's coordinate and avoid double work.
>>
>> Oh, sorry for sending my patch without any asking. I thought there is
>> nobody caring about it. And what's more, I am planning to do a fsck.ubifs
>> actually. Haha, so I think we can cooperate on it. Could you share more
>> about your work so far?
>
> It started as a simple tool to unpack UBIFS.
> As unpacking involves also analyzing the whole file system we can extend
> it to repair problems.
> The first version will only be able to repair minor issue but I hope
> we can add more advanced features step by step.

Similar with what's in my mind. Let me share my idea here.

(1), ubifs_dump.
As what I sent out, this tool is dumping the metadata and data of ubifs.

(2), fsck.ubifs
This tool is going to check the all nodes in media. I did not begin it.

(3), fsck.ubifs --repair
Introduce a --repair to repair problems we found in checking.

Sounds similar with what you are going to do.

Yang
>
>>>
>>> It's first feature is being able to extract all files from an UBIFS.
>>> Scanning and identifying all UBIFS nodes, as your tool does, is a subset
>>> of the needed functionality. As you sent patches first I'll happily rebase
>>> my tool to your patches.
>>
>> Thanx a lot. :)
>>> One of the major differences is that it will work without the UBI/UBIFS kernel
>>> modules. You can use /dev/ubiXY or a plain nanddump.
>>> I hope I can release the first version soon.
>>
>> Yea, sounds very interesting, although I am not sure how it can be
>> implemented. Looking forward your release.
>
> You mean using a plain nanddump? It is not that hard, all you need is a "mini-UBI"
> implementation. My current UBIFS unpacker works so far only with nanddumps.
> It's the only thing I get from customers, nobody sends me an UBIFS only image. :D
>
> Thanks,
> //richard
> .
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [PATCH 24/27] ubifs: ubifs_dump: dump index area
  2015-08-19  8:39 ` [PATCH 24/27] ubifs: ubifs_dump: dump index area Dongsheng Yang
@ 2015-09-24  6:25   ` Richard Weinberger
  2015-09-24  6:32     ` Dongsheng Yang
  2015-09-24  6:46     ` [PATCH v2] " Dongsheng Yang
  0 siblings, 2 replies; 44+ messages in thread
From: Richard Weinberger @ 2015-09-24  6:25 UTC (permalink / raw)
  To: Dongsheng Yang, dedekind1, computersforpeace; +Cc: linux-mtd

Yang,

while porting my tool to your patches I've spotted this:

Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
> +static int dump_zbr(struct ubifs_info *c, struct ubifs_zbranch *zbr)

[...]

> +
> +	if (le32_to_cpu(((struct ubifs_ch *)buf)->node_type) != UBIFS_IDX_NODE)

->node_type is u8 not le32.

> +		goto out;
> +
> +	idx = ((struct ubifs_idx_node *)buf);
> +	for (i = 0; i < le32_to_cpu(idx->child_cnt); i++) {

->child_cnt is le16 not le32.

> +		struct ubifs_branch *br;
> +		struct ubifs_zbranch child;

Please double check also other places where access on-disk data
to use the correct width.

Thanks,
//richard

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [PATCH 24/27] ubifs: ubifs_dump: dump index area
  2015-09-24  6:25   ` Richard Weinberger
@ 2015-09-24  6:32     ` Dongsheng Yang
  2015-09-24  6:46     ` [PATCH v2] " Dongsheng Yang
  1 sibling, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-09-24  6:32 UTC (permalink / raw)
  To: Richard Weinberger, dedekind1, computersforpeace; +Cc: linux-mtd

On 09/24/2015 02:25 PM, Richard Weinberger wrote:
> Yang,
>
> while porting my tool to your patches I've spotted this:
>
> Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
>> +static int dump_zbr(struct ubifs_info *c, struct ubifs_zbranch *zbr)
>
> [...]
>
>> +
>> +	if (le32_to_cpu(((struct ubifs_ch *)buf)->node_type) != UBIFS_IDX_NODE)
>
> ->node_type is u8 not le32.
>
>> +		goto out;
>> +
>> +	idx = ((struct ubifs_idx_node *)buf);
>> +	for (i = 0; i < le32_to_cpu(idx->child_cnt); i++) {
>
> ->child_cnt is le16 not le32.
>
>> +		struct ubifs_branch *br;
>> +		struct ubifs_zbranch child;
>
> Please double check also other places where access on-disk data
> to use the correct width.

Oh, yes. Will update it soon.

Yang
>
> Thanks,
> //richard
> .
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

* [PATCH v2] ubifs: ubifs_dump: dump index area
  2015-09-24  6:25   ` Richard Weinberger
  2015-09-24  6:32     ` Dongsheng Yang
@ 2015-09-24  6:46     ` Dongsheng Yang
  1 sibling, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-09-24  6:46 UTC (permalink / raw)
  To: linux-mtd; +Cc: richard, Dongsheng Yang

Scan the whole index tree and dump each index node.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
	changelog:
		- fix the incorrect width in reading data
		  from flash. Thanx Richard to point it out.

 ubifs-utils/ubifs_dump/ubifs_dump.c | 95 +++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 0d375f0..ab3a682 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -886,6 +886,97 @@ static int dump_lpt(void)
 	return 0;
 }
 
+/**
+ * ubifs_read_node - read node.
+ * @c: UBIFS file-system description object
+ * @buf: buffer to read to
+ * @type: node type
+ * @len: node length (not aligned)
+ * @lnum: logical eraseblock number
+ * @offs: offset within the logical eraseblock
+ *
+ * This function reads a node of known type and and length, checks it and
+ * stores in @buf. Returns zero in case of success, %-EUCLEAN if CRC mismatched
+ * and a negative error code in case of failure.
+ */
+int ubifs_read_node(const struct ubifs_info *c, void *buf, int len,
+		    int lnum, loff_t offs)
+{
+	int err;
+
+	ubifs_assert(lnum >= 0 && lnum < c->leb_cnt && offs >= 0);
+	ubifs_assert(len >= UBIFS_CH_SZ && offs + len <= c->leb_size);
+	ubifs_assert(!(offs & 7) && offs < c->leb_size);
+	ubifs_assert(type >= 0 && type < UBIFS_NODE_TYPES_CNT);
+
+	err = ubifs_read((loff_t)lnum * c->leb_size + offs, len, buf);
+	if (err && err != -EBADMSG)
+		return err;
+
+	return 0;
+}
+
+static int dump_zbr(struct ubifs_info *c, struct ubifs_zbranch *zbr)
+{
+	int i = 0;
+	int err = 0;
+	struct ubifs_idx_node *idx;
+	void *buf;
+
+	buf = malloc(zbr->len);
+	if (!buf)
+		return -ENOMEM;
+
+	err = ubifs_read_node(c, buf, zbr->len, zbr->lnum, zbr->offs);
+	if (err)
+		goto out;
+
+	dump_node(c, buf);
+
+	if (((struct ubifs_ch *)buf)->node_type != UBIFS_IDX_NODE)
+		goto out;
+
+	idx = ((struct ubifs_idx_node *)buf);
+	for (i = 0; i < le16_to_cpu(idx->child_cnt); i++) {
+		struct ubifs_branch *br;
+		struct ubifs_zbranch child;
+
+		br = ubifs_idx_branch(c, idx, i);
+		child.lnum = le32_to_cpu(br->lnum);
+		child.offs = le32_to_cpu(br->offs);
+		child.len = le32_to_cpu(br->len);
+		err = dump_zbr(c, &child);
+		if (err)
+			goto out;
+	}
+
+out:
+	free(buf);
+	return err;
+}
+
+static int dump_index()
+{
+	int err = 0;
+
+	printf("\tINDEX AREA: \n");
+	err = dump_zbr(c, &c->zroot);
+	if (err)
+		return err;
+	return 0;
+}
+
+static int dump_main()
+{
+	int err = 0;
+	printf("MAIN AREA: \n");
+	err = dump_index();
+	if (err)
+		return err;
+
+	return 0;
+}
+
 static int dump()
 {
 	int err = 0;
@@ -911,6 +1002,10 @@ static int dump()
 	err = dump_lpt();
 	if (err)
 		goto free;
+
+	err = dump_main();
+	if (err)
+		goto out;
 free:
 	free(leb_buf);
 out:
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* Re: [PATCH 10/27] ubifs: introduce list.h
  2015-08-19  8:39 ` [PATCH 10/27] ubifs: introduce list.h Dongsheng Yang
@ 2015-10-07 20:41   ` Richard Weinberger
  2015-10-08  0:02     ` Dongsheng Yang
  0 siblings, 1 reply; 44+ messages in thread
From: Richard Weinberger @ 2015-10-07 20:41 UTC (permalink / raw)
  To: Dongsheng Yang, dedekind1, computersforpeace; +Cc: linux-mtd

Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
> Copy the list.h from kernel to ubifs-utils/
> 
> Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
> ---
>  ubifs-utils/include/list.h | 484 +++++++++++++++++++++++++++++++++++++++++++++

This belongs to common mtd-utils code as it is not UBIFS specific.
And why didn't you copy the whole list.h file? I'm missing
list_last_entry() for example.

Thanks,
//richard

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [PATCH 10/27] ubifs: introduce list.h
  2015-10-07 20:41   ` Richard Weinberger
@ 2015-10-08  0:02     ` Dongsheng Yang
  0 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-10-08  0:02 UTC (permalink / raw)
  To: Richard Weinberger, dedekind1, computersforpeace; +Cc: linux-mtd

On 10/08/2015 04:41 AM, Richard Weinberger wrote:
> Am 19.08.2015 um 10:39 schrieb Dongsheng Yang:
>> Copy the list.h from kernel to ubifs-utils/
>>
>> Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
>> ---
>>   ubifs-utils/include/list.h | 484 +++++++++++++++++++++++++++++++++++++++++++++
>
> This belongs to common mtd-utils code as it is not UBIFS specific.

Yes, It's in my TODO, but I did not put it at this time, because
there is a list implementation in jffs2 part, IIRC. So I am planing
to do it later to move all common code into higher level.
> And why didn't you copy the whole list.h file?

Hmmmm.....I only implemented what I required in this patchset. The 
commit message is confusing, It's not a *copy* from kernel. Maybe
it's better that "Implement list structure in ubifs-utils like kernel."

> I'm missing
> list_last_entry() for example.

If you want something more, I think it's okey to add them into list.h

Thanx
Yang

>
> Thanks,
> //richard
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [PATCH 19/27] ubifs: ubifs_dump: dump master node
  2015-08-19  8:39 ` [PATCH 19/27] ubifs: ubifs_dump: dump master node Dongsheng Yang
@ 2015-10-08 10:07   ` David Gstir
  2015-10-09  3:52     ` Dongsheng Yang
  2015-10-09  4:05   ` [PATCH v2] " Dongsheng Yang
  1 sibling, 1 reply; 44+ messages in thread
From: David Gstir @ 2015-10-08 10:07 UTC (permalink / raw)
  To: Dongsheng Yang; +Cc: dedekind1, richard, computersforpeace, linux-mtd

Hi Yang,

I just spotted this issue while reading through your patches:

> On 19.08.2015, at 10:39, Dongsheng Yang <yangds.fnst@cn.fujitsu.com> wrote:

[...]

> +static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node)
> {
> +	struct ubifs_scan_leb *sleb;
> +	struct ubifs_scan_node *snod;
> +	int lnum, offs = 0, nodes_cnt;
> +	static void *leb_buf;
> 	int err = 0;
> +	
> +	lnum = UBIFS_MST_LNUM;
> 
> -	err = init();
> -	if (err)
> +	leb_buf = malloc(c->leb_size);
> +	if (!leb_buf)
> +		return -ENOMEM;
> +
> +	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
> +	if (IS_ERR(sleb)) {
> +		err = PTR_ERR(sleb);
> 		goto out;

This goto will run ubifs_scan_destroy(sleb), which does not work since sleb ist just an ERR_PTR here.

> +	}
> +	nodes_cnt = sleb->nodes_cnt;
> +	if (nodes_cnt > 0) {
> +		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
> +				  list);
> +		if (snod->type != UBIFS_MST_NODE) {
> +			err = -EINVAL;
> +			goto out;
> +		}
> +		memcpy(mst_node, snod->node, snod->len);
> +		offs = snod->offs;
> +	}
> +	ubifs_scan_destroy(sleb);
> +
> +	lnum += 1;
> +
> +	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
> +	if (IS_ERR(sleb)) {
> +		err = PTR_ERR(sleb);
> +		goto out;

Same here.

> +	}
> +	err = -EUCLEAN;
> +	if (sleb->nodes_cnt != nodes_cnt)
> +		goto out;
> +	if (!sleb->nodes_cnt)
> +		goto out;
> +	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
> +	if (snod->type != UBIFS_MST_NODE) {
> +		err = -EINVAL;
> +		goto out;
> +	}
> +	if (snod->offs != offs)
> +		goto out;
> +	if (memcmp((void *)mst_node + UBIFS_CH_SZ,
> +		   (void *)snod->node + UBIFS_CH_SZ,
> +		   UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
> +		goto out;
> +	err = 0;
> +
> +out:
> +	free(leb_buf);
> +	ubifs_scan_destroy(sleb);
> +	return err;
> +}

Cheers,
David

^ permalink raw reply	[flat|nested] 44+ messages in thread

* Re: [PATCH 19/27] ubifs: ubifs_dump: dump master node
  2015-10-08 10:07   ` David Gstir
@ 2015-10-09  3:52     ` Dongsheng Yang
  0 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-10-09  3:52 UTC (permalink / raw)
  To: David Gstir; +Cc: dedekind1, richard, computersforpeace, linux-mtd

On 10/08/2015 06:07 PM, David Gstir wrote:
> Hi Yang,
>
> I just spotted this issue while reading through your patches:
>
>> On 19.08.2015, at 10:39, Dongsheng Yang <yangds.fnst@cn.fujitsu.com> wrote:
>
> [...]
>
>> +static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node)
>> {
>> +	struct ubifs_scan_leb *sleb;
>> +	struct ubifs_scan_node *snod;
>> +	int lnum, offs = 0, nodes_cnt;
>> +	static void *leb_buf;
>> 	int err = 0;
>> +	
>> +	lnum = UBIFS_MST_LNUM;
>>
>> -	err = init();
>> -	if (err)
>> +	leb_buf = malloc(c->leb_size);
>> +	if (!leb_buf)
>> +		return -ENOMEM;
>> +
>> +	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
>> +	if (IS_ERR(sleb)) {
>> +		err = PTR_ERR(sleb);
>> 		goto out;
>
> This goto will run ubifs_scan_destroy(sleb), which does not work since sleb ist just an ERR_PTR here.

Yes, my mistake, will update it soon.

Thanx
Yang
>
>> +	}
>> +	nodes_cnt = sleb->nodes_cnt;
>> +	if (nodes_cnt > 0) {
>> +		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
>> +				  list);
>> +		if (snod->type != UBIFS_MST_NODE) {
>> +			err = -EINVAL;
>> +			goto out;
>> +		}
>> +		memcpy(mst_node, snod->node, snod->len);
>> +		offs = snod->offs;
>> +	}
>> +	ubifs_scan_destroy(sleb);
>> +
>> +	lnum += 1;
>> +
>> +	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
>> +	if (IS_ERR(sleb)) {
>> +		err = PTR_ERR(sleb);
>> +		goto out;
>
> Same here.
>
>> +	}
>> +	err = -EUCLEAN;
>> +	if (sleb->nodes_cnt != nodes_cnt)
>> +		goto out;
>> +	if (!sleb->nodes_cnt)
>> +		goto out;
>> +	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
>> +	if (snod->type != UBIFS_MST_NODE) {
>> +		err = -EINVAL;
>> +		goto out;
>> +	}
>> +	if (snod->offs != offs)
>> +		goto out;
>> +	if (memcmp((void *)mst_node + UBIFS_CH_SZ,
>> +		   (void *)snod->node + UBIFS_CH_SZ,
>> +		   UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
>> +		goto out;
>> +	err = 0;
>> +
>> +out:
>> +	free(leb_buf);
>> +	ubifs_scan_destroy(sleb);
>> +	return err;
>> +}
>
> Cheers,
> David
>
>
> .
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

* [PATCH v2] ubifs: ubifs_dump: dump master node
  2015-08-19  8:39 ` [PATCH 19/27] ubifs: ubifs_dump: dump master node Dongsheng Yang
  2015-10-08 10:07   ` David Gstir
@ 2015-10-09  4:05   ` Dongsheng Yang
  2015-10-09  4:06     ` Dongsheng Yang
  1 sibling, 1 reply; 44+ messages in thread
From: Dongsheng Yang @ 2015-10-09  4:05 UTC (permalink / raw)
  To: david, linux-mtd; +Cc: richard, computersforpeace, Dongsheng Yang

Search the master lebs and found the latest master node.
Then dump it out.

Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
---
changelog:
	- v1:
		fix a bug in error path in scan_for_master();

 Makefile                            |   1 +
 ubifs-utils/ubifs_dump/ubifs_dump.c | 139 +++++++++++++++++++++++++++++-------
 2 files changed, 113 insertions(+), 27 deletions(-)

diff --git a/Makefile b/Makefile
index cad71dc..7abbef9 100644
--- a/Makefile
+++ b/Makefile
@@ -133,6 +133,7 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
 $(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
 
 obj-ubifs_dump = $(UBIFS_LIBS)
+obj-ubifs_dump += ../lib/scan.o
 LDFLAGS_ubifs_dump = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
 LDLIBS_ubifs_dump = -lz -llzo2 -lm -luuid
 $(call mkdep,ubifs-utils/ubifs_dump/,ubifs_dump,,ubi-utils/libubi.a)
diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
index 9ddb0f7..f7fac0d 100644
--- a/ubifs-utils/ubifs_dump/ubifs_dump.c
+++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
@@ -2,9 +2,11 @@
 #define PROGRAM_NAME "ubifs-dump"
 #include "common.h"
 
+#include "ubifs.h"
 #include "io.h"
 #include "key.h"
 #include "lpt.h"
+#include "scan.h"
 
 #define DBG_KEY_BUF_LEN		48
 
@@ -17,9 +19,6 @@ static const struct option longopts[] = {
 struct ubifs_info info_;
 static struct ubifs_info *c = &info_;
 
-/* Global buffers */
-static void *leb_buf;
-
 /* Global nodes*/
 struct ubifs_mst_node mst;
 struct ubifs_sb_node sup;
@@ -376,26 +375,6 @@ void dump_node(const struct ubifs_info *c, const void *node)
 	printf("\n");
 }
 
-/**
- * init - initialize things.
- */
-static int init(void)
-{
-	leb_buf = malloc(c->leb_size);
-	if (!leb_buf)
-		return err_msg("out of memory");
-
-	return 0;
-}
-
-/**
- * deinit - deinitialize things.
- */
-static void deinit(void)
-{
-	free(leb_buf);
-}
-
 /*
  * init_constants_sb - initialize UBIFS constants.
  * @c: UBIFS file-system description object
@@ -498,20 +477,126 @@ static int dump_super(void)
 	return init_constants_sb(c);
 }
 
-static int dump()
+/**
+ * scan_for_master - search the valid master node.
+ * @c: UBIFS file-system description object
+ *
+ * This function scans the master node LEBs and search for the latest master
+ * node. Returns zero in case of success, %-EUCLEAN if there master area is
+ * corrupted and requires recovery, and a negative error code in case of
+ * failure.
+ */
+static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node)
 {
+	struct ubifs_scan_leb *sleb;
+	struct ubifs_scan_node *snod;
+	int lnum, offs = 0, nodes_cnt;
+	static void *leb_buf;
 	int err = 0;
 
-	err = init();
-	if (err)
+	lnum = UBIFS_MST_LNUM;
+
+	leb_buf = malloc(c->leb_size);
+	if (!leb_buf)
+		return -ENOMEM;
+
+	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
+	if (IS_ERR(sleb))
+		return PTR_ERR(sleb);
+	nodes_cnt = sleb->nodes_cnt;
+	if (nodes_cnt > 0) {
+		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
+				  list);
+		if (snod->type != UBIFS_MST_NODE) {
+			err = -EINVAL;
+			goto out;
+		}
+		memcpy(mst_node, snod->node, snod->len);
+		offs = snod->offs;
+	}
+	ubifs_scan_destroy(sleb);
+
+	lnum += 1;
+
+	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
+	if (IS_ERR(sleb)) {
+		return PTR_ERR(sleb);
+	}
+	err = -EUCLEAN;
+	if (sleb->nodes_cnt != nodes_cnt)
+		goto out;
+	if (!sleb->nodes_cnt)
+		goto out;
+	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
+	if (snod->type != UBIFS_MST_NODE) {
+		err = -EINVAL;
+		goto out;
+	}
+	if (snod->offs != offs)
 		goto out;
+	if (memcmp((void *)mst_node + UBIFS_CH_SZ,
+		   (void *)snod->node + UBIFS_CH_SZ,
+		   UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
+		goto out;
+	err = 0;
+
+out:
+	free(leb_buf);
+	ubifs_scan_destroy(sleb);
+	return err;
+}
+
+static int dump_master(void)
+{
+	int err = 0;
+
+	printf("MASTER: \n");
+	err = scan_for_master(c, &mst);
+	if (err)
+		return err;
+	dump_node(c, &mst);
+	mst.flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
+
+	c->max_sqnum       = le64_to_cpu(mst.ch.sqnum);
+	c->highest_inum    = le64_to_cpu(mst.highest_inum);
+	c->zroot.lnum      = le32_to_cpu(mst.root_lnum);
+	c->zroot.offs      = le32_to_cpu(mst.root_offs);
+	c->zroot.len       = le32_to_cpu(mst.root_len);
+	c->gc_lnum         = le32_to_cpu(mst.gc_lnum);
+	c->ihead_lnum      = le32_to_cpu(mst.ihead_lnum);
+	c->ihead_offs      = le32_to_cpu(mst.ihead_offs);
+	c->lpt_lnum        = le32_to_cpu(mst.lpt_lnum);
+	c->lpt_offs        = le32_to_cpu(mst.lpt_offs);
+	c->nhead_lnum      = le32_to_cpu(mst.nhead_lnum);
+	c->nhead_offs      = le32_to_cpu(mst.nhead_offs);
+	c->ltab_lnum       = le32_to_cpu(mst.ltab_lnum);
+	c->ltab_offs       = le32_to_cpu(mst.ltab_offs);
+	c->lsave_lnum      = le32_to_cpu(mst.lsave_lnum);
+	c->lsave_offs      = le32_to_cpu(mst.lsave_offs);
+	c->lscan_lnum      = le32_to_cpu(mst.lscan_lnum);
+	c->lst.empty_lebs  = le32_to_cpu(mst.empty_lebs);
+	c->lst.idx_lebs    = le32_to_cpu(mst.idx_lebs);
+	c->lst.total_free  = le64_to_cpu(mst.total_free);
+	c->lst.total_dirty = le64_to_cpu(mst.total_dirty);
+	c->lst.total_used  = le64_to_cpu(mst.total_used);
+	c->lst.total_dead  = le64_to_cpu(mst.total_dead);
+	c->lst.total_dark  = le64_to_cpu(mst.total_dark);
+
+	return 0;
+}
+
+static int dump()
+{
+	int err = 0;
 
 	err = dump_super();
 	if (err)
 		goto out;
 
+	err = dump_master();
+	if (err)
+		goto out;
 out:
-	deinit();
 	return err;
 }
 
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 44+ messages in thread

* Re: [PATCH v2] ubifs: ubifs_dump: dump master node
  2015-10-09  4:05   ` [PATCH v2] " Dongsheng Yang
@ 2015-10-09  4:06     ` Dongsheng Yang
  0 siblings, 0 replies; 44+ messages in thread
From: Dongsheng Yang @ 2015-10-09  4:06 UTC (permalink / raw)
  To: david, linux-mtd; +Cc: richard, computersforpeace, Dongsheng Yang

On 10/09/2015 12:05 PM, Dongsheng Yang wrote:
> Search the master lebs and found the latest master node.
> Then dump it out.
>
> Signed-off-by: Dongsheng Yang <yangds.fnst@cn.fujitsu.com>
> ---
> changelog:
> 	- v1:
> 		fix a bug in error path in scan_for_master();
>
>   Makefile                            |   1 +
>   ubifs-utils/ubifs_dump/ubifs_dump.c | 139 +++++++++++++++++++++++++++++-------
>   2 files changed, 113 insertions(+), 27 deletions(-)
>
> diff --git a/Makefile b/Makefile
> index cad71dc..7abbef9 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -133,6 +133,7 @@ $(foreach v,$(UBI_BINS),$(eval $(call mkdep,ubi-utils/,$(v),libubi.a ubiutils-co
>   $(foreach v,crc16.o lpt.o compr.o devtable.o io.o hashtable.o hashtable_itr.o,$(eval UBIFS_LIBS += ../lib/$(v)))
>
>   obj-ubifs_dump = $(UBIFS_LIBS)
> +obj-ubifs_dump += ../lib/scan.o
>   LDFLAGS_ubifs_dump = $(ZLIBLDFLAGS) $(LZOLDFLAGS) $(UUIDLDFLAGS)
>   LDLIBS_ubifs_dump = -lz -llzo2 -lm -luuid
>   $(call mkdep,ubifs-utils/ubifs_dump/,ubifs_dump,,ubi-utils/libubi.a)
> diff --git a/ubifs-utils/ubifs_dump/ubifs_dump.c b/ubifs-utils/ubifs_dump/ubifs_dump.c
> index 9ddb0f7..f7fac0d 100644
> --- a/ubifs-utils/ubifs_dump/ubifs_dump.c
> +++ b/ubifs-utils/ubifs_dump/ubifs_dump.c
> @@ -2,9 +2,11 @@
>   #define PROGRAM_NAME "ubifs-dump"
>   #include "common.h"
>
> +#include "ubifs.h"
>   #include "io.h"
>   #include "key.h"
>   #include "lpt.h"
> +#include "scan.h"
>
>   #define DBG_KEY_BUF_LEN		48
>
> @@ -17,9 +19,6 @@ static const struct option longopts[] = {
>   struct ubifs_info info_;
>   static struct ubifs_info *c = &info_;
>
> -/* Global buffers */
> -static void *leb_buf;
> -
>   /* Global nodes*/
>   struct ubifs_mst_node mst;
>   struct ubifs_sb_node sup;
> @@ -376,26 +375,6 @@ void dump_node(const struct ubifs_info *c, const void *node)
>   	printf("\n");
>   }
>
> -/**
> - * init - initialize things.
> - */
> -static int init(void)
> -{
> -	leb_buf = malloc(c->leb_size);
> -	if (!leb_buf)
> -		return err_msg("out of memory");
> -
> -	return 0;
> -}
> -
> -/**
> - * deinit - deinitialize things.
> - */
> -static void deinit(void)
> -{
> -	free(leb_buf);
> -}
> -
>   /*
>    * init_constants_sb - initialize UBIFS constants.
>    * @c: UBIFS file-system description object
> @@ -498,20 +477,126 @@ static int dump_super(void)
>   	return init_constants_sb(c);
>   }
>
> -static int dump()
> +/**
> + * scan_for_master - search the valid master node.
> + * @c: UBIFS file-system description object
> + *
> + * This function scans the master node LEBs and search for the latest master
> + * node. Returns zero in case of success, %-EUCLEAN if there master area is
> + * corrupted and requires recovery, and a negative error code in case of
> + * failure.
> + */
> +static int scan_for_master(struct ubifs_info *c, struct ubifs_mst_node *mst_node)
>   {
> +	struct ubifs_scan_leb *sleb;
> +	struct ubifs_scan_node *snod;
> +	int lnum, offs = 0, nodes_cnt;
> +	static void *leb_buf;
>   	int err = 0;
>
> -	err = init();
> -	if (err)
> +	lnum = UBIFS_MST_LNUM;
> +
> +	leb_buf = malloc(c->leb_size);
> +	if (!leb_buf)
> +		return -ENOMEM;
> +
> +	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
> +	if (IS_ERR(sleb))
> +		return PTR_ERR(sleb);

Damn, forgot to free leb_buf. will send v3 soon.

Yang
> +	nodes_cnt = sleb->nodes_cnt;
> +	if (nodes_cnt > 0) {
> +		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
> +				  list);
> +		if (snod->type != UBIFS_MST_NODE) {
> +			err = -EINVAL;
> +			goto out;
> +		}
> +		memcpy(mst_node, snod->node, snod->len);
> +		offs = snod->offs;
> +	}
> +	ubifs_scan_destroy(sleb);
> +
> +	lnum += 1;
> +
> +	sleb = ubifs_scan(c, lnum, 0, leb_buf, 1);
> +	if (IS_ERR(sleb)) {
> +		return PTR_ERR(sleb);
> +	}
> +	err = -EUCLEAN;
> +	if (sleb->nodes_cnt != nodes_cnt)
> +		goto out;
> +	if (!sleb->nodes_cnt)
> +		goto out;
> +	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
> +	if (snod->type != UBIFS_MST_NODE) {
> +		err = -EINVAL;
> +		goto out;
> +	}
> +	if (snod->offs != offs)
>   		goto out;
> +	if (memcmp((void *)mst_node + UBIFS_CH_SZ,
> +		   (void *)snod->node + UBIFS_CH_SZ,
> +		   UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
> +		goto out;
> +	err = 0;
> +
> +out:
> +	free(leb_buf);
> +	ubifs_scan_destroy(sleb);
> +	return err;
> +}
> +
> +static int dump_master(void)
> +{
> +	int err = 0;
> +
> +	printf("MASTER: \n");
> +	err = scan_for_master(c, &mst);
> +	if (err)
> +		return err;
> +	dump_node(c, &mst);
> +	mst.flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
> +
> +	c->max_sqnum       = le64_to_cpu(mst.ch.sqnum);
> +	c->highest_inum    = le64_to_cpu(mst.highest_inum);
> +	c->zroot.lnum      = le32_to_cpu(mst.root_lnum);
> +	c->zroot.offs      = le32_to_cpu(mst.root_offs);
> +	c->zroot.len       = le32_to_cpu(mst.root_len);
> +	c->gc_lnum         = le32_to_cpu(mst.gc_lnum);
> +	c->ihead_lnum      = le32_to_cpu(mst.ihead_lnum);
> +	c->ihead_offs      = le32_to_cpu(mst.ihead_offs);
> +	c->lpt_lnum        = le32_to_cpu(mst.lpt_lnum);
> +	c->lpt_offs        = le32_to_cpu(mst.lpt_offs);
> +	c->nhead_lnum      = le32_to_cpu(mst.nhead_lnum);
> +	c->nhead_offs      = le32_to_cpu(mst.nhead_offs);
> +	c->ltab_lnum       = le32_to_cpu(mst.ltab_lnum);
> +	c->ltab_offs       = le32_to_cpu(mst.ltab_offs);
> +	c->lsave_lnum      = le32_to_cpu(mst.lsave_lnum);
> +	c->lsave_offs      = le32_to_cpu(mst.lsave_offs);
> +	c->lscan_lnum      = le32_to_cpu(mst.lscan_lnum);
> +	c->lst.empty_lebs  = le32_to_cpu(mst.empty_lebs);
> +	c->lst.idx_lebs    = le32_to_cpu(mst.idx_lebs);
> +	c->lst.total_free  = le64_to_cpu(mst.total_free);
> +	c->lst.total_dirty = le64_to_cpu(mst.total_dirty);
> +	c->lst.total_used  = le64_to_cpu(mst.total_used);
> +	c->lst.total_dead  = le64_to_cpu(mst.total_dead);
> +	c->lst.total_dark  = le64_to_cpu(mst.total_dark);
> +
> +	return 0;
> +}
> +
> +static int dump()
> +{
> +	int err = 0;
>
>   	err = dump_super();
>   	if (err)
>   		goto out;
>
> +	err = dump_master();
> +	if (err)
> +		goto out;
>   out:
> -	deinit();
>   	return err;
>   }
>
>

^ permalink raw reply	[flat|nested] 44+ messages in thread

end of thread, other threads:[~2015-10-09  4:13 UTC | newest]

Thread overview: 44+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-19  8:39 [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
2015-08-19  8:39 ` [PATCH 01/27] mtd-utils: Restructure the mtd-utils source Dongsheng Yang
2015-08-19  8:39 ` [PATCH 02/27] ubifs: pick some common definitions into ubifs_common.h Dongsheng Yang
2015-08-19  8:39 ` [PATCH 03/27] ubifs: move the all io related code into io.[h|c] Dongsheng Yang
2015-08-19  8:39 ` [PATCH 04/27] ubifs: remove the including of mkfs.ubifs.h in lpt.c Dongsheng Yang
2015-08-19  8:39 ` [PATCH 05/27] ubifs: cut off the dependence from compr.o to mkfs.ubifs Dongsheng Yang
2015-08-19  8:39 ` [PATCH 06/27] ubifs: cut off the dependence from devtable to mkfs.ubifs.h Dongsheng Yang
2015-08-19  8:39 ` [PATCH 07/27] ubifs: introduce ubifs-utils/include and ubifs-utils/lib Dongsheng Yang
2015-08-19  8:39 ` [PATCH 08/27] ubifs: move more functions into io lib Dongsheng Yang
2015-08-19  8:39 ` [PATCH 09/27] ubifs: introduce a new tool ubifs_dump Dongsheng Yang
2015-08-19  8:39 ` [PATCH 10/27] ubifs: introduce list.h Dongsheng Yang
2015-10-07 20:41   ` Richard Weinberger
2015-10-08  0:02     ` Dongsheng Yang
2015-08-19  8:39 ` [PATCH 11/27] ubifs: copy some important data in ubifs.h from kernel to ubifs-utils Dongsheng Yang
2015-08-19  8:39 ` [PATCH 12/27] ubifs: copy some important functions in key.h " Dongsheng Yang
2015-08-19  8:39 ` [PATCH 13/27] ubifs: ubifs_dump: add dump_ch and dump_node functions Dongsheng Yang
2015-08-19  8:39 ` [PATCH 14/27] ubifs: defs.h: introduce some compatible definition for printk class Dongsheng Yang
2015-08-19  8:39 ` [PATCH 15/27] ubifs: io: introduce ubifs_read function to read ubi volume Dongsheng Yang
2015-08-19  8:39 ` [PATCH 16/27] ubifs: ubifs_dump: dump super block Dongsheng Yang
2015-08-19  8:39 ` [PATCH 17/27] ubifs: introduce scan for ubifs-utils Dongsheng Yang
2015-08-19  8:39 ` [PATCH 18/27] ubifs: add some more compatible definitions in defs.h Dongsheng Yang
2015-08-19  8:39 ` [PATCH 19/27] ubifs: ubifs_dump: dump master node Dongsheng Yang
2015-10-08 10:07   ` David Gstir
2015-10-09  3:52     ` Dongsheng Yang
2015-10-09  4:05   ` [PATCH v2] " Dongsheng Yang
2015-10-09  4:06     ` Dongsheng Yang
2015-08-19  8:39 ` [PATCH 20/27] ubifs: ubifs_dump: dump log area Dongsheng Yang
2015-08-19  8:39 ` [PATCH 21/27] ubifs: introduce lprops lib Dongsheng Yang
2015-08-19  8:39 ` [PATCH 22/27] ubifs: lpt: implement functions to scan lpt Dongsheng Yang
2015-08-19  8:39 ` [PATCH 23/27] ubifs: ubifs_dump: dump lpt area Dongsheng Yang
2015-08-19  8:39 ` [PATCH 24/27] ubifs: ubifs_dump: dump index area Dongsheng Yang
2015-09-24  6:25   ` Richard Weinberger
2015-09-24  6:32     ` Dongsheng Yang
2015-09-24  6:46     ` [PATCH v2] " Dongsheng Yang
2015-08-19  8:39 ` [PATCH 25/27] ubifs: defs.h: introduce some compatible definitions about integer such as __u16 Dongsheng Yang
2015-08-19  8:39 ` [PATCH 26/27] ubifs: introduce hexdump lib Dongsheng Yang
2015-08-19  8:39 ` [PATCH 27/27] ubifs: ubifs_dump: dump data in hex format Dongsheng Yang
2015-08-19  8:48 ` [RFC PATCH 00/27] Introduce ubifs_dump in ubifs-utils Dongsheng Yang
2015-08-19 20:45 ` Richard Weinberger
2015-08-20  3:07   ` Dongsheng Yang
2015-09-16 13:32 ` Richard Weinberger
2015-09-17  5:39   ` Dongsheng Yang
2015-09-17 21:21     ` Richard Weinberger
2015-09-18  2:23       ` Dongsheng Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).