claim.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/device.h>
  14. #include <linux/sizes.h>
  15. #include <linux/pmem.h>
  16. #include "nd-core.h"
  17. #include "pfn.h"
  18. #include "btt.h"
  19. #include "nd.h"
  20. void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
  21. {
  22. struct nd_namespace_common *ndns = *_ndns;
  23. struct nvdimm_bus *nvdimm_bus;
  24. if (!ndns)
  25. return;
  26. nvdimm_bus = walk_to_nvdimm_bus(&ndns->dev);
  27. lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
  28. dev_WARN_ONCE(dev, ndns->claim != dev, "%s: invalid claim\n", __func__);
  29. ndns->claim = NULL;
  30. *_ndns = NULL;
  31. put_device(&ndns->dev);
  32. }
  33. void nd_detach_ndns(struct device *dev,
  34. struct nd_namespace_common **_ndns)
  35. {
  36. struct nd_namespace_common *ndns = *_ndns;
  37. if (!ndns)
  38. return;
  39. get_device(&ndns->dev);
  40. nvdimm_bus_lock(&ndns->dev);
  41. __nd_detach_ndns(dev, _ndns);
  42. nvdimm_bus_unlock(&ndns->dev);
  43. put_device(&ndns->dev);
  44. }
  45. bool __nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
  46. struct nd_namespace_common **_ndns)
  47. {
  48. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&attach->dev);
  49. if (attach->claim)
  50. return false;
  51. lockdep_assert_held(&nvdimm_bus->reconfig_mutex);
  52. dev_WARN_ONCE(dev, *_ndns, "%s: invalid claim\n", __func__);
  53. attach->claim = dev;
  54. *_ndns = attach;
  55. get_device(&attach->dev);
  56. return true;
  57. }
  58. bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
  59. struct nd_namespace_common **_ndns)
  60. {
  61. bool claimed;
  62. nvdimm_bus_lock(&attach->dev);
  63. claimed = __nd_attach_ndns(dev, attach, _ndns);
  64. nvdimm_bus_unlock(&attach->dev);
  65. return claimed;
  66. }
  67. static int namespace_match(struct device *dev, void *data)
  68. {
  69. char *name = data;
  70. return strcmp(name, dev_name(dev)) == 0;
  71. }
  72. static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
  73. {
  74. struct nd_region *nd_region = to_nd_region(dev->parent);
  75. struct device *seed = NULL;
  76. if (is_nd_btt(dev))
  77. seed = nd_region->btt_seed;
  78. else if (is_nd_pfn(dev))
  79. seed = nd_region->pfn_seed;
  80. else if (is_nd_dax(dev))
  81. seed = nd_region->dax_seed;
  82. if (seed == dev || ndns || dev->driver)
  83. return false;
  84. return true;
  85. }
  86. struct nd_pfn *to_nd_pfn_safe(struct device *dev)
  87. {
  88. /*
  89. * pfn device attributes are re-used by dax device instances, so we
  90. * need to be careful to correct device-to-nd_pfn conversion.
  91. */
  92. if (is_nd_pfn(dev))
  93. return to_nd_pfn(dev);
  94. if (is_nd_dax(dev)) {
  95. struct nd_dax *nd_dax = to_nd_dax(dev);
  96. return &nd_dax->nd_pfn;
  97. }
  98. WARN_ON(1);
  99. return NULL;
  100. }
  101. static void nd_detach_and_reset(struct device *dev,
  102. struct nd_namespace_common **_ndns)
  103. {
  104. /* detach the namespace and destroy / reset the device */
  105. __nd_detach_ndns(dev, _ndns);
  106. if (is_idle(dev, *_ndns)) {
  107. nd_device_unregister(dev, ND_ASYNC);
  108. } else if (is_nd_btt(dev)) {
  109. struct nd_btt *nd_btt = to_nd_btt(dev);
  110. nd_btt->lbasize = 0;
  111. kfree(nd_btt->uuid);
  112. nd_btt->uuid = NULL;
  113. } else if (is_nd_pfn(dev) || is_nd_dax(dev)) {
  114. struct nd_pfn *nd_pfn = to_nd_pfn_safe(dev);
  115. kfree(nd_pfn->uuid);
  116. nd_pfn->uuid = NULL;
  117. nd_pfn->mode = PFN_MODE_NONE;
  118. }
  119. }
  120. ssize_t nd_namespace_store(struct device *dev,
  121. struct nd_namespace_common **_ndns, const char *buf,
  122. size_t len)
  123. {
  124. struct nd_namespace_common *ndns;
  125. struct device *found;
  126. char *name;
  127. if (dev->driver) {
  128. dev_dbg(dev, "%s: -EBUSY\n", __func__);
  129. return -EBUSY;
  130. }
  131. name = kstrndup(buf, len, GFP_KERNEL);
  132. if (!name)
  133. return -ENOMEM;
  134. strim(name);
  135. if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
  136. /* pass */;
  137. else {
  138. len = -EINVAL;
  139. goto out;
  140. }
  141. ndns = *_ndns;
  142. if (strcmp(name, "") == 0) {
  143. nd_detach_and_reset(dev, _ndns);
  144. goto out;
  145. } else if (ndns) {
  146. dev_dbg(dev, "namespace already set to: %s\n",
  147. dev_name(&ndns->dev));
  148. len = -EBUSY;
  149. goto out;
  150. }
  151. found = device_find_child(dev->parent, name, namespace_match);
  152. if (!found) {
  153. dev_dbg(dev, "'%s' not found under %s\n", name,
  154. dev_name(dev->parent));
  155. len = -ENODEV;
  156. goto out;
  157. }
  158. ndns = to_ndns(found);
  159. if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
  160. dev_dbg(dev, "%s too small to host\n", name);
  161. len = -ENXIO;
  162. goto out_attach;
  163. }
  164. WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
  165. if (!__nd_attach_ndns(dev, ndns, _ndns)) {
  166. dev_dbg(dev, "%s already claimed\n",
  167. dev_name(&ndns->dev));
  168. len = -EBUSY;
  169. }
  170. out_attach:
  171. put_device(&ndns->dev); /* from device_find_child */
  172. out:
  173. kfree(name);
  174. return len;
  175. }
  176. /*
  177. * nd_sb_checksum: compute checksum for a generic info block
  178. *
  179. * Returns a fletcher64 checksum of everything in the given info block
  180. * except the last field (since that's where the checksum lives).
  181. */
  182. u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
  183. {
  184. u64 sum;
  185. __le64 sum_save;
  186. BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
  187. BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
  188. BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
  189. sum_save = nd_gen_sb->checksum;
  190. nd_gen_sb->checksum = 0;
  191. sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
  192. nd_gen_sb->checksum = sum_save;
  193. return sum;
  194. }
  195. EXPORT_SYMBOL(nd_sb_checksum);
  196. static int nsio_rw_bytes(struct nd_namespace_common *ndns,
  197. resource_size_t offset, void *buf, size_t size, int rw)
  198. {
  199. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  200. unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
  201. sector_t sector = offset >> 9;
  202. int rc = 0;
  203. if (unlikely(!size))
  204. return 0;
  205. if (unlikely(offset + size > nsio->size)) {
  206. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  207. return -EFAULT;
  208. }
  209. if (rw == READ) {
  210. if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align)))
  211. return -EIO;
  212. return memcpy_mcsafe(buf, nsio->addr + offset, size);
  213. }
  214. if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) {
  215. /*
  216. * FIXME: nsio_rw_bytes() may be called from atomic
  217. * context in the btt case and the ACPI DSM path for
  218. * clearing the error takes sleeping locks and allocates
  219. * memory. An explicit error clearing path, and support
  220. * for tracking badblocks in BTT metadata is needed to
  221. * work around this collision.
  222. */
  223. if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512)
  224. && (!ndns->claim || !is_nd_btt(ndns->claim))) {
  225. long cleared;
  226. cleared = nvdimm_clear_poison(&ndns->dev,
  227. nsio->res.start + offset, size);
  228. if (cleared < size)
  229. rc = -EIO;
  230. if (cleared > 0 && cleared / 512) {
  231. cleared /= 512;
  232. badblocks_clear(&nsio->bb, sector, cleared);
  233. }
  234. invalidate_pmem(nsio->addr + offset, size);
  235. } else
  236. rc = -EIO;
  237. }
  238. memcpy_to_pmem(nsio->addr + offset, buf, size);
  239. nvdimm_flush(to_nd_region(ndns->dev.parent));
  240. return rc;
  241. }
  242. int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio)
  243. {
  244. struct resource *res = &nsio->res;
  245. struct nd_namespace_common *ndns = &nsio->common;
  246. nsio->size = resource_size(res);
  247. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  248. dev_name(&ndns->dev))) {
  249. dev_warn(dev, "could not reserve region %pR\n", res);
  250. return -EBUSY;
  251. }
  252. ndns->rw_bytes = nsio_rw_bytes;
  253. if (devm_init_badblocks(dev, &nsio->bb))
  254. return -ENOMEM;
  255. nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
  256. &nsio->res);
  257. nsio->addr = devm_memremap(dev, res->start, resource_size(res),
  258. ARCH_MEMREMAP_PMEM);
  259. return PTR_ERR_OR_ZERO(nsio->addr);
  260. }
  261. EXPORT_SYMBOL_GPL(devm_nsio_enable);
  262. void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio)
  263. {
  264. struct resource *res = &nsio->res;
  265. devm_memunmap(dev, nsio->addr);
  266. devm_exit_badblocks(dev, &nsio->bb);
  267. devm_release_mem_region(dev, res->start, resource_size(res));
  268. }
  269. EXPORT_SYMBOL_GPL(devm_nsio_disable);