namespace_devs.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  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/sort.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/nd.h>
  19. #include "nd-core.h"
  20. #include "pmem.h"
  21. #include "nd.h"
  22. static void namespace_io_release(struct device *dev)
  23. {
  24. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  25. kfree(nsio);
  26. }
  27. static void namespace_pmem_release(struct device *dev)
  28. {
  29. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  30. struct nd_region *nd_region = to_nd_region(dev->parent);
  31. if (nspm->id >= 0)
  32. ida_simple_remove(&nd_region->ns_ida, nspm->id);
  33. kfree(nspm->alt_name);
  34. kfree(nspm->uuid);
  35. kfree(nspm);
  36. }
  37. static void namespace_blk_release(struct device *dev)
  38. {
  39. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  40. struct nd_region *nd_region = to_nd_region(dev->parent);
  41. if (nsblk->id >= 0)
  42. ida_simple_remove(&nd_region->ns_ida, nsblk->id);
  43. kfree(nsblk->alt_name);
  44. kfree(nsblk->uuid);
  45. kfree(nsblk->res);
  46. kfree(nsblk);
  47. }
  48. static const struct device_type namespace_io_device_type = {
  49. .name = "nd_namespace_io",
  50. .release = namespace_io_release,
  51. };
  52. static const struct device_type namespace_pmem_device_type = {
  53. .name = "nd_namespace_pmem",
  54. .release = namespace_pmem_release,
  55. };
  56. static const struct device_type namespace_blk_device_type = {
  57. .name = "nd_namespace_blk",
  58. .release = namespace_blk_release,
  59. };
  60. static bool is_namespace_pmem(const struct device *dev)
  61. {
  62. return dev ? dev->type == &namespace_pmem_device_type : false;
  63. }
  64. static bool is_namespace_blk(const struct device *dev)
  65. {
  66. return dev ? dev->type == &namespace_blk_device_type : false;
  67. }
  68. static bool is_namespace_io(const struct device *dev)
  69. {
  70. return dev ? dev->type == &namespace_io_device_type : false;
  71. }
  72. static int is_uuid_busy(struct device *dev, void *data)
  73. {
  74. u8 *uuid1 = data, *uuid2 = NULL;
  75. if (is_namespace_pmem(dev)) {
  76. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  77. uuid2 = nspm->uuid;
  78. } else if (is_namespace_blk(dev)) {
  79. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  80. uuid2 = nsblk->uuid;
  81. } else if (is_nd_btt(dev)) {
  82. struct nd_btt *nd_btt = to_nd_btt(dev);
  83. uuid2 = nd_btt->uuid;
  84. } else if (is_nd_pfn(dev)) {
  85. struct nd_pfn *nd_pfn = to_nd_pfn(dev);
  86. uuid2 = nd_pfn->uuid;
  87. }
  88. if (uuid2 && memcmp(uuid1, uuid2, NSLABEL_UUID_LEN) == 0)
  89. return -EBUSY;
  90. return 0;
  91. }
  92. static int is_namespace_uuid_busy(struct device *dev, void *data)
  93. {
  94. if (is_nd_region(dev))
  95. return device_for_each_child(dev, data, is_uuid_busy);
  96. return 0;
  97. }
  98. /**
  99. * nd_is_uuid_unique - verify that no other namespace has @uuid
  100. * @dev: any device on a nvdimm_bus
  101. * @uuid: uuid to check
  102. */
  103. bool nd_is_uuid_unique(struct device *dev, u8 *uuid)
  104. {
  105. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(dev);
  106. if (!nvdimm_bus)
  107. return false;
  108. WARN_ON_ONCE(!is_nvdimm_bus_locked(&nvdimm_bus->dev));
  109. if (device_for_each_child(&nvdimm_bus->dev, uuid,
  110. is_namespace_uuid_busy) != 0)
  111. return false;
  112. return true;
  113. }
  114. bool pmem_should_map_pages(struct device *dev)
  115. {
  116. struct nd_region *nd_region = to_nd_region(dev->parent);
  117. struct nd_namespace_io *nsio;
  118. if (!IS_ENABLED(CONFIG_ZONE_DEVICE))
  119. return false;
  120. if (!test_bit(ND_REGION_PAGEMAP, &nd_region->flags))
  121. return false;
  122. if (is_nd_pfn(dev) || is_nd_btt(dev))
  123. return false;
  124. nsio = to_nd_namespace_io(dev);
  125. if (region_intersects(nsio->res.start, resource_size(&nsio->res),
  126. IORESOURCE_SYSTEM_RAM,
  127. IORES_DESC_NONE) == REGION_MIXED)
  128. return false;
  129. return ARCH_MEMREMAP_PMEM == MEMREMAP_WB;
  130. }
  131. EXPORT_SYMBOL(pmem_should_map_pages);
  132. unsigned int pmem_sector_size(struct nd_namespace_common *ndns)
  133. {
  134. if (is_namespace_pmem(&ndns->dev)) {
  135. struct nd_namespace_pmem *nspm;
  136. nspm = to_nd_namespace_pmem(&ndns->dev);
  137. if (nspm->lbasize == 0 || nspm->lbasize == 512)
  138. /* default */;
  139. else if (nspm->lbasize == 4096)
  140. return 4096;
  141. else
  142. dev_WARN(&ndns->dev, "unsupported sector size: %ld\n",
  143. nspm->lbasize);
  144. }
  145. /*
  146. * There is no namespace label (is_namespace_io()), or the label
  147. * indicates the default sector size.
  148. */
  149. return 512;
  150. }
  151. EXPORT_SYMBOL(pmem_sector_size);
  152. const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
  153. char *name)
  154. {
  155. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  156. const char *suffix = NULL;
  157. if (ndns->claim && is_nd_btt(ndns->claim))
  158. suffix = "s";
  159. if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev)) {
  160. int nsidx = 0;
  161. if (is_namespace_pmem(&ndns->dev)) {
  162. struct nd_namespace_pmem *nspm;
  163. nspm = to_nd_namespace_pmem(&ndns->dev);
  164. nsidx = nspm->id;
  165. }
  166. if (nsidx)
  167. sprintf(name, "pmem%d.%d%s", nd_region->id, nsidx,
  168. suffix ? suffix : "");
  169. else
  170. sprintf(name, "pmem%d%s", nd_region->id,
  171. suffix ? suffix : "");
  172. } else if (is_namespace_blk(&ndns->dev)) {
  173. struct nd_namespace_blk *nsblk;
  174. nsblk = to_nd_namespace_blk(&ndns->dev);
  175. sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id,
  176. suffix ? suffix : "");
  177. } else {
  178. return NULL;
  179. }
  180. return name;
  181. }
  182. EXPORT_SYMBOL(nvdimm_namespace_disk_name);
  183. const u8 *nd_dev_to_uuid(struct device *dev)
  184. {
  185. static const u8 null_uuid[16];
  186. if (!dev)
  187. return null_uuid;
  188. if (is_namespace_pmem(dev)) {
  189. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  190. return nspm->uuid;
  191. } else if (is_namespace_blk(dev)) {
  192. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  193. return nsblk->uuid;
  194. } else
  195. return null_uuid;
  196. }
  197. EXPORT_SYMBOL(nd_dev_to_uuid);
  198. static ssize_t nstype_show(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. struct nd_region *nd_region = to_nd_region(dev->parent);
  202. return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
  203. }
  204. static DEVICE_ATTR_RO(nstype);
  205. static ssize_t __alt_name_store(struct device *dev, const char *buf,
  206. const size_t len)
  207. {
  208. char *input, *pos, *alt_name, **ns_altname;
  209. ssize_t rc;
  210. if (is_namespace_pmem(dev)) {
  211. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  212. ns_altname = &nspm->alt_name;
  213. } else if (is_namespace_blk(dev)) {
  214. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  215. ns_altname = &nsblk->alt_name;
  216. } else
  217. return -ENXIO;
  218. if (dev->driver || to_ndns(dev)->claim)
  219. return -EBUSY;
  220. input = kmemdup(buf, len + 1, GFP_KERNEL);
  221. if (!input)
  222. return -ENOMEM;
  223. input[len] = '\0';
  224. pos = strim(input);
  225. if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
  226. rc = -EINVAL;
  227. goto out;
  228. }
  229. alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
  230. if (!alt_name) {
  231. rc = -ENOMEM;
  232. goto out;
  233. }
  234. kfree(*ns_altname);
  235. *ns_altname = alt_name;
  236. sprintf(*ns_altname, "%s", pos);
  237. rc = len;
  238. out:
  239. kfree(input);
  240. return rc;
  241. }
  242. static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
  243. {
  244. struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
  245. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  246. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  247. struct nd_label_id label_id;
  248. resource_size_t size = 0;
  249. struct resource *res;
  250. if (!nsblk->uuid)
  251. return 0;
  252. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  253. for_each_dpa_resource(ndd, res)
  254. if (strcmp(res->name, label_id.id) == 0)
  255. size += resource_size(res);
  256. return size;
  257. }
  258. static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
  259. {
  260. struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
  261. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  262. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  263. struct nd_label_id label_id;
  264. struct resource *res;
  265. int count, i;
  266. if (!nsblk->uuid || !nsblk->lbasize || !ndd)
  267. return false;
  268. count = 0;
  269. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  270. for_each_dpa_resource(ndd, res) {
  271. if (strcmp(res->name, label_id.id) != 0)
  272. continue;
  273. /*
  274. * Resources with unacknowledged adjustments indicate a
  275. * failure to update labels
  276. */
  277. if (res->flags & DPA_RESOURCE_ADJUSTED)
  278. return false;
  279. count++;
  280. }
  281. /* These values match after a successful label update */
  282. if (count != nsblk->num_resources)
  283. return false;
  284. for (i = 0; i < nsblk->num_resources; i++) {
  285. struct resource *found = NULL;
  286. for_each_dpa_resource(ndd, res)
  287. if (res == nsblk->res[i]) {
  288. found = res;
  289. break;
  290. }
  291. /* stale resource */
  292. if (!found)
  293. return false;
  294. }
  295. return true;
  296. }
  297. resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
  298. {
  299. resource_size_t size;
  300. nvdimm_bus_lock(&nsblk->common.dev);
  301. size = __nd_namespace_blk_validate(nsblk);
  302. nvdimm_bus_unlock(&nsblk->common.dev);
  303. return size;
  304. }
  305. EXPORT_SYMBOL(nd_namespace_blk_validate);
  306. static int nd_namespace_label_update(struct nd_region *nd_region,
  307. struct device *dev)
  308. {
  309. dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
  310. "namespace must be idle during label update\n");
  311. if (dev->driver || to_ndns(dev)->claim)
  312. return 0;
  313. /*
  314. * Only allow label writes that will result in a valid namespace
  315. * or deletion of an existing namespace.
  316. */
  317. if (is_namespace_pmem(dev)) {
  318. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  319. resource_size_t size = resource_size(&nspm->nsio.res);
  320. if (size == 0 && nspm->uuid)
  321. /* delete allocation */;
  322. else if (!nspm->uuid)
  323. return 0;
  324. return nd_pmem_namespace_label_update(nd_region, nspm, size);
  325. } else if (is_namespace_blk(dev)) {
  326. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  327. resource_size_t size = nd_namespace_blk_size(nsblk);
  328. if (size == 0 && nsblk->uuid)
  329. /* delete allocation */;
  330. else if (!nsblk->uuid || !nsblk->lbasize)
  331. return 0;
  332. return nd_blk_namespace_label_update(nd_region, nsblk, size);
  333. } else
  334. return -ENXIO;
  335. }
  336. static ssize_t alt_name_store(struct device *dev,
  337. struct device_attribute *attr, const char *buf, size_t len)
  338. {
  339. struct nd_region *nd_region = to_nd_region(dev->parent);
  340. ssize_t rc;
  341. device_lock(dev);
  342. nvdimm_bus_lock(dev);
  343. wait_nvdimm_bus_probe_idle(dev);
  344. rc = __alt_name_store(dev, buf, len);
  345. if (rc >= 0)
  346. rc = nd_namespace_label_update(nd_region, dev);
  347. dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
  348. nvdimm_bus_unlock(dev);
  349. device_unlock(dev);
  350. return rc < 0 ? rc : len;
  351. }
  352. static ssize_t alt_name_show(struct device *dev,
  353. struct device_attribute *attr, char *buf)
  354. {
  355. char *ns_altname;
  356. if (is_namespace_pmem(dev)) {
  357. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  358. ns_altname = nspm->alt_name;
  359. } else if (is_namespace_blk(dev)) {
  360. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  361. ns_altname = nsblk->alt_name;
  362. } else
  363. return -ENXIO;
  364. return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
  365. }
  366. static DEVICE_ATTR_RW(alt_name);
  367. static int scan_free(struct nd_region *nd_region,
  368. struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
  369. resource_size_t n)
  370. {
  371. bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
  372. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  373. int rc = 0;
  374. while (n) {
  375. struct resource *res, *last;
  376. resource_size_t new_start;
  377. last = NULL;
  378. for_each_dpa_resource(ndd, res)
  379. if (strcmp(res->name, label_id->id) == 0)
  380. last = res;
  381. res = last;
  382. if (!res)
  383. return 0;
  384. if (n >= resource_size(res)) {
  385. n -= resource_size(res);
  386. nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
  387. nvdimm_free_dpa(ndd, res);
  388. /* retry with last resource deleted */
  389. continue;
  390. }
  391. /*
  392. * Keep BLK allocations relegated to high DPA as much as
  393. * possible
  394. */
  395. if (is_blk)
  396. new_start = res->start + n;
  397. else
  398. new_start = res->start;
  399. rc = adjust_resource(res, new_start, resource_size(res) - n);
  400. if (rc == 0)
  401. res->flags |= DPA_RESOURCE_ADJUSTED;
  402. nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
  403. break;
  404. }
  405. return rc;
  406. }
  407. /**
  408. * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
  409. * @nd_region: the set of dimms to reclaim @n bytes from
  410. * @label_id: unique identifier for the namespace consuming this dpa range
  411. * @n: number of bytes per-dimm to release
  412. *
  413. * Assumes resources are ordered. Starting from the end try to
  414. * adjust_resource() the allocation to @n, but if @n is larger than the
  415. * allocation delete it and find the 'new' last allocation in the label
  416. * set.
  417. */
  418. static int shrink_dpa_allocation(struct nd_region *nd_region,
  419. struct nd_label_id *label_id, resource_size_t n)
  420. {
  421. int i;
  422. for (i = 0; i < nd_region->ndr_mappings; i++) {
  423. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  424. int rc;
  425. rc = scan_free(nd_region, nd_mapping, label_id, n);
  426. if (rc)
  427. return rc;
  428. }
  429. return 0;
  430. }
  431. static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
  432. struct nd_region *nd_region, struct nd_mapping *nd_mapping,
  433. resource_size_t n)
  434. {
  435. bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
  436. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  437. resource_size_t first_dpa;
  438. struct resource *res;
  439. int rc = 0;
  440. /* allocate blk from highest dpa first */
  441. if (is_blk)
  442. first_dpa = nd_mapping->start + nd_mapping->size - n;
  443. else
  444. first_dpa = nd_mapping->start;
  445. /* first resource allocation for this label-id or dimm */
  446. res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
  447. if (!res)
  448. rc = -EBUSY;
  449. nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
  450. return rc ? n : 0;
  451. }
  452. /**
  453. * space_valid() - validate free dpa space against constraints
  454. * @nd_region: hosting region of the free space
  455. * @ndd: dimm device data for debug
  456. * @label_id: namespace id to allocate space
  457. * @prev: potential allocation that precedes free space
  458. * @next: allocation that follows the given free space range
  459. * @exist: first allocation with same id in the mapping
  460. * @n: range that must satisfied for pmem allocations
  461. * @valid: free space range to validate
  462. *
  463. * BLK-space is valid as long as it does not precede a PMEM
  464. * allocation in a given region. PMEM-space must be contiguous
  465. * and adjacent to an existing existing allocation (if one
  466. * exists). If reserving PMEM any space is valid.
  467. */
  468. static void space_valid(struct nd_region *nd_region, struct nvdimm_drvdata *ndd,
  469. struct nd_label_id *label_id, struct resource *prev,
  470. struct resource *next, struct resource *exist,
  471. resource_size_t n, struct resource *valid)
  472. {
  473. bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
  474. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  475. if (valid->start >= valid->end)
  476. goto invalid;
  477. if (is_reserve)
  478. return;
  479. if (!is_pmem) {
  480. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  481. struct nvdimm_bus *nvdimm_bus;
  482. struct blk_alloc_info info = {
  483. .nd_mapping = nd_mapping,
  484. .available = nd_mapping->size,
  485. .res = valid,
  486. };
  487. WARN_ON(!is_nd_blk(&nd_region->dev));
  488. nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  489. device_for_each_child(&nvdimm_bus->dev, &info, alias_dpa_busy);
  490. return;
  491. }
  492. /* allocation needs to be contiguous, so this is all or nothing */
  493. if (resource_size(valid) < n)
  494. goto invalid;
  495. /* we've got all the space we need and no existing allocation */
  496. if (!exist)
  497. return;
  498. /* allocation needs to be contiguous with the existing namespace */
  499. if (valid->start == exist->end + 1
  500. || valid->end == exist->start - 1)
  501. return;
  502. invalid:
  503. /* truncate @valid size to 0 */
  504. valid->end = valid->start - 1;
  505. }
  506. enum alloc_loc {
  507. ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
  508. };
  509. static resource_size_t scan_allocate(struct nd_region *nd_region,
  510. struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
  511. resource_size_t n)
  512. {
  513. resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
  514. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  515. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  516. struct resource *res, *exist = NULL, valid;
  517. const resource_size_t to_allocate = n;
  518. int first;
  519. for_each_dpa_resource(ndd, res)
  520. if (strcmp(label_id->id, res->name) == 0)
  521. exist = res;
  522. valid.start = nd_mapping->start;
  523. valid.end = mapping_end;
  524. valid.name = "free space";
  525. retry:
  526. first = 0;
  527. for_each_dpa_resource(ndd, res) {
  528. struct resource *next = res->sibling, *new_res = NULL;
  529. resource_size_t allocate, available = 0;
  530. enum alloc_loc loc = ALLOC_ERR;
  531. const char *action;
  532. int rc = 0;
  533. /* ignore resources outside this nd_mapping */
  534. if (res->start > mapping_end)
  535. continue;
  536. if (res->end < nd_mapping->start)
  537. continue;
  538. /* space at the beginning of the mapping */
  539. if (!first++ && res->start > nd_mapping->start) {
  540. valid.start = nd_mapping->start;
  541. valid.end = res->start - 1;
  542. space_valid(nd_region, ndd, label_id, NULL, next, exist,
  543. to_allocate, &valid);
  544. available = resource_size(&valid);
  545. if (available)
  546. loc = ALLOC_BEFORE;
  547. }
  548. /* space between allocations */
  549. if (!loc && next) {
  550. valid.start = res->start + resource_size(res);
  551. valid.end = min(mapping_end, next->start - 1);
  552. space_valid(nd_region, ndd, label_id, res, next, exist,
  553. to_allocate, &valid);
  554. available = resource_size(&valid);
  555. if (available)
  556. loc = ALLOC_MID;
  557. }
  558. /* space at the end of the mapping */
  559. if (!loc && !next) {
  560. valid.start = res->start + resource_size(res);
  561. valid.end = mapping_end;
  562. space_valid(nd_region, ndd, label_id, res, next, exist,
  563. to_allocate, &valid);
  564. available = resource_size(&valid);
  565. if (available)
  566. loc = ALLOC_AFTER;
  567. }
  568. if (!loc || !available)
  569. continue;
  570. allocate = min(available, n);
  571. switch (loc) {
  572. case ALLOC_BEFORE:
  573. if (strcmp(res->name, label_id->id) == 0) {
  574. /* adjust current resource up */
  575. rc = adjust_resource(res, res->start - allocate,
  576. resource_size(res) + allocate);
  577. action = "cur grow up";
  578. } else
  579. action = "allocate";
  580. break;
  581. case ALLOC_MID:
  582. if (strcmp(next->name, label_id->id) == 0) {
  583. /* adjust next resource up */
  584. rc = adjust_resource(next, next->start
  585. - allocate, resource_size(next)
  586. + allocate);
  587. new_res = next;
  588. action = "next grow up";
  589. } else if (strcmp(res->name, label_id->id) == 0) {
  590. action = "grow down";
  591. } else
  592. action = "allocate";
  593. break;
  594. case ALLOC_AFTER:
  595. if (strcmp(res->name, label_id->id) == 0)
  596. action = "grow down";
  597. else
  598. action = "allocate";
  599. break;
  600. default:
  601. return n;
  602. }
  603. if (strcmp(action, "allocate") == 0) {
  604. /* BLK allocate bottom up */
  605. if (!is_pmem)
  606. valid.start += available - allocate;
  607. new_res = nvdimm_allocate_dpa(ndd, label_id,
  608. valid.start, allocate);
  609. if (!new_res)
  610. rc = -EBUSY;
  611. } else if (strcmp(action, "grow down") == 0) {
  612. /* adjust current resource down */
  613. rc = adjust_resource(res, res->start, resource_size(res)
  614. + allocate);
  615. if (rc == 0)
  616. res->flags |= DPA_RESOURCE_ADJUSTED;
  617. }
  618. if (!new_res)
  619. new_res = res;
  620. nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
  621. action, loc, rc);
  622. if (rc)
  623. return n;
  624. n -= allocate;
  625. if (n) {
  626. /*
  627. * Retry scan with newly inserted resources.
  628. * For example, if we did an ALLOC_BEFORE
  629. * insertion there may also have been space
  630. * available for an ALLOC_AFTER insertion, so we
  631. * need to check this same resource again
  632. */
  633. goto retry;
  634. } else
  635. return 0;
  636. }
  637. /*
  638. * If we allocated nothing in the BLK case it may be because we are in
  639. * an initial "pmem-reserve pass". Only do an initial BLK allocation
  640. * when none of the DPA space is reserved.
  641. */
  642. if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
  643. return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
  644. return n;
  645. }
  646. static int merge_dpa(struct nd_region *nd_region,
  647. struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
  648. {
  649. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  650. struct resource *res;
  651. if (strncmp("pmem", label_id->id, 4) == 0)
  652. return 0;
  653. retry:
  654. for_each_dpa_resource(ndd, res) {
  655. int rc;
  656. struct resource *next = res->sibling;
  657. resource_size_t end = res->start + resource_size(res);
  658. if (!next || strcmp(res->name, label_id->id) != 0
  659. || strcmp(next->name, label_id->id) != 0
  660. || end != next->start)
  661. continue;
  662. end += resource_size(next);
  663. nvdimm_free_dpa(ndd, next);
  664. rc = adjust_resource(res, res->start, end - res->start);
  665. nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
  666. if (rc)
  667. return rc;
  668. res->flags |= DPA_RESOURCE_ADJUSTED;
  669. goto retry;
  670. }
  671. return 0;
  672. }
  673. static int __reserve_free_pmem(struct device *dev, void *data)
  674. {
  675. struct nvdimm *nvdimm = data;
  676. struct nd_region *nd_region;
  677. struct nd_label_id label_id;
  678. int i;
  679. if (!is_memory(dev))
  680. return 0;
  681. nd_region = to_nd_region(dev);
  682. if (nd_region->ndr_mappings == 0)
  683. return 0;
  684. memset(&label_id, 0, sizeof(label_id));
  685. strcat(label_id.id, "pmem-reserve");
  686. for (i = 0; i < nd_region->ndr_mappings; i++) {
  687. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  688. resource_size_t n, rem = 0;
  689. if (nd_mapping->nvdimm != nvdimm)
  690. continue;
  691. n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
  692. if (n == 0)
  693. return 0;
  694. rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
  695. dev_WARN_ONCE(&nd_region->dev, rem,
  696. "pmem reserve underrun: %#llx of %#llx bytes\n",
  697. (unsigned long long) n - rem,
  698. (unsigned long long) n);
  699. return rem ? -ENXIO : 0;
  700. }
  701. return 0;
  702. }
  703. static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
  704. struct nd_mapping *nd_mapping)
  705. {
  706. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  707. struct resource *res, *_res;
  708. for_each_dpa_resource_safe(ndd, res, _res)
  709. if (strcmp(res->name, "pmem-reserve") == 0)
  710. nvdimm_free_dpa(ndd, res);
  711. }
  712. static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
  713. struct nd_mapping *nd_mapping)
  714. {
  715. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  716. int rc;
  717. rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
  718. __reserve_free_pmem);
  719. if (rc)
  720. release_free_pmem(nvdimm_bus, nd_mapping);
  721. return rc;
  722. }
  723. /**
  724. * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
  725. * @nd_region: the set of dimms to allocate @n more bytes from
  726. * @label_id: unique identifier for the namespace consuming this dpa range
  727. * @n: number of bytes per-dimm to add to the existing allocation
  728. *
  729. * Assumes resources are ordered. For BLK regions, first consume
  730. * BLK-only available DPA free space, then consume PMEM-aliased DPA
  731. * space starting at the highest DPA. For PMEM regions start
  732. * allocations from the start of an interleave set and end at the first
  733. * BLK allocation or the end of the interleave set, whichever comes
  734. * first.
  735. */
  736. static int grow_dpa_allocation(struct nd_region *nd_region,
  737. struct nd_label_id *label_id, resource_size_t n)
  738. {
  739. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  740. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  741. int i;
  742. for (i = 0; i < nd_region->ndr_mappings; i++) {
  743. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  744. resource_size_t rem = n;
  745. int rc, j;
  746. /*
  747. * In the BLK case try once with all unallocated PMEM
  748. * reserved, and once without
  749. */
  750. for (j = is_pmem; j < 2; j++) {
  751. bool blk_only = j == 0;
  752. if (blk_only) {
  753. rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
  754. if (rc)
  755. return rc;
  756. }
  757. rem = scan_allocate(nd_region, nd_mapping,
  758. label_id, rem);
  759. if (blk_only)
  760. release_free_pmem(nvdimm_bus, nd_mapping);
  761. /* try again and allow encroachments into PMEM */
  762. if (rem == 0)
  763. break;
  764. }
  765. dev_WARN_ONCE(&nd_region->dev, rem,
  766. "allocation underrun: %#llx of %#llx bytes\n",
  767. (unsigned long long) n - rem,
  768. (unsigned long long) n);
  769. if (rem)
  770. return -ENXIO;
  771. rc = merge_dpa(nd_region, nd_mapping, label_id);
  772. if (rc)
  773. return rc;
  774. }
  775. return 0;
  776. }
  777. static void nd_namespace_pmem_set_resource(struct nd_region *nd_region,
  778. struct nd_namespace_pmem *nspm, resource_size_t size)
  779. {
  780. struct resource *res = &nspm->nsio.res;
  781. resource_size_t offset = 0;
  782. if (size && !nspm->uuid) {
  783. WARN_ON_ONCE(1);
  784. size = 0;
  785. }
  786. if (size && nspm->uuid) {
  787. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  788. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  789. struct nd_label_id label_id;
  790. struct resource *res;
  791. if (!ndd) {
  792. size = 0;
  793. goto out;
  794. }
  795. nd_label_gen_id(&label_id, nspm->uuid, 0);
  796. /* calculate a spa offset from the dpa allocation offset */
  797. for_each_dpa_resource(ndd, res)
  798. if (strcmp(res->name, label_id.id) == 0) {
  799. offset = (res->start - nd_mapping->start)
  800. * nd_region->ndr_mappings;
  801. goto out;
  802. }
  803. WARN_ON_ONCE(1);
  804. size = 0;
  805. }
  806. out:
  807. res->start = nd_region->ndr_start + offset;
  808. res->end = res->start + size - 1;
  809. }
  810. static bool uuid_not_set(const u8 *uuid, struct device *dev, const char *where)
  811. {
  812. if (!uuid) {
  813. dev_dbg(dev, "%s: uuid not set\n", where);
  814. return true;
  815. }
  816. return false;
  817. }
  818. static ssize_t __size_store(struct device *dev, unsigned long long val)
  819. {
  820. resource_size_t allocated = 0, available = 0;
  821. struct nd_region *nd_region = to_nd_region(dev->parent);
  822. struct nd_namespace_common *ndns = to_ndns(dev);
  823. struct nd_mapping *nd_mapping;
  824. struct nvdimm_drvdata *ndd;
  825. struct nd_label_id label_id;
  826. u32 flags = 0, remainder;
  827. int rc, i, id = -1;
  828. u8 *uuid = NULL;
  829. if (dev->driver || ndns->claim)
  830. return -EBUSY;
  831. if (is_namespace_pmem(dev)) {
  832. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  833. uuid = nspm->uuid;
  834. id = nspm->id;
  835. } else if (is_namespace_blk(dev)) {
  836. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  837. uuid = nsblk->uuid;
  838. flags = NSLABEL_FLAG_LOCAL;
  839. id = nsblk->id;
  840. }
  841. /*
  842. * We need a uuid for the allocation-label and dimm(s) on which
  843. * to store the label.
  844. */
  845. if (uuid_not_set(uuid, dev, __func__))
  846. return -ENXIO;
  847. if (nd_region->ndr_mappings == 0) {
  848. dev_dbg(dev, "not associated with dimm(s)\n");
  849. return -ENXIO;
  850. }
  851. div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
  852. if (remainder) {
  853. dev_dbg(dev, "%llu is not %dK aligned\n", val,
  854. (SZ_4K * nd_region->ndr_mappings) / SZ_1K);
  855. return -EINVAL;
  856. }
  857. nd_label_gen_id(&label_id, uuid, flags);
  858. for (i = 0; i < nd_region->ndr_mappings; i++) {
  859. nd_mapping = &nd_region->mapping[i];
  860. ndd = to_ndd(nd_mapping);
  861. /*
  862. * All dimms in an interleave set, or the base dimm for a blk
  863. * region, need to be enabled for the size to be changed.
  864. */
  865. if (!ndd)
  866. return -ENXIO;
  867. allocated += nvdimm_allocated_dpa(ndd, &label_id);
  868. }
  869. available = nd_region_available_dpa(nd_region);
  870. if (val > available + allocated)
  871. return -ENOSPC;
  872. if (val == allocated)
  873. return 0;
  874. val = div_u64(val, nd_region->ndr_mappings);
  875. allocated = div_u64(allocated, nd_region->ndr_mappings);
  876. if (val < allocated)
  877. rc = shrink_dpa_allocation(nd_region, &label_id,
  878. allocated - val);
  879. else
  880. rc = grow_dpa_allocation(nd_region, &label_id, val - allocated);
  881. if (rc)
  882. return rc;
  883. if (is_namespace_pmem(dev)) {
  884. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  885. nd_namespace_pmem_set_resource(nd_region, nspm,
  886. val * nd_region->ndr_mappings);
  887. }
  888. /*
  889. * Try to delete the namespace if we deleted all of its
  890. * allocation, this is not the seed or 0th device for the
  891. * region, and it is not actively claimed by a btt, pfn, or dax
  892. * instance.
  893. */
  894. if (val == 0 && id != 0 && nd_region->ns_seed != dev && !ndns->claim)
  895. nd_device_unregister(dev, ND_ASYNC);
  896. return rc;
  897. }
  898. static ssize_t size_store(struct device *dev,
  899. struct device_attribute *attr, const char *buf, size_t len)
  900. {
  901. struct nd_region *nd_region = to_nd_region(dev->parent);
  902. unsigned long long val;
  903. u8 **uuid = NULL;
  904. int rc;
  905. rc = kstrtoull(buf, 0, &val);
  906. if (rc)
  907. return rc;
  908. device_lock(dev);
  909. nvdimm_bus_lock(dev);
  910. wait_nvdimm_bus_probe_idle(dev);
  911. rc = __size_store(dev, val);
  912. if (rc >= 0)
  913. rc = nd_namespace_label_update(nd_region, dev);
  914. if (is_namespace_pmem(dev)) {
  915. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  916. uuid = &nspm->uuid;
  917. } else if (is_namespace_blk(dev)) {
  918. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  919. uuid = &nsblk->uuid;
  920. }
  921. if (rc == 0 && val == 0 && uuid) {
  922. /* setting size zero == 'delete namespace' */
  923. kfree(*uuid);
  924. *uuid = NULL;
  925. }
  926. dev_dbg(dev, "%llx %s (%d)\n", val, rc < 0 ? "fail" : "success", rc);
  927. nvdimm_bus_unlock(dev);
  928. device_unlock(dev);
  929. return rc < 0 ? rc : len;
  930. }
  931. resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  932. {
  933. struct device *dev = &ndns->dev;
  934. if (is_namespace_pmem(dev)) {
  935. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  936. return resource_size(&nspm->nsio.res);
  937. } else if (is_namespace_blk(dev)) {
  938. return nd_namespace_blk_size(to_nd_namespace_blk(dev));
  939. } else if (is_namespace_io(dev)) {
  940. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  941. return resource_size(&nsio->res);
  942. } else
  943. WARN_ONCE(1, "unknown namespace type\n");
  944. return 0;
  945. }
  946. resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  947. {
  948. resource_size_t size;
  949. nvdimm_bus_lock(&ndns->dev);
  950. size = __nvdimm_namespace_capacity(ndns);
  951. nvdimm_bus_unlock(&ndns->dev);
  952. return size;
  953. }
  954. EXPORT_SYMBOL(nvdimm_namespace_capacity);
  955. static ssize_t size_show(struct device *dev,
  956. struct device_attribute *attr, char *buf)
  957. {
  958. return sprintf(buf, "%llu\n", (unsigned long long)
  959. nvdimm_namespace_capacity(to_ndns(dev)));
  960. }
  961. static DEVICE_ATTR(size, 0444, size_show, size_store);
  962. static u8 *namespace_to_uuid(struct device *dev)
  963. {
  964. if (is_namespace_pmem(dev)) {
  965. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  966. return nspm->uuid;
  967. } else if (is_namespace_blk(dev)) {
  968. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  969. return nsblk->uuid;
  970. } else
  971. return ERR_PTR(-ENXIO);
  972. }
  973. static ssize_t uuid_show(struct device *dev,
  974. struct device_attribute *attr, char *buf)
  975. {
  976. u8 *uuid = namespace_to_uuid(dev);
  977. if (IS_ERR(uuid))
  978. return PTR_ERR(uuid);
  979. if (uuid)
  980. return sprintf(buf, "%pUb\n", uuid);
  981. return sprintf(buf, "\n");
  982. }
  983. /**
  984. * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
  985. * @nd_region: parent region so we can updates all dimms in the set
  986. * @dev: namespace type for generating label_id
  987. * @new_uuid: incoming uuid
  988. * @old_uuid: reference to the uuid storage location in the namespace object
  989. */
  990. static int namespace_update_uuid(struct nd_region *nd_region,
  991. struct device *dev, u8 *new_uuid, u8 **old_uuid)
  992. {
  993. u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
  994. struct nd_label_id old_label_id;
  995. struct nd_label_id new_label_id;
  996. int i;
  997. if (!nd_is_uuid_unique(dev, new_uuid))
  998. return -EINVAL;
  999. if (*old_uuid == NULL)
  1000. goto out;
  1001. /*
  1002. * If we've already written a label with this uuid, then it's
  1003. * too late to rename because we can't reliably update the uuid
  1004. * without losing the old namespace. Userspace must delete this
  1005. * namespace to abandon the old uuid.
  1006. */
  1007. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1008. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1009. /*
  1010. * This check by itself is sufficient because old_uuid
  1011. * would be NULL above if this uuid did not exist in the
  1012. * currently written set.
  1013. *
  1014. * FIXME: can we delete uuid with zero dpa allocated?
  1015. */
  1016. if (list_empty(&nd_mapping->labels))
  1017. return -EBUSY;
  1018. }
  1019. nd_label_gen_id(&old_label_id, *old_uuid, flags);
  1020. nd_label_gen_id(&new_label_id, new_uuid, flags);
  1021. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1022. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1023. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1024. struct resource *res;
  1025. for_each_dpa_resource(ndd, res)
  1026. if (strcmp(res->name, old_label_id.id) == 0)
  1027. sprintf((void *) res->name, "%s",
  1028. new_label_id.id);
  1029. }
  1030. kfree(*old_uuid);
  1031. out:
  1032. *old_uuid = new_uuid;
  1033. return 0;
  1034. }
  1035. static ssize_t uuid_store(struct device *dev,
  1036. struct device_attribute *attr, const char *buf, size_t len)
  1037. {
  1038. struct nd_region *nd_region = to_nd_region(dev->parent);
  1039. u8 *uuid = NULL;
  1040. ssize_t rc = 0;
  1041. u8 **ns_uuid;
  1042. if (is_namespace_pmem(dev)) {
  1043. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1044. ns_uuid = &nspm->uuid;
  1045. } else if (is_namespace_blk(dev)) {
  1046. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1047. ns_uuid = &nsblk->uuid;
  1048. } else
  1049. return -ENXIO;
  1050. device_lock(dev);
  1051. nvdimm_bus_lock(dev);
  1052. wait_nvdimm_bus_probe_idle(dev);
  1053. if (to_ndns(dev)->claim)
  1054. rc = -EBUSY;
  1055. if (rc >= 0)
  1056. rc = nd_uuid_store(dev, &uuid, buf, len);
  1057. if (rc >= 0)
  1058. rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
  1059. if (rc >= 0)
  1060. rc = nd_namespace_label_update(nd_region, dev);
  1061. else
  1062. kfree(uuid);
  1063. dev_dbg(dev, "result: %zd wrote: %s%s", rc, buf,
  1064. buf[len - 1] == '\n' ? "" : "\n");
  1065. nvdimm_bus_unlock(dev);
  1066. device_unlock(dev);
  1067. return rc < 0 ? rc : len;
  1068. }
  1069. static DEVICE_ATTR_RW(uuid);
  1070. static ssize_t resource_show(struct device *dev,
  1071. struct device_attribute *attr, char *buf)
  1072. {
  1073. struct resource *res;
  1074. if (is_namespace_pmem(dev)) {
  1075. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1076. res = &nspm->nsio.res;
  1077. } else if (is_namespace_io(dev)) {
  1078. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  1079. res = &nsio->res;
  1080. } else
  1081. return -ENXIO;
  1082. /* no address to convey if the namespace has no allocation */
  1083. if (resource_size(res) == 0)
  1084. return -ENXIO;
  1085. return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
  1086. }
  1087. static DEVICE_ATTR_RO(resource);
  1088. static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
  1089. 4096, 4104, 4160, 4224, 0 };
  1090. static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
  1091. static ssize_t sector_size_show(struct device *dev,
  1092. struct device_attribute *attr, char *buf)
  1093. {
  1094. if (is_namespace_blk(dev)) {
  1095. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1096. return nd_size_select_show(nsblk->lbasize,
  1097. blk_lbasize_supported, buf);
  1098. }
  1099. if (is_namespace_pmem(dev)) {
  1100. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1101. return nd_size_select_show(nspm->lbasize,
  1102. pmem_lbasize_supported, buf);
  1103. }
  1104. return -ENXIO;
  1105. }
  1106. static ssize_t sector_size_store(struct device *dev,
  1107. struct device_attribute *attr, const char *buf, size_t len)
  1108. {
  1109. struct nd_region *nd_region = to_nd_region(dev->parent);
  1110. const unsigned long *supported;
  1111. unsigned long *lbasize;
  1112. ssize_t rc = 0;
  1113. if (is_namespace_blk(dev)) {
  1114. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1115. lbasize = &nsblk->lbasize;
  1116. supported = blk_lbasize_supported;
  1117. } else if (is_namespace_pmem(dev)) {
  1118. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1119. lbasize = &nspm->lbasize;
  1120. supported = pmem_lbasize_supported;
  1121. } else
  1122. return -ENXIO;
  1123. device_lock(dev);
  1124. nvdimm_bus_lock(dev);
  1125. if (to_ndns(dev)->claim)
  1126. rc = -EBUSY;
  1127. if (rc >= 0)
  1128. rc = nd_size_select_store(dev, buf, lbasize, supported);
  1129. if (rc >= 0)
  1130. rc = nd_namespace_label_update(nd_region, dev);
  1131. dev_dbg(dev, "result: %zd %s: %s%s", rc, rc < 0 ? "tried" : "wrote",
  1132. buf, buf[len - 1] == '\n' ? "" : "\n");
  1133. nvdimm_bus_unlock(dev);
  1134. device_unlock(dev);
  1135. return rc ? rc : len;
  1136. }
  1137. static DEVICE_ATTR_RW(sector_size);
  1138. static ssize_t dpa_extents_show(struct device *dev,
  1139. struct device_attribute *attr, char *buf)
  1140. {
  1141. struct nd_region *nd_region = to_nd_region(dev->parent);
  1142. struct nd_label_id label_id;
  1143. int count = 0, i;
  1144. u8 *uuid = NULL;
  1145. u32 flags = 0;
  1146. nvdimm_bus_lock(dev);
  1147. if (is_namespace_pmem(dev)) {
  1148. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1149. uuid = nspm->uuid;
  1150. flags = 0;
  1151. } else if (is_namespace_blk(dev)) {
  1152. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1153. uuid = nsblk->uuid;
  1154. flags = NSLABEL_FLAG_LOCAL;
  1155. }
  1156. if (!uuid)
  1157. goto out;
  1158. nd_label_gen_id(&label_id, uuid, flags);
  1159. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1160. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1161. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1162. struct resource *res;
  1163. for_each_dpa_resource(ndd, res)
  1164. if (strcmp(res->name, label_id.id) == 0)
  1165. count++;
  1166. }
  1167. out:
  1168. nvdimm_bus_unlock(dev);
  1169. return sprintf(buf, "%d\n", count);
  1170. }
  1171. static DEVICE_ATTR_RO(dpa_extents);
  1172. static int btt_claim_class(struct device *dev)
  1173. {
  1174. struct nd_region *nd_region = to_nd_region(dev->parent);
  1175. int i, loop_bitmask = 0;
  1176. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1177. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1178. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1179. struct nd_namespace_index *nsindex;
  1180. /*
  1181. * If any of the DIMMs do not support labels the only
  1182. * possible BTT format is v1.
  1183. */
  1184. if (!ndd) {
  1185. loop_bitmask = 0;
  1186. break;
  1187. }
  1188. nsindex = to_namespace_index(ndd, ndd->ns_current);
  1189. if (nsindex == NULL)
  1190. loop_bitmask |= 1;
  1191. else {
  1192. /* check whether existing labels are v1.1 or v1.2 */
  1193. if (__le16_to_cpu(nsindex->major) == 1
  1194. && __le16_to_cpu(nsindex->minor) == 1)
  1195. loop_bitmask |= 2;
  1196. else
  1197. loop_bitmask |= 4;
  1198. }
  1199. }
  1200. /*
  1201. * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
  1202. * block is found, a v1.1 label for any mapping will set bit 1, and a
  1203. * v1.2 label will set bit 2.
  1204. *
  1205. * At the end of the loop, at most one of the three bits must be set.
  1206. * If multiple bits were set, it means the different mappings disagree
  1207. * about their labels, and this must be cleaned up first.
  1208. *
  1209. * If all the label index blocks are found to agree, nsindex of NULL
  1210. * implies labels haven't been initialized yet, and when they will,
  1211. * they will be of the 1.2 format, so we can assume BTT2.0
  1212. *
  1213. * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
  1214. * found, we enforce BTT2.0
  1215. *
  1216. * If the loop was never entered, default to BTT1.1 (legacy namespaces)
  1217. */
  1218. switch (loop_bitmask) {
  1219. case 0:
  1220. case 2:
  1221. return NVDIMM_CCLASS_BTT;
  1222. case 1:
  1223. case 4:
  1224. return NVDIMM_CCLASS_BTT2;
  1225. default:
  1226. return -ENXIO;
  1227. }
  1228. }
  1229. static ssize_t holder_show(struct device *dev,
  1230. struct device_attribute *attr, char *buf)
  1231. {
  1232. struct nd_namespace_common *ndns = to_ndns(dev);
  1233. ssize_t rc;
  1234. device_lock(dev);
  1235. rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
  1236. device_unlock(dev);
  1237. return rc;
  1238. }
  1239. static DEVICE_ATTR_RO(holder);
  1240. static ssize_t __holder_class_store(struct device *dev, const char *buf)
  1241. {
  1242. struct nd_namespace_common *ndns = to_ndns(dev);
  1243. if (dev->driver || ndns->claim)
  1244. return -EBUSY;
  1245. if (strcmp(buf, "btt") == 0 || strcmp(buf, "btt\n") == 0)
  1246. ndns->claim_class = btt_claim_class(dev);
  1247. else if (strcmp(buf, "pfn") == 0 || strcmp(buf, "pfn\n") == 0)
  1248. ndns->claim_class = NVDIMM_CCLASS_PFN;
  1249. else if (strcmp(buf, "dax") == 0 || strcmp(buf, "dax\n") == 0)
  1250. ndns->claim_class = NVDIMM_CCLASS_DAX;
  1251. else if (strcmp(buf, "") == 0 || strcmp(buf, "\n") == 0)
  1252. ndns->claim_class = NVDIMM_CCLASS_NONE;
  1253. else
  1254. return -EINVAL;
  1255. /* btt_claim_class() could've returned an error */
  1256. if (ndns->claim_class < 0)
  1257. return ndns->claim_class;
  1258. return 0;
  1259. }
  1260. static ssize_t holder_class_store(struct device *dev,
  1261. struct device_attribute *attr, const char *buf, size_t len)
  1262. {
  1263. struct nd_region *nd_region = to_nd_region(dev->parent);
  1264. ssize_t rc;
  1265. device_lock(dev);
  1266. nvdimm_bus_lock(dev);
  1267. wait_nvdimm_bus_probe_idle(dev);
  1268. rc = __holder_class_store(dev, buf);
  1269. if (rc >= 0)
  1270. rc = nd_namespace_label_update(nd_region, dev);
  1271. dev_dbg(dev, "%s(%zd)\n", rc < 0 ? "fail " : "", rc);
  1272. nvdimm_bus_unlock(dev);
  1273. device_unlock(dev);
  1274. return rc < 0 ? rc : len;
  1275. }
  1276. static ssize_t holder_class_show(struct device *dev,
  1277. struct device_attribute *attr, char *buf)
  1278. {
  1279. struct nd_namespace_common *ndns = to_ndns(dev);
  1280. ssize_t rc;
  1281. device_lock(dev);
  1282. if (ndns->claim_class == NVDIMM_CCLASS_NONE)
  1283. rc = sprintf(buf, "\n");
  1284. else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
  1285. (ndns->claim_class == NVDIMM_CCLASS_BTT2))
  1286. rc = sprintf(buf, "btt\n");
  1287. else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
  1288. rc = sprintf(buf, "pfn\n");
  1289. else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
  1290. rc = sprintf(buf, "dax\n");
  1291. else
  1292. rc = sprintf(buf, "<unknown>\n");
  1293. device_unlock(dev);
  1294. return rc;
  1295. }
  1296. static DEVICE_ATTR_RW(holder_class);
  1297. static ssize_t mode_show(struct device *dev,
  1298. struct device_attribute *attr, char *buf)
  1299. {
  1300. struct nd_namespace_common *ndns = to_ndns(dev);
  1301. struct device *claim;
  1302. char *mode;
  1303. ssize_t rc;
  1304. device_lock(dev);
  1305. claim = ndns->claim;
  1306. if (claim && is_nd_btt(claim))
  1307. mode = "safe";
  1308. else if (claim && is_nd_pfn(claim))
  1309. mode = "memory";
  1310. else if (claim && is_nd_dax(claim))
  1311. mode = "dax";
  1312. else if (!claim && pmem_should_map_pages(dev))
  1313. mode = "memory";
  1314. else
  1315. mode = "raw";
  1316. rc = sprintf(buf, "%s\n", mode);
  1317. device_unlock(dev);
  1318. return rc;
  1319. }
  1320. static DEVICE_ATTR_RO(mode);
  1321. static ssize_t force_raw_store(struct device *dev,
  1322. struct device_attribute *attr, const char *buf, size_t len)
  1323. {
  1324. bool force_raw;
  1325. int rc = strtobool(buf, &force_raw);
  1326. if (rc)
  1327. return rc;
  1328. to_ndns(dev)->force_raw = force_raw;
  1329. return len;
  1330. }
  1331. static ssize_t force_raw_show(struct device *dev,
  1332. struct device_attribute *attr, char *buf)
  1333. {
  1334. return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
  1335. }
  1336. static DEVICE_ATTR_RW(force_raw);
  1337. static struct attribute *nd_namespace_attributes[] = {
  1338. &dev_attr_nstype.attr,
  1339. &dev_attr_size.attr,
  1340. &dev_attr_mode.attr,
  1341. &dev_attr_uuid.attr,
  1342. &dev_attr_holder.attr,
  1343. &dev_attr_resource.attr,
  1344. &dev_attr_alt_name.attr,
  1345. &dev_attr_force_raw.attr,
  1346. &dev_attr_sector_size.attr,
  1347. &dev_attr_dpa_extents.attr,
  1348. &dev_attr_holder_class.attr,
  1349. NULL,
  1350. };
  1351. static umode_t namespace_visible(struct kobject *kobj,
  1352. struct attribute *a, int n)
  1353. {
  1354. struct device *dev = container_of(kobj, struct device, kobj);
  1355. if (a == &dev_attr_resource.attr) {
  1356. if (is_namespace_blk(dev))
  1357. return 0;
  1358. return 0400;
  1359. }
  1360. if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
  1361. if (a == &dev_attr_size.attr)
  1362. return 0644;
  1363. return a->mode;
  1364. }
  1365. if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
  1366. || a == &dev_attr_holder.attr
  1367. || a == &dev_attr_holder_class.attr
  1368. || a == &dev_attr_force_raw.attr
  1369. || a == &dev_attr_mode.attr)
  1370. return a->mode;
  1371. return 0;
  1372. }
  1373. static struct attribute_group nd_namespace_attribute_group = {
  1374. .attrs = nd_namespace_attributes,
  1375. .is_visible = namespace_visible,
  1376. };
  1377. static const struct attribute_group *nd_namespace_attribute_groups[] = {
  1378. &nd_device_attribute_group,
  1379. &nd_namespace_attribute_group,
  1380. &nd_numa_attribute_group,
  1381. NULL,
  1382. };
  1383. struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
  1384. {
  1385. struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
  1386. struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
  1387. struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
  1388. struct nd_namespace_common *ndns = NULL;
  1389. resource_size_t size;
  1390. if (nd_btt || nd_pfn || nd_dax) {
  1391. if (nd_btt)
  1392. ndns = nd_btt->ndns;
  1393. else if (nd_pfn)
  1394. ndns = nd_pfn->ndns;
  1395. else if (nd_dax)
  1396. ndns = nd_dax->nd_pfn.ndns;
  1397. if (!ndns)
  1398. return ERR_PTR(-ENODEV);
  1399. /*
  1400. * Flush any in-progess probes / removals in the driver
  1401. * for the raw personality of this namespace.
  1402. */
  1403. device_lock(&ndns->dev);
  1404. device_unlock(&ndns->dev);
  1405. if (ndns->dev.driver) {
  1406. dev_dbg(&ndns->dev, "is active, can't bind %s\n",
  1407. dev_name(dev));
  1408. return ERR_PTR(-EBUSY);
  1409. }
  1410. if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
  1411. "host (%s) vs claim (%s) mismatch\n",
  1412. dev_name(dev),
  1413. dev_name(ndns->claim)))
  1414. return ERR_PTR(-ENXIO);
  1415. } else {
  1416. ndns = to_ndns(dev);
  1417. if (ndns->claim) {
  1418. dev_dbg(dev, "claimed by %s, failing probe\n",
  1419. dev_name(ndns->claim));
  1420. return ERR_PTR(-ENXIO);
  1421. }
  1422. }
  1423. size = nvdimm_namespace_capacity(ndns);
  1424. if (size < ND_MIN_NAMESPACE_SIZE) {
  1425. dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
  1426. &size, ND_MIN_NAMESPACE_SIZE);
  1427. return ERR_PTR(-ENODEV);
  1428. }
  1429. if (is_namespace_pmem(&ndns->dev)) {
  1430. struct nd_namespace_pmem *nspm;
  1431. nspm = to_nd_namespace_pmem(&ndns->dev);
  1432. if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
  1433. return ERR_PTR(-ENODEV);
  1434. } else if (is_namespace_blk(&ndns->dev)) {
  1435. struct nd_namespace_blk *nsblk;
  1436. nsblk = to_nd_namespace_blk(&ndns->dev);
  1437. if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
  1438. return ERR_PTR(-ENODEV);
  1439. if (!nsblk->lbasize) {
  1440. dev_dbg(&ndns->dev, "sector size not set\n");
  1441. return ERR_PTR(-ENODEV);
  1442. }
  1443. if (!nd_namespace_blk_validate(nsblk))
  1444. return ERR_PTR(-ENODEV);
  1445. }
  1446. return ndns;
  1447. }
  1448. EXPORT_SYMBOL(nvdimm_namespace_common_probe);
  1449. static struct device **create_namespace_io(struct nd_region *nd_region)
  1450. {
  1451. struct nd_namespace_io *nsio;
  1452. struct device *dev, **devs;
  1453. struct resource *res;
  1454. nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
  1455. if (!nsio)
  1456. return NULL;
  1457. devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
  1458. if (!devs) {
  1459. kfree(nsio);
  1460. return NULL;
  1461. }
  1462. dev = &nsio->common.dev;
  1463. dev->type = &namespace_io_device_type;
  1464. dev->parent = &nd_region->dev;
  1465. res = &nsio->res;
  1466. res->name = dev_name(&nd_region->dev);
  1467. res->flags = IORESOURCE_MEM;
  1468. res->start = nd_region->ndr_start;
  1469. res->end = res->start + nd_region->ndr_size - 1;
  1470. devs[0] = dev;
  1471. return devs;
  1472. }
  1473. static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
  1474. u64 cookie, u16 pos)
  1475. {
  1476. struct nd_namespace_label *found = NULL;
  1477. int i;
  1478. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1479. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1480. struct nd_interleave_set *nd_set = nd_region->nd_set;
  1481. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1482. struct nd_label_ent *label_ent;
  1483. bool found_uuid = false;
  1484. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  1485. struct nd_namespace_label *nd_label = label_ent->label;
  1486. u16 position, nlabel;
  1487. u64 isetcookie;
  1488. if (!nd_label)
  1489. continue;
  1490. isetcookie = __le64_to_cpu(nd_label->isetcookie);
  1491. position = __le16_to_cpu(nd_label->position);
  1492. nlabel = __le16_to_cpu(nd_label->nlabel);
  1493. if (isetcookie != cookie)
  1494. continue;
  1495. if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
  1496. continue;
  1497. if (namespace_label_has(ndd, type_guid)
  1498. && !guid_equal(&nd_set->type_guid,
  1499. &nd_label->type_guid)) {
  1500. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
  1501. nd_set->type_guid.b,
  1502. nd_label->type_guid.b);
  1503. continue;
  1504. }
  1505. if (found_uuid) {
  1506. dev_dbg(ndd->dev, "duplicate entry for uuid\n");
  1507. return false;
  1508. }
  1509. found_uuid = true;
  1510. if (nlabel != nd_region->ndr_mappings)
  1511. continue;
  1512. if (position != pos)
  1513. continue;
  1514. found = nd_label;
  1515. break;
  1516. }
  1517. if (found)
  1518. break;
  1519. }
  1520. return found != NULL;
  1521. }
  1522. static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
  1523. {
  1524. int i;
  1525. if (!pmem_id)
  1526. return -ENODEV;
  1527. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1528. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1529. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1530. struct nd_namespace_label *nd_label = NULL;
  1531. u64 hw_start, hw_end, pmem_start, pmem_end;
  1532. struct nd_label_ent *label_ent;
  1533. lockdep_assert_held(&nd_mapping->lock);
  1534. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  1535. nd_label = label_ent->label;
  1536. if (!nd_label)
  1537. continue;
  1538. if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
  1539. break;
  1540. nd_label = NULL;
  1541. }
  1542. if (!nd_label) {
  1543. WARN_ON(1);
  1544. return -EINVAL;
  1545. }
  1546. /*
  1547. * Check that this label is compliant with the dpa
  1548. * range published in NFIT
  1549. */
  1550. hw_start = nd_mapping->start;
  1551. hw_end = hw_start + nd_mapping->size;
  1552. pmem_start = __le64_to_cpu(nd_label->dpa);
  1553. pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
  1554. if (pmem_start >= hw_start && pmem_start < hw_end
  1555. && pmem_end <= hw_end && pmem_end > hw_start)
  1556. /* pass */;
  1557. else {
  1558. dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
  1559. dev_name(ndd->dev), nd_label->uuid);
  1560. return -EINVAL;
  1561. }
  1562. /* move recently validated label to the front of the list */
  1563. list_move(&label_ent->list, &nd_mapping->labels);
  1564. }
  1565. return 0;
  1566. }
  1567. /**
  1568. * create_namespace_pmem - validate interleave set labelling, retrieve label0
  1569. * @nd_region: region with mappings to validate
  1570. * @nspm: target namespace to create
  1571. * @nd_label: target pmem namespace label to evaluate
  1572. */
  1573. static struct device *create_namespace_pmem(struct nd_region *nd_region,
  1574. struct nd_namespace_index *nsindex,
  1575. struct nd_namespace_label *nd_label)
  1576. {
  1577. u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
  1578. u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
  1579. struct nd_label_ent *label_ent;
  1580. struct nd_namespace_pmem *nspm;
  1581. struct nd_mapping *nd_mapping;
  1582. resource_size_t size = 0;
  1583. struct resource *res;
  1584. struct device *dev;
  1585. int rc = 0;
  1586. u16 i;
  1587. if (cookie == 0) {
  1588. dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
  1589. return ERR_PTR(-ENXIO);
  1590. }
  1591. if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
  1592. dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
  1593. nd_label->uuid);
  1594. if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
  1595. return ERR_PTR(-EAGAIN);
  1596. dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
  1597. nd_label->uuid);
  1598. }
  1599. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1600. if (!nspm)
  1601. return ERR_PTR(-ENOMEM);
  1602. nspm->id = -1;
  1603. dev = &nspm->nsio.common.dev;
  1604. dev->type = &namespace_pmem_device_type;
  1605. dev->parent = &nd_region->dev;
  1606. res = &nspm->nsio.res;
  1607. res->name = dev_name(&nd_region->dev);
  1608. res->flags = IORESOURCE_MEM;
  1609. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1610. if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
  1611. continue;
  1612. if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
  1613. continue;
  1614. break;
  1615. }
  1616. if (i < nd_region->ndr_mappings) {
  1617. struct nvdimm *nvdimm = nd_region->mapping[i].nvdimm;
  1618. /*
  1619. * Give up if we don't find an instance of a uuid at each
  1620. * position (from 0 to nd_region->ndr_mappings - 1), or if we
  1621. * find a dimm with two instances of the same uuid.
  1622. */
  1623. dev_err(&nd_region->dev, "%s missing label for %pUb\n",
  1624. nvdimm_name(nvdimm), nd_label->uuid);
  1625. rc = -EINVAL;
  1626. goto err;
  1627. }
  1628. /*
  1629. * Fix up each mapping's 'labels' to have the validated pmem label for
  1630. * that position at labels[0], and NULL at labels[1]. In the process,
  1631. * check that the namespace aligns with interleave-set. We know
  1632. * that it does not overlap with any blk namespaces by virtue of
  1633. * the dimm being enabled (i.e. nd_label_reserve_dpa()
  1634. * succeeded).
  1635. */
  1636. rc = select_pmem_id(nd_region, nd_label->uuid);
  1637. if (rc)
  1638. goto err;
  1639. /* Calculate total size and populate namespace properties from label0 */
  1640. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1641. struct nd_namespace_label *label0;
  1642. struct nvdimm_drvdata *ndd;
  1643. nd_mapping = &nd_region->mapping[i];
  1644. label_ent = list_first_entry_or_null(&nd_mapping->labels,
  1645. typeof(*label_ent), list);
  1646. label0 = label_ent ? label_ent->label : 0;
  1647. if (!label0) {
  1648. WARN_ON(1);
  1649. continue;
  1650. }
  1651. size += __le64_to_cpu(label0->rawsize);
  1652. if (__le16_to_cpu(label0->position) != 0)
  1653. continue;
  1654. WARN_ON(nspm->alt_name || nspm->uuid);
  1655. nspm->alt_name = kmemdup((void __force *) label0->name,
  1656. NSLABEL_NAME_LEN, GFP_KERNEL);
  1657. nspm->uuid = kmemdup((void __force *) label0->uuid,
  1658. NSLABEL_UUID_LEN, GFP_KERNEL);
  1659. nspm->lbasize = __le64_to_cpu(label0->lbasize);
  1660. ndd = to_ndd(nd_mapping);
  1661. if (namespace_label_has(ndd, abstraction_guid))
  1662. nspm->nsio.common.claim_class
  1663. = to_nvdimm_cclass(&label0->abstraction_guid);
  1664. }
  1665. if (!nspm->alt_name || !nspm->uuid) {
  1666. rc = -ENOMEM;
  1667. goto err;
  1668. }
  1669. nd_namespace_pmem_set_resource(nd_region, nspm, size);
  1670. return dev;
  1671. err:
  1672. namespace_pmem_release(dev);
  1673. switch (rc) {
  1674. case -EINVAL:
  1675. dev_dbg(&nd_region->dev, "invalid label(s)\n");
  1676. break;
  1677. case -ENODEV:
  1678. dev_dbg(&nd_region->dev, "label not found\n");
  1679. break;
  1680. default:
  1681. dev_dbg(&nd_region->dev, "unexpected err: %d\n", rc);
  1682. break;
  1683. }
  1684. return ERR_PTR(rc);
  1685. }
  1686. struct resource *nsblk_add_resource(struct nd_region *nd_region,
  1687. struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
  1688. resource_size_t start)
  1689. {
  1690. struct nd_label_id label_id;
  1691. struct resource *res;
  1692. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  1693. res = krealloc(nsblk->res,
  1694. sizeof(void *) * (nsblk->num_resources + 1),
  1695. GFP_KERNEL);
  1696. if (!res)
  1697. return NULL;
  1698. nsblk->res = (struct resource **) res;
  1699. for_each_dpa_resource(ndd, res)
  1700. if (strcmp(res->name, label_id.id) == 0
  1701. && res->start == start) {
  1702. nsblk->res[nsblk->num_resources++] = res;
  1703. return res;
  1704. }
  1705. return NULL;
  1706. }
  1707. static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
  1708. {
  1709. struct nd_namespace_blk *nsblk;
  1710. struct device *dev;
  1711. if (!is_nd_blk(&nd_region->dev))
  1712. return NULL;
  1713. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1714. if (!nsblk)
  1715. return NULL;
  1716. dev = &nsblk->common.dev;
  1717. dev->type = &namespace_blk_device_type;
  1718. nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
  1719. if (nsblk->id < 0) {
  1720. kfree(nsblk);
  1721. return NULL;
  1722. }
  1723. dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
  1724. dev->parent = &nd_region->dev;
  1725. dev->groups = nd_namespace_attribute_groups;
  1726. return &nsblk->common.dev;
  1727. }
  1728. static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
  1729. {
  1730. struct nd_namespace_pmem *nspm;
  1731. struct resource *res;
  1732. struct device *dev;
  1733. if (!is_memory(&nd_region->dev))
  1734. return NULL;
  1735. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1736. if (!nspm)
  1737. return NULL;
  1738. dev = &nspm->nsio.common.dev;
  1739. dev->type = &namespace_pmem_device_type;
  1740. dev->parent = &nd_region->dev;
  1741. res = &nspm->nsio.res;
  1742. res->name = dev_name(&nd_region->dev);
  1743. res->flags = IORESOURCE_MEM;
  1744. nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
  1745. if (nspm->id < 0) {
  1746. kfree(nspm);
  1747. return NULL;
  1748. }
  1749. dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
  1750. dev->parent = &nd_region->dev;
  1751. dev->groups = nd_namespace_attribute_groups;
  1752. nd_namespace_pmem_set_resource(nd_region, nspm, 0);
  1753. return dev;
  1754. }
  1755. void nd_region_create_ns_seed(struct nd_region *nd_region)
  1756. {
  1757. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1758. if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
  1759. return;
  1760. if (is_nd_blk(&nd_region->dev))
  1761. nd_region->ns_seed = nd_namespace_blk_create(nd_region);
  1762. else
  1763. nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
  1764. /*
  1765. * Seed creation failures are not fatal, provisioning is simply
  1766. * disabled until memory becomes available
  1767. */
  1768. if (!nd_region->ns_seed)
  1769. dev_err(&nd_region->dev, "failed to create %s namespace\n",
  1770. is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
  1771. else
  1772. nd_device_register(nd_region->ns_seed);
  1773. }
  1774. void nd_region_create_dax_seed(struct nd_region *nd_region)
  1775. {
  1776. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1777. nd_region->dax_seed = nd_dax_create(nd_region);
  1778. /*
  1779. * Seed creation failures are not fatal, provisioning is simply
  1780. * disabled until memory becomes available
  1781. */
  1782. if (!nd_region->dax_seed)
  1783. dev_err(&nd_region->dev, "failed to create dax namespace\n");
  1784. }
  1785. void nd_region_create_pfn_seed(struct nd_region *nd_region)
  1786. {
  1787. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1788. nd_region->pfn_seed = nd_pfn_create(nd_region);
  1789. /*
  1790. * Seed creation failures are not fatal, provisioning is simply
  1791. * disabled until memory becomes available
  1792. */
  1793. if (!nd_region->pfn_seed)
  1794. dev_err(&nd_region->dev, "failed to create pfn namespace\n");
  1795. }
  1796. void nd_region_create_btt_seed(struct nd_region *nd_region)
  1797. {
  1798. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1799. nd_region->btt_seed = nd_btt_create(nd_region);
  1800. /*
  1801. * Seed creation failures are not fatal, provisioning is simply
  1802. * disabled until memory becomes available
  1803. */
  1804. if (!nd_region->btt_seed)
  1805. dev_err(&nd_region->dev, "failed to create btt namespace\n");
  1806. }
  1807. static int add_namespace_resource(struct nd_region *nd_region,
  1808. struct nd_namespace_label *nd_label, struct device **devs,
  1809. int count)
  1810. {
  1811. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1812. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1813. int i;
  1814. for (i = 0; i < count; i++) {
  1815. u8 *uuid = namespace_to_uuid(devs[i]);
  1816. struct resource *res;
  1817. if (IS_ERR_OR_NULL(uuid)) {
  1818. WARN_ON(1);
  1819. continue;
  1820. }
  1821. if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
  1822. continue;
  1823. if (is_namespace_blk(devs[i])) {
  1824. res = nsblk_add_resource(nd_region, ndd,
  1825. to_nd_namespace_blk(devs[i]),
  1826. __le64_to_cpu(nd_label->dpa));
  1827. if (!res)
  1828. return -ENXIO;
  1829. nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
  1830. } else {
  1831. dev_err(&nd_region->dev,
  1832. "error: conflicting extents for uuid: %pUb\n",
  1833. nd_label->uuid);
  1834. return -ENXIO;
  1835. }
  1836. break;
  1837. }
  1838. return i;
  1839. }
  1840. static struct device *create_namespace_blk(struct nd_region *nd_region,
  1841. struct nd_namespace_label *nd_label, int count)
  1842. {
  1843. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1844. struct nd_interleave_set *nd_set = nd_region->nd_set;
  1845. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1846. struct nd_namespace_blk *nsblk;
  1847. char name[NSLABEL_NAME_LEN];
  1848. struct device *dev = NULL;
  1849. struct resource *res;
  1850. if (namespace_label_has(ndd, type_guid)) {
  1851. if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
  1852. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
  1853. nd_set->type_guid.b,
  1854. nd_label->type_guid.b);
  1855. return ERR_PTR(-EAGAIN);
  1856. }
  1857. if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
  1858. dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
  1859. nd_set->cookie2,
  1860. __le64_to_cpu(nd_label->isetcookie));
  1861. return ERR_PTR(-EAGAIN);
  1862. }
  1863. }
  1864. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1865. if (!nsblk)
  1866. return ERR_PTR(-ENOMEM);
  1867. dev = &nsblk->common.dev;
  1868. dev->type = &namespace_blk_device_type;
  1869. dev->parent = &nd_region->dev;
  1870. nsblk->id = -1;
  1871. nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
  1872. nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
  1873. GFP_KERNEL);
  1874. if (namespace_label_has(ndd, abstraction_guid))
  1875. nsblk->common.claim_class
  1876. = to_nvdimm_cclass(&nd_label->abstraction_guid);
  1877. if (!nsblk->uuid)
  1878. goto blk_err;
  1879. memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
  1880. if (name[0])
  1881. nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
  1882. GFP_KERNEL);
  1883. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1884. __le64_to_cpu(nd_label->dpa));
  1885. if (!res)
  1886. goto blk_err;
  1887. nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
  1888. return dev;
  1889. blk_err:
  1890. namespace_blk_release(dev);
  1891. return ERR_PTR(-ENXIO);
  1892. }
  1893. static int cmp_dpa(const void *a, const void *b)
  1894. {
  1895. const struct device *dev_a = *(const struct device **) a;
  1896. const struct device *dev_b = *(const struct device **) b;
  1897. struct nd_namespace_blk *nsblk_a, *nsblk_b;
  1898. struct nd_namespace_pmem *nspm_a, *nspm_b;
  1899. if (is_namespace_io(dev_a))
  1900. return 0;
  1901. if (is_namespace_blk(dev_a)) {
  1902. nsblk_a = to_nd_namespace_blk(dev_a);
  1903. nsblk_b = to_nd_namespace_blk(dev_b);
  1904. return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
  1905. sizeof(resource_size_t));
  1906. }
  1907. nspm_a = to_nd_namespace_pmem(dev_a);
  1908. nspm_b = to_nd_namespace_pmem(dev_b);
  1909. return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
  1910. sizeof(resource_size_t));
  1911. }
  1912. static struct device **scan_labels(struct nd_region *nd_region)
  1913. {
  1914. int i, count = 0;
  1915. struct device *dev, **devs = NULL;
  1916. struct nd_label_ent *label_ent, *e;
  1917. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1918. resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
  1919. /* "safe" because create_namespace_pmem() might list_move() label_ent */
  1920. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  1921. struct nd_namespace_label *nd_label = label_ent->label;
  1922. struct device **__devs;
  1923. u32 flags;
  1924. if (!nd_label)
  1925. continue;
  1926. flags = __le32_to_cpu(nd_label->flags);
  1927. if (is_nd_blk(&nd_region->dev)
  1928. == !!(flags & NSLABEL_FLAG_LOCAL))
  1929. /* pass, region matches label type */;
  1930. else
  1931. continue;
  1932. /* skip labels that describe extents outside of the region */
  1933. if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
  1934. continue;
  1935. i = add_namespace_resource(nd_region, nd_label, devs, count);
  1936. if (i < 0)
  1937. goto err;
  1938. if (i < count)
  1939. continue;
  1940. __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
  1941. if (!__devs)
  1942. goto err;
  1943. memcpy(__devs, devs, sizeof(dev) * count);
  1944. kfree(devs);
  1945. devs = __devs;
  1946. if (is_nd_blk(&nd_region->dev))
  1947. dev = create_namespace_blk(nd_region, nd_label, count);
  1948. else {
  1949. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1950. struct nd_namespace_index *nsindex;
  1951. nsindex = to_namespace_index(ndd, ndd->ns_current);
  1952. dev = create_namespace_pmem(nd_region, nsindex, nd_label);
  1953. }
  1954. if (IS_ERR(dev)) {
  1955. switch (PTR_ERR(dev)) {
  1956. case -EAGAIN:
  1957. /* skip invalid labels */
  1958. continue;
  1959. case -ENODEV:
  1960. /* fallthrough to seed creation */
  1961. break;
  1962. default:
  1963. goto err;
  1964. }
  1965. } else
  1966. devs[count++] = dev;
  1967. }
  1968. dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
  1969. count, is_nd_blk(&nd_region->dev)
  1970. ? "blk" : "pmem", count == 1 ? "" : "s");
  1971. if (count == 0) {
  1972. /* Publish a zero-sized namespace for userspace to configure. */
  1973. nd_mapping_free_labels(nd_mapping);
  1974. devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
  1975. if (!devs)
  1976. goto err;
  1977. if (is_nd_blk(&nd_region->dev)) {
  1978. struct nd_namespace_blk *nsblk;
  1979. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1980. if (!nsblk)
  1981. goto err;
  1982. dev = &nsblk->common.dev;
  1983. dev->type = &namespace_blk_device_type;
  1984. } else {
  1985. struct nd_namespace_pmem *nspm;
  1986. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1987. if (!nspm)
  1988. goto err;
  1989. dev = &nspm->nsio.common.dev;
  1990. dev->type = &namespace_pmem_device_type;
  1991. nd_namespace_pmem_set_resource(nd_region, nspm, 0);
  1992. }
  1993. dev->parent = &nd_region->dev;
  1994. devs[count++] = dev;
  1995. } else if (is_memory(&nd_region->dev)) {
  1996. /* clean unselected labels */
  1997. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1998. struct list_head *l, *e;
  1999. LIST_HEAD(list);
  2000. int j;
  2001. nd_mapping = &nd_region->mapping[i];
  2002. if (list_empty(&nd_mapping->labels)) {
  2003. WARN_ON(1);
  2004. continue;
  2005. }
  2006. j = count;
  2007. list_for_each_safe(l, e, &nd_mapping->labels) {
  2008. if (!j--)
  2009. break;
  2010. list_move_tail(l, &list);
  2011. }
  2012. nd_mapping_free_labels(nd_mapping);
  2013. list_splice_init(&list, &nd_mapping->labels);
  2014. }
  2015. }
  2016. if (count > 1)
  2017. sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
  2018. return devs;
  2019. err:
  2020. if (devs) {
  2021. for (i = 0; devs[i]; i++)
  2022. if (is_nd_blk(&nd_region->dev))
  2023. namespace_blk_release(devs[i]);
  2024. else
  2025. namespace_pmem_release(devs[i]);
  2026. kfree(devs);
  2027. }
  2028. return NULL;
  2029. }
  2030. static struct device **create_namespaces(struct nd_region *nd_region)
  2031. {
  2032. struct nd_mapping *nd_mapping;
  2033. struct device **devs;
  2034. int i;
  2035. if (nd_region->ndr_mappings == 0)
  2036. return NULL;
  2037. /* lock down all mappings while we scan labels */
  2038. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2039. nd_mapping = &nd_region->mapping[i];
  2040. mutex_lock_nested(&nd_mapping->lock, i);
  2041. }
  2042. devs = scan_labels(nd_region);
  2043. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2044. int reverse = nd_region->ndr_mappings - 1 - i;
  2045. nd_mapping = &nd_region->mapping[reverse];
  2046. mutex_unlock(&nd_mapping->lock);
  2047. }
  2048. return devs;
  2049. }
  2050. static int init_active_labels(struct nd_region *nd_region)
  2051. {
  2052. int i;
  2053. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2054. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  2055. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  2056. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  2057. struct nd_label_ent *label_ent;
  2058. int count, j;
  2059. /*
  2060. * If the dimm is disabled then we may need to prevent
  2061. * the region from being activated.
  2062. */
  2063. if (!ndd) {
  2064. if (test_bit(NDD_LOCKED, &nvdimm->flags))
  2065. /* fail, label data may be unreadable */;
  2066. else if (test_bit(NDD_ALIASING, &nvdimm->flags))
  2067. /* fail, labels needed to disambiguate dpa */;
  2068. else
  2069. return 0;
  2070. dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
  2071. dev_name(&nd_mapping->nvdimm->dev),
  2072. test_bit(NDD_LOCKED, &nvdimm->flags)
  2073. ? "locked" : "disabled");
  2074. return -ENXIO;
  2075. }
  2076. nd_mapping->ndd = ndd;
  2077. atomic_inc(&nvdimm->busy);
  2078. get_ndd(ndd);
  2079. count = nd_label_active_count(ndd);
  2080. dev_dbg(ndd->dev, "count: %d\n", count);
  2081. if (!count)
  2082. continue;
  2083. for (j = 0; j < count; j++) {
  2084. struct nd_namespace_label *label;
  2085. label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
  2086. if (!label_ent)
  2087. break;
  2088. label = nd_label_active(ndd, j);
  2089. label_ent->label = label;
  2090. mutex_lock(&nd_mapping->lock);
  2091. list_add_tail(&label_ent->list, &nd_mapping->labels);
  2092. mutex_unlock(&nd_mapping->lock);
  2093. }
  2094. if (j >= count)
  2095. continue;
  2096. mutex_lock(&nd_mapping->lock);
  2097. nd_mapping_free_labels(nd_mapping);
  2098. mutex_unlock(&nd_mapping->lock);
  2099. return -ENOMEM;
  2100. }
  2101. return 0;
  2102. }
  2103. int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
  2104. {
  2105. struct device **devs = NULL;
  2106. int i, rc = 0, type;
  2107. *err = 0;
  2108. nvdimm_bus_lock(&nd_region->dev);
  2109. rc = init_active_labels(nd_region);
  2110. if (rc) {
  2111. nvdimm_bus_unlock(&nd_region->dev);
  2112. return rc;
  2113. }
  2114. type = nd_region_to_nstype(nd_region);
  2115. switch (type) {
  2116. case ND_DEVICE_NAMESPACE_IO:
  2117. devs = create_namespace_io(nd_region);
  2118. break;
  2119. case ND_DEVICE_NAMESPACE_PMEM:
  2120. case ND_DEVICE_NAMESPACE_BLK:
  2121. devs = create_namespaces(nd_region);
  2122. break;
  2123. default:
  2124. break;
  2125. }
  2126. nvdimm_bus_unlock(&nd_region->dev);
  2127. if (!devs)
  2128. return -ENODEV;
  2129. for (i = 0; devs[i]; i++) {
  2130. struct device *dev = devs[i];
  2131. int id;
  2132. if (type == ND_DEVICE_NAMESPACE_BLK) {
  2133. struct nd_namespace_blk *nsblk;
  2134. nsblk = to_nd_namespace_blk(dev);
  2135. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  2136. GFP_KERNEL);
  2137. nsblk->id = id;
  2138. } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
  2139. struct nd_namespace_pmem *nspm;
  2140. nspm = to_nd_namespace_pmem(dev);
  2141. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  2142. GFP_KERNEL);
  2143. nspm->id = id;
  2144. } else
  2145. id = i;
  2146. if (id < 0)
  2147. break;
  2148. dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
  2149. dev->groups = nd_namespace_attribute_groups;
  2150. nd_device_register(dev);
  2151. }
  2152. if (i)
  2153. nd_region->ns_seed = devs[0];
  2154. if (devs[i]) {
  2155. int j;
  2156. for (j = i; devs[j]; j++) {
  2157. struct device *dev = devs[j];
  2158. device_initialize(dev);
  2159. put_device(dev);
  2160. }
  2161. *err = j - i;
  2162. /*
  2163. * All of the namespaces we tried to register failed, so
  2164. * fail region activation.
  2165. */
  2166. if (*err == 0)
  2167. rc = -ENODEV;
  2168. }
  2169. kfree(devs);
  2170. if (rc == -ENODEV)
  2171. return rc;
  2172. return i;
  2173. }