@@ -576,6 +576,11 @@ static void nvme_free_ns(struct kref *kref)
576576 kfree (ns );
577577}
578578
579+ static inline bool nvme_get_ns (struct nvme_ns * ns )
580+ {
581+ return kref_get_unless_zero (& ns -> kref );
582+ }
583+
579584void nvme_put_ns (struct nvme_ns * ns )
580585{
581586 kref_put (& ns -> kref , nvme_free_ns );
@@ -584,9 +589,6 @@ EXPORT_SYMBOL_NS_GPL(nvme_put_ns, NVME_TARGET_PASSTHRU);
584589
585590static inline void nvme_clear_nvme_request (struct request * req )
586591{
587- struct nvme_command * cmd = nvme_req (req )-> cmd ;
588-
589- memset (cmd , 0 , sizeof (* cmd ));
590592 nvme_req (req )-> retries = 0 ;
591593 nvme_req (req )-> flags = 0 ;
592594 req -> rq_flags |= RQF_DONTPREP ;
@@ -637,6 +639,66 @@ static struct request *nvme_alloc_request_qid(struct request_queue *q,
637639 return req ;
638640}
639641
642+ /*
643+ * For something we're not in a state to send to the device the default action
644+ * is to busy it and retry it after the controller state is recovered. However,
645+ * if the controller is deleting or if anything is marked for failfast or
646+ * nvme multipath it is immediately failed.
647+ *
648+ * Note: commands used to initialize the controller will be marked for failfast.
649+ * Note: nvme cli/ioctl commands are marked for failfast.
650+ */
651+ blk_status_t nvme_fail_nonready_command (struct nvme_ctrl * ctrl ,
652+ struct request * rq )
653+ {
654+ if (ctrl -> state != NVME_CTRL_DELETING_NOIO &&
655+ ctrl -> state != NVME_CTRL_DEAD &&
656+ !test_bit (NVME_CTRL_FAILFAST_EXPIRED , & ctrl -> flags ) &&
657+ !blk_noretry_request (rq ) && !(rq -> cmd_flags & REQ_NVME_MPATH ))
658+ return BLK_STS_RESOURCE ;
659+ return nvme_host_path_error (rq );
660+ }
661+ EXPORT_SYMBOL_GPL (nvme_fail_nonready_command );
662+
663+ bool __nvme_check_ready (struct nvme_ctrl * ctrl , struct request * rq ,
664+ bool queue_live )
665+ {
666+ struct nvme_request * req = nvme_req (rq );
667+
668+ /*
669+ * currently we have a problem sending passthru commands
670+ * on the admin_q if the controller is not LIVE because we can't
671+ * make sure that they are going out after the admin connect,
672+ * controller enable and/or other commands in the initialization
673+ * sequence. until the controller will be LIVE, fail with
674+ * BLK_STS_RESOURCE so that they will be rescheduled.
675+ */
676+ if (rq -> q == ctrl -> admin_q && (req -> flags & NVME_REQ_USERCMD ))
677+ return false;
678+
679+ if (ctrl -> ops -> flags & NVME_F_FABRICS ) {
680+ /*
681+ * Only allow commands on a live queue, except for the connect
682+ * command, which is require to set the queue live in the
683+ * appropinquate states.
684+ */
685+ switch (ctrl -> state ) {
686+ case NVME_CTRL_CONNECTING :
687+ if (blk_rq_is_passthrough (rq ) && nvme_is_fabrics (req -> cmd ) &&
688+ req -> cmd -> fabrics .fctype == nvme_fabrics_type_connect )
689+ return true;
690+ break ;
691+ default :
692+ break ;
693+ case NVME_CTRL_DEAD :
694+ return false;
695+ }
696+ }
697+
698+ return queue_live ;
699+ }
700+ EXPORT_SYMBOL_GPL (__nvme_check_ready );
701+
640702static int nvme_toggle_streams (struct nvme_ctrl * ctrl , bool enable )
641703{
642704 struct nvme_command c ;
@@ -898,8 +960,10 @@ blk_status_t nvme_setup_cmd(struct nvme_ns *ns, struct request *req)
898960 struct nvme_command * cmd = nvme_req (req )-> cmd ;
899961 blk_status_t ret = BLK_STS_OK ;
900962
901- if (!(req -> rq_flags & RQF_DONTPREP ))
963+ if (!(req -> rq_flags & RQF_DONTPREP )) {
902964 nvme_clear_nvme_request (req );
965+ memset (cmd , 0 , sizeof (* cmd ));
966+ }
903967
904968 switch (req_op (req )) {
905969 case REQ_OP_DRV_IN :
@@ -1494,7 +1558,7 @@ static int nvme_ns_open(struct nvme_ns *ns)
14941558 /* should never be called due to GENHD_FL_HIDDEN */
14951559 if (WARN_ON_ONCE (nvme_ns_head_multipath (ns -> head )))
14961560 goto fail ;
1497- if (!kref_get_unless_zero ( & ns -> kref ))
1561+ if (!nvme_get_ns ( ns ))
14981562 goto fail ;
14991563 if (!try_module_get (ns -> ctrl -> ops -> module ))
15001564 goto fail_put_ns ;
@@ -1999,28 +2063,6 @@ static const struct block_device_operations nvme_bdev_ops = {
19992063 .pr_ops = & nvme_pr_ops ,
20002064};
20012065
2002- #ifdef CONFIG_NVME_MULTIPATH
2003- struct nvme_ctrl * nvme_find_get_live_ctrl (struct nvme_subsystem * subsys )
2004- {
2005- struct nvme_ctrl * ctrl ;
2006- int ret ;
2007-
2008- ret = mutex_lock_killable (& nvme_subsystems_lock );
2009- if (ret )
2010- return ERR_PTR (ret );
2011- list_for_each_entry (ctrl , & subsys -> ctrls , subsys_entry ) {
2012- if (ctrl -> state == NVME_CTRL_LIVE )
2013- goto found ;
2014- }
2015- mutex_unlock (& nvme_subsystems_lock );
2016- return ERR_PTR (- EWOULDBLOCK );
2017- found :
2018- nvme_get_ctrl (ctrl );
2019- mutex_unlock (& nvme_subsystems_lock );
2020- return ctrl ;
2021- }
2022- #endif /* CONFIG_NVME_MULTIPATH */
2023-
20242066static int nvme_wait_ready (struct nvme_ctrl * ctrl , u64 cap , bool enabled )
20252067{
20262068 unsigned long timeout =
@@ -3604,7 +3646,7 @@ struct nvme_ns *nvme_find_get_ns(struct nvme_ctrl *ctrl, unsigned nsid)
36043646 down_read (& ctrl -> namespaces_rwsem );
36053647 list_for_each_entry (ns , & ctrl -> namespaces , list ) {
36063648 if (ns -> head -> ns_id == nsid ) {
3607- if (!kref_get_unless_zero ( & ns -> kref ))
3649+ if (!nvme_get_ns ( ns ))
36083650 continue ;
36093651 ret = ns ;
36103652 break ;
0 commit comments