hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/block/bsg.c
....@@ -1,13 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * bsg.c - block layer implementation of the sg v4 interface
3
- *
4
- * Copyright (C) 2004 Jens Axboe <axboe@suse.de> SUSE Labs
5
- * Copyright (C) 2004 Peter M. Jones <pjones@redhat.com>
6
- *
7
- * This file is subject to the terms and conditions of the GNU General Public
8
- * License version 2. See the file "COPYING" in the main directory of this
9
- * archive for more details.
10
- *
114 */
125 #include <linux/module.h>
136 #include <linux/init.h>
....@@ -19,6 +12,7 @@
1912 #include <linux/idr.h>
2013 #include <linux/bsg.h>
2114 #include <linux/slab.h>
15
+#include <linux/pm_runtime.h>
2216
2317 #include <scsi/scsi.h>
2418 #include <scsi/scsi_ioctl.h>
....@@ -74,6 +68,11 @@
7468 {
7569 struct scsi_request *sreq = scsi_req(rq);
7670
71
+ if (hdr->dout_xfer_len && hdr->din_xfer_len) {
72
+ pr_warn_once("BIDI support in bsg has been removed.\n");
73
+ return -EOPNOTSUPP;
74
+ }
75
+
7776 sreq->cmd_len = hdr->request_len;
7877 if (sreq->cmd_len > BLK_MAX_CDB) {
7978 sreq->cmd = kzalloc(sreq->cmd_len, GFP_KERNEL);
....@@ -114,14 +113,10 @@
114113 hdr->response_len = len;
115114 }
116115
117
- if (rq->next_rq) {
118
- hdr->dout_resid = sreq->resid_len;
119
- hdr->din_resid = scsi_req(rq->next_rq)->resid_len;
120
- } else if (rq_data_dir(rq) == READ) {
116
+ if (rq_data_dir(rq) == READ)
121117 hdr->din_resid = sreq->resid_len;
122
- } else {
118
+ else
123119 hdr->dout_resid = sreq->resid_len;
124
- }
125120
126121 return ret;
127122 }
....@@ -138,32 +133,37 @@
138133 .free_rq = bsg_scsi_free_rq,
139134 };
140135
141
-static struct request *
142
-bsg_map_hdr(struct request_queue *q, struct sg_io_v4 *hdr, fmode_t mode)
136
+static int bsg_sg_io(struct request_queue *q, fmode_t mode, void __user *uarg)
143137 {
144
- struct request *rq, *next_rq = NULL;
138
+ struct request *rq;
139
+ struct bio *bio;
140
+ struct sg_io_v4 hdr;
145141 int ret;
146142
143
+ if (copy_from_user(&hdr, uarg, sizeof(hdr)))
144
+ return -EFAULT;
145
+
147146 if (!q->bsg_dev.class_dev)
148
- return ERR_PTR(-ENXIO);
147
+ return -ENXIO;
149148
150
- if (hdr->guard != 'Q')
151
- return ERR_PTR(-EINVAL);
152
-
153
- ret = q->bsg_dev.ops->check_proto(hdr);
149
+ if (hdr.guard != 'Q')
150
+ return -EINVAL;
151
+ ret = q->bsg_dev.ops->check_proto(&hdr);
154152 if (ret)
155
- return ERR_PTR(ret);
153
+ return ret;
156154
157
- rq = blk_get_request(q, hdr->dout_xfer_len ?
155
+ rq = blk_get_request(q, hdr.dout_xfer_len ?
158156 REQ_OP_SCSI_OUT : REQ_OP_SCSI_IN, 0);
159157 if (IS_ERR(rq))
160
- return rq;
158
+ return PTR_ERR(rq);
161159
162
- ret = q->bsg_dev.ops->fill_hdr(rq, hdr, mode);
163
- if (ret)
164
- goto out;
160
+ ret = q->bsg_dev.ops->fill_hdr(rq, &hdr, mode);
161
+ if (ret) {
162
+ blk_put_request(rq);
163
+ return ret;
164
+ }
165165
166
- rq->timeout = msecs_to_jiffies(hdr->timeout);
166
+ rq->timeout = msecs_to_jiffies(hdr.timeout);
167167 if (!rq->timeout)
168168 rq->timeout = q->sg_timeout;
169169 if (!rq->timeout)
....@@ -171,64 +171,28 @@
171171 if (rq->timeout < BLK_MIN_SG_TIMEOUT)
172172 rq->timeout = BLK_MIN_SG_TIMEOUT;
173173
174
- if (hdr->dout_xfer_len && hdr->din_xfer_len) {
175
- if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
176
- ret = -EOPNOTSUPP;
177
- goto out;
178
- }
179
-
180
- next_rq = blk_get_request(q, REQ_OP_SCSI_IN, 0);
181
- if (IS_ERR(next_rq)) {
182
- ret = PTR_ERR(next_rq);
183
- goto out;
184
- }
185
-
186
- rq->next_rq = next_rq;
187
- ret = blk_rq_map_user(q, next_rq, NULL, uptr64(hdr->din_xferp),
188
- hdr->din_xfer_len, GFP_KERNEL);
189
- if (ret)
190
- goto out_free_nextrq;
191
- }
192
-
193
- if (hdr->dout_xfer_len) {
194
- ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->dout_xferp),
195
- hdr->dout_xfer_len, GFP_KERNEL);
196
- } else if (hdr->din_xfer_len) {
197
- ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr->din_xferp),
198
- hdr->din_xfer_len, GFP_KERNEL);
174
+ if (hdr.dout_xfer_len) {
175
+ ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.dout_xferp),
176
+ hdr.dout_xfer_len, GFP_KERNEL);
177
+ } else if (hdr.din_xfer_len) {
178
+ ret = blk_rq_map_user(q, rq, NULL, uptr64(hdr.din_xferp),
179
+ hdr.din_xfer_len, GFP_KERNEL);
199180 }
200181
201182 if (ret)
202
- goto out_unmap_nextrq;
203
- return rq;
183
+ goto out_free_rq;
204184
205
-out_unmap_nextrq:
206
- if (rq->next_rq)
207
- blk_rq_unmap_user(rq->next_rq->bio);
208
-out_free_nextrq:
209
- if (rq->next_rq)
210
- blk_put_request(rq->next_rq);
211
-out:
212
- q->bsg_dev.ops->free_rq(rq);
213
- blk_put_request(rq);
214
- return ERR_PTR(ret);
215
-}
185
+ bio = rq->bio;
216186
217
-static int blk_complete_sgv4_hdr_rq(struct request *rq, struct sg_io_v4 *hdr,
218
- struct bio *bio, struct bio *bidi_bio)
219
-{
220
- int ret;
221
-
222
- ret = rq->q->bsg_dev.ops->complete_rq(rq, hdr);
223
-
224
- if (rq->next_rq) {
225
- blk_rq_unmap_user(bidi_bio);
226
- blk_put_request(rq->next_rq);
227
- }
228
-
187
+ blk_execute_rq(q, NULL, rq, !(hdr.flags & BSG_FLAG_Q_AT_TAIL));
188
+ ret = rq->q->bsg_dev.ops->complete_rq(rq, &hdr);
229189 blk_rq_unmap_user(bio);
190
+
191
+out_free_rq:
230192 rq->q->bsg_dev.ops->free_rq(rq);
231193 blk_put_request(rq);
194
+ if (!ret && copy_to_user(uarg, &hdr, sizeof(hdr)))
195
+ return -EFAULT;
232196 return ret;
233197 }
234198
....@@ -345,12 +309,15 @@
345309 static int bsg_open(struct inode *inode, struct file *file)
346310 {
347311 struct bsg_device *bd;
312
+ struct bsg_class_device *bcd;
348313
349314 bd = bsg_get_device(inode, file);
350315
351316 if (IS_ERR(bd))
352317 return PTR_ERR(bd);
353318
319
+ bcd = &bd->queue->bsg_dev;
320
+ pm_runtime_get_sync(bcd->class_dev->parent);
354321 file->private_data = bd;
355322 return 0;
356323 }
....@@ -358,36 +325,48 @@
358325 static int bsg_release(struct inode *inode, struct file *file)
359326 {
360327 struct bsg_device *bd = file->private_data;
328
+ struct bsg_class_device *bcd;
361329
362330 file->private_data = NULL;
331
+
332
+ bcd = &bd->queue->bsg_dev;
333
+ pm_runtime_put_sync(bcd->class_dev->parent);
363334 return bsg_put_device(bd);
335
+}
336
+
337
+static int bsg_get_command_q(struct bsg_device *bd, int __user *uarg)
338
+{
339
+ return put_user(bd->max_queue, uarg);
340
+}
341
+
342
+static int bsg_set_command_q(struct bsg_device *bd, int __user *uarg)
343
+{
344
+ int queue;
345
+
346
+ if (get_user(queue, uarg))
347
+ return -EFAULT;
348
+ if (queue < 1)
349
+ return -EINVAL;
350
+
351
+ spin_lock_irq(&bd->lock);
352
+ bd->max_queue = queue;
353
+ spin_unlock_irq(&bd->lock);
354
+ return 0;
364355 }
365356
366357 static long bsg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
367358 {
368359 struct bsg_device *bd = file->private_data;
369
- int __user *uarg = (int __user *) arg;
370
- int ret;
360
+ void __user *uarg = (void __user *) arg;
371361
372362 switch (cmd) {
373
- /*
374
- * our own ioctls
375
- */
363
+ /*
364
+ * Our own ioctls
365
+ */
376366 case SG_GET_COMMAND_Q:
377
- return put_user(bd->max_queue, uarg);
378
- case SG_SET_COMMAND_Q: {
379
- int queue;
380
-
381
- if (get_user(queue, uarg))
382
- return -EFAULT;
383
- if (queue < 1)
384
- return -EINVAL;
385
-
386
- spin_lock_irq(&bd->lock);
387
- bd->max_queue = queue;
388
- spin_unlock_irq(&bd->lock);
389
- return 0;
390
- }
367
+ return bsg_get_command_q(bd, uarg);
368
+ case SG_SET_COMMAND_Q:
369
+ return bsg_set_command_q(bd, uarg);
391370
392371 /*
393372 * SCSI/sg ioctls
....@@ -400,36 +379,13 @@
400379 case SG_GET_RESERVED_SIZE:
401380 case SG_SET_RESERVED_SIZE:
402381 case SG_EMULATED_HOST:
403
- case SCSI_IOCTL_SEND_COMMAND: {
404
- void __user *uarg = (void __user *) arg;
405382 return scsi_cmd_ioctl(bd->queue, NULL, file->f_mode, cmd, uarg);
406
- }
407
- case SG_IO: {
408
- struct request *rq;
409
- struct bio *bio, *bidi_bio = NULL;
410
- struct sg_io_v4 hdr;
411
- int at_head;
412
-
413
- if (copy_from_user(&hdr, uarg, sizeof(hdr)))
414
- return -EFAULT;
415
-
416
- rq = bsg_map_hdr(bd->queue, &hdr, file->f_mode);
417
- if (IS_ERR(rq))
418
- return PTR_ERR(rq);
419
-
420
- bio = rq->bio;
421
- if (rq->next_rq)
422
- bidi_bio = rq->next_rq->bio;
423
-
424
- at_head = (0 == (hdr.flags & BSG_FLAG_Q_AT_TAIL));
425
- blk_execute_rq(bd->queue, NULL, rq, at_head);
426
- ret = blk_complete_sgv4_hdr_rq(rq, &hdr, bio, bidi_bio);
427
-
428
- if (copy_to_user(uarg, &hdr, sizeof(hdr)))
429
- return -EFAULT;
430
-
431
- return ret;
432
- }
383
+ case SG_IO:
384
+ return bsg_sg_io(bd->queue, file->f_mode, uarg);
385
+ case SCSI_IOCTL_SEND_COMMAND:
386
+ pr_warn_ratelimited("%s: calling unsupported SCSI_IOCTL_SEND_COMMAND\n",
387
+ current->comm);
388
+ return -EINVAL;
433389 default:
434390 return -ENOTTY;
435391 }
....@@ -439,6 +395,7 @@
439395 .open = bsg_open,
440396 .release = bsg_release,
441397 .unlocked_ioctl = bsg_ioctl,
398
+ .compat_ioctl = compat_ptr_ioctl,
442399 .owner = THIS_MODULE,
443400 .llseek = default_llseek,
444401 };
....@@ -471,7 +428,7 @@
471428 /*
472429 * we need a proper transport to send commands, not a stacked device
473430 */
474
- if (!queue_is_rq_based(q))
431
+ if (!queue_is_mq(q))
475432 return 0;
476433
477434 bcd = &q->bsg_dev;