Home | History | Annotate | Line # | Download | only in mount_sysctlfs
sysctlfs.c revision 1.2
      1 /*	$NetBSD: sysctlfs.c,v 1.2 2007/08/09 22:03:20 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006, 2007  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 /*
     29  * sysctlfs: mount sysctls as a file system tree.  Supports query and
     30  * modify of nodes in the sysctl namespace in addition to namespace
     31  * traversal.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: sysctlfs.c,v 1.2 2007/08/09 22:03:20 pooka Exp $");
     37 #endif /* !lint */
     38 
     39 #include <sys/types.h>
     40 #include <sys/sysctl.h>
     41 
     42 #include <assert.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <mntopts.h>
     46 #include <paths.h>
     47 #include <puffs.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 #include <util.h>
     52 
     53 PUFFSOP_PROTOS(sysctlfs)
     54 
     55 struct sfsnode {
     56 	int sysctl_flags;
     57 	ino_t myid;
     58 };
     59 
     60 #define SFSPATH_DOTDOT 0
     61 #define SFSPATH_NORMAL 1
     62 
     63 #define N_HIERARCHY 10
     64 typedef int SfsName[N_HIERARCHY];
     65 
     66 struct sfsfid {
     67 	int len;
     68 	SfsName path;
     69 };
     70 
     71 struct sfsnode rn;
     72 SfsName sname_root;
     73 struct timespec fstime;
     74 
     75 ino_t nextid = 3;
     76 
     77 #define ISADIR(a) ((SYSCTL_TYPE(a->sysctl_flags) == CTLTYPE_NODE))
     78 #define SFS_MAXFILE 8192
     79 #define SFS_NODEPERDIR 128
     80 
     81 static int sysctlfs_domount(struct puffs_usermount *);
     82 
     83 /*
     84  * build paths.  doesn't support rename (but neither does the fs)
     85  */
     86 static int
     87 sysctlfs_pathbuild(struct puffs_usermount *pu,
     88 	const struct puffs_pathobj *parent, const struct puffs_pathobj *comp,
     89 	size_t offset, struct puffs_pathobj *res)
     90 {
     91 	SfsName *sname;
     92 	size_t clen;
     93 
     94 	assert(parent->po_len < N_HIERARCHY); /* code uses +1 */
     95 
     96 	sname = malloc(sizeof(SfsName));
     97 	assert(sname != NULL);
     98 
     99 	clen = parent->po_len;
    100 	if (comp->po_len == SFSPATH_DOTDOT) {
    101 		assert(clen != 0);
    102 		clen--;
    103 	}
    104 
    105 	memcpy(sname, parent->po_path, clen * sizeof(int));
    106 
    107 	res->po_path = sname;
    108 	res->po_len = clen;
    109 
    110 	return 0;
    111 }
    112 
    113 static int
    114 sysctlfs_pathtransform(struct puffs_usermount *pu,
    115 	const struct puffs_pathobj *p, const const struct puffs_cn *pcn,
    116 	struct puffs_pathobj *res)
    117 {
    118 
    119 	res->po_path = NULL;
    120 	/*
    121 	 * XXX: overload.  prevents us from doing rename, but the fs
    122 	 * (and sysctl(3)) doesn't support it, so no biggie
    123 	 */
    124 	if (PCNISDOTDOT(pcn)) {
    125 		res->po_len = SFSPATH_DOTDOT;
    126 	}else {
    127 		res->po_len = SFSPATH_NORMAL;
    128 	}
    129 
    130 	return 0;
    131 }
    132 
    133 static int
    134 sysctlfs_pathcmp(struct puffs_usermount *pu, struct puffs_pathobj *po1,
    135 	struct puffs_pathobj *po2, size_t clen, int checkprefix)
    136 {
    137 
    138 	if (memcmp(po1->po_path, po2->po_path, clen * sizeof(int)) == 0)
    139 		return 0;
    140 	return 1;
    141 }
    142 
    143 static void
    144 sysctlfs_pathfree(struct puffs_usermount *pu, struct puffs_pathobj *po)
    145 {
    146 
    147 	free(po->po_path);
    148 }
    149 
    150 static struct puffs_node *
    151 getnode(struct puffs_usermount *pu, struct puffs_pathobj *po, int nodetype)
    152 {
    153 	struct sysctlnode sn[SFS_NODEPERDIR];
    154 	struct sysctlnode qnode;
    155 	struct puffs_node *pn;
    156 	struct sfsnode *sfs;
    157 	SfsName myname, *sname;
    158 	size_t sl;
    159 	int i;
    160 
    161 	/*
    162 	 * Check if we need to create a new in-memory node or if we
    163 	 * already have one for this path.  Shortcut for the rootnode.
    164 	 * Also, memcmp against zero-length would be quite true always.
    165 	 */
    166 	if (po->po_len == 0)
    167 		pn = puffs_getroot(pu);
    168 	else
    169 		pn = puffs_pn_nodewalk(pu, puffs_path_walkcmp, po);
    170 
    171 	if (pn == NULL) {
    172 		/*
    173 		 * don't know nodetype?  query...
    174 		 *
    175 		 * XXX1: nothing really guarantees 0 is an invalid nodetype
    176 		 * XXX2: is there really no easier way of doing this?  we
    177 		 *       know the whole mib path
    178 		 */
    179 		if (!nodetype) {
    180 			sname = po->po_path;
    181 			memcpy(myname, po->po_path, po->po_len * sizeof(int));
    182 
    183 			memset(&qnode, 0, sizeof(qnode));
    184 			qnode.sysctl_flags = SYSCTL_VERSION;
    185 			myname[po->po_len-1] = CTL_QUERY;
    186 
    187 			sl = sizeof(sn);
    188 			if (sysctl(myname, po->po_len, sn, &sl,
    189 			    &qnode, sizeof(qnode)) == -1)
    190 				abort();
    191 
    192 			for (i = 0; i < sl / sizeof(struct sysctlnode); i++) {
    193 				 if (sn[i].sysctl_num==(*sname)[po->po_len-1]) {
    194 					nodetype = sn[i].sysctl_flags;
    195 					break;
    196 				}
    197 			}
    198 			if (!nodetype)
    199 				return NULL;
    200 		}
    201 
    202 		sfs = emalloc(sizeof(struct sfsnode));
    203 		sfs->sysctl_flags = nodetype;
    204 		sfs->myid = nextid++;
    205 
    206 		pn = puffs_pn_new(pu, sfs);
    207 		assert(pn);
    208 	}
    209 
    210 	return pn;
    211 }
    212 
    213 int
    214 main(int argc, char *argv[])
    215 {
    216 	struct puffs_usermount *pu;
    217 	struct puffs_ops *pops;
    218 	mntoptparse_t mp;
    219 	int mntflags, pflags, lflags;
    220 	int ch;
    221 
    222 	setprogname(argv[0]);
    223 
    224 	if (argc < 2)
    225 		errx(1, "usage: %s sysctlfs [-o mntopts]mountpath",
    226 		    getprogname());
    227 
    228 	mntflags = pflags = lflags = 0;
    229 	while ((ch = getopt(argc, argv, "o:s")) != -1) {
    230 		switch (ch) {
    231 		case 'o':
    232 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    233 			if (mp == NULL)
    234 				err(1, "getmntopts");
    235 			freemntopts(mp);
    236 			break;
    237 		case 's':
    238 			lflags = PUFFSLOOP_NODAEMON;
    239 			break;
    240 		}
    241 	}
    242 	argv += optind;
    243 	argc -= optind;
    244 	pflags |= PUFFS_FLAG_BUILDPATH | PUFFS_KFLAG_NOCACHE;
    245 
    246 	if (pflags & PUFFS_FLAG_OPDUMP)
    247 		lflags |= PUFFSLOOP_NODAEMON;
    248 
    249 	if (argc != 2)
    250 		errx(1, "usage: %s [-o mntopts]mountpath", getprogname());
    251 
    252 	PUFFSOP_INIT(pops);
    253 
    254 	PUFFSOP_SETFSNOP(pops, unmount);
    255 	PUFFSOP_SETFSNOP(pops, sync);
    256 	PUFFSOP_SETFSNOP(pops, statvfs);
    257 	PUFFSOP_SET(pops, sysctlfs, fs, nodetofh);
    258 	PUFFSOP_SET(pops, sysctlfs, fs, fhtonode);
    259 
    260 	PUFFSOP_SET(pops, sysctlfs, node, lookup);
    261 	PUFFSOP_SET(pops, sysctlfs, node, getattr);
    262 	PUFFSOP_SET(pops, sysctlfs, node, setattr);
    263 	PUFFSOP_SET(pops, sysctlfs, node, readdir);
    264 	PUFFSOP_SET(pops, sysctlfs, node, read);
    265 	PUFFSOP_SET(pops, sysctlfs, node, write);
    266 	PUFFSOP_SET(pops, puffs_genfs, node, reclaim);
    267 
    268 	pu = puffs_init(pops, _PATH_PUFFS, "sysctlfs", NULL, pflags);
    269 	if (pu == NULL)
    270 		err(1, "puffs_init");
    271 
    272 	puffs_set_pathbuild(pu, sysctlfs_pathbuild);
    273 	puffs_set_pathtransform(pu, sysctlfs_pathtransform);
    274 	puffs_set_pathcmp(pu, sysctlfs_pathcmp);
    275 	puffs_set_pathfree(pu, sysctlfs_pathfree);
    276 
    277 	puffs_setfhsize(pu, sizeof(struct sfsfid), PUFFS_FHFLAG_NFSV3);
    278 
    279 	if (sysctlfs_domount(pu) != 0)
    280 		errx(1, "domount");
    281 
    282 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
    283 		err(1, "puffs_mount");
    284 
    285 	if (puffs_mainloop(pu, lflags) == -1)
    286 		err(1, "mainloop");
    287 
    288 	return 0;
    289 }
    290 
    291 static int
    292 sysctlfs_domount(struct puffs_usermount *pu)
    293 {
    294 	struct puffs_pathobj *po_root;
    295 	struct puffs_node *pn_root;
    296 	struct timeval tv_now;
    297 
    298 	rn.myid = 2;
    299 	rn.sysctl_flags = CTLTYPE_NODE;
    300 
    301 	gettimeofday(&tv_now, NULL);
    302 	TIMEVAL_TO_TIMESPEC(&tv_now, &fstime);
    303 
    304 	pn_root = puffs_pn_new(pu, &rn);
    305 	assert(pn_root != NULL);
    306 	puffs_setroot(pu, pn_root);
    307 
    308 	po_root = puffs_getrootpathobj(pu);
    309 	po_root->po_path = &sname_root;
    310 	po_root->po_len = 0;
    311 
    312 	return 0;
    313 }
    314 
    315 int
    316 sysctlfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
    317 	struct puffs_newinfo *pni)
    318 {
    319 	struct puffs_pathobj po;
    320 	struct puffs_node *pn;
    321 	struct sfsnode *sfs;
    322 	struct sfsfid *sfid;
    323 
    324 	sfid = fid;
    325 
    326 	po.po_len = sfid->len;
    327 	po.po_path = &sfid->path;
    328 
    329 	pn = getnode(puffs_cc_getusermount(pcc), &po, 0);
    330 	if (pn == NULL)
    331 		return EINVAL;
    332 	sfs = pn->pn_data;
    333 
    334 	puffs_newinfo_setcookie(pni, pn);
    335 	if (ISADIR(sfs))
    336 		puffs_newinfo_setvtype(pni, VDIR);
    337 	else
    338 		puffs_newinfo_setvtype(pni, VREG);
    339 
    340 	return 0;
    341 }
    342 
    343 int
    344 sysctlfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
    345 	void *fid, size_t *fidsize)
    346 {
    347 	struct puffs_node *pn = cookie;
    348 	struct sfsfid *sfid;
    349 
    350 	sfid = fid;
    351 	sfid->len = PNPLEN(pn);
    352 	memcpy(&sfid->path, PNPATH(pn), sfid->len * sizeof(int));
    353 
    354 	return 0;
    355 }
    356 
    357 static void
    358 doprint(struct sfsnode *sfs, struct puffs_pathobj *po,
    359 	char *buf, size_t bufsize)
    360 {
    361 	size_t sz;
    362 
    363 	assert(!ISADIR(sfs));
    364 
    365 	memset(buf, 0, bufsize);
    366 	switch (SYSCTL_TYPE(sfs->sysctl_flags)) {
    367 	case CTLTYPE_INT: {
    368 		int i;
    369 		sz = sizeof(int);
    370 		if (sysctl(po->po_path, po->po_len, &i, &sz, NULL, 0) == -1)
    371 			break;
    372 		snprintf(buf, bufsize, "%d", i);
    373 		break;
    374 	}
    375 	case CTLTYPE_QUAD: {
    376 		quad_t q;
    377 		sz = sizeof(q);
    378 		if (sysctl(po->po_path, po->po_len, &q, &sz, NULL, 0) == -1)
    379 			break;
    380 		snprintf(buf, bufsize, "%" PRId64, q);
    381 		break;
    382 	}
    383 	case CTLTYPE_STRUCT:
    384 		snprintf(buf, bufsize, "CTLTYPE_STRUCT: implement me and "
    385 		    "score a cookie");
    386 		break;
    387 	case CTLTYPE_STRING: {
    388 		sz = bufsize;
    389 		if (sysctl(po->po_path, po->po_len, buf, &sz, NULL, 0) == -1)
    390 			break;
    391 		break;
    392 	}
    393 	default:
    394 		snprintf(buf, bufsize, "invalid sysctl CTLTYPE");
    395 		break;
    396 	}
    397 }
    398 
    399 static int
    400 getlinks(struct sfsnode *sfs, struct puffs_pathobj *po)
    401 {
    402 	struct sysctlnode sn[SFS_NODEPERDIR];
    403 	struct sysctlnode qnode;
    404 	SfsName *sname;
    405 	size_t sl;
    406 
    407 	if (!ISADIR(sfs))
    408 		return 1;
    409 
    410 	memset(&qnode, 0, sizeof(qnode));
    411 	sl = sizeof(sn);
    412 	qnode.sysctl_flags = SYSCTL_VERSION;
    413 	sname = po->po_path;
    414 	(*sname)[po->po_len] = CTL_QUERY;
    415 
    416 	if (sysctl(*sname, po->po_len + 1, sn, &sl,
    417 	    &qnode, sizeof(qnode)) == -1)
    418 		return 0;
    419 
    420 	return (sl / sizeof(sn[0])) + 2;
    421 }
    422 
    423 static int
    424 getsize(struct sfsnode *sfs, struct puffs_pathobj *po)
    425 {
    426 	char buf[SFS_MAXFILE];
    427 
    428 	if (ISADIR(sfs))
    429 		return getlinks(sfs, po) * 16; /* totally arbitrary */
    430 
    431 	doprint(sfs, po, buf, sizeof(buf));
    432 	return strlen(buf) + 1;
    433 }
    434 
    435 int
    436 sysctlfs_node_lookup(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
    437 	const struct puffs_cn *pcn)
    438 {
    439 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    440 	struct puffs_cn *p2cn = __UNCONST(pcn); /* XXX: fix the interface */
    441 	struct sysctlnode sn[SFS_NODEPERDIR];
    442 	struct sysctlnode qnode;
    443 	struct puffs_node *pn_dir = opc;
    444 	struct puffs_node *pn_new;
    445 	struct sfsnode *sfs_dir = pn_dir->pn_data, *sfs_new;
    446 	SfsName *sname = PCNPATH(pcn);
    447 	size_t sl;
    448 	int i, nodetype;
    449 
    450 	assert(ISADIR(sfs_dir));
    451 
    452 	/*
    453 	 * If we're looking for dotdot, we already have the entire pathname
    454 	 * in sname, courtesy of pathbuild, so we can skip this step.
    455 	 */
    456 	if (!PCNISDOTDOT(pcn)) {
    457 		memset(&qnode, 0, sizeof(qnode));
    458 		sl = SFS_NODEPERDIR * sizeof(struct sysctlnode);
    459 		qnode.sysctl_flags = SYSCTL_VERSION;
    460 		(*sname)[PCNPLEN(pcn)] = CTL_QUERY;
    461 
    462 		if (sysctl(*sname, PCNPLEN(pcn) + 1, sn, &sl,
    463 		    &qnode, sizeof(qnode)) == -1)
    464 			return ENOENT;
    465 
    466 		for (i = 0; i < sl / sizeof(struct sysctlnode); i++)
    467 			if (strcmp(sn[i].sysctl_name, pcn->pcn_name) == 0)
    468 				break;
    469 		if (i == sl / sizeof(struct sysctlnode))
    470 			return ENOENT;
    471 
    472 		(*sname)[PCNPLEN(pcn)] = sn[i].sysctl_num;
    473 		p2cn->pcn_po_full.po_len++;
    474 		nodetype = sn[i].sysctl_flags;
    475 	} else
    476 		nodetype = CTLTYPE_NODE;
    477 
    478 	pn_new = getnode(pu, &p2cn->pcn_po_full, nodetype);
    479 	sfs_new = pn_new->pn_data;
    480 
    481 	puffs_newinfo_setcookie(pni, pn_new);
    482 	if (ISADIR(sfs_new))
    483 		puffs_newinfo_setvtype(pni, VDIR);
    484 	else
    485 		puffs_newinfo_setvtype(pni, VREG);
    486 
    487 	return 0;
    488 }
    489 
    490 int
    491 sysctlfs_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    492 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    493 {
    494 	struct puffs_node *pn = opc;
    495 	struct sfsnode *sfs = pn->pn_data;
    496 
    497 	memset(va, 0, sizeof(struct vattr));
    498 
    499 	if (ISADIR(sfs)) {
    500 		va->va_type = VDIR;
    501 		va->va_mode = 0777;
    502 	} else {
    503 		va->va_type = VREG;
    504 		va->va_mode = 0666;
    505 	}
    506 	va->va_nlink = getlinks(sfs, &pn->pn_po);
    507 	va->va_fileid = sfs->myid;
    508 	va->va_size = getsize(sfs, &pn->pn_po);
    509 	va->va_gen = 1;
    510 	va->va_rdev = PUFFS_VNOVAL;
    511 	va->va_blocksize = 512;
    512 	va->va_filerev = 1;
    513 
    514 	va->va_atime = va->va_mtime = va->va_ctime = va->va_birthtime = fstime;
    515 
    516 	return 0;
    517 }
    518 
    519 int
    520 sysctlfs_node_setattr(struct puffs_cc *pcc, void *opc,
    521 	const struct vattr *va, const struct puffs_cred *pcr,
    522 	const struct puffs_cid *pcid)
    523 {
    524 
    525 	/* dummy, but required for write */
    526 	return 0;
    527 }
    528 
    529 int
    530 sysctlfs_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
    531 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    532 	int *eofflag, off_t *cookies, size_t *ncookies)
    533 {
    534 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    535 	struct sysctlnode sn[SFS_NODEPERDIR];
    536 	struct sysctlnode qnode;
    537 	struct puffs_node *pn_dir = opc;
    538 	struct puffs_node *pn_res;
    539 	struct puffs_pathobj po;
    540 	struct sfsnode *sfs_dir = pn_dir->pn_data, *sfs_ent;
    541 	SfsName *sname;
    542 	size_t sl;
    543 	enum vtype vt;
    544 	ino_t id;
    545 	int i;
    546 
    547 	*ncookies = 0;
    548 
    549  again:
    550 	if (*readoff == DENT_DOT || *readoff == DENT_DOTDOT) {
    551 		puffs_gendotdent(&dent, sfs_dir->myid, *readoff, reslen);
    552 		(*readoff)++;
    553 		PUFFS_STORE_DCOOKIE(cookies, ncookies, *readoff);
    554 		goto again;
    555 	}
    556 
    557 	memset(&qnode, 0, sizeof(qnode));
    558 	sl = SFS_NODEPERDIR * sizeof(struct sysctlnode);
    559 	qnode.sysctl_flags = SYSCTL_VERSION;
    560 	sname = PNPATH(pn_dir);
    561 	(*sname)[PNPLEN(pn_dir)] = CTL_QUERY;
    562 
    563 	if (sysctl(*sname, PNPLEN(pn_dir) + 1, sn, &sl,
    564 	    &qnode, sizeof(qnode)) == -1)
    565 		return ENOENT;
    566 
    567 	po.po_path = sname;
    568 	po.po_len = PNPLEN(pn_dir)+1;
    569 
    570 	for (i = DENT_ADJ(*readoff); i < sl / sizeof(struct sysctlnode); i++) {
    571 		if (SYSCTL_TYPE(sn[i].sysctl_flags) == CTLTYPE_NODE)
    572 			vt = VDIR;
    573 		else
    574 			vt = VREG;
    575 
    576 		/*
    577 		 * check if the node exists.  if so, give it the real
    578 		 * inode number.  otherwise just fake it.
    579 		 */
    580 		(*sname)[PNPLEN(pn_dir)] = sn[i].sysctl_num;
    581 		pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp, &po);
    582 		if (pn_res) {
    583 			sfs_ent = pn_res->pn_data;
    584 			id = sfs_ent->myid;
    585 		} else {
    586 			id = nextid++;
    587 		}
    588 
    589 		if (!puffs_nextdent(&dent, sn[i].sysctl_name, id,
    590 		    puffs_vtype2dt(vt), reslen))
    591 			return 0;
    592 
    593 		(*readoff)++;
    594 		PUFFS_STORE_DCOOKIE(cookies, ncookies, *readoff);
    595 	}
    596 
    597 	*eofflag = 1;
    598 	return 0;
    599 }
    600 
    601 int
    602 sysctlfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    603 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    604 	int ioflag)
    605 {
    606 	char localbuf[SFS_MAXFILE];
    607 	struct puffs_node *pn = opc;
    608 	struct sfsnode *sfs = pn->pn_data;
    609 	int xfer;
    610 
    611 	if (ISADIR(sfs))
    612 		return EISDIR;
    613 
    614 	doprint(sfs, &pn->pn_po, localbuf, sizeof(localbuf));
    615 	xfer = MIN(*resid, strlen(localbuf) - offset);
    616 
    617 	if (xfer <= 0)
    618 		return 0;
    619 
    620 	memcpy(buf, localbuf + offset, xfer);
    621 	*resid -= xfer;
    622 
    623 	if (*resid) {
    624 		buf[xfer] = '\n';
    625 		(*resid)--;
    626 	}
    627 
    628 	return 0;
    629 }
    630 
    631 int
    632 sysctlfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    633 	off_t offset, size_t *resid, const struct puffs_cred *cred,
    634 	int ioflag)
    635 {
    636 	struct puffs_node *pn = opc;
    637 	struct sfsnode *sfs = pn->pn_data;
    638 	long long ll;
    639 	int i, rv;
    640 
    641 	if (puffs_cred_isjuggernaut(cred) == 0)
    642 		return EACCES;
    643 
    644 	if (ISADIR(sfs))
    645 		return EISDIR;
    646 
    647 	if (offset != 0)
    648 		return EINVAL;
    649 
    650 	if (ioflag & PUFFS_IO_APPEND)
    651 		return EINVAL;
    652 
    653 	switch (SYSCTL_TYPE(sfs->sysctl_flags)) {
    654 	case CTLTYPE_INT:
    655 		if (sscanf((const char *)buf, "%d", &i) != 1)
    656 			return EINVAL;
    657 		rv = sysctl(PNPATH(pn), PNPLEN(pn), NULL, NULL,
    658 		    &i, sizeof(int));
    659 		break;
    660 	case CTLTYPE_QUAD:
    661 		if (sscanf((const char *)buf, "%lld", &ll) != 1)
    662 			return EINVAL;
    663 		rv =  sysctl(PNPATH(pn), PNPLEN(pn), NULL, NULL,
    664 		    &ll, sizeof(long long));
    665 		break;
    666 	case CTLTYPE_STRING:
    667 		rv = sysctl(PNPATH(pn), PNPLEN(pn), NULL, NULL, buf, *resid);
    668 		break;
    669 	default:
    670 		rv = EINVAL;
    671 		break;
    672 	}
    673 
    674 	if (rv)
    675 		return rv;
    676 
    677 	*resid = 0;
    678 	return 0;
    679 }
    680