Whamcloud - gitweb
LU-4512 hsm: Fix lhsmtool_posix --report option
[fs/lustre-release.git] / lustre / tests / check_fhandle_syscalls.c
1 /* GPL HEADER START
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 only,
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License version 2 for more details (a copy is included
13  * in the LICENSE file that accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License
16  * version 2 along with this program; If not, see
17  * http://www.gnu.org/licenses/gpl-2.0.html
18  *
19  * GPL HEADER END
20  */
21
22 /*
23  * Copyright (C) 2013, DataDirect Networks, Inc.
24  * Author: Swapnil Pimpale <spimpale@ddn.com>
25  */
26
27 #ifndef _GNU_SOURCE
28 #define _GNU_SOURCE
29 #endif
30
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <mntent.h>
39 #include <linux/unistd.h>
40 #include <sys/stat.h>
41 #include <sys/syscall.h>
42
43 #include <liblustre.h>
44 #include <lustre/lustre_user.h>
45
46 #define MAX_HANDLE_SZ 128
47
48 #if !defined(HAVE_FHANDLE_GLIBC_SUPPORT) && defined(HAVE_FHANDLE_SYSCALLS)
49 /* Because the kernel supports this functions doesn't mean that glibc does.
50  * Just in case we define what we need */
51 struct file_handle
52 {
53         unsigned int handle_bytes;
54         int handle_type;
55         /* File identifier.  */
56         unsigned char f_handle[0];
57 };
58
59 #ifndef __NR_name_to_handle_at
60 #define __NR_name_to_handle_at 264
61 #endif
62
63 #ifndef __NR_open_by_handle_at
64 #define __NR_open_by_handle_at 265
65 #endif
66
67 static inline int
68 name_to_handle_at(int mnt_fd, const char *filename, struct file_handle *fh,
69                   int *mnt_id, int flags)
70 {
71         return syscall(__NR_name_to_handle_at, mnt_fd, filename, fh,
72                        &mnt_id, flags);
73 }
74
75 static inline int
76 open_by_handle_at(int mnt_fd, struct file_handle *fh, int mode)
77 {
78         return syscall(__NR_open_by_handle_at, mnt_fd, fh, mode);
79 }
80 #endif
81
82 void usage(char *prog)
83 {
84         fprintf(stderr, "usage: %s <filepath>\n",
85                 prog);
86         fprintf(stderr, "the absolute path of a test file on a "
87                 "lustre file system is needed.\n");
88         exit(1);
89 }
90
91 int main(int argc, char **argv)
92 {
93 #ifdef HAVE_FHANDLE_SYSCALLS
94         char *filename, *file, *mount_point = NULL, *readbuf = NULL;
95         int ret, rc = -EINVAL, mnt_id, mnt_fd, fd1, fd2, i, len, offset;
96         struct file_handle *fh = NULL;
97         int file_size, mtime, ctime;
98         struct lu_fid *parent, *fid;
99         struct mntent *ent;
100         struct stat st;
101         __ino_t inode;
102         FILE *mntpt;
103
104         if (argc != 2)
105                 usage(argv[0]);
106
107         file = argv[1];
108         if (file[0] != '/') {
109                 fprintf(stderr, "Need the absolete path of the file\n");
110                 goto out;
111         }
112
113         fd1 = open(file, O_RDONLY);
114         if (fd1 < 0) {
115                 fprintf(stderr, "open file %s error: %s\n",
116                         file, strerror(errno));
117                 rc = errno;
118                 goto out;
119         }
120
121         /* Get file stats using fd1 from traditional open */
122         bzero(&st, sizeof(struct stat));
123         rc = fstat(fd1, &st);
124         if (rc < 0) {
125                 fprintf(stderr, "fstat(%s) error: %s\n", file,
126                         strerror(errno));
127                 rc = errno;
128                 goto out_fd1;
129         }
130
131         inode = st.st_ino;
132         mtime = st.st_mtime;
133         ctime = st.st_ctime;
134         file_size = st.st_size;
135
136         /* Now for the setup to use fhandles */
137         mntpt = setmntent("/etc/mtab", "r");
138         if (mntpt == NULL) {
139                 fprintf(stderr, "setmntent error: %s\n",
140                         strerror(errno));
141                 rc = errno;
142                 goto out_fd1;
143         }
144
145         while (NULL != (ent = getmntent(mntpt))) {
146                 if ((strncmp(file, ent->mnt_dir, strlen(ent->mnt_dir)) == 0) &&
147                     (strcmp(ent->mnt_type, "lustre") == 0)) {
148                         mount_point = ent->mnt_dir;
149                         break;
150                 }
151         }
152         endmntent(mntpt);
153
154         if (mount_point == NULL) {
155                 fprintf(stderr, "file is not located on a lustre file "
156                         "system?\n");
157                 goto out_fd1;
158         }
159
160         filename = rindex(file, '/') + 1;
161
162         /* Open mount point directory */
163         mnt_fd = open(mount_point, O_DIRECTORY);
164         if (mnt_fd < 0) {
165                 fprintf(stderr, "open(%s) error: %s\n)", mount_point,
166                         strerror(errno));
167                 rc = errno;
168                 goto out_fd1;
169         }
170
171         /* Allocate memory for file handle */
172         fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
173         if (!fh) {
174                 fprintf(stderr, "malloc(%d) error: %s\n", MAX_HANDLE_SZ,
175                         strerror(errno));
176                 rc = errno;
177                 goto out_mnt_fd;
178         }
179         fh->handle_bytes = MAX_HANDLE_SZ;
180
181         /* Convert name to handle */
182         ret = name_to_handle_at(mnt_fd, filename, fh, &mnt_id,
183                                 AT_SYMLINK_FOLLOW);
184         if (ret) {
185                 fprintf(stderr, "name_by_handle_at(%s) error: %s\n", filename,
186                         strerror(errno));
187                 rc = errno;
188                 goto out_f_handle;
189         }
190
191         /* Print out the contents of the file handle */
192         fprintf(stdout, "fh_bytes: %u\nfh_type: %d\nfh_data: ",
193                 fh->handle_bytes, fh->handle_type);
194         for (i = 0; i < fh->handle_bytes; i++)
195                 fprintf(stdout, "%02x ", fh->f_handle[i]);
196         fprintf(stdout, "\n");
197
198         /* Lustre stores both the parents FID and the file FID
199          * in the f_handle. */
200         parent = (struct lu_fid *)(fh->f_handle + 16);
201         fid = (struct lu_fid *)fh->f_handle;
202         fprintf(stdout, "file's parent FID is "DFID"\n", PFID(parent));
203         fprintf(stdout, "file FID is "DFID"\n", PFID(fid));
204
205         /* Open the file handle */
206         fd2 = open_by_handle_at(mnt_fd, fh, O_RDONLY);
207         if (fd2 < 0) {
208                 fprintf(stderr, "open_by_handle_at(%s) error: %s\n", filename,
209                         strerror(errno));
210                 rc = errno;
211                 goto out_f_handle;
212         }
213
214         /* Get file size */
215         bzero(&st, sizeof(struct stat));
216         rc = fstat(fd2, &st);
217         if (rc < 0) {
218                 fprintf(stderr, "fstat(%s) error: %s\n", filename,
219                         strerror(errno));
220                 rc = errno;
221                 goto out_fd2;
222         }
223
224         if (ctime != st.st_ctime || file_size != st.st_size ||
225             inode != st.st_ino || mtime != st.st_mtime) {
226                 fprintf(stderr, "stat data does not match between fopen "
227                         "and fhandle case\n");
228                 goto out_fd2;
229         }
230
231         if (st.st_size) {
232                 len = st.st_blksize;
233                 readbuf = malloc(len);
234                 if (readbuf == NULL) {
235                         fprintf(stderr, "malloc(%d) error: %s\n", len,
236                                 strerror(errno));
237                         rc = errno;
238                         goto out_fd2;
239                 }
240
241                 for (offset = 0; offset < st.st_size; offset += len) {
242                         /* read from the file */
243                         rc = read(fd2, readbuf, len);
244                         if (rc < 0) {
245                                 fprintf(stderr, "read(%s) error: %s\n",
246                                         filename, strerror(errno));
247                                 rc = errno;
248                                 goto out_readbuf;
249                         }
250                 }
251         }
252
253         rc = 0;
254         fprintf(stdout, "check_fhandle_syscalls test Passed!\n");
255
256 out_readbuf:
257         if (readbuf != NULL)
258                 free(readbuf);
259 out_fd2:
260         close(fd2);
261 out_f_handle:
262         free(fh);
263 out_mnt_fd:
264         close(mnt_fd);
265 out_fd1:
266         close(fd1);
267 out:
268         return rc;
269 #else /* !HAVE_FHANDLE_SYSCALLS */
270         if (argc != 2)
271                 usage(argv[0]);
272
273         fprintf(stderr, "HAVE_FHANDLE_SYSCALLS not defined\n");
274         return 0;
275 #endif /* HAVE_FHANDLE_SYSCALLS */
276 }