Skip to contentSkip to conversation

Hard disk protection revisited

RFC In review Jon Escombe 2 participants 4 messages Oct 6, 2005 – Oct 7, 2005

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 Hard disk protection revisited

I would like to submit the latest disk protection (a.k.a. parking and freezing) code from the hdaps-devel side for comment, along with a brief overview of what's in the patch -

During initialisation, disk drivers with 'protect' helper functions (currently ide and libata) fill in two new function pointers in the queue structure. A sysfs 'protect' queue attribute is then created in the block layer for devices who's lower level drivers have registered these helpers.

When a value (in seconds) is written to the protect attribute, the block layer code calls the driver 'protect' helper function. This helper parks/suspends the disk, and then stops the queue. Control then returns to the block layer which re-uses the plugging timer to set an automatic timeout to restart the queue. When 0 is written to the protect attribute, or the timer expires, the queue is restarted using the 'unprotect' helper function, and the plugging timer is restored.

This interface is intended to be used by a daemon process, that monitors the hdaps driver output for excessive changes in acceleration, and keeps the device parked and the queue stopped until the values become normal.

Patch applies to 2.6.14-rc3 (also 2.6.13.x), and requires libata_passthru..

Regards,
Jon.

______________________________________________________________
Email via Mailtraq4Free from Enstar (www.mailtraqdirect.co.uk)

drivers/block/ll_rw_blk.c +127 -0
diff -urN linux-2.6.14-rc3.passthru/drivers/block/ll_rw_blk.c linux-2.6.14-rc3.hdaps/drivers/block/ll_rw_blk.c
@@ -38,6 +38,8 @@
 static void blk_unplug_work(void *data);
 static void blk_unplug_timeout(unsigned long data);
 static void drive_stat_acct(struct request *rq, int nr_sectors, int new_io);
+static int blk_protect_register(request_queue_t *q);
+static void blk_protect_unregister(request_queue_t *q);
 
 /*
  * For the allocated request tables
@@ -345,6 +347,18 @@
 
 EXPORT_SYMBOL(blk_queue_issue_flush_fn);
 
+void blk_queue_issue_protect_fn(request_queue_t *q, issue_protect_fn *ipf)
+{
+	q->issue_protect_fn = ipf;
+}
+EXPORT_SYMBOL(blk_queue_issue_protect_fn);
+
+void blk_queue_issue_unprotect_fn(request_queue_t *q, issue_unprotect_fn *iuf)
+{
+	q->issue_unprotect_fn = iuf;
+}
+EXPORT_SYMBOL(blk_queue_issue_unprotect_fn);
+
 /*
  * Cache flushing for ordered writes handling
  */
@@ -3693,6 +3707,7 @@
 		return ret;
 	}
 
+	blk_protect_register(q);
 	return 0;
 }
 
@@ -3701,9 +3716,121 @@
 	request_queue_t *q = disk->queue;
 
 	if (q && q->request_fn) {
+		blk_protect_unregister(q);
 		elv_unregister_queue(q);
 
 		kobject_unregister(&q->kobj);
 		kobject_put(&disk->kobj);
 	}
 }
