btt_devs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "btt.h"
  22. #include "nd.h"
  23. static void __nd_btt_detach_ndns(struct nd_btt *nd_btt)
  24. {
  25. struct nd_namespace_common *ndns = nd_btt->ndns;
  26. dev_WARN_ONCE(&nd_btt->dev, !mutex_is_locked(&ndns->dev.mutex)
  27. || ndns->claim != &nd_btt->dev,
  28. "%s: invalid claim\n", __func__);
  29. ndns->claim = NULL;
  30. nd_btt->ndns = NULL;
  31. put_device(&ndns->dev);
  32. }
  33. static void nd_btt_detach_ndns(struct nd_btt *nd_btt)
  34. {
  35. struct nd_namespace_common *ndns = nd_btt->ndns;
  36. if (!ndns)
  37. return;
  38. get_device(&ndns->dev);
  39. device_lock(&ndns->dev);
  40. __nd_btt_detach_ndns(nd_btt);
  41. device_unlock(&ndns->dev);
  42. put_device(&ndns->dev);
  43. }
  44. static bool __nd_btt_attach_ndns(struct nd_btt *nd_btt,
  45. struct nd_namespace_common *ndns)
  46. {
  47. if (ndns->claim)
  48. return false;
  49. dev_WARN_ONCE(&nd_btt->dev, !mutex_is_locked(&ndns->dev.mutex)
  50. || nd_btt->ndns,
  51. "%s: invalid claim\n", __func__);
  52. ndns->claim = &nd_btt->dev;
  53. nd_btt->ndns = ndns;
  54. get_device(&ndns->dev);
  55. return true;
  56. }
  57. static bool nd_btt_attach_ndns(struct nd_btt *nd_btt,
  58. struct nd_namespace_common *ndns)
  59. {
  60. bool claimed;
  61. device_lock(&ndns->dev);
  62. claimed = __nd_btt_attach_ndns(nd_btt, ndns);
  63. device_unlock(&ndns->dev);
  64. return claimed;
  65. }
  66. static void nd_btt_release(struct device *dev)
  67. {
  68. struct nd_region *nd_region = to_nd_region(dev->parent);
  69. struct nd_btt *nd_btt = to_nd_btt(dev);
  70. dev_dbg(dev, "%s\n", __func__);
  71. nd_btt_detach_ndns(nd_btt);
  72. ida_simple_remove(&nd_region->btt_ida, nd_btt->id);
  73. kfree(nd_btt->uuid);
  74. kfree(nd_btt);
  75. }
  76. static struct device_type nd_btt_device_type = {
  77. .name = "nd_btt",
  78. .release = nd_btt_release,
  79. };
  80. bool is_nd_btt(struct device *dev)
  81. {
  82. return dev->type == &nd_btt_device_type;
  83. }
  84. EXPORT_SYMBOL(is_nd_btt);
  85. struct nd_btt *to_nd_btt(struct device *dev)
  86. {
  87. struct nd_btt *nd_btt = container_of(dev, struct nd_btt, dev);
  88. WARN_ON(!is_nd_btt(dev));
  89. return nd_btt;
  90. }
  91. EXPORT_SYMBOL(to_nd_btt);
  92. static const unsigned long btt_lbasize_supported[] = { 512, 520, 528,
  93. 4096, 4104, 4160, 4224, 0 };
  94. static ssize_t sector_size_show(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. struct nd_btt *nd_btt = to_nd_btt(dev);
  98. return nd_sector_size_show(nd_btt->lbasize, btt_lbasize_supported, buf);
  99. }
  100. static ssize_t sector_size_store(struct device *dev,
  101. struct device_attribute *attr, const char *buf, size_t len)
  102. {
  103. struct nd_btt *nd_btt = to_nd_btt(dev);
  104. ssize_t rc;
  105. device_lock(dev);
  106. nvdimm_bus_lock(dev);
  107. rc = nd_sector_size_store(dev, buf, &nd_btt->lbasize,
  108. btt_lbasize_supported);
  109. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  110. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  111. nvdimm_bus_unlock(dev);
  112. device_unlock(dev);
  113. return rc ? rc : len;
  114. }
  115. static DEVICE_ATTR_RW(sector_size);
  116. static ssize_t uuid_show(struct device *dev,
  117. struct device_attribute *attr, char *buf)
  118. {
  119. struct nd_btt *nd_btt = to_nd_btt(dev);
  120. if (nd_btt->uuid)
  121. return sprintf(buf, "%pUb\n", nd_btt->uuid);
  122. return sprintf(buf, "\n");
  123. }
  124. static ssize_t uuid_store(struct device *dev,
  125. struct device_attribute *attr, const char *buf, size_t len)
  126. {
  127. struct nd_btt *nd_btt = to_nd_btt(dev);
  128. ssize_t rc;
  129. device_lock(dev);
  130. rc = nd_uuid_store(dev, &nd_btt->uuid, buf, len);
  131. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  132. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  133. device_unlock(dev);
  134. return rc ? rc : len;
  135. }
  136. static DEVICE_ATTR_RW(uuid);
  137. static ssize_t namespace_show(struct device *dev,
  138. struct device_attribute *attr, char *buf)
  139. {
  140. struct nd_btt *nd_btt = to_nd_btt(dev);
  141. ssize_t rc;
  142. nvdimm_bus_lock(dev);
  143. rc = sprintf(buf, "%s\n", nd_btt->ndns
  144. ? dev_name(&nd_btt->ndns->dev) : "");
  145. nvdimm_bus_unlock(dev);
  146. return rc;
  147. }
  148. static int namespace_match(struct device *dev, void *data)
  149. {
  150. char *name = data;
  151. return strcmp(name, dev_name(dev)) == 0;
  152. }
  153. static bool is_nd_btt_idle(struct device *dev)
  154. {
  155. struct nd_region *nd_region = to_nd_region(dev->parent);
  156. struct nd_btt *nd_btt = to_nd_btt(dev);
  157. if (nd_region->btt_seed == dev || nd_btt->ndns || dev->driver)
  158. return false;
  159. return true;
  160. }
  161. static ssize_t __namespace_store(struct device *dev,
  162. struct device_attribute *attr, const char *buf, size_t len)
  163. {
  164. struct nd_btt *nd_btt = to_nd_btt(dev);
  165. struct nd_namespace_common *ndns;
  166. struct device *found;
  167. char *name;
  168. if (dev->driver) {
  169. dev_dbg(dev, "%s: -EBUSY\n", __func__);
  170. return -EBUSY;
  171. }
  172. name = kstrndup(buf, len, GFP_KERNEL);
  173. if (!name)
  174. return -ENOMEM;
  175. strim(name);
  176. if (strncmp(name, "namespace", 9) == 0 || strcmp(name, "") == 0)
  177. /* pass */;
  178. else {
  179. len = -EINVAL;
  180. goto out;
  181. }
  182. ndns = nd_btt->ndns;
  183. if (strcmp(name, "") == 0) {
  184. /* detach the namespace and destroy / reset the btt device */
  185. nd_btt_detach_ndns(nd_btt);
  186. if (is_nd_btt_idle(dev))
  187. nd_device_unregister(dev, ND_ASYNC);
  188. else {
  189. nd_btt->lbasize = 0;
  190. kfree(nd_btt->uuid);
  191. nd_btt->uuid = NULL;
  192. }
  193. goto out;
  194. } else if (ndns) {
  195. dev_dbg(dev, "namespace already set to: %s\n",
  196. dev_name(&ndns->dev));
  197. len = -EBUSY;
  198. goto out;
  199. }
  200. found = device_find_child(dev->parent, name, namespace_match);
  201. if (!found) {
  202. dev_dbg(dev, "'%s' not found under %s\n", name,
  203. dev_name(dev->parent));
  204. len = -ENODEV;
  205. goto out;
  206. }
  207. ndns = to_ndns(found);
  208. if (__nvdimm_namespace_capacity(ndns) < SZ_16M) {
  209. dev_dbg(dev, "%s too small to host btt\n", name);
  210. len = -ENXIO;
  211. goto out_attach;
  212. }
  213. WARN_ON_ONCE(!is_nvdimm_bus_locked(&nd_btt->dev));
  214. if (!nd_btt_attach_ndns(nd_btt, ndns)) {
  215. dev_dbg(dev, "%s already claimed\n",
  216. dev_name(&ndns->dev));
  217. len = -EBUSY;
  218. }
  219. out_attach:
  220. put_device(&ndns->dev); /* from device_find_child */
  221. out:
  222. kfree(name);
  223. return len;
  224. }
  225. static ssize_t namespace_store(struct device *dev,
  226. struct device_attribute *attr, const char *buf, size_t len)
  227. {
  228. ssize_t rc;
  229. nvdimm_bus_lock(dev);
  230. device_lock(dev);
  231. rc = __namespace_store(dev, attr, buf, len);
  232. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  233. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  234. device_unlock(dev);
  235. nvdimm_bus_unlock(dev);
  236. return rc;
  237. }
  238. static DEVICE_ATTR_RW(namespace);
  239. static struct attribute *nd_btt_attributes[] = {
  240. &dev_attr_sector_size.attr,
  241. &dev_attr_namespace.attr,
  242. &dev_attr_uuid.attr,
  243. NULL,
  244. };
  245. static struct attribute_group nd_btt_attribute_group = {
  246. .attrs = nd_btt_attributes,
  247. };
  248. static const struct attribute_group *nd_btt_attribute_groups[] = {
  249. &nd_btt_attribute_group,
  250. &nd_device_attribute_group,
  251. &nd_numa_attribute_group,
  252. NULL,
  253. };
  254. static struct device *__nd_btt_create(struct nd_region *nd_region,
  255. unsigned long lbasize, u8 *uuid,
  256. struct nd_namespace_common *ndns)
  257. {
  258. struct nd_btt *nd_btt;
  259. struct device *dev;
  260. nd_btt = kzalloc(sizeof(*nd_btt), GFP_KERNEL);
  261. if (!nd_btt)
  262. return NULL;
  263. nd_btt->id = ida_simple_get(&nd_region->btt_ida, 0, 0, GFP_KERNEL);
  264. if (nd_btt->id < 0) {
  265. kfree(nd_btt);
  266. return NULL;
  267. }
  268. nd_btt->lbasize = lbasize;
  269. if (uuid)
  270. uuid = kmemdup(uuid, 16, GFP_KERNEL);
  271. nd_btt->uuid = uuid;
  272. dev = &nd_btt->dev;
  273. dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
  274. dev->parent = &nd_region->dev;
  275. dev->type = &nd_btt_device_type;
  276. dev->groups = nd_btt_attribute_groups;
  277. device_initialize(&nd_btt->dev);
  278. if (ndns && !__nd_btt_attach_ndns(nd_btt, ndns)) {
  279. dev_dbg(&ndns->dev, "%s failed, already claimed by %s\n",
  280. __func__, dev_name(ndns->claim));
  281. put_device(dev);
  282. return NULL;
  283. }
  284. return dev;
  285. }
  286. struct device *nd_btt_create(struct nd_region *nd_region)
  287. {
  288. struct device *dev = __nd_btt_create(nd_region, 0, NULL, NULL);
  289. if (dev)
  290. __nd_device_register(dev);
  291. return dev;
  292. }
  293. /*
  294. * nd_btt_sb_checksum: compute checksum for btt info block
  295. *
  296. * Returns a fletcher64 checksum of everything in the given info block
  297. * except the last field (since that's where the checksum lives).
  298. */
  299. u64 nd_btt_sb_checksum(struct btt_sb *btt_sb)
  300. {
  301. u64 sum;
  302. __le64 sum_save;
  303. sum_save = btt_sb->checksum;
  304. btt_sb->checksum = 0;
  305. sum = nd_fletcher64(btt_sb, sizeof(*btt_sb), 1);
  306. btt_sb->checksum = sum_save;
  307. return sum;
  308. }
  309. EXPORT_SYMBOL(nd_btt_sb_checksum);
  310. static int __nd_btt_probe(struct nd_btt *nd_btt,
  311. struct nd_namespace_common *ndns, struct btt_sb *btt_sb)
  312. {
  313. u64 checksum;
  314. if (!btt_sb || !ndns || !nd_btt)
  315. return -ENODEV;
  316. if (nvdimm_read_bytes(ndns, SZ_4K, btt_sb, sizeof(*btt_sb)))
  317. return -ENXIO;
  318. if (nvdimm_namespace_capacity(ndns) < SZ_16M)
  319. return -ENXIO;
  320. if (memcmp(btt_sb->signature, BTT_SIG, BTT_SIG_LEN) != 0)
  321. return -ENODEV;
  322. checksum = le64_to_cpu(btt_sb->checksum);
  323. btt_sb->checksum = 0;
  324. if (checksum != nd_btt_sb_checksum(btt_sb))
  325. return -ENODEV;
  326. btt_sb->checksum = cpu_to_le64(checksum);
  327. nd_btt->lbasize = le32_to_cpu(btt_sb->external_lbasize);
  328. nd_btt->uuid = kmemdup(btt_sb->uuid, 16, GFP_KERNEL);
  329. if (!nd_btt->uuid)
  330. return -ENOMEM;
  331. __nd_device_register(&nd_btt->dev);
  332. return 0;
  333. }
  334. int nd_btt_probe(struct nd_namespace_common *ndns, void *drvdata)
  335. {
  336. int rc;
  337. struct device *dev;
  338. struct btt_sb *btt_sb;
  339. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  340. if (ndns->force_raw)
  341. return -ENODEV;
  342. nvdimm_bus_lock(&ndns->dev);
  343. dev = __nd_btt_create(nd_region, 0, NULL, ndns);
  344. nvdimm_bus_unlock(&ndns->dev);
  345. if (!dev)
  346. return -ENOMEM;
  347. dev_set_drvdata(dev, drvdata);
  348. btt_sb = kzalloc(sizeof(*btt_sb), GFP_KERNEL);
  349. rc = __nd_btt_probe(to_nd_btt(dev), ndns, btt_sb);
  350. kfree(btt_sb);
  351. dev_dbg(&ndns->dev, "%s: btt: %s\n", __func__,
  352. rc == 0 ? dev_name(dev) : "<none>");
  353. if (rc < 0) {
  354. __nd_btt_detach_ndns(to_nd_btt(dev));
  355. put_device(dev);
  356. }
  357. return rc;
  358. }
  359. EXPORT_SYMBOL(nd_btt_probe);