aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/sg.c
diff options
context:
space:
mode:
authorJörn Engel <joern@logfs.org>2012-04-24 16:13:11 -0400
committerJames Bottomley <JBottomley@Parallels.com>2012-05-17 05:08:55 -0400
commitb499e5249eb80e4a7e71cfd04c6f628abdb27498 (patch)
tree003abeec481756acce963a8c840e7f787d464c97 /drivers/scsi/sg.c
parent6acddc5e911bb3a4a007448371ed7317c85669da (diff)
[SCSI] sg: protect sdp->exclude
Changes since v1: set_exclude now returns the new value, which gets rid of the comma expression and the operator precedence bug. Thanks to Douglas for spotting it. sdp->exclude was previously protected by the BKL. The sg_mutex, which replaced the BKL, only semi-protected it, as it was missing from sg_release() and sg_proc_seq_show_debug(). Take an explicit spinlock for it. Signed-off-by: Joern Engel <joern@logfs.org> Acked-by: Douglas Gilbert <dgilbert@interlog.com> Signed-off-by: James Bottomley <JBottomley@Parallels.com>
Diffstat (limited to 'drivers/scsi/sg.c')
-rw-r--r--drivers/scsi/sg.c37
1 files changed, 30 insertions, 7 deletions
diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 4a00364445f0..8e15c448a761 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -105,6 +105,7 @@ static int sg_add(struct device *, struct class_interface *);
105static void sg_remove(struct device *, struct class_interface *); 105static void sg_remove(struct device *, struct class_interface *);
106 106
107static DEFINE_MUTEX(sg_mutex); 107static DEFINE_MUTEX(sg_mutex);
108static DEFINE_SPINLOCK(sg_open_exclusive_lock);
108 109
109static DEFINE_IDR(sg_index_idr); 110static DEFINE_IDR(sg_index_idr);
110static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock 111static DEFINE_RWLOCK(sg_index_lock); /* Also used to lock
@@ -173,7 +174,8 @@ typedef struct sg_device { /* holds the state of each scsi generic device */
173 u32 index; /* device index number */ 174 u32 index; /* device index number */
174 struct list_head sfds; 175 struct list_head sfds;
175 volatile char detached; /* 0->attached, 1->detached pending removal */ 176 volatile char detached; /* 0->attached, 1->detached pending removal */
176 volatile char exclude; /* opened for exclusive access */ 177 /* exclude protected by sg_open_exclusive_lock */
178 char exclude; /* opened for exclusive access */
177 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */ 179 char sgdebug; /* 0->off, 1->sense, 9->dump dev, 10-> all devs */
178 struct gendisk *disk; 180 struct gendisk *disk;
179 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */ 181 struct cdev * cdev; /* char_dev [sysfs: /sys/cdev/major/sg<n>] */
@@ -221,6 +223,27 @@ static int sg_allow_access(struct file *filp, unsigned char *cmd)
221 return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE); 223 return blk_verify_command(cmd, filp->f_mode & FMODE_WRITE);
222} 224}
223 225
226static int get_exclude(Sg_device *sdp)
227{
228 unsigned long flags;
229 int ret;
230
231 spin_lock_irqsave(&sg_open_exclusive_lock, flags);
232 ret = sdp->exclude;
233 spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
234 return ret;
235}
236
237static int set_exclude(Sg_device *sdp, char val)
238{
239 unsigned long flags;
240
241 spin_lock_irqsave(&sg_open_exclusive_lock, flags);
242 sdp->exclude = val;
243 spin_unlock_irqrestore(&sg_open_exclusive_lock, flags);
244 return val;
245}
246
224static int 247static int
225sg_open(struct inode *inode, struct file *filp) 248sg_open(struct inode *inode, struct file *filp)
226{ 249{
@@ -269,17 +292,17 @@ sg_open(struct inode *inode, struct file *filp)
269 goto error_out; 292 goto error_out;
270 } 293 }
271 res = wait_event_interruptible(sdp->o_excl_wait, 294 res = wait_event_interruptible(sdp->o_excl_wait,
272 ((!list_empty(&sdp->sfds) || sdp->exclude) ? 0 : (sdp->exclude = 1))); 295 ((!list_empty(&sdp->sfds) || get_exclude(sdp)) ? 0 : set_exclude(sdp, 1)));
273 if (res) { 296 if (res) {
274 retval = res; /* -ERESTARTSYS because signal hit process */ 297 retval = res; /* -ERESTARTSYS because signal hit process */
275 goto error_out; 298 goto error_out;
276 } 299 }
277 } else if (sdp->exclude) { /* some other fd has an exclusive lock on dev */ 300 } else if (get_exclude(sdp)) { /* some other fd has an exclusive lock on dev */
278 if (flags & O_NONBLOCK) { 301 if (flags & O_NONBLOCK) {
279 retval = -EBUSY; 302 retval = -EBUSY;
280 goto error_out; 303 goto error_out;
281 } 304 }
282 res = wait_event_interruptible(sdp->o_excl_wait, !sdp->exclude); 305 res = wait_event_interruptible(sdp->o_excl_wait, !get_exclude(sdp));
283 if (res) { 306 if (res) {
284 retval = res; /* -ERESTARTSYS because signal hit process */ 307 retval = res; /* -ERESTARTSYS because signal hit process */
285 goto error_out; 308 goto error_out;
@@ -298,7 +321,7 @@ sg_open(struct inode *inode, struct file *filp)
298 filp->private_data = sfp; 321 filp->private_data = sfp;
299 else { 322 else {
300 if (flags & O_EXCL) { 323 if (flags & O_EXCL) {
301 sdp->exclude = 0; /* undo if error */ 324 set_exclude(sdp, 0); /* undo if error */
302 wake_up_interruptible(&sdp->o_excl_wait); 325 wake_up_interruptible(&sdp->o_excl_wait);
303 } 326 }
304 retval = -ENOMEM; 327 retval = -ENOMEM;
@@ -329,7 +352,7 @@ sg_release(struct inode *inode, struct file *filp)
329 return -ENXIO; 352 return -ENXIO;
330 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name)); 353 SCSI_LOG_TIMEOUT(3, printk("sg_release: %s\n", sdp->disk->disk_name));
331 354
332 sdp->exclude = 0; 355 set_exclude(sdp, 0);
333 wake_up_interruptible(&sdp->o_excl_wait); 356 wake_up_interruptible(&sdp->o_excl_wait);
334 357
335 scsi_autopm_put_device(sdp->device); 358 scsi_autopm_put_device(sdp->device);
@@ -2606,7 +2629,7 @@ static int sg_proc_seq_show_debug(struct seq_file *s, void *v)
2606 scsidp->lun, 2629 scsidp->lun,
2607 scsidp->host->hostt->emulated); 2630 scsidp->host->hostt->emulated);
2608 seq_printf(s, " sg_tablesize=%d excl=%d\n", 2631 seq_printf(s, " sg_tablesize=%d excl=%d\n",
2609 sdp->sg_tablesize, sdp->exclude); 2632 sdp->sg_tablesize, get_exclude(sdp));
2610 sg_proc_debug_helper(s, sdp); 2633 sg_proc_debug_helper(s, sdp);
2611 } 2634 }
2612 read_unlock_irqrestore(&sg_index_lock, iflags); 2635 read_unlock_irqrestore(&sg_index_lock, iflags);