X-Git-Url: http://git.apps.os.sepia.ceph.com/?p=xfstests-dev.git;a=blobdiff_plain;f=src%2Fopen_by_handle.c;h=f5be2350e3fdefd447edb419467e812b550ea8fd;hp=e8edcf5857a31aa04abf78dc1668fc3d5eebc802;hb=555cdbee56476fa8e34cd8bd400f39fa70e0ece0;hpb=fcaeb02ed177ef8fd72b73465089c9fc0e2b31f9 diff --git a/src/open_by_handle.c b/src/open_by_handle.c index e8edcf58..f5be2350 100644 --- a/src/open_by_handle.c +++ b/src/open_by_handle.c @@ -27,7 +27,7 @@ /* -usage: open_by_handle [-cludmrwap] [num_files] +usage: open_by_handle [-cludmrwapknhs] [<-i|-o> ] [num_files] Examples: @@ -43,24 +43,39 @@ Examples: open_by_handle -p [N] -3. Get file handles for existing test set, write data to files, +3. Get file handles for existing test set and write them to a file: + + open_by_handle -p -o [N] + +4. Read file handles from file and open files by handle without + dropping caches beforehand. Sleep afterhand to keep files open: + + open_by_handle -nps -i [N] + +5. Get file handles for existing test set, write data to files, drop caches, open all files by handle, read and verify written data, write new data to file: open_by_handle -rwa [N] -4. Get file handles for existing test set, unlink all test files, +6. Get file handles for existing test set, unlink all test files, remove test_dir, drop caches, try to open all files by handle and expect ESTALE: open_by_handle -dp [N] -5. Get file handles for existing test set, rename all test files, +7. Get file handles for existing test set, keep open file handles for all + test files, unlink all test files, drop caches and try to open all files + by handle (should work): + + open_by_handle -dk [N] + +8. Get file handles for existing test set, rename all test files, drop caches, try to open all files by handle (should work): open_by_handle -m [N] -6. Get file handles for existing test set, hardlink all test files, +9. Get file handles for existing test set, hardlink all test files, then unlink the original files, drop caches and try to open all files by handle (should work): @@ -97,10 +112,12 @@ struct handle { void usage(void) { - fprintf(stderr, "usage: open_by_handle [-cludmrwap] [num_files]\n"); + fprintf(stderr, "usage: open_by_handle [-cludmrwapknhs] [<-i|-o> ] [num_files]\n"); fprintf(stderr, "\n"); fprintf(stderr, "open_by_handle -c [N] - create N test files under test_dir, try to get file handles and exit\n"); fprintf(stderr, "open_by_handle [N] - get file handles of test files, drop caches and try to open by handle\n"); + fprintf(stderr, "open_by_handle -n [N] - get file handles of test files and try to open by handle without drop caches\n"); + fprintf(stderr, "open_by_handle -k [N] - get file handles of files that are kept open, drop caches and try to open by handle\n"); fprintf(stderr, "open_by_handle -w [N] - write data to test files before open by handle\n"); fprintf(stderr, "open_by_handle -r [N] - read data from test files after open by handle and verify written data\n"); fprintf(stderr, "open_by_handle -a [N] - write data to test files after open by handle\n"); @@ -109,6 +126,9 @@ void usage(void) fprintf(stderr, "open_by_handle -d [N] - unlink test files and hardlinks, drop caches and try to open by handle\n"); fprintf(stderr, "open_by_handle -m [N] - rename test files, drop caches and try to open by handle\n"); fprintf(stderr, "open_by_handle -p - create/delete and try to open by handle also test_dir itself\n"); + fprintf(stderr, "open_by_handle -i [N] - read test files handles from file and try to open by handle\n"); + fprintf(stderr, "open_by_handle -o [N] - get file handles of test files and write handles to file\n"); + fprintf(stderr, "open_by_handle -s [N] - wait in sleep loop after opening files by handle to keep them open\n"); exit(EXIT_FAILURE); } @@ -124,14 +144,17 @@ int main(int argc, char **argv) char *test_dir; char *mount_dir; int mount_fd, mount_id; + char *infile = NULL, *outfile = NULL; + int in_fd = 0, out_fd = 0; int numfiles = 1; int create = 0, delete = 0, nlink = 1, move = 0; int rd = 0, wr = 0, wrafter = 0, parent = 0; + int keepopen = 0, drop_caches = 1, sleep_loop = 0; - if (argc < 2 || argc > 4) + if (argc < 2) usage(); - while ((c = getopt(argc, argv, "cludmrwap")) != -1) { + while ((c = getopt(argc, argv, "cludmrwapknhi:o:s")) != -1) { switch (c) { case 'c': create = 1; @@ -165,13 +188,38 @@ int main(int argc, char **argv) case 'p': parent = 1; break; + case 'k': + keepopen = 1; + break; + case 'n': + drop_caches = 0; + break; + case 'i': + infile = optarg; + in_fd = open(infile, O_RDONLY); + if (in_fd < 0) { + perror(infile); + return EXIT_FAILURE; + } + break; + case 'o': + outfile = optarg; + out_fd = creat(outfile, 0644); + if (out_fd < 0) { + perror(outfile); + return EXIT_FAILURE; + } + break; + case 's': + sleep_loop = 1; + break; default: fprintf(stderr, "illegal option '%s'\n", argv[optind]); case 'h': usage(); } } - if (optind == argc || optind > 2) + if (optind == argc) usage(); test_dir = argv[optind++]; if (optind < argc) @@ -181,12 +229,34 @@ int main(int argc, char **argv) usage(); } - if (parent) { + /* + * The way we determine the mount_dir to be used for mount_fd argument + * for open_by_handle_at() depends on other command line arguments: + * + * -p flag usually (see -i below) implies that test_dir is NOT a mount + * point, but a directory inside a mount point that we will create + * and/or encode/decode during the test, so we use test_dir's parent + * for mount_fd. Even when not creatig test_dir, if we would use + * test_dir as mount_fd, then drop_caches will not drop the test_dir + * dcache entry. + * + * If -p is not specified, we don't have a hint whether test_dir is a + * mount point or not, so we assume the worst case, that it is a + * mount point and therefore, we cannnot use parent as mount_fd, + * because parent may be on a differnt file system. + * + * -i flag, even with -p flag, implies that test_dir IS a mount point, + * because we are testing open by handle of dir, which may have been + * deleted or renamed and we are not creating nor encoding the + * directory file handle. -i flag is meant to be used for tests + * after encoding file handles and mount cycle the file system. If + * we would require the test to pass in with -ip the test_dir we + * want to decode and not the mount point, that would have populated + * the dentry cache and the use of -ip flag combination would not + * allow testing decode of dir file handle in cold dcache scenario. + */ + if (parent && !in_fd) { strcpy(dname, test_dir); - /* - * -p flag implies that test_dir is NOT a mount point, - * so its parent can be used as mount_fd for open_by_handle_at. - */ mount_dir = dirname(dname); if (create) ret = mkdir(test_dir, 0700); @@ -213,7 +283,7 @@ int main(int argc, char **argv) sprintf(fname2, "%s/link%06d", test_dir, i); fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd < 0) { - strcat(fname, ": open"); + strcat(fname, ": open(O_CREAT)"); perror(fname); return EXIT_FAILURE; } @@ -230,25 +300,67 @@ int main(int argc, char **argv) /* sync to get the new inodes to hit the disk */ sync(); - /* create the handles */ + /* + * encode the file handles or read them from file (-i) and maybe store + * them to a file (-o). + */ for (i=0; i < numfiles; i++) { sprintf(fname, "%s/file%06d", test_dir, i); - handle[i].fh.handle_bytes = MAX_HANDLE_SZ; - ret = name_to_handle_at(AT_FDCWD, fname, &handle[i].fh, &mount_id, 0); - if (ret < 0) { - strcat(fname, ": name_to_handle"); - perror(fname); - return EXIT_FAILURE; + if (in_fd) { + ret = read(in_fd, (char *)&handle[i], sizeof(*handle)); + if (ret < sizeof(*handle)) { + fprintf(stderr, "failed reading file handle #%d from '%s'\n", i, infile); + return EXIT_FAILURE; + } + } else { + handle[i].fh.handle_bytes = MAX_HANDLE_SZ; + ret = name_to_handle_at(AT_FDCWD, fname, &handle[i].fh, &mount_id, 0); + if (ret < 0) { + strcat(fname, ": name_to_handle"); + perror(fname); + return EXIT_FAILURE; + } + } + if (keepopen) { + /* Open without close to keep unlinked files around */ + fd = open(fname, O_RDONLY); + if (fd < 0) { + strcat(fname, ": open(O_RDONLY)"); + perror(fname); + return EXIT_FAILURE; + } + } + if (out_fd) { + ret = write(out_fd, (char *)&handle[i], sizeof(*handle)); + if (ret < sizeof(*handle)) { + fprintf(stderr, "failed writing file handle #%d to '%s'\n", i, outfile); + return EXIT_FAILURE; + } } } if (parent) { - dir_handle.fh.handle_bytes = MAX_HANDLE_SZ; - ret = name_to_handle_at(AT_FDCWD, test_dir, &dir_handle.fh, &mount_id, 0); - if (ret < 0) { - strcat(dname, ": name_to_handle"); - perror(dname); - return EXIT_FAILURE; + if (in_fd) { + ret = read(in_fd, (char *)&dir_handle, sizeof(*handle)); + if (ret < sizeof(*handle)) { + fprintf(stderr, "failed reading dir file handle from '%s'\n", infile); + return EXIT_FAILURE; + } + } else { + dir_handle.fh.handle_bytes = MAX_HANDLE_SZ; + ret = name_to_handle_at(AT_FDCWD, test_dir, &dir_handle.fh, &mount_id, 0); + if (ret < 0) { + strcat(dname, ": name_to_handle"); + perror(dname); + return EXIT_FAILURE; + } + } + if (out_fd) { + ret = write(out_fd, (char *)&dir_handle, sizeof(*handle)); + if (ret < sizeof(*handle)) { + fprintf(stderr, "failed writing dir file handle to '%s'\n", outfile); + return EXIT_FAILURE; + } } } @@ -269,8 +381,8 @@ int main(int argc, char **argv) close(fd); } - /* after creating test set only check that fs supports exportfs */ - if (create) + /* If creating test set or saving files handles, we are done */ + if (create || out_fd) return EXIT_SUCCESS; /* hardlink the files */ @@ -337,10 +449,12 @@ int main(int argc, char **argv) * buftarg page cache is emptied so that the inode cluster has to be * fetched from disk again for the open_by_handle() call. */ - ret = system("echo 3 > /proc/sys/vm/drop_caches"); - if (ret < 0) { - perror("drop_caches"); - return EXIT_FAILURE; + if (drop_caches) { + ret = system("echo 3 > /proc/sys/vm/drop_caches"); + if (ret < 0) { + perror("drop_caches"); + return EXIT_FAILURE; + } } /* @@ -350,7 +464,7 @@ int main(int argc, char **argv) for (i=0; i < numfiles; i++) { errno = 0; fd = open_by_handle_at(mount_fd, &handle[i].fh, wrafter ? O_RDWR : O_RDONLY); - if (nlink && fd >= 0) { + if ((nlink || keepopen) && fd >= 0) { if (rd) { char buf[4] = {0}; int size = read(fd, buf, 4); @@ -368,9 +482,10 @@ int main(int argc, char **argv) perror(fname); return EXIT_FAILURE; } - close(fd); + if (!sleep_loop) + close(fd); continue; - } else if (!nlink && fd < 0 && (errno == ENOENT || errno == ESTALE)) { + } else if (!nlink && !keepopen && fd < 0 && (errno == ENOENT || errno == ESTALE)) { continue; } sprintf(fname, "%s/file%06d", test_dir, i); @@ -378,9 +493,10 @@ int main(int argc, char **argv) printf("open_by_handle(%s) opened an unlinked file!\n", fname); close(fd); } else { - printf("open_by_handle(%s) returned %d incorrectly on %s file!\n", + printf("open_by_handle(%s) returned %d incorrectly on %s%s file!\n", fname, errno, - nlink ? "a linked" : "an unlinked"); + nlink ? "a linked" : "an unlinked", + keepopen ? " open" : ""); } failed++; } @@ -391,7 +507,7 @@ int main(int argc, char **argv) if (!nlink) { printf("open_by_handle(%s) opened an unlinked dir!\n", dname); return EXIT_FAILURE; - } else { + } else if (rd) { /* * Sanity check dir fd - expect to access orig file IFF * it was not unlinked nor renamed. @@ -418,7 +534,8 @@ int main(int argc, char **argv) return EXIT_FAILURE; } } - close(fd); + if (!sleep_loop) + close(fd); } else if (nlink || !(errno == ENOENT || errno == ESTALE)) { printf("open_by_handle(%s) returned %d incorrectly on %s dir!\n", dname, errno, @@ -429,5 +546,13 @@ int main(int argc, char **argv) if (failed) return EXIT_FAILURE; + + /* + * Sleep keeping files open by handle - the program need to be killed + * to release the open files. + */ + while (sleep_loop) + sleep(1); + return EXIT_SUCCESS; }