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