pfn_devs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. nd_pfn->mode = PFN_MODE_PMEM;
  76. } else if (strncmp(buf, "ram\n", n) == 0
  77. || strncmp(buf, "ram", n) == 0)
  78. nd_pfn->mode = PFN_MODE_RAM;
  79. else if (strncmp(buf, "none\n", n) == 0
  80. || strncmp(buf, "none", n) == 0)
  81. nd_pfn->mode = PFN_MODE_NONE;
  82. else
  83. rc = -EINVAL;
  84. }
  85. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  86. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  87. nvdimm_bus_unlock(dev);
  88. device_unlock(dev);
  89. return rc ? rc : len;
  90. }
  91. static DEVICE_ATTR_RW(mode);
  92. static ssize_t align_show(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  96. return sprintf(buf, "%lx\n", nd_pfn->align);
  97. }
  98. static ssize_t __align_store(struct nd_pfn *nd_pfn, const char *buf)
  99. {
  100. unsigned long val;
  101. int rc;
  102. rc = kstrtoul(buf, 0, &val);
  103. if (rc)
  104. return rc;
  105. if (!is_power_of_2(val) || val < PAGE_SIZE || val > SZ_1G)
  106. return -EINVAL;
  107. if (nd_pfn->dev.driver)
  108. return -EBUSY;
  109. else
  110. nd_pfn->align = val;
  111. return 0;
  112. }
  113. static ssize_t align_store(struct device *dev,
  114. struct device_attribute *attr, const char *buf, size_t len)
  115. {
  116. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  117. ssize_t rc;
  118. device_lock(dev);
  119. nvdimm_bus_lock(dev);
  120. rc = __align_store(nd_pfn, buf);
  121. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  122. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  123. nvdimm_bus_unlock(dev);
  124. device_unlock(dev);
  125. return rc ? rc : len;
  126. }
  127. static DEVICE_ATTR_RW(align);
  128. static ssize_t uuid_show(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  132. if (nd_pfn->uuid)
  133. return sprintf(buf, "%pUb\n", nd_pfn->uuid);
  134. return sprintf(buf, "\n");
  135. }
  136. static ssize_t uuid_store(struct device *dev,
  137. struct device_attribute *attr, const char *buf, size_t len)
  138. {
  139. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  140. ssize_t rc;
  141. device_lock(dev);
  142. rc = nd_uuid_store(dev, &nd_pfn->uuid, buf, len);
  143. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  144. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  145. device_unlock(dev);
  146. return rc ? rc : len;
  147. }
  148. static DEVICE_ATTR_RW(uuid);
  149. static ssize_t namespace_show(struct device *dev,
  150. struct device_attribute *attr, char *buf)
  151. {
  152. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  153. ssize_t rc;
  154. nvdimm_bus_lock(dev);
  155. rc = sprintf(buf, "%s\n", nd_pfn->ndns
  156. ? dev_name(&nd_pfn->ndns->dev) : "");
  157. nvdimm_bus_unlock(dev);
  158. return rc;
  159. }
  160. static ssize_t namespace_store(struct device *dev,
  161. struct device_attribute *attr, const char *buf, size_t len)
  162. {
  163. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  164. ssize_t rc;
  165. device_lock(dev);
  166. nvdimm_bus_lock(dev);
  167. rc = nd_namespace_store(dev, &nd_pfn->ndns, buf, len);
  168. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  169. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  170. nvdimm_bus_unlock(dev);
  171. device_unlock(dev);
  172. return rc;
  173. }
  174. static DEVICE_ATTR_RW(namespace);
  175. static struct attribute *nd_pfn_attributes[] = {
  176. &dev_attr_mode.attr,
  177. &dev_attr_namespace.attr,
  178. &dev_attr_uuid.attr,
  179. &dev_attr_align.attr,
  180. NULL,
  181. };
  182. static struct attribute_group nd_pfn_attribute_group = {
  183. .attrs = nd_pfn_attributes,
  184. };
  185. static const struct attribute_group *nd_pfn_attribute_groups[] = {
  186. &nd_pfn_attribute_group,
  187. &nd_device_attribute_group,
  188. &nd_numa_attribute_group,
  189. NULL,
  190. };
  191. static struct device *__nd_pfn_create(struct nd_region *nd_region,
  192. struct nd_namespace_common *ndns)
  193. {
  194. struct nd_pfn *nd_pfn;
  195. struct device *dev;
  196. /* we can only create pages for contiguous ranged of pmem */
  197. if (!is_nd_pmem(&nd_region->dev))
  198. return NULL;
  199. nd_pfn = kzalloc(sizeof(*nd_pfn), GFP_KERNEL);
  200. if (!nd_pfn)
  201. return NULL;
  202. nd_pfn->id = ida_simple_get(&nd_region->pfn_ida, 0, 0, GFP_KERNEL);
  203. if (nd_pfn->id < 0) {
  204. kfree(nd_pfn);
  205. return NULL;
  206. }
  207. nd_pfn->mode = PFN_MODE_NONE;
  208. nd_pfn->align = HPAGE_SIZE;
  209. dev = &nd_pfn->dev;
  210. dev_set_name(dev, "pfn%d.%d", nd_region->id, nd_pfn->id);
  211. dev->parent = &nd_region->dev;
  212. dev->type = &nd_pfn_device_type;
  213. dev->groups = nd_pfn_attribute_groups;
  214. device_initialize(&nd_pfn->dev);
  215. if (ndns && !__nd_attach_ndns(&nd_pfn->dev, ndns, &nd_pfn->ndns)) {
  216. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  217. __func__, dev_name(ndns->claim));
  218. put_device(dev);
  219. return NULL;
  220. }
  221. return dev;
  222. }
  223. struct device *nd_pfn_create(struct nd_region *nd_region)
  224. {
  225. struct device *dev = __nd_pfn_create(nd_region, NULL);
  226. if (dev)
  227. __nd_device_register(dev);
  228. return dev;
  229. }
  230. int nd_pfn_validate(struct nd_pfn *nd_pfn)
  231. {
  232. u64 checksum, offset;
  233. struct nd_namespace_io *nsio;
  234. struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
  235. struct nd_namespace_common *ndns = nd_pfn->ndns;
  236. const u8 *parent_uuid = nd_dev_to_uuid(&ndns->dev);
  237. if (!pfn_sb || !ndns)
  238. return -ENODEV;
  239. if (!is_nd_pmem(nd_pfn->dev.parent))
  240. return -ENODEV;
  241. if (nvdimm_read_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb)))
  242. return -ENXIO;
  243. if (memcmp(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN) != 0)
  244. return -ENODEV;
  245. checksum = le64_to_cpu(pfn_sb->checksum);
  246. pfn_sb->checksum = 0;
  247. if (checksum != nd_sb_checksum((struct nd_gen_sb *) pfn_sb))
  248. return -ENODEV;
  249. pfn_sb->checksum = cpu_to_le64(checksum);
  250. if (memcmp(pfn_sb->parent_uuid, parent_uuid, 16) != 0)
  251. return -ENODEV;
  252. switch (le32_to_cpu(pfn_sb->mode)) {
  253. case PFN_MODE_RAM:
  254. break;
  255. case PFN_MODE_PMEM:
  256. /* TODO: allocate from PMEM support */
  257. return -ENOTTY;
  258. default:
  259. return -ENXIO;
  260. }
  261. if (!nd_pfn->uuid) {
  262. /* from probe we allocate */
  263. nd_pfn->uuid = kmemdup(pfn_sb->uuid, 16, GFP_KERNEL);
  264. if (!nd_pfn->uuid)
  265. return -ENOMEM;
  266. } else {
  267. /* from init we validate */
  268. if (memcmp(nd_pfn->uuid, pfn_sb->uuid, 16) != 0)
  269. return -EINVAL;
  270. }
  271. if (nd_pfn->align > nvdimm_namespace_capacity(ndns)) {
  272. dev_err(&nd_pfn->dev, "alignment: %lx exceeds capacity %llx\n",
  273. nd_pfn->align, nvdimm_namespace_capacity(ndns));
  274. return -EINVAL;
  275. }
  276. /*
  277. * These warnings are verbose because they can only trigger in
  278. * the case where the physical address alignment of the
  279. * namespace has changed since the pfn superblock was
  280. * established.
  281. */
  282. offset = le64_to_cpu(pfn_sb->dataoff);
  283. nsio = to_nd_namespace_io(&ndns->dev);
  284. if (offset >= resource_size(&nsio->res)) {
  285. dev_err(&nd_pfn->dev, "pfn array size exceeds capacity of %s\n",
  286. dev_name(&ndns->dev));
  287. return -EBUSY;
  288. }
  289. nd_pfn->align = 1UL << ilog2(offset);
  290. if (!is_power_of_2(offset) || offset < PAGE_SIZE) {
  291. dev_err(&nd_pfn->dev, "bad offset: %#llx dax disabled\n",
  292. offset);
  293. return -ENXIO;
  294. }
  295. return 0;
  296. }
  297. EXPORT_SYMBOL(nd_pfn_validate);
  298. int nd_pfn_probe(struct nd_namespace_common *ndns, void *drvdata)
  299. {
  300. int rc;
  301. struct device *dev;
  302. struct nd_pfn *nd_pfn;
  303. struct nd_pfn_sb *pfn_sb;
  304. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  305. if (ndns->force_raw)
  306. return -ENODEV;
  307. nvdimm_bus_lock(&ndns->dev);
  308. dev = __nd_pfn_create(nd_region, ndns);
  309. nvdimm_bus_unlock(&ndns->dev);
  310. if (!dev)
  311. return -ENOMEM;
  312. dev_set_drvdata(dev, drvdata);
  313. pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
  314. nd_pfn = to_nd_pfn(dev);
  315. nd_pfn->pfn_sb = pfn_sb;
  316. rc = nd_pfn_validate(nd_pfn);
  317. nd_pfn->pfn_sb = NULL;
  318. kfree(pfn_sb);
  319. dev_dbg(&ndns->dev, "%s: pfn: %s\n", __func__,
  320. rc == 0 ? dev_name(dev) : "<none>");
  321. if (rc < 0) {
  322. __nd_detach_ndns(dev, &nd_pfn->ndns);
  323. put_device(dev);
  324. } else
  325. __nd_device_register(&nd_pfn->dev);
  326. return rc;
  327. }
  328. EXPORT_SYMBOL(nd_pfn_probe);