namespace_devs.c 50 KB

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