namespace_devs.c 65 KB

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