| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * fault injection support for nvme. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (c) 2018, Oracle and/or its affiliates |
|---|
| 5 | | - * |
|---|
| 6 | 6 | */ |
|---|
| 7 | 7 | |
|---|
| 8 | 8 | #include <linux/moduleparam.h> |
|---|
| .. | .. |
|---|
| 15 | 15 | static char *fail_request; |
|---|
| 16 | 16 | module_param(fail_request, charp, 0000); |
|---|
| 17 | 17 | |
|---|
| 18 | | -void nvme_fault_inject_init(struct nvme_ns *ns) |
|---|
| 18 | +void nvme_fault_inject_init(struct nvme_fault_inject *fault_inj, |
|---|
| 19 | + const char *dev_name) |
|---|
| 19 | 20 | { |
|---|
| 20 | 21 | struct dentry *dir, *parent; |
|---|
| 21 | | - char *name = ns->disk->disk_name; |
|---|
| 22 | | - struct nvme_fault_inject *fault_inj = &ns->fault_inject; |
|---|
| 23 | 22 | struct fault_attr *attr = &fault_inj->attr; |
|---|
| 24 | 23 | |
|---|
| 25 | 24 | /* set default fault injection attribute */ |
|---|
| .. | .. |
|---|
| 27 | 26 | setup_fault_attr(&fail_default_attr, fail_request); |
|---|
| 28 | 27 | |
|---|
| 29 | 28 | /* create debugfs directory and attribute */ |
|---|
| 30 | | - parent = debugfs_create_dir(name, NULL); |
|---|
| 29 | + parent = debugfs_create_dir(dev_name, NULL); |
|---|
| 31 | 30 | if (!parent) { |
|---|
| 32 | | - pr_warn("%s: failed to create debugfs directory\n", name); |
|---|
| 31 | + pr_warn("%s: failed to create debugfs directory\n", dev_name); |
|---|
| 33 | 32 | return; |
|---|
| 34 | 33 | } |
|---|
| 35 | 34 | |
|---|
| 36 | 35 | *attr = fail_default_attr; |
|---|
| 37 | 36 | dir = fault_create_debugfs_attr("fault_inject", parent, attr); |
|---|
| 38 | 37 | if (IS_ERR(dir)) { |
|---|
| 39 | | - pr_warn("%s: failed to create debugfs attr\n", name); |
|---|
| 38 | + pr_warn("%s: failed to create debugfs attr\n", dev_name); |
|---|
| 40 | 39 | debugfs_remove_recursive(parent); |
|---|
| 41 | 40 | return; |
|---|
| 42 | 41 | } |
|---|
| 43 | | - ns->fault_inject.parent = parent; |
|---|
| 42 | + fault_inj->parent = parent; |
|---|
| 44 | 43 | |
|---|
| 45 | 44 | /* create debugfs for status code and dont_retry */ |
|---|
| 46 | 45 | fault_inj->status = NVME_SC_INVALID_OPCODE; |
|---|
| .. | .. |
|---|
| 49 | 48 | debugfs_create_bool("dont_retry", 0600, dir, &fault_inj->dont_retry); |
|---|
| 50 | 49 | } |
|---|
| 51 | 50 | |
|---|
| 52 | | -void nvme_fault_inject_fini(struct nvme_ns *ns) |
|---|
| 51 | +void nvme_fault_inject_fini(struct nvme_fault_inject *fault_inject) |
|---|
| 53 | 52 | { |
|---|
| 54 | 53 | /* remove debugfs directories */ |
|---|
| 55 | | - debugfs_remove_recursive(ns->fault_inject.parent); |
|---|
| 54 | + debugfs_remove_recursive(fault_inject->parent); |
|---|
| 56 | 55 | } |
|---|
| 57 | 56 | |
|---|
| 58 | 57 | void nvme_should_fail(struct request *req) |
|---|
| 59 | 58 | { |
|---|
| 60 | 59 | struct gendisk *disk = req->rq_disk; |
|---|
| 61 | | - struct nvme_ns *ns = NULL; |
|---|
| 60 | + struct nvme_fault_inject *fault_inject = NULL; |
|---|
| 62 | 61 | u16 status; |
|---|
| 63 | 62 | |
|---|
| 64 | | - /* |
|---|
| 65 | | - * make sure this request is coming from a valid namespace |
|---|
| 66 | | - */ |
|---|
| 67 | | - if (!disk) |
|---|
| 68 | | - return; |
|---|
| 63 | + if (disk) { |
|---|
| 64 | + struct nvme_ns *ns = disk->private_data; |
|---|
| 69 | 65 | |
|---|
| 70 | | - ns = disk->private_data; |
|---|
| 71 | | - if (ns && should_fail(&ns->fault_inject.attr, 1)) { |
|---|
| 66 | + if (ns) |
|---|
| 67 | + fault_inject = &ns->fault_inject; |
|---|
| 68 | + else |
|---|
| 69 | + WARN_ONCE(1, "No namespace found for request\n"); |
|---|
| 70 | + } else { |
|---|
| 71 | + fault_inject = &nvme_req(req)->ctrl->fault_inject; |
|---|
| 72 | + } |
|---|
| 73 | + |
|---|
| 74 | + if (fault_inject && should_fail(&fault_inject->attr, 1)) { |
|---|
| 72 | 75 | /* inject status code and DNR bit */ |
|---|
| 73 | | - status = ns->fault_inject.status; |
|---|
| 74 | | - if (ns->fault_inject.dont_retry) |
|---|
| 76 | + status = fault_inject->status; |
|---|
| 77 | + if (fault_inject->dont_retry) |
|---|
| 75 | 78 | status |= NVME_SC_DNR; |
|---|
| 76 | 79 | nvme_req(req)->status = status; |
|---|
| 77 | 80 | } |
|---|