+
+/*
+ * Restore the unplugging timer that we re-used
+ * to implement the queue freeze timeout...
+ */
+static void blk_unfreeze_work(void *data)
+{
+	request_queue_t *q = (request_queue_t *) data;
+
+	INIT_WORK(&q->unplug_work, blk_unplug_work, q);
+	q->unplug_timer.function = blk_unplug_timeout;
+
+	q->issue_unprotect_fn(q);
+}
+
+/*
+ * Called when the queue freeze timeout expires...
+ */
+static void blk_unfreeze_timeout(unsigned long data)
+{
+	request_queue_t *q = (request_queue_t *) data;
+	kblockd_schedule_work(&q->unplug_work);
+}
+
+/* 
+ * The lower level driver parks and freezes the queue, and this block layer
+ *  function sets up the freeze timeout timer on return. If the queue is
+ *  already frozen then this is called to extend the timer...
+ */
+void blk_freeze_queue(request_queue_t *q, int seconds)
+{
+	/* set/reset the timer */
+	mod_timer(&q->unplug_timer, msecs_to_jiffies(seconds*1000) + jiffies);
+
+	/* we do this every iteration - is this sane? */
+	INIT_WORK(&q->unplug_work, blk_unfreeze_work, q);
+	q->unplug_timer.function = blk_unfreeze_timeout;
+}
+
+/* 
+ * When reading the 'protect' attribute, we return boolean frozen or active
+ * todo:
+ * - maybe we should return seconds remaining instead?
+ */
+static ssize_t queue_protect_show(struct request_queue *q, char *page)
+{
+	return queue_var_show(blk_queue_stopped(q), (page));
+}
+
+/* 
+ * When writing the 'protect' attribute, input is the number of seconds
+ * to freeze the queue for. We call a lower level helper function to 
+ * park the heads and freeze/block the queue, then we make a block layer
+ * call to setup the thaw timeout. If input is 0, then we thaw the queue.
+ */
+static ssize_t queue_protect_store(struct request_queue *q, const char *page, size_t count)
+{
+	unsigned long freeze = 0;
+	queue_var_store(&freeze, page, count);
+
+	printk(KERN_DEBUG "queue_protect_store(): freeze = %lu\n", freeze);
+
+	if(freeze>0) {
+		/* Park and freeze */
+		if (!blk_queue_stopped(q))
+			q->issue_protect_fn(q);
+		/* set / reset the thaw timer */
+		blk_freeze_queue(q, freeze);
+	}
+	else
+		blk_unfreeze_timeout((unsigned long) q);
+
+	return count;
+}
+
+static struct queue_sysfs_entry queue_protect_entry = {
+	.attr = {.name = "protect", .mode = S_IRUGO | S_IWUSR },
+	.show = queue_protect_show,
+	.store = queue_protect_store,
+};
+
+static int blk_protect_register(request_queue_t *q)
+{
+	int error = 0;
+
+	/* check that the lower level driver has a protect handler */	
+	if (!q->issue_protect_fn)
+		return 1;
+	
+	/* create the attribute */
+	error = sysfs_create_file(&q->kobj, &queue_protect_entry.attr);
+	if(error){
+		printk(KERN_ERR 
+			"blk_protect_register(): failed to create protect queue attribute!\n");
+		return error;
+	}
+	
+	kobject_get(&q->kobj);
+	return 0;		
+}
+
+static void blk_protect_unregister(request_queue_t *q)
+{
+	/* check that the lower level driver has a protect handler */	
+	if (!q->issue_protect_fn)
+		return;
+
+	/* remove the attribute */
+	sysfs_remove_file(&q->kobj,&queue_protect_entry.attr);
+	kobject_put(&q->kobj);
+}
drivers/ide/ide-disk.c +113 -0

125 lines not on this page · raw message

