multipath.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2017-2018 Christoph Hellwig.
  4. */
  5. #include <linux/backing-dev.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/vmalloc.h>
  8. #include <trace/events/block.h>
  9. #include "nvme.h"
  10. bool multipath = true;
  11. static bool multipath_always_on;
  12. static int multipath_param_set(const char *val, const struct kernel_param *kp)
  13. {
  14. int ret;
  15. bool *arg = kp->arg;
  16. ret = param_set_bool(val, kp);
  17. if (ret)
  18. return ret;
  19. if (multipath_always_on && !*arg) {
  20. pr_err("Can't disable multipath when multipath_always_on is configured.\n");
  21. *arg = true;
  22. return -EINVAL;
  23. }
  24. return 0;
  25. }
  26. static const struct kernel_param_ops multipath_param_ops = {
  27. .set = multipath_param_set,
  28. .get = param_get_bool,
  29. };
  30. module_param_cb(multipath, &multipath_param_ops, &multipath, 0444);
  31. MODULE_PARM_DESC(multipath,
  32. "turn on native support for multiple controllers per subsystem");
  33. static int multipath_always_on_set(const char *val,
  34. const struct kernel_param *kp)
  35. {
  36. int ret;
  37. bool *arg = kp->arg;
  38. ret = param_set_bool(val, kp);
  39. if (ret < 0)
  40. return ret;
  41. if (*arg)
  42. multipath = true;
  43. return 0;
  44. }
  45. static const struct kernel_param_ops multipath_always_on_ops = {
  46. .set = multipath_always_on_set,
  47. .get = param_get_bool,
  48. };
  49. module_param_cb(multipath_always_on, &multipath_always_on_ops,
  50. &multipath_always_on, 0444);
  51. MODULE_PARM_DESC(multipath_always_on,
  52. "create multipath node always except for private namespace with non-unique nsid; note that this also implicitly enables native multipath support");
  53. static const char *nvme_iopolicy_names[] = {
  54. [NVME_IOPOLICY_NUMA] = "numa",
  55. [NVME_IOPOLICY_RR] = "round-robin",
  56. [NVME_IOPOLICY_QD] = "queue-depth",
  57. };
  58. static int iopolicy = NVME_IOPOLICY_NUMA;
  59. static int nvme_set_iopolicy(const char *val, const struct kernel_param *kp)
  60. {
  61. if (!val)
  62. return -EINVAL;
  63. if (!strncmp(val, "numa", 4))
  64. iopolicy = NVME_IOPOLICY_NUMA;
  65. else if (!strncmp(val, "round-robin", 11))
  66. iopolicy = NVME_IOPOLICY_RR;
  67. else if (!strncmp(val, "queue-depth", 11))
  68. iopolicy = NVME_IOPOLICY_QD;
  69. else
  70. return -EINVAL;
  71. return 0;
  72. }
  73. static int nvme_get_iopolicy(char *buf, const struct kernel_param *kp)
  74. {
  75. return sprintf(buf, "%s\n", nvme_iopolicy_names[iopolicy]);
  76. }
  77. module_param_call(iopolicy, nvme_set_iopolicy, nvme_get_iopolicy,
  78. &iopolicy, 0644);
  79. MODULE_PARM_DESC(iopolicy,
  80. "Default multipath I/O policy; 'numa' (default), 'round-robin' or 'queue-depth'");
  81. void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys)
  82. {
  83. subsys->iopolicy = iopolicy;
  84. }
  85. void nvme_mpath_unfreeze(struct nvme_subsystem *subsys)
  86. {
  87. struct nvme_ns_head *h;
  88. lockdep_assert_held(&subsys->lock);
  89. list_for_each_entry(h, &subsys->nsheads, entry)
  90. if (h->disk)
  91. blk_mq_unfreeze_queue_nomemrestore(h->disk->queue);
  92. }
  93. void nvme_mpath_wait_freeze(struct nvme_subsystem *subsys)
  94. {
  95. struct nvme_ns_head *h;
  96. lockdep_assert_held(&subsys->lock);
  97. list_for_each_entry(h, &subsys->nsheads, entry)
  98. if (h->disk)
  99. blk_mq_freeze_queue_wait(h->disk->queue);
  100. }
  101. void nvme_mpath_start_freeze(struct nvme_subsystem *subsys)
  102. {
  103. struct nvme_ns_head *h;
  104. lockdep_assert_held(&subsys->lock);
  105. list_for_each_entry(h, &subsys->nsheads, entry)
  106. if (h->disk)
  107. blk_freeze_queue_start(h->disk->queue);
  108. }
  109. void nvme_failover_req(struct request *req)
  110. {
  111. struct nvme_ns *ns = req->q->queuedata;
  112. u16 status = nvme_req(req)->status & NVME_SCT_SC_MASK;
  113. unsigned long flags;
  114. struct bio *bio;
  115. nvme_mpath_clear_current_path(ns);
  116. /*
  117. * If we got back an ANA error, we know the controller is alive but not
  118. * ready to serve this namespace. Kick of a re-read of the ANA
  119. * information page, and just try any other available path for now.
  120. */
  121. if (nvme_is_ana_error(status) && ns->ctrl->ana_log_buf) {
  122. set_bit(NVME_NS_ANA_PENDING, &ns->flags);
  123. queue_work(nvme_wq, &ns->ctrl->ana_work);
  124. }
  125. spin_lock_irqsave(&ns->head->requeue_lock, flags);
  126. for (bio = req->bio; bio; bio = bio->bi_next) {
  127. bio_set_dev(bio, ns->head->disk->part0);
  128. if (bio->bi_opf & REQ_POLLED) {
  129. bio->bi_opf &= ~REQ_POLLED;
  130. bio->bi_cookie = BLK_QC_T_NONE;
  131. }
  132. /*
  133. * The alternate request queue that we may end up submitting
  134. * the bio to may be frozen temporarily, in this case REQ_NOWAIT
  135. * will fail the I/O immediately with EAGAIN to the issuer.
  136. * We are not in the issuer context which cannot block. Clear
  137. * the flag to avoid spurious EAGAIN I/O failures.
  138. */
  139. bio->bi_opf &= ~REQ_NOWAIT;
  140. }
  141. blk_steal_bios(&ns->head->requeue_list, req);
  142. spin_unlock_irqrestore(&ns->head->requeue_lock, flags);
  143. nvme_req(req)->status = 0;
  144. nvme_end_req(req);
  145. kblockd_schedule_work(&ns->head->requeue_work);
  146. }
  147. void nvme_mpath_start_request(struct request *rq)
  148. {
  149. struct nvme_ns *ns = rq->q->queuedata;
  150. struct gendisk *disk = ns->head->disk;
  151. if ((READ_ONCE(ns->head->subsys->iopolicy) == NVME_IOPOLICY_QD) &&
  152. !(nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)) {
  153. atomic_inc(&ns->ctrl->nr_active);
  154. nvme_req(rq)->flags |= NVME_MPATH_CNT_ACTIVE;
  155. }
  156. if (!blk_queue_io_stat(disk->queue) || blk_rq_is_passthrough(rq) ||
  157. (nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
  158. return;
  159. nvme_req(rq)->flags |= NVME_MPATH_IO_STATS;
  160. nvme_req(rq)->start_time = bdev_start_io_acct(disk->part0, req_op(rq),
  161. jiffies);
  162. }
  163. EXPORT_SYMBOL_GPL(nvme_mpath_start_request);
  164. void nvme_mpath_end_request(struct request *rq)
  165. {
  166. struct nvme_ns *ns = rq->q->queuedata;
  167. if (nvme_req(rq)->flags & NVME_MPATH_CNT_ACTIVE)
  168. atomic_dec_if_positive(&ns->ctrl->nr_active);
  169. if (!(nvme_req(rq)->flags & NVME_MPATH_IO_STATS))
  170. return;
  171. bdev_end_io_acct(ns->head->disk->part0, req_op(rq),
  172. blk_rq_bytes(rq) >> SECTOR_SHIFT,
  173. nvme_req(rq)->start_time);
  174. }
  175. void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
  176. {
  177. struct nvme_ns *ns;
  178. int srcu_idx;
  179. srcu_idx = srcu_read_lock(&ctrl->srcu);
  180. list_for_each_entry_srcu(ns, &ctrl->namespaces, list,
  181. srcu_read_lock_held(&ctrl->srcu)) {
  182. if (!ns->head->disk)
  183. continue;
  184. kblockd_schedule_work(&ns->head->requeue_work);
  185. if (nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
  186. disk_uevent(ns->head->disk, KOBJ_CHANGE);
  187. }
  188. srcu_read_unlock(&ctrl->srcu, srcu_idx);
  189. }
  190. static const char *nvme_ana_state_names[] = {
  191. [0] = "invalid state",
  192. [NVME_ANA_OPTIMIZED] = "optimized",
  193. [NVME_ANA_NONOPTIMIZED] = "non-optimized",
  194. [NVME_ANA_INACCESSIBLE] = "inaccessible",
  195. [NVME_ANA_PERSISTENT_LOSS] = "persistent-loss",
  196. [NVME_ANA_CHANGE] = "change",
  197. };
  198. bool nvme_mpath_clear_current_path(struct nvme_ns *ns)
  199. {
  200. struct nvme_ns_head *head = ns->head;
  201. bool changed = false;
  202. int node;
  203. if (!head)
  204. goto out;
  205. for_each_node(node) {
  206. if (ns == rcu_access_pointer(head->current_path[node])) {
  207. rcu_assign_pointer(head->current_path[node], NULL);
  208. changed = true;
  209. }
  210. }
  211. out:
  212. return changed;
  213. }
  214. void nvme_mpath_clear_ctrl_paths(struct nvme_ctrl *ctrl)
  215. {
  216. struct nvme_ns *ns;
  217. int srcu_idx;
  218. srcu_idx = srcu_read_lock(&ctrl->srcu);
  219. list_for_each_entry_srcu(ns, &ctrl->namespaces, list,
  220. srcu_read_lock_held(&ctrl->srcu)) {
  221. nvme_mpath_clear_current_path(ns);
  222. kblockd_schedule_work(&ns->head->requeue_work);
  223. }
  224. srcu_read_unlock(&ctrl->srcu, srcu_idx);
  225. }
  226. void nvme_mpath_revalidate_paths(struct nvme_ns *ns)
  227. {
  228. struct nvme_ns_head *head = ns->head;
  229. sector_t capacity = get_capacity(head->disk);
  230. int node;
  231. int srcu_idx;
  232. srcu_idx = srcu_read_lock(&head->srcu);
  233. list_for_each_entry_srcu(ns, &head->list, siblings,
  234. srcu_read_lock_held(&head->srcu)) {
  235. if (capacity != get_capacity(ns->disk))
  236. clear_bit(NVME_NS_READY, &ns->flags);
  237. }
  238. srcu_read_unlock(&head->srcu, srcu_idx);
  239. for_each_node(node)
  240. rcu_assign_pointer(head->current_path[node], NULL);
  241. kblockd_schedule_work(&head->requeue_work);
  242. }
  243. static bool nvme_path_is_disabled(struct nvme_ns *ns)
  244. {
  245. enum nvme_ctrl_state state = nvme_ctrl_state(ns->ctrl);
  246. /*
  247. * We don't treat NVME_CTRL_DELETING as a disabled path as I/O should
  248. * still be able to complete assuming that the controller is connected.
  249. * Otherwise it will fail immediately and return to the requeue list.
  250. */
  251. if (state != NVME_CTRL_LIVE && state != NVME_CTRL_DELETING)
  252. return true;
  253. if (test_bit(NVME_NS_ANA_PENDING, &ns->flags) ||
  254. !test_bit(NVME_NS_READY, &ns->flags))
  255. return true;
  256. return false;
  257. }
  258. static struct nvme_ns *__nvme_find_path(struct nvme_ns_head *head, int node)
  259. {
  260. int found_distance = INT_MAX, fallback_distance = INT_MAX, distance;
  261. struct nvme_ns *found = NULL, *fallback = NULL, *ns;
  262. list_for_each_entry_srcu(ns, &head->list, siblings,
  263. srcu_read_lock_held(&head->srcu)) {
  264. if (nvme_path_is_disabled(ns))
  265. continue;
  266. if (ns->ctrl->numa_node != NUMA_NO_NODE &&
  267. READ_ONCE(head->subsys->iopolicy) == NVME_IOPOLICY_NUMA)
  268. distance = node_distance(node, ns->ctrl->numa_node);
  269. else
  270. distance = LOCAL_DISTANCE;
  271. switch (ns->ana_state) {
  272. case NVME_ANA_OPTIMIZED:
  273. if (distance < found_distance) {
  274. found_distance = distance;
  275. found = ns;
  276. }
  277. break;
  278. case NVME_ANA_NONOPTIMIZED:
  279. if (distance < fallback_distance) {
  280. fallback_distance = distance;
  281. fallback = ns;
  282. }
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. if (!found)
  289. found = fallback;
  290. if (found)
  291. rcu_assign_pointer(head->current_path[node], found);
  292. return found;
  293. }
  294. static struct nvme_ns *nvme_next_ns(struct nvme_ns_head *head,
  295. struct nvme_ns *ns)
  296. {
  297. ns = list_next_or_null_rcu(&head->list, &ns->siblings, struct nvme_ns,
  298. siblings);
  299. if (ns)
  300. return ns;
  301. return list_first_or_null_rcu(&head->list, struct nvme_ns, siblings);
  302. }
  303. static struct nvme_ns *nvme_round_robin_path(struct nvme_ns_head *head)
  304. {
  305. struct nvme_ns *ns, *found = NULL;
  306. int node = numa_node_id();
  307. struct nvme_ns *old = srcu_dereference(head->current_path[node],
  308. &head->srcu);
  309. if (unlikely(!old))
  310. return __nvme_find_path(head, node);
  311. if (list_is_singular(&head->list)) {
  312. if (nvme_path_is_disabled(old))
  313. return NULL;
  314. return old;
  315. }
  316. for (ns = nvme_next_ns(head, old);
  317. ns && ns != old;
  318. ns = nvme_next_ns(head, ns)) {
  319. if (nvme_path_is_disabled(ns))
  320. continue;
  321. if (ns->ana_state == NVME_ANA_OPTIMIZED) {
  322. found = ns;
  323. goto out;
  324. }
  325. if (ns->ana_state == NVME_ANA_NONOPTIMIZED)
  326. found = ns;
  327. }
  328. /*
  329. * The loop above skips the current path for round-robin semantics.
  330. * Fall back to the current path if either:
  331. * - no other optimized path found and current is optimized,
  332. * - no other usable path found and current is usable.
  333. */
  334. if (!nvme_path_is_disabled(old) &&
  335. (old->ana_state == NVME_ANA_OPTIMIZED ||
  336. (!found && old->ana_state == NVME_ANA_NONOPTIMIZED)))
  337. return old;
  338. if (!found)
  339. return NULL;
  340. out:
  341. rcu_assign_pointer(head->current_path[node], found);
  342. return found;
  343. }
  344. static struct nvme_ns *nvme_queue_depth_path(struct nvme_ns_head *head)
  345. {
  346. struct nvme_ns *best_opt = NULL, *best_nonopt = NULL, *ns;
  347. unsigned int min_depth_opt = UINT_MAX, min_depth_nonopt = UINT_MAX;
  348. unsigned int depth;
  349. list_for_each_entry_srcu(ns, &head->list, siblings,
  350. srcu_read_lock_held(&head->srcu)) {
  351. if (nvme_path_is_disabled(ns))
  352. continue;
  353. depth = atomic_read(&ns->ctrl->nr_active);
  354. switch (ns->ana_state) {
  355. case NVME_ANA_OPTIMIZED:
  356. if (depth < min_depth_opt) {
  357. min_depth_opt = depth;
  358. best_opt = ns;
  359. }
  360. break;
  361. case NVME_ANA_NONOPTIMIZED:
  362. if (depth < min_depth_nonopt) {
  363. min_depth_nonopt = depth;
  364. best_nonopt = ns;
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. if (min_depth_opt == 0)
  371. return best_opt;
  372. }
  373. return best_opt ? best_opt : best_nonopt;
  374. }
  375. static inline bool nvme_path_is_optimized(struct nvme_ns *ns)
  376. {
  377. return nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE &&
  378. ns->ana_state == NVME_ANA_OPTIMIZED;
  379. }
  380. static struct nvme_ns *nvme_numa_path(struct nvme_ns_head *head)
  381. {
  382. int node = numa_node_id();
  383. struct nvme_ns *ns;
  384. ns = srcu_dereference(head->current_path[node], &head->srcu);
  385. if (unlikely(!ns))
  386. return __nvme_find_path(head, node);
  387. if (unlikely(!nvme_path_is_optimized(ns)))
  388. return __nvme_find_path(head, node);
  389. return ns;
  390. }
  391. inline struct nvme_ns *nvme_find_path(struct nvme_ns_head *head)
  392. {
  393. switch (READ_ONCE(head->subsys->iopolicy)) {
  394. case NVME_IOPOLICY_QD:
  395. return nvme_queue_depth_path(head);
  396. case NVME_IOPOLICY_RR:
  397. return nvme_round_robin_path(head);
  398. default:
  399. return nvme_numa_path(head);
  400. }
  401. }
  402. static bool nvme_available_path(struct nvme_ns_head *head)
  403. {
  404. struct nvme_ns *ns;
  405. if (!test_bit(NVME_NSHEAD_DISK_LIVE, &head->flags))
  406. return false;
  407. list_for_each_entry_srcu(ns, &head->list, siblings,
  408. srcu_read_lock_held(&head->srcu)) {
  409. if (test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ns->ctrl->flags))
  410. continue;
  411. switch (nvme_ctrl_state(ns->ctrl)) {
  412. case NVME_CTRL_LIVE:
  413. case NVME_CTRL_RESETTING:
  414. case NVME_CTRL_CONNECTING:
  415. return true;
  416. default:
  417. break;
  418. }
  419. }
  420. /*
  421. * If "head->delayed_removal_secs" is configured (i.e., non-zero), do
  422. * not immediately fail I/O. Instead, requeue the I/O for the configured
  423. * duration, anticipating that if there's a transient link failure then
  424. * it may recover within this time window. This parameter is exported to
  425. * userspace via sysfs, and its default value is zero. It is internally
  426. * mapped to NVME_NSHEAD_QUEUE_IF_NO_PATH. When delayed_removal_secs is
  427. * non-zero, this flag is set to true. When zero, the flag is cleared.
  428. */
  429. return nvme_mpath_queue_if_no_path(head);
  430. }
  431. static void nvme_ns_head_submit_bio(struct bio *bio)
  432. {
  433. struct nvme_ns_head *head = bio->bi_bdev->bd_disk->private_data;
  434. struct device *dev = disk_to_dev(head->disk);
  435. struct nvme_ns *ns;
  436. int srcu_idx;
  437. /*
  438. * The namespace might be going away and the bio might be moved to a
  439. * different queue via blk_steal_bios(), so we need to use the bio_split
  440. * pool from the original queue to allocate the bvecs from.
  441. */
  442. bio = bio_split_to_limits(bio);
  443. if (!bio)
  444. return;
  445. srcu_idx = srcu_read_lock(&head->srcu);
  446. ns = nvme_find_path(head);
  447. if (likely(ns)) {
  448. bio_set_dev(bio, ns->disk->part0);
  449. bio->bi_opf |= REQ_NVME_MPATH;
  450. trace_block_bio_remap(bio, disk_devt(ns->head->disk),
  451. bio->bi_iter.bi_sector);
  452. submit_bio_noacct(bio);
  453. } else if (nvme_available_path(head)) {
  454. dev_warn_ratelimited(dev, "no usable path - requeuing I/O\n");
  455. spin_lock_irq(&head->requeue_lock);
  456. bio_list_add(&head->requeue_list, bio);
  457. spin_unlock_irq(&head->requeue_lock);
  458. } else {
  459. dev_warn_ratelimited(dev, "no available path - failing I/O\n");
  460. bio_io_error(bio);
  461. }
  462. srcu_read_unlock(&head->srcu, srcu_idx);
  463. }
  464. static int nvme_ns_head_open(struct gendisk *disk, blk_mode_t mode)
  465. {
  466. if (!nvme_tryget_ns_head(disk->private_data))
  467. return -ENXIO;
  468. return 0;
  469. }
  470. static void nvme_ns_head_release(struct gendisk *disk)
  471. {
  472. nvme_put_ns_head(disk->private_data);
  473. }
  474. static int nvme_ns_head_get_unique_id(struct gendisk *disk, u8 id[16],
  475. enum blk_unique_id type)
  476. {
  477. struct nvme_ns_head *head = disk->private_data;
  478. struct nvme_ns *ns;
  479. int srcu_idx, ret = -EWOULDBLOCK;
  480. srcu_idx = srcu_read_lock(&head->srcu);
  481. ns = nvme_find_path(head);
  482. if (ns)
  483. ret = nvme_ns_get_unique_id(ns, id, type);
  484. srcu_read_unlock(&head->srcu, srcu_idx);
  485. return ret;
  486. }
  487. #ifdef CONFIG_BLK_DEV_ZONED
  488. static int nvme_ns_head_report_zones(struct gendisk *disk, sector_t sector,
  489. unsigned int nr_zones, struct blk_report_zones_args *args)
  490. {
  491. struct nvme_ns_head *head = disk->private_data;
  492. struct nvme_ns *ns;
  493. int srcu_idx, ret = -EWOULDBLOCK;
  494. srcu_idx = srcu_read_lock(&head->srcu);
  495. ns = nvme_find_path(head);
  496. if (ns)
  497. ret = nvme_ns_report_zones(ns, sector, nr_zones, args);
  498. srcu_read_unlock(&head->srcu, srcu_idx);
  499. return ret;
  500. }
  501. #else
  502. #define nvme_ns_head_report_zones NULL
  503. #endif /* CONFIG_BLK_DEV_ZONED */
  504. const struct block_device_operations nvme_ns_head_ops = {
  505. .owner = THIS_MODULE,
  506. .submit_bio = nvme_ns_head_submit_bio,
  507. .open = nvme_ns_head_open,
  508. .release = nvme_ns_head_release,
  509. .ioctl = nvme_ns_head_ioctl,
  510. .compat_ioctl = blkdev_compat_ptr_ioctl,
  511. .getgeo = nvme_getgeo,
  512. .get_unique_id = nvme_ns_head_get_unique_id,
  513. .report_zones = nvme_ns_head_report_zones,
  514. .pr_ops = &nvme_pr_ops,
  515. };
  516. static inline struct nvme_ns_head *cdev_to_ns_head(struct cdev *cdev)
  517. {
  518. return container_of(cdev, struct nvme_ns_head, cdev);
  519. }
  520. static int nvme_ns_head_chr_open(struct inode *inode, struct file *file)
  521. {
  522. if (!nvme_tryget_ns_head(cdev_to_ns_head(inode->i_cdev)))
  523. return -ENXIO;
  524. return 0;
  525. }
  526. static int nvme_ns_head_chr_release(struct inode *inode, struct file *file)
  527. {
  528. nvme_put_ns_head(cdev_to_ns_head(inode->i_cdev));
  529. return 0;
  530. }
  531. static const struct file_operations nvme_ns_head_chr_fops = {
  532. .owner = THIS_MODULE,
  533. .open = nvme_ns_head_chr_open,
  534. .release = nvme_ns_head_chr_release,
  535. .unlocked_ioctl = nvme_ns_head_chr_ioctl,
  536. .compat_ioctl = compat_ptr_ioctl,
  537. .uring_cmd = nvme_ns_head_chr_uring_cmd,
  538. .uring_cmd_iopoll = nvme_ns_chr_uring_cmd_iopoll,
  539. };
  540. static int nvme_add_ns_head_cdev(struct nvme_ns_head *head)
  541. {
  542. int ret;
  543. head->cdev_device.parent = &head->subsys->dev;
  544. ret = dev_set_name(&head->cdev_device, "ng%dn%d",
  545. head->subsys->instance, head->instance);
  546. if (ret)
  547. return ret;
  548. ret = nvme_cdev_add(&head->cdev, &head->cdev_device,
  549. &nvme_ns_head_chr_fops, THIS_MODULE);
  550. return ret;
  551. }
  552. static void nvme_partition_scan_work(struct work_struct *work)
  553. {
  554. struct nvme_ns_head *head =
  555. container_of(work, struct nvme_ns_head, partition_scan_work);
  556. if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN,
  557. &head->disk->state)))
  558. return;
  559. mutex_lock(&head->disk->open_mutex);
  560. bdev_disk_changed(head->disk, false);
  561. mutex_unlock(&head->disk->open_mutex);
  562. }
  563. static void nvme_requeue_work(struct work_struct *work)
  564. {
  565. struct nvme_ns_head *head =
  566. container_of(work, struct nvme_ns_head, requeue_work);
  567. struct bio *bio, *next;
  568. spin_lock_irq(&head->requeue_lock);
  569. next = bio_list_get(&head->requeue_list);
  570. spin_unlock_irq(&head->requeue_lock);
  571. while ((bio = next) != NULL) {
  572. next = bio->bi_next;
  573. bio->bi_next = NULL;
  574. submit_bio_noacct(bio);
  575. }
  576. }
  577. static void nvme_remove_head(struct nvme_ns_head *head)
  578. {
  579. if (test_and_clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
  580. /*
  581. * requeue I/O after NVME_NSHEAD_DISK_LIVE has been cleared
  582. * to allow multipath to fail all I/O.
  583. */
  584. kblockd_schedule_work(&head->requeue_work);
  585. nvme_cdev_del(&head->cdev, &head->cdev_device);
  586. synchronize_srcu(&head->srcu);
  587. del_gendisk(head->disk);
  588. }
  589. nvme_put_ns_head(head);
  590. }
  591. static void nvme_remove_head_work(struct work_struct *work)
  592. {
  593. struct nvme_ns_head *head = container_of(to_delayed_work(work),
  594. struct nvme_ns_head, remove_work);
  595. bool remove = false;
  596. mutex_lock(&head->subsys->lock);
  597. if (list_empty(&head->list)) {
  598. list_del_init(&head->entry);
  599. remove = true;
  600. }
  601. mutex_unlock(&head->subsys->lock);
  602. if (remove)
  603. nvme_remove_head(head);
  604. module_put(THIS_MODULE);
  605. }
  606. int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)
  607. {
  608. struct queue_limits lim;
  609. mutex_init(&head->lock);
  610. bio_list_init(&head->requeue_list);
  611. spin_lock_init(&head->requeue_lock);
  612. INIT_WORK(&head->requeue_work, nvme_requeue_work);
  613. INIT_WORK(&head->partition_scan_work, nvme_partition_scan_work);
  614. INIT_DELAYED_WORK(&head->remove_work, nvme_remove_head_work);
  615. head->delayed_removal_secs = 0;
  616. /*
  617. * If "multipath_always_on" is enabled, a multipath node is added
  618. * regardless of whether the disk is single/multi ported, and whether
  619. * the namespace is shared or private. If "multipath_always_on" is not
  620. * enabled, a multipath node is added only if the subsystem supports
  621. * multiple controllers and the "multipath" option is configured. In
  622. * either case, for private namespaces, we ensure that the NSID is
  623. * unique.
  624. */
  625. if (!multipath_always_on) {
  626. if (!(ctrl->subsys->cmic & NVME_CTRL_CMIC_MULTI_CTRL) ||
  627. !multipath)
  628. return 0;
  629. }
  630. if (!nvme_is_unique_nsid(ctrl, head))
  631. return 0;
  632. blk_set_stacking_limits(&lim);
  633. lim.dma_alignment = 3;
  634. lim.features |= BLK_FEAT_IO_STAT | BLK_FEAT_NOWAIT |
  635. BLK_FEAT_POLL | BLK_FEAT_ATOMIC_WRITES;
  636. if (head->ids.csi == NVME_CSI_ZNS)
  637. lim.features |= BLK_FEAT_ZONED;
  638. head->disk = blk_alloc_disk(&lim, ctrl->numa_node);
  639. if (IS_ERR(head->disk))
  640. return PTR_ERR(head->disk);
  641. head->disk->fops = &nvme_ns_head_ops;
  642. head->disk->private_data = head;
  643. /*
  644. * We need to suppress the partition scan from occuring within the
  645. * controller's scan_work context. If a path error occurs here, the IO
  646. * will wait until a path becomes available or all paths are torn down,
  647. * but that action also occurs within scan_work, so it would deadlock.
  648. * Defer the partition scan to a different context that does not block
  649. * scan_work.
  650. */
  651. set_bit(GD_SUPPRESS_PART_SCAN, &head->disk->state);
  652. sprintf(head->disk->disk_name, "nvme%dn%d",
  653. ctrl->subsys->instance, head->instance);
  654. nvme_tryget_ns_head(head);
  655. return 0;
  656. }
  657. static void nvme_mpath_set_live(struct nvme_ns *ns)
  658. {
  659. struct nvme_ns_head *head = ns->head;
  660. int rc;
  661. if (!head->disk)
  662. return;
  663. /*
  664. * test_and_set_bit() is used because it is protecting against two nvme
  665. * paths simultaneously calling device_add_disk() on the same namespace
  666. * head.
  667. */
  668. if (!test_and_set_bit(NVME_NSHEAD_DISK_LIVE, &head->flags)) {
  669. rc = device_add_disk(&head->subsys->dev, head->disk,
  670. nvme_ns_attr_groups);
  671. if (rc) {
  672. clear_bit(NVME_NSHEAD_DISK_LIVE, &head->flags);
  673. return;
  674. }
  675. nvme_add_ns_head_cdev(head);
  676. queue_work(nvme_wq, &head->partition_scan_work);
  677. }
  678. nvme_mpath_add_sysfs_link(ns->head);
  679. mutex_lock(&head->lock);
  680. if (nvme_path_is_optimized(ns)) {
  681. int node, srcu_idx;
  682. srcu_idx = srcu_read_lock(&head->srcu);
  683. for_each_online_node(node)
  684. __nvme_find_path(head, node);
  685. srcu_read_unlock(&head->srcu, srcu_idx);
  686. }
  687. mutex_unlock(&head->lock);
  688. synchronize_srcu(&head->srcu);
  689. kblockd_schedule_work(&head->requeue_work);
  690. }
  691. static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
  692. int (*cb)(struct nvme_ctrl *ctrl, struct nvme_ana_group_desc *,
  693. void *))
  694. {
  695. void *base = ctrl->ana_log_buf;
  696. size_t offset = sizeof(struct nvme_ana_rsp_hdr);
  697. int error, i;
  698. lockdep_assert_held(&ctrl->ana_lock);
  699. for (i = 0; i < le16_to_cpu(ctrl->ana_log_buf->ngrps); i++) {
  700. struct nvme_ana_group_desc *desc = base + offset;
  701. u32 nr_nsids;
  702. size_t nsid_buf_size;
  703. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
  704. return -EINVAL;
  705. nr_nsids = le32_to_cpu(desc->nnsids);
  706. nsid_buf_size = flex_array_size(desc, nsids, nr_nsids);
  707. if (WARN_ON_ONCE(desc->grpid == 0))
  708. return -EINVAL;
  709. if (WARN_ON_ONCE(le32_to_cpu(desc->grpid) > ctrl->anagrpmax))
  710. return -EINVAL;
  711. if (WARN_ON_ONCE(desc->state == 0))
  712. return -EINVAL;
  713. if (WARN_ON_ONCE(desc->state > NVME_ANA_CHANGE))
  714. return -EINVAL;
  715. offset += sizeof(*desc);
  716. if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
  717. return -EINVAL;
  718. error = cb(ctrl, desc, data);
  719. if (error)
  720. return error;
  721. offset += nsid_buf_size;
  722. }
  723. return 0;
  724. }
  725. static inline bool nvme_state_is_live(enum nvme_ana_state state)
  726. {
  727. return state == NVME_ANA_OPTIMIZED || state == NVME_ANA_NONOPTIMIZED;
  728. }
  729. static void nvme_update_ns_ana_state(struct nvme_ana_group_desc *desc,
  730. struct nvme_ns *ns)
  731. {
  732. ns->ana_grpid = le32_to_cpu(desc->grpid);
  733. ns->ana_state = desc->state;
  734. clear_bit(NVME_NS_ANA_PENDING, &ns->flags);
  735. /*
  736. * nvme_mpath_set_live() will trigger I/O to the multipath path device
  737. * and in turn to this path device. However we cannot accept this I/O
  738. * if the controller is not live. This may deadlock if called from
  739. * nvme_mpath_init_identify() and the ctrl will never complete
  740. * initialization, preventing I/O from completing. For this case we
  741. * will reprocess the ANA log page in nvme_mpath_update() once the
  742. * controller is ready.
  743. */
  744. if (nvme_state_is_live(ns->ana_state) &&
  745. nvme_ctrl_state(ns->ctrl) == NVME_CTRL_LIVE)
  746. nvme_mpath_set_live(ns);
  747. else {
  748. /*
  749. * Add sysfs link from multipath head gendisk node to path
  750. * device gendisk node.
  751. * If path's ana state is live (i.e. state is either optimized
  752. * or non-optimized) while we alloc the ns then sysfs link would
  753. * be created from nvme_mpath_set_live(). In that case we would
  754. * not fallthrough this code path. However for the path's ana
  755. * state other than live, we call nvme_mpath_set_live() only
  756. * after ana state transitioned to the live state. But we still
  757. * want to create the sysfs link from head node to a path device
  758. * irrespctive of the path's ana state.
  759. * If we reach through here then it means that path's ana state
  760. * is not live but still create the sysfs link to this path from
  761. * head node if head node of the path has already come alive.
  762. */
  763. if (test_bit(NVME_NSHEAD_DISK_LIVE, &ns->head->flags))
  764. nvme_mpath_add_sysfs_link(ns->head);
  765. }
  766. }
  767. static int nvme_update_ana_state(struct nvme_ctrl *ctrl,
  768. struct nvme_ana_group_desc *desc, void *data)
  769. {
  770. u32 nr_nsids = le32_to_cpu(desc->nnsids), n = 0;
  771. unsigned *nr_change_groups = data;
  772. struct nvme_ns *ns;
  773. int srcu_idx;
  774. dev_dbg(ctrl->device, "ANA group %d: %s.\n",
  775. le32_to_cpu(desc->grpid),
  776. nvme_ana_state_names[desc->state]);
  777. if (desc->state == NVME_ANA_CHANGE)
  778. (*nr_change_groups)++;
  779. if (!nr_nsids)
  780. return 0;
  781. srcu_idx = srcu_read_lock(&ctrl->srcu);
  782. list_for_each_entry_srcu(ns, &ctrl->namespaces, list,
  783. srcu_read_lock_held(&ctrl->srcu)) {
  784. unsigned nsid;
  785. again:
  786. nsid = le32_to_cpu(desc->nsids[n]);
  787. if (ns->head->ns_id < nsid)
  788. continue;
  789. if (ns->head->ns_id == nsid)
  790. nvme_update_ns_ana_state(desc, ns);
  791. if (++n == nr_nsids)
  792. break;
  793. if (ns->head->ns_id > nsid)
  794. goto again;
  795. }
  796. srcu_read_unlock(&ctrl->srcu, srcu_idx);
  797. return 0;
  798. }
  799. static int nvme_read_ana_log(struct nvme_ctrl *ctrl)
  800. {
  801. u32 nr_change_groups = 0;
  802. int error;
  803. mutex_lock(&ctrl->ana_lock);
  804. error = nvme_get_log(ctrl, NVME_NSID_ALL, NVME_LOG_ANA, 0, NVME_CSI_NVM,
  805. ctrl->ana_log_buf, ctrl->ana_log_size, 0);
  806. if (error) {
  807. dev_warn(ctrl->device, "Failed to get ANA log: %d\n", error);
  808. goto out_unlock;
  809. }
  810. error = nvme_parse_ana_log(ctrl, &nr_change_groups,
  811. nvme_update_ana_state);
  812. if (error)
  813. goto out_unlock;
  814. /*
  815. * In theory we should have an ANATT timer per group as they might enter
  816. * the change state at different times. But that is a lot of overhead
  817. * just to protect against a target that keeps entering new changes
  818. * states while never finishing previous ones. But we'll still
  819. * eventually time out once all groups are in change state, so this
  820. * isn't a big deal.
  821. *
  822. * We also double the ANATT value to provide some slack for transports
  823. * or AEN processing overhead.
  824. */
  825. if (nr_change_groups)
  826. mod_timer(&ctrl->anatt_timer, ctrl->anatt * HZ * 2 + jiffies);
  827. else
  828. timer_delete_sync(&ctrl->anatt_timer);
  829. out_unlock:
  830. mutex_unlock(&ctrl->ana_lock);
  831. return error;
  832. }
  833. static void nvme_ana_work(struct work_struct *work)
  834. {
  835. struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ana_work);
  836. if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE)
  837. return;
  838. nvme_read_ana_log(ctrl);
  839. }
  840. void nvme_mpath_update(struct nvme_ctrl *ctrl)
  841. {
  842. u32 nr_change_groups = 0;
  843. if (!ctrl->ana_log_buf)
  844. return;
  845. mutex_lock(&ctrl->ana_lock);
  846. nvme_parse_ana_log(ctrl, &nr_change_groups, nvme_update_ana_state);
  847. mutex_unlock(&ctrl->ana_lock);
  848. }
  849. static void nvme_anatt_timeout(struct timer_list *t)
  850. {
  851. struct nvme_ctrl *ctrl = timer_container_of(ctrl, t, anatt_timer);
  852. dev_info(ctrl->device, "ANATT timeout, resetting controller.\n");
  853. nvme_reset_ctrl(ctrl);
  854. }
  855. void nvme_mpath_stop(struct nvme_ctrl *ctrl)
  856. {
  857. if (!nvme_ctrl_use_ana(ctrl))
  858. return;
  859. timer_delete_sync(&ctrl->anatt_timer);
  860. cancel_work_sync(&ctrl->ana_work);
  861. }
  862. #define SUBSYS_ATTR_RW(_name, _mode, _show, _store) \
  863. struct device_attribute subsys_attr_##_name = \
  864. __ATTR(_name, _mode, _show, _store)
  865. static ssize_t nvme_subsys_iopolicy_show(struct device *dev,
  866. struct device_attribute *attr, char *buf)
  867. {
  868. struct nvme_subsystem *subsys =
  869. container_of(dev, struct nvme_subsystem, dev);
  870. return sysfs_emit(buf, "%s\n",
  871. nvme_iopolicy_names[READ_ONCE(subsys->iopolicy)]);
  872. }
  873. static void nvme_subsys_iopolicy_update(struct nvme_subsystem *subsys,
  874. int iopolicy)
  875. {
  876. struct nvme_ctrl *ctrl;
  877. int old_iopolicy = READ_ONCE(subsys->iopolicy);
  878. if (old_iopolicy == iopolicy)
  879. return;
  880. WRITE_ONCE(subsys->iopolicy, iopolicy);
  881. /* iopolicy changes clear the mpath by design */
  882. mutex_lock(&nvme_subsystems_lock);
  883. list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
  884. nvme_mpath_clear_ctrl_paths(ctrl);
  885. mutex_unlock(&nvme_subsystems_lock);
  886. pr_notice("subsysnqn %s iopolicy changed from %s to %s\n",
  887. subsys->subnqn,
  888. nvme_iopolicy_names[old_iopolicy],
  889. nvme_iopolicy_names[iopolicy]);
  890. }
  891. static ssize_t nvme_subsys_iopolicy_store(struct device *dev,
  892. struct device_attribute *attr, const char *buf, size_t count)
  893. {
  894. struct nvme_subsystem *subsys =
  895. container_of(dev, struct nvme_subsystem, dev);
  896. int i;
  897. for (i = 0; i < ARRAY_SIZE(nvme_iopolicy_names); i++) {
  898. if (sysfs_streq(buf, nvme_iopolicy_names[i])) {
  899. nvme_subsys_iopolicy_update(subsys, i);
  900. return count;
  901. }
  902. }
  903. return -EINVAL;
  904. }
  905. SUBSYS_ATTR_RW(iopolicy, S_IRUGO | S_IWUSR,
  906. nvme_subsys_iopolicy_show, nvme_subsys_iopolicy_store);
  907. static ssize_t ana_grpid_show(struct device *dev, struct device_attribute *attr,
  908. char *buf)
  909. {
  910. return sysfs_emit(buf, "%d\n", nvme_get_ns_from_dev(dev)->ana_grpid);
  911. }
  912. DEVICE_ATTR_RO(ana_grpid);
  913. static ssize_t ana_state_show(struct device *dev, struct device_attribute *attr,
  914. char *buf)
  915. {
  916. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  917. return sysfs_emit(buf, "%s\n", nvme_ana_state_names[ns->ana_state]);
  918. }
  919. DEVICE_ATTR_RO(ana_state);
  920. static ssize_t queue_depth_show(struct device *dev,
  921. struct device_attribute *attr, char *buf)
  922. {
  923. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  924. if (ns->head->subsys->iopolicy != NVME_IOPOLICY_QD)
  925. return 0;
  926. return sysfs_emit(buf, "%d\n", atomic_read(&ns->ctrl->nr_active));
  927. }
  928. DEVICE_ATTR_RO(queue_depth);
  929. static ssize_t numa_nodes_show(struct device *dev, struct device_attribute *attr,
  930. char *buf)
  931. {
  932. int node, srcu_idx;
  933. nodemask_t numa_nodes;
  934. struct nvme_ns *current_ns;
  935. struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
  936. struct nvme_ns_head *head = ns->head;
  937. if (head->subsys->iopolicy != NVME_IOPOLICY_NUMA)
  938. return 0;
  939. nodes_clear(numa_nodes);
  940. srcu_idx = srcu_read_lock(&head->srcu);
  941. for_each_node(node) {
  942. current_ns = srcu_dereference(head->current_path[node],
  943. &head->srcu);
  944. if (ns == current_ns)
  945. node_set(node, numa_nodes);
  946. }
  947. srcu_read_unlock(&head->srcu, srcu_idx);
  948. return sysfs_emit(buf, "%*pbl\n", nodemask_pr_args(&numa_nodes));
  949. }
  950. DEVICE_ATTR_RO(numa_nodes);
  951. static ssize_t delayed_removal_secs_show(struct device *dev,
  952. struct device_attribute *attr, char *buf)
  953. {
  954. struct gendisk *disk = dev_to_disk(dev);
  955. struct nvme_ns_head *head = disk->private_data;
  956. int ret;
  957. mutex_lock(&head->subsys->lock);
  958. ret = sysfs_emit(buf, "%u\n", head->delayed_removal_secs);
  959. mutex_unlock(&head->subsys->lock);
  960. return ret;
  961. }
  962. static ssize_t delayed_removal_secs_store(struct device *dev,
  963. struct device_attribute *attr, const char *buf, size_t count)
  964. {
  965. struct gendisk *disk = dev_to_disk(dev);
  966. struct nvme_ns_head *head = disk->private_data;
  967. unsigned int sec;
  968. int ret;
  969. ret = kstrtouint(buf, 0, &sec);
  970. if (ret < 0)
  971. return ret;
  972. mutex_lock(&head->subsys->lock);
  973. head->delayed_removal_secs = sec;
  974. if (sec)
  975. set_bit(NVME_NSHEAD_QUEUE_IF_NO_PATH, &head->flags);
  976. else
  977. clear_bit(NVME_NSHEAD_QUEUE_IF_NO_PATH, &head->flags);
  978. mutex_unlock(&head->subsys->lock);
  979. /*
  980. * Ensure that update to NVME_NSHEAD_QUEUE_IF_NO_PATH is seen
  981. * by its reader.
  982. */
  983. synchronize_srcu(&head->srcu);
  984. return count;
  985. }
  986. DEVICE_ATTR_RW(delayed_removal_secs);
  987. static int nvme_lookup_ana_group_desc(struct nvme_ctrl *ctrl,
  988. struct nvme_ana_group_desc *desc, void *data)
  989. {
  990. struct nvme_ana_group_desc *dst = data;
  991. if (desc->grpid != dst->grpid)
  992. return 0;
  993. *dst = *desc;
  994. return -ENXIO; /* just break out of the loop */
  995. }
  996. void nvme_mpath_add_sysfs_link(struct nvme_ns_head *head)
  997. {
  998. struct device *target;
  999. int rc, srcu_idx;
  1000. struct nvme_ns *ns;
  1001. struct kobject *kobj;
  1002. /*
  1003. * Ensure head disk node is already added otherwise we may get invalid
  1004. * kobj for head disk node
  1005. */
  1006. if (!test_bit(GD_ADDED, &head->disk->state))
  1007. return;
  1008. kobj = &disk_to_dev(head->disk)->kobj;
  1009. /*
  1010. * loop through each ns chained through the head->list and create the
  1011. * sysfs link from head node to the ns path node
  1012. */
  1013. srcu_idx = srcu_read_lock(&head->srcu);
  1014. list_for_each_entry_srcu(ns, &head->list, siblings,
  1015. srcu_read_lock_held(&head->srcu)) {
  1016. /*
  1017. * Ensure that ns path disk node is already added otherwise we
  1018. * may get invalid kobj name for target
  1019. */
  1020. if (!test_bit(GD_ADDED, &ns->disk->state))
  1021. continue;
  1022. /*
  1023. * Avoid creating link if it already exists for the given path.
  1024. * When path ana state transitions from optimized to non-
  1025. * optimized or vice-versa, the nvme_mpath_set_live() is
  1026. * invoked which in truns call this function. Now if the sysfs
  1027. * link already exists for the given path and we attempt to re-
  1028. * create the link then sysfs code would warn about it loudly.
  1029. * So we evaluate NVME_NS_SYSFS_ATTR_LINK flag here to ensure
  1030. * that we're not creating duplicate link.
  1031. * The test_and_set_bit() is used because it is protecting
  1032. * against multiple nvme paths being simultaneously added.
  1033. */
  1034. if (test_and_set_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags))
  1035. continue;
  1036. target = disk_to_dev(ns->disk);
  1037. /*
  1038. * Create sysfs link from head gendisk kobject @kobj to the
  1039. * ns path gendisk kobject @target->kobj.
  1040. */
  1041. rc = sysfs_add_link_to_group(kobj, nvme_ns_mpath_attr_group.name,
  1042. &target->kobj, dev_name(target));
  1043. if (unlikely(rc)) {
  1044. dev_err(disk_to_dev(ns->head->disk),
  1045. "failed to create link to %s\n",
  1046. dev_name(target));
  1047. clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags);
  1048. }
  1049. }
  1050. srcu_read_unlock(&head->srcu, srcu_idx);
  1051. }
  1052. void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns)
  1053. {
  1054. struct device *target;
  1055. struct kobject *kobj;
  1056. if (!test_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags))
  1057. return;
  1058. target = disk_to_dev(ns->disk);
  1059. kobj = &disk_to_dev(ns->head->disk)->kobj;
  1060. sysfs_remove_link_from_group(kobj, nvme_ns_mpath_attr_group.name,
  1061. dev_name(target));
  1062. clear_bit(NVME_NS_SYSFS_ATTR_LINK, &ns->flags);
  1063. }
  1064. void nvme_mpath_add_disk(struct nvme_ns *ns, __le32 anagrpid)
  1065. {
  1066. if (nvme_ctrl_use_ana(ns->ctrl)) {
  1067. struct nvme_ana_group_desc desc = {
  1068. .grpid = anagrpid,
  1069. .state = 0,
  1070. };
  1071. mutex_lock(&ns->ctrl->ana_lock);
  1072. ns->ana_grpid = le32_to_cpu(anagrpid);
  1073. nvme_parse_ana_log(ns->ctrl, &desc, nvme_lookup_ana_group_desc);
  1074. mutex_unlock(&ns->ctrl->ana_lock);
  1075. if (desc.state) {
  1076. /* found the group desc: update */
  1077. nvme_update_ns_ana_state(&desc, ns);
  1078. } else {
  1079. /* group desc not found: trigger a re-read */
  1080. set_bit(NVME_NS_ANA_PENDING, &ns->flags);
  1081. queue_work(nvme_wq, &ns->ctrl->ana_work);
  1082. }
  1083. } else {
  1084. ns->ana_state = NVME_ANA_OPTIMIZED;
  1085. nvme_mpath_set_live(ns);
  1086. }
  1087. #ifdef CONFIG_BLK_DEV_ZONED
  1088. if (blk_queue_is_zoned(ns->queue) && ns->head->disk)
  1089. ns->head->disk->nr_zones = ns->disk->nr_zones;
  1090. #endif
  1091. }
  1092. void nvme_mpath_remove_disk(struct nvme_ns_head *head)
  1093. {
  1094. bool remove = false;
  1095. if (!head->disk)
  1096. return;
  1097. mutex_lock(&head->subsys->lock);
  1098. /*
  1099. * We are called when all paths have been removed, and at that point
  1100. * head->list is expected to be empty. However, nvme_ns_remove() and
  1101. * nvme_init_ns_head() can run concurrently and so if head->delayed_
  1102. * removal_secs is configured, it is possible that by the time we reach
  1103. * this point, head->list may no longer be empty. Therefore, we recheck
  1104. * head->list here. If it is no longer empty then we skip enqueuing the
  1105. * delayed head removal work.
  1106. */
  1107. if (!list_empty(&head->list))
  1108. goto out;
  1109. /*
  1110. * Ensure that no one could remove this module while the head
  1111. * remove work is pending.
  1112. */
  1113. if (head->delayed_removal_secs && try_module_get(THIS_MODULE)) {
  1114. mod_delayed_work(nvme_wq, &head->remove_work,
  1115. head->delayed_removal_secs * HZ);
  1116. } else {
  1117. list_del_init(&head->entry);
  1118. remove = true;
  1119. }
  1120. out:
  1121. mutex_unlock(&head->subsys->lock);
  1122. if (remove)
  1123. nvme_remove_head(head);
  1124. }
  1125. void nvme_mpath_put_disk(struct nvme_ns_head *head)
  1126. {
  1127. if (!head->disk)
  1128. return;
  1129. /* make sure all pending bios are cleaned up */
  1130. kblockd_schedule_work(&head->requeue_work);
  1131. flush_work(&head->requeue_work);
  1132. flush_work(&head->partition_scan_work);
  1133. put_disk(head->disk);
  1134. }
  1135. void nvme_mpath_init_ctrl(struct nvme_ctrl *ctrl)
  1136. {
  1137. mutex_init(&ctrl->ana_lock);
  1138. timer_setup(&ctrl->anatt_timer, nvme_anatt_timeout, 0);
  1139. INIT_WORK(&ctrl->ana_work, nvme_ana_work);
  1140. }
  1141. int nvme_mpath_init_identify(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
  1142. {
  1143. size_t max_transfer_size = ctrl->max_hw_sectors << SECTOR_SHIFT;
  1144. size_t ana_log_size;
  1145. int error = 0;
  1146. /* check if multipath is enabled and we have the capability */
  1147. if (!multipath || !ctrl->subsys ||
  1148. !(ctrl->subsys->cmic & NVME_CTRL_CMIC_ANA))
  1149. return 0;
  1150. /* initialize this in the identify path to cover controller resets */
  1151. atomic_set(&ctrl->nr_active, 0);
  1152. if (!ctrl->max_namespaces ||
  1153. ctrl->max_namespaces > le32_to_cpu(id->nn)) {
  1154. dev_err(ctrl->device,
  1155. "Invalid MNAN value %u\n", ctrl->max_namespaces);
  1156. return -EINVAL;
  1157. }
  1158. ctrl->anacap = id->anacap;
  1159. ctrl->anatt = id->anatt;
  1160. ctrl->nanagrpid = le32_to_cpu(id->nanagrpid);
  1161. ctrl->anagrpmax = le32_to_cpu(id->anagrpmax);
  1162. ana_log_size = sizeof(struct nvme_ana_rsp_hdr) +
  1163. ctrl->nanagrpid * sizeof(struct nvme_ana_group_desc) +
  1164. ctrl->max_namespaces * sizeof(__le32);
  1165. if (ana_log_size > max_transfer_size) {
  1166. dev_err(ctrl->device,
  1167. "ANA log page size (%zd) larger than MDTS (%zd).\n",
  1168. ana_log_size, max_transfer_size);
  1169. dev_err(ctrl->device, "disabling ANA support.\n");
  1170. goto out_uninit;
  1171. }
  1172. if (ana_log_size > ctrl->ana_log_size) {
  1173. nvme_mpath_stop(ctrl);
  1174. nvme_mpath_uninit(ctrl);
  1175. ctrl->ana_log_buf = kvmalloc(ana_log_size, GFP_KERNEL);
  1176. if (!ctrl->ana_log_buf)
  1177. return -ENOMEM;
  1178. }
  1179. ctrl->ana_log_size = ana_log_size;
  1180. error = nvme_read_ana_log(ctrl);
  1181. if (error)
  1182. goto out_uninit;
  1183. return 0;
  1184. out_uninit:
  1185. nvme_mpath_uninit(ctrl);
  1186. return error;
  1187. }
  1188. void nvme_mpath_uninit(struct nvme_ctrl *ctrl)
  1189. {
  1190. kvfree(ctrl->ana_log_buf);
  1191. ctrl->ana_log_buf = NULL;
  1192. ctrl->ana_log_size = 0;
  1193. }