namespace_devs.c 59 KB

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