btt_devs.c 9.0 KB

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