btt_devs.c 8.8 KB

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