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.
25 * Copyright (c) 2014, Intel Corporation.
27 * Author: Swapnil Pimpale <spimpale@ddn.com>
42 #include <linux/unistd.h>
44 #include <sys/syscall.h>
46 #include <lustre/lustre_user.h>
48 #define MAX_HANDLE_SZ 128
50 #if !defined(HAVE_FHANDLE_GLIBC_SUPPORT) && defined(HAVE_FHANDLE_SYSCALLS)
51 /* Because the kernel supports this functions doesn't mean that glibc does.
52 * Just in case we define what we need */
57 unsigned char f_handle[0];
60 #if defined(_ASM_X86_UNISTD_64_H)
62 #ifndef __NR_name_to_handle_at
63 #define __NR_name_to_handle_at 303
66 #ifndef __NR_open_by_handle_at
67 #define __NR_open_by_handle_at 304
70 #elif defined(_ASM_X86_UNISTD_32_H)
72 #ifndef __NR_name_to_handle_at
73 #define __NR_name_to_handle_at 341
76 #ifndef __NR_open_by_handle_at
77 #define __NR_open_by_handle_at 342
82 #ifndef __NR_name_to_handle_at
83 #define __NR_name_to_handle_at 264
86 #ifndef __NR_open_by_handle_at
87 #define __NR_open_by_handle_at 265
94 name_to_handle_at(int mnt_fd, const char *filename, struct file_handle *fh,
95 int *mnt_id, int flags)
97 return syscall(__NR_name_to_handle_at, mnt_fd, filename, fh,
102 open_by_handle_at(int mnt_fd, struct file_handle *fh, int mode)
104 return syscall(__NR_open_by_handle_at, mnt_fd, fh, mode);
108 void usage(char *prog)
110 fprintf(stderr, "usage: %s <filepath>\n",
112 fprintf(stderr, "the absolute path of a test file on a "
113 "lustre file system is needed.\n");
117 int main(int argc, char **argv)
119 #ifdef HAVE_FHANDLE_SYSCALLS
120 char *filename, *file, *mount_point = NULL, *readbuf = NULL;
121 int ret, rc = -EINVAL, mnt_id, mnt_fd, fd1, fd2, i, len, offset;
122 struct file_handle *fh = NULL;
123 int file_size, mtime, ctime;
124 struct lu_fid *parent, *fid;
134 if (file[0] != '/') {
135 fprintf(stderr, "Need the absolete path of the file\n");
139 fd1 = open(file, O_RDONLY);
141 fprintf(stderr, "open file %s error: %s\n",
142 file, strerror(errno));
147 /* Get file stats using fd1 from traditional open */
148 bzero(&st, sizeof(struct stat));
149 rc = fstat(fd1, &st);
151 fprintf(stderr, "fstat(%s) error: %s\n", file,
160 file_size = st.st_size;
162 /* Now for the setup to use fhandles */
163 mntpt = setmntent("/etc/mtab", "r");
165 fprintf(stderr, "setmntent error: %s\n",
171 while (NULL != (ent = getmntent(mntpt))) {
172 if ((strncmp(file, ent->mnt_dir, strlen(ent->mnt_dir)) == 0) &&
173 (strcmp(ent->mnt_type, "lustre") == 0)) {
174 mount_point = ent->mnt_dir;
180 if (mount_point == NULL) {
181 fprintf(stderr, "file is not located on a lustre file "
186 filename = rindex(file, '/') + 1;
188 /* Open mount point directory */
189 mnt_fd = open(mount_point, O_DIRECTORY);
191 fprintf(stderr, "open(%s) error: %s\n)", mount_point,
197 /* Allocate memory for file handle */
198 fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
200 fprintf(stderr, "malloc(%d) error: %s\n", MAX_HANDLE_SZ,
205 fh->handle_bytes = MAX_HANDLE_SZ;
207 /* Convert name to handle */
208 ret = name_to_handle_at(mnt_fd, filename, fh, &mnt_id,
211 fprintf(stderr, "name_by_handle_at(%s) error: %s\n", filename,
217 /* Print out the contents of the file handle */
218 fprintf(stdout, "fh_bytes: %u\nfh_type: %d\nfh_data: ",
219 fh->handle_bytes, fh->handle_type);
220 for (i = 0; i < fh->handle_bytes; i++)
221 fprintf(stdout, "%02x ", fh->f_handle[i]);
222 fprintf(stdout, "\n");
224 /* Lustre stores both the parents FID and the file FID
225 * in the f_handle. */
226 parent = (struct lu_fid *)(fh->f_handle + 16);
227 fid = (struct lu_fid *)fh->f_handle;
228 fprintf(stdout, "file's parent FID is "DFID"\n", PFID(parent));
229 fprintf(stdout, "file FID is "DFID"\n", PFID(fid));
231 /* Open the file handle */
232 fd2 = open_by_handle_at(mnt_fd, fh, O_RDONLY);
234 fprintf(stderr, "open_by_handle_at(%s) error: %s\n", filename,
241 bzero(&st, sizeof(struct stat));
242 rc = fstat(fd2, &st);
244 fprintf(stderr, "fstat(%s) error: %s\n", filename,
250 if (ctime != st.st_ctime || file_size != st.st_size ||
251 inode != st.st_ino || mtime != st.st_mtime) {
252 fprintf(stderr, "stat data does not match between fopen "
253 "and fhandle case\n");
259 readbuf = malloc(len);
260 if (readbuf == NULL) {
261 fprintf(stderr, "malloc(%d) error: %s\n", len,
267 for (offset = 0; offset < st.st_size; offset += len) {
268 /* read from the file */
269 rc = read(fd2, readbuf, len);
271 fprintf(stderr, "read(%s) error: %s\n",
272 filename, strerror(errno));
280 fprintf(stdout, "check_fhandle_syscalls test Passed!\n");
295 #else /* !HAVE_FHANDLE_SYSCALLS */
299 fprintf(stderr, "HAVE_FHANDLE_SYSCALLS not defined\n");
301 #endif /* HAVE_FHANDLE_SYSCALLS */