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