2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010-2012 Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/fdcio.h>
35 #include <sys/disklabel.h>
36 #include <sys/mount.h>
39 #include <sys/endian.h>
40 #include <sys/stddef.h>
42 #include <sys/dirent.h>
58 #include <fs/nandfs/nandfs_fs.h>
59 #include <dev/nand/nand_dev.h>
64 #define debug(fmt, args...) do { \
65 printf("nandfs:" fmt "\n", ##args); } while (0)
67 #define debug(fmt, args...)
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
82 struct nandfs_inode *inode;
85 static struct file_info user_files[] = {
86 { NANDFS_ROOT_INO, NULL, S_IFDIR | 0755, 0, 1, NULL, NULL },
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 };
99 LIST_ENTRY(nandfs_block) block_link;
105 static LIST_HEAD(, nandfs_block) block_head =
106 LIST_HEAD_INITIALIZER(&block_head);
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;
115 static struct nandfs_fsdata fsdata;
116 static struct nandfs_super_block super_block;
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];
129 static u_char *volumelabel = NULL;
131 static struct nandfs_super_root *sr;
133 static uint32_t nuserfiles;
134 static uint32_t seg_nblocks;
135 static uint32_t seg_endblock;
137 #define SIZE_TO_BLOCK(size) howmany(size, blocksize)
140 nandfs_first_block(void)
142 uint32_t i, first_free, start_bad_segments = 0;
144 for (i = 0; i < bad_segments_count; i++) {
145 if (i == bad_segments[i])
146 start_bad_segments++;
151 first_free = SIZE_TO_BLOCK(NANDFS_DATA_OFFSET_BYTES(erasesize) +
152 (start_bad_segments * segsize));
154 if (first_free < (uint32_t)blocks_per_segment)
155 return (blocks_per_segment);
165 "usage: newfs_nandfs [ -options ] device\n"
166 "where the options are:\n"
168 "\t-B blocks-per-segment\n"
169 "\t-L volume label\n"
170 "\t-m reserved-segments-percentage\n");
175 nandfs_log2(unsigned n)
180 * N.B. this function will return 0 if supplied 0.
182 for (count = 0; n/2; count++)
187 /* from NetBSD's src/sys/net/if_ethersubr.c */
189 crc32_le(uint32_t crc, const uint8_t *buf, size_t len)
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
201 for (i = 0; i < len; i++) {
203 crc = (crc >> 4) ^ crctab[crc & 0xf];
204 crc = (crc >> 4) ^ crctab[crc & 0xf];
211 get_block(uint32_t block_nr, uint64_t offset)
213 struct nandfs_block *block, *new_block;
215 LIST_FOREACH(block, &block_head, block_link) {
216 if (block->number == block_nr)
220 debug("allocating block %x\n", block_nr);
222 new_block = malloc(sizeof(*block));
224 err(1, "cannot allocate block");
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");
232 memset(new_block->data, 0, blocksize);
234 LIST_INSERT_HEAD(&block_head, new_block, block_link);
236 return (new_block->data);
240 nandfs_seg_usage_blk_offset(uint64_t seg, uint64_t *blk, uint64_t *offset)
245 seg_size = sizeof(struct nandfs_segment_usage);
247 off = roundup(sizeof(struct nandfs_sufile_header), seg_size);
248 off += (seg * seg_size);
250 *blk = off / blocksize;
251 *offset = (off % blocksize) / seg_size;
260 size = sizeof(struct nandfs_segment_summary );
261 size += seg_nblocks * sizeof(struct nandfs_binfo_v);
263 if (size > blocksize)
264 err(1, "segsum info bigger that blocksize");
271 prepare_blockgrouped_file(uint32_t block)
273 struct nandfs_block_group_desc *desc;
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;
283 alloc_blockgrouped_file(uint32_t block, uint32_t entry)
285 struct nandfs_block_group_desc *desc;
289 desc = (struct nandfs_block_group_desc *)get_block(block, 0);
290 bitmap = (uint32_t *)get_block(block + 1, 1);
292 bitmap += (entry >> 5);
293 if (*bitmap & (1 << (entry % 32))) {
294 printf("nandfs: blockgrouped entry %d already allocated\n",
297 *bitmap |= (1 << (entry % 32));
299 desc_nr = entry / (blocksize * 8);
300 desc[desc_nr].bg_nfrees--;
305 count_su_blocks(void)
307 uint64_t maxblk, blk, offset, i;
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);
318 debug("bad segment needs %#jx", blk);
319 if (blk >= NANDFS_NDADDR) {
320 printf("nandfs: file too big (%jd > %d)\n", blk, NANDFS_NDADDR);
324 sufile.size = (blk + 1) * blocksize;
329 count_seg_blocks(void)
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));
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;
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;
352 SIZE_TO_BLOCK((NANDFS_SUFILE_FIRST_SEGMENT_USAGE_OFFSET + 1) *
353 sizeof(struct nandfs_segment_usage));
355 debug("bad blocks found: extra space for sufile");
356 sufile.nblocks = count_su_blocks();
359 sufile.blocks = malloc(sufile.nblocks * sizeof(uint32_t));
360 seg_nblocks += sufile.nblocks;
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;
369 assign_file_blocks(uint64_t start_block)
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++;
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++;
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++;
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++;
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++;
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;
410 prepare_blockgrouped_file(datfile.blocks[0]);
414 update_datfile(uint64_t block)
416 struct nandfs_dat_entry *dat;
417 static uint64_t vblock = 0;
418 uint64_t allocated, i, off;
421 alloc_blockgrouped_file(datfile.blocks[0], vblock);
425 i = vblock / (blocksize / sizeof(*dat));
426 off = vblock % (blocksize / sizeof(*dat));
429 dat = (struct nandfs_dat_entry *)get_block(datfile.blocks[2 + i], 2 + i);
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;
439 static union nandfs_binfo *
440 update_block_info(union nandfs_binfo *binfo, struct file_info *file)
442 nandfs_daddr_t vblock;
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;
454 binfo->bi_dat.bi_blkoff = i;
455 binfo->bi_dat.bi_ino = file->ino;
456 file->inode->i_db[i] = datfile.blocks[i];
465 save_segsum(struct nandfs_segment_summary *ss)
467 union nandfs_binfo *binfo;
468 struct nandfs_block *block;
469 uint32_t sum_bytes, i;
470 uint8_t crc_data, crc_skip;
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;
477 ss->ss_create = nandfs_time;
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;
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);
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]);
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);
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);
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)
511 if (block->number == NANDFS_FIRST_BLOCK)
512 crc_data = crc32_le(0,
513 (uint8_t *)block->data + crc_skip,
514 blocksize - crc_skip);
516 crc_data = crc32_le(crc_data, (uint8_t *)block->data,
519 ss->ss_datasum = crc_data;
527 memset(&fsdata, 0, sizeof(struct nandfs_fsdata));
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;
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);
550 memcpy(fsdata.f_volume_name, volumelabel, 16);
552 fsdata.f_sum = crc32_le(0, (const uint8_t *)&fsdata,
553 NANDFS_FSDATA_CRC_BYTES);
557 save_fsdata(void *data)
560 memcpy(data, &fsdata, sizeof(fsdata));
564 create_super_block(void)
567 memset(&super_block, 0, sizeof(struct nandfs_super_block));
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;
579 super_block.s_sum = crc32_le(0, (const uint8_t *)&super_block,
584 save_super_block(void *data)
587 memcpy(data, &super_block, sizeof(super_block));
591 save_super_root(void)
594 sr->sr_bytes = NANDFS_SR_BYTES;
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;
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)
609 de->rec_len = NANDFS_DIR_REC_LEN(de->name_len);
610 de = (void *)((uint8_t *)de + de->rec_len);
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");
619 de->rec_len = reclen;
620 de->name_len = strlen(name);
621 de->file_type = type;
623 (strlen(name) + NANDFS_DIR_PAD - 1) & ~NANDFS_DIR_ROUND);
624 memcpy(de->name, name, strlen(name));
629 static struct nandfs_dir_entry *
630 make_dir(void *block, uint64_t ino, uint64_t parent_ino)
632 struct nandfs_dir_entry *de = (struct nandfs_dir_entry *)block;
634 /* create '..' entry */
635 de->inode = parent_ino;
636 de->rec_len = NANDFS_DIR_REC_LEN(2);
638 de->file_type = DT_DIR;
639 memset(de->name, 0, NANDFS_DIR_NAME_LEN(2));
640 memcpy(de->name, "..", 2);
642 /* create '.' entry */
643 de = (void *)((uint8_t *)block + NANDFS_DIR_REC_LEN(2));
645 de->rec_len = blocksize - NANDFS_DIR_REC_LEN(2);
647 de->file_type = DT_DIR;
648 memset(de->name, 0, NANDFS_DIR_NAME_LEN(1));
649 memcpy(de->name, ".", 1);
657 struct file_info *root = &user_files[0];
658 struct nandfs_dir_entry *de;
662 block = get_block(root->blocks[0], 0);
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));
669 root->size = ((uintptr_t)de - (uintptr_t)block) +
670 NANDFS_DIR_REC_LEN(de->name_len);
676 struct nandfs_sufile_header *header;
677 struct nandfs_segment_usage *su;
678 uint64_t blk, i, off;
683 * At the beginning just zero-out everything
685 for (i = 0; i < sufile.nblocks; i++)
686 get_block(sufile.blocks[i], 0);
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;
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;
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;
724 struct nandfs_cpfile_header *header;
725 struct nandfs_checkpoint *cp, *initial_cp;
726 int i, entries = blocksize / sizeof(struct nandfs_checkpoint);
729 header = (struct nandfs_cpfile_header *)get_block(cpfile.blocks[0], 0);
730 header->ch_ncheckpoints = 1;
731 header->ch_nsnapshots = 0;
733 cp = (struct nandfs_checkpoint *)header;
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));
746 ifile.inode = &initial_cp->cp_ifile_inode;
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;
758 init_inode(struct nandfs_inode *inode, struct file_info *file)
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;
768 inode->i_size = file->size;
772 if (file->ino == NANDFS_USER_INO)
773 inode->i_flags = SF_NOUNLINK|UF_NOUNLINK;
781 struct nandfs_inode *inode;
782 struct file_info *file;
783 uint64_t ino, blk, off;
786 prepare_blockgrouped_file(ifile.blocks[0]);
787 for (i = 0; i <= NANDFS_USER_INO; i++)
788 alloc_blockgrouped_file(ifile.blocks[0], i);
790 for (i = 0; i < nuserfiles; i++) {
791 file = &user_files[i];
793 blk = ino / (blocksize / sizeof(*inode));
794 off = ino % (blocksize / sizeof(*inode));
796 (struct nandfs_inode *)get_block(ifile.blocks[2 + blk], 2 + blk);
797 file->inode = &inode[off];
798 init_inode(file->inode, file);
801 init_inode(ifile.inode, &ifile);
802 init_inode(cpfile.inode, &cpfile);
803 init_inode(sufile.inode, &sufile);
804 init_inode(datfile.inode, &datfile);
810 uint64_t start_block;
811 uint32_t segsum_size;
815 nuserfiles = nitems(user_files);
817 /* Count and assign blocks */
819 segsum_size = segment_size();
820 start_block = NANDFS_FIRST_BLOCK + SIZE_TO_BLOCK(segsum_size);
821 assign_file_blocks(start_block);
823 /* Create super root structure */
826 /* Create root directory */
829 /* Fill in file contents */
835 /* Save fsdata and superblocks */
837 create_super_block();
839 for (i = 0; i < NANDFS_NFSAREAS; i++) {
840 if (fsdata_blocks_state[i] != NANDFS_BLOCK_GOOD)
843 data = get_block((i * erasesize)/blocksize, 0);
846 data = get_block((i * erasesize + NANDFS_SBLOCK_OFFSET_BYTES) /
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));
856 /* Save segment summary and CRCs */
857 save_segsum(get_block(NANDFS_FIRST_BLOCK, 0));
865 struct nandfs_block *block;
869 /* Overwrite next block with ff if not nand device */
871 data = get_block(seg_endblock, 0);
872 memset(data, 0xff, blocksize);
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");
884 check_parameters(void)
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,
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);
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.");
908 /* check volume label */
911 while (isalnum(volumelabel[++i]))
914 if (volumelabel[i] != '\0') {
915 errx(1, "bad volume label. "
916 "Valid characters are alphanumerics.");
919 if (strlen(volumelabel) >= 16)
920 errx(1, "Bad volume label. Length is longer than %d.",
924 nandfs_time = time(NULL);
928 print_parameters(void)
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);
939 * Exit with error if file system is mounted.
942 check_mounted(const char *fname, mode_t mode)
949 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
950 err(1, "getmntinfo");
952 len = strlen(_PATH_DEV);
954 if (!strncmp(s1, _PATH_DEV, len))
957 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
960 s2 = mp->f_mntfromname;
962 if (!strncmp(s2, _PATH_DEV, len))
964 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
966 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
971 calculate_geometry(int fd)
973 struct chip_param_io chip_params;
974 char ident[DISK_IDENT_SIZE];
975 char medianame[MAXPATHLEN];
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);
982 is_nand = (strstr(medianame, "gnand") != NULL);
983 debug("is_nand = %d", is_nand);
985 sectorsize = g_sectorsize(fd);
986 debug("sectorsize: %#zx", sectorsize);
988 /* Get storage size */
989 mediasize = g_mediasize(fd);
990 debug("mediasize: %#jx", mediasize);
992 /* Get storage erase unit size */
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;
998 errx(1, "Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
1000 debug("erasesize: %#jx", (uintmax_t)erasesize);
1002 if (blocks_per_segment == 0) {
1003 if (erasesize >= NANDFS_MIN_SEGSIZE)
1004 blocks_per_segment = erasesize / blocksize;
1006 blocks_per_segment = NANDFS_MIN_SEGSIZE / blocksize;
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);
1017 erase_device(int fd)
1020 uint64_t i, nblocks;
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;
1031 fsdata_blocks_state[i] = NANDFS_BLOCK_GOOD;
1034 if (failed == NANDFS_NFSAREAS) {
1035 printf("%d first blocks not usable. Unable to create "
1036 "filesystem.\n", failed);
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",
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;
1052 if (bad_segments_count == nsegments) {
1053 printf("no valid segments\n");
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");
1070 erase_initial(int fd)
1075 memset(buf, 0xff, sizeof(buf));
1077 lseek(fd, 0, SEEK_SET);
1078 for (i = 0; i < NANDFS_NFSAREAS * erasesize; i += sizeof(buf))
1079 write(fd, buf, sizeof(buf));
1083 create_nandfs(int fd)
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",
1100 blocks_per_segment * blocksize) / (1024 * 1024),
1101 ((nsegments - bad_segments_count) *
1102 blocks_per_segment * blocksize) / (1024 * 1024));
1106 main(int argc, char *argv[])
1109 char buf[MAXPATHLEN];
1110 const char opts[] = "b:B:L:m:";
1114 while ((ch = getopt(argc, argv, opts)) != -1) {
1117 blocksize = strtol(optarg, (char **)NULL, 10);
1122 blocks_per_segment = strtol(optarg, (char **)NULL, 10);
1123 if (blocks_per_segment == 0)
1127 volumelabel = optarg;
1130 rsv_segment_percent = strtol(optarg, (char **)NULL, 10);
1131 if (rsv_segment_percent == 0)
1141 if (argc < 1 || argc > 2)
1144 /* construct proper device path */
1146 if (!strchr(fname, '/')) {
1147 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
1148 if (!(fname = strdup(buf)))
1152 fd = g_open(fname, 1);
1154 err(1, "Cannot open %s", fname);
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);
1161 check_mounted(fname, sb.st_mode);
1163 calculate_geometry(fd);