cregit-Linux how code gets into the kernel

Release 4.14 drivers/nvdimm/btt.c

Directory: drivers/nvdimm
/*
 * Block Translation Table
 * Copyright (c) 2014-2015, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */
#include <linux/highmem.h>
#include <linux/debugfs.h>
#include <linux/blkdev.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/mutex.h>
#include <linux/hdreg.h>
#include <linux/genhd.h>
#include <linux/sizes.h>
#include <linux/ndctl.h>
#include <linux/fs.h>
#include <linux/nd.h>
#include "btt.h"
#include "nd.h"


enum log_ent_request {
	
LOG_NEW_ENT = 0,
	
LOG_OLD_ENT
};


static struct device *to_dev(struct arena_info *arena) { return &arena->nd_btt->dev; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma21100.00%1100.00%
Total21100.00%1100.00%


static u64 adjust_initial_offset(struct nd_btt *nd_btt, u64 offset) { return offset + nd_btt->initial_offset; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma21100.00%1100.00%
Total21100.00%1100.00%


static int arena_read_bytes(struct arena_info *arena, resource_size_t offset, void *buf, size_t n, unsigned long flags) { struct nd_btt *nd_btt = arena->nd_btt; struct nd_namespace_common *ndns = nd_btt->ndns; /* arena offsets may be shifted from the base of the device */ offset = adjust_initial_offset(nd_btt, offset); return nvdimm_read_bytes(ndns, offset, buf, n, flags); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma67100.00%4100.00%
Total67100.00%4100.00%


static int arena_write_bytes(struct arena_info *arena, resource_size_t offset, void *buf, size_t n, unsigned long flags) { struct nd_btt *nd_btt = arena->nd_btt; struct nd_namespace_common *ndns = nd_btt->ndns; /* arena offsets may be shifted from the base of the device */ offset = adjust_initial_offset(nd_btt, offset); return nvdimm_write_bytes(ndns, offset, buf, n, flags); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma67100.00%4100.00%
Total67100.00%4100.00%


static int btt_info_write(struct arena_info *arena, struct btt_sb *super) { int ret; /* * infooff and info2off should always be at least 512B aligned. * We rely on that to make sure rw_bytes does error clearing * correctly, so make sure that is the case. */ dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->infooff, 512), "arena->infooff: %#llx is unaligned\n", arena->infooff); dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->info2off, 512), "arena->info2off: %#llx is unaligned\n", arena->info2off); ret = arena_write_bytes(arena, arena->info2off, super, sizeof(struct btt_sb), 0); if (ret) return ret; return arena_write_bytes(arena, arena->infooff, super, sizeof(struct btt_sb), 0); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma116100.00%4100.00%
Total116100.00%4100.00%


static int btt_info_read(struct arena_info *arena, struct btt_sb *super) { return arena_read_bytes(arena, arena->infooff, super, sizeof(struct btt_sb), 0); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma36100.00%2100.00%
Total36100.00%2100.00%

/* * 'raw' version of btt_map write * Assumptions: * mapping is in little-endian * mapping contains 'E' and 'Z' flags as desired */
static int __btt_map_write(struct arena_info *arena, u32 lba, __le32 mapping, unsigned long flags) { u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE); if (unlikely(lba >= arena->external_nlba)) dev_err_ratelimited(to_dev(arena), "%s: lba %#x out of range (max: %#x)\n", __func__, lba, arena->external_nlba); return arena_write_bytes(arena, ns_off, &mapping, MAP_ENT_SIZE, flags); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma78100.00%3100.00%
Total78100.00%3100.00%


static int btt_map_write(struct arena_info *arena, u32 lba, u32 mapping, u32 z_flag, u32 e_flag, unsigned long rwb_flags) { u32 ze; __le32 mapping_le; /* * This 'mapping' is supposed to be just the LBA mapping, without * any flags set, so strip the flag bits. */ mapping = ent_lba(mapping); ze = (z_flag << 1) + e_flag; switch (ze) { case 0: /* * We want to set neither of the Z or E flags, and * in the actual layout, this means setting the bit * positions of both to '1' to indicate a 'normal' * map entry */ mapping |= MAP_ENT_NORMAL; break; case 1: mapping |= (1 << MAP_ERR_SHIFT); break; case 2: mapping |= (1 << MAP_TRIM_SHIFT); break; default: /* * The case where Z and E are both sent in as '1' could be * construed as a valid 'normal' case, but we decide not to, * to avoid confusion */ dev_err_ratelimited(to_dev(arena), "Invalid use of Z and E flags\n"); return -EIO; } mapping_le = cpu_to_le32(mapping); return __btt_map_write(arena, lba, mapping_le, rwb_flags); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma125100.00%4100.00%
Total125100.00%4100.00%


static int btt_map_read(struct arena_info *arena, u32 lba, u32 *mapping, int *trim, int *error, unsigned long rwb_flags) { int ret; __le32 in; u32 raw_mapping, postmap, ze, z_flag, e_flag; u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE); if (unlikely(lba >= arena->external_nlba)) dev_err_ratelimited(to_dev(arena), "%s: lba %#x out of range (max: %#x)\n", __func__, lba, arena->external_nlba); ret = arena_read_bytes(arena, ns_off, &in, MAP_ENT_SIZE, rwb_flags); if (ret) return ret; raw_mapping = le32_to_cpu(in); z_flag = ent_z_flag(raw_mapping); e_flag = ent_e_flag(raw_mapping); ze = (z_flag << 1) + e_flag; postmap = ent_lba(raw_mapping); /* Reuse the {z,e}_flag variables for *trim and *error */ z_flag = 0; e_flag = 0; switch (ze) { case 0: /* Initial state. Return postmap = premap */ *mapping = lba; break; case 1: *mapping = postmap; e_flag = 1; break; case 2: *mapping = postmap; z_flag = 1; break; case 3: *mapping = postmap; break; default: return -EIO; } if (trim) *trim = z_flag; if (error) *error = e_flag; return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma236100.00%4100.00%
Total236100.00%4100.00%


static int btt_log_read_pair(struct arena_info *arena, u32 lane, struct log_entry *ent) { return arena_read_bytes(arena, arena->logoff + (2 * lane * LOG_ENT_SIZE), ent, 2 * LOG_ENT_SIZE, 0); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma45100.00%2100.00%
Total45100.00%2100.00%

static struct dentry *debugfs_root;
static void arena_debugfs_init(struct arena_info *a, struct dentry *parent, int idx) { char dirname[32]; struct dentry *d; /* If for some reason, parent bttN was not created, exit */ if (!parent) return; snprintf(dirname, 32, "arena%d", idx); d = debugfs_create_dir(dirname, parent); if (IS_ERR_OR_NULL(d)) return; a->debugfs_dir = d; debugfs_create_x64("size", S_IRUGO, d, &a->size); debugfs_create_x64("external_lba_start", S_IRUGO, d, &a->external_lba_start); debugfs_create_x32("internal_nlba", S_IRUGO, d, &a->internal_nlba); debugfs_create_u32("internal_lbasize", S_IRUGO, d, &a->internal_lbasize); debugfs_create_x32("external_nlba", S_IRUGO, d, &a->external_nlba); debugfs_create_u32("external_lbasize", S_IRUGO, d, &a->external_lbasize); debugfs_create_u32("nfree", S_IRUGO, d, &a->nfree); debugfs_create_u16("version_major", S_IRUGO, d, &a->version_major); debugfs_create_u16("version_minor", S_IRUGO, d, &a->version_minor); debugfs_create_x64("nextoff", S_IRUGO, d, &a->nextoff); debugfs_create_x64("infooff", S_IRUGO, d, &a->infooff); debugfs_create_x64("dataoff", S_IRUGO, d, &a->dataoff); debugfs_create_x64("mapoff", S_IRUGO, d, &a->mapoff); debugfs_create_x64("logoff", S_IRUGO, d, &a->logoff); debugfs_create_x64("info2off", S_IRUGO, d, &a->info2off); debugfs_create_x32("flags", S_IRUGO, d, &a->flags); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma295100.00%1100.00%
Total295100.00%1100.00%


static void btt_debugfs_init(struct btt *btt) { int i = 0; struct arena_info *arena; btt->debugfs_dir = debugfs_create_dir(dev_name(&btt->nd_btt->dev), debugfs_root); if (IS_ERR_OR_NULL(btt->debugfs_dir)) return; list_for_each_entry(arena, &btt->arena_list, list) { arena_debugfs_init(arena, btt->debugfs_dir, i); i++; } }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma74100.00%1100.00%
Total74100.00%1100.00%

/* * This function accepts two log entries, and uses the * sequence number to find the 'older' entry. * It also updates the sequence number in this old entry to * make it the 'new' one if the mark_flag is set. * Finally, it returns which of the entries was the older one. * * TODO The logic feels a bit kludge-y. make it better.. */
static int btt_log_get_old(struct log_entry *ent) { int old; /* * the first ever time this is seen, the entry goes into [0] * the next time, the following logic works out to put this * (next) entry into [1] */ if (ent[0].seq == 0) { ent[0].seq = cpu_to_le32(1); return 0; } if (ent[0].seq == ent[1].seq) return -EINVAL; if (le32_to_cpu(ent[0].seq) + le32_to_cpu(ent[1].seq) > 5) return -EINVAL; if (le32_to_cpu(ent[0].seq) < le32_to_cpu(ent[1].seq)) { if (le32_to_cpu(ent[1].seq) - le32_to_cpu(ent[0].seq) == 1) old = 0; else old = 1; } else { if (le32_to_cpu(ent[0].seq) - le32_to_cpu(ent[1].seq) == 1) old = 1; else old = 0; } return old; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma187100.00%1100.00%
Total187100.00%1100.00%

/* * This function copies the desired (old/new) log entry into ent if * it is not NULL. It returns the sub-slot number (0 or 1) * where the desired log entry was found. Negative return values * indicate errors. */
static int btt_log_read(struct arena_info *arena, u32 lane, struct log_entry *ent, int old_flag) { int ret; int old_ent, ret_ent; struct log_entry log[2]; ret = btt_log_read_pair(arena, lane, log); if (ret) return -EIO; old_ent = btt_log_get_old(log); if (old_ent < 0 || old_ent > 1) { dev_err(to_dev(arena), "log corruption (%d): lane %d seq [%d, %d]\n", old_ent, lane, log[0].seq, log[1].seq); /* TODO set error state? */ return -EIO; } ret_ent = (old_flag ? old_ent : (1 - old_ent)); if (ent != NULL) memcpy(ent, &log[ret_ent], LOG_ENT_SIZE); return ret_ent; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma144100.00%2100.00%
Total144100.00%2100.00%

/* * This function commits a log entry to media * It does _not_ prepare the freelist entry for the next write * btt_flog_write is the wrapper for updating the freelist elements */
static int __btt_log_write(struct arena_info *arena, u32 lane, u32 sub, struct log_entry *ent, unsigned long flags) { int ret; /* * Ignore the padding in log_entry for calculating log_half. * The entry is 'committed' when we write the sequence number, * and we want to ensure that that is the last thing written. * We don't bother writing the padding as that would be extra * media wear and write amplification */ unsigned int log_half = (LOG_ENT_SIZE - 2 * sizeof(u64)) / 2; u64 ns_off = arena->logoff + (((2 * lane) + sub) * LOG_ENT_SIZE); void *src = ent; /* split the 16B write into atomic, durable halves */ ret = arena_write_bytes(arena, ns_off, src, log_half, flags); if (ret) return ret; ns_off += log_half; src += log_half; return arena_write_bytes(arena, ns_off, src, log_half, flags); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma119100.00%2100.00%
Total119100.00%2100.00%


static int btt_flog_write(struct arena_info *arena, u32 lane, u32 sub, struct log_entry *ent) { int ret; ret = __btt_log_write(arena, lane, sub, ent, NVDIMM_IO_ATOMIC); if (ret) return ret; /* prepare the next free entry */ arena->freelist[lane].sub = 1 - arena->freelist[lane].sub; if (++(arena->freelist[lane].seq) == 4) arena->freelist[lane].seq = 1; if (ent_e_flag(ent->old_map)) arena->freelist[lane].has_err = 1; arena->freelist[lane].block = le32_to_cpu(ent_lba(ent->old_map)); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma137100.00%3100.00%
Total137100.00%3100.00%

/* * This function initializes the BTT map to the initial state, which is * all-zeroes, and indicates an identity mapping */
static int btt_map_init(struct arena_info *arena) { int ret = -EINVAL; void *zerobuf; size_t offset = 0; size_t chunk_size = SZ_2M; size_t mapsize = arena->logoff - arena->mapoff; zerobuf = kzalloc(chunk_size, GFP_KERNEL); if (!zerobuf) return -ENOMEM; /* * mapoff should always be at least 512B aligned. We rely on that to * make sure rw_bytes does error clearing correctly, so make sure that * is the case. */ dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->mapoff, 512), "arena->mapoff: %#llx is unaligned\n", arena->mapoff); while (mapsize) { size_t size = min(mapsize, chunk_size); dev_WARN_ONCE(to_dev(arena), size < 512, "chunk size: %#zx is unaligned\n", size); ret = arena_write_bytes(arena, arena->mapoff + offset, zerobuf, size, 0); if (ret) goto free; offset += size; mapsize -= size; cond_resched(); } free: kfree(zerobuf); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma16399.39%480.00%
Randy Dunlap10.61%120.00%
Total164100.00%5100.00%

/* * This function initializes the BTT log with 'fake' entries pointing * to the initial reserved set of blocks as being free */
static int btt_log_init(struct arena_info *arena) { size_t logsize = arena->info2off - arena->logoff; size_t chunk_size = SZ_4K, offset = 0; struct log_entry log; void *zerobuf; int ret; u32 i; zerobuf = kzalloc(chunk_size, GFP_KERNEL); if (!zerobuf) return -ENOMEM; /* * logoff should always be at least 512B aligned. We rely on that to * make sure rw_bytes does error clearing correctly, so make sure that * is the case. */ dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->logoff, 512), "arena->logoff: %#llx is unaligned\n", arena->logoff); while (logsize) { size_t size = min(logsize, chunk_size); dev_WARN_ONCE(to_dev(arena), size < 512, "chunk size: %#zx is unaligned\n", size); ret = arena_write_bytes(arena, arena->logoff + offset, zerobuf, size, 0); if (ret) goto free; offset += size; logsize -= size; cond_resched(); } for (i = 0; i < arena->nfree; i++) { log.lba = cpu_to_le32(i); log.old_map = cpu_to_le32(arena->external_nlba + i); log.new_map = cpu_to_le32(arena->external_nlba + i); log.seq = cpu_to_le32(LOG_SEQ_INIT); ret = __btt_log_write(arena, i, 0, &log, 0); if (ret) goto free; } free: kfree(zerobuf); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma25099.60%480.00%
Randy Dunlap10.40%120.00%
Total251100.00%5100.00%


static u64 to_namespace_offset(struct arena_info *arena, u64 lba) { return arena->dataoff + ((u64)lba * arena->internal_lbasize); }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma30100.00%2100.00%
Total30100.00%2100.00%


static int arena_clear_freelist_error(struct arena_info *arena, u32 lane) { int ret = 0; if (arena->freelist[lane].has_err) { void *zero_page = page_address(ZERO_PAGE(0)); u32 lba = arena->freelist[lane].block; u64 nsoff = to_namespace_offset(arena, lba); unsigned long len = arena->sector_size; mutex_lock(&arena->err_lock); while (len) { unsigned long chunk = min(len, PAGE_SIZE); ret = arena_write_bytes(arena, nsoff, zero_page, chunk, 0); if (ret) break; len -= chunk; nsoff += chunk; if (len == 0) arena->freelist[lane].has_err = 0; } mutex_unlock(&arena->err_lock); } return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma155100.00%1100.00%
Total155100.00%1100.00%


static int btt_freelist_init(struct arena_info *arena) { int old, new, ret; u32 i, map_entry; struct log_entry log_new, log_old; arena->freelist = kcalloc(arena->nfree, sizeof(struct free_entry), GFP_KERNEL); if (!arena->freelist) return -ENOMEM; for (i = 0; i < arena->nfree; i++) { old = btt_log_read(arena, i, &log_old, LOG_OLD_ENT); if (old < 0) return old; new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT); if (new < 0) return new; /* sub points to the next one to be overwritten */ arena->freelist[i].sub = 1 - new; arena->freelist[i].seq = nd_inc_seq(le32_to_cpu(log_new.seq)); arena->freelist[i].block = le32_to_cpu(log_new.old_map); /* * FIXME: if error clearing fails during init, we want to make * the BTT read-only */ if (ent_e_flag(log_new.old_map)) { ret = arena_clear_freelist_error(arena, i); if (ret) dev_err_ratelimited(to_dev(arena), "Unable to clear known errors\n"); } /* This implies a newly created or untouched flog entry */ if (log_new.old_map == log_new.new_map) continue; /* Check if map recovery is needed */ ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry, NULL, NULL, 0); if (ret) return ret; if ((le32_to_cpu(log_new.new_map) != map_entry) && (le32_to_cpu(log_new.old_map) == map_entry)) { /* * Last transaction wrote the flog, but wasn't able * to complete the map write. So fix up the map. */ ret = btt_map_write(arena, le32_to_cpu(log_new.lba), le32_to_cpu(log_new.new_map), 0, 0, 0); if (ret) return ret; } } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma313100.00%4100.00%
Total313100.00%4100.00%


static int btt_rtt_init(struct arena_info *arena) { arena->rtt = kcalloc(arena->nfree, sizeof(u32), GFP_KERNEL); if (arena->rtt == NULL) return -ENOMEM; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma44100.00%1100.00%
Total44100.00%1100.00%


static int btt_maplocks_init(struct arena_info *arena) { u32 i; arena->map_locks = kcalloc(arena->nfree, sizeof(struct aligned_lock), GFP_KERNEL); if (!arena->map_locks) return -ENOMEM; for (i = 0; i < arena->nfree; i++) spin_lock_init(&arena->map_locks[i].lock); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma75100.00%1100.00%
Total75100.00%1100.00%


static struct arena_info *alloc_arena(struct btt *btt, size_t size, size_t start, size_t arena_off) { struct arena_info *arena; u64 logsize, mapsize, datasize; u64 available = size; arena = kzalloc(sizeof(struct arena_info), GFP_KERNEL); if (!arena) return NULL; arena->nd_btt = btt->nd_btt; arena->sector_size = btt->sector_size; if (!size) return arena; arena->size = size; arena->external_lba_start = start; arena->external_lbasize = btt->lbasize; arena->internal_lbasize = roundup(arena->external_lbasize, INT_LBASIZE_ALIGNMENT); arena->nfree = BTT_DEFAULT_NFREE; arena->version_major = btt->nd_btt->version_major; arena->version_minor = btt->nd_btt->version_minor; if (available % BTT_PG_SIZE) available -= (available % BTT_PG_SIZE); /* Two pages are reserved for the super block and its copy */ available -= 2 * BTT_PG_SIZE; /* The log takes a fixed amount of space based on nfree */ logsize = roundup(2 * arena->nfree * sizeof(struct log_entry), BTT_PG_SIZE); available -= logsize; /* Calculate optimal split between map and data area */ arena->internal_nlba = div_u64(available - BTT_PG_SIZE, arena->internal_lbasize + MAP_ENT_SIZE); arena->external_nlba = arena->internal_nlba - arena->nfree; mapsize = roundup((arena->external_nlba * MAP_ENT_SIZE), BTT_PG_SIZE); datasize = available - mapsize; /* 'Absolute' values, relative to start of storage space */ arena->infooff = arena_off; arena->dataoff = arena->infooff + BTT_PG_SIZE; arena->mapoff = arena->dataoff + datasize; arena->logoff = arena->mapoff + mapsize; arena->info2off = arena->logoff + logsize; return arena; }

Contributors

PersonTokensPropCommitsCommitProp
Vishal Verma289100.00%3100.00%
Total289100.00%3100.00%


static void free_arenas(struct btt *