btt_devs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_sector_size_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_sector_size_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 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. kfree(nd_btt);
  164. return NULL;
  165. }
  166. nd_btt->lbasize = lbasize;
  167. if (uuid)
  168. uuid = kmemdup(uuid, 16, GFP_KERNEL);
  169. nd_btt->uuid = uuid;
  170. dev = &nd_btt->dev;
  171. dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
  172. dev->parent = &nd_region->dev;
  173. dev->type = &nd_btt_device_type;
  174. dev->groups = nd_btt_attribute_groups;
  175. device_initialize(&nd_btt->dev);
  176. if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
  177. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  178. __func__, dev_name(ndns->claim));
  179. put_device(dev);
  180. return NULL;
  181. }
  182. return dev;
  183. }
  184. struct device *nd_btt_create(struct nd_region *nd_region)
  185. {
  186. struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
  187. __nd_device_register(dev);
  188. return dev;
  189. }
  190. static bool uuid_is_null(u8 *uuid)
  191. {
  192. static const u8 null_uuid[16];
  193. return (memcmp(uuid, null_uuid, 16) == 0);
  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 (!uuid_is_null(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. static int __nd_btt_probe(struct nd_btt *nd_btt,
  228. struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
  229. {
  230. if (!btt_sb || !ndns || !nd_btt)
  231. return -ENODEV;
  232. if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb)))
  233. return -ENXIO;
  234. if (nvdimm_namespace_capacity(ndns) < SZ_16M)
  235. return -ENXIO;
  236. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  237. return -ENODEV;
  238. nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
  239. nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
  240. if (!nd_btt->uuid)
  241. return -ENOMEM;
  242. __nd_device_register(&nd_btt->dev);
  243. return 0;
  244. }
  245. int nd_btt_probe(struct device *dev, struct nd_namespace_common *ndns)
  246. {
  247. int rc;
  248. struct device *btt_dev;
  249. struct btt_sb *btt_sb;
  250. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  251. if (ndns->force_raw)
  252. return -ENODEV;
  253. nvdimm_bus_lock(&ndns->dev);
  254. btt_dev = __nd_btt_create(nd_region, 0, NULL, ndns);
  255. nvdimm_bus_unlock(&ndns->dev);
  256. if (!btt_dev)
  257. return -ENOMEM;
  258. btt_sb = devm_kzalloc(dev, sizeof(*btt_sb), GFP_KERNEL);
  259. rc = __nd_btt_probe(to_nd_btt(btt_dev), ndns, btt_sb);
  260. dev_dbg(dev, "%s: btt: %s\n", __func__,
  261. rc == 0 ? dev_name(btt_dev) : "<none>");
  262. if (rc < 0) {
  263. struct nd_btt *nd_btt = to_nd_btt(btt_dev);
  264. __nd_detach_ndns(btt_dev, &nd_btt->ndns);
  265. put_device(btt_dev);
  266. }
  267. return rc;
  268. }
  269. EXPORT_SYMBOL(nd_btt_probe);