configfs.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Configfs interface for the NVMe target.
  4. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/stat.h>
  11. #include <linux/ctype.h>
  12. #include <linux/pci.h>
  13. #include <linux/pci-p2pdma.h>
  14. #include "nvmet.h"
  15. static const struct config_item_type nvmet_host_type;
  16. static const struct config_item_type nvmet_subsys_type;
  17. static LIST_HEAD(nvmet_ports_list);
  18. struct list_head *nvmet_ports = &nvmet_ports_list;
  19. struct nvmet_type_name_map {
  20. u8 type;
  21. const char *name;
  22. };
  23. static struct nvmet_type_name_map nvmet_transport[] = {
  24. { NVMF_TRTYPE_RDMA, "rdma" },
  25. { NVMF_TRTYPE_FC, "fc" },
  26. { NVMF_TRTYPE_TCP, "tcp" },
  27. { NVMF_TRTYPE_LOOP, "loop" },
  28. };
  29. static const struct nvmet_type_name_map nvmet_addr_family[] = {
  30. { NVMF_ADDR_FAMILY_PCI, "pcie" },
  31. { NVMF_ADDR_FAMILY_IP4, "ipv4" },
  32. { NVMF_ADDR_FAMILY_IP6, "ipv6" },
  33. { NVMF_ADDR_FAMILY_IB, "ib" },
  34. { NVMF_ADDR_FAMILY_FC, "fc" },
  35. { NVMF_ADDR_FAMILY_LOOP, "loop" },
  36. };
  37. static bool nvmet_is_port_enabled(struct nvmet_port *p, const char *caller)
  38. {
  39. if (p->enabled)
  40. pr_err("Disable port '%u' before changing attribute in %s\n",
  41. le16_to_cpu(p->disc_addr.portid), caller);
  42. return p->enabled;
  43. }
  44. /*
  45. * nvmet_port Generic ConfigFS definitions.
  46. * Used in any place in the ConfigFS tree that refers to an address.
  47. */
  48. static ssize_t nvmet_addr_adrfam_show(struct config_item *item, char *page)
  49. {
  50. u8 adrfam = to_nvmet_port(item)->disc_addr.adrfam;
  51. int i;
  52. for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
  53. if (nvmet_addr_family[i].type == adrfam)
  54. return sprintf(page, "%s\n", nvmet_addr_family[i].name);
  55. }
  56. return sprintf(page, "\n");
  57. }
  58. static ssize_t nvmet_addr_adrfam_store(struct config_item *item,
  59. const char *page, size_t count)
  60. {
  61. struct nvmet_port *port = to_nvmet_port(item);
  62. int i;
  63. if (nvmet_is_port_enabled(port, __func__))
  64. return -EACCES;
  65. for (i = 1; i < ARRAY_SIZE(nvmet_addr_family); i++) {
  66. if (sysfs_streq(page, nvmet_addr_family[i].name))
  67. goto found;
  68. }
  69. pr_err("Invalid value '%s' for adrfam\n", page);
  70. return -EINVAL;
  71. found:
  72. port->disc_addr.adrfam = nvmet_addr_family[i].type;
  73. return count;
  74. }
  75. CONFIGFS_ATTR(nvmet_, addr_adrfam);
  76. static ssize_t nvmet_addr_portid_show(struct config_item *item,
  77. char *page)
  78. {
  79. struct nvmet_port *port = to_nvmet_port(item);
  80. return snprintf(page, PAGE_SIZE, "%d\n",
  81. le16_to_cpu(port->disc_addr.portid));
  82. }
  83. static ssize_t nvmet_addr_portid_store(struct config_item *item,
  84. const char *page, size_t count)
  85. {
  86. struct nvmet_port *port = to_nvmet_port(item);
  87. u16 portid = 0;
  88. if (kstrtou16(page, 0, &portid)) {
  89. pr_err("Invalid value '%s' for portid\n", page);
  90. return -EINVAL;
  91. }
  92. if (nvmet_is_port_enabled(port, __func__))
  93. return -EACCES;
  94. port->disc_addr.portid = cpu_to_le16(portid);
  95. return count;
  96. }
  97. CONFIGFS_ATTR(nvmet_, addr_portid);
  98. static ssize_t nvmet_addr_traddr_show(struct config_item *item,
  99. char *page)
  100. {
  101. struct nvmet_port *port = to_nvmet_port(item);
  102. return snprintf(page, PAGE_SIZE, "%s\n",
  103. port->disc_addr.traddr);
  104. }
  105. static ssize_t nvmet_addr_traddr_store(struct config_item *item,
  106. const char *page, size_t count)
  107. {
  108. struct nvmet_port *port = to_nvmet_port(item);
  109. if (count > NVMF_TRADDR_SIZE) {
  110. pr_err("Invalid value '%s' for traddr\n", page);
  111. return -EINVAL;
  112. }
  113. if (nvmet_is_port_enabled(port, __func__))
  114. return -EACCES;
  115. if (sscanf(page, "%s\n", port->disc_addr.traddr) != 1)
  116. return -EINVAL;
  117. return count;
  118. }
  119. CONFIGFS_ATTR(nvmet_, addr_traddr);
  120. static const struct nvmet_type_name_map nvmet_addr_treq[] = {
  121. { NVMF_TREQ_NOT_SPECIFIED, "not specified" },
  122. { NVMF_TREQ_REQUIRED, "required" },
  123. { NVMF_TREQ_NOT_REQUIRED, "not required" },
  124. };
  125. static ssize_t nvmet_addr_treq_show(struct config_item *item, char *page)
  126. {
  127. u8 treq = to_nvmet_port(item)->disc_addr.treq &
  128. NVME_TREQ_SECURE_CHANNEL_MASK;
  129. int i;
  130. for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
  131. if (treq == nvmet_addr_treq[i].type)
  132. return sprintf(page, "%s\n", nvmet_addr_treq[i].name);
  133. }
  134. return sprintf(page, "\n");
  135. }
  136. static ssize_t nvmet_addr_treq_store(struct config_item *item,
  137. const char *page, size_t count)
  138. {
  139. struct nvmet_port *port = to_nvmet_port(item);
  140. u8 treq = port->disc_addr.treq & ~NVME_TREQ_SECURE_CHANNEL_MASK;
  141. int i;
  142. if (nvmet_is_port_enabled(port, __func__))
  143. return -EACCES;
  144. for (i = 0; i < ARRAY_SIZE(nvmet_addr_treq); i++) {
  145. if (sysfs_streq(page, nvmet_addr_treq[i].name))
  146. goto found;
  147. }
  148. pr_err("Invalid value '%s' for treq\n", page);
  149. return -EINVAL;
  150. found:
  151. treq |= nvmet_addr_treq[i].type;
  152. port->disc_addr.treq = treq;
  153. return count;
  154. }
  155. CONFIGFS_ATTR(nvmet_, addr_treq);
  156. static ssize_t nvmet_addr_trsvcid_show(struct config_item *item,
  157. char *page)
  158. {
  159. struct nvmet_port *port = to_nvmet_port(item);
  160. return snprintf(page, PAGE_SIZE, "%s\n",
  161. port->disc_addr.trsvcid);
  162. }
  163. static ssize_t nvmet_addr_trsvcid_store(struct config_item *item,
  164. const char *page, size_t count)
  165. {
  166. struct nvmet_port *port = to_nvmet_port(item);
  167. if (count > NVMF_TRSVCID_SIZE) {
  168. pr_err("Invalid value '%s' for trsvcid\n", page);
  169. return -EINVAL;
  170. }
  171. if (nvmet_is_port_enabled(port, __func__))
  172. return -EACCES;
  173. if (sscanf(page, "%s\n", port->disc_addr.trsvcid) != 1)
  174. return -EINVAL;
  175. return count;
  176. }
  177. CONFIGFS_ATTR(nvmet_, addr_trsvcid);
  178. static ssize_t nvmet_param_inline_data_size_show(struct config_item *item,
  179. char *page)
  180. {
  181. struct nvmet_port *port = to_nvmet_port(item);
  182. return snprintf(page, PAGE_SIZE, "%d\n", port->inline_data_size);
  183. }
  184. static ssize_t nvmet_param_inline_data_size_store(struct config_item *item,
  185. const char *page, size_t count)
  186. {
  187. struct nvmet_port *port = to_nvmet_port(item);
  188. int ret;
  189. if (nvmet_is_port_enabled(port, __func__))
  190. return -EACCES;
  191. ret = kstrtoint(page, 0, &port->inline_data_size);
  192. if (ret) {
  193. pr_err("Invalid value '%s' for inline_data_size\n", page);
  194. return -EINVAL;
  195. }
  196. return count;
  197. }
  198. CONFIGFS_ATTR(nvmet_, param_inline_data_size);
  199. #ifdef CONFIG_BLK_DEV_INTEGRITY
  200. static ssize_t nvmet_param_pi_enable_show(struct config_item *item,
  201. char *page)
  202. {
  203. struct nvmet_port *port = to_nvmet_port(item);
  204. return snprintf(page, PAGE_SIZE, "%d\n", port->pi_enable);
  205. }
  206. static ssize_t nvmet_param_pi_enable_store(struct config_item *item,
  207. const char *page, size_t count)
  208. {
  209. struct nvmet_port *port = to_nvmet_port(item);
  210. bool val;
  211. if (strtobool(page, &val))
  212. return -EINVAL;
  213. if (port->enabled) {
  214. pr_err("Disable port before setting pi_enable value.\n");
  215. return -EACCES;
  216. }
  217. port->pi_enable = val;
  218. return count;
  219. }
  220. CONFIGFS_ATTR(nvmet_, param_pi_enable);
  221. #endif
  222. static ssize_t nvmet_addr_trtype_show(struct config_item *item,
  223. char *page)
  224. {
  225. struct nvmet_port *port = to_nvmet_port(item);
  226. int i;
  227. for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
  228. if (port->disc_addr.trtype == nvmet_transport[i].type)
  229. return sprintf(page, "%s\n", nvmet_transport[i].name);
  230. }
  231. return sprintf(page, "\n");
  232. }
  233. static void nvmet_port_init_tsas_rdma(struct nvmet_port *port)
  234. {
  235. port->disc_addr.tsas.rdma.qptype = NVMF_RDMA_QPTYPE_CONNECTED;
  236. port->disc_addr.tsas.rdma.prtype = NVMF_RDMA_PRTYPE_NOT_SPECIFIED;
  237. port->disc_addr.tsas.rdma.cms = NVMF_RDMA_CMS_RDMA_CM;
  238. }
  239. static ssize_t nvmet_addr_trtype_store(struct config_item *item,
  240. const char *page, size_t count)
  241. {
  242. struct nvmet_port *port = to_nvmet_port(item);
  243. int i;
  244. if (nvmet_is_port_enabled(port, __func__))
  245. return -EACCES;
  246. for (i = 0; i < ARRAY_SIZE(nvmet_transport); i++) {
  247. if (sysfs_streq(page, nvmet_transport[i].name))
  248. goto found;
  249. }
  250. pr_err("Invalid value '%s' for trtype\n", page);
  251. return -EINVAL;
  252. found:
  253. memset(&port->disc_addr.tsas, 0, NVMF_TSAS_SIZE);
  254. port->disc_addr.trtype = nvmet_transport[i].type;
  255. if (port->disc_addr.trtype == NVMF_TRTYPE_RDMA)
  256. nvmet_port_init_tsas_rdma(port);
  257. return count;
  258. }
  259. CONFIGFS_ATTR(nvmet_, addr_trtype);
  260. /*
  261. * Namespace structures & file operation functions below
  262. */
  263. static ssize_t nvmet_ns_device_path_show(struct config_item *item, char *page)
  264. {
  265. return sprintf(page, "%s\n", to_nvmet_ns(item)->device_path);
  266. }
  267. static ssize_t nvmet_ns_device_path_store(struct config_item *item,
  268. const char *page, size_t count)
  269. {
  270. struct nvmet_ns *ns = to_nvmet_ns(item);
  271. struct nvmet_subsys *subsys = ns->subsys;
  272. size_t len;
  273. int ret;
  274. mutex_lock(&subsys->lock);
  275. ret = -EBUSY;
  276. if (ns->enabled)
  277. goto out_unlock;
  278. ret = -EINVAL;
  279. len = strcspn(page, "\n");
  280. if (!len)
  281. goto out_unlock;
  282. kfree(ns->device_path);
  283. ret = -ENOMEM;
  284. ns->device_path = kmemdup_nul(page, len, GFP_KERNEL);
  285. if (!ns->device_path)
  286. goto out_unlock;
  287. mutex_unlock(&subsys->lock);
  288. return count;
  289. out_unlock:
  290. mutex_unlock(&subsys->lock);
  291. return ret;
  292. }
  293. CONFIGFS_ATTR(nvmet_ns_, device_path);
  294. #ifdef CONFIG_PCI_P2PDMA
  295. static ssize_t nvmet_ns_p2pmem_show(struct config_item *item, char *page)
  296. {
  297. struct nvmet_ns *ns = to_nvmet_ns(item);
  298. return pci_p2pdma_enable_show(page, ns->p2p_dev, ns->use_p2pmem);
  299. }
  300. static ssize_t nvmet_ns_p2pmem_store(struct config_item *item,
  301. const char *page, size_t count)
  302. {
  303. struct nvmet_ns *ns = to_nvmet_ns(item);
  304. struct pci_dev *p2p_dev = NULL;
  305. bool use_p2pmem;
  306. int ret = count;
  307. int error;
  308. mutex_lock(&ns->subsys->lock);
  309. if (ns->enabled) {
  310. ret = -EBUSY;
  311. goto out_unlock;
  312. }
  313. error = pci_p2pdma_enable_store(page, &p2p_dev, &use_p2pmem);
  314. if (error) {
  315. ret = error;
  316. goto out_unlock;
  317. }
  318. ns->use_p2pmem = use_p2pmem;
  319. pci_dev_put(ns->p2p_dev);
  320. ns->p2p_dev = p2p_dev;
  321. out_unlock:
  322. mutex_unlock(&ns->subsys->lock);
  323. return ret;
  324. }
  325. CONFIGFS_ATTR(nvmet_ns_, p2pmem);
  326. #endif /* CONFIG_PCI_P2PDMA */
  327. static ssize_t nvmet_ns_device_uuid_show(struct config_item *item, char *page)
  328. {
  329. return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->uuid);
  330. }
  331. static ssize_t nvmet_ns_device_uuid_store(struct config_item *item,
  332. const char *page, size_t count)
  333. {
  334. struct nvmet_ns *ns = to_nvmet_ns(item);
  335. struct nvmet_subsys *subsys = ns->subsys;
  336. int ret = 0;
  337. mutex_lock(&subsys->lock);
  338. if (ns->enabled) {
  339. ret = -EBUSY;
  340. goto out_unlock;
  341. }
  342. if (uuid_parse(page, &ns->uuid))
  343. ret = -EINVAL;
  344. out_unlock:
  345. mutex_unlock(&subsys->lock);
  346. return ret ? ret : count;
  347. }
  348. CONFIGFS_ATTR(nvmet_ns_, device_uuid);
  349. static ssize_t nvmet_ns_device_nguid_show(struct config_item *item, char *page)
  350. {
  351. return sprintf(page, "%pUb\n", &to_nvmet_ns(item)->nguid);
  352. }
  353. static ssize_t nvmet_ns_device_nguid_store(struct config_item *item,
  354. const char *page, size_t count)
  355. {
  356. struct nvmet_ns *ns = to_nvmet_ns(item);
  357. struct nvmet_subsys *subsys = ns->subsys;
  358. u8 nguid[16];
  359. const char *p = page;
  360. int i;
  361. int ret = 0;
  362. mutex_lock(&subsys->lock);
  363. if (ns->enabled) {
  364. ret = -EBUSY;
  365. goto out_unlock;
  366. }
  367. for (i = 0; i < 16; i++) {
  368. if (p + 2 > page + count) {
  369. ret = -EINVAL;
  370. goto out_unlock;
  371. }
  372. if (!isxdigit(p[0]) || !isxdigit(p[1])) {
  373. ret = -EINVAL;
  374. goto out_unlock;
  375. }
  376. nguid[i] = (hex_to_bin(p[0]) << 4) | hex_to_bin(p[1]);
  377. p += 2;
  378. if (*p == '-' || *p == ':')
  379. p++;
  380. }
  381. memcpy(&ns->nguid, nguid, sizeof(nguid));
  382. out_unlock:
  383. mutex_unlock(&subsys->lock);
  384. return ret ? ret : count;
  385. }
  386. CONFIGFS_ATTR(nvmet_ns_, device_nguid);
  387. static ssize_t nvmet_ns_ana_grpid_show(struct config_item *item, char *page)
  388. {
  389. return sprintf(page, "%u\n", to_nvmet_ns(item)->anagrpid);
  390. }
  391. static ssize_t nvmet_ns_ana_grpid_store(struct config_item *item,
  392. const char *page, size_t count)
  393. {
  394. struct nvmet_ns *ns = to_nvmet_ns(item);
  395. u32 oldgrpid, newgrpid;
  396. int ret;
  397. ret = kstrtou32(page, 0, &newgrpid);
  398. if (ret)
  399. return ret;
  400. if (newgrpid < 1 || newgrpid > NVMET_MAX_ANAGRPS)
  401. return -EINVAL;
  402. down_write(&nvmet_ana_sem);
  403. oldgrpid = ns->anagrpid;
  404. nvmet_ana_group_enabled[newgrpid]++;
  405. ns->anagrpid = newgrpid;
  406. nvmet_ana_group_enabled[oldgrpid]--;
  407. nvmet_ana_chgcnt++;
  408. up_write(&nvmet_ana_sem);
  409. nvmet_send_ana_event(ns->subsys, NULL);
  410. return count;
  411. }
  412. CONFIGFS_ATTR(nvmet_ns_, ana_grpid);
  413. static ssize_t nvmet_ns_enable_show(struct config_item *item, char *page)
  414. {
  415. return sprintf(page, "%d\n", to_nvmet_ns(item)->enabled);
  416. }
  417. static ssize_t nvmet_ns_enable_store(struct config_item *item,
  418. const char *page, size_t count)
  419. {
  420. struct nvmet_ns *ns = to_nvmet_ns(item);
  421. bool enable;
  422. int ret = 0;
  423. if (strtobool(page, &enable))
  424. return -EINVAL;
  425. if (enable)
  426. ret = nvmet_ns_enable(ns);
  427. else
  428. nvmet_ns_disable(ns);
  429. return ret ? ret : count;
  430. }
  431. CONFIGFS_ATTR(nvmet_ns_, enable);
  432. static ssize_t nvmet_ns_buffered_io_show(struct config_item *item, char *page)
  433. {
  434. return sprintf(page, "%d\n", to_nvmet_ns(item)->buffered_io);
  435. }
  436. static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
  437. const char *page, size_t count)
  438. {
  439. struct nvmet_ns *ns = to_nvmet_ns(item);
  440. bool val;
  441. if (strtobool(page, &val))
  442. return -EINVAL;
  443. mutex_lock(&ns->subsys->lock);
  444. if (ns->enabled) {
  445. pr_err("disable ns before setting buffered_io value.\n");
  446. mutex_unlock(&ns->subsys->lock);
  447. return -EINVAL;
  448. }
  449. ns->buffered_io = val;
  450. mutex_unlock(&ns->subsys->lock);
  451. return count;
  452. }
  453. CONFIGFS_ATTR(nvmet_ns_, buffered_io);
  454. static ssize_t nvmet_ns_revalidate_size_store(struct config_item *item,
  455. const char *page, size_t count)
  456. {
  457. struct nvmet_ns *ns = to_nvmet_ns(item);
  458. bool val;
  459. if (strtobool(page, &val))
  460. return -EINVAL;
  461. if (!val)
  462. return -EINVAL;
  463. mutex_lock(&ns->subsys->lock);
  464. if (!ns->enabled) {
  465. pr_err("enable ns before revalidate.\n");
  466. mutex_unlock(&ns->subsys->lock);
  467. return -EINVAL;
  468. }
  469. nvmet_ns_revalidate(ns);
  470. mutex_unlock(&ns->subsys->lock);
  471. return count;
  472. }
  473. CONFIGFS_ATTR_WO(nvmet_ns_, revalidate_size);
  474. static struct configfs_attribute *nvmet_ns_attrs[] = {
  475. &nvmet_ns_attr_device_path,
  476. &nvmet_ns_attr_device_nguid,
  477. &nvmet_ns_attr_device_uuid,
  478. &nvmet_ns_attr_ana_grpid,
  479. &nvmet_ns_attr_enable,
  480. &nvmet_ns_attr_buffered_io,
  481. &nvmet_ns_attr_revalidate_size,
  482. #ifdef CONFIG_PCI_P2PDMA
  483. &nvmet_ns_attr_p2pmem,
  484. #endif
  485. NULL,
  486. };
  487. static void nvmet_ns_release(struct config_item *item)
  488. {
  489. struct nvmet_ns *ns = to_nvmet_ns(item);
  490. nvmet_ns_free(ns);
  491. }
  492. static struct configfs_item_operations nvmet_ns_item_ops = {
  493. .release = nvmet_ns_release,
  494. };
  495. static const struct config_item_type nvmet_ns_type = {
  496. .ct_item_ops = &nvmet_ns_item_ops,
  497. .ct_attrs = nvmet_ns_attrs,
  498. .ct_owner = THIS_MODULE,
  499. };
  500. static struct config_group *nvmet_ns_make(struct config_group *group,
  501. const char *name)
  502. {
  503. struct nvmet_subsys *subsys = namespaces_to_subsys(&group->cg_item);
  504. struct nvmet_ns *ns;
  505. int ret;
  506. u32 nsid;
  507. ret = kstrtou32(name, 0, &nsid);
  508. if (ret)
  509. goto out;
  510. ret = -EINVAL;
  511. if (nsid == 0 || nsid == NVME_NSID_ALL) {
  512. pr_err("invalid nsid %#x", nsid);
  513. goto out;
  514. }
  515. ret = -ENOMEM;
  516. ns = nvmet_ns_alloc(subsys, nsid);
  517. if (!ns)
  518. goto out;
  519. config_group_init_type_name(&ns->group, name, &nvmet_ns_type);
  520. pr_info("adding nsid %d to subsystem %s\n", nsid, subsys->subsysnqn);
  521. return &ns->group;
  522. out:
  523. return ERR_PTR(ret);
  524. }
  525. static struct configfs_group_operations nvmet_namespaces_group_ops = {
  526. .make_group = nvmet_ns_make,
  527. };
  528. static const struct config_item_type nvmet_namespaces_type = {
  529. .ct_group_ops = &nvmet_namespaces_group_ops,
  530. .ct_owner = THIS_MODULE,
  531. };
  532. #ifdef CONFIG_NVME_TARGET_PASSTHRU
  533. static ssize_t nvmet_passthru_device_path_show(struct config_item *item,
  534. char *page)
  535. {
  536. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  537. return snprintf(page, PAGE_SIZE, "%s\n", subsys->passthru_ctrl_path);
  538. }
  539. static ssize_t nvmet_passthru_device_path_store(struct config_item *item,
  540. const char *page, size_t count)
  541. {
  542. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  543. size_t len;
  544. int ret;
  545. mutex_lock(&subsys->lock);
  546. ret = -EBUSY;
  547. if (subsys->passthru_ctrl)
  548. goto out_unlock;
  549. ret = -EINVAL;
  550. len = strcspn(page, "\n");
  551. if (!len)
  552. goto out_unlock;
  553. kfree(subsys->passthru_ctrl_path);
  554. ret = -ENOMEM;
  555. subsys->passthru_ctrl_path = kstrndup(page, len, GFP_KERNEL);
  556. if (!subsys->passthru_ctrl_path)
  557. goto out_unlock;
  558. mutex_unlock(&subsys->lock);
  559. return count;
  560. out_unlock:
  561. mutex_unlock(&subsys->lock);
  562. return ret;
  563. }
  564. CONFIGFS_ATTR(nvmet_passthru_, device_path);
  565. static ssize_t nvmet_passthru_enable_show(struct config_item *item,
  566. char *page)
  567. {
  568. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  569. return sprintf(page, "%d\n", subsys->passthru_ctrl ? 1 : 0);
  570. }
  571. static ssize_t nvmet_passthru_enable_store(struct config_item *item,
  572. const char *page, size_t count)
  573. {
  574. struct nvmet_subsys *subsys = to_subsys(item->ci_parent);
  575. bool enable;
  576. int ret = 0;
  577. if (strtobool(page, &enable))
  578. return -EINVAL;
  579. if (enable)
  580. ret = nvmet_passthru_ctrl_enable(subsys);
  581. else
  582. nvmet_passthru_ctrl_disable(subsys);
  583. return ret ? ret : count;
  584. }
  585. CONFIGFS_ATTR(nvmet_passthru_, enable);
  586. static struct configfs_attribute *nvmet_passthru_attrs[] = {
  587. &nvmet_passthru_attr_device_path,
  588. &nvmet_passthru_attr_enable,
  589. NULL,
  590. };
  591. static const struct config_item_type nvmet_passthru_type = {
  592. .ct_attrs = nvmet_passthru_attrs,
  593. .ct_owner = THIS_MODULE,
  594. };
  595. static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
  596. {
  597. config_group_init_type_name(&subsys->passthru_group,
  598. "passthru", &nvmet_passthru_type);
  599. configfs_add_default_group(&subsys->passthru_group,
  600. &subsys->group);
  601. }
  602. #else /* CONFIG_NVME_TARGET_PASSTHRU */
  603. static void nvmet_add_passthru_group(struct nvmet_subsys *subsys)
  604. {
  605. }
  606. #endif /* CONFIG_NVME_TARGET_PASSTHRU */
  607. static int nvmet_port_subsys_allow_link(struct config_item *parent,
  608. struct config_item *target)
  609. {
  610. struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
  611. struct nvmet_subsys *subsys;
  612. struct nvmet_subsys_link *link, *p;
  613. int ret;
  614. if (target->ci_type != &nvmet_subsys_type) {
  615. pr_err("can only link subsystems into the subsystems dir.!\n");
  616. return -EINVAL;
  617. }
  618. subsys = to_subsys(target);
  619. link = kmalloc(sizeof(*link), GFP_KERNEL);
  620. if (!link)
  621. return -ENOMEM;
  622. link->subsys = subsys;
  623. down_write(&nvmet_config_sem);
  624. ret = -EEXIST;
  625. list_for_each_entry(p, &port->subsystems, entry) {
  626. if (p->subsys == subsys)
  627. goto out_free_link;
  628. }
  629. if (list_empty(&port->subsystems)) {
  630. ret = nvmet_enable_port(port);
  631. if (ret)
  632. goto out_free_link;
  633. }
  634. list_add_tail(&link->entry, &port->subsystems);
  635. nvmet_port_disc_changed(port, subsys);
  636. up_write(&nvmet_config_sem);
  637. return 0;
  638. out_free_link:
  639. up_write(&nvmet_config_sem);
  640. kfree(link);
  641. return ret;
  642. }
  643. static void nvmet_port_subsys_drop_link(struct config_item *parent,
  644. struct config_item *target)
  645. {
  646. struct nvmet_port *port = to_nvmet_port(parent->ci_parent);
  647. struct nvmet_subsys *subsys = to_subsys(target);
  648. struct nvmet_subsys_link *p;
  649. down_write(&nvmet_config_sem);
  650. list_for_each_entry(p, &port->subsystems, entry) {
  651. if (p->subsys == subsys)
  652. goto found;
  653. }
  654. up_write(&nvmet_config_sem);
  655. return;
  656. found:
  657. list_del(&p->entry);
  658. nvmet_port_del_ctrls(port, subsys);
  659. nvmet_port_disc_changed(port, subsys);
  660. if (list_empty(&port->subsystems))
  661. nvmet_disable_port(port);
  662. up_write(&nvmet_config_sem);
  663. kfree(p);
  664. }
  665. static struct configfs_item_operations nvmet_port_subsys_item_ops = {
  666. .allow_link = nvmet_port_subsys_allow_link,
  667. .drop_link = nvmet_port_subsys_drop_link,
  668. };
  669. static const struct config_item_type nvmet_port_subsys_type = {
  670. .ct_item_ops = &nvmet_port_subsys_item_ops,
  671. .ct_owner = THIS_MODULE,
  672. };
  673. static int nvmet_allowed_hosts_allow_link(struct config_item *parent,
  674. struct config_item *target)
  675. {
  676. struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
  677. struct nvmet_host *host;
  678. struct nvmet_host_link *link, *p;
  679. int ret;
  680. if (target->ci_type != &nvmet_host_type) {
  681. pr_err("can only link hosts into the allowed_hosts directory!\n");
  682. return -EINVAL;
  683. }
  684. host = to_host(target);
  685. link = kmalloc(sizeof(*link), GFP_KERNEL);
  686. if (!link)
  687. return -ENOMEM;
  688. link->host = host;
  689. down_write(&nvmet_config_sem);
  690. ret = -EINVAL;
  691. if (subsys->allow_any_host) {
  692. pr_err("can't add hosts when allow_any_host is set!\n");
  693. goto out_free_link;
  694. }
  695. ret = -EEXIST;
  696. list_for_each_entry(p, &subsys->hosts, entry) {
  697. if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
  698. goto out_free_link;
  699. }
  700. list_add_tail(&link->entry, &subsys->hosts);
  701. nvmet_subsys_disc_changed(subsys, host);
  702. up_write(&nvmet_config_sem);
  703. return 0;
  704. out_free_link:
  705. up_write(&nvmet_config_sem);
  706. kfree(link);
  707. return ret;
  708. }
  709. static void nvmet_allowed_hosts_drop_link(struct config_item *parent,
  710. struct config_item *target)
  711. {
  712. struct nvmet_subsys *subsys = to_subsys(parent->ci_parent);
  713. struct nvmet_host *host = to_host(target);
  714. struct nvmet_host_link *p;
  715. down_write(&nvmet_config_sem);
  716. list_for_each_entry(p, &subsys->hosts, entry) {
  717. if (!strcmp(nvmet_host_name(p->host), nvmet_host_name(host)))
  718. goto found;
  719. }
  720. up_write(&nvmet_config_sem);
  721. return;
  722. found:
  723. list_del(&p->entry);
  724. nvmet_subsys_disc_changed(subsys, host);
  725. up_write(&nvmet_config_sem);
  726. kfree(p);
  727. }
  728. static struct configfs_item_operations nvmet_allowed_hosts_item_ops = {
  729. .allow_link = nvmet_allowed_hosts_allow_link,
  730. .drop_link = nvmet_allowed_hosts_drop_link,
  731. };
  732. static const struct config_item_type nvmet_allowed_hosts_type = {
  733. .ct_item_ops = &nvmet_allowed_hosts_item_ops,
  734. .ct_owner = THIS_MODULE,
  735. };
  736. static ssize_t nvmet_subsys_attr_allow_any_host_show(struct config_item *item,
  737. char *page)
  738. {
  739. return snprintf(page, PAGE_SIZE, "%d\n",
  740. to_subsys(item)->allow_any_host);
  741. }
  742. static ssize_t nvmet_subsys_attr_allow_any_host_store(struct config_item *item,
  743. const char *page, size_t count)
  744. {
  745. struct nvmet_subsys *subsys = to_subsys(item);
  746. bool allow_any_host;
  747. int ret = 0;
  748. if (strtobool(page, &allow_any_host))
  749. return -EINVAL;
  750. down_write(&nvmet_config_sem);
  751. if (allow_any_host && !list_empty(&subsys->hosts)) {
  752. pr_err("Can't set allow_any_host when explicit hosts are set!\n");
  753. ret = -EINVAL;
  754. goto out_unlock;
  755. }
  756. if (subsys->allow_any_host != allow_any_host) {
  757. subsys->allow_any_host = allow_any_host;
  758. nvmet_subsys_disc_changed(subsys, NULL);
  759. }
  760. out_unlock:
  761. up_write(&nvmet_config_sem);
  762. return ret ? ret : count;
  763. }
  764. CONFIGFS_ATTR(nvmet_subsys_, attr_allow_any_host);
  765. static ssize_t nvmet_subsys_attr_version_show(struct config_item *item,
  766. char *page)
  767. {
  768. struct nvmet_subsys *subsys = to_subsys(item);
  769. if (NVME_TERTIARY(subsys->ver))
  770. return snprintf(page, PAGE_SIZE, "%llu.%llu.%llu\n",
  771. NVME_MAJOR(subsys->ver),
  772. NVME_MINOR(subsys->ver),
  773. NVME_TERTIARY(subsys->ver));
  774. return snprintf(page, PAGE_SIZE, "%llu.%llu\n",
  775. NVME_MAJOR(subsys->ver),
  776. NVME_MINOR(subsys->ver));
  777. }
  778. static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
  779. const char *page, size_t count)
  780. {
  781. struct nvmet_subsys *subsys = to_subsys(item);
  782. int major, minor, tertiary = 0;
  783. int ret;
  784. /* passthru subsystems use the underlying controller's version */
  785. if (nvmet_passthru_ctrl(subsys))
  786. return -EINVAL;
  787. ret = sscanf(page, "%d.%d.%d\n", &major, &minor, &tertiary);
  788. if (ret != 2 && ret != 3)
  789. return -EINVAL;
  790. down_write(&nvmet_config_sem);
  791. subsys->ver = NVME_VS(major, minor, tertiary);
  792. up_write(&nvmet_config_sem);
  793. return count;
  794. }
  795. CONFIGFS_ATTR(nvmet_subsys_, attr_version);
  796. static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
  797. char *page)
  798. {
  799. struct nvmet_subsys *subsys = to_subsys(item);
  800. return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
  801. }
  802. static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
  803. const char *page, size_t count)
  804. {
  805. u64 serial;
  806. if (sscanf(page, "%llx\n", &serial) != 1)
  807. return -EINVAL;
  808. down_write(&nvmet_config_sem);
  809. to_subsys(item)->serial = serial;
  810. up_write(&nvmet_config_sem);
  811. return count;
  812. }
  813. CONFIGFS_ATTR(nvmet_subsys_, attr_serial);
  814. static ssize_t nvmet_subsys_attr_cntlid_min_show(struct config_item *item,
  815. char *page)
  816. {
  817. return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_min);
  818. }
  819. static ssize_t nvmet_subsys_attr_cntlid_min_store(struct config_item *item,
  820. const char *page, size_t cnt)
  821. {
  822. u16 cntlid_min;
  823. if (sscanf(page, "%hu\n", &cntlid_min) != 1)
  824. return -EINVAL;
  825. if (cntlid_min == 0)
  826. return -EINVAL;
  827. down_write(&nvmet_config_sem);
  828. if (cntlid_min >= to_subsys(item)->cntlid_max)
  829. goto out_unlock;
  830. to_subsys(item)->cntlid_min = cntlid_min;
  831. up_write(&nvmet_config_sem);
  832. return cnt;
  833. out_unlock:
  834. up_write(&nvmet_config_sem);
  835. return -EINVAL;
  836. }
  837. CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_min);
  838. static ssize_t nvmet_subsys_attr_cntlid_max_show(struct config_item *item,
  839. char *page)
  840. {
  841. return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cntlid_max);
  842. }
  843. static ssize_t nvmet_subsys_attr_cntlid_max_store(struct config_item *item,
  844. const char *page, size_t cnt)
  845. {
  846. u16 cntlid_max;
  847. if (sscanf(page, "%hu\n", &cntlid_max) != 1)
  848. return -EINVAL;
  849. if (cntlid_max == 0)
  850. return -EINVAL;
  851. down_write(&nvmet_config_sem);
  852. if (cntlid_max <= to_subsys(item)->cntlid_min)
  853. goto out_unlock;
  854. to_subsys(item)->cntlid_max = cntlid_max;
  855. up_write(&nvmet_config_sem);
  856. return cnt;
  857. out_unlock:
  858. up_write(&nvmet_config_sem);
  859. return -EINVAL;
  860. }
  861. CONFIGFS_ATTR(nvmet_subsys_, attr_cntlid_max);
  862. static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
  863. char *page)
  864. {
  865. struct nvmet_subsys *subsys = to_subsys(item);
  866. struct nvmet_subsys_model *subsys_model;
  867. char *model = NVMET_DEFAULT_CTRL_MODEL;
  868. int ret;
  869. rcu_read_lock();
  870. subsys_model = rcu_dereference(subsys->model);
  871. if (subsys_model)
  872. model = subsys_model->number;
  873. ret = snprintf(page, PAGE_SIZE, "%s\n", model);
  874. rcu_read_unlock();
  875. return ret;
  876. }
  877. /* See Section 1.5 of NVMe 1.4 */
  878. static bool nvmet_is_ascii(const char c)
  879. {
  880. return c >= 0x20 && c <= 0x7e;
  881. }
  882. static ssize_t nvmet_subsys_attr_model_store(struct config_item *item,
  883. const char *page, size_t count)
  884. {
  885. struct nvmet_subsys *subsys = to_subsys(item);
  886. struct nvmet_subsys_model *new_model;
  887. char *new_model_number;
  888. int pos = 0, len;
  889. len = strcspn(page, "\n");
  890. if (!len)
  891. return -EINVAL;
  892. for (pos = 0; pos < len; pos++) {
  893. if (!nvmet_is_ascii(page[pos]))
  894. return -EINVAL;
  895. }
  896. new_model_number = kmemdup_nul(page, len, GFP_KERNEL);
  897. if (!new_model_number)
  898. return -ENOMEM;
  899. new_model = kzalloc(sizeof(*new_model) + len + 1, GFP_KERNEL);
  900. if (!new_model) {
  901. kfree(new_model_number);
  902. return -ENOMEM;
  903. }
  904. memcpy(new_model->number, new_model_number, len);
  905. down_write(&nvmet_config_sem);
  906. mutex_lock(&subsys->lock);
  907. new_model = rcu_replace_pointer(subsys->model, new_model,
  908. mutex_is_locked(&subsys->lock));
  909. mutex_unlock(&subsys->lock);
  910. up_write(&nvmet_config_sem);
  911. kfree_rcu(new_model, rcuhead);
  912. kfree(new_model_number);
  913. return count;
  914. }
  915. CONFIGFS_ATTR(nvmet_subsys_, attr_model);
  916. #ifdef CONFIG_BLK_DEV_INTEGRITY
  917. static ssize_t nvmet_subsys_attr_pi_enable_show(struct config_item *item,
  918. char *page)
  919. {
  920. return snprintf(page, PAGE_SIZE, "%d\n", to_subsys(item)->pi_support);
  921. }
  922. static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
  923. const char *page, size_t count)
  924. {
  925. struct nvmet_subsys *subsys = to_subsys(item);
  926. bool pi_enable;
  927. if (strtobool(page, &pi_enable))
  928. return -EINVAL;
  929. subsys->pi_support = pi_enable;
  930. return count;
  931. }
  932. CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
  933. #endif
  934. static struct configfs_attribute *nvmet_subsys_attrs[] = {
  935. &nvmet_subsys_attr_attr_allow_any_host,
  936. &nvmet_subsys_attr_attr_version,
  937. &nvmet_subsys_attr_attr_serial,
  938. &nvmet_subsys_attr_attr_cntlid_min,
  939. &nvmet_subsys_attr_attr_cntlid_max,
  940. &nvmet_subsys_attr_attr_model,
  941. #ifdef CONFIG_BLK_DEV_INTEGRITY
  942. &nvmet_subsys_attr_attr_pi_enable,
  943. #endif
  944. NULL,
  945. };
  946. /*
  947. * Subsystem structures & folder operation functions below
  948. */
  949. static void nvmet_subsys_release(struct config_item *item)
  950. {
  951. struct nvmet_subsys *subsys = to_subsys(item);
  952. nvmet_subsys_del_ctrls(subsys);
  953. nvmet_subsys_put(subsys);
  954. }
  955. static struct configfs_item_operations nvmet_subsys_item_ops = {
  956. .release = nvmet_subsys_release,
  957. };
  958. static const struct config_item_type nvmet_subsys_type = {
  959. .ct_item_ops = &nvmet_subsys_item_ops,
  960. .ct_attrs = nvmet_subsys_attrs,
  961. .ct_owner = THIS_MODULE,
  962. };
  963. static struct config_group *nvmet_subsys_make(struct config_group *group,
  964. const char *name)
  965. {
  966. struct nvmet_subsys *subsys;
  967. if (sysfs_streq(name, NVME_DISC_SUBSYS_NAME)) {
  968. pr_err("can't create discovery subsystem through configfs\n");
  969. return ERR_PTR(-EINVAL);
  970. }
  971. subsys = nvmet_subsys_alloc(name, NVME_NQN_NVME);
  972. if (IS_ERR(subsys))
  973. return ERR_CAST(subsys);
  974. config_group_init_type_name(&subsys->group, name, &nvmet_subsys_type);
  975. config_group_init_type_name(&subsys->namespaces_group,
  976. "namespaces", &nvmet_namespaces_type);
  977. configfs_add_default_group(&subsys->namespaces_group, &subsys->group);
  978. config_group_init_type_name(&subsys->allowed_hosts_group,
  979. "allowed_hosts", &nvmet_allowed_hosts_type);
  980. configfs_add_default_group(&subsys->allowed_hosts_group,
  981. &subsys->group);
  982. nvmet_add_passthru_group(subsys);
  983. return &subsys->group;
  984. }
  985. static struct configfs_group_operations nvmet_subsystems_group_ops = {
  986. .make_group = nvmet_subsys_make,
  987. };
  988. static const struct config_item_type nvmet_subsystems_type = {
  989. .ct_group_ops = &nvmet_subsystems_group_ops,
  990. .ct_owner = THIS_MODULE,
  991. };
  992. static ssize_t nvmet_referral_enable_show(struct config_item *item,
  993. char *page)
  994. {
  995. return snprintf(page, PAGE_SIZE, "%d\n", to_nvmet_port(item)->enabled);
  996. }
  997. static ssize_t nvmet_referral_enable_store(struct config_item *item,
  998. const char *page, size_t count)
  999. {
  1000. struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
  1001. struct nvmet_port *port = to_nvmet_port(item);
  1002. bool enable;
  1003. if (strtobool(page, &enable))
  1004. goto inval;
  1005. if (enable)
  1006. nvmet_referral_enable(parent, port);
  1007. else
  1008. nvmet_referral_disable(parent, port);
  1009. return count;
  1010. inval:
  1011. pr_err("Invalid value '%s' for enable\n", page);
  1012. return -EINVAL;
  1013. }
  1014. CONFIGFS_ATTR(nvmet_referral_, enable);
  1015. /*
  1016. * Discovery Service subsystem definitions
  1017. */
  1018. static struct configfs_attribute *nvmet_referral_attrs[] = {
  1019. &nvmet_attr_addr_adrfam,
  1020. &nvmet_attr_addr_portid,
  1021. &nvmet_attr_addr_treq,
  1022. &nvmet_attr_addr_traddr,
  1023. &nvmet_attr_addr_trsvcid,
  1024. &nvmet_attr_addr_trtype,
  1025. &nvmet_referral_attr_enable,
  1026. NULL,
  1027. };
  1028. static void nvmet_referral_notify(struct config_group *group,
  1029. struct config_item *item)
  1030. {
  1031. struct nvmet_port *parent = to_nvmet_port(item->ci_parent->ci_parent);
  1032. struct nvmet_port *port = to_nvmet_port(item);
  1033. nvmet_referral_disable(parent, port);
  1034. }
  1035. static void nvmet_referral_release(struct config_item *item)
  1036. {
  1037. struct nvmet_port *port = to_nvmet_port(item);
  1038. kfree(port);
  1039. }
  1040. static struct configfs_item_operations nvmet_referral_item_ops = {
  1041. .release = nvmet_referral_release,
  1042. };
  1043. static const struct config_item_type nvmet_referral_type = {
  1044. .ct_owner = THIS_MODULE,
  1045. .ct_attrs = nvmet_referral_attrs,
  1046. .ct_item_ops = &nvmet_referral_item_ops,
  1047. };
  1048. static struct config_group *nvmet_referral_make(
  1049. struct config_group *group, const char *name)
  1050. {
  1051. struct nvmet_port *port;
  1052. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1053. if (!port)
  1054. return ERR_PTR(-ENOMEM);
  1055. INIT_LIST_HEAD(&port->entry);
  1056. config_group_init_type_name(&port->group, name, &nvmet_referral_type);
  1057. return &port->group;
  1058. }
  1059. static struct configfs_group_operations nvmet_referral_group_ops = {
  1060. .make_group = nvmet_referral_make,
  1061. .disconnect_notify = nvmet_referral_notify,
  1062. };
  1063. static const struct config_item_type nvmet_referrals_type = {
  1064. .ct_owner = THIS_MODULE,
  1065. .ct_group_ops = &nvmet_referral_group_ops,
  1066. };
  1067. static struct nvmet_type_name_map nvmet_ana_state[] = {
  1068. { NVME_ANA_OPTIMIZED, "optimized" },
  1069. { NVME_ANA_NONOPTIMIZED, "non-optimized" },
  1070. { NVME_ANA_INACCESSIBLE, "inaccessible" },
  1071. { NVME_ANA_PERSISTENT_LOSS, "persistent-loss" },
  1072. { NVME_ANA_CHANGE, "change" },
  1073. };
  1074. static ssize_t nvmet_ana_group_ana_state_show(struct config_item *item,
  1075. char *page)
  1076. {
  1077. struct nvmet_ana_group *grp = to_ana_group(item);
  1078. enum nvme_ana_state state = grp->port->ana_state[grp->grpid];
  1079. int i;
  1080. for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
  1081. if (state == nvmet_ana_state[i].type)
  1082. return sprintf(page, "%s\n", nvmet_ana_state[i].name);
  1083. }
  1084. return sprintf(page, "\n");
  1085. }
  1086. static ssize_t nvmet_ana_group_ana_state_store(struct config_item *item,
  1087. const char *page, size_t count)
  1088. {
  1089. struct nvmet_ana_group *grp = to_ana_group(item);
  1090. enum nvme_ana_state *ana_state = grp->port->ana_state;
  1091. int i;
  1092. for (i = 0; i < ARRAY_SIZE(nvmet_ana_state); i++) {
  1093. if (sysfs_streq(page, nvmet_ana_state[i].name))
  1094. goto found;
  1095. }
  1096. pr_err("Invalid value '%s' for ana_state\n", page);
  1097. return -EINVAL;
  1098. found:
  1099. down_write(&nvmet_ana_sem);
  1100. ana_state[grp->grpid] = (enum nvme_ana_state) nvmet_ana_state[i].type;
  1101. nvmet_ana_chgcnt++;
  1102. up_write(&nvmet_ana_sem);
  1103. nvmet_port_send_ana_event(grp->port);
  1104. return count;
  1105. }
  1106. CONFIGFS_ATTR(nvmet_ana_group_, ana_state);
  1107. static struct configfs_attribute *nvmet_ana_group_attrs[] = {
  1108. &nvmet_ana_group_attr_ana_state,
  1109. NULL,
  1110. };
  1111. static void nvmet_ana_group_release(struct config_item *item)
  1112. {
  1113. struct nvmet_ana_group *grp = to_ana_group(item);
  1114. if (grp == &grp->port->ana_default_group)
  1115. return;
  1116. down_write(&nvmet_ana_sem);
  1117. grp->port->ana_state[grp->grpid] = NVME_ANA_INACCESSIBLE;
  1118. nvmet_ana_group_enabled[grp->grpid]--;
  1119. up_write(&nvmet_ana_sem);
  1120. nvmet_port_send_ana_event(grp->port);
  1121. kfree(grp);
  1122. }
  1123. static struct configfs_item_operations nvmet_ana_group_item_ops = {
  1124. .release = nvmet_ana_group_release,
  1125. };
  1126. static const struct config_item_type nvmet_ana_group_type = {
  1127. .ct_item_ops = &nvmet_ana_group_item_ops,
  1128. .ct_attrs = nvmet_ana_group_attrs,
  1129. .ct_owner = THIS_MODULE,
  1130. };
  1131. static struct config_group *nvmet_ana_groups_make_group(
  1132. struct config_group *group, const char *name)
  1133. {
  1134. struct nvmet_port *port = ana_groups_to_port(&group->cg_item);
  1135. struct nvmet_ana_group *grp;
  1136. u32 grpid;
  1137. int ret;
  1138. ret = kstrtou32(name, 0, &grpid);
  1139. if (ret)
  1140. goto out;
  1141. ret = -EINVAL;
  1142. if (grpid <= 1 || grpid > NVMET_MAX_ANAGRPS)
  1143. goto out;
  1144. ret = -ENOMEM;
  1145. grp = kzalloc(sizeof(*grp), GFP_KERNEL);
  1146. if (!grp)
  1147. goto out;
  1148. grp->port = port;
  1149. grp->grpid = grpid;
  1150. down_write(&nvmet_ana_sem);
  1151. nvmet_ana_group_enabled[grpid]++;
  1152. up_write(&nvmet_ana_sem);
  1153. nvmet_port_send_ana_event(grp->port);
  1154. config_group_init_type_name(&grp->group, name, &nvmet_ana_group_type);
  1155. return &grp->group;
  1156. out:
  1157. return ERR_PTR(ret);
  1158. }
  1159. static struct configfs_group_operations nvmet_ana_groups_group_ops = {
  1160. .make_group = nvmet_ana_groups_make_group,
  1161. };
  1162. static const struct config_item_type nvmet_ana_groups_type = {
  1163. .ct_group_ops = &nvmet_ana_groups_group_ops,
  1164. .ct_owner = THIS_MODULE,
  1165. };
  1166. /*
  1167. * Ports definitions.
  1168. */
  1169. static void nvmet_port_release(struct config_item *item)
  1170. {
  1171. struct nvmet_port *port = to_nvmet_port(item);
  1172. /* Let inflight controllers teardown complete */
  1173. flush_scheduled_work();
  1174. list_del(&port->global_entry);
  1175. kfree(port->ana_state);
  1176. kfree(port);
  1177. }
  1178. static struct configfs_attribute *nvmet_port_attrs[] = {
  1179. &nvmet_attr_addr_adrfam,
  1180. &nvmet_attr_addr_treq,
  1181. &nvmet_attr_addr_traddr,
  1182. &nvmet_attr_addr_trsvcid,
  1183. &nvmet_attr_addr_trtype,
  1184. &nvmet_attr_param_inline_data_size,
  1185. #ifdef CONFIG_BLK_DEV_INTEGRITY
  1186. &nvmet_attr_param_pi_enable,
  1187. #endif
  1188. NULL,
  1189. };
  1190. static struct configfs_item_operations nvmet_port_item_ops = {
  1191. .release = nvmet_port_release,
  1192. };
  1193. static const struct config_item_type nvmet_port_type = {
  1194. .ct_attrs = nvmet_port_attrs,
  1195. .ct_item_ops = &nvmet_port_item_ops,
  1196. .ct_owner = THIS_MODULE,
  1197. };
  1198. static struct config_group *nvmet_ports_make(struct config_group *group,
  1199. const char *name)
  1200. {
  1201. struct nvmet_port *port;
  1202. u16 portid;
  1203. u32 i;
  1204. if (kstrtou16(name, 0, &portid))
  1205. return ERR_PTR(-EINVAL);
  1206. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1207. if (!port)
  1208. return ERR_PTR(-ENOMEM);
  1209. port->ana_state = kcalloc(NVMET_MAX_ANAGRPS + 1,
  1210. sizeof(*port->ana_state), GFP_KERNEL);
  1211. if (!port->ana_state) {
  1212. kfree(port);
  1213. return ERR_PTR(-ENOMEM);
  1214. }
  1215. for (i = 1; i <= NVMET_MAX_ANAGRPS; i++) {
  1216. if (i == NVMET_DEFAULT_ANA_GRPID)
  1217. port->ana_state[1] = NVME_ANA_OPTIMIZED;
  1218. else
  1219. port->ana_state[i] = NVME_ANA_INACCESSIBLE;
  1220. }
  1221. list_add(&port->global_entry, &nvmet_ports_list);
  1222. INIT_LIST_HEAD(&port->entry);
  1223. INIT_LIST_HEAD(&port->subsystems);
  1224. INIT_LIST_HEAD(&port->referrals);
  1225. port->inline_data_size = -1; /* < 0 == let the transport choose */
  1226. port->disc_addr.portid = cpu_to_le16(portid);
  1227. port->disc_addr.adrfam = NVMF_ADDR_FAMILY_MAX;
  1228. port->disc_addr.treq = NVMF_TREQ_DISABLE_SQFLOW;
  1229. config_group_init_type_name(&port->group, name, &nvmet_port_type);
  1230. config_group_init_type_name(&port->subsys_group,
  1231. "subsystems", &nvmet_port_subsys_type);
  1232. configfs_add_default_group(&port->subsys_group, &port->group);
  1233. config_group_init_type_name(&port->referrals_group,
  1234. "referrals", &nvmet_referrals_type);
  1235. configfs_add_default_group(&port->referrals_group, &port->group);
  1236. config_group_init_type_name(&port->ana_groups_group,
  1237. "ana_groups", &nvmet_ana_groups_type);
  1238. configfs_add_default_group(&port->ana_groups_group, &port->group);
  1239. port->ana_default_group.port = port;
  1240. port->ana_default_group.grpid = NVMET_DEFAULT_ANA_GRPID;
  1241. config_group_init_type_name(&port->ana_default_group.group,
  1242. __stringify(NVMET_DEFAULT_ANA_GRPID),
  1243. &nvmet_ana_group_type);
  1244. configfs_add_default_group(&port->ana_default_group.group,
  1245. &port->ana_groups_group);
  1246. return &port->group;
  1247. }
  1248. static struct configfs_group_operations nvmet_ports_group_ops = {
  1249. .make_group = nvmet_ports_make,
  1250. };
  1251. static const struct config_item_type nvmet_ports_type = {
  1252. .ct_group_ops = &nvmet_ports_group_ops,
  1253. .ct_owner = THIS_MODULE,
  1254. };
  1255. static struct config_group nvmet_subsystems_group;
  1256. static struct config_group nvmet_ports_group;
  1257. static void nvmet_host_release(struct config_item *item)
  1258. {
  1259. struct nvmet_host *host = to_host(item);
  1260. kfree(host);
  1261. }
  1262. static struct configfs_item_operations nvmet_host_item_ops = {
  1263. .release = nvmet_host_release,
  1264. };
  1265. static const struct config_item_type nvmet_host_type = {
  1266. .ct_item_ops = &nvmet_host_item_ops,
  1267. .ct_owner = THIS_MODULE,
  1268. };
  1269. static struct config_group *nvmet_hosts_make_group(struct config_group *group,
  1270. const char *name)
  1271. {
  1272. struct nvmet_host *host;
  1273. host = kzalloc(sizeof(*host), GFP_KERNEL);
  1274. if (!host)
  1275. return ERR_PTR(-ENOMEM);
  1276. config_group_init_type_name(&host->group, name, &nvmet_host_type);
  1277. return &host->group;
  1278. }
  1279. static struct configfs_group_operations nvmet_hosts_group_ops = {
  1280. .make_group = nvmet_hosts_make_group,
  1281. };
  1282. static const struct config_item_type nvmet_hosts_type = {
  1283. .ct_group_ops = &nvmet_hosts_group_ops,
  1284. .ct_owner = THIS_MODULE,
  1285. };
  1286. static struct config_group nvmet_hosts_group;
  1287. static const struct config_item_type nvmet_root_type = {
  1288. .ct_owner = THIS_MODULE,
  1289. };
  1290. static struct configfs_subsystem nvmet_configfs_subsystem = {
  1291. .su_group = {
  1292. .cg_item = {
  1293. .ci_namebuf = "nvmet",
  1294. .ci_type = &nvmet_root_type,
  1295. },
  1296. },
  1297. };
  1298. int __init nvmet_init_configfs(void)
  1299. {
  1300. int ret;
  1301. config_group_init(&nvmet_configfs_subsystem.su_group);
  1302. mutex_init(&nvmet_configfs_subsystem.su_mutex);
  1303. config_group_init_type_name(&nvmet_subsystems_group,
  1304. "subsystems", &nvmet_subsystems_type);
  1305. configfs_add_default_group(&nvmet_subsystems_group,
  1306. &nvmet_configfs_subsystem.su_group);
  1307. config_group_init_type_name(&nvmet_ports_group,
  1308. "ports", &nvmet_ports_type);
  1309. configfs_add_default_group(&nvmet_ports_group,
  1310. &nvmet_configfs_subsystem.su_group);
  1311. config_group_init_type_name(&nvmet_hosts_group,
  1312. "hosts", &nvmet_hosts_type);
  1313. configfs_add_default_group(&nvmet_hosts_group,
  1314. &nvmet_configfs_subsystem.su_group);
  1315. ret = configfs_register_subsystem(&nvmet_configfs_subsystem);
  1316. if (ret) {
  1317. pr_err("configfs_register_subsystem: %d\n", ret);
  1318. return ret;
  1319. }
  1320. return 0;
  1321. }
  1322. void __exit nvmet_exit_configfs(void)
  1323. {
  1324. configfs_unregister_subsystem(&nvmet_configfs_subsystem);
  1325. }