]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/newfs_nandfs/newfs_nandfs.c
Followup to r347996
[FreeBSD/FreeBSD.git] / sbin / newfs_nandfs / newfs_nandfs.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2012 Semihalf.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/fdcio.h>
34 #include <sys/disk.h>
35 #include <sys/disklabel.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/endian.h>
40 #include <sys/stddef.h>
41 #include <sys/uuid.h>
42 #include <sys/dirent.h>
43 #include <sys/stat.h>
44
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <inttypes.h>
50 #include <libgeom.h>
51 #include <paths.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 #include <fs/nandfs/nandfs_fs.h>
59 #include <dev/nand/nand_dev.h>
60
61 #define DEBUG
62 #undef DEBUG
63 #ifdef DEBUG
64 #define debug(fmt, args...) do { \
65         printf("nandfs:" fmt "\n", ##args); } while (0)
66 #else
67 #define debug(fmt, args...)
68 #endif
69
70 #define NANDFS_FIRST_BLOCK      nandfs_first_block()
71 #define NANDFS_FIRST_CNO                1
72 #define NANDFS_BLOCK_BAD        1
73 #define NANDFS_BLOCK_GOOD       0
74
75 struct file_info {
76         uint64_t        ino;
77         const char      *name;
78         uint32_t        mode;
79         uint64_t        size;
80         uint8_t         nblocks;
81         uint32_t        *blocks;
82         struct nandfs_inode *inode;
83 };
84
85 static struct file_info user_files[] = {
86         { NANDFS_ROOT_INO, NULL, S_IFDIR | 0755, 0, 1, NULL, NULL },
87 };
88
89 static struct file_info ifile =
90         { NANDFS_IFILE_INO, NULL, 0, 0, -1, NULL, NULL };
91 static struct file_info sufile =
92         { NANDFS_SUFILE_INO, NULL, 0, 0, -1, NULL, NULL };
93 static struct file_info cpfile =
94         { NANDFS_CPFILE_INO, NULL, 0, 0, -1, NULL, NULL };
95 static struct file_info datfile =
96         { NANDFS_DAT_INO, NULL, 0, 0, -1, NULL, NULL };
97
98 struct nandfs_block {
99         LIST_ENTRY(nandfs_block) block_link;
100         uint32_t number;
101         uint64_t offset;
102         void    *data;
103 };
104
105 static LIST_HEAD(, nandfs_block) block_head =
106         LIST_HEAD_INITIALIZER(&block_head);
107
108 /* Storage geometry */
109 static off_t mediasize;
110 static ssize_t sectorsize;
111 static uint64_t nsegments;
112 static uint64_t erasesize;
113 static uint64_t segsize;
114
115 static struct nandfs_fsdata fsdata;
116 static struct nandfs_super_block super_block;
117
118 static int is_nand;
119
120 /* Nandfs parameters */
121 static size_t blocksize = NANDFS_DEF_BLOCKSIZE;
122 static long blocks_per_segment;
123 static long rsv_segment_percent = 5;
124 static time_t nandfs_time;
125 static uint32_t bad_segments_count = 0;
126 static uint32_t *bad_segments = NULL;
127 static uint8_t fsdata_blocks_state[NANDFS_NFSAREAS];
128
129 static u_char *volumelabel = NULL;
130
131 static struct nandfs_super_root *sr;
132
133 static uint32_t nuserfiles;
134 static uint32_t seg_nblocks;
135 static uint32_t seg_endblock;
136
137 #define SIZE_TO_BLOCK(size) howmany(size, blocksize)
138
139 static uint32_t
140 nandfs_first_block(void)
141 {
142         uint32_t i, first_free, start_bad_segments = 0;
143
144         for (i = 0; i < bad_segments_count; i++) {
145                 if (i == bad_segments[i])
146                         start_bad_segments++;
147                 else
148                         break;
149         }
150
151         first_free = SIZE_TO_BLOCK(NANDFS_DATA_OFFSET_BYTES(erasesize) +
152             (start_bad_segments * segsize));
153
154         if (first_free < (uint32_t)blocks_per_segment)
155                 return (blocks_per_segment);
156         else
157                 return (first_free);
158 }
159
160 static void
161 usage(void)
162 {
163
164         fprintf(stderr,
165             "usage: newfs_nandfs [ -options ] device\n"
166             "where the options are:\n"
167             "\t-b block-size\n"
168             "\t-B blocks-per-segment\n"
169             "\t-L volume label\n"
170             "\t-m reserved-segments-percentage\n");
171         exit(1);
172 }
173
174 static int
175 nandfs_log2(unsigned n)
176 {
177         unsigned count;
178
179         /*
180          * N.B. this function will return 0 if supplied 0.
181          */
182         for (count = 0; n/2; count++)
183                 n /= 2;
184         return count;
185 }
186
187 /* from NetBSD's src/sys/net/if_ethersubr.c */
188 static uint32_t
189 crc32_le(uint32_t crc, const uint8_t *buf, size_t len)
190 {
191         static const uint32_t crctab[] = {
192                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
193                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
194                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
195                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
196         };
197         size_t i;
198
199         crc = crc ^ ~0U;
200
201         for (i = 0; i < len; i++) {
202                 crc ^= buf[i];
203                 crc = (crc >> 4) ^ crctab[crc & 0xf];
204                 crc = (crc >> 4) ^ crctab[crc & 0xf];
205         }
206
207         return (crc ^ ~0U);
208 }
209
210 static void *
211 get_block(uint32_t block_nr, uint64_t offset)
212 {
213         struct nandfs_block *block, *new_block;
214
215         LIST_FOREACH(block, &block_head, block_link) {
216                 if (block->number == block_nr)
217                         return block->data;
218         }
219
220         debug("allocating block %x\n", block_nr);
221
222         new_block = malloc(sizeof(*block));
223         if (!new_block)
224                 err(1, "cannot allocate block");
225
226         new_block->number = block_nr;
227         new_block->offset = offset;
228         new_block->data = malloc(blocksize);
229         if (!new_block->data)
230                 err(1, "cannot allocate block data");
231
232         memset(new_block->data, 0, blocksize);
233
234         LIST_INSERT_HEAD(&block_head, new_block, block_link);
235
236         return (new_block->data);
237 }
238
239 static int
240 nandfs_seg_usage_blk_offset(uint64_t seg, uint64_t *blk, uint64_t *offset)
241 {
242         uint64_t off;
243         uint16_t seg_size;
244
245         seg_size = sizeof(struct nandfs_segment_usage);
246
247         off = roundup(sizeof(struct nandfs_sufile_header), seg_size);
248         off += (seg * seg_size);
249
250         *blk = off / blocksize;
251         *offset = (off % blocksize) / seg_size;
252         return (0);
253 }
254
255 static uint32_t
256 segment_size(void)
257 {
258         u_int size;
259
260         size = sizeof(struct nandfs_segment_summary );
261         size += seg_nblocks * sizeof(struct nandfs_binfo_v);
262
263         if (size > blocksize)
264                 err(1, "segsum info bigger that blocksize");
265
266         return (size);
267 }
268
269
270 static void
271 prepare_blockgrouped_file(uint32_t block)
272 {
273         struct nandfs_block_group_desc *desc;
274         uint32_t i, entries;
275
276         desc = (struct nandfs_block_group_desc *)get_block(block, 0);
277         entries = blocksize / sizeof(struct nandfs_block_group_desc);
278         for (i = 0; i < entries; i++)
279                 desc[i].bg_nfrees = blocksize * 8;
280 }
281
282 static void
283 alloc_blockgrouped_file(uint32_t block, uint32_t entry)
284 {
285         struct nandfs_block_group_desc *desc;
286         uint32_t desc_nr;
287         uint32_t *bitmap;
288
289         desc = (struct nandfs_block_group_desc *)get_block(block, 0);
290         bitmap = (uint32_t *)get_block(block + 1, 1);
291
292         bitmap += (entry >> 5);
293         if (*bitmap & (1 << (entry % 32))) {
294                 printf("nandfs: blockgrouped entry %d already allocated\n",
295                     entry);
296         }
297         *bitmap |= (1 << (entry % 32));
298
299         desc_nr = entry / (blocksize * 8);
300         desc[desc_nr].bg_nfrees--;
301 }
302
303
304 static uint64_t
305 count_su_blocks(void)
306 {
307         uint64_t maxblk, blk, offset, i;
308
309         maxblk = blk = 0;
310
311         for (i = 0; i < bad_segments_count; i++) {
312                 nandfs_seg_usage_blk_offset(bad_segments[i], &blk, &offset);
313                 debug("bad segment at block:%jx off: %jx", blk, offset);
314                 if (blk > maxblk)
315                         maxblk = blk;
316         }
317
318         debug("bad segment needs %#jx", blk);
319         if (blk >= NANDFS_NDADDR) {
320                 printf("nandfs: file too big (%jd > %d)\n", blk, NANDFS_NDADDR);
321                 exit(2);
322         }
323
324         sufile.size = (blk + 1) * blocksize;
325         return (blk + 1);
326 }
327
328 static void
329 count_seg_blocks(void)
330 {
331         uint32_t i;
332
333         for (i = 0; i < nuserfiles; i++)
334                 if (user_files[i].nblocks) {
335                         seg_nblocks += user_files[i].nblocks;
336                         user_files[i].blocks = malloc(user_files[i].nblocks * sizeof(uint32_t));
337                 }
338
339         ifile.nblocks = 2 +
340             SIZE_TO_BLOCK(sizeof(struct nandfs_inode) * (NANDFS_USER_INO + 1));
341         ifile.blocks = malloc(ifile.nblocks * sizeof(uint32_t));
342         seg_nblocks += ifile.nblocks;
343
344         cpfile.nblocks =
345             SIZE_TO_BLOCK((NANDFS_CPFILE_FIRST_CHECKPOINT_OFFSET + 1) *
346             sizeof(struct nandfs_checkpoint));
347         cpfile.blocks = malloc(cpfile.nblocks * sizeof(uint32_t));
348         seg_nblocks += cpfile.nblocks;
349
350         if (!bad_segments) {
351                 sufile.nblocks =
352                     SIZE_TO_BLOCK((NANDFS_SUFILE_FIRST_SEGMENT_USAGE_OFFSET + 1) *
353                     sizeof(struct nandfs_segment_usage));
354         } else {
355                 debug("bad blocks found: extra space for sufile");
356                 sufile.nblocks = count_su_blocks();
357         }
358
359         sufile.blocks = malloc(sufile.nblocks * sizeof(uint32_t));
360         seg_nblocks += sufile.nblocks;
361
362         datfile.nblocks = 2 +
363             SIZE_TO_BLOCK((seg_nblocks) * sizeof(struct nandfs_dat_entry));
364         datfile.blocks = malloc(datfile.nblocks * sizeof(uint32_t));
365         seg_nblocks += datfile.nblocks;
366 }
367
368 static void
369 assign_file_blocks(uint64_t start_block)
370 {
371         uint32_t i, j;
372
373         for (i = 0; i < nuserfiles; i++)
374                 for (j = 0; j < user_files[i].nblocks; j++) {
375                         debug("user file %d at block %d at %#jx",
376                             i, j, (uintmax_t)start_block);
377                         user_files[i].blocks[j] = start_block++;
378                 }
379
380         for (j = 0; j < ifile.nblocks; j++) {
381                 debug("ifile block %d at %#jx", j, (uintmax_t)start_block);
382                 ifile.blocks[j] = start_block++;
383         }
384
385         for (j = 0; j < cpfile.nblocks; j++) {
386                 debug("cpfile block %d at %#jx", j, (uintmax_t)start_block);
387                 cpfile.blocks[j] = start_block++;
388         }
389
390         for (j = 0; j < sufile.nblocks; j++) {
391                 debug("sufile block %d at %#jx", j, (uintmax_t)start_block);
392                 sufile.blocks[j] = start_block++;
393         }
394
395         for (j = 0; j < datfile.nblocks; j++) {
396                 debug("datfile block %d at %#jx", j, (uintmax_t)start_block);
397                 datfile.blocks[j] = start_block++;
398         }
399
400         /* add one for superroot */
401         debug("sr at block %#jx", (uintmax_t)start_block);
402         sr = (struct nandfs_super_root *)get_block(start_block++, 0);
403         seg_endblock = start_block;
404 }
405
406 static void
407 save_datfile(void)
408 {
409
410         prepare_blockgrouped_file(datfile.blocks[0]);
411 }
412
413 static uint64_t
414 update_datfile(uint64_t block)
415 {
416         struct nandfs_dat_entry *dat;
417         static uint64_t vblock = 0;
418         uint64_t allocated, i, off;
419
420         if (vblock == 0) {
421                 alloc_blockgrouped_file(datfile.blocks[0], vblock);
422                 vblock++;
423         }
424         allocated = vblock;
425         i = vblock / (blocksize / sizeof(*dat));
426         off = vblock % (blocksize / sizeof(*dat));
427         vblock++;
428
429         dat = (struct nandfs_dat_entry *)get_block(datfile.blocks[2 + i], 2 + i);
430
431         alloc_blockgrouped_file(datfile.blocks[0], allocated);
432         dat[off].de_blocknr = block;
433         dat[off].de_start = NANDFS_FIRST_CNO;
434         dat[off].de_end = UINTMAX_MAX;
435
436         return (allocated);
437 }
438
439 static union nandfs_binfo *
440 update_block_info(union nandfs_binfo *binfo, struct file_info *file)
441 {
442         nandfs_daddr_t vblock;
443         uint32_t i;
444
445         for (i = 0; i < file->nblocks; i++) {
446                 debug("%s: blk %x", __func__, i);
447                 if (file->ino != NANDFS_DAT_INO) {
448                         vblock = update_datfile(file->blocks[i]);
449                         binfo->bi_v.bi_vblocknr = vblock;
450                         binfo->bi_v.bi_blkoff = i;
451                         binfo->bi_v.bi_ino = file->ino;
452                         file->inode->i_db[i] = vblock;
453                 } else {
454                         binfo->bi_dat.bi_blkoff = i;
455                         binfo->bi_dat.bi_ino = file->ino;
456                         file->inode->i_db[i] = datfile.blocks[i];
457                 }
458                 binfo++;
459         }
460
461         return (binfo);
462 }
463
464 static void
465 save_segsum(struct nandfs_segment_summary *ss)
466 {
467         union nandfs_binfo *binfo;
468         struct nandfs_block *block;
469         uint32_t sum_bytes, i;
470         uint8_t crc_data, crc_skip;
471
472         sum_bytes = segment_size();
473         ss->ss_magic = NANDFS_SEGSUM_MAGIC;
474         ss->ss_bytes = sizeof(struct nandfs_segment_summary);
475         ss->ss_flags = NANDFS_SS_LOGBGN | NANDFS_SS_LOGEND | NANDFS_SS_SR;
476         ss->ss_seq = 1;
477         ss->ss_create = nandfs_time;
478
479         ss->ss_next = nandfs_first_block() + blocks_per_segment;
480         /* nblocks = segment blocks + segsum block + superroot */
481         ss->ss_nblocks = seg_nblocks + 2;
482         ss->ss_nbinfos = seg_nblocks;
483         ss->ss_sumbytes = sum_bytes;
484
485         crc_skip = sizeof(ss->ss_datasum) + sizeof(ss->ss_sumsum);
486         ss->ss_sumsum = crc32_le(0, (uint8_t *)ss + crc_skip,
487             sum_bytes - crc_skip);
488         crc_data = 0;
489
490         binfo = (union nandfs_binfo *)(ss + 1);
491         for (i = 0; i < nuserfiles; i++) {
492                 if (user_files[i].nblocks)
493                         binfo = update_block_info(binfo, &user_files[i]);
494         }
495
496         binfo = update_block_info(binfo, &ifile);
497         binfo = update_block_info(binfo, &cpfile);
498         binfo = update_block_info(binfo, &sufile);
499         update_block_info(binfo, &datfile);
500
501         /* save superroot crc */
502         crc_skip = sizeof(sr->sr_sum);
503         sr->sr_sum = crc32_le(0, (uint8_t *)sr + crc_skip,
504             NANDFS_SR_BYTES - crc_skip);
505
506         /* segment checksup */
507         crc_skip = sizeof(ss->ss_datasum);
508         LIST_FOREACH(block, &block_head, block_link) {
509                 if (block->number < NANDFS_FIRST_BLOCK)
510                         continue;
511                 if (block->number == NANDFS_FIRST_BLOCK)
512                         crc_data = crc32_le(0,
513                             (uint8_t *)block->data + crc_skip,
514                             blocksize - crc_skip);
515                 else
516                         crc_data = crc32_le(crc_data, (uint8_t *)block->data,
517                             blocksize);
518         }
519         ss->ss_datasum = crc_data;
520 }
521
522 static void
523 create_fsdata(void)
524 {
525         struct uuid tmp;
526
527         memset(&fsdata, 0, sizeof(struct nandfs_fsdata));
528
529         fsdata.f_magic = NANDFS_FSDATA_MAGIC;
530         fsdata.f_nsegments = nsegments;
531         fsdata.f_erasesize = erasesize;
532         fsdata.f_first_data_block = NANDFS_FIRST_BLOCK;
533         fsdata.f_blocks_per_segment = blocks_per_segment;
534         fsdata.f_r_segments_percentage = rsv_segment_percent;
535         fsdata.f_rev_level = NANDFS_CURRENT_REV;
536         fsdata.f_sbbytes = NANDFS_SB_BYTES;
537         fsdata.f_bytes = NANDFS_FSDATA_CRC_BYTES;
538         fsdata.f_ctime = nandfs_time;
539         fsdata.f_log_block_size = nandfs_log2(blocksize) - 10;
540         fsdata.f_errors = 1;
541         fsdata.f_inode_size = sizeof(struct nandfs_inode);
542         fsdata.f_dat_entry_size = sizeof(struct nandfs_dat_entry);
543         fsdata.f_checkpoint_size = sizeof(struct nandfs_checkpoint);
544         fsdata.f_segment_usage_size = sizeof(struct nandfs_segment_usage);
545
546         uuidgen(&tmp, 1);
547         fsdata.f_uuid = tmp;
548
549         if (volumelabel)
550                 memcpy(fsdata.f_volume_name, volumelabel, 16);
551
552         fsdata.f_sum = crc32_le(0, (const uint8_t *)&fsdata,
553             NANDFS_FSDATA_CRC_BYTES);
554 }
555
556 static void
557 save_fsdata(void *data)
558 {
559
560         memcpy(data, &fsdata, sizeof(fsdata));
561 }
562
563 static void
564 create_super_block(void)
565 {
566
567         memset(&super_block, 0, sizeof(struct nandfs_super_block));
568
569         super_block.s_magic = NANDFS_SUPER_MAGIC;
570         super_block.s_last_cno = NANDFS_FIRST_CNO;
571         super_block.s_last_pseg = NANDFS_FIRST_BLOCK;
572         super_block.s_last_seq = 1;
573         super_block.s_free_blocks_count =
574             (nsegments - bad_segments_count) * blocks_per_segment;
575         super_block.s_mtime = 0;
576         super_block.s_wtime = nandfs_time;
577         super_block.s_state = NANDFS_VALID_FS;
578
579         super_block.s_sum = crc32_le(0, (const uint8_t *)&super_block,
580             NANDFS_SB_BYTES);
581 }
582
583 static void
584 save_super_block(void *data)
585 {
586
587         memcpy(data, &super_block, sizeof(super_block));
588 }
589
590 static void
591 save_super_root(void)
592 {
593
594         sr->sr_bytes = NANDFS_SR_BYTES;
595         sr->sr_flags = 0;
596         sr->sr_nongc_ctime = nandfs_time;
597         datfile.inode = &sr->sr_dat;
598         cpfile.inode = &sr->sr_cpfile;
599         sufile.inode = &sr->sr_sufile;
600 }
601
602 static struct nandfs_dir_entry *
603 add_de(void *block, struct nandfs_dir_entry *de, uint64_t ino,
604     const char *name, uint8_t type)
605 {
606         uint16_t reclen;
607
608         /* modify last de */
609         de->rec_len = NANDFS_DIR_REC_LEN(de->name_len);
610         de = (void *)((uint8_t *)de + de->rec_len);
611
612         reclen = blocksize - ((uintptr_t)de - (uintptr_t)block);
613         if (reclen < NANDFS_DIR_REC_LEN(strlen(name))) {
614                 printf("nandfs: too many dir entries for one block\n");
615                 return (NULL);
616         }
617
618         de->inode = ino;
619         de->rec_len = reclen;
620         de->name_len = strlen(name);
621         de->file_type = type;
622         memset(de->name, 0,
623             (strlen(name) + NANDFS_DIR_PAD - 1) & ~NANDFS_DIR_ROUND);
624         memcpy(de->name, name, strlen(name));
625
626         return (de);
627 }
628
629 static struct nandfs_dir_entry *
630 make_dir(void *block, uint64_t ino, uint64_t parent_ino)
631 {
632         struct nandfs_dir_entry *de = (struct nandfs_dir_entry *)block;
633
634         /* create '..' entry */
635         de->inode = parent_ino;
636         de->rec_len = NANDFS_DIR_REC_LEN(2);
637         de->name_len = 2;
638         de->file_type = DT_DIR;
639         memset(de->name, 0, NANDFS_DIR_NAME_LEN(2));
640         memcpy(de->name, "..", 2);
641
642         /* create '.' entry */
643         de = (void *)((uint8_t *)block + NANDFS_DIR_REC_LEN(2));
644         de->inode = ino;
645         de->rec_len = blocksize - NANDFS_DIR_REC_LEN(2);
646         de->name_len = 1;
647         de->file_type = DT_DIR;
648         memset(de->name, 0, NANDFS_DIR_NAME_LEN(1));
649         memcpy(de->name, ".", 1);
650
651         return (de);
652 }
653
654 static void
655 save_root_dir(void)
656 {
657         struct file_info *root = &user_files[0];
658         struct nandfs_dir_entry *de;
659         uint32_t i;
660         void *block;
661
662         block = get_block(root->blocks[0], 0);
663
664         de = make_dir(block, root->ino, root->ino);
665         for (i = 1; i < nuserfiles; i++)
666                 de = add_de(block, de, user_files[i].ino, user_files[i].name,
667                     IFTODT(user_files[i].mode));
668
669         root->size = ((uintptr_t)de - (uintptr_t)block) +
670             NANDFS_DIR_REC_LEN(de->name_len);
671 }
672
673 static void
674 save_sufile(void)
675 {
676         struct nandfs_sufile_header *header;
677         struct nandfs_segment_usage *su;
678         uint64_t blk, i, off;
679         void *block;
680         int start;
681
682         /*
683          * At the beginning just zero-out everything
684          */
685         for (i = 0; i < sufile.nblocks; i++)
686                 get_block(sufile.blocks[i], 0);
687
688         start = 0;
689
690         block = get_block(sufile.blocks[start], 0);
691         header = (struct nandfs_sufile_header *)block;
692         header->sh_ncleansegs = nsegments - bad_segments_count - 1;
693         header->sh_ndirtysegs = 1;
694         header->sh_last_alloc = 1;
695
696         su = (struct nandfs_segment_usage *)header;
697         off = NANDFS_SUFILE_FIRST_SEGMENT_USAGE_OFFSET;
698         /* Allocate data segment */
699         su[off].su_lastmod = nandfs_time;
700         /* nblocks = segment blocks + segsum block + superroot */
701         su[off].su_nblocks = seg_nblocks + 2;
702         su[off].su_flags = NANDFS_SEGMENT_USAGE_DIRTY;
703         off++;
704         /* Allocate next segment */
705         su[off].su_lastmod = nandfs_time;
706         su[off].su_nblocks = 0;
707         su[off].su_flags = NANDFS_SEGMENT_USAGE_DIRTY;
708         for (i = 0; i < bad_segments_count; i++) {
709                 nandfs_seg_usage_blk_offset(bad_segments[i], &blk, &off);
710                 debug("storing bad_segments[%jd]=%x at %jx off %jx\n", i,
711                     bad_segments[i], blk, off);
712                 block = get_block(sufile.blocks[blk],
713                     off * sizeof(struct nandfs_segment_usage *));
714                 su = (struct nandfs_segment_usage *)block;
715                 su[off].su_lastmod = nandfs_time;
716                 su[off].su_nblocks = 0;
717                 su[off].su_flags = NANDFS_SEGMENT_USAGE_ERROR;
718         }
719 }
720
721 static void
722 save_cpfile(void)
723 {
724         struct nandfs_cpfile_header *header;
725         struct nandfs_checkpoint *cp, *initial_cp;
726         int i, entries = blocksize / sizeof(struct nandfs_checkpoint);
727         uint64_t cno;
728
729         header = (struct nandfs_cpfile_header *)get_block(cpfile.blocks[0], 0);
730         header->ch_ncheckpoints = 1;
731         header->ch_nsnapshots = 0;
732
733         cp = (struct nandfs_checkpoint *)header;
734
735         /* fill first checkpoint data*/
736         initial_cp = &cp[NANDFS_CPFILE_FIRST_CHECKPOINT_OFFSET];
737         initial_cp->cp_flags = 0;
738         initial_cp->cp_checkpoints_count = 0;
739         initial_cp->cp_cno = NANDFS_FIRST_CNO;
740         initial_cp->cp_create = nandfs_time;
741         initial_cp->cp_nblk_inc = seg_endblock - 1;
742         initial_cp->cp_blocks_count = seg_nblocks;
743         memset(&initial_cp->cp_snapshot_list, 0,
744             sizeof(struct nandfs_snapshot_list));
745
746         ifile.inode = &initial_cp->cp_ifile_inode;
747
748         /* mark rest of cp as invalid */
749         cno = NANDFS_FIRST_CNO + 1;
750         i = NANDFS_CPFILE_FIRST_CHECKPOINT_OFFSET + 1;
751         for (; i < entries; i++) {
752                 cp[i].cp_cno = cno++;
753                 cp[i].cp_flags = NANDFS_CHECKPOINT_INVALID;
754         }
755 }
756
757 static void
758 init_inode(struct nandfs_inode *inode, struct file_info *file)
759 {
760
761         inode->i_blocks = file->nblocks;
762         inode->i_ctime = nandfs_time;
763         inode->i_mtime = nandfs_time;
764         inode->i_mode = file->mode & 0xffff;
765         inode->i_links_count = 1;
766
767         if (file->size > 0)
768                 inode->i_size = file->size;
769         else
770                 inode->i_size = 0;
771
772         if (file->ino == NANDFS_USER_INO)
773                 inode->i_flags = SF_NOUNLINK|UF_NOUNLINK;
774         else
775                 inode->i_flags = 0;
776 }
777
778 static void
779 save_ifile(void)
780 {
781         struct nandfs_inode *inode;
782         struct file_info *file;
783         uint64_t ino, blk, off;
784         uint32_t i;
785
786         prepare_blockgrouped_file(ifile.blocks[0]);
787         for (i = 0; i <= NANDFS_USER_INO; i++)
788                 alloc_blockgrouped_file(ifile.blocks[0], i);
789
790         for (i = 0; i < nuserfiles; i++) {
791                 file = &user_files[i];
792                 ino = file->ino;
793                 blk = ino / (blocksize / sizeof(*inode));
794                 off = ino % (blocksize / sizeof(*inode));
795                 inode =
796                     (struct nandfs_inode *)get_block(ifile.blocks[2 + blk], 2 + blk);
797                 file->inode = &inode[off];
798                 init_inode(file->inode, file);
799         }
800
801         init_inode(ifile.inode, &ifile);
802         init_inode(cpfile.inode, &cpfile);
803         init_inode(sufile.inode, &sufile);
804         init_inode(datfile.inode, &datfile);
805 }
806
807 static int
808 create_fs(void)
809 {
810         uint64_t start_block;
811         uint32_t segsum_size;
812         char *data;
813         int i;
814
815         nuserfiles = nitems(user_files);
816
817         /* Count and assign blocks */
818         count_seg_blocks();
819         segsum_size = segment_size();
820         start_block = NANDFS_FIRST_BLOCK + SIZE_TO_BLOCK(segsum_size);
821         assign_file_blocks(start_block);
822
823         /* Create super root structure */
824         save_super_root();
825
826         /* Create root directory */
827         save_root_dir();
828
829         /* Fill in file contents */
830         save_sufile();
831         save_cpfile();
832         save_ifile();
833         save_datfile();
834
835         /* Save fsdata and superblocks */
836         create_fsdata();
837         create_super_block();
838
839         for (i = 0; i < NANDFS_NFSAREAS; i++) {
840                 if (fsdata_blocks_state[i] != NANDFS_BLOCK_GOOD)
841                         continue;
842
843                 data = get_block((i * erasesize)/blocksize, 0);
844                 save_fsdata(data);
845
846                 data = get_block((i * erasesize + NANDFS_SBLOCK_OFFSET_BYTES) /
847                     blocksize, 0);
848                 if (blocksize > NANDFS_SBLOCK_OFFSET_BYTES)
849                         data += NANDFS_SBLOCK_OFFSET_BYTES;
850                 save_super_block(data);
851                 memset(data + sizeof(struct nandfs_super_block), 0xff,
852                     (blocksize - sizeof(struct nandfs_super_block) -
853                     NANDFS_SBLOCK_OFFSET_BYTES));
854         }
855
856         /* Save segment summary and CRCs */
857         save_segsum(get_block(NANDFS_FIRST_BLOCK, 0));
858
859         return (0);
860 }
861
862 static void
863 write_fs(int fda)
864 {
865         struct nandfs_block *block;
866         char *data;
867         u_int ret;
868
869         /* Overwrite next block with ff if not nand device */
870         if (!is_nand) {
871                 data = get_block(seg_endblock, 0);
872                 memset(data, 0xff, blocksize);
873         }
874
875         LIST_FOREACH(block, &block_head, block_link) {
876                 lseek(fda, block->number * blocksize, SEEK_SET);
877                 ret = write(fda, block->data, blocksize);
878                 if (ret != blocksize)
879                         err(1, "cannot write filesystem data");
880         }
881 }
882
883 static void
884 check_parameters(void)
885 {
886         int i;
887
888         /* check blocksize */
889         if ((blocksize < NANDFS_MIN_BLOCKSIZE) || (blocksize > MAXBSIZE) ||
890             ((blocksize - 1) & blocksize)) {
891                 errx(1, "Bad blocksize (%zu). Must be in range [%u-%u] "
892                     "and a power of two.", blocksize, NANDFS_MIN_BLOCKSIZE,
893                     MAXBSIZE);
894         }
895
896         /* check blocks per segments */
897         if ((blocks_per_segment < NANDFS_SEG_MIN_BLOCKS) ||
898             ((blocksize - 1) & blocksize))
899                 errx(1, "Bad blocks per segment (%lu). Must be greater than "
900                     "%u and a power of two.", blocks_per_segment,
901                     NANDFS_SEG_MIN_BLOCKS);
902
903         /* check reserved segment percentage */
904         if ((rsv_segment_percent < 1) || (rsv_segment_percent > 99))
905                 errx(1, "Bad reserved segment percentage. "
906                     "Must in range 1..99.");
907
908         /* check volume label */
909         i = 0;
910         if (volumelabel) {
911                 while (isalnum(volumelabel[++i]))
912                         ;
913
914                 if (volumelabel[i] != '\0') {
915                         errx(1, "bad volume label. "
916                             "Valid characters are alphanumerics.");
917                 }
918
919                 if (strlen(volumelabel) >= 16)
920                         errx(1, "Bad volume label. Length is longer than %d.",
921                             16);
922         }
923
924         nandfs_time = time(NULL);
925 }
926
927 static void
928 print_parameters(void)
929 {
930
931         printf("filesystem parameters:\n");
932         printf("blocksize: %#zx sectorsize: %#zx\n", blocksize, sectorsize);
933         printf("erasesize: %#jx mediasize: %#jx\n", erasesize, mediasize);
934         printf("segment size: %#jx blocks per segment: %#x\n", segsize,
935             (uint32_t)blocks_per_segment);
936 }
937
938 /*
939  * Exit with error if file system is mounted.
940  */
941 static void
942 check_mounted(const char *fname, mode_t mode)
943 {
944         struct statfs *mp;
945         const char *s1, *s2;
946         size_t len;
947         int n, r;
948
949         if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
950                 err(1, "getmntinfo");
951
952         len = strlen(_PATH_DEV);
953         s1 = fname;
954         if (!strncmp(s1, _PATH_DEV, len))
955                 s1 += len;
956
957         r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
958
959         for (; n--; mp++) {
960                 s2 = mp->f_mntfromname;
961
962                 if (!strncmp(s2, _PATH_DEV, len))
963                         s2 += len;
964                 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
965                     !strcmp(s1, s2))
966                         errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
967         }
968 }
969
970 static void
971 calculate_geometry(int fd)
972 {
973         struct chip_param_io chip_params;
974         char ident[DISK_IDENT_SIZE];
975         char medianame[MAXPATHLEN];
976
977         /* Check storage type */
978         g_get_ident(fd, ident, DISK_IDENT_SIZE);
979         g_get_name(ident, medianame, MAXPATHLEN);
980         debug("device name: %s", medianame);
981
982         is_nand = (strstr(medianame, "gnand") != NULL);
983         debug("is_nand = %d", is_nand);
984
985         sectorsize = g_sectorsize(fd);
986         debug("sectorsize: %#zx", sectorsize);
987
988         /* Get storage size */
989         mediasize = g_mediasize(fd);
990         debug("mediasize: %#jx", mediasize);
991
992         /* Get storage erase unit size */
993         if (!is_nand)
994                 erasesize = NANDFS_DEF_ERASESIZE;
995         else if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) != -1)
996                 erasesize = chip_params.page_size * chip_params.pages_per_block;
997         else
998                 errx(1, "Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
999
1000         debug("erasesize: %#jx", (uintmax_t)erasesize);
1001
1002         if (blocks_per_segment == 0) {
1003                 if (erasesize >= NANDFS_MIN_SEGSIZE)
1004                         blocks_per_segment = erasesize / blocksize;
1005                 else
1006                         blocks_per_segment = NANDFS_MIN_SEGSIZE / blocksize;
1007         }
1008
1009         /* Calculate number of segments */
1010         segsize = blocksize * blocks_per_segment;
1011         nsegments = ((mediasize - NANDFS_NFSAREAS * erasesize) / segsize) - 2;
1012         debug("segsize: %#jx", segsize);
1013         debug("nsegments: %#jx", nsegments);
1014 }
1015
1016 static void
1017 erase_device(int fd)
1018 {
1019         int rest, failed;
1020         uint64_t i, nblocks;
1021         off_t offset;
1022
1023         failed = 0;
1024         for (i = 0; i < NANDFS_NFSAREAS; i++) {
1025                 debug("Deleting %jx\n", i * erasesize);
1026                 if (g_delete(fd, i * erasesize, erasesize)) {
1027                         printf("cannot delete %jx\n", i * erasesize);
1028                         fsdata_blocks_state[i] = NANDFS_BLOCK_BAD;
1029                         failed++;
1030                 } else
1031                         fsdata_blocks_state[i] = NANDFS_BLOCK_GOOD;
1032         }
1033
1034         if (failed == NANDFS_NFSAREAS) {
1035                 printf("%d first blocks not usable. Unable to create "
1036                     "filesystem.\n", failed);
1037                 exit(1);
1038         }
1039
1040         for (i = 0; i < nsegments; i++) {
1041                 offset = NANDFS_NFSAREAS * erasesize + i * segsize;
1042                 if (g_delete(fd, offset, segsize)) {
1043                         printf("cannot delete segment %jx (offset %jd)\n",
1044                             i, offset);
1045                         bad_segments_count++;
1046                         bad_segments = realloc(bad_segments,
1047                             bad_segments_count * sizeof(uint32_t));
1048                         bad_segments[bad_segments_count - 1] = i;
1049                 }
1050         }
1051
1052         if (bad_segments_count == nsegments) {
1053                 printf("no valid segments\n");
1054                 exit(1);
1055         }
1056
1057         /* Delete remaining blocks at the end of device */
1058         rest = mediasize % segsize;
1059         nblocks = rest / erasesize;
1060         for (i = 0; i < nblocks; i++) {
1061                 offset = (segsize * nsegments) + (i * erasesize);
1062                 if (g_delete(fd, offset, erasesize)) {
1063                         printf("cannot delete space after last segment "
1064                             "- probably a bad block\n");
1065                 }
1066         }
1067 }
1068
1069 static void
1070 erase_initial(int fd)
1071 {
1072         char buf[512];
1073         u_int i;
1074
1075         memset(buf, 0xff, sizeof(buf));
1076
1077         lseek(fd, 0, SEEK_SET);
1078         for (i = 0; i < NANDFS_NFSAREAS * erasesize; i += sizeof(buf))
1079                 write(fd, buf, sizeof(buf));
1080 }
1081
1082 static void
1083 create_nandfs(int fd)
1084 {
1085
1086         create_fs();
1087
1088         write_fs(fd);
1089 }
1090
1091 static void
1092 print_summary(void)
1093 {
1094
1095         printf("filesystem was created successfully\n");
1096         printf("total segments: %#jx valid segments: %#jx\n", nsegments,
1097             nsegments - bad_segments_count);
1098         printf("total space: %ju MB free: %ju MB\n",
1099             (nsegments *
1100             blocks_per_segment * blocksize) / (1024 * 1024),
1101             ((nsegments - bad_segments_count) *
1102             blocks_per_segment * blocksize) / (1024 * 1024));
1103 }
1104
1105 int
1106 main(int argc, char *argv[])
1107 {
1108         struct stat sb;
1109         char buf[MAXPATHLEN];
1110         const char opts[] = "b:B:L:m:";
1111         const char *fname;
1112         int ch, fd;
1113
1114         while ((ch = getopt(argc, argv, opts)) != -1) {
1115                 switch (ch) {
1116                 case 'b':
1117                         blocksize = strtol(optarg, (char **)NULL, 10);
1118                         if (blocksize == 0)
1119                                 usage();
1120                         break;
1121                 case 'B':
1122                         blocks_per_segment = strtol(optarg, (char **)NULL, 10);
1123                         if (blocks_per_segment == 0)
1124                                 usage();
1125                         break;
1126                 case 'L':
1127                         volumelabel = optarg;
1128                         break;
1129                 case 'm':
1130                         rsv_segment_percent = strtol(optarg, (char **)NULL, 10);
1131                         if (rsv_segment_percent == 0)
1132                                 usage();
1133                         break;
1134                 default:
1135                         usage();
1136                 }
1137         }
1138
1139         argc -= optind;
1140         argv += optind;
1141         if (argc < 1 || argc > 2)
1142                 usage();
1143
1144         /* construct proper device path */
1145         fname = *argv++;
1146         if (!strchr(fname, '/')) {
1147                 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
1148                 if (!(fname = strdup(buf)))
1149                         err(1, NULL);
1150         }
1151
1152         fd = g_open(fname, 1);
1153         if (fd == -1)
1154                 err(1, "Cannot open %s", fname);
1155
1156         if (fstat(fd, &sb) == -1)
1157                 err(1, "Cannot stat %s", fname);
1158         if (!S_ISCHR(sb.st_mode))
1159                 warnx("%s is not a character device", fname);
1160
1161         check_mounted(fname, sb.st_mode);
1162
1163         calculate_geometry(fd);
1164
1165         check_parameters();
1166
1167         print_parameters();
1168
1169         if (is_nand)
1170                 erase_device(fd);
1171         else
1172                 erase_initial(fd);
1173
1174         create_nandfs(fd);
1175
1176         print_summary();
1177
1178         g_close(fd);
1179
1180         return (0);
1181 }
1182
1183