core.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /*
  2. * Common code for the NVMe target.
  3. * Copyright (c) 2015-2016 HGST, a Western Digital Company.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/random.h>
  17. #include <linux/rculist.h>
  18. #include "nvmet.h"
  19. struct workqueue_struct *buffered_io_wq;
  20. static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
  21. static DEFINE_IDA(cntlid_ida);
  22. /*
  23. * This read/write semaphore is used to synchronize access to configuration
  24. * information on a target system that will result in discovery log page
  25. * information change for at least one host.
  26. * The full list of resources to protected by this semaphore is:
  27. *
  28. * - subsystems list
  29. * - per-subsystem allowed hosts list
  30. * - allow_any_host subsystem attribute
  31. * - nvmet_genctr
  32. * - the nvmet_transports array
  33. *
  34. * When updating any of those lists/structures write lock should be obtained,
  35. * while when reading (popolating discovery log page or checking host-subsystem
  36. * link) read lock is obtained to allow concurrent reads.
  37. */
  38. DECLARE_RWSEM(nvmet_config_sem);
  39. u32 nvmet_ana_group_enabled[NVMET_MAX_ANAGRPS + 1];
  40. u64 nvmet_ana_chgcnt;
  41. DECLARE_RWSEM(nvmet_ana_sem);
  42. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  43. const char *subsysnqn);
  44. u16 nvmet_copy_to_sgl(struct nvmet_req *req, off_t off, const void *buf,
  45. size_t len)
  46. {
  47. if (sg_pcopy_from_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
  48. return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
  49. return 0;
  50. }
  51. u16 nvmet_copy_from_sgl(struct nvmet_req *req, off_t off, void *buf, size_t len)
  52. {
  53. if (sg_pcopy_to_buffer(req->sg, req->sg_cnt, buf, len, off) != len)
  54. return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
  55. return 0;
  56. }
  57. u16 nvmet_zero_sgl(struct nvmet_req *req, off_t off, size_t len)
  58. {
  59. if (sg_zero_buffer(req->sg, req->sg_cnt, len, off) != len)
  60. return NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR;
  61. return 0;
  62. }
  63. static unsigned int nvmet_max_nsid(struct nvmet_subsys *subsys)
  64. {
  65. struct nvmet_ns *ns;
  66. if (list_empty(&subsys->namespaces))
  67. return 0;
  68. ns = list_last_entry(&subsys->namespaces, struct nvmet_ns, dev_link);
  69. return ns->nsid;
  70. }
  71. static u32 nvmet_async_event_result(struct nvmet_async_event *aen)
  72. {
  73. return aen->event_type | (aen->event_info << 8) | (aen->log_page << 16);
  74. }
  75. static void nvmet_async_events_free(struct nvmet_ctrl *ctrl)
  76. {
  77. struct nvmet_req *req;
  78. while (1) {
  79. mutex_lock(&ctrl->lock);
  80. if (!ctrl->nr_async_event_cmds) {
  81. mutex_unlock(&ctrl->lock);
  82. return;
  83. }
  84. req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
  85. mutex_unlock(&ctrl->lock);
  86. nvmet_req_complete(req, NVME_SC_INTERNAL | NVME_SC_DNR);
  87. }
  88. }
  89. static void nvmet_async_event_work(struct work_struct *work)
  90. {
  91. struct nvmet_ctrl *ctrl =
  92. container_of(work, struct nvmet_ctrl, async_event_work);
  93. struct nvmet_async_event *aen;
  94. struct nvmet_req *req;
  95. while (1) {
  96. mutex_lock(&ctrl->lock);
  97. aen = list_first_entry_or_null(&ctrl->async_events,
  98. struct nvmet_async_event, entry);
  99. if (!aen || !ctrl->nr_async_event_cmds) {
  100. mutex_unlock(&ctrl->lock);
  101. return;
  102. }
  103. req = ctrl->async_event_cmds[--ctrl->nr_async_event_cmds];
  104. nvmet_set_result(req, nvmet_async_event_result(aen));
  105. list_del(&aen->entry);
  106. kfree(aen);
  107. mutex_unlock(&ctrl->lock);
  108. nvmet_req_complete(req, 0);
  109. }
  110. }
  111. static void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
  112. u8 event_info, u8 log_page)
  113. {
  114. struct nvmet_async_event *aen;
  115. aen = kmalloc(sizeof(*aen), GFP_KERNEL);
  116. if (!aen)
  117. return;
  118. aen->event_type = event_type;
  119. aen->event_info = event_info;
  120. aen->log_page = log_page;
  121. mutex_lock(&ctrl->lock);
  122. list_add_tail(&aen->entry, &ctrl->async_events);
  123. mutex_unlock(&ctrl->lock);
  124. schedule_work(&ctrl->async_event_work);
  125. }
  126. static bool nvmet_aen_disabled(struct nvmet_ctrl *ctrl, u32 aen)
  127. {
  128. if (!(READ_ONCE(ctrl->aen_enabled) & aen))
  129. return true;
  130. return test_and_set_bit(aen, &ctrl->aen_masked);
  131. }
  132. static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
  133. {
  134. u32 i;
  135. mutex_lock(&ctrl->lock);
  136. if (ctrl->nr_changed_ns > NVME_MAX_CHANGED_NAMESPACES)
  137. goto out_unlock;
  138. for (i = 0; i < ctrl->nr_changed_ns; i++) {
  139. if (ctrl->changed_ns_list[i] == nsid)
  140. goto out_unlock;
  141. }
  142. if (ctrl->nr_changed_ns == NVME_MAX_CHANGED_NAMESPACES) {
  143. ctrl->changed_ns_list[0] = cpu_to_le32(0xffffffff);
  144. ctrl->nr_changed_ns = U32_MAX;
  145. goto out_unlock;
  146. }
  147. ctrl->changed_ns_list[ctrl->nr_changed_ns++] = nsid;
  148. out_unlock:
  149. mutex_unlock(&ctrl->lock);
  150. }
  151. void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid)
  152. {
  153. struct nvmet_ctrl *ctrl;
  154. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  155. nvmet_add_to_changed_ns_log(ctrl, cpu_to_le32(nsid));
  156. if (nvmet_aen_disabled(ctrl, NVME_AEN_CFG_NS_ATTR))
  157. continue;
  158. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
  159. NVME_AER_NOTICE_NS_CHANGED,
  160. NVME_LOG_CHANGED_NS);
  161. }
  162. }
  163. void nvmet_send_ana_event(struct nvmet_subsys *subsys,
  164. struct nvmet_port *port)
  165. {
  166. struct nvmet_ctrl *ctrl;
  167. mutex_lock(&subsys->lock);
  168. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  169. if (port && ctrl->port != port)
  170. continue;
  171. if (nvmet_aen_disabled(ctrl, NVME_AEN_CFG_ANA_CHANGE))
  172. continue;
  173. nvmet_add_async_event(ctrl, NVME_AER_TYPE_NOTICE,
  174. NVME_AER_NOTICE_ANA, NVME_LOG_ANA);
  175. }
  176. mutex_unlock(&subsys->lock);
  177. }
  178. void nvmet_port_send_ana_event(struct nvmet_port *port)
  179. {
  180. struct nvmet_subsys_link *p;
  181. down_read(&nvmet_config_sem);
  182. list_for_each_entry(p, &port->subsystems, entry)
  183. nvmet_send_ana_event(p->subsys, port);
  184. up_read(&nvmet_config_sem);
  185. }
  186. int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
  187. {
  188. int ret = 0;
  189. down_write(&nvmet_config_sem);
  190. if (nvmet_transports[ops->type])
  191. ret = -EINVAL;
  192. else
  193. nvmet_transports[ops->type] = ops;
  194. up_write(&nvmet_config_sem);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_GPL(nvmet_register_transport);
  198. void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
  199. {
  200. down_write(&nvmet_config_sem);
  201. nvmet_transports[ops->type] = NULL;
  202. up_write(&nvmet_config_sem);
  203. }
  204. EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
  205. int nvmet_enable_port(struct nvmet_port *port)
  206. {
  207. const struct nvmet_fabrics_ops *ops;
  208. int ret;
  209. lockdep_assert_held(&nvmet_config_sem);
  210. ops = nvmet_transports[port->disc_addr.trtype];
  211. if (!ops) {
  212. up_write(&nvmet_config_sem);
  213. request_module("nvmet-transport-%d", port->disc_addr.trtype);
  214. down_write(&nvmet_config_sem);
  215. ops = nvmet_transports[port->disc_addr.trtype];
  216. if (!ops) {
  217. pr_err("transport type %d not supported\n",
  218. port->disc_addr.trtype);
  219. return -EINVAL;
  220. }
  221. }
  222. if (!try_module_get(ops->owner))
  223. return -EINVAL;
  224. ret = ops->add_port(port);
  225. if (ret) {
  226. module_put(ops->owner);
  227. return ret;
  228. }
  229. /* If the transport didn't set inline_data_size, then disable it. */
  230. if (port->inline_data_size < 0)
  231. port->inline_data_size = 0;
  232. port->enabled = true;
  233. return 0;
  234. }
  235. void nvmet_disable_port(struct nvmet_port *port)
  236. {
  237. const struct nvmet_fabrics_ops *ops;
  238. lockdep_assert_held(&nvmet_config_sem);
  239. port->enabled = false;
  240. ops = nvmet_transports[port->disc_addr.trtype];
  241. ops->remove_port(port);
  242. module_put(ops->owner);
  243. }
  244. static void nvmet_keep_alive_timer(struct work_struct *work)
  245. {
  246. struct nvmet_ctrl *ctrl = container_of(to_delayed_work(work),
  247. struct nvmet_ctrl, ka_work);
  248. pr_err("ctrl %d keep-alive timer (%d seconds) expired!\n",
  249. ctrl->cntlid, ctrl->kato);
  250. nvmet_ctrl_fatal_error(ctrl);
  251. }
  252. static void nvmet_start_keep_alive_timer(struct nvmet_ctrl *ctrl)
  253. {
  254. if (unlikely(ctrl->kato == 0))
  255. return;
  256. pr_debug("ctrl %d start keep-alive timer for %d secs\n",
  257. ctrl->cntlid, ctrl->kato);
  258. INIT_DELAYED_WORK(&ctrl->ka_work, nvmet_keep_alive_timer);
  259. schedule_delayed_work(&ctrl->ka_work, ctrl->kato * HZ);
  260. }
  261. static void nvmet_stop_keep_alive_timer(struct nvmet_ctrl *ctrl)
  262. {
  263. if (unlikely(ctrl->kato == 0))
  264. return;
  265. pr_debug("ctrl %d stop keep-alive\n", ctrl->cntlid);
  266. cancel_delayed_work_sync(&ctrl->ka_work);
  267. }
  268. static struct nvmet_ns *__nvmet_find_namespace(struct nvmet_ctrl *ctrl,
  269. __le32 nsid)
  270. {
  271. struct nvmet_ns *ns;
  272. list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
  273. if (ns->nsid == le32_to_cpu(nsid))
  274. return ns;
  275. }
  276. return NULL;
  277. }
  278. struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid)
  279. {
  280. struct nvmet_ns *ns;
  281. rcu_read_lock();
  282. ns = __nvmet_find_namespace(ctrl, nsid);
  283. if (ns)
  284. percpu_ref_get(&ns->ref);
  285. rcu_read_unlock();
  286. return ns;
  287. }
  288. static void nvmet_destroy_namespace(struct percpu_ref *ref)
  289. {
  290. struct nvmet_ns *ns = container_of(ref, struct nvmet_ns, ref);
  291. complete(&ns->disable_done);
  292. }
  293. void nvmet_put_namespace(struct nvmet_ns *ns)
  294. {
  295. percpu_ref_put(&ns->ref);
  296. }
  297. static void nvmet_ns_dev_disable(struct nvmet_ns *ns)
  298. {
  299. nvmet_bdev_ns_disable(ns);
  300. nvmet_file_ns_disable(ns);
  301. }
  302. int nvmet_ns_enable(struct nvmet_ns *ns)
  303. {
  304. struct nvmet_subsys *subsys = ns->subsys;
  305. int ret;
  306. mutex_lock(&subsys->lock);
  307. ret = -EMFILE;
  308. if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
  309. goto out_unlock;
  310. ret = 0;
  311. if (ns->enabled)
  312. goto out_unlock;
  313. ret = nvmet_bdev_ns_enable(ns);
  314. if (ret == -ENOTBLK)
  315. ret = nvmet_file_ns_enable(ns);
  316. if (ret)
  317. goto out_unlock;
  318. ret = percpu_ref_init(&ns->ref, nvmet_destroy_namespace,
  319. 0, GFP_KERNEL);
  320. if (ret)
  321. goto out_dev_put;
  322. if (ns->nsid > subsys->max_nsid)
  323. subsys->max_nsid = ns->nsid;
  324. /*
  325. * The namespaces list needs to be sorted to simplify the implementation
  326. * of the Identify Namepace List subcommand.
  327. */
  328. if (list_empty(&subsys->namespaces)) {
  329. list_add_tail_rcu(&ns->dev_link, &subsys->namespaces);
  330. } else {
  331. struct nvmet_ns *old;
  332. list_for_each_entry_rcu(old, &subsys->namespaces, dev_link) {
  333. BUG_ON(ns->nsid == old->nsid);
  334. if (ns->nsid < old->nsid)
  335. break;
  336. }
  337. list_add_tail_rcu(&ns->dev_link, &old->dev_link);
  338. }
  339. subsys->nr_namespaces++;
  340. nvmet_ns_changed(subsys, ns->nsid);
  341. ns->enabled = true;
  342. ret = 0;
  343. out_unlock:
  344. mutex_unlock(&subsys->lock);
  345. return ret;
  346. out_dev_put:
  347. nvmet_ns_dev_disable(ns);
  348. goto out_unlock;
  349. }
  350. void nvmet_ns_disable(struct nvmet_ns *ns)
  351. {
  352. struct nvmet_subsys *subsys = ns->subsys;
  353. mutex_lock(&subsys->lock);
  354. if (!ns->enabled)
  355. goto out_unlock;
  356. ns->enabled = false;
  357. list_del_rcu(&ns->dev_link);
  358. if (ns->nsid == subsys->max_nsid)
  359. subsys->max_nsid = nvmet_max_nsid(subsys);
  360. mutex_unlock(&subsys->lock);
  361. /*
  362. * Now that we removed the namespaces from the lookup list, we
  363. * can kill the per_cpu ref and wait for any remaining references
  364. * to be dropped, as well as a RCU grace period for anyone only
  365. * using the namepace under rcu_read_lock(). Note that we can't
  366. * use call_rcu here as we need to ensure the namespaces have
  367. * been fully destroyed before unloading the module.
  368. */
  369. percpu_ref_kill(&ns->ref);
  370. synchronize_rcu();
  371. wait_for_completion(&ns->disable_done);
  372. percpu_ref_exit(&ns->ref);
  373. mutex_lock(&subsys->lock);
  374. subsys->nr_namespaces--;
  375. nvmet_ns_changed(subsys, ns->nsid);
  376. nvmet_ns_dev_disable(ns);
  377. out_unlock:
  378. mutex_unlock(&subsys->lock);
  379. }
  380. void nvmet_ns_free(struct nvmet_ns *ns)
  381. {
  382. nvmet_ns_disable(ns);
  383. down_write(&nvmet_ana_sem);
  384. nvmet_ana_group_enabled[ns->anagrpid]--;
  385. up_write(&nvmet_ana_sem);
  386. kfree(ns->device_path);
  387. kfree(ns);
  388. }
  389. struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid)
  390. {
  391. struct nvmet_ns *ns;
  392. ns = kzalloc(sizeof(*ns), GFP_KERNEL);
  393. if (!ns)
  394. return NULL;
  395. INIT_LIST_HEAD(&ns->dev_link);
  396. init_completion(&ns->disable_done);
  397. ns->nsid = nsid;
  398. ns->subsys = subsys;
  399. down_write(&nvmet_ana_sem);
  400. ns->anagrpid = NVMET_DEFAULT_ANA_GRPID;
  401. nvmet_ana_group_enabled[ns->anagrpid]++;
  402. up_write(&nvmet_ana_sem);
  403. uuid_gen(&ns->uuid);
  404. ns->buffered_io = false;
  405. return ns;
  406. }
  407. static void __nvmet_req_complete(struct nvmet_req *req, u16 status)
  408. {
  409. u32 old_sqhd, new_sqhd;
  410. u16 sqhd;
  411. if (status)
  412. nvmet_set_status(req, status);
  413. if (req->sq->size) {
  414. do {
  415. old_sqhd = req->sq->sqhd;
  416. new_sqhd = (old_sqhd + 1) % req->sq->size;
  417. } while (cmpxchg(&req->sq->sqhd, old_sqhd, new_sqhd) !=
  418. old_sqhd);
  419. }
  420. sqhd = req->sq->sqhd & 0x0000FFFF;
  421. req->rsp->sq_head = cpu_to_le16(sqhd);
  422. req->rsp->sq_id = cpu_to_le16(req->sq->qid);
  423. req->rsp->command_id = req->cmd->common.command_id;
  424. if (req->ns)
  425. nvmet_put_namespace(req->ns);
  426. req->ops->queue_response(req);
  427. }
  428. void nvmet_req_complete(struct nvmet_req *req, u16 status)
  429. {
  430. __nvmet_req_complete(req, status);
  431. percpu_ref_put(&req->sq->ref);
  432. }
  433. EXPORT_SYMBOL_GPL(nvmet_req_complete);
  434. void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq,
  435. u16 qid, u16 size)
  436. {
  437. cq->qid = qid;
  438. cq->size = size;
  439. ctrl->cqs[qid] = cq;
  440. }
  441. void nvmet_sq_setup(struct nvmet_ctrl *ctrl, struct nvmet_sq *sq,
  442. u16 qid, u16 size)
  443. {
  444. sq->sqhd = 0;
  445. sq->qid = qid;
  446. sq->size = size;
  447. ctrl->sqs[qid] = sq;
  448. }
  449. static void nvmet_confirm_sq(struct percpu_ref *ref)
  450. {
  451. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  452. complete(&sq->confirm_done);
  453. }
  454. void nvmet_sq_destroy(struct nvmet_sq *sq)
  455. {
  456. /*
  457. * If this is the admin queue, complete all AERs so that our
  458. * queue doesn't have outstanding requests on it.
  459. */
  460. if (sq->ctrl && sq->ctrl->sqs && sq->ctrl->sqs[0] == sq)
  461. nvmet_async_events_free(sq->ctrl);
  462. percpu_ref_kill_and_confirm(&sq->ref, nvmet_confirm_sq);
  463. wait_for_completion(&sq->confirm_done);
  464. wait_for_completion(&sq->free_done);
  465. percpu_ref_exit(&sq->ref);
  466. if (sq->ctrl) {
  467. nvmet_ctrl_put(sq->ctrl);
  468. sq->ctrl = NULL; /* allows reusing the queue later */
  469. }
  470. }
  471. EXPORT_SYMBOL_GPL(nvmet_sq_destroy);
  472. static void nvmet_sq_free(struct percpu_ref *ref)
  473. {
  474. struct nvmet_sq *sq = container_of(ref, struct nvmet_sq, ref);
  475. complete(&sq->free_done);
  476. }
  477. int nvmet_sq_init(struct nvmet_sq *sq)
  478. {
  479. int ret;
  480. ret = percpu_ref_init(&sq->ref, nvmet_sq_free, 0, GFP_KERNEL);
  481. if (ret) {
  482. pr_err("percpu_ref init failed!\n");
  483. return ret;
  484. }
  485. init_completion(&sq->free_done);
  486. init_completion(&sq->confirm_done);
  487. return 0;
  488. }
  489. EXPORT_SYMBOL_GPL(nvmet_sq_init);
  490. static inline u16 nvmet_check_ana_state(struct nvmet_port *port,
  491. struct nvmet_ns *ns)
  492. {
  493. enum nvme_ana_state state = port->ana_state[ns->anagrpid];
  494. if (unlikely(state == NVME_ANA_INACCESSIBLE))
  495. return NVME_SC_ANA_INACCESSIBLE;
  496. if (unlikely(state == NVME_ANA_PERSISTENT_LOSS))
  497. return NVME_SC_ANA_PERSISTENT_LOSS;
  498. if (unlikely(state == NVME_ANA_CHANGE))
  499. return NVME_SC_ANA_TRANSITION;
  500. return 0;
  501. }
  502. static inline u16 nvmet_io_cmd_check_access(struct nvmet_req *req)
  503. {
  504. if (unlikely(req->ns->readonly)) {
  505. switch (req->cmd->common.opcode) {
  506. case nvme_cmd_read:
  507. case nvme_cmd_flush:
  508. break;
  509. default:
  510. return NVME_SC_NS_WRITE_PROTECTED;
  511. }
  512. }
  513. return 0;
  514. }
  515. static u16 nvmet_parse_io_cmd(struct nvmet_req *req)
  516. {
  517. struct nvme_command *cmd = req->cmd;
  518. u16 ret;
  519. ret = nvmet_check_ctrl_status(req, cmd);
  520. if (unlikely(ret))
  521. return ret;
  522. req->ns = nvmet_find_namespace(req->sq->ctrl, cmd->rw.nsid);
  523. if (unlikely(!req->ns))
  524. return NVME_SC_INVALID_NS | NVME_SC_DNR;
  525. ret = nvmet_check_ana_state(req->port, req->ns);
  526. if (unlikely(ret))
  527. return ret;
  528. ret = nvmet_io_cmd_check_access(req);
  529. if (unlikely(ret))
  530. return ret;
  531. if (req->ns->file)
  532. return nvmet_file_parse_io_cmd(req);
  533. else
  534. return nvmet_bdev_parse_io_cmd(req);
  535. }
  536. bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
  537. struct nvmet_sq *sq, const struct nvmet_fabrics_ops *ops)
  538. {
  539. u8 flags = req->cmd->common.flags;
  540. u16 status;
  541. req->cq = cq;
  542. req->sq = sq;
  543. req->ops = ops;
  544. req->sg = NULL;
  545. req->sg_cnt = 0;
  546. req->transfer_len = 0;
  547. req->rsp->status = 0;
  548. req->ns = NULL;
  549. /* no support for fused commands yet */
  550. if (unlikely(flags & (NVME_CMD_FUSE_FIRST | NVME_CMD_FUSE_SECOND))) {
  551. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  552. goto fail;
  553. }
  554. /*
  555. * For fabrics, PSDT field shall describe metadata pointer (MPTR) that
  556. * contains an address of a single contiguous physical buffer that is
  557. * byte aligned.
  558. */
  559. if (unlikely((flags & NVME_CMD_SGL_ALL) != NVME_CMD_SGL_METABUF)) {
  560. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  561. goto fail;
  562. }
  563. if (unlikely(!req->sq->ctrl))
  564. /* will return an error for any Non-connect command: */
  565. status = nvmet_parse_connect_cmd(req);
  566. else if (likely(req->sq->qid != 0))
  567. status = nvmet_parse_io_cmd(req);
  568. else if (req->cmd->common.opcode == nvme_fabrics_command)
  569. status = nvmet_parse_fabrics_cmd(req);
  570. else if (req->sq->ctrl->subsys->type == NVME_NQN_DISC)
  571. status = nvmet_parse_discovery_cmd(req);
  572. else
  573. status = nvmet_parse_admin_cmd(req);
  574. if (status)
  575. goto fail;
  576. if (unlikely(!percpu_ref_tryget_live(&sq->ref))) {
  577. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  578. goto fail;
  579. }
  580. return true;
  581. fail:
  582. __nvmet_req_complete(req, status);
  583. return false;
  584. }
  585. EXPORT_SYMBOL_GPL(nvmet_req_init);
  586. void nvmet_req_uninit(struct nvmet_req *req)
  587. {
  588. percpu_ref_put(&req->sq->ref);
  589. if (req->ns)
  590. nvmet_put_namespace(req->ns);
  591. }
  592. EXPORT_SYMBOL_GPL(nvmet_req_uninit);
  593. void nvmet_req_execute(struct nvmet_req *req)
  594. {
  595. if (unlikely(req->data_len != req->transfer_len))
  596. nvmet_req_complete(req, NVME_SC_SGL_INVALID_DATA | NVME_SC_DNR);
  597. else
  598. req->execute(req);
  599. }
  600. EXPORT_SYMBOL_GPL(nvmet_req_execute);
  601. static inline bool nvmet_cc_en(u32 cc)
  602. {
  603. return (cc >> NVME_CC_EN_SHIFT) & 0x1;
  604. }
  605. static inline u8 nvmet_cc_css(u32 cc)
  606. {
  607. return (cc >> NVME_CC_CSS_SHIFT) & 0x7;
  608. }
  609. static inline u8 nvmet_cc_mps(u32 cc)
  610. {
  611. return (cc >> NVME_CC_MPS_SHIFT) & 0xf;
  612. }
  613. static inline u8 nvmet_cc_ams(u32 cc)
  614. {
  615. return (cc >> NVME_CC_AMS_SHIFT) & 0x7;
  616. }
  617. static inline u8 nvmet_cc_shn(u32 cc)
  618. {
  619. return (cc >> NVME_CC_SHN_SHIFT) & 0x3;
  620. }
  621. static inline u8 nvmet_cc_iosqes(u32 cc)
  622. {
  623. return (cc >> NVME_CC_IOSQES_SHIFT) & 0xf;
  624. }
  625. static inline u8 nvmet_cc_iocqes(u32 cc)
  626. {
  627. return (cc >> NVME_CC_IOCQES_SHIFT) & 0xf;
  628. }
  629. static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
  630. {
  631. lockdep_assert_held(&ctrl->lock);
  632. /*
  633. * Only I/O controllers should verify iosqes,iocqes.
  634. * Strictly speaking, the spec says a discovery controller
  635. * should verify iosqes,iocqes are zeroed, however that
  636. * would break backwards compatibility, so don't enforce it.
  637. */
  638. if (ctrl->subsys->type != NVME_NQN_DISC &&
  639. (nvmet_cc_iosqes(ctrl->cc) != NVME_NVM_IOSQES ||
  640. nvmet_cc_iocqes(ctrl->cc) != NVME_NVM_IOCQES)) {
  641. ctrl->csts = NVME_CSTS_CFS;
  642. return;
  643. }
  644. if (nvmet_cc_mps(ctrl->cc) != 0 ||
  645. nvmet_cc_ams(ctrl->cc) != 0 ||
  646. nvmet_cc_css(ctrl->cc) != 0) {
  647. ctrl->csts = NVME_CSTS_CFS;
  648. return;
  649. }
  650. ctrl->csts = NVME_CSTS_RDY;
  651. /*
  652. * Controllers that are not yet enabled should not really enforce the
  653. * keep alive timeout, but we still want to track a timeout and cleanup
  654. * in case a host died before it enabled the controller. Hence, simply
  655. * reset the keep alive timer when the controller is enabled.
  656. */
  657. if (ctrl->kato)
  658. mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
  659. }
  660. static void nvmet_clear_ctrl(struct nvmet_ctrl *ctrl)
  661. {
  662. lockdep_assert_held(&ctrl->lock);
  663. /* XXX: tear down queues? */
  664. ctrl->csts &= ~NVME_CSTS_RDY;
  665. ctrl->cc = 0;
  666. }
  667. void nvmet_update_cc(struct nvmet_ctrl *ctrl, u32 new)
  668. {
  669. u32 old;
  670. mutex_lock(&ctrl->lock);
  671. old = ctrl->cc;
  672. ctrl->cc = new;
  673. if (nvmet_cc_en(new) && !nvmet_cc_en(old))
  674. nvmet_start_ctrl(ctrl);
  675. if (!nvmet_cc_en(new) && nvmet_cc_en(old))
  676. nvmet_clear_ctrl(ctrl);
  677. if (nvmet_cc_shn(new) && !nvmet_cc_shn(old)) {
  678. nvmet_clear_ctrl(ctrl);
  679. ctrl->csts |= NVME_CSTS_SHST_CMPLT;
  680. }
  681. if (!nvmet_cc_shn(new) && nvmet_cc_shn(old))
  682. ctrl->csts &= ~NVME_CSTS_SHST_CMPLT;
  683. mutex_unlock(&ctrl->lock);
  684. }
  685. static void nvmet_init_cap(struct nvmet_ctrl *ctrl)
  686. {
  687. /* command sets supported: NVMe command set: */
  688. ctrl->cap = (1ULL << 37);
  689. /* CC.EN timeout in 500msec units: */
  690. ctrl->cap |= (15ULL << 24);
  691. /* maximum queue entries supported: */
  692. ctrl->cap |= NVMET_QUEUE_SIZE - 1;
  693. }
  694. u16 nvmet_ctrl_find_get(const char *subsysnqn, const char *hostnqn, u16 cntlid,
  695. struct nvmet_req *req, struct nvmet_ctrl **ret)
  696. {
  697. struct nvmet_subsys *subsys;
  698. struct nvmet_ctrl *ctrl;
  699. u16 status = 0;
  700. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  701. if (!subsys) {
  702. pr_warn("connect request for invalid subsystem %s!\n",
  703. subsysnqn);
  704. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  705. return NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  706. }
  707. mutex_lock(&subsys->lock);
  708. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
  709. if (ctrl->cntlid == cntlid) {
  710. if (strncmp(hostnqn, ctrl->hostnqn, NVMF_NQN_SIZE)) {
  711. pr_warn("hostnqn mismatch.\n");
  712. continue;
  713. }
  714. if (!kref_get_unless_zero(&ctrl->ref))
  715. continue;
  716. *ret = ctrl;
  717. goto out;
  718. }
  719. }
  720. pr_warn("could not find controller %d for subsys %s / host %s\n",
  721. cntlid, subsysnqn, hostnqn);
  722. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(cntlid);
  723. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  724. out:
  725. mutex_unlock(&subsys->lock);
  726. nvmet_subsys_put(subsys);
  727. return status;
  728. }
  729. u16 nvmet_check_ctrl_status(struct nvmet_req *req, struct nvme_command *cmd)
  730. {
  731. if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
  732. pr_err("got cmd %d while CC.EN == 0 on qid = %d\n",
  733. cmd->common.opcode, req->sq->qid);
  734. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  735. }
  736. if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
  737. pr_err("got cmd %d while CSTS.RDY == 0 on qid = %d\n",
  738. cmd->common.opcode, req->sq->qid);
  739. return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
  740. }
  741. return 0;
  742. }
  743. static bool __nvmet_host_allowed(struct nvmet_subsys *subsys,
  744. const char *hostnqn)
  745. {
  746. struct nvmet_host_link *p;
  747. if (subsys->allow_any_host)
  748. return true;
  749. list_for_each_entry(p, &subsys->hosts, entry) {
  750. if (!strcmp(nvmet_host_name(p->host), hostnqn))
  751. return true;
  752. }
  753. return false;
  754. }
  755. static bool nvmet_host_discovery_allowed(struct nvmet_req *req,
  756. const char *hostnqn)
  757. {
  758. struct nvmet_subsys_link *s;
  759. list_for_each_entry(s, &req->port->subsystems, entry) {
  760. if (__nvmet_host_allowed(s->subsys, hostnqn))
  761. return true;
  762. }
  763. return false;
  764. }
  765. bool nvmet_host_allowed(struct nvmet_req *req, struct nvmet_subsys *subsys,
  766. const char *hostnqn)
  767. {
  768. lockdep_assert_held(&nvmet_config_sem);
  769. if (subsys->type == NVME_NQN_DISC)
  770. return nvmet_host_discovery_allowed(req, hostnqn);
  771. else
  772. return __nvmet_host_allowed(subsys, hostnqn);
  773. }
  774. static void nvmet_fatal_error_handler(struct work_struct *work)
  775. {
  776. struct nvmet_ctrl *ctrl =
  777. container_of(work, struct nvmet_ctrl, fatal_err_work);
  778. pr_err("ctrl %d fatal error occurred!\n", ctrl->cntlid);
  779. ctrl->ops->delete_ctrl(ctrl);
  780. }
  781. u16 nvmet_alloc_ctrl(const char *subsysnqn, const char *hostnqn,
  782. struct nvmet_req *req, u32 kato, struct nvmet_ctrl **ctrlp)
  783. {
  784. struct nvmet_subsys *subsys;
  785. struct nvmet_ctrl *ctrl;
  786. int ret;
  787. u16 status;
  788. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  789. subsys = nvmet_find_get_subsys(req->port, subsysnqn);
  790. if (!subsys) {
  791. pr_warn("connect request for invalid subsystem %s!\n",
  792. subsysnqn);
  793. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(subsysnqn);
  794. goto out;
  795. }
  796. status = NVME_SC_CONNECT_INVALID_PARAM | NVME_SC_DNR;
  797. down_read(&nvmet_config_sem);
  798. if (!nvmet_host_allowed(req, subsys, hostnqn)) {
  799. pr_info("connect by host %s for subsystem %s not allowed\n",
  800. hostnqn, subsysnqn);
  801. req->rsp->result.u32 = IPO_IATTR_CONNECT_DATA(hostnqn);
  802. up_read(&nvmet_config_sem);
  803. status = NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR;
  804. goto out_put_subsystem;
  805. }
  806. up_read(&nvmet_config_sem);
  807. status = NVME_SC_INTERNAL;
  808. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  809. if (!ctrl)
  810. goto out_put_subsystem;
  811. mutex_init(&ctrl->lock);
  812. nvmet_init_cap(ctrl);
  813. ctrl->port = req->port;
  814. INIT_WORK(&ctrl->async_event_work, nvmet_async_event_work);
  815. INIT_LIST_HEAD(&ctrl->async_events);
  816. INIT_WORK(&ctrl->fatal_err_work, nvmet_fatal_error_handler);
  817. memcpy(ctrl->subsysnqn, subsysnqn, NVMF_NQN_SIZE);
  818. memcpy(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE);
  819. kref_init(&ctrl->ref);
  820. ctrl->subsys = subsys;
  821. WRITE_ONCE(ctrl->aen_enabled, NVMET_AEN_CFG_OPTIONAL);
  822. ctrl->changed_ns_list = kmalloc_array(NVME_MAX_CHANGED_NAMESPACES,
  823. sizeof(__le32), GFP_KERNEL);
  824. if (!ctrl->changed_ns_list)
  825. goto out_free_ctrl;
  826. ctrl->cqs = kcalloc(subsys->max_qid + 1,
  827. sizeof(struct nvmet_cq *),
  828. GFP_KERNEL);
  829. if (!ctrl->cqs)
  830. goto out_free_changed_ns_list;
  831. ctrl->sqs = kcalloc(subsys->max_qid + 1,
  832. sizeof(struct nvmet_sq *),
  833. GFP_KERNEL);
  834. if (!ctrl->sqs)
  835. goto out_free_cqs;
  836. ret = ida_simple_get(&cntlid_ida,
  837. NVME_CNTLID_MIN, NVME_CNTLID_MAX,
  838. GFP_KERNEL);
  839. if (ret < 0) {
  840. status = NVME_SC_CONNECT_CTRL_BUSY | NVME_SC_DNR;
  841. goto out_free_sqs;
  842. }
  843. ctrl->cntlid = ret;
  844. ctrl->ops = req->ops;
  845. if (ctrl->subsys->type == NVME_NQN_DISC) {
  846. /* Don't accept keep-alive timeout for discovery controllers */
  847. if (kato) {
  848. status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
  849. goto out_remove_ida;
  850. }
  851. /*
  852. * Discovery controllers use some arbitrary high value in order
  853. * to cleanup stale discovery sessions
  854. *
  855. * From the latest base diff RC:
  856. * "The Keep Alive command is not supported by
  857. * Discovery controllers. A transport may specify a
  858. * fixed Discovery controller activity timeout value
  859. * (e.g., 2 minutes). If no commands are received
  860. * by a Discovery controller within that time
  861. * period, the controller may perform the
  862. * actions for Keep Alive Timer expiration".
  863. */
  864. ctrl->kato = NVMET_DISC_KATO;
  865. } else {
  866. /* keep-alive timeout in seconds */
  867. ctrl->kato = DIV_ROUND_UP(kato, 1000);
  868. }
  869. nvmet_start_keep_alive_timer(ctrl);
  870. mutex_lock(&subsys->lock);
  871. list_add_tail(&ctrl->subsys_entry, &subsys->ctrls);
  872. mutex_unlock(&subsys->lock);
  873. *ctrlp = ctrl;
  874. return 0;
  875. out_remove_ida:
  876. ida_simple_remove(&cntlid_ida, ctrl->cntlid);
  877. out_free_sqs:
  878. kfree(ctrl->sqs);
  879. out_free_cqs:
  880. kfree(ctrl->cqs);
  881. out_free_changed_ns_list:
  882. kfree(ctrl->changed_ns_list);
  883. out_free_ctrl:
  884. kfree(ctrl);
  885. out_put_subsystem:
  886. nvmet_subsys_put(subsys);
  887. out:
  888. return status;
  889. }
  890. static void nvmet_ctrl_free(struct kref *ref)
  891. {
  892. struct nvmet_ctrl *ctrl = container_of(ref, struct nvmet_ctrl, ref);
  893. struct nvmet_subsys *subsys = ctrl->subsys;
  894. mutex_lock(&subsys->lock);
  895. list_del(&ctrl->subsys_entry);
  896. mutex_unlock(&subsys->lock);
  897. nvmet_stop_keep_alive_timer(ctrl);
  898. flush_work(&ctrl->async_event_work);
  899. cancel_work_sync(&ctrl->fatal_err_work);
  900. ida_simple_remove(&cntlid_ida, ctrl->cntlid);
  901. kfree(ctrl->sqs);
  902. kfree(ctrl->cqs);
  903. kfree(ctrl->changed_ns_list);
  904. kfree(ctrl);
  905. nvmet_subsys_put(subsys);
  906. }
  907. void nvmet_ctrl_put(struct nvmet_ctrl *ctrl)
  908. {
  909. kref_put(&ctrl->ref, nvmet_ctrl_free);
  910. }
  911. void nvmet_ctrl_fatal_error(struct nvmet_ctrl *ctrl)
  912. {
  913. mutex_lock(&ctrl->lock);
  914. if (!(ctrl->csts & NVME_CSTS_CFS)) {
  915. ctrl->csts |= NVME_CSTS_CFS;
  916. schedule_work(&ctrl->fatal_err_work);
  917. }
  918. mutex_unlock(&ctrl->lock);
  919. }
  920. EXPORT_SYMBOL_GPL(nvmet_ctrl_fatal_error);
  921. static struct nvmet_subsys *nvmet_find_get_subsys(struct nvmet_port *port,
  922. const char *subsysnqn)
  923. {
  924. struct nvmet_subsys_link *p;
  925. if (!port)
  926. return NULL;
  927. if (!strncmp(NVME_DISC_SUBSYS_NAME, subsysnqn,
  928. NVMF_NQN_SIZE)) {
  929. if (!kref_get_unless_zero(&nvmet_disc_subsys->ref))
  930. return NULL;
  931. return nvmet_disc_subsys;
  932. }
  933. down_read(&nvmet_config_sem);
  934. list_for_each_entry(p, &port->subsystems, entry) {
  935. if (!strncmp(p->subsys->subsysnqn, subsysnqn,
  936. NVMF_NQN_SIZE)) {
  937. if (!kref_get_unless_zero(&p->subsys->ref))
  938. break;
  939. up_read(&nvmet_config_sem);
  940. return p->subsys;
  941. }
  942. }
  943. up_read(&nvmet_config_sem);
  944. return NULL;
  945. }
  946. struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
  947. enum nvme_subsys_type type)
  948. {
  949. struct nvmet_subsys *subsys;
  950. subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
  951. if (!subsys)
  952. return NULL;
  953. subsys->ver = NVME_VS(1, 3, 0); /* NVMe 1.3.0 */
  954. /* generate a random serial number as our controllers are ephemeral: */
  955. get_random_bytes(&subsys->serial, sizeof(subsys->serial));
  956. switch (type) {
  957. case NVME_NQN_NVME:
  958. subsys->max_qid = NVMET_NR_QUEUES;
  959. break;
  960. case NVME_NQN_DISC:
  961. subsys->max_qid = 0;
  962. break;
  963. default:
  964. pr_err("%s: Unknown Subsystem type - %d\n", __func__, type);
  965. kfree(subsys);
  966. return NULL;
  967. }
  968. subsys->type = type;
  969. subsys->subsysnqn = kstrndup(subsysnqn, NVMF_NQN_SIZE,
  970. GFP_KERNEL);
  971. if (!subsys->subsysnqn) {
  972. kfree(subsys);
  973. return NULL;
  974. }
  975. kref_init(&subsys->ref);
  976. mutex_init(&subsys->lock);
  977. INIT_LIST_HEAD(&subsys->namespaces);
  978. INIT_LIST_HEAD(&subsys->ctrls);
  979. INIT_LIST_HEAD(&subsys->hosts);
  980. return subsys;
  981. }
  982. static void nvmet_subsys_free(struct kref *ref)
  983. {
  984. struct nvmet_subsys *subsys =
  985. container_of(ref, struct nvmet_subsys, ref);
  986. WARN_ON_ONCE(!list_empty(&subsys->namespaces));
  987. kfree(subsys->subsysnqn);
  988. kfree(subsys);
  989. }
  990. void nvmet_subsys_del_ctrls(struct nvmet_subsys *subsys)
  991. {
  992. struct nvmet_ctrl *ctrl;
  993. mutex_lock(&subsys->lock);
  994. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  995. ctrl->ops->delete_ctrl(ctrl);
  996. mutex_unlock(&subsys->lock);
  997. }
  998. void nvmet_subsys_put(struct nvmet_subsys *subsys)
  999. {
  1000. kref_put(&subsys->ref, nvmet_subsys_free);
  1001. }
  1002. static int __init nvmet_init(void)
  1003. {
  1004. int error;
  1005. nvmet_ana_group_enabled[NVMET_DEFAULT_ANA_GRPID] = 1;
  1006. buffered_io_wq = alloc_workqueue("nvmet-buffered-io-wq",
  1007. WQ_MEM_RECLAIM, 0);
  1008. if (!buffered_io_wq) {
  1009. error = -ENOMEM;
  1010. goto out;
  1011. }
  1012. error = nvmet_init_discovery();
  1013. if (error)
  1014. goto out_free_work_queue;
  1015. error = nvmet_init_configfs();
  1016. if (error)
  1017. goto out_exit_discovery;
  1018. return 0;
  1019. out_exit_discovery:
  1020. nvmet_exit_discovery();
  1021. out_free_work_queue:
  1022. destroy_workqueue(buffered_io_wq);
  1023. out:
  1024. return error;
  1025. }
  1026. static void __exit nvmet_exit(void)
  1027. {
  1028. nvmet_exit_configfs();
  1029. nvmet_exit_discovery();
  1030. ida_destroy(&cntlid_ida);
  1031. destroy_workqueue(buffered_io_wq);
  1032. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
  1033. BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
  1034. }
  1035. module_init(nvmet_init);
  1036. module_exit(nvmet_exit);
  1037. MODULE_LICENSE("GPL v2");