claim.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "pfn.h"
  17. #include "btt.h"
  18. #include "nd.h"
  19. void __nd_detach_ndns(struct device *dev, struct nd_namespace_common **_ndns)
  20. {
  21. struct nd_namespace_common *ndns = *_ndns;
  22. dev_WARN_ONCE(dev, !mutex_is_locked(&ndns->dev.mutex)
  23. || ndns->claim != dev,
  24. "%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. dev_WARN_ONCE(dev, !mutex_is_locked(&attach->dev.mutex)
  47. || *_ndns,
  48. "%s: invalid claim\n", __func__);
  49. attach->claim = dev;
  50. *_ndns = attach;
  51. get_device(&attach->dev);
  52. return true;
  53. }
  54. bool nd_attach_ndns(struct device *dev, struct nd_namespace_common *attach,
  55. struct nd_namespace_common **_ndns)
  56. {
  57. bool claimed;
  58. device_lock(&attach->dev);
  59. claimed = __nd_attach_ndns(dev, attach, _ndns);
  60. device_unlock(&attach->dev);
  61. return claimed;
  62. }
  63. static int namespace_match(struct device *dev, void *data)
  64. {
  65. char *name = data;
  66. return strcmp(name, dev_name(dev)) == 0;
  67. }
  68. static bool is_idle(struct device *dev, struct nd_namespace_common *ndns)
  69. {
  70. struct nd_region *nd_region = to_nd_region(dev->parent);
  71. struct device *seed = NULL;
  72. if (is_nd_btt(dev))
  73. seed = nd_region->btt_seed;
  74. else if (is_nd_pfn(dev))
  75. seed = nd_region->pfn_seed;
  76. if (seed == dev || ndns || dev->driver)
  77. return false;
  78. return true;
  79. }
  80. static void nd_detach_and_reset(struct device *dev,
  81. struct nd_namespace_common **_ndns)
  82. {
  83. /* detach the namespace and destroy / reset the device */
  84. nd_detach_ndns(dev, _ndns);
  85. if (is_idle(dev, *_ndns)) {
  86. nd_device_unregister(dev, ND_ASYNC);
  87. } else if (is_nd_btt(dev)) {
  88. struct nd_btt *nd_btt = to_nd_btt(dev);
  89. nd_btt->lbasize = 0;
  90. kfree(nd_btt->uuid);
  91. nd_btt->uuid = NULL;
  92. } else if (is_nd_pfn(dev)) {
  93. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  94. kfree(nd_pfn->uuid);
  95. nd_pfn->uuid = NULL;
  96. nd_pfn->mode = PFN_MODE_NONE;
  97. }
  98. }
  99. ssize_t nd_namespace_store(struct device *dev,
  100. struct nd_namespace_common **_ndns, const char *buf,
  101. size_t len)
  102. {
  103. struct nd_namespace_common *ndns;
  104. struct device *found;
  105. char *name;
  106. if (dev->driver) {
  107. dev_dbg(dev, "%s: -EBUSY\n", __func__);
  108. return -EBUSY;
  109. }
  110. name = kstrndup(buf, len, GFP_KERNEL);
  111. if (!name)
  112. return -ENOMEM;
  113. strim(name);
  114. if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
  115. /* pass */;
  116. else {
  117. len = -EINVAL;
  118. goto out;
  119. }
  120. ndns = *_ndns;
  121. if (strcmp(name, "") == 0) {
  122. nd_detach_and_reset(dev, _ndns);
  123. goto out;
  124. } else if (ndns) {
  125. dev_dbg(dev, "namespace already set to: %s\n",
  126. dev_name(&ndns->dev));
  127. len = -EBUSY;
  128. goto out;
  129. }
  130. found = device_find_child(dev->parent, name, namespace_match);
  131. if (!found) {
  132. dev_dbg(dev, "'%s' not found under %s\n", name,
  133. dev_name(dev->parent));
  134. len = -ENODEV;
  135. goto out;
  136. }
  137. ndns = to_ndns(found);
  138. if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
  139. dev_dbg(dev, "%s too small to host\n", name);
  140. len = -ENXIO;
  141. goto out_attach;
  142. }
  143. WARN_ON_ONCE(!is_nvdimm_bus_locked(dev));
  144. if (!nd_attach_ndns(dev, ndns, _ndns)) {
  145. dev_dbg(dev, "%s already claimed\n",
  146. dev_name(&ndns->dev));
  147. len = -EBUSY;
  148. }
  149. out_attach:
  150. put_device(&ndns->dev); /* from device_find_child */
  151. out:
  152. kfree(name);
  153. return len;
  154. }
  155. /*
  156. * nd_sb_checksum: compute checksum for a generic info block
  157. *
  158. * Returns a fletcher64 checksum of everything in the given info block
  159. * except the last field (since that's where the checksum lives).
  160. */
  161. u64 nd_sb_checksum(struct nd_gen_sb *nd_gen_sb)
  162. {
  163. u64 sum;
  164. __le64 sum_save;
  165. BUILD_BUG_ON(sizeof(struct btt_sb) != SZ_4K);
  166. BUILD_BUG_ON(sizeof(struct nd_pfn_sb) != SZ_4K);
  167. BUILD_BUG_ON(sizeof(struct nd_gen_sb) != SZ_4K);
  168. sum_save = nd_gen_sb->checksum;
  169. nd_gen_sb->checksum = 0;
  170. sum = nd_fletcher64(nd_gen_sb, sizeof(*nd_gen_sb), 1);
  171. nd_gen_sb->checksum = sum_save;
  172. return sum;
  173. }
  174. EXPORT_SYMBOL(nd_sb_checksum);