2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include "yaffs_packedtags2.h"
16 #include "yaffs_trace.h"
18 /* This code packs a set of extended tags into a binary structure for
22 /* Some of the information is "extra" struff which can be packed in to
24 * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
27 /* Extra flags applied to chunk_id */
29 #define EXTRA_HEADER_INFO_FLAG 0x80000000
30 #define EXTRA_SHRINK_FLAG 0x40000000
31 #define EXTRA_SHADOWS_FLAG 0x20000000
32 #define EXTRA_SPARE_FLAGS 0x10000000
34 #define ALL_EXTRA_FLAGS 0xf0000000
36 /* Also, the top 4 bits of the object Id are set to the object type. */
37 #define EXTRA_OBJECT_TYPE_SHIFT (28)
38 #define EXTRA_OBJECT_TYPE_MASK ((0x0f) << EXTRA_OBJECT_TYPE_SHIFT)
40 static void yaffs_dump_packed_tags2_tags_only(
41 const struct yaffs_packed_tags2_tags_only *ptt)
43 yaffs_trace(YAFFS_TRACE_MTD,
44 "packed tags obj %d chunk %d byte %d seq %d",
45 ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
48 static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
50 yaffs_dump_packed_tags2_tags_only(&pt->t);
53 static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
55 yaffs_trace(YAFFS_TRACE_MTD,
56 "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
57 t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
58 t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
63 void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
64 const struct yaffs_ext_tags *t)
66 ptt->chunk_id = t->chunk_id;
67 ptt->seq_number = t->seq_number;
68 ptt->n_bytes = t->n_bytes;
69 ptt->obj_id = t->obj_id;
71 if (t->chunk_id == 0 && t->extra_available) {
72 /* Store the extra header info instead */
73 /* We save the parent object in the chunk_id */
74 ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
75 if (t->extra_is_shrink)
76 ptt->chunk_id |= EXTRA_SHRINK_FLAG;
78 ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
80 ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
81 ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
83 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
84 ptt->n_bytes = t->extra_equiv_id;
85 else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
86 ptt->n_bytes = t->extra_length;
91 yaffs_dump_packed_tags2_tags_only(ptt);
95 void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
96 const struct yaffs_ext_tags *t, int tags_ecc)
98 yaffs_pack_tags2_tags_only(&pt->t, t);
101 yaffs_ecc_calc_other((unsigned char *)&pt->t,
102 sizeof(struct yaffs_packed_tags2_tags_only),
106 void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
107 struct yaffs_packed_tags2_tags_only *ptt)
109 memset(t, 0, sizeof(struct yaffs_ext_tags));
111 if (ptt->seq_number == 0xffffffff)
116 t->obj_id = ptt->obj_id;
117 t->chunk_id = ptt->chunk_id;
118 t->n_bytes = ptt->n_bytes;
120 t->serial_number = 0;
121 t->seq_number = ptt->seq_number;
123 /* Do extra header info stuff */
124 if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
128 t->extra_available = 1;
129 t->extra_parent_id = ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
130 t->extra_is_shrink = ptt->chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0;
131 t->extra_shadows = ptt->chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0;
132 t->extra_obj_type = ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
133 t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
135 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
136 t->extra_equiv_id = ptt->n_bytes;
138 t->extra_length = ptt->n_bytes;
140 yaffs_dump_packed_tags2_tags_only(ptt);
144 void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
147 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
149 if (pt->t.seq_number != 0xffffffff && tags_ecc) {
150 /* Chunk is in use and we need to do ECC */
152 struct yaffs_ecc_other ecc;
154 yaffs_ecc_calc_other((unsigned char *)&pt->t,
155 sizeof(struct yaffs_packed_tags2_tags_only),
158 yaffs_ecc_correct_other((unsigned char *)&pt->t,
159 sizeof(struct yaffs_packed_tags2_tags_only),
163 ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
166 ecc_result = YAFFS_ECC_RESULT_FIXED;
169 ecc_result = YAFFS_ECC_RESULT_UNFIXED;
172 ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
175 yaffs_unpack_tags2_tags_only(t, &pt->t);
177 t->ecc_result = ecc_result;
179 yaffs_dump_packed_tags2(pt);