Autodetect cgroup blkio mount point

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/HOWTO b/HOWTO
index 7a7d14e..0b40f75 100644
--- a/HOWTO
+++ b/HOWTO
@@ -1004,18 +1004,13 @@
 		given in the stats is the first error that was hit during the
 		run.
 
-cgroup_root=str	Root of the mounted blkio cgroup file systems. This is a Linux
-		specific IO controller. If your system doesn't have it mounted,
-		you can do so with:
+cgroup=str	Add job to this control group. If it doesn't exist, it will
+		be created. The system must have a mounted cgroup blkio
+		mount point for this to work. If your system doesn't have it
+		mounted, you can do so with:
 
 		# mount -t cgroup -o blkio none /cgroup
 
-		The cgroup_root defaults to /cgroup, if mounted elsewhere
-		please specify this option.
-
-cgroup=str	Add job to this control group. If it doesn't exist, it will
-		be created.
-
 cgroup_weight=int	Set the weight of the cgroup to this value. See
 		the documentation that comes with the kernel, allowed values
 		are in the range of 100..1000.
diff --git a/cgroup.c b/cgroup.c
index 29d89dd..14bbc57 100644
--- a/cgroup.c
+++ b/cgroup.c
@@ -3,6 +3,7 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <mntent.h>
 #include "fio.h"
 #include "flist.h"
 #include "cgroup.h"
@@ -15,6 +16,33 @@
 	char *root;
 };
 
+static char *find_cgroup_mnt(struct thread_data *td)
+{
+	char *mntpoint = NULL;
+	struct mntent *mnt;
+	FILE *f;
+
+	f = setmntent("/proc/mounts", "r");
+	if (!f) {
+		td_verror(td, errno, "setmntent /proc/mounts");
+		return NULL;
+	}
+
+	while ((mnt = getmntent(f)) != NULL) {
+		if (!strcmp(mnt->mnt_type, "cgroup") &&
+		    strstr(mnt->mnt_opts, "blkio"))
+			break;
+	}
+
+	if (mnt)
+		mntpoint = smalloc_strdup(mnt->mnt_dir);
+	else
+		log_err("fio: cgroup blkio does not appear to be mounted\n");
+
+	endmntent(f);
+	return mntpoint;
+}
+
 static void add_cgroup(const char *name, struct flist_head *clist)
 {
 	struct cgroup_member *cm;
@@ -46,26 +74,14 @@
 	fio_mutex_up(lock);
 }
 
-/*
- * Check if the given root appears valid
- */
-static int cgroup_check_fs(struct thread_data *td)
-{
-	struct stat sb;
-	char tmp[256];
-
-	sprintf(tmp, "%s/tasks", td->o.cgroup_root);
-	return stat(tmp, &sb);
-}
-
-static char *get_cgroup_root(struct thread_data *td)
+static char *get_cgroup_root(struct thread_data *td, char *mnt)
 {
 	char *str = malloc(64);
 
 	if (td->o.cgroup)
-		sprintf(str, "%s/%s", td->o.cgroup_root, td->o.cgroup);
+		sprintf(str, "%s/%s", mnt, td->o.cgroup);
 	else
-		sprintf(str, "%s/%s", td->o.cgroup_root, td->o.name);
+		sprintf(str, "%s/%s", mnt, td->o.name);
 
 	return str;
 }
@@ -100,30 +116,31 @@
 /*
  * Move pid to root class
  */
-static int cgroup_del_pid(struct thread_data *td)
+static int cgroup_del_pid(struct thread_data *td, char *mnt)
 {
-	return cgroup_write_pid(td, td->o.cgroup_root);
+	return cgroup_write_pid(td, mnt);
 }
 
