Whamcloud - gitweb
LU-9010 tests: Change static defines to use macro for kinode.c
[fs/lustre-release.git] / lustre / tests / kernel / kinode.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22
23 /*
24  * Copyright 2017 Cray Inc. All rights reserved.
25  * Author: Frank Zago.
26  */
27
28 /* Check that the inode number is the same whether the call to
29  * vfs_getattr is coming from a system call or from a kthread. When
30  * CONFIG_X86_X32 was set, the result used to be different for
31  * Lustre. In addition, a user can also check that the same inode
32  * number is also seen from the kernel and userspace.  */
33
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/completion.h>
37 #include <linux/kthread.h>
38 #include <linux/fs.h>
39 #include <linux/version.h>
40
41 /* Random ID passed by userspace, and printed in messages, used to
42  * separate different runs of that module. */
43 static int run_id;
44 module_param(run_id, int, 0644);
45 MODULE_PARM_DESC(run_id, "run ID");
46
47 /* Name of the file to stat. */
48 static char fname[4096];
49 module_param_string(fname, fname, sizeof(fname), 0644);
50 MODULE_PARM_DESC(fname, "name of file to stat");
51
52 static DECLARE_COMPLETION(thr_start);
53
54 #define PREFIX "lustre_kinode_%u:"
55
56 static int stat_file(struct kstat *stbuf)
57 {
58         struct file *fd;
59         int rc;
60
61         fd = filp_open(fname, O_RDONLY, 0);
62         if (IS_ERR(fd)) {
63                 pr_err(PREFIX " can't open file %s\n", run_id, fname);
64                 return -EIO;
65         }
66
67 #ifdef HAVE_INODEOPS_ENHANCED_GETATTR
68         rc = vfs_getattr(&fd->f_path, stbuf, STATX_INO, AT_STATX_SYNC_AS_STAT);
69 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
70         rc = vfs_getattr(&fd->f_path, stbuf);
71 #else
72         rc = vfs_getattr(fd->f_path.mnt, fd->f_path.dentry, stbuf);
73 #endif
74         if (rc != 0) {
75                 pr_err(PREFIX " vfs_getattr failed: %d\n", run_id, rc);
76                 goto out;
77         }
78
79         pr_err(PREFIX " inode is %llu\n", run_id, stbuf->ino);
80         rc = 0;
81
82 out:
83         filp_close(fd, NULL);
84
85         return rc;
86 }
87
88 static int stat_thread(void *data)
89 {
90         struct kstat *stbuf = data;
91         int rc;
92
93         /* Signal caller that thread has started. */
94         complete(&thr_start);
95
96         rc = stat_file(stbuf);
97
98         /* Wait for call to kthread_stop. */
99         set_current_state(TASK_INTERRUPTIBLE);
100         while (!kthread_should_stop()) {
101                 schedule();
102                 set_current_state(TASK_INTERRUPTIBLE);
103         }
104         set_current_state(TASK_RUNNING);
105
106         return rc;
107 }
108
109 static int __init kinode_init(void)
110 {
111         struct task_struct *thr;
112         struct kstat stbuf1;
113         struct kstat stbuf2;
114         int rc;
115
116 #ifdef CONFIG_X86_X32
117         pr_err(PREFIX " CONFIG_X86_X32 is set\n", run_id);
118 #else
119         pr_err(PREFIX " CONFIG_X86_X32 is not set\n", run_id);
120 #endif
121
122         if (strlen(fname) < 1) {
123                 pr_err(PREFIX " invalid file name '%s'\n", run_id, fname);
124                 goto out;
125         }
126
127         rc = stat_file(&stbuf1);
128         if (rc) {
129                 pr_err(PREFIX " direct stat failed: %d\n", run_id, rc);
130                 goto out;
131         }
132
133         /* Run the same from a kthread. */
134         thr = kthread_run(stat_thread, &stbuf2, "kinode_%u", run_id);
135         if (IS_ERR(thr)) {
136                 pr_err(PREFIX " Cannot create kthread\n", run_id);
137                 goto out;
138         }
139
140         /* Wait for the thread to start, then wait for it to
141          * terminate. */
142         wait_for_completion(&thr_start);
143         rc = kthread_stop(thr);
144         if (rc) {
145                 pr_err(PREFIX " indirect stat failed: %d\n", run_id, rc);
146                 goto out;
147         }
148
149         if (stbuf1.ino != stbuf2.ino)
150                 pr_err(PREFIX " inode numbers are different: %llu %llu\n",
151                        run_id, stbuf1.ino, stbuf2.ino);
152         else
153                 pr_err(PREFIX " inode numbers are identical: %llu\n",
154                        run_id, stbuf1.ino);
155
156 out:
157         /* Don't load. */
158         return -EINVAL;
159 }
160
161 static void __exit kinode_exit(void)
162 {
163 }
164
165 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
166 MODULE_DESCRIPTION("Lustre inode stat test module");
167 MODULE_VERSION(LUSTRE_VERSION_STRING);
168 MODULE_LICENSE("GPL");
169
170 module_init(kinode_init);
171 module_exit(kinode_exit);