]>
Commit | Line | Data |
---|---|---|
c6bd0c97 | 1 | /* |
7dec050a | 2 | * set_fields.c --- set a superblock value |
efc6f628 | 3 | * |
7dec050a | 4 | * Copyright (C) 2000, 2001, 2002, 2003, 2004 by Theodore Ts'o. |
efc6f628 | 5 | * |
7dec050a TT |
6 | * %Begin-Header% |
7 | * This file may be redistributed under the terms of the GNU Public | |
8 | * License. | |
9 | * %End-Header% | |
c6bd0c97 TT |
10 | */ |
11 | ||
007df860 TT |
12 | #define _XOPEN_SOURCE 600 /* for inclusion of strptime() and strtoull */ |
13 | ||
14 | #ifdef HAVE_STRTOULL | |
15 | #define STRTOULL strtoull | |
16 | #else | |
17 | #define STRTOULL strtoul | |
18 | #endif | |
619a8ea7 | 19 | |
d1154eb4 | 20 | #include "config.h" |
c6bd0c97 TT |
21 | #include <stdio.h> |
22 | #include <unistd.h> | |
23 | #include <stdlib.h> | |
24 | #include <ctype.h> | |
25 | #include <string.h> | |
8192c633 | 26 | #include <strings.h> |
c6bd0c97 TT |
27 | #include <time.h> |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #ifdef HAVE_ERRNO_H | |
31 | #include <errno.h> | |
32 | #endif | |
f7e37e8b | 33 | #include <assert.h> |
a98b51e9 TT |
34 | #if HAVE_STRINGS_H |
35 | #include <strings.h> | |
36 | #endif | |
c6bd0c97 TT |
37 | #include <fcntl.h> |
38 | #include <utime.h> | |
39 | ||
40 | #include "debugfs.h" | |
3e699064 TT |
41 | #include "uuid/uuid.h" |
42 | #include "e2p/e2p.h" | |
2d2d799c | 43 | #include "support/quotaio.h" |
c6bd0c97 TT |
44 | |
45 | static struct ext2_super_block set_sb; | |
16c581d0 | 46 | static struct ext2_inode_large set_inode; |
c7c1209e | 47 | static struct ext2_group_desc set_gd; |
16c581d0 | 48 | static struct ext4_group_desc set_gd4; |
3cebf9c1 | 49 | static struct mmp_struct set_mmp; |
cf96ecf1 | 50 | static dgrp_t set_bg; |
08fd3f36 TT |
51 | static ext2_ino_t set_ino; |
52 | static int array_idx; | |
53 | ||
54 | #define FLAG_ARRAY 0x0001 | |
f7e37e8b | 55 | #define FLAG_ALIAS 0x0002 /* Data intersects with other field */ |
c6bd0c97 | 56 | |
7dec050a | 57 | struct field_set_info { |
b044c2e0 | 58 | const char *name; |
c6bd0c97 | 59 | void *ptr; |
16c581d0 | 60 | void *ptr2; |
54434927 | 61 | unsigned int size; |
16c581d0 | 62 | errcode_t (*func)(struct field_set_info *info, char *field, char *arg); |
08fd3f36 TT |
63 | int flags; |
64 | int max_idx; | |
c6bd0c97 TT |
65 | }; |
66 | ||
16c581d0 TT |
67 | static errcode_t parse_uint(struct field_set_info *info, char *field, char *arg); |
68 | static errcode_t parse_int(struct field_set_info *info, char *field, char *arg); | |
69 | static errcode_t parse_string(struct field_set_info *info, char *field, char *arg); | |
70 | static errcode_t parse_uuid(struct field_set_info *info, char *field, char *arg); | |
71 | static errcode_t parse_hashalg(struct field_set_info *info, char *field, char *arg); | |
72 | static errcode_t parse_time(struct field_set_info *info, char *field, char *arg); | |
73 | static errcode_t parse_bmap(struct field_set_info *info, char *field, char *arg); | |
74 | static errcode_t parse_gd_csum(struct field_set_info *info, char *field, char *arg); | |
3cebf9c1 TT |
75 | static errcode_t parse_mmp_clear(struct field_set_info *info, char *field, |
76 | char *arg); | |
c6bd0c97 | 77 | |
c4ef5b4f | 78 | #if __GNUC_PREREQ (4, 6) || defined(__clang__) |
12aa7ad5 TT |
79 | #pragma GCC diagnostic push |
80 | #pragma GCC diagnostic ignored "-Wmissing-field-initializers" | |
fddc423d | 81 | #endif |
12aa7ad5 | 82 | |
7dec050a | 83 | static struct field_set_info super_fields[] = { |
16c581d0 TT |
84 | { "inodes_count", &set_sb.s_inodes_count, NULL, 4, parse_uint }, |
85 | { "blocks_count", &set_sb.s_blocks_count, &set_sb.s_blocks_count_hi, | |
86 | 4, parse_uint }, | |
87 | { "r_blocks_count", &set_sb.s_r_blocks_count, | |
88 | &set_sb.s_r_blocks_count_hi, 4, parse_uint }, | |
89 | { "free_blocks_count", &set_sb.s_free_blocks_count, | |
90 | &set_sb.s_free_blocks_hi, 4, parse_uint }, | |
91 | { "free_inodes_count", &set_sb.s_free_inodes_count, NULL, 4, parse_uint }, | |
92 | { "first_data_block", &set_sb.s_first_data_block, NULL, 4, parse_uint }, | |
93 | { "log_block_size", &set_sb.s_log_block_size, NULL, 4, parse_uint }, | |
94 | { "log_cluster_size", &set_sb.s_log_cluster_size, NULL, 4, parse_int }, | |
95 | { "blocks_per_group", &set_sb.s_blocks_per_group, NULL, 4, parse_uint }, | |
96 | { "clusters_per_group", &set_sb.s_clusters_per_group, NULL, 4, parse_uint }, | |
97 | { "inodes_per_group", &set_sb.s_inodes_per_group, NULL, 4, parse_uint }, | |
98 | { "mtime", &set_sb.s_mtime, NULL, 4, parse_time }, | |
99 | { "wtime", &set_sb.s_wtime, NULL, 4, parse_time }, | |
100 | { "mnt_count", &set_sb.s_mnt_count, NULL, 2, parse_uint }, | |
101 | { "max_mnt_count", &set_sb.s_max_mnt_count, NULL, 2, parse_int }, | |
c6bd0c97 | 102 | /* s_magic */ |
16c581d0 TT |
103 | { "state", &set_sb.s_state, NULL, 2, parse_uint }, |
104 | { "errors", &set_sb.s_errors, NULL, 2, parse_uint }, | |
105 | { "minor_rev_level", &set_sb.s_minor_rev_level, NULL, 2, parse_uint }, | |
106 | { "lastcheck", &set_sb.s_lastcheck, NULL, 4, parse_time }, | |
107 | { "checkinterval", &set_sb.s_checkinterval, NULL, 4, parse_uint }, | |
108 | { "creator_os", &set_sb.s_creator_os, NULL, 4, parse_uint }, | |
109 | { "rev_level", &set_sb.s_rev_level, NULL, 4, parse_uint }, | |
110 | { "def_resuid", &set_sb.s_def_resuid, NULL, 2, parse_uint }, | |
111 | { "def_resgid", &set_sb.s_def_resgid, NULL, 2, parse_uint }, | |
112 | { "first_ino", &set_sb.s_first_ino, NULL, 4, parse_uint }, | |
113 | { "inode_size", &set_sb.s_inode_size, NULL, 2, parse_uint }, | |
114 | { "block_group_nr", &set_sb.s_block_group_nr, NULL, 2, parse_uint }, | |
115 | { "feature_compat", &set_sb.s_feature_compat, NULL, 4, parse_uint }, | |
116 | { "feature_incompat", &set_sb.s_feature_incompat, NULL, 4, parse_uint }, | |
117 | { "feature_ro_compat", &set_sb.s_feature_ro_compat, NULL, 4, parse_uint }, | |
118 | { "uuid", &set_sb.s_uuid, NULL, 16, parse_uuid }, | |
119 | { "volume_name", &set_sb.s_volume_name, NULL, 16, parse_string }, | |
120 | { "last_mounted", &set_sb.s_last_mounted, NULL, 64, parse_string }, | |
16c581d0 | 121 | { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, NULL, |
c6bd0c97 | 122 | 4, parse_uint }, |
16c581d0 TT |
123 | { "prealloc_blocks", &set_sb.s_prealloc_blocks, NULL, 1, parse_uint }, |
124 | { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, NULL, 1, | |
c6bd0c97 | 125 | parse_uint }, |
16c581d0 | 126 | { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, NULL, 2, |
a06bd026 | 127 | parse_uint }, |
16c581d0 TT |
128 | { "journal_uuid", &set_sb.s_journal_uuid, NULL, 16, parse_uuid }, |
129 | { "journal_inum", &set_sb.s_journal_inum, NULL, 4, parse_uint }, | |
130 | { "journal_dev", &set_sb.s_journal_dev, NULL, 4, parse_uint }, | |
131 | { "last_orphan", &set_sb.s_last_orphan, NULL, 4, parse_uint }, | |
132 | { "hash_seed", &set_sb.s_hash_seed, NULL, 16, parse_uuid }, | |
133 | { "def_hash_version", &set_sb.s_def_hash_version, NULL, 1, parse_hashalg }, | |
134 | { "jnl_backup_type", &set_sb.s_jnl_backup_type, NULL, 1, parse_uint }, | |
135 | { "desc_size", &set_sb.s_desc_size, NULL, 2, parse_uint }, | |
136 | { "default_mount_opts", &set_sb.s_default_mount_opts, NULL, 4, parse_uint }, | |
137 | { "first_meta_bg", &set_sb.s_first_meta_bg, NULL, 4, parse_uint }, | |
138 | { "mkfs_time", &set_sb.s_mkfs_time, NULL, 4, parse_time }, | |
139 | { "jnl_blocks", &set_sb.s_jnl_blocks[0], NULL, 4, parse_uint, FLAG_ARRAY, | |
89c80156 | 140 | 17 }, |
16c581d0 TT |
141 | { "min_extra_isize", &set_sb.s_min_extra_isize, NULL, 2, parse_uint }, |
142 | { "want_extra_isize", &set_sb.s_want_extra_isize, NULL, 2, parse_uint }, | |
143 | { "flags", &set_sb.s_flags, NULL, 4, parse_uint }, | |
144 | { "raid_stride", &set_sb.s_raid_stride, NULL, 2, parse_uint }, | |
0f5eba75 | 145 | { "mmp_interval", &set_sb.s_mmp_update_interval, NULL, 2, parse_uint }, |
16c581d0 TT |
146 | { "mmp_block", &set_sb.s_mmp_block, NULL, 8, parse_uint }, |
147 | { "raid_stripe_width", &set_sb.s_raid_stripe_width, NULL, 4, parse_uint }, | |
148 | { "log_groups_per_flex", &set_sb.s_log_groups_per_flex, NULL, 1, parse_uint }, | |
149 | { "kbytes_written", &set_sb.s_kbytes_written, NULL, 8, parse_uint }, | |
150 | { "snapshot_inum", &set_sb.s_snapshot_inum, NULL, 4, parse_uint }, | |
151 | { "snapshot_id", &set_sb.s_snapshot_id, NULL, 4, parse_uint }, | |
152 | { "snapshot_r_blocks_count", &set_sb.s_snapshot_r_blocks_count, | |
153 | NULL, 8, parse_uint }, | |
154 | { "snapshot_list", &set_sb.s_snapshot_list, NULL, 4, parse_uint }, | |
155 | { "mount_opts", &set_sb.s_mount_opts, NULL, 64, parse_string }, | |
156 | { "usr_quota_inum", &set_sb.s_usr_quota_inum, NULL, 4, parse_uint }, | |
157 | { "grp_quota_inum", &set_sb.s_grp_quota_inum, NULL, 4, parse_uint }, | |
080e09b4 | 158 | { "prj_quota_inum", &set_sb.s_prj_quota_inum, NULL, 4, parse_uint }, |
16c581d0 | 159 | { "overhead_blocks", &set_sb.s_overhead_blocks, NULL, 4, parse_uint }, |
65c6c3e0 TT |
160 | { "backup_bgs", &set_sb.s_backup_bgs[0], NULL, 4, parse_uint, |
161 | FLAG_ARRAY, 2 }, | |
16c581d0 | 162 | { "checksum", &set_sb.s_checksum, NULL, 4, parse_uint }, |
1eedf3e5 | 163 | { "checksum_type", &set_sb.s_checksum_type, NULL, 1, parse_uint }, |
fc898cb9 | 164 | { "encryption_level", &set_sb.s_encryption_level, NULL, 1, parse_uint }, |
a5116959 TT |
165 | { "error_count", &set_sb.s_error_count, NULL, 4, parse_uint }, |
166 | { "first_error_time", &set_sb.s_first_error_time, NULL, 4, parse_time }, | |
167 | { "first_error_ino", &set_sb.s_first_error_ino, NULL, 4, parse_uint }, | |
168 | { "first_error_block", &set_sb.s_first_error_block, NULL, 8, parse_uint }, | |
169 | { "first_error_func", &set_sb.s_first_error_func, NULL, 32, parse_string }, | |
f7e37e8b | 170 | { "first_error_line", &set_sb.s_first_error_line, NULL, 4, parse_uint }, |
a5116959 TT |
171 | { "last_error_time", &set_sb.s_last_error_time, NULL, 4, parse_time }, |
172 | { "last_error_ino", &set_sb.s_last_error_ino, NULL, 4, parse_uint }, | |
173 | { "last_error_block", &set_sb.s_last_error_block, NULL, 8, parse_uint }, | |
174 | { "last_error_func", &set_sb.s_last_error_func, NULL, 32, parse_string }, | |
f7e37e8b | 175 | { "last_error_line", &set_sb.s_last_error_line, NULL, 4, parse_uint }, |
22f22ab1 TT |
176 | { "encrypt_algos", &set_sb.s_encrypt_algos, NULL, 1, parse_uint, |
177 | FLAG_ARRAY, 4 }, | |
41f22101 | 178 | { "encrypt_pw_salt", &set_sb.s_encrypt_pw_salt, NULL, 16, parse_uuid }, |
fc898cb9 | 179 | { "lpf_ino", &set_sb.s_lpf_ino, NULL, 4, parse_uint }, |
2ed0adbc | 180 | { "checksum_seed", &set_sb.s_checksum_seed, NULL, 4, parse_uint }, |
c6bd0c97 TT |
181 | { 0, 0, 0, 0 } |
182 | }; | |
183 | ||
7dec050a | 184 | static struct field_set_info inode_fields[] = { |
16c581d0 TT |
185 | { "mode", &set_inode.i_mode, NULL, 2, parse_uint }, |
186 | { "uid", &set_inode.i_uid, &set_inode.osd2.linux2.l_i_uid_high, | |
187 | 2, parse_uint }, | |
188 | { "size", &set_inode.i_size, &set_inode.i_size_high, 4, parse_uint }, | |
188960ea TT |
189 | { "atime", &set_inode.i_atime, &set_inode.i_atime_extra, |
190 | 4, parse_time }, | |
191 | { "ctime", &set_inode.i_ctime, &set_inode.i_ctime_extra, | |
192 | 4, parse_time }, | |
193 | { "mtime", &set_inode.i_mtime, &set_inode.i_mtime_extra, | |
194 | 4, parse_time }, | |
195 | { "dtime", &set_inode.i_dtime, NULL, | |
196 | 4, parse_time }, | |
16c581d0 TT |
197 | { "gid", &set_inode.i_gid, &set_inode.osd2.linux2.l_i_gid_high, |
198 | 2, parse_uint }, | |
199 | { "links_count", &set_inode.i_links_count, NULL, 2, parse_uint }, | |
200 | /* Special case: i_blocks is 4 bytes, i_blocks_high is 2 bytes */ | |
201 | { "blocks", &set_inode.i_blocks, &set_inode.osd2.linux2.l_i_blocks_hi, | |
202 | 6, parse_uint }, | |
203 | { "flags", &set_inode.i_flags, NULL, 4, parse_uint }, | |
204 | { "version", &set_inode.osd1.linux1.l_i_version, | |
205 | &set_inode.i_version_hi, 4, parse_uint }, | |
f7e37e8b KK |
206 | { "translator", &set_inode.osd1.hurd1.h_i_translator, NULL, |
207 | 4, parse_uint, FLAG_ALIAS }, | |
16c581d0 | 208 | { "block", &set_inode.i_block[0], NULL, 4, parse_uint, FLAG_ARRAY, |
08fd3f36 | 209 | EXT2_NDIR_BLOCKS }, |
16c581d0 TT |
210 | { "block[IND]", &set_inode.i_block[EXT2_IND_BLOCK], NULL, 4, parse_uint }, |
211 | { "block[DIND]", &set_inode.i_block[EXT2_DIND_BLOCK], NULL, 4, parse_uint }, | |
212 | { "block[TIND]", &set_inode.i_block[EXT2_TIND_BLOCK], NULL, 4, parse_uint }, | |
213 | { "generation", &set_inode.i_generation, NULL, 4, parse_uint }, | |
214 | /* Special case: i_file_acl_high is 2 bytes */ | |
215 | { "file_acl", &set_inode.i_file_acl, | |
216 | &set_inode.osd2.linux2.l_i_file_acl_high, 6, parse_uint }, | |
16c581d0 | 217 | { "faddr", &set_inode.i_faddr, NULL, 4, parse_uint }, |
f7e37e8b | 218 | { "frag", &set_inode.osd2.hurd2.h_i_frag, NULL, 1, parse_uint, FLAG_ALIAS }, |
16c581d0 TT |
219 | { "fsize", &set_inode.osd2.hurd2.h_i_fsize, NULL, 1, parse_uint }, |
220 | { "checksum", &set_inode.osd2.linux2.l_i_checksum_lo, | |
221 | &set_inode.i_checksum_hi, 2, parse_uint }, | |
222 | { "author", &set_inode.osd2.hurd2.h_i_author, NULL, | |
f7e37e8b | 223 | 4, parse_uint, FLAG_ALIAS }, |
16c581d0 TT |
224 | { "extra_isize", &set_inode.i_extra_isize, NULL, |
225 | 2, parse_uint }, | |
226 | { "ctime_extra", &set_inode.i_ctime_extra, NULL, | |
188960ea | 227 | 4, parse_uint, FLAG_ALIAS }, |
16c581d0 | 228 | { "mtime_extra", &set_inode.i_mtime_extra, NULL, |
188960ea | 229 | 4, parse_uint, FLAG_ALIAS }, |
16c581d0 | 230 | { "atime_extra", &set_inode.i_atime_extra, NULL, |
188960ea TT |
231 | 4, parse_uint, FLAG_ALIAS }, |
232 | { "crtime", &set_inode.i_crtime, &set_inode.i_crtime_extra, | |
233 | 4, parse_time }, | |
16c581d0 | 234 | { "crtime_extra", &set_inode.i_crtime_extra, NULL, |
188960ea | 235 | 4, parse_uint, FLAG_ALIAS }, |
f3d9ac36 | 236 | { "projid", &set_inode.i_projid, NULL, 4, parse_uint }, |
16c581d0 | 237 | { "bmap", NULL, NULL, 4, parse_bmap, FLAG_ARRAY }, |
7dec050a TT |
238 | { 0, 0, 0, 0 } |
239 | }; | |
240 | ||
c7c1209e | 241 | static struct field_set_info ext2_bg_fields[] = { |
16c581d0 TT |
242 | { "block_bitmap", &set_gd.bg_block_bitmap, NULL, 4, parse_uint }, |
243 | { "inode_bitmap", &set_gd.bg_inode_bitmap, NULL, 4, parse_uint }, | |
244 | { "inode_table", &set_gd.bg_inode_table, NULL, 4, parse_uint }, | |
245 | { "free_blocks_count", &set_gd.bg_free_blocks_count, NULL, 2, parse_uint }, | |
246 | { "free_inodes_count", &set_gd.bg_free_inodes_count, NULL, 2, parse_uint }, | |
247 | { "used_dirs_count", &set_gd.bg_used_dirs_count, NULL, 2, parse_uint }, | |
248 | { "flags", &set_gd.bg_flags, NULL, 2, parse_uint }, | |
249 | { "itable_unused", &set_gd.bg_itable_unused, NULL, 2, parse_uint }, | |
250 | { "checksum", &set_gd.bg_checksum, NULL, 2, parse_gd_csum }, | |
c7c1209e TT |
251 | { 0, 0, 0, 0 } |
252 | }; | |
253 | ||
16c581d0 TT |
254 | static struct field_set_info ext4_bg_fields[] = { |
255 | { "block_bitmap", &set_gd4.bg_block_bitmap, | |
256 | &set_gd4.bg_block_bitmap_hi, 4, parse_uint }, | |
257 | { "inode_bitmap", &set_gd4.bg_inode_bitmap, | |
258 | &set_gd4.bg_inode_bitmap_hi, 4, parse_uint }, | |
259 | { "inode_table", &set_gd4.bg_inode_table, | |
260 | &set_gd4.bg_inode_table_hi, 4, parse_uint }, | |
261 | { "free_blocks_count", &set_gd4.bg_free_blocks_count, | |
262 | &set_gd4.bg_free_blocks_count_hi, 2, parse_uint }, | |
263 | { "free_inodes_count", &set_gd4.bg_free_inodes_count, | |
264 | &set_gd4.bg_free_inodes_count_hi, 2, parse_uint }, | |
265 | { "used_dirs_count", &set_gd4.bg_used_dirs_count, | |
266 | &set_gd4.bg_used_dirs_count_hi, 2, parse_uint }, | |
267 | { "flags", &set_gd4.bg_flags, NULL, 2, parse_uint }, | |
268 | { "exclude_bitmap", &set_gd4.bg_exclude_bitmap_lo, | |
269 | &set_gd4.bg_exclude_bitmap_hi, 4, parse_uint }, | |
270 | { "block_bitmap_csum", &set_gd4.bg_block_bitmap_csum_lo, | |
271 | &set_gd4.bg_block_bitmap_csum_hi, 2, parse_uint }, | |
272 | { "inode_bitmap_csum", &set_gd4.bg_inode_bitmap_csum_lo, | |
273 | &set_gd4.bg_inode_bitmap_csum_hi, 2, parse_uint }, | |
274 | { "itable_unused", &set_gd4.bg_itable_unused, | |
275 | &set_gd4.bg_itable_unused_hi, 2, parse_uint }, | |
276 | { "checksum", &set_gd4.bg_checksum, NULL, 2, parse_gd_csum }, | |
277 | { 0, 0, 0, 0 } | |
278 | }; | |
279 | ||
3cebf9c1 | 280 | static struct field_set_info mmp_fields[] = { |
f7e37e8b KK |
281 | { "clear", &set_mmp.mmp_magic, NULL, sizeof(set_mmp), |
282 | parse_mmp_clear, FLAG_ALIAS }, | |
3cebf9c1 TT |
283 | { "magic", &set_mmp.mmp_magic, NULL, 4, parse_uint }, |
284 | { "seq", &set_mmp.mmp_seq, NULL, 4, parse_uint }, | |
285 | { "time", &set_mmp.mmp_time, NULL, 8, parse_uint }, | |
286 | { "nodename", &set_mmp.mmp_nodename, NULL, sizeof(set_mmp.mmp_nodename), | |
287 | parse_string }, | |
288 | { "bdevname", &set_mmp.mmp_bdevname, NULL, sizeof(set_mmp.mmp_bdevname), | |
289 | parse_string }, | |
290 | { "check_interval", &set_mmp.mmp_check_interval, NULL, 2, parse_uint }, | |
a9620d8b | 291 | { "checksum", &set_mmp.mmp_checksum, NULL, 4, parse_uint }, |
f7e37e8b | 292 | { 0, 0, 0, 0 } |
3cebf9c1 | 293 | }; |
fddc423d | 294 | #if __GNUC_PREREQ (4, 6) |
12aa7ad5 | 295 | #pragma GCC diagnostic pop |
fddc423d | 296 | #endif |
7105c183 | 297 | |
f7e37e8b KK |
298 | #ifdef UNITTEST |
299 | ||
12aa7ad5 | 300 | |
f7e37e8b KK |
301 | static void do_verify_field_set_info(struct field_set_info *fields, |
302 | const void *data, size_t size) | |
303 | { | |
304 | struct field_set_info *ss, *ss2; | |
305 | const char *begin = (char *)data; | |
306 | const char *end = begin + size; | |
307 | ||
308 | for (ss = fields ; ss->name ; ss++) { | |
309 | const char *ptr; | |
310 | ||
311 | /* Check pointers */ | |
312 | ptr = ss->ptr; | |
313 | assert(!ptr || (ptr >= begin && ptr < end)); | |
314 | ptr = ss->ptr2; | |
315 | assert(!ptr || (ptr >= begin && ptr < end)); | |
316 | ||
317 | /* Check function */ | |
318 | assert(ss->func); | |
319 | ||
320 | for (ss2 = fields ; ss2 != ss ; ss2++) { | |
321 | /* Check duplicate names */ | |
322 | assert(strcmp(ss->name, ss2->name)); | |
323 | ||
324 | if (ss->flags & FLAG_ALIAS || ss2->flags & FLAG_ALIAS) | |
325 | continue; | |
326 | /* Check false aliases, might be copy-n-paste error */ | |
327 | assert(!ss->ptr || (ss->ptr != ss2->ptr && | |
328 | ss->ptr != ss2->ptr2)); | |
329 | assert(!ss->ptr2 || (ss->ptr2 != ss2->ptr && | |
330 | ss->ptr2 != ss2->ptr2)); | |
331 | } | |
332 | } | |
333 | } | |
334 | ||
335 | int main(int argc, char **argv) | |
336 | { | |
337 | do_verify_field_set_info(super_fields, &set_sb, sizeof(set_sb)); | |
338 | do_verify_field_set_info(inode_fields, &set_inode, sizeof(set_inode)); | |
339 | do_verify_field_set_info(ext2_bg_fields, &set_gd, sizeof(set_gd)); | |
340 | do_verify_field_set_info(ext4_bg_fields, &set_gd4, sizeof(set_gd4)); | |
341 | do_verify_field_set_info(mmp_fields, &set_mmp, sizeof(set_mmp)); | |
342 | return 0; | |
343 | } | |
344 | ||
345 | ext2_filsys current_fs; | |
346 | ext2_ino_t root, cwd; | |
347 | ||
348 | #endif /* UNITTEST */ | |
349 | ||
16c581d0 TT |
350 | static int check_suffix(const char *field) |
351 | { | |
352 | int len = strlen(field); | |
353 | ||
354 | if (len <= 3) | |
355 | return 0; | |
356 | field += len-3; | |
357 | if (!strcmp(field, "_lo")) | |
358 | return 1; | |
359 | if (!strcmp(field, "_hi")) | |
360 | return 2; | |
361 | return 0; | |
362 | } | |
c7c1209e | 363 | |
7dec050a TT |
364 | static struct field_set_info *find_field(struct field_set_info *fields, |
365 | char *field) | |
c6bd0c97 | 366 | { |
7dec050a | 367 | struct field_set_info *ss; |
08fd3f36 TT |
368 | const char *prefix; |
369 | char *arg, *delim, *idx, *tmp; | |
16c581d0 | 370 | int suffix, prefix_len; |
08fd3f36 TT |
371 | |
372 | if (fields == super_fields) | |
373 | prefix = "s_"; | |
c7c1209e | 374 | else if (fields == inode_fields) |
08fd3f36 | 375 | prefix = "i_"; |
c7c1209e TT |
376 | else |
377 | prefix = "bg_"; | |
378 | prefix_len = strlen(prefix); | |
379 | if (strncmp(field, prefix, prefix_len) == 0) | |
380 | field += prefix_len; | |
08fd3f36 TT |
381 | |
382 | arg = malloc(strlen(field)+1); | |
383 | if (!arg) | |
384 | return NULL; | |
385 | strcpy(arg, field); | |
386 | ||
387 | idx = strchr(arg, '['); | |
388 | if (idx) { | |
389 | *idx++ = 0; | |
390 | delim = idx + strlen(idx) - 1; | |
391 | if (!*idx || *delim != ']') | |
392 | idx = 0; | |
393 | else | |
394 | *delim = 0; | |
395 | } | |
efc6f628 | 396 | /* |
08fd3f36 TT |
397 | * Can we parse the number? |
398 | */ | |
399 | if (idx) { | |
400 | array_idx = strtol(idx, &tmp, 0); | |
59a591f6 TT |
401 | if (*tmp) { |
402 | *(--idx) = '['; | |
403 | *delim = ']'; | |
08fd3f36 | 404 | idx = 0; |
59a591f6 | 405 | } |
08fd3f36 TT |
406 | } |
407 | ||
16c581d0 TT |
408 | /* |
409 | * If there is a valid _hi or a _lo suffix, strip it off | |
410 | */ | |
411 | suffix = check_suffix(arg); | |
412 | if (suffix > 0) | |
413 | arg[strlen(arg)-3] = 0; | |
414 | ||
7dec050a | 415 | for (ss = fields ; ss->name ; ss++) { |
16c581d0 TT |
416 | if (suffix && ss->ptr2 == 0) |
417 | continue; | |
08fd3f36 TT |
418 | if (ss->flags & FLAG_ARRAY) { |
419 | if (!idx || (strcmp(ss->name, arg) != 0)) | |
420 | continue; | |
421 | if (ss->max_idx > 0 && array_idx >= ss->max_idx) | |
422 | continue; | |
423 | } else { | |
16c581d0 | 424 | if (strcmp(ss->name, arg) != 0) |
08fd3f36 TT |
425 | continue; |
426 | } | |
6e9761c2 | 427 | free(arg); |
08fd3f36 | 428 | return ss; |
c6bd0c97 | 429 | } |
6e9761c2 | 430 | free(arg); |
c6bd0c97 TT |
431 | return NULL; |
432 | } | |
433 | ||
16c581d0 TT |
434 | /* |
435 | * Note: info->size == 6 is special; this means a base size 4 bytes, | |
055866d8 | 436 | * and secondary (high) size of 2 bytes. This is needed for the |
16c581d0 TT |
437 | * special case of i_blocks_high and i_file_acl_high. |
438 | */ | |
439 | static errcode_t parse_uint(struct field_set_info *info, char *field, | |
440 | char *arg) | |
c6bd0c97 | 441 | { |
16c581d0 TT |
442 | unsigned long long n, num, mask, limit; |
443 | int suffix = check_suffix(field); | |
c6bd0c97 | 444 | char *tmp; |
16c581d0 TT |
445 | void *field1 = info->ptr, *field2 = info->ptr2; |
446 | int size = (info->size == 6) ? 4 : info->size; | |
08fd3f36 | 447 | union { |
007df860 | 448 | __u64 *ptr64; |
08fd3f36 TT |
449 | __u32 *ptr32; |
450 | __u16 *ptr16; | |
451 | __u8 *ptr8; | |
452 | } u; | |
453 | ||
16c581d0 TT |
454 | if (suffix == 1) |
455 | field2 = 0; | |
456 | if (suffix == 2) { | |
457 | field1 = field2; | |
458 | field2 = 0; | |
459 | } | |
460 | ||
461 | u.ptr8 = (__u8 *) field1; | |
08fd3f36 TT |
462 | if (info->flags & FLAG_ARRAY) |
463 | u.ptr8 += array_idx * info->size; | |
c6bd0c97 | 464 | |
007df860 TT |
465 | errno = 0; |
466 | num = STRTOULL(arg, &tmp, 0); | |
467 | if (*tmp || errno) { | |
c6bd0c97 TT |
468 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", |
469 | arg, info->name); | |
470 | return EINVAL; | |
471 | } | |
16c581d0 | 472 | mask = ~0ULL >> ((8 - size) * 8); |
007df860 | 473 | limit = ~0ULL >> ((8 - info->size) * 8); |
16c581d0 TT |
474 | if (field2 && info->size != 6) |
475 | limit = ~0ULL >> ((8 - info->size*2) * 8); | |
476 | ||
007df860 TT |
477 | if (num > limit) { |
478 | fprintf(stderr, "Value '%s' exceeds field %s maximum %llu.\n", | |
479 | arg, info->name, limit); | |
480 | return EINVAL; | |
481 | } | |
16c581d0 TT |
482 | n = num & mask; |
483 | switch (size) { | |
007df860 | 484 | case 8: |
7c1384c3 TT |
485 | /* Should never get here */ |
486 | fprintf(stderr, "64-bit field %s has a second 64-bit field\n" | |
487 | "defined; BUG?!?\n", info->name); | |
488 | *u.ptr64 = 0; | |
007df860 | 489 | break; |
c6bd0c97 | 490 | case 4: |
16c581d0 | 491 | *u.ptr32 = n; |
c6bd0c97 TT |
492 | break; |
493 | case 2: | |
16c581d0 | 494 | *u.ptr16 = n; |
c6bd0c97 TT |
495 | break; |
496 | case 1: | |
16c581d0 TT |
497 | *u.ptr8 = n; |
498 | break; | |
499 | } | |
500 | if (!field2) | |
501 | return 0; | |
366d526f | 502 | n = (size == 8) ? 0 : (num >> (size*8)); |
16c581d0 TT |
503 | u.ptr8 = (__u8 *) field2; |
504 | if (info->size == 6) | |
505 | size = 2; | |
506 | switch (size) { | |
507 | case 8: | |
508 | *u.ptr64 = n; | |
509 | break; | |
510 | case 4: | |
511 | *u.ptr32 = n; | |
512 | break; | |
513 | case 2: | |
514 | *u.ptr16 = n; | |
515 | break; | |
516 | case 1: | |
517 | *u.ptr8 = n; | |
c6bd0c97 TT |
518 | break; |
519 | } | |
520 | return 0; | |
521 | } | |
522 | ||
3cebf9c1 TT |
523 | static errcode_t parse_int(struct field_set_info *info, |
524 | char *field EXT2FS_ATTR((unused)), char *arg) | |
c6bd0c97 TT |
525 | { |
526 | long num; | |
527 | char *tmp; | |
528 | __s32 *ptr32; | |
529 | __s16 *ptr16; | |
530 | __s8 *ptr8; | |
531 | ||
532 | num = strtol(arg, &tmp, 0); | |
533 | if (*tmp) { | |
534 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
535 | arg, info->name); | |
536 | return EINVAL; | |
537 | } | |
538 | switch (info->size) { | |
539 | case 4: | |
540 | ptr32 = (__s32 *) info->ptr; | |
541 | *ptr32 = num; | |
542 | break; | |
543 | case 2: | |
544 | ptr16 = (__s16 *) info->ptr; | |
545 | *ptr16 = num; | |
546 | break; | |
547 | case 1: | |
548 | ptr8 = (__s8 *) info->ptr; | |
549 | *ptr8 = num; | |
550 | break; | |
551 | } | |
552 | return 0; | |
553 | } | |
554 | ||
3cebf9c1 TT |
555 | static errcode_t parse_string(struct field_set_info *info, |
556 | char *field EXT2FS_ATTR((unused)), char *arg) | |
c6bd0c97 TT |
557 | { |
558 | char *cp = (char *) info->ptr; | |
559 | ||
560 | if (strlen(arg) >= info->size) { | |
561 | fprintf(stderr, "Error maximum size for %s is %d.\n", | |
562 | info->name, info->size); | |
563 | return EINVAL; | |
564 | } | |
565 | strcpy(cp, arg); | |
566 | return 0; | |
567 | } | |
568 | ||
3cebf9c1 | 569 | static errcode_t parse_time(struct field_set_info *info, |
188960ea | 570 | char *field, char *arg) |
7dec050a | 571 | { |
188960ea TT |
572 | __s64 t; |
573 | __u32 t_low, t_high; | |
574 | __u32 *ptr_low, *ptr_high; | |
188960ea TT |
575 | |
576 | if (check_suffix(field)) | |
577 | return parse_uint(info, field, arg); | |
7dec050a | 578 | |
188960ea TT |
579 | ptr_low = (__u32 *) info->ptr; |
580 | ptr_high = (__u32 *) info->ptr2; | |
7dec050a | 581 | |
4efae606 TT |
582 | t = string_to_time(arg); |
583 | ||
188960ea | 584 | if (t == -1) { |
4efae606 TT |
585 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", |
586 | arg, info->name); | |
587 | return EINVAL; | |
7dec050a | 588 | } |
188960ea TT |
589 | t_low = (__u32) t; |
590 | t_high = ((t - (__s32)t) >> 32) & EXT4_EPOCH_MASK; | |
591 | *ptr_low = t_low; | |
592 | if (ptr_high) | |
593 | *ptr_high = (*ptr_high & ~EXT4_EPOCH_MASK) | t_high; | |
7dec050a TT |
594 | return 0; |
595 | } | |
596 | ||
3cebf9c1 TT |
597 | static errcode_t parse_uuid(struct field_set_info *info, |
598 | char *field EXT2FS_ATTR((unused)), char *arg) | |
f61fc0b5 | 599 | { |
3e699064 | 600 | unsigned char * p = (unsigned char *) info->ptr; |
efc6f628 | 601 | |
f61fc0b5 TT |
602 | if ((strcasecmp(arg, "null") == 0) || |
603 | (strcasecmp(arg, "clear") == 0)) { | |
604 | uuid_clear(p); | |
605 | } else if (strcasecmp(arg, "time") == 0) { | |
606 | uuid_generate_time(p); | |
607 | } else if (strcasecmp(arg, "random") == 0) { | |
608 | uuid_generate(p); | |
609 | } else if (uuid_parse(arg, p)) { | |
610 | fprintf(stderr, "Invalid UUID format: %s\n", arg); | |
611 | return EINVAL; | |
612 | } | |
613 | return 0; | |
614 | } | |
615 | ||
3cebf9c1 TT |
616 | static errcode_t parse_hashalg(struct field_set_info *info, |
617 | char *field EXT2FS_ATTR((unused)), char *arg) | |
f61fc0b5 TT |
618 | { |
619 | int hashv; | |
620 | unsigned char *p = (unsigned char *) info->ptr; | |
621 | ||
622 | hashv = e2p_string2hash(arg); | |
623 | if (hashv < 0) { | |
624 | fprintf(stderr, "Invalid hash algorithm: %s\n", arg); | |
625 | return EINVAL; | |
626 | } | |
627 | *p = hashv; | |
628 | return 0; | |
629 | } | |
630 | ||
3cebf9c1 TT |
631 | static errcode_t parse_bmap(struct field_set_info *info, |
632 | char *field EXT2FS_ATTR((unused)), char *arg) | |
08fd3f36 | 633 | { |
4dbfd79d | 634 | blk64_t blk; |
08fd3f36 TT |
635 | errcode_t retval; |
636 | char *tmp; | |
637 | ||
4dbfd79d | 638 | blk = strtoull(arg, &tmp, 0); |
08fd3f36 TT |
639 | if (*tmp) { |
640 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
641 | arg, info->name); | |
642 | return EINVAL; | |
643 | } | |
08fd3f36 | 644 | |
4dbfd79d | 645 | retval = ext2fs_bmap2(current_fs, set_ino, |
fa633bf9 DW |
646 | (struct ext2_inode *) &set_inode, |
647 | NULL, BMAP_ALLOC | BMAP_SET, array_idx, NULL, | |
648 | &blk); | |
08fd3f36 TT |
649 | if (retval) { |
650 | com_err("set_inode", retval, "while setting block map"); | |
651 | } | |
619a8ea7 | 652 | return retval; |
08fd3f36 TT |
653 | } |
654 | ||
16c581d0 TT |
655 | static errcode_t parse_gd_csum(struct field_set_info *info, char *field, |
656 | char *arg) | |
cf96ecf1 | 657 | { |
612f7753 | 658 | __u16 *checksum = info->ptr; |
cf96ecf1 AD |
659 | |
660 | if (strcmp(arg, "calc") == 0) { | |
612f7753 TT |
661 | *checksum = ext2fs_group_desc_csum(current_fs, set_bg); |
662 | printf("Checksum set to 0x%04x\n", *checksum); | |
cf96ecf1 AD |
663 | return 0; |
664 | } | |
16c581d0 | 665 | return parse_uint(info, field, arg); |
cf96ecf1 | 666 | } |
f61fc0b5 | 667 | |
7dec050a | 668 | static void print_possible_fields(struct field_set_info *fields) |
6e96adcb | 669 | { |
7dec050a TT |
670 | struct field_set_info *ss; |
671 | const char *type, *cmd; | |
672 | FILE *f; | |
08fd3f36 | 673 | char name[40], idx[40]; |
6e96adcb | 674 | |
7dec050a TT |
675 | if (fields == super_fields) { |
676 | type = "Superblock"; | |
677 | cmd = "set_super_value"; | |
c7c1209e | 678 | } else if (fields == inode_fields) { |
7dec050a TT |
679 | type = "Inode"; |
680 | cmd = "set_inode"; | |
7105c183 ES |
681 | } else if (fields == mmp_fields) { |
682 | type = "MMP"; | |
683 | cmd = "set_mmp_value"; | |
c7c1209e TT |
684 | } else { |
685 | type = "Block group descriptor"; | |
686 | cmd = "set_block_group"; | |
7dec050a TT |
687 | } |
688 | f = open_pager(); | |
689 | ||
690 | fprintf(f, "%s fields supported by the %s command:\n", type, cmd); | |
691 | ||
692 | for (ss = fields ; ss->name ; ss++) { | |
6e96adcb TT |
693 | type = "unknown"; |
694 | if (ss->func == parse_string) | |
695 | type = "string"; | |
696 | else if (ss->func == parse_int) | |
697 | type = "integer"; | |
698 | else if (ss->func == parse_uint) | |
699 | type = "unsigned integer"; | |
f61fc0b5 TT |
700 | else if (ss->func == parse_uuid) |
701 | type = "UUID"; | |
702 | else if (ss->func == parse_hashalg) | |
703 | type = "hash algorithm"; | |
7dec050a TT |
704 | else if (ss->func == parse_time) |
705 | type = "date/time"; | |
1d3a9511 TT |
706 | else if (ss->func == parse_bmap) |
707 | type = "set physical->logical block map"; | |
16c581d0 TT |
708 | else if (ss->func == parse_gd_csum) |
709 | type = "unsigned integer OR \"calc\""; | |
08fd3f36 TT |
710 | strcpy(name, ss->name); |
711 | if (ss->flags & FLAG_ARRAY) { | |
efc6f628 | 712 | if (ss->max_idx > 0) |
08fd3f36 TT |
713 | sprintf(idx, "[%d]", ss->max_idx); |
714 | else | |
715 | strcpy(idx, "[]"); | |
716 | strcat(name, idx); | |
717 | } | |
16c581d0 TT |
718 | if (ss->ptr2) |
719 | strcat(name, "[_hi|_lo]"); | |
720 | fprintf(f, "\t%-25s\t%s\n", name, type); | |
6e96adcb | 721 | } |
7dec050a | 722 | close_pager(f); |
6e96adcb TT |
723 | } |
724 | ||
725 | ||
2fcbcb1b TT |
726 | void do_set_super(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), |
727 | void *infop EXT2FS_ATTR((unused))) | |
c6bd0c97 | 728 | { |
e1018eea TT |
729 | const char *usage = "<field> <value>\n" |
730 | "\t\"set_super_value -l\" will list the names of " | |
731 | "superblock fields\n\twhich can be set."; | |
7dec050a | 732 | static struct field_set_info *ss; |
efc6f628 | 733 | |
6e96adcb | 734 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
7dec050a | 735 | print_possible_fields(super_fields); |
6e96adcb TT |
736 | return; |
737 | } | |
738 | ||
e1018eea TT |
739 | if (common_args_process(argc, argv, 3, 3, "set_super_value", |
740 | usage, CHECK_FS_RW)) | |
c6bd0c97 TT |
741 | return; |
742 | ||
7dec050a | 743 | if ((ss = find_field(super_fields, argv[1])) == 0) { |
c6bd0c97 TT |
744 | com_err(argv[0], 0, "invalid field specifier: %s", argv[1]); |
745 | return; | |
746 | } | |
747 | set_sb = *current_fs->super; | |
16c581d0 | 748 | if (ss->func(ss, argv[1], argv[2]) == 0) { |
c6bd0c97 TT |
749 | *current_fs->super = set_sb; |
750 | ext2fs_mark_super_dirty(current_fs); | |
751 | } | |
752 | } | |
7dec050a | 753 | |
2fcbcb1b TT |
754 | void do_set_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), |
755 | void *infop EXT2FS_ATTR((unused))) | |
7dec050a TT |
756 | { |
757 | const char *usage = "<inode> <field> <value>\n" | |
1d3a9511 | 758 | "\t\"set_inode_field -l\" will list the names of " |
7dec050a TT |
759 | "the fields in an ext2 inode\n\twhich can be set."; |
760 | static struct field_set_info *ss; | |
efc6f628 | 761 | |
7dec050a TT |
762 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
763 | print_possible_fields(inode_fields); | |
764 | return; | |
765 | } | |
766 | ||
767 | if (common_args_process(argc, argv, 4, 4, "set_inode", | |
768 | usage, CHECK_FS_RW)) | |
769 | return; | |
770 | ||
771 | if ((ss = find_field(inode_fields, argv[2])) == 0) { | |
772 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); | |
773 | return; | |
774 | } | |
775 | ||
08fd3f36 TT |
776 | set_ino = string_to_inode(argv[1]); |
777 | if (!set_ino) | |
7dec050a TT |
778 | return; |
779 | ||
16c581d0 TT |
780 | if (debugfs_read_inode_full(set_ino, |
781 | (struct ext2_inode *) &set_inode, argv[1], | |
782 | sizeof(set_inode))) | |
7dec050a TT |
783 | return; |
784 | ||
16c581d0 TT |
785 | if (ss->func(ss, argv[2], argv[3]) == 0) { |
786 | if (debugfs_write_inode_full(set_ino, | |
787 | (struct ext2_inode *) &set_inode, | |
788 | argv[1], sizeof(set_inode))) | |
7dec050a TT |
789 | return; |
790 | } | |
791 | } | |
c7c1209e | 792 | |
2fcbcb1b TT |
793 | void do_set_block_group_descriptor(int argc, char *argv[], |
794 | int sci_idx EXT2FS_ATTR((unused)), | |
795 | void *infop EXT2FS_ATTR((unused))) | |
c7c1209e TT |
796 | { |
797 | const char *usage = "<bg number> <field> <value>\n" | |
7600aa0f | 798 | "\t\"set_block_group -l\" will list the names of " |
c7c1209e | 799 | "the fields in a block group descriptor\n\twhich can be set."; |
16c581d0 | 800 | struct field_set_info *table; |
c7c1209e | 801 | struct field_set_info *ss; |
c7c1209e | 802 | char *end; |
16c581d0 TT |
803 | void *edit, *target; |
804 | int size; | |
805 | ||
806 | /* | |
2bc30417 AD |
807 | * Determine whether we are editing an ext2 or ext4 block group |
808 | * descriptor. Descriptors larger than ext4_group_desc cannot | |
809 | * have their fields edited yet, because they do not have any | |
810 | * names assigned. When that happens, this function needs to | |
811 | * be updated for the new descriptor struct and fields. | |
16c581d0 | 812 | */ |
2bc30417 AD |
813 | if (current_fs && |
814 | EXT2_DESC_SIZE(current_fs->super) >= EXT2_MIN_DESC_SIZE_64BIT) { | |
16c581d0 TT |
815 | table = ext4_bg_fields; |
816 | edit = &set_gd4; | |
817 | size = sizeof(set_gd4); | |
818 | } else { | |
819 | table = ext2_bg_fields; | |
820 | edit = &set_gd; | |
821 | size = sizeof(set_gd); | |
822 | } | |
c7c1209e TT |
823 | |
824 | if ((argc == 2) && !strcmp(argv[1], "-l")) { | |
16c581d0 | 825 | print_possible_fields(table); |
c7c1209e TT |
826 | return; |
827 | } | |
828 | ||
7600aa0f | 829 | if (common_args_process(argc, argv, 4, 4, "set_block_group", |
c7c1209e TT |
830 | usage, CHECK_FS_RW)) |
831 | return; | |
832 | ||
833 | set_bg = strtoul(argv[1], &end, 0); | |
834 | if (*end) { | |
835 | com_err(argv[0], 0, "invalid block group number: %s", argv[1]); | |
836 | return; | |
837 | } | |
838 | ||
839 | if (set_bg >= current_fs->group_desc_count) { | |
840 | com_err(argv[0], 0, "block group number too big: %d", set_bg); | |
841 | return; | |
842 | } | |
843 | ||
16c581d0 | 844 | if ((ss = find_field(table, argv[2])) == 0) { |
c7c1209e TT |
845 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); |
846 | return; | |
847 | } | |
848 | ||
16c581d0 TT |
849 | target = ext2fs_group_desc(current_fs, current_fs->group_desc, set_bg); |
850 | memcpy(edit, target, size); | |
851 | if (ss->func(ss, argv[2], argv[3]) == 0) { | |
852 | memcpy(target, edit, size); | |
c7c1209e TT |
853 | ext2fs_mark_super_dirty(current_fs); |
854 | } | |
855 | } | |
0f5eba75 | 856 | |
3cebf9c1 TT |
857 | static errcode_t parse_mmp_clear(struct field_set_info *info, |
858 | char *field EXT2FS_ATTR((unused)), | |
859 | char *arg EXT2FS_ATTR((unused))) | |
0f5eba75 AD |
860 | { |
861 | errcode_t retval; | |
862 | ||
863 | retval = ext2fs_mmp_clear(current_fs); | |
864 | if (retval != 0) | |
865 | com_err("set_mmp_value", retval, "while clearing MMP block\n"); | |
866 | else | |
867 | memcpy(info->ptr, current_fs->mmp_buf, info->size); | |
868 | ||
869 | return 1; /* we don't need the MMP block written again */ | |
870 | } | |
871 | ||
25f291c9 | 872 | #ifdef CONFIG_MMP |
2fcbcb1b TT |
873 | void do_set_mmp_value(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)), |
874 | void *infop EXT2FS_ATTR((unused))) | |
0f5eba75 AD |
875 | { |
876 | const char *usage = "<field> <value>\n" | |
877 | "\t\"set_mmp_value -l\" will list the names of " | |
878 | "MMP fields\n\twhich can be set."; | |
879 | static struct field_set_info *smmp; | |
880 | struct mmp_struct *mmp_s; | |
881 | errcode_t retval; | |
882 | ||
883 | if (argc == 2 && strcmp(argv[1], "-l") == 0) { | |
884 | print_possible_fields(mmp_fields); | |
885 | return; | |
886 | } | |
887 | ||
7105c183 ES |
888 | if (check_fs_open(argv[0])) |
889 | return; | |
890 | ||
0f5eba75 AD |
891 | if (current_fs->super->s_mmp_block == 0) { |
892 | com_err(argv[0], 0, "no MMP block allocated\n"); | |
893 | return; | |
894 | } | |
895 | ||
896 | if (common_args_process(argc, argv, 2, 3, "set_mmp_value", | |
897 | usage, CHECK_FS_RW)) | |
898 | return; | |
899 | ||
900 | mmp_s = current_fs->mmp_buf; | |
901 | if (mmp_s == NULL) { | |
902 | retval = ext2fs_get_mem(current_fs->blocksize, &mmp_s); | |
903 | if (retval) { | |
904 | com_err(argv[0], retval, "allocating MMP buffer\n"); | |
905 | return; | |
906 | } | |
907 | retval = ext2fs_mmp_read(current_fs, | |
908 | current_fs->super->s_mmp_block, mmp_s); | |
909 | if (retval) { | |
910 | com_err(argv[0], retval, "reading MMP block %llu.\n", | |
911 | (long long)current_fs->super->s_mmp_block); | |
85b8f7af | 912 | ext2fs_free_mem(&mmp_s); |
0f5eba75 AD |
913 | return; |
914 | } | |
915 | current_fs->mmp_buf = mmp_s; | |
916 | } | |
917 | ||
918 | smmp = find_field(mmp_fields, argv[1]); | |
919 | if (smmp == 0) { | |
920 | com_err(argv[0], 0, "invalid field specifier: %s", argv[1]); | |
921 | return; | |
922 | } | |
923 | ||
924 | set_mmp = *mmp_s; | |
925 | if (smmp->func(smmp, argv[1], argv[2]) == 0) { | |
926 | ext2fs_mmp_write(current_fs, current_fs->super->s_mmp_block, | |
927 | &set_mmp); | |
928 | *mmp_s = set_mmp; | |
929 | } | |
25f291c9 | 930 | } |
d6a4bcb5 | 931 | #else |
25f291c9 | 932 | void do_set_mmp_value(int argc EXT2FS_ATTR((unused)), |
2fcbcb1b TT |
933 | char *argv[] EXT2FS_ATTR((unused)), |
934 | int sci_idx EXT2FS_ATTR((unused)), | |
935 | void *infop EXT2FS_ATTR((unused))) | |
25f291c9 | 936 | { |
d6a4bcb5 TB |
937 | fprintf(stdout, "MMP is unsupported, please recompile with " |
938 | "--enable-mmp\n"); | |
0f5eba75 | 939 | } |
25f291c9 | 940 | #endif |
0f5eba75 | 941 |