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