2 * Copyright (c) 2010-2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/fdcio.h>
33 #include <sys/disklabel.h>
34 #include <sys/mount.h>
37 #include <sys/endian.h>
38 #include <sys/stddef.h>
40 #include <sys/dirent.h>
56 #include <fs/nandfs/nandfs_fs.h>
57 #include <dev/nand/nand_dev.h>
62 #define debug(fmt, args...) do { \
63 printf("nandfs:" fmt "\n", ##args); } while (0)
65 #define debug(fmt, args...)
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
80 struct nandfs_inode *inode;
83 static struct file_info user_files[] = {
84 { NANDFS_ROOT_INO, NULL, S_IFDIR | 0755, 0, 1, NULL, NULL },
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 };
97 LIST_ENTRY(nandfs_block) block_link;
103 static LIST_HEAD(, nandfs_block) block_head =
104 LIST_HEAD_INITIALIZER(&block_head);
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;
113 static struct nandfs_fsdata fsdata;
114 static struct nandfs_super_block super_block;
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];
127 static u_char *volumelabel = NULL;
129 static struct nandfs_super_root *sr;
131 static uint32_t nuserfiles;
132 static uint32_t seg_nblocks;
133 static uint32_t seg_endblock;
135 #define SIZE_TO_BLOCK(size) (((size) + (blocksize - 1)) / blocksize)
138 nandfs_first_block(void)
140 uint32_t i, first_free, start_bad_segments = 0;
142 for (i = 0; i < bad_segments_count; i++) {
143 if (i == bad_segments[i])
144 start_bad_segments++;
149 first_free = SIZE_TO_BLOCK(NANDFS_DATA_OFFSET_BYTES(erasesize) +
150 (start_bad_segments * segsize));
152 if (first_free < (uint32_t)blocks_per_segment)
153 return (blocks_per_segment);
163 "usage: newfs_nandfs [ -options ] device\n"
164 "where the options are:\n"
166 "\t-B blocks-per-segment\n"
167 "\t-L volume label\n"
168 "\t-m reserved-segments-percentage\n");
173 nandfs_log2(unsigned n)
178 * N.B. this function will return 0 if supplied 0.
180 for (count = 0; n/2; count++)
185 /* from NetBSD's src/sys/net/if_ethersubr.c */
187 crc32_le(uint32_t crc, const uint8_t *buf, size_t len)
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
199 for (i = 0; i < len; i++) {
201 crc = (crc >> 4) ^ crctab[crc & 0xf];
202 crc = (crc >> 4) ^ crctab[crc & 0xf];
209 get_block(uint32_t block_nr, uint64_t offset)
211 struct nandfs_block *block, *new_block;
213 LIST_FOREACH(block, &block_head, block_link) {
214 if (block->number == block_nr)
218 debug("allocating block %x\n", block_nr);
220 new_block = malloc(sizeof(*block));
222 err(1, "cannot allocate block");
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");
230 memset(new_block->data, 0, blocksize);
232 LIST_INSERT_HEAD(&block_head, new_block, block_link);
234 return (new_block->data);
238 nandfs_seg_usage_blk_offset(uint64_t seg, uint64_t *blk, uint64_t *offset)
243 seg_size = sizeof(struct nandfs_segment_usage);
245 off = roundup(sizeof(struct nandfs_sufile_header), seg_size);
246 off += (seg * seg_size);
248 *blk = off / blocksize;
249 *offset = (off % blocksize) / seg_size;
258 size = sizeof(struct nandfs_segment_summary );
259 size += seg_nblocks * sizeof(struct nandfs_binfo_v);
261 if (size > blocksize)
262 err(1, "segsum info bigger that blocksize");
269 prepare_blockgrouped_file(uint32_t block)
271 struct nandfs_block_group_desc *desc;
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;
281 alloc_blockgrouped_file(uint32_t block, uint32_t entry)
283 struct nandfs_block_group_desc *desc;
287 desc = (struct nandfs_block_group_desc *)get_block(block, 0);
288 bitmap = (uint32_t *)get_block(block + 1, 1);
290 bitmap += (entry >> 5);
291 if (*bitmap & (1 << (entry % 32))) {
292 printf("nandfs: blockgrouped entry %d already allocated\n",
295 *bitmap |= (1 << (entry % 32));
297 desc_nr = entry / (blocksize * 8);
298 desc[desc_nr].bg_nfrees--;
303 count_su_blocks(void)
305 uint64_t maxblk, blk, offset, i;
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);
316 debug("bad segment needs %#jx", blk);
318 printf("nandfs: file too big (%jd > %d)\n", blk, NDADDR);
322 sufile.size = (blk + 1) * blocksize;
327 count_seg_blocks(void)
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));
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;
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;
350 SIZE_TO_BLOCK((NANDFS_SUFILE_FIRST_SEGMENT_USAGE_OFFSET + 1) *
351 sizeof(struct nandfs_segment_usage));
353 debug("bad blocks found: extra space for sufile");
354 sufile.nblocks = count_su_blocks();
357 sufile.blocks = malloc(sufile.nblocks * sizeof(uint32_t));
358 seg_nblocks += sufile.nblocks;
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;
367 assign_file_blocks(uint64_t start_block)
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++;
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++;
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++;
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++;
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++;
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;
408 prepare_blockgrouped_file(datfile.blocks[0]);
412 update_datfile(uint64_t block)
414 struct nandfs_dat_entry *dat;
415 static uint64_t vblock = 0;
416 uint64_t allocated, i, off;
419 alloc_blockgrouped_file(datfile.blocks[0], vblock);
423 i = vblock / (blocksize / sizeof(*dat));
424 off = vblock % (blocksize / sizeof(*dat));
427 dat = (struct nandfs_dat_entry *)get_block(datfile.blocks[2 + i], 2 + i);
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;
437 static union nandfs_binfo *
438 update_block_info(union nandfs_binfo *binfo, struct file_info *file)
440 nandfs_daddr_t vblock;
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;
452 binfo->bi_dat.bi_blkoff = i;
453 binfo->bi_dat.bi_ino = file->ino;
454 file->inode->i_db[i] = datfile.blocks[i];
463 save_segsum(struct nandfs_segment_summary *ss)
465 union nandfs_binfo *binfo;
466 struct nandfs_block *block;
467 uint32_t sum_bytes, i;
468 uint8_t crc_data, crc_skip;
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;
475 ss->ss_create = nandfs_time;
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;
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);
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]);
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);
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);
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)
509 if (block->number == NANDFS_FIRST_BLOCK)
510 crc_data = crc32_le(0,
511 (uint8_t *)block->data + crc_skip,
512 blocksize - crc_skip);
514 crc_data = crc32_le(crc_data, (uint8_t *)block->data,
517 ss->ss_datasum = crc_data;
525 memset(&fsdata, 0, sizeof(struct nandfs_fsdata));
527 fsdata.f_magic = NANDFS_FSDATA_MAGIC;
528 fsdata.f_nsegments = nsegments;
529 fsdata.f_erasesize = erasesize;
530 fsdata.f_first_data_block = NANDFS_FIRST_BLOCK;
531 fsdata.f_blocks_per_segment = blocks_per_segment;
532 fsdata.f_r_segments_percentage = rsv_segment_percent;
533 fsdata.f_rev_level = NANDFS_CURRENT_REV;
534 fsdata.f_sbbytes = NANDFS_SB_BYTES;
535 fsdata.f_bytes = NANDFS_FSDATA_CRC_BYTES;
536 fsdata.f_ctime = nandfs_time;
537 fsdata.f_log_block_size = nandfs_log2(blocksize) - 10;
539 fsdata.f_inode_size = sizeof(struct nandfs_inode);
540 fsdata.f_dat_entry_size = sizeof(struct nandfs_dat_entry);
541 fsdata.f_checkpoint_size = sizeof(struct nandfs_checkpoint);
542 fsdata.f_segment_usage_size = sizeof(struct nandfs_segment_usage);
548 memcpy(fsdata.f_volume_name, volumelabel, 16);
550 fsdata.f_sum = crc32_le(0, (const uint8_t *)&fsdata,
551 NANDFS_FSDATA_CRC_BYTES);
555 save_fsdata(void *data)
558 memcpy(data, &fsdata, sizeof(fsdata));
562 create_super_block(void)
565 memset(&super_block, 0, sizeof(struct nandfs_super_block));
567 super_block.s_magic = NANDFS_SUPER_MAGIC;
568 super_block.s_last_cno = NANDFS_FIRST_CNO;
569 super_block.s_last_pseg = NANDFS_FIRST_BLOCK;
570 super_block.s_last_seq = 1;
571 super_block.s_free_blocks_count =
572 (nsegments - bad_segments_count) * blocks_per_segment;
573 super_block.s_mtime = 0;
574 super_block.s_wtime = nandfs_time;
575 super_block.s_state = NANDFS_VALID_FS;
577 super_block.s_sum = crc32_le(0, (const uint8_t *)&super_block,
582 save_super_block(void *data)
585 memcpy(data, &super_block, sizeof(super_block));
589 save_super_root(void)
592 sr->sr_bytes = NANDFS_SR_BYTES;
594 sr->sr_nongc_ctime = nandfs_time;
595 datfile.inode = &sr->sr_dat;
596 cpfile.inode = &sr->sr_cpfile;
597 sufile.inode = &sr->sr_sufile;
600 static struct nandfs_dir_entry *
601 add_de(void *block, struct nandfs_dir_entry *de, uint64_t ino,
602 const char *name, uint8_t type)
607 de->rec_len = NANDFS_DIR_REC_LEN(de->name_len);
608 de = (void *)((uint8_t *)de + de->rec_len);
610 reclen = blocksize - ((uintptr_t)de - (uintptr_t)block);
611 if (reclen < NANDFS_DIR_REC_LEN(strlen(name))) {
612 printf("nandfs: too many dir entries for one block\n");
617 de->rec_len = reclen;
618 de->name_len = strlen(name);
619 de->file_type = type;
621 (strlen(name) + NANDFS_DIR_PAD - 1) & ~NANDFS_DIR_ROUND);
622 memcpy(de->name, name, strlen(name));
627 static struct nandfs_dir_entry *
628 make_dir(void *block, uint64_t ino, uint64_t parent_ino)
630 struct nandfs_dir_entry *de = (struct nandfs_dir_entry *)block;
632 /* create '..' entry */
633 de->inode = parent_ino;
634 de->rec_len = NANDFS_DIR_REC_LEN(2);
636 de->file_type = DT_DIR;
637 memset(de->name, 0, NANDFS_DIR_NAME_LEN(2));
638 memcpy(de->name, "..", 2);
640 /* create '.' entry */
641 de = (void *)((uint8_t *)block + NANDFS_DIR_REC_LEN(2));
643 de->rec_len = blocksize - NANDFS_DIR_REC_LEN(2);
645 de->file_type = DT_DIR;
646 memset(de->name, 0, NANDFS_DIR_NAME_LEN(1));
647 memcpy(de->name, ".", 1);
655 struct file_info *root = &user_files[0];
656 struct nandfs_dir_entry *de;
660 block = get_block(root->blocks[0], 0);
662 de = make_dir(block, root->ino, root->ino);
663 for (i = 1; i < nuserfiles; i++)
664 de = add_de(block, de, user_files[i].ino, user_files[i].name,
665 IFTODT(user_files[i].mode));
667 root->size = ((uintptr_t)de - (uintptr_t)block) +
668 NANDFS_DIR_REC_LEN(de->name_len);
674 struct nandfs_sufile_header *header;
675 struct nandfs_segment_usage *su;
676 uint64_t blk, i, off;
681 * At the beginning just zero-out everything
683 for (i = 0; i < sufile.nblocks; i++)
684 get_block(sufile.blocks[i], 0);
688 block = get_block(sufile.blocks[start], 0);
689 header = (struct nandfs_sufile_header *)block;
690 header->sh_ncleansegs = nsegments - bad_segments_count - 1;
691 header->sh_ndirtysegs = 1;
692 header->sh_last_alloc = 1;
694 su = (struct nandfs_segment_usage *)header;
695 off = NANDFS_SUFILE_FIRST_SEGMENT_USAGE_OFFSET;
696 /* Allocate data segment */
697 su[off].su_lastmod = nandfs_time;
698 /* nblocks = segment blocks + segsum block + superroot */
699 su[off].su_nblocks = seg_nblocks + 2;
700 su[off].su_flags = NANDFS_SEGMENT_USAGE_DIRTY;
702 /* Allocate next segment */
703 su[off].su_lastmod = nandfs_time;
704 su[off].su_nblocks = 0;
705 su[off].su_flags = NANDFS_SEGMENT_USAGE_DIRTY;
706 for (i = 0; i < bad_segments_count; i++) {
707 nandfs_seg_usage_blk_offset(bad_segments[i], &blk, &off);
708 debug("storing bad_segments[%jd]=%x at %jx off %jx\n", i,
709 bad_segments[i], blk, off);
710 block = get_block(sufile.blocks[blk],
711 off * sizeof(struct nandfs_segment_usage *));
712 su = (struct nandfs_segment_usage *)block;
713 su[off].su_lastmod = nandfs_time;
714 su[off].su_nblocks = 0;
715 su[off].su_flags = NANDFS_SEGMENT_USAGE_ERROR;
722 struct nandfs_cpfile_header *header;
723 struct nandfs_checkpoint *cp, *initial_cp;
724 int i, entries = blocksize / sizeof(struct nandfs_checkpoint);
727 header = (struct nandfs_cpfile_header *)get_block(cpfile.blocks[0], 0);
728 header->ch_ncheckpoints = 1;
729 header->ch_nsnapshots = 0;
731 cp = (struct nandfs_checkpoint *)header;
733 /* fill first checkpoint data*/
734 initial_cp = &cp[NANDFS_CPFILE_FIRST_CHECKPOINT_OFFSET];
735 initial_cp->cp_flags = 0;
736 initial_cp->cp_checkpoints_count = 0;
737 initial_cp->cp_cno = NANDFS_FIRST_CNO;
738 initial_cp->cp_create = nandfs_time;
739 initial_cp->cp_nblk_inc = seg_endblock - 1;
740 initial_cp->cp_blocks_count = seg_nblocks;
741 memset(&initial_cp->cp_snapshot_list, 0,
742 sizeof(struct nandfs_snapshot_list));
744 ifile.inode = &initial_cp->cp_ifile_inode;
746 /* mark rest of cp as invalid */
747 cno = NANDFS_FIRST_CNO + 1;
748 i = NANDFS_CPFILE_FIRST_CHECKPOINT_OFFSET + 1;
749 for (; i < entries; i++) {
750 cp[i].cp_cno = cno++;
751 cp[i].cp_flags = NANDFS_CHECKPOINT_INVALID;
756 init_inode(struct nandfs_inode *inode, struct file_info *file)
759 inode->i_blocks = file->nblocks;
760 inode->i_ctime = nandfs_time;
761 inode->i_mtime = nandfs_time;
762 inode->i_mode = file->mode & 0xffff;
763 inode->i_links_count = 1;
766 inode->i_size = file->size;
770 if (file->ino == NANDFS_USER_INO)
771 inode->i_flags = SF_NOUNLINK|UF_NOUNLINK;
779 struct nandfs_inode *inode;
780 struct file_info *file;
781 uint64_t ino, blk, off;
784 prepare_blockgrouped_file(ifile.blocks[0]);
785 for (i = 0; i <= NANDFS_USER_INO; i++)
786 alloc_blockgrouped_file(ifile.blocks[0], i);
788 for (i = 0; i < nuserfiles; i++) {
789 file = &user_files[i];
791 blk = ino / (blocksize / sizeof(*inode));
792 off = ino % (blocksize / sizeof(*inode));
794 (struct nandfs_inode *)get_block(ifile.blocks[2 + blk], 2 + blk);
795 file->inode = &inode[off];
796 init_inode(file->inode, file);
799 init_inode(ifile.inode, &ifile);
800 init_inode(cpfile.inode, &cpfile);
801 init_inode(sufile.inode, &sufile);
802 init_inode(datfile.inode, &datfile);
808 uint64_t start_block;
809 uint32_t segsum_size;
813 nuserfiles = (sizeof(user_files) / sizeof(user_files[0]));
815 /* Count and assign blocks */
817 segsum_size = segment_size();
818 start_block = NANDFS_FIRST_BLOCK + SIZE_TO_BLOCK(segsum_size);
819 assign_file_blocks(start_block);
821 /* Create super root structure */
824 /* Create root directory */
827 /* Fill in file contents */
833 /* Save fsdata and superblocks */
835 create_super_block();
837 for (i = 0; i < NANDFS_NFSAREAS; i++) {
838 if (fsdata_blocks_state[i] != NANDFS_BLOCK_GOOD)
841 data = get_block((i * erasesize)/blocksize, 0);
844 data = get_block((i * erasesize + NANDFS_SBLOCK_OFFSET_BYTES) /
846 if (blocksize > NANDFS_SBLOCK_OFFSET_BYTES)
847 data += NANDFS_SBLOCK_OFFSET_BYTES;
848 save_super_block(data);
849 memset(data + sizeof(struct nandfs_super_block), 0xff,
850 (blocksize - sizeof(struct nandfs_super_block) -
851 NANDFS_SBLOCK_OFFSET_BYTES));
854 /* Save segment summary and CRCs */
855 save_segsum(get_block(NANDFS_FIRST_BLOCK, 0));
863 struct nandfs_block *block;
867 /* Overwrite next block with ff if not nand device */
869 data = get_block(seg_endblock, 0);
870 memset(data, 0xff, blocksize);
873 LIST_FOREACH(block, &block_head, block_link) {
874 lseek(fda, block->number * blocksize, SEEK_SET);
875 ret = write(fda, block->data, blocksize);
876 if (ret != blocksize)
877 err(1, "cannot write filesystem data");
882 check_parameters(void)
886 /* check blocksize */
887 if ((blocksize < NANDFS_MIN_BLOCKSIZE) || (blocksize > MAXBSIZE) ||
888 ((blocksize - 1) & blocksize)) {
889 errx(1, "Bad blocksize (%zu). Must be in range [%u-%u] "
890 "and a power of two.", blocksize, NANDFS_MIN_BLOCKSIZE,
894 /* check blocks per segments */
895 if ((blocks_per_segment < NANDFS_SEG_MIN_BLOCKS) ||
896 ((blocksize - 1) & blocksize))
897 errx(1, "Bad blocks per segment (%lu). Must be greater than "
898 "%u and a power of two.", blocks_per_segment,
899 NANDFS_SEG_MIN_BLOCKS);
901 /* check reserved segment percentage */
902 if ((rsv_segment_percent < 1) || (rsv_segment_percent > 99))
903 errx(1, "Bad reserved segment percentage. "
904 "Must in range 1..99.");
906 /* check volume label */
909 while (isalnum(volumelabel[++i]))
912 if (volumelabel[i] != '\0') {
913 errx(1, "bad volume label. "
914 "Valid characters are alphanumerics.");
917 if (strlen(volumelabel) >= 16)
918 errx(1, "Bad volume label. Length is longer than %d.",
922 nandfs_time = time(NULL);
926 print_parameters(void)
929 printf("filesystem parameters:\n");
930 printf("blocksize: %#zx sectorsize: %#zx\n", blocksize, sectorsize);
931 printf("erasesize: %#jx mediasize: %#jx\n", erasesize, mediasize);
932 printf("segment size: %#jx blocks per segment: %#x\n", segsize,
933 (uint32_t)blocks_per_segment);
937 * Exit with error if file system is mounted.
940 check_mounted(const char *fname, mode_t mode)
947 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
948 err(1, "getmntinfo");
950 len = strlen(_PATH_DEV);
952 if (!strncmp(s1, _PATH_DEV, len))
955 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
958 s2 = mp->f_mntfromname;
960 if (!strncmp(s2, _PATH_DEV, len))
962 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
964 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
969 calculate_geometry(int fd)
971 struct chip_param_io chip_params;
972 char ident[DISK_IDENT_SIZE];
973 char medianame[MAXPATHLEN];
975 /* Check storage type */
976 g_get_ident(fd, ident, DISK_IDENT_SIZE);
977 g_get_name(ident, medianame, MAXPATHLEN);
978 debug("device name: %s", medianame);
980 is_nand = (strstr(medianame, "gnand") != NULL);
981 debug("is_nand = %d", is_nand);
983 sectorsize = g_sectorsize(fd);
984 debug("sectorsize: %#zx", sectorsize);
986 /* Get storage size */
987 mediasize = g_mediasize(fd);
988 debug("mediasize: %#jx", mediasize);
990 /* Get storage erase unit size */
992 erasesize = NANDFS_DEF_ERASESIZE;
993 else if (ioctl(fd, NAND_IO_GET_CHIP_PARAM, &chip_params) != -1)
994 erasesize = chip_params.page_size * chip_params.pages_per_block;
996 errx(1, "Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
998 debug("erasesize: %#jx", (uintmax_t)erasesize);
1000 if (blocks_per_segment == 0) {
1001 if (erasesize >= NANDFS_MIN_SEGSIZE)
1002 blocks_per_segment = erasesize / blocksize;
1004 blocks_per_segment = NANDFS_MIN_SEGSIZE / blocksize;
1007 /* Calculate number of segments */
1008 segsize = blocksize * blocks_per_segment;
1009 nsegments = ((mediasize - NANDFS_NFSAREAS * erasesize) / segsize) - 2;
1010 debug("segsize: %#jx", segsize);
1011 debug("nsegments: %#jx", nsegments);
1015 erase_device(int fd)
1018 uint64_t i, nblocks;
1022 for (i = 0; i < NANDFS_NFSAREAS; i++) {
1023 debug("Deleting %jx\n", i * erasesize);
1024 if (g_delete(fd, i * erasesize, erasesize)) {
1025 printf("cannot delete %jx\n", i * erasesize);
1026 fsdata_blocks_state[i] = NANDFS_BLOCK_BAD;
1029 fsdata_blocks_state[i] = NANDFS_BLOCK_GOOD;
1032 if (failed == NANDFS_NFSAREAS) {
1033 printf("%d first blocks not usable. Unable to create "
1034 "filesystem.\n", failed);
1038 for (i = 0; i < nsegments; i++) {
1039 offset = NANDFS_NFSAREAS * erasesize + i * segsize;
1040 if (g_delete(fd, offset, segsize)) {
1041 printf("cannot delete segment %jx (offset %jd)\n",
1043 bad_segments_count++;
1044 bad_segments = realloc(bad_segments,
1045 bad_segments_count * sizeof(uint32_t));
1046 bad_segments[bad_segments_count - 1] = i;
1050 if (bad_segments_count == nsegments) {
1051 printf("no valid segments\n");
1055 /* Delete remaining blocks at the end of device */
1056 rest = mediasize % segsize;
1057 nblocks = rest / erasesize;
1058 for (i = 0; i < nblocks; i++) {
1059 offset = (segsize * nsegments) + (i * erasesize);
1060 if (g_delete(fd, offset, erasesize)) {
1061 printf("cannot delete space after last segment "
1062 "- probably a bad block\n");
1068 erase_initial(int fd)
1073 memset(buf, 0xff, sizeof(buf));
1075 lseek(fd, 0, SEEK_SET);
1076 for (i = 0; i < NANDFS_NFSAREAS * erasesize; i += sizeof(buf))
1077 write(fd, buf, sizeof(buf));
1081 create_nandfs(int fd)
1093 printf("filesystem was created successfully\n");
1094 printf("total segments: %#jx valid segments: %#jx\n", nsegments,
1095 nsegments - bad_segments_count);
1096 printf("total space: %ju MB free: %ju MB\n",
1098 blocks_per_segment * blocksize) / (1024 * 1024),
1099 ((nsegments - bad_segments_count) *
1100 blocks_per_segment * blocksize) / (1024 * 1024));
1104 main(int argc, char *argv[])
1107 char buf[MAXPATHLEN];
1108 const char opts[] = "b:B:L:m:";
1112 while ((ch = getopt(argc, argv, opts)) != -1) {
1115 blocksize = strtol(optarg, (char **)NULL, 10);
1120 blocks_per_segment = strtol(optarg, (char **)NULL, 10);
1121 if (blocks_per_segment == 0)
1125 volumelabel = optarg;
1128 rsv_segment_percent = strtol(optarg, (char **)NULL, 10);
1129 if (rsv_segment_percent == 0)
1139 if (argc < 1 || argc > 2)
1142 /* construct proper device path */
1144 if (!strchr(fname, '/')) {
1145 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
1146 if (!(fname = strdup(buf)))
1150 fd = g_open(fname, 1);
1152 err(1, "Cannot open %s", fname);
1154 if (fstat(fd, &sb) == -1)
1155 err(1, "Cannot stat %s", fname);
1156 if (!S_ISCHR(sb.st_mode))
1157 warnx("%s is not a character device", fname);
1159 check_mounted(fname, sb.st_mode);
1161 calculate_geometry(fd);