pfn_devs.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "pfn.h"
  22. #include "nd.h"
  23. static void nd_pfn_release(struct device *dev)
  24. {
  25. struct nd_region *nd_region = to_nd_region(dev->parent);
  26. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  27. dev_dbg(dev, "%s\n", __func__);
  28. nd_detach_ndns(&nd_pfn->dev, &nd_pfn->ndns);
  29. ida_simple_remove(&nd_region->pfn_ida, nd_pfn->id);
  30. kfree(nd_pfn->uuid);
  31. kfree(nd_pfn);
  32. }
  33. static struct device_type nd_pfn_device_type = {
  34. .name = "nd_pfn",
  35. .release = nd_pfn_release,
  36. };
  37. bool is_nd_pfn(struct device *dev)
  38. {
  39. return dev ? dev->type == &nd_pfn_device_type : false;
  40. }
  41. EXPORT_SYMBOL(is_nd_pfn);
  42. struct nd_pfn *to_nd_pfn(struct device *dev)
  43. {
  44. struct nd_pfn *nd_pfn = container_of(dev, struct nd_pfn, dev);
  45. WARN_ON(!is_nd_pfn(dev));
  46. return nd_pfn;
  47. }
  48. EXPORT_SYMBOL(to_nd_pfn);
  49. static ssize_t mode_show(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  53. switch (nd_pfn->mode) {
  54. case PFN_MODE_RAM:
  55. return sprintf(buf, "ram\n");
  56. case PFN_MODE_PMEM:
  57. return sprintf(buf, "pmem\n");
  58. default:
  59. return sprintf(buf, "none\n");
  60. }
  61. }
  62. static ssize_t mode_store(struct device *dev,
  63. struct device_attribute *attr, const char *buf, size_t len)
  64. {
  65. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  66. ssize_t rc = 0;
  67. device_lock(dev);
  68. nvdimm_bus_lock(dev);
  69. if (dev->driver)
  70. rc = -EBUSY;
  71. else {
  72. size_t n = len - 1;
  73. if (strncmp(buf, "pmem\n", n) == 0
  74. || strncmp(buf, "pmem", n) == 0) {
  75. /* TODO: allocate from PMEM support */
  76. rc = -ENOTTY;
  77. } else if (strncmp(buf, "ram\n", n) == 0
  78. || strncmp(buf, "ram", n) == 0)
  79. nd_pfn->mode = PFN_MODE_RAM;
  80. else if (strncmp(buf, "none\n", n) == 0
  81. || strncmp(buf, "none", n) == 0)
  82. nd_pfn->mode = PFN_MODE_NONE;
  83. else
  84. rc = -EINVAL;
  85. }
  86. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  87. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  88. nvdimm_bus_unlock(dev);
  89. device_unlock(dev);
  90. return rc ? rc : len;
  91. }
  92. static DEVICE_ATTR_RW(mode);
  93. static ssize_t uuid_show(struct device *dev,
  94. struct device_attribute *attr, char *buf)
  95. {
  96. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  97. if (nd_pfn->uuid)
  98. return sprintf(buf, "%pUb\n", nd_pfn->uuid);
  99. return sprintf(buf, "\n");
  100. }
  101. static ssize_t uuid_store(struct device *dev,
  102. struct device_attribute *attr, const char *buf, size_t len)
  103. {
  104. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  105. ssize_t rc;
  106. device_lock(dev);
  107. rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
  108. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  109. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  110. device_unlock(dev);
  111. return rc ? rc : len;
  112. }
  113. static DEVICE_ATTR_RW(uuid);
  114. static ssize_t namespace_show(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  118. ssize_t rc;
  119. nvdimm_bus_lock(dev);
  120. rc = sprintf(buf, "%s\n", nd_pfn->ndns
  121. ? dev_name(&nd_pfn->ndns->dev) : "");
  122. nvdimm_bus_unlock(dev);
  123. return rc;
  124. }
  125. static ssize_t namespace_store(struct device *dev,
  126. struct device_attribute *attr, const char *buf, size_t len)
  127. {
  128. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  129. ssize_t rc;
  130. device_lock(dev);
  131. nvdimm_bus_lock(dev);
  132. rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
  133. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  134. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  135. nvdimm_bus_unlock(dev);
  136. device_unlock(dev);
  137. return rc;
  138. }
  139. static DEVICE_ATTR_RW(namespace);
  140. static struct attribute *nd_pfn_attributes[] = {
  141. &dev_attr_mode.attr,
  142. &dev_attr_namespace.attr,
  143. &dev_attr_uuid.attr,
  144. NULL,
  145. };
  146. static struct attribute_group nd_pfn_attribute_group = {
  147. .attrs = nd_pfn_attributes,
  148. };
  149. static const struct attribute_group *nd_pfn_attribute_groups[] = {
  150. &nd_pfn_attribute_group,
  151. &nd_device_attribute_group,
  152. &nd_numa_attribute_group,
  153. NULL,
  154. };
  155. static struct device *__nd_pfn_create(struct nd_region *nd_region,
  156. u8 *uuid, enum nd_pfn_mode mode,
  157. struct nd_namespace_common *ndns)
  158. {
  159. struct nd_pfn *nd_pfn;
  160. struct device *dev;
  161. /* we can only create pages for contiguous ranged of pmem */
  162. if (!is_nd_pmem(&nd_region->dev))
  163. return NULL;
  164. nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
  165. if (!nd_pfn)
  166. return NULL;
  167. nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
  168. if (nd_pfn->id < 0) {
  169. kfree(nd_pfn);
  170. return NULL;
  171. }
  172. nd_pfn->mode = mode;
  173. if (uuid)
  174. uuid = kmemdup(uuid, 16, GFP_KERNEL);
  175. nd_pfn->uuid = uuid;
  176. dev = &nd_pfn->dev;
  177. dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
  178. dev->parent = &nd_region->dev;
  179. dev->type = &nd_pfn_device_type;
  180. dev->groups = nd_pfn_attribute_groups;
  181. device_initialize(&nd_pfn->dev);
  182. if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
  183. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  184. __func__, dev_name(ndns->claim));
  185. put_device(dev);
  186. return NULL;
  187. }
  188. return dev;
  189. }
  190. struct device *nd_pfn_create(struct nd_region *nd_region)
  191. {
  192. struct device *dev = __nd_pfn_create(nd_region, NULL, PFN_MODE_NONE,
  193. NULL);
  194. if (dev)
  195. __nd_device_register(dev);
  196. return dev;
  197. }
  198. int nd_pfn_validate(struct nd_pfn *nd_pfn)
  199. {
  200. struct nd_namespace_common *ndns = nd_pfn->ndns;
  201. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  202. struct nd_namespace_io *nsio;
  203. u64 checksum, offset;
  204. if (!pfn_sb || !ndns)
  205. return -ENODEV;
  206. if (!is_nd_pmem(nd_pfn->dev.parent))
  207. return -ENODEV;
  208. /* section alignment for simple hotplug */
  209. if (nvdimm_namespace_capacity(ndns) < ND_PFN_ALIGN)
  210. return -ENODEV;
  211. if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
  212. return -ENXIO;
  213. if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
  214. return -ENODEV;
  215. checksum = le64_to_cpu(pfn_sb->checksum);
  216. pfn_sb->checksum = 0;
  217. if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
  218. return -ENODEV;
  219. pfn_sb->checksum = cpu_to_le64(checksum);
  220. switch (le32_to_cpu(pfn_sb->mode)) {
  221. case PFN_MODE_RAM:
  222. break;
  223. case PFN_MODE_PMEM:
  224. /* TODO: allocate from PMEM support */
  225. return -ENOTTY;
  226. default:
  227. return -ENXIO;
  228. }
  229. if (!nd_pfn->uuid) {
  230. /* from probe we allocate */
  231. nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
  232. if (!nd_pfn->uuid)
  233. return -ENOMEM;
  234. } else {
  235. /* from init we validate */
  236. if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
  237. return -ENODEV;
  238. }
  239. /*
  240. * These warnings are verbose because they can only trigger in
  241. * the case where the physical address alignment of the
  242. * namespace has changed since the pfn superblock was
  243. * established.
  244. */
  245. offset = le64_to_cpu(pfn_sb->dataoff);
  246. nsio = to_nd_namespace_io(&ndns->dev);
  247. if (nsio->res.start & ND_PFN_MASK) {
  248. dev_err(&nd_pfn->dev,
  249. "init failed: %s not section aligned\n",
  250. dev_name(&ndns->dev));
  251. return -EBUSY;
  252. } else if (offset >= resource_size(&nsio->res)) {
  253. dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
  254. dev_name(&ndns->dev));
  255. return -EBUSY;
  256. }
  257. return 0;
  258. }
  259. EXPORT_SYMBOL(nd_pfn_validate);
  260. int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
  261. {
  262. int rc;
  263. struct device *dev;
  264. struct nd_pfn *nd_pfn;
  265. struct nd_pfn_sb *pfn_sb;
  266. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  267. if (ndns->force_raw)
  268. return -ENODEV;
  269. nvdimm_bus_lock(&ndns->dev);
  270. dev = __nd_pfn_create(nd_region, NULL, PFN_MODE_NONE, ndns);
  271. nvdimm_bus_unlock(&ndns->dev);
  272. if (!dev)
  273. return -ENOMEM;
  274. dev_set_drvdata(dev, drvdata);
  275. pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
  276. nd_pfn = to_nd_pfn(dev);
  277. nd_pfn->pfn_sb = pfn_sb;
  278. rc = nd_pfn_validate(nd_pfn);
  279. nd_pfn->pfn_sb = NULL;
  280. kfree(pfn_sb);
  281. dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
  282. rc == 0 ? dev_name(dev) : "<none>");
  283. if (rc < 0) {
  284. __nd_detach_ndns(dev, &nd_pfn->ndns);
  285. put_device(dev);
  286. } else
  287. __nd_device_register(&nd_pfn->dev);
  288. return rc;
  289. }
  290. EXPORT_SYMBOL(nd_pfn_probe);