10 void xwrite (int fd, const char *buf, size_t num)
12 if (write (fd, buf, num) < 0)
16 void xlseek (int fd, off_t pos)
18 if (lseek (fd, pos, SEEK_SET) < 0)
22 int main (int argc, char *argv[])
24 struct sufs_superblock sb;
26 struct sufs_dirent *ent;
28 uint64_t size, nib, nino, ndata;
29 uint32_t bs = -1, nibms, ndbms, offset = SUFS_BOOTSIZE;
34 while ((option = getopt (argc, argv, "b:n")) != -1) {
37 bs = (uint32_t)strtoul (optarg, &endp, 10);
39 errx (1, "invalid block size");
41 errx (1, "block size less than 128 is not supported");
55 fputs ("Usage: newfs_sufs file\n", stderr);
59 fd = open (argv[0], O_WRONLY);
63 if (fstat (fd, &st) < 0)
72 // total number of blocks
73 size = st.st_size / bs;
77 // number of inode blocks
80 if ((nib * bs / sizeof (struct sufs_inode)) < 64)
81 nib = 64 * sizeof (struct sufs_inode) / bs;
87 nino = nib * bs / sizeof (struct sufs_inode);
89 // number of inode bitmap blocks
90 nibms = (nino + 8 * bs - 1) / (8 * bs);
92 // number of data blocks
93 ndata = size - nib - nibms - (offset / bs) - 1;
97 // number of data bitmap blocks
98 ndbms = (ndata + 8 * bs - 1) / (8 * bs);
100 // adjust for number of data bitmap blocks
103 memset (&sb, 0, sizeof (sb));
104 sb.magic = SUFS_MAGIC;
109 sb.ibmoff = offset / bs + 1;
110 sb.dbmoff = sb.ibmoff + nibms;
111 sb.ioff = sb.dbmoff + ndbms;
112 sb.doff = sb.ioff + nib;
113 buf = calloc (1, bs);
114 memcpy (buf, &sb, sizeof (sb));
115 xwrite (fd, buf, bs);
117 printf ("size = %zu\n", (size_t)size);
118 printf ("blksize = %zu\n", (size_t)bs);
119 printf ("nib = %zu\n", (size_t)nib);
120 printf ("nino = %zu\n", (size_t)nino);
121 printf ("nibms = %zu\n", (size_t)nibms);
122 printf ("ndbms = %zu\n", (size_t)ndbms);
123 printf ("ndata = %zu\n", (size_t)ndata);
124 printf ("ibmoff = %zu\n", (size_t)sb.ibmoff);
125 printf ("dbmoff = %zu\n", (size_t)sb.dbmoff);
126 printf ("ioff = %zu\n", (size_t)sb.ioff);
127 printf ("doff = %zu\n", (size_t)sb.doff);
129 // clear inode bitmap
130 xlseek (fd, sb.ibmoff * bs);
131 memset (buf, 0xff, bs);
133 xwrite (fd, buf, bs);
135 for (uint32_t i = 1; i < nibms; ++i)
136 xwrite (fd, buf, bs);
139 xlseek (fd, sb.dbmoff * bs);
141 xwrite (fd, buf, bs);
143 for (uint32_t i = 1; i < ndbms; ++i)
144 xwrite (fd, buf, bs);
146 // initialize root directory
148 ent = (struct sufs_dirent *)buf;
149 ent->ino = SUFS_INO_ROOT;
153 ent = (struct sufs_dirent *)(buf + 16);
154 ent->ino = SUFS_INO_ROOT;
159 xlseek (fd, sb.doff * bs);
160 xwrite (fd, buf, bs);
162 // initialize inode 0 & 1
163 now = (int64_t)time (NULL);
164 memset (&in, 0, sizeof (in));
165 in.mode = S_IFDIR | 0755;
175 in.direct[0] = sb.doff;
177 memcpy (buf + sizeof (in), &in, sizeof (in));
178 xlseek (fd, sb.ioff * bs);
179 xwrite (fd, buf, bs);