namespace_devs.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598
  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->groups = nd_namespace_attribute_groups;
  1770. nd_namespace_pmem_set_resource(nd_region, nspm, 0);
  1771. return dev;
  1772. }
  1773. void nd_region_create_ns_seed(struct nd_region *nd_region)
  1774. {
  1775. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1776. if (nd_region_to_nstype(nd_region) == ND_DEVICE_NAMESPACE_IO)
  1777. return;
  1778. if (is_nd_blk(&nd_region->dev))
  1779. nd_region->ns_seed = nd_namespace_blk_create(nd_region);
  1780. else
  1781. nd_region->ns_seed = nd_namespace_pmem_create(nd_region);
  1782. /*
  1783. * Seed creation failures are not fatal, provisioning is simply
  1784. * disabled until memory becomes available
  1785. */
  1786. if (!nd_region->ns_seed)
  1787. dev_err(&nd_region->dev, "failed to create %s namespace\n",
  1788. is_nd_blk(&nd_region->dev) ? "blk" : "pmem");
  1789. else
  1790. nd_device_register(nd_region->ns_seed);
  1791. }
  1792. void nd_region_create_dax_seed(struct nd_region *nd_region)
  1793. {
  1794. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1795. nd_region->dax_seed = nd_dax_create(nd_region);
  1796. /*
  1797. * Seed creation failures are not fatal, provisioning is simply
  1798. * disabled until memory becomes available
  1799. */
  1800. if (!nd_region->dax_seed)
  1801. dev_err(&nd_region->dev, "failed to create dax namespace\n");
  1802. }
  1803. void nd_region_create_pfn_seed(struct nd_region *nd_region)
  1804. {
  1805. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1806. nd_region->pfn_seed = nd_pfn_create(nd_region);
  1807. /*
  1808. * Seed creation failures are not fatal, provisioning is simply
  1809. * disabled until memory becomes available
  1810. */
  1811. if (!nd_region->pfn_seed)
  1812. dev_err(&nd_region->dev, "failed to create pfn namespace\n");
  1813. }
  1814. void nd_region_create_btt_seed(struct nd_region *nd_region)
  1815. {
  1816. WARN_ON(!is_nvdimm_bus_locked(&nd_region->dev));
  1817. nd_region->btt_seed = nd_btt_create(nd_region);
  1818. /*
  1819. * Seed creation failures are not fatal, provisioning is simply
  1820. * disabled until memory becomes available
  1821. */
  1822. if (!nd_region->btt_seed)
  1823. dev_err(&nd_region->dev, "failed to create btt namespace\n");
  1824. }
  1825. static int add_namespace_resource(struct nd_region *nd_region,
  1826. struct nd_namespace_label *nd_label, struct device **devs,
  1827. int count)
  1828. {
  1829. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1830. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1831. int i;
  1832. for (i = 0; i < count; i++) {
  1833. u8 *uuid = namespace_to_uuid(devs[i]);
  1834. struct resource *res;
  1835. if (IS_ERR_OR_NULL(uuid)) {
  1836. WARN_ON(1);
  1837. continue;
  1838. }
  1839. if (memcmp(uuid, nd_label->uuid, NSLABEL_UUID_LEN) != 0)
  1840. continue;
  1841. if (is_namespace_blk(devs[i])) {
  1842. res = nsblk_add_resource(nd_region, ndd,
  1843. to_nd_namespace_blk(devs[i]),
  1844. __le64_to_cpu(nd_label->dpa));
  1845. if (!res)
  1846. return -ENXIO;
  1847. nd_dbg_dpa(nd_region, ndd, res, "%d assign\n", count);
  1848. } else {
  1849. dev_err(&nd_region->dev,
  1850. "error: conflicting extents for uuid: %pUb\n",
  1851. nd_label->uuid);
  1852. return -ENXIO;
  1853. }
  1854. break;
  1855. }
  1856. return i;
  1857. }
  1858. static struct device *create_namespace_blk(struct nd_region *nd_region,
  1859. struct nd_namespace_label *nd_label, int count)
  1860. {
  1861. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1862. struct nd_interleave_set *nd_set = nd_region->nd_set;
  1863. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1864. struct nd_namespace_blk *nsblk;
  1865. char name[NSLABEL_NAME_LEN];
  1866. struct device *dev = NULL;
  1867. struct resource *res;
  1868. if (namespace_label_has(ndd, type_guid)) {
  1869. if (!guid_equal(&nd_set->type_guid, &nd_label->type_guid)) {
  1870. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n",
  1871. nd_set->type_guid.b,
  1872. nd_label->type_guid.b);
  1873. return ERR_PTR(-EAGAIN);
  1874. }
  1875. if (nd_label->isetcookie != __cpu_to_le64(nd_set->cookie2)) {
  1876. dev_dbg(ndd->dev, "expect cookie %#llx got %#llx\n",
  1877. nd_set->cookie2,
  1878. __le64_to_cpu(nd_label->isetcookie));
  1879. return ERR_PTR(-EAGAIN);
  1880. }
  1881. }
  1882. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1883. if (!nsblk)
  1884. return ERR_PTR(-ENOMEM);
  1885. dev = &nsblk->common.dev;
  1886. dev->type = &namespace_blk_device_type;
  1887. dev->parent = &nd_region->dev;
  1888. nsblk->id = -1;
  1889. nsblk->lbasize = __le64_to_cpu(nd_label->lbasize);
  1890. nsblk->uuid = kmemdup(nd_label->uuid, NSLABEL_UUID_LEN,
  1891. GFP_KERNEL);
  1892. if (namespace_label_has(ndd, abstraction_guid))
  1893. nsblk->common.claim_class
  1894. = to_nvdimm_cclass(&nd_label->abstraction_guid);
  1895. if (!nsblk->uuid)
  1896. goto blk_err;
  1897. memcpy(name, nd_label->name, NSLABEL_NAME_LEN);
  1898. if (name[0])
  1899. nsblk->alt_name = kmemdup(name, NSLABEL_NAME_LEN,
  1900. GFP_KERNEL);
  1901. res = nsblk_add_resource(nd_region, ndd, nsblk,
  1902. __le64_to_cpu(nd_label->dpa));
  1903. if (!res)
  1904. goto blk_err;
  1905. nd_dbg_dpa(nd_region, ndd, res, "%d: assign\n", count);
  1906. return dev;
  1907. blk_err:
  1908. namespace_blk_release(dev);
  1909. return ERR_PTR(-ENXIO);
  1910. }
  1911. static int cmp_dpa(const void *a, const void *b)
  1912. {
  1913. const struct device *dev_a = *(const struct device **) a;
  1914. const struct device *dev_b = *(const struct device **) b;
  1915. struct nd_namespace_blk *nsblk_a, *nsblk_b;
  1916. struct nd_namespace_pmem *nspm_a, *nspm_b;
  1917. if (is_namespace_io(dev_a))
  1918. return 0;
  1919. if (is_namespace_blk(dev_a)) {
  1920. nsblk_a = to_nd_namespace_blk(dev_a);
  1921. nsblk_b = to_nd_namespace_blk(dev_b);
  1922. return memcmp(&nsblk_a->res[0]->start, &nsblk_b->res[0]->start,
  1923. sizeof(resource_size_t));
  1924. }
  1925. nspm_a = to_nd_namespace_pmem(dev_a);
  1926. nspm_b = to_nd_namespace_pmem(dev_b);
  1927. return memcmp(&nspm_a->nsio.res.start, &nspm_b->nsio.res.start,
  1928. sizeof(resource_size_t));
  1929. }
  1930. static struct device **scan_labels(struct nd_region *nd_region)
  1931. {
  1932. int i, count = 0;
  1933. struct device *dev, **devs = NULL;
  1934. struct nd_label_ent *label_ent, *e;
  1935. struct nd_mapping *nd_mapping = &nd_region->mapping[0];
  1936. resource_size_t map_end = nd_mapping->start + nd_mapping->size - 1;
  1937. /* "safe" because create_namespace_pmem() might list_move() label_ent */
  1938. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  1939. struct nd_namespace_label *nd_label = label_ent->label;
  1940. struct device **__devs;
  1941. u32 flags;
  1942. if (!nd_label)
  1943. continue;
  1944. flags = __le32_to_cpu(nd_label->flags);
  1945. if (is_nd_blk(&nd_region->dev)
  1946. == !!(flags & NSLABEL_FLAG_LOCAL))
  1947. /* pass, region matches label type */;
  1948. else
  1949. continue;
  1950. /* skip labels that describe extents outside of the region */
  1951. if (nd_label->dpa < nd_mapping->start || nd_label->dpa > map_end)
  1952. continue;
  1953. i = add_namespace_resource(nd_region, nd_label, devs, count);
  1954. if (i < 0)
  1955. goto err;
  1956. if (i < count)
  1957. continue;
  1958. __devs = kcalloc(count + 2, sizeof(dev), GFP_KERNEL);
  1959. if (!__devs)
  1960. goto err;
  1961. memcpy(__devs, devs, sizeof(dev) * count);
  1962. kfree(devs);
  1963. devs = __devs;
  1964. if (is_nd_blk(&nd_region->dev))
  1965. dev = create_namespace_blk(nd_region, nd_label, count);
  1966. else {
  1967. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  1968. struct nd_namespace_index *nsindex;
  1969. nsindex = to_namespace_index(ndd, ndd->ns_current);
  1970. dev = create_namespace_pmem(nd_region, nsindex, nd_label);
  1971. }
  1972. if (IS_ERR(dev)) {
  1973. switch (PTR_ERR(dev)) {
  1974. case -EAGAIN:
  1975. /* skip invalid labels */
  1976. continue;
  1977. case -ENODEV:
  1978. /* fallthrough to seed creation */
  1979. break;
  1980. default:
  1981. goto err;
  1982. }
  1983. } else
  1984. devs[count++] = dev;
  1985. }
  1986. dev_dbg(&nd_region->dev, "discovered %d %s namespace%s\n",
  1987. count, is_nd_blk(&nd_region->dev)
  1988. ? "blk" : "pmem", count == 1 ? "" : "s");
  1989. if (count == 0) {
  1990. /* Publish a zero-sized namespace for userspace to configure. */
  1991. nd_mapping_free_labels(nd_mapping);
  1992. devs = kcalloc(2, sizeof(dev), GFP_KERNEL);
  1993. if (!devs)
  1994. goto err;
  1995. if (is_nd_blk(&nd_region->dev)) {
  1996. struct nd_namespace_blk *nsblk;
  1997. nsblk = kzalloc(sizeof(*nsblk), GFP_KERNEL);
  1998. if (!nsblk)
  1999. goto err;
  2000. dev = &nsblk->common.dev;
  2001. dev->type = &namespace_blk_device_type;
  2002. } else {
  2003. struct nd_namespace_pmem *nspm;
  2004. nspm = kzalloc(sizeof(*nspm), GFP_KERNEL);
  2005. if (!nspm)
  2006. goto err;
  2007. dev = &nspm->nsio.common.dev;
  2008. dev->type = &namespace_pmem_device_type;
  2009. nd_namespace_pmem_set_resource(nd_region, nspm, 0);
  2010. }
  2011. dev->parent = &nd_region->dev;
  2012. devs[count++] = dev;
  2013. } else if (is_memory(&nd_region->dev)) {
  2014. /* clean unselected labels */
  2015. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2016. struct list_head *l, *e;
  2017. LIST_HEAD(list);
  2018. int j;
  2019. nd_mapping = &nd_region->mapping[i];
  2020. if (list_empty(&nd_mapping->labels)) {
  2021. WARN_ON(1);
  2022. continue;
  2023. }
  2024. j = count;
  2025. list_for_each_safe(l, e, &nd_mapping->labels) {
  2026. if (!j--)
  2027. break;
  2028. list_move_tail(l, &list);
  2029. }
  2030. nd_mapping_free_labels(nd_mapping);
  2031. list_splice_init(&list, &nd_mapping->labels);
  2032. }
  2033. }
  2034. if (count > 1)
  2035. sort(devs, count, sizeof(struct device *), cmp_dpa, NULL);
  2036. return devs;
  2037. err:
  2038. if (devs) {
  2039. for (i = 0; devs[i]; i++)
  2040. if (is_nd_blk(&nd_region->dev))
  2041. namespace_blk_release(devs[i]);
  2042. else
  2043. namespace_pmem_release(devs[i]);
  2044. kfree(devs);
  2045. }
  2046. return NULL;
  2047. }
  2048. static struct device **create_namespaces(struct nd_region *nd_region)
  2049. {
  2050. struct nd_mapping *nd_mapping;
  2051. struct device **devs;
  2052. int i;
  2053. if (nd_region->ndr_mappings == 0)
  2054. return NULL;
  2055. /* lock down all mappings while we scan labels */
  2056. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2057. nd_mapping = &nd_region->mapping[i];
  2058. mutex_lock_nested(&nd_mapping->lock, i);
  2059. }
  2060. devs = scan_labels(nd_region);
  2061. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2062. int reverse = nd_region->ndr_mappings - 1 - i;
  2063. nd_mapping = &nd_region->mapping[reverse];
  2064. mutex_unlock(&nd_mapping->lock);
  2065. }
  2066. return devs;
  2067. }
  2068. static int init_active_labels(struct nd_region *nd_region)
  2069. {
  2070. int i;
  2071. for (i = 0; i < nd_region->ndr_mappings; i++) {
  2072. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  2073. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  2074. struct nvdimm *nvdimm = nd_mapping->nvdimm;
  2075. struct nd_label_ent *label_ent;
  2076. int count, j;
  2077. /*
  2078. * If the dimm is disabled then we may need to prevent
  2079. * the region from being activated.
  2080. */
  2081. if (!ndd) {
  2082. if (test_bit(NDD_LOCKED, &nvdimm->flags))
  2083. /* fail, label data may be unreadable */;
  2084. else if (test_bit(NDD_ALIASING, &nvdimm->flags))
  2085. /* fail, labels needed to disambiguate dpa */;
  2086. else
  2087. return 0;
  2088. dev_err(&nd_region->dev, "%s: is %s, failing probe\n",
  2089. dev_name(&nd_mapping->nvdimm->dev),
  2090. test_bit(NDD_LOCKED, &nvdimm->flags)
  2091. ? "locked" : "disabled");
  2092. return -ENXIO;
  2093. }
  2094. nd_mapping->ndd = ndd;
  2095. atomic_inc(&nvdimm->busy);
  2096. get_ndd(ndd);
  2097. count = nd_label_active_count(ndd);
  2098. dev_dbg(ndd->dev, "count: %d\n", count);
  2099. if (!count)
  2100. continue;
  2101. for (j = 0; j < count; j++) {
  2102. struct nd_namespace_label *label;
  2103. label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
  2104. if (!label_ent)
  2105. break;
  2106. label = nd_label_active(ndd, j);
  2107. label_ent->label = label;
  2108. mutex_lock(&nd_mapping->lock);
  2109. list_add_tail(&label_ent->list, &nd_mapping->labels);
  2110. mutex_unlock(&nd_mapping->lock);
  2111. }
  2112. if (j >= count)
  2113. continue;
  2114. mutex_lock(&nd_mapping->lock);
  2115. nd_mapping_free_labels(nd_mapping);
  2116. mutex_unlock(&nd_mapping->lock);
  2117. return -ENOMEM;
  2118. }
  2119. return 0;
  2120. }
  2121. int nd_region_register_namespaces(struct nd_region *nd_region, int *err)
  2122. {
  2123. struct device **devs = NULL;
  2124. int i, rc = 0, type;
  2125. *err = 0;
  2126. nvdimm_bus_lock(&nd_region->dev);
  2127. rc = init_active_labels(nd_region);
  2128. if (rc) {
  2129. nvdimm_bus_unlock(&nd_region->dev);
  2130. return rc;
  2131. }
  2132. type = nd_region_to_nstype(nd_region);
  2133. switch (type) {
  2134. case ND_DEVICE_NAMESPACE_IO:
  2135. devs = create_namespace_io(nd_region);
  2136. break;
  2137. case ND_DEVICE_NAMESPACE_PMEM:
  2138. case ND_DEVICE_NAMESPACE_BLK:
  2139. devs = create_namespaces(nd_region);
  2140. break;
  2141. default:
  2142. break;
  2143. }
  2144. nvdimm_bus_unlock(&nd_region->dev);
  2145. if (!devs)
  2146. return -ENODEV;
  2147. for (i = 0; devs[i]; i++) {
  2148. struct device *dev = devs[i];
  2149. int id;
  2150. if (type == ND_DEVICE_NAMESPACE_BLK) {
  2151. struct nd_namespace_blk *nsblk;
  2152. nsblk = to_nd_namespace_blk(dev);
  2153. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  2154. GFP_KERNEL);
  2155. nsblk->id = id;
  2156. } else if (type == ND_DEVICE_NAMESPACE_PMEM) {
  2157. struct nd_namespace_pmem *nspm;
  2158. nspm = to_nd_namespace_pmem(dev);
  2159. id = ida_simple_get(&nd_region->ns_ida, 0, 0,
  2160. GFP_KERNEL);
  2161. nspm->id = id;
  2162. } else
  2163. id = i;
  2164. if (id < 0)
  2165. break;
  2166. dev_set_name(dev, "namespace%d.%d", nd_region->id, id);
  2167. dev->groups = nd_namespace_attribute_groups;
  2168. nd_device_register(dev);
  2169. }
  2170. if (i)
  2171. nd_region->ns_seed = devs[0];
  2172. if (devs[i]) {
  2173. int j;
  2174. for (j = i; devs[j]; j++) {
  2175. struct device *dev = devs[j];
  2176. device_initialize(dev);
  2177. put_device(dev);
  2178. }
  2179. *err = j - i;
  2180. /*
  2181. * All of the namespaces we tried to register failed, so
  2182. * fail region activation.
  2183. */
  2184. if (*err == 0)
  2185. rc = -ENODEV;
  2186. }
  2187. kfree(devs);
  2188. if (rc == -ENODEV)
  2189. return rc;
  2190. return i;
  2191. }