]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/newfs/newfs.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sbin / newfs / newfs.c
1 /*
2  * Copyright (c) 2002 Networks Associates Technology, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by Marshall
6  * Kirk McKusick and Network Associates Laboratories, the Security
7  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
8  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
9  * research program.
10  *
11  * Copyright (c) 1983, 1989, 1993, 1994
12  *      The Regents of the University of California.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38
39 #if 0
40 #ifndef lint
41 static const char copyright[] =
42 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
43         The Regents of the University of California.  All rights reserved.\n";
44 #endif /* not lint */
45
46 #ifndef lint
47 static char sccsid[] = "@(#)newfs.c     8.13 (Berkeley) 5/1/95";
48 #endif /* not lint */
49 #endif
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
52
53 /*
54  * newfs: friendly front end to mkfs
55  */
56 #include <sys/param.h>
57 #include <sys/stat.h>
58 #include <sys/disk.h>
59 #include <sys/disklabel.h>
60 #include <sys/file.h>
61 #include <sys/mount.h>
62
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ufs/ufsmount.h>
67
68 #include <ctype.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <inttypes.h>
72 #include <paths.h>
73 #include <stdarg.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <syslog.h>
78 #include <unistd.h>
79
80 #include "newfs.h"
81
82 /*
83  * The following two constants set the default block and fragment sizes.
84  * Both constants must be a power of 2 and meet the following constraints:
85  *      MINBSIZE <= DESBLKSIZE <= MAXBSIZE
86  *      sectorsize <= DESFRAGSIZE <= DESBLKSIZE
87  *      DESBLKSIZE / DESFRAGSIZE <= 8
88  */
89 #define DFL_FRAGSIZE    2048
90 #define DFL_BLKSIZE     16384
91
92 /*
93  * Cylinder groups may have up to MAXBLKSPERCG blocks. The actual
94  * number used depends upon how much information can be stored
95  * in a cylinder group map which must fit in a single file system
96  * block. The default is to use as many as possible blocks per group.
97  */
98 #define MAXBLKSPERCG    0x7fffffff      /* desired fs_fpg ("infinity") */
99
100 /*
101  * MAXBLKPG determines the maximum number of data blocks which are
102  * placed in a single cylinder group. The default is one indirect
103  * block worth of data blocks.
104  */
105 #define MAXBLKPG(bsize) ((bsize) / sizeof(ufs2_daddr_t))
106
107 /*
108  * Each file system has a number of inodes statically allocated.
109  * We allocate one inode slot per NFPI fragments, expecting this
110  * to be far more than we will ever need.
111  */
112 #define NFPI            4
113
114 int     Lflag;                  /* add a volume label */
115 int     Nflag;                  /* run without writing file system */
116 int     Oflag = 2;              /* file system format (1 => UFS1, 2 => UFS2) */
117 int     Rflag;                  /* regression test */
118 int     Uflag;                  /* enable soft updates for file system */
119 int     Eflag = 0;              /* exit in middle of newfs for testing */
120 int     Jflag;                  /* enable gjournal for file system */
121 int     lflag;                  /* enable multilabel for file system */
122 int     nflag;                  /* do not create .snap directory */
123 quad_t  fssize;                 /* file system size */
124 int     sectorsize;             /* bytes/sector */
125 int     realsectorsize;         /* bytes/sector in hardware */
126 int     fsize = 0;              /* fragment size */
127 int     bsize = 0;              /* block size */
128 int     maxbsize = 0;           /* maximum clustering */
129 int     maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
130 int     minfree = MINFREE;      /* free space threshold */
131 int     opt = DEFAULTOPT;       /* optimization preference (space or time) */
132 int     density;                /* number of bytes per inode */
133 int     maxcontig = 0;          /* max contiguous blocks to allocate */
134 int     maxbpg;                 /* maximum blocks per file in a cyl group */
135 int     avgfilesize = AVFILESIZ;/* expected average file size */
136 int     avgfilesperdir = AFPDIR;/* expected number of files per directory */
137 u_char  *volumelabel = NULL;    /* volume label for filesystem */
138 struct uufsd disk;              /* libufs disk structure */
139
140 static char     device[MAXPATHLEN];
141 static char     *disktype;
142 static int      unlabeled;
143
144 static struct disklabel *getdisklabel(char *s);
145 static void rewritelabel(char *s, struct disklabel *lp);
146 static void usage(void);
147
148 int
149 main(int argc, char *argv[])
150 {
151         struct partition *pp;
152         struct disklabel *lp;
153         struct partition oldpartition;
154         struct stat st;
155         char *cp, *special;
156         int ch, i;
157         off_t mediasize;
158
159         while ((ch = getopt(argc, argv,
160             "EJL:NO:RS:T:Ua:b:c:d:e:f:g:h:i:lm:no:s:")) != -1)
161                 switch (ch) {
162                 case 'E':
163                         Eflag++;
164                         break;
165                 case 'J':
166                         Jflag = 1;
167                         break;
168                 case 'L':
169                         volumelabel = optarg;
170                         i = -1;
171                         while (isalnum(volumelabel[++i]));
172                         if (volumelabel[i] != '\0') {
173                                 errx(1, "bad volume label. Valid characters are alphanumerics.");
174                         }
175                         if (strlen(volumelabel) >= MAXVOLLEN) {
176                                 errx(1, "bad volume label. Length is longer than %d.",
177                                     MAXVOLLEN);
178                         }
179                         Lflag = 1;
180                         break;
181                 case 'N':
182                         Nflag = 1;
183                         break;
184                 case 'O':
185                         if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
186                                 errx(1, "%s: bad file system format value",
187                                     optarg);
188                         break;
189                 case 'R':
190                         Rflag = 1;
191                         break;
192                 case 'S':
193                         if ((sectorsize = atoi(optarg)) <= 0)
194                                 errx(1, "%s: bad sector size", optarg);
195                         break;
196                 case 'T':
197                         disktype = optarg;
198                         break;
199                 case 'U':
200                         Uflag = 1;
201                         break;
202                 case 'a':
203                         if ((maxcontig = atoi(optarg)) <= 0)
204                                 errx(1, "%s: bad maximum contiguous blocks",
205                                     optarg);
206                         break;
207                 case 'b':
208                         if ((bsize = atoi(optarg)) < MINBSIZE)
209                                 errx(1, "%s: block size too small, min is %d",
210                                     optarg, MINBSIZE);
211                         if (bsize > MAXBSIZE)
212                                 errx(1, "%s: block size too large, max is %d",
213                                     optarg, MAXBSIZE);
214                         break;
215                 case 'c':
216                         if ((maxblkspercg = atoi(optarg)) <= 0)
217                                 errx(1, "%s: bad blocks per cylinder group",
218                                     optarg);
219                         break;
220                 case 'd':
221                         if ((maxbsize = atoi(optarg)) < MINBSIZE)
222                                 errx(1, "%s: bad extent block size", optarg);
223                         break;
224                 case 'e':
225                         if ((maxbpg = atoi(optarg)) <= 0)
226                           errx(1, "%s: bad blocks per file in a cylinder group",
227                                     optarg);
228                         break;
229                 case 'f':
230                         if ((fsize = atoi(optarg)) <= 0)
231                                 errx(1, "%s: bad fragment size", optarg);
232                         break;
233                 case 'g':
234                         if ((avgfilesize = atoi(optarg)) <= 0)
235                                 errx(1, "%s: bad average file size", optarg);
236                         break;
237                 case 'h':
238                         if ((avgfilesperdir = atoi(optarg)) <= 0)
239                                errx(1, "%s: bad average files per dir", optarg);
240                         break;
241                 case 'i':
242                         if ((density = atoi(optarg)) <= 0)
243                                 errx(1, "%s: bad bytes per inode", optarg);
244                         break;
245                 case 'l':
246                         lflag = 1;
247                         break;
248                 case 'm':
249                         if ((minfree = atoi(optarg)) < 0 || minfree > 99)
250                                 errx(1, "%s: bad free space %%", optarg);
251                         break;
252                 case 'n':
253                         nflag = 1;
254                         break;
255                 case 'o':
256                         if (strcmp(optarg, "space") == 0)
257                                 opt = FS_OPTSPACE;
258                         else if (strcmp(optarg, "time") == 0)
259                                 opt = FS_OPTTIME;
260                         else
261                                 errx(1, 
262                 "%s: unknown optimization preference: use `space' or `time'",
263                                     optarg);
264                         break;
265                 case 's':
266                         errno = 0;
267                         fssize = strtoimax(optarg, NULL, 0);
268                         if (errno != 0)
269                                 err(1, "%s: bad file system size", optarg);
270                         break;
271                 case '?':
272                 default:
273                         usage();
274                 }
275         argc -= optind;
276         argv += optind;
277
278         if (argc != 1)
279                 usage();
280
281         special = argv[0];
282         cp = strrchr(special, '/');
283         if (cp == 0) {
284                 /*
285                  * No path prefix; try prefixing _PATH_DEV.
286                  */
287                 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
288                 special = device;
289         }
290
291         if (ufs_disk_fillout_blank(&disk, special) == -1 ||
292             (!Nflag && ufs_disk_write(&disk) == -1)) {
293                 if (disk.d_error != NULL)
294                         errx(1, "%s: %s", special, disk.d_error);
295                 else
296                         err(1, "%s", special);
297         }
298         if (fstat(disk.d_fd, &st) < 0)
299                 err(1, "%s", special);
300         if ((st.st_mode & S_IFMT) != S_IFCHR)
301                 errx(1, "%s: not a character-special device", special);
302
303         if (sectorsize == 0)
304                 ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize);
305         if (sectorsize && !ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize)) {
306                 if (fssize == 0)
307                         fssize = mediasize / sectorsize;
308                 else if (fssize > mediasize / sectorsize)
309                         errx(1, "%s: maximum file system size is %jd",
310                             special, (intmax_t)(mediasize / sectorsize));
311         }
312         pp = NULL;
313         lp = getdisklabel(special);
314         if (lp != NULL) {
315                 cp = strchr(special, '\0');
316                 cp--;
317                 if ((*cp < 'a' || *cp > 'h') && !isdigit(*cp))
318                         errx(1, "%s: can't figure out file system partition",
319                             special);
320                 if (isdigit(*cp))
321                         pp = &lp->d_partitions[RAW_PART];
322                 else
323                         pp = &lp->d_partitions[*cp - 'a'];
324                 oldpartition = *pp;
325                 if (pp->p_size == 0)
326                         errx(1, "%s: `%c' partition is unavailable",
327                             special, *cp);
328                 if (pp->p_fstype == FS_BOOT)
329                         errx(1, "%s: `%c' partition overlaps boot program",
330                             special, *cp);
331                 if (fssize == 0)
332                         fssize = pp->p_size;
333                 if (fssize > pp->p_size)
334                         errx(1, 
335                     "%s: maximum file system size %d", special, pp->p_size);
336                 if (sectorsize == 0)
337                         sectorsize = lp->d_secsize;
338                 if (fsize == 0)
339                         fsize = pp->p_fsize;
340                 if (bsize == 0)
341                         bsize = pp->p_frag * pp->p_fsize;
342         }
343         if (sectorsize <= 0)
344                 errx(1, "%s: no default sector size", special);
345         if (fsize <= 0)
346                 fsize = MAX(DFL_FRAGSIZE, sectorsize);
347         if (bsize <= 0)
348                 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
349         if (maxbsize == 0)
350                 maxbsize = bsize;
351         /*
352          * Maxcontig sets the default for the maximum number of blocks
353          * that may be allocated sequentially. With file system clustering
354          * it is possible to allocate contiguous blocks up to the maximum
355          * transfer size permitted by the controller or buffering.
356          */
357         if (maxcontig == 0)
358                 maxcontig = MAX(1, MAXPHYS / bsize);
359         if (density == 0)
360                 density = NFPI * fsize;
361         if (minfree < MINFREE && opt != FS_OPTSPACE) {
362                 fprintf(stderr, "Warning: changing optimization to space ");
363                 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
364                 opt = FS_OPTSPACE;
365         }
366         if (maxbpg == 0)
367                 maxbpg = MAXBLKPG(bsize);
368         realsectorsize = sectorsize;
369         if (sectorsize != DEV_BSIZE) {          /* XXX */
370                 int secperblk = sectorsize / DEV_BSIZE;
371
372                 sectorsize = DEV_BSIZE;
373                 fssize *= secperblk;
374                 if (pp != NULL)
375                         pp->p_size *= secperblk;
376         }
377         mkfs(pp, special);
378         if (!unlabeled) {
379                 if (realsectorsize != DEV_BSIZE)
380                         pp->p_size /= realsectorsize / DEV_BSIZE;
381                 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
382                         rewritelabel(special, lp);
383         }
384         ufs_disk_close(&disk);
385         exit(0);
386 }
387
388 struct disklabel *
389 getdisklabel(char *s)
390 {
391         static struct disklabel lab;
392         struct disklabel *lp;
393
394         if (!ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab))
395                 return (&lab);
396         unlabeled++;
397         if (disktype) {
398                 lp = getdiskbyname(disktype);
399                 if (lp != NULL)
400                         return (lp);
401         }
402         return (NULL);
403 }
404
405 void
406 rewritelabel(char *s, struct disklabel *lp)
407 {
408         if (unlabeled)
409                 return;
410         lp->d_checksum = 0;
411         lp->d_checksum = dkcksum(lp);
412         if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) < 0)
413                 warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
414 }
415
416 static void
417 usage()
418 {
419         fprintf(stderr,
420             "usage: %s [ -fsoptions ] special-device%s\n",
421             getprogname(),
422             " [device-type]");
423         fprintf(stderr, "where fsoptions are:\n");
424         fprintf(stderr, "\t-J Enable journaling via gjournal\n");
425         fprintf(stderr, "\t-L volume label to add to superblock\n");
426         fprintf(stderr,
427             "\t-N do not create file system, just print out parameters\n");
428         fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
429         fprintf(stderr, "\t-R regression test, supress random factors\n");
430         fprintf(stderr, "\t-S sector size\n");
431         fprintf(stderr, "\t-T disktype\n");
432         fprintf(stderr, "\t-U enable soft updates\n");
433         fprintf(stderr, "\t-a maximum contiguous blocks\n");
434         fprintf(stderr, "\t-b block size\n");
435         fprintf(stderr, "\t-c blocks per cylinders group\n");
436         fprintf(stderr, "\t-d maximum extent size\n");
437         fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
438         fprintf(stderr, "\t-f frag size\n");
439         fprintf(stderr, "\t-g average file size\n");
440         fprintf(stderr, "\t-h average files per directory\n");
441         fprintf(stderr, "\t-i number of bytes per inode\n");
442         fprintf(stderr, "\t-l enable multilabel MAC\n");
443         fprintf(stderr, "\t-n do not create .snap directory\n");
444         fprintf(stderr, "\t-m minimum free space %%\n");
445         fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
446         fprintf(stderr, "\t-s file systemsize (sectors)\n");
447         exit(1);
448 }