Changes to enable Linux 4.8 compilation
[yaffs2.git] / yaffs_packedtags2.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 #include "yaffs_packedtags2.h"
15 #include "yportenv.h"
16 #include "yaffs_trace.h"
17 #include "yaffs_endian.h"
18
19 /* This code packs a set of extended tags into a binary structure for
20  * NAND storage
21  */
22
23 /* Some of the information is "extra" struff which can be packed in to
24  * speed scanning
25  * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
26  */
27
28 /* Extra flags applied to chunk_id */
29
30 #define EXTRA_HEADER_INFO_FLAG  0x80000000
31 #define EXTRA_SHRINK_FLAG       0x40000000
32 #define EXTRA_SHADOWS_FLAG      0x20000000
33 #define EXTRA_SPARE_FLAGS       0x10000000
34
35 #define ALL_EXTRA_FLAGS         0xf0000000
36
37 /* Also, the top 4 bits of the object Id are set to the object type. */
38 #define EXTRA_OBJECT_TYPE_SHIFT (28)
39 #define EXTRA_OBJECT_TYPE_MASK  ((0x0f) << EXTRA_OBJECT_TYPE_SHIFT)
40
41 static void yaffs_dump_packed_tags2_tags_only(
42                                 const struct yaffs_packed_tags2_tags_only *ptt)
43 {
44         yaffs_trace(YAFFS_TRACE_MTD,
45                 "packed tags obj %d chunk %d byte %d seq %d",
46                 ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
47 }
48
49 static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
50 {
51         yaffs_dump_packed_tags2_tags_only(&pt->t);
52 }
53
54 static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
55 {
56         yaffs_trace(YAFFS_TRACE_MTD,
57                 "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
58                 t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
59                 t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
60                 t->seq_number);
61
62 }
63
64 static int yaffs_check_tags_extra_packable(const struct yaffs_ext_tags *t)
65 {
66         if (t->chunk_id != 0 || !t->extra_available)
67                 return 0;
68
69         /* Check if the file size is too long to store */
70         if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE &&
71             (t->extra_file_size >> 31) != 0)
72                 return 0;
73         return 1;
74 }
75
76 void yaffs_pack_tags2_tags_only(struct yaffs_dev *dev,
77                                 struct yaffs_packed_tags2_tags_only *ptt,
78                                 const struct yaffs_ext_tags *t)
79 {
80         ptt->chunk_id = t->chunk_id;
81         ptt->seq_number = t->seq_number;
82         ptt->n_bytes = t->n_bytes;
83         ptt->obj_id = t->obj_id;
84
85         /* Only store extra tags for object headers.
86          * If it is a file then only store  if the file size is short\
87          * enough to fit.
88          */
89         if (yaffs_check_tags_extra_packable(t)) {
90                 /* Store the extra header info instead */
91                 /* We save the parent object in the chunk_id */
92                 ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
93                 if (t->extra_is_shrink)
94                         ptt->chunk_id |= EXTRA_SHRINK_FLAG;
95                 if (t->extra_shadows)
96                         ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
97
98                 ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
99                 ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
100
101                 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
102                         ptt->n_bytes = t->extra_equiv_id;
103                 else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
104                         ptt->n_bytes = (unsigned) t->extra_file_size;
105                 else
106                         ptt->n_bytes = 0;
107         }
108
109         yaffs_dump_packed_tags2_tags_only(ptt);
110         yaffs_dump_tags2(t);
111         yaffs_do_endian_packed_tags2(dev, ptt);
112 }
113
114 void yaffs_pack_tags2(struct yaffs_dev *dev,
115                       struct yaffs_packed_tags2 *pt,
116                       const struct yaffs_ext_tags *t, int tags_ecc)
117 {
118         yaffs_pack_tags2_tags_only(dev, &pt->t, t);
119
120         if (tags_ecc)
121                 yaffs_ecc_calc_other((unsigned char *)&pt->t,
122                                     sizeof(struct yaffs_packed_tags2_tags_only),
123                                     &pt->ecc);
124 }
125
126 void yaffs_unpack_tags2_tags_only(struct yaffs_dev *dev,
127                                   struct yaffs_ext_tags *t,
128                                   struct yaffs_packed_tags2_tags_only *ptt_ptr)
129 {
130         struct yaffs_packed_tags2_tags_only ptt_copy = *ptt_ptr;
131
132         memset(t, 0, sizeof(struct yaffs_ext_tags));
133
134         if (ptt_copy.seq_number == 0xffffffff)
135                 return;
136
137         yaffs_do_endian_packed_tags2(dev, &ptt_copy);
138
139         t->block_bad = 0;
140         t->chunk_used = 1;
141         t->obj_id = ptt_copy.obj_id;
142         t->chunk_id = ptt_copy.chunk_id;
143         t->n_bytes = ptt_copy.n_bytes;
144         t->is_deleted = 0;
145         t->serial_number = 0;
146         t->seq_number = ptt_copy.seq_number;
147
148         /* Do extra header info stuff */
149         if (ptt_copy.chunk_id & EXTRA_HEADER_INFO_FLAG) {
150                 t->chunk_id = 0;
151                 t->n_bytes = 0;
152
153                 t->extra_available = 1;
154                 t->extra_parent_id = ptt_copy.chunk_id & (~(ALL_EXTRA_FLAGS));
155                 t->extra_is_shrink = ptt_copy.chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0;
156                 t->extra_shadows = ptt_copy.chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0;
157                 t->extra_obj_type = ptt_copy.obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
158                 t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
159
160                 if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
161                         t->extra_equiv_id = ptt_copy.n_bytes;
162                 else
163                         t->extra_file_size = ptt_copy.n_bytes;
164         }
165         yaffs_dump_packed_tags2_tags_only(ptt_ptr);
166         yaffs_dump_tags2(t);
167 }
168
169 void yaffs_unpack_tags2(struct yaffs_dev *dev,
170                         struct yaffs_ext_tags *t,
171                         struct yaffs_packed_tags2 *pt,
172                         int tags_ecc)
173 {
174         enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
175
176         if (pt->t.seq_number != 0xffffffff && tags_ecc) {
177                 /* Chunk is in use and we need to do ECC */
178
179                 struct yaffs_ecc_other ecc;
180                 int result;
181                 yaffs_ecc_calc_other((unsigned char *)&pt->t,
182                                 sizeof(struct yaffs_packed_tags2_tags_only),
183                                 &ecc);
184                 result =
185                     yaffs_ecc_correct_other((unsigned char *)&pt->t,
186                                 sizeof(struct yaffs_packed_tags2_tags_only),
187                                 &pt->ecc, &ecc);
188                 switch (result) {
189                 case 0:
190                         ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
191                         break;
192                 case 1:
193                         ecc_result = YAFFS_ECC_RESULT_FIXED;
194                         break;
195                 case -1:
196                         ecc_result = YAFFS_ECC_RESULT_UNFIXED;
197                         break;
198                 default:
199                         ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
200                 }
201         }
202         yaffs_unpack_tags2_tags_only(dev, t, &pt->t);
203
204         t->ecc_result = ecc_result;
205
206         yaffs_dump_packed_tags2(pt);
207         yaffs_dump_tags2(t);
208 }