aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ide/ide-park.c
diff options
context:
space:
mode:
authorElias Oltmanns <eo@nebensachen.de>2008-10-13 15:39:50 -0400
committerBartlomiej Zolnierkiewicz <bzolnier@gmail.com>2008-10-13 15:39:50 -0400
commit4abdc6ee7c47a1a6e12f95717e461baeebee5df7 (patch)
tree51cac3d66891d026483838563e35124467a8eb86 /drivers/ide/ide-park.c
parent08243ba731ee08ff42cf1589379c81567690218f (diff)
ide: Implement disk shock protection support (v4)
On user request (through sysfs), the IDLE IMMEDIATE command with UNLOAD FEATURE as specified in ATA-7 is issued to the device and processing of the request queue is stopped thereafter until the specified timeout expires or user space asks to resume normal operation. This is supposed to prevent the heads of a hard drive from accidentally crashing onto the platter when a heavy shock is anticipated (like a falling laptop expected to hit the floor). Port resets are deferred whenever a device on that port is in the parked state. v3: Elias Oltmanns <eo@nebensachen.de> wrote: [...] > >> 1. Make sure that no negative value is being passed to > >> jiffies_to_msecs() in ide_park_show(). > >> 2. Drop the superfluous variable hwif in ide_special_rq(). > >> 3. Skip initialisation of task and tf in ide_special_rq() if we are not > >> handling a (un)park request. > > > > Well, #3 should have been done differently because we donn't want to > > check for REQ_(UN)?PARK_HEADS more often than is necessary. > > While preparing the backport to 2.6.27, it has just occurred to me that > we need to clear the IDE_DFLAG_PARKED flag in ide_disk_pre_reset() > because this flag must not be set after *any* sort of access to the > device. v4: Fix a memory leak due to a missing blk_put_request() in issue_park_cmd(). Additionally, we should plug the queue when enqueueing the unpark request because there is no guarantee that the park timeout has not expired by then. Even though the chance for that to happen is very slim, the request might end up hanging in the queue until the next I/O operation is queued up. While at it, clean up the code a little: - make issue_park_cmd() a function of type void since nobody cares for the return value anyway; - use blk_start_queueing() instead of __blk_run_queue() since we don't have to worry about recursion; - remove a superfluous pointer deference in task_no_data_intr(). Signed-off-by: Elias Oltmanns <eo@nebensachen.de> Cc: Jeff Garzik <jeff@garzik.org>, Cc: Randy Dunlap <randy.dunlap@oracle.com> Cc: Tejun Heo <htejun@gmail.com> Signed-off-by: Bartlomiej Zolnierkiewicz <bzolnier@gmail.com>
Diffstat (limited to 'drivers/ide/ide-park.c')
-rw-r--r--drivers/ide/ide-park.c121
1 files changed, 121 insertions, 0 deletions
diff --git a/drivers/ide/ide-park.c b/drivers/ide/ide-park.c
new file mode 100644
index 000000000000..03b00e57e93f
--- /dev/null
+++ b/drivers/ide/ide-park.c
@@ -0,0 +1,121 @@
1#include <linux/kernel.h>
2#include <linux/ide.h>
3#include <linux/jiffies.h>
4#include <linux/blkdev.h>
5
6DECLARE_WAIT_QUEUE_HEAD(ide_park_wq);
7
8static void issue_park_cmd(ide_drive_t *drive, unsigned long timeout)
9{
10 struct request_queue *q = drive->queue;
11 struct request *rq;
12 int rc;
13
14 timeout += jiffies;
15 spin_lock_irq(&ide_lock);
16 if (drive->dev_flags & IDE_DFLAG_PARKED) {
17 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
18 int reset_timer;
19
20 reset_timer = time_before(timeout, drive->sleep);
21 drive->sleep = timeout;
22 wake_up_all(&ide_park_wq);
23 if (reset_timer && hwgroup->sleeping &&
24 del_timer(&hwgroup->timer)) {
25 hwgroup->sleeping = 0;
26 hwgroup->busy = 0;
27 blk_start_queueing(q);
28 }
29 spin_unlock_irq(&ide_lock);
30 return;
31 }
32 spin_unlock_irq(&ide_lock);
33
34 rq = blk_get_request(q, READ, __GFP_WAIT);
35 rq->cmd[0] = REQ_PARK_HEADS;
36 rq->cmd_len = 1;
37 rq->cmd_type = REQ_TYPE_SPECIAL;
38 rq->special = &timeout;
39 rc = blk_execute_rq(q, NULL, rq, 1);
40 blk_put_request(rq);
41 if (rc)
42 goto out;
43
44 /*
45 * Make sure that *some* command is sent to the drive after the
46 * timeout has expired, so power management will be reenabled.
47 */
48 rq = blk_get_request(q, READ, GFP_NOWAIT);
49 if (unlikely(!rq))
50 goto out;
51
52 rq->cmd[0] = REQ_UNPARK_HEADS;
53 rq->cmd_len = 1;
54 rq->cmd_type = REQ_TYPE_SPECIAL;
55 elv_add_request(q, rq, ELEVATOR_INSERT_FRONT, 1);
56
57out:
58 return;
59}
60
61ssize_t ide_park_show(struct device *dev, struct device_attribute *attr,
62 char *buf)
63{
64 ide_drive_t *drive = to_ide_device(dev);
65 unsigned long now;
66 unsigned int msecs;
67
68 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
69 return -EOPNOTSUPP;
70
71 spin_lock_irq(&ide_lock);
72 now = jiffies;
73 if (drive->dev_flags & IDE_DFLAG_PARKED &&
74 time_after(drive->sleep, now))
75 msecs = jiffies_to_msecs(drive->sleep - now);
76 else
77 msecs = 0;
78 spin_unlock_irq(&ide_lock);
79
80 return snprintf(buf, 20, "%u\n", msecs);
81}
82
83ssize_t ide_park_store(struct device *dev, struct device_attribute *attr,
84 const char *buf, size_t len)
85{
86#define MAX_PARK_TIMEOUT 30000
87 ide_drive_t *drive = to_ide_device(dev);
88 long int input;
89 int rc;
90
91 rc = strict_strtol(buf, 10, &input);
92 if (rc || input < -2)
93 return -EINVAL;
94 if (input > MAX_PARK_TIMEOUT) {
95 input = MAX_PARK_TIMEOUT;
96 rc = -EOVERFLOW;
97 }
98
99 mutex_lock(&ide_setting_mtx);
100 if (input >= 0) {
101 if (drive->dev_flags & IDE_DFLAG_NO_UNLOAD)
102 rc = -EOPNOTSUPP;
103 else if (input || drive->dev_flags & IDE_DFLAG_PARKED)
104 issue_park_cmd(drive, msecs_to_jiffies(input));
105 } else {
106 if (drive->media == ide_disk)
107 switch (input) {
108 case -1:
109 drive->dev_flags &= ~IDE_DFLAG_NO_UNLOAD;
110 break;
111 case -2:
112 drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
113 break;
114 }
115 else
116 rc = -EOPNOTSUPP;
117 }
118 mutex_unlock(&ide_setting_mtx);
119
120 return rc ? rc : len;
121}