#include // printk() #include #include "defines.h" #include "kfile.h" #include "ksync.h" static unsigned int g_nLocks = 0; ///////////////////////////////////////////////////////////////////////////// static long _vfs_ioctl(struct file *pf, unsigned int cmd, unsigned long arg) { int ret = -ENOTTY; if(pf->f_op->unlocked_ioctl) { ret = pf->f_op->unlocked_ioctl(pf, cmd, arg); if(ret == -ENOIOCTLCMD) ret = -ENOTTY; } return ret; } ///////////////////////////////////////////////////////////////////////////// struct file* kf_open(const char *path, int flags, int rights) { struct file *pf = NULL; mm_segment_t oldfs; int err = 0; oldfs = get_fs(); set_fs(get_ds()); pf = filp_open(path, flags, rights); set_fs(oldfs); if(IS_ERR(pf)) { err = PTR_ERR(pf); set_fs(oldfs); return NULL; } return pf; } ///////////////////////////////////////////////////////////////////////////// struct file* kf_open_locked(const char *path, int flags, int rights) { struct file *pf = NULL; kfile_lock(); if(!(pf = kf_open(path, flags, rights))) kfile_unlock(); ++g_nLocks; return pf; } ///////////////////////////////////////////////////////////////////////////// void kf_close(struct file *pf) { if(pf) { filp_close(pf, NULL); if(g_nLocks > 0) { --g_nLocks; kfile_unlock(); } } } ///////////////////////////////////////////////////////////////////////////// int kf_read(struct file *pf, unsigned long long offset, unsigned char *data, unsigned int size) { int ret; mm_segment_t oldfs; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_read(pf, data, size, &offset); set_fs(oldfs); return ret; } ///////////////////////////////////////////////////////////////////////////// int kf_write(struct file *pf, unsigned long long offset, unsigned char *data, unsigned int size) { int ret; mm_segment_t oldfs; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_write(pf, data, size, &offset); set_fs(oldfs); return ret; } ///////////////////////////////////////////////////////////////////////////// long kf_ioctl(struct file *pf, unsigned int cmd, unsigned long arg) { long ret; mm_segment_t oldfs; oldfs = get_fs(); set_fs(get_ds()); ret = _vfs_ioctl(pf, cmd, arg); set_fs(oldfs); return ret; } ///////////////////////////////////////////////////////////////////////////// int kf_sync(struct file *pf) { vfs_fsync(pf, 0); return 0; }