namespace_devs.c 55 KB

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