3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (C) 2013, DataDirect Networks, Inc.
24 * Author: Swapnil Pimpale <spimpale@ddn.com>
39 #include <linux/unistd.h>
41 #include <sys/syscall.h>
43 #include <libcfs/libcfs.h>
44 #include <lustre/lustre_user.h>
46 #define MAX_HANDLE_SZ 128
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 */
55 unsigned char f_handle[0];
58 #if defined(_ASM_X86_UNISTD_64_H)
60 #ifndef __NR_name_to_handle_at
61 #define __NR_name_to_handle_at 303
64 #ifndef __NR_open_by_handle_at
65 #define __NR_open_by_handle_at 304
68 #elif defined(_ASM_X86_UNISTD_32_H)
70 #ifndef __NR_name_to_handle_at
71 #define __NR_name_to_handle_at 341
74 #ifndef __NR_open_by_handle_at
75 #define __NR_open_by_handle_at 342
80 #ifndef __NR_name_to_handle_at
81 #define __NR_name_to_handle_at 264
84 #ifndef __NR_open_by_handle_at
85 #define __NR_open_by_handle_at 265
92 name_to_handle_at(int mnt_fd, const char *filename, struct file_handle *fh,
93 int *mnt_id, int flags)
95 return syscall(__NR_name_to_handle_at, mnt_fd, filename, fh,
100 open_by_handle_at(int mnt_fd, struct file_handle *fh, int mode)
102 return syscall(__NR_open_by_handle_at, mnt_fd, fh, mode);
106 void usage(char *prog)
108 fprintf(stderr, "usage: %s <filepath>\n",
110 fprintf(stderr, "the absolute path of a test file on a "
111 "lustre file system is needed.\n");
115 int main(int argc, char **argv)
117 #ifdef HAVE_FHANDLE_SYSCALLS
118 char *filename, *file, *mount_point = NULL, *readbuf = NULL;
119 int ret, rc = -EINVAL, mnt_id, mnt_fd, fd1, fd2, i, len, offset;
120 struct file_handle *fh = NULL;
121 int file_size, mtime, ctime;
122 struct lu_fid *parent, *fid;
132 if (file[0] != '/') {
133 fprintf(stderr, "Need the absolete path of the file\n");
137 fd1 = open(file, O_RDONLY);
139 fprintf(stderr, "open file %s error: %s\n",
140 file, strerror(errno));
145 /* Get file stats using fd1 from traditional open */
146 bzero(&st, sizeof(struct stat));
147 rc = fstat(fd1, &st);
149 fprintf(stderr, "fstat(%s) error: %s\n", file,
158 file_size = st.st_size;
160 /* Now for the setup to use fhandles */
161 mntpt = setmntent("/etc/mtab", "r");
163 fprintf(stderr, "setmntent error: %s\n",
169 while (NULL != (ent = getmntent(mntpt))) {
170 if ((strncmp(file, ent->mnt_dir, strlen(ent->mnt_dir)) == 0) &&
171 (strcmp(ent->mnt_type, "lustre") == 0)) {
172 mount_point = ent->mnt_dir;
178 if (mount_point == NULL) {
179 fprintf(stderr, "file is not located on a lustre file "
184 filename = rindex(file, '/') + 1;
186 /* Open mount point directory */
187 mnt_fd = open(mount_point, O_DIRECTORY);
189 fprintf(stderr, "open(%s) error: %s\n)", mount_point,
195 /* Allocate memory for file handle */
196 fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
198 fprintf(stderr, "malloc(%d) error: %s\n", MAX_HANDLE_SZ,
203 fh->handle_bytes = MAX_HANDLE_SZ;
205 /* Convert name to handle */
206 ret = name_to_handle_at(mnt_fd, filename, fh, &mnt_id,
209 fprintf(stderr, "name_by_handle_at(%s) error: %s\n", filename,
215 /* Print out the contents of the file handle */
216 fprintf(stdout, "fh_bytes: %u\nfh_type: %d\nfh_data: ",
217 fh->handle_bytes, fh->handle_type);
218 for (i = 0; i < fh->handle_bytes; i++)
219 fprintf(stdout, "%02x ", fh->f_handle[i]);
220 fprintf(stdout, "\n");
222 /* Lustre stores both the parents FID and the file FID
223 * in the f_handle. */
224 parent = (struct lu_fid *)(fh->f_handle + 16);
225 fid = (struct lu_fid *)fh->f_handle;
226 fprintf(stdout, "file's parent FID is "DFID"\n", PFID(parent));
227 fprintf(stdout, "file FID is "DFID"\n", PFID(fid));
229 /* Open the file handle */
230 fd2 = open_by_handle_at(mnt_fd, fh, O_RDONLY);
232 fprintf(stderr, "open_by_handle_at(%s) error: %s\n", filename,
239 bzero(&st, sizeof(struct stat));
240 rc = fstat(fd2, &st);
242 fprintf(stderr, "fstat(%s) error: %s\n", filename,
248 if (ctime != st.st_ctime || file_size != st.st_size ||
249 inode != st.st_ino || mtime != st.st_mtime) {
250 fprintf(stderr, "stat data does not match between fopen "
251 "and fhandle case\n");
257 readbuf = malloc(len);
258 if (readbuf == NULL) {
259 fprintf(stderr, "malloc(%d) error: %s\n", len,
265 for (offset = 0; offset < st.st_size; offset += len) {
266 /* read from the file */
267 rc = read(fd2, readbuf, len);
269 fprintf(stderr, "read(%s) error: %s\n",
270 filename, strerror(errno));
278 fprintf(stdout, "check_fhandle_syscalls test Passed!\n");
293 #else /* !HAVE_FHANDLE_SYSCALLS */
297 fprintf(stderr, "HAVE_FHANDLE_SYSCALLS not defined\n");
299 #endif /* HAVE_FHANDLE_SYSCALLS */