Home | History | Annotate | Line # | Download | only in h_dtfs
dtfs_vfsops.c revision 1.1
      1 /*	$NetBSD: dtfs_vfsops.c,v 1.1 2010/07/06 14:16:44 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/types.h>
     29 #include <sys/resource.h>
     30 
     31 #include <assert.h>
     32 #include <err.h>
     33 #include <errno.h>
     34 #include <puffs.h>
     35 #include <string.h>
     36 #include <stdlib.h>
     37 #include <unistd.h>
     38 #include <util.h>
     39 
     40 #include "dtfs.h"
     41 
     42 static int
     43 rtstr(struct puffs_usermount *pu, const char *str, enum vtype vt)
     44 {
     45 	struct puffs_node *pn = puffs_getroot(pu);
     46 	struct vattr *va = &pn->pn_va;
     47 	struct dtfs_file *df = DTFS_PTOF(pn);
     48 	char ltarg[256+1];
     49 
     50 	if (sscanf(str, "%*s %256s", ltarg) != 1)
     51 		return 1;
     52 
     53 	dtfs_baseattrs(va, vt, 2);
     54 	df->df_linktarget = estrdup(ltarg);
     55 
     56 	va->va_nlink = 1;
     57 	va->va_size = strlen(df->df_linktarget);
     58 
     59 	puffs_setrootinfo(pu, vt, 0, 0);
     60 
     61 	return 0;
     62 }
     63 
     64 static int
     65 rtdev(struct puffs_usermount *pu, const char *str, enum vtype vt)
     66 {
     67 	struct puffs_node *pn = puffs_getroot(pu);
     68 	struct vattr *va = &pn->pn_va;
     69 	int major, minor;
     70 
     71 	if (sscanf(str, "%*s %d %d", &major, &minor) != 2)
     72 		return 1;
     73 
     74 	dtfs_baseattrs(va, vt, 2);
     75 	va->va_nlink = 1;
     76 	va->va_rdev = makedev(major, minor);
     77 
     78 	if (vt == VBLK)
     79 		va->va_mode |= S_IFBLK;
     80 	else
     81 		va->va_mode |= S_IFCHR;
     82 
     83 	puffs_setrootinfo(pu, vt, 0, va->va_rdev);
     84 
     85 	return 0;
     86 }
     87 
     88 static int
     89 rtnorm(struct puffs_usermount *pu, const char *str, enum vtype vt)
     90 {
     91 	struct puffs_node *pn = puffs_getroot(pu);
     92 	struct vattr *va = &pn->pn_va;
     93 
     94 	dtfs_baseattrs(va, vt, 2);
     95 	if (vt == VDIR)
     96 		va->va_nlink = 2;
     97 	else
     98 		va->va_nlink = 1;
     99 
    100 	puffs_setrootinfo(pu, vt, 0, 0);
    101 
    102 	return 0;
    103 }
    104 
    105 struct rtype {
    106 	char *tstr;
    107 	enum vtype vt;
    108 	int (*pfunc)(struct puffs_usermount *, const char *, enum vtype);
    109 } rtypes[] = {
    110 	{ "reg", VREG, rtnorm },
    111 	{ "dir", VDIR, rtnorm },
    112 	{ "blk", VBLK, rtdev },
    113 	{ "chr", VCHR, rtdev },
    114 	{ "lnk", VLNK, rtstr },
    115 	{ "sock", VSOCK, rtnorm },
    116 	{ "fifo", VFIFO, rtnorm }
    117 };
    118 #define NTYPES (sizeof(rtypes) / sizeof(rtypes[0]))
    119 
    120 int
    121 dtfs_domount(struct puffs_usermount *pu, const char *typestr)
    122 {
    123 	struct dtfs_mount *dtm;
    124 	struct dtfs_file *dff;
    125 	struct puffs_node *pn;
    126 	int i;
    127 
    128 	/* create mount-local thingie */
    129 	dtm = puffs_getspecific(pu);
    130 	dtm->dtm_nextfileid = 3;
    131 	dtm->dtm_nfiles = 1;
    132 	dtm->dtm_fsizes = 0;
    133 	LIST_INIT(&dtm->dtm_pollent);
    134 
    135 	/*
    136 	 * create root directory, do it "by hand" to avoid special-casing
    137 	 * dtfs_genfile()
    138 	 */
    139 	dff = dtfs_newdir();
    140 	dff->df_dotdot = NULL;
    141 	pn = puffs_pn_new(pu, dff);
    142 	if (!pn)
    143 		errx(1, "puffs_newpnode");
    144 	puffs_setroot(pu, pn);
    145 
    146 	if (!typestr) {
    147 		rtnorm(pu, NULL, VDIR);
    148 	} else {
    149 		for (i = 0; i < NTYPES; i++) {
    150 			if (strncmp(rtypes[i].tstr, typestr,
    151 			    strlen(rtypes[i].tstr)) == 0) {
    152 				if (rtypes[i].pfunc(pu, typestr,
    153 				    rtypes[i].vt) != 0) {
    154 					fprintf(stderr, "failed to parse "
    155 					    "\"%s\"\n", typestr);
    156 					return 1;
    157 				}
    158 				break;
    159 			}
    160 		}
    161 		if (i == NTYPES) {
    162 			fprintf(stderr, "no maching type for %s\n", typestr);
    163 			return 1;
    164 		}
    165 	}
    166 
    167 	return 0;
    168 }
    169 
    170 /*
    171  * statvfs() should fill in the following members of struct statvfs:
    172  *
    173  * unsigned long   f_bsize;         file system block size
    174  * unsigned long   f_frsize;        fundamental file system block size
    175  * unsigned long   f_iosize;        optimal file system block size
    176  * fsblkcnt_t      f_blocks;        number of blocks in file system,
    177  *                                            (in units of f_frsize)
    178  *
    179  * fsblkcnt_t      f_bfree;         free blocks avail in file system
    180  * fsblkcnt_t      f_bavail;        free blocks avail to non-root
    181  * fsblkcnt_t      f_bresvd;        blocks reserved for root
    182  *
    183  * fsfilcnt_t      f_files;         total file nodes in file system
    184  * fsfilcnt_t      f_ffree;         free file nodes in file system
    185  * fsfilcnt_t      f_favail;        free file nodes avail to non-root
    186  * fsfilcnt_t      f_fresvd;        file nodes reserved for root
    187  *
    188  *
    189  * The rest are filled in by the kernel.
    190  */
    191 #define ROUND(a,b) (((a) + ((b)-1)) & ~((b)-1))
    192 #define NFILES 1024*1024
    193 int
    194 dtfs_fs_statvfs(struct puffs_usermount *pu, struct statvfs *sbp)
    195 {
    196 	struct rlimit rlim;
    197 	struct dtfs_mount *dtm;
    198 	off_t btot, bfree;
    199 	int pgsize;
    200 
    201 	dtm = puffs_getspecific(pu);
    202 	pgsize = getpagesize();
    203 	memset(sbp, 0, sizeof(struct statvfs));
    204 
    205 	/*
    206 	 * Use datasize rlimit as an _approximation_ for free size.
    207 	 * This, of course, is not accurate due to overhead and not
    208 	 * accounting for metadata.
    209 	 */
    210 	if (getrlimit(RLIMIT_DATA, &rlim) == 0)
    211 		btot = rlim.rlim_cur;
    212 	else
    213 		btot = 16*1024*1024;
    214 	bfree = btot - dtm->dtm_fsizes;
    215 
    216 	sbp->f_blocks = ROUND(btot, pgsize) / pgsize;
    217 	sbp->f_files = NFILES;
    218 
    219 	sbp->f_bsize = sbp->f_frsize = sbp->f_iosize = pgsize;
    220 	sbp->f_bfree = sbp->f_bavail = ROUND(bfree, pgsize) / pgsize;
    221 	sbp->f_ffree = sbp->f_favail = NFILES - dtm->dtm_nfiles;
    222 
    223 	sbp->f_bresvd = sbp->f_fresvd = 0;
    224 
    225 	return 0;
    226 }
    227 #undef ROUND
    228 
    229 static void *
    230 addrcmp(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
    231 {
    232 
    233 	if (pn == arg)
    234 		return pn;
    235 
    236 	return NULL;
    237 }
    238 
    239 int
    240 dtfs_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize,
    241 	struct puffs_newinfo *pni)
    242 {
    243 	struct dtfs_fid *dfid;
    244 	struct puffs_node *pn;
    245 
    246 	assert(fidsize == sizeof(struct dtfs_fid));
    247 	dfid = fid;
    248 
    249 	pn = puffs_pn_nodewalk(pu, addrcmp, dfid->dfid_addr);
    250 	if (pn == NULL)
    251 		return EINVAL;
    252 
    253 	if (pn->pn_va.va_fileid != dfid->dfid_fileid
    254 	    || pn->pn_va.va_gen != dfid->dfid_gen)
    255 		return EINVAL;
    256 
    257 	puffs_newinfo_setcookie(pni, pn);
    258 	puffs_newinfo_setvtype(pni, pn->pn_va.va_type);
    259 	puffs_newinfo_setsize(pni, pn->pn_va.va_size);
    260 	puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev);
    261 
    262 	return 0;
    263 }
    264 
    265 int
    266 dtfs_fs_nodetofh(struct puffs_usermount *pu, void *cookie,
    267 	void *fid, size_t *fidsize)
    268 {
    269 	struct puffs_node *pn = cookie;
    270 	struct dtfs_fid *dfid;
    271 	extern int dynamicfh;
    272 
    273 	if (dynamicfh == 0) {
    274 		assert(*fidsize >= sizeof(struct dtfs_fid));
    275 	} else {
    276 		if (*fidsize < sizeof(struct dtfs_fid)) {
    277 			*fidsize = sizeof(struct dtfs_fid);
    278 			return E2BIG;
    279 		}
    280 		*fidsize = sizeof(struct dtfs_fid);
    281 	}
    282 
    283 	dfid = fid;
    284 
    285 	dfid->dfid_addr = pn;
    286 	dfid->dfid_fileid = pn->pn_va.va_fileid;
    287 	dfid->dfid_gen = pn->pn_va.va_gen;
    288 
    289 	return 0;
    290 }
    291 
    292 int
    293 dtfs_fs_unmount(struct puffs_usermount *pu, int flags)
    294 {
    295 
    296 	return 0;
    297 }
    298