Whamcloud - gitweb
tst_libext2fs: Avoid multiple definition of global variables
[tools/e2fsprogs.git] / lib / ext2fs / csum.c
1 /*
2  * csum.c --- checksumming of ext3 structures
3  *
4  * Copyright (C) 2006 Cluster File Systems, Inc.
5  * Copyright (C) 2006, 2007 by Andreas Dilger <adilger@clusterfs.com>
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Library
9  * General Public License, version 2.
10  * %End-Header%
11  */
12
13 #include "config.h"
14 #if HAVE_SYS_TYPES_H
15 #include <sys/types.h>
16 #endif
17
18 #include "ext2_fs.h"
19 #include "ext2fs.h"
20 #include "crc16.h"
21 #include <assert.h>
22
23 #ifndef offsetof
24 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
25 #endif
26
27 #ifdef DEBUG
28 #define STATIC
29 #else
30 #define STATIC static
31 #endif
32
33 void ext2fs_init_csum_seed(ext2_filsys fs)
34 {
35         if (ext2fs_has_feature_csum_seed(fs->super))
36                 fs->csum_seed = fs->super->s_checksum_seed;
37         else if (ext2fs_has_feature_metadata_csum(fs->super) ||
38                  ext2fs_has_feature_ea_inode(fs->super))
39                 fs->csum_seed = ext2fs_crc32c_le(~0, fs->super->s_uuid,
40                                                  sizeof(fs->super->s_uuid));
41 }
42
43 static __u32 ext2fs_mmp_csum(ext2_filsys fs, struct mmp_struct *mmp)
44 {
45         int offset = offsetof(struct mmp_struct, mmp_checksum);
46
47         return ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)mmp, offset);
48 }
49
50 int ext2fs_mmp_csum_verify(ext2_filsys fs, struct mmp_struct *mmp)
51 {
52         __u32 calculated;
53
54         if (!ext2fs_has_feature_metadata_csum(fs->super))
55                 return 1;
56
57         calculated = ext2fs_mmp_csum(fs, mmp);
58
59         return ext2fs_le32_to_cpu(mmp->mmp_checksum) == calculated;
60 }
61
62 errcode_t ext2fs_mmp_csum_set(ext2_filsys fs, struct mmp_struct *mmp)
63 {
64         __u32 crc;
65
66         if (!ext2fs_has_feature_metadata_csum(fs->super))
67                 return 0;
68
69         crc = ext2fs_mmp_csum(fs, mmp);
70         mmp->mmp_checksum = ext2fs_cpu_to_le32(crc);
71
72         return 0;
73 }
74
75 int ext2fs_verify_csum_type(ext2_filsys fs, struct ext2_super_block *sb)
76 {
77         if (!ext2fs_has_feature_metadata_csum(fs->super))
78                 return 1;
79
80         return sb->s_checksum_type == EXT2_CRC32C_CHKSUM;
81 }
82
83 static __u32 ext2fs_superblock_csum(ext2_filsys fs EXT2FS_ATTR((unused)),
84                                     struct ext2_super_block *sb)
85 {
86         int offset = offsetof(struct ext2_super_block, s_checksum);
87
88         return ext2fs_crc32c_le(~0, (unsigned char *)sb, offset);
89 }
90
91 /* NOTE: The input to this function MUST be in LE order */
92 int ext2fs_superblock_csum_verify(ext2_filsys fs, struct ext2_super_block *sb)
93 {
94         __u32 flag, calculated;
95
96         if (fs->flags & EXT2_FLAG_SWAP_BYTES)
97                 flag = EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
98         else
99                 flag = ext2fs_cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
100
101         if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, flag))
102                 return 1;
103
104         calculated = ext2fs_superblock_csum(fs, sb);
105
106         return ext2fs_le32_to_cpu(sb->s_checksum) == calculated;
107 }
108
109 /* NOTE: The input to this function MUST be in LE order */
110 errcode_t ext2fs_superblock_csum_set(ext2_filsys fs,
111                                      struct ext2_super_block *sb)
112 {
113         __u32 flag, crc;
114
115         if (fs->flags & EXT2_FLAG_SWAP_BYTES)
116                 flag = EXT4_FEATURE_RO_COMPAT_METADATA_CSUM;
117         else
118                 flag = ext2fs_cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM);
119
120         if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, flag))
121                 return 0;
122
123         crc = ext2fs_superblock_csum(fs, sb);
124         sb->s_checksum = ext2fs_cpu_to_le32(crc);
125
126         return 0;
127 }
128
129 static errcode_t ext2fs_ext_attr_block_csum(ext2_filsys fs,
130                                             ext2_ino_t inum EXT2FS_ATTR((unused)),
131                                             blk64_t block,
132                                             struct ext2_ext_attr_header *hdr,
133                                             __u32 *crc)
134 {
135         char *buf = (char *)hdr;
136         __u32 old_crc = hdr->h_checksum;
137
138         hdr->h_checksum = 0;
139         block = ext2fs_cpu_to_le64(block);
140         *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&block,
141                                 sizeof(block));
142         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, fs->blocksize);
143         hdr->h_checksum = old_crc;
144
145         return 0;
146 }
147
148 int ext2fs_ext_attr_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
149                                       blk64_t block,
150                                       struct ext2_ext_attr_header *hdr)
151 {
152         __u32 calculated;
153         errcode_t retval;
154
155         if (!ext2fs_has_feature_metadata_csum(fs->super))
156                 return 1;
157
158         retval = ext2fs_ext_attr_block_csum(fs, inum, block, hdr, &calculated);
159         if (retval)
160                 return 0;
161
162         return ext2fs_le32_to_cpu(hdr->h_checksum) == calculated;
163 }
164
165 errcode_t ext2fs_ext_attr_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
166                                          blk64_t block,
167                                          struct ext2_ext_attr_header *hdr)
168 {
169         errcode_t retval;
170         __u32 crc;
171
172         if (!ext2fs_has_feature_metadata_csum(fs->super))
173                 return 0;
174
175         retval = ext2fs_ext_attr_block_csum(fs, inum, block, hdr, &crc);
176         if (retval)
177                 return retval;
178         hdr->h_checksum = ext2fs_cpu_to_le32(crc);
179         return 0;
180 }
181
182 static __u16 do_nothing16(__u16 x)
183 {
184         return x;
185 }
186
187 static __u16 disk_to_host16(__u16 x)
188 {
189         return ext2fs_le16_to_cpu(x);
190 }
191
192 static errcode_t __get_dx_countlimit(ext2_filsys fs,
193                                      struct ext2_dir_entry *dirent,
194                                      struct ext2_dx_countlimit **cc,
195                                      int *offset,
196                                      int need_swab)
197 {
198         struct ext2_dir_entry *dp;
199         struct ext2_dx_root_info *root;
200         struct ext2_dx_countlimit *c;
201         int count_offset, max_sane_entries;
202         unsigned int rec_len;
203         __u16 (*translate)(__u16) = (need_swab ? disk_to_host16 : do_nothing16);
204
205         rec_len = translate(dirent->rec_len);
206
207         if (rec_len == fs->blocksize && translate(dirent->name_len) == 0)
208                 count_offset = 8;
209         else if (rec_len == 12) {
210                 dp = (struct ext2_dir_entry *)(((char *)dirent) + rec_len);
211                 rec_len = translate(dp->rec_len);
212                 if (rec_len != fs->blocksize - 12)
213                         return EXT2_ET_DB_NOT_FOUND;
214                 root = (struct ext2_dx_root_info *)(((char *)dp + 12));
215                 if (root->reserved_zero ||
216                     root->info_length != sizeof(struct ext2_dx_root_info))
217                         return EXT2_ET_DB_NOT_FOUND;
218                 count_offset = 32;
219         } else
220                 return EXT2_ET_DB_NOT_FOUND;
221
222         c = (struct ext2_dx_countlimit *)(((char *)dirent) + count_offset);
223         max_sane_entries = (fs->blocksize - count_offset) /
224                            sizeof(struct ext2_dx_entry);
225         if (ext2fs_le16_to_cpu(c->limit) > max_sane_entries ||
226             ext2fs_le16_to_cpu(c->count) > max_sane_entries)
227                 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
228
229         if (offset)
230                 *offset = count_offset;
231         if (cc)
232                 *cc = c;
233
234         return 0;
235 }
236
237 errcode_t ext2fs_get_dx_countlimit(ext2_filsys fs,
238                                    struct ext2_dir_entry *dirent,
239                                    struct ext2_dx_countlimit **cc,
240                                    int *offset)
241 {
242         return __get_dx_countlimit(fs, dirent, cc, offset, 0);
243 }
244
245 void ext2fs_initialize_dirent_tail(ext2_filsys fs,
246                                    struct ext2_dir_entry_tail *t)
247 {
248         memset(t, 0, sizeof(struct ext2_dir_entry_tail));
249         ext2fs_set_rec_len(fs, sizeof(struct ext2_dir_entry_tail),
250                            (struct ext2_dir_entry *)t);
251         t->det_reserved_name_len = EXT2_DIR_NAME_LEN_CSUM;
252 }
253
254 static errcode_t __get_dirent_tail(ext2_filsys fs,
255                                    struct ext2_dir_entry *dirent,
256                                    struct ext2_dir_entry_tail **tt,
257                                    int need_swab)
258 {
259         struct ext2_dir_entry *d;
260         void *top;
261         struct ext2_dir_entry_tail *t;
262         unsigned int rec_len;
263         errcode_t retval = 0;
264         __u16 (*translate)(__u16) = (need_swab ? disk_to_host16 : do_nothing16);
265
266         d = dirent;
267         top = EXT2_DIRENT_TAIL(dirent, fs->blocksize);
268
269         rec_len = translate(d->rec_len);
270         while (rec_len && !(rec_len & 0x3)) {
271                 d = (struct ext2_dir_entry *)(((char *)d) + rec_len);
272                 if ((void *)d >= top)
273                         break;
274                 rec_len = translate(d->rec_len);
275         }
276
277         if (d != top)
278                 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
279
280         t = (struct ext2_dir_entry_tail *)d;
281         if (t->det_reserved_zero1 ||
282             translate(t->det_rec_len) != sizeof(struct ext2_dir_entry_tail) ||
283             translate(t->det_reserved_name_len) != EXT2_DIR_NAME_LEN_CSUM)
284                 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
285
286         if (tt)
287                 *tt = t;
288         return retval;
289 }
290
291 int ext2fs_dirent_has_tail(ext2_filsys fs, struct ext2_dir_entry *dirent)
292 {
293         return __get_dirent_tail(fs, dirent, NULL, 0) == 0;
294 }
295
296 static errcode_t ext2fs_dirent_csum(ext2_filsys fs, ext2_ino_t inum,
297                                     struct ext2_dir_entry *dirent, __u32 *crc,
298                                     int size)
299 {
300         errcode_t retval;
301         char *buf = (char *)dirent;
302         __u32 gen;
303         struct ext2_inode inode;
304
305         retval = ext2fs_read_inode(fs, inum, &inode);
306         if (retval)
307                 return retval;
308
309         inum = ext2fs_cpu_to_le32(inum);
310         gen = ext2fs_cpu_to_le32(inode.i_generation);
311         *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
312                                 sizeof(inum));
313         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
314         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, size);
315
316         return 0;
317 }
318
319 int ext2fs_dirent_csum_verify(ext2_filsys fs, ext2_ino_t inum,
320                               struct ext2_dir_entry *dirent)
321 {
322         errcode_t retval;
323         __u32 calculated;
324         struct ext2_dir_entry_tail *t;
325
326         retval = __get_dirent_tail(fs, dirent, &t, 1);
327         if (retval)
328                 return 1;
329
330         /*
331          * The checksum field is overlaid with the dirent->name field
332          * so the swapfs.c functions won't change the endianness.
333          */
334         retval = ext2fs_dirent_csum(fs, inum, dirent, &calculated,
335                                     (char *)t - (char *)dirent);
336         if (retval)
337                 return 0;
338         return ext2fs_le32_to_cpu(t->det_checksum) == calculated;
339 }
340
341 static errcode_t ext2fs_dirent_csum_set(ext2_filsys fs, ext2_ino_t inum,
342                                         struct ext2_dir_entry *dirent)
343 {
344         errcode_t retval;
345         __u32 crc;
346         struct ext2_dir_entry_tail *t;
347
348         retval = __get_dirent_tail(fs, dirent, &t, 1);
349         if (retval)
350                 return retval;
351
352         /* swapfs.c functions don't change the checksum endianness */
353         retval = ext2fs_dirent_csum(fs, inum, dirent, &crc,
354                                     (char *)t - (char *)dirent);
355         if (retval)
356                 return retval;
357         t->det_checksum = ext2fs_cpu_to_le32(crc);
358         return 0;
359 }
360
361 static errcode_t ext2fs_dx_csum(ext2_filsys fs, ext2_ino_t inum,
362                                 struct ext2_dir_entry *dirent,
363                                 __u32 *crc, int count_offset, int count,
364                                 struct ext2_dx_tail *t)
365 {
366         errcode_t retval;
367         char *buf = (char *)dirent;
368         int size;
369         __u32 old_csum, gen;
370         struct ext2_inode inode;
371
372         size = count_offset + (count * sizeof(struct ext2_dx_entry));
373         old_csum = t->dt_checksum;
374         t->dt_checksum = 0;
375
376         retval = ext2fs_read_inode(fs, inum, &inode);
377         if (retval)
378                 return retval;
379
380         inum = ext2fs_cpu_to_le32(inum);
381         gen = ext2fs_cpu_to_le32(inode.i_generation);
382         *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
383                                 sizeof(inum));
384         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
385         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, size);
386         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)t,
387                                 sizeof(struct ext2_dx_tail));
388         t->dt_checksum = old_csum;
389
390         return 0;
391 }
392
393 static int ext2fs_dx_csum_verify(ext2_filsys fs, ext2_ino_t inum,
394                                  struct ext2_dir_entry *dirent)
395 {
396         __u32 calculated;
397         errcode_t retval;
398         struct ext2_dx_countlimit *c;
399         struct ext2_dx_tail *t;
400         int count_offset, limit, count;
401
402         retval = __get_dx_countlimit(fs, dirent, &c, &count_offset, 1);
403         if (retval)
404                 return 1;
405         limit = ext2fs_le16_to_cpu(c->limit);
406         count = ext2fs_le16_to_cpu(c->count);
407         if (count_offset + (limit * sizeof(struct ext2_dx_entry)) >
408             fs->blocksize - sizeof(struct ext2_dx_tail))
409                 return 0;
410         /* htree structs are accessed in LE order */
411         t = (struct ext2_dx_tail *)(((struct ext2_dx_entry *)c) + limit);
412         retval = ext2fs_dx_csum(fs, inum, dirent, &calculated, count_offset,
413                                 count, t);
414         if (retval)
415                 return 0;
416
417         return ext2fs_le32_to_cpu(t->dt_checksum) == calculated;
418 }
419
420 static errcode_t ext2fs_dx_csum_set(ext2_filsys fs, ext2_ino_t inum,
421                                     struct ext2_dir_entry *dirent)
422 {
423         __u32 crc;
424         errcode_t retval = 0;
425         struct ext2_dx_countlimit *c;
426         struct ext2_dx_tail *t;
427         int count_offset, limit, count;
428
429         retval = __get_dx_countlimit(fs, dirent, &c, &count_offset, 1);
430         if (retval)
431                 return retval;
432         limit = ext2fs_le16_to_cpu(c->limit);
433         count = ext2fs_le16_to_cpu(c->count);
434         if (count_offset + (limit * sizeof(struct ext2_dx_entry)) >
435             fs->blocksize - sizeof(struct ext2_dx_tail))
436                 return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
437         t = (struct ext2_dx_tail *)(((struct ext2_dx_entry *)c) + limit);
438
439         /* htree structs are accessed in LE order */
440         retval = ext2fs_dx_csum(fs, inum, dirent, &crc, count_offset, count, t);
441         if (retval)
442                 return retval;
443         t->dt_checksum = ext2fs_cpu_to_le32(crc);
444         return retval;
445 }
446
447 int ext2fs_dir_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
448                                  struct ext2_dir_entry *dirent)
449 {
450         if (!ext2fs_has_feature_metadata_csum(fs->super))
451                 return 1;
452
453         if (__get_dirent_tail(fs, dirent, NULL, 1) == 0)
454                 return ext2fs_dirent_csum_verify(fs, inum, dirent);
455         if (__get_dx_countlimit(fs, dirent, NULL, NULL, 1) == 0)
456                 return ext2fs_dx_csum_verify(fs, inum, dirent);
457
458         return 0;
459 }
460
461 errcode_t ext2fs_dir_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
462                                     struct ext2_dir_entry *dirent)
463 {
464         if (!ext2fs_has_feature_metadata_csum(fs->super))
465                 return 0;
466
467         if (__get_dirent_tail(fs, dirent, NULL, 1) == 0)
468                 return ext2fs_dirent_csum_set(fs, inum, dirent);
469         if (__get_dx_countlimit(fs, dirent, NULL, NULL, 1) == 0)
470                 return ext2fs_dx_csum_set(fs, inum, dirent);
471
472         if (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)
473                 return 0;
474         return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
475 }
476
477 #define EXT3_EXTENT_TAIL_OFFSET(hdr)    (sizeof(struct ext3_extent_header) + \
478         (sizeof(struct ext3_extent) * ext2fs_le16_to_cpu((hdr)->eh_max)))
479
480 static struct ext3_extent_tail *get_extent_tail(struct ext3_extent_header *h)
481 {
482         return (struct ext3_extent_tail *)(((char *)h) +
483                                            EXT3_EXTENT_TAIL_OFFSET(h));
484 }
485
486 static errcode_t ext2fs_extent_block_csum(ext2_filsys fs, ext2_ino_t inum,
487                                           struct ext3_extent_header *eh,
488                                           __u32 *crc)
489 {
490         int size;
491         __u32 gen;
492         errcode_t retval;
493         struct ext2_inode inode;
494
495         size = EXT3_EXTENT_TAIL_OFFSET(eh) + offsetof(struct ext3_extent_tail,
496                                                       et_checksum);
497
498         retval = ext2fs_read_inode(fs, inum, &inode);
499         if (retval)
500                 return retval;
501         inum = ext2fs_cpu_to_le32(inum);
502         gen = ext2fs_cpu_to_le32(inode.i_generation);
503         *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
504                                 sizeof(inum));
505         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
506         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)eh, size);
507
508         return 0;
509 }
510
511 int ext2fs_extent_block_csum_verify(ext2_filsys fs, ext2_ino_t inum,
512                                     struct ext3_extent_header *eh)
513 {
514         errcode_t retval;
515         __u32 provided, calculated;
516         struct ext3_extent_tail *t = get_extent_tail(eh);
517
518         /*
519          * The extent tree structures are accessed in LE order, so we must
520          * swap the checksum bytes here.
521          */
522         if (!ext2fs_has_feature_metadata_csum(fs->super))
523                 return 1;
524
525         provided = ext2fs_le32_to_cpu(t->et_checksum);
526         retval = ext2fs_extent_block_csum(fs, inum, eh, &calculated);
527         if (retval)
528                 return 0;
529
530         return provided == calculated;
531 }
532
533 errcode_t ext2fs_extent_block_csum_set(ext2_filsys fs, ext2_ino_t inum,
534                                        struct ext3_extent_header *eh)
535 {
536         errcode_t retval;
537         __u32 crc;
538         struct ext3_extent_tail *t = get_extent_tail(eh);
539
540         if (!ext2fs_has_feature_metadata_csum(fs->super))
541                 return 0;
542
543         /*
544          * The extent tree structures are accessed in LE order, so we must
545          * swap the checksum bytes here.
546          */
547         retval = ext2fs_extent_block_csum(fs, inum, eh, &crc);
548         if (retval)
549                 return retval;
550         t->et_checksum = ext2fs_cpu_to_le32(crc);
551         return retval;
552 }
553
554 int ext2fs_inode_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
555                                     char *bitmap, int size)
556 {
557         struct ext4_group_desc *gdp = (struct ext4_group_desc *)
558                         ext2fs_group_desc(fs, fs->group_desc, group);
559         __u32 provided, calculated;
560
561         if (!ext2fs_has_feature_metadata_csum(fs->super))
562                 return 1;
563         provided = gdp->bg_inode_bitmap_csum_lo;
564         calculated = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap,
565                                       size);
566         if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
567                 provided |= (__u32)gdp->bg_inode_bitmap_csum_hi << 16;
568         else
569                 calculated &= 0xFFFF;
570
571         return provided == calculated;
572 }
573
574 errcode_t ext2fs_inode_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
575                                        char *bitmap, int size)
576 {
577         __u32 crc;
578         struct ext4_group_desc *gdp = (struct ext4_group_desc *)
579                         ext2fs_group_desc(fs, fs->group_desc, group);
580
581         if (!ext2fs_has_feature_metadata_csum(fs->super))
582                 return 0;
583
584         crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, size);
585         gdp->bg_inode_bitmap_csum_lo = crc & 0xFFFF;
586         if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END)
587                 gdp->bg_inode_bitmap_csum_hi = crc >> 16;
588
589         return 0;
590 }
591
592 int ext2fs_block_bitmap_csum_verify(ext2_filsys fs, dgrp_t group,
593                                     char *bitmap, int size)
594 {
595         struct ext4_group_desc *gdp = (struct ext4_group_desc *)
596                         ext2fs_group_desc(fs, fs->group_desc, group);
597         __u32 provided, calculated;
598
599         if (!ext2fs_has_feature_metadata_csum(fs->super))
600                 return 1;
601         provided = gdp->bg_block_bitmap_csum_lo;
602         calculated = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap,
603                                       size);
604         if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
605                 provided |= (__u32)gdp->bg_block_bitmap_csum_hi << 16;
606         else
607                 calculated &= 0xFFFF;
608
609         return provided == calculated;
610 }
611
612 errcode_t ext2fs_block_bitmap_csum_set(ext2_filsys fs, dgrp_t group,
613                                        char *bitmap, int size)
614 {
615         __u32 crc;
616         struct ext4_group_desc *gdp = (struct ext4_group_desc *)
617                         ext2fs_group_desc(fs, fs->group_desc, group);
618
619         if (!ext2fs_has_feature_metadata_csum(fs->super))
620                 return 0;
621
622         crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, size);
623         gdp->bg_block_bitmap_csum_lo = crc & 0xFFFF;
624         if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION)
625                 gdp->bg_block_bitmap_csum_hi = crc >> 16;
626
627         return 0;
628 }
629
630 static errcode_t ext2fs_inode_csum(ext2_filsys fs, ext2_ino_t inum,
631                                struct ext2_inode_large *inode,
632                                __u32 *crc, int has_hi)
633 {
634         __u32 gen;
635         struct ext2_inode_large *desc = inode;
636         size_t size = EXT2_INODE_SIZE(fs->super);
637         __u16 old_lo;
638         __u16 old_hi = 0;
639
640         old_lo = inode->i_checksum_lo;
641         inode->i_checksum_lo = 0;
642         if (has_hi) {
643                 old_hi = inode->i_checksum_hi;
644                 inode->i_checksum_hi = 0;
645         }
646
647         inum = ext2fs_cpu_to_le32(inum);
648         gen = inode->i_generation;
649         *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum,
650                                 sizeof(inum));
651         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen));
652         *crc = ext2fs_crc32c_le(*crc, (unsigned char *)desc, size);
653
654         inode->i_checksum_lo = old_lo;
655         if (has_hi)
656                 inode->i_checksum_hi = old_hi;
657         return 0;
658 }
659
660 int ext2fs_inode_csum_verify(ext2_filsys fs, ext2_ino_t inum,
661                              struct ext2_inode_large *inode)
662 {
663         errcode_t retval;
664         __u32 provided, calculated;
665         unsigned int i, has_hi;
666         char *cp;
667
668         if (!ext2fs_has_feature_metadata_csum(fs->super))
669                 return 1;
670
671         has_hi = (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE &&
672                   inode->i_extra_isize >= EXT4_INODE_CSUM_HI_EXTRA_END);
673
674         provided = ext2fs_le16_to_cpu(inode->i_checksum_lo);
675         retval = ext2fs_inode_csum(fs, inum, inode, &calculated, has_hi);
676         if (retval)
677                 return 0;
678         if (has_hi) {
679                 __u32 hi = ext2fs_le16_to_cpu(inode->i_checksum_hi);
680                 provided |= hi << 16;
681         } else
682                 calculated &= 0xFFFF;
683
684         if (provided == calculated)
685                 return 1;
686
687         /*
688          * If the checksum didn't match, it's possible it was due to
689          * the inode being all zero's.  It's unlikely this is the
690          * case, but it can happen.  So check for it here.  (We only
691          * check the base inode since that's good enough, and it's not
692          * worth the bother to figure out how much of the extended
693          * inode, if any, is present.)
694          */
695         for (cp = (char *) inode, i = 0;
696              i < sizeof(struct ext2_inode);
697              cp++, i++)
698                 if (*cp)
699                         return 0;
700         return 1;               /* Inode must have been all zero's */
701 }
702
703 errcode_t ext2fs_inode_csum_set(ext2_filsys fs, ext2_ino_t inum,
704                            struct ext2_inode_large *inode)
705 {
706         errcode_t retval;
707         __u32 crc;
708         int has_hi;
709
710         if (!ext2fs_has_feature_metadata_csum(fs->super))
711                 return 0;
712
713         has_hi = (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE &&
714                   inode->i_extra_isize >= EXT4_INODE_CSUM_HI_EXTRA_END);
715
716         retval = ext2fs_inode_csum(fs, inum, inode, &crc, has_hi);
717         if (retval)
718                 return retval;
719         inode->i_checksum_lo = ext2fs_cpu_to_le16(crc & 0xFFFF);
720         if (has_hi)
721                 inode->i_checksum_hi = ext2fs_cpu_to_le16(crc >> 16);
722         return 0;
723 }
724
725 __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
726 {
727         struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc,
728                                                          group);
729         size_t offset, size = EXT2_DESC_SIZE(fs->super);
730         __u16 crc = 0;
731 #ifdef WORDS_BIGENDIAN
732         struct ext4_group_desc swabdesc;
733         size_t save_size = size;
734         const size_t ext4_bg_size = sizeof(struct ext4_group_desc);
735         struct ext2_group_desc *save_desc = desc;
736
737         /* Have to swab back to little-endian to do the checksum */
738         if (size > ext4_bg_size)
739                 size = ext4_bg_size;
740         memcpy(&swabdesc, desc, size);
741         ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc);
742         desc = (struct ext2_group_desc *) &swabdesc;
743         group = ext2fs_swab32(group);
744 #endif
745
746         if (ext2fs_has_feature_metadata_csum(fs->super)) {
747                 /* new metadata csum code */
748                 __u16 old_crc;
749                 __u32 crc32;
750
751                 old_crc = desc->bg_checksum;
752                 desc->bg_checksum = 0;
753                 crc32 = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&group,
754                                          sizeof(group));
755                 crc32 = ext2fs_crc32c_le(crc32, (unsigned char *)desc,
756                                          size);
757                 desc->bg_checksum = old_crc;
758 #ifdef WORDS_BIGENDIAN
759                 if (save_size > ext4_bg_size)
760                         crc32 = ext2fs_crc32c_le(crc32,
761                                      (unsigned char *)save_desc + ext4_bg_size,
762                                      save_size - ext4_bg_size);
763 #endif
764                 crc = crc32 & 0xFFFF;
765                 goto out;
766         }
767
768         /* old crc16 code */
769         offset = offsetof(struct ext2_group_desc, bg_checksum);
770         crc = ext2fs_crc16(~0, fs->super->s_uuid,
771                            sizeof(fs->super->s_uuid));
772         crc = ext2fs_crc16(crc, &group, sizeof(group));
773         crc = ext2fs_crc16(crc, desc, offset);
774         offset += sizeof(desc->bg_checksum); /* skip checksum */
775         /* for checksum of struct ext4_group_desc do the rest...*/
776         if (offset < size) {
777                 crc = ext2fs_crc16(crc, (char *)desc + offset,
778                                    size - offset);
779         }
780 #ifdef WORDS_BIGENDIAN
781         /*
782          * If the size of the bg descriptor is greater than 64
783          * bytes, which is the size of the traditional ext4 bg
784          * descriptor, checksum the rest of the descriptor here
785          */
786         if (save_size > ext4_bg_size)
787                 crc = ext2fs_crc16(crc, (char *)save_desc + ext4_bg_size,
788                                    save_size - ext4_bg_size);
789 #endif
790
791 out:
792         return crc;
793 }
794
795 int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
796 {
797         if (ext2fs_has_group_desc_csum(fs) &&
798             (ext2fs_bg_checksum(fs, group) !=
799              ext2fs_group_desc_csum(fs, group)))
800                 return 0;
801
802         return 1;
803 }
804
805 void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
806 {
807         if (!ext2fs_has_group_desc_csum(fs))
808                 return;
809
810         /* ext2fs_bg_checksum_set() sets the actual checksum field but
811          * does not calculate the checksum itself. */
812         ext2fs_bg_checksum_set(fs, group, ext2fs_group_desc_csum(fs, group));
813 }
814
815 static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
816                                    __u32 inodes_per_grp, dgrp_t grp_no)
817 {
818         ext2_ino_t i, start_ino, end_ino;
819
820         start_ino = grp_no * inodes_per_grp + 1;
821         end_ino = start_ino + inodes_per_grp - 1;
822
823         for (i = end_ino; i >= start_ino; i--) {
824                 if (ext2fs_fast_test_inode_bitmap2(bitmap, i))
825                         return i - start_ino + 1;
826         }
827         return inodes_per_grp;
828 }
829
830 /* update the bitmap flags, set the itable high watermark, and calculate
831  * checksums for the group descriptors */
832 errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
833 {
834         struct ext2_super_block *sb = fs->super;
835         int dirty = 0;
836         dgrp_t i;
837
838         if (!fs->inode_map)
839                 return EXT2_ET_NO_INODE_BITMAP;
840
841         if (!ext2fs_has_group_desc_csum(fs))
842                 return 0;
843
844         for (i = 0; i < fs->group_desc_count; i++) {
845                 __u32 old_csum = ext2fs_bg_checksum(fs, i);
846                 __u32 old_unused = ext2fs_bg_itable_unused(fs, i);
847                 __u32 old_flags = ext2fs_bg_flags(fs, i);
848                 __u32 old_free_inodes_count = ext2fs_bg_free_inodes_count(fs, i);
849                 __u32 old_free_blocks_count = ext2fs_bg_free_blocks_count(fs, i);
850
851                 if (old_free_blocks_count == sb->s_blocks_per_group &&
852                     i != fs->group_desc_count - 1)
853                         ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT);
854
855                 if (old_free_inodes_count == sb->s_inodes_per_group) {
856                         ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
857                         ext2fs_bg_itable_unused_set(fs, i, sb->s_inodes_per_group);
858                 } else {
859                         int unused =
860                                 sb->s_inodes_per_group -
861                                 find_last_inode_ingrp(fs->inode_map,
862                                                       sb->s_inodes_per_group, i);
863
864                         ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
865                         ext2fs_bg_itable_unused_set(fs, i, unused);
866                 }
867
868                 ext2fs_group_desc_csum_set(fs, i);
869                 if (old_flags != ext2fs_bg_flags(fs, i))
870                         dirty = 1;
871                 if (old_unused != ext2fs_bg_itable_unused(fs, i))
872                         dirty = 1;
873                 if (old_csum != ext2fs_bg_checksum(fs, i))
874                         dirty = 1;
875         }
876         if (dirty)
877                 ext2fs_mark_super_dirty(fs);
878         return 0;
879 }
880
881 #ifdef DEBUG
882 #include "e2p/e2p.h"
883
884 void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
885 {
886         __u16 crc1, crc2, crc3;
887         dgrp_t swabgroup;
888         struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc,
889                                                          group);
890         size_t size = EXT2_DESC_SIZE(fs->super);
891         struct ext2_super_block *sb = fs->super;
892         int offset = offsetof(struct ext2_group_desc, bg_checksum);
893 #ifdef WORDS_BIGENDIAN
894         struct ext4_group_desc swabdesc;
895         struct ext2_group_desc *save_desc = desc;
896         const size_t ext4_bg_size = sizeof(struct ext4_group_desc);
897         size_t save_size = size;
898 #endif
899
900 #ifdef WORDS_BIGENDIAN
901         /* Have to swab back to little-endian to do the checksum */
902         if (size > ext4_bg_size)
903                 size = ext4_bg_size;
904         memcpy(&swabdesc, desc, size);
905         ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc);
906         desc = (struct ext2_group_desc *) &swabdesc;
907
908         swabgroup = ext2fs_swab32(group);
909 #else
910         swabgroup = group;
911 #endif
912
913         crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
914         crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
915         crc3 = ext2fs_crc16(crc2, desc, offset);
916         offset += sizeof(desc->bg_checksum); /* skip checksum */
917         /* for checksum of struct ext4_group_desc do the rest...*/
918         if (offset < size)
919                 crc3 = ext2fs_crc16(crc3, (char *)desc + offset, size - offset);
920 #ifdef WORDS_BIGENDIAN
921         if (save_size > ext4_bg_size)
922                 crc3 = ext2fs_crc16(crc3, (char *)save_desc + ext4_bg_size,
923                                     save_size - ext4_bg_size);
924 #endif
925
926         printf("%s UUID %s=%04x, grp %u=%04x: %04x=%04x\n",
927                msg, e2p_uuid2str(sb->s_uuid), crc1, group, crc2, crc3,
928                ext2fs_group_desc_csum(fs, group));
929 }
930
931 unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23,
932                               0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb };
933
934 int main(int argc, char **argv)
935 {
936         struct ext2_super_block param;
937         errcode_t               retval;
938         ext2_filsys             fs;
939         int                     i;
940         __u16 csum1, csum2, csum_known = 0xd3a4;
941
942         memset(&param, 0, sizeof(param));
943         ext2fs_blocks_count_set(&param, 32768);
944 #if 0
945         param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
946         param.s_desc_size = 128;
947         csum_known = 0x5b6e;
948 #endif
949
950         retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
951                                    test_io_manager, &fs);
952         if (retval) {
953                 com_err("setup", retval,
954                         "While initializing filesystem");
955                 exit(1);
956         }
957         memcpy(fs->super->s_uuid, sb_uuid, 16);
958         fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
959
960         for (i=0; i < fs->group_desc_count; i++) {
961                 ext2fs_block_bitmap_loc_set(fs, i, 124);
962                 ext2fs_inode_bitmap_loc_set(fs, i, 125);
963                 ext2fs_inode_table_loc_set(fs, i, 126);
964                 ext2fs_bg_free_blocks_count_set(fs, i, 31119);
965                 ext2fs_bg_free_inodes_count_set(fs, i, 15701);
966                 ext2fs_bg_used_dirs_count_set(fs, i, 2);
967                 ext2fs_bg_flags_zap(fs, i);
968         };
969
970         csum1 = ext2fs_group_desc_csum(fs, 0);
971         print_csum("csum0000", fs, 0);
972
973         if (csum1 != csum_known) {
974                 printf("checksum for group 0 should be %04x\n", csum_known);
975                 exit(1);
976         }
977         csum2 = ext2fs_group_desc_csum(fs, 1);
978         print_csum("csum0001", fs, 1);
979         if (csum1 == csum2) {
980                 printf("checksums for different groups shouldn't match\n");
981                 exit(1);
982         }
983         csum2 = ext2fs_group_desc_csum(fs, 2);
984         print_csum("csumffff", fs, 2);
985         if (csum1 == csum2) {
986                 printf("checksums for different groups shouldn't match\n");
987                 exit(1);
988         }
989         ext2fs_bg_checksum_set(fs, 0, csum1);
990         csum2 = ext2fs_group_desc_csum(fs, 0);
991         print_csum("csum_set", fs, 0);
992         if (csum1 != csum2) {
993                 printf("checksums should not depend on checksum field\n");
994                 exit(1);
995         }
996         if (!ext2fs_group_desc_csum_verify(fs, 0)) {
997                 printf("checksums should verify against gd_checksum\n");
998                 exit(1);
999         }
1000         memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid));
1001         print_csum("new_uuid", fs, 0);
1002         if (ext2fs_group_desc_csum_verify(fs, 0) != 0) {
1003                 printf("checksums for different filesystems shouldn't match\n");
1004                 exit(1);
1005         }
1006         csum1 = ext2fs_group_desc_csum(fs, 0);
1007         ext2fs_bg_checksum_set(fs, 0, csum1);
1008         print_csum("csum_new", fs, 0);
1009         ext2fs_bg_free_blocks_count_set(fs, 0, 1);
1010         csum2 = ext2fs_group_desc_csum(fs, 0);
1011         print_csum("csum_blk", fs, 0);
1012         if (csum1 == csum2) {
1013                 printf("checksums for different data shouldn't match\n");
1014                 exit(1);
1015         }
1016         ext2fs_free(fs);
1017
1018         return 0;
1019 }
1020 #endif