Upload Tizen:Base source
[framework/base/util-linux-ng.git] / disk-utils / mkfs.bfs.c
1 /*
2  *  mkfs.bfs - Create SCO BFS filesystem - aeb, 1999-09-07
3  *
4  *  Usage: mkfs.bfs [-N nr-of-inodes] [-V volume-name] [-F fsname] device
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdarg.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/ioctl.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <time.h>
18 #include "nls.h"
19 #include "blkdev.h"
20
21 #define BFS_ROOT_INO            2
22 #define BFS_NAMELEN             14
23 #define BFS_BLOCKSIZE           512
24 #define BFS_SUPER_MAGIC         0x1badface
25
26 /* superblock - 512 bytes */
27 struct bfssb {
28         unsigned int s_magic;
29         unsigned int s_start;     /* byte offset of start of data */
30         unsigned int s_end;       /* sizeof(slice)-1 */
31
32         /* for recovery during compaction */
33         int s_from, s_to;         /* src and dest block of current transfer */
34         int s_backup_from, s_backup_to;
35
36         /* labels - may well contain garbage */
37         char s_fsname[6];
38         char s_volume[6];
39         char s_pad[472];
40 };
41
42 /* inode - 64 bytes */
43 struct bfsi {
44         unsigned short i_ino;
45         unsigned char i_pad1[2];
46         unsigned long i_first_block;
47         unsigned long i_last_block;
48         unsigned long i_bytes_to_end;
49         unsigned long i_type;           /* 1: file, 2: the unique dir */
50         unsigned long i_mode;
51         unsigned long i_uid, i_gid;
52         unsigned long i_nlinks;
53         unsigned long i_atime, i_mtime, i_ctime;
54         unsigned char i_pad2[16];
55 };
56
57 #define BFS_DIR_TYPE    2
58
59 /* directory entry - 16 bytes */
60 struct bfsde {
61         unsigned short d_ino;
62         char d_name[BFS_NAMELEN];
63 };
64
65
66 static char *progname;
67
68 static void
69 fatal(char *s, ...) {
70     va_list p;
71
72     va_start(p, s);
73     fflush(stdout);
74     fprintf(stderr, "\n%s: ", progname);
75     vfprintf(stderr, s, p);
76     va_end(p);
77     fprintf(stderr, "\n");
78     exit(1);
79 }
80
81 static void
82 usage(void) {
83         fprintf(stderr, _(
84                 "Usage: %s [-v] [-N nr-of-inodes] [-V volume-name]\n"
85                 "       [-F fsname] device [block-count]\n"),
86                 progname);
87         exit(1);
88 }
89
90 int
91 main(int argc, char *argv[]) {
92         char *device, *volume, *fsname;
93         int inodes;
94         unsigned long long total_blocks, ino_bytes, ino_blocks, data_blocks;
95         unsigned long long user_specified_total_blocks = 0;
96         int verbose = 0;
97         int fd;
98         struct bfssb sb;
99         struct bfsi ri;
100         struct bfsde de;
101         struct stat statbuf;
102         time_t now;
103         int c, i, len;
104         char *p;
105
106         progname = argv[0];
107         if ((p = strrchr(progname, '/')) != NULL)
108                 progname = p+1;
109
110         if (argc < 2)
111                 usage();
112
113         if (argc == 2 &&
114             (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
115                 printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
116                 exit(0);
117         }
118
119         volume = fsname = "      ";     /* is there a default? */
120         inodes = 0;
121
122         while ((c = getopt(argc, argv, "vF:N:V:cl:")) != -1) {
123                 switch (c) {
124                 case 'N':
125                         inodes = atol(optarg);
126                         break;
127
128                 case 'V':
129                         len = strlen(optarg);
130                         if (len <= 0 || len > 6)
131                                 fatal(_("volume name too long"));
132                         volume = strdup(optarg);
133                         break;
134
135                 case 'F':
136                         len = strlen(optarg);
137                         if (len <= 0 || len > 6)
138                                 fatal(_("fsname name too long"));
139                         fsname = strdup(optarg);
140                         break;
141
142                 case 'v':
143                         verbose = 1;
144                         break;
145
146                         /* when called via mkfs we may get options c,l,v */
147                 case 'c':
148                 case 'l':
149                         break;
150
151                 default:
152                         usage();
153                 }
154         }
155
156         if (optind == argc)
157                 usage();
158
159         device = argv[optind++];
160
161         if (stat(device, &statbuf) == -1) {
162                 perror(device);
163                 fatal(_("cannot stat device %s"), device);
164         }
165
166         if (!S_ISBLK(statbuf.st_mode))
167                 fatal(_("%s is not a block special device"), device);
168
169         fd = open(device, O_RDWR | O_EXCL);
170         if (fd == -1) {
171                 perror(device);
172                 fatal(_("cannot open %s"), device);
173         }
174
175         if (optind == argc-1)
176                 user_specified_total_blocks = atoll(argv[optind]);
177         else if (optind != argc)
178                 usage();
179
180         if (blkdev_get_sectors(fd, &total_blocks) == -1) {
181                 if (!user_specified_total_blocks) {
182                         perror("blkdev_get_sectors");
183                         fatal(_("cannot get size of %s"), device);
184                 }
185                 total_blocks = user_specified_total_blocks;
186         } else if (user_specified_total_blocks) {
187                 if (user_specified_total_blocks > total_blocks)
188                         fatal(_("blocks argument too large, max is %llu"),
189                               total_blocks);
190                 total_blocks = user_specified_total_blocks;
191         }
192
193         if (!inodes) {
194                 /* pick some reasonable default */
195                 inodes = 8*(total_blocks/800);
196                 if (inodes < 48)
197                         inodes = 48;
198                 if (inodes > 512)
199                         inodes = 512;
200         } else {
201                 /* believe the user */
202                 if (inodes > 512)
203                         fatal(_("too many inodes - max is 512"));
204         }
205
206         ino_bytes = inodes * sizeof(struct bfsi);
207         ino_blocks = (ino_bytes + BFS_BLOCKSIZE - 1) / BFS_BLOCKSIZE;
208         data_blocks = total_blocks - ino_blocks - 1;
209
210         /* mimic the behaviour of SCO's mkfs - maybe this limit is needed */
211         if (data_blocks < 32)
212                 fatal(_("not enough space, need at least %llu blocks"),
213                       ino_blocks + 33);
214
215         memset(&sb, 0, sizeof(sb));
216         sb.s_magic = BFS_SUPER_MAGIC;
217         sb.s_start = ino_bytes + sizeof(struct bfssb);
218         sb.s_end = total_blocks * BFS_BLOCKSIZE - 1;
219         sb.s_from = sb.s_to = sb.s_backup_from = sb.s_backup_to = -1;
220         memcpy(sb.s_fsname, fsname, 6);
221         memcpy(sb.s_volume, volume, 6);
222
223         if (verbose) {
224                 fprintf(stderr, _("Device: %s\n"), device);
225                 fprintf(stderr, _("Volume: <%-6s>\n"), volume);
226                 fprintf(stderr, _("FSname: <%-6s>\n"), fsname);
227                 fprintf(stderr, _("BlockSize: %d\n"), BFS_BLOCKSIZE);
228                 if (ino_blocks==1)
229                         fprintf(stderr, _("Inodes: %d (in 1 block)\n"),
230                                 inodes);
231                 else
232                         fprintf(stderr, _("Inodes: %d (in %lld blocks)\n"),
233                                 inodes, ino_blocks);
234                 fprintf(stderr, _("Blocks: %lld\n"), total_blocks);
235                 fprintf(stderr, _("Inode end: %d, Data end: %d\n"),
236                         sb.s_start-1, sb.s_end);
237         }
238
239         if (write(fd, &sb, sizeof(sb)) != sizeof(sb))
240                 fatal(_("error writing superblock"));
241
242         memset(&ri, 0, sizeof(ri));
243         ri.i_ino = BFS_ROOT_INO;
244         ri.i_first_block = 1 + ino_blocks;
245         ri.i_last_block = ri.i_first_block +
246                 (inodes * sizeof(de) - 1) / BFS_BLOCKSIZE;
247         ri.i_bytes_to_end = ri.i_first_block * BFS_BLOCKSIZE
248                 + 2 * sizeof(struct bfsde) - 1;
249         ri.i_type = BFS_DIR_TYPE;
250         ri.i_mode = S_IFDIR | 0755;     /* or just 0755 */
251         ri.i_uid = 0;
252         ri.i_gid = 1;                   /* random */
253         ri.i_nlinks = 2;
254         time(&now);
255         ri.i_atime = now;
256         ri.i_mtime = now;
257         ri.i_ctime = now;
258
259         if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
260                 fatal(_("error writing root inode"));
261
262         memset(&ri, 0, sizeof(ri));
263         for (i=1; i<inodes; i++)
264                 if (write(fd, &ri, sizeof(ri)) != sizeof(ri))
265                         fatal(_("error writing inode"));
266
267         if (lseek(fd, (1 + ino_blocks)*BFS_BLOCKSIZE, SEEK_SET) == -1)
268                 fatal(_("seek error"));
269
270         memset(&de, 0, sizeof(de));
271         de.d_ino = BFS_ROOT_INO;
272         memcpy(de.d_name, ".", 1);
273         if (write(fd, &de, sizeof(de)) != sizeof(de))
274                 fatal(_("error writing . entry"));
275
276         memcpy(de.d_name, "..", 2);
277         if (write(fd, &de, sizeof(de)) != sizeof(de))
278                 fatal(_("error writing .. entry"));
279
280         if (close(fd) == -1) {
281                 perror(device);
282                 fatal(_("error closing %s"), device);
283         }
284
285         return 0;
286 }