Lines Matching +full:rc +full:- +full:map +full:- +full:name

1 // SPDX-License-Identifier: GPL-2.0-only
3 * rfd_ftl.c -- resident flash disk (flash translation layer)
95 struct block *block = &part->blocks[block_no]; in build_block_map()
98 block->offset = part->block_size * block_no; in build_block_map()
100 if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) { in build_block_map()
101 block->state = BLOCK_UNUSED; in build_block_map()
102 return -ENOENT; in build_block_map()
105 block->state = BLOCK_OK; in build_block_map()
107 for (i=0; i<part->data_sectors_per_block; i++) { in build_block_map()
110 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]); in build_block_map()
116 block->free_sectors++; in build_block_map()
123 if (entry >= part->sector_count) { in build_block_map()
127 part->mbd.mtd->name, block_no, i, entry); in build_block_map()
131 if (part->sector_map[entry] != -1) { in build_block_map()
134 part->mbd.mtd->name, entry); in build_block_map()
135 part->errors = 1; in build_block_map()
139 part->sector_map[entry] = block->offset + in build_block_map()
140 (i + part->header_sectors_per_block) * SECTOR_SIZE; in build_block_map()
142 block->used_sectors++; in build_block_map()
145 if (block->free_sectors == part->data_sectors_per_block) in build_block_map()
146 part->reserved_block = block_no; in build_block_map()
154 int i, rc = -ENOMEM; in scan_header() local
158 sectors_per_block = part->block_size / SECTOR_SIZE; in scan_header()
159 part->total_blocks = (u32)part->mbd.mtd->size / part->block_size; in scan_header()
161 if (part->total_blocks < 2) in scan_header()
162 return -ENOENT; in scan_header()
164 /* each erase block has three bytes header, followed by the map */ in scan_header()
165 part->header_sectors_per_block = in scan_header()
167 sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE; in scan_header()
169 part->data_sectors_per_block = sectors_per_block - in scan_header()
170 part->header_sectors_per_block; in scan_header()
172 part->header_size = (HEADER_MAP_OFFSET + in scan_header()
173 part->data_sectors_per_block) * sizeof(u16); in scan_header()
175 part->cylinders = (part->data_sectors_per_block * in scan_header()
176 (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK; in scan_header()
178 part->sector_count = part->cylinders * SECTORS_PER_TRACK; in scan_header()
180 part->current_block = -1; in scan_header()
181 part->reserved_block = -1; in scan_header()
182 part->is_reclaiming = 0; in scan_header()
184 part->header_cache = kmalloc(part->header_size, GFP_KERNEL); in scan_header()
185 if (!part->header_cache) in scan_header()
188 part->blocks = kcalloc(part->total_blocks, sizeof(struct block), in scan_header()
190 if (!part->blocks) in scan_header()
193 part->sector_map = vmalloc(array_size(sizeof(u_long), in scan_header()
194 part->sector_count)); in scan_header()
195 if (!part->sector_map) in scan_header()
198 for (i=0; i<part->sector_count; i++) in scan_header()
199 part->sector_map[i] = -1; in scan_header()
201 for (i=0, blocks_found=0; i<part->total_blocks; i++) { in scan_header()
202 rc = mtd_read(part->mbd.mtd, i * part->block_size, in scan_header()
203 part->header_size, &retlen, in scan_header()
204 (u_char *)part->header_cache); in scan_header()
206 if (!rc && retlen != part->header_size) in scan_header()
207 rc = -EIO; in scan_header()
209 if (rc) in scan_header()
218 part->mbd.mtd->name); in scan_header()
219 rc = -ENOENT; in scan_header()
223 if (part->reserved_block == -1) { in scan_header()
225 part->mbd.mtd->name); in scan_header()
227 part->errors = 1; in scan_header()
233 vfree(part->sector_map); in scan_header()
234 kfree(part->header_cache); in scan_header()
235 kfree(part->blocks); in scan_header()
237 return rc; in scan_header()
245 int rc; in rfd_ftl_readsect() local
247 if (sector >= part->sector_count) in rfd_ftl_readsect()
248 return -EIO; in rfd_ftl_readsect()
250 addr = part->sector_map[sector]; in rfd_ftl_readsect()
251 if (addr != -1) { in rfd_ftl_readsect()
252 rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen, in rfd_ftl_readsect()
254 if (!rc && retlen != SECTOR_SIZE) in rfd_ftl_readsect()
255 rc = -EIO; in rfd_ftl_readsect()
257 if (rc) { in rfd_ftl_readsect()
259 "0x%lx\n", part->mbd.mtd->name, addr); in rfd_ftl_readsect()
260 return rc; in rfd_ftl_readsect()
271 int rc; in erase_block() local
275 return -ENOMEM; in erase_block()
277 erase->addr = part->blocks[block].offset; in erase_block()
278 erase->len = part->block_size; in erase_block()
280 part->blocks[block].state = BLOCK_ERASING; in erase_block()
281 part->blocks[block].free_sectors = 0; in erase_block()
283 rc = mtd_erase(part->mbd.mtd, erase); in erase_block()
284 if (rc) { in erase_block()
286 "failed\n", (unsigned long long)erase->addr, in erase_block()
287 (unsigned long long)erase->len, part->mbd.mtd->name); in erase_block()
288 part->blocks[block].state = BLOCK_FAILED; in erase_block()
289 part->blocks[block].free_sectors = 0; in erase_block()
290 part->blocks[block].used_sectors = 0; in erase_block()
295 part->blocks[block].state = BLOCK_ERASED; in erase_block()
296 part->blocks[block].free_sectors = part->data_sectors_per_block; in erase_block()
297 part->blocks[block].used_sectors = 0; in erase_block()
298 part->blocks[block].erases++; in erase_block()
300 rc = mtd_write(part->mbd.mtd, part->blocks[block].offset, in erase_block()
302 if (!rc && retlen != sizeof(magic)) in erase_block()
303 rc = -EIO; in erase_block()
305 if (rc) { in erase_block()
307 part->mbd.mtd->name, part->blocks[block].offset); in erase_block()
308 part->blocks[block].state = BLOCK_FAILED; in erase_block()
310 part->blocks[block].state = BLOCK_OK; in erase_block()
316 return rc; in erase_block()
322 u16 *map; in move_block_contents() local
324 int i, rc = -ENOMEM; in move_block_contents() local
326 part->is_reclaiming = 1; in move_block_contents()
332 map = kmalloc(part->header_size, GFP_KERNEL); in move_block_contents()
333 if (!map) in move_block_contents()
336 rc = mtd_read(part->mbd.mtd, part->blocks[block_no].offset, in move_block_contents()
337 part->header_size, &retlen, (u_char *)map); in move_block_contents()
339 if (!rc && retlen != part->header_size) in move_block_contents()
340 rc = -EIO; in move_block_contents()
342 if (rc) { in move_block_contents()
344 "0x%lx\n", part->mbd.mtd->name, in move_block_contents()
345 part->blocks[block_no].offset); in move_block_contents()
350 for (i=0; i<part->data_sectors_per_block; i++) { in move_block_contents()
351 u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]); in move_block_contents()
362 if (entry >= part->sector_count) in move_block_contents()
365 addr = part->blocks[block_no].offset + in move_block_contents()
366 (i + part->header_sectors_per_block) * SECTOR_SIZE; in move_block_contents()
369 *old_sector = -1; in move_block_contents()
370 if (!part->blocks[block_no].used_sectors--) { in move_block_contents()
371 rc = erase_block(part, block_no); in move_block_contents()
376 rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen, in move_block_contents()
379 if (!rc && retlen != SECTOR_SIZE) in move_block_contents()
380 rc = -EIO; in move_block_contents()
382 if (rc) { in move_block_contents()
385 part->mbd.mtd->name); in move_block_contents()
390 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part, in move_block_contents()
393 if (rc) in move_block_contents()
398 kfree(map); in move_block_contents()
402 part->is_reclaiming = 0; in move_block_contents()
404 return rc; in move_block_contents()
410 int rc; in reclaim_block() local
413 mtd_sync(part->mbd.mtd); in reclaim_block()
416 best_block = -1; in reclaim_block()
417 if (*old_sector != -1) in reclaim_block()
418 old_sector_block = *old_sector / part->block_size; in reclaim_block()
420 old_sector_block = -1; in reclaim_block()
422 for (block=0; block<part->total_blocks; block++) { in reclaim_block()
425 if (block == part->reserved_block) in reclaim_block()
433 if (part->blocks[block].free_sectors) in reclaim_block()
436 this_score = part->blocks[block].used_sectors; in reclaim_block()
439 this_score--; in reclaim_block()
442 if (part->blocks[block].used_sectors == in reclaim_block()
443 part->data_sectors_per_block) in reclaim_block()
447 this_score += part->blocks[block].erases; in reclaim_block()
455 if (best_block == -1) in reclaim_block()
456 return -ENOSPC; in reclaim_block()
458 part->current_block = -1; in reclaim_block()
459 part->reserved_block = best_block; in reclaim_block()
463 part->blocks[best_block].used_sectors, in reclaim_block()
464 part->blocks[best_block].free_sectors); in reclaim_block()
466 if (part->blocks[best_block].used_sectors) in reclaim_block()
467 rc = move_block_contents(part, best_block, old_sector); in reclaim_block()
469 rc = erase_block(part, best_block); in reclaim_block()
471 return rc; in reclaim_block()
483 block = part->current_block == -1 ? in find_free_block()
484 jiffies % part->total_blocks : part->current_block; in find_free_block()
488 if (part->blocks[block].free_sectors && in find_free_block()
489 block != part->reserved_block) in find_free_block()
492 if (part->blocks[block].state == BLOCK_UNUSED) in find_free_block()
495 if (++block >= part->total_blocks) in find_free_block()
500 return -1; in find_free_block()
505 int rc, block; in find_writable_block() local
510 if (block == -1) { in find_writable_block()
511 if (!part->is_reclaiming) { in find_writable_block()
512 rc = reclaim_block(part, old_sector); in find_writable_block()
513 if (rc) in find_writable_block()
519 if (block == -1) { in find_writable_block()
520 rc = -ENOSPC; in find_writable_block()
525 rc = mtd_read(part->mbd.mtd, part->blocks[block].offset, in find_writable_block()
526 part->header_size, &retlen, in find_writable_block()
527 (u_char *)part->header_cache); in find_writable_block()
529 if (!rc && retlen != part->header_size) in find_writable_block()
530 rc = -EIO; in find_writable_block()
532 if (rc) { in find_writable_block()
534 "0x%lx\n", part->mbd.mtd->name, in find_writable_block()
535 part->blocks[block].offset); in find_writable_block()
539 part->current_block = block; in find_writable_block()
542 return rc; in find_writable_block()
547 int block, offset, rc; in mark_sector_deleted() local
552 block = old_addr / part->block_size; in mark_sector_deleted()
553 offset = (old_addr % part->block_size) / SECTOR_SIZE - in mark_sector_deleted()
554 part->header_sectors_per_block; in mark_sector_deleted()
556 addr = part->blocks[block].offset + in mark_sector_deleted()
558 rc = mtd_write(part->mbd.mtd, addr, sizeof(del), &retlen, in mark_sector_deleted()
561 if (!rc && retlen != sizeof(del)) in mark_sector_deleted()
562 rc = -EIO; in mark_sector_deleted()
564 if (rc) { in mark_sector_deleted()
566 "0x%lx\n", part->mbd.mtd->name, addr); in mark_sector_deleted()
569 if (block == part->current_block) in mark_sector_deleted()
570 part->header_cache[offset + HEADER_MAP_OFFSET] = del; in mark_sector_deleted()
572 part->blocks[block].used_sectors--; in mark_sector_deleted()
574 if (!part->blocks[block].used_sectors && in mark_sector_deleted()
575 !part->blocks[block].free_sectors) in mark_sector_deleted()
576 rc = erase_block(part, block); in mark_sector_deleted()
579 return rc; in mark_sector_deleted()
586 i = stop = part->data_sectors_per_block - block->free_sectors; in find_free_sector()
589 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]) in find_free_sector()
593 if (++i == part->data_sectors_per_block) in find_free_sector()
598 return -1; in find_free_sector()
607 int rc; in do_writesect() local
611 if (part->current_block == -1 || in do_writesect()
612 !part->blocks[part->current_block].free_sectors) { in do_writesect()
614 rc = find_writable_block(part, old_addr); in do_writesect()
615 if (rc) in do_writesect()
619 block = &part->blocks[part->current_block]; in do_writesect()
624 rc = -ENOSPC; in do_writesect()
628 addr = (i + part->header_sectors_per_block) * SECTOR_SIZE + in do_writesect()
629 block->offset; in do_writesect()
630 rc = mtd_write(part->mbd.mtd, addr, SECTOR_SIZE, &retlen, in do_writesect()
633 if (!rc && retlen != SECTOR_SIZE) in do_writesect()
634 rc = -EIO; in do_writesect()
636 if (rc) { in do_writesect()
638 part->mbd.mtd->name, addr); in do_writesect()
642 part->sector_map[sector] = addr; in do_writesect()
646 part->header_cache[i + HEADER_MAP_OFFSET] = entry; in do_writesect()
648 addr = block->offset + (HEADER_MAP_OFFSET + i) * sizeof(u16); in do_writesect()
649 rc = mtd_write(part->mbd.mtd, addr, sizeof(entry), &retlen, in do_writesect()
652 if (!rc && retlen != sizeof(entry)) in do_writesect()
653 rc = -EIO; in do_writesect()
655 if (rc) { in do_writesect()
657 part->mbd.mtd->name, addr); in do_writesect()
660 block->used_sectors++; in do_writesect()
661 block->free_sectors--; in do_writesect()
664 return rc; in do_writesect()
672 int rc = 0; in rfd_ftl_writesect() local
676 if (part->reserved_block == -1) { in rfd_ftl_writesect()
677 rc = -EACCES; in rfd_ftl_writesect()
681 if (sector >= part->sector_count) { in rfd_ftl_writesect()
682 rc = -EIO; in rfd_ftl_writesect()
686 old_addr = part->sector_map[sector]; in rfd_ftl_writesect()
692 rc = do_writesect(dev, sector, buf, &old_addr); in rfd_ftl_writesect()
693 if (rc) in rfd_ftl_writesect()
699 part->sector_map[sector] = -1; in rfd_ftl_writesect()
701 if (old_addr != -1) in rfd_ftl_writesect()
702 rc = mark_sector_deleted(part, old_addr); in rfd_ftl_writesect()
705 return rc; in rfd_ftl_writesect()
713 int rc; in rfd_ftl_discardsect() local
716 if (sector >= part->sector_count) in rfd_ftl_discardsect()
717 return -EIO; in rfd_ftl_discardsect()
719 addr = part->sector_map[sector]; in rfd_ftl_discardsect()
721 if (addr != -1) { in rfd_ftl_discardsect()
722 rc = mark_sector_deleted(part, addr); in rfd_ftl_discardsect()
723 if (rc) in rfd_ftl_discardsect()
724 return rc; in rfd_ftl_discardsect()
726 part->sector_map[sector] = -1; in rfd_ftl_discardsect()
730 nr_sects--; in rfd_ftl_discardsect()
740 geo->heads = 1; in rfd_ftl_getgeo()
741 geo->sectors = SECTORS_PER_TRACK; in rfd_ftl_getgeo()
742 geo->cylinders = part->cylinders; in rfd_ftl_getgeo()
751 if ((mtd->type != MTD_NORFLASH && mtd->type != MTD_RAM) || in rfd_ftl_add_mtd()
752 mtd->size > UINT_MAX) in rfd_ftl_add_mtd()
759 part->mbd.mtd = mtd; in rfd_ftl_add_mtd()
762 part->block_size = block_size; in rfd_ftl_add_mtd()
764 if (!mtd->erasesize) { in rfd_ftl_add_mtd()
768 part->block_size = mtd->erasesize; in rfd_ftl_add_mtd()
772 part->mbd.size = part->sector_count; in rfd_ftl_add_mtd()
773 part->mbd.tr = tr; in rfd_ftl_add_mtd()
774 part->mbd.devnum = -1; in rfd_ftl_add_mtd()
775 if (!(mtd->flags & MTD_WRITEABLE)) in rfd_ftl_add_mtd()
776 part->mbd.readonly = 1; in rfd_ftl_add_mtd()
777 else if (part->errors) { in rfd_ftl_add_mtd()
779 "setting read-only\n", mtd->name); in rfd_ftl_add_mtd()
780 part->mbd.readonly = 1; in rfd_ftl_add_mtd()
783 printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n", in rfd_ftl_add_mtd()
784 mtd->name, mtd->type, mtd->flags); in rfd_ftl_add_mtd()
786 if (!add_mtd_blktrans_dev(&part->mbd)) in rfd_ftl_add_mtd()
798 for (i=0; i<part->total_blocks; i++) { in rfd_ftl_remove_dev()
800 part->mbd.mtd->name, i, part->blocks[i].erases); in rfd_ftl_remove_dev()
803 vfree(part->sector_map); in rfd_ftl_remove_dev()
804 kfree(part->header_cache); in rfd_ftl_remove_dev()
805 kfree(part->blocks); in rfd_ftl_remove_dev()
806 del_mtd_blktrans_dev(&part->mbd); in rfd_ftl_remove_dev()
810 .name = "rfd",