claim.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 "nd-core.h"
  16. #include "pmem.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, "namespace already active\n");
  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. switch (ndns->claim_class) {
  160. case NVDIMM_CCLASS_NONE:
  161. break;
  162. case NVDIMM_CCLASS_BTT:
  163. case NVDIMM_CCLASS_BTT2:
  164. if (!is_nd_btt(dev)) {
  165. len = -EBUSY;
  166. goto out_attach;
  167. }
  168. break;
  169. case NVDIMM_CCLASS_PFN:
  170. if (!is_nd_pfn(dev)) {
  171. len = -EBUSY;
  172. goto out_attach;
  173. }
  174. break;
  175. case NVDIMM_CCLASS_DAX:
  176. if (!is_nd_dax(dev)) {
  177. len = -EBUSY;
  178. goto out_attach;
  179. }
  180. break;
  181. default:
  182. len = -EBUSY;
  183. goto out_attach;
  184. break;
  185. }
  186. if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
  187. dev_dbg(dev, "%s too small to host\n", name);
  188. len = -ENXIO;
  189. goto out_attach;
  190. }
  191. WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
  192. if (!__nd_attach_ndns(dev, ndns, _ndns)) {
  193. dev_dbg(dev, "%s already claimed\n",
  194. dev_name(&ndns->dev));
  195. len = -EBUSY;
  196. }
  197. out_attach:
  198. put_device(&ndns->dev); /* from device_find_child */
  199. out:
  200. kfree(name);
  201. return len;
  202. }
  203. /*
  204. * nd_sb_checksum: compute checksum for a generic info block
  205. *
  206. * Returns a fletcher64 checksum of everything in the given info block
  207. * except the last field (since that's where the checksum lives).
  208. */
  209. u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
  210. {
  211. u64 sum;
  212. __le64 sum_save;
  213. BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
  214. BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
  215. BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
  216. sum_save = nd_gen_sb->checksum;
  217. nd_gen_sb->checksum = 0;
  218. sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
  219. nd_gen_sb->checksum = sum_save;
  220. return sum;
  221. }
  222. EXPORT_SYMBOL(nd_sb_checksum);
  223. static int nsio_rw_bytes(struct nd_namespace_common *ndns,
  224. resource_size_t offset, void *buf, size_t size, int rw,
  225. unsigned long flags)
  226. {
  227. struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
  228. unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
  229. sector_t sector = offset >> 9;
  230. int rc = 0;
  231. if (unlikely(!size))
  232. return 0;
  233. if (unlikely(offset + size > nsio->size)) {
  234. dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
  235. return -EFAULT;
  236. }
  237. if (rw == READ) {
  238. if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align)))
  239. return -EIO;
  240. if (memcpy_mcsafe(buf, nsio->addr + offset, size) != 0)
  241. return -EIO;
  242. return 0;
  243. }
  244. if (unlikely(is_bad_pmem(&nsio->bb, sector, sz_align))) {
  245. if (IS_ALIGNED(offset, 512) && IS_ALIGNED(size, 512)
  246. && !(flags & NVDIMM_IO_ATOMIC)) {
  247. long cleared;
  248. might_sleep();
  249. cleared = nvdimm_clear_poison(&ndns->dev,
  250. nsio->res.start + offset, size);
  251. if (cleared < size)
  252. rc = -EIO;
  253. if (cleared > 0 && cleared / 512) {
  254. cleared /= 512;
  255. badblocks_clear(&nsio->bb, sector, cleared);
  256. }
  257. arch_invalidate_pmem(nsio->addr + offset, size);
  258. } else
  259. rc = -EIO;
  260. }
  261. memcpy_flushcache(nsio->addr + offset, buf, size);
  262. nvdimm_flush(to_nd_region(ndns->dev.parent));
  263. return rc;
  264. }
  265. int devm_nsio_enable(struct device *dev, struct nd_namespace_io *nsio)
  266. {
  267. struct resource *res = &nsio->res;
  268. struct nd_namespace_common *ndns = &nsio->common;
  269. nsio->size = resource_size(res);
  270. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  271. dev_name(&ndns->dev))) {
  272. dev_warn(dev, "could not reserve region %pR\n", res);
  273. return -EBUSY;
  274. }
  275. ndns->rw_bytes = nsio_rw_bytes;
  276. if (devm_init_badblocks(dev, &nsio->bb))
  277. return -ENOMEM;
  278. nvdimm_badblocks_populate(to_nd_region(ndns->dev.parent), &nsio->bb,
  279. &nsio->res);
  280. nsio->addr = devm_memremap(dev, res->start, resource_size(res),
  281. ARCH_MEMREMAP_PMEM);
  282. return PTR_ERR_OR_ZERO(nsio->addr);
  283. }
  284. EXPORT_SYMBOL_GPL(devm_nsio_enable);
  285. void devm_nsio_disable(struct device *dev, struct nd_namespace_io *nsio)
  286. {
  287. struct resource *res = &nsio->res;
  288. devm_memunmap(dev, nsio->addr);
  289. devm_exit_badblocks(dev, &nsio->bb);
  290. devm_release_mem_region(dev, res->start, resource_size(res));
  291. }
  292. EXPORT_SYMBOL_GPL(devm_nsio_disable);