tunefs.c revision 1.42 1 1.42 bouyer /* $NetBSD: tunefs.c,v 1.42 2009/09/13 18:30:30 bouyer 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.42 bouyer __RCSID("$NetBSD: tunefs.c,v 1.42 2009/09/13 18:30:30 bouyer 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.18 bouyer
55 1.18 bouyer #include <machine/bswap.h>
56 1.8 mycroft
57 1.22 lukem #include <err.h>
58 1.8 mycroft #include <errno.h>
59 1.6 cgd #include <fcntl.h>
60 1.1 cgd #include <fstab.h>
61 1.22 lukem #include <paths.h>
62 1.1 cgd #include <stdio.h>
63 1.6 cgd #include <stdlib.h>
64 1.14 thorpej #include <string.h>
65 1.6 cgd #include <unistd.h>
66 1.25 lukem #include <util.h>
67 1.6 cgd
68 1.6 cgd /* the optimization warning string template */
69 1.8 mycroft #define OPTWARN "should optimize for %s with minfree %s %d%%"
70 1.1 cgd
71 1.1 cgd union {
72 1.1 cgd struct fs sb;
73 1.1 cgd char pad[MAXBSIZE];
74 1.1 cgd } sbun;
75 1.1 cgd #define sblock sbun.sb
76 1.13 bouyer char buf[MAXBSIZE];
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.27 fvdl
84 1.27 fvdl static off_t sblock_try[] = SBLOCKSEARCH;
85 1.22 lukem
86 1.25 lukem static void bwrite(daddr_t, char *, int, const char *);
87 1.25 lukem static void bread(daddr_t, char *, int, const char *);
88 1.35 simonb static void change_log_info(long long);
89 1.22 lukem static void getsb(struct fs *, const char *);
90 1.26 lukem static int openpartition(const char *, int, char *, size_t);
91 1.35 simonb static void show_log_info(void);
92 1.22 lukem static void usage(void);
93 1.6 cgd
94 1.6 cgd int
95 1.22 lukem main(int argc, char *argv[])
96 1.1 cgd {
97 1.25 lukem int i, ch, Aflag, Fflag, Nflag, openflags;
98 1.22 lukem const char *special, *chg[2];
99 1.26 lukem char device[MAXPATHLEN];
100 1.27 fvdl int maxbpg, minfree, optim;
101 1.24 lukem int avgfilesize, avgfpdir;
102 1.35 simonb long long logfilesize;
103 1.22 lukem
104 1.22 lukem Aflag = Fflag = Nflag = 0;
105 1.27 fvdl maxbpg = minfree = optim = -1;
106 1.24 lukem avgfilesize = avgfpdir = -1;
107 1.35 simonb logfilesize = -1;
108 1.22 lukem chg[FS_OPTSPACE] = "space";
109 1.22 lukem chg[FS_OPTTIME] = "time";
110 1.22 lukem
111 1.38 ad while ((ch = getopt(argc, argv, "AFNe:g:h:l:m:o:")) != -1) {
112 1.22 lukem switch (ch) {
113 1.22 lukem
114 1.22 lukem case 'A':
115 1.22 lukem Aflag++;
116 1.22 lukem break;
117 1.22 lukem
118 1.22 lukem case 'F':
119 1.22 lukem Fflag++;
120 1.22 lukem break;
121 1.22 lukem
122 1.22 lukem case 'N':
123 1.22 lukem Nflag++;
124 1.22 lukem break;
125 1.22 lukem
126 1.22 lukem case 'e':
127 1.35 simonb maxbpg = strsuftoll(
128 1.22 lukem "maximum blocks per file in a cylinder group",
129 1.35 simonb optarg, 1, INT_MAX);
130 1.22 lukem break;
131 1.22 lukem
132 1.24 lukem case 'g':
133 1.35 simonb avgfilesize = strsuftoll("average file size", optarg,
134 1.35 simonb 1, INT_MAX);
135 1.24 lukem break;
136 1.24 lukem
137 1.24 lukem case 'h':
138 1.35 simonb avgfpdir = strsuftoll(
139 1.24 lukem "expected number of files per directory",
140 1.35 simonb optarg, 1, INT_MAX);
141 1.35 simonb break;
142 1.35 simonb
143 1.35 simonb case 'l':
144 1.35 simonb logfilesize = strsuftoll("journal log file size",
145 1.35 simonb optarg, 0, INT_MAX);
146 1.24 lukem break;
147 1.22 lukem
148 1.22 lukem case 'm':
149 1.35 simonb minfree = strsuftoll("minimum percentage of free space",
150 1.35 simonb optarg, 0, 99);
151 1.22 lukem break;
152 1.22 lukem
153 1.22 lukem case 'o':
154 1.22 lukem if (strcmp(optarg, chg[FS_OPTSPACE]) == 0)
155 1.22 lukem optim = FS_OPTSPACE;
156 1.22 lukem else if (strcmp(optarg, chg[FS_OPTTIME]) == 0)
157 1.22 lukem optim = FS_OPTTIME;
158 1.22 lukem else
159 1.22 lukem errx(10,
160 1.22 lukem "bad %s (options are `space' or `time')",
161 1.22 lukem "optimization preference");
162 1.22 lukem break;
163 1.22 lukem
164 1.22 lukem default:
165 1.22 lukem usage();
166 1.22 lukem }
167 1.22 lukem }
168 1.22 lukem argc -= optind;
169 1.22 lukem argv += optind;
170 1.22 lukem if (argc != 1)
171 1.6 cgd usage();
172 1.22 lukem
173 1.22 lukem special = argv[0];
174 1.25 lukem openflags = Nflag ? O_RDONLY : O_RDWR;
175 1.26 lukem if (Fflag)
176 1.25 lukem fi = open(special, openflags);
177 1.26 lukem else {
178 1.26 lukem fi = openpartition(special, openflags, device, sizeof(device));
179 1.25 lukem special = device;
180 1.1 cgd }
181 1.26 lukem if (fi == -1)
182 1.26 lukem err(1, "%s", special);
183 1.1 cgd getsb(&sblock, special);
184 1.22 lukem
185 1.23 lukem #define CHANGEVAL(old, new, type, suffix) do \
186 1.23 lukem if ((new) != -1) { \
187 1.23 lukem if ((new) == (old)) \
188 1.23 lukem warnx("%s remains unchanged at %d%s", \
189 1.23 lukem (type), (old), (suffix)); \
190 1.23 lukem else { \
191 1.23 lukem warnx("%s changes from %d%s to %d%s", \
192 1.23 lukem (type), (old), (suffix), (new), (suffix)); \
193 1.23 lukem (old) = (new); \
194 1.23 lukem } \
195 1.23 lukem } while (/* CONSTCOND */0)
196 1.23 lukem
197 1.25 lukem warnx("tuning %s", special);
198 1.23 lukem CHANGEVAL(sblock.fs_maxbpg, maxbpg,
199 1.23 lukem "maximum blocks per file in a cylinder group", "");
200 1.23 lukem CHANGEVAL(sblock.fs_minfree, minfree,
201 1.23 lukem "minimum percentage of free space", "%");
202 1.22 lukem if (minfree != -1) {
203 1.22 lukem if (minfree >= MINFREE &&
204 1.22 lukem sblock.fs_optim == FS_OPTSPACE)
205 1.22 lukem warnx(OPTWARN, "time", ">=", MINFREE);
206 1.22 lukem if (minfree < MINFREE &&
207 1.22 lukem sblock.fs_optim == FS_OPTTIME)
208 1.22 lukem warnx(OPTWARN, "space", "<", MINFREE);
209 1.22 lukem }
210 1.22 lukem if (optim != -1) {
211 1.22 lukem if (sblock.fs_optim == optim) {
212 1.22 lukem warnx("%s remains unchanged as %s",
213 1.22 lukem "optimization preference",
214 1.22 lukem chg[optim]);
215 1.22 lukem } else {
216 1.22 lukem warnx("%s changes from %s to %s",
217 1.22 lukem "optimization preference",
218 1.22 lukem chg[sblock.fs_optim], chg[optim]);
219 1.22 lukem sblock.fs_optim = optim;
220 1.22 lukem if (sblock.fs_minfree >= MINFREE &&
221 1.22 lukem optim == FS_OPTSPACE)
222 1.22 lukem warnx(OPTWARN, "time", ">=", MINFREE);
223 1.22 lukem if (sblock.fs_minfree < MINFREE &&
224 1.22 lukem optim == FS_OPTTIME)
225 1.22 lukem warnx(OPTWARN, "space", "<", MINFREE);
226 1.22 lukem }
227 1.22 lukem }
228 1.24 lukem CHANGEVAL(sblock.fs_avgfilesize, avgfilesize,
229 1.24 lukem "average file size", "");
230 1.24 lukem CHANGEVAL(sblock.fs_avgfpdir, avgfpdir,
231 1.24 lukem "expected number of files per directory", "");
232 1.1 cgd
233 1.35 simonb if (logfilesize >= 0)
234 1.35 simonb change_log_info(logfilesize);
235 1.35 simonb
236 1.12 lukem if (Nflag) {
237 1.37 simonb printf("tunefs: current settings of %s\n", special);
238 1.37 simonb printf("\tmaximum contiguous block count %d\n",
239 1.12 lukem sblock.fs_maxcontig);
240 1.37 simonb printf("\tmaximum blocks per file in a cylinder group %d\n",
241 1.12 lukem sblock.fs_maxbpg);
242 1.37 simonb printf("\tminimum percentage of free space %d%%\n",
243 1.12 lukem sblock.fs_minfree);
244 1.37 simonb printf("\toptimization preference: %s\n", chg[sblock.fs_optim]);
245 1.37 simonb printf("\taverage file size: %d\n", sblock.fs_avgfilesize);
246 1.37 simonb printf("\texpected number of files per directory: %d\n",
247 1.24 lukem sblock.fs_avgfpdir);
248 1.35 simonb show_log_info();
249 1.37 simonb printf("tunefs: no changes made\n");
250 1.12 lukem exit(0);
251 1.12 lukem }
252 1.22 lukem
253 1.27 fvdl memcpy(buf, (char *)&sblock, SBLOCKSIZE);
254 1.13 bouyer if (needswap)
255 1.21 lukem ffs_sb_swap((struct fs*)buf, (struct fs*)buf);
256 1.27 fvdl bwrite(sblockloc, buf, SBLOCKSIZE, special);
257 1.1 cgd if (Aflag)
258 1.1 cgd for (i = 0; i < sblock.fs_ncg; i++)
259 1.1 cgd bwrite(fsbtodb(&sblock, cgsblock(&sblock, i)),
260 1.27 fvdl buf, SBLOCKSIZE, special);
261 1.1 cgd close(fi);
262 1.1 cgd exit(0);
263 1.6 cgd }
264 1.6 cgd
265 1.35 simonb static void
266 1.35 simonb show_log_info(void)
267 1.35 simonb {
268 1.35 simonb const char *loc;
269 1.36 simonb uint64_t size, blksize, logsize;
270 1.35 simonb int print;
271 1.35 simonb
272 1.35 simonb switch (sblock.fs_journal_location) {
273 1.35 simonb case UFS_WAPBL_JOURNALLOC_NONE:
274 1.35 simonb print = blksize = 0;
275 1.35 simonb /* nothing */
276 1.35 simonb break;
277 1.35 simonb case UFS_WAPBL_JOURNALLOC_END_PARTITION:
278 1.35 simonb loc = "end of partition";
279 1.35 simonb size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT];
280 1.35 simonb blksize = sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
281 1.35 simonb print = 1;
282 1.35 simonb break;
283 1.35 simonb case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
284 1.35 simonb loc = "in filesystem";
285 1.35 simonb size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT];
286 1.35 simonb blksize = sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
287 1.35 simonb print = 1;
288 1.35 simonb break;
289 1.35 simonb default:
290 1.35 simonb loc = "unknown";
291 1.35 simonb size = blksize = 0;
292 1.35 simonb print = 1;
293 1.35 simonb break;
294 1.35 simonb }
295 1.35 simonb
296 1.35 simonb if (print) {
297 1.36 simonb logsize = size * blksize;
298 1.36 simonb
299 1.37 simonb printf("\tjournal log file location: %s\n", loc);
300 1.37 simonb printf("\tjournal log file size: ");
301 1.36 simonb if (logsize == 0)
302 1.37 simonb printf("0\n");
303 1.36 simonb else {
304 1.36 simonb char sizebuf[8];
305 1.36 simonb humanize_number(sizebuf, 6, size * blksize, "B",
306 1.36 simonb HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
307 1.37 simonb printf("%s (%" PRId64 " bytes)", sizebuf, logsize);
308 1.36 simonb }
309 1.37 simonb printf("\n");
310 1.37 simonb printf("\tjournal log flags:");
311 1.35 simonb if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CREATE_LOG)
312 1.40 bouyer printf(" create-log");
313 1.35 simonb if (sblock.fs_journal_flags & UFS_WAPBL_FLAGS_CLEAR_LOG)
314 1.37 simonb printf(" clear-log");
315 1.37 simonb printf("\n");
316 1.35 simonb }
317 1.35 simonb }
318 1.35 simonb
319 1.35 simonb static void
320 1.35 simonb change_log_info(long long logfilesize)
321 1.6 cgd {
322 1.35 simonb /*
323 1.35 simonb * NOTES:
324 1.35 simonb * - only operate on in-filesystem log sizes
325 1.35 simonb * - can't change size of existing log
326 1.35 simonb * - if current is same, no action
327 1.35 simonb * - if current is zero and new is non-zero, set flag to create log
328 1.35 simonb * on next mount
329 1.35 simonb * - if current is non-zero and new is zero, set flag to clear log
330 1.35 simonb * on next mount
331 1.35 simonb */
332 1.35 simonb int in_fs_log;
333 1.35 simonb uint64_t old_size;
334 1.35 simonb
335 1.35 simonb old_size = 0;
336 1.35 simonb switch (sblock.fs_journal_location) {
337 1.35 simonb case UFS_WAPBL_JOURNALLOC_END_PARTITION:
338 1.35 simonb in_fs_log = 0;
339 1.35 simonb old_size = sblock.fs_journallocs[UFS_WAPBL_EPART_COUNT] *
340 1.35 simonb sblock.fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
341 1.35 simonb break;
342 1.35 simonb
343 1.35 simonb case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
344 1.35 simonb in_fs_log = 1;
345 1.35 simonb old_size = sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] *
346 1.35 simonb sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
347 1.35 simonb break;
348 1.35 simonb
349 1.35 simonb case UFS_WAPBL_JOURNALLOC_NONE:
350 1.35 simonb default:
351 1.35 simonb in_fs_log = 0;
352 1.35 simonb old_size = 0;
353 1.35 simonb break;
354 1.35 simonb }
355 1.35 simonb
356 1.35 simonb if (logfilesize == 0) {
357 1.35 simonb /*
358 1.35 simonb * Don't clear out the locators - the kernel might need
359 1.35 simonb * these to find the log! Just set the "clear the log"
360 1.35 simonb * flag and let the kernel do the rest.
361 1.35 simonb */
362 1.35 simonb sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CLEAR_LOG;
363 1.35 simonb sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CREATE_LOG;
364 1.35 simonb warnx("log file size cleared from %" PRIu64 "", old_size);
365 1.35 simonb return;
366 1.35 simonb }
367 1.8 mycroft
368 1.41 bouyer if (!in_fs_log && logfilesize > 0 && old_size > 0)
369 1.41 bouyer errx(1, "Can't change size of non-in-filesystem log");
370 1.41 bouyer
371 1.42 bouyer if (old_size == (uint64_t)logfilesize && logfilesize > 0) {
372 1.41 bouyer /* no action */
373 1.41 bouyer warnx("log file size remains unchanged at %lld", logfilesize);
374 1.41 bouyer return;
375 1.41 bouyer }
376 1.41 bouyer
377 1.35 simonb if (old_size == 0) {
378 1.35 simonb /* create new log of desired size next mount */
379 1.35 simonb sblock.fs_journal_location = UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM;
380 1.35 simonb sblock.fs_journallocs[UFS_WAPBL_INFS_ADDR] = 0;
381 1.35 simonb sblock.fs_journallocs[UFS_WAPBL_INFS_COUNT] = logfilesize;
382 1.35 simonb sblock.fs_journallocs[UFS_WAPBL_INFS_BLKSZ] = 0;
383 1.35 simonb sblock.fs_journallocs[UFS_WAPBL_INFS_INO] = 0;
384 1.35 simonb sblock.fs_journal_flags |= UFS_WAPBL_FLAGS_CREATE_LOG;
385 1.35 simonb sblock.fs_journal_flags &= ~UFS_WAPBL_FLAGS_CLEAR_LOG;
386 1.35 simonb warnx("log file size set to %lld", logfilesize);
387 1.35 simonb } else {
388 1.35 simonb errx(1,
389 1.35 simonb "Can't change existing log size from %" PRIu64 " to %lld",
390 1.35 simonb old_size, logfilesize);
391 1.35 simonb }
392 1.22 lukem }
393 1.22 lukem
394 1.22 lukem static void
395 1.22 lukem usage(void)
396 1.22 lukem {
397 1.22 lukem
398 1.29 jmmv fprintf(stderr, "usage: tunefs [-AFN] tuneup-options special-device\n");
399 1.1 cgd fprintf(stderr, "where tuneup-options are:\n");
400 1.1 cgd fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
401 1.24 lukem fprintf(stderr, "\t-g average file size\n");
402 1.24 lukem fprintf(stderr, "\t-h expected number of files per directory\n");
403 1.35 simonb fprintf(stderr, "\t-l journal log file size (`0' to clear journal)\n");
404 1.1 cgd fprintf(stderr, "\t-m minimum percentage of free space\n");
405 1.23 lukem fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
406 1.8 mycroft exit(2);
407 1.1 cgd }
408 1.1 cgd
409 1.22 lukem static void
410 1.22 lukem getsb(struct fs *fs, const char *file)
411 1.1 cgd {
412 1.27 fvdl int i;
413 1.1 cgd
414 1.30 dsl for (i = 0; ; i++) {
415 1.30 dsl if (sblock_try[i] == -1)
416 1.30 dsl errx(5, "cannot find filesystem superblock");
417 1.27 fvdl bread(sblock_try[i] / dev_bsize, (char *)fs, SBLOCKSIZE, file);
418 1.27 fvdl switch(fs->fs_magic) {
419 1.27 fvdl case FS_UFS2_MAGIC:
420 1.27 fvdl is_ufs2 = 1;
421 1.27 fvdl /*FALLTHROUGH*/
422 1.27 fvdl case FS_UFS1_MAGIC:
423 1.30 dsl break;
424 1.27 fvdl case FS_UFS2_MAGIC_SWAPPED:
425 1.27 fvdl is_ufs2 = 1;
426 1.27 fvdl /*FALLTHROUGH*/
427 1.27 fvdl case FS_UFS1_MAGIC_SWAPPED:
428 1.22 lukem warnx("%s: swapping byte order", file);
429 1.13 bouyer needswap = 1;
430 1.21 lukem ffs_sb_swap(fs, fs);
431 1.30 dsl break;
432 1.27 fvdl default:
433 1.30 dsl continue;
434 1.27 fvdl }
435 1.30 dsl if (!is_ufs2 && sblock_try[i] == SBLOCK_UFS2)
436 1.30 dsl continue;
437 1.31 dsl if ((is_ufs2 || fs->fs_old_flags & FS_FLAGS_UPDATED)
438 1.30 dsl && fs->fs_sblockloc != sblock_try[i])
439 1.30 dsl continue;
440 1.30 dsl break;
441 1.17 ross }
442 1.30 dsl
443 1.1 cgd dev_bsize = fs->fs_fsize / fsbtodb(fs, 1);
444 1.27 fvdl sblockloc = sblock_try[i] / dev_bsize;
445 1.1 cgd }
446 1.1 cgd
447 1.22 lukem static void
448 1.25 lukem bwrite(daddr_t blk, char *buffer, int size, const char *file)
449 1.1 cgd {
450 1.25 lukem off_t offset;
451 1.8 mycroft
452 1.25 lukem offset = (off_t)blk * dev_bsize;
453 1.25 lukem if (lseek(fi, offset, SEEK_SET) == -1)
454 1.25 lukem err(6, "%s: seeking to %lld", file, (long long)offset);
455 1.22 lukem if (write(fi, buffer, size) != size)
456 1.25 lukem err(7, "%s: writing %d bytes", file, size);
457 1.1 cgd }
458 1.1 cgd
459 1.25 lukem static void
460 1.25 lukem bread(daddr_t blk, char *buffer, int cnt, const char *file)
461 1.1 cgd {
462 1.25 lukem off_t offset;
463 1.25 lukem int i;
464 1.1 cgd
465 1.25 lukem offset = (off_t)blk * dev_bsize;
466 1.25 lukem if (lseek(fi, offset, SEEK_SET) == -1)
467 1.25 lukem err(4, "%s: seeking to %lld", file, (long long)offset);
468 1.25 lukem if ((i = read(fi, buffer, cnt)) != cnt)
469 1.25 lukem errx(5, "%s: short read", file);
470 1.26 lukem }
471 1.26 lukem
472 1.26 lukem static int
473 1.26 lukem openpartition(const char *name, int flags, char *device, size_t devicelen)
474 1.26 lukem {
475 1.26 lukem char rawspec[MAXPATHLEN], *p;
476 1.26 lukem struct fstab *fs;
477 1.26 lukem int fd, oerrno;
478 1.26 lukem
479 1.26 lukem fs = getfsfile(name);
480 1.26 lukem if (fs) {
481 1.26 lukem if ((p = strrchr(fs->fs_spec, '/')) != NULL) {
482 1.26 lukem snprintf(rawspec, sizeof(rawspec), "%.*s/r%s",
483 1.26 lukem (int)(p - fs->fs_spec), fs->fs_spec, p + 1);
484 1.26 lukem name = rawspec;
485 1.26 lukem } else
486 1.26 lukem name = fs->fs_spec;
487 1.26 lukem }
488 1.26 lukem fd = opendisk(name, flags, device, devicelen, 0);
489 1.26 lukem if (fd == -1 && errno == ENOENT) {
490 1.26 lukem oerrno = errno;
491 1.26 lukem strlcpy(device, name, devicelen);
492 1.26 lukem errno = oerrno;
493 1.26 lukem }
494 1.26 lukem return (fd);
495 1.1 cgd }
496