Whamcloud - gitweb
LU-9468 llite: fix for stat under kthread and X86_X32
[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/kthread.h>
37 #include <linux/fs.h>
38 #include <linux/version.h>
39
40 /* Random ID passed by userspace, and printed in messages, used to
41  * separate different runs of that module. */
42 static int run_id;
43 module_param(run_id, int, 0644);
44 MODULE_PARM_DESC(run_id, "run ID");
45
46 /* Name of the file to stat. */
47 static char fname[4096];
48 module_param_string(fname, fname, sizeof(fname), 0644);
49 MODULE_PARM_DESC(fname, "name of file to stat");
50
51 struct completion thr_start;
52
53 #define PREFIX "lustre_kinode_%u:"
54
55 static int stat_file(struct kstat *stbuf)
56 {
57         struct file *fd;
58         int rc;
59
60         fd = filp_open(fname, O_RDONLY, 0);
61         if (IS_ERR(fd)) {
62                 pr_err(PREFIX " can't open file %s\n", run_id, fname);
63                 return -EIO;
64         }
65
66 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
67         rc = vfs_getattr(&fd->f_path, stbuf);
68 #else
69         rc = vfs_getattr(fd->f_path.mnt, fd->f_path.dentry, stbuf);
70 #endif
71         if (rc != 0) {
72                 pr_err(PREFIX " vfs_getattr failed: %d\n", run_id, rc);
73                 goto out;
74         }
75
76         pr_err(PREFIX " inode is %llu\n", run_id, stbuf->ino);
77         rc = 0;
78
79 out:
80         filp_close(fd, NULL);
81
82         return rc;
83 }
84
85 static int stat_thread(void *data)
86 {
87         struct kstat *stbuf = data;
88         int rc;
89
90         /* Signal caller that thread has started. */
91         complete(&thr_start);
92
93         rc = stat_file(stbuf);
94
95         /* Wait for call to kthread_stop. */
96         set_current_state(TASK_INTERRUPTIBLE);
97         while (!kthread_should_stop()) {
98                 schedule();
99                 set_current_state(TASK_INTERRUPTIBLE);
100         }
101         set_current_state(TASK_RUNNING);
102
103         return rc;
104 }
105
106 static int __init kinode_init(void)
107 {
108         struct task_struct *thr;
109         struct kstat stbuf1;
110         struct kstat stbuf2;
111         int rc;
112
113 #ifdef CONFIG_X86_X32
114         pr_err(PREFIX " CONFIG_X86_X32 is set\n", run_id);
115 #else
116         pr_err(PREFIX " CONFIG_X86_X32 is not set\n", run_id);
117 #endif
118
119         if (strlen(fname) < 1) {
120                 pr_err(PREFIX " invalid file name '%s'\n", run_id, fname);
121                 goto out;
122         }
123
124         rc = stat_file(&stbuf1);
125         if (rc) {
126                 pr_err(PREFIX " direct stat failed: %d\n", run_id, rc);
127                 goto out;
128         }
129
130         /* Run the same from a kthread. */
131         init_completion(&thr_start);
132         thr = kthread_run(stat_thread, &stbuf2, "kinode_%u", run_id);
133         if (IS_ERR(thr)) {
134                 pr_err(PREFIX " Cannot create kthread\n", run_id);
135                 goto out;
136         }
137
138         /* Wait for the thread to start, then wait for it to
139          * terminate. */
140         wait_for_completion(&thr_start);
141         rc = kthread_stop(thr);
142         if (rc) {
143                 pr_err(PREFIX " indirect stat failed: %d\n", run_id, rc);
144                 goto out;
145         }
146
147         if (stbuf1.ino != stbuf2.ino)
148                 pr_err(PREFIX " inode numbers are different: %llu %llu\n",
149                        run_id, stbuf1.ino, stbuf2.ino);
150         else
151                 pr_err(PREFIX " inode numbers are identical: %llu\n",
152                        run_id, stbuf1.ino);
153
154 out:
155         /* Don't load. */
156         return -EINVAL;
157 }
158
159 static void __exit kinode_exit(void)
160 {
161 }
162
163 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
164 MODULE_DESCRIPTION("Lustre inode stat test module");
165 MODULE_VERSION(LUSTRE_VERSION_STRING);
166 MODULE_LICENSE("GPL");
167
168 module_init(kinode_init);
169 module_exit(kinode_exit);