From patchwork Tue Apr 18 18:17:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 9685969 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 065556037E for ; Tue, 18 Apr 2017 18:17:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EEF371FFD6 for ; Tue, 18 Apr 2017 18:17:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E3DEC2022C; Tue, 18 Apr 2017 18:17:22 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 617EC201F5 for ; Tue, 18 Apr 2017 18:17:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753014AbdDRSRV (ORCPT ); Tue, 18 Apr 2017 14:17:21 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33344 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752610AbdDRSRT (ORCPT ); Tue, 18 Apr 2017 14:17:19 -0400 Received: by mail-wm0-f68.google.com with SMTP id o81so899504wmb.0; Tue, 18 Apr 2017 11:17:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=c8CDF9zfbVKB7HrP+64t0/T/L5dundKmVIq/2uv3r+0=; b=V84sINY90mm0tP0YttNwiSfGUiFGk2mvmhBATuSV0Edv0UtPM/336GCMmo7i0JQX3E 4VA66A/oss1ifSnCie6S+7hGdHPps6fI1qCNp6ZX4UlulfSvmwJPHCZ6cP/W/47idX9a 0spYgPGOJ8RZllnNMzem6UbUhdlhrUW0WocqtcID5HXgP34MRdza+IIowARjw938sQQU h3uZh64FTmf6e2rmipqwYNIXwaVeoJOwOik0WgT7KbVK2HNYiObheKXbu2oG+zNAuzM5 2g+vW5UejmPXqj5JjcY/MxbFO7e9eUgTd3yBTaKqIF3iYlnyrxRiBM1fwwEmChjMItv+ QzzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=c8CDF9zfbVKB7HrP+64t0/T/L5dundKmVIq/2uv3r+0=; b=IafW7MdcvYwhgAtLn2mlgAmSTKk2+aoe+/zHgGjDDGNjWpnQhs0kr6XARQD1Yyfntc DIO7urUlUxBF/Zw1/ZxHvyKxwUMFO7NvA62Sa8Iu29nhP4bYKWapoQJ0Y9jzzP0hu0Mu nEFVf8LCFjXgVOYHhg4QtLfM5LUNQCkcxpUs3a34MCPLHNLWCL+8BksSfgOtxKl7zKFl gksT/JH2xXbL2/kZM2LKqXod7zZyhPfF11MqiZ7ViK3vvbD5aR6ImOE9hMkDq+wI2U/n aIiQFfaZmMWP2hFl/caS/39kkQyoKI/PSVIVaxHMj1FXG/DtaUMzIegrmdxu31IevCzd ljHQ== X-Gm-Message-State: AN3rC/5+p2Fa8+X9IEqRDyqkvA3V67b2Po6vIO9q2AVxoMXm1wK8c/2Z OueP4quRIbkpSg== X-Received: by 10.28.208.74 with SMTP id h71mr10577566wmg.36.1492539436607; Tue, 18 Apr 2017 11:17:16 -0700 (PDT) Received: from amir-VirtualBox.lan (bzq-79-179-55-127.red.bezeqint.net. [79.179.55.127]) by smtp.gmail.com with ESMTPSA id v126sm15958709wmd.11.2017.04.18.11.17.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 18 Apr 2017 11:17:16 -0700 (PDT) From: Amir Goldstein To: Eryu Guan Cc: Miklos Szeredi , Trond Myklebust , Jeff Layton , "J . Bruce Fields" , fstests@vger.kernel.org, linux-unionfs@vger.kernel.org Subject: [PATCH 1/4] src/open_by_handle: helper to test open_by_handle_at() syscall Date: Tue, 18 Apr 2017 21:17:21 +0300 Message-Id: <1492539444-25938-2-git-send-email-amir73il@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1492539444-25938-1-git-send-email-amir73il@gmail.com> References: <1492539444-25938-1-git-send-email-amir73il@gmail.com> Sender: fstests-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a clone of src/stale_handle.c test that uses generic open_by_handle_at() syscall instead of the xfs specific ioctl. No test is using this helper yet. Signed-off-by: Amir Goldstein --- src/Makefile | 2 +- src/open_by_handle.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 src/open_by_handle.c diff --git a/src/Makefile b/src/Makefile index e62d7a9..6b38e77 100644 --- a/src/Makefile +++ b/src/Makefile @@ -22,7 +22,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ seek_copy_test t_readdir_1 t_readdir_2 fsync-tester nsexec cloner \ renameat2 t_getcwd e4compact test-nextquota punch-alternating \ attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \ - dio-invalidate-cache stat_test + dio-invalidate-cache stat_test open_by_handle SUBDIRS = diff --git a/src/open_by_handle.c b/src/open_by_handle.c new file mode 100644 index 0000000..8f04865 --- /dev/null +++ b/src/open_by_handle.c @@ -0,0 +1,145 @@ +/* + * open_by_handle.c - attempt to create a file handle and open it + * with open_by_handle_at() syscall + * + * Copyright (C) 2017 CTERA Networks. All Rights Reserved. + * Author: Amir Goldstein + * + * from: + * stale_handle.c + * + * Copyright (C) 2010 Red Hat, Inc. All Rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#define TEST_UTIME + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NUMFILES 1024 + +struct handle { + struct file_handle fh; + unsigned char fid[MAX_HANDLE_SZ]; +} handle[NUMFILES]; + +int main(int argc, char **argv) +{ + int i; + int fd; + int ret; + int failed = 0; + char fname[PATH_MAX]; + char *test_dir; + int mount_fd, mount_id; + + if (argc != 2) { + fprintf(stderr, "usage: open_by_handle \n"); + return EXIT_FAILURE; + } + + test_dir = argv[1]; + mount_fd = open(test_dir, O_RDONLY|O_DIRECTORY); + if (mount_fd < 0) { + perror("open test_dir"); + return EXIT_FAILURE; + } + + /* + * create a large number of files to force allocation of new inode + * chunks on disk. + */ + for (i=0; i < NUMFILES; i++) { + sprintf(fname, "%s/file%06d", test_dir, i); + fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644); + if (fd < 0) { + printf("Warning (%s,%d), open(%s) failed.\n", __FILE__, __LINE__, fname); + perror(fname); + return EXIT_FAILURE; + } + close(fd); + } + + /* sync to get the new inodes to hit the disk */ + sync(); + + /* create the handles */ + 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) { + perror("name_to_handle"); + return EXIT_FAILURE; + } + } + + /* unlink the files */ + for (i=0; i < NUMFILES; i++) { + sprintf(fname, "%s/file%06d", test_dir, i); + ret = unlink(fname); + if (ret < 0) { + perror("unlink"); + return EXIT_FAILURE; + } + } + + /* sync to get log forced for unlink transactions to hit the disk */ + sync(); + + /* sync once more FTW */ + sync(); + + /* + * now drop the caches so that unlinked inodes are reclaimed and + * 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; + } + + /* + * now try to open the files by the stored handles. Expecting ENOENT + * for all of them. + */ + for (i=0; i < NUMFILES; i++) { + errno = 0; + fd = open_by_handle_at(mount_fd, &handle[i].fh, O_RDWR); + if (fd < 0 && (errno == ENOENT || errno == ESTALE)) { + continue; + } + if (fd >= 0) { + printf("open_by_handle(%d) opened an unlinked file!\n", i); + close(fd); + } else + printf("open_by_handle(%d) returned %d incorrectly on an unlinked file!\n", i, errno); + failed++; + } + if (failed) + return EXIT_FAILURE; + return EXIT_SUCCESS; +}