blob: 75369c49f1cb30dac38161f621ff0a1acf55abaf [file] [log] [blame]
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001/*
2 * swapfs.c --- swap ext2 filesystem data structures
3 *
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00004 * Copyright (C) 1995, 1996 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'o50e1e101997-04-26 13:58:21 +000010 */
11
12#include <stdio.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000013#if HAVE_UNISTD_H
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000015#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000016#include <time.h>
17
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000018#if EXT2_FLAT_INCLUDES
19#include "ext2_fs.h"
20#else
Theodore Ts'o50e1e101997-04-26 13:58:21 +000021#include <linux/ext2_fs.h>
Theodore Ts'ob5abe6f1998-01-19 14:47:53 +000022#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000023
24#include "ext2fs.h"
25
Theodore Ts'oe5b38a52001-01-01 16:17:12 +000026void ext2fs_swap_super(struct ext2_super_block * sb)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000027{
Theodore Ts'oe5b38a52001-01-01 16:17:12 +000028 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_frag_size = ext2fs_swab32(sb->s_log_frag_size);
36 sb->s_blocks_per_group = ext2fs_swab32(sb->s_blocks_per_group);
37 sb->s_frags_per_group = ext2fs_swab32(sb->s_frags_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_journal_inum = ext2fs_swab32(sb->s_journal_inum);
61 sb->s_journal_dev = ext2fs_swab32(sb->s_journal_dev);
62 sb->s_last_orphan = ext2fs_swab32(sb->s_last_orphan);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000063}
64
65void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
66{
67 gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
68 gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
69 gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
70 gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
71 gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
72 gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
73}
74
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000075void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
76 struct ext2_inode *f, int hostorder)
77{
78 unsigned i;
79 int islnk = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000080
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000081 if (hostorder && LINUX_S_ISLNK(f->i_mode))
82 islnk = 1;
83 t->i_mode = ext2fs_swab16(f->i_mode);
84 if (!hostorder && LINUX_S_ISLNK(t->i_mode))
85 islnk = 1;
86 t->i_uid = ext2fs_swab16(f->i_uid);
87 t->i_size = ext2fs_swab32(f->i_size);
88 t->i_atime = ext2fs_swab32(f->i_atime);
89 t->i_ctime = ext2fs_swab32(f->i_ctime);
90 t->i_mtime = ext2fs_swab32(f->i_mtime);
91 t->i_dtime = ext2fs_swab32(f->i_dtime);
92 t->i_gid = ext2fs_swab16(f->i_gid);
93 t->i_links_count = ext2fs_swab16(f->i_links_count);
94 t->i_blocks = ext2fs_swab32(f->i_blocks);
95 t->i_flags = ext2fs_swab32(f->i_flags);
96 if (!islnk || f->i_blocks) {
97 for (i = 0; i < EXT2_N_BLOCKS; i++)
98 t->i_block[i] = ext2fs_swab32(f->i_block[i]);
99 } else if (t != f) {
100 for (i = 0; i < EXT2_N_BLOCKS; i++)
101 t->i_block[i] = f->i_block[i];
102 }
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000103 t->i_generation = ext2fs_swab32(f->i_generation);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000104 t->i_file_acl = ext2fs_swab32(f->i_file_acl);
105 t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
106 t->i_faddr = ext2fs_swab32(f->i_faddr);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000107
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000108 switch (fs->super->s_creator_os) {
109 case EXT2_OS_LINUX:
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000110 t->osd1.linux1.l_i_reserved1 =
111 ext2fs_swab32(f->osd1.linux1.l_i_reserved1);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000112 t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag;
113 t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize;
114 t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
Theodore Ts'o80e808f2000-02-02 16:19:59 +0000115 t->osd2.linux2.l_i_uid_high =
116 ext2fs_swab16 (f->osd2.linux2.l_i_uid_high);
117 t->osd2.linux2.l_i_gid_high =
118 ext2fs_swab16 (f->osd2.linux2.l_i_gid_high);
119 t->osd2.linux2.l_i_reserved2 =
120 ext2fs_swab32(f->osd2.linux2.l_i_reserved2);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000121 break;
122 case EXT2_OS_HURD:
123 t->osd1.hurd1.h_i_translator =
124 ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
125 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
126 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
127 t->osd2.hurd2.h_i_mode_high =
128 ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
129 t->osd2.hurd2.h_i_uid_high =
130 ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
131 t->osd2.hurd2.h_i_gid_high =
132 ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
133 t->osd2.hurd2.h_i_author =
134 ext2fs_swab32 (f->osd2.hurd2.h_i_author);
135 break;
136 case EXT2_OS_MASIX:
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000137 t->osd1.masix1.m_i_reserved1 =
138 ext2fs_swab32(f->osd1.masix1.m_i_reserved1);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000139 t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
140 t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
141 t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000142 t->osd2.masix2.m_i_reserved2[0] =
143 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]);
144 t->osd2.masix2.m_i_reserved2[1] =
145 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000146 break;
147 }
148}
149