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