-int cgroup_setup(struct thread_data *td, struct flist_head *clist)
+int cgroup_setup(struct thread_data *td, struct flist_head *clist, char **mnt)
 {
 	char *root;
 
-	if (cgroup_check_fs(td)) {
-		log_err("fio: blkio cgroup mount point %s not valid\n",
-							td->o.cgroup_root);
-		return 1;
+	if (!*mnt) {
+		*mnt = find_cgroup_mnt(td);
+		if (!*mnt)
+			return 1;
 	}
 
 	/*
 	 * Create container, if it doesn't exist
 	 */
-	root = get_cgroup_root(td);
+	root = get_cgroup_root(td, *mnt);
 	if (mkdir(root, 0755) < 0) {
 		int __e = errno;
 
 		if (__e != EEXIST) {
 			td_verror(td, __e, "cgroup mkdir");
+			log_err("fio: path %s\n", root);
 			goto err;
 		}
 	} else
@@ -146,14 +163,14 @@
 	return 1;
 }
 
-void cgroup_shutdown(struct thread_data *td)
+void cgroup_shutdown(struct thread_data *td, char **mnt)
 {
-	if (cgroup_check_fs(td))
+	if (*mnt == NULL)
 		return;
 	if (!td->o.cgroup_weight && !td->o.cgroup)
 		return;
 
-	cgroup_del_pid(td);
+	cgroup_del_pid(td, *mnt);
 }
 
 static void fio_init cgroup_init(void)
diff --git a/cgroup.h b/cgroup.h
index b246ef8..f7ad210 100644
--- a/cgroup.h
+++ b/cgroup.h
@@ -3,20 +3,21 @@
 
 #ifdef FIO_HAVE_CGROUPS
 
-int cgroup_setup(struct thread_data *td, struct flist_head *list);
-void cgroup_shutdown(struct thread_data *td);
+int cgroup_setup(struct thread_data *, struct flist_head *, char **);
+void cgroup_shutdown(struct thread_data *, char **);
 
 void cgroup_kill(struct flist_head *list);
 
 #else
 
-static inline int cgroup_setup(struct thread_data *td, struct flist_head *list);
+static inline int cgroup_setup(struct thread_data *td, struct flist_head *list,
+			       char **mnt)
 {
 	td_verror(td, EINVAL, "cgroup_setup");
 	return 1;
 }
 
-static inline void cgroup_shutdown(struct thread_data *td)
+static inline void cgroup_shutdown(struct thread_data *td, char **mnt)
 {
 }
 
diff --git a/fio.1 b/fio.1
index 648b4e9..2e0a283 100644
--- a/fio.1
+++ b/fio.1
@@ -725,18 +725,12 @@
 these time calls will be excluded from other uses. Fio will manually clear it
 from the CPU mask of other jobs.
 .TP
-.BI cgroup_root \fR=\fPstr
-Root of the mounted blkio cgroup file systems. This is a Linux
-specific IO controller. If your system doesn't have it mounted,
-you can do so with:
-
-# mount -t cgroup -o blkio none /cgroup
-
-The cgroup_root defaults to /cgroup, if mounted elsewhere please specify this
-option.
-.TP
 .BI cgroup \fR=\fPstr
 Add job to this control group. If it doesn't exist, it will be created.
+The system must have a mounted cgroup blkio mount point for this to work. If
+your system doesn't have it mounted, you can do so with:
+
+# mount -t cgroup -o blkio none /cgroup
 .TP
 .BI cgroup_weight \fR=\fPint
 Set the weight of the cgroup to this value. See the documentation that comes
diff --git a/fio.c b/fio.c
index bcc130b..7d4c96a 100644
--- a/fio.c
+++ b/fio.c
@@ -62,6 +62,7 @@
 static struct itimerval itimer;
 static pthread_t gtod_thread;
 static struct flist_head *cgroup_list;
+static char *cgroup_mnt;
 
 struct io_log *agg_io_log[2];
 
@@ -1077,7 +1078,7 @@
 		}
 	}
 
-	if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list))
+	if (td->o.cgroup_weight && cgroup_setup(td, cgroup_list, &cgroup_mnt))
 		goto err;
 
 	if (nice(td->o.nice) == -1) {
@@ -1209,7 +1210,7 @@
 	close_and_free_files(td);
 	close_ioengine(td);
 	cleanup_io_u(td);
-	cgroup_shutdown(td);
+	cgroup_shutdown(td, &cgroup_mnt);
 
 	if (td->o.cpumask_set) {
 		int ret = fio_cpuset_exit(&td->o.cpumask);
@@ -1674,6 +1675,8 @@
 
 	cgroup_kill(cgroup_list);
 	sfree(cgroup_list);
+	if (cgroup_mnt)
+		sfree(cgroup_mnt);
 
 	fio_mutex_remove(startup_mutex);
 	fio_mutex_remove(writeout_mutex);
diff --git a/fio.h b/fio.h
index 17aeaad..b1af7b1 100644
--- a/fio.h
+++ b/fio.h
@@ -275,7 +275,6 @@
 	/*
 	 * blkio cgroup support
 	 */
-	char *cgroup_root;
 	char *cgroup;
 	unsigned int cgroup_weight;
 };
diff --git a/options.c b/options.c
index 77cfd42..16908a0 100644
--- a/options.c
+++ b/options.c
@@ -1727,13 +1727,6 @@
 		.help	= "Select a specific builtin performance test",
 	},
 	{
-		.name	= "cgroup_root",
-		.type	= FIO_OPT_STR_STORE,
-		.off1	= td_var_offset(cgroup_root),
-		.help	= "Root of mounted blkio cgroup",
-		.def	= "/cgroup",
-	},
-	{
 		.name	= "cgroup",
 		.type	= FIO_OPT_STR_STORE,
 		.off1	= td_var_offset(cgroup),