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