namespace_devs.c 59 KB

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