[SCSI] iscsi class: remove host no argument from session creation callout
[safe/jmp/linux-2.6] / drivers / scsi / sg.c
index b447527..7bf54c9 100644 (file)
@@ -98,10 +98,8 @@ static int scatter_elem_sz = SG_SCATTER_SZ;
 static int scatter_elem_sz_prev = SG_SCATTER_SZ;
 
 #define SG_SECTOR_SZ 512
-#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
 
 static int sg_add(struct device *, struct class_interface *);
-static void sg_device_destroy(struct kref *kref);
 static void sg_remove(struct device *, struct class_interface *);
 
 static DEFINE_IDR(sg_index_idr);
@@ -138,10 +136,11 @@ typedef struct sg_request {       /* SG_MAX_QUEUE requests outstanding per file */
        volatile char done;     /* 0->before bh, 1->before read, 2->read */
        struct request *rq;
        struct bio *bio;
+       struct execute_work ew;
 } Sg_request;
 
 typedef struct sg_fd {         /* holds the state of a file descriptor */
-       struct sg_fd *nextfp;   /* NULL when last opened fd on this device */
+       struct list_head sfd_siblings;
        struct sg_device *parentdp;     /* owning device */
        wait_queue_head_t read_wait;    /* queue read until command done */
        rwlock_t rq_list_lock;  /* protect access to list in req_arr */
@@ -168,7 +167,7 @@ typedef struct sg_device { /* holds the state of each scsi generic device */
        wait_queue_head_t o_excl_wait;  /* queue open() when O_EXCL in use */
        int sg_tablesize;       /* adapter's max scatter-gather table size */
        u32 index;              /* device index number */
-       Sg_fd *headfp;          /* first open fd belonging to this device */
+       struct list_head sfds;
        volatile char detached; /* 0->attached, 1->detached pending removal */
        volatile char exclude;  /* opened for exclusive access */
        char sgdebug;           /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
@@ -177,19 +176,16 @@ typedef struct sg_device { /* holds the state of each scsi generic device */
        struct kref d_ref;
 } Sg_device;
 
-static int sg_fasync(int fd, struct file *filp, int mode);
 /* tasklet or soft irq callback */
 static void sg_rq_end_io(struct request *rq, int uptodate);
 static int sg_start_req(Sg_request *srp, unsigned char *cmd);
 static void sg_finish_rem_req(Sg_request * srp);
 static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
-static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
-                        int tablesize);
 static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
                           Sg_request * srp);
 static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
                        const char __user *buf, size_t count, int blocking,
-                       int read_only, Sg_request **o_srp);
+                       int read_only, int sg_io_owned, Sg_request **o_srp);
 static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
                           unsigned char *cmnd, int timeout, int blocking);
 static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
@@ -203,12 +199,8 @@ 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 Sg_device *sg_lookup_dev(int dev);
 static Sg_device *sg_get_dev(int dev);
 static void sg_put_dev(Sg_device *sdp);
-#ifdef CONFIG_SCSI_PROC_FS
-static int sg_last_dev(void);
-#endif
 
 #define SZ_SG_HEADER sizeof(struct sg_header)
 #define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
@@ -266,13 +258,13 @@ sg_open(struct inode *inode, struct file *filp)
                        retval = -EPERM; /* Can't lock it with read only access */
                        goto error_out;
                }
-               if (sdp->headfp && (flags & O_NONBLOCK)) {
+               if (!list_empty(&sdp->sfds) && (flags & O_NONBLOCK)) {
                        retval = -EBUSY;
                        goto error_out;
                }
                res = 0;
                __wait_event_interruptible(sdp->o_excl_wait,
-                       ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
+                                          ((!list_empty(&sdp->sfds) || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
                if (res) {
                        retval = res;   /* -ERESTARTSYS because signal hit process */
                        goto error_out;
@@ -294,7 +286,7 @@ sg_open(struct inode *inode, struct file *filp)
                retval = -ENODEV;
                goto error_out;
        }
-       if (!sdp->headfp) {     /* no existing opens on this device */
+       if (list_empty(&sdp->sfds)) {   /* no existing opens on this device */
                sdp->sgdebug = 0;
                q = sdp->device->request_queue;
                sdp->sg_tablesize = min(q->max_hw_segments,
@@ -561,7 +553,8 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
                return -EFAULT;
        blocking = !(filp->f_flags & O_NONBLOCK);
        if (old_hdr.reply_len < 0)
-               return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL);
+               return sg_new_write(sfp, filp, buf, count,
+                                   blocking, 0, 0, NULL);
        if (count < (SZ_SG_HEADER + 6))
                return -EIO;    /* The minimum scsi command length is 6 bytes. */
 
@@ -642,7 +635,7 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
 
 static ssize_t
 sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
-                size_t count, int blocking, int read_only,
+                size_t count, int blocking, int read_only, int sg_io_owned,
                 Sg_request **o_srp)
 {
        int k;
@@ -662,6 +655,7 @@ sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
                SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
                return -EDOM;
        }
+       srp->sg_io_owned = sg_io_owned;
        hp = &srp->header;
        if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
                sg_remove_request(sfp, srp);
@@ -766,18 +760,6 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
 }
 
 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)
 {
@@ -809,27 +791,26 @@ sg_ioctl(struct inode *inode, struct file *filp,
                                return -EFAULT;
                        result =
                            sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
-                                        blocking, read_only, &srp);
+                                        blocking, read_only, 1, &srp);
                        if (result < 0)
                                return result;
-                       srp->sg_io_owned = 1;
                        while (1) {
                                result = 0;     /* following macro to beat race condition */
                                __wait_event_interruptible(sfp->read_wait,
-                                       (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)),
-                                                          result);
+                                       (srp->done || sdp->detached),
+                                       result);
                                if (sdp->detached)
                                        return -ENODEV;
-                               if (sfp->closed)
-                                       return 0;       /* request packet dropped already */
-                               if (0 == result)
+                               write_lock_irq(&sfp->rq_list_lock);
+                               if (srp->done) {
+                                       srp->done = 2;
+                                       write_unlock_irq(&sfp->rq_list_lock);
                                        break;
+                               }
                                srp->orphan = 1;
+                               write_unlock_irq(&sfp->rq_list_lock);
                                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;
                }
@@ -1245,6 +1226,15 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
        return 0;
 }
 
+static void sg_rq_end_io_usercontext(struct work_struct *work)
+{
+       struct sg_request *srp = container_of(work, struct sg_request, ew.work);
+       struct sg_fd *sfp = srp->parentfp;
+
+       sg_finish_rem_req(srp);
+       kref_put(&sfp->f_ref, sg_remove_sfp);
+}
+
 /*
  * This function is a "bottom half" handler that is called by the mid
  * level when a command is completed (or has failed).
@@ -1323,10 +1313,9 @@ static void sg_rq_end_io(struct request *rq, int uptodate)
                 */
                wake_up_interruptible(&sfp->read_wait);
                kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
+               kref_put(&sfp->f_ref, sg_remove_sfp);
        } else
-               sg_finish_rem_req(srp); /* call with srp->done == 0 */
-
-       kref_put(&sfp->f_ref, sg_remove_sfp);
+               execute_in_process_context(sg_rq_end_io_usercontext, &srp->ew);
 }
 
 static struct file_operations sg_fops = {
@@ -1386,6 +1375,7 @@ static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
        disk->first_minor = k;
        sdp->disk = disk;
        sdp->device = scsidp;
+       INIT_LIST_HEAD(&sdp->sfds);
        init_waitqueue_head(&sdp->o_excl_wait);
        sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
        sdp->index = k;
@@ -1528,7 +1518,7 @@ static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
        /* Need a write lock to set sdp->detached. */
        write_lock_irqsave(&sg_index_lock, iflags);
        sdp->detached = 1;
-       for (sfp = sdp->headfp; sfp; sfp = sfp->nextfp) {
+       list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
                wake_up_interruptible(&sfp->read_wait);
                kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
        }
@@ -1733,8 +1723,8 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
                return -EFAULT;
        if (0 == blk_size)
                ++blk_size;     /* don't know why */
-/* round request up to next highest SG_SECTOR_SZ byte boundary */
-       blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK);
+       /* round request up to next highest SG_SECTOR_SZ byte boundary */
+       blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
        SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
                                   buff_size, blk_size));
 
@@ -2035,14 +2025,7 @@ sg_add_sfp(Sg_device * sdp, int dev)
        sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
        sfp->parentdp = sdp;
        write_lock_irqsave(&sg_index_lock, iflags);
-       if (!sdp->headfp)
-               sdp->headfp = sfp;
-       else {                  /* add to tail of existing list */
-               Sg_fd *pfp = sdp->headfp;
-               while (pfp->nextfp)
-                       pfp = pfp->nextfp;
-               pfp->nextfp = sfp;
-       }
+       list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
        write_unlock_irqrestore(&sg_index_lock, iflags);
        SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
        if (unlikely(sg_big_buff != def_reserved_size))
@@ -2091,28 +2074,10 @@ static void sg_remove_sfp(struct kref *kref)
 {
        struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
        struct sg_device *sdp = sfp->parentdp;
-       Sg_fd *fp;
-       Sg_fd *prev_fp;
        unsigned long iflags;
 
-       /* CAUTION!  Note that sfp can still be found by walking sdp->headfp
-        * even though the refcount is now 0.  Therefore, unlink sfp from
-        * sdp->headfp BEFORE doing any other cleanup.
-        */
-
        write_lock_irqsave(&sg_index_lock, iflags);
-       prev_fp = sdp->headfp;
-       if (sfp == prev_fp)
-               sdp->headfp = prev_fp->nextfp;
-       else {
-               while ((fp = prev_fp->nextfp)) {
-                       if (sfp == fp) {
-                               prev_fp->nextfp = fp->nextfp;
-                               break;
-                       }
-                       prev_fp = fp;
-               }
-       }
+       list_del(&sfp->sfd_siblings);
        write_unlock_irqrestore(&sg_index_lock, iflags);
        wake_up_interruptible(&sdp->o_excl_wait);
 
@@ -2497,10 +2462,12 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
        const char * cp;
        unsigned int ms;
 
-       for (k = 0, fp = sdp->headfp; fp != NULL; ++k, fp = fp->nextfp) {
+       k = 0;
+       list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
+               k++;
                read_lock(&fp->rq_list_lock); /* irqs already disabled */
                seq_printf(s, "   FD(%d): timeout=%dms bufflen=%d "
-                          "(res)sgat=%d low_dma=%d\n", k + 1,
+                          "(res)sgat=%d low_dma=%d\n", k,
                           jiffies_to_msecs(fp->timeout),
                           fp->reserve.bufflen,
                           (int) fp->reserve.k_use_sg,
@@ -2570,7 +2537,7 @@ static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
 
        read_lock_irqsave(&sg_index_lock, iflags);
        sdp = it ? sg_lookup_dev(it->index) : NULL;
-       if (sdp && sdp->headfp) {
+       if (sdp && !list_empty(&sdp->sfds)) {
                struct scsi_device *scsidp = sdp->device;
 
                seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);