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: stable/10/sbin/newfs_nandfs/newfs_nandfs.c 316772 2017-04-13 17:11:50Z dim $");
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
nandfs_first_block(void)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
usage(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
nandfs_log2(unsigned n)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
crc32_le(uint32_t crc,const uint8_t * buf,size_t len)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 *
get_block(uint32_t block_nr,uint64_t offset)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
nandfs_seg_usage_blk_offset(uint64_t seg,uint64_t * blk,uint64_t * offset)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
segment_size(void)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
prepare_blockgrouped_file(uint32_t block)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
alloc_blockgrouped_file(uint32_t block,uint32_t entry)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
count_su_blocks(void)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
count_seg_blocks(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
assign_file_blocks(uint64_t start_block)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
save_datfile(void)405 save_datfile(void)
406 {
407 
408 	prepare_blockgrouped_file(datfile.blocks[0]);
409 }
410 
411 static uint64_t
update_datfile(uint64_t block)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 *
update_block_info(union nandfs_binfo * binfo,struct file_info * file)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
save_segsum(struct nandfs_segment_summary * ss)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
create_fsdata(void)521 create_fsdata(void)
522 {
523 	struct uuid tmp;
524 
525 	memset(&fsdata, 0, sizeof(struct nandfs_fsdata));
526 
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;
538 	fsdata.f_errors = 1;
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);
543 
544 	uuidgen(&tmp, 1);
545 	fsdata.f_uuid = tmp;
546 
547 	if (volumelabel)
548 		memcpy(fsdata.f_volume_name, volumelabel, 16);
549 
550 	fsdata.f_sum = crc32_le(0, (const uint8_t *)&fsdata,
551 	    NANDFS_FSDATA_CRC_BYTES);
552 }
553 
554 static void
save_fsdata(void * data)555 save_fsdata(void *data)
556 {
557 
558 	memcpy(data, &fsdata, sizeof(fsdata));
559 }
560 
561 static void
create_super_block(void)562 create_super_block(void)
563 {
564 
565 	memset(&super_block, 0, sizeof(struct nandfs_super_block));
566 
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;
576 
577 	super_block.s_sum = crc32_le(0, (const uint8_t *)&super_block,
578 	    NANDFS_SB_BYTES);
579 }
580 
581 static void
save_super_block(void * data)582 save_super_block(void *data)
583 {
584 
585 	memcpy(data, &super_block, sizeof(super_block));
586 }
587 
588 static void
save_super_root(void)589 save_super_root(void)
590 {
591 
592 	sr->sr_bytes = NANDFS_SR_BYTES;
593 	sr->sr_flags = 0;
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;
598 }
599 
600 static struct nandfs_dir_entry *
add_de(void * block,struct nandfs_dir_entry * de,uint64_t ino,const char * name,uint8_t type)601 add_de(void *block, struct nandfs_dir_entry *de, uint64_t ino,
602     const char *name, uint8_t type)
603 {
604 	uint16_t reclen;
605 
606 	/* modify last de */
607 	de->rec_len = NANDFS_DIR_REC_LEN(de->name_len);
608 	de = (void *)((uint8_t *)de + de->rec_len);
609 
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");
613 		return (NULL);
614 	}
615 
616 	de->inode = ino;
617 	de->rec_len = reclen;
618 	de->name_len = strlen(name);
619 	de->file_type = type;
620 	memset(de->name, 0,
621 	    (strlen(name) + NANDFS_DIR_PAD - 1) & ~NANDFS_DIR_ROUND);
622 	memcpy(de->name, name, strlen(name));
623 
624 	return (de);
625 }
626 
627 static struct nandfs_dir_entry *
make_dir(void * block,uint64_t ino,uint64_t parent_ino)628 make_dir(void *block, uint64_t ino, uint64_t parent_ino)
629 {
630 	struct nandfs_dir_entry *de = (struct nandfs_dir_entry *)block;
631 
632 	/* create '..' entry */
633 	de->inode = parent_ino;
634 	de->rec_len = NANDFS_DIR_REC_LEN(2);
635 	de->name_len = 2;
636 	de->file_type = DT_DIR;
637 	memset(de->name, 0, NANDFS_DIR_NAME_LEN(2));
638 	memcpy(de->name, "..", 2);
639 
640 	/* create '.' entry */
641 	de = (void *)((uint8_t *)block + NANDFS_DIR_REC_LEN(2));
642 	de->inode = ino;
643 	de->rec_len = blocksize - NANDFS_DIR_REC_LEN(2);
644 	de->name_len = 1;
645 	de->file_type = DT_DIR;
646 	memset(de->name, 0, NANDFS_DIR_NAME_LEN(1));
647 	memcpy(de->name, ".", 1);
648 
649 	return (de);
650 }
651 
652 static void
save_root_dir(void)653 save_root_dir(void)
654 {
655 	struct file_info *root = &user_files[0];
656 	struct nandfs_dir_entry *de;
657 	uint32_t i;
658 	void *block;
659 
660 	block = get_block(root->blocks[0], 0);
661 
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));
666 
667 	root->size = ((uintptr_t)de - (uintptr_t)block) +
668 	    NANDFS_DIR_REC_LEN(de->name_len);
669 }
670 
671 static void
save_sufile(void)672 save_sufile(void)
673 {
674 	struct nandfs_sufile_header *header;
675 	struct nandfs_segment_usage *su;
676 	uint64_t blk, i, off;
677 	void *block;
678 	int start;
679 
680 	/*
681 	 * At the beginning just zero-out everything
682 	 */
683 	for (i = 0; i < sufile.nblocks; i++)
684 		get_block(sufile.blocks[i], 0);
685 
686 	start = 0;
687 
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;
693 
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;
701 	off++;
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;
716 	}
717 }
718 
719 static void
save_cpfile(void)720 save_cpfile(void)
721 {
722 	struct nandfs_cpfile_header *header;
723 	struct nandfs_checkpoint *cp, *initial_cp;
724 	int i, entries = blocksize / sizeof(struct nandfs_checkpoint);
725 	uint64_t cno;
726 
727 	header = (struct nandfs_cpfile_header *)get_block(cpfile.blocks[0], 0);
728 	header->ch_ncheckpoints = 1;
729 	header->ch_nsnapshots = 0;
730 
731 	cp = (struct nandfs_checkpoint *)header;
732 
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));
743 
744 	ifile.inode = &initial_cp->cp_ifile_inode;
745 
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;
752 	}
753 }
754 
755 static void
init_inode(struct nandfs_inode * inode,struct file_info * file)756 init_inode(struct nandfs_inode *inode, struct file_info *file)
757 {
758 
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;
764 
765 	if (file->size > 0)
766 		inode->i_size = file->size;
767 	else
768 		inode->i_size = 0;
769 
770 	if (file->ino == NANDFS_USER_INO)
771 		inode->i_flags = SF_NOUNLINK|UF_NOUNLINK;
772 	else
773 		inode->i_flags = 0;
774 }
775 
776 static void
save_ifile(void)777 save_ifile(void)
778 {
779 	struct nandfs_inode *inode;
780 	struct file_info *file;
781 	uint64_t ino, blk, off;
782 	uint32_t i;
783 
784 	prepare_blockgrouped_file(ifile.blocks[0]);
785 	for (i = 0; i <= NANDFS_USER_INO; i++)
786 		alloc_blockgrouped_file(ifile.blocks[0], i);
787 
788 	for (i = 0; i < nuserfiles; i++) {
789 		file = &user_files[i];
790 		ino = file->ino;
791 		blk = ino / (blocksize / sizeof(*inode));
792 		off = ino % (blocksize / sizeof(*inode));
793 		inode =
794 		    (struct nandfs_inode *)get_block(ifile.blocks[2 + blk], 2 + blk);
795 		file->inode = &inode[off];
796 		init_inode(file->inode, file);
797 	}
798 
799 	init_inode(ifile.inode, &ifile);
800 	init_inode(cpfile.inode, &cpfile);
801 	init_inode(sufile.inode, &sufile);
802 	init_inode(datfile.inode, &datfile);
803 }
804 
805 static int
create_fs(void)806 create_fs(void)
807 {
808 	uint64_t start_block;
809 	uint32_t segsum_size;
810 	char *data;
811 	int i;
812 
813 	nuserfiles = (sizeof(user_files) / sizeof(user_files[0]));
814 
815 	/* Count and assign blocks */
816 	count_seg_blocks();
817 	segsum_size = segment_size();
818 	start_block = NANDFS_FIRST_BLOCK + SIZE_TO_BLOCK(segsum_size);
819 	assign_file_blocks(start_block);
820 
821 	/* Create super root structure */
822 	save_super_root();
823 
824 	/* Create root directory */
825 	save_root_dir();
826 
827 	/* Fill in file contents */
828 	save_sufile();
829 	save_cpfile();
830 	save_ifile();
831 	save_datfile();
832 
833 	/* Save fsdata and superblocks */
834 	create_fsdata();
835 	create_super_block();
836 
837 	for (i = 0; i < NANDFS_NFSAREAS; i++) {
838 		if (fsdata_blocks_state[i] != NANDFS_BLOCK_GOOD)
839 			continue;
840 
841 		data = get_block((i * erasesize)/blocksize, 0);
842 		save_fsdata(data);
843 
844 		data = get_block((i * erasesize + NANDFS_SBLOCK_OFFSET_BYTES) /
845 		    blocksize, 0);
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));
852 	}
853 
854 	/* Save segment summary and CRCs */
855 	save_segsum(get_block(NANDFS_FIRST_BLOCK, 0));
856 
857 	return (0);
858 }
859 
860 static void
write_fs(int fda)861 write_fs(int fda)
862 {
863 	struct nandfs_block *block;
864 	char *data;
865 	u_int ret;
866 
867 	/* Overwrite next block with ff if not nand device */
868 	if (!is_nand) {
869 		data = get_block(seg_endblock, 0);
870 		memset(data, 0xff, blocksize);
871 	}
872 
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");
878 	}
879 }
880 
881 static void
check_parameters(void)882 check_parameters(void)
883 {
884 	int i;
885 
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,
891 		    MAXBSIZE);
892 	}
893 
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);
900 
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.");
905 
906 	/* check volume label */
907 	i = 0;
908 	if (volumelabel) {
909 		while (isalnum(volumelabel[++i]))
910 			;
911 
912 		if (volumelabel[i] != '\0') {
913 			errx(1, "bad volume label. "
914 			    "Valid characters are alphanumerics.");
915 		}
916 
917 		if (strlen(volumelabel) >= 16)
918 			errx(1, "Bad volume label. Length is longer than %d.",
919 			    16);
920 	}
921 
922 	nandfs_time = time(NULL);
923 }
924 
925 static void
print_parameters(void)926 print_parameters(void)
927 {
928 
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);
934 }
935 
936 /*
937  * Exit with error if file system is mounted.
938  */
939 static void
check_mounted(const char * fname,mode_t mode)940 check_mounted(const char *fname, mode_t mode)
941 {
942 	struct statfs *mp;
943 	const char *s1, *s2;
944 	size_t len;
945 	int n, r;
946 
947 	if (!(n = getmntinfo(&mp, MNT_NOWAIT)))
948 		err(1, "getmntinfo");
949 
950 	len = strlen(_PATH_DEV);
951 	s1 = fname;
952 	if (!strncmp(s1, _PATH_DEV, len))
953 		s1 += len;
954 
955 	r = S_ISCHR(mode) && s1 != fname && *s1 == 'r';
956 
957 	for (; n--; mp++) {
958 		s2 = mp->f_mntfromname;
959 
960 		if (!strncmp(s2, _PATH_DEV, len))
961 			s2 += len;
962 		if ((r && s2 != mp->f_mntfromname && !strcmp(s1 + 1, s2)) ||
963 		    !strcmp(s1, s2))
964 			errx(1, "%s is mounted on %s", fname, mp->f_mntonname);
965 	}
966 }
967 
968 static void
calculate_geometry(int fd)969 calculate_geometry(int fd)
970 {
971 	struct chip_param_io chip_params;
972 	char ident[DISK_IDENT_SIZE];
973 	char medianame[MAXPATHLEN];
974 
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);
979 
980 	is_nand = (strstr(medianame, "gnand") != NULL);
981 	debug("is_nand = %d", is_nand);
982 
983 	sectorsize = g_sectorsize(fd);
984 	debug("sectorsize: %#zx", sectorsize);
985 
986 	/* Get storage size */
987 	mediasize = g_mediasize(fd);
988 	debug("mediasize: %#jx", mediasize);
989 
990 	/* Get storage erase unit size */
991 	if (!is_nand)
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;
995 	else
996 		errx(1, "Cannot ioctl(NAND_IO_GET_CHIP_PARAM)");
997 
998 	debug("erasesize: %#jx", (uintmax_t)erasesize);
999 
1000 	if (blocks_per_segment == 0) {
1001 		if (erasesize >= NANDFS_MIN_SEGSIZE)
1002 			blocks_per_segment = erasesize / blocksize;
1003 		else
1004 			blocks_per_segment = NANDFS_MIN_SEGSIZE / blocksize;
1005 	}
1006 
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);
1012 }
1013 
1014 static void
erase_device(int fd)1015 erase_device(int fd)
1016 {
1017 	int rest, failed;
1018 	uint64_t i, nblocks;
1019 	off_t offset;
1020 
1021 	failed = 0;
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;
1027 			failed++;
1028 		} else
1029 			fsdata_blocks_state[i] = NANDFS_BLOCK_GOOD;
1030 	}
1031 
1032 	if (failed == NANDFS_NFSAREAS) {
1033 		printf("%d first blocks not usable. Unable to create "
1034 		    "filesystem.\n", failed);
1035 		exit(1);
1036 	}
1037 
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",
1042 			    i, offset);
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;
1047 		}
1048 	}
1049 
1050 	if (bad_segments_count == nsegments) {
1051 		printf("no valid segments\n");
1052 		exit(1);
1053 	}
1054 
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");
1063 		}
1064 	}
1065 }
1066 
1067 static void
erase_initial(int fd)1068 erase_initial(int fd)
1069 {
1070 	char buf[512];
1071 	u_int i;
1072 
1073 	memset(buf, 0xff, sizeof(buf));
1074 
1075 	lseek(fd, 0, SEEK_SET);
1076 	for (i = 0; i < NANDFS_NFSAREAS * erasesize; i += sizeof(buf))
1077 		write(fd, buf, sizeof(buf));
1078 }
1079 
1080 static void
create_nandfs(int fd)1081 create_nandfs(int fd)
1082 {
1083 
1084 	create_fs();
1085 
1086 	write_fs(fd);
1087 }
1088 
1089 static void
print_summary(void)1090 print_summary(void)
1091 {
1092 
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",
1097 	    (nsegments *
1098 	    blocks_per_segment * blocksize) / (1024 * 1024),
1099 	    ((nsegments - bad_segments_count) *
1100 	    blocks_per_segment * blocksize) / (1024 * 1024));
1101 }
1102 
1103 int
main(int argc,char * argv[])1104 main(int argc, char *argv[])
1105 {
1106 	struct stat sb;
1107 	char buf[MAXPATHLEN];
1108 	const char opts[] = "b:B:L:m:";
1109 	const char *fname;
1110 	int ch, fd;
1111 
1112 	while ((ch = getopt(argc, argv, opts)) != -1) {
1113 		switch (ch) {
1114 		case 'b':
1115 			blocksize = strtol(optarg, (char **)NULL, 10);
1116 			if (blocksize == 0)
1117 				usage();
1118 			break;
1119 		case 'B':
1120 			blocks_per_segment = strtol(optarg, (char **)NULL, 10);
1121 			if (blocks_per_segment == 0)
1122 				usage();
1123 			break;
1124 		case 'L':
1125 			volumelabel = optarg;
1126 			break;
1127 		case 'm':
1128 			rsv_segment_percent = strtol(optarg, (char **)NULL, 10);
1129 			if (rsv_segment_percent == 0)
1130 				usage();
1131 			break;
1132 		default:
1133 			usage();
1134 		}
1135 	}
1136 
1137 	argc -= optind;
1138 	argv += optind;
1139 	if (argc < 1 || argc > 2)
1140 		usage();
1141 
1142 	/* construct proper device path */
1143 	fname = *argv++;
1144 	if (!strchr(fname, '/')) {
1145 		snprintf(buf, sizeof(buf), "%s%s", _PATH_DEV, fname);
1146 		if (!(fname = strdup(buf)))
1147 			err(1, NULL);
1148 	}
1149 
1150 	fd = g_open(fname, 1);
1151 	if (fd == -1)
1152 		err(1, "Cannot open %s", fname);
1153 
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);
1158 
1159 	check_mounted(fname, sb.st_mode);
1160 
1161 	calculate_geometry(fd);
1162 
1163 	check_parameters();
1164 
1165 	print_parameters();
1166 
1167 	if (is_nand)
1168 		erase_device(fd);
1169 	else
1170 		erase_initial(fd);
1171 
1172 	create_nandfs(fd);
1173 
1174 	print_summary();
1175 
1176 	g_close(fd);
1177 
1178 	return (0);
1179 }
1180 
1181 
1182