namespace_devs.c 66 KB

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