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 <liblustre.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 */
53 unsigned int handle_bytes;
55 /* File identifier. */
56 unsigned char f_handle[0];
59 #ifndef __NR_name_to_handle_at
60 #define __NR_name_to_handle_at 264
63 #ifndef __NR_open_by_handle_at
64 #define __NR_open_by_handle_at 265
68 name_to_handle_at(int mnt_fd, const char *filename, struct file_handle *fh,
69 int *mnt_id, int flags)
71 return syscall(__NR_name_to_handle_at, mnt_fd, filename, fh,
76 open_by_handle_at(int mnt_fd, struct file_handle *fh, int mode)
78 return syscall(__NR_open_by_handle_at, mnt_fd, fh, mode);
82 void usage(char *prog)
84 fprintf(stderr, "usage: %s <filepath>\n",
86 fprintf(stderr, "the absolute path of a test file on a "
87 "lustre file system is needed.\n");
91 int main(int argc, char **argv)
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;
108 if (file[0] != '/') {
109 fprintf(stderr, "Need the absolete path of the file\n");
113 fd1 = open(file, O_RDONLY);
115 fprintf(stderr, "open file %s error: %s\n",
116 file, strerror(errno));
121 /* Get file stats using fd1 from traditional open */
122 bzero(&st, sizeof(struct stat));
123 rc = fstat(fd1, &st);
125 fprintf(stderr, "fstat(%s) error: %s\n", file,
134 file_size = st.st_size;
136 /* Now for the setup to use fhandles */
137 mntpt = setmntent("/etc/mtab", "r");
139 fprintf(stderr, "setmntent error: %s\n",
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;
154 if (mount_point == NULL) {
155 fprintf(stderr, "file is not located on a lustre file "
160 filename = rindex(file, '/') + 1;
162 /* Open mount point directory */
163 mnt_fd = open(mount_point, O_DIRECTORY);
165 fprintf(stderr, "open(%s) error: %s\n)", mount_point,
171 /* Allocate memory for file handle */
172 fh = malloc(sizeof(struct file_handle) + MAX_HANDLE_SZ);
174 fprintf(stderr, "malloc(%d) error: %s\n", MAX_HANDLE_SZ,
179 fh->handle_bytes = MAX_HANDLE_SZ;
181 /* Convert name to handle */
182 ret = name_to_handle_at(mnt_fd, filename, fh, &mnt_id,
185 fprintf(stderr, "name_by_handle_at(%s) error: %s\n", filename,
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");
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));
205 /* Open the file handle */
206 fd2 = open_by_handle_at(mnt_fd, fh, O_RDONLY);
208 fprintf(stderr, "open_by_handle_at(%s) error: %s\n", filename,
215 bzero(&st, sizeof(struct stat));
216 rc = fstat(fd2, &st);
218 fprintf(stderr, "fstat(%s) error: %s\n", filename,
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");
233 readbuf = malloc(len);
234 if (readbuf == NULL) {
235 fprintf(stderr, "malloc(%d) error: %s\n", len,
241 for (offset = 0; offset < st.st_size; offset += len) {
242 /* read from the file */
243 rc = read(fd2, readbuf, len);
245 fprintf(stderr, "read(%s) error: %s\n",
246 filename, strerror(errno));
254 fprintf(stdout, "check_fhandle_syscalls test Passed!\n");
269 #else /* !HAVE_FHANDLE_SYSCALLS */
273 fprintf(stderr, "HAVE_FHANDLE_SYSCALLS not defined\n");
275 #endif /* HAVE_FHANDLE_SYSCALLS */