15212e11fSVishal Verma /* 25212e11fSVishal Verma * Block Translation Table 35212e11fSVishal Verma * Copyright (c) 2014-2015, Intel Corporation. 45212e11fSVishal Verma * 55212e11fSVishal Verma * This program is free software; you can redistribute it and/or modify it 65212e11fSVishal Verma * under the terms and conditions of the GNU General Public License, 75212e11fSVishal Verma * version 2, as published by the Free Software Foundation. 85212e11fSVishal Verma * 95212e11fSVishal Verma * This program is distributed in the hope it will be useful, but WITHOUT 105212e11fSVishal Verma * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 115212e11fSVishal Verma * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 125212e11fSVishal Verma * more details. 135212e11fSVishal Verma */ 145212e11fSVishal Verma #include <linux/highmem.h> 155212e11fSVishal Verma #include <linux/debugfs.h> 165212e11fSVishal Verma #include <linux/blkdev.h> 175212e11fSVishal Verma #include <linux/module.h> 185212e11fSVishal Verma #include <linux/device.h> 195212e11fSVishal Verma #include <linux/mutex.h> 205212e11fSVishal Verma #include <linux/hdreg.h> 215212e11fSVishal Verma #include <linux/genhd.h> 225212e11fSVishal Verma #include <linux/sizes.h> 235212e11fSVishal Verma #include <linux/ndctl.h> 245212e11fSVishal Verma #include <linux/fs.h> 255212e11fSVishal Verma #include <linux/nd.h> 265212e11fSVishal Verma #include "btt.h" 275212e11fSVishal Verma #include "nd.h" 285212e11fSVishal Verma 295212e11fSVishal Verma enum log_ent_request { 305212e11fSVishal Verma LOG_NEW_ENT = 0, 315212e11fSVishal Verma LOG_OLD_ENT 325212e11fSVishal Verma }; 335212e11fSVishal Verma 34*86652d2eSVishal Verma static struct device *to_dev(struct arena_info *arena) 35*86652d2eSVishal Verma { 36*86652d2eSVishal Verma return &arena->nd_btt->dev; 37*86652d2eSVishal Verma } 38*86652d2eSVishal Verma 39d9b83c75SVishal Verma static u64 adjust_initial_offset(struct nd_btt *nd_btt, u64 offset) 40d9b83c75SVishal Verma { 41d9b83c75SVishal Verma return offset + nd_btt->initial_offset; 42d9b83c75SVishal Verma } 43d9b83c75SVishal Verma 445212e11fSVishal Verma static int arena_read_bytes(struct arena_info *arena, resource_size_t offset, 453ae3d67bSVishal Verma void *buf, size_t n, unsigned long flags) 465212e11fSVishal Verma { 475212e11fSVishal Verma struct nd_btt *nd_btt = arena->nd_btt; 485212e11fSVishal Verma struct nd_namespace_common *ndns = nd_btt->ndns; 495212e11fSVishal Verma 5014e49454SVishal Verma /* arena offsets may be shifted from the base of the device */ 51d9b83c75SVishal Verma offset = adjust_initial_offset(nd_btt, offset); 523ae3d67bSVishal Verma return nvdimm_read_bytes(ndns, offset, buf, n, flags); 535212e11fSVishal Verma } 545212e11fSVishal Verma 555212e11fSVishal Verma static int arena_write_bytes(struct arena_info *arena, resource_size_t offset, 563ae3d67bSVishal Verma void *buf, size_t n, unsigned long flags) 575212e11fSVishal Verma { 585212e11fSVishal Verma struct nd_btt *nd_btt = arena->nd_btt; 595212e11fSVishal Verma struct nd_namespace_common *ndns = nd_btt->ndns; 605212e11fSVishal Verma 6114e49454SVishal Verma /* arena offsets may be shifted from the base of the device */ 62d9b83c75SVishal Verma offset = adjust_initial_offset(nd_btt, offset); 633ae3d67bSVishal Verma return nvdimm_write_bytes(ndns, offset, buf, n, flags); 645212e11fSVishal Verma } 655212e11fSVishal Verma 665212e11fSVishal Verma static int btt_info_write(struct arena_info *arena, struct btt_sb *super) 675212e11fSVishal Verma { 685212e11fSVishal Verma int ret; 695212e11fSVishal Verma 70b177fe85SVishal Verma /* 71b177fe85SVishal Verma * infooff and info2off should always be at least 512B aligned. 72b177fe85SVishal Verma * We rely on that to make sure rw_bytes does error clearing 73b177fe85SVishal Verma * correctly, so make sure that is the case. 74b177fe85SVishal Verma */ 75*86652d2eSVishal Verma dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->infooff, 512), 76*86652d2eSVishal Verma "arena->infooff: %#llx is unaligned\n", arena->infooff); 77*86652d2eSVishal Verma dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->info2off, 512), 78*86652d2eSVishal Verma "arena->info2off: %#llx is unaligned\n", arena->info2off); 79b177fe85SVishal Verma 805212e11fSVishal Verma ret = arena_write_bytes(arena, arena->info2off, super, 813ae3d67bSVishal Verma sizeof(struct btt_sb), 0); 825212e11fSVishal Verma if (ret) 835212e11fSVishal Verma return ret; 845212e11fSVishal Verma 855212e11fSVishal Verma return arena_write_bytes(arena, arena->infooff, super, 863ae3d67bSVishal Verma sizeof(struct btt_sb), 0); 875212e11fSVishal Verma } 885212e11fSVishal Verma 895212e11fSVishal Verma static int btt_info_read(struct arena_info *arena, struct btt_sb *super) 905212e11fSVishal Verma { 915212e11fSVishal Verma return arena_read_bytes(arena, arena->infooff, super, 923ae3d67bSVishal Verma sizeof(struct btt_sb), 0); 935212e11fSVishal Verma } 945212e11fSVishal Verma 955212e11fSVishal Verma /* 965212e11fSVishal Verma * 'raw' version of btt_map write 975212e11fSVishal Verma * Assumptions: 985212e11fSVishal Verma * mapping is in little-endian 995212e11fSVishal Verma * mapping contains 'E' and 'Z' flags as desired 1005212e11fSVishal Verma */ 1013ae3d67bSVishal Verma static int __btt_map_write(struct arena_info *arena, u32 lba, __le32 mapping, 1023ae3d67bSVishal Verma unsigned long flags) 1035212e11fSVishal Verma { 1045212e11fSVishal Verma u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE); 1055212e11fSVishal Verma 106*86652d2eSVishal Verma if (unlikely(lba >= arena->external_nlba)) 107*86652d2eSVishal Verma dev_err_ratelimited(to_dev(arena), 108*86652d2eSVishal Verma "%s: lba %#x out of range (max: %#x)\n", 109*86652d2eSVishal Verma __func__, lba, arena->external_nlba); 1103ae3d67bSVishal Verma return arena_write_bytes(arena, ns_off, &mapping, MAP_ENT_SIZE, flags); 1115212e11fSVishal Verma } 1125212e11fSVishal Verma 1135212e11fSVishal Verma static int btt_map_write(struct arena_info *arena, u32 lba, u32 mapping, 1143ae3d67bSVishal Verma u32 z_flag, u32 e_flag, unsigned long rwb_flags) 1155212e11fSVishal Verma { 1165212e11fSVishal Verma u32 ze; 1175212e11fSVishal Verma __le32 mapping_le; 1185212e11fSVishal Verma 1195212e11fSVishal Verma /* 1205212e11fSVishal Verma * This 'mapping' is supposed to be just the LBA mapping, without 1215212e11fSVishal Verma * any flags set, so strip the flag bits. 1225212e11fSVishal Verma */ 1230595d539SVishal Verma mapping = ent_lba(mapping); 1245212e11fSVishal Verma 1255212e11fSVishal Verma ze = (z_flag << 1) + e_flag; 1265212e11fSVishal Verma switch (ze) { 1275212e11fSVishal Verma case 0: 1285212e11fSVishal Verma /* 1295212e11fSVishal Verma * We want to set neither of the Z or E flags, and 1305212e11fSVishal Verma * in the actual layout, this means setting the bit 1315212e11fSVishal Verma * positions of both to '1' to indicate a 'normal' 1325212e11fSVishal Verma * map entry 1335212e11fSVishal Verma */ 1345212e11fSVishal Verma mapping |= MAP_ENT_NORMAL; 1355212e11fSVishal Verma break; 1365212e11fSVishal Verma case 1: 1375212e11fSVishal Verma mapping |= (1 << MAP_ERR_SHIFT); 1385212e11fSVishal Verma break; 1395212e11fSVishal Verma case 2: 1405212e11fSVishal Verma mapping |= (1 << MAP_TRIM_SHIFT); 1415212e11fSVishal Verma break; 1425212e11fSVishal Verma default: 1435212e11fSVishal Verma /* 1445212e11fSVishal Verma * The case where Z and E are both sent in as '1' could be 1455212e11fSVishal Verma * construed as a valid 'normal' case, but we decide not to, 1465212e11fSVishal Verma * to avoid confusion 1475212e11fSVishal Verma */ 148*86652d2eSVishal Verma dev_err_ratelimited(to_dev(arena), 149*86652d2eSVishal Verma "Invalid use of Z and E flags\n"); 1505212e11fSVishal Verma return -EIO; 1515212e11fSVishal Verma } 1525212e11fSVishal Verma 1535212e11fSVishal Verma mapping_le = cpu_to_le32(mapping); 1543ae3d67bSVishal Verma return __btt_map_write(arena, lba, mapping_le, rwb_flags); 1555212e11fSVishal Verma } 1565212e11fSVishal Verma 1575212e11fSVishal Verma static int btt_map_read(struct arena_info *arena, u32 lba, u32 *mapping, 1583ae3d67bSVishal Verma int *trim, int *error, unsigned long rwb_flags) 1595212e11fSVishal Verma { 1605212e11fSVishal Verma int ret; 1615212e11fSVishal Verma __le32 in; 1625212e11fSVishal Verma u32 raw_mapping, postmap, ze, z_flag, e_flag; 1635212e11fSVishal Verma u64 ns_off = arena->mapoff + (lba * MAP_ENT_SIZE); 1645212e11fSVishal Verma 165*86652d2eSVishal Verma if (unlikely(lba >= arena->external_nlba)) 166*86652d2eSVishal Verma dev_err_ratelimited(to_dev(arena), 167*86652d2eSVishal Verma "%s: lba %#x out of range (max: %#x)\n", 168*86652d2eSVishal Verma __func__, lba, arena->external_nlba); 1695212e11fSVishal Verma 1703ae3d67bSVishal Verma ret = arena_read_bytes(arena, ns_off, &in, MAP_ENT_SIZE, rwb_flags); 1715212e11fSVishal Verma if (ret) 1725212e11fSVishal Verma return ret; 1735212e11fSVishal Verma 1745212e11fSVishal Verma raw_mapping = le32_to_cpu(in); 1755212e11fSVishal Verma 1760595d539SVishal Verma z_flag = ent_z_flag(raw_mapping); 1770595d539SVishal Verma e_flag = ent_e_flag(raw_mapping); 1785212e11fSVishal Verma ze = (z_flag << 1) + e_flag; 1790595d539SVishal Verma postmap = ent_lba(raw_mapping); 1805212e11fSVishal Verma 1815212e11fSVishal Verma /* Reuse the {z,e}_flag variables for *trim and *error */ 1825212e11fSVishal Verma z_flag = 0; 1835212e11fSVishal Verma e_flag = 0; 1845212e11fSVishal Verma 1855212e11fSVishal Verma switch (ze) { 1865212e11fSVishal Verma case 0: 1875212e11fSVishal Verma /* Initial state. Return postmap = premap */ 1885212e11fSVishal Verma *mapping = lba; 1895212e11fSVishal Verma break; 1905212e11fSVishal Verma case 1: 1915212e11fSVishal Verma *mapping = postmap; 1925212e11fSVishal Verma e_flag = 1; 1935212e11fSVishal Verma break; 1945212e11fSVishal Verma case 2: 1955212e11fSVishal Verma *mapping = postmap; 1965212e11fSVishal Verma z_flag = 1; 1975212e11fSVishal Verma break; 1985212e11fSVishal Verma case 3: 1995212e11fSVishal Verma *mapping = postmap; 2005212e11fSVishal Verma break; 2015212e11fSVishal Verma default: 2025212e11fSVishal Verma return -EIO; 2035212e11fSVishal Verma } 2045212e11fSVishal Verma 2055212e11fSVishal Verma if (trim) 2065212e11fSVishal Verma *trim = z_flag; 2075212e11fSVishal Verma if (error) 2085212e11fSVishal Verma *error = e_flag; 2095212e11fSVishal Verma 2105212e11fSVishal Verma return ret; 2115212e11fSVishal Verma } 2125212e11fSVishal Verma 2135212e11fSVishal Verma static int btt_log_read_pair(struct arena_info *arena, u32 lane, 2145212e11fSVishal Verma struct log_entry *ent) 2155212e11fSVishal Verma { 2165212e11fSVishal Verma return arena_read_bytes(arena, 2175212e11fSVishal Verma arena->logoff + (2 * lane * LOG_ENT_SIZE), ent, 2183ae3d67bSVishal Verma 2 * LOG_ENT_SIZE, 0); 2195212e11fSVishal Verma } 2205212e11fSVishal Verma 2215212e11fSVishal Verma static struct dentry *debugfs_root; 2225212e11fSVishal Verma 2235212e11fSVishal Verma static void arena_debugfs_init(struct arena_info *a, struct dentry *parent, 2245212e11fSVishal Verma int idx) 2255212e11fSVishal Verma { 2265212e11fSVishal Verma char dirname[32]; 2275212e11fSVishal Verma struct dentry *d; 2285212e11fSVishal Verma 2295212e11fSVishal Verma /* If for some reason, parent bttN was not created, exit */ 2305212e11fSVishal Verma if (!parent) 2315212e11fSVishal Verma return; 2325212e11fSVishal Verma 2335212e11fSVishal Verma snprintf(dirname, 32, "arena%d", idx); 2345212e11fSVishal Verma d = debugfs_create_dir(dirname, parent); 2355212e11fSVishal Verma if (IS_ERR_OR_NULL(d)) 2365212e11fSVishal Verma return; 2375212e11fSVishal Verma a->debugfs_dir = d; 2385212e11fSVishal Verma 2395212e11fSVishal Verma debugfs_create_x64("size", S_IRUGO, d, &a->size); 2405212e11fSVishal Verma debugfs_create_x64("external_lba_start", S_IRUGO, d, 2415212e11fSVishal Verma &a->external_lba_start); 2425212e11fSVishal Verma debugfs_create_x32("internal_nlba", S_IRUGO, d, &a->internal_nlba); 2435212e11fSVishal Verma debugfs_create_u32("internal_lbasize", S_IRUGO, d, 2445212e11fSVishal Verma &a->internal_lbasize); 2455212e11fSVishal Verma debugfs_create_x32("external_nlba", S_IRUGO, d, &a->external_nlba); 2465212e11fSVishal Verma debugfs_create_u32("external_lbasize", S_IRUGO, d, 2475212e11fSVishal Verma &a->external_lbasize); 2485212e11fSVishal Verma debugfs_create_u32("nfree", S_IRUGO, d, &a->nfree); 2495212e11fSVishal Verma debugfs_create_u16("version_major", S_IRUGO, d, &a->version_major); 2505212e11fSVishal Verma debugfs_create_u16("version_minor", S_IRUGO, d, &a->version_minor); 2515212e11fSVishal Verma debugfs_create_x64("nextoff", S_IRUGO, d, &a->nextoff); 2525212e11fSVishal Verma debugfs_create_x64("infooff", S_IRUGO, d, &a->infooff); 2535212e11fSVishal Verma debugfs_create_x64("dataoff", S_IRUGO, d, &a->dataoff); 2545212e11fSVishal Verma debugfs_create_x64("mapoff", S_IRUGO, d, &a->mapoff); 2555212e11fSVishal Verma debugfs_create_x64("logoff", S_IRUGO, d, &a->logoff); 2565212e11fSVishal Verma debugfs_create_x64("info2off", S_IRUGO, d, &a->info2off); 2575212e11fSVishal Verma debugfs_create_x32("flags", S_IRUGO, d, &a->flags); 2585212e11fSVishal Verma } 2595212e11fSVishal Verma 2605212e11fSVishal Verma static void btt_debugfs_init(struct btt *btt) 2615212e11fSVishal Verma { 2625212e11fSVishal Verma int i = 0; 2635212e11fSVishal Verma struct arena_info *arena; 2645212e11fSVishal Verma 2655212e11fSVishal Verma btt->debugfs_dir = debugfs_create_dir(dev_name(&btt->nd_btt->dev), 2665212e11fSVishal Verma debugfs_root); 2675212e11fSVishal Verma if (IS_ERR_OR_NULL(btt->debugfs_dir)) 2685212e11fSVishal Verma return; 2695212e11fSVishal Verma 2705212e11fSVishal Verma list_for_each_entry(arena, &btt->arena_list, list) { 2715212e11fSVishal Verma arena_debugfs_init(arena, btt->debugfs_dir, i); 2725212e11fSVishal Verma i++; 2735212e11fSVishal Verma } 2745212e11fSVishal Verma } 2755212e11fSVishal Verma 2765212e11fSVishal Verma /* 2775212e11fSVishal Verma * This function accepts two log entries, and uses the 2785212e11fSVishal Verma * sequence number to find the 'older' entry. 2795212e11fSVishal Verma * It also updates the sequence number in this old entry to 2805212e11fSVishal Verma * make it the 'new' one if the mark_flag is set. 2815212e11fSVishal Verma * Finally, it returns which of the entries was the older one. 2825212e11fSVishal Verma * 2835212e11fSVishal Verma * TODO The logic feels a bit kludge-y. make it better.. 2845212e11fSVishal Verma */ 2855212e11fSVishal Verma static int btt_log_get_old(struct log_entry *ent) 2865212e11fSVishal Verma { 2875212e11fSVishal Verma int old; 2885212e11fSVishal Verma 2895212e11fSVishal Verma /* 2905212e11fSVishal Verma * the first ever time this is seen, the entry goes into [0] 2915212e11fSVishal Verma * the next time, the following logic works out to put this 2925212e11fSVishal Verma * (next) entry into [1] 2935212e11fSVishal Verma */ 2945212e11fSVishal Verma if (ent[0].seq == 0) { 2955212e11fSVishal Verma ent[0].seq = cpu_to_le32(1); 2965212e11fSVishal Verma return 0; 2975212e11fSVishal Verma } 2985212e11fSVishal Verma 2995212e11fSVishal Verma if (ent[0].seq == ent[1].seq) 3005212e11fSVishal Verma return -EINVAL; 3015212e11fSVishal Verma if (le32_to_cpu(ent[0].seq) + le32_to_cpu(ent[1].seq) > 5) 3025212e11fSVishal Verma return -EINVAL; 3035212e11fSVishal Verma 3045212e11fSVishal Verma if (le32_to_cpu(ent[0].seq) < le32_to_cpu(ent[1].seq)) { 3055212e11fSVishal Verma if (le32_to_cpu(ent[1].seq) - le32_to_cpu(ent[0].seq) == 1) 3065212e11fSVishal Verma old = 0; 3075212e11fSVishal Verma else 3085212e11fSVishal Verma old = 1; 3095212e11fSVishal Verma } else { 3105212e11fSVishal Verma if (le32_to_cpu(ent[0].seq) - le32_to_cpu(ent[1].seq) == 1) 3115212e11fSVishal Verma old = 1; 3125212e11fSVishal Verma else 3135212e11fSVishal Verma old = 0; 3145212e11fSVishal Verma } 3155212e11fSVishal Verma 3165212e11fSVishal Verma return old; 3175212e11fSVishal Verma } 3185212e11fSVishal Verma 3195212e11fSVishal Verma /* 3205212e11fSVishal Verma * This function copies the desired (old/new) log entry into ent if 3215212e11fSVishal Verma * it is not NULL. It returns the sub-slot number (0 or 1) 3225212e11fSVishal Verma * where the desired log entry was found. Negative return values 3235212e11fSVishal Verma * indicate errors. 3245212e11fSVishal Verma */ 3255212e11fSVishal Verma static int btt_log_read(struct arena_info *arena, u32 lane, 3265212e11fSVishal Verma struct log_entry *ent, int old_flag) 3275212e11fSVishal Verma { 3285212e11fSVishal Verma int ret; 3295212e11fSVishal Verma int old_ent, ret_ent; 3305212e11fSVishal Verma struct log_entry log[2]; 3315212e11fSVishal Verma 3325212e11fSVishal Verma ret = btt_log_read_pair(arena, lane, log); 3335212e11fSVishal Verma if (ret) 3345212e11fSVishal Verma return -EIO; 3355212e11fSVishal Verma 3365212e11fSVishal Verma old_ent = btt_log_get_old(log); 3375212e11fSVishal Verma if (old_ent < 0 || old_ent > 1) { 338e6be2dcbSVishal Verma dev_err(to_dev(arena), 3395212e11fSVishal Verma "log corruption (%d): lane %d seq [%d, %d]\n", 3405212e11fSVishal Verma old_ent, lane, log[0].seq, log[1].seq); 3415212e11fSVishal Verma /* TODO set error state? */ 3425212e11fSVishal Verma return -EIO; 3435212e11fSVishal Verma } 3445212e11fSVishal Verma 3455212e11fSVishal Verma ret_ent = (old_flag ? old_ent : (1 - old_ent)); 3465212e11fSVishal Verma 3475212e11fSVishal Verma if (ent != NULL) 3485212e11fSVishal Verma memcpy(ent, &log[ret_ent], LOG_ENT_SIZE); 3495212e11fSVishal Verma 3505212e11fSVishal Verma return ret_ent; 3515212e11fSVishal Verma } 3525212e11fSVishal Verma 3535212e11fSVishal Verma /* 3545212e11fSVishal Verma * This function commits a log entry to media 3555212e11fSVishal Verma * It does _not_ prepare the freelist entry for the next write 3565212e11fSVishal Verma * btt_flog_write is the wrapper for updating the freelist elements 3575212e11fSVishal Verma */ 3585212e11fSVishal Verma static int __btt_log_write(struct arena_info *arena, u32 lane, 3593ae3d67bSVishal Verma u32 sub, struct log_entry *ent, unsigned long flags) 3605212e11fSVishal Verma { 3615212e11fSVishal Verma int ret; 3625212e11fSVishal Verma /* 3635212e11fSVishal Verma * Ignore the padding in log_entry for calculating log_half. 3645212e11fSVishal Verma * The entry is 'committed' when we write the sequence number, 3655212e11fSVishal Verma * and we want to ensure that that is the last thing written. 3665212e11fSVishal Verma * We don't bother writing the padding as that would be extra 3675212e11fSVishal Verma * media wear and write amplification 3685212e11fSVishal Verma */ 3695212e11fSVishal Verma unsigned int log_half = (LOG_ENT_SIZE - 2 * sizeof(u64)) / 2; 3705212e11fSVishal Verma u64 ns_off = arena->logoff + (((2 * lane) + sub) * LOG_ENT_SIZE); 3715212e11fSVishal Verma void *src = ent; 3725212e11fSVishal Verma 3735212e11fSVishal Verma /* split the 16B write into atomic, durable halves */ 3743ae3d67bSVishal Verma ret = arena_write_bytes(arena, ns_off, src, log_half, flags); 3755212e11fSVishal Verma if (ret) 3765212e11fSVishal Verma return ret; 3775212e11fSVishal Verma 3785212e11fSVishal Verma ns_off += log_half; 3795212e11fSVishal Verma src += log_half; 3803ae3d67bSVishal Verma return arena_write_bytes(arena, ns_off, src, log_half, flags); 3815212e11fSVishal Verma } 3825212e11fSVishal Verma 3835212e11fSVishal Verma static int btt_flog_write(struct arena_info *arena, u32 lane, u32 sub, 3845212e11fSVishal Verma struct log_entry *ent) 3855212e11fSVishal Verma { 3865212e11fSVishal Verma int ret; 3875212e11fSVishal Verma 3883ae3d67bSVishal Verma ret = __btt_log_write(arena, lane, sub, ent, NVDIMM_IO_ATOMIC); 3895212e11fSVishal Verma if (ret) 3905212e11fSVishal Verma return ret; 3915212e11fSVishal Verma 3925212e11fSVishal Verma /* prepare the next free entry */ 3935212e11fSVishal Verma arena->freelist[lane].sub = 1 - arena->freelist[lane].sub; 3945212e11fSVishal Verma if (++(arena->freelist[lane].seq) == 4) 3955212e11fSVishal Verma arena->freelist[lane].seq = 1; 396d9b83c75SVishal Verma if (ent_e_flag(ent->old_map)) 397d9b83c75SVishal Verma arena->freelist[lane].has_err = 1; 398d9b83c75SVishal Verma arena->freelist[lane].block = le32_to_cpu(ent_lba(ent->old_map)); 3995212e11fSVishal Verma 4005212e11fSVishal Verma return ret; 4015212e11fSVishal Verma } 4025212e11fSVishal Verma 4035212e11fSVishal Verma /* 4045212e11fSVishal Verma * This function initializes the BTT map to the initial state, which is 4055212e11fSVishal Verma * all-zeroes, and indicates an identity mapping 4065212e11fSVishal Verma */ 4075212e11fSVishal Verma static int btt_map_init(struct arena_info *arena) 4085212e11fSVishal Verma { 4095212e11fSVishal Verma int ret = -EINVAL; 4105212e11fSVishal Verma void *zerobuf; 4115212e11fSVishal Verma size_t offset = 0; 4125212e11fSVishal Verma size_t chunk_size = SZ_2M; 4135212e11fSVishal Verma size_t mapsize = arena->logoff - arena->mapoff; 4145212e11fSVishal Verma 4155212e11fSVishal Verma zerobuf = kzalloc(chunk_size, GFP_KERNEL); 4165212e11fSVishal Verma if (!zerobuf) 4175212e11fSVishal Verma return -ENOMEM; 4185212e11fSVishal Verma 419b177fe85SVishal Verma /* 420b177fe85SVishal Verma * mapoff should always be at least 512B aligned. We rely on that to 421b177fe85SVishal Verma * make sure rw_bytes does error clearing correctly, so make sure that 422b177fe85SVishal Verma * is the case. 423b177fe85SVishal Verma */ 424*86652d2eSVishal Verma dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->mapoff, 512), 425*86652d2eSVishal Verma "arena->mapoff: %#llx is unaligned\n", arena->mapoff); 426b177fe85SVishal Verma 4275212e11fSVishal Verma while (mapsize) { 4285212e11fSVishal Verma size_t size = min(mapsize, chunk_size); 4295212e11fSVishal Verma 430*86652d2eSVishal Verma dev_WARN_ONCE(to_dev(arena), size < 512, 431*86652d2eSVishal Verma "chunk size: %#lx is unaligned\n", size); 4325212e11fSVishal Verma ret = arena_write_bytes(arena, arena->mapoff + offset, zerobuf, 4333ae3d67bSVishal Verma size, 0); 4345212e11fSVishal Verma if (ret) 4355212e11fSVishal Verma goto free; 4365212e11fSVishal Verma 4375212e11fSVishal Verma offset += size; 4385212e11fSVishal Verma mapsize -= size; 4395212e11fSVishal Verma cond_resched(); 4405212e11fSVishal Verma } 4415212e11fSVishal Verma 4425212e11fSVishal Verma free: 4435212e11fSVishal Verma kfree(zerobuf); 4445212e11fSVishal Verma return ret; 4455212e11fSVishal Verma } 4465212e11fSVishal Verma 4475212e11fSVishal Verma /* 4485212e11fSVishal Verma * This function initializes the BTT log with 'fake' entries pointing 4495212e11fSVishal Verma * to the initial reserved set of blocks as being free 4505212e11fSVishal Verma */ 4515212e11fSVishal Verma static int btt_log_init(struct arena_info *arena) 4525212e11fSVishal Verma { 453b177fe85SVishal Verma size_t logsize = arena->info2off - arena->logoff; 454b177fe85SVishal Verma size_t chunk_size = SZ_4K, offset = 0; 455b177fe85SVishal Verma struct log_entry log; 456b177fe85SVishal Verma void *zerobuf; 4575212e11fSVishal Verma int ret; 4585212e11fSVishal Verma u32 i; 4595212e11fSVishal Verma 460b177fe85SVishal Verma zerobuf = kzalloc(chunk_size, GFP_KERNEL); 461b177fe85SVishal Verma if (!zerobuf) 462b177fe85SVishal Verma return -ENOMEM; 463b177fe85SVishal Verma /* 464b177fe85SVishal Verma * logoff should always be at least 512B aligned. We rely on that to 465b177fe85SVishal Verma * make sure rw_bytes does error clearing correctly, so make sure that 466b177fe85SVishal Verma * is the case. 467b177fe85SVishal Verma */ 468*86652d2eSVishal Verma dev_WARN_ONCE(to_dev(arena), !IS_ALIGNED(arena->logoff, 512), 469*86652d2eSVishal Verma "arena->logoff: %#llx is unaligned\n", arena->logoff); 470b177fe85SVishal Verma 471b177fe85SVishal Verma while (logsize) { 472b177fe85SVishal Verma size_t size = min(logsize, chunk_size); 473b177fe85SVishal Verma 474*86652d2eSVishal Verma dev_WARN_ONCE(to_dev(arena), size < 512, 475*86652d2eSVishal Verma "chunk size: %#lx is unaligned\n", size); 476b177fe85SVishal Verma ret = arena_write_bytes(arena, arena->logoff + offset, zerobuf, 477b177fe85SVishal Verma size, 0); 478b177fe85SVishal Verma if (ret) 479b177fe85SVishal Verma goto free; 480b177fe85SVishal Verma 481b177fe85SVishal Verma offset += size; 482b177fe85SVishal Verma logsize -= size; 483b177fe85SVishal Verma cond_resched(); 484b177fe85SVishal Verma } 4855212e11fSVishal Verma 4865212e11fSVishal Verma for (i = 0; i < arena->nfree; i++) { 4875212e11fSVishal Verma log.lba = cpu_to_le32(i); 4885212e11fSVishal Verma log.old_map = cpu_to_le32(arena->external_nlba + i); 4895212e11fSVishal Verma log.new_map = cpu_to_le32(arena->external_nlba + i); 4905212e11fSVishal Verma log.seq = cpu_to_le32(LOG_SEQ_INIT); 4913ae3d67bSVishal Verma ret = __btt_log_write(arena, i, 0, &log, 0); 4925212e11fSVishal Verma if (ret) 493b177fe85SVishal Verma goto free; 4945212e11fSVishal Verma } 4955212e11fSVishal Verma 496b177fe85SVishal Verma free: 497b177fe85SVishal Verma kfree(zerobuf); 498b177fe85SVishal Verma return ret; 4995212e11fSVishal Verma } 5005212e11fSVishal Verma 501d9b83c75SVishal Verma static u64 to_namespace_offset(struct arena_info *arena, u64 lba) 502d9b83c75SVishal Verma { 503d9b83c75SVishal Verma return arena->dataoff + ((u64)lba * arena->internal_lbasize); 504d9b83c75SVishal Verma } 505d9b83c75SVishal Verma 506d9b83c75SVishal Verma static int arena_clear_freelist_error(struct arena_info *arena, u32 lane) 507d9b83c75SVishal Verma { 508d9b83c75SVishal Verma int ret = 0; 509d9b83c75SVishal Verma 510d9b83c75SVishal Verma if (arena->freelist[lane].has_err) { 511d9b83c75SVishal Verma void *zero_page = page_address(ZERO_PAGE(0)); 512d9b83c75SVishal Verma u32 lba = arena->freelist[lane].block; 513d9b83c75SVishal Verma u64 nsoff = to_namespace_offset(arena, lba); 514d9b83c75SVishal Verma unsigned long len = arena->sector_size; 515d9b83c75SVishal Verma 516d9b83c75SVishal Verma mutex_lock(&arena->err_lock); 517d9b83c75SVishal Verma 518d9b83c75SVishal Verma while (len) { 519d9b83c75SVishal Verma unsigned long chunk = min(len, PAGE_SIZE); 520d9b83c75SVishal Verma 521d9b83c75SVishal Verma ret = arena_write_bytes(arena, nsoff, zero_page, 522d9b83c75SVishal Verma chunk, 0); 523d9b83c75SVishal Verma if (ret) 524d9b83c75SVishal Verma break; 525d9b83c75SVishal Verma len -= chunk; 526d9b83c75SVishal Verma nsoff += chunk; 527d9b83c75SVishal Verma if (len == 0) 528d9b83c75SVishal Verma arena->freelist[lane].has_err = 0; 529d9b83c75SVishal Verma } 530d9b83c75SVishal Verma mutex_unlock(&arena->err_lock); 531d9b83c75SVishal Verma } 532d9b83c75SVishal Verma return ret; 533d9b83c75SVishal Verma } 534d9b83c75SVishal Verma 5355212e11fSVishal Verma static int btt_freelist_init(struct arena_info *arena) 5365212e11fSVishal Verma { 5375212e11fSVishal Verma int old, new, ret; 5385212e11fSVishal Verma u32 i, map_entry; 5395212e11fSVishal Verma struct log_entry log_new, log_old; 5405212e11fSVishal Verma 5415212e11fSVishal Verma arena->freelist = kcalloc(arena->nfree, sizeof(struct free_entry), 5425212e11fSVishal Verma GFP_KERNEL); 5435212e11fSVishal Verma if (!arena->freelist) 5445212e11fSVishal Verma return -ENOMEM; 5455212e11fSVishal Verma 5465212e11fSVishal Verma for (i = 0; i < arena->nfree; i++) { 5475212e11fSVishal Verma old = btt_log_read(arena, i, &log_old, LOG_OLD_ENT); 5485212e11fSVishal Verma if (old < 0) 5495212e11fSVishal Verma return old; 5505212e11fSVishal Verma 5515212e11fSVishal Verma new = btt_log_read(arena, i, &log_new, LOG_NEW_ENT); 5525212e11fSVishal Verma if (new < 0) 5535212e11fSVishal Verma return new; 5545212e11fSVishal Verma 5555212e11fSVishal Verma /* sub points to the next one to be overwritten */ 5565212e11fSVishal Verma arena->freelist[i].sub = 1 - new; 5575212e11fSVishal Verma arena->freelist[i].seq = nd_inc_seq(le32_to_cpu(log_new.seq)); 5585212e11fSVishal Verma arena->freelist[i].block = le32_to_cpu(log_new.old_map); 5595212e11fSVishal Verma 560d9b83c75SVishal Verma /* 561d9b83c75SVishal Verma * FIXME: if error clearing fails during init, we want to make 562d9b83c75SVishal Verma * the BTT read-only 563d9b83c75SVishal Verma */ 564d9b83c75SVishal Verma if (ent_e_flag(log_new.old_map)) { 565d9b83c75SVishal Verma ret = arena_clear_freelist_error(arena, i); 566d9b83c75SVishal Verma if (ret) 567*86652d2eSVishal Verma dev_err_ratelimited(to_dev(arena), 568*86652d2eSVishal Verma "Unable to clear known errors\n"); 569d9b83c75SVishal Verma } 570d9b83c75SVishal Verma 5715212e11fSVishal Verma /* This implies a newly created or untouched flog entry */ 5725212e11fSVishal Verma if (log_new.old_map == log_new.new_map) 5735212e11fSVishal Verma continue; 5745212e11fSVishal Verma 5755212e11fSVishal Verma /* Check if map recovery is needed */ 5765212e11fSVishal Verma ret = btt_map_read(arena, le32_to_cpu(log_new.lba), &map_entry, 5773ae3d67bSVishal Verma NULL, NULL, 0); 5785212e11fSVishal Verma if (ret) 5795212e11fSVishal Verma return ret; 5805212e11fSVishal Verma if ((le32_to_cpu(log_new.new_map) != map_entry) && 5815212e11fSVishal Verma (le32_to_cpu(log_new.old_map) == map_entry)) { 5825212e11fSVishal Verma /* 5835212e11fSVishal Verma * Last transaction wrote the flog, but wasn't able 5845212e11fSVishal Verma * to complete the map write. So fix up the map. 5855212e11fSVishal Verma */ 5865212e11fSVishal Verma ret = btt_map_write(arena, le32_to_cpu(log_new.lba), 5873ae3d67bSVishal Verma le32_to_cpu(log_new.new_map), 0, 0, 0); 5885212e11fSVishal Verma if (ret) 5895212e11fSVishal Verma return ret; 5905212e11fSVishal Verma } 5915212e11fSVishal Verma } 5925212e11fSVishal Verma 5935212e11fSVishal Verma return 0; 5945212e11fSVishal Verma } 5955212e11fSVishal Verma 5965212e11fSVishal Verma static int btt_rtt_init(struct arena_info *arena) 5975212e11fSVishal Verma { 5985212e11fSVishal Verma arena->rtt = kcalloc(arena->nfree, sizeof(u32), GFP_KERNEL); 5995212e11fSVishal Verma if (arena->rtt == NULL) 6005212e11fSVishal Verma return -ENOMEM; 6015212e11fSVishal Verma 6025212e11fSVishal Verma return 0; 6035212e11fSVishal Verma } 6045212e11fSVishal Verma 6055212e11fSVishal Verma static int btt_maplocks_init(struct arena_info *arena) 6065212e11fSVishal Verma { 6075212e11fSVishal Verma u32 i; 6085212e11fSVishal Verma 6095212e11fSVishal Verma arena->map_locks = kcalloc(arena->nfree, sizeof(struct aligned_lock), 6105212e11fSVishal Verma GFP_KERNEL); 6115212e11fSVishal Verma if (!arena->map_locks) 6125212e11fSVishal Verma return -ENOMEM; 6135212e11fSVishal Verma 6145212e11fSVishal Verma for (i = 0; i < arena->nfree; i++) 6155212e11fSVishal Verma spin_lock_init(&arena->map_locks[i].lock); 6165212e11fSVishal Verma 6175212e11fSVishal Verma return 0; 6185212e11fSVishal Verma } 6195212e11fSVishal Verma 6205212e11fSVishal Verma static struct arena_info *alloc_arena(struct btt *btt, size_t size, 6215212e11fSVishal Verma size_t start, size_t arena_off) 6225212e11fSVishal Verma { 6235212e11fSVishal Verma struct arena_info *arena; 6245212e11fSVishal Verma u64 logsize, mapsize, datasize; 6255212e11fSVishal Verma u64 available = size; 6265212e11fSVishal Verma 6275212e11fSVishal Verma arena = kzalloc(sizeof(struct arena_info), GFP_KERNEL); 6285212e11fSVishal Verma if (!arena) 6295212e11fSVishal Verma return NULL; 6305212e11fSVishal Verma arena->nd_btt = btt->nd_btt; 63175892004SVishal Verma arena->sector_size = btt->sector_size; 6325212e11fSVishal Verma 6335212e11fSVishal Verma if (!size) 6345212e11fSVishal Verma return arena; 6355212e11fSVishal Verma 6365212e11fSVishal Verma arena->size = size; 6375212e11fSVishal Verma arena->external_lba_start = start; 6385212e11fSVishal Verma arena->external_lbasize = btt->lbasize; 6395212e11fSVishal Verma arena->internal_lbasize = roundup(arena->external_lbasize, 6405212e11fSVishal Verma INT_LBASIZE_ALIGNMENT); 6415212e11fSVishal Verma arena->nfree = BTT_DEFAULT_NFREE; 64214e49454SVishal Verma arena->version_major = btt->nd_btt->version_major; 64314e49454SVishal Verma arena->version_minor = btt->nd_btt->version_minor; 6445212e11fSVishal Verma 6455212e11fSVishal Verma if (available % BTT_PG_SIZE) 6465212e11fSVishal Verma available -= (available % BTT_PG_SIZE); 6475212e11fSVishal Verma 6485212e11fSVishal Verma /* Two pages are reserved for the super block and its copy */ 6495212e11fSVishal Verma available -= 2 * BTT_PG_SIZE; 6505212e11fSVishal Verma 6515212e11fSVishal Verma /* The log takes a fixed amount of space based on nfree */ 6525212e11fSVishal Verma logsize = roundup(2 * arena->nfree * sizeof(struct log_entry), 6535212e11fSVishal Verma BTT_PG_SIZE); 6545212e11fSVishal Verma available -= logsize; 6555212e11fSVishal Verma 6565212e11fSVishal Verma /* Calculate optimal split between map and data area */ 6575212e11fSVishal Verma arena->internal_nlba = div_u64(available - BTT_PG_SIZE, 6585212e11fSVishal Verma arena->internal_lbasize + MAP_ENT_SIZE); 6595212e11fSVishal Verma arena->external_nlba = arena->internal_nlba - arena->nfree; 6605212e11fSVishal Verma 6615212e11fSVishal Verma mapsize = roundup((arena->external_nlba * MAP_ENT_SIZE), BTT_PG_SIZE); 6625212e11fSVishal Verma datasize = available - mapsize; 6635212e11fSVishal Verma 6645212e11fSVishal Verma /* 'Absolute' values, relative to start of storage space */ 6655212e11fSVishal Verma arena->infooff = arena_off; 6665212e11fSVishal Verma arena->dataoff = arena->infooff + BTT_PG_SIZE; 6675212e11fSVishal Verma arena->mapoff = arena->dataoff + datasize; 6685212e11fSVishal Verma arena->logoff = arena->mapoff + mapsize; 6695212e11fSVishal Verma arena->info2off = arena->logoff + logsize; 6705212e11fSVishal Verma return arena; 6715212e11fSVishal Verma } 6725212e11fSVishal Verma 6735212e11fSVishal Verma static void free_arenas(struct btt *btt) 6745212e11fSVishal Verma { 6755212e11fSVishal Verma struct arena_info *arena, *next; 6765212e11fSVishal Verma 6775212e11fSVishal Verma list_for_each_entry_safe(arena, next, &btt->arena_list, list) { 6785212e11fSVishal Verma list_del(&arena->list); 6795212e11fSVishal Verma kfree(arena->rtt); 6805212e11fSVishal Verma kfree(arena->map_locks); 6815212e11fSVishal Verma kfree(arena->freelist); 6825212e11fSVishal Verma debugfs_remove_recursive(arena->debugfs_dir); 6835212e11fSVishal Verma kfree(arena); 6845212e11fSVishal Verma } 6855212e11fSVishal Verma } 6865212e11fSVishal Verma 6875212e11fSVishal Verma /* 6885212e11fSVishal Verma * This function reads an existing valid btt superblock and 6895212e11fSVishal Verma * populates the corresponding arena_info struct 6905212e11fSVishal Verma */ 6915212e11fSVishal Verma static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super, 6925212e11fSVishal Verma u64 arena_off) 6935212e11fSVishal Verma { 6945212e11fSVishal Verma arena->internal_nlba = le32_to_cpu(super->internal_nlba); 6955212e11fSVishal Verma arena->internal_lbasize = le32_to_cpu(super->internal_lbasize); 6965212e11fSVishal Verma arena->external_nlba = le32_to_cpu(super->external_nlba); 6975212e11fSVishal Verma arena->external_lbasize = le32_to_cpu(super->external_lbasize); 6985212e11fSVishal Verma arena->nfree = le32_to_cpu(super->nfree); 6995212e11fSVishal Verma arena->version_major = le16_to_cpu(super->version_major); 7005212e11fSVishal Verma arena->version_minor = le16_to_cpu(super->version_minor); 7015212e11fSVishal Verma 7025212e11fSVishal Verma arena->nextoff = (super->nextoff == 0) ? 0 : (arena_off + 7035212e11fSVishal Verma le64_to_cpu(super->nextoff)); 7045212e11fSVishal Verma arena->infooff = arena_off; 7055212e11fSVishal Verma arena->dataoff = arena_off + le64_to_cpu(super->dataoff); 7065212e11fSVishal Verma arena->mapoff = arena_off + le64_to_cpu(super->mapoff); 7075212e11fSVishal Verma arena->logoff = arena_off + le64_to_cpu(super->logoff); 7085212e11fSVishal Verma arena->info2off = arena_off + le64_to_cpu(super->info2off); 7095212e11fSVishal Verma 7105e329406SDan Williams arena->size = (le64_to_cpu(super->nextoff) > 0) 7115e329406SDan Williams ? (le64_to_cpu(super->nextoff)) 7125e329406SDan Williams : (arena->info2off - arena->infooff + BTT_PG_SIZE); 7135212e11fSVishal Verma 7145212e11fSVishal Verma arena->flags = le32_to_cpu(super->flags); 7155212e11fSVishal Verma } 7165212e11fSVishal Verma 7175212e11fSVishal Verma static int discover_arenas(struct btt *btt) 7185212e11fSVishal Verma { 7195212e11fSVishal Verma int ret = 0; 7205212e11fSVishal Verma struct arena_info *arena; 7215212e11fSVishal Verma struct btt_sb *super; 7225212e11fSVishal Verma size_t remaining = btt->rawsize; 7235212e11fSVishal Verma u64 cur_nlba = 0; 7245212e11fSVishal Verma size_t cur_off = 0; 7255212e11fSVishal Verma int num_arenas = 0; 7265212e11fSVishal Verma 7275212e11fSVishal Verma super = kzalloc(sizeof(*super), GFP_KERNEL); 7285212e11fSVishal Verma if (!super) 7295212e11fSVishal Verma return -ENOMEM; 7305212e11fSVishal Verma 7315212e11fSVishal Verma while (remaining) { 7325212e11fSVishal Verma /* Alloc memory for arena */ 7335212e11fSVishal Verma arena = alloc_arena(btt, 0, 0, 0); 7345212e11fSVishal Verma if (!arena) { 7355212e11fSVishal Verma ret = -ENOMEM; 7365212e11fSVishal Verma goto out_super; 7375212e11fSVishal Verma } 7385212e11fSVishal Verma 7395212e11fSVishal Verma arena->infooff = cur_off; 7405212e11fSVishal Verma ret = btt_info_read(arena, super); 7415212e11fSVishal Verma if (ret) 7425212e11fSVishal Verma goto out; 7435212e11fSVishal Verma 744ab45e763SVishal Verma if (!nd_btt_arena_is_valid(btt->nd_btt, super)) { 7455212e11fSVishal Verma if (remaining == btt->rawsize) { 7465212e11fSVishal Verma btt->init_state = INIT_NOTFOUND; 7475212e11fSVishal Verma dev_info(to_dev(arena), "No existing arenas\n"); 7485212e11fSVishal Verma goto out; 7495212e11fSVishal Verma } else { 750e6be2dcbSVishal Verma dev_err(to_dev(arena), 7515212e11fSVishal Verma "Found corrupted metadata!\n"); 7525212e11fSVishal Verma ret = -ENODEV; 7535212e11fSVishal Verma goto out; 7545212e11fSVishal Verma } 7555212e11fSVishal Verma } 7565212e11fSVishal Verma 7575212e11fSVishal Verma arena->external_lba_start = cur_nlba; 7585212e11fSVishal Verma parse_arena_meta(arena, super, cur_off); 7595212e11fSVishal Verma 760d9b83c75SVishal Verma mutex_init(&arena->err_lock); 7615212e11fSVishal Verma ret = btt_freelist_init(arena); 7625212e11fSVishal Verma if (ret) 7635212e11fSVishal Verma goto out; 7645212e11fSVishal Verma 7655212e11fSVishal Verma ret = btt_rtt_init(arena); 7665212e11fSVishal Verma if (ret) 7675212e11fSVishal Verma goto out; 7685212e11fSVishal Verma 7695212e11fSVishal Verma ret = btt_maplocks_init(arena); 7705212e11fSVishal Verma if (ret) 7715212e11fSVishal Verma goto out; 7725212e11fSVishal Verma 7735212e11fSVishal Verma list_add_tail(&arena->list, &btt->arena_list); 7745212e11fSVishal Verma 7755212e11fSVishal Verma remaining -= arena->size; 7765212e11fSVishal Verma cur_off += arena->size; 7775212e11fSVishal Verma cur_nlba += arena->external_nlba; 7785212e11fSVishal Verma num_arenas++; 7795212e11fSVishal Verma 7805212e11fSVishal Verma if (arena->nextoff == 0) 7815212e11fSVishal Verma break; 7825212e11fSVishal Verma } 7835212e11fSVishal Verma btt->num_arenas = num_arenas; 7845212e11fSVishal Verma btt->nlba = cur_nlba; 7855212e11fSVishal Verma btt->init_state = INIT_READY; 7865212e11fSVishal Verma 7875212e11fSVishal Verma kfree(super); 7885212e11fSVishal Verma return ret; 7895212e11fSVishal Verma 7905212e11fSVishal Verma out: 7915212e11fSVishal Verma kfree(arena); 7925212e11fSVishal Verma free_arenas(btt); 7935212e11fSVishal Verma out_super: 7945212e11fSVishal Verma kfree(super); 7955212e11fSVishal Verma return ret; 7965212e11fSVishal Verma } 7975212e11fSVishal Verma 7985212e11fSVishal Verma static int create_arenas(struct btt *btt) 7995212e11fSVishal Verma { 8005212e11fSVishal Verma size_t remaining = btt->rawsize; 8015212e11fSVishal Verma size_t cur_off = 0; 8025212e11fSVishal Verma 8035212e11fSVishal Verma while (remaining) { 8045212e11fSVishal Verma struct arena_info *arena; 8055212e11fSVishal Verma size_t arena_size = min_t(u64, ARENA_MAX_SIZE, remaining); 8065212e11fSVishal Verma 8075212e11fSVishal Verma remaining -= arena_size; 8085212e11fSVishal Verma if (arena_size < ARENA_MIN_SIZE) 8095212e11fSVishal Verma break; 8105212e11fSVishal Verma 8115212e11fSVishal Verma arena = alloc_arena(btt, arena_size, btt->nlba, cur_off); 8125212e11fSVishal Verma if (!arena) { 8135212e11fSVishal Verma free_arenas(btt); 8145212e11fSVishal Verma return -ENOMEM; 8155212e11fSVishal Verma } 8165212e11fSVishal Verma btt->nlba += arena->external_nlba; 8175212e11fSVishal Verma if (remaining >= ARENA_MIN_SIZE) 8185212e11fSVishal Verma arena->nextoff = arena->size; 8195212e11fSVishal Verma else 8205212e11fSVishal Verma arena->nextoff = 0; 8215212e11fSVishal Verma cur_off += arena_size; 8225212e11fSVishal Verma list_add_tail(&arena->list, &btt->arena_list); 8235212e11fSVishal Verma } 8245212e11fSVishal Verma 8255212e11fSVishal Verma return 0; 8265212e11fSVishal Verma } 8275212e11fSVishal Verma 8285212e11fSVishal Verma /* 8295212e11fSVishal Verma * This function completes arena initialization by writing 8305212e11fSVishal Verma * all the metadata. 8315212e11fSVishal Verma * It is only called for an uninitialized arena when a write 8325212e11fSVishal Verma * to that arena occurs for the first time. 8335212e11fSVishal Verma */ 834fbde1414SVishal Verma static int btt_arena_write_layout(struct arena_info *arena) 8355212e11fSVishal Verma { 8365212e11fSVishal Verma int ret; 837e1455744SDan Williams u64 sum; 8385212e11fSVishal Verma struct btt_sb *super; 839fbde1414SVishal Verma struct nd_btt *nd_btt = arena->nd_btt; 8406ec68954SVishal Verma const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev); 8415212e11fSVishal Verma 8425212e11fSVishal Verma ret = btt_map_init(arena); 8435212e11fSVishal Verma if (ret) 8445212e11fSVishal Verma return ret; 8455212e11fSVishal Verma 8465212e11fSVishal Verma ret = btt_log_init(arena); 8475212e11fSVishal Verma if (ret) 8485212e11fSVishal Verma return ret; 8495212e11fSVishal Verma 8505212e11fSVishal Verma super = kzalloc(sizeof(struct btt_sb), GFP_NOIO); 8515212e11fSVishal Verma if (!super) 8525212e11fSVishal Verma return -ENOMEM; 8535212e11fSVishal Verma 8545212e11fSVishal Verma strncpy(super->signature, BTT_SIG, BTT_SIG_LEN); 855fbde1414SVishal Verma memcpy(super->uuid, nd_btt->uuid, 16); 8566ec68954SVishal Verma memcpy(super->parent_uuid, parent_uuid, 16); 8575212e11fSVishal Verma super->flags = cpu_to_le32(arena->flags); 8585212e11fSVishal Verma super->version_major = cpu_to_le16(arena->version_major); 8595212e11fSVishal Verma super->version_minor = cpu_to_le16(arena->version_minor); 8605212e11fSVishal Verma super->external_lbasize = cpu_to_le32(arena->external_lbasize); 8615212e11fSVishal Verma super->external_nlba = cpu_to_le32(arena->external_nlba); 8625212e11fSVishal Verma super->internal_lbasize = cpu_to_le32(arena->internal_lbasize); 8635212e11fSVishal Verma super->internal_nlba = cpu_to_le32(arena->internal_nlba); 8645212e11fSVishal Verma super->nfree = cpu_to_le32(arena->nfree); 8655212e11fSVishal Verma super->infosize = cpu_to_le32(sizeof(struct btt_sb)); 8665212e11fSVishal Verma super->nextoff = cpu_to_le64(arena->nextoff); 8675212e11fSVishal Verma /* 8685212e11fSVishal Verma * Subtract arena->infooff (arena start) so numbers are relative 8695212e11fSVishal Verma * to 'this' arena 8705212e11fSVishal Verma */ 8715212e11fSVishal Verma super->dataoff = cpu_to_le64(arena->dataoff - arena->infooff); 8725212e11fSVishal Verma super->mapoff = cpu_to_le64(arena->mapoff - arena->infooff); 8735212e11fSVishal Verma super->logoff = cpu_to_le64(arena->logoff - arena->infooff); 8745212e11fSVishal Verma super->info2off = cpu_to_le64(arena->info2off - arena->infooff); 8755212e11fSVishal Verma 8765212e11fSVishal Verma super->flags = 0; 877e1455744SDan Williams sum = nd_sb_checksum((struct nd_gen_sb *) super); 878e1455744SDan Williams super->checksum = cpu_to_le64(sum); 8795212e11fSVishal Verma 8805212e11fSVishal Verma ret = btt_info_write(arena, super); 8815212e11fSVishal Verma 8825212e11fSVishal Verma kfree(super); 8835212e11fSVishal Verma return ret; 8845212e11fSVishal Verma } 8855212e11fSVishal Verma 8865212e11fSVishal Verma /* 8875212e11fSVishal Verma * This function completes the initialization for the BTT namespace 8885212e11fSVishal Verma * such that it is ready to accept IOs 8895212e11fSVishal Verma */ 8905212e11fSVishal Verma static int btt_meta_init(struct btt *btt) 8915212e11fSVishal Verma { 8925212e11fSVishal Verma int ret = 0; 8935212e11fSVishal Verma struct arena_info *arena; 8945212e11fSVishal Verma 8955212e11fSVishal Verma mutex_lock(&btt->init_lock); 8965212e11fSVishal Verma list_for_each_entry(arena, &btt->arena_list, list) { 897fbde1414SVishal Verma ret = btt_arena_write_layout(arena); 8985212e11fSVishal Verma if (ret) 8995212e11fSVishal Verma goto unlock; 9005212e11fSVishal Verma 9015212e11fSVishal Verma ret = btt_freelist_init(arena); 9025212e11fSVishal Verma if (ret) 9035212e11fSVishal Verma goto unlock; 9045212e11fSVishal Verma 9055212e11fSVishal Verma ret = btt_rtt_init(arena); 9065212e11fSVishal Verma if (ret) 9075212e11fSVishal Verma goto unlock; 9085212e11fSVishal Verma 9095212e11fSVishal Verma ret = btt_maplocks_init(arena); 9105212e11fSVishal Verma if (ret) 9115212e11fSVishal Verma goto unlock; 9125212e11fSVishal Verma } 9135212e11fSVishal Verma 9145212e11fSVishal Verma btt->init_state = INIT_READY; 9155212e11fSVishal Verma 9165212e11fSVishal Verma unlock: 9175212e11fSVishal Verma mutex_unlock(&btt->init_lock); 9185212e11fSVishal Verma return ret; 9195212e11fSVishal Verma } 9205212e11fSVishal Verma 92141cd8b70SVishal Verma static u32 btt_meta_size(struct btt *btt) 92241cd8b70SVishal Verma { 92341cd8b70SVishal Verma return btt->lbasize - btt->sector_size; 92441cd8b70SVishal Verma } 92541cd8b70SVishal Verma 9265212e11fSVishal Verma /* 9275212e11fSVishal Verma * This function calculates the arena in which the given LBA lies 9285212e11fSVishal Verma * by doing a linear walk. This is acceptable since we expect only 9295212e11fSVishal Verma * a few arenas. If we have backing devices that get much larger, 9305212e11fSVishal Verma * we can construct a balanced binary tree of arenas at init time 9315212e11fSVishal Verma * so that this range search becomes faster. 9325212e11fSVishal Verma */ 9335212e11fSVishal Verma static int lba_to_arena(struct btt *btt, sector_t sector, __u32 *premap, 9345212e11fSVishal Verma struct arena_info **arena) 9355212e11fSVishal Verma { 9365212e11fSVishal Verma struct arena_info *arena_list; 9375212e11fSVishal Verma __u64 lba = div_u64(sector << SECTOR_SHIFT, btt->sector_size); 9385212e11fSVishal Verma 9395212e11fSVishal Verma list_for_each_entry(arena_list, &btt->arena_list, list) { 9405212e11fSVishal Verma if (lba < arena_list->external_nlba) { 9415212e11fSVishal Verma *arena = arena_list; 9425212e11fSVishal Verma *premap = lba; 9435212e11fSVishal Verma return 0; 9445212e11fSVishal Verma } 9455212e11fSVishal Verma lba -= arena_list->external_nlba; 9465212e11fSVishal Verma } 9475212e11fSVishal Verma 9485212e11fSVishal Verma return -EIO; 9495212e11fSVishal Verma } 9505212e11fSVishal Verma 9515212e11fSVishal Verma /* 9525212e11fSVishal Verma * The following (lock_map, unlock_map) are mostly just to improve 9535212e11fSVishal Verma * readability, since they index into an array of locks 9545212e11fSVishal Verma */ 9555212e11fSVishal Verma static void lock_map(struct arena_info *arena, u32 premap) 9565212e11fSVishal Verma __acquires(&arena->map_locks[idx].lock) 9575212e11fSVishal Verma { 9585212e11fSVishal Verma u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree; 9595212e11fSVishal Verma 9605212e11fSVishal Verma spin_lock(&arena->map_locks[idx].lock); 9615212e11fSVishal Verma } 9625212e11fSVishal Verma 9635212e11fSVishal Verma static void unlock_map(struct arena_info *arena, u32 premap) 9645212e11fSVishal Verma __releases(&arena->map_locks[idx].lock) 9655212e11fSVishal Verma { 9665212e11fSVishal Verma u32 idx = (premap * MAP_ENT_SIZE / L1_CACHE_BYTES) % arena->nfree; 9675212e11fSVishal Verma 9685212e11fSVishal Verma spin_unlock(&arena->map_locks[idx].lock); 9695212e11fSVishal Verma } 9705212e11fSVishal Verma 9715212e11fSVishal Verma static int btt_data_read(struct arena_info *arena, struct page *page, 9725212e11fSVishal Verma unsigned int off, u32 lba, u32 len) 9735212e11fSVishal Verma { 9745212e11fSVishal Verma int ret; 9755212e11fSVishal Verma u64 nsoff = to_namespace_offset(arena, lba); 9765212e11fSVishal Verma void *mem = kmap_atomic(page); 9775212e11fSVishal Verma 9783ae3d67bSVishal Verma ret = arena_read_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC); 9795212e11fSVishal Verma kunmap_atomic(mem); 9805212e11fSVishal Verma 9815212e11fSVishal Verma return ret; 9825212e11fSVishal Verma } 9835212e11fSVishal Verma 9845212e11fSVishal Verma static int btt_data_write(struct arena_info *arena, u32 lba, 9855212e11fSVishal Verma struct page *page, unsigned int off, u32 len) 9865212e11fSVishal Verma { 9875212e11fSVishal Verma int ret; 9885212e11fSVishal Verma u64 nsoff = to_namespace_offset(arena, lba); 9895212e11fSVishal Verma void *mem = kmap_atomic(page); 9905212e11fSVishal Verma 9913ae3d67bSVishal Verma ret = arena_write_bytes(arena, nsoff, mem + off, len, NVDIMM_IO_ATOMIC); 9925212e11fSVishal Verma kunmap_atomic(mem); 9935212e11fSVishal Verma 9945212e11fSVishal Verma return ret; 9955212e11fSVishal Verma } 9965212e11fSVishal Verma 9975212e11fSVishal Verma static void zero_fill_data(struct page *page, unsigned int off, u32 len) 9985212e11fSVishal Verma { 9995212e11fSVishal Verma void *mem = kmap_atomic(page); 10005212e11fSVishal Verma 10015212e11fSVishal Verma memset(mem + off, 0, len); 10025212e11fSVishal Verma kunmap_atomic(mem); 10035212e11fSVishal Verma } 10045212e11fSVishal Verma 100541cd8b70SVishal Verma #ifdef CONFIG_BLK_DEV_INTEGRITY 100641cd8b70SVishal Verma static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip, 100741cd8b70SVishal Verma struct arena_info *arena, u32 postmap, int rw) 100841cd8b70SVishal Verma { 100941cd8b70SVishal Verma unsigned int len = btt_meta_size(btt); 101041cd8b70SVishal Verma u64 meta_nsoff; 101141cd8b70SVishal Verma int ret = 0; 101241cd8b70SVishal Verma 101341cd8b70SVishal Verma if (bip == NULL) 101441cd8b70SVishal Verma return 0; 101541cd8b70SVishal Verma 101641cd8b70SVishal Verma meta_nsoff = to_namespace_offset(arena, postmap) + btt->sector_size; 101741cd8b70SVishal Verma 101841cd8b70SVishal Verma while (len) { 101941cd8b70SVishal Verma unsigned int cur_len; 102041cd8b70SVishal Verma struct bio_vec bv; 102141cd8b70SVishal Verma void *mem; 102241cd8b70SVishal Verma 102341cd8b70SVishal Verma bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter); 102441cd8b70SVishal Verma /* 102541cd8b70SVishal Verma * The 'bv' obtained from bvec_iter_bvec has its .bv_len and 102641cd8b70SVishal Verma * .bv_offset already adjusted for iter->bi_bvec_done, and we 102741cd8b70SVishal Verma * can use those directly 102841cd8b70SVishal Verma */ 102941cd8b70SVishal Verma 103041cd8b70SVishal Verma cur_len = min(len, bv.bv_len); 103141cd8b70SVishal Verma mem = kmap_atomic(bv.bv_page); 103241cd8b70SVishal Verma if (rw) 103341cd8b70SVishal Verma ret = arena_write_bytes(arena, meta_nsoff, 10343ae3d67bSVishal Verma mem + bv.bv_offset, cur_len, 10353ae3d67bSVishal Verma NVDIMM_IO_ATOMIC); 103641cd8b70SVishal Verma else 103741cd8b70SVishal Verma ret = arena_read_bytes(arena, meta_nsoff, 10383ae3d67bSVishal Verma mem + bv.bv_offset, cur_len, 10393ae3d67bSVishal Verma NVDIMM_IO_ATOMIC); 104041cd8b70SVishal Verma 104141cd8b70SVishal Verma kunmap_atomic(mem); 104241cd8b70SVishal Verma if (ret) 104341cd8b70SVishal Verma return ret; 104441cd8b70SVishal Verma 104541cd8b70SVishal Verma len -= cur_len; 104641cd8b70SVishal Verma meta_nsoff += cur_len; 1047b1fb2c52SDmitry Monakhov if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len)) 1048b1fb2c52SDmitry Monakhov return -EIO; 104941cd8b70SVishal Verma } 105041cd8b70SVishal Verma 105141cd8b70SVishal Verma return ret; 105241cd8b70SVishal Verma } 105341cd8b70SVishal Verma 105441cd8b70SVishal Verma #else /* CONFIG_BLK_DEV_INTEGRITY */ 105541cd8b70SVishal Verma static int btt_rw_integrity(struct btt *btt, struct bio_integrity_payload *bip, 105641cd8b70SVishal Verma struct arena_info *arena, u32 postmap, int rw) 105741cd8b70SVishal Verma { 105841cd8b70SVishal Verma return 0; 105941cd8b70SVishal Verma } 106041cd8b70SVishal Verma #endif 106141cd8b70SVishal Verma 106241cd8b70SVishal Verma static int btt_read_pg(struct btt *btt, struct bio_integrity_payload *bip, 106341cd8b70SVishal Verma struct page *page, unsigned int off, sector_t sector, 106441cd8b70SVishal Verma unsigned int len) 10655212e11fSVishal Verma { 10665212e11fSVishal Verma int ret = 0; 10675212e11fSVishal Verma int t_flag, e_flag; 10685212e11fSVishal Verma struct arena_info *arena = NULL; 10695212e11fSVishal Verma u32 lane = 0, premap, postmap; 10705212e11fSVishal Verma 10715212e11fSVishal Verma while (len) { 10725212e11fSVishal Verma u32 cur_len; 10735212e11fSVishal Verma 10745212e11fSVishal Verma lane = nd_region_acquire_lane(btt->nd_region); 10755212e11fSVishal Verma 10765212e11fSVishal Verma ret = lba_to_arena(btt, sector, &premap, &arena); 10775212e11fSVishal Verma if (ret) 10785212e11fSVishal Verma goto out_lane; 10795212e11fSVishal Verma 10805212e11fSVishal Verma cur_len = min(btt->sector_size, len); 10815212e11fSVishal Verma 10823ae3d67bSVishal Verma ret = btt_map_read(arena, premap, &postmap, &t_flag, &e_flag, 10833ae3d67bSVishal Verma NVDIMM_IO_ATOMIC); 10845212e11fSVishal Verma if (ret) 10855212e11fSVishal Verma goto out_lane; 10865212e11fSVishal Verma 10875212e11fSVishal Verma /* 10885212e11fSVishal Verma * We loop to make sure that the post map LBA didn't change 10895212e11fSVishal Verma * from under us between writing the RTT and doing the actual 10905212e11fSVishal Verma * read. 10915212e11fSVishal Verma */ 10925212e11fSVishal Verma while (1) { 10935212e11fSVishal Verma u32 new_map; 10941398199dSVishal Verma int new_t, new_e; 10955212e11fSVishal Verma 10965212e11fSVishal Verma if (t_flag) { 10975212e11fSVishal Verma zero_fill_data(page, off, cur_len); 10985212e11fSVishal Verma goto out_lane; 10995212e11fSVishal Verma } 11005212e11fSVishal Verma 11015212e11fSVishal Verma if (e_flag) { 11025212e11fSVishal Verma ret = -EIO; 11035212e11fSVishal Verma goto out_lane; 11045212e11fSVishal Verma } 11055212e11fSVishal Verma 11065212e11fSVishal Verma arena->rtt[lane] = RTT_VALID | postmap; 11075212e11fSVishal Verma /* 11085212e11fSVishal Verma * Barrier to make sure this write is not reordered 11095212e11fSVishal Verma * to do the verification map_read before the RTT store 11105212e11fSVishal Verma */ 11115212e11fSVishal Verma barrier(); 11125212e11fSVishal Verma 11131398199dSVishal Verma ret = btt_map_read(arena, premap, &new_map, &new_t, 11141398199dSVishal Verma &new_e, NVDIMM_IO_ATOMIC); 11155212e11fSVishal Verma if (ret) 11165212e11fSVishal Verma goto out_rtt; 11175212e11fSVishal Verma 11181398199dSVishal Verma if ((postmap == new_map) && (t_flag == new_t) && 11191398199dSVishal Verma (e_flag == new_e)) 11205212e11fSVishal Verma break; 11215212e11fSVishal Verma 11225212e11fSVishal Verma postmap = new_map; 11231398199dSVishal Verma t_flag = new_t; 11241398199dSVishal Verma e_flag = new_e; 11255212e11fSVishal Verma } 11265212e11fSVishal Verma 11275212e11fSVishal Verma ret = btt_data_read(arena, page, off, postmap, cur_len); 1128d9b83c75SVishal Verma if (ret) { 1129d9b83c75SVishal Verma int rc; 1130d9b83c75SVishal Verma 1131d9b83c75SVishal Verma /* Media error - set the e_flag */ 1132d9b83c75SVishal Verma rc = btt_map_write(arena, premap, postmap, 0, 1, 1133d9b83c75SVishal Verma NVDIMM_IO_ATOMIC); 11345212e11fSVishal Verma goto out_rtt; 1135d9b83c75SVishal Verma } 11365212e11fSVishal Verma 113741cd8b70SVishal Verma if (bip) { 113841cd8b70SVishal Verma ret = btt_rw_integrity(btt, bip, arena, postmap, READ); 113941cd8b70SVishal Verma if (ret) 114041cd8b70SVishal Verma goto out_rtt; 114141cd8b70SVishal Verma } 114241cd8b70SVishal Verma 11435212e11fSVishal Verma arena->rtt[lane] = RTT_INVALID; 11445212e11fSVishal Verma nd_region_release_lane(btt->nd_region, lane); 11455212e11fSVishal Verma 11465212e11fSVishal Verma len -= cur_len; 11475212e11fSVishal Verma off += cur_len; 11485212e11fSVishal Verma sector += btt->sector_size >> SECTOR_SHIFT; 11495212e11fSVishal Verma } 11505212e11fSVishal Verma 11515212e11fSVishal Verma return 0; 11525212e11fSVishal Verma 11535212e11fSVishal Verma out_rtt: 11545212e11fSVishal Verma arena->rtt[lane] = RTT_INVALID; 11555212e11fSVishal Verma out_lane: 11565212e11fSVishal Verma nd_region_release_lane(btt->nd_region, lane); 11575212e11fSVishal Verma return ret; 11585212e11fSVishal Verma } 11595212e11fSVishal Verma 1160d9b83c75SVishal Verma /* 1161d9b83c75SVishal Verma * Normally, arena_{read,write}_bytes will take care of the initial offset 1162d9b83c75SVishal Verma * adjustment, but in the case of btt_is_badblock, where we query is_bad_pmem, 1163d9b83c75SVishal Verma * we need the final, raw namespace offset here 1164d9b83c75SVishal Verma */ 1165d9b83c75SVishal Verma static bool btt_is_badblock(struct btt *btt, struct arena_info *arena, 1166d9b83c75SVishal Verma u32 postmap) 1167d9b83c75SVishal Verma { 1168d9b83c75SVishal Verma u64 nsoff = adjust_initial_offset(arena->nd_btt, 1169d9b83c75SVishal Verma to_namespace_offset(arena, postmap)); 1170d9b83c75SVishal Verma sector_t phys_sector = nsoff >> 9; 1171d9b83c75SVishal Verma 1172d9b83c75SVishal Verma return is_bad_pmem(btt->phys_bb, phys_sector, arena->internal_lbasize); 1173d9b83c75SVishal Verma } 1174d9b83c75SVishal Verma 117541cd8b70SVishal Verma static int btt_write_pg(struct btt *btt, struct bio_integrity_payload *bip, 117641cd8b70SVishal Verma sector_t sector, struct page *page, unsigned int off, 117741cd8b70SVishal Verma unsigned int len) 11785212e11fSVishal Verma { 11795212e11fSVishal Verma int ret = 0; 11805212e11fSVishal Verma struct arena_info *arena = NULL; 11815212e11fSVishal Verma u32 premap = 0, old_postmap, new_postmap, lane = 0, i; 11825212e11fSVishal Verma struct log_entry log; 11835212e11fSVishal Verma int sub; 11845212e11fSVishal Verma 11855212e11fSVishal Verma while (len) { 11865212e11fSVishal Verma u32 cur_len; 1187d9b83c75SVishal Verma int e_flag; 11885212e11fSVishal Verma 1189d9b83c75SVishal Verma retry: 11905212e11fSVishal Verma lane = nd_region_acquire_lane(btt->nd_region); 11915212e11fSVishal Verma 11925212e11fSVishal Verma ret = lba_to_arena(btt, sector, &premap, &arena); 11935212e11fSVishal Verma if (ret) 11945212e11fSVishal Verma goto out_lane; 11955212e11fSVishal Verma cur_len = min(btt->sector_size, len); 11965212e11fSVishal Verma 11975212e11fSVishal Verma if ((arena->flags & IB_FLAG_ERROR_MASK) != 0) { 11985212e11fSVishal Verma ret = -EIO; 11995212e11fSVishal Verma goto out_lane; 12005212e11fSVishal Verma } 12015212e11fSVishal Verma 1202d9b83c75SVishal Verma if (btt_is_badblock(btt, arena, arena->freelist[lane].block)) 1203d9b83c75SVishal Verma arena->freelist[lane].has_err = 1; 1204d9b83c75SVishal Verma 1205d9b83c75SVishal Verma if (mutex_is_locked(&arena->err_lock) 1206d9b83c75SVishal Verma || arena->freelist[lane].has_err) { 1207d9b83c75SVishal Verma nd_region_release_lane(btt->nd_region, lane); 1208d9b83c75SVishal Verma 1209d9b83c75SVishal Verma ret = arena_clear_freelist_error(arena, lane); 1210d9b83c75SVishal Verma if (ret) 1211d9b83c75SVishal Verma return ret; 1212d9b83c75SVishal Verma 1213d9b83c75SVishal Verma /* OK to acquire a different lane/free block */ 1214d9b83c75SVishal Verma goto retry; 1215d9b83c75SVishal Verma } 1216d9b83c75SVishal Verma 12175212e11fSVishal Verma new_postmap = arena->freelist[lane].block; 12185212e11fSVishal Verma 12195212e11fSVishal Verma /* Wait if the new block is being read from */ 12205212e11fSVishal Verma for (i = 0; i < arena->nfree; i++) 12215212e11fSVishal Verma while (arena->rtt[i] == (RTT_VALID | new_postmap)) 12225212e11fSVishal Verma cpu_relax(); 12235212e11fSVishal Verma 12245212e11fSVishal Verma 12255212e11fSVishal Verma if (new_postmap >= arena->internal_nlba) { 12265212e11fSVishal Verma ret = -EIO; 12275212e11fSVishal Verma goto out_lane; 122841cd8b70SVishal Verma } 122941cd8b70SVishal Verma 123041cd8b70SVishal Verma ret = btt_data_write(arena, new_postmap, page, off, cur_len); 12315212e11fSVishal Verma if (ret) 12325212e11fSVishal Verma goto out_lane; 12335212e11fSVishal Verma 123441cd8b70SVishal Verma if (bip) { 123541cd8b70SVishal Verma ret = btt_rw_integrity(btt, bip, arena, new_postmap, 123641cd8b70SVishal Verma WRITE); 123741cd8b70SVishal Verma if (ret) 123841cd8b70SVishal Verma goto out_lane; 123941cd8b70SVishal Verma } 124041cd8b70SVishal Verma 12415212e11fSVishal Verma lock_map(arena, premap); 1242d9b83c75SVishal Verma ret = btt_map_read(arena, premap, &old_postmap, NULL, &e_flag, 12433ae3d67bSVishal Verma NVDIMM_IO_ATOMIC); 12445212e11fSVishal Verma if (ret) 12455212e11fSVishal Verma goto out_map; 12465212e11fSVishal Verma if (old_postmap >= arena->internal_nlba) { 12475212e11fSVishal Verma ret = -EIO; 12485212e11fSVishal Verma goto out_map; 12495212e11fSVishal Verma } 1250d9b83c75SVishal Verma if (e_flag) 1251d9b83c75SVishal Verma set_e_flag(old_postmap); 12525212e11fSVishal Verma 12535212e11fSVishal Verma log.lba = cpu_to_le32(premap); 12545212e11fSVishal Verma log.old_map = cpu_to_le32(old_postmap); 12555212e11fSVishal Verma log.new_map = cpu_to_le32(new_postmap); 12565212e11fSVishal Verma log.seq = cpu_to_le32(arena->freelist[lane].seq); 12575212e11fSVishal Verma sub = arena->freelist[lane].sub; 12585212e11fSVishal Verma ret = btt_flog_write(arena, lane, sub, &log); 12595212e11fSVishal Verma if (ret) 12605212e11fSVishal Verma goto out_map; 12615212e11fSVishal Verma 12621db1f3ceSVishal Verma ret = btt_map_write(arena, premap, new_postmap, 0, 0, 12631db1f3ceSVishal Verma NVDIMM_IO_ATOMIC); 12645212e11fSVishal Verma if (ret) 12655212e11fSVishal Verma goto out_map; 12665212e11fSVishal Verma 12675212e11fSVishal Verma unlock_map(arena, premap); 12685212e11fSVishal Verma nd_region_release_lane(btt->nd_region, lane); 12695212e11fSVishal Verma 1270d9b83c75SVishal Verma if (e_flag) { 1271d9b83c75SVishal Verma ret = arena_clear_freelist_error(arena, lane); 1272d9b83c75SVishal Verma if (ret) 1273d9b83c75SVishal Verma return ret; 1274d9b83c75SVishal Verma } 1275d9b83c75SVishal Verma 12765212e11fSVishal Verma len -= cur_len; 12775212e11fSVishal Verma off += cur_len; 12785212e11fSVishal Verma sector += btt->sector_size >> SECTOR_SHIFT; 12795212e11fSVishal Verma } 12805212e11fSVishal Verma 12815212e11fSVishal Verma return 0; 12825212e11fSVishal Verma 12835212e11fSVishal Verma out_map: 12845212e11fSVishal Verma unlock_map(arena, premap); 12855212e11fSVishal Verma out_lane: 12865212e11fSVishal Verma nd_region_release_lane(btt->nd_region, lane); 12875212e11fSVishal Verma return ret; 12885212e11fSVishal Verma } 12895212e11fSVishal Verma 129041cd8b70SVishal Verma static int btt_do_bvec(struct btt *btt, struct bio_integrity_payload *bip, 129141cd8b70SVishal Verma struct page *page, unsigned int len, unsigned int off, 1292c11f0c0bSJens Axboe bool is_write, sector_t sector) 12935212e11fSVishal Verma { 12945212e11fSVishal Verma int ret; 12955212e11fSVishal Verma 1296c11f0c0bSJens Axboe if (!is_write) { 129741cd8b70SVishal Verma ret = btt_read_pg(btt, bip, page, off, sector, len); 12985212e11fSVishal Verma flush_dcache_page(page); 12995212e11fSVishal Verma } else { 13005212e11fSVishal Verma flush_dcache_page(page); 130141cd8b70SVishal Verma ret = btt_write_pg(btt, bip, sector, page, off, len); 13025212e11fSVishal Verma } 13035212e11fSVishal Verma 13045212e11fSVishal Verma return ret; 13055212e11fSVishal Verma } 13065212e11fSVishal Verma 1307dece1635SJens Axboe static blk_qc_t btt_make_request(struct request_queue *q, struct bio *bio) 13085212e11fSVishal Verma { 130941cd8b70SVishal Verma struct bio_integrity_payload *bip = bio_integrity(bio); 13105212e11fSVishal Verma struct btt *btt = q->queuedata; 13115212e11fSVishal Verma struct bvec_iter iter; 1312f0dc089cSDan Williams unsigned long start; 13135212e11fSVishal Verma struct bio_vec bvec; 1314abf54548SMike Christie int err = 0; 1315f0dc089cSDan Williams bool do_acct; 13165212e11fSVishal Verma 1317e23947bdSDmitry Monakhov if (!bio_integrity_prep(bio)) 1318e23947bdSDmitry Monakhov return BLK_QC_T_NONE; 131941cd8b70SVishal Verma 1320f0dc089cSDan Williams do_acct = nd_iostat_start(bio, &start); 13215212e11fSVishal Verma bio_for_each_segment(bvec, bio, iter) { 13225212e11fSVishal Verma unsigned int len = bvec.bv_len; 13235212e11fSVishal Verma 1324*86652d2eSVishal Verma if (len > PAGE_SIZE || len < btt->sector_size || 1325*86652d2eSVishal Verma len % btt->sector_size) { 1326*86652d2eSVishal Verma dev_err_ratelimited(&btt->nd_btt->dev, 1327*86652d2eSVishal Verma "unaligned bio segment (len: %d)\n", len); 1328*86652d2eSVishal Verma bio->bi_status = BLK_STS_IOERR; 1329*86652d2eSVishal Verma break; 1330*86652d2eSVishal Verma } 13315212e11fSVishal Verma 133241cd8b70SVishal Verma err = btt_do_bvec(btt, bip, bvec.bv_page, len, bvec.bv_offset, 1333c11f0c0bSJens Axboe op_is_write(bio_op(bio)), iter.bi_sector); 13345212e11fSVishal Verma if (err) { 1335e6be2dcbSVishal Verma dev_err(&btt->nd_btt->dev, 13365212e11fSVishal Verma "io error in %s sector %lld, len %d,\n", 1337abf54548SMike Christie (op_is_write(bio_op(bio))) ? "WRITE" : 1338abf54548SMike Christie "READ", 13395212e11fSVishal Verma (unsigned long long) iter.bi_sector, len); 13404e4cbee9SChristoph Hellwig bio->bi_status = errno_to_blk_status(err); 1341f0dc089cSDan Williams break; 13425212e11fSVishal Verma } 13435212e11fSVishal Verma } 1344f0dc089cSDan Williams if (do_acct) 1345f0dc089cSDan Williams nd_iostat_end(bio, start); 13465212e11fSVishal Verma 13474246a0b6SChristoph Hellwig bio_endio(bio); 1348dece1635SJens Axboe return BLK_QC_T_NONE; 13495212e11fSVishal Verma } 13505212e11fSVishal Verma 13515212e11fSVishal Verma static int btt_rw_page(struct block_device *bdev, sector_t sector, 1352c11f0c0bSJens Axboe struct page *page, bool is_write) 13535212e11fSVishal Verma { 13545212e11fSVishal Verma struct btt *btt = bdev->bd_disk->private_data; 1355c13c43d5SVishal Verma int rc; 13565212e11fSVishal Verma 1357c13c43d5SVishal Verma rc = btt_do_bvec(btt, NULL, page, PAGE_SIZE, 0, is_write, sector); 1358c13c43d5SVishal Verma if (rc == 0) 1359c11f0c0bSJens Axboe page_endio(page, is_write, 0); 1360c13c43d5SVishal Verma 1361c13c43d5SVishal Verma return rc; 13625212e11fSVishal Verma } 13635212e11fSVishal Verma 13645212e11fSVishal Verma 13655212e11fSVishal Verma static int btt_getgeo(struct block_device *bd, struct hd_geometry *geo) 13665212e11fSVishal Verma { 13675212e11fSVishal Verma /* some standard values */ 13685212e11fSVishal Verma geo->heads = 1 << 6; 13695212e11fSVishal Verma geo->sectors = 1 << 5; 13705212e11fSVishal Verma geo->cylinders = get_capacity(bd->bd_disk) >> 11; 13715212e11fSVishal Verma return 0; 13725212e11fSVishal Verma } 13735212e11fSVishal Verma 13745212e11fSVishal Verma static const struct block_device_operations btt_fops = { 13755212e11fSVishal Verma .owner = THIS_MODULE, 13765212e11fSVishal Verma .rw_page = btt_rw_page, 13775212e11fSVishal Verma .getgeo = btt_getgeo, 137858138820SDan Williams .revalidate_disk = nvdimm_revalidate_disk, 13795212e11fSVishal Verma }; 13805212e11fSVishal Verma 13815212e11fSVishal Verma static int btt_blk_init(struct btt *btt) 13825212e11fSVishal Verma { 13835212e11fSVishal Verma struct nd_btt *nd_btt = btt->nd_btt; 13845212e11fSVishal Verma struct nd_namespace_common *ndns = nd_btt->ndns; 13855212e11fSVishal Verma 13865212e11fSVishal Verma /* create a new disk and request queue for btt */ 13875212e11fSVishal Verma btt->btt_queue = blk_alloc_queue(GFP_KERNEL); 13885212e11fSVishal Verma if (!btt->btt_queue) 13895212e11fSVishal Verma return -ENOMEM; 13905212e11fSVishal Verma 13915212e11fSVishal Verma btt->btt_disk = alloc_disk(0); 13925212e11fSVishal Verma if (!btt->btt_disk) { 13935212e11fSVishal Verma blk_cleanup_queue(btt->btt_queue); 13945212e11fSVishal Verma return -ENOMEM; 13955212e11fSVishal Verma } 13965212e11fSVishal Verma 13975212e11fSVishal Verma nvdimm_namespace_disk_name(ndns, btt->btt_disk->disk_name); 13985212e11fSVishal Verma btt->btt_disk->first_minor = 0; 13995212e11fSVishal Verma btt->btt_disk->fops = &btt_fops; 14005212e11fSVishal Verma btt->btt_disk->private_data = btt; 14015212e11fSVishal Verma btt->btt_disk->queue = btt->btt_queue; 14025212e11fSVishal Verma btt->btt_disk->flags = GENHD_FL_EXT_DEVT; 14035212e11fSVishal Verma 14045212e11fSVishal Verma blk_queue_make_request(btt->btt_queue, btt_make_request); 14055212e11fSVishal Verma blk_queue_logical_block_size(btt->btt_queue, btt->sector_size); 14065212e11fSVishal Verma blk_queue_max_hw_sectors(btt->btt_queue, UINT_MAX); 14075212e11fSVishal Verma queue_flag_set_unlocked(QUEUE_FLAG_NONROT, btt->btt_queue); 14085212e11fSVishal Verma btt->btt_queue->queuedata = btt; 14095212e11fSVishal Verma 141041cd8b70SVishal Verma set_capacity(btt->btt_disk, 0); 14110d52c756SDan Williams device_add_disk(&btt->nd_btt->dev, btt->btt_disk); 141241cd8b70SVishal Verma if (btt_meta_size(btt)) { 141341cd8b70SVishal Verma int rc = nd_integrity_init(btt->btt_disk, btt_meta_size(btt)); 141441cd8b70SVishal Verma 141541cd8b70SVishal Verma if (rc) { 141641cd8b70SVishal Verma del_gendisk(btt->btt_disk); 141741cd8b70SVishal Verma put_disk(btt->btt_disk); 141841cd8b70SVishal Verma blk_cleanup_queue(btt->btt_queue); 141941cd8b70SVishal Verma return rc; 142041cd8b70SVishal Verma } 142141cd8b70SVishal Verma } 142241cd8b70SVishal Verma set_capacity(btt->btt_disk, btt->nlba * btt->sector_size >> 9); 1423abe8b4e3SVishal Verma btt->nd_btt->size = btt->nlba * (u64)btt->sector_size; 142458138820SDan Williams revalidate_disk(btt->btt_disk); 14255212e11fSVishal Verma 14265212e11fSVishal Verma return 0; 14275212e11fSVishal Verma } 14285212e11fSVishal Verma 14295212e11fSVishal Verma static void btt_blk_cleanup(struct btt *btt) 14305212e11fSVishal Verma { 14315212e11fSVishal Verma del_gendisk(btt->btt_disk); 14325212e11fSVishal Verma put_disk(btt->btt_disk); 14335212e11fSVishal Verma blk_cleanup_queue(btt->btt_queue); 14345212e11fSVishal Verma } 14355212e11fSVishal Verma 14365212e11fSVishal Verma /** 14375212e11fSVishal Verma * btt_init - initialize a block translation table for the given device 14385212e11fSVishal Verma * @nd_btt: device with BTT geometry and backing device info 14395212e11fSVishal Verma * @rawsize: raw size in bytes of the backing device 14405212e11fSVishal Verma * @lbasize: lba size of the backing device 14415212e11fSVishal Verma * @uuid: A uuid for the backing device - this is stored on media 14425212e11fSVishal Verma * @maxlane: maximum number of parallel requests the device can handle 14435212e11fSVishal Verma * 14445212e11fSVishal Verma * Initialize a Block Translation Table on a backing device to provide 14455212e11fSVishal Verma * single sector power fail atomicity. 14465212e11fSVishal Verma * 14475212e11fSVishal Verma * Context: 14485212e11fSVishal Verma * Might sleep. 14495212e11fSVishal Verma * 14505212e11fSVishal Verma * Returns: 14515212e11fSVishal Verma * Pointer to a new struct btt on success, NULL on failure. 14525212e11fSVishal Verma */ 14535212e11fSVishal Verma static struct btt *btt_init(struct nd_btt *nd_btt, unsigned long long rawsize, 14545212e11fSVishal Verma u32 lbasize, u8 *uuid, struct nd_region *nd_region) 14555212e11fSVishal Verma { 14565212e11fSVishal Verma int ret; 14575212e11fSVishal Verma struct btt *btt; 1458d9b83c75SVishal Verma struct nd_namespace_io *nsio; 14595212e11fSVishal Verma struct device *dev = &nd_btt->dev; 14605212e11fSVishal Verma 1461e32bc729SDan Williams btt = devm_kzalloc(dev, sizeof(struct btt), GFP_KERNEL); 14625212e11fSVishal Verma if (!btt) 14635212e11fSVishal Verma return NULL; 14645212e11fSVishal Verma 14655212e11fSVishal Verma btt->nd_btt = nd_btt; 14665212e11fSVishal Verma btt->rawsize = rawsize; 14675212e11fSVishal Verma btt->lbasize = lbasize; 14685212e11fSVishal Verma btt->sector_size = ((lbasize >= 4096) ? 4096 : 512); 14695212e11fSVishal Verma INIT_LIST_HEAD(&btt->arena_list); 14705212e11fSVishal Verma mutex_init(&btt->init_lock); 14715212e11fSVishal Verma btt->nd_region = nd_region; 1472d9b83c75SVishal Verma nsio = to_nd_namespace_io(&nd_btt->ndns->dev); 1473d9b83c75SVishal Verma btt->phys_bb = &nsio->bb; 14745212e11fSVishal Verma 14755212e11fSVishal Verma ret = discover_arenas(btt); 14765212e11fSVishal Verma if (ret) { 14775212e11fSVishal Verma dev_err(dev, "init: error in arena_discover: %d\n", ret); 1478e32bc729SDan Williams return NULL; 14795212e11fSVishal Verma } 14805212e11fSVishal Verma 148158138820SDan Williams if (btt->init_state != INIT_READY && nd_region->ro) { 1482e6be2dcbSVishal Verma dev_warn(dev, "%s is read-only, unable to init btt metadata\n", 148358138820SDan Williams dev_name(&nd_region->dev)); 1484e32bc729SDan Williams return NULL; 148558138820SDan Williams } else if (btt->init_state != INIT_READY) { 14865212e11fSVishal Verma btt->num_arenas = (rawsize / ARENA_MAX_SIZE) + 14875212e11fSVishal Verma ((rawsize % ARENA_MAX_SIZE) ? 1 : 0); 14885212e11fSVishal Verma dev_dbg(dev, "init: %d arenas for %llu rawsize\n", 14895212e11fSVishal Verma btt->num_arenas, rawsize); 14905212e11fSVishal Verma 14915212e11fSVishal Verma ret = create_arenas(btt); 14925212e11fSVishal Verma if (ret) { 14935212e11fSVishal Verma dev_info(dev, "init: create_arenas: %d\n", ret); 1494e32bc729SDan Williams return NULL; 14955212e11fSVishal Verma } 14965212e11fSVishal Verma 14975212e11fSVishal Verma ret = btt_meta_init(btt); 14985212e11fSVishal Verma if (ret) { 14995212e11fSVishal Verma dev_err(dev, "init: error in meta_init: %d\n", ret); 1500e32bc729SDan Williams return NULL; 15015212e11fSVishal Verma } 15025212e11fSVishal Verma } 15035212e11fSVishal Verma 15045212e11fSVishal Verma ret = btt_blk_init(btt); 15055212e11fSVishal Verma if (ret) { 15065212e11fSVishal Verma dev_err(dev, "init: error in blk_init: %d\n", ret); 1507e32bc729SDan Williams return NULL; 15085212e11fSVishal Verma } 15095212e11fSVishal Verma 15105212e11fSVishal Verma btt_debugfs_init(btt); 15115212e11fSVishal Verma 15125212e11fSVishal Verma return btt; 15135212e11fSVishal Verma } 15145212e11fSVishal Verma 15155212e11fSVishal Verma /** 15165212e11fSVishal Verma * btt_fini - de-initialize a BTT 15175212e11fSVishal Verma * @btt: the BTT handle that was generated by btt_init 15185212e11fSVishal Verma * 15195212e11fSVishal Verma * De-initialize a Block Translation Table on device removal 15205212e11fSVishal Verma * 15215212e11fSVishal Verma * Context: 15225212e11fSVishal Verma * Might sleep. 15235212e11fSVishal Verma */ 15245212e11fSVishal Verma static void btt_fini(struct btt *btt) 15255212e11fSVishal Verma { 15265212e11fSVishal Verma if (btt) { 15275212e11fSVishal Verma btt_blk_cleanup(btt); 15285212e11fSVishal Verma free_arenas(btt); 15295212e11fSVishal Verma debugfs_remove_recursive(btt->debugfs_dir); 15305212e11fSVishal Verma } 15315212e11fSVishal Verma } 15325212e11fSVishal Verma 15335212e11fSVishal Verma int nvdimm_namespace_attach_btt(struct nd_namespace_common *ndns) 15345212e11fSVishal Verma { 15355212e11fSVishal Verma struct nd_btt *nd_btt = to_nd_btt(ndns->claim); 15365212e11fSVishal Verma struct nd_region *nd_region; 153714e49454SVishal Verma struct btt_sb *btt_sb; 15385212e11fSVishal Verma struct btt *btt; 15395212e11fSVishal Verma size_t rawsize; 15405212e11fSVishal Verma 15419dec4892SDan Williams if (!nd_btt->uuid || !nd_btt->ndns || !nd_btt->lbasize) { 15429dec4892SDan Williams dev_dbg(&nd_btt->dev, "incomplete btt configuration\n"); 15435212e11fSVishal Verma return -ENODEV; 15449dec4892SDan Williams } 15455212e11fSVishal Verma 154614e49454SVishal Verma btt_sb = devm_kzalloc(&nd_btt->dev, sizeof(*btt_sb), GFP_KERNEL); 1547ed36b4dbSChristophe Jaillet if (!btt_sb) 1548ed36b4dbSChristophe Jaillet return -ENOMEM; 154914e49454SVishal Verma 155014e49454SVishal Verma /* 155114e49454SVishal Verma * If this returns < 0, that is ok as it just means there wasn't 155214e49454SVishal Verma * an existing BTT, and we're creating a new one. We still need to 155314e49454SVishal Verma * call this as we need the version dependent fields in nd_btt to be 155414e49454SVishal Verma * set correctly based on the holder class 155514e49454SVishal Verma */ 155614e49454SVishal Verma nd_btt_version(nd_btt, ndns, btt_sb); 155714e49454SVishal Verma 155814e49454SVishal Verma rawsize = nvdimm_namespace_capacity(ndns) - nd_btt->initial_offset; 15595212e11fSVishal Verma if (rawsize < ARENA_MIN_SIZE) { 15609dec4892SDan Williams dev_dbg(&nd_btt->dev, "%s must be at least %ld bytes\n", 156114e49454SVishal Verma dev_name(&ndns->dev), 156214e49454SVishal Verma ARENA_MIN_SIZE + nd_btt->initial_offset); 15635212e11fSVishal Verma return -ENXIO; 15645212e11fSVishal Verma } 15655212e11fSVishal Verma nd_region = to_nd_region(nd_btt->dev.parent); 15665212e11fSVishal Verma btt = btt_init(nd_btt, rawsize, nd_btt->lbasize, nd_btt->uuid, 15675212e11fSVishal Verma nd_region); 15685212e11fSVishal Verma if (!btt) 15695212e11fSVishal Verma return -ENOMEM; 15705212e11fSVishal Verma nd_btt->btt = btt; 15715212e11fSVishal Verma 15725212e11fSVishal Verma return 0; 15735212e11fSVishal Verma } 15745212e11fSVishal Verma EXPORT_SYMBOL(nvdimm_namespace_attach_btt); 15755212e11fSVishal Verma 1576298f2bc5SDan Williams int nvdimm_namespace_detach_btt(struct nd_btt *nd_btt) 15775212e11fSVishal Verma { 15785212e11fSVishal Verma struct btt *btt = nd_btt->btt; 15795212e11fSVishal Verma 15805212e11fSVishal Verma btt_fini(btt); 15815212e11fSVishal Verma nd_btt->btt = NULL; 15825212e11fSVishal Verma 15835212e11fSVishal Verma return 0; 15845212e11fSVishal Verma } 15855212e11fSVishal Verma EXPORT_SYMBOL(nvdimm_namespace_detach_btt); 15865212e11fSVishal Verma 15875212e11fSVishal Verma static int __init nd_btt_init(void) 15885212e11fSVishal Verma { 1589ff8e92d5SNeilBrown int rc = 0; 15905212e11fSVishal Verma 15915212e11fSVishal Verma debugfs_root = debugfs_create_dir("btt", NULL); 1592ff8e92d5SNeilBrown if (IS_ERR_OR_NULL(debugfs_root)) 15935212e11fSVishal Verma rc = -ENXIO; 15945212e11fSVishal Verma 15955212e11fSVishal Verma return rc; 15965212e11fSVishal Verma } 15975212e11fSVishal Verma 15985212e11fSVishal Verma static void __exit nd_btt_exit(void) 15995212e11fSVishal Verma { 16005212e11fSVishal Verma debugfs_remove_recursive(debugfs_root); 16015212e11fSVishal Verma } 16025212e11fSVishal Verma 16035212e11fSVishal Verma MODULE_ALIAS_ND_DEVICE(ND_DEVICE_BTT); 16045212e11fSVishal Verma MODULE_AUTHOR("Vishal Verma <vishal.l.verma@linux.intel.com>"); 16055212e11fSVishal Verma MODULE_LICENSE("GPL v2"); 16065212e11fSVishal Verma module_init(nd_btt_init); 16075212e11fSVishal Verma module_exit(nd_btt_exit); 1608