blob: 8176a651695598733d8f546368b50c3b8b6ea294 [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
26void ext2fs_swap_super(struct ext2_super_block * super)
27{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000028 struct ext2fs_sb *s = (struct ext2fs_sb *) super;
29
Theodore Ts'o50e1e101997-04-26 13:58:21 +000030 super->s_inodes_count = ext2fs_swab32(super->s_inodes_count);
31 super->s_blocks_count = ext2fs_swab32(super->s_blocks_count);
32 super->s_r_blocks_count = ext2fs_swab32(super->s_r_blocks_count);
33 super->s_free_blocks_count = ext2fs_swab32(super->s_free_blocks_count);
34 super->s_free_inodes_count = ext2fs_swab32(super->s_free_inodes_count);
35 super->s_first_data_block = ext2fs_swab32(super->s_first_data_block);
36 super->s_log_block_size = ext2fs_swab32(super->s_log_block_size);
37 super->s_log_frag_size = ext2fs_swab32(super->s_log_frag_size);
38 super->s_blocks_per_group = ext2fs_swab32(super->s_blocks_per_group);
39 super->s_frags_per_group = ext2fs_swab32(super->s_frags_per_group);
40 super->s_inodes_per_group = ext2fs_swab32(super->s_inodes_per_group);
41 super->s_mtime = ext2fs_swab32(super->s_mtime);
42 super->s_wtime = ext2fs_swab32(super->s_wtime);
43 super->s_mnt_count = ext2fs_swab16(super->s_mnt_count);
44 super->s_max_mnt_count = ext2fs_swab16(super->s_max_mnt_count);
45 super->s_magic = ext2fs_swab16(super->s_magic);
46 super->s_state = ext2fs_swab16(super->s_state);
47 super->s_errors = ext2fs_swab16(super->s_errors);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000048 s->s_minor_rev_level = ext2fs_swab16(s->s_minor_rev_level);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000049 super->s_lastcheck = ext2fs_swab32(super->s_lastcheck);
50 super->s_checkinterval = ext2fs_swab32(super->s_checkinterval);
51 super->s_creator_os = ext2fs_swab32(super->s_creator_os);
52 super->s_rev_level = ext2fs_swab32(super->s_rev_level);
53#ifdef EXT2_DEF_RESUID
54 super->s_def_resuid = ext2fs_swab16(super->s_def_resuid);
55 super->s_def_resgid = ext2fs_swab16(super->s_def_resgid);
56#endif
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000057 s->s_first_ino = ext2fs_swab32(s->s_first_ino);
58 s->s_inode_size = ext2fs_swab16(s->s_inode_size);
59 s->s_block_group_nr = ext2fs_swab16(s->s_block_group_nr);
60 s->s_feature_compat = ext2fs_swab32(s->s_feature_compat);
61 s->s_feature_incompat = ext2fs_swab32(s->s_feature_incompat);
62 s->s_feature_ro_compat = ext2fs_swab32(s->s_feature_ro_compat);
Theodore Ts'o2c1a0ce2000-02-11 15:39:19 +000063 s->s_algorithm_usage_bitmap = ext2fs_swab32(s->s_algorithm_usage_bitmap);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000064}
65
66void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
67{
68 gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
69 gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
70 gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
71 gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
72 gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
73 gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
74}
75
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000076void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
77 struct ext2_inode *f, int hostorder)
78{
79 unsigned i;
80 int islnk = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000081
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000082 if (hostorder && LINUX_S_ISLNK(f->i_mode))
83 islnk = 1;
84 t->i_mode = ext2fs_swab16(f->i_mode);
85 if (!hostorder && LINUX_S_ISLNK(t->i_mode))
86 islnk = 1;
87 t->i_uid = ext2fs_swab16(f->i_uid);
88 t->i_size = ext2fs_swab32(f->i_size);
89 t->i_atime = ext2fs_swab32(f->i_atime);
90 t->i_ctime = ext2fs_swab32(f->i_ctime);
91 t->i_mtime = ext2fs_swab32(f->i_mtime);
92 t->i_dtime = ext2fs_swab32(f->i_dtime);
93 t->i_gid = ext2fs_swab16(f->i_gid);
94 t->i_links_count = ext2fs_swab16(f->i_links_count);
95 t->i_blocks = ext2fs_swab32(f->i_blocks);
96 t->i_flags = ext2fs_swab32(f->i_flags);
97 if (!islnk || f->i_blocks) {
98 for (i = 0; i < EXT2_N_BLOCKS; i++)
99 t->i_block[i] = ext2fs_swab32(f->i_block[i]);
100 } else if (t != f) {
101 for (i = 0; i < EXT2_N_BLOCKS; i++)
102 t->i_block[i] = f->i_block[i];
103 }
Theodore Ts'oe72a9ba1999-06-25 15:40:18 +0000104 t->i_generation = ext2fs_swab32(f->i_generation);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000105 t->i_file_acl = ext2fs_swab32(f->i_file_acl);
106 t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
107 t->i_faddr = ext2fs_swab32(f->i_faddr);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000108
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000109 switch (fs->super->s_creator_os) {
110 case EXT2_OS_LINUX:
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000111 t->osd1.linux1.l_i_reserved1 =
112 ext2fs_swab32(f->osd1.linux1.l_i_reserved1);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000113 t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag;
114 t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize;
115 t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
Theodore Ts'o80e808f2000-02-02 16:19:59 +0000116 t->osd2.linux2.l_i_uid_high =
117 ext2fs_swab16 (f->osd2.linux2.l_i_uid_high);
118 t->osd2.linux2.l_i_gid_high =
119 ext2fs_swab16 (f->osd2.linux2.l_i_gid_high);
120 t->osd2.linux2.l_i_reserved2 =
121 ext2fs_swab32(f->osd2.linux2.l_i_reserved2);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000122 break;
123 case EXT2_OS_HURD:
124 t->osd1.hurd1.h_i_translator =
125 ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
126 t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
127 t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
128 t->osd2.hurd2.h_i_mode_high =
129 ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
130 t->osd2.hurd2.h_i_uid_high =
131 ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
132 t->osd2.hurd2.h_i_gid_high =
133 ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
134 t->osd2.hurd2.h_i_author =
135 ext2fs_swab32 (f->osd2.hurd2.h_i_author);
136 break;
137 case EXT2_OS_MASIX:
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000138 t->osd1.masix1.m_i_reserved1 =
139 ext2fs_swab32(f->osd1.masix1.m_i_reserved1);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000140 t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
141 t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
142 t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
Theodore Ts'o19c78dc1997-04-29 16:17:09 +0000143 t->osd2.masix2.m_i_reserved2[0] =
144 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]);
145 t->osd2.masix2.m_i_reserved2[1] =
146 ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000147 break;
148 }
149}
150