namespace_devs.c 53 KB

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