namespace_devs.c 67 KB

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