+int set_blockdev_scheduler(const char *path, const char *scheduler)
+{
+ char buf[PATH_MAX], *c;
+ int rc;
+
+ /* Before setting the scheduler, we need to check to see if it's
+ * already set to "noop". If it is, we don't want to override
+ * that setting. If it's set to anything other than "noop", set
+ * the scheduler to what has been passed in. */
+
+ rc = read_file(path, buf, sizeof(buf));
+ if (rc) {
+ if (verbose)
+ fprintf(stderr, "%s: cannot open '%s': %s\n",
+ progname, path, strerror(errno));
+ return rc;
+ }
+
+ /* The expected format of buf: noop anticipatory deadline [cfq] */
+ c = strchr(buf, '[');
+
+ /* If c is NULL, the format is not what we expect. Play it safe
+ * and error out. */
+ if (c == NULL) {
+ if (verbose)
+ fprintf(stderr, "%s: cannot parse scheduler "
+ "options for '%s'\n", progname, path);
+ return -EINVAL;
+ }
+
+ if (strncmp(c+1, "noop", 4) == 0)
+ return 0;
+
+ rc = write_file(path, scheduler);
+ if (rc) {
+ if (verbose)
+ fprintf(stderr, "%s: cannot set scheduler on "
+ "'%s': %s\n", progname, path,
+ strerror(errno));
+ return rc;
+ }
+
+ return rc;
+}
+