claim.c 7.2 KB

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