]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sbin/newfs/newfs.c
Add missed mergeinfo.
[FreeBSD/stable/8.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 int     Eflag;                  /* Erase previous disk contents */
83 int     Lflag;                  /* add a volume label */
84 int     Nflag;                  /* run without writing file system */
85 int     Oflag = 2;              /* file system format (1 => UFS1, 2 => UFS2) */
86 int     Rflag;                  /* regression test */
87 int     Uflag;                  /* enable soft updates for file system */
88 int     Xflag = 0;              /* exit in middle of newfs for testing */
89 int     Jflag;                  /* enable gjournal for file system */
90 int     lflag;                  /* enable multilabel for file system */
91 int     nflag;                  /* do not create .snap directory */
92 int     tflag;                  /* enable TRIM */
93 intmax_t fssize;                /* file system size */
94 int     sectorsize;             /* bytes/sector */
95 int     realsectorsize;         /* bytes/sector in hardware */
96 int     fsize = 0;              /* fragment size */
97 int     bsize = 0;              /* block size */
98 int     maxbsize = 0;           /* maximum clustering */
99 int     maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
100 int     minfree = MINFREE;      /* free space threshold */
101 int     opt = DEFAULTOPT;       /* optimization preference (space or time) */
102 int     density;                /* number of bytes per inode */
103 int     maxcontig = 0;          /* max contiguous blocks to allocate */
104 int     maxbpg;                 /* maximum blocks per file in a cyl group */
105 int     avgfilesize = AVFILESIZ;/* expected average file size */
106 int     avgfilesperdir = AFPDIR;/* expected number of files per directory */
107 u_char  *volumelabel = NULL;    /* volume label for filesystem */
108 struct uufsd disk;              /* libufs disk structure */
109
110 static char     device[MAXPATHLEN];
111 static u_char   bootarea[BBSIZE];
112 static int      is_file;                /* work on a file, not a device */
113 static char     *dkname;
114 static char     *disktype;
115 static int      unlabeled;
116
117 static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t);
118 static struct disklabel *getdisklabel(char *s);
119 static void rewritelabel(char *s, struct disklabel *lp);
120 static void usage(void);
121
122 ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */
123
124 int
125 main(int argc, char *argv[])
126 {
127         struct partition *pp;
128         struct disklabel *lp;
129         struct partition oldpartition;
130         struct stat st;
131         char *cp, *special;
132         intmax_t reserved;
133         int ch, i;
134         off_t mediasize;
135         char part_name;         /* partition name, default to full disk */
136
137         part_name = 'c';
138         reserved = 0;
139         while ((ch = getopt(argc, argv,
140             "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:lm:no:p:r:s:t")) != -1)
141                 switch (ch) {
142                 case 'E':
143                         Eflag = 1;
144                         break;
145                 case 'J':
146                         Jflag = 1;
147                         break;
148                 case 'L':
149                         volumelabel = optarg;
150                         i = -1;
151                         while (isalnum(volumelabel[++i]));
152                         if (volumelabel[i] != '\0') {
153                                 errx(1, "bad volume label. Valid characters are alphanumerics.");
154                         }
155                         if (strlen(volumelabel) >= MAXVOLLEN) {
156                                 errx(1, "bad volume label. Length is longer than %d.",
157                                     MAXVOLLEN);
158                         }
159                         Lflag = 1;
160                         break;
161                 case 'N':
162                         Nflag = 1;
163                         break;
164                 case 'O':
165                         if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
166                                 errx(1, "%s: bad file system format value",
167                                     optarg);
168                         break;
169                 case 'R':
170                         Rflag = 1;
171                         break;
172                 case 'S':
173                         if ((sectorsize = atoi(optarg)) <= 0)
174                                 errx(1, "%s: bad sector size", optarg);
175                         break;
176                 case 'T':
177                         disktype = optarg;
178                         break;
179                 case 'U':
180                         Uflag = 1;
181                         break;
182                 case 'X':
183                         Xflag++;
184                         break;
185                 case 'a':
186                         if ((maxcontig = atoi(optarg)) <= 0)
187                                 errx(1, "%s: bad maximum contiguous blocks",
188                                     optarg);
189                         break;
190                 case 'b':
191                         if ((bsize = atoi(optarg)) < MINBSIZE)
192                                 errx(1, "%s: block size too small, min is %d",
193                                     optarg, MINBSIZE);
194                         if (bsize > MAXBSIZE)
195                                 errx(1, "%s: block size too large, max is %d",
196                                     optarg, MAXBSIZE);
197                         break;
198                 case 'c':
199                         if ((maxblkspercg = atoi(optarg)) <= 0)
200                                 errx(1, "%s: bad blocks per cylinder group",
201                                     optarg);
202                         break;
203                 case 'd':
204                         if ((maxbsize = atoi(optarg)) < MINBSIZE)
205                                 errx(1, "%s: bad extent block size", optarg);
206                         break;
207                 case 'e':
208                         if ((maxbpg = atoi(optarg)) <= 0)
209                           errx(1, "%s: bad blocks per file in a cylinder group",
210                                     optarg);
211                         break;
212                 case 'f':
213                         if ((fsize = atoi(optarg)) <= 0)
214                                 errx(1, "%s: bad fragment size", optarg);
215                         break;
216                 case 'g':
217                         if ((avgfilesize = atoi(optarg)) <= 0)
218                                 errx(1, "%s: bad average file size", optarg);
219                         break;
220                 case 'h':
221                         if ((avgfilesperdir = atoi(optarg)) <= 0)
222                                errx(1, "%s: bad average files per dir", optarg);
223                         break;
224                 case 'i':
225                         if ((density = atoi(optarg)) <= 0)
226                                 errx(1, "%s: bad bytes per inode", optarg);
227                         break;
228                 case 'l':
229                         lflag = 1;
230                         break;
231                 case 'm':
232                         if ((minfree = atoi(optarg)) < 0 || minfree > 99)
233                                 errx(1, "%s: bad free space %%", optarg);
234                         break;
235                 case 'n':
236                         nflag = 1;
237                         break;
238                 case 'o':
239                         if (strcmp(optarg, "space") == 0)
240                                 opt = FS_OPTSPACE;
241                         else if (strcmp(optarg, "time") == 0)
242                                 opt = FS_OPTTIME;
243                         else
244                                 errx(1, 
245                 "%s: unknown optimization preference: use `space' or `time'",
246                                     optarg);
247                         break;
248                 case 'r':
249                         errno = 0;
250                         reserved = strtoimax(optarg, &cp, 0);
251                         if (errno != 0 || cp == optarg ||
252                             *cp != '\0' || reserved < 0)
253                                 errx(1, "%s: bad reserved size", optarg);
254                         break;
255                 case 'p':
256                         is_file = 1;
257                         part_name = optarg[0];
258                         break;
259
260                 case 's':
261                         errno = 0;
262                         fssize = strtoimax(optarg, &cp, 0);
263                         if (errno != 0 || cp == optarg ||
264                             *cp != '\0' || fssize < 0)
265                                 errx(1, "%s: bad file system size", optarg);
266                         break;
267                 case 't':
268                         tflag = 1;
269                         break;
270                 case '?':
271                 default:
272                         usage();
273                 }
274         argc -= optind;
275         argv += optind;
276
277         if (argc != 1)
278                 usage();
279
280         special = argv[0];
281         if (!special[0])
282                 err(1, "empty file/special name");
283         cp = strrchr(special, '/');
284         if (cp == 0) {
285                 /*
286                  * No path prefix; try prefixing _PATH_DEV.
287                  */
288                 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
289                 special = device;
290         }
291
292         if (is_file) {
293                 /* bypass ufs_disk_fillout_blank */
294                 bzero( &disk, sizeof(disk));
295                 disk.d_bsize = 1;
296                 disk.d_name = special;
297                 disk.d_fd = open(special, O_RDONLY);
298                 if (disk.d_fd < 0 ||
299                     (!Nflag && ufs_disk_write(&disk) == -1))
300                         errx(1, "%s: ", special);
301         } else if (ufs_disk_fillout_blank(&disk, special) == -1 ||
302             (!Nflag && ufs_disk_write(&disk) == -1)) {
303                 if (disk.d_error != NULL)
304                         errx(1, "%s: %s", special, disk.d_error);
305                 else
306                         err(1, "%s", special);
307         }
308         if (fstat(disk.d_fd, &st) < 0)
309                 err(1, "%s", special);
310         if ((st.st_mode & S_IFMT) != S_IFCHR) {
311                 warn("%s: not a character-special device", special);
312                 is_file = 1;    /* assume it is a file */
313                 dkname = special;
314                 if (sectorsize == 0)
315                         sectorsize = 512;
316                 mediasize = st.st_size;
317                 /* set fssize from the partition */
318         } else {
319             if (sectorsize == 0)
320                 if (ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize) == -1)
321                     sectorsize = 0;     /* back out on error for safety */
322             if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
323                 getfssize(&fssize, special, mediasize / sectorsize, reserved);
324         }
325         pp = NULL;
326         lp = getdisklabel(special);
327         if (lp != NULL) {
328                 if (!is_file) /* already set for files */
329                         part_name = special[strlen(special) - 1];
330                 if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) &&
331                                 !isdigit(part_name))
332                         errx(1, "%s: can't figure out file system partition",
333                                         special);
334                 cp = &part_name;
335                 if (isdigit(*cp))
336                         pp = &lp->d_partitions[RAW_PART];
337                 else
338                         pp = &lp->d_partitions[*cp - 'a'];
339                 oldpartition = *pp;
340                 if (pp->p_size == 0)
341                         errx(1, "%s: `%c' partition is unavailable",
342                             special, *cp);
343                 if (pp->p_fstype == FS_BOOT)
344                         errx(1, "%s: `%c' partition overlaps boot program",
345                             special, *cp);
346                 getfssize(&fssize, special, pp->p_size, reserved);
347                 if (sectorsize == 0)
348                         sectorsize = lp->d_secsize;
349                 if (fsize == 0)
350                         fsize = pp->p_fsize;
351                 if (bsize == 0)
352                         bsize = pp->p_frag * pp->p_fsize;
353                 if (is_file)
354                         part_ofs = pp->p_offset;
355         }
356         if (sectorsize <= 0)
357                 errx(1, "%s: no default sector size", special);
358         if (fsize <= 0)
359                 fsize = MAX(DFL_FRAGSIZE, sectorsize);
360         if (bsize <= 0)
361                 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
362         if (minfree < MINFREE && opt != FS_OPTSPACE) {
363                 fprintf(stderr, "Warning: changing optimization to space ");
364                 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
365                 opt = FS_OPTSPACE;
366         }
367         realsectorsize = sectorsize;
368         if (sectorsize != DEV_BSIZE) {          /* XXX */
369                 int secperblk = sectorsize / DEV_BSIZE;
370
371                 sectorsize = DEV_BSIZE;
372                 fssize *= secperblk;
373                 if (pp != NULL)
374                         pp->p_size *= secperblk;
375         }
376         mkfs(pp, special);
377         if (!unlabeled) {
378                 if (realsectorsize != DEV_BSIZE)
379                         pp->p_size /= realsectorsize / DEV_BSIZE;
380                 if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition)))
381                         rewritelabel(special, lp);
382         }
383         ufs_disk_close(&disk);
384         exit(0);
385 }
386
387 void
388 getfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved)
389 {
390         intmax_t available;
391
392         available = disksize - reserved;
393         if (available <= 0)
394                 errx(1, "%s: reserved not less than device size %jd",
395                     s, disksize);
396         if (*fsz == 0)
397                 *fsz = available;
398         else if (*fsz > available)
399                 errx(1, "%s: maximum file system size is %jd",
400                     s, available);
401 }
402
403 struct disklabel *
404 getdisklabel(char *s)
405 {
406         static struct disklabel lab;
407         struct disklabel *lp;
408
409         if (is_file) {
410                 if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
411                         err(4, "cannot read bootarea");
412                 if (bsd_disklabel_le_dec(
413                     bootarea + (0 /* labeloffset */ +
414                                 1 /* labelsoffset */ * sectorsize),
415                     &lab, MAXPARTITIONS))
416                         errx(1, "no valid label found");
417
418                 lp = &lab;
419                 return &lab;
420         }
421
422         if (ioctl(disk.d_fd, DIOCGDINFO, (char *)&lab) != -1)
423                 return (&lab);
424         unlabeled++;
425         if (disktype) {
426                 lp = getdiskbyname(disktype);
427                 if (lp != NULL)
428                         return (lp);
429         }
430         return (NULL);
431 }
432
433 void
434 rewritelabel(char *s, struct disklabel *lp)
435 {
436         if (unlabeled)
437                 return;
438         lp->d_checksum = 0;
439         lp->d_checksum = dkcksum(lp);
440         if (is_file) {
441                 bsd_disklabel_le_enc(bootarea + 0 /* labeloffset */ +
442                         1 /* labelsoffset */ * sectorsize, lp);
443                 lseek(disk.d_fd, 0, SEEK_SET);
444                 if (write(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
445                         errx(1, "cannot write label");
446                 return;
447         }
448         if (ioctl(disk.d_fd, DIOCWDINFO, (char *)lp) == -1)
449                 warn("ioctl (WDINFO): %s: can't rewrite disk label", s);
450 }
451
452 static void
453 usage()
454 {
455         fprintf(stderr,
456             "usage: %s [ -fsoptions ] special-device%s\n",
457             getprogname(),
458             " [device-type]");
459         fprintf(stderr, "where fsoptions are:\n");
460         fprintf(stderr, "\t-E Erase previous disk content\n");
461         fprintf(stderr, "\t-J Enable journaling via gjournal\n");
462         fprintf(stderr, "\t-L volume label to add to superblock\n");
463         fprintf(stderr,
464             "\t-N do not create file system, just print out parameters\n");
465         fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
466         fprintf(stderr, "\t-R regression test, suppress random factors\n");
467         fprintf(stderr, "\t-S sector size\n");
468         fprintf(stderr, "\t-T disktype\n");
469         fprintf(stderr, "\t-U enable soft updates\n");
470         fprintf(stderr, "\t-a maximum contiguous blocks\n");
471         fprintf(stderr, "\t-b block size\n");
472         fprintf(stderr, "\t-c blocks per cylinders group\n");
473         fprintf(stderr, "\t-d maximum extent size\n");
474         fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
475         fprintf(stderr, "\t-f frag size\n");
476         fprintf(stderr, "\t-g average file size\n");
477         fprintf(stderr, "\t-h average files per directory\n");
478         fprintf(stderr, "\t-i number of bytes per inode\n");
479         fprintf(stderr, "\t-l enable multilabel MAC\n");
480         fprintf(stderr, "\t-n do not create .snap directory\n");
481         fprintf(stderr, "\t-m minimum free space %%\n");
482         fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
483         fprintf(stderr, "\t-p partition name (a..h)\n");
484         fprintf(stderr, "\t-r reserved sectors at the end of device\n");
485         fprintf(stderr, "\t-s file system size (sectors)\n");
486         fprintf(stderr, "\t-t enable TRIM\n");
487         exit(1);
488 }