add -lminixfs with fs support routines

. move cache size heuristic from mfs there
	  so mfs and ext2 can share it
	. add vfs credentials retrieving function, with
	  backwards compatability from previous struct
	  format, to be used by both ext2 and mfs
	. fix for ext2 - STATICINIT was fed no.
	  of bytes instead of no. of elements, overallocating
	  memory by a megabyte or two for the superblock
This commit is contained in:
Ben Gras
2011-09-08 16:49:54 +00:00
parent ce1a2793f9
commit 4857d5d554
19 changed files with 202 additions and 91 deletions

View File

@@ -18,7 +18,7 @@ SUBDIR= csu ${LIBCOMPAT_DIR} ${LIBC_DIR} libdriver libnetdriver \
libedit ${LIBM_DIR} libsys libtimers libminixutil libbz2 libl libhgfs \
libz libfetch libarchive libvtreefs libaudiodriver libmthread \
libexec libdevman libusb ${LIBMINLIB_DIR} ${LIBASYN_DIR} \
libddekit
libddekit libminixfs
.if defined(NBSD_LIBC) && (${NBSD_LIBC} != "no")
SUBDIR+= libelf libminc libcrypt libterminfo libcurses libvassert

8
lib/libminixfs/Makefile Normal file
View File

@@ -0,0 +1,8 @@
# Makefile for libminixfs
.include <bsd.own.mk>
LIB= minixfs
SRCS= fetch_credentials.c cache.c
.include <bsd.lib.mk>

59
lib/libminixfs/cache.c Normal file
View File

@@ -0,0 +1,59 @@
#define _SYSTEM
#include <minix/libminixfs.h>
#include <minix/dmap.h>
#include <minix/u64.h>
#include <sys/param.h>
#include <errno.h>
u32_t fs_bufs_heuristic(int minbufs, u32_t btotal, u32_t bfree,
int blocksize, dev_t majordev)
{
struct vm_stats_info vsi;
int bufs;
u32_t kbytes_used_fs, kbytes_total_fs, kbcache, kb_fsmax;
u32_t kbytes_remain_mem, bused;
bused = btotal-bfree;
/* but we simply need minbufs no matter what, and we don't
* want more than that if we're a memory device
*/
if(majordev == MEMORY_MAJOR) {
return minbufs;
}
/* set a reasonable cache size; cache at most a certain
* portion of the used FS, and at most a certain %age of remaining
* memory
*/
if((vm_info_stats(&vsi) != OK)) {
bufs = 1024;
printf("fslib: heuristic info fail: default to %d bufs\n", bufs);
return bufs;
}
kbytes_remain_mem = div64u(mul64u(vsi.vsi_free, vsi.vsi_pagesize), 1024);
/* check fs usage. */
kbytes_used_fs = div64u(mul64u(bused, blocksize), 1024);
kbytes_total_fs = div64u(mul64u(btotal, blocksize), 1024);
/* heuristic for a desired cache size based on FS usage;
* but never bigger than half of the total filesystem
*/
kb_fsmax = sqrt_approx(kbytes_used_fs)*40;
kb_fsmax = MIN(kb_fsmax, kbytes_total_fs/2);
/* heuristic for a maximum usage - 10% of remaining memory */
kbcache = MIN(kbytes_remain_mem/10, kb_fsmax);
bufs = kbcache * 1024 / blocksize;
/* but we simply need MINBUFS no matter what */
if(bufs < minbufs)
bufs = minbufs;
return bufs;
}

View File

@@ -0,0 +1,51 @@
#include <minix/vfsif.h>
#include <minix/type.h>
#include <assert.h>
#include "minixfs.h"
int fs_lookup_credentials(vfs_ucred_t *credentials,
uid_t *caller_uid, gid_t *caller_gid, cp_grant_id_t grant2, size_t cred_size)
{
vfs_ucred_old_t old_cred;
int r;
memset(credentials, 0, sizeof(*credentials));
if(cred_size == sizeof(*credentials)) {
r = sys_safecopyfrom(VFS_PROC_NR, grant2, (vir_bytes) 0,
(vir_bytes) credentials, cred_size, D);
if (r != OK) {
printf("FS: cred copy (regular) failed.\n");
return(r);
}
} else if(cred_size == sizeof(old_cred)) {
int g;
r = sys_safecopyfrom(VFS_PROC_NR, grant2, (vir_bytes) 0,
(vir_bytes) &old_cred, sizeof(old_cred), D);
if (r != OK) {
printf("FS: cred copy (fallback) failed.\n");
return(r);
}
credentials->vu_ngroups = old_cred.vu_ngroups;
credentials->vu_uid = old_cred.vu_uid;
credentials->vu_gid = old_cred.vu_gid;
for(g = 0; g < NGROUPS_MAX_OLD; g++) {
assert(g < NGROUPS_MAX);
credentials->vu_sgroups[g] = old_cred.vu_sgroups[g];
}
} else {
static int w = 0;
if(!w) { printf("FS: cred size incompatible with VFS.\n"); w = 1; }
return(EINVAL); /* Wrong size. */
}
assert(credentials->vu_ngroups <= NGROUPS_MAX);
*caller_uid = credentials->vu_uid;
*caller_gid = credentials->vu_gid;
return OK;
}

4
lib/libminixfs/minixfs.h Normal file
View File

@@ -0,0 +1,4 @@
#define _SYSTEM
#include <lib.h> /* common to all libraries */
#include <minix/com.h> /* need task numbers + message types */