Home | History | Annotate | Line # | Download | only in pcifs
      1  1.1  agc /*
      2  1.1  agc  * Copyright  2007 Alistair Crooks.  All rights reserved.
      3  1.1  agc  *
      4  1.1  agc  * Redistribution and use in source and binary forms, with or without
      5  1.1  agc  * modification, are permitted provided that the following conditions
      6  1.1  agc  * are met:
      7  1.1  agc  * 1. Redistributions of source code must retain the above copyright
      8  1.1  agc  *    notice, this list of conditions and the following disclaimer.
      9  1.1  agc  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1  agc  *    notice, this list of conditions and the following disclaimer in the
     11  1.1  agc  *    documentation and/or other materials provided with the distribution.
     12  1.1  agc  * 3. The name of the author may not be used to endorse or promote
     13  1.1  agc  *    products derived from this software without specific prior written
     14  1.1  agc  *    permission.
     15  1.1  agc  *
     16  1.1  agc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17  1.1  agc  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1  agc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20  1.1  agc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     22  1.1  agc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  agc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  1.1  agc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     25  1.1  agc  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     26  1.1  agc  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  agc  */
     28  1.1  agc #include <sys/types.h>
     29  1.1  agc 
     30  1.1  agc #include <err.h>
     31  1.1  agc #include <errno.h>
     32  1.1  agc #include <fcntl.h>
     33  1.1  agc #include <fuse.h>
     34  1.1  agc #include <stdio.h>
     35  1.1  agc #include <stdlib.h>
     36  1.1  agc #include <string.h>
     37  1.1  agc #include <unistd.h>
     38  1.1  agc 
     39  1.1  agc #include "virtdir.h"
     40  1.1  agc #include "defs.h"
     41  1.1  agc 
     42  1.1  agc static int		 verbose; /* how chatty are we? */
     43  1.1  agc 
     44  1.1  agc static virtdir_t	 pci;
     45  1.1  agc 
     46  1.1  agc 
     47  1.1  agc /* perform the stat operation */
     48  1.1  agc /* if this is the root, then just synthesise the data */
     49  1.1  agc /* otherwise, retrieve the data, and be sure to fill in the size */
     50  1.1  agc static int
     51  1.1  agc pcifs_getattr(const char *path, struct stat *st)
     52  1.1  agc {
     53  1.1  agc 	virt_dirent_t	*ep;
     54  1.1  agc 
     55  1.1  agc 	if (strcmp(path, "/") == 0) {
     56  1.1  agc 		(void) memset(st, 0x0, sizeof(*st));
     57  1.1  agc 		st->st_mode = S_IFDIR | 0755;
     58  1.1  agc 		st->st_nlink = 2;
     59  1.1  agc 		return 0;
     60  1.1  agc 	}
     61  1.1  agc 	if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
     62  1.1  agc 		return -ENOENT;
     63  1.1  agc 	}
     64  1.1  agc 	switch(ep->type) {
     65  1.1  agc 	case 'f':
     66  1.1  agc 		break;
     67  1.1  agc 	case 'd':
     68  1.1  agc 		(void) memcpy(st, &pci.dir, sizeof(*st));
     69  1.1  agc 		break;
     70  1.1  agc 	case 'l':
     71  1.1  agc 		(void) memcpy(st, &pci.lnk, sizeof(*st));
     72  1.1  agc 		st->st_size = ep->tgtlen;
     73  1.1  agc 		st->st_mode = S_IFLNK | 0644;
     74  1.1  agc 		break;
     75  1.1  agc 	}
     76  1.1  agc 	return 0;
     77  1.1  agc }
     78  1.1  agc 
     79  1.1  agc /* readdir operation */
     80  1.1  agc static int
     81  1.1  agc pcifs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
     82  1.1  agc 	      off_t offset, struct fuse_file_info * fi)
     83  1.1  agc {
     84  1.1  agc 	virt_dirent_t	*dp;
     85  1.1  agc 	VIRTDIR		*dirp;
     86  1.1  agc 
     87  1.1  agc 	if ((dirp = openvirtdir(&pci, path)) == NULL) {
     88  1.1  agc 		return 0;
     89  1.1  agc 	}
     90  1.1  agc 	filler(buf, ".", NULL, 0);
     91  1.1  agc 	filler(buf, "..", NULL, 0);
     92  1.1  agc 	while ((dp = readvirtdir(dirp)) != NULL) {
     93  1.1  agc 		filler(buf, dp->d_name, NULL, 0);
     94  1.1  agc 	}
     95  1.1  agc 	closevirtdir(dirp);
     96  1.1  agc 	return 0;
     97  1.1  agc }
     98  1.1  agc 
     99  1.1  agc /* open the file in the file system */
    100  1.1  agc static int
    101  1.1  agc pcifs_open(const char *path, struct fuse_file_info * fi)
    102  1.1  agc {
    103  1.1  agc 	return 0;
    104  1.1  agc }
    105  1.1  agc 
    106  1.1  agc /* read the file's contents in the file system */
    107  1.1  agc static int
    108  1.1  agc pcifs_read(const char *path, char *buf, size_t size, off_t offset,
    109  1.1  agc 	   struct fuse_file_info * fi)
    110  1.1  agc {
    111  1.1  agc 	return 0;
    112  1.1  agc }
    113  1.1  agc 
    114  1.1  agc /* fill in the statvfs struct */
    115  1.1  agc static int
    116  1.1  agc pcifs_statfs(const char *path, struct statvfs *st)
    117  1.1  agc {
    118  1.1  agc 	(void) memset(st, 0x0, sizeof(*st));
    119  1.1  agc 	return 0;
    120  1.1  agc }
    121  1.1  agc 
    122  1.1  agc /* read the symbolic link */
    123  1.1  agc static int
    124  1.1  agc pcifs_readlink(const char *path, char *buf, size_t size)
    125  1.1  agc {
    126  1.1  agc 	virt_dirent_t	*ep;
    127  1.1  agc 
    128  1.1  agc 	if ((ep = virtdir_find(&pci, path, strlen(path))) == NULL) {
    129  1.1  agc 		return -ENOENT;
    130  1.1  agc 	}
    131  1.1  agc 	if (ep->tgt == NULL) {
    132  1.1  agc 		return -ENOENT;
    133  1.1  agc 	}
    134  1.1  agc 	(void) strlcpy(buf, ep->tgt, size);
    135  1.1  agc 	return 0;
    136  1.1  agc }
    137  1.1  agc 
    138  1.1  agc /* operations struct */
    139  1.1  agc static struct fuse_operations pcifs_oper = {
    140  1.1  agc 	.getattr = pcifs_getattr,
    141  1.1  agc 	.readlink = pcifs_readlink,
    142  1.1  agc 	.readdir = pcifs_readdir,
    143  1.1  agc 	.open = pcifs_open,
    144  1.1  agc 	.read = pcifs_read,
    145  1.1  agc 	.statfs = pcifs_statfs
    146  1.1  agc };
    147  1.1  agc 
    148  1.1  agc /* build up a fuse_tree from the information in the database */
    149  1.1  agc static int
    150  1.1  agc build_tree(virtdir_t *tp, int bus)
    151  1.1  agc {
    152  1.1  agc 	struct stat	 dir;
    153  1.1  agc 	struct stat	 f;
    154  1.1  agc 	FILE		*pp;
    155  1.1  agc 	char		 name[MAXPATHLEN];
    156  1.1  agc 	char		 buf[BUFSIZ];
    157  1.1  agc 	char		*cp;
    158  1.1  agc 	int		 cc;
    159  1.1  agc 
    160  1.1  agc 	(void) stat(".", &dir);
    161  1.1  agc 	(void) memcpy(&f, &dir, sizeof(f));
    162  1.1  agc 	(void) snprintf(buf, sizeof(buf), "pcictl pci%d list", bus);
    163  1.1  agc 	f.st_mode = S_IFREG | 0644;
    164  1.1  agc 	if ((pp = popen(buf, "r")) == NULL) {
    165  1.1  agc 		return 0;
    166  1.1  agc 	}
    167  1.1  agc 	while (fgets(buf, sizeof(buf), pp) != NULL) {
    168  1.1  agc 		buf[strlen(buf) - 1] = 0x0;
    169  1.1  agc 		if ((cp = strchr(buf, ' ')) == NULL) {
    170  1.1  agc 			continue;
    171  1.1  agc 		}
    172  1.1  agc 		cc = snprintf(name, sizeof(name), "/%.*s", (int)(cp - buf), buf);
    173  1.2  agc 		virtdir_add(tp, name, cc, 'l', cp + 1, strlen(cp + 1));
    174  1.1  agc 		if (verbose) {
    175  1.1  agc 			printf("pcifs: adding symbolic link `%s' -> `%s'\n", name, cp + 1);
    176  1.1  agc 		}
    177  1.1  agc 	}
    178  1.1  agc 	(void) pclose(pp);
    179  1.1  agc 	return 1;
    180  1.1  agc }
    181  1.1  agc 
    182  1.1  agc int
    183  1.1  agc main(int argc, char **argv)
    184  1.1  agc {
    185  1.1  agc 	int	 bus;
    186  1.1  agc 	int	 i;
    187  1.1  agc 
    188  1.1  agc 	bus = 0;
    189  1.1  agc 	while ((i = getopt(argc, argv, "b:v")) != -1) {
    190  1.1  agc 		switch(i) {
    191  1.1  agc 		case 'b':
    192  1.1  agc 			bus = atoi(optarg);
    193  1.1  agc 			break;
    194  1.1  agc 		case 'v':
    195  1.1  agc 			verbose += 1;
    196  1.1  agc 			break;
    197  1.1  agc 		}
    198  1.1  agc 	}
    199  1.1  agc 	if (!build_tree(&pci, bus)) {
    200  1.1  agc 		exit(EXIT_FAILURE);
    201  1.1  agc 	}
    202  1.1  agc 	return fuse_main(argc, argv, &pcifs_oper, NULL);
    203  1.1  agc }
    204