.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /******************************************************************************* |
---|
2 | 3 | * Filename: target_core_tmr.c |
---|
3 | 4 | * |
---|
.. | .. |
---|
6 | 7 | * (c) Copyright 2009-2013 Datera, Inc. |
---|
7 | 8 | * |
---|
8 | 9 | * Nicholas A. Bellinger <nab@kernel.org> |
---|
9 | | - * |
---|
10 | | - * This program is free software; you can redistribute it and/or modify |
---|
11 | | - * it under the terms of the GNU General Public License as published by |
---|
12 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
13 | | - * (at your option) any later version. |
---|
14 | | - * |
---|
15 | | - * This program is distributed in the hope that it will be useful, |
---|
16 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | | - * GNU General Public License for more details. |
---|
19 | | - * |
---|
20 | | - * You should have received a copy of the GNU General Public License |
---|
21 | | - * along with this program; if not, write to the Free Software |
---|
22 | | - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
23 | 10 | * |
---|
24 | 11 | ******************************************************************************/ |
---|
25 | 12 | |
---|
.. | .. |
---|
91 | 78 | } |
---|
92 | 79 | |
---|
93 | 80 | static bool __target_check_io_state(struct se_cmd *se_cmd, |
---|
94 | | - struct se_session *tmr_sess, int tas) |
---|
| 81 | + struct se_session *tmr_sess, bool tas) |
---|
95 | 82 | { |
---|
96 | 83 | struct se_session *sess = se_cmd->se_sess; |
---|
97 | 84 | |
---|
98 | | - assert_spin_locked(&sess->sess_cmd_lock); |
---|
99 | | - WARN_ON_ONCE(!irqs_disabled()); |
---|
| 85 | + lockdep_assert_held(&sess->sess_cmd_lock); |
---|
| 86 | + |
---|
100 | 87 | /* |
---|
101 | 88 | * If command already reached CMD_T_COMPLETE state within |
---|
102 | 89 | * target_complete_cmd() or CMD_T_FABRIC_STOP due to shutdown, |
---|
.. | .. |
---|
111 | 98 | if (se_cmd->transport_state & (CMD_T_COMPLETE | CMD_T_FABRIC_STOP)) { |
---|
112 | 99 | pr_debug("Attempted to abort io tag: %llu already complete or" |
---|
113 | 100 | " fabric stop, skipping\n", se_cmd->tag); |
---|
114 | | - spin_unlock(&se_cmd->t_state_lock); |
---|
115 | | - return false; |
---|
116 | | - } |
---|
117 | | - if (se_cmd->transport_state & CMD_T_PRE_EXECUTE) { |
---|
118 | | - if (se_cmd->scsi_status) { |
---|
119 | | - pr_debug("Attempted to abort io tag: %llu early failure" |
---|
120 | | - " status: 0x%02x\n", se_cmd->tag, |
---|
121 | | - se_cmd->scsi_status); |
---|
122 | | - spin_unlock(&se_cmd->t_state_lock); |
---|
123 | | - return false; |
---|
124 | | - } |
---|
125 | | - } |
---|
126 | | - if (sess->sess_tearing_down) { |
---|
127 | | - pr_debug("Attempted to abort io tag: %llu already shutdown," |
---|
128 | | - " skipping\n", se_cmd->tag); |
---|
129 | 101 | spin_unlock(&se_cmd->t_state_lock); |
---|
130 | 102 | return false; |
---|
131 | 103 | } |
---|
.. | .. |
---|
144 | 116 | struct se_tmr_req *tmr, |
---|
145 | 117 | struct se_session *se_sess) |
---|
146 | 118 | { |
---|
147 | | - struct se_cmd *se_cmd; |
---|
| 119 | + LIST_HEAD(aborted_list); |
---|
| 120 | + struct se_cmd *se_cmd, *next; |
---|
148 | 121 | unsigned long flags; |
---|
| 122 | + bool rc; |
---|
149 | 123 | u64 ref_tag; |
---|
| 124 | + int i; |
---|
150 | 125 | |
---|
151 | | - spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
---|
152 | | - list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) { |
---|
| 126 | + for (i = 0; i < dev->queue_cnt; i++) { |
---|
| 127 | + spin_lock_irqsave(&dev->queues[i].lock, flags); |
---|
| 128 | + list_for_each_entry_safe(se_cmd, next, &dev->queues[i].state_list, |
---|
| 129 | + state_list) { |
---|
| 130 | + if (se_sess != se_cmd->se_sess) |
---|
| 131 | + continue; |
---|
153 | 132 | |
---|
154 | | - if (dev != se_cmd->se_dev) |
---|
155 | | - continue; |
---|
| 133 | + /* |
---|
| 134 | + * skip task management functions, including |
---|
| 135 | + * tmr->task_cmd |
---|
| 136 | + */ |
---|
| 137 | + if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
---|
| 138 | + continue; |
---|
156 | 139 | |
---|
157 | | - /* skip task management functions, including tmr->task_cmd */ |
---|
158 | | - if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
---|
159 | | - continue; |
---|
| 140 | + ref_tag = se_cmd->tag; |
---|
| 141 | + if (tmr->ref_task_tag != ref_tag) |
---|
| 142 | + continue; |
---|
160 | 143 | |
---|
161 | | - ref_tag = se_cmd->tag; |
---|
162 | | - if (tmr->ref_task_tag != ref_tag) |
---|
163 | | - continue; |
---|
| 144 | + pr_err("ABORT_TASK: Found referenced %s task_tag: %llu\n", |
---|
| 145 | + se_cmd->se_tfo->fabric_name, ref_tag); |
---|
164 | 146 | |
---|
165 | | - printk("ABORT_TASK: Found referenced %s task_tag: %llu\n", |
---|
166 | | - se_cmd->se_tfo->get_fabric_name(), ref_tag); |
---|
| 147 | + spin_lock(&se_sess->sess_cmd_lock); |
---|
| 148 | + rc = __target_check_io_state(se_cmd, se_sess, 0); |
---|
| 149 | + spin_unlock(&se_sess->sess_cmd_lock); |
---|
| 150 | + if (!rc) |
---|
| 151 | + continue; |
---|
167 | 152 | |
---|
168 | | - if (!__target_check_io_state(se_cmd, se_sess, 0)) |
---|
169 | | - continue; |
---|
| 153 | + list_move_tail(&se_cmd->state_list, &aborted_list); |
---|
| 154 | + se_cmd->state_active = false; |
---|
| 155 | + spin_unlock_irqrestore(&dev->queues[i].lock, flags); |
---|
170 | 156 | |
---|
171 | | - spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
---|
| 157 | + /* |
---|
| 158 | + * Ensure that this ABORT request is visible to the LU |
---|
| 159 | + * RESET code. |
---|
| 160 | + */ |
---|
| 161 | + if (!tmr->tmr_dev) |
---|
| 162 | + WARN_ON_ONCE(transport_lookup_tmr_lun(tmr->task_cmd) < 0); |
---|
172 | 163 | |
---|
173 | | - cancel_work_sync(&se_cmd->work); |
---|
174 | | - transport_wait_for_tasks(se_cmd); |
---|
| 164 | + if (dev->transport->tmr_notify) |
---|
| 165 | + dev->transport->tmr_notify(dev, TMR_ABORT_TASK, |
---|
| 166 | + &aborted_list); |
---|
175 | 167 | |
---|
176 | | - if (!transport_cmd_finish_abort(se_cmd)) |
---|
177 | | - target_put_sess_cmd(se_cmd); |
---|
| 168 | + list_del_init(&se_cmd->state_list); |
---|
| 169 | + target_put_cmd_and_wait(se_cmd); |
---|
178 | 170 | |
---|
179 | | - printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for" |
---|
180 | | - " ref_tag: %llu\n", ref_tag); |
---|
181 | | - tmr->response = TMR_FUNCTION_COMPLETE; |
---|
182 | | - atomic_long_inc(&dev->aborts_complete); |
---|
183 | | - return; |
---|
| 171 | + pr_err("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for ref_tag: %llu\n", |
---|
| 172 | + ref_tag); |
---|
| 173 | + tmr->response = TMR_FUNCTION_COMPLETE; |
---|
| 174 | + atomic_long_inc(&dev->aborts_complete); |
---|
| 175 | + return; |
---|
| 176 | + } |
---|
| 177 | + spin_unlock_irqrestore(&dev->queues[i].lock, flags); |
---|
184 | 178 | } |
---|
185 | | - spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
---|
| 179 | + |
---|
| 180 | + if (dev->transport->tmr_notify) |
---|
| 181 | + dev->transport->tmr_notify(dev, TMR_ABORT_TASK, &aborted_list); |
---|
186 | 182 | |
---|
187 | 183 | printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %lld\n", |
---|
188 | 184 | tmr->ref_task_tag); |
---|
.. | .. |
---|
206 | 202 | * LUN_RESET tmr.. |
---|
207 | 203 | */ |
---|
208 | 204 | spin_lock_irqsave(&dev->se_tmr_lock, flags); |
---|
209 | | - if (tmr) |
---|
210 | | - list_del_init(&tmr->tmr_list); |
---|
211 | 205 | list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) { |
---|
| 206 | + if (tmr_p == tmr) |
---|
| 207 | + continue; |
---|
| 208 | + |
---|
212 | 209 | cmd = tmr_p->task_cmd; |
---|
213 | 210 | if (!cmd) { |
---|
214 | 211 | pr_err("Unable to locate struct se_cmd for TMR\n"); |
---|
215 | 212 | continue; |
---|
216 | 213 | } |
---|
| 214 | + |
---|
| 215 | + /* |
---|
| 216 | + * We only execute one LUN_RESET at a time so we can't wait |
---|
| 217 | + * on them below. |
---|
| 218 | + */ |
---|
| 219 | + if (tmr_p->function == TMR_LUN_RESET) |
---|
| 220 | + continue; |
---|
| 221 | + |
---|
217 | 222 | /* |
---|
218 | 223 | * If this function was called with a valid pr_res_key |
---|
219 | 224 | * parameter (eg: for PROUT PREEMPT_AND_ABORT service action |
---|
.. | .. |
---|
227 | 232 | continue; |
---|
228 | 233 | |
---|
229 | 234 | spin_lock(&sess->sess_cmd_lock); |
---|
230 | | - spin_lock(&cmd->t_state_lock); |
---|
231 | | - if (!(cmd->transport_state & CMD_T_ACTIVE) || |
---|
232 | | - (cmd->transport_state & CMD_T_FABRIC_STOP)) { |
---|
233 | | - spin_unlock(&cmd->t_state_lock); |
---|
234 | | - spin_unlock(&sess->sess_cmd_lock); |
---|
235 | | - continue; |
---|
236 | | - } |
---|
237 | | - if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) { |
---|
238 | | - spin_unlock(&cmd->t_state_lock); |
---|
239 | | - spin_unlock(&sess->sess_cmd_lock); |
---|
240 | | - continue; |
---|
241 | | - } |
---|
242 | | - if (sess->sess_tearing_down) { |
---|
243 | | - spin_unlock(&cmd->t_state_lock); |
---|
244 | | - spin_unlock(&sess->sess_cmd_lock); |
---|
245 | | - continue; |
---|
246 | | - } |
---|
247 | | - cmd->transport_state |= CMD_T_ABORTED; |
---|
248 | | - spin_unlock(&cmd->t_state_lock); |
---|
| 235 | + rc = __target_check_io_state(cmd, sess, 0); |
---|
| 236 | + spin_unlock(&sess->sess_cmd_lock); |
---|
249 | 237 | |
---|
250 | | - rc = kref_get_unless_zero(&cmd->cmd_kref); |
---|
251 | 238 | if (!rc) { |
---|
252 | 239 | printk("LUN_RESET TMR: non-zero kref_get_unless_zero\n"); |
---|
253 | | - spin_unlock(&sess->sess_cmd_lock); |
---|
254 | 240 | continue; |
---|
255 | 241 | } |
---|
256 | | - spin_unlock(&sess->sess_cmd_lock); |
---|
257 | 242 | |
---|
258 | 243 | list_move_tail(&tmr_p->tmr_list, &drain_tmr_list); |
---|
259 | 244 | } |
---|
.. | .. |
---|
268 | 253 | (preempt_and_abort_list) ? "Preempt" : "", tmr_p, |
---|
269 | 254 | tmr_p->function, tmr_p->response, cmd->t_state); |
---|
270 | 255 | |
---|
271 | | - cancel_work_sync(&cmd->work); |
---|
272 | | - transport_wait_for_tasks(cmd); |
---|
273 | | - |
---|
274 | | - if (!transport_cmd_finish_abort(cmd)) |
---|
275 | | - target_put_sess_cmd(cmd); |
---|
| 256 | + target_put_cmd_and_wait(cmd); |
---|
276 | 257 | } |
---|
277 | 258 | } |
---|
278 | 259 | |
---|
| 260 | +/** |
---|
| 261 | + * core_tmr_drain_state_list() - abort SCSI commands associated with a device |
---|
| 262 | + * |
---|
| 263 | + * @dev: Device for which to abort outstanding SCSI commands. |
---|
| 264 | + * @prout_cmd: Pointer to the SCSI PREEMPT AND ABORT if this function is called |
---|
| 265 | + * to realize the PREEMPT AND ABORT functionality. |
---|
| 266 | + * @tmr_sess: Session through which the LUN RESET has been received. |
---|
| 267 | + * @tas: Task Aborted Status (TAS) bit from the SCSI control mode page. |
---|
| 268 | + * A quote from SPC-4, paragraph "7.5.10 Control mode page": |
---|
| 269 | + * "A task aborted status (TAS) bit set to zero specifies that |
---|
| 270 | + * aborted commands shall be terminated by the device server |
---|
| 271 | + * without any response to the application client. A TAS bit set |
---|
| 272 | + * to one specifies that commands aborted by the actions of an I_T |
---|
| 273 | + * nexus other than the I_T nexus on which the command was |
---|
| 274 | + * received shall be completed with TASK ABORTED status." |
---|
| 275 | + * @preempt_and_abort_list: For the PREEMPT AND ABORT functionality, a list |
---|
| 276 | + * with registrations that will be preempted. |
---|
| 277 | + */ |
---|
279 | 278 | static void core_tmr_drain_state_list( |
---|
280 | 279 | struct se_device *dev, |
---|
281 | 280 | struct se_cmd *prout_cmd, |
---|
282 | 281 | struct se_session *tmr_sess, |
---|
283 | | - int tas, |
---|
| 282 | + bool tas, |
---|
284 | 283 | struct list_head *preempt_and_abort_list) |
---|
285 | 284 | { |
---|
286 | 285 | LIST_HEAD(drain_task_list); |
---|
287 | 286 | struct se_session *sess; |
---|
288 | 287 | struct se_cmd *cmd, *next; |
---|
289 | 288 | unsigned long flags; |
---|
290 | | - int rc; |
---|
| 289 | + int rc, i; |
---|
291 | 290 | |
---|
292 | 291 | /* |
---|
293 | 292 | * Complete outstanding commands with TASK_ABORTED SAM status. |
---|
.. | .. |
---|
311 | 310 | * Note that this seems to be independent of TAS (Task Aborted Status) |
---|
312 | 311 | * in the Control Mode Page. |
---|
313 | 312 | */ |
---|
314 | | - spin_lock_irqsave(&dev->execute_task_lock, flags); |
---|
315 | | - list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) { |
---|
316 | | - /* |
---|
317 | | - * For PREEMPT_AND_ABORT usage, only process commands |
---|
318 | | - * with a matching reservation key. |
---|
319 | | - */ |
---|
320 | | - if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd)) |
---|
321 | | - continue; |
---|
| 313 | + for (i = 0; i < dev->queue_cnt; i++) { |
---|
| 314 | + spin_lock_irqsave(&dev->queues[i].lock, flags); |
---|
| 315 | + list_for_each_entry_safe(cmd, next, &dev->queues[i].state_list, |
---|
| 316 | + state_list) { |
---|
| 317 | + /* |
---|
| 318 | + * For PREEMPT_AND_ABORT usage, only process commands |
---|
| 319 | + * with a matching reservation key. |
---|
| 320 | + */ |
---|
| 321 | + if (target_check_cdb_and_preempt(preempt_and_abort_list, |
---|
| 322 | + cmd)) |
---|
| 323 | + continue; |
---|
322 | 324 | |
---|
323 | | - /* |
---|
324 | | - * Not aborting PROUT PREEMPT_AND_ABORT CDB.. |
---|
325 | | - */ |
---|
326 | | - if (prout_cmd == cmd) |
---|
327 | | - continue; |
---|
| 325 | + /* |
---|
| 326 | + * Not aborting PROUT PREEMPT_AND_ABORT CDB.. |
---|
| 327 | + */ |
---|
| 328 | + if (prout_cmd == cmd) |
---|
| 329 | + continue; |
---|
328 | 330 | |
---|
329 | | - sess = cmd->se_sess; |
---|
330 | | - if (WARN_ON_ONCE(!sess)) |
---|
331 | | - continue; |
---|
| 331 | + sess = cmd->se_sess; |
---|
| 332 | + if (WARN_ON_ONCE(!sess)) |
---|
| 333 | + continue; |
---|
332 | 334 | |
---|
333 | | - spin_lock(&sess->sess_cmd_lock); |
---|
334 | | - rc = __target_check_io_state(cmd, tmr_sess, tas); |
---|
335 | | - spin_unlock(&sess->sess_cmd_lock); |
---|
336 | | - if (!rc) |
---|
337 | | - continue; |
---|
| 335 | + spin_lock(&sess->sess_cmd_lock); |
---|
| 336 | + rc = __target_check_io_state(cmd, tmr_sess, tas); |
---|
| 337 | + spin_unlock(&sess->sess_cmd_lock); |
---|
| 338 | + if (!rc) |
---|
| 339 | + continue; |
---|
338 | 340 | |
---|
339 | | - list_move_tail(&cmd->state_list, &drain_task_list); |
---|
340 | | - cmd->state_active = false; |
---|
| 341 | + list_move_tail(&cmd->state_list, &drain_task_list); |
---|
| 342 | + cmd->state_active = false; |
---|
| 343 | + } |
---|
| 344 | + spin_unlock_irqrestore(&dev->queues[i].lock, flags); |
---|
341 | 345 | } |
---|
342 | | - spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
---|
| 346 | + |
---|
| 347 | + if (dev->transport->tmr_notify) |
---|
| 348 | + dev->transport->tmr_notify(dev, preempt_and_abort_list ? |
---|
| 349 | + TMR_LUN_RESET_PRO : TMR_LUN_RESET, |
---|
| 350 | + &drain_task_list); |
---|
343 | 351 | |
---|
344 | 352 | while (!list_empty(&drain_task_list)) { |
---|
345 | 353 | cmd = list_entry(drain_task_list.next, struct se_cmd, state_list); |
---|
.. | .. |
---|
350 | 358 | cmd->tag, (preempt_and_abort_list) ? "preempt" : "", |
---|
351 | 359 | cmd->pr_res_key); |
---|
352 | 360 | |
---|
353 | | - /* |
---|
354 | | - * If the command may be queued onto a workqueue cancel it now. |
---|
355 | | - * |
---|
356 | | - * This is equivalent to removal from the execute queue in the |
---|
357 | | - * loop above, but we do it down here given that |
---|
358 | | - * cancel_work_sync may block. |
---|
359 | | - */ |
---|
360 | | - cancel_work_sync(&cmd->work); |
---|
361 | | - transport_wait_for_tasks(cmd); |
---|
362 | | - |
---|
363 | | - if (!transport_cmd_finish_abort(cmd)) |
---|
364 | | - target_put_sess_cmd(cmd); |
---|
| 361 | + target_put_cmd_and_wait(cmd); |
---|
365 | 362 | } |
---|
366 | 363 | } |
---|
367 | 364 | |
---|
.. | .. |
---|
374 | 371 | struct se_node_acl *tmr_nacl = NULL; |
---|
375 | 372 | struct se_portal_group *tmr_tpg = NULL; |
---|
376 | 373 | struct se_session *tmr_sess = NULL; |
---|
377 | | - int tas; |
---|
| 374 | + bool tas; |
---|
378 | 375 | /* |
---|
379 | 376 | * TASK_ABORTED status bit, this is configurable via ConfigFS |
---|
380 | 377 | * struct se_device attributes. spc4r17 section 7.4.6 Control mode page |
---|
.. | .. |
---|
398 | 395 | if (tmr_nacl && tmr_tpg) { |
---|
399 | 396 | pr_debug("LUN_RESET: TMR caller fabric: %s" |
---|
400 | 397 | " initiator port %s\n", |
---|
401 | | - tmr_tpg->se_tpg_tfo->get_fabric_name(), |
---|
| 398 | + tmr_tpg->se_tpg_tfo->fabric_name, |
---|
402 | 399 | tmr_nacl->initiatorname); |
---|
403 | 400 | } |
---|
404 | 401 | } |
---|
| 402 | + |
---|
| 403 | + |
---|
| 404 | + /* |
---|
| 405 | + * We only allow one reset or preempt and abort to execute at a time |
---|
| 406 | + * to prevent one call from claiming all the cmds causing a second |
---|
| 407 | + * call from returning while cmds it should have waited on are still |
---|
| 408 | + * running. |
---|
| 409 | + */ |
---|
| 410 | + mutex_lock(&dev->lun_reset_mutex); |
---|
| 411 | + |
---|
405 | 412 | pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n", |
---|
406 | 413 | (preempt_and_abort_list) ? "Preempt" : "TMR", |
---|
407 | 414 | dev->transport->name, tas); |
---|
408 | | - |
---|
409 | 415 | core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list); |
---|
410 | 416 | core_tmr_drain_state_list(dev, prout_cmd, tmr_sess, tas, |
---|
411 | 417 | preempt_and_abort_list); |
---|
| 418 | + |
---|
| 419 | + mutex_unlock(&dev->lun_reset_mutex); |
---|
412 | 420 | |
---|
413 | 421 | /* |
---|
414 | 422 | * Clear any legacy SPC-2 reservation when called during |
---|
.. | .. |
---|
417 | 425 | if (!preempt_and_abort_list && |
---|
418 | 426 | (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) { |
---|
419 | 427 | spin_lock(&dev->dev_reservation_lock); |
---|
420 | | - dev->dev_reserved_node_acl = NULL; |
---|
| 428 | + dev->reservation_holder = NULL; |
---|
421 | 429 | dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS; |
---|
422 | 430 | spin_unlock(&dev->dev_reservation_lock); |
---|
423 | 431 | pr_debug("LUN_RESET: SCSI-2 Released reservation\n"); |
---|