namespace_devs.c 55 KB

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