3
0

swapfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * swapfs.c --- swap ext2 filesystem data structures
  4. *
  5. * Copyright (C) 1995, 1996, 2002 Theodore Ts'o.
  6. *
  7. * %Begin-Header%
  8. * This file may be redistributed under the terms of the GNU Public
  9. * License.
  10. * %End-Header%
  11. */
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <time.h>
  16. #include "ext2_fs.h"
  17. #include "ext2fs.h"
  18. #include "ext2_ext_attr.h"
  19. #if BB_BIG_ENDIAN
  20. void ext2fs_swap_super(struct ext2_super_block * sb)
  21. {
  22. int i;
  23. sb->s_inodes_count = ext2fs_swab32(sb->s_inodes_count);
  24. sb->s_blocks_count = ext2fs_swab32(sb->s_blocks_count);
  25. sb->s_r_blocks_count = ext2fs_swab32(sb->s_r_blocks_count);
  26. sb->s_free_blocks_count = ext2fs_swab32(sb->s_free_blocks_count);
  27. sb->s_free_inodes_count = ext2fs_swab32(sb->s_free_inodes_count);
  28. sb->s_first_data_block = ext2fs_swab32(sb->s_first_data_block);
  29. sb->s_log_block_size = ext2fs_swab32(sb->s_log_block_size);
  30. sb->s_log_frag_size = ext2fs_swab32(sb->s_log_frag_size);
  31. sb->s_blocks_per_group = ext2fs_swab32(sb->s_blocks_per_group);
  32. sb->s_frags_per_group = ext2fs_swab32(sb->s_frags_per_group);
  33. sb->s_inodes_per_group = ext2fs_swab32(sb->s_inodes_per_group);
  34. sb->s_mtime = ext2fs_swab32(sb->s_mtime);
  35. sb->s_wtime = ext2fs_swab32(sb->s_wtime);
  36. sb->s_mnt_count = ext2fs_swab16(sb->s_mnt_count);
  37. sb->s_max_mnt_count = ext2fs_swab16(sb->s_max_mnt_count);
  38. sb->s_magic = ext2fs_swab16(sb->s_magic);
  39. sb->s_state = ext2fs_swab16(sb->s_state);
  40. sb->s_errors = ext2fs_swab16(sb->s_errors);
  41. sb->s_minor_rev_level = ext2fs_swab16(sb->s_minor_rev_level);
  42. sb->s_lastcheck = ext2fs_swab32(sb->s_lastcheck);
  43. sb->s_checkinterval = ext2fs_swab32(sb->s_checkinterval);
  44. sb->s_creator_os = ext2fs_swab32(sb->s_creator_os);
  45. sb->s_rev_level = ext2fs_swab32(sb->s_rev_level);
  46. sb->s_def_resuid = ext2fs_swab16(sb->s_def_resuid);
  47. sb->s_def_resgid = ext2fs_swab16(sb->s_def_resgid);
  48. sb->s_first_ino = ext2fs_swab32(sb->s_first_ino);
  49. sb->s_inode_size = ext2fs_swab16(sb->s_inode_size);
  50. sb->s_block_group_nr = ext2fs_swab16(sb->s_block_group_nr);
  51. sb->s_feature_compat = ext2fs_swab32(sb->s_feature_compat);
  52. sb->s_feature_incompat = ext2fs_swab32(sb->s_feature_incompat);
  53. sb->s_feature_ro_compat = ext2fs_swab32(sb->s_feature_ro_compat);
  54. sb->s_algorithm_usage_bitmap = ext2fs_swab32(sb->s_algorithm_usage_bitmap);
  55. sb->s_reserved_gdt_blocks = ext2fs_swab16(sb->s_reserved_gdt_blocks);
  56. sb->s_journal_inum = ext2fs_swab32(sb->s_journal_inum);
  57. sb->s_journal_dev = ext2fs_swab32(sb->s_journal_dev);
  58. sb->s_last_orphan = ext2fs_swab32(sb->s_last_orphan);
  59. sb->s_default_mount_opts = ext2fs_swab32(sb->s_default_mount_opts);
  60. sb->s_first_meta_bg = ext2fs_swab32(sb->s_first_meta_bg);
  61. sb->s_mkfs_time = ext2fs_swab32(sb->s_mkfs_time);
  62. for (i=0; i < 4; i++)
  63. sb->s_hash_seed[i] = ext2fs_swab32(sb->s_hash_seed[i]);
  64. for (i=0; i < 17; i++)
  65. sb->s_jnl_blocks[i] = ext2fs_swab32(sb->s_jnl_blocks[i]);
  66. }
  67. void ext2fs_swap_group_desc(struct ext2_group_desc *gdp)
  68. {
  69. gdp->bg_block_bitmap = ext2fs_swab32(gdp->bg_block_bitmap);
  70. gdp->bg_inode_bitmap = ext2fs_swab32(gdp->bg_inode_bitmap);
  71. gdp->bg_inode_table = ext2fs_swab32(gdp->bg_inode_table);
  72. gdp->bg_free_blocks_count = ext2fs_swab16(gdp->bg_free_blocks_count);
  73. gdp->bg_free_inodes_count = ext2fs_swab16(gdp->bg_free_inodes_count);
  74. gdp->bg_used_dirs_count = ext2fs_swab16(gdp->bg_used_dirs_count);
  75. }
  76. void ext2fs_swap_ext_attr(char *to, char *from, int bufsize, int has_header)
  77. {
  78. struct ext2_ext_attr_header *from_header =
  79. (struct ext2_ext_attr_header *)from;
  80. struct ext2_ext_attr_header *to_header =
  81. (struct ext2_ext_attr_header *)to;
  82. struct ext2_ext_attr_entry *from_entry, *to_entry;
  83. char *from_end = (char *)from_header + bufsize;
  84. int n;
  85. if (to_header != from_header)
  86. memcpy(to_header, from_header, bufsize);
  87. from_entry = (struct ext2_ext_attr_entry *)from_header;
  88. to_entry = (struct ext2_ext_attr_entry *)to_header;
  89. if (has_header) {
  90. to_header->h_magic = ext2fs_swab32(from_header->h_magic);
  91. to_header->h_blocks = ext2fs_swab32(from_header->h_blocks);
  92. to_header->h_refcount = ext2fs_swab32(from_header->h_refcount);
  93. for (n=0; n<4; n++)
  94. to_header->h_reserved[n] =
  95. ext2fs_swab32(from_header->h_reserved[n]);
  96. from_entry = (struct ext2_ext_attr_entry *)(from_header+1);
  97. to_entry = (struct ext2_ext_attr_entry *)(to_header+1);
  98. }
  99. while ((char *)from_entry < from_end && *(__u32 *)from_entry) {
  100. to_entry->e_value_offs =
  101. ext2fs_swab16(from_entry->e_value_offs);
  102. to_entry->e_value_block =
  103. ext2fs_swab32(from_entry->e_value_block);
  104. to_entry->e_value_size =
  105. ext2fs_swab32(from_entry->e_value_size);
  106. from_entry = EXT2_EXT_ATTR_NEXT(from_entry);
  107. to_entry = EXT2_EXT_ATTR_NEXT(to_entry);
  108. }
  109. }
  110. void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t,
  111. struct ext2_inode_large *f, int hostorder,
  112. int bufsize)
  113. {
  114. unsigned i;
  115. int islnk = 0;
  116. __u32 *eaf, *eat;
  117. if (hostorder && LINUX_S_ISLNK(f->i_mode))
  118. islnk = 1;
  119. t->i_mode = ext2fs_swab16(f->i_mode);
  120. if (!hostorder && LINUX_S_ISLNK(t->i_mode))
  121. islnk = 1;
  122. t->i_uid = ext2fs_swab16(f->i_uid);
  123. t->i_size = ext2fs_swab32(f->i_size);
  124. t->i_atime = ext2fs_swab32(f->i_atime);
  125. t->i_ctime = ext2fs_swab32(f->i_ctime);
  126. t->i_mtime = ext2fs_swab32(f->i_mtime);
  127. t->i_dtime = ext2fs_swab32(f->i_dtime);
  128. t->i_gid = ext2fs_swab16(f->i_gid);
  129. t->i_links_count = ext2fs_swab16(f->i_links_count);
  130. t->i_blocks = ext2fs_swab32(f->i_blocks);
  131. t->i_flags = ext2fs_swab32(f->i_flags);
  132. t->i_file_acl = ext2fs_swab32(f->i_file_acl);
  133. t->i_dir_acl = ext2fs_swab32(f->i_dir_acl);
  134. if (!islnk || ext2fs_inode_data_blocks(fs, (struct ext2_inode *)t)) {
  135. for (i = 0; i < EXT2_N_BLOCKS; i++)
  136. t->i_block[i] = ext2fs_swab32(f->i_block[i]);
  137. } else if (t != f) {
  138. for (i = 0; i < EXT2_N_BLOCKS; i++)
  139. t->i_block[i] = f->i_block[i];
  140. }
  141. t->i_generation = ext2fs_swab32(f->i_generation);
  142. t->i_faddr = ext2fs_swab32(f->i_faddr);
  143. switch (fs->super->s_creator_os) {
  144. case EXT2_OS_LINUX:
  145. t->osd1.linux1.l_i_reserved1 =
  146. ext2fs_swab32(f->osd1.linux1.l_i_reserved1);
  147. t->osd2.linux2.l_i_frag = f->osd2.linux2.l_i_frag;
  148. t->osd2.linux2.l_i_fsize = f->osd2.linux2.l_i_fsize;
  149. t->osd2.linux2.i_pad1 = ext2fs_swab16(f->osd2.linux2.i_pad1);
  150. t->osd2.linux2.l_i_uid_high =
  151. ext2fs_swab16 (f->osd2.linux2.l_i_uid_high);
  152. t->osd2.linux2.l_i_gid_high =
  153. ext2fs_swab16 (f->osd2.linux2.l_i_gid_high);
  154. t->osd2.linux2.l_i_reserved2 =
  155. ext2fs_swab32(f->osd2.linux2.l_i_reserved2);
  156. break;
  157. case EXT2_OS_HURD:
  158. t->osd1.hurd1.h_i_translator =
  159. ext2fs_swab32 (f->osd1.hurd1.h_i_translator);
  160. t->osd2.hurd2.h_i_frag = f->osd2.hurd2.h_i_frag;
  161. t->osd2.hurd2.h_i_fsize = f->osd2.hurd2.h_i_fsize;
  162. t->osd2.hurd2.h_i_mode_high =
  163. ext2fs_swab16 (f->osd2.hurd2.h_i_mode_high);
  164. t->osd2.hurd2.h_i_uid_high =
  165. ext2fs_swab16 (f->osd2.hurd2.h_i_uid_high);
  166. t->osd2.hurd2.h_i_gid_high =
  167. ext2fs_swab16 (f->osd2.hurd2.h_i_gid_high);
  168. t->osd2.hurd2.h_i_author =
  169. ext2fs_swab32 (f->osd2.hurd2.h_i_author);
  170. break;
  171. case EXT2_OS_MASIX:
  172. t->osd1.masix1.m_i_reserved1 =
  173. ext2fs_swab32(f->osd1.masix1.m_i_reserved1);
  174. t->osd2.masix2.m_i_frag = f->osd2.masix2.m_i_frag;
  175. t->osd2.masix2.m_i_fsize = f->osd2.masix2.m_i_fsize;
  176. t->osd2.masix2.m_pad1 = ext2fs_swab16(f->osd2.masix2.m_pad1);
  177. t->osd2.masix2.m_i_reserved2[0] =
  178. ext2fs_swab32(f->osd2.masix2.m_i_reserved2[0]);
  179. t->osd2.masix2.m_i_reserved2[1] =
  180. ext2fs_swab32(f->osd2.masix2.m_i_reserved2[1]);
  181. break;
  182. }
  183. if (bufsize < (int) (sizeof(struct ext2_inode) + sizeof(__u16)))
  184. return; /* no i_extra_isize field */
  185. t->i_extra_isize = ext2fs_swab16(f->i_extra_isize);
  186. if (t->i_extra_isize > EXT2_INODE_SIZE(fs->super) -
  187. sizeof(struct ext2_inode)) {
  188. /* this is error case: i_extra_size is too large */
  189. return;
  190. }
  191. i = sizeof(struct ext2_inode) + t->i_extra_isize + sizeof(__u32);
  192. if (bufsize < (int) i)
  193. return; /* no space for EA magic */
  194. eaf = (__u32 *) (((char *) f) + sizeof(struct ext2_inode) +
  195. f->i_extra_isize);
  196. if (ext2fs_swab32(*eaf) != EXT2_EXT_ATTR_MAGIC)
  197. return; /* it seems no magic here */
  198. eat = (__u32 *) (((char *) t) + sizeof(struct ext2_inode) +
  199. f->i_extra_isize);
  200. *eat = ext2fs_swab32(*eaf);
  201. /* convert EA(s) */
  202. ext2fs_swap_ext_attr((char *) (eat + 1), (char *) (eaf + 1),
  203. bufsize - sizeof(struct ext2_inode) -
  204. t->i_extra_isize - sizeof(__u32), 0);
  205. }
  206. void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t,
  207. struct ext2_inode *f, int hostorder)
  208. {
  209. ext2fs_swap_inode_full(fs, (struct ext2_inode_large *) t,
  210. (struct ext2_inode_large *) f, hostorder,
  211. sizeof(struct ext2_inode));
  212. }
  213. #endif