namespace_devs.c 50 KB

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