namespace_devs.c 65 KB

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