Skip to content

Commit 3c522ce

Browse files
Miklos Szereditorvalds
authored andcommitted
block: fix refcounting in BLKBSZSET
Adam Kovari and others reported that disconnecting an USB drive with an ntfs-3g filesystem would cause "kernel BUG at fs/inode.c:1421!" to be triggered. The BUG could be traced back to ioctl(BLKBSZSET), which would erroneously decrement the refcount on the bdev. This is because blkdev_get() expects the refcount to be already incremented and either returns success or decrements the refcount and returns an error. The bug was introduced by e525fd8 (block: make blkdev_get/put() handle exclusive access), which didn't take into account this behavior of blkdev_get(). This fixes https://bugzilla.kernel.org/show_bug.cgi?id=29202 (and likely 29792 too) Reported-by: Adam Kovari <kovariadam@gmail.com> Acked-by: Tejun Heo <tj@kernel.org> Signed-off-by: Miklos Szeredi <mszeredi@suse.cz> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent dcace5a commit 3c522ce

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎block/ioctl.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,11 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
294294
return -EINVAL;
295295
if (get_user(n, (int __user *) arg))
296296
return -EFAULT;
297-
if (!(mode & FMODE_EXCL) &&
298-
blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
299-
return -EBUSY;
297+
if (!(mode & FMODE_EXCL)) {
298+
bdgrab(bdev);
299+
if (blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
300+
return -EBUSY;
301+
}
300302
ret = set_blocksize(bdev, n);
301303
if (!(mode & FMODE_EXCL))
302304
blkdev_put(bdev, mode | FMODE_EXCL);

0 commit comments

Comments
 (0)