namespace_devs.c 47 KB

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