]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/newfs/newfs.c
Merge LUA 5.4.6
[FreeBSD/FreeBSD.git] / sbin / newfs / newfs.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Networks Associates Technology, Inc.
5  * All rights reserved.
6  *
7  * This software was developed for the FreeBSD Project by Marshall
8  * Kirk McKusick and Network Associates Laboratories, the Security
9  * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10  * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11  * research program.
12  *
13  * Copyright (c) 1983, 1989, 1993, 1994
14  *      The Regents of the University of California.  All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40
41 #if 0
42 #ifndef lint
43 static const char copyright[] =
44 "@(#) Copyright (c) 1983, 1989, 1993, 1994\n\
45         The Regents of the University of California.  All rights reserved.\n";
46 #endif /* not lint */
47
48 #ifndef lint
49 static char sccsid[] = "@(#)newfs.c     8.13 (Berkeley) 5/1/95";
50 #endif /* not lint */
51 #endif
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
54
55 /*
56  * newfs: friendly front end to mkfs
57  */
58 #include <sys/param.h>
59 #include <sys/stat.h>
60 #include <sys/disk.h>
61 #include <sys/disklabel.h>
62 #include <sys/file.h>
63 #include <sys/mount.h>
64
65 #include <ufs/ufs/dir.h>
66 #include <ufs/ufs/dinode.h>
67 #include <ufs/ffs/fs.h>
68 #include <ufs/ufs/extattr.h>
69 #include <ufs/ufs/quota.h>
70 #include <ufs/ufs/ufsmount.h>
71
72 #include <ctype.h>
73 #include <err.h>
74 #include <errno.h>
75 #include <inttypes.h>
76 #include <paths.h>
77 #include <stdarg.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <syslog.h>
82 #include <unistd.h>
83
84 #include <libutil.h>
85
86 #include "newfs.h"
87
88 int     Eflag;                  /* Erase previous disk contents */
89 int     Lflag;                  /* add a volume label */
90 int     Nflag;                  /* run without writing file system */
91 int     Oflag = 2;              /* file system format (1 => UFS1, 2 => UFS2) */
92 int     Rflag;                  /* regression test */
93 int     Uflag;                  /* enable soft updates for file system */
94 int     jflag;                  /* enable soft updates journaling for filesys */
95 int     Xflag = 0;              /* exit in middle of newfs for testing */
96 int     Jflag;                  /* enable gjournal for file system */
97 int     lflag;                  /* enable multilabel for file system */
98 int     nflag;                  /* do not create .snap directory */
99 int     tflag;                  /* enable TRIM */
100 intmax_t fssize;                /* file system size */
101 off_t   mediasize;              /* device size */
102 int     sectorsize;             /* bytes/sector */
103 int     realsectorsize;         /* bytes/sector in hardware */
104 int     fsize = 0;              /* fragment size */
105 int     bsize = 0;              /* block size */
106 int     maxbsize = 0;           /* maximum clustering */
107 int     maxblkspercg = MAXBLKSPERCG; /* maximum blocks per cylinder group */
108 int     minfree = MINFREE;      /* free space threshold */
109 int     metaspace;              /* space held for metadata blocks */
110 int     opt = DEFAULTOPT;       /* optimization preference (space or time) */
111 int     density;                /* number of bytes per inode */
112 int     maxcontig = 0;          /* max contiguous blocks to allocate */
113 int     maxbpg;                 /* maximum blocks per file in a cyl group */
114 int     avgfilesize = AVFILESIZ;/* expected average file size */
115 int     avgfilesperdir = AFPDIR;/* expected number of files per directory */
116 u_char  *volumelabel = NULL;    /* volume label for filesystem */
117 struct uufsd disk;              /* libufs disk structure */
118
119 static char     device[MAXPATHLEN];
120 static u_char   bootarea[BBSIZE];
121 static int      is_file;                /* work on a file, not a device */
122 static char     *dkname;
123 static char     *disktype;
124
125 static void getfssize(intmax_t *, const char *p, intmax_t, intmax_t);
126 static struct disklabel *getdisklabel(void);
127 static void usage(void) __dead2;
128 static int expand_number_int(const char *buf, int *num);
129
130 ufs2_daddr_t part_ofs; /* partition offset in blocks, used with files */
131
132 int
133 main(int argc, char *argv[])
134 {
135         struct partition *pp;
136         struct disklabel *lp;
137         struct stat st;
138         char *cp, *special;
139         intmax_t reserved;
140         int ch, rval;
141         size_t i;
142         char part_name;         /* partition name, default to full disk */
143
144         part_name = 'c';
145         reserved = 0;
146         while ((ch = getopt(argc, argv,
147             "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:jk:lm:no:p:r:s:t")) != -1)
148                 switch (ch) {
149                 case 'E':
150                         Eflag = 1;
151                         break;
152                 case 'J':
153                         Jflag = 1;
154                         break;
155                 case 'L':
156                         volumelabel = optarg;
157                         for (i = 0; isalnum(volumelabel[i]) ||
158                             volumelabel[i] == '_' || volumelabel[i] == '-';
159                             i++)
160                                 continue;
161                         if (volumelabel[i] != '\0') {
162                                 errx(1, "bad volume label. Valid characters "
163                                     "are alphanumerics, dashes, and underscores.");
164                         }
165                         if (strlen(volumelabel) >= MAXVOLLEN) {
166                                 errx(1, "bad volume label. Length is longer than %d.",
167                                     MAXVOLLEN);
168                         }
169                         Lflag = 1;
170                         break;
171                 case 'N':
172                         Nflag = 1;
173                         break;
174                 case 'O':
175                         if ((Oflag = atoi(optarg)) < 1 || Oflag > 2)
176                                 errx(1, "%s: bad file system format value",
177                                     optarg);
178                         break;
179                 case 'R':
180                         Rflag = 1;
181                         break;
182                 case 'S':
183                         rval = expand_number_int(optarg, &sectorsize);
184                         if (rval < 0 || sectorsize <= 0)
185                                 errx(1, "%s: bad sector size", optarg);
186                         break;
187                 case 'T':
188                         disktype = optarg;
189                         break;
190                 case 'j':
191                         jflag = 1;
192                         /* fall through to enable soft updates */
193                         /* FALLTHROUGH */
194                 case 'U':
195                         Uflag = 1;
196                         break;
197                 case 'X':
198                         Xflag++;
199                         break;
200                 case 'a':
201                         rval = expand_number_int(optarg, &maxcontig);
202                         if (rval < 0 || maxcontig <= 0)
203                                 errx(1, "%s: bad maximum contiguous blocks",
204                                     optarg);
205                         break;
206                 case 'b':
207                         rval = expand_number_int(optarg, &bsize);
208                         if (rval < 0)
209                                  errx(1, "%s: bad block size",
210                                     optarg);
211                         if (bsize < MINBSIZE)
212                                 errx(1, "%s: block size too small, min is %d",
213                                     optarg, MINBSIZE);
214                         if (bsize > MAXBSIZE)
215                                 errx(1, "%s: block size too large, max is %d",
216                                     optarg, MAXBSIZE);
217                         break;
218                 case 'c':
219                         rval = expand_number_int(optarg, &maxblkspercg);
220                         if (rval < 0 || maxblkspercg <= 0)
221                                 errx(1, "%s: bad blocks per cylinder group",
222                                     optarg);
223                         break;
224                 case 'd':
225                         rval = expand_number_int(optarg, &maxbsize);
226                         if (rval < 0 || maxbsize < MINBSIZE)
227                                 errx(1, "%s: bad extent block size", optarg);
228                         break;
229                 case 'e':
230                         rval = expand_number_int(optarg, &maxbpg);
231                         if (rval < 0 || maxbpg <= 0)
232                           errx(1, "%s: bad blocks per file in a cylinder group",
233                                     optarg);
234                         break;
235                 case 'f':
236                         rval = expand_number_int(optarg, &fsize);
237                         if (rval < 0 || fsize <= 0)
238                                 errx(1, "%s: bad fragment size", optarg);
239                         break;
240                 case 'g':
241                         rval = expand_number_int(optarg, &avgfilesize);
242                         if (rval < 0 || avgfilesize <= 0)
243                                 errx(1, "%s: bad average file size", optarg);
244                         break;
245                 case 'h':
246                         rval = expand_number_int(optarg, &avgfilesperdir);
247                         if (rval < 0 || avgfilesperdir <= 0)
248                                errx(1, "%s: bad average files per dir", optarg);
249                         break;
250                 case 'i':
251                         rval = expand_number_int(optarg, &density);
252                         if (rval < 0 || density <= 0)
253                                 errx(1, "%s: bad bytes per inode", optarg);
254                         break;
255                 case 'l':
256                         lflag = 1;
257                         break;
258                 case 'k':
259                         if ((metaspace = atoi(optarg)) < 0)
260                                 errx(1, "%s: bad metadata space %%", optarg);
261                         if (metaspace == 0)
262                                 /* force to stay zero in mkfs */
263                                 metaspace = -1;
264                         break;
265                 case 'm':
266                         if ((minfree = atoi(optarg)) < 0 || minfree > 99)
267                                 errx(1, "%s: bad free space %%", optarg);
268                         break;
269                 case 'n':
270                         nflag = 1;
271                         break;
272                 case 'o':
273                         if (strcmp(optarg, "space") == 0)
274                                 opt = FS_OPTSPACE;
275                         else if (strcmp(optarg, "time") == 0)
276                                 opt = FS_OPTTIME;
277                         else
278                                 errx(1, 
279                 "%s: unknown optimization preference: use `space' or `time'",
280                                     optarg);
281                         break;
282                 case 'r':
283                         errno = 0;
284                         reserved = strtoimax(optarg, &cp, 0);
285                         if (errno != 0 || cp == optarg ||
286                             *cp != '\0' || reserved < 0)
287                                 errx(1, "%s: bad reserved size", optarg);
288                         break;
289                 case 'p':
290                         is_file = 1;
291                         part_name = optarg[0];
292                         break;
293
294                 case 's':
295                         errno = 0;
296                         fssize = strtoimax(optarg, &cp, 0);
297                         if (errno != 0 || cp == optarg ||
298                             *cp != '\0' || fssize < 0)
299                                 errx(1, "%s: bad file system size", optarg);
300                         break;
301                 case 't':
302                         tflag = 1;
303                         break;
304                 case '?':
305                 default:
306                         usage();
307                 }
308         argc -= optind;
309         argv += optind;
310
311         if (argc != 1)
312                 usage();
313
314         special = argv[0];
315         if (!special[0])
316                 err(1, "empty file/special name");
317         cp = strrchr(special, '/');
318         if (cp == NULL) {
319                 /*
320                  * No path prefix; try prefixing _PATH_DEV.
321                  */
322                 snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special);
323                 special = device;
324         }
325
326         if (is_file) {
327                 /* bypass ufs_disk_fillout_blank */
328                 bzero( &disk, sizeof(disk));
329                 disk.d_bsize = 1;
330                 disk.d_name = special;
331                 disk.d_fd = open(special, O_RDONLY);
332                 if (disk.d_fd < 0 ||
333                     (!Nflag && ufs_disk_write(&disk) == -1))
334                         errx(1, "%s: ", special);
335         } else if (ufs_disk_fillout_blank(&disk, special) == -1 ||
336             (!Nflag && ufs_disk_write(&disk) == -1)) {
337                 if (disk.d_error != NULL)
338                         errx(1, "%s: %s", special, disk.d_error);
339                 else
340                         err(1, "%s", special);
341         }
342         if (fstat(disk.d_fd, &st) < 0)
343                 err(1, "%s", special);
344         if ((st.st_mode & S_IFMT) != S_IFCHR) {
345                 warn("%s: not a character-special device", special);
346                 is_file = 1;    /* assume it is a file */
347                 dkname = special;
348                 if (sectorsize == 0)
349                         sectorsize = 512;
350                 mediasize = st.st_size;
351                 /* set fssize from the partition */
352         } else {
353             if (sectorsize == 0)
354                 if (ioctl(disk.d_fd, DIOCGSECTORSIZE, &sectorsize) == -1)
355                     sectorsize = 0;     /* back out on error for safety */
356             if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1)
357                 getfssize(&fssize, special, mediasize / sectorsize, reserved);
358         }
359         pp = NULL;
360         lp = getdisklabel();
361         if (lp != NULL) {
362                 if (!is_file) /* already set for files */
363                         part_name = special[strlen(special) - 1];
364                 if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) &&
365                                 !isdigit(part_name))
366                         errx(1, "%s: can't figure out file system partition",
367                                         special);
368                 cp = &part_name;
369                 if (isdigit(*cp))
370                         pp = &lp->d_partitions[RAW_PART];
371                 else
372                         pp = &lp->d_partitions[*cp - 'a'];
373                 if (pp->p_size == 0)
374                         errx(1, "%s: `%c' partition is unavailable",
375                             special, *cp);
376                 if (pp->p_fstype == FS_BOOT)
377                         errx(1, "%s: `%c' partition overlaps boot program",
378                             special, *cp);
379                 getfssize(&fssize, special, pp->p_size, reserved);
380                 if (sectorsize == 0)
381                         sectorsize = lp->d_secsize;
382                 if (fsize == 0)
383                         fsize = pp->p_fsize;
384                 if (bsize == 0)
385                         bsize = pp->p_frag * pp->p_fsize;
386                 if (is_file)
387                         part_ofs = pp->p_offset;
388         }
389         if (sectorsize <= 0)
390                 errx(1, "%s: no default sector size", special);
391         if (fsize <= 0)
392                 fsize = MAX(DFL_FRAGSIZE, sectorsize);
393         if (bsize <= 0)
394                 bsize = MIN(DFL_BLKSIZE, 8 * fsize);
395         if (minfree < MINFREE && opt != FS_OPTSPACE) {
396                 fprintf(stderr, "Warning: changing optimization to space ");
397                 fprintf(stderr, "because minfree is less than %d%%\n", MINFREE);
398                 opt = FS_OPTSPACE;
399         }
400         realsectorsize = sectorsize;
401         if (sectorsize != DEV_BSIZE) {          /* XXX */
402                 int secperblk = sectorsize / DEV_BSIZE;
403
404                 sectorsize = DEV_BSIZE;
405                 fssize *= secperblk;
406                 if (pp != NULL)
407                         pp->p_size *= secperblk;
408         }
409         mkfs(pp, special);
410         ufs_disk_close(&disk);
411         if (!jflag)
412                 exit(0);
413         if (execlp("tunefs", "newfs", "-j", "enable", special, NULL) < 0)
414                 err(1, "Cannot enable soft updates journaling, tunefs");
415         /* NOT REACHED */
416 }
417
418 void
419 getfssize(intmax_t *fsz, const char *s, intmax_t disksize, intmax_t reserved)
420 {
421         intmax_t available;
422
423         available = disksize - reserved;
424         if (available <= 0)
425                 errx(1, "%s: reserved not less than device size %jd",
426                     s, disksize);
427         if (*fsz == 0)
428                 *fsz = available;
429         else if (*fsz > available)
430                 errx(1, "%s: maximum file system size is %jd",
431                     s, available);
432 }
433
434 struct disklabel *
435 getdisklabel(void)
436 {
437         static struct disklabel lab;
438         struct disklabel *lp;
439
440         if (is_file) {
441                 if (read(disk.d_fd, bootarea, BBSIZE) != BBSIZE)
442                         err(4, "cannot read bootarea");
443                 if (bsd_disklabel_le_dec(
444                     bootarea + (0 /* labeloffset */ +
445                                 1 /* labelsoffset */ * sectorsize),
446                     &lab, MAXPARTITIONS))
447                         errx(1, "no valid label found");
448
449                 lp = &lab;
450                 return &lab;
451         }
452
453         if (disktype) {
454                 lp = getdiskbyname(disktype);
455                 if (lp != NULL)
456                         return (lp);
457         }
458         return (NULL);
459 }
460
461 static void
462 usage(void)
463 {
464         fprintf(stderr,
465             "usage: %s [ -fsoptions ] special-device%s\n",
466             getprogname(),
467             " [device-type]");
468         fprintf(stderr, "where fsoptions are:\n");
469         fprintf(stderr, "\t-E Erase previous disk content\n");
470         fprintf(stderr, "\t-J Enable journaling via gjournal\n");
471         fprintf(stderr, "\t-L volume label to add to superblock\n");
472         fprintf(stderr,
473             "\t-N do not create file system, just print out parameters\n");
474         fprintf(stderr, "\t-O file system format: 1 => UFS1, 2 => UFS2\n");
475         fprintf(stderr, "\t-R regression test, suppress random factors\n");
476         fprintf(stderr, "\t-S sector size\n");
477         fprintf(stderr, "\t-T disktype\n");
478         fprintf(stderr, "\t-U enable soft updates\n");
479         fprintf(stderr, "\t-a maximum contiguous blocks\n");
480         fprintf(stderr, "\t-b block size\n");
481         fprintf(stderr, "\t-c blocks per cylinders group\n");
482         fprintf(stderr, "\t-d maximum extent size\n");
483         fprintf(stderr, "\t-e maximum blocks per file in a cylinder group\n");
484         fprintf(stderr, "\t-f frag size\n");
485         fprintf(stderr, "\t-g average file size\n");
486         fprintf(stderr, "\t-h average files per directory\n");
487         fprintf(stderr, "\t-i number of bytes per inode\n");
488         fprintf(stderr, "\t-j enable soft updates journaling\n");
489         fprintf(stderr, "\t-k space to hold for metadata blocks\n");
490         fprintf(stderr, "\t-l enable multilabel MAC\n");
491         fprintf(stderr, "\t-n do not create .snap directory\n");
492         fprintf(stderr, "\t-m minimum free space %%\n");
493         fprintf(stderr, "\t-o optimization preference (`space' or `time')\n");
494         fprintf(stderr, "\t-p partition name (a..h)\n");
495         fprintf(stderr, "\t-r reserved sectors at the end of device\n");
496         fprintf(stderr, "\t-s file system size (sectors)\n");
497         fprintf(stderr, "\t-t enable TRIM\n");
498         exit(1);
499 }
500
501 static int
502 expand_number_int(const char *buf, int *num)
503 {
504         int64_t num64;
505         int rval;
506
507         rval = expand_number(buf, &num64);
508         if (rval < 0)
509                 return (rval);
510         if (num64 > INT_MAX || num64 < INT_MIN) {
511                 errno = ERANGE;
512                 return (-1);
513         }
514         *num = (int)num64;
515         return (0);
516 }