Skip to content

Instantly share code, notes, and snippets.

@leiless
Created October 13, 2018 14:33
Show Gist options
  • Save leiless/c66babd42e943b91b708907d8f1c33c9 to your computer and use it in GitHub Desktop.
Save leiless/c66babd42e943b91b708907d8f1c33c9 to your computer and use it in GitHub Desktop.
/*
* Created 181013
*/
#include <mach/mach_types.h>
#include <libkern/libkern.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#define KEXTNAME_S "fs_iterator"
#define LOG(fmt, ...) printf(KEXTNAME_S ": " fmt "\n", ##__VA_ARGS__)
static uint64_t vn_cnt = 0;
static int vnode_callout(struct vnode *vp __unused, void *arg __unused)
{
vn_cnt++;
return VNODE_RETURNED;
}
static int mount_callout(struct mount *mp, void *arg __unused)
{
char name[MFSNAMELEN];
vfs_name(mp, name);
int typeno = vfs_typenum(mp);
uint64_t flags = vfs_flags(mp);
int isrdwr = vfs_isrdwr(mp);
int isrdonly = vfs_isrdonly(mp);
int isforce = vfs_isforce(mp);
int isunmount = vfs_isunmount(mp);
int isupdate = vfs_isupdate(mp);
int isreload = vfs_isreload(mp);
int issynchronous = vfs_issynchronous(mp);
int iswriteupgrade = vfs_iswriteupgrade(mp);
LOG("name: %s type: %#x flags: %#llx rdwr: %d "
"rdonly: %d force: %d unmount: %d update: %d "
"reload: %d synchronous: %d writeupgrade: %d",
name, typeno, flags, isrdwr, isrdonly, isforce, isunmount,
isupdate, isreload, issynchronous, iswriteupgrade);
struct timeval tv;
microtime(&tv);
LOG("%ld.%d", tv.tv_sec, tv.tv_usec / 1000);
int e = vnode_iterate(mp, VNODE_ITERATE_ALL, vnode_callout, NULL);
if (e != 0) LOG("vnode_iterate() failure errno: %d", e);
microtime(&tv);
LOG("%ld.%d", tv.tv_sec, tv.tv_usec / 1000);
LOG("%llu vnodes", vn_cnt);
return VFS_RETURNED_DONE;
}
kern_return_t fs_iterator_start(kmod_info_t *ki __unused, void *d __unused)
{
int e = vfs_iterate(0, mount_callout, NULL);
if (e != 0) LOG("vfs_iterate() failure errno: %d", e);
return KERN_FAILURE;
}
kern_return_t fs_iterator_stop(kmod_info_t *ki __unused, void *d __unused)
{
return KERN_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment