tunefs.c revision 1.56 1 /* $NetBSD: tunefs.c,v 1.56 2022/11/17 06:40:39 chs Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tunefs.c 8.3 (Berkeley) 5/3/95";
41 #else
42 __RCSID("$NetBSD: tunefs.c,v 1.56 2022/11/17 06:40:39 chs Exp $");
43 #endif
44 #endif /* not lint */
45
46 /*
47 * tunefs: change layout parameters to an existing file system.
48 */
49 #include <sys/param.h>
50 #include <sys/statvfs.h>
51
52 #include <ufs/ffs/fs.h>
53 #include <ufs/ffs/ffs_extern.h>
54 #include <ufs/ufs/ufs_wapbl.h>
55 #include <ufs/ufs/ufsmount.h>
56 #include <ufs/ufs/quota2.h>
57
58 #include <machine/bswap.h>
59
60 #include <err.h>
61 #include <errno.h>
62 #include <fcntl.h>
63 #include <fstab.h>
64 #include <paths.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <util.h>
70
71 /* the optimization warning string template */
72 #define OPTWARN "should optimize for %s with minfree %s %d%%"
73
74 union {
75 struct fs sb;
76 char data[MAXBSIZE];
77 } sbun, buf;
78 #define sblock sbun.sb
79
80 int fi;
81 long dev_bsize = 512;
82 int needswap = 0;
83 int is_ufs2 = 0;
84 int extattr = 0;
85 off_t sblockloc;
86 int userquota = 0;
87 int groupquota = 0;
88 #define Q2_EN (1)
89 #define Q2_IGN (0)
90 #define Q2_DIS (-1)
91
92 static off_t sblock_try[] = SBLOCKSEARCH;
93
94 static void bwrite(daddr_t, char *, int, const char *);
95 static void bread(daddr_t, char *, int, const char *);
96 static void change_log_info(long long);
97 static void getsb(struct fs *, const char *);
98 static int openpartition(const char *, int, char *, size_t);
99 static int isactive(int, struct statvfs *);
100 static void show_log_info(void);
101 __dead static void usage(void);
102
103 int
104 main(int argc, char *argv[])
105 {
106 int i, ch, aflag, pflag, Aflag, Fflag, Nflag, openflags;
107 const char *special, *chg[2];
108 char device[MAXPATHLEN];
109 int maxbpg, minfree, optim, secsize;
110 int avgfilesize, avgfpdir, active;
111 long long logfilesize;
112 int secshift, fsbtodb;
113 const char *avalue, *pvalue, *name;
114 struct statvfs sfs;
115
116 aflag = pflag = Aflag = Fflag = Nflag = 0;
117 avalue = pvalue = NULL;
118 maxbpg = minfree = optim = secsize = -1;
119 avgfilesize = avgfpdir = -1;
120 logfilesize = -1;
121 secshift = 0;
122 chg[FS_OPTSPACE] = "space";
123 chg[FS_OPTTIME] = "time";
124
125 while ((ch = getopt(argc, argv, "AFNa:e:g:h:l:m:o:p:q:S:")) != -1) {
126 switch (ch) {
127
128 case 'A':
129 Aflag++;
130 break;
131
132 case 'F':
133 Fflag++;
134 break;
135
136 case 'N':
137 Nflag++;
138 break;
139
140 case 'a':
141 name = "ACLs";
142 avalue = optarg;
143 if (strcmp(avalue, "enable") &&
144 strcmp(avalue, "disable")) {
145 errx(10, "bad %s (options are %s)",
146 name, "`enable' or `disable'");
147 }
148 aflag = 1;
149 break;
150
151 case 'e':
152 maxbpg = strsuftoll(
153 "maximum blocks per file in a cylinder group",
154 optarg, 1, INT_MAX);
155 break;
156
157 case 'g':
158 avgfilesize = strsuftoll("average file size", optarg,
159 1, INT_MAX);
160 break;
161
162 case 'h':
163 avgfpdir = strsuftoll(
164 "expected number of files per directory",
165 optarg, 1, INT_MAX);
166 break;
167
168 case 'l':
169 logfilesize = strsuftoll("journal log file size",
170 optarg, 0, INT_MAX);
171 break;
172
173 case 'm':
174 minfree = strsuftoll("minimum percentage of free space",
175 optarg, 0, 99);
176 break;
177
178 case 'o':
179 if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
180 optim = FS_OPTSPACE;
181 else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
182 optim = FS_OPTTIME;
183 else
184 errx(10,
185 "bad %s (options are `space' or `time')",
186 "optimization preference");
187 break;
188
189 case 'p':
190 name = "POSIX1e ACLs";
191 pvalue = optarg;
192 if (strcmp(pvalue, "enable") &&
193 strcmp(pvalue, "disable")) {
194 errx(10, "bad %s (options are %s)",
195 name, "`enable' or `disable'");
196 }
197 pflag = 1;
198 break;
199
200 case 'q':
201 if (strcmp(optarg, "user") == 0)
202 userquota = Q2_EN;
203 else if (strcmp(optarg, "group") == 0)
204 groupquota = Q2_EN;
205 else if (strcmp(optarg, "nouser") == 0)
206 userquota = Q2_DIS;
207 else if (strcmp(optarg, "nogroup") == 0)
208 groupquota = Q2_DIS;
209 else
210 errx(11, "invalid quota type %s", optarg);
211 break;
212
213 case 'S':
214 secsize = strsuftoll("physical sector size",
215 optarg, 0, INT_MAX);
216 secshift = ffs(secsize) - 1;
217 if (secsize != 0 && 1 << secshift != secsize)
218 errx(12, "sector size %d is not a power of two", secsize);
219 break;
220
221 default:
222 usage();
223 }
224 }
225 argc -= optind;
226 argv += optind;
227 if (argc != 1)
228 usage();
229
230 special = argv[0];
231 openflags = Nflag ? O_RDONLY : O_RDWR;
232 if (Fflag)
233 fi = open(special, openflags);
234 else {
235 fi = openpartition(special, openflags, device, sizeof(device));
236 special = device;
237 }
238 if (fi == -1)
239 err(1, "%s", special);
240 active = !Fflag && isactive(fi, &sfs);
241 getsb(&sblock, special);
242
243 #define CHANGEVAL(old, new, type, suffix) do \
244 if ((new) != -1) { \
245 if ((new) == (old)) \
246 warnx("%s remains unchanged at %d%s", \
247 (type), (old), (suffix)); \
248 else { \
249 warnx("%s changes from %d%s to %d%s", \
250 (type), (old), (suffix), (new), (suffix)); \
251 (old) = (new); \
252 } \
253 } while (/* CONSTCOND */0)
254
255 warnx("tuning %s", special);
256 CHANGEVAL(sblock.fs_maxbpg, maxbpg,
257 "maximum blocks per file in a cylinder group", "");
258 CHANGEVAL(sblock.fs_minfree, minfree,
259 "minimum percentage of free space", "%");
260 if (minfree != -1) {
261 if (minfree >= MINFREE &&
262 sblock.fs_optim == FS_OPTSPACE)
263 warnx(OPTWARN, "time", ">=", MINFREE);
264 if (minfree < MINFREE &&
265 sblock.fs_optim == FS_OPTTIME)
266 warnx(OPTWARN, "space", "<", MINFREE);
267 }
268 if (optim != -1) {
269 if (sblock.fs_optim == optim) {
270 warnx("%s remains unchanged as %s",
271 "optimization preference",
272 chg[optim]);
273 } else {
274 warnx("%s changes from %s to %s",
275 "optimization preference",
276 chg[sblock.fs_optim], chg[optim]);
277 sblock.fs_optim = optim;
278 if (sblock.fs_minfree >= MINFREE &&
279 optim == FS_OPTSPACE)
280 warnx(OPTWARN, "time", ">=", MINFREE);
281 if (sblock.fs_minfree < MINFREE &&
282 optim == FS_OPTTIME)
283 warnx(OPTWARN, "space", "<", MINFREE);
284 }
285 }
286 if (secsize != -1) {
287 if (secsize == 0) {
288 secsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
289 secshift = ffs(secsize) - 1;
290 }
291
292 if (secshift < DEV_BSHIFT)
293 warnx("sector size must be at least %d", DEV_BSIZE);
294 else if (secshift > sblock.fs_fshift)
295 warnx("sector size %d cannot be larger than fragment size %d",
296 secsize, sblock.fs_fsize);
297 else {
298 fsbtodb = sblock.fs_fshift - secshift;
299 if (fsbtodb == sblock.fs_fsbtodb) {
300 warnx("sector size remains unchanged as %d",
301 sblock.fs_fsize / FFS_FSBTODB(&sblock, 1));
302 } else {
303 warnx("sector size changed from %d to %d",
304 sblock.fs_fsize / FFS_FSBTODB(&sblock, 1),
305 secsize);
306 sblock.fs_fsbtodb = fsbtodb;
307 }
308 }
309 }
310 CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
311 "average file size", "");
312 CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
313 "expected number of files per directory", "");
314
315 if (logfilesize >= 0)
316 change_log_info(logfilesize);
317 if (userquota == Q2_EN || groupquota == Q2_EN)
318 sblock.fs_flags |= FS_DOQUOTA2;
319 if (sblock.fs_flags & FS_DOQUOTA2) {
320 sblock.fs_quota_magic = Q2_HEAD_MAGIC;
321 switch(userquota) {
322 case Q2_EN:
323 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
324 == 0) {
325 printf("enabling user quotas\n");
326 sblock.fs_quota_flags |=
327 FS_Q2_DO_TYPE(USRQUOTA);
328 sblock.fs_quotafile[USRQUOTA] = 0;
329 }
330 break;
331 case Q2_DIS:
332 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA))
333 != 0) {
334 printf("disabling user quotas\n");
335 sblock.fs_quota_flags &=
336 ~FS_Q2_DO_TYPE(USRQUOTA);
337 }
338 }
339 switch(groupquota) {
340 case Q2_EN:
341 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
342 == 0) {
343 printf("enabling group quotas\n");
344 sblock.fs_quota_flags |=
345 FS_Q2_DO_TYPE(GRPQUOTA);
346 sblock.fs_quotafile[GRPQUOTA] = 0;
347 }
348 break;
349 case Q2_DIS:
350 if ((sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
351 != 0) {
352 printf("disabling group quotas\n");
353 sblock.fs_quota_flags &=
354 ~FS_Q2_DO_TYPE(GRPQUOTA);
355 }
356 }
357 }
358 /*
359 * if we disabled all quotas, FS_DOQUOTA2 and associated inode(s) will
360 * be cleared by kernel or fsck.
361 */
362 if (aflag) {
363 name = "ACLs";
364 if (strcmp(avalue, "enable") == 0) {
365 if (is_ufs2 && !extattr) {
366 warnx("%s not supported by this fs", name);
367 } else if (sblock.fs_flags & FS_NFS4ACLS) {
368 warnx("%s remains unchanged as enabled", name);
369 } else if (sblock.fs_flags & FS_POSIX1EACLS) {
370 warnx("%s and POSIX.1e ACLs are mutually "
371 "exclusive", name);
372 } else {
373 sblock.fs_flags |= FS_NFS4ACLS;
374 printf("%s set\n", name);
375 }
376 } else if (strcmp(avalue, "disable") == 0) {
377 if ((~sblock.fs_flags & FS_NFS4ACLS) == FS_NFS4ACLS) {
378 warnx("%s remains unchanged as disabled",
379 name);
380 } else {
381 sblock.fs_flags &= ~FS_NFS4ACLS;
382 printf("%s cleared\n", name);
383 }
384 }
385 }
386
387 if (pflag) {
388 name = "POSIX1e ACLs";
389 if (strcmp(pvalue, "enable") == 0) {
390 if (is_ufs2 && !extattr) {
391 warnx("%s not supported by this fs", name);
392 } else if (sblock.fs_flags & FS_POSIX1EACLS) {
393 warnx("%s remains unchanged as enabled", name);
394 } else if (sblock.fs_flags & FS_NFS4ACLS) {
395 warnx("%s and ACLs are mutually "
396 "exclusive", name);
397 } else {
398 sblock.fs_flags |= FS_POSIX1EACLS;
399 printf("%s set\n", name);
400 }
401 } else if (strcmp(pvalue, "disable") == 0) {
402 if ((~sblock.fs_flags & FS_POSIX1EACLS) ==
403 FS_POSIX1EACLS) {
404 warnx("%s remains unchanged as disabled",
405 name);
406 } else {
407 sblock.fs_flags &= ~FS_POSIX1EACLS;
408 printf("%s cleared\n", name);
409 }
410 }
411 }
412
413 if (Nflag) {
414 printf("%s: current settings of %s\n", getprogname(), special);
415 printf("\tmaximum contiguous block count %d\n",
416 sblock.fs_maxcontig);
417 printf("\tmaximum blocks per file in a cylinder group %d\n",
418 sblock.fs_maxbpg);
419 printf("\tminimum percentage of free space %d%%\n",
420 sblock.fs_minfree);
421 printf("\toptimization preference: %s\n", chg[sblock.fs_optim]);
422 printf("\taverage file size: %d\n", sblock.fs_avgfilesize);
423 printf("\texpected number of files per directory: %d\n",
424 sblock.fs_avgfpdir);
425 show_log_info();
426 printf("\tquotas");
427 if (sblock.fs_flags & FS_DOQUOTA2) {
428 if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(USRQUOTA)) {
429 printf(" user");
430 if (sblock.fs_quota_flags &
431 FS_Q2_DO_TYPE(GRPQUOTA))
432 printf(",");
433 }
434 if (sblock.fs_quota_flags & FS_Q2_DO_TYPE(GRPQUOTA))
435 printf(" group");
436 printf(" enabled\n");
437 } else {
438 printf(" disabled\n");
439 }
440 printf("\tPOSIX.1e ACLs %s\n",
441 (sblock.fs_flags & FS_POSIX1EACLS) ? "enabled" : "disabled");
442 printf("\tNFS4 ACLs %s\n",
443 (sblock.fs_flags & FS_NFS4ACLS) ? "enabled" : "disabled");
444 printf("%s: no changes made\n", getprogname());
445 return 0;
446 }
447
448 memcpy(&buf, (char *)&sblock, SBLOCKSIZE);
449 if (needswap)
450 ffs_sb_swap((struct fs*)&buf, (struct fs*)&buf);
451
452 /* write superblock to original coordinates (use old dev_bsize!) */
453 bwrite(sblockloc, buf.data, SBLOCKSIZE, special);
454
455 if (active) {
456 struct ufs_args args;
457 args.fspec = sfs.f_mntfromname;
458 if (mount(MOUNT_FFS, sfs.f_mntonname, sfs.f_flag | MNT_UPDATE,
459 &args, sizeof args) == -1)
460 warn("mount");
461 else
462 printf("%s: mount of %s on %s updated\n",
463 getprogname(), sfs.f_mntfromname, sfs.f_mntonname);
464 }
465
466
467 /* correct dev_bsize from possibly changed superblock data */
468 dev_bsize = sblock.fs_fsize / FFS_FSBTODB(&sblock, 1);
469
470 if (Aflag)
471 for (i = 0; i < sblock.fs_ncg; i++)
472 bwrite(FFS_FSBTODB(&sblock, cgsblock(&sblock, i)),
473 buf.data, SBLOCKSIZE, special);
474 close(fi);
475 exit(0);
476 }
477
478 static int
479 isactive(int fd, struct statvfs *rsfs)
480 {
481 struct stat st0, st;
482 struct statvfs *sfs;
483 int n;
484
485 if (fstat(fd, &st0) == -1) {
486 warn("stat");
487 return 0;
488 }
489
490 if ((n = getmntinfo(&sfs, 0)) == -1) {
491 warn("getmntinfo");
492 return 0;
493 }
494
495 for (int i = 0; i < n; i++) {
496 if (stat(sfs[i].f_mntfromname, &st) == -1)
497 continue;
498 if (st.st_rdev != st0.st_rdev)
499 continue;
500 *rsfs = sfs[i];
501 return 1;
502
503 }
504 return 0;
505 }
506
507 static void
508 show_log_info(void)
509 {
510 const char *loc;
511 uint64_t size, blksize, logsize;
512 int print;
513
514 switch (sblock.fs_journal_location) {
515 case UFS_WAPBL_JOURNALLOC_NONE:
516 print = blksize = 0;
517 /* nothing */
518 break;
519 case UFS_WAPBL_JOURNALLOC_END_PARTITION:
520 loc = "end of partition";
521 size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT];
522 blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
523 print = 1;
524 break;
525 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
526 loc = "in filesystem";
527 size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT];
528 blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
529 print = 1;
530 break;
531 default:
532 loc = "unknown";
533 size = blksize = 0;
534 print = 1;
535 break;
536 }
537
538 if (print) {
539 logsize = size * blksize;
540
541 printf("\tjournal log file location: %s\n", loc);
542 printf("\tjournal log file size: ");
543 if (logsize == 0)
544 printf("0\n");
545 else {
546 char sizebuf[8];
547 humanize_number(sizebuf, 6, size * blksize, "B",
548 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
549 printf("%s (%" PRId64 " bytes)", sizebuf, logsize);
550 }
551 printf("\n");
552 printf("\tjournal log flags:");
553 if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG)
554 printf(" create-log");
555 if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG)
556 printf(" clear-log");
557 printf("\n");
558 }
559 }
560
561 static void
562 change_log_info(long long logfilesize)
563 {
564 /*
565 * NOTES:
566 * - only operate on in-filesystem log sizes
567 * - can't change size of existing log
568 * - if current is same, no action
569 * - if current is zero and new is non-zero, set flag to create log
570 * on next mount
571 * - if current is non-zero and new is zero, set flag to clear log
572 * on next mount
573 */
574 int in_fs_log;
575 uint64_t old_size;
576
577 old_size = 0;
578 switch (sblock.fs_journal_location) {
579 case UFS_WAPBL_JOURNALLOC_END_PARTITION:
580 in_fs_log = 0;
581 old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] *
582 sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
583 break;
584
585 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
586 in_fs_log = 1;
587 old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] *
588 sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
589 break;
590
591 case UFS_WAPBL_JOURNALLOC_NONE:
592 default:
593 in_fs_log = 0;
594 old_size = 0;
595 break;
596 }
597
598 if (logfilesize == 0) {
599 /*
600 * Don't clear out the locators - the kernel might need
601 * these to find the log! Just set the "clear the log"
602 * flag and let the kernel do the rest.
603 */
604 sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG;
605 sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG;
606 warnx("log file size cleared from %" PRIu64 "", old_size);
607 return;
608 }
609
610 if (!in_fs_log && logfilesize > 0 && old_size > 0)
611 errx(1, "Can't change size of non-in-filesystem log");
612
613 if (old_size == (uint64_t)logfilesize && logfilesize > 0) {
614 /* no action */
615 warnx("log file size remains unchanged at %lld", logfilesize);
616 return;
617 }
618
619 if (old_size == 0) {
620 /* create new log of desired size next mount */
621 sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
622 sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0;
623 sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize;
624 sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0;
625 sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0;
626 sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG;
627 sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG;
628 warnx("log file size set to %lld", logfilesize);
629 } else {
630 errx(1,
631 "Can't change existing log size from %" PRIu64 " to %lld",
632 old_size, logfilesize);
633 }
634 }
635
636 static void
637 usage(void)
638 {
639
640 fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
641 fprintf(stderr, "where tuneup-options are:\n");
642 fprintf(stderr, "\t-a ACLS: `enable' or `disable'\n");
643 fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
644 fprintf(stderr, "\t-g average file size\n");
645 fprintf(stderr, "\t-h expected number of files per directory\n");
646 fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n");
647 fprintf(stderr, "\t-m minimum percentage of free space\n");
648 fprintf(stderr, "\t-p POSIX.1e ACLS: `enable' or `disable'\n");
649 fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
650 fprintf(stderr, "\t-q quota type (`[no]user' or `[no]group')\n");
651 fprintf(stderr, "\t-S sector size\n");
652 exit(2);
653 }
654
655 static void
656 getsb(struct fs *fs, const char *file)
657 {
658 int i;
659
660 for (i = 0; ; i++) {
661 if (sblock_try[i] == -1)
662 errx(5, "cannot find filesystem superblock");
663 bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
664 switch(fs->fs_magic) {
665 case FS_UFS2EA_MAGIC:
666 extattr = 1;
667 /*FALLTHROUGH*/
668 case FS_UFS2_MAGIC:
669 is_ufs2 = 1;
670 /*FALLTHROUGH*/
671 case FS_UFS1_MAGIC:
672 break;
673 case FS_UFS2EA_MAGIC_SWAPPED:
674 extattr = 1;
675 /*FALLTHROUGH*/
676 case FS_UFS2_MAGIC_SWAPPED:
677 is_ufs2 = 1;
678 /*FALLTHROUGH*/
679 case FS_UFS1_MAGIC_SWAPPED:
680 warnx("%s: swapping byte order", file);
681 needswap = 1;
682 ffs_sb_swap(fs, fs);
683 break;
684 default:
685 continue;
686 }
687 if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
688 continue;
689 if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED)
690 && fs->fs_sblockloc != sblock_try[i])
691 continue;
692 break;
693 }
694
695 dev_bsize = fs->fs_fsize / FFS_FSBTODB(fs, 1);
696 sblockloc = sblock_try[i] / dev_bsize;
697 }
698
699 static void
700 bwrite(daddr_t blk, char *buffer, int size, const char *file)
701 {
702 off_t offset;
703
704 offset = (off_t)blk * dev_bsize;
705 if (lseek(fi, offset, SEEK_SET) == -1)
706 err(6, "%s: seeking to %lld", file, (long long)offset);
707 if (write(fi, buffer, size) != size)
708 err(7, "%s: writing %d bytes", file, size);
709 }
710
711 static void
712 bread(daddr_t blk, char *buffer, int cnt, const char *file)
713 {
714 off_t offset;
715 int i;
716
717 offset = (off_t)blk * dev_bsize;
718 if (lseek(fi, offset, SEEK_SET) == -1)
719 err(4, "%s: seeking to %lld", file, (long long)offset);
720 if ((i = read(fi, buffer, cnt)) != cnt)
721 errx(5, "%s: short read", file);
722 }
723
724 static int
725 openpartition(const char *name, int flags, char *device, size_t devicelen)
726 {
727 char specname[MAXPATHLEN];
728 char rawname[MAXPATHLEN];
729 const char *special, *raw;
730 struct fstab *fs;
731 int fd, oerrno;
732
733 fs = getfsfile(name);
734 special = fs ? fs->fs_spec : name;
735
736 raw = getfsspecname(specname, sizeof(specname), special);
737 if (raw == NULL)
738 err(1, "%s: %s", name, specname);
739 special = getdiskrawname(rawname, sizeof(rawname), raw);
740 if (special == NULL)
741 special = raw;
742
743 fd = opendisk(special, flags, device, devicelen, 0);
744 if (fd == -1 && errno == ENOENT) {
745 oerrno = errno;
746 strlcpy(device, special, devicelen);
747 errno = oerrno;
748 }
749 return (fd);
750 }
751