btt_devs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/blkdev.h>
  14. #include <linux/device.h>
  15. #include <linux/genhd.h>
  16. #include <linux/sizes.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/mm.h>
  20. #include "nd-core.h"
  21. #include "btt.h"
  22. #include "nd.h"
  23. static void nd_btt_release(struct device *dev)
  24. {
  25. struct nd_region *nd_region = to_nd_region(dev->parent);
  26. struct nd_btt *nd_btt = to_nd_btt(dev);
  27. dev_dbg(dev, "trace\n");
  28. nd_detach_ndns(&nd_btt->dev, &nd_btt->ndns);
  29. ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
  30. kfree(nd_btt->uuid);
  31. kfree(nd_btt);
  32. }
  33. static struct device_type nd_btt_device_type = {
  34. .name = "nd_btt",
  35. .release = nd_btt_release,
  36. };
  37. bool is_nd_btt(struct device *dev)
  38. {
  39. return dev->type == &nd_btt_device_type;
  40. }
  41. EXPORT_SYMBOL(is_nd_btt);
  42. struct nd_btt *to_nd_btt(struct device *dev)
  43. {
  44. struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
  45. WARN_ON(!is_nd_btt(dev));
  46. return nd_btt;
  47. }
  48. EXPORT_SYMBOL(to_nd_btt);
  49. static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
  50. 4096, 4104, 4160, 4224, 0 };
  51. static ssize_t sector_size_show(struct device *dev,
  52. struct device_attribute *attr, char *buf)
  53. {
  54. struct nd_btt *nd_btt = to_nd_btt(dev);
  55. return nd_size_select_show(nd_btt->lbasize, btt_lbasize_supported, buf);
  56. }
  57. static ssize_t sector_size_store(struct device *dev,
  58. struct device_attribute *attr, const char *buf, size_t len)
  59. {
  60. struct nd_btt *nd_btt = to_nd_btt(dev);
  61. ssize_t rc;
  62. device_lock(dev);
  63. nvdimm_bus_lock(dev);
  64. rc = nd_size_select_store(dev, buf, &nd_btt->lbasize,
  65. btt_lbasize_supported);
  66. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  67. buf[len - 1] == '\n' ? "" : "\n");
  68. nvdimm_bus_unlock(dev);
  69. device_unlock(dev);
  70. return rc ? rc : len;
  71. }
  72. static DEVICE_ATTR_RW(sector_size);
  73. static ssize_t uuid_show(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct nd_btt *nd_btt = to_nd_btt(dev);
  77. if (nd_btt->uuid)
  78. return sprintf(buf, "%pUb\n", nd_btt->uuid);
  79. return sprintf(buf, "\n");
  80. }
  81. static ssize_t uuid_store(struct device *dev,
  82. struct device_attribute *attr, const char *buf, size_t len)
  83. {
  84. struct nd_btt *nd_btt = to_nd_btt(dev);
  85. ssize_t rc;
  86. device_lock(dev);
  87. rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
  88. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  89. buf[len - 1] == '\n' ? "" : "\n");
  90. device_unlock(dev);
  91. return rc ? rc : len;
  92. }
  93. static DEVICE_ATTR_RW(uuid);
  94. static ssize_t namespace_show(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. struct nd_btt *nd_btt = to_nd_btt(dev);
  98. ssize_t rc;
  99. nvdimm_bus_lock(dev);
  100. rc = sprintf(buf, "%s\n", nd_btt->ndns
  101. ? dev_name(&nd_btt->ndns->dev) : "");
  102. nvdimm_bus_unlock(dev);
  103. return rc;
  104. }
  105. static ssize_t namespace_store(struct device *dev,
  106. struct device_attribute *attr, const char *buf, size_t len)
  107. {
  108. struct nd_btt *nd_btt = to_nd_btt(dev);
  109. ssize_t rc;
  110. device_lock(dev);
  111. nvdimm_bus_lock(dev);
  112. rc = nd_namespace_store(dev, &nd_btt->ndns, buf, len);
  113. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  114. buf[len - 1] == '\n' ? "" : "\n");
  115. nvdimm_bus_unlock(dev);
  116. device_unlock(dev);
  117. return rc;
  118. }
  119. static DEVICE_ATTR_RW(namespace);
  120. static ssize_t size_show(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct nd_btt *nd_btt = to_nd_btt(dev);
  124. ssize_t rc;
  125. device_lock(dev);
  126. if (dev->driver)
  127. rc = sprintf(buf, "%llu\n", nd_btt->size);
  128. else {
  129. /* no size to convey if the btt instance is disabled */
  130. rc = -ENXIO;
  131. }
  132. device_unlock(dev);
  133. return rc;
  134. }
  135. static DEVICE_ATTR_RO(size);
  136. static struct attribute *nd_btt_attributes[] = {
  137. &dev_attr_sector_size.attr,
  138. &dev_attr_namespace.attr,
  139. &dev_attr_uuid.attr,
  140. &dev_attr_size.attr,
  141. NULL,
  142. };
  143. static struct attribute_group nd_btt_attribute_group = {
  144. .attrs = nd_btt_attributes,
  145. };
  146. static const struct attribute_group *nd_btt_attribute_groups[] = {
  147. &nd_btt_attribute_group,
  148. &nd_device_attribute_group,
  149. &nd_numa_attribute_group,
  150. NULL,
  151. };
  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. dev->groups = nd_btt_attribute_groups;
  176. device_initialize(&nd_btt->dev);
  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 u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
  211. u64 checksum;
  212. if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
  213. return false;
  214. if (!guid_is_null((guid_t *)&super->parent_uuid))
  215. if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
  216. return false;
  217. checksum = le64_to_cpu(super->checksum);
  218. super->checksum = 0;
  219. if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
  220. return false;
  221. super->checksum = cpu_to_le64(checksum);
  222. /* TODO: figure out action for this */
  223. if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
  224. dev_info(&nd_btt->dev, "Found arena with an error flag\n");
  225. return true;
  226. }
  227. EXPORT_SYMBOL(nd_btt_arena_is_valid);
  228. int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
  229. struct btt_sb *btt_sb)
  230. {
  231. if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
  232. /* Probe/setup for BTT v2.0 */
  233. nd_btt->initial_offset = 0;
  234. nd_btt->version_major = 2;
  235. nd_btt->version_minor = 0;
  236. if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
  237. return -ENXIO;
  238. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  239. return -ENODEV;
  240. if ((le16_to_cpu(btt_sb->version_major) != 2) ||
  241. (le16_to_cpu(btt_sb->version_minor) != 0))
  242. return -ENODEV;
  243. } else {
  244. /*
  245. * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
  246. * NVDIMM_CCLASS_BTT)
  247. */
  248. nd_btt->initial_offset = SZ_4K;
  249. nd_btt->version_major = 1;
  250. nd_btt->version_minor = 1;
  251. if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
  252. return -ENXIO;
  253. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  254. return -ENODEV;
  255. if ((le16_to_cpu(btt_sb->version_major) != 1) ||
  256. (le16_to_cpu(btt_sb->version_minor) != 1))
  257. return -ENODEV;
  258. }
  259. return 0;
  260. }
  261. EXPORT_SYMBOL(nd_btt_version);
  262. static int __nd_btt_probe(struct nd_btt *nd_btt,
  263. struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
  264. {
  265. int rc;
  266. if (!btt_sb || !ndns || !nd_btt)
  267. return -ENODEV;
  268. if (nvdimm_namespace_capacity(ndns) < SZ_16M)
  269. return -ENXIO;
  270. rc = nd_btt_version(nd_btt, ndns, btt_sb);
  271. if (rc < 0)
  272. return rc;
  273. nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
  274. nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
  275. if (!nd_btt->uuid)
  276. return -ENOMEM;
  277. __nd_device_register(&nd_btt->dev);
  278. return 0;
  279. }
  280. int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
  281. {
  282. int rc;
  283. struct device *btt_dev;
  284. struct btt_sb *btt_sb;
  285. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  286. if (ndns->force_raw)
  287. return -ENODEV;
  288. switch (ndns->claim_class) {
  289. case NVDIMM_CCLASS_NONE:
  290. case NVDIMM_CCLASS_BTT:
  291. case NVDIMM_CCLASS_BTT2:
  292. break;
  293. default:
  294. return -ENODEV;
  295. }
  296. nvdimm_bus_lock(&ndns->dev);
  297. btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
  298. nvdimm_bus_unlock(&ndns->dev);
  299. if (!btt_dev)
  300. return -ENOMEM;
  301. btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
  302. rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
  303. dev_dbg(dev, "btt: %s\n", rc == 0 ? dev_name(btt_dev) : "<none>");
  304. if (rc < 0) {
  305. struct nd_btt *nd_btt = to_nd_btt(btt_dev);
  306. nd_detach_ndns(btt_dev, &nd_btt->ndns);
  307. put_device(btt_dev);
  308. }
  309. return rc;
  310. }
  311. EXPORT_SYMBOL(nd_btt_probe);