drivers/ide/ide-io.c +14 -0
diff -urN linux-2.6.14-rc3.passthru/drivers/ide/ide-io.c linux-2.6.14-rc3.hdaps/drivers/ide/ide-io.c
@@ -1181,6 +1181,17 @@
 		}
 
 		/*
+		 * Don't accept a request when the queue is stopped (unless we
+		 * are resuming from suspend). Prevents existing queue entries 
+		 * being processed after queue is stopped by the hard disk 
+		 * protection mechanism...
+		 */
+		if (test_bit(QUEUE_FLAG_STOPPED, &drive->queue->queue_flags) && !blk_pm_resume_request(rq)) {
+			hwgroup->busy = 0;
+			break;
+		}
+
+		/*
 		 * Sanity: don't accept a request that isn't a PM request
 		 * if we are currently power managed. This is very important as
 		 * blk_stop_queue() doesn't prevent the elv_next_request()
@@ -1661,6 +1672,9 @@
 		where = ELEVATOR_INSERT_FRONT;
 		rq->flags |= REQ_PREEMPT;
 	}
+	if (action == ide_next)
+		where = ELEVATOR_INSERT_FRONT;
+
 	__elv_add_request(drive->queue, rq, where, 0);
 	ide_do_request(hwgroup, IDE_NO_IRQ);
 	spin_unlock_irqrestore(&ide_lock, flags);
drivers/scsi/libata-scsi.c +25 -0
diff -urN linux-2.6.14-rc3.passthru/drivers/scsi/libata-scsi.c linux-2.6.14-rc3.hdaps/drivers/scsi/libata-scsi.c
@@ -607,6 +607,29 @@
 	}
 }
 
+extern int scsi_protect_queue(request_queue_t *q, int unload);
+extern int scsi_unprotect_queue(request_queue_t *q);
+
+static int ata_scsi_issue_protect_fn(request_queue_t *q)
+{
+	struct scsi_device *sdev = q->queuedata;
+	struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
+	struct ata_device *dev = &ap->device[sdev->id];
+
+	if (ata_id_has_unload(dev->id))
+		printk(KERN_DEBUG "ata_scsi_issue_protect_fn(): unload support reported..\n");
+	else
+		printk(KERN_DEBUG "ata_scsi_issue_protect_fn(): unload support NOT reported..\n");
+
+	/* call scsi_protect_queue, requesting either unload or standby */
+	return scsi_protect_queue(q, ata_id_has_unload(dev->id) ? 1 : 0);
+}
+
+static int ata_scsi_issue_unprotect_fn(request_queue_t *q)
+{
+	return scsi_unprotect_queue(q);
+}
+
 /**
  *	ata_scsi_slave_config - Set SCSI device attributes
  *	@sdev: SCSI device to examine
@@ -647,6 +670,8 @@
 			blk_queue_max_sectors(sdev->request_queue, 2048);
 		}
 	}
+	blk_queue_issue_protect_fn(sdev->request_queue, ata_scsi_issue_protect_fn);	
+	blk_queue_issue_unprotect_fn(sdev->request_queue, ata_scsi_issue_unprotect_fn);	
 
 	return 0;	/* scsi layer doesn't check return value, sigh */
 }
drivers/scsi/scsi_lib.c +149 -0

167 lines not on this page · raw message

include/linux/ata.h +1 -0
diff -urN linux-2.6.14-rc3.passthru/include/linux/ata.h linux-2.6.14-rc3.hdaps/include/linux/ata.h
@@ -234,6 +234,7 @@
 #define ata_id_is_sata(id)	((id)[93] == 0)
 #define ata_id_rahead_enabled(id) ((id)[85] & (1 << 6))
 #define ata_id_wcache_enabled(id) ((id)[85] & (1 << 5))
+#define ata_id_has_unload(id) ((id)[84] & (1 << 13))
 #define ata_id_has_flush(id) ((id)[83] & (1 << 12))
 #define ata_id_has_flush_ext(id) ((id)[83] & (1 << 13))
 #define ata_id_has_lba48(id)	((id)[83] & (1 << 10))
include/linux/blkdev.h +6 -0
diff -urN linux-2.6.14-rc3.passthru/include/linux/blkdev.h linux-2.6.14-rc3.hdaps/include/linux/blkdev.h
@@ -287,6 +287,8 @@
 typedef int (merge_bvec_fn) (request_queue_t *, struct bio *, struct bio_vec *);
 typedef void (activity_fn) (void *data, int rw);
 typedef int (issue_flush_fn) (request_queue_t *, struct gendisk *, sector_t *);
+typedef int (issue_protect_fn) (request_queue_t *);
+typedef int (issue_unprotect_fn) (request_queue_t *);
 typedef int (prepare_flush_fn) (request_queue_t *, struct request *);
 typedef void (end_flush_fn) (request_queue_t *, struct request *);
 
@@ -331,6 +333,8 @@
 	issue_flush_fn		*issue_flush_fn;
 	prepare_flush_fn	*prepare_flush_fn;
 	end_flush_fn		*end_flush_fn;
