namespace_devs.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  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/module.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/pmem.h>
  17. #include <linux/nd.h>
  18. #include "nd-core.h"
  19. #include "nd.h"
  20. static void namespace_io_release(struct device *dev)
  21. {
  22. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  23. kfree(nsio);
  24. }
  25. static void namespace_pmem_release(struct device *dev)
  26. {
  27. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  28. kfree(nspm->alt_name);
  29. kfree(nspm->uuid);
  30. kfree(nspm);
  31. }
  32. static void namespace_blk_release(struct device *dev)
  33. {
  34. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  35. struct nd_region *nd_region = to_nd_region(dev->parent);
  36. if (nsblk->id >= 0)
  37. ida_simple_remove(&nd_region->ns_ida, nsblk->id);
  38. kfree(nsblk->alt_name);
  39. kfree(nsblk->uuid);
  40. kfree(nsblk->res);
  41. kfree(nsblk);
  42. }
  43. static struct device_type namespace_io_device_type = {
  44. .name = "nd_namespace_io",
  45. .release = namespace_io_release,
  46. };
  47. static struct device_type namespace_pmem_device_type = {
  48. .name = "nd_namespace_pmem",
  49. .release = namespace_pmem_release,
  50. };
  51. static struct device_type namespace_blk_device_type = {
  52. .name = "nd_namespace_blk",
  53. .release = namespace_blk_release,
  54. };
  55. static bool is_namespace_pmem(struct device *dev)
  56. {
  57. return dev ? dev->type == &namespace_pmem_device_type : false;
  58. }
  59. static bool is_namespace_blk(struct device *dev)
  60. {
  61. return dev ? dev->type == &namespace_blk_device_type : false;
  62. }
  63. static bool is_namespace_io(struct device *dev)
  64. {
  65. return dev ? dev->type == &namespace_io_device_type : false;
  66. }
  67. static int is_uuid_busy(struct device *dev, void *data)
  68. {
  69. u8 *uuid1 = data, *uuid2 = NULL;
  70. if (is_namespace_pmem(dev)) {
  71. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  72. uuid2 = nspm->uuid;
  73. } else if (is_namespace_blk(dev)) {
  74. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  75. uuid2 = nsblk->uuid;
  76. } else if (is_nd_btt(dev)) {
  77. struct nd_btt *nd_btt = to_nd_btt(dev);
  78. uuid2 = nd_btt->uuid;
  79. } else if (is_nd_pfn(dev)) {
  80. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  81. uuid2 = nd_pfn->uuid;
  82. }
  83. if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
  84. return -EBUSY;
  85. return 0;
  86. }
  87. static int is_namespace_uuid_busy(struct device *dev, void *data)
  88. {
  89. if (is_nd_pmem(dev) || is_nd_blk(dev))
  90. return device_for_each_child(dev, data, is_uuid_busy);
  91. return 0;
  92. }
  93. /**
  94. * nd_is_uuid_unique - verify that no other namespace has @uuid
  95. * @dev: any device on a nvdimm_bus
  96. * @uuid: uuid to check
  97. */
  98. bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
  99. {
  100. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  101. if (!nvdimm_bus)
  102. return false;
  103. WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
  104. if (device_for_each_child(&nvdimm_bus->dev, uuid,
  105. is_namespace_uuid_busy) != 0)
  106. return false;
  107. return true;
  108. }
  109. bool pmem_should_map_pages(struct device *dev)
  110. {
  111. struct nd_region *nd_region = to_nd_region(dev->parent);
  112. if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
  113. return false;
  114. if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
  115. return false;
  116. if (is_nd_pfn(dev) || is_nd_btt(dev))
  117. return false;
  118. #ifdef ARCH_MEMREMAP_PMEM
  119. return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
  120. #else
  121. return false;
  122. #endif
  123. }
  124. EXPORT_SYMBOL(pmem_should_map_pages);
  125. const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
  126. char *name)
  127. {
  128. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  129. const char *suffix = NULL;
  130. if (ndns->claim) {
  131. if (is_nd_btt(ndns->claim))
  132. suffix = "s";
  133. else if (is_nd_pfn(ndns->claim))
  134. suffix = "m";
  135. else
  136. dev_WARN_ONCE(&ndns->dev, 1,
  137. "unknown claim type by %s\n",
  138. dev_name(ndns->claim));
  139. }
  140. if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
  141. if (!suffix && pmem_should_map_pages(&ndns->dev))
  142. suffix = "m";
  143. sprintf(name, "pmem%d%s", nd_region->id, suffix ? suffix : "");
  144. } else if (is_namespace_blk(&ndns->dev)) {
  145. struct nd_namespace_blk *nsblk;
  146. nsblk = to_nd_namespace_blk(&ndns->dev);
  147. sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
  148. suffix ? suffix : "");
  149. } else {
  150. return NULL;
  151. }
  152. return name;
  153. }
  154. EXPORT_SYMBOL(nvdimm_namespace_disk_name);
  155. const u8 *nd_dev_to_uuid(struct device *dev)
  156. {
  157. static const u8 null_uuid[16];
  158. if (!dev)
  159. return null_uuid;
  160. if (is_namespace_pmem(dev)) {
  161. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  162. return nspm->uuid;
  163. } else if (is_namespace_blk(dev)) {
  164. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  165. return nsblk->uuid;
  166. } else
  167. return null_uuid;
  168. }
  169. EXPORT_SYMBOL(nd_dev_to_uuid);
  170. static ssize_t nstype_show(struct device *dev,
  171. struct device_attribute *attr, char *buf)
  172. {
  173. struct nd_region *nd_region = to_nd_region(dev->parent);
  174. return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
  175. }
  176. static DEVICE_ATTR_RO(nstype);
  177. static ssize_t __alt_name_store(struct device *dev, const char *buf,
  178. const size_t len)
  179. {
  180. char *input, *pos, *alt_name, **ns_altname;
  181. ssize_t rc;
  182. if (is_namespace_pmem(dev)) {
  183. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  184. ns_altname = &nspm->alt_name;
  185. } else if (is_namespace_blk(dev)) {
  186. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  187. ns_altname = &nsblk->alt_name;
  188. } else
  189. return -ENXIO;
  190. if (dev->driver || to_ndns(dev)->claim)
  191. return -EBUSY;
  192. input = kmemdup(buf, len + 1, GFP_KERNEL);
  193. if (!input)
  194. return -ENOMEM;
  195. input[len] = '\0';
  196. pos = strim(input);
  197. if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
  198. rc = -EINVAL;
  199. goto out;
  200. }
  201. alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
  202. if (!alt_name) {
  203. rc = -ENOMEM;
  204. goto out;
  205. }
  206. kfree(*ns_altname);
  207. *ns_altname = alt_name;
  208. sprintf(*ns_altname, "%s", pos);
  209. rc = len;
  210. out:
  211. kfree(input);
  212. return rc;
  213. }
  214. static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
  215. {
  216. struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
  217. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  218. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  219. struct nd_label_id label_id;
  220. resource_size_t size = 0;
  221. struct resource *res;
  222. if (!nsblk->uuid)
  223. return 0;
  224. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  225. for_each_dpa_resource(ndd, res)
  226. if (strcmp(res->name, label_id.id) == 0)
  227. size += resource_size(res);
  228. return size;
  229. }
  230. static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
  231. {
  232. struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
  233. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  234. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  235. struct nd_label_id label_id;
  236. struct resource *res;
  237. int count, i;
  238. if (!nsblk->uuid || !nsblk->lbasize || !ndd)
  239. return false;
  240. count = 0;
  241. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  242. for_each_dpa_resource(ndd, res) {
  243. if (strcmp(res->name, label_id.id) != 0)
  244. continue;
  245. /*
  246. * Resources with unacknoweldged adjustments indicate a
  247. * failure to update labels
  248. */
  249. if (res->flags & DPA_RESOURCE_ADJUSTED)
  250. return false;
  251. count++;
  252. }
  253. /* These values match after a successful label update */
  254. if (count != nsblk->num_resources)
  255. return false;
  256. for (i = 0; i < nsblk->num_resources; i++) {
  257. struct resource *found = NULL;
  258. for_each_dpa_resource(ndd, res)
  259. if (res == nsblk->res[i]) {
  260. found = res;
  261. break;
  262. }
  263. /* stale resource */
  264. if (!found)
  265. return false;
  266. }
  267. return true;
  268. }
  269. resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
  270. {
  271. resource_size_t size;
  272. nvdimm_bus_lock(&nsblk->common.dev);
  273. size = __nd_namespace_blk_validate(nsblk);
  274. nvdimm_bus_unlock(&nsblk->common.dev);
  275. return size;
  276. }
  277. EXPORT_SYMBOL(nd_namespace_blk_validate);
  278. static int nd_namespace_label_update(struct nd_region *nd_region,
  279. struct device *dev)
  280. {
  281. dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
  282. "namespace must be idle during label update\n");
  283. if (dev->driver || to_ndns(dev)->claim)
  284. return 0;
  285. /*
  286. * Only allow label writes that will result in a valid namespace
  287. * or deletion of an existing namespace.
  288. */
  289. if (is_namespace_pmem(dev)) {
  290. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  291. resource_size_t size = resource_size(&nspm->nsio.res);
  292. if (size == 0 && nspm->uuid)
  293. /* delete allocation */;
  294. else if (!nspm->uuid)
  295. return 0;
  296. return nd_pmem_namespace_label_update(nd_region, nspm, size);
  297. } else if (is_namespace_blk(dev)) {
  298. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  299. resource_size_t size = nd_namespace_blk_size(nsblk);
  300. if (size == 0 && nsblk->uuid)
  301. /* delete allocation */;
  302. else if (!nsblk->uuid || !nsblk->lbasize)
  303. return 0;
  304. return nd_blk_namespace_label_update(nd_region, nsblk, size);
  305. } else
  306. return -ENXIO;
  307. }
  308. static ssize_t alt_name_store(struct device *dev,
  309. struct device_attribute *attr, const char *buf, size_t len)
  310. {
  311. struct nd_region *nd_region = to_nd_region(dev->parent);
  312. ssize_t rc;
  313. device_lock(dev);
  314. nvdimm_bus_lock(dev);
  315. wait_nvdimm_bus_probe_idle(dev);
  316. rc = __alt_name_store(dev, buf, len);
  317. if (rc >= 0)
  318. rc = nd_namespace_label_update(nd_region, dev);
  319. dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
  320. nvdimm_bus_unlock(dev);
  321. device_unlock(dev);
  322. return rc < 0 ? rc : len;
  323. }
  324. static ssize_t alt_name_show(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. char *ns_altname;
  328. if (is_namespace_pmem(dev)) {
  329. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  330. ns_altname = nspm->alt_name;
  331. } else if (is_namespace_blk(dev)) {
  332. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  333. ns_altname = nsblk->alt_name;
  334. } else
  335. return -ENXIO;
  336. return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
  337. }
  338. static DEVICE_ATTR_RW(alt_name);
  339. static int scan_free(struct nd_region *nd_region,
  340. struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
  341. resource_size_t n)
  342. {
  343. bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
  344. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  345. int rc = 0;
  346. while (n) {
  347. struct resource *res, *last;
  348. resource_size_t new_start;
  349. last = NULL;
  350. for_each_dpa_resource(ndd, res)
  351. if (strcmp(res->name, label_id->id) == 0)
  352. last = res;
  353. res = last;
  354. if (!res)
  355. return 0;
  356. if (n >= resource_size(res)) {
  357. n -= resource_size(res);
  358. nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
  359. nvdimm_free_dpa(ndd, res);
  360. /* retry with last resource deleted */
  361. continue;
  362. }
  363. /*
  364. * Keep BLK allocations relegated to high DPA as much as
  365. * possible
  366. */
  367. if (is_blk)
  368. new_start = res->start + n;
  369. else
  370. new_start = res->start;
  371. rc = adjust_resource(res, new_start, resource_size(res) - n);
  372. if (rc == 0)
  373. res->flags |= DPA_RESOURCE_ADJUSTED;
  374. nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
  375. break;
  376. }
  377. return rc;
  378. }
  379. /**
  380. * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
  381. * @nd_region: the set of dimms to reclaim @n bytes from
  382. * @label_id: unique identifier for the namespace consuming this dpa range
  383. * @n: number of bytes per-dimm to release
  384. *
  385. * Assumes resources are ordered. Starting from the end try to
  386. * adjust_resource() the allocation to @n, but if @n is larger than the
  387. * allocation delete it and find the 'new' last allocation in the label
  388. * set.
  389. */
  390. static int shrink_dpa_allocation(struct nd_region *nd_region,
  391. struct nd_label_id *label_id, resource_size_t n)
  392. {
  393. int i;
  394. for (i = 0; i < nd_region->ndr_mappings; i++) {
  395. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  396. int rc;
  397. rc = scan_free(nd_region, nd_mapping, label_id, n);
  398. if (rc)
  399. return rc;
  400. }
  401. return 0;
  402. }
  403. static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
  404. struct nd_region *nd_region, struct nd_mapping *nd_mapping,
  405. resource_size_t n)
  406. {
  407. bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
  408. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  409. resource_size_t first_dpa;
  410. struct resource *res;
  411. int rc = 0;
  412. /* allocate blk from highest dpa first */
  413. if (is_blk)
  414. first_dpa = nd_mapping->start + nd_mapping->size - n;
  415. else
  416. first_dpa = nd_mapping->start;
  417. /* first resource allocation for this label-id or dimm */
  418. res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
  419. if (!res)
  420. rc = -EBUSY;
  421. nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
  422. return rc ? n : 0;
  423. }
  424. static bool space_valid(bool is_pmem, bool is_reserve,
  425. struct nd_label_id *label_id, struct resource *res)
  426. {
  427. /*
  428. * For BLK-space any space is valid, for PMEM-space, it must be
  429. * contiguous with an existing allocation unless we are
  430. * reserving pmem.
  431. */
  432. if (is_reserve || !is_pmem)
  433. return true;
  434. if (!res || strcmp(res->name, label_id->id) == 0)
  435. return true;
  436. return false;
  437. }
  438. enum alloc_loc {
  439. ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
  440. };
  441. static resource_size_t scan_allocate(struct nd_region *nd_region,
  442. struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
  443. resource_size_t n)
  444. {
  445. resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
  446. bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
  447. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  448. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  449. const resource_size_t to_allocate = n;
  450. struct resource *res;
  451. int first;
  452. retry:
  453. first = 0;
  454. for_each_dpa_resource(ndd, res) {
  455. resource_size_t allocate, available = 0, free_start, free_end;
  456. struct resource *next = res->sibling, *new_res = NULL;
  457. enum alloc_loc loc = ALLOC_ERR;
  458. const char *action;
  459. int rc = 0;
  460. /* ignore resources outside this nd_mapping */
  461. if (res->start > mapping_end)
  462. continue;
  463. if (res->end < nd_mapping->start)
  464. continue;
  465. /* space at the beginning of the mapping */
  466. if (!first++ && res->start > nd_mapping->start) {
  467. free_start = nd_mapping->start;
  468. available = res->start - free_start;
  469. if (space_valid(is_pmem, is_reserve, label_id, NULL))
  470. loc = ALLOC_BEFORE;
  471. }
  472. /* space between allocations */
  473. if (!loc && next) {
  474. free_start = res->start + resource_size(res);
  475. free_end = min(mapping_end, next->start - 1);
  476. if (space_valid(is_pmem, is_reserve, label_id, res)
  477. && free_start < free_end) {
  478. available = free_end + 1 - free_start;
  479. loc = ALLOC_MID;
  480. }
  481. }
  482. /* space at the end of the mapping */
  483. if (!loc && !next) {
  484. free_start = res->start + resource_size(res);
  485. free_end = mapping_end;
  486. if (space_valid(is_pmem, is_reserve, label_id, res)
  487. && free_start < free_end) {
  488. available = free_end + 1 - free_start;
  489. loc = ALLOC_AFTER;
  490. }
  491. }
  492. if (!loc || !available)
  493. continue;
  494. allocate = min(available, n);
  495. switch (loc) {
  496. case ALLOC_BEFORE:
  497. if (strcmp(res->name, label_id->id) == 0) {
  498. /* adjust current resource up */
  499. if (is_pmem && !is_reserve)
  500. return n;
  501. rc = adjust_resource(res, res->start - allocate,
  502. resource_size(res) + allocate);
  503. action = "cur grow up";
  504. } else
  505. action = "allocate";
  506. break;
  507. case ALLOC_MID:
  508. if (strcmp(next->name, label_id->id) == 0) {
  509. /* adjust next resource up */
  510. if (is_pmem && !is_reserve)
  511. return n;
  512. rc = adjust_resource(next, next->start
  513. - allocate, resource_size(next)
  514. + allocate);
  515. new_res = next;
  516. action = "next grow up";
  517. } else if (strcmp(res->name, label_id->id) == 0) {
  518. action = "grow down";
  519. } else
  520. action = "allocate";
  521. break;
  522. case ALLOC_AFTER:
  523. if (strcmp(res->name, label_id->id) == 0)
  524. action = "grow down";
  525. else
  526. action = "allocate";
  527. break;
  528. default:
  529. return n;
  530. }
  531. if (strcmp(action, "allocate") == 0) {
  532. /* BLK allocate bottom up */
  533. if (!is_pmem)
  534. free_start += available - allocate;
  535. else if (!is_reserve && free_start != nd_mapping->start)
  536. return n;
  537. new_res = nvdimm_allocate_dpa(ndd, label_id,
  538. free_start, allocate);
  539. if (!new_res)
  540. rc = -EBUSY;
  541. } else if (strcmp(action, "grow down") == 0) {
  542. /* adjust current resource down */
  543. rc = adjust_resource(res, res->start, resource_size(res)
  544. + allocate);
  545. if (rc == 0)
  546. res->flags |= DPA_RESOURCE_ADJUSTED;
  547. }
  548. if (!new_res)
  549. new_res = res;
  550. nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
  551. action, loc, rc);
  552. if (rc)
  553. return n;
  554. n -= allocate;
  555. if (n) {
  556. /*
  557. * Retry scan with newly inserted resources.
  558. * For example, if we did an ALLOC_BEFORE
  559. * insertion there may also have been space
  560. * available for an ALLOC_AFTER insertion, so we
  561. * need to check this same resource again
  562. */
  563. goto retry;
  564. } else
  565. return 0;
  566. }
  567. /*
  568. * If we allocated nothing in the BLK case it may be because we are in
  569. * an initial "pmem-reserve pass". Only do an initial BLK allocation
  570. * when none of the DPA space is reserved.
  571. */
  572. if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
  573. return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
  574. return n;
  575. }
  576. static int merge_dpa(struct nd_region *nd_region,
  577. struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
  578. {
  579. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  580. struct resource *res;
  581. if (strncmp("pmem", label_id->id, 4) == 0)
  582. return 0;
  583. retry:
  584. for_each_dpa_resource(ndd, res) {
  585. int rc;
  586. struct resource *next = res->sibling;
  587. resource_size_t end = res->start + resource_size(res);
  588. if (!next || strcmp(res->name, label_id->id) != 0
  589. || strcmp(next->name, label_id->id) != 0
  590. || end != next->start)
  591. continue;
  592. end += resource_size(next);
  593. nvdimm_free_dpa(ndd, next);
  594. rc = adjust_resource(res, res->start, end - res->start);
  595. nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
  596. if (rc)
  597. return rc;
  598. res->flags |= DPA_RESOURCE_ADJUSTED;
  599. goto retry;
  600. }
  601. return 0;
  602. }
  603. static int __reserve_free_pmem(struct device *dev, void *data)
  604. {
  605. struct nvdimm *nvdimm = data;
  606. struct nd_region *nd_region;
  607. struct nd_label_id label_id;
  608. int i;
  609. if (!is_nd_pmem(dev))
  610. return 0;
  611. nd_region = to_nd_region(dev);
  612. if (nd_region->ndr_mappings == 0)
  613. return 0;
  614. memset(&label_id, 0, sizeof(label_id));
  615. strcat(label_id.id, "pmem-reserve");
  616. for (i = 0; i < nd_region->ndr_mappings; i++) {
  617. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  618. resource_size_t n, rem = 0;
  619. if (nd_mapping->nvdimm != nvdimm)
  620. continue;
  621. n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
  622. if (n == 0)
  623. return 0;
  624. rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
  625. dev_WARN_ONCE(&nd_region->dev, rem,
  626. "pmem reserve underrun: %#llx of %#llx bytes\n",
  627. (unsigned long long) n - rem,
  628. (unsigned long long) n);
  629. return rem ? -ENXIO : 0;
  630. }
  631. return 0;
  632. }
  633. static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
  634. struct nd_mapping *nd_mapping)
  635. {
  636. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  637. struct resource *res, *_res;
  638. for_each_dpa_resource_safe(ndd, res, _res)
  639. if (strcmp(res->name, "pmem-reserve") == 0)
  640. nvdimm_free_dpa(ndd, res);
  641. }
  642. static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
  643. struct nd_mapping *nd_mapping)
  644. {
  645. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  646. int rc;
  647. rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
  648. __reserve_free_pmem);
  649. if (rc)
  650. release_free_pmem(nvdimm_bus, nd_mapping);
  651. return rc;
  652. }
  653. /**
  654. * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
  655. * @nd_region: the set of dimms to allocate @n more bytes from
  656. * @label_id: unique identifier for the namespace consuming this dpa range
  657. * @n: number of bytes per-dimm to add to the existing allocation
  658. *
  659. * Assumes resources are ordered. For BLK regions, first consume
  660. * BLK-only available DPA free space, then consume PMEM-aliased DPA
  661. * space starting at the highest DPA. For PMEM regions start
  662. * allocations from the start of an interleave set and end at the first
  663. * BLK allocation or the end of the interleave set, whichever comes
  664. * first.
  665. */
  666. static int grow_dpa_allocation(struct nd_region *nd_region,
  667. struct nd_label_id *label_id, resource_size_t n)
  668. {
  669. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  670. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  671. int i;
  672. for (i = 0; i < nd_region->ndr_mappings; i++) {
  673. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  674. resource_size_t rem = n;
  675. int rc, j;
  676. /*
  677. * In the BLK case try once with all unallocated PMEM
  678. * reserved, and once without
  679. */
  680. for (j = is_pmem; j < 2; j++) {
  681. bool blk_only = j == 0;
  682. if (blk_only) {
  683. rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
  684. if (rc)
  685. return rc;
  686. }
  687. rem = scan_allocate(nd_region, nd_mapping,
  688. label_id, rem);
  689. if (blk_only)
  690. release_free_pmem(nvdimm_bus, nd_mapping);
  691. /* try again and allow encroachments into PMEM */
  692. if (rem == 0)
  693. break;
  694. }
  695. dev_WARN_ONCE(&nd_region->dev, rem,
  696. "allocation underrun: %#llx of %#llx bytes\n",
  697. (unsigned long long) n - rem,
  698. (unsigned long long) n);
  699. if (rem)
  700. return -ENXIO;
  701. rc = merge_dpa(nd_region, nd_mapping, label_id);
  702. if (rc)
  703. return rc;
  704. }
  705. return 0;
  706. }
  707. static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
  708. struct nd_namespace_pmem *nspm, resource_size_t size)
  709. {
  710. struct resource *res = &nspm->nsio.res;
  711. res->start = nd_region->ndr_start;
  712. res->end = nd_region->ndr_start + size - 1;
  713. }
  714. static ssize_t __size_store(struct device *dev, unsigned long long val)
  715. {
  716. resource_size_t allocated = 0, available = 0;
  717. struct nd_region *nd_region = to_nd_region(dev->parent);
  718. struct nd_mapping *nd_mapping;
  719. struct nvdimm_drvdata *ndd;
  720. struct nd_label_id label_id;
  721. u32 flags = 0, remainder;
  722. u8 *uuid = NULL;
  723. int rc, i;
  724. if (dev->driver || to_ndns(dev)->claim)
  725. return -EBUSY;
  726. if (is_namespace_pmem(dev)) {
  727. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  728. uuid = nspm->uuid;
  729. } else if (is_namespace_blk(dev)) {
  730. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  731. uuid = nsblk->uuid;
  732. flags = NSLABEL_FLAG_LOCAL;
  733. }
  734. /*
  735. * We need a uuid for the allocation-label and dimm(s) on which
  736. * to store the label.
  737. */
  738. if (!uuid || nd_region->ndr_mappings == 0)
  739. return -ENXIO;
  740. div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
  741. if (remainder) {
  742. dev_dbg(dev, "%llu is not %dK aligned\n", val,
  743. (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
  744. return -EINVAL;
  745. }
  746. nd_label_gen_id(&label_id, uuid, flags);
  747. for (i = 0; i < nd_region->ndr_mappings; i++) {
  748. nd_mapping = &nd_region->mapping[i];
  749. ndd = to_ndd(nd_mapping);
  750. /*
  751. * All dimms in an interleave set, or the base dimm for a blk
  752. * region, need to be enabled for the size to be changed.
  753. */
  754. if (!ndd)
  755. return -ENXIO;
  756. allocated += nvdimm_allocated_dpa(ndd, &label_id);
  757. }
  758. available = nd_region_available_dpa(nd_region);
  759. if (val > available + allocated)
  760. return -ENOSPC;
  761. if (val == allocated)
  762. return 0;
  763. val = div_u64(val, nd_region->ndr_mappings);
  764. allocated = div_u64(allocated, nd_region->ndr_mappings);
  765. if (val < allocated)
  766. rc = shrink_dpa_allocation(nd_region, &label_id,
  767. allocated - val);
  768. else
  769. rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
  770. if (rc)
  771. return rc;
  772. if (is_namespace_pmem(dev)) {
  773. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  774. nd_namespace_pmem_set_size(nd_region, nspm,
  775. val * nd_region->ndr_mappings);
  776. } else if (is_namespace_blk(dev)) {
  777. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  778. /*
  779. * Try to delete the namespace if we deleted all of its
  780. * allocation, this is not the seed device for the
  781. * region, and it is not actively claimed by a btt
  782. * instance.
  783. */
  784. if (val == 0 && nd_region->ns_seed != dev
  785. && !nsblk->common.claim)
  786. nd_device_unregister(dev, ND_ASYNC);
  787. }
  788. return rc;
  789. }
  790. static ssize_t size_store(struct device *dev,
  791. struct device_attribute *attr, const char *buf, size_t len)
  792. {
  793. struct nd_region *nd_region = to_nd_region(dev->parent);
  794. unsigned long long val;
  795. u8 **uuid = NULL;
  796. int rc;
  797. rc = kstrtoull(buf, 0, &val);
  798. if (rc)
  799. return rc;
  800. device_lock(dev);
  801. nvdimm_bus_lock(dev);
  802. wait_nvdimm_bus_probe_idle(dev);
  803. rc = __size_store(dev, val);
  804. if (rc >= 0)
  805. rc = nd_namespace_label_update(nd_region, dev);
  806. if (is_namespace_pmem(dev)) {
  807. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  808. uuid = &nspm->uuid;
  809. } else if (is_namespace_blk(dev)) {
  810. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  811. uuid = &nsblk->uuid;
  812. }
  813. if (rc == 0 && val == 0 && uuid) {
  814. /* setting size zero == 'delete namespace' */
  815. kfree(*uuid);
  816. *uuid = NULL;
  817. }
  818. dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
  819. ? "fail" : "success", rc);
  820. nvdimm_bus_unlock(dev);
  821. device_unlock(dev);
  822. return rc < 0 ? rc : len;
  823. }
  824. resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  825. {
  826. struct device *dev = &ndns->dev;
  827. if (is_namespace_pmem(dev)) {
  828. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  829. return resource_size(&nspm->nsio.res);
  830. } else if (is_namespace_blk(dev)) {
  831. return nd_namespace_blk_size(to_nd_namespace_blk(dev));
  832. } else if (is_namespace_io(dev)) {
  833. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  834. return resource_size(&nsio->res);
  835. } else
  836. WARN_ONCE(1, "unknown namespace type\n");
  837. return 0;
  838. }
  839. resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  840. {
  841. resource_size_t size;
  842. nvdimm_bus_lock(&ndns->dev);
  843. size = __nvdimm_namespace_capacity(ndns);
  844. nvdimm_bus_unlock(&ndns->dev);
  845. return size;
  846. }
  847. EXPORT_SYMBOL(nvdimm_namespace_capacity);
  848. static ssize_t size_show(struct device *dev,
  849. struct device_attribute *attr, char *buf)
  850. {
  851. return sprintf(buf, "%llu\n", (unsigned long long)
  852. nvdimm_namespace_capacity(to_ndns(dev)));
  853. }
  854. static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
  855. static ssize_t uuid_show(struct device *dev,
  856. struct device_attribute *attr, char *buf)
  857. {
  858. u8 *uuid;
  859. if (is_namespace_pmem(dev)) {
  860. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  861. uuid = nspm->uuid;
  862. } else if (is_namespace_blk(dev)) {
  863. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  864. uuid = nsblk->uuid;
  865. } else
  866. return -ENXIO;
  867. if (uuid)
  868. return sprintf(buf, "%pUb\n", uuid);
  869. return sprintf(buf, "\n");
  870. }
  871. /**
  872. * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
  873. * @nd_region: parent region so we can updates all dimms in the set
  874. * @dev: namespace type for generating label_id
  875. * @new_uuid: incoming uuid
  876. * @old_uuid: reference to the uuid storage location in the namespace object
  877. */
  878. static int namespace_update_uuid(struct nd_region *nd_region,
  879. struct device *dev, u8 *new_uuid, u8 **old_uuid)
  880. {
  881. u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
  882. struct nd_label_id old_label_id;
  883. struct nd_label_id new_label_id;
  884. int i;
  885. if (!nd_is_uuid_unique(dev, new_uuid))
  886. return -EINVAL;
  887. if (*old_uuid == NULL)
  888. goto out;
  889. /*
  890. * If we've already written a label with this uuid, then it's
  891. * too late to rename because we can't reliably update the uuid
  892. * without losing the old namespace. Userspace must delete this
  893. * namespace to abandon the old uuid.
  894. */
  895. for (i = 0; i < nd_region->ndr_mappings; i++) {
  896. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  897. /*
  898. * This check by itself is sufficient because old_uuid
  899. * would be NULL above if this uuid did not exist in the
  900. * currently written set.
  901. *
  902. * FIXME: can we delete uuid with zero dpa allocated?
  903. */
  904. if (nd_mapping->labels)
  905. return -EBUSY;
  906. }
  907. nd_label_gen_id(&old_label_id, *old_uuid, flags);
  908. nd_label_gen_id(&new_label_id, new_uuid, flags);
  909. for (i = 0; i < nd_region->ndr_mappings; i++) {
  910. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  911. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  912. struct resource *res;
  913. for_each_dpa_resource(ndd, res)
  914. if (strcmp(res->name, old_label_id.id) == 0)
  915. sprintf((void *) res->name, "%s",
  916. new_label_id.id);
  917. }
  918. kfree(*old_uuid);
  919. out:
  920. *old_uuid = new_uuid;
  921. return 0;
  922. }
  923. static ssize_t uuid_store(struct device *dev,
  924. struct device_attribute *attr, const char *buf, size_t len)
  925. {
  926. struct nd_region *nd_region = to_nd_region(dev->parent);
  927. u8 *uuid = NULL;
  928. ssize_t rc = 0;
  929. u8 **ns_uuid;
  930. if (is_namespace_pmem(dev)) {
  931. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  932. ns_uuid = &nspm->uuid;
  933. } else if (is_namespace_blk(dev)) {
  934. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  935. ns_uuid = &nsblk->uuid;
  936. } else
  937. return -ENXIO;
  938. device_lock(dev);
  939. nvdimm_bus_lock(dev);
  940. wait_nvdimm_bus_probe_idle(dev);
  941. if (to_ndns(dev)->claim)
  942. rc = -EBUSY;
  943. if (rc >= 0)
  944. rc = nd_uuid_store(dev, &uuid, buf, len);
  945. if (rc >= 0)
  946. rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
  947. if (rc >= 0)
  948. rc = nd_namespace_label_update(nd_region, dev);
  949. else
  950. kfree(uuid);
  951. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  952. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  953. nvdimm_bus_unlock(dev);
  954. device_unlock(dev);
  955. return rc < 0 ? rc : len;
  956. }
  957. static DEVICE_ATTR_RW(uuid);
  958. static ssize_t resource_show(struct device *dev,
  959. struct device_attribute *attr, char *buf)
  960. {
  961. struct resource *res;
  962. if (is_namespace_pmem(dev)) {
  963. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  964. res = &nspm->nsio.res;
  965. } else if (is_namespace_io(dev)) {
  966. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  967. res = &nsio->res;
  968. } else
  969. return -ENXIO;
  970. /* no address to convey if the namespace has no allocation */
  971. if (resource_size(res) == 0)
  972. return -ENXIO;
  973. return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
  974. }
  975. static DEVICE_ATTR_RO(resource);
  976. static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
  977. 4096, 4104, 4160, 4224, 0 };
  978. static ssize_t sector_size_show(struct device *dev,
  979. struct device_attribute *attr, char *buf)
  980. {
  981. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  982. if (!is_namespace_blk(dev))
  983. return -ENXIO;
  984. return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
  985. }
  986. static ssize_t sector_size_store(struct device *dev,
  987. struct device_attribute *attr, const char *buf, size_t len)
  988. {
  989. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  990. struct nd_region *nd_region = to_nd_region(dev->parent);
  991. ssize_t rc = 0;
  992. if (!is_namespace_blk(dev))
  993. return -ENXIO;
  994. device_lock(dev);
  995. nvdimm_bus_lock(dev);
  996. if (to_ndns(dev)->claim)
  997. rc = -EBUSY;
  998. if (rc >= 0)
  999. rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
  1000. ns_lbasize_supported);
  1001. if (rc >= 0)
  1002. rc = nd_namespace_label_update(nd_region, dev);
  1003. dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
  1004. rc, rc < 0 ? "tried" : "wrote", buf,
  1005. buf[len - 1] == '\n' ? "" : "\n");
  1006. nvdimm_bus_unlock(dev);
  1007. device_unlock(dev);
  1008. return rc ? rc : len;
  1009. }
  1010. static DEVICE_ATTR_RW(sector_size);
  1011. static ssize_t dpa_extents_show(struct device *dev,
  1012. struct device_attribute *attr, char *buf)
  1013. {
  1014. struct nd_region *nd_region = to_nd_region(dev->parent);
  1015. struct nd_label_id label_id;
  1016. int count = 0, i;
  1017. u8 *uuid = NULL;
  1018. u32 flags = 0;
  1019. nvdimm_bus_lock(dev);
  1020. if (is_namespace_pmem(dev)) {
  1021. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1022. uuid = nspm->uuid;
  1023. flags = 0;
  1024. } else if (is_namespace_blk(dev)) {
  1025. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1026. uuid = nsblk->uuid;
  1027. flags = NSLABEL_FLAG_LOCAL;
  1028. }
  1029. if (!uuid)
  1030. goto out;
  1031. nd_label_gen_id(&label_id, uuid, flags);
  1032. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1033. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1034. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1035. struct resource *res;
  1036. for_each_dpa_resource(ndd, res)
  1037. if (strcmp(res->name, label_id.id) == 0)
  1038. count++;
  1039. }
  1040. out:
  1041. nvdimm_bus_unlock(dev);
  1042. return sprintf(buf, "%d\n", count);
  1043. }
  1044. static DEVICE_ATTR_RO(dpa_extents);
  1045. static ssize_t holder_show(struct device *dev,
  1046. struct device_attribute *attr, char *buf)
  1047. {
  1048. struct nd_namespace_common *ndns = to_ndns(dev);
  1049. ssize_t rc;
  1050. device_lock(dev);
  1051. rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
  1052. device_unlock(dev);
  1053. return rc;
  1054. }
  1055. static DEVICE_ATTR_RO(holder);
  1056. static ssize_t force_raw_store(struct device *dev,
  1057. struct device_attribute *attr, const char *buf, size_t len)
  1058. {
  1059. bool force_raw;
  1060. int rc = strtobool(buf, &force_raw);
  1061. if (rc)
  1062. return rc;
  1063. to_ndns(dev)->force_raw = force_raw;
  1064. return len;
  1065. }
  1066. static ssize_t force_raw_show(struct device *dev,
  1067. struct device_attribute *attr, char *buf)
  1068. {
  1069. return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
  1070. }
  1071. static DEVICE_ATTR_RW(force_raw);
  1072. static struct attribute *nd_namespace_attributes[] = {
  1073. &dev_attr_nstype.attr,
  1074. &dev_attr_size.attr,
  1075. &dev_attr_uuid.attr,
  1076. &dev_attr_holder.attr,
  1077. &dev_attr_resource.attr,
  1078. &dev_attr_alt_name.attr,
  1079. &dev_attr_force_raw.attr,
  1080. &dev_attr_sector_size.attr,
  1081. &dev_attr_dpa_extents.attr,
  1082. NULL,
  1083. };
  1084. static umode_t namespace_visible(struct kobject *kobj,
  1085. struct attribute *a, int n)
  1086. {
  1087. struct device *dev = container_of(kobj, struct device, kobj);
  1088. if (a == &dev_attr_resource.attr) {
  1089. if (is_namespace_blk(dev))
  1090. return 0;
  1091. return 0400;
  1092. }
  1093. if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
  1094. if (a == &dev_attr_size.attr)
  1095. return S_IWUSR | S_IRUGO;
  1096. if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
  1097. return 0;
  1098. return a->mode;
  1099. }
  1100. if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
  1101. || a == &dev_attr_holder.attr
  1102. || a == &dev_attr_force_raw.attr)
  1103. return a->mode;
  1104. return 0;
  1105. }
  1106. static struct attribute_group nd_namespace_attribute_group = {
  1107. .attrs = nd_namespace_attributes,
  1108. .is_visible = namespace_visible,
  1109. };
  1110. static const struct attribute_group *nd_namespace_attribute_groups[] = {
  1111. &nd_device_attribute_group,
  1112. &nd_namespace_attribute_group,
  1113. &nd_numa_attribute_group,
  1114. NULL,
  1115. };
  1116. struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
  1117. {
  1118. struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
  1119. struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
  1120. struct nd_namespace_common *ndns;
  1121. resource_size_t size;
  1122. if (nd_btt || nd_pfn) {
  1123. struct device *host = NULL;
  1124. if (nd_btt) {
  1125. host = &nd_btt->dev;
  1126. ndns = nd_btt->ndns;
  1127. } else if (nd_pfn) {
  1128. host = &nd_pfn->dev;
  1129. ndns = nd_pfn->ndns;
  1130. }
  1131. if (!ndns || !host)
  1132. return ERR_PTR(-ENODEV);
  1133. /*
  1134. * Flush any in-progess probes / removals in the driver
  1135. * for the raw personality of this namespace.
  1136. */
  1137. device_lock(&ndns->dev);
  1138. device_unlock(&ndns->dev);
  1139. if (ndns->dev.driver) {
  1140. dev_dbg(&ndns->dev, "is active, can't bind %s\n",
  1141. dev_name(host));
  1142. return ERR_PTR(-EBUSY);
  1143. }
  1144. if (dev_WARN_ONCE(&ndns->dev, ndns->claim != host,
  1145. "host (%s) vs claim (%s) mismatch\n",
  1146. dev_name(host),
  1147. dev_name(ndns->claim)))
  1148. return ERR_PTR(-ENXIO);
  1149. } else {
  1150. ndns = to_ndns(dev);
  1151. if (ndns->claim) {
  1152. dev_dbg(dev, "claimed by %s, failing probe\n",
  1153. dev_name(ndns->claim));
  1154. return ERR_PTR(-ENXIO);
  1155. }
  1156. }
  1157. size = nvdimm_namespace_capacity(ndns);
  1158. if (size < ND_MIN_NAMESPACE_SIZE) {
  1159. dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
  1160. &size, ND_MIN_NAMESPACE_SIZE);
  1161. return ERR_PTR(-ENODEV);
  1162. }
  1163. if (is_namespace_pmem(&ndns->dev)) {
  1164. struct nd_namespace_pmem *nspm;
  1165. nspm = to_nd_namespace_pmem(&ndns->dev);
  1166. if (!nspm->uuid) {
  1167. dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__);
  1168. return ERR_PTR(-ENODEV);
  1169. }
  1170. } else if (is_namespace_blk(&ndns->dev)) {
  1171. struct nd_namespace_blk *nsblk;
  1172. nsblk = to_nd_namespace_blk(&ndns->dev);
  1173. if (!nd_namespace_blk_validate(nsblk))
  1174. return ERR_PTR(-ENODEV);
  1175. }
  1176. return ndns;
  1177. }
  1178. EXPORT_SYMBOL(nvdimm_namespace_common_probe);
  1179. static struct device **create_namespace_io(struct nd_region *nd_region)
  1180. {
  1181. struct nd_namespace_io *nsio;
  1182. struct device *dev, **devs;
  1183. struct resource *res;
  1184. nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
  1185. if (!nsio)
  1186. return NULL;
  1187. devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
  1188. if (!devs) {
  1189. kfree(nsio);
  1190. return NULL;
  1191. }
  1192. dev = &nsio->common.dev;
  1193. dev->type = &namespace_io_device_type;
  1194. dev->parent = &nd_region->dev;
  1195. res = &nsio->res;
  1196. res->name = dev_name(&nd_region->dev);
  1197. res->flags = IORESOURCE_MEM;
  1198. res->start = nd_region->ndr_start;
  1199. res->end = res->start + nd_region->ndr_size - 1;
  1200. devs[0] = dev;
  1201. return devs;
  1202. }
  1203. static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
  1204. u64 cookie, u16 pos)
  1205. {
  1206. struct nd_namespace_label *found = NULL;
  1207. int i;
  1208. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1209. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1210. struct nd_namespace_label *nd_label;
  1211. bool found_uuid = false;
  1212. int l;
  1213. for_each_label(l, nd_label, nd_mapping->labels) {
  1214. u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
  1215. u16 position = __le16_to_cpu(nd_label->position);
  1216. u16 nlabel = __le16_to_cpu(nd_label->nlabel);
  1217. if (isetcookie != cookie)
  1218. continue;
  1219. if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
  1220. continue;
  1221. if (found_uuid) {
  1222. dev_dbg(to_ndd(nd_mapping)->dev,
  1223. "%s duplicate entry for uuid\n",
  1224. __func__);
  1225. return false;
  1226. }
  1227. found_uuid = true;
  1228. if (nlabel != nd_region->ndr_mappings)
  1229. continue;
  1230. if (position != pos)
  1231. continue;
  1232. found = nd_label;
  1233. break;
  1234. }
  1235. if (found)
  1236. break;
  1237. }
  1238. return found != NULL;
  1239. }
  1240. static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
  1241. {
  1242. struct nd_namespace_label *select = NULL;
  1243. int i;
  1244. if (!pmem_id)
  1245. return -ENODEV;
  1246. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1247. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1248. struct nd_namespace_label *nd_label;
  1249. u64 hw_start, hw_end, pmem_start, pmem_end;
  1250. int l;
  1251. for_each_label(l, nd_label, nd_mapping->labels)
  1252. if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
  1253. break;
  1254. if (!nd_label) {
  1255. WARN_ON(1);
  1256. return -EINVAL;
  1257. }
  1258. select = nd_label;
  1259. /*
  1260. * Check that this label is compliant with the dpa
  1261. * range published in NFIT
  1262. */
  1263. hw_start = nd_mapping->start;
  1264. hw_end = hw_start + nd_mapping->size;
  1265. pmem_start = __le64_to_cpu(select->dpa);
  1266. pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
  1267. if (pmem_start == hw_start && pmem_end <= hw_end)
  1268. /* pass */;
  1269. else
  1270. return -EINVAL;
  1271. nd_mapping->labels[0] = select;
  1272. nd_mapping->labels[1] = NULL;
  1273. }
  1274. return 0;
  1275. }
  1276. /**
  1277. * find_pmem_label_set - validate interleave set labelling, retrieve label0
  1278. * @nd_region: region with mappings to validate
  1279. */
  1280. static int find_pmem_label_set(struct nd_region *nd_region,
  1281. struct nd_namespace_pmem *nspm)
  1282. {
  1283. u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
  1284. u64 cookie = nd_region_interleave_set_cookie(nd_region);
  1285. struct nd_namespace_label *nd_label;
  1286. u8 select_id[NSLABEL_UUID_LEN];
  1287. resource_size_t size = 0;
  1288. u8 *pmem_id = NULL;
  1289. int rc = -ENODEV, l;
  1290. u16 i;
  1291. if (cookie == 0) {
  1292. dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
  1293. return -ENXIO;
  1294. }
  1295. /*
  1296. * Find a complete set of labels by uuid. By definition we can start
  1297. * with any mapping as the reference label
  1298. */
  1299. for_each_label(l, nd_label, nd_region->mapping[0].labels) {
  1300. u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
  1301. if (isetcookie != cookie) {
  1302. dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
  1303. nd_label->uuid);
  1304. if (isetcookie != altcookie)
  1305. continue;
  1306. dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
  1307. nd_label->uuid);
  1308. }
  1309. for (i = 0; nd_region->ndr_mappings; i++) {
  1310. if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
  1311. continue;
  1312. if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
  1313. continue;
  1314. break;
  1315. }
  1316. if (i < nd_region->ndr_mappings) {
  1317. /*
  1318. * Give up if we don't find an instance of a
  1319. * uuid at each position (from 0 to
  1320. * nd_region->ndr_mappings - 1), or if we find a
  1321. * dimm with two instances of the same uuid.
  1322. */
  1323. rc = -EINVAL;
  1324. goto err;
  1325. } else if (pmem_id) {
  1326. /*
  1327. * If there is more than one valid uuid set, we
  1328. * need userspace to clean this up.
  1329. */
  1330. rc = -EBUSY;
  1331. goto err;
  1332. }
  1333. memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
  1334. pmem_id = select_id;
  1335. }
  1336. /*
  1337. * Fix up each mapping's 'labels' to have the validated pmem label for
  1338. * that position at labels[0], and NULL at labels[1]. In the process,
  1339. * check that the namespace aligns with interleave-set. We know
  1340. * that it does not overlap with any blk namespaces by virtue of
  1341. * the dimm being enabled (i.e. nd_label_reserve_dpa()
  1342. * succeeded).
  1343. */
  1344. rc = select_pmem_id(nd_region, pmem_id);
  1345. if (rc)
  1346. goto err;
  1347. /* Calculate total size and populate namespace properties from label0 */
  1348. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1349. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1350. struct nd_namespace_label *label0 = nd_mapping->labels[0];
  1351. size += __le64_to_cpu(label0->rawsize);
  1352. if (__le16_to_cpu(label0->position) != 0)
  1353. continue;
  1354. WARN_ON(nspm->alt_name || nspm->uuid);
  1355. nspm->alt_name = kmemdup((void __force *) label0->name,
  1356. NSLABEL_NAME_LEN, GFP_KERNEL);
  1357. nspm->uuid = kmemdup((void __force *) label0->uuid,
  1358. NSLABEL_UUID_LEN, GFP_KERNEL);
  1359. }
  1360. if (!nspm->alt_name || !nspm->uuid) {
  1361. rc = -ENOMEM;
  1362. goto err;
  1363. }
  1364. nd_namespace_pmem_set_size(nd_region, nspm, size);
  1365. return 0;
  1366. err:
  1367. switch (rc) {
  1368. case -EINVAL:
  1369. dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
  1370. break;
  1371. case -ENODEV:
  1372. dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
  1373. break;
  1374. default:
  1375. dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
  1376. __func__, rc);
  1377. break;
  1378. }
  1379. return rc;
  1380. }
  1381. static struct device **create_namespace_pmem(struct nd_region *nd_region)
  1382. {
  1383. struct nd_namespace_pmem *nspm;
  1384. struct device *dev, **devs;
  1385. struct resource *res;
  1386. int rc;
  1387. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1388. if (!nspm)
  1389. return NULL;
  1390. dev = &nspm->nsio.common.dev;
  1391. dev->type = &namespace_pmem_device_type;
  1392. dev->parent = &nd_region->dev;
  1393. res = &nspm->nsio.res;
  1394. res->name = dev_name(&nd_region->dev);
  1395. res->flags = IORESOURCE_MEM;
  1396. rc = find_pmem_label_set(nd_region, nspm);
  1397. if (rc == -ENODEV) {
  1398. int i;
  1399. /* Pass, try to permit namespace creation... */
  1400. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1401. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1402. kfree(nd_mapping->labels);
  1403. nd_mapping->labels = NULL;
  1404. }
  1405. /* Publish a zero-sized namespace for userspace to configure. */
  1406. nd_namespace_pmem_set_size(nd_region, nspm, 0);
  1407. rc = 0;
  1408. } else if (rc)
  1409. goto err;
  1410. devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
  1411. if (!devs)
  1412. goto err;
  1413. devs[0] = dev;
  1414. return devs;
  1415. err:
  1416. namespace_pmem_release(&nspm->nsio.common.dev);
  1417. return NULL;
  1418. }
  1419. struct resource *nsblk_add_resource(struct nd_region *nd_region,
  1420. struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
  1421. resource_size_t start)
  1422. {
  1423. struct nd_label_id label_id;
  1424. struct resource *res;
  1425. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  1426. res = krealloc(nsblk->res,
  1427. sizeof(void *) * (nsblk->num_resources + 1),
  1428. GFP_KERNEL);
  1429. if (!res)
  1430. return NULL;
  1431. nsblk->res = (struct resource **) res;
  1432. for_each_dpa_resource(ndd, res)
  1433. if (strcmp(res->name, label_id.id) == 0
  1434. && res->start == start) {
  1435. nsblk->res[nsblk->num_resources++] = res;
  1436. return res;
  1437. }
  1438. return NULL;
  1439. }
  1440. static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
  1441. {
  1442. struct nd_namespace_blk *nsblk;
  1443. struct device *dev;
  1444. if (!is_nd_blk(&nd_region->dev))
  1445. return NULL;
  1446. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1447. if (!nsblk)
  1448. return NULL;
  1449. dev = &nsblk->common.dev;
  1450. dev->type = &namespace_blk_device_type;
  1451. nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
  1452. if (nsblk->id < 0) {
  1453. kfree(nsblk);
  1454. return NULL;
  1455. }
  1456. dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
  1457. dev->parent = &nd_region->dev;
  1458. dev->groups = nd_namespace_attribute_groups;
  1459. return &nsblk->common.dev;
  1460. }
  1461. void nd_region_create_blk_seed(struct nd_region *nd_region)
  1462. {
  1463. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1464. nd_region->ns_seed = nd_namespace_blk_create(nd_region);
  1465. /*
  1466. * Seed creation failures are not fatal, provisioning is simply
  1467. * disabled until memory becomes available
  1468. */
  1469. if (!nd_region->ns_seed)
  1470. dev_err(&nd_region->dev, "failed to create blk namespace\n");
  1471. else
  1472. nd_device_register(nd_region->ns_seed);
  1473. }
  1474. void nd_region_create_btt_seed(struct nd_region *nd_region)
  1475. {
  1476. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1477. nd_region->btt_seed = nd_btt_create(nd_region);
  1478. /*
  1479. * Seed creation failures are not fatal, provisioning is simply
  1480. * disabled until memory becomes available
  1481. */
  1482. if (!nd_region->btt_seed)
  1483. dev_err(&nd_region->dev, "failed to create btt namespace\n");
  1484. }
  1485. static struct device **create_namespace_blk(struct nd_region *nd_region)
  1486. {
  1487. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1488. struct nd_namespace_label *nd_label;
  1489. struct device *dev, **devs = NULL;
  1490. struct nd_namespace_blk *nsblk;
  1491. struct nvdimm_drvdata *ndd;
  1492. int i, l, count = 0;
  1493. struct resource *res;
  1494. if (nd_region->ndr_mappings == 0)
  1495. return NULL;
  1496. ndd = to_ndd(nd_mapping);
  1497. for_each_label(l, nd_label, nd_mapping->labels) {
  1498. u32 flags = __le32_to_cpu(nd_label->flags);
  1499. char *name[NSLABEL_NAME_LEN];
  1500. struct device **__devs;
  1501. if (flags & NSLABEL_FLAG_LOCAL)
  1502. /* pass */;
  1503. else
  1504. continue;
  1505. for (i = 0; i < count; i++) {
  1506. nsblk = to_nd_namespace_blk(devs[i]);
  1507. if (memcmp(nsblk->uuid, nd_label->uuid,
  1508. NSLABEL_UUID_LEN) == 0) {
  1509. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1510. __le64_to_cpu(nd_label->dpa));
  1511. if (!res)
  1512. goto err;
  1513. nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
  1514. dev_name(&nsblk->common.dev));
  1515. break;
  1516. }
  1517. }
  1518. if (i < count)
  1519. continue;
  1520. __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
  1521. if (!__devs)
  1522. goto err;
  1523. memcpy(__devs, devs, sizeof(dev) * count);
  1524. kfree(devs);
  1525. devs = __devs;
  1526. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1527. if (!nsblk)
  1528. goto err;
  1529. dev = &nsblk->common.dev;
  1530. dev->type = &namespace_blk_device_type;
  1531. dev->parent = &nd_region->dev;
  1532. dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
  1533. devs[count++] = dev;
  1534. nsblk->id = -1;
  1535. nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
  1536. nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
  1537. GFP_KERNEL);
  1538. if (!nsblk->uuid)
  1539. goto err;
  1540. memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
  1541. if (name[0])
  1542. nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
  1543. GFP_KERNEL);
  1544. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1545. __le64_to_cpu(nd_label->dpa));
  1546. if (!res)
  1547. goto err;
  1548. nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
  1549. dev_name(&nsblk->common.dev));
  1550. }
  1551. dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
  1552. __func__, count, count == 1 ? "" : "s");
  1553. if (count == 0) {
  1554. /* Publish a zero-sized namespace for userspace to configure. */
  1555. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1556. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1557. kfree(nd_mapping->labels);
  1558. nd_mapping->labels = NULL;
  1559. }
  1560. devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
  1561. if (!devs)
  1562. goto err;
  1563. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1564. if (!nsblk)
  1565. goto err;
  1566. dev = &nsblk->common.dev;
  1567. dev->type = &namespace_blk_device_type;
  1568. dev->parent = &nd_region->dev;
  1569. devs[count++] = dev;
  1570. }
  1571. return devs;
  1572. err:
  1573. for (i = 0; i < count; i++) {
  1574. nsblk = to_nd_namespace_blk(devs[i]);
  1575. namespace_blk_release(&nsblk->common.dev);
  1576. }
  1577. kfree(devs);
  1578. return NULL;
  1579. }
  1580. static int init_active_labels(struct nd_region *nd_region)
  1581. {
  1582. int i;
  1583. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1584. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1585. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1586. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  1587. int count, j;
  1588. /*
  1589. * If the dimm is disabled then prevent the region from
  1590. * being activated if it aliases DPA.
  1591. */
  1592. if (!ndd) {
  1593. if ((nvdimm->flags & NDD_ALIASING) == 0)
  1594. return 0;
  1595. dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
  1596. dev_name(&nd_mapping->nvdimm->dev));
  1597. return -ENXIO;
  1598. }
  1599. nd_mapping->ndd = ndd;
  1600. atomic_inc(&nvdimm->busy);
  1601. get_ndd(ndd);
  1602. count = nd_label_active_count(ndd);
  1603. dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
  1604. if (!count)
  1605. continue;
  1606. nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
  1607. GFP_KERNEL);
  1608. if (!nd_mapping->labels)
  1609. return -ENOMEM;
  1610. for (j = 0; j < count; j++) {
  1611. struct nd_namespace_label *label;
  1612. label = nd_label_active(ndd, j);
  1613. nd_mapping->labels[j] = label;
  1614. }
  1615. }
  1616. return 0;
  1617. }
  1618. int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
  1619. {
  1620. struct device **devs = NULL;
  1621. int i, rc = 0, type;
  1622. *err = 0;
  1623. nvdimm_bus_lock(&nd_region->dev);
  1624. rc = init_active_labels(nd_region);
  1625. if (rc) {
  1626. nvdimm_bus_unlock(&nd_region->dev);
  1627. return rc;
  1628. }
  1629. type = nd_region_to_nstype(nd_region);
  1630. switch (type) {
  1631. case ND_DEVICE_NAMESPACE_IO:
  1632. devs = create_namespace_io(nd_region);
  1633. break;
  1634. case ND_DEVICE_NAMESPACE_PMEM:
  1635. devs = create_namespace_pmem(nd_region);
  1636. break;
  1637. case ND_DEVICE_NAMESPACE_BLK:
  1638. devs = create_namespace_blk(nd_region);
  1639. break;
  1640. default:
  1641. break;
  1642. }
  1643. nvdimm_bus_unlock(&nd_region->dev);
  1644. if (!devs)
  1645. return -ENODEV;
  1646. for (i = 0; devs[i]; i++) {
  1647. struct device *dev = devs[i];
  1648. int id;
  1649. if (type == ND_DEVICE_NAMESPACE_BLK) {
  1650. struct nd_namespace_blk *nsblk;
  1651. nsblk = to_nd_namespace_blk(dev);
  1652. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  1653. GFP_KERNEL);
  1654. nsblk->id = id;
  1655. } else
  1656. id = i;
  1657. if (id < 0)
  1658. break;
  1659. dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
  1660. dev->groups = nd_namespace_attribute_groups;
  1661. nd_device_register(dev);
  1662. }
  1663. if (i)
  1664. nd_region->ns_seed = devs[0];
  1665. if (devs[i]) {
  1666. int j;
  1667. for (j = i; devs[j]; j++) {
  1668. struct device *dev = devs[j];
  1669. device_initialize(dev);
  1670. put_device(dev);
  1671. }
  1672. *err = j - i;
  1673. /*
  1674. * All of the namespaces we tried to register failed, so
  1675. * fail region activation.
  1676. */
  1677. if (*err == 0)
  1678. rc = -ENODEV;
  1679. }
  1680. kfree(devs);
  1681. if (rc == -ENODEV)
  1682. return rc;
  1683. return i;
  1684. }