X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=blobdiff_plain;f=lustre%2Ftests%2Fmmap_sanity.c;h=478896ea415a69b41461ccdf92bdefff3e8d18e5;hp=cfa4cafb0a07a59bddead29399c3a396969d5f17;hb=2e3e6ff54c312903e69633fcd0fea90da969c3a9;hpb=d2d56f38da01001c92a09afc6b52b5acbd9bc13c diff --git a/lustre/tests/mmap_sanity.c b/lustre/tests/mmap_sanity.c index cfa4cafb..478896e 100644 --- a/lustre/tests/mmap_sanity.c +++ b/lustre/tests/mmap_sanity.c @@ -1,6 +1,39 @@ /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*- * vim:expandtab:shiftwidth=8:tabstop=8: + * + * GPL HEADER START + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 only, + * as published by the Free Software Foundation. + * + * 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 version 2 for more details (a copy is included + * in the LICENSE file that accompanied this code). + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; If not, see + * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + * + * GPL HEADER END */ +/* + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. + * Use is subject to license terms. + */ +/* + * This file is part of Lustre, http://www.lustre.org/ + * Lustre is a trademark of Sun Microsystems, Inc. + */ + #include #include #include @@ -125,7 +158,11 @@ static int mmap_tst1(char *mnt) perror(mmap_file); return errno; } - ftruncate(fd, region); + if (ftruncate(fd, region) < 0) { + perror("ftruncate()"); + rc = errno; + goto out_close; + } ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { @@ -160,7 +197,11 @@ static int mmap_tst2(char *mnt) perror(mmap_file); return errno; } - ftruncate(fd, page_size); + if (ftruncate(fd, page_size) < 0) { + perror("ftruncate()"); + rc = errno; + goto out_close; + } ptr = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); if (ptr == MAP_FAILED) { @@ -217,7 +258,11 @@ static int mmap_tst3(char *mnt) perror(mmap_file); return errno; } - ftruncate(fd, region); + if (ftruncate(fd, region) < 0) { + perror("ftruncate()"); + rc = errno; + goto out_close; + } ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { @@ -295,14 +340,22 @@ static int mmap_tst4(char *mnt) perror(fileb); return errno; } - ftruncate(fdr, region); + if (ftruncate(fdr, region) < 0) { + perror("ftruncate()"); + rc = errno; + goto out_close; + } fdw = open(filea, O_CREAT|O_RDWR, 0600); if (fdw < 0) { perror(filea); rc = errno; goto out_close; } - ftruncate(fdw, region); + if (ftruncate(fdw, region) < 0) { + perror("ftruncate()"); + rc = errno; + goto out_close; + } ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fdr, 0); if (ptr == MAP_FAILED) { @@ -461,7 +514,11 @@ static int mmap_tst5(char *mnt) perror(mmap_file); return errno; } - ftruncate(fd, region); + if (ftruncate(fd, region) < 0) { + perror("ftruncate()"); + rc = errno; + goto out_close; + } ptr = mmap(NULL, region, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) { @@ -516,7 +573,11 @@ static int mmap_tst6(char *mnt) perror(mmap_file); return errno; } - ftruncate(fd, page_size); + if (ftruncate(fd, page_size) < 0) { + perror("ftruncate()"); + rc = errno; + goto out; + } fd2 = open(mmap_file2, O_RDWR, 0600); if (fd2 < 0) { @@ -566,6 +627,67 @@ out: return rc; } +static int mmap_tst7_func(char *mnt, int rw) +{ + char fname[256]; + char *buf = MAP_FAILED; + ssize_t bytes; + int fd = -1; + int rc = 0; + + if (snprintf(fname, 256, "%s/mmap_tst7.%s", + mnt, (rw == 0) ? "read":"write") >= 256) { + fprintf(stderr, "dir name too long\n"); + rc = ENAMETOOLONG; + goto out; + } + fd = open(fname, O_RDWR | O_DIRECT | O_CREAT, 0644); + if (fd == -1) { + perror("open"); + rc = errno; + goto out; + } + if (ftruncate(fd, 2 * page_size) == -1) { + perror("truncate"); + rc = errno; + goto out; + } + buf = mmap(NULL, page_size, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (buf == MAP_FAILED) { + perror("mmap"); + rc = errno; + goto out; + } + /* ensure the second page isn't mapped */ + munmap(buf + page_size, page_size); + bytes = (rw == 0) ? read(fd, buf, 2 * page_size) : + write(fd, buf, 2 * page_size); + /* Expected behavior */ + if (bytes == page_size) + goto out; + fprintf(stderr, "%s returned %zd, errno = %d\n", + (rw == 0)?"read":"write", bytes, errno); + rc = EIO; +out: + if (buf != MAP_FAILED) + munmap(buf, page_size); + if (fd != -1) + close(fd); + return rc; +} + +static int mmap_tst7(char *mnt) +{ + int rc; + + rc = mmap_tst7_func(mnt, 0); + if (rc != 0) + return rc; + rc = mmap_tst7_func(mnt, 1); + return rc; +} + static int remote_tst(int tc, char *mnt) { int rc = 0; @@ -601,6 +723,7 @@ struct test_case tests[] = { "which mmapped to just this file", mmap_tst5, 1 }, { 6, "mmap test6: check mmap write/read content on two nodes", mmap_tst6, 2 }, + { 7, "mmap test7: file i/o with an unmapped buffer", mmap_tst7, 1}, { 0, NULL, 0, 0 } };