+	issue_protect_fn	*issue_protect_fn;
+	issue_unprotect_fn	*issue_unprotect_fn;
 
 	/*
 	 * Auto-unplugging state
@@ -644,6 +648,8 @@
 extern struct request *blk_start_pre_flush(request_queue_t *,struct request *);
 extern int blk_complete_barrier_rq(request_queue_t *, struct request *, int);
 extern int blk_complete_barrier_rq_locked(request_queue_t *, struct request *, int);
+extern void blk_queue_issue_protect_fn(request_queue_t *, issue_protect_fn *);
+extern void blk_queue_issue_unprotect_fn(request_queue_t *, issue_unprotect_fn *);
 
 extern int blk_rq_map_sg(request_queue_t *, struct request *, struct scatterlist *);
 extern void blk_dump_rq_flags(struct request *, char *);
#1 lore raw
Quoted 18 lines · Jon Escombe

I would like to submit the latest disk protection (a.k.a. parking and freezing) code from the hdaps-devel side for comment, along with a brief overview of what's in the patch -

During initialisation, disk drivers with 'protect' helper functions (currently ide and libata) fill in two new function pointers in the queue structure. A sysfs 'protect' queue attribute is then created in the block layer for devices who's lower level drivers have registered these helpers.

When a value (in seconds) is written to the protect attribute, the block layer code calls the driver 'protect' helper function. This helper parks/suspends the disk, and then stops the queue. Control then returns to the block layer which re-uses the plugging timer to set an automatic timeout to restart the queue. When 0 is written to the protect attribute, or the timer expires, the queue is restarted using the 'unprotect' helper function, and the plugging timer is restored.

Quoted 5 lines

This interface is intended to be used by a daemon process, that monitors the hdaps driver output for excessive changes in acceleration, and keeps the device parked and the queue stopped until the values become normal.

Patch applies to 2.6.14-rc3 (also 2.6.13.x), and requires libata_passthru..

I have to nack this one for now, I still want the generic command types patch to go in first. We have far too many queue hooks already, adding two more for a relatively obscure use such as this one is not a good idea.

My suggestion is to maintain this patch out of tree for now, it will be a few kernel release iterations before the command type patch is in.

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 9 lines · Jens Axboe

I have to nack this one for now, I still want the generic command types patch to go in first. We have far too many queue hooks already, adding two more for a relatively obscure use such as this one is not a good idea.

My suggestion is to maintain this patch out of tree for now, it will be a few kernel release iterations before the command type patch is in.

That's a fair comment (and not entirely unexpected), I don't have a problem with looking after this out of tree for now...

One issue with the generic command approach occured to me while making this patch - although it's more likely an issue with my understanding ;)

I'm assuming that it would work like this -- the block layer still has the sysfs attribute, and queues the new command for the lower driver to pick up. The driver receives the command and does it's custom park/freeze work, then calls a common block layer function to setup the timer (all good so far). Where it gets hazy (for me) is how the block layer starts the queue up again - as this ended up needing to be driver specific & I can't see how the block layer would get another command down if the queue is stopped?

Regards,
Jon.

______________________________________________________________
Email via Mailtraq4Free from Enstar (www.mailtraqdirect.co.uk)

#3 lore raw
Quoted 18 lines · Jon Escombe
Quoted 9 lines · Jens Axboe

I have to nack this one for now, I still want the generic command types patch to go in first. We have far too many queue hooks already, adding two more for a relatively obscure use such as this one is not a good idea.

My suggestion is to maintain this patch out of tree for now, it will be a few kernel release iterations before the command type patch is in.

That's a fair comment (and not entirely unexpected), I don't have a problem with looking after this out of tree for now...

One issue with the generic command approach occured to me while making this patch - although it's more likely an issue with my understanding ;)

Quoted 8 lines

I'm assuming that it would work like this -- the block layer still has the sysfs attribute, and queues the new command for the lower driver to pick up. The driver receives the command and does it's custom park/freeze work, then calls a common block layer function to setup the timer (all good so far). Where it gets hazy (for me) is how the block layer starts the queue up again - as this ended up needing to be driver specific & I can't see how the block layer would get another command down if the queue is stopped?

It just sends an unfreeze request. It's similar to how we have to allow suspend commands on an otherwise suspended queue. Since the block layer has control of the frozen queue, it should not be a problem.

Signature · 1 line
Jens Axboe