public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space
@ 2014-04-25  2:02 Xiaoguang Wang
  2014-04-25  2:02 ` [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free Xiaoguang Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Xiaoguang Wang @ 2014-04-25  2:02 UTC (permalink / raw)
  To: ltp-list

The existing lib/tst_cwd_has_free.c only determines the filesystem, which
the current directory is in. And I think tst_cwd_has_free() is not entirely
correct. See this code in tst_cwd_has_free():
	return ((float)sf.f_bfree) / (1024 / sf.f_bsize) >=
		 required_kib ? 1 : 0;
if sf.f_bsize is greater than 1024, this code is wrong.

I choose to remove tst_cwd_has_free.c and add lib/tst_fs_has_free.c.
The prototype is below:
	int tst_fs_has_free(void (*cleanup)(void), const char *path,
		unsigned int size, unsigned int mult);

User can specify the path to determine the corresponding filesystem.

Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
---
 include/test.h                                | 17 +++++++++--
 lib/tst_cwd_has_free.c                        | 22 --------------
 lib/tst_fs_has_free.c                         | 42 +++++++++++++++++++++++++++
 testcases/kernel/syscalls/swapoff/swapoff01.c |  2 +-
 testcases/kernel/syscalls/swapoff/swapoff02.c |  2 +-
 testcases/kernel/syscalls/swapon/libswapon.c  |  3 +-
 6 files changed, 61 insertions(+), 27 deletions(-)
 delete mode 100644 lib/tst_cwd_has_free.c
 create mode 100644 lib/tst_fs_has_free.c

diff --git a/include/test.h b/include/test.h
index f5c602b..558575c 100644
--- a/include/test.h
+++ b/include/test.h
@@ -39,6 +39,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 #include "compiler.h"
 
@@ -204,8 +205,20 @@ struct tst_kern_exv {
 
 int tst_kvercmp2(int r1, int r2, int r3, struct tst_kern_exv *vers);
 
-/* lib/tst_cwd_has_free.c */
-int tst_cwd_has_free(int required_kib);
+enum {
+	TST_KB = 1024,
+	TST_MB = 1048576,
+	TST_GB = 1073741824,
+};
+
+/* lib/tst_fs_has_free.c
+ *
+ * @path: path is the pathname of any file within the mounted file system
+ * @mult: mult should be TST_KB, TST_MB or TST_GB
+ * the required free space is calculated by @size * @mult
+ */
+int tst_fs_has_free(void (*cleanup)(void), const char *path,
+		    unsigned int size, unsigned int mult);
 
 int tst_is_virt(int virt_type);
 
diff --git a/lib/tst_cwd_has_free.c b/lib/tst_cwd_has_free.c
deleted file mode 100644
index e44c31f..0000000
--- a/lib/tst_cwd_has_free.c
+++ /dev/null
@@ -1,22 +0,0 @@
-/*
- *    AUTHOR
- *    	Ricky Ng-Adam <rngadam@yahoo.com>, 2005-01-01
- *
- *    DESCRIPTION
- * 	Check if there is enough blocks to fill number of KiB specified
- * 	If current directory has enough blocks, return 1
- * 	If current directory has NOT enough blocks, return 0
- *
- *
- */
-#include <sys/vfs.h>
-
-int tst_cwd_has_free(int required_kib)
-{
-	struct statfs sf;
-	statfs(".", &sf);
-
-	/* check that we have enough blocks to create swap file */
-	return ((float)sf.f_bfree) / (1024 / sf.f_bsize) >=
-	    required_kib ? 1 : 0;
-}
diff --git a/lib/tst_fs_has_free.c b/lib/tst_fs_has_free.c
new file mode 100644
index 0000000..9a84e45
--- /dev/null
+++ b/lib/tst_fs_has_free.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2014 Fujitsu Ltd.
+ * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it would be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/*
+ * DESCRIPTION
+ * 	Check if the mounted file system has enough free space,
+ * 	if it is, tst_fs_has_free() returns 1, otherwise 0.
+ */
+
+#include <stdint.h>
+#include <sys/vfs.h>
+#include "test.h"
+
+int tst_fs_has_free(void (*cleanup)(void), const char *path,
+		    unsigned int size, unsigned int mult)
+{
+	struct statfs sf;
+
+	if (statfs(path, &sf)) {
+		tst_brkm(TBROK | TERRNO, cleanup,
+			 "tst_fs_has_free: failed to statfs(%s)", path);
+	}
+
+	if ((uint64_t)sf.f_bfree * sf.f_bsize > (uint64_t)size * mult)
+		return 1;
+	else
+		return 0;
+}
diff --git a/testcases/kernel/syscalls/swapoff/swapoff01.c b/testcases/kernel/syscalls/swapoff/swapoff01.c
index b4d19de..96b52ae 100644
--- a/testcases/kernel/syscalls/swapoff/swapoff01.c
+++ b/testcases/kernel/syscalls/swapoff/swapoff01.c
@@ -99,7 +99,7 @@ static void setup(void)
 	break;
 	}
 
-	if (!tst_cwd_has_free(65536)) {
+	if (!tst_fs_has_free(NULL, ".", 64, TST_KB)) {
 		tst_brkm(TBROK, cleanup,
 			 "Insufficient disk space to create swap file");
 	}
diff --git a/testcases/kernel/syscalls/swapoff/swapoff02.c b/testcases/kernel/syscalls/swapoff/swapoff02.c
index 978dc01..f5929cc 100644
--- a/testcases/kernel/syscalls/swapoff/swapoff02.c
+++ b/testcases/kernel/syscalls/swapoff/swapoff02.c
@@ -157,7 +157,7 @@ static void setup(void)
 	break;
 	}
 
-	if (!tst_cwd_has_free(1)) {
+	if (!tst_fs_has_free(NULL, ".", 1, TST_KB)) {
 		tst_brkm(TBROK, cleanup,
 			 "Insufficient disk space to create swap file");
 	}
diff --git a/testcases/kernel/syscalls/swapon/libswapon.c b/testcases/kernel/syscalls/swapon/libswapon.c
index 8eca7dc..4c5f045 100644
--- a/testcases/kernel/syscalls/swapon/libswapon.c
+++ b/testcases/kernel/syscalls/swapon/libswapon.c
@@ -27,7 +27,8 @@
  */
 void make_swapfile(void (cleanup)(void), const char *swapfile)
 {
-	if (!tst_cwd_has_free(sysconf(_SC_PAGESIZE)*10)) {
+	if (!tst_fs_has_free(NULL, ".", sysconf(_SC_PAGESIZE) * 10 / TST_KB,
+	    TST_KB)) {
 		tst_brkm(TBROK, cleanup,
 			"Insufficient disk space to create swap file");
 	}
-- 
1.8.2.1


------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free
  2014-04-25  2:02 [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space Xiaoguang Wang
@ 2014-04-25  2:02 ` Xiaoguang Wang
  2014-05-07 17:03   ` chrubis
  2014-05-07 13:56 ` [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space chrubis
  2014-05-07 17:07 ` chrubis
  2 siblings, 1 reply; 5+ messages in thread
From: Xiaoguang Wang @ 2014-04-25  2:02 UTC (permalink / raw)
  To: ltp-list

Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
---
 tools/apicmds/.gitignore  |  1 +
 tools/apicmds/Makefile    |  2 +-
 tools/apicmds/ltpapicmd.c | 31 +++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/tools/apicmds/.gitignore b/tools/apicmds/.gitignore
index 0c1a896..0ac3e43 100644
--- a/tools/apicmds/.gitignore
+++ b/tools/apicmds/.gitignore
@@ -9,3 +9,4 @@ tst_ncpus
 tst_ncpus_max
 tst_res
 tst_resm
+tst_fs_has_free
diff --git a/tools/apicmds/Makefile b/tools/apicmds/Makefile
index 9ecf570..aa3eefe 100644
--- a/tools/apicmds/Makefile
+++ b/tools/apicmds/Makefile
@@ -27,7 +27,7 @@ include $(top_srcdir)/include/mk/testcases.mk
 CPPFLAGS		+= -D_GNU_SOURCE
 
 MAKE_TARGETS		:= $(addprefix tst_,brk brkm exit flush kvercmp kvercmp2 \
-				res resm ncpus ncpus_max get_unused_port)
+				res resm ncpus ncpus_max get_unused_port fs_has_free)
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
 
diff --git a/tools/apicmds/ltpapicmd.c b/tools/apicmds/ltpapicmd.c
index c8099c5..5f9c582 100644
--- a/tools/apicmds/ltpapicmd.c
+++ b/tools/apicmds/ltpapicmd.c
@@ -73,6 +73,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include "test.h"
 #include "usctest.h"
 #include "safe_macros.h"
@@ -324,6 +325,33 @@ err:
 	exit(1);
 }
 
+int apicmd_fs_has_free(int argc, char *argv[])
+{
+	int ret = 2;
+
+	if (argc != 3) {
+		fprintf(stderr, "Usage: tst_fs_has_free path required_kib\n"
+			"path: the pathname of any file within the mounted "
+			"filesystem\n required_kib: the required free space"
+			"(count in KB)\n");
+		goto fs_has_free_err;
+	}
+
+	char *endptr;
+	uint64_t required_kib = strtoull(argv[1], &endptr, 0);
+
+	if (*argv[1] == '\0' || *endptr != '\0') {
+		fprintf(stderr, "%s is not a valid numeric string\n", argv[1]);
+		goto fs_has_free_err;
+	}
+
+	ret = tst_fs_has_free(NULL, argv[0], required_kib, TST_KB);
+	ret = 1 ^ ret;
+
+fs_has_free_err:
+	exit(ret);
+}
+
 /*
  * Function:    main - entry point of this program
  *
@@ -359,6 +387,7 @@ int main(int argc, char *argv[])
 	if (TCID == NULL || tst_total == NULL || tst_cntstr == NULL) {
 		if (!strcmp(cmd_name, "tst_kvercmp") &&
 		    !strcmp(cmd_name, "tst_kvercmp2") &&
+		    !strcmp(cmd_name, "tst_fs_has_free") &&
 		    !strcmp(cmd_name, "tst_get_unused_port")) {
 			fprintf(stderr,
 				"\nSet variables TCID, TST_TOTAL, and TST_COUNT before each test:\n"
@@ -409,6 +438,8 @@ int main(int argc, char *argv[])
 		printf("%li\n", tst_ncpus_max());
 	} else if (strcmp(cmd_name, "tst_get_unused_port") == 0) {
 		printf("%u\n", apicmd_get_unused_port(argc, argv));
+	} else if (strcmp(cmd_name, "tst_fs_has_free") == 0) {
+		apicmd_fs_has_free(argc, argv);
 	}
 
 	exit(0);
-- 
1.8.2.1


------------------------------------------------------------------------------
Start Your Social Network Today - Download eXo Platform
Build your Enterprise Intranet with eXo Platform Software
Java Based Open Source Intranet - Social, Extensible, Cloud Ready
Get Started Now And Turn Your Intranet Into A Collaboration Platform
http://p.sf.net/sfu/ExoPlatform
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space
  2014-04-25  2:02 [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space Xiaoguang Wang
  2014-04-25  2:02 ` [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free Xiaoguang Wang
@ 2014-05-07 13:56 ` chrubis
  2014-05-07 17:07 ` chrubis
  2 siblings, 0 replies; 5+ messages in thread
From: chrubis @ 2014-05-07 13:56 UTC (permalink / raw)
  To: Xiaoguang Wang; +Cc: ltp-list

Hi!
> The existing lib/tst_cwd_has_free.c only determines the filesystem, which
> the current directory is in. And I think tst_cwd_has_free() is not entirely
> correct. See this code in tst_cwd_has_free():
> 	return ((float)sf.f_bfree) / (1024 / sf.f_bsize) >=
> 		 required_kib ? 1 : 0;
> if sf.f_bsize is greater than 1024, this code is wrong.
> 
> I choose to remove tst_cwd_has_free.c and add lib/tst_fs_has_free.c.
> The prototype is below:
> 	int tst_fs_has_free(void (*cleanup)(void), const char *path,
> 		unsigned int size, unsigned int mult);
> 
> User can specify the path to determine the corresponding filesystem.

Pushed with a small fixes, thanks.

> +enum {
> +	TST_KB = 1024,
> +	TST_MB = 1048576,
> +	TST_GB = 1073741824,
> +};

I've added TST_BYTES = 1 here so that we don't have to divide the 10 *
pagesize in the last case.

> diff --git a/testcases/kernel/syscalls/swapoff/swapoff01.c b/testcases/kernel/syscalls/swapoff/swapoff01.c
> index b4d19de..96b52ae 100644
> --- a/testcases/kernel/syscalls/swapoff/swapoff01.c
> +++ b/testcases/kernel/syscalls/swapoff/swapoff01.c
> @@ -99,7 +99,7 @@ static void setup(void)
>  	break;
>  	}
>  
> -	if (!tst_cwd_has_free(65536)) {
> +	if (!tst_fs_has_free(NULL, ".", 64, TST_KB)) {
>  		tst_brkm(TBROK, cleanup,
>  			 "Insufficient disk space to create swap file");
>  	}


And changes this to:

	if (!tst_fs_has_free(NULL, ".", 64, TST_MB)) {

Because as far as I can tell, the test creates 64 megabytes files.


PS: We should document the function in Test Writing Guidelines. Will you
    send a patch or should I write it?

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free
  2014-04-25  2:02 ` [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free Xiaoguang Wang
@ 2014-05-07 17:03   ` chrubis
  0 siblings, 0 replies; 5+ messages in thread
From: chrubis @ 2014-05-07 17:03 UTC (permalink / raw)
  To: Xiaoguang Wang; +Cc: ltp-list

Hi!
> Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
> ---
>  tools/apicmds/.gitignore  |  1 +
>  tools/apicmds/Makefile    |  2 +-
>  tools/apicmds/ltpapicmd.c | 31 +++++++++++++++++++++++++++++++
>  3 files changed, 33 insertions(+), 1 deletion(-)

I've modified the code a bit to support kB, MB and GB suffixes and
pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space
  2014-04-25  2:02 [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space Xiaoguang Wang
  2014-04-25  2:02 ` [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free Xiaoguang Wang
  2014-05-07 13:56 ` [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space chrubis
@ 2014-05-07 17:07 ` chrubis
  2 siblings, 0 replies; 5+ messages in thread
From: chrubis @ 2014-05-07 17:07 UTC (permalink / raw)
  To: Xiaoguang Wang; +Cc: ltp-list

Hi!
> +	if ((uint64_t)sf.f_bfree * sf.f_bsize > (uint64_t)size * mult)

While I've tried this code I've found that we should rather be using
sf.f_bavail than sf.f_bfree.

Because f_bfree describes total free blocks while f_bavail describes
block available to use for unprivileged user. So if some test asks for
30GB and the code that creates the file runs as unprivileged user it may
still fail even after this check was successful.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
Is your legacy SCM system holding you back? Join Perforce May 7 to find out:
&#149; 3 signs your SCM is hindering your productivity
&#149; Requirements for releasing software faster
&#149; Expert tips and advice for migrating your SCM now
http://p.sf.net/sfu/perforce
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-05-07 17:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-25  2:02 [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space Xiaoguang Wang
2014-04-25  2:02 ` [LTP] [PATCH v3 2/2] tools/apicmd: add tst_fs_has_free Xiaoguang Wang
2014-05-07 17:03   ` chrubis
2014-05-07 13:56 ` [LTP] [PATCH v3 1/2] lib/tst_fs_has_free.c: add tst_fs_has_free() to determine filesystem's free space chrubis
2014-05-07 17:07 ` chrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox