namespace_devs.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582
  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: %s(%zd)\n", __func__, 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, "%s: not associated with dimm(s)\n", __func__);
  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, "%s: %llx %s (%d)\n", __func__, val, rc < 0
  927. ? "fail" : "success", rc);
  928. nvdimm_bus_unlock(dev);
  929. device_unlock(dev);
  930. return rc < 0 ? rc : len;
  931. }
  932. resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  933. {
  934. struct device *dev = &ndns->dev;
  935. if (is_namespace_pmem(dev)) {
  936. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  937. return resource_size(&nspm->nsio.res);
  938. } else if (is_namespace_blk(dev)) {
  939. return nd_namespace_blk_size(to_nd_namespace_blk(dev));
  940. } else if (is_namespace_io(dev)) {
  941. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  942. return resource_size(&nsio->res);
  943. } else
  944. WARN_ONCE(1, "unknown namespace type\n");
  945. return 0;
  946. }
  947. resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  948. {
  949. resource_size_t size;
  950. nvdimm_bus_lock(&ndns->dev);
  951. size = __nvdimm_namespace_capacity(ndns);
  952. nvdimm_bus_unlock(&ndns->dev);
  953. return size;
  954. }
  955. EXPORT_SYMBOL(nvdimm_namespace_capacity);
  956. static ssize_t size_show(struct device *dev,
  957. struct device_attribute *attr, char *buf)
  958. {
  959. return sprintf(buf, "%llu\n", (unsigned long long)
  960. nvdimm_namespace_capacity(to_ndns(dev)));
  961. }
  962. static DEVICE_ATTR(size, 0444, size_show, size_store);
  963. static u8 *namespace_to_uuid(struct device *dev)
  964. {
  965. if (is_namespace_pmem(dev)) {
  966. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  967. return nspm->uuid;
  968. } else if (is_namespace_blk(dev)) {
  969. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  970. return nsblk->uuid;
  971. } else
  972. return ERR_PTR(-ENXIO);
  973. }
  974. static ssize_t uuid_show(struct device *dev,
  975. struct device_attribute *attr, char *buf)
  976. {
  977. u8 *uuid = namespace_to_uuid(dev);
  978. if (IS_ERR(uuid))
  979. return PTR_ERR(uuid);
  980. if (uuid)
  981. return sprintf(buf, "%pUb\n", uuid);
  982. return sprintf(buf, "\n");
  983. }
  984. /**
  985. * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
  986. * @nd_region: parent region so we can updates all dimms in the set
  987. * @dev: namespace type for generating label_id
  988. * @new_uuid: incoming uuid
  989. * @old_uuid: reference to the uuid storage location in the namespace object
  990. */
  991. static int namespace_update_uuid(struct nd_region *nd_region,
  992. struct device *dev, u8 *new_uuid, u8 **old_uuid)
  993. {
  994. u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
  995. struct nd_label_id old_label_id;
  996. struct nd_label_id new_label_id;
  997. int i;
  998. if (!nd_is_uuid_unique(dev, new_uuid))
  999. return -EINVAL;
  1000. if (*old_uuid == NULL)
  1001. goto out;
  1002. /*
  1003. * If we've already written a label with this uuid, then it's
  1004. * too late to rename because we can't reliably update the uuid
  1005. * without losing the old namespace. Userspace must delete this
  1006. * namespace to abandon the old uuid.
  1007. */
  1008. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1009. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1010. /*
  1011. * This check by itself is sufficient because old_uuid
  1012. * would be NULL above if this uuid did not exist in the
  1013. * currently written set.
  1014. *
  1015. * FIXME: can we delete uuid with zero dpa allocated?
  1016. */
  1017. if (list_empty(&nd_mapping->labels))
  1018. return -EBUSY;
  1019. }
  1020. nd_label_gen_id(&old_label_id, *old_uuid, flags);
  1021. nd_label_gen_id(&new_label_id, new_uuid, flags);
  1022. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1023. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1024. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1025. struct resource *res;
  1026. for_each_dpa_resource(ndd, res)
  1027. if (strcmp(res->name, old_label_id.id) == 0)
  1028. sprintf((void *) res->name, "%s",
  1029. new_label_id.id);
  1030. }
  1031. kfree(*old_uuid);
  1032. out:
  1033. *old_uuid = new_uuid;
  1034. return 0;
  1035. }
  1036. static ssize_t uuid_store(struct device *dev,
  1037. struct device_attribute *attr, const char *buf, size_t len)
  1038. {
  1039. struct nd_region *nd_region = to_nd_region(dev->parent);
  1040. u8 *uuid = NULL;
  1041. ssize_t rc = 0;
  1042. u8 **ns_uuid;
  1043. if (is_namespace_pmem(dev)) {
  1044. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1045. ns_uuid = &nspm->uuid;
  1046. } else if (is_namespace_blk(dev)) {
  1047. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1048. ns_uuid = &nsblk->uuid;
  1049. } else
  1050. return -ENXIO;
  1051. device_lock(dev);
  1052. nvdimm_bus_lock(dev);
  1053. wait_nvdimm_bus_probe_idle(dev);
  1054. if (to_ndns(dev)->claim)
  1055. rc = -EBUSY;
  1056. if (rc >= 0)
  1057. rc = nd_uuid_store(dev, &uuid, buf, len);
  1058. if (rc >= 0)
  1059. rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
  1060. if (rc >= 0)
  1061. rc = nd_namespace_label_update(nd_region, dev);
  1062. else
  1063. kfree(uuid);
  1064. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  1065. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  1066. nvdimm_bus_unlock(dev);
  1067. device_unlock(dev);
  1068. return rc < 0 ? rc : len;
  1069. }
  1070. static DEVICE_ATTR_RW(uuid);
  1071. static ssize_t resource_show(struct device *dev,
  1072. struct device_attribute *attr, char *buf)
  1073. {
  1074. struct resource *res;
  1075. if (is_namespace_pmem(dev)) {
  1076. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1077. res = &nspm->nsio.res;
  1078. } else if (is_namespace_io(dev)) {
  1079. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  1080. res = &nsio->res;
  1081. } else
  1082. return -ENXIO;
  1083. /* no address to convey if the namespace has no allocation */
  1084. if (resource_size(res) == 0)
  1085. return -ENXIO;
  1086. return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
  1087. }
  1088. static DEVICE_ATTR_RO(resource);
  1089. static const unsigned long blk_lbasize_supported[] = { 512, 520, 528,
  1090. 4096, 4104, 4160, 4224, 0 };
  1091. static const unsigned long pmem_lbasize_supported[] = { 512, 4096, 0 };
  1092. static ssize_t sector_size_show(struct device *dev,
  1093. struct device_attribute *attr, char *buf)
  1094. {
  1095. if (is_namespace_blk(dev)) {
  1096. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1097. return nd_size_select_show(nsblk->lbasize,
  1098. blk_lbasize_supported, buf);
  1099. }
  1100. if (is_namespace_pmem(dev)) {
  1101. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1102. return nd_size_select_show(nspm->lbasize,
  1103. pmem_lbasize_supported, buf);
  1104. }
  1105. return -ENXIO;
  1106. }
  1107. static ssize_t sector_size_store(struct device *dev,
  1108. struct device_attribute *attr, const char *buf, size_t len)
  1109. {
  1110. struct nd_region *nd_region = to_nd_region(dev->parent);
  1111. const unsigned long *supported;
  1112. unsigned long *lbasize;
  1113. ssize_t rc = 0;
  1114. if (is_namespace_blk(dev)) {
  1115. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1116. lbasize = &nsblk->lbasize;
  1117. supported = blk_lbasize_supported;
  1118. } else if (is_namespace_pmem(dev)) {
  1119. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1120. lbasize = &nspm->lbasize;
  1121. supported = pmem_lbasize_supported;
  1122. } else
  1123. return -ENXIO;
  1124. device_lock(dev);
  1125. nvdimm_bus_lock(dev);
  1126. if (to_ndns(dev)->claim)
  1127. rc = -EBUSY;
  1128. if (rc >= 0)
  1129. rc = nd_size_select_store(dev, buf, lbasize, supported);
  1130. if (rc >= 0)
  1131. rc = nd_namespace_label_update(nd_region, dev);
  1132. dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
  1133. rc, rc < 0 ? "tried" : "wrote", buf,
  1134. buf[len - 1] == '\n' ? "" : "\n");
  1135. nvdimm_bus_unlock(dev);
  1136. device_unlock(dev);
  1137. return rc ? rc : len;
  1138. }
  1139. static DEVICE_ATTR_RW(sector_size);
  1140. static ssize_t dpa_extents_show(struct device *dev,
  1141. struct device_attribute *attr, char *buf)
  1142. {
  1143. struct nd_region *nd_region = to_nd_region(dev->parent);
  1144. struct nd_label_id label_id;
  1145. int count = 0, i;
  1146. u8 *uuid = NULL;
  1147. u32 flags = 0;
  1148. nvdimm_bus_lock(dev);
  1149. if (is_namespace_pmem(dev)) {
  1150. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  1151. uuid = nspm->uuid;
  1152. flags = 0;
  1153. } else if (is_namespace_blk(dev)) {
  1154. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  1155. uuid = nsblk->uuid;
  1156. flags = NSLABEL_FLAG_LOCAL;
  1157. }
  1158. if (!uuid)
  1159. goto out;
  1160. nd_label_gen_id(&label_id, uuid, flags);
  1161. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1162. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1163. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1164. struct resource *res;
  1165. for_each_dpa_resource(ndd, res)
  1166. if (strcmp(res->name, label_id.id) == 0)
  1167. count++;
  1168. }
  1169. out:
  1170. nvdimm_bus_unlock(dev);
  1171. return sprintf(buf, "%d\n", count);
  1172. }
  1173. static DEVICE_ATTR_RO(dpa_extents);
  1174. static int btt_claim_class(struct device *dev)
  1175. {
  1176. struct nd_region *nd_region = to_nd_region(dev->parent);
  1177. int i, loop_bitmask = 0;
  1178. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1179. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1180. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1181. struct nd_namespace_index *nsindex;
  1182. /*
  1183. * If any of the DIMMs do not support labels the only
  1184. * possible BTT format is v1.
  1185. */
  1186. if (!ndd) {
  1187. loop_bitmask = 0;
  1188. break;
  1189. }
  1190. nsindex = to_namespace_index(ndd, ndd->ns_current);
  1191. if (nsindex == NULL)
  1192. loop_bitmask |= 1;
  1193. else {
  1194. /* check whether existing labels are v1.1 or v1.2 */
  1195. if (__le16_to_cpu(nsindex->major) == 1
  1196. && __le16_to_cpu(nsindex->minor) == 1)
  1197. loop_bitmask |= 2;
  1198. else
  1199. loop_bitmask |= 4;
  1200. }
  1201. }
  1202. /*
  1203. * If nsindex is null loop_bitmask's bit 0 will be set, and if an index
  1204. * block is found, a v1.1 label for any mapping will set bit 1, and a
  1205. * v1.2 label will set bit 2.
  1206. *
  1207. * At the end of the loop, at most one of the three bits must be set.
  1208. * If multiple bits were set, it means the different mappings disagree
  1209. * about their labels, and this must be cleaned up first.
  1210. *
  1211. * If all the label index blocks are found to agree, nsindex of NULL
  1212. * implies labels haven't been initialized yet, and when they will,
  1213. * they will be of the 1.2 format, so we can assume BTT2.0
  1214. *
  1215. * If 1.1 labels are found, we enforce BTT1.1, and if 1.2 labels are
  1216. * found, we enforce BTT2.0
  1217. *
  1218. * If the loop was never entered, default to BTT1.1 (legacy namespaces)
  1219. */
  1220. switch (loop_bitmask) {
  1221. case 0:
  1222. case 2:
  1223. return NVDIMM_CCLASS_BTT;
  1224. case 1:
  1225. case 4:
  1226. return NVDIMM_CCLASS_BTT2;
  1227. default:
  1228. return -ENXIO;
  1229. }
  1230. }
  1231. static ssize_t holder_show(struct device *dev,
  1232. struct device_attribute *attr, char *buf)
  1233. {
  1234. struct nd_namespace_common *ndns = to_ndns(dev);
  1235. ssize_t rc;
  1236. device_lock(dev);
  1237. rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
  1238. device_unlock(dev);
  1239. return rc;
  1240. }
  1241. static DEVICE_ATTR_RO(holder);
  1242. static ssize_t __holder_class_store(struct device *dev, const char *buf)
  1243. {
  1244. struct nd_namespace_common *ndns = to_ndns(dev);
  1245. if (dev->driver || ndns->claim)
  1246. return -EBUSY;
  1247. if (strcmp(buf, "btt") == 0 || strcmp(buf, "btt\n") == 0)
  1248. ndns->claim_class = btt_claim_class(dev);
  1249. else if (strcmp(buf, "pfn") == 0 || strcmp(buf, "pfn\n") == 0)
  1250. ndns->claim_class = NVDIMM_CCLASS_PFN;
  1251. else if (strcmp(buf, "dax") == 0 || strcmp(buf, "dax\n") == 0)
  1252. ndns->claim_class = NVDIMM_CCLASS_DAX;
  1253. else if (strcmp(buf, "") == 0 || strcmp(buf, "\n") == 0)
  1254. ndns->claim_class = NVDIMM_CCLASS_NONE;
  1255. else
  1256. return -EINVAL;
  1257. /* btt_claim_class() could've returned an error */
  1258. if (ndns->claim_class < 0)
  1259. return ndns->claim_class;
  1260. return 0;
  1261. }
  1262. static ssize_t holder_class_store(struct device *dev,
  1263. struct device_attribute *attr, const char *buf, size_t len)
  1264. {
  1265. struct nd_region *nd_region = to_nd_region(dev->parent);
  1266. ssize_t rc;
  1267. device_lock(dev);
  1268. nvdimm_bus_lock(dev);
  1269. wait_nvdimm_bus_probe_idle(dev);
  1270. rc = __holder_class_store(dev, buf);
  1271. if (rc >= 0)
  1272. rc = nd_namespace_label_update(nd_region, dev);
  1273. dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
  1274. nvdimm_bus_unlock(dev);
  1275. device_unlock(dev);
  1276. return rc < 0 ? rc : len;
  1277. }
  1278. static ssize_t holder_class_show(struct device *dev,
  1279. struct device_attribute *attr, char *buf)
  1280. {
  1281. struct nd_namespace_common *ndns = to_ndns(dev);
  1282. ssize_t rc;
  1283. device_lock(dev);
  1284. if (ndns->claim_class == NVDIMM_CCLASS_NONE)
  1285. rc = sprintf(buf, "\n");
  1286. else if ((ndns->claim_class == NVDIMM_CCLASS_BTT) ||
  1287. (ndns->claim_class == NVDIMM_CCLASS_BTT2))
  1288. rc = sprintf(buf, "btt\n");
  1289. else if (ndns->claim_class == NVDIMM_CCLASS_PFN)
  1290. rc = sprintf(buf, "pfn\n");
  1291. else if (ndns->claim_class == NVDIMM_CCLASS_DAX)
  1292. rc = sprintf(buf, "dax\n");
  1293. else
  1294. rc = sprintf(buf, "<unknown>\n");
  1295. device_unlock(dev);
  1296. return rc;
  1297. }
  1298. static DEVICE_ATTR_RW(holder_class);
  1299. static ssize_t mode_show(struct device *dev,
  1300. struct device_attribute *attr, char *buf)
  1301. {
  1302. struct nd_namespace_common *ndns = to_ndns(dev);
  1303. struct device *claim;
  1304. char *mode;
  1305. ssize_t rc;
  1306. device_lock(dev);
  1307. claim = ndns->claim;
  1308. if (claim && is_nd_btt(claim))
  1309. mode = "safe";
  1310. else if (claim && is_nd_pfn(claim))
  1311. mode = "memory";
  1312. else if (claim && is_nd_dax(claim))
  1313. mode = "dax";
  1314. else if (!claim && pmem_should_map_pages(dev))
  1315. mode = "memory";
  1316. else
  1317. mode = "raw";
  1318. rc = sprintf(buf, "%s\n", mode);
  1319. device_unlock(dev);
  1320. return rc;
  1321. }
  1322. static DEVICE_ATTR_RO(mode);
  1323. static ssize_t force_raw_store(struct device *dev,
  1324. struct device_attribute *attr, const char *buf, size_t len)
  1325. {
  1326. bool force_raw;
  1327. int rc = strtobool(buf, &force_raw);
  1328. if (rc)
  1329. return rc;
  1330. to_ndns(dev)->force_raw = force_raw;
  1331. return len;
  1332. }
  1333. static ssize_t force_raw_show(struct device *dev,
  1334. struct device_attribute *attr, char *buf)
  1335. {
  1336. return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
  1337. }
  1338. static DEVICE_ATTR_RW(force_raw);
  1339. static struct attribute *nd_namespace_attributes[] = {
  1340. &dev_attr_nstype.attr,
  1341. &dev_attr_size.attr,
  1342. &dev_attr_mode.attr,
  1343. &dev_attr_uuid.attr,
  1344. &dev_attr_holder.attr,
  1345. &dev_attr_resource.attr,
  1346. &dev_attr_alt_name.attr,
  1347. &dev_attr_force_raw.attr,
  1348. &dev_attr_sector_size.attr,
  1349. &dev_attr_dpa_extents.attr,
  1350. &dev_attr_holder_class.attr,
  1351. NULL,
  1352. };
  1353. static umode_t namespace_visible(struct kobject *kobj,
  1354. struct attribute *a, int n)
  1355. {
  1356. struct device *dev = container_of(kobj, struct device, kobj);
  1357. if (a == &dev_attr_resource.attr) {
  1358. if (is_namespace_blk(dev))
  1359. return 0;
  1360. return a->mode;
  1361. }
  1362. if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
  1363. if (a == &dev_attr_size.attr)
  1364. return 0644;
  1365. return a->mode;
  1366. }
  1367. if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
  1368. || a == &dev_attr_holder.attr
  1369. || a == &dev_attr_holder_class.attr
  1370. || a == &dev_attr_force_raw.attr
  1371. || a == &dev_attr_mode.attr)
  1372. return a->mode;
  1373. return 0;
  1374. }
  1375. static struct attribute_group nd_namespace_attribute_group = {
  1376. .attrs = nd_namespace_attributes,
  1377. .is_visible = namespace_visible,
  1378. };
  1379. static const struct attribute_group *nd_namespace_attribute_groups[] = {
  1380. &nd_device_attribute_group,
  1381. &nd_namespace_attribute_group,
  1382. &nd_numa_attribute_group,
  1383. NULL,
  1384. };
  1385. struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
  1386. {
  1387. struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
  1388. struct nd_pfn *nd_pfn = is_nd_pfn(dev) ? to_nd_pfn(dev) : NULL;
  1389. struct nd_dax *nd_dax = is_nd_dax(dev) ? to_nd_dax(dev) : NULL;
  1390. struct nd_namespace_common *ndns = NULL;
  1391. resource_size_t size;
  1392. if (nd_btt || nd_pfn || nd_dax) {
  1393. if (nd_btt)
  1394. ndns = nd_btt->ndns;
  1395. else if (nd_pfn)
  1396. ndns = nd_pfn->ndns;
  1397. else if (nd_dax)
  1398. ndns = nd_dax->nd_pfn.ndns;
  1399. if (!ndns)
  1400. return ERR_PTR(-ENODEV);
  1401. /*
  1402. * Flush any in-progess probes / removals in the driver
  1403. * for the raw personality of this namespace.
  1404. */
  1405. device_lock(&ndns->dev);
  1406. device_unlock(&ndns->dev);
  1407. if (ndns->dev.driver) {
  1408. dev_dbg(&ndns->dev, "is active, can't bind %s\n",
  1409. dev_name(dev));
  1410. return ERR_PTR(-EBUSY);
  1411. }
  1412. if (dev_WARN_ONCE(&ndns->dev, ndns->claim != dev,
  1413. "host (%s) vs claim (%s) mismatch\n",
  1414. dev_name(dev),
  1415. dev_name(ndns->claim)))
  1416. return ERR_PTR(-ENXIO);
  1417. } else {
  1418. ndns = to_ndns(dev);
  1419. if (ndns->claim) {
  1420. dev_dbg(dev, "claimed by %s, failing probe\n",
  1421. dev_name(ndns->claim));
  1422. return ERR_PTR(-ENXIO);
  1423. }
  1424. }
  1425. size = nvdimm_namespace_capacity(ndns);
  1426. if (size < ND_MIN_NAMESPACE_SIZE) {
  1427. dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
  1428. &size, ND_MIN_NAMESPACE_SIZE);
  1429. return ERR_PTR(-ENODEV);
  1430. }
  1431. if (is_namespace_pmem(&ndns->dev)) {
  1432. struct nd_namespace_pmem *nspm;
  1433. nspm = to_nd_namespace_pmem(&ndns->dev);
  1434. if (uuid_not_set(nspm->uuid, &ndns->dev, __func__))
  1435. return ERR_PTR(-ENODEV);
  1436. } else if (is_namespace_blk(&ndns->dev)) {
  1437. struct nd_namespace_blk *nsblk;
  1438. nsblk = to_nd_namespace_blk(&ndns->dev);
  1439. if (uuid_not_set(nsblk->uuid, &ndns->dev, __func__))
  1440. return ERR_PTR(-ENODEV);
  1441. if (!nsblk->lbasize) {
  1442. dev_dbg(&ndns->dev, "%s: sector size not set\n",
  1443. __func__);
  1444. return ERR_PTR(-ENODEV);
  1445. }
  1446. if (!nd_namespace_blk_validate(nsblk))
  1447. return ERR_PTR(-ENODEV);
  1448. }
  1449. return ndns;
  1450. }
  1451. EXPORT_SYMBOL(nvdimm_namespace_common_probe);
  1452. static struct device **create_namespace_io(struct nd_region *nd_region)
  1453. {
  1454. struct nd_namespace_io *nsio;
  1455. struct device *dev, **devs;
  1456. struct resource *res;
  1457. nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
  1458. if (!nsio)
  1459. return NULL;
  1460. devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
  1461. if (!devs) {
  1462. kfree(nsio);
  1463. return NULL;
  1464. }
  1465. dev = &nsio->common.dev;
  1466. dev->type = &namespace_io_device_type;
  1467. dev->parent = &nd_region->dev;
  1468. res = &nsio->res;
  1469. res->name = dev_name(&nd_region->dev);
  1470. res->flags = IORESOURCE_MEM;
  1471. res->start = nd_region->ndr_start;
  1472. res->end = res->start + nd_region->ndr_size - 1;
  1473. devs[0] = dev;
  1474. return devs;
  1475. }
  1476. static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
  1477. u64 cookie, u16 pos)
  1478. {
  1479. struct nd_namespace_label *found = NULL;
  1480. int i;
  1481. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1482. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1483. struct nd_interleave_set *nd_set = nd_region->nd_set;
  1484. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1485. struct nd_label_ent *label_ent;
  1486. bool found_uuid = false;
  1487. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  1488. struct nd_namespace_label *nd_label = label_ent->label;
  1489. u16 position, nlabel;
  1490. u64 isetcookie;
  1491. if (!nd_label)
  1492. continue;
  1493. isetcookie = __le64_to_cpu(nd_label->isetcookie);
  1494. position = __le16_to_cpu(nd_label->position);
  1495. nlabel = __le16_to_cpu(nd_label->nlabel);
  1496. if (isetcookie != cookie)
  1497. continue;
  1498. if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
  1499. continue;
  1500. if (namespace_label_has(ndd, type_guid)
  1501. && !guid_equal(&nd_set->type_guid,
  1502. &nd_label->type_guid)) {
  1503. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
  1504. nd_set->type_guid.b,
  1505. nd_label->type_guid.b);
  1506. continue;
  1507. }
  1508. if (found_uuid) {
  1509. dev_dbg(ndd->dev,
  1510. "%s duplicate entry for uuid\n",
  1511. __func__);
  1512. return false;
  1513. }
  1514. found_uuid = true;
  1515. if (nlabel != nd_region->ndr_mappings)
  1516. continue;
  1517. if (position != pos)
  1518. continue;
  1519. found = nd_label;
  1520. break;
  1521. }
  1522. if (found)
  1523. break;
  1524. }
  1525. return found != NULL;
  1526. }
  1527. static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
  1528. {
  1529. int i;
  1530. if (!pmem_id)
  1531. return -ENODEV;
  1532. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1533. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1534. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1535. struct nd_namespace_label *nd_label = NULL;
  1536. u64 hw_start, hw_end, pmem_start, pmem_end;
  1537. struct nd_label_ent *label_ent;
  1538. lockdep_assert_held(&nd_mapping->lock);
  1539. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  1540. nd_label = label_ent->label;
  1541. if (!nd_label)
  1542. continue;
  1543. if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
  1544. break;
  1545. nd_label = NULL;
  1546. }
  1547. if (!nd_label) {
  1548. WARN_ON(1);
  1549. return -EINVAL;
  1550. }
  1551. /*
  1552. * Check that this label is compliant with the dpa
  1553. * range published in NFIT
  1554. */
  1555. hw_start = nd_mapping->start;
  1556. hw_end = hw_start + nd_mapping->size;
  1557. pmem_start = __le64_to_cpu(nd_label->dpa);
  1558. pmem_end = pmem_start + __le64_to_cpu(nd_label->rawsize);
  1559. if (pmem_start >= hw_start && pmem_start < hw_end
  1560. && pmem_end <= hw_end && pmem_end > hw_start)
  1561. /* pass */;
  1562. else {
  1563. dev_dbg(&nd_region->dev, "%s invalid label for %pUb\n",
  1564. dev_name(ndd->dev), nd_label->uuid);
  1565. return -EINVAL;
  1566. }
  1567. /* move recently validated label to the front of the list */
  1568. list_move(&label_ent->list, &nd_mapping->labels);
  1569. }
  1570. return 0;
  1571. }
  1572. /**
  1573. * create_namespace_pmem - validate interleave set labelling, retrieve label0
  1574. * @nd_region: region with mappings to validate
  1575. * @nspm: target namespace to create
  1576. * @nd_label: target pmem namespace label to evaluate
  1577. */
  1578. struct device *create_namespace_pmem(struct nd_region *nd_region,
  1579. struct nd_namespace_index *nsindex,
  1580. struct nd_namespace_label *nd_label)
  1581. {
  1582. u64 cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
  1583. u64 altcookie = nd_region_interleave_set_altcookie(nd_region);
  1584. struct nd_label_ent *label_ent;
  1585. struct nd_namespace_pmem *nspm;
  1586. struct nd_mapping *nd_mapping;
  1587. resource_size_t size = 0;
  1588. struct resource *res;
  1589. struct device *dev;
  1590. int rc = 0;
  1591. u16 i;
  1592. if (cookie == 0) {
  1593. dev_dbg(&nd_region->dev, "invalid interleave-set-cookie\n");
  1594. return ERR_PTR(-ENXIO);
  1595. }
  1596. if (__le64_to_cpu(nd_label->isetcookie) != cookie) {
  1597. dev_dbg(&nd_region->dev, "invalid cookie in label: %pUb\n",
  1598. nd_label->uuid);
  1599. if (__le64_to_cpu(nd_label->isetcookie) != altcookie)
  1600. return ERR_PTR(-EAGAIN);
  1601. dev_dbg(&nd_region->dev, "valid altcookie in label: %pUb\n",
  1602. nd_label->uuid);
  1603. }
  1604. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1605. if (!nspm)
  1606. return ERR_PTR(-ENOMEM);
  1607. nspm->id = -1;
  1608. dev = &nspm->nsio.common.dev;
  1609. dev->type = &namespace_pmem_device_type;
  1610. dev->parent = &nd_region->dev;
  1611. res = &nspm->nsio.res;
  1612. res->name = dev_name(&nd_region->dev);
  1613. res->flags = IORESOURCE_MEM;
  1614. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1615. if (has_uuid_at_pos(nd_region, nd_label->uuid, cookie, i))
  1616. continue;
  1617. if (has_uuid_at_pos(nd_region, nd_label->uuid, altcookie, i))
  1618. continue;
  1619. break;
  1620. }
  1621. if (i < nd_region->ndr_mappings) {
  1622. struct nvdimm_drvdata *ndd = to_ndd(&nd_region->mapping[i]);
  1623. /*
  1624. * Give up if we don't find an instance of a uuid at each
  1625. * position (from 0 to nd_region->ndr_mappings - 1), or if we
  1626. * find a dimm with two instances of the same uuid.
  1627. */
  1628. dev_err(&nd_region->dev, "%s missing label for %pUb\n",
  1629. dev_name(ndd->dev), nd_label->uuid);
  1630. rc = -EINVAL;
  1631. goto err;
  1632. }
  1633. /*
  1634. * Fix up each mapping's 'labels' to have the validated pmem label for
  1635. * that position at labels[0], and NULL at labels[1]. In the process,
  1636. * check that the namespace aligns with interleave-set. We know
  1637. * that it does not overlap with any blk namespaces by virtue of
  1638. * the dimm being enabled (i.e. nd_label_reserve_dpa()
  1639. * succeeded).
  1640. */
  1641. rc = select_pmem_id(nd_region, nd_label->uuid);
  1642. if (rc)
  1643. goto err;
  1644. /* Calculate total size and populate namespace properties from label0 */
  1645. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1646. struct nd_namespace_label *label0;
  1647. struct nvdimm_drvdata *ndd;
  1648. nd_mapping = &nd_region->mapping[i];
  1649. label_ent = list_first_entry_or_null(&nd_mapping->labels,
  1650. typeof(*label_ent), list);
  1651. label0 = label_ent ? label_ent->label : 0;
  1652. if (!label0) {
  1653. WARN_ON(1);
  1654. continue;
  1655. }
  1656. size += __le64_to_cpu(label0->rawsize);
  1657. if (__le16_to_cpu(label0->position) != 0)
  1658. continue;
  1659. WARN_ON(nspm->alt_name || nspm->uuid);
  1660. nspm->alt_name = kmemdup((void __force *) label0->name,
  1661. NSLABEL_NAME_LEN, GFP_KERNEL);
  1662. nspm->uuid = kmemdup((void __force *) label0->uuid,
  1663. NSLABEL_UUID_LEN, GFP_KERNEL);
  1664. nspm->lbasize = __le64_to_cpu(label0->lbasize);
  1665. ndd = to_ndd(nd_mapping);
  1666. if (namespace_label_has(ndd, abstraction_guid))
  1667. nspm->nsio.common.claim_class
  1668. = to_nvdimm_cclass(&label0->abstraction_guid);
  1669. }
  1670. if (!nspm->alt_name || !nspm->uuid) {
  1671. rc = -ENOMEM;
  1672. goto err;
  1673. }
  1674. nd_namespace_pmem_set_resource(nd_region, nspm, size);
  1675. return dev;
  1676. err:
  1677. namespace_pmem_release(dev);
  1678. switch (rc) {
  1679. case -EINVAL:
  1680. dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
  1681. break;
  1682. case -ENODEV:
  1683. dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
  1684. break;
  1685. default:
  1686. dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
  1687. __func__, rc);
  1688. break;
  1689. }
  1690. return ERR_PTR(rc);
  1691. }
  1692. struct resource *nsblk_add_resource(struct nd_region *nd_region,
  1693. struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
  1694. resource_size_t start)
  1695. {
  1696. struct nd_label_id label_id;
  1697. struct resource *res;
  1698. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  1699. res = krealloc(nsblk->res,
  1700. sizeof(void *) * (nsblk->num_resources + 1),
  1701. GFP_KERNEL);
  1702. if (!res)
  1703. return NULL;
  1704. nsblk->res = (struct resource **) res;
  1705. for_each_dpa_resource(ndd, res)
  1706. if (strcmp(res->name, label_id.id) == 0
  1707. && res->start == start) {
  1708. nsblk->res[nsblk->num_resources++] = res;
  1709. return res;
  1710. }
  1711. return NULL;
  1712. }
  1713. static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
  1714. {
  1715. struct nd_namespace_blk *nsblk;
  1716. struct device *dev;
  1717. if (!is_nd_blk(&nd_region->dev))
  1718. return NULL;
  1719. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1720. if (!nsblk)
  1721. return NULL;
  1722. dev = &nsblk->common.dev;
  1723. dev->type = &namespace_blk_device_type;
  1724. nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
  1725. if (nsblk->id < 0) {
  1726. kfree(nsblk);
  1727. return NULL;
  1728. }
  1729. dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
  1730. dev->parent = &nd_region->dev;
  1731. dev->groups = nd_namespace_attribute_groups;
  1732. return &nsblk->common.dev;
  1733. }
  1734. static struct device *nd_namespace_pmem_create(struct nd_region *nd_region)
  1735. {
  1736. struct nd_namespace_pmem *nspm;
  1737. struct resource *res;
  1738. struct device *dev;
  1739. if (!is_memory(&nd_region->dev))
  1740. return NULL;
  1741. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1742. if (!nspm)
  1743. return NULL;
  1744. dev = &nspm->nsio.common.dev;
  1745. dev->type = &namespace_pmem_device_type;
  1746. dev->parent = &nd_region->dev;
  1747. res = &nspm->nsio.res;
  1748. res->name = dev_name(&nd_region->dev);
  1749. res->flags = IORESOURCE_MEM;
  1750. nspm->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
  1751. if (nspm->id < 0) {
  1752. kfree(nspm);
  1753. return NULL;
  1754. }
  1755. dev_set_name(dev, "namespace%d.%d", nd_region->id, nspm->id);
  1756. dev->parent = &nd_region->dev;
  1757. dev->groups = nd_namespace_attribute_groups;
  1758. nd_namespace_pmem_set_resource(nd_region, nspm, 0);
  1759. return dev;
  1760. }
  1761. void nd_region_create_ns_seed(struct nd_region *nd_region)
  1762. {
  1763. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1764. if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
  1765. return;
  1766. if (is_nd_blk(&nd_region->dev))
  1767. nd_region->ns_seed = nd_namespace_blk_create(nd_region);
  1768. else
  1769. nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
  1770. /*
  1771. * Seed creation failures are not fatal, provisioning is simply
  1772. * disabled until memory becomes available
  1773. */
  1774. if (!nd_region->ns_seed)
  1775. dev_err(&nd_region->dev, "failed to create %s namespace\n",
  1776. is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
  1777. else
  1778. nd_device_register(nd_region->ns_seed);
  1779. }
  1780. void nd_region_create_dax_seed(struct nd_region *nd_region)
  1781. {
  1782. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1783. nd_region->dax_seed = nd_dax_create(nd_region);
  1784. /*
  1785. * Seed creation failures are not fatal, provisioning is simply
  1786. * disabled until memory becomes available
  1787. */
  1788. if (!nd_region->dax_seed)
  1789. dev_err(&nd_region->dev, "failed to create dax namespace\n");
  1790. }
  1791. void nd_region_create_pfn_seed(struct nd_region *nd_region)
  1792. {
  1793. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1794. nd_region->pfn_seed = nd_pfn_create(nd_region);
  1795. /*
  1796. * Seed creation failures are not fatal, provisioning is simply
  1797. * disabled until memory becomes available
  1798. */
  1799. if (!nd_region->pfn_seed)
  1800. dev_err(&nd_region->dev, "failed to create pfn namespace\n");
  1801. }
  1802. void nd_region_create_btt_seed(struct nd_region *nd_region)
  1803. {
  1804. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1805. nd_region->btt_seed = nd_btt_create(nd_region);
  1806. /*
  1807. * Seed creation failures are not fatal, provisioning is simply
  1808. * disabled until memory becomes available
  1809. */
  1810. if (!nd_region->btt_seed)
  1811. dev_err(&nd_region->dev, "failed to create btt namespace\n");
  1812. }
  1813. static int add_namespace_resource(struct nd_region *nd_region,
  1814. struct nd_namespace_label *nd_label, struct device **devs,
  1815. int count)
  1816. {
  1817. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1818. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1819. int i;
  1820. for (i = 0; i < count; i++) {
  1821. u8 *uuid = namespace_to_uuid(devs[i]);
  1822. struct resource *res;
  1823. if (IS_ERR_OR_NULL(uuid)) {
  1824. WARN_ON(1);
  1825. continue;
  1826. }
  1827. if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
  1828. continue;
  1829. if (is_namespace_blk(devs[i])) {
  1830. res = nsblk_add_resource(nd_region, ndd,
  1831. to_nd_namespace_blk(devs[i]),
  1832. __le64_to_cpu(nd_label->dpa));
  1833. if (!res)
  1834. return -ENXIO;
  1835. nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
  1836. } else {
  1837. dev_err(&nd_region->dev,
  1838. "error: conflicting extents for uuid: %pUb\n",
  1839. nd_label->uuid);
  1840. return -ENXIO;
  1841. }
  1842. break;
  1843. }
  1844. return i;
  1845. }
  1846. struct device *create_namespace_blk(struct nd_region *nd_region,
  1847. struct nd_namespace_label *nd_label, int count)
  1848. {
  1849. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1850. struct nd_interleave_set *nd_set = nd_region->nd_set;
  1851. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1852. struct nd_namespace_blk *nsblk;
  1853. char name[NSLABEL_NAME_LEN];
  1854. struct device *dev = NULL;
  1855. struct resource *res;
  1856. if (namespace_label_has(ndd, type_guid)) {
  1857. if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
  1858. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
  1859. nd_set->type_guid.b,
  1860. nd_label->type_guid.b);
  1861. return ERR_PTR(-EAGAIN);
  1862. }
  1863. if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
  1864. dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
  1865. nd_set->cookie2,
  1866. __le64_to_cpu(nd_label->isetcookie));
  1867. return ERR_PTR(-EAGAIN);
  1868. }
  1869. }
  1870. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1871. if (!nsblk)
  1872. return ERR_PTR(-ENOMEM);
  1873. dev = &nsblk->common.dev;
  1874. dev->type = &namespace_blk_device_type;
  1875. dev->parent = &nd_region->dev;
  1876. nsblk->id = -1;
  1877. nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
  1878. nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
  1879. GFP_KERNEL);
  1880. if (namespace_label_has(ndd, abstraction_guid))
  1881. nsblk->common.claim_class
  1882. = to_nvdimm_cclass(&nd_label->abstraction_guid);
  1883. if (!nsblk->uuid)
  1884. goto blk_err;
  1885. memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
  1886. if (name[0])
  1887. nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
  1888. GFP_KERNEL);
  1889. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1890. __le64_to_cpu(nd_label->dpa));
  1891. if (!res)
  1892. goto blk_err;
  1893. nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
  1894. return dev;
  1895. blk_err:
  1896. namespace_blk_release(dev);
  1897. return ERR_PTR(-ENXIO);
  1898. }
  1899. static int cmp_dpa(const void *a, const void *b)
  1900. {
  1901. const struct device *dev_a = *(const struct device **) a;
  1902. const struct device *dev_b = *(const struct device **) b;
  1903. struct nd_namespace_blk *nsblk_a, *nsblk_b;
  1904. struct nd_namespace_pmem *nspm_a, *nspm_b;
  1905. if (is_namespace_io(dev_a))
  1906. return 0;
  1907. if (is_namespace_blk(dev_a)) {
  1908. nsblk_a = to_nd_namespace_blk(dev_a);
  1909. nsblk_b = to_nd_namespace_blk(dev_b);
  1910. return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
  1911. sizeof(resource_size_t));
  1912. }
  1913. nspm_a = to_nd_namespace_pmem(dev_a);
  1914. nspm_b = to_nd_namespace_pmem(dev_b);
  1915. return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
  1916. sizeof(resource_size_t));
  1917. }
  1918. static struct device **scan_labels(struct nd_region *nd_region)
  1919. {
  1920. int i, count = 0;
  1921. struct device *dev, **devs = NULL;
  1922. struct nd_label_ent *label_ent, *e;
  1923. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1924. resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
  1925. /* "safe" because create_namespace_pmem() might list_move() label_ent */
  1926. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  1927. struct nd_namespace_label *nd_label = label_ent->label;
  1928. struct device **__devs;
  1929. u32 flags;
  1930. if (!nd_label)
  1931. continue;
  1932. flags = __le32_to_cpu(nd_label->flags);
  1933. if (is_nd_blk(&nd_region->dev)
  1934. == !!(flags & NSLABEL_FLAG_LOCAL))
  1935. /* pass, region matches label type */;
  1936. else
  1937. continue;
  1938. /* skip labels that describe extents outside of the region */
  1939. if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
  1940. continue;
  1941. i = add_namespace_resource(nd_region, nd_label, devs, count);
  1942. if (i < 0)
  1943. goto err;
  1944. if (i < count)
  1945. continue;
  1946. __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
  1947. if (!__devs)
  1948. goto err;
  1949. memcpy(__devs, devs, sizeof(dev) * count);
  1950. kfree(devs);
  1951. devs = __devs;
  1952. if (is_nd_blk(&nd_region->dev))
  1953. dev = create_namespace_blk(nd_region, nd_label, count);
  1954. else {
  1955. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1956. struct nd_namespace_index *nsindex;
  1957. nsindex = to_namespace_index(ndd, ndd->ns_current);
  1958. dev = create_namespace_pmem(nd_region, nsindex, nd_label);
  1959. }
  1960. if (IS_ERR(dev)) {
  1961. switch (PTR_ERR(dev)) {
  1962. case -EAGAIN:
  1963. /* skip invalid labels */
  1964. continue;
  1965. case -ENODEV:
  1966. /* fallthrough to seed creation */
  1967. break;
  1968. default:
  1969. goto err;
  1970. }
  1971. } else
  1972. devs[count++] = dev;
  1973. }
  1974. dev_dbg(&nd_region->dev, "%s: discovered %d %s namespace%s\n",
  1975. __func__, count, is_nd_blk(&nd_region->dev)
  1976. ? "blk" : "pmem", count == 1 ? "" : "s");
  1977. if (count == 0) {
  1978. /* Publish a zero-sized namespace for userspace to configure. */
  1979. nd_mapping_free_labels(nd_mapping);
  1980. devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
  1981. if (!devs)
  1982. goto err;
  1983. if (is_nd_blk(&nd_region->dev)) {
  1984. struct nd_namespace_blk *nsblk;
  1985. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1986. if (!nsblk)
  1987. goto err;
  1988. dev = &nsblk->common.dev;
  1989. dev->type = &namespace_blk_device_type;
  1990. } else {
  1991. struct nd_namespace_pmem *nspm;
  1992. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1993. if (!nspm)
  1994. goto err;
  1995. dev = &nspm->nsio.common.dev;
  1996. dev->type = &namespace_pmem_device_type;
  1997. nd_namespace_pmem_set_resource(nd_region, nspm, 0);
  1998. }
  1999. dev->parent = &nd_region->dev;
  2000. devs[count++] = dev;
  2001. } else if (is_memory(&nd_region->dev)) {
  2002. /* clean unselected labels */
  2003. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2004. struct list_head *l, *e;
  2005. LIST_HEAD(list);
  2006. int j;
  2007. nd_mapping = &nd_region->mapping[i];
  2008. if (list_empty(&nd_mapping->labels)) {
  2009. WARN_ON(1);
  2010. continue;
  2011. }
  2012. j = count;
  2013. list_for_each_safe(l, e, &nd_mapping->labels) {
  2014. if (!j--)
  2015. break;
  2016. list_move_tail(l, &list);
  2017. }
  2018. nd_mapping_free_labels(nd_mapping);
  2019. list_splice_init(&list, &nd_mapping->labels);
  2020. }
  2021. }
  2022. if (count > 1)
  2023. sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
  2024. return devs;
  2025. err:
  2026. if (devs) {
  2027. for (i = 0; devs[i]; i++)
  2028. if (is_nd_blk(&nd_region->dev))
  2029. namespace_blk_release(devs[i]);
  2030. else
  2031. namespace_pmem_release(devs[i]);
  2032. kfree(devs);
  2033. }
  2034. return NULL;
  2035. }
  2036. static struct device **create_namespaces(struct nd_region *nd_region)
  2037. {
  2038. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  2039. struct device **devs;
  2040. int i;
  2041. if (nd_region->ndr_mappings == 0)
  2042. return NULL;
  2043. /* lock down all mappings while we scan labels */
  2044. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2045. nd_mapping = &nd_region->mapping[i];
  2046. mutex_lock_nested(&nd_mapping->lock, i);
  2047. }
  2048. devs = scan_labels(nd_region);
  2049. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2050. int reverse = nd_region->ndr_mappings - 1 - i;
  2051. nd_mapping = &nd_region->mapping[reverse];
  2052. mutex_unlock(&nd_mapping->lock);
  2053. }
  2054. return devs;
  2055. }
  2056. static int init_active_labels(struct nd_region *nd_region)
  2057. {
  2058. int i;
  2059. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2060. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  2061. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  2062. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  2063. struct nd_label_ent *label_ent;
  2064. int count, j;
  2065. /*
  2066. * If the dimm is disabled then we may need to prevent
  2067. * the region from being activated.
  2068. */
  2069. if (!ndd) {
  2070. if (test_bit(NDD_LOCKED, &nvdimm->flags))
  2071. /* fail, label data may be unreadable */;
  2072. else if (test_bit(NDD_ALIASING, &nvdimm->flags))
  2073. /* fail, labels needed to disambiguate dpa */;
  2074. else
  2075. return 0;
  2076. dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
  2077. dev_name(&nd_mapping->nvdimm->dev),
  2078. test_bit(NDD_LOCKED, &nvdimm->flags)
  2079. ? "locked" : "disabled");
  2080. return -ENXIO;
  2081. }
  2082. nd_mapping->ndd = ndd;
  2083. atomic_inc(&nvdimm->busy);
  2084. get_ndd(ndd);
  2085. count = nd_label_active_count(ndd);
  2086. dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
  2087. if (!count)
  2088. continue;
  2089. for (j = 0; j < count; j++) {
  2090. struct nd_namespace_label *label;
  2091. label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
  2092. if (!label_ent)
  2093. break;
  2094. label = nd_label_active(ndd, j);
  2095. label_ent->label = label;
  2096. mutex_lock(&nd_mapping->lock);
  2097. list_add_tail(&label_ent->list, &nd_mapping->labels);
  2098. mutex_unlock(&nd_mapping->lock);
  2099. }
  2100. if (j >= count)
  2101. continue;
  2102. mutex_lock(&nd_mapping->lock);
  2103. nd_mapping_free_labels(nd_mapping);
  2104. mutex_unlock(&nd_mapping->lock);
  2105. return -ENOMEM;
  2106. }
  2107. return 0;
  2108. }
  2109. int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
  2110. {
  2111. struct device **devs = NULL;
  2112. int i, rc = 0, type;
  2113. *err = 0;
  2114. nvdimm_bus_lock(&nd_region->dev);
  2115. rc = init_active_labels(nd_region);
  2116. if (rc) {
  2117. nvdimm_bus_unlock(&nd_region->dev);
  2118. return rc;
  2119. }
  2120. type = nd_region_to_nstype(nd_region);
  2121. switch (type) {
  2122. case ND_DEVICE_NAMESPACE_IO:
  2123. devs = create_namespace_io(nd_region);
  2124. break;
  2125. case ND_DEVICE_NAMESPACE_PMEM:
  2126. case ND_DEVICE_NAMESPACE_BLK:
  2127. devs = create_namespaces(nd_region);
  2128. break;
  2129. default:
  2130. break;
  2131. }
  2132. nvdimm_bus_unlock(&nd_region->dev);
  2133. if (!devs)
  2134. return -ENODEV;
  2135. for (i = 0; devs[i]; i++) {
  2136. struct device *dev = devs[i];
  2137. int id;
  2138. if (type == ND_DEVICE_NAMESPACE_BLK) {
  2139. struct nd_namespace_blk *nsblk;
  2140. nsblk = to_nd_namespace_blk(dev);
  2141. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  2142. GFP_KERNEL);
  2143. nsblk->id = id;
  2144. } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
  2145. struct nd_namespace_pmem *nspm;
  2146. nspm = to_nd_namespace_pmem(dev);
  2147. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  2148. GFP_KERNEL);
  2149. nspm->id = id;
  2150. } else
  2151. id = i;
  2152. if (id < 0)
  2153. break;
  2154. dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
  2155. dev->groups = nd_namespace_attribute_groups;
  2156. nd_device_register(dev);
  2157. }
  2158. if (i)
  2159. nd_region->ns_seed = devs[0];
  2160. if (devs[i]) {
  2161. int j;
  2162. for (j = i; devs[j]; j++) {
  2163. struct device *dev = devs[j];
  2164. device_initialize(dev);
  2165. put_device(dev);
  2166. }
  2167. *err = j - i;
  2168. /*
  2169. * All of the namespaces we tried to register failed, so
  2170. * fail region activation.
  2171. */
  2172. if (*err == 0)
  2173. rc = -ENODEV;
  2174. }
  2175. kfree(devs);
  2176. if (rc == -ENODEV)
  2177. return rc;
  2178. return i;
  2179. }