namespace_devs.c 58 KB

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