btt_devs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 struct attribute *nd_btt_attributes[] = {
  121. &dev_attr_sector_size.attr,
  122. &dev_attr_namespace.attr,
  123. &dev_attr_uuid.attr,
  124. NULL,
  125. };
  126. static struct attribute_group nd_btt_attribute_group = {
  127. .attrs = nd_btt_attributes,
  128. };
  129. static const struct attribute_group *nd_btt_attribute_groups[] = {
  130. &nd_btt_attribute_group,
  131. &nd_device_attribute_group,
  132. &nd_numa_attribute_group,
  133. NULL,
  134. };
  135. static struct device *__nd_btt_create(struct nd_region *nd_region,
  136. unsigned long lbasize, u8 *uuid,
  137. struct nd_namespace_common *ndns)
  138. {
  139. struct nd_btt *nd_btt;
  140. struct device *dev;
  141. nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
  142. if (!nd_btt)
  143. return NULL;
  144. nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
  145. if (nd_btt->id < 0) {
  146. kfree(nd_btt);
  147. return NULL;
  148. }
  149. nd_btt->lbasize = lbasize;
  150. if (uuid)
  151. uuid = kmemdup(uuid, 16, GFP_KERNEL);
  152. nd_btt->uuid = uuid;
  153. dev = &nd_btt->dev;
  154. dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
  155. dev->parent = &nd_region->dev;
  156. dev->type = &nd_btt_device_type;
  157. dev->groups = nd_btt_attribute_groups;
  158. device_initialize(&nd_btt->dev);
  159. if (ndns && !__nd_attach_ndns(&nd_btt->dev, ndns, &nd_btt->ndns)) {
  160. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  161. __func__, dev_name(ndns->claim));
  162. put_device(dev);
  163. return NULL;
  164. }
  165. return dev;
  166. }
  167. struct device *nd_btt_create(struct nd_region *nd_region)
  168. {
  169. struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
  170. if (dev)
  171. __nd_device_register(dev);
  172. return dev;
  173. }
  174. static bool uuid_is_null(u8 *uuid)
  175. {
  176. static const u8 null_uuid[16];
  177. return (memcmp(uuid, null_uuid, 16) == 0);
  178. }
  179. /**
  180. * nd_btt_arena_is_valid - check if the metadata layout is valid
  181. * @nd_btt: device with BTT geometry and backing device info
  182. * @super: pointer to the arena's info block being tested
  183. *
  184. * Check consistency of the btt info block with itself by validating
  185. * the checksum, and with the parent namespace by verifying the
  186. * parent_uuid contained in the info block with the one supplied in.
  187. *
  188. * Returns:
  189. * false for an invalid info block, true for a valid one
  190. */
  191. bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super)
  192. {
  193. const u8 *parent_uuid = nd_dev_to_uuid(&nd_btt->ndns->dev);
  194. u64 checksum;
  195. if (memcmp(super->signature, BTT_SIG, BTT_SIG_LEN) != 0)
  196. return false;
  197. if (!uuid_is_null(super->parent_uuid))
  198. if (memcmp(super->parent_uuid, parent_uuid, 16) != 0)
  199. return false;
  200. checksum = le64_to_cpu(super->checksum);
  201. super->checksum = 0;
  202. if (checksum != nd_sb_checksum((struct nd_gen_sb *) super))
  203. return false;
  204. super->checksum = cpu_to_le64(checksum);
  205. /* TODO: figure out action for this */
  206. if ((le32_to_cpu(super->flags) & IB_FLAG_ERROR_MASK) != 0)
  207. dev_info(&nd_btt->dev, "Found arena with an error flag\n");
  208. return true;
  209. }
  210. EXPORT_SYMBOL(nd_btt_arena_is_valid);
  211. static int __nd_btt_probe(struct nd_btt *nd_btt,
  212. struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
  213. {
  214. if (!btt_sb || !ndns || !nd_btt)
  215. return -ENODEV;
  216. if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb)))
  217. return -ENXIO;
  218. if (nvdimm_namespace_capacity(ndns) < SZ_16M)
  219. return -ENXIO;
  220. if (!nd_btt_arena_is_valid(nd_btt, btt_sb))
  221. return -ENODEV;
  222. nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
  223. nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
  224. if (!nd_btt->uuid)
  225. return -ENOMEM;
  226. __nd_device_register(&nd_btt->dev);
  227. return 0;
  228. }
  229. int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata)
  230. {
  231. int rc;
  232. struct device *dev;
  233. struct btt_sb *btt_sb;
  234. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  235. if (ndns->force_raw)
  236. return -ENODEV;
  237. nvdimm_bus_lock(&ndns->dev);
  238. dev = __nd_btt_create(nd_region, 0, NULL, ndns);
  239. nvdimm_bus_unlock(&ndns->dev);
  240. if (!dev)
  241. return -ENOMEM;
  242. dev_set_drvdata(dev, drvdata);
  243. btt_sb = kzalloc(sizeof(*btt_sb), GFP_KERNEL);
  244. rc = __nd_btt_probe(to_nd_btt(dev), ndns, btt_sb);
  245. kfree(btt_sb);
  246. dev_dbg(&ndns->dev, "%s: btt: %s\n", __func__,
  247. rc == 0 ? dev_name(dev) : "<none>");
  248. if (rc < 0) {
  249. struct nd_btt *nd_btt = to_nd_btt(dev);
  250. __nd_detach_ndns(dev, &nd_btt->ndns);
  251. put_device(dev);
  252. }
  253. return rc;
  254. }
  255. EXPORT_SYMBOL(nd_btt_probe);