hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/scsi/st.c
....@@ -1,6 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 SCSI Tape Driver for Linux version 1.1 and newer. See the accompanying
3
- file Documentation/scsi/st.txt for more information.
4
+ file Documentation/scsi/st.rst for more information.
45
56 History:
67 Rewritten from Dwayne Forsyth's SCSI tape driver by Kai Makisara.
....@@ -21,6 +22,7 @@
2122
2223 #include <linux/module.h>
2324
25
+#include <linux/compat.h>
2426 #include <linux/fs.h>
2527 #include <linux/kernel.h>
2628 #include <linux/sched/signal.h>
....@@ -43,6 +45,7 @@
4345
4446 #include <linux/uaccess.h>
4547 #include <asm/dma.h>
48
+#include <asm/unaligned.h>
4649
4750 #include <scsi/scsi.h>
4851 #include <scsi/scsi_dbg.h>
....@@ -169,7 +172,7 @@
169172
170173 /* Remove mode bits and auto-rewind bit (7) */
171174 #define TAPE_NR(x) ( ((iminor(x) & ~255) >> (ST_NBR_MODE_BITS + 1)) | \
172
- (iminor(x) & ~(-1 << ST_MODE_SHIFT)) )
175
+ (iminor(x) & ((1 << ST_MODE_SHIFT)-1)))
173176 #define TAPE_MODE(x) ((iminor(x) & ST_MODE_MASK) >> ST_MODE_SHIFT)
174177
175178 /* Construct the minor number from the device (d), mode (m), and non-rewind (n) data */
....@@ -227,7 +230,6 @@
227230
228231
229232
230
-#include "osst_detect.h"
231233 #ifndef SIGS_FROM_OSST
232234 #define SIGS_FROM_OSST \
233235 {"OnStream", "SC-", "", "osst"}, \
....@@ -337,12 +339,14 @@
337339 switch (sense[0] & 0x7f) {
338340 case 0x71:
339341 s->deferred = 1;
342
+ fallthrough;
340343 case 0x70:
341344 s->fixed_format = 1;
342345 s->flags = sense[2] & 0xe0;
343346 break;
344347 case 0x73:
345348 s->deferred = 1;
349
+ fallthrough;
346350 case 0x72:
347351 s->fixed_format = 0;
348352 ucp = scsi_sense_desc_find(sense, SCSI_SENSE_BUFFERSIZE, 4);
....@@ -530,7 +534,7 @@
530534 complete(SRpnt->waiting);
531535
532536 blk_rq_unmap_user(tmp);
533
- __blk_put_request(req->q, req);
537
+ blk_put_request(req);
534538 }
535539
536540 static int st_scsi_execute(struct st_request *SRpnt, const unsigned char *cmd,
....@@ -1453,7 +1457,6 @@
14531457 accessing this tape. */
14541458 static int st_release(struct inode *inode, struct file *filp)
14551459 {
1456
- int result = 0;
14571460 struct scsi_tape *STp = filp->private_data;
14581461
14591462 if (STp->door_locked == ST_LOCKED_AUTO)
....@@ -1466,9 +1469,9 @@
14661469 scsi_autopm_put_device(STp->device);
14671470 scsi_tape_put(STp);
14681471
1469
- return result;
1472
+ return 0;
14701473 }
1471
-
1474
+
14721475 /* The checks common to both reading and writing */
14731476 static ssize_t rw_checks(struct scsi_tape *STp, struct file *filp, size_t count)
14741477 {
....@@ -2677,8 +2680,7 @@
26772680 if (!debugging)
26782681 return;
26792682
2680
- sc = cmd[2] & 0x80 ? 0xff000000 : 0;
2681
- sc |= (cmd[2] << 16) | (cmd[3] << 8) | cmd[4];
2683
+ sc = sign_extend32(get_unaligned_be24(&cmd[2]), 23);
26822684 if (direction)
26832685 sc = -sc;
26842686 st_printk(ST_DEB_MSG, STp, "Spacing tape %s over %d %s.\n",
....@@ -2721,6 +2723,7 @@
27212723 switch (cmd_in) {
27222724 case MTFSFM:
27232725 chg_eof = 0; /* Changed from the FSF after this */
2726
+ fallthrough;
27242727 case MTFSF:
27252728 cmd[0] = SPACE;
27262729 cmd[1] = 0x01; /* Space FileMarks */
....@@ -2735,6 +2738,7 @@
27352738 break;
27362739 case MTBSFM:
27372740 chg_eof = 0; /* Changed from the FSF after this */
2741
+ fallthrough;
27382742 case MTBSF:
27392743 cmd[0] = SPACE;
27402744 cmd[1] = 0x01; /* Space FileMarks */
....@@ -3496,7 +3500,7 @@
34963500
34973501
34983502 /* The ioctl command */
3499
-static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
3503
+static long st_ioctl_common(struct file *file, unsigned int cmd_in, void __user *p)
35003504 {
35013505 int i, cmd_nr, cmd_type, bt;
35023506 int retval = 0;
....@@ -3504,7 +3508,6 @@
35043508 struct scsi_tape *STp = file->private_data;
35053509 struct st_modedef *STm;
35063510 struct st_partstat *STps;
3507
- void __user *p = (void __user *)arg;
35083511
35093512 if (mutex_lock_interruptible(&STp->lock))
35103513 return -ERESTARTSYS;
....@@ -3796,14 +3799,11 @@
37963799 if (STp->cleaning_req)
37973800 mt_status.mt_gstat |= GMT_CLN(0xffffffff);
37983801
3799
- i = copy_to_user(p, &mt_status, sizeof(struct mtget));
3800
- if (i) {
3801
- retval = (-EFAULT);
3802
+ retval = put_user_mtget(p, &mt_status);
3803
+ if (retval)
38023804 goto out;
3803
- }
38043805
38053806 STp->recover_reg = 0; /* Clear after read */
3806
- retval = 0;
38073807 goto out;
38083808 } /* End of MTIOCGET */
38093809 if (cmd_type == _IOC_TYPE(MTIOCPOS) && cmd_nr == _IOC_NR(MTIOCPOS)) {
....@@ -3817,16 +3817,24 @@
38173817 goto out;
38183818 }
38193819 mt_pos.mt_blkno = blk;
3820
- i = copy_to_user(p, &mt_pos, sizeof(struct mtpos));
3821
- if (i)
3822
- retval = (-EFAULT);
3820
+ retval = put_user_mtpos(p, &mt_pos);
38233821 goto out;
38243822 }
38253823 mutex_unlock(&STp->lock);
38263824 switch (cmd_in) {
3825
+ case SCSI_IOCTL_STOP_UNIT:
3826
+ /* unload */
3827
+ retval = scsi_ioctl(STp->device, cmd_in, p);
3828
+ if (!retval) {
3829
+ STp->rew_at_close = 0;
3830
+ STp->ready = ST_NO_TAPE;
3831
+ }
3832
+ return retval;
3833
+
38273834 case SCSI_IOCTL_GET_IDLUN:
38283835 case SCSI_IOCTL_GET_BUS_NUMBER:
38293836 break;
3837
+
38303838 default:
38313839 if ((cmd_in == SG_IO ||
38323840 cmd_in == SCSI_IOCTL_SEND_COMMAND ||
....@@ -3840,30 +3848,46 @@
38403848 return i;
38413849 break;
38423850 }
3843
- retval = scsi_ioctl(STp->device, cmd_in, p);
3844
- if (!retval && cmd_in == SCSI_IOCTL_STOP_UNIT) { /* unload */
3845
- STp->rew_at_close = 0;
3846
- STp->ready = ST_NO_TAPE;
3847
- }
3848
- return retval;
3851
+ return -ENOTTY;
38493852
38503853 out:
38513854 mutex_unlock(&STp->lock);
38523855 return retval;
38533856 }
38543857
3855
-#ifdef CONFIG_COMPAT
3856
-static long st_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3858
+static long st_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
38573859 {
3860
+ void __user *p = (void __user *)arg;
38583861 struct scsi_tape *STp = file->private_data;
3859
- struct scsi_device *sdev = STp->device;
3860
- int ret = -ENOIOCTLCMD;
3861
- if (sdev->host->hostt->compat_ioctl) {
3862
+ int ret;
38623863
3863
- ret = sdev->host->hostt->compat_ioctl(sdev, cmd, (void __user *)arg);
3864
+ ret = st_ioctl_common(file, cmd_in, p);
3865
+ if (ret != -ENOTTY)
3866
+ return ret;
38643867
3868
+ return scsi_ioctl(STp->device, cmd_in, p);
3869
+}
3870
+
3871
+#ifdef CONFIG_COMPAT
3872
+static long st_compat_ioctl(struct file *file, unsigned int cmd_in, unsigned long arg)
3873
+{
3874
+ void __user *p = compat_ptr(arg);
3875
+ struct scsi_tape *STp = file->private_data;
3876
+ int ret;
3877
+
3878
+ /* argument conversion is handled using put_user_mtpos/put_user_mtget */
3879
+ switch (cmd_in) {
3880
+ case MTIOCPOS32:
3881
+ return st_ioctl_common(file, MTIOCPOS, p);
3882
+ case MTIOCGET32:
3883
+ return st_ioctl_common(file, MTIOCGET, p);
38653884 }
3866
- return ret;
3885
+
3886
+ ret = st_ioctl_common(file, cmd_in, p);
3887
+ if (ret != -ENOTTY)
3888
+ return ret;
3889
+
3890
+ return scsi_compat_ioctl(STp->device, cmd_in, p);
38673891 }
38683892 #endif
38693893
....@@ -4262,9 +4286,10 @@
42624286 if (SDp->type != TYPE_TAPE)
42634287 return -ENODEV;
42644288 if ((stp = st_incompatible(SDp))) {
4265
- sdev_printk(KERN_INFO, SDp, "Found incompatible tape\n");
42664289 sdev_printk(KERN_INFO, SDp,
4267
- "st: The suggested driver is %s.\n", stp);
4290
+ "OnStream tapes are no longer supported;\n");
4291
+ sdev_printk(KERN_INFO, SDp,
4292
+ "please mail to linux-scsi@vger.kernel.org.\n");
42684293 return -ENODEV;
42694294 }
42704295
....@@ -4896,7 +4921,7 @@
48964921 unsigned long end = (uaddr + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
48974922 unsigned long start = uaddr >> PAGE_SHIFT;
48984923 const int nr_pages = end - start;
4899
- int res, i, j;
4924
+ int res, i;
49004925 struct page **pages;
49014926 struct rq_map_data *mdata = &STbp->map_data;
49024927
....@@ -4918,7 +4943,8 @@
49184943
49194944 /* Try to fault in all of the necessary pages */
49204945 /* rw==READ means read from drive, write into memory area */
4921
- res = get_user_pages_fast(uaddr, nr_pages, rw == READ, pages);
4946
+ res = pin_user_pages_fast(uaddr, nr_pages, rw == READ ? FOLL_WRITE : 0,
4947
+ pages);
49224948
49234949 /* Errors and no page mapped should return here */
49244950 if (res < nr_pages)
....@@ -4937,8 +4963,7 @@
49374963 return nr_pages;
49384964 out_unmap:
49394965 if (res > 0) {
4940
- for (j=0; j < res; j++)
4941
- put_page(pages[j]);
4966
+ unpin_user_pages(pages, res);
49424967 res = 0;
49434968 }
49444969 kfree(pages);
....@@ -4950,18 +4975,9 @@
49504975 static int sgl_unmap_user_pages(struct st_buffer *STbp,
49514976 const unsigned int nr_pages, int dirtied)
49524977 {
4953
- int i;
4978
+ /* FIXME: cache flush missing for rw==READ */
4979
+ unpin_user_pages_dirty_lock(STbp->mapped_pages, nr_pages, dirtied);
49544980
4955
- for (i=0; i < nr_pages; i++) {
4956
- struct page *page = STbp->mapped_pages[i];
4957
-
4958
- if (dirtied)
4959
- SetPageDirty(page);
4960
- /* FIXME: cache flush missing for rw==READ
4961
- * FIXME: call the correct reference counting function
4962
- */
4963
- put_page(page);
4964
- }
49654981 kfree(STbp->mapped_pages);
49664982 STbp->mapped_pages = NULL;
49674983