Turn on PF_NOFREEZE flag instead of setting task state to TASK_UNINTERRUPTIBLE.
[linux-2.6.git] / drivers / scsi / sg.c
index a3e68b4..7fb610d 100644 (file)
@@ -47,8 +47,9 @@ static int sg_version_num = 30531;    /* 2 digits for each component */
 #include <linux/devfs_fs_kernel.h>
 #include <linux/cdev.h>
 #include <linux/seq_file.h>
-
 #include <linux/blkdev.h>
+#include <linux/delay.h>
+
 #include "scsi.h"
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_driver.h>
@@ -205,8 +206,6 @@ static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
 static Sg_request *sg_add_request(Sg_fd * sfp);
 static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
 static int sg_res_in_use(Sg_fd * sfp);
-static int sg_ms_to_jif(unsigned int msecs);
-static inline unsigned sg_jif_to_ms(int jifs);
 static int sg_allow_access(unsigned char opcode, char dev_type);
 static int sg_build_direct(Sg_request * srp, Sg_fd * sfp, int dxfer_len);
 static Sg_device *sg_get_dev(int dev);
@@ -234,18 +233,13 @@ sg_open(struct inode *inode, struct file *filp)
        int res;
        int retval;
 
+       nonseekable_open(inode, filp);
        SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
        sdp = sg_get_dev(dev);
        if ((!sdp) || (!sdp->device))
                return -ENXIO;
        if (sdp->detached)
                return -ENODEV;
-               
-       /* scsi generic is only for non-disk, non-cd, non-tape devices */       
-       if ( (sdp->device->type == TYPE_DISK) || (sdp->device->type == TYPE_MOD) || (sdp->device->type == TYPE_ROM) 
-          || (sdp->device->type == TYPE_WORM) || ( sdp->device->type == TYPE_TAPE))
-               printk(KERN_WARNING "%s: Using deprecated /dev/sg mechanism instead of SG_IO on the actual device\n",current->comm);
-               
 
        /* This driver's module count bumped by fops_get in <linux/fs.h> */
        /* Prevent the device driver from vanishing while we sleep */
@@ -349,7 +343,6 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
                return -ENXIO;
        SCSI_LOG_TIMEOUT(3, printk("sg_read: %s, count=%d\n",
                                   sdp->disk->disk_name, (int) count));
-       if (ppos != &filp->f_pos) ;     /* FIXME: Hmm.  Seek to the right place, or fail?  */
        if ((k = verify_area(VERIFY_WRITE, buf, count)))
                return k;
        if (sfp->force_packid && (count >= SZ_SG_HEADER)) {
@@ -507,7 +500,6 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
        if (!((filp->f_flags & O_NONBLOCK) ||
              scsi_block_when_processing_errors(sdp->device)))
                return -ENXIO;
-       if (ppos != &filp->f_pos) ;     /* FIXME: Hmm.  Seek to the right place, or fail?  */
 
        if ((k = verify_area(VERIFY_READ, buf, count)))
                return k;       /* protects following copy_from_user()s + get_user()s */
@@ -571,6 +563,20 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
        hp->usr_ptr = NULL;
        if (__copy_from_user(cmnd, buf, cmd_size))
                return -EFAULT;
+       /*
+        * SG_DXFER_TO_FROM_DEV is functionally equivalent to SG_DXFER_FROM_DEV,
+        * but is is possible that the app intended SG_DXFER_TO_DEV, because there
+        * is a non-zero input_size, so emit a warning.
+        */
+       if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV)
+               if (printk_ratelimit())
+                       printk(KERN_WARNING
+                              "sg_write: data in/out %d/%d bytes for SCSI command 0x%x--"
+                              "guessing data in;\n" KERN_WARNING "   "
+                              "program %s not setting count and/or reply_len properly\n",
+                              old_hdr.reply_len - (int)SZ_SG_HEADER,
+                              input_size, (unsigned int) cmnd[0],
+                              current->comm);
        k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
        return (k < 0) ? k : count;
 }
@@ -584,6 +590,7 @@ sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
        sg_io_hdr_t *hp;
        unsigned char cmnd[sizeof (dummy_cmdp->sr_cmnd)];
        int timeout;
+       unsigned long ul_timeout;
 
        if (count < SZ_SG_IO_HDR)
                return -EINVAL;
@@ -618,7 +625,8 @@ sg_new_write(Sg_fd * sfp, const char __user *buf, size_t count,
                        return -EBUSY;  /* reserve buffer already being used */
                }
        }
-       timeout = sg_ms_to_jif(srp->header.timeout);
+       ul_timeout = msecs_to_jiffies(srp->header.timeout);
+       timeout = (ul_timeout < INT_MAX) ? ul_timeout : INT_MAX;
        if ((!hp->cmdp) || (hp->cmd_len < 6) || (hp->cmd_len > sizeof (cmnd))) {
                sg_remove_request(sfp, srp);
                return -EMSGSIZE;
@@ -726,6 +734,18 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
        return 0;
 }
 
+static int
+sg_srp_done(Sg_request *srp, Sg_fd *sfp)
+{
+       unsigned long iflags;
+       int done;
+
+       read_lock_irqsave(&sfp->rq_list_lock, iflags);
+       done = srp->done;
+       read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
+       return done;
+}
+
 static int
 sg_ioctl(struct inode *inode, struct file *filp,
         unsigned int cmd_in, unsigned long arg)
@@ -765,7 +785,7 @@ sg_ioctl(struct inode *inode, struct file *filp,
                        while (1) {
                                result = 0;     /* following macro to beat race condition */
                                __wait_event_interruptible(sfp->read_wait,
-                                       (sdp->detached || sfp->closed || srp->done),
+                                       (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
                                                           result);
                                if (sdp->detached)
                                        return -ENODEV;
@@ -776,7 +796,9 @@ sg_ioctl(struct inode *inode, struct file *filp,
                                srp->orphan = 1;
                                return result;  /* -ERESTARTSYS because signal hit process */
                        }
+                       write_lock_irqsave(&sfp->rq_list_lock, iflags);
                        srp->done = 2;
+                       write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
                        result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
                        return (result < 0) ? result : 0;
                }
@@ -935,7 +957,7 @@ sg_ioctl(struct inode *inode, struct file *filp,
                                            srp->header.driver_status;
                                        rinfo[val].duration =
                                            srp->done ? srp->header.duration :
-                                           sg_jif_to_ms(
+                                           jiffies_to_msecs(
                                                jiffies - srp->header.duration);
                                        rinfo[val].orphan = srp->orphan;
                                        rinfo[val].sg_io_owned = srp->sg_io_owned;
@@ -1219,6 +1241,7 @@ sg_cmd_done(Scsi_Cmnd * SCpnt)
        Sg_device *sdp = NULL;
        Sg_fd *sfp;
        Sg_request *srp = NULL;
+       unsigned long iflags;
 
        if (SCpnt && (SRpnt = SCpnt->sc_request))
                srp = (Sg_request *) SRpnt->upper_private_data;
@@ -1257,7 +1280,7 @@ sg_cmd_done(Scsi_Cmnd * SCpnt)
        srp->header.resid = SCpnt->resid;
        /* N.B. unit of duration changes here from jiffies to millisecs */
        srp->header.duration =
-           sg_jif_to_ms(jiffies - (int) srp->header.duration);
+           jiffies_to_msecs(jiffies - srp->header.duration);
        if (0 != SRpnt->sr_result) {
                memcpy(srp->sense_b, SRpnt->sr_sense_buffer,
                       sizeof (srp->sense_b));
@@ -1307,8 +1330,10 @@ sg_cmd_done(Scsi_Cmnd * SCpnt)
        if (sfp && srp) {
                /* Now wake up any sg_read() that is waiting for this packet. */
                kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
+               write_lock_irqsave(&sfp->rq_list_lock, iflags);
                srp->done = 1;
                wake_up_interruptible(&sfp->read_wait);
+               write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
        }
 }
 
@@ -1502,7 +1527,7 @@ sg_remove(struct class_device *cl_dev)
                                tsfp = sfp->nextfp;
                                for (srp = sfp->headrp; srp; srp = tsrp) {
                                        tsrp = srp->nextrp;
-                                       if (sfp->closed || (0 == srp->done))
+                                       if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
                                                sg_finish_rem_req(srp);
                                }
                                if (sfp->closed) {
@@ -1541,7 +1566,7 @@ sg_remove(struct class_device *cl_dev)
        }
 
        if (delay)
-               scsi_sleep(2);  /* dirty detach so delay device destruction */
+               msleep(10);     /* dirty detach so delay device destruction */
 }
 
 /* Set 'perm' (4th argument) to 0 to disable module_param's definition
@@ -2410,12 +2435,12 @@ sg_add_sfp(Sg_device * sdp, int dev)
        Sg_fd *sfp;
        unsigned long iflags;
 
-       sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, 0);
+       sfp = (Sg_fd *) sg_page_malloc(sizeof (Sg_fd), 0, NULL);
        if (!sfp)
                return NULL;
        memset(sfp, 0, sizeof (Sg_fd));
        init_waitqueue_head(&sfp->read_wait);
-       sfp->rq_list_lock = RW_LOCK_UNLOCKED;
+       rwlock_init(&sfp->rq_list_lock);
 
        sfp->timeout = SG_DEFAULT_TIMEOUT;
        sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
@@ -2484,7 +2509,7 @@ sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
 
        for (srp = sfp->headrp; srp; srp = tsrp) {
                tsrp = srp->nextrp;
-               if (srp->done)
+               if (sg_srp_done(srp, sfp))
                        sg_finish_rem_req(srp);
                else
                        ++dirty;
@@ -2581,30 +2606,6 @@ sg_page_free(char *buff, int size)
        free_pages((unsigned long) buff, order);
 }
 
-static int
-sg_ms_to_jif(unsigned int msecs)
-{
-       if ((UINT_MAX / 2U) < msecs)
-               return INT_MAX; /* special case, set largest possible */
-       else
-               return ((int) msecs <
-                       (INT_MAX / 1000)) ? (((int) msecs * HZ) / 1000)
-                   : (((int) msecs / 1000) * HZ);
-}
-
-static inline unsigned
-sg_jif_to_ms(int jifs)
-{
-       if (jifs <= 0)
-               return 0U;
-       else {
-               unsigned int j = (unsigned int) jifs;
-               return (j <
-                       (UINT_MAX / 1000)) ? ((j * 1000) / HZ) : ((j / HZ) *
-                                                                 1000);
-       }
-}
-
 static unsigned char allow_ops[] = { TEST_UNIT_READY, REQUEST_SENSE,
        INQUIRY, READ_CAPACITY, READ_BUFFER, READ_6, READ_10, READ_12,
        MODE_SENSE, MODE_SENSE_10, LOG_SENSE
@@ -2820,7 +2821,7 @@ sg_proc_write_adio(struct file *filp, const char __user *buffer,
        if (copy_from_user(buff, buffer, num))
                return -EFAULT;
        buff[num] = '\0';
-       sg_allow_dio = simple_strtoul(buff, 0, 10) ? 1 : 0;
+       sg_allow_dio = simple_strtoul(buff, NULL, 10) ? 1 : 0;
        return count;
 }
 
@@ -2843,7 +2844,7 @@ sg_proc_write_dressz(struct file *filp, const char __user *buffer,
        if (copy_from_user(buff, buffer, num))
                return -EFAULT;
        buff[num] = '\0';
-       k = simple_strtoul(buff, 0, 10);
+       k = simple_strtoul(buff, NULL, 10);
        if (k <= 1048576) {     /* limit "big buff" to 1 MB */
                sg_big_buff = k;
                return count;
@@ -2967,7 +2968,7 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
        for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) {
                seq_printf(s, "   FD(%d): timeout=%dms bufflen=%d "
                           "(res)sgat=%d low_dma=%d\n", k + 1,
-                          sg_jif_to_ms(fp->timeout),
+                          jiffies_to_msecs(fp->timeout),
                           fp->reserve.bufflen,
                           (int) fp->reserve.k_use_sg,
                           (int) fp->low_dma);
@@ -3003,8 +3004,8 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
                                seq_printf(s, " dur=%d", hp->duration);
                        else
                                seq_printf(s, " t_o/elap=%d/%d",
-                                 new_interface ? hp->timeout : sg_jif_to_ms(fp->timeout),
-                                 sg_jif_to_ms(hp->duration ? (jiffies - hp->duration) : 0));
+                                 new_interface ? hp->timeout : jiffies_to_msecs(fp->timeout),
+                                 jiffies_to_msecs(hp->duration ? (jiffies - hp->duration) : 0));
                        seq_printf(s, "ms sgat=%d op=0x%02x\n", usg,
                                   (int) srp->data.cmd_opcode);
                }