]>
Commit | Line | Data |
---|---|---|
c6bd0c97 | 1 | /* |
7dec050a TT |
2 | * set_fields.c --- set a superblock value |
3 | * | |
4 | * Copyright (C) 2000, 2001, 2002, 2003, 2004 by 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% | |
c6bd0c97 TT |
10 | */ |
11 | ||
12 | #include <stdio.h> | |
13 | #include <unistd.h> | |
14 | #include <stdlib.h> | |
15 | #include <ctype.h> | |
16 | #include <string.h> | |
17 | #include <time.h> | |
18 | #include <sys/types.h> | |
19 | #include <sys/stat.h> | |
20 | #ifdef HAVE_ERRNO_H | |
21 | #include <errno.h> | |
22 | #endif | |
23 | #include <fcntl.h> | |
24 | #include <utime.h> | |
25 | ||
26 | #include "debugfs.h" | |
3e699064 TT |
27 | #include "uuid/uuid.h" |
28 | #include "e2p/e2p.h" | |
c6bd0c97 TT |
29 | |
30 | static struct ext2_super_block set_sb; | |
7dec050a | 31 | static struct ext2_inode set_inode; |
c6bd0c97 | 32 | |
7dec050a | 33 | struct field_set_info { |
b044c2e0 | 34 | const char *name; |
c6bd0c97 | 35 | void *ptr; |
54434927 | 36 | unsigned int size; |
7dec050a | 37 | errcode_t (*func)(struct field_set_info *info, char *arg); |
c6bd0c97 TT |
38 | }; |
39 | ||
7dec050a TT |
40 | static errcode_t parse_uint(struct field_set_info *info, char *arg); |
41 | static errcode_t parse_int(struct field_set_info *info, char *arg); | |
42 | static errcode_t parse_string(struct field_set_info *info, char *arg); | |
43 | static errcode_t parse_uuid(struct field_set_info *info, char *arg); | |
44 | static errcode_t parse_hashalg(struct field_set_info *info, char *arg); | |
45 | static errcode_t parse_time(struct field_set_info *info, char *arg); | |
c6bd0c97 | 46 | |
7dec050a | 47 | static struct field_set_info super_fields[] = { |
c6bd0c97 TT |
48 | { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint }, |
49 | { "blocks_count", &set_sb.s_blocks_count, 4, parse_uint }, | |
50 | { "r_blocks_count", &set_sb.s_r_blocks_count, 4, parse_uint }, | |
51 | { "free_blocks_count", &set_sb.s_free_blocks_count, 4, parse_uint }, | |
52 | { "free_inodes_count", &set_sb.s_free_inodes_count, 4, parse_uint }, | |
53 | { "first_data_block", &set_sb.s_first_data_block, 4, parse_uint }, | |
54 | { "log_block_size", &set_sb.s_log_block_size, 4, parse_uint }, | |
55 | { "log_frag_size", &set_sb.s_log_frag_size, 4, parse_int }, | |
56 | { "blocks_per_group", &set_sb.s_blocks_per_group, 4, parse_uint }, | |
57 | { "frags_per_group", &set_sb.s_frags_per_group, 4, parse_uint }, | |
58 | { "inodes_per_group", &set_sb.s_inodes_per_group, 4, parse_uint }, | |
7dec050a TT |
59 | { "mtime", &set_sb.s_mtime, 4, parse_time }, |
60 | { "wtime", &set_sb.s_wtime, 4, parse_time }, | |
c6bd0c97 TT |
61 | { "mnt_count", &set_sb.s_mnt_count, 2, parse_uint }, |
62 | { "max_mnt_count", &set_sb.s_max_mnt_count, 2, parse_int }, | |
63 | /* s_magic */ | |
64 | { "state", &set_sb.s_state, 2, parse_uint }, | |
65 | { "errors", &set_sb.s_errors, 2, parse_uint }, | |
66 | { "minor_rev_level", &set_sb.s_minor_rev_level, 2, parse_uint }, | |
7dec050a | 67 | { "lastcheck", &set_sb.s_lastcheck, 4, parse_time }, |
c6bd0c97 TT |
68 | { "checkinterval", &set_sb.s_checkinterval, 4, parse_uint }, |
69 | { "creator_os", &set_sb.s_creator_os, 4, parse_uint }, | |
70 | { "rev_level", &set_sb.s_rev_level, 4, parse_uint }, | |
71 | { "def_resuid", &set_sb.s_def_resuid, 2, parse_uint }, | |
72 | { "def_resgid", &set_sb.s_def_resgid, 2, parse_uint }, | |
73 | { "first_ino", &set_sb.s_first_ino, 4, parse_uint }, | |
426d734f | 74 | { "inode_size", &set_sb.s_inode_size, 2, parse_uint }, |
c6bd0c97 TT |
75 | { "block_group_nr", &set_sb.s_block_group_nr, 2, parse_uint }, |
76 | { "feature_compat", &set_sb.s_feature_compat, 4, parse_uint }, | |
77 | { "feature_incompat", &set_sb.s_feature_incompat, 4, parse_uint }, | |
78 | { "feature_ro_compat", &set_sb.s_feature_ro_compat, 4, parse_uint }, | |
f61fc0b5 | 79 | { "uuid", &set_sb.s_uuid, 16, parse_uuid }, |
c6bd0c97 TT |
80 | { "volume_name", &set_sb.s_volume_name, 16, parse_string }, |
81 | { "last_mounted", &set_sb.s_last_mounted, 64, parse_string }, | |
364a373e | 82 | { "lastcheck", &set_sb.s_lastcheck, 4, parse_uint }, |
c6bd0c97 TT |
83 | { "algorithm_usage_bitmap", &set_sb.s_algorithm_usage_bitmap, |
84 | 4, parse_uint }, | |
85 | { "prealloc_blocks", &set_sb.s_prealloc_blocks, 1, parse_uint }, | |
86 | { "prealloc_dir_blocks", &set_sb.s_prealloc_dir_blocks, 1, | |
87 | parse_uint }, | |
a06bd026 TT |
88 | { "reserved_gdt_blocks", &set_sb.s_reserved_gdt_blocks, 2, |
89 | parse_uint }, | |
c6bd0c97 | 90 | /* s_padding1 */ |
f61fc0b5 | 91 | { "journal_uuid", &set_sb.s_journal_uuid, 16, parse_uuid }, |
c6bd0c97 TT |
92 | { "journal_inum", &set_sb.s_journal_inum, 4, parse_uint }, |
93 | { "journal_dev", &set_sb.s_journal_dev, 4, parse_uint }, | |
94 | { "last_orphan", &set_sb.s_last_orphan, 4, parse_uint }, | |
f61fc0b5 TT |
95 | { "hash_seed", &set_sb.s_hash_seed, 16, parse_uuid }, |
96 | { "def_hash_version", &set_sb.s_def_hash_version, 1, parse_hashalg }, | |
a06bd026 TT |
97 | { "jnl_backup_type", &set_sb.s_jnl_backup_type, 1, parse_uint }, |
98 | /* s_reserved_word_pad */ | |
99 | { "default_mount_opts", &set_sb.s_default_mount_opts, 4, parse_uint }, | |
100 | { "first_meta_bg", &set_sb.s_first_meta_bg, 4, parse_uint }, | |
7dec050a | 101 | { "mkfs_time", &set_sb.s_mkfs_time, 4, parse_time }, |
c6bd0c97 TT |
102 | { 0, 0, 0, 0 } |
103 | }; | |
104 | ||
7dec050a TT |
105 | static struct field_set_info inode_fields[] = { |
106 | { "inodes_count", &set_sb.s_inodes_count, 4, parse_uint }, | |
107 | { "mode", &set_inode.i_mode, 2, parse_uint }, | |
108 | { "uid", &set_inode.i_uid, 2, parse_uint }, | |
109 | { "size", &set_inode.i_uid, 4, parse_uint }, | |
110 | { "atime", &set_inode.i_atime, 4, parse_time }, | |
111 | { "ctime", &set_inode.i_ctime, 4, parse_time }, | |
112 | { "mtime", &set_inode.i_mtime, 4, parse_time }, | |
113 | { "dtime", &set_inode.i_dtime, 4, parse_time }, | |
114 | { "gid", &set_inode.i_gid, 2, parse_uint }, | |
115 | { "links_count", &set_inode.i_links_count, 2, parse_uint }, | |
116 | { "blocks", &set_inode.i_blocks, 4, parse_uint }, | |
117 | { "flags", &set_inode.i_flags, 4, parse_uint }, | |
118 | { "translator", &set_inode.osd1.hurd1.h_i_translator, 4, parse_uint }, | |
119 | { "block[0]", &set_inode.i_block[0], 4, parse_uint }, | |
120 | { "block[1]", &set_inode.i_block[1], 4, parse_uint }, | |
121 | { "block[2]", &set_inode.i_block[2], 4, parse_uint }, | |
122 | { "block[3]", &set_inode.i_block[3], 4, parse_uint }, | |
123 | { "block[4]", &set_inode.i_block[4], 4, parse_uint }, | |
124 | { "block[5]", &set_inode.i_block[5], 4, parse_uint }, | |
125 | { "block[6]", &set_inode.i_block[6], 4, parse_uint }, | |
126 | { "block[7]", &set_inode.i_block[7], 4, parse_uint }, | |
127 | { "block[8]", &set_inode.i_block[8], 4, parse_uint }, | |
128 | { "block[9]", &set_inode.i_block[9], 4, parse_uint }, | |
129 | { "block[10]", &set_inode.i_block[10], 4, parse_uint }, | |
130 | { "block[11]", &set_inode.i_block[11], 4, parse_uint }, | |
131 | { "block[12]", &set_inode.i_block[12], 4, parse_uint }, | |
132 | { "block[13]", &set_inode.i_block[13], 4, parse_uint }, | |
133 | { "block[14]", &set_inode.i_block[14], 4, parse_uint }, | |
134 | { "generation", &set_inode.i_generation, 4, parse_uint }, | |
135 | { "file_acl", &set_inode.i_file_acl, 4, parse_uint }, | |
136 | { "dir_acl", &set_inode.i_dir_acl, 4, parse_uint }, | |
137 | { "faddr", &set_inode.i_faddr, 4, parse_uint }, | |
138 | { "frag", &set_inode.osd2.linux2.l_i_frag, 8, parse_uint }, | |
139 | { "fsize", &set_inode.osd2.linux2.l_i_fsize, 8, parse_uint }, | |
140 | { "uid_high", &set_inode.osd2.linux2.l_i_uid_high, 8, parse_uint }, | |
141 | { "gid_high", &set_inode.osd2.linux2.l_i_gid_high, 8, parse_uint }, | |
142 | { "author", &set_inode.osd2.hurd2.h_i_author, 8, parse_uint }, | |
143 | { 0, 0, 0, 0 } | |
144 | }; | |
145 | ||
146 | ||
147 | static struct field_set_info *find_field(struct field_set_info *fields, | |
148 | char *field) | |
c6bd0c97 | 149 | { |
7dec050a | 150 | struct field_set_info *ss; |
c6bd0c97 | 151 | |
3db9305a TT |
152 | if (strncmp(field, "s_", 2) == 0) |
153 | field += 2; | |
7dec050a | 154 | for (ss = fields ; ss->name ; ss++) { |
c6bd0c97 TT |
155 | if (strcmp(ss->name, field) == 0) |
156 | return ss; | |
157 | } | |
158 | return NULL; | |
159 | } | |
160 | ||
7dec050a | 161 | static errcode_t parse_uint(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
162 | { |
163 | unsigned long num; | |
164 | char *tmp; | |
165 | __u32 *ptr32; | |
166 | __u16 *ptr16; | |
167 | __u8 *ptr8; | |
168 | ||
169 | num = strtoul(arg, &tmp, 0); | |
170 | if (*tmp) { | |
171 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
172 | arg, info->name); | |
173 | return EINVAL; | |
174 | } | |
175 | switch (info->size) { | |
176 | case 4: | |
177 | ptr32 = (__u32 *) info->ptr; | |
178 | *ptr32 = num; | |
179 | break; | |
180 | case 2: | |
181 | ptr16 = (__u16 *) info->ptr; | |
182 | *ptr16 = num; | |
183 | break; | |
184 | case 1: | |
185 | ptr8 = (__u8 *) info->ptr; | |
186 | *ptr8 = num; | |
187 | break; | |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
7dec050a | 192 | static errcode_t parse_int(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
193 | { |
194 | long num; | |
195 | char *tmp; | |
196 | __s32 *ptr32; | |
197 | __s16 *ptr16; | |
198 | __s8 *ptr8; | |
199 | ||
200 | num = strtol(arg, &tmp, 0); | |
201 | if (*tmp) { | |
202 | fprintf(stderr, "Couldn't parse '%s' for field %s.\n", | |
203 | arg, info->name); | |
204 | return EINVAL; | |
205 | } | |
206 | switch (info->size) { | |
207 | case 4: | |
208 | ptr32 = (__s32 *) info->ptr; | |
209 | *ptr32 = num; | |
210 | break; | |
211 | case 2: | |
212 | ptr16 = (__s16 *) info->ptr; | |
213 | *ptr16 = num; | |
214 | break; | |
215 | case 1: | |
216 | ptr8 = (__s8 *) info->ptr; | |
217 | *ptr8 = num; | |
218 | break; | |
219 | } | |
220 | return 0; | |
221 | } | |
222 | ||
7dec050a | 223 | static errcode_t parse_string(struct field_set_info *info, char *arg) |
c6bd0c97 TT |
224 | { |
225 | char *cp = (char *) info->ptr; | |
226 | ||
227 | if (strlen(arg) >= info->size) { | |
228 | fprintf(stderr, "Error maximum size for %s is %d.\n", | |
229 | info->name, info->size); | |
230 | return EINVAL; | |
231 | } | |
232 | strcpy(cp, arg); | |
233 | return 0; | |
234 | } | |
235 | ||
7dec050a TT |
236 | static errcode_t parse_time(struct field_set_info *info, char *arg) |
237 | { | |
238 | struct tm ts; | |
239 | __u32 *ptr32; | |
240 | ||
241 | ptr32 = (__u32 *) info->ptr; | |
242 | ||
243 | if (strcmp(arg, "now") == 0) { | |
244 | *ptr32 = time(0); | |
245 | return 0; | |
246 | } | |
247 | memset(&ts, 0, sizeof(ts)); | |
248 | #ifdef HAVE_STRPTIME | |
249 | strptime(arg, "%Y%m%d%H%M%S", &ts); | |
250 | #else | |
251 | sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon, | |
252 | &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); | |
253 | ts.tm_year -= 1900; | |
254 | ts.tm_mon -= 1; | |
255 | if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 || | |
256 | ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || | |
257 | ts.tm_min > 59 || ts.tm_sec > 61) | |
258 | ts.tm_mday = 0; | |
259 | #endif | |
260 | if (ts.tm_mday == 0) { | |
261 | /* Try it as an integer... */ | |
262 | return parse_uint(info, arg); | |
263 | } | |
264 | *ptr32 = mktime(&ts); | |
265 | return 0; | |
266 | } | |
267 | ||
268 | static errcode_t parse_uuid(struct field_set_info *info, char *arg) | |
f61fc0b5 | 269 | { |
3e699064 | 270 | unsigned char * p = (unsigned char *) info->ptr; |
f61fc0b5 TT |
271 | |
272 | if ((strcasecmp(arg, "null") == 0) || | |
273 | (strcasecmp(arg, "clear") == 0)) { | |
274 | uuid_clear(p); | |
275 | } else if (strcasecmp(arg, "time") == 0) { | |
276 | uuid_generate_time(p); | |
277 | } else if (strcasecmp(arg, "random") == 0) { | |
278 | uuid_generate(p); | |
279 | } else if (uuid_parse(arg, p)) { | |
280 | fprintf(stderr, "Invalid UUID format: %s\n", arg); | |
281 | return EINVAL; | |
282 | } | |
283 | return 0; | |
284 | } | |
285 | ||
7dec050a | 286 | static errcode_t parse_hashalg(struct field_set_info *info, char *arg) |
f61fc0b5 TT |
287 | { |
288 | int hashv; | |
289 | unsigned char *p = (unsigned char *) info->ptr; | |
290 | ||
291 | hashv = e2p_string2hash(arg); | |
292 | if (hashv < 0) { | |
293 | fprintf(stderr, "Invalid hash algorithm: %s\n", arg); | |
294 | return EINVAL; | |
295 | } | |
296 | *p = hashv; | |
297 | return 0; | |
298 | } | |
299 | ||
300 | ||
7dec050a | 301 | static void print_possible_fields(struct field_set_info *fields) |
6e96adcb | 302 | { |
7dec050a TT |
303 | struct field_set_info *ss; |
304 | const char *type, *cmd; | |
305 | FILE *f; | |
6e96adcb | 306 | |
7dec050a TT |
307 | if (fields == super_fields) { |
308 | type = "Superblock"; | |
309 | cmd = "set_super_value"; | |
310 | } else { | |
311 | type = "Inode"; | |
312 | cmd = "set_inode"; | |
313 | } | |
314 | f = open_pager(); | |
315 | ||
316 | fprintf(f, "%s fields supported by the %s command:\n", type, cmd); | |
317 | ||
318 | for (ss = fields ; ss->name ; ss++) { | |
6e96adcb TT |
319 | type = "unknown"; |
320 | if (ss->func == parse_string) | |
321 | type = "string"; | |
322 | else if (ss->func == parse_int) | |
323 | type = "integer"; | |
324 | else if (ss->func == parse_uint) | |
325 | type = "unsigned integer"; | |
f61fc0b5 TT |
326 | else if (ss->func == parse_uuid) |
327 | type = "UUID"; | |
328 | else if (ss->func == parse_hashalg) | |
329 | type = "hash algorithm"; | |
7dec050a TT |
330 | else if (ss->func == parse_time) |
331 | type = "date/time"; | |
332 | fprintf(f, "\t%-20s\t%s\n", ss->name, type); | |
6e96adcb | 333 | } |
7dec050a | 334 | close_pager(f); |
6e96adcb TT |
335 | } |
336 | ||
337 | ||
c6bd0c97 TT |
338 | void do_set_super(int argc, char *argv[]) |
339 | { | |
e1018eea TT |
340 | const char *usage = "<field> <value>\n" |
341 | "\t\"set_super_value -l\" will list the names of " | |
342 | "superblock fields\n\twhich can be set."; | |
7dec050a | 343 | static struct field_set_info *ss; |
c6bd0c97 | 344 | |
6e96adcb | 345 | if ((argc == 2) && !strcmp(argv[1], "-l")) { |
7dec050a | 346 | print_possible_fields(super_fields); |
6e96adcb TT |
347 | return; |
348 | } | |
349 | ||
e1018eea TT |
350 | if (common_args_process(argc, argv, 3, 3, "set_super_value", |
351 | usage, CHECK_FS_RW)) | |
c6bd0c97 TT |
352 | return; |
353 | ||
7dec050a | 354 | if ((ss = find_field(super_fields, argv[1])) == 0) { |
c6bd0c97 TT |
355 | com_err(argv[0], 0, "invalid field specifier: %s", argv[1]); |
356 | return; | |
357 | } | |
358 | set_sb = *current_fs->super; | |
359 | if (ss->func(ss, argv[2]) == 0) { | |
360 | *current_fs->super = set_sb; | |
361 | ext2fs_mark_super_dirty(current_fs); | |
362 | } | |
363 | } | |
7dec050a TT |
364 | |
365 | void do_set_inode(int argc, char *argv[]) | |
366 | { | |
367 | const char *usage = "<inode> <field> <value>\n" | |
368 | "\t\"set_inode -l\" will list the names of " | |
369 | "the fields in an ext2 inode\n\twhich can be set."; | |
370 | static struct field_set_info *ss; | |
371 | ext2_ino_t ino; | |
372 | ||
373 | if ((argc == 2) && !strcmp(argv[1], "-l")) { | |
374 | print_possible_fields(inode_fields); | |
375 | return; | |
376 | } | |
377 | ||
378 | if (common_args_process(argc, argv, 4, 4, "set_inode", | |
379 | usage, CHECK_FS_RW)) | |
380 | return; | |
381 | ||
382 | if ((ss = find_field(inode_fields, argv[2])) == 0) { | |
383 | com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); | |
384 | return; | |
385 | } | |
386 | ||
387 | ino = string_to_inode(argv[1]); | |
388 | if (!ino) | |
389 | return; | |
390 | ||
391 | if (debugfs_read_inode(ino, &set_inode, argv[1])) | |
392 | return; | |
393 | ||
394 | if (ss->func(ss, argv[3]) == 0) { | |
395 | if (debugfs_write_inode(ino, &set_inode, argv[1])) | |
396 | return; | |
397 | } | |
398 | } |