1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) Wipro Technologies Ltd, 2005.  All Rights Reserved.
4 *    AUTHOR: Prashant P Yendigeri <prashant.yendigeri@wipro.com>
5 * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6 */
7
8/*\
9 * [Description]
10 *
11 * Verify that statvfs() executes successfully for all
12 * available filesystems. Verify statvfs.f_namemax field
13 * by trying to create files of valid and invalid length names.
14 */
15
16#include <sys/statvfs.h>
17#include "tst_test.h"
18
19#define MNT_POINT "mntpoint"
20#define TEST_PATH MNT_POINT"/testfile"
21#define NLS_MAX_CHARSET_SIZE 6
22
23static void run(void)
24{
25	struct statvfs buf;
26	char valid_fname[PATH_MAX], toolong_fname[PATH_MAX];
27	long fs_type;
28	long valid_len;
29
30	fs_type = tst_fs_type(TEST_PATH);
31
32	TST_EXP_PASS(statvfs(TEST_PATH, &buf));
33
34	valid_len = buf.f_namemax;
35	if (fs_type == TST_VFAT_MAGIC || fs_type == TST_EXFAT_MAGIC)
36		valid_len = buf.f_namemax / NLS_MAX_CHARSET_SIZE;
37
38	memset(valid_fname, 'a', valid_len);
39	memset(toolong_fname, 'b', valid_len + 1);
40
41	valid_fname[valid_len] = 0;
42	toolong_fname[valid_len+1] = 0;
43
44	TST_EXP_FD(creat(valid_fname, 0444));
45	if (TST_PASS)
46		SAFE_CLOSE(TST_RET);
47
48	TST_EXP_FAIL(creat(toolong_fname, 0444), ENAMETOOLONG);
49}
50
51static void setup(void)
52{
53	SAFE_TOUCH(TEST_PATH, 0666, NULL);
54}
55
56static struct tst_test test = {
57	.test_all = run,
58	.setup = setup,
59	.needs_root = 1,
60	.mount_device = 1,
61	.mntpoint = MNT_POINT,
62	.all_filesystems = 1
63};
64