2 * stale_handle.c - attempt to create a stale handle and open it
4 * Copyright (C) 2010 Red Hat, Inc. All Rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <sys/types.h>
32 #include <xfs/handle.h>
35 int main(int argc, char **argv)
41 char fname[MAXPATHLEN];
43 void *handle[NUMFILES];
44 size_t hlen[NUMFILES];
51 fprintf(stderr, "usage: stale_handle test_dir\n");
56 if (stat(test_dir, &st) != 0) {
61 ret = path_to_fshandle(test_dir, (void **)fshandle, &fshlen);
63 perror("path_to_fshandle");
68 * create a large number of files to force allocation of new inode
71 for (i=0; i < NUMFILES; i++) {
72 sprintf(fname, "%s/file%06d", test_dir, i);
73 fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644);
75 printf("Warning (%s,%d), open(%s) failed.\n", __FILE__, __LINE__, fname);
82 /* sync to get the new inodes to hit the disk */
85 /* create the handles */
86 for (i=0; i < NUMFILES; i++) {
87 sprintf(fname, "%s/file%06d", test_dir, i);
88 ret = path_to_handle(fname, &handle[i], &hlen[i]);
90 perror("path_to_handle");
95 /* unlink the files */
96 for (i=0; i < NUMFILES; i++) {
97 sprintf(fname, "%s/file%06d", test_dir, i);
105 /* sync to get log forced for unlink transactions to hit the disk */
108 /* sync once more FTW */
112 * now drop the caches so that unlinked inodes are reclaimed and
113 * buftarg page cache is emptied so that the inode cluster has to be
114 * fetched from disk again for the open_by_handle() call.
116 system("echo 3 > /proc/sys/vm/drop_caches");
119 * now try to open the files by the stored handles. Expecting ENOENT
122 for (i=0; i < NUMFILES; i++) {
124 fd = open_by_handle(handle[i], hlen[i], O_RDWR);
125 if (fd < 0 && (errno == ENOENT || errno == ESTALE)) {
126 free_handle(handle[i], hlen[i]);
130 printf("open_by_handle(%d) opened an unlinked file!\n", i);
133 printf("open_by_handle(%d) returned %d incorrectly on an unlinked file!\n", i, errno);
134 free_handle(handle[i], hlen[i]);