Whamcloud - gitweb
AOSP: Suppress warnings/errors for clang+mingw
[tools/e2fsprogs.git] / lib / ext2fs / swapfs.c
1 /*
2  * swapfs.c --- swap ext2 filesystem data structures
3  *
4  * Copyright (C) 1995, 1996, 2002 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <string.h>
18 #include <time.h>
19
20 #include "ext2_fs.h"
21 #include "ext2fs.h"
22 #include "ext2fsP.h"
23 #include <ext2fs/ext2_ext_attr.h>
24
25 #ifdef WORDS_BIGENDIAN
26 void ext2fs_swap_super(struct ext2_super_block * sb)
27 {
28         int i;
29
30         sb->s_inodes_count = ext2fs_swab32(sb->s_inodes_count);
31         sb->s_blocks_count = ext2fs_swab32(sb->s_blocks_count);
32         sb->s_r_blocks_count = ext2fs_swab32(sb->s_r_blocks_count);
33         sb->s_free_blocks_count = ext2fs_swab32(sb->s_free_blocks_count);
34         sb->s_free_inodes_count = ext2fs_swab32(sb->s_free_inodes_count);
35         sb->s_first_data_block = ext2fs_swab32(sb->s_first_data_block);
36         sb->s_log_block_size = ext2fs_swab32(sb->s_log_block_size);
37         sb->s_log_cluster_size = ext2fs_swab32(sb->s_log_cluster_size);
38         sb->s_blocks_per_group = ext2fs_swab32(sb->s_blocks_per_group);
39         sb->s_clusters_per_group = ext2fs_swab32(sb->s_clusters_per_group);
40         sb->s_inodes_per_group = ext2fs_swab32(sb->s_inodes_per_group);
41         sb->s_mtime = ext2fs_swab32(sb->s_mtime);
42         sb->s_wtime = ext2fs_swab32(sb->s_wtime);
43         sb->s_mnt_count = ext2fs_swab16(sb->s_mnt_count);
44         sb->s_max_mnt_count = ext2fs_swab16(sb->s_max_mnt_count);
45         sb->s_magic = ext2fs_swab16(sb->s_magic);
46         sb->s_state = ext2fs_swab16(sb->s_state);
47         sb->s_errors = ext2fs_swab16(sb->s_errors);
48         sb->s_minor_rev_level = ext2fs_swab16(sb->s_minor_rev_level);
49         sb->s_lastcheck = ext2fs_swab32(sb->s_lastcheck);
50         sb->s_checkinterval = ext2fs_swab32(sb->s_checkinterval);
51         sb->s_creator_os = ext2fs_swab32(sb->s_creator_os);
52         sb->s_rev_level = ext2fs_swab32(sb->s_rev_level);
53         sb->s_def_resuid = ext2fs_swab16(sb->s_def_resuid);
54         sb->s_def_resgid = ext2fs_swab16(sb->s_def_resgid);
55         sb->s_first_ino = ext2fs_swab32(sb->s_first_ino);
56         sb->s_inode_size = ext2fs_swab16(sb->s_inode_size);
57         sb->s_block_group_nr = ext2fs_swab16(sb->s_block_group_nr);
58         sb->s_feature_compat = ext2fs_swab32(sb->s_feature_compat);
59         sb->s_feature_incompat = ext2fs_swab32(sb->s_feature_incompat);
60         sb->s_feature_ro_compat = ext2fs_swab32(sb->s_feature_ro_compat);
61         /* sb->s_uuid is __u8 and does not need swabbing */
62         /* sb->s_volume_name is char and does not need swabbing */
63         /* sb->s_last_mounted is char and does not need swabbing */
64         sb->s_algorithm_usage_bitmap = ext2fs_swab32(sb->s_algorithm_usage_bitmap);
65         /* sb->s_prealloc_blocks is __u8 and does not need swabbing */
66         /* sb->s_prealloc_dir_blocks is __u8 and does not need swabbing */
67         sb->s_reserved_gdt_blocks = ext2fs_swab16(sb->s_reserved_gdt_blocks);
68         /* sb->s_journal_uuid is __u8 and does not need swabbing */
69         sb->s_journal_inum = ext2fs_swab32(sb->s_journal_inum);
70         sb->s_journal_dev = ext2fs_swab32(sb->s_journal_dev);
71         sb->s_last_orphan = ext2fs_swab32(sb->s_last_orphan);
72         for (i = 0; i < 4; i++)
73                 sb->s_hash_seed[i] = ext2fs_swab32(sb->s_hash_seed[i]);
74         /* sb->s_def_hash_version is __u8 and does not need swabbing */
75         /* sb->s_jnl_backup_type is __u8 and does not need swabbing */
76         sb->s_desc_size = ext2fs_swab16(sb->s_desc_size);
77         sb->s_default_mount_opts = ext2fs_swab32(sb->s_default_mount_opts);
78         sb->s_first_meta_bg = ext2fs_swab32(sb->s_first_meta_bg);
79         sb->s_mkfs_time = ext2fs_swab32(sb->s_mkfs_time);
80         /* if journal backup is for a valid extent-based journal... */
81         if (ext2fs_extent_header_verify(sb->s_jnl_blocks,
82                                         sizeof(sb->s_jnl_blocks)) == 0) {
83                 /* ... swap only the journal i_size and i_size_high,
84                  * and the extent data is not swapped on read */
85                 i = 15;
86         } else {
87                 /* direct/indirect journal: swap it all */
88                 i = 0;
89         }
90         for (; i < 17; i++)
91                 sb->s_jnl_blocks[i] = ext2fs_swab32(sb->s_jnl_blocks[i]);
92         sb->s_blocks_count_hi = ext2fs_swab32(sb->s_blocks_count_hi);
93         sb->s_r_blocks_count_hi = ext2fs_swab32(sb->s_r_blocks_count_hi);
94         sb->s_free_blocks_hi = ext2fs_swab32(sb->s_free_blocks_hi);
95         sb->s_min_extra_isize = ext2fs_swab16(sb->s_min_extra_isize);
96         sb->s_want_extra_isize = ext2fs_swab16(sb->s_want_extra_isize);
97         sb->s_flags = ext2fs_swab32(sb->s_flags);
98         sb->s_raid_stride = ext2fs_swab16(sb->s_raid_stride);
99         sb->s_mmp_update_interval = ext2fs_swab16(sb->s_mmp_update_interval);
100         sb->s_mmp_block = ext2fs_swab64(sb->s_mmp_block);
101         sb->s_raid_stripe_width = ext2fs_swab32(sb->s_raid_stripe_width);
102         /* sb->s_log_groups_per_flex is __u8 and does not need swabbing */
103         /* sb->s_checksum_type is __u8 and does not need swabbing */
104         /* sb->s_encryption_level is __u8 and does not need swabbing */
105         /* sb->s_reserved_pad is __u8 and does not need swabbing */
106         sb->s_kbytes_written = ext2fs_swab64(sb->s_kbytes_written);
107         sb->s_snapshot_inum = ext2fs_swab32(sb->s_snapshot_inum);
108         sb->s_snapshot_id = ext2fs_swab32(sb->s_snapshot_id);
109         sb->s_snapshot_r_blocks_count =
110                 ext2fs_swab64(sb->s_snapshot_r_blocks_count);
111         sb->s_snapshot_list = ext2fs_swab32(sb->s_snapshot_list);
112         sb->s_error_count = ext2fs_swab32(sb->s_error_count);
113         sb->s_first_error_time = ext2fs_swab32(sb->s_first_error_time);
114         sb->s_first_error_ino = ext2fs_swab32(sb->s_first_error_ino);
115         sb->s_first_error_block = ext2fs_swab64(sb->s_first_error_block);
116         /* sb->s_first_error_func is __u8 and does not need swabbing */
117         sb->s_last_error_time = ext2fs_swab32(sb->s_last_error_time);
118         sb->s_last_error_ino = ext2fs_swab32(sb->s_last_error_ino);
119         sb->s_last_error_block = ext2fs_swab64(sb->s_last_error_block);
120         /* sb->s_last_error_func is __u8 and does not need swabbing */
121         /* sb->s_mount_opts is __u8 and does not need swabbing */
122         sb->s_usr_quota_inum = ext2fs_swab32(sb->s_usr_quota_inum);
123         sb->s_grp_quota_inum = ext2fs_swab32(sb->s_grp_quota_inum);
124         sb->s_overhead_blocks = ext2fs_swab32(sb->s_overhead_blocks);
125         sb->s_backup_bgs[0] = ext2fs_swab32(sb->s_backup_bgs[0]);
126         sb->s_backup_bgs[1] = ext2fs_swab32(sb->s_backup_bgs[1]);
127         /* sb->s_encrypt_algos is __u8 and does not need swabbing */
128         /* sb->s_encrypt_pw_salt is __u8 and does not need swabbing */
129         sb->s_lpf_ino = ext2fs_swab32(sb->s_lpf_ino);
130         sb->s_prj_quota_inum = ext2fs_swab32(sb->s_prj_quota_inum);
131         sb->s_checksum_seed = ext2fs_swab32(sb->s_checksum_seed);
132         /* catch when new fields are used from s_reserved */
133         EXT2FS_BUILD_BUG_ON(sizeof(sb->s_reserved) != 98 * sizeof(__le32));
134         sb->s_checksum = ext2fs_swab32(sb->s_checksum);
135 }
136
137 void ext2fs_swap_group_desc2(ext2_filsys fs, struct ext2_group_desc *gdp)
138 {
139         struct ext4_group_desc *gdp4 = (struct ext4_group_desc *)gdp;
140
141         /* Do the 32-bit parts first */
142         gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
143         gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
144         gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
145         gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
146         gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
147         gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
148         gdp->bg_flags = ext2fs_swab16(gdp->bg_flags);
149         gdp->bg_exclude_bitmap_lo = ext2fs_swab32(gdp->bg_exclude_bitmap_lo);
150         gdp->bg_block_bitmap_csum_lo =
151                 ext2fs_swab16(gdp->bg_block_bitmap_csum_lo);
152         gdp->bg_inode_bitmap_csum_lo =
153                 ext2fs_swab16(gdp->bg_inode_bitmap_csum_lo);
154         gdp->bg_itable_unused = ext2fs_swab16(gdp->bg_itable_unused);
155         gdp->bg_checksum = ext2fs_swab16(gdp->bg_checksum);
156         /* If we're 32-bit, we're done */
157         if (fs == NULL || EXT2_DESC_SIZE(fs->super) < EXT2_MIN_DESC_SIZE_64BIT)
158                 return;
159
160         /* Swap the 64-bit parts */
161         gdp4->bg_block_bitmap_hi = ext2fs_swab32(gdp4->bg_block_bitmap_hi);
162         gdp4->bg_inode_bitmap_hi = ext2fs_swab32(gdp4->bg_inode_bitmap_hi);
163         gdp4->bg_inode_table_hi = ext2fs_swab32(gdp4->bg_inode_table_hi);
164         gdp4->bg_free_blocks_count_hi =
165                 ext2fs_swab16(gdp4->bg_free_blocks_count_hi);
166         gdp4->bg_free_inodes_count_hi =
167                 ext2fs_swab16(gdp4->bg_free_inodes_count_hi);
168         gdp4->bg_used_dirs_count_hi =
169                 ext2fs_swab16(gdp4->bg_used_dirs_count_hi);
170         gdp4->bg_itable_unused_hi = ext2fs_swab16(gdp4->bg_itable_unused_hi);
171         gdp4->bg_exclude_bitmap_hi = ext2fs_swab16(gdp4->bg_exclude_bitmap_hi);
172         gdp4->bg_block_bitmap_csum_hi =
173                 ext2fs_swab16(gdp4->bg_block_bitmap_csum_hi);
174         gdp4->bg_inode_bitmap_csum_hi =
175                 ext2fs_swab16(gdp4->bg_inode_bitmap_csum_hi);
176         EXT2FS_BUILD_BUG_ON(sizeof(gdp4->bg_reserved) != sizeof(__u32));
177 }
178
179 void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
180 {
181         ext2fs_swap_group_desc2(0, gdp);
182 }
183
184
185 void ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header *to_header,
186                                  struct ext2_ext_attr_header *from_header)
187 {
188         int n;
189
190         to_header->h_magic    = ext2fs_swab32(from_header->h_magic);
191         to_header->h_blocks   = ext2fs_swab32(from_header->h_blocks);
192         to_header->h_refcount = ext2fs_swab32(from_header->h_refcount);
193         to_header->h_hash     = ext2fs_swab32(from_header->h_hash);
194         to_header->h_checksum = ext2fs_swab32(from_header->h_checksum);
195         for (n = 0; n < 3; n++)
196                 to_header->h_reserved[n] =
197                         ext2fs_swab32(from_header->h_reserved[n]);
198 }
199
200 void ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry *to_entry,
201                                 struct ext2_ext_attr_entry *from_entry)
202 {
203         to_entry->e_value_offs  = ext2fs_swab16(from_entry->e_value_offs);
204         to_entry->e_value_inum  = ext2fs_swab32(from_entry->e_value_inum);
205         to_entry->e_value_size  = ext2fs_swab32(from_entry->e_value_size);
206         to_entry->e_hash        = ext2fs_swab32(from_entry->e_hash);
207 }
208
209 void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, int has_header)
210 {
211         struct ext2_ext_attr_header *from_header =
212                 (struct ext2_ext_attr_header *)from;
213         struct ext2_ext_attr_header *to_header =
214                 (struct ext2_ext_attr_header *)to;
215         struct ext2_ext_attr_entry *from_entry, *to_entry;
216         char *from_end = (char *)from_header + bufsize;
217
218         if (to_header != from_header)
219                 memcpy(to_header, from_header, bufsize);
220
221         if (has_header) {
222                 ext2fs_swap_ext_attr_header(to_header, from_header);
223
224                 from_entry = (struct ext2_ext_attr_entry *)(from_header+1);
225                 to_entry   = (struct ext2_ext_attr_entry *)(to_header+1);
226         } else {
227                 from_entry = (struct ext2_ext_attr_entry *)from_header;
228                 to_entry   = (struct ext2_ext_attr_entry *)to_header;
229         }
230
231         while ((char *)from_entry < from_end &&
232                (char *)EXT2_EXT_ATTR_NEXT(from_entry) <= from_end &&
233                *(__u32 *)from_entry) {
234                 ext2fs_swap_ext_attr_entry(to_entry, from_entry);
235                 from_entry = EXT2_EXT_ATTR_NEXT(from_entry);
236                 to_entry   = EXT2_EXT_ATTR_NEXT(to_entry);
237         }
238 }
239
240 void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
241                             struct ext2_inode_large *f, int hostorder,
242                             int bufsize)
243 {
244         unsigned i, extra_isize, attr_magic;
245         int has_extents, has_inline_data, islnk, fast_symlink;
246         int inode_size;
247         __u32 *eaf, *eat;
248
249         /*
250          * Note that t and f may point to the same address. That's why
251          * if (hostorder) condition is executed before swab calls and
252          * if (!hostorder) afterwards.
253          */
254         if (hostorder) {
255                 islnk = LINUX_S_ISLNK(f->i_mode);
256                 fast_symlink = ext2fs_is_fast_symlink(EXT2_INODE(f));
257                 has_extents = (f->i_flags & EXT4_EXTENTS_FL) != 0;
258                 has_inline_data = (f->i_flags & EXT4_INLINE_DATA_FL) != 0;
259         }
260
261         t->i_mode = ext2fs_swab16(f->i_mode);
262         t->i_uid = ext2fs_swab16(f->i_uid);
263         t->i_size = ext2fs_swab32(f->i_size);
264         t->i_atime = ext2fs_swab32(f->i_atime);
265         t->i_ctime = ext2fs_swab32(f->i_ctime);
266         t->i_mtime = ext2fs_swab32(f->i_mtime);
267         t->i_dtime = ext2fs_swab32(f->i_dtime);
268         t->i_gid = ext2fs_swab16(f->i_gid);
269         t->i_links_count = ext2fs_swab16(f->i_links_count);
270         t->i_file_acl = ext2fs_swab32(f->i_file_acl);
271         t->i_blocks = ext2fs_swab32(f->i_blocks);
272         t->i_flags = ext2fs_swab32(f->i_flags);
273         t->i_size_high = ext2fs_swab32(f->i_size_high);
274
275         if (!hostorder) {
276                 islnk = LINUX_S_ISLNK(t->i_mode);
277                 fast_symlink = ext2fs_is_fast_symlink(EXT2_INODE(t));
278                 has_extents = (t->i_flags & EXT4_EXTENTS_FL) != 0;
279                 has_inline_data = (t->i_flags & EXT4_INLINE_DATA_FL) != 0;
280         }
281
282         /*
283          * Extent data and inline data are swapped on access, not here
284          */
285         if (!has_extents && !has_inline_data && (!islnk || !fast_symlink)) {
286                 for (i = 0; i < EXT2_N_BLOCKS; i++)
287                         t->i_block[i] = ext2fs_swab32(f->i_block[i]);
288         } else if (t != f) {
289                 for (i = 0; i < EXT2_N_BLOCKS; i++)
290                         t->i_block[i] = f->i_block[i];
291         }
292         t->i_generation = ext2fs_swab32(f->i_generation);
293         t->i_faddr = ext2fs_swab32(f->i_faddr);
294
295         switch (fs->super->s_creator_os) {
296         case EXT2_OS_LINUX:
297                 t->osd1.linux1.l_i_version =
298                         ext2fs_swab32(f->osd1.linux1.l_i_version);
299                 t->osd2.linux2.l_i_blocks_hi =
300                         ext2fs_swab16(f->osd2.linux2.l_i_blocks_hi);
301                 t->osd2.linux2.l_i_file_acl_high =
302                         ext2fs_swab16(f->osd2.linux2.l_i_file_acl_high);
303                 t->osd2.linux2.l_i_uid_high =
304                   ext2fs_swab16 (f->osd2.linux2.l_i_uid_high);
305                 t->osd2.linux2.l_i_gid_high =
306                   ext2fs_swab16 (f->osd2.linux2.l_i_gid_high);
307                 t->osd2.linux2.l_i_checksum_lo =
308                         ext2fs_swab16(f->osd2.linux2.l_i_checksum_lo);
309                 break;
310         case EXT2_OS_HURD:
311                 t->osd1.hurd1.h_i_translator =
312                   ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
313                 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
314                 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
315                 t->osd2.hurd2.h_i_mode_high =
316                   ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
317                 t->osd2.hurd2.h_i_uid_high =
318                   ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
319                 t->osd2.hurd2.h_i_gid_high =
320                   ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
321                 t->osd2.hurd2.h_i_author =
322                   ext2fs_swab32 (f->osd2.hurd2.h_i_author);
323                 break;
324         default:
325                 break;
326         }
327
328         if (bufsize < (int) (sizeof(struct ext2_inode) + sizeof(__u16)))
329                 return; /* no i_extra_isize field */
330
331         if (hostorder)
332                 extra_isize = f->i_extra_isize;
333         t->i_extra_isize = ext2fs_swab16(f->i_extra_isize);
334         if (!hostorder)
335                 extra_isize = t->i_extra_isize;
336         if (extra_isize > EXT2_INODE_SIZE(fs->super) -
337                                 sizeof(struct ext2_inode)) {
338                 /* this is error case: i_extra_size is too large */
339                 return;
340         }
341         if (extra_isize & 3)
342                 return;         /* Illegal inode extra_isize */
343
344         inode_size = EXT2_GOOD_OLD_INODE_SIZE + extra_isize;
345         if (inode_includes(inode_size, i_checksum_hi))
346                 t->i_checksum_hi = ext2fs_swab16(f->i_checksum_hi);
347         if (inode_includes(inode_size, i_ctime_extra))
348                 t->i_ctime_extra = ext2fs_swab32(f->i_ctime_extra);
349         if (inode_includes(inode_size, i_mtime_extra))
350                 t->i_mtime_extra = ext2fs_swab32(f->i_mtime_extra);
351         if (inode_includes(inode_size, i_atime_extra))
352                 t->i_atime_extra = ext2fs_swab32(f->i_atime_extra);
353         if (inode_includes(inode_size, i_crtime))
354                 t->i_crtime = ext2fs_swab32(f->i_crtime);
355         if (inode_includes(inode_size, i_crtime_extra))
356                 t->i_crtime_extra = ext2fs_swab32(f->i_crtime_extra);
357         if (inode_includes(inode_size, i_version_hi))
358                 t->i_version_hi = ext2fs_swab32(f->i_version_hi);
359         if (inode_includes(inode_size, i_projid))
360                 t->i_projid = ext2fs_swab16(f->i_projid);
361         /* catch new static fields added after i_projid */
362         EXT2FS_BUILD_BUG_ON(sizeof(struct ext2_inode_large) != 160);
363
364         i = sizeof(struct ext2_inode) + extra_isize + sizeof(__u32);
365         if (bufsize < (int) i)
366                 return; /* no space for EA magic */
367
368         eaf = (__u32 *) (((char *) f) + sizeof(struct ext2_inode) +
369                                         extra_isize);
370
371         attr_magic = *eaf;
372         if (!hostorder)
373                 attr_magic = ext2fs_swab32(attr_magic);
374
375         if (attr_magic != EXT2_EXT_ATTR_MAGIC)
376                 return; /* it seems no magic here */
377
378         eat = (__u32 *) (((char *) t) + sizeof(struct ext2_inode) +
379                                         extra_isize);
380         *eat = ext2fs_swab32(*eaf);
381
382         /* convert EA(s) */
383         ext2fs_swap_ext_attr((char *) (eat + 1), (char *) (eaf + 1),
384                              bufsize - sizeof(struct ext2_inode) -
385                              extra_isize - sizeof(__u32), 0);
386
387 }
388
389 void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
390                        struct ext2_inode *f, int hostorder)
391 {
392         ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) t,
393                                 (struct ext2_inode_large *) f, hostorder,
394                                 sizeof(struct ext2_inode));
395 }
396
397 void ext2fs_swap_mmp(struct mmp_struct *mmp)
398 {
399         mmp->mmp_magic = ext2fs_swab32(mmp->mmp_magic);
400         mmp->mmp_seq = ext2fs_swab32(mmp->mmp_seq);
401         mmp->mmp_time = ext2fs_swab64(mmp->mmp_time);
402         mmp->mmp_check_interval = ext2fs_swab16(mmp->mmp_check_interval);
403         mmp->mmp_checksum = ext2fs_swab32(mmp->mmp_checksum);
404 }
405
406 errcode_t ext2fs_dirent_swab_in(ext2_filsys fs, char *buf, int flags)
407 {
408         return ext2fs_dirent_swab_in2(fs, buf, fs->blocksize, flags);
409 }
410
411 errcode_t ext2fs_dirent_swab_in2(ext2_filsys fs, char *buf,
412                                  size_t size, int flags)
413 {
414         errcode_t       retval;
415         char            *p, *end;
416         struct ext2_dir_entry *dirent;
417         unsigned int    name_len, rec_len;
418
419         p = (char *) buf;
420         end = (char *) buf + size;
421         while (p < end-8) {
422                 dirent = (struct ext2_dir_entry *) p;
423                 dirent->inode = ext2fs_swab32(dirent->inode);
424                 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
425                 dirent->name_len = ext2fs_swab16(dirent->name_len);
426                 name_len = dirent->name_len;
427                 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
428                         dirent->name_len = ext2fs_swab16(dirent->name_len);
429                 retval = ext2fs_get_rec_len(fs, dirent, &rec_len);
430                 if (retval)
431                         return retval;
432                 if ((rec_len < 8) || (rec_len % 4)) {
433                         rec_len = 8;
434                         retval = EXT2_ET_DIR_CORRUPTED;
435                 } else if (((name_len & 0xFF) + 8) > rec_len)
436                         retval = EXT2_ET_DIR_CORRUPTED;
437                 p += rec_len;
438         }
439
440         return 0;
441 }
442
443 errcode_t ext2fs_dirent_swab_out(ext2_filsys fs, char *buf, int flags)
444 {
445         return ext2fs_dirent_swab_out2(fs, buf, fs->blocksize, flags);
446 }
447
448 errcode_t ext2fs_dirent_swab_out2(ext2_filsys fs, char *buf,
449                                   size_t size, int flags)
450 {
451         errcode_t       retval;
452         char            *p, *end;
453         unsigned int    rec_len;
454         struct ext2_dir_entry *dirent;
455
456         p = buf;
457         end = buf + size;
458         while (p < end) {
459                 dirent = (struct ext2_dir_entry *) p;
460                 retval = ext2fs_get_rec_len(fs, dirent, &rec_len);
461                 if (retval)
462                         return retval;
463                 if ((rec_len < 8) ||
464                     (rec_len % 4)) {
465                         ext2fs_free_mem(&buf);
466                         return EXT2_ET_DIR_CORRUPTED;
467                 }
468                 p += rec_len;
469                 dirent->inode = ext2fs_swab32(dirent->inode);
470                 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
471                 dirent->name_len = ext2fs_swab16(dirent->name_len);
472
473                 if (flags & EXT2_DIRBLOCK_V2_STRUCT)
474                         dirent->name_len = ext2fs_swab16(dirent->name_len);
475         }
476
477         return 0;
478 }
479
480 #endif