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