Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/file/file_fadvise.c
48676 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright 2007 Sun Microsystems, Inc. All rights reserved.24* Use is subject to license terms.25*/2627/*28* Copyright (c) 2022 by Information2 Software, Inc. All rights reserved.29*/3031#include "file_common.h"32#include <sys/types.h>33#include <unistd.h>34#include <fcntl.h>35#include <string.h>3637/*38* Call fadvise to prefetch data39*/40static const char *execname = "file_fadvise";4142static void43usage(void)44{45(void) fprintf(stderr,46"usage: %s -f filename -a advice \n", execname);47}4849typedef struct advice_name {50const char *name;51int value;52} advice_name;5354static const struct advice_name table[] = {55#define ADV(name) {#name, name}56ADV(POSIX_FADV_NORMAL),57ADV(POSIX_FADV_RANDOM),58ADV(POSIX_FADV_SEQUENTIAL),59ADV(POSIX_FADV_WILLNEED),60ADV(POSIX_FADV_DONTNEED),61ADV(POSIX_FADV_NOREUSE),62{NULL}63};6465int66main(int argc, char *argv[])67{68char *filename = NULL;69int advice = POSIX_FADV_NORMAL;70int fd, ch;71int err = 0;7273while ((ch = getopt(argc, argv, "a:f:")) != EOF) {74switch (ch) {75case 'a':76advice = -1;77for (const advice_name *p = table; p->name; ++p) {78if (strcmp(p->name, optarg) == 0)79advice = p->value;80}81break;82case 'f':83filename = optarg;84break;85case '?':86(void) printf("unknown arg %c\n", optopt);87usage();88break;89}90}9192if (!filename) {93(void) printf("Filename not specified (-f <file>)\n");94err++;95}9697if (advice == -1) {98(void) printf("advice is invalid\n");99err++;100}101102if (err) {103usage(); /* no return */104return (1);105}106107if ((fd = open(filename, O_RDWR, 0666)) < 0) {108perror("open");109return (1);110}111112if (posix_fadvise(fd, 0, 0, advice) != 0) {113perror("posix_fadvise");114close(fd);115return (1);116}117118close(fd);119120return (0);121}122123124