123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <linux/kernel.h> // printk()
- #include <linux/syscalls.h>
- #include "kfile.h"
- /////////////////////////////////////////////////////////////////////////////
- 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;
- }
- /////////////////////////////////////////////////////////////////////////////
- void kf_close(struct file *pf)
- {
- if(pf)
- filp_close(pf, NULL);
- }
- /////////////////////////////////////////////////////////////////////////////
- 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;
- }
|