aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/sg.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2009-03-28 16:30:43 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-03-28 16:30:43 -0400
commitd54b3538b0bfb31351d02d1669d4a978d2abfc5f (patch)
tree5ce539ecba525b30bbfb1c46c55487099264947e /drivers/scsi/sg.c
parent5d80f8e5a9dc9c9a94d4aeaa567e219a808b8a4a (diff)
parentaf50bb993dfa673cf21ab812efe620d7e0c36319 (diff)
Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
* git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6: (119 commits) [SCSI] scsi_dh_rdac: Retry for NOT_READY check condition [SCSI] mpt2sas: make global symbols unique [SCSI] sd: Make revalidate less chatty [SCSI] sd: Try READ CAPACITY 16 first for SBC-2 devices [SCSI] sd: Refactor sd_read_capacity() [SCSI] mpt2sas v00.100.11.15 [SCSI] mpt2sas: add MPT2SAS_MINOR(221) to miscdevice.h [SCSI] ch: Add scsi type modalias [SCSI] 3w-9xxx: add power management support [SCSI] bsg: add linux/types.h include to bsg.h [SCSI] cxgb3i: fix function descriptions [SCSI] libiscsi: fix possbile null ptr session command cleanup [SCSI] iscsi class: remove host no argument from session creation callout [SCSI] libiscsi: pass session failure a session struct [SCSI] iscsi lib: remove qdepth param from iscsi host allocation [SCSI] iscsi lib: have lib create work queue for transmitting IO [SCSI] iscsi class: fix lock dep warning on logout [SCSI] libiscsi: don't cap queue depth in iscsi modules [SCSI] iscsi_tcp: replace scsi_debug/tcp_debug logging with iscsi conn logging [SCSI] libiscsi_tcp: replace tcp_debug/scsi_debug logging with session/conn logging ...
Diffstat (limited to 'drivers/scsi/sg.c')
-rw-r--r--drivers/scsi/sg.c495
1 files changed, 223 insertions, 272 deletions
diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index b4ef2f84ea32..ffc87851f2e8 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -98,7 +98,6 @@ static int scatter_elem_sz = SG_SCATTER_SZ;
98static int scatter_elem_sz_prev = SG_SCATTER_SZ; 98static int scatter_elem_sz_prev = SG_SCATTER_SZ;
99 99
100#define SG_SECTOR_SZ 512 100#define SG_SECTOR_SZ 512
101#define SG_SECTOR_MSK (SG_SECTOR_SZ - 1)
102 101
103static int sg_add(struct device *, struct class_interface *); 102static int sg_add(struct device *, struct class_interface *);
104static void sg_remove(struct device *, struct class_interface *); 103static void sg_remove(struct device *, struct class_interface *);
@@ -137,10 +136,11 @@ typedef struct sg_request { /* SG_MAX_QUEUE requests outstanding per file */
137 volatile char done; /* 0->before bh, 1->before read, 2->read */ 136 volatile char done; /* 0->before bh, 1->before read, 2->read */
138 struct request *rq; 137 struct request *rq;
139 struct bio *bio; 138 struct bio *bio;
139 struct execute_work ew;
140} Sg_request; 140} Sg_request;
141 141
142typedef struct sg_fd { /* holds the state of a file descriptor */ 142typedef struct sg_fd { /* holds the state of a file descriptor */
143 struct sg_fd *nextfp; /* NULL when last opened fd on this device */ 143 struct list_head sfd_siblings;
144 struct sg_device *parentdp; /* owning device */ 144 struct sg_device *parentdp; /* owning device */
145 wait_queue_head_t read_wait; /* queue read until command done */ 145 wait_queue_head_t read_wait; /* queue read until command done */
146 rwlock_t rq_list_lock; /* protect access to list in req_arr */ 146 rwlock_t rq_list_lock; /* protect access to list in req_arr */
@@ -158,6 +158,8 @@ typedef struct sg_fd { /* holds the state of a file descriptor */
158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */ 158 char next_cmd_len; /* 0 -> automatic (def), >0 -> use on next write() */
159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */ 159 char keep_orphan; /* 0 -> drop orphan (def), 1 -> keep for read() */
160 char mmap_called; /* 0 -> mmap() never called on this fd */ 160 char mmap_called; /* 0 -> mmap() never called on this fd */
161 struct kref f_ref;
162 struct execute_work ew;
161} Sg_fd; 163} Sg_fd;
162 164
163typedef struct sg_device { /* holds the state of each scsi generic device */ 165typedef struct sg_device { /* holds the state of each scsi generic device */
@@ -165,27 +167,25 @@ typedef struct sg_device { /* holds the state of each scsi generic device */
165 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */ 167 wait_queue_head_t o_excl_wait; /* queue open() when O_EXCL in use */
166 int sg_tablesize; /* adapter's max scatter-gather table size */ 168 int sg_tablesize; /* adapter's max scatter-gather table size */
167 u32 index; /* device index number */ 169 u32 index; /* device index number */
168 Sg_fd *headfp; /* first open fd belonging to this device */ 170 struct list_head sfds;
169 volatile char detached; /* 0->attached, 1->detached pending removal */ 171 volatile char detached; /* 0->attached, 1->detached pending removal */
170 volatile char exclude; /* opened for exclusive access */ 172 volatile char exclude; /* opened for exclusive access */
171 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */ 173 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
172 struct gendisk *disk; 174 struct gendisk *disk;
173 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */ 175 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
176 struct kref d_ref;
174} Sg_device; 177} Sg_device;
175 178
176static int sg_fasync(int fd, struct file *filp, int mode);
177/* tasklet or soft irq callback */ 179/* tasklet or soft irq callback */
178static void sg_rq_end_io(struct request *rq, int uptodate); 180static void sg_rq_end_io(struct request *rq, int uptodate);
179static int sg_start_req(Sg_request *srp, unsigned char *cmd); 181static int sg_start_req(Sg_request *srp, unsigned char *cmd);
180static void sg_finish_rem_req(Sg_request * srp); 182static void sg_finish_rem_req(Sg_request * srp);
181static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size); 183static int sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size);
182static int sg_build_sgat(Sg_scatter_hold * schp, const Sg_fd * sfp,
183 int tablesize);
184static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count, 184static ssize_t sg_new_read(Sg_fd * sfp, char __user *buf, size_t count,
185 Sg_request * srp); 185 Sg_request * srp);
186static ssize_t sg_new_write(Sg_fd *sfp, struct file *file, 186static ssize_t sg_new_write(Sg_fd *sfp, struct file *file,
187 const char __user *buf, size_t count, int blocking, 187 const char __user *buf, size_t count, int blocking,
188 int read_only, Sg_request **o_srp); 188 int read_only, int sg_io_owned, Sg_request **o_srp);
189static int sg_common_write(Sg_fd * sfp, Sg_request * srp, 189static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
190 unsigned char *cmnd, int timeout, int blocking); 190 unsigned char *cmnd, int timeout, int blocking);
191static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer); 191static int sg_read_oxfer(Sg_request * srp, char __user *outp, int num_read_xfer);
@@ -194,16 +194,13 @@ static void sg_build_reserve(Sg_fd * sfp, int req_size);
194static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size); 194static void sg_link_reserve(Sg_fd * sfp, Sg_request * srp, int size);
195static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp); 195static void sg_unlink_reserve(Sg_fd * sfp, Sg_request * srp);
196static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev); 196static Sg_fd *sg_add_sfp(Sg_device * sdp, int dev);
197static int sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp); 197static void sg_remove_sfp(struct kref *);
198static void __sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp);
199static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id); 198static Sg_request *sg_get_rq_mark(Sg_fd * sfp, int pack_id);
200static Sg_request *sg_add_request(Sg_fd * sfp); 199static Sg_request *sg_add_request(Sg_fd * sfp);
201static int sg_remove_request(Sg_fd * sfp, Sg_request * srp); 200static int sg_remove_request(Sg_fd * sfp, Sg_request * srp);
202static int sg_res_in_use(Sg_fd * sfp); 201static int sg_res_in_use(Sg_fd * sfp);
203static Sg_device *sg_get_dev(int dev); 202static Sg_device *sg_get_dev(int dev);
204#ifdef CONFIG_SCSI_PROC_FS 203static void sg_put_dev(Sg_device *sdp);
205static int sg_last_dev(void);
206#endif
207 204
208#define SZ_SG_HEADER sizeof(struct sg_header) 205#define SZ_SG_HEADER sizeof(struct sg_header)
209#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t) 206#define SZ_SG_IO_HDR sizeof(sg_io_hdr_t)
@@ -237,22 +234,17 @@ sg_open(struct inode *inode, struct file *filp)
237 nonseekable_open(inode, filp); 234 nonseekable_open(inode, filp);
238 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags)); 235 SCSI_LOG_TIMEOUT(3, printk("sg_open: dev=%d, flags=0x%x\n", dev, flags));
239 sdp = sg_get_dev(dev); 236 sdp = sg_get_dev(dev);
240 if ((!sdp) || (!sdp->device)) { 237 if (IS_ERR(sdp)) {
241 unlock_kernel(); 238 retval = PTR_ERR(sdp);
242 return -ENXIO; 239 sdp = NULL;
243 } 240 goto sg_put;
244 if (sdp->detached) {
245 unlock_kernel();
246 return -ENODEV;
247 } 241 }
248 242
249 /* This driver's module count bumped by fops_get in <linux/fs.h> */ 243 /* This driver's module count bumped by fops_get in <linux/fs.h> */
250 /* Prevent the device driver from vanishing while we sleep */ 244 /* Prevent the device driver from vanishing while we sleep */
251 retval = scsi_device_get(sdp->device); 245 retval = scsi_device_get(sdp->device);
252 if (retval) { 246 if (retval)
253 unlock_kernel(); 247 goto sg_put;
254 return retval;
255 }
256 248
257 if (!((flags & O_NONBLOCK) || 249 if (!((flags & O_NONBLOCK) ||
258 scsi_block_when_processing_errors(sdp->device))) { 250 scsi_block_when_processing_errors(sdp->device))) {
@@ -266,13 +258,13 @@ sg_open(struct inode *inode, struct file *filp)
266 retval = -EPERM; /* Can't lock it with read only access */ 258 retval = -EPERM; /* Can't lock it with read only access */
267 goto error_out; 259 goto error_out;
268 } 260 }
269 if (sdp->headfp && (flags & O_NONBLOCK)) { 261 if (!list_empty(&sdp->sfds) && (flags & O_NONBLOCK)) {
270 retval = -EBUSY; 262 retval = -EBUSY;
271 goto error_out; 263 goto error_out;
272 } 264 }
273 res = 0; 265 res = 0;
274 __wait_event_interruptible(sdp->o_excl_wait, 266 __wait_event_interruptible(sdp->o_excl_wait,
275 ((sdp->headfp || sdp->exclude) ? 0 : (sdp->exclude = 1)), res); 267 ((!list_empty(&sdp->sfds) || sdp->exclude) ? 0 : (sdp->exclude = 1)), res);
276 if (res) { 268 if (res) {
277 retval = res; /* -ERESTARTSYS because signal hit process */ 269 retval = res; /* -ERESTARTSYS because signal hit process */
278 goto error_out; 270 goto error_out;
@@ -294,7 +286,7 @@ sg_open(struct inode *inode, struct file *filp)
294 retval = -ENODEV; 286 retval = -ENODEV;
295 goto error_out; 287 goto error_out;
296 } 288 }
297 if (!sdp->headfp) { /* no existing opens on this device */ 289 if (list_empty(&sdp->sfds)) { /* no existing opens on this device */
298 sdp->sgdebug = 0; 290 sdp->sgdebug = 0;
299 q = sdp->device->request_queue; 291 q = sdp->device->request_queue;
300 sdp->sg_tablesize = min(q->max_hw_segments, 292 sdp->sg_tablesize = min(q->max_hw_segments,
@@ -303,16 +295,20 @@ sg_open(struct inode *inode, struct file *filp)
303 if ((sfp = sg_add_sfp(sdp, dev))) 295 if ((sfp = sg_add_sfp(sdp, dev)))
304 filp->private_data = sfp; 296 filp->private_data = sfp;
305 else { 297 else {
306 if (flags & O_EXCL) 298 if (flags & O_EXCL) {
307 sdp->exclude = 0; /* undo if error */ 299 sdp->exclude = 0; /* undo if error */
300 wake_up_interruptible(&sdp->o_excl_wait);
301 }
308 retval = -ENOMEM; 302 retval = -ENOMEM;
309 goto error_out; 303 goto error_out;
310 } 304 }
311 unlock_kernel(); 305 retval = 0;
312 return 0; 306error_out:
313 307 if (retval)
314 error_out: 308 scsi_device_put(sdp->device);
315 scsi_device_put(sdp->device); 309sg_put:
310 if (sdp)
311 sg_put_dev(sdp);
316 unlock_kernel(); 312 unlock_kernel();
317 return retval; 313 return retval;
318} 314}
@@ -327,13 +323,13 @@ sg_release(struct inode *inode, struct file *filp)
327 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp))) 323 if ((!(sfp = (Sg_fd *) filp->private_data)) || (!(sdp = sfp->parentdp)))
328 return -ENXIO; 324 return -ENXIO;
329 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name)); 325 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
330 if (0 == sg_remove_sfp(sdp, sfp)) { /* Returns 1 when sdp gone */ 326
331 if (!sdp->detached) { 327 sfp->closed = 1;
332 scsi_device_put(sdp->device); 328
333 } 329 sdp->exclude = 0;
334 sdp->exclude = 0; 330 wake_up_interruptible(&sdp->o_excl_wait);
335 wake_up_interruptible(&sdp->o_excl_wait); 331
336 } 332 kref_put(&sfp->f_ref, sg_remove_sfp);
337 return 0; 333 return 0;
338} 334}
339 335
@@ -557,7 +553,8 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
557 return -EFAULT; 553 return -EFAULT;
558 blocking = !(filp->f_flags & O_NONBLOCK); 554 blocking = !(filp->f_flags & O_NONBLOCK);
559 if (old_hdr.reply_len < 0) 555 if (old_hdr.reply_len < 0)
560 return sg_new_write(sfp, filp, buf, count, blocking, 0, NULL); 556 return sg_new_write(sfp, filp, buf, count,
557 blocking, 0, 0, NULL);
561 if (count < (SZ_SG_HEADER + 6)) 558 if (count < (SZ_SG_HEADER + 6))
562 return -EIO; /* The minimum scsi command length is 6 bytes. */ 559 return -EIO; /* The minimum scsi command length is 6 bytes. */
563 560
@@ -638,7 +635,7 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
638 635
639static ssize_t 636static ssize_t
640sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf, 637sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
641 size_t count, int blocking, int read_only, 638 size_t count, int blocking, int read_only, int sg_io_owned,
642 Sg_request **o_srp) 639 Sg_request **o_srp)
643{ 640{
644 int k; 641 int k;
@@ -658,6 +655,7 @@ sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
658 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n")); 655 SCSI_LOG_TIMEOUT(1, printk("sg_new_write: queue full\n"));
659 return -EDOM; 656 return -EDOM;
660 } 657 }
658 srp->sg_io_owned = sg_io_owned;
661 hp = &srp->header; 659 hp = &srp->header;
662 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) { 660 if (__copy_from_user(hp, buf, SZ_SG_IO_HDR)) {
663 sg_remove_request(sfp, srp); 661 sg_remove_request(sfp, srp);
@@ -755,24 +753,13 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
755 hp->duration = jiffies_to_msecs(jiffies); 753 hp->duration = jiffies_to_msecs(jiffies);
756 754
757 srp->rq->timeout = timeout; 755 srp->rq->timeout = timeout;
756 kref_get(&sfp->f_ref); /* sg_rq_end_io() does kref_put(). */
758 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk, 757 blk_execute_rq_nowait(sdp->device->request_queue, sdp->disk,
759 srp->rq, 1, sg_rq_end_io); 758 srp->rq, 1, sg_rq_end_io);
760 return 0; 759 return 0;
761} 760}
762 761
763static int 762static int
764sg_srp_done(Sg_request *srp, Sg_fd *sfp)
765{
766 unsigned long iflags;
767 int done;
768
769 read_lock_irqsave(&sfp->rq_list_lock, iflags);
770 done = srp->done;
771 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
772 return done;
773}
774
775static int
776sg_ioctl(struct inode *inode, struct file *filp, 763sg_ioctl(struct inode *inode, struct file *filp,
777 unsigned int cmd_in, unsigned long arg) 764 unsigned int cmd_in, unsigned long arg)
778{ 765{
@@ -804,27 +791,26 @@ sg_ioctl(struct inode *inode, struct file *filp,
804 return -EFAULT; 791 return -EFAULT;
805 result = 792 result =
806 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR, 793 sg_new_write(sfp, filp, p, SZ_SG_IO_HDR,
807 blocking, read_only, &srp); 794 blocking, read_only, 1, &srp);
808 if (result < 0) 795 if (result < 0)
809 return result; 796 return result;
810 srp->sg_io_owned = 1;
811 while (1) { 797 while (1) {
812 result = 0; /* following macro to beat race condition */ 798 result = 0; /* following macro to beat race condition */
813 __wait_event_interruptible(sfp->read_wait, 799 __wait_event_interruptible(sfp->read_wait,
814 (sdp->detached || sfp->closed || sg_srp_done(srp, sfp)), 800 (srp->done || sdp->detached),
815 result); 801 result);
816 if (sdp->detached) 802 if (sdp->detached)
817 return -ENODEV; 803 return -ENODEV;
818 if (sfp->closed) 804 write_lock_irq(&sfp->rq_list_lock);
819 return 0; /* request packet dropped already */ 805 if (srp->done) {
820 if (0 == result) 806 srp->done = 2;
807 write_unlock_irq(&sfp->rq_list_lock);
821 break; 808 break;
809 }
822 srp->orphan = 1; 810 srp->orphan = 1;
811 write_unlock_irq(&sfp->rq_list_lock);
823 return result; /* -ERESTARTSYS because signal hit process */ 812 return result; /* -ERESTARTSYS because signal hit process */
824 } 813 }
825 write_lock_irqsave(&sfp->rq_list_lock, iflags);
826 srp->done = 2;
827 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
828 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp); 814 result = sg_new_read(sfp, p, SZ_SG_IO_HDR, srp);
829 return (result < 0) ? result : 0; 815 return (result < 0) ? result : 0;
830 } 816 }
@@ -1238,6 +1224,15 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
1238 return 0; 1224 return 0;
1239} 1225}
1240 1226
1227static void sg_rq_end_io_usercontext(struct work_struct *work)
1228{
1229 struct sg_request *srp = container_of(work, struct sg_request, ew.work);
1230 struct sg_fd *sfp = srp->parentfp;
1231
1232 sg_finish_rem_req(srp);
1233 kref_put(&sfp->f_ref, sg_remove_sfp);
1234}
1235
1241/* 1236/*
1242 * This function is a "bottom half" handler that is called by the mid 1237 * This function is a "bottom half" handler that is called by the mid
1243 * level when a command is completed (or has failed). 1238 * level when a command is completed (or has failed).
@@ -1245,24 +1240,23 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
1245static void sg_rq_end_io(struct request *rq, int uptodate) 1240static void sg_rq_end_io(struct request *rq, int uptodate)
1246{ 1241{
1247 struct sg_request *srp = rq->end_io_data; 1242 struct sg_request *srp = rq->end_io_data;
1248 Sg_device *sdp = NULL; 1243 Sg_device *sdp;
1249 Sg_fd *sfp; 1244 Sg_fd *sfp;
1250 unsigned long iflags; 1245 unsigned long iflags;
1251 unsigned int ms; 1246 unsigned int ms;
1252 char *sense; 1247 char *sense;
1253 int result, resid; 1248 int result, resid, done = 1;
1254 1249
1255 if (NULL == srp) { 1250 if (WARN_ON(srp->done != 0))
1256 printk(KERN_ERR "sg_cmd_done: NULL request\n");
1257 return; 1251 return;
1258 } 1252
1259 sfp = srp->parentfp; 1253 sfp = srp->parentfp;
1260 if (sfp) 1254 if (WARN_ON(sfp == NULL))
1261 sdp = sfp->parentdp;
1262 if ((NULL == sdp) || sdp->detached) {
1263 printk(KERN_INFO "sg_cmd_done: device detached\n");
1264 return; 1255 return;
1265 } 1256
1257 sdp = sfp->parentdp;
1258 if (unlikely(sdp->detached))
1259 printk(KERN_INFO "sg_rq_end_io: device detached\n");
1266 1260
1267 sense = rq->sense; 1261 sense = rq->sense;
1268 result = rq->errors; 1262 result = rq->errors;
@@ -1301,33 +1295,25 @@ static void sg_rq_end_io(struct request *rq, int uptodate)
1301 } 1295 }
1302 /* Rely on write phase to clean out srp status values, so no "else" */ 1296 /* Rely on write phase to clean out srp status values, so no "else" */
1303 1297
1304 if (sfp->closed) { /* whoops this fd already released, cleanup */ 1298 write_lock_irqsave(&sfp->rq_list_lock, iflags);
1305 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, freeing ...\n")); 1299 if (unlikely(srp->orphan)) {
1306 sg_finish_rem_req(srp);
1307 srp = NULL;
1308 if (NULL == sfp->headrp) {
1309 SCSI_LOG_TIMEOUT(1, printk("sg_cmd_done: already closed, final cleanup\n"));
1310 if (0 == sg_remove_sfp(sdp, sfp)) { /* device still present */
1311 scsi_device_put(sdp->device);
1312 }
1313 sfp = NULL;
1314 }
1315 } else if (srp && srp->orphan) {
1316 if (sfp->keep_orphan) 1300 if (sfp->keep_orphan)
1317 srp->sg_io_owned = 0; 1301 srp->sg_io_owned = 0;
1318 else { 1302 else
1319 sg_finish_rem_req(srp); 1303 done = 0;
1320 srp = NULL;
1321 }
1322 } 1304 }
1323 if (sfp && srp) { 1305 srp->done = done;
1324 /* Now wake up any sg_read() that is waiting for this packet. */ 1306 write_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1325 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN); 1307
1326 write_lock_irqsave(&sfp->rq_list_lock, iflags); 1308 if (likely(done)) {
1327 srp->done = 1; 1309 /* Now wake up any sg_read() that is waiting for this
1310 * packet.
1311 */
1328 wake_up_interruptible(&sfp->read_wait); 1312 wake_up_interruptible(&sfp->read_wait);
1329 write_unlock_irqrestore(&sfp->rq_list_lock, iflags); 1313 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_IN);
1330 } 1314 kref_put(&sfp->f_ref, sg_remove_sfp);
1315 } else
1316 execute_in_process_context(sg_rq_end_io_usercontext, &srp->ew);
1331} 1317}
1332 1318
1333static struct file_operations sg_fops = { 1319static struct file_operations sg_fops = {
@@ -1362,17 +1348,18 @@ static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1362 printk(KERN_WARNING "kmalloc Sg_device failure\n"); 1348 printk(KERN_WARNING "kmalloc Sg_device failure\n");
1363 return ERR_PTR(-ENOMEM); 1349 return ERR_PTR(-ENOMEM);
1364 } 1350 }
1365 error = -ENOMEM; 1351
1366 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) { 1352 if (!idr_pre_get(&sg_index_idr, GFP_KERNEL)) {
1367 printk(KERN_WARNING "idr expansion Sg_device failure\n"); 1353 printk(KERN_WARNING "idr expansion Sg_device failure\n");
1354 error = -ENOMEM;
1368 goto out; 1355 goto out;
1369 } 1356 }
1370 1357
1371 write_lock_irqsave(&sg_index_lock, iflags); 1358 write_lock_irqsave(&sg_index_lock, iflags);
1372 error = idr_get_new(&sg_index_idr, sdp, &k);
1373 write_unlock_irqrestore(&sg_index_lock, iflags);
1374 1359
1360 error = idr_get_new(&sg_index_idr, sdp, &k);
1375 if (error) { 1361 if (error) {
1362 write_unlock_irqrestore(&sg_index_lock, iflags);
1376 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n", 1363 printk(KERN_WARNING "idr allocation Sg_device failure: %d\n",
1377 error); 1364 error);
1378 goto out; 1365 goto out;
@@ -1386,9 +1373,13 @@ static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1386 disk->first_minor = k; 1373 disk->first_minor = k;
1387 sdp->disk = disk; 1374 sdp->disk = disk;
1388 sdp->device = scsidp; 1375 sdp->device = scsidp;
1376 INIT_LIST_HEAD(&sdp->sfds);
1389 init_waitqueue_head(&sdp->o_excl_wait); 1377 init_waitqueue_head(&sdp->o_excl_wait);
1390 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments); 1378 sdp->sg_tablesize = min(q->max_hw_segments, q->max_phys_segments);
1391 sdp->index = k; 1379 sdp->index = k;
1380 kref_init(&sdp->d_ref);
1381
1382 write_unlock_irqrestore(&sg_index_lock, iflags);
1392 1383
1393 error = 0; 1384 error = 0;
1394 out: 1385 out:
@@ -1399,6 +1390,8 @@ static Sg_device *sg_alloc(struct gendisk *disk, struct scsi_device *scsidp)
1399 return sdp; 1390 return sdp;
1400 1391
1401 overflow: 1392 overflow:
1393 idr_remove(&sg_index_idr, k);
1394 write_unlock_irqrestore(&sg_index_lock, iflags);
1402 sdev_printk(KERN_WARNING, scsidp, 1395 sdev_printk(KERN_WARNING, scsidp,
1403 "Unable to attach sg device type=%d, minor " 1396 "Unable to attach sg device type=%d, minor "
1404 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1); 1397 "number exceeds %d\n", scsidp->type, SG_MAX_DEVS - 1);
@@ -1486,49 +1479,46 @@ out:
1486 return error; 1479 return error;
1487} 1480}
1488 1481
1489static void 1482static void sg_device_destroy(struct kref *kref)
1490sg_remove(struct device *cl_dev, struct class_interface *cl_intf) 1483{
1484 struct sg_device *sdp = container_of(kref, struct sg_device, d_ref);
1485 unsigned long flags;
1486
1487 /* CAUTION! Note that the device can still be found via idr_find()
1488 * even though the refcount is 0. Therefore, do idr_remove() BEFORE
1489 * any other cleanup.
1490 */
1491
1492 write_lock_irqsave(&sg_index_lock, flags);
1493 idr_remove(&sg_index_idr, sdp->index);
1494 write_unlock_irqrestore(&sg_index_lock, flags);
1495
1496 SCSI_LOG_TIMEOUT(3,
1497 printk("sg_device_destroy: %s\n",
1498 sdp->disk->disk_name));
1499
1500 put_disk(sdp->disk);
1501 kfree(sdp);
1502}
1503
1504static void sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
1491{ 1505{
1492 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent); 1506 struct scsi_device *scsidp = to_scsi_device(cl_dev->parent);
1493 Sg_device *sdp = dev_get_drvdata(cl_dev); 1507 Sg_device *sdp = dev_get_drvdata(cl_dev);
1494 unsigned long iflags; 1508 unsigned long iflags;
1495 Sg_fd *sfp; 1509 Sg_fd *sfp;
1496 Sg_fd *tsfp;
1497 Sg_request *srp;
1498 Sg_request *tsrp;
1499 int delay;
1500 1510
1501 if (!sdp) 1511 if (!sdp || sdp->detached)
1502 return; 1512 return;
1503 1513
1504 delay = 0; 1514 SCSI_LOG_TIMEOUT(3, printk("sg_remove: %s\n", sdp->disk->disk_name));
1515
1516 /* Need a write lock to set sdp->detached. */
1505 write_lock_irqsave(&sg_index_lock, iflags); 1517 write_lock_irqsave(&sg_index_lock, iflags);
1506 if (sdp->headfp) { 1518 sdp->detached = 1;
1507 sdp->detached = 1; 1519 list_for_each_entry(sfp, &sdp->sfds, sfd_siblings) {
1508 for (sfp = sdp->headfp; sfp; sfp = tsfp) { 1520 wake_up_interruptible(&sfp->read_wait);
1509 tsfp = sfp->nextfp; 1521 kill_fasync(&sfp->async_qp, SIGPOLL, POLL_HUP);
1510 for (srp = sfp->headrp; srp; srp = tsrp) {
1511 tsrp = srp->nextrp;
1512 if (sfp->closed || (0 == sg_srp_done(srp, sfp)))
1513 sg_finish_rem_req(srp);
1514 }
1515 if (sfp->closed) {
1516 scsi_device_put(sdp->device);
1517 __sg_remove_sfp(sdp, sfp);
1518 } else {
1519 delay = 1;
1520 wake_up_interruptible(&sfp->read_wait);
1521 kill_fasync(&sfp->async_qp, SIGPOLL,
1522 POLL_HUP);
1523 }
1524 }
1525 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d, dirty\n", sdp->index));
1526 if (NULL == sdp->headfp) {
1527 idr_remove(&sg_index_idr, sdp->index);
1528 }
1529 } else { /* nothing active, simple case */
1530 SCSI_LOG_TIMEOUT(3, printk("sg_remove: dev=%d\n", sdp->index));
1531 idr_remove(&sg_index_idr, sdp->index);
1532 } 1522 }
1533 write_unlock_irqrestore(&sg_index_lock, iflags); 1523 write_unlock_irqrestore(&sg_index_lock, iflags);
1534 1524
@@ -1536,13 +1526,8 @@ sg_remove(struct device *cl_dev, struct class_interface *cl_intf)
1536 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index)); 1526 device_destroy(sg_sysfs_class, MKDEV(SCSI_GENERIC_MAJOR, sdp->index));
1537 cdev_del(sdp->cdev); 1527 cdev_del(sdp->cdev);
1538 sdp->cdev = NULL; 1528 sdp->cdev = NULL;
1539 put_disk(sdp->disk);
1540 sdp->disk = NULL;
1541 if (NULL == sdp->headfp)
1542 kfree(sdp);
1543 1529
1544 if (delay) 1530 sg_put_dev(sdp);
1545 msleep(10); /* dirty detach so delay device destruction */
1546} 1531}
1547 1532
1548module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR); 1533module_param_named(scatter_elem_sz, scatter_elem_sz, int, S_IRUGO | S_IWUSR);
@@ -1736,8 +1721,8 @@ sg_build_indirect(Sg_scatter_hold * schp, Sg_fd * sfp, int buff_size)
1736 return -EFAULT; 1721 return -EFAULT;
1737 if (0 == blk_size) 1722 if (0 == blk_size)
1738 ++blk_size; /* don't know why */ 1723 ++blk_size; /* don't know why */
1739/* round request up to next highest SG_SECTOR_SZ byte boundary */ 1724 /* round request up to next highest SG_SECTOR_SZ byte boundary */
1740 blk_size = (blk_size + SG_SECTOR_MSK) & (~SG_SECTOR_MSK); 1725 blk_size = ALIGN(blk_size, SG_SECTOR_SZ);
1741 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n", 1726 SCSI_LOG_TIMEOUT(4, printk("sg_build_indirect: buff_size=%d, blk_size=%d\n",
1742 buff_size, blk_size)); 1727 buff_size, blk_size));
1743 1728
@@ -1939,22 +1924,6 @@ sg_get_rq_mark(Sg_fd * sfp, int pack_id)
1939 return resp; 1924 return resp;
1940} 1925}
1941 1926
1942#ifdef CONFIG_SCSI_PROC_FS
1943static Sg_request *
1944sg_get_nth_request(Sg_fd * sfp, int nth)
1945{
1946 Sg_request *resp;
1947 unsigned long iflags;
1948 int k;
1949
1950 read_lock_irqsave(&sfp->rq_list_lock, iflags);
1951 for (k = 0, resp = sfp->headrp; resp && (k < nth);
1952 ++k, resp = resp->nextrp) ;
1953 read_unlock_irqrestore(&sfp->rq_list_lock, iflags);
1954 return resp;
1955}
1956#endif
1957
1958/* always adds to end of list */ 1927/* always adds to end of list */
1959static Sg_request * 1928static Sg_request *
1960sg_add_request(Sg_fd * sfp) 1929sg_add_request(Sg_fd * sfp)
@@ -2030,22 +1999,6 @@ sg_remove_request(Sg_fd * sfp, Sg_request * srp)
2030 return res; 1999 return res;
2031} 2000}
2032 2001
2033#ifdef CONFIG_SCSI_PROC_FS
2034static Sg_fd *
2035sg_get_nth_sfp(Sg_device * sdp, int nth)
2036{
2037 Sg_fd *resp;
2038 unsigned long iflags;
2039 int k;
2040
2041 read_lock_irqsave(&sg_index_lock, iflags);
2042 for (k = 0, resp = sdp->headfp; resp && (k < nth);
2043 ++k, resp = resp->nextfp) ;
2044 read_unlock_irqrestore(&sg_index_lock, iflags);
2045 return resp;
2046}
2047#endif
2048
2049static Sg_fd * 2002static Sg_fd *
2050sg_add_sfp(Sg_device * sdp, int dev) 2003sg_add_sfp(Sg_device * sdp, int dev)
2051{ 2004{
@@ -2060,6 +2013,7 @@ sg_add_sfp(Sg_device * sdp, int dev)
2060 init_waitqueue_head(&sfp->read_wait); 2013 init_waitqueue_head(&sfp->read_wait);
2061 rwlock_init(&sfp->rq_list_lock); 2014 rwlock_init(&sfp->rq_list_lock);
2062 2015
2016 kref_init(&sfp->f_ref);
2063 sfp->timeout = SG_DEFAULT_TIMEOUT; 2017 sfp->timeout = SG_DEFAULT_TIMEOUT;
2064 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER; 2018 sfp->timeout_user = SG_DEFAULT_TIMEOUT_USER;
2065 sfp->force_packid = SG_DEF_FORCE_PACK_ID; 2019 sfp->force_packid = SG_DEF_FORCE_PACK_ID;
@@ -2069,14 +2023,7 @@ sg_add_sfp(Sg_device * sdp, int dev)
2069 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN; 2023 sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
2070 sfp->parentdp = sdp; 2024 sfp->parentdp = sdp;
2071 write_lock_irqsave(&sg_index_lock, iflags); 2025 write_lock_irqsave(&sg_index_lock, iflags);
2072 if (!sdp->headfp) 2026 list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
2073 sdp->headfp = sfp;
2074 else { /* add to tail of existing list */
2075 Sg_fd *pfp = sdp->headfp;
2076 while (pfp->nextfp)
2077 pfp = pfp->nextfp;
2078 pfp->nextfp = sfp;
2079 }
2080 write_unlock_irqrestore(&sg_index_lock, iflags); 2027 write_unlock_irqrestore(&sg_index_lock, iflags);
2081 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp)); 2028 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));
2082 if (unlikely(sg_big_buff != def_reserved_size)) 2029 if (unlikely(sg_big_buff != def_reserved_size))
@@ -2087,75 +2034,52 @@ sg_add_sfp(Sg_device * sdp, int dev)
2087 sg_build_reserve(sfp, bufflen); 2034 sg_build_reserve(sfp, bufflen);
2088 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n", 2035 SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: bufflen=%d, k_use_sg=%d\n",
2089 sfp->reserve.bufflen, sfp->reserve.k_use_sg)); 2036 sfp->reserve.bufflen, sfp->reserve.k_use_sg));
2037
2038 kref_get(&sdp->d_ref);
2039 __module_get(THIS_MODULE);
2090 return sfp; 2040 return sfp;
2091} 2041}
2092 2042
2093static void 2043static void sg_remove_sfp_usercontext(struct work_struct *work)
2094__sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2095{ 2044{
2096 Sg_fd *fp; 2045 struct sg_fd *sfp = container_of(work, struct sg_fd, ew.work);
2097 Sg_fd *prev_fp; 2046 struct sg_device *sdp = sfp->parentdp;
2047
2048 /* Cleanup any responses which were never read(). */
2049 while (sfp->headrp)
2050 sg_finish_rem_req(sfp->headrp);
2098 2051
2099 prev_fp = sdp->headfp;
2100 if (sfp == prev_fp)
2101 sdp->headfp = prev_fp->nextfp;
2102 else {
2103 while ((fp = prev_fp->nextfp)) {
2104 if (sfp == fp) {
2105 prev_fp->nextfp = fp->nextfp;
2106 break;
2107 }
2108 prev_fp = fp;
2109 }
2110 }
2111 if (sfp->reserve.bufflen > 0) { 2052 if (sfp->reserve.bufflen > 0) {
2112 SCSI_LOG_TIMEOUT(6, 2053 SCSI_LOG_TIMEOUT(6,
2113 printk("__sg_remove_sfp: bufflen=%d, k_use_sg=%d\n", 2054 printk("sg_remove_sfp: bufflen=%d, k_use_sg=%d\n",
2114 (int) sfp->reserve.bufflen, (int) sfp->reserve.k_use_sg)); 2055 (int) sfp->reserve.bufflen,
2056 (int) sfp->reserve.k_use_sg));
2115 sg_remove_scat(&sfp->reserve); 2057 sg_remove_scat(&sfp->reserve);
2116 } 2058 }
2117 sfp->parentdp = NULL; 2059
2118 SCSI_LOG_TIMEOUT(6, printk("__sg_remove_sfp: sfp=0x%p\n", sfp)); 2060 SCSI_LOG_TIMEOUT(6,
2061 printk("sg_remove_sfp: %s, sfp=0x%p\n",
2062 sdp->disk->disk_name,
2063 sfp));
2119 kfree(sfp); 2064 kfree(sfp);
2065
2066 scsi_device_put(sdp->device);
2067 sg_put_dev(sdp);
2068 module_put(THIS_MODULE);
2120} 2069}
2121 2070
2122/* Returns 0 in normal case, 1 when detached and sdp object removed */ 2071static void sg_remove_sfp(struct kref *kref)
2123static int
2124sg_remove_sfp(Sg_device * sdp, Sg_fd * sfp)
2125{ 2072{
2126 Sg_request *srp; 2073 struct sg_fd *sfp = container_of(kref, struct sg_fd, f_ref);
2127 Sg_request *tsrp; 2074 struct sg_device *sdp = sfp->parentdp;
2128 int dirty = 0; 2075 unsigned long iflags;
2129 int res = 0;
2130 2076
2131 for (srp = sfp->headrp; srp; srp = tsrp) { 2077 write_lock_irqsave(&sg_index_lock, iflags);
2132 tsrp = srp->nextrp; 2078 list_del(&sfp->sfd_siblings);
2133 if (sg_srp_done(srp, sfp)) 2079 write_unlock_irqrestore(&sg_index_lock, iflags);
2134 sg_finish_rem_req(srp); 2080 wake_up_interruptible(&sdp->o_excl_wait);
2135 else 2081
2136 ++dirty; 2082 execute_in_process_context(sg_remove_sfp_usercontext, &sfp->ew);
2137 }
2138 if (0 == dirty) {
2139 unsigned long iflags;
2140
2141 write_lock_irqsave(&sg_index_lock, iflags);
2142 __sg_remove_sfp(sdp, sfp);
2143 if (sdp->detached && (NULL == sdp->headfp)) {
2144 idr_remove(&sg_index_idr, sdp->index);
2145 kfree(sdp);
2146 res = 1;
2147 }
2148 write_unlock_irqrestore(&sg_index_lock, iflags);
2149 } else {
2150 /* MOD_INC's to inhibit unloading sg and associated adapter driver */
2151 /* only bump the access_count if we actually succeeded in
2152 * throwing another counter on the host module */
2153 scsi_device_get(sdp->device); /* XXX: retval ignored? */
2154 sfp->closed = 1; /* flag dirty state on this fd */
2155 SCSI_LOG_TIMEOUT(1, printk("sg_remove_sfp: worrisome, %d writes pending\n",
2156 dirty));
2157 }
2158 return res;
2159} 2083}
2160 2084
2161static int 2085static int
@@ -2197,19 +2121,38 @@ sg_last_dev(void)
2197} 2121}
2198#endif 2122#endif
2199 2123
2200static Sg_device * 2124/* must be called with sg_index_lock held */
2201sg_get_dev(int dev) 2125static Sg_device *sg_lookup_dev(int dev)
2202{ 2126{
2203 Sg_device *sdp; 2127 return idr_find(&sg_index_idr, dev);
2204 unsigned long iflags; 2128}
2205 2129
2206 read_lock_irqsave(&sg_index_lock, iflags); 2130static Sg_device *sg_get_dev(int dev)
2207 sdp = idr_find(&sg_index_idr, dev); 2131{
2208 read_unlock_irqrestore(&sg_index_lock, iflags); 2132 struct sg_device *sdp;
2133 unsigned long flags;
2134
2135 read_lock_irqsave(&sg_index_lock, flags);
2136 sdp = sg_lookup_dev(dev);
2137 if (!sdp)
2138 sdp = ERR_PTR(-ENXIO);
2139 else if (sdp->detached) {
2140 /* If sdp->detached, then the refcount may already be 0, in
2141 * which case it would be a bug to do kref_get().
2142 */
2143 sdp = ERR_PTR(-ENODEV);
2144 } else
2145 kref_get(&sdp->d_ref);
2146 read_unlock_irqrestore(&sg_index_lock, flags);
2209 2147
2210 return sdp; 2148 return sdp;
2211} 2149}
2212 2150
2151static void sg_put_dev(struct sg_device *sdp)
2152{
2153 kref_put(&sdp->d_ref, sg_device_destroy);
2154}
2155
2213#ifdef CONFIG_SCSI_PROC_FS 2156#ifdef CONFIG_SCSI_PROC_FS
2214 2157
2215static struct proc_dir_entry *sg_proc_sgp = NULL; 2158static struct proc_dir_entry *sg_proc_sgp = NULL;
@@ -2466,8 +2409,10 @@ static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2466 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; 2409 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2467 Sg_device *sdp; 2410 Sg_device *sdp;
2468 struct scsi_device *scsidp; 2411 struct scsi_device *scsidp;
2412 unsigned long iflags;
2469 2413
2470 sdp = it ? sg_get_dev(it->index) : NULL; 2414 read_lock_irqsave(&sg_index_lock, iflags);
2415 sdp = it ? sg_lookup_dev(it->index) : NULL;
2471 if (sdp && (scsidp = sdp->device) && (!sdp->detached)) 2416 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2472 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", 2417 seq_printf(s, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
2473 scsidp->host->host_no, scsidp->channel, 2418 scsidp->host->host_no, scsidp->channel,
@@ -2478,6 +2423,7 @@ static int sg_proc_seq_show_dev(struct seq_file *s, void *v)
2478 (int) scsi_device_online(scsidp)); 2423 (int) scsi_device_online(scsidp));
2479 else 2424 else
2480 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n"); 2425 seq_printf(s, "-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\t-1\n");
2426 read_unlock_irqrestore(&sg_index_lock, iflags);
2481 return 0; 2427 return 0;
2482} 2428}
2483 2429
@@ -2491,16 +2437,20 @@ static int sg_proc_seq_show_devstrs(struct seq_file *s, void *v)
2491 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; 2437 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2492 Sg_device *sdp; 2438 Sg_device *sdp;
2493 struct scsi_device *scsidp; 2439 struct scsi_device *scsidp;
2440 unsigned long iflags;
2494 2441
2495 sdp = it ? sg_get_dev(it->index) : NULL; 2442 read_lock_irqsave(&sg_index_lock, iflags);
2443 sdp = it ? sg_lookup_dev(it->index) : NULL;
2496 if (sdp && (scsidp = sdp->device) && (!sdp->detached)) 2444 if (sdp && (scsidp = sdp->device) && (!sdp->detached))
2497 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n", 2445 seq_printf(s, "%8.8s\t%16.16s\t%4.4s\n",
2498 scsidp->vendor, scsidp->model, scsidp->rev); 2446 scsidp->vendor, scsidp->model, scsidp->rev);
2499 else 2447 else
2500 seq_printf(s, "<no active device>\n"); 2448 seq_printf(s, "<no active device>\n");
2449 read_unlock_irqrestore(&sg_index_lock, iflags);
2501 return 0; 2450 return 0;
2502} 2451}
2503 2452
2453/* must be called while holding sg_index_lock */
2504static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp) 2454static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2505{ 2455{
2506 int k, m, new_interface, blen, usg; 2456 int k, m, new_interface, blen, usg;
@@ -2510,9 +2460,12 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2510 const char * cp; 2460 const char * cp;
2511 unsigned int ms; 2461 unsigned int ms;
2512 2462
2513 for (k = 0; (fp = sg_get_nth_sfp(sdp, k)); ++k) { 2463 k = 0;
2464 list_for_each_entry(fp, &sdp->sfds, sfd_siblings) {
2465 k++;
2466 read_lock(&fp->rq_list_lock); /* irqs already disabled */
2514 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d " 2467 seq_printf(s, " FD(%d): timeout=%dms bufflen=%d "
2515 "(res)sgat=%d low_dma=%d\n", k + 1, 2468 "(res)sgat=%d low_dma=%d\n", k,
2516 jiffies_to_msecs(fp->timeout), 2469 jiffies_to_msecs(fp->timeout),
2517 fp->reserve.bufflen, 2470 fp->reserve.bufflen,
2518 (int) fp->reserve.k_use_sg, 2471 (int) fp->reserve.k_use_sg,
@@ -2520,7 +2473,9 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2520 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n", 2473 seq_printf(s, " cmd_q=%d f_packid=%d k_orphan=%d closed=%d\n",
2521 (int) fp->cmd_q, (int) fp->force_packid, 2474 (int) fp->cmd_q, (int) fp->force_packid,
2522 (int) fp->keep_orphan, (int) fp->closed); 2475 (int) fp->keep_orphan, (int) fp->closed);
2523 for (m = 0; (srp = sg_get_nth_request(fp, m)); ++m) { 2476 for (m = 0, srp = fp->headrp;
2477 srp != NULL;
2478 ++m, srp = srp->nextrp) {
2524 hp = &srp->header; 2479 hp = &srp->header;
2525 new_interface = (hp->interface_id == '\0') ? 0 : 1; 2480 new_interface = (hp->interface_id == '\0') ? 0 : 1;
2526 if (srp->res_used) { 2481 if (srp->res_used) {
@@ -2557,6 +2512,7 @@ static void sg_proc_debug_helper(struct seq_file *s, Sg_device * sdp)
2557 } 2512 }
2558 if (0 == m) 2513 if (0 == m)
2559 seq_printf(s, " No requests active\n"); 2514 seq_printf(s, " No requests active\n");
2515 read_unlock(&fp->rq_list_lock);
2560 } 2516 }
2561} 2517}
2562 2518
@@ -2569,39 +2525,34 @@ static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2569{ 2525{
2570 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v; 2526 struct sg_proc_deviter * it = (struct sg_proc_deviter *) v;
2571 Sg_device *sdp; 2527 Sg_device *sdp;
2528 unsigned long iflags;
2572 2529
2573 if (it && (0 == it->index)) { 2530 if (it && (0 == it->index)) {
2574 seq_printf(s, "max_active_device=%d(origin 1)\n", 2531 seq_printf(s, "max_active_device=%d(origin 1)\n",
2575 (int)it->max); 2532 (int)it->max);
2576 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff); 2533 seq_printf(s, " def_reserved_size=%d\n", sg_big_buff);
2577 } 2534 }
2578 sdp = it ? sg_get_dev(it->index) : NULL;
2579 if (sdp) {
2580 struct scsi_device *scsidp = sdp->device;
2581 2535
2582 if (NULL == scsidp) { 2536 read_lock_irqsave(&sg_index_lock, iflags);
2583 seq_printf(s, "device %d detached ??\n", 2537 sdp = it ? sg_lookup_dev(it->index) : NULL;
2584 (int)it->index); 2538 if (sdp && !list_empty(&sdp->sfds)) {
2585 return 0; 2539 struct scsi_device *scsidp = sdp->device;
2586 }
2587 2540
2588 if (sg_get_nth_sfp(sdp, 0)) { 2541 seq_printf(s, " >>> device=%s ", sdp->disk->disk_name);
2589 seq_printf(s, " >>> device=%s ", 2542 if (sdp->detached)
2590 sdp->disk->disk_name); 2543 seq_printf(s, "detached pending close ");
2591 if (sdp->detached) 2544 else
2592 seq_printf(s, "detached pending close "); 2545 seq_printf
2593 else 2546 (s, "scsi%d chan=%d id=%d lun=%d em=%d",
2594 seq_printf 2547 scsidp->host->host_no,
2595 (s, "scsi%d chan=%d id=%d lun=%d em=%d", 2548 scsidp->channel, scsidp->id,
2596 scsidp->host->host_no, 2549 scsidp->lun,
2597 scsidp->channel, scsidp->id, 2550 scsidp->host->hostt->emulated);
2598 scsidp->lun, 2551 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2599 scsidp->host->hostt->emulated); 2552 sdp->sg_tablesize, sdp->exclude);
2600 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2601 sdp->sg_tablesize, sdp->exclude);
2602 }
2603 sg_proc_debug_helper(s, sdp); 2553 sg_proc_debug_helper(s, sdp);
2604 } 2554 }
2555 read_unlock_irqrestore(&sg_index_lock, iflags);
2605 return 0; 2556 return 0;
2606} 2557}
2607 2558