Home | History | Annotate | Download | only in diskomizer
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * 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.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 #pragma ident	"@(#)daio_dev.c	1.6	09/05/26 SMI"
     23 
     24 /*
     25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     26  * Use is subject to license terms.
     27  */
     28 
     29 #include <diskomizer/daio.h>
     30 #include <diskomizer/checker.h>
     31 #include "findap.h"
     32 #include <sys/asynch.h>
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 #include <fcntl.h>
     36 #include <unistd.h>
     37 #include <errno.h>
     38 #include <stdlib.h>
     39 #include <string.h>
     40 #include <diskomizer/assert.h>
     41 
     42 #pragma weak directio
     43 
     44 struct daio_devs {
     45 	int max_fd;
     46 	char **paths;
     47 };
     48 
     49 static struct daio_devs dev_names;
     50 
     51 int
     52 daio_dev_open(const char *path, int flags, mode_t mode)
     53 {
     54 	int fd;
     55 	fd = open(path, flags, mode);
     56 	if (fd < 0) {
     57 		return (fd);
     58 	}
     59 
     60 	if ((fd + 1) > dev_names.max_fd) {
     61 		char **x;
     62 		int newlen;
     63 
     64 		for (newlen = dev_names.max_fd == 0 ? 4 : dev_names.max_fd * 2;
     65 		    newlen <= fd; newlen *= 2)
     66 			/* EMPTY */;
     67 
     68 		x = realloc(dev_names.paths, newlen * sizeof (char *));
     69 		if (x == NULL) {
     70 			close(fd);
     71 			return (-1);
     72 		}
     73 		dev_names.paths = x;
     74 		memset(&dev_names.paths[dev_names.max_fd], 0,
     75 		    sizeof (char *) * (newlen - dev_names.max_fd));
     76 		dev_names.max_fd = newlen;
     77 	}
     78 	dev_names.paths[fd] = strdup(path);
     79 	if (dev_names.paths[fd] == NULL) {
     80 		close(fd);
     81 		fd = -1;
     82 	}
     83 	return (fd);
     84 }
     85 
     86 void
     87 daio_dev_dd(int pri, int fd, size_t len, size_t iolen, off_t off)
     88 {
     89 	off64_t x = off / (off64_t)iolen;
     90 
     91 	plog(pri, "use \""
     92 	    "dd if=%s bs=%d iseek=%lld count=1\" to read the "
     93 	    "block\n",
     94 	    dev_names.paths[fd], len, (long long)x);
     95 }
     96 
     97 const char *
     98 daio_dev_path(int fd)
     99 {
    100 	return (dev_names.paths[fd]);
    101 }
    102 
    103 int
    104 daio_dev_close(int fd)
    105 {
    106 	free(dev_names.paths[fd]);
    107 	dev_names.paths[fd] = NULL;
    108 	return (close(fd));
    109 }
    110 
    111 int
    112 daio_dev_directio(int fd, int advice)
    113 {
    114 	if (directio == NULL) {
    115 		return (-1);
    116 	} else {
    117 		return (directio(fd, advice));
    118 	}
    119 }
    120