From: Milan Broz <[email-masked]>

Clean up reference counting.

Signed-off-by: Milan Broz <[email-masked]>
---
 drivers/md/dm-crypt.c |   24 ++++++++++++------------
 1 files changed, 12 insertions(+), 12 deletions(-)

Index: linux-2.6.24-rc1/drivers/md/dm-crypt.c
===================================================================
--- linux-2.6.24-rc1.orig/drivers/md/dm-crypt.c	2007-11-06 15:00:02.000000000 +0000
+++ linux-2.6.24-rc1/drivers/md/dm-crypt.c	2007-11-06 18:01:06.000000000 +0000
@@ -598,6 +598,8 @@ static void crypt_write_io_done(struct d
 
 	clone->bi_sector = cc->start + io->sector;
 	io->sector += bio_sectors(clone);
+
+	generic_make_request(clone);
 }
 
 static void crypt_write_io_loop(struct dm_crypt_io *io)
@@ -630,19 +632,11 @@ static void crypt_write_io_loop(struct d
 		if (unlikely(r < 0))
 			return;
 
-		/* Grab another reference to the io struct
-		 * before we kick off the request */
-		if (remaining)
-			atomic_inc(&io->pending);
-
-		generic_make_request(clone);
-
-		/* Do not reference clone after this - it
-		 * may be gone already. */
-
 		/* out of memory -> run queues */
-		if (unlikely(remaining))
+		if (unlikely(remaining)) {
+			atomic_inc(&io->pending);
 			congestion_wait(WRITE, HZ/100);
+		}
 	}
 }
 
@@ -650,10 +644,16 @@ static void crypt_write_io_process(struc
 {
 	struct crypt_config *cc = io->target->private;
 
-	atomic_inc(&io->pending);
+	/*
+	 * One ref for the use within this function and one for the
+	 * clone to be submitted in crypt_write_io_done().
+	 */
+	atomic_add(2, &io->pending);
 
 	crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, io->sector);
 	crypt_write_io_loop(io);
+
+	crypt_dec_pending(io);
 }
 
 static void crypt_read_io_done(struct dm_crypt_io *io, int error)