Subject: [PATCH] (4/6) further block_device cleanups
From: Alexander Viro <[email-masked]>
To: Linus Torvalds <[email-masked]>
Cc: Andrea Arcangeli <[email-masked]>
Date: Sat, 22 Sep 2001 02:10:21 -0400 (EDT)

Part 4: open()/close()

a) blkdev_open() does bd_acqiure().  It does bdput() in case of failure;
b) blkdev_close() and initrd_release() do bdput().
c) for the sake of consistency, we use ->f_op->release() instead of
blkdev_close() for all places where we clsoe infile (loading ramdisk)

diff -urN S10-pre13-swapfile/drivers/block/rd.c S10-pre13-current/drivers/block/rd.c
--- S10-pre13-swapfile/drivers/block/rd.c	Fri Sep 21 16:31:16 2001
+++ S10-pre13-current/drivers/block/rd.c	Fri Sep 21 22:27:21 2001
@@ -422,6 +422,7 @@
 		initrd_start = 0;
 		inode->i_bdev->bd_cache_openers--;
 		blkdev_put(inode->i_bdev, BDEV_FILE);
+		bdput(inode->i_bdev);
 	}
 	return 0;
 }
@@ -780,7 +781,7 @@
 		if (i && (i % devblocks == 0)) {
 			printk("done disk #%d.\n", i/devblocks);
 			rotate = 0;
-			if (blkdev_close(inode, &infile) != 0) {
+			if (infile.f_op->release(inode, &infile) != 0) {
 				printk("Error closing the disk.\n");
 				goto noclose_input;
 			}
diff -urN S10-pre13-swapfile/fs/block_dev.c S10-pre13-current/fs/block_dev.c
--- S10-pre13-swapfile/fs/block_dev.c	Fri Sep 21 18:59:05 2001
+++ S10-pre13-current/fs/block_dev.c	Fri Sep 21 22:30:25 2001
@@ -753,8 +753,8 @@
 
 int blkdev_open(struct inode * inode, struct file * filp)
 {
-	int ret = -ENXIO;
-	struct block_device *bdev = inode->i_bdev;
+	int ret;
+	struct block_device *bdev;
 
 	/*
 	 * Preserve backwards compatibility and allow large file access
@@ -764,13 +764,15 @@
 	 */
 	filp->f_flags |= O_LARGEFILE;
 
+	bd_acquire(inode);
+	bdev = inode->i_bdev;
 	down(&bdev->bd_sem);
 
-	if (get_inode(bdev)) {
-		up(&bdev->bd_sem);
-		return -ENOMEM;
-	}
+	ret = get_inode(bdev);
+	if (ret)
+		goto out;
 
+	ret = -ENXIO;
 	lock_kernel();
 	if (!bdev->bd_op)
 		bdev->bd_op = get_blkfops(MAJOR(inode->i_rdev));
@@ -791,7 +793,10 @@
 		}
 	}	
 	unlock_kernel();
+out:
 	up(&bdev->bd_sem);
+	if (ret)
+		bdput(bdev);
 	return ret;
 }	
 
@@ -883,6 +888,7 @@
 	}
 	unlock_kernel();
 	up(&bdev->bd_sem);
+	bdput(bdev);
 
 	return ret;
 }