blob: 83d2e6860d388bfb05be3b8813b8d0ea2733aaf8 [file] [log] [blame]
Christoph Hellwigbc50ad72019-02-18 09:36:29 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Taib9e03852018-02-08 13:38:29 -05002/*
3 * fault injection support for nvme.
4 *
5 * Copyright (c) 2018, Oracle and/or its affiliates
Thomas Taib9e03852018-02-08 13:38:29 -05006 */
7
8#include <linux/moduleparam.h>
9#include "nvme.h"
10
11static DECLARE_FAULT_ATTR(fail_default_attr);
12/* optional fault injection attributes boot time option:
13 * nvme_core.fail_request=<interval>,<probability>,<space>,<times>
14 */
15static char *fail_request;
16module_param(fail_request, charp, 0000);
17
Akinobu Mitaa3646452019-06-20 08:49:02 +020018void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj,
19 const char *dev_name)
Thomas Taib9e03852018-02-08 13:38:29 -050020{
21 struct dentry *dir, *parent;
Thomas Taib9e03852018-02-08 13:38:29 -050022 struct fault_attr *attr = &fault_inj->attr;
23
24 /* set default fault injection attribute */
25 if (fail_request)
26 setup_fault_attr(&fail_default_attr, fail_request);
27
28 /* create debugfs directory and attribute */
Akinobu Mitaa3646452019-06-20 08:49:02 +020029 parent = debugfs_create_dir(dev_name, NULL);
Thomas Taib9e03852018-02-08 13:38:29 -050030 if (!parent) {
Akinobu Mitaa3646452019-06-20 08:49:02 +020031 pr_warn("%s: failed to create debugfs directory\n", dev_name);
Thomas Taib9e03852018-02-08 13:38:29 -050032 return;
33 }
34
35 *attr = fail_default_attr;
36 dir = fault_create_debugfs_attr("fault_inject", parent, attr);
37 if (IS_ERR(dir)) {
Akinobu Mitaa3646452019-06-20 08:49:02 +020038 pr_warn("%s: failed to create debugfs attr\n", dev_name);
Thomas Taib9e03852018-02-08 13:38:29 -050039 debugfs_remove_recursive(parent);
40 return;
41 }
Akinobu Mitaa3646452019-06-20 08:49:02 +020042 fault_inj->parent = parent;
Thomas Taib9e03852018-02-08 13:38:29 -050043
44 /* create debugfs for status code and dont_retry */
45 fault_inj->status = NVME_SC_INVALID_OPCODE;
46 fault_inj->dont_retry = true;
47 debugfs_create_x16("status", 0600, dir, &fault_inj->status);
48 debugfs_create_bool("dont_retry", 0600, dir, &fault_inj->dont_retry);
49}
50
Akinobu Mitaa3646452019-06-20 08:49:02 +020051void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject)
Thomas Taib9e03852018-02-08 13:38:29 -050052{
53 /* remove debugfs directories */
Akinobu Mitaa3646452019-06-20 08:49:02 +020054 debugfs_remove_recursive(fault_inject->parent);
Thomas Taib9e03852018-02-08 13:38:29 -050055}
56
57void nvme_should_fail(struct request *req)
58{
Christoph Hellwigf3fa33a2021-11-26 13:18:00 +010059 struct gendisk *disk = req->q->disk;
Akinobu Mitaa3646452019-06-20 08:49:02 +020060 struct nvme_fault_inject *fault_inject = NULL;
Thomas Taib9e03852018-02-08 13:38:29 -050061 u16 status;
62
Akinobu Mitaa3646452019-06-20 08:49:02 +020063 if (disk) {
64 struct nvme_ns *ns = disk->private_data;
Thomas Taib9e03852018-02-08 13:38:29 -050065
Akinobu Mitaa3646452019-06-20 08:49:02 +020066 if (ns)
67 fault_inject = &ns->fault_inject;
68 else
69 WARN_ONCE(1, "No namespace found for request\n");
Akinobu Mitaf79d5fd2019-06-09 23:17:01 +090070 } else {
71 fault_inject = &nvme_req(req)->ctrl->fault_inject;
Akinobu Mitaa3646452019-06-20 08:49:02 +020072 }
73
74 if (fault_inject && should_fail(&fault_inject->attr, 1)) {
Thomas Taib9e03852018-02-08 13:38:29 -050075 /* inject status code and DNR bit */
Akinobu Mitaa3646452019-06-20 08:49:02 +020076 status = fault_inject->status;
77 if (fault_inject->dont_retry)
Thomas Taib9e03852018-02-08 13:38:29 -050078 status |= NVME_SC_DNR;
79 nvme_req(req)->status = status;
80 }
81}
82EXPORT_SYMBOL_GPL(nvme_should_fail);