namespace_devs.c 65 KB

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