namespace_devs.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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/slab.h>
  16. #include <linux/nd.h>
  17. #include "nd-core.h"
  18. #include "nd.h"
  19. static void namespace_io_release(struct device *dev)
  20. {
  21. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  22. kfree(nsio);
  23. }
  24. static void namespace_pmem_release(struct device *dev)
  25. {
  26. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  27. kfree(nspm->alt_name);
  28. kfree(nspm->uuid);
  29. kfree(nspm);
  30. }
  31. static void namespace_blk_release(struct device *dev)
  32. {
  33. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  34. struct nd_region *nd_region = to_nd_region(dev->parent);
  35. if (nsblk->id >= 0)
  36. ida_simple_remove(&nd_region->ns_ida, nsblk->id);
  37. kfree(nsblk->alt_name);
  38. kfree(nsblk->uuid);
  39. kfree(nsblk->res);
  40. kfree(nsblk);
  41. }
  42. static struct device_type namespace_io_device_type = {
  43. .name = "nd_namespace_io",
  44. .release = namespace_io_release,
  45. };
  46. static struct device_type namespace_pmem_device_type = {
  47. .name = "nd_namespace_pmem",
  48. .release = namespace_pmem_release,
  49. };
  50. static struct device_type namespace_blk_device_type = {
  51. .name = "nd_namespace_blk",
  52. .release = namespace_blk_release,
  53. };
  54. static bool is_namespace_pmem(struct device *dev)
  55. {
  56. return dev ? dev->type == &namespace_pmem_device_type : false;
  57. }
  58. static bool is_namespace_blk(struct device *dev)
  59. {
  60. return dev ? dev->type == &namespace_blk_device_type : false;
  61. }
  62. static bool is_namespace_io(struct device *dev)
  63. {
  64. return dev ? dev->type == &namespace_io_device_type : false;
  65. }
  66. const char *nvdimm_namespace_disk_name(struct nd_namespace_common *ndns,
  67. char *name)
  68. {
  69. struct nd_region *nd_region = to_nd_region(ndns->dev.parent);
  70. const char *suffix = "";
  71. if (ndns->claim && is_nd_btt(ndns->claim))
  72. suffix = "s";
  73. if (is_namespace_pmem(&ndns->dev) || is_namespace_io(&ndns->dev))
  74. sprintf(name, "pmem%d%s", nd_region->id, suffix);
  75. else if (is_namespace_blk(&ndns->dev)) {
  76. struct nd_namespace_blk *nsblk;
  77. nsblk = to_nd_namespace_blk(&ndns->dev);
  78. sprintf(name, "ndblk%d.%d%s", nd_region->id, nsblk->id, suffix);
  79. } else {
  80. return NULL;
  81. }
  82. return name;
  83. }
  84. EXPORT_SYMBOL(nvdimm_namespace_disk_name);
  85. static ssize_t nstype_show(struct device *dev,
  86. struct device_attribute *attr, char *buf)
  87. {
  88. struct nd_region *nd_region = to_nd_region(dev->parent);
  89. return sprintf(buf, "%d\n", nd_region_to_nstype(nd_region));
  90. }
  91. static DEVICE_ATTR_RO(nstype);
  92. static ssize_t __alt_name_store(struct device *dev, const char *buf,
  93. const size_t len)
  94. {
  95. char *input, *pos, *alt_name, **ns_altname;
  96. ssize_t rc;
  97. if (is_namespace_pmem(dev)) {
  98. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  99. ns_altname = &nspm->alt_name;
  100. } else if (is_namespace_blk(dev)) {
  101. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  102. ns_altname = &nsblk->alt_name;
  103. } else
  104. return -ENXIO;
  105. if (dev->driver || to_ndns(dev)->claim)
  106. return -EBUSY;
  107. input = kmemdup(buf, len + 1, GFP_KERNEL);
  108. if (!input)
  109. return -ENOMEM;
  110. input[len] = '\0';
  111. pos = strim(input);
  112. if (strlen(pos) + 1 > NSLABEL_NAME_LEN) {
  113. rc = -EINVAL;
  114. goto out;
  115. }
  116. alt_name = kzalloc(NSLABEL_NAME_LEN, GFP_KERNEL);
  117. if (!alt_name) {
  118. rc = -ENOMEM;
  119. goto out;
  120. }
  121. kfree(*ns_altname);
  122. *ns_altname = alt_name;
  123. sprintf(*ns_altname, "%s", pos);
  124. rc = len;
  125. out:
  126. kfree(input);
  127. return rc;
  128. }
  129. static resource_size_t nd_namespace_blk_size(struct nd_namespace_blk *nsblk)
  130. {
  131. struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
  132. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  133. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  134. struct nd_label_id label_id;
  135. resource_size_t size = 0;
  136. struct resource *res;
  137. if (!nsblk->uuid)
  138. return 0;
  139. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  140. for_each_dpa_resource(ndd, res)
  141. if (strcmp(res->name, label_id.id) == 0)
  142. size += resource_size(res);
  143. return size;
  144. }
  145. static bool __nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
  146. {
  147. struct nd_region *nd_region = to_nd_region(nsblk->common.dev.parent);
  148. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  149. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  150. struct nd_label_id label_id;
  151. struct resource *res;
  152. int count, i;
  153. if (!nsblk->uuid || !nsblk->lbasize || !ndd)
  154. return false;
  155. count = 0;
  156. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  157. for_each_dpa_resource(ndd, res) {
  158. if (strcmp(res->name, label_id.id) != 0)
  159. continue;
  160. /*
  161. * Resources with unacknoweldged adjustments indicate a
  162. * failure to update labels
  163. */
  164. if (res->flags & DPA_RESOURCE_ADJUSTED)
  165. return false;
  166. count++;
  167. }
  168. /* These values match after a successful label update */
  169. if (count != nsblk->num_resources)
  170. return false;
  171. for (i = 0; i < nsblk->num_resources; i++) {
  172. struct resource *found = NULL;
  173. for_each_dpa_resource(ndd, res)
  174. if (res == nsblk->res[i]) {
  175. found = res;
  176. break;
  177. }
  178. /* stale resource */
  179. if (!found)
  180. return false;
  181. }
  182. return true;
  183. }
  184. resource_size_t nd_namespace_blk_validate(struct nd_namespace_blk *nsblk)
  185. {
  186. resource_size_t size;
  187. nvdimm_bus_lock(&nsblk->common.dev);
  188. size = __nd_namespace_blk_validate(nsblk);
  189. nvdimm_bus_unlock(&nsblk->common.dev);
  190. return size;
  191. }
  192. EXPORT_SYMBOL(nd_namespace_blk_validate);
  193. static int nd_namespace_label_update(struct nd_region *nd_region,
  194. struct device *dev)
  195. {
  196. dev_WARN_ONCE(dev, dev->driver || to_ndns(dev)->claim,
  197. "namespace must be idle during label update\n");
  198. if (dev->driver || to_ndns(dev)->claim)
  199. return 0;
  200. /*
  201. * Only allow label writes that will result in a valid namespace
  202. * or deletion of an existing namespace.
  203. */
  204. if (is_namespace_pmem(dev)) {
  205. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  206. resource_size_t size = resource_size(&nspm->nsio.res);
  207. if (size == 0 && nspm->uuid)
  208. /* delete allocation */;
  209. else if (!nspm->uuid)
  210. return 0;
  211. return nd_pmem_namespace_label_update(nd_region, nspm, size);
  212. } else if (is_namespace_blk(dev)) {
  213. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  214. resource_size_t size = nd_namespace_blk_size(nsblk);
  215. if (size == 0 && nsblk->uuid)
  216. /* delete allocation */;
  217. else if (!nsblk->uuid || !nsblk->lbasize)
  218. return 0;
  219. return nd_blk_namespace_label_update(nd_region, nsblk, size);
  220. } else
  221. return -ENXIO;
  222. }
  223. static ssize_t alt_name_store(struct device *dev,
  224. struct device_attribute *attr, const char *buf, size_t len)
  225. {
  226. struct nd_region *nd_region = to_nd_region(dev->parent);
  227. ssize_t rc;
  228. device_lock(dev);
  229. nvdimm_bus_lock(dev);
  230. wait_nvdimm_bus_probe_idle(dev);
  231. rc = __alt_name_store(dev, buf, len);
  232. if (rc >= 0)
  233. rc = nd_namespace_label_update(nd_region, dev);
  234. dev_dbg(dev, "%s: %s(%zd)\n", __func__, rc < 0 ? "fail " : "", rc);
  235. nvdimm_bus_unlock(dev);
  236. device_unlock(dev);
  237. return rc < 0 ? rc : len;
  238. }
  239. static ssize_t alt_name_show(struct device *dev,
  240. struct device_attribute *attr, char *buf)
  241. {
  242. char *ns_altname;
  243. if (is_namespace_pmem(dev)) {
  244. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  245. ns_altname = nspm->alt_name;
  246. } else if (is_namespace_blk(dev)) {
  247. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  248. ns_altname = nsblk->alt_name;
  249. } else
  250. return -ENXIO;
  251. return sprintf(buf, "%s\n", ns_altname ? ns_altname : "");
  252. }
  253. static DEVICE_ATTR_RW(alt_name);
  254. static int scan_free(struct nd_region *nd_region,
  255. struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
  256. resource_size_t n)
  257. {
  258. bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
  259. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  260. int rc = 0;
  261. while (n) {
  262. struct resource *res, *last;
  263. resource_size_t new_start;
  264. last = NULL;
  265. for_each_dpa_resource(ndd, res)
  266. if (strcmp(res->name, label_id->id) == 0)
  267. last = res;
  268. res = last;
  269. if (!res)
  270. return 0;
  271. if (n >= resource_size(res)) {
  272. n -= resource_size(res);
  273. nd_dbg_dpa(nd_region, ndd, res, "delete %d\n", rc);
  274. nvdimm_free_dpa(ndd, res);
  275. /* retry with last resource deleted */
  276. continue;
  277. }
  278. /*
  279. * Keep BLK allocations relegated to high DPA as much as
  280. * possible
  281. */
  282. if (is_blk)
  283. new_start = res->start + n;
  284. else
  285. new_start = res->start;
  286. rc = adjust_resource(res, new_start, resource_size(res) - n);
  287. if (rc == 0)
  288. res->flags |= DPA_RESOURCE_ADJUSTED;
  289. nd_dbg_dpa(nd_region, ndd, res, "shrink %d\n", rc);
  290. break;
  291. }
  292. return rc;
  293. }
  294. /**
  295. * shrink_dpa_allocation - for each dimm in region free n bytes for label_id
  296. * @nd_region: the set of dimms to reclaim @n bytes from
  297. * @label_id: unique identifier for the namespace consuming this dpa range
  298. * @n: number of bytes per-dimm to release
  299. *
  300. * Assumes resources are ordered. Starting from the end try to
  301. * adjust_resource() the allocation to @n, but if @n is larger than the
  302. * allocation delete it and find the 'new' last allocation in the label
  303. * set.
  304. */
  305. static int shrink_dpa_allocation(struct nd_region *nd_region,
  306. struct nd_label_id *label_id, resource_size_t n)
  307. {
  308. int i;
  309. for (i = 0; i < nd_region->ndr_mappings; i++) {
  310. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  311. int rc;
  312. rc = scan_free(nd_region, nd_mapping, label_id, n);
  313. if (rc)
  314. return rc;
  315. }
  316. return 0;
  317. }
  318. static resource_size_t init_dpa_allocation(struct nd_label_id *label_id,
  319. struct nd_region *nd_region, struct nd_mapping *nd_mapping,
  320. resource_size_t n)
  321. {
  322. bool is_blk = strncmp(label_id->id, "blk", 3) == 0;
  323. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  324. resource_size_t first_dpa;
  325. struct resource *res;
  326. int rc = 0;
  327. /* allocate blk from highest dpa first */
  328. if (is_blk)
  329. first_dpa = nd_mapping->start + nd_mapping->size - n;
  330. else
  331. first_dpa = nd_mapping->start;
  332. /* first resource allocation for this label-id or dimm */
  333. res = nvdimm_allocate_dpa(ndd, label_id, first_dpa, n);
  334. if (!res)
  335. rc = -EBUSY;
  336. nd_dbg_dpa(nd_region, ndd, res, "init %d\n", rc);
  337. return rc ? n : 0;
  338. }
  339. static bool space_valid(bool is_pmem, bool is_reserve,
  340. struct nd_label_id *label_id, struct resource *res)
  341. {
  342. /*
  343. * For BLK-space any space is valid, for PMEM-space, it must be
  344. * contiguous with an existing allocation unless we are
  345. * reserving pmem.
  346. */
  347. if (is_reserve || !is_pmem)
  348. return true;
  349. if (!res || strcmp(res->name, label_id->id) == 0)
  350. return true;
  351. return false;
  352. }
  353. enum alloc_loc {
  354. ALLOC_ERR = 0, ALLOC_BEFORE, ALLOC_MID, ALLOC_AFTER,
  355. };
  356. static resource_size_t scan_allocate(struct nd_region *nd_region,
  357. struct nd_mapping *nd_mapping, struct nd_label_id *label_id,
  358. resource_size_t n)
  359. {
  360. resource_size_t mapping_end = nd_mapping->start + nd_mapping->size - 1;
  361. bool is_reserve = strcmp(label_id->id, "pmem-reserve") == 0;
  362. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  363. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  364. const resource_size_t to_allocate = n;
  365. struct resource *res;
  366. int first;
  367. retry:
  368. first = 0;
  369. for_each_dpa_resource(ndd, res) {
  370. resource_size_t allocate, available = 0, free_start, free_end;
  371. struct resource *next = res->sibling, *new_res = NULL;
  372. enum alloc_loc loc = ALLOC_ERR;
  373. const char *action;
  374. int rc = 0;
  375. /* ignore resources outside this nd_mapping */
  376. if (res->start > mapping_end)
  377. continue;
  378. if (res->end < nd_mapping->start)
  379. continue;
  380. /* space at the beginning of the mapping */
  381. if (!first++ && res->start > nd_mapping->start) {
  382. free_start = nd_mapping->start;
  383. available = res->start - free_start;
  384. if (space_valid(is_pmem, is_reserve, label_id, NULL))
  385. loc = ALLOC_BEFORE;
  386. }
  387. /* space between allocations */
  388. if (!loc && next) {
  389. free_start = res->start + resource_size(res);
  390. free_end = min(mapping_end, next->start - 1);
  391. if (space_valid(is_pmem, is_reserve, label_id, res)
  392. && free_start < free_end) {
  393. available = free_end + 1 - free_start;
  394. loc = ALLOC_MID;
  395. }
  396. }
  397. /* space at the end of the mapping */
  398. if (!loc && !next) {
  399. free_start = res->start + resource_size(res);
  400. free_end = mapping_end;
  401. if (space_valid(is_pmem, is_reserve, label_id, res)
  402. && free_start < free_end) {
  403. available = free_end + 1 - free_start;
  404. loc = ALLOC_AFTER;
  405. }
  406. }
  407. if (!loc || !available)
  408. continue;
  409. allocate = min(available, n);
  410. switch (loc) {
  411. case ALLOC_BEFORE:
  412. if (strcmp(res->name, label_id->id) == 0) {
  413. /* adjust current resource up */
  414. if (is_pmem && !is_reserve)
  415. return n;
  416. rc = adjust_resource(res, res->start - allocate,
  417. resource_size(res) + allocate);
  418. action = "cur grow up";
  419. } else
  420. action = "allocate";
  421. break;
  422. case ALLOC_MID:
  423. if (strcmp(next->name, label_id->id) == 0) {
  424. /* adjust next resource up */
  425. if (is_pmem && !is_reserve)
  426. return n;
  427. rc = adjust_resource(next, next->start
  428. - allocate, resource_size(next)
  429. + allocate);
  430. new_res = next;
  431. action = "next grow up";
  432. } else if (strcmp(res->name, label_id->id) == 0) {
  433. action = "grow down";
  434. } else
  435. action = "allocate";
  436. break;
  437. case ALLOC_AFTER:
  438. if (strcmp(res->name, label_id->id) == 0)
  439. action = "grow down";
  440. else
  441. action = "allocate";
  442. break;
  443. default:
  444. return n;
  445. }
  446. if (strcmp(action, "allocate") == 0) {
  447. /* BLK allocate bottom up */
  448. if (!is_pmem)
  449. free_start += available - allocate;
  450. else if (!is_reserve && free_start != nd_mapping->start)
  451. return n;
  452. new_res = nvdimm_allocate_dpa(ndd, label_id,
  453. free_start, allocate);
  454. if (!new_res)
  455. rc = -EBUSY;
  456. } else if (strcmp(action, "grow down") == 0) {
  457. /* adjust current resource down */
  458. rc = adjust_resource(res, res->start, resource_size(res)
  459. + allocate);
  460. if (rc == 0)
  461. res->flags |= DPA_RESOURCE_ADJUSTED;
  462. }
  463. if (!new_res)
  464. new_res = res;
  465. nd_dbg_dpa(nd_region, ndd, new_res, "%s(%d) %d\n",
  466. action, loc, rc);
  467. if (rc)
  468. return n;
  469. n -= allocate;
  470. if (n) {
  471. /*
  472. * Retry scan with newly inserted resources.
  473. * For example, if we did an ALLOC_BEFORE
  474. * insertion there may also have been space
  475. * available for an ALLOC_AFTER insertion, so we
  476. * need to check this same resource again
  477. */
  478. goto retry;
  479. } else
  480. return 0;
  481. }
  482. /*
  483. * If we allocated nothing in the BLK case it may be because we are in
  484. * an initial "pmem-reserve pass". Only do an initial BLK allocation
  485. * when none of the DPA space is reserved.
  486. */
  487. if ((is_pmem || !ndd->dpa.child) && n == to_allocate)
  488. return init_dpa_allocation(label_id, nd_region, nd_mapping, n);
  489. return n;
  490. }
  491. static int merge_dpa(struct nd_region *nd_region,
  492. struct nd_mapping *nd_mapping, struct nd_label_id *label_id)
  493. {
  494. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  495. struct resource *res;
  496. if (strncmp("pmem", label_id->id, 4) == 0)
  497. return 0;
  498. retry:
  499. for_each_dpa_resource(ndd, res) {
  500. int rc;
  501. struct resource *next = res->sibling;
  502. resource_size_t end = res->start + resource_size(res);
  503. if (!next || strcmp(res->name, label_id->id) != 0
  504. || strcmp(next->name, label_id->id) != 0
  505. || end != next->start)
  506. continue;
  507. end += resource_size(next);
  508. nvdimm_free_dpa(ndd, next);
  509. rc = adjust_resource(res, res->start, end - res->start);
  510. nd_dbg_dpa(nd_region, ndd, res, "merge %d\n", rc);
  511. if (rc)
  512. return rc;
  513. res->flags |= DPA_RESOURCE_ADJUSTED;
  514. goto retry;
  515. }
  516. return 0;
  517. }
  518. static int __reserve_free_pmem(struct device *dev, void *data)
  519. {
  520. struct nvdimm *nvdimm = data;
  521. struct nd_region *nd_region;
  522. struct nd_label_id label_id;
  523. int i;
  524. if (!is_nd_pmem(dev))
  525. return 0;
  526. nd_region = to_nd_region(dev);
  527. if (nd_region->ndr_mappings == 0)
  528. return 0;
  529. memset(&label_id, 0, sizeof(label_id));
  530. strcat(label_id.id, "pmem-reserve");
  531. for (i = 0; i < nd_region->ndr_mappings; i++) {
  532. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  533. resource_size_t n, rem = 0;
  534. if (nd_mapping->nvdimm != nvdimm)
  535. continue;
  536. n = nd_pmem_available_dpa(nd_region, nd_mapping, &rem);
  537. if (n == 0)
  538. return 0;
  539. rem = scan_allocate(nd_region, nd_mapping, &label_id, n);
  540. dev_WARN_ONCE(&nd_region->dev, rem,
  541. "pmem reserve underrun: %#llx of %#llx bytes\n",
  542. (unsigned long long) n - rem,
  543. (unsigned long long) n);
  544. return rem ? -ENXIO : 0;
  545. }
  546. return 0;
  547. }
  548. static void release_free_pmem(struct nvdimm_bus *nvdimm_bus,
  549. struct nd_mapping *nd_mapping)
  550. {
  551. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  552. struct resource *res, *_res;
  553. for_each_dpa_resource_safe(ndd, res, _res)
  554. if (strcmp(res->name, "pmem-reserve") == 0)
  555. nvdimm_free_dpa(ndd, res);
  556. }
  557. static int reserve_free_pmem(struct nvdimm_bus *nvdimm_bus,
  558. struct nd_mapping *nd_mapping)
  559. {
  560. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  561. int rc;
  562. rc = device_for_each_child(&nvdimm_bus->dev, nvdimm,
  563. __reserve_free_pmem);
  564. if (rc)
  565. release_free_pmem(nvdimm_bus, nd_mapping);
  566. return rc;
  567. }
  568. /**
  569. * grow_dpa_allocation - for each dimm allocate n bytes for @label_id
  570. * @nd_region: the set of dimms to allocate @n more bytes from
  571. * @label_id: unique identifier for the namespace consuming this dpa range
  572. * @n: number of bytes per-dimm to add to the existing allocation
  573. *
  574. * Assumes resources are ordered. For BLK regions, first consume
  575. * BLK-only available DPA free space, then consume PMEM-aliased DPA
  576. * space starting at the highest DPA. For PMEM regions start
  577. * allocations from the start of an interleave set and end at the first
  578. * BLK allocation or the end of the interleave set, whichever comes
  579. * first.
  580. */
  581. static int grow_dpa_allocation(struct nd_region *nd_region,
  582. struct nd_label_id *label_id, resource_size_t n)
  583. {
  584. struct nvdimm_bus *nvdimm_bus = walk_to_nvdimm_bus(&nd_region->dev);
  585. bool is_pmem = strncmp(label_id->id, "pmem", 4) == 0;
  586. int i;
  587. for (i = 0; i < nd_region->ndr_mappings; i++) {
  588. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  589. resource_size_t rem = n;
  590. int rc, j;
  591. /*
  592. * In the BLK case try once with all unallocated PMEM
  593. * reserved, and once without
  594. */
  595. for (j = is_pmem; j < 2; j++) {
  596. bool blk_only = j == 0;
  597. if (blk_only) {
  598. rc = reserve_free_pmem(nvdimm_bus, nd_mapping);
  599. if (rc)
  600. return rc;
  601. }
  602. rem = scan_allocate(nd_region, nd_mapping,
  603. label_id, rem);
  604. if (blk_only)
  605. release_free_pmem(nvdimm_bus, nd_mapping);
  606. /* try again and allow encroachments into PMEM */
  607. if (rem == 0)
  608. break;
  609. }
  610. dev_WARN_ONCE(&nd_region->dev, rem,
  611. "allocation underrun: %#llx of %#llx bytes\n",
  612. (unsigned long long) n - rem,
  613. (unsigned long long) n);
  614. if (rem)
  615. return -ENXIO;
  616. rc = merge_dpa(nd_region, nd_mapping, label_id);
  617. if (rc)
  618. return rc;
  619. }
  620. return 0;
  621. }
  622. static void nd_namespace_pmem_set_size(struct nd_region *nd_region,
  623. struct nd_namespace_pmem *nspm, resource_size_t size)
  624. {
  625. struct resource *res = &nspm->nsio.res;
  626. res->start = nd_region->ndr_start;
  627. res->end = nd_region->ndr_start + size - 1;
  628. }
  629. static ssize_t __size_store(struct device *dev, unsigned long long val)
  630. {
  631. resource_size_t allocated = 0, available = 0;
  632. struct nd_region *nd_region = to_nd_region(dev->parent);
  633. struct nd_mapping *nd_mapping;
  634. struct nvdimm_drvdata *ndd;
  635. struct nd_label_id label_id;
  636. u32 flags = 0, remainder;
  637. u8 *uuid = NULL;
  638. int rc, i;
  639. if (dev->driver || to_ndns(dev)->claim)
  640. return -EBUSY;
  641. if (is_namespace_pmem(dev)) {
  642. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  643. uuid = nspm->uuid;
  644. } else if (is_namespace_blk(dev)) {
  645. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  646. uuid = nsblk->uuid;
  647. flags = NSLABEL_FLAG_LOCAL;
  648. }
  649. /*
  650. * We need a uuid for the allocation-label and dimm(s) on which
  651. * to store the label.
  652. */
  653. if (!uuid || nd_region->ndr_mappings == 0)
  654. return -ENXIO;
  655. div_u64_rem(val, SZ_4K * nd_region->ndr_mappings, &remainder);
  656. if (remainder) {
  657. dev_dbg(dev, "%llu is not %dK aligned\n", val,
  658. (SZ_4K * nd_region->ndr_mappings) / 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, or the base dimm for a blk
  667. * region, need to be enabled 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_available_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_size(nd_region, nspm,
  690. val * nd_region->ndr_mappings);
  691. } else if (is_namespace_blk(dev)) {
  692. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  693. /*
  694. * Try to delete the namespace if we deleted all of its
  695. * allocation, this is not the seed device for the
  696. * region, and it is not actively claimed by a btt
  697. * instance.
  698. */
  699. if (val == 0 && nd_region->ns_seed != dev
  700. && !nsblk->common.claim)
  701. nd_device_unregister(dev, ND_ASYNC);
  702. }
  703. return rc;
  704. }
  705. static ssize_t size_store(struct device *dev,
  706. struct device_attribute *attr, const char *buf, size_t len)
  707. {
  708. struct nd_region *nd_region = to_nd_region(dev->parent);
  709. unsigned long long val;
  710. u8 **uuid = NULL;
  711. int rc;
  712. rc = kstrtoull(buf, 0, &val);
  713. if (rc)
  714. return rc;
  715. device_lock(dev);
  716. nvdimm_bus_lock(dev);
  717. wait_nvdimm_bus_probe_idle(dev);
  718. rc = __size_store(dev, val);
  719. if (rc >= 0)
  720. rc = nd_namespace_label_update(nd_region, dev);
  721. if (is_namespace_pmem(dev)) {
  722. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  723. uuid = &nspm->uuid;
  724. } else if (is_namespace_blk(dev)) {
  725. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  726. uuid = &nsblk->uuid;
  727. }
  728. if (rc == 0 && val == 0 && uuid) {
  729. /* setting size zero == 'delete namespace' */
  730. kfree(*uuid);
  731. *uuid = NULL;
  732. }
  733. dev_dbg(dev, "%s: %llx %s (%d)\n", __func__, val, rc < 0
  734. ? "fail" : "success", rc);
  735. nvdimm_bus_unlock(dev);
  736. device_unlock(dev);
  737. return rc < 0 ? rc : len;
  738. }
  739. resource_size_t __nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  740. {
  741. struct device *dev = &ndns->dev;
  742. if (is_namespace_pmem(dev)) {
  743. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  744. return resource_size(&nspm->nsio.res);
  745. } else if (is_namespace_blk(dev)) {
  746. return nd_namespace_blk_size(to_nd_namespace_blk(dev));
  747. } else if (is_namespace_io(dev)) {
  748. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  749. return resource_size(&nsio->res);
  750. } else
  751. WARN_ONCE(1, "unknown namespace type\n");
  752. return 0;
  753. }
  754. resource_size_t nvdimm_namespace_capacity(struct nd_namespace_common *ndns)
  755. {
  756. resource_size_t size;
  757. nvdimm_bus_lock(&ndns->dev);
  758. size = __nvdimm_namespace_capacity(ndns);
  759. nvdimm_bus_unlock(&ndns->dev);
  760. return size;
  761. }
  762. EXPORT_SYMBOL(nvdimm_namespace_capacity);
  763. static ssize_t size_show(struct device *dev,
  764. struct device_attribute *attr, char *buf)
  765. {
  766. return sprintf(buf, "%llu\n", (unsigned long long)
  767. nvdimm_namespace_capacity(to_ndns(dev)));
  768. }
  769. static DEVICE_ATTR(size, S_IRUGO, size_show, size_store);
  770. static ssize_t uuid_show(struct device *dev,
  771. struct device_attribute *attr, char *buf)
  772. {
  773. u8 *uuid;
  774. if (is_namespace_pmem(dev)) {
  775. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  776. uuid = nspm->uuid;
  777. } else if (is_namespace_blk(dev)) {
  778. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  779. uuid = nsblk->uuid;
  780. } else
  781. return -ENXIO;
  782. if (uuid)
  783. return sprintf(buf, "%pUb\n", uuid);
  784. return sprintf(buf, "\n");
  785. }
  786. /**
  787. * namespace_update_uuid - check for a unique uuid and whether we're "renaming"
  788. * @nd_region: parent region so we can updates all dimms in the set
  789. * @dev: namespace type for generating label_id
  790. * @new_uuid: incoming uuid
  791. * @old_uuid: reference to the uuid storage location in the namespace object
  792. */
  793. static int namespace_update_uuid(struct nd_region *nd_region,
  794. struct device *dev, u8 *new_uuid, u8 **old_uuid)
  795. {
  796. u32 flags = is_namespace_blk(dev) ? NSLABEL_FLAG_LOCAL : 0;
  797. struct nd_label_id old_label_id;
  798. struct nd_label_id new_label_id;
  799. int i;
  800. if (!nd_is_uuid_unique(dev, new_uuid))
  801. return -EINVAL;
  802. if (*old_uuid == NULL)
  803. goto out;
  804. /*
  805. * If we've already written a label with this uuid, then it's
  806. * too late to rename because we can't reliably update the uuid
  807. * without losing the old namespace. Userspace must delete this
  808. * namespace to abandon the old uuid.
  809. */
  810. for (i = 0; i < nd_region->ndr_mappings; i++) {
  811. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  812. /*
  813. * This check by itself is sufficient because old_uuid
  814. * would be NULL above if this uuid did not exist in the
  815. * currently written set.
  816. *
  817. * FIXME: can we delete uuid with zero dpa allocated?
  818. */
  819. if (nd_mapping->labels)
  820. return -EBUSY;
  821. }
  822. nd_label_gen_id(&old_label_id, *old_uuid, flags);
  823. nd_label_gen_id(&new_label_id, new_uuid, flags);
  824. for (i = 0; i < nd_region->ndr_mappings; i++) {
  825. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  826. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  827. struct resource *res;
  828. for_each_dpa_resource(ndd, res)
  829. if (strcmp(res->name, old_label_id.id) == 0)
  830. sprintf((void *) res->name, "%s",
  831. new_label_id.id);
  832. }
  833. kfree(*old_uuid);
  834. out:
  835. *old_uuid = new_uuid;
  836. return 0;
  837. }
  838. static ssize_t uuid_store(struct device *dev,
  839. struct device_attribute *attr, const char *buf, size_t len)
  840. {
  841. struct nd_region *nd_region = to_nd_region(dev->parent);
  842. u8 *uuid = NULL;
  843. ssize_t rc = 0;
  844. u8 **ns_uuid;
  845. if (is_namespace_pmem(dev)) {
  846. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  847. ns_uuid = &nspm->uuid;
  848. } else if (is_namespace_blk(dev)) {
  849. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  850. ns_uuid = &nsblk->uuid;
  851. } else
  852. return -ENXIO;
  853. device_lock(dev);
  854. nvdimm_bus_lock(dev);
  855. wait_nvdimm_bus_probe_idle(dev);
  856. if (to_ndns(dev)->claim)
  857. rc = -EBUSY;
  858. if (rc >= 0)
  859. rc = nd_uuid_store(dev, &uuid, buf, len);
  860. if (rc >= 0)
  861. rc = namespace_update_uuid(nd_region, dev, uuid, ns_uuid);
  862. if (rc >= 0)
  863. rc = nd_namespace_label_update(nd_region, dev);
  864. else
  865. kfree(uuid);
  866. dev_dbg(dev, "%s: result: %zd wrote: %s%s", __func__,
  867. rc, buf, buf[len - 1] == '\n' ? "" : "\n");
  868. nvdimm_bus_unlock(dev);
  869. device_unlock(dev);
  870. return rc < 0 ? rc : len;
  871. }
  872. static DEVICE_ATTR_RW(uuid);
  873. static ssize_t resource_show(struct device *dev,
  874. struct device_attribute *attr, char *buf)
  875. {
  876. struct resource *res;
  877. if (is_namespace_pmem(dev)) {
  878. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  879. res = &nspm->nsio.res;
  880. } else if (is_namespace_io(dev)) {
  881. struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
  882. res = &nsio->res;
  883. } else
  884. return -ENXIO;
  885. /* no address to convey if the namespace has no allocation */
  886. if (resource_size(res) == 0)
  887. return -ENXIO;
  888. return sprintf(buf, "%#llx\n", (unsigned long long) res->start);
  889. }
  890. static DEVICE_ATTR_RO(resource);
  891. static const unsigned long ns_lbasize_supported[] = { 512, 520, 528,
  892. 4096, 4104, 4160, 4224, 0 };
  893. static ssize_t sector_size_show(struct device *dev,
  894. struct device_attribute *attr, char *buf)
  895. {
  896. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  897. if (!is_namespace_blk(dev))
  898. return -ENXIO;
  899. return nd_sector_size_show(nsblk->lbasize, ns_lbasize_supported, buf);
  900. }
  901. static ssize_t sector_size_store(struct device *dev,
  902. struct device_attribute *attr, const char *buf, size_t len)
  903. {
  904. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  905. struct nd_region *nd_region = to_nd_region(dev->parent);
  906. ssize_t rc = 0;
  907. if (!is_namespace_blk(dev))
  908. return -ENXIO;
  909. device_lock(dev);
  910. nvdimm_bus_lock(dev);
  911. if (to_ndns(dev)->claim)
  912. rc = -EBUSY;
  913. if (rc >= 0)
  914. rc = nd_sector_size_store(dev, buf, &nsblk->lbasize,
  915. ns_lbasize_supported);
  916. if (rc >= 0)
  917. rc = nd_namespace_label_update(nd_region, dev);
  918. dev_dbg(dev, "%s: result: %zd %s: %s%s", __func__,
  919. rc, rc < 0 ? "tried" : "wrote", buf,
  920. buf[len - 1] == '\n' ? "" : "\n");
  921. nvdimm_bus_unlock(dev);
  922. device_unlock(dev);
  923. return rc ? rc : len;
  924. }
  925. static DEVICE_ATTR_RW(sector_size);
  926. static ssize_t dpa_extents_show(struct device *dev,
  927. struct device_attribute *attr, char *buf)
  928. {
  929. struct nd_region *nd_region = to_nd_region(dev->parent);
  930. struct nd_label_id label_id;
  931. int count = 0, i;
  932. u8 *uuid = NULL;
  933. u32 flags = 0;
  934. nvdimm_bus_lock(dev);
  935. if (is_namespace_pmem(dev)) {
  936. struct nd_namespace_pmem *nspm = to_nd_namespace_pmem(dev);
  937. uuid = nspm->uuid;
  938. flags = 0;
  939. } else if (is_namespace_blk(dev)) {
  940. struct nd_namespace_blk *nsblk = to_nd_namespace_blk(dev);
  941. uuid = nsblk->uuid;
  942. flags = NSLABEL_FLAG_LOCAL;
  943. }
  944. if (!uuid)
  945. goto out;
  946. nd_label_gen_id(&label_id, uuid, flags);
  947. for (i = 0; i < nd_region->ndr_mappings; i++) {
  948. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  949. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  950. struct resource *res;
  951. for_each_dpa_resource(ndd, res)
  952. if (strcmp(res->name, label_id.id) == 0)
  953. count++;
  954. }
  955. out:
  956. nvdimm_bus_unlock(dev);
  957. return sprintf(buf, "%d\n", count);
  958. }
  959. static DEVICE_ATTR_RO(dpa_extents);
  960. static ssize_t holder_show(struct device *dev,
  961. struct device_attribute *attr, char *buf)
  962. {
  963. struct nd_namespace_common *ndns = to_ndns(dev);
  964. ssize_t rc;
  965. device_lock(dev);
  966. rc = sprintf(buf, "%s\n", ndns->claim ? dev_name(ndns->claim) : "");
  967. device_unlock(dev);
  968. return rc;
  969. }
  970. static DEVICE_ATTR_RO(holder);
  971. static ssize_t force_raw_store(struct device *dev,
  972. struct device_attribute *attr, const char *buf, size_t len)
  973. {
  974. bool force_raw;
  975. int rc = strtobool(buf, &force_raw);
  976. if (rc)
  977. return rc;
  978. to_ndns(dev)->force_raw = force_raw;
  979. return len;
  980. }
  981. static ssize_t force_raw_show(struct device *dev,
  982. struct device_attribute *attr, char *buf)
  983. {
  984. return sprintf(buf, "%d\n", to_ndns(dev)->force_raw);
  985. }
  986. static DEVICE_ATTR_RW(force_raw);
  987. static struct attribute *nd_namespace_attributes[] = {
  988. &dev_attr_nstype.attr,
  989. &dev_attr_size.attr,
  990. &dev_attr_uuid.attr,
  991. &dev_attr_holder.attr,
  992. &dev_attr_resource.attr,
  993. &dev_attr_alt_name.attr,
  994. &dev_attr_force_raw.attr,
  995. &dev_attr_sector_size.attr,
  996. &dev_attr_dpa_extents.attr,
  997. NULL,
  998. };
  999. static umode_t namespace_visible(struct kobject *kobj,
  1000. struct attribute *a, int n)
  1001. {
  1002. struct device *dev = container_of(kobj, struct device, kobj);
  1003. if (a == &dev_attr_resource.attr) {
  1004. if (is_namespace_blk(dev))
  1005. return 0;
  1006. return a->mode;
  1007. }
  1008. if (is_namespace_pmem(dev) || is_namespace_blk(dev)) {
  1009. if (a == &dev_attr_size.attr)
  1010. return S_IWUSR | S_IRUGO;
  1011. if (is_namespace_pmem(dev) && a == &dev_attr_sector_size.attr)
  1012. return 0;
  1013. return a->mode;
  1014. }
  1015. if (a == &dev_attr_nstype.attr || a == &dev_attr_size.attr
  1016. || a == &dev_attr_holder.attr
  1017. || a == &dev_attr_force_raw.attr)
  1018. return a->mode;
  1019. return 0;
  1020. }
  1021. static struct attribute_group nd_namespace_attribute_group = {
  1022. .attrs = nd_namespace_attributes,
  1023. .is_visible = namespace_visible,
  1024. };
  1025. static const struct attribute_group *nd_namespace_attribute_groups[] = {
  1026. &nd_device_attribute_group,
  1027. &nd_namespace_attribute_group,
  1028. &nd_numa_attribute_group,
  1029. NULL,
  1030. };
  1031. struct nd_namespace_common *nvdimm_namespace_common_probe(struct device *dev)
  1032. {
  1033. struct nd_btt *nd_btt = is_nd_btt(dev) ? to_nd_btt(dev) : NULL;
  1034. struct nd_namespace_common *ndns;
  1035. resource_size_t size;
  1036. if (nd_btt) {
  1037. ndns = nd_btt->ndns;
  1038. if (!ndns)
  1039. return ERR_PTR(-ENODEV);
  1040. /*
  1041. * Flush any in-progess probes / removals in the driver
  1042. * for the raw personality of this namespace.
  1043. */
  1044. device_lock(&ndns->dev);
  1045. device_unlock(&ndns->dev);
  1046. if (ndns->dev.driver) {
  1047. dev_dbg(&ndns->dev, "is active, can't bind %s\n",
  1048. dev_name(&nd_btt->dev));
  1049. return ERR_PTR(-EBUSY);
  1050. }
  1051. if (dev_WARN_ONCE(&ndns->dev, ndns->claim != &nd_btt->dev,
  1052. "host (%s) vs claim (%s) mismatch\n",
  1053. dev_name(&nd_btt->dev),
  1054. dev_name(ndns->claim)))
  1055. return ERR_PTR(-ENXIO);
  1056. } else {
  1057. ndns = to_ndns(dev);
  1058. if (ndns->claim) {
  1059. dev_dbg(dev, "claimed by %s, failing probe\n",
  1060. dev_name(ndns->claim));
  1061. return ERR_PTR(-ENXIO);
  1062. }
  1063. }
  1064. size = nvdimm_namespace_capacity(ndns);
  1065. if (size < ND_MIN_NAMESPACE_SIZE) {
  1066. dev_dbg(&ndns->dev, "%pa, too small must be at least %#x\n",
  1067. &size, ND_MIN_NAMESPACE_SIZE);
  1068. return ERR_PTR(-ENODEV);
  1069. }
  1070. if (is_namespace_pmem(&ndns->dev)) {
  1071. struct nd_namespace_pmem *nspm;
  1072. nspm = to_nd_namespace_pmem(&ndns->dev);
  1073. if (!nspm->uuid) {
  1074. dev_dbg(&ndns->dev, "%s: uuid not set\n", __func__);
  1075. return ERR_PTR(-ENODEV);
  1076. }
  1077. } else if (is_namespace_blk(&ndns->dev)) {
  1078. struct nd_namespace_blk *nsblk;
  1079. nsblk = to_nd_namespace_blk(&ndns->dev);
  1080. if (!nd_namespace_blk_validate(nsblk))
  1081. return ERR_PTR(-ENODEV);
  1082. }
  1083. return ndns;
  1084. }
  1085. EXPORT_SYMBOL(nvdimm_namespace_common_probe);
  1086. static struct device **create_namespace_io(struct nd_region *nd_region)
  1087. {
  1088. struct nd_namespace_io *nsio;
  1089. struct device *dev, **devs;
  1090. struct resource *res;
  1091. nsio = kzalloc(sizeof(*nsio), GFP_KERNEL);
  1092. if (!nsio)
  1093. return NULL;
  1094. devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
  1095. if (!devs) {
  1096. kfree(nsio);
  1097. return NULL;
  1098. }
  1099. dev = &nsio->common.dev;
  1100. dev->type = &namespace_io_device_type;
  1101. dev->parent = &nd_region->dev;
  1102. res = &nsio->res;
  1103. res->name = dev_name(&nd_region->dev);
  1104. res->flags = IORESOURCE_MEM;
  1105. res->start = nd_region->ndr_start;
  1106. res->end = res->start + nd_region->ndr_size - 1;
  1107. devs[0] = dev;
  1108. return devs;
  1109. }
  1110. static bool has_uuid_at_pos(struct nd_region *nd_region, u8 *uuid,
  1111. u64 cookie, u16 pos)
  1112. {
  1113. struct nd_namespace_label *found = NULL;
  1114. int i;
  1115. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1116. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1117. struct nd_namespace_label *nd_label;
  1118. bool found_uuid = false;
  1119. int l;
  1120. for_each_label(l, nd_label, nd_mapping->labels) {
  1121. u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
  1122. u16 position = __le16_to_cpu(nd_label->position);
  1123. u16 nlabel = __le16_to_cpu(nd_label->nlabel);
  1124. if (isetcookie != cookie)
  1125. continue;
  1126. if (memcmp(nd_label->uuid, uuid, NSLABEL_UUID_LEN) != 0)
  1127. continue;
  1128. if (found_uuid) {
  1129. dev_dbg(to_ndd(nd_mapping)->dev,
  1130. "%s duplicate entry for uuid\n",
  1131. __func__);
  1132. return false;
  1133. }
  1134. found_uuid = true;
  1135. if (nlabel != nd_region->ndr_mappings)
  1136. continue;
  1137. if (position != pos)
  1138. continue;
  1139. found = nd_label;
  1140. break;
  1141. }
  1142. if (found)
  1143. break;
  1144. }
  1145. return found != NULL;
  1146. }
  1147. static int select_pmem_id(struct nd_region *nd_region, u8 *pmem_id)
  1148. {
  1149. struct nd_namespace_label *select = NULL;
  1150. int i;
  1151. if (!pmem_id)
  1152. return -ENODEV;
  1153. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1154. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1155. struct nd_namespace_label *nd_label;
  1156. u64 hw_start, hw_end, pmem_start, pmem_end;
  1157. int l;
  1158. for_each_label(l, nd_label, nd_mapping->labels)
  1159. if (memcmp(nd_label->uuid, pmem_id, NSLABEL_UUID_LEN) == 0)
  1160. break;
  1161. if (!nd_label) {
  1162. WARN_ON(1);
  1163. return -EINVAL;
  1164. }
  1165. select = nd_label;
  1166. /*
  1167. * Check that this label is compliant with the dpa
  1168. * range published in NFIT
  1169. */
  1170. hw_start = nd_mapping->start;
  1171. hw_end = hw_start + nd_mapping->size;
  1172. pmem_start = __le64_to_cpu(select->dpa);
  1173. pmem_end = pmem_start + __le64_to_cpu(select->rawsize);
  1174. if (pmem_start == hw_start && pmem_end <= hw_end)
  1175. /* pass */;
  1176. else
  1177. return -EINVAL;
  1178. nd_mapping->labels[0] = select;
  1179. nd_mapping->labels[1] = NULL;
  1180. }
  1181. return 0;
  1182. }
  1183. /**
  1184. * find_pmem_label_set - validate interleave set labelling, retrieve label0
  1185. * @nd_region: region with mappings to validate
  1186. */
  1187. static int find_pmem_label_set(struct nd_region *nd_region,
  1188. struct nd_namespace_pmem *nspm)
  1189. {
  1190. u64 cookie = nd_region_interleave_set_cookie(nd_region);
  1191. struct nd_namespace_label *nd_label;
  1192. u8 select_id[NSLABEL_UUID_LEN];
  1193. resource_size_t size = 0;
  1194. u8 *pmem_id = NULL;
  1195. int rc = -ENODEV, l;
  1196. u16 i;
  1197. if (cookie == 0)
  1198. return -ENXIO;
  1199. /*
  1200. * Find a complete set of labels by uuid. By definition we can start
  1201. * with any mapping as the reference label
  1202. */
  1203. for_each_label(l, nd_label, nd_region->mapping[0].labels) {
  1204. u64 isetcookie = __le64_to_cpu(nd_label->isetcookie);
  1205. if (isetcookie != cookie)
  1206. continue;
  1207. for (i = 0; nd_region->ndr_mappings; i++)
  1208. if (!has_uuid_at_pos(nd_region, nd_label->uuid,
  1209. cookie, i))
  1210. break;
  1211. if (i < nd_region->ndr_mappings) {
  1212. /*
  1213. * Give up if we don't find an instance of a
  1214. * uuid at each position (from 0 to
  1215. * nd_region->ndr_mappings - 1), or if we find a
  1216. * dimm with two instances of the same uuid.
  1217. */
  1218. rc = -EINVAL;
  1219. goto err;
  1220. } else if (pmem_id) {
  1221. /*
  1222. * If there is more than one valid uuid set, we
  1223. * need userspace to clean this up.
  1224. */
  1225. rc = -EBUSY;
  1226. goto err;
  1227. }
  1228. memcpy(select_id, nd_label->uuid, NSLABEL_UUID_LEN);
  1229. pmem_id = select_id;
  1230. }
  1231. /*
  1232. * Fix up each mapping's 'labels' to have the validated pmem label for
  1233. * that position at labels[0], and NULL at labels[1]. In the process,
  1234. * check that the namespace aligns with interleave-set. We know
  1235. * that it does not overlap with any blk namespaces by virtue of
  1236. * the dimm being enabled (i.e. nd_label_reserve_dpa()
  1237. * succeeded).
  1238. */
  1239. rc = select_pmem_id(nd_region, pmem_id);
  1240. if (rc)
  1241. goto err;
  1242. /* Calculate total size and populate namespace properties from label0 */
  1243. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1244. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1245. struct nd_namespace_label *label0 = nd_mapping->labels[0];
  1246. size += __le64_to_cpu(label0->rawsize);
  1247. if (__le16_to_cpu(label0->position) != 0)
  1248. continue;
  1249. WARN_ON(nspm->alt_name || nspm->uuid);
  1250. nspm->alt_name = kmemdup((void __force *) label0->name,
  1251. NSLABEL_NAME_LEN, GFP_KERNEL);
  1252. nspm->uuid = kmemdup((void __force *) label0->uuid,
  1253. NSLABEL_UUID_LEN, GFP_KERNEL);
  1254. }
  1255. if (!nspm->alt_name || !nspm->uuid) {
  1256. rc = -ENOMEM;
  1257. goto err;
  1258. }
  1259. nd_namespace_pmem_set_size(nd_region, nspm, size);
  1260. return 0;
  1261. err:
  1262. switch (rc) {
  1263. case -EINVAL:
  1264. dev_dbg(&nd_region->dev, "%s: invalid label(s)\n", __func__);
  1265. break;
  1266. case -ENODEV:
  1267. dev_dbg(&nd_region->dev, "%s: label not found\n", __func__);
  1268. break;
  1269. default:
  1270. dev_dbg(&nd_region->dev, "%s: unexpected err: %d\n",
  1271. __func__, rc);
  1272. break;
  1273. }
  1274. return rc;
  1275. }
  1276. static struct device **create_namespace_pmem(struct nd_region *nd_region)
  1277. {
  1278. struct nd_namespace_pmem *nspm;
  1279. struct device *dev, **devs;
  1280. struct resource *res;
  1281. int rc;
  1282. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  1283. if (!nspm)
  1284. return NULL;
  1285. dev = &nspm->nsio.common.dev;
  1286. dev->type = &namespace_pmem_device_type;
  1287. dev->parent = &nd_region->dev;
  1288. res = &nspm->nsio.res;
  1289. res->name = dev_name(&nd_region->dev);
  1290. res->flags = IORESOURCE_MEM;
  1291. rc = find_pmem_label_set(nd_region, nspm);
  1292. if (rc == -ENODEV) {
  1293. int i;
  1294. /* Pass, try to permit namespace creation... */
  1295. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1296. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1297. kfree(nd_mapping->labels);
  1298. nd_mapping->labels = NULL;
  1299. }
  1300. /* Publish a zero-sized namespace for userspace to configure. */
  1301. nd_namespace_pmem_set_size(nd_region, nspm, 0);
  1302. rc = 0;
  1303. } else if (rc)
  1304. goto err;
  1305. devs = kcalloc(2, sizeof(struct device *), GFP_KERNEL);
  1306. if (!devs)
  1307. goto err;
  1308. devs[0] = dev;
  1309. return devs;
  1310. err:
  1311. namespace_pmem_release(&nspm->nsio.common.dev);
  1312. return NULL;
  1313. }
  1314. struct resource *nsblk_add_resource(struct nd_region *nd_region,
  1315. struct nvdimm_drvdata *ndd, struct nd_namespace_blk *nsblk,
  1316. resource_size_t start)
  1317. {
  1318. struct nd_label_id label_id;
  1319. struct resource *res;
  1320. nd_label_gen_id(&label_id, nsblk->uuid, NSLABEL_FLAG_LOCAL);
  1321. res = krealloc(nsblk->res,
  1322. sizeof(void *) * (nsblk->num_resources + 1),
  1323. GFP_KERNEL);
  1324. if (!res)
  1325. return NULL;
  1326. nsblk->res = (struct resource **) res;
  1327. for_each_dpa_resource(ndd, res)
  1328. if (strcmp(res->name, label_id.id) == 0
  1329. && res->start == start) {
  1330. nsblk->res[nsblk->num_resources++] = res;
  1331. return res;
  1332. }
  1333. return NULL;
  1334. }
  1335. static struct device *nd_namespace_blk_create(struct nd_region *nd_region)
  1336. {
  1337. struct nd_namespace_blk *nsblk;
  1338. struct device *dev;
  1339. if (!is_nd_blk(&nd_region->dev))
  1340. return NULL;
  1341. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1342. if (!nsblk)
  1343. return NULL;
  1344. dev = &nsblk->common.dev;
  1345. dev->type = &namespace_blk_device_type;
  1346. nsblk->id = ida_simple_get(&nd_region->ns_ida, 0, 0, GFP_KERNEL);
  1347. if (nsblk->id < 0) {
  1348. kfree(nsblk);
  1349. return NULL;
  1350. }
  1351. dev_set_name(dev, "namespace%d.%d", nd_region->id, nsblk->id);
  1352. dev->parent = &nd_region->dev;
  1353. dev->groups = nd_namespace_attribute_groups;
  1354. return &nsblk->common.dev;
  1355. }
  1356. void nd_region_create_blk_seed(struct nd_region *nd_region)
  1357. {
  1358. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1359. nd_region->ns_seed = nd_namespace_blk_create(nd_region);
  1360. /*
  1361. * Seed creation failures are not fatal, provisioning is simply
  1362. * disabled until memory becomes available
  1363. */
  1364. if (!nd_region->ns_seed)
  1365. dev_err(&nd_region->dev, "failed to create blk namespace\n");
  1366. else
  1367. nd_device_register(nd_region->ns_seed);
  1368. }
  1369. void nd_region_create_btt_seed(struct nd_region *nd_region)
  1370. {
  1371. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1372. nd_region->btt_seed = nd_btt_create(nd_region);
  1373. /*
  1374. * Seed creation failures are not fatal, provisioning is simply
  1375. * disabled until memory becomes available
  1376. */
  1377. if (!nd_region->btt_seed)
  1378. dev_err(&nd_region->dev, "failed to create btt namespace\n");
  1379. }
  1380. static struct device **create_namespace_blk(struct nd_region *nd_region)
  1381. {
  1382. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1383. struct nd_namespace_label *nd_label;
  1384. struct device *dev, **devs = NULL;
  1385. struct nd_namespace_blk *nsblk;
  1386. struct nvdimm_drvdata *ndd;
  1387. int i, l, count = 0;
  1388. struct resource *res;
  1389. if (nd_region->ndr_mappings == 0)
  1390. return NULL;
  1391. ndd = to_ndd(nd_mapping);
  1392. for_each_label(l, nd_label, nd_mapping->labels) {
  1393. u32 flags = __le32_to_cpu(nd_label->flags);
  1394. char *name[NSLABEL_NAME_LEN];
  1395. struct device **__devs;
  1396. if (flags & NSLABEL_FLAG_LOCAL)
  1397. /* pass */;
  1398. else
  1399. continue;
  1400. for (i = 0; i < count; i++) {
  1401. nsblk = to_nd_namespace_blk(devs[i]);
  1402. if (memcmp(nsblk->uuid, nd_label->uuid,
  1403. NSLABEL_UUID_LEN) == 0) {
  1404. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1405. __le64_to_cpu(nd_label->dpa));
  1406. if (!res)
  1407. goto err;
  1408. nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
  1409. dev_name(&nsblk->common.dev));
  1410. break;
  1411. }
  1412. }
  1413. if (i < count)
  1414. continue;
  1415. __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
  1416. if (!__devs)
  1417. goto err;
  1418. memcpy(__devs, devs, sizeof(dev) * count);
  1419. kfree(devs);
  1420. devs = __devs;
  1421. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1422. if (!nsblk)
  1423. goto err;
  1424. dev = &nsblk->common.dev;
  1425. dev->type = &namespace_blk_device_type;
  1426. dev->parent = &nd_region->dev;
  1427. dev_set_name(dev, "namespace%d.%d", nd_region->id, count);
  1428. devs[count++] = dev;
  1429. nsblk->id = -1;
  1430. nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
  1431. nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
  1432. GFP_KERNEL);
  1433. if (!nsblk->uuid)
  1434. goto err;
  1435. memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
  1436. if (name[0])
  1437. nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
  1438. GFP_KERNEL);
  1439. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1440. __le64_to_cpu(nd_label->dpa));
  1441. if (!res)
  1442. goto err;
  1443. nd_dbg_dpa(nd_region, ndd, res, "%s assign\n",
  1444. dev_name(&nsblk->common.dev));
  1445. }
  1446. dev_dbg(&nd_region->dev, "%s: discovered %d blk namespace%s\n",
  1447. __func__, count, count == 1 ? "" : "s");
  1448. if (count == 0) {
  1449. /* Publish a zero-sized namespace for userspace to configure. */
  1450. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1451. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1452. kfree(nd_mapping->labels);
  1453. nd_mapping->labels = NULL;
  1454. }
  1455. devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
  1456. if (!devs)
  1457. goto err;
  1458. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1459. if (!nsblk)
  1460. goto err;
  1461. dev = &nsblk->common.dev;
  1462. dev->type = &namespace_blk_device_type;
  1463. dev->parent = &nd_region->dev;
  1464. devs[count++] = dev;
  1465. }
  1466. return devs;
  1467. err:
  1468. for (i = 0; i < count; i++) {
  1469. nsblk = to_nd_namespace_blk(devs[i]);
  1470. namespace_blk_release(&nsblk->common.dev);
  1471. }
  1472. kfree(devs);
  1473. return NULL;
  1474. }
  1475. static int init_active_labels(struct nd_region *nd_region)
  1476. {
  1477. int i;
  1478. for (i = 0; i < nd_region->ndr_mappings; i++) {
  1479. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  1480. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1481. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  1482. int count, j;
  1483. /*
  1484. * If the dimm is disabled then prevent the region from
  1485. * being activated if it aliases DPA.
  1486. */
  1487. if (!ndd) {
  1488. if ((nvdimm->flags & NDD_ALIASING) == 0)
  1489. return 0;
  1490. dev_dbg(&nd_region->dev, "%s: is disabled, failing probe\n",
  1491. dev_name(&nd_mapping->nvdimm->dev));
  1492. return -ENXIO;
  1493. }
  1494. nd_mapping->ndd = ndd;
  1495. atomic_inc(&nvdimm->busy);
  1496. get_ndd(ndd);
  1497. count = nd_label_active_count(ndd);
  1498. dev_dbg(ndd->dev, "%s: %d\n", __func__, count);
  1499. if (!count)
  1500. continue;
  1501. nd_mapping->labels = kcalloc(count + 1, sizeof(void *),
  1502. GFP_KERNEL);
  1503. if (!nd_mapping->labels)
  1504. return -ENOMEM;
  1505. for (j = 0; j < count; j++) {
  1506. struct nd_namespace_label *label;
  1507. label = nd_label_active(ndd, j);
  1508. nd_mapping->labels[j] = label;
  1509. }
  1510. }
  1511. return 0;
  1512. }
  1513. int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
  1514. {
  1515. struct device **devs = NULL;
  1516. int i, rc = 0, type;
  1517. *err = 0;
  1518. nvdimm_bus_lock(&nd_region->dev);
  1519. rc = init_active_labels(nd_region);
  1520. if (rc) {
  1521. nvdimm_bus_unlock(&nd_region->dev);
  1522. return rc;
  1523. }
  1524. type = nd_region_to_nstype(nd_region);
  1525. switch (type) {
  1526. case ND_DEVICE_NAMESPACE_IO:
  1527. devs = create_namespace_io(nd_region);
  1528. break;
  1529. case ND_DEVICE_NAMESPACE_PMEM:
  1530. devs = create_namespace_pmem(nd_region);
  1531. break;
  1532. case ND_DEVICE_NAMESPACE_BLK:
  1533. devs = create_namespace_blk(nd_region);
  1534. break;
  1535. default:
  1536. break;
  1537. }
  1538. nvdimm_bus_unlock(&nd_region->dev);
  1539. if (!devs)
  1540. return -ENODEV;
  1541. for (i = 0; devs[i]; i++) {
  1542. struct device *dev = devs[i];
  1543. int id;
  1544. if (type == ND_DEVICE_NAMESPACE_BLK) {
  1545. struct nd_namespace_blk *nsblk;
  1546. nsblk = to_nd_namespace_blk(dev);
  1547. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  1548. GFP_KERNEL);
  1549. nsblk->id = id;
  1550. } else
  1551. id = i;
  1552. if (id < 0)
  1553. break;
  1554. dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
  1555. dev->groups = nd_namespace_attribute_groups;
  1556. nd_device_register(dev);
  1557. }
  1558. if (i)
  1559. nd_region->ns_seed = devs[0];
  1560. if (devs[i]) {
  1561. int j;
  1562. for (j = i; devs[j]; j++) {
  1563. struct device *dev = devs[j];
  1564. device_initialize(dev);
  1565. put_device(dev);
  1566. }
  1567. *err = j - i;
  1568. /*
  1569. * All of the namespaces we tried to register failed, so
  1570. * fail region activation.
  1571. */
  1572. if (*err == 0)
  1573. rc = -ENODEV;
  1574. }
  1575. kfree(devs);
  1576. if (rc == -ENODEV)
  1577. return rc;
  1578. return i;
  1579. }