• 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 "ext2fsP.h"
23 #include <ext2fs/ext2_ext_attr.h>
24 
25 #ifdef WORDS_BIGENDIAN
ext2fs_swap_super(struct ext2_super_block * sb)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 	/* s_*_time_hi are __u8 and does not need swabbing */
133 	sb->s_encoding = ext2fs_swab16(sb->s_encoding);
134 	sb->s_encoding_flags = ext2fs_swab16(sb->s_encoding_flags);
135 	/* catch when new fields are used from s_reserved */
136 	EXT2FS_BUILD_BUG_ON(sizeof(sb->s_reserved) != 95 * sizeof(__le32));
137 	sb->s_checksum = ext2fs_swab32(sb->s_checksum);
138 }
139 
ext2fs_swap_group_desc2(ext2_filsys fs,struct ext2_group_desc * gdp)140 void ext2fs_swap_group_desc2(ext2_filsys fs, struct ext2_group_desc *gdp)
141 {
142 	struct ext4_group_desc *gdp4 = (struct ext4_group_desc *)gdp;
143 
144 	/* Do the 32-bit parts first */
145 	gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
146 	gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
147 	gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
148 	gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
149 	gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
150 	gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
151 	gdp->bg_flags = ext2fs_swab16(gdp->bg_flags);
152 	gdp->bg_exclude_bitmap_lo = ext2fs_swab32(gdp->bg_exclude_bitmap_lo);
153 	gdp->bg_block_bitmap_csum_lo =
154 		ext2fs_swab16(gdp->bg_block_bitmap_csum_lo);
155 	gdp->bg_inode_bitmap_csum_lo =
156 		ext2fs_swab16(gdp->bg_inode_bitmap_csum_lo);
157 	gdp->bg_itable_unused = ext2fs_swab16(gdp->bg_itable_unused);
158 	gdp->bg_checksum = ext2fs_swab16(gdp->bg_checksum);
159 	/* If we're 32-bit, we're done */
160 	if (fs == NULL || EXT2_DESC_SIZE(fs->super) < EXT2_MIN_DESC_SIZE_64BIT)
161 		return;
162 
163 	/* Swap the 64-bit parts */
164 	gdp4->bg_block_bitmap_hi = ext2fs_swab32(gdp4->bg_block_bitmap_hi);
165 	gdp4->bg_inode_bitmap_hi = ext2fs_swab32(gdp4->bg_inode_bitmap_hi);
166 	gdp4->bg_inode_table_hi = ext2fs_swab32(gdp4->bg_inode_table_hi);
167 	gdp4->bg_free_blocks_count_hi =
168 		ext2fs_swab16(gdp4->bg_free_blocks_count_hi);
169 	gdp4->bg_free_inodes_count_hi =
170 		ext2fs_swab16(gdp4->bg_free_inodes_count_hi);
171 	gdp4->bg_used_dirs_count_hi =
172 		ext2fs_swab16(gdp4->bg_used_dirs_count_hi);
173 	gdp4->bg_itable_unused_hi = ext2fs_swab16(gdp4->bg_itable_unused_hi);
174 	gdp4->bg_exclude_bitmap_hi = ext2fs_swab16(gdp4->bg_exclude_bitmap_hi);
175 	gdp4->bg_block_bitmap_csum_hi =
176 		ext2fs_swab16(gdp4->bg_block_bitmap_csum_hi);
177 	gdp4->bg_inode_bitmap_csum_hi =
178 		ext2fs_swab16(gdp4->bg_inode_bitmap_csum_hi);
179 	EXT2FS_BUILD_BUG_ON(sizeof(gdp4->bg_reserved) != sizeof(__u32));
180 }
181 
ext2fs_swap_group_desc(struct ext2_group_desc * gdp)182 void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
183 {
184 	ext2fs_swap_group_desc2(0, gdp);
185 }
186 
187 
ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header * to_header,struct ext2_ext_attr_header * from_header)188 void ext2fs_swap_ext_attr_header(struct ext2_ext_attr_header *to_header,
189 				 struct ext2_ext_attr_header *from_header)
190 {
191 	int n;
192 
193 	to_header->h_magic    = ext2fs_swab32(from_header->h_magic);
194 	to_header->h_blocks   = ext2fs_swab32(from_header->h_blocks);
195 	to_header->h_refcount = ext2fs_swab32(from_header->h_refcount);
196 	to_header->h_hash     = ext2fs_swab32(from_header->h_hash);
197 	to_header->h_checksum = ext2fs_swab32(from_header->h_checksum);
198 	for (n = 0; n < 3; n++)
199 		to_header->h_reserved[n] =
200 			ext2fs_swab32(from_header->h_reserved[n]);
201 }
202 
ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry * to_entry,struct ext2_ext_attr_entry * from_entry)203 void ext2fs_swap_ext_attr_entry(struct ext2_ext_attr_entry *to_entry,
204 				struct ext2_ext_attr_entry *from_entry)
205 {
206 	to_entry->e_value_offs  = ext2fs_swab16(from_entry->e_value_offs);
207 	to_entry->e_value_inum  = ext2fs_swab32(from_entry->e_value_inum);
208 	to_entry->e_value_size  = ext2fs_swab32(from_entry->e_value_size);
209 	to_entry->e_hash	= ext2fs_swab32(from_entry->e_hash);
210 }
211 
ext2fs_swap_ext_attr(char * to,char * from,int bufsize,int has_header)212 void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, int has_header)
213 {
214 	struct ext2_ext_attr_header *from_header =
215 		(struct ext2_ext_attr_header *)from;
216 	struct ext2_ext_attr_header *to_header =
217 		(struct ext2_ext_attr_header *)to;
218 	struct ext2_ext_attr_entry *from_entry, *to_entry;
219 	char *from_end = (char *)from_header + bufsize;
220 
221 	if (to_header != from_header)
222 		memcpy(to_header, from_header, bufsize);
223 
224 	if (has_header) {
225 		ext2fs_swap_ext_attr_header(to_header, from_header);
226 
227 		from_entry = (struct ext2_ext_attr_entry *)(from_header+1);
228 		to_entry   = (struct ext2_ext_attr_entry *)(to_header+1);
229 	} else {
230 		from_entry = (struct ext2_ext_attr_entry *)from_header;
231 		to_entry   = (struct ext2_ext_attr_entry *)to_header;
232 	}
233 
234 	while ((char *)from_entry < from_end &&
235 	       (char *)EXT2_EXT_ATTR_NEXT(from_entry) <= from_end &&
236 	       *(__u32 *)from_entry) {
237 		ext2fs_swap_ext_attr_entry(to_entry, from_entry);
238 		from_entry = EXT2_EXT_ATTR_NEXT(from_entry);
239 		to_entry   = EXT2_EXT_ATTR_NEXT(to_entry);
240 	}
241 }
242 
ext2fs_swap_inode_full(ext2_filsys fs,struct ext2_inode_large * t,struct ext2_inode_large * f,int hostorder,int bufsize)243 void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
244 			    struct ext2_inode_large *f, int hostorder,
245 			    int bufsize)
246 {
247 	unsigned i, extra_isize, attr_magic;
248 	int has_extents, has_inline_data, islnk, fast_symlink;
249 	int inode_size;
250 	__u32 *eaf, *eat;
251 
252 	/*
253 	 * Note that t and f may point to the same address. That's why
254 	 * if (hostorder) condition is executed before swab calls and
255 	 * if (!hostorder) afterwards.
256 	 */
257 	if (hostorder) {
258 		islnk = LINUX_S_ISLNK(f->i_mode);
259 		fast_symlink = ext2fs_is_fast_symlink(EXT2_INODE(f));
260 		has_extents = (f->i_flags & EXT4_EXTENTS_FL) != 0;
261 		has_inline_data = (f->i_flags & EXT4_INLINE_DATA_FL) != 0;
262 	}
263 
264 	t->i_mode = ext2fs_swab16(f->i_mode);
265 	t->i_uid = ext2fs_swab16(f->i_uid);
266 	t->i_size = ext2fs_swab32(f->i_size);
267 	t->i_atime = ext2fs_swab32(f->i_atime);
268 	t->i_ctime = ext2fs_swab32(f->i_ctime);
269 	t->i_mtime = ext2fs_swab32(f->i_mtime);
270 	t->i_dtime = ext2fs_swab32(f->i_dtime);
271 	t->i_gid = ext2fs_swab16(f->i_gid);
272 	t->i_links_count = ext2fs_swab16(f->i_links_count);
273 	t->i_file_acl = ext2fs_swab32(f->i_file_acl);
274 	t->i_blocks = ext2fs_swab32(f->i_blocks);
275 	t->i_flags = ext2fs_swab32(f->i_flags);
276 	t->i_size_high = ext2fs_swab32(f->i_size_high);
277 
278 	if (!hostorder) {
279 		islnk = LINUX_S_ISLNK(t->i_mode);
280 		fast_symlink = ext2fs_is_fast_symlink(EXT2_INODE(t));
281 		has_extents = (t->i_flags & EXT4_EXTENTS_FL) != 0;
282 		has_inline_data = (t->i_flags & EXT4_INLINE_DATA_FL) != 0;
283 	}
284 
285 	/*
286 	 * Extent data and inline data are swapped on access, not here
287 	 */
288 	if (!has_extents && !has_inline_data && (!islnk || !fast_symlink)) {
289 		for (i = 0; i < EXT2_N_BLOCKS; i++)
290 			t->i_block[i] = ext2fs_swab32(f->i_block[i]);
291 	} else if (t != f) {
292 		for (i = 0; i < EXT2_N_BLOCKS; i++)
293 			t->i_block[i] = f->i_block[i];
294 	}
295 	t->i_generation = ext2fs_swab32(f->i_generation);
296 	t->i_faddr = ext2fs_swab32(f->i_faddr);
297 
298 	switch (fs->super->s_creator_os) {
299 	case EXT2_OS_LINUX:
300 		t->osd1.linux1.l_i_version =
301 			ext2fs_swab32(f->osd1.linux1.l_i_version);
302 		t->osd2.linux2.l_i_blocks_hi =
303 			ext2fs_swab16(f->osd2.linux2.l_i_blocks_hi);
304 		t->osd2.linux2.l_i_file_acl_high =
305 			ext2fs_swab16(f->osd2.linux2.l_i_file_acl_high);
306 		t->osd2.linux2.l_i_uid_high =
307 		  ext2fs_swab16 (f->osd2.linux2.l_i_uid_high);
308 		t->osd2.linux2.l_i_gid_high =
309 		  ext2fs_swab16 (f->osd2.linux2.l_i_gid_high);
310 		t->osd2.linux2.l_i_checksum_lo =
311 			ext2fs_swab16(f->osd2.linux2.l_i_checksum_lo);
312 		break;
313 	case EXT2_OS_HURD:
314 		t->osd1.hurd1.h_i_translator =
315 		  ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
316 		t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
317 		t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
318 		t->osd2.hurd2.h_i_mode_high =
319 		  ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
320 		t->osd2.hurd2.h_i_uid_high =
321 		  ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
322 		t->osd2.hurd2.h_i_gid_high =
323 		  ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
324 		t->osd2.hurd2.h_i_author =
325 		  ext2fs_swab32 (f->osd2.hurd2.h_i_author);
326 		break;
327 	default:
328 		break;
329 	}
330 
331 	if (bufsize < (int) (sizeof(struct ext2_inode) + sizeof(__u16)))
332 		return; /* no i_extra_isize field */
333 
334 	if (hostorder)
335 		extra_isize = f->i_extra_isize;
336 	t->i_extra_isize = ext2fs_swab16(f->i_extra_isize);
337 	if (!hostorder)
338 		extra_isize = t->i_extra_isize;
339 	if (extra_isize > EXT2_INODE_SIZE(fs->super) -
340 				sizeof(struct ext2_inode)) {
341 		/* this is error case: i_extra_size is too large */
342 		return;
343 	}
344 	if (extra_isize & 3)
345 		return;		/* Illegal inode extra_isize */
346 
347 	inode_size = EXT2_GOOD_OLD_INODE_SIZE + extra_isize;
348 	if (inode_includes(inode_size, i_checksum_hi))
349 		t->i_checksum_hi = ext2fs_swab16(f->i_checksum_hi);
350 	if (inode_includes(inode_size, i_ctime_extra))
351 		t->i_ctime_extra = ext2fs_swab32(f->i_ctime_extra);
352 	if (inode_includes(inode_size, i_mtime_extra))
353 		t->i_mtime_extra = ext2fs_swab32(f->i_mtime_extra);
354 	if (inode_includes(inode_size, i_atime_extra))
355 		t->i_atime_extra = ext2fs_swab32(f->i_atime_extra);
356 	if (inode_includes(inode_size, i_crtime))
357 		t->i_crtime = ext2fs_swab32(f->i_crtime);
358 	if (inode_includes(inode_size, i_crtime_extra))
359 		t->i_crtime_extra = ext2fs_swab32(f->i_crtime_extra);
360 	if (inode_includes(inode_size, i_version_hi))
361 		t->i_version_hi = ext2fs_swab32(f->i_version_hi);
362 	if (inode_includes(inode_size, i_projid))
363                 t->i_projid = ext2fs_swab32(f->i_projid);
364 	/* catch new static fields added after i_projid */
365 	EXT2FS_BUILD_BUG_ON(sizeof(struct ext2_inode_large) != 160);
366 
367 	i = sizeof(struct ext2_inode) + extra_isize + sizeof(__u32);
368 	if (bufsize < (int) i)
369 		return; /* no space for EA magic */
370 
371 	eaf = (__u32 *) (((char *) f) + sizeof(struct ext2_inode) +
372 					extra_isize);
373 
374 	attr_magic = *eaf;
375 	if (!hostorder)
376 		attr_magic = ext2fs_swab32(attr_magic);
377 
378 	if (attr_magic != EXT2_EXT_ATTR_MAGIC)
379 		return; /* it seems no magic here */
380 
381 	eat = (__u32 *) (((char *) t) + sizeof(struct ext2_inode) +
382 					extra_isize);
383 	*eat = ext2fs_swab32(*eaf);
384 
385 	/* convert EA(s) */
386 	ext2fs_swap_ext_attr((char *) (eat + 1), (char *) (eaf + 1),
387 			     bufsize - sizeof(struct ext2_inode) -
388 			     extra_isize - sizeof(__u32), 0);
389 
390 }
391 
ext2fs_swap_inode(ext2_filsys fs,struct ext2_inode * t,struct ext2_inode * f,int hostorder)392 void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
393 		       struct ext2_inode *f, int hostorder)
394 {
395 	ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) t,
396 				(struct ext2_inode_large *) f, hostorder,
397 				sizeof(struct ext2_inode));
398 }
399 
ext2fs_swap_mmp(struct mmp_struct * mmp)400 void ext2fs_swap_mmp(struct mmp_struct *mmp)
401 {
402 	mmp->mmp_magic = ext2fs_swab32(mmp->mmp_magic);
403 	mmp->mmp_seq = ext2fs_swab32(mmp->mmp_seq);
404 	mmp->mmp_time = ext2fs_swab64(mmp->mmp_time);
405 	mmp->mmp_check_interval = ext2fs_swab16(mmp->mmp_check_interval);
406 	mmp->mmp_checksum = ext2fs_swab32(mmp->mmp_checksum);
407 }
408 
ext2fs_dirent_swab_in(ext2_filsys fs,char * buf,int flags)409 errcode_t ext2fs_dirent_swab_in(ext2_filsys fs, char *buf, int flags)
410 {
411 	return ext2fs_dirent_swab_in2(fs, buf, fs->blocksize, flags);
412 }
413 
ext2fs_dirent_swab_in2(ext2_filsys fs,char * buf,size_t size,int flags)414 errcode_t ext2fs_dirent_swab_in2(ext2_filsys fs, char *buf,
415 				 size_t size, int flags)
416 {
417 	errcode_t	retval;
418 	char		*p, *end;
419 	struct ext2_dir_entry *dirent;
420 	unsigned int	name_len, rec_len;
421 
422 	p = (char *) buf;
423 	end = (char *) buf + size;
424 	while (p < end-8) {
425 		dirent = (struct ext2_dir_entry *) p;
426 		dirent->inode = ext2fs_swab32(dirent->inode);
427 		dirent->rec_len = ext2fs_swab16(dirent->rec_len);
428 		dirent->name_len = ext2fs_swab16(dirent->name_len);
429 		name_len = dirent->name_len;
430 		if (flags & EXT2_DIRBLOCK_V2_STRUCT)
431 			dirent->name_len = ext2fs_swab16(dirent->name_len);
432 		retval = ext2fs_get_rec_len(fs, dirent, &rec_len);
433 		if (retval)
434 			return retval;
435 		if ((rec_len < 8) || (rec_len % 4)) {
436 			rec_len = 8;
437 			retval = EXT2_ET_DIR_CORRUPTED;
438 		} else if (((name_len & 0xFF) + 8) > rec_len)
439 			retval = EXT2_ET_DIR_CORRUPTED;
440 		p += rec_len;
441 	}
442 
443 	return 0;
444 }
445 
ext2fs_dirent_swab_out(ext2_filsys fs,char * buf,int flags)446 errcode_t ext2fs_dirent_swab_out(ext2_filsys fs, char *buf, int flags)
447 {
448 	return ext2fs_dirent_swab_out2(fs, buf, fs->blocksize, flags);
449 }
450 
ext2fs_dirent_swab_out2(ext2_filsys fs,char * buf,size_t size,int flags)451 errcode_t ext2fs_dirent_swab_out2(ext2_filsys fs, char *buf,
452 				  size_t size, int flags)
453 {
454 	errcode_t	retval;
455 	char		*p, *end;
456 	unsigned int	rec_len;
457 	struct ext2_dir_entry *dirent;
458 
459 	p = buf;
460 	end = buf + size;
461 	while (p < end) {
462 		dirent = (struct ext2_dir_entry *) p;
463 		retval = ext2fs_get_rec_len(fs, dirent, &rec_len);
464 		if (retval)
465 			return retval;
466 		if ((rec_len < 8) ||
467 		    (rec_len % 4)) {
468 			ext2fs_free_mem(&buf);
469 			return EXT2_ET_DIR_CORRUPTED;
470 		}
471 		p += rec_len;
472 		dirent->inode = ext2fs_swab32(dirent->inode);
473 		dirent->rec_len = ext2fs_swab16(dirent->rec_len);
474 		dirent->name_len = ext2fs_swab16(dirent->name_len);
475 
476 		if (flags & EXT2_DIRBLOCK_V2_STRUCT)
477 			dirent->name_len = ext2fs_swab16(dirent->name_len);
478 	}
479 
480 	return 0;
481 }
482 
483 #endif
484