1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Metadata stress testing program for file system
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or
5f08c3bdfSopenharmony_ci * modify it under the terms of the GNU General Public
6f08c3bdfSopenharmony_ci * License as published by the Free Software Foundation; version
7f08c3bdfSopenharmony_ci * 2.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12f08c3bdfSopenharmony_ci * General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * You should find a copy of v2 of the GNU General Public License somewhere
15f08c3bdfSopenharmony_ci * on your Linux system; if not, write to the Free Software Foundation,
16f08c3bdfSopenharmony_ci * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17f08c3bdfSopenharmony_ci *
18f08c3bdfSopenharmony_ci * This program helps you to traverse each node in the k tree
19f08c3bdfSopenharmony_ci * Do the i-node operations on the all file entries in recursive
20f08c3bdfSopenharmony_ci *
21f08c3bdfSopenharmony_ci * Copyright (C) 2009, Intel Corp.
22f08c3bdfSopenharmony_ci * Author: Shaohui Zheng <shaohui.zheng@intel.com>
23f08c3bdfSopenharmony_ci */
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include <stdio.h>
26f08c3bdfSopenharmony_ci#include <stdlib.h>
27f08c3bdfSopenharmony_ci#include <dirent.h>
28f08c3bdfSopenharmony_ci#include <unistd.h>
29f08c3bdfSopenharmony_ci#include <sys/types.h>
30f08c3bdfSopenharmony_ci#include <sys/stat.h>
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci#define MAX_PATH 8192
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci/*
35f08c3bdfSopenharmony_ci * Traverse a k-tree in recusive
36f08c3bdfSopenharmony_ci *
37f08c3bdfSopenharmony_ci * parameters:
38f08c3bdfSopenharmony_ci * lvl: tree level number from button to top
39f08c3bdfSopenharmony_ci * node_nr: the maximun nodes number
40f08c3bdfSopenharmony_ci * return val: if it is leaf, return 0, or return 1
41f08c3bdfSopenharmony_ci */
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ciint k_tree_trav(int lvl, int node_nr)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	int cnt;
46f08c3bdfSopenharmony_ci	char dir[MAX_PATH], cwd[MAX_PATH], f1[MAX_PATH], f2[MAX_PATH],
47f08c3bdfSopenharmony_ci	    ln[MAX_PATH];
48f08c3bdfSopenharmony_ci	if (lvl <= 0)
49f08c3bdfSopenharmony_ci		return 0;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	for (cnt = 0; cnt < node_nr; cnt++) {
52f08c3bdfSopenharmony_ci		sprintf(dir, "%d-d", cnt);
53f08c3bdfSopenharmony_ci		sprintf(f1, "%d-f", cnt);
54f08c3bdfSopenharmony_ci		sprintf(f2, "%d-f-t", cnt);
55f08c3bdfSopenharmony_ci		sprintf(ln, "%d-l", cnt);
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci		// link and unlink testing for each file i-node
58f08c3bdfSopenharmony_ci		link(f1, f2);
59f08c3bdfSopenharmony_ci		unlink(f1);
60f08c3bdfSopenharmony_ci		rename(f2, f1);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci		// symlink testing
63f08c3bdfSopenharmony_ci		symlink(ln, f1);
64f08c3bdfSopenharmony_ci		unlink(ln);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci		getcwd(cwd, sizeof(cwd));
67f08c3bdfSopenharmony_ci		chmod(dir, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
68f08c3bdfSopenharmony_ci		chdir(dir);
69f08c3bdfSopenharmony_ci		k_tree_trav(lvl - 1, node_nr);
70f08c3bdfSopenharmony_ci		chdir(cwd);
71f08c3bdfSopenharmony_ci	}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	return 1;
74f08c3bdfSopenharmony_ci}
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ciint main(int argc, char **argv)
77f08c3bdfSopenharmony_ci{
78f08c3bdfSopenharmony_ci	if (argc < 2) {
79f08c3bdfSopenharmony_ci		printf("Usage: %s tree_depth tree_width\n", argv[0]);
80f08c3bdfSopenharmony_ci		return 1;
81f08c3bdfSopenharmony_ci	}
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci	printf("Traverse k tree (depth: %s, width: %s)...\n", argv[1], argv[2]);
84f08c3bdfSopenharmony_ci	k_tree_trav(atoi(argv[1]), atoi(argv[2]));
85f08c3bdfSopenharmony_ci	printf("Traverse k tree (depth: %s, width: %s), done\n", argv[1],
86f08c3bdfSopenharmony_ci	       argv[2]);
87f08c3bdfSopenharmony_ci	return 0;
88f08c3bdfSopenharmony_ci}
89