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;
524 memset(&fsdata, 0, sizeof(struct nandfs_fsdata));
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;
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);
543 uuidgen(&fsdata.f_uuid, 1);
546 memcpy(fsdata.f_volume_name, volumelabel, 16);
548 fsdata.f_sum = crc32_le(0, (const uint8_t *)&fsdata,
549 NANDFS_FSDATA_CRC_BYTES);
553 save_fsdata(void *data)
556 memcpy(data, &fsdata, sizeof(fsdata));
560 create_super_block(void)
563 memset(&super_block, 0, sizeof(struct nandfs_super_block));
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;
575 super_block.s_sum = crc32_le(0, (const uint8_t *)&super_block,
580 save_super_block(void *data)
583 memcpy(data, &super_block, sizeof(super_block));
587 save_super_root(void)
590 sr->sr_bytes = NANDFS_SR_BYTES;
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;
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)
605 de->rec_len = NANDFS_DIR_REC_LEN(de->name_len);
606 de = (void *)((uint8_t *)de + de->rec_len);
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");
615 de->rec_len = reclen;
616 de->name_len = strlen(name);
617 de->file_type = type;
619 (strlen(name) + NANDFS_DIR_PAD - 1) & ~NANDFS_DIR_ROUND);
620 memcpy(de->name, name, strlen(name));
625 static struct nandfs_dir_entry *
626 make_dir(void *block, uint64_t ino, uint64_t parent_ino)
628 struct nandfs_dir_entry *de = (struct nandfs_dir_entry *)block;
630 /* create '..' entry */
631 de->inode = parent_ino;
632 de->rec_len = NANDFS_DIR_REC_LEN(2);
634 de->file_type = DT_DIR;
635 memset(de->name, 0, NANDFS_DIR_NAME_LEN(2));
636 memcpy(de->name, "..", 2);
638 /* create '.' entry */
639 de = (void *)((uint8_t *)block + NANDFS_DIR_REC_LEN(2));
641 de->rec_len = blocksize - NANDFS_DIR_REC_LEN(2);
643 de->file_type = DT_DIR;
644 memset(de->name, 0, NANDFS_DIR_NAME_LEN(1));
645 memcpy(de->name, ".", 1);
653 struct file_info *root = &user_files[0];
654 struct nandfs_dir_entry *de;
658 block = get_block(root->blocks[0], 0);
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));
665 root->size = ((uintptr_t)de - (uintptr_t)block) +
666 NANDFS_DIR_REC_LEN(de->name_len);
672 struct nandfs_sufile_header *header;
673 struct nandfs_segment_usage *su;
674 uint64_t blk, i, off;
679 * At the beginning just zero-out everything
681 for (i = 0; i < sufile.nblocks; i++)
682 get_block(sufile.blocks[i], 0);
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;
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;
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;
720 struct nandfs_cpfile_header *header;
721 struct nandfs_checkpoint *cp, *initial_cp;
722 int i, entries = blocksize / sizeof(struct nandfs_checkpoint);
725 header = (struct nandfs_cpfile_header *)get_block(cpfile.blocks[0], 0);
726 header->ch_ncheckpoints = 1;
727 header->ch_nsnapshots = 0;
729 cp = (struct nandfs_checkpoint *)header;
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));
742 ifile.inode = &initial_cp->cp_ifile_inode;
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;
754 init_inode(struct nandfs_inode *inode, struct file_info *file)
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;
764 inode->i_size = file->size;
768 if (file->ino == NANDFS_USER_INO)
769 inode->i_flags = SF_NOUNLINK|UF_NOUNLINK;
777 struct nandfs_inode *inode;
778 struct file_info *file;
779 uint64_t ino, blk, off;
782 prepare_blockgrouped_file(ifile.blocks[0]);
783 for (i = 0; i <= NANDFS_USER_INO; i++)
784 alloc_blockgrouped_file(ifile.blocks[0], i);
786 for (i = 0; i < nuserfiles; i++) {
787 file = &user_files[i];
789 blk = ino / (blocksize / sizeof(*inode));
790 off = ino % (blocksize / sizeof(*inode));
792 (struct nandfs_inode *)get_block(ifile.blocks[2 + blk], 2 + blk);
793 file->inode = &inode[off];
794 init_inode(file->inode, file);
797 init_inode(ifile.inode, &ifile);
798 init_inode(cpfile.inode, &cpfile);
799 init_inode(sufile.inode, &sufile);
800 init_inode(datfile.inode, &datfile);
806 uint64_t start_block;
807 uint32_t segsum_size;
811 nuserfiles = (sizeof(user_files) / sizeof(user_files[0]));
813 /* Count and assign blocks */
815 segsum_size = segment_size();
816 start_block = NANDFS_FIRST_BLOCK + SIZE_TO_BLOCK(segsum_size);
817 assign_file_blocks(start_block);
819 /* Create super root structure */
822 /* Create root directory */
825 /* Fill in file contents */
831 /* Save fsdata and superblocks */
833 create_super_block();
835 for (i = 0; i < NANDFS_NFSAREAS; i++) {
836 if (fsdata_blocks_state[i] != NANDFS_BLOCK_GOOD)
839 data = get_block((i * erasesize)/blocksize, 0);
842 data = get_block((i * erasesize + NANDFS_SBLOCK_OFFSET_BYTES) /
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));
852 /* Save segment summary and CRCs */
853 save_segsum(get_block(NANDFS_FIRST_BLOCK, 0));
861 struct nandfs_block *block;
865 /* Overwrite next block with ff if not nand device */
867 data = get_block(seg_endblock, 0);
868 memset(data, 0xff, blocksize);
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");
880 check_parameters(void)
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,
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);
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.");
904 /* check volume label */
907 while (isalnum(volumelabel[++i]))
910 if (volumelabel[i] != '\0') {
911 errx(1, "bad volume label. "
912 "Valid characters are alphanumerics.");
915 if (strlen(volumelabel) >= 16)
916 errx(1, "Bad volume label. Length is longer than %d.",
920 nandfs_time = time(NULL);
924 print_parameters(void)
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);
935 * Exit with error if file system is mounted.
938 check_mounted(const char *fname, mode_t mode)
945 if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
946 err(1, "getmntinfo");
948 len = strlen(_PATH_DEV);
950 if (!strncmp(s1, _PATH_DEV, len))
953 r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
956 s2 = mp->f_mntfromname;
958 if (!strncmp(s2, _PATH_DEV, len))
960 if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
962 errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
967 calculate_geometry(int fd)
969 struct chip_param_io chip_params;
970 char ident[DISK_IDENT_SIZE];
971 char medianame[MAXPATHLEN];
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);
978 is_nand = (strstr(medianame, "gnand") != NULL);
979 debug("is_nand = %d", is_nand);
981 sectorsize = g_sectorsize(fd);
982 debug("sectorsize: %#zx", sectorsize);
984 /* Get storage size */
985 mediasize = g_mediasize(fd);
986 debug("mediasize: %#jx", mediasize);
988 /* Get storage erase unit size */
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;
994 errx(1, "Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
996 debug("erasesize: %#jx", (uintmax_t)erasesize);
998 if (blocks_per_segment == 0) {
999 if (erasesize >= NANDFS_MIN_SEGSIZE)
1000 blocks_per_segment = erasesize / blocksize;
1002 blocks_per_segment = NANDFS_MIN_SEGSIZE / blocksize;
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);
1013 erase_device(int fd)
1016 uint64_t i, nblocks;
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;
1027 fsdata_blocks_state[i] = NANDFS_BLOCK_GOOD;
1030 if (failed == NANDFS_NFSAREAS) {
1031 printf("%d first blocks not usable. Unable to create "
1032 "filesystem.\n", failed);
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",
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;
1048 if (bad_segments_count == nsegments) {
1049 printf("no valid segments\n");
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");
1066 erase_initial(int fd)
1071 memset(buf, 0xff, sizeof(buf));
1073 lseek(fd, 0, SEEK_SET);
1074 for (i = 0; i < NANDFS_NFSAREAS * erasesize; i += sizeof(buf))
1075 write(fd, buf, sizeof(buf));
1079 create_nandfs(int fd)
1091 printf("filesystem was created successfully\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",
1096 blocks_per_segment * blocksize) / (1024 * 1024),
1097 ((nsegments - bad_segments_count) *
1098 blocks_per_segment * blocksize) / (1024 * 1024));
1102 main(int argc, char *argv[])
1105 char buf[MAXPATHLEN];
1106 const char opts[] = "b:B:L:m:";
1110 while ((ch = getopt(argc, argv, opts)) != -1) {
1113 blocksize = strtol(optarg, (char **)NULL, 10);
1118 blocks_per_segment = strtol(optarg, (char **)NULL, 10);
1119 if (blocks_per_segment == 0)
1123 volumelabel = optarg;
1126 rsv_segment_percent = strtol(optarg, (char **)NULL, 10);
1127 if (rsv_segment_percent == 0)
1137 if (argc < 1 || argc > 2)
1140 /* construct proper device path */
1142 if (!strchr(fname, '/')) {
1143 snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
1144 if (!(fname = strdup(buf)))
1148 fd = g_open(fname, 1);
1150 err(1, "Cannot open %s", fname);
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);
1157 check_mounted(fname, sb.st_mode);
1159 calculate_geometry(fd);