btt_devs.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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, "%s\n", __func__);
  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, "%s: result: %zd wrote: %s%s", __func__,
  67. rc, buf, 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, "%s: result: %zd wrote: %s%s", __func__,
  89. rc, buf, 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, "%s: result: %zd wrote: %s%s", __func__,
  114. rc, buf, 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 ssize_t log_zero_flags_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. return sprintf(buf, "Y\n");
  140. }
  141. static DEVICE_ATTR_RO(log_zero_flags);
  142. static struct attribute *nd_btt_attributes[] = {
  143. &dev_attr_sector_size.attr,
  144. &dev_attr_namespace.attr,
  145. &dev_attr_uuid.attr,
  146. &dev_attr_size.attr,
  147. &dev_attr_log_zero_flags.attr,
  148. NULL,
  149. };
  150. static struct attribute_group nd_btt_attribute_group = {
  151. .attrs = nd_btt_attributes,
  152. };
  153. static const struct attribute_group *nd_btt_attribute_groups[] = {
  154. &nd_btt_attribute_group,
  155. &nd_device_attribute_group,
  156. &nd_numa_attribute_group,
  157. NULL,
  158. };
  159. static struct device *__nd_btt_create(struct nd_region *nd_region,
  160. unsigned long lbasize, u8 *uuid,
  161. struct nd_namespace_common *ndns)
  162. {
  163. struct nd_btt *nd_btt;
  164. struct device *dev;
  165. nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
  166. if (!nd_btt)
  167. return NULL;
  168. nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
  169. if (nd_btt->id < 0)
  170. goto out_nd_btt;
  171. nd_btt->lbasize = lbasize;
  172. if (uuid) {
  173. uuid = kmemdup(uuid, 16, GFP_KERNEL);
  174. if (!uuid)
  175. goto out_put_id;
  176. }
  177. nd_btt->uuid = uuid;
  178. dev = &nd_btt->dev;
  179. dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
  180. dev->parent = &nd_region->dev;
  181. dev->type = &nd_btt_device_type;
  182. dev->groups = nd_btt_attribute_groups;
  183. device_initialize(&nd_btt->dev);
  184. if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
  185. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  186. __func__, dev_name(ndns->claim));
  187. put_device(dev);
  188. return NULL;
  189. }
  190. return dev;
  191. out_put_id:
  192. ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
  193. out_nd_btt:
  194. kfree(nd_btt);
  195. return NULL;
  196. }
  197. struct device *nd_btt_create(struct nd_region *nd_region)
  198. {
  199. struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
  200. __nd_device_register(dev);
  201. return dev;
  202. }
  203. /**
  204. * nd_btt_arena_is_valid - check if the metadata layout is valid
  205. * @nd_btt: device with BTT geometry and backing device info
  206. * @super: pointer to the arena's info block being tested
  207. *
  208. * Check consistency of the btt info block with itself by validating
  209. * the checksum, and with the parent namespace by verifying the
  210. * parent_uuid contained in the info block with the one supplied in.
  211. *
  212. * Returns:
  213. * false for an invalid info block, true for a valid one
  214. */
  215. bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
  216. {
  217. const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
  218. u64 checksum;
  219. if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
  220. return false;
  221. if (!guid_is_null((guid_t *)&super->parent_uuid))
  222. if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
  223. return false;
  224. checksum = le64_to_cpu(super->checksum);
  225. super->checksum = 0;
  226. if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
  227. return false;
  228. super->checksum = cpu_to_le64(checksum);
  229. /* TODO: figure out action for this */
  230. if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
  231. dev_info(&nd_btt->dev, "Found arena with an error flag\n");
  232. return true;
  233. }
  234. EXPORT_SYMBOL(nd_btt_arena_is_valid);
  235. int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
  236. struct btt_sb *btt_sb)
  237. {
  238. if (ndns->claim_class == NVDIMM_CCLASS_BTT2) {
  239. /* Probe/setup for BTT v2.0 */
  240. nd_btt->initial_offset = 0;
  241. nd_btt->version_major = 2;
  242. nd_btt->version_minor = 0;
  243. if (nvdimm_read_bytes(ndns, 0, btt_sb, sizeof(*btt_sb), 0))
  244. return -ENXIO;
  245. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  246. return -ENODEV;
  247. if ((le16_to_cpu(btt_sb->version_major) != 2) ||
  248. (le16_to_cpu(btt_sb->version_minor) != 0))
  249. return -ENODEV;
  250. } else {
  251. /*
  252. * Probe/setup for BTT v1.1 (NVDIMM_CCLASS_NONE or
  253. * NVDIMM_CCLASS_BTT)
  254. */
  255. nd_btt->initial_offset = SZ_4K;
  256. nd_btt->version_major = 1;
  257. nd_btt->version_minor = 1;
  258. if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb), 0))
  259. return -ENXIO;
  260. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  261. return -ENODEV;
  262. if ((le16_to_cpu(btt_sb->version_major) != 1) ||
  263. (le16_to_cpu(btt_sb->version_minor) != 1))
  264. return -ENODEV;
  265. }
  266. return 0;
  267. }
  268. EXPORT_SYMBOL(nd_btt_version);
  269. static int __nd_btt_probe(struct nd_btt *nd_btt,
  270. struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
  271. {
  272. int rc;
  273. if (!btt_sb || !ndns || !nd_btt)
  274. return -ENODEV;
  275. if (nvdimm_namespace_capacity(ndns) < SZ_16M)
  276. return -ENXIO;
  277. rc = nd_btt_version(nd_btt, ndns, btt_sb);
  278. if (rc < 0)
  279. return rc;
  280. nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
  281. nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
  282. if (!nd_btt->uuid)
  283. return -ENOMEM;
  284. __nd_device_register(&nd_btt->dev);
  285. return 0;
  286. }
  287. int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
  288. {
  289. int rc;
  290. struct device *btt_dev;
  291. struct btt_sb *btt_sb;
  292. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  293. if (ndns->force_raw)
  294. return -ENODEV;
  295. switch (ndns->claim_class) {
  296. case NVDIMM_CCLASS_NONE:
  297. case NVDIMM_CCLASS_BTT:
  298. case NVDIMM_CCLASS_BTT2:
  299. break;
  300. default:
  301. return -ENODEV;
  302. }
  303. nvdimm_bus_lock(&ndns->dev);
  304. btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
  305. nvdimm_bus_unlock(&ndns->dev);
  306. if (!btt_dev)
  307. return -ENOMEM;
  308. btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
  309. rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
  310. dev_dbg(dev, "%s: btt: %s\n", __func__,
  311. rc == 0 ? dev_name(btt_dev) : "<none>");
  312. if (rc < 0) {
  313. struct nd_btt *nd_btt = to_nd_btt(btt_dev);
  314. nd_detach_ndns(btt_dev, &nd_btt->ndns);
  315. put_device(btt_dev);
  316. }
  317. return rc;
  318. }
  319. EXPORT_SYMBOL(nd_btt_probe);