btt_devs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/blkdev.h>
  6. #include <linux/device.h>
  7. #include <linux/genhd.h>
  8. #include <linux/sizes.h>
  9. #include <linux/slab.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include "nd-core.h"
  13. #include "btt.h"
  14. #include "nd.h"
  15. static void nd_btt_release(struct device *dev)
  16. {
  17. struct nd_region *nd_region = to_nd_region(dev->parent);
  18. struct nd_btt *nd_btt = to_nd_btt(dev);
  19. dev_dbg(dev, "trace\n");
  20. nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
  21. ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
  22. kfree(nd_btt->uuid);
  23. kfree(nd_btt);
  24. }
  25. struct nd_btt *to_nd_btt(struct device *dev)
  26. {
  27. struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
  28. WARN_ON(!is_nd_btt(dev));
  29. return nd_btt;
  30. }
  31. EXPORT_SYMBOL(to_nd_btt);
  32. static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
  33. 4096, 4104, 4160, 4224, 0 };
  34. static ssize_t sector_size_show(struct device *dev,
  35. struct device_attribute *attr, char *buf)
  36. {
  37. struct nd_btt *nd_btt = to_nd_btt(dev);
  38. return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
  39. }
  40. static ssize_t sector_size_store(struct device *dev,
  41. struct device_attribute *attr, const char *buf, size_t len)
  42. {
  43. struct nd_btt *nd_btt = to_nd_btt(dev);
  44. ssize_t rc;
  45. nd_device_lock(dev);
  46. nvdimm_bus_lock(dev);
  47. rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
  48. btt_lbasize_supported);
  49. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  50. buf[len - 1] == '\n' ? "" : "\n");
  51. nvdimm_bus_unlock(dev);
  52. nd_device_unlock(dev);
  53. return rc ? rc : len;
  54. }
  55. static DEVICE_ATTR_RW(sector_size);
  56. static ssize_t uuid_show(struct device *dev,
  57. struct device_attribute *attr, char *buf)
  58. {
  59. struct nd_btt *nd_btt = to_nd_btt(dev);
  60. if (nd_btt->uuid)
  61. return sprintf(buf, "%pUb\n", nd_btt->uuid);
  62. return sprintf(buf, "\n");
  63. }
  64. static ssize_t uuid_store(struct device *dev,
  65. struct device_attribute *attr, const char *buf, size_t len)
  66. {
  67. struct nd_btt *nd_btt = to_nd_btt(dev);
  68. ssize_t rc;
  69. nd_device_lock(dev);
  70. rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
  71. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  72. buf[len - 1] == '\n' ? "" : "\n");
  73. nd_device_unlock(dev);
  74. return rc ? rc : len;
  75. }
  76. static DEVICE_ATTR_RW(uuid);
  77. static ssize_t namespace_show(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct nd_btt *nd_btt = to_nd_btt(dev);
  81. ssize_t rc;
  82. nvdimm_bus_lock(dev);
  83. rc = sprintf(buf, "%s\n", nd_btt->ndns
  84. ? dev_name(&nd_btt->ndns->dev) : "");
  85. nvdimm_bus_unlock(dev);
  86. return rc;
  87. }
  88. static ssize_t namespace_store(struct device *dev,
  89. struct device_attribute *attr, const char *buf, size_t len)
  90. {
  91. struct nd_btt *nd_btt = to_nd_btt(dev);
  92. ssize_t rc;
  93. nd_device_lock(dev);
  94. nvdimm_bus_lock(dev);
  95. rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
  96. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  97. buf[len - 1] == '\n' ? "" : "\n");
  98. nvdimm_bus_unlock(dev);
  99. nd_device_unlock(dev);
  100. return rc;
  101. }
  102. static DEVICE_ATTR_RW(namespace);
  103. static ssize_t size_show(struct device *dev,
  104. struct device_attribute *attr, char *buf)
  105. {
  106. struct nd_btt *nd_btt = to_nd_btt(dev);
  107. ssize_t rc;
  108. nd_device_lock(dev);
  109. if (dev->driver)
  110. rc = sprintf(buf, "%llu\n", nd_btt->size);
  111. else {
  112. /* no size to convey if the btt instance is disabled */
  113. rc = -ENXIO;
  114. }
  115. nd_device_unlock(dev);
  116. return rc;
  117. }
  118. static DEVICE_ATTR_RO(size);
  119. static ssize_t log_zero_flags_show(struct device *dev,
  120. struct device_attribute *attr, char *buf)
  121. {
  122. return sprintf(buf, "Y\n");
  123. }
  124. static DEVICE_ATTR_RO(log_zero_flags);
  125. static struct attribute *nd_btt_attributes[] = {
  126. &dev_attr_sector_size.attr,
  127. &dev_attr_namespace.attr,
  128. &dev_attr_uuid.attr,
  129. &dev_attr_size.attr,
  130. &dev_attr_log_zero_flags.attr,
  131. NULL,
  132. };
  133. static struct attribute_group nd_btt_attribute_group = {
  134. .attrs = nd_btt_attributes,
  135. };
  136. static const struct attribute_group *nd_btt_attribute_groups[] = {
  137. &nd_btt_attribute_group,
  138. &nd_device_attribute_group,
  139. &nd_numa_attribute_group,
  140. NULL,
  141. };
  142. static const struct device_type nd_btt_device_type = {
  143. .name = "nd_btt",
  144. .release = nd_btt_release,
  145. .groups = nd_btt_attribute_groups,
  146. };
  147. bool is_nd_btt(struct device *dev)
  148. {
  149. return dev->type == &nd_btt_device_type;
  150. }
  151. EXPORT_SYMBOL(is_nd_btt);
  152. static struct device *__nd_btt_create(struct nd_region *nd_region,
  153. unsigned long lbasize, u8 *uuid,
  154. struct nd_namespace_common *ndns)
  155. {
  156. struct nd_btt *nd_btt;
  157. struct device *dev;
  158. nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
  159. if (!nd_btt)
  160. return NULL;
  161. nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
  162. if (nd_btt->id < 0)
  163. goto out_nd_btt;
  164. nd_btt->lbasize = lbasize;
  165. if (uuid) {
  166. uuid = kmemdup(uuid, 16, GFP_KERNEL);
  167. if (!uuid)
  168. goto out_put_id;
  169. }
  170. nd_btt->uuid = uuid;
  171. dev = &nd_btt->dev;
  172. dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
  173. dev->parent = &nd_region->dev;
  174. dev->type = &nd_btt_device_type;
  175. device_initialize(&nd_btt->dev);
  176. if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
  177. dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
  178. dev_name(ndns->claim));
  179. put_device(dev);
  180. return NULL;
  181. }
  182. return dev;
  183. out_put_id:
  184. ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
  185. out_nd_btt:
  186. kfree(nd_btt);
  187. return NULL;
  188. }
  189. struct device *nd_btt_create(struct nd_region *nd_region)
  190. {
  191. struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
  192. __nd_device_register(dev);
  193. return dev;
  194. }
  195. /**
  196. * nd_btt_arena_is_valid - check if the metadata layout is valid
  197. * @nd_btt: device with BTT geometry and backing device info
  198. * @super: pointer to the arena's info block being tested
  199. *
  200. * Check consistency of the btt info block with itself by validating
  201. * the checksum, and with the parent namespace by verifying the
  202. * parent_uuid contained in the info block with the one supplied in.
  203. *
  204. * Returns:
  205. * false for an invalid info block, true for a valid one
  206. */
  207. bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
  208. {
  209. const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
  210. u64 checksum;
  211. if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
  212. return false;
  213. if (!guid_is_null((guid_t *)&super->parent_uuid))
  214. if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
  215. return false;
  216. checksum = le64_to_cpu(super->checksum);
  217. super->checksum = 0;
  218. if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
  219. return false;
  220. super->checksum = cpu_to_le64(checksum);
  221. /* TODO: figure out action for this */
  222. if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
  223. dev_info(&nd_btt->dev, "Found arena with an error flag\n");
  224. return true;
  225. }
  226. EXPORT_SYMBOL(nd_btt_arena_is_valid);
  227. int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
  228. struct btt_sb *btt_sb)
  229. {
  230. if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
  231. /* Probe/setup for BTT v2.0 */
  232. nd_btt->initial_offset = 0;
  233. nd_btt->version_major = 2;
  234. nd_btt->version_minor = 0;
  235. if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
  236. return -ENXIO;
  237. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  238. return -ENODEV;
  239. if ((le16_to_cpu(btt_sb->version_major) != 2) ||
  240. (le16_to_cpu(btt_sb->version_minor) != 0))
  241. return -ENODEV;
  242. } else {
  243. /*
  244. * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
  245. * NVDIMM_CCLASS_BTT)
  246. */
  247. nd_btt->initial_offset = SZ_4K;
  248. nd_btt->version_major = 1;
  249. nd_btt->version_minor = 1;
  250. if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
  251. return -ENXIO;
  252. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  253. return -ENODEV;
  254. if ((le16_to_cpu(btt_sb->version_major) != 1) ||
  255. (le16_to_cpu(btt_sb->version_minor) != 1))
  256. return -ENODEV;
  257. }
  258. return 0;
  259. }
  260. EXPORT_SYMBOL(nd_btt_version);
  261. static int __nd_btt_probe(struct nd_btt *nd_btt,
  262. struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
  263. {
  264. int rc;
  265. if (!btt_sb || !ndns || !nd_btt)
  266. return -ENODEV;
  267. if (nvdimm_namespace_capacity(ndns) < SZ_16M)
  268. return -ENXIO;
  269. rc = nd_btt_version(nd_btt, ndns, btt_sb);
  270. if (rc < 0)
  271. return rc;
  272. nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
  273. nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
  274. if (!nd_btt->uuid)
  275. return -ENOMEM;
  276. __nd_device_register(&nd_btt->dev);
  277. return 0;
  278. }
  279. int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
  280. {
  281. int rc;
  282. struct device *btt_dev;
  283. struct btt_sb *btt_sb;
  284. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  285. if (ndns->force_raw)
  286. return -ENODEV;
  287. switch (ndns->claim_class) {
  288. case NVDIMM_CCLASS_NONE:
  289. case NVDIMM_CCLASS_BTT:
  290. case NVDIMM_CCLASS_BTT2:
  291. break;
  292. default:
  293. return -ENODEV;
  294. }
  295. nvdimm_bus_lock(&ndns->dev);
  296. btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
  297. nvdimm_bus_unlock(&ndns->dev);
  298. if (!btt_dev)
  299. return -ENOMEM;
  300. btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
  301. rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
  302. dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
  303. if (rc < 0) {
  304. struct nd_btt *nd_btt = to_nd_btt(btt_dev);
  305. nd_detach_ndns(btt_dev, &nd_btt->ndns);
  306. put_device(btt_dev);
  307. }
  308. return rc;
  309. }
  310. EXPORT_SYMBOL(nd_btt